4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
52 #include <sys/fs/zfs.h>
54 #include <sys/systeminfo.h>
55 #include <sys/fm/fs/zfs.h>
56 #include <sys/fm/util.h>
57 #include <sys/fm/protocol.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/mount.h>
60 #include <sys/sysmacros.h>
66 #include "zpool_util.h"
67 #include "zfs_comutil.h"
68 #include "zfeature_common.h"
70 #include "statcommon.h"
72 static int zpool_do_create(int, char **);
73 static int zpool_do_destroy(int, char **);
75 static int zpool_do_add(int, char **);
76 static int zpool_do_remove(int, char **);
77 static int zpool_do_labelclear(int, char **);
79 static int zpool_do_list(int, char **);
80 static int zpool_do_iostat(int, char **);
81 static int zpool_do_status(int, char **);
83 static int zpool_do_online(int, char **);
84 static int zpool_do_offline(int, char **);
85 static int zpool_do_clear(int, char **);
86 static int zpool_do_reopen(int, char **);
88 static int zpool_do_reguid(int, char **);
90 static int zpool_do_attach(int, char **);
91 static int zpool_do_detach(int, char **);
92 static int zpool_do_replace(int, char **);
93 static int zpool_do_split(int, char **);
95 static int zpool_do_scrub(int, char **);
97 static int zpool_do_import(int, char **);
98 static int zpool_do_export(int, char **);
100 static int zpool_do_upgrade(int, char **);
102 static int zpool_do_history(int, char **);
103 static int zpool_do_events(int, char **);
105 static int zpool_do_get(int, char **);
106 static int zpool_do_set(int, char **);
108 static int zpool_do_sync(int, char **);
111 * These libumem hooks provide a reasonable set of defaults for the allocator's
112 * debugging facilities.
117 _umem_debug_init(void)
119 return ("default,verbose"); /* $UMEM_DEBUG setting */
123 _umem_logging_init(void)
125 return ("fail,contents"); /* $UMEM_LOGGING setting */
160 * Flags for stats to display with "zpool iostats"
168 IOS_COUNT
, /* always last element */
171 /* iostat_type entries as bitmasks */
172 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
173 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
174 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
175 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
176 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
178 /* Mask of all the histo bits */
179 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
182 * Lookup table for iostat flags to nvlist names. Basically a list
183 * of all the nvlists a flag requires. Also specifies the order in
184 * which data gets printed in zpool iostat.
186 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
188 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
189 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
190 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
191 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
192 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
193 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
194 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
195 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
196 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
199 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
200 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
201 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
202 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
205 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
206 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
207 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
208 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
209 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
212 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
213 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
214 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
215 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
216 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
217 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
218 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
219 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
220 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
221 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
227 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
228 * Right now, only one histo bit is ever set at one time, so we can
229 * just do a highbit64(a)
231 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
233 typedef struct zpool_command
{
235 int (*func
)(int, char **);
240 * Master command table. Each ZFS command has a name, associated function, and
241 * usage message. The usage messages need to be internationalized, so we have
242 * to have a function to return the usage message based on a command index.
244 * These commands are organized according to how they are displayed in the usage
245 * message. An empty command (one with a NULL name) indicates an empty line in
246 * the generic usage message.
248 static zpool_command_t command_table
[] = {
249 { "create", zpool_do_create
, HELP_CREATE
},
250 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
252 { "add", zpool_do_add
, HELP_ADD
},
253 { "remove", zpool_do_remove
, HELP_REMOVE
},
255 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
257 { "list", zpool_do_list
, HELP_LIST
},
258 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
259 { "status", zpool_do_status
, HELP_STATUS
},
261 { "online", zpool_do_online
, HELP_ONLINE
},
262 { "offline", zpool_do_offline
, HELP_OFFLINE
},
263 { "clear", zpool_do_clear
, HELP_CLEAR
},
264 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
266 { "attach", zpool_do_attach
, HELP_ATTACH
},
267 { "detach", zpool_do_detach
, HELP_DETACH
},
268 { "replace", zpool_do_replace
, HELP_REPLACE
},
269 { "split", zpool_do_split
, HELP_SPLIT
},
271 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
273 { "import", zpool_do_import
, HELP_IMPORT
},
274 { "export", zpool_do_export
, HELP_EXPORT
},
275 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
276 { "reguid", zpool_do_reguid
, HELP_REGUID
},
278 { "history", zpool_do_history
, HELP_HISTORY
},
279 { "events", zpool_do_events
, HELP_EVENTS
},
281 { "get", zpool_do_get
, HELP_GET
},
282 { "set", zpool_do_set
, HELP_SET
},
283 { "sync", zpool_do_sync
, HELP_SYNC
},
286 #define NCOMMAND (ARRAY_SIZE(command_table))
288 static zpool_command_t
*current_command
;
289 static char history_str
[HIS_MAX_RECORD_LEN
];
290 static boolean_t log_history
= B_TRUE
;
291 static uint_t timestamp_fmt
= NODATE
;
294 get_usage(zpool_help_t idx
)
298 return (gettext("\tadd [-fgLnP] [-o property=value] "
299 "<pool> <vdev> ...\n"));
301 return (gettext("\tattach [-f] [-o property=value] "
302 "<pool> <device> <new-device>\n"));
304 return (gettext("\tclear [-nF] <pool> [device]\n"));
306 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
307 "\t [-O file-system-property=value] ... \n"
308 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
310 return (gettext("\tdestroy [-f] <pool>\n"));
312 return (gettext("\tdetach <pool> <device>\n"));
314 return (gettext("\texport [-af] <pool> ...\n"));
316 return (gettext("\thistory [-il] [<pool>] ...\n"));
318 return (gettext("\timport [-d dir] [-D]\n"
319 "\timport [-d dir | -c cachefile] [-F [-n]] [-l] "
321 "\timport [-o mntopts] [-o property=value] ... \n"
322 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
323 "[-R root] [-F [-n]] -a\n"
324 "\timport [-o mntopts] [-o property=value] ... \n"
325 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
326 "[-R root] [-F [-n]]\n"
327 "\t <pool | id> [newpool]\n"));
329 return (gettext("\tiostat [[[-c [script1,script2,...]"
330 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
331 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
332 " [interval [count]]\n"));
333 case HELP_LABELCLEAR
:
334 return (gettext("\tlabelclear [-f] <vdev>\n"));
336 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
337 "[-T d|u] [pool] ... \n"
338 "\t [interval [count]]\n"));
340 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
342 return (gettext("\tonline <pool> <device> ...\n"));
344 return (gettext("\treplace [-f] [-o property=value] "
345 "<pool> <device> [new-device]\n"));
347 return (gettext("\tremove [-nps] <pool> <device> ...\n"));
349 return (gettext("\treopen [-n] <pool>\n"));
351 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
353 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
354 "[-T d|u] [pool] ... \n"
355 "\t [interval [count]]\n"));
357 return (gettext("\tupgrade\n"
359 "\tupgrade [-V version] <-a | pool ...>\n"));
361 return (gettext("\tevents [-vHf [pool] | -c]\n"));
363 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
364 "<\"all\" | property[,...]> <pool> ...\n"));
366 return (gettext("\tset <property=value> <pool> \n"));
368 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
369 "\t [-o property=value] <pool> <newpool> "
370 "[<device> ...]\n"));
372 return (gettext("\treguid <pool>\n"));
374 return (gettext("\tsync [pool] ...\n"));
383 * Callback routine that will print out a pool property value.
386 print_prop_cb(int prop
, void *cb
)
390 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
392 if (zpool_prop_readonly(prop
))
393 (void) fprintf(fp
, " NO ");
395 (void) fprintf(fp
, " YES ");
397 if (zpool_prop_values(prop
) == NULL
)
398 (void) fprintf(fp
, "-\n");
400 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
406 * Display usage message. If we're inside a command, display only the usage for
407 * that command. Otherwise, iterate over the entire command table and display
408 * a complete usage message.
411 usage(boolean_t requested
)
413 FILE *fp
= requested
? stdout
: stderr
;
415 if (current_command
== NULL
) {
418 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
420 gettext("where 'command' is one of the following:\n\n"));
422 for (i
= 0; i
< NCOMMAND
; i
++) {
423 if (command_table
[i
].name
== NULL
)
424 (void) fprintf(fp
, "\n");
426 (void) fprintf(fp
, "%s",
427 get_usage(command_table
[i
].usage
));
430 (void) fprintf(fp
, gettext("usage:\n"));
431 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
434 if (current_command
!= NULL
&&
435 ((strcmp(current_command
->name
, "set") == 0) ||
436 (strcmp(current_command
->name
, "get") == 0) ||
437 (strcmp(current_command
->name
, "list") == 0))) {
440 gettext("\nthe following properties are supported:\n"));
442 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
443 "PROPERTY", "EDIT", "VALUES");
445 /* Iterate over all properties */
446 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
449 (void) fprintf(fp
, "\t%-15s ", "feature@...");
450 (void) fprintf(fp
, "YES disabled | enabled | active\n");
452 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
453 "appended with a feature name.\nSee zpool-features(5).\n"));
457 * See comments at end of main().
459 if (getenv("ZFS_ABORT") != NULL
) {
460 (void) printf("dumping core by request\n");
464 exit(requested
? 0 : 2);
468 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
469 boolean_t print_logs
, int name_flags
)
476 (void) printf("\t%*s%s\n", indent
, "", name
);
478 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
479 &child
, &children
) != 0)
482 for (c
= 0; c
< children
; c
++) {
483 uint64_t is_log
= B_FALSE
;
485 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
487 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
490 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
491 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
492 B_FALSE
, name_flags
);
498 prop_list_contains_feature(nvlist_t
*proplist
)
501 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
502 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
503 if (zpool_prop_feature(nvpair_name(nvp
)))
510 * Add a property pair (name, string-value) into a property nvlist.
513 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
516 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
522 if (*props
== NULL
&&
523 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
524 (void) fprintf(stderr
,
525 gettext("internal error: out of memory\n"));
532 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
534 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
535 !zpool_prop_feature(propname
)) {
536 (void) fprintf(stderr
, gettext("property '%s' is "
537 "not a valid pool property\n"), propname
);
542 * feature@ properties and version should not be specified
545 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
546 nvlist_exists(proplist
, vname
)) ||
547 (prop
== ZPOOL_PROP_VERSION
&&
548 prop_list_contains_feature(proplist
))) {
549 (void) fprintf(stderr
, gettext("'feature@' and "
550 "'version' properties cannot be specified "
556 if (zpool_prop_feature(propname
))
559 normnm
= zpool_prop_to_name(prop
);
561 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
562 normnm
= zfs_prop_to_name(fprop
);
568 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
569 prop
!= ZPOOL_PROP_CACHEFILE
) {
570 (void) fprintf(stderr
, gettext("property '%s' "
571 "specified multiple times\n"), propname
);
575 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
576 (void) fprintf(stderr
, gettext("internal "
577 "error: out of memory\n"));
585 * Set a default property pair (name, string-value) in a property nvlist
588 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
593 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
596 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
600 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
602 * -f Force addition of devices, even if they appear in use
603 * -g Display guid for individual vdev name.
604 * -L Follow links when resolving vdev path name.
605 * -n Do not add the devices, but display the resulting layout if
606 * they were to be added.
607 * -o Set property=value.
608 * -P Display full path for vdev name.
610 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
611 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
615 zpool_do_add(int argc
, char **argv
)
617 boolean_t force
= B_FALSE
;
618 boolean_t dryrun
= B_FALSE
;
626 nvlist_t
*props
= NULL
;
630 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
636 name_flags
|= VDEV_NAME_GUID
;
639 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
645 if ((propval
= strchr(optarg
, '=')) == NULL
) {
646 (void) fprintf(stderr
, gettext("missing "
647 "'=' for -o option\n"));
653 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
654 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
658 name_flags
|= VDEV_NAME_PATH
;
661 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
670 /* get pool name and check number of arguments */
672 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
676 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
685 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
688 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
689 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
695 /* unless manually specified use "ashift" pool property (if set) */
696 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
699 char strval
[ZPOOL_MAXPROPLEN
];
701 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
702 if (src
!= ZPROP_SRC_DEFAULT
) {
703 (void) sprintf(strval
, "%" PRId32
, intval
);
704 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
705 &props
, B_TRUE
) == 0);
709 /* pass off to get_vdev_spec for processing */
710 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
712 if (nvroot
== NULL
) {
718 nvlist_t
*poolnvroot
;
720 uint_t l2children
, c
;
722 boolean_t hadcache
= B_FALSE
;
724 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
727 (void) printf(gettext("would update '%s' to the following "
728 "configuration:\n"), zpool_get_name(zhp
));
730 /* print original main pool and new tree */
731 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
733 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
735 /* Do the same for the logs */
736 if (num_logs(poolnvroot
) > 0) {
737 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
739 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
741 } else if (num_logs(nvroot
) > 0) {
742 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
746 /* Do the same for the caches */
747 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
748 &l2child
, &l2children
) == 0 && l2children
) {
750 (void) printf(gettext("\tcache\n"));
751 for (c
= 0; c
< l2children
; c
++) {
752 vname
= zpool_vdev_name(g_zfs
, NULL
,
753 l2child
[c
], name_flags
);
754 (void) printf("\t %s\n", vname
);
758 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
759 &l2child
, &l2children
) == 0 && l2children
) {
761 (void) printf(gettext("\tcache\n"));
762 for (c
= 0; c
< l2children
; c
++) {
763 vname
= zpool_vdev_name(g_zfs
, NULL
,
764 l2child
[c
], name_flags
);
765 (void) printf("\t %s\n", vname
);
772 ret
= (zpool_add(zhp
, nvroot
) != 0);
783 * zpool remove <pool> <vdev> ...
785 * Removes the given vdev from the pool.
788 zpool_do_remove(int argc
, char **argv
)
792 zpool_handle_t
*zhp
= NULL
;
793 boolean_t stop
= B_FALSE
;
795 boolean_t noop
= B_FALSE
;
796 boolean_t parsable
= B_FALSE
;
799 while ((c
= getopt(argc
, argv
, "nps")) != -1) {
811 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
820 /* get pool name and check number of arguments */
822 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
828 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
832 (void) fprintf(stderr
, gettext("stop request ignored\n"));
838 (void) fprintf(stderr
, gettext("too many arguments\n"));
841 if (zpool_vdev_remove_cancel(zhp
) != 0)
845 (void) fprintf(stderr
, gettext("missing device\n"));
849 for (i
= 1; i
< argc
; i
++) {
853 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
859 (void) printf("%s %llu\n",
860 argv
[i
], (unsigned long long)size
);
863 zfs_nicenum(size
, valstr
,
865 (void) printf("Memory that will be "
866 "used after removing %s: %s\n",
870 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
881 * zpool labelclear [-f] <vdev>
883 * -f Force clearing the label for the vdevs which are members of
884 * the exported or foreign pools.
886 * Verifies that the vdev is not active and zeros out the label information
890 zpool_do_labelclear(int argc
, char **argv
)
892 char vdev
[MAXPATHLEN
];
895 int c
, fd
= -1, ret
= 0;
898 boolean_t inuse
= B_FALSE
;
899 boolean_t force
= B_FALSE
;
902 while ((c
= getopt(argc
, argv
, "f")) != -1) {
908 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
919 (void) fprintf(stderr
, gettext("missing vdev name\n"));
923 (void) fprintf(stderr
, gettext("too many arguments\n"));
928 * Check if we were given absolute path and use it as is.
929 * Otherwise if the provided vdev name doesn't point to a file,
930 * try prepending expected disk paths and partition numbers.
932 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
933 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
936 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
937 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
938 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
942 if (error
|| (stat(vdev
, &st
) != 0)) {
943 (void) fprintf(stderr
, gettext(
944 "failed to find device %s, try specifying absolute "
945 "path instead\n"), argv
[0]);
950 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
951 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
952 vdev
, strerror(errno
));
956 if (ioctl(fd
, BLKFLSBUF
) != 0)
957 (void) fprintf(stderr
, gettext("failed to invalidate "
958 "cache for %s: %s\n"), vdev
, strerror(errno
));
960 if (zpool_read_label(fd
, &config
, NULL
) != 0 || config
== NULL
) {
961 (void) fprintf(stderr
,
962 gettext("failed to check state for %s\n"), vdev
);
968 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
970 (void) fprintf(stderr
,
971 gettext("failed to check state for %s\n"), vdev
);
981 case POOL_STATE_ACTIVE
:
982 case POOL_STATE_SPARE
:
983 case POOL_STATE_L2CACHE
:
984 (void) fprintf(stderr
, gettext(
985 "%s is a member (%s) of pool \"%s\"\n"),
986 vdev
, zpool_pool_state_to_name(state
), name
);
990 case POOL_STATE_EXPORTED
:
993 (void) fprintf(stderr
, gettext(
994 "use '-f' to override the following error:\n"
995 "%s is a member of exported pool \"%s\"\n"),
1000 case POOL_STATE_POTENTIALLY_ACTIVE
:
1003 (void) fprintf(stderr
, gettext(
1004 "use '-f' to override the following error:\n"
1005 "%s is a member of potentially active pool \"%s\"\n"),
1010 case POOL_STATE_DESTROYED
:
1011 /* inuse should never be set for a destroyed pool */
1017 ret
= zpool_clear_label(fd
);
1019 (void) fprintf(stderr
,
1020 gettext("failed to clear label for %s\n"), vdev
);
1031 * zpool create [-fnd] [-o property=value] ...
1032 * [-O file-system-property=value] ...
1033 * [-R root] [-m mountpoint] <pool> <dev> ...
1035 * -f Force creation, even if devices appear in use
1036 * -n Do not create the pool, but display the resulting layout if it
1037 * were to be created.
1038 * -R Create a pool under an alternate root
1039 * -m Set default mountpoint for the root dataset. By default it's
1041 * -o Set property=value.
1042 * -o Set feature@feature=enabled|disabled.
1043 * -d Don't automatically enable all supported pool features
1044 * (individual features can be enabled with -o).
1045 * -O Set fsproperty=value in the pool's root file system
1047 * Creates the named pool according to the given vdev specification. The
1048 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
1049 * we get the nvlist back from get_vdev_spec(), we either print out the contents
1050 * (if '-n' was specified), or pass it to libzfs to do the creation.
1053 zpool_do_create(int argc
, char **argv
)
1055 boolean_t force
= B_FALSE
;
1056 boolean_t dryrun
= B_FALSE
;
1057 boolean_t enable_all_pool_feat
= B_TRUE
;
1059 nvlist_t
*nvroot
= NULL
;
1063 char *altroot
= NULL
;
1064 char *mountpoint
= NULL
;
1065 nvlist_t
*fsprops
= NULL
;
1066 nvlist_t
*props
= NULL
;
1070 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1079 enable_all_pool_feat
= B_FALSE
;
1083 if (add_prop_list(zpool_prop_to_name(
1084 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1086 if (add_prop_list_default(zpool_prop_to_name(
1087 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1091 /* Equivalent to -O mountpoint=optarg */
1092 mountpoint
= optarg
;
1095 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1096 (void) fprintf(stderr
, gettext("missing "
1097 "'=' for -o option\n"));
1103 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1107 * If the user is creating a pool that doesn't support
1108 * feature flags, don't enable any features.
1110 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1114 ver
= strtoull(propval
, &end
, 10);
1116 ver
< SPA_VERSION_FEATURES
) {
1117 enable_all_pool_feat
= B_FALSE
;
1120 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1124 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1125 (void) fprintf(stderr
, gettext("missing "
1126 "'=' for -O option\n"));
1133 * Mountpoints are checked and then added later.
1134 * Uniquely among properties, they can be specified
1135 * more than once, to avoid conflict with -m.
1137 if (0 == strcmp(optarg
,
1138 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1139 mountpoint
= propval
;
1140 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1147 * Sanity check temporary pool name.
1149 if (strchr(optarg
, '/') != NULL
) {
1150 (void) fprintf(stderr
, gettext("cannot create "
1151 "'%s': invalid character '/' in temporary "
1153 (void) fprintf(stderr
, gettext("use 'zfs "
1154 "create' to create a dataset\n"));
1158 if (add_prop_list(zpool_prop_to_name(
1159 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1161 if (add_prop_list_default(zpool_prop_to_name(
1162 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1167 (void) fprintf(stderr
, gettext("missing argument for "
1168 "'%c' option\n"), optopt
);
1171 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1180 /* get pool name and check number of arguments */
1182 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1186 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1193 * As a special case, check for use of '/' in the name, and direct the
1194 * user to use 'zfs create' instead.
1196 if (strchr(poolname
, '/') != NULL
) {
1197 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1198 "character '/' in pool name\n"), poolname
);
1199 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1200 "create a dataset\n"));
1204 /* pass off to get_vdev_spec for bulk processing */
1205 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1206 argc
- 1, argv
+ 1);
1210 /* make_root_vdev() allows 0 toplevel children if there are spares */
1211 if (!zfs_allocatable_devs(nvroot
)) {
1212 (void) fprintf(stderr
, gettext("invalid vdev "
1213 "specification: at least one toplevel vdev must be "
1218 if (altroot
!= NULL
&& altroot
[0] != '/') {
1219 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1220 "must be an absolute path\n"), altroot
);
1225 * Check the validity of the mountpoint and direct the user to use the
1226 * '-m' mountpoint option if it looks like its in use.
1228 if (mountpoint
== NULL
||
1229 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1230 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1231 char buf
[MAXPATHLEN
];
1234 if (mountpoint
&& mountpoint
[0] != '/') {
1235 (void) fprintf(stderr
, gettext("invalid mountpoint "
1236 "'%s': must be an absolute path, 'legacy', or "
1237 "'none'\n"), mountpoint
);
1241 if (mountpoint
== NULL
) {
1242 if (altroot
!= NULL
)
1243 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1246 (void) snprintf(buf
, sizeof (buf
), "/%s",
1249 if (altroot
!= NULL
)
1250 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1251 altroot
, mountpoint
);
1253 (void) snprintf(buf
, sizeof (buf
), "%s",
1257 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1258 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1259 "%s\n"), buf
, strerror(errno
));
1260 (void) fprintf(stderr
, gettext("use '-m' "
1261 "option to provide a different default\n"));
1266 while (count
< 3 && readdir(dirp
) != NULL
)
1268 (void) closedir(dirp
);
1271 (void) fprintf(stderr
, gettext("mountpoint "
1272 "'%s' exists and is not empty\n"), buf
);
1273 (void) fprintf(stderr
, gettext("use '-m' "
1274 "option to provide a "
1275 "different default\n"));
1282 * Now that the mountpoint's validity has been checked, ensure that
1283 * the property is set appropriately prior to creating the pool.
1285 if (mountpoint
!= NULL
) {
1286 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1287 mountpoint
, &fsprops
, B_FALSE
);
1295 * For a dry run invocation, print out a basic message and run
1296 * through all the vdevs in the list and print out in an
1297 * appropriate hierarchy.
1299 (void) printf(gettext("would create '%s' with the "
1300 "following layout:\n\n"), poolname
);
1302 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1303 if (num_logs(nvroot
) > 0)
1304 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1309 * Hand off to libzfs.
1312 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1313 char propname
[MAXPATHLEN
];
1315 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1317 (void) snprintf(propname
, sizeof (propname
),
1318 "feature@%s", feat
->fi_uname
);
1321 * Only features contained in props will be enabled:
1322 * remove from the nvlist every ZFS_FEATURE_DISABLED
1323 * value and add every missing ZFS_FEATURE_ENABLED if
1324 * enable_all_pool_feat is set.
1326 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1327 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1328 (void) nvlist_remove_all(props
,
1330 } else if (enable_all_pool_feat
) {
1331 ret
= add_prop_list(propname
,
1332 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1339 if (zpool_create(g_zfs
, poolname
,
1340 nvroot
, props
, fsprops
) == 0) {
1341 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1342 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1344 if (zfs_mount(pool
, NULL
, 0) == 0)
1345 ret
= zfs_shareall(pool
);
1348 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1349 (void) fprintf(stderr
, gettext("pool name may have "
1355 nvlist_free(nvroot
);
1356 nvlist_free(fsprops
);
1360 nvlist_free(fsprops
);
1367 * zpool destroy <pool>
1369 * -f Forcefully unmount any datasets
1371 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1374 zpool_do_destroy(int argc
, char **argv
)
1376 boolean_t force
= B_FALSE
;
1379 zpool_handle_t
*zhp
;
1383 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1389 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1398 /* check arguments */
1400 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1404 (void) fprintf(stderr
, gettext("too many arguments\n"));
1410 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1412 * As a special case, check for use of '/' in the name, and
1413 * direct the user to use 'zfs destroy' instead.
1415 if (strchr(pool
, '/') != NULL
)
1416 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1417 "destroy a dataset\n"));
1421 if (zpool_disable_datasets(zhp
, force
) != 0) {
1422 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1423 "could not unmount datasets\n"), zpool_get_name(zhp
));
1428 /* The history must be logged as part of the export */
1429 log_history
= B_FALSE
;
1431 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1438 typedef struct export_cbdata
{
1440 boolean_t hardforce
;
1447 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1449 export_cbdata_t
*cb
= data
;
1451 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1454 /* The history must be logged as part of the export */
1455 log_history
= B_FALSE
;
1457 if (cb
->hardforce
) {
1458 if (zpool_export_force(zhp
, history_str
) != 0)
1460 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1468 * zpool export [-f] <pool> ...
1470 * -a Export all pools
1471 * -f Forcefully unmount datasets
1473 * Export the given pools. By default, the command will attempt to cleanly
1474 * unmount any active datasets within the pool. If the '-f' flag is specified,
1475 * then the datasets will be forcefully unmounted.
1478 zpool_do_export(int argc
, char **argv
)
1481 boolean_t do_all
= B_FALSE
;
1482 boolean_t force
= B_FALSE
;
1483 boolean_t hardforce
= B_FALSE
;
1487 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1499 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1506 cb
.hardforce
= hardforce
;
1512 (void) fprintf(stderr
, gettext("too many arguments\n"));
1516 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1517 zpool_export_one
, &cb
));
1520 /* check arguments */
1522 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1526 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1532 * Given a vdev configuration, determine the maximum width needed for the device
1536 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1544 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1545 if (strlen(name
) + depth
> max
)
1546 max
= strlen(name
) + depth
;
1550 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1551 &child
, &children
) == 0) {
1552 for (c
= 0; c
< children
; c
++)
1553 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1554 max
, name_flags
)) > max
)
1558 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1559 &child
, &children
) == 0) {
1560 for (c
= 0; c
< children
; c
++)
1561 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1562 max
, name_flags
)) > max
)
1566 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1567 &child
, &children
) == 0) {
1568 for (c
= 0; c
< children
; c
++)
1569 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1570 max
, name_flags
)) > max
)
1577 typedef struct spare_cbdata
{
1579 zpool_handle_t
*cb_zhp
;
1583 find_vdev(nvlist_t
*nv
, uint64_t search
)
1589 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1593 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1594 &child
, &children
) == 0) {
1595 for (c
= 0; c
< children
; c
++)
1596 if (find_vdev(child
[c
], search
))
1604 find_spare(zpool_handle_t
*zhp
, void *data
)
1606 spare_cbdata_t
*cbp
= data
;
1607 nvlist_t
*config
, *nvroot
;
1609 config
= zpool_get_config(zhp
, NULL
);
1610 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1613 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1622 typedef struct status_cbdata
{
1626 boolean_t cb_allpools
;
1627 boolean_t cb_verbose
;
1628 boolean_t cb_explain
;
1630 boolean_t cb_dedup_stats
;
1631 boolean_t cb_print_status
;
1632 vdev_cmd_data_list_t
*vcdl
;
1635 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1637 is_blank_str(char *str
)
1639 while (str
!= NULL
&& *str
!= '\0') {
1647 /* Print command output lines for specific vdev in a specific pool */
1649 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1651 vdev_cmd_data_t
*data
;
1655 for (i
= 0; i
< vcdl
->count
; i
++) {
1656 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1657 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1658 /* Not the vdev we're looking for */
1662 data
= &vcdl
->data
[i
];
1663 /* Print out all the output values for this vdev */
1664 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1666 /* Does this vdev have values for this column? */
1667 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1668 if (strcmp(data
->cols
[k
],
1669 vcdl
->uniq_cols
[j
]) == 0) {
1670 /* yes it does, record the value */
1671 val
= data
->lines
[k
];
1676 * Mark empty values with dashes to make output
1679 if (is_blank_str(val
))
1682 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1683 if (j
< vcdl
->uniq_cols_cnt
- 1)
1687 /* Print out any values that aren't in a column at the end */
1688 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1689 /* Did we have any columns? If so print a spacer. */
1690 if (vcdl
->uniq_cols_cnt
> 0)
1693 val
= data
->lines
[j
];
1694 printf("%s", val
? val
: "");
1701 * Print out configuration state as requested by status_callback.
1704 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1705 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1709 pool_scan_stat_t
*ps
= NULL
;
1711 char rbuf
[6], wbuf
[6], cbuf
[6];
1713 uint64_t notpresent
;
1714 spare_cbdata_t spare_cb
;
1719 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1720 &child
, &children
) != 0)
1723 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1724 (uint64_t **)&vs
, &c
) == 0);
1726 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1728 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
1731 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1734 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1737 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1739 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1743 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1747 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1748 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1749 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1750 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1753 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1754 ¬present
) == 0) {
1755 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1756 (void) printf(" was %s", path
);
1757 } else if (vs
->vs_aux
!= 0) {
1760 switch (vs
->vs_aux
) {
1761 case VDEV_AUX_OPEN_FAILED
:
1762 (void) printf(gettext("cannot open"));
1765 case VDEV_AUX_BAD_GUID_SUM
:
1766 (void) printf(gettext("missing device"));
1769 case VDEV_AUX_NO_REPLICAS
:
1770 (void) printf(gettext("insufficient replicas"));
1773 case VDEV_AUX_VERSION_NEWER
:
1774 (void) printf(gettext("newer version"));
1777 case VDEV_AUX_UNSUP_FEAT
:
1778 (void) printf(gettext("unsupported feature(s)"));
1781 case VDEV_AUX_SPARED
:
1782 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1783 &spare_cb
.cb_guid
) == 0);
1784 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1785 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1786 zpool_get_name(zhp
)) == 0)
1787 (void) printf(gettext("currently in "
1790 (void) printf(gettext("in use by "
1792 zpool_get_name(spare_cb
.cb_zhp
));
1793 zpool_close(spare_cb
.cb_zhp
);
1795 (void) printf(gettext("currently in use"));
1799 case VDEV_AUX_ERR_EXCEEDED
:
1800 (void) printf(gettext("too many errors"));
1803 case VDEV_AUX_IO_FAILURE
:
1804 (void) printf(gettext("experienced I/O failures"));
1807 case VDEV_AUX_BAD_LOG
:
1808 (void) printf(gettext("bad intent log"));
1811 case VDEV_AUX_EXTERNAL
:
1812 (void) printf(gettext("external device fault"));
1815 case VDEV_AUX_SPLIT_POOL
:
1816 (void) printf(gettext("split into new pool"));
1819 case VDEV_AUX_ACTIVE
:
1820 (void) printf(gettext("currently in use"));
1823 case VDEV_AUX_CHILDREN_OFFLINE
:
1824 (void) printf(gettext("all children offline"));
1828 (void) printf(gettext("corrupted data"));
1833 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1834 (uint64_t **)&ps
, &c
);
1836 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&&
1837 vs
->vs_scan_processed
!= 0 && children
== 0) {
1838 (void) printf(gettext(" (%s)"),
1839 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1840 "resilvering" : "repairing");
1843 if (cb
->vcdl
!= NULL
) {
1844 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1846 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1850 (void) printf("\n");
1852 for (c
= 0; c
< children
; c
++) {
1853 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1855 /* Don't print logs or holes here */
1856 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1858 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1860 if (islog
|| ishole
)
1862 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1863 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1864 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1871 * Print the configuration of an exported pool. Iterate over all vdevs in the
1872 * pool, printing out the name and status for each one.
1875 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1883 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1884 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1885 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1888 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1889 (uint64_t **)&vs
, &c
) == 0);
1891 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1892 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1894 if (vs
->vs_aux
!= 0) {
1897 switch (vs
->vs_aux
) {
1898 case VDEV_AUX_OPEN_FAILED
:
1899 (void) printf(gettext("cannot open"));
1902 case VDEV_AUX_BAD_GUID_SUM
:
1903 (void) printf(gettext("missing device"));
1906 case VDEV_AUX_NO_REPLICAS
:
1907 (void) printf(gettext("insufficient replicas"));
1910 case VDEV_AUX_VERSION_NEWER
:
1911 (void) printf(gettext("newer version"));
1914 case VDEV_AUX_UNSUP_FEAT
:
1915 (void) printf(gettext("unsupported feature(s)"));
1918 case VDEV_AUX_ERR_EXCEEDED
:
1919 (void) printf(gettext("too many errors"));
1922 case VDEV_AUX_ACTIVE
:
1923 (void) printf(gettext("currently in use"));
1926 case VDEV_AUX_CHILDREN_OFFLINE
:
1927 (void) printf(gettext("all children offline"));
1931 (void) printf(gettext("corrupted data"));
1935 (void) printf("\n");
1937 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1938 &child
, &children
) != 0)
1941 for (c
= 0; c
< children
; c
++) {
1942 uint64_t is_log
= B_FALSE
;
1944 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1949 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1950 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1951 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1955 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1956 &child
, &children
) == 0) {
1957 (void) printf(gettext("\tcache\n"));
1958 for (c
= 0; c
< children
; c
++) {
1959 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1961 (void) printf("\t %s\n", vname
);
1966 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1967 &child
, &children
) == 0) {
1968 (void) printf(gettext("\tspares\n"));
1969 for (c
= 0; c
< children
; c
++) {
1970 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1972 (void) printf("\t %s\n", vname
);
1980 * Logs are recorded as top level vdevs in the main pool child array
1981 * but with "is_log" set to 1. We use either print_status_config() or
1982 * print_import_config() to print the top level logs then any log
1983 * children (eg mirrored slogs) are printed recursively - which
1984 * works because only the top level vdev is marked "is_log"
1987 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1992 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1996 (void) printf(gettext("\tlogs\n"));
1998 for (c
= 0; c
< children
; c
++) {
1999 uint64_t is_log
= B_FALSE
;
2002 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2006 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2007 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2008 if (cb
->cb_print_status
)
2009 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2012 print_import_config(cb
, name
, child
[c
], 2);
2018 * Display the status for the given pool.
2021 show_import(nvlist_t
*config
)
2023 uint64_t pool_state
;
2027 uint64_t hostid
= 0;
2029 char *hostname
= "unknown";
2030 nvlist_t
*nvroot
, *nvinfo
;
2031 zpool_status_t reason
;
2032 zpool_errata_t errata
;
2036 status_cbdata_t cb
= { 0 };
2038 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2040 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2042 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2044 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2047 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2048 (uint64_t **)&vs
, &vsc
) == 0);
2049 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2051 reason
= zpool_import_status(config
, &msgid
, &errata
);
2053 (void) printf(gettext(" pool: %s\n"), name
);
2054 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2055 (void) printf(gettext(" state: %s"), health
);
2056 if (pool_state
== POOL_STATE_DESTROYED
)
2057 (void) printf(gettext(" (DESTROYED)"));
2058 (void) printf("\n");
2061 case ZPOOL_STATUS_MISSING_DEV_R
:
2062 case ZPOOL_STATUS_MISSING_DEV_NR
:
2063 case ZPOOL_STATUS_BAD_GUID_SUM
:
2064 (void) printf(gettext(" status: One or more devices are "
2065 "missing from the system.\n"));
2068 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2069 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2070 (void) printf(gettext(" status: One or more devices contains "
2071 "corrupted data.\n"));
2074 case ZPOOL_STATUS_CORRUPT_DATA
:
2076 gettext(" status: The pool data is corrupted.\n"));
2079 case ZPOOL_STATUS_OFFLINE_DEV
:
2080 (void) printf(gettext(" status: One or more devices "
2081 "are offlined.\n"));
2084 case ZPOOL_STATUS_CORRUPT_POOL
:
2085 (void) printf(gettext(" status: The pool metadata is "
2089 case ZPOOL_STATUS_VERSION_OLDER
:
2090 (void) printf(gettext(" status: The pool is formatted using a "
2091 "legacy on-disk version.\n"));
2094 case ZPOOL_STATUS_VERSION_NEWER
:
2095 (void) printf(gettext(" status: The pool is formatted using an "
2096 "incompatible version.\n"));
2099 case ZPOOL_STATUS_FEAT_DISABLED
:
2100 (void) printf(gettext(" status: Some supported features are "
2101 "not enabled on the pool.\n"));
2104 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2105 (void) printf(gettext("status: The pool uses the following "
2106 "feature(s) not supported on this system:\n"));
2107 zpool_print_unsup_feat(config
);
2110 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2111 (void) printf(gettext("status: The pool can only be accessed "
2112 "in read-only mode on this system. It\n\tcannot be "
2113 "accessed in read-write mode because it uses the "
2114 "following\n\tfeature(s) not supported on this system:\n"));
2115 zpool_print_unsup_feat(config
);
2118 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2119 (void) printf(gettext(" status: The pool is currently "
2120 "imported by another system.\n"));
2123 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2124 (void) printf(gettext(" status: The pool has the "
2125 "multihost property on. It cannot\n\tbe safely imported "
2126 "when the system hostid is not set.\n"));
2129 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2130 (void) printf(gettext(" status: The pool was last accessed by "
2131 "another system.\n"));
2134 case ZPOOL_STATUS_FAULTED_DEV_R
:
2135 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2136 (void) printf(gettext(" status: One or more devices are "
2140 case ZPOOL_STATUS_BAD_LOG
:
2141 (void) printf(gettext(" status: An intent log record cannot be "
2145 case ZPOOL_STATUS_RESILVERING
:
2146 (void) printf(gettext(" status: One or more devices were being "
2150 case ZPOOL_STATUS_ERRATA
:
2151 (void) printf(gettext(" status: Errata #%d detected.\n"),
2157 * No other status can be seen when importing pools.
2159 assert(reason
== ZPOOL_STATUS_OK
);
2163 * Print out an action according to the overall state of the pool.
2165 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2166 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2167 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2168 (void) printf(gettext(" action: The pool can be "
2169 "imported using its name or numeric identifier, "
2170 "though\n\tsome features will not be available "
2171 "without an explicit 'zpool upgrade'.\n"));
2172 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2173 (void) printf(gettext(" action: The pool can be "
2174 "imported using its name or numeric "
2175 "identifier and\n\tthe '-f' flag.\n"));
2176 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2178 case ZPOOL_ERRATA_NONE
:
2181 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2182 (void) printf(gettext(" action: The pool can "
2183 "be imported using its name or numeric "
2184 "identifier,\n\thowever there is a compat"
2185 "ibility issue which should be corrected"
2186 "\n\tby running 'zpool scrub'\n"));
2189 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2190 (void) printf(gettext(" action: The pool can"
2191 "not be imported with this version of ZFS "
2192 "due to\n\tan active asynchronous destroy. "
2193 "Revert to an earlier version\n\tand "
2194 "allow the destroy to complete before "
2198 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2199 (void) printf(gettext(" action: Existing "
2200 "encrypted datasets contain an on-disk "
2201 "incompatibility, which\n\tneeds to be "
2202 "corrected. Backup these datasets to new "
2203 "encrypted datasets\n\tand destroy the "
2209 * All errata must contain an action message.
2214 (void) printf(gettext(" action: The pool can be "
2215 "imported using its name or numeric "
2218 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2219 (void) printf(gettext(" action: The pool can be imported "
2220 "despite missing or damaged devices. The\n\tfault "
2221 "tolerance of the pool may be compromised if imported.\n"));
2224 case ZPOOL_STATUS_VERSION_NEWER
:
2225 (void) printf(gettext(" action: The pool cannot be "
2226 "imported. Access the pool on a system running "
2227 "newer\n\tsoftware, or recreate the pool from "
2230 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2231 (void) printf(gettext("action: The pool cannot be "
2232 "imported. Access the pool on a system that "
2233 "supports\n\tthe required feature(s), or recreate "
2234 "the pool from backup.\n"));
2236 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2237 (void) printf(gettext("action: The pool cannot be "
2238 "imported in read-write mode. Import the pool "
2240 "\t\"-o readonly=on\", access the pool on a system "
2241 "that supports the\n\trequired feature(s), or "
2242 "recreate the pool from backup.\n"));
2244 case ZPOOL_STATUS_MISSING_DEV_R
:
2245 case ZPOOL_STATUS_MISSING_DEV_NR
:
2246 case ZPOOL_STATUS_BAD_GUID_SUM
:
2247 (void) printf(gettext(" action: The pool cannot be "
2248 "imported. Attach the missing\n\tdevices and try "
2251 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2252 VERIFY0(nvlist_lookup_nvlist(config
,
2253 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2255 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2256 hostname
= fnvlist_lookup_string(nvinfo
,
2257 ZPOOL_CONFIG_MMP_HOSTNAME
);
2259 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2260 hostid
= fnvlist_lookup_uint64(nvinfo
,
2261 ZPOOL_CONFIG_MMP_HOSTID
);
2263 (void) printf(gettext(" action: The pool must be "
2264 "exported from %s (hostid=%lx)\n\tbefore it "
2265 "can be safely imported.\n"), hostname
,
2266 (unsigned long) hostid
);
2268 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2269 (void) printf(gettext(" action: Set a unique system "
2270 "hostid with the zgenhostid(8) command.\n"));
2273 (void) printf(gettext(" action: The pool cannot be "
2274 "imported due to damaged devices or data.\n"));
2278 /* Print the comment attached to the pool. */
2279 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2280 (void) printf(gettext("comment: %s\n"), comment
);
2283 * If the state is "closed" or "can't open", and the aux state
2284 * is "corrupt data":
2286 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2287 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2288 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2289 if (pool_state
== POOL_STATE_DESTROYED
)
2290 (void) printf(gettext("\tThe pool was destroyed, "
2291 "but can be imported using the '-Df' flags.\n"));
2292 else if (pool_state
!= POOL_STATE_EXPORTED
)
2293 (void) printf(gettext("\tThe pool may be active on "
2294 "another system, but can be imported using\n\t"
2295 "the '-f' flag.\n"));
2299 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2302 (void) printf(gettext(" config:\n\n"));
2304 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2306 if (cb
.cb_namewidth
< 10)
2307 cb
.cb_namewidth
= 10;
2309 print_import_config(&cb
, name
, nvroot
, 0);
2310 if (num_logs(nvroot
) > 0)
2311 print_logs(NULL
, &cb
, nvroot
);
2313 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2314 (void) printf(gettext("\n\tAdditional devices are known to "
2315 "be part of this pool, though their\n\texact "
2316 "configuration cannot be determined.\n"));
2321 zfs_force_import_required(nvlist_t
*config
)
2324 uint64_t hostid
= 0;
2327 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2328 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2330 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2333 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2334 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2335 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2336 ZPOOL_CONFIG_MMP_STATE
);
2338 if (mmp_state
!= MMP_STATE_INACTIVE
)
2346 * Perform the import for the given configuration. This passes the heavy
2347 * lifting off to zpool_import_props(), and then mounts the datasets contained
2351 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2352 nvlist_t
*props
, int flags
)
2355 zpool_handle_t
*zhp
;
2359 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2360 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2362 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2363 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2364 "is formatted using an unsupported ZFS version\n"), name
);
2366 } else if (zfs_force_import_required(config
) &&
2367 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2368 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2371 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2372 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2373 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2374 ZPOOL_CONFIG_MMP_STATE
);
2376 if (mmp_state
== MMP_STATE_ACTIVE
) {
2377 char *hostname
= "<unknown>";
2378 uint64_t hostid
= 0;
2380 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2381 hostname
= fnvlist_lookup_string(nvinfo
,
2382 ZPOOL_CONFIG_MMP_HOSTNAME
);
2384 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2385 hostid
= fnvlist_lookup_uint64(nvinfo
,
2386 ZPOOL_CONFIG_MMP_HOSTID
);
2388 (void) fprintf(stderr
, gettext("cannot import '%s': "
2389 "pool is imported on %s (hostid: "
2390 "0x%lx)\nExport the pool on the other system, "
2391 "then run 'zpool import'.\n"),
2392 name
, hostname
, (unsigned long) hostid
);
2393 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2394 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2395 "pool has the multihost property on and the\n"
2396 "system's hostid is not set. Set a unique hostid "
2397 "with the zgenhostid(8) command.\n"), name
);
2399 char *hostname
= "<unknown>";
2400 uint64_t timestamp
= 0;
2401 uint64_t hostid
= 0;
2403 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2404 hostname
= fnvlist_lookup_string(config
,
2405 ZPOOL_CONFIG_HOSTNAME
);
2407 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2408 timestamp
= fnvlist_lookup_uint64(config
,
2409 ZPOOL_CONFIG_TIMESTAMP
);
2411 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2412 hostid
= fnvlist_lookup_uint64(config
,
2413 ZPOOL_CONFIG_HOSTID
);
2415 (void) fprintf(stderr
, gettext("cannot import '%s': "
2416 "pool was previously in use from another system.\n"
2417 "Last accessed by %s (hostid=%lx) at %s"
2418 "The pool can be imported, use 'zpool import -f' "
2419 "to import the pool.\n"), name
, hostname
,
2420 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2426 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2429 if (newname
!= NULL
)
2430 name
= (char *)newname
;
2432 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2436 * Loading keys is best effort. We don't want to return immediately
2437 * if it fails but we do want to give the error to the caller.
2439 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
2440 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
2445 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2446 !(flags
& ZFS_IMPORT_ONLY
) &&
2447 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2457 * zpool import [-d dir] [-D]
2458 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2459 * [-d dir | -c cachefile] [-f] -a
2460 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2461 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2463 * -c Read pool information from a cachefile instead of searching
2466 * -d Scan in a specific directory, other than /dev/. More than
2467 * one directory can be specified using multiple '-d' options.
2469 * -D Scan for previously destroyed pools or import all or only
2470 * specified destroyed pools.
2472 * -R Temporarily import the pool, with all mountpoints relative to
2473 * the given root. The pool will remain exported when the machine
2476 * -V Import even in the presence of faulted vdevs. This is an
2477 * intentionally undocumented option for testing purposes, and
2478 * treats the pool configuration as complete, leaving any bad
2479 * vdevs in the FAULTED state. In other words, it does verbatim
2482 * -f Force import, even if it appears that the pool is active.
2484 * -F Attempt rewind if necessary.
2486 * -n See if rewind would work, but don't actually rewind.
2488 * -N Import the pool but don't mount datasets.
2490 * -T Specify a starting txg to use for import. This option is
2491 * intentionally undocumented option for testing purposes.
2493 * -a Import all pools found.
2495 * -l Load encryption keys while importing.
2497 * -o Set property=value and/or temporary mount options (without '=').
2499 * -s Scan using the default search path, the libblkid cache will
2502 * The import command scans for pools to import, and import pools based on pool
2503 * name and GUID. The pool can also be renamed as part of the import process.
2506 zpool_do_import(int argc
, char **argv
)
2508 char **searchdirs
= NULL
;
2509 char *env
, *envdup
= NULL
;
2513 nvlist_t
*pools
= NULL
;
2514 boolean_t do_all
= B_FALSE
;
2515 boolean_t do_destroyed
= B_FALSE
;
2516 char *mntopts
= NULL
;
2519 uint64_t searchguid
= 0;
2520 char *searchname
= NULL
;
2522 nvlist_t
*found_config
;
2523 nvlist_t
*policy
= NULL
;
2524 nvlist_t
*props
= NULL
;
2526 int flags
= ZFS_IMPORT_NORMAL
;
2527 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2528 boolean_t dryrun
= B_FALSE
;
2529 boolean_t do_rewind
= B_FALSE
;
2530 boolean_t xtreme_rewind
= B_FALSE
;
2531 boolean_t do_scan
= B_FALSE
;
2532 uint64_t pool_state
, txg
= -1ULL;
2533 char *cachefile
= NULL
;
2534 importargs_t idata
= { 0 };
2538 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
2547 if (searchdirs
== NULL
) {
2548 searchdirs
= safe_malloc(sizeof (char *));
2550 char **tmp
= safe_malloc((nsearch
+ 1) *
2552 bcopy(searchdirs
, tmp
, nsearch
*
2557 searchdirs
[nsearch
++] = optarg
;
2560 do_destroyed
= B_TRUE
;
2563 flags
|= ZFS_IMPORT_ANY_HOST
;
2569 flags
|= ZFS_IMPORT_LOAD_KEYS
;
2572 flags
|= ZFS_IMPORT_MISSING_LOG
;
2578 flags
|= ZFS_IMPORT_ONLY
;
2581 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2584 if (add_prop_list(optarg
, propval
,
2592 if (add_prop_list(zpool_prop_to_name(
2593 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2595 if (add_prop_list_default(zpool_prop_to_name(
2596 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2603 flags
|= ZFS_IMPORT_TEMP_NAME
;
2604 if (add_prop_list_default(zpool_prop_to_name(
2605 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2611 txg
= strtoull(optarg
, &endptr
, 0);
2612 if (errno
!= 0 || *endptr
!= '\0') {
2613 (void) fprintf(stderr
,
2614 gettext("invalid txg value\n"));
2617 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2620 flags
|= ZFS_IMPORT_VERBATIM
;
2623 xtreme_rewind
= B_TRUE
;
2626 (void) fprintf(stderr
, gettext("missing argument for "
2627 "'%c' option\n"), optopt
);
2631 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2640 if (cachefile
&& nsearch
!= 0) {
2641 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2645 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
2646 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
2650 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
2651 (void) fprintf(stderr
, gettext("-l is only meaningful during "
2656 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2657 (void) fprintf(stderr
,
2658 gettext("-n or -X only meaningful with -F\n"));
2662 rewind_policy
= ZPOOL_TRY_REWIND
;
2664 rewind_policy
= ZPOOL_DO_REWIND
;
2666 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2668 /* In the future, we can capture further policy and include it here */
2669 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2670 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
2671 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
2672 rewind_policy
) != 0)
2675 /* check argument count */
2678 (void) fprintf(stderr
, gettext("too many arguments\n"));
2683 (void) fprintf(stderr
, gettext("too many arguments\n"));
2689 * Check for the effective uid. We do this explicitly here because
2690 * otherwise any attempt to discover pools will silently fail.
2692 if (argc
== 0 && geteuid() != 0) {
2693 (void) fprintf(stderr
, gettext("cannot "
2694 "discover pools: permission denied\n"));
2695 if (searchdirs
!= NULL
)
2699 nvlist_free(policy
);
2704 * Depending on the arguments given, we do one of the following:
2706 * <none> Iterate through all pools and display information about
2709 * -a Iterate through all pools and try to import each one.
2711 * <id> Find the pool that corresponds to the given GUID/pool
2712 * name and import that one.
2714 * -D Above options applies only to destroyed pools.
2720 searchguid
= strtoull(argv
[0], &endptr
, 10);
2721 if (errno
!= 0 || *endptr
!= '\0') {
2722 searchname
= argv
[0];
2725 found_config
= NULL
;
2728 * User specified a name or guid. Ensure it's unique.
2730 idata
.unique
= B_TRUE
;
2734 * Check the environment for the preferred search path.
2736 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2739 envdup
= strdup(env
);
2741 dir
= strtok(envdup
, ":");
2742 while (dir
!= NULL
) {
2743 if (searchdirs
== NULL
) {
2744 searchdirs
= safe_malloc(sizeof (char *));
2746 char **tmp
= safe_malloc((nsearch
+ 1) *
2748 bcopy(searchdirs
, tmp
, nsearch
*
2753 searchdirs
[nsearch
++] = dir
;
2754 dir
= strtok(NULL
, ":");
2758 idata
.path
= searchdirs
;
2759 idata
.paths
= nsearch
;
2760 idata
.poolname
= searchname
;
2761 idata
.guid
= searchguid
;
2762 idata
.cachefile
= cachefile
;
2763 idata
.scan
= do_scan
;
2764 idata
.policy
= policy
;
2766 pools
= zpool_search_import(g_zfs
, &idata
);
2768 if (pools
!= NULL
&& idata
.exists
&&
2769 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2770 (void) fprintf(stderr
, gettext("cannot import '%s': "
2771 "a pool with that name already exists\n"),
2773 (void) fprintf(stderr
, gettext("use the form '%s "
2774 "<pool | id> <newpool>' to give it a new name\n"),
2777 } else if (pools
== NULL
&& idata
.exists
) {
2778 (void) fprintf(stderr
, gettext("cannot import '%s': "
2779 "a pool with that name is already created/imported,\n"),
2781 (void) fprintf(stderr
, gettext("and no additional pools "
2782 "with that name were found\n"));
2784 } else if (pools
== NULL
) {
2786 (void) fprintf(stderr
, gettext("cannot import '%s': "
2787 "no such pool available\n"), argv
[0]);
2793 if (searchdirs
!= NULL
)
2797 nvlist_free(policy
);
2804 * At this point we have a list of import candidate configs. Even if
2805 * we were searching by pool name or guid, we still need to
2806 * post-process the list to deal with pool state and possible
2812 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2814 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2816 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2818 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2820 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2823 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
2830 (void) printf("\n");
2833 err
|= do_import(config
, NULL
, mntopts
,
2836 show_import(config
);
2838 } else if (searchname
!= NULL
) {
2842 * We are searching for a pool based on name.
2844 verify(nvlist_lookup_string(config
,
2845 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2847 if (strcmp(name
, searchname
) == 0) {
2848 if (found_config
!= NULL
) {
2849 (void) fprintf(stderr
, gettext(
2850 "cannot import '%s': more than "
2851 "one matching pool\n"), searchname
);
2852 (void) fprintf(stderr
, gettext(
2853 "import by numeric ID instead\n"));
2856 found_config
= config
;
2862 * Search for a pool by guid.
2864 verify(nvlist_lookup_uint64(config
,
2865 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2867 if (guid
== searchguid
)
2868 found_config
= config
;
2873 * If we were searching for a specific pool, verify that we found a
2874 * pool, and then do the import.
2876 if (argc
!= 0 && err
== 0) {
2877 if (found_config
== NULL
) {
2878 (void) fprintf(stderr
, gettext("cannot import '%s': "
2879 "no such pool available\n"), argv
[0]);
2882 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2883 argv
[1], mntopts
, props
, flags
);
2888 * If we were just looking for pools, report an error if none were
2891 if (argc
== 0 && first
)
2892 (void) fprintf(stderr
,
2893 gettext("no pools available to import\n"));
2898 nvlist_free(policy
);
2899 if (searchdirs
!= NULL
)
2904 return (err
? 1 : 0);
2908 * zpool sync [-f] [pool] ...
2910 * -f (undocumented) force uberblock (and config including zpool cache file)
2913 * Sync the specified pool(s).
2914 * Without arguments "zpool sync" will sync all pools.
2915 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2919 zpool_do_sync(int argc
, char **argv
)
2922 boolean_t force
= B_FALSE
;
2925 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
2931 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2940 /* if argc == 0 we will execute zpool_sync_one on all pools */
2941 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
2946 typedef struct iostat_cbdata
{
2951 char **cb_vdev_names
; /* Only show these vdevs */
2952 unsigned int cb_vdev_names_count
;
2953 boolean_t cb_verbose
;
2954 boolean_t cb_literal
;
2955 boolean_t cb_scripted
;
2956 zpool_list_t
*cb_list
;
2957 vdev_cmd_data_list_t
*vcdl
;
2961 typedef struct name_and_columns
{
2962 const char *name
; /* Column name */
2963 unsigned int columns
; /* Center name to this number of columns */
2964 } name_and_columns_t
;
2966 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2968 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
2970 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2972 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2973 {"asyncq_wait", 2}, {"scrub"}},
2974 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
2975 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2977 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
2978 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
2979 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
2980 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
2984 /* Shorthand - if "columns" field not set, default to 1 column */
2985 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
2987 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2989 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2990 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
2991 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2992 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
2993 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2994 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
2995 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2996 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
2999 static const char *histo_to_title
[] = {
3000 [IOS_L_HISTO
] = "latency",
3001 [IOS_RQ_HISTO
] = "req_size",
3005 * Return the number of labels in a null-terminated name_and_columns_t
3010 label_array_len(const name_and_columns_t
*labels
)
3014 while (labels
[i
].name
)
3021 * Return the number of strings in a null-terminated string array.
3024 * const char foo[] = {"bar", "baz", NULL}
3029 str_array_len(const char *array
[])
3040 * Return a default column width for default/latency/queue columns. This does
3041 * not include histograms, which have their columns autosized.
3044 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3046 unsigned long column_width
= 5; /* Normal niceprint */
3047 static unsigned long widths
[] = {
3049 * Choose some sane default column sizes for printing the
3052 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3053 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3054 [IOS_QUEUES
] = 6, /* 1M queue entries */
3058 column_width
= widths
[type
];
3060 return (column_width
);
3064 * Print the column labels, i.e:
3066 * capacity operations bandwidth
3067 * alloc free read write read write ...
3069 * If force_column_width is set, use it for the column width. If not set, use
3070 * the default column width.
3073 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3074 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3077 unsigned int text_start
, rw_column_width
, spaces_to_end
;
3078 uint64_t flags
= cb
->cb_flags
;
3080 unsigned int column_width
= force_column_width
;
3082 /* For each bit set in flags */
3083 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3084 idx
= lowbit64(f
) - 1;
3085 if (!force_column_width
)
3086 column_width
= default_column_width(cb
, idx
);
3087 /* Print our top labels centered over "read write" label. */
3088 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3089 const char *name
= labels
[idx
][i
].name
;
3091 * We treat labels[][].columns == 0 as shorthand
3092 * for one column. It makes writing out the label
3093 * tables more concise.
3095 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3096 unsigned int slen
= strlen(name
);
3098 rw_column_width
= (column_width
* columns
) +
3099 (2 * (columns
- 1));
3101 text_start
= (int)((rw_column_width
)/columns
-
3104 printf(" "); /* Two spaces between columns */
3106 /* Space from beginning of column to label */
3107 for (s
= 0; s
< text_start
; s
++)
3112 /* Print space after label to end of column */
3113 spaces_to_end
= rw_column_width
- text_start
- slen
;
3114 for (s
= 0; s
< spaces_to_end
; s
++)
3123 * print_cmd_columns - Print custom column titles from -c
3125 * If the user specified the "zpool status|iostat -c" then print their custom
3126 * column titles in the header. For example, print_cmd_columns() would print
3127 * the " col1 col2" part of this:
3129 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3131 * capacity operations bandwidth
3132 * pool alloc free read write read write col1 col2
3133 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3134 * mypool 269K 1008M 0 0 107 946
3135 * mirror 269K 1008M 0 0 107 946
3136 * sdb - - 0 0 102 473 val1 val2
3137 * sdc - - 0 0 5 473 val1 val2
3138 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3141 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3144 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3146 if (vcdl
->count
== 0 || data
== NULL
)
3150 * Each vdev cmd should have the same column names unless the user did
3151 * something weird with their cmd. Just take the column names from the
3152 * first vdev and assume it works for all of them.
3154 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3157 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3160 printf("%*s", vcdl
->uniq_cols_width
[i
],
3161 vcdl
->uniq_cols
[i
]);
3168 * Utility function to print out a line of dashes like:
3170 * -------------------------------- ----- ----- ----- ----- -----
3172 * ...or a dashed named-row line like:
3178 * @force_column_width If non-zero, use the value as the column width.
3179 * Otherwise use the default column widths.
3181 * @name: Print a dashed named-row line starting
3182 * with @name. Otherwise, print a regular
3186 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3190 unsigned int namewidth
;
3191 uint64_t flags
= cb
->cb_flags
;
3194 const name_and_columns_t
*labels
;
3198 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3199 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3200 } else if (cb
->cb_vdev_names_count
) {
3206 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3207 name
? strlen(name
) : 0);
3211 printf("%-*s", namewidth
, name
);
3213 for (i
= 0; i
< namewidth
; i
++)
3217 /* For each bit in flags */
3218 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3219 unsigned int column_width
;
3220 idx
= lowbit64(f
) - 1;
3221 if (force_column_width
)
3222 column_width
= force_column_width
;
3224 column_width
= default_column_width(cb
, idx
);
3226 labels
= iostat_bottom_labels
[idx
];
3227 for (i
= 0; i
< label_array_len(labels
); i
++) {
3229 printf(" %*s-", column_width
- 1, " ");
3231 printf(" %.*s", column_width
,
3232 "--------------------");
3239 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3240 unsigned int force_column_width
)
3242 print_iostat_dashes(cb
, force_column_width
, NULL
);
3246 print_iostat_separator(iostat_cbdata_t
*cb
)
3248 print_iostat_separator_impl(cb
, 0);
3252 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3253 const char *histo_vdev_name
)
3255 unsigned int namewidth
;
3258 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3259 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3260 } else if (cb
->cb_vdev_names_count
) {
3266 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3267 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3269 if (histo_vdev_name
)
3270 printf("%-*s", namewidth
, histo_vdev_name
);
3272 printf("%*s", namewidth
, "");
3275 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3278 printf("%-*s", namewidth
, title
);
3280 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3281 if (cb
->vcdl
!= NULL
)
3282 print_cmd_columns(cb
->vcdl
, 0);
3286 print_iostat_separator_impl(cb
, force_column_width
);
3288 if (cb
->vcdl
!= NULL
)
3289 print_cmd_columns(cb
->vcdl
, 1);
3295 print_iostat_header(iostat_cbdata_t
*cb
)
3297 print_iostat_header_impl(cb
, 0, NULL
);
3302 * Display a single statistic.
3305 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3306 unsigned int column_size
, boolean_t scripted
)
3310 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3313 printf("\t%s", buf
);
3315 printf(" %*s", column_size
, buf
);
3319 * Calculate the default vdev stats
3321 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3322 * stats into calcvs.
3325 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3326 vdev_stat_t
*calcvs
)
3330 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3331 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3332 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3334 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3335 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
3339 * Internal representation of the extended iostats data.
3341 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3342 * or single uint64_t's. We make both look like arrays to make them easier
3343 * to process. In order to make single uint64_t's look like arrays, we set
3344 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3345 * we can just use *data and count.
3349 uint_t count
; /* Number of entries in data[] */
3350 uint64_t __data
; /* Only used when data is a single uint64_t */
3354 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
3358 for (i
= 0; i
< len
; i
++)
3359 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
3365 * Helper function to lookup a uint64_t array or uint64_t value and store its
3366 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3367 * it look like a one element array to make it easier to process.
3370 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
3371 struct stat_array
*nva
)
3376 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
3377 switch (nvpair_type(tmp
)) {
3378 case DATA_TYPE_UINT64_ARRAY
:
3379 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
3381 case DATA_TYPE_UINT64
:
3382 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
3383 nva
->data
= &nva
->__data
;
3387 /* Not a uint64_t */
3396 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3397 * subtract them, and return the results in a newly allocated stat_array.
3398 * You must free the returned array after you are done with it with
3399 * free_calc_stats().
3401 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3404 static struct stat_array
*
3405 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3408 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3409 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3411 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3413 /* Extract our extended stats nvlist from the main list */
3414 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3417 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3421 newnva
= safe_malloc(alloc_size
);
3422 oldnva
= safe_malloc(alloc_size
);
3423 calcnva
= safe_malloc(alloc_size
);
3425 for (j
= 0; j
< len
; j
++) {
3426 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3428 calcnva
[j
].count
= newnva
[j
].count
;
3429 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3430 calcnva
[j
].data
= safe_malloc(alloc_size
);
3431 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3434 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3436 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3437 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3446 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3449 for (i
= 0; i
< len
; i
++)
3456 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3457 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3463 enum zfs_nicenum_format format
;
3464 unsigned int buckets
;
3465 unsigned int start_bucket
;
3468 format
= ZFS_NICENUM_RAW
;
3470 format
= ZFS_NICENUM_1024
;
3472 /* All these histos are the same size, so just use nva[0].count */
3473 buckets
= nva
[0].count
;
3475 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3476 /* Start at 512 - req size should never be lower than this */
3482 for (j
= start_bucket
; j
< buckets
; j
++) {
3483 /* Print histogram bucket label */
3484 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3485 /* Ending range of this bucket */
3486 val
= (1UL << (j
+ 1)) - 1;
3487 zfs_nicetime(val
, buf
, sizeof (buf
));
3489 /* Request size (starting range of bucket) */
3491 zfs_nicenum(val
, buf
, sizeof (buf
));
3494 if (cb
->cb_scripted
)
3495 printf("%llu", (u_longlong_t
)val
);
3497 printf("%-*s", namewidth
, buf
);
3499 /* Print the values on the line */
3500 for (i
= 0; i
< len
; i
++) {
3501 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3502 column_width
, cb
->cb_scripted
);
3509 print_solid_separator(unsigned int length
)
3517 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3518 nvlist_t
*newnv
, double scale
, const char *name
)
3520 unsigned int column_width
;
3521 unsigned int namewidth
;
3522 unsigned int entire_width
;
3523 enum iostat_type type
;
3524 struct stat_array
*nva
;
3526 unsigned int names_len
;
3528 /* What type of histo are we? */
3529 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3531 /* Get NULL-terminated array of nvlist names for our histo */
3532 names
= vsx_type_to_nvlist
[type
];
3533 names_len
= str_array_len(names
); /* num of names */
3535 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3537 if (cb
->cb_literal
) {
3538 column_width
= MAX(5,
3539 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3544 namewidth
= MAX(cb
->cb_namewidth
,
3545 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3548 * Calculate the entire line width of what we're printing. The
3549 * +2 is for the two spaces between columns:
3553 /* |___| <---------- column_width */
3555 /* |__________| <--- entire_width */
3557 entire_width
= namewidth
+ (column_width
+ 2) *
3558 label_array_len(iostat_bottom_labels
[type
]);
3560 if (cb
->cb_scripted
)
3561 printf("%s\n", name
);
3563 print_iostat_header_impl(cb
, column_width
, name
);
3565 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3568 free_calc_stats(nva
, names_len
);
3569 if (!cb
->cb_scripted
)
3570 print_solid_separator(entire_width
);
3574 * Calculate the average latency of a power-of-two latency histogram
3577 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3580 uint64_t count
= 0, total
= 0;
3582 for (i
= 0; i
< buckets
; i
++) {
3584 * Our buckets are power-of-two latency ranges. Use the
3585 * midpoint latency of each bucket to calculate the average.
3593 if (histo
[i
] != 0) {
3594 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3599 /* Prevent divide by zero */
3600 return (count
== 0 ? 0 : total
/ count
);
3604 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3605 nvlist_t
*newnv
, double scale
)
3609 const char *names
[] = {
3610 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3611 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3612 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3613 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3614 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3615 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3616 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3617 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3618 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3619 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3622 struct stat_array
*nva
;
3624 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3625 enum zfs_nicenum_format format
;
3627 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3630 format
= ZFS_NICENUM_RAW
;
3632 format
= ZFS_NICENUM_1024
;
3634 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3635 val
= nva
[i
].data
[0] * scale
;
3636 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3639 free_calc_stats(nva
, ARRAY_SIZE(names
));
3643 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3644 nvlist_t
*newnv
, double scale
)
3648 const char *names
[] = {
3649 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3650 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3651 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3652 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3653 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3654 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3655 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3656 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3657 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3659 struct stat_array
*nva
;
3661 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3662 enum zfs_nicenum_format format
;
3664 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3667 format
= ZFS_NICENUM_RAWTIME
;
3669 format
= ZFS_NICENUM_TIME
;
3671 /* Print our avg latencies on the line */
3672 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3673 /* Compute average latency for a latency histo */
3674 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3675 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3677 free_calc_stats(nva
, ARRAY_SIZE(names
));
3681 * Print default statistics (capacity/operations/bandwidth)
3684 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3686 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3687 enum zfs_nicenum_format format
;
3688 char na
; /* char to print for "not applicable" values */
3690 if (cb
->cb_literal
) {
3691 format
= ZFS_NICENUM_RAW
;
3694 format
= ZFS_NICENUM_1024
;
3698 /* only toplevel vdevs have capacity stats */
3699 if (vs
->vs_space
== 0) {
3700 if (cb
->cb_scripted
)
3701 printf("\t%c\t%c", na
, na
);
3703 printf(" %*c %*c", column_width
, na
, column_width
,
3706 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3708 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3709 column_width
, cb
->cb_scripted
);
3712 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3713 format
, column_width
, cb
->cb_scripted
);
3714 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3715 format
, column_width
, cb
->cb_scripted
);
3716 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3717 format
, column_width
, cb
->cb_scripted
);
3718 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3719 format
, column_width
, cb
->cb_scripted
);
3723 * Print out all the statistics for the given vdev. This can either be the
3724 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3725 * is a verbose output, and we don't want to display the toplevel pool stats.
3727 * Returns the number of stat lines printed.
3730 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3731 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3733 nvlist_t
**oldchild
, **newchild
;
3734 uint_t c
, children
, oldchildren
;
3735 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3736 vdev_stat_t zerovs
= { 0 };
3743 calcvs
= safe_malloc(sizeof (*calcvs
));
3745 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
3748 if (oldnv
!= NULL
) {
3749 verify(nvlist_lookup_uint64_array(oldnv
,
3750 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3755 /* Do we only want to see a specific vdev? */
3756 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3757 /* Yes we do. Is this the vdev? */
3758 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3760 * This is our vdev. Since it is the only vdev we
3761 * will be displaying, make depth = 0 so that it
3762 * doesn't get indented.
3769 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3770 /* Couldn't match the name */
3775 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3776 (uint64_t **)&newvs
, &c
) == 0);
3779 * Print the vdev name unless it's is a histogram. Histograms
3780 * display the vdev name in the header itself.
3782 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3783 if (cb
->cb_scripted
) {
3786 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3787 (void) printf("%*s%s", depth
, "", name
);
3789 (void) printf("%*s%s%*s", depth
, "", name
,
3790 (int)(cb
->cb_namewidth
- strlen(name
) -
3795 /* Calculate our scaling factor */
3796 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3797 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3799 * If we specify printing histograms with no time interval, then
3800 * print the histogram numbers over the entire lifetime of the
3808 scale
= (double)NANOSEC
/ tdelta
;
3811 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3812 calc_default_iostats(oldvs
, newvs
, calcvs
);
3813 print_iostat_default(calcvs
, cb
, scale
);
3815 if (cb
->cb_flags
& IOS_LATENCY_M
)
3816 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3817 if (cb
->cb_flags
& IOS_QUEUES_M
)
3818 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3819 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3821 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3824 if (cb
->vcdl
!= NULL
) {
3826 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3829 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3833 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3842 if (!cb
->cb_verbose
)
3845 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3846 &newchild
, &children
) != 0)
3850 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3851 &oldchild
, &oldchildren
) != 0)
3854 children
= MIN(oldchildren
, children
);
3857 for (c
= 0; c
< children
; c
++) {
3858 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3860 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3863 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3866 if (ishole
|| islog
)
3869 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3871 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3872 newchild
[c
], cb
, depth
+ 2);
3877 * Log device section
3880 if (num_logs(newnv
) > 0) {
3881 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3882 !cb
->cb_vdev_names
) {
3883 print_iostat_dashes(cb
, 0, "logs");
3887 for (c
= 0; c
< children
; c
++) {
3888 uint64_t islog
= B_FALSE
;
3889 (void) nvlist_lookup_uint64(newchild
[c
],
3890 ZPOOL_CONFIG_IS_LOG
, &islog
);
3893 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3895 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3896 oldchild
[c
] : NULL
, newchild
[c
],
3905 * Include level 2 ARC devices in iostat output
3907 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3908 &newchild
, &children
) != 0)
3912 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
3913 &oldchild
, &oldchildren
) != 0)
3916 children
= MIN(oldchildren
, children
);
3920 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3921 !cb
->cb_vdev_names
) {
3922 print_iostat_dashes(cb
, 0, "cache");
3926 for (c
= 0; c
< children
; c
++) {
3927 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3929 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
3930 : NULL
, newchild
[c
], cb
, depth
+ 2);
3939 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
3941 iostat_cbdata_t
*cb
= data
;
3945 * If the pool has disappeared, remove it from the list and continue.
3947 if (zpool_refresh_stats(zhp
, &missing
) != 0)
3951 pool_list_remove(cb
->cb_list
, zhp
);
3957 * Callback to print out the iostats for the given pool.
3960 print_iostat(zpool_handle_t
*zhp
, void *data
)
3962 iostat_cbdata_t
*cb
= data
;
3963 nvlist_t
*oldconfig
, *newconfig
;
3964 nvlist_t
*oldnvroot
, *newnvroot
;
3967 newconfig
= zpool_get_config(zhp
, &oldconfig
);
3969 if (cb
->cb_iteration
== 1)
3972 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3975 if (oldconfig
== NULL
)
3978 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3981 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
3983 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
3984 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
3985 print_iostat_separator(cb
);
3986 if (cb
->vcdl
!= NULL
) {
3987 print_cmd_columns(cb
->vcdl
, 1);
4002 if (isatty(STDOUT_FILENO
)) {
4003 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4005 columns
= ws
.ws_col
;
4014 get_namewidth(zpool_handle_t
*zhp
, void *data
)
4016 iostat_cbdata_t
*cb
= data
;
4017 nvlist_t
*config
, *nvroot
;
4020 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4021 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4023 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4024 if (!cb
->cb_verbose
)
4025 cb
->cb_namewidth
= MAX(poolname_len
, cb
->cb_namewidth
);
4027 cb
->cb_namewidth
= MAX(poolname_len
,
4028 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
4029 cb
->cb_name_flags
));
4032 * The width must be at least 10, but may be as large as the
4033 * column width - 42 so that we can still fit in one line.
4035 columns
= get_columns();
4037 if (cb
->cb_namewidth
< 10)
4038 cb
->cb_namewidth
= 10;
4039 if (cb
->cb_namewidth
> columns
- 42)
4040 cb
->cb_namewidth
= columns
- 42;
4046 * Parse the input string, get the 'interval' and 'count' value if there is one.
4049 get_interval_count(int *argcp
, char **argv
, float *iv
,
4053 unsigned long count
= 0;
4057 * Determine if the last argument is an integer or a pool name
4059 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4063 interval
= strtof(argv
[argc
- 1], &end
);
4065 if (*end
== '\0' && errno
== 0) {
4066 if (interval
== 0) {
4067 (void) fprintf(stderr
, gettext("interval "
4068 "cannot be zero\n"));
4072 * Ignore the last parameter
4077 * If this is not a valid number, just plow on. The
4078 * user will get a more informative error message later
4086 * If the last argument is also an integer, then we have both a count
4089 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4094 interval
= strtof(argv
[argc
- 1], &end
);
4096 if (*end
== '\0' && errno
== 0) {
4097 if (interval
== 0) {
4098 (void) fprintf(stderr
, gettext("interval "
4099 "cannot be zero\n"));
4104 * Ignore the last parameter
4118 get_timestamp_arg(char c
)
4121 timestamp_fmt
= UDATE
;
4123 timestamp_fmt
= DDATE
;
4129 * Return stat flags that are supported by all pools by both the module and
4130 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4131 * It will get ANDed down until only the flags that are supported on all pools
4135 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4137 uint64_t *mask
= data
;
4138 nvlist_t
*config
, *nvroot
, *nvx
;
4142 config
= zpool_get_config(zhp
, NULL
);
4143 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4146 /* Default stats are always supported, but for completeness.. */
4147 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4148 flags
|= IOS_DEFAULT_M
;
4150 /* Get our extended stats nvlist from the main list */
4151 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4154 * No extended stats; they're probably running an older
4155 * module. No big deal, we support that too.
4160 /* For each extended stat, make sure all its nvpairs are supported */
4161 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4162 if (!vsx_type_to_nvlist
[j
][0])
4165 /* Start off by assuming the flag is supported, then check */
4166 flags
|= (1ULL << j
);
4167 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4168 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4169 /* flag isn't supported */
4170 flags
= flags
& ~(1ULL << j
);
4176 *mask
= *mask
& flags
;
4181 * Return a bitmask of stats that are supported on all pools by both the module
4185 get_stat_flags(zpool_list_t
*list
)
4190 * get_stat_flags_cb() will lop off bits from "mask" until only the
4191 * flags that are supported on all pools remain.
4193 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4198 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4201 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4203 iostat_cbdata_t
*cb
= cb_data
;
4207 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4209 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4210 ret
= 1; /* match */
4217 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4220 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4222 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4226 * Check if vdevs are in a pool
4228 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4229 * return 0. If pool_name is NULL, then search all pools.
4232 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4233 iostat_cbdata_t
*cb
)
4240 if ((argc
== 0) || !*argv
)
4246 /* Temporarily hijack cb_vdev_names for a second... */
4247 tmp_name
= cb
->cb_vdev_names
;
4249 /* Go though our list of prospective vdev names */
4250 for (i
= 0; i
< argc
; i
++) {
4251 cb
->cb_vdev_names
= argv
+ i
;
4253 /* Is this name a vdev in our pools? */
4254 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4262 cb
->cb_vdev_names
= tmp_name
;
4268 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4271 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4278 * Do we have a pool named *name? If so, return 1, otherwise 0.
4283 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4286 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4288 are_all_pools(int argc
, char **argv
)
4290 if ((argc
== 0) || !*argv
)
4294 if (!is_pool(argv
[argc
]))
4301 * Helper function to print out vdev/pool names we can't resolve. Used for an
4305 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4306 iostat_cbdata_t
*cb
)
4311 for (i
= 0; i
< argc
; i
++) {
4315 str
= gettext("pool");
4316 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
4317 str
= gettext("vdev in this pool");
4318 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
4319 str
= gettext("vdev in another pool");
4321 str
= gettext("unknown");
4323 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
4328 * Same as get_interval_count(), but with additional checks to not misinterpret
4329 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4333 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
4334 unsigned long *count
, iostat_cbdata_t
*cb
)
4336 char **tmpargv
= argv
;
4337 int argc_for_interval
= 0;
4339 /* Is the last arg an interval value? Or a guid? */
4340 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
4342 * The last arg is not a guid, so it's probably an
4345 argc_for_interval
++;
4348 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
4350 * The 2nd to last arg is not a guid, so it's probably
4351 * an interval value.
4353 argc_for_interval
++;
4357 /* Point to our list of possible intervals */
4358 tmpargv
= &argv
[*argc
- argc_for_interval
];
4360 *argc
= *argc
- argc_for_interval
;
4361 get_interval_count(&argc_for_interval
, tmpargv
,
4366 * Floating point sleep(). Allows you to pass in a floating point value for
4372 struct timespec req
;
4373 req
.tv_sec
= floor(sec
);
4374 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
4375 nanosleep(&req
, NULL
);
4379 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4382 * name: Short name of the script ('iostat').
4383 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4386 print_zpool_script_help(char *name
, char *path
)
4388 char *argv
[] = {path
, "-h", NULL
};
4389 char **lines
= NULL
;
4393 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
4395 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
4397 libzfs_free_str_array(lines
, lines_cnt
);
4401 for (int i
= 0; i
< lines_cnt
; i
++)
4402 if (!is_blank_str(lines
[i
]))
4403 printf(" %-14s %s\n", name
, lines
[i
]);
4405 libzfs_free_str_array(lines
, lines_cnt
);
4409 * Go though the zpool status/iostat -c scripts in the user's path, run their
4410 * help option (-h), and print out the results.
4413 print_zpool_dir_scripts(char *dirpath
)
4417 char fullpath
[MAXPATHLEN
];
4418 struct stat dir_stat
;
4420 if ((dir
= opendir(dirpath
)) != NULL
) {
4421 /* print all the files and directories within directory */
4422 while ((ent
= readdir(dir
)) != NULL
) {
4423 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
4425 /* Print the scripts */
4426 if (stat(fullpath
, &dir_stat
) == 0)
4427 if (dir_stat
.st_mode
& S_IXUSR
&&
4428 S_ISREG(dir_stat
.st_mode
))
4429 print_zpool_script_help(ent
->d_name
,
4437 * Print out help text for all zpool status/iostat -c scripts.
4440 print_zpool_script_list(char *subcommand
)
4444 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
4446 sp
= zpool_get_cmd_search_path();
4450 dir
= strtok(sp
, ":");
4451 while (dir
!= NULL
) {
4452 print_zpool_dir_scripts(dir
);
4453 dir
= strtok(NULL
, ":");
4460 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4461 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4462 * [interval [count]]
4464 * -c CMD For each vdev, run command CMD
4465 * -g Display guid for individual vdev name.
4466 * -L Follow links when resolving vdev path name.
4467 * -P Display full path for vdev name.
4468 * -v Display statistics for individual vdevs
4470 * -p Display values in parsable (exact) format.
4471 * -H Scripted mode. Don't display headers, and separate properties
4473 * -l Display average latency
4474 * -q Display queue depths
4475 * -w Display latency histograms
4476 * -r Display request size histogram
4477 * -T Display a timestamp in date(1) or Unix format
4479 * This command can be tricky because we want to be able to deal with pool
4480 * creation/destruction as well as vdev configuration changes. The bulk of this
4481 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4482 * on pool_list_update() to detect the addition of new pools. Configuration
4483 * changes are all handled within libzfs.
4486 zpool_do_iostat(int argc
, char **argv
)
4492 unsigned long count
= 0;
4494 boolean_t verbose
= B_FALSE
;
4495 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4496 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4497 boolean_t omit_since_boot
= B_FALSE
;
4498 boolean_t guid
= B_FALSE
;
4499 boolean_t follow_links
= B_FALSE
;
4500 boolean_t full_name
= B_FALSE
;
4501 iostat_cbdata_t cb
= { 0 };
4504 /* Used for printing error message */
4505 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4506 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4508 uint64_t unsupported_flags
;
4511 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4516 gettext("Can't set -c flag twice\n"));
4520 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
4521 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4522 fprintf(stderr
, gettext(
4523 "Can't run -c, disabled by "
4524 "ZPOOL_SCRIPTS_ENABLED.\n"));
4528 if ((getuid() <= 0 || geteuid() <= 0) &&
4529 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4530 fprintf(stderr
, gettext(
4531 "Can't run -c with root privileges "
4532 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4542 follow_links
= B_TRUE
;
4548 get_timestamp_arg(*optarg
);
4572 omit_since_boot
= B_TRUE
;
4578 if (optopt
== 'c') {
4579 print_zpool_script_list("iostat");
4583 gettext("invalid option '%c'\n"), optopt
);
4592 cb
.cb_literal
= parsable
;
4593 cb
.cb_scripted
= scripted
;
4596 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4598 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4600 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4601 cb
.cb_iteration
= 0;
4602 cb
.cb_namewidth
= 0;
4603 cb
.cb_verbose
= verbose
;
4605 /* Get our interval and count values (if any) */
4607 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4610 get_interval_count(&argc
, argv
, &interval
, &count
);
4614 /* No args, so just print the defaults. */
4615 } else if (are_all_pools(argc
, argv
)) {
4616 /* All the args are pool names */
4617 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4618 /* All the args are vdevs */
4619 cb
.cb_vdev_names
= argv
;
4620 cb
.cb_vdev_names_count
= argc
;
4621 argc
= 0; /* No pools to process */
4622 } else if (are_all_pools(1, argv
)) {
4623 /* The first arg is a pool name */
4624 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4625 /* ...and the rest are vdev names */
4626 cb
.cb_vdev_names
= argv
+ 1;
4627 cb
.cb_vdev_names_count
= argc
- 1;
4628 argc
= 1; /* One pool to process */
4630 fprintf(stderr
, gettext("Expected either a list of "));
4631 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4632 fprintf(stderr
, " \"%s\", ", argv
[0]);
4633 fprintf(stderr
, gettext("but got:\n"));
4634 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4636 fprintf(stderr
, "\n");
4642 * The args don't make sense. The first arg isn't a pool name,
4643 * nor are all the args vdevs.
4645 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4646 fprintf(stderr
, "\n");
4650 if (cb
.cb_vdev_names_count
!= 0) {
4652 * If user specified vdevs, it implies verbose.
4654 cb
.cb_verbose
= B_TRUE
;
4658 * Construct the list of all interesting pools.
4661 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4664 if (pool_list_count(list
) == 0 && argc
!= 0) {
4665 pool_list_free(list
);
4669 if (pool_list_count(list
) == 0 && interval
== 0) {
4670 pool_list_free(list
);
4671 (void) fprintf(stderr
, gettext("no pools available\n"));
4675 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
4676 pool_list_free(list
);
4677 (void) fprintf(stderr
,
4678 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4683 if (l_histo
&& rq_histo
) {
4684 pool_list_free(list
);
4685 (void) fprintf(stderr
,
4686 gettext("Only one of [-r|-w] can be passed at a time\n"));
4692 * Enter the main iostat loop.
4698 * Histograms tables look out of place when you try to display
4699 * them with the other stats, so make a rule that you can only
4700 * print histograms by themselves.
4702 cb
.cb_flags
= IOS_L_HISTO_M
;
4703 } else if (rq_histo
) {
4704 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4706 cb
.cb_flags
= IOS_DEFAULT_M
;
4708 cb
.cb_flags
|= IOS_LATENCY_M
;
4710 cb
.cb_flags
|= IOS_QUEUES_M
;
4714 * See if the module supports all the stats we want to display.
4716 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4717 if (unsupported_flags
) {
4721 gettext("The loaded zfs module doesn't support:"));
4723 /* for each bit set in unsupported_flags */
4724 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4725 idx
= lowbit64(f
) - 1;
4726 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4729 fprintf(stderr
, ". Try running a newer module.\n");
4730 pool_list_free(list
);
4736 if ((npools
= pool_list_count(list
)) == 0)
4737 (void) fprintf(stderr
, gettext("no pools available\n"));
4740 * If this is the first iteration and -y was supplied
4741 * we skip any printing.
4743 boolean_t skip
= (omit_since_boot
&&
4744 cb
.cb_iteration
== 0);
4747 * Refresh all statistics. This is done as an
4748 * explicit step before calculating the maximum name
4749 * width, so that any * configuration changes are
4750 * properly accounted for.
4752 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4756 * Iterate over all pools to determine the maximum width
4757 * for the pool / device name column across all pools.
4759 cb
.cb_namewidth
= 0;
4760 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4763 if (timestamp_fmt
!= NODATE
)
4764 print_timestamp(timestamp_fmt
);
4766 if (cmd
!= NULL
&& cb
.cb_verbose
&&
4767 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
4768 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4769 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
4770 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
4776 * If it's the first time and we're not skipping it,
4777 * or either skip or verbose mode, print the header.
4779 * The histogram code explicitly prints its header on
4780 * every vdev, so skip this for histograms.
4782 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4783 (skip
!= verbose
)) &&
4784 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4786 print_iostat_header(&cb
);
4789 (void) fsleep(interval
);
4794 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4797 * If there's more than one pool, and we're not in
4798 * verbose mode (which prints a separator for us),
4799 * then print a separator.
4801 * In addition, if we're printing specific vdevs then
4802 * we also want an ending separator.
4804 if (((npools
> 1 && !verbose
&&
4805 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4806 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4807 cb
.cb_vdev_names_count
)) &&
4809 print_iostat_separator(&cb
);
4810 if (cb
.vcdl
!= NULL
)
4811 print_cmd_columns(cb
.vcdl
, 1);
4815 if (cb
.vcdl
!= NULL
)
4816 free_vdev_cmd_data_list(cb
.vcdl
);
4821 * Flush the output so that redirection to a file isn't buffered
4824 (void) fflush(stdout
);
4829 if (count
!= 0 && --count
== 0)
4832 (void) fsleep(interval
);
4835 pool_list_free(list
);
4840 typedef struct list_cbdata
{
4841 boolean_t cb_verbose
;
4844 boolean_t cb_scripted
;
4845 zprop_list_t
*cb_proplist
;
4846 boolean_t cb_literal
;
4850 * Given a list of columns to display, output appropriate headers for each one.
4853 print_header(list_cbdata_t
*cb
)
4855 zprop_list_t
*pl
= cb
->cb_proplist
;
4856 char headerbuf
[ZPOOL_MAXPROPLEN
];
4858 boolean_t first
= B_TRUE
;
4859 boolean_t right_justify
;
4862 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4863 width
= pl
->pl_width
;
4864 if (first
&& cb
->cb_verbose
) {
4866 * Reset the width to accommodate the verbose listing
4869 width
= cb
->cb_namewidth
;
4877 right_justify
= B_FALSE
;
4878 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4879 header
= zpool_prop_column_name(pl
->pl_prop
);
4880 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4884 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4885 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4886 headerbuf
[i
] = '\0';
4890 if (pl
->pl_next
== NULL
&& !right_justify
)
4891 (void) printf("%s", header
);
4892 else if (right_justify
)
4893 (void) printf("%*s", (int)width
, header
);
4895 (void) printf("%-*s", (int)width
, header
);
4898 (void) printf("\n");
4902 * Given a pool and a list of properties, print out all the properties according
4903 * to the described layout.
4906 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4908 zprop_list_t
*pl
= cb
->cb_proplist
;
4909 boolean_t first
= B_TRUE
;
4910 char property
[ZPOOL_MAXPROPLEN
];
4912 boolean_t right_justify
;
4915 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4917 width
= pl
->pl_width
;
4918 if (first
&& cb
->cb_verbose
) {
4920 * Reset the width to accommodate the verbose listing
4923 width
= cb
->cb_namewidth
;
4927 if (cb
->cb_scripted
)
4928 (void) printf("\t");
4935 right_justify
= B_FALSE
;
4936 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4937 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
4938 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
4943 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4944 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
4945 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
4946 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
4947 sizeof (property
)) == 0) {
4955 * If this is being called in scripted mode, or if this is the
4956 * last column and it is left-justified, don't include a width
4959 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
4960 (void) printf("%s", propstr
);
4961 else if (right_justify
)
4962 (void) printf("%*s", (int)width
, propstr
);
4964 (void) printf("%-*s", (int)width
, propstr
);
4967 (void) printf("\n");
4971 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
4972 boolean_t valid
, enum zfs_nicenum_format format
)
4976 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
4979 case ZPOOL_PROP_EXPANDSZ
:
4981 (void) strlcpy(propval
, "-", sizeof (propval
));
4983 zfs_nicenum_format(value
, propval
, sizeof (propval
),
4986 case ZPOOL_PROP_FRAGMENTATION
:
4987 if (value
== ZFS_FRAG_INVALID
) {
4988 (void) strlcpy(propval
, "-", sizeof (propval
));
4989 } else if (format
== ZFS_NICENUM_RAW
) {
4990 (void) snprintf(propval
, sizeof (propval
), "%llu",
4991 (unsigned long long)value
);
4993 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4994 (unsigned long long)value
);
4997 case ZPOOL_PROP_CAPACITY
:
4998 if (format
== ZFS_NICENUM_RAW
)
4999 (void) snprintf(propval
, sizeof (propval
), "%llu",
5000 (unsigned long long)value
);
5002 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5003 (unsigned long long)value
);
5006 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5010 (void) strlcpy(propval
, "-", sizeof (propval
));
5013 (void) printf("\t%s", propval
);
5015 (void) printf(" %*s", (int)width
, propval
);
5019 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5020 list_cbdata_t
*cb
, int depth
)
5026 boolean_t scripted
= cb
->cb_scripted
;
5027 uint64_t islog
= B_FALSE
;
5028 boolean_t haslog
= B_FALSE
;
5029 char *dashes
= "%-*s - - - - - -\n";
5031 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5032 (uint64_t **)&vs
, &c
) == 0);
5035 boolean_t toplevel
= (vs
->vs_space
!= 0);
5037 enum zfs_nicenum_format format
;
5040 format
= ZFS_NICENUM_RAW
;
5042 format
= ZFS_NICENUM_1024
;
5044 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5048 (void) printf("\t%s", name
);
5049 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5050 (void) printf("%*s%s", depth
, "", name
);
5052 (void) printf("%*s%s%*s", depth
, "", name
,
5053 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5056 * Print the properties for the individual vdevs. Some
5057 * properties are only applicable to toplevel vdevs. The
5058 * 'toplevel' boolean value is passed to the print_one_column()
5059 * to indicate that the value is valid.
5061 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
5063 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
5065 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
5066 scripted
, toplevel
, format
);
5067 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
5069 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
5070 vs
->vs_fragmentation
, scripted
,
5071 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
5073 cap
= (vs
->vs_space
== 0) ? 0 :
5074 (vs
->vs_alloc
* 100 / vs
->vs_space
);
5075 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
5077 (void) printf("\n");
5080 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5081 &child
, &children
) != 0)
5084 for (c
= 0; c
< children
; c
++) {
5085 uint64_t ishole
= B_FALSE
;
5087 if (nvlist_lookup_uint64(child
[c
],
5088 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5091 if (nvlist_lookup_uint64(child
[c
],
5092 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
5097 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5099 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5103 if (haslog
== B_TRUE
) {
5104 /* LINTED E_SEC_PRINTF_VAR_FMT */
5105 (void) printf(dashes
, cb
->cb_namewidth
, "log");
5106 for (c
= 0; c
< children
; c
++) {
5107 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
5108 &islog
) != 0 || !islog
)
5110 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5112 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5117 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
5118 &child
, &children
) == 0 && children
> 0) {
5119 /* LINTED E_SEC_PRINTF_VAR_FMT */
5120 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
5121 for (c
= 0; c
< children
; c
++) {
5122 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5124 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5129 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
5130 &children
) == 0 && children
> 0) {
5131 /* LINTED E_SEC_PRINTF_VAR_FMT */
5132 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5133 for (c
= 0; c
< children
; c
++) {
5134 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5136 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5144 * Generic callback function to list a pool.
5147 list_callback(zpool_handle_t
*zhp
, void *data
)
5149 list_cbdata_t
*cbp
= data
;
5153 config
= zpool_get_config(zhp
, NULL
);
5155 print_pool(zhp
, cbp
);
5156 if (!cbp
->cb_verbose
)
5159 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5161 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
5167 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5169 * -g Display guid for individual vdev name.
5170 * -H Scripted mode. Don't display headers, and separate properties
5172 * -L Follow links when resolving vdev path name.
5173 * -o List of properties to display. Defaults to
5174 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5175 * "dedupratio,health,altroot"
5176 * -p Display values in parsable (exact) format.
5177 * -P Display full path for vdev name.
5178 * -T Display a timestamp in date(1) or Unix format
5180 * List all pools in the system, whether or not they're healthy. Output space
5181 * statistics for each one, as well as health status summary.
5184 zpool_do_list(int argc
, char **argv
)
5188 list_cbdata_t cb
= { 0 };
5189 static char default_props
[] =
5190 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5191 "dedupratio,health,altroot";
5192 char *props
= default_props
;
5194 unsigned long count
= 0;
5196 boolean_t first
= B_TRUE
;
5199 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
5202 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5205 cb
.cb_scripted
= B_TRUE
;
5208 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5214 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5217 cb
.cb_literal
= B_TRUE
;
5220 get_timestamp_arg(*optarg
);
5223 cb
.cb_verbose
= B_TRUE
;
5226 (void) fprintf(stderr
, gettext("missing argument for "
5227 "'%c' option\n"), optopt
);
5231 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5240 get_interval_count(&argc
, argv
, &interval
, &count
);
5242 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5246 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
5250 if (pool_list_count(list
) == 0)
5253 if (timestamp_fmt
!= NODATE
)
5254 print_timestamp(timestamp_fmt
);
5256 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
5260 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
5265 if (count
!= 0 && --count
== 0)
5268 pool_list_free(list
);
5269 (void) fsleep(interval
);
5272 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
5273 (void) printf(gettext("no pools available\n"));
5277 pool_list_free(list
);
5278 zprop_free_list(cb
.cb_proplist
);
5283 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
5285 boolean_t force
= B_FALSE
;
5288 char *poolname
, *old_disk
, *new_disk
;
5289 zpool_handle_t
*zhp
;
5290 nvlist_t
*props
= NULL
;
5295 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
5301 if ((propval
= strchr(optarg
, '=')) == NULL
) {
5302 (void) fprintf(stderr
, gettext("missing "
5303 "'=' for -o option\n"));
5309 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
5310 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
5314 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5323 /* get pool name and check number of arguments */
5325 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5332 (void) fprintf(stderr
,
5333 gettext("missing <device> specification\n"));
5341 (void) fprintf(stderr
,
5342 gettext("missing <new_device> specification\n"));
5345 new_disk
= old_disk
;
5355 (void) fprintf(stderr
, gettext("too many arguments\n"));
5359 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
5364 if (zpool_get_config(zhp
, NULL
) == NULL
) {
5365 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
5372 /* unless manually specified use "ashift" pool property (if set) */
5373 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
5376 char strval
[ZPOOL_MAXPROPLEN
];
5378 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
5379 if (src
!= ZPROP_SRC_DEFAULT
) {
5380 (void) sprintf(strval
, "%" PRId32
, intval
);
5381 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
5382 &props
, B_TRUE
) == 0);
5386 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
5388 if (nvroot
== NULL
) {
5394 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
5397 nvlist_free(nvroot
);
5404 * zpool replace [-f] <pool> <device> <new_device>
5406 * -f Force attach, even if <new_device> appears to be in use.
5408 * Replace <device> with <new_device>.
5412 zpool_do_replace(int argc
, char **argv
)
5414 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
5418 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5420 * -f Force attach, even if <new_device> appears to be in use.
5421 * -o Set property=value.
5423 * Attach <new_device> to the mirror containing <device>. If <device> is not
5424 * part of a mirror, then <device> will be transformed into a mirror of
5425 * <device> and <new_device>. In either case, <new_device> will begin life
5426 * with a DTL of [0, now], and will immediately begin to resilver itself.
5429 zpool_do_attach(int argc
, char **argv
)
5431 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
5435 * zpool detach [-f] <pool> <device>
5437 * -f Force detach of <device>, even if DTLs argue against it
5438 * (not supported yet)
5440 * Detach a device from a mirror. The operation will be refused if <device>
5441 * is the last device in the mirror, or if the DTLs indicate that this device
5442 * has the only valid copy of some data.
5446 zpool_do_detach(int argc
, char **argv
)
5449 char *poolname
, *path
;
5450 zpool_handle_t
*zhp
;
5454 while ((c
= getopt(argc
, argv
, "f")) != -1) {
5458 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5467 /* get pool name and check number of arguments */
5469 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5474 (void) fprintf(stderr
,
5475 gettext("missing <device> specification\n"));
5482 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5485 ret
= zpool_vdev_detach(zhp
, path
);
5493 * zpool split [-gLnP] [-o prop=val] ...
5495 * [-R altroot] <pool> <newpool> [<device> ...]
5497 * -g Display guid for individual vdev name.
5498 * -L Follow links when resolving vdev path name.
5499 * -n Do not split the pool, but display the resulting layout if
5500 * it were to be split.
5501 * -o Set property=value, or set mount options.
5502 * -P Display full path for vdev name.
5503 * -R Mount the split-off pool under an alternate root.
5504 * -l Load encryption keys while importing.
5506 * Splits the named pool and gives it the new pool name. Devices to be split
5507 * off may be listed, provided that no more than one device is specified
5508 * per top-level vdev mirror. The newly split pool is left in an exported
5509 * state unless -R is specified.
5511 * Restrictions: the top-level of the pool pool must only be made up of
5512 * mirrors; all devices in the pool must be healthy; no device may be
5513 * undergoing a resilvering operation.
5516 zpool_do_split(int argc
, char **argv
)
5518 char *srcpool
, *newpool
, *propval
;
5519 char *mntopts
= NULL
;
5522 boolean_t loadkeys
= B_FALSE
;
5523 zpool_handle_t
*zhp
;
5524 nvlist_t
*config
, *props
= NULL
;
5526 flags
.dryrun
= B_FALSE
;
5527 flags
.import
= B_FALSE
;
5528 flags
.name_flags
= 0;
5531 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
5534 flags
.name_flags
|= VDEV_NAME_GUID
;
5537 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5540 flags
.import
= B_TRUE
;
5542 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5543 &props
, B_TRUE
) != 0) {
5552 flags
.dryrun
= B_TRUE
;
5555 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5558 if (add_prop_list(optarg
, propval
,
5559 &props
, B_TRUE
) != 0) {
5568 flags
.name_flags
|= VDEV_NAME_PATH
;
5571 (void) fprintf(stderr
, gettext("missing argument for "
5572 "'%c' option\n"), optopt
);
5576 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5583 if (!flags
.import
&& mntopts
!= NULL
) {
5584 (void) fprintf(stderr
, gettext("setting mntopts is only "
5585 "valid when importing the pool\n"));
5589 if (!flags
.import
&& loadkeys
) {
5590 (void) fprintf(stderr
, gettext("loading keys is only "
5591 "valid when importing the pool\n"));
5599 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5603 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5613 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5618 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5619 if (config
== NULL
) {
5623 (void) printf(gettext("would create '%s' with the "
5624 "following layout:\n\n"), newpool
);
5625 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5632 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5633 nvlist_free(config
);
5639 * The split was successful. Now we need to open the new
5640 * pool and import it.
5642 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5643 nvlist_free(config
);
5649 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
5654 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5655 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5657 (void) fprintf(stderr
, gettext("Split was successful, but "
5658 "the datasets could not all be mounted\n"));
5659 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5660 "different altroot\n"), "zpool import");
5663 nvlist_free(config
);
5672 * zpool online <pool> <device> ...
5675 zpool_do_online(int argc
, char **argv
)
5679 zpool_handle_t
*zhp
;
5681 vdev_state_t newstate
;
5685 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5688 flags
|= ZFS_ONLINE_EXPAND
;
5692 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5701 /* get pool name and check number of arguments */
5703 (void) fprintf(stderr
, gettext("missing pool name\n"));
5707 (void) fprintf(stderr
, gettext("missing device name\n"));
5713 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5716 for (i
= 1; i
< argc
; i
++) {
5717 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5718 if (newstate
!= VDEV_STATE_HEALTHY
) {
5719 (void) printf(gettext("warning: device '%s' "
5720 "onlined, but remains in faulted state\n"),
5722 if (newstate
== VDEV_STATE_FAULTED
)
5723 (void) printf(gettext("use 'zpool "
5724 "clear' to restore a faulted "
5727 (void) printf(gettext("use 'zpool "
5728 "replace' to replace devices "
5729 "that are no longer present\n"));
5742 * zpool offline [-ft] <pool> <device> ...
5744 * -f Force the device into a faulted state.
5746 * -t Only take the device off-line temporarily. The offline/faulted
5747 * state will not be persistent across reboots.
5751 zpool_do_offline(int argc
, char **argv
)
5755 zpool_handle_t
*zhp
;
5757 boolean_t istmp
= B_FALSE
;
5758 boolean_t fault
= B_FALSE
;
5761 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5770 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5779 /* get pool name and check number of arguments */
5781 (void) fprintf(stderr
, gettext("missing pool name\n"));
5785 (void) fprintf(stderr
, gettext("missing device name\n"));
5791 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5794 for (i
= 1; i
< argc
; i
++) {
5796 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
5798 if (istmp
== B_FALSE
) {
5799 /* Force the fault to persist across imports */
5800 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
5802 aux
= VDEV_AUX_EXTERNAL
;
5805 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
5808 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5819 * zpool clear <pool> [device]
5821 * Clear all errors associated with a pool or a particular device.
5824 zpool_do_clear(int argc
, char **argv
)
5828 boolean_t dryrun
= B_FALSE
;
5829 boolean_t do_rewind
= B_FALSE
;
5830 boolean_t xtreme_rewind
= B_FALSE
;
5831 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5832 nvlist_t
*policy
= NULL
;
5833 zpool_handle_t
*zhp
;
5834 char *pool
, *device
;
5837 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5846 xtreme_rewind
= B_TRUE
;
5849 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5859 (void) fprintf(stderr
, gettext("missing pool name\n"));
5864 (void) fprintf(stderr
, gettext("too many arguments\n"));
5868 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5869 (void) fprintf(stderr
,
5870 gettext("-n or -X only meaningful with -F\n"));
5874 rewind_policy
= ZPOOL_TRY_REWIND
;
5876 rewind_policy
= ZPOOL_DO_REWIND
;
5878 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5880 /* In future, further rewind policy choices can be passed along here */
5881 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5882 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
5883 rewind_policy
) != 0) {
5888 device
= argc
== 2 ? argv
[1] : NULL
;
5890 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5891 nvlist_free(policy
);
5895 if (zpool_clear(zhp
, device
, policy
) != 0)
5900 nvlist_free(policy
);
5906 * zpool reguid <pool>
5909 zpool_do_reguid(int argc
, char **argv
)
5913 zpool_handle_t
*zhp
;
5917 while ((c
= getopt(argc
, argv
, "")) != -1) {
5920 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5929 /* get pool name and check number of arguments */
5931 (void) fprintf(stderr
, gettext("missing pool name\n"));
5936 (void) fprintf(stderr
, gettext("too many arguments\n"));
5941 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5944 ret
= zpool_reguid(zhp
);
5952 * zpool reopen <pool>
5954 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5957 zpool_do_reopen(int argc
, char **argv
)
5961 boolean_t scrub_restart
= B_TRUE
;
5964 while ((c
= getopt(argc
, argv
, "n")) != -1) {
5967 scrub_restart
= B_FALSE
;
5970 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5979 /* if argc == 0 we will execute zpool_reopen_one on all pools */
5980 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_reopen_one
,
5986 typedef struct scrub_cbdata
{
5990 pool_scrub_cmd_t cb_scrub_cmd
;
5994 scrub_callback(zpool_handle_t
*zhp
, void *data
)
5996 scrub_cbdata_t
*cb
= data
;
6000 * Ignore faulted pools.
6002 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
6003 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
6004 "currently unavailable\n"), zpool_get_name(zhp
));
6008 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
6014 * zpool scrub [-s | -p] <pool> ...
6016 * -s Stop. Stops any in-progress scrub.
6017 * -p Pause. Pause in-progress scrub.
6020 zpool_do_scrub(int argc
, char **argv
)
6025 cb
.cb_type
= POOL_SCAN_SCRUB
;
6026 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
6029 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
6032 cb
.cb_type
= POOL_SCAN_NONE
;
6035 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
6038 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6044 if (cb
.cb_type
== POOL_SCAN_NONE
&&
6045 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
6046 (void) fprintf(stderr
, gettext("invalid option combination: "
6047 "-s and -p are mutually exclusive\n"));
6057 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6061 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
6065 * Print out detailed scrub status.
6068 print_scan_status(pool_scan_stat_t
*ps
)
6070 time_t start
, end
, pause
;
6071 uint64_t total_secs_left
;
6072 uint64_t elapsed
, secs_left
, mins_left
, hours_left
, days_left
;
6073 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
6074 uint64_t scan_rate
, issue_rate
;
6075 double fraction_done
;
6076 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
6077 char srate_buf
[7], irate_buf
[7];
6079 (void) printf(gettext(" scan: "));
6081 /* If there's never been a scan, there's not much to say. */
6082 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
6083 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
6084 (void) printf(gettext("none requested\n"));
6088 start
= ps
->pss_start_time
;
6089 end
= ps
->pss_end_time
;
6090 pause
= ps
->pss_pass_scrub_pause
;
6092 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
6094 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
6095 ps
->pss_func
== POOL_SCAN_RESILVER
);
6097 /* Scan is finished or canceled. */
6098 if (ps
->pss_state
== DSS_FINISHED
) {
6099 total_secs_left
= end
- start
;
6100 days_left
= total_secs_left
/ 60 / 60 / 24;
6101 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6102 mins_left
= (total_secs_left
/ 60) % 60;
6103 secs_left
= (total_secs_left
% 60);
6105 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6106 (void) printf(gettext("scrub repaired %s "
6107 "in %llu days %02llu:%02llu:%02llu "
6108 "with %llu errors on %s"), processed_buf
,
6109 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6110 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6111 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6112 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6113 (void) printf(gettext("resilvered %s "
6114 "in %llu days %02llu:%02llu:%02llu "
6115 "with %llu errors on %s"), processed_buf
,
6116 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6117 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6118 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6121 } else if (ps
->pss_state
== DSS_CANCELED
) {
6122 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6123 (void) printf(gettext("scrub canceled on %s"),
6125 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6126 (void) printf(gettext("resilver canceled on %s"),
6132 assert(ps
->pss_state
== DSS_SCANNING
);
6134 /* Scan is in progress. Resilvers can't be paused. */
6135 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6137 (void) printf(gettext("scrub in progress since %s"),
6140 (void) printf(gettext("scrub paused since %s"),
6142 (void) printf(gettext("\tscrub started on %s"),
6145 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6146 (void) printf(gettext("resilver in progress since %s"),
6150 scanned
= ps
->pss_examined
;
6151 pass_scanned
= ps
->pss_pass_exam
;
6152 issued
= ps
->pss_issued
;
6153 pass_issued
= ps
->pss_pass_issued
;
6154 total
= ps
->pss_to_examine
;
6156 /* we are only done with a block once we have issued the IO for it */
6157 fraction_done
= (double)issued
/ total
;
6159 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6160 elapsed
= time(NULL
) - ps
->pss_pass_start
;
6161 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
6162 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
6164 scan_rate
= pass_scanned
/ elapsed
;
6165 issue_rate
= pass_issued
/ elapsed
;
6166 total_secs_left
= (issue_rate
!= 0) ?
6167 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
6169 days_left
= total_secs_left
/ 60 / 60 / 24;
6170 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6171 mins_left
= (total_secs_left
/ 60) % 60;
6172 secs_left
= (total_secs_left
% 60);
6174 /* format all of the numbers we will be reporting */
6175 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
6176 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
6177 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
6178 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
6179 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
6181 /* do not print estimated time if we have a paused scrub */
6183 (void) printf(gettext("\t%s scanned at %s/s, "
6184 "%s issued at %s/s, %s total\n"),
6185 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
6187 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6188 scanned_buf
, issued_buf
, total_buf
);
6191 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6192 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6193 processed_buf
, 100 * fraction_done
);
6194 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6195 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6196 processed_buf
, 100 * fraction_done
);
6200 if (issue_rate
>= 10 * 1024 * 1024) {
6201 (void) printf(gettext(", %llu days "
6202 "%02llu:%02llu:%02llu to go\n"),
6203 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6204 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
);
6206 (void) printf(gettext(", no estimated "
6207 "completion time\n"));
6210 (void) printf(gettext("\n"));
6215 * Print out detailed removal status.
6218 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
6220 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
6222 nvlist_t
*config
, *nvroot
;
6227 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
6231 * Determine name of vdev.
6233 config
= zpool_get_config(zhp
, NULL
);
6234 nvroot
= fnvlist_lookup_nvlist(config
,
6235 ZPOOL_CONFIG_VDEV_TREE
);
6236 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
6237 &child
, &children
) == 0);
6238 assert(prs
->prs_removing_vdev
< children
);
6239 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
6240 child
[prs
->prs_removing_vdev
], B_TRUE
);
6242 (void) printf(gettext("remove: "));
6244 start
= prs
->prs_start_time
;
6245 end
= prs
->prs_end_time
;
6246 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
6249 * Removal is finished or canceled.
6251 if (prs
->prs_state
== DSS_FINISHED
) {
6252 uint64_t minutes_taken
= (end
- start
) / 60;
6254 (void) printf(gettext("Removal of vdev %llu copied %s "
6255 "in %lluh%um, completed on %s"),
6256 (longlong_t
)prs
->prs_removing_vdev
,
6258 (u_longlong_t
)(minutes_taken
/ 60),
6259 (uint_t
)(minutes_taken
% 60),
6260 ctime((time_t *)&end
));
6261 } else if (prs
->prs_state
== DSS_CANCELED
) {
6262 (void) printf(gettext("Removal of %s canceled on %s"),
6263 vdev_name
, ctime(&end
));
6265 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
6266 double fraction_done
;
6269 assert(prs
->prs_state
== DSS_SCANNING
);
6272 * Removal is in progress.
6274 (void) printf(gettext(
6275 "Evacuation of %s in progress since %s"),
6276 vdev_name
, ctime(&start
));
6278 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
6279 total
= prs
->prs_to_copy
;
6280 fraction_done
= (double)copied
/ total
;
6282 /* elapsed time for this pass */
6283 elapsed
= time(NULL
) - prs
->prs_start_time
;
6284 elapsed
= elapsed
> 0 ? elapsed
: 1;
6285 rate
= copied
/ elapsed
;
6286 rate
= rate
> 0 ? rate
: 1;
6287 mins_left
= ((total
- copied
) / rate
) / 60;
6288 hours_left
= mins_left
/ 60;
6290 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
6291 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
6292 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
6295 * do not print estimated time if hours_left is more than
6298 (void) printf(gettext(" %s copied out of %s at %s/s, "
6300 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
6301 if (hours_left
< (30 * 24)) {
6302 (void) printf(gettext(", %lluh%um to go\n"),
6303 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
6305 (void) printf(gettext(
6306 ", (copy is slow, no estimated time)\n"));
6310 if (prs
->prs_mapping_memory
> 0) {
6312 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
6313 (void) printf(gettext(" %s memory used for "
6314 "removed device mappings\n"),
6320 print_error_log(zpool_handle_t
*zhp
)
6322 nvlist_t
*nverrlist
= NULL
;
6325 size_t len
= MAXPATHLEN
* 2;
6327 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
6330 (void) printf("errors: Permanent errors have been "
6331 "detected in the following files:\n\n");
6333 pathname
= safe_malloc(len
);
6335 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
6337 uint64_t dsobj
, obj
;
6339 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
6340 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
6342 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
6344 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
6345 (void) printf("%7s %s\n", "", pathname
);
6348 nvlist_free(nverrlist
);
6352 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
6361 (void) printf(gettext("\tspares\n"));
6363 for (i
= 0; i
< nspares
; i
++) {
6364 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
6366 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
6372 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
6381 (void) printf(gettext("\tcache\n"));
6383 for (i
= 0; i
< nl2cache
; i
++) {
6384 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
6386 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
6392 print_dedup_stats(nvlist_t
*config
)
6394 ddt_histogram_t
*ddh
;
6398 char dspace
[6], mspace
[6];
6401 * If the pool was faulted then we may not have been able to
6402 * obtain the config. Otherwise, if we have anything in the dedup
6403 * table continue processing the stats.
6405 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
6406 (uint64_t **)&ddo
, &c
) != 0)
6409 (void) printf("\n");
6410 (void) printf(gettext(" dedup: "));
6411 if (ddo
->ddo_count
== 0) {
6412 (void) printf(gettext("no DDT entries\n"));
6416 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
6417 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
6418 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6419 (u_longlong_t
)ddo
->ddo_count
,
6423 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
6424 (uint64_t **)&dds
, &c
) == 0);
6425 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
6426 (uint64_t **)&ddh
, &c
) == 0);
6427 zpool_dump_ddt(dds
, ddh
);
6431 * Display a summary of pool status. Displays a summary such as:
6435 * reason: One or more devices ...
6436 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6442 * When given the '-v' option, we print out the complete config. If the '-e'
6443 * option is specified, then we print out error rate information as well.
6446 status_callback(zpool_handle_t
*zhp
, void *data
)
6448 status_cbdata_t
*cbp
= data
;
6449 nvlist_t
*config
, *nvroot
;
6451 zpool_status_t reason
;
6452 zpool_errata_t errata
;
6457 config
= zpool_get_config(zhp
, NULL
);
6458 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
6463 * If we were given 'zpool status -x', only report those pools with
6466 if (cbp
->cb_explain
&&
6467 (reason
== ZPOOL_STATUS_OK
||
6468 reason
== ZPOOL_STATUS_VERSION_OLDER
||
6469 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
6470 if (!cbp
->cb_allpools
) {
6471 (void) printf(gettext("pool '%s' is healthy\n"),
6472 zpool_get_name(zhp
));
6474 cbp
->cb_first
= B_FALSE
;
6480 cbp
->cb_first
= B_FALSE
;
6482 (void) printf("\n");
6484 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6485 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
6486 (uint64_t **)&vs
, &c
) == 0);
6487 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6489 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
6490 (void) printf(gettext(" state: %s\n"), health
);
6493 case ZPOOL_STATUS_MISSING_DEV_R
:
6494 (void) printf(gettext("status: One or more devices could not "
6495 "be opened. Sufficient replicas exist for\n\tthe pool to "
6496 "continue functioning in a degraded state.\n"));
6497 (void) printf(gettext("action: Attach the missing device and "
6498 "online it using 'zpool online'.\n"));
6501 case ZPOOL_STATUS_MISSING_DEV_NR
:
6502 (void) printf(gettext("status: One or more devices could not "
6503 "be opened. There are insufficient\n\treplicas for the "
6504 "pool to continue functioning.\n"));
6505 (void) printf(gettext("action: Attach the missing device and "
6506 "online it using 'zpool online'.\n"));
6509 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
6510 (void) printf(gettext("status: One or more devices could not "
6511 "be used because the label is missing or\n\tinvalid. "
6512 "Sufficient replicas exist for the pool to continue\n\t"
6513 "functioning in a degraded state.\n"));
6514 (void) printf(gettext("action: Replace the device using "
6515 "'zpool replace'.\n"));
6518 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
6519 (void) printf(gettext("status: One or more devices could not "
6520 "be used because the label is missing \n\tor invalid. "
6521 "There are insufficient replicas for the pool to "
6522 "continue\n\tfunctioning.\n"));
6523 zpool_explain_recover(zpool_get_handle(zhp
),
6524 zpool_get_name(zhp
), reason
, config
);
6527 case ZPOOL_STATUS_FAILING_DEV
:
6528 (void) printf(gettext("status: One or more devices has "
6529 "experienced an unrecoverable error. An\n\tattempt was "
6530 "made to correct the error. Applications are "
6532 (void) printf(gettext("action: Determine if the device needs "
6533 "to be replaced, and clear the errors\n\tusing "
6534 "'zpool clear' or replace the device with 'zpool "
6538 case ZPOOL_STATUS_OFFLINE_DEV
:
6539 (void) printf(gettext("status: One or more devices has "
6540 "been taken offline by the administrator.\n\tSufficient "
6541 "replicas exist for the pool to continue functioning in "
6542 "a\n\tdegraded state.\n"));
6543 (void) printf(gettext("action: Online the device using "
6544 "'zpool online' or replace the device with\n\t'zpool "
6548 case ZPOOL_STATUS_REMOVED_DEV
:
6549 (void) printf(gettext("status: One or more devices has "
6550 "been removed by the administrator.\n\tSufficient "
6551 "replicas exist for the pool to continue functioning in "
6552 "a\n\tdegraded state.\n"));
6553 (void) printf(gettext("action: Online the device using "
6554 "'zpool online' or replace the device with\n\t'zpool "
6558 case ZPOOL_STATUS_RESILVERING
:
6559 (void) printf(gettext("status: One or more devices is "
6560 "currently being resilvered. The pool will\n\tcontinue "
6561 "to function, possibly in a degraded state.\n"));
6562 (void) printf(gettext("action: Wait for the resilver to "
6566 case ZPOOL_STATUS_CORRUPT_DATA
:
6567 (void) printf(gettext("status: One or more devices has "
6568 "experienced an error resulting in data\n\tcorruption. "
6569 "Applications may be affected.\n"));
6570 (void) printf(gettext("action: Restore the file in question "
6571 "if possible. Otherwise restore the\n\tentire pool from "
6575 case ZPOOL_STATUS_CORRUPT_POOL
:
6576 (void) printf(gettext("status: The pool metadata is corrupted "
6577 "and the pool cannot be opened.\n"));
6578 zpool_explain_recover(zpool_get_handle(zhp
),
6579 zpool_get_name(zhp
), reason
, config
);
6582 case ZPOOL_STATUS_VERSION_OLDER
:
6583 (void) printf(gettext("status: The pool is formatted using a "
6584 "legacy on-disk format. The pool can\n\tstill be used, "
6585 "but some features are unavailable.\n"));
6586 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6587 "upgrade'. Once this is done, the\n\tpool will no longer "
6588 "be accessible on software that does not support\n\t"
6589 "feature flags.\n"));
6592 case ZPOOL_STATUS_VERSION_NEWER
:
6593 (void) printf(gettext("status: The pool has been upgraded to a "
6594 "newer, incompatible on-disk version.\n\tThe pool cannot "
6595 "be accessed on this system.\n"));
6596 (void) printf(gettext("action: Access the pool from a system "
6597 "running more recent software, or\n\trestore the pool from "
6601 case ZPOOL_STATUS_FEAT_DISABLED
:
6602 (void) printf(gettext("status: Some supported features are not "
6603 "enabled on the pool. The pool can\n\tstill be used, but "
6604 "some features are unavailable.\n"));
6605 (void) printf(gettext("action: Enable all features using "
6606 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6607 "longer be accessible by software that does not support\n\t"
6608 "the features. See zpool-features(5) for details.\n"));
6611 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
6612 (void) printf(gettext("status: The pool cannot be accessed on "
6613 "this system because it uses the\n\tfollowing feature(s) "
6614 "not supported on this system:\n"));
6615 zpool_print_unsup_feat(config
);
6616 (void) printf("\n");
6617 (void) printf(gettext("action: Access the pool from a system "
6618 "that supports the required feature(s),\n\tor restore the "
6619 "pool from backup.\n"));
6622 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
6623 (void) printf(gettext("status: The pool can only be accessed "
6624 "in read-only mode on this system. It\n\tcannot be "
6625 "accessed in read-write mode because it uses the "
6626 "following\n\tfeature(s) not supported on this system:\n"));
6627 zpool_print_unsup_feat(config
);
6628 (void) printf("\n");
6629 (void) printf(gettext("action: The pool cannot be accessed in "
6630 "read-write mode. Import the pool with\n"
6631 "\t\"-o readonly=on\", access the pool from a system that "
6632 "supports the\n\trequired feature(s), or restore the "
6633 "pool from backup.\n"));
6636 case ZPOOL_STATUS_FAULTED_DEV_R
:
6637 (void) printf(gettext("status: One or more devices are "
6638 "faulted in response to persistent errors.\n\tSufficient "
6639 "replicas exist for the pool to continue functioning "
6640 "in a\n\tdegraded state.\n"));
6641 (void) printf(gettext("action: Replace the faulted device, "
6642 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6645 case ZPOOL_STATUS_FAULTED_DEV_NR
:
6646 (void) printf(gettext("status: One or more devices are "
6647 "faulted in response to persistent errors. There are "
6648 "insufficient replicas for the pool to\n\tcontinue "
6650 (void) printf(gettext("action: Destroy and re-create the pool "
6651 "from a backup source. Manually marking the device\n"
6652 "\trepaired using 'zpool clear' may allow some data "
6653 "to be recovered.\n"));
6656 case ZPOOL_STATUS_IO_FAILURE_MMP
:
6657 (void) printf(gettext("status: The pool is suspended because "
6658 "multihost writes failed or were delayed;\n\tanother "
6659 "system could import the pool undetected.\n"));
6660 (void) printf(gettext("action: Make sure the pool's devices "
6661 "are connected, then reboot your system and\n\timport the "
6665 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
6666 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
6667 (void) printf(gettext("status: One or more devices are "
6668 "faulted in response to IO failures.\n"));
6669 (void) printf(gettext("action: Make sure the affected devices "
6670 "are connected, then run 'zpool clear'.\n"));
6673 case ZPOOL_STATUS_BAD_LOG
:
6674 (void) printf(gettext("status: An intent log record "
6675 "could not be read.\n"
6676 "\tWaiting for administrator intervention to fix the "
6677 "faulted pool.\n"));
6678 (void) printf(gettext("action: Either restore the affected "
6679 "device(s) and run 'zpool online',\n"
6680 "\tor ignore the intent log records by running "
6681 "'zpool clear'.\n"));
6684 case ZPOOL_STATUS_HOSTID_MISMATCH
:
6685 (void) printf(gettext("status: Mismatch between pool hostid "
6686 "and system hostid on imported pool.\n\tThis pool was "
6687 "previously imported into a system with a different "
6688 "hostid,\n\tand then was verbatim imported into this "
6690 (void) printf(gettext("action: Export this pool on all systems "
6691 "on which it is imported.\n"
6692 "\tThen import it to correct the mismatch.\n"));
6695 case ZPOOL_STATUS_ERRATA
:
6696 (void) printf(gettext("status: Errata #%d detected.\n"),
6700 case ZPOOL_ERRATA_NONE
:
6703 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
6704 (void) printf(gettext("action: To correct the issue "
6705 "run 'zpool scrub'.\n"));
6708 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
6709 (void) printf(gettext("\tExisting encrypted datasets "
6710 "contain an on-disk incompatibility\n\twhich "
6711 "needs to be corrected.\n"));
6712 (void) printf(gettext("action: To correct the issue "
6713 "backup existing encrypted datasets to new\n\t"
6714 "encrypted datasets and destroy the old ones. "
6715 "'zfs mount -o ro' can\n\tbe used to temporarily "
6716 "mount existing encrypted datasets readonly.\n"));
6721 * All errata which allow the pool to be imported
6722 * must contain an action message.
6730 * The remaining errors can't actually be generated, yet.
6732 assert(reason
== ZPOOL_STATUS_OK
);
6736 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6739 if (config
!= NULL
) {
6741 nvlist_t
**spares
, **l2cache
;
6742 uint_t nspares
, nl2cache
;
6743 pool_scan_stat_t
*ps
= NULL
;
6744 pool_removal_stat_t
*prs
= NULL
;
6746 (void) nvlist_lookup_uint64_array(nvroot
,
6747 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
6748 print_scan_status(ps
);
6750 (void) nvlist_lookup_uint64_array(nvroot
,
6751 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
6752 print_removal_status(zhp
, prs
);
6754 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
6755 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6756 if (cbp
->cb_namewidth
< 10)
6757 cbp
->cb_namewidth
= 10;
6759 (void) printf(gettext("config:\n\n"));
6760 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6761 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6764 if (cbp
->vcdl
!= NULL
)
6765 print_cmd_columns(cbp
->vcdl
, 0);
6768 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6771 if (num_logs(nvroot
) > 0)
6772 print_logs(zhp
, cbp
, nvroot
);
6773 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6774 &l2cache
, &nl2cache
) == 0)
6775 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6777 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6778 &spares
, &nspares
) == 0)
6779 print_spares(zhp
, cbp
, spares
, nspares
);
6781 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6783 nvlist_t
*nverrlist
= NULL
;
6786 * If the approximate error count is small, get a
6787 * precise count by fetching the entire log and
6788 * uniquifying the results.
6790 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6791 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6796 while ((elem
= nvlist_next_nvpair(nverrlist
,
6801 nvlist_free(nverrlist
);
6803 (void) printf("\n");
6806 (void) printf(gettext("errors: No known data "
6808 else if (!cbp
->cb_verbose
)
6809 (void) printf(gettext("errors: %llu data "
6810 "errors, use '-v' for a list\n"),
6811 (u_longlong_t
)nerr
);
6813 print_error_log(zhp
);
6816 if (cbp
->cb_dedup_stats
)
6817 print_dedup_stats(config
);
6819 (void) printf(gettext("config: The configuration cannot be "
6827 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6828 * [interval [count]]
6830 * -c CMD For each vdev, run command CMD
6831 * -g Display guid for individual vdev name.
6832 * -L Follow links when resolving vdev path name.
6833 * -P Display full path for vdev name.
6834 * -v Display complete error logs
6835 * -x Display only pools with potential problems
6836 * -D Display dedup status (undocumented)
6837 * -T Display a timestamp in date(1) or Unix format
6839 * Describes the health status of all pools or some subset.
6842 zpool_do_status(int argc
, char **argv
)
6847 unsigned long count
= 0;
6848 status_cbdata_t cb
= { 0 };
6852 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
6857 gettext("Can't set -c flag twice\n"));
6861 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
6862 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6863 fprintf(stderr
, gettext(
6864 "Can't run -c, disabled by "
6865 "ZPOOL_SCRIPTS_ENABLED.\n"));
6869 if ((getuid() <= 0 || geteuid() <= 0) &&
6870 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6871 fprintf(stderr
, gettext(
6872 "Can't run -c with root privileges "
6873 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6879 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6882 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6885 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6888 cb
.cb_verbose
= B_TRUE
;
6891 cb
.cb_explain
= B_TRUE
;
6894 cb
.cb_dedup_stats
= B_TRUE
;
6897 get_timestamp_arg(*optarg
);
6900 if (optopt
== 'c') {
6901 print_zpool_script_list("status");
6905 gettext("invalid option '%c'\n"), optopt
);
6914 get_interval_count(&argc
, argv
, &interval
, &count
);
6917 cb
.cb_allpools
= B_TRUE
;
6919 cb
.cb_first
= B_TRUE
;
6920 cb
.cb_print_status
= B_TRUE
;
6923 if (timestamp_fmt
!= NODATE
)
6924 print_timestamp(timestamp_fmt
);
6927 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
6930 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
6931 status_callback
, &cb
);
6933 if (cb
.vcdl
!= NULL
)
6934 free_vdev_cmd_data_list(cb
.vcdl
);
6936 if (argc
== 0 && cb
.cb_count
== 0)
6937 (void) fprintf(stderr
, gettext("no pools available\n"));
6938 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
6939 (void) printf(gettext("all pools are healthy\n"));
6947 if (count
!= 0 && --count
== 0)
6950 (void) fsleep(interval
);
6956 typedef struct upgrade_cbdata
{
6959 uint64_t cb_version
;
6964 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
6966 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
6967 int *count
= (int *)unsupp_fs
;
6969 if (zfs_version
> ZPL_VERSION
) {
6970 (void) printf(gettext("%s (v%d) is not supported by this "
6971 "implementation of ZFS.\n"),
6972 zfs_get_name(zhp
), zfs_version
);
6976 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
6984 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
6988 uint64_t oldversion
;
6991 config
= zpool_get_config(zhp
, NULL
);
6992 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6995 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
6996 assert(oldversion
< version
);
6998 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
7003 (void) fprintf(stderr
, gettext("Upgrade not performed due "
7004 "to %d unsupported filesystems (max v%d).\n"),
7005 unsupp_fs
, (int)ZPL_VERSION
);
7009 ret
= zpool_upgrade(zhp
, version
);
7013 if (version
>= SPA_VERSION_FEATURES
) {
7014 (void) printf(gettext("Successfully upgraded "
7015 "'%s' from version %llu to feature flags.\n"),
7016 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
7018 (void) printf(gettext("Successfully upgraded "
7019 "'%s' from version %llu to version %llu.\n"),
7020 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
7021 (u_longlong_t
)version
);
7028 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
7031 boolean_t firstff
= B_TRUE
;
7032 nvlist_t
*enabled
= zpool_get_features(zhp
);
7035 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7036 const char *fname
= spa_feature_table
[i
].fi_uname
;
7037 const char *fguid
= spa_feature_table
[i
].fi_guid
;
7038 if (!nvlist_exists(enabled
, fguid
)) {
7040 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
7041 ret
= zpool_set_prop(zhp
, propname
,
7042 ZFS_FEATURE_ENABLED
);
7050 (void) printf(gettext("Enabled the "
7051 "following features on '%s':\n"),
7052 zpool_get_name(zhp
));
7055 (void) printf(gettext(" %s\n"), fname
);
7066 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
7068 upgrade_cbdata_t
*cbp
= arg
;
7071 boolean_t printnl
= B_FALSE
;
7074 config
= zpool_get_config(zhp
, NULL
);
7075 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7078 assert(SPA_VERSION_IS_SUPPORTED(version
));
7080 if (version
< cbp
->cb_version
) {
7081 cbp
->cb_first
= B_FALSE
;
7082 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7088 * If they did "zpool upgrade -a", then we could
7089 * be doing ioctls to different pools. We need
7090 * to log this history once to each pool, and bypass
7091 * the normal history logging that happens in main().
7093 (void) zpool_log_history(g_zfs
, history_str
);
7094 log_history
= B_FALSE
;
7097 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7099 ret
= upgrade_enable_all(zhp
, &count
);
7104 cbp
->cb_first
= B_FALSE
;
7110 (void) printf(gettext("\n"));
7117 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
7119 upgrade_cbdata_t
*cbp
= arg
;
7123 config
= zpool_get_config(zhp
, NULL
);
7124 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7127 assert(SPA_VERSION_IS_SUPPORTED(version
));
7129 if (version
< SPA_VERSION_FEATURES
) {
7130 if (cbp
->cb_first
) {
7131 (void) printf(gettext("The following pools are "
7132 "formatted with legacy version numbers and can\n"
7133 "be upgraded to use feature flags. After "
7134 "being upgraded, these pools\nwill no "
7135 "longer be accessible by software that does not "
7136 "support feature\nflags.\n\n"));
7137 (void) printf(gettext("VER POOL\n"));
7138 (void) printf(gettext("--- ------------\n"));
7139 cbp
->cb_first
= B_FALSE
;
7142 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
7143 zpool_get_name(zhp
));
7150 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
7152 upgrade_cbdata_t
*cbp
= arg
;
7156 config
= zpool_get_config(zhp
, NULL
);
7157 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
7160 if (version
>= SPA_VERSION_FEATURES
) {
7162 boolean_t poolfirst
= B_TRUE
;
7163 nvlist_t
*enabled
= zpool_get_features(zhp
);
7165 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7166 const char *fguid
= spa_feature_table
[i
].fi_guid
;
7167 const char *fname
= spa_feature_table
[i
].fi_uname
;
7168 if (!nvlist_exists(enabled
, fguid
)) {
7169 if (cbp
->cb_first
) {
7170 (void) printf(gettext("\nSome "
7171 "supported features are not "
7172 "enabled on the following pools. "
7173 "Once a\nfeature is enabled the "
7174 "pool may become incompatible with "
7175 "software\nthat does not support "
7177 "zpool-features(5) for "
7179 (void) printf(gettext("POOL "
7181 (void) printf(gettext("------"
7183 cbp
->cb_first
= B_FALSE
;
7187 (void) printf(gettext("%s\n"),
7188 zpool_get_name(zhp
));
7189 poolfirst
= B_FALSE
;
7192 (void) printf(gettext(" %s\n"), fname
);
7195 * If they did "zpool upgrade -a", then we could
7196 * be doing ioctls to different pools. We need
7197 * to log this history once to each pool, and bypass
7198 * the normal history logging that happens in main().
7200 (void) zpool_log_history(g_zfs
, history_str
);
7201 log_history
= B_FALSE
;
7210 upgrade_one(zpool_handle_t
*zhp
, void *data
)
7212 boolean_t printnl
= B_FALSE
;
7213 upgrade_cbdata_t
*cbp
= data
;
7214 uint64_t cur_version
;
7217 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
7218 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
7219 "Pool 'log' must be renamed using export and import"
7224 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
7225 if (cur_version
> cbp
->cb_version
) {
7226 (void) printf(gettext("Pool '%s' is already formatted "
7227 "using more current version '%llu'.\n\n"),
7228 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
7232 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
7233 (void) printf(gettext("Pool '%s' is already formatted "
7234 "using version %llu.\n\n"), zpool_get_name(zhp
),
7235 (u_longlong_t
)cbp
->cb_version
);
7239 if (cur_version
!= cbp
->cb_version
) {
7241 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7246 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7248 ret
= upgrade_enable_all(zhp
, &count
);
7254 } else if (cur_version
== SPA_VERSION
) {
7255 (void) printf(gettext("Pool '%s' already has all "
7256 "supported features enabled.\n"),
7257 zpool_get_name(zhp
));
7262 (void) printf(gettext("\n"));
7271 * zpool upgrade [-V version] <-a | pool ...>
7273 * With no arguments, display downrev'd ZFS pool available for upgrade.
7274 * Individual pools can be upgraded by specifying the pool, and '-a' will
7275 * upgrade all pools.
7278 zpool_do_upgrade(int argc
, char **argv
)
7281 upgrade_cbdata_t cb
= { 0 };
7283 boolean_t showversions
= B_FALSE
;
7284 boolean_t upgradeall
= B_FALSE
;
7289 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
7292 upgradeall
= B_TRUE
;
7295 showversions
= B_TRUE
;
7298 cb
.cb_version
= strtoll(optarg
, &end
, 10);
7300 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
7301 (void) fprintf(stderr
,
7302 gettext("invalid version '%s'\n"), optarg
);
7307 (void) fprintf(stderr
, gettext("missing argument for "
7308 "'%c' option\n"), optopt
);
7312 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7323 if (cb
.cb_version
== 0) {
7324 cb
.cb_version
= SPA_VERSION
;
7325 } else if (!upgradeall
&& argc
== 0) {
7326 (void) fprintf(stderr
, gettext("-V option is "
7327 "incompatible with other arguments\n"));
7332 if (upgradeall
|| argc
!= 0) {
7333 (void) fprintf(stderr
, gettext("-v option is "
7334 "incompatible with other arguments\n"));
7337 } else if (upgradeall
) {
7339 (void) fprintf(stderr
, gettext("-a option should not "
7340 "be used along with a pool name\n"));
7345 (void) printf(gettext("This system supports ZFS pool feature "
7350 (void) printf(gettext("The following features are "
7352 (void) printf(gettext("FEAT DESCRIPTION\n"));
7353 (void) printf("----------------------------------------------"
7354 "---------------\n");
7355 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7356 zfeature_info_t
*fi
= &spa_feature_table
[i
];
7358 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
7359 " (read-only compatible)" : "";
7361 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
7362 (void) printf(" %s\n", fi
->fi_desc
);
7364 (void) printf("\n");
7366 (void) printf(gettext("The following legacy versions are also "
7368 (void) printf(gettext("VER DESCRIPTION\n"));
7369 (void) printf("--- -----------------------------------------"
7370 "---------------\n");
7371 (void) printf(gettext(" 1 Initial ZFS version\n"));
7372 (void) printf(gettext(" 2 Ditto blocks "
7373 "(replicated metadata)\n"));
7374 (void) printf(gettext(" 3 Hot spares and double parity "
7376 (void) printf(gettext(" 4 zpool history\n"));
7377 (void) printf(gettext(" 5 Compression using the gzip "
7379 (void) printf(gettext(" 6 bootfs pool property\n"));
7380 (void) printf(gettext(" 7 Separate intent log devices\n"));
7381 (void) printf(gettext(" 8 Delegated administration\n"));
7382 (void) printf(gettext(" 9 refquota and refreservation "
7384 (void) printf(gettext(" 10 Cache devices\n"));
7385 (void) printf(gettext(" 11 Improved scrub performance\n"));
7386 (void) printf(gettext(" 12 Snapshot properties\n"));
7387 (void) printf(gettext(" 13 snapused property\n"));
7388 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7389 (void) printf(gettext(" 15 user/group space accounting\n"));
7390 (void) printf(gettext(" 16 stmf property support\n"));
7391 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7392 (void) printf(gettext(" 18 Snapshot user holds\n"));
7393 (void) printf(gettext(" 19 Log device removal\n"));
7394 (void) printf(gettext(" 20 Compression using zle "
7395 "(zero-length encoding)\n"));
7396 (void) printf(gettext(" 21 Deduplication\n"));
7397 (void) printf(gettext(" 22 Received properties\n"));
7398 (void) printf(gettext(" 23 Slim ZIL\n"));
7399 (void) printf(gettext(" 24 System attributes\n"));
7400 (void) printf(gettext(" 25 Improved scrub stats\n"));
7401 (void) printf(gettext(" 26 Improved snapshot deletion "
7403 (void) printf(gettext(" 27 Improved snapshot creation "
7405 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7406 (void) printf(gettext("\nFor more information on a particular "
7407 "version, including supported releases,\n"));
7408 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7409 } else if (argc
== 0 && upgradeall
) {
7410 cb
.cb_first
= B_TRUE
;
7411 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
7412 if (ret
== 0 && cb
.cb_first
) {
7413 if (cb
.cb_version
== SPA_VERSION
) {
7414 (void) printf(gettext("All pools are already "
7415 "formatted using feature flags.\n\n"));
7416 (void) printf(gettext("Every feature flags "
7417 "pool already has all supported features "
7420 (void) printf(gettext("All pools are already "
7421 "formatted with version %llu or higher.\n"),
7422 (u_longlong_t
)cb
.cb_version
);
7425 } else if (argc
== 0) {
7426 cb
.cb_first
= B_TRUE
;
7427 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
7431 (void) printf(gettext("All pools are formatted "
7432 "using feature flags.\n\n"));
7434 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7435 "for a list of available legacy versions.\n"));
7438 cb
.cb_first
= B_TRUE
;
7439 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
7443 (void) printf(gettext("Every feature flags pool has "
7444 "all supported features enabled.\n"));
7446 (void) printf(gettext("\n"));
7449 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
7456 typedef struct hist_cbdata
{
7463 * Print out the command history for a specific pool.
7466 get_history_one(zpool_handle_t
*zhp
, void *data
)
7472 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
7474 cb
->first
= B_FALSE
;
7476 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
7478 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
7481 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
7482 &records
, &numrecords
) == 0);
7483 for (i
= 0; i
< numrecords
; i
++) {
7484 nvlist_t
*rec
= records
[i
];
7487 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
7491 tsec
= fnvlist_lookup_uint64(records
[i
],
7493 (void) localtime_r(&tsec
, &t
);
7494 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
7497 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
7498 (void) printf("%s %s", tbuf
,
7499 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
7500 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
7502 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
7505 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
7506 (void) printf("%s unrecognized record:\n",
7508 dump_nvlist(rec
, 4);
7511 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
7512 zfs_history_event_names
[ievent
],
7513 (longlong_t
)fnvlist_lookup_uint64(
7514 rec
, ZPOOL_HIST_TXG
),
7515 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
7516 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
7519 (void) printf("%s [txg:%lld] %s", tbuf
,
7520 (longlong_t
)fnvlist_lookup_uint64(
7521 rec
, ZPOOL_HIST_TXG
),
7522 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
7523 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
7524 (void) printf(" %s (%llu)",
7525 fnvlist_lookup_string(rec
,
7527 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
7530 (void) printf(" %s", fnvlist_lookup_string(rec
,
7531 ZPOOL_HIST_INT_STR
));
7532 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
7535 (void) printf("%s ioctl %s\n", tbuf
,
7536 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
7537 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
7538 (void) printf(" input:\n");
7539 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7540 ZPOOL_HIST_INPUT_NVL
), 8);
7542 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
7543 (void) printf(" output:\n");
7544 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7545 ZPOOL_HIST_OUTPUT_NVL
), 8);
7547 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
7548 (void) printf(" errno: %lld\n",
7549 (longlong_t
)fnvlist_lookup_int64(rec
,
7555 (void) printf("%s unrecognized record:\n", tbuf
);
7556 dump_nvlist(rec
, 4);
7560 (void) printf("\n");
7563 (void) printf(" [");
7564 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
7565 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
7566 struct passwd
*pwd
= getpwuid(who
);
7567 (void) printf("user %d ", (int)who
);
7569 (void) printf("(%s) ", pwd
->pw_name
);
7571 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
7572 (void) printf("on %s",
7573 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
7575 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
7576 (void) printf(":%s",
7577 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
7581 (void) printf("\n");
7583 (void) printf("\n");
7590 * zpool history <pool>
7592 * Displays the history of commands that modified pools.
7595 zpool_do_history(int argc
, char **argv
)
7597 hist_cbdata_t cbdata
= { 0 };
7601 cbdata
.first
= B_TRUE
;
7603 while ((c
= getopt(argc
, argv
, "li")) != -1) {
7606 cbdata
.longfmt
= B_TRUE
;
7609 cbdata
.internal
= B_TRUE
;
7612 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7620 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
7623 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
7624 (void) fprintf(stderr
, gettext("no pools available\n"));
7631 typedef struct ev_opts
{
7636 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
7640 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
7642 char ctime_str
[26], str
[32], *ptr
;
7646 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
7647 memset(str
, ' ', 32);
7648 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
7649 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
7650 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
7651 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
7652 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
7654 (void) printf(gettext("%s\t"), str
);
7656 (void) printf(gettext("%s "), str
);
7658 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
7659 (void) printf(gettext("%s\n"), ptr
);
7663 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
7667 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
7668 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
7670 data_type_t type
= nvpair_type(nvp
);
7671 const char *name
= nvpair_name(nvp
);
7681 printf(gettext("%*s%s = "), depth
, "", name
);
7684 case DATA_TYPE_BOOLEAN
:
7685 printf(gettext("%s"), "1");
7688 case DATA_TYPE_BOOLEAN_VALUE
:
7689 (void) nvpair_value_boolean_value(nvp
, &b
);
7690 printf(gettext("%s"), b
? "1" : "0");
7693 case DATA_TYPE_BYTE
:
7694 (void) nvpair_value_byte(nvp
, &i8
);
7695 printf(gettext("0x%x"), i8
);
7698 case DATA_TYPE_INT8
:
7699 (void) nvpair_value_int8(nvp
, (void *)&i8
);
7700 printf(gettext("0x%x"), i8
);
7703 case DATA_TYPE_UINT8
:
7704 (void) nvpair_value_uint8(nvp
, &i8
);
7705 printf(gettext("0x%x"), i8
);
7708 case DATA_TYPE_INT16
:
7709 (void) nvpair_value_int16(nvp
, (void *)&i16
);
7710 printf(gettext("0x%x"), i16
);
7713 case DATA_TYPE_UINT16
:
7714 (void) nvpair_value_uint16(nvp
, &i16
);
7715 printf(gettext("0x%x"), i16
);
7718 case DATA_TYPE_INT32
:
7719 (void) nvpair_value_int32(nvp
, (void *)&i32
);
7720 printf(gettext("0x%x"), i32
);
7723 case DATA_TYPE_UINT32
:
7724 (void) nvpair_value_uint32(nvp
, &i32
);
7725 printf(gettext("0x%x"), i32
);
7728 case DATA_TYPE_INT64
:
7729 (void) nvpair_value_int64(nvp
, (void *)&i64
);
7730 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7733 case DATA_TYPE_UINT64
:
7734 (void) nvpair_value_uint64(nvp
, &i64
);
7736 * translate vdev state values to readable
7737 * strings to aide zpool events consumers
7740 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
7742 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
7743 printf(gettext("\"%s\" (0x%llx)"),
7744 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
7747 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7751 case DATA_TYPE_HRTIME
:
7752 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
7753 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7756 case DATA_TYPE_STRING
:
7757 (void) nvpair_value_string(nvp
, &str
);
7758 printf(gettext("\"%s\""), str
? str
: "<NULL>");
7761 case DATA_TYPE_NVLIST
:
7762 printf(gettext("(embedded nvlist)\n"));
7763 (void) nvpair_value_nvlist(nvp
, &cnv
);
7764 zpool_do_events_nvprint(cnv
, depth
+ 8);
7765 printf(gettext("%*s(end %s)"), depth
, "", name
);
7768 case DATA_TYPE_NVLIST_ARRAY
: {
7772 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
7773 printf(gettext("(%d embedded nvlists)\n"), nelem
);
7774 for (i
= 0; i
< nelem
; i
++) {
7775 printf(gettext("%*s%s[%d] = %s\n"),
7776 depth
, "", name
, i
, "(embedded nvlist)");
7777 zpool_do_events_nvprint(val
[i
], depth
+ 8);
7778 printf(gettext("%*s(end %s[%i])\n"),
7779 depth
, "", name
, i
);
7781 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
7785 case DATA_TYPE_INT8_ARRAY
: {
7789 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
7790 for (i
= 0; i
< nelem
; i
++)
7791 printf(gettext("0x%x "), val
[i
]);
7796 case DATA_TYPE_UINT8_ARRAY
: {
7800 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
7801 for (i
= 0; i
< nelem
; i
++)
7802 printf(gettext("0x%x "), val
[i
]);
7807 case DATA_TYPE_INT16_ARRAY
: {
7811 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
7812 for (i
= 0; i
< nelem
; i
++)
7813 printf(gettext("0x%x "), val
[i
]);
7818 case DATA_TYPE_UINT16_ARRAY
: {
7822 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
7823 for (i
= 0; i
< nelem
; i
++)
7824 printf(gettext("0x%x "), val
[i
]);
7829 case DATA_TYPE_INT32_ARRAY
: {
7833 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
7834 for (i
= 0; i
< nelem
; i
++)
7835 printf(gettext("0x%x "), val
[i
]);
7840 case DATA_TYPE_UINT32_ARRAY
: {
7844 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
7845 for (i
= 0; i
< nelem
; i
++)
7846 printf(gettext("0x%x "), val
[i
]);
7851 case DATA_TYPE_INT64_ARRAY
: {
7855 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
7856 for (i
= 0; i
< nelem
; i
++)
7857 printf(gettext("0x%llx "),
7858 (u_longlong_t
)val
[i
]);
7863 case DATA_TYPE_UINT64_ARRAY
: {
7867 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
7868 for (i
= 0; i
< nelem
; i
++)
7869 printf(gettext("0x%llx "),
7870 (u_longlong_t
)val
[i
]);
7875 case DATA_TYPE_STRING_ARRAY
: {
7879 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
7880 for (i
= 0; i
< nelem
; i
++)
7881 printf(gettext("\"%s\" "),
7882 str
[i
] ? str
[i
] : "<NULL>");
7887 case DATA_TYPE_BOOLEAN_ARRAY
:
7888 case DATA_TYPE_BYTE_ARRAY
:
7889 case DATA_TYPE_DOUBLE
:
7890 case DATA_TYPE_UNKNOWN
:
7891 printf(gettext("<unknown>"));
7895 printf(gettext("\n"));
7900 zpool_do_events_next(ev_opts_t
*opts
)
7903 int zevent_fd
, ret
, dropped
;
7906 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
7907 VERIFY(zevent_fd
>= 0);
7909 if (!opts
->scripted
)
7910 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7913 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
7914 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
7915 if (ret
|| nvl
== NULL
)
7919 (void) printf(gettext("dropped %d events\n"), dropped
);
7921 if (strlen(opts
->poolname
) > 0 &&
7922 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
7923 strcmp(opts
->poolname
, pool
) != 0)
7926 zpool_do_events_short(nvl
, opts
);
7928 if (opts
->verbose
) {
7929 zpool_do_events_nvprint(nvl
, 8);
7930 printf(gettext("\n"));
7932 (void) fflush(stdout
);
7937 VERIFY(0 == close(zevent_fd
));
7943 zpool_do_events_clear(ev_opts_t
*opts
)
7947 ret
= zpool_events_clear(g_zfs
, &count
);
7949 (void) printf(gettext("cleared %d events\n"), count
);
7955 * zpool events [-vHf [pool] | -c]
7957 * Displays events logs by ZFS.
7960 zpool_do_events(int argc
, char **argv
)
7962 ev_opts_t opts
= { 0 };
7967 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
7982 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7991 (void) fprintf(stderr
, gettext("too many arguments\n"));
7993 } else if (argc
== 1) {
7994 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
7995 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
7996 (void) fprintf(stderr
,
7997 gettext("invalid pool name '%s'\n"), opts
.poolname
);
8002 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
8004 (void) fprintf(stderr
,
8005 gettext("invalid options combined with -c\n"));
8010 ret
= zpool_do_events_clear(&opts
);
8012 ret
= zpool_do_events_next(&opts
);
8018 get_callback(zpool_handle_t
*zhp
, void *data
)
8020 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
8021 char value
[MAXNAMELEN
];
8022 zprop_source_t srctype
;
8025 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
8028 * Skip the special fake placeholder. This will also skip
8029 * over the name property when 'all' is specified.
8031 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
8032 pl
== cbp
->cb_proplist
)
8035 if (pl
->pl_prop
== ZPROP_INVAL
&&
8036 (zpool_prop_feature(pl
->pl_user_prop
) ||
8037 zpool_prop_unsupported(pl
->pl_user_prop
))) {
8038 srctype
= ZPROP_SRC_LOCAL
;
8040 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
8041 value
, sizeof (value
)) == 0) {
8042 zprop_print_one_property(zpool_get_name(zhp
),
8043 cbp
, pl
->pl_user_prop
, value
, srctype
,
8047 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
8048 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
8051 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
8052 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
8060 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
8062 * -H Scripted mode. Don't display headers, and separate properties
8064 * -o List of columns to display. Defaults to
8065 * "name,property,value,source".
8066 * -p Display values in parsable (exact) format.
8068 * Get properties of pools in the system. Output space statistics
8069 * for each one as well as other attributes.
8072 zpool_do_get(int argc
, char **argv
)
8074 zprop_get_cbdata_t cb
= { 0 };
8075 zprop_list_t fake_name
= { 0 };
8080 cb
.cb_first
= B_TRUE
;
8083 * Set up default columns and sources.
8085 cb
.cb_sources
= ZPROP_SRC_ALL
;
8086 cb
.cb_columns
[0] = GET_COL_NAME
;
8087 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
8088 cb
.cb_columns
[2] = GET_COL_VALUE
;
8089 cb
.cb_columns
[3] = GET_COL_SOURCE
;
8090 cb
.cb_type
= ZFS_TYPE_POOL
;
8093 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
8096 cb
.cb_literal
= B_TRUE
;
8099 cb
.cb_scripted
= B_TRUE
;
8102 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
8104 while (*optarg
!= '\0') {
8105 static char *col_subopts
[] =
8106 { "name", "property", "value", "source",
8109 if (i
== ZFS_GET_NCOLS
) {
8110 (void) fprintf(stderr
, gettext("too "
8111 "many fields given to -o "
8116 switch (getsubopt(&optarg
, col_subopts
,
8119 cb
.cb_columns
[i
++] = GET_COL_NAME
;
8122 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
8125 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
8128 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
8132 (void) fprintf(stderr
,
8133 gettext("\"all\" conflicts "
8134 "with specific fields "
8135 "given to -o option\n"));
8138 cb
.cb_columns
[0] = GET_COL_NAME
;
8139 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
8140 cb
.cb_columns
[2] = GET_COL_VALUE
;
8141 cb
.cb_columns
[3] = GET_COL_SOURCE
;
8145 (void) fprintf(stderr
,
8146 gettext("invalid column name "
8153 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8163 (void) fprintf(stderr
, gettext("missing property "
8168 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
8169 ZFS_TYPE_POOL
) != 0)
8175 if (cb
.cb_proplist
!= NULL
) {
8176 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
8177 fake_name
.pl_width
= strlen(gettext("NAME"));
8178 fake_name
.pl_next
= cb
.cb_proplist
;
8179 cb
.cb_proplist
= &fake_name
;
8182 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
8185 if (cb
.cb_proplist
== &fake_name
)
8186 zprop_free_list(fake_name
.pl_next
);
8188 zprop_free_list(cb
.cb_proplist
);
8193 typedef struct set_cbdata
{
8196 boolean_t cb_any_successful
;
8200 set_callback(zpool_handle_t
*zhp
, void *data
)
8203 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
8205 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
8208 cb
->cb_any_successful
= B_TRUE
;
8214 zpool_do_set(int argc
, char **argv
)
8216 set_cbdata_t cb
= { 0 };
8219 if (argc
> 1 && argv
[1][0] == '-') {
8220 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8226 (void) fprintf(stderr
, gettext("missing property=value "
8232 (void) fprintf(stderr
, gettext("missing pool name\n"));
8237 (void) fprintf(stderr
, gettext("too many pool names\n"));
8241 cb
.cb_propname
= argv
[1];
8242 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
8243 if (cb
.cb_value
== NULL
) {
8244 (void) fprintf(stderr
, gettext("missing value in "
8245 "property=value argument\n"));
8249 *(cb
.cb_value
) = '\0';
8252 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
8259 find_command_idx(char *command
, int *idx
)
8263 for (i
= 0; i
< NCOMMAND
; i
++) {
8264 if (command_table
[i
].name
== NULL
)
8267 if (strcmp(command
, command_table
[i
].name
) == 0) {
8276 main(int argc
, char **argv
)
8282 (void) setlocale(LC_ALL
, "");
8283 (void) textdomain(TEXT_DOMAIN
);
8289 * Make sure the user has specified some command.
8292 (void) fprintf(stderr
, gettext("missing command\n"));
8301 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
8304 if ((g_zfs
= libzfs_init()) == NULL
) {
8305 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
8309 libzfs_print_on_error(g_zfs
, B_TRUE
);
8311 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8314 * Run the appropriate command.
8316 if (find_command_idx(cmdname
, &i
) == 0) {
8317 current_command
= &command_table
[i
];
8318 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
8319 } else if (strchr(cmdname
, '=')) {
8320 verify(find_command_idx("set", &i
) == 0);
8321 current_command
= &command_table
[i
];
8322 ret
= command_table
[i
].func(argc
, argv
);
8323 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
8325 * 'freeze' is a vile debugging abomination, so we treat
8328 zfs_cmd_t zc
= {"\0"};
8330 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
8331 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
8333 (void) fprintf(stderr
,
8334 gettext("failed to freeze pool: %d\n"), errno
);
8340 (void) fprintf(stderr
, gettext("unrecognized "
8341 "command '%s'\n"), cmdname
);
8346 if (ret
== 0 && log_history
)
8347 (void) zpool_log_history(g_zfs
, history_str
);
8352 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8353 * for the purposes of running ::findleaks.
8355 if (getenv("ZFS_ABORT") != NULL
) {
8356 (void) printf("dumping core by request\n");