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>
34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
35 * Copyright (c) 2021, Klara Inc.
36 * Copyright [2021] Hewlett Packard Enterprise Development LP
60 #include <sys/fs/zfs.h>
62 #include <sys/systeminfo.h>
63 #include <sys/fm/fs/zfs.h>
64 #include <sys/fm/util.h>
65 #include <sys/fm/protocol.h>
66 #include <sys/zfs_ioctl.h>
67 #include <sys/mount.h>
68 #include <sys/sysmacros.h>
75 #include "zpool_util.h"
76 #include "zfs_comutil.h"
77 #include "zfeature_common.h"
79 #include "statcommon.h"
81 libzfs_handle_t
*g_zfs
;
83 static int zpool_do_create(int, char **);
84 static int zpool_do_destroy(int, char **);
86 static int zpool_do_add(int, char **);
87 static int zpool_do_remove(int, char **);
88 static int zpool_do_labelclear(int, char **);
90 static int zpool_do_checkpoint(int, char **);
92 static int zpool_do_list(int, char **);
93 static int zpool_do_iostat(int, char **);
94 static int zpool_do_status(int, char **);
96 static int zpool_do_online(int, char **);
97 static int zpool_do_offline(int, char **);
98 static int zpool_do_clear(int, char **);
99 static int zpool_do_reopen(int, char **);
101 static int zpool_do_reguid(int, char **);
103 static int zpool_do_attach(int, char **);
104 static int zpool_do_detach(int, char **);
105 static int zpool_do_replace(int, char **);
106 static int zpool_do_split(int, char **);
108 static int zpool_do_initialize(int, char **);
109 static int zpool_do_scrub(int, char **);
110 static int zpool_do_resilver(int, char **);
111 static int zpool_do_trim(int, char **);
113 static int zpool_do_import(int, char **);
114 static int zpool_do_export(int, char **);
116 static int zpool_do_upgrade(int, char **);
118 static int zpool_do_history(int, char **);
119 static int zpool_do_events(int, char **);
121 static int zpool_do_get(int, char **);
122 static int zpool_do_set(int, char **);
124 static int zpool_do_sync(int, char **);
126 static int zpool_do_version(int, char **);
128 static int zpool_do_wait(int, char **);
130 static zpool_compat_status_t
zpool_do_load_compat(
131 const char *, boolean_t
*);
134 * These libumem hooks provide a reasonable set of defaults for the allocator's
135 * debugging facilities.
140 _umem_debug_init(void)
142 return ("default,verbose"); /* $UMEM_DEBUG setting */
146 _umem_logging_init(void)
148 return ("fail,contents"); /* $UMEM_LOGGING setting */
189 * Flags for stats to display with "zpool iostats"
197 IOS_COUNT
, /* always last element */
200 /* iostat_type entries as bitmasks */
201 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
202 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
203 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
204 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
205 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
207 /* Mask of all the histo bits */
208 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
211 * Lookup table for iostat flags to nvlist names. Basically a list
212 * of all the nvlists a flag requires. Also specifies the order in
213 * which data gets printed in zpool iostat.
215 static const char *vsx_type_to_nvlist
[IOS_COUNT
][15] = {
217 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
218 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
221 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
222 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
225 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
226 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
227 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
230 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
231 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
232 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
233 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
234 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
235 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
238 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
239 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
240 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
241 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
242 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
243 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
244 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
247 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
248 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
249 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
250 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
251 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
252 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
253 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
254 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
255 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
256 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
257 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
258 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
259 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO
,
260 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO
,
266 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
267 * Right now, only one histo bit is ever set at one time, so we can
268 * just do a highbit64(a)
270 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
272 typedef struct zpool_command
{
274 int (*func
)(int, char **);
279 * Master command table. Each ZFS command has a name, associated function, and
280 * usage message. The usage messages need to be internationalized, so we have
281 * to have a function to return the usage message based on a command index.
283 * These commands are organized according to how they are displayed in the usage
284 * message. An empty command (one with a NULL name) indicates an empty line in
285 * the generic usage message.
287 static zpool_command_t command_table
[] = {
288 { "version", zpool_do_version
, HELP_VERSION
},
290 { "create", zpool_do_create
, HELP_CREATE
},
291 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
293 { "add", zpool_do_add
, HELP_ADD
},
294 { "remove", zpool_do_remove
, HELP_REMOVE
},
296 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
298 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
300 { "list", zpool_do_list
, HELP_LIST
},
301 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
302 { "status", zpool_do_status
, HELP_STATUS
},
304 { "online", zpool_do_online
, HELP_ONLINE
},
305 { "offline", zpool_do_offline
, HELP_OFFLINE
},
306 { "clear", zpool_do_clear
, HELP_CLEAR
},
307 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
309 { "attach", zpool_do_attach
, HELP_ATTACH
},
310 { "detach", zpool_do_detach
, HELP_DETACH
},
311 { "replace", zpool_do_replace
, HELP_REPLACE
},
312 { "split", zpool_do_split
, HELP_SPLIT
},
314 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
315 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
316 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
317 { "trim", zpool_do_trim
, HELP_TRIM
},
319 { "import", zpool_do_import
, HELP_IMPORT
},
320 { "export", zpool_do_export
, HELP_EXPORT
},
321 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
322 { "reguid", zpool_do_reguid
, HELP_REGUID
},
324 { "history", zpool_do_history
, HELP_HISTORY
},
325 { "events", zpool_do_events
, HELP_EVENTS
},
327 { "get", zpool_do_get
, HELP_GET
},
328 { "set", zpool_do_set
, HELP_SET
},
329 { "sync", zpool_do_sync
, HELP_SYNC
},
331 { "wait", zpool_do_wait
, HELP_WAIT
},
334 #define NCOMMAND (ARRAY_SIZE(command_table))
336 #define VDEV_ALLOC_CLASS_LOGS "logs"
338 static zpool_command_t
*current_command
;
339 static zfs_type_t current_prop_type
= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
);
340 static char history_str
[HIS_MAX_RECORD_LEN
];
341 static boolean_t log_history
= B_TRUE
;
342 static uint_t timestamp_fmt
= NODATE
;
345 get_usage(zpool_help_t idx
)
349 return (gettext("\tadd [-fgLnP] [-o property=value] "
350 "<pool> <vdev> ...\n"));
352 return (gettext("\tattach [-fsw] [-o property=value] "
353 "<pool> <device> <new-device>\n"));
355 return (gettext("\tclear [-nF] <pool> [device]\n"));
357 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
358 "\t [-O file-system-property=value] ... \n"
359 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
360 case HELP_CHECKPOINT
:
361 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
363 return (gettext("\tdestroy [-f] <pool>\n"));
365 return (gettext("\tdetach <pool> <device>\n"));
367 return (gettext("\texport [-af] <pool> ...\n"));
369 return (gettext("\thistory [-il] [<pool>] ...\n"));
371 return (gettext("\timport [-d dir] [-D]\n"
372 "\timport [-o mntopts] [-o property=value] ... \n"
373 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
374 "[-R root] [-F [-n]] -a\n"
375 "\timport [-o mntopts] [-o property=value] ... \n"
376 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
377 "[-R root] [-F [-n]]\n"
378 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
380 return (gettext("\tiostat [[[-c [script1,script2,...]"
381 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
382 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
383 " [[-n] interval [count]]\n"));
384 case HELP_LABELCLEAR
:
385 return (gettext("\tlabelclear [-f] <vdev>\n"));
387 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
388 "[-T d|u] [pool] ... \n"
389 "\t [interval [count]]\n"));
391 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
393 return (gettext("\tonline [-e] <pool> <device> ...\n"));
395 return (gettext("\treplace [-fsw] [-o property=value] "
396 "<pool> <device> [new-device]\n"));
398 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
400 return (gettext("\treopen [-n] <pool>\n"));
401 case HELP_INITIALIZE
:
402 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
403 "[<device> ...]\n"));
405 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
407 return (gettext("\tresilver <pool> ...\n"));
409 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
410 "[<device> ...]\n"));
412 return (gettext("\tstatus [-c [script1,script2,...]] "
413 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
414 "\t [interval [count]]\n"));
416 return (gettext("\tupgrade\n"
418 "\tupgrade [-V version] <-a | pool ...>\n"));
420 return (gettext("\tevents [-vHf [pool] | -c]\n"));
422 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
423 "<\"all\" | property[,...]> <pool> ...\n"));
425 return (gettext("\tset <property=value> <pool> \n"));
427 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
428 "\t [-o property=value] <pool> <newpool> "
429 "[<device> ...]\n"));
431 return (gettext("\treguid <pool>\n"));
433 return (gettext("\tsync [pool] ...\n"));
435 return (gettext("\tversion\n"));
437 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
438 "<pool> [interval]\n"));
440 __builtin_unreachable();
445 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
451 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
455 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
458 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
459 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
460 fnvlist_add_boolean(res
, path
);
466 for (i
= 0; i
< children
; i
++) {
467 zpool_collect_leaves(zhp
, child
[i
], res
);
472 * Callback routine that will print out a pool property value.
475 print_pool_prop_cb(int prop
, void *cb
)
479 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
481 if (zpool_prop_readonly(prop
))
482 (void) fprintf(fp
, " NO ");
484 (void) fprintf(fp
, " YES ");
486 if (zpool_prop_values(prop
) == NULL
)
487 (void) fprintf(fp
, "-\n");
489 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
495 * Callback routine that will print out a vdev property value.
498 print_vdev_prop_cb(int prop
, void *cb
)
502 (void) fprintf(fp
, "\t%-19s ", vdev_prop_to_name(prop
));
504 if (vdev_prop_readonly(prop
))
505 (void) fprintf(fp
, " NO ");
507 (void) fprintf(fp
, " YES ");
509 if (vdev_prop_values(prop
) == NULL
)
510 (void) fprintf(fp
, "-\n");
512 (void) fprintf(fp
, "%s\n", vdev_prop_values(prop
));
518 * Display usage message. If we're inside a command, display only the usage for
519 * that command. Otherwise, iterate over the entire command table and display
520 * a complete usage message.
523 usage(boolean_t requested
)
525 FILE *fp
= requested
? stdout
: stderr
;
527 if (current_command
== NULL
) {
530 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
532 gettext("where 'command' is one of the following:\n\n"));
534 for (i
= 0; i
< NCOMMAND
; i
++) {
535 if (command_table
[i
].name
== NULL
)
536 (void) fprintf(fp
, "\n");
538 (void) fprintf(fp
, "%s",
539 get_usage(command_table
[i
].usage
));
542 (void) fprintf(fp
, gettext("usage:\n"));
543 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
546 if (current_command
!= NULL
&&
547 current_prop_type
!= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
) &&
548 ((strcmp(current_command
->name
, "set") == 0) ||
549 (strcmp(current_command
->name
, "get") == 0) ||
550 (strcmp(current_command
->name
, "list") == 0))) {
553 gettext("\nthe following properties are supported:\n"));
555 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
556 "PROPERTY", "EDIT", "VALUES");
558 /* Iterate over all properties */
559 if (current_prop_type
== ZFS_TYPE_POOL
) {
560 (void) zprop_iter(print_pool_prop_cb
, fp
, B_FALSE
,
561 B_TRUE
, current_prop_type
);
563 (void) fprintf(fp
, "\t%-19s ", "feature@...");
564 (void) fprintf(fp
, "YES "
565 "disabled | enabled | active\n");
567 (void) fprintf(fp
, gettext("\nThe feature@ properties "
568 "must be appended with a feature name.\n"
569 "See zpool-features(7).\n"));
570 } else if (current_prop_type
== ZFS_TYPE_VDEV
) {
571 (void) zprop_iter(print_vdev_prop_cb
, fp
, B_FALSE
,
572 B_TRUE
, current_prop_type
);
577 * See comments at end of main().
579 if (getenv("ZFS_ABORT") != NULL
) {
580 (void) printf("dumping core by request\n");
584 exit(requested
? 0 : 2);
588 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
589 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
592 * -c Cancel. Ends active initializing.
593 * -s Suspend. Initializing can then be restarted with no flags.
594 * -w Wait. Blocks until initializing has completed.
597 zpool_do_initialize(int argc
, char **argv
)
604 boolean_t wait
= B_FALSE
;
606 struct option long_options
[] = {
607 {"cancel", no_argument
, NULL
, 'c'},
608 {"suspend", no_argument
, NULL
, 's'},
609 {"wait", no_argument
, NULL
, 'w'},
613 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
614 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
617 if (cmd_type
!= POOL_INITIALIZE_START
&&
618 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
619 (void) fprintf(stderr
, gettext("-c cannot be "
620 "combined with other options\n"));
623 cmd_type
= POOL_INITIALIZE_CANCEL
;
626 if (cmd_type
!= POOL_INITIALIZE_START
&&
627 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
628 (void) fprintf(stderr
, gettext("-s cannot be "
629 "combined with other options\n"));
632 cmd_type
= POOL_INITIALIZE_SUSPEND
;
639 (void) fprintf(stderr
,
640 gettext("invalid option '%c'\n"), optopt
);
642 (void) fprintf(stderr
,
643 gettext("invalid option '%s'\n"),
654 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
659 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
660 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
666 zhp
= zpool_open(g_zfs
, poolname
);
670 vdevs
= fnvlist_alloc();
672 /* no individual leaf vdevs specified, so add them all */
673 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
674 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
675 ZPOOL_CONFIG_VDEV_TREE
);
676 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
678 for (int i
= 1; i
< argc
; i
++) {
679 fnvlist_add_boolean(vdevs
, argv
[i
]);
684 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
686 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
695 * print a pool vdev config for dry runs
698 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
699 const char *match
, int name_flags
)
704 boolean_t printed
= B_FALSE
;
706 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
707 &child
, &children
) != 0) {
709 (void) printf("\t%*s%s\n", indent
, "", name
);
713 for (c
= 0; c
< children
; c
++) {
714 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
717 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
720 if (is_hole
== B_TRUE
) {
724 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
727 class = VDEV_ALLOC_BIAS_LOG
;
728 (void) nvlist_lookup_string(child
[c
],
729 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
730 if (strcmp(match
, class) != 0)
733 if (!printed
&& name
!= NULL
) {
734 (void) printf("\t%*s%s\n", indent
, "", name
);
737 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
738 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
745 * Print the list of l2cache devices for dry runs.
748 print_cache_list(nvlist_t
*nv
, int indent
)
753 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
754 &child
, &children
) == 0 && children
> 0) {
755 (void) printf("\t%*s%s\n", indent
, "", "cache");
759 for (c
= 0; c
< children
; c
++) {
762 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
763 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
769 * Print the list of spares for dry runs.
772 print_spare_list(nvlist_t
*nv
, int indent
)
777 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
778 &child
, &children
) == 0 && children
> 0) {
779 (void) printf("\t%*s%s\n", indent
, "", "spares");
783 for (c
= 0; c
< children
; c
++) {
786 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
787 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
793 prop_list_contains_feature(nvlist_t
*proplist
)
796 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
797 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
798 if (zpool_prop_feature(nvpair_name(nvp
)))
805 * Add a property pair (name, string-value) into a property nvlist.
808 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
811 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
816 if (*props
== NULL
&&
817 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
818 (void) fprintf(stderr
,
819 gettext("internal error: out of memory\n"));
826 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
828 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
830 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
831 (!zpool_prop_feature(propname
) &&
832 !zpool_prop_vdev(propname
))) {
833 (void) fprintf(stderr
, gettext("property '%s' is "
834 "not a valid pool or vdev property\n"), propname
);
839 * feature@ properties and version should not be specified
842 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
843 nvlist_exists(proplist
, vname
)) ||
844 (prop
== ZPOOL_PROP_VERSION
&&
845 prop_list_contains_feature(proplist
))) {
846 (void) fprintf(stderr
, gettext("'feature@' and "
847 "'version' properties cannot be specified "
853 * if version is specified, only "legacy" compatibility
856 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
857 strcmp(propval
, ZPOOL_COMPAT_LEGACY
) != 0 &&
858 nvlist_exists(proplist
, vname
)) ||
859 (prop
== ZPOOL_PROP_VERSION
&&
860 nvlist_exists(proplist
, cname
) &&
861 strcmp(fnvlist_lookup_string(proplist
, cname
),
862 ZPOOL_COMPAT_LEGACY
) != 0)) {
863 (void) fprintf(stderr
, gettext("when 'version' is "
864 "specified, the 'compatibility' feature may only "
865 "be set to '" ZPOOL_COMPAT_LEGACY
"'\n"));
869 if (zpool_prop_feature(propname
) || zpool_prop_vdev(propname
))
872 normnm
= zpool_prop_to_name(prop
);
874 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
876 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
878 normnm
= zfs_prop_to_name(fsprop
);
879 } else if (zfs_prop_user(propname
) ||
880 zfs_prop_userquota(propname
)) {
883 (void) fprintf(stderr
, gettext("property '%s' is "
884 "not a valid filesystem property\n"), propname
);
889 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
890 prop
!= ZPOOL_PROP_CACHEFILE
) {
891 (void) fprintf(stderr
, gettext("property '%s' "
892 "specified multiple times\n"), propname
);
896 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
897 (void) fprintf(stderr
, gettext("internal "
898 "error: out of memory\n"));
906 * Set a default property pair (name, string-value) in a property nvlist
909 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
)
913 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
916 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
920 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
922 * -f Force addition of devices, even if they appear in use
923 * -g Display guid for individual vdev name.
924 * -L Follow links when resolving vdev path name.
925 * -n Do not add the devices, but display the resulting layout if
926 * they were to be added.
927 * -o Set property=value.
928 * -P Display full path for vdev name.
930 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
931 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
935 zpool_do_add(int argc
, char **argv
)
937 boolean_t force
= B_FALSE
;
938 boolean_t dryrun
= B_FALSE
;
946 nvlist_t
*props
= NULL
;
950 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
956 name_flags
|= VDEV_NAME_GUID
;
959 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
965 if ((propval
= strchr(optarg
, '=')) == NULL
) {
966 (void) fprintf(stderr
, gettext("missing "
967 "'=' for -o option\n"));
973 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
974 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
978 name_flags
|= VDEV_NAME_PATH
;
981 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
990 /* get pool name and check number of arguments */
992 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
996 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1005 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1008 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
1009 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
1015 /* unless manually specified use "ashift" pool property (if set) */
1016 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
1019 char strval
[ZPOOL_MAXPROPLEN
];
1021 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
1022 if (src
!= ZPROP_SRC_DEFAULT
) {
1023 (void) sprintf(strval
, "%" PRId32
, intval
);
1024 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
1025 &props
, B_TRUE
) == 0);
1029 /* pass off to make_root_vdev for processing */
1030 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
1032 if (nvroot
== NULL
) {
1038 nvlist_t
*poolnvroot
;
1039 nvlist_t
**l2child
, **sparechild
;
1040 uint_t l2children
, sparechildren
, c
;
1042 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1044 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1047 (void) printf(gettext("would update '%s' to the following "
1048 "configuration:\n\n"), zpool_get_name(zhp
));
1050 /* print original main pool and new tree */
1051 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1052 name_flags
| VDEV_NAME_TYPE_ID
);
1053 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1055 /* print other classes: 'dedup', 'special', and 'log' */
1056 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1057 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1058 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1059 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1060 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1061 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1062 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1063 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1066 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1067 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1068 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1069 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1070 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1071 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1072 print_vdev_tree(zhp
, "special", nvroot
, 0,
1073 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1076 if (num_logs(poolnvroot
) > 0) {
1077 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1078 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1079 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1080 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1081 } else if (num_logs(nvroot
) > 0) {
1082 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1083 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1086 /* Do the same for the caches */
1087 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1088 &l2child
, &l2children
) == 0 && l2children
) {
1090 (void) printf(gettext("\tcache\n"));
1091 for (c
= 0; c
< l2children
; c
++) {
1092 vname
= zpool_vdev_name(g_zfs
, NULL
,
1093 l2child
[c
], name_flags
);
1094 (void) printf("\t %s\n", vname
);
1098 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1099 &l2child
, &l2children
) == 0 && l2children
) {
1101 (void) printf(gettext("\tcache\n"));
1102 for (c
= 0; c
< l2children
; c
++) {
1103 vname
= zpool_vdev_name(g_zfs
, NULL
,
1104 l2child
[c
], name_flags
);
1105 (void) printf("\t %s\n", vname
);
1109 /* And finally the spares */
1110 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1111 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1113 (void) printf(gettext("\tspares\n"));
1114 for (c
= 0; c
< sparechildren
; c
++) {
1115 vname
= zpool_vdev_name(g_zfs
, NULL
,
1116 sparechild
[c
], name_flags
);
1117 (void) printf("\t %s\n", vname
);
1121 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1122 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1124 (void) printf(gettext("\tspares\n"));
1125 for (c
= 0; c
< sparechildren
; c
++) {
1126 vname
= zpool_vdev_name(g_zfs
, NULL
,
1127 sparechild
[c
], name_flags
);
1128 (void) printf("\t %s\n", vname
);
1135 ret
= (zpool_add(zhp
, nvroot
) != 0);
1139 nvlist_free(nvroot
);
1146 * zpool remove [-npsw] <pool> <vdev> ...
1148 * Removes the given vdev from the pool.
1151 zpool_do_remove(int argc
, char **argv
)
1155 zpool_handle_t
*zhp
= NULL
;
1156 boolean_t stop
= B_FALSE
;
1158 boolean_t noop
= B_FALSE
;
1159 boolean_t parsable
= B_FALSE
;
1160 boolean_t wait
= B_FALSE
;
1163 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1178 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1187 /* get pool name and check number of arguments */
1189 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1195 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1199 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1205 (void) fprintf(stderr
, gettext("too many arguments\n"));
1208 if (zpool_vdev_remove_cancel(zhp
) != 0)
1211 (void) fprintf(stderr
, gettext("invalid option "
1212 "combination: -w cannot be used with -s\n"));
1217 (void) fprintf(stderr
, gettext("missing device\n"));
1221 for (i
= 1; i
< argc
; i
++) {
1225 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1231 (void) printf("%s %llu\n",
1232 argv
[i
], (unsigned long long)size
);
1235 zfs_nicenum(size
, valstr
,
1237 (void) printf("Memory that will be "
1238 "used after removing %s: %s\n",
1242 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1247 if (ret
== 0 && wait
)
1248 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1256 * Return 1 if a vdev is active (being used in a pool)
1257 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1259 * This is useful for checking if a disk in an active pool is offlined or
1263 vdev_is_active(char *vdev_path
)
1266 fd
= open(vdev_path
, O_EXCL
);
1268 return (1); /* cant open O_EXCL - disk is active */
1272 return (0); /* disk is inactive in the pool */
1276 * zpool labelclear [-f] <vdev>
1278 * -f Force clearing the label for the vdevs which are members of
1279 * the exported or foreign pools.
1281 * Verifies that the vdev is not active and zeros out the label information
1285 zpool_do_labelclear(int argc
, char **argv
)
1287 char vdev
[MAXPATHLEN
];
1290 int c
, fd
= -1, ret
= 0;
1293 boolean_t inuse
= B_FALSE
;
1294 boolean_t force
= B_FALSE
;
1297 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1303 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1314 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1318 (void) fprintf(stderr
, gettext("too many arguments\n"));
1323 * Check if we were given absolute path and use it as is.
1324 * Otherwise if the provided vdev name doesn't point to a file,
1325 * try prepending expected disk paths and partition numbers.
1327 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1328 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1331 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1332 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1333 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1337 if (error
|| (stat(vdev
, &st
) != 0)) {
1338 (void) fprintf(stderr
, gettext(
1339 "failed to find device %s, try specifying absolute "
1340 "path instead\n"), argv
[0]);
1345 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1346 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1347 vdev
, strerror(errno
));
1352 * Flush all dirty pages for the block device. This should not be
1353 * fatal when the device does not support BLKFLSBUF as would be the
1354 * case for a file vdev.
1356 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1357 (void) fprintf(stderr
, gettext("failed to invalidate "
1358 "cache for %s: %s\n"), vdev
, strerror(errno
));
1360 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1361 (void) fprintf(stderr
,
1362 gettext("failed to read label from %s\n"), vdev
);
1366 nvlist_free(config
);
1368 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1370 (void) fprintf(stderr
,
1371 gettext("failed to check state for %s\n"), vdev
);
1381 case POOL_STATE_ACTIVE
:
1382 case POOL_STATE_SPARE
:
1383 case POOL_STATE_L2CACHE
:
1385 * We allow the user to call 'zpool offline -f'
1386 * on an offlined disk in an active pool. We can check if
1387 * the disk is online by calling vdev_is_active().
1389 if (force
&& !vdev_is_active(vdev
))
1392 (void) fprintf(stderr
, gettext(
1393 "%s is a member (%s) of pool \"%s\""),
1394 vdev
, zpool_pool_state_to_name(state
), name
);
1397 (void) fprintf(stderr
, gettext(
1398 ". Offline the disk first to clear its label."));
1404 case POOL_STATE_EXPORTED
:
1407 (void) fprintf(stderr
, gettext(
1408 "use '-f' to override the following error:\n"
1409 "%s is a member of exported pool \"%s\"\n"),
1414 case POOL_STATE_POTENTIALLY_ACTIVE
:
1417 (void) fprintf(stderr
, gettext(
1418 "use '-f' to override the following error:\n"
1419 "%s is a member of potentially active pool \"%s\"\n"),
1424 case POOL_STATE_DESTROYED
:
1425 /* inuse should never be set for a destroyed pool */
1431 ret
= zpool_clear_label(fd
);
1433 (void) fprintf(stderr
,
1434 gettext("failed to clear label for %s\n"), vdev
);
1445 * zpool create [-fnd] [-o property=value] ...
1446 * [-O file-system-property=value] ...
1447 * [-R root] [-m mountpoint] <pool> <dev> ...
1449 * -f Force creation, even if devices appear in use
1450 * -n Do not create the pool, but display the resulting layout if it
1451 * were to be created.
1452 * -R Create a pool under an alternate root
1453 * -m Set default mountpoint for the root dataset. By default it's
1455 * -o Set property=value.
1456 * -o Set feature@feature=enabled|disabled.
1457 * -d Don't automatically enable all supported pool features
1458 * (individual features can be enabled with -o).
1459 * -O Set fsproperty=value in the pool's root file system
1461 * Creates the named pool according to the given vdev specification. The
1462 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1463 * Once we get the nvlist back from make_root_vdev(), we either print out the
1464 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1467 zpool_do_create(int argc
, char **argv
)
1469 boolean_t force
= B_FALSE
;
1470 boolean_t dryrun
= B_FALSE
;
1471 boolean_t enable_pool_features
= B_TRUE
;
1474 nvlist_t
*nvroot
= NULL
;
1478 char *altroot
= NULL
;
1479 char *compat
= NULL
;
1480 char *mountpoint
= NULL
;
1481 nvlist_t
*fsprops
= NULL
;
1482 nvlist_t
*props
= NULL
;
1486 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1495 enable_pool_features
= B_FALSE
;
1499 if (add_prop_list(zpool_prop_to_name(
1500 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1502 if (add_prop_list_default(zpool_prop_to_name(
1503 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1507 /* Equivalent to -O mountpoint=optarg */
1508 mountpoint
= optarg
;
1511 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1512 (void) fprintf(stderr
, gettext("missing "
1513 "'=' for -o option\n"));
1519 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1523 * If the user is creating a pool that doesn't support
1524 * feature flags, don't enable any features.
1526 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1530 ver
= strtoull(propval
, &end
, 10);
1532 ver
< SPA_VERSION_FEATURES
) {
1533 enable_pool_features
= B_FALSE
;
1536 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1538 if (zpool_name_to_prop(optarg
) ==
1539 ZPOOL_PROP_COMPATIBILITY
)
1543 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1544 (void) fprintf(stderr
, gettext("missing "
1545 "'=' for -O option\n"));
1552 * Mountpoints are checked and then added later.
1553 * Uniquely among properties, they can be specified
1554 * more than once, to avoid conflict with -m.
1556 if (0 == strcmp(optarg
,
1557 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1558 mountpoint
= propval
;
1559 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1566 * Sanity check temporary pool name.
1568 if (strchr(optarg
, '/') != NULL
) {
1569 (void) fprintf(stderr
, gettext("cannot create "
1570 "'%s': invalid character '/' in temporary "
1572 (void) fprintf(stderr
, gettext("use 'zfs "
1573 "create' to create a dataset\n"));
1577 if (add_prop_list(zpool_prop_to_name(
1578 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1580 if (add_prop_list_default(zpool_prop_to_name(
1581 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1586 (void) fprintf(stderr
, gettext("missing argument for "
1587 "'%c' option\n"), optopt
);
1590 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1599 /* get pool name and check number of arguments */
1601 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1605 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1612 * As a special case, check for use of '/' in the name, and direct the
1613 * user to use 'zfs create' instead.
1615 if (strchr(poolname
, '/') != NULL
) {
1616 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1617 "character '/' in pool name\n"), poolname
);
1618 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1619 "create a dataset\n"));
1623 /* pass off to make_root_vdev for bulk processing */
1624 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1625 argc
- 1, argv
+ 1);
1629 /* make_root_vdev() allows 0 toplevel children if there are spares */
1630 if (!zfs_allocatable_devs(nvroot
)) {
1631 (void) fprintf(stderr
, gettext("invalid vdev "
1632 "specification: at least one toplevel vdev must be "
1637 if (altroot
!= NULL
&& altroot
[0] != '/') {
1638 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1639 "must be an absolute path\n"), altroot
);
1644 * Check the validity of the mountpoint and direct the user to use the
1645 * '-m' mountpoint option if it looks like its in use.
1647 if (mountpoint
== NULL
||
1648 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1649 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1650 char buf
[MAXPATHLEN
];
1653 if (mountpoint
&& mountpoint
[0] != '/') {
1654 (void) fprintf(stderr
, gettext("invalid mountpoint "
1655 "'%s': must be an absolute path, 'legacy', or "
1656 "'none'\n"), mountpoint
);
1660 if (mountpoint
== NULL
) {
1661 if (altroot
!= NULL
)
1662 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1665 (void) snprintf(buf
, sizeof (buf
), "/%s",
1668 if (altroot
!= NULL
)
1669 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1670 altroot
, mountpoint
);
1672 (void) snprintf(buf
, sizeof (buf
), "%s",
1676 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1677 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1678 "%s\n"), buf
, strerror(errno
));
1679 (void) fprintf(stderr
, gettext("use '-m' "
1680 "option to provide a different default\n"));
1685 while (count
< 3 && readdir(dirp
) != NULL
)
1687 (void) closedir(dirp
);
1690 (void) fprintf(stderr
, gettext("mountpoint "
1691 "'%s' exists and is not empty\n"), buf
);
1692 (void) fprintf(stderr
, gettext("use '-m' "
1693 "option to provide a "
1694 "different default\n"));
1701 * Now that the mountpoint's validity has been checked, ensure that
1702 * the property is set appropriately prior to creating the pool.
1704 if (mountpoint
!= NULL
) {
1705 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1706 mountpoint
, &fsprops
, B_FALSE
);
1714 * For a dry run invocation, print out a basic message and run
1715 * through all the vdevs in the list and print out in an
1716 * appropriate hierarchy.
1718 (void) printf(gettext("would create '%s' with the "
1719 "following layout:\n\n"), poolname
);
1721 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1722 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1723 VDEV_ALLOC_BIAS_DEDUP
, 0);
1724 print_vdev_tree(NULL
, "special", nvroot
, 0,
1725 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1726 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1727 VDEV_ALLOC_BIAS_LOG
, 0);
1728 print_cache_list(nvroot
, 0);
1729 print_spare_list(nvroot
, 0);
1734 * Load in feature set.
1735 * Note: if compatibility property not given, we'll have
1736 * NULL, which means 'all features'.
1738 boolean_t requested_features
[SPA_FEATURES
];
1739 if (zpool_do_load_compat(compat
, requested_features
) !=
1740 ZPOOL_COMPATIBILITY_OK
)
1744 * props contains list of features to enable.
1746 * - remove it if feature@name=disabled
1747 * - leave it there if feature@name=enabled
1749 * - enable_pool_features (ie: no '-d' or '-o version')
1750 * - it's supported by the kernel module
1751 * - it's in the requested feature set
1752 * - warn if it's enabled but not in compat
1754 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1755 char propname
[MAXPATHLEN
];
1757 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1759 (void) snprintf(propname
, sizeof (propname
),
1760 "feature@%s", feat
->fi_uname
);
1762 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1763 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1764 (void) nvlist_remove_all(props
,
1767 ZFS_FEATURE_ENABLED
) == 0 &&
1768 !requested_features
[i
])
1769 (void) fprintf(stderr
, gettext(
1770 "Warning: feature \"%s\" enabled "
1771 "but is not in specified "
1772 "'compatibility' feature set.\n"),
1775 enable_pool_features
&&
1776 feat
->fi_zfs_mod_supported
&&
1777 requested_features
[i
]) {
1778 ret
= add_prop_list(propname
,
1779 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1786 if (zpool_create(g_zfs
, poolname
,
1787 nvroot
, props
, fsprops
) == 0) {
1788 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1789 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1791 if (zfs_mount(pool
, NULL
, 0) == 0) {
1792 ret
= zfs_shareall(pool
);
1793 zfs_commit_all_shares();
1797 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1798 (void) fprintf(stderr
, gettext("pool name may have "
1804 nvlist_free(nvroot
);
1805 nvlist_free(fsprops
);
1809 nvlist_free(fsprops
);
1816 * zpool destroy <pool>
1818 * -f Forcefully unmount any datasets
1820 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1823 zpool_do_destroy(int argc
, char **argv
)
1825 boolean_t force
= B_FALSE
;
1828 zpool_handle_t
*zhp
;
1832 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1838 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1847 /* check arguments */
1849 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1853 (void) fprintf(stderr
, gettext("too many arguments\n"));
1859 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1861 * As a special case, check for use of '/' in the name, and
1862 * direct the user to use 'zfs destroy' instead.
1864 if (strchr(pool
, '/') != NULL
)
1865 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1866 "destroy a dataset\n"));
1870 if (zpool_disable_datasets(zhp
, force
) != 0) {
1871 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1872 "could not unmount datasets\n"), zpool_get_name(zhp
));
1877 /* The history must be logged as part of the export */
1878 log_history
= B_FALSE
;
1880 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1887 typedef struct export_cbdata
{
1889 boolean_t hardforce
;
1896 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1898 export_cbdata_t
*cb
= data
;
1900 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1903 /* The history must be logged as part of the export */
1904 log_history
= B_FALSE
;
1906 if (cb
->hardforce
) {
1907 if (zpool_export_force(zhp
, history_str
) != 0)
1909 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1917 * zpool export [-f] <pool> ...
1919 * -a Export all pools
1920 * -f Forcefully unmount datasets
1922 * Export the given pools. By default, the command will attempt to cleanly
1923 * unmount any active datasets within the pool. If the '-f' flag is specified,
1924 * then the datasets will be forcefully unmounted.
1927 zpool_do_export(int argc
, char **argv
)
1930 boolean_t do_all
= B_FALSE
;
1931 boolean_t force
= B_FALSE
;
1932 boolean_t hardforce
= B_FALSE
;
1936 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1948 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1955 cb
.hardforce
= hardforce
;
1961 (void) fprintf(stderr
, gettext("too many arguments\n"));
1965 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1966 ZFS_TYPE_POOL
, B_FALSE
, zpool_export_one
, &cb
));
1969 /* check arguments */
1971 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1975 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
1976 B_FALSE
, zpool_export_one
, &cb
);
1982 * Given a vdev configuration, determine the maximum width needed for the device
1986 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1994 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1995 if (strlen(name
) + depth
> max
)
1996 max
= strlen(name
) + depth
;
2000 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2001 &child
, &children
) == 0) {
2002 for (c
= 0; c
< children
; c
++)
2003 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
2004 max
, name_flags
)) > max
)
2008 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2009 &child
, &children
) == 0) {
2010 for (c
= 0; c
< children
; c
++)
2011 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
2012 max
, name_flags
)) > max
)
2016 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2017 &child
, &children
) == 0) {
2018 for (c
= 0; c
< children
; c
++)
2019 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
2020 max
, name_flags
)) > max
)
2027 typedef struct spare_cbdata
{
2029 zpool_handle_t
*cb_zhp
;
2033 find_vdev(nvlist_t
*nv
, uint64_t search
)
2039 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
2043 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2044 &child
, &children
) == 0) {
2045 for (c
= 0; c
< children
; c
++)
2046 if (find_vdev(child
[c
], search
))
2054 find_spare(zpool_handle_t
*zhp
, void *data
)
2056 spare_cbdata_t
*cbp
= data
;
2057 nvlist_t
*config
, *nvroot
;
2059 config
= zpool_get_config(zhp
, NULL
);
2060 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2063 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
2072 typedef struct status_cbdata
{
2076 boolean_t cb_allpools
;
2077 boolean_t cb_verbose
;
2078 boolean_t cb_literal
;
2079 boolean_t cb_explain
;
2081 boolean_t cb_dedup_stats
;
2082 boolean_t cb_print_status
;
2083 boolean_t cb_print_slow_ios
;
2084 boolean_t cb_print_vdev_init
;
2085 boolean_t cb_print_vdev_trim
;
2086 vdev_cmd_data_list_t
*vcdl
;
2089 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2091 is_blank_str(char *str
)
2093 while (str
!= NULL
&& *str
!= '\0') {
2101 /* Print command output lines for specific vdev in a specific pool */
2103 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
2105 vdev_cmd_data_t
*data
;
2109 for (i
= 0; i
< vcdl
->count
; i
++) {
2110 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2111 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2112 /* Not the vdev we're looking for */
2116 data
= &vcdl
->data
[i
];
2117 /* Print out all the output values for this vdev */
2118 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2120 /* Does this vdev have values for this column? */
2121 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2122 if (strcmp(data
->cols
[k
],
2123 vcdl
->uniq_cols
[j
]) == 0) {
2124 /* yes it does, record the value */
2125 val
= data
->lines
[k
];
2130 * Mark empty values with dashes to make output
2133 if (val
== NULL
|| is_blank_str(val
))
2136 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2137 if (j
< vcdl
->uniq_cols_cnt
- 1)
2141 /* Print out any values that aren't in a column at the end */
2142 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2143 /* Did we have any columns? If so print a spacer. */
2144 if (vcdl
->uniq_cols_cnt
> 0)
2147 val
= data
->lines
[j
];
2148 printf("%s", val
? val
: "");
2155 * Print vdev initialization status for leaves
2158 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2161 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2162 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2163 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2164 !vs
->vs_scan_removing
) {
2167 struct tm zaction_ts
;
2169 time_t t
= vs
->vs_initialize_action_time
;
2170 int initialize_pct
= 100;
2171 if (vs
->vs_initialize_state
!=
2172 VDEV_INITIALIZE_COMPLETE
) {
2173 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2174 100 / (vs
->vs_initialize_bytes_est
+ 1));
2177 (void) localtime_r(&t
, &zaction_ts
);
2178 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2180 switch (vs
->vs_initialize_state
) {
2181 case VDEV_INITIALIZE_SUSPENDED
:
2182 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2183 gettext("suspended, started at"), tbuf
);
2185 case VDEV_INITIALIZE_ACTIVE
:
2186 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2187 gettext("started at"), tbuf
);
2189 case VDEV_INITIALIZE_COMPLETE
:
2190 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2191 gettext("completed at"), tbuf
);
2195 (void) printf(gettext(" (%d%% initialized%s)"),
2196 initialize_pct
, zbuf
);
2198 (void) printf(gettext(" (uninitialized)"));
2200 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2201 (void) printf(gettext(" (initializing)"));
2206 * Print vdev TRIM status for leaves
2209 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2212 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2213 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2214 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2215 !vs
->vs_scan_removing
) {
2218 struct tm zaction_ts
;
2220 time_t t
= vs
->vs_trim_action_time
;
2222 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2223 trim_pct
= (vs
->vs_trim_bytes_done
*
2224 100 / (vs
->vs_trim_bytes_est
+ 1));
2227 (void) localtime_r(&t
, &zaction_ts
);
2228 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2230 switch (vs
->vs_trim_state
) {
2231 case VDEV_TRIM_SUSPENDED
:
2232 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2233 gettext("suspended, started at"), tbuf
);
2235 case VDEV_TRIM_ACTIVE
:
2236 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2237 gettext("started at"), tbuf
);
2239 case VDEV_TRIM_COMPLETE
:
2240 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2241 gettext("completed at"), tbuf
);
2245 (void) printf(gettext(" (%d%% trimmed%s)"),
2247 } else if (vs
->vs_trim_notsup
) {
2248 (void) printf(gettext(" (trim unsupported)"));
2250 (void) printf(gettext(" (untrimmed)"));
2252 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2253 (void) printf(gettext(" (trimming)"));
2258 * Return the color associated with a health string. This includes returning
2259 * NULL for no color change.
2262 health_str_to_color(const char *health
)
2264 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2265 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2266 strcmp(health
, gettext("UNAVAIL")) == 0) {
2270 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2271 strcmp(health
, gettext("DEGRADED")) == 0 ||
2272 strcmp(health
, gettext("REMOVED")) == 0) {
2273 return (ANSI_YELLOW
);
2280 * Print out configuration state as requested by status_callback.
2283 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2284 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2286 nvlist_t
**child
, *root
;
2287 uint_t c
, i
, vsc
, children
;
2288 pool_scan_stat_t
*ps
= NULL
;
2290 char rbuf
[6], wbuf
[6], cbuf
[6];
2292 uint64_t notpresent
;
2293 spare_cbdata_t spare_cb
;
2297 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2299 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2300 &child
, &children
) != 0)
2303 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2304 (uint64_t **)&vs
, &vsc
) == 0);
2306 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2308 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2311 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2315 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2318 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2319 state
= gettext("INUSE");
2320 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2321 state
= gettext("AVAIL");
2324 printf_color(health_str_to_color(state
),
2325 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2329 if (vs
->vs_read_errors
)
2332 if (vs
->vs_write_errors
)
2335 if (vs
->vs_checksum_errors
)
2338 if (cb
->cb_literal
) {
2340 printf_color(rcolor
, "%5llu",
2341 (u_longlong_t
)vs
->vs_read_errors
);
2343 printf_color(wcolor
, "%5llu",
2344 (u_longlong_t
)vs
->vs_write_errors
);
2346 printf_color(ccolor
, "%5llu",
2347 (u_longlong_t
)vs
->vs_checksum_errors
);
2349 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2350 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2351 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2354 printf_color(rcolor
, "%5s", rbuf
);
2356 printf_color(wcolor
, "%5s", wbuf
);
2358 printf_color(ccolor
, "%5s", cbuf
);
2360 if (cb
->cb_print_slow_ios
) {
2361 if (children
== 0) {
2362 /* Only leafs vdevs have slow IOs */
2363 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2366 snprintf(rbuf
, sizeof (rbuf
), "-");
2370 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2372 printf(" %5s", rbuf
);
2376 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2377 ¬present
) == 0) {
2378 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2379 (void) printf(" %s %s", gettext("was"), path
);
2380 } else if (vs
->vs_aux
!= 0) {
2382 color_start(ANSI_RED
);
2383 switch (vs
->vs_aux
) {
2384 case VDEV_AUX_OPEN_FAILED
:
2385 (void) printf(gettext("cannot open"));
2388 case VDEV_AUX_BAD_GUID_SUM
:
2389 (void) printf(gettext("missing device"));
2392 case VDEV_AUX_NO_REPLICAS
:
2393 (void) printf(gettext("insufficient replicas"));
2396 case VDEV_AUX_VERSION_NEWER
:
2397 (void) printf(gettext("newer version"));
2400 case VDEV_AUX_UNSUP_FEAT
:
2401 (void) printf(gettext("unsupported feature(s)"));
2404 case VDEV_AUX_ASHIFT_TOO_BIG
:
2405 (void) printf(gettext("unsupported minimum blocksize"));
2408 case VDEV_AUX_SPARED
:
2409 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2410 &spare_cb
.cb_guid
) == 0);
2411 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2412 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2413 zpool_get_name(zhp
)) == 0)
2414 (void) printf(gettext("currently in "
2417 (void) printf(gettext("in use by "
2419 zpool_get_name(spare_cb
.cb_zhp
));
2420 zpool_close(spare_cb
.cb_zhp
);
2422 (void) printf(gettext("currently in use"));
2426 case VDEV_AUX_ERR_EXCEEDED
:
2427 (void) printf(gettext("too many errors"));
2430 case VDEV_AUX_IO_FAILURE
:
2431 (void) printf(gettext("experienced I/O failures"));
2434 case VDEV_AUX_BAD_LOG
:
2435 (void) printf(gettext("bad intent log"));
2438 case VDEV_AUX_EXTERNAL
:
2439 (void) printf(gettext("external device fault"));
2442 case VDEV_AUX_SPLIT_POOL
:
2443 (void) printf(gettext("split into new pool"));
2446 case VDEV_AUX_ACTIVE
:
2447 (void) printf(gettext("currently in use"));
2450 case VDEV_AUX_CHILDREN_OFFLINE
:
2451 (void) printf(gettext("all children offline"));
2454 case VDEV_AUX_BAD_LABEL
:
2455 (void) printf(gettext("invalid label"));
2459 (void) printf(gettext("corrupted data"));
2463 } else if (children
== 0 && !isspare
&&
2464 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2465 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2466 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2468 gettext(" block size: %dB configured, %dB native"),
2469 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2472 if (vs
->vs_scan_removing
!= 0) {
2473 (void) printf(gettext(" (removing)"));
2474 } else if (vs
->vs_noalloc
!= 0) {
2475 (void) printf(gettext(" (non-allocating)"));
2478 /* The root vdev has the scrub/resilver stats */
2479 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2480 ZPOOL_CONFIG_VDEV_TREE
);
2481 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2482 (uint64_t **)&ps
, &c
);
2484 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2485 if (vs
->vs_scan_processed
!= 0) {
2486 (void) printf(gettext(" (%s)"),
2487 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2488 "resilvering" : "repairing");
2489 } else if (vs
->vs_resilver_deferred
) {
2490 (void) printf(gettext(" (awaiting resilver)"));
2494 /* The top-level vdevs have the rebuild stats */
2495 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2497 if (vs
->vs_rebuild_processed
!= 0) {
2498 (void) printf(gettext(" (resilvering)"));
2502 if (cb
->vcdl
!= NULL
) {
2503 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2505 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2509 /* Display vdev initialization and trim status for leaves. */
2510 if (children
== 0) {
2511 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2512 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2515 (void) printf("\n");
2517 for (c
= 0; c
< children
; c
++) {
2518 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2520 /* Don't print logs or holes here */
2521 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2523 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2525 if (islog
|| ishole
)
2527 /* Only print normal classes here */
2528 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2531 /* Provide vdev_rebuild_stats to children if available */
2533 (void) nvlist_lookup_uint64_array(nv
,
2534 ZPOOL_CONFIG_REBUILD_STATS
,
2535 (uint64_t **)&vrs
, &i
);
2538 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2539 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2540 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2547 * Print the configuration of an exported pool. Iterate over all vdevs in the
2548 * pool, printing out the name and status for each one.
2551 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2559 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2560 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2561 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2564 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2565 (uint64_t **)&vs
, &c
) == 0);
2567 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2568 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2570 if (vs
->vs_aux
!= 0) {
2573 switch (vs
->vs_aux
) {
2574 case VDEV_AUX_OPEN_FAILED
:
2575 (void) printf(gettext("cannot open"));
2578 case VDEV_AUX_BAD_GUID_SUM
:
2579 (void) printf(gettext("missing device"));
2582 case VDEV_AUX_NO_REPLICAS
:
2583 (void) printf(gettext("insufficient replicas"));
2586 case VDEV_AUX_VERSION_NEWER
:
2587 (void) printf(gettext("newer version"));
2590 case VDEV_AUX_UNSUP_FEAT
:
2591 (void) printf(gettext("unsupported feature(s)"));
2594 case VDEV_AUX_ERR_EXCEEDED
:
2595 (void) printf(gettext("too many errors"));
2598 case VDEV_AUX_ACTIVE
:
2599 (void) printf(gettext("currently in use"));
2602 case VDEV_AUX_CHILDREN_OFFLINE
:
2603 (void) printf(gettext("all children offline"));
2606 case VDEV_AUX_BAD_LABEL
:
2607 (void) printf(gettext("invalid label"));
2611 (void) printf(gettext("corrupted data"));
2615 (void) printf("\n");
2617 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2618 &child
, &children
) != 0)
2621 for (c
= 0; c
< children
; c
++) {
2622 uint64_t is_log
= B_FALSE
;
2624 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2628 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2631 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2632 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2633 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2637 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2638 &child
, &children
) == 0) {
2639 (void) printf(gettext("\tcache\n"));
2640 for (c
= 0; c
< children
; c
++) {
2641 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2643 (void) printf("\t %s\n", vname
);
2648 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2649 &child
, &children
) == 0) {
2650 (void) printf(gettext("\tspares\n"));
2651 for (c
= 0; c
< children
; c
++) {
2652 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2654 (void) printf("\t %s\n", vname
);
2661 * Print specialized class vdevs.
2663 * These are recorded as top level vdevs in the main pool child array
2664 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2665 * print_status_config() or print_import_config() to print the top level
2666 * class vdevs then any of their children (eg mirrored slogs) are printed
2667 * recursively - which works because only the top level vdev is marked.
2670 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2675 boolean_t printed
= B_FALSE
;
2677 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2679 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2683 for (c
= 0; c
< children
; c
++) {
2684 uint64_t is_log
= B_FALSE
;
2688 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2692 bias
= VDEV_ALLOC_CLASS_LOGS
;
2694 (void) nvlist_lookup_string(child
[c
],
2695 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2696 (void) nvlist_lookup_string(child
[c
],
2697 ZPOOL_CONFIG_TYPE
, &type
);
2700 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2702 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2706 (void) printf("\t%s\t\n", gettext(class));
2710 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2711 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2712 if (cb
->cb_print_status
)
2713 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2716 print_import_config(cb
, name
, child
[c
], 2);
2722 * Display the status for the given pool.
2725 show_import(nvlist_t
*config
, boolean_t report_error
)
2727 uint64_t pool_state
;
2731 uint64_t hostid
= 0;
2733 char *hostname
= "unknown";
2734 nvlist_t
*nvroot
, *nvinfo
;
2735 zpool_status_t reason
;
2736 zpool_errata_t errata
;
2740 status_cbdata_t cb
= { 0 };
2742 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2744 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2746 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2748 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2751 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2752 (uint64_t **)&vs
, &vsc
) == 0);
2753 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2755 reason
= zpool_import_status(config
, &msgid
, &errata
);
2758 * If we're importing using a cachefile, then we won't report any
2759 * errors unless we are in the scan phase of the import.
2761 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
2764 (void) printf(gettext(" pool: %s\n"), name
);
2765 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2766 (void) printf(gettext(" state: %s"), health
);
2767 if (pool_state
== POOL_STATE_DESTROYED
)
2768 (void) printf(gettext(" (DESTROYED)"));
2769 (void) printf("\n");
2772 case ZPOOL_STATUS_MISSING_DEV_R
:
2773 case ZPOOL_STATUS_MISSING_DEV_NR
:
2774 case ZPOOL_STATUS_BAD_GUID_SUM
:
2775 printf_color(ANSI_BOLD
, gettext("status: "));
2776 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2777 "missing from the system.\n"));
2780 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2781 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2782 printf_color(ANSI_BOLD
, gettext("status: "));
2783 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2784 " corrupted data.\n"));
2787 case ZPOOL_STATUS_CORRUPT_DATA
:
2789 gettext(" status: The pool data is corrupted.\n"));
2792 case ZPOOL_STATUS_OFFLINE_DEV
:
2793 printf_color(ANSI_BOLD
, gettext("status: "));
2794 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2795 "are offlined.\n"));
2798 case ZPOOL_STATUS_CORRUPT_POOL
:
2799 printf_color(ANSI_BOLD
, gettext("status: "));
2800 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2804 case ZPOOL_STATUS_VERSION_OLDER
:
2805 printf_color(ANSI_BOLD
, gettext("status: "));
2806 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2807 "a legacy on-disk version.\n"));
2810 case ZPOOL_STATUS_VERSION_NEWER
:
2811 printf_color(ANSI_BOLD
, gettext("status: "));
2812 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2813 "an incompatible version.\n"));
2816 case ZPOOL_STATUS_FEAT_DISABLED
:
2817 printf_color(ANSI_BOLD
, gettext("status: "));
2818 printf_color(ANSI_YELLOW
, gettext("Some supported "
2819 "features are not enabled on the pool.\n\t"
2820 "(Note that they may be intentionally disabled "
2821 "if the\n\t'compatibility' property is set.)\n"));
2824 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2825 printf_color(ANSI_BOLD
, gettext("status: "));
2826 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2827 "the file(s) indicated by the 'compatibility'\n"
2831 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
2832 printf_color(ANSI_BOLD
, gettext("status: "));
2833 printf_color(ANSI_YELLOW
, gettext("One or more features "
2834 "are enabled on the pool despite not being\n"
2835 "requested by the 'compatibility' property.\n"));
2838 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2839 printf_color(ANSI_BOLD
, gettext("status: "));
2840 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2841 "feature(s) not supported on this system:\n"));
2842 color_start(ANSI_YELLOW
);
2843 zpool_print_unsup_feat(config
);
2847 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2848 printf_color(ANSI_BOLD
, gettext("status: "));
2849 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2850 "accessed in read-only mode on this system. It\n\tcannot be"
2851 " accessed in read-write mode because it uses the "
2852 "following\n\tfeature(s) not supported on this system:\n"));
2853 color_start(ANSI_YELLOW
);
2854 zpool_print_unsup_feat(config
);
2858 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2859 printf_color(ANSI_BOLD
, gettext("status: "));
2860 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2861 "imported by another system.\n"));
2864 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2865 printf_color(ANSI_BOLD
, gettext("status: "));
2866 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2867 "multihost property on. It cannot\n\tbe safely imported "
2868 "when the system hostid is not set.\n"));
2871 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2872 printf_color(ANSI_BOLD
, gettext("status: "));
2873 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2874 "by another system.\n"));
2877 case ZPOOL_STATUS_FAULTED_DEV_R
:
2878 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2879 printf_color(ANSI_BOLD
, gettext("status: "));
2880 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2884 case ZPOOL_STATUS_BAD_LOG
:
2885 printf_color(ANSI_BOLD
, gettext("status: "));
2886 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2890 case ZPOOL_STATUS_RESILVERING
:
2891 case ZPOOL_STATUS_REBUILDING
:
2892 printf_color(ANSI_BOLD
, gettext("status: "));
2893 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2894 "being resilvered.\n"));
2897 case ZPOOL_STATUS_ERRATA
:
2898 printf_color(ANSI_BOLD
, gettext("status: "));
2899 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2903 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2904 printf_color(ANSI_BOLD
, gettext("status: "));
2905 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2906 "configured to use a non-native block size.\n"
2907 "\tExpect reduced performance.\n"));
2912 * No other status can be seen when importing pools.
2914 assert(reason
== ZPOOL_STATUS_OK
);
2918 * Print out an action according to the overall state of the pool.
2920 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2921 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2922 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2923 (void) printf(gettext(" action: The pool can be "
2924 "imported using its name or numeric identifier, "
2925 "though\n\tsome features will not be available "
2926 "without an explicit 'zpool upgrade'.\n"));
2927 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
2928 (void) printf(gettext(" action: The pool can be "
2929 "imported using its name or numeric\n\tidentifier, "
2930 "though the file(s) indicated by its "
2931 "'compatibility'\n\tproperty cannot be parsed at "
2933 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2934 (void) printf(gettext(" action: The pool can be "
2935 "imported using its name or numeric "
2936 "identifier and\n\tthe '-f' flag.\n"));
2937 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2939 case ZPOOL_ERRATA_NONE
:
2942 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2943 (void) printf(gettext(" action: The pool can "
2944 "be imported using its name or numeric "
2945 "identifier,\n\thowever there is a compat"
2946 "ibility issue which should be corrected"
2947 "\n\tby running 'zpool scrub'\n"));
2950 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2951 (void) printf(gettext(" action: The pool can"
2952 "not be imported with this version of ZFS "
2953 "due to\n\tan active asynchronous destroy. "
2954 "Revert to an earlier version\n\tand "
2955 "allow the destroy to complete before "
2959 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2960 (void) printf(gettext(" action: Existing "
2961 "encrypted datasets contain an on-disk "
2962 "incompatibility, which\n\tneeds to be "
2963 "corrected. Backup these datasets to new "
2964 "encrypted datasets\n\tand destroy the "
2968 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2969 (void) printf(gettext(" action: Existing "
2970 "encrypted snapshots and bookmarks contain "
2971 "an on-disk\n\tincompatibility. This may "
2972 "cause on-disk corruption if they are used"
2973 "\n\twith 'zfs recv'. To correct the "
2974 "issue, enable the bookmark_v2 feature.\n\t"
2975 "No additional action is needed if there "
2976 "are no encrypted snapshots or\n\t"
2977 "bookmarks. If preserving the encrypted "
2978 "snapshots and bookmarks is\n\trequired, "
2979 "use a non-raw send to backup and restore "
2980 "them. Alternately,\n\tthey may be removed"
2981 " to resolve the incompatibility.\n"));
2985 * All errata must contain an action message.
2990 (void) printf(gettext(" action: The pool can be "
2991 "imported using its name or numeric "
2994 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2995 (void) printf(gettext(" action: The pool can be imported "
2996 "despite missing or damaged devices. The\n\tfault "
2997 "tolerance of the pool may be compromised if imported.\n"));
3000 case ZPOOL_STATUS_VERSION_NEWER
:
3001 (void) printf(gettext(" action: The pool cannot be "
3002 "imported. Access the pool on a system running "
3003 "newer\n\tsoftware, or recreate the pool from "
3006 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
3007 printf_color(ANSI_BOLD
, gettext("action: "));
3008 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3009 "imported. Access the pool on a system that "
3010 "supports\n\tthe required feature(s), or recreate "
3011 "the pool from backup.\n"));
3013 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3014 printf_color(ANSI_BOLD
, gettext("action: "));
3015 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3016 "imported in read-write mode. Import the pool "
3018 "\t\"-o readonly=on\", access the pool on a system "
3019 "that supports the\n\trequired feature(s), or "
3020 "recreate the pool from backup.\n"));
3022 case ZPOOL_STATUS_MISSING_DEV_R
:
3023 case ZPOOL_STATUS_MISSING_DEV_NR
:
3024 case ZPOOL_STATUS_BAD_GUID_SUM
:
3025 (void) printf(gettext(" action: The pool cannot be "
3026 "imported. Attach the missing\n\tdevices and try "
3029 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3030 VERIFY0(nvlist_lookup_nvlist(config
,
3031 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
3033 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3034 hostname
= fnvlist_lookup_string(nvinfo
,
3035 ZPOOL_CONFIG_MMP_HOSTNAME
);
3037 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3038 hostid
= fnvlist_lookup_uint64(nvinfo
,
3039 ZPOOL_CONFIG_MMP_HOSTID
);
3041 (void) printf(gettext(" action: The pool must be "
3042 "exported from %s (hostid=%lx)\n\tbefore it "
3043 "can be safely imported.\n"), hostname
,
3044 (unsigned long) hostid
);
3046 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3047 (void) printf(gettext(" action: Set a unique system "
3048 "hostid with the zgenhostid(8) command.\n"));
3051 (void) printf(gettext(" action: The pool cannot be "
3052 "imported due to damaged devices or data.\n"));
3056 /* Print the comment attached to the pool. */
3057 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3058 (void) printf(gettext("comment: %s\n"), comment
);
3061 * If the state is "closed" or "can't open", and the aux state
3062 * is "corrupt data":
3064 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
3065 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
3066 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
3067 if (pool_state
== POOL_STATE_DESTROYED
)
3068 (void) printf(gettext("\tThe pool was destroyed, "
3069 "but can be imported using the '-Df' flags.\n"));
3070 else if (pool_state
!= POOL_STATE_EXPORTED
)
3071 (void) printf(gettext("\tThe pool may be active on "
3072 "another system, but can be imported using\n\t"
3073 "the '-f' flag.\n"));
3076 if (msgid
!= NULL
) {
3077 (void) printf(gettext(
3078 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3082 (void) printf(gettext(" config:\n\n"));
3084 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
3086 if (cb
.cb_namewidth
< 10)
3087 cb
.cb_namewidth
= 10;
3089 print_import_config(&cb
, name
, nvroot
, 0);
3091 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
3092 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
3093 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
3095 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
3096 (void) printf(gettext("\n\tAdditional devices are known to "
3097 "be part of this pool, though their\n\texact "
3098 "configuration cannot be determined.\n"));
3104 zfs_force_import_required(nvlist_t
*config
)
3107 uint64_t hostid
= 0;
3110 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
3111 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
3113 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3116 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3117 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3118 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3119 ZPOOL_CONFIG_MMP_STATE
);
3121 if (mmp_state
!= MMP_STATE_INACTIVE
)
3129 * Perform the import for the given configuration. This passes the heavy
3130 * lifting off to zpool_import_props(), and then mounts the datasets contained
3134 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3135 nvlist_t
*props
, int flags
)
3138 zpool_handle_t
*zhp
;
3142 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3143 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3145 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3146 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3147 "is formatted using an unsupported ZFS version\n"), name
);
3149 } else if (zfs_force_import_required(config
) &&
3150 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3151 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3154 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3155 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3156 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3157 ZPOOL_CONFIG_MMP_STATE
);
3159 if (mmp_state
== MMP_STATE_ACTIVE
) {
3160 char *hostname
= "<unknown>";
3161 uint64_t hostid
= 0;
3163 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3164 hostname
= fnvlist_lookup_string(nvinfo
,
3165 ZPOOL_CONFIG_MMP_HOSTNAME
);
3167 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3168 hostid
= fnvlist_lookup_uint64(nvinfo
,
3169 ZPOOL_CONFIG_MMP_HOSTID
);
3171 (void) fprintf(stderr
, gettext("cannot import '%s': "
3172 "pool is imported on %s (hostid: "
3173 "0x%lx)\nExport the pool on the other system, "
3174 "then run 'zpool import'.\n"),
3175 name
, hostname
, (unsigned long) hostid
);
3176 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3177 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3178 "pool has the multihost property on and the\n"
3179 "system's hostid is not set. Set a unique hostid "
3180 "with the zgenhostid(8) command.\n"), name
);
3182 char *hostname
= "<unknown>";
3183 uint64_t timestamp
= 0;
3184 uint64_t hostid
= 0;
3186 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3187 hostname
= fnvlist_lookup_string(config
,
3188 ZPOOL_CONFIG_HOSTNAME
);
3190 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3191 timestamp
= fnvlist_lookup_uint64(config
,
3192 ZPOOL_CONFIG_TIMESTAMP
);
3194 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3195 hostid
= fnvlist_lookup_uint64(config
,
3196 ZPOOL_CONFIG_HOSTID
);
3198 (void) fprintf(stderr
, gettext("cannot import '%s': "
3199 "pool was previously in use from another system.\n"
3200 "Last accessed by %s (hostid=%lx) at %s"
3201 "The pool can be imported, use 'zpool import -f' "
3202 "to import the pool.\n"), name
, hostname
,
3203 (unsigned long)hostid
, ctime((time_t *)×tamp
));
3209 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3212 if (newname
!= NULL
)
3213 name
= (char *)newname
;
3215 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3219 * Loading keys is best effort. We don't want to return immediately
3220 * if it fails but we do want to give the error to the caller.
3222 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
3223 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
3228 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3229 !(flags
& ZFS_IMPORT_ONLY
) &&
3230 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3240 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3241 char *orig_name
, char *new_name
,
3242 boolean_t do_destroyed
, boolean_t pool_specified
, boolean_t do_all
,
3243 importargs_t
*import
)
3245 nvlist_t
*config
= NULL
;
3246 nvlist_t
*found_config
= NULL
;
3247 uint64_t pool_state
;
3250 * At this point we have a list of import candidate configs. Even if
3251 * we were searching by pool name or guid, we still need to
3252 * post-process the list to deal with pool state and possible
3256 nvpair_t
*elem
= NULL
;
3257 boolean_t first
= B_TRUE
;
3258 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3260 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3262 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3264 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3266 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3269 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3270 import
->policy
) == 0);
3272 if (!pool_specified
) {
3276 (void) printf("\n");
3279 err
|= do_import(config
, NULL
, mntopts
,
3283 * If we're importing from cachefile, then
3284 * we don't want to report errors until we
3285 * are in the scan phase of the import. If
3286 * we get an error, then we return that error
3287 * to invoke the scan phase.
3289 if (import
->cachefile
&& !import
->scan
)
3290 err
= show_import(config
, B_FALSE
);
3292 (void) show_import(config
, B_TRUE
);
3294 } else if (import
->poolname
!= NULL
) {
3298 * We are searching for a pool based on name.
3300 verify(nvlist_lookup_string(config
,
3301 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3303 if (strcmp(name
, import
->poolname
) == 0) {
3304 if (found_config
!= NULL
) {
3305 (void) fprintf(stderr
, gettext(
3306 "cannot import '%s': more than "
3307 "one matching pool\n"),
3309 (void) fprintf(stderr
, gettext(
3310 "import by numeric ID instead\n"));
3313 found_config
= config
;
3319 * Search for a pool by guid.
3321 verify(nvlist_lookup_uint64(config
,
3322 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3324 if (guid
== import
->guid
)
3325 found_config
= config
;
3330 * If we were searching for a specific pool, verify that we found a
3331 * pool, and then do the import.
3333 if (pool_specified
&& err
== 0) {
3334 if (found_config
== NULL
) {
3335 (void) fprintf(stderr
, gettext("cannot import '%s': "
3336 "no such pool available\n"), orig_name
);
3339 err
|= do_import(found_config
, new_name
,
3340 mntopts
, props
, flags
);
3345 * If we were just looking for pools, report an error if none were
3348 if (!pool_specified
&& first
)
3349 (void) fprintf(stderr
,
3350 gettext("no pools available to import\n"));
3354 typedef struct target_exists_args
{
3355 const char *poolname
;
3357 } target_exists_args_t
;
3360 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3362 target_exists_args_t
*args
= data
;
3363 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3369 if (args
->poolname
!= NULL
) {
3372 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3374 if (strcmp(pool_name
, args
->poolname
) == 0)
3379 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3381 if (pool_guid
== args
->poolguid
)
3389 * zpool checkpoint <pool>
3390 * checkpoint --discard <pool>
3392 * -d Discard the checkpoint from a checkpointed
3395 * -w Wait for discarding a checkpoint to complete.
3398 * Checkpoints the specified pool, by taking a "snapshot" of its
3399 * current state. A pool can only have one checkpoint at a time.
3402 zpool_do_checkpoint(int argc
, char **argv
)
3404 boolean_t discard
, wait
;
3406 zpool_handle_t
*zhp
;
3409 struct option long_options
[] = {
3410 {"discard", no_argument
, NULL
, 'd'},
3411 {"wait", no_argument
, NULL
, 'w'},
3417 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3426 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3432 if (wait
&& !discard
) {
3433 (void) fprintf(stderr
, gettext("--wait only valid when "
3434 "--discard also specified\n"));
3442 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3447 (void) fprintf(stderr
, gettext("too many arguments\n"));
3453 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3454 /* As a special case, check for use of '/' in the name */
3455 if (strchr(pool
, '/') != NULL
)
3456 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3457 "doesn't work on datasets. To save the state "
3458 "of a dataset from a specific point in time "
3459 "please use 'zfs snapshot'\n"));
3464 err
= (zpool_discard_checkpoint(zhp
) != 0);
3465 if (err
== 0 && wait
)
3466 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3468 err
= (zpool_checkpoint(zhp
) != 0);
3476 #define CHECKPOINT_OPT 1024
3479 * zpool import [-d dir] [-D]
3480 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3481 * [-d dir | -c cachefile | -s] [-f] -a
3482 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3483 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3486 * -c Read pool information from a cachefile instead of searching
3487 * devices. If importing from a cachefile config fails, then
3488 * fallback to searching for devices only in the directories that
3489 * exist in the cachefile.
3491 * -d Scan in a specific directory, other than /dev/. More than
3492 * one directory can be specified using multiple '-d' options.
3494 * -D Scan for previously destroyed pools or import all or only
3495 * specified destroyed pools.
3497 * -R Temporarily import the pool, with all mountpoints relative to
3498 * the given root. The pool will remain exported when the machine
3501 * -V Import even in the presence of faulted vdevs. This is an
3502 * intentionally undocumented option for testing purposes, and
3503 * treats the pool configuration as complete, leaving any bad
3504 * vdevs in the FAULTED state. In other words, it does verbatim
3507 * -f Force import, even if it appears that the pool is active.
3509 * -F Attempt rewind if necessary.
3511 * -n See if rewind would work, but don't actually rewind.
3513 * -N Import the pool but don't mount datasets.
3515 * -T Specify a starting txg to use for import. This option is
3516 * intentionally undocumented option for testing purposes.
3518 * -a Import all pools found.
3520 * -l Load encryption keys while importing.
3522 * -o Set property=value and/or temporary mount options (without '=').
3524 * -s Scan using the default search path, the libblkid cache will
3527 * --rewind-to-checkpoint
3528 * Import the pool and revert back to the checkpoint.
3530 * The import command scans for pools to import, and import pools based on pool
3531 * name and GUID. The pool can also be renamed as part of the import process.
3534 zpool_do_import(int argc
, char **argv
)
3536 char **searchdirs
= NULL
;
3537 char *env
, *envdup
= NULL
;
3541 nvlist_t
*pools
= NULL
;
3542 boolean_t do_all
= B_FALSE
;
3543 boolean_t do_destroyed
= B_FALSE
;
3544 char *mntopts
= NULL
;
3545 uint64_t searchguid
= 0;
3546 char *searchname
= NULL
;
3548 nvlist_t
*policy
= NULL
;
3549 nvlist_t
*props
= NULL
;
3550 int flags
= ZFS_IMPORT_NORMAL
;
3551 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3552 boolean_t dryrun
= B_FALSE
;
3553 boolean_t do_rewind
= B_FALSE
;
3554 boolean_t xtreme_rewind
= B_FALSE
;
3555 boolean_t do_scan
= B_FALSE
;
3556 boolean_t pool_exists
= B_FALSE
;
3557 boolean_t pool_specified
= B_FALSE
;
3558 uint64_t txg
= -1ULL;
3559 char *cachefile
= NULL
;
3560 importargs_t idata
= { 0 };
3563 struct option long_options
[] = {
3564 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3569 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3570 long_options
, NULL
)) != -1) {
3579 searchdirs
= safe_realloc(searchdirs
,
3580 (nsearch
+ 1) * sizeof (char *));
3581 searchdirs
[nsearch
++] = optarg
;
3584 do_destroyed
= B_TRUE
;
3587 flags
|= ZFS_IMPORT_ANY_HOST
;
3593 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3596 flags
|= ZFS_IMPORT_MISSING_LOG
;
3602 flags
|= ZFS_IMPORT_ONLY
;
3605 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3608 if (add_prop_list(optarg
, propval
,
3616 if (add_prop_list(zpool_prop_to_name(
3617 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3619 if (add_prop_list_default(zpool_prop_to_name(
3620 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3627 flags
|= ZFS_IMPORT_TEMP_NAME
;
3628 if (add_prop_list_default(zpool_prop_to_name(
3629 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3635 txg
= strtoull(optarg
, &endptr
, 0);
3636 if (errno
!= 0 || *endptr
!= '\0') {
3637 (void) fprintf(stderr
,
3638 gettext("invalid txg value\n"));
3641 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3644 flags
|= ZFS_IMPORT_VERBATIM
;
3647 xtreme_rewind
= B_TRUE
;
3649 case CHECKPOINT_OPT
:
3650 flags
|= ZFS_IMPORT_CHECKPOINT
;
3653 (void) fprintf(stderr
, gettext("missing argument for "
3654 "'%c' option\n"), optopt
);
3658 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3667 if (cachefile
&& nsearch
!= 0) {
3668 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3672 if (cachefile
&& do_scan
) {
3673 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
3677 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3678 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3682 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3683 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3688 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3689 (void) fprintf(stderr
,
3690 gettext("-n or -X only meaningful with -F\n"));
3694 rewind_policy
= ZPOOL_TRY_REWIND
;
3696 rewind_policy
= ZPOOL_DO_REWIND
;
3698 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3700 /* In the future, we can capture further policy and include it here */
3701 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3702 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3703 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3704 rewind_policy
) != 0)
3707 /* check argument count */
3710 (void) fprintf(stderr
, gettext("too many arguments\n"));
3715 (void) fprintf(stderr
, gettext("too many arguments\n"));
3721 * Check for the effective uid. We do this explicitly here because
3722 * otherwise any attempt to discover pools will silently fail.
3724 if (argc
== 0 && geteuid() != 0) {
3725 (void) fprintf(stderr
, gettext("cannot "
3726 "discover pools: permission denied\n"));
3727 if (searchdirs
!= NULL
)
3731 nvlist_free(policy
);
3736 * Depending on the arguments given, we do one of the following:
3738 * <none> Iterate through all pools and display information about
3741 * -a Iterate through all pools and try to import each one.
3743 * <id> Find the pool that corresponds to the given GUID/pool
3744 * name and import that one.
3746 * -D Above options applies only to destroyed pools.
3752 searchguid
= strtoull(argv
[0], &endptr
, 10);
3753 if (errno
!= 0 || *endptr
!= '\0') {
3754 searchname
= argv
[0];
3757 pool_specified
= B_TRUE
;
3760 * User specified a name or guid. Ensure it's unique.
3762 target_exists_args_t search
= {searchname
, searchguid
};
3763 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3767 * Check the environment for the preferred search path.
3769 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3770 char *dir
, *tmp
= NULL
;
3772 envdup
= strdup(env
);
3774 for (dir
= strtok_r(envdup
, ":", &tmp
);
3776 dir
= strtok_r(NULL
, ":", &tmp
)) {
3777 searchdirs
= safe_realloc(searchdirs
,
3778 (nsearch
+ 1) * sizeof (char *));
3779 searchdirs
[nsearch
++] = dir
;
3783 idata
.path
= searchdirs
;
3784 idata
.paths
= nsearch
;
3785 idata
.poolname
= searchname
;
3786 idata
.guid
= searchguid
;
3787 idata
.cachefile
= cachefile
;
3788 idata
.scan
= do_scan
;
3789 idata
.policy
= policy
;
3791 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3793 if (pools
!= NULL
&& pool_exists
&&
3794 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3795 (void) fprintf(stderr
, gettext("cannot import '%s': "
3796 "a pool with that name already exists\n"),
3798 (void) fprintf(stderr
, gettext("use the form '%s "
3799 "<pool | id> <newpool>' to give it a new name\n"),
3802 } else if (pools
== NULL
&& pool_exists
) {
3803 (void) fprintf(stderr
, gettext("cannot import '%s': "
3804 "a pool with that name is already created/imported,\n"),
3806 (void) fprintf(stderr
, gettext("and no additional pools "
3807 "with that name were found\n"));
3809 } else if (pools
== NULL
) {
3811 (void) fprintf(stderr
, gettext("cannot import '%s': "
3812 "no such pool available\n"), argv
[0]);
3820 nvlist_free(policy
);
3826 err
= import_pools(pools
, props
, mntopts
, flags
,
3827 argc
>= 1 ? argv
[0] : NULL
,
3828 argc
>= 2 ? argv
[1] : NULL
,
3829 do_destroyed
, pool_specified
, do_all
, &idata
);
3832 * If we're using the cachefile and we failed to import, then
3833 * fallback to scanning the directory for pools that match
3834 * those in the cachefile.
3836 if (err
!= 0 && cachefile
!= NULL
) {
3837 (void) printf(gettext("cachefile import failed, retrying\n"));
3840 * We use the scan flag to gather the directories that exist
3841 * in the cachefile. If we need to fallback to searching for
3842 * the pool config, we will only search devices in these
3845 idata
.scan
= B_TRUE
;
3847 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3849 err
= import_pools(pools
, props
, mntopts
, flags
,
3850 argc
>= 1 ? argv
[0] : NULL
,
3851 argc
>= 2 ? argv
[1] : NULL
,
3852 do_destroyed
, pool_specified
, do_all
, &idata
);
3858 nvlist_free(policy
);
3862 return (err
? 1 : 0);
3866 * zpool sync [-f] [pool] ...
3868 * -f (undocumented) force uberblock (and config including zpool cache file)
3871 * Sync the specified pool(s).
3872 * Without arguments "zpool sync" will sync all pools.
3873 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3877 zpool_do_sync(int argc
, char **argv
)
3880 boolean_t force
= B_FALSE
;
3883 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3889 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3898 /* if argc == 0 we will execute zpool_sync_one on all pools */
3899 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
3900 B_FALSE
, zpool_sync_one
, &force
);
3905 typedef struct iostat_cbdata
{
3909 boolean_t cb_verbose
;
3910 boolean_t cb_literal
;
3911 boolean_t cb_scripted
;
3912 zpool_list_t
*cb_list
;
3913 vdev_cmd_data_list_t
*vcdl
;
3914 vdev_cbdata_t cb_vdevs
;
3918 typedef struct name_and_columns
{
3919 const char *name
; /* Column name */
3920 unsigned int columns
; /* Center name to this number of columns */
3921 } name_and_columns_t
;
3923 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
3925 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3927 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3929 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3930 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3932 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3933 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3934 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL
}},
3935 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3936 {"asyncq_wait", 2}, {NULL
}},
3937 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3938 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3939 {"trim", 2}, {"rebuild", 2}, {NULL
}},
3942 /* Shorthand - if "columns" field not set, default to 1 column */
3943 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3945 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3947 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3948 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3950 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3951 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3952 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
3953 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3954 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3956 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3957 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3958 {"ind"}, {"agg"}, {NULL
}},
3961 static const char *histo_to_title
[] = {
3962 [IOS_L_HISTO
] = "latency",
3963 [IOS_RQ_HISTO
] = "req_size",
3967 * Return the number of labels in a null-terminated name_and_columns_t
3972 label_array_len(const name_and_columns_t
*labels
)
3976 while (labels
[i
].name
)
3983 * Return the number of strings in a null-terminated string array.
3986 * const char foo[] = {"bar", "baz", NULL}
3991 str_array_len(const char *array
[])
4002 * Return a default column width for default/latency/queue columns. This does
4003 * not include histograms, which have their columns autosized.
4006 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
4008 unsigned long column_width
= 5; /* Normal niceprint */
4009 static unsigned long widths
[] = {
4011 * Choose some sane default column sizes for printing the
4014 [IOS_DEFAULT
] = 15, /* 1PB capacity */
4015 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
4016 [IOS_QUEUES
] = 6, /* 1M queue entries */
4017 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
4018 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
4022 column_width
= widths
[type
];
4024 return (column_width
);
4028 * Print the column labels, i.e:
4030 * capacity operations bandwidth
4031 * alloc free read write read write ...
4033 * If force_column_width is set, use it for the column width. If not set, use
4034 * the default column width.
4037 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4038 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
4041 int text_start
, rw_column_width
, spaces_to_end
;
4042 uint64_t flags
= cb
->cb_flags
;
4044 unsigned int column_width
= force_column_width
;
4046 /* For each bit set in flags */
4047 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4048 idx
= lowbit64(f
) - 1;
4049 if (!force_column_width
)
4050 column_width
= default_column_width(cb
, idx
);
4051 /* Print our top labels centered over "read write" label. */
4052 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
4053 const char *name
= labels
[idx
][i
].name
;
4055 * We treat labels[][].columns == 0 as shorthand
4056 * for one column. It makes writing out the label
4057 * tables more concise.
4059 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
4060 unsigned int slen
= strlen(name
);
4062 rw_column_width
= (column_width
* columns
) +
4063 (2 * (columns
- 1));
4065 text_start
= (int)((rw_column_width
) / columns
-
4070 printf(" "); /* Two spaces between columns */
4072 /* Space from beginning of column to label */
4073 for (s
= 0; s
< text_start
; s
++)
4078 /* Print space after label to end of column */
4079 spaces_to_end
= rw_column_width
- text_start
- slen
;
4080 if (spaces_to_end
< 0)
4083 for (s
= 0; s
< spaces_to_end
; s
++)
4091 * print_cmd_columns - Print custom column titles from -c
4093 * If the user specified the "zpool status|iostat -c" then print their custom
4094 * column titles in the header. For example, print_cmd_columns() would print
4095 * the " col1 col2" part of this:
4097 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4099 * capacity operations bandwidth
4100 * pool alloc free read write read write col1 col2
4101 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4102 * mypool 269K 1008M 0 0 107 946
4103 * mirror 269K 1008M 0 0 107 946
4104 * sdb - - 0 0 102 473 val1 val2
4105 * sdc - - 0 0 5 473 val1 val2
4106 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4109 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4112 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4114 if (vcdl
->count
== 0 || data
== NULL
)
4118 * Each vdev cmd should have the same column names unless the user did
4119 * something weird with their cmd. Just take the column names from the
4120 * first vdev and assume it works for all of them.
4122 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4125 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4128 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4129 vcdl
->uniq_cols
[i
]);
4136 * Utility function to print out a line of dashes like:
4138 * -------------------------------- ----- ----- ----- ----- -----
4140 * ...or a dashed named-row line like:
4146 * @force_column_width If non-zero, use the value as the column width.
4147 * Otherwise use the default column widths.
4149 * @name: Print a dashed named-row line starting
4150 * with @name. Otherwise, print a regular
4154 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4158 unsigned int namewidth
;
4159 uint64_t flags
= cb
->cb_flags
;
4162 const name_and_columns_t
*labels
;
4166 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4167 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4168 } else if (cb
->cb_vdevs
.cb_names_count
) {
4174 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4175 name
? strlen(name
) : 0);
4179 printf("%-*s", namewidth
, name
);
4181 for (i
= 0; i
< namewidth
; i
++)
4185 /* For each bit in flags */
4186 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4187 unsigned int column_width
;
4188 idx
= lowbit64(f
) - 1;
4189 if (force_column_width
)
4190 column_width
= force_column_width
;
4192 column_width
= default_column_width(cb
, idx
);
4194 labels
= iostat_bottom_labels
[idx
];
4195 for (i
= 0; i
< label_array_len(labels
); i
++) {
4197 printf(" %*s-", column_width
- 1, " ");
4199 printf(" %.*s", column_width
,
4200 "--------------------");
4207 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4208 unsigned int force_column_width
)
4210 print_iostat_dashes(cb
, force_column_width
, NULL
);
4214 print_iostat_separator(iostat_cbdata_t
*cb
)
4216 print_iostat_separator_impl(cb
, 0);
4220 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4221 const char *histo_vdev_name
)
4223 unsigned int namewidth
;
4226 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4227 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4228 } else if (cb
->cb_vdevs
.cb_names_count
) {
4234 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4235 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4237 if (histo_vdev_name
)
4238 printf("%-*s", namewidth
, histo_vdev_name
);
4240 printf("%*s", namewidth
, "");
4243 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4246 printf("%-*s", namewidth
, title
);
4248 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4249 if (cb
->vcdl
!= NULL
)
4250 print_cmd_columns(cb
->vcdl
, 0);
4254 print_iostat_separator_impl(cb
, force_column_width
);
4256 if (cb
->vcdl
!= NULL
)
4257 print_cmd_columns(cb
->vcdl
, 1);
4263 print_iostat_header(iostat_cbdata_t
*cb
)
4265 print_iostat_header_impl(cb
, 0, NULL
);
4270 * Display a single statistic.
4273 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4274 unsigned int column_size
, boolean_t scripted
)
4278 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4281 printf("\t%s", buf
);
4283 printf(" %*s", column_size
, buf
);
4287 * Calculate the default vdev stats
4289 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4290 * stats into calcvs.
4293 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4294 vdev_stat_t
*calcvs
)
4298 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4299 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4300 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4302 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4303 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4307 * Internal representation of the extended iostats data.
4309 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4310 * or single uint64_t's. We make both look like arrays to make them easier
4311 * to process. In order to make single uint64_t's look like arrays, we set
4312 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4313 * we can just use *data and count.
4317 uint_t count
; /* Number of entries in data[] */
4318 uint64_t __data
; /* Only used when data is a single uint64_t */
4322 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4326 for (i
= 0; i
< len
; i
++)
4327 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4333 * Helper function to lookup a uint64_t array or uint64_t value and store its
4334 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4335 * it look like a one element array to make it easier to process.
4338 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4339 struct stat_array
*nva
)
4344 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4345 switch (nvpair_type(tmp
)) {
4346 case DATA_TYPE_UINT64_ARRAY
:
4347 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4349 case DATA_TYPE_UINT64
:
4350 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4351 nva
->data
= &nva
->__data
;
4355 /* Not a uint64_t */
4364 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4365 * subtract them, and return the results in a newly allocated stat_array.
4366 * You must free the returned array after you are done with it with
4367 * free_calc_stats().
4369 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4372 static struct stat_array
*
4373 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4376 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4377 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4379 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4381 /* Extract our extended stats nvlist from the main list */
4382 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4385 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4389 newnva
= safe_malloc(alloc_size
);
4390 oldnva
= safe_malloc(alloc_size
);
4391 calcnva
= safe_malloc(alloc_size
);
4393 for (j
= 0; j
< len
; j
++) {
4394 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4396 calcnva
[j
].count
= newnva
[j
].count
;
4397 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4398 calcnva
[j
].data
= safe_malloc(alloc_size
);
4399 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4402 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4404 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4405 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4414 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4417 for (i
= 0; i
< len
; i
++)
4424 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4425 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4431 enum zfs_nicenum_format format
;
4432 unsigned int buckets
;
4433 unsigned int start_bucket
;
4436 format
= ZFS_NICENUM_RAW
;
4438 format
= ZFS_NICENUM_1024
;
4440 /* All these histos are the same size, so just use nva[0].count */
4441 buckets
= nva
[0].count
;
4443 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4444 /* Start at 512 - req size should never be lower than this */
4450 for (j
= start_bucket
; j
< buckets
; j
++) {
4451 /* Print histogram bucket label */
4452 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4453 /* Ending range of this bucket */
4454 val
= (1UL << (j
+ 1)) - 1;
4455 zfs_nicetime(val
, buf
, sizeof (buf
));
4457 /* Request size (starting range of bucket) */
4459 zfs_nicenum(val
, buf
, sizeof (buf
));
4462 if (cb
->cb_scripted
)
4463 printf("%llu", (u_longlong_t
)val
);
4465 printf("%-*s", namewidth
, buf
);
4467 /* Print the values on the line */
4468 for (i
= 0; i
< len
; i
++) {
4469 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4470 column_width
, cb
->cb_scripted
);
4477 print_solid_separator(unsigned int length
)
4485 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4486 nvlist_t
*newnv
, double scale
, const char *name
)
4488 unsigned int column_width
;
4489 unsigned int namewidth
;
4490 unsigned int entire_width
;
4491 enum iostat_type type
;
4492 struct stat_array
*nva
;
4494 unsigned int names_len
;
4496 /* What type of histo are we? */
4497 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4499 /* Get NULL-terminated array of nvlist names for our histo */
4500 names
= vsx_type_to_nvlist
[type
];
4501 names_len
= str_array_len(names
); /* num of names */
4503 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4505 if (cb
->cb_literal
) {
4506 column_width
= MAX(5,
4507 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4512 namewidth
= MAX(cb
->cb_namewidth
,
4513 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4516 * Calculate the entire line width of what we're printing. The
4517 * +2 is for the two spaces between columns:
4521 /* |___| <---------- column_width */
4523 /* |__________| <--- entire_width */
4525 entire_width
= namewidth
+ (column_width
+ 2) *
4526 label_array_len(iostat_bottom_labels
[type
]);
4528 if (cb
->cb_scripted
)
4529 printf("%s\n", name
);
4531 print_iostat_header_impl(cb
, column_width
, name
);
4533 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4536 free_calc_stats(nva
, names_len
);
4537 if (!cb
->cb_scripted
)
4538 print_solid_separator(entire_width
);
4542 * Calculate the average latency of a power-of-two latency histogram
4545 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4548 uint64_t count
= 0, total
= 0;
4550 for (i
= 0; i
< buckets
; i
++) {
4552 * Our buckets are power-of-two latency ranges. Use the
4553 * midpoint latency of each bucket to calculate the average.
4561 if (histo
[i
] != 0) {
4562 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4567 /* Prevent divide by zero */
4568 return (count
== 0 ? 0 : total
/ count
);
4572 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*newnv
)
4574 const char *names
[] = {
4575 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4576 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4577 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4578 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4579 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4580 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4581 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4582 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4583 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4584 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4585 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4586 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4587 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE
,
4588 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
4591 struct stat_array
*nva
;
4593 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4594 enum zfs_nicenum_format format
;
4596 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4599 format
= ZFS_NICENUM_RAW
;
4601 format
= ZFS_NICENUM_1024
;
4603 for (int i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4604 uint64_t val
= nva
[i
].data
[0];
4605 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4608 free_calc_stats(nva
, ARRAY_SIZE(names
));
4612 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4617 const char *names
[] = {
4618 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4619 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4620 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4621 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4622 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4623 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4624 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4625 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4626 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4627 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4628 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
4630 struct stat_array
*nva
;
4632 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4633 enum zfs_nicenum_format format
;
4635 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4638 format
= ZFS_NICENUM_RAWTIME
;
4640 format
= ZFS_NICENUM_TIME
;
4642 /* Print our avg latencies on the line */
4643 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4644 /* Compute average latency for a latency histo */
4645 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4646 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4648 free_calc_stats(nva
, ARRAY_SIZE(names
));
4652 * Print default statistics (capacity/operations/bandwidth)
4655 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4657 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4658 enum zfs_nicenum_format format
;
4659 char na
; /* char to print for "not applicable" values */
4661 if (cb
->cb_literal
) {
4662 format
= ZFS_NICENUM_RAW
;
4665 format
= ZFS_NICENUM_1024
;
4669 /* only toplevel vdevs have capacity stats */
4670 if (vs
->vs_space
== 0) {
4671 if (cb
->cb_scripted
)
4672 printf("\t%c\t%c", na
, na
);
4674 printf(" %*c %*c", column_width
, na
, column_width
,
4677 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4679 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4680 column_width
, cb
->cb_scripted
);
4683 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4684 format
, column_width
, cb
->cb_scripted
);
4685 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4686 format
, column_width
, cb
->cb_scripted
);
4687 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4688 format
, column_width
, cb
->cb_scripted
);
4689 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4690 format
, column_width
, cb
->cb_scripted
);
4693 static const char *class_name
[] = {
4694 VDEV_ALLOC_BIAS_DEDUP
,
4695 VDEV_ALLOC_BIAS_SPECIAL
,
4696 VDEV_ALLOC_CLASS_LOGS
4700 * Print out all the statistics for the given vdev. This can either be the
4701 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4702 * is a verbose output, and we don't want to display the toplevel pool stats.
4704 * Returns the number of stat lines printed.
4707 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4708 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4710 nvlist_t
**oldchild
, **newchild
;
4711 uint_t c
, children
, oldchildren
;
4712 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4713 vdev_stat_t zerovs
= { 0 };
4720 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4723 calcvs
= safe_malloc(sizeof (*calcvs
));
4725 if (oldnv
!= NULL
) {
4726 verify(nvlist_lookup_uint64_array(oldnv
,
4727 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4732 /* Do we only want to see a specific vdev? */
4733 for (i
= 0; i
< cb
->cb_vdevs
.cb_names_count
; i
++) {
4734 /* Yes we do. Is this the vdev? */
4735 if (strcmp(name
, cb
->cb_vdevs
.cb_names
[i
]) == 0) {
4737 * This is our vdev. Since it is the only vdev we
4738 * will be displaying, make depth = 0 so that it
4739 * doesn't get indented.
4746 if (cb
->cb_vdevs
.cb_names_count
&& (i
== cb
->cb_vdevs
.cb_names_count
)) {
4747 /* Couldn't match the name */
4752 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4753 (uint64_t **)&newvs
, &c
) == 0);
4756 * Print the vdev name unless it's is a histogram. Histograms
4757 * display the vdev name in the header itself.
4759 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4760 if (cb
->cb_scripted
) {
4763 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4764 (void) printf("%*s%s", depth
, "", name
);
4766 (void) printf("%*s%s%*s", depth
, "", name
,
4767 (int)(cb
->cb_namewidth
- strlen(name
) -
4772 /* Calculate our scaling factor */
4773 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4774 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4776 * If we specify printing histograms with no time interval, then
4777 * print the histogram numbers over the entire lifetime of the
4785 scale
= (double)NANOSEC
/ tdelta
;
4788 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4789 calc_default_iostats(oldvs
, newvs
, calcvs
);
4790 print_iostat_default(calcvs
, cb
, scale
);
4792 if (cb
->cb_flags
& IOS_LATENCY_M
)
4793 print_iostat_latency(cb
, oldnv
, newnv
);
4794 if (cb
->cb_flags
& IOS_QUEUES_M
)
4795 print_iostat_queues(cb
, newnv
);
4796 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4798 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4801 if (cb
->vcdl
!= NULL
) {
4803 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4806 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4810 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4819 if (!cb
->cb_verbose
)
4822 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4823 &newchild
, &children
) != 0)
4827 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4828 &oldchild
, &oldchildren
) != 0)
4831 children
= MIN(oldchildren
, children
);
4835 * print normal top-level devices
4837 for (c
= 0; c
< children
; c
++) {
4838 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4840 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4843 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4846 if (ishole
|| islog
)
4849 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4852 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4853 cb
->cb_vdevs
.cb_name_flags
);
4854 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4855 newchild
[c
], cb
, depth
+ 2);
4860 * print all other top-level devices
4862 for (uint_t n
= 0; n
< 3; n
++) {
4863 boolean_t printed
= B_FALSE
;
4865 for (c
= 0; c
< children
; c
++) {
4866 uint64_t islog
= B_FALSE
;
4870 (void) nvlist_lookup_uint64(newchild
[c
],
4871 ZPOOL_CONFIG_IS_LOG
, &islog
);
4873 bias
= VDEV_ALLOC_CLASS_LOGS
;
4875 (void) nvlist_lookup_string(newchild
[c
],
4876 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4877 (void) nvlist_lookup_string(newchild
[c
],
4878 ZPOOL_CONFIG_TYPE
, &type
);
4880 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4882 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4886 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4888 !cb
->cb_vdevs
.cb_names
) {
4889 print_iostat_dashes(cb
, 0,
4896 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4897 cb
->cb_vdevs
.cb_name_flags
);
4898 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4899 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4905 * Include level 2 ARC devices in iostat output
4907 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4908 &newchild
, &children
) != 0)
4912 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4913 &oldchild
, &oldchildren
) != 0)
4916 children
= MIN(oldchildren
, children
);
4920 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4921 !cb
->cb_vdevs
.cb_names
) {
4922 print_iostat_dashes(cb
, 0, "cache");
4926 for (c
= 0; c
< children
; c
++) {
4927 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4928 cb
->cb_vdevs
.cb_name_flags
);
4929 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4930 : NULL
, newchild
[c
], cb
, depth
+ 2);
4939 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4941 iostat_cbdata_t
*cb
= data
;
4945 * If the pool has disappeared, remove it from the list and continue.
4947 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4951 pool_list_remove(cb
->cb_list
, zhp
);
4957 * Callback to print out the iostats for the given pool.
4960 print_iostat(zpool_handle_t
*zhp
, void *data
)
4962 iostat_cbdata_t
*cb
= data
;
4963 nvlist_t
*oldconfig
, *newconfig
;
4964 nvlist_t
*oldnvroot
, *newnvroot
;
4967 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4969 if (cb
->cb_iteration
== 1)
4972 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4975 if (oldconfig
== NULL
)
4978 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4981 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4983 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4984 !cb
->cb_scripted
&& cb
->cb_verbose
&&
4985 !cb
->cb_vdevs
.cb_names_count
) {
4986 print_iostat_separator(cb
);
4987 if (cb
->vcdl
!= NULL
) {
4988 print_cmd_columns(cb
->vcdl
, 1);
5003 if (isatty(STDOUT_FILENO
)) {
5004 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
5006 columns
= ws
.ws_col
;
5015 * Return the required length of the pool/vdev name column. The minimum
5016 * allowed width and output formatting flags must be provided.
5019 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
5021 nvlist_t
*config
, *nvroot
;
5022 int width
= min_width
;
5024 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
5025 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5027 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
5028 if (verbose
== B_FALSE
) {
5029 width
= MAX(poolname_len
, min_width
);
5031 width
= MAX(poolname_len
,
5032 max_width(zhp
, nvroot
, 0, min_width
, flags
));
5040 * Parse the input string, get the 'interval' and 'count' value if there is one.
5043 get_interval_count(int *argcp
, char **argv
, float *iv
,
5047 unsigned long count
= 0;
5051 * Determine if the last argument is an integer or a pool name
5053 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5057 interval
= strtof(argv
[argc
- 1], &end
);
5059 if (*end
== '\0' && errno
== 0) {
5060 if (interval
== 0) {
5061 (void) fprintf(stderr
, gettext(
5062 "interval cannot be zero\n"));
5066 * Ignore the last parameter
5071 * If this is not a valid number, just plow on. The
5072 * user will get a more informative error message later
5080 * If the last argument is also an integer, then we have both a count
5083 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5088 interval
= strtof(argv
[argc
- 1], &end
);
5090 if (*end
== '\0' && errno
== 0) {
5091 if (interval
== 0) {
5092 (void) fprintf(stderr
, gettext(
5093 "interval cannot be zero\n"));
5098 * Ignore the last parameter
5112 get_timestamp_arg(char c
)
5115 timestamp_fmt
= UDATE
;
5117 timestamp_fmt
= DDATE
;
5123 * Return stat flags that are supported by all pools by both the module and
5124 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5125 * It will get ANDed down until only the flags that are supported on all pools
5129 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5131 uint64_t *mask
= data
;
5132 nvlist_t
*config
, *nvroot
, *nvx
;
5136 config
= zpool_get_config(zhp
, NULL
);
5137 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5140 /* Default stats are always supported, but for completeness.. */
5141 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5142 flags
|= IOS_DEFAULT_M
;
5144 /* Get our extended stats nvlist from the main list */
5145 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5148 * No extended stats; they're probably running an older
5149 * module. No big deal, we support that too.
5154 /* For each extended stat, make sure all its nvpairs are supported */
5155 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5156 if (!vsx_type_to_nvlist
[j
][0])
5159 /* Start off by assuming the flag is supported, then check */
5160 flags
|= (1ULL << j
);
5161 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5162 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5163 /* flag isn't supported */
5164 flags
= flags
& ~(1ULL << j
);
5170 *mask
= *mask
& flags
;
5175 * Return a bitmask of stats that are supported on all pools by both the module
5179 get_stat_flags(zpool_list_t
*list
)
5184 * get_stat_flags_cb() will lop off bits from "mask" until only the
5185 * flags that are supported on all pools remain.
5187 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5192 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5195 is_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *cb_data
)
5197 vdev_cbdata_t
*cb
= cb_data
;
5199 int ret
= 1; /* assume match */
5200 zpool_handle_t
*zhp
= zhp_data
;
5202 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
5204 if (strcmp(name
, cb
->cb_names
[0])) {
5206 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, VDEV_NAME_GUID
);
5207 ret
= (strcmp(name
, cb
->cb_names
[0]) == 0);
5215 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5218 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5220 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5224 * Check if vdevs are in a pool
5226 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5227 * return 0. If pool_name is NULL, then search all pools.
5230 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5238 if ((argc
== 0) || !*argv
)
5244 /* Temporarily hijack cb_names for a second... */
5245 tmp_name
= cb
->cb_names
;
5247 /* Go though our list of prospective vdev names */
5248 for (i
= 0; i
< argc
; i
++) {
5249 cb
->cb_names
= argv
+ i
;
5251 /* Is this name a vdev in our pools? */
5252 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5253 ZFS_TYPE_POOL
, B_FALSE
, is_vdev
, cb
);
5260 cb
->cb_names
= tmp_name
;
5266 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5269 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5276 * Do we have a pool named *name? If so, return 1, otherwise 0.
5281 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, ZFS_TYPE_POOL
, B_FALSE
,
5285 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5287 are_all_pools(int argc
, char **argv
)
5289 if ((argc
== 0) || !*argv
)
5293 if (!is_pool(argv
[argc
]))
5300 * Helper function to print out vdev/pool names we can't resolve. Used for an
5304 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5310 for (i
= 0; i
< argc
; i
++) {
5314 str
= gettext("pool");
5315 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5316 str
= gettext("vdev in this pool");
5317 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5318 str
= gettext("vdev in another pool");
5320 str
= gettext("unknown");
5322 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5327 * Same as get_interval_count(), but with additional checks to not misinterpret
5328 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5329 * cb.cb_vdevs.cb_name_flags.
5332 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5333 unsigned long *count
, iostat_cbdata_t
*cb
)
5335 char **tmpargv
= argv
;
5336 int argc_for_interval
= 0;
5338 /* Is the last arg an interval value? Or a guid? */
5339 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
,
5342 * The last arg is not a guid, so it's probably an
5345 argc_for_interval
++;
5348 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
,
5351 * The 2nd to last arg is not a guid, so it's probably
5352 * an interval value.
5354 argc_for_interval
++;
5358 /* Point to our list of possible intervals */
5359 tmpargv
= &argv
[*argc
- argc_for_interval
];
5361 *argc
= *argc
- argc_for_interval
;
5362 get_interval_count(&argc_for_interval
, tmpargv
,
5367 * Floating point sleep(). Allows you to pass in a floating point value for
5373 struct timespec req
;
5374 req
.tv_sec
= floor(sec
);
5375 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5376 nanosleep(&req
, NULL
);
5380 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5381 * if we were unable to determine its size.
5384 terminal_height(void)
5388 if (isatty(STDOUT_FILENO
) == 0)
5391 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5392 return (win
.ws_row
);
5398 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5401 * name: Short name of the script ('iostat').
5402 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5405 print_zpool_script_help(char *name
, char *path
)
5407 char *argv
[] = {path
, "-h", NULL
};
5408 char **lines
= NULL
;
5412 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5414 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5416 libzfs_free_str_array(lines
, lines_cnt
);
5420 for (int i
= 0; i
< lines_cnt
; i
++)
5421 if (!is_blank_str(lines
[i
]))
5422 printf(" %-14s %s\n", name
, lines
[i
]);
5424 libzfs_free_str_array(lines
, lines_cnt
);
5428 * Go though the zpool status/iostat -c scripts in the user's path, run their
5429 * help option (-h), and print out the results.
5432 print_zpool_dir_scripts(char *dirpath
)
5436 char fullpath
[MAXPATHLEN
];
5437 struct stat dir_stat
;
5439 if ((dir
= opendir(dirpath
)) != NULL
) {
5440 /* print all the files and directories within directory */
5441 while ((ent
= readdir(dir
)) != NULL
) {
5442 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5444 /* Print the scripts */
5445 if (stat(fullpath
, &dir_stat
) == 0)
5446 if (dir_stat
.st_mode
& S_IXUSR
&&
5447 S_ISREG(dir_stat
.st_mode
))
5448 print_zpool_script_help(ent
->d_name
,
5456 * Print out help text for all zpool status/iostat -c scripts.
5459 print_zpool_script_list(char *subcommand
)
5461 char *dir
, *sp
, *tmp
;
5463 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5465 sp
= zpool_get_cmd_search_path();
5469 for (dir
= strtok_r(sp
, ":", &tmp
);
5471 dir
= strtok_r(NULL
, ":", &tmp
))
5472 print_zpool_dir_scripts(dir
);
5478 * Set the minimum pool/vdev name column width. The width must be at least 10,
5479 * but may be as large as the column width - 42 so it still fits on one line.
5480 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5483 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5485 iostat_cbdata_t
*cb
= data
;
5486 int width
, available_width
;
5489 * get_namewidth() returns the maximum width of any name in that column
5490 * for any pool/vdev/device line that will be output.
5492 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_vdevs
.cb_name_flags
,
5496 * The width we are calculating is the width of the header and also the
5497 * padding width for names that are less than maximum width. The stats
5498 * take up 42 characters, so the width available for names is:
5500 available_width
= get_columns() - 42;
5503 * If the maximum width fits on a screen, then great! Make everything
5504 * line up by justifying all lines to the same width. If that max
5505 * width is larger than what's available, the name plus stats won't fit
5506 * on one line, and justifying to that width would cause every line to
5507 * wrap on the screen. We only want lines with long names to wrap.
5508 * Limit the padding to what won't wrap.
5510 if (width
> available_width
)
5511 width
= available_width
;
5514 * And regardless of whatever the screen width is (get_columns can
5515 * return 0 if the width is not known or less than 42 for a narrow
5516 * terminal) have the width be a minimum of 10.
5521 /* Save the calculated width */
5522 cb
->cb_namewidth
= width
;
5528 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5529 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5530 * [interval [count]]
5532 * -c CMD For each vdev, run command CMD
5533 * -g Display guid for individual vdev name.
5534 * -L Follow links when resolving vdev path name.
5535 * -P Display full path for vdev name.
5536 * -v Display statistics for individual vdevs
5538 * -p Display values in parsable (exact) format.
5539 * -H Scripted mode. Don't display headers, and separate properties
5541 * -l Display average latency
5542 * -q Display queue depths
5543 * -w Display latency histograms
5544 * -r Display request size histogram
5545 * -T Display a timestamp in date(1) or Unix format
5546 * -n Only print headers once
5548 * This command can be tricky because we want to be able to deal with pool
5549 * creation/destruction as well as vdev configuration changes. The bulk of this
5550 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5551 * on pool_list_update() to detect the addition of new pools. Configuration
5552 * changes are all handled within libzfs.
5555 zpool_do_iostat(int argc
, char **argv
)
5561 unsigned long count
= 0;
5564 boolean_t verbose
= B_FALSE
;
5565 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5566 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5567 boolean_t omit_since_boot
= B_FALSE
;
5568 boolean_t guid
= B_FALSE
;
5569 boolean_t follow_links
= B_FALSE
;
5570 boolean_t full_name
= B_FALSE
;
5571 boolean_t headers_once
= B_FALSE
;
5572 iostat_cbdata_t cb
= { 0 };
5575 /* Used for printing error message */
5576 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5577 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5579 uint64_t unsupported_flags
;
5582 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5587 gettext("Can't set -c flag twice\n"));
5591 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5592 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5593 fprintf(stderr
, gettext(
5594 "Can't run -c, disabled by "
5595 "ZPOOL_SCRIPTS_ENABLED.\n"));
5599 if ((getuid() <= 0 || geteuid() <= 0) &&
5600 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5601 fprintf(stderr
, gettext(
5602 "Can't run -c with root privileges "
5603 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5613 follow_links
= B_TRUE
;
5619 get_timestamp_arg(*optarg
);
5643 omit_since_boot
= B_TRUE
;
5646 headers_once
= B_TRUE
;
5652 if (optopt
== 'c') {
5653 print_zpool_script_list("iostat");
5657 gettext("invalid option '%c'\n"), optopt
);
5666 cb
.cb_literal
= parsable
;
5667 cb
.cb_scripted
= scripted
;
5670 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_GUID
;
5672 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5674 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_PATH
;
5675 cb
.cb_iteration
= 0;
5676 cb
.cb_namewidth
= 0;
5677 cb
.cb_verbose
= verbose
;
5679 /* Get our interval and count values (if any) */
5681 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5684 get_interval_count(&argc
, argv
, &interval
, &count
);
5688 /* No args, so just print the defaults. */
5689 } else if (are_all_pools(argc
, argv
)) {
5690 /* All the args are pool names */
5691 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
5692 /* All the args are vdevs */
5693 cb
.cb_vdevs
.cb_names
= argv
;
5694 cb
.cb_vdevs
.cb_names_count
= argc
;
5695 argc
= 0; /* No pools to process */
5696 } else if (are_all_pools(1, argv
)) {
5697 /* The first arg is a pool name */
5698 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
5700 /* ...and the rest are vdev names */
5701 cb
.cb_vdevs
.cb_names
= argv
+ 1;
5702 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
5703 argc
= 1; /* One pool to process */
5705 fprintf(stderr
, gettext("Expected either a list of "));
5706 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5707 fprintf(stderr
, " \"%s\", ", argv
[0]);
5708 fprintf(stderr
, gettext("but got:\n"));
5709 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5710 argv
[0], &cb
.cb_vdevs
);
5711 fprintf(stderr
, "\n");
5717 * The args don't make sense. The first arg isn't a pool name,
5718 * nor are all the args vdevs.
5720 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5721 fprintf(stderr
, "\n");
5725 if (cb
.cb_vdevs
.cb_names_count
!= 0) {
5727 * If user specified vdevs, it implies verbose.
5729 cb
.cb_verbose
= B_TRUE
;
5733 * Construct the list of all interesting pools.
5736 if ((list
= pool_list_get(argc
, argv
, NULL
, ZFS_TYPE_POOL
, parsable
,
5740 if (pool_list_count(list
) == 0 && argc
!= 0) {
5741 pool_list_free(list
);
5745 if (pool_list_count(list
) == 0 && interval
== 0) {
5746 pool_list_free(list
);
5747 (void) fprintf(stderr
, gettext("no pools available\n"));
5751 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5752 pool_list_free(list
);
5753 (void) fprintf(stderr
,
5754 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5759 if (l_histo
&& rq_histo
) {
5760 pool_list_free(list
);
5761 (void) fprintf(stderr
,
5762 gettext("Only one of [-r|-w] can be passed at a time\n"));
5768 * Enter the main iostat loop.
5774 * Histograms tables look out of place when you try to display
5775 * them with the other stats, so make a rule that you can only
5776 * print histograms by themselves.
5778 cb
.cb_flags
= IOS_L_HISTO_M
;
5779 } else if (rq_histo
) {
5780 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5782 cb
.cb_flags
= IOS_DEFAULT_M
;
5784 cb
.cb_flags
|= IOS_LATENCY_M
;
5786 cb
.cb_flags
|= IOS_QUEUES_M
;
5790 * See if the module supports all the stats we want to display.
5792 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5793 if (unsupported_flags
) {
5797 gettext("The loaded zfs module doesn't support:"));
5799 /* for each bit set in unsupported_flags */
5800 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5801 idx
= lowbit64(f
) - 1;
5802 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5805 fprintf(stderr
, ". Try running a newer module.\n");
5806 pool_list_free(list
);
5812 if ((npools
= pool_list_count(list
)) == 0)
5813 (void) fprintf(stderr
, gettext("no pools available\n"));
5816 * If this is the first iteration and -y was supplied
5817 * we skip any printing.
5819 boolean_t skip
= (omit_since_boot
&&
5820 cb
.cb_iteration
== 0);
5823 * Refresh all statistics. This is done as an
5824 * explicit step before calculating the maximum name
5825 * width, so that any * configuration changes are
5826 * properly accounted for.
5828 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5832 * Iterate over all pools to determine the maximum width
5833 * for the pool / device name column across all pools.
5835 cb
.cb_namewidth
= 0;
5836 (void) pool_list_iter(list
, B_FALSE
,
5837 get_namewidth_iostat
, &cb
);
5839 if (timestamp_fmt
!= NODATE
)
5840 print_timestamp(timestamp_fmt
);
5842 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5843 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5844 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5845 argv
, cmd
, g_zfs
, cb
.cb_vdevs
.cb_names
,
5846 cb
.cb_vdevs
.cb_names_count
,
5847 cb
.cb_vdevs
.cb_name_flags
);
5854 * Check terminal size so we can print headers
5855 * even when terminal window has its height
5858 winheight
= terminal_height();
5860 * Are we connected to TTY? If not, headers_once
5861 * should be true, to avoid breaking scripts.
5864 headers_once
= B_TRUE
;
5867 * If it's the first time and we're not skipping it,
5868 * or either skip or verbose mode, print the header.
5870 * The histogram code explicitly prints its header on
5871 * every vdev, so skip this for histograms.
5873 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5874 (skip
!= verbose
) ||
5876 (cb
.cb_iteration
% winheight
) == 0)) &&
5877 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5879 print_iostat_header(&cb
);
5882 (void) fsleep(interval
);
5886 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5889 * If there's more than one pool, and we're not in
5890 * verbose mode (which prints a separator for us),
5891 * then print a separator.
5893 * In addition, if we're printing specific vdevs then
5894 * we also want an ending separator.
5896 if (((npools
> 1 && !verbose
&&
5897 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5898 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5899 cb
.cb_vdevs
.cb_names_count
)) &&
5901 print_iostat_separator(&cb
);
5902 if (cb
.vcdl
!= NULL
)
5903 print_cmd_columns(cb
.vcdl
, 1);
5907 if (cb
.vcdl
!= NULL
)
5908 free_vdev_cmd_data_list(cb
.vcdl
);
5913 * Flush the output so that redirection to a file isn't buffered
5916 (void) fflush(stdout
);
5921 if (count
!= 0 && --count
== 0)
5924 (void) fsleep(interval
);
5927 pool_list_free(list
);
5932 typedef struct list_cbdata
{
5933 boolean_t cb_verbose
;
5936 boolean_t cb_scripted
;
5937 zprop_list_t
*cb_proplist
;
5938 boolean_t cb_literal
;
5943 * Given a list of columns to display, output appropriate headers for each one.
5946 print_header(list_cbdata_t
*cb
)
5948 zprop_list_t
*pl
= cb
->cb_proplist
;
5949 char headerbuf
[ZPOOL_MAXPROPLEN
];
5951 boolean_t first
= B_TRUE
;
5952 boolean_t right_justify
;
5955 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5956 width
= pl
->pl_width
;
5957 if (first
&& cb
->cb_verbose
) {
5959 * Reset the width to accommodate the verbose listing
5962 width
= cb
->cb_namewidth
;
5970 right_justify
= B_FALSE
;
5971 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5972 header
= zpool_prop_column_name(pl
->pl_prop
);
5973 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5977 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5978 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5979 headerbuf
[i
] = '\0';
5983 if (pl
->pl_next
== NULL
&& !right_justify
)
5984 (void) printf("%s", header
);
5985 else if (right_justify
)
5986 (void) printf("%*s", (int)width
, header
);
5988 (void) printf("%-*s", (int)width
, header
);
5991 (void) printf("\n");
5995 * Given a pool and a list of properties, print out all the properties according
5996 * to the described layout. Used by zpool_do_list().
5999 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
6001 zprop_list_t
*pl
= cb
->cb_proplist
;
6002 boolean_t first
= B_TRUE
;
6003 char property
[ZPOOL_MAXPROPLEN
];
6005 boolean_t right_justify
;
6008 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6010 width
= pl
->pl_width
;
6011 if (first
&& cb
->cb_verbose
) {
6013 * Reset the width to accommodate the verbose listing
6016 width
= cb
->cb_namewidth
;
6020 if (cb
->cb_scripted
)
6021 (void) printf("\t");
6028 right_justify
= B_FALSE
;
6029 if (pl
->pl_prop
!= ZPROP_INVAL
) {
6030 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
6031 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
6036 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6037 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
6038 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
6039 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
6040 sizeof (property
)) == 0) {
6048 * If this is being called in scripted mode, or if this is the
6049 * last column and it is left-justified, don't include a width
6052 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
6053 (void) printf("%s", propstr
);
6054 else if (right_justify
)
6055 (void) printf("%*s", (int)width
, propstr
);
6057 (void) printf("%-*s", (int)width
, propstr
);
6060 (void) printf("\n");
6064 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
6065 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
6069 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6072 case ZPOOL_PROP_EXPANDSZ
:
6073 case ZPOOL_PROP_CHECKPOINT
:
6074 case ZPOOL_PROP_DEDUPRATIO
:
6076 (void) strlcpy(propval
, "-", sizeof (propval
));
6078 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6081 case ZPOOL_PROP_FRAGMENTATION
:
6082 if (value
== ZFS_FRAG_INVALID
) {
6083 (void) strlcpy(propval
, "-", sizeof (propval
));
6084 } else if (format
== ZFS_NICENUM_RAW
) {
6085 (void) snprintf(propval
, sizeof (propval
), "%llu",
6086 (unsigned long long)value
);
6088 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6089 (unsigned long long)value
);
6092 case ZPOOL_PROP_CAPACITY
:
6093 /* capacity value is in parts-per-10,000 (aka permyriad) */
6094 if (format
== ZFS_NICENUM_RAW
)
6095 (void) snprintf(propval
, sizeof (propval
), "%llu",
6096 (unsigned long long)value
/ 100);
6098 (void) snprintf(propval
, sizeof (propval
),
6099 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6100 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6102 case ZPOOL_PROP_HEALTH
:
6104 (void) strlcpy(propval
, str
, sizeof (propval
));
6107 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6111 (void) strlcpy(propval
, "-", sizeof (propval
));
6114 (void) printf("\t%s", propval
);
6116 (void) printf(" %*s", (int)width
, propval
);
6120 * print static default line per vdev
6121 * not compatible with '-o' <proplist> option
6124 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6125 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
6131 boolean_t scripted
= cb
->cb_scripted
;
6132 uint64_t islog
= B_FALSE
;
6133 char *dashes
= "%-*s - - - - "
6136 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
6137 (uint64_t **)&vs
, &c
) == 0);
6140 boolean_t toplevel
= (vs
->vs_space
!= 0);
6142 enum zfs_nicenum_format format
;
6146 format
= ZFS_NICENUM_RAW
;
6148 format
= ZFS_NICENUM_1024
;
6150 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
6154 (void) printf("\t%s", name
);
6155 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
6156 (void) printf("%*s%s", depth
, "", name
);
6158 (void) printf("%*s%s%*s", depth
, "", name
,
6159 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
6162 * Print the properties for the individual vdevs. Some
6163 * properties are only applicable to toplevel vdevs. The
6164 * 'toplevel' boolean value is passed to the print_one_column()
6165 * to indicate that the value is valid.
6167 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
6169 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6170 scripted
, toplevel
, format
);
6171 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6172 NULL
, scripted
, toplevel
, format
);
6173 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6174 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6175 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6176 scripted
, B_TRUE
, format
);
6177 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6178 vs
->vs_fragmentation
, NULL
, scripted
,
6179 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6181 cap
= (vs
->vs_space
== 0) ? 0 :
6182 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6183 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6184 scripted
, toplevel
, format
);
6185 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6186 scripted
, toplevel
, format
);
6187 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6189 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6191 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6194 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6196 (void) printf("\n");
6199 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6200 &child
, &children
) != 0)
6203 /* list the normal vdevs first */
6204 for (c
= 0; c
< children
; c
++) {
6205 uint64_t ishole
= B_FALSE
;
6207 if (nvlist_lookup_uint64(child
[c
],
6208 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6211 if (nvlist_lookup_uint64(child
[c
],
6212 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6215 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6218 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6220 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6224 /* list the classes: 'logs', 'dedup', and 'special' */
6225 for (uint_t n
= 0; n
< 3; n
++) {
6226 boolean_t printed
= B_FALSE
;
6228 for (c
= 0; c
< children
; c
++) {
6232 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6233 &islog
) == 0 && islog
) {
6234 bias
= VDEV_ALLOC_CLASS_LOGS
;
6236 (void) nvlist_lookup_string(child
[c
],
6237 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6238 (void) nvlist_lookup_string(child
[c
],
6239 ZPOOL_CONFIG_TYPE
, &type
);
6241 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6243 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6247 /* LINTED E_SEC_PRINTF_VAR_FMT */
6248 (void) printf(dashes
, cb
->cb_namewidth
,
6252 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6254 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6260 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6261 &child
, &children
) == 0 && children
> 0) {
6262 /* LINTED E_SEC_PRINTF_VAR_FMT */
6263 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6264 for (c
= 0; c
< children
; c
++) {
6265 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6267 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6273 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6274 &children
) == 0 && children
> 0) {
6275 /* LINTED E_SEC_PRINTF_VAR_FMT */
6276 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6277 for (c
= 0; c
< children
; c
++) {
6278 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6280 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6288 * Generic callback function to list a pool.
6291 list_callback(zpool_handle_t
*zhp
, void *data
)
6293 list_cbdata_t
*cbp
= data
;
6295 print_pool(zhp
, cbp
);
6297 if (cbp
->cb_verbose
) {
6298 nvlist_t
*config
, *nvroot
;
6300 config
= zpool_get_config(zhp
, NULL
);
6301 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6303 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6310 * Set the minimum pool/vdev name column width. The width must be at least 9,
6311 * but may be as large as needed.
6314 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6316 list_cbdata_t
*cb
= data
;
6319 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6325 cb
->cb_namewidth
= width
;
6331 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6333 * -g Display guid for individual vdev name.
6334 * -H Scripted mode. Don't display headers, and separate properties
6336 * -L Follow links when resolving vdev path name.
6337 * -o List of properties to display. Defaults to
6338 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6339 * "dedupratio,health,altroot"
6340 * -p Display values in parsable (exact) format.
6341 * -P Display full path for vdev name.
6342 * -T Display a timestamp in date(1) or Unix format
6344 * List all pools in the system, whether or not they're healthy. Output space
6345 * statistics for each one, as well as health status summary.
6348 zpool_do_list(int argc
, char **argv
)
6352 list_cbdata_t cb
= { 0 };
6353 static char default_props
[] =
6354 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6355 "capacity,dedupratio,health,altroot";
6356 char *props
= default_props
;
6358 unsigned long count
= 0;
6360 boolean_t first
= B_TRUE
;
6361 current_prop_type
= ZFS_TYPE_POOL
;
6364 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6367 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6370 cb
.cb_scripted
= B_TRUE
;
6373 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6379 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6382 cb
.cb_literal
= B_TRUE
;
6385 get_timestamp_arg(*optarg
);
6388 cb
.cb_verbose
= B_TRUE
;
6389 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6392 (void) fprintf(stderr
, gettext("missing argument for "
6393 "'%c' option\n"), optopt
);
6397 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6406 get_interval_count(&argc
, argv
, &interval
, &count
);
6408 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6412 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6413 ZFS_TYPE_POOL
, cb
.cb_literal
, &ret
)) == NULL
)
6416 if (pool_list_count(list
) == 0)
6419 cb
.cb_namewidth
= 0;
6420 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6422 if (timestamp_fmt
!= NODATE
)
6423 print_timestamp(timestamp_fmt
);
6425 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6429 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6434 if (count
!= 0 && --count
== 0)
6437 pool_list_free(list
);
6438 (void) fsleep(interval
);
6441 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6442 (void) printf(gettext("no pools available\n"));
6446 pool_list_free(list
);
6447 zprop_free_list(cb
.cb_proplist
);
6452 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6454 boolean_t force
= B_FALSE
;
6455 boolean_t rebuild
= B_FALSE
;
6456 boolean_t wait
= B_FALSE
;
6459 char *poolname
, *old_disk
, *new_disk
;
6460 zpool_handle_t
*zhp
;
6461 nvlist_t
*props
= NULL
;
6466 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6472 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6473 (void) fprintf(stderr
, gettext("missing "
6474 "'=' for -o option\n"));
6480 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6481 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6491 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6500 /* get pool name and check number of arguments */
6502 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6509 (void) fprintf(stderr
,
6510 gettext("missing <device> specification\n"));
6518 (void) fprintf(stderr
,
6519 gettext("missing <new_device> specification\n"));
6522 new_disk
= old_disk
;
6532 (void) fprintf(stderr
, gettext("too many arguments\n"));
6536 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6541 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6542 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6549 /* unless manually specified use "ashift" pool property (if set) */
6550 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6553 char strval
[ZPOOL_MAXPROPLEN
];
6555 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6556 if (src
!= ZPROP_SRC_DEFAULT
) {
6557 (void) sprintf(strval
, "%" PRId32
, intval
);
6558 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6559 &props
, B_TRUE
) == 0);
6563 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6565 if (nvroot
== NULL
) {
6571 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6574 if (ret
== 0 && wait
)
6575 ret
= zpool_wait(zhp
,
6576 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6579 nvlist_free(nvroot
);
6586 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6588 * -f Force attach, even if <new_device> appears to be in use.
6589 * -s Use sequential instead of healing reconstruction for resilver.
6590 * -o Set property=value.
6591 * -w Wait for replacing to complete before returning
6593 * Replace <device> with <new_device>.
6596 zpool_do_replace(int argc
, char **argv
)
6598 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6602 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6604 * -f Force attach, even if <new_device> appears to be in use.
6605 * -s Use sequential instead of healing reconstruction for resilver.
6606 * -o Set property=value.
6607 * -w Wait for resilvering to complete before returning
6609 * Attach <new_device> to the mirror containing <device>. If <device> is not
6610 * part of a mirror, then <device> will be transformed into a mirror of
6611 * <device> and <new_device>. In either case, <new_device> will begin life
6612 * with a DTL of [0, now], and will immediately begin to resilver itself.
6615 zpool_do_attach(int argc
, char **argv
)
6617 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6621 * zpool detach [-f] <pool> <device>
6623 * -f Force detach of <device>, even if DTLs argue against it
6624 * (not supported yet)
6626 * Detach a device from a mirror. The operation will be refused if <device>
6627 * is the last device in the mirror, or if the DTLs indicate that this device
6628 * has the only valid copy of some data.
6631 zpool_do_detach(int argc
, char **argv
)
6634 char *poolname
, *path
;
6635 zpool_handle_t
*zhp
;
6639 while ((c
= getopt(argc
, argv
, "")) != -1) {
6642 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6651 /* get pool name and check number of arguments */
6653 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6658 (void) fprintf(stderr
,
6659 gettext("missing <device> specification\n"));
6666 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6669 ret
= zpool_vdev_detach(zhp
, path
);
6677 * zpool split [-gLnP] [-o prop=val] ...
6679 * [-R altroot] <pool> <newpool> [<device> ...]
6681 * -g Display guid for individual vdev name.
6682 * -L Follow links when resolving vdev path name.
6683 * -n Do not split the pool, but display the resulting layout if
6684 * it were to be split.
6685 * -o Set property=value, or set mount options.
6686 * -P Display full path for vdev name.
6687 * -R Mount the split-off pool under an alternate root.
6688 * -l Load encryption keys while importing.
6690 * Splits the named pool and gives it the new pool name. Devices to be split
6691 * off may be listed, provided that no more than one device is specified
6692 * per top-level vdev mirror. The newly split pool is left in an exported
6693 * state unless -R is specified.
6695 * Restrictions: the top-level of the pool pool must only be made up of
6696 * mirrors; all devices in the pool must be healthy; no device may be
6697 * undergoing a resilvering operation.
6700 zpool_do_split(int argc
, char **argv
)
6702 char *srcpool
, *newpool
, *propval
;
6703 char *mntopts
= NULL
;
6706 boolean_t loadkeys
= B_FALSE
;
6707 zpool_handle_t
*zhp
;
6708 nvlist_t
*config
, *props
= NULL
;
6710 flags
.dryrun
= B_FALSE
;
6711 flags
.import
= B_FALSE
;
6712 flags
.name_flags
= 0;
6715 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6718 flags
.name_flags
|= VDEV_NAME_GUID
;
6721 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6724 flags
.import
= B_TRUE
;
6726 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6727 &props
, B_TRUE
) != 0) {
6736 flags
.dryrun
= B_TRUE
;
6739 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6742 if (add_prop_list(optarg
, propval
,
6743 &props
, B_TRUE
) != 0) {
6752 flags
.name_flags
|= VDEV_NAME_PATH
;
6755 (void) fprintf(stderr
, gettext("missing argument for "
6756 "'%c' option\n"), optopt
);
6760 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6767 if (!flags
.import
&& mntopts
!= NULL
) {
6768 (void) fprintf(stderr
, gettext("setting mntopts is only "
6769 "valid when importing the pool\n"));
6773 if (!flags
.import
&& loadkeys
) {
6774 (void) fprintf(stderr
, gettext("loading keys is only "
6775 "valid when importing the pool\n"));
6783 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6787 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6797 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6802 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6803 if (config
== NULL
) {
6807 (void) printf(gettext("would create '%s' with the "
6808 "following layout:\n\n"), newpool
);
6809 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6811 print_vdev_tree(NULL
, "dedup", config
, 0,
6812 VDEV_ALLOC_BIAS_DEDUP
, 0);
6813 print_vdev_tree(NULL
, "special", config
, 0,
6814 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6820 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6821 nvlist_free(config
);
6827 * The split was successful. Now we need to open the new
6828 * pool and import it.
6830 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6831 nvlist_free(config
);
6837 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6842 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6843 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6845 (void) fprintf(stderr
, gettext("Split was successful, but "
6846 "the datasets could not all be mounted\n"));
6847 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6848 "different altroot\n"), "zpool import");
6851 nvlist_free(config
);
6860 * zpool online <pool> <device> ...
6863 zpool_do_online(int argc
, char **argv
)
6867 zpool_handle_t
*zhp
;
6869 vdev_state_t newstate
;
6873 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6876 flags
|= ZFS_ONLINE_EXPAND
;
6879 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6888 /* get pool name and check number of arguments */
6890 (void) fprintf(stderr
, gettext("missing pool name\n"));
6894 (void) fprintf(stderr
, gettext("missing device name\n"));
6900 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6903 for (i
= 1; i
< argc
; i
++) {
6904 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6905 if (newstate
!= VDEV_STATE_HEALTHY
) {
6906 (void) printf(gettext("warning: device '%s' "
6907 "onlined, but remains in faulted state\n"),
6909 if (newstate
== VDEV_STATE_FAULTED
)
6910 (void) printf(gettext("use 'zpool "
6911 "clear' to restore a faulted "
6914 (void) printf(gettext("use 'zpool "
6915 "replace' to replace devices "
6916 "that are no longer present\n"));
6929 * zpool offline [-ft] <pool> <device> ...
6931 * -f Force the device into a faulted state.
6933 * -t Only take the device off-line temporarily. The offline/faulted
6934 * state will not be persistent across reboots.
6937 zpool_do_offline(int argc
, char **argv
)
6941 zpool_handle_t
*zhp
;
6943 boolean_t istmp
= B_FALSE
;
6944 boolean_t fault
= B_FALSE
;
6947 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6956 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6965 /* get pool name and check number of arguments */
6967 (void) fprintf(stderr
, gettext("missing pool name\n"));
6971 (void) fprintf(stderr
, gettext("missing device name\n"));
6977 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6980 for (i
= 1; i
< argc
; i
++) {
6982 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6984 if (istmp
== B_FALSE
) {
6985 /* Force the fault to persist across imports */
6986 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6988 aux
= VDEV_AUX_EXTERNAL
;
6991 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6994 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
7005 * zpool clear <pool> [device]
7007 * Clear all errors associated with a pool or a particular device.
7010 zpool_do_clear(int argc
, char **argv
)
7014 boolean_t dryrun
= B_FALSE
;
7015 boolean_t do_rewind
= B_FALSE
;
7016 boolean_t xtreme_rewind
= B_FALSE
;
7017 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
7018 nvlist_t
*policy
= NULL
;
7019 zpool_handle_t
*zhp
;
7020 char *pool
, *device
;
7023 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
7032 xtreme_rewind
= B_TRUE
;
7035 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7045 (void) fprintf(stderr
, gettext("missing pool name\n"));
7050 (void) fprintf(stderr
, gettext("too many arguments\n"));
7054 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
7055 (void) fprintf(stderr
,
7056 gettext("-n or -X only meaningful with -F\n"));
7060 rewind_policy
= ZPOOL_TRY_REWIND
;
7062 rewind_policy
= ZPOOL_DO_REWIND
;
7064 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
7066 /* In future, further rewind policy choices can be passed along here */
7067 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
7068 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
7069 rewind_policy
) != 0) {
7074 device
= argc
== 2 ? argv
[1] : NULL
;
7076 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
7077 nvlist_free(policy
);
7081 if (zpool_clear(zhp
, device
, policy
) != 0)
7086 nvlist_free(policy
);
7092 * zpool reguid <pool>
7095 zpool_do_reguid(int argc
, char **argv
)
7099 zpool_handle_t
*zhp
;
7103 while ((c
= getopt(argc
, argv
, "")) != -1) {
7106 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7115 /* get pool name and check number of arguments */
7117 (void) fprintf(stderr
, gettext("missing pool name\n"));
7122 (void) fprintf(stderr
, gettext("too many arguments\n"));
7127 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7130 ret
= zpool_reguid(zhp
);
7138 * zpool reopen <pool>
7140 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7143 zpool_do_reopen(int argc
, char **argv
)
7147 boolean_t scrub_restart
= B_TRUE
;
7150 while ((c
= getopt(argc
, argv
, "n")) != -1) {
7153 scrub_restart
= B_FALSE
;
7156 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7165 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7166 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7167 B_FALSE
, zpool_reopen_one
, &scrub_restart
);
7172 typedef struct scrub_cbdata
{
7174 pool_scrub_cmd_t cb_scrub_cmd
;
7178 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7180 nvlist_t
*config
, *nvroot
;
7182 config
= zpool_get_config(zhp
, NULL
);
7184 if (config
!= NULL
) {
7185 pool_checkpoint_stat_t
*pcs
= NULL
;
7188 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7189 (void) nvlist_lookup_uint64_array(nvroot
,
7190 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7192 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7195 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7196 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7204 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7206 scrub_cbdata_t
*cb
= data
;
7210 * Ignore faulted pools.
7212 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7213 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7214 "currently unavailable\n"), zpool_get_name(zhp
));
7218 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7220 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7221 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7222 (void) printf(gettext("warning: will not scrub state that "
7223 "belongs to the checkpoint of pool '%s'\n"),
7224 zpool_get_name(zhp
));
7231 wait_callback(zpool_handle_t
*zhp
, void *data
)
7233 zpool_wait_activity_t
*act
= data
;
7234 return (zpool_wait(zhp
, *act
));
7238 * zpool scrub [-s | -p] [-w] <pool> ...
7240 * -s Stop. Stops any in-progress scrub.
7241 * -p Pause. Pause in-progress scrub.
7242 * -w Wait. Blocks until scrub has completed.
7245 zpool_do_scrub(int argc
, char **argv
)
7249 boolean_t wait
= B_FALSE
;
7252 cb
.cb_type
= POOL_SCAN_SCRUB
;
7253 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7256 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7259 cb
.cb_type
= POOL_SCAN_NONE
;
7262 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7268 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7274 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7275 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7276 (void) fprintf(stderr
, gettext("invalid option combination: "
7277 "-s and -p are mutually exclusive\n"));
7281 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7282 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7283 (void) fprintf(stderr
, gettext("invalid option combination: "
7284 "-w cannot be used with -p or -s\n"));
7292 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7296 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7297 B_FALSE
, scrub_callback
, &cb
);
7299 if (wait
&& !error
) {
7300 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7301 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7302 B_FALSE
, wait_callback
, &act
);
7309 * zpool resilver <pool> ...
7311 * Restarts any in-progress resilver
7314 zpool_do_resilver(int argc
, char **argv
)
7319 cb
.cb_type
= POOL_SCAN_RESILVER
;
7320 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7323 while ((c
= getopt(argc
, argv
, "")) != -1) {
7326 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7336 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7340 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7341 B_FALSE
, scrub_callback
, &cb
));
7345 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7347 * -c Cancel. Ends any in-progress trim.
7348 * -d Secure trim. Requires kernel and device support.
7349 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7350 * adding a multiplier suffix such as 'k' or 'm'.
7351 * -s Suspend. TRIM can then be restarted with no flags.
7352 * -w Wait. Blocks until trimming has completed.
7355 zpool_do_trim(int argc
, char **argv
)
7357 struct option long_options
[] = {
7358 {"cancel", no_argument
, NULL
, 'c'},
7359 {"secure", no_argument
, NULL
, 'd'},
7360 {"rate", required_argument
, NULL
, 'r'},
7361 {"suspend", no_argument
, NULL
, 's'},
7362 {"wait", no_argument
, NULL
, 'w'},
7366 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7368 boolean_t secure
= B_FALSE
;
7369 boolean_t wait
= B_FALSE
;
7372 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7376 if (cmd_type
!= POOL_TRIM_START
&&
7377 cmd_type
!= POOL_TRIM_CANCEL
) {
7378 (void) fprintf(stderr
, gettext("-c cannot be "
7379 "combined with other options\n"));
7382 cmd_type
= POOL_TRIM_CANCEL
;
7385 if (cmd_type
!= POOL_TRIM_START
) {
7386 (void) fprintf(stderr
, gettext("-d cannot be "
7387 "combined with the -c or -s options\n"));
7393 if (cmd_type
!= POOL_TRIM_START
) {
7394 (void) fprintf(stderr
, gettext("-r cannot be "
7395 "combined with the -c or -s options\n"));
7398 if (zfs_nicestrtonum(g_zfs
, optarg
, &rate
) == -1) {
7399 (void) fprintf(stderr
, "%s: %s\n",
7400 gettext("invalid value for rate"),
7401 libzfs_error_description(g_zfs
));
7406 if (cmd_type
!= POOL_TRIM_START
&&
7407 cmd_type
!= POOL_TRIM_SUSPEND
) {
7408 (void) fprintf(stderr
, gettext("-s cannot be "
7409 "combined with other options\n"));
7412 cmd_type
= POOL_TRIM_SUSPEND
;
7419 (void) fprintf(stderr
,
7420 gettext("invalid option '%c'\n"), optopt
);
7422 (void) fprintf(stderr
,
7423 gettext("invalid option '%s'\n"),
7434 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7439 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7440 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7445 char *poolname
= argv
[0];
7446 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7450 trimflags_t trim_flags
= {
7456 nvlist_t
*vdevs
= fnvlist_alloc();
7458 /* no individual leaf vdevs specified, so add them all */
7459 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7460 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7461 ZPOOL_CONFIG_VDEV_TREE
);
7462 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7463 trim_flags
.fullpool
= B_TRUE
;
7465 trim_flags
.fullpool
= B_FALSE
;
7466 for (int i
= 1; i
< argc
; i
++) {
7467 fnvlist_add_boolean(vdevs
, argv
[i
]);
7471 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7473 fnvlist_free(vdevs
);
7480 * Converts a total number of seconds to a human readable string broken
7481 * down in to days/hours/minutes/seconds.
7484 secs_to_dhms(uint64_t total
, char *buf
)
7486 uint64_t days
= total
/ 60 / 60 / 24;
7487 uint64_t hours
= (total
/ 60 / 60) % 24;
7488 uint64_t mins
= (total
/ 60) % 60;
7489 uint64_t secs
= (total
% 60);
7492 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7493 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7494 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7496 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7497 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7498 (u_longlong_t
)secs
);
7503 * Print out detailed scrub status.
7506 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7508 time_t start
, end
, pause
;
7509 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7510 uint64_t elapsed
, scan_rate
, issue_rate
;
7511 double fraction_done
;
7512 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7513 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7516 printf_color(ANSI_BOLD
, gettext("scan:"));
7519 /* If there's never been a scan, there's not much to say. */
7520 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7521 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7522 (void) printf(gettext("none requested\n"));
7526 start
= ps
->pss_start_time
;
7527 end
= ps
->pss_end_time
;
7528 pause
= ps
->pss_pass_scrub_pause
;
7530 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7532 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7533 ps
->pss_func
== POOL_SCAN_RESILVER
);
7535 /* Scan is finished or canceled. */
7536 if (ps
->pss_state
== DSS_FINISHED
) {
7537 secs_to_dhms(end
- start
, time_buf
);
7539 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7540 (void) printf(gettext("scrub repaired %s "
7541 "in %s with %llu errors on %s"), processed_buf
,
7542 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7544 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7545 (void) printf(gettext("resilvered %s "
7546 "in %s with %llu errors on %s"), processed_buf
,
7547 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7551 } else if (ps
->pss_state
== DSS_CANCELED
) {
7552 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7553 (void) printf(gettext("scrub canceled on %s"),
7555 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7556 (void) printf(gettext("resilver canceled on %s"),
7562 assert(ps
->pss_state
== DSS_SCANNING
);
7564 /* Scan is in progress. Resilvers can't be paused. */
7565 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7567 (void) printf(gettext("scrub in progress since %s"),
7570 (void) printf(gettext("scrub paused since %s"),
7572 (void) printf(gettext("\tscrub started on %s"),
7575 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7576 (void) printf(gettext("resilver in progress since %s"),
7580 scanned
= ps
->pss_examined
;
7581 pass_scanned
= ps
->pss_pass_exam
;
7582 issued
= ps
->pss_issued
;
7583 pass_issued
= ps
->pss_pass_issued
;
7584 total
= ps
->pss_to_examine
;
7586 /* we are only done with a block once we have issued the IO for it */
7587 fraction_done
= (double)issued
/ total
;
7589 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7590 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7591 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7592 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7594 scan_rate
= pass_scanned
/ elapsed
;
7595 issue_rate
= pass_issued
/ elapsed
;
7596 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7597 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7598 secs_to_dhms(total_secs_left
, time_buf
);
7600 /* format all of the numbers we will be reporting */
7601 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7602 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7603 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7604 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7605 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7607 /* do not print estimated time if we have a paused scrub */
7609 (void) printf(gettext("\t%s scanned at %s/s, "
7610 "%s issued at %s/s, %s total\n"),
7611 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7613 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7614 scanned_buf
, issued_buf
, total_buf
);
7617 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7618 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7619 processed_buf
, 100 * fraction_done
);
7620 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7621 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7622 processed_buf
, 100 * fraction_done
);
7626 if (total_secs_left
!= UINT64_MAX
&&
7627 issue_rate
>= 10 * 1024 * 1024) {
7628 (void) printf(gettext(", %s to go\n"), time_buf
);
7630 (void) printf(gettext(", no estimated "
7631 "completion time\n"));
7634 (void) printf(gettext("\n"));
7639 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7641 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7645 printf_color(ANSI_BOLD
, gettext("scan:"));
7648 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7649 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7650 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7651 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7652 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7653 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7654 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7655 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7656 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7657 (bytes_est
+ 1), 100);
7659 /* Format all of the numbers we will be reporting */
7660 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7661 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7662 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7663 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7664 sizeof (bytes_scanned_buf
));
7665 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7666 sizeof (bytes_issued_buf
));
7667 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7668 sizeof (bytes_rebuilt_buf
));
7669 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7670 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7671 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7673 time_t start
= vrs
->vrs_start_time
;
7674 time_t end
= vrs
->vrs_end_time
;
7676 /* Rebuild is finished or canceled. */
7677 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7678 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7679 (void) printf(gettext("resilvered (%s) %s in %s "
7680 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7681 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7683 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7684 (void) printf(gettext("resilver (%s) canceled on %s"),
7685 vdev_name
, ctime(&end
));
7687 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7688 (void) printf(gettext("resilver (%s) in progress since %s"),
7689 vdev_name
, ctime(&start
));
7692 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7694 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7695 MAX(scan_rate
, 1), time_buf
);
7697 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7698 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7699 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7700 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7701 bytes_rebuilt_buf
, scan_pct
);
7703 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7704 if (scan_rate
>= 10 * 1024 * 1024) {
7705 (void) printf(gettext(", %s to go\n"), time_buf
);
7707 (void) printf(gettext(", no estimated "
7708 "completion time\n"));
7711 (void) printf(gettext("\n"));
7716 * Print rebuild status for top-level vdevs.
7719 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7724 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7725 &child
, &children
) != 0)
7728 for (uint_t c
= 0; c
< children
; c
++) {
7729 vdev_rebuild_stat_t
*vrs
;
7732 if (nvlist_lookup_uint64_array(child
[c
],
7733 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7734 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7735 child
[c
], VDEV_NAME_TYPE_ID
);
7736 print_rebuild_status_impl(vrs
, name
);
7743 * As we don't scrub checkpointed blocks, we want to warn the user that we
7744 * skipped scanning some blocks if a checkpoint exists or existed at any
7745 * time during the scan. If a sequential instead of healing reconstruction
7746 * was performed then the blocks were reconstructed. However, their checksums
7747 * have not been verified so we still print the warning.
7750 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7752 if (ps
== NULL
|| pcs
== NULL
)
7755 if (pcs
->pcs_state
== CS_NONE
||
7756 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7759 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7761 if (ps
->pss_state
== DSS_NONE
)
7764 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7765 ps
->pss_end_time
< pcs
->pcs_start_time
)
7768 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7769 (void) printf(gettext(" scan warning: skipped blocks "
7770 "that are only referenced by the checkpoint.\n"));
7772 assert(ps
->pss_state
== DSS_SCANNING
);
7773 (void) printf(gettext(" scan warning: skipping blocks "
7774 "that are only referenced by the checkpoint.\n"));
7779 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7780 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7781 * the last completed (or cancelled) rebuild.
7784 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7788 boolean_t rebuilding
= B_FALSE
;
7789 uint64_t end_time
= 0;
7791 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7792 &child
, &children
) != 0)
7795 for (uint_t c
= 0; c
< children
; c
++) {
7796 vdev_rebuild_stat_t
*vrs
;
7799 if (nvlist_lookup_uint64_array(child
[c
],
7800 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7802 if (vrs
->vrs_end_time
> end_time
)
7803 end_time
= vrs
->vrs_end_time
;
7805 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7806 rebuilding
= B_TRUE
;
7813 if (rebuild_end_time
!= NULL
)
7814 *rebuild_end_time
= end_time
;
7816 return (rebuilding
);
7820 * Print the scan status.
7823 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7825 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7826 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7827 boolean_t active_resilver
= B_FALSE
;
7828 pool_checkpoint_stat_t
*pcs
= NULL
;
7829 pool_scan_stat_t
*ps
= NULL
;
7832 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7833 (uint64_t **)&ps
, &c
) == 0) {
7834 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7835 resilver_end_time
= ps
->pss_end_time
;
7836 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7839 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7840 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7843 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7844 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7846 /* Always print the scrub status when available. */
7848 print_scan_scrub_resilver_status(ps
);
7851 * When there is an active resilver or rebuild print its status.
7852 * Otherwise print the status of the last resilver or rebuild.
7854 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7855 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7856 print_scan_scrub_resilver_status(ps
);
7857 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7858 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7859 print_rebuild_status(zhp
, nvroot
);
7862 (void) nvlist_lookup_uint64_array(nvroot
,
7863 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7864 print_checkpoint_scan_warning(ps
, pcs
);
7868 * Print out detailed removal status.
7871 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7873 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7875 nvlist_t
*config
, *nvroot
;
7880 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7884 * Determine name of vdev.
7886 config
= zpool_get_config(zhp
, NULL
);
7887 nvroot
= fnvlist_lookup_nvlist(config
,
7888 ZPOOL_CONFIG_VDEV_TREE
);
7889 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7890 &child
, &children
) == 0);
7891 assert(prs
->prs_removing_vdev
< children
);
7892 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7893 child
[prs
->prs_removing_vdev
], B_TRUE
);
7895 printf_color(ANSI_BOLD
, gettext("remove: "));
7897 start
= prs
->prs_start_time
;
7898 end
= prs
->prs_end_time
;
7899 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7902 * Removal is finished or canceled.
7904 if (prs
->prs_state
== DSS_FINISHED
) {
7905 uint64_t minutes_taken
= (end
- start
) / 60;
7907 (void) printf(gettext("Removal of vdev %llu copied %s "
7908 "in %lluh%um, completed on %s"),
7909 (longlong_t
)prs
->prs_removing_vdev
,
7911 (u_longlong_t
)(minutes_taken
/ 60),
7912 (uint_t
)(minutes_taken
% 60),
7913 ctime((time_t *)&end
));
7914 } else if (prs
->prs_state
== DSS_CANCELED
) {
7915 (void) printf(gettext("Removal of %s canceled on %s"),
7916 vdev_name
, ctime(&end
));
7918 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7919 double fraction_done
;
7922 assert(prs
->prs_state
== DSS_SCANNING
);
7925 * Removal is in progress.
7927 (void) printf(gettext(
7928 "Evacuation of %s in progress since %s"),
7929 vdev_name
, ctime(&start
));
7931 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7932 total
= prs
->prs_to_copy
;
7933 fraction_done
= (double)copied
/ total
;
7935 /* elapsed time for this pass */
7936 elapsed
= time(NULL
) - prs
->prs_start_time
;
7937 elapsed
= elapsed
> 0 ? elapsed
: 1;
7938 rate
= copied
/ elapsed
;
7939 rate
= rate
> 0 ? rate
: 1;
7940 mins_left
= ((total
- copied
) / rate
) / 60;
7941 hours_left
= mins_left
/ 60;
7943 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7944 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7945 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7948 * do not print estimated time if hours_left is more than
7951 (void) printf(gettext(
7952 "\t%s copied out of %s at %s/s, %.2f%% done"),
7953 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7954 if (hours_left
< (30 * 24)) {
7955 (void) printf(gettext(", %lluh%um to go\n"),
7956 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7958 (void) printf(gettext(
7959 ", (copy is slow, no estimated time)\n"));
7964 if (prs
->prs_mapping_memory
> 0) {
7966 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7967 (void) printf(gettext(
7968 "\t%s memory used for removed device mappings\n"),
7974 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7979 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7982 (void) printf(gettext("checkpoint: "));
7984 start
= pcs
->pcs_start_time
;
7985 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7987 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7988 char *date
= ctime(&start
);
7991 * ctime() adds a newline at the end of the generated
7992 * string, thus the weird format specifier and the
7993 * strlen() call used to chop it off from the output.
7995 (void) printf(gettext("created %.*s, consumes %s\n"),
7996 (int)(strlen(date
) - 1), date
, space_buf
);
8000 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
8002 (void) printf(gettext("discarding, %s remaining.\n"),
8007 print_error_log(zpool_handle_t
*zhp
)
8009 nvlist_t
*nverrlist
= NULL
;
8012 size_t len
= MAXPATHLEN
* 2;
8014 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
8017 (void) printf("errors: Permanent errors have been "
8018 "detected in the following files:\n\n");
8020 pathname
= safe_malloc(len
);
8022 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
8024 uint64_t dsobj
, obj
;
8026 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
8027 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
8029 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
8031 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
8032 (void) printf("%7s %s\n", "", pathname
);
8035 nvlist_free(nverrlist
);
8039 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
8048 (void) printf(gettext("\tspares\n"));
8050 for (i
= 0; i
< nspares
; i
++) {
8051 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
8053 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
8059 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
8068 (void) printf(gettext("\tcache\n"));
8070 for (i
= 0; i
< nl2cache
; i
++) {
8071 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
8073 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
8080 print_dedup_stats(nvlist_t
*config
)
8082 ddt_histogram_t
*ddh
;
8086 char dspace
[6], mspace
[6];
8089 * If the pool was faulted then we may not have been able to
8090 * obtain the config. Otherwise, if we have anything in the dedup
8091 * table continue processing the stats.
8093 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
8094 (uint64_t **)&ddo
, &c
) != 0)
8097 (void) printf("\n");
8098 (void) printf(gettext(" dedup: "));
8099 if (ddo
->ddo_count
== 0) {
8100 (void) printf(gettext("no DDT entries\n"));
8104 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
8105 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
8106 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8107 (u_longlong_t
)ddo
->ddo_count
,
8111 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
8112 (uint64_t **)&dds
, &c
) == 0);
8113 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
8114 (uint64_t **)&ddh
, &c
) == 0);
8115 zpool_dump_ddt(dds
, ddh
);
8119 * Display a summary of pool status. Displays a summary such as:
8123 * reason: One or more devices ...
8124 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8130 * When given the '-v' option, we print out the complete config. If the '-e'
8131 * option is specified, then we print out error rate information as well.
8134 status_callback(zpool_handle_t
*zhp
, void *data
)
8136 status_cbdata_t
*cbp
= data
;
8137 nvlist_t
*config
, *nvroot
;
8139 zpool_status_t reason
;
8140 zpool_errata_t errata
;
8145 config
= zpool_get_config(zhp
, NULL
);
8146 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
8151 * If we were given 'zpool status -x', only report those pools with
8154 if (cbp
->cb_explain
&&
8155 (reason
== ZPOOL_STATUS_OK
||
8156 reason
== ZPOOL_STATUS_VERSION_OLDER
||
8157 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
8158 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
8159 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
8160 if (!cbp
->cb_allpools
) {
8161 (void) printf(gettext("pool '%s' is healthy\n"),
8162 zpool_get_name(zhp
));
8164 cbp
->cb_first
= B_FALSE
;
8170 cbp
->cb_first
= B_FALSE
;
8172 (void) printf("\n");
8174 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8175 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8176 (uint64_t **)&vs
, &c
) == 0);
8178 health
= zpool_get_state_str(zhp
);
8181 printf_color(ANSI_BOLD
, gettext("pool:"));
8182 printf(" %s\n", zpool_get_name(zhp
));
8184 printf_color(ANSI_BOLD
, gettext("state: "));
8186 printf_color(health_str_to_color(health
), "%s", health
);
8191 case ZPOOL_STATUS_MISSING_DEV_R
:
8192 printf_color(ANSI_BOLD
, gettext("status: "));
8193 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8194 "not be opened. Sufficient replicas exist for\n\tthe pool "
8195 "to continue functioning in a degraded state.\n"));
8196 printf_color(ANSI_BOLD
, gettext("action: "));
8197 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8198 "and online it using 'zpool online'.\n"));
8201 case ZPOOL_STATUS_MISSING_DEV_NR
:
8202 printf_color(ANSI_BOLD
, gettext("status: "));
8203 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8204 "not be opened. There are insufficient\n\treplicas for the"
8205 " pool to continue functioning.\n"));
8206 printf_color(ANSI_BOLD
, gettext("action: "));
8207 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8208 "and online it using 'zpool online'.\n"));
8211 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8212 printf_color(ANSI_BOLD
, gettext("status: "));
8213 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8214 "not be used because the label is missing or\n\tinvalid. "
8215 "Sufficient replicas exist for the pool to continue\n\t"
8216 "functioning in a degraded state.\n"));
8217 printf_color(ANSI_BOLD
, gettext("action: "));
8218 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8219 "'zpool replace'.\n"));
8222 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8223 printf_color(ANSI_BOLD
, gettext("status: "));
8224 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8225 "not be used because the label is missing \n\tor invalid. "
8226 "There are insufficient replicas for the pool to "
8227 "continue\n\tfunctioning.\n"));
8228 zpool_explain_recover(zpool_get_handle(zhp
),
8229 zpool_get_name(zhp
), reason
, config
);
8232 case ZPOOL_STATUS_FAILING_DEV
:
8233 printf_color(ANSI_BOLD
, gettext("status: "));
8234 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8235 "experienced an unrecoverable error. An\n\tattempt was "
8236 "made to correct the error. Applications are "
8238 printf_color(ANSI_BOLD
, gettext("action: "));
8239 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8240 "device needs to be replaced, and clear the errors\n\tusing"
8241 " 'zpool clear' or replace the device with 'zpool "
8245 case ZPOOL_STATUS_OFFLINE_DEV
:
8246 printf_color(ANSI_BOLD
, gettext("status: "));
8247 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8248 "been taken offline by the administrator.\n\tSufficient "
8249 "replicas exist for the pool to continue functioning in "
8250 "a\n\tdegraded state.\n"));
8251 printf_color(ANSI_BOLD
, gettext("action: "));
8252 printf_color(ANSI_YELLOW
, gettext("Online the device "
8253 "using 'zpool online' or replace the device with\n\t'zpool "
8257 case ZPOOL_STATUS_REMOVED_DEV
:
8258 printf_color(ANSI_BOLD
, gettext("status: "));
8259 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8260 "been removed by the administrator.\n\tSufficient "
8261 "replicas exist for the pool to continue functioning in "
8262 "a\n\tdegraded state.\n"));
8263 printf_color(ANSI_BOLD
, gettext("action: "));
8264 printf_color(ANSI_YELLOW
, gettext("Online the device "
8265 "using zpool online' or replace the device with\n\t'zpool "
8269 case ZPOOL_STATUS_RESILVERING
:
8270 case ZPOOL_STATUS_REBUILDING
:
8271 printf_color(ANSI_BOLD
, gettext("status: "));
8272 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8273 "currently being resilvered. The pool will\n\tcontinue "
8274 "to function, possibly in a degraded state.\n"));
8275 printf_color(ANSI_BOLD
, gettext("action: "));
8276 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8280 case ZPOOL_STATUS_REBUILD_SCRUB
:
8281 printf_color(ANSI_BOLD
, gettext("status: "));
8282 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8283 "been sequentially resilvered, scrubbing\n\tthe pool "
8284 "is recommended.\n"));
8285 printf_color(ANSI_BOLD
, gettext("action: "));
8286 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8287 "verify all data checksums.\n"));
8290 case ZPOOL_STATUS_CORRUPT_DATA
:
8291 printf_color(ANSI_BOLD
, gettext("status: "));
8292 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8293 "experienced an error resulting in data\n\tcorruption. "
8294 "Applications may be affected.\n"));
8295 printf_color(ANSI_BOLD
, gettext("action: "));
8296 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8297 " if possible. Otherwise restore the\n\tentire pool from "
8301 case ZPOOL_STATUS_CORRUPT_POOL
:
8302 printf_color(ANSI_BOLD
, gettext("status: "));
8303 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8304 "corrupted and the pool cannot be opened.\n"));
8305 zpool_explain_recover(zpool_get_handle(zhp
),
8306 zpool_get_name(zhp
), reason
, config
);
8309 case ZPOOL_STATUS_VERSION_OLDER
:
8310 printf_color(ANSI_BOLD
, gettext("status: "));
8311 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8312 "a legacy on-disk format. The pool can\n\tstill be used, "
8313 "but some features are unavailable.\n"));
8314 printf_color(ANSI_BOLD
, gettext("action: "));
8315 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8316 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8317 "longer be accessible on software that does not support\n\t"
8318 "feature flags.\n"));
8321 case ZPOOL_STATUS_VERSION_NEWER
:
8322 printf_color(ANSI_BOLD
, gettext("status: "));
8323 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8324 "to a newer, incompatible on-disk version.\n\tThe pool "
8325 "cannot be accessed on this system.\n"));
8326 printf_color(ANSI_BOLD
, gettext("action: "));
8327 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8328 "system running more recent software, or\n\trestore the "
8329 "pool from backup.\n"));
8332 case ZPOOL_STATUS_FEAT_DISABLED
:
8333 printf_color(ANSI_BOLD
, gettext("status: "));
8334 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8335 "requested features are not enabled on the pool.\n\t"
8336 "The pool can still be used, but some features are "
8338 printf_color(ANSI_BOLD
, gettext("action: "));
8339 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8340 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8341 "longer be accessible by software that does not support\n\t"
8342 "the features. See zpool-features(7) for details.\n"));
8345 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8346 printf_color(ANSI_BOLD
, gettext("status: "));
8347 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8348 "compatibility list specified, but it could not be\n\t"
8349 "read/parsed at this time. The pool can still be used, "
8350 "but this\n\tshould be investigated.\n"));
8351 printf_color(ANSI_BOLD
, gettext("action: "));
8352 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8353 "'compatibility' property against the\n\t"
8354 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8355 ZPOOL_DATA_COMPAT_D
".\n"));
8358 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
8359 printf_color(ANSI_BOLD
, gettext("status: "));
8360 printf_color(ANSI_YELLOW
, gettext("One or more features "
8361 "are enabled on the pool despite not being\n\t"
8362 "requested by the 'compatibility' property.\n"));
8363 printf_color(ANSI_BOLD
, gettext("action: "));
8364 printf_color(ANSI_YELLOW
, gettext("Consider setting "
8365 "'compatibility' to an appropriate value, or\n\t"
8366 "adding needed features to the relevant file in\n\t"
8367 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
8370 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8371 printf_color(ANSI_BOLD
, gettext("status: "));
8372 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8373 "on this system because it uses the\n\tfollowing feature(s)"
8374 " not supported on this system:\n"));
8375 zpool_print_unsup_feat(config
);
8376 (void) printf("\n");
8377 printf_color(ANSI_BOLD
, gettext("action: "));
8378 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8379 "system that supports the required feature(s),\n\tor "
8380 "restore the pool from backup.\n"));
8383 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8384 printf_color(ANSI_BOLD
, gettext("status: "));
8385 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8386 "accessed in read-only mode on this system. It\n\tcannot be"
8387 " accessed in read-write mode because it uses the "
8388 "following\n\tfeature(s) not supported on this system:\n"));
8389 zpool_print_unsup_feat(config
);
8390 (void) printf("\n");
8391 printf_color(ANSI_BOLD
, gettext("action: "));
8392 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8393 "in read-write mode. Import the pool with\n"
8394 "\t\"-o readonly=on\", access the pool from a system that "
8395 "supports the\n\trequired feature(s), or restore the "
8396 "pool from backup.\n"));
8399 case ZPOOL_STATUS_FAULTED_DEV_R
:
8400 printf_color(ANSI_BOLD
, gettext("status: "));
8401 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8402 "faulted in response to persistent errors.\n\tSufficient "
8403 "replicas exist for the pool to continue functioning "
8404 "in a\n\tdegraded state.\n"));
8405 printf_color(ANSI_BOLD
, gettext("action: "));
8406 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8407 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8410 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8411 printf_color(ANSI_BOLD
, gettext("status: "));
8412 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8413 "faulted in response to persistent errors. There are "
8414 "insufficient replicas for the pool to\n\tcontinue "
8416 printf_color(ANSI_BOLD
, gettext("action: "));
8417 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8418 "pool from a backup source. Manually marking the device\n"
8419 "\trepaired using 'zpool clear' may allow some data "
8420 "to be recovered.\n"));
8423 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8424 printf_color(ANSI_BOLD
, gettext("status: "));
8425 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8426 "because multihost writes failed or were delayed;\n\t"
8427 "another system could import the pool undetected.\n"));
8428 printf_color(ANSI_BOLD
, gettext("action: "));
8429 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8430 " are connected, then reboot your system and\n\timport the "
8434 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8435 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8436 printf_color(ANSI_BOLD
, gettext("status: "));
8437 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8438 "faulted in response to IO failures.\n"));
8439 printf_color(ANSI_BOLD
, gettext("action: "));
8440 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8441 "devices are connected, then run 'zpool clear'.\n"));
8444 case ZPOOL_STATUS_BAD_LOG
:
8445 printf_color(ANSI_BOLD
, gettext("status: "));
8446 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8447 "could not be read.\n"
8448 "\tWaiting for administrator intervention to fix the "
8449 "faulted pool.\n"));
8450 printf_color(ANSI_BOLD
, gettext("action: "));
8451 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8452 "device(s) and run 'zpool online',\n"
8453 "\tor ignore the intent log records by running "
8454 "'zpool clear'.\n"));
8457 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8458 (void) printf(gettext("status: One or more devices are "
8459 "configured to use a non-native block size.\n"
8460 "\tExpect reduced performance.\n"));
8461 (void) printf(gettext("action: Replace affected devices with "
8462 "devices that support the\n\tconfigured block size, or "
8463 "migrate data to a properly configured\n\tpool.\n"));
8466 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8467 printf_color(ANSI_BOLD
, gettext("status: "));
8468 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8469 " and system hostid on imported pool.\n\tThis pool was "
8470 "previously imported into a system with a different "
8471 "hostid,\n\tand then was verbatim imported into this "
8473 printf_color(ANSI_BOLD
, gettext("action: "));
8474 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8475 "systems on which it is imported.\n"
8476 "\tThen import it to correct the mismatch.\n"));
8479 case ZPOOL_STATUS_ERRATA
:
8480 printf_color(ANSI_BOLD
, gettext("status: "));
8481 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8485 case ZPOOL_ERRATA_NONE
:
8488 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8489 printf_color(ANSI_BOLD
, gettext("action: "));
8490 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8491 " run 'zpool scrub'.\n"));
8494 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8495 (void) printf(gettext("\tExisting encrypted datasets "
8496 "contain an on-disk incompatibility\n\twhich "
8497 "needs to be corrected.\n"));
8498 printf_color(ANSI_BOLD
, gettext("action: "));
8499 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8500 " backup existing encrypted datasets to new\n\t"
8501 "encrypted datasets and destroy the old ones. "
8502 "'zfs mount -o ro' can\n\tbe used to temporarily "
8503 "mount existing encrypted datasets readonly.\n"));
8506 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8507 (void) printf(gettext("\tExisting encrypted snapshots "
8508 "and bookmarks contain an on-disk\n\tincompat"
8509 "ibility. This may cause on-disk corruption if "
8510 "they are used\n\twith 'zfs recv'.\n"));
8511 printf_color(ANSI_BOLD
, gettext("action: "));
8512 printf_color(ANSI_YELLOW
, gettext("To correct the"
8513 "issue, enable the bookmark_v2 feature. No "
8514 "additional\n\taction is needed if there are no "
8515 "encrypted snapshots or bookmarks.\n\tIf preserving"
8516 "the encrypted snapshots and bookmarks is required,"
8517 " use\n\ta non-raw send to backup and restore them."
8518 " Alternately, they may be\n\tremoved to resolve "
8519 "the incompatibility.\n"));
8524 * All errata which allow the pool to be imported
8525 * must contain an action message.
8533 * The remaining errors can't actually be generated, yet.
8535 assert(reason
== ZPOOL_STATUS_OK
);
8538 if (msgid
!= NULL
) {
8540 printf_color(ANSI_BOLD
, gettext("see:"));
8542 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8546 if (config
!= NULL
) {
8548 nvlist_t
**spares
, **l2cache
;
8549 uint_t nspares
, nl2cache
;
8550 pool_checkpoint_stat_t
*pcs
= NULL
;
8551 pool_removal_stat_t
*prs
= NULL
;
8553 print_scan_status(zhp
, nvroot
);
8555 (void) nvlist_lookup_uint64_array(nvroot
,
8556 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8557 print_removal_status(zhp
, prs
);
8559 (void) nvlist_lookup_uint64_array(nvroot
,
8560 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8561 print_checkpoint_status(pcs
);
8563 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8564 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8565 if (cbp
->cb_namewidth
< 10)
8566 cbp
->cb_namewidth
= 10;
8568 color_start(ANSI_BOLD
);
8569 (void) printf(gettext("config:\n\n"));
8570 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8571 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8575 if (cbp
->cb_print_slow_ios
) {
8576 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8579 if (cbp
->vcdl
!= NULL
)
8580 print_cmd_columns(cbp
->vcdl
, 0);
8584 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8587 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8588 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8589 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8591 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8592 &l2cache
, &nl2cache
) == 0)
8593 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8595 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8596 &spares
, &nspares
) == 0)
8597 print_spares(zhp
, cbp
, spares
, nspares
);
8599 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8601 nvlist_t
*nverrlist
= NULL
;
8604 * If the approximate error count is small, get a
8605 * precise count by fetching the entire log and
8606 * uniquifying the results.
8608 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8609 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8614 while ((elem
= nvlist_next_nvpair(nverrlist
,
8619 nvlist_free(nverrlist
);
8621 (void) printf("\n");
8624 (void) printf(gettext("errors: No known data "
8626 else if (!cbp
->cb_verbose
)
8627 (void) printf(gettext("errors: %llu data "
8628 "errors, use '-v' for a list\n"),
8629 (u_longlong_t
)nerr
);
8631 print_error_log(zhp
);
8634 if (cbp
->cb_dedup_stats
)
8635 print_dedup_stats(config
);
8637 (void) printf(gettext("config: The configuration cannot be "
8645 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8646 * [interval [count]]
8648 * -c CMD For each vdev, run command CMD
8649 * -i Display vdev initialization status.
8650 * -g Display guid for individual vdev name.
8651 * -L Follow links when resolving vdev path name.
8652 * -p Display values in parsable (exact) format.
8653 * -P Display full path for vdev name.
8654 * -s Display slow IOs column.
8655 * -v Display complete error logs
8656 * -x Display only pools with potential problems
8657 * -D Display dedup status (undocumented)
8658 * -t Display vdev TRIM status.
8659 * -T Display a timestamp in date(1) or Unix format
8661 * Describes the health status of all pools or some subset.
8664 zpool_do_status(int argc
, char **argv
)
8669 unsigned long count
= 0;
8670 status_cbdata_t cb
= { 0 };
8674 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8679 gettext("Can't set -c flag twice\n"));
8683 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8684 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8685 fprintf(stderr
, gettext(
8686 "Can't run -c, disabled by "
8687 "ZPOOL_SCRIPTS_ENABLED.\n"));
8691 if ((getuid() <= 0 || geteuid() <= 0) &&
8692 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8693 fprintf(stderr
, gettext(
8694 "Can't run -c with root privileges "
8695 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8701 cb
.cb_print_vdev_init
= B_TRUE
;
8704 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8707 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8710 cb
.cb_literal
= B_TRUE
;
8713 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8716 cb
.cb_print_slow_ios
= B_TRUE
;
8719 cb
.cb_verbose
= B_TRUE
;
8722 cb
.cb_explain
= B_TRUE
;
8725 cb
.cb_dedup_stats
= B_TRUE
;
8728 cb
.cb_print_vdev_trim
= B_TRUE
;
8731 get_timestamp_arg(*optarg
);
8734 if (optopt
== 'c') {
8735 print_zpool_script_list("status");
8739 gettext("invalid option '%c'\n"), optopt
);
8748 get_interval_count(&argc
, argv
, &interval
, &count
);
8751 cb
.cb_allpools
= B_TRUE
;
8753 cb
.cb_first
= B_TRUE
;
8754 cb
.cb_print_status
= B_TRUE
;
8757 if (timestamp_fmt
!= NODATE
)
8758 print_timestamp(timestamp_fmt
);
8761 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8764 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8765 cb
.cb_literal
, status_callback
, &cb
);
8767 if (cb
.vcdl
!= NULL
)
8768 free_vdev_cmd_data_list(cb
.vcdl
);
8770 if (argc
== 0 && cb
.cb_count
== 0)
8771 (void) fprintf(stderr
, gettext("no pools available\n"));
8772 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8773 (void) printf(gettext("all pools are healthy\n"));
8781 if (count
!= 0 && --count
== 0)
8784 (void) fsleep(interval
);
8790 typedef struct upgrade_cbdata
{
8793 uint64_t cb_version
;
8798 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8800 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8801 int *count
= (int *)unsupp_fs
;
8803 if (zfs_version
> ZPL_VERSION
) {
8804 (void) printf(gettext("%s (v%d) is not supported by this "
8805 "implementation of ZFS.\n"),
8806 zfs_get_name(zhp
), zfs_version
);
8810 zfs_iter_filesystems(zhp
, 0, check_unsupp_fs
, unsupp_fs
);
8818 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8822 uint64_t oldversion
;
8825 config
= zpool_get_config(zhp
, NULL
);
8826 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8829 char compat
[ZFS_MAXPROPLEN
];
8830 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8831 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8834 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8835 assert(oldversion
< version
);
8837 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8842 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8843 "to %d unsupported filesystems (max v%d).\n"),
8844 unsupp_fs
, (int)ZPL_VERSION
);
8848 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
8849 (void) fprintf(stderr
, gettext("Upgrade not performed because "
8850 "'compatibility' property set to '"
8851 ZPOOL_COMPAT_LEGACY
"'.\n"));
8855 ret
= zpool_upgrade(zhp
, version
);
8859 if (version
>= SPA_VERSION_FEATURES
) {
8860 (void) printf(gettext("Successfully upgraded "
8861 "'%s' from version %llu to feature flags.\n"),
8862 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8864 (void) printf(gettext("Successfully upgraded "
8865 "'%s' from version %llu to version %llu.\n"),
8866 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8867 (u_longlong_t
)version
);
8874 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8877 boolean_t firstff
= B_TRUE
;
8878 nvlist_t
*enabled
= zpool_get_features(zhp
);
8880 char compat
[ZFS_MAXPROPLEN
];
8881 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8882 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8885 boolean_t requested_features
[SPA_FEATURES
];
8886 if (zpool_do_load_compat(compat
, requested_features
) !=
8887 ZPOOL_COMPATIBILITY_OK
)
8891 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8892 const char *fname
= spa_feature_table
[i
].fi_uname
;
8893 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8895 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8898 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
8900 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8901 ret
= zpool_set_prop(zhp
, propname
,
8902 ZFS_FEATURE_ENABLED
);
8910 (void) printf(gettext("Enabled the "
8911 "following features on '%s':\n"),
8912 zpool_get_name(zhp
));
8915 (void) printf(gettext(" %s\n"), fname
);
8926 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8928 upgrade_cbdata_t
*cbp
= arg
;
8931 boolean_t modified_pool
= B_FALSE
;
8934 config
= zpool_get_config(zhp
, NULL
);
8935 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8938 assert(SPA_VERSION_IS_SUPPORTED(version
));
8940 if (version
< cbp
->cb_version
) {
8941 cbp
->cb_first
= B_FALSE
;
8942 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8945 modified_pool
= B_TRUE
;
8948 * If they did "zpool upgrade -a", then we could
8949 * be doing ioctls to different pools. We need
8950 * to log this history once to each pool, and bypass
8951 * the normal history logging that happens in main().
8953 (void) zpool_log_history(g_zfs
, history_str
);
8954 log_history
= B_FALSE
;
8957 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8959 ret
= upgrade_enable_all(zhp
, &count
);
8964 cbp
->cb_first
= B_FALSE
;
8965 modified_pool
= B_TRUE
;
8969 if (modified_pool
) {
8970 (void) printf("\n");
8971 (void) after_zpool_upgrade(zhp
);
8978 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8980 upgrade_cbdata_t
*cbp
= arg
;
8984 config
= zpool_get_config(zhp
, NULL
);
8985 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8988 assert(SPA_VERSION_IS_SUPPORTED(version
));
8990 if (version
< SPA_VERSION_FEATURES
) {
8991 if (cbp
->cb_first
) {
8992 (void) printf(gettext("The following pools are "
8993 "formatted with legacy version numbers and can\n"
8994 "be upgraded to use feature flags. After "
8995 "being upgraded, these pools\nwill no "
8996 "longer be accessible by software that does not "
8997 "support feature\nflags.\n\n"
8998 "Note that setting a pool's 'compatibility' "
8999 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
9000 "inhibit upgrades.\n\n"));
9001 (void) printf(gettext("VER POOL\n"));
9002 (void) printf(gettext("--- ------------\n"));
9003 cbp
->cb_first
= B_FALSE
;
9006 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
9007 zpool_get_name(zhp
));
9014 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
9016 upgrade_cbdata_t
*cbp
= arg
;
9020 config
= zpool_get_config(zhp
, NULL
);
9021 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9024 if (version
>= SPA_VERSION_FEATURES
) {
9026 boolean_t poolfirst
= B_TRUE
;
9027 nvlist_t
*enabled
= zpool_get_features(zhp
);
9029 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9030 const char *fguid
= spa_feature_table
[i
].fi_guid
;
9031 const char *fname
= spa_feature_table
[i
].fi_uname
;
9033 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
9036 if (!nvlist_exists(enabled
, fguid
)) {
9037 if (cbp
->cb_first
) {
9038 (void) printf(gettext("\nSome "
9039 "supported features are not "
9040 "enabled on the following pools. "
9041 "Once a\nfeature is enabled the "
9042 "pool may become incompatible with "
9043 "software\nthat does not support "
9045 "zpool-features(7) for "
9047 "Note that the pool "
9048 "'compatibility' feature can be "
9049 "used to inhibit\nfeature "
9051 (void) printf(gettext("POOL "
9053 (void) printf(gettext("------"
9055 cbp
->cb_first
= B_FALSE
;
9059 (void) printf(gettext("%s\n"),
9060 zpool_get_name(zhp
));
9061 poolfirst
= B_FALSE
;
9064 (void) printf(gettext(" %s\n"), fname
);
9067 * If they did "zpool upgrade -a", then we could
9068 * be doing ioctls to different pools. We need
9069 * to log this history once to each pool, and bypass
9070 * the normal history logging that happens in main().
9072 (void) zpool_log_history(g_zfs
, history_str
);
9073 log_history
= B_FALSE
;
9081 upgrade_one(zpool_handle_t
*zhp
, void *data
)
9083 boolean_t modified_pool
= B_FALSE
;
9084 upgrade_cbdata_t
*cbp
= data
;
9085 uint64_t cur_version
;
9088 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
9089 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
9090 "Pool 'log' must be renamed using export and import"
9095 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
9096 if (cur_version
> cbp
->cb_version
) {
9097 (void) printf(gettext("Pool '%s' is already formatted "
9098 "using more current version '%llu'.\n\n"),
9099 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
9103 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
9104 (void) printf(gettext("Pool '%s' is already formatted "
9105 "using version %llu.\n\n"), zpool_get_name(zhp
),
9106 (u_longlong_t
)cbp
->cb_version
);
9110 if (cur_version
!= cbp
->cb_version
) {
9111 modified_pool
= B_TRUE
;
9112 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9117 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9119 ret
= upgrade_enable_all(zhp
, &count
);
9124 modified_pool
= B_TRUE
;
9125 } else if (cur_version
== SPA_VERSION
) {
9126 (void) printf(gettext("Pool '%s' already has all "
9127 "supported and requested features enabled.\n"),
9128 zpool_get_name(zhp
));
9132 if (modified_pool
) {
9133 (void) printf("\n");
9134 (void) after_zpool_upgrade(zhp
);
9143 * zpool upgrade [-V version] <-a | pool ...>
9145 * With no arguments, display downrev'd ZFS pool available for upgrade.
9146 * Individual pools can be upgraded by specifying the pool, and '-a' will
9147 * upgrade all pools.
9150 zpool_do_upgrade(int argc
, char **argv
)
9153 upgrade_cbdata_t cb
= { 0 };
9155 boolean_t showversions
= B_FALSE
;
9156 boolean_t upgradeall
= B_FALSE
;
9161 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
9164 upgradeall
= B_TRUE
;
9167 showversions
= B_TRUE
;
9170 cb
.cb_version
= strtoll(optarg
, &end
, 10);
9172 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
9173 (void) fprintf(stderr
,
9174 gettext("invalid version '%s'\n"), optarg
);
9179 (void) fprintf(stderr
, gettext("missing argument for "
9180 "'%c' option\n"), optopt
);
9184 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9195 if (cb
.cb_version
== 0) {
9196 cb
.cb_version
= SPA_VERSION
;
9197 } else if (!upgradeall
&& argc
== 0) {
9198 (void) fprintf(stderr
, gettext("-V option is "
9199 "incompatible with other arguments\n"));
9204 if (upgradeall
|| argc
!= 0) {
9205 (void) fprintf(stderr
, gettext("-v option is "
9206 "incompatible with other arguments\n"));
9209 } else if (upgradeall
) {
9211 (void) fprintf(stderr
, gettext("-a option should not "
9212 "be used along with a pool name\n"));
9217 (void) printf(gettext("This system supports ZFS pool feature "
9222 (void) printf(gettext("The following features are "
9224 (void) printf(gettext("FEAT DESCRIPTION\n"));
9225 (void) printf("----------------------------------------------"
9226 "---------------\n");
9227 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9228 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9229 if (!fi
->fi_zfs_mod_supported
)
9232 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9233 " (read-only compatible)" : "";
9235 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9236 (void) printf(" %s\n", fi
->fi_desc
);
9238 (void) printf("\n");
9240 (void) printf(gettext("The following legacy versions are also "
9242 (void) printf(gettext("VER DESCRIPTION\n"));
9243 (void) printf("--- -----------------------------------------"
9244 "---------------\n");
9245 (void) printf(gettext(" 1 Initial ZFS version\n"));
9246 (void) printf(gettext(" 2 Ditto blocks "
9247 "(replicated metadata)\n"));
9248 (void) printf(gettext(" 3 Hot spares and double parity "
9250 (void) printf(gettext(" 4 zpool history\n"));
9251 (void) printf(gettext(" 5 Compression using the gzip "
9253 (void) printf(gettext(" 6 bootfs pool property\n"));
9254 (void) printf(gettext(" 7 Separate intent log devices\n"));
9255 (void) printf(gettext(" 8 Delegated administration\n"));
9256 (void) printf(gettext(" 9 refquota and refreservation "
9258 (void) printf(gettext(" 10 Cache devices\n"));
9259 (void) printf(gettext(" 11 Improved scrub performance\n"));
9260 (void) printf(gettext(" 12 Snapshot properties\n"));
9261 (void) printf(gettext(" 13 snapused property\n"));
9262 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9263 (void) printf(gettext(" 15 user/group space accounting\n"));
9264 (void) printf(gettext(" 16 stmf property support\n"));
9265 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9266 (void) printf(gettext(" 18 Snapshot user holds\n"));
9267 (void) printf(gettext(" 19 Log device removal\n"));
9268 (void) printf(gettext(" 20 Compression using zle "
9269 "(zero-length encoding)\n"));
9270 (void) printf(gettext(" 21 Deduplication\n"));
9271 (void) printf(gettext(" 22 Received properties\n"));
9272 (void) printf(gettext(" 23 Slim ZIL\n"));
9273 (void) printf(gettext(" 24 System attributes\n"));
9274 (void) printf(gettext(" 25 Improved scrub stats\n"));
9275 (void) printf(gettext(" 26 Improved snapshot deletion "
9277 (void) printf(gettext(" 27 Improved snapshot creation "
9279 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9280 (void) printf(gettext("\nFor more information on a particular "
9281 "version, including supported releases,\n"));
9282 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9283 } else if (argc
== 0 && upgradeall
) {
9284 cb
.cb_first
= B_TRUE
;
9285 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9286 if (ret
== 0 && cb
.cb_first
) {
9287 if (cb
.cb_version
== SPA_VERSION
) {
9288 (void) printf(gettext("All pools are already "
9289 "formatted using feature flags.\n\n"));
9290 (void) printf(gettext("Every feature flags "
9291 "pool already has all supported and "
9292 "requested features enabled.\n"));
9294 (void) printf(gettext("All pools are already "
9295 "formatted with version %llu or higher.\n"),
9296 (u_longlong_t
)cb
.cb_version
);
9299 } else if (argc
== 0) {
9300 cb
.cb_first
= B_TRUE
;
9301 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9305 (void) printf(gettext("All pools are formatted "
9306 "using feature flags.\n\n"));
9308 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9309 "for a list of available legacy versions.\n"));
9312 cb
.cb_first
= B_TRUE
;
9313 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9317 (void) printf(gettext("Every feature flags pool has "
9318 "all supported and requested features enabled.\n"));
9320 (void) printf(gettext("\n"));
9323 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9324 B_FALSE
, upgrade_one
, &cb
);
9330 typedef struct hist_cbdata
{
9337 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9343 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9344 &records
, &numrecords
) == 0);
9345 for (i
= 0; i
< numrecords
; i
++) {
9346 nvlist_t
*rec
= records
[i
];
9349 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9353 tsec
= fnvlist_lookup_uint64(records
[i
],
9355 (void) localtime_r(&tsec
, &t
);
9356 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9359 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9360 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9361 ZPOOL_HIST_ELAPSED_NS
);
9362 (void) snprintf(tbuf
+ strlen(tbuf
),
9363 sizeof (tbuf
) - strlen(tbuf
),
9364 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9367 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9368 (void) printf("%s %s", tbuf
,
9369 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9370 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9372 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9375 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9376 (void) printf("%s unrecognized record:\n",
9378 dump_nvlist(rec
, 4);
9381 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9382 zfs_history_event_names
[ievent
],
9383 (longlong_t
)fnvlist_lookup_uint64(
9384 rec
, ZPOOL_HIST_TXG
),
9385 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9386 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9389 (void) printf("%s [txg:%lld] %s", tbuf
,
9390 (longlong_t
)fnvlist_lookup_uint64(
9391 rec
, ZPOOL_HIST_TXG
),
9392 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9393 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9394 (void) printf(" %s (%llu)",
9395 fnvlist_lookup_string(rec
,
9397 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9400 (void) printf(" %s", fnvlist_lookup_string(rec
,
9401 ZPOOL_HIST_INT_STR
));
9402 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9405 (void) printf("%s ioctl %s\n", tbuf
,
9406 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9407 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9408 (void) printf(" input:\n");
9409 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9410 ZPOOL_HIST_INPUT_NVL
), 8);
9412 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9413 (void) printf(" output:\n");
9414 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9415 ZPOOL_HIST_OUTPUT_NVL
), 8);
9417 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9418 (void) printf(" output nvlist omitted; "
9419 "original size: %lldKB\n",
9420 (longlong_t
)fnvlist_lookup_int64(rec
,
9421 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9423 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9424 (void) printf(" errno: %lld\n",
9425 (longlong_t
)fnvlist_lookup_int64(rec
,
9431 (void) printf("%s unrecognized record:\n", tbuf
);
9432 dump_nvlist(rec
, 4);
9436 (void) printf("\n");
9439 (void) printf(" [");
9440 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9441 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9442 struct passwd
*pwd
= getpwuid(who
);
9443 (void) printf("user %d ", (int)who
);
9445 (void) printf("(%s) ", pwd
->pw_name
);
9447 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9448 (void) printf("on %s",
9449 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9451 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9452 (void) printf(":%s",
9453 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9457 (void) printf("\n");
9462 * Print out the command history for a specific pool.
9465 get_history_one(zpool_handle_t
*zhp
, void *data
)
9469 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9471 boolean_t eof
= B_FALSE
;
9473 cb
->first
= B_FALSE
;
9475 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9478 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9481 print_history_records(nvhis
, cb
);
9484 (void) printf("\n");
9490 * zpool history <pool>
9492 * Displays the history of commands that modified pools.
9495 zpool_do_history(int argc
, char **argv
)
9497 hist_cbdata_t cbdata
= { 0 };
9501 cbdata
.first
= B_TRUE
;
9503 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9506 cbdata
.longfmt
= B_TRUE
;
9509 cbdata
.internal
= B_TRUE
;
9512 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9520 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9521 B_FALSE
, get_history_one
, &cbdata
);
9523 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9524 (void) fprintf(stderr
, gettext("no pools available\n"));
9531 typedef struct ev_opts
{
9536 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9540 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9542 char ctime_str
[26], str
[32], *ptr
;
9546 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9547 memset(str
, ' ', 32);
9548 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9549 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9550 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9551 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9552 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9554 (void) printf(gettext("%s\t"), str
);
9556 (void) printf(gettext("%s "), str
);
9558 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9559 (void) printf(gettext("%s\n"), ptr
);
9563 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9567 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9568 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9570 data_type_t type
= nvpair_type(nvp
);
9571 const char *name
= nvpair_name(nvp
);
9581 printf(gettext("%*s%s = "), depth
, "", name
);
9584 case DATA_TYPE_BOOLEAN
:
9585 printf(gettext("%s"), "1");
9588 case DATA_TYPE_BOOLEAN_VALUE
:
9589 (void) nvpair_value_boolean_value(nvp
, &b
);
9590 printf(gettext("%s"), b
? "1" : "0");
9593 case DATA_TYPE_BYTE
:
9594 (void) nvpair_value_byte(nvp
, &i8
);
9595 printf(gettext("0x%x"), i8
);
9598 case DATA_TYPE_INT8
:
9599 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9600 printf(gettext("0x%x"), i8
);
9603 case DATA_TYPE_UINT8
:
9604 (void) nvpair_value_uint8(nvp
, &i8
);
9605 printf(gettext("0x%x"), i8
);
9608 case DATA_TYPE_INT16
:
9609 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9610 printf(gettext("0x%x"), i16
);
9613 case DATA_TYPE_UINT16
:
9614 (void) nvpair_value_uint16(nvp
, &i16
);
9615 printf(gettext("0x%x"), i16
);
9618 case DATA_TYPE_INT32
:
9619 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9620 printf(gettext("0x%x"), i32
);
9623 case DATA_TYPE_UINT32
:
9624 (void) nvpair_value_uint32(nvp
, &i32
);
9625 printf(gettext("0x%x"), i32
);
9628 case DATA_TYPE_INT64
:
9629 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9630 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9633 case DATA_TYPE_UINT64
:
9634 (void) nvpair_value_uint64(nvp
, &i64
);
9636 * translate vdev state values to readable
9637 * strings to aide zpool events consumers
9640 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9642 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9643 printf(gettext("\"%s\" (0x%llx)"),
9644 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9647 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9651 case DATA_TYPE_HRTIME
:
9652 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9653 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9656 case DATA_TYPE_STRING
:
9657 (void) nvpair_value_string(nvp
, &str
);
9658 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9661 case DATA_TYPE_NVLIST
:
9662 printf(gettext("(embedded nvlist)\n"));
9663 (void) nvpair_value_nvlist(nvp
, &cnv
);
9664 zpool_do_events_nvprint(cnv
, depth
+ 8);
9665 printf(gettext("%*s(end %s)"), depth
, "", name
);
9668 case DATA_TYPE_NVLIST_ARRAY
: {
9672 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9673 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9674 for (i
= 0; i
< nelem
; i
++) {
9675 printf(gettext("%*s%s[%d] = %s\n"),
9676 depth
, "", name
, i
, "(embedded nvlist)");
9677 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9678 printf(gettext("%*s(end %s[%i])\n"),
9679 depth
, "", name
, i
);
9681 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9685 case DATA_TYPE_INT8_ARRAY
: {
9689 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9690 for (i
= 0; i
< nelem
; i
++)
9691 printf(gettext("0x%x "), val
[i
]);
9696 case DATA_TYPE_UINT8_ARRAY
: {
9700 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9701 for (i
= 0; i
< nelem
; i
++)
9702 printf(gettext("0x%x "), val
[i
]);
9707 case DATA_TYPE_INT16_ARRAY
: {
9711 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9712 for (i
= 0; i
< nelem
; i
++)
9713 printf(gettext("0x%x "), val
[i
]);
9718 case DATA_TYPE_UINT16_ARRAY
: {
9722 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9723 for (i
= 0; i
< nelem
; i
++)
9724 printf(gettext("0x%x "), val
[i
]);
9729 case DATA_TYPE_INT32_ARRAY
: {
9733 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9734 for (i
= 0; i
< nelem
; i
++)
9735 printf(gettext("0x%x "), val
[i
]);
9740 case DATA_TYPE_UINT32_ARRAY
: {
9744 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9745 for (i
= 0; i
< nelem
; i
++)
9746 printf(gettext("0x%x "), val
[i
]);
9751 case DATA_TYPE_INT64_ARRAY
: {
9755 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9756 for (i
= 0; i
< nelem
; i
++)
9757 printf(gettext("0x%llx "),
9758 (u_longlong_t
)val
[i
]);
9763 case DATA_TYPE_UINT64_ARRAY
: {
9767 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9768 for (i
= 0; i
< nelem
; i
++)
9769 printf(gettext("0x%llx "),
9770 (u_longlong_t
)val
[i
]);
9775 case DATA_TYPE_STRING_ARRAY
: {
9779 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9780 for (i
= 0; i
< nelem
; i
++)
9781 printf(gettext("\"%s\" "),
9782 str
[i
] ? str
[i
] : "<NULL>");
9787 case DATA_TYPE_BOOLEAN_ARRAY
:
9788 case DATA_TYPE_BYTE_ARRAY
:
9789 case DATA_TYPE_DOUBLE
:
9790 case DATA_TYPE_DONTCARE
:
9791 case DATA_TYPE_UNKNOWN
:
9792 printf(gettext("<unknown>"));
9796 printf(gettext("\n"));
9801 zpool_do_events_next(ev_opts_t
*opts
)
9804 int zevent_fd
, ret
, dropped
;
9807 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9808 VERIFY(zevent_fd
>= 0);
9810 if (!opts
->scripted
)
9811 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9814 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9815 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9816 if (ret
|| nvl
== NULL
)
9820 (void) printf(gettext("dropped %d events\n"), dropped
);
9822 if (strlen(opts
->poolname
) > 0 &&
9823 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9824 strcmp(opts
->poolname
, pool
) != 0)
9827 zpool_do_events_short(nvl
, opts
);
9829 if (opts
->verbose
) {
9830 zpool_do_events_nvprint(nvl
, 8);
9831 printf(gettext("\n"));
9833 (void) fflush(stdout
);
9838 VERIFY(0 == close(zevent_fd
));
9844 zpool_do_events_clear(void)
9848 ret
= zpool_events_clear(g_zfs
, &count
);
9850 (void) printf(gettext("cleared %d events\n"), count
);
9856 * zpool events [-vHf [pool] | -c]
9858 * Displays events logs by ZFS.
9861 zpool_do_events(int argc
, char **argv
)
9863 ev_opts_t opts
= { 0 };
9868 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9883 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9892 (void) fprintf(stderr
, gettext("too many arguments\n"));
9894 } else if (argc
== 1) {
9895 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9896 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9897 (void) fprintf(stderr
,
9898 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9903 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9905 (void) fprintf(stderr
,
9906 gettext("invalid options combined with -c\n"));
9911 ret
= zpool_do_events_clear();
9913 ret
= zpool_do_events_next(&opts
);
9919 get_callback_vdev(zpool_handle_t
*zhp
, char *vdevname
, void *data
)
9921 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9922 char value
[ZFS_MAXPROPLEN
];
9923 zprop_source_t srctype
;
9925 for (zprop_list_t
*pl
= cbp
->cb_proplist
; pl
!= NULL
;
9929 * If the first property is pool name, it is a special
9930 * placeholder that we can skip. This will also skip
9931 * over the name property when 'all' is specified.
9933 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9934 pl
== cbp
->cb_proplist
)
9937 if (pl
->pl_prop
== ZPROP_INVAL
) {
9938 prop_name
= pl
->pl_user_prop
;
9940 prop_name
= (char *)vdev_prop_to_name(pl
->pl_prop
);
9942 if (zpool_get_vdev_prop(zhp
, vdevname
, pl
->pl_prop
,
9943 prop_name
, value
, sizeof (value
), &srctype
,
9944 cbp
->cb_literal
) == 0) {
9945 zprop_print_one_property(vdevname
, cbp
, prop_name
,
9946 value
, srctype
, NULL
, NULL
);
9954 get_callback_vdev_width_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9956 zpool_handle_t
*zhp
= zhp_data
;
9957 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9958 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9959 cbp
->cb_vdevs
.cb_name_flags
);
9962 /* Adjust the column widths for the vdev properties */
9963 ret
= vdev_expand_proplist(zhp
, vdevname
, &cbp
->cb_proplist
);
9969 get_callback_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9971 zpool_handle_t
*zhp
= zhp_data
;
9972 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9973 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9974 cbp
->cb_vdevs
.cb_name_flags
);
9977 /* Display the properties */
9978 ret
= get_callback_vdev(zhp
, vdevname
, data
);
9984 get_callback(zpool_handle_t
*zhp
, void *data
)
9986 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9987 char value
[MAXNAMELEN
];
9988 zprop_source_t srctype
;
9992 if (cbp
->cb_type
== ZFS_TYPE_VDEV
) {
9993 if (strcmp(cbp
->cb_vdevs
.cb_names
[0], "all-vdevs") == 0) {
9994 for_each_vdev(zhp
, get_callback_vdev_width_cb
, data
);
9995 for_each_vdev(zhp
, get_callback_vdev_cb
, data
);
9997 /* Adjust column widths for vdev properties */
9998 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10000 vdev_expand_proplist(zhp
,
10001 cbp
->cb_vdevs
.cb_names
[vid
],
10002 &cbp
->cb_proplist
);
10004 /* Display the properties */
10005 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10007 get_callback_vdev(zhp
,
10008 cbp
->cb_vdevs
.cb_names
[vid
], data
);
10012 assert(cbp
->cb_type
== ZFS_TYPE_POOL
);
10013 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
10015 * Skip the special fake placeholder. This will also
10016 * skip over the name property when 'all' is specified.
10018 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
10019 pl
== cbp
->cb_proplist
)
10022 if (pl
->pl_prop
== ZPROP_INVAL
&&
10023 (zpool_prop_feature(pl
->pl_user_prop
) ||
10024 zpool_prop_unsupported(pl
->pl_user_prop
))) {
10025 srctype
= ZPROP_SRC_LOCAL
;
10027 if (zpool_prop_get_feature(zhp
,
10028 pl
->pl_user_prop
, value
,
10029 sizeof (value
)) == 0) {
10030 zprop_print_one_property(
10031 zpool_get_name(zhp
), cbp
,
10032 pl
->pl_user_prop
, value
, srctype
,
10036 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
10037 sizeof (value
), &srctype
,
10038 cbp
->cb_literal
) != 0)
10041 zprop_print_one_property(zpool_get_name(zhp
),
10042 cbp
, zpool_prop_to_name(pl
->pl_prop
),
10043 value
, srctype
, NULL
, NULL
);
10052 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10054 * -H Scripted mode. Don't display headers, and separate properties
10056 * -o List of columns to display. Defaults to
10057 * "name,property,value,source".
10058 * -p Display values in parsable (exact) format.
10060 * Get properties of pools in the system. Output space statistics
10061 * for each one as well as other attributes.
10064 zpool_do_get(int argc
, char **argv
)
10066 zprop_get_cbdata_t cb
= { 0 };
10067 zprop_list_t fake_name
= { 0 };
10071 char *propstr
= NULL
;
10073 cb
.cb_first
= B_TRUE
;
10076 * Set up default columns and sources.
10078 cb
.cb_sources
= ZPROP_SRC_ALL
;
10079 cb
.cb_columns
[0] = GET_COL_NAME
;
10080 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
10081 cb
.cb_columns
[2] = GET_COL_VALUE
;
10082 cb
.cb_columns
[3] = GET_COL_SOURCE
;
10083 cb
.cb_type
= ZFS_TYPE_POOL
;
10084 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10085 current_prop_type
= cb
.cb_type
;
10087 /* check options */
10088 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
10091 cb
.cb_literal
= B_TRUE
;
10094 cb
.cb_scripted
= B_TRUE
;
10097 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
10099 while (*optarg
!= '\0') {
10100 static char *col_subopts
[] =
10101 { "name", "property", "value", "source",
10104 if (i
== ZFS_GET_NCOLS
) {
10105 (void) fprintf(stderr
, gettext("too "
10106 "many fields given to -o "
10111 switch (getsubopt(&optarg
, col_subopts
,
10114 cb
.cb_columns
[i
++] = GET_COL_NAME
;
10117 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
10120 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
10123 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
10127 (void) fprintf(stderr
,
10128 gettext("\"all\" conflicts "
10129 "with specific fields "
10130 "given to -o option\n"));
10133 cb
.cb_columns
[0] = GET_COL_NAME
;
10134 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
10135 cb
.cb_columns
[2] = GET_COL_VALUE
;
10136 cb
.cb_columns
[3] = GET_COL_SOURCE
;
10140 (void) fprintf(stderr
,
10141 gettext("invalid column name "
10148 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10158 (void) fprintf(stderr
, gettext("missing property "
10163 /* Properties list is needed later by zprop_get_list() */
10170 /* No args, so just print the defaults. */
10171 } else if (are_all_pools(argc
, argv
)) {
10172 /* All the args are pool names */
10173 } else if (are_all_pools(1, argv
)) {
10174 /* The first arg is a pool name */
10175 if ((argc
== 2 && strcmp(argv
[1], "all-vdevs") == 0) ||
10176 are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10178 /* ... and the rest are vdev names */
10179 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10180 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
10181 cb
.cb_type
= ZFS_TYPE_VDEV
;
10182 argc
= 1; /* One pool to process */
10184 fprintf(stderr
, gettext("Expected a list of vdevs in"
10185 " \"%s\", but got:\n"), argv
[0]);
10186 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
10187 argv
[0], &cb
.cb_vdevs
);
10188 fprintf(stderr
, "\n");
10194 * The first arg isn't a pool name,
10196 fprintf(stderr
, gettext("missing pool name.\n"));
10197 fprintf(stderr
, "\n");
10202 if (zprop_get_list(g_zfs
, propstr
, &cb
.cb_proplist
,
10203 cb
.cb_type
) != 0) {
10204 /* Use correct list of valid properties (pool or vdev) */
10205 current_prop_type
= cb
.cb_type
;
10209 if (cb
.cb_proplist
!= NULL
) {
10210 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
10211 fake_name
.pl_width
= strlen(gettext("NAME"));
10212 fake_name
.pl_next
= cb
.cb_proplist
;
10213 cb
.cb_proplist
= &fake_name
;
10216 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_type
,
10217 cb
.cb_literal
, get_callback
, &cb
);
10219 if (cb
.cb_proplist
== &fake_name
)
10220 zprop_free_list(fake_name
.pl_next
);
10222 zprop_free_list(cb
.cb_proplist
);
10227 typedef struct set_cbdata
{
10230 zfs_type_t cb_type
;
10231 vdev_cbdata_t cb_vdevs
;
10232 boolean_t cb_any_successful
;
10236 set_pool_callback(zpool_handle_t
*zhp
, set_cbdata_t
*cb
)
10240 /* Check if we have out-of-bounds features */
10241 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
10242 boolean_t features
[SPA_FEATURES
];
10243 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
10244 ZPOOL_COMPATIBILITY_OK
)
10247 nvlist_t
*enabled
= zpool_get_features(zhp
);
10249 for (i
= 0; i
< SPA_FEATURES
; i
++) {
10250 const char *fguid
= spa_feature_table
[i
].fi_guid
;
10251 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
10254 if (i
< SPA_FEATURES
)
10255 (void) fprintf(stderr
, gettext("Warning: one or "
10256 "more features already enabled on pool '%s'\n"
10257 "are not present in this compatibility set.\n"),
10258 zpool_get_name(zhp
));
10261 /* if we're setting a feature, check it's in compatibility set */
10262 if (zpool_prop_feature(cb
->cb_propname
) &&
10263 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
10264 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
10267 if (zfeature_lookup_name(fname
, &f
) == 0) {
10268 char compat
[ZFS_MAXPROPLEN
];
10269 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
10270 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
10273 boolean_t features
[SPA_FEATURES
];
10274 if (zpool_do_load_compat(compat
, features
) !=
10275 ZPOOL_COMPATIBILITY_OK
) {
10276 (void) fprintf(stderr
, gettext("Error: "
10277 "cannot enable feature '%s' on pool '%s'\n"
10278 "because the pool's 'compatibility' "
10279 "property cannot be parsed.\n"),
10280 fname
, zpool_get_name(zhp
));
10284 if (!features
[f
]) {
10285 (void) fprintf(stderr
, gettext("Error: "
10286 "cannot enable feature '%s' on pool '%s'\n"
10287 "as it is not specified in this pool's "
10288 "current compatibility set.\n"
10289 "Consider setting 'compatibility' to a "
10290 "less restrictive set, or to 'off'.\n"),
10291 fname
, zpool_get_name(zhp
));
10297 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
10303 set_callback(zpool_handle_t
*zhp
, void *data
)
10306 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
10308 if (cb
->cb_type
== ZFS_TYPE_VDEV
) {
10309 error
= zpool_set_vdev_prop(zhp
, *cb
->cb_vdevs
.cb_names
,
10310 cb
->cb_propname
, cb
->cb_value
);
10312 assert(cb
->cb_type
== ZFS_TYPE_POOL
);
10313 error
= set_pool_callback(zhp
, cb
);
10316 cb
->cb_any_successful
= !error
;
10321 zpool_do_set(int argc
, char **argv
)
10323 set_cbdata_t cb
= { 0 };
10326 current_prop_type
= ZFS_TYPE_POOL
;
10327 if (argc
> 1 && argv
[1][0] == '-') {
10328 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10334 (void) fprintf(stderr
, gettext("missing property=value "
10340 (void) fprintf(stderr
, gettext("missing pool name\n"));
10345 (void) fprintf(stderr
, gettext("too many pool names\n"));
10349 cb
.cb_propname
= argv
[1];
10350 cb
.cb_type
= ZFS_TYPE_POOL
;
10351 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10352 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
10353 if (cb
.cb_value
== NULL
) {
10354 (void) fprintf(stderr
, gettext("missing value in "
10355 "property=value argument\n"));
10359 *(cb
.cb_value
) = '\0';
10364 if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
10365 /* Argument is a vdev */
10366 cb
.cb_vdevs
.cb_names
= argv
;
10367 cb
.cb_vdevs
.cb_names_count
= 1;
10368 cb
.cb_type
= ZFS_TYPE_VDEV
;
10369 argc
= 0; /* No pools to process */
10370 } else if (are_all_pools(1, argv
)) {
10371 /* The first arg is a pool name */
10372 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10374 /* 2nd argument is a vdev */
10375 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10376 cb
.cb_vdevs
.cb_names_count
= 1;
10377 cb
.cb_type
= ZFS_TYPE_VDEV
;
10378 argc
= 1; /* One pool to process */
10379 } else if (argc
> 1) {
10380 (void) fprintf(stderr
,
10381 gettext("too many pool names\n"));
10386 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
10387 B_FALSE
, set_callback
, &cb
);
10392 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10394 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
10396 uint64_t bytes_remaining
;
10398 uint_t c
, children
;
10401 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
10402 activity
== ZPOOL_WAIT_TRIM
);
10404 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
10405 (uint64_t **)&vs
, &c
) == 0);
10407 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
10408 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
10409 bytes_remaining
= vs
->vs_initialize_bytes_est
-
10410 vs
->vs_initialize_bytes_done
;
10411 else if (activity
== ZPOOL_WAIT_TRIM
&&
10412 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
10413 bytes_remaining
= vs
->vs_trim_bytes_est
-
10414 vs
->vs_trim_bytes_done
;
10416 bytes_remaining
= 0;
10418 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10419 &child
, &children
) != 0)
10422 for (c
= 0; c
< children
; c
++)
10423 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
10425 return (bytes_remaining
);
10428 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10430 vdev_activity_top_remaining(nvlist_t
*nv
)
10432 uint64_t bytes_remaining
= 0;
10437 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10438 &child
, &children
) != 0)
10441 for (uint_t c
= 0; c
< children
; c
++) {
10442 vdev_rebuild_stat_t
*vrs
;
10445 error
= nvlist_lookup_uint64_array(child
[c
],
10446 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
10448 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
10449 bytes_remaining
+= (vrs
->vrs_bytes_est
-
10450 vrs
->vrs_bytes_rebuilt
);
10455 return (bytes_remaining
);
10458 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10460 vdev_any_spare_replacing(nvlist_t
*nv
)
10463 uint_t c
, children
;
10466 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
10468 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
10469 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
10470 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
10474 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10475 &child
, &children
) != 0)
10478 for (c
= 0; c
< children
; c
++) {
10479 if (vdev_any_spare_replacing(child
[c
]))
10486 typedef struct wait_data
{
10488 boolean_t wd_scripted
;
10489 boolean_t wd_exact
;
10490 boolean_t wd_headers_once
;
10491 boolean_t wd_should_exit
;
10492 /* Which activities to wait for */
10493 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10495 pthread_cond_t wd_cv
;
10496 pthread_mutex_t wd_mutex
;
10500 * Print to stdout a single line, containing one column for each activity that
10501 * we are waiting for specifying how many bytes of work are left for that
10505 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10507 nvlist_t
*config
, *nvroot
;
10510 pool_checkpoint_stat_t
*pcs
= NULL
;
10511 pool_scan_stat_t
*pss
= NULL
;
10512 pool_removal_stat_t
*prs
= NULL
;
10513 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
10514 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10515 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10517 /* Calculate the width of each column */
10518 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10520 * Make sure we have enough space in the col for pretty-printed
10521 * numbers and for the column header, and then leave a couple
10522 * spaces between cols for readability.
10524 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10527 /* Print header if appropriate */
10528 int term_height
= terminal_height();
10529 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10530 row
% (term_height
-1) == 0);
10531 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10532 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10533 if (wd
->wd_enabled
[i
])
10534 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10536 (void) printf("\n");
10539 /* Bytes of work remaining in each activity */
10540 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10542 bytes_rem
[ZPOOL_WAIT_FREE
] =
10543 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10545 config
= zpool_get_config(zhp
, NULL
);
10546 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10548 (void) nvlist_lookup_uint64_array(nvroot
,
10549 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10550 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10551 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10553 (void) nvlist_lookup_uint64_array(nvroot
,
10554 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10555 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10556 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10559 (void) nvlist_lookup_uint64_array(nvroot
,
10560 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10561 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10562 pss
->pss_pass_scrub_pause
== 0) {
10563 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10564 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10565 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10567 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10568 } else if (check_rebuilding(nvroot
, NULL
)) {
10569 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10570 vdev_activity_top_remaining(nvroot
);
10573 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10574 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10575 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10576 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10579 * A replace finishes after resilvering finishes, so the amount of work
10580 * left for a replace is the same as for resilvering.
10582 * It isn't quite correct to say that if we have any 'spare' or
10583 * 'replacing' vdevs and a resilver is happening, then a replace is in
10584 * progress, like we do here. When a hot spare is used, the faulted vdev
10585 * is not removed after the hot spare is resilvered, so parent 'spare'
10586 * vdev is not removed either. So we could have a 'spare' vdev, but be
10587 * resilvering for a different reason. However, we use it as a heuristic
10588 * because we don't have access to the DTLs, which could tell us whether
10589 * or not we have really finished resilvering a hot spare.
10591 if (vdev_any_spare_replacing(nvroot
))
10592 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10594 if (timestamp_fmt
!= NODATE
)
10595 print_timestamp(timestamp_fmt
);
10597 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10599 if (!wd
->wd_enabled
[i
])
10603 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10606 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10608 if (wd
->wd_scripted
)
10609 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10611 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10613 (void) printf("\n");
10614 (void) fflush(stdout
);
10618 wait_status_thread(void *arg
)
10620 wait_data_t
*wd
= (wait_data_t
*)arg
;
10621 zpool_handle_t
*zhp
;
10623 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10624 return (void *)(1);
10626 for (int row
= 0; ; row
++) {
10628 struct timespec timeout
;
10630 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10632 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10633 zpool_props_refresh(zhp
) != 0) {
10635 return (void *)(uintptr_t)(missing
? 0 : 1);
10638 print_wait_status_row(wd
, zhp
, row
);
10640 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10641 long nanos
= timeout
.tv_nsec
+
10642 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10643 if (nanos
>= NANOSEC
) {
10645 timeout
.tv_nsec
= nanos
- NANOSEC
;
10647 timeout
.tv_nsec
= nanos
;
10649 pthread_mutex_lock(&wd
->wd_mutex
);
10650 if (!wd
->wd_should_exit
)
10651 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10653 pthread_mutex_unlock(&wd
->wd_mutex
);
10655 break; /* signaled by main thread */
10656 } else if (ret
!= ETIMEDOUT
) {
10657 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10658 "failed: %s\n"), strerror(ret
));
10660 return (void *)(uintptr_t)(1);
10665 return (void *)(0);
10669 zpool_do_wait(int argc
, char **argv
)
10671 boolean_t verbose
= B_FALSE
;
10675 unsigned long count
;
10676 pthread_t status_thr
;
10678 zpool_handle_t
*zhp
;
10681 wd
.wd_scripted
= B_FALSE
;
10682 wd
.wd_exact
= B_FALSE
;
10683 wd
.wd_headers_once
= B_FALSE
;
10684 wd
.wd_should_exit
= B_FALSE
;
10686 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10687 pthread_cond_init(&wd
.wd_cv
, NULL
);
10689 /* By default, wait for all types of activity. */
10690 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10691 wd
.wd_enabled
[i
] = B_TRUE
;
10693 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10696 wd
.wd_scripted
= B_TRUE
;
10699 wd
.wd_headers_once
= B_TRUE
;
10702 wd
.wd_exact
= B_TRUE
;
10705 get_timestamp_arg(*optarg
);
10709 static char *col_subopts
[] = { "discard", "free",
10710 "initialize", "replace", "remove", "resilver",
10711 "scrub", "trim", NULL
};
10713 /* Reset activities array */
10714 bzero(&wd
.wd_enabled
, sizeof (wd
.wd_enabled
));
10715 while (*optarg
!= '\0') {
10716 int activity
= getsubopt(&optarg
, col_subopts
,
10719 if (activity
< 0) {
10720 (void) fprintf(stderr
,
10721 gettext("invalid activity '%s'\n"),
10726 wd
.wd_enabled
[activity
] = B_TRUE
;
10731 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10740 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10742 /* This subcmd only accepts an interval, not a count */
10743 (void) fprintf(stderr
, gettext("too many arguments\n"));
10747 if (wd
.wd_interval
!= 0)
10751 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10755 (void) fprintf(stderr
, gettext("too many arguments\n"));
10759 wd
.wd_poolname
= argv
[0];
10761 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10766 * We use a separate thread for printing status updates because
10767 * the main thread will call lzc_wait(), which blocks as long
10768 * as an activity is in progress, which can be a long time.
10770 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10772 (void) fprintf(stderr
, gettext("failed to create status"
10773 "thread: %s\n"), strerror(errno
));
10780 * Loop over all activities that we are supposed to wait for until none
10781 * of them are in progress. Note that this means we can end up waiting
10782 * for more activities to complete than just those that were in progress
10783 * when we began waiting; if an activity we are interested in begins
10784 * while we are waiting for another activity, we will wait for both to
10785 * complete before exiting.
10788 boolean_t missing
= B_FALSE
;
10789 boolean_t any_waited
= B_FALSE
;
10791 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10794 if (!wd
.wd_enabled
[i
])
10797 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10798 if (error
!= 0 || missing
)
10801 any_waited
= (any_waited
|| waited
);
10804 if (error
!= 0 || missing
|| !any_waited
)
10812 pthread_mutex_lock(&wd
.wd_mutex
);
10813 wd
.wd_should_exit
= B_TRUE
;
10814 pthread_cond_signal(&wd
.wd_cv
);
10815 pthread_mutex_unlock(&wd
.wd_mutex
);
10816 (void) pthread_join(status_thr
, (void *)&status
);
10821 pthread_mutex_destroy(&wd
.wd_mutex
);
10822 pthread_cond_destroy(&wd
.wd_cv
);
10827 find_command_idx(char *command
, int *idx
)
10831 for (i
= 0; i
< NCOMMAND
; i
++) {
10832 if (command_table
[i
].name
== NULL
)
10835 if (strcmp(command
, command_table
[i
].name
) == 0) {
10844 * Display version message
10847 zpool_do_version(int argc
, char **argv
)
10849 (void) argc
, (void) argv
;
10851 if (zfs_version_print() == -1)
10858 * Do zpool_load_compat() and print error message on failure
10860 static zpool_compat_status_t
10861 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
10865 zpool_compat_status_t ret
;
10867 ret
= zpool_load_compat(compat
, list
, report
, 1024);
10870 case ZPOOL_COMPATIBILITY_OK
:
10873 case ZPOOL_COMPATIBILITY_NOFILES
:
10874 case ZPOOL_COMPATIBILITY_BADFILE
:
10875 case ZPOOL_COMPATIBILITY_BADTOKEN
:
10876 (void) fprintf(stderr
, "Error: %s\n", report
);
10879 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
10880 (void) fprintf(stderr
, "Warning: %s\n", report
);
10881 ret
= ZPOOL_COMPATIBILITY_OK
;
10888 main(int argc
, char **argv
)
10895 (void) setlocale(LC_ALL
, "");
10896 (void) setlocale(LC_NUMERIC
, "C");
10897 (void) textdomain(TEXT_DOMAIN
);
10903 * Make sure the user has specified some command.
10906 (void) fprintf(stderr
, gettext("missing command\n"));
10913 * Special case '-?'
10915 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10919 * Special case '-V|--version'
10921 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10922 return (zpool_do_version(argc
, argv
));
10924 if ((g_zfs
= libzfs_init()) == NULL
) {
10925 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10929 libzfs_print_on_error(g_zfs
, B_TRUE
);
10931 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10934 * Many commands modify input strings for string parsing reasons.
10935 * We create a copy to protect the original argv.
10937 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10938 for (i
= 0; i
< argc
; i
++)
10939 newargv
[i
] = strdup(argv
[i
]);
10940 newargv
[argc
] = NULL
;
10943 * Run the appropriate command.
10945 if (find_command_idx(cmdname
, &i
) == 0) {
10946 current_command
= &command_table
[i
];
10947 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10948 } else if (strchr(cmdname
, '=')) {
10949 verify(find_command_idx("set", &i
) == 0);
10950 current_command
= &command_table
[i
];
10951 ret
= command_table
[i
].func(argc
, newargv
);
10952 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10954 * 'freeze' is a vile debugging abomination, so we treat
10957 zfs_cmd_t zc
= {"\0"};
10959 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10960 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10962 (void) fprintf(stderr
,
10963 gettext("failed to freeze pool: %d\n"), errno
);
10969 (void) fprintf(stderr
, gettext("unrecognized "
10970 "command '%s'\n"), cmdname
);
10975 for (i
= 0; i
< argc
; i
++)
10979 if (ret
== 0 && log_history
)
10980 (void) zpool_log_history(g_zfs
, history_str
);
10982 libzfs_fini(g_zfs
);
10985 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10986 * for the purposes of running ::findleaks.
10988 if (getenv("ZFS_ABORT") != NULL
) {
10989 (void) printf("dumping core by request\n");