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, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
57 #include <sys/fs/zfs.h>
59 #include <sys/systeminfo.h>
60 #include <sys/fm/fs/zfs.h>
61 #include <sys/fm/util.h>
62 #include <sys/fm/protocol.h>
63 #include <sys/zfs_ioctl.h>
64 #include <sys/mount.h>
65 #include <sys/sysmacros.h>
72 #include "zpool_util.h"
73 #include "zfs_comutil.h"
74 #include "zfeature_common.h"
76 #include "statcommon.h"
78 libzfs_handle_t
*g_zfs
;
80 static int zpool_do_create(int, char **);
81 static int zpool_do_destroy(int, char **);
83 static int zpool_do_add(int, char **);
84 static int zpool_do_remove(int, char **);
85 static int zpool_do_labelclear(int, char **);
87 static int zpool_do_checkpoint(int, char **);
89 static int zpool_do_list(int, char **);
90 static int zpool_do_iostat(int, char **);
91 static int zpool_do_status(int, char **);
93 static int zpool_do_online(int, char **);
94 static int zpool_do_offline(int, char **);
95 static int zpool_do_clear(int, char **);
96 static int zpool_do_reopen(int, char **);
98 static int zpool_do_reguid(int, char **);
100 static int zpool_do_attach(int, char **);
101 static int zpool_do_detach(int, char **);
102 static int zpool_do_replace(int, char **);
103 static int zpool_do_split(int, char **);
105 static int zpool_do_initialize(int, char **);
106 static int zpool_do_scrub(int, char **);
107 static int zpool_do_resilver(int, char **);
108 static int zpool_do_trim(int, char **);
110 static int zpool_do_import(int, char **);
111 static int zpool_do_export(int, char **);
113 static int zpool_do_upgrade(int, char **);
115 static int zpool_do_history(int, char **);
116 static int zpool_do_events(int, char **);
118 static int zpool_do_get(int, char **);
119 static int zpool_do_set(int, char **);
121 static int zpool_do_sync(int, char **);
123 static int zpool_do_version(int, char **);
125 static int zpool_do_wait(int, char **);
128 * These libumem hooks provide a reasonable set of defaults for the allocator's
129 * debugging facilities.
134 _umem_debug_init(void)
136 return ("default,verbose"); /* $UMEM_DEBUG setting */
140 _umem_logging_init(void)
142 return ("fail,contents"); /* $UMEM_LOGGING setting */
183 * Flags for stats to display with "zpool iostats"
191 IOS_COUNT
, /* always last element */
194 /* iostat_type entries as bitmasks */
195 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
196 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
197 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
198 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
199 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
201 /* Mask of all the histo bits */
202 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
205 * Lookup table for iostat flags to nvlist names. Basically a list
206 * of all the nvlists a flag requires. Also specifies the order in
207 * which data gets printed in zpool iostat.
209 static const char *vsx_type_to_nvlist
[IOS_COUNT
][13] = {
211 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
212 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
213 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
214 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
215 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
216 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
217 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
218 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
225 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
226 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
227 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
230 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
231 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
232 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
233 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
234 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
235 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
238 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
239 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
240 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
241 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
242 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
243 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
244 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
245 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
246 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
247 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
248 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
249 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
255 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
256 * Right now, only one histo bit is ever set at one time, so we can
257 * just do a highbit64(a)
259 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
261 typedef struct zpool_command
{
263 int (*func
)(int, char **);
268 * Master command table. Each ZFS command has a name, associated function, and
269 * usage message. The usage messages need to be internationalized, so we have
270 * to have a function to return the usage message based on a command index.
272 * These commands are organized according to how they are displayed in the usage
273 * message. An empty command (one with a NULL name) indicates an empty line in
274 * the generic usage message.
276 static zpool_command_t command_table
[] = {
277 { "version", zpool_do_version
, HELP_VERSION
},
279 { "create", zpool_do_create
, HELP_CREATE
},
280 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
282 { "add", zpool_do_add
, HELP_ADD
},
283 { "remove", zpool_do_remove
, HELP_REMOVE
},
285 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
287 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
289 { "list", zpool_do_list
, HELP_LIST
},
290 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
291 { "status", zpool_do_status
, HELP_STATUS
},
293 { "online", zpool_do_online
, HELP_ONLINE
},
294 { "offline", zpool_do_offline
, HELP_OFFLINE
},
295 { "clear", zpool_do_clear
, HELP_CLEAR
},
296 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
298 { "attach", zpool_do_attach
, HELP_ATTACH
},
299 { "detach", zpool_do_detach
, HELP_DETACH
},
300 { "replace", zpool_do_replace
, HELP_REPLACE
},
301 { "split", zpool_do_split
, HELP_SPLIT
},
303 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
304 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
305 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
306 { "trim", zpool_do_trim
, HELP_TRIM
},
308 { "import", zpool_do_import
, HELP_IMPORT
},
309 { "export", zpool_do_export
, HELP_EXPORT
},
310 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
311 { "reguid", zpool_do_reguid
, HELP_REGUID
},
313 { "history", zpool_do_history
, HELP_HISTORY
},
314 { "events", zpool_do_events
, HELP_EVENTS
},
316 { "get", zpool_do_get
, HELP_GET
},
317 { "set", zpool_do_set
, HELP_SET
},
318 { "sync", zpool_do_sync
, HELP_SYNC
},
320 { "wait", zpool_do_wait
, HELP_WAIT
},
323 #define NCOMMAND (ARRAY_SIZE(command_table))
325 #define VDEV_ALLOC_CLASS_LOGS "logs"
327 static zpool_command_t
*current_command
;
328 static char history_str
[HIS_MAX_RECORD_LEN
];
329 static boolean_t log_history
= B_TRUE
;
330 static uint_t timestamp_fmt
= NODATE
;
333 get_usage(zpool_help_t idx
)
337 return (gettext("\tadd [-fgLnP] [-o property=value] "
338 "<pool> <vdev> ...\n"));
340 return (gettext("\tattach [-fsw] [-o property=value] "
341 "<pool> <device> <new-device>\n"));
343 return (gettext("\tclear [-nF] <pool> [device]\n"));
345 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
346 "\t [-O file-system-property=value] ... \n"
347 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
348 case HELP_CHECKPOINT
:
349 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
351 return (gettext("\tdestroy [-f] <pool>\n"));
353 return (gettext("\tdetach <pool> <device>\n"));
355 return (gettext("\texport [-af] <pool> ...\n"));
357 return (gettext("\thistory [-il] [<pool>] ...\n"));
359 return (gettext("\timport [-d dir] [-D]\n"
360 "\timport [-o mntopts] [-o property=value] ... \n"
361 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
362 "[-R root] [-F [-n]] -a\n"
363 "\timport [-o mntopts] [-o property=value] ... \n"
364 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
365 "[-R root] [-F [-n]]\n"
366 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
368 return (gettext("\tiostat [[[-c [script1,script2,...]"
369 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
370 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
371 " [[-n] interval [count]]\n"));
372 case HELP_LABELCLEAR
:
373 return (gettext("\tlabelclear [-f] <vdev>\n"));
375 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
376 "[-T d|u] [pool] ... \n"
377 "\t [interval [count]]\n"));
379 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
381 return (gettext("\tonline [-e] <pool> <device> ...\n"));
383 return (gettext("\treplace [-fsw] [-o property=value] "
384 "<pool> <device> [new-device]\n"));
386 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
388 return (gettext("\treopen [-n] <pool>\n"));
389 case HELP_INITIALIZE
:
390 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
391 "[<device> ...]\n"));
393 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
395 return (gettext("\tresilver <pool> ...\n"));
397 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
398 "[<device> ...]\n"));
400 return (gettext("\tstatus [-c [script1,script2,...]] "
401 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
402 "\t [interval [count]]\n"));
404 return (gettext("\tupgrade\n"
406 "\tupgrade [-V version] <-a | pool ...>\n"));
408 return (gettext("\tevents [-vHf [pool] | -c]\n"));
410 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
411 "<\"all\" | property[,...]> <pool> ...\n"));
413 return (gettext("\tset <property=value> <pool> \n"));
415 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
416 "\t [-o property=value] <pool> <newpool> "
417 "[<device> ...]\n"));
419 return (gettext("\treguid <pool>\n"));
421 return (gettext("\tsync [pool] ...\n"));
423 return (gettext("\tversion\n"));
425 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
426 "<pool> [interval]\n"));
434 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
440 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
444 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
447 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
448 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
449 fnvlist_add_boolean(res
, path
);
455 for (i
= 0; i
< children
; i
++) {
456 zpool_collect_leaves(zhp
, child
[i
], res
);
461 * Callback routine that will print out a pool property value.
464 print_prop_cb(int prop
, void *cb
)
468 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
470 if (zpool_prop_readonly(prop
))
471 (void) fprintf(fp
, " NO ");
473 (void) fprintf(fp
, " YES ");
475 if (zpool_prop_values(prop
) == NULL
)
476 (void) fprintf(fp
, "-\n");
478 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
484 * Display usage message. If we're inside a command, display only the usage for
485 * that command. Otherwise, iterate over the entire command table and display
486 * a complete usage message.
489 usage(boolean_t requested
)
491 FILE *fp
= requested
? stdout
: stderr
;
493 if (current_command
== NULL
) {
496 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
498 gettext("where 'command' is one of the following:\n\n"));
500 for (i
= 0; i
< NCOMMAND
; i
++) {
501 if (command_table
[i
].name
== NULL
)
502 (void) fprintf(fp
, "\n");
504 (void) fprintf(fp
, "%s",
505 get_usage(command_table
[i
].usage
));
508 (void) fprintf(fp
, gettext("usage:\n"));
509 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
512 if (current_command
!= NULL
&&
513 ((strcmp(current_command
->name
, "set") == 0) ||
514 (strcmp(current_command
->name
, "get") == 0) ||
515 (strcmp(current_command
->name
, "list") == 0))) {
518 gettext("\nthe following properties are supported:\n"));
520 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
521 "PROPERTY", "EDIT", "VALUES");
523 /* Iterate over all properties */
524 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
527 (void) fprintf(fp
, "\t%-19s ", "feature@...");
528 (void) fprintf(fp
, "YES disabled | enabled | active\n");
530 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
531 "appended with a feature name.\nSee zpool-features(5).\n"));
535 * See comments at end of main().
537 if (getenv("ZFS_ABORT") != NULL
) {
538 (void) printf("dumping core by request\n");
542 exit(requested
? 0 : 2);
546 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
547 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
550 * -c Cancel. Ends active initializing.
551 * -s Suspend. Initializing can then be restarted with no flags.
552 * -w Wait. Blocks until initializing has completed.
555 zpool_do_initialize(int argc
, char **argv
)
562 boolean_t wait
= B_FALSE
;
564 struct option long_options
[] = {
565 {"cancel", no_argument
, NULL
, 'c'},
566 {"suspend", no_argument
, NULL
, 's'},
567 {"wait", no_argument
, NULL
, 'w'},
571 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
572 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
575 if (cmd_type
!= POOL_INITIALIZE_START
&&
576 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
577 (void) fprintf(stderr
, gettext("-c cannot be "
578 "combined with other options\n"));
581 cmd_type
= POOL_INITIALIZE_CANCEL
;
584 if (cmd_type
!= POOL_INITIALIZE_START
&&
585 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
586 (void) fprintf(stderr
, gettext("-s cannot be "
587 "combined with other options\n"));
590 cmd_type
= POOL_INITIALIZE_SUSPEND
;
597 (void) fprintf(stderr
,
598 gettext("invalid option '%c'\n"), optopt
);
600 (void) fprintf(stderr
,
601 gettext("invalid option '%s'\n"),
612 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
617 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
618 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
624 zhp
= zpool_open(g_zfs
, poolname
);
628 vdevs
= fnvlist_alloc();
630 /* no individual leaf vdevs specified, so add them all */
631 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
632 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
633 ZPOOL_CONFIG_VDEV_TREE
);
634 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
636 for (int i
= 1; i
< argc
; i
++) {
637 fnvlist_add_boolean(vdevs
, argv
[i
]);
642 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
644 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
653 * print a pool vdev config for dry runs
656 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
657 const char *match
, int name_flags
)
662 boolean_t printed
= B_FALSE
;
664 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
665 &child
, &children
) != 0) {
667 (void) printf("\t%*s%s\n", indent
, "", name
);
671 for (c
= 0; c
< children
; c
++) {
672 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
675 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
678 if (is_hole
== B_TRUE
) {
682 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
685 class = VDEV_ALLOC_BIAS_LOG
;
686 (void) nvlist_lookup_string(child
[c
],
687 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
688 if (strcmp(match
, class) != 0)
691 if (!printed
&& name
!= NULL
) {
692 (void) printf("\t%*s%s\n", indent
, "", name
);
695 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
696 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
703 * Print the list of l2cache devices for dry runs.
706 print_cache_list(nvlist_t
*nv
, int indent
)
711 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
712 &child
, &children
) == 0 && children
> 0) {
713 (void) printf("\t%*s%s\n", indent
, "", "cache");
717 for (c
= 0; c
< children
; c
++) {
720 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
721 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
727 * Print the list of spares for dry runs.
730 print_spare_list(nvlist_t
*nv
, int indent
)
735 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
736 &child
, &children
) == 0 && children
> 0) {
737 (void) printf("\t%*s%s\n", indent
, "", "spares");
741 for (c
= 0; c
< children
; c
++) {
744 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
745 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
751 prop_list_contains_feature(nvlist_t
*proplist
)
754 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
755 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
756 if (zpool_prop_feature(nvpair_name(nvp
)))
763 * Add a property pair (name, string-value) into a property nvlist.
766 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
769 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
774 if (*props
== NULL
&&
775 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
776 (void) fprintf(stderr
,
777 gettext("internal error: out of memory\n"));
784 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
786 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
787 !zpool_prop_feature(propname
)) {
788 (void) fprintf(stderr
, gettext("property '%s' is "
789 "not a valid pool property\n"), propname
);
794 * feature@ properties and version should not be specified
797 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
798 nvlist_exists(proplist
, vname
)) ||
799 (prop
== ZPOOL_PROP_VERSION
&&
800 prop_list_contains_feature(proplist
))) {
801 (void) fprintf(stderr
, gettext("'feature@' and "
802 "'version' properties cannot be specified "
808 if (zpool_prop_feature(propname
))
811 normnm
= zpool_prop_to_name(prop
);
813 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
815 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
817 normnm
= zfs_prop_to_name(fsprop
);
818 } else if (zfs_prop_user(propname
) ||
819 zfs_prop_userquota(propname
)) {
822 (void) fprintf(stderr
, gettext("property '%s' is "
823 "not a valid filesystem property\n"), propname
);
828 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
829 prop
!= ZPOOL_PROP_CACHEFILE
) {
830 (void) fprintf(stderr
, gettext("property '%s' "
831 "specified multiple times\n"), propname
);
835 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
836 (void) fprintf(stderr
, gettext("internal "
837 "error: out of memory\n"));
845 * Set a default property pair (name, string-value) in a property nvlist
848 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
853 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
856 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
860 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
862 * -f Force addition of devices, even if they appear in use
863 * -g Display guid for individual vdev name.
864 * -L Follow links when resolving vdev path name.
865 * -n Do not add the devices, but display the resulting layout if
866 * they were to be added.
867 * -o Set property=value.
868 * -P Display full path for vdev name.
870 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
871 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
875 zpool_do_add(int argc
, char **argv
)
877 boolean_t force
= B_FALSE
;
878 boolean_t dryrun
= B_FALSE
;
886 nvlist_t
*props
= NULL
;
890 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
896 name_flags
|= VDEV_NAME_GUID
;
899 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
905 if ((propval
= strchr(optarg
, '=')) == NULL
) {
906 (void) fprintf(stderr
, gettext("missing "
907 "'=' for -o option\n"));
913 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
914 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
918 name_flags
|= VDEV_NAME_PATH
;
921 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
930 /* get pool name and check number of arguments */
932 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
936 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
945 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
948 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
949 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
955 /* unless manually specified use "ashift" pool property (if set) */
956 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
959 char strval
[ZPOOL_MAXPROPLEN
];
961 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
962 if (src
!= ZPROP_SRC_DEFAULT
) {
963 (void) sprintf(strval
, "%" PRId32
, intval
);
964 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
965 &props
, B_TRUE
) == 0);
969 /* pass off to make_root_vdev for processing */
970 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
972 if (nvroot
== NULL
) {
978 nvlist_t
*poolnvroot
;
979 nvlist_t
**l2child
, **sparechild
;
980 uint_t l2children
, sparechildren
, c
;
982 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
984 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
987 (void) printf(gettext("would update '%s' to the following "
988 "configuration:\n\n"), zpool_get_name(zhp
));
990 /* print original main pool and new tree */
991 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
992 name_flags
| VDEV_NAME_TYPE_ID
);
993 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
995 /* print other classes: 'dedup', 'special', and 'log' */
996 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
997 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
998 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
999 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1000 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1001 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1002 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1003 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1006 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1007 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1008 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1009 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1010 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1011 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1012 print_vdev_tree(zhp
, "special", nvroot
, 0,
1013 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1016 if (num_logs(poolnvroot
) > 0) {
1017 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1018 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1019 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1020 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1021 } else if (num_logs(nvroot
) > 0) {
1022 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1023 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1026 /* Do the same for the caches */
1027 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1028 &l2child
, &l2children
) == 0 && l2children
) {
1030 (void) printf(gettext("\tcache\n"));
1031 for (c
= 0; c
< l2children
; c
++) {
1032 vname
= zpool_vdev_name(g_zfs
, NULL
,
1033 l2child
[c
], name_flags
);
1034 (void) printf("\t %s\n", vname
);
1038 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1039 &l2child
, &l2children
) == 0 && l2children
) {
1041 (void) printf(gettext("\tcache\n"));
1042 for (c
= 0; c
< l2children
; c
++) {
1043 vname
= zpool_vdev_name(g_zfs
, NULL
,
1044 l2child
[c
], name_flags
);
1045 (void) printf("\t %s\n", vname
);
1049 /* And finaly the spares */
1050 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1051 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1053 (void) printf(gettext("\tspares\n"));
1054 for (c
= 0; c
< sparechildren
; c
++) {
1055 vname
= zpool_vdev_name(g_zfs
, NULL
,
1056 sparechild
[c
], name_flags
);
1057 (void) printf("\t %s\n", vname
);
1061 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1062 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1064 (void) printf(gettext("\tspares\n"));
1065 for (c
= 0; c
< sparechildren
; c
++) {
1066 vname
= zpool_vdev_name(g_zfs
, NULL
,
1067 sparechild
[c
], name_flags
);
1068 (void) printf("\t %s\n", vname
);
1075 ret
= (zpool_add(zhp
, nvroot
) != 0);
1079 nvlist_free(nvroot
);
1086 * zpool remove [-npsw] <pool> <vdev> ...
1088 * Removes the given vdev from the pool.
1091 zpool_do_remove(int argc
, char **argv
)
1095 zpool_handle_t
*zhp
= NULL
;
1096 boolean_t stop
= B_FALSE
;
1098 boolean_t noop
= B_FALSE
;
1099 boolean_t parsable
= B_FALSE
;
1100 boolean_t wait
= B_FALSE
;
1103 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1118 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1127 /* get pool name and check number of arguments */
1129 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1135 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1139 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1145 (void) fprintf(stderr
, gettext("too many arguments\n"));
1148 if (zpool_vdev_remove_cancel(zhp
) != 0)
1151 (void) fprintf(stderr
, gettext("invalid option "
1152 "combination: -w cannot be used with -s\n"));
1157 (void) fprintf(stderr
, gettext("missing device\n"));
1161 for (i
= 1; i
< argc
; i
++) {
1165 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1171 (void) printf("%s %llu\n",
1172 argv
[i
], (unsigned long long)size
);
1175 zfs_nicenum(size
, valstr
,
1177 (void) printf("Memory that will be "
1178 "used after removing %s: %s\n",
1182 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1187 if (ret
== 0 && wait
)
1188 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1196 * zpool labelclear [-f] <vdev>
1198 * -f Force clearing the label for the vdevs which are members of
1199 * the exported or foreign pools.
1201 * Verifies that the vdev is not active and zeros out the label information
1205 zpool_do_labelclear(int argc
, char **argv
)
1207 char vdev
[MAXPATHLEN
];
1210 int c
, fd
= -1, ret
= 0;
1213 boolean_t inuse
= B_FALSE
;
1214 boolean_t force
= B_FALSE
;
1217 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1223 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1234 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1238 (void) fprintf(stderr
, gettext("too many arguments\n"));
1243 * Check if we were given absolute path and use it as is.
1244 * Otherwise if the provided vdev name doesn't point to a file,
1245 * try prepending expected disk paths and partition numbers.
1247 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1248 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1251 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1252 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1253 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1257 if (error
|| (stat(vdev
, &st
) != 0)) {
1258 (void) fprintf(stderr
, gettext(
1259 "failed to find device %s, try specifying absolute "
1260 "path instead\n"), argv
[0]);
1265 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1266 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1267 vdev
, strerror(errno
));
1272 * Flush all dirty pages for the block device. This should not be
1273 * fatal when the device does not support BLKFLSBUF as would be the
1274 * case for a file vdev.
1276 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1277 (void) fprintf(stderr
, gettext("failed to invalidate "
1278 "cache for %s: %s\n"), vdev
, strerror(errno
));
1280 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1281 (void) fprintf(stderr
,
1282 gettext("failed to read label from %s\n"), vdev
);
1286 nvlist_free(config
);
1288 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1290 (void) fprintf(stderr
,
1291 gettext("failed to check state for %s\n"), vdev
);
1301 case POOL_STATE_ACTIVE
:
1302 case POOL_STATE_SPARE
:
1303 case POOL_STATE_L2CACHE
:
1304 (void) fprintf(stderr
, gettext(
1305 "%s is a member (%s) of pool \"%s\"\n"),
1306 vdev
, zpool_pool_state_to_name(state
), name
);
1310 case POOL_STATE_EXPORTED
:
1313 (void) fprintf(stderr
, gettext(
1314 "use '-f' to override the following error:\n"
1315 "%s is a member of exported pool \"%s\"\n"),
1320 case POOL_STATE_POTENTIALLY_ACTIVE
:
1323 (void) fprintf(stderr
, gettext(
1324 "use '-f' to override the following error:\n"
1325 "%s is a member of potentially active pool \"%s\"\n"),
1330 case POOL_STATE_DESTROYED
:
1331 /* inuse should never be set for a destroyed pool */
1337 ret
= zpool_clear_label(fd
);
1339 (void) fprintf(stderr
,
1340 gettext("failed to clear label for %s\n"), vdev
);
1351 * zpool create [-fnd] [-o property=value] ...
1352 * [-O file-system-property=value] ...
1353 * [-R root] [-m mountpoint] <pool> <dev> ...
1355 * -f Force creation, even if devices appear in use
1356 * -n Do not create the pool, but display the resulting layout if it
1357 * were to be created.
1358 * -R Create a pool under an alternate root
1359 * -m Set default mountpoint for the root dataset. By default it's
1361 * -o Set property=value.
1362 * -o Set feature@feature=enabled|disabled.
1363 * -d Don't automatically enable all supported pool features
1364 * (individual features can be enabled with -o).
1365 * -O Set fsproperty=value in the pool's root file system
1367 * Creates the named pool according to the given vdev specification. The
1368 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1369 * Once we get the nvlist back from make_root_vdev(), we either print out the
1370 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1373 zpool_do_create(int argc
, char **argv
)
1375 boolean_t force
= B_FALSE
;
1376 boolean_t dryrun
= B_FALSE
;
1377 boolean_t enable_all_pool_feat
= B_TRUE
;
1379 nvlist_t
*nvroot
= NULL
;
1383 char *altroot
= NULL
;
1384 char *mountpoint
= NULL
;
1385 nvlist_t
*fsprops
= NULL
;
1386 nvlist_t
*props
= NULL
;
1390 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1399 enable_all_pool_feat
= B_FALSE
;
1403 if (add_prop_list(zpool_prop_to_name(
1404 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1406 if (add_prop_list_default(zpool_prop_to_name(
1407 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1411 /* Equivalent to -O mountpoint=optarg */
1412 mountpoint
= optarg
;
1415 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1416 (void) fprintf(stderr
, gettext("missing "
1417 "'=' for -o option\n"));
1423 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1427 * If the user is creating a pool that doesn't support
1428 * feature flags, don't enable any features.
1430 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1434 ver
= strtoull(propval
, &end
, 10);
1436 ver
< SPA_VERSION_FEATURES
) {
1437 enable_all_pool_feat
= B_FALSE
;
1440 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1444 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1445 (void) fprintf(stderr
, gettext("missing "
1446 "'=' for -O option\n"));
1453 * Mountpoints are checked and then added later.
1454 * Uniquely among properties, they can be specified
1455 * more than once, to avoid conflict with -m.
1457 if (0 == strcmp(optarg
,
1458 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1459 mountpoint
= propval
;
1460 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1467 * Sanity check temporary pool name.
1469 if (strchr(optarg
, '/') != NULL
) {
1470 (void) fprintf(stderr
, gettext("cannot create "
1471 "'%s': invalid character '/' in temporary "
1473 (void) fprintf(stderr
, gettext("use 'zfs "
1474 "create' to create a dataset\n"));
1478 if (add_prop_list(zpool_prop_to_name(
1479 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1481 if (add_prop_list_default(zpool_prop_to_name(
1482 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1487 (void) fprintf(stderr
, gettext("missing argument for "
1488 "'%c' option\n"), optopt
);
1491 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1500 /* get pool name and check number of arguments */
1502 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1506 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1513 * As a special case, check for use of '/' in the name, and direct the
1514 * user to use 'zfs create' instead.
1516 if (strchr(poolname
, '/') != NULL
) {
1517 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1518 "character '/' in pool name\n"), poolname
);
1519 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1520 "create a dataset\n"));
1524 /* pass off to make_root_vdev for bulk processing */
1525 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1526 argc
- 1, argv
+ 1);
1530 /* make_root_vdev() allows 0 toplevel children if there are spares */
1531 if (!zfs_allocatable_devs(nvroot
)) {
1532 (void) fprintf(stderr
, gettext("invalid vdev "
1533 "specification: at least one toplevel vdev must be "
1538 if (altroot
!= NULL
&& altroot
[0] != '/') {
1539 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1540 "must be an absolute path\n"), altroot
);
1545 * Check the validity of the mountpoint and direct the user to use the
1546 * '-m' mountpoint option if it looks like its in use.
1548 if (mountpoint
== NULL
||
1549 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1550 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1551 char buf
[MAXPATHLEN
];
1554 if (mountpoint
&& mountpoint
[0] != '/') {
1555 (void) fprintf(stderr
, gettext("invalid mountpoint "
1556 "'%s': must be an absolute path, 'legacy', or "
1557 "'none'\n"), mountpoint
);
1561 if (mountpoint
== NULL
) {
1562 if (altroot
!= NULL
)
1563 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1566 (void) snprintf(buf
, sizeof (buf
), "/%s",
1569 if (altroot
!= NULL
)
1570 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1571 altroot
, mountpoint
);
1573 (void) snprintf(buf
, sizeof (buf
), "%s",
1577 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1578 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1579 "%s\n"), buf
, strerror(errno
));
1580 (void) fprintf(stderr
, gettext("use '-m' "
1581 "option to provide a different default\n"));
1586 while (count
< 3 && readdir(dirp
) != NULL
)
1588 (void) closedir(dirp
);
1591 (void) fprintf(stderr
, gettext("mountpoint "
1592 "'%s' exists and is not empty\n"), buf
);
1593 (void) fprintf(stderr
, gettext("use '-m' "
1594 "option to provide a "
1595 "different default\n"));
1602 * Now that the mountpoint's validity has been checked, ensure that
1603 * the property is set appropriately prior to creating the pool.
1605 if (mountpoint
!= NULL
) {
1606 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1607 mountpoint
, &fsprops
, B_FALSE
);
1615 * For a dry run invocation, print out a basic message and run
1616 * through all the vdevs in the list and print out in an
1617 * appropriate hierarchy.
1619 (void) printf(gettext("would create '%s' with the "
1620 "following layout:\n\n"), poolname
);
1622 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1623 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1624 VDEV_ALLOC_BIAS_DEDUP
, 0);
1625 print_vdev_tree(NULL
, "special", nvroot
, 0,
1626 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1627 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1628 VDEV_ALLOC_BIAS_LOG
, 0);
1629 print_cache_list(nvroot
, 0);
1630 print_spare_list(nvroot
, 0);
1635 * Hand off to libzfs.
1638 for (i
= 0; i
< SPA_FEATURES
; i
++) {
1639 char propname
[MAXPATHLEN
];
1641 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1643 (void) snprintf(propname
, sizeof (propname
),
1644 "feature@%s", feat
->fi_uname
);
1647 * Only features contained in props will be enabled:
1648 * remove from the nvlist every ZFS_FEATURE_DISABLED
1649 * value and add every missing ZFS_FEATURE_ENABLED if
1650 * enable_all_pool_feat is set.
1652 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1653 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1654 (void) nvlist_remove_all(props
,
1656 } else if (enable_all_pool_feat
&&
1657 feat
->fi_zfs_mod_supported
) {
1658 ret
= add_prop_list(propname
,
1659 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1666 if (zpool_create(g_zfs
, poolname
,
1667 nvroot
, props
, fsprops
) == 0) {
1668 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1669 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1671 if (zfs_mount(pool
, NULL
, 0) == 0) {
1672 ret
= zfs_shareall(pool
);
1673 zfs_commit_all_shares();
1677 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1678 (void) fprintf(stderr
, gettext("pool name may have "
1684 nvlist_free(nvroot
);
1685 nvlist_free(fsprops
);
1689 nvlist_free(fsprops
);
1696 * zpool destroy <pool>
1698 * -f Forcefully unmount any datasets
1700 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1703 zpool_do_destroy(int argc
, char **argv
)
1705 boolean_t force
= B_FALSE
;
1708 zpool_handle_t
*zhp
;
1712 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1718 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1727 /* check arguments */
1729 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1733 (void) fprintf(stderr
, gettext("too many arguments\n"));
1739 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1741 * As a special case, check for use of '/' in the name, and
1742 * direct the user to use 'zfs destroy' instead.
1744 if (strchr(pool
, '/') != NULL
)
1745 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1746 "destroy a dataset\n"));
1750 if (zpool_disable_datasets(zhp
, force
) != 0) {
1751 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1752 "could not unmount datasets\n"), zpool_get_name(zhp
));
1757 /* The history must be logged as part of the export */
1758 log_history
= B_FALSE
;
1760 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1767 typedef struct export_cbdata
{
1769 boolean_t hardforce
;
1776 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1778 export_cbdata_t
*cb
= data
;
1780 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1783 /* The history must be logged as part of the export */
1784 log_history
= B_FALSE
;
1786 if (cb
->hardforce
) {
1787 if (zpool_export_force(zhp
, history_str
) != 0)
1789 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1797 * zpool export [-f] <pool> ...
1799 * -a Export all pools
1800 * -f Forcefully unmount datasets
1802 * Export the given pools. By default, the command will attempt to cleanly
1803 * unmount any active datasets within the pool. If the '-f' flag is specified,
1804 * then the datasets will be forcefully unmounted.
1807 zpool_do_export(int argc
, char **argv
)
1810 boolean_t do_all
= B_FALSE
;
1811 boolean_t force
= B_FALSE
;
1812 boolean_t hardforce
= B_FALSE
;
1816 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1828 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1835 cb
.hardforce
= hardforce
;
1841 (void) fprintf(stderr
, gettext("too many arguments\n"));
1845 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1846 B_FALSE
, zpool_export_one
, &cb
));
1849 /* check arguments */
1851 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1855 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
, zpool_export_one
,
1862 * Given a vdev configuration, determine the maximum width needed for the device
1866 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1874 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1875 if (strlen(name
) + depth
> max
)
1876 max
= strlen(name
) + depth
;
1880 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1881 &child
, &children
) == 0) {
1882 for (c
= 0; c
< children
; c
++)
1883 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1884 max
, name_flags
)) > max
)
1888 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1889 &child
, &children
) == 0) {
1890 for (c
= 0; c
< children
; c
++)
1891 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1892 max
, name_flags
)) > max
)
1896 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1897 &child
, &children
) == 0) {
1898 for (c
= 0; c
< children
; c
++)
1899 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1900 max
, name_flags
)) > max
)
1907 typedef struct spare_cbdata
{
1909 zpool_handle_t
*cb_zhp
;
1913 find_vdev(nvlist_t
*nv
, uint64_t search
)
1919 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1923 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1924 &child
, &children
) == 0) {
1925 for (c
= 0; c
< children
; c
++)
1926 if (find_vdev(child
[c
], search
))
1934 find_spare(zpool_handle_t
*zhp
, void *data
)
1936 spare_cbdata_t
*cbp
= data
;
1937 nvlist_t
*config
, *nvroot
;
1939 config
= zpool_get_config(zhp
, NULL
);
1940 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1943 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1952 typedef struct status_cbdata
{
1956 boolean_t cb_allpools
;
1957 boolean_t cb_verbose
;
1958 boolean_t cb_literal
;
1959 boolean_t cb_explain
;
1961 boolean_t cb_dedup_stats
;
1962 boolean_t cb_print_status
;
1963 boolean_t cb_print_slow_ios
;
1964 boolean_t cb_print_vdev_init
;
1965 boolean_t cb_print_vdev_trim
;
1966 vdev_cmd_data_list_t
*vcdl
;
1969 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
1971 is_blank_str(char *str
)
1973 while (str
!= NULL
&& *str
!= '\0') {
1981 /* Print command output lines for specific vdev in a specific pool */
1983 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
1985 vdev_cmd_data_t
*data
;
1989 for (i
= 0; i
< vcdl
->count
; i
++) {
1990 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
1991 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
1992 /* Not the vdev we're looking for */
1996 data
= &vcdl
->data
[i
];
1997 /* Print out all the output values for this vdev */
1998 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2000 /* Does this vdev have values for this column? */
2001 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2002 if (strcmp(data
->cols
[k
],
2003 vcdl
->uniq_cols
[j
]) == 0) {
2004 /* yes it does, record the value */
2005 val
= data
->lines
[k
];
2010 * Mark empty values with dashes to make output
2013 if (val
== NULL
|| is_blank_str(val
))
2016 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2017 if (j
< vcdl
->uniq_cols_cnt
- 1)
2021 /* Print out any values that aren't in a column at the end */
2022 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2023 /* Did we have any columns? If so print a spacer. */
2024 if (vcdl
->uniq_cols_cnt
> 0)
2027 val
= data
->lines
[j
];
2028 printf("%s", val
? val
: "");
2035 * Print vdev initialization status for leaves
2038 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2041 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2042 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2043 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2044 !vs
->vs_scan_removing
) {
2047 struct tm zaction_ts
;
2049 time_t t
= vs
->vs_initialize_action_time
;
2050 int initialize_pct
= 100;
2051 if (vs
->vs_initialize_state
!=
2052 VDEV_INITIALIZE_COMPLETE
) {
2053 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2054 100 / (vs
->vs_initialize_bytes_est
+ 1));
2057 (void) localtime_r(&t
, &zaction_ts
);
2058 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2060 switch (vs
->vs_initialize_state
) {
2061 case VDEV_INITIALIZE_SUSPENDED
:
2062 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2063 gettext("suspended, started at"), tbuf
);
2065 case VDEV_INITIALIZE_ACTIVE
:
2066 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2067 gettext("started at"), tbuf
);
2069 case VDEV_INITIALIZE_COMPLETE
:
2070 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2071 gettext("completed at"), tbuf
);
2075 (void) printf(gettext(" (%d%% initialized%s)"),
2076 initialize_pct
, zbuf
);
2078 (void) printf(gettext(" (uninitialized)"));
2080 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2081 (void) printf(gettext(" (initializing)"));
2086 * Print vdev TRIM status for leaves
2089 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2092 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2093 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2094 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2095 !vs
->vs_scan_removing
) {
2098 struct tm zaction_ts
;
2100 time_t t
= vs
->vs_trim_action_time
;
2102 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2103 trim_pct
= (vs
->vs_trim_bytes_done
*
2104 100 / (vs
->vs_trim_bytes_est
+ 1));
2107 (void) localtime_r(&t
, &zaction_ts
);
2108 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2110 switch (vs
->vs_trim_state
) {
2111 case VDEV_TRIM_SUSPENDED
:
2112 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2113 gettext("suspended, started at"), tbuf
);
2115 case VDEV_TRIM_ACTIVE
:
2116 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2117 gettext("started at"), tbuf
);
2119 case VDEV_TRIM_COMPLETE
:
2120 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2121 gettext("completed at"), tbuf
);
2125 (void) printf(gettext(" (%d%% trimmed%s)"),
2127 } else if (vs
->vs_trim_notsup
) {
2128 (void) printf(gettext(" (trim unsupported)"));
2130 (void) printf(gettext(" (untrimmed)"));
2132 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2133 (void) printf(gettext(" (trimming)"));
2138 * Return the color associated with a health string. This includes returning
2139 * NULL for no color change.
2142 health_str_to_color(const char *health
)
2144 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2145 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2146 strcmp(health
, gettext("UNAVAIL")) == 0) {
2150 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2151 strcmp(health
, gettext("DEGRADED")) == 0 ||
2152 strcmp(health
, gettext("REMOVED")) == 0) {
2153 return (ANSI_YELLOW
);
2160 * Print out configuration state as requested by status_callback.
2163 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2164 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2166 nvlist_t
**child
, *root
;
2167 uint_t c
, i
, vsc
, children
;
2168 pool_scan_stat_t
*ps
= NULL
;
2170 char rbuf
[6], wbuf
[6], cbuf
[6];
2172 uint64_t notpresent
;
2173 spare_cbdata_t spare_cb
;
2177 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2179 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2180 &child
, &children
) != 0)
2183 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2184 (uint64_t **)&vs
, &vsc
) == 0);
2186 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2188 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2191 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2195 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2198 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2199 state
= gettext("INUSE");
2200 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2201 state
= gettext("AVAIL");
2204 printf_color(health_str_to_color(state
),
2205 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2209 if (vs
->vs_read_errors
)
2212 if (vs
->vs_write_errors
)
2215 if (vs
->vs_checksum_errors
)
2218 if (cb
->cb_literal
) {
2220 printf_color(rcolor
, "%5llu",
2221 (u_longlong_t
)vs
->vs_read_errors
);
2223 printf_color(wcolor
, "%5llu",
2224 (u_longlong_t
)vs
->vs_write_errors
);
2226 printf_color(ccolor
, "%5llu",
2227 (u_longlong_t
)vs
->vs_checksum_errors
);
2229 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2230 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2231 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2234 printf_color(rcolor
, "%5s", rbuf
);
2236 printf_color(wcolor
, "%5s", wbuf
);
2238 printf_color(ccolor
, "%5s", cbuf
);
2240 if (cb
->cb_print_slow_ios
) {
2241 if (children
== 0) {
2242 /* Only leafs vdevs have slow IOs */
2243 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2246 snprintf(rbuf
, sizeof (rbuf
), "-");
2250 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2252 printf(" %5s", rbuf
);
2256 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2257 ¬present
) == 0) {
2258 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2259 (void) printf(" %s %s", gettext("was"), path
);
2260 } else if (vs
->vs_aux
!= 0) {
2262 color_start(ANSI_RED
);
2263 switch (vs
->vs_aux
) {
2264 case VDEV_AUX_OPEN_FAILED
:
2265 (void) printf(gettext("cannot open"));
2268 case VDEV_AUX_BAD_GUID_SUM
:
2269 (void) printf(gettext("missing device"));
2272 case VDEV_AUX_NO_REPLICAS
:
2273 (void) printf(gettext("insufficient replicas"));
2276 case VDEV_AUX_VERSION_NEWER
:
2277 (void) printf(gettext("newer version"));
2280 case VDEV_AUX_UNSUP_FEAT
:
2281 (void) printf(gettext("unsupported feature(s)"));
2284 case VDEV_AUX_ASHIFT_TOO_BIG
:
2285 (void) printf(gettext("unsupported minimum blocksize"));
2288 case VDEV_AUX_SPARED
:
2289 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2290 &spare_cb
.cb_guid
) == 0);
2291 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2292 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2293 zpool_get_name(zhp
)) == 0)
2294 (void) printf(gettext("currently in "
2297 (void) printf(gettext("in use by "
2299 zpool_get_name(spare_cb
.cb_zhp
));
2300 zpool_close(spare_cb
.cb_zhp
);
2302 (void) printf(gettext("currently in use"));
2306 case VDEV_AUX_ERR_EXCEEDED
:
2307 (void) printf(gettext("too many errors"));
2310 case VDEV_AUX_IO_FAILURE
:
2311 (void) printf(gettext("experienced I/O failures"));
2314 case VDEV_AUX_BAD_LOG
:
2315 (void) printf(gettext("bad intent log"));
2318 case VDEV_AUX_EXTERNAL
:
2319 (void) printf(gettext("external device fault"));
2322 case VDEV_AUX_SPLIT_POOL
:
2323 (void) printf(gettext("split into new pool"));
2326 case VDEV_AUX_ACTIVE
:
2327 (void) printf(gettext("currently in use"));
2330 case VDEV_AUX_CHILDREN_OFFLINE
:
2331 (void) printf(gettext("all children offline"));
2335 (void) printf(gettext("corrupted data"));
2339 } else if (children
== 0 && !isspare
&&
2340 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2341 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2342 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2344 gettext(" block size: %dB configured, %dB native"),
2345 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2348 /* The root vdev has the scrub/resilver stats */
2349 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2350 ZPOOL_CONFIG_VDEV_TREE
);
2351 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2352 (uint64_t **)&ps
, &c
);
2354 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2355 if (vs
->vs_scan_processed
!= 0) {
2356 (void) printf(gettext(" (%s)"),
2357 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2358 "resilvering" : "repairing");
2359 } else if (vs
->vs_resilver_deferred
) {
2360 (void) printf(gettext(" (awaiting resilver)"));
2364 /* The top-level vdevs have the rebuild stats */
2365 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2367 if (vs
->vs_rebuild_processed
!= 0) {
2368 (void) printf(gettext(" (resilvering)"));
2372 if (cb
->vcdl
!= NULL
) {
2373 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2375 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2379 /* Display vdev initialization and trim status for leaves. */
2380 if (children
== 0) {
2381 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2382 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2385 (void) printf("\n");
2387 for (c
= 0; c
< children
; c
++) {
2388 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2390 /* Don't print logs or holes here */
2391 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2393 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2395 if (islog
|| ishole
)
2397 /* Only print normal classes here */
2398 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2401 /* Provide vdev_rebuild_stats to children if available */
2403 (void) nvlist_lookup_uint64_array(nv
,
2404 ZPOOL_CONFIG_REBUILD_STATS
,
2405 (uint64_t **)&vrs
, &i
);
2408 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2409 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2410 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2417 * Print the configuration of an exported pool. Iterate over all vdevs in the
2418 * pool, printing out the name and status for each one.
2421 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2429 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2430 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2431 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2434 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2435 (uint64_t **)&vs
, &c
) == 0);
2437 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2438 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2440 if (vs
->vs_aux
!= 0) {
2443 switch (vs
->vs_aux
) {
2444 case VDEV_AUX_OPEN_FAILED
:
2445 (void) printf(gettext("cannot open"));
2448 case VDEV_AUX_BAD_GUID_SUM
:
2449 (void) printf(gettext("missing device"));
2452 case VDEV_AUX_NO_REPLICAS
:
2453 (void) printf(gettext("insufficient replicas"));
2456 case VDEV_AUX_VERSION_NEWER
:
2457 (void) printf(gettext("newer version"));
2460 case VDEV_AUX_UNSUP_FEAT
:
2461 (void) printf(gettext("unsupported feature(s)"));
2464 case VDEV_AUX_ERR_EXCEEDED
:
2465 (void) printf(gettext("too many errors"));
2468 case VDEV_AUX_ACTIVE
:
2469 (void) printf(gettext("currently in use"));
2472 case VDEV_AUX_CHILDREN_OFFLINE
:
2473 (void) printf(gettext("all children offline"));
2477 (void) printf(gettext("corrupted data"));
2481 (void) printf("\n");
2483 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2484 &child
, &children
) != 0)
2487 for (c
= 0; c
< children
; c
++) {
2488 uint64_t is_log
= B_FALSE
;
2490 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2494 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2497 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2498 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2499 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2503 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2504 &child
, &children
) == 0) {
2505 (void) printf(gettext("\tcache\n"));
2506 for (c
= 0; c
< children
; c
++) {
2507 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2509 (void) printf("\t %s\n", vname
);
2514 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2515 &child
, &children
) == 0) {
2516 (void) printf(gettext("\tspares\n"));
2517 for (c
= 0; c
< children
; c
++) {
2518 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2520 (void) printf("\t %s\n", vname
);
2527 * Print specialized class vdevs.
2529 * These are recorded as top level vdevs in the main pool child array
2530 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2531 * print_status_config() or print_import_config() to print the top level
2532 * class vdevs then any of their children (eg mirrored slogs) are printed
2533 * recursively - which works because only the top level vdev is marked.
2536 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2541 boolean_t printed
= B_FALSE
;
2543 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2545 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2549 for (c
= 0; c
< children
; c
++) {
2550 uint64_t is_log
= B_FALSE
;
2554 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2558 bias
= VDEV_ALLOC_CLASS_LOGS
;
2560 (void) nvlist_lookup_string(child
[c
],
2561 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2562 (void) nvlist_lookup_string(child
[c
],
2563 ZPOOL_CONFIG_TYPE
, &type
);
2566 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2568 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2572 (void) printf("\t%s\t\n", gettext(class));
2576 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2577 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2578 if (cb
->cb_print_status
)
2579 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2582 print_import_config(cb
, name
, child
[c
], 2);
2588 * Display the status for the given pool.
2591 show_import(nvlist_t
*config
)
2593 uint64_t pool_state
;
2597 uint64_t hostid
= 0;
2599 char *hostname
= "unknown";
2600 nvlist_t
*nvroot
, *nvinfo
;
2601 zpool_status_t reason
;
2602 zpool_errata_t errata
;
2606 status_cbdata_t cb
= { 0 };
2608 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2610 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2612 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2614 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2617 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2618 (uint64_t **)&vs
, &vsc
) == 0);
2619 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2621 reason
= zpool_import_status(config
, &msgid
, &errata
);
2623 (void) printf(gettext(" pool: %s\n"), name
);
2624 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2625 (void) printf(gettext(" state: %s"), health
);
2626 if (pool_state
== POOL_STATE_DESTROYED
)
2627 (void) printf(gettext(" (DESTROYED)"));
2628 (void) printf("\n");
2631 case ZPOOL_STATUS_MISSING_DEV_R
:
2632 case ZPOOL_STATUS_MISSING_DEV_NR
:
2633 case ZPOOL_STATUS_BAD_GUID_SUM
:
2634 printf_color(ANSI_BOLD
, gettext("status: "));
2635 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2636 "missing from the system.\n"));
2639 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2640 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2641 printf_color(ANSI_BOLD
, gettext("status: "));
2642 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2643 " corrupted data.\n"));
2646 case ZPOOL_STATUS_CORRUPT_DATA
:
2648 gettext(" status: The pool data is corrupted.\n"));
2651 case ZPOOL_STATUS_OFFLINE_DEV
:
2652 printf_color(ANSI_BOLD
, gettext("status: "));
2653 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2654 "are offlined.\n"));
2657 case ZPOOL_STATUS_CORRUPT_POOL
:
2658 printf_color(ANSI_BOLD
, gettext("status: "));
2659 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2663 case ZPOOL_STATUS_VERSION_OLDER
:
2664 printf_color(ANSI_BOLD
, gettext("status: "));
2665 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2666 "a legacy on-disk version.\n"));
2669 case ZPOOL_STATUS_VERSION_NEWER
:
2670 printf_color(ANSI_BOLD
, gettext("status: "));
2671 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2672 "an incompatible version.\n"));
2675 case ZPOOL_STATUS_FEAT_DISABLED
:
2676 printf_color(ANSI_BOLD
, gettext("status: "));
2677 printf_color(ANSI_YELLOW
, gettext("Some supported features are "
2678 "not enabled on the pool.\n"));
2681 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2682 printf_color(ANSI_BOLD
, gettext("status: "));
2683 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2684 "feature(s) not supported on this system:\n"));
2685 color_start(ANSI_YELLOW
);
2686 zpool_print_unsup_feat(config
);
2690 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2691 printf_color(ANSI_BOLD
, gettext("status: "));
2692 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2693 "accessed in read-only mode on this system. It\n\tcannot be"
2694 " accessed in read-write mode because it uses the "
2695 "following\n\tfeature(s) not supported on this system:\n"));
2696 color_start(ANSI_YELLOW
);
2697 zpool_print_unsup_feat(config
);
2701 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2702 printf_color(ANSI_BOLD
, gettext("status: "));
2703 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2704 "imported by another system.\n"));
2707 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2708 printf_color(ANSI_BOLD
, gettext("status: "));
2709 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2710 "multihost property on. It cannot\n\tbe safely imported "
2711 "when the system hostid is not set.\n"));
2714 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2715 printf_color(ANSI_BOLD
, gettext("status: "));
2716 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2717 "by another system.\n"));
2720 case ZPOOL_STATUS_FAULTED_DEV_R
:
2721 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2722 printf_color(ANSI_BOLD
, gettext("status: "));
2723 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2727 case ZPOOL_STATUS_BAD_LOG
:
2728 printf_color(ANSI_BOLD
, gettext("status: "));
2729 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2733 case ZPOOL_STATUS_RESILVERING
:
2734 case ZPOOL_STATUS_REBUILDING
:
2735 printf_color(ANSI_BOLD
, gettext("status: "));
2736 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2737 "being resilvered.\n"));
2740 case ZPOOL_STATUS_ERRATA
:
2741 printf_color(ANSI_BOLD
, gettext("status: "));
2742 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2746 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2747 printf_color(ANSI_BOLD
, gettext("status: "));
2748 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2749 "configured to use a non-native block size.\n"
2750 "\tExpect reduced performance.\n"));
2755 * No other status can be seen when importing pools.
2757 assert(reason
== ZPOOL_STATUS_OK
);
2761 * Print out an action according to the overall state of the pool.
2763 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2764 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2765 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2766 (void) printf(gettext(" action: The pool can be "
2767 "imported using its name or numeric identifier, "
2768 "though\n\tsome features will not be available "
2769 "without an explicit 'zpool upgrade'.\n"));
2770 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2771 (void) printf(gettext(" action: The pool can be "
2772 "imported using its name or numeric "
2773 "identifier and\n\tthe '-f' flag.\n"));
2774 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2776 case ZPOOL_ERRATA_NONE
:
2779 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2780 (void) printf(gettext(" action: The pool can "
2781 "be imported using its name or numeric "
2782 "identifier,\n\thowever there is a compat"
2783 "ibility issue which should be corrected"
2784 "\n\tby running 'zpool scrub'\n"));
2787 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2788 (void) printf(gettext(" action: The pool can"
2789 "not be imported with this version of ZFS "
2790 "due to\n\tan active asynchronous destroy. "
2791 "Revert to an earlier version\n\tand "
2792 "allow the destroy to complete before "
2796 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2797 (void) printf(gettext(" action: Existing "
2798 "encrypted datasets contain an on-disk "
2799 "incompatibility, which\n\tneeds to be "
2800 "corrected. Backup these datasets to new "
2801 "encrypted datasets\n\tand destroy the "
2805 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2806 (void) printf(gettext(" action: Existing "
2807 "encrypted snapshots and bookmarks contain "
2808 "an on-disk\n\tincompatibility. This may "
2809 "cause on-disk corruption if they are used"
2810 "\n\twith 'zfs recv'. To correct the "
2811 "issue, enable the bookmark_v2 feature.\n\t"
2812 "No additional action is needed if there "
2813 "are no encrypted snapshots or\n\t"
2814 "bookmarks. If preserving the encrypted "
2815 "snapshots and bookmarks is\n\trequired, "
2816 "use a non-raw send to backup and restore "
2817 "them. Alternately,\n\tthey may be removed"
2818 " to resolve the incompatibility.\n"));
2822 * All errata must contain an action message.
2827 (void) printf(gettext(" action: The pool can be "
2828 "imported using its name or numeric "
2831 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2832 (void) printf(gettext(" action: The pool can be imported "
2833 "despite missing or damaged devices. The\n\tfault "
2834 "tolerance of the pool may be compromised if imported.\n"));
2837 case ZPOOL_STATUS_VERSION_NEWER
:
2838 (void) printf(gettext(" action: The pool cannot be "
2839 "imported. Access the pool on a system running "
2840 "newer\n\tsoftware, or recreate the pool from "
2843 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2844 printf_color(ANSI_BOLD
, gettext("action: "));
2845 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2846 "imported. Access the pool on a system that "
2847 "supports\n\tthe required feature(s), or recreate "
2848 "the pool from backup.\n"));
2850 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2851 printf_color(ANSI_BOLD
, gettext("action: "));
2852 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2853 "imported in read-write mode. Import the pool "
2855 "\t\"-o readonly=on\", access the pool on a system "
2856 "that supports the\n\trequired feature(s), or "
2857 "recreate the pool from backup.\n"));
2859 case ZPOOL_STATUS_MISSING_DEV_R
:
2860 case ZPOOL_STATUS_MISSING_DEV_NR
:
2861 case ZPOOL_STATUS_BAD_GUID_SUM
:
2862 (void) printf(gettext(" action: The pool cannot be "
2863 "imported. Attach the missing\n\tdevices and try "
2866 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2867 VERIFY0(nvlist_lookup_nvlist(config
,
2868 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2870 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2871 hostname
= fnvlist_lookup_string(nvinfo
,
2872 ZPOOL_CONFIG_MMP_HOSTNAME
);
2874 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2875 hostid
= fnvlist_lookup_uint64(nvinfo
,
2876 ZPOOL_CONFIG_MMP_HOSTID
);
2878 (void) printf(gettext(" action: The pool must be "
2879 "exported from %s (hostid=%lx)\n\tbefore it "
2880 "can be safely imported.\n"), hostname
,
2881 (unsigned long) hostid
);
2883 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2884 (void) printf(gettext(" action: Set a unique system "
2885 "hostid with the zgenhostid(8) command.\n"));
2888 (void) printf(gettext(" action: The pool cannot be "
2889 "imported due to damaged devices or data.\n"));
2893 /* Print the comment attached to the pool. */
2894 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2895 (void) printf(gettext("comment: %s\n"), comment
);
2898 * If the state is "closed" or "can't open", and the aux state
2899 * is "corrupt data":
2901 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2902 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2903 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2904 if (pool_state
== POOL_STATE_DESTROYED
)
2905 (void) printf(gettext("\tThe pool was destroyed, "
2906 "but can be imported using the '-Df' flags.\n"));
2907 else if (pool_state
!= POOL_STATE_EXPORTED
)
2908 (void) printf(gettext("\tThe pool may be active on "
2909 "another system, but can be imported using\n\t"
2910 "the '-f' flag.\n"));
2913 if (msgid
!= NULL
) {
2914 (void) printf(gettext(
2915 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
2919 (void) printf(gettext(" config:\n\n"));
2921 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2923 if (cb
.cb_namewidth
< 10)
2924 cb
.cb_namewidth
= 10;
2926 print_import_config(&cb
, name
, nvroot
, 0);
2928 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
2929 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
2930 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
2932 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2933 (void) printf(gettext("\n\tAdditional devices are known to "
2934 "be part of this pool, though their\n\texact "
2935 "configuration cannot be determined.\n"));
2940 zfs_force_import_required(nvlist_t
*config
)
2943 uint64_t hostid
= 0;
2946 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2947 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2949 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
2952 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2953 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
2954 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2955 ZPOOL_CONFIG_MMP_STATE
);
2957 if (mmp_state
!= MMP_STATE_INACTIVE
)
2965 * Perform the import for the given configuration. This passes the heavy
2966 * lifting off to zpool_import_props(), and then mounts the datasets contained
2970 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
2971 nvlist_t
*props
, int flags
)
2974 zpool_handle_t
*zhp
;
2978 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2979 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2981 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
2982 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
2983 "is formatted using an unsupported ZFS version\n"), name
);
2985 } else if (zfs_force_import_required(config
) &&
2986 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
2987 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
2990 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2991 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
2992 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2993 ZPOOL_CONFIG_MMP_STATE
);
2995 if (mmp_state
== MMP_STATE_ACTIVE
) {
2996 char *hostname
= "<unknown>";
2997 uint64_t hostid
= 0;
2999 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3000 hostname
= fnvlist_lookup_string(nvinfo
,
3001 ZPOOL_CONFIG_MMP_HOSTNAME
);
3003 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3004 hostid
= fnvlist_lookup_uint64(nvinfo
,
3005 ZPOOL_CONFIG_MMP_HOSTID
);
3007 (void) fprintf(stderr
, gettext("cannot import '%s': "
3008 "pool is imported on %s (hostid: "
3009 "0x%lx)\nExport the pool on the other system, "
3010 "then run 'zpool import'.\n"),
3011 name
, hostname
, (unsigned long) hostid
);
3012 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3013 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3014 "pool has the multihost property on and the\n"
3015 "system's hostid is not set. Set a unique hostid "
3016 "with the zgenhostid(8) command.\n"), name
);
3018 char *hostname
= "<unknown>";
3019 uint64_t timestamp
= 0;
3020 uint64_t hostid
= 0;
3022 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3023 hostname
= fnvlist_lookup_string(config
,
3024 ZPOOL_CONFIG_HOSTNAME
);
3026 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3027 timestamp
= fnvlist_lookup_uint64(config
,
3028 ZPOOL_CONFIG_TIMESTAMP
);
3030 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3031 hostid
= fnvlist_lookup_uint64(config
,
3032 ZPOOL_CONFIG_HOSTID
);
3034 (void) fprintf(stderr
, gettext("cannot import '%s': "
3035 "pool was previously in use from another system.\n"
3036 "Last accessed by %s (hostid=%lx) at %s"
3037 "The pool can be imported, use 'zpool import -f' "
3038 "to import the pool.\n"), name
, hostname
,
3039 (unsigned long)hostid
, ctime((time_t *)×tamp
));
3045 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3048 if (newname
!= NULL
)
3049 name
= (char *)newname
;
3051 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3055 * Loading keys is best effort. We don't want to return immediately
3056 * if it fails but we do want to give the error to the caller.
3058 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
3059 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
3064 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3065 !(flags
& ZFS_IMPORT_ONLY
) &&
3066 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3075 typedef struct target_exists_args
{
3076 const char *poolname
;
3078 } target_exists_args_t
;
3081 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3083 target_exists_args_t
*args
= data
;
3084 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3090 if (args
->poolname
!= NULL
) {
3093 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3095 if (strcmp(pool_name
, args
->poolname
) == 0)
3100 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3102 if (pool_guid
== args
->poolguid
)
3110 * zpool checkpoint <pool>
3111 * checkpoint --discard <pool>
3113 * -d Discard the checkpoint from a checkpointed
3116 * -w Wait for discarding a checkpoint to complete.
3119 * Checkpoints the specified pool, by taking a "snapshot" of its
3120 * current state. A pool can only have one checkpoint at a time.
3123 zpool_do_checkpoint(int argc
, char **argv
)
3125 boolean_t discard
, wait
;
3127 zpool_handle_t
*zhp
;
3130 struct option long_options
[] = {
3131 {"discard", no_argument
, NULL
, 'd'},
3132 {"wait", no_argument
, NULL
, 'w'},
3138 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3147 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3153 if (wait
&& !discard
) {
3154 (void) fprintf(stderr
, gettext("--wait only valid when "
3155 "--discard also specified\n"));
3163 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3168 (void) fprintf(stderr
, gettext("too many arguments\n"));
3174 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3175 /* As a special case, check for use of '/' in the name */
3176 if (strchr(pool
, '/') != NULL
)
3177 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3178 "doesn't work on datasets. To save the state "
3179 "of a dataset from a specific point in time "
3180 "please use 'zfs snapshot'\n"));
3185 err
= (zpool_discard_checkpoint(zhp
) != 0);
3186 if (err
== 0 && wait
)
3187 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3189 err
= (zpool_checkpoint(zhp
) != 0);
3197 #define CHECKPOINT_OPT 1024
3200 * zpool import [-d dir] [-D]
3201 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3202 * [-d dir | -c cachefile] [-f] -a
3203 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3204 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
3206 * -c Read pool information from a cachefile instead of searching
3209 * -d Scan in a specific directory, other than /dev/. More than
3210 * one directory can be specified using multiple '-d' options.
3212 * -D Scan for previously destroyed pools or import all or only
3213 * specified destroyed pools.
3215 * -R Temporarily import the pool, with all mountpoints relative to
3216 * the given root. The pool will remain exported when the machine
3219 * -V Import even in the presence of faulted vdevs. This is an
3220 * intentionally undocumented option for testing purposes, and
3221 * treats the pool configuration as complete, leaving any bad
3222 * vdevs in the FAULTED state. In other words, it does verbatim
3225 * -f Force import, even if it appears that the pool is active.
3227 * -F Attempt rewind if necessary.
3229 * -n See if rewind would work, but don't actually rewind.
3231 * -N Import the pool but don't mount datasets.
3233 * -T Specify a starting txg to use for import. This option is
3234 * intentionally undocumented option for testing purposes.
3236 * -a Import all pools found.
3238 * -l Load encryption keys while importing.
3240 * -o Set property=value and/or temporary mount options (without '=').
3242 * -s Scan using the default search path, the libblkid cache will
3245 * --rewind-to-checkpoint
3246 * Import the pool and revert back to the checkpoint.
3248 * The import command scans for pools to import, and import pools based on pool
3249 * name and GUID. The pool can also be renamed as part of the import process.
3252 zpool_do_import(int argc
, char **argv
)
3254 char **searchdirs
= NULL
;
3255 char *env
, *envdup
= NULL
;
3259 nvlist_t
*pools
= NULL
;
3260 boolean_t do_all
= B_FALSE
;
3261 boolean_t do_destroyed
= B_FALSE
;
3262 char *mntopts
= NULL
;
3265 uint64_t searchguid
= 0;
3266 char *searchname
= NULL
;
3268 nvlist_t
*found_config
;
3269 nvlist_t
*policy
= NULL
;
3270 nvlist_t
*props
= NULL
;
3272 int flags
= ZFS_IMPORT_NORMAL
;
3273 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3274 boolean_t dryrun
= B_FALSE
;
3275 boolean_t do_rewind
= B_FALSE
;
3276 boolean_t xtreme_rewind
= B_FALSE
;
3277 boolean_t do_scan
= B_FALSE
;
3278 boolean_t pool_exists
= B_FALSE
;
3279 uint64_t pool_state
, txg
= -1ULL;
3280 char *cachefile
= NULL
;
3281 importargs_t idata
= { 0 };
3284 struct option long_options
[] = {
3285 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3290 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3291 long_options
, NULL
)) != -1) {
3300 if (searchdirs
== NULL
) {
3301 searchdirs
= safe_malloc(sizeof (char *));
3303 char **tmp
= safe_malloc((nsearch
+ 1) *
3305 bcopy(searchdirs
, tmp
, nsearch
*
3310 searchdirs
[nsearch
++] = optarg
;
3313 do_destroyed
= B_TRUE
;
3316 flags
|= ZFS_IMPORT_ANY_HOST
;
3322 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3325 flags
|= ZFS_IMPORT_MISSING_LOG
;
3331 flags
|= ZFS_IMPORT_ONLY
;
3334 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3337 if (add_prop_list(optarg
, propval
,
3345 if (add_prop_list(zpool_prop_to_name(
3346 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3348 if (add_prop_list_default(zpool_prop_to_name(
3349 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3356 flags
|= ZFS_IMPORT_TEMP_NAME
;
3357 if (add_prop_list_default(zpool_prop_to_name(
3358 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3364 txg
= strtoull(optarg
, &endptr
, 0);
3365 if (errno
!= 0 || *endptr
!= '\0') {
3366 (void) fprintf(stderr
,
3367 gettext("invalid txg value\n"));
3370 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3373 flags
|= ZFS_IMPORT_VERBATIM
;
3376 xtreme_rewind
= B_TRUE
;
3378 case CHECKPOINT_OPT
:
3379 flags
|= ZFS_IMPORT_CHECKPOINT
;
3382 (void) fprintf(stderr
, gettext("missing argument for "
3383 "'%c' option\n"), optopt
);
3387 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3396 if (cachefile
&& nsearch
!= 0) {
3397 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3401 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3402 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3406 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3407 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3412 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3413 (void) fprintf(stderr
,
3414 gettext("-n or -X only meaningful with -F\n"));
3418 rewind_policy
= ZPOOL_TRY_REWIND
;
3420 rewind_policy
= ZPOOL_DO_REWIND
;
3422 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3424 /* In the future, we can capture further policy and include it here */
3425 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3426 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3427 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3428 rewind_policy
) != 0)
3431 /* check argument count */
3434 (void) fprintf(stderr
, gettext("too many arguments\n"));
3439 (void) fprintf(stderr
, gettext("too many arguments\n"));
3445 * Check for the effective uid. We do this explicitly here because
3446 * otherwise any attempt to discover pools will silently fail.
3448 if (argc
== 0 && geteuid() != 0) {
3449 (void) fprintf(stderr
, gettext("cannot "
3450 "discover pools: permission denied\n"));
3451 if (searchdirs
!= NULL
)
3455 nvlist_free(policy
);
3460 * Depending on the arguments given, we do one of the following:
3462 * <none> Iterate through all pools and display information about
3465 * -a Iterate through all pools and try to import each one.
3467 * <id> Find the pool that corresponds to the given GUID/pool
3468 * name and import that one.
3470 * -D Above options applies only to destroyed pools.
3476 searchguid
= strtoull(argv
[0], &endptr
, 10);
3477 if (errno
!= 0 || *endptr
!= '\0') {
3478 searchname
= argv
[0];
3481 found_config
= NULL
;
3484 * User specified a name or guid. Ensure it's unique.
3486 target_exists_args_t search
= {searchname
, searchguid
};
3487 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3491 * Check the environment for the preferred search path.
3493 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3496 envdup
= strdup(env
);
3498 dir
= strtok(envdup
, ":");
3499 while (dir
!= NULL
) {
3500 if (searchdirs
== NULL
) {
3501 searchdirs
= safe_malloc(sizeof (char *));
3503 char **tmp
= safe_malloc((nsearch
+ 1) *
3505 bcopy(searchdirs
, tmp
, nsearch
*
3510 searchdirs
[nsearch
++] = dir
;
3511 dir
= strtok(NULL
, ":");
3515 idata
.path
= searchdirs
;
3516 idata
.paths
= nsearch
;
3517 idata
.poolname
= searchname
;
3518 idata
.guid
= searchguid
;
3519 idata
.cachefile
= cachefile
;
3520 idata
.scan
= do_scan
;
3521 idata
.policy
= policy
;
3523 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3525 if (pools
!= NULL
&& pool_exists
&&
3526 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3527 (void) fprintf(stderr
, gettext("cannot import '%s': "
3528 "a pool with that name already exists\n"),
3530 (void) fprintf(stderr
, gettext("use the form '%s "
3531 "<pool | id> <newpool>' to give it a new name\n"),
3534 } else if (pools
== NULL
&& pool_exists
) {
3535 (void) fprintf(stderr
, gettext("cannot import '%s': "
3536 "a pool with that name is already created/imported,\n"),
3538 (void) fprintf(stderr
, gettext("and no additional pools "
3539 "with that name were found\n"));
3541 } else if (pools
== NULL
) {
3543 (void) fprintf(stderr
, gettext("cannot import '%s': "
3544 "no such pool available\n"), argv
[0]);
3550 if (searchdirs
!= NULL
)
3554 nvlist_free(policy
);
3561 * At this point we have a list of import candidate configs. Even if
3562 * we were searching by pool name or guid, we still need to
3563 * post-process the list to deal with pool state and possible
3569 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3571 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3573 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3575 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3577 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3580 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3587 (void) printf("\n");
3590 err
|= do_import(config
, NULL
, mntopts
,
3593 show_import(config
);
3595 } else if (searchname
!= NULL
) {
3599 * We are searching for a pool based on name.
3601 verify(nvlist_lookup_string(config
,
3602 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3604 if (strcmp(name
, searchname
) == 0) {
3605 if (found_config
!= NULL
) {
3606 (void) fprintf(stderr
, gettext(
3607 "cannot import '%s': more than "
3608 "one matching pool\n"), searchname
);
3609 (void) fprintf(stderr
, gettext(
3610 "import by numeric ID instead\n"));
3613 found_config
= config
;
3619 * Search for a pool by guid.
3621 verify(nvlist_lookup_uint64(config
,
3622 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3624 if (guid
== searchguid
)
3625 found_config
= config
;
3630 * If we were searching for a specific pool, verify that we found a
3631 * pool, and then do the import.
3633 if (argc
!= 0 && err
== 0) {
3634 if (found_config
== NULL
) {
3635 (void) fprintf(stderr
, gettext("cannot import '%s': "
3636 "no such pool available\n"), argv
[0]);
3639 err
|= do_import(found_config
, argc
== 1 ? NULL
:
3640 argv
[1], mntopts
, props
, flags
);
3645 * If we were just looking for pools, report an error if none were
3648 if (argc
== 0 && first
)
3649 (void) fprintf(stderr
,
3650 gettext("no pools available to import\n"));
3655 nvlist_free(policy
);
3656 if (searchdirs
!= NULL
)
3661 return (err
? 1 : 0);
3665 * zpool sync [-f] [pool] ...
3667 * -f (undocumented) force uberblock (and config including zpool cache file)
3670 * Sync the specified pool(s).
3671 * Without arguments "zpool sync" will sync all pools.
3672 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3676 zpool_do_sync(int argc
, char **argv
)
3679 boolean_t force
= B_FALSE
;
3682 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3688 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3697 /* if argc == 0 we will execute zpool_sync_one on all pools */
3698 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
, zpool_sync_one
,
3704 typedef struct iostat_cbdata
{
3709 char **cb_vdev_names
; /* Only show these vdevs */
3710 unsigned int cb_vdev_names_count
;
3711 boolean_t cb_verbose
;
3712 boolean_t cb_literal
;
3713 boolean_t cb_scripted
;
3714 zpool_list_t
*cb_list
;
3715 vdev_cmd_data_list_t
*vcdl
;
3719 typedef struct name_and_columns
{
3720 const char *name
; /* Column name */
3721 unsigned int columns
; /* Center name to this number of columns */
3722 } name_and_columns_t
;
3724 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */
3726 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3728 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3730 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3731 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL
}},
3732 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3733 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3734 {"trimq_write", 2}, {NULL
}},
3735 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3736 {"asyncq_wait", 2}, {NULL
}},
3737 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3738 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3739 {"trim", 2}, {NULL
}},
3742 /* Shorthand - if "columns" field not set, default to 1 column */
3743 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3745 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3747 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3748 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL
}},
3749 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3750 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3751 {"pend"}, {"activ"}, {NULL
}},
3752 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3753 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL
}},
3754 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3755 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3758 static const char *histo_to_title
[] = {
3759 [IOS_L_HISTO
] = "latency",
3760 [IOS_RQ_HISTO
] = "req_size",
3764 * Return the number of labels in a null-terminated name_and_columns_t
3769 label_array_len(const name_and_columns_t
*labels
)
3773 while (labels
[i
].name
)
3780 * Return the number of strings in a null-terminated string array.
3783 * const char foo[] = {"bar", "baz", NULL}
3788 str_array_len(const char *array
[])
3799 * Return a default column width for default/latency/queue columns. This does
3800 * not include histograms, which have their columns autosized.
3803 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3805 unsigned long column_width
= 5; /* Normal niceprint */
3806 static unsigned long widths
[] = {
3808 * Choose some sane default column sizes for printing the
3811 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3812 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3813 [IOS_QUEUES
] = 6, /* 1M queue entries */
3814 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
3815 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
3819 column_width
= widths
[type
];
3821 return (column_width
);
3825 * Print the column labels, i.e:
3827 * capacity operations bandwidth
3828 * alloc free read write read write ...
3830 * If force_column_width is set, use it for the column width. If not set, use
3831 * the default column width.
3834 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3835 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3838 int text_start
, rw_column_width
, spaces_to_end
;
3839 uint64_t flags
= cb
->cb_flags
;
3841 unsigned int column_width
= force_column_width
;
3843 /* For each bit set in flags */
3844 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3845 idx
= lowbit64(f
) - 1;
3846 if (!force_column_width
)
3847 column_width
= default_column_width(cb
, idx
);
3848 /* Print our top labels centered over "read write" label. */
3849 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3850 const char *name
= labels
[idx
][i
].name
;
3852 * We treat labels[][].columns == 0 as shorthand
3853 * for one column. It makes writing out the label
3854 * tables more concise.
3856 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3857 unsigned int slen
= strlen(name
);
3859 rw_column_width
= (column_width
* columns
) +
3860 (2 * (columns
- 1));
3862 text_start
= (int)((rw_column_width
) / columns
-
3867 printf(" "); /* Two spaces between columns */
3869 /* Space from beginning of column to label */
3870 for (s
= 0; s
< text_start
; s
++)
3875 /* Print space after label to end of column */
3876 spaces_to_end
= rw_column_width
- text_start
- slen
;
3877 if (spaces_to_end
< 0)
3880 for (s
= 0; s
< spaces_to_end
; s
++)
3888 * print_cmd_columns - Print custom column titles from -c
3890 * If the user specified the "zpool status|iostat -c" then print their custom
3891 * column titles in the header. For example, print_cmd_columns() would print
3892 * the " col1 col2" part of this:
3894 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3896 * capacity operations bandwidth
3897 * pool alloc free read write read write col1 col2
3898 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3899 * mypool 269K 1008M 0 0 107 946
3900 * mirror 269K 1008M 0 0 107 946
3901 * sdb - - 0 0 102 473 val1 val2
3902 * sdc - - 0 0 5 473 val1 val2
3903 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3906 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3909 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3911 if (vcdl
->count
== 0 || data
== NULL
)
3915 * Each vdev cmd should have the same column names unless the user did
3916 * something weird with their cmd. Just take the column names from the
3917 * first vdev and assume it works for all of them.
3919 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3922 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3925 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
3926 vcdl
->uniq_cols
[i
]);
3933 * Utility function to print out a line of dashes like:
3935 * -------------------------------- ----- ----- ----- ----- -----
3937 * ...or a dashed named-row line like:
3943 * @force_column_width If non-zero, use the value as the column width.
3944 * Otherwise use the default column widths.
3946 * @name: Print a dashed named-row line starting
3947 * with @name. Otherwise, print a regular
3951 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3955 unsigned int namewidth
;
3956 uint64_t flags
= cb
->cb_flags
;
3959 const name_and_columns_t
*labels
;
3963 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
3964 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
3965 } else if (cb
->cb_vdev_names_count
) {
3971 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
3972 name
? strlen(name
) : 0);
3976 printf("%-*s", namewidth
, name
);
3978 for (i
= 0; i
< namewidth
; i
++)
3982 /* For each bit in flags */
3983 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3984 unsigned int column_width
;
3985 idx
= lowbit64(f
) - 1;
3986 if (force_column_width
)
3987 column_width
= force_column_width
;
3989 column_width
= default_column_width(cb
, idx
);
3991 labels
= iostat_bottom_labels
[idx
];
3992 for (i
= 0; i
< label_array_len(labels
); i
++) {
3994 printf(" %*s-", column_width
- 1, " ");
3996 printf(" %.*s", column_width
,
3997 "--------------------");
4004 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4005 unsigned int force_column_width
)
4007 print_iostat_dashes(cb
, force_column_width
, NULL
);
4011 print_iostat_separator(iostat_cbdata_t
*cb
)
4013 print_iostat_separator_impl(cb
, 0);
4017 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4018 const char *histo_vdev_name
)
4020 unsigned int namewidth
;
4023 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4024 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4025 } else if (cb
->cb_vdev_names_count
) {
4031 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4032 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4034 if (histo_vdev_name
)
4035 printf("%-*s", namewidth
, histo_vdev_name
);
4037 printf("%*s", namewidth
, "");
4040 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4043 printf("%-*s", namewidth
, title
);
4045 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4046 if (cb
->vcdl
!= NULL
)
4047 print_cmd_columns(cb
->vcdl
, 0);
4051 print_iostat_separator_impl(cb
, force_column_width
);
4053 if (cb
->vcdl
!= NULL
)
4054 print_cmd_columns(cb
->vcdl
, 1);
4060 print_iostat_header(iostat_cbdata_t
*cb
)
4062 print_iostat_header_impl(cb
, 0, NULL
);
4067 * Display a single statistic.
4070 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4071 unsigned int column_size
, boolean_t scripted
)
4075 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4078 printf("\t%s", buf
);
4080 printf(" %*s", column_size
, buf
);
4084 * Calculate the default vdev stats
4086 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4087 * stats into calcvs.
4090 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4091 vdev_stat_t
*calcvs
)
4095 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4096 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4097 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4099 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4100 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4104 * Internal representation of the extended iostats data.
4106 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4107 * or single uint64_t's. We make both look like arrays to make them easier
4108 * to process. In order to make single uint64_t's look like arrays, we set
4109 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4110 * we can just use *data and count.
4114 uint_t count
; /* Number of entries in data[] */
4115 uint64_t __data
; /* Only used when data is a single uint64_t */
4119 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4123 for (i
= 0; i
< len
; i
++)
4124 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4130 * Helper function to lookup a uint64_t array or uint64_t value and store its
4131 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4132 * it look like a one element array to make it easier to process.
4135 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4136 struct stat_array
*nva
)
4141 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4142 switch (nvpair_type(tmp
)) {
4143 case DATA_TYPE_UINT64_ARRAY
:
4144 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4146 case DATA_TYPE_UINT64
:
4147 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4148 nva
->data
= &nva
->__data
;
4152 /* Not a uint64_t */
4161 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4162 * subtract them, and return the results in a newly allocated stat_array.
4163 * You must free the returned array after you are done with it with
4164 * free_calc_stats().
4166 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4169 static struct stat_array
*
4170 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4173 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4174 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4176 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4178 /* Extract our extended stats nvlist from the main list */
4179 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4182 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4186 newnva
= safe_malloc(alloc_size
);
4187 oldnva
= safe_malloc(alloc_size
);
4188 calcnva
= safe_malloc(alloc_size
);
4190 for (j
= 0; j
< len
; j
++) {
4191 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4193 calcnva
[j
].count
= newnva
[j
].count
;
4194 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4195 calcnva
[j
].data
= safe_malloc(alloc_size
);
4196 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4199 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4201 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4202 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4211 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4214 for (i
= 0; i
< len
; i
++)
4221 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4222 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4228 enum zfs_nicenum_format format
;
4229 unsigned int buckets
;
4230 unsigned int start_bucket
;
4233 format
= ZFS_NICENUM_RAW
;
4235 format
= ZFS_NICENUM_1024
;
4237 /* All these histos are the same size, so just use nva[0].count */
4238 buckets
= nva
[0].count
;
4240 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4241 /* Start at 512 - req size should never be lower than this */
4247 for (j
= start_bucket
; j
< buckets
; j
++) {
4248 /* Print histogram bucket label */
4249 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4250 /* Ending range of this bucket */
4251 val
= (1UL << (j
+ 1)) - 1;
4252 zfs_nicetime(val
, buf
, sizeof (buf
));
4254 /* Request size (starting range of bucket) */
4256 zfs_nicenum(val
, buf
, sizeof (buf
));
4259 if (cb
->cb_scripted
)
4260 printf("%llu", (u_longlong_t
)val
);
4262 printf("%-*s", namewidth
, buf
);
4264 /* Print the values on the line */
4265 for (i
= 0; i
< len
; i
++) {
4266 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4267 column_width
, cb
->cb_scripted
);
4274 print_solid_separator(unsigned int length
)
4282 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4283 nvlist_t
*newnv
, double scale
, const char *name
)
4285 unsigned int column_width
;
4286 unsigned int namewidth
;
4287 unsigned int entire_width
;
4288 enum iostat_type type
;
4289 struct stat_array
*nva
;
4291 unsigned int names_len
;
4293 /* What type of histo are we? */
4294 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4296 /* Get NULL-terminated array of nvlist names for our histo */
4297 names
= vsx_type_to_nvlist
[type
];
4298 names_len
= str_array_len(names
); /* num of names */
4300 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4302 if (cb
->cb_literal
) {
4303 column_width
= MAX(5,
4304 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4309 namewidth
= MAX(cb
->cb_namewidth
,
4310 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4313 * Calculate the entire line width of what we're printing. The
4314 * +2 is for the two spaces between columns:
4318 /* |___| <---------- column_width */
4320 /* |__________| <--- entire_width */
4322 entire_width
= namewidth
+ (column_width
+ 2) *
4323 label_array_len(iostat_bottom_labels
[type
]);
4325 if (cb
->cb_scripted
)
4326 printf("%s\n", name
);
4328 print_iostat_header_impl(cb
, column_width
, name
);
4330 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4333 free_calc_stats(nva
, names_len
);
4334 if (!cb
->cb_scripted
)
4335 print_solid_separator(entire_width
);
4339 * Calculate the average latency of a power-of-two latency histogram
4342 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4345 uint64_t count
= 0, total
= 0;
4347 for (i
= 0; i
< buckets
; i
++) {
4349 * Our buckets are power-of-two latency ranges. Use the
4350 * midpoint latency of each bucket to calculate the average.
4358 if (histo
[i
] != 0) {
4359 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4364 /* Prevent divide by zero */
4365 return (count
== 0 ? 0 : total
/ count
);
4369 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4374 const char *names
[] = {
4375 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4376 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4377 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4378 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4379 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4380 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4381 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4382 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4383 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4384 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4385 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4386 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4389 struct stat_array
*nva
;
4391 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4392 enum zfs_nicenum_format format
;
4394 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4397 format
= ZFS_NICENUM_RAW
;
4399 format
= ZFS_NICENUM_1024
;
4401 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4402 val
= nva
[i
].data
[0];
4403 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4406 free_calc_stats(nva
, ARRAY_SIZE(names
));
4410 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4415 const char *names
[] = {
4416 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4417 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4418 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4419 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4420 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4421 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4422 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4423 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4424 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4425 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4427 struct stat_array
*nva
;
4429 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4430 enum zfs_nicenum_format format
;
4432 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4435 format
= ZFS_NICENUM_RAWTIME
;
4437 format
= ZFS_NICENUM_TIME
;
4439 /* Print our avg latencies on the line */
4440 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4441 /* Compute average latency for a latency histo */
4442 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4443 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4445 free_calc_stats(nva
, ARRAY_SIZE(names
));
4449 * Print default statistics (capacity/operations/bandwidth)
4452 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4454 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4455 enum zfs_nicenum_format format
;
4456 char na
; /* char to print for "not applicable" values */
4458 if (cb
->cb_literal
) {
4459 format
= ZFS_NICENUM_RAW
;
4462 format
= ZFS_NICENUM_1024
;
4466 /* only toplevel vdevs have capacity stats */
4467 if (vs
->vs_space
== 0) {
4468 if (cb
->cb_scripted
)
4469 printf("\t%c\t%c", na
, na
);
4471 printf(" %*c %*c", column_width
, na
, column_width
,
4474 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4476 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4477 column_width
, cb
->cb_scripted
);
4480 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4481 format
, column_width
, cb
->cb_scripted
);
4482 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4483 format
, column_width
, cb
->cb_scripted
);
4484 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4485 format
, column_width
, cb
->cb_scripted
);
4486 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4487 format
, column_width
, cb
->cb_scripted
);
4490 static const char *class_name
[] = {
4491 VDEV_ALLOC_BIAS_DEDUP
,
4492 VDEV_ALLOC_BIAS_SPECIAL
,
4493 VDEV_ALLOC_CLASS_LOGS
4497 * Print out all the statistics for the given vdev. This can either be the
4498 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4499 * is a verbose output, and we don't want to display the toplevel pool stats.
4501 * Returns the number of stat lines printed.
4504 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4505 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4507 nvlist_t
**oldchild
, **newchild
;
4508 uint_t c
, children
, oldchildren
;
4509 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4510 vdev_stat_t zerovs
= { 0 };
4517 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4520 calcvs
= safe_malloc(sizeof (*calcvs
));
4522 if (oldnv
!= NULL
) {
4523 verify(nvlist_lookup_uint64_array(oldnv
,
4524 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4529 /* Do we only want to see a specific vdev? */
4530 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
4531 /* Yes we do. Is this the vdev? */
4532 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
4534 * This is our vdev. Since it is the only vdev we
4535 * will be displaying, make depth = 0 so that it
4536 * doesn't get indented.
4543 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
4544 /* Couldn't match the name */
4549 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4550 (uint64_t **)&newvs
, &c
) == 0);
4553 * Print the vdev name unless it's is a histogram. Histograms
4554 * display the vdev name in the header itself.
4556 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4557 if (cb
->cb_scripted
) {
4560 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4561 (void) printf("%*s%s", depth
, "", name
);
4563 (void) printf("%*s%s%*s", depth
, "", name
,
4564 (int)(cb
->cb_namewidth
- strlen(name
) -
4569 /* Calculate our scaling factor */
4570 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4571 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4573 * If we specify printing histograms with no time interval, then
4574 * print the histogram numbers over the entire lifetime of the
4582 scale
= (double)NANOSEC
/ tdelta
;
4585 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4586 calc_default_iostats(oldvs
, newvs
, calcvs
);
4587 print_iostat_default(calcvs
, cb
, scale
);
4589 if (cb
->cb_flags
& IOS_LATENCY_M
)
4590 print_iostat_latency(cb
, oldnv
, newnv
);
4591 if (cb
->cb_flags
& IOS_QUEUES_M
)
4592 print_iostat_queues(cb
, oldnv
, newnv
);
4593 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4595 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4598 if (cb
->vcdl
!= NULL
) {
4600 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4603 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4607 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4616 if (!cb
->cb_verbose
)
4619 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4620 &newchild
, &children
) != 0)
4624 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4625 &oldchild
, &oldchildren
) != 0)
4628 children
= MIN(oldchildren
, children
);
4632 * print normal top-level devices
4634 for (c
= 0; c
< children
; c
++) {
4635 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4637 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4640 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4643 if (ishole
|| islog
)
4646 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4649 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4651 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4652 newchild
[c
], cb
, depth
+ 2);
4657 * print all other top-level devices
4659 for (uint_t n
= 0; n
< 3; n
++) {
4660 boolean_t printed
= B_FALSE
;
4662 for (c
= 0; c
< children
; c
++) {
4663 uint64_t islog
= B_FALSE
;
4667 (void) nvlist_lookup_uint64(newchild
[c
],
4668 ZPOOL_CONFIG_IS_LOG
, &islog
);
4670 bias
= VDEV_ALLOC_CLASS_LOGS
;
4672 (void) nvlist_lookup_string(newchild
[c
],
4673 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4674 (void) nvlist_lookup_string(newchild
[c
],
4675 ZPOOL_CONFIG_TYPE
, &type
);
4677 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4679 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4683 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4684 !cb
->cb_scripted
&& !cb
->cb_vdev_names
) {
4685 print_iostat_dashes(cb
, 0,
4692 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4694 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4695 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4701 * Include level 2 ARC devices in iostat output
4703 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4704 &newchild
, &children
) != 0)
4708 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4709 &oldchild
, &oldchildren
) != 0)
4712 children
= MIN(oldchildren
, children
);
4716 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4717 !cb
->cb_vdev_names
) {
4718 print_iostat_dashes(cb
, 0, "cache");
4722 for (c
= 0; c
< children
; c
++) {
4723 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4725 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4726 : NULL
, newchild
[c
], cb
, depth
+ 2);
4735 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4737 iostat_cbdata_t
*cb
= data
;
4741 * If the pool has disappeared, remove it from the list and continue.
4743 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4747 pool_list_remove(cb
->cb_list
, zhp
);
4753 * Callback to print out the iostats for the given pool.
4756 print_iostat(zpool_handle_t
*zhp
, void *data
)
4758 iostat_cbdata_t
*cb
= data
;
4759 nvlist_t
*oldconfig
, *newconfig
;
4760 nvlist_t
*oldnvroot
, *newnvroot
;
4763 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4765 if (cb
->cb_iteration
== 1)
4768 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4771 if (oldconfig
== NULL
)
4774 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4777 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4779 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4780 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4781 print_iostat_separator(cb
);
4782 if (cb
->vcdl
!= NULL
) {
4783 print_cmd_columns(cb
->vcdl
, 1);
4798 if (isatty(STDOUT_FILENO
)) {
4799 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4801 columns
= ws
.ws_col
;
4810 * Return the required length of the pool/vdev name column. The minimum
4811 * allowed width and output formatting flags must be provided.
4814 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
4816 nvlist_t
*config
, *nvroot
;
4817 int width
= min_width
;
4819 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4820 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4822 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4823 if (verbose
== B_FALSE
) {
4824 width
= MAX(poolname_len
, min_width
);
4826 width
= MAX(poolname_len
,
4827 max_width(zhp
, nvroot
, 0, min_width
, flags
));
4835 * Parse the input string, get the 'interval' and 'count' value if there is one.
4838 get_interval_count(int *argcp
, char **argv
, float *iv
,
4842 unsigned long count
= 0;
4846 * Determine if the last argument is an integer or a pool name
4848 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4852 interval
= strtof(argv
[argc
- 1], &end
);
4854 if (*end
== '\0' && errno
== 0) {
4855 if (interval
== 0) {
4856 (void) fprintf(stderr
, gettext("interval "
4857 "cannot be zero\n"));
4861 * Ignore the last parameter
4866 * If this is not a valid number, just plow on. The
4867 * user will get a more informative error message later
4875 * If the last argument is also an integer, then we have both a count
4878 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4883 interval
= strtof(argv
[argc
- 1], &end
);
4885 if (*end
== '\0' && errno
== 0) {
4886 if (interval
== 0) {
4887 (void) fprintf(stderr
, gettext("interval "
4888 "cannot be zero\n"));
4893 * Ignore the last parameter
4907 get_timestamp_arg(char c
)
4910 timestamp_fmt
= UDATE
;
4912 timestamp_fmt
= DDATE
;
4918 * Return stat flags that are supported by all pools by both the module and
4919 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4920 * It will get ANDed down until only the flags that are supported on all pools
4924 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4926 uint64_t *mask
= data
;
4927 nvlist_t
*config
, *nvroot
, *nvx
;
4931 config
= zpool_get_config(zhp
, NULL
);
4932 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4935 /* Default stats are always supported, but for completeness.. */
4936 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4937 flags
|= IOS_DEFAULT_M
;
4939 /* Get our extended stats nvlist from the main list */
4940 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4943 * No extended stats; they're probably running an older
4944 * module. No big deal, we support that too.
4949 /* For each extended stat, make sure all its nvpairs are supported */
4950 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
4951 if (!vsx_type_to_nvlist
[j
][0])
4954 /* Start off by assuming the flag is supported, then check */
4955 flags
|= (1ULL << j
);
4956 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
4957 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
4958 /* flag isn't supported */
4959 flags
= flags
& ~(1ULL << j
);
4965 *mask
= *mask
& flags
;
4970 * Return a bitmask of stats that are supported on all pools by both the module
4974 get_stat_flags(zpool_list_t
*list
)
4979 * get_stat_flags_cb() will lop off bits from "mask" until only the
4980 * flags that are supported on all pools remain.
4982 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
4987 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
4990 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
4992 iostat_cbdata_t
*cb
= cb_data
;
4996 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
4998 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
4999 ret
= 1; /* match */
5006 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
5009 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5011 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5015 * Check if vdevs are in a pool
5017 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5018 * return 0. If pool_name is NULL, then search all pools.
5021 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5022 iostat_cbdata_t
*cb
)
5029 if ((argc
== 0) || !*argv
)
5035 /* Temporarily hijack cb_vdev_names for a second... */
5036 tmp_name
= cb
->cb_vdev_names
;
5038 /* Go though our list of prospective vdev names */
5039 for (i
= 0; i
< argc
; i
++) {
5040 cb
->cb_vdev_names
= argv
+ i
;
5042 /* Is this name a vdev in our pools? */
5043 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5044 B_FALSE
, is_vdev
, cb
);
5051 cb
->cb_vdev_names
= tmp_name
;
5057 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5060 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5067 * Do we have a pool named *name? If so, return 1, otherwise 0.
5072 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, B_FALSE
, is_pool_cb
,
5076 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5078 are_all_pools(int argc
, char **argv
)
5080 if ((argc
== 0) || !*argv
)
5084 if (!is_pool(argv
[argc
]))
5091 * Helper function to print out vdev/pool names we can't resolve. Used for an
5095 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5096 iostat_cbdata_t
*cb
)
5101 for (i
= 0; i
< argc
; i
++) {
5105 str
= gettext("pool");
5106 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5107 str
= gettext("vdev in this pool");
5108 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5109 str
= gettext("vdev in another pool");
5111 str
= gettext("unknown");
5113 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5118 * Same as get_interval_count(), but with additional checks to not misinterpret
5119 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5123 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5124 unsigned long *count
, iostat_cbdata_t
*cb
)
5126 char **tmpargv
= argv
;
5127 int argc_for_interval
= 0;
5129 /* Is the last arg an interval value? Or a guid? */
5130 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
5132 * The last arg is not a guid, so it's probably an
5135 argc_for_interval
++;
5138 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
5140 * The 2nd to last arg is not a guid, so it's probably
5141 * an interval value.
5143 argc_for_interval
++;
5147 /* Point to our list of possible intervals */
5148 tmpargv
= &argv
[*argc
- argc_for_interval
];
5150 *argc
= *argc
- argc_for_interval
;
5151 get_interval_count(&argc_for_interval
, tmpargv
,
5156 * Floating point sleep(). Allows you to pass in a floating point value for
5162 struct timespec req
;
5163 req
.tv_sec
= floor(sec
);
5164 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5165 nanosleep(&req
, NULL
);
5169 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5170 * if we were unable to determine its size.
5173 terminal_height(void)
5177 if (isatty(STDOUT_FILENO
) == 0)
5180 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5181 return (win
.ws_row
);
5187 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5190 * name: Short name of the script ('iostat').
5191 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5194 print_zpool_script_help(char *name
, char *path
)
5196 char *argv
[] = {path
, "-h", NULL
};
5197 char **lines
= NULL
;
5201 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5203 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5205 libzfs_free_str_array(lines
, lines_cnt
);
5209 for (int i
= 0; i
< lines_cnt
; i
++)
5210 if (!is_blank_str(lines
[i
]))
5211 printf(" %-14s %s\n", name
, lines
[i
]);
5213 libzfs_free_str_array(lines
, lines_cnt
);
5217 * Go though the zpool status/iostat -c scripts in the user's path, run their
5218 * help option (-h), and print out the results.
5221 print_zpool_dir_scripts(char *dirpath
)
5225 char fullpath
[MAXPATHLEN
];
5226 struct stat dir_stat
;
5228 if ((dir
= opendir(dirpath
)) != NULL
) {
5229 /* print all the files and directories within directory */
5230 while ((ent
= readdir(dir
)) != NULL
) {
5231 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5233 /* Print the scripts */
5234 if (stat(fullpath
, &dir_stat
) == 0)
5235 if (dir_stat
.st_mode
& S_IXUSR
&&
5236 S_ISREG(dir_stat
.st_mode
))
5237 print_zpool_script_help(ent
->d_name
,
5245 * Print out help text for all zpool status/iostat -c scripts.
5248 print_zpool_script_list(char *subcommand
)
5252 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5254 sp
= zpool_get_cmd_search_path();
5258 dir
= strtok(sp
, ":");
5259 while (dir
!= NULL
) {
5260 print_zpool_dir_scripts(dir
);
5261 dir
= strtok(NULL
, ":");
5268 * Set the minimum pool/vdev name column width. The width must be at least 10,
5269 * but may be as large as the column width - 42 so it still fits on one line.
5270 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5273 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5275 iostat_cbdata_t
*cb
= data
;
5276 int width
, available_width
;
5279 * get_namewidth() returns the maximum width of any name in that column
5280 * for any pool/vdev/device line that will be output.
5282 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
5286 * The width we are calculating is the width of the header and also the
5287 * padding width for names that are less than maximum width. The stats
5288 * take up 42 characters, so the width available for names is:
5290 available_width
= get_columns() - 42;
5293 * If the maximum width fits on a screen, then great! Make everything
5294 * line up by justifying all lines to the same width. If that max
5295 * width is larger than what's available, the name plus stats won't fit
5296 * on one line, and justifying to that width would cause every line to
5297 * wrap on the screen. We only want lines with long names to wrap.
5298 * Limit the padding to what won't wrap.
5300 if (width
> available_width
)
5301 width
= available_width
;
5304 * And regardless of whatever the screen width is (get_columns can
5305 * return 0 if the width is not known or less than 42 for a narrow
5306 * terminal) have the width be a minimum of 10.
5311 /* Save the calculated width */
5312 cb
->cb_namewidth
= width
;
5318 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5319 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5320 * [interval [count]]
5322 * -c CMD For each vdev, run command CMD
5323 * -g Display guid for individual vdev name.
5324 * -L Follow links when resolving vdev path name.
5325 * -P Display full path for vdev name.
5326 * -v Display statistics for individual vdevs
5328 * -p Display values in parsable (exact) format.
5329 * -H Scripted mode. Don't display headers, and separate properties
5331 * -l Display average latency
5332 * -q Display queue depths
5333 * -w Display latency histograms
5334 * -r Display request size histogram
5335 * -T Display a timestamp in date(1) or Unix format
5336 * -n Only print headers once
5338 * This command can be tricky because we want to be able to deal with pool
5339 * creation/destruction as well as vdev configuration changes. The bulk of this
5340 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5341 * on pool_list_update() to detect the addition of new pools. Configuration
5342 * changes are all handled within libzfs.
5345 zpool_do_iostat(int argc
, char **argv
)
5351 unsigned long count
= 0;
5354 boolean_t verbose
= B_FALSE
;
5355 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5356 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5357 boolean_t omit_since_boot
= B_FALSE
;
5358 boolean_t guid
= B_FALSE
;
5359 boolean_t follow_links
= B_FALSE
;
5360 boolean_t full_name
= B_FALSE
;
5361 boolean_t headers_once
= B_FALSE
;
5362 iostat_cbdata_t cb
= { 0 };
5365 /* Used for printing error message */
5366 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5367 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5369 uint64_t unsupported_flags
;
5372 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5377 gettext("Can't set -c flag twice\n"));
5381 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5382 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5383 fprintf(stderr
, gettext(
5384 "Can't run -c, disabled by "
5385 "ZPOOL_SCRIPTS_ENABLED.\n"));
5389 if ((getuid() <= 0 || geteuid() <= 0) &&
5390 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5391 fprintf(stderr
, gettext(
5392 "Can't run -c with root privileges "
5393 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5403 follow_links
= B_TRUE
;
5409 get_timestamp_arg(*optarg
);
5433 omit_since_boot
= B_TRUE
;
5436 headers_once
= B_TRUE
;
5442 if (optopt
== 'c') {
5443 print_zpool_script_list("iostat");
5447 gettext("invalid option '%c'\n"), optopt
);
5456 cb
.cb_literal
= parsable
;
5457 cb
.cb_scripted
= scripted
;
5460 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5462 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5464 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5465 cb
.cb_iteration
= 0;
5466 cb
.cb_namewidth
= 0;
5467 cb
.cb_verbose
= verbose
;
5469 /* Get our interval and count values (if any) */
5471 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5474 get_interval_count(&argc
, argv
, &interval
, &count
);
5478 /* No args, so just print the defaults. */
5479 } else if (are_all_pools(argc
, argv
)) {
5480 /* All the args are pool names */
5481 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
5482 /* All the args are vdevs */
5483 cb
.cb_vdev_names
= argv
;
5484 cb
.cb_vdev_names_count
= argc
;
5485 argc
= 0; /* No pools to process */
5486 } else if (are_all_pools(1, argv
)) {
5487 /* The first arg is a pool name */
5488 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
5489 /* ...and the rest are vdev names */
5490 cb
.cb_vdev_names
= argv
+ 1;
5491 cb
.cb_vdev_names_count
= argc
- 1;
5492 argc
= 1; /* One pool to process */
5494 fprintf(stderr
, gettext("Expected either a list of "));
5495 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5496 fprintf(stderr
, " \"%s\", ", argv
[0]);
5497 fprintf(stderr
, gettext("but got:\n"));
5498 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5500 fprintf(stderr
, "\n");
5506 * The args don't make sense. The first arg isn't a pool name,
5507 * nor are all the args vdevs.
5509 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5510 fprintf(stderr
, "\n");
5514 if (cb
.cb_vdev_names_count
!= 0) {
5516 * If user specified vdevs, it implies verbose.
5518 cb
.cb_verbose
= B_TRUE
;
5522 * Construct the list of all interesting pools.
5525 if ((list
= pool_list_get(argc
, argv
, NULL
, parsable
, &ret
)) == NULL
)
5528 if (pool_list_count(list
) == 0 && argc
!= 0) {
5529 pool_list_free(list
);
5533 if (pool_list_count(list
) == 0 && interval
== 0) {
5534 pool_list_free(list
);
5535 (void) fprintf(stderr
, gettext("no pools available\n"));
5539 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5540 pool_list_free(list
);
5541 (void) fprintf(stderr
,
5542 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5547 if (l_histo
&& rq_histo
) {
5548 pool_list_free(list
);
5549 (void) fprintf(stderr
,
5550 gettext("Only one of [-r|-w] can be passed at a time\n"));
5556 * Enter the main iostat loop.
5562 * Histograms tables look out of place when you try to display
5563 * them with the other stats, so make a rule that you can only
5564 * print histograms by themselves.
5566 cb
.cb_flags
= IOS_L_HISTO_M
;
5567 } else if (rq_histo
) {
5568 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5570 cb
.cb_flags
= IOS_DEFAULT_M
;
5572 cb
.cb_flags
|= IOS_LATENCY_M
;
5574 cb
.cb_flags
|= IOS_QUEUES_M
;
5578 * See if the module supports all the stats we want to display.
5580 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5581 if (unsupported_flags
) {
5585 gettext("The loaded zfs module doesn't support:"));
5587 /* for each bit set in unsupported_flags */
5588 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5589 idx
= lowbit64(f
) - 1;
5590 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5593 fprintf(stderr
, ". Try running a newer module.\n");
5594 pool_list_free(list
);
5600 if ((npools
= pool_list_count(list
)) == 0)
5601 (void) fprintf(stderr
, gettext("no pools available\n"));
5604 * If this is the first iteration and -y was supplied
5605 * we skip any printing.
5607 boolean_t skip
= (omit_since_boot
&&
5608 cb
.cb_iteration
== 0);
5611 * Refresh all statistics. This is done as an
5612 * explicit step before calculating the maximum name
5613 * width, so that any * configuration changes are
5614 * properly accounted for.
5616 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5620 * Iterate over all pools to determine the maximum width
5621 * for the pool / device name column across all pools.
5623 cb
.cb_namewidth
= 0;
5624 (void) pool_list_iter(list
, B_FALSE
,
5625 get_namewidth_iostat
, &cb
);
5627 if (timestamp_fmt
!= NODATE
)
5628 print_timestamp(timestamp_fmt
);
5630 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5631 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5632 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5633 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
5634 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
5641 * Check terminal size so we can print headers
5642 * even when terminal window has its height
5645 winheight
= terminal_height();
5647 * Are we connected to TTY? If not, headers_once
5648 * should be true, to avoid breaking scripts.
5651 headers_once
= B_TRUE
;
5654 * If it's the first time and we're not skipping it,
5655 * or either skip or verbose mode, print the header.
5657 * The histogram code explicitly prints its header on
5658 * every vdev, so skip this for histograms.
5660 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5661 (skip
!= verbose
) ||
5663 (cb
.cb_iteration
% winheight
) == 0)) &&
5664 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5666 print_iostat_header(&cb
);
5669 (void) fsleep(interval
);
5673 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5676 * If there's more than one pool, and we're not in
5677 * verbose mode (which prints a separator for us),
5678 * then print a separator.
5680 * In addition, if we're printing specific vdevs then
5681 * we also want an ending separator.
5683 if (((npools
> 1 && !verbose
&&
5684 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5685 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5686 cb
.cb_vdev_names_count
)) &&
5688 print_iostat_separator(&cb
);
5689 if (cb
.vcdl
!= NULL
)
5690 print_cmd_columns(cb
.vcdl
, 1);
5694 if (cb
.vcdl
!= NULL
)
5695 free_vdev_cmd_data_list(cb
.vcdl
);
5700 * Flush the output so that redirection to a file isn't buffered
5703 (void) fflush(stdout
);
5708 if (count
!= 0 && --count
== 0)
5711 (void) fsleep(interval
);
5714 pool_list_free(list
);
5719 typedef struct list_cbdata
{
5720 boolean_t cb_verbose
;
5723 boolean_t cb_scripted
;
5724 zprop_list_t
*cb_proplist
;
5725 boolean_t cb_literal
;
5730 * Given a list of columns to display, output appropriate headers for each one.
5733 print_header(list_cbdata_t
*cb
)
5735 zprop_list_t
*pl
= cb
->cb_proplist
;
5736 char headerbuf
[ZPOOL_MAXPROPLEN
];
5738 boolean_t first
= B_TRUE
;
5739 boolean_t right_justify
;
5742 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5743 width
= pl
->pl_width
;
5744 if (first
&& cb
->cb_verbose
) {
5746 * Reset the width to accommodate the verbose listing
5749 width
= cb
->cb_namewidth
;
5757 right_justify
= B_FALSE
;
5758 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5759 header
= zpool_prop_column_name(pl
->pl_prop
);
5760 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5764 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5765 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5766 headerbuf
[i
] = '\0';
5770 if (pl
->pl_next
== NULL
&& !right_justify
)
5771 (void) printf("%s", header
);
5772 else if (right_justify
)
5773 (void) printf("%*s", (int)width
, header
);
5775 (void) printf("%-*s", (int)width
, header
);
5778 (void) printf("\n");
5782 * Given a pool and a list of properties, print out all the properties according
5783 * to the described layout. Used by zpool_do_list().
5786 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5788 zprop_list_t
*pl
= cb
->cb_proplist
;
5789 boolean_t first
= B_TRUE
;
5790 char property
[ZPOOL_MAXPROPLEN
];
5792 boolean_t right_justify
;
5795 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5797 width
= pl
->pl_width
;
5798 if (first
&& cb
->cb_verbose
) {
5800 * Reset the width to accommodate the verbose listing
5803 width
= cb
->cb_namewidth
;
5807 if (cb
->cb_scripted
)
5808 (void) printf("\t");
5815 right_justify
= B_FALSE
;
5816 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5817 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5818 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5823 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5824 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5825 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5826 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5827 sizeof (property
)) == 0) {
5835 * If this is being called in scripted mode, or if this is the
5836 * last column and it is left-justified, don't include a width
5839 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5840 (void) printf("%s", propstr
);
5841 else if (right_justify
)
5842 (void) printf("%*s", (int)width
, propstr
);
5844 (void) printf("%-*s", (int)width
, propstr
);
5847 (void) printf("\n");
5851 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
5852 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
5856 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
5859 case ZPOOL_PROP_EXPANDSZ
:
5860 case ZPOOL_PROP_CHECKPOINT
:
5861 case ZPOOL_PROP_DEDUPRATIO
:
5863 (void) strlcpy(propval
, "-", sizeof (propval
));
5865 zfs_nicenum_format(value
, propval
, sizeof (propval
),
5868 case ZPOOL_PROP_FRAGMENTATION
:
5869 if (value
== ZFS_FRAG_INVALID
) {
5870 (void) strlcpy(propval
, "-", sizeof (propval
));
5871 } else if (format
== ZFS_NICENUM_RAW
) {
5872 (void) snprintf(propval
, sizeof (propval
), "%llu",
5873 (unsigned long long)value
);
5875 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5876 (unsigned long long)value
);
5879 case ZPOOL_PROP_CAPACITY
:
5880 /* capacity value is in parts-per-10,000 (aka permyriad) */
5881 if (format
== ZFS_NICENUM_RAW
)
5882 (void) snprintf(propval
, sizeof (propval
), "%llu",
5883 (unsigned long long)value
/ 100);
5885 (void) snprintf(propval
, sizeof (propval
),
5886 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
5887 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
5889 case ZPOOL_PROP_HEALTH
:
5891 snprintf(propval
, sizeof (propval
), "%-*s", (int)width
, str
);
5894 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5898 (void) strlcpy(propval
, "-", sizeof (propval
));
5901 (void) printf("\t%s", propval
);
5903 (void) printf(" %*s", (int)width
, propval
);
5907 * print static default line per vdev
5908 * not compatible with '-o' <proplist> option
5911 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5912 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
5918 boolean_t scripted
= cb
->cb_scripted
;
5919 uint64_t islog
= B_FALSE
;
5920 char *dashes
= "%-*s - - - - "
5923 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5924 (uint64_t **)&vs
, &c
) == 0);
5927 boolean_t toplevel
= (vs
->vs_space
!= 0);
5929 enum zfs_nicenum_format format
;
5933 format
= ZFS_NICENUM_RAW
;
5935 format
= ZFS_NICENUM_1024
;
5937 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5941 (void) printf("\t%s", name
);
5942 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5943 (void) printf("%*s%s", depth
, "", name
);
5945 (void) printf("%*s%s%*s", depth
, "", name
,
5946 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5949 * Print the properties for the individual vdevs. Some
5950 * properties are only applicable to toplevel vdevs. The
5951 * 'toplevel' boolean value is passed to the print_one_column()
5952 * to indicate that the value is valid.
5954 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
5956 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
5957 scripted
, toplevel
, format
);
5958 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
5959 NULL
, scripted
, toplevel
, format
);
5960 print_one_column(ZPOOL_PROP_CHECKPOINT
,
5961 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
5962 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
5963 scripted
, B_TRUE
, format
);
5964 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
5965 vs
->vs_fragmentation
, NULL
, scripted
,
5966 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
5968 cap
= (vs
->vs_space
== 0) ? 0 :
5969 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
5970 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
5971 scripted
, toplevel
, format
);
5972 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
5973 scripted
, toplevel
, format
);
5974 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
5976 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
5978 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
5981 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
5983 (void) printf("\n");
5986 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
5987 &child
, &children
) != 0)
5990 /* list the normal vdevs first */
5991 for (c
= 0; c
< children
; c
++) {
5992 uint64_t ishole
= B_FALSE
;
5994 if (nvlist_lookup_uint64(child
[c
],
5995 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
5998 if (nvlist_lookup_uint64(child
[c
],
5999 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6002 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6005 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6007 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6011 /* list the classes: 'logs', 'dedup', and 'special' */
6012 for (uint_t n
= 0; n
< 3; n
++) {
6013 boolean_t printed
= B_FALSE
;
6015 for (c
= 0; c
< children
; c
++) {
6019 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6020 &islog
) == 0 && islog
) {
6021 bias
= VDEV_ALLOC_CLASS_LOGS
;
6023 (void) nvlist_lookup_string(child
[c
],
6024 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6025 (void) nvlist_lookup_string(child
[c
],
6026 ZPOOL_CONFIG_TYPE
, &type
);
6028 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6030 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6034 /* LINTED E_SEC_PRINTF_VAR_FMT */
6035 (void) printf(dashes
, cb
->cb_namewidth
,
6039 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6041 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6047 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6048 &child
, &children
) == 0 && children
> 0) {
6049 /* LINTED E_SEC_PRINTF_VAR_FMT */
6050 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6051 for (c
= 0; c
< children
; c
++) {
6052 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6054 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6060 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6061 &children
) == 0 && children
> 0) {
6062 /* LINTED E_SEC_PRINTF_VAR_FMT */
6063 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6064 for (c
= 0; c
< children
; c
++) {
6065 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6067 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6075 * Generic callback function to list a pool.
6078 list_callback(zpool_handle_t
*zhp
, void *data
)
6080 list_cbdata_t
*cbp
= data
;
6082 print_pool(zhp
, cbp
);
6084 if (cbp
->cb_verbose
) {
6085 nvlist_t
*config
, *nvroot
;
6087 config
= zpool_get_config(zhp
, NULL
);
6088 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6090 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6097 * Set the minimum pool/vdev name column width. The width must be at least 9,
6098 * but may be as large as needed.
6101 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6103 list_cbdata_t
*cb
= data
;
6106 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6112 cb
->cb_namewidth
= width
;
6118 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6120 * -g Display guid for individual vdev name.
6121 * -H Scripted mode. Don't display headers, and separate properties
6123 * -L Follow links when resolving vdev path name.
6124 * -o List of properties to display. Defaults to
6125 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6126 * "dedupratio,health,altroot"
6127 * -p Display values in parsable (exact) format.
6128 * -P Display full path for vdev name.
6129 * -T Display a timestamp in date(1) or Unix format
6131 * List all pools in the system, whether or not they're healthy. Output space
6132 * statistics for each one, as well as health status summary.
6135 zpool_do_list(int argc
, char **argv
)
6139 list_cbdata_t cb
= { 0 };
6140 static char default_props
[] =
6141 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6142 "capacity,dedupratio,health,altroot";
6143 char *props
= default_props
;
6145 unsigned long count
= 0;
6147 boolean_t first
= B_TRUE
;
6150 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6153 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6156 cb
.cb_scripted
= B_TRUE
;
6159 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6165 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6168 cb
.cb_literal
= B_TRUE
;
6171 get_timestamp_arg(*optarg
);
6174 cb
.cb_verbose
= B_TRUE
;
6175 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6178 (void) fprintf(stderr
, gettext("missing argument for "
6179 "'%c' option\n"), optopt
);
6183 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6192 get_interval_count(&argc
, argv
, &interval
, &count
);
6194 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6198 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6199 cb
.cb_literal
, &ret
)) == NULL
)
6202 if (pool_list_count(list
) == 0)
6205 cb
.cb_namewidth
= 0;
6206 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6208 if (timestamp_fmt
!= NODATE
)
6209 print_timestamp(timestamp_fmt
);
6211 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6215 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6220 if (count
!= 0 && --count
== 0)
6223 pool_list_free(list
);
6224 (void) fsleep(interval
);
6227 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6228 (void) printf(gettext("no pools available\n"));
6232 pool_list_free(list
);
6233 zprop_free_list(cb
.cb_proplist
);
6238 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6240 boolean_t force
= B_FALSE
;
6241 boolean_t rebuild
= B_FALSE
;
6242 boolean_t wait
= B_FALSE
;
6245 char *poolname
, *old_disk
, *new_disk
;
6246 zpool_handle_t
*zhp
;
6247 nvlist_t
*props
= NULL
;
6252 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6258 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6259 (void) fprintf(stderr
, gettext("missing "
6260 "'=' for -o option\n"));
6266 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6267 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6277 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6286 /* get pool name and check number of arguments */
6288 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6295 (void) fprintf(stderr
,
6296 gettext("missing <device> specification\n"));
6304 (void) fprintf(stderr
,
6305 gettext("missing <new_device> specification\n"));
6308 new_disk
= old_disk
;
6318 (void) fprintf(stderr
, gettext("too many arguments\n"));
6322 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6327 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6328 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6335 /* unless manually specified use "ashift" pool property (if set) */
6336 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6339 char strval
[ZPOOL_MAXPROPLEN
];
6341 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6342 if (src
!= ZPROP_SRC_DEFAULT
) {
6343 (void) sprintf(strval
, "%" PRId32
, intval
);
6344 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6345 &props
, B_TRUE
) == 0);
6349 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6351 if (nvroot
== NULL
) {
6357 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6360 if (ret
== 0 && wait
)
6361 ret
= zpool_wait(zhp
,
6362 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6365 nvlist_free(nvroot
);
6372 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6374 * -f Force attach, even if <new_device> appears to be in use.
6375 * -s Use sequential instead of healing reconstruction for resilver.
6376 * -o Set property=value.
6377 * -w Wait for replacing to complete before returning
6379 * Replace <device> with <new_device>.
6383 zpool_do_replace(int argc
, char **argv
)
6385 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6389 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6391 * -f Force attach, even if <new_device> appears to be in use.
6392 * -s Use sequential instead of healing reconstruction for resilver.
6393 * -o Set property=value.
6394 * -w Wait for resilvering to complete before returning
6396 * Attach <new_device> to the mirror containing <device>. If <device> is not
6397 * part of a mirror, then <device> will be transformed into a mirror of
6398 * <device> and <new_device>. In either case, <new_device> will begin life
6399 * with a DTL of [0, now], and will immediately begin to resilver itself.
6402 zpool_do_attach(int argc
, char **argv
)
6404 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6408 * zpool detach [-f] <pool> <device>
6410 * -f Force detach of <device>, even if DTLs argue against it
6411 * (not supported yet)
6413 * Detach a device from a mirror. The operation will be refused if <device>
6414 * is the last device in the mirror, or if the DTLs indicate that this device
6415 * has the only valid copy of some data.
6419 zpool_do_detach(int argc
, char **argv
)
6422 char *poolname
, *path
;
6423 zpool_handle_t
*zhp
;
6427 while ((c
= getopt(argc
, argv
, "")) != -1) {
6430 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6439 /* get pool name and check number of arguments */
6441 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6446 (void) fprintf(stderr
,
6447 gettext("missing <device> specification\n"));
6454 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6457 ret
= zpool_vdev_detach(zhp
, path
);
6465 * zpool split [-gLnP] [-o prop=val] ...
6467 * [-R altroot] <pool> <newpool> [<device> ...]
6469 * -g Display guid for individual vdev name.
6470 * -L Follow links when resolving vdev path name.
6471 * -n Do not split the pool, but display the resulting layout if
6472 * it were to be split.
6473 * -o Set property=value, or set mount options.
6474 * -P Display full path for vdev name.
6475 * -R Mount the split-off pool under an alternate root.
6476 * -l Load encryption keys while importing.
6478 * Splits the named pool and gives it the new pool name. Devices to be split
6479 * off may be listed, provided that no more than one device is specified
6480 * per top-level vdev mirror. The newly split pool is left in an exported
6481 * state unless -R is specified.
6483 * Restrictions: the top-level of the pool pool must only be made up of
6484 * mirrors; all devices in the pool must be healthy; no device may be
6485 * undergoing a resilvering operation.
6488 zpool_do_split(int argc
, char **argv
)
6490 char *srcpool
, *newpool
, *propval
;
6491 char *mntopts
= NULL
;
6494 boolean_t loadkeys
= B_FALSE
;
6495 zpool_handle_t
*zhp
;
6496 nvlist_t
*config
, *props
= NULL
;
6498 flags
.dryrun
= B_FALSE
;
6499 flags
.import
= B_FALSE
;
6500 flags
.name_flags
= 0;
6503 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6506 flags
.name_flags
|= VDEV_NAME_GUID
;
6509 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6512 flags
.import
= B_TRUE
;
6514 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6515 &props
, B_TRUE
) != 0) {
6524 flags
.dryrun
= B_TRUE
;
6527 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6530 if (add_prop_list(optarg
, propval
,
6531 &props
, B_TRUE
) != 0) {
6540 flags
.name_flags
|= VDEV_NAME_PATH
;
6543 (void) fprintf(stderr
, gettext("missing argument for "
6544 "'%c' option\n"), optopt
);
6548 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6555 if (!flags
.import
&& mntopts
!= NULL
) {
6556 (void) fprintf(stderr
, gettext("setting mntopts is only "
6557 "valid when importing the pool\n"));
6561 if (!flags
.import
&& loadkeys
) {
6562 (void) fprintf(stderr
, gettext("loading keys is only "
6563 "valid when importing the pool\n"));
6571 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6575 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6585 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6590 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6591 if (config
== NULL
) {
6595 (void) printf(gettext("would create '%s' with the "
6596 "following layout:\n\n"), newpool
);
6597 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6599 print_vdev_tree(NULL
, "dedup", config
, 0,
6600 VDEV_ALLOC_BIAS_DEDUP
, 0);
6601 print_vdev_tree(NULL
, "special", config
, 0,
6602 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6608 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6609 nvlist_free(config
);
6615 * The split was successful. Now we need to open the new
6616 * pool and import it.
6618 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6619 nvlist_free(config
);
6625 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6630 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6631 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6633 (void) fprintf(stderr
, gettext("Split was successful, but "
6634 "the datasets could not all be mounted\n"));
6635 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6636 "different altroot\n"), "zpool import");
6639 nvlist_free(config
);
6648 * zpool online <pool> <device> ...
6651 zpool_do_online(int argc
, char **argv
)
6655 zpool_handle_t
*zhp
;
6657 vdev_state_t newstate
;
6661 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6664 flags
|= ZFS_ONLINE_EXPAND
;
6667 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6676 /* get pool name and check number of arguments */
6678 (void) fprintf(stderr
, gettext("missing pool name\n"));
6682 (void) fprintf(stderr
, gettext("missing device name\n"));
6688 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6691 for (i
= 1; i
< argc
; i
++) {
6692 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6693 if (newstate
!= VDEV_STATE_HEALTHY
) {
6694 (void) printf(gettext("warning: device '%s' "
6695 "onlined, but remains in faulted state\n"),
6697 if (newstate
== VDEV_STATE_FAULTED
)
6698 (void) printf(gettext("use 'zpool "
6699 "clear' to restore a faulted "
6702 (void) printf(gettext("use 'zpool "
6703 "replace' to replace devices "
6704 "that are no longer present\n"));
6717 * zpool offline [-ft] <pool> <device> ...
6719 * -f Force the device into a faulted state.
6721 * -t Only take the device off-line temporarily. The offline/faulted
6722 * state will not be persistent across reboots.
6726 zpool_do_offline(int argc
, char **argv
)
6730 zpool_handle_t
*zhp
;
6732 boolean_t istmp
= B_FALSE
;
6733 boolean_t fault
= B_FALSE
;
6736 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6745 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6754 /* get pool name and check number of arguments */
6756 (void) fprintf(stderr
, gettext("missing pool name\n"));
6760 (void) fprintf(stderr
, gettext("missing device name\n"));
6766 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6769 for (i
= 1; i
< argc
; i
++) {
6771 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6773 if (istmp
== B_FALSE
) {
6774 /* Force the fault to persist across imports */
6775 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6777 aux
= VDEV_AUX_EXTERNAL
;
6780 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6783 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
6794 * zpool clear <pool> [device]
6796 * Clear all errors associated with a pool or a particular device.
6799 zpool_do_clear(int argc
, char **argv
)
6803 boolean_t dryrun
= B_FALSE
;
6804 boolean_t do_rewind
= B_FALSE
;
6805 boolean_t xtreme_rewind
= B_FALSE
;
6806 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
6807 nvlist_t
*policy
= NULL
;
6808 zpool_handle_t
*zhp
;
6809 char *pool
, *device
;
6812 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
6821 xtreme_rewind
= B_TRUE
;
6824 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6834 (void) fprintf(stderr
, gettext("missing pool name\n"));
6839 (void) fprintf(stderr
, gettext("too many arguments\n"));
6843 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
6844 (void) fprintf(stderr
,
6845 gettext("-n or -X only meaningful with -F\n"));
6849 rewind_policy
= ZPOOL_TRY_REWIND
;
6851 rewind_policy
= ZPOOL_DO_REWIND
;
6853 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
6855 /* In future, further rewind policy choices can be passed along here */
6856 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
6857 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
6858 rewind_policy
) != 0) {
6863 device
= argc
== 2 ? argv
[1] : NULL
;
6865 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
6866 nvlist_free(policy
);
6870 if (zpool_clear(zhp
, device
, policy
) != 0)
6875 nvlist_free(policy
);
6881 * zpool reguid <pool>
6884 zpool_do_reguid(int argc
, char **argv
)
6888 zpool_handle_t
*zhp
;
6892 while ((c
= getopt(argc
, argv
, "")) != -1) {
6895 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6904 /* get pool name and check number of arguments */
6906 (void) fprintf(stderr
, gettext("missing pool name\n"));
6911 (void) fprintf(stderr
, gettext("too many arguments\n"));
6916 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6919 ret
= zpool_reguid(zhp
);
6927 * zpool reopen <pool>
6929 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6932 zpool_do_reopen(int argc
, char **argv
)
6936 boolean_t scrub_restart
= B_TRUE
;
6939 while ((c
= getopt(argc
, argv
, "n")) != -1) {
6942 scrub_restart
= B_FALSE
;
6945 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6954 /* if argc == 0 we will execute zpool_reopen_one on all pools */
6955 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
, zpool_reopen_one
,
6961 typedef struct scrub_cbdata
{
6963 pool_scrub_cmd_t cb_scrub_cmd
;
6967 zpool_has_checkpoint(zpool_handle_t
*zhp
)
6969 nvlist_t
*config
, *nvroot
;
6971 config
= zpool_get_config(zhp
, NULL
);
6973 if (config
!= NULL
) {
6974 pool_checkpoint_stat_t
*pcs
= NULL
;
6977 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6978 (void) nvlist_lookup_uint64_array(nvroot
,
6979 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
6981 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
6984 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
6985 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
6993 scrub_callback(zpool_handle_t
*zhp
, void *data
)
6995 scrub_cbdata_t
*cb
= data
;
6999 * Ignore faulted pools.
7001 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7002 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7003 "currently unavailable\n"), zpool_get_name(zhp
));
7007 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7009 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7010 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7011 (void) printf(gettext("warning: will not scrub state that "
7012 "belongs to the checkpoint of pool '%s'\n"),
7013 zpool_get_name(zhp
));
7020 wait_callback(zpool_handle_t
*zhp
, void *data
)
7022 zpool_wait_activity_t
*act
= data
;
7023 return (zpool_wait(zhp
, *act
));
7027 * zpool scrub [-s | -p] [-w] <pool> ...
7029 * -s Stop. Stops any in-progress scrub.
7030 * -p Pause. Pause in-progress scrub.
7031 * -w Wait. Blocks until scrub has completed.
7034 zpool_do_scrub(int argc
, char **argv
)
7038 boolean_t wait
= B_FALSE
;
7041 cb
.cb_type
= POOL_SCAN_SCRUB
;
7042 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7045 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7048 cb
.cb_type
= POOL_SCAN_NONE
;
7051 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7057 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7063 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7064 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7065 (void) fprintf(stderr
, gettext("invalid option combination: "
7066 "-s and -p are mutually exclusive\n"));
7070 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7071 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7072 (void) fprintf(stderr
, gettext("invalid option combination: "
7073 "-w cannot be used with -p or -s\n"));
7081 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7085 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7086 scrub_callback
, &cb
);
7088 if (wait
&& !error
) {
7089 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7090 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7091 wait_callback
, &act
);
7098 * zpool resilver <pool> ...
7100 * Restarts any in-progress resilver
7103 zpool_do_resilver(int argc
, char **argv
)
7108 cb
.cb_type
= POOL_SCAN_RESILVER
;
7109 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7112 while ((c
= getopt(argc
, argv
, "")) != -1) {
7115 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7125 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7129 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7130 scrub_callback
, &cb
));
7134 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7136 * -c Cancel. Ends any in-progress trim.
7137 * -d Secure trim. Requires kernel and device support.
7138 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7139 * adding a multiplier suffix such as 'k' or 'm'.
7140 * -s Suspend. TRIM can then be restarted with no flags.
7141 * -w Wait. Blocks until trimming has completed.
7144 zpool_do_trim(int argc
, char **argv
)
7146 struct option long_options
[] = {
7147 {"cancel", no_argument
, NULL
, 'c'},
7148 {"secure", no_argument
, NULL
, 'd'},
7149 {"rate", required_argument
, NULL
, 'r'},
7150 {"suspend", no_argument
, NULL
, 's'},
7151 {"wait", no_argument
, NULL
, 'w'},
7155 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7157 boolean_t secure
= B_FALSE
;
7158 boolean_t wait
= B_FALSE
;
7161 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7165 if (cmd_type
!= POOL_TRIM_START
&&
7166 cmd_type
!= POOL_TRIM_CANCEL
) {
7167 (void) fprintf(stderr
, gettext("-c cannot be "
7168 "combined with other options\n"));
7171 cmd_type
= POOL_TRIM_CANCEL
;
7174 if (cmd_type
!= POOL_TRIM_START
) {
7175 (void) fprintf(stderr
, gettext("-d cannot be "
7176 "combined with the -c or -s options\n"));
7182 if (cmd_type
!= POOL_TRIM_START
) {
7183 (void) fprintf(stderr
, gettext("-r cannot be "
7184 "combined with the -c or -s options\n"));
7187 if (zfs_nicestrtonum(NULL
, optarg
, &rate
) == -1) {
7188 (void) fprintf(stderr
,
7189 gettext("invalid value for rate\n"));
7194 if (cmd_type
!= POOL_TRIM_START
&&
7195 cmd_type
!= POOL_TRIM_SUSPEND
) {
7196 (void) fprintf(stderr
, gettext("-s cannot be "
7197 "combined with other options\n"));
7200 cmd_type
= POOL_TRIM_SUSPEND
;
7207 (void) fprintf(stderr
,
7208 gettext("invalid option '%c'\n"), optopt
);
7210 (void) fprintf(stderr
,
7211 gettext("invalid option '%s'\n"),
7222 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7227 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7228 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7233 char *poolname
= argv
[0];
7234 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7238 trimflags_t trim_flags
= {
7244 nvlist_t
*vdevs
= fnvlist_alloc();
7246 /* no individual leaf vdevs specified, so add them all */
7247 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7248 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7249 ZPOOL_CONFIG_VDEV_TREE
);
7250 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7251 trim_flags
.fullpool
= B_TRUE
;
7253 trim_flags
.fullpool
= B_FALSE
;
7254 for (int i
= 1; i
< argc
; i
++) {
7255 fnvlist_add_boolean(vdevs
, argv
[i
]);
7259 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7261 fnvlist_free(vdevs
);
7268 * Converts a total number of seconds to a human readable string broken
7269 * down in to days/hours/minutes/seconds.
7272 secs_to_dhms(uint64_t total
, char *buf
)
7274 uint64_t days
= total
/ 60 / 60 / 24;
7275 uint64_t hours
= (total
/ 60 / 60) % 24;
7276 uint64_t mins
= (total
/ 60) % 60;
7277 uint64_t secs
= (total
% 60);
7280 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7281 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7282 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7284 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7285 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7286 (u_longlong_t
)secs
);
7291 * Print out detailed scrub status.
7294 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7296 time_t start
, end
, pause
;
7297 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7298 uint64_t elapsed
, scan_rate
, issue_rate
;
7299 double fraction_done
;
7300 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7301 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7304 printf_color(ANSI_BOLD
, gettext("scan:"));
7307 /* If there's never been a scan, there's not much to say. */
7308 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7309 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7310 (void) printf(gettext("none requested\n"));
7314 start
= ps
->pss_start_time
;
7315 end
= ps
->pss_end_time
;
7316 pause
= ps
->pss_pass_scrub_pause
;
7318 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7320 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7321 ps
->pss_func
== POOL_SCAN_RESILVER
);
7323 /* Scan is finished or canceled. */
7324 if (ps
->pss_state
== DSS_FINISHED
) {
7325 secs_to_dhms(end
- start
, time_buf
);
7327 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7328 (void) printf(gettext("scrub repaired %s "
7329 "in %s with %llu errors on %s"), processed_buf
,
7330 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7332 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7333 (void) printf(gettext("resilvered %s "
7334 "in %s with %llu errors on %s"), processed_buf
,
7335 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7339 } else if (ps
->pss_state
== DSS_CANCELED
) {
7340 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7341 (void) printf(gettext("scrub canceled on %s"),
7343 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7344 (void) printf(gettext("resilver canceled on %s"),
7350 assert(ps
->pss_state
== DSS_SCANNING
);
7352 /* Scan is in progress. Resilvers can't be paused. */
7353 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7355 (void) printf(gettext("scrub in progress since %s"),
7358 (void) printf(gettext("scrub paused since %s"),
7360 (void) printf(gettext("\tscrub started on %s"),
7363 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7364 (void) printf(gettext("resilver in progress since %s"),
7368 scanned
= ps
->pss_examined
;
7369 pass_scanned
= ps
->pss_pass_exam
;
7370 issued
= ps
->pss_issued
;
7371 pass_issued
= ps
->pss_pass_issued
;
7372 total
= ps
->pss_to_examine
;
7374 /* we are only done with a block once we have issued the IO for it */
7375 fraction_done
= (double)issued
/ total
;
7377 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7378 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7379 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7380 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7382 scan_rate
= pass_scanned
/ elapsed
;
7383 issue_rate
= pass_issued
/ elapsed
;
7384 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7385 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7386 secs_to_dhms(total_secs_left
, time_buf
);
7388 /* format all of the numbers we will be reporting */
7389 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7390 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7391 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7392 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7393 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7395 /* do not print estimated time if we have a paused scrub */
7397 (void) printf(gettext("\t%s scanned at %s/s, "
7398 "%s issued at %s/s, %s total\n"),
7399 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7401 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7402 scanned_buf
, issued_buf
, total_buf
);
7405 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7406 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7407 processed_buf
, 100 * fraction_done
);
7408 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7409 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7410 processed_buf
, 100 * fraction_done
);
7414 if (total_secs_left
!= UINT64_MAX
&&
7415 issue_rate
>= 10 * 1024 * 1024) {
7416 (void) printf(gettext(", %s to go\n"), time_buf
);
7418 (void) printf(gettext(", no estimated "
7419 "completion time\n"));
7422 (void) printf(gettext("\n"));
7427 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7429 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7433 printf_color(ANSI_BOLD
, gettext("scan:"));
7436 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7437 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7438 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7439 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7440 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7441 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7442 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7443 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7444 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7445 (bytes_est
+ 1), 100);
7447 /* Format all of the numbers we will be reporting */
7448 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7449 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7450 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7451 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7452 sizeof (bytes_scanned_buf
));
7453 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7454 sizeof (bytes_issued_buf
));
7455 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7456 sizeof (bytes_rebuilt_buf
));
7457 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7458 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7459 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7461 time_t start
= vrs
->vrs_start_time
;
7462 time_t end
= vrs
->vrs_end_time
;
7464 /* Rebuild is finished or canceled. */
7465 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7466 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7467 (void) printf(gettext("resilvered (%s) %s in %s "
7468 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7469 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7471 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7472 (void) printf(gettext("resilver (%s) canceled on %s"),
7473 vdev_name
, ctime(&end
));
7475 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7476 (void) printf(gettext("resilver (%s) in progress since %s"),
7477 vdev_name
, ctime(&start
));
7480 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7482 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7483 MAX(scan_rate
, 1), time_buf
);
7485 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7486 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7487 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7488 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7489 bytes_rebuilt_buf
, scan_pct
);
7491 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7492 if (scan_rate
>= 10 * 1024 * 1024) {
7493 (void) printf(gettext(", %s to go\n"), time_buf
);
7495 (void) printf(gettext(", no estimated "
7496 "completion time\n"));
7499 (void) printf(gettext("\n"));
7504 * Print rebuild status for top-level vdevs.
7507 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7512 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7513 &child
, &children
) != 0)
7516 for (uint_t c
= 0; c
< children
; c
++) {
7517 vdev_rebuild_stat_t
*vrs
;
7520 if (nvlist_lookup_uint64_array(child
[c
],
7521 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7522 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7523 child
[c
], VDEV_NAME_TYPE_ID
);
7524 print_rebuild_status_impl(vrs
, name
);
7531 * As we don't scrub checkpointed blocks, we want to warn the user that we
7532 * skipped scanning some blocks if a checkpoint exists or existed at any
7533 * time during the scan. If a sequential instead of healing reconstruction
7534 * was performed then the blocks were reconstructed. However, their checksums
7535 * have not been verified so we still print the warning.
7538 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7540 if (ps
== NULL
|| pcs
== NULL
)
7543 if (pcs
->pcs_state
== CS_NONE
||
7544 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7547 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7549 if (ps
->pss_state
== DSS_NONE
)
7552 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7553 ps
->pss_end_time
< pcs
->pcs_start_time
)
7556 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7557 (void) printf(gettext(" scan warning: skipped blocks "
7558 "that are only referenced by the checkpoint.\n"));
7560 assert(ps
->pss_state
== DSS_SCANNING
);
7561 (void) printf(gettext(" scan warning: skipping blocks "
7562 "that are only referenced by the checkpoint.\n"));
7567 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7568 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7569 * the last completed (or cancelled) rebuild.
7572 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7576 boolean_t rebuilding
= B_FALSE
;
7577 uint64_t end_time
= 0;
7579 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7580 &child
, &children
) != 0)
7583 for (uint_t c
= 0; c
< children
; c
++) {
7584 vdev_rebuild_stat_t
*vrs
;
7587 if (nvlist_lookup_uint64_array(child
[c
],
7588 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7590 if (vrs
->vrs_end_time
> end_time
)
7591 end_time
= vrs
->vrs_end_time
;
7593 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7594 rebuilding
= B_TRUE
;
7601 if (rebuild_end_time
!= NULL
)
7602 *rebuild_end_time
= end_time
;
7604 return (rebuilding
);
7608 * Print the scan status.
7611 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7613 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7614 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7615 boolean_t active_resilver
= B_FALSE
;
7616 pool_checkpoint_stat_t
*pcs
= NULL
;
7617 pool_scan_stat_t
*ps
= NULL
;
7620 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7621 (uint64_t **)&ps
, &c
) == 0) {
7622 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7623 resilver_end_time
= ps
->pss_end_time
;
7624 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7627 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7628 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7631 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7632 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7634 /* Always print the scrub status when available. */
7636 print_scan_scrub_resilver_status(ps
);
7639 * When there is an active resilver or rebuild print its status.
7640 * Otherwise print the status of the last resilver or rebuild.
7642 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7643 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7644 print_scan_scrub_resilver_status(ps
);
7645 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7646 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7647 print_rebuild_status(zhp
, nvroot
);
7650 (void) nvlist_lookup_uint64_array(nvroot
,
7651 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7652 print_checkpoint_scan_warning(ps
, pcs
);
7656 * Print out detailed removal status.
7659 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7661 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7663 nvlist_t
*config
, *nvroot
;
7668 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7672 * Determine name of vdev.
7674 config
= zpool_get_config(zhp
, NULL
);
7675 nvroot
= fnvlist_lookup_nvlist(config
,
7676 ZPOOL_CONFIG_VDEV_TREE
);
7677 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7678 &child
, &children
) == 0);
7679 assert(prs
->prs_removing_vdev
< children
);
7680 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7681 child
[prs
->prs_removing_vdev
], B_TRUE
);
7683 printf_color(ANSI_BOLD
, gettext("remove: "));
7685 start
= prs
->prs_start_time
;
7686 end
= prs
->prs_end_time
;
7687 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7690 * Removal is finished or canceled.
7692 if (prs
->prs_state
== DSS_FINISHED
) {
7693 uint64_t minutes_taken
= (end
- start
) / 60;
7695 (void) printf(gettext("Removal of vdev %llu copied %s "
7696 "in %lluh%um, completed on %s"),
7697 (longlong_t
)prs
->prs_removing_vdev
,
7699 (u_longlong_t
)(minutes_taken
/ 60),
7700 (uint_t
)(minutes_taken
% 60),
7701 ctime((time_t *)&end
));
7702 } else if (prs
->prs_state
== DSS_CANCELED
) {
7703 (void) printf(gettext("Removal of %s canceled on %s"),
7704 vdev_name
, ctime(&end
));
7706 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7707 double fraction_done
;
7710 assert(prs
->prs_state
== DSS_SCANNING
);
7713 * Removal is in progress.
7715 (void) printf(gettext(
7716 "Evacuation of %s in progress since %s"),
7717 vdev_name
, ctime(&start
));
7719 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7720 total
= prs
->prs_to_copy
;
7721 fraction_done
= (double)copied
/ total
;
7723 /* elapsed time for this pass */
7724 elapsed
= time(NULL
) - prs
->prs_start_time
;
7725 elapsed
= elapsed
> 0 ? elapsed
: 1;
7726 rate
= copied
/ elapsed
;
7727 rate
= rate
> 0 ? rate
: 1;
7728 mins_left
= ((total
- copied
) / rate
) / 60;
7729 hours_left
= mins_left
/ 60;
7731 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7732 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7733 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7736 * do not print estimated time if hours_left is more than
7739 (void) printf(gettext(" %s copied out of %s at %s/s, "
7741 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7742 if (hours_left
< (30 * 24)) {
7743 (void) printf(gettext(", %lluh%um to go\n"),
7744 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7746 (void) printf(gettext(
7747 ", (copy is slow, no estimated time)\n"));
7752 if (prs
->prs_mapping_memory
> 0) {
7754 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7755 (void) printf(gettext(" %s memory used for "
7756 "removed device mappings\n"),
7762 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7767 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7770 (void) printf(gettext("checkpoint: "));
7772 start
= pcs
->pcs_start_time
;
7773 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7775 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7776 char *date
= ctime(&start
);
7779 * ctime() adds a newline at the end of the generated
7780 * string, thus the weird format specifier and the
7781 * strlen() call used to chop it off from the output.
7783 (void) printf(gettext("created %.*s, consumes %s\n"),
7784 (int)(strlen(date
) - 1), date
, space_buf
);
7788 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7790 (void) printf(gettext("discarding, %s remaining.\n"),
7795 print_error_log(zpool_handle_t
*zhp
)
7797 nvlist_t
*nverrlist
= NULL
;
7800 size_t len
= MAXPATHLEN
* 2;
7802 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
7805 (void) printf("errors: Permanent errors have been "
7806 "detected in the following files:\n\n");
7808 pathname
= safe_malloc(len
);
7810 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
7812 uint64_t dsobj
, obj
;
7814 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
7815 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
7817 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
7819 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
7820 (void) printf("%7s %s\n", "", pathname
);
7823 nvlist_free(nverrlist
);
7827 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
7836 (void) printf(gettext("\tspares\n"));
7838 for (i
= 0; i
< nspares
; i
++) {
7839 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
7841 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
7847 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
7856 (void) printf(gettext("\tcache\n"));
7858 for (i
= 0; i
< nl2cache
; i
++) {
7859 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
7861 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
7868 print_dedup_stats(nvlist_t
*config
)
7870 ddt_histogram_t
*ddh
;
7874 char dspace
[6], mspace
[6];
7877 * If the pool was faulted then we may not have been able to
7878 * obtain the config. Otherwise, if we have anything in the dedup
7879 * table continue processing the stats.
7881 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
7882 (uint64_t **)&ddo
, &c
) != 0)
7885 (void) printf("\n");
7886 (void) printf(gettext(" dedup: "));
7887 if (ddo
->ddo_count
== 0) {
7888 (void) printf(gettext("no DDT entries\n"));
7892 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
7893 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
7894 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
7895 (u_longlong_t
)ddo
->ddo_count
,
7899 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
7900 (uint64_t **)&dds
, &c
) == 0);
7901 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
7902 (uint64_t **)&ddh
, &c
) == 0);
7903 zpool_dump_ddt(dds
, ddh
);
7907 * Display a summary of pool status. Displays a summary such as:
7911 * reason: One or more devices ...
7912 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
7918 * When given the '-v' option, we print out the complete config. If the '-e'
7919 * option is specified, then we print out error rate information as well.
7922 status_callback(zpool_handle_t
*zhp
, void *data
)
7924 status_cbdata_t
*cbp
= data
;
7925 nvlist_t
*config
, *nvroot
;
7927 zpool_status_t reason
;
7928 zpool_errata_t errata
;
7933 config
= zpool_get_config(zhp
, NULL
);
7934 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
7939 * If we were given 'zpool status -x', only report those pools with
7942 if (cbp
->cb_explain
&&
7943 (reason
== ZPOOL_STATUS_OK
||
7944 reason
== ZPOOL_STATUS_VERSION_OLDER
||
7945 reason
== ZPOOL_STATUS_FEAT_DISABLED
)) {
7946 if (!cbp
->cb_allpools
) {
7947 (void) printf(gettext("pool '%s' is healthy\n"),
7948 zpool_get_name(zhp
));
7950 cbp
->cb_first
= B_FALSE
;
7956 cbp
->cb_first
= B_FALSE
;
7958 (void) printf("\n");
7960 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7961 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
7962 (uint64_t **)&vs
, &c
) == 0);
7964 health
= zpool_get_state_str(zhp
);
7967 printf_color(ANSI_BOLD
, gettext("pool:"));
7968 printf(" %s\n", zpool_get_name(zhp
));
7970 printf_color(ANSI_BOLD
, gettext("state: "));
7972 printf_color(health_str_to_color(health
), "%s", health
);
7977 case ZPOOL_STATUS_MISSING_DEV_R
:
7978 printf_color(ANSI_BOLD
, gettext("status: "));
7979 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7980 "not be opened. Sufficient replicas exist for\n\tthe pool "
7981 "to continue functioning in a degraded state.\n"));
7982 printf_color(ANSI_BOLD
, gettext("action: "));
7983 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
7984 "and online it using 'zpool online'.\n"));
7987 case ZPOOL_STATUS_MISSING_DEV_NR
:
7988 printf_color(ANSI_BOLD
, gettext("status: "));
7989 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
7990 "not be opened. There are insufficient\n\treplicas for the"
7991 " pool to continue functioning.\n"));
7992 printf_color(ANSI_BOLD
, gettext("action: "));
7993 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
7994 "and online it using 'zpool online'.\n"));
7997 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
7998 printf_color(ANSI_BOLD
, gettext("status: "));
7999 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8000 "not be used because the label is missing or\n\tinvalid. "
8001 "Sufficient replicas exist for the pool to continue\n\t"
8002 "functioning in a degraded state.\n"));
8003 printf_color(ANSI_BOLD
, gettext("action: "));
8004 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8005 "'zpool replace'.\n"));
8008 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8009 printf_color(ANSI_BOLD
, gettext("status: "));
8010 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8011 "not be used because the label is missing \n\tor invalid. "
8012 "There are insufficient replicas for the pool to "
8013 "continue\n\tfunctioning.\n"));
8014 zpool_explain_recover(zpool_get_handle(zhp
),
8015 zpool_get_name(zhp
), reason
, config
);
8018 case ZPOOL_STATUS_FAILING_DEV
:
8019 printf_color(ANSI_BOLD
, gettext("status: "));
8020 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8021 "experienced an unrecoverable error. An\n\tattempt was "
8022 "made to correct the error. Applications are "
8024 printf_color(ANSI_BOLD
, gettext("action: "));
8025 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8026 "device needs to be replaced, and clear the errors\n\tusing"
8027 " 'zpool clear' or replace the device with 'zpool "
8031 case ZPOOL_STATUS_OFFLINE_DEV
:
8032 printf_color(ANSI_BOLD
, gettext("status: "));
8033 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8034 "been taken offline by the administrator.\n\tSufficient "
8035 "replicas exist for the pool to continue functioning in "
8036 "a\n\tdegraded state.\n"));
8037 printf_color(ANSI_BOLD
, gettext("action: "));
8038 printf_color(ANSI_YELLOW
, gettext("Online the device "
8039 "using 'zpool online' or replace the device with\n\t'zpool "
8043 case ZPOOL_STATUS_REMOVED_DEV
:
8044 printf_color(ANSI_BOLD
, gettext("status: "));
8045 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8046 "been removed by the administrator.\n\tSufficient "
8047 "replicas exist for the pool to continue functioning in "
8048 "a\n\tdegraded state.\n"));
8049 printf_color(ANSI_BOLD
, gettext("action: "));
8050 printf_color(ANSI_YELLOW
, gettext("Online the device "
8051 "using zpool online' or replace the device with\n\t'zpool "
8055 case ZPOOL_STATUS_RESILVERING
:
8056 case ZPOOL_STATUS_REBUILDING
:
8057 printf_color(ANSI_BOLD
, gettext("status: "));
8058 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8059 "currently being resilvered. The pool will\n\tcontinue "
8060 "to function, possibly in a degraded state.\n"));
8061 printf_color(ANSI_BOLD
, gettext("action: "));
8062 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8066 case ZPOOL_STATUS_REBUILD_SCRUB
:
8067 printf_color(ANSI_BOLD
, gettext("status: "));
8068 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8069 "been sequentially resilvered, scrubbing\n\tthe pool "
8070 "is recommended.\n"));
8071 printf_color(ANSI_BOLD
, gettext("action: "));
8072 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8073 "verify all data checksums.\n"));
8076 case ZPOOL_STATUS_CORRUPT_DATA
:
8077 printf_color(ANSI_BOLD
, gettext("status: "));
8078 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8079 "experienced an error resulting in data\n\tcorruption. "
8080 "Applications may be affected.\n"));
8081 printf_color(ANSI_BOLD
, gettext("action: "));
8082 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8083 " if possible. Otherwise restore the\n\tentire pool from "
8087 case ZPOOL_STATUS_CORRUPT_POOL
:
8088 printf_color(ANSI_BOLD
, gettext("status: "));
8089 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8090 "corrupted and the pool cannot be opened.\n"));
8091 zpool_explain_recover(zpool_get_handle(zhp
),
8092 zpool_get_name(zhp
), reason
, config
);
8095 case ZPOOL_STATUS_VERSION_OLDER
:
8096 printf_color(ANSI_BOLD
, gettext("status: "));
8097 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8098 "a legacy on-disk format. The pool can\n\tstill be used, "
8099 "but some features are unavailable.\n"));
8100 printf_color(ANSI_BOLD
, gettext("action: "));
8101 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8102 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8103 "longer be accessible on software that does not support\n\t"
8104 "feature flags.\n"));
8107 case ZPOOL_STATUS_VERSION_NEWER
:
8108 printf_color(ANSI_BOLD
, gettext("status: "));
8109 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8110 "to a newer, incompatible on-disk version.\n\tThe pool "
8111 "cannot be accessed on this system.\n"));
8112 printf_color(ANSI_BOLD
, gettext("action: "));
8113 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8114 "system running more recent software, or\n\trestore the "
8115 "pool from backup.\n"));
8118 case ZPOOL_STATUS_FEAT_DISABLED
:
8119 printf_color(ANSI_BOLD
, gettext("status: "));
8120 printf_color(ANSI_YELLOW
, gettext("Some supported features are "
8121 "not enabled on the pool. The pool can\n\tstill be used, "
8122 "but some features are unavailable.\n"));
8123 printf_color(ANSI_BOLD
, gettext("action: "));
8124 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8125 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8126 "longer be accessible by software that does not support\n\t"
8127 "the features. See zpool-features(5) for details.\n"));
8130 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8131 printf_color(ANSI_BOLD
, gettext("status: "));
8132 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8133 "on this system because it uses the\n\tfollowing feature(s)"
8134 " not supported on this system:\n"));
8135 zpool_print_unsup_feat(config
);
8136 (void) printf("\n");
8137 printf_color(ANSI_BOLD
, gettext("action: "));
8138 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8139 "system that supports the required feature(s),\n\tor "
8140 "restore the pool from backup.\n"));
8143 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8144 printf_color(ANSI_BOLD
, gettext("status: "));
8145 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8146 "accessed in read-only mode on this system. It\n\tcannot be"
8147 " accessed in read-write mode because it uses the "
8148 "following\n\tfeature(s) not supported on this system:\n"));
8149 zpool_print_unsup_feat(config
);
8150 (void) printf("\n");
8151 printf_color(ANSI_BOLD
, gettext("action: "));
8152 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8153 "in read-write mode. Import the pool with\n"
8154 "\t\"-o readonly=on\", access the pool from a system that "
8155 "supports the\n\trequired feature(s), or restore the "
8156 "pool from backup.\n"));
8159 case ZPOOL_STATUS_FAULTED_DEV_R
:
8160 printf_color(ANSI_BOLD
, gettext("status: "));
8161 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8162 "faulted in response to persistent errors.\n\tSufficient "
8163 "replicas exist for the pool to continue functioning "
8164 "in a\n\tdegraded state.\n"));
8165 printf_color(ANSI_BOLD
, gettext("action: "));
8166 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8167 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8170 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8171 printf_color(ANSI_BOLD
, gettext("status: "));
8172 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8173 "faulted in response to persistent errors. There are "
8174 "insufficient replicas for the pool to\n\tcontinue "
8176 printf_color(ANSI_BOLD
, gettext("action: "));
8177 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8178 "pool from a backup source. Manually marking the device\n"
8179 "\trepaired using 'zpool clear' may allow some data "
8180 "to be recovered.\n"));
8183 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8184 printf_color(ANSI_BOLD
, gettext("status: "));
8185 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8186 "because multihost writes failed or were delayed;\n\t"
8187 "another system could import the pool undetected.\n"));
8188 printf_color(ANSI_BOLD
, gettext("action: "));
8189 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8190 " are connected, then reboot your system and\n\timport the "
8194 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8195 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8196 printf_color(ANSI_BOLD
, gettext("status: "));
8197 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8198 "faulted in response to IO failures.\n"));
8199 printf_color(ANSI_BOLD
, gettext("action: "));
8200 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8201 "devices are connected, then run 'zpool clear'.\n"));
8204 case ZPOOL_STATUS_BAD_LOG
:
8205 printf_color(ANSI_BOLD
, gettext("status: "));
8206 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8207 "could not be read.\n"
8208 "\tWaiting for administrator intervention to fix the "
8209 "faulted pool.\n"));
8210 printf_color(ANSI_BOLD
, gettext("action: "));
8211 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8212 "device(s) and run 'zpool online',\n"
8213 "\tor ignore the intent log records by running "
8214 "'zpool clear'.\n"));
8217 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8218 (void) printf(gettext("status: One or more devices are "
8219 "configured to use a non-native block size.\n"
8220 "\tExpect reduced performance.\n"));
8221 (void) printf(gettext("action: Replace affected devices with "
8222 "devices that support the\n\tconfigured block size, or "
8223 "migrate data to a properly configured\n\tpool.\n"));
8226 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8227 printf_color(ANSI_BOLD
, gettext("status: "));
8228 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8229 " and system hostid on imported pool.\n\tThis pool was "
8230 "previously imported into a system with a different "
8231 "hostid,\n\tand then was verbatim imported into this "
8233 printf_color(ANSI_BOLD
, gettext("action: "));
8234 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8235 "systems on which it is imported.\n"
8236 "\tThen import it to correct the mismatch.\n"));
8239 case ZPOOL_STATUS_ERRATA
:
8240 printf_color(ANSI_BOLD
, gettext("status: "));
8241 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8245 case ZPOOL_ERRATA_NONE
:
8248 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8249 printf_color(ANSI_BOLD
, gettext("action: "));
8250 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8251 " run 'zpool scrub'.\n"));
8254 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8255 (void) printf(gettext("\tExisting encrypted datasets "
8256 "contain an on-disk incompatibility\n\twhich "
8257 "needs to be corrected.\n"));
8258 printf_color(ANSI_BOLD
, gettext("action: "));
8259 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8260 " backup existing encrypted datasets to new\n\t"
8261 "encrypted datasets and destroy the old ones. "
8262 "'zfs mount -o ro' can\n\tbe used to temporarily "
8263 "mount existing encrypted datasets readonly.\n"));
8266 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8267 (void) printf(gettext("\tExisting encrypted snapshots "
8268 "and bookmarks contain an on-disk\n\tincompat"
8269 "ibility. This may cause on-disk corruption if "
8270 "they are used\n\twith 'zfs recv'.\n"));
8271 printf_color(ANSI_BOLD
, gettext("action: "));
8272 printf_color(ANSI_YELLOW
, gettext("To correct the"
8273 "issue, enable the bookmark_v2 feature. No "
8274 "additional\n\taction is needed if there are no "
8275 "encrypted snapshots or bookmarks.\n\tIf preserving"
8276 "the encrypted snapshots and bookmarks is required,"
8277 " use\n\ta non-raw send to backup and restore them."
8278 " Alternately, they may be\n\tremoved to resolve "
8279 "the incompatibility.\n"));
8284 * All errata which allow the pool to be imported
8285 * must contain an action message.
8293 * The remaining errors can't actually be generated, yet.
8295 assert(reason
== ZPOOL_STATUS_OK
);
8298 if (msgid
!= NULL
) {
8300 printf_color(ANSI_BOLD
, gettext("see:"));
8302 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8306 if (config
!= NULL
) {
8308 nvlist_t
**spares
, **l2cache
;
8309 uint_t nspares
, nl2cache
;
8310 pool_checkpoint_stat_t
*pcs
= NULL
;
8311 pool_removal_stat_t
*prs
= NULL
;
8313 print_scan_status(zhp
, nvroot
);
8315 (void) nvlist_lookup_uint64_array(nvroot
,
8316 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8317 print_removal_status(zhp
, prs
);
8319 (void) nvlist_lookup_uint64_array(nvroot
,
8320 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8321 print_checkpoint_status(pcs
);
8323 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8324 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8325 if (cbp
->cb_namewidth
< 10)
8326 cbp
->cb_namewidth
= 10;
8328 color_start(ANSI_BOLD
);
8329 (void) printf(gettext("config:\n\n"));
8330 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8331 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8335 if (cbp
->cb_print_slow_ios
) {
8336 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8339 if (cbp
->vcdl
!= NULL
)
8340 print_cmd_columns(cbp
->vcdl
, 0);
8344 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8347 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8348 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8349 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8351 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8352 &l2cache
, &nl2cache
) == 0)
8353 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8355 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8356 &spares
, &nspares
) == 0)
8357 print_spares(zhp
, cbp
, spares
, nspares
);
8359 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8361 nvlist_t
*nverrlist
= NULL
;
8364 * If the approximate error count is small, get a
8365 * precise count by fetching the entire log and
8366 * uniquifying the results.
8368 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8369 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8374 while ((elem
= nvlist_next_nvpair(nverrlist
,
8379 nvlist_free(nverrlist
);
8381 (void) printf("\n");
8384 (void) printf(gettext("errors: No known data "
8386 else if (!cbp
->cb_verbose
)
8387 (void) printf(gettext("errors: %llu data "
8388 "errors, use '-v' for a list\n"),
8389 (u_longlong_t
)nerr
);
8391 print_error_log(zhp
);
8394 if (cbp
->cb_dedup_stats
)
8395 print_dedup_stats(config
);
8397 (void) printf(gettext("config: The configuration cannot be "
8405 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8406 * [interval [count]]
8408 * -c CMD For each vdev, run command CMD
8409 * -i Display vdev initialization status.
8410 * -g Display guid for individual vdev name.
8411 * -L Follow links when resolving vdev path name.
8412 * -p Display values in parsable (exact) format.
8413 * -P Display full path for vdev name.
8414 * -s Display slow IOs column.
8415 * -v Display complete error logs
8416 * -x Display only pools with potential problems
8417 * -D Display dedup status (undocumented)
8418 * -t Display vdev TRIM status.
8419 * -T Display a timestamp in date(1) or Unix format
8421 * Describes the health status of all pools or some subset.
8424 zpool_do_status(int argc
, char **argv
)
8429 unsigned long count
= 0;
8430 status_cbdata_t cb
= { 0 };
8434 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8439 gettext("Can't set -c flag twice\n"));
8443 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8444 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8445 fprintf(stderr
, gettext(
8446 "Can't run -c, disabled by "
8447 "ZPOOL_SCRIPTS_ENABLED.\n"));
8451 if ((getuid() <= 0 || geteuid() <= 0) &&
8452 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8453 fprintf(stderr
, gettext(
8454 "Can't run -c with root privileges "
8455 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8461 cb
.cb_print_vdev_init
= B_TRUE
;
8464 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8467 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8470 cb
.cb_literal
= B_TRUE
;
8473 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8476 cb
.cb_print_slow_ios
= B_TRUE
;
8479 cb
.cb_verbose
= B_TRUE
;
8482 cb
.cb_explain
= B_TRUE
;
8485 cb
.cb_dedup_stats
= B_TRUE
;
8488 cb
.cb_print_vdev_trim
= B_TRUE
;
8491 get_timestamp_arg(*optarg
);
8494 if (optopt
== 'c') {
8495 print_zpool_script_list("status");
8499 gettext("invalid option '%c'\n"), optopt
);
8508 get_interval_count(&argc
, argv
, &interval
, &count
);
8511 cb
.cb_allpools
= B_TRUE
;
8513 cb
.cb_first
= B_TRUE
;
8514 cb
.cb_print_status
= B_TRUE
;
8517 if (timestamp_fmt
!= NODATE
)
8518 print_timestamp(timestamp_fmt
);
8521 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8524 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, cb
.cb_literal
,
8525 status_callback
, &cb
);
8527 if (cb
.vcdl
!= NULL
)
8528 free_vdev_cmd_data_list(cb
.vcdl
);
8530 if (argc
== 0 && cb
.cb_count
== 0)
8531 (void) fprintf(stderr
, gettext("no pools available\n"));
8532 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8533 (void) printf(gettext("all pools are healthy\n"));
8541 if (count
!= 0 && --count
== 0)
8544 (void) fsleep(interval
);
8550 typedef struct upgrade_cbdata
{
8553 uint64_t cb_version
;
8558 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8560 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8561 int *count
= (int *)unsupp_fs
;
8563 if (zfs_version
> ZPL_VERSION
) {
8564 (void) printf(gettext("%s (v%d) is not supported by this "
8565 "implementation of ZFS.\n"),
8566 zfs_get_name(zhp
), zfs_version
);
8570 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8578 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8582 uint64_t oldversion
;
8585 config
= zpool_get_config(zhp
, NULL
);
8586 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8589 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8590 assert(oldversion
< version
);
8592 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8597 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8598 "to %d unsupported filesystems (max v%d).\n"),
8599 unsupp_fs
, (int)ZPL_VERSION
);
8603 ret
= zpool_upgrade(zhp
, version
);
8607 if (version
>= SPA_VERSION_FEATURES
) {
8608 (void) printf(gettext("Successfully upgraded "
8609 "'%s' from version %llu to feature flags.\n"),
8610 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8612 (void) printf(gettext("Successfully upgraded "
8613 "'%s' from version %llu to version %llu.\n"),
8614 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8615 (u_longlong_t
)version
);
8622 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8625 boolean_t firstff
= B_TRUE
;
8626 nvlist_t
*enabled
= zpool_get_features(zhp
);
8629 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8630 const char *fname
= spa_feature_table
[i
].fi_uname
;
8631 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8633 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8636 if (!nvlist_exists(enabled
, fguid
)) {
8638 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8639 ret
= zpool_set_prop(zhp
, propname
,
8640 ZFS_FEATURE_ENABLED
);
8648 (void) printf(gettext("Enabled the "
8649 "following features on '%s':\n"),
8650 zpool_get_name(zhp
));
8653 (void) printf(gettext(" %s\n"), fname
);
8664 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8666 upgrade_cbdata_t
*cbp
= arg
;
8669 boolean_t printnl
= B_FALSE
;
8672 config
= zpool_get_config(zhp
, NULL
);
8673 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8676 assert(SPA_VERSION_IS_SUPPORTED(version
));
8678 if (version
< cbp
->cb_version
) {
8679 cbp
->cb_first
= B_FALSE
;
8680 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8686 * If they did "zpool upgrade -a", then we could
8687 * be doing ioctls to different pools. We need
8688 * to log this history once to each pool, and bypass
8689 * the normal history logging that happens in main().
8691 (void) zpool_log_history(g_zfs
, history_str
);
8692 log_history
= B_FALSE
;
8695 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8697 ret
= upgrade_enable_all(zhp
, &count
);
8702 cbp
->cb_first
= B_FALSE
;
8708 (void) printf(gettext("\n"));
8715 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8717 upgrade_cbdata_t
*cbp
= arg
;
8721 config
= zpool_get_config(zhp
, NULL
);
8722 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8725 assert(SPA_VERSION_IS_SUPPORTED(version
));
8727 if (version
< SPA_VERSION_FEATURES
) {
8728 if (cbp
->cb_first
) {
8729 (void) printf(gettext("The following pools are "
8730 "formatted with legacy version numbers and can\n"
8731 "be upgraded to use feature flags. After "
8732 "being upgraded, these pools\nwill no "
8733 "longer be accessible by software that does not "
8734 "support feature\nflags.\n\n"));
8735 (void) printf(gettext("VER POOL\n"));
8736 (void) printf(gettext("--- ------------\n"));
8737 cbp
->cb_first
= B_FALSE
;
8740 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
8741 zpool_get_name(zhp
));
8748 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
8750 upgrade_cbdata_t
*cbp
= arg
;
8754 config
= zpool_get_config(zhp
, NULL
);
8755 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8758 if (version
>= SPA_VERSION_FEATURES
) {
8760 boolean_t poolfirst
= B_TRUE
;
8761 nvlist_t
*enabled
= zpool_get_features(zhp
);
8763 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8764 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8765 const char *fname
= spa_feature_table
[i
].fi_uname
;
8767 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8770 if (!nvlist_exists(enabled
, fguid
)) {
8771 if (cbp
->cb_first
) {
8772 (void) printf(gettext("\nSome "
8773 "supported features are not "
8774 "enabled on the following pools. "
8775 "Once a\nfeature is enabled the "
8776 "pool may become incompatible with "
8777 "software\nthat does not support "
8779 "zpool-features(5) for "
8781 (void) printf(gettext("POOL "
8783 (void) printf(gettext("------"
8785 cbp
->cb_first
= B_FALSE
;
8789 (void) printf(gettext("%s\n"),
8790 zpool_get_name(zhp
));
8791 poolfirst
= B_FALSE
;
8794 (void) printf(gettext(" %s\n"), fname
);
8797 * If they did "zpool upgrade -a", then we could
8798 * be doing ioctls to different pools. We need
8799 * to log this history once to each pool, and bypass
8800 * the normal history logging that happens in main().
8802 (void) zpool_log_history(g_zfs
, history_str
);
8803 log_history
= B_FALSE
;
8812 upgrade_one(zpool_handle_t
*zhp
, void *data
)
8814 boolean_t printnl
= B_FALSE
;
8815 upgrade_cbdata_t
*cbp
= data
;
8816 uint64_t cur_version
;
8819 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
8820 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
8821 "Pool 'log' must be renamed using export and import"
8826 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
8827 if (cur_version
> cbp
->cb_version
) {
8828 (void) printf(gettext("Pool '%s' is already formatted "
8829 "using more current version '%llu'.\n\n"),
8830 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
8834 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
8835 (void) printf(gettext("Pool '%s' is already formatted "
8836 "using version %llu.\n\n"), zpool_get_name(zhp
),
8837 (u_longlong_t
)cbp
->cb_version
);
8841 if (cur_version
!= cbp
->cb_version
) {
8843 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8848 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8850 ret
= upgrade_enable_all(zhp
, &count
);
8856 } else if (cur_version
== SPA_VERSION
) {
8857 (void) printf(gettext("Pool '%s' already has all "
8858 "supported features enabled.\n"),
8859 zpool_get_name(zhp
));
8864 (void) printf(gettext("\n"));
8873 * zpool upgrade [-V version] <-a | pool ...>
8875 * With no arguments, display downrev'd ZFS pool available for upgrade.
8876 * Individual pools can be upgraded by specifying the pool, and '-a' will
8877 * upgrade all pools.
8880 zpool_do_upgrade(int argc
, char **argv
)
8883 upgrade_cbdata_t cb
= { 0 };
8885 boolean_t showversions
= B_FALSE
;
8886 boolean_t upgradeall
= B_FALSE
;
8891 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
8894 upgradeall
= B_TRUE
;
8897 showversions
= B_TRUE
;
8900 cb
.cb_version
= strtoll(optarg
, &end
, 10);
8902 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
8903 (void) fprintf(stderr
,
8904 gettext("invalid version '%s'\n"), optarg
);
8909 (void) fprintf(stderr
, gettext("missing argument for "
8910 "'%c' option\n"), optopt
);
8914 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8925 if (cb
.cb_version
== 0) {
8926 cb
.cb_version
= SPA_VERSION
;
8927 } else if (!upgradeall
&& argc
== 0) {
8928 (void) fprintf(stderr
, gettext("-V option is "
8929 "incompatible with other arguments\n"));
8934 if (upgradeall
|| argc
!= 0) {
8935 (void) fprintf(stderr
, gettext("-v option is "
8936 "incompatible with other arguments\n"));
8939 } else if (upgradeall
) {
8941 (void) fprintf(stderr
, gettext("-a option should not "
8942 "be used along with a pool name\n"));
8947 (void) printf(gettext("This system supports ZFS pool feature "
8952 (void) printf(gettext("The following features are "
8954 (void) printf(gettext("FEAT DESCRIPTION\n"));
8955 (void) printf("----------------------------------------------"
8956 "---------------\n");
8957 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8958 zfeature_info_t
*fi
= &spa_feature_table
[i
];
8960 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
8961 " (read-only compatible)" : "";
8963 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
8964 (void) printf(" %s\n", fi
->fi_desc
);
8966 (void) printf("\n");
8968 (void) printf(gettext("The following legacy versions are also "
8970 (void) printf(gettext("VER DESCRIPTION\n"));
8971 (void) printf("--- -----------------------------------------"
8972 "---------------\n");
8973 (void) printf(gettext(" 1 Initial ZFS version\n"));
8974 (void) printf(gettext(" 2 Ditto blocks "
8975 "(replicated metadata)\n"));
8976 (void) printf(gettext(" 3 Hot spares and double parity "
8978 (void) printf(gettext(" 4 zpool history\n"));
8979 (void) printf(gettext(" 5 Compression using the gzip "
8981 (void) printf(gettext(" 6 bootfs pool property\n"));
8982 (void) printf(gettext(" 7 Separate intent log devices\n"));
8983 (void) printf(gettext(" 8 Delegated administration\n"));
8984 (void) printf(gettext(" 9 refquota and refreservation "
8986 (void) printf(gettext(" 10 Cache devices\n"));
8987 (void) printf(gettext(" 11 Improved scrub performance\n"));
8988 (void) printf(gettext(" 12 Snapshot properties\n"));
8989 (void) printf(gettext(" 13 snapused property\n"));
8990 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
8991 (void) printf(gettext(" 15 user/group space accounting\n"));
8992 (void) printf(gettext(" 16 stmf property support\n"));
8993 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
8994 (void) printf(gettext(" 18 Snapshot user holds\n"));
8995 (void) printf(gettext(" 19 Log device removal\n"));
8996 (void) printf(gettext(" 20 Compression using zle "
8997 "(zero-length encoding)\n"));
8998 (void) printf(gettext(" 21 Deduplication\n"));
8999 (void) printf(gettext(" 22 Received properties\n"));
9000 (void) printf(gettext(" 23 Slim ZIL\n"));
9001 (void) printf(gettext(" 24 System attributes\n"));
9002 (void) printf(gettext(" 25 Improved scrub stats\n"));
9003 (void) printf(gettext(" 26 Improved snapshot deletion "
9005 (void) printf(gettext(" 27 Improved snapshot creation "
9007 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9008 (void) printf(gettext("\nFor more information on a particular "
9009 "version, including supported releases,\n"));
9010 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9011 } else if (argc
== 0 && upgradeall
) {
9012 cb
.cb_first
= B_TRUE
;
9013 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9014 if (ret
== 0 && cb
.cb_first
) {
9015 if (cb
.cb_version
== SPA_VERSION
) {
9016 (void) printf(gettext("All pools are already "
9017 "formatted using feature flags.\n\n"));
9018 (void) printf(gettext("Every feature flags "
9019 "pool already has all supported features "
9022 (void) printf(gettext("All pools are already "
9023 "formatted with version %llu or higher.\n"),
9024 (u_longlong_t
)cb
.cb_version
);
9027 } else if (argc
== 0) {
9028 cb
.cb_first
= B_TRUE
;
9029 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9033 (void) printf(gettext("All pools are formatted "
9034 "using feature flags.\n\n"));
9036 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9037 "for a list of available legacy versions.\n"));
9040 cb
.cb_first
= B_TRUE
;
9041 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9045 (void) printf(gettext("Every feature flags pool has "
9046 "all supported features enabled.\n"));
9048 (void) printf(gettext("\n"));
9051 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
,
9058 typedef struct hist_cbdata
{
9065 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9071 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9072 &records
, &numrecords
) == 0);
9073 for (i
= 0; i
< numrecords
; i
++) {
9074 nvlist_t
*rec
= records
[i
];
9077 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9081 tsec
= fnvlist_lookup_uint64(records
[i
],
9083 (void) localtime_r(&tsec
, &t
);
9084 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9087 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9088 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9089 ZPOOL_HIST_ELAPSED_NS
);
9090 (void) snprintf(tbuf
+ strlen(tbuf
),
9091 sizeof (tbuf
) - strlen(tbuf
),
9092 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9095 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9096 (void) printf("%s %s", tbuf
,
9097 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9098 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9100 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9103 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9104 (void) printf("%s unrecognized record:\n",
9106 dump_nvlist(rec
, 4);
9109 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9110 zfs_history_event_names
[ievent
],
9111 (longlong_t
)fnvlist_lookup_uint64(
9112 rec
, ZPOOL_HIST_TXG
),
9113 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9114 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9117 (void) printf("%s [txg:%lld] %s", tbuf
,
9118 (longlong_t
)fnvlist_lookup_uint64(
9119 rec
, ZPOOL_HIST_TXG
),
9120 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9121 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9122 (void) printf(" %s (%llu)",
9123 fnvlist_lookup_string(rec
,
9125 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9128 (void) printf(" %s", fnvlist_lookup_string(rec
,
9129 ZPOOL_HIST_INT_STR
));
9130 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9133 (void) printf("%s ioctl %s\n", tbuf
,
9134 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9135 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9136 (void) printf(" input:\n");
9137 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9138 ZPOOL_HIST_INPUT_NVL
), 8);
9140 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9141 (void) printf(" output:\n");
9142 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9143 ZPOOL_HIST_OUTPUT_NVL
), 8);
9145 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9146 (void) printf(" output nvlist omitted; "
9147 "original size: %lldKB\n",
9148 (longlong_t
)fnvlist_lookup_int64(rec
,
9149 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9151 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9152 (void) printf(" errno: %lld\n",
9153 (longlong_t
)fnvlist_lookup_int64(rec
,
9159 (void) printf("%s unrecognized record:\n", tbuf
);
9160 dump_nvlist(rec
, 4);
9164 (void) printf("\n");
9167 (void) printf(" [");
9168 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9169 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9170 struct passwd
*pwd
= getpwuid(who
);
9171 (void) printf("user %d ", (int)who
);
9173 (void) printf("(%s) ", pwd
->pw_name
);
9175 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9176 (void) printf("on %s",
9177 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9179 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9180 (void) printf(":%s",
9181 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9185 (void) printf("\n");
9190 * Print out the command history for a specific pool.
9193 get_history_one(zpool_handle_t
*zhp
, void *data
)
9197 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9199 boolean_t eof
= B_FALSE
;
9201 cb
->first
= B_FALSE
;
9203 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9206 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9209 print_history_records(nvhis
, cb
);
9212 (void) printf("\n");
9218 * zpool history <pool>
9220 * Displays the history of commands that modified pools.
9223 zpool_do_history(int argc
, char **argv
)
9225 hist_cbdata_t cbdata
= { 0 };
9229 cbdata
.first
= B_TRUE
;
9231 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9234 cbdata
.longfmt
= B_TRUE
;
9237 cbdata
.internal
= B_TRUE
;
9240 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9248 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
, get_history_one
,
9251 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9252 (void) fprintf(stderr
, gettext("no pools available\n"));
9259 typedef struct ev_opts
{
9264 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9268 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9270 char ctime_str
[26], str
[32], *ptr
;
9274 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9275 memset(str
, ' ', 32);
9276 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9277 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9278 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9279 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9280 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9282 (void) printf(gettext("%s\t"), str
);
9284 (void) printf(gettext("%s "), str
);
9286 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9287 (void) printf(gettext("%s\n"), ptr
);
9291 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9295 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9296 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9298 data_type_t type
= nvpair_type(nvp
);
9299 const char *name
= nvpair_name(nvp
);
9309 printf(gettext("%*s%s = "), depth
, "", name
);
9312 case DATA_TYPE_BOOLEAN
:
9313 printf(gettext("%s"), "1");
9316 case DATA_TYPE_BOOLEAN_VALUE
:
9317 (void) nvpair_value_boolean_value(nvp
, &b
);
9318 printf(gettext("%s"), b
? "1" : "0");
9321 case DATA_TYPE_BYTE
:
9322 (void) nvpair_value_byte(nvp
, &i8
);
9323 printf(gettext("0x%x"), i8
);
9326 case DATA_TYPE_INT8
:
9327 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9328 printf(gettext("0x%x"), i8
);
9331 case DATA_TYPE_UINT8
:
9332 (void) nvpair_value_uint8(nvp
, &i8
);
9333 printf(gettext("0x%x"), i8
);
9336 case DATA_TYPE_INT16
:
9337 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9338 printf(gettext("0x%x"), i16
);
9341 case DATA_TYPE_UINT16
:
9342 (void) nvpair_value_uint16(nvp
, &i16
);
9343 printf(gettext("0x%x"), i16
);
9346 case DATA_TYPE_INT32
:
9347 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9348 printf(gettext("0x%x"), i32
);
9351 case DATA_TYPE_UINT32
:
9352 (void) nvpair_value_uint32(nvp
, &i32
);
9353 printf(gettext("0x%x"), i32
);
9356 case DATA_TYPE_INT64
:
9357 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9358 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9361 case DATA_TYPE_UINT64
:
9362 (void) nvpair_value_uint64(nvp
, &i64
);
9364 * translate vdev state values to readable
9365 * strings to aide zpool events consumers
9368 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9370 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9371 printf(gettext("\"%s\" (0x%llx)"),
9372 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9375 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9379 case DATA_TYPE_HRTIME
:
9380 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9381 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9384 case DATA_TYPE_STRING
:
9385 (void) nvpair_value_string(nvp
, &str
);
9386 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9389 case DATA_TYPE_NVLIST
:
9390 printf(gettext("(embedded nvlist)\n"));
9391 (void) nvpair_value_nvlist(nvp
, &cnv
);
9392 zpool_do_events_nvprint(cnv
, depth
+ 8);
9393 printf(gettext("%*s(end %s)"), depth
, "", name
);
9396 case DATA_TYPE_NVLIST_ARRAY
: {
9400 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9401 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9402 for (i
= 0; i
< nelem
; i
++) {
9403 printf(gettext("%*s%s[%d] = %s\n"),
9404 depth
, "", name
, i
, "(embedded nvlist)");
9405 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9406 printf(gettext("%*s(end %s[%i])\n"),
9407 depth
, "", name
, i
);
9409 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9413 case DATA_TYPE_INT8_ARRAY
: {
9417 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9418 for (i
= 0; i
< nelem
; i
++)
9419 printf(gettext("0x%x "), val
[i
]);
9424 case DATA_TYPE_UINT8_ARRAY
: {
9428 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9429 for (i
= 0; i
< nelem
; i
++)
9430 printf(gettext("0x%x "), val
[i
]);
9435 case DATA_TYPE_INT16_ARRAY
: {
9439 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9440 for (i
= 0; i
< nelem
; i
++)
9441 printf(gettext("0x%x "), val
[i
]);
9446 case DATA_TYPE_UINT16_ARRAY
: {
9450 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9451 for (i
= 0; i
< nelem
; i
++)
9452 printf(gettext("0x%x "), val
[i
]);
9457 case DATA_TYPE_INT32_ARRAY
: {
9461 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9462 for (i
= 0; i
< nelem
; i
++)
9463 printf(gettext("0x%x "), val
[i
]);
9468 case DATA_TYPE_UINT32_ARRAY
: {
9472 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9473 for (i
= 0; i
< nelem
; i
++)
9474 printf(gettext("0x%x "), val
[i
]);
9479 case DATA_TYPE_INT64_ARRAY
: {
9483 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9484 for (i
= 0; i
< nelem
; i
++)
9485 printf(gettext("0x%llx "),
9486 (u_longlong_t
)val
[i
]);
9491 case DATA_TYPE_UINT64_ARRAY
: {
9495 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9496 for (i
= 0; i
< nelem
; i
++)
9497 printf(gettext("0x%llx "),
9498 (u_longlong_t
)val
[i
]);
9503 case DATA_TYPE_STRING_ARRAY
: {
9507 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9508 for (i
= 0; i
< nelem
; i
++)
9509 printf(gettext("\"%s\" "),
9510 str
[i
] ? str
[i
] : "<NULL>");
9515 case DATA_TYPE_BOOLEAN_ARRAY
:
9516 case DATA_TYPE_BYTE_ARRAY
:
9517 case DATA_TYPE_DOUBLE
:
9518 case DATA_TYPE_DONTCARE
:
9519 case DATA_TYPE_UNKNOWN
:
9520 printf(gettext("<unknown>"));
9524 printf(gettext("\n"));
9529 zpool_do_events_next(ev_opts_t
*opts
)
9532 int zevent_fd
, ret
, dropped
;
9535 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9536 VERIFY(zevent_fd
>= 0);
9538 if (!opts
->scripted
)
9539 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9542 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9543 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9544 if (ret
|| nvl
== NULL
)
9548 (void) printf(gettext("dropped %d events\n"), dropped
);
9550 if (strlen(opts
->poolname
) > 0 &&
9551 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9552 strcmp(opts
->poolname
, pool
) != 0)
9555 zpool_do_events_short(nvl
, opts
);
9557 if (opts
->verbose
) {
9558 zpool_do_events_nvprint(nvl
, 8);
9559 printf(gettext("\n"));
9561 (void) fflush(stdout
);
9566 VERIFY(0 == close(zevent_fd
));
9572 zpool_do_events_clear(ev_opts_t
*opts
)
9576 ret
= zpool_events_clear(g_zfs
, &count
);
9578 (void) printf(gettext("cleared %d events\n"), count
);
9584 * zpool events [-vHf [pool] | -c]
9586 * Displays events logs by ZFS.
9589 zpool_do_events(int argc
, char **argv
)
9591 ev_opts_t opts
= { 0 };
9596 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9611 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9620 (void) fprintf(stderr
, gettext("too many arguments\n"));
9622 } else if (argc
== 1) {
9623 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9624 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9625 (void) fprintf(stderr
,
9626 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9631 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9633 (void) fprintf(stderr
,
9634 gettext("invalid options combined with -c\n"));
9639 ret
= zpool_do_events_clear(&opts
);
9641 ret
= zpool_do_events_next(&opts
);
9647 get_callback(zpool_handle_t
*zhp
, void *data
)
9649 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9650 char value
[MAXNAMELEN
];
9651 zprop_source_t srctype
;
9654 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
9657 * Skip the special fake placeholder. This will also skip
9658 * over the name property when 'all' is specified.
9660 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9661 pl
== cbp
->cb_proplist
)
9664 if (pl
->pl_prop
== ZPROP_INVAL
&&
9665 (zpool_prop_feature(pl
->pl_user_prop
) ||
9666 zpool_prop_unsupported(pl
->pl_user_prop
))) {
9667 srctype
= ZPROP_SRC_LOCAL
;
9669 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
9670 value
, sizeof (value
)) == 0) {
9671 zprop_print_one_property(zpool_get_name(zhp
),
9672 cbp
, pl
->pl_user_prop
, value
, srctype
,
9676 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
9677 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
9680 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
9681 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
9689 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
9691 * -H Scripted mode. Don't display headers, and separate properties
9693 * -o List of columns to display. Defaults to
9694 * "name,property,value,source".
9695 * -p Display values in parsable (exact) format.
9697 * Get properties of pools in the system. Output space statistics
9698 * for each one as well as other attributes.
9701 zpool_do_get(int argc
, char **argv
)
9703 zprop_get_cbdata_t cb
= { 0 };
9704 zprop_list_t fake_name
= { 0 };
9709 cb
.cb_first
= B_TRUE
;
9712 * Set up default columns and sources.
9714 cb
.cb_sources
= ZPROP_SRC_ALL
;
9715 cb
.cb_columns
[0] = GET_COL_NAME
;
9716 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9717 cb
.cb_columns
[2] = GET_COL_VALUE
;
9718 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9719 cb
.cb_type
= ZFS_TYPE_POOL
;
9722 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
9725 cb
.cb_literal
= B_TRUE
;
9728 cb
.cb_scripted
= B_TRUE
;
9731 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
9733 while (*optarg
!= '\0') {
9734 static char *col_subopts
[] =
9735 { "name", "property", "value", "source",
9738 if (i
== ZFS_GET_NCOLS
) {
9739 (void) fprintf(stderr
, gettext("too "
9740 "many fields given to -o "
9745 switch (getsubopt(&optarg
, col_subopts
,
9748 cb
.cb_columns
[i
++] = GET_COL_NAME
;
9751 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
9754 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
9757 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
9761 (void) fprintf(stderr
,
9762 gettext("\"all\" conflicts "
9763 "with specific fields "
9764 "given to -o option\n"));
9767 cb
.cb_columns
[0] = GET_COL_NAME
;
9768 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9769 cb
.cb_columns
[2] = GET_COL_VALUE
;
9770 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9774 (void) fprintf(stderr
,
9775 gettext("invalid column name "
9782 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9792 (void) fprintf(stderr
, gettext("missing property "
9797 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
9798 ZFS_TYPE_POOL
) != 0)
9804 if (cb
.cb_proplist
!= NULL
) {
9805 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
9806 fake_name
.pl_width
= strlen(gettext("NAME"));
9807 fake_name
.pl_next
= cb
.cb_proplist
;
9808 cb
.cb_proplist
= &fake_name
;
9811 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_literal
,
9814 if (cb
.cb_proplist
== &fake_name
)
9815 zprop_free_list(fake_name
.pl_next
);
9817 zprop_free_list(cb
.cb_proplist
);
9822 typedef struct set_cbdata
{
9825 boolean_t cb_any_successful
;
9829 set_callback(zpool_handle_t
*zhp
, void *data
)
9832 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
9834 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
9837 cb
->cb_any_successful
= B_TRUE
;
9843 zpool_do_set(int argc
, char **argv
)
9845 set_cbdata_t cb
= { 0 };
9848 if (argc
> 1 && argv
[1][0] == '-') {
9849 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9855 (void) fprintf(stderr
, gettext("missing property=value "
9861 (void) fprintf(stderr
, gettext("missing pool name\n"));
9866 (void) fprintf(stderr
, gettext("too many pool names\n"));
9870 cb
.cb_propname
= argv
[1];
9871 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
9872 if (cb
.cb_value
== NULL
) {
9873 (void) fprintf(stderr
, gettext("missing value in "
9874 "property=value argument\n"));
9878 *(cb
.cb_value
) = '\0';
9881 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
, B_FALSE
,
9887 /* Add up the total number of bytes left to initialize/trim across all vdevs */
9889 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
9891 uint64_t bytes_remaining
;
9896 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
9897 activity
== ZPOOL_WAIT_TRIM
);
9899 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
9900 (uint64_t **)&vs
, &c
) == 0);
9902 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
9903 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
9904 bytes_remaining
= vs
->vs_initialize_bytes_est
-
9905 vs
->vs_initialize_bytes_done
;
9906 else if (activity
== ZPOOL_WAIT_TRIM
&&
9907 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
9908 bytes_remaining
= vs
->vs_trim_bytes_est
-
9909 vs
->vs_trim_bytes_done
;
9911 bytes_remaining
= 0;
9913 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9914 &child
, &children
) != 0)
9917 for (c
= 0; c
< children
; c
++)
9918 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
9920 return (bytes_remaining
);
9923 /* Add up the total number of bytes left to rebuild across top-level vdevs */
9925 vdev_activity_top_remaining(nvlist_t
*nv
)
9927 uint64_t bytes_remaining
= 0;
9932 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9933 &child
, &children
) != 0)
9936 for (uint_t c
= 0; c
< children
; c
++) {
9937 vdev_rebuild_stat_t
*vrs
;
9940 error
= nvlist_lookup_uint64_array(child
[c
],
9941 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
9943 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
9944 bytes_remaining
+= (vrs
->vrs_bytes_est
-
9945 vrs
->vrs_bytes_rebuilt
);
9950 return (bytes_remaining
);
9953 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
9955 vdev_any_spare_replacing(nvlist_t
*nv
)
9961 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
9963 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
9964 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
9965 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
9969 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9970 &child
, &children
) != 0)
9973 for (c
= 0; c
< children
; c
++) {
9974 if (vdev_any_spare_replacing(child
[c
]))
9981 typedef struct wait_data
{
9983 boolean_t wd_scripted
;
9985 boolean_t wd_headers_once
;
9986 boolean_t wd_should_exit
;
9987 /* Which activities to wait for */
9988 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
9990 pthread_cond_t wd_cv
;
9991 pthread_mutex_t wd_mutex
;
9995 * Print to stdout a single line, containing one column for each activity that
9996 * we are waiting for specifying how many bytes of work are left for that
10000 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10002 nvlist_t
*config
, *nvroot
;
10005 pool_checkpoint_stat_t
*pcs
= NULL
;
10006 pool_scan_stat_t
*pss
= NULL
;
10007 pool_removal_stat_t
*prs
= NULL
;
10008 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
10009 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10010 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10012 /* Calculate the width of each column */
10013 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10015 * Make sure we have enough space in the col for pretty-printed
10016 * numbers and for the column header, and then leave a couple
10017 * spaces between cols for readability.
10019 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10022 /* Print header if appropriate */
10023 int term_height
= terminal_height();
10024 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10025 row
% (term_height
-1) == 0);
10026 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10027 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10028 if (wd
->wd_enabled
[i
])
10029 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10031 (void) printf("\n");
10034 /* Bytes of work remaining in each activity */
10035 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10037 bytes_rem
[ZPOOL_WAIT_FREE
] =
10038 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10040 config
= zpool_get_config(zhp
, NULL
);
10041 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10043 (void) nvlist_lookup_uint64_array(nvroot
,
10044 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10045 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10046 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10048 (void) nvlist_lookup_uint64_array(nvroot
,
10049 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10050 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10051 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10054 (void) nvlist_lookup_uint64_array(nvroot
,
10055 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10056 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10057 pss
->pss_pass_scrub_pause
== 0) {
10058 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10059 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10060 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10062 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10063 } else if (check_rebuilding(nvroot
, NULL
)) {
10064 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10065 vdev_activity_top_remaining(nvroot
);
10068 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10069 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10070 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10071 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10074 * A replace finishes after resilvering finishes, so the amount of work
10075 * left for a replace is the same as for resilvering.
10077 * It isn't quite correct to say that if we have any 'spare' or
10078 * 'replacing' vdevs and a resilver is happening, then a replace is in
10079 * progress, like we do here. When a hot spare is used, the faulted vdev
10080 * is not removed after the hot spare is resilvered, so parent 'spare'
10081 * vdev is not removed either. So we could have a 'spare' vdev, but be
10082 * resilvering for a different reason. However, we use it as a heuristic
10083 * because we don't have access to the DTLs, which could tell us whether
10084 * or not we have really finished resilvering a hot spare.
10086 if (vdev_any_spare_replacing(nvroot
))
10087 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10089 if (timestamp_fmt
!= NODATE
)
10090 print_timestamp(timestamp_fmt
);
10092 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10094 if (!wd
->wd_enabled
[i
])
10098 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10101 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10103 if (wd
->wd_scripted
)
10104 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10106 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10108 (void) printf("\n");
10109 (void) fflush(stdout
);
10113 wait_status_thread(void *arg
)
10115 wait_data_t
*wd
= (wait_data_t
*)arg
;
10116 zpool_handle_t
*zhp
;
10118 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10119 return (void *)(1);
10121 for (int row
= 0; ; row
++) {
10123 struct timespec timeout
;
10125 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10127 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10128 zpool_props_refresh(zhp
) != 0) {
10130 return (void *)(uintptr_t)(missing
? 0 : 1);
10133 print_wait_status_row(wd
, zhp
, row
);
10135 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10136 long nanos
= timeout
.tv_nsec
+
10137 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10138 if (nanos
>= NANOSEC
) {
10140 timeout
.tv_nsec
= nanos
- NANOSEC
;
10142 timeout
.tv_nsec
= nanos
;
10144 pthread_mutex_lock(&wd
->wd_mutex
);
10145 if (!wd
->wd_should_exit
)
10146 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10148 pthread_mutex_unlock(&wd
->wd_mutex
);
10150 break; /* signaled by main thread */
10151 } else if (ret
!= ETIMEDOUT
) {
10152 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10153 "failed: %s\n"), strerror(ret
));
10155 return (void *)(uintptr_t)(1);
10160 return (void *)(0);
10164 zpool_do_wait(int argc
, char **argv
)
10166 boolean_t verbose
= B_FALSE
;
10170 unsigned long count
;
10171 pthread_t status_thr
;
10173 zpool_handle_t
*zhp
;
10176 wd
.wd_scripted
= B_FALSE
;
10177 wd
.wd_exact
= B_FALSE
;
10178 wd
.wd_headers_once
= B_FALSE
;
10179 wd
.wd_should_exit
= B_FALSE
;
10181 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10182 pthread_cond_init(&wd
.wd_cv
, NULL
);
10184 /* By default, wait for all types of activity. */
10185 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10186 wd
.wd_enabled
[i
] = B_TRUE
;
10188 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10191 wd
.wd_scripted
= B_TRUE
;
10194 wd
.wd_headers_once
= B_TRUE
;
10197 wd
.wd_exact
= B_TRUE
;
10200 get_timestamp_arg(*optarg
);
10204 static char *col_subopts
[] = { "discard", "free",
10205 "initialize", "replace", "remove", "resilver",
10206 "scrub", "trim", NULL
};
10208 /* Reset activities array */
10209 bzero(&wd
.wd_enabled
, sizeof (wd
.wd_enabled
));
10210 while (*optarg
!= '\0') {
10211 int activity
= getsubopt(&optarg
, col_subopts
,
10214 if (activity
< 0) {
10215 (void) fprintf(stderr
,
10216 gettext("invalid activity '%s'\n"),
10221 wd
.wd_enabled
[activity
] = B_TRUE
;
10226 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10235 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10237 /* This subcmd only accepts an interval, not a count */
10238 (void) fprintf(stderr
, gettext("too many arguments\n"));
10242 if (wd
.wd_interval
!= 0)
10246 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10250 (void) fprintf(stderr
, gettext("too many arguments\n"));
10254 wd
.wd_poolname
= argv
[0];
10256 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10261 * We use a separate thread for printing status updates because
10262 * the main thread will call lzc_wait(), which blocks as long
10263 * as an activity is in progress, which can be a long time.
10265 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10267 (void) fprintf(stderr
, gettext("failed to create status"
10268 "thread: %s\n"), strerror(errno
));
10275 * Loop over all activities that we are supposed to wait for until none
10276 * of them are in progress. Note that this means we can end up waiting
10277 * for more activities to complete than just those that were in progress
10278 * when we began waiting; if an activity we are interested in begins
10279 * while we are waiting for another activity, we will wait for both to
10280 * complete before exiting.
10283 boolean_t missing
= B_FALSE
;
10284 boolean_t any_waited
= B_FALSE
;
10286 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10289 if (!wd
.wd_enabled
[i
])
10292 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10293 if (error
!= 0 || missing
)
10296 any_waited
= (any_waited
|| waited
);
10299 if (error
!= 0 || missing
|| !any_waited
)
10307 pthread_mutex_lock(&wd
.wd_mutex
);
10308 wd
.wd_should_exit
= B_TRUE
;
10309 pthread_cond_signal(&wd
.wd_cv
);
10310 pthread_mutex_unlock(&wd
.wd_mutex
);
10311 (void) pthread_join(status_thr
, (void *)&status
);
10316 pthread_mutex_destroy(&wd
.wd_mutex
);
10317 pthread_cond_destroy(&wd
.wd_cv
);
10322 find_command_idx(char *command
, int *idx
)
10326 for (i
= 0; i
< NCOMMAND
; i
++) {
10327 if (command_table
[i
].name
== NULL
)
10330 if (strcmp(command
, command_table
[i
].name
) == 0) {
10339 * Display version message
10342 zpool_do_version(int argc
, char **argv
)
10344 if (zfs_version_print() == -1)
10351 main(int argc
, char **argv
)
10358 (void) setlocale(LC_ALL
, "");
10359 (void) setlocale(LC_NUMERIC
, "C");
10360 (void) textdomain(TEXT_DOMAIN
);
10366 * Make sure the user has specified some command.
10369 (void) fprintf(stderr
, gettext("missing command\n"));
10376 * Special case '-?'
10378 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10382 * Special case '-V|--version'
10384 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10385 return (zpool_do_version(argc
, argv
));
10387 if ((g_zfs
= libzfs_init()) == NULL
) {
10388 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10392 libzfs_print_on_error(g_zfs
, B_TRUE
);
10394 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10397 * Many commands modify input strings for string parsing reasons.
10398 * We create a copy to protect the original argv.
10400 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10401 for (i
= 0; i
< argc
; i
++)
10402 newargv
[i
] = strdup(argv
[i
]);
10403 newargv
[argc
] = NULL
;
10406 * Run the appropriate command.
10408 if (find_command_idx(cmdname
, &i
) == 0) {
10409 current_command
= &command_table
[i
];
10410 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10411 } else if (strchr(cmdname
, '=')) {
10412 verify(find_command_idx("set", &i
) == 0);
10413 current_command
= &command_table
[i
];
10414 ret
= command_table
[i
].func(argc
, newargv
);
10415 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10417 * 'freeze' is a vile debugging abomination, so we treat
10420 zfs_cmd_t zc
= {"\0"};
10422 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10423 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10425 (void) fprintf(stderr
,
10426 gettext("failed to freeze pool: %d\n"), errno
);
10432 (void) fprintf(stderr
, gettext("unrecognized "
10433 "command '%s'\n"), cmdname
);
10438 for (i
= 0; i
< argc
; i
++)
10442 if (ret
== 0 && log_history
)
10443 (void) zpool_log_history(g_zfs
, history_str
);
10445 libzfs_fini(g_zfs
);
10448 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10449 * for the purposes of running ::findleaks.
10451 if (getenv("ZFS_ABORT") != NULL
) {
10452 (void) printf("dumping core by request\n");