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 <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. Currently, this supports removing
786 * spares, cache, and log devices from the pool.
789 zpool_do_remove(int argc
, char **argv
)
793 zpool_handle_t
*zhp
= NULL
;
798 /* get pool name and check number of arguments */
800 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
804 (void) fprintf(stderr
, gettext("missing device\n"));
810 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
813 for (i
= 1; i
< argc
; i
++) {
814 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
823 * zpool labelclear [-f] <vdev>
825 * -f Force clearing the label for the vdevs which are members of
826 * the exported or foreign pools.
828 * Verifies that the vdev is not active and zeros out the label information
832 zpool_do_labelclear(int argc
, char **argv
)
834 char vdev
[MAXPATHLEN
];
837 int c
, fd
= -1, ret
= 0;
840 boolean_t inuse
= B_FALSE
;
841 boolean_t force
= B_FALSE
;
844 while ((c
= getopt(argc
, argv
, "f")) != -1) {
850 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
861 (void) fprintf(stderr
, gettext("missing vdev name\n"));
865 (void) fprintf(stderr
, gettext("too many arguments\n"));
870 * Check if we were given absolute path and use it as is.
871 * Otherwise if the provided vdev name doesn't point to a file,
872 * try prepending expected disk paths and partition numbers.
874 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
875 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
878 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
879 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
880 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
884 if (error
|| (stat(vdev
, &st
) != 0)) {
885 (void) fprintf(stderr
, gettext(
886 "failed to find device %s, try specifying absolute "
887 "path instead\n"), argv
[0]);
892 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
893 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
894 vdev
, strerror(errno
));
898 if (ioctl(fd
, BLKFLSBUF
) != 0)
899 (void) fprintf(stderr
, gettext("failed to invalidate "
900 "cache for %s: %s\n"), vdev
, strerror(errno
));
902 if (zpool_read_label(fd
, &config
, NULL
) != 0 || config
== NULL
) {
903 (void) fprintf(stderr
,
904 gettext("failed to check state for %s\n"), vdev
);
910 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
912 (void) fprintf(stderr
,
913 gettext("failed to check state for %s\n"), vdev
);
923 case POOL_STATE_ACTIVE
:
924 case POOL_STATE_SPARE
:
925 case POOL_STATE_L2CACHE
:
926 (void) fprintf(stderr
, gettext(
927 "%s is a member (%s) of pool \"%s\"\n"),
928 vdev
, zpool_pool_state_to_name(state
), name
);
932 case POOL_STATE_EXPORTED
:
935 (void) fprintf(stderr
, gettext(
936 "use '-f' to override the following error:\n"
937 "%s is a member of exported pool \"%s\"\n"),
942 case POOL_STATE_POTENTIALLY_ACTIVE
:
945 (void) fprintf(stderr
, gettext(
946 "use '-f' to override the following error:\n"
947 "%s is a member of potentially active pool \"%s\"\n"),
952 case POOL_STATE_DESTROYED
:
953 /* inuse should never be set for a destroyed pool */
959 ret
= zpool_clear_label(fd
);
961 (void) fprintf(stderr
,
962 gettext("failed to clear label for %s\n"), vdev
);
973 * zpool create [-fnd] [-o property=value] ...
974 * [-O file-system-property=value] ...
975 * [-R root] [-m mountpoint] <pool> <dev> ...
977 * -f Force creation, even if devices appear in use
978 * -n Do not create the pool, but display the resulting layout if it
979 * were to be created.
980 * -R Create a pool under an alternate root
981 * -m Set default mountpoint for the root dataset. By default it's
983 * -o Set property=value.
984 * -o Set feature@feature=enabled|disabled.
985 * -d Don't automatically enable all supported pool features
986 * (individual features can be enabled with -o).
987 * -O Set fsproperty=value in the pool's root file system
989 * Creates the named pool according to the given vdev specification. The
990 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
991 * we get the nvlist back from get_vdev_spec(), we either print out the contents
992 * (if '-n' was specified), or pass it to libzfs to do the creation.
995 zpool_do_create(int argc
, char **argv
)
997 boolean_t force
= B_FALSE
;
998 boolean_t dryrun
= B_FALSE
;
999 boolean_t enable_all_pool_feat
= B_TRUE
;
1001 nvlist_t
*nvroot
= NULL
;
1005 char *altroot
= NULL
;
1006 char *mountpoint
= NULL
;
1007 nvlist_t
*fsprops
= NULL
;
1008 nvlist_t
*props
= NULL
;
1012 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1021 enable_all_pool_feat
= B_FALSE
;
1025 if (add_prop_list(zpool_prop_to_name(
1026 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1028 if (add_prop_list_default(zpool_prop_to_name(
1029 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1033 /* Equivalent to -O mountpoint=optarg */
1034 mountpoint
= optarg
;
1037 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1038 (void) fprintf(stderr
, gettext("missing "
1039 "'=' for -o option\n"));
1045 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1049 * If the user is creating a pool that doesn't support
1050 * feature flags, don't enable any features.
1052 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1056 ver
= strtoull(propval
, &end
, 10);
1058 ver
< SPA_VERSION_FEATURES
) {
1059 enable_all_pool_feat
= B_FALSE
;
1062 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1066 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1067 (void) fprintf(stderr
, gettext("missing "
1068 "'=' for -O option\n"));
1075 * Mountpoints are checked and then added later.
1076 * Uniquely among properties, they can be specified
1077 * more than once, to avoid conflict with -m.
1079 if (0 == strcmp(optarg
,
1080 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1081 mountpoint
= propval
;
1082 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1089 * Sanity check temporary pool name.
1091 if (strchr(optarg
, '/') != NULL
) {
1092 (void) fprintf(stderr
, gettext("cannot create "
1093 "'%s': invalid character '/' in temporary "
1095 (void) fprintf(stderr
, gettext("use 'zfs "
1096 "create' to create a dataset\n"));
1100 if (add_prop_list(zpool_prop_to_name(
1101 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1103 if (add_prop_list_default(zpool_prop_to_name(
1104 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1109 (void) fprintf(stderr
, gettext("missing argument for "
1110 "'%c' option\n"), optopt
);
1113 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1122 /* get pool name and check number of arguments */
1124 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1128 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1135 * As a special case, check for use of '/' in the name, and direct the
1136 * user to use 'zfs create' instead.
1138 if (strchr(poolname
, '/') != NULL
) {
1139 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1140 "character '/' in pool name\n"), poolname
);
1141 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1142 "create a dataset\n"));
1146 /* pass off to get_vdev_spec for bulk processing */
1147 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1148 argc
- 1, argv
+ 1);
1152 /* make_root_vdev() allows 0 toplevel children if there are spares */
1153 if (!zfs_allocatable_devs(nvroot
)) {
1154 (void) fprintf(stderr
, gettext("invalid vdev "
1155 "specification: at least one toplevel vdev must be "
1160 if (altroot
!= NULL
&& altroot
[0] != '/') {
1161 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1162 "must be an absolute path\n"), altroot
);
1167 * Check the validity of the mountpoint and direct the user to use the
1168 * '-m' mountpoint option if it looks like its in use.
1170 if (mountpoint
== NULL
||
1171 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1172 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1173 char buf
[MAXPATHLEN
];
1176 if (mountpoint
&& mountpoint
[0] != '/') {
1177 (void) fprintf(stderr
, gettext("invalid mountpoint "
1178 "'%s': must be an absolute path, 'legacy', or "
1179 "'none'\n"), mountpoint
);
1183 if (mountpoint
== NULL
) {
1184 if (altroot
!= NULL
)
1185 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1188 (void) snprintf(buf
, sizeof (buf
), "/%s",
1191 if (altroot
!= NULL
)
1192 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1193 altroot
, mountpoint
);
1195 (void) snprintf(buf
, sizeof (buf
), "%s",
1199 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1200 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1201 "%s\n"), buf
, strerror(errno
));
1202 (void) fprintf(stderr
, gettext("use '-m' "
1203 "option to provide a different default\n"));
1208 while (count
< 3 && readdir(dirp
) != NULL
)
1210 (void) closedir(dirp
);
1213 (void) fprintf(stderr
, gettext("mountpoint "
1214 "'%s' exists and is not empty\n"), buf
);
1215 (void) fprintf(stderr
, gettext("use '-m' "
1216 "option to provide a "
1217 "different default\n"));
1224 * Now that the mountpoint's validity has been checked, ensure that
1225 * the property is set appropriately prior to creating the pool.
1227 if (mountpoint
!= NULL
) {
1228 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1229 mountpoint
, &fsprops
, B_FALSE
);
1237 * For a dry run invocation, print out a basic message and run
1238 * through all the vdevs in the list and print out in an
1239 * appropriate hierarchy.
1241 (void) printf(gettext("would create '%s' with the "
1242 "following layout:\n\n"), poolname
);
1244 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1245 if (num_logs(nvroot
) > 0)
1246 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1251 * Hand off to libzfs.
1254 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1255 char propname
[MAXPATHLEN
];
1257 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1259 (void) snprintf(propname
, sizeof (propname
),
1260 "feature@%s", feat
->fi_uname
);
1263 * Only features contained in props will be enabled:
1264 * remove from the nvlist every ZFS_FEATURE_DISABLED
1265 * value and add every missing ZFS_FEATURE_ENABLED if
1266 * enable_all_pool_feat is set.
1268 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1269 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1270 (void) nvlist_remove_all(props
,
1272 } else if (enable_all_pool_feat
) {
1273 ret
= add_prop_list(propname
,
1274 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1281 if (zpool_create(g_zfs
, poolname
,
1282 nvroot
, props
, fsprops
) == 0) {
1283 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1284 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1286 if (zfs_mount(pool
, NULL
, 0) == 0)
1287 ret
= zfs_shareall(pool
);
1290 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1291 (void) fprintf(stderr
, gettext("pool name may have "
1297 nvlist_free(nvroot
);
1298 nvlist_free(fsprops
);
1302 nvlist_free(fsprops
);
1309 * zpool destroy <pool>
1311 * -f Forcefully unmount any datasets
1313 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1316 zpool_do_destroy(int argc
, char **argv
)
1318 boolean_t force
= B_FALSE
;
1321 zpool_handle_t
*zhp
;
1325 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1331 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1340 /* check arguments */
1342 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1346 (void) fprintf(stderr
, gettext("too many arguments\n"));
1352 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1354 * As a special case, check for use of '/' in the name, and
1355 * direct the user to use 'zfs destroy' instead.
1357 if (strchr(pool
, '/') != NULL
)
1358 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1359 "destroy a dataset\n"));
1363 if (zpool_disable_datasets(zhp
, force
) != 0) {
1364 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1365 "could not unmount datasets\n"), zpool_get_name(zhp
));
1370 /* The history must be logged as part of the export */
1371 log_history
= B_FALSE
;
1373 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1380 typedef struct export_cbdata
{
1382 boolean_t hardforce
;
1389 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1391 export_cbdata_t
*cb
= data
;
1393 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1396 /* The history must be logged as part of the export */
1397 log_history
= B_FALSE
;
1399 if (cb
->hardforce
) {
1400 if (zpool_export_force(zhp
, history_str
) != 0)
1402 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1410 * zpool export [-f] <pool> ...
1412 * -a Export all pools
1413 * -f Forcefully unmount datasets
1415 * Export the given pools. By default, the command will attempt to cleanly
1416 * unmount any active datasets within the pool. If the '-f' flag is specified,
1417 * then the datasets will be forcefully unmounted.
1420 zpool_do_export(int argc
, char **argv
)
1423 boolean_t do_all
= B_FALSE
;
1424 boolean_t force
= B_FALSE
;
1425 boolean_t hardforce
= B_FALSE
;
1429 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1441 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1448 cb
.hardforce
= hardforce
;
1454 (void) fprintf(stderr
, gettext("too many arguments\n"));
1458 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1459 zpool_export_one
, &cb
));
1462 /* check arguments */
1464 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1468 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1474 * Given a vdev configuration, determine the maximum width needed for the device
1478 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1486 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1487 if (strlen(name
) + depth
> max
)
1488 max
= strlen(name
) + depth
;
1492 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1493 &child
, &children
) == 0) {
1494 for (c
= 0; c
< children
; c
++)
1495 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1496 max
, name_flags
)) > max
)
1500 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1501 &child
, &children
) == 0) {
1502 for (c
= 0; c
< children
; c
++)
1503 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1504 max
, name_flags
)) > max
)
1508 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1509 &child
, &children
) == 0) {
1510 for (c
= 0; c
< children
; c
++)
1511 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1512 max
, name_flags
)) > max
)
1519 typedef struct spare_cbdata
{
1521 zpool_handle_t
*cb_zhp
;
1525 find_vdev(nvlist_t
*nv
, uint64_t search
)
1531 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1535 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1536 &child
, &children
) == 0) {
1537 for (c
= 0; c
< children
; c
++)
1538 if (find_vdev(child
[c
], search
))
1546 find_spare(zpool_handle_t
*zhp
, void *data
)
1548 spare_cbdata_t
*cbp
= data
;
1549 nvlist_t
*config
, *nvroot
;
1551 config
= zpool_get_config(zhp
, NULL
);
1552 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1555 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1564 typedef struct status_cbdata
{
1568 boolean_t cb_allpools
;
1569 boolean_t cb_verbose
;
1570 boolean_t cb_explain
;
1572 boolean_t cb_dedup_stats
;
1573 boolean_t cb_print_status
;
1574 vdev_cmd_data_list_t
*vcdl
;
1577 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1579 is_blank_str(char *str
)
1581 while (str
!= NULL
&& *str
!= '\0') {
1589 /* Print command output lines for specific vdev in a specific pool */
1591 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1593 vdev_cmd_data_t
*data
;
1597 for (i
= 0; i
< vcdl
->count
; i
++) {
1598 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1599 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1600 /* Not the vdev we're looking for */
1604 data
= &vcdl
->data
[i
];
1605 /* Print out all the output values for this vdev */
1606 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1608 /* Does this vdev have values for this column? */
1609 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1610 if (strcmp(data
->cols
[k
],
1611 vcdl
->uniq_cols
[j
]) == 0) {
1612 /* yes it does, record the value */
1613 val
= data
->lines
[k
];
1618 * Mark empty values with dashes to make output
1621 if (is_blank_str(val
))
1624 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1625 if (j
< vcdl
->uniq_cols_cnt
- 1)
1629 /* Print out any values that aren't in a column at the end */
1630 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1631 /* Did we have any columns? If so print a spacer. */
1632 if (vcdl
->uniq_cols_cnt
> 0)
1635 val
= data
->lines
[j
];
1636 printf("%s", val
? val
: "");
1643 * Print out configuration state as requested by status_callback.
1646 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1647 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1651 pool_scan_stat_t
*ps
= NULL
;
1653 char rbuf
[6], wbuf
[6], cbuf
[6];
1655 uint64_t notpresent
;
1656 spare_cbdata_t spare_cb
;
1660 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1661 &child
, &children
) != 0)
1664 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1665 (uint64_t **)&vs
, &c
) == 0);
1667 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1670 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1673 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1675 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1679 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1683 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1684 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1685 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1686 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1689 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1690 ¬present
) == 0) {
1691 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1692 (void) printf(" was %s", path
);
1693 } else if (vs
->vs_aux
!= 0) {
1696 switch (vs
->vs_aux
) {
1697 case VDEV_AUX_OPEN_FAILED
:
1698 (void) printf(gettext("cannot open"));
1701 case VDEV_AUX_BAD_GUID_SUM
:
1702 (void) printf(gettext("missing device"));
1705 case VDEV_AUX_NO_REPLICAS
:
1706 (void) printf(gettext("insufficient replicas"));
1709 case VDEV_AUX_VERSION_NEWER
:
1710 (void) printf(gettext("newer version"));
1713 case VDEV_AUX_UNSUP_FEAT
:
1714 (void) printf(gettext("unsupported feature(s)"));
1717 case VDEV_AUX_SPARED
:
1718 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1719 &spare_cb
.cb_guid
) == 0);
1720 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1721 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1722 zpool_get_name(zhp
)) == 0)
1723 (void) printf(gettext("currently in "
1726 (void) printf(gettext("in use by "
1728 zpool_get_name(spare_cb
.cb_zhp
));
1729 zpool_close(spare_cb
.cb_zhp
);
1731 (void) printf(gettext("currently in use"));
1735 case VDEV_AUX_ERR_EXCEEDED
:
1736 (void) printf(gettext("too many errors"));
1739 case VDEV_AUX_IO_FAILURE
:
1740 (void) printf(gettext("experienced I/O failures"));
1743 case VDEV_AUX_BAD_LOG
:
1744 (void) printf(gettext("bad intent log"));
1747 case VDEV_AUX_EXTERNAL
:
1748 (void) printf(gettext("external device fault"));
1751 case VDEV_AUX_SPLIT_POOL
:
1752 (void) printf(gettext("split into new pool"));
1755 case VDEV_AUX_ACTIVE
:
1756 (void) printf(gettext("currently in use"));
1760 (void) printf(gettext("corrupted data"));
1765 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1766 (uint64_t **)&ps
, &c
);
1768 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&&
1769 vs
->vs_scan_processed
!= 0 && children
== 0) {
1770 (void) printf(gettext(" (%s)"),
1771 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1772 "resilvering" : "repairing");
1775 if (cb
->vcdl
!= NULL
) {
1776 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1778 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1782 (void) printf("\n");
1784 for (c
= 0; c
< children
; c
++) {
1785 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1787 /* Don't print logs or holes here */
1788 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1790 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1792 if (islog
|| ishole
)
1794 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1795 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1796 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1803 * Print the configuration of an exported pool. Iterate over all vdevs in the
1804 * pool, printing out the name and status for each one.
1807 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1815 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1816 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1817 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1820 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1821 (uint64_t **)&vs
, &c
) == 0);
1823 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1824 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1826 if (vs
->vs_aux
!= 0) {
1829 switch (vs
->vs_aux
) {
1830 case VDEV_AUX_OPEN_FAILED
:
1831 (void) printf(gettext("cannot open"));
1834 case VDEV_AUX_BAD_GUID_SUM
:
1835 (void) printf(gettext("missing device"));
1838 case VDEV_AUX_NO_REPLICAS
:
1839 (void) printf(gettext("insufficient replicas"));
1842 case VDEV_AUX_VERSION_NEWER
:
1843 (void) printf(gettext("newer version"));
1846 case VDEV_AUX_UNSUP_FEAT
:
1847 (void) printf(gettext("unsupported feature(s)"));
1850 case VDEV_AUX_ERR_EXCEEDED
:
1851 (void) printf(gettext("too many errors"));
1854 case VDEV_AUX_ACTIVE
:
1855 (void) printf(gettext("currently in use"));
1859 (void) printf(gettext("corrupted data"));
1863 (void) printf("\n");
1865 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1866 &child
, &children
) != 0)
1869 for (c
= 0; c
< children
; c
++) {
1870 uint64_t is_log
= B_FALSE
;
1872 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1877 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1878 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1879 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1883 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1884 &child
, &children
) == 0) {
1885 (void) printf(gettext("\tcache\n"));
1886 for (c
= 0; c
< children
; c
++) {
1887 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1889 (void) printf("\t %s\n", vname
);
1894 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1895 &child
, &children
) == 0) {
1896 (void) printf(gettext("\tspares\n"));
1897 for (c
= 0; c
< children
; c
++) {
1898 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1900 (void) printf("\t %s\n", vname
);
1908 * Logs are recorded as top level vdevs in the main pool child array
1909 * but with "is_log" set to 1. We use either print_status_config() or
1910 * print_import_config() to print the top level logs then any log
1911 * children (eg mirrored slogs) are printed recursively - which
1912 * works because only the top level vdev is marked "is_log"
1915 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1920 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1924 (void) printf(gettext("\tlogs\n"));
1926 for (c
= 0; c
< children
; c
++) {
1927 uint64_t is_log
= B_FALSE
;
1930 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1934 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1935 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1936 if (cb
->cb_print_status
)
1937 print_status_config(zhp
, cb
, name
, child
[c
], 2,
1940 print_import_config(cb
, name
, child
[c
], 2);
1946 * Display the status for the given pool.
1949 show_import(nvlist_t
*config
)
1951 uint64_t pool_state
;
1955 uint64_t hostid
= 0;
1957 char *hostname
= "unknown";
1958 nvlist_t
*nvroot
, *nvinfo
;
1959 zpool_status_t reason
;
1960 zpool_errata_t errata
;
1964 status_cbdata_t cb
= { 0 };
1966 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1968 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1970 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1972 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1975 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1976 (uint64_t **)&vs
, &vsc
) == 0);
1977 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1979 reason
= zpool_import_status(config
, &msgid
, &errata
);
1981 (void) printf(gettext(" pool: %s\n"), name
);
1982 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1983 (void) printf(gettext(" state: %s"), health
);
1984 if (pool_state
== POOL_STATE_DESTROYED
)
1985 (void) printf(gettext(" (DESTROYED)"));
1986 (void) printf("\n");
1989 case ZPOOL_STATUS_MISSING_DEV_R
:
1990 case ZPOOL_STATUS_MISSING_DEV_NR
:
1991 case ZPOOL_STATUS_BAD_GUID_SUM
:
1992 (void) printf(gettext(" status: One or more devices are "
1993 "missing from the system.\n"));
1996 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1997 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1998 (void) printf(gettext(" status: One or more devices contains "
1999 "corrupted data.\n"));
2002 case ZPOOL_STATUS_CORRUPT_DATA
:
2004 gettext(" status: The pool data is corrupted.\n"));
2007 case ZPOOL_STATUS_OFFLINE_DEV
:
2008 (void) printf(gettext(" status: One or more devices "
2009 "are offlined.\n"));
2012 case ZPOOL_STATUS_CORRUPT_POOL
:
2013 (void) printf(gettext(" status: The pool metadata is "
2017 case ZPOOL_STATUS_VERSION_OLDER
:
2018 (void) printf(gettext(" status: The pool is formatted using a "
2019 "legacy on-disk version.\n"));
2022 case ZPOOL_STATUS_VERSION_NEWER
:
2023 (void) printf(gettext(" status: The pool is formatted using an "
2024 "incompatible version.\n"));
2027 case ZPOOL_STATUS_FEAT_DISABLED
:
2028 (void) printf(gettext(" status: Some supported features are "
2029 "not enabled on the pool.\n"));
2032 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2033 (void) printf(gettext("status: The pool uses the following "
2034 "feature(s) not supported on this system:\n"));
2035 zpool_print_unsup_feat(config
);
2038 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2039 (void) printf(gettext("status: The pool can only be accessed "
2040 "in read-only mode on this system. It\n\tcannot be "
2041 "accessed in read-write mode because it uses the "
2042 "following\n\tfeature(s) not supported on this system:\n"));
2043 zpool_print_unsup_feat(config
);
2046 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2047 (void) printf(gettext(" status: The pool is currently "
2048 "imported by another system.\n"));
2051 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2052 (void) printf(gettext(" status: The pool has the "
2053 "multihost property on. It cannot\n\tbe safely imported "
2054 "when the system hostid is not set.\n"));
2057 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2058 (void) printf(gettext(" status: The pool was last accessed by "
2059 "another system.\n"));
2062 case ZPOOL_STATUS_FAULTED_DEV_R
:
2063 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2064 (void) printf(gettext(" status: One or more devices are "
2068 case ZPOOL_STATUS_BAD_LOG
:
2069 (void) printf(gettext(" status: An intent log record cannot be "
2073 case ZPOOL_STATUS_RESILVERING
:
2074 (void) printf(gettext(" status: One or more devices were being "
2078 case ZPOOL_STATUS_ERRATA
:
2079 (void) printf(gettext(" status: Errata #%d detected.\n"),
2085 * No other status can be seen when importing pools.
2087 assert(reason
== ZPOOL_STATUS_OK
);
2091 * Print out an action according to the overall state of the pool.
2093 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2094 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2095 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2096 (void) printf(gettext(" action: The pool can be "
2097 "imported using its name or numeric identifier, "
2098 "though\n\tsome features will not be available "
2099 "without an explicit 'zpool upgrade'.\n"));
2100 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2101 (void) printf(gettext(" action: The pool can be "
2102 "imported using its name or numeric "
2103 "identifier and\n\tthe '-f' flag.\n"));
2104 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2106 case ZPOOL_ERRATA_NONE
:
2109 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2110 (void) printf(gettext(" action: The pool can "
2111 "be imported using its name or numeric "
2112 "identifier,\n\thowever there is a compat"
2113 "ibility issue which should be corrected"
2114 "\n\tby running 'zpool scrub'\n"));
2117 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2118 (void) printf(gettext(" action: The pool can"
2119 "not be imported with this version of ZFS "
2120 "due to\n\tan active asynchronous destroy. "
2121 "Revert to an earlier version\n\tand "
2122 "allow the destroy to complete before "
2126 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2127 (void) printf(gettext(" action: Existing "
2128 "encrypted datasets contain an on-disk "
2129 "incompatibility, which\n\tneeds to be "
2130 "corrected. Backup these datasets to new "
2131 "encrypted datasets\n\tand destroy the "
2137 * All errata must contain an action message.
2142 (void) printf(gettext(" action: The pool can be "
2143 "imported using its name or numeric "
2146 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2147 (void) printf(gettext(" action: The pool can be imported "
2148 "despite missing or damaged devices. The\n\tfault "
2149 "tolerance of the pool may be compromised if imported.\n"));
2152 case ZPOOL_STATUS_VERSION_NEWER
:
2153 (void) printf(gettext(" action: The pool cannot be "
2154 "imported. Access the pool on a system running "
2155 "newer\n\tsoftware, or recreate the pool from "
2158 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2159 (void) printf(gettext("action: The pool cannot be "
2160 "imported. Access the pool on a system that "
2161 "supports\n\tthe required feature(s), or recreate "
2162 "the pool from backup.\n"));
2164 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2165 (void) printf(gettext("action: The pool cannot be "
2166 "imported in read-write mode. Import the pool "
2168 "\t\"-o readonly=on\", access the pool on a system "
2169 "that supports the\n\trequired feature(s), or "
2170 "recreate the pool from backup.\n"));
2172 case ZPOOL_STATUS_MISSING_DEV_R
:
2173 case ZPOOL_STATUS_MISSING_DEV_NR
:
2174 case ZPOOL_STATUS_BAD_GUID_SUM
:
2175 (void) printf(gettext(" action: The pool cannot be "
2176 "imported. Attach the missing\n\tdevices and try "
2179 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2180 VERIFY0(nvlist_lookup_nvlist(config
,
2181 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2183 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2184 hostname
= fnvlist_lookup_string(nvinfo
,
2185 ZPOOL_CONFIG_MMP_HOSTNAME
);
2187 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2188 hostid
= fnvlist_lookup_uint64(nvinfo
,
2189 ZPOOL_CONFIG_MMP_HOSTID
);
2191 (void) printf(gettext(" action: The pool must be "
2192 "exported from %s (hostid=%lx)\n\tbefore it "
2193 "can be safely imported.\n"), hostname
,
2194 (unsigned long) hostid
);
2196 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2197 (void) printf(gettext(" action: Set a unique system "
2198 "hostid with the zgenhostid(8) command.\n"));
2201 (void) printf(gettext(" action: The pool cannot be "
2202 "imported due to damaged devices or data.\n"));
2206 /* Print the comment attached to the pool. */
2207 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2208 (void) printf(gettext("comment: %s\n"), comment
);
2211 * If the state is "closed" or "can't open", and the aux state
2212 * is "corrupt data":
2214 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2215 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2216 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2217 if (pool_state
== POOL_STATE_DESTROYED
)
2218 (void) printf(gettext("\tThe pool was destroyed, "
2219 "but can be imported using the '-Df' flags.\n"));
2220 else if (pool_state
!= POOL_STATE_EXPORTED
)
2221 (void) printf(gettext("\tThe pool may be active on "
2222 "another system, but can be imported using\n\t"
2223 "the '-f' flag.\n"));
2227 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2230 (void) printf(gettext(" config:\n\n"));
2232 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2234 if (cb
.cb_namewidth
< 10)
2235 cb
.cb_namewidth
= 10;
2237 print_import_config(&cb
, name
, nvroot
, 0);
2238 if (num_logs(nvroot
) > 0)
2239 print_logs(NULL
, &cb
, nvroot
);
2241 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2242 (void) printf(gettext("\n\tAdditional devices are known to "
2243 "be part of this pool, though their\n\texact "
2244 "configuration cannot be determined.\n"));
2249 zfs_force_import_required(nvlist_t
*config
)
2252 uint64_t hostid
= 0;
2255 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2256 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2258 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2261 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2262 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2263 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2264 ZPOOL_CONFIG_MMP_STATE
);
2266 if (mmp_state
!= MMP_STATE_INACTIVE
)
2274 * Perform the import for the given configuration. This passes the heavy
2275 * lifting off to zpool_import_props(), and then mounts the datasets contained
2279 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2280 nvlist_t
*props
, int flags
)
2283 zpool_handle_t
*zhp
;
2287 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2288 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2290 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2291 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2292 "is formatted using an unsupported ZFS version\n"), name
);
2294 } else if (zfs_force_import_required(config
) &&
2295 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2296 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2299 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2300 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2301 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2302 ZPOOL_CONFIG_MMP_STATE
);
2304 if (mmp_state
== MMP_STATE_ACTIVE
) {
2305 char *hostname
= "<unknown>";
2306 uint64_t hostid
= 0;
2308 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2309 hostname
= fnvlist_lookup_string(nvinfo
,
2310 ZPOOL_CONFIG_MMP_HOSTNAME
);
2312 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2313 hostid
= fnvlist_lookup_uint64(nvinfo
,
2314 ZPOOL_CONFIG_MMP_HOSTID
);
2316 (void) fprintf(stderr
, gettext("cannot import '%s': "
2317 "pool is imported on %s (hostid: "
2318 "0x%lx)\nExport the pool on the other system, "
2319 "then run 'zpool import'.\n"),
2320 name
, hostname
, (unsigned long) hostid
);
2321 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2322 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2323 "pool has the multihost property on and the\n"
2324 "system's hostid is not set. Set a unique hostid "
2325 "with the zgenhostid(8) command.\n"), name
);
2327 char *hostname
= "<unknown>";
2328 uint64_t timestamp
= 0;
2329 uint64_t hostid
= 0;
2331 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2332 hostname
= fnvlist_lookup_string(config
,
2333 ZPOOL_CONFIG_HOSTNAME
);
2335 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2336 timestamp
= fnvlist_lookup_uint64(config
,
2337 ZPOOL_CONFIG_TIMESTAMP
);
2339 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2340 hostid
= fnvlist_lookup_uint64(config
,
2341 ZPOOL_CONFIG_HOSTID
);
2343 (void) fprintf(stderr
, gettext("cannot import '%s': "
2344 "pool was previously in use from another system.\n"
2345 "Last accessed by %s (hostid=%lx) at %s"
2346 "The pool can be imported, use 'zpool import -f' "
2347 "to import the pool.\n"), name
, hostname
,
2348 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2354 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2357 if (newname
!= NULL
)
2358 name
= (char *)newname
;
2360 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2364 * Loading keys is best effort. We don't want to return immediately
2365 * if it fails but we do want to give the error to the caller.
2367 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
2368 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
2373 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2374 !(flags
& ZFS_IMPORT_ONLY
) &&
2375 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2385 * zpool import [-d dir] [-D]
2386 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2387 * [-d dir | -c cachefile] [-f] -a
2388 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
2389 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2391 * -c Read pool information from a cachefile instead of searching
2394 * -d Scan in a specific directory, other than /dev/. More than
2395 * one directory can be specified using multiple '-d' options.
2397 * -D Scan for previously destroyed pools or import all or only
2398 * specified destroyed pools.
2400 * -R Temporarily import the pool, with all mountpoints relative to
2401 * the given root. The pool will remain exported when the machine
2404 * -V Import even in the presence of faulted vdevs. This is an
2405 * intentionally undocumented option for testing purposes, and
2406 * treats the pool configuration as complete, leaving any bad
2407 * vdevs in the FAULTED state. In other words, it does verbatim
2410 * -f Force import, even if it appears that the pool is active.
2412 * -F Attempt rewind if necessary.
2414 * -n See if rewind would work, but don't actually rewind.
2416 * -N Import the pool but don't mount datasets.
2418 * -T Specify a starting txg to use for import. This option is
2419 * intentionally undocumented option for testing purposes.
2421 * -a Import all pools found.
2423 * -l Load encryption keys while importing.
2425 * -o Set property=value and/or temporary mount options (without '=').
2427 * -s Scan using the default search path, the libblkid cache will
2430 * The import command scans for pools to import, and import pools based on pool
2431 * name and GUID. The pool can also be renamed as part of the import process.
2434 zpool_do_import(int argc
, char **argv
)
2436 char **searchdirs
= NULL
;
2437 char *env
, *envdup
= NULL
;
2441 nvlist_t
*pools
= NULL
;
2442 boolean_t do_all
= B_FALSE
;
2443 boolean_t do_destroyed
= B_FALSE
;
2444 char *mntopts
= NULL
;
2447 uint64_t searchguid
= 0;
2448 char *searchname
= NULL
;
2450 nvlist_t
*found_config
;
2451 nvlist_t
*policy
= NULL
;
2452 nvlist_t
*props
= NULL
;
2454 int flags
= ZFS_IMPORT_NORMAL
;
2455 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2456 boolean_t dryrun
= B_FALSE
;
2457 boolean_t do_rewind
= B_FALSE
;
2458 boolean_t xtreme_rewind
= B_FALSE
;
2459 boolean_t do_scan
= B_FALSE
;
2460 uint64_t pool_state
, txg
= -1ULL;
2461 char *cachefile
= NULL
;
2462 importargs_t idata
= { 0 };
2466 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX")) != -1) {
2475 if (searchdirs
== NULL
) {
2476 searchdirs
= safe_malloc(sizeof (char *));
2478 char **tmp
= safe_malloc((nsearch
+ 1) *
2480 bcopy(searchdirs
, tmp
, nsearch
*
2485 searchdirs
[nsearch
++] = optarg
;
2488 do_destroyed
= B_TRUE
;
2491 flags
|= ZFS_IMPORT_ANY_HOST
;
2497 flags
|= ZFS_IMPORT_LOAD_KEYS
;
2500 flags
|= ZFS_IMPORT_MISSING_LOG
;
2506 flags
|= ZFS_IMPORT_ONLY
;
2509 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2512 if (add_prop_list(optarg
, propval
,
2520 if (add_prop_list(zpool_prop_to_name(
2521 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2523 if (add_prop_list_default(zpool_prop_to_name(
2524 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2531 flags
|= ZFS_IMPORT_TEMP_NAME
;
2532 if (add_prop_list_default(zpool_prop_to_name(
2533 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2539 txg
= strtoull(optarg
, &endptr
, 0);
2540 if (errno
!= 0 || *endptr
!= '\0') {
2541 (void) fprintf(stderr
,
2542 gettext("invalid txg value\n"));
2545 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2548 flags
|= ZFS_IMPORT_VERBATIM
;
2551 xtreme_rewind
= B_TRUE
;
2554 (void) fprintf(stderr
, gettext("missing argument for "
2555 "'%c' option\n"), optopt
);
2559 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2568 if (cachefile
&& nsearch
!= 0) {
2569 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2573 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
2574 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
2578 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
2579 (void) fprintf(stderr
, gettext("-l is only meaningful during "
2584 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2585 (void) fprintf(stderr
,
2586 gettext("-n or -X only meaningful with -F\n"));
2590 rewind_policy
= ZPOOL_TRY_REWIND
;
2592 rewind_policy
= ZPOOL_DO_REWIND
;
2594 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2596 /* In the future, we can capture further policy and include it here */
2597 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2598 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2599 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2602 /* check argument count */
2605 (void) fprintf(stderr
, gettext("too many arguments\n"));
2610 (void) fprintf(stderr
, gettext("too many arguments\n"));
2616 * Check for the effective uid. We do this explicitly here because
2617 * otherwise any attempt to discover pools will silently fail.
2619 if (argc
== 0 && geteuid() != 0) {
2620 (void) fprintf(stderr
, gettext("cannot "
2621 "discover pools: permission denied\n"));
2622 if (searchdirs
!= NULL
)
2626 nvlist_free(policy
);
2631 * Depending on the arguments given, we do one of the following:
2633 * <none> Iterate through all pools and display information about
2636 * -a Iterate through all pools and try to import each one.
2638 * <id> Find the pool that corresponds to the given GUID/pool
2639 * name and import that one.
2641 * -D Above options applies only to destroyed pools.
2647 searchguid
= strtoull(argv
[0], &endptr
, 10);
2648 if (errno
!= 0 || *endptr
!= '\0') {
2649 searchname
= argv
[0];
2652 found_config
= NULL
;
2655 * User specified a name or guid. Ensure it's unique.
2657 idata
.unique
= B_TRUE
;
2661 * Check the environment for the preferred search path.
2663 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2666 envdup
= strdup(env
);
2668 dir
= strtok(envdup
, ":");
2669 while (dir
!= NULL
) {
2670 if (searchdirs
== NULL
) {
2671 searchdirs
= safe_malloc(sizeof (char *));
2673 char **tmp
= safe_malloc((nsearch
+ 1) *
2675 bcopy(searchdirs
, tmp
, nsearch
*
2680 searchdirs
[nsearch
++] = dir
;
2681 dir
= strtok(NULL
, ":");
2685 idata
.path
= searchdirs
;
2686 idata
.paths
= nsearch
;
2687 idata
.poolname
= searchname
;
2688 idata
.guid
= searchguid
;
2689 idata
.cachefile
= cachefile
;
2690 idata
.scan
= do_scan
;
2692 pools
= zpool_search_import(g_zfs
, &idata
);
2694 if (pools
!= NULL
&& idata
.exists
&&
2695 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2696 (void) fprintf(stderr
, gettext("cannot import '%s': "
2697 "a pool with that name already exists\n"),
2699 (void) fprintf(stderr
, gettext("use the form '%s "
2700 "<pool | id> <newpool>' to give it a new name\n"),
2703 } else if (pools
== NULL
&& idata
.exists
) {
2704 (void) fprintf(stderr
, gettext("cannot import '%s': "
2705 "a pool with that name is already created/imported,\n"),
2707 (void) fprintf(stderr
, gettext("and no additional pools "
2708 "with that name were found\n"));
2710 } else if (pools
== NULL
) {
2712 (void) fprintf(stderr
, gettext("cannot import '%s': "
2713 "no such pool available\n"), argv
[0]);
2719 if (searchdirs
!= NULL
)
2723 nvlist_free(policy
);
2730 * At this point we have a list of import candidate configs. Even if
2731 * we were searching by pool name or guid, we still need to
2732 * post-process the list to deal with pool state and possible
2738 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2740 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2742 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2744 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2746 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2749 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2756 (void) printf("\n");
2759 err
|= do_import(config
, NULL
, mntopts
,
2762 show_import(config
);
2764 } else if (searchname
!= NULL
) {
2768 * We are searching for a pool based on name.
2770 verify(nvlist_lookup_string(config
,
2771 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2773 if (strcmp(name
, searchname
) == 0) {
2774 if (found_config
!= NULL
) {
2775 (void) fprintf(stderr
, gettext(
2776 "cannot import '%s': more than "
2777 "one matching pool\n"), searchname
);
2778 (void) fprintf(stderr
, gettext(
2779 "import by numeric ID instead\n"));
2782 found_config
= config
;
2788 * Search for a pool by guid.
2790 verify(nvlist_lookup_uint64(config
,
2791 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2793 if (guid
== searchguid
)
2794 found_config
= config
;
2799 * If we were searching for a specific pool, verify that we found a
2800 * pool, and then do the import.
2802 if (argc
!= 0 && err
== 0) {
2803 if (found_config
== NULL
) {
2804 (void) fprintf(stderr
, gettext("cannot import '%s': "
2805 "no such pool available\n"), argv
[0]);
2808 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2809 argv
[1], mntopts
, props
, flags
);
2814 * If we were just looking for pools, report an error if none were
2817 if (argc
== 0 && first
)
2818 (void) fprintf(stderr
,
2819 gettext("no pools available to import\n"));
2824 nvlist_free(policy
);
2825 if (searchdirs
!= NULL
)
2830 return (err
? 1 : 0);
2834 * zpool sync [-f] [pool] ...
2836 * -f (undocumented) force uberblock (and config including zpool cache file)
2839 * Sync the specified pool(s).
2840 * Without arguments "zpool sync" will sync all pools.
2841 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2845 zpool_do_sync(int argc
, char **argv
)
2848 boolean_t force
= B_FALSE
;
2851 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
2857 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2866 /* if argc == 0 we will execute zpool_sync_one on all pools */
2867 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
2872 typedef struct iostat_cbdata
{
2877 char **cb_vdev_names
; /* Only show these vdevs */
2878 unsigned int cb_vdev_names_count
;
2879 boolean_t cb_verbose
;
2880 boolean_t cb_literal
;
2881 boolean_t cb_scripted
;
2882 zpool_list_t
*cb_list
;
2883 vdev_cmd_data_list_t
*vcdl
;
2887 typedef struct name_and_columns
{
2888 const char *name
; /* Column name */
2889 unsigned int columns
; /* Center name to this number of columns */
2890 } name_and_columns_t
;
2892 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2894 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
2896 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2898 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2899 {"asyncq_wait", 2}, {"scrub"}},
2900 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
2901 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2903 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
2904 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
2905 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
2906 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
2910 /* Shorthand - if "columns" field not set, default to 1 column */
2911 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
2913 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2915 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2916 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
2917 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2918 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
2919 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2920 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
2921 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2922 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
2925 static const char *histo_to_title
[] = {
2926 [IOS_L_HISTO
] = "latency",
2927 [IOS_RQ_HISTO
] = "req_size",
2931 * Return the number of labels in a null-terminated name_and_columns_t
2936 label_array_len(const name_and_columns_t
*labels
)
2940 while (labels
[i
].name
)
2947 * Return the number of strings in a null-terminated string array.
2950 * const char foo[] = {"bar", "baz", NULL}
2955 str_array_len(const char *array
[])
2966 * Return a default column width for default/latency/queue columns. This does
2967 * not include histograms, which have their columns autosized.
2970 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
2972 unsigned long column_width
= 5; /* Normal niceprint */
2973 static unsigned long widths
[] = {
2975 * Choose some sane default column sizes for printing the
2978 [IOS_DEFAULT
] = 15, /* 1PB capacity */
2979 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
2980 [IOS_QUEUES
] = 6, /* 1M queue entries */
2984 column_width
= widths
[type
];
2986 return (column_width
);
2990 * Print the column labels, i.e:
2992 * capacity operations bandwidth
2993 * alloc free read write read write ...
2995 * If force_column_width is set, use it for the column width. If not set, use
2996 * the default column width.
2999 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3000 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3003 unsigned int text_start
, rw_column_width
, spaces_to_end
;
3004 uint64_t flags
= cb
->cb_flags
;
3006 unsigned int column_width
= force_column_width
;
3008 /* For each bit set in flags */
3009 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3010 idx
= lowbit64(f
) - 1;
3011 if (!force_column_width
)
3012 column_width
= default_column_width(cb
, idx
);
3013 /* Print our top labels centered over "read write" label. */
3014 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3015 const char *name
= labels
[idx
][i
].name
;
3017 * We treat labels[][].columns == 0 as shorthand
3018 * for one column. It makes writing out the label
3019 * tables more concise.
3021 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3022 unsigned int slen
= strlen(name
);
3024 rw_column_width
= (column_width
* columns
) +
3025 (2 * (columns
- 1));
3027 text_start
= (int)((rw_column_width
)/columns
-
3030 printf(" "); /* Two spaces between columns */
3032 /* Space from beginning of column to label */
3033 for (s
= 0; s
< text_start
; s
++)
3038 /* Print space after label to end of column */
3039 spaces_to_end
= rw_column_width
- text_start
- slen
;
3040 for (s
= 0; s
< spaces_to_end
; s
++)
3049 * print_cmd_columns - Print custom column titles from -c
3051 * If the user specified the "zpool status|iostat -c" then print their custom
3052 * column titles in the header. For example, print_cmd_columns() would print
3053 * the " col1 col2" part of this:
3055 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3057 * capacity operations bandwidth
3058 * pool alloc free read write read write col1 col2
3059 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3060 * mypool 269K 1008M 0 0 107 946
3061 * mirror 269K 1008M 0 0 107 946
3062 * sdb - - 0 0 102 473 val1 val2
3063 * sdc - - 0 0 5 473 val1 val2
3064 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3067 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3070 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3072 if (vcdl
->count
== 0 || data
== NULL
)
3076 * Each vdev cmd should have the same column names unless the user did
3077 * something weird with their cmd. Just take the column names from the
3078 * first vdev and assume it works for all of them.
3080 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3083 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3086 printf("%*s", vcdl
->uniq_cols_width
[i
],
3087 vcdl
->uniq_cols
[i
]);
3094 * Utility function to print out a line of dashes like:
3096 * -------------------------------- ----- ----- ----- ----- -----
3098 * ...or a dashed named-row line like:
3104 * @force_column_width If non-zero, use the value as the column width.
3105 * Otherwise use the default column widths.
3107 * @name: Print a dashed named-row line starting
3108 * with @name. Otherwise, print a regular
3112 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3116 unsigned int namewidth
;
3117 uint64_t flags
= cb
->cb_flags
;
3120 const name_and_columns_t
*labels
;
3124 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3125 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3126 } else if (cb
->cb_vdev_names_count
) {
3132 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3133 name
? strlen(name
) : 0);
3137 printf("%-*s", namewidth
, name
);
3139 for (i
= 0; i
< namewidth
; i
++)
3143 /* For each bit in flags */
3144 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3145 unsigned int column_width
;
3146 idx
= lowbit64(f
) - 1;
3147 if (force_column_width
)
3148 column_width
= force_column_width
;
3150 column_width
= default_column_width(cb
, idx
);
3152 labels
= iostat_bottom_labels
[idx
];
3153 for (i
= 0; i
< label_array_len(labels
); i
++) {
3155 printf(" %*s-", column_width
- 1, " ");
3157 printf(" %.*s", column_width
,
3158 "--------------------");
3165 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3166 unsigned int force_column_width
)
3168 print_iostat_dashes(cb
, force_column_width
, NULL
);
3172 print_iostat_separator(iostat_cbdata_t
*cb
)
3174 print_iostat_separator_impl(cb
, 0);
3178 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3179 const char *histo_vdev_name
)
3181 unsigned int namewidth
;
3184 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3185 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3186 } else if (cb
->cb_vdev_names_count
) {
3192 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3193 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3195 if (histo_vdev_name
)
3196 printf("%-*s", namewidth
, histo_vdev_name
);
3198 printf("%*s", namewidth
, "");
3201 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3204 printf("%-*s", namewidth
, title
);
3206 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3207 if (cb
->vcdl
!= NULL
)
3208 print_cmd_columns(cb
->vcdl
, 0);
3212 print_iostat_separator_impl(cb
, force_column_width
);
3214 if (cb
->vcdl
!= NULL
)
3215 print_cmd_columns(cb
->vcdl
, 1);
3221 print_iostat_header(iostat_cbdata_t
*cb
)
3223 print_iostat_header_impl(cb
, 0, NULL
);
3228 * Display a single statistic.
3231 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3232 unsigned int column_size
, boolean_t scripted
)
3236 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3239 printf("\t%s", buf
);
3241 printf(" %*s", column_size
, buf
);
3245 * Calculate the default vdev stats
3247 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3248 * stats into calcvs.
3251 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3252 vdev_stat_t
*calcvs
)
3256 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3257 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3258 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3260 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3261 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
3265 * Internal representation of the extended iostats data.
3267 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3268 * or single uint64_t's. We make both look like arrays to make them easier
3269 * to process. In order to make single uint64_t's look like arrays, we set
3270 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3271 * we can just use *data and count.
3275 uint_t count
; /* Number of entries in data[] */
3276 uint64_t __data
; /* Only used when data is a single uint64_t */
3280 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
3284 for (i
= 0; i
< len
; i
++)
3285 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
3291 * Helper function to lookup a uint64_t array or uint64_t value and store its
3292 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3293 * it look like a one element array to make it easier to process.
3296 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
3297 struct stat_array
*nva
)
3302 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
3303 switch (nvpair_type(tmp
)) {
3304 case DATA_TYPE_UINT64_ARRAY
:
3305 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
3307 case DATA_TYPE_UINT64
:
3308 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
3309 nva
->data
= &nva
->__data
;
3313 /* Not a uint64_t */
3322 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3323 * subtract them, and return the results in a newly allocated stat_array.
3324 * You must free the returned array after you are done with it with
3325 * free_calc_stats().
3327 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3330 static struct stat_array
*
3331 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3334 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3335 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3337 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3339 /* Extract our extended stats nvlist from the main list */
3340 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3343 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3347 newnva
= safe_malloc(alloc_size
);
3348 oldnva
= safe_malloc(alloc_size
);
3349 calcnva
= safe_malloc(alloc_size
);
3351 for (j
= 0; j
< len
; j
++) {
3352 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3354 calcnva
[j
].count
= newnva
[j
].count
;
3355 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3356 calcnva
[j
].data
= safe_malloc(alloc_size
);
3357 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3360 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3362 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3363 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3372 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3375 for (i
= 0; i
< len
; i
++)
3382 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3383 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3389 enum zfs_nicenum_format format
;
3390 unsigned int buckets
;
3391 unsigned int start_bucket
;
3394 format
= ZFS_NICENUM_RAW
;
3396 format
= ZFS_NICENUM_1024
;
3398 /* All these histos are the same size, so just use nva[0].count */
3399 buckets
= nva
[0].count
;
3401 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3402 /* Start at 512 - req size should never be lower than this */
3408 for (j
= start_bucket
; j
< buckets
; j
++) {
3409 /* Print histogram bucket label */
3410 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3411 /* Ending range of this bucket */
3412 val
= (1UL << (j
+ 1)) - 1;
3413 zfs_nicetime(val
, buf
, sizeof (buf
));
3415 /* Request size (starting range of bucket) */
3417 zfs_nicenum(val
, buf
, sizeof (buf
));
3420 if (cb
->cb_scripted
)
3421 printf("%llu", (u_longlong_t
)val
);
3423 printf("%-*s", namewidth
, buf
);
3425 /* Print the values on the line */
3426 for (i
= 0; i
< len
; i
++) {
3427 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3428 column_width
, cb
->cb_scripted
);
3435 print_solid_separator(unsigned int length
)
3443 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3444 nvlist_t
*newnv
, double scale
, const char *name
)
3446 unsigned int column_width
;
3447 unsigned int namewidth
;
3448 unsigned int entire_width
;
3449 enum iostat_type type
;
3450 struct stat_array
*nva
;
3452 unsigned int names_len
;
3454 /* What type of histo are we? */
3455 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3457 /* Get NULL-terminated array of nvlist names for our histo */
3458 names
= vsx_type_to_nvlist
[type
];
3459 names_len
= str_array_len(names
); /* num of names */
3461 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3463 if (cb
->cb_literal
) {
3464 column_width
= MAX(5,
3465 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3470 namewidth
= MAX(cb
->cb_namewidth
,
3471 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3474 * Calculate the entire line width of what we're printing. The
3475 * +2 is for the two spaces between columns:
3479 /* |___| <---------- column_width */
3481 /* |__________| <--- entire_width */
3483 entire_width
= namewidth
+ (column_width
+ 2) *
3484 label_array_len(iostat_bottom_labels
[type
]);
3486 if (cb
->cb_scripted
)
3487 printf("%s\n", name
);
3489 print_iostat_header_impl(cb
, column_width
, name
);
3491 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3494 free_calc_stats(nva
, names_len
);
3495 if (!cb
->cb_scripted
)
3496 print_solid_separator(entire_width
);
3500 * Calculate the average latency of a power-of-two latency histogram
3503 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3506 uint64_t count
= 0, total
= 0;
3508 for (i
= 0; i
< buckets
; i
++) {
3510 * Our buckets are power-of-two latency ranges. Use the
3511 * midpoint latency of each bucket to calculate the average.
3519 if (histo
[i
] != 0) {
3520 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3525 /* Prevent divide by zero */
3526 return (count
== 0 ? 0 : total
/ count
);
3530 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3531 nvlist_t
*newnv
, double scale
)
3535 const char *names
[] = {
3536 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3537 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3538 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3539 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3540 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3541 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3542 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3543 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3544 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3545 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3548 struct stat_array
*nva
;
3550 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3551 enum zfs_nicenum_format format
;
3553 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3556 format
= ZFS_NICENUM_RAW
;
3558 format
= ZFS_NICENUM_1024
;
3560 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3561 val
= nva
[i
].data
[0] * scale
;
3562 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3565 free_calc_stats(nva
, ARRAY_SIZE(names
));
3569 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3570 nvlist_t
*newnv
, double scale
)
3574 const char *names
[] = {
3575 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3576 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3577 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3578 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3579 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3580 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3581 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3582 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3583 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3585 struct stat_array
*nva
;
3587 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3588 enum zfs_nicenum_format format
;
3590 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3593 format
= ZFS_NICENUM_RAWTIME
;
3595 format
= ZFS_NICENUM_TIME
;
3597 /* Print our avg latencies on the line */
3598 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3599 /* Compute average latency for a latency histo */
3600 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3601 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3603 free_calc_stats(nva
, ARRAY_SIZE(names
));
3607 * Print default statistics (capacity/operations/bandwidth)
3610 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3612 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3613 enum zfs_nicenum_format format
;
3614 char na
; /* char to print for "not applicable" values */
3616 if (cb
->cb_literal
) {
3617 format
= ZFS_NICENUM_RAW
;
3620 format
= ZFS_NICENUM_1024
;
3624 /* only toplevel vdevs have capacity stats */
3625 if (vs
->vs_space
== 0) {
3626 if (cb
->cb_scripted
)
3627 printf("\t%c\t%c", na
, na
);
3629 printf(" %*c %*c", column_width
, na
, column_width
,
3632 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3634 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3635 column_width
, cb
->cb_scripted
);
3638 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3639 format
, column_width
, cb
->cb_scripted
);
3640 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3641 format
, column_width
, cb
->cb_scripted
);
3642 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3643 format
, column_width
, cb
->cb_scripted
);
3644 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3645 format
, column_width
, cb
->cb_scripted
);
3649 * Print out all the statistics for the given vdev. This can either be the
3650 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3651 * is a verbose output, and we don't want to display the toplevel pool stats.
3653 * Returns the number of stat lines printed.
3656 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3657 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3659 nvlist_t
**oldchild
, **newchild
;
3660 uint_t c
, children
, oldchildren
;
3661 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3662 vdev_stat_t zerovs
= { 0 };
3669 calcvs
= safe_malloc(sizeof (*calcvs
));
3671 if (oldnv
!= NULL
) {
3672 verify(nvlist_lookup_uint64_array(oldnv
,
3673 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3678 /* Do we only want to see a specific vdev? */
3679 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3680 /* Yes we do. Is this the vdev? */
3681 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3683 * This is our vdev. Since it is the only vdev we
3684 * will be displaying, make depth = 0 so that it
3685 * doesn't get indented.
3692 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3693 /* Couldn't match the name */
3698 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3699 (uint64_t **)&newvs
, &c
) == 0);
3702 * Print the vdev name unless it's is a histogram. Histograms
3703 * display the vdev name in the header itself.
3705 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3706 if (cb
->cb_scripted
) {
3709 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3710 (void) printf("%*s%s", depth
, "", name
);
3712 (void) printf("%*s%s%*s", depth
, "", name
,
3713 (int)(cb
->cb_namewidth
- strlen(name
) -
3718 /* Calculate our scaling factor */
3719 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3720 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3722 * If we specify printing histograms with no time interval, then
3723 * print the histogram numbers over the entire lifetime of the
3731 scale
= (double)NANOSEC
/ tdelta
;
3734 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3735 calc_default_iostats(oldvs
, newvs
, calcvs
);
3736 print_iostat_default(calcvs
, cb
, scale
);
3738 if (cb
->cb_flags
& IOS_LATENCY_M
)
3739 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3740 if (cb
->cb_flags
& IOS_QUEUES_M
)
3741 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3742 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3744 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3747 if (cb
->vcdl
!= NULL
) {
3749 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3752 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3756 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3765 if (!cb
->cb_verbose
)
3768 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3769 &newchild
, &children
) != 0)
3773 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3774 &oldchild
, &oldchildren
) != 0)
3777 children
= MIN(oldchildren
, children
);
3780 for (c
= 0; c
< children
; c
++) {
3781 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3783 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3786 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3789 if (ishole
|| islog
)
3792 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3794 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3795 newchild
[c
], cb
, depth
+ 2);
3800 * Log device section
3803 if (num_logs(newnv
) > 0) {
3804 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3805 !cb
->cb_vdev_names
) {
3806 print_iostat_dashes(cb
, 0, "logs");
3810 for (c
= 0; c
< children
; c
++) {
3811 uint64_t islog
= B_FALSE
;
3812 (void) nvlist_lookup_uint64(newchild
[c
],
3813 ZPOOL_CONFIG_IS_LOG
, &islog
);
3816 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3818 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3819 oldchild
[c
] : NULL
, newchild
[c
],
3828 * Include level 2 ARC devices in iostat output
3830 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3831 &newchild
, &children
) != 0)
3835 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
3836 &oldchild
, &oldchildren
) != 0)
3839 children
= MIN(oldchildren
, children
);
3843 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3844 !cb
->cb_vdev_names
) {
3845 print_iostat_dashes(cb
, 0, "cache");
3849 for (c
= 0; c
< children
; c
++) {
3850 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3852 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
3853 : NULL
, newchild
[c
], cb
, depth
+ 2);
3862 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
3864 iostat_cbdata_t
*cb
= data
;
3868 * If the pool has disappeared, remove it from the list and continue.
3870 if (zpool_refresh_stats(zhp
, &missing
) != 0)
3874 pool_list_remove(cb
->cb_list
, zhp
);
3880 * Callback to print out the iostats for the given pool.
3883 print_iostat(zpool_handle_t
*zhp
, void *data
)
3885 iostat_cbdata_t
*cb
= data
;
3886 nvlist_t
*oldconfig
, *newconfig
;
3887 nvlist_t
*oldnvroot
, *newnvroot
;
3890 newconfig
= zpool_get_config(zhp
, &oldconfig
);
3892 if (cb
->cb_iteration
== 1)
3895 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3898 if (oldconfig
== NULL
)
3901 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3904 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
3906 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
3907 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
3908 print_iostat_separator(cb
);
3909 if (cb
->vcdl
!= NULL
) {
3910 print_cmd_columns(cb
->vcdl
, 1);
3925 if (isatty(STDOUT_FILENO
)) {
3926 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
3928 columns
= ws
.ws_col
;
3937 get_namewidth(zpool_handle_t
*zhp
, void *data
)
3939 iostat_cbdata_t
*cb
= data
;
3940 nvlist_t
*config
, *nvroot
;
3943 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
3944 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3946 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
3947 if (!cb
->cb_verbose
)
3948 cb
->cb_namewidth
= MAX(poolname_len
, cb
->cb_namewidth
);
3950 cb
->cb_namewidth
= MAX(poolname_len
,
3951 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
3952 cb
->cb_name_flags
));
3955 * The width must be at least 10, but may be as large as the
3956 * column width - 42 so that we can still fit in one line.
3958 columns
= get_columns();
3960 if (cb
->cb_namewidth
< 10)
3961 cb
->cb_namewidth
= 10;
3962 if (cb
->cb_namewidth
> columns
- 42)
3963 cb
->cb_namewidth
= columns
- 42;
3969 * Parse the input string, get the 'interval' and 'count' value if there is one.
3972 get_interval_count(int *argcp
, char **argv
, float *iv
,
3976 unsigned long count
= 0;
3980 * Determine if the last argument is an integer or a pool name
3982 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3986 interval
= strtof(argv
[argc
- 1], &end
);
3988 if (*end
== '\0' && errno
== 0) {
3989 if (interval
== 0) {
3990 (void) fprintf(stderr
, gettext("interval "
3991 "cannot be zero\n"));
3995 * Ignore the last parameter
4000 * If this is not a valid number, just plow on. The
4001 * user will get a more informative error message later
4009 * If the last argument is also an integer, then we have both a count
4012 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
4017 interval
= strtof(argv
[argc
- 1], &end
);
4019 if (*end
== '\0' && errno
== 0) {
4020 if (interval
== 0) {
4021 (void) fprintf(stderr
, gettext("interval "
4022 "cannot be zero\n"));
4027 * Ignore the last parameter
4041 get_timestamp_arg(char c
)
4044 timestamp_fmt
= UDATE
;
4046 timestamp_fmt
= DDATE
;
4052 * Return stat flags that are supported by all pools by both the module and
4053 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4054 * It will get ANDed down until only the flags that are supported on all pools
4058 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4060 uint64_t *mask
= data
;
4061 nvlist_t
*config
, *nvroot
, *nvx
;
4065 config
= zpool_get_config(zhp
, NULL
);
4066 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4069 /* Default stats are always supported, but for completeness.. */
4070 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4071 flags
|= IOS_DEFAULT_M
;
4073 /* Get our extended stats nvlist from the main list */
4074 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4077 * No extended stats; they're probably running an older
4078 * module. No big deal, we support that too.
4083 /* For each extended stat, make sure all its nvpairs are supported */
4084 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4085 if (!vsx_type_to_nvlist
[j
][0])
4088 /* Start off by assuming the flag is supported, then check */
4089 flags
|= (1ULL << j
);
4090 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4091 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4092 /* flag isn't supported */
4093 flags
= flags
& ~(1ULL << j
);
4099 *mask
= *mask
& flags
;
4104 * Return a bitmask of stats that are supported on all pools by both the module
4108 get_stat_flags(zpool_list_t
*list
)
4113 * get_stat_flags_cb() will lop off bits from "mask" until only the
4114 * flags that are supported on all pools remain.
4116 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4121 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4124 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4126 iostat_cbdata_t
*cb
= cb_data
;
4130 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4132 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4133 ret
= 1; /* match */
4140 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4143 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4145 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4149 * Check if vdevs are in a pool
4151 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4152 * return 0. If pool_name is NULL, then search all pools.
4155 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4156 iostat_cbdata_t
*cb
)
4163 if ((argc
== 0) || !*argv
)
4169 /* Temporarily hijack cb_vdev_names for a second... */
4170 tmp_name
= cb
->cb_vdev_names
;
4172 /* Go though our list of prospective vdev names */
4173 for (i
= 0; i
< argc
; i
++) {
4174 cb
->cb_vdev_names
= argv
+ i
;
4176 /* Is this name a vdev in our pools? */
4177 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4185 cb
->cb_vdev_names
= tmp_name
;
4191 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4194 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4201 * Do we have a pool named *name? If so, return 1, otherwise 0.
4206 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4209 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4211 are_all_pools(int argc
, char **argv
)
4213 if ((argc
== 0) || !*argv
)
4217 if (!is_pool(argv
[argc
]))
4224 * Helper function to print out vdev/pool names we can't resolve. Used for an
4228 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4229 iostat_cbdata_t
*cb
)
4234 for (i
= 0; i
< argc
; i
++) {
4238 str
= gettext("pool");
4239 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
4240 str
= gettext("vdev in this pool");
4241 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
4242 str
= gettext("vdev in another pool");
4244 str
= gettext("unknown");
4246 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
4251 * Same as get_interval_count(), but with additional checks to not misinterpret
4252 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4256 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
4257 unsigned long *count
, iostat_cbdata_t
*cb
)
4259 char **tmpargv
= argv
;
4260 int argc_for_interval
= 0;
4262 /* Is the last arg an interval value? Or a guid? */
4263 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
4265 * The last arg is not a guid, so it's probably an
4268 argc_for_interval
++;
4271 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
4273 * The 2nd to last arg is not a guid, so it's probably
4274 * an interval value.
4276 argc_for_interval
++;
4280 /* Point to our list of possible intervals */
4281 tmpargv
= &argv
[*argc
- argc_for_interval
];
4283 *argc
= *argc
- argc_for_interval
;
4284 get_interval_count(&argc_for_interval
, tmpargv
,
4289 * Floating point sleep(). Allows you to pass in a floating point value for
4295 struct timespec req
;
4296 req
.tv_sec
= floor(sec
);
4297 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
4298 nanosleep(&req
, NULL
);
4302 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4305 * name: Short name of the script ('iostat').
4306 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4309 print_zpool_script_help(char *name
, char *path
)
4311 char *argv
[] = {path
, "-h", NULL
};
4312 char **lines
= NULL
;
4316 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
4318 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
4320 libzfs_free_str_array(lines
, lines_cnt
);
4324 for (int i
= 0; i
< lines_cnt
; i
++)
4325 if (!is_blank_str(lines
[i
]))
4326 printf(" %-14s %s\n", name
, lines
[i
]);
4328 libzfs_free_str_array(lines
, lines_cnt
);
4332 * Go though the zpool status/iostat -c scripts in the user's path, run their
4333 * help option (-h), and print out the results.
4336 print_zpool_dir_scripts(char *dirpath
)
4340 char fullpath
[MAXPATHLEN
];
4341 struct stat dir_stat
;
4343 if ((dir
= opendir(dirpath
)) != NULL
) {
4344 /* print all the files and directories within directory */
4345 while ((ent
= readdir(dir
)) != NULL
) {
4346 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
4348 /* Print the scripts */
4349 if (stat(fullpath
, &dir_stat
) == 0)
4350 if (dir_stat
.st_mode
& S_IXUSR
&&
4351 S_ISREG(dir_stat
.st_mode
))
4352 print_zpool_script_help(ent
->d_name
,
4360 * Print out help text for all zpool status/iostat -c scripts.
4363 print_zpool_script_list(char *subcommand
)
4367 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
4369 sp
= zpool_get_cmd_search_path();
4373 dir
= strtok(sp
, ":");
4374 while (dir
!= NULL
) {
4375 print_zpool_dir_scripts(dir
);
4376 dir
= strtok(NULL
, ":");
4383 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4384 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4385 * [interval [count]]
4387 * -c CMD For each vdev, run command CMD
4388 * -g Display guid for individual vdev name.
4389 * -L Follow links when resolving vdev path name.
4390 * -P Display full path for vdev name.
4391 * -v Display statistics for individual vdevs
4393 * -p Display values in parsable (exact) format.
4394 * -H Scripted mode. Don't display headers, and separate properties
4396 * -l Display average latency
4397 * -q Display queue depths
4398 * -w Display latency histograms
4399 * -r Display request size histogram
4400 * -T Display a timestamp in date(1) or Unix format
4402 * This command can be tricky because we want to be able to deal with pool
4403 * creation/destruction as well as vdev configuration changes. The bulk of this
4404 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4405 * on pool_list_update() to detect the addition of new pools. Configuration
4406 * changes are all handled within libzfs.
4409 zpool_do_iostat(int argc
, char **argv
)
4415 unsigned long count
= 0;
4417 boolean_t verbose
= B_FALSE
;
4418 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4419 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4420 boolean_t omit_since_boot
= B_FALSE
;
4421 boolean_t guid
= B_FALSE
;
4422 boolean_t follow_links
= B_FALSE
;
4423 boolean_t full_name
= B_FALSE
;
4424 iostat_cbdata_t cb
= { 0 };
4427 /* Used for printing error message */
4428 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4429 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4431 uint64_t unsupported_flags
;
4434 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4439 gettext("Can't set -c flag twice\n"));
4443 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
4444 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4445 fprintf(stderr
, gettext(
4446 "Can't run -c, disabled by "
4447 "ZPOOL_SCRIPTS_ENABLED.\n"));
4451 if ((getuid() <= 0 || geteuid() <= 0) &&
4452 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4453 fprintf(stderr
, gettext(
4454 "Can't run -c with root privileges "
4455 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4465 follow_links
= B_TRUE
;
4471 get_timestamp_arg(*optarg
);
4495 omit_since_boot
= B_TRUE
;
4501 if (optopt
== 'c') {
4502 print_zpool_script_list("iostat");
4506 gettext("invalid option '%c'\n"), optopt
);
4515 cb
.cb_literal
= parsable
;
4516 cb
.cb_scripted
= scripted
;
4519 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4521 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4523 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4524 cb
.cb_iteration
= 0;
4525 cb
.cb_namewidth
= 0;
4526 cb
.cb_verbose
= verbose
;
4528 /* Get our interval and count values (if any) */
4530 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4533 get_interval_count(&argc
, argv
, &interval
, &count
);
4537 /* No args, so just print the defaults. */
4538 } else if (are_all_pools(argc
, argv
)) {
4539 /* All the args are pool names */
4540 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4541 /* All the args are vdevs */
4542 cb
.cb_vdev_names
= argv
;
4543 cb
.cb_vdev_names_count
= argc
;
4544 argc
= 0; /* No pools to process */
4545 } else if (are_all_pools(1, argv
)) {
4546 /* The first arg is a pool name */
4547 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4548 /* ...and the rest are vdev names */
4549 cb
.cb_vdev_names
= argv
+ 1;
4550 cb
.cb_vdev_names_count
= argc
- 1;
4551 argc
= 1; /* One pool to process */
4553 fprintf(stderr
, gettext("Expected either a list of "));
4554 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4555 fprintf(stderr
, " \"%s\", ", argv
[0]);
4556 fprintf(stderr
, gettext("but got:\n"));
4557 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4559 fprintf(stderr
, "\n");
4565 * The args don't make sense. The first arg isn't a pool name,
4566 * nor are all the args vdevs.
4568 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4569 fprintf(stderr
, "\n");
4573 if (cb
.cb_vdev_names_count
!= 0) {
4575 * If user specified vdevs, it implies verbose.
4577 cb
.cb_verbose
= B_TRUE
;
4581 * Construct the list of all interesting pools.
4584 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4587 if (pool_list_count(list
) == 0 && argc
!= 0) {
4588 pool_list_free(list
);
4592 if (pool_list_count(list
) == 0 && interval
== 0) {
4593 pool_list_free(list
);
4594 (void) fprintf(stderr
, gettext("no pools available\n"));
4598 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
4599 pool_list_free(list
);
4600 (void) fprintf(stderr
,
4601 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4606 if (l_histo
&& rq_histo
) {
4607 pool_list_free(list
);
4608 (void) fprintf(stderr
,
4609 gettext("Only one of [-r|-w] can be passed at a time\n"));
4615 * Enter the main iostat loop.
4621 * Histograms tables look out of place when you try to display
4622 * them with the other stats, so make a rule that you can only
4623 * print histograms by themselves.
4625 cb
.cb_flags
= IOS_L_HISTO_M
;
4626 } else if (rq_histo
) {
4627 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4629 cb
.cb_flags
= IOS_DEFAULT_M
;
4631 cb
.cb_flags
|= IOS_LATENCY_M
;
4633 cb
.cb_flags
|= IOS_QUEUES_M
;
4637 * See if the module supports all the stats we want to display.
4639 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4640 if (unsupported_flags
) {
4644 gettext("The loaded zfs module doesn't support:"));
4646 /* for each bit set in unsupported_flags */
4647 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4648 idx
= lowbit64(f
) - 1;
4649 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4652 fprintf(stderr
, ". Try running a newer module.\n");
4653 pool_list_free(list
);
4659 if ((npools
= pool_list_count(list
)) == 0)
4660 (void) fprintf(stderr
, gettext("no pools available\n"));
4663 * If this is the first iteration and -y was supplied
4664 * we skip any printing.
4666 boolean_t skip
= (omit_since_boot
&&
4667 cb
.cb_iteration
== 0);
4670 * Refresh all statistics. This is done as an
4671 * explicit step before calculating the maximum name
4672 * width, so that any * configuration changes are
4673 * properly accounted for.
4675 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4679 * Iterate over all pools to determine the maximum width
4680 * for the pool / device name column across all pools.
4682 cb
.cb_namewidth
= 0;
4683 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4686 if (timestamp_fmt
!= NODATE
)
4687 print_timestamp(timestamp_fmt
);
4689 if (cmd
!= NULL
&& cb
.cb_verbose
&&
4690 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
4691 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4692 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
4693 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
4699 * If it's the first time and we're not skipping it,
4700 * or either skip or verbose mode, print the header.
4702 * The histogram code explicitly prints its header on
4703 * every vdev, so skip this for histograms.
4705 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4706 (skip
!= verbose
)) &&
4707 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4709 print_iostat_header(&cb
);
4712 (void) fsleep(interval
);
4717 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4720 * If there's more than one pool, and we're not in
4721 * verbose mode (which prints a separator for us),
4722 * then print a separator.
4724 * In addition, if we're printing specific vdevs then
4725 * we also want an ending separator.
4727 if (((npools
> 1 && !verbose
&&
4728 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4729 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4730 cb
.cb_vdev_names_count
)) &&
4732 print_iostat_separator(&cb
);
4733 if (cb
.vcdl
!= NULL
)
4734 print_cmd_columns(cb
.vcdl
, 1);
4738 if (cb
.vcdl
!= NULL
)
4739 free_vdev_cmd_data_list(cb
.vcdl
);
4744 * Flush the output so that redirection to a file isn't buffered
4747 (void) fflush(stdout
);
4752 if (count
!= 0 && --count
== 0)
4755 (void) fsleep(interval
);
4758 pool_list_free(list
);
4763 typedef struct list_cbdata
{
4764 boolean_t cb_verbose
;
4767 boolean_t cb_scripted
;
4768 zprop_list_t
*cb_proplist
;
4769 boolean_t cb_literal
;
4773 * Given a list of columns to display, output appropriate headers for each one.
4776 print_header(list_cbdata_t
*cb
)
4778 zprop_list_t
*pl
= cb
->cb_proplist
;
4779 char headerbuf
[ZPOOL_MAXPROPLEN
];
4781 boolean_t first
= B_TRUE
;
4782 boolean_t right_justify
;
4785 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4786 width
= pl
->pl_width
;
4787 if (first
&& cb
->cb_verbose
) {
4789 * Reset the width to accommodate the verbose listing
4792 width
= cb
->cb_namewidth
;
4800 right_justify
= B_FALSE
;
4801 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4802 header
= zpool_prop_column_name(pl
->pl_prop
);
4803 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4807 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4808 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4809 headerbuf
[i
] = '\0';
4813 if (pl
->pl_next
== NULL
&& !right_justify
)
4814 (void) printf("%s", header
);
4815 else if (right_justify
)
4816 (void) printf("%*s", (int)width
, header
);
4818 (void) printf("%-*s", (int)width
, header
);
4821 (void) printf("\n");
4825 * Given a pool and a list of properties, print out all the properties according
4826 * to the described layout.
4829 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4831 zprop_list_t
*pl
= cb
->cb_proplist
;
4832 boolean_t first
= B_TRUE
;
4833 char property
[ZPOOL_MAXPROPLEN
];
4835 boolean_t right_justify
;
4838 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4840 width
= pl
->pl_width
;
4841 if (first
&& cb
->cb_verbose
) {
4843 * Reset the width to accommodate the verbose listing
4846 width
= cb
->cb_namewidth
;
4850 if (cb
->cb_scripted
)
4851 (void) printf("\t");
4858 right_justify
= B_FALSE
;
4859 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4860 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
4861 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
4866 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4867 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
4868 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
4869 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
4870 sizeof (property
)) == 0) {
4878 * If this is being called in scripted mode, or if this is the
4879 * last column and it is left-justified, don't include a width
4882 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
4883 (void) printf("%s", propstr
);
4884 else if (right_justify
)
4885 (void) printf("%*s", (int)width
, propstr
);
4887 (void) printf("%-*s", (int)width
, propstr
);
4890 (void) printf("\n");
4894 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
4895 boolean_t valid
, enum zfs_nicenum_format format
)
4899 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
4902 case ZPOOL_PROP_EXPANDSZ
:
4904 (void) strlcpy(propval
, "-", sizeof (propval
));
4906 zfs_nicenum_format(value
, propval
, sizeof (propval
),
4909 case ZPOOL_PROP_FRAGMENTATION
:
4910 if (value
== ZFS_FRAG_INVALID
) {
4911 (void) strlcpy(propval
, "-", sizeof (propval
));
4912 } else if (format
== ZFS_NICENUM_RAW
) {
4913 (void) snprintf(propval
, sizeof (propval
), "%llu",
4914 (unsigned long long)value
);
4916 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4917 (unsigned long long)value
);
4920 case ZPOOL_PROP_CAPACITY
:
4921 if (format
== ZFS_NICENUM_RAW
)
4922 (void) snprintf(propval
, sizeof (propval
), "%llu",
4923 (unsigned long long)value
);
4925 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4926 (unsigned long long)value
);
4929 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
4933 (void) strlcpy(propval
, "-", sizeof (propval
));
4936 (void) printf("\t%s", propval
);
4938 (void) printf(" %*s", (int)width
, propval
);
4942 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
4943 list_cbdata_t
*cb
, int depth
)
4949 boolean_t scripted
= cb
->cb_scripted
;
4950 uint64_t islog
= B_FALSE
;
4951 boolean_t haslog
= B_FALSE
;
4952 char *dashes
= "%-*s - - - - - -\n";
4954 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
4955 (uint64_t **)&vs
, &c
) == 0);
4958 boolean_t toplevel
= (vs
->vs_space
!= 0);
4960 enum zfs_nicenum_format format
;
4963 format
= ZFS_NICENUM_RAW
;
4965 format
= ZFS_NICENUM_1024
;
4968 (void) printf("\t%s", name
);
4969 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
4970 (void) printf("%*s%s", depth
, "", name
);
4972 (void) printf("%*s%s%*s", depth
, "", name
,
4973 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
4976 * Print the properties for the individual vdevs. Some
4977 * properties are only applicable to toplevel vdevs. The
4978 * 'toplevel' boolean value is passed to the print_one_column()
4979 * to indicate that the value is valid.
4981 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
4983 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
4985 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
4986 scripted
, toplevel
, format
);
4987 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
4989 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
4990 vs
->vs_fragmentation
, scripted
,
4991 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
4993 cap
= (vs
->vs_space
== 0) ? 0 :
4994 (vs
->vs_alloc
* 100 / vs
->vs_space
);
4995 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
4997 (void) printf("\n");
5000 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5001 &child
, &children
) != 0)
5004 for (c
= 0; c
< children
; c
++) {
5005 uint64_t ishole
= B_FALSE
;
5007 if (nvlist_lookup_uint64(child
[c
],
5008 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5011 if (nvlist_lookup_uint64(child
[c
],
5012 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
5017 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5019 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5023 if (haslog
== B_TRUE
) {
5024 /* LINTED E_SEC_PRINTF_VAR_FMT */
5025 (void) printf(dashes
, cb
->cb_namewidth
, "log");
5026 for (c
= 0; c
< children
; c
++) {
5027 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
5028 &islog
) != 0 || !islog
)
5030 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5032 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5037 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
5038 &child
, &children
) == 0 && children
> 0) {
5039 /* LINTED E_SEC_PRINTF_VAR_FMT */
5040 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
5041 for (c
= 0; c
< children
; c
++) {
5042 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5044 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5049 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
5050 &children
) == 0 && children
> 0) {
5051 /* LINTED E_SEC_PRINTF_VAR_FMT */
5052 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5053 for (c
= 0; c
< children
; c
++) {
5054 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5056 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5064 * Generic callback function to list a pool.
5067 list_callback(zpool_handle_t
*zhp
, void *data
)
5069 list_cbdata_t
*cbp
= data
;
5073 config
= zpool_get_config(zhp
, NULL
);
5075 print_pool(zhp
, cbp
);
5076 if (!cbp
->cb_verbose
)
5079 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5081 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
5087 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5089 * -g Display guid for individual vdev name.
5090 * -H Scripted mode. Don't display headers, and separate properties
5092 * -L Follow links when resolving vdev path name.
5093 * -o List of properties to display. Defaults to
5094 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5095 * "dedupratio,health,altroot"
5096 * -p Display values in parsable (exact) format.
5097 * -P Display full path for vdev name.
5098 * -T Display a timestamp in date(1) or Unix format
5100 * List all pools in the system, whether or not they're healthy. Output space
5101 * statistics for each one, as well as health status summary.
5104 zpool_do_list(int argc
, char **argv
)
5108 list_cbdata_t cb
= { 0 };
5109 static char default_props
[] =
5110 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5111 "dedupratio,health,altroot";
5112 char *props
= default_props
;
5114 unsigned long count
= 0;
5116 boolean_t first
= B_TRUE
;
5119 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
5122 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5125 cb
.cb_scripted
= B_TRUE
;
5128 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5134 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5137 cb
.cb_literal
= B_TRUE
;
5140 get_timestamp_arg(*optarg
);
5143 cb
.cb_verbose
= B_TRUE
;
5146 (void) fprintf(stderr
, gettext("missing argument for "
5147 "'%c' option\n"), optopt
);
5151 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5160 get_interval_count(&argc
, argv
, &interval
, &count
);
5162 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5166 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
5170 if (pool_list_count(list
) == 0)
5173 if (timestamp_fmt
!= NODATE
)
5174 print_timestamp(timestamp_fmt
);
5176 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
5180 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
5185 if (count
!= 0 && --count
== 0)
5188 pool_list_free(list
);
5189 (void) fsleep(interval
);
5192 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
5193 (void) printf(gettext("no pools available\n"));
5197 pool_list_free(list
);
5198 zprop_free_list(cb
.cb_proplist
);
5203 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
5205 boolean_t force
= B_FALSE
;
5208 char *poolname
, *old_disk
, *new_disk
;
5209 zpool_handle_t
*zhp
;
5210 nvlist_t
*props
= NULL
;
5215 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
5221 if ((propval
= strchr(optarg
, '=')) == NULL
) {
5222 (void) fprintf(stderr
, gettext("missing "
5223 "'=' for -o option\n"));
5229 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
5230 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
5234 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5243 /* get pool name and check number of arguments */
5245 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5252 (void) fprintf(stderr
,
5253 gettext("missing <device> specification\n"));
5261 (void) fprintf(stderr
,
5262 gettext("missing <new_device> specification\n"));
5265 new_disk
= old_disk
;
5275 (void) fprintf(stderr
, gettext("too many arguments\n"));
5279 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
5284 if (zpool_get_config(zhp
, NULL
) == NULL
) {
5285 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
5292 /* unless manually specified use "ashift" pool property (if set) */
5293 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
5296 char strval
[ZPOOL_MAXPROPLEN
];
5298 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
5299 if (src
!= ZPROP_SRC_DEFAULT
) {
5300 (void) sprintf(strval
, "%" PRId32
, intval
);
5301 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
5302 &props
, B_TRUE
) == 0);
5306 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
5308 if (nvroot
== NULL
) {
5314 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
5317 nvlist_free(nvroot
);
5324 * zpool replace [-f] <pool> <device> <new_device>
5326 * -f Force attach, even if <new_device> appears to be in use.
5328 * Replace <device> with <new_device>.
5332 zpool_do_replace(int argc
, char **argv
)
5334 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
5338 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5340 * -f Force attach, even if <new_device> appears to be in use.
5341 * -o Set property=value.
5343 * Attach <new_device> to the mirror containing <device>. If <device> is not
5344 * part of a mirror, then <device> will be transformed into a mirror of
5345 * <device> and <new_device>. In either case, <new_device> will begin life
5346 * with a DTL of [0, now], and will immediately begin to resilver itself.
5349 zpool_do_attach(int argc
, char **argv
)
5351 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
5355 * zpool detach [-f] <pool> <device>
5357 * -f Force detach of <device>, even if DTLs argue against it
5358 * (not supported yet)
5360 * Detach a device from a mirror. The operation will be refused if <device>
5361 * is the last device in the mirror, or if the DTLs indicate that this device
5362 * has the only valid copy of some data.
5366 zpool_do_detach(int argc
, char **argv
)
5369 char *poolname
, *path
;
5370 zpool_handle_t
*zhp
;
5374 while ((c
= getopt(argc
, argv
, "f")) != -1) {
5378 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5387 /* get pool name and check number of arguments */
5389 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5394 (void) fprintf(stderr
,
5395 gettext("missing <device> specification\n"));
5402 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5405 ret
= zpool_vdev_detach(zhp
, path
);
5413 * zpool split [-gLnP] [-o prop=val] ...
5415 * [-R altroot] <pool> <newpool> [<device> ...]
5417 * -g Display guid for individual vdev name.
5418 * -L Follow links when resolving vdev path name.
5419 * -n Do not split the pool, but display the resulting layout if
5420 * it were to be split.
5421 * -o Set property=value, or set mount options.
5422 * -P Display full path for vdev name.
5423 * -R Mount the split-off pool under an alternate root.
5424 * -l Load encryption keys while importing.
5426 * Splits the named pool and gives it the new pool name. Devices to be split
5427 * off may be listed, provided that no more than one device is specified
5428 * per top-level vdev mirror. The newly split pool is left in an exported
5429 * state unless -R is specified.
5431 * Restrictions: the top-level of the pool pool must only be made up of
5432 * mirrors; all devices in the pool must be healthy; no device may be
5433 * undergoing a resilvering operation.
5436 zpool_do_split(int argc
, char **argv
)
5438 char *srcpool
, *newpool
, *propval
;
5439 char *mntopts
= NULL
;
5442 boolean_t loadkeys
= B_FALSE
;
5443 zpool_handle_t
*zhp
;
5444 nvlist_t
*config
, *props
= NULL
;
5446 flags
.dryrun
= B_FALSE
;
5447 flags
.import
= B_FALSE
;
5448 flags
.name_flags
= 0;
5451 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
5454 flags
.name_flags
|= VDEV_NAME_GUID
;
5457 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5460 flags
.import
= B_TRUE
;
5462 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5463 &props
, B_TRUE
) != 0) {
5472 flags
.dryrun
= B_TRUE
;
5475 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5478 if (add_prop_list(optarg
, propval
,
5479 &props
, B_TRUE
) != 0) {
5488 flags
.name_flags
|= VDEV_NAME_PATH
;
5491 (void) fprintf(stderr
, gettext("missing argument for "
5492 "'%c' option\n"), optopt
);
5496 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5503 if (!flags
.import
&& mntopts
!= NULL
) {
5504 (void) fprintf(stderr
, gettext("setting mntopts is only "
5505 "valid when importing the pool\n"));
5509 if (!flags
.import
&& loadkeys
) {
5510 (void) fprintf(stderr
, gettext("loading keys is only "
5511 "valid when importing the pool\n"));
5519 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5523 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5533 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5538 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5539 if (config
== NULL
) {
5543 (void) printf(gettext("would create '%s' with the "
5544 "following layout:\n\n"), newpool
);
5545 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5552 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5553 nvlist_free(config
);
5559 * The split was successful. Now we need to open the new
5560 * pool and import it.
5562 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5563 nvlist_free(config
);
5569 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
5574 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5575 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5577 (void) fprintf(stderr
, gettext("Split was successful, but "
5578 "the datasets could not all be mounted\n"));
5579 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5580 "different altroot\n"), "zpool import");
5583 nvlist_free(config
);
5592 * zpool online <pool> <device> ...
5595 zpool_do_online(int argc
, char **argv
)
5599 zpool_handle_t
*zhp
;
5601 vdev_state_t newstate
;
5605 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5608 flags
|= ZFS_ONLINE_EXPAND
;
5612 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5621 /* get pool name and check number of arguments */
5623 (void) fprintf(stderr
, gettext("missing pool name\n"));
5627 (void) fprintf(stderr
, gettext("missing device name\n"));
5633 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5636 for (i
= 1; i
< argc
; i
++) {
5637 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5638 if (newstate
!= VDEV_STATE_HEALTHY
) {
5639 (void) printf(gettext("warning: device '%s' "
5640 "onlined, but remains in faulted state\n"),
5642 if (newstate
== VDEV_STATE_FAULTED
)
5643 (void) printf(gettext("use 'zpool "
5644 "clear' to restore a faulted "
5647 (void) printf(gettext("use 'zpool "
5648 "replace' to replace devices "
5649 "that are no longer present\n"));
5662 * zpool offline [-ft] <pool> <device> ...
5664 * -f Force the device into a faulted state.
5666 * -t Only take the device off-line temporarily. The offline/faulted
5667 * state will not be persistent across reboots.
5671 zpool_do_offline(int argc
, char **argv
)
5675 zpool_handle_t
*zhp
;
5677 boolean_t istmp
= B_FALSE
;
5678 boolean_t fault
= B_FALSE
;
5681 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5690 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5699 /* get pool name and check number of arguments */
5701 (void) fprintf(stderr
, gettext("missing pool name\n"));
5705 (void) fprintf(stderr
, gettext("missing device name\n"));
5711 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5714 for (i
= 1; i
< argc
; i
++) {
5716 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
5718 if (istmp
== B_FALSE
) {
5719 /* Force the fault to persist across imports */
5720 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
5722 aux
= VDEV_AUX_EXTERNAL
;
5725 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
5728 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5739 * zpool clear <pool> [device]
5741 * Clear all errors associated with a pool or a particular device.
5744 zpool_do_clear(int argc
, char **argv
)
5748 boolean_t dryrun
= B_FALSE
;
5749 boolean_t do_rewind
= B_FALSE
;
5750 boolean_t xtreme_rewind
= B_FALSE
;
5751 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5752 nvlist_t
*policy
= NULL
;
5753 zpool_handle_t
*zhp
;
5754 char *pool
, *device
;
5757 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5766 xtreme_rewind
= B_TRUE
;
5769 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5779 (void) fprintf(stderr
, gettext("missing pool name\n"));
5784 (void) fprintf(stderr
, gettext("too many arguments\n"));
5788 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5789 (void) fprintf(stderr
,
5790 gettext("-n or -X only meaningful with -F\n"));
5794 rewind_policy
= ZPOOL_TRY_REWIND
;
5796 rewind_policy
= ZPOOL_DO_REWIND
;
5798 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5800 /* In future, further rewind policy choices can be passed along here */
5801 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5802 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
5806 device
= argc
== 2 ? argv
[1] : NULL
;
5808 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5809 nvlist_free(policy
);
5813 if (zpool_clear(zhp
, device
, policy
) != 0)
5818 nvlist_free(policy
);
5824 * zpool reguid <pool>
5827 zpool_do_reguid(int argc
, char **argv
)
5831 zpool_handle_t
*zhp
;
5835 while ((c
= getopt(argc
, argv
, "")) != -1) {
5838 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5847 /* get pool name and check number of arguments */
5849 (void) fprintf(stderr
, gettext("missing pool name\n"));
5854 (void) fprintf(stderr
, gettext("too many arguments\n"));
5859 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5862 ret
= zpool_reguid(zhp
);
5870 * zpool reopen <pool>
5872 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5875 zpool_do_reopen(int argc
, char **argv
)
5879 boolean_t scrub_restart
= B_TRUE
;
5882 while ((c
= getopt(argc
, argv
, "n")) != -1) {
5885 scrub_restart
= B_FALSE
;
5888 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5897 /* if argc == 0 we will execute zpool_reopen_one on all pools */
5898 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_reopen_one
,
5904 typedef struct scrub_cbdata
{
5908 pool_scrub_cmd_t cb_scrub_cmd
;
5912 scrub_callback(zpool_handle_t
*zhp
, void *data
)
5914 scrub_cbdata_t
*cb
= data
;
5918 * Ignore faulted pools.
5920 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5921 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
5922 "currently unavailable\n"), zpool_get_name(zhp
));
5926 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
5932 * zpool scrub [-s | -p] <pool> ...
5934 * -s Stop. Stops any in-progress scrub.
5935 * -p Pause. Pause in-progress scrub.
5938 zpool_do_scrub(int argc
, char **argv
)
5943 cb
.cb_type
= POOL_SCAN_SCRUB
;
5944 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
5947 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
5950 cb
.cb_type
= POOL_SCAN_NONE
;
5953 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
5956 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5962 if (cb
.cb_type
== POOL_SCAN_NONE
&&
5963 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
5964 (void) fprintf(stderr
, gettext("invalid option combination: "
5965 "-s and -p are mutually exclusive\n"));
5975 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5979 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
5983 * Print out detailed scrub status.
5986 print_scan_status(pool_scan_stat_t
*ps
)
5988 time_t start
, end
, pause
;
5989 uint64_t total_secs_left
;
5990 uint64_t elapsed
, secs_left
, mins_left
, hours_left
, days_left
;
5991 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
5992 uint64_t scan_rate
, issue_rate
;
5993 double fraction_done
;
5994 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
5995 char srate_buf
[7], irate_buf
[7];
5997 (void) printf(gettext(" scan: "));
5999 /* If there's never been a scan, there's not much to say. */
6000 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
6001 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
6002 (void) printf(gettext("none requested\n"));
6006 start
= ps
->pss_start_time
;
6007 end
= ps
->pss_end_time
;
6008 pause
= ps
->pss_pass_scrub_pause
;
6010 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
6012 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
6013 ps
->pss_func
== POOL_SCAN_RESILVER
);
6015 /* Scan is finished or canceled. */
6016 if (ps
->pss_state
== DSS_FINISHED
) {
6017 total_secs_left
= end
- start
;
6018 days_left
= total_secs_left
/ 60 / 60 / 24;
6019 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6020 mins_left
= (total_secs_left
/ 60) % 60;
6021 secs_left
= (total_secs_left
% 60);
6023 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6024 (void) printf(gettext("scrub repaired %s "
6025 "in %llu days %02llu:%02llu:%02llu "
6026 "with %llu errors on %s"), processed_buf
,
6027 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6028 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6029 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6030 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6031 (void) printf(gettext("resilvered %s "
6032 "in %llu days %02llu:%02llu:%02llu "
6033 "with %llu errors on %s"), processed_buf
,
6034 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6035 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
6036 (u_longlong_t
)ps
->pss_errors
, ctime(&end
));
6039 } else if (ps
->pss_state
== DSS_CANCELED
) {
6040 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6041 (void) printf(gettext("scrub canceled on %s"),
6043 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6044 (void) printf(gettext("resilver canceled on %s"),
6050 assert(ps
->pss_state
== DSS_SCANNING
);
6052 /* Scan is in progress. Resilvers can't be paused. */
6053 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6055 (void) printf(gettext("scrub in progress since %s"),
6058 (void) printf(gettext("scrub paused since %s"),
6060 (void) printf(gettext("\tscrub started on %s"),
6063 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6064 (void) printf(gettext("resilver in progress since %s"),
6068 scanned
= ps
->pss_examined
;
6069 pass_scanned
= ps
->pss_pass_exam
;
6070 issued
= ps
->pss_issued
;
6071 pass_issued
= ps
->pss_pass_issued
;
6072 total
= ps
->pss_to_examine
;
6074 /* we are only done with a block once we have issued the IO for it */
6075 fraction_done
= (double)issued
/ total
;
6077 /* elapsed time for this pass, rounding up to 1 if it's 0 */
6078 elapsed
= time(NULL
) - ps
->pss_pass_start
;
6079 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
6080 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
6082 scan_rate
= pass_scanned
/ elapsed
;
6083 issue_rate
= pass_issued
/ elapsed
;
6084 total_secs_left
= (issue_rate
!= 0) ?
6085 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
6087 days_left
= total_secs_left
/ 60 / 60 / 24;
6088 hours_left
= (total_secs_left
/ 60 / 60) % 24;
6089 mins_left
= (total_secs_left
/ 60) % 60;
6090 secs_left
= (total_secs_left
% 60);
6092 /* format all of the numbers we will be reporting */
6093 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
6094 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
6095 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
6096 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
6097 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
6099 /* do not print estimated time if we have a paused scrub */
6101 (void) printf(gettext("\t%s scanned at %s/s, "
6102 "%s issued at %s/s, %s total\n"),
6103 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
6105 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
6106 scanned_buf
, issued_buf
, total_buf
);
6109 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6110 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
6111 processed_buf
, 100 * fraction_done
);
6112 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6113 (void) printf(gettext("\t%s repaired, %.2f%% done"),
6114 processed_buf
, 100 * fraction_done
);
6118 if (issue_rate
>= 10 * 1024 * 1024) {
6119 (void) printf(gettext(", %llu days "
6120 "%02llu:%02llu:%02llu to go\n"),
6121 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
6122 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
);
6124 (void) printf(gettext(", no estimated "
6125 "completion time\n"));
6128 (void) printf(gettext("\n"));
6133 print_error_log(zpool_handle_t
*zhp
)
6135 nvlist_t
*nverrlist
= NULL
;
6138 size_t len
= MAXPATHLEN
* 2;
6140 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
6143 (void) printf("errors: Permanent errors have been "
6144 "detected in the following files:\n\n");
6146 pathname
= safe_malloc(len
);
6148 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
6150 uint64_t dsobj
, obj
;
6152 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
6153 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
6155 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
6157 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
6158 (void) printf("%7s %s\n", "", pathname
);
6161 nvlist_free(nverrlist
);
6165 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
6174 (void) printf(gettext("\tspares\n"));
6176 for (i
= 0; i
< nspares
; i
++) {
6177 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
6179 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
6185 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
6194 (void) printf(gettext("\tcache\n"));
6196 for (i
= 0; i
< nl2cache
; i
++) {
6197 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
6199 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
6205 print_dedup_stats(nvlist_t
*config
)
6207 ddt_histogram_t
*ddh
;
6211 char dspace
[6], mspace
[6];
6214 * If the pool was faulted then we may not have been able to
6215 * obtain the config. Otherwise, if we have anything in the dedup
6216 * table continue processing the stats.
6218 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
6219 (uint64_t **)&ddo
, &c
) != 0)
6222 (void) printf("\n");
6223 (void) printf(gettext(" dedup: "));
6224 if (ddo
->ddo_count
== 0) {
6225 (void) printf(gettext("no DDT entries\n"));
6229 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
6230 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
6231 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6232 (u_longlong_t
)ddo
->ddo_count
,
6236 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
6237 (uint64_t **)&dds
, &c
) == 0);
6238 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
6239 (uint64_t **)&ddh
, &c
) == 0);
6240 zpool_dump_ddt(dds
, ddh
);
6244 * Display a summary of pool status. Displays a summary such as:
6248 * reason: One or more devices ...
6249 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6255 * When given the '-v' option, we print out the complete config. If the '-e'
6256 * option is specified, then we print out error rate information as well.
6259 status_callback(zpool_handle_t
*zhp
, void *data
)
6261 status_cbdata_t
*cbp
= data
;
6262 nvlist_t
*config
, *nvroot
;
6264 zpool_status_t reason
;
6265 zpool_errata_t errata
;
6270 config
= zpool_get_config(zhp
, NULL
);
6271 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
6276 * If we were given 'zpool status -x', only report those pools with
6279 if (cbp
->cb_explain
&&
6280 (reason
== ZPOOL_STATUS_OK
||
6281 reason
== ZPOOL_STATUS_VERSION_OLDER
||
6282 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
6283 if (!cbp
->cb_allpools
) {
6284 (void) printf(gettext("pool '%s' is healthy\n"),
6285 zpool_get_name(zhp
));
6287 cbp
->cb_first
= B_FALSE
;
6293 cbp
->cb_first
= B_FALSE
;
6295 (void) printf("\n");
6297 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6299 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
6300 (uint64_t **)&vs
, &c
) == 0);
6301 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6303 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
6304 (void) printf(gettext(" state: %s\n"), health
);
6307 case ZPOOL_STATUS_MISSING_DEV_R
:
6308 (void) printf(gettext("status: One or more devices could not "
6309 "be opened. Sufficient replicas exist for\n\tthe pool to "
6310 "continue functioning in a degraded state.\n"));
6311 (void) printf(gettext("action: Attach the missing device and "
6312 "online it using 'zpool online'.\n"));
6315 case ZPOOL_STATUS_MISSING_DEV_NR
:
6316 (void) printf(gettext("status: One or more devices could not "
6317 "be opened. There are insufficient\n\treplicas for the "
6318 "pool to continue functioning.\n"));
6319 (void) printf(gettext("action: Attach the missing device and "
6320 "online it using 'zpool online'.\n"));
6323 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
6324 (void) printf(gettext("status: One or more devices could not "
6325 "be used because the label is missing or\n\tinvalid. "
6326 "Sufficient replicas exist for the pool to continue\n\t"
6327 "functioning in a degraded state.\n"));
6328 (void) printf(gettext("action: Replace the device using "
6329 "'zpool replace'.\n"));
6332 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
6333 (void) printf(gettext("status: One or more devices could not "
6334 "be used because the label is missing \n\tor invalid. "
6335 "There are insufficient replicas for the pool to "
6336 "continue\n\tfunctioning.\n"));
6337 zpool_explain_recover(zpool_get_handle(zhp
),
6338 zpool_get_name(zhp
), reason
, config
);
6341 case ZPOOL_STATUS_FAILING_DEV
:
6342 (void) printf(gettext("status: One or more devices has "
6343 "experienced an unrecoverable error. An\n\tattempt was "
6344 "made to correct the error. Applications are "
6346 (void) printf(gettext("action: Determine if the device needs "
6347 "to be replaced, and clear the errors\n\tusing "
6348 "'zpool clear' or replace the device with 'zpool "
6352 case ZPOOL_STATUS_OFFLINE_DEV
:
6353 (void) printf(gettext("status: One or more devices has "
6354 "been taken offline by the administrator.\n\tSufficient "
6355 "replicas exist for the pool to continue functioning in "
6356 "a\n\tdegraded state.\n"));
6357 (void) printf(gettext("action: Online the device using "
6358 "'zpool online' or replace the device with\n\t'zpool "
6362 case ZPOOL_STATUS_REMOVED_DEV
:
6363 (void) printf(gettext("status: One or more devices has "
6364 "been removed by the administrator.\n\tSufficient "
6365 "replicas exist for the pool to continue functioning in "
6366 "a\n\tdegraded state.\n"));
6367 (void) printf(gettext("action: Online the device using "
6368 "'zpool online' or replace the device with\n\t'zpool "
6372 case ZPOOL_STATUS_RESILVERING
:
6373 (void) printf(gettext("status: One or more devices is "
6374 "currently being resilvered. The pool will\n\tcontinue "
6375 "to function, possibly in a degraded state.\n"));
6376 (void) printf(gettext("action: Wait for the resilver to "
6380 case ZPOOL_STATUS_CORRUPT_DATA
:
6381 (void) printf(gettext("status: One or more devices has "
6382 "experienced an error resulting in data\n\tcorruption. "
6383 "Applications may be affected.\n"));
6384 (void) printf(gettext("action: Restore the file in question "
6385 "if possible. Otherwise restore the\n\tentire pool from "
6389 case ZPOOL_STATUS_CORRUPT_POOL
:
6390 (void) printf(gettext("status: The pool metadata is corrupted "
6391 "and the pool cannot be opened.\n"));
6392 zpool_explain_recover(zpool_get_handle(zhp
),
6393 zpool_get_name(zhp
), reason
, config
);
6396 case ZPOOL_STATUS_VERSION_OLDER
:
6397 (void) printf(gettext("status: The pool is formatted using a "
6398 "legacy on-disk format. The pool can\n\tstill be used, "
6399 "but some features are unavailable.\n"));
6400 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6401 "upgrade'. Once this is done, the\n\tpool will no longer "
6402 "be accessible on software that does not support\n\t"
6403 "feature flags.\n"));
6406 case ZPOOL_STATUS_VERSION_NEWER
:
6407 (void) printf(gettext("status: The pool has been upgraded to a "
6408 "newer, incompatible on-disk version.\n\tThe pool cannot "
6409 "be accessed on this system.\n"));
6410 (void) printf(gettext("action: Access the pool from a system "
6411 "running more recent software, or\n\trestore the pool from "
6415 case ZPOOL_STATUS_FEAT_DISABLED
:
6416 (void) printf(gettext("status: Some supported features are not "
6417 "enabled on the pool. The pool can\n\tstill be used, but "
6418 "some features are unavailable.\n"));
6419 (void) printf(gettext("action: Enable all features using "
6420 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6421 "longer be accessible by software that does not support\n\t"
6422 "the features. See zpool-features(5) for details.\n"));
6425 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
6426 (void) printf(gettext("status: The pool cannot be accessed on "
6427 "this system because it uses the\n\tfollowing feature(s) "
6428 "not supported on this system:\n"));
6429 zpool_print_unsup_feat(config
);
6430 (void) printf("\n");
6431 (void) printf(gettext("action: Access the pool from a system "
6432 "that supports the required feature(s),\n\tor restore the "
6433 "pool from backup.\n"));
6436 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
6437 (void) printf(gettext("status: The pool can only be accessed "
6438 "in read-only mode on this system. It\n\tcannot be "
6439 "accessed in read-write mode because it uses the "
6440 "following\n\tfeature(s) not supported on this system:\n"));
6441 zpool_print_unsup_feat(config
);
6442 (void) printf("\n");
6443 (void) printf(gettext("action: The pool cannot be accessed in "
6444 "read-write mode. Import the pool with\n"
6445 "\t\"-o readonly=on\", access the pool from a system that "
6446 "supports the\n\trequired feature(s), or restore the "
6447 "pool from backup.\n"));
6450 case ZPOOL_STATUS_FAULTED_DEV_R
:
6451 (void) printf(gettext("status: One or more devices are "
6452 "faulted in response to persistent errors.\n\tSufficient "
6453 "replicas exist for the pool to continue functioning "
6454 "in a\n\tdegraded state.\n"));
6455 (void) printf(gettext("action: Replace the faulted device, "
6456 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6459 case ZPOOL_STATUS_FAULTED_DEV_NR
:
6460 (void) printf(gettext("status: One or more devices are "
6461 "faulted in response to persistent errors. There are "
6462 "insufficient replicas for the pool to\n\tcontinue "
6464 (void) printf(gettext("action: Destroy and re-create the pool "
6465 "from a backup source. Manually marking the device\n"
6466 "\trepaired using 'zpool clear' may allow some data "
6467 "to be recovered.\n"));
6470 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
6471 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
6472 (void) printf(gettext("status: One or more devices are "
6473 "faulted in response to IO failures.\n"));
6474 (void) printf(gettext("action: Make sure the affected devices "
6475 "are connected, then run 'zpool clear'.\n"));
6478 case ZPOOL_STATUS_BAD_LOG
:
6479 (void) printf(gettext("status: An intent log record "
6480 "could not be read.\n"
6481 "\tWaiting for administrator intervention to fix the "
6482 "faulted pool.\n"));
6483 (void) printf(gettext("action: Either restore the affected "
6484 "device(s) and run 'zpool online',\n"
6485 "\tor ignore the intent log records by running "
6486 "'zpool clear'.\n"));
6489 case ZPOOL_STATUS_HOSTID_MISMATCH
:
6490 (void) printf(gettext("status: Mismatch between pool hostid "
6491 "and system hostid on imported pool.\n\tThis pool was "
6492 "previously imported into a system with a different "
6493 "hostid,\n\tand then was verbatim imported into this "
6495 (void) printf(gettext("action: Export this pool on all systems "
6496 "on which it is imported.\n"
6497 "\tThen import it to correct the mismatch.\n"));
6500 case ZPOOL_STATUS_ERRATA
:
6501 (void) printf(gettext("status: Errata #%d detected.\n"),
6505 case ZPOOL_ERRATA_NONE
:
6508 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
6509 (void) printf(gettext("action: To correct the issue "
6510 "run 'zpool scrub'.\n"));
6513 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
6514 (void) printf(gettext("\tExisting encrypted datasets "
6515 "contain an on-disk incompatibility\n\twhich "
6516 "needs to be corrected.\n"));
6517 (void) printf(gettext("action: To correct the issue "
6518 "backup existing encrypted datasets to new\n\t"
6519 "encrypted datasets and destroy the old ones. "
6520 "'zfs mount -o ro' can\n\tbe used to temporarily "
6521 "mount existing encrypted datasets readonly.\n"));
6526 * All errata which allow the pool to be imported
6527 * must contain an action message.
6535 * The remaining errors can't actually be generated, yet.
6537 assert(reason
== ZPOOL_STATUS_OK
);
6541 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6544 if (config
!= NULL
) {
6546 nvlist_t
**spares
, **l2cache
;
6547 uint_t nspares
, nl2cache
;
6548 pool_scan_stat_t
*ps
= NULL
;
6550 (void) nvlist_lookup_uint64_array(nvroot
,
6551 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
6552 print_scan_status(ps
);
6554 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
6555 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6556 if (cbp
->cb_namewidth
< 10)
6557 cbp
->cb_namewidth
= 10;
6559 (void) printf(gettext("config:\n\n"));
6560 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6561 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6564 if (cbp
->vcdl
!= NULL
)
6565 print_cmd_columns(cbp
->vcdl
, 0);
6568 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6571 if (num_logs(nvroot
) > 0)
6572 print_logs(zhp
, cbp
, nvroot
);
6573 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6574 &l2cache
, &nl2cache
) == 0)
6575 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6577 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6578 &spares
, &nspares
) == 0)
6579 print_spares(zhp
, cbp
, spares
, nspares
);
6581 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6583 nvlist_t
*nverrlist
= NULL
;
6586 * If the approximate error count is small, get a
6587 * precise count by fetching the entire log and
6588 * uniquifying the results.
6590 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6591 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6596 while ((elem
= nvlist_next_nvpair(nverrlist
,
6601 nvlist_free(nverrlist
);
6603 (void) printf("\n");
6606 (void) printf(gettext("errors: No known data "
6608 else if (!cbp
->cb_verbose
)
6609 (void) printf(gettext("errors: %llu data "
6610 "errors, use '-v' for a list\n"),
6611 (u_longlong_t
)nerr
);
6613 print_error_log(zhp
);
6616 if (cbp
->cb_dedup_stats
)
6617 print_dedup_stats(config
);
6619 (void) printf(gettext("config: The configuration cannot be "
6627 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6628 * [interval [count]]
6630 * -c CMD For each vdev, run command CMD
6631 * -g Display guid for individual vdev name.
6632 * -L Follow links when resolving vdev path name.
6633 * -P Display full path for vdev name.
6634 * -v Display complete error logs
6635 * -x Display only pools with potential problems
6636 * -D Display dedup status (undocumented)
6637 * -T Display a timestamp in date(1) or Unix format
6639 * Describes the health status of all pools or some subset.
6642 zpool_do_status(int argc
, char **argv
)
6647 unsigned long count
= 0;
6648 status_cbdata_t cb
= { 0 };
6652 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
6657 gettext("Can't set -c flag twice\n"));
6661 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
6662 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6663 fprintf(stderr
, gettext(
6664 "Can't run -c, disabled by "
6665 "ZPOOL_SCRIPTS_ENABLED.\n"));
6669 if ((getuid() <= 0 || geteuid() <= 0) &&
6670 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6671 fprintf(stderr
, gettext(
6672 "Can't run -c with root privileges "
6673 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6679 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6682 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6685 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6688 cb
.cb_verbose
= B_TRUE
;
6691 cb
.cb_explain
= B_TRUE
;
6694 cb
.cb_dedup_stats
= B_TRUE
;
6697 get_timestamp_arg(*optarg
);
6700 if (optopt
== 'c') {
6701 print_zpool_script_list("status");
6705 gettext("invalid option '%c'\n"), optopt
);
6714 get_interval_count(&argc
, argv
, &interval
, &count
);
6717 cb
.cb_allpools
= B_TRUE
;
6719 cb
.cb_first
= B_TRUE
;
6720 cb
.cb_print_status
= B_TRUE
;
6723 if (timestamp_fmt
!= NODATE
)
6724 print_timestamp(timestamp_fmt
);
6727 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
6730 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
6731 status_callback
, &cb
);
6733 if (cb
.vcdl
!= NULL
)
6734 free_vdev_cmd_data_list(cb
.vcdl
);
6736 if (argc
== 0 && cb
.cb_count
== 0)
6737 (void) fprintf(stderr
, gettext("no pools available\n"));
6738 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
6739 (void) printf(gettext("all pools are healthy\n"));
6747 if (count
!= 0 && --count
== 0)
6750 (void) fsleep(interval
);
6756 typedef struct upgrade_cbdata
{
6759 uint64_t cb_version
;
6764 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
6766 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
6767 int *count
= (int *)unsupp_fs
;
6769 if (zfs_version
> ZPL_VERSION
) {
6770 (void) printf(gettext("%s (v%d) is not supported by this "
6771 "implementation of ZFS.\n"),
6772 zfs_get_name(zhp
), zfs_version
);
6776 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
6784 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
6788 uint64_t oldversion
;
6791 config
= zpool_get_config(zhp
, NULL
);
6792 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6795 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
6796 assert(oldversion
< version
);
6798 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
6803 (void) fprintf(stderr
, gettext("Upgrade not performed due "
6804 "to %d unsupported filesystems (max v%d).\n"),
6805 unsupp_fs
, (int)ZPL_VERSION
);
6809 ret
= zpool_upgrade(zhp
, version
);
6813 if (version
>= SPA_VERSION_FEATURES
) {
6814 (void) printf(gettext("Successfully upgraded "
6815 "'%s' from version %llu to feature flags.\n"),
6816 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
6818 (void) printf(gettext("Successfully upgraded "
6819 "'%s' from version %llu to version %llu.\n"),
6820 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
6821 (u_longlong_t
)version
);
6828 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
6831 boolean_t firstff
= B_TRUE
;
6832 nvlist_t
*enabled
= zpool_get_features(zhp
);
6835 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6836 const char *fname
= spa_feature_table
[i
].fi_uname
;
6837 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6838 if (!nvlist_exists(enabled
, fguid
)) {
6840 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
6841 ret
= zpool_set_prop(zhp
, propname
,
6842 ZFS_FEATURE_ENABLED
);
6850 (void) printf(gettext("Enabled the "
6851 "following features on '%s':\n"),
6852 zpool_get_name(zhp
));
6855 (void) printf(gettext(" %s\n"), fname
);
6866 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
6868 upgrade_cbdata_t
*cbp
= arg
;
6871 boolean_t printnl
= B_FALSE
;
6874 config
= zpool_get_config(zhp
, NULL
);
6875 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6878 assert(SPA_VERSION_IS_SUPPORTED(version
));
6880 if (version
< cbp
->cb_version
) {
6881 cbp
->cb_first
= B_FALSE
;
6882 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6888 * If they did "zpool upgrade -a", then we could
6889 * be doing ioctls to different pools. We need
6890 * to log this history once to each pool, and bypass
6891 * the normal history logging that happens in main().
6893 (void) zpool_log_history(g_zfs
, history_str
);
6894 log_history
= B_FALSE
;
6897 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6899 ret
= upgrade_enable_all(zhp
, &count
);
6904 cbp
->cb_first
= B_FALSE
;
6910 (void) printf(gettext("\n"));
6917 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
6919 upgrade_cbdata_t
*cbp
= arg
;
6923 config
= zpool_get_config(zhp
, NULL
);
6924 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6927 assert(SPA_VERSION_IS_SUPPORTED(version
));
6929 if (version
< SPA_VERSION_FEATURES
) {
6930 if (cbp
->cb_first
) {
6931 (void) printf(gettext("The following pools are "
6932 "formatted with legacy version numbers and can\n"
6933 "be upgraded to use feature flags. After "
6934 "being upgraded, these pools\nwill no "
6935 "longer be accessible by software that does not "
6936 "support feature\nflags.\n\n"));
6937 (void) printf(gettext("VER POOL\n"));
6938 (void) printf(gettext("--- ------------\n"));
6939 cbp
->cb_first
= B_FALSE
;
6942 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
6943 zpool_get_name(zhp
));
6950 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
6952 upgrade_cbdata_t
*cbp
= arg
;
6956 config
= zpool_get_config(zhp
, NULL
);
6957 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6960 if (version
>= SPA_VERSION_FEATURES
) {
6962 boolean_t poolfirst
= B_TRUE
;
6963 nvlist_t
*enabled
= zpool_get_features(zhp
);
6965 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6966 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6967 const char *fname
= spa_feature_table
[i
].fi_uname
;
6968 if (!nvlist_exists(enabled
, fguid
)) {
6969 if (cbp
->cb_first
) {
6970 (void) printf(gettext("\nSome "
6971 "supported features are not "
6972 "enabled on the following pools. "
6973 "Once a\nfeature is enabled the "
6974 "pool may become incompatible with "
6975 "software\nthat does not support "
6977 "zpool-features(5) for "
6979 (void) printf(gettext("POOL "
6981 (void) printf(gettext("------"
6983 cbp
->cb_first
= B_FALSE
;
6987 (void) printf(gettext("%s\n"),
6988 zpool_get_name(zhp
));
6989 poolfirst
= B_FALSE
;
6992 (void) printf(gettext(" %s\n"), fname
);
6995 * If they did "zpool upgrade -a", then we could
6996 * be doing ioctls to different pools. We need
6997 * to log this history once to each pool, and bypass
6998 * the normal history logging that happens in main().
7000 (void) zpool_log_history(g_zfs
, history_str
);
7001 log_history
= B_FALSE
;
7010 upgrade_one(zpool_handle_t
*zhp
, void *data
)
7012 boolean_t printnl
= B_FALSE
;
7013 upgrade_cbdata_t
*cbp
= data
;
7014 uint64_t cur_version
;
7017 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
7018 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
7019 "Pool 'log' must be renamed using export and import"
7024 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
7025 if (cur_version
> cbp
->cb_version
) {
7026 (void) printf(gettext("Pool '%s' is already formatted "
7027 "using more current version '%llu'.\n\n"),
7028 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
7032 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
7033 (void) printf(gettext("Pool '%s' is already formatted "
7034 "using version %llu.\n\n"), zpool_get_name(zhp
),
7035 (u_longlong_t
)cbp
->cb_version
);
7039 if (cur_version
!= cbp
->cb_version
) {
7041 ret
= upgrade_version(zhp
, cbp
->cb_version
);
7046 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
7048 ret
= upgrade_enable_all(zhp
, &count
);
7054 } else if (cur_version
== SPA_VERSION
) {
7055 (void) printf(gettext("Pool '%s' already has all "
7056 "supported features enabled.\n"),
7057 zpool_get_name(zhp
));
7062 (void) printf(gettext("\n"));
7071 * zpool upgrade [-V version] <-a | pool ...>
7073 * With no arguments, display downrev'd ZFS pool available for upgrade.
7074 * Individual pools can be upgraded by specifying the pool, and '-a' will
7075 * upgrade all pools.
7078 zpool_do_upgrade(int argc
, char **argv
)
7081 upgrade_cbdata_t cb
= { 0 };
7083 boolean_t showversions
= B_FALSE
;
7084 boolean_t upgradeall
= B_FALSE
;
7089 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
7092 upgradeall
= B_TRUE
;
7095 showversions
= B_TRUE
;
7098 cb
.cb_version
= strtoll(optarg
, &end
, 10);
7100 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
7101 (void) fprintf(stderr
,
7102 gettext("invalid version '%s'\n"), optarg
);
7107 (void) fprintf(stderr
, gettext("missing argument for "
7108 "'%c' option\n"), optopt
);
7112 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7123 if (cb
.cb_version
== 0) {
7124 cb
.cb_version
= SPA_VERSION
;
7125 } else if (!upgradeall
&& argc
== 0) {
7126 (void) fprintf(stderr
, gettext("-V option is "
7127 "incompatible with other arguments\n"));
7132 if (upgradeall
|| argc
!= 0) {
7133 (void) fprintf(stderr
, gettext("-v option is "
7134 "incompatible with other arguments\n"));
7137 } else if (upgradeall
) {
7139 (void) fprintf(stderr
, gettext("-a option should not "
7140 "be used along with a pool name\n"));
7145 (void) printf(gettext("This system supports ZFS pool feature "
7150 (void) printf(gettext("The following features are "
7152 (void) printf(gettext("FEAT DESCRIPTION\n"));
7153 (void) printf("----------------------------------------------"
7154 "---------------\n");
7155 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7156 zfeature_info_t
*fi
= &spa_feature_table
[i
];
7158 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
7159 " (read-only compatible)" : "";
7161 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
7162 (void) printf(" %s\n", fi
->fi_desc
);
7164 (void) printf("\n");
7166 (void) printf(gettext("The following legacy versions are also "
7168 (void) printf(gettext("VER DESCRIPTION\n"));
7169 (void) printf("--- -----------------------------------------"
7170 "---------------\n");
7171 (void) printf(gettext(" 1 Initial ZFS version\n"));
7172 (void) printf(gettext(" 2 Ditto blocks "
7173 "(replicated metadata)\n"));
7174 (void) printf(gettext(" 3 Hot spares and double parity "
7176 (void) printf(gettext(" 4 zpool history\n"));
7177 (void) printf(gettext(" 5 Compression using the gzip "
7179 (void) printf(gettext(" 6 bootfs pool property\n"));
7180 (void) printf(gettext(" 7 Separate intent log devices\n"));
7181 (void) printf(gettext(" 8 Delegated administration\n"));
7182 (void) printf(gettext(" 9 refquota and refreservation "
7184 (void) printf(gettext(" 10 Cache devices\n"));
7185 (void) printf(gettext(" 11 Improved scrub performance\n"));
7186 (void) printf(gettext(" 12 Snapshot properties\n"));
7187 (void) printf(gettext(" 13 snapused property\n"));
7188 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7189 (void) printf(gettext(" 15 user/group space accounting\n"));
7190 (void) printf(gettext(" 16 stmf property support\n"));
7191 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7192 (void) printf(gettext(" 18 Snapshot user holds\n"));
7193 (void) printf(gettext(" 19 Log device removal\n"));
7194 (void) printf(gettext(" 20 Compression using zle "
7195 "(zero-length encoding)\n"));
7196 (void) printf(gettext(" 21 Deduplication\n"));
7197 (void) printf(gettext(" 22 Received properties\n"));
7198 (void) printf(gettext(" 23 Slim ZIL\n"));
7199 (void) printf(gettext(" 24 System attributes\n"));
7200 (void) printf(gettext(" 25 Improved scrub stats\n"));
7201 (void) printf(gettext(" 26 Improved snapshot deletion "
7203 (void) printf(gettext(" 27 Improved snapshot creation "
7205 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7206 (void) printf(gettext("\nFor more information on a particular "
7207 "version, including supported releases,\n"));
7208 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7209 } else if (argc
== 0 && upgradeall
) {
7210 cb
.cb_first
= B_TRUE
;
7211 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
7212 if (ret
== 0 && cb
.cb_first
) {
7213 if (cb
.cb_version
== SPA_VERSION
) {
7214 (void) printf(gettext("All pools are already "
7215 "formatted using feature flags.\n\n"));
7216 (void) printf(gettext("Every feature flags "
7217 "pool already has all supported features "
7220 (void) printf(gettext("All pools are already "
7221 "formatted with version %llu or higher.\n"),
7222 (u_longlong_t
)cb
.cb_version
);
7225 } else if (argc
== 0) {
7226 cb
.cb_first
= B_TRUE
;
7227 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
7231 (void) printf(gettext("All pools are formatted "
7232 "using feature flags.\n\n"));
7234 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7235 "for a list of available legacy versions.\n"));
7238 cb
.cb_first
= B_TRUE
;
7239 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
7243 (void) printf(gettext("Every feature flags pool has "
7244 "all supported features enabled.\n"));
7246 (void) printf(gettext("\n"));
7249 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
7256 typedef struct hist_cbdata
{
7263 * Print out the command history for a specific pool.
7266 get_history_one(zpool_handle_t
*zhp
, void *data
)
7272 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
7274 cb
->first
= B_FALSE
;
7276 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
7278 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
7281 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
7282 &records
, &numrecords
) == 0);
7283 for (i
= 0; i
< numrecords
; i
++) {
7284 nvlist_t
*rec
= records
[i
];
7287 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
7291 tsec
= fnvlist_lookup_uint64(records
[i
],
7293 (void) localtime_r(&tsec
, &t
);
7294 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
7297 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
7298 (void) printf("%s %s", tbuf
,
7299 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
7300 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
7302 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
7305 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
7306 (void) printf("%s unrecognized record:\n",
7308 dump_nvlist(rec
, 4);
7311 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
7312 zfs_history_event_names
[ievent
],
7313 (longlong_t
)fnvlist_lookup_uint64(
7314 rec
, ZPOOL_HIST_TXG
),
7315 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
7316 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
7319 (void) printf("%s [txg:%lld] %s", tbuf
,
7320 (longlong_t
)fnvlist_lookup_uint64(
7321 rec
, ZPOOL_HIST_TXG
),
7322 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
7323 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
7324 (void) printf(" %s (%llu)",
7325 fnvlist_lookup_string(rec
,
7327 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
7330 (void) printf(" %s", fnvlist_lookup_string(rec
,
7331 ZPOOL_HIST_INT_STR
));
7332 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
7335 (void) printf("%s ioctl %s\n", tbuf
,
7336 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
7337 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
7338 (void) printf(" input:\n");
7339 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7340 ZPOOL_HIST_INPUT_NVL
), 8);
7342 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
7343 (void) printf(" output:\n");
7344 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7345 ZPOOL_HIST_OUTPUT_NVL
), 8);
7347 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
7348 (void) printf(" errno: %lld\n",
7349 (longlong_t
)fnvlist_lookup_int64(rec
,
7355 (void) printf("%s unrecognized record:\n", tbuf
);
7356 dump_nvlist(rec
, 4);
7360 (void) printf("\n");
7363 (void) printf(" [");
7364 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
7365 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
7366 struct passwd
*pwd
= getpwuid(who
);
7367 (void) printf("user %d ", (int)who
);
7369 (void) printf("(%s) ", pwd
->pw_name
);
7371 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
7372 (void) printf("on %s",
7373 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
7375 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
7376 (void) printf(":%s",
7377 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
7381 (void) printf("\n");
7383 (void) printf("\n");
7390 * zpool history <pool>
7392 * Displays the history of commands that modified pools.
7395 zpool_do_history(int argc
, char **argv
)
7397 hist_cbdata_t cbdata
= { 0 };
7401 cbdata
.first
= B_TRUE
;
7403 while ((c
= getopt(argc
, argv
, "li")) != -1) {
7406 cbdata
.longfmt
= B_TRUE
;
7409 cbdata
.internal
= B_TRUE
;
7412 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7420 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
7423 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
7424 (void) fprintf(stderr
, gettext("no pools available\n"));
7431 typedef struct ev_opts
{
7436 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
7440 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
7442 char ctime_str
[26], str
[32], *ptr
;
7446 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
7447 memset(str
, ' ', 32);
7448 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
7449 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
7450 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
7451 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
7452 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
7454 (void) printf(gettext("%s\t"), str
);
7456 (void) printf(gettext("%s "), str
);
7458 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
7459 (void) printf(gettext("%s\n"), ptr
);
7463 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
7467 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
7468 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
7470 data_type_t type
= nvpair_type(nvp
);
7471 const char *name
= nvpair_name(nvp
);
7481 printf(gettext("%*s%s = "), depth
, "", name
);
7484 case DATA_TYPE_BOOLEAN
:
7485 printf(gettext("%s"), "1");
7488 case DATA_TYPE_BOOLEAN_VALUE
:
7489 (void) nvpair_value_boolean_value(nvp
, &b
);
7490 printf(gettext("%s"), b
? "1" : "0");
7493 case DATA_TYPE_BYTE
:
7494 (void) nvpair_value_byte(nvp
, &i8
);
7495 printf(gettext("0x%x"), i8
);
7498 case DATA_TYPE_INT8
:
7499 (void) nvpair_value_int8(nvp
, (void *)&i8
);
7500 printf(gettext("0x%x"), i8
);
7503 case DATA_TYPE_UINT8
:
7504 (void) nvpair_value_uint8(nvp
, &i8
);
7505 printf(gettext("0x%x"), i8
);
7508 case DATA_TYPE_INT16
:
7509 (void) nvpair_value_int16(nvp
, (void *)&i16
);
7510 printf(gettext("0x%x"), i16
);
7513 case DATA_TYPE_UINT16
:
7514 (void) nvpair_value_uint16(nvp
, &i16
);
7515 printf(gettext("0x%x"), i16
);
7518 case DATA_TYPE_INT32
:
7519 (void) nvpair_value_int32(nvp
, (void *)&i32
);
7520 printf(gettext("0x%x"), i32
);
7523 case DATA_TYPE_UINT32
:
7524 (void) nvpair_value_uint32(nvp
, &i32
);
7525 printf(gettext("0x%x"), i32
);
7528 case DATA_TYPE_INT64
:
7529 (void) nvpair_value_int64(nvp
, (void *)&i64
);
7530 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7533 case DATA_TYPE_UINT64
:
7534 (void) nvpair_value_uint64(nvp
, &i64
);
7536 * translate vdev state values to readable
7537 * strings to aide zpool events consumers
7540 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
7542 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
7543 printf(gettext("\"%s\" (0x%llx)"),
7544 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
7547 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7551 case DATA_TYPE_HRTIME
:
7552 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
7553 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7556 case DATA_TYPE_STRING
:
7557 (void) nvpair_value_string(nvp
, &str
);
7558 printf(gettext("\"%s\""), str
? str
: "<NULL>");
7561 case DATA_TYPE_NVLIST
:
7562 printf(gettext("(embedded nvlist)\n"));
7563 (void) nvpair_value_nvlist(nvp
, &cnv
);
7564 zpool_do_events_nvprint(cnv
, depth
+ 8);
7565 printf(gettext("%*s(end %s)"), depth
, "", name
);
7568 case DATA_TYPE_NVLIST_ARRAY
: {
7572 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
7573 printf(gettext("(%d embedded nvlists)\n"), nelem
);
7574 for (i
= 0; i
< nelem
; i
++) {
7575 printf(gettext("%*s%s[%d] = %s\n"),
7576 depth
, "", name
, i
, "(embedded nvlist)");
7577 zpool_do_events_nvprint(val
[i
], depth
+ 8);
7578 printf(gettext("%*s(end %s[%i])\n"),
7579 depth
, "", name
, i
);
7581 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
7585 case DATA_TYPE_INT8_ARRAY
: {
7589 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
7590 for (i
= 0; i
< nelem
; i
++)
7591 printf(gettext("0x%x "), val
[i
]);
7596 case DATA_TYPE_UINT8_ARRAY
: {
7600 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
7601 for (i
= 0; i
< nelem
; i
++)
7602 printf(gettext("0x%x "), val
[i
]);
7607 case DATA_TYPE_INT16_ARRAY
: {
7611 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
7612 for (i
= 0; i
< nelem
; i
++)
7613 printf(gettext("0x%x "), val
[i
]);
7618 case DATA_TYPE_UINT16_ARRAY
: {
7622 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
7623 for (i
= 0; i
< nelem
; i
++)
7624 printf(gettext("0x%x "), val
[i
]);
7629 case DATA_TYPE_INT32_ARRAY
: {
7633 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
7634 for (i
= 0; i
< nelem
; i
++)
7635 printf(gettext("0x%x "), val
[i
]);
7640 case DATA_TYPE_UINT32_ARRAY
: {
7644 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
7645 for (i
= 0; i
< nelem
; i
++)
7646 printf(gettext("0x%x "), val
[i
]);
7651 case DATA_TYPE_INT64_ARRAY
: {
7655 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
7656 for (i
= 0; i
< nelem
; i
++)
7657 printf(gettext("0x%llx "),
7658 (u_longlong_t
)val
[i
]);
7663 case DATA_TYPE_UINT64_ARRAY
: {
7667 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
7668 for (i
= 0; i
< nelem
; i
++)
7669 printf(gettext("0x%llx "),
7670 (u_longlong_t
)val
[i
]);
7675 case DATA_TYPE_STRING_ARRAY
: {
7679 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
7680 for (i
= 0; i
< nelem
; i
++)
7681 printf(gettext("\"%s\" "),
7682 str
[i
] ? str
[i
] : "<NULL>");
7687 case DATA_TYPE_BOOLEAN_ARRAY
:
7688 case DATA_TYPE_BYTE_ARRAY
:
7689 case DATA_TYPE_DOUBLE
:
7690 case DATA_TYPE_UNKNOWN
:
7691 printf(gettext("<unknown>"));
7695 printf(gettext("\n"));
7700 zpool_do_events_next(ev_opts_t
*opts
)
7703 int zevent_fd
, ret
, dropped
;
7706 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
7707 VERIFY(zevent_fd
>= 0);
7709 if (!opts
->scripted
)
7710 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7713 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
7714 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
7715 if (ret
|| nvl
== NULL
)
7719 (void) printf(gettext("dropped %d events\n"), dropped
);
7721 if (strlen(opts
->poolname
) > 0 &&
7722 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
7723 strcmp(opts
->poolname
, pool
) != 0)
7726 zpool_do_events_short(nvl
, opts
);
7728 if (opts
->verbose
) {
7729 zpool_do_events_nvprint(nvl
, 8);
7730 printf(gettext("\n"));
7732 (void) fflush(stdout
);
7737 VERIFY(0 == close(zevent_fd
));
7743 zpool_do_events_clear(ev_opts_t
*opts
)
7747 ret
= zpool_events_clear(g_zfs
, &count
);
7749 (void) printf(gettext("cleared %d events\n"), count
);
7755 * zpool events [-vHf [pool] | -c]
7757 * Displays events logs by ZFS.
7760 zpool_do_events(int argc
, char **argv
)
7762 ev_opts_t opts
= { 0 };
7767 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
7782 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7791 (void) fprintf(stderr
, gettext("too many arguments\n"));
7793 } else if (argc
== 1) {
7794 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
7795 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
7796 (void) fprintf(stderr
,
7797 gettext("invalid pool name '%s'\n"), opts
.poolname
);
7802 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
7804 (void) fprintf(stderr
,
7805 gettext("invalid options combined with -c\n"));
7810 ret
= zpool_do_events_clear(&opts
);
7812 ret
= zpool_do_events_next(&opts
);
7818 get_callback(zpool_handle_t
*zhp
, void *data
)
7820 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
7821 char value
[MAXNAMELEN
];
7822 zprop_source_t srctype
;
7825 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
7828 * Skip the special fake placeholder. This will also skip
7829 * over the name property when 'all' is specified.
7831 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
7832 pl
== cbp
->cb_proplist
)
7835 if (pl
->pl_prop
== ZPROP_INVAL
&&
7836 (zpool_prop_feature(pl
->pl_user_prop
) ||
7837 zpool_prop_unsupported(pl
->pl_user_prop
))) {
7838 srctype
= ZPROP_SRC_LOCAL
;
7840 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
7841 value
, sizeof (value
)) == 0) {
7842 zprop_print_one_property(zpool_get_name(zhp
),
7843 cbp
, pl
->pl_user_prop
, value
, srctype
,
7847 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
7848 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
7851 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
7852 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
7860 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7862 * -H Scripted mode. Don't display headers, and separate properties
7864 * -o List of columns to display. Defaults to
7865 * "name,property,value,source".
7866 * -p Display values in parsable (exact) format.
7868 * Get properties of pools in the system. Output space statistics
7869 * for each one as well as other attributes.
7872 zpool_do_get(int argc
, char **argv
)
7874 zprop_get_cbdata_t cb
= { 0 };
7875 zprop_list_t fake_name
= { 0 };
7880 cb
.cb_first
= B_TRUE
;
7883 * Set up default columns and sources.
7885 cb
.cb_sources
= ZPROP_SRC_ALL
;
7886 cb
.cb_columns
[0] = GET_COL_NAME
;
7887 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7888 cb
.cb_columns
[2] = GET_COL_VALUE
;
7889 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7890 cb
.cb_type
= ZFS_TYPE_POOL
;
7893 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
7896 cb
.cb_literal
= B_TRUE
;
7899 cb
.cb_scripted
= B_TRUE
;
7902 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
7904 while (*optarg
!= '\0') {
7905 static char *col_subopts
[] =
7906 { "name", "property", "value", "source",
7909 if (i
== ZFS_GET_NCOLS
) {
7910 (void) fprintf(stderr
, gettext("too "
7911 "many fields given to -o "
7916 switch (getsubopt(&optarg
, col_subopts
,
7919 cb
.cb_columns
[i
++] = GET_COL_NAME
;
7922 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
7925 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
7928 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
7932 (void) fprintf(stderr
,
7933 gettext("\"all\" conflicts "
7934 "with specific fields "
7935 "given to -o option\n"));
7938 cb
.cb_columns
[0] = GET_COL_NAME
;
7939 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7940 cb
.cb_columns
[2] = GET_COL_VALUE
;
7941 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7945 (void) fprintf(stderr
,
7946 gettext("invalid column name "
7953 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7963 (void) fprintf(stderr
, gettext("missing property "
7968 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
7969 ZFS_TYPE_POOL
) != 0)
7975 if (cb
.cb_proplist
!= NULL
) {
7976 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
7977 fake_name
.pl_width
= strlen(gettext("NAME"));
7978 fake_name
.pl_next
= cb
.cb_proplist
;
7979 cb
.cb_proplist
= &fake_name
;
7982 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
7985 if (cb
.cb_proplist
== &fake_name
)
7986 zprop_free_list(fake_name
.pl_next
);
7988 zprop_free_list(cb
.cb_proplist
);
7993 typedef struct set_cbdata
{
7996 boolean_t cb_any_successful
;
8000 set_callback(zpool_handle_t
*zhp
, void *data
)
8003 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
8005 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
8008 cb
->cb_any_successful
= B_TRUE
;
8014 zpool_do_set(int argc
, char **argv
)
8016 set_cbdata_t cb
= { 0 };
8019 if (argc
> 1 && argv
[1][0] == '-') {
8020 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8026 (void) fprintf(stderr
, gettext("missing property=value "
8032 (void) fprintf(stderr
, gettext("missing pool name\n"));
8037 (void) fprintf(stderr
, gettext("too many pool names\n"));
8041 cb
.cb_propname
= argv
[1];
8042 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
8043 if (cb
.cb_value
== NULL
) {
8044 (void) fprintf(stderr
, gettext("missing value in "
8045 "property=value argument\n"));
8049 *(cb
.cb_value
) = '\0';
8052 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
8059 find_command_idx(char *command
, int *idx
)
8063 for (i
= 0; i
< NCOMMAND
; i
++) {
8064 if (command_table
[i
].name
== NULL
)
8067 if (strcmp(command
, command_table
[i
].name
) == 0) {
8076 main(int argc
, char **argv
)
8082 (void) setlocale(LC_ALL
, "");
8083 (void) textdomain(TEXT_DOMAIN
);
8089 * Make sure the user has specified some command.
8092 (void) fprintf(stderr
, gettext("missing command\n"));
8101 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
8104 if ((g_zfs
= libzfs_init()) == NULL
) {
8105 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
8109 libzfs_print_on_error(g_zfs
, B_TRUE
);
8111 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
8114 * Run the appropriate command.
8116 if (find_command_idx(cmdname
, &i
) == 0) {
8117 current_command
= &command_table
[i
];
8118 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
8119 } else if (strchr(cmdname
, '=')) {
8120 verify(find_command_idx("set", &i
) == 0);
8121 current_command
= &command_table
[i
];
8122 ret
= command_table
[i
].func(argc
, argv
);
8123 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
8125 * 'freeze' is a vile debugging abomination, so we treat
8128 zfs_cmd_t zc
= {"\0"};
8130 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
8131 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
8133 (void) fprintf(stderr
,
8134 gettext("failed to freeze pool: %d\n"), errno
);
8140 (void) fprintf(stderr
, gettext("unrecognized "
8141 "command '%s'\n"), cmdname
);
8146 if (ret
== 0 && log_history
)
8147 (void) zpool_log_history(g_zfs
, history_str
);
8152 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8153 * for the purposes of running ::findleaks.
8155 if (getenv("ZFS_ABORT") != NULL
) {
8156 (void) printf("dumping core by request\n");