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.
48 #include <sys/fs/zfs.h>
50 #include <sys/fm/fs/zfs.h>
51 #include <sys/fm/util.h>
52 #include <sys/fm/protocol.h>
53 #include <sys/zfs_ioctl.h>
58 #include "zpool_util.h"
59 #include "zfs_comutil.h"
60 #include "zfeature_common.h"
62 #include "statcommon.h"
64 static int zpool_do_create(int, char **);
65 static int zpool_do_destroy(int, char **);
67 static int zpool_do_add(int, char **);
68 static int zpool_do_remove(int, char **);
69 static int zpool_do_labelclear(int, char **);
71 static int zpool_do_list(int, char **);
72 static int zpool_do_iostat(int, char **);
73 static int zpool_do_status(int, char **);
75 static int zpool_do_online(int, char **);
76 static int zpool_do_offline(int, char **);
77 static int zpool_do_clear(int, char **);
78 static int zpool_do_reopen(int, char **);
80 static int zpool_do_reguid(int, char **);
82 static int zpool_do_attach(int, char **);
83 static int zpool_do_detach(int, char **);
84 static int zpool_do_replace(int, char **);
85 static int zpool_do_split(int, char **);
87 static int zpool_do_scrub(int, char **);
89 static int zpool_do_import(int, char **);
90 static int zpool_do_export(int, char **);
92 static int zpool_do_upgrade(int, char **);
94 static int zpool_do_history(int, char **);
95 static int zpool_do_events(int, char **);
97 static int zpool_do_get(int, char **);
98 static int zpool_do_set(int, char **);
101 * These libumem hooks provide a reasonable set of defaults for the allocator's
102 * debugging facilities.
107 _umem_debug_init(void)
109 return ("default,verbose"); /* $UMEM_DEBUG setting */
113 _umem_logging_init(void)
115 return ("fail,contents"); /* $UMEM_LOGGING setting */
149 * Flags for stats to display with "zpool iostats"
157 IOS_COUNT
, /* always last element */
160 /* iostat_type entries as bitmasks */
161 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
162 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
163 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
164 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
165 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
167 /* Mask of all the histo bits */
168 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
171 * Lookup table for iostat flags to nvlist names. Basically a list
172 * of all the nvlists a flag requires. Also specifies the order in
173 * which data gets printed in zpool iostat.
175 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
177 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
178 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
179 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
180 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
181 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
182 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
183 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
184 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
185 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
188 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
189 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
190 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
191 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
194 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
195 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
196 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
197 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
198 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
201 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
202 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
203 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
204 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
205 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
206 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
207 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
208 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
209 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
210 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
216 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
217 * Right now, only one histo bit is ever set at one time, so we can
218 * just do a highbit64(a)
220 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
222 typedef struct zpool_command
{
224 int (*func
)(int, char **);
229 * Master command table. Each ZFS command has a name, associated function, and
230 * usage message. The usage messages need to be internationalized, so we have
231 * to have a function to return the usage message based on a command index.
233 * These commands are organized according to how they are displayed in the usage
234 * message. An empty command (one with a NULL name) indicates an empty line in
235 * the generic usage message.
237 static zpool_command_t command_table
[] = {
238 { "create", zpool_do_create
, HELP_CREATE
},
239 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
241 { "add", zpool_do_add
, HELP_ADD
},
242 { "remove", zpool_do_remove
, HELP_REMOVE
},
244 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
246 { "list", zpool_do_list
, HELP_LIST
},
247 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
248 { "status", zpool_do_status
, HELP_STATUS
},
250 { "online", zpool_do_online
, HELP_ONLINE
},
251 { "offline", zpool_do_offline
, HELP_OFFLINE
},
252 { "clear", zpool_do_clear
, HELP_CLEAR
},
253 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
255 { "attach", zpool_do_attach
, HELP_ATTACH
},
256 { "detach", zpool_do_detach
, HELP_DETACH
},
257 { "replace", zpool_do_replace
, HELP_REPLACE
},
258 { "split", zpool_do_split
, HELP_SPLIT
},
260 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
262 { "import", zpool_do_import
, HELP_IMPORT
},
263 { "export", zpool_do_export
, HELP_EXPORT
},
264 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
265 { "reguid", zpool_do_reguid
, HELP_REGUID
},
267 { "history", zpool_do_history
, HELP_HISTORY
},
268 { "events", zpool_do_events
, HELP_EVENTS
},
270 { "get", zpool_do_get
, HELP_GET
},
271 { "set", zpool_do_set
, HELP_SET
},
274 #define NCOMMAND (ARRAY_SIZE(command_table))
276 static zpool_command_t
*current_command
;
277 static char history_str
[HIS_MAX_RECORD_LEN
];
278 static boolean_t log_history
= B_TRUE
;
279 static uint_t timestamp_fmt
= NODATE
;
282 get_usage(zpool_help_t idx
) {
285 return (gettext("\tadd [-fgLnP] [-o property=value] "
286 "<pool> <vdev> ...\n"));
288 return (gettext("\tattach [-f] [-o property=value] "
289 "<pool> <device> <new-device>\n"));
291 return (gettext("\tclear [-nF] <pool> [device]\n"));
293 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
294 "\t [-O file-system-property=value] ... \n"
295 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
297 return (gettext("\tdestroy [-f] <pool>\n"));
299 return (gettext("\tdetach <pool> <device>\n"));
301 return (gettext("\texport [-af] <pool> ...\n"));
303 return (gettext("\thistory [-il] [<pool>] ...\n"));
305 return (gettext("\timport [-d dir] [-D]\n"
306 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
307 "\timport [-o mntopts] [-o property=value] ... \n"
308 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
309 "[-R root] [-F [-n]] -a\n"
310 "\timport [-o mntopts] [-o property=value] ... \n"
311 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
312 "[-R root] [-F [-n]]\n"
313 "\t <pool | id> [newpool]\n"));
315 return (gettext("\tiostat [-T d | u] [-ghHLpPvy] "
317 "\t [[pool ...]|[pool vdev ...]|[vdev ...]] "
318 "[interval [count]]\n"));
319 case HELP_LABELCLEAR
:
320 return (gettext("\tlabelclear [-f] <vdev>\n"));
322 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
323 "[-T d|u] [pool] ... [interval [count]]\n"));
325 return (gettext("\toffline [-t] <pool> <device> ...\n"));
327 return (gettext("\tonline <pool> <device> ...\n"));
329 return (gettext("\treplace [-f] [-o property=value] "
330 "<pool> <device> [new-device]\n"));
332 return (gettext("\tremove <pool> <device> ...\n"));
334 return (gettext("\treopen <pool>\n"));
336 return (gettext("\tscrub [-s] <pool> ...\n"));
338 return (gettext("\tstatus [-gLPvxD] [-T d|u] [pool] ... "
339 "[interval [count]]\n"));
341 return (gettext("\tupgrade\n"
343 "\tupgrade [-V version] <-a | pool ...>\n"));
345 return (gettext("\tevents [-vHfc]\n"));
347 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
348 "<\"all\" | property[,...]> <pool> ...\n"));
350 return (gettext("\tset <property=value> <pool> \n"));
352 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
353 "\t [-o property=value] <pool> <newpool> "
354 "[<device> ...]\n"));
356 return (gettext("\treguid <pool>\n"));
365 * Callback routine that will print out a pool property value.
368 print_prop_cb(int prop
, void *cb
)
372 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
374 if (zpool_prop_readonly(prop
))
375 (void) fprintf(fp
, " NO ");
377 (void) fprintf(fp
, " YES ");
379 if (zpool_prop_values(prop
) == NULL
)
380 (void) fprintf(fp
, "-\n");
382 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
388 * Display usage message. If we're inside a command, display only the usage for
389 * that command. Otherwise, iterate over the entire command table and display
390 * a complete usage message.
393 usage(boolean_t requested
)
395 FILE *fp
= requested
? stdout
: stderr
;
397 if (current_command
== NULL
) {
400 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
402 gettext("where 'command' is one of the following:\n\n"));
404 for (i
= 0; i
< NCOMMAND
; i
++) {
405 if (command_table
[i
].name
== NULL
)
406 (void) fprintf(fp
, "\n");
408 (void) fprintf(fp
, "%s",
409 get_usage(command_table
[i
].usage
));
412 (void) fprintf(fp
, gettext("usage:\n"));
413 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
416 if (current_command
!= NULL
&&
417 ((strcmp(current_command
->name
, "set") == 0) ||
418 (strcmp(current_command
->name
, "get") == 0) ||
419 (strcmp(current_command
->name
, "list") == 0))) {
422 gettext("\nthe following properties are supported:\n"));
424 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
425 "PROPERTY", "EDIT", "VALUES");
427 /* Iterate over all properties */
428 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
431 (void) fprintf(fp
, "\t%-15s ", "feature@...");
432 (void) fprintf(fp
, "YES disabled | enabled | active\n");
434 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
435 "appended with a feature name.\nSee zpool-features(5).\n"));
439 * See comments at end of main().
441 if (getenv("ZFS_ABORT") != NULL
) {
442 (void) printf("dumping core by request\n");
446 exit(requested
? 0 : 2);
450 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
451 boolean_t print_logs
, int name_flags
)
458 (void) printf("\t%*s%s\n", indent
, "", name
);
460 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
461 &child
, &children
) != 0)
464 for (c
= 0; c
< children
; c
++) {
465 uint64_t is_log
= B_FALSE
;
467 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
469 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
472 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
473 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
474 B_FALSE
, name_flags
);
480 prop_list_contains_feature(nvlist_t
*proplist
)
483 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
484 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
485 if (zpool_prop_feature(nvpair_name(nvp
)))
492 * Add a property pair (name, string-value) into a property nvlist.
495 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
498 zpool_prop_t prop
= ZPROP_INVAL
;
504 if (*props
== NULL
&&
505 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
506 (void) fprintf(stderr
,
507 gettext("internal error: out of memory\n"));
514 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
516 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
517 !zpool_prop_feature(propname
)) {
518 (void) fprintf(stderr
, gettext("property '%s' is "
519 "not a valid pool property\n"), propname
);
524 * feature@ properties and version should not be specified
527 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
528 nvlist_exists(proplist
, vname
)) ||
529 (prop
== ZPOOL_PROP_VERSION
&&
530 prop_list_contains_feature(proplist
))) {
531 (void) fprintf(stderr
, gettext("'feature@' and "
532 "'version' properties cannot be specified "
538 if (zpool_prop_feature(propname
))
541 normnm
= zpool_prop_to_name(prop
);
543 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
544 normnm
= zfs_prop_to_name(fprop
);
550 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
551 prop
!= ZPOOL_PROP_CACHEFILE
) {
552 (void) fprintf(stderr
, gettext("property '%s' "
553 "specified multiple times\n"), propname
);
557 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
558 (void) fprintf(stderr
, gettext("internal "
559 "error: out of memory\n"));
567 * Set a default property pair (name, string-value) in a property nvlist
570 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
575 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
578 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
582 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
584 * -f Force addition of devices, even if they appear in use
585 * -g Display guid for individual vdev name.
586 * -L Follow links when resolving vdev path name.
587 * -n Do not add the devices, but display the resulting layout if
588 * they were to be added.
589 * -o Set property=value.
590 * -P Display full path for vdev name.
592 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
593 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
597 zpool_do_add(int argc
, char **argv
)
599 boolean_t force
= B_FALSE
;
600 boolean_t dryrun
= B_FALSE
;
608 nvlist_t
*props
= NULL
;
612 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
618 name_flags
|= VDEV_NAME_GUID
;
621 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
627 if ((propval
= strchr(optarg
, '=')) == NULL
) {
628 (void) fprintf(stderr
, gettext("missing "
629 "'=' for -o option\n"));
635 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
636 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
640 name_flags
|= VDEV_NAME_PATH
;
643 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
652 /* get pool name and check number of arguments */
654 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
658 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
667 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
670 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
671 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
677 /* pass off to get_vdev_spec for processing */
678 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
680 if (nvroot
== NULL
) {
686 nvlist_t
*poolnvroot
;
688 uint_t l2children
, c
;
690 boolean_t hadcache
= B_FALSE
;
692 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
695 (void) printf(gettext("would update '%s' to the following "
696 "configuration:\n"), zpool_get_name(zhp
));
698 /* print original main pool and new tree */
699 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
701 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
703 /* Do the same for the logs */
704 if (num_logs(poolnvroot
) > 0) {
705 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
707 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
709 } else if (num_logs(nvroot
) > 0) {
710 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
714 /* Do the same for the caches */
715 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
716 &l2child
, &l2children
) == 0 && l2children
) {
718 (void) printf(gettext("\tcache\n"));
719 for (c
= 0; c
< l2children
; c
++) {
720 vname
= zpool_vdev_name(g_zfs
, NULL
,
721 l2child
[c
], name_flags
);
722 (void) printf("\t %s\n", vname
);
726 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
727 &l2child
, &l2children
) == 0 && l2children
) {
729 (void) printf(gettext("\tcache\n"));
730 for (c
= 0; c
< l2children
; c
++) {
731 vname
= zpool_vdev_name(g_zfs
, NULL
,
732 l2child
[c
], name_flags
);
733 (void) printf("\t %s\n", vname
);
740 ret
= (zpool_add(zhp
, nvroot
) != 0);
751 * zpool remove <pool> <vdev> ...
753 * Removes the given vdev from the pool. Currently, this supports removing
754 * spares, cache, and log devices from the pool.
757 zpool_do_remove(int argc
, char **argv
)
761 zpool_handle_t
*zhp
= NULL
;
766 /* get pool name and check number of arguments */
768 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
772 (void) fprintf(stderr
, gettext("missing device\n"));
778 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
781 for (i
= 1; i
< argc
; i
++) {
782 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
791 * zpool labelclear <vdev>
793 * Verifies that the vdev is not active and zeros out the label information
797 zpool_do_labelclear(int argc
, char **argv
)
800 int c
, fd
= -1, ret
= 0;
802 boolean_t inuse
= B_FALSE
;
803 boolean_t force
= B_FALSE
;
806 while ((c
= getopt(argc
, argv
, "f")) != -1) {
812 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
823 (void) fprintf(stderr
, gettext("missing vdev device name\n"));
828 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
829 (void) fprintf(stderr
, gettext("Unable to open %s\n"), vdev
);
834 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0) {
838 (void) fprintf(stderr
,
839 gettext("Unable to determine pool state for %s\n"
840 "Use -f to force the clearing any label data\n"), vdev
);
848 case POOL_STATE_ACTIVE
:
849 case POOL_STATE_SPARE
:
850 case POOL_STATE_L2CACHE
:
851 (void) fprintf(stderr
,
852 gettext("labelclear operation failed.\n"
853 "\tVdev %s is a member (%s), of pool \"%s\".\n"
854 "\tTo remove label information from this device, "
855 "export or destroy\n\tthe pool, or remove %s from "
856 "the configuration of this pool\n\tand retry the "
857 "labelclear operation.\n"),
858 vdev
, zpool_pool_state_to_name(state
), name
, vdev
);
862 case POOL_STATE_EXPORTED
:
866 (void) fprintf(stderr
,
867 gettext("labelclear operation failed.\n\tVdev "
868 "%s is a member of the exported pool \"%s\".\n"
869 "\tUse \"zpool labelclear -f %s\" to force the "
870 "removal of label\n\tinformation.\n"),
875 case POOL_STATE_POTENTIALLY_ACTIVE
:
879 (void) fprintf(stderr
,
880 gettext("labelclear operation failed.\n"
881 "\tVdev %s is a member of the pool \"%s\".\n"
882 "\tThis pool is unknown to this system, but may "
883 "be active on\n\tanother system. Use "
884 "\'zpool labelclear -f %s\' to force the\n"
885 "\tremoval of label information.\n"),
890 case POOL_STATE_DESTROYED
:
891 /* inuse should never be set for a destroyed pool... */
897 if (zpool_clear_label(fd
) != 0) {
898 (void) fprintf(stderr
,
899 gettext("Label clear failed on vdev %s\n"), vdev
);
912 * zpool create [-fnd] [-o property=value] ...
913 * [-O file-system-property=value] ...
914 * [-R root] [-m mountpoint] <pool> <dev> ...
916 * -f Force creation, even if devices appear in use
917 * -n Do not create the pool, but display the resulting layout if it
918 * were to be created.
919 * -R Create a pool under an alternate root
920 * -m Set default mountpoint for the root dataset. By default it's
922 * -o Set property=value.
923 * -o Set feature@feature=enabled|disabled.
924 * -d Don't automatically enable all supported pool features
925 * (individual features can be enabled with -o).
926 * -O Set fsproperty=value in the pool's root file system
928 * Creates the named pool according to the given vdev specification. The
929 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
930 * we get the nvlist back from get_vdev_spec(), we either print out the contents
931 * (if '-n' was specified), or pass it to libzfs to do the creation.
934 zpool_do_create(int argc
, char **argv
)
936 boolean_t force
= B_FALSE
;
937 boolean_t dryrun
= B_FALSE
;
938 boolean_t enable_all_pool_feat
= B_TRUE
;
940 nvlist_t
*nvroot
= NULL
;
944 char *altroot
= NULL
;
945 char *mountpoint
= NULL
;
946 nvlist_t
*fsprops
= NULL
;
947 nvlist_t
*props
= NULL
;
951 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
960 enable_all_pool_feat
= B_FALSE
;
964 if (add_prop_list(zpool_prop_to_name(
965 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
967 if (add_prop_list_default(zpool_prop_to_name(
968 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
972 /* Equivalent to -O mountpoint=optarg */
976 if ((propval
= strchr(optarg
, '=')) == NULL
) {
977 (void) fprintf(stderr
, gettext("missing "
978 "'=' for -o option\n"));
984 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
988 * If the user is creating a pool that doesn't support
989 * feature flags, don't enable any features.
991 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
995 ver
= strtoull(propval
, &end
, 10);
997 ver
< SPA_VERSION_FEATURES
) {
998 enable_all_pool_feat
= B_FALSE
;
1001 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1005 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1006 (void) fprintf(stderr
, gettext("missing "
1007 "'=' for -O option\n"));
1014 * Mountpoints are checked and then added later.
1015 * Uniquely among properties, they can be specified
1016 * more than once, to avoid conflict with -m.
1018 if (0 == strcmp(optarg
,
1019 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1020 mountpoint
= propval
;
1021 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1028 * Sanity check temporary pool name.
1030 if (strchr(optarg
, '/') != NULL
) {
1031 (void) fprintf(stderr
, gettext("cannot create "
1032 "'%s': invalid character '/' in temporary "
1034 (void) fprintf(stderr
, gettext("use 'zfs "
1035 "create' to create a dataset\n"));
1039 if (add_prop_list(zpool_prop_to_name(
1040 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1042 if (add_prop_list_default(zpool_prop_to_name(
1043 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1048 (void) fprintf(stderr
, gettext("missing argument for "
1049 "'%c' option\n"), optopt
);
1052 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1061 /* get pool name and check number of arguments */
1063 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1067 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1074 * As a special case, check for use of '/' in the name, and direct the
1075 * user to use 'zfs create' instead.
1077 if (strchr(poolname
, '/') != NULL
) {
1078 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1079 "character '/' in pool name\n"), poolname
);
1080 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1081 "create a dataset\n"));
1085 /* pass off to get_vdev_spec for bulk processing */
1086 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1087 argc
- 1, argv
+ 1);
1091 /* make_root_vdev() allows 0 toplevel children if there are spares */
1092 if (!zfs_allocatable_devs(nvroot
)) {
1093 (void) fprintf(stderr
, gettext("invalid vdev "
1094 "specification: at least one toplevel vdev must be "
1099 if (altroot
!= NULL
&& altroot
[0] != '/') {
1100 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1101 "must be an absolute path\n"), altroot
);
1106 * Check the validity of the mountpoint and direct the user to use the
1107 * '-m' mountpoint option if it looks like its in use.
1109 if (mountpoint
== NULL
||
1110 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1111 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1112 char buf
[MAXPATHLEN
];
1115 if (mountpoint
&& mountpoint
[0] != '/') {
1116 (void) fprintf(stderr
, gettext("invalid mountpoint "
1117 "'%s': must be an absolute path, 'legacy', or "
1118 "'none'\n"), mountpoint
);
1122 if (mountpoint
== NULL
) {
1123 if (altroot
!= NULL
)
1124 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1127 (void) snprintf(buf
, sizeof (buf
), "/%s",
1130 if (altroot
!= NULL
)
1131 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1132 altroot
, mountpoint
);
1134 (void) snprintf(buf
, sizeof (buf
), "%s",
1138 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1139 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1140 "%s\n"), buf
, strerror(errno
));
1141 (void) fprintf(stderr
, gettext("use '-m' "
1142 "option to provide a different default\n"));
1147 while (count
< 3 && readdir(dirp
) != NULL
)
1149 (void) closedir(dirp
);
1152 (void) fprintf(stderr
, gettext("mountpoint "
1153 "'%s' exists and is not empty\n"), buf
);
1154 (void) fprintf(stderr
, gettext("use '-m' "
1155 "option to provide a "
1156 "different default\n"));
1163 * Now that the mountpoint's validity has been checked, ensure that
1164 * the property is set appropriately prior to creating the pool.
1166 if (mountpoint
!= NULL
) {
1167 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1168 mountpoint
, &fsprops
, B_FALSE
);
1176 * For a dry run invocation, print out a basic message and run
1177 * through all the vdevs in the list and print out in an
1178 * appropriate hierarchy.
1180 (void) printf(gettext("would create '%s' with the "
1181 "following layout:\n\n"), poolname
);
1183 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1184 if (num_logs(nvroot
) > 0)
1185 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1190 * Hand off to libzfs.
1193 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1194 char propname
[MAXPATHLEN
];
1196 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1198 (void) snprintf(propname
, sizeof (propname
),
1199 "feature@%s", feat
->fi_uname
);
1202 * Only features contained in props will be enabled:
1203 * remove from the nvlist every ZFS_FEATURE_DISABLED
1204 * value and add every missing ZFS_FEATURE_ENABLED if
1205 * enable_all_pool_feat is set.
1207 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1208 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1209 (void) nvlist_remove_all(props
,
1211 } else if (enable_all_pool_feat
) {
1212 ret
= add_prop_list(propname
,
1213 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1220 if (zpool_create(g_zfs
, poolname
,
1221 nvroot
, props
, fsprops
) == 0) {
1222 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1223 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1225 if (zfs_mount(pool
, NULL
, 0) == 0)
1226 ret
= zfs_shareall(pool
);
1229 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1230 (void) fprintf(stderr
, gettext("pool name may have "
1236 nvlist_free(nvroot
);
1237 nvlist_free(fsprops
);
1241 nvlist_free(fsprops
);
1248 * zpool destroy <pool>
1250 * -f Forcefully unmount any datasets
1252 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1255 zpool_do_destroy(int argc
, char **argv
)
1257 boolean_t force
= B_FALSE
;
1260 zpool_handle_t
*zhp
;
1264 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1270 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1279 /* check arguments */
1281 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1285 (void) fprintf(stderr
, gettext("too many arguments\n"));
1291 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1293 * As a special case, check for use of '/' in the name, and
1294 * direct the user to use 'zfs destroy' instead.
1296 if (strchr(pool
, '/') != NULL
)
1297 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1298 "destroy a dataset\n"));
1302 if (zpool_disable_datasets(zhp
, force
) != 0) {
1303 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1304 "could not unmount datasets\n"), zpool_get_name(zhp
));
1309 /* The history must be logged as part of the export */
1310 log_history
= B_FALSE
;
1312 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1319 typedef struct export_cbdata
{
1321 boolean_t hardforce
;
1328 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1330 export_cbdata_t
*cb
= data
;
1332 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1335 /* The history must be logged as part of the export */
1336 log_history
= B_FALSE
;
1338 if (cb
->hardforce
) {
1339 if (zpool_export_force(zhp
, history_str
) != 0)
1341 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1349 * zpool export [-f] <pool> ...
1351 * -a Export all pools
1352 * -f Forcefully unmount datasets
1354 * Export the given pools. By default, the command will attempt to cleanly
1355 * unmount any active datasets within the pool. If the '-f' flag is specified,
1356 * then the datasets will be forcefully unmounted.
1359 zpool_do_export(int argc
, char **argv
)
1362 boolean_t do_all
= B_FALSE
;
1363 boolean_t force
= B_FALSE
;
1364 boolean_t hardforce
= B_FALSE
;
1368 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1380 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1387 cb
.hardforce
= hardforce
;
1393 (void) fprintf(stderr
, gettext("too many arguments\n"));
1397 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1398 zpool_export_one
, &cb
));
1401 /* check arguments */
1403 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1407 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1413 * Given a vdev configuration, determine the maximum width needed for the device
1417 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1425 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
| VDEV_NAME_TYPE_ID
);
1426 if (strlen(name
) + depth
> max
)
1427 max
= strlen(name
) + depth
;
1431 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1432 &child
, &children
) == 0) {
1433 for (c
= 0; c
< children
; c
++)
1434 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1435 max
, name_flags
)) > max
)
1439 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1440 &child
, &children
) == 0) {
1441 for (c
= 0; c
< children
; c
++)
1442 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1443 max
, name_flags
)) > max
)
1447 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1448 &child
, &children
) == 0) {
1449 for (c
= 0; c
< children
; c
++)
1450 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1451 max
, name_flags
)) > max
)
1458 typedef struct spare_cbdata
{
1460 zpool_handle_t
*cb_zhp
;
1464 find_vdev(nvlist_t
*nv
, uint64_t search
)
1470 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1474 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1475 &child
, &children
) == 0) {
1476 for (c
= 0; c
< children
; c
++)
1477 if (find_vdev(child
[c
], search
))
1485 find_spare(zpool_handle_t
*zhp
, void *data
)
1487 spare_cbdata_t
*cbp
= data
;
1488 nvlist_t
*config
, *nvroot
;
1490 config
= zpool_get_config(zhp
, NULL
);
1491 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1494 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1503 typedef struct status_cbdata
{
1507 boolean_t cb_allpools
;
1508 boolean_t cb_verbose
;
1509 boolean_t cb_explain
;
1511 boolean_t cb_dedup_stats
;
1512 boolean_t cb_print_status
;
1513 vdev_cmd_data_list_t
*vcdl
;
1516 /* Print output line for specific vdev in a specific pool */
1518 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1521 for (i
= 0; i
< vcdl
->count
; i
++) {
1522 if ((strcmp(vcdl
->data
[i
].path
, path
) == 0) &&
1523 (strcmp(vcdl
->data
[i
].pool
, pool
) == 0)) {
1524 printf("%s", vcdl
->data
[i
].line
);
1531 * Print out configuration state as requested by status_callback.
1534 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1535 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1539 pool_scan_stat_t
*ps
= NULL
;
1541 char rbuf
[6], wbuf
[6], cbuf
[6];
1543 uint64_t notpresent
;
1544 spare_cbdata_t spare_cb
;
1548 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1549 &child
, &children
) != 0)
1552 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1553 (uint64_t **)&vs
, &c
) == 0);
1555 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1558 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1561 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1563 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1567 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1571 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1572 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1573 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1574 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1577 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1578 ¬present
) == 0) {
1579 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1580 (void) printf(" was %s", path
);
1581 } else if (vs
->vs_aux
!= 0) {
1584 switch (vs
->vs_aux
) {
1585 case VDEV_AUX_OPEN_FAILED
:
1586 (void) printf(gettext("cannot open"));
1589 case VDEV_AUX_BAD_GUID_SUM
:
1590 (void) printf(gettext("missing device"));
1593 case VDEV_AUX_NO_REPLICAS
:
1594 (void) printf(gettext("insufficient replicas"));
1597 case VDEV_AUX_VERSION_NEWER
:
1598 (void) printf(gettext("newer version"));
1601 case VDEV_AUX_UNSUP_FEAT
:
1602 (void) printf(gettext("unsupported feature(s)"));
1605 case VDEV_AUX_SPARED
:
1606 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1607 &spare_cb
.cb_guid
) == 0);
1608 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1609 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1610 zpool_get_name(zhp
)) == 0)
1611 (void) printf(gettext("currently in "
1614 (void) printf(gettext("in use by "
1616 zpool_get_name(spare_cb
.cb_zhp
));
1617 zpool_close(spare_cb
.cb_zhp
);
1619 (void) printf(gettext("currently in use"));
1623 case VDEV_AUX_ERR_EXCEEDED
:
1624 (void) printf(gettext("too many errors"));
1627 case VDEV_AUX_IO_FAILURE
:
1628 (void) printf(gettext("experienced I/O failures"));
1631 case VDEV_AUX_BAD_LOG
:
1632 (void) printf(gettext("bad intent log"));
1635 case VDEV_AUX_EXTERNAL
:
1636 (void) printf(gettext("external device fault"));
1639 case VDEV_AUX_SPLIT_POOL
:
1640 (void) printf(gettext("split into new pool"));
1644 (void) printf(gettext("corrupted data"));
1649 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1650 (uint64_t **)&ps
, &c
);
1652 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1653 vs
->vs_scan_processed
!= 0 && children
== 0) {
1654 (void) printf(gettext(" (%s)"),
1655 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1656 "resilvering" : "repairing");
1659 if (cb
->vcdl
!= NULL
) {
1660 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1662 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1666 (void) printf("\n");
1668 for (c
= 0; c
< children
; c
++) {
1669 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1671 /* Don't print logs or holes here */
1672 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1674 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1676 if (islog
|| ishole
)
1678 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1679 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1680 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1687 * Print the configuration of an exported pool. Iterate over all vdevs in the
1688 * pool, printing out the name and status for each one.
1691 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1699 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1700 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1701 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1704 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1705 (uint64_t **)&vs
, &c
) == 0);
1707 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1708 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1710 if (vs
->vs_aux
!= 0) {
1713 switch (vs
->vs_aux
) {
1714 case VDEV_AUX_OPEN_FAILED
:
1715 (void) printf(gettext("cannot open"));
1718 case VDEV_AUX_BAD_GUID_SUM
:
1719 (void) printf(gettext("missing device"));
1722 case VDEV_AUX_NO_REPLICAS
:
1723 (void) printf(gettext("insufficient replicas"));
1726 case VDEV_AUX_VERSION_NEWER
:
1727 (void) printf(gettext("newer version"));
1730 case VDEV_AUX_UNSUP_FEAT
:
1731 (void) printf(gettext("unsupported feature(s)"));
1734 case VDEV_AUX_ERR_EXCEEDED
:
1735 (void) printf(gettext("too many errors"));
1739 (void) printf(gettext("corrupted data"));
1743 (void) printf("\n");
1745 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1746 &child
, &children
) != 0)
1749 for (c
= 0; c
< children
; c
++) {
1750 uint64_t is_log
= B_FALSE
;
1752 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1757 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1758 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1759 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1763 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1764 &child
, &children
) == 0) {
1765 (void) printf(gettext("\tcache\n"));
1766 for (c
= 0; c
< children
; c
++) {
1767 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1769 (void) printf("\t %s\n", vname
);
1774 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1775 &child
, &children
) == 0) {
1776 (void) printf(gettext("\tspares\n"));
1777 for (c
= 0; c
< children
; c
++) {
1778 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1780 (void) printf("\t %s\n", vname
);
1788 * Logs are recorded as top level vdevs in the main pool child array
1789 * but with "is_log" set to 1. We use either print_status_config() or
1790 * print_import_config() to print the top level logs then any log
1791 * children (eg mirrored slogs) are printed recursively - which
1792 * works because only the top level vdev is marked "is_log"
1795 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1800 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1804 (void) printf(gettext("\tlogs\n"));
1806 for (c
= 0; c
< children
; c
++) {
1807 uint64_t is_log
= B_FALSE
;
1810 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1814 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1815 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1816 if (cb
->cb_print_status
)
1817 print_status_config(zhp
, cb
, name
, child
[c
], 2,
1820 print_import_config(cb
, name
, child
[c
], 2);
1826 * Display the status for the given pool.
1829 show_import(nvlist_t
*config
)
1831 uint64_t pool_state
;
1837 zpool_status_t reason
;
1838 zpool_errata_t errata
;
1842 status_cbdata_t cb
= { 0 };
1844 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1846 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1848 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1850 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1853 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1854 (uint64_t **)&vs
, &vsc
) == 0);
1855 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1857 reason
= zpool_import_status(config
, &msgid
, &errata
);
1859 (void) printf(gettext(" pool: %s\n"), name
);
1860 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1861 (void) printf(gettext(" state: %s"), health
);
1862 if (pool_state
== POOL_STATE_DESTROYED
)
1863 (void) printf(gettext(" (DESTROYED)"));
1864 (void) printf("\n");
1867 case ZPOOL_STATUS_MISSING_DEV_R
:
1868 case ZPOOL_STATUS_MISSING_DEV_NR
:
1869 case ZPOOL_STATUS_BAD_GUID_SUM
:
1870 (void) printf(gettext(" status: One or more devices are "
1871 "missing from the system.\n"));
1874 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1875 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1876 (void) printf(gettext(" status: One or more devices contains "
1877 "corrupted data.\n"));
1880 case ZPOOL_STATUS_CORRUPT_DATA
:
1882 gettext(" status: The pool data is corrupted.\n"));
1885 case ZPOOL_STATUS_OFFLINE_DEV
:
1886 (void) printf(gettext(" status: One or more devices "
1887 "are offlined.\n"));
1890 case ZPOOL_STATUS_CORRUPT_POOL
:
1891 (void) printf(gettext(" status: The pool metadata is "
1895 case ZPOOL_STATUS_VERSION_OLDER
:
1896 (void) printf(gettext(" status: The pool is formatted using a "
1897 "legacy on-disk version.\n"));
1900 case ZPOOL_STATUS_VERSION_NEWER
:
1901 (void) printf(gettext(" status: The pool is formatted using an "
1902 "incompatible version.\n"));
1905 case ZPOOL_STATUS_FEAT_DISABLED
:
1906 (void) printf(gettext(" status: Some supported features are "
1907 "not enabled on the pool.\n"));
1910 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
1911 (void) printf(gettext("status: The pool uses the following "
1912 "feature(s) not supported on this sytem:\n"));
1913 zpool_print_unsup_feat(config
);
1916 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
1917 (void) printf(gettext("status: The pool can only be accessed "
1918 "in read-only mode on this system. It\n\tcannot be "
1919 "accessed in read-write mode because it uses the "
1920 "following\n\tfeature(s) not supported on this system:\n"));
1921 zpool_print_unsup_feat(config
);
1924 case ZPOOL_STATUS_HOSTID_MISMATCH
:
1925 (void) printf(gettext(" status: The pool was last accessed by "
1926 "another system.\n"));
1929 case ZPOOL_STATUS_FAULTED_DEV_R
:
1930 case ZPOOL_STATUS_FAULTED_DEV_NR
:
1931 (void) printf(gettext(" status: One or more devices are "
1935 case ZPOOL_STATUS_BAD_LOG
:
1936 (void) printf(gettext(" status: An intent log record cannot be "
1940 case ZPOOL_STATUS_RESILVERING
:
1941 (void) printf(gettext(" status: One or more devices were being "
1945 case ZPOOL_STATUS_ERRATA
:
1946 (void) printf(gettext(" status: Errata #%d detected.\n"),
1952 * No other status can be seen when importing pools.
1954 assert(reason
== ZPOOL_STATUS_OK
);
1958 * Print out an action according to the overall state of the pool.
1960 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
1961 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
1962 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
1963 (void) printf(gettext(" action: The pool can be "
1964 "imported using its name or numeric identifier, "
1965 "though\n\tsome features will not be available "
1966 "without an explicit 'zpool upgrade'.\n"));
1967 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
1968 (void) printf(gettext(" action: The pool can be "
1969 "imported using its name or numeric "
1970 "identifier and\n\tthe '-f' flag.\n"));
1971 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
1973 case ZPOOL_ERRATA_NONE
:
1976 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
1977 (void) printf(gettext(" action: The pool can "
1978 "be imported using its name or numeric "
1979 "identifier,\n\thowever there is a compat"
1980 "ibility issue which should be corrected"
1981 "\n\tby running 'zpool scrub'\n"));
1984 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
1985 (void) printf(gettext(" action: The pool can"
1986 "not be imported with this version of ZFS "
1987 "due to\n\tan active asynchronous destroy. "
1988 "Revert to an earlier version\n\tand "
1989 "allow the destroy to complete before "
1995 * All errata must contain an action message.
2000 (void) printf(gettext(" action: The pool can be "
2001 "imported using its name or numeric "
2004 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2005 (void) printf(gettext(" action: The pool can be imported "
2006 "despite missing or damaged devices. The\n\tfault "
2007 "tolerance of the pool may be compromised if imported.\n"));
2010 case ZPOOL_STATUS_VERSION_NEWER
:
2011 (void) printf(gettext(" action: The pool cannot be "
2012 "imported. Access the pool on a system running "
2013 "newer\n\tsoftware, or recreate the pool from "
2016 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2017 (void) printf(gettext("action: The pool cannot be "
2018 "imported. Access the pool on a system that "
2019 "supports\n\tthe required feature(s), or recreate "
2020 "the pool from backup.\n"));
2022 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2023 (void) printf(gettext("action: The pool cannot be "
2024 "imported in read-write mode. Import the pool "
2026 "\t\"-o readonly=on\", access the pool on a system "
2027 "that supports the\n\trequired feature(s), or "
2028 "recreate the pool from backup.\n"));
2030 case ZPOOL_STATUS_MISSING_DEV_R
:
2031 case ZPOOL_STATUS_MISSING_DEV_NR
:
2032 case ZPOOL_STATUS_BAD_GUID_SUM
:
2033 (void) printf(gettext(" action: The pool cannot be "
2034 "imported. Attach the missing\n\tdevices and try "
2038 (void) printf(gettext(" action: The pool cannot be "
2039 "imported due to damaged devices or data.\n"));
2043 /* Print the comment attached to the pool. */
2044 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2045 (void) printf(gettext("comment: %s\n"), comment
);
2048 * If the state is "closed" or "can't open", and the aux state
2049 * is "corrupt data":
2051 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2052 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2053 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2054 if (pool_state
== POOL_STATE_DESTROYED
)
2055 (void) printf(gettext("\tThe pool was destroyed, "
2056 "but can be imported using the '-Df' flags.\n"));
2057 else if (pool_state
!= POOL_STATE_EXPORTED
)
2058 (void) printf(gettext("\tThe pool may be active on "
2059 "another system, but can be imported using\n\t"
2060 "the '-f' flag.\n"));
2064 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2067 (void) printf(gettext(" config:\n\n"));
2069 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, 0, 0);
2070 if (cb
.cb_namewidth
< 10)
2071 cb
.cb_namewidth
= 10;
2073 print_import_config(&cb
, name
, nvroot
, 0);
2074 if (num_logs(nvroot
) > 0)
2075 print_logs(NULL
, &cb
, nvroot
);
2077 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2078 (void) printf(gettext("\n\tAdditional devices are known to "
2079 "be part of this pool, though their\n\texact "
2080 "configuration cannot be determined.\n"));
2085 * Perform the import for the given configuration. This passes the heavy
2086 * lifting off to zpool_import_props(), and then mounts the datasets contained
2090 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2091 nvlist_t
*props
, int flags
)
2093 zpool_handle_t
*zhp
;
2098 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2101 verify(nvlist_lookup_uint64(config
,
2102 ZPOOL_CONFIG_POOL_STATE
, &state
) == 0);
2103 verify(nvlist_lookup_uint64(config
,
2104 ZPOOL_CONFIG_VERSION
, &version
) == 0);
2105 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2106 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2107 "is formatted using an unsupported ZFS version\n"), name
);
2109 } else if (state
!= POOL_STATE_EXPORTED
&&
2110 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2111 uint64_t hostid
= 0;
2112 unsigned long system_hostid
= get_system_hostid();
2114 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
2117 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
) {
2122 verify(nvlist_lookup_string(config
,
2123 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2124 verify(nvlist_lookup_uint64(config
,
2125 ZPOOL_CONFIG_TIMESTAMP
, ×tamp
) == 0);
2127 (void) fprintf(stderr
, gettext("cannot import "
2128 "'%s': pool may be in use from other "
2129 "system, it was last accessed by %s "
2130 "(hostid: 0x%lx) on %s"), name
, hostname
,
2131 (unsigned long)hostid
,
2132 asctime(localtime(&t
)));
2133 (void) fprintf(stderr
, gettext("use '-f' to "
2134 "import anyway\n"));
2139 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2142 if (newname
!= NULL
)
2143 name
= (char *)newname
;
2145 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2148 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2149 !(flags
& ZFS_IMPORT_ONLY
) &&
2150 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2160 * zpool import [-d dir] [-D]
2161 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2162 * [-d dir | -c cachefile] [-f] -a
2163 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2164 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2166 * -c Read pool information from a cachefile instead of searching
2169 * -d Scan in a specific directory, other than /dev/. More than
2170 * one directory can be specified using multiple '-d' options.
2172 * -D Scan for previously destroyed pools or import all or only
2173 * specified destroyed pools.
2175 * -R Temporarily import the pool, with all mountpoints relative to
2176 * the given root. The pool will remain exported when the machine
2179 * -V Import even in the presence of faulted vdevs. This is an
2180 * intentionally undocumented option for testing purposes, and
2181 * treats the pool configuration as complete, leaving any bad
2182 * vdevs in the FAULTED state. In other words, it does verbatim
2185 * -f Force import, even if it appears that the pool is active.
2187 * -F Attempt rewind if necessary.
2189 * -n See if rewind would work, but don't actually rewind.
2191 * -N Import the pool but don't mount datasets.
2193 * -T Specify a starting txg to use for import. This option is
2194 * intentionally undocumented option for testing purposes.
2196 * -a Import all pools found.
2198 * -o Set property=value and/or temporary mount options (without '=').
2200 * -s Scan using the default search path, the libblkid cache will
2203 * The import command scans for pools to import, and import pools based on pool
2204 * name and GUID. The pool can also be renamed as part of the import process.
2207 zpool_do_import(int argc
, char **argv
)
2209 char **searchdirs
= NULL
;
2210 char *env
, *envdup
= NULL
;
2214 nvlist_t
*pools
= NULL
;
2215 boolean_t do_all
= B_FALSE
;
2216 boolean_t do_destroyed
= B_FALSE
;
2217 char *mntopts
= NULL
;
2220 uint64_t searchguid
= 0;
2221 char *searchname
= NULL
;
2223 nvlist_t
*found_config
;
2224 nvlist_t
*policy
= NULL
;
2225 nvlist_t
*props
= NULL
;
2227 int flags
= ZFS_IMPORT_NORMAL
;
2228 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2229 boolean_t dryrun
= B_FALSE
;
2230 boolean_t do_rewind
= B_FALSE
;
2231 boolean_t xtreme_rewind
= B_FALSE
;
2232 boolean_t do_scan
= B_FALSE
;
2233 uint64_t pool_state
, txg
= -1ULL;
2234 char *cachefile
= NULL
;
2235 importargs_t idata
= { 0 };
2239 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2248 if (searchdirs
== NULL
) {
2249 searchdirs
= safe_malloc(sizeof (char *));
2251 char **tmp
= safe_malloc((nsearch
+ 1) *
2253 bcopy(searchdirs
, tmp
, nsearch
*
2258 searchdirs
[nsearch
++] = optarg
;
2261 do_destroyed
= B_TRUE
;
2264 flags
|= ZFS_IMPORT_ANY_HOST
;
2270 flags
|= ZFS_IMPORT_MISSING_LOG
;
2276 flags
|= ZFS_IMPORT_ONLY
;
2279 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2282 if (add_prop_list(optarg
, propval
,
2290 if (add_prop_list(zpool_prop_to_name(
2291 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2293 if (add_prop_list_default(zpool_prop_to_name(
2294 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2301 flags
|= ZFS_IMPORT_TEMP_NAME
;
2302 if (add_prop_list_default(zpool_prop_to_name(
2303 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2309 txg
= strtoull(optarg
, &endptr
, 0);
2310 if (errno
!= 0 || *endptr
!= '\0') {
2311 (void) fprintf(stderr
,
2312 gettext("invalid txg value\n"));
2315 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2318 flags
|= ZFS_IMPORT_VERBATIM
;
2321 xtreme_rewind
= B_TRUE
;
2324 (void) fprintf(stderr
, gettext("missing argument for "
2325 "'%c' option\n"), optopt
);
2329 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2338 if (cachefile
&& nsearch
!= 0) {
2339 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2343 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2344 (void) fprintf(stderr
,
2345 gettext("-n or -X only meaningful with -F\n"));
2349 rewind_policy
= ZPOOL_TRY_REWIND
;
2351 rewind_policy
= ZPOOL_DO_REWIND
;
2353 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2355 /* In the future, we can capture further policy and include it here */
2356 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2357 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2358 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2361 /* check argument count */
2364 (void) fprintf(stderr
, gettext("too many arguments\n"));
2369 (void) fprintf(stderr
, gettext("too many arguments\n"));
2375 * Check for the effective uid. We do this explicitly here because
2376 * otherwise any attempt to discover pools will silently fail.
2378 if (argc
== 0 && geteuid() != 0) {
2379 (void) fprintf(stderr
, gettext("cannot "
2380 "discover pools: permission denied\n"));
2381 if (searchdirs
!= NULL
)
2385 nvlist_free(policy
);
2390 * Depending on the arguments given, we do one of the following:
2392 * <none> Iterate through all pools and display information about
2395 * -a Iterate through all pools and try to import each one.
2397 * <id> Find the pool that corresponds to the given GUID/pool
2398 * name and import that one.
2400 * -D Above options applies only to destroyed pools.
2406 searchguid
= strtoull(argv
[0], &endptr
, 10);
2407 if (errno
!= 0 || *endptr
!= '\0') {
2408 searchname
= argv
[0];
2411 found_config
= NULL
;
2414 * User specified a name or guid. Ensure it's unique.
2416 idata
.unique
= B_TRUE
;
2420 * Check the environment for the preferred search path.
2422 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2425 envdup
= strdup(env
);
2427 dir
= strtok(envdup
, ":");
2428 while (dir
!= NULL
) {
2429 if (searchdirs
== NULL
) {
2430 searchdirs
= safe_malloc(sizeof (char *));
2432 char **tmp
= safe_malloc((nsearch
+ 1) *
2434 bcopy(searchdirs
, tmp
, nsearch
*
2439 searchdirs
[nsearch
++] = dir
;
2440 dir
= strtok(NULL
, ":");
2444 idata
.path
= searchdirs
;
2445 idata
.paths
= nsearch
;
2446 idata
.poolname
= searchname
;
2447 idata
.guid
= searchguid
;
2448 idata
.cachefile
= cachefile
;
2449 idata
.scan
= do_scan
;
2452 * Under Linux the zpool_find_import_impl() function leverages the
2453 * taskq implementation to parallelize device scanning. It is
2454 * therefore necessary to initialize this functionality for the
2455 * duration of the zpool_search_import() function.
2458 pools
= zpool_search_import(g_zfs
, &idata
);
2461 if (pools
!= NULL
&& idata
.exists
&&
2462 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2463 (void) fprintf(stderr
, gettext("cannot import '%s': "
2464 "a pool with that name already exists\n"),
2466 (void) fprintf(stderr
, gettext("use the form '%s "
2467 "<pool | id> <newpool>' to give it a new name\n"),
2470 } else if (pools
== NULL
&& idata
.exists
) {
2471 (void) fprintf(stderr
, gettext("cannot import '%s': "
2472 "a pool with that name is already created/imported,\n"),
2474 (void) fprintf(stderr
, gettext("and no additional pools "
2475 "with that name were found\n"));
2477 } else if (pools
== NULL
) {
2479 (void) fprintf(stderr
, gettext("cannot import '%s': "
2480 "no such pool available\n"), argv
[0]);
2486 if (searchdirs
!= NULL
)
2490 nvlist_free(policy
);
2497 * At this point we have a list of import candidate configs. Even if
2498 * we were searching by pool name or guid, we still need to
2499 * post-process the list to deal with pool state and possible
2505 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2507 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2509 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2511 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2513 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2516 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2523 (void) printf("\n");
2526 err
|= do_import(config
, NULL
, mntopts
,
2529 show_import(config
);
2531 } else if (searchname
!= NULL
) {
2535 * We are searching for a pool based on name.
2537 verify(nvlist_lookup_string(config
,
2538 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2540 if (strcmp(name
, searchname
) == 0) {
2541 if (found_config
!= NULL
) {
2542 (void) fprintf(stderr
, gettext(
2543 "cannot import '%s': more than "
2544 "one matching pool\n"), searchname
);
2545 (void) fprintf(stderr
, gettext(
2546 "import by numeric ID instead\n"));
2549 found_config
= config
;
2555 * Search for a pool by guid.
2557 verify(nvlist_lookup_uint64(config
,
2558 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2560 if (guid
== searchguid
)
2561 found_config
= config
;
2566 * If we were searching for a specific pool, verify that we found a
2567 * pool, and then do the import.
2569 if (argc
!= 0 && err
== 0) {
2570 if (found_config
== NULL
) {
2571 (void) fprintf(stderr
, gettext("cannot import '%s': "
2572 "no such pool available\n"), argv
[0]);
2575 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2576 argv
[1], mntopts
, props
, flags
);
2581 * If we were just looking for pools, report an error if none were
2584 if (argc
== 0 && first
)
2585 (void) fprintf(stderr
,
2586 gettext("no pools available to import\n"));
2591 nvlist_free(policy
);
2592 if (searchdirs
!= NULL
)
2597 return (err
? 1 : 0);
2600 typedef struct iostat_cbdata
{
2605 char **cb_vdev_names
; /* Only show these vdevs */
2606 unsigned int cb_vdev_names_count
;
2607 boolean_t cb_verbose
;
2608 boolean_t cb_literal
;
2609 boolean_t cb_scripted
;
2610 zpool_list_t
*cb_list
;
2611 vdev_cmd_data_list_t
*vcdl
;
2615 typedef struct name_and_columns
{
2616 const char *name
; /* Column name */
2617 unsigned int columns
; /* Center name to this number of columns */
2618 } name_and_columns_t
;
2620 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2622 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
2624 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2626 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2627 {"asyncq_wait", 2}, {"scrub"}},
2628 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
2629 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2631 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
2632 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
2633 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
2634 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
2638 /* Shorthand - if "columns" field not set, default to 1 column */
2639 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
2641 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2643 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2644 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
2645 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2646 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
2647 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2648 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
2649 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2650 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
2653 static const char *histo_to_title
[] = {
2654 [IOS_L_HISTO
] = "latency",
2655 [IOS_RQ_HISTO
] = "req_size",
2659 * Return the number of labels in a null-terminated name_and_columns_t
2664 label_array_len(const name_and_columns_t
*labels
)
2668 while (labels
[i
].name
)
2675 * Return the number of strings in a null-terminated string array.
2678 * const char foo[] = {"bar", "baz", NULL}
2683 str_array_len(const char *array
[])
2694 * Return a default column width for default/latency/queue columns. This does
2695 * not include histograms, which have their columns autosized.
2698 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
2700 unsigned long column_width
= 5; /* Normal niceprint */
2701 static unsigned long widths
[] = {
2703 * Choose some sane default column sizes for printing the
2706 [IOS_DEFAULT
] = 15, /* 1PB capacity */
2707 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
2708 [IOS_QUEUES
] = 6, /* 1M queue entries */
2712 column_width
= widths
[type
];
2714 return (column_width
);
2718 * Print the column labels, i.e:
2720 * capacity operations bandwidth
2721 * alloc free read write read write ...
2723 * If force_column_width is set, use it for the column width. If not set, use
2724 * the default column width.
2727 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2728 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
2731 unsigned int text_start
, rw_column_width
, spaces_to_end
;
2732 uint64_t flags
= cb
->cb_flags
;
2734 unsigned int column_width
= force_column_width
;
2736 /* For each bit set in flags */
2737 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2738 idx
= lowbit64(f
) - 1;
2739 if (!force_column_width
)
2740 column_width
= default_column_width(cb
, idx
);
2741 /* Print our top labels centered over "read write" label. */
2742 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
2743 const char *name
= labels
[idx
][i
].name
;
2745 * We treat labels[][].columns == 0 as shorthand
2746 * for one column. It makes writing out the label
2747 * tables more concise.
2749 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
2750 unsigned int slen
= strlen(name
);
2752 rw_column_width
= (column_width
* columns
) +
2753 (2 * (columns
- 1));
2755 text_start
= (int) ((rw_column_width
)/columns
-
2758 printf(" "); /* Two spaces between columns */
2760 /* Space from beginning of column to label */
2761 for (s
= 0; s
< text_start
; s
++)
2766 /* Print space after label to end of column */
2767 spaces_to_end
= rw_column_width
- text_start
- slen
;
2768 for (s
= 0; s
< spaces_to_end
; s
++)
2777 * Utility function to print out a line of dashes like:
2779 * -------------------------------- ----- ----- ----- ----- -----
2781 * ...or a dashed named-row line like:
2787 * @force_column_width If non-zero, use the value as the column width.
2788 * Otherwise use the default column widths.
2790 * @name: Print a dashed named-row line starting
2791 * with @name. Otherwise, print a regular
2795 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2799 unsigned int namewidth
;
2800 uint64_t flags
= cb
->cb_flags
;
2803 const name_and_columns_t
*labels
;
2807 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
2808 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
2809 } else if (cb
->cb_vdev_names_count
) {
2815 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
2816 name
? strlen(name
) : 0);
2820 printf("%-*s", namewidth
, name
);
2822 for (i
= 0; i
< namewidth
; i
++)
2826 /* For each bit in flags */
2827 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2828 unsigned int column_width
;
2829 idx
= lowbit64(f
) - 1;
2830 if (force_column_width
)
2831 column_width
= force_column_width
;
2833 column_width
= default_column_width(cb
, idx
);
2835 labels
= iostat_bottom_labels
[idx
];
2836 for (i
= 0; i
< label_array_len(labels
); i
++) {
2838 printf(" %*s-", column_width
- 1, " ");
2840 printf(" %.*s", column_width
,
2841 "--------------------");
2849 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
2850 unsigned int force_column_width
)
2852 print_iostat_dashes(cb
, force_column_width
, NULL
);
2856 print_iostat_separator(iostat_cbdata_t
*cb
)
2858 print_iostat_separator_impl(cb
, 0);
2862 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2863 const char *histo_vdev_name
)
2865 unsigned int namewidth
;
2868 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
2869 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
2870 } else if (cb
->cb_vdev_names_count
) {
2876 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
2877 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
2879 if (histo_vdev_name
)
2880 printf("%-*s", namewidth
, histo_vdev_name
);
2882 printf("%*s", namewidth
, "");
2885 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
2887 printf("%-*s", namewidth
, title
);
2889 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
2891 print_iostat_separator_impl(cb
, force_column_width
);
2895 print_iostat_header(iostat_cbdata_t
*cb
)
2897 print_iostat_header_impl(cb
, 0, NULL
);
2902 * Display a single statistic.
2905 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
2906 unsigned int column_size
, boolean_t scripted
)
2910 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
2913 printf("\t%s", buf
);
2915 printf(" %*s", column_size
, buf
);
2919 * Calculate the default vdev stats
2921 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
2922 * stats into calcvs.
2925 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
2926 vdev_stat_t
*calcvs
)
2930 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
2931 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
2932 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
2934 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
2935 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
2939 * Internal representation of the extended iostats data.
2941 * The extended iostat stats are exported in nvlists as either uint64_t arrays
2942 * or single uint64_t's. We make both look like arrays to make them easier
2943 * to process. In order to make single uint64_t's look like arrays, we set
2944 * __data to the stat data, and then set *data = &__data with count = 1. Then,
2945 * we can just use *data and count.
2949 uint_t count
; /* Number of entries in data[] */
2950 uint64_t __data
; /* Only used when data is a single uint64_t */
2954 stat_histo_max(struct stat_array
*nva
, unsigned int len
) {
2957 for (i
= 0; i
< len
; i
++)
2958 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
2964 * Helper function to lookup a uint64_t array or uint64_t value and store its
2965 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
2966 * it look like a one element array to make it easier to process.
2969 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
2970 struct stat_array
*nva
) {
2974 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
2975 switch (nvpair_type(tmp
)) {
2976 case DATA_TYPE_UINT64_ARRAY
:
2977 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
2979 case DATA_TYPE_UINT64
:
2980 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
2981 nva
->data
= &nva
->__data
;
2985 /* Not a uint64_t */
2994 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
2995 * subtract them, and return the results in a newly allocated stat_array.
2996 * You must free the returned array after you are done with it with
2997 * free_calc_stats().
2999 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3002 static struct stat_array
*
3003 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3006 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3007 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3009 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3011 /* Extract our extended stats nvlist from the main list */
3012 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3015 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3019 newnva
= safe_malloc(alloc_size
);
3020 oldnva
= safe_malloc(alloc_size
);
3021 calcnva
= safe_malloc(alloc_size
);
3023 for (j
= 0; j
< len
; j
++) {
3024 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3026 calcnva
[j
].count
= newnva
[j
].count
;
3027 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3028 calcnva
[j
].data
= safe_malloc(alloc_size
);
3029 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3032 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3034 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3035 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3044 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3047 for (i
= 0; i
< len
; i
++)
3054 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3055 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3061 enum zfs_nicenum_format format
;
3062 unsigned int buckets
;
3063 unsigned int start_bucket
;
3066 format
= ZFS_NICENUM_RAW
;
3068 format
= ZFS_NICENUM_1024
;
3070 /* All these histos are the same size, so just use nva[0].count */
3071 buckets
= nva
[0].count
;
3073 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3074 /* Start at 512 - req size should never be lower than this */
3080 for (j
= start_bucket
; j
< buckets
; j
++) {
3081 /* Print histogram bucket label */
3082 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3083 /* Ending range of this bucket */
3084 val
= (1UL << (j
+ 1)) - 1;
3085 zfs_nicetime(val
, buf
, sizeof (buf
));
3087 /* Request size (starting range of bucket) */
3089 zfs_nicenum(val
, buf
, sizeof (buf
));
3092 if (cb
->cb_scripted
)
3093 printf("%llu", (u_longlong_t
) val
);
3095 printf("%-*s", namewidth
, buf
);
3097 /* Print the values on the line */
3098 for (i
= 0; i
< len
; i
++) {
3099 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3100 column_width
, cb
->cb_scripted
);
3107 print_solid_separator(unsigned int length
)
3115 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3116 nvlist_t
*newnv
, double scale
, const char *name
)
3118 unsigned int column_width
;
3119 unsigned int namewidth
;
3120 unsigned int entire_width
;
3121 enum iostat_type type
;
3122 struct stat_array
*nva
;
3124 unsigned int names_len
;
3126 /* What type of histo are we? */
3127 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3129 /* Get NULL-terminated array of nvlist names for our histo */
3130 names
= vsx_type_to_nvlist
[type
];
3131 names_len
= str_array_len(names
); /* num of names */
3133 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3135 if (cb
->cb_literal
) {
3136 column_width
= MAX(5,
3137 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3142 namewidth
= MAX(cb
->cb_namewidth
,
3143 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3146 * Calculate the entire line width of what we're printing. The
3147 * +2 is for the two spaces between columns:
3151 /* |___| <---------- column_width */
3153 /* |__________| <--- entire_width */
3155 entire_width
= namewidth
+ (column_width
+ 2) *
3156 label_array_len(iostat_bottom_labels
[type
]);
3158 if (cb
->cb_scripted
)
3159 printf("%s\n", name
);
3161 print_iostat_header_impl(cb
, column_width
, name
);
3163 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3166 free_calc_stats(nva
, names_len
);
3167 if (!cb
->cb_scripted
)
3168 print_solid_separator(entire_width
);
3172 * Calculate the average latency of a power-of-two latency histogram
3175 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3178 uint64_t count
= 0, total
= 0;
3180 for (i
= 0; i
< buckets
; i
++) {
3182 * Our buckets are power-of-two latency ranges. Use the
3183 * midpoint latency of each bucket to calculate the average.
3191 if (histo
[i
] != 0) {
3192 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3197 /* Prevent divide by zero */
3198 return (count
== 0 ? 0 : total
/ count
);
3202 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3203 nvlist_t
*newnv
, double scale
)
3207 const char *names
[] = {
3208 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3209 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3210 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3211 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3212 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3213 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3214 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3215 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3216 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3217 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3220 struct stat_array
*nva
;
3222 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3223 enum zfs_nicenum_format format
;
3225 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3228 format
= ZFS_NICENUM_RAW
;
3230 format
= ZFS_NICENUM_1024
;
3232 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3233 val
= nva
[i
].data
[0] * scale
;
3234 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3237 free_calc_stats(nva
, ARRAY_SIZE(names
));
3241 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3242 nvlist_t
*newnv
, double scale
)
3246 const char *names
[] = {
3247 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3248 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3249 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3250 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3251 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3252 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3253 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3254 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3255 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3257 struct stat_array
*nva
;
3259 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3260 enum zfs_nicenum_format format
;
3262 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3265 format
= ZFS_NICENUM_RAW
;
3267 format
= ZFS_NICENUM_TIME
;
3269 /* Print our avg latencies on the line */
3270 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3271 /* Compute average latency for a latency histo */
3272 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3273 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3275 free_calc_stats(nva
, ARRAY_SIZE(names
));
3279 * Print default statistics (capacity/operations/bandwidth)
3282 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3284 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3285 enum zfs_nicenum_format format
;
3286 char na
; /* char to print for "not applicable" values */
3288 if (cb
->cb_literal
) {
3289 format
= ZFS_NICENUM_RAW
;
3292 format
= ZFS_NICENUM_1024
;
3296 /* only toplevel vdevs have capacity stats */
3297 if (vs
->vs_space
== 0) {
3298 if (cb
->cb_scripted
)
3299 printf("\t%c\t%c", na
, na
);
3301 printf(" %*c %*c", column_width
, na
, column_width
,
3304 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3306 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3307 column_width
, cb
->cb_scripted
);
3310 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3311 format
, column_width
, cb
->cb_scripted
);
3312 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3313 format
, column_width
, cb
->cb_scripted
);
3314 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3315 format
, column_width
, cb
->cb_scripted
);
3316 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3317 format
, column_width
, cb
->cb_scripted
);
3321 * Print out all the statistics for the given vdev. This can either be the
3322 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3323 * is a verbose output, and we don't want to display the toplevel pool stats.
3325 * Returns the number of stat lines printed.
3328 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3329 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3331 nvlist_t
**oldchild
, **newchild
;
3333 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3334 vdev_stat_t zerovs
= { 0 };
3341 calcvs
= safe_malloc(sizeof (*calcvs
));
3343 if (oldnv
!= NULL
) {
3344 verify(nvlist_lookup_uint64_array(oldnv
,
3345 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3350 /* Do we only want to see a specific vdev? */
3351 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3352 /* Yes we do. Is this the vdev? */
3353 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3355 * This is our vdev. Since it is the only vdev we
3356 * will be displaying, make depth = 0 so that it
3357 * doesn't get indented.
3364 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3365 /* Couldn't match the name */
3370 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3371 (uint64_t **)&newvs
, &c
) == 0);
3374 * Print the vdev name unless it's is a histogram. Histograms
3375 * display the vdev name in the header itself.
3377 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3378 if (cb
->cb_scripted
) {
3381 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3382 (void) printf("%*s%s", depth
, "", name
);
3384 (void) printf("%*s%s%*s", depth
, "", name
,
3385 (int)(cb
->cb_namewidth
- strlen(name
) -
3390 /* Calculate our scaling factor */
3391 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3392 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3394 * If we specify printing histograms with no time interval, then
3395 * print the histogram numbers over the entire lifetime of the
3403 scale
= (double)NANOSEC
/ tdelta
;
3406 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3407 calc_default_iostats(oldvs
, newvs
, calcvs
);
3408 print_iostat_default(calcvs
, cb
, scale
);
3410 if (cb
->cb_flags
& IOS_LATENCY_M
)
3411 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3412 if (cb
->cb_flags
& IOS_QUEUES_M
)
3413 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3414 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3416 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3419 if (cb
->vcdl
!= NULL
) {
3421 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3423 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3425 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3426 if (cb
->cb_flags
& IOS_ANYHISTO_M
)
3431 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3440 if (!cb
->cb_verbose
)
3443 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3444 &newchild
, &children
) != 0)
3447 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3448 &oldchild
, &c
) != 0)
3451 for (c
= 0; c
< children
; c
++) {
3452 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3454 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3457 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3460 if (ishole
|| islog
)
3463 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3465 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3466 newchild
[c
], cb
, depth
+ 2);
3471 * Log device section
3474 if (num_logs(newnv
) > 0) {
3475 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3476 !cb
->cb_vdev_names
) {
3477 print_iostat_dashes(cb
, 0, "logs");
3480 for (c
= 0; c
< children
; c
++) {
3481 uint64_t islog
= B_FALSE
;
3482 (void) nvlist_lookup_uint64(newchild
[c
],
3483 ZPOOL_CONFIG_IS_LOG
, &islog
);
3486 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3488 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3489 oldchild
[c
] : NULL
, newchild
[c
],
3498 * Include level 2 ARC devices in iostat output
3500 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3501 &newchild
, &children
) != 0)
3504 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
3505 &oldchild
, &c
) != 0)
3509 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3510 !cb
->cb_vdev_names
) {
3511 print_iostat_dashes(cb
, 0, "cache");
3514 for (c
= 0; c
< children
; c
++) {
3515 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3517 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
3518 : NULL
, newchild
[c
], cb
, depth
+ 2);
3527 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
3529 iostat_cbdata_t
*cb
= data
;
3533 * If the pool has disappeared, remove it from the list and continue.
3535 if (zpool_refresh_stats(zhp
, &missing
) != 0)
3539 pool_list_remove(cb
->cb_list
, zhp
);
3545 * Callback to print out the iostats for the given pool.
3548 print_iostat(zpool_handle_t
*zhp
, void *data
)
3550 iostat_cbdata_t
*cb
= data
;
3551 nvlist_t
*oldconfig
, *newconfig
;
3552 nvlist_t
*oldnvroot
, *newnvroot
;
3555 newconfig
= zpool_get_config(zhp
, &oldconfig
);
3557 if (cb
->cb_iteration
== 1)
3560 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3563 if (oldconfig
== NULL
)
3566 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3569 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
3571 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
3572 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
3573 print_iostat_separator(cb
);
3586 if (isatty(STDOUT_FILENO
)) {
3587 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
3589 columns
= ws
.ws_col
;
3598 get_namewidth(zpool_handle_t
*zhp
, void *data
)
3600 iostat_cbdata_t
*cb
= data
;
3601 nvlist_t
*config
, *nvroot
;
3604 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
3605 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3607 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
3608 if (!cb
->cb_verbose
)
3609 cb
->cb_namewidth
= poolname_len
;
3611 cb
->cb_namewidth
= MAX(poolname_len
,
3612 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
3613 cb
->cb_name_flags
));
3616 * The width must be at least 10, but may be as large as the
3617 * column width - 42 so that we can still fit in one line.
3619 columns
= get_columns();
3621 if (cb
->cb_namewidth
< 10)
3622 cb
->cb_namewidth
= 10;
3623 if (cb
->cb_namewidth
> columns
- 42)
3624 cb
->cb_namewidth
= columns
- 42;
3630 * Parse the input string, get the 'interval' and 'count' value if there is one.
3633 get_interval_count(int *argcp
, char **argv
, float *iv
,
3637 unsigned long count
= 0;
3641 * Determine if the last argument is an integer or a pool name
3643 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3647 interval
= strtof(argv
[argc
- 1], &end
);
3649 if (*end
== '\0' && errno
== 0) {
3650 if (interval
== 0) {
3651 (void) fprintf(stderr
, gettext("interval "
3652 "cannot be zero\n"));
3656 * Ignore the last parameter
3661 * If this is not a valid number, just plow on. The
3662 * user will get a more informative error message later
3670 * If the last argument is also an integer, then we have both a count
3673 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3678 interval
= strtof(argv
[argc
- 1], &end
);
3680 if (*end
== '\0' && errno
== 0) {
3681 if (interval
== 0) {
3682 (void) fprintf(stderr
, gettext("interval "
3683 "cannot be zero\n"));
3688 * Ignore the last parameter
3702 get_timestamp_arg(char c
)
3705 timestamp_fmt
= UDATE
;
3707 timestamp_fmt
= DDATE
;
3713 * Return stat flags that are supported by all pools by both the module and
3714 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
3715 * It will get ANDed down until only the flags that are supported on all pools
3719 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
3721 uint64_t *mask
= data
;
3722 nvlist_t
*config
, *nvroot
, *nvx
;
3726 config
= zpool_get_config(zhp
, NULL
);
3727 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3730 /* Default stats are always supported, but for completeness.. */
3731 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
3732 flags
|= IOS_DEFAULT_M
;
3734 /* Get our extended stats nvlist from the main list */
3735 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3738 * No extended stats; they're probably running an older
3739 * module. No big deal, we support that too.
3744 /* For each extended stat, make sure all its nvpairs are supported */
3745 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
3746 if (!vsx_type_to_nvlist
[j
][0])
3749 /* Start off by assuming the flag is supported, then check */
3750 flags
|= (1ULL << j
);
3751 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
3752 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
3753 /* flag isn't supported */
3754 flags
= flags
& ~(1ULL << j
);
3760 *mask
= *mask
& flags
;
3765 * Return a bitmask of stats that are supported on all pools by both the module
3769 get_stat_flags(zpool_list_t
*list
)
3774 * get_stat_flags_cb() will lop off bits from "mask" until only the
3775 * flags that are supported on all pools remain.
3777 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
3782 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
3785 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
3787 iostat_cbdata_t
*cb
= cb_data
;
3791 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
3793 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
3794 ret
= 1; /* match */
3801 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
3804 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
3806 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
3810 * Check if vdevs are in a pool
3812 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
3813 * return 0. If pool_name is NULL, then search all pools.
3816 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
3817 iostat_cbdata_t
*cb
)
3824 if ((argc
== 0) || !*argv
)
3830 /* Temporarily hijack cb_vdev_names for a second... */
3831 tmp_name
= cb
->cb_vdev_names
;
3833 /* Go though our list of prospective vdev names */
3834 for (i
= 0; i
< argc
; i
++) {
3835 cb
->cb_vdev_names
= argv
+ i
;
3837 /* Is this name a vdev in our pools? */
3838 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
3846 cb
->cb_vdev_names
= tmp_name
;
3852 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
3855 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
3862 * Do we have a pool named *name? If so, return 1, otherwise 0.
3867 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
3870 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
3872 are_all_pools(int argc
, char **argv
) {
3873 if ((argc
== 0) || !*argv
)
3877 if (!is_pool(argv
[argc
]))
3884 * Helper function to print out vdev/pool names we can't resolve. Used for an
3888 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
3889 iostat_cbdata_t
*cb
)
3894 for (i
= 0; i
< argc
; i
++) {
3898 str
= gettext("pool");
3899 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
3900 str
= gettext("vdev in this pool");
3901 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
3902 str
= gettext("vdev in another pool");
3904 str
= gettext("unknown");
3906 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
3911 * Same as get_interval_count(), but with additional checks to not misinterpret
3912 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
3916 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
3917 unsigned long *count
, iostat_cbdata_t
*cb
)
3919 char **tmpargv
= argv
;
3920 int argc_for_interval
= 0;
3922 /* Is the last arg an interval value? Or a guid? */
3923 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
3925 * The last arg is not a guid, so it's probably an
3928 argc_for_interval
++;
3931 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
3933 * The 2nd to last arg is not a guid, so it's probably
3934 * an interval value.
3936 argc_for_interval
++;
3940 /* Point to our list of possible intervals */
3941 tmpargv
= &argv
[*argc
- argc_for_interval
];
3943 *argc
= *argc
- argc_for_interval
;
3944 get_interval_count(&argc_for_interval
, tmpargv
,
3949 * Floating point sleep(). Allows you to pass in a floating point value for
3954 struct timespec req
;
3955 req
.tv_sec
= floor(sec
);
3956 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
3957 nanosleep(&req
, NULL
);
3962 * zpool iostat [-c CMD] [-ghHLpPvy] [[-lq]|[-r|-w]] [-n name] [-T d|u]
3963 * [[ pool ...]|[pool vdev ...]|[vdev ...]]
3964 * [interval [count]]
3966 * -c CMD For each vdev, run command CMD
3967 * -g Display guid for individual vdev name.
3968 * -L Follow links when resolving vdev path name.
3969 * -P Display full path for vdev name.
3970 * -v Display statistics for individual vdevs
3972 * -p Display values in parsable (exact) format.
3973 * -H Scripted mode. Don't display headers, and separate properties
3975 * -l Display average latency
3976 * -q Display queue depths
3977 * -w Display latency histograms
3978 * -r Display request size histogram
3979 * -T Display a timestamp in date(1) or Unix format
3981 * This command can be tricky because we want to be able to deal with pool
3982 * creation/destruction as well as vdev configuration changes. The bulk of this
3983 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
3984 * on pool_list_update() to detect the addition of new pools. Configuration
3985 * changes are all handled within libzfs.
3988 zpool_do_iostat(int argc
, char **argv
)
3994 unsigned long count
= 0;
3996 boolean_t verbose
= B_FALSE
;
3997 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
3998 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
3999 boolean_t omit_since_boot
= B_FALSE
;
4000 boolean_t guid
= B_FALSE
;
4001 boolean_t follow_links
= B_FALSE
;
4002 boolean_t full_name
= B_FALSE
;
4003 iostat_cbdata_t cb
= { 0 };
4006 /* Used for printing error message */
4007 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4008 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4010 uint64_t unsupported_flags
;
4013 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4022 follow_links
= B_TRUE
;
4028 get_timestamp_arg(*optarg
);
4052 omit_since_boot
= B_TRUE
;
4058 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4067 cb
.cb_literal
= parsable
;
4068 cb
.cb_scripted
= scripted
;
4071 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4073 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4075 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4076 cb
.cb_iteration
= 0;
4077 cb
.cb_namewidth
= 0;
4078 cb
.cb_verbose
= verbose
;
4080 /* Get our interval and count values (if any) */
4082 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4085 get_interval_count(&argc
, argv
, &interval
, &count
);
4089 /* No args, so just print the defaults. */
4090 } else if (are_all_pools(argc
, argv
)) {
4091 /* All the args are pool names */
4092 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4093 /* All the args are vdevs */
4094 cb
.cb_vdev_names
= argv
;
4095 cb
.cb_vdev_names_count
= argc
;
4096 argc
= 0; /* No pools to process */
4097 } else if (are_all_pools(1, argv
)) {
4098 /* The first arg is a pool name */
4099 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4100 /* ...and the rest are vdev names */
4101 cb
.cb_vdev_names
= argv
+ 1;
4102 cb
.cb_vdev_names_count
= argc
- 1;
4103 argc
= 1; /* One pool to process */
4105 fprintf(stderr
, gettext("Expected either a list of "));
4106 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4107 fprintf(stderr
, " \"%s\", ", argv
[0]);
4108 fprintf(stderr
, gettext("but got:\n"));
4109 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4111 fprintf(stderr
, "\n");
4117 * The args don't make sense. The first arg isn't a pool name,
4118 * nor are all the args vdevs.
4120 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4121 fprintf(stderr
, "\n");
4125 if (cb
.cb_vdev_names_count
!= 0) {
4127 * If user specified vdevs, it implies verbose.
4129 cb
.cb_verbose
= B_TRUE
;
4133 * Construct the list of all interesting pools.
4136 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4139 if (pool_list_count(list
) == 0 && argc
!= 0) {
4140 pool_list_free(list
);
4144 if (pool_list_count(list
) == 0 && interval
== 0) {
4145 pool_list_free(list
);
4146 (void) fprintf(stderr
, gettext("no pools available\n"));
4150 if ((l_histo
|| rq_histo
) && (queues
|| latency
)) {
4151 pool_list_free(list
);
4152 (void) fprintf(stderr
,
4153 gettext("[-r|-w] isn't allowed with [-q|-l]\n"));
4158 if (l_histo
&& rq_histo
) {
4159 pool_list_free(list
);
4160 (void) fprintf(stderr
,
4161 gettext("Only one of [-r|-w] can be passed at a time\n"));
4167 * Enter the main iostat loop.
4173 * Histograms tables look out of place when you try to display
4174 * them with the other stats, so make a rule that you can only
4175 * print histograms by themselves.
4177 cb
.cb_flags
= IOS_L_HISTO_M
;
4178 } else if (rq_histo
) {
4179 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4181 cb
.cb_flags
= IOS_DEFAULT_M
;
4183 cb
.cb_flags
|= IOS_LATENCY_M
;
4185 cb
.cb_flags
|= IOS_QUEUES_M
;
4189 * See if the module supports all the stats we want to display.
4191 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4192 if (unsupported_flags
) {
4196 gettext("The loaded zfs module doesn't support:"));
4198 /* for each bit set in unsupported_flags */
4199 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4200 idx
= lowbit64(f
) - 1;
4201 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4204 fprintf(stderr
, ". Try running a newer module.\n"),
4205 pool_list_free(list
);
4211 if ((npools
= pool_list_count(list
)) == 0)
4212 (void) fprintf(stderr
, gettext("no pools available\n"));
4215 * If this is the first iteration and -y was supplied
4216 * we skip any printing.
4218 boolean_t skip
= (omit_since_boot
&&
4219 cb
.cb_iteration
== 0);
4222 * Refresh all statistics. This is done as an
4223 * explicit step before calculating the maximum name
4224 * width, so that any * configuration changes are
4225 * properly accounted for.
4227 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4231 * Iterate over all pools to determine the maximum width
4232 * for the pool / device name column across all pools.
4234 cb
.cb_namewidth
= 0;
4235 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4238 if (timestamp_fmt
!= NODATE
)
4239 print_timestamp(timestamp_fmt
);
4242 * If it's the first time and we're not skipping it,
4243 * or either skip or verbose mode, print the header.
4245 * The histogram code explicitly prints its header on
4246 * every vdev, so skip this for histograms.
4248 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4249 (skip
!= verbose
)) &&
4250 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4252 print_iostat_header(&cb
);
4255 (void) fsleep(interval
);
4260 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4263 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4265 if (cb
.vcdl
!= NULL
)
4266 free_vdev_cmd_data_list(cb
.vcdl
);
4269 * If there's more than one pool, and we're not in
4270 * verbose mode (which prints a separator for us),
4271 * then print a separator.
4273 * In addition, if we're printing specific vdevs then
4274 * we also want an ending separator.
4276 if (((npools
> 1 && !verbose
&&
4277 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4278 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4279 cb
.cb_vdev_names_count
)) &&
4281 print_iostat_separator(&cb
);
4286 * Flush the output so that redirection to a file isn't buffered
4289 (void) fflush(stdout
);
4294 if (count
!= 0 && --count
== 0)
4297 (void) fsleep(interval
);
4300 pool_list_free(list
);
4305 typedef struct list_cbdata
{
4306 boolean_t cb_verbose
;
4309 boolean_t cb_scripted
;
4310 zprop_list_t
*cb_proplist
;
4311 boolean_t cb_literal
;
4315 * Given a list of columns to display, output appropriate headers for each one.
4318 print_header(list_cbdata_t
*cb
)
4320 zprop_list_t
*pl
= cb
->cb_proplist
;
4321 char headerbuf
[ZPOOL_MAXPROPLEN
];
4323 boolean_t first
= B_TRUE
;
4324 boolean_t right_justify
;
4327 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4328 width
= pl
->pl_width
;
4329 if (first
&& cb
->cb_verbose
) {
4331 * Reset the width to accommodate the verbose listing
4334 width
= cb
->cb_namewidth
;
4342 right_justify
= B_FALSE
;
4343 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4344 header
= zpool_prop_column_name(pl
->pl_prop
);
4345 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4349 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4350 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4351 headerbuf
[i
] = '\0';
4355 if (pl
->pl_next
== NULL
&& !right_justify
)
4356 (void) printf("%s", header
);
4357 else if (right_justify
)
4358 (void) printf("%*s", (int)width
, header
);
4360 (void) printf("%-*s", (int)width
, header
);
4363 (void) printf("\n");
4367 * Given a pool and a list of properties, print out all the properties according
4368 * to the described layout.
4371 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4373 zprop_list_t
*pl
= cb
->cb_proplist
;
4374 boolean_t first
= B_TRUE
;
4375 char property
[ZPOOL_MAXPROPLEN
];
4377 boolean_t right_justify
;
4380 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4382 width
= pl
->pl_width
;
4383 if (first
&& cb
->cb_verbose
) {
4385 * Reset the width to accommodate the verbose listing
4388 width
= cb
->cb_namewidth
;
4392 if (cb
->cb_scripted
)
4393 (void) printf("\t");
4400 right_justify
= B_FALSE
;
4401 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4402 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
4403 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
4408 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4409 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
4410 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
4411 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
4412 sizeof (property
)) == 0) {
4420 * If this is being called in scripted mode, or if this is the
4421 * last column and it is left-justified, don't include a width
4424 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
4425 (void) printf("%s", propstr
);
4426 else if (right_justify
)
4427 (void) printf("%*s", (int)width
, propstr
);
4429 (void) printf("%-*s", (int)width
, propstr
);
4432 (void) printf("\n");
4436 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
4437 boolean_t valid
, enum zfs_nicenum_format format
)
4441 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
4444 case ZPOOL_PROP_EXPANDSZ
:
4446 (void) strlcpy(propval
, "-", sizeof (propval
));
4448 zfs_nicenum_format(value
, propval
, sizeof (propval
),
4451 case ZPOOL_PROP_FRAGMENTATION
:
4452 if (value
== ZFS_FRAG_INVALID
) {
4453 (void) strlcpy(propval
, "-", sizeof (propval
));
4454 } else if (format
== ZFS_NICENUM_RAW
) {
4455 (void) snprintf(propval
, sizeof (propval
), "%llu",
4456 (unsigned long long)value
);
4458 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4459 (unsigned long long)value
);
4462 case ZPOOL_PROP_CAPACITY
:
4463 if (format
== ZFS_NICENUM_RAW
)
4464 (void) snprintf(propval
, sizeof (propval
), "%llu",
4465 (unsigned long long)value
);
4467 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4468 (unsigned long long)value
);
4471 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
4475 (void) strlcpy(propval
, "-", sizeof (propval
));
4478 (void) printf("\t%s", propval
);
4480 (void) printf(" %*s", (int)width
, propval
);
4484 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
4485 list_cbdata_t
*cb
, int depth
)
4491 boolean_t scripted
= cb
->cb_scripted
;
4492 uint64_t islog
= B_FALSE
;
4493 boolean_t haslog
= B_FALSE
;
4494 char *dashes
= "%-*s - - - - - -\n";
4496 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
4497 (uint64_t **)&vs
, &c
) == 0);
4500 boolean_t toplevel
= (vs
->vs_space
!= 0);
4502 enum zfs_nicenum_format format
;
4505 format
= ZFS_NICENUM_RAW
;
4507 format
= ZFS_NICENUM_1024
;
4510 (void) printf("\t%s", name
);
4511 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
4512 (void) printf("%*s%s", depth
, "", name
);
4514 (void) printf("%*s%s%*s", depth
, "", name
,
4515 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
4518 * Print the properties for the individual vdevs. Some
4519 * properties are only applicable to toplevel vdevs. The
4520 * 'toplevel' boolean value is passed to the print_one_column()
4521 * to indicate that the value is valid.
4523 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
4525 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
4527 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
4528 scripted
, toplevel
, format
);
4529 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
4531 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
4532 vs
->vs_fragmentation
, scripted
,
4533 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
4535 cap
= (vs
->vs_space
== 0) ? 0 :
4536 (vs
->vs_alloc
* 100 / vs
->vs_space
);
4537 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
4539 (void) printf("\n");
4542 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
4543 &child
, &children
) != 0)
4546 for (c
= 0; c
< children
; c
++) {
4547 uint64_t ishole
= B_FALSE
;
4549 if (nvlist_lookup_uint64(child
[c
],
4550 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
4553 if (nvlist_lookup_uint64(child
[c
],
4554 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
4559 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4561 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4565 if (haslog
== B_TRUE
) {
4566 /* LINTED E_SEC_PRINTF_VAR_FMT */
4567 (void) printf(dashes
, cb
->cb_namewidth
, "log");
4568 for (c
= 0; c
< children
; c
++) {
4569 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
4570 &islog
) != 0 || !islog
)
4572 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4574 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4579 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
4580 &child
, &children
) == 0 && children
> 0) {
4581 /* LINTED E_SEC_PRINTF_VAR_FMT */
4582 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
4583 for (c
= 0; c
< children
; c
++) {
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_SPARES
, &child
,
4592 &children
) == 0 && children
> 0) {
4593 /* LINTED E_SEC_PRINTF_VAR_FMT */
4594 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
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);
4606 * Generic callback function to list a pool.
4609 list_callback(zpool_handle_t
*zhp
, void *data
)
4611 list_cbdata_t
*cbp
= data
;
4615 config
= zpool_get_config(zhp
, NULL
);
4617 print_pool(zhp
, cbp
);
4618 if (!cbp
->cb_verbose
)
4621 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4623 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
4629 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
4631 * -g Display guid for individual vdev name.
4632 * -H Scripted mode. Don't display headers, and separate properties
4634 * -L Follow links when resolving vdev path name.
4635 * -o List of properties to display. Defaults to
4636 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
4637 * "dedupratio,health,altroot"
4638 * -p Display values in parsable (exact) format.
4639 * -P Display full path for vdev name.
4640 * -T Display a timestamp in date(1) or Unix format
4642 * List all pools in the system, whether or not they're healthy. Output space
4643 * statistics for each one, as well as health status summary.
4646 zpool_do_list(int argc
, char **argv
)
4650 list_cbdata_t cb
= { 0 };
4651 static char default_props
[] =
4652 "name,size,allocated,free,expandsize,fragmentation,capacity,"
4653 "dedupratio,health,altroot";
4654 char *props
= default_props
;
4656 unsigned long count
= 0;
4658 boolean_t first
= B_TRUE
;
4661 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
4664 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4667 cb
.cb_scripted
= B_TRUE
;
4670 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4676 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4679 cb
.cb_literal
= B_TRUE
;
4682 get_timestamp_arg(*optarg
);
4685 cb
.cb_verbose
= B_TRUE
;
4688 (void) fprintf(stderr
, gettext("missing argument for "
4689 "'%c' option\n"), optopt
);
4693 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4702 get_interval_count(&argc
, argv
, &interval
, &count
);
4704 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
4708 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
4712 if (pool_list_count(list
) == 0)
4715 if (timestamp_fmt
!= NODATE
)
4716 print_timestamp(timestamp_fmt
);
4718 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
4722 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
4727 if (count
!= 0 && --count
== 0)
4730 pool_list_free(list
);
4731 (void) fsleep(interval
);
4734 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
4735 (void) printf(gettext("no pools available\n"));
4739 pool_list_free(list
);
4740 zprop_free_list(cb
.cb_proplist
);
4745 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
4747 boolean_t force
= B_FALSE
;
4750 char *poolname
, *old_disk
, *new_disk
;
4751 zpool_handle_t
*zhp
;
4752 nvlist_t
*props
= NULL
;
4757 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
4763 if ((propval
= strchr(optarg
, '=')) == NULL
) {
4764 (void) fprintf(stderr
, gettext("missing "
4765 "'=' for -o option\n"));
4771 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
4772 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
4776 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4785 /* get pool name and check number of arguments */
4787 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4794 (void) fprintf(stderr
,
4795 gettext("missing <device> specification\n"));
4803 (void) fprintf(stderr
,
4804 gettext("missing <new_device> specification\n"));
4807 new_disk
= old_disk
;
4817 (void) fprintf(stderr
, gettext("too many arguments\n"));
4821 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
4826 if (zpool_get_config(zhp
, NULL
) == NULL
) {
4827 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
4834 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
4836 if (nvroot
== NULL
) {
4842 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
4845 nvlist_free(nvroot
);
4852 * zpool replace [-f] <pool> <device> <new_device>
4854 * -f Force attach, even if <new_device> appears to be in use.
4856 * Replace <device> with <new_device>.
4860 zpool_do_replace(int argc
, char **argv
)
4862 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
4866 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
4868 * -f Force attach, even if <new_device> appears to be in use.
4869 * -o Set property=value.
4871 * Attach <new_device> to the mirror containing <device>. If <device> is not
4872 * part of a mirror, then <device> will be transformed into a mirror of
4873 * <device> and <new_device>. In either case, <new_device> will begin life
4874 * with a DTL of [0, now], and will immediately begin to resilver itself.
4877 zpool_do_attach(int argc
, char **argv
)
4879 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
4883 * zpool detach [-f] <pool> <device>
4885 * -f Force detach of <device>, even if DTLs argue against it
4886 * (not supported yet)
4888 * Detach a device from a mirror. The operation will be refused if <device>
4889 * is the last device in the mirror, or if the DTLs indicate that this device
4890 * has the only valid copy of some data.
4894 zpool_do_detach(int argc
, char **argv
)
4897 char *poolname
, *path
;
4898 zpool_handle_t
*zhp
;
4902 while ((c
= getopt(argc
, argv
, "f")) != -1) {
4906 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4915 /* get pool name and check number of arguments */
4917 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
4922 (void) fprintf(stderr
,
4923 gettext("missing <device> specification\n"));
4930 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
4933 ret
= zpool_vdev_detach(zhp
, path
);
4941 * zpool split [-gLnP] [-o prop=val] ...
4943 * [-R altroot] <pool> <newpool> [<device> ...]
4945 * -g Display guid for individual vdev name.
4946 * -L Follow links when resolving vdev path name.
4947 * -n Do not split the pool, but display the resulting layout if
4948 * it were to be split.
4949 * -o Set property=value, or set mount options.
4950 * -P Display full path for vdev name.
4951 * -R Mount the split-off pool under an alternate root.
4953 * Splits the named pool and gives it the new pool name. Devices to be split
4954 * off may be listed, provided that no more than one device is specified
4955 * per top-level vdev mirror. The newly split pool is left in an exported
4956 * state unless -R is specified.
4958 * Restrictions: the top-level of the pool pool must only be made up of
4959 * mirrors; all devices in the pool must be healthy; no device may be
4960 * undergoing a resilvering operation.
4963 zpool_do_split(int argc
, char **argv
)
4965 char *srcpool
, *newpool
, *propval
;
4966 char *mntopts
= NULL
;
4969 zpool_handle_t
*zhp
;
4970 nvlist_t
*config
, *props
= NULL
;
4972 flags
.dryrun
= B_FALSE
;
4973 flags
.import
= B_FALSE
;
4974 flags
.name_flags
= 0;
4977 while ((c
= getopt(argc
, argv
, ":gLR:no:P")) != -1) {
4980 flags
.name_flags
|= VDEV_NAME_GUID
;
4983 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4986 flags
.import
= B_TRUE
;
4988 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
4989 &props
, B_TRUE
) != 0) {
4995 flags
.dryrun
= B_TRUE
;
4998 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5001 if (add_prop_list(optarg
, propval
,
5002 &props
, B_TRUE
) != 0) {
5011 flags
.name_flags
|= VDEV_NAME_PATH
;
5014 (void) fprintf(stderr
, gettext("missing argument for "
5015 "'%c' option\n"), optopt
);
5019 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5026 if (!flags
.import
&& mntopts
!= NULL
) {
5027 (void) fprintf(stderr
, gettext("setting mntopts is only "
5028 "valid when importing the pool\n"));
5036 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5040 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5050 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5055 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5056 if (config
== NULL
) {
5060 (void) printf(gettext("would create '%s' with the "
5061 "following layout:\n\n"), newpool
);
5062 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5069 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5070 nvlist_free(config
);
5076 * The split was successful. Now we need to open the new
5077 * pool and import it.
5079 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5080 nvlist_free(config
);
5084 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5085 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5087 (void) fprintf(stderr
, gettext("Split was successful, but "
5088 "the datasets could not all be mounted\n"));
5089 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5090 "different altroot\n"), "zpool import");
5093 nvlist_free(config
);
5102 * zpool online <pool> <device> ...
5105 zpool_do_online(int argc
, char **argv
)
5109 zpool_handle_t
*zhp
;
5111 vdev_state_t newstate
;
5115 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5118 flags
|= ZFS_ONLINE_EXPAND
;
5122 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5131 /* get pool name and check number of arguments */
5133 (void) fprintf(stderr
, gettext("missing pool name\n"));
5137 (void) fprintf(stderr
, gettext("missing device name\n"));
5143 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5146 for (i
= 1; i
< argc
; i
++) {
5147 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5148 if (newstate
!= VDEV_STATE_HEALTHY
) {
5149 (void) printf(gettext("warning: device '%s' "
5150 "onlined, but remains in faulted state\n"),
5152 if (newstate
== VDEV_STATE_FAULTED
)
5153 (void) printf(gettext("use 'zpool "
5154 "clear' to restore a faulted "
5157 (void) printf(gettext("use 'zpool "
5158 "replace' to replace devices "
5159 "that are no longer present\n"));
5172 * zpool offline [-ft] <pool> <device> ...
5174 * -f Force the device into the offline state, even if doing
5175 * so would appear to compromise pool availability.
5176 * (not supported yet)
5178 * -t Only take the device off-line temporarily. The offline
5179 * state will not be persistent across reboots.
5183 zpool_do_offline(int argc
, char **argv
)
5187 zpool_handle_t
*zhp
;
5189 boolean_t istmp
= B_FALSE
;
5192 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5199 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5208 /* get pool name and check number of arguments */
5210 (void) fprintf(stderr
, gettext("missing pool name\n"));
5214 (void) fprintf(stderr
, gettext("missing device name\n"));
5220 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5223 for (i
= 1; i
< argc
; i
++) {
5224 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5234 * zpool clear <pool> [device]
5236 * Clear all errors associated with a pool or a particular device.
5239 zpool_do_clear(int argc
, char **argv
)
5243 boolean_t dryrun
= B_FALSE
;
5244 boolean_t do_rewind
= B_FALSE
;
5245 boolean_t xtreme_rewind
= B_FALSE
;
5246 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5247 nvlist_t
*policy
= NULL
;
5248 zpool_handle_t
*zhp
;
5249 char *pool
, *device
;
5252 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5261 xtreme_rewind
= B_TRUE
;
5264 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5274 (void) fprintf(stderr
, gettext("missing pool name\n"));
5279 (void) fprintf(stderr
, gettext("too many arguments\n"));
5283 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5284 (void) fprintf(stderr
,
5285 gettext("-n or -X only meaningful with -F\n"));
5289 rewind_policy
= ZPOOL_TRY_REWIND
;
5291 rewind_policy
= ZPOOL_DO_REWIND
;
5293 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5295 /* In future, further rewind policy choices can be passed along here */
5296 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5297 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
5301 device
= argc
== 2 ? argv
[1] : NULL
;
5303 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5304 nvlist_free(policy
);
5308 if (zpool_clear(zhp
, device
, policy
) != 0)
5313 nvlist_free(policy
);
5319 * zpool reguid <pool>
5322 zpool_do_reguid(int argc
, char **argv
)
5326 zpool_handle_t
*zhp
;
5330 while ((c
= getopt(argc
, argv
, "")) != -1) {
5333 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5342 /* get pool name and check number of arguments */
5344 (void) fprintf(stderr
, gettext("missing pool name\n"));
5349 (void) fprintf(stderr
, gettext("too many arguments\n"));
5354 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5357 ret
= zpool_reguid(zhp
);
5365 * zpool reopen <pool>
5367 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5370 zpool_do_reopen(int argc
, char **argv
)
5374 zpool_handle_t
*zhp
;
5378 while ((c
= getopt(argc
, argv
, "")) != -1) {
5381 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5391 (void) fprintf(stderr
, gettext("missing pool name\n"));
5396 (void) fprintf(stderr
, gettext("too many arguments\n"));
5401 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
5404 ret
= zpool_reopen(zhp
);
5409 typedef struct scrub_cbdata
{
5416 scrub_callback(zpool_handle_t
*zhp
, void *data
)
5418 scrub_cbdata_t
*cb
= data
;
5422 * Ignore faulted pools.
5424 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5425 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
5426 "currently unavailable\n"), zpool_get_name(zhp
));
5430 err
= zpool_scan(zhp
, cb
->cb_type
);
5436 * zpool scrub [-s] <pool> ...
5438 * -s Stop. Stops any in-progress scrub.
5441 zpool_do_scrub(int argc
, char **argv
)
5446 cb
.cb_type
= POOL_SCAN_SCRUB
;
5449 while ((c
= getopt(argc
, argv
, "s")) != -1) {
5452 cb
.cb_type
= POOL_SCAN_NONE
;
5455 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5467 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5471 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
5475 * Print out detailed scrub status.
5478 print_scan_status(pool_scan_stat_t
*ps
)
5481 uint64_t elapsed
, mins_left
, hours_left
;
5482 uint64_t pass_exam
, examined
, total
;
5484 double fraction_done
;
5485 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
5487 (void) printf(gettext(" scan: "));
5489 /* If there's never been a scan, there's not much to say. */
5490 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
5491 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
5492 (void) printf(gettext("none requested\n"));
5496 start
= ps
->pss_start_time
;
5497 end
= ps
->pss_end_time
;
5498 zfs_nicenum(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
5500 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
5501 ps
->pss_func
== POOL_SCAN_RESILVER
);
5503 * Scan is finished or canceled.
5505 if (ps
->pss_state
== DSS_FINISHED
) {
5506 uint64_t minutes_taken
= (end
- start
) / 60;
5509 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5510 fmt
= gettext("scrub repaired %s in %lluh%um with "
5511 "%llu errors on %s");
5512 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5513 fmt
= gettext("resilvered %s in %lluh%um with "
5514 "%llu errors on %s");
5517 (void) printf(fmt
, processed_buf
,
5518 (u_longlong_t
)(minutes_taken
/ 60),
5519 (uint_t
)(minutes_taken
% 60),
5520 (u_longlong_t
)ps
->pss_errors
,
5521 ctime((time_t *)&end
));
5523 } else if (ps
->pss_state
== DSS_CANCELED
) {
5524 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5525 (void) printf(gettext("scrub canceled on %s"),
5527 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5528 (void) printf(gettext("resilver canceled on %s"),
5534 assert(ps
->pss_state
== DSS_SCANNING
);
5537 * Scan is in progress.
5539 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5540 (void) printf(gettext("scrub in progress since %s"),
5542 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5543 (void) printf(gettext("resilver in progress since %s"),
5547 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
5548 total
= ps
->pss_to_examine
;
5549 fraction_done
= (double)examined
/ total
;
5551 /* elapsed time for this pass */
5552 elapsed
= time(NULL
) - ps
->pss_pass_start
;
5553 elapsed
= elapsed
? elapsed
: 1;
5554 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
5555 rate
= pass_exam
/ elapsed
;
5556 rate
= rate
? rate
: 1;
5557 mins_left
= ((total
- examined
) / rate
) / 60;
5558 hours_left
= mins_left
/ 60;
5560 zfs_nicenum(examined
, examined_buf
, sizeof (examined_buf
));
5561 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
5562 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
5565 * do not print estimated time if hours_left is more than 30 days
5567 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
5568 examined_buf
, total_buf
, rate_buf
);
5569 if (hours_left
< (30 * 24)) {
5570 (void) printf(gettext(", %lluh%um to go\n"),
5571 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
5573 (void) printf(gettext(
5574 ", (scan is slow, no estimated time)\n"));
5577 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5578 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
5579 processed_buf
, 100 * fraction_done
);
5580 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5581 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
5582 processed_buf
, 100 * fraction_done
);
5587 print_error_log(zpool_handle_t
*zhp
)
5589 nvlist_t
*nverrlist
= NULL
;
5592 size_t len
= MAXPATHLEN
* 2;
5594 if (zpool_get_errlog(zhp
, &nverrlist
) != 0) {
5595 (void) printf("errors: List of errors unavailable "
5596 "(insufficient privileges)\n");
5600 (void) printf("errors: Permanent errors have been "
5601 "detected in the following files:\n\n");
5603 pathname
= safe_malloc(len
);
5605 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
5607 uint64_t dsobj
, obj
;
5609 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
5610 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
5612 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
5614 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
5615 (void) printf("%7s %s\n", "", pathname
);
5618 nvlist_free(nverrlist
);
5622 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
5631 (void) printf(gettext("\tspares\n"));
5633 for (i
= 0; i
< nspares
; i
++) {
5634 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
5636 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
5642 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
5651 (void) printf(gettext("\tcache\n"));
5653 for (i
= 0; i
< nl2cache
; i
++) {
5654 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
5656 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
5662 print_dedup_stats(nvlist_t
*config
)
5664 ddt_histogram_t
*ddh
;
5670 * If the pool was faulted then we may not have been able to
5671 * obtain the config. Otherwise, if we have anything in the dedup
5672 * table continue processing the stats.
5674 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
5675 (uint64_t **)&ddo
, &c
) != 0)
5678 (void) printf("\n");
5679 (void) printf(gettext(" dedup: "));
5680 if (ddo
->ddo_count
== 0) {
5681 (void) printf(gettext("no DDT entries\n"));
5685 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
5686 (u_longlong_t
)ddo
->ddo_count
,
5687 (u_longlong_t
)ddo
->ddo_dspace
,
5688 (u_longlong_t
)ddo
->ddo_mspace
);
5690 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
5691 (uint64_t **)&dds
, &c
) == 0);
5692 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
5693 (uint64_t **)&ddh
, &c
) == 0);
5694 zpool_dump_ddt(dds
, ddh
);
5698 * Display a summary of pool status. Displays a summary such as:
5702 * reason: One or more devices ...
5703 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
5709 * When given the '-v' option, we print out the complete config. If the '-e'
5710 * option is specified, then we print out error rate information as well.
5713 status_callback(zpool_handle_t
*zhp
, void *data
)
5715 status_cbdata_t
*cbp
= data
;
5716 nvlist_t
*config
, *nvroot
;
5718 zpool_status_t reason
;
5719 zpool_errata_t errata
;
5724 config
= zpool_get_config(zhp
, NULL
);
5725 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
5730 * If we were given 'zpool status -x', only report those pools with
5733 if (cbp
->cb_explain
&&
5734 (reason
== ZPOOL_STATUS_OK
||
5735 reason
== ZPOOL_STATUS_VERSION_OLDER
||
5736 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
5737 if (!cbp
->cb_allpools
) {
5738 (void) printf(gettext("pool '%s' is healthy\n"),
5739 zpool_get_name(zhp
));
5741 cbp
->cb_first
= B_FALSE
;
5747 cbp
->cb_first
= B_FALSE
;
5749 (void) printf("\n");
5751 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5753 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
5754 (uint64_t **)&vs
, &c
) == 0);
5755 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
5757 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
5758 (void) printf(gettext(" state: %s\n"), health
);
5761 case ZPOOL_STATUS_MISSING_DEV_R
:
5762 (void) printf(gettext("status: One or more devices could not "
5763 "be opened. Sufficient replicas exist for\n\tthe pool to "
5764 "continue functioning in a degraded state.\n"));
5765 (void) printf(gettext("action: Attach the missing device and "
5766 "online it using 'zpool online'.\n"));
5769 case ZPOOL_STATUS_MISSING_DEV_NR
:
5770 (void) printf(gettext("status: One or more devices could not "
5771 "be opened. There are insufficient\n\treplicas for the "
5772 "pool to continue functioning.\n"));
5773 (void) printf(gettext("action: Attach the missing device and "
5774 "online it using 'zpool online'.\n"));
5777 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
5778 (void) printf(gettext("status: One or more devices could not "
5779 "be used because the label is missing or\n\tinvalid. "
5780 "Sufficient replicas exist for the pool to continue\n\t"
5781 "functioning in a degraded state.\n"));
5782 (void) printf(gettext("action: Replace the device using "
5783 "'zpool replace'.\n"));
5786 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
5787 (void) printf(gettext("status: One or more devices could not "
5788 "be used because the label is missing \n\tor invalid. "
5789 "There are insufficient replicas for the pool to "
5790 "continue\n\tfunctioning.\n"));
5791 zpool_explain_recover(zpool_get_handle(zhp
),
5792 zpool_get_name(zhp
), reason
, config
);
5795 case ZPOOL_STATUS_FAILING_DEV
:
5796 (void) printf(gettext("status: One or more devices has "
5797 "experienced an unrecoverable error. An\n\tattempt was "
5798 "made to correct the error. Applications are "
5800 (void) printf(gettext("action: Determine if the device needs "
5801 "to be replaced, and clear the errors\n\tusing "
5802 "'zpool clear' or replace the device with 'zpool "
5806 case ZPOOL_STATUS_OFFLINE_DEV
:
5807 (void) printf(gettext("status: One or more devices has "
5808 "been taken offline by the administrator.\n\tSufficient "
5809 "replicas exist for the pool to continue functioning in "
5810 "a\n\tdegraded state.\n"));
5811 (void) printf(gettext("action: Online the device using "
5812 "'zpool online' or replace the device with\n\t'zpool "
5816 case ZPOOL_STATUS_REMOVED_DEV
:
5817 (void) printf(gettext("status: One or more devices has "
5818 "been removed by the administrator.\n\tSufficient "
5819 "replicas exist for the pool to continue functioning in "
5820 "a\n\tdegraded state.\n"));
5821 (void) printf(gettext("action: Online the device using "
5822 "'zpool online' or replace the device with\n\t'zpool "
5826 case ZPOOL_STATUS_RESILVERING
:
5827 (void) printf(gettext("status: One or more devices is "
5828 "currently being resilvered. The pool will\n\tcontinue "
5829 "to function, possibly in a degraded state.\n"));
5830 (void) printf(gettext("action: Wait for the resilver to "
5834 case ZPOOL_STATUS_CORRUPT_DATA
:
5835 (void) printf(gettext("status: One or more devices has "
5836 "experienced an error resulting in data\n\tcorruption. "
5837 "Applications may be affected.\n"));
5838 (void) printf(gettext("action: Restore the file in question "
5839 "if possible. Otherwise restore the\n\tentire pool from "
5843 case ZPOOL_STATUS_CORRUPT_POOL
:
5844 (void) printf(gettext("status: The pool metadata is corrupted "
5845 "and the pool cannot be opened.\n"));
5846 zpool_explain_recover(zpool_get_handle(zhp
),
5847 zpool_get_name(zhp
), reason
, config
);
5850 case ZPOOL_STATUS_VERSION_OLDER
:
5851 (void) printf(gettext("status: The pool is formatted using a "
5852 "legacy on-disk format. The pool can\n\tstill be used, "
5853 "but some features are unavailable.\n"));
5854 (void) printf(gettext("action: Upgrade the pool using 'zpool "
5855 "upgrade'. Once this is done, the\n\tpool will no longer "
5856 "be accessible on software that does not support\n\t"
5857 "feature flags.\n"));
5860 case ZPOOL_STATUS_VERSION_NEWER
:
5861 (void) printf(gettext("status: The pool has been upgraded to a "
5862 "newer, incompatible on-disk version.\n\tThe pool cannot "
5863 "be accessed on this system.\n"));
5864 (void) printf(gettext("action: Access the pool from a system "
5865 "running more recent software, or\n\trestore the pool from "
5869 case ZPOOL_STATUS_FEAT_DISABLED
:
5870 (void) printf(gettext("status: Some supported features are not "
5871 "enabled on the pool. The pool can\n\tstill be used, but "
5872 "some features are unavailable.\n"));
5873 (void) printf(gettext("action: Enable all features using "
5874 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
5875 "longer be accessible by software that does not support\n\t"
5876 "the features. See zpool-features(5) for details.\n"));
5879 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
5880 (void) printf(gettext("status: The pool cannot be accessed on "
5881 "this system because it uses the\n\tfollowing feature(s) "
5882 "not supported on this system:\n"));
5883 zpool_print_unsup_feat(config
);
5884 (void) printf("\n");
5885 (void) printf(gettext("action: Access the pool from a system "
5886 "that supports the required feature(s),\n\tor restore the "
5887 "pool from backup.\n"));
5890 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
5891 (void) printf(gettext("status: The pool can only be accessed "
5892 "in read-only mode on this system. It\n\tcannot be "
5893 "accessed in read-write mode because it uses the "
5894 "following\n\tfeature(s) not supported on this system:\n"));
5895 zpool_print_unsup_feat(config
);
5896 (void) printf("\n");
5897 (void) printf(gettext("action: The pool cannot be accessed in "
5898 "read-write mode. Import the pool with\n"
5899 "\t\"-o readonly=on\", access the pool from a system that "
5900 "supports the\n\trequired feature(s), or restore the "
5901 "pool from backup.\n"));
5904 case ZPOOL_STATUS_FAULTED_DEV_R
:
5905 (void) printf(gettext("status: One or more devices are "
5906 "faulted in response to persistent errors.\n\tSufficient "
5907 "replicas exist for the pool to continue functioning "
5908 "in a\n\tdegraded state.\n"));
5909 (void) printf(gettext("action: Replace the faulted device, "
5910 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
5913 case ZPOOL_STATUS_FAULTED_DEV_NR
:
5914 (void) printf(gettext("status: One or more devices are "
5915 "faulted in response to persistent errors. There are "
5916 "insufficient replicas for the pool to\n\tcontinue "
5918 (void) printf(gettext("action: Destroy and re-create the pool "
5919 "from a backup source. Manually marking the device\n"
5920 "\trepaired using 'zpool clear' may allow some data "
5921 "to be recovered.\n"));
5924 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
5925 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
5926 (void) printf(gettext("status: One or more devices are "
5927 "faulted in response to IO failures.\n"));
5928 (void) printf(gettext("action: Make sure the affected devices "
5929 "are connected, then run 'zpool clear'.\n"));
5932 case ZPOOL_STATUS_BAD_LOG
:
5933 (void) printf(gettext("status: An intent log record "
5934 "could not be read.\n"
5935 "\tWaiting for administrator intervention to fix the "
5936 "faulted pool.\n"));
5937 (void) printf(gettext("action: Either restore the affected "
5938 "device(s) and run 'zpool online',\n"
5939 "\tor ignore the intent log records by running "
5940 "'zpool clear'.\n"));
5943 case ZPOOL_STATUS_HOSTID_MISMATCH
:
5944 (void) printf(gettext("status: Mismatch between pool hostid "
5945 "and system hostid on imported pool.\n\tThis pool was "
5946 "previously imported into a system with a different "
5947 "hostid,\n\tand then was verbatim imported into this "
5949 (void) printf(gettext("action: Export this pool on all systems "
5950 "on which it is imported.\n"
5951 "\tThen import it to correct the mismatch.\n"));
5954 case ZPOOL_STATUS_ERRATA
:
5955 (void) printf(gettext("status: Errata #%d detected.\n"),
5959 case ZPOOL_ERRATA_NONE
:
5962 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
5963 (void) printf(gettext("action: To correct the issue "
5964 "run 'zpool scrub'.\n"));
5969 * All errata which allow the pool to be imported
5970 * must contain an action message.
5978 * The remaining errors can't actually be generated, yet.
5980 assert(reason
== ZPOOL_STATUS_OK
);
5984 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
5987 if (config
!= NULL
) {
5989 nvlist_t
**spares
, **l2cache
;
5990 uint_t nspares
, nl2cache
;
5991 pool_scan_stat_t
*ps
= NULL
;
5993 (void) nvlist_lookup_uint64_array(nvroot
,
5994 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
5995 print_scan_status(ps
);
5997 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
5998 cbp
->cb_name_flags
);
5999 if (cbp
->cb_namewidth
< 10)
6000 cbp
->cb_namewidth
= 10;
6002 (void) printf(gettext("config:\n\n"));
6003 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"),
6004 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6006 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6009 if (num_logs(nvroot
) > 0)
6010 print_logs(zhp
, cbp
, nvroot
);
6011 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6012 &l2cache
, &nl2cache
) == 0)
6013 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6015 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6016 &spares
, &nspares
) == 0)
6017 print_spares(zhp
, cbp
, spares
, nspares
);
6019 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6021 nvlist_t
*nverrlist
= NULL
;
6024 * If the approximate error count is small, get a
6025 * precise count by fetching the entire log and
6026 * uniquifying the results.
6028 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6029 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6034 while ((elem
= nvlist_next_nvpair(nverrlist
,
6039 nvlist_free(nverrlist
);
6041 (void) printf("\n");
6044 (void) printf(gettext("errors: No known data "
6046 else if (!cbp
->cb_verbose
)
6047 (void) printf(gettext("errors: %llu data "
6048 "errors, use '-v' for a list\n"),
6049 (u_longlong_t
)nerr
);
6051 print_error_log(zhp
);
6054 if (cbp
->cb_dedup_stats
)
6055 print_dedup_stats(config
);
6057 (void) printf(gettext("config: The configuration cannot be "
6065 * zpool status [-c CMD] [-gLPvx] [-T d|u] [pool] ... [interval [count]]
6067 * -c CMD For each vdev, run command CMD
6068 * -g Display guid for individual vdev name.
6069 * -L Follow links when resolving vdev path name.
6070 * -P Display full path for vdev name.
6071 * -v Display complete error logs
6072 * -x Display only pools with potential problems
6073 * -D Display dedup status (undocumented)
6074 * -T Display a timestamp in date(1) or Unix format
6076 * Describes the health status of all pools or some subset.
6079 zpool_do_status(int argc
, char **argv
)
6084 unsigned long count
= 0;
6085 status_cbdata_t cb
= { 0 };
6089 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
6095 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6098 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6101 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6104 cb
.cb_verbose
= B_TRUE
;
6107 cb
.cb_explain
= B_TRUE
;
6110 cb
.cb_dedup_stats
= B_TRUE
;
6113 get_timestamp_arg(*optarg
);
6116 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6125 get_interval_count(&argc
, argv
, &interval
, &count
);
6128 cb
.cb_allpools
= B_TRUE
;
6130 cb
.cb_first
= B_TRUE
;
6131 cb
.cb_print_status
= B_TRUE
;
6134 if (timestamp_fmt
!= NODATE
)
6135 print_timestamp(timestamp_fmt
);
6138 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
);
6140 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
6141 status_callback
, &cb
);
6143 if (cb
.vcdl
!= NULL
)
6144 free_vdev_cmd_data_list(cb
.vcdl
);
6146 if (argc
== 0 && cb
.cb_count
== 0)
6147 (void) fprintf(stderr
, gettext("no pools available\n"));
6148 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
6149 (void) printf(gettext("all pools are healthy\n"));
6157 if (count
!= 0 && --count
== 0)
6160 (void) fsleep(interval
);
6166 typedef struct upgrade_cbdata
{
6169 uint64_t cb_version
;
6174 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
6176 int zfs_version
= (int) zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
6177 int *count
= (int *)unsupp_fs
;
6179 if (zfs_version
> ZPL_VERSION
) {
6180 (void) printf(gettext("%s (v%d) is not supported by this "
6181 "implementation of ZFS.\n"),
6182 zfs_get_name(zhp
), zfs_version
);
6186 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
6194 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
6198 uint64_t oldversion
;
6201 config
= zpool_get_config(zhp
, NULL
);
6202 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6205 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
6206 assert(oldversion
< version
);
6208 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
6213 (void) fprintf(stderr
, gettext("Upgrade not performed due "
6214 "to %d unsupported filesystems (max v%d).\n"),
6215 unsupp_fs
, (int) ZPL_VERSION
);
6219 ret
= zpool_upgrade(zhp
, version
);
6223 if (version
>= SPA_VERSION_FEATURES
) {
6224 (void) printf(gettext("Successfully upgraded "
6225 "'%s' from version %llu to feature flags.\n"),
6226 zpool_get_name(zhp
), (u_longlong_t
) oldversion
);
6228 (void) printf(gettext("Successfully upgraded "
6229 "'%s' from version %llu to version %llu.\n"),
6230 zpool_get_name(zhp
), (u_longlong_t
) oldversion
,
6231 (u_longlong_t
) version
);
6238 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
6241 boolean_t firstff
= B_TRUE
;
6242 nvlist_t
*enabled
= zpool_get_features(zhp
);
6245 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6246 const char *fname
= spa_feature_table
[i
].fi_uname
;
6247 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6248 if (!nvlist_exists(enabled
, fguid
)) {
6250 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
6251 ret
= zpool_set_prop(zhp
, propname
,
6252 ZFS_FEATURE_ENABLED
);
6260 (void) printf(gettext("Enabled the "
6261 "following features on '%s':\n"),
6262 zpool_get_name(zhp
));
6265 (void) printf(gettext(" %s\n"), fname
);
6276 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
6278 upgrade_cbdata_t
*cbp
= arg
;
6281 boolean_t printnl
= B_FALSE
;
6284 config
= zpool_get_config(zhp
, NULL
);
6285 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6288 assert(SPA_VERSION_IS_SUPPORTED(version
));
6290 if (version
< cbp
->cb_version
) {
6291 cbp
->cb_first
= B_FALSE
;
6292 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6298 * If they did "zpool upgrade -a", then we could
6299 * be doing ioctls to different pools. We need
6300 * to log this history once to each pool, and bypass
6301 * the normal history logging that happens in main().
6303 (void) zpool_log_history(g_zfs
, history_str
);
6304 log_history
= B_FALSE
;
6307 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6309 ret
= upgrade_enable_all(zhp
, &count
);
6314 cbp
->cb_first
= B_FALSE
;
6320 (void) printf(gettext("\n"));
6327 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
6329 upgrade_cbdata_t
*cbp
= arg
;
6333 config
= zpool_get_config(zhp
, NULL
);
6334 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6337 assert(SPA_VERSION_IS_SUPPORTED(version
));
6339 if (version
< SPA_VERSION_FEATURES
) {
6340 if (cbp
->cb_first
) {
6341 (void) printf(gettext("The following pools are "
6342 "formatted with legacy version numbers and can\n"
6343 "be upgraded to use feature flags. After "
6344 "being upgraded, these pools\nwill no "
6345 "longer be accessible by software that does not "
6346 "support feature\nflags.\n\n"));
6347 (void) printf(gettext("VER POOL\n"));
6348 (void) printf(gettext("--- ------------\n"));
6349 cbp
->cb_first
= B_FALSE
;
6352 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
6353 zpool_get_name(zhp
));
6360 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
6362 upgrade_cbdata_t
*cbp
= arg
;
6366 config
= zpool_get_config(zhp
, NULL
);
6367 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6370 if (version
>= SPA_VERSION_FEATURES
) {
6372 boolean_t poolfirst
= B_TRUE
;
6373 nvlist_t
*enabled
= zpool_get_features(zhp
);
6375 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6376 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6377 const char *fname
= spa_feature_table
[i
].fi_uname
;
6378 if (!nvlist_exists(enabled
, fguid
)) {
6379 if (cbp
->cb_first
) {
6380 (void) printf(gettext("\nSome "
6381 "supported features are not "
6382 "enabled on the following pools. "
6383 "Once a\nfeature is enabled the "
6384 "pool may become incompatible with "
6385 "software\nthat does not support "
6387 "zpool-features(5) for "
6389 (void) printf(gettext("POOL "
6391 (void) printf(gettext("------"
6393 cbp
->cb_first
= B_FALSE
;
6397 (void) printf(gettext("%s\n"),
6398 zpool_get_name(zhp
));
6399 poolfirst
= B_FALSE
;
6402 (void) printf(gettext(" %s\n"), fname
);
6405 * If they did "zpool upgrade -a", then we could
6406 * be doing ioctls to different pools. We need
6407 * to log this history once to each pool, and bypass
6408 * the normal history logging that happens in main().
6410 (void) zpool_log_history(g_zfs
, history_str
);
6411 log_history
= B_FALSE
;
6420 upgrade_one(zpool_handle_t
*zhp
, void *data
)
6422 boolean_t printnl
= B_FALSE
;
6423 upgrade_cbdata_t
*cbp
= data
;
6424 uint64_t cur_version
;
6427 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
6428 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
6429 "Pool 'log' must be renamed using export and import"
6434 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
6435 if (cur_version
> cbp
->cb_version
) {
6436 (void) printf(gettext("Pool '%s' is already formatted "
6437 "using more current version '%llu'.\n\n"),
6438 zpool_get_name(zhp
), (u_longlong_t
) cur_version
);
6442 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
6443 (void) printf(gettext("Pool '%s' is already formatted "
6444 "using version %llu.\n\n"), zpool_get_name(zhp
),
6445 (u_longlong_t
) cbp
->cb_version
);
6449 if (cur_version
!= cbp
->cb_version
) {
6451 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6456 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6458 ret
= upgrade_enable_all(zhp
, &count
);
6464 } else if (cur_version
== SPA_VERSION
) {
6465 (void) printf(gettext("Pool '%s' already has all "
6466 "supported features enabled.\n"),
6467 zpool_get_name(zhp
));
6472 (void) printf(gettext("\n"));
6481 * zpool upgrade [-V version] <-a | pool ...>
6483 * With no arguments, display downrev'd ZFS pool available for upgrade.
6484 * Individual pools can be upgraded by specifying the pool, and '-a' will
6485 * upgrade all pools.
6488 zpool_do_upgrade(int argc
, char **argv
)
6491 upgrade_cbdata_t cb
= { 0 };
6493 boolean_t showversions
= B_FALSE
;
6494 boolean_t upgradeall
= B_FALSE
;
6499 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
6502 upgradeall
= B_TRUE
;
6505 showversions
= B_TRUE
;
6508 cb
.cb_version
= strtoll(optarg
, &end
, 10);
6510 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
6511 (void) fprintf(stderr
,
6512 gettext("invalid version '%s'\n"), optarg
);
6517 (void) fprintf(stderr
, gettext("missing argument for "
6518 "'%c' option\n"), optopt
);
6522 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6533 if (cb
.cb_version
== 0) {
6534 cb
.cb_version
= SPA_VERSION
;
6535 } else if (!upgradeall
&& argc
== 0) {
6536 (void) fprintf(stderr
, gettext("-V option is "
6537 "incompatible with other arguments\n"));
6542 if (upgradeall
|| argc
!= 0) {
6543 (void) fprintf(stderr
, gettext("-v option is "
6544 "incompatible with other arguments\n"));
6547 } else if (upgradeall
) {
6549 (void) fprintf(stderr
, gettext("-a option should not "
6550 "be used along with a pool name\n"));
6555 (void) printf(gettext("This system supports ZFS pool feature "
6560 (void) printf(gettext("The following features are "
6562 (void) printf(gettext("FEAT DESCRIPTION\n"));
6563 (void) printf("----------------------------------------------"
6564 "---------------\n");
6565 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6566 zfeature_info_t
*fi
= &spa_feature_table
[i
];
6568 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
6569 " (read-only compatible)" : "";
6571 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
6572 (void) printf(" %s\n", fi
->fi_desc
);
6574 (void) printf("\n");
6576 (void) printf(gettext("The following legacy versions are also "
6578 (void) printf(gettext("VER DESCRIPTION\n"));
6579 (void) printf("--- -----------------------------------------"
6580 "---------------\n");
6581 (void) printf(gettext(" 1 Initial ZFS version\n"));
6582 (void) printf(gettext(" 2 Ditto blocks "
6583 "(replicated metadata)\n"));
6584 (void) printf(gettext(" 3 Hot spares and double parity "
6586 (void) printf(gettext(" 4 zpool history\n"));
6587 (void) printf(gettext(" 5 Compression using the gzip "
6589 (void) printf(gettext(" 6 bootfs pool property\n"));
6590 (void) printf(gettext(" 7 Separate intent log devices\n"));
6591 (void) printf(gettext(" 8 Delegated administration\n"));
6592 (void) printf(gettext(" 9 refquota and refreservation "
6594 (void) printf(gettext(" 10 Cache devices\n"));
6595 (void) printf(gettext(" 11 Improved scrub performance\n"));
6596 (void) printf(gettext(" 12 Snapshot properties\n"));
6597 (void) printf(gettext(" 13 snapused property\n"));
6598 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
6599 (void) printf(gettext(" 15 user/group space accounting\n"));
6600 (void) printf(gettext(" 16 stmf property support\n"));
6601 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
6602 (void) printf(gettext(" 18 Snapshot user holds\n"));
6603 (void) printf(gettext(" 19 Log device removal\n"));
6604 (void) printf(gettext(" 20 Compression using zle "
6605 "(zero-length encoding)\n"));
6606 (void) printf(gettext(" 21 Deduplication\n"));
6607 (void) printf(gettext(" 22 Received properties\n"));
6608 (void) printf(gettext(" 23 Slim ZIL\n"));
6609 (void) printf(gettext(" 24 System attributes\n"));
6610 (void) printf(gettext(" 25 Improved scrub stats\n"));
6611 (void) printf(gettext(" 26 Improved snapshot deletion "
6613 (void) printf(gettext(" 27 Improved snapshot creation "
6615 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
6616 (void) printf(gettext("\nFor more information on a particular "
6617 "version, including supported releases,\n"));
6618 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
6619 } else if (argc
== 0 && upgradeall
) {
6620 cb
.cb_first
= B_TRUE
;
6621 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
6622 if (ret
== 0 && cb
.cb_first
) {
6623 if (cb
.cb_version
== SPA_VERSION
) {
6624 (void) printf(gettext("All pools are already "
6625 "formatted using feature flags.\n\n"));
6626 (void) printf(gettext("Every feature flags "
6627 "pool already has all supported features "
6630 (void) printf(gettext("All pools are already "
6631 "formatted with version %llu or higher.\n"),
6632 (u_longlong_t
) cb
.cb_version
);
6635 } else if (argc
== 0) {
6636 cb
.cb_first
= B_TRUE
;
6637 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
6641 (void) printf(gettext("All pools are formatted "
6642 "using feature flags.\n\n"));
6644 (void) printf(gettext("\nUse 'zpool upgrade -v' "
6645 "for a list of available legacy versions.\n"));
6648 cb
.cb_first
= B_TRUE
;
6649 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
6653 (void) printf(gettext("Every feature flags pool has "
6654 "all supported features enabled.\n"));
6656 (void) printf(gettext("\n"));
6659 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
6666 typedef struct hist_cbdata
{
6673 * Print out the command history for a specific pool.
6676 get_history_one(zpool_handle_t
*zhp
, void *data
)
6682 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
6684 cb
->first
= B_FALSE
;
6686 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
6688 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
6691 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
6692 &records
, &numrecords
) == 0);
6693 for (i
= 0; i
< numrecords
; i
++) {
6694 nvlist_t
*rec
= records
[i
];
6697 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
6701 tsec
= fnvlist_lookup_uint64(records
[i
],
6703 (void) localtime_r(&tsec
, &t
);
6704 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
6707 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
6708 (void) printf("%s %s", tbuf
,
6709 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
6710 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
6712 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
6715 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
6716 (void) printf("%s unrecognized record:\n",
6718 dump_nvlist(rec
, 4);
6721 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
6722 zfs_history_event_names
[ievent
],
6723 (longlong_t
) fnvlist_lookup_uint64(
6724 rec
, ZPOOL_HIST_TXG
),
6725 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
6726 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
6729 (void) printf("%s [txg:%lld] %s", tbuf
,
6730 (longlong_t
) fnvlist_lookup_uint64(
6731 rec
, ZPOOL_HIST_TXG
),
6732 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
6733 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
6734 (void) printf(" %s (%llu)",
6735 fnvlist_lookup_string(rec
,
6737 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
6740 (void) printf(" %s", fnvlist_lookup_string(rec
,
6741 ZPOOL_HIST_INT_STR
));
6742 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
6745 (void) printf("%s ioctl %s\n", tbuf
,
6746 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
6747 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
6748 (void) printf(" input:\n");
6749 dump_nvlist(fnvlist_lookup_nvlist(rec
,
6750 ZPOOL_HIST_INPUT_NVL
), 8);
6752 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
6753 (void) printf(" output:\n");
6754 dump_nvlist(fnvlist_lookup_nvlist(rec
,
6755 ZPOOL_HIST_OUTPUT_NVL
), 8);
6760 (void) printf("%s unrecognized record:\n", tbuf
);
6761 dump_nvlist(rec
, 4);
6765 (void) printf("\n");
6768 (void) printf(" [");
6769 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
6770 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
6771 struct passwd
*pwd
= getpwuid(who
);
6772 (void) printf("user %d ", (int)who
);
6774 (void) printf("(%s) ", pwd
->pw_name
);
6776 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
6777 (void) printf("on %s",
6778 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
6780 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
6781 (void) printf(":%s",
6782 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
6786 (void) printf("\n");
6788 (void) printf("\n");
6795 * zpool history <pool>
6797 * Displays the history of commands that modified pools.
6800 zpool_do_history(int argc
, char **argv
)
6802 hist_cbdata_t cbdata
= { 0 };
6806 cbdata
.first
= B_TRUE
;
6808 while ((c
= getopt(argc
, argv
, "li")) != -1) {
6811 cbdata
.longfmt
= B_TRUE
;
6814 cbdata
.internal
= B_TRUE
;
6817 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6825 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
6828 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
6829 (void) fprintf(stderr
, gettext("no pools available\n"));
6836 typedef struct ev_opts
{
6844 zpool_do_events_short(nvlist_t
*nvl
)
6846 char ctime_str
[26], str
[32], *ptr
;
6850 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
6851 memset(str
, ' ', 32);
6852 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
6853 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
6854 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
6855 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
6856 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
6857 (void) printf(gettext("%s "), str
);
6859 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
6860 (void) printf(gettext("%s\n"), ptr
);
6864 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
6868 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
6869 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
6871 data_type_t type
= nvpair_type(nvp
);
6872 const char *name
= nvpair_name(nvp
);
6882 printf(gettext("%*s%s = "), depth
, "", name
);
6885 case DATA_TYPE_BOOLEAN
:
6886 printf(gettext("%s"), "1");
6889 case DATA_TYPE_BOOLEAN_VALUE
:
6890 (void) nvpair_value_boolean_value(nvp
, &b
);
6891 printf(gettext("%s"), b
? "1" : "0");
6894 case DATA_TYPE_BYTE
:
6895 (void) nvpair_value_byte(nvp
, &i8
);
6896 printf(gettext("0x%x"), i8
);
6899 case DATA_TYPE_INT8
:
6900 (void) nvpair_value_int8(nvp
, (void *)&i8
);
6901 printf(gettext("0x%x"), i8
);
6904 case DATA_TYPE_UINT8
:
6905 (void) nvpair_value_uint8(nvp
, &i8
);
6906 printf(gettext("0x%x"), i8
);
6909 case DATA_TYPE_INT16
:
6910 (void) nvpair_value_int16(nvp
, (void *)&i16
);
6911 printf(gettext("0x%x"), i16
);
6914 case DATA_TYPE_UINT16
:
6915 (void) nvpair_value_uint16(nvp
, &i16
);
6916 printf(gettext("0x%x"), i16
);
6919 case DATA_TYPE_INT32
:
6920 (void) nvpair_value_int32(nvp
, (void *)&i32
);
6921 printf(gettext("0x%x"), i32
);
6924 case DATA_TYPE_UINT32
:
6925 (void) nvpair_value_uint32(nvp
, &i32
);
6926 printf(gettext("0x%x"), i32
);
6929 case DATA_TYPE_INT64
:
6930 (void) nvpair_value_int64(nvp
, (void *)&i64
);
6931 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
6934 case DATA_TYPE_UINT64
:
6935 (void) nvpair_value_uint64(nvp
, &i64
);
6937 * translate vdev state values to readable
6938 * strings to aide zpool events consumers
6941 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
6943 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
6944 printf(gettext("\"%s\" (0x%llx)"),
6945 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
6948 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
6952 case DATA_TYPE_HRTIME
:
6953 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
6954 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
6957 case DATA_TYPE_STRING
:
6958 (void) nvpair_value_string(nvp
, &str
);
6959 printf(gettext("\"%s\""), str
? str
: "<NULL>");
6962 case DATA_TYPE_NVLIST
:
6963 printf(gettext("(embedded nvlist)\n"));
6964 (void) nvpair_value_nvlist(nvp
, &cnv
);
6965 zpool_do_events_nvprint(cnv
, depth
+ 8);
6966 printf(gettext("%*s(end %s)"), depth
, "", name
);
6969 case DATA_TYPE_NVLIST_ARRAY
: {
6973 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
6974 printf(gettext("(%d embedded nvlists)\n"), nelem
);
6975 for (i
= 0; i
< nelem
; i
++) {
6976 printf(gettext("%*s%s[%d] = %s\n"),
6977 depth
, "", name
, i
, "(embedded nvlist)");
6978 zpool_do_events_nvprint(val
[i
], depth
+ 8);
6979 printf(gettext("%*s(end %s[%i])\n"),
6980 depth
, "", name
, i
);
6982 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
6986 case DATA_TYPE_INT8_ARRAY
: {
6990 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
6991 for (i
= 0; i
< nelem
; i
++)
6992 printf(gettext("0x%x "), val
[i
]);
6997 case DATA_TYPE_UINT8_ARRAY
: {
7001 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
7002 for (i
= 0; i
< nelem
; i
++)
7003 printf(gettext("0x%x "), val
[i
]);
7008 case DATA_TYPE_INT16_ARRAY
: {
7012 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
7013 for (i
= 0; i
< nelem
; i
++)
7014 printf(gettext("0x%x "), val
[i
]);
7019 case DATA_TYPE_UINT16_ARRAY
: {
7023 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
7024 for (i
= 0; i
< nelem
; i
++)
7025 printf(gettext("0x%x "), val
[i
]);
7030 case DATA_TYPE_INT32_ARRAY
: {
7034 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
7035 for (i
= 0; i
< nelem
; i
++)
7036 printf(gettext("0x%x "), val
[i
]);
7041 case DATA_TYPE_UINT32_ARRAY
: {
7045 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
7046 for (i
= 0; i
< nelem
; i
++)
7047 printf(gettext("0x%x "), val
[i
]);
7052 case DATA_TYPE_INT64_ARRAY
: {
7056 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
7057 for (i
= 0; i
< nelem
; i
++)
7058 printf(gettext("0x%llx "),
7059 (u_longlong_t
)val
[i
]);
7064 case DATA_TYPE_UINT64_ARRAY
: {
7068 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
7069 for (i
= 0; i
< nelem
; i
++)
7070 printf(gettext("0x%llx "),
7071 (u_longlong_t
)val
[i
]);
7076 case DATA_TYPE_STRING_ARRAY
: {
7080 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
7081 for (i
= 0; i
< nelem
; i
++)
7082 printf(gettext("\"%s\" "),
7083 str
[i
] ? str
[i
] : "<NULL>");
7088 case DATA_TYPE_BOOLEAN_ARRAY
:
7089 case DATA_TYPE_BYTE_ARRAY
:
7090 case DATA_TYPE_DOUBLE
:
7091 case DATA_TYPE_UNKNOWN
:
7092 printf(gettext("<unknown>"));
7096 printf(gettext("\n"));
7101 zpool_do_events_next(ev_opts_t
*opts
)
7104 int zevent_fd
, ret
, dropped
;
7106 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
7107 VERIFY(zevent_fd
>= 0);
7109 if (!opts
->scripted
)
7110 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7113 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
7114 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
7115 if (ret
|| nvl
== NULL
)
7119 (void) printf(gettext("dropped %d events\n"), dropped
);
7121 zpool_do_events_short(nvl
);
7123 if (opts
->verbose
) {
7124 zpool_do_events_nvprint(nvl
, 8);
7125 printf(gettext("\n"));
7127 (void) fflush(stdout
);
7132 VERIFY(0 == close(zevent_fd
));
7138 zpool_do_events_clear(ev_opts_t
*opts
)
7142 ret
= zpool_events_clear(g_zfs
, &count
);
7144 (void) printf(gettext("cleared %d events\n"), count
);
7150 * zpool events [-vfc]
7152 * Displays events logs by ZFS.
7155 zpool_do_events(int argc
, char **argv
)
7157 ev_opts_t opts
= { 0 };
7162 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
7177 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7186 ret
= zpool_do_events_clear(&opts
);
7188 ret
= zpool_do_events_next(&opts
);
7194 get_callback(zpool_handle_t
*zhp
, void *data
)
7196 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
7197 char value
[MAXNAMELEN
];
7198 zprop_source_t srctype
;
7201 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
7204 * Skip the special fake placeholder. This will also skip
7205 * over the name property when 'all' is specified.
7207 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
7208 pl
== cbp
->cb_proplist
)
7211 if (pl
->pl_prop
== ZPROP_INVAL
&&
7212 (zpool_prop_feature(pl
->pl_user_prop
) ||
7213 zpool_prop_unsupported(pl
->pl_user_prop
))) {
7214 srctype
= ZPROP_SRC_LOCAL
;
7216 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
7217 value
, sizeof (value
)) == 0) {
7218 zprop_print_one_property(zpool_get_name(zhp
),
7219 cbp
, pl
->pl_user_prop
, value
, srctype
,
7223 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
7224 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
7227 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
7228 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
7236 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7238 * -H Scripted mode. Don't display headers, and separate properties
7240 * -o List of columns to display. Defaults to
7241 * "name,property,value,source".
7242 * -p Diplay values in parsable (exact) format.
7244 * Get properties of pools in the system. Output space statistics
7245 * for each one as well as other attributes.
7248 zpool_do_get(int argc
, char **argv
)
7250 zprop_get_cbdata_t cb
= { 0 };
7251 zprop_list_t fake_name
= { 0 };
7256 cb
.cb_first
= B_TRUE
;
7259 * Set up default columns and sources.
7261 cb
.cb_sources
= ZPROP_SRC_ALL
;
7262 cb
.cb_columns
[0] = GET_COL_NAME
;
7263 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7264 cb
.cb_columns
[2] = GET_COL_VALUE
;
7265 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7266 cb
.cb_type
= ZFS_TYPE_POOL
;
7269 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
7272 cb
.cb_literal
= B_TRUE
;
7275 cb
.cb_scripted
= B_TRUE
;
7278 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
7280 while (*optarg
!= '\0') {
7281 static char *col_subopts
[] =
7282 { "name", "property", "value", "source",
7285 if (i
== ZFS_GET_NCOLS
) {
7286 (void) fprintf(stderr
, gettext("too "
7287 "many fields given to -o "
7292 switch (getsubopt(&optarg
, col_subopts
,
7295 cb
.cb_columns
[i
++] = GET_COL_NAME
;
7298 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
7301 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
7304 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
7308 (void) fprintf(stderr
,
7309 gettext("\"all\" conflicts "
7310 "with specific fields "
7311 "given to -o option\n"));
7314 cb
.cb_columns
[0] = GET_COL_NAME
;
7315 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7316 cb
.cb_columns
[2] = GET_COL_VALUE
;
7317 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7321 (void) fprintf(stderr
,
7322 gettext("invalid column name "
7329 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7339 (void) fprintf(stderr
, gettext("missing property "
7344 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
7345 ZFS_TYPE_POOL
) != 0)
7351 if (cb
.cb_proplist
!= NULL
) {
7352 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
7353 fake_name
.pl_width
= strlen(gettext("NAME"));
7354 fake_name
.pl_next
= cb
.cb_proplist
;
7355 cb
.cb_proplist
= &fake_name
;
7358 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
7361 if (cb
.cb_proplist
== &fake_name
)
7362 zprop_free_list(fake_name
.pl_next
);
7364 zprop_free_list(cb
.cb_proplist
);
7369 typedef struct set_cbdata
{
7372 boolean_t cb_any_successful
;
7376 set_callback(zpool_handle_t
*zhp
, void *data
)
7379 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
7381 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
7384 cb
->cb_any_successful
= B_TRUE
;
7390 zpool_do_set(int argc
, char **argv
)
7392 set_cbdata_t cb
= { 0 };
7395 if (argc
> 1 && argv
[1][0] == '-') {
7396 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7402 (void) fprintf(stderr
, gettext("missing property=value "
7408 (void) fprintf(stderr
, gettext("missing pool name\n"));
7413 (void) fprintf(stderr
, gettext("too many pool names\n"));
7417 cb
.cb_propname
= argv
[1];
7418 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
7419 if (cb
.cb_value
== NULL
) {
7420 (void) fprintf(stderr
, gettext("missing value in "
7421 "property=value argument\n"));
7425 *(cb
.cb_value
) = '\0';
7428 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
7435 find_command_idx(char *command
, int *idx
)
7439 for (i
= 0; i
< NCOMMAND
; i
++) {
7440 if (command_table
[i
].name
== NULL
)
7443 if (strcmp(command
, command_table
[i
].name
) == 0) {
7452 main(int argc
, char **argv
)
7458 (void) setlocale(LC_ALL
, "");
7459 (void) textdomain(TEXT_DOMAIN
);
7462 dprintf_setup(&argc
, argv
);
7467 * Make sure the user has specified some command.
7470 (void) fprintf(stderr
, gettext("missing command\n"));
7479 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
7482 if ((g_zfs
= libzfs_init()) == NULL
) {
7483 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7487 libzfs_print_on_error(g_zfs
, B_TRUE
);
7489 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7492 * Run the appropriate command.
7494 if (find_command_idx(cmdname
, &i
) == 0) {
7495 current_command
= &command_table
[i
];
7496 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7497 } else if (strchr(cmdname
, '=')) {
7498 verify(find_command_idx("set", &i
) == 0);
7499 current_command
= &command_table
[i
];
7500 ret
= command_table
[i
].func(argc
, argv
);
7501 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
7503 * 'freeze' is a vile debugging abomination, so we treat
7507 int fd
= open(ZFS_DEV
, O_RDWR
);
7508 (void) strlcpy((void *)buf
, argv
[2], sizeof (buf
));
7509 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
7511 (void) fprintf(stderr
, gettext("unrecognized "
7512 "command '%s'\n"), cmdname
);
7517 if (ret
== 0 && log_history
)
7518 (void) zpool_log_history(g_zfs
, history_str
);
7523 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7524 * for the purposes of running ::findleaks.
7526 if (getenv("ZFS_ABORT") != NULL
) {
7527 (void) printf("dumping core by request\n");