4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
51 #include <sys/fs/zfs.h>
53 #include <sys/systeminfo.h>
54 #include <sys/fm/fs/zfs.h>
55 #include <sys/fm/util.h>
56 #include <sys/fm/protocol.h>
57 #include <sys/zfs_ioctl.h>
63 #include "zpool_util.h"
64 #include "zfs_comutil.h"
65 #include "zfeature_common.h"
67 #include "statcommon.h"
69 static int zpool_do_create(int, char **);
70 static int zpool_do_destroy(int, char **);
72 static int zpool_do_add(int, char **);
73 static int zpool_do_remove(int, char **);
74 static int zpool_do_labelclear(int, char **);
76 static int zpool_do_list(int, char **);
77 static int zpool_do_iostat(int, char **);
78 static int zpool_do_status(int, char **);
80 static int zpool_do_online(int, char **);
81 static int zpool_do_offline(int, char **);
82 static int zpool_do_clear(int, char **);
83 static int zpool_do_reopen(int, char **);
85 static int zpool_do_reguid(int, char **);
87 static int zpool_do_attach(int, char **);
88 static int zpool_do_detach(int, char **);
89 static int zpool_do_replace(int, char **);
90 static int zpool_do_split(int, char **);
92 static int zpool_do_scrub(int, char **);
94 static int zpool_do_import(int, char **);
95 static int zpool_do_export(int, char **);
97 static int zpool_do_upgrade(int, char **);
99 static int zpool_do_history(int, char **);
100 static int zpool_do_events(int, char **);
102 static int zpool_do_get(int, char **);
103 static int zpool_do_set(int, char **);
105 static int zpool_do_sync(int, char **);
108 * These libumem hooks provide a reasonable set of defaults for the allocator's
109 * debugging facilities.
114 _umem_debug_init(void)
116 return ("default,verbose"); /* $UMEM_DEBUG setting */
120 _umem_logging_init(void)
122 return ("fail,contents"); /* $UMEM_LOGGING setting */
157 * Flags for stats to display with "zpool iostats"
165 IOS_COUNT
, /* always last element */
168 /* iostat_type entries as bitmasks */
169 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
170 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
171 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
172 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
173 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
175 /* Mask of all the histo bits */
176 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
179 * Lookup table for iostat flags to nvlist names. Basically a list
180 * of all the nvlists a flag requires. Also specifies the order in
181 * which data gets printed in zpool iostat.
183 static const char *vsx_type_to_nvlist
[IOS_COUNT
][11] = {
185 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
186 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
187 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
188 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
189 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
190 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
191 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
192 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
193 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
196 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
197 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
198 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
199 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
202 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
203 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
204 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
205 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
206 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
209 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
210 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
211 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
212 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
213 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
214 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
215 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
216 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
217 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
218 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
224 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
225 * Right now, only one histo bit is ever set at one time, so we can
226 * just do a highbit64(a)
228 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
230 typedef struct zpool_command
{
232 int (*func
)(int, char **);
237 * Master command table. Each ZFS command has a name, associated function, and
238 * usage message. The usage messages need to be internationalized, so we have
239 * to have a function to return the usage message based on a command index.
241 * These commands are organized according to how they are displayed in the usage
242 * message. An empty command (one with a NULL name) indicates an empty line in
243 * the generic usage message.
245 static zpool_command_t command_table
[] = {
246 { "create", zpool_do_create
, HELP_CREATE
},
247 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
249 { "add", zpool_do_add
, HELP_ADD
},
250 { "remove", zpool_do_remove
, HELP_REMOVE
},
252 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
254 { "list", zpool_do_list
, HELP_LIST
},
255 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
256 { "status", zpool_do_status
, HELP_STATUS
},
258 { "online", zpool_do_online
, HELP_ONLINE
},
259 { "offline", zpool_do_offline
, HELP_OFFLINE
},
260 { "clear", zpool_do_clear
, HELP_CLEAR
},
261 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
263 { "attach", zpool_do_attach
, HELP_ATTACH
},
264 { "detach", zpool_do_detach
, HELP_DETACH
},
265 { "replace", zpool_do_replace
, HELP_REPLACE
},
266 { "split", zpool_do_split
, HELP_SPLIT
},
268 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
270 { "import", zpool_do_import
, HELP_IMPORT
},
271 { "export", zpool_do_export
, HELP_EXPORT
},
272 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
273 { "reguid", zpool_do_reguid
, HELP_REGUID
},
275 { "history", zpool_do_history
, HELP_HISTORY
},
276 { "events", zpool_do_events
, HELP_EVENTS
},
278 { "get", zpool_do_get
, HELP_GET
},
279 { "set", zpool_do_set
, HELP_SET
},
280 { "sync", zpool_do_sync
, HELP_SYNC
},
283 #define NCOMMAND (ARRAY_SIZE(command_table))
285 static zpool_command_t
*current_command
;
286 static char history_str
[HIS_MAX_RECORD_LEN
];
287 static boolean_t log_history
= B_TRUE
;
288 static uint_t timestamp_fmt
= NODATE
;
291 get_usage(zpool_help_t idx
)
295 return (gettext("\tadd [-fgLnP] [-o property=value] "
296 "<pool> <vdev> ...\n"));
298 return (gettext("\tattach [-f] [-o property=value] "
299 "<pool> <device> <new-device>\n"));
301 return (gettext("\tclear [-nF] <pool> [device]\n"));
303 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
304 "\t [-O file-system-property=value] ... \n"
305 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
307 return (gettext("\tdestroy [-f] <pool>\n"));
309 return (gettext("\tdetach <pool> <device>\n"));
311 return (gettext("\texport [-af] <pool> ...\n"));
313 return (gettext("\thistory [-il] [<pool>] ...\n"));
315 return (gettext("\timport [-d dir] [-D]\n"
316 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
317 "\timport [-o mntopts] [-o property=value] ... \n"
318 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
319 "[-R root] [-F [-n]] -a\n"
320 "\timport [-o mntopts] [-o property=value] ... \n"
321 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
322 "[-R root] [-F [-n]]\n"
323 "\t <pool | id> [newpool]\n"));
325 return (gettext("\tiostat [[[-c [script1,script2,...]"
326 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
327 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
328 " [interval [count]]\n"));
329 case HELP_LABELCLEAR
:
330 return (gettext("\tlabelclear [-f] <vdev>\n"));
332 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
333 "[-T d|u] [pool] ... [interval [count]]\n"));
335 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
337 return (gettext("\tonline <pool> <device> ...\n"));
339 return (gettext("\treplace [-f] [-o property=value] "
340 "<pool> <device> [new-device]\n"));
342 return (gettext("\tremove <pool> <device> ...\n"));
344 return (gettext("\treopen <pool>\n"));
346 return (gettext("\tscrub [-s | -p] <pool> ...\n"));
348 return (gettext("\tstatus [-c [script1,script2,...]] [-gLPvxD]"
349 "[-T d|u] [pool] ... [interval [count]]\n"));
351 return (gettext("\tupgrade\n"
353 "\tupgrade [-V version] <-a | pool ...>\n"));
355 return (gettext("\tevents [-vHfc]\n"));
357 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
358 "<\"all\" | property[,...]> <pool> ...\n"));
360 return (gettext("\tset <property=value> <pool> \n"));
362 return (gettext("\tsplit [-gLnP] [-R altroot] [-o mntopts]\n"
363 "\t [-o property=value] <pool> <newpool> "
364 "[<device> ...]\n"));
366 return (gettext("\treguid <pool>\n"));
368 return (gettext("\tsync [pool] ...\n"));
377 * Callback routine that will print out a pool property value.
380 print_prop_cb(int prop
, void *cb
)
384 (void) fprintf(fp
, "\t%-15s ", zpool_prop_to_name(prop
));
386 if (zpool_prop_readonly(prop
))
387 (void) fprintf(fp
, " NO ");
389 (void) fprintf(fp
, " YES ");
391 if (zpool_prop_values(prop
) == NULL
)
392 (void) fprintf(fp
, "-\n");
394 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
400 * Display usage message. If we're inside a command, display only the usage for
401 * that command. Otherwise, iterate over the entire command table and display
402 * a complete usage message.
405 usage(boolean_t requested
)
407 FILE *fp
= requested
? stdout
: stderr
;
409 if (current_command
== NULL
) {
412 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
414 gettext("where 'command' is one of the following:\n\n"));
416 for (i
= 0; i
< NCOMMAND
; i
++) {
417 if (command_table
[i
].name
== NULL
)
418 (void) fprintf(fp
, "\n");
420 (void) fprintf(fp
, "%s",
421 get_usage(command_table
[i
].usage
));
424 (void) fprintf(fp
, gettext("usage:\n"));
425 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
428 if (current_command
!= NULL
&&
429 ((strcmp(current_command
->name
, "set") == 0) ||
430 (strcmp(current_command
->name
, "get") == 0) ||
431 (strcmp(current_command
->name
, "list") == 0))) {
434 gettext("\nthe following properties are supported:\n"));
436 (void) fprintf(fp
, "\n\t%-15s %s %s\n\n",
437 "PROPERTY", "EDIT", "VALUES");
439 /* Iterate over all properties */
440 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
443 (void) fprintf(fp
, "\t%-15s ", "feature@...");
444 (void) fprintf(fp
, "YES disabled | enabled | active\n");
446 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
447 "appended with a feature name.\nSee zpool-features(5).\n"));
451 * See comments at end of main().
453 if (getenv("ZFS_ABORT") != NULL
) {
454 (void) printf("dumping core by request\n");
458 exit(requested
? 0 : 2);
462 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
463 boolean_t print_logs
, int name_flags
)
470 (void) printf("\t%*s%s\n", indent
, "", name
);
472 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
473 &child
, &children
) != 0)
476 for (c
= 0; c
< children
; c
++) {
477 uint64_t is_log
= B_FALSE
;
479 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
481 if ((is_log
&& !print_logs
) || (!is_log
&& print_logs
))
484 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
485 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2,
486 B_FALSE
, name_flags
);
492 prop_list_contains_feature(nvlist_t
*proplist
)
495 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
496 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
497 if (zpool_prop_feature(nvpair_name(nvp
)))
504 * Add a property pair (name, string-value) into a property nvlist.
507 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
510 zpool_prop_t prop
= ZPROP_INVAL
;
516 if (*props
== NULL
&&
517 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
518 (void) fprintf(stderr
,
519 gettext("internal error: out of memory\n"));
526 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
528 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
&&
529 !zpool_prop_feature(propname
)) {
530 (void) fprintf(stderr
, gettext("property '%s' is "
531 "not a valid pool property\n"), propname
);
536 * feature@ properties and version should not be specified
539 if ((prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
) &&
540 nvlist_exists(proplist
, vname
)) ||
541 (prop
== ZPOOL_PROP_VERSION
&&
542 prop_list_contains_feature(proplist
))) {
543 (void) fprintf(stderr
, gettext("'feature@' and "
544 "'version' properties cannot be specified "
550 if (zpool_prop_feature(propname
))
553 normnm
= zpool_prop_to_name(prop
);
555 if ((fprop
= zfs_name_to_prop(propname
)) != ZPROP_INVAL
) {
556 normnm
= zfs_prop_to_name(fprop
);
562 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
563 prop
!= ZPOOL_PROP_CACHEFILE
) {
564 (void) fprintf(stderr
, gettext("property '%s' "
565 "specified multiple times\n"), propname
);
569 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
570 (void) fprintf(stderr
, gettext("internal "
571 "error: out of memory\n"));
579 * Set a default property pair (name, string-value) in a property nvlist
582 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
587 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
590 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
594 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
596 * -f Force addition of devices, even if they appear in use
597 * -g Display guid for individual vdev name.
598 * -L Follow links when resolving vdev path name.
599 * -n Do not add the devices, but display the resulting layout if
600 * they were to be added.
601 * -o Set property=value.
602 * -P Display full path for vdev name.
604 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
605 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
609 zpool_do_add(int argc
, char **argv
)
611 boolean_t force
= B_FALSE
;
612 boolean_t dryrun
= B_FALSE
;
620 nvlist_t
*props
= NULL
;
624 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
630 name_flags
|= VDEV_NAME_GUID
;
633 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
639 if ((propval
= strchr(optarg
, '=')) == NULL
) {
640 (void) fprintf(stderr
, gettext("missing "
641 "'=' for -o option\n"));
647 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
648 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
652 name_flags
|= VDEV_NAME_PATH
;
655 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
664 /* get pool name and check number of arguments */
666 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
670 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
679 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
682 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
683 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
689 /* unless manually specified use "ashift" pool property (if set) */
690 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
693 char strval
[ZPOOL_MAXPROPLEN
];
695 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
696 if (src
!= ZPROP_SRC_DEFAULT
) {
697 (void) sprintf(strval
, "%" PRId32
, intval
);
698 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
699 &props
, B_TRUE
) == 0);
703 /* pass off to get_vdev_spec for processing */
704 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
706 if (nvroot
== NULL
) {
712 nvlist_t
*poolnvroot
;
714 uint_t l2children
, c
;
716 boolean_t hadcache
= B_FALSE
;
718 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
721 (void) printf(gettext("would update '%s' to the following "
722 "configuration:\n"), zpool_get_name(zhp
));
724 /* print original main pool and new tree */
725 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, B_FALSE
,
727 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_FALSE
, name_flags
);
729 /* Do the same for the logs */
730 if (num_logs(poolnvroot
) > 0) {
731 print_vdev_tree(zhp
, "logs", poolnvroot
, 0, B_TRUE
,
733 print_vdev_tree(zhp
, NULL
, nvroot
, 0, B_TRUE
,
735 } else if (num_logs(nvroot
) > 0) {
736 print_vdev_tree(zhp
, "logs", nvroot
, 0, B_TRUE
,
740 /* Do the same for the caches */
741 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
742 &l2child
, &l2children
) == 0 && l2children
) {
744 (void) printf(gettext("\tcache\n"));
745 for (c
= 0; c
< l2children
; c
++) {
746 vname
= zpool_vdev_name(g_zfs
, NULL
,
747 l2child
[c
], name_flags
);
748 (void) printf("\t %s\n", vname
);
752 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
753 &l2child
, &l2children
) == 0 && l2children
) {
755 (void) printf(gettext("\tcache\n"));
756 for (c
= 0; c
< l2children
; c
++) {
757 vname
= zpool_vdev_name(g_zfs
, NULL
,
758 l2child
[c
], name_flags
);
759 (void) printf("\t %s\n", vname
);
766 ret
= (zpool_add(zhp
, nvroot
) != 0);
777 * zpool remove <pool> <vdev> ...
779 * Removes the given vdev from the pool. Currently, this supports removing
780 * spares, cache, and log devices from the pool.
783 zpool_do_remove(int argc
, char **argv
)
787 zpool_handle_t
*zhp
= NULL
;
792 /* get pool name and check number of arguments */
794 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
798 (void) fprintf(stderr
, gettext("missing device\n"));
804 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
807 for (i
= 1; i
< argc
; i
++) {
808 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
817 * zpool labelclear [-f] <vdev>
819 * -f Force clearing the label for the vdevs which are members of
820 * the exported or foreign pools.
822 * Verifies that the vdev is not active and zeros out the label information
826 zpool_do_labelclear(int argc
, char **argv
)
828 char vdev
[MAXPATHLEN
];
831 int c
, fd
= -1, ret
= 0;
834 boolean_t inuse
= B_FALSE
;
835 boolean_t force
= B_FALSE
;
838 while ((c
= getopt(argc
, argv
, "f")) != -1) {
844 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
855 (void) fprintf(stderr
, gettext("missing vdev name\n"));
859 (void) fprintf(stderr
, gettext("too many arguments\n"));
864 * Check if we were given absolute path and use it as is.
865 * Otherwise if the provided vdev name doesn't point to a file,
866 * try prepending expected disk paths and partition numbers.
868 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
869 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
872 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
873 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
874 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
878 if (error
|| (stat(vdev
, &st
) != 0)) {
879 (void) fprintf(stderr
, gettext(
880 "failed to find device %s, try specifying absolute "
881 "path instead\n"), argv
[0]);
886 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
887 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
888 vdev
, strerror(errno
));
892 if (ioctl(fd
, BLKFLSBUF
) != 0)
893 (void) fprintf(stderr
, gettext("failed to invalidate "
894 "cache for %s: %s\n"), vdev
, strerror(errno
));
896 if (zpool_read_label(fd
, &config
, NULL
) != 0 || config
== NULL
) {
897 (void) fprintf(stderr
,
898 gettext("failed to check state for %s\n"), vdev
);
903 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
905 (void) fprintf(stderr
,
906 gettext("failed to check state for %s\n"), vdev
);
915 case POOL_STATE_ACTIVE
:
916 case POOL_STATE_SPARE
:
917 case POOL_STATE_L2CACHE
:
918 (void) fprintf(stderr
, gettext(
919 "%s is a member (%s) of pool \"%s\"\n"),
920 vdev
, zpool_pool_state_to_name(state
), name
);
924 case POOL_STATE_EXPORTED
:
927 (void) fprintf(stderr
, gettext(
928 "use '-f' to override the following error:\n"
929 "%s is a member of exported pool \"%s\"\n"),
934 case POOL_STATE_POTENTIALLY_ACTIVE
:
937 (void) fprintf(stderr
, gettext(
938 "use '-f' to override the following error:\n"
939 "%s is a member of potentially active pool \"%s\"\n"),
944 case POOL_STATE_DESTROYED
:
945 /* inuse should never be set for a destroyed pool */
951 ret
= zpool_clear_label(fd
);
953 (void) fprintf(stderr
,
954 gettext("failed to clear label for %s\n"), vdev
);
965 * zpool create [-fnd] [-o property=value] ...
966 * [-O file-system-property=value] ...
967 * [-R root] [-m mountpoint] <pool> <dev> ...
969 * -f Force creation, even if devices appear in use
970 * -n Do not create the pool, but display the resulting layout if it
971 * were to be created.
972 * -R Create a pool under an alternate root
973 * -m Set default mountpoint for the root dataset. By default it's
975 * -o Set property=value.
976 * -o Set feature@feature=enabled|disabled.
977 * -d Don't automatically enable all supported pool features
978 * (individual features can be enabled with -o).
979 * -O Set fsproperty=value in the pool's root file system
981 * Creates the named pool according to the given vdev specification. The
982 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
983 * we get the nvlist back from get_vdev_spec(), we either print out the contents
984 * (if '-n' was specified), or pass it to libzfs to do the creation.
987 zpool_do_create(int argc
, char **argv
)
989 boolean_t force
= B_FALSE
;
990 boolean_t dryrun
= B_FALSE
;
991 boolean_t enable_all_pool_feat
= B_TRUE
;
993 nvlist_t
*nvroot
= NULL
;
997 char *altroot
= NULL
;
998 char *mountpoint
= NULL
;
999 nvlist_t
*fsprops
= NULL
;
1000 nvlist_t
*props
= NULL
;
1004 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1013 enable_all_pool_feat
= B_FALSE
;
1017 if (add_prop_list(zpool_prop_to_name(
1018 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1020 if (add_prop_list_default(zpool_prop_to_name(
1021 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1025 /* Equivalent to -O mountpoint=optarg */
1026 mountpoint
= optarg
;
1029 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1030 (void) fprintf(stderr
, gettext("missing "
1031 "'=' for -o option\n"));
1037 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1041 * If the user is creating a pool that doesn't support
1042 * feature flags, don't enable any features.
1044 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1048 ver
= strtoull(propval
, &end
, 10);
1050 ver
< SPA_VERSION_FEATURES
) {
1051 enable_all_pool_feat
= B_FALSE
;
1054 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1058 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1059 (void) fprintf(stderr
, gettext("missing "
1060 "'=' for -O option\n"));
1067 * Mountpoints are checked and then added later.
1068 * Uniquely among properties, they can be specified
1069 * more than once, to avoid conflict with -m.
1071 if (0 == strcmp(optarg
,
1072 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1073 mountpoint
= propval
;
1074 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1081 * Sanity check temporary pool name.
1083 if (strchr(optarg
, '/') != NULL
) {
1084 (void) fprintf(stderr
, gettext("cannot create "
1085 "'%s': invalid character '/' in temporary "
1087 (void) fprintf(stderr
, gettext("use 'zfs "
1088 "create' to create a dataset\n"));
1092 if (add_prop_list(zpool_prop_to_name(
1093 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1095 if (add_prop_list_default(zpool_prop_to_name(
1096 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1101 (void) fprintf(stderr
, gettext("missing argument for "
1102 "'%c' option\n"), optopt
);
1105 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1114 /* get pool name and check number of arguments */
1116 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1120 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1127 * As a special case, check for use of '/' in the name, and direct the
1128 * user to use 'zfs create' instead.
1130 if (strchr(poolname
, '/') != NULL
) {
1131 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1132 "character '/' in pool name\n"), poolname
);
1133 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1134 "create a dataset\n"));
1138 /* pass off to get_vdev_spec for bulk processing */
1139 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1140 argc
- 1, argv
+ 1);
1144 /* make_root_vdev() allows 0 toplevel children if there are spares */
1145 if (!zfs_allocatable_devs(nvroot
)) {
1146 (void) fprintf(stderr
, gettext("invalid vdev "
1147 "specification: at least one toplevel vdev must be "
1152 if (altroot
!= NULL
&& altroot
[0] != '/') {
1153 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1154 "must be an absolute path\n"), altroot
);
1159 * Check the validity of the mountpoint and direct the user to use the
1160 * '-m' mountpoint option if it looks like its in use.
1162 if (mountpoint
== NULL
||
1163 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1164 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1165 char buf
[MAXPATHLEN
];
1168 if (mountpoint
&& mountpoint
[0] != '/') {
1169 (void) fprintf(stderr
, gettext("invalid mountpoint "
1170 "'%s': must be an absolute path, 'legacy', or "
1171 "'none'\n"), mountpoint
);
1175 if (mountpoint
== NULL
) {
1176 if (altroot
!= NULL
)
1177 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1180 (void) snprintf(buf
, sizeof (buf
), "/%s",
1183 if (altroot
!= NULL
)
1184 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1185 altroot
, mountpoint
);
1187 (void) snprintf(buf
, sizeof (buf
), "%s",
1191 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1192 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1193 "%s\n"), buf
, strerror(errno
));
1194 (void) fprintf(stderr
, gettext("use '-m' "
1195 "option to provide a different default\n"));
1200 while (count
< 3 && readdir(dirp
) != NULL
)
1202 (void) closedir(dirp
);
1205 (void) fprintf(stderr
, gettext("mountpoint "
1206 "'%s' exists and is not empty\n"), buf
);
1207 (void) fprintf(stderr
, gettext("use '-m' "
1208 "option to provide a "
1209 "different default\n"));
1216 * Now that the mountpoint's validity has been checked, ensure that
1217 * the property is set appropriately prior to creating the pool.
1219 if (mountpoint
!= NULL
) {
1220 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1221 mountpoint
, &fsprops
, B_FALSE
);
1229 * For a dry run invocation, print out a basic message and run
1230 * through all the vdevs in the list and print out in an
1231 * appropriate hierarchy.
1233 (void) printf(gettext("would create '%s' with the "
1234 "following layout:\n\n"), poolname
);
1236 print_vdev_tree(NULL
, poolname
, nvroot
, 0, B_FALSE
, 0);
1237 if (num_logs(nvroot
) > 0)
1238 print_vdev_tree(NULL
, "logs", nvroot
, 0, B_TRUE
, 0);
1243 * Hand off to libzfs.
1246 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1247 char propname
[MAXPATHLEN
];
1249 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1251 (void) snprintf(propname
, sizeof (propname
),
1252 "feature@%s", feat
->fi_uname
);
1255 * Only features contained in props will be enabled:
1256 * remove from the nvlist every ZFS_FEATURE_DISABLED
1257 * value and add every missing ZFS_FEATURE_ENABLED if
1258 * enable_all_pool_feat is set.
1260 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1261 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1262 (void) nvlist_remove_all(props
,
1264 } else if (enable_all_pool_feat
) {
1265 ret
= add_prop_list(propname
,
1266 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1273 if (zpool_create(g_zfs
, poolname
,
1274 nvroot
, props
, fsprops
) == 0) {
1275 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1276 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1278 if (zfs_mount(pool
, NULL
, 0) == 0)
1279 ret
= zfs_shareall(pool
);
1282 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1283 (void) fprintf(stderr
, gettext("pool name may have "
1289 nvlist_free(nvroot
);
1290 nvlist_free(fsprops
);
1294 nvlist_free(fsprops
);
1301 * zpool destroy <pool>
1303 * -f Forcefully unmount any datasets
1305 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1308 zpool_do_destroy(int argc
, char **argv
)
1310 boolean_t force
= B_FALSE
;
1313 zpool_handle_t
*zhp
;
1317 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1323 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1332 /* check arguments */
1334 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1338 (void) fprintf(stderr
, gettext("too many arguments\n"));
1344 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1346 * As a special case, check for use of '/' in the name, and
1347 * direct the user to use 'zfs destroy' instead.
1349 if (strchr(pool
, '/') != NULL
)
1350 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1351 "destroy a dataset\n"));
1355 if (zpool_disable_datasets(zhp
, force
) != 0) {
1356 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1357 "could not unmount datasets\n"), zpool_get_name(zhp
));
1362 /* The history must be logged as part of the export */
1363 log_history
= B_FALSE
;
1365 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1372 typedef struct export_cbdata
{
1374 boolean_t hardforce
;
1381 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1383 export_cbdata_t
*cb
= data
;
1385 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1388 /* The history must be logged as part of the export */
1389 log_history
= B_FALSE
;
1391 if (cb
->hardforce
) {
1392 if (zpool_export_force(zhp
, history_str
) != 0)
1394 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1402 * zpool export [-f] <pool> ...
1404 * -a Export all pools
1405 * -f Forcefully unmount datasets
1407 * Export the given pools. By default, the command will attempt to cleanly
1408 * unmount any active datasets within the pool. If the '-f' flag is specified,
1409 * then the datasets will be forcefully unmounted.
1412 zpool_do_export(int argc
, char **argv
)
1415 boolean_t do_all
= B_FALSE
;
1416 boolean_t force
= B_FALSE
;
1417 boolean_t hardforce
= B_FALSE
;
1421 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1433 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1440 cb
.hardforce
= hardforce
;
1446 (void) fprintf(stderr
, gettext("too many arguments\n"));
1450 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1451 zpool_export_one
, &cb
));
1454 /* check arguments */
1456 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1460 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, zpool_export_one
, &cb
);
1466 * Given a vdev configuration, determine the maximum width needed for the device
1470 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1478 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1479 if (strlen(name
) + depth
> max
)
1480 max
= strlen(name
) + depth
;
1484 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1485 &child
, &children
) == 0) {
1486 for (c
= 0; c
< children
; c
++)
1487 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1488 max
, name_flags
)) > max
)
1492 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
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_CHILDREN
,
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
)
1511 typedef struct spare_cbdata
{
1513 zpool_handle_t
*cb_zhp
;
1517 find_vdev(nvlist_t
*nv
, uint64_t search
)
1523 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1527 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1528 &child
, &children
) == 0) {
1529 for (c
= 0; c
< children
; c
++)
1530 if (find_vdev(child
[c
], search
))
1538 find_spare(zpool_handle_t
*zhp
, void *data
)
1540 spare_cbdata_t
*cbp
= data
;
1541 nvlist_t
*config
, *nvroot
;
1543 config
= zpool_get_config(zhp
, NULL
);
1544 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1547 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1556 typedef struct status_cbdata
{
1560 boolean_t cb_allpools
;
1561 boolean_t cb_verbose
;
1562 boolean_t cb_explain
;
1564 boolean_t cb_dedup_stats
;
1565 boolean_t cb_print_status
;
1566 vdev_cmd_data_list_t
*vcdl
;
1569 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1571 is_blank_str(char *str
)
1573 while (str
!= NULL
&& *str
!= '\0') {
1581 /* Print command output lines for specific vdev in a specific pool */
1583 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1585 vdev_cmd_data_t
*data
;
1589 for (i
= 0; i
< vcdl
->count
; i
++) {
1590 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1591 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1592 /* Not the vdev we're looking for */
1596 data
= &vcdl
->data
[i
];
1597 /* Print out all the output values for this vdev */
1598 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
1600 /* Does this vdev have values for this column? */
1601 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
1602 if (strcmp(data
->cols
[k
],
1603 vcdl
->uniq_cols
[j
]) == 0) {
1604 /* yes it does, record the value */
1605 val
= data
->lines
[k
];
1610 * Mark empty values with dashes to make output
1613 if (is_blank_str(val
))
1616 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
1617 if (j
< vcdl
->uniq_cols_cnt
- 1)
1621 /* Print out any values that aren't in a column at the end */
1622 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
1623 /* Did we have any columns? If so print a spacer. */
1624 if (vcdl
->uniq_cols_cnt
> 0)
1627 val
= data
->lines
[j
];
1628 printf("%s", val
? val
: "");
1635 * Print out configuration state as requested by status_callback.
1638 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
1639 nvlist_t
*nv
, int depth
, boolean_t isspare
)
1643 pool_scan_stat_t
*ps
= NULL
;
1645 char rbuf
[6], wbuf
[6], cbuf
[6];
1647 uint64_t notpresent
;
1648 spare_cbdata_t spare_cb
;
1652 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1653 &child
, &children
) != 0)
1656 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1657 (uint64_t **)&vs
, &c
) == 0);
1659 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1662 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1665 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
1667 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
1671 (void) printf("\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
1675 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
1676 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
1677 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
, sizeof (cbuf
));
1678 (void) printf(" %5s %5s %5s", rbuf
, wbuf
, cbuf
);
1681 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
1682 ¬present
) == 0) {
1683 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
1684 (void) printf(" was %s", path
);
1685 } else if (vs
->vs_aux
!= 0) {
1688 switch (vs
->vs_aux
) {
1689 case VDEV_AUX_OPEN_FAILED
:
1690 (void) printf(gettext("cannot open"));
1693 case VDEV_AUX_BAD_GUID_SUM
:
1694 (void) printf(gettext("missing device"));
1697 case VDEV_AUX_NO_REPLICAS
:
1698 (void) printf(gettext("insufficient replicas"));
1701 case VDEV_AUX_VERSION_NEWER
:
1702 (void) printf(gettext("newer version"));
1705 case VDEV_AUX_UNSUP_FEAT
:
1706 (void) printf(gettext("unsupported feature(s)"));
1709 case VDEV_AUX_SPARED
:
1710 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1711 &spare_cb
.cb_guid
) == 0);
1712 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
1713 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
1714 zpool_get_name(zhp
)) == 0)
1715 (void) printf(gettext("currently in "
1718 (void) printf(gettext("in use by "
1720 zpool_get_name(spare_cb
.cb_zhp
));
1721 zpool_close(spare_cb
.cb_zhp
);
1723 (void) printf(gettext("currently in use"));
1727 case VDEV_AUX_ERR_EXCEEDED
:
1728 (void) printf(gettext("too many errors"));
1731 case VDEV_AUX_IO_FAILURE
:
1732 (void) printf(gettext("experienced I/O failures"));
1735 case VDEV_AUX_BAD_LOG
:
1736 (void) printf(gettext("bad intent log"));
1739 case VDEV_AUX_EXTERNAL
:
1740 (void) printf(gettext("external device fault"));
1743 case VDEV_AUX_SPLIT_POOL
:
1744 (void) printf(gettext("split into new pool"));
1747 case VDEV_AUX_ACTIVE
:
1748 (void) printf(gettext("currently in use"));
1752 (void) printf(gettext("corrupted data"));
1757 (void) nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_SCAN_STATS
,
1758 (uint64_t **)&ps
, &c
);
1760 if (ps
&& ps
->pss_state
== DSS_SCANNING
&&
1761 vs
->vs_scan_processed
!= 0 && children
== 0) {
1762 (void) printf(gettext(" (%s)"),
1763 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
1764 "resilvering" : "repairing");
1767 if (cb
->vcdl
!= NULL
) {
1768 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
1770 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
1774 (void) printf("\n");
1776 for (c
= 0; c
< children
; c
++) {
1777 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
1779 /* Don't print logs or holes here */
1780 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1782 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
1784 if (islog
|| ishole
)
1786 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1787 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1788 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
1795 * Print the configuration of an exported pool. Iterate over all vdevs in the
1796 * pool, printing out the name and status for each one.
1799 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
1807 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1808 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
1809 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
1812 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
1813 (uint64_t **)&vs
, &c
) == 0);
1815 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
1816 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
1818 if (vs
->vs_aux
!= 0) {
1821 switch (vs
->vs_aux
) {
1822 case VDEV_AUX_OPEN_FAILED
:
1823 (void) printf(gettext("cannot open"));
1826 case VDEV_AUX_BAD_GUID_SUM
:
1827 (void) printf(gettext("missing device"));
1830 case VDEV_AUX_NO_REPLICAS
:
1831 (void) printf(gettext("insufficient replicas"));
1834 case VDEV_AUX_VERSION_NEWER
:
1835 (void) printf(gettext("newer version"));
1838 case VDEV_AUX_UNSUP_FEAT
:
1839 (void) printf(gettext("unsupported feature(s)"));
1842 case VDEV_AUX_ERR_EXCEEDED
:
1843 (void) printf(gettext("too many errors"));
1846 case VDEV_AUX_ACTIVE
:
1847 (void) printf(gettext("currently in use"));
1851 (void) printf(gettext("corrupted data"));
1855 (void) printf("\n");
1857 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1858 &child
, &children
) != 0)
1861 for (c
= 0; c
< children
; c
++) {
1862 uint64_t is_log
= B_FALSE
;
1864 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1869 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1870 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1871 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
1875 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1876 &child
, &children
) == 0) {
1877 (void) printf(gettext("\tcache\n"));
1878 for (c
= 0; c
< children
; c
++) {
1879 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1881 (void) printf("\t %s\n", vname
);
1886 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1887 &child
, &children
) == 0) {
1888 (void) printf(gettext("\tspares\n"));
1889 for (c
= 0; c
< children
; c
++) {
1890 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
1892 (void) printf("\t %s\n", vname
);
1900 * Logs are recorded as top level vdevs in the main pool child array
1901 * but with "is_log" set to 1. We use either print_status_config() or
1902 * print_import_config() to print the top level logs then any log
1903 * children (eg mirrored slogs) are printed recursively - which
1904 * works because only the top level vdev is marked "is_log"
1907 print_logs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
)
1912 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
1916 (void) printf(gettext("\tlogs\n"));
1918 for (c
= 0; c
< children
; c
++) {
1919 uint64_t is_log
= B_FALSE
;
1922 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1926 name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
1927 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
1928 if (cb
->cb_print_status
)
1929 print_status_config(zhp
, cb
, name
, child
[c
], 2,
1932 print_import_config(cb
, name
, child
[c
], 2);
1938 * Display the status for the given pool.
1941 show_import(nvlist_t
*config
)
1943 uint64_t pool_state
;
1947 uint64_t hostid
= 0;
1949 char *hostname
= "unknown";
1950 nvlist_t
*nvroot
, *nvinfo
;
1951 zpool_status_t reason
;
1952 zpool_errata_t errata
;
1956 status_cbdata_t cb
= { 0 };
1958 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1960 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1962 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
1964 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1967 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
1968 (uint64_t **)&vs
, &vsc
) == 0);
1969 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
1971 reason
= zpool_import_status(config
, &msgid
, &errata
);
1973 (void) printf(gettext(" pool: %s\n"), name
);
1974 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
1975 (void) printf(gettext(" state: %s"), health
);
1976 if (pool_state
== POOL_STATE_DESTROYED
)
1977 (void) printf(gettext(" (DESTROYED)"));
1978 (void) printf("\n");
1981 case ZPOOL_STATUS_MISSING_DEV_R
:
1982 case ZPOOL_STATUS_MISSING_DEV_NR
:
1983 case ZPOOL_STATUS_BAD_GUID_SUM
:
1984 (void) printf(gettext(" status: One or more devices are "
1985 "missing from the system.\n"));
1988 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
1989 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
1990 (void) printf(gettext(" status: One or more devices contains "
1991 "corrupted data.\n"));
1994 case ZPOOL_STATUS_CORRUPT_DATA
:
1996 gettext(" status: The pool data is corrupted.\n"));
1999 case ZPOOL_STATUS_OFFLINE_DEV
:
2000 (void) printf(gettext(" status: One or more devices "
2001 "are offlined.\n"));
2004 case ZPOOL_STATUS_CORRUPT_POOL
:
2005 (void) printf(gettext(" status: The pool metadata is "
2009 case ZPOOL_STATUS_VERSION_OLDER
:
2010 (void) printf(gettext(" status: The pool is formatted using a "
2011 "legacy on-disk version.\n"));
2014 case ZPOOL_STATUS_VERSION_NEWER
:
2015 (void) printf(gettext(" status: The pool is formatted using an "
2016 "incompatible version.\n"));
2019 case ZPOOL_STATUS_FEAT_DISABLED
:
2020 (void) printf(gettext(" status: Some supported features are "
2021 "not enabled on the pool.\n"));
2024 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2025 (void) printf(gettext("status: The pool uses the following "
2026 "feature(s) not supported on this system:\n"));
2027 zpool_print_unsup_feat(config
);
2030 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2031 (void) printf(gettext("status: The pool can only be accessed "
2032 "in read-only mode on this system. It\n\tcannot be "
2033 "accessed in read-write mode because it uses the "
2034 "following\n\tfeature(s) not supported on this system:\n"));
2035 zpool_print_unsup_feat(config
);
2038 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2039 (void) printf(gettext(" status: The pool is currently "
2040 "imported by another system.\n"));
2043 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2044 (void) printf(gettext(" status: The pool has the "
2045 "multihost property on. It cannot\n\tbe safely imported "
2046 "when the system hostid is not set.\n"));
2049 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2050 (void) printf(gettext(" status: The pool was last accessed by "
2051 "another system.\n"));
2054 case ZPOOL_STATUS_FAULTED_DEV_R
:
2055 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2056 (void) printf(gettext(" status: One or more devices are "
2060 case ZPOOL_STATUS_BAD_LOG
:
2061 (void) printf(gettext(" status: An intent log record cannot be "
2065 case ZPOOL_STATUS_RESILVERING
:
2066 (void) printf(gettext(" status: One or more devices were being "
2070 case ZPOOL_STATUS_ERRATA
:
2071 (void) printf(gettext(" status: Errata #%d detected.\n"),
2077 * No other status can be seen when importing pools.
2079 assert(reason
== ZPOOL_STATUS_OK
);
2083 * Print out an action according to the overall state of the pool.
2085 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2086 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2087 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2088 (void) printf(gettext(" action: The pool can be "
2089 "imported using its name or numeric identifier, "
2090 "though\n\tsome features will not be available "
2091 "without an explicit 'zpool upgrade'.\n"));
2092 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2093 (void) printf(gettext(" action: The pool can be "
2094 "imported using its name or numeric "
2095 "identifier and\n\tthe '-f' flag.\n"));
2096 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2098 case ZPOOL_ERRATA_NONE
:
2101 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2102 (void) printf(gettext(" action: The pool can "
2103 "be imported using its name or numeric "
2104 "identifier,\n\thowever there is a compat"
2105 "ibility issue which should be corrected"
2106 "\n\tby running 'zpool scrub'\n"));
2109 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2110 (void) printf(gettext(" action: The pool can"
2111 "not be imported with this version of ZFS "
2112 "due to\n\tan active asynchronous destroy. "
2113 "Revert to an earlier version\n\tand "
2114 "allow the destroy to complete before "
2120 * All errata must contain an action message.
2125 (void) printf(gettext(" action: The pool can be "
2126 "imported using its name or numeric "
2129 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2130 (void) printf(gettext(" action: The pool can be imported "
2131 "despite missing or damaged devices. The\n\tfault "
2132 "tolerance of the pool may be compromised if imported.\n"));
2135 case ZPOOL_STATUS_VERSION_NEWER
:
2136 (void) printf(gettext(" action: The pool cannot be "
2137 "imported. Access the pool on a system running "
2138 "newer\n\tsoftware, or recreate the pool from "
2141 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2142 (void) printf(gettext("action: The pool cannot be "
2143 "imported. Access the pool on a system that "
2144 "supports\n\tthe required feature(s), or recreate "
2145 "the pool from backup.\n"));
2147 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2148 (void) printf(gettext("action: The pool cannot be "
2149 "imported in read-write mode. Import the pool "
2151 "\t\"-o readonly=on\", access the pool on a system "
2152 "that supports the\n\trequired feature(s), or "
2153 "recreate the pool from backup.\n"));
2155 case ZPOOL_STATUS_MISSING_DEV_R
:
2156 case ZPOOL_STATUS_MISSING_DEV_NR
:
2157 case ZPOOL_STATUS_BAD_GUID_SUM
:
2158 (void) printf(gettext(" action: The pool cannot be "
2159 "imported. Attach the missing\n\tdevices and try "
2162 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2163 VERIFY0(nvlist_lookup_nvlist(config
,
2164 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2166 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2167 hostname
= fnvlist_lookup_string(nvinfo
,
2168 ZPOOL_CONFIG_MMP_HOSTNAME
);
2170 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2171 hostid
= fnvlist_lookup_uint64(nvinfo
,
2172 ZPOOL_CONFIG_MMP_HOSTID
);
2174 (void) printf(gettext(" action: The pool must be "
2175 "exported from %s (hostid=%lx)\n\tbefore it "
2176 "can be safely imported.\n"), hostname
,
2177 (unsigned long) hostid
);
2179 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2180 (void) printf(gettext(" action: Set a unique system "
2181 "hostid with the zgenhostid(8) command.\n"));
2184 (void) printf(gettext(" action: The pool cannot be "
2185 "imported due to damaged devices or data.\n"));
2189 /* Print the comment attached to the pool. */
2190 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2191 (void) printf(gettext("comment: %s\n"), comment
);
2194 * If the state is "closed" or "can't open", and the aux state
2195 * is "corrupt data":
2197 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2198 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2199 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2200 if (pool_state
== POOL_STATE_DESTROYED
)
2201 (void) printf(gettext("\tThe pool was destroyed, "
2202 "but can be imported using the '-Df' flags.\n"));
2203 else if (pool_state
!= POOL_STATE_EXPORTED
)
2204 (void) printf(gettext("\tThe pool may be active on "
2205 "another system, but can be imported using\n\t"
2206 "the '-f' flag.\n"));
2210 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
2213 (void) printf(gettext(" config:\n\n"));
2215 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, 0, VDEV_NAME_TYPE_ID
);
2216 if (cb
.cb_namewidth
< 10)
2217 cb
.cb_namewidth
= 10;
2219 print_import_config(&cb
, name
, nvroot
, 0);
2220 if (num_logs(nvroot
) > 0)
2221 print_logs(NULL
, &cb
, nvroot
);
2223 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2224 (void) printf(gettext("\n\tAdditional devices are known to "
2225 "be part of this pool, though their\n\texact "
2226 "configuration cannot be determined.\n"));
2231 zfs_force_import_required(nvlist_t
*config
)
2234 uint64_t hostid
= 0;
2237 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2238 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2240 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2243 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2244 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2245 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2246 ZPOOL_CONFIG_MMP_STATE
);
2248 if (mmp_state
!= MMP_STATE_INACTIVE
)
2256 * Perform the import for the given configuration. This passes the heavy
2257 * lifting off to zpool_import_props(), and then mounts the datasets contained
2261 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2262 nvlist_t
*props
, int flags
)
2264 zpool_handle_t
*zhp
;
2269 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2270 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2271 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2273 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2274 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2275 "is formatted using an unsupported ZFS version\n"), name
);
2277 } else if (zfs_force_import_required(config
) &&
2278 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2279 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2282 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2283 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2284 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2285 ZPOOL_CONFIG_MMP_STATE
);
2287 if (mmp_state
== MMP_STATE_ACTIVE
) {
2288 char *hostname
= "<unknown>";
2289 uint64_t hostid
= 0;
2291 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2292 hostname
= fnvlist_lookup_string(nvinfo
,
2293 ZPOOL_CONFIG_MMP_HOSTNAME
);
2295 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2296 hostid
= fnvlist_lookup_uint64(nvinfo
,
2297 ZPOOL_CONFIG_MMP_HOSTID
);
2299 (void) fprintf(stderr
, gettext("cannot import '%s': "
2300 "pool is imported on %s (hostid: "
2301 "0x%lx)\nExport the pool on the other system, "
2302 "then run 'zpool import'.\n"),
2303 name
, hostname
, (unsigned long) hostid
);
2304 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2305 (void) fprintf(stderr
, gettext("Cannot import '%s': "
2306 "pool has the multihost property on and the\n"
2307 "system's hostid is not set. Set a unique hostid "
2308 "with the zgenhostid(8) command.\n"), name
);
2310 char *hostname
= "<unknown>";
2311 uint64_t timestamp
= 0;
2312 uint64_t hostid
= 0;
2314 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
2315 hostname
= fnvlist_lookup_string(config
,
2316 ZPOOL_CONFIG_HOSTNAME
);
2318 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
2319 timestamp
= fnvlist_lookup_uint64(config
,
2320 ZPOOL_CONFIG_TIMESTAMP
);
2322 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
2323 hostid
= fnvlist_lookup_uint64(config
,
2324 ZPOOL_CONFIG_HOSTID
);
2326 (void) fprintf(stderr
, gettext("cannot import '%s': "
2327 "pool was previously in use from another system.\n"
2328 "Last accessed by %s (hostid=%lx) at %s"
2329 "The pool can be imported, use 'zpool import -f' "
2330 "to import the pool.\n"), name
, hostname
,
2331 (unsigned long)hostid
, ctime((time_t *)×tamp
));
2337 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
2340 if (newname
!= NULL
)
2341 name
= (char *)newname
;
2343 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
2346 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
2347 !(flags
& ZFS_IMPORT_ONLY
) &&
2348 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
2358 * zpool import [-d dir] [-D]
2359 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2360 * [-d dir | -c cachefile] [-f] -a
2361 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2362 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2364 * -c Read pool information from a cachefile instead of searching
2367 * -d Scan in a specific directory, other than /dev/. More than
2368 * one directory can be specified using multiple '-d' options.
2370 * -D Scan for previously destroyed pools or import all or only
2371 * specified destroyed pools.
2373 * -R Temporarily import the pool, with all mountpoints relative to
2374 * the given root. The pool will remain exported when the machine
2377 * -V Import even in the presence of faulted vdevs. This is an
2378 * intentionally undocumented option for testing purposes, and
2379 * treats the pool configuration as complete, leaving any bad
2380 * vdevs in the FAULTED state. In other words, it does verbatim
2383 * -f Force import, even if it appears that the pool is active.
2385 * -F Attempt rewind if necessary.
2387 * -n See if rewind would work, but don't actually rewind.
2389 * -N Import the pool but don't mount datasets.
2391 * -T Specify a starting txg to use for import. This option is
2392 * intentionally undocumented option for testing purposes.
2394 * -a Import all pools found.
2396 * -o Set property=value and/or temporary mount options (without '=').
2398 * -s Scan using the default search path, the libblkid cache will
2401 * The import command scans for pools to import, and import pools based on pool
2402 * name and GUID. The pool can also be renamed as part of the import process.
2405 zpool_do_import(int argc
, char **argv
)
2407 char **searchdirs
= NULL
;
2408 char *env
, *envdup
= NULL
;
2412 nvlist_t
*pools
= NULL
;
2413 boolean_t do_all
= B_FALSE
;
2414 boolean_t do_destroyed
= B_FALSE
;
2415 char *mntopts
= NULL
;
2418 uint64_t searchguid
= 0;
2419 char *searchname
= NULL
;
2421 nvlist_t
*found_config
;
2422 nvlist_t
*policy
= NULL
;
2423 nvlist_t
*props
= NULL
;
2425 int flags
= ZFS_IMPORT_NORMAL
;
2426 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
2427 boolean_t dryrun
= B_FALSE
;
2428 boolean_t do_rewind
= B_FALSE
;
2429 boolean_t xtreme_rewind
= B_FALSE
;
2430 boolean_t do_scan
= B_FALSE
;
2431 uint64_t pool_state
, txg
= -1ULL;
2432 char *cachefile
= NULL
;
2433 importargs_t idata
= { 0 };
2437 while ((c
= getopt(argc
, argv
, ":aCc:d:DEfFmnNo:R:stT:VX")) != -1) {
2446 if (searchdirs
== NULL
) {
2447 searchdirs
= safe_malloc(sizeof (char *));
2449 char **tmp
= safe_malloc((nsearch
+ 1) *
2451 bcopy(searchdirs
, tmp
, nsearch
*
2456 searchdirs
[nsearch
++] = optarg
;
2459 do_destroyed
= B_TRUE
;
2462 flags
|= ZFS_IMPORT_ANY_HOST
;
2468 flags
|= ZFS_IMPORT_MISSING_LOG
;
2474 flags
|= ZFS_IMPORT_ONLY
;
2477 if ((propval
= strchr(optarg
, '=')) != NULL
) {
2480 if (add_prop_list(optarg
, propval
,
2488 if (add_prop_list(zpool_prop_to_name(
2489 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
2491 if (add_prop_list_default(zpool_prop_to_name(
2492 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2499 flags
|= ZFS_IMPORT_TEMP_NAME
;
2500 if (add_prop_list_default(zpool_prop_to_name(
2501 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
2507 txg
= strtoull(optarg
, &endptr
, 0);
2508 if (errno
!= 0 || *endptr
!= '\0') {
2509 (void) fprintf(stderr
,
2510 gettext("invalid txg value\n"));
2513 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
2516 flags
|= ZFS_IMPORT_VERBATIM
;
2519 xtreme_rewind
= B_TRUE
;
2522 (void) fprintf(stderr
, gettext("missing argument for "
2523 "'%c' option\n"), optopt
);
2527 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2536 if (cachefile
&& nsearch
!= 0) {
2537 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
2541 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
2542 (void) fprintf(stderr
,
2543 gettext("-n or -X only meaningful with -F\n"));
2547 rewind_policy
= ZPOOL_TRY_REWIND
;
2549 rewind_policy
= ZPOOL_DO_REWIND
;
2551 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
2553 /* In the future, we can capture further policy and include it here */
2554 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
2555 nvlist_add_uint64(policy
, ZPOOL_REWIND_REQUEST_TXG
, txg
) != 0 ||
2556 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
2559 /* check argument count */
2562 (void) fprintf(stderr
, gettext("too many arguments\n"));
2567 (void) fprintf(stderr
, gettext("too many arguments\n"));
2573 * Check for the effective uid. We do this explicitly here because
2574 * otherwise any attempt to discover pools will silently fail.
2576 if (argc
== 0 && geteuid() != 0) {
2577 (void) fprintf(stderr
, gettext("cannot "
2578 "discover pools: permission denied\n"));
2579 if (searchdirs
!= NULL
)
2583 nvlist_free(policy
);
2588 * Depending on the arguments given, we do one of the following:
2590 * <none> Iterate through all pools and display information about
2593 * -a Iterate through all pools and try to import each one.
2595 * <id> Find the pool that corresponds to the given GUID/pool
2596 * name and import that one.
2598 * -D Above options applies only to destroyed pools.
2604 searchguid
= strtoull(argv
[0], &endptr
, 10);
2605 if (errno
!= 0 || *endptr
!= '\0') {
2606 searchname
= argv
[0];
2609 found_config
= NULL
;
2612 * User specified a name or guid. Ensure it's unique.
2614 idata
.unique
= B_TRUE
;
2618 * Check the environment for the preferred search path.
2620 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
2623 envdup
= strdup(env
);
2625 dir
= strtok(envdup
, ":");
2626 while (dir
!= NULL
) {
2627 if (searchdirs
== NULL
) {
2628 searchdirs
= safe_malloc(sizeof (char *));
2630 char **tmp
= safe_malloc((nsearch
+ 1) *
2632 bcopy(searchdirs
, tmp
, nsearch
*
2637 searchdirs
[nsearch
++] = dir
;
2638 dir
= strtok(NULL
, ":");
2642 idata
.path
= searchdirs
;
2643 idata
.paths
= nsearch
;
2644 idata
.poolname
= searchname
;
2645 idata
.guid
= searchguid
;
2646 idata
.cachefile
= cachefile
;
2647 idata
.scan
= do_scan
;
2649 pools
= zpool_search_import(g_zfs
, &idata
);
2651 if (pools
!= NULL
&& idata
.exists
&&
2652 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
2653 (void) fprintf(stderr
, gettext("cannot import '%s': "
2654 "a pool with that name already exists\n"),
2656 (void) fprintf(stderr
, gettext("use the form '%s "
2657 "<pool | id> <newpool>' to give it a new name\n"),
2660 } else if (pools
== NULL
&& idata
.exists
) {
2661 (void) fprintf(stderr
, gettext("cannot import '%s': "
2662 "a pool with that name is already created/imported,\n"),
2664 (void) fprintf(stderr
, gettext("and no additional pools "
2665 "with that name were found\n"));
2667 } else if (pools
== NULL
) {
2669 (void) fprintf(stderr
, gettext("cannot import '%s': "
2670 "no such pool available\n"), argv
[0]);
2676 if (searchdirs
!= NULL
)
2680 nvlist_free(policy
);
2687 * At this point we have a list of import candidate configs. Even if
2688 * we were searching by pool name or guid, we still need to
2689 * post-process the list to deal with pool state and possible
2695 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2697 verify(nvpair_value_nvlist(elem
, &config
) == 0);
2699 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2701 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
2703 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
2706 verify(nvlist_add_nvlist(config
, ZPOOL_REWIND_POLICY
,
2713 (void) printf("\n");
2716 err
|= do_import(config
, NULL
, mntopts
,
2719 show_import(config
);
2721 } else if (searchname
!= NULL
) {
2725 * We are searching for a pool based on name.
2727 verify(nvlist_lookup_string(config
,
2728 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
2730 if (strcmp(name
, searchname
) == 0) {
2731 if (found_config
!= NULL
) {
2732 (void) fprintf(stderr
, gettext(
2733 "cannot import '%s': more than "
2734 "one matching pool\n"), searchname
);
2735 (void) fprintf(stderr
, gettext(
2736 "import by numeric ID instead\n"));
2739 found_config
= config
;
2745 * Search for a pool by guid.
2747 verify(nvlist_lookup_uint64(config
,
2748 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
2750 if (guid
== searchguid
)
2751 found_config
= config
;
2756 * If we were searching for a specific pool, verify that we found a
2757 * pool, and then do the import.
2759 if (argc
!= 0 && err
== 0) {
2760 if (found_config
== NULL
) {
2761 (void) fprintf(stderr
, gettext("cannot import '%s': "
2762 "no such pool available\n"), argv
[0]);
2765 err
|= do_import(found_config
, argc
== 1 ? NULL
:
2766 argv
[1], mntopts
, props
, flags
);
2771 * If we were just looking for pools, report an error if none were
2774 if (argc
== 0 && first
)
2775 (void) fprintf(stderr
,
2776 gettext("no pools available to import\n"));
2781 nvlist_free(policy
);
2782 if (searchdirs
!= NULL
)
2787 return (err
? 1 : 0);
2791 * zpool sync [-f] [pool] ...
2793 * -f (undocumented) force uberblock (and config including zpool cache file)
2796 * Sync the specified pool(s).
2797 * Without arguments "zpool sync" will sync all pools.
2798 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
2802 zpool_do_sync(int argc
, char **argv
)
2805 boolean_t force
= B_FALSE
;
2808 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
2814 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2823 /* if argc == 0 we will execute zpool_sync_one on all pools */
2824 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, zpool_sync_one
, &force
);
2829 typedef struct iostat_cbdata
{
2834 char **cb_vdev_names
; /* Only show these vdevs */
2835 unsigned int cb_vdev_names_count
;
2836 boolean_t cb_verbose
;
2837 boolean_t cb_literal
;
2838 boolean_t cb_scripted
;
2839 zpool_list_t
*cb_list
;
2840 vdev_cmd_data_list_t
*vcdl
;
2844 typedef struct name_and_columns
{
2845 const char *name
; /* Column name */
2846 unsigned int columns
; /* Center name to this number of columns */
2847 } name_and_columns_t
;
2849 #define IOSTAT_MAX_LABELS 11 /* Max number of labels on one line */
2851 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
2853 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
2855 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
2856 {"asyncq_wait", 2}, {"scrub"}},
2857 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
2858 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
2860 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2},
2861 {"sync_queue", 2}, {"async_queue", 2}, {NULL
}},
2862 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
2863 {"async_read", 2}, {"async_write", 2}, {"scrub", 2}, {NULL
}},
2867 /* Shorthand - if "columns" field not set, default to 1 column */
2868 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
2870 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
2872 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2873 {"write"}, {"read"}, {"write"}, {"wait"}, {NULL
}},
2874 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
2875 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
2876 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
2877 {"write"}, {"read"}, {"write"}, {"scrub"}, {NULL
}},
2878 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
2879 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
2882 static const char *histo_to_title
[] = {
2883 [IOS_L_HISTO
] = "latency",
2884 [IOS_RQ_HISTO
] = "req_size",
2888 * Return the number of labels in a null-terminated name_and_columns_t
2893 label_array_len(const name_and_columns_t
*labels
)
2897 while (labels
[i
].name
)
2904 * Return the number of strings in a null-terminated string array.
2907 * const char foo[] = {"bar", "baz", NULL}
2912 str_array_len(const char *array
[])
2923 * Return a default column width for default/latency/queue columns. This does
2924 * not include histograms, which have their columns autosized.
2927 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
2929 unsigned long column_width
= 5; /* Normal niceprint */
2930 static unsigned long widths
[] = {
2932 * Choose some sane default column sizes for printing the
2935 [IOS_DEFAULT
] = 15, /* 1PB capacity */
2936 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
2937 [IOS_QUEUES
] = 6, /* 1M queue entries */
2941 column_width
= widths
[type
];
2943 return (column_width
);
2947 * Print the column labels, i.e:
2949 * capacity operations bandwidth
2950 * alloc free read write read write ...
2952 * If force_column_width is set, use it for the column width. If not set, use
2953 * the default column width.
2956 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
2957 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
2960 unsigned int text_start
, rw_column_width
, spaces_to_end
;
2961 uint64_t flags
= cb
->cb_flags
;
2963 unsigned int column_width
= force_column_width
;
2965 /* For each bit set in flags */
2966 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
2967 idx
= lowbit64(f
) - 1;
2968 if (!force_column_width
)
2969 column_width
= default_column_width(cb
, idx
);
2970 /* Print our top labels centered over "read write" label. */
2971 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
2972 const char *name
= labels
[idx
][i
].name
;
2974 * We treat labels[][].columns == 0 as shorthand
2975 * for one column. It makes writing out the label
2976 * tables more concise.
2978 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
2979 unsigned int slen
= strlen(name
);
2981 rw_column_width
= (column_width
* columns
) +
2982 (2 * (columns
- 1));
2984 text_start
= (int)((rw_column_width
)/columns
-
2987 printf(" "); /* Two spaces between columns */
2989 /* Space from beginning of column to label */
2990 for (s
= 0; s
< text_start
; s
++)
2995 /* Print space after label to end of column */
2996 spaces_to_end
= rw_column_width
- text_start
- slen
;
2997 for (s
= 0; s
< spaces_to_end
; s
++)
3006 * print_cmd_columns - Print custom column titles from -c
3008 * If the user specified the "zpool status|iostat -c" then print their custom
3009 * column titles in the header. For example, print_cmd_columns() would print
3010 * the " col1 col2" part of this:
3012 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3014 * capacity operations bandwidth
3015 * pool alloc free read write read write col1 col2
3016 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3017 * mypool 269K 1008M 0 0 107 946
3018 * mirror 269K 1008M 0 0 107 946
3019 * sdb - - 0 0 102 473 val1 val2
3020 * sdc - - 0 0 5 473 val1 val2
3021 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3024 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3027 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3029 if (vcdl
->count
== 0 || data
== NULL
)
3033 * Each vdev cmd should have the same column names unless the user did
3034 * something weird with their cmd. Just take the column names from the
3035 * first vdev and assume it works for all of them.
3037 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3040 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3043 printf("%*s", vcdl
->uniq_cols_width
[i
],
3044 vcdl
->uniq_cols
[i
]);
3051 * Utility function to print out a line of dashes like:
3053 * -------------------------------- ----- ----- ----- ----- -----
3055 * ...or a dashed named-row line like:
3061 * @force_column_width If non-zero, use the value as the column width.
3062 * Otherwise use the default column widths.
3064 * @name: Print a dashed named-row line starting
3065 * with @name. Otherwise, print a regular
3069 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3073 unsigned int namewidth
;
3074 uint64_t flags
= cb
->cb_flags
;
3077 const name_and_columns_t
*labels
;
3081 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3082 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3083 } else if (cb
->cb_vdev_names_count
) {
3089 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3090 name
? strlen(name
) : 0);
3094 printf("%-*s", namewidth
, name
);
3096 for (i
= 0; i
< namewidth
; i
++)
3100 /* For each bit in flags */
3101 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3102 unsigned int column_width
;
3103 idx
= lowbit64(f
) - 1;
3104 if (force_column_width
)
3105 column_width
= force_column_width
;
3107 column_width
= default_column_width(cb
, idx
);
3109 labels
= iostat_bottom_labels
[idx
];
3110 for (i
= 0; i
< label_array_len(labels
); i
++) {
3112 printf(" %*s-", column_width
- 1, " ");
3114 printf(" %.*s", column_width
,
3115 "--------------------");
3122 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
3123 unsigned int force_column_width
)
3125 print_iostat_dashes(cb
, force_column_width
, NULL
);
3129 print_iostat_separator(iostat_cbdata_t
*cb
)
3131 print_iostat_separator_impl(cb
, 0);
3135 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3136 const char *histo_vdev_name
)
3138 unsigned int namewidth
;
3141 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3142 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3143 } else if (cb
->cb_vdev_names_count
) {
3149 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3150 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
3152 if (histo_vdev_name
)
3153 printf("%-*s", namewidth
, histo_vdev_name
);
3155 printf("%*s", namewidth
, "");
3158 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
3161 printf("%-*s", namewidth
, title
);
3163 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
3164 if (cb
->vcdl
!= NULL
)
3165 print_cmd_columns(cb
->vcdl
, 0);
3169 print_iostat_separator_impl(cb
, force_column_width
);
3171 if (cb
->vcdl
!= NULL
)
3172 print_cmd_columns(cb
->vcdl
, 1);
3178 print_iostat_header(iostat_cbdata_t
*cb
)
3180 print_iostat_header_impl(cb
, 0, NULL
);
3185 * Display a single statistic.
3188 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
3189 unsigned int column_size
, boolean_t scripted
)
3193 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
3196 printf("\t%s", buf
);
3198 printf(" %*s", column_size
, buf
);
3202 * Calculate the default vdev stats
3204 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
3205 * stats into calcvs.
3208 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
3209 vdev_stat_t
*calcvs
)
3213 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
3214 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
3215 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
3217 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
3218 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
3222 * Internal representation of the extended iostats data.
3224 * The extended iostat stats are exported in nvlists as either uint64_t arrays
3225 * or single uint64_t's. We make both look like arrays to make them easier
3226 * to process. In order to make single uint64_t's look like arrays, we set
3227 * __data to the stat data, and then set *data = &__data with count = 1. Then,
3228 * we can just use *data and count.
3232 uint_t count
; /* Number of entries in data[] */
3233 uint64_t __data
; /* Only used when data is a single uint64_t */
3237 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
3241 for (i
= 0; i
< len
; i
++)
3242 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
3248 * Helper function to lookup a uint64_t array or uint64_t value and store its
3249 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
3250 * it look like a one element array to make it easier to process.
3253 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
3254 struct stat_array
*nva
)
3259 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
3260 switch (nvpair_type(tmp
)) {
3261 case DATA_TYPE_UINT64_ARRAY
:
3262 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
3264 case DATA_TYPE_UINT64
:
3265 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
3266 nva
->data
= &nva
->__data
;
3270 /* Not a uint64_t */
3279 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
3280 * subtract them, and return the results in a newly allocated stat_array.
3281 * You must free the returned array after you are done with it with
3282 * free_calc_stats().
3284 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
3287 static struct stat_array
*
3288 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
3291 nvlist_t
*oldnvx
= NULL
, *newnvx
;
3292 struct stat_array
*oldnva
, *newnva
, *calcnva
;
3294 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
3296 /* Extract our extended stats nvlist from the main list */
3297 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3300 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
3304 newnva
= safe_malloc(alloc_size
);
3305 oldnva
= safe_malloc(alloc_size
);
3306 calcnva
= safe_malloc(alloc_size
);
3308 for (j
= 0; j
< len
; j
++) {
3309 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
3311 calcnva
[j
].count
= newnva
[j
].count
;
3312 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
3313 calcnva
[j
].data
= safe_malloc(alloc_size
);
3314 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
3317 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
3319 for (i
= 0; i
< oldnva
[j
].count
; i
++)
3320 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
3329 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
3332 for (i
= 0; i
< len
; i
++)
3339 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
3340 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
3346 enum zfs_nicenum_format format
;
3347 unsigned int buckets
;
3348 unsigned int start_bucket
;
3351 format
= ZFS_NICENUM_RAW
;
3353 format
= ZFS_NICENUM_1024
;
3355 /* All these histos are the same size, so just use nva[0].count */
3356 buckets
= nva
[0].count
;
3358 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
3359 /* Start at 512 - req size should never be lower than this */
3365 for (j
= start_bucket
; j
< buckets
; j
++) {
3366 /* Print histogram bucket label */
3367 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
3368 /* Ending range of this bucket */
3369 val
= (1UL << (j
+ 1)) - 1;
3370 zfs_nicetime(val
, buf
, sizeof (buf
));
3372 /* Request size (starting range of bucket) */
3374 zfs_nicenum(val
, buf
, sizeof (buf
));
3377 if (cb
->cb_scripted
)
3378 printf("%llu", (u_longlong_t
)val
);
3380 printf("%-*s", namewidth
, buf
);
3382 /* Print the values on the line */
3383 for (i
= 0; i
< len
; i
++) {
3384 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
3385 column_width
, cb
->cb_scripted
);
3392 print_solid_separator(unsigned int length
)
3400 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3401 nvlist_t
*newnv
, double scale
, const char *name
)
3403 unsigned int column_width
;
3404 unsigned int namewidth
;
3405 unsigned int entire_width
;
3406 enum iostat_type type
;
3407 struct stat_array
*nva
;
3409 unsigned int names_len
;
3411 /* What type of histo are we? */
3412 type
= IOS_HISTO_IDX(cb
->cb_flags
);
3414 /* Get NULL-terminated array of nvlist names for our histo */
3415 names
= vsx_type_to_nvlist
[type
];
3416 names_len
= str_array_len(names
); /* num of names */
3418 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
3420 if (cb
->cb_literal
) {
3421 column_width
= MAX(5,
3422 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
3427 namewidth
= MAX(cb
->cb_namewidth
,
3428 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
3431 * Calculate the entire line width of what we're printing. The
3432 * +2 is for the two spaces between columns:
3436 /* |___| <---------- column_width */
3438 /* |__________| <--- entire_width */
3440 entire_width
= namewidth
+ (column_width
+ 2) *
3441 label_array_len(iostat_bottom_labels
[type
]);
3443 if (cb
->cb_scripted
)
3444 printf("%s\n", name
);
3446 print_iostat_header_impl(cb
, column_width
, name
);
3448 print_iostat_histo(nva
, names_len
, cb
, column_width
,
3451 free_calc_stats(nva
, names_len
);
3452 if (!cb
->cb_scripted
)
3453 print_solid_separator(entire_width
);
3457 * Calculate the average latency of a power-of-two latency histogram
3460 single_histo_average(uint64_t *histo
, unsigned int buckets
)
3463 uint64_t count
= 0, total
= 0;
3465 for (i
= 0; i
< buckets
; i
++) {
3467 * Our buckets are power-of-two latency ranges. Use the
3468 * midpoint latency of each bucket to calculate the average.
3476 if (histo
[i
] != 0) {
3477 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
3482 /* Prevent divide by zero */
3483 return (count
== 0 ? 0 : total
/ count
);
3487 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3488 nvlist_t
*newnv
, double scale
)
3492 const char *names
[] = {
3493 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
3494 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
3495 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
3496 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
3497 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
3498 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
3499 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
3500 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
3501 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
3502 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
3505 struct stat_array
*nva
;
3507 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
3508 enum zfs_nicenum_format format
;
3510 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
3513 format
= ZFS_NICENUM_RAW
;
3515 format
= ZFS_NICENUM_1024
;
3517 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3518 val
= nva
[i
].data
[0] * scale
;
3519 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3522 free_calc_stats(nva
, ARRAY_SIZE(names
));
3526 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
3527 nvlist_t
*newnv
, double scale
)
3531 const char *names
[] = {
3532 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
3533 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
3534 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
3535 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
3536 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
3537 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
3538 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
3539 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
3540 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
3542 struct stat_array
*nva
;
3544 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
3545 enum zfs_nicenum_format format
;
3547 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
3550 format
= ZFS_NICENUM_RAWTIME
;
3552 format
= ZFS_NICENUM_TIME
;
3554 /* Print our avg latencies on the line */
3555 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
3556 /* Compute average latency for a latency histo */
3557 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
) * scale
;
3558 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
3560 free_calc_stats(nva
, ARRAY_SIZE(names
));
3564 * Print default statistics (capacity/operations/bandwidth)
3567 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
3569 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
3570 enum zfs_nicenum_format format
;
3571 char na
; /* char to print for "not applicable" values */
3573 if (cb
->cb_literal
) {
3574 format
= ZFS_NICENUM_RAW
;
3577 format
= ZFS_NICENUM_1024
;
3581 /* only toplevel vdevs have capacity stats */
3582 if (vs
->vs_space
== 0) {
3583 if (cb
->cb_scripted
)
3584 printf("\t%c\t%c", na
, na
);
3586 printf(" %*c %*c", column_width
, na
, column_width
,
3589 print_one_stat(vs
->vs_alloc
, format
, column_width
,
3591 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
3592 column_width
, cb
->cb_scripted
);
3595 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
3596 format
, column_width
, cb
->cb_scripted
);
3597 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
3598 format
, column_width
, cb
->cb_scripted
);
3599 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
3600 format
, column_width
, cb
->cb_scripted
);
3601 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
3602 format
, column_width
, cb
->cb_scripted
);
3606 * Print out all the statistics for the given vdev. This can either be the
3607 * toplevel configuration, or called recursively. If 'name' is NULL, then this
3608 * is a verbose output, and we don't want to display the toplevel pool stats.
3610 * Returns the number of stat lines printed.
3613 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
3614 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
3616 nvlist_t
**oldchild
, **newchild
;
3618 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
3619 vdev_stat_t zerovs
= { 0 };
3626 calcvs
= safe_malloc(sizeof (*calcvs
));
3628 if (oldnv
!= NULL
) {
3629 verify(nvlist_lookup_uint64_array(oldnv
,
3630 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
3635 /* Do we only want to see a specific vdev? */
3636 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
3637 /* Yes we do. Is this the vdev? */
3638 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
3640 * This is our vdev. Since it is the only vdev we
3641 * will be displaying, make depth = 0 so that it
3642 * doesn't get indented.
3649 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
3650 /* Couldn't match the name */
3655 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
3656 (uint64_t **)&newvs
, &c
) == 0);
3659 * Print the vdev name unless it's is a histogram. Histograms
3660 * display the vdev name in the header itself.
3662 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3663 if (cb
->cb_scripted
) {
3666 if (strlen(name
) + depth
> cb
->cb_namewidth
)
3667 (void) printf("%*s%s", depth
, "", name
);
3669 (void) printf("%*s%s%*s", depth
, "", name
,
3670 (int)(cb
->cb_namewidth
- strlen(name
) -
3675 /* Calculate our scaling factor */
3676 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
3677 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
3679 * If we specify printing histograms with no time interval, then
3680 * print the histogram numbers over the entire lifetime of the
3688 scale
= (double)NANOSEC
/ tdelta
;
3691 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
3692 calc_default_iostats(oldvs
, newvs
, calcvs
);
3693 print_iostat_default(calcvs
, cb
, scale
);
3695 if (cb
->cb_flags
& IOS_LATENCY_M
)
3696 print_iostat_latency(cb
, oldnv
, newnv
, scale
);
3697 if (cb
->cb_flags
& IOS_QUEUES_M
)
3698 print_iostat_queues(cb
, oldnv
, newnv
, scale
);
3699 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3701 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
3704 if (cb
->vcdl
!= NULL
) {
3706 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
3709 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
3713 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
3722 if (!cb
->cb_verbose
)
3725 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
3726 &newchild
, &children
) != 0)
3729 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
3730 &oldchild
, &c
) != 0)
3733 for (c
= 0; c
< children
; c
++) {
3734 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
3736 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
3739 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
3742 if (ishole
|| islog
)
3745 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3747 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
3748 newchild
[c
], cb
, depth
+ 2);
3753 * Log device section
3756 if (num_logs(newnv
) > 0) {
3757 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3758 !cb
->cb_vdev_names
) {
3759 print_iostat_dashes(cb
, 0, "logs");
3763 for (c
= 0; c
< children
; c
++) {
3764 uint64_t islog
= B_FALSE
;
3765 (void) nvlist_lookup_uint64(newchild
[c
],
3766 ZPOOL_CONFIG_IS_LOG
, &islog
);
3769 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3771 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
3772 oldchild
[c
] : NULL
, newchild
[c
],
3781 * Include level 2 ARC devices in iostat output
3783 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
3784 &newchild
, &children
) != 0)
3787 if (oldnv
&& nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
3788 &oldchild
, &c
) != 0)
3792 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
3793 !cb
->cb_vdev_names
) {
3794 print_iostat_dashes(cb
, 0, "cache");
3798 for (c
= 0; c
< children
; c
++) {
3799 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
3801 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
3802 : NULL
, newchild
[c
], cb
, depth
+ 2);
3811 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
3813 iostat_cbdata_t
*cb
= data
;
3817 * If the pool has disappeared, remove it from the list and continue.
3819 if (zpool_refresh_stats(zhp
, &missing
) != 0)
3823 pool_list_remove(cb
->cb_list
, zhp
);
3829 * Callback to print out the iostats for the given pool.
3832 print_iostat(zpool_handle_t
*zhp
, void *data
)
3834 iostat_cbdata_t
*cb
= data
;
3835 nvlist_t
*oldconfig
, *newconfig
;
3836 nvlist_t
*oldnvroot
, *newnvroot
;
3839 newconfig
= zpool_get_config(zhp
, &oldconfig
);
3841 if (cb
->cb_iteration
== 1)
3844 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3847 if (oldconfig
== NULL
)
3850 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
3853 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
3855 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
3856 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
3857 print_iostat_separator(cb
);
3858 if (cb
->vcdl
!= NULL
) {
3859 print_cmd_columns(cb
->vcdl
, 1);
3874 if (isatty(STDOUT_FILENO
)) {
3875 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
3877 columns
= ws
.ws_col
;
3886 get_namewidth(zpool_handle_t
*zhp
, void *data
)
3888 iostat_cbdata_t
*cb
= data
;
3889 nvlist_t
*config
, *nvroot
;
3892 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
3893 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3895 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
3896 if (!cb
->cb_verbose
)
3897 cb
->cb_namewidth
= poolname_len
;
3899 cb
->cb_namewidth
= MAX(poolname_len
,
3900 max_width(zhp
, nvroot
, 0, cb
->cb_namewidth
,
3901 cb
->cb_name_flags
));
3904 * The width must be at least 10, but may be as large as the
3905 * column width - 42 so that we can still fit in one line.
3907 columns
= get_columns();
3909 if (cb
->cb_namewidth
< 10)
3910 cb
->cb_namewidth
= 10;
3911 if (cb
->cb_namewidth
> columns
- 42)
3912 cb
->cb_namewidth
= columns
- 42;
3918 * Parse the input string, get the 'interval' and 'count' value if there is one.
3921 get_interval_count(int *argcp
, char **argv
, float *iv
,
3925 unsigned long count
= 0;
3929 * Determine if the last argument is an integer or a pool name
3931 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3935 interval
= strtof(argv
[argc
- 1], &end
);
3937 if (*end
== '\0' && errno
== 0) {
3938 if (interval
== 0) {
3939 (void) fprintf(stderr
, gettext("interval "
3940 "cannot be zero\n"));
3944 * Ignore the last parameter
3949 * If this is not a valid number, just plow on. The
3950 * user will get a more informative error message later
3958 * If the last argument is also an integer, then we have both a count
3961 if (argc
> 0 && isnumber(argv
[argc
- 1])) {
3966 interval
= strtof(argv
[argc
- 1], &end
);
3968 if (*end
== '\0' && errno
== 0) {
3969 if (interval
== 0) {
3970 (void) fprintf(stderr
, gettext("interval "
3971 "cannot be zero\n"));
3976 * Ignore the last parameter
3990 get_timestamp_arg(char c
)
3993 timestamp_fmt
= UDATE
;
3995 timestamp_fmt
= DDATE
;
4001 * Return stat flags that are supported by all pools by both the module and
4002 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4003 * It will get ANDed down until only the flags that are supported on all pools
4007 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4009 uint64_t *mask
= data
;
4010 nvlist_t
*config
, *nvroot
, *nvx
;
4014 config
= zpool_get_config(zhp
, NULL
);
4015 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4018 /* Default stats are always supported, but for completeness.. */
4019 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4020 flags
|= IOS_DEFAULT_M
;
4022 /* Get our extended stats nvlist from the main list */
4023 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4026 * No extended stats; they're probably running an older
4027 * module. No big deal, we support that too.
4032 /* For each extended stat, make sure all its nvpairs are supported */
4033 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4034 if (!vsx_type_to_nvlist
[j
][0])
4037 /* Start off by assuming the flag is supported, then check */
4038 flags
|= (1ULL << j
);
4039 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4040 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4041 /* flag isn't supported */
4042 flags
= flags
& ~(1ULL << j
);
4048 *mask
= *mask
& flags
;
4053 * Return a bitmask of stats that are supported on all pools by both the module
4057 get_stat_flags(zpool_list_t
*list
)
4062 * get_stat_flags_cb() will lop off bits from "mask" until only the
4063 * flags that are supported on all pools remain.
4065 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4070 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4073 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4075 iostat_cbdata_t
*cb
= cb_data
;
4079 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4081 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4082 ret
= 1; /* match */
4089 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
4092 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
4094 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
4098 * Check if vdevs are in a pool
4100 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
4101 * return 0. If pool_name is NULL, then search all pools.
4104 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
4105 iostat_cbdata_t
*cb
)
4112 if ((argc
== 0) || !*argv
)
4118 /* Temporarily hijack cb_vdev_names for a second... */
4119 tmp_name
= cb
->cb_vdev_names
;
4121 /* Go though our list of prospective vdev names */
4122 for (i
= 0; i
< argc
; i
++) {
4123 cb
->cb_vdev_names
= argv
+ i
;
4125 /* Is this name a vdev in our pools? */
4126 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
4134 cb
->cb_vdev_names
= tmp_name
;
4140 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
4143 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
4150 * Do we have a pool named *name? If so, return 1, otherwise 0.
4155 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, is_pool_cb
, name
));
4158 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
4160 are_all_pools(int argc
, char **argv
)
4162 if ((argc
== 0) || !*argv
)
4166 if (!is_pool(argv
[argc
]))
4173 * Helper function to print out vdev/pool names we can't resolve. Used for an
4177 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
4178 iostat_cbdata_t
*cb
)
4183 for (i
= 0; i
< argc
; i
++) {
4187 str
= gettext("pool");
4188 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
4189 str
= gettext("vdev in this pool");
4190 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
4191 str
= gettext("vdev in another pool");
4193 str
= gettext("unknown");
4195 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
4200 * Same as get_interval_count(), but with additional checks to not misinterpret
4201 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
4205 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
4206 unsigned long *count
, iostat_cbdata_t
*cb
)
4208 char **tmpargv
= argv
;
4209 int argc_for_interval
= 0;
4211 /* Is the last arg an interval value? Or a guid? */
4212 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
4214 * The last arg is not a guid, so it's probably an
4217 argc_for_interval
++;
4220 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
4222 * The 2nd to last arg is not a guid, so it's probably
4223 * an interval value.
4225 argc_for_interval
++;
4229 /* Point to our list of possible intervals */
4230 tmpargv
= &argv
[*argc
- argc_for_interval
];
4232 *argc
= *argc
- argc_for_interval
;
4233 get_interval_count(&argc_for_interval
, tmpargv
,
4238 * Floating point sleep(). Allows you to pass in a floating point value for
4244 struct timespec req
;
4245 req
.tv_sec
= floor(sec
);
4246 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
4247 nanosleep(&req
, NULL
);
4251 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
4254 * name: Short name of the script ('iostat').
4255 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
4258 print_zpool_script_help(char *name
, char *path
)
4260 char *argv
[] = {path
, "-h", NULL
};
4261 char **lines
= NULL
;
4265 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
4267 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
4269 libzfs_free_str_array(lines
, lines_cnt
);
4273 for (int i
= 0; i
< lines_cnt
; i
++)
4274 if (!is_blank_str(lines
[i
]))
4275 printf(" %-14s %s\n", name
, lines
[i
]);
4277 libzfs_free_str_array(lines
, lines_cnt
);
4281 * Go though the zpool status/iostat -c scripts in the user's path, run their
4282 * help option (-h), and print out the results.
4285 print_zpool_dir_scripts(char *dirpath
)
4289 char fullpath
[MAXPATHLEN
];
4290 struct stat dir_stat
;
4292 if ((dir
= opendir(dirpath
)) != NULL
) {
4293 /* print all the files and directories within directory */
4294 while ((ent
= readdir(dir
)) != NULL
) {
4295 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
4297 /* Print the scripts */
4298 if (stat(fullpath
, &dir_stat
) == 0)
4299 if (dir_stat
.st_mode
& S_IXUSR
&&
4300 S_ISREG(dir_stat
.st_mode
))
4301 print_zpool_script_help(ent
->d_name
,
4309 * Print out help text for all zpool status/iostat -c scripts.
4312 print_zpool_script_list(char *subcommand
)
4316 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
4318 sp
= zpool_get_cmd_search_path();
4322 dir
= strtok(sp
, ":");
4323 while (dir
!= NULL
) {
4324 print_zpool_dir_scripts(dir
);
4325 dir
= strtok(NULL
, ":");
4332 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
4333 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
4334 * [interval [count]]
4336 * -c CMD For each vdev, run command CMD
4337 * -g Display guid for individual vdev name.
4338 * -L Follow links when resolving vdev path name.
4339 * -P Display full path for vdev name.
4340 * -v Display statistics for individual vdevs
4342 * -p Display values in parsable (exact) format.
4343 * -H Scripted mode. Don't display headers, and separate properties
4345 * -l Display average latency
4346 * -q Display queue depths
4347 * -w Display latency histograms
4348 * -r Display request size histogram
4349 * -T Display a timestamp in date(1) or Unix format
4351 * This command can be tricky because we want to be able to deal with pool
4352 * creation/destruction as well as vdev configuration changes. The bulk of this
4353 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
4354 * on pool_list_update() to detect the addition of new pools. Configuration
4355 * changes are all handled within libzfs.
4358 zpool_do_iostat(int argc
, char **argv
)
4364 unsigned long count
= 0;
4366 boolean_t verbose
= B_FALSE
;
4367 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
4368 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
4369 boolean_t omit_since_boot
= B_FALSE
;
4370 boolean_t guid
= B_FALSE
;
4371 boolean_t follow_links
= B_FALSE
;
4372 boolean_t full_name
= B_FALSE
;
4373 iostat_cbdata_t cb
= { 0 };
4376 /* Used for printing error message */
4377 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
4378 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
4380 uint64_t unsupported_flags
;
4383 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwH")) != -1) {
4388 gettext("Can't set -c flag twice\n"));
4392 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
4393 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
4394 fprintf(stderr
, gettext(
4395 "Can't run -c, disabled by "
4396 "ZPOOL_SCRIPTS_ENABLED.\n"));
4400 if ((getuid() <= 0 || geteuid() <= 0) &&
4401 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
4402 fprintf(stderr
, gettext(
4403 "Can't run -c with root privileges "
4404 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
4414 follow_links
= B_TRUE
;
4420 get_timestamp_arg(*optarg
);
4444 omit_since_boot
= B_TRUE
;
4450 if (optopt
== 'c') {
4451 print_zpool_script_list("iostat");
4455 gettext("invalid option '%c'\n"), optopt
);
4464 cb
.cb_literal
= parsable
;
4465 cb
.cb_scripted
= scripted
;
4468 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
4470 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4472 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
4473 cb
.cb_iteration
= 0;
4474 cb
.cb_namewidth
= 0;
4475 cb
.cb_verbose
= verbose
;
4477 /* Get our interval and count values (if any) */
4479 get_interval_count_filter_guids(&argc
, argv
, &interval
,
4482 get_interval_count(&argc
, argv
, &interval
, &count
);
4486 /* No args, so just print the defaults. */
4487 } else if (are_all_pools(argc
, argv
)) {
4488 /* All the args are pool names */
4489 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
4490 /* All the args are vdevs */
4491 cb
.cb_vdev_names
= argv
;
4492 cb
.cb_vdev_names_count
= argc
;
4493 argc
= 0; /* No pools to process */
4494 } else if (are_all_pools(1, argv
)) {
4495 /* The first arg is a pool name */
4496 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
4497 /* ...and the rest are vdev names */
4498 cb
.cb_vdev_names
= argv
+ 1;
4499 cb
.cb_vdev_names_count
= argc
- 1;
4500 argc
= 1; /* One pool to process */
4502 fprintf(stderr
, gettext("Expected either a list of "));
4503 fprintf(stderr
, gettext("pools, or list of vdevs in"));
4504 fprintf(stderr
, " \"%s\", ", argv
[0]);
4505 fprintf(stderr
, gettext("but got:\n"));
4506 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
4508 fprintf(stderr
, "\n");
4514 * The args don't make sense. The first arg isn't a pool name,
4515 * nor are all the args vdevs.
4517 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
4518 fprintf(stderr
, "\n");
4522 if (cb
.cb_vdev_names_count
!= 0) {
4524 * If user specified vdevs, it implies verbose.
4526 cb
.cb_verbose
= B_TRUE
;
4530 * Construct the list of all interesting pools.
4533 if ((list
= pool_list_get(argc
, argv
, NULL
, &ret
)) == NULL
)
4536 if (pool_list_count(list
) == 0 && argc
!= 0) {
4537 pool_list_free(list
);
4541 if (pool_list_count(list
) == 0 && interval
== 0) {
4542 pool_list_free(list
);
4543 (void) fprintf(stderr
, gettext("no pools available\n"));
4547 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
4548 pool_list_free(list
);
4549 (void) fprintf(stderr
,
4550 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
4555 if (l_histo
&& rq_histo
) {
4556 pool_list_free(list
);
4557 (void) fprintf(stderr
,
4558 gettext("Only one of [-r|-w] can be passed at a time\n"));
4564 * Enter the main iostat loop.
4570 * Histograms tables look out of place when you try to display
4571 * them with the other stats, so make a rule that you can only
4572 * print histograms by themselves.
4574 cb
.cb_flags
= IOS_L_HISTO_M
;
4575 } else if (rq_histo
) {
4576 cb
.cb_flags
= IOS_RQ_HISTO_M
;
4578 cb
.cb_flags
= IOS_DEFAULT_M
;
4580 cb
.cb_flags
|= IOS_LATENCY_M
;
4582 cb
.cb_flags
|= IOS_QUEUES_M
;
4586 * See if the module supports all the stats we want to display.
4588 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
4589 if (unsupported_flags
) {
4593 gettext("The loaded zfs module doesn't support:"));
4595 /* for each bit set in unsupported_flags */
4596 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
4597 idx
= lowbit64(f
) - 1;
4598 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
4601 fprintf(stderr
, ". Try running a newer module.\n");
4602 pool_list_free(list
);
4608 if ((npools
= pool_list_count(list
)) == 0)
4609 (void) fprintf(stderr
, gettext("no pools available\n"));
4612 * If this is the first iteration and -y was supplied
4613 * we skip any printing.
4615 boolean_t skip
= (omit_since_boot
&&
4616 cb
.cb_iteration
== 0);
4619 * Refresh all statistics. This is done as an
4620 * explicit step before calculating the maximum name
4621 * width, so that any * configuration changes are
4622 * properly accounted for.
4624 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
4628 * Iterate over all pools to determine the maximum width
4629 * for the pool / device name column across all pools.
4631 cb
.cb_namewidth
= 0;
4632 (void) pool_list_iter(list
, B_FALSE
, get_namewidth
,
4635 if (timestamp_fmt
!= NODATE
)
4636 print_timestamp(timestamp_fmt
);
4638 if (cmd
!= NULL
&& cb
.cb_verbose
&&
4639 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
4640 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
4641 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
4642 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
4648 * If it's the first time and we're not skipping it,
4649 * or either skip or verbose mode, print the header.
4651 * The histogram code explicitly prints its header on
4652 * every vdev, so skip this for histograms.
4654 if (((++cb
.cb_iteration
== 1 && !skip
) ||
4655 (skip
!= verbose
)) &&
4656 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
4658 print_iostat_header(&cb
);
4661 (void) fsleep(interval
);
4666 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
4669 * If there's more than one pool, and we're not in
4670 * verbose mode (which prints a separator for us),
4671 * then print a separator.
4673 * In addition, if we're printing specific vdevs then
4674 * we also want an ending separator.
4676 if (((npools
> 1 && !verbose
&&
4677 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
4678 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
4679 cb
.cb_vdev_names_count
)) &&
4681 print_iostat_separator(&cb
);
4682 if (cb
.vcdl
!= NULL
)
4683 print_cmd_columns(cb
.vcdl
, 1);
4687 if (cb
.vcdl
!= NULL
)
4688 free_vdev_cmd_data_list(cb
.vcdl
);
4693 * Flush the output so that redirection to a file isn't buffered
4696 (void) fflush(stdout
);
4701 if (count
!= 0 && --count
== 0)
4704 (void) fsleep(interval
);
4707 pool_list_free(list
);
4712 typedef struct list_cbdata
{
4713 boolean_t cb_verbose
;
4716 boolean_t cb_scripted
;
4717 zprop_list_t
*cb_proplist
;
4718 boolean_t cb_literal
;
4722 * Given a list of columns to display, output appropriate headers for each one.
4725 print_header(list_cbdata_t
*cb
)
4727 zprop_list_t
*pl
= cb
->cb_proplist
;
4728 char headerbuf
[ZPOOL_MAXPROPLEN
];
4730 boolean_t first
= B_TRUE
;
4731 boolean_t right_justify
;
4734 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4735 width
= pl
->pl_width
;
4736 if (first
&& cb
->cb_verbose
) {
4738 * Reset the width to accommodate the verbose listing
4741 width
= cb
->cb_namewidth
;
4749 right_justify
= B_FALSE
;
4750 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4751 header
= zpool_prop_column_name(pl
->pl_prop
);
4752 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4756 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
4757 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
4758 headerbuf
[i
] = '\0';
4762 if (pl
->pl_next
== NULL
&& !right_justify
)
4763 (void) printf("%s", header
);
4764 else if (right_justify
)
4765 (void) printf("%*s", (int)width
, header
);
4767 (void) printf("%-*s", (int)width
, header
);
4770 (void) printf("\n");
4774 * Given a pool and a list of properties, print out all the properties according
4775 * to the described layout.
4778 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
4780 zprop_list_t
*pl
= cb
->cb_proplist
;
4781 boolean_t first
= B_TRUE
;
4782 char property
[ZPOOL_MAXPROPLEN
];
4784 boolean_t right_justify
;
4787 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
4789 width
= pl
->pl_width
;
4790 if (first
&& cb
->cb_verbose
) {
4792 * Reset the width to accommodate the verbose listing
4795 width
= cb
->cb_namewidth
;
4799 if (cb
->cb_scripted
)
4800 (void) printf("\t");
4807 right_justify
= B_FALSE
;
4808 if (pl
->pl_prop
!= ZPROP_INVAL
) {
4809 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
4810 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
4815 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
4816 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
4817 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
4818 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
4819 sizeof (property
)) == 0) {
4827 * If this is being called in scripted mode, or if this is the
4828 * last column and it is left-justified, don't include a width
4831 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
4832 (void) printf("%s", propstr
);
4833 else if (right_justify
)
4834 (void) printf("%*s", (int)width
, propstr
);
4836 (void) printf("%-*s", (int)width
, propstr
);
4839 (void) printf("\n");
4843 print_one_column(zpool_prop_t prop
, uint64_t value
, boolean_t scripted
,
4844 boolean_t valid
, enum zfs_nicenum_format format
)
4848 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
4851 case ZPOOL_PROP_EXPANDSZ
:
4853 (void) strlcpy(propval
, "-", sizeof (propval
));
4855 zfs_nicenum_format(value
, propval
, sizeof (propval
),
4858 case ZPOOL_PROP_FRAGMENTATION
:
4859 if (value
== ZFS_FRAG_INVALID
) {
4860 (void) strlcpy(propval
, "-", sizeof (propval
));
4861 } else if (format
== ZFS_NICENUM_RAW
) {
4862 (void) snprintf(propval
, sizeof (propval
), "%llu",
4863 (unsigned long long)value
);
4865 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4866 (unsigned long long)value
);
4869 case ZPOOL_PROP_CAPACITY
:
4870 if (format
== ZFS_NICENUM_RAW
)
4871 (void) snprintf(propval
, sizeof (propval
), "%llu",
4872 (unsigned long long)value
);
4874 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
4875 (unsigned long long)value
);
4878 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
4882 (void) strlcpy(propval
, "-", sizeof (propval
));
4885 (void) printf("\t%s", propval
);
4887 (void) printf(" %*s", (int)width
, propval
);
4891 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
4892 list_cbdata_t
*cb
, int depth
)
4898 boolean_t scripted
= cb
->cb_scripted
;
4899 uint64_t islog
= B_FALSE
;
4900 boolean_t haslog
= B_FALSE
;
4901 char *dashes
= "%-*s - - - - - -\n";
4903 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
4904 (uint64_t **)&vs
, &c
) == 0);
4907 boolean_t toplevel
= (vs
->vs_space
!= 0);
4909 enum zfs_nicenum_format format
;
4912 format
= ZFS_NICENUM_RAW
;
4914 format
= ZFS_NICENUM_1024
;
4917 (void) printf("\t%s", name
);
4918 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
4919 (void) printf("%*s%s", depth
, "", name
);
4921 (void) printf("%*s%s%*s", depth
, "", name
,
4922 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
4925 * Print the properties for the individual vdevs. Some
4926 * properties are only applicable to toplevel vdevs. The
4927 * 'toplevel' boolean value is passed to the print_one_column()
4928 * to indicate that the value is valid.
4930 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, scripted
,
4932 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, scripted
,
4934 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
4935 scripted
, toplevel
, format
);
4936 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, scripted
,
4938 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
4939 vs
->vs_fragmentation
, scripted
,
4940 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
4942 cap
= (vs
->vs_space
== 0) ? 0 :
4943 (vs
->vs_alloc
* 100 / vs
->vs_space
);
4944 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, scripted
, toplevel
,
4946 (void) printf("\n");
4949 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
4950 &child
, &children
) != 0)
4953 for (c
= 0; c
< children
; c
++) {
4954 uint64_t ishole
= B_FALSE
;
4956 if (nvlist_lookup_uint64(child
[c
],
4957 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
4960 if (nvlist_lookup_uint64(child
[c
],
4961 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
) {
4966 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4968 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4972 if (haslog
== B_TRUE
) {
4973 /* LINTED E_SEC_PRINTF_VAR_FMT */
4974 (void) printf(dashes
, cb
->cb_namewidth
, "log");
4975 for (c
= 0; c
< children
; c
++) {
4976 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
4977 &islog
) != 0 || !islog
)
4979 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4981 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4986 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
4987 &child
, &children
) == 0 && children
> 0) {
4988 /* LINTED E_SEC_PRINTF_VAR_FMT */
4989 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
4990 for (c
= 0; c
< children
; c
++) {
4991 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
4993 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
4998 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
4999 &children
) == 0 && children
> 0) {
5000 /* LINTED E_SEC_PRINTF_VAR_FMT */
5001 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
5002 for (c
= 0; c
< children
; c
++) {
5003 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
5005 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2);
5013 * Generic callback function to list a pool.
5016 list_callback(zpool_handle_t
*zhp
, void *data
)
5018 list_cbdata_t
*cbp
= data
;
5022 config
= zpool_get_config(zhp
, NULL
);
5024 print_pool(zhp
, cbp
);
5025 if (!cbp
->cb_verbose
)
5028 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5030 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0);
5036 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
5038 * -g Display guid for individual vdev name.
5039 * -H Scripted mode. Don't display headers, and separate properties
5041 * -L Follow links when resolving vdev path name.
5042 * -o List of properties to display. Defaults to
5043 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
5044 * "dedupratio,health,altroot"
5045 * -p Display values in parsable (exact) format.
5046 * -P Display full path for vdev name.
5047 * -T Display a timestamp in date(1) or Unix format
5049 * List all pools in the system, whether or not they're healthy. Output space
5050 * statistics for each one, as well as health status summary.
5053 zpool_do_list(int argc
, char **argv
)
5057 list_cbdata_t cb
= { 0 };
5058 static char default_props
[] =
5059 "name,size,allocated,free,expandsize,fragmentation,capacity,"
5060 "dedupratio,health,altroot";
5061 char *props
= default_props
;
5063 unsigned long count
= 0;
5065 boolean_t first
= B_TRUE
;
5068 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
5071 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5074 cb
.cb_scripted
= B_TRUE
;
5077 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5083 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5086 cb
.cb_literal
= B_TRUE
;
5089 get_timestamp_arg(*optarg
);
5092 cb
.cb_verbose
= B_TRUE
;
5095 (void) fprintf(stderr
, gettext("missing argument for "
5096 "'%c' option\n"), optopt
);
5100 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5109 get_interval_count(&argc
, argv
, &interval
, &count
);
5111 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
5115 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
5119 if (pool_list_count(list
) == 0)
5122 if (timestamp_fmt
!= NODATE
)
5123 print_timestamp(timestamp_fmt
);
5125 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
5129 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
5134 if (count
!= 0 && --count
== 0)
5137 pool_list_free(list
);
5138 (void) fsleep(interval
);
5141 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
5142 (void) printf(gettext("no pools available\n"));
5146 pool_list_free(list
);
5147 zprop_free_list(cb
.cb_proplist
);
5152 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
5154 boolean_t force
= B_FALSE
;
5157 char *poolname
, *old_disk
, *new_disk
;
5158 zpool_handle_t
*zhp
;
5159 nvlist_t
*props
= NULL
;
5164 while ((c
= getopt(argc
, argv
, "fo:")) != -1) {
5170 if ((propval
= strchr(optarg
, '=')) == NULL
) {
5171 (void) fprintf(stderr
, gettext("missing "
5172 "'=' for -o option\n"));
5178 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
5179 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
5183 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5192 /* get pool name and check number of arguments */
5194 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5201 (void) fprintf(stderr
,
5202 gettext("missing <device> specification\n"));
5210 (void) fprintf(stderr
,
5211 gettext("missing <new_device> specification\n"));
5214 new_disk
= old_disk
;
5224 (void) fprintf(stderr
, gettext("too many arguments\n"));
5228 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
5233 if (zpool_get_config(zhp
, NULL
) == NULL
) {
5234 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
5241 /* unless manually specified use "ashift" pool property (if set) */
5242 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
5245 char strval
[ZPOOL_MAXPROPLEN
];
5247 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
5248 if (src
!= ZPROP_SRC_DEFAULT
) {
5249 (void) sprintf(strval
, "%" PRId32
, intval
);
5250 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
5251 &props
, B_TRUE
) == 0);
5255 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
5257 if (nvroot
== NULL
) {
5263 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
);
5266 nvlist_free(nvroot
);
5273 * zpool replace [-f] <pool> <device> <new_device>
5275 * -f Force attach, even if <new_device> appears to be in use.
5277 * Replace <device> with <new_device>.
5281 zpool_do_replace(int argc
, char **argv
)
5283 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
5287 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
5289 * -f Force attach, even if <new_device> appears to be in use.
5290 * -o Set property=value.
5292 * Attach <new_device> to the mirror containing <device>. If <device> is not
5293 * part of a mirror, then <device> will be transformed into a mirror of
5294 * <device> and <new_device>. In either case, <new_device> will begin life
5295 * with a DTL of [0, now], and will immediately begin to resilver itself.
5298 zpool_do_attach(int argc
, char **argv
)
5300 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
5304 * zpool detach [-f] <pool> <device>
5306 * -f Force detach of <device>, even if DTLs argue against it
5307 * (not supported yet)
5309 * Detach a device from a mirror. The operation will be refused if <device>
5310 * is the last device in the mirror, or if the DTLs indicate that this device
5311 * has the only valid copy of some data.
5315 zpool_do_detach(int argc
, char **argv
)
5318 char *poolname
, *path
;
5319 zpool_handle_t
*zhp
;
5323 while ((c
= getopt(argc
, argv
, "f")) != -1) {
5327 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5336 /* get pool name and check number of arguments */
5338 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5343 (void) fprintf(stderr
,
5344 gettext("missing <device> specification\n"));
5351 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5354 ret
= zpool_vdev_detach(zhp
, path
);
5362 * zpool split [-gLnP] [-o prop=val] ...
5364 * [-R altroot] <pool> <newpool> [<device> ...]
5366 * -g Display guid for individual vdev name.
5367 * -L Follow links when resolving vdev path name.
5368 * -n Do not split the pool, but display the resulting layout if
5369 * it were to be split.
5370 * -o Set property=value, or set mount options.
5371 * -P Display full path for vdev name.
5372 * -R Mount the split-off pool under an alternate root.
5374 * Splits the named pool and gives it the new pool name. Devices to be split
5375 * off may be listed, provided that no more than one device is specified
5376 * per top-level vdev mirror. The newly split pool is left in an exported
5377 * state unless -R is specified.
5379 * Restrictions: the top-level of the pool pool must only be made up of
5380 * mirrors; all devices in the pool must be healthy; no device may be
5381 * undergoing a resilvering operation.
5384 zpool_do_split(int argc
, char **argv
)
5386 char *srcpool
, *newpool
, *propval
;
5387 char *mntopts
= NULL
;
5390 zpool_handle_t
*zhp
;
5391 nvlist_t
*config
, *props
= NULL
;
5393 flags
.dryrun
= B_FALSE
;
5394 flags
.import
= B_FALSE
;
5395 flags
.name_flags
= 0;
5398 while ((c
= getopt(argc
, argv
, ":gLR:no:P")) != -1) {
5401 flags
.name_flags
|= VDEV_NAME_GUID
;
5404 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5407 flags
.import
= B_TRUE
;
5409 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
5410 &props
, B_TRUE
) != 0) {
5416 flags
.dryrun
= B_TRUE
;
5419 if ((propval
= strchr(optarg
, '=')) != NULL
) {
5422 if (add_prop_list(optarg
, propval
,
5423 &props
, B_TRUE
) != 0) {
5432 flags
.name_flags
|= VDEV_NAME_PATH
;
5435 (void) fprintf(stderr
, gettext("missing argument for "
5436 "'%c' option\n"), optopt
);
5440 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5447 if (!flags
.import
&& mntopts
!= NULL
) {
5448 (void) fprintf(stderr
, gettext("setting mntopts is only "
5449 "valid when importing the pool\n"));
5457 (void) fprintf(stderr
, gettext("Missing pool name\n"));
5461 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
5471 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
5476 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
5477 if (config
== NULL
) {
5481 (void) printf(gettext("would create '%s' with the "
5482 "following layout:\n\n"), newpool
);
5483 print_vdev_tree(NULL
, newpool
, config
, 0, B_FALSE
,
5490 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
5491 nvlist_free(config
);
5497 * The split was successful. Now we need to open the new
5498 * pool and import it.
5500 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
5501 nvlist_free(config
);
5505 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
5506 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
5508 (void) fprintf(stderr
, gettext("Split was successful, but "
5509 "the datasets could not all be mounted\n"));
5510 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
5511 "different altroot\n"), "zpool import");
5514 nvlist_free(config
);
5523 * zpool online <pool> <device> ...
5526 zpool_do_online(int argc
, char **argv
)
5530 zpool_handle_t
*zhp
;
5532 vdev_state_t newstate
;
5536 while ((c
= getopt(argc
, argv
, "et")) != -1) {
5539 flags
|= ZFS_ONLINE_EXPAND
;
5543 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5552 /* get pool name and check number of arguments */
5554 (void) fprintf(stderr
, gettext("missing pool name\n"));
5558 (void) fprintf(stderr
, gettext("missing device name\n"));
5564 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5567 for (i
= 1; i
< argc
; i
++) {
5568 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
5569 if (newstate
!= VDEV_STATE_HEALTHY
) {
5570 (void) printf(gettext("warning: device '%s' "
5571 "onlined, but remains in faulted state\n"),
5573 if (newstate
== VDEV_STATE_FAULTED
)
5574 (void) printf(gettext("use 'zpool "
5575 "clear' to restore a faulted "
5578 (void) printf(gettext("use 'zpool "
5579 "replace' to replace devices "
5580 "that are no longer present\n"));
5593 * zpool offline [-ft] <pool> <device> ...
5595 * -f Force the device into a faulted state.
5597 * -t Only take the device off-line temporarily. The offline/faulted
5598 * state will not be persistent across reboots.
5602 zpool_do_offline(int argc
, char **argv
)
5606 zpool_handle_t
*zhp
;
5608 boolean_t istmp
= B_FALSE
;
5609 boolean_t fault
= B_FALSE
;
5612 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
5621 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5630 /* get pool name and check number of arguments */
5632 (void) fprintf(stderr
, gettext("missing pool name\n"));
5636 (void) fprintf(stderr
, gettext("missing device name\n"));
5642 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5645 for (i
= 1; i
< argc
; i
++) {
5647 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
5649 if (istmp
== B_FALSE
) {
5650 /* Force the fault to persist across imports */
5651 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
5653 aux
= VDEV_AUX_EXTERNAL
;
5656 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
5659 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
5670 * zpool clear <pool> [device]
5672 * Clear all errors associated with a pool or a particular device.
5675 zpool_do_clear(int argc
, char **argv
)
5679 boolean_t dryrun
= B_FALSE
;
5680 boolean_t do_rewind
= B_FALSE
;
5681 boolean_t xtreme_rewind
= B_FALSE
;
5682 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
5683 nvlist_t
*policy
= NULL
;
5684 zpool_handle_t
*zhp
;
5685 char *pool
, *device
;
5688 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
5697 xtreme_rewind
= B_TRUE
;
5700 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5710 (void) fprintf(stderr
, gettext("missing pool name\n"));
5715 (void) fprintf(stderr
, gettext("too many arguments\n"));
5719 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
5720 (void) fprintf(stderr
,
5721 gettext("-n or -X only meaningful with -F\n"));
5725 rewind_policy
= ZPOOL_TRY_REWIND
;
5727 rewind_policy
= ZPOOL_DO_REWIND
;
5729 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
5731 /* In future, further rewind policy choices can be passed along here */
5732 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
5733 nvlist_add_uint32(policy
, ZPOOL_REWIND_REQUEST
, rewind_policy
) != 0)
5737 device
= argc
== 2 ? argv
[1] : NULL
;
5739 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
5740 nvlist_free(policy
);
5744 if (zpool_clear(zhp
, device
, policy
) != 0)
5749 nvlist_free(policy
);
5755 * zpool reguid <pool>
5758 zpool_do_reguid(int argc
, char **argv
)
5762 zpool_handle_t
*zhp
;
5766 while ((c
= getopt(argc
, argv
, "")) != -1) {
5769 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5778 /* get pool name and check number of arguments */
5780 (void) fprintf(stderr
, gettext("missing pool name\n"));
5785 (void) fprintf(stderr
, gettext("too many arguments\n"));
5790 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
5793 ret
= zpool_reguid(zhp
);
5801 * zpool reopen <pool>
5803 * Reopen the pool so that the kernel can update the sizes of all vdevs.
5806 zpool_do_reopen(int argc
, char **argv
)
5810 zpool_handle_t
*zhp
;
5814 while ((c
= getopt(argc
, argv
, "")) != -1) {
5817 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5827 (void) fprintf(stderr
, gettext("missing pool name\n"));
5832 (void) fprintf(stderr
, gettext("too many arguments\n"));
5837 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
)
5840 ret
= zpool_reopen(zhp
);
5845 typedef struct scrub_cbdata
{
5849 pool_scrub_cmd_t cb_scrub_cmd
;
5853 scrub_callback(zpool_handle_t
*zhp
, void *data
)
5855 scrub_cbdata_t
*cb
= data
;
5859 * Ignore faulted pools.
5861 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5862 (void) fprintf(stderr
, gettext("cannot scrub '%s': pool is "
5863 "currently unavailable\n"), zpool_get_name(zhp
));
5867 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
5873 * zpool scrub [-s | -p] <pool> ...
5875 * -s Stop. Stops any in-progress scrub.
5876 * -p Pause. Pause in-progress scrub.
5879 zpool_do_scrub(int argc
, char **argv
)
5884 cb
.cb_type
= POOL_SCAN_SCRUB
;
5885 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
5888 while ((c
= getopt(argc
, argv
, "sp")) != -1) {
5891 cb
.cb_type
= POOL_SCAN_NONE
;
5894 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
5897 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
5903 if (cb
.cb_type
== POOL_SCAN_NONE
&&
5904 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
5905 (void) fprintf(stderr
, gettext("invalid option combination: "
5906 "-s and -p are mutually exclusive\n"));
5916 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
5920 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, scrub_callback
, &cb
));
5924 * Print out detailed scrub status.
5927 print_scan_status(pool_scan_stat_t
*ps
)
5929 time_t start
, end
, pause
;
5930 uint64_t elapsed
, mins_left
, hours_left
;
5931 uint64_t pass_exam
, examined
, total
;
5933 double fraction_done
;
5934 char processed_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
5936 (void) printf(gettext(" scan: "));
5938 /* If there's never been a scan, there's not much to say. */
5939 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
5940 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
5941 (void) printf(gettext("none requested\n"));
5945 start
= ps
->pss_start_time
;
5946 end
= ps
->pss_end_time
;
5947 pause
= ps
->pss_pass_scrub_pause
;
5948 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
5950 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
5951 ps
->pss_func
== POOL_SCAN_RESILVER
);
5953 * Scan is finished or canceled.
5955 if (ps
->pss_state
== DSS_FINISHED
) {
5956 uint64_t minutes_taken
= (end
- start
) / 60;
5959 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5960 fmt
= gettext("scrub repaired %s in %lluh%um with "
5961 "%llu errors on %s");
5962 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5963 fmt
= gettext("resilvered %s in %lluh%um with "
5964 "%llu errors on %s");
5967 (void) printf(fmt
, processed_buf
,
5968 (u_longlong_t
)(minutes_taken
/ 60),
5969 (uint_t
)(minutes_taken
% 60),
5970 (u_longlong_t
)ps
->pss_errors
,
5971 ctime((time_t *)&end
));
5973 } else if (ps
->pss_state
== DSS_CANCELED
) {
5974 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5975 (void) printf(gettext("scrub canceled on %s"),
5977 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
5978 (void) printf(gettext("resilver canceled on %s"),
5984 assert(ps
->pss_state
== DSS_SCANNING
);
5987 * Scan is in progress.
5989 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
5991 (void) printf(gettext("scrub in progress since %s"),
5995 struct tm
*p
= localtime(&pause
);
5996 (void) strftime(buf
, sizeof (buf
), "%a %b %e %T %Y", p
);
5997 (void) printf(gettext("scrub paused since %s\n"), buf
);
5998 (void) printf(gettext("\tscrub started on %s"),
6001 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6002 (void) printf(gettext("resilver in progress since %s"),
6006 examined
= ps
->pss_examined
? ps
->pss_examined
: 1;
6007 total
= ps
->pss_to_examine
;
6008 fraction_done
= (double)examined
/ total
;
6010 /* elapsed time for this pass */
6011 elapsed
= time(NULL
) - ps
->pss_pass_start
;
6012 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
6013 elapsed
= elapsed
? elapsed
: 1;
6014 pass_exam
= ps
->pss_pass_exam
? ps
->pss_pass_exam
: 1;
6015 rate
= pass_exam
/ elapsed
;
6016 rate
= rate
? rate
: 1;
6017 mins_left
= ((total
- examined
) / rate
) / 60;
6018 hours_left
= mins_left
/ 60;
6020 zfs_nicebytes(examined
, examined_buf
, sizeof (examined_buf
));
6021 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
6024 * do not print estimated time if hours_left is more than 30 days
6025 * or we have a paused scrub
6028 zfs_nicebytes(rate
, rate_buf
, sizeof (rate_buf
));
6029 (void) printf(gettext("\t%s scanned out of %s at %s/s"),
6030 examined_buf
, total_buf
, rate_buf
);
6031 if (hours_left
< (30 * 24)) {
6032 (void) printf(gettext(", %lluh%um to go\n"),
6033 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
6035 (void) printf(gettext(
6036 ", (scan is slow, no estimated time)\n"));
6039 (void) printf(gettext("\t%s scanned out of %s\n"),
6040 examined_buf
, total_buf
);
6043 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
6044 (void) printf(gettext("\t%s resilvered, %.2f%% done\n"),
6045 processed_buf
, 100 * fraction_done
);
6046 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
6047 (void) printf(gettext("\t%s repaired, %.2f%% done\n"),
6048 processed_buf
, 100 * fraction_done
);
6053 print_error_log(zpool_handle_t
*zhp
)
6055 nvlist_t
*nverrlist
= NULL
;
6058 size_t len
= MAXPATHLEN
* 2;
6060 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
6063 (void) printf("errors: Permanent errors have been "
6064 "detected in the following files:\n\n");
6066 pathname
= safe_malloc(len
);
6068 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
6070 uint64_t dsobj
, obj
;
6072 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
6073 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
6075 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
6077 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
6078 (void) printf("%7s %s\n", "", pathname
);
6081 nvlist_free(nverrlist
);
6085 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
6094 (void) printf(gettext("\tspares\n"));
6096 for (i
= 0; i
< nspares
; i
++) {
6097 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
6099 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
);
6105 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
6114 (void) printf(gettext("\tcache\n"));
6116 for (i
= 0; i
< nl2cache
; i
++) {
6117 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
6119 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2, B_FALSE
);
6125 print_dedup_stats(nvlist_t
*config
)
6127 ddt_histogram_t
*ddh
;
6131 char dspace
[6], mspace
[6];
6134 * If the pool was faulted then we may not have been able to
6135 * obtain the config. Otherwise, if we have anything in the dedup
6136 * table continue processing the stats.
6138 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
6139 (uint64_t **)&ddo
, &c
) != 0)
6142 (void) printf("\n");
6143 (void) printf(gettext(" dedup: "));
6144 if (ddo
->ddo_count
== 0) {
6145 (void) printf(gettext("no DDT entries\n"));
6149 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
6150 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
6151 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
6152 (u_longlong_t
)ddo
->ddo_count
,
6156 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
6157 (uint64_t **)&dds
, &c
) == 0);
6158 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
6159 (uint64_t **)&ddh
, &c
) == 0);
6160 zpool_dump_ddt(dds
, ddh
);
6164 * Display a summary of pool status. Displays a summary such as:
6168 * reason: One or more devices ...
6169 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
6175 * When given the '-v' option, we print out the complete config. If the '-e'
6176 * option is specified, then we print out error rate information as well.
6179 status_callback(zpool_handle_t
*zhp
, void *data
)
6181 status_cbdata_t
*cbp
= data
;
6182 nvlist_t
*config
, *nvroot
;
6184 zpool_status_t reason
;
6185 zpool_errata_t errata
;
6190 config
= zpool_get_config(zhp
, NULL
);
6191 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
6196 * If we were given 'zpool status -x', only report those pools with
6199 if (cbp
->cb_explain
&&
6200 (reason
== ZPOOL_STATUS_OK
||
6201 reason
== ZPOOL_STATUS_VERSION_OLDER
||
6202 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
6203 if (!cbp
->cb_allpools
) {
6204 (void) printf(gettext("pool '%s' is healthy\n"),
6205 zpool_get_name(zhp
));
6207 cbp
->cb_first
= B_FALSE
;
6213 cbp
->cb_first
= B_FALSE
;
6215 (void) printf("\n");
6217 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6219 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
6220 (uint64_t **)&vs
, &c
) == 0);
6221 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6223 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp
));
6224 (void) printf(gettext(" state: %s\n"), health
);
6227 case ZPOOL_STATUS_MISSING_DEV_R
:
6228 (void) printf(gettext("status: One or more devices could not "
6229 "be opened. Sufficient replicas exist for\n\tthe pool to "
6230 "continue functioning in a degraded state.\n"));
6231 (void) printf(gettext("action: Attach the missing device and "
6232 "online it using 'zpool online'.\n"));
6235 case ZPOOL_STATUS_MISSING_DEV_NR
:
6236 (void) printf(gettext("status: One or more devices could not "
6237 "be opened. There are insufficient\n\treplicas for the "
6238 "pool to continue functioning.\n"));
6239 (void) printf(gettext("action: Attach the missing device and "
6240 "online it using 'zpool online'.\n"));
6243 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
6244 (void) printf(gettext("status: One or more devices could not "
6245 "be used because the label is missing or\n\tinvalid. "
6246 "Sufficient replicas exist for the pool to continue\n\t"
6247 "functioning in a degraded state.\n"));
6248 (void) printf(gettext("action: Replace the device using "
6249 "'zpool replace'.\n"));
6252 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
6253 (void) printf(gettext("status: One or more devices could not "
6254 "be used because the label is missing \n\tor invalid. "
6255 "There are insufficient replicas for the pool to "
6256 "continue\n\tfunctioning.\n"));
6257 zpool_explain_recover(zpool_get_handle(zhp
),
6258 zpool_get_name(zhp
), reason
, config
);
6261 case ZPOOL_STATUS_FAILING_DEV
:
6262 (void) printf(gettext("status: One or more devices has "
6263 "experienced an unrecoverable error. An\n\tattempt was "
6264 "made to correct the error. Applications are "
6266 (void) printf(gettext("action: Determine if the device needs "
6267 "to be replaced, and clear the errors\n\tusing "
6268 "'zpool clear' or replace the device with 'zpool "
6272 case ZPOOL_STATUS_OFFLINE_DEV
:
6273 (void) printf(gettext("status: One or more devices has "
6274 "been taken offline by the administrator.\n\tSufficient "
6275 "replicas exist for the pool to continue functioning in "
6276 "a\n\tdegraded state.\n"));
6277 (void) printf(gettext("action: Online the device using "
6278 "'zpool online' or replace the device with\n\t'zpool "
6282 case ZPOOL_STATUS_REMOVED_DEV
:
6283 (void) printf(gettext("status: One or more devices has "
6284 "been removed by the administrator.\n\tSufficient "
6285 "replicas exist for the pool to continue functioning in "
6286 "a\n\tdegraded state.\n"));
6287 (void) printf(gettext("action: Online the device using "
6288 "'zpool online' or replace the device with\n\t'zpool "
6292 case ZPOOL_STATUS_RESILVERING
:
6293 (void) printf(gettext("status: One or more devices is "
6294 "currently being resilvered. The pool will\n\tcontinue "
6295 "to function, possibly in a degraded state.\n"));
6296 (void) printf(gettext("action: Wait for the resilver to "
6300 case ZPOOL_STATUS_CORRUPT_DATA
:
6301 (void) printf(gettext("status: One or more devices has "
6302 "experienced an error resulting in data\n\tcorruption. "
6303 "Applications may be affected.\n"));
6304 (void) printf(gettext("action: Restore the file in question "
6305 "if possible. Otherwise restore the\n\tentire pool from "
6309 case ZPOOL_STATUS_CORRUPT_POOL
:
6310 (void) printf(gettext("status: The pool metadata is corrupted "
6311 "and the pool cannot be opened.\n"));
6312 zpool_explain_recover(zpool_get_handle(zhp
),
6313 zpool_get_name(zhp
), reason
, config
);
6316 case ZPOOL_STATUS_VERSION_OLDER
:
6317 (void) printf(gettext("status: The pool is formatted using a "
6318 "legacy on-disk format. The pool can\n\tstill be used, "
6319 "but some features are unavailable.\n"));
6320 (void) printf(gettext("action: Upgrade the pool using 'zpool "
6321 "upgrade'. Once this is done, the\n\tpool will no longer "
6322 "be accessible on software that does not support\n\t"
6323 "feature flags.\n"));
6326 case ZPOOL_STATUS_VERSION_NEWER
:
6327 (void) printf(gettext("status: The pool has been upgraded to a "
6328 "newer, incompatible on-disk version.\n\tThe pool cannot "
6329 "be accessed on this system.\n"));
6330 (void) printf(gettext("action: Access the pool from a system "
6331 "running more recent software, or\n\trestore the pool from "
6335 case ZPOOL_STATUS_FEAT_DISABLED
:
6336 (void) printf(gettext("status: Some supported features are not "
6337 "enabled on the pool. The pool can\n\tstill be used, but "
6338 "some features are unavailable.\n"));
6339 (void) printf(gettext("action: Enable all features using "
6340 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
6341 "longer be accessible by software that does not support\n\t"
6342 "the features. See zpool-features(5) for details.\n"));
6345 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
6346 (void) printf(gettext("status: The pool cannot be accessed on "
6347 "this system because it uses the\n\tfollowing feature(s) "
6348 "not supported on this system:\n"));
6349 zpool_print_unsup_feat(config
);
6350 (void) printf("\n");
6351 (void) printf(gettext("action: Access the pool from a system "
6352 "that supports the required feature(s),\n\tor restore the "
6353 "pool from backup.\n"));
6356 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
6357 (void) printf(gettext("status: The pool can only be accessed "
6358 "in read-only mode on this system. It\n\tcannot be "
6359 "accessed in read-write mode because it uses the "
6360 "following\n\tfeature(s) not supported on this system:\n"));
6361 zpool_print_unsup_feat(config
);
6362 (void) printf("\n");
6363 (void) printf(gettext("action: The pool cannot be accessed in "
6364 "read-write mode. Import the pool with\n"
6365 "\t\"-o readonly=on\", access the pool from a system that "
6366 "supports the\n\trequired feature(s), or restore the "
6367 "pool from backup.\n"));
6370 case ZPOOL_STATUS_FAULTED_DEV_R
:
6371 (void) printf(gettext("status: One or more devices are "
6372 "faulted in response to persistent errors.\n\tSufficient "
6373 "replicas exist for the pool to continue functioning "
6374 "in a\n\tdegraded state.\n"));
6375 (void) printf(gettext("action: Replace the faulted device, "
6376 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
6379 case ZPOOL_STATUS_FAULTED_DEV_NR
:
6380 (void) printf(gettext("status: One or more devices are "
6381 "faulted in response to persistent errors. There are "
6382 "insufficient replicas for the pool to\n\tcontinue "
6384 (void) printf(gettext("action: Destroy and re-create the pool "
6385 "from a backup source. Manually marking the device\n"
6386 "\trepaired using 'zpool clear' may allow some data "
6387 "to be recovered.\n"));
6390 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
6391 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
6392 (void) printf(gettext("status: One or more devices are "
6393 "faulted in response to IO failures.\n"));
6394 (void) printf(gettext("action: Make sure the affected devices "
6395 "are connected, then run 'zpool clear'.\n"));
6398 case ZPOOL_STATUS_BAD_LOG
:
6399 (void) printf(gettext("status: An intent log record "
6400 "could not be read.\n"
6401 "\tWaiting for administrator intervention to fix the "
6402 "faulted pool.\n"));
6403 (void) printf(gettext("action: Either restore the affected "
6404 "device(s) and run 'zpool online',\n"
6405 "\tor ignore the intent log records by running "
6406 "'zpool clear'.\n"));
6409 case ZPOOL_STATUS_HOSTID_MISMATCH
:
6410 (void) printf(gettext("status: Mismatch between pool hostid "
6411 "and system hostid on imported pool.\n\tThis pool was "
6412 "previously imported into a system with a different "
6413 "hostid,\n\tand then was verbatim imported into this "
6415 (void) printf(gettext("action: Export this pool on all systems "
6416 "on which it is imported.\n"
6417 "\tThen import it to correct the mismatch.\n"));
6420 case ZPOOL_STATUS_ERRATA
:
6421 (void) printf(gettext("status: Errata #%d detected.\n"),
6425 case ZPOOL_ERRATA_NONE
:
6428 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
6429 (void) printf(gettext("action: To correct the issue "
6430 "run 'zpool scrub'.\n"));
6435 * All errata which allow the pool to be imported
6436 * must contain an action message.
6444 * The remaining errors can't actually be generated, yet.
6446 assert(reason
== ZPOOL_STATUS_OK
);
6450 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
6453 if (config
!= NULL
) {
6455 nvlist_t
**spares
, **l2cache
;
6456 uint_t nspares
, nl2cache
;
6457 pool_scan_stat_t
*ps
= NULL
;
6459 (void) nvlist_lookup_uint64_array(nvroot
,
6460 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &c
);
6461 print_scan_status(ps
);
6463 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
6464 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6465 if (cbp
->cb_namewidth
< 10)
6466 cbp
->cb_namewidth
= 10;
6468 (void) printf(gettext("config:\n\n"));
6469 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
6470 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
6473 if (cbp
->vcdl
!= NULL
)
6474 print_cmd_columns(cbp
->vcdl
, 0);
6477 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
6480 if (num_logs(nvroot
) > 0)
6481 print_logs(zhp
, cbp
, nvroot
);
6482 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6483 &l2cache
, &nl2cache
) == 0)
6484 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
6486 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6487 &spares
, &nspares
) == 0)
6488 print_spares(zhp
, cbp
, spares
, nspares
);
6490 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
6492 nvlist_t
*nverrlist
= NULL
;
6495 * If the approximate error count is small, get a
6496 * precise count by fetching the entire log and
6497 * uniquifying the results.
6499 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
6500 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
6505 while ((elem
= nvlist_next_nvpair(nverrlist
,
6510 nvlist_free(nverrlist
);
6512 (void) printf("\n");
6515 (void) printf(gettext("errors: No known data "
6517 else if (!cbp
->cb_verbose
)
6518 (void) printf(gettext("errors: %llu data "
6519 "errors, use '-v' for a list\n"),
6520 (u_longlong_t
)nerr
);
6522 print_error_log(zhp
);
6525 if (cbp
->cb_dedup_stats
)
6526 print_dedup_stats(config
);
6528 (void) printf(gettext("config: The configuration cannot be "
6536 * zpool status [-c [script1,script2,...]] [-gLPvx] [-T d|u] [pool] ...
6537 * [interval [count]]
6539 * -c CMD For each vdev, run command CMD
6540 * -g Display guid for individual vdev name.
6541 * -L Follow links when resolving vdev path name.
6542 * -P Display full path for vdev name.
6543 * -v Display complete error logs
6544 * -x Display only pools with potential problems
6545 * -D Display dedup status (undocumented)
6546 * -T Display a timestamp in date(1) or Unix format
6548 * Describes the health status of all pools or some subset.
6551 zpool_do_status(int argc
, char **argv
)
6556 unsigned long count
= 0;
6557 status_cbdata_t cb
= { 0 };
6561 while ((c
= getopt(argc
, argv
, "c:gLPvxDT:")) != -1) {
6566 gettext("Can't set -c flag twice\n"));
6570 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
6571 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6572 fprintf(stderr
, gettext(
6573 "Can't run -c, disabled by "
6574 "ZPOOL_SCRIPTS_ENABLED.\n"));
6578 if ((getuid() <= 0 || geteuid() <= 0) &&
6579 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6580 fprintf(stderr
, gettext(
6581 "Can't run -c with root privileges "
6582 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6588 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6591 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6594 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6597 cb
.cb_verbose
= B_TRUE
;
6600 cb
.cb_explain
= B_TRUE
;
6603 cb
.cb_dedup_stats
= B_TRUE
;
6606 get_timestamp_arg(*optarg
);
6609 if (optopt
== 'c') {
6610 print_zpool_script_list("status");
6614 gettext("invalid option '%c'\n"), optopt
);
6623 get_interval_count(&argc
, argv
, &interval
, &count
);
6626 cb
.cb_allpools
= B_TRUE
;
6628 cb
.cb_first
= B_TRUE
;
6629 cb
.cb_print_status
= B_TRUE
;
6632 if (timestamp_fmt
!= NODATE
)
6633 print_timestamp(timestamp_fmt
);
6636 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
6639 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
,
6640 status_callback
, &cb
);
6642 if (cb
.vcdl
!= NULL
)
6643 free_vdev_cmd_data_list(cb
.vcdl
);
6645 if (argc
== 0 && cb
.cb_count
== 0)
6646 (void) fprintf(stderr
, gettext("no pools available\n"));
6647 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
6648 (void) printf(gettext("all pools are healthy\n"));
6656 if (count
!= 0 && --count
== 0)
6659 (void) fsleep(interval
);
6665 typedef struct upgrade_cbdata
{
6668 uint64_t cb_version
;
6673 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
6675 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
6676 int *count
= (int *)unsupp_fs
;
6678 if (zfs_version
> ZPL_VERSION
) {
6679 (void) printf(gettext("%s (v%d) is not supported by this "
6680 "implementation of ZFS.\n"),
6681 zfs_get_name(zhp
), zfs_version
);
6685 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
6693 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
6697 uint64_t oldversion
;
6700 config
= zpool_get_config(zhp
, NULL
);
6701 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6704 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
6705 assert(oldversion
< version
);
6707 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
6712 (void) fprintf(stderr
, gettext("Upgrade not performed due "
6713 "to %d unsupported filesystems (max v%d).\n"),
6714 unsupp_fs
, (int)ZPL_VERSION
);
6718 ret
= zpool_upgrade(zhp
, version
);
6722 if (version
>= SPA_VERSION_FEATURES
) {
6723 (void) printf(gettext("Successfully upgraded "
6724 "'%s' from version %llu to feature flags.\n"),
6725 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
6727 (void) printf(gettext("Successfully upgraded "
6728 "'%s' from version %llu to version %llu.\n"),
6729 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
6730 (u_longlong_t
)version
);
6737 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
6740 boolean_t firstff
= B_TRUE
;
6741 nvlist_t
*enabled
= zpool_get_features(zhp
);
6744 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6745 const char *fname
= spa_feature_table
[i
].fi_uname
;
6746 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6747 if (!nvlist_exists(enabled
, fguid
)) {
6749 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
6750 ret
= zpool_set_prop(zhp
, propname
,
6751 ZFS_FEATURE_ENABLED
);
6759 (void) printf(gettext("Enabled the "
6760 "following features on '%s':\n"),
6761 zpool_get_name(zhp
));
6764 (void) printf(gettext(" %s\n"), fname
);
6775 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
6777 upgrade_cbdata_t
*cbp
= arg
;
6780 boolean_t printnl
= B_FALSE
;
6783 config
= zpool_get_config(zhp
, NULL
);
6784 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6787 assert(SPA_VERSION_IS_SUPPORTED(version
));
6789 if (version
< cbp
->cb_version
) {
6790 cbp
->cb_first
= B_FALSE
;
6791 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6797 * If they did "zpool upgrade -a", then we could
6798 * be doing ioctls to different pools. We need
6799 * to log this history once to each pool, and bypass
6800 * the normal history logging that happens in main().
6802 (void) zpool_log_history(g_zfs
, history_str
);
6803 log_history
= B_FALSE
;
6806 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6808 ret
= upgrade_enable_all(zhp
, &count
);
6813 cbp
->cb_first
= B_FALSE
;
6819 (void) printf(gettext("\n"));
6826 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
6828 upgrade_cbdata_t
*cbp
= arg
;
6832 config
= zpool_get_config(zhp
, NULL
);
6833 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6836 assert(SPA_VERSION_IS_SUPPORTED(version
));
6838 if (version
< SPA_VERSION_FEATURES
) {
6839 if (cbp
->cb_first
) {
6840 (void) printf(gettext("The following pools are "
6841 "formatted with legacy version numbers and can\n"
6842 "be upgraded to use feature flags. After "
6843 "being upgraded, these pools\nwill no "
6844 "longer be accessible by software that does not "
6845 "support feature\nflags.\n\n"));
6846 (void) printf(gettext("VER POOL\n"));
6847 (void) printf(gettext("--- ------------\n"));
6848 cbp
->cb_first
= B_FALSE
;
6851 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
6852 zpool_get_name(zhp
));
6859 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
6861 upgrade_cbdata_t
*cbp
= arg
;
6865 config
= zpool_get_config(zhp
, NULL
);
6866 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
6869 if (version
>= SPA_VERSION_FEATURES
) {
6871 boolean_t poolfirst
= B_TRUE
;
6872 nvlist_t
*enabled
= zpool_get_features(zhp
);
6874 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6875 const char *fguid
= spa_feature_table
[i
].fi_guid
;
6876 const char *fname
= spa_feature_table
[i
].fi_uname
;
6877 if (!nvlist_exists(enabled
, fguid
)) {
6878 if (cbp
->cb_first
) {
6879 (void) printf(gettext("\nSome "
6880 "supported features are not "
6881 "enabled on the following pools. "
6882 "Once a\nfeature is enabled the "
6883 "pool may become incompatible with "
6884 "software\nthat does not support "
6886 "zpool-features(5) for "
6888 (void) printf(gettext("POOL "
6890 (void) printf(gettext("------"
6892 cbp
->cb_first
= B_FALSE
;
6896 (void) printf(gettext("%s\n"),
6897 zpool_get_name(zhp
));
6898 poolfirst
= B_FALSE
;
6901 (void) printf(gettext(" %s\n"), fname
);
6904 * If they did "zpool upgrade -a", then we could
6905 * be doing ioctls to different pools. We need
6906 * to log this history once to each pool, and bypass
6907 * the normal history logging that happens in main().
6909 (void) zpool_log_history(g_zfs
, history_str
);
6910 log_history
= B_FALSE
;
6919 upgrade_one(zpool_handle_t
*zhp
, void *data
)
6921 boolean_t printnl
= B_FALSE
;
6922 upgrade_cbdata_t
*cbp
= data
;
6923 uint64_t cur_version
;
6926 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
6927 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
6928 "Pool 'log' must be renamed using export and import"
6933 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
6934 if (cur_version
> cbp
->cb_version
) {
6935 (void) printf(gettext("Pool '%s' is already formatted "
6936 "using more current version '%llu'.\n\n"),
6937 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
6941 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
6942 (void) printf(gettext("Pool '%s' is already formatted "
6943 "using version %llu.\n\n"), zpool_get_name(zhp
),
6944 (u_longlong_t
)cbp
->cb_version
);
6948 if (cur_version
!= cbp
->cb_version
) {
6950 ret
= upgrade_version(zhp
, cbp
->cb_version
);
6955 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
6957 ret
= upgrade_enable_all(zhp
, &count
);
6963 } else if (cur_version
== SPA_VERSION
) {
6964 (void) printf(gettext("Pool '%s' already has all "
6965 "supported features enabled.\n"),
6966 zpool_get_name(zhp
));
6971 (void) printf(gettext("\n"));
6980 * zpool upgrade [-V version] <-a | pool ...>
6982 * With no arguments, display downrev'd ZFS pool available for upgrade.
6983 * Individual pools can be upgraded by specifying the pool, and '-a' will
6984 * upgrade all pools.
6987 zpool_do_upgrade(int argc
, char **argv
)
6990 upgrade_cbdata_t cb
= { 0 };
6992 boolean_t showversions
= B_FALSE
;
6993 boolean_t upgradeall
= B_FALSE
;
6998 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
7001 upgradeall
= B_TRUE
;
7004 showversions
= B_TRUE
;
7007 cb
.cb_version
= strtoll(optarg
, &end
, 10);
7009 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
7010 (void) fprintf(stderr
,
7011 gettext("invalid version '%s'\n"), optarg
);
7016 (void) fprintf(stderr
, gettext("missing argument for "
7017 "'%c' option\n"), optopt
);
7021 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7032 if (cb
.cb_version
== 0) {
7033 cb
.cb_version
= SPA_VERSION
;
7034 } else if (!upgradeall
&& argc
== 0) {
7035 (void) fprintf(stderr
, gettext("-V option is "
7036 "incompatible with other arguments\n"));
7041 if (upgradeall
|| argc
!= 0) {
7042 (void) fprintf(stderr
, gettext("-v option is "
7043 "incompatible with other arguments\n"));
7046 } else if (upgradeall
) {
7048 (void) fprintf(stderr
, gettext("-a option should not "
7049 "be used along with a pool name\n"));
7054 (void) printf(gettext("This system supports ZFS pool feature "
7059 (void) printf(gettext("The following features are "
7061 (void) printf(gettext("FEAT DESCRIPTION\n"));
7062 (void) printf("----------------------------------------------"
7063 "---------------\n");
7064 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7065 zfeature_info_t
*fi
= &spa_feature_table
[i
];
7067 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
7068 " (read-only compatible)" : "";
7070 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
7071 (void) printf(" %s\n", fi
->fi_desc
);
7073 (void) printf("\n");
7075 (void) printf(gettext("The following legacy versions are also "
7077 (void) printf(gettext("VER DESCRIPTION\n"));
7078 (void) printf("--- -----------------------------------------"
7079 "---------------\n");
7080 (void) printf(gettext(" 1 Initial ZFS version\n"));
7081 (void) printf(gettext(" 2 Ditto blocks "
7082 "(replicated metadata)\n"));
7083 (void) printf(gettext(" 3 Hot spares and double parity "
7085 (void) printf(gettext(" 4 zpool history\n"));
7086 (void) printf(gettext(" 5 Compression using the gzip "
7088 (void) printf(gettext(" 6 bootfs pool property\n"));
7089 (void) printf(gettext(" 7 Separate intent log devices\n"));
7090 (void) printf(gettext(" 8 Delegated administration\n"));
7091 (void) printf(gettext(" 9 refquota and refreservation "
7093 (void) printf(gettext(" 10 Cache devices\n"));
7094 (void) printf(gettext(" 11 Improved scrub performance\n"));
7095 (void) printf(gettext(" 12 Snapshot properties\n"));
7096 (void) printf(gettext(" 13 snapused property\n"));
7097 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
7098 (void) printf(gettext(" 15 user/group space accounting\n"));
7099 (void) printf(gettext(" 16 stmf property support\n"));
7100 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
7101 (void) printf(gettext(" 18 Snapshot user holds\n"));
7102 (void) printf(gettext(" 19 Log device removal\n"));
7103 (void) printf(gettext(" 20 Compression using zle "
7104 "(zero-length encoding)\n"));
7105 (void) printf(gettext(" 21 Deduplication\n"));
7106 (void) printf(gettext(" 22 Received properties\n"));
7107 (void) printf(gettext(" 23 Slim ZIL\n"));
7108 (void) printf(gettext(" 24 System attributes\n"));
7109 (void) printf(gettext(" 25 Improved scrub stats\n"));
7110 (void) printf(gettext(" 26 Improved snapshot deletion "
7112 (void) printf(gettext(" 27 Improved snapshot creation "
7114 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
7115 (void) printf(gettext("\nFor more information on a particular "
7116 "version, including supported releases,\n"));
7117 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
7118 } else if (argc
== 0 && upgradeall
) {
7119 cb
.cb_first
= B_TRUE
;
7120 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
7121 if (ret
== 0 && cb
.cb_first
) {
7122 if (cb
.cb_version
== SPA_VERSION
) {
7123 (void) printf(gettext("All pools are already "
7124 "formatted using feature flags.\n\n"));
7125 (void) printf(gettext("Every feature flags "
7126 "pool already has all supported features "
7129 (void) printf(gettext("All pools are already "
7130 "formatted with version %llu or higher.\n"),
7131 (u_longlong_t
)cb
.cb_version
);
7134 } else if (argc
== 0) {
7135 cb
.cb_first
= B_TRUE
;
7136 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
7140 (void) printf(gettext("All pools are formatted "
7141 "using feature flags.\n\n"));
7143 (void) printf(gettext("\nUse 'zpool upgrade -v' "
7144 "for a list of available legacy versions.\n"));
7147 cb
.cb_first
= B_TRUE
;
7148 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
7152 (void) printf(gettext("Every feature flags pool has "
7153 "all supported features enabled.\n"));
7155 (void) printf(gettext("\n"));
7158 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
,
7165 typedef struct hist_cbdata
{
7172 * Print out the command history for a specific pool.
7175 get_history_one(zpool_handle_t
*zhp
, void *data
)
7181 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
7183 cb
->first
= B_FALSE
;
7185 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
7187 if ((ret
= zpool_get_history(zhp
, &nvhis
)) != 0)
7190 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
7191 &records
, &numrecords
) == 0);
7192 for (i
= 0; i
< numrecords
; i
++) {
7193 nvlist_t
*rec
= records
[i
];
7196 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
7200 tsec
= fnvlist_lookup_uint64(records
[i
],
7202 (void) localtime_r(&tsec
, &t
);
7203 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
7206 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
7207 (void) printf("%s %s", tbuf
,
7208 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
7209 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
7211 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
7214 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
7215 (void) printf("%s unrecognized record:\n",
7217 dump_nvlist(rec
, 4);
7220 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
7221 zfs_history_event_names
[ievent
],
7222 (longlong_t
)fnvlist_lookup_uint64(
7223 rec
, ZPOOL_HIST_TXG
),
7224 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
7225 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
7228 (void) printf("%s [txg:%lld] %s", tbuf
,
7229 (longlong_t
)fnvlist_lookup_uint64(
7230 rec
, ZPOOL_HIST_TXG
),
7231 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
7232 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
7233 (void) printf(" %s (%llu)",
7234 fnvlist_lookup_string(rec
,
7236 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
7239 (void) printf(" %s", fnvlist_lookup_string(rec
,
7240 ZPOOL_HIST_INT_STR
));
7241 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
7244 (void) printf("%s ioctl %s\n", tbuf
,
7245 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
7246 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
7247 (void) printf(" input:\n");
7248 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7249 ZPOOL_HIST_INPUT_NVL
), 8);
7251 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
7252 (void) printf(" output:\n");
7253 dump_nvlist(fnvlist_lookup_nvlist(rec
,
7254 ZPOOL_HIST_OUTPUT_NVL
), 8);
7259 (void) printf("%s unrecognized record:\n", tbuf
);
7260 dump_nvlist(rec
, 4);
7264 (void) printf("\n");
7267 (void) printf(" [");
7268 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
7269 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
7270 struct passwd
*pwd
= getpwuid(who
);
7271 (void) printf("user %d ", (int)who
);
7273 (void) printf("(%s) ", pwd
->pw_name
);
7275 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
7276 (void) printf("on %s",
7277 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
7279 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
7280 (void) printf(":%s",
7281 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
7285 (void) printf("\n");
7287 (void) printf("\n");
7294 * zpool history <pool>
7296 * Displays the history of commands that modified pools.
7299 zpool_do_history(int argc
, char **argv
)
7301 hist_cbdata_t cbdata
= { 0 };
7305 cbdata
.first
= B_TRUE
;
7307 while ((c
= getopt(argc
, argv
, "li")) != -1) {
7310 cbdata
.longfmt
= B_TRUE
;
7313 cbdata
.internal
= B_TRUE
;
7316 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7324 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, get_history_one
,
7327 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
7328 (void) fprintf(stderr
, gettext("no pools available\n"));
7335 typedef struct ev_opts
{
7343 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
7345 char ctime_str
[26], str
[32], *ptr
;
7349 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
7350 memset(str
, ' ', 32);
7351 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
7352 (void) strncpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
7353 (void) strncpy(str
+7, ctime_str
+20, 4); /* '1993' */
7354 (void) strncpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
7355 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
7357 (void) printf(gettext("%s\t"), str
);
7359 (void) printf(gettext("%s "), str
);
7361 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
7362 (void) printf(gettext("%s\n"), ptr
);
7366 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
7370 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
7371 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
7373 data_type_t type
= nvpair_type(nvp
);
7374 const char *name
= nvpair_name(nvp
);
7384 printf(gettext("%*s%s = "), depth
, "", name
);
7387 case DATA_TYPE_BOOLEAN
:
7388 printf(gettext("%s"), "1");
7391 case DATA_TYPE_BOOLEAN_VALUE
:
7392 (void) nvpair_value_boolean_value(nvp
, &b
);
7393 printf(gettext("%s"), b
? "1" : "0");
7396 case DATA_TYPE_BYTE
:
7397 (void) nvpair_value_byte(nvp
, &i8
);
7398 printf(gettext("0x%x"), i8
);
7401 case DATA_TYPE_INT8
:
7402 (void) nvpair_value_int8(nvp
, (void *)&i8
);
7403 printf(gettext("0x%x"), i8
);
7406 case DATA_TYPE_UINT8
:
7407 (void) nvpair_value_uint8(nvp
, &i8
);
7408 printf(gettext("0x%x"), i8
);
7411 case DATA_TYPE_INT16
:
7412 (void) nvpair_value_int16(nvp
, (void *)&i16
);
7413 printf(gettext("0x%x"), i16
);
7416 case DATA_TYPE_UINT16
:
7417 (void) nvpair_value_uint16(nvp
, &i16
);
7418 printf(gettext("0x%x"), i16
);
7421 case DATA_TYPE_INT32
:
7422 (void) nvpair_value_int32(nvp
, (void *)&i32
);
7423 printf(gettext("0x%x"), i32
);
7426 case DATA_TYPE_UINT32
:
7427 (void) nvpair_value_uint32(nvp
, &i32
);
7428 printf(gettext("0x%x"), i32
);
7431 case DATA_TYPE_INT64
:
7432 (void) nvpair_value_int64(nvp
, (void *)&i64
);
7433 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7436 case DATA_TYPE_UINT64
:
7437 (void) nvpair_value_uint64(nvp
, &i64
);
7439 * translate vdev state values to readable
7440 * strings to aide zpool events consumers
7443 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
7445 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
7446 printf(gettext("\"%s\" (0x%llx)"),
7447 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
7450 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7454 case DATA_TYPE_HRTIME
:
7455 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
7456 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
7459 case DATA_TYPE_STRING
:
7460 (void) nvpair_value_string(nvp
, &str
);
7461 printf(gettext("\"%s\""), str
? str
: "<NULL>");
7464 case DATA_TYPE_NVLIST
:
7465 printf(gettext("(embedded nvlist)\n"));
7466 (void) nvpair_value_nvlist(nvp
, &cnv
);
7467 zpool_do_events_nvprint(cnv
, depth
+ 8);
7468 printf(gettext("%*s(end %s)"), depth
, "", name
);
7471 case DATA_TYPE_NVLIST_ARRAY
: {
7475 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
7476 printf(gettext("(%d embedded nvlists)\n"), nelem
);
7477 for (i
= 0; i
< nelem
; i
++) {
7478 printf(gettext("%*s%s[%d] = %s\n"),
7479 depth
, "", name
, i
, "(embedded nvlist)");
7480 zpool_do_events_nvprint(val
[i
], depth
+ 8);
7481 printf(gettext("%*s(end %s[%i])\n"),
7482 depth
, "", name
, i
);
7484 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
7488 case DATA_TYPE_INT8_ARRAY
: {
7492 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
7493 for (i
= 0; i
< nelem
; i
++)
7494 printf(gettext("0x%x "), val
[i
]);
7499 case DATA_TYPE_UINT8_ARRAY
: {
7503 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
7504 for (i
= 0; i
< nelem
; i
++)
7505 printf(gettext("0x%x "), val
[i
]);
7510 case DATA_TYPE_INT16_ARRAY
: {
7514 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
7515 for (i
= 0; i
< nelem
; i
++)
7516 printf(gettext("0x%x "), val
[i
]);
7521 case DATA_TYPE_UINT16_ARRAY
: {
7525 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
7526 for (i
= 0; i
< nelem
; i
++)
7527 printf(gettext("0x%x "), val
[i
]);
7532 case DATA_TYPE_INT32_ARRAY
: {
7536 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
7537 for (i
= 0; i
< nelem
; i
++)
7538 printf(gettext("0x%x "), val
[i
]);
7543 case DATA_TYPE_UINT32_ARRAY
: {
7547 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
7548 for (i
= 0; i
< nelem
; i
++)
7549 printf(gettext("0x%x "), val
[i
]);
7554 case DATA_TYPE_INT64_ARRAY
: {
7558 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
7559 for (i
= 0; i
< nelem
; i
++)
7560 printf(gettext("0x%llx "),
7561 (u_longlong_t
)val
[i
]);
7566 case DATA_TYPE_UINT64_ARRAY
: {
7570 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
7571 for (i
= 0; i
< nelem
; i
++)
7572 printf(gettext("0x%llx "),
7573 (u_longlong_t
)val
[i
]);
7578 case DATA_TYPE_STRING_ARRAY
: {
7582 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
7583 for (i
= 0; i
< nelem
; i
++)
7584 printf(gettext("\"%s\" "),
7585 str
[i
] ? str
[i
] : "<NULL>");
7590 case DATA_TYPE_BOOLEAN_ARRAY
:
7591 case DATA_TYPE_BYTE_ARRAY
:
7592 case DATA_TYPE_DOUBLE
:
7593 case DATA_TYPE_UNKNOWN
:
7594 printf(gettext("<unknown>"));
7598 printf(gettext("\n"));
7603 zpool_do_events_next(ev_opts_t
*opts
)
7606 int zevent_fd
, ret
, dropped
;
7608 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
7609 VERIFY(zevent_fd
>= 0);
7611 if (!opts
->scripted
)
7612 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
7615 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
7616 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
7617 if (ret
|| nvl
== NULL
)
7621 (void) printf(gettext("dropped %d events\n"), dropped
);
7623 zpool_do_events_short(nvl
, opts
);
7625 if (opts
->verbose
) {
7626 zpool_do_events_nvprint(nvl
, 8);
7627 printf(gettext("\n"));
7629 (void) fflush(stdout
);
7634 VERIFY(0 == close(zevent_fd
));
7640 zpool_do_events_clear(ev_opts_t
*opts
)
7644 ret
= zpool_events_clear(g_zfs
, &count
);
7646 (void) printf(gettext("cleared %d events\n"), count
);
7652 * zpool events [-vfc]
7654 * Displays events logs by ZFS.
7657 zpool_do_events(int argc
, char **argv
)
7659 ev_opts_t opts
= { 0 };
7664 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
7679 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7688 ret
= zpool_do_events_clear(&opts
);
7690 ret
= zpool_do_events_next(&opts
);
7696 get_callback(zpool_handle_t
*zhp
, void *data
)
7698 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
7699 char value
[MAXNAMELEN
];
7700 zprop_source_t srctype
;
7703 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
7706 * Skip the special fake placeholder. This will also skip
7707 * over the name property when 'all' is specified.
7709 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
7710 pl
== cbp
->cb_proplist
)
7713 if (pl
->pl_prop
== ZPROP_INVAL
&&
7714 (zpool_prop_feature(pl
->pl_user_prop
) ||
7715 zpool_prop_unsupported(pl
->pl_user_prop
))) {
7716 srctype
= ZPROP_SRC_LOCAL
;
7718 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
7719 value
, sizeof (value
)) == 0) {
7720 zprop_print_one_property(zpool_get_name(zhp
),
7721 cbp
, pl
->pl_user_prop
, value
, srctype
,
7725 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
7726 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
7729 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
7730 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
7738 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
7740 * -H Scripted mode. Don't display headers, and separate properties
7742 * -o List of columns to display. Defaults to
7743 * "name,property,value,source".
7744 * -p Display values in parsable (exact) format.
7746 * Get properties of pools in the system. Output space statistics
7747 * for each one as well as other attributes.
7750 zpool_do_get(int argc
, char **argv
)
7752 zprop_get_cbdata_t cb
= { 0 };
7753 zprop_list_t fake_name
= { 0 };
7758 cb
.cb_first
= B_TRUE
;
7761 * Set up default columns and sources.
7763 cb
.cb_sources
= ZPROP_SRC_ALL
;
7764 cb
.cb_columns
[0] = GET_COL_NAME
;
7765 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7766 cb
.cb_columns
[2] = GET_COL_VALUE
;
7767 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7768 cb
.cb_type
= ZFS_TYPE_POOL
;
7771 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
7774 cb
.cb_literal
= B_TRUE
;
7777 cb
.cb_scripted
= B_TRUE
;
7780 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
7782 while (*optarg
!= '\0') {
7783 static char *col_subopts
[] =
7784 { "name", "property", "value", "source",
7787 if (i
== ZFS_GET_NCOLS
) {
7788 (void) fprintf(stderr
, gettext("too "
7789 "many fields given to -o "
7794 switch (getsubopt(&optarg
, col_subopts
,
7797 cb
.cb_columns
[i
++] = GET_COL_NAME
;
7800 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
7803 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
7806 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
7810 (void) fprintf(stderr
,
7811 gettext("\"all\" conflicts "
7812 "with specific fields "
7813 "given to -o option\n"));
7816 cb
.cb_columns
[0] = GET_COL_NAME
;
7817 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
7818 cb
.cb_columns
[2] = GET_COL_VALUE
;
7819 cb
.cb_columns
[3] = GET_COL_SOURCE
;
7823 (void) fprintf(stderr
,
7824 gettext("invalid column name "
7831 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7841 (void) fprintf(stderr
, gettext("missing property "
7846 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
7847 ZFS_TYPE_POOL
) != 0)
7853 if (cb
.cb_proplist
!= NULL
) {
7854 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
7855 fake_name
.pl_width
= strlen(gettext("NAME"));
7856 fake_name
.pl_next
= cb
.cb_proplist
;
7857 cb
.cb_proplist
= &fake_name
;
7860 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
,
7863 if (cb
.cb_proplist
== &fake_name
)
7864 zprop_free_list(fake_name
.pl_next
);
7866 zprop_free_list(cb
.cb_proplist
);
7871 typedef struct set_cbdata
{
7874 boolean_t cb_any_successful
;
7878 set_callback(zpool_handle_t
*zhp
, void *data
)
7881 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
7883 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
7886 cb
->cb_any_successful
= B_TRUE
;
7892 zpool_do_set(int argc
, char **argv
)
7894 set_cbdata_t cb
= { 0 };
7897 if (argc
> 1 && argv
[1][0] == '-') {
7898 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7904 (void) fprintf(stderr
, gettext("missing property=value "
7910 (void) fprintf(stderr
, gettext("missing pool name\n"));
7915 (void) fprintf(stderr
, gettext("too many pool names\n"));
7919 cb
.cb_propname
= argv
[1];
7920 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
7921 if (cb
.cb_value
== NULL
) {
7922 (void) fprintf(stderr
, gettext("missing value in "
7923 "property=value argument\n"));
7927 *(cb
.cb_value
) = '\0';
7930 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
,
7937 find_command_idx(char *command
, int *idx
)
7941 for (i
= 0; i
< NCOMMAND
; i
++) {
7942 if (command_table
[i
].name
== NULL
)
7945 if (strcmp(command
, command_table
[i
].name
) == 0) {
7954 main(int argc
, char **argv
)
7960 (void) setlocale(LC_ALL
, "");
7961 (void) textdomain(TEXT_DOMAIN
);
7967 * Make sure the user has specified some command.
7970 (void) fprintf(stderr
, gettext("missing command\n"));
7979 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
7982 if ((g_zfs
= libzfs_init()) == NULL
) {
7983 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
7987 libzfs_print_on_error(g_zfs
, B_TRUE
);
7989 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
7992 * Run the appropriate command.
7994 if (find_command_idx(cmdname
, &i
) == 0) {
7995 current_command
= &command_table
[i
];
7996 ret
= command_table
[i
].func(argc
- 1, argv
+ 1);
7997 } else if (strchr(cmdname
, '=')) {
7998 verify(find_command_idx("set", &i
) == 0);
7999 current_command
= &command_table
[i
];
8000 ret
= command_table
[i
].func(argc
, argv
);
8001 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
8003 * 'freeze' is a vile debugging abomination, so we treat
8007 int fd
= open(ZFS_DEV
, O_RDWR
);
8008 (void) strlcpy((void *)buf
, argv
[2], sizeof (buf
));
8009 return (!!ioctl(fd
, ZFS_IOC_POOL_FREEZE
, buf
));
8011 (void) fprintf(stderr
, gettext("unrecognized "
8012 "command '%s'\n"), cmdname
);
8017 if (ret
== 0 && log_history
)
8018 (void) zpool_log_history(g_zfs
, history_str
);
8023 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8024 * for the purposes of running ::findleaks.
8026 if (getenv("ZFS_ABORT") != NULL
) {
8027 (void) printf("dumping core by request\n");