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
59 #include <sys/fs/zfs.h>
61 #include <sys/systeminfo.h>
62 #include <sys/fm/fs/zfs.h>
63 #include <sys/fm/util.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/mount.h>
67 #include <sys/sysmacros.h>
74 #include "zpool_util.h"
75 #include "zfs_comutil.h"
76 #include "zfeature_common.h"
78 #include "statcommon.h"
80 libzfs_handle_t
*g_zfs
;
82 static int zpool_do_create(int, char **);
83 static int zpool_do_destroy(int, char **);
85 static int zpool_do_add(int, char **);
86 static int zpool_do_remove(int, char **);
87 static int zpool_do_labelclear(int, char **);
89 static int zpool_do_checkpoint(int, char **);
91 static int zpool_do_list(int, char **);
92 static int zpool_do_iostat(int, char **);
93 static int zpool_do_status(int, char **);
95 static int zpool_do_online(int, char **);
96 static int zpool_do_offline(int, char **);
97 static int zpool_do_clear(int, char **);
98 static int zpool_do_reopen(int, char **);
100 static int zpool_do_reguid(int, char **);
102 static int zpool_do_attach(int, char **);
103 static int zpool_do_detach(int, char **);
104 static int zpool_do_replace(int, char **);
105 static int zpool_do_split(int, char **);
107 static int zpool_do_initialize(int, char **);
108 static int zpool_do_scrub(int, char **);
109 static int zpool_do_resilver(int, char **);
110 static int zpool_do_trim(int, char **);
112 static int zpool_do_import(int, char **);
113 static int zpool_do_export(int, char **);
115 static int zpool_do_upgrade(int, char **);
117 static int zpool_do_history(int, char **);
118 static int zpool_do_events(int, char **);
120 static int zpool_do_get(int, char **);
121 static int zpool_do_set(int, char **);
123 static int zpool_do_sync(int, char **);
125 static int zpool_do_version(int, char **);
127 static int zpool_do_wait(int, char **);
129 static zpool_compat_status_t
zpool_do_load_compat(
130 const char *, boolean_t
*);
133 * These libumem hooks provide a reasonable set of defaults for the allocator's
134 * debugging facilities.
139 _umem_debug_init(void)
141 return ("default,verbose"); /* $UMEM_DEBUG setting */
145 _umem_logging_init(void)
147 return ("fail,contents"); /* $UMEM_LOGGING setting */
188 * Flags for stats to display with "zpool iostats"
196 IOS_COUNT
, /* always last element */
199 /* iostat_type entries as bitmasks */
200 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
201 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
202 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
203 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
204 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
206 /* Mask of all the histo bits */
207 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
210 * Lookup table for iostat flags to nvlist names. Basically a list
211 * of all the nvlists a flag requires. Also specifies the order in
212 * which data gets printed in zpool iostat.
214 static const char *vsx_type_to_nvlist
[IOS_COUNT
][15] = {
216 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
217 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
218 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
221 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
222 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
225 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
226 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
229 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
230 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
231 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
232 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
233 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
234 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
237 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
238 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
239 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
240 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
241 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
242 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
243 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
246 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
247 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
248 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
249 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
250 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
251 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
252 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
253 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
254 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
255 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
256 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
257 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
258 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO
,
259 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO
,
265 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
266 * Right now, only one histo bit is ever set at one time, so we can
267 * just do a highbit64(a)
269 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
271 typedef struct zpool_command
{
273 int (*func
)(int, char **);
278 * Master command table. Each ZFS command has a name, associated function, and
279 * usage message. The usage messages need to be internationalized, so we have
280 * to have a function to return the usage message based on a command index.
282 * These commands are organized according to how they are displayed in the usage
283 * message. An empty command (one with a NULL name) indicates an empty line in
284 * the generic usage message.
286 static zpool_command_t command_table
[] = {
287 { "version", zpool_do_version
, HELP_VERSION
},
289 { "create", zpool_do_create
, HELP_CREATE
},
290 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
292 { "add", zpool_do_add
, HELP_ADD
},
293 { "remove", zpool_do_remove
, HELP_REMOVE
},
295 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
297 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
299 { "list", zpool_do_list
, HELP_LIST
},
300 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
301 { "status", zpool_do_status
, HELP_STATUS
},
303 { "online", zpool_do_online
, HELP_ONLINE
},
304 { "offline", zpool_do_offline
, HELP_OFFLINE
},
305 { "clear", zpool_do_clear
, HELP_CLEAR
},
306 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
308 { "attach", zpool_do_attach
, HELP_ATTACH
},
309 { "detach", zpool_do_detach
, HELP_DETACH
},
310 { "replace", zpool_do_replace
, HELP_REPLACE
},
311 { "split", zpool_do_split
, HELP_SPLIT
},
313 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
314 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
315 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
316 { "trim", zpool_do_trim
, HELP_TRIM
},
318 { "import", zpool_do_import
, HELP_IMPORT
},
319 { "export", zpool_do_export
, HELP_EXPORT
},
320 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
321 { "reguid", zpool_do_reguid
, HELP_REGUID
},
323 { "history", zpool_do_history
, HELP_HISTORY
},
324 { "events", zpool_do_events
, HELP_EVENTS
},
326 { "get", zpool_do_get
, HELP_GET
},
327 { "set", zpool_do_set
, HELP_SET
},
328 { "sync", zpool_do_sync
, HELP_SYNC
},
330 { "wait", zpool_do_wait
, HELP_WAIT
},
333 #define NCOMMAND (ARRAY_SIZE(command_table))
335 #define VDEV_ALLOC_CLASS_LOGS "logs"
337 static zpool_command_t
*current_command
;
338 static zfs_type_t current_prop_type
= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
);
339 static char history_str
[HIS_MAX_RECORD_LEN
];
340 static boolean_t log_history
= B_TRUE
;
341 static uint_t timestamp_fmt
= NODATE
;
344 get_usage(zpool_help_t idx
)
348 return (gettext("\tadd [-fgLnP] [-o property=value] "
349 "<pool> <vdev> ...\n"));
351 return (gettext("\tattach [-fsw] [-o property=value] "
352 "<pool> <device> <new-device>\n"));
354 return (gettext("\tclear [-nF] <pool> [device]\n"));
356 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
357 "\t [-O file-system-property=value] ... \n"
358 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
359 case HELP_CHECKPOINT
:
360 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
362 return (gettext("\tdestroy [-f] <pool>\n"));
364 return (gettext("\tdetach <pool> <device>\n"));
366 return (gettext("\texport [-af] <pool> ...\n"));
368 return (gettext("\thistory [-il] [<pool>] ...\n"));
370 return (gettext("\timport [-d dir] [-D]\n"
371 "\timport [-o mntopts] [-o property=value] ... \n"
372 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
373 "[-R root] [-F [-n]] -a\n"
374 "\timport [-o mntopts] [-o property=value] ... \n"
375 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
376 "[-R root] [-F [-n]]\n"
377 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
379 return (gettext("\tiostat [[[-c [script1,script2,...]"
380 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
381 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
382 " [[-n] interval [count]]\n"));
383 case HELP_LABELCLEAR
:
384 return (gettext("\tlabelclear [-f] <vdev>\n"));
386 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
387 "[-T d|u] [pool] ... \n"
388 "\t [interval [count]]\n"));
390 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
392 return (gettext("\tonline [-e] <pool> <device> ...\n"));
394 return (gettext("\treplace [-fsw] [-o property=value] "
395 "<pool> <device> [new-device]\n"));
397 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
399 return (gettext("\treopen [-n] <pool>\n"));
400 case HELP_INITIALIZE
:
401 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
402 "[<device> ...]\n"));
404 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
406 return (gettext("\tresilver <pool> ...\n"));
408 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
409 "[<device> ...]\n"));
411 return (gettext("\tstatus [-c [script1,script2,...]] "
412 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
413 "\t [interval [count]]\n"));
415 return (gettext("\tupgrade\n"
417 "\tupgrade [-V version] <-a | pool ...>\n"));
419 return (gettext("\tevents [-vHf [pool] | -c]\n"));
421 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
422 "<\"all\" | property[,...]> <pool> ...\n"));
424 return (gettext("\tset <property=value> <pool> \n"));
426 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
427 "\t [-o property=value] <pool> <newpool> "
428 "[<device> ...]\n"));
430 return (gettext("\treguid <pool>\n"));
432 return (gettext("\tsync [pool] ...\n"));
434 return (gettext("\tversion\n"));
436 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
437 "<pool> [interval]\n"));
439 __builtin_unreachable();
444 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
450 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
454 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
457 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
458 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
459 fnvlist_add_boolean(res
, path
);
465 for (i
= 0; i
< children
; i
++) {
466 zpool_collect_leaves(zhp
, child
[i
], res
);
471 * Callback routine that will print out a pool property value.
474 print_pool_prop_cb(int prop
, void *cb
)
478 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
480 if (zpool_prop_readonly(prop
))
481 (void) fprintf(fp
, " NO ");
483 (void) fprintf(fp
, " YES ");
485 if (zpool_prop_values(prop
) == NULL
)
486 (void) fprintf(fp
, "-\n");
488 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
494 * Callback routine that will print out a vdev property value.
497 print_vdev_prop_cb(int prop
, void *cb
)
501 (void) fprintf(fp
, "\t%-19s ", vdev_prop_to_name(prop
));
503 if (vdev_prop_readonly(prop
))
504 (void) fprintf(fp
, " NO ");
506 (void) fprintf(fp
, " YES ");
508 if (vdev_prop_values(prop
) == NULL
)
509 (void) fprintf(fp
, "-\n");
511 (void) fprintf(fp
, "%s\n", vdev_prop_values(prop
));
517 * Display usage message. If we're inside a command, display only the usage for
518 * that command. Otherwise, iterate over the entire command table and display
519 * a complete usage message.
521 static __attribute__((noreturn
)) void
522 usage(boolean_t requested
)
524 FILE *fp
= requested
? stdout
: stderr
;
526 if (current_command
== NULL
) {
529 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
531 gettext("where 'command' is one of the following:\n\n"));
533 for (i
= 0; i
< NCOMMAND
; i
++) {
534 if (command_table
[i
].name
== NULL
)
535 (void) fprintf(fp
, "\n");
537 (void) fprintf(fp
, "%s",
538 get_usage(command_table
[i
].usage
));
541 (void) fprintf(fp
, gettext("usage:\n"));
542 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
545 if (current_command
!= NULL
&&
546 current_prop_type
!= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
) &&
547 ((strcmp(current_command
->name
, "set") == 0) ||
548 (strcmp(current_command
->name
, "get") == 0) ||
549 (strcmp(current_command
->name
, "list") == 0))) {
552 gettext("\nthe following properties are supported:\n"));
554 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
555 "PROPERTY", "EDIT", "VALUES");
557 /* Iterate over all properties */
558 if (current_prop_type
== ZFS_TYPE_POOL
) {
559 (void) zprop_iter(print_pool_prop_cb
, fp
, B_FALSE
,
560 B_TRUE
, current_prop_type
);
562 (void) fprintf(fp
, "\t%-19s ", "feature@...");
563 (void) fprintf(fp
, "YES "
564 "disabled | enabled | active\n");
566 (void) fprintf(fp
, gettext("\nThe feature@ properties "
567 "must be appended with a feature name.\n"
568 "See zpool-features(7).\n"));
569 } else if (current_prop_type
== ZFS_TYPE_VDEV
) {
570 (void) zprop_iter(print_vdev_prop_cb
, fp
, B_FALSE
,
571 B_TRUE
, current_prop_type
);
576 * See comments at end of main().
578 if (getenv("ZFS_ABORT") != NULL
) {
579 (void) printf("dumping core by request\n");
583 exit(requested
? 0 : 2);
587 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
588 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
591 * -c Cancel. Ends active initializing.
592 * -s Suspend. Initializing can then be restarted with no flags.
593 * -w Wait. Blocks until initializing has completed.
596 zpool_do_initialize(int argc
, char **argv
)
603 boolean_t wait
= B_FALSE
;
605 struct option long_options
[] = {
606 {"cancel", no_argument
, NULL
, 'c'},
607 {"suspend", no_argument
, NULL
, 's'},
608 {"wait", no_argument
, NULL
, 'w'},
612 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
613 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
616 if (cmd_type
!= POOL_INITIALIZE_START
&&
617 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
618 (void) fprintf(stderr
, gettext("-c cannot be "
619 "combined with other options\n"));
622 cmd_type
= POOL_INITIALIZE_CANCEL
;
625 if (cmd_type
!= POOL_INITIALIZE_START
&&
626 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
627 (void) fprintf(stderr
, gettext("-s cannot be "
628 "combined with other options\n"));
631 cmd_type
= POOL_INITIALIZE_SUSPEND
;
638 (void) fprintf(stderr
,
639 gettext("invalid option '%c'\n"), optopt
);
641 (void) fprintf(stderr
,
642 gettext("invalid option '%s'\n"),
653 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
658 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
659 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
665 zhp
= zpool_open(g_zfs
, poolname
);
669 vdevs
= fnvlist_alloc();
671 /* no individual leaf vdevs specified, so add them all */
672 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
673 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
674 ZPOOL_CONFIG_VDEV_TREE
);
675 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
677 for (int i
= 1; i
< argc
; i
++) {
678 fnvlist_add_boolean(vdevs
, argv
[i
]);
683 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
685 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
694 * print a pool vdev config for dry runs
697 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
698 const char *match
, int name_flags
)
703 boolean_t printed
= B_FALSE
;
705 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
706 &child
, &children
) != 0) {
708 (void) printf("\t%*s%s\n", indent
, "", name
);
712 for (c
= 0; c
< children
; c
++) {
713 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
716 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
719 if (is_hole
== B_TRUE
) {
723 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
726 class = VDEV_ALLOC_BIAS_LOG
;
727 (void) nvlist_lookup_string(child
[c
],
728 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
729 if (strcmp(match
, class) != 0)
732 if (!printed
&& name
!= NULL
) {
733 (void) printf("\t%*s%s\n", indent
, "", name
);
736 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
737 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
744 * Print the list of l2cache devices for dry runs.
747 print_cache_list(nvlist_t
*nv
, int indent
)
752 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
753 &child
, &children
) == 0 && children
> 0) {
754 (void) printf("\t%*s%s\n", indent
, "", "cache");
758 for (c
= 0; c
< children
; c
++) {
761 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
762 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
768 * Print the list of spares for dry runs.
771 print_spare_list(nvlist_t
*nv
, int indent
)
776 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
777 &child
, &children
) == 0 && children
> 0) {
778 (void) printf("\t%*s%s\n", indent
, "", "spares");
782 for (c
= 0; c
< children
; c
++) {
785 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
786 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
792 prop_list_contains_feature(nvlist_t
*proplist
)
795 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
796 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
797 if (zpool_prop_feature(nvpair_name(nvp
)))
804 * Add a property pair (name, string-value) into a property nvlist.
807 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
810 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
815 if (*props
== NULL
&&
816 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
817 (void) fprintf(stderr
,
818 gettext("internal error: out of memory\n"));
825 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
827 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
829 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
830 (!zpool_prop_feature(propname
) &&
831 !zpool_prop_vdev(propname
))) {
832 (void) fprintf(stderr
, gettext("property '%s' is "
833 "not a valid pool or vdev property\n"), propname
);
838 * feature@ properties and version should not be specified
841 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
842 nvlist_exists(proplist
, vname
)) ||
843 (prop
== ZPOOL_PROP_VERSION
&&
844 prop_list_contains_feature(proplist
))) {
845 (void) fprintf(stderr
, gettext("'feature@' and "
846 "'version' properties cannot be specified "
852 * if version is specified, only "legacy" compatibility
855 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
856 strcmp(propval
, ZPOOL_COMPAT_LEGACY
) != 0 &&
857 nvlist_exists(proplist
, vname
)) ||
858 (prop
== ZPOOL_PROP_VERSION
&&
859 nvlist_exists(proplist
, cname
) &&
860 strcmp(fnvlist_lookup_string(proplist
, cname
),
861 ZPOOL_COMPAT_LEGACY
) != 0)) {
862 (void) fprintf(stderr
, gettext("when 'version' is "
863 "specified, the 'compatibility' feature may only "
864 "be set to '" ZPOOL_COMPAT_LEGACY
"'\n"));
868 if (zpool_prop_feature(propname
) || zpool_prop_vdev(propname
))
871 normnm
= zpool_prop_to_name(prop
);
873 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
875 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
877 normnm
= zfs_prop_to_name(fsprop
);
878 } else if (zfs_prop_user(propname
) ||
879 zfs_prop_userquota(propname
)) {
882 (void) fprintf(stderr
, gettext("property '%s' is "
883 "not a valid filesystem property\n"), propname
);
888 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
889 prop
!= ZPOOL_PROP_CACHEFILE
) {
890 (void) fprintf(stderr
, gettext("property '%s' "
891 "specified multiple times\n"), propname
);
895 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
896 (void) fprintf(stderr
, gettext("internal "
897 "error: out of memory\n"));
905 * Set a default property pair (name, string-value) in a property nvlist
908 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
)
912 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
915 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
919 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
921 * -f Force addition of devices, even if they appear in use
922 * -g Display guid for individual vdev name.
923 * -L Follow links when resolving vdev path name.
924 * -n Do not add the devices, but display the resulting layout if
925 * they were to be added.
926 * -o Set property=value.
927 * -P Display full path for vdev name.
929 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
930 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
934 zpool_do_add(int argc
, char **argv
)
936 boolean_t force
= B_FALSE
;
937 boolean_t dryrun
= B_FALSE
;
945 nvlist_t
*props
= NULL
;
949 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
955 name_flags
|= VDEV_NAME_GUID
;
958 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
964 if ((propval
= strchr(optarg
, '=')) == NULL
) {
965 (void) fprintf(stderr
, gettext("missing "
966 "'=' for -o option\n"));
972 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
973 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
977 name_flags
|= VDEV_NAME_PATH
;
980 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
989 /* get pool name and check number of arguments */
991 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
995 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1004 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1007 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
1008 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
1014 /* unless manually specified use "ashift" pool property (if set) */
1015 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
1018 char strval
[ZPOOL_MAXPROPLEN
];
1020 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
1021 if (src
!= ZPROP_SRC_DEFAULT
) {
1022 (void) sprintf(strval
, "%" PRId32
, intval
);
1023 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
1024 &props
, B_TRUE
) == 0);
1028 /* pass off to make_root_vdev for processing */
1029 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
1031 if (nvroot
== NULL
) {
1037 nvlist_t
*poolnvroot
;
1038 nvlist_t
**l2child
, **sparechild
;
1039 uint_t l2children
, sparechildren
, c
;
1041 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1043 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1046 (void) printf(gettext("would update '%s' to the following "
1047 "configuration:\n\n"), zpool_get_name(zhp
));
1049 /* print original main pool and new tree */
1050 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1051 name_flags
| VDEV_NAME_TYPE_ID
);
1052 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1054 /* print other classes: 'dedup', 'special', and 'log' */
1055 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1056 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1057 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1058 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1059 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1060 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1061 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1062 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1065 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1066 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1067 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1068 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1069 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1070 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1071 print_vdev_tree(zhp
, "special", nvroot
, 0,
1072 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1075 if (num_logs(poolnvroot
) > 0) {
1076 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1077 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1078 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1079 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1080 } else if (num_logs(nvroot
) > 0) {
1081 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1082 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1085 /* Do the same for the caches */
1086 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1087 &l2child
, &l2children
) == 0 && l2children
) {
1089 (void) printf(gettext("\tcache\n"));
1090 for (c
= 0; c
< l2children
; c
++) {
1091 vname
= zpool_vdev_name(g_zfs
, NULL
,
1092 l2child
[c
], name_flags
);
1093 (void) printf("\t %s\n", vname
);
1097 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1098 &l2child
, &l2children
) == 0 && l2children
) {
1100 (void) printf(gettext("\tcache\n"));
1101 for (c
= 0; c
< l2children
; c
++) {
1102 vname
= zpool_vdev_name(g_zfs
, NULL
,
1103 l2child
[c
], name_flags
);
1104 (void) printf("\t %s\n", vname
);
1108 /* And finally the spares */
1109 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1110 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1112 (void) printf(gettext("\tspares\n"));
1113 for (c
= 0; c
< sparechildren
; c
++) {
1114 vname
= zpool_vdev_name(g_zfs
, NULL
,
1115 sparechild
[c
], name_flags
);
1116 (void) printf("\t %s\n", vname
);
1120 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1121 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1123 (void) printf(gettext("\tspares\n"));
1124 for (c
= 0; c
< sparechildren
; c
++) {
1125 vname
= zpool_vdev_name(g_zfs
, NULL
,
1126 sparechild
[c
], name_flags
);
1127 (void) printf("\t %s\n", vname
);
1134 ret
= (zpool_add(zhp
, nvroot
) != 0);
1138 nvlist_free(nvroot
);
1145 * zpool remove [-npsw] <pool> <vdev> ...
1147 * Removes the given vdev from the pool.
1150 zpool_do_remove(int argc
, char **argv
)
1154 zpool_handle_t
*zhp
= NULL
;
1155 boolean_t stop
= B_FALSE
;
1157 boolean_t noop
= B_FALSE
;
1158 boolean_t parsable
= B_FALSE
;
1159 boolean_t wait
= B_FALSE
;
1162 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1177 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1186 /* get pool name and check number of arguments */
1188 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1194 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1198 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1204 (void) fprintf(stderr
, gettext("too many arguments\n"));
1207 if (zpool_vdev_remove_cancel(zhp
) != 0)
1210 (void) fprintf(stderr
, gettext("invalid option "
1211 "combination: -w cannot be used with -s\n"));
1216 (void) fprintf(stderr
, gettext("missing device\n"));
1220 for (i
= 1; i
< argc
; i
++) {
1224 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1230 (void) printf("%s %llu\n",
1231 argv
[i
], (unsigned long long)size
);
1234 zfs_nicenum(size
, valstr
,
1236 (void) printf("Memory that will be "
1237 "used after removing %s: %s\n",
1241 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1246 if (ret
== 0 && wait
)
1247 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1255 * Return 1 if a vdev is active (being used in a pool)
1256 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1258 * This is useful for checking if a disk in an active pool is offlined or
1262 vdev_is_active(char *vdev_path
)
1265 fd
= open(vdev_path
, O_EXCL
);
1267 return (1); /* cant open O_EXCL - disk is active */
1271 return (0); /* disk is inactive in the pool */
1275 * zpool labelclear [-f] <vdev>
1277 * -f Force clearing the label for the vdevs which are members of
1278 * the exported or foreign pools.
1280 * Verifies that the vdev is not active and zeros out the label information
1284 zpool_do_labelclear(int argc
, char **argv
)
1286 char vdev
[MAXPATHLEN
];
1289 int c
, fd
= -1, ret
= 0;
1292 boolean_t inuse
= B_FALSE
;
1293 boolean_t force
= B_FALSE
;
1296 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1302 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1313 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1317 (void) fprintf(stderr
, gettext("too many arguments\n"));
1322 * Check if we were given absolute path and use it as is.
1323 * Otherwise if the provided vdev name doesn't point to a file,
1324 * try prepending expected disk paths and partition numbers.
1326 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1327 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1330 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1331 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1332 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1336 if (error
|| (stat(vdev
, &st
) != 0)) {
1337 (void) fprintf(stderr
, gettext(
1338 "failed to find device %s, try specifying absolute "
1339 "path instead\n"), argv
[0]);
1344 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1345 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1346 vdev
, strerror(errno
));
1351 * Flush all dirty pages for the block device. This should not be
1352 * fatal when the device does not support BLKFLSBUF as would be the
1353 * case for a file vdev.
1355 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1356 (void) fprintf(stderr
, gettext("failed to invalidate "
1357 "cache for %s: %s\n"), vdev
, strerror(errno
));
1359 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1360 (void) fprintf(stderr
,
1361 gettext("failed to read label from %s\n"), vdev
);
1365 nvlist_free(config
);
1367 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1369 (void) fprintf(stderr
,
1370 gettext("failed to check state for %s\n"), vdev
);
1380 case POOL_STATE_ACTIVE
:
1381 case POOL_STATE_SPARE
:
1382 case POOL_STATE_L2CACHE
:
1384 * We allow the user to call 'zpool offline -f'
1385 * on an offlined disk in an active pool. We can check if
1386 * the disk is online by calling vdev_is_active().
1388 if (force
&& !vdev_is_active(vdev
))
1391 (void) fprintf(stderr
, gettext(
1392 "%s is a member (%s) of pool \"%s\""),
1393 vdev
, zpool_pool_state_to_name(state
), name
);
1396 (void) fprintf(stderr
, gettext(
1397 ". Offline the disk first to clear its label."));
1403 case POOL_STATE_EXPORTED
:
1406 (void) fprintf(stderr
, gettext(
1407 "use '-f' to override the following error:\n"
1408 "%s is a member of exported pool \"%s\"\n"),
1413 case POOL_STATE_POTENTIALLY_ACTIVE
:
1416 (void) fprintf(stderr
, gettext(
1417 "use '-f' to override the following error:\n"
1418 "%s is a member of potentially active pool \"%s\"\n"),
1423 case POOL_STATE_DESTROYED
:
1424 /* inuse should never be set for a destroyed pool */
1430 ret
= zpool_clear_label(fd
);
1432 (void) fprintf(stderr
,
1433 gettext("failed to clear label for %s\n"), vdev
);
1444 * zpool create [-fnd] [-o property=value] ...
1445 * [-O file-system-property=value] ...
1446 * [-R root] [-m mountpoint] <pool> <dev> ...
1448 * -f Force creation, even if devices appear in use
1449 * -n Do not create the pool, but display the resulting layout if it
1450 * were to be created.
1451 * -R Create a pool under an alternate root
1452 * -m Set default mountpoint for the root dataset. By default it's
1454 * -o Set property=value.
1455 * -o Set feature@feature=enabled|disabled.
1456 * -d Don't automatically enable all supported pool features
1457 * (individual features can be enabled with -o).
1458 * -O Set fsproperty=value in the pool's root file system
1460 * Creates the named pool according to the given vdev specification. The
1461 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1462 * Once we get the nvlist back from make_root_vdev(), we either print out the
1463 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1466 zpool_do_create(int argc
, char **argv
)
1468 boolean_t force
= B_FALSE
;
1469 boolean_t dryrun
= B_FALSE
;
1470 boolean_t enable_pool_features
= B_TRUE
;
1473 nvlist_t
*nvroot
= NULL
;
1477 char *altroot
= NULL
;
1478 char *compat
= NULL
;
1479 char *mountpoint
= NULL
;
1480 nvlist_t
*fsprops
= NULL
;
1481 nvlist_t
*props
= NULL
;
1485 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1494 enable_pool_features
= B_FALSE
;
1498 if (add_prop_list(zpool_prop_to_name(
1499 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1501 if (add_prop_list_default(zpool_prop_to_name(
1502 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1506 /* Equivalent to -O mountpoint=optarg */
1507 mountpoint
= optarg
;
1510 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1511 (void) fprintf(stderr
, gettext("missing "
1512 "'=' for -o option\n"));
1518 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1522 * If the user is creating a pool that doesn't support
1523 * feature flags, don't enable any features.
1525 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1529 ver
= strtoull(propval
, &end
, 10);
1531 ver
< SPA_VERSION_FEATURES
) {
1532 enable_pool_features
= B_FALSE
;
1535 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1537 if (zpool_name_to_prop(optarg
) ==
1538 ZPOOL_PROP_COMPATIBILITY
)
1542 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1543 (void) fprintf(stderr
, gettext("missing "
1544 "'=' for -O option\n"));
1551 * Mountpoints are checked and then added later.
1552 * Uniquely among properties, they can be specified
1553 * more than once, to avoid conflict with -m.
1555 if (0 == strcmp(optarg
,
1556 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1557 mountpoint
= propval
;
1558 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1565 * Sanity check temporary pool name.
1567 if (strchr(optarg
, '/') != NULL
) {
1568 (void) fprintf(stderr
, gettext("cannot create "
1569 "'%s': invalid character '/' in temporary "
1571 (void) fprintf(stderr
, gettext("use 'zfs "
1572 "create' to create a dataset\n"));
1576 if (add_prop_list(zpool_prop_to_name(
1577 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1579 if (add_prop_list_default(zpool_prop_to_name(
1580 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1585 (void) fprintf(stderr
, gettext("missing argument for "
1586 "'%c' option\n"), optopt
);
1589 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1598 /* get pool name and check number of arguments */
1600 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1604 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1611 * As a special case, check for use of '/' in the name, and direct the
1612 * user to use 'zfs create' instead.
1614 if (strchr(poolname
, '/') != NULL
) {
1615 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1616 "character '/' in pool name\n"), poolname
);
1617 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1618 "create a dataset\n"));
1622 /* pass off to make_root_vdev for bulk processing */
1623 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1624 argc
- 1, argv
+ 1);
1628 /* make_root_vdev() allows 0 toplevel children if there are spares */
1629 if (!zfs_allocatable_devs(nvroot
)) {
1630 (void) fprintf(stderr
, gettext("invalid vdev "
1631 "specification: at least one toplevel vdev must be "
1636 if (altroot
!= NULL
&& altroot
[0] != '/') {
1637 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1638 "must be an absolute path\n"), altroot
);
1643 * Check the validity of the mountpoint and direct the user to use the
1644 * '-m' mountpoint option if it looks like its in use.
1646 if (mountpoint
== NULL
||
1647 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1648 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1649 char buf
[MAXPATHLEN
];
1652 if (mountpoint
&& mountpoint
[0] != '/') {
1653 (void) fprintf(stderr
, gettext("invalid mountpoint "
1654 "'%s': must be an absolute path, 'legacy', or "
1655 "'none'\n"), mountpoint
);
1659 if (mountpoint
== NULL
) {
1660 if (altroot
!= NULL
)
1661 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1664 (void) snprintf(buf
, sizeof (buf
), "/%s",
1667 if (altroot
!= NULL
)
1668 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1669 altroot
, mountpoint
);
1671 (void) snprintf(buf
, sizeof (buf
), "%s",
1675 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1676 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1677 "%s\n"), buf
, strerror(errno
));
1678 (void) fprintf(stderr
, gettext("use '-m' "
1679 "option to provide a different default\n"));
1684 while (count
< 3 && readdir(dirp
) != NULL
)
1686 (void) closedir(dirp
);
1689 (void) fprintf(stderr
, gettext("mountpoint "
1690 "'%s' exists and is not empty\n"), buf
);
1691 (void) fprintf(stderr
, gettext("use '-m' "
1692 "option to provide a "
1693 "different default\n"));
1700 * Now that the mountpoint's validity has been checked, ensure that
1701 * the property is set appropriately prior to creating the pool.
1703 if (mountpoint
!= NULL
) {
1704 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1705 mountpoint
, &fsprops
, B_FALSE
);
1713 * For a dry run invocation, print out a basic message and run
1714 * through all the vdevs in the list and print out in an
1715 * appropriate hierarchy.
1717 (void) printf(gettext("would create '%s' with the "
1718 "following layout:\n\n"), poolname
);
1720 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1721 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1722 VDEV_ALLOC_BIAS_DEDUP
, 0);
1723 print_vdev_tree(NULL
, "special", nvroot
, 0,
1724 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1725 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1726 VDEV_ALLOC_BIAS_LOG
, 0);
1727 print_cache_list(nvroot
, 0);
1728 print_spare_list(nvroot
, 0);
1733 * Load in feature set.
1734 * Note: if compatibility property not given, we'll have
1735 * NULL, which means 'all features'.
1737 boolean_t requested_features
[SPA_FEATURES
];
1738 if (zpool_do_load_compat(compat
, requested_features
) !=
1739 ZPOOL_COMPATIBILITY_OK
)
1743 * props contains list of features to enable.
1745 * - remove it if feature@name=disabled
1746 * - leave it there if feature@name=enabled
1748 * - enable_pool_features (ie: no '-d' or '-o version')
1749 * - it's supported by the kernel module
1750 * - it's in the requested feature set
1751 * - warn if it's enabled but not in compat
1753 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1754 char propname
[MAXPATHLEN
];
1756 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1758 (void) snprintf(propname
, sizeof (propname
),
1759 "feature@%s", feat
->fi_uname
);
1761 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1763 ZFS_FEATURE_DISABLED
) == 0) {
1764 (void) nvlist_remove_all(props
,
1766 } else if (strcmp(propval
,
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"),
1776 enable_pool_features
&&
1777 feat
->fi_zfs_mod_supported
&&
1778 requested_features
[i
]) {
1779 ret
= add_prop_list(propname
,
1780 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1787 if (zpool_create(g_zfs
, poolname
,
1788 nvroot
, props
, fsprops
) == 0) {
1789 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1790 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1792 if (zfs_mount(pool
, NULL
, 0) == 0) {
1793 ret
= zfs_shareall(pool
);
1794 zfs_commit_all_shares();
1798 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1799 (void) fprintf(stderr
, gettext("pool name may have "
1805 nvlist_free(nvroot
);
1806 nvlist_free(fsprops
);
1810 nvlist_free(fsprops
);
1817 * zpool destroy <pool>
1819 * -f Forcefully unmount any datasets
1821 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1824 zpool_do_destroy(int argc
, char **argv
)
1826 boolean_t force
= B_FALSE
;
1829 zpool_handle_t
*zhp
;
1833 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1839 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1848 /* check arguments */
1850 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1854 (void) fprintf(stderr
, gettext("too many arguments\n"));
1860 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1862 * As a special case, check for use of '/' in the name, and
1863 * direct the user to use 'zfs destroy' instead.
1865 if (strchr(pool
, '/') != NULL
)
1866 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1867 "destroy a dataset\n"));
1871 if (zpool_disable_datasets(zhp
, force
) != 0) {
1872 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1873 "could not unmount datasets\n"), zpool_get_name(zhp
));
1878 /* The history must be logged as part of the export */
1879 log_history
= B_FALSE
;
1881 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1888 typedef struct export_cbdata
{
1890 boolean_t hardforce
;
1897 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1899 export_cbdata_t
*cb
= data
;
1901 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1904 /* The history must be logged as part of the export */
1905 log_history
= B_FALSE
;
1907 if (cb
->hardforce
) {
1908 if (zpool_export_force(zhp
, history_str
) != 0)
1910 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1918 * zpool export [-f] <pool> ...
1920 * -a Export all pools
1921 * -f Forcefully unmount datasets
1923 * Export the given pools. By default, the command will attempt to cleanly
1924 * unmount any active datasets within the pool. If the '-f' flag is specified,
1925 * then the datasets will be forcefully unmounted.
1928 zpool_do_export(int argc
, char **argv
)
1931 boolean_t do_all
= B_FALSE
;
1932 boolean_t force
= B_FALSE
;
1933 boolean_t hardforce
= B_FALSE
;
1937 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1949 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1956 cb
.hardforce
= hardforce
;
1962 (void) fprintf(stderr
, gettext("too many arguments\n"));
1966 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1967 ZFS_TYPE_POOL
, B_FALSE
, zpool_export_one
, &cb
));
1970 /* check arguments */
1972 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1976 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
1977 B_FALSE
, zpool_export_one
, &cb
);
1983 * Given a vdev configuration, determine the maximum width needed for the device
1987 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1995 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1996 if (strlen(name
) + depth
> max
)
1997 max
= strlen(name
) + depth
;
2001 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2002 &child
, &children
) == 0) {
2003 for (c
= 0; c
< children
; c
++)
2004 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
2005 max
, name_flags
)) > max
)
2009 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2010 &child
, &children
) == 0) {
2011 for (c
= 0; c
< children
; c
++)
2012 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
2013 max
, name_flags
)) > max
)
2017 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2018 &child
, &children
) == 0) {
2019 for (c
= 0; c
< children
; c
++)
2020 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
2021 max
, name_flags
)) > max
)
2028 typedef struct spare_cbdata
{
2030 zpool_handle_t
*cb_zhp
;
2034 find_vdev(nvlist_t
*nv
, uint64_t search
)
2040 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
2044 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2045 &child
, &children
) == 0) {
2046 for (c
= 0; c
< children
; c
++)
2047 if (find_vdev(child
[c
], search
))
2055 find_spare(zpool_handle_t
*zhp
, void *data
)
2057 spare_cbdata_t
*cbp
= data
;
2058 nvlist_t
*config
, *nvroot
;
2060 config
= zpool_get_config(zhp
, NULL
);
2061 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2064 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
2073 typedef struct status_cbdata
{
2077 boolean_t cb_allpools
;
2078 boolean_t cb_verbose
;
2079 boolean_t cb_literal
;
2080 boolean_t cb_explain
;
2082 boolean_t cb_dedup_stats
;
2083 boolean_t cb_print_status
;
2084 boolean_t cb_print_slow_ios
;
2085 boolean_t cb_print_vdev_init
;
2086 boolean_t cb_print_vdev_trim
;
2087 vdev_cmd_data_list_t
*vcdl
;
2090 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2092 is_blank_str(char *str
)
2094 while (str
!= NULL
&& *str
!= '\0') {
2102 /* Print command output lines for specific vdev in a specific pool */
2104 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
2106 vdev_cmd_data_t
*data
;
2110 for (i
= 0; i
< vcdl
->count
; i
++) {
2111 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2112 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2113 /* Not the vdev we're looking for */
2117 data
= &vcdl
->data
[i
];
2118 /* Print out all the output values for this vdev */
2119 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2121 /* Does this vdev have values for this column? */
2122 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2123 if (strcmp(data
->cols
[k
],
2124 vcdl
->uniq_cols
[j
]) == 0) {
2125 /* yes it does, record the value */
2126 val
= data
->lines
[k
];
2131 * Mark empty values with dashes to make output
2134 if (val
== NULL
|| is_blank_str(val
))
2137 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2138 if (j
< vcdl
->uniq_cols_cnt
- 1)
2142 /* Print out any values that aren't in a column at the end */
2143 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2144 /* Did we have any columns? If so print a spacer. */
2145 if (vcdl
->uniq_cols_cnt
> 0)
2148 val
= data
->lines
[j
];
2149 printf("%s", val
? val
: "");
2156 * Print vdev initialization status for leaves
2159 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2162 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2163 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2164 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2165 !vs
->vs_scan_removing
) {
2168 struct tm zaction_ts
;
2170 time_t t
= vs
->vs_initialize_action_time
;
2171 int initialize_pct
= 100;
2172 if (vs
->vs_initialize_state
!=
2173 VDEV_INITIALIZE_COMPLETE
) {
2174 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2175 100 / (vs
->vs_initialize_bytes_est
+ 1));
2178 (void) localtime_r(&t
, &zaction_ts
);
2179 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2181 switch (vs
->vs_initialize_state
) {
2182 case VDEV_INITIALIZE_SUSPENDED
:
2183 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2184 gettext("suspended, started at"), tbuf
);
2186 case VDEV_INITIALIZE_ACTIVE
:
2187 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2188 gettext("started at"), tbuf
);
2190 case VDEV_INITIALIZE_COMPLETE
:
2191 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2192 gettext("completed at"), tbuf
);
2196 (void) printf(gettext(" (%d%% initialized%s)"),
2197 initialize_pct
, zbuf
);
2199 (void) printf(gettext(" (uninitialized)"));
2201 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2202 (void) printf(gettext(" (initializing)"));
2207 * Print vdev TRIM status for leaves
2210 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2213 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2214 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2215 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2216 !vs
->vs_scan_removing
) {
2219 struct tm zaction_ts
;
2221 time_t t
= vs
->vs_trim_action_time
;
2223 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2224 trim_pct
= (vs
->vs_trim_bytes_done
*
2225 100 / (vs
->vs_trim_bytes_est
+ 1));
2228 (void) localtime_r(&t
, &zaction_ts
);
2229 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2231 switch (vs
->vs_trim_state
) {
2232 case VDEV_TRIM_SUSPENDED
:
2233 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2234 gettext("suspended, started at"), tbuf
);
2236 case VDEV_TRIM_ACTIVE
:
2237 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2238 gettext("started at"), tbuf
);
2240 case VDEV_TRIM_COMPLETE
:
2241 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2242 gettext("completed at"), tbuf
);
2246 (void) printf(gettext(" (%d%% trimmed%s)"),
2248 } else if (vs
->vs_trim_notsup
) {
2249 (void) printf(gettext(" (trim unsupported)"));
2251 (void) printf(gettext(" (untrimmed)"));
2253 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2254 (void) printf(gettext(" (trimming)"));
2259 * Return the color associated with a health string. This includes returning
2260 * NULL for no color change.
2263 health_str_to_color(const char *health
)
2265 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2266 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2267 strcmp(health
, gettext("UNAVAIL")) == 0) {
2271 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2272 strcmp(health
, gettext("DEGRADED")) == 0 ||
2273 strcmp(health
, gettext("REMOVED")) == 0) {
2274 return (ANSI_YELLOW
);
2281 * Print out configuration state as requested by status_callback.
2284 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2285 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2287 nvlist_t
**child
, *root
;
2288 uint_t c
, i
, vsc
, children
;
2289 pool_scan_stat_t
*ps
= NULL
;
2291 char rbuf
[6], wbuf
[6], cbuf
[6];
2293 uint64_t notpresent
;
2294 spare_cbdata_t spare_cb
;
2298 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2300 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2301 &child
, &children
) != 0)
2304 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2305 (uint64_t **)&vs
, &vsc
) == 0);
2307 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2309 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2312 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2316 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2319 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2320 state
= gettext("INUSE");
2321 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2322 state
= gettext("AVAIL");
2325 printf_color(health_str_to_color(state
),
2326 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2330 if (vs
->vs_read_errors
)
2333 if (vs
->vs_write_errors
)
2336 if (vs
->vs_checksum_errors
)
2339 if (cb
->cb_literal
) {
2341 printf_color(rcolor
, "%5llu",
2342 (u_longlong_t
)vs
->vs_read_errors
);
2344 printf_color(wcolor
, "%5llu",
2345 (u_longlong_t
)vs
->vs_write_errors
);
2347 printf_color(ccolor
, "%5llu",
2348 (u_longlong_t
)vs
->vs_checksum_errors
);
2350 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2351 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2352 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2355 printf_color(rcolor
, "%5s", rbuf
);
2357 printf_color(wcolor
, "%5s", wbuf
);
2359 printf_color(ccolor
, "%5s", cbuf
);
2361 if (cb
->cb_print_slow_ios
) {
2362 if (children
== 0) {
2363 /* Only leafs vdevs have slow IOs */
2364 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2367 snprintf(rbuf
, sizeof (rbuf
), "-");
2371 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2373 printf(" %5s", rbuf
);
2377 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2378 ¬present
) == 0) {
2379 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2380 (void) printf(" %s %s", gettext("was"), path
);
2381 } else if (vs
->vs_aux
!= 0) {
2383 color_start(ANSI_RED
);
2384 switch (vs
->vs_aux
) {
2385 case VDEV_AUX_OPEN_FAILED
:
2386 (void) printf(gettext("cannot open"));
2389 case VDEV_AUX_BAD_GUID_SUM
:
2390 (void) printf(gettext("missing device"));
2393 case VDEV_AUX_NO_REPLICAS
:
2394 (void) printf(gettext("insufficient replicas"));
2397 case VDEV_AUX_VERSION_NEWER
:
2398 (void) printf(gettext("newer version"));
2401 case VDEV_AUX_UNSUP_FEAT
:
2402 (void) printf(gettext("unsupported feature(s)"));
2405 case VDEV_AUX_ASHIFT_TOO_BIG
:
2406 (void) printf(gettext("unsupported minimum blocksize"));
2409 case VDEV_AUX_SPARED
:
2410 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2411 &spare_cb
.cb_guid
) == 0);
2412 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2413 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2414 zpool_get_name(zhp
)) == 0)
2415 (void) printf(gettext("currently in "
2418 (void) printf(gettext("in use by "
2420 zpool_get_name(spare_cb
.cb_zhp
));
2421 zpool_close(spare_cb
.cb_zhp
);
2423 (void) printf(gettext("currently in use"));
2427 case VDEV_AUX_ERR_EXCEEDED
:
2428 (void) printf(gettext("too many errors"));
2431 case VDEV_AUX_IO_FAILURE
:
2432 (void) printf(gettext("experienced I/O failures"));
2435 case VDEV_AUX_BAD_LOG
:
2436 (void) printf(gettext("bad intent log"));
2439 case VDEV_AUX_EXTERNAL
:
2440 (void) printf(gettext("external device fault"));
2443 case VDEV_AUX_SPLIT_POOL
:
2444 (void) printf(gettext("split into new pool"));
2447 case VDEV_AUX_ACTIVE
:
2448 (void) printf(gettext("currently in use"));
2451 case VDEV_AUX_CHILDREN_OFFLINE
:
2452 (void) printf(gettext("all children offline"));
2455 case VDEV_AUX_BAD_LABEL
:
2456 (void) printf(gettext("invalid label"));
2460 (void) printf(gettext("corrupted data"));
2464 } else if (children
== 0 && !isspare
&&
2465 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2466 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2467 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2469 gettext(" block size: %dB configured, %dB native"),
2470 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2473 if (vs
->vs_scan_removing
!= 0) {
2474 (void) printf(gettext(" (removing)"));
2475 } else if (vs
->vs_noalloc
!= 0) {
2476 (void) printf(gettext(" (non-allocating)"));
2479 /* The root vdev has the scrub/resilver stats */
2480 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2481 ZPOOL_CONFIG_VDEV_TREE
);
2482 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2483 (uint64_t **)&ps
, &c
);
2485 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2486 if (vs
->vs_scan_processed
!= 0) {
2487 (void) printf(gettext(" (%s)"),
2488 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2489 "resilvering" : "repairing");
2490 } else if (vs
->vs_resilver_deferred
) {
2491 (void) printf(gettext(" (awaiting resilver)"));
2495 /* The top-level vdevs have the rebuild stats */
2496 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2498 if (vs
->vs_rebuild_processed
!= 0) {
2499 (void) printf(gettext(" (resilvering)"));
2503 if (cb
->vcdl
!= NULL
) {
2504 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2506 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2510 /* Display vdev initialization and trim status for leaves. */
2511 if (children
== 0) {
2512 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2513 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2516 (void) printf("\n");
2518 for (c
= 0; c
< children
; c
++) {
2519 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2521 /* Don't print logs or holes here */
2522 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2524 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2526 if (islog
|| ishole
)
2528 /* Only print normal classes here */
2529 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2532 /* Provide vdev_rebuild_stats to children if available */
2534 (void) nvlist_lookup_uint64_array(nv
,
2535 ZPOOL_CONFIG_REBUILD_STATS
,
2536 (uint64_t **)&vrs
, &i
);
2539 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2540 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2541 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2548 * Print the configuration of an exported pool. Iterate over all vdevs in the
2549 * pool, printing out the name and status for each one.
2552 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2560 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2561 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2562 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2565 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2566 (uint64_t **)&vs
, &c
) == 0);
2568 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2569 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2571 if (vs
->vs_aux
!= 0) {
2574 switch (vs
->vs_aux
) {
2575 case VDEV_AUX_OPEN_FAILED
:
2576 (void) printf(gettext("cannot open"));
2579 case VDEV_AUX_BAD_GUID_SUM
:
2580 (void) printf(gettext("missing device"));
2583 case VDEV_AUX_NO_REPLICAS
:
2584 (void) printf(gettext("insufficient replicas"));
2587 case VDEV_AUX_VERSION_NEWER
:
2588 (void) printf(gettext("newer version"));
2591 case VDEV_AUX_UNSUP_FEAT
:
2592 (void) printf(gettext("unsupported feature(s)"));
2595 case VDEV_AUX_ERR_EXCEEDED
:
2596 (void) printf(gettext("too many errors"));
2599 case VDEV_AUX_ACTIVE
:
2600 (void) printf(gettext("currently in use"));
2603 case VDEV_AUX_CHILDREN_OFFLINE
:
2604 (void) printf(gettext("all children offline"));
2607 case VDEV_AUX_BAD_LABEL
:
2608 (void) printf(gettext("invalid label"));
2612 (void) printf(gettext("corrupted data"));
2616 (void) printf("\n");
2618 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2619 &child
, &children
) != 0)
2622 for (c
= 0; c
< children
; c
++) {
2623 uint64_t is_log
= B_FALSE
;
2625 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2629 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2632 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2633 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2634 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2638 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2639 &child
, &children
) == 0) {
2640 (void) printf(gettext("\tcache\n"));
2641 for (c
= 0; c
< children
; c
++) {
2642 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2644 (void) printf("\t %s\n", vname
);
2649 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2650 &child
, &children
) == 0) {
2651 (void) printf(gettext("\tspares\n"));
2652 for (c
= 0; c
< children
; c
++) {
2653 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2655 (void) printf("\t %s\n", vname
);
2662 * Print specialized class vdevs.
2664 * These are recorded as top level vdevs in the main pool child array
2665 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2666 * print_status_config() or print_import_config() to print the top level
2667 * class vdevs then any of their children (eg mirrored slogs) are printed
2668 * recursively - which works because only the top level vdev is marked.
2671 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2676 boolean_t printed
= B_FALSE
;
2678 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2680 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2684 for (c
= 0; c
< children
; c
++) {
2685 uint64_t is_log
= B_FALSE
;
2689 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2693 bias
= VDEV_ALLOC_CLASS_LOGS
;
2695 (void) nvlist_lookup_string(child
[c
],
2696 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2697 (void) nvlist_lookup_string(child
[c
],
2698 ZPOOL_CONFIG_TYPE
, &type
);
2701 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2703 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2707 (void) printf("\t%s\t\n", gettext(class));
2711 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2712 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2713 if (cb
->cb_print_status
)
2714 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2717 print_import_config(cb
, name
, child
[c
], 2);
2723 * Display the status for the given pool.
2726 show_import(nvlist_t
*config
, boolean_t report_error
)
2728 uint64_t pool_state
;
2732 uint64_t hostid
= 0;
2734 char *hostname
= "unknown";
2735 nvlist_t
*nvroot
, *nvinfo
;
2736 zpool_status_t reason
;
2737 zpool_errata_t errata
;
2741 status_cbdata_t cb
= { 0 };
2743 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2745 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2747 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2749 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2752 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2753 (uint64_t **)&vs
, &vsc
) == 0);
2754 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2756 reason
= zpool_import_status(config
, &msgid
, &errata
);
2759 * If we're importing using a cachefile, then we won't report any
2760 * errors unless we are in the scan phase of the import.
2762 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
2765 (void) printf(gettext(" pool: %s\n"), name
);
2766 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2767 (void) printf(gettext(" state: %s"), health
);
2768 if (pool_state
== POOL_STATE_DESTROYED
)
2769 (void) printf(gettext(" (DESTROYED)"));
2770 (void) printf("\n");
2773 case ZPOOL_STATUS_MISSING_DEV_R
:
2774 case ZPOOL_STATUS_MISSING_DEV_NR
:
2775 case ZPOOL_STATUS_BAD_GUID_SUM
:
2776 printf_color(ANSI_BOLD
, gettext("status: "));
2777 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2778 "missing from the system.\n"));
2781 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2782 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2783 printf_color(ANSI_BOLD
, gettext("status: "));
2784 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2785 " corrupted data.\n"));
2788 case ZPOOL_STATUS_CORRUPT_DATA
:
2790 gettext(" status: The pool data is corrupted.\n"));
2793 case ZPOOL_STATUS_OFFLINE_DEV
:
2794 printf_color(ANSI_BOLD
, gettext("status: "));
2795 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2796 "are offlined.\n"));
2799 case ZPOOL_STATUS_CORRUPT_POOL
:
2800 printf_color(ANSI_BOLD
, gettext("status: "));
2801 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2805 case ZPOOL_STATUS_VERSION_OLDER
:
2806 printf_color(ANSI_BOLD
, gettext("status: "));
2807 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2808 "a legacy on-disk version.\n"));
2811 case ZPOOL_STATUS_VERSION_NEWER
:
2812 printf_color(ANSI_BOLD
, gettext("status: "));
2813 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2814 "an incompatible version.\n"));
2817 case ZPOOL_STATUS_FEAT_DISABLED
:
2818 printf_color(ANSI_BOLD
, gettext("status: "));
2819 printf_color(ANSI_YELLOW
, gettext("Some supported "
2820 "features are not enabled on the pool.\n\t"
2821 "(Note that they may be intentionally disabled "
2822 "if the\n\t'compatibility' property is set.)\n"));
2825 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2826 printf_color(ANSI_BOLD
, gettext("status: "));
2827 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2828 "the file(s) indicated by the 'compatibility'\n"
2832 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
2833 printf_color(ANSI_BOLD
, gettext("status: "));
2834 printf_color(ANSI_YELLOW
, gettext("One or more features "
2835 "are enabled on the pool despite not being\n"
2836 "requested by the 'compatibility' property.\n"));
2839 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2840 printf_color(ANSI_BOLD
, gettext("status: "));
2841 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2842 "feature(s) not supported on this system:\n"));
2843 color_start(ANSI_YELLOW
);
2844 zpool_print_unsup_feat(config
);
2848 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2849 printf_color(ANSI_BOLD
, gettext("status: "));
2850 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2851 "accessed in read-only mode on this system. It\n\tcannot be"
2852 " accessed in read-write mode because it uses the "
2853 "following\n\tfeature(s) not supported on this system:\n"));
2854 color_start(ANSI_YELLOW
);
2855 zpool_print_unsup_feat(config
);
2859 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2860 printf_color(ANSI_BOLD
, gettext("status: "));
2861 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2862 "imported by another system.\n"));
2865 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2866 printf_color(ANSI_BOLD
, gettext("status: "));
2867 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2868 "multihost property on. It cannot\n\tbe safely imported "
2869 "when the system hostid is not set.\n"));
2872 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2873 printf_color(ANSI_BOLD
, gettext("status: "));
2874 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2875 "by another system.\n"));
2878 case ZPOOL_STATUS_FAULTED_DEV_R
:
2879 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2880 printf_color(ANSI_BOLD
, gettext("status: "));
2881 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2885 case ZPOOL_STATUS_BAD_LOG
:
2886 printf_color(ANSI_BOLD
, gettext("status: "));
2887 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2891 case ZPOOL_STATUS_RESILVERING
:
2892 case ZPOOL_STATUS_REBUILDING
:
2893 printf_color(ANSI_BOLD
, gettext("status: "));
2894 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2895 "being resilvered.\n"));
2898 case ZPOOL_STATUS_ERRATA
:
2899 printf_color(ANSI_BOLD
, gettext("status: "));
2900 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2904 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2905 printf_color(ANSI_BOLD
, gettext("status: "));
2906 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2907 "configured to use a non-native block size.\n"
2908 "\tExpect reduced performance.\n"));
2913 * No other status can be seen when importing pools.
2915 assert(reason
== ZPOOL_STATUS_OK
);
2919 * Print out an action according to the overall state of the pool.
2921 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2922 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2923 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2924 (void) printf(gettext(" action: The pool can be "
2925 "imported using its name or numeric identifier, "
2926 "though\n\tsome features will not be available "
2927 "without an explicit 'zpool upgrade'.\n"));
2928 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
2929 (void) printf(gettext(" action: The pool can be "
2930 "imported using its name or numeric\n\tidentifier, "
2931 "though the file(s) indicated by its "
2932 "'compatibility'\n\tproperty cannot be parsed at "
2934 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2935 (void) printf(gettext(" action: The pool can be "
2936 "imported using its name or numeric "
2937 "identifier and\n\tthe '-f' flag.\n"));
2938 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2940 case ZPOOL_ERRATA_NONE
:
2943 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2944 (void) printf(gettext(" action: The pool can "
2945 "be imported using its name or numeric "
2946 "identifier,\n\thowever there is a compat"
2947 "ibility issue which should be corrected"
2948 "\n\tby running 'zpool scrub'\n"));
2951 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2952 (void) printf(gettext(" action: The pool can"
2953 "not be imported with this version of ZFS "
2954 "due to\n\tan active asynchronous destroy. "
2955 "Revert to an earlier version\n\tand "
2956 "allow the destroy to complete before "
2960 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2961 (void) printf(gettext(" action: Existing "
2962 "encrypted datasets contain an on-disk "
2963 "incompatibility, which\n\tneeds to be "
2964 "corrected. Backup these datasets to new "
2965 "encrypted datasets\n\tand destroy the "
2969 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2970 (void) printf(gettext(" action: Existing "
2971 "encrypted snapshots and bookmarks contain "
2972 "an on-disk\n\tincompatibility. This may "
2973 "cause on-disk corruption if they are used"
2974 "\n\twith 'zfs recv'. To correct the "
2975 "issue, enable the bookmark_v2 feature.\n\t"
2976 "No additional action is needed if there "
2977 "are no encrypted snapshots or\n\t"
2978 "bookmarks. If preserving the encrypted "
2979 "snapshots and bookmarks is\n\trequired, "
2980 "use a non-raw send to backup and restore "
2981 "them. Alternately,\n\tthey may be removed"
2982 " to resolve the incompatibility.\n"));
2986 * All errata must contain an action message.
2991 (void) printf(gettext(" action: The pool can be "
2992 "imported using its name or numeric "
2995 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2996 (void) printf(gettext(" action: The pool can be imported "
2997 "despite missing or damaged devices. The\n\tfault "
2998 "tolerance of the pool may be compromised if imported.\n"));
3001 case ZPOOL_STATUS_VERSION_NEWER
:
3002 (void) printf(gettext(" action: The pool cannot be "
3003 "imported. Access the pool on a system running "
3004 "newer\n\tsoftware, or recreate the pool from "
3007 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
3008 printf_color(ANSI_BOLD
, gettext("action: "));
3009 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3010 "imported. Access the pool on a system that "
3011 "supports\n\tthe required feature(s), or recreate "
3012 "the pool from backup.\n"));
3014 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3015 printf_color(ANSI_BOLD
, gettext("action: "));
3016 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3017 "imported in read-write mode. Import the pool "
3019 "\t\"-o readonly=on\", access the pool on a system "
3020 "that supports the\n\trequired feature(s), or "
3021 "recreate the pool from backup.\n"));
3023 case ZPOOL_STATUS_MISSING_DEV_R
:
3024 case ZPOOL_STATUS_MISSING_DEV_NR
:
3025 case ZPOOL_STATUS_BAD_GUID_SUM
:
3026 (void) printf(gettext(" action: The pool cannot be "
3027 "imported. Attach the missing\n\tdevices and try "
3030 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3031 VERIFY0(nvlist_lookup_nvlist(config
,
3032 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
3034 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3035 hostname
= fnvlist_lookup_string(nvinfo
,
3036 ZPOOL_CONFIG_MMP_HOSTNAME
);
3038 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3039 hostid
= fnvlist_lookup_uint64(nvinfo
,
3040 ZPOOL_CONFIG_MMP_HOSTID
);
3042 (void) printf(gettext(" action: The pool must be "
3043 "exported from %s (hostid=%"PRIx64
")\n\tbefore it "
3044 "can be safely imported.\n"), hostname
, 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 const 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%"PRIx64
")\nExport the pool on the other "
3174 "system, then run 'zpool import'.\n"),
3175 name
, hostname
, 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 const char *hostname
= "<unknown>";
3183 time_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=%"PRIx64
") at %s"
3201 "The pool can be imported, use 'zpool import -f' "
3202 "to import the pool.\n"), name
, hostname
,
3203 hostid
, ctime(×tamp
));
3209 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3212 if (newname
!= NULL
)
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 zfs_crypto_attempt_load_keys(g_zfs
, name
) != 0)
3226 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3227 !(flags
& ZFS_IMPORT_ONLY
) &&
3228 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3238 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3239 char *orig_name
, char *new_name
,
3240 boolean_t do_destroyed
, boolean_t pool_specified
, boolean_t do_all
,
3241 importargs_t
*import
)
3243 nvlist_t
*config
= NULL
;
3244 nvlist_t
*found_config
= NULL
;
3245 uint64_t pool_state
;
3248 * At this point we have a list of import candidate configs. Even if
3249 * we were searching by pool name or guid, we still need to
3250 * post-process the list to deal with pool state and possible
3254 nvpair_t
*elem
= NULL
;
3255 boolean_t first
= B_TRUE
;
3256 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3258 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3260 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3262 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3264 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3267 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3268 import
->policy
) == 0);
3270 if (!pool_specified
) {
3274 (void) putchar('\n');
3277 err
|= do_import(config
, NULL
, mntopts
,
3281 * If we're importing from cachefile, then
3282 * we don't want to report errors until we
3283 * are in the scan phase of the import. If
3284 * we get an error, then we return that error
3285 * to invoke the scan phase.
3287 if (import
->cachefile
&& !import
->scan
)
3288 err
= show_import(config
, B_FALSE
);
3290 (void) show_import(config
, B_TRUE
);
3292 } else if (import
->poolname
!= NULL
) {
3296 * We are searching for a pool based on name.
3298 verify(nvlist_lookup_string(config
,
3299 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3301 if (strcmp(name
, import
->poolname
) == 0) {
3302 if (found_config
!= NULL
) {
3303 (void) fprintf(stderr
, gettext(
3304 "cannot import '%s': more than "
3305 "one matching pool\n"),
3307 (void) fprintf(stderr
, gettext(
3308 "import by numeric ID instead\n"));
3311 found_config
= config
;
3317 * Search for a pool by guid.
3319 verify(nvlist_lookup_uint64(config
,
3320 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3322 if (guid
== import
->guid
)
3323 found_config
= config
;
3328 * If we were searching for a specific pool, verify that we found a
3329 * pool, and then do the import.
3331 if (pool_specified
&& err
== 0) {
3332 if (found_config
== NULL
) {
3333 (void) fprintf(stderr
, gettext("cannot import '%s': "
3334 "no such pool available\n"), orig_name
);
3337 err
|= do_import(found_config
, new_name
,
3338 mntopts
, props
, flags
);
3343 * If we were just looking for pools, report an error if none were
3346 if (!pool_specified
&& first
)
3347 (void) fprintf(stderr
,
3348 gettext("no pools available to import\n"));
3352 typedef struct target_exists_args
{
3353 const char *poolname
;
3355 } target_exists_args_t
;
3358 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3360 target_exists_args_t
*args
= data
;
3361 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3367 if (args
->poolname
!= NULL
) {
3370 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3372 if (strcmp(pool_name
, args
->poolname
) == 0)
3377 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3379 if (pool_guid
== args
->poolguid
)
3387 * zpool checkpoint <pool>
3388 * checkpoint --discard <pool>
3390 * -d Discard the checkpoint from a checkpointed
3393 * -w Wait for discarding a checkpoint to complete.
3396 * Checkpoints the specified pool, by taking a "snapshot" of its
3397 * current state. A pool can only have one checkpoint at a time.
3400 zpool_do_checkpoint(int argc
, char **argv
)
3402 boolean_t discard
, wait
;
3404 zpool_handle_t
*zhp
;
3407 struct option long_options
[] = {
3408 {"discard", no_argument
, NULL
, 'd'},
3409 {"wait", no_argument
, NULL
, 'w'},
3415 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3424 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3430 if (wait
&& !discard
) {
3431 (void) fprintf(stderr
, gettext("--wait only valid when "
3432 "--discard also specified\n"));
3440 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3445 (void) fprintf(stderr
, gettext("too many arguments\n"));
3451 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3452 /* As a special case, check for use of '/' in the name */
3453 if (strchr(pool
, '/') != NULL
)
3454 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3455 "doesn't work on datasets. To save the state "
3456 "of a dataset from a specific point in time "
3457 "please use 'zfs snapshot'\n"));
3462 err
= (zpool_discard_checkpoint(zhp
) != 0);
3463 if (err
== 0 && wait
)
3464 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3466 err
= (zpool_checkpoint(zhp
) != 0);
3474 #define CHECKPOINT_OPT 1024
3477 * zpool import [-d dir] [-D]
3478 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3479 * [-d dir | -c cachefile | -s] [-f] -a
3480 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3481 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3484 * -c Read pool information from a cachefile instead of searching
3485 * devices. If importing from a cachefile config fails, then
3486 * fallback to searching for devices only in the directories that
3487 * exist in the cachefile.
3489 * -d Scan in a specific directory, other than /dev/. More than
3490 * one directory can be specified using multiple '-d' options.
3492 * -D Scan for previously destroyed pools or import all or only
3493 * specified destroyed pools.
3495 * -R Temporarily import the pool, with all mountpoints relative to
3496 * the given root. The pool will remain exported when the machine
3499 * -V Import even in the presence of faulted vdevs. This is an
3500 * intentionally undocumented option for testing purposes, and
3501 * treats the pool configuration as complete, leaving any bad
3502 * vdevs in the FAULTED state. In other words, it does verbatim
3505 * -f Force import, even if it appears that the pool is active.
3507 * -F Attempt rewind if necessary.
3509 * -n See if rewind would work, but don't actually rewind.
3511 * -N Import the pool but don't mount datasets.
3513 * -T Specify a starting txg to use for import. This option is
3514 * intentionally undocumented option for testing purposes.
3516 * -a Import all pools found.
3518 * -l Load encryption keys while importing.
3520 * -o Set property=value and/or temporary mount options (without '=').
3522 * -s Scan using the default search path, the libblkid cache will
3525 * --rewind-to-checkpoint
3526 * Import the pool and revert back to the checkpoint.
3528 * The import command scans for pools to import, and import pools based on pool
3529 * name and GUID. The pool can also be renamed as part of the import process.
3532 zpool_do_import(int argc
, char **argv
)
3534 char **searchdirs
= NULL
;
3535 char *env
, *envdup
= NULL
;
3539 nvlist_t
*pools
= NULL
;
3540 boolean_t do_all
= B_FALSE
;
3541 boolean_t do_destroyed
= B_FALSE
;
3542 char *mntopts
= NULL
;
3543 uint64_t searchguid
= 0;
3544 char *searchname
= NULL
;
3546 nvlist_t
*policy
= NULL
;
3547 nvlist_t
*props
= NULL
;
3548 int flags
= ZFS_IMPORT_NORMAL
;
3549 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3550 boolean_t dryrun
= B_FALSE
;
3551 boolean_t do_rewind
= B_FALSE
;
3552 boolean_t xtreme_rewind
= B_FALSE
;
3553 boolean_t do_scan
= B_FALSE
;
3554 boolean_t pool_exists
= B_FALSE
;
3555 boolean_t pool_specified
= B_FALSE
;
3556 uint64_t txg
= -1ULL;
3557 char *cachefile
= NULL
;
3558 importargs_t idata
= { 0 };
3561 struct option long_options
[] = {
3562 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3567 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3568 long_options
, NULL
)) != -1) {
3577 searchdirs
= safe_realloc(searchdirs
,
3578 (nsearch
+ 1) * sizeof (char *));
3579 searchdirs
[nsearch
++] = optarg
;
3582 do_destroyed
= B_TRUE
;
3585 flags
|= ZFS_IMPORT_ANY_HOST
;
3591 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3594 flags
|= ZFS_IMPORT_MISSING_LOG
;
3600 flags
|= ZFS_IMPORT_ONLY
;
3603 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3606 if (add_prop_list(optarg
, propval
,
3614 if (add_prop_list(zpool_prop_to_name(
3615 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3617 if (add_prop_list_default(zpool_prop_to_name(
3618 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3625 flags
|= ZFS_IMPORT_TEMP_NAME
;
3626 if (add_prop_list_default(zpool_prop_to_name(
3627 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3633 txg
= strtoull(optarg
, &endptr
, 0);
3634 if (errno
!= 0 || *endptr
!= '\0') {
3635 (void) fprintf(stderr
,
3636 gettext("invalid txg value\n"));
3639 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3642 flags
|= ZFS_IMPORT_VERBATIM
;
3645 xtreme_rewind
= B_TRUE
;
3647 case CHECKPOINT_OPT
:
3648 flags
|= ZFS_IMPORT_CHECKPOINT
;
3651 (void) fprintf(stderr
, gettext("missing argument for "
3652 "'%c' option\n"), optopt
);
3656 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3665 if (cachefile
&& nsearch
!= 0) {
3666 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3670 if (cachefile
&& do_scan
) {
3671 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
3675 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3676 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3680 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3681 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3686 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3687 (void) fprintf(stderr
,
3688 gettext("-n or -X only meaningful with -F\n"));
3692 rewind_policy
= ZPOOL_TRY_REWIND
;
3694 rewind_policy
= ZPOOL_DO_REWIND
;
3696 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3698 /* In the future, we can capture further policy and include it here */
3699 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3700 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3701 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3702 rewind_policy
) != 0)
3705 /* check argument count */
3708 (void) fprintf(stderr
, gettext("too many arguments\n"));
3713 (void) fprintf(stderr
, gettext("too many arguments\n"));
3719 * Check for the effective uid. We do this explicitly here because
3720 * otherwise any attempt to discover pools will silently fail.
3722 if (argc
== 0 && geteuid() != 0) {
3723 (void) fprintf(stderr
, gettext("cannot "
3724 "discover pools: permission denied\n"));
3728 nvlist_free(policy
);
3733 * Depending on the arguments given, we do one of the following:
3735 * <none> Iterate through all pools and display information about
3738 * -a Iterate through all pools and try to import each one.
3740 * <id> Find the pool that corresponds to the given GUID/pool
3741 * name and import that one.
3743 * -D Above options applies only to destroyed pools.
3749 searchguid
= strtoull(argv
[0], &endptr
, 10);
3750 if (errno
!= 0 || *endptr
!= '\0') {
3751 searchname
= argv
[0];
3754 pool_specified
= B_TRUE
;
3757 * User specified a name or guid. Ensure it's unique.
3759 target_exists_args_t search
= {searchname
, searchguid
};
3760 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3764 * Check the environment for the preferred search path.
3766 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3767 char *dir
, *tmp
= NULL
;
3769 envdup
= strdup(env
);
3771 for (dir
= strtok_r(envdup
, ":", &tmp
);
3773 dir
= strtok_r(NULL
, ":", &tmp
)) {
3774 searchdirs
= safe_realloc(searchdirs
,
3775 (nsearch
+ 1) * sizeof (char *));
3776 searchdirs
[nsearch
++] = dir
;
3780 idata
.path
= searchdirs
;
3781 idata
.paths
= nsearch
;
3782 idata
.poolname
= searchname
;
3783 idata
.guid
= searchguid
;
3784 idata
.cachefile
= cachefile
;
3785 idata
.scan
= do_scan
;
3786 idata
.policy
= policy
;
3788 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3790 if (pools
!= NULL
&& pool_exists
&&
3791 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3792 (void) fprintf(stderr
, gettext("cannot import '%s': "
3793 "a pool with that name already exists\n"),
3795 (void) fprintf(stderr
, gettext("use the form '%s "
3796 "<pool | id> <newpool>' to give it a new name\n"),
3799 } else if (pools
== NULL
&& pool_exists
) {
3800 (void) fprintf(stderr
, gettext("cannot import '%s': "
3801 "a pool with that name is already created/imported,\n"),
3803 (void) fprintf(stderr
, gettext("and no additional pools "
3804 "with that name were found\n"));
3806 } else if (pools
== NULL
) {
3808 (void) fprintf(stderr
, gettext("cannot import '%s': "
3809 "no such pool available\n"), argv
[0]);
3817 nvlist_free(policy
);
3823 err
= import_pools(pools
, props
, mntopts
, flags
,
3824 argc
>= 1 ? argv
[0] : NULL
,
3825 argc
>= 2 ? argv
[1] : NULL
,
3826 do_destroyed
, pool_specified
, do_all
, &idata
);
3829 * If we're using the cachefile and we failed to import, then
3830 * fallback to scanning the directory for pools that match
3831 * those in the cachefile.
3833 if (err
!= 0 && cachefile
!= NULL
) {
3834 (void) printf(gettext("cachefile import failed, retrying\n"));
3837 * We use the scan flag to gather the directories that exist
3838 * in the cachefile. If we need to fallback to searching for
3839 * the pool config, we will only search devices in these
3842 idata
.scan
= B_TRUE
;
3844 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3846 err
= import_pools(pools
, props
, mntopts
, flags
,
3847 argc
>= 1 ? argv
[0] : NULL
,
3848 argc
>= 2 ? argv
[1] : NULL
,
3849 do_destroyed
, pool_specified
, do_all
, &idata
);
3855 nvlist_free(policy
);
3859 return (err
? 1 : 0);
3863 * zpool sync [-f] [pool] ...
3865 * -f (undocumented) force uberblock (and config including zpool cache file)
3868 * Sync the specified pool(s).
3869 * Without arguments "zpool sync" will sync all pools.
3870 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3874 zpool_do_sync(int argc
, char **argv
)
3877 boolean_t force
= B_FALSE
;
3880 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3886 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3895 /* if argc == 0 we will execute zpool_sync_one on all pools */
3896 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
3897 B_FALSE
, zpool_sync_one
, &force
);
3902 typedef struct iostat_cbdata
{
3906 boolean_t cb_verbose
;
3907 boolean_t cb_literal
;
3908 boolean_t cb_scripted
;
3909 zpool_list_t
*cb_list
;
3910 vdev_cmd_data_list_t
*vcdl
;
3911 vdev_cbdata_t cb_vdevs
;
3915 typedef struct name_and_columns
{
3916 const char *name
; /* Column name */
3917 unsigned int columns
; /* Center name to this number of columns */
3918 } name_and_columns_t
;
3920 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
3922 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3924 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3926 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3927 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3929 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3930 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3931 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL
}},
3932 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3933 {"asyncq_wait", 2}, {NULL
}},
3934 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3935 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3936 {"trim", 2}, {"rebuild", 2}, {NULL
}},
3939 /* Shorthand - if "columns" field not set, default to 1 column */
3940 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3942 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3944 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3945 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3947 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3948 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3949 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
3950 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3951 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3953 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3954 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3955 {"ind"}, {"agg"}, {NULL
}},
3958 static const char *histo_to_title
[] = {
3959 [IOS_L_HISTO
] = "latency",
3960 [IOS_RQ_HISTO
] = "req_size",
3964 * Return the number of labels in a null-terminated name_and_columns_t
3969 label_array_len(const name_and_columns_t
*labels
)
3973 while (labels
[i
].name
)
3980 * Return the number of strings in a null-terminated string array.
3983 * const char foo[] = {"bar", "baz", NULL}
3988 str_array_len(const char *array
[])
3999 * Return a default column width for default/latency/queue columns. This does
4000 * not include histograms, which have their columns autosized.
4003 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
4005 unsigned long column_width
= 5; /* Normal niceprint */
4006 static unsigned long widths
[] = {
4008 * Choose some sane default column sizes for printing the
4011 [IOS_DEFAULT
] = 15, /* 1PB capacity */
4012 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
4013 [IOS_QUEUES
] = 6, /* 1M queue entries */
4014 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
4015 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
4019 column_width
= widths
[type
];
4021 return (column_width
);
4025 * Print the column labels, i.e:
4027 * capacity operations bandwidth
4028 * alloc free read write read write ...
4030 * If force_column_width is set, use it for the column width. If not set, use
4031 * the default column width.
4034 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4035 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
4038 int text_start
, rw_column_width
, spaces_to_end
;
4039 uint64_t flags
= cb
->cb_flags
;
4041 unsigned int column_width
= force_column_width
;
4043 /* For each bit set in flags */
4044 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4045 idx
= lowbit64(f
) - 1;
4046 if (!force_column_width
)
4047 column_width
= default_column_width(cb
, idx
);
4048 /* Print our top labels centered over "read write" label. */
4049 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
4050 const char *name
= labels
[idx
][i
].name
;
4052 * We treat labels[][].columns == 0 as shorthand
4053 * for one column. It makes writing out the label
4054 * tables more concise.
4056 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
4057 unsigned int slen
= strlen(name
);
4059 rw_column_width
= (column_width
* columns
) +
4060 (2 * (columns
- 1));
4062 text_start
= (int)((rw_column_width
) / columns
-
4067 printf(" "); /* Two spaces between columns */
4069 /* Space from beginning of column to label */
4070 for (s
= 0; s
< text_start
; s
++)
4075 /* Print space after label to end of column */
4076 spaces_to_end
= rw_column_width
- text_start
- slen
;
4077 if (spaces_to_end
< 0)
4080 for (s
= 0; s
< spaces_to_end
; s
++)
4088 * print_cmd_columns - Print custom column titles from -c
4090 * If the user specified the "zpool status|iostat -c" then print their custom
4091 * column titles in the header. For example, print_cmd_columns() would print
4092 * the " col1 col2" part of this:
4094 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4096 * capacity operations bandwidth
4097 * pool alloc free read write read write col1 col2
4098 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4099 * mypool 269K 1008M 0 0 107 946
4100 * mirror 269K 1008M 0 0 107 946
4101 * sdb - - 0 0 102 473 val1 val2
4102 * sdc - - 0 0 5 473 val1 val2
4103 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4106 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4109 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4111 if (vcdl
->count
== 0 || data
== NULL
)
4115 * Each vdev cmd should have the same column names unless the user did
4116 * something weird with their cmd. Just take the column names from the
4117 * first vdev and assume it works for all of them.
4119 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4122 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4125 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4126 vcdl
->uniq_cols
[i
]);
4133 * Utility function to print out a line of dashes like:
4135 * -------------------------------- ----- ----- ----- ----- -----
4137 * ...or a dashed named-row line like:
4143 * @force_column_width If non-zero, use the value as the column width.
4144 * Otherwise use the default column widths.
4146 * @name: Print a dashed named-row line starting
4147 * with @name. Otherwise, print a regular
4151 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4155 unsigned int namewidth
;
4156 uint64_t flags
= cb
->cb_flags
;
4159 const name_and_columns_t
*labels
;
4163 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4164 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4165 } else if (cb
->cb_vdevs
.cb_names_count
) {
4171 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4172 name
? strlen(name
) : 0);
4176 printf("%-*s", namewidth
, name
);
4178 for (i
= 0; i
< namewidth
; i
++)
4182 /* For each bit in flags */
4183 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4184 unsigned int column_width
;
4185 idx
= lowbit64(f
) - 1;
4186 if (force_column_width
)
4187 column_width
= force_column_width
;
4189 column_width
= default_column_width(cb
, idx
);
4191 labels
= iostat_bottom_labels
[idx
];
4192 for (i
= 0; i
< label_array_len(labels
); i
++) {
4194 printf(" %*s-", column_width
- 1, " ");
4196 printf(" %.*s", column_width
,
4197 "--------------------");
4204 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4205 unsigned int force_column_width
)
4207 print_iostat_dashes(cb
, force_column_width
, NULL
);
4211 print_iostat_separator(iostat_cbdata_t
*cb
)
4213 print_iostat_separator_impl(cb
, 0);
4217 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4218 const char *histo_vdev_name
)
4220 unsigned int namewidth
;
4223 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4224 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4225 } else if (cb
->cb_vdevs
.cb_names_count
) {
4231 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4232 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4234 if (histo_vdev_name
)
4235 printf("%-*s", namewidth
, histo_vdev_name
);
4237 printf("%*s", namewidth
, "");
4240 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4243 printf("%-*s", namewidth
, title
);
4245 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4246 if (cb
->vcdl
!= NULL
)
4247 print_cmd_columns(cb
->vcdl
, 0);
4251 print_iostat_separator_impl(cb
, force_column_width
);
4253 if (cb
->vcdl
!= NULL
)
4254 print_cmd_columns(cb
->vcdl
, 1);
4260 print_iostat_header(iostat_cbdata_t
*cb
)
4262 print_iostat_header_impl(cb
, 0, NULL
);
4267 * Display a single statistic.
4270 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4271 unsigned int column_size
, boolean_t scripted
)
4275 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4278 printf("\t%s", buf
);
4280 printf(" %*s", column_size
, buf
);
4284 * Calculate the default vdev stats
4286 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4287 * stats into calcvs.
4290 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4291 vdev_stat_t
*calcvs
)
4295 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4296 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4297 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4299 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4300 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4304 * Internal representation of the extended iostats data.
4306 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4307 * or single uint64_t's. We make both look like arrays to make them easier
4308 * to process. In order to make single uint64_t's look like arrays, we set
4309 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4310 * we can just use *data and count.
4314 uint_t count
; /* Number of entries in data[] */
4315 uint64_t __data
; /* Only used when data is a single uint64_t */
4319 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4323 for (i
= 0; i
< len
; i
++)
4324 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4330 * Helper function to lookup a uint64_t array or uint64_t value and store its
4331 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4332 * it look like a one element array to make it easier to process.
4335 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4336 struct stat_array
*nva
)
4341 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4342 switch (nvpair_type(tmp
)) {
4343 case DATA_TYPE_UINT64_ARRAY
:
4344 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4346 case DATA_TYPE_UINT64
:
4347 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4348 nva
->data
= &nva
->__data
;
4352 /* Not a uint64_t */
4361 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4362 * subtract them, and return the results in a newly allocated stat_array.
4363 * You must free the returned array after you are done with it with
4364 * free_calc_stats().
4366 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4369 static struct stat_array
*
4370 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4373 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4374 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4376 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4378 /* Extract our extended stats nvlist from the main list */
4379 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4382 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4386 newnva
= safe_malloc(alloc_size
);
4387 oldnva
= safe_malloc(alloc_size
);
4388 calcnva
= safe_malloc(alloc_size
);
4390 for (j
= 0; j
< len
; j
++) {
4391 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4393 calcnva
[j
].count
= newnva
[j
].count
;
4394 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4395 calcnva
[j
].data
= safe_malloc(alloc_size
);
4396 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4399 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4401 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4402 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4411 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4414 for (i
= 0; i
< len
; i
++)
4421 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4422 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4428 enum zfs_nicenum_format format
;
4429 unsigned int buckets
;
4430 unsigned int start_bucket
;
4433 format
= ZFS_NICENUM_RAW
;
4435 format
= ZFS_NICENUM_1024
;
4437 /* All these histos are the same size, so just use nva[0].count */
4438 buckets
= nva
[0].count
;
4440 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4441 /* Start at 512 - req size should never be lower than this */
4447 for (j
= start_bucket
; j
< buckets
; j
++) {
4448 /* Print histogram bucket label */
4449 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4450 /* Ending range of this bucket */
4451 val
= (1UL << (j
+ 1)) - 1;
4452 zfs_nicetime(val
, buf
, sizeof (buf
));
4454 /* Request size (starting range of bucket) */
4456 zfs_nicenum(val
, buf
, sizeof (buf
));
4459 if (cb
->cb_scripted
)
4460 printf("%llu", (u_longlong_t
)val
);
4462 printf("%-*s", namewidth
, buf
);
4464 /* Print the values on the line */
4465 for (i
= 0; i
< len
; i
++) {
4466 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4467 column_width
, cb
->cb_scripted
);
4474 print_solid_separator(unsigned int length
)
4482 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4483 nvlist_t
*newnv
, double scale
, const char *name
)
4485 unsigned int column_width
;
4486 unsigned int namewidth
;
4487 unsigned int entire_width
;
4488 enum iostat_type type
;
4489 struct stat_array
*nva
;
4491 unsigned int names_len
;
4493 /* What type of histo are we? */
4494 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4496 /* Get NULL-terminated array of nvlist names for our histo */
4497 names
= vsx_type_to_nvlist
[type
];
4498 names_len
= str_array_len(names
); /* num of names */
4500 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4502 if (cb
->cb_literal
) {
4503 column_width
= MAX(5,
4504 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4509 namewidth
= MAX(cb
->cb_namewidth
,
4510 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4513 * Calculate the entire line width of what we're printing. The
4514 * +2 is for the two spaces between columns:
4518 /* |___| <---------- column_width */
4520 /* |__________| <--- entire_width */
4522 entire_width
= namewidth
+ (column_width
+ 2) *
4523 label_array_len(iostat_bottom_labels
[type
]);
4525 if (cb
->cb_scripted
)
4526 printf("%s\n", name
);
4528 print_iostat_header_impl(cb
, column_width
, name
);
4530 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4533 free_calc_stats(nva
, names_len
);
4534 if (!cb
->cb_scripted
)
4535 print_solid_separator(entire_width
);
4539 * Calculate the average latency of a power-of-two latency histogram
4542 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4545 uint64_t count
= 0, total
= 0;
4547 for (i
= 0; i
< buckets
; i
++) {
4549 * Our buckets are power-of-two latency ranges. Use the
4550 * midpoint latency of each bucket to calculate the average.
4558 if (histo
[i
] != 0) {
4559 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4564 /* Prevent divide by zero */
4565 return (count
== 0 ? 0 : total
/ count
);
4569 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*newnv
)
4571 const char *names
[] = {
4572 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4573 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4574 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4575 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4576 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4577 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4578 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4579 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4580 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4581 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4582 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4583 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4584 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE
,
4585 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
4588 struct stat_array
*nva
;
4590 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4591 enum zfs_nicenum_format format
;
4593 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4596 format
= ZFS_NICENUM_RAW
;
4598 format
= ZFS_NICENUM_1024
;
4600 for (int i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4601 uint64_t val
= nva
[i
].data
[0];
4602 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4605 free_calc_stats(nva
, ARRAY_SIZE(names
));
4609 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4614 const char *names
[] = {
4615 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4616 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4617 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4618 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4619 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4620 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4621 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4622 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4623 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4624 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4625 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
4627 struct stat_array
*nva
;
4629 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4630 enum zfs_nicenum_format format
;
4632 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4635 format
= ZFS_NICENUM_RAWTIME
;
4637 format
= ZFS_NICENUM_TIME
;
4639 /* Print our avg latencies on the line */
4640 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4641 /* Compute average latency for a latency histo */
4642 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4643 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4645 free_calc_stats(nva
, ARRAY_SIZE(names
));
4649 * Print default statistics (capacity/operations/bandwidth)
4652 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4654 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4655 enum zfs_nicenum_format format
;
4656 char na
; /* char to print for "not applicable" values */
4658 if (cb
->cb_literal
) {
4659 format
= ZFS_NICENUM_RAW
;
4662 format
= ZFS_NICENUM_1024
;
4666 /* only toplevel vdevs have capacity stats */
4667 if (vs
->vs_space
== 0) {
4668 if (cb
->cb_scripted
)
4669 printf("\t%c\t%c", na
, na
);
4671 printf(" %*c %*c", column_width
, na
, column_width
,
4674 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4676 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4677 column_width
, cb
->cb_scripted
);
4680 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4681 format
, column_width
, cb
->cb_scripted
);
4682 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4683 format
, column_width
, cb
->cb_scripted
);
4684 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4685 format
, column_width
, cb
->cb_scripted
);
4686 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4687 format
, column_width
, cb
->cb_scripted
);
4690 static const char *const class_name
[] = {
4691 VDEV_ALLOC_BIAS_DEDUP
,
4692 VDEV_ALLOC_BIAS_SPECIAL
,
4693 VDEV_ALLOC_CLASS_LOGS
4697 * Print out all the statistics for the given vdev. This can either be the
4698 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4699 * is a verbose output, and we don't want to display the toplevel pool stats.
4701 * Returns the number of stat lines printed.
4704 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4705 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4707 nvlist_t
**oldchild
, **newchild
;
4708 uint_t c
, children
, oldchildren
;
4709 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4710 vdev_stat_t zerovs
= { 0 };
4717 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4720 calcvs
= safe_malloc(sizeof (*calcvs
));
4722 if (oldnv
!= NULL
) {
4723 verify(nvlist_lookup_uint64_array(oldnv
,
4724 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4729 /* Do we only want to see a specific vdev? */
4730 for (i
= 0; i
< cb
->cb_vdevs
.cb_names_count
; i
++) {
4731 /* Yes we do. Is this the vdev? */
4732 if (strcmp(name
, cb
->cb_vdevs
.cb_names
[i
]) == 0) {
4734 * This is our vdev. Since it is the only vdev we
4735 * will be displaying, make depth = 0 so that it
4736 * doesn't get indented.
4743 if (cb
->cb_vdevs
.cb_names_count
&& (i
== cb
->cb_vdevs
.cb_names_count
)) {
4744 /* Couldn't match the name */
4749 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4750 (uint64_t **)&newvs
, &c
) == 0);
4753 * Print the vdev name unless it's is a histogram. Histograms
4754 * display the vdev name in the header itself.
4756 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4757 if (cb
->cb_scripted
) {
4760 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4761 (void) printf("%*s%s", depth
, "", name
);
4763 (void) printf("%*s%s%*s", depth
, "", name
,
4764 (int)(cb
->cb_namewidth
- strlen(name
) -
4769 /* Calculate our scaling factor */
4770 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4771 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4773 * If we specify printing histograms with no time interval, then
4774 * print the histogram numbers over the entire lifetime of the
4782 scale
= (double)NANOSEC
/ tdelta
;
4785 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4786 calc_default_iostats(oldvs
, newvs
, calcvs
);
4787 print_iostat_default(calcvs
, cb
, scale
);
4789 if (cb
->cb_flags
& IOS_LATENCY_M
)
4790 print_iostat_latency(cb
, oldnv
, newnv
);
4791 if (cb
->cb_flags
& IOS_QUEUES_M
)
4792 print_iostat_queues(cb
, newnv
);
4793 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4795 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4798 if (cb
->vcdl
!= NULL
) {
4800 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4803 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4807 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4816 if (!cb
->cb_verbose
)
4819 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4820 &newchild
, &children
) != 0)
4824 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4825 &oldchild
, &oldchildren
) != 0)
4828 children
= MIN(oldchildren
, children
);
4832 * print normal top-level devices
4834 for (c
= 0; c
< children
; c
++) {
4835 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4837 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4840 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4843 if (ishole
|| islog
)
4846 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4849 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4850 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
4851 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4852 newchild
[c
], cb
, depth
+ 2);
4857 * print all other top-level devices
4859 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
4860 boolean_t printed
= B_FALSE
;
4862 for (c
= 0; c
< children
; c
++) {
4863 uint64_t islog
= B_FALSE
;
4867 (void) nvlist_lookup_uint64(newchild
[c
],
4868 ZPOOL_CONFIG_IS_LOG
, &islog
);
4870 bias
= VDEV_ALLOC_CLASS_LOGS
;
4872 (void) nvlist_lookup_string(newchild
[c
],
4873 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4874 (void) nvlist_lookup_string(newchild
[c
],
4875 ZPOOL_CONFIG_TYPE
, &type
);
4877 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4879 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4883 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4885 !cb
->cb_vdevs
.cb_names
) {
4886 print_iostat_dashes(cb
, 0,
4893 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4894 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
4895 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4896 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4902 * Include level 2 ARC devices in iostat output
4904 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4905 &newchild
, &children
) != 0)
4909 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4910 &oldchild
, &oldchildren
) != 0)
4913 children
= MIN(oldchildren
, children
);
4917 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4918 !cb
->cb_vdevs
.cb_names
) {
4919 print_iostat_dashes(cb
, 0, "cache");
4923 for (c
= 0; c
< children
; c
++) {
4924 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4925 cb
->cb_vdevs
.cb_name_flags
);
4926 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4927 : NULL
, newchild
[c
], cb
, depth
+ 2);
4936 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4938 iostat_cbdata_t
*cb
= data
;
4942 * If the pool has disappeared, remove it from the list and continue.
4944 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4948 pool_list_remove(cb
->cb_list
, zhp
);
4954 * Callback to print out the iostats for the given pool.
4957 print_iostat(zpool_handle_t
*zhp
, void *data
)
4959 iostat_cbdata_t
*cb
= data
;
4960 nvlist_t
*oldconfig
, *newconfig
;
4961 nvlist_t
*oldnvroot
, *newnvroot
;
4964 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4966 if (cb
->cb_iteration
== 1)
4969 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4972 if (oldconfig
== NULL
)
4975 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4978 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4980 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4981 !cb
->cb_scripted
&& cb
->cb_verbose
&&
4982 !cb
->cb_vdevs
.cb_names_count
) {
4983 print_iostat_separator(cb
);
4984 if (cb
->vcdl
!= NULL
) {
4985 print_cmd_columns(cb
->vcdl
, 1);
5000 if (isatty(STDOUT_FILENO
)) {
5001 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
5003 columns
= ws
.ws_col
;
5012 * Return the required length of the pool/vdev name column. The minimum
5013 * allowed width and output formatting flags must be provided.
5016 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
5018 nvlist_t
*config
, *nvroot
;
5019 int width
= min_width
;
5021 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
5022 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5024 size_t poolname_len
= strlen(zpool_get_name(zhp
));
5025 if (verbose
== B_FALSE
) {
5026 width
= MAX(poolname_len
, min_width
);
5028 width
= MAX(poolname_len
,
5029 max_width(zhp
, nvroot
, 0, min_width
, flags
));
5037 * Parse the input string, get the 'interval' and 'count' value if there is one.
5040 get_interval_count(int *argcp
, char **argv
, float *iv
,
5044 unsigned long count
= 0;
5048 * Determine if the last argument is an integer or a pool name
5050 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5054 interval
= strtof(argv
[argc
- 1], &end
);
5056 if (*end
== '\0' && errno
== 0) {
5057 if (interval
== 0) {
5058 (void) fprintf(stderr
, gettext(
5059 "interval cannot be zero\n"));
5063 * Ignore the last parameter
5068 * If this is not a valid number, just plow on. The
5069 * user will get a more informative error message later
5077 * If the last argument is also an integer, then we have both a count
5080 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5085 interval
= strtof(argv
[argc
- 1], &end
);
5087 if (*end
== '\0' && errno
== 0) {
5088 if (interval
== 0) {
5089 (void) fprintf(stderr
, gettext(
5090 "interval cannot be zero\n"));
5095 * Ignore the last parameter
5109 get_timestamp_arg(char c
)
5112 timestamp_fmt
= UDATE
;
5114 timestamp_fmt
= DDATE
;
5120 * Return stat flags that are supported by all pools by both the module and
5121 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5122 * It will get ANDed down until only the flags that are supported on all pools
5126 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5128 uint64_t *mask
= data
;
5129 nvlist_t
*config
, *nvroot
, *nvx
;
5133 config
= zpool_get_config(zhp
, NULL
);
5134 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5137 /* Default stats are always supported, but for completeness.. */
5138 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5139 flags
|= IOS_DEFAULT_M
;
5141 /* Get our extended stats nvlist from the main list */
5142 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5145 * No extended stats; they're probably running an older
5146 * module. No big deal, we support that too.
5151 /* For each extended stat, make sure all its nvpairs are supported */
5152 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5153 if (!vsx_type_to_nvlist
[j
][0])
5156 /* Start off by assuming the flag is supported, then check */
5157 flags
|= (1ULL << j
);
5158 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5159 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5160 /* flag isn't supported */
5161 flags
= flags
& ~(1ULL << j
);
5167 *mask
= *mask
& flags
;
5172 * Return a bitmask of stats that are supported on all pools by both the module
5176 get_stat_flags(zpool_list_t
*list
)
5181 * get_stat_flags_cb() will lop off bits from "mask" until only the
5182 * flags that are supported on all pools remain.
5184 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5189 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5192 is_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *cb_data
)
5194 vdev_cbdata_t
*cb
= cb_data
;
5196 int ret
= 1; /* assume match */
5197 zpool_handle_t
*zhp
= zhp_data
;
5199 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
5201 if (strcmp(name
, cb
->cb_names
[0])) {
5203 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, VDEV_NAME_GUID
);
5204 ret
= (strcmp(name
, cb
->cb_names
[0]) == 0);
5212 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5215 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5217 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5221 * Check if vdevs are in a pool
5223 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5224 * return 0. If pool_name is NULL, then search all pools.
5227 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5235 if ((argc
== 0) || !*argv
)
5241 /* Temporarily hijack cb_names for a second... */
5242 tmp_name
= cb
->cb_names
;
5244 /* Go though our list of prospective vdev names */
5245 for (i
= 0; i
< argc
; i
++) {
5246 cb
->cb_names
= argv
+ i
;
5248 /* Is this name a vdev in our pools? */
5249 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5250 ZFS_TYPE_POOL
, B_FALSE
, is_vdev
, cb
);
5257 cb
->cb_names
= tmp_name
;
5263 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5266 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5273 * Do we have a pool named *name? If so, return 1, otherwise 0.
5278 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, ZFS_TYPE_POOL
, B_FALSE
,
5282 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5284 are_all_pools(int argc
, char **argv
)
5286 if ((argc
== 0) || !*argv
)
5290 if (!is_pool(argv
[argc
]))
5297 * Helper function to print out vdev/pool names we can't resolve. Used for an
5301 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5307 for (i
= 0; i
< argc
; i
++) {
5311 str
= gettext("pool");
5312 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5313 str
= gettext("vdev in this pool");
5314 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5315 str
= gettext("vdev in another pool");
5317 str
= gettext("unknown");
5319 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5324 * Same as get_interval_count(), but with additional checks to not misinterpret
5325 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5326 * cb.cb_vdevs.cb_name_flags.
5329 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5330 unsigned long *count
, iostat_cbdata_t
*cb
)
5332 char **tmpargv
= argv
;
5333 int argc_for_interval
= 0;
5335 /* Is the last arg an interval value? Or a guid? */
5336 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
,
5339 * The last arg is not a guid, so it's probably an
5342 argc_for_interval
++;
5345 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
,
5348 * The 2nd to last arg is not a guid, so it's probably
5349 * an interval value.
5351 argc_for_interval
++;
5355 /* Point to our list of possible intervals */
5356 tmpargv
= &argv
[*argc
- argc_for_interval
];
5358 *argc
= *argc
- argc_for_interval
;
5359 get_interval_count(&argc_for_interval
, tmpargv
,
5364 * Floating point sleep(). Allows you to pass in a floating point value for
5370 struct timespec req
;
5371 req
.tv_sec
= floor(sec
);
5372 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5373 nanosleep(&req
, NULL
);
5377 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5378 * if we were unable to determine its size.
5381 terminal_height(void)
5385 if (isatty(STDOUT_FILENO
) == 0)
5388 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5389 return (win
.ws_row
);
5395 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5398 * name: Short name of the script ('iostat').
5399 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5402 print_zpool_script_help(char *name
, char *path
)
5404 char *argv
[] = {path
, "-h", NULL
};
5405 char **lines
= NULL
;
5409 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5411 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5413 libzfs_free_str_array(lines
, lines_cnt
);
5417 for (int i
= 0; i
< lines_cnt
; i
++)
5418 if (!is_blank_str(lines
[i
]))
5419 printf(" %-14s %s\n", name
, lines
[i
]);
5421 libzfs_free_str_array(lines
, lines_cnt
);
5425 * Go though the zpool status/iostat -c scripts in the user's path, run their
5426 * help option (-h), and print out the results.
5429 print_zpool_dir_scripts(char *dirpath
)
5433 char fullpath
[MAXPATHLEN
];
5434 struct stat dir_stat
;
5436 if ((dir
= opendir(dirpath
)) != NULL
) {
5437 /* print all the files and directories within directory */
5438 while ((ent
= readdir(dir
)) != NULL
) {
5439 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5441 /* Print the scripts */
5442 if (stat(fullpath
, &dir_stat
) == 0)
5443 if (dir_stat
.st_mode
& S_IXUSR
&&
5444 S_ISREG(dir_stat
.st_mode
))
5445 print_zpool_script_help(ent
->d_name
,
5453 * Print out help text for all zpool status/iostat -c scripts.
5456 print_zpool_script_list(char *subcommand
)
5458 char *dir
, *sp
, *tmp
;
5460 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5462 sp
= zpool_get_cmd_search_path();
5466 for (dir
= strtok_r(sp
, ":", &tmp
);
5468 dir
= strtok_r(NULL
, ":", &tmp
))
5469 print_zpool_dir_scripts(dir
);
5475 * Set the minimum pool/vdev name column width. The width must be at least 10,
5476 * but may be as large as the column width - 42 so it still fits on one line.
5477 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5480 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5482 iostat_cbdata_t
*cb
= data
;
5483 int width
, available_width
;
5486 * get_namewidth() returns the maximum width of any name in that column
5487 * for any pool/vdev/device line that will be output.
5489 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_vdevs
.cb_name_flags
,
5493 * The width we are calculating is the width of the header and also the
5494 * padding width for names that are less than maximum width. The stats
5495 * take up 42 characters, so the width available for names is:
5497 available_width
= get_columns() - 42;
5500 * If the maximum width fits on a screen, then great! Make everything
5501 * line up by justifying all lines to the same width. If that max
5502 * width is larger than what's available, the name plus stats won't fit
5503 * on one line, and justifying to that width would cause every line to
5504 * wrap on the screen. We only want lines with long names to wrap.
5505 * Limit the padding to what won't wrap.
5507 if (width
> available_width
)
5508 width
= available_width
;
5511 * And regardless of whatever the screen width is (get_columns can
5512 * return 0 if the width is not known or less than 42 for a narrow
5513 * terminal) have the width be a minimum of 10.
5518 /* Save the calculated width */
5519 cb
->cb_namewidth
= width
;
5525 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5526 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5527 * [interval [count]]
5529 * -c CMD For each vdev, run command CMD
5530 * -g Display guid for individual vdev name.
5531 * -L Follow links when resolving vdev path name.
5532 * -P Display full path for vdev name.
5533 * -v Display statistics for individual vdevs
5535 * -p Display values in parsable (exact) format.
5536 * -H Scripted mode. Don't display headers, and separate properties
5538 * -l Display average latency
5539 * -q Display queue depths
5540 * -w Display latency histograms
5541 * -r Display request size histogram
5542 * -T Display a timestamp in date(1) or Unix format
5543 * -n Only print headers once
5545 * This command can be tricky because we want to be able to deal with pool
5546 * creation/destruction as well as vdev configuration changes. The bulk of this
5547 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5548 * on pool_list_update() to detect the addition of new pools. Configuration
5549 * changes are all handled within libzfs.
5552 zpool_do_iostat(int argc
, char **argv
)
5558 unsigned long count
= 0;
5561 boolean_t verbose
= B_FALSE
;
5562 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5563 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5564 boolean_t omit_since_boot
= B_FALSE
;
5565 boolean_t guid
= B_FALSE
;
5566 boolean_t follow_links
= B_FALSE
;
5567 boolean_t full_name
= B_FALSE
;
5568 boolean_t headers_once
= B_FALSE
;
5569 iostat_cbdata_t cb
= { 0 };
5572 /* Used for printing error message */
5573 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5574 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5576 uint64_t unsupported_flags
;
5579 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5584 gettext("Can't set -c flag twice\n"));
5588 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5589 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5590 fprintf(stderr
, gettext(
5591 "Can't run -c, disabled by "
5592 "ZPOOL_SCRIPTS_ENABLED.\n"));
5596 if ((getuid() <= 0 || geteuid() <= 0) &&
5597 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5598 fprintf(stderr
, gettext(
5599 "Can't run -c with root privileges "
5600 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5610 follow_links
= B_TRUE
;
5616 get_timestamp_arg(*optarg
);
5640 omit_since_boot
= B_TRUE
;
5643 headers_once
= B_TRUE
;
5649 if (optopt
== 'c') {
5650 print_zpool_script_list("iostat");
5654 gettext("invalid option '%c'\n"), optopt
);
5663 cb
.cb_literal
= parsable
;
5664 cb
.cb_scripted
= scripted
;
5667 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_GUID
;
5669 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5671 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_PATH
;
5672 cb
.cb_iteration
= 0;
5673 cb
.cb_namewidth
= 0;
5674 cb
.cb_verbose
= verbose
;
5676 /* Get our interval and count values (if any) */
5678 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5681 get_interval_count(&argc
, argv
, &interval
, &count
);
5685 /* No args, so just print the defaults. */
5686 } else if (are_all_pools(argc
, argv
)) {
5687 /* All the args are pool names */
5688 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
5689 /* All the args are vdevs */
5690 cb
.cb_vdevs
.cb_names
= argv
;
5691 cb
.cb_vdevs
.cb_names_count
= argc
;
5692 argc
= 0; /* No pools to process */
5693 } else if (are_all_pools(1, argv
)) {
5694 /* The first arg is a pool name */
5695 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
5697 /* ...and the rest are vdev names */
5698 cb
.cb_vdevs
.cb_names
= argv
+ 1;
5699 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
5700 argc
= 1; /* One pool to process */
5702 fprintf(stderr
, gettext("Expected either a list of "));
5703 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5704 fprintf(stderr
, " \"%s\", ", argv
[0]);
5705 fprintf(stderr
, gettext("but got:\n"));
5706 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5707 argv
[0], &cb
.cb_vdevs
);
5708 fprintf(stderr
, "\n");
5714 * The args don't make sense. The first arg isn't a pool name,
5715 * nor are all the args vdevs.
5717 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5718 fprintf(stderr
, "\n");
5722 if (cb
.cb_vdevs
.cb_names_count
!= 0) {
5724 * If user specified vdevs, it implies verbose.
5726 cb
.cb_verbose
= B_TRUE
;
5730 * Construct the list of all interesting pools.
5733 if ((list
= pool_list_get(argc
, argv
, NULL
, ZFS_TYPE_POOL
, parsable
,
5737 if (pool_list_count(list
) == 0 && argc
!= 0) {
5738 pool_list_free(list
);
5742 if (pool_list_count(list
) == 0 && interval
== 0) {
5743 pool_list_free(list
);
5744 (void) fprintf(stderr
, gettext("no pools available\n"));
5748 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5749 pool_list_free(list
);
5750 (void) fprintf(stderr
,
5751 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5756 if (l_histo
&& rq_histo
) {
5757 pool_list_free(list
);
5758 (void) fprintf(stderr
,
5759 gettext("Only one of [-r|-w] can be passed at a time\n"));
5765 * Enter the main iostat loop.
5771 * Histograms tables look out of place when you try to display
5772 * them with the other stats, so make a rule that you can only
5773 * print histograms by themselves.
5775 cb
.cb_flags
= IOS_L_HISTO_M
;
5776 } else if (rq_histo
) {
5777 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5779 cb
.cb_flags
= IOS_DEFAULT_M
;
5781 cb
.cb_flags
|= IOS_LATENCY_M
;
5783 cb
.cb_flags
|= IOS_QUEUES_M
;
5787 * See if the module supports all the stats we want to display.
5789 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5790 if (unsupported_flags
) {
5794 gettext("The loaded zfs module doesn't support:"));
5796 /* for each bit set in unsupported_flags */
5797 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5798 idx
= lowbit64(f
) - 1;
5799 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5802 fprintf(stderr
, ". Try running a newer module.\n");
5803 pool_list_free(list
);
5809 if ((npools
= pool_list_count(list
)) == 0)
5810 (void) fprintf(stderr
, gettext("no pools available\n"));
5813 * If this is the first iteration and -y was supplied
5814 * we skip any printing.
5816 boolean_t skip
= (omit_since_boot
&&
5817 cb
.cb_iteration
== 0);
5820 * Refresh all statistics. This is done as an
5821 * explicit step before calculating the maximum name
5822 * width, so that any * configuration changes are
5823 * properly accounted for.
5825 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5829 * Iterate over all pools to determine the maximum width
5830 * for the pool / device name column across all pools.
5832 cb
.cb_namewidth
= 0;
5833 (void) pool_list_iter(list
, B_FALSE
,
5834 get_namewidth_iostat
, &cb
);
5836 if (timestamp_fmt
!= NODATE
)
5837 print_timestamp(timestamp_fmt
);
5839 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5840 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5841 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5842 argv
, cmd
, g_zfs
, cb
.cb_vdevs
.cb_names
,
5843 cb
.cb_vdevs
.cb_names_count
,
5844 cb
.cb_vdevs
.cb_name_flags
);
5851 * Check terminal size so we can print headers
5852 * even when terminal window has its height
5855 winheight
= terminal_height();
5857 * Are we connected to TTY? If not, headers_once
5858 * should be true, to avoid breaking scripts.
5861 headers_once
= B_TRUE
;
5864 * If it's the first time and we're not skipping it,
5865 * or either skip or verbose mode, print the header.
5867 * The histogram code explicitly prints its header on
5868 * every vdev, so skip this for histograms.
5870 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5871 (skip
!= verbose
) ||
5873 (cb
.cb_iteration
% winheight
) == 0)) &&
5874 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5876 print_iostat_header(&cb
);
5879 (void) fsleep(interval
);
5883 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5886 * If there's more than one pool, and we're not in
5887 * verbose mode (which prints a separator for us),
5888 * then print a separator.
5890 * In addition, if we're printing specific vdevs then
5891 * we also want an ending separator.
5893 if (((npools
> 1 && !verbose
&&
5894 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5895 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5896 cb
.cb_vdevs
.cb_names_count
)) &&
5898 print_iostat_separator(&cb
);
5899 if (cb
.vcdl
!= NULL
)
5900 print_cmd_columns(cb
.vcdl
, 1);
5904 if (cb
.vcdl
!= NULL
)
5905 free_vdev_cmd_data_list(cb
.vcdl
);
5910 * Flush the output so that redirection to a file isn't buffered
5913 (void) fflush(stdout
);
5918 if (count
!= 0 && --count
== 0)
5921 (void) fsleep(interval
);
5924 pool_list_free(list
);
5929 typedef struct list_cbdata
{
5930 boolean_t cb_verbose
;
5933 boolean_t cb_scripted
;
5934 zprop_list_t
*cb_proplist
;
5935 boolean_t cb_literal
;
5940 * Given a list of columns to display, output appropriate headers for each one.
5943 print_header(list_cbdata_t
*cb
)
5945 zprop_list_t
*pl
= cb
->cb_proplist
;
5946 char headerbuf
[ZPOOL_MAXPROPLEN
];
5948 boolean_t first
= B_TRUE
;
5949 boolean_t right_justify
;
5952 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5953 width
= pl
->pl_width
;
5954 if (first
&& cb
->cb_verbose
) {
5956 * Reset the width to accommodate the verbose listing
5959 width
= cb
->cb_namewidth
;
5963 (void) fputs(" ", stdout
);
5967 right_justify
= B_FALSE
;
5968 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5969 header
= zpool_prop_column_name(pl
->pl_prop
);
5970 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5974 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5975 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5976 headerbuf
[i
] = '\0';
5980 if (pl
->pl_next
== NULL
&& !right_justify
)
5981 (void) fputs(header
, stdout
);
5982 else if (right_justify
)
5983 (void) printf("%*s", (int)width
, header
);
5985 (void) printf("%-*s", (int)width
, header
);
5988 (void) fputc('\n', stdout
);
5992 * Given a pool and a list of properties, print out all the properties according
5993 * to the described layout. Used by zpool_do_list().
5996 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5998 zprop_list_t
*pl
= cb
->cb_proplist
;
5999 boolean_t first
= B_TRUE
;
6000 char property
[ZPOOL_MAXPROPLEN
];
6002 boolean_t right_justify
;
6005 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6007 width
= pl
->pl_width
;
6008 if (first
&& cb
->cb_verbose
) {
6010 * Reset the width to accommodate the verbose listing
6013 width
= cb
->cb_namewidth
;
6017 if (cb
->cb_scripted
)
6018 (void) fputc('\t', stdout
);
6020 (void) fputs(" ", stdout
);
6025 right_justify
= B_FALSE
;
6026 if (pl
->pl_prop
!= ZPROP_INVAL
) {
6027 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
6028 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
6033 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6034 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
6035 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
6036 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
6037 sizeof (property
)) == 0) {
6045 * If this is being called in scripted mode, or if this is the
6046 * last column and it is left-justified, don't include a width
6049 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
6050 (void) fputs(propstr
, stdout
);
6051 else if (right_justify
)
6052 (void) printf("%*s", (int)width
, propstr
);
6054 (void) printf("%-*s", (int)width
, propstr
);
6057 (void) fputc('\n', stdout
);
6061 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
6062 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
6066 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6069 case ZPOOL_PROP_SIZE
:
6070 case ZPOOL_PROP_EXPANDSZ
:
6071 case ZPOOL_PROP_CHECKPOINT
:
6072 case ZPOOL_PROP_DEDUPRATIO
:
6074 (void) strlcpy(propval
, "-", sizeof (propval
));
6076 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6079 case ZPOOL_PROP_FRAGMENTATION
:
6080 if (value
== ZFS_FRAG_INVALID
) {
6081 (void) strlcpy(propval
, "-", sizeof (propval
));
6082 } else if (format
== ZFS_NICENUM_RAW
) {
6083 (void) snprintf(propval
, sizeof (propval
), "%llu",
6084 (unsigned long long)value
);
6086 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6087 (unsigned long long)value
);
6090 case ZPOOL_PROP_CAPACITY
:
6091 /* capacity value is in parts-per-10,000 (aka permyriad) */
6092 if (format
== ZFS_NICENUM_RAW
)
6093 (void) snprintf(propval
, sizeof (propval
), "%llu",
6094 (unsigned long long)value
/ 100);
6096 (void) snprintf(propval
, sizeof (propval
),
6097 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6098 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6100 case ZPOOL_PROP_HEALTH
:
6102 (void) strlcpy(propval
, str
, sizeof (propval
));
6105 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6109 (void) strlcpy(propval
, "-", sizeof (propval
));
6112 (void) printf("\t%s", propval
);
6114 (void) printf(" %*s", (int)width
, propval
);
6118 * print static default line per vdev
6119 * not compatible with '-o' <proplist> option
6122 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6123 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
6129 boolean_t scripted
= cb
->cb_scripted
;
6130 uint64_t islog
= B_FALSE
;
6131 const char *dashes
= "%-*s - - - - "
6134 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
6135 (uint64_t **)&vs
, &c
) == 0);
6138 boolean_t toplevel
= (vs
->vs_space
!= 0);
6140 enum zfs_nicenum_format format
;
6144 format
= ZFS_NICENUM_RAW
;
6146 format
= ZFS_NICENUM_1024
;
6148 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
6152 (void) printf("\t%s", name
);
6153 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
6154 (void) printf("%*s%s", depth
, "", name
);
6156 (void) printf("%*s%s%*s", depth
, "", name
,
6157 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
6160 * Print the properties for the individual vdevs. Some
6161 * properties are only applicable to toplevel vdevs. The
6162 * 'toplevel' boolean value is passed to the print_one_column()
6163 * to indicate that the value is valid.
6166 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_pspace
, NULL
,
6167 scripted
, B_TRUE
, format
);
6169 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
,
6170 scripted
, toplevel
, format
);
6171 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6172 scripted
, toplevel
, format
);
6173 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6174 NULL
, scripted
, toplevel
, format
);
6175 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6176 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6177 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6178 scripted
, B_TRUE
, format
);
6179 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6180 vs
->vs_fragmentation
, NULL
, scripted
,
6181 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6183 cap
= (vs
->vs_space
== 0) ? 0 :
6184 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6185 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6186 scripted
, toplevel
, format
);
6187 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6188 scripted
, toplevel
, format
);
6189 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6191 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6193 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6196 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6198 (void) fputc('\n', stdout
);
6201 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6202 &child
, &children
) != 0)
6205 /* list the normal vdevs first */
6206 for (c
= 0; c
< children
; c
++) {
6207 uint64_t ishole
= B_FALSE
;
6209 if (nvlist_lookup_uint64(child
[c
],
6210 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6213 if (nvlist_lookup_uint64(child
[c
],
6214 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6217 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6220 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6221 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6222 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6226 /* list the classes: 'logs', 'dedup', and 'special' */
6227 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
6228 boolean_t printed
= B_FALSE
;
6230 for (c
= 0; c
< children
; c
++) {
6234 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6235 &islog
) == 0 && islog
) {
6236 bias
= VDEV_ALLOC_CLASS_LOGS
;
6238 (void) nvlist_lookup_string(child
[c
],
6239 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6240 (void) nvlist_lookup_string(child
[c
],
6241 ZPOOL_CONFIG_TYPE
, &type
);
6243 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6245 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6249 /* LINTED E_SEC_PRINTF_VAR_FMT */
6250 (void) printf(dashes
, cb
->cb_namewidth
,
6254 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6255 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6256 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6262 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6263 &child
, &children
) == 0 && children
> 0) {
6264 /* LINTED E_SEC_PRINTF_VAR_FMT */
6265 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6266 for (c
= 0; c
< children
; c
++) {
6267 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6269 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6275 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6276 &children
) == 0 && children
> 0) {
6277 /* LINTED E_SEC_PRINTF_VAR_FMT */
6278 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6279 for (c
= 0; c
< children
; c
++) {
6280 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6282 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6290 * Generic callback function to list a pool.
6293 list_callback(zpool_handle_t
*zhp
, void *data
)
6295 list_cbdata_t
*cbp
= data
;
6297 print_pool(zhp
, cbp
);
6299 if (cbp
->cb_verbose
) {
6300 nvlist_t
*config
, *nvroot
;
6302 config
= zpool_get_config(zhp
, NULL
);
6303 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6305 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6312 * Set the minimum pool/vdev name column width. The width must be at least 9,
6313 * but may be as large as needed.
6316 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6318 list_cbdata_t
*cb
= data
;
6321 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6327 cb
->cb_namewidth
= width
;
6333 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6335 * -g Display guid for individual vdev name.
6336 * -H Scripted mode. Don't display headers, and separate properties
6338 * -L Follow links when resolving vdev path name.
6339 * -o List of properties to display. Defaults to
6340 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6341 * "dedupratio,health,altroot"
6342 * -p Display values in parsable (exact) format.
6343 * -P Display full path for vdev name.
6344 * -T Display a timestamp in date(1) or Unix format
6346 * List all pools in the system, whether or not they're healthy. Output space
6347 * statistics for each one, as well as health status summary.
6350 zpool_do_list(int argc
, char **argv
)
6354 list_cbdata_t cb
= { 0 };
6355 static char default_props
[] =
6356 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6357 "capacity,dedupratio,health,altroot";
6358 char *props
= default_props
;
6360 unsigned long count
= 0;
6362 boolean_t first
= B_TRUE
;
6363 current_prop_type
= ZFS_TYPE_POOL
;
6366 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6369 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6372 cb
.cb_scripted
= B_TRUE
;
6375 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6381 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6384 cb
.cb_literal
= B_TRUE
;
6387 get_timestamp_arg(*optarg
);
6390 cb
.cb_verbose
= B_TRUE
;
6391 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6394 (void) fprintf(stderr
, gettext("missing argument for "
6395 "'%c' option\n"), optopt
);
6399 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6408 get_interval_count(&argc
, argv
, &interval
, &count
);
6410 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6414 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6415 ZFS_TYPE_POOL
, cb
.cb_literal
, &ret
)) == NULL
)
6418 if (pool_list_count(list
) == 0)
6421 cb
.cb_namewidth
= 0;
6422 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6424 if (timestamp_fmt
!= NODATE
)
6425 print_timestamp(timestamp_fmt
);
6427 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6431 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6436 if (count
!= 0 && --count
== 0)
6439 pool_list_free(list
);
6440 (void) fsleep(interval
);
6443 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6444 (void) printf(gettext("no pools available\n"));
6448 pool_list_free(list
);
6449 zprop_free_list(cb
.cb_proplist
);
6454 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6456 boolean_t force
= B_FALSE
;
6457 boolean_t rebuild
= B_FALSE
;
6458 boolean_t wait
= B_FALSE
;
6461 char *poolname
, *old_disk
, *new_disk
;
6462 zpool_handle_t
*zhp
;
6463 nvlist_t
*props
= NULL
;
6468 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6474 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6475 (void) fprintf(stderr
, gettext("missing "
6476 "'=' for -o option\n"));
6482 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6483 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6493 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6502 /* get pool name and check number of arguments */
6504 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6511 (void) fprintf(stderr
,
6512 gettext("missing <device> specification\n"));
6520 (void) fprintf(stderr
,
6521 gettext("missing <new_device> specification\n"));
6524 new_disk
= old_disk
;
6534 (void) fprintf(stderr
, gettext("too many arguments\n"));
6538 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6543 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6544 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6551 /* unless manually specified use "ashift" pool property (if set) */
6552 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6555 char strval
[ZPOOL_MAXPROPLEN
];
6557 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6558 if (src
!= ZPROP_SRC_DEFAULT
) {
6559 (void) sprintf(strval
, "%" PRId32
, intval
);
6560 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6561 &props
, B_TRUE
) == 0);
6565 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6567 if (nvroot
== NULL
) {
6573 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6576 if (ret
== 0 && wait
)
6577 ret
= zpool_wait(zhp
,
6578 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6581 nvlist_free(nvroot
);
6588 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6590 * -f Force attach, even if <new_device> appears to be in use.
6591 * -s Use sequential instead of healing reconstruction for resilver.
6592 * -o Set property=value.
6593 * -w Wait for replacing to complete before returning
6595 * Replace <device> with <new_device>.
6598 zpool_do_replace(int argc
, char **argv
)
6600 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6604 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6606 * -f Force attach, even if <new_device> appears to be in use.
6607 * -s Use sequential instead of healing reconstruction for resilver.
6608 * -o Set property=value.
6609 * -w Wait for resilvering to complete before returning
6611 * Attach <new_device> to the mirror containing <device>. If <device> is not
6612 * part of a mirror, then <device> will be transformed into a mirror of
6613 * <device> and <new_device>. In either case, <new_device> will begin life
6614 * with a DTL of [0, now], and will immediately begin to resilver itself.
6617 zpool_do_attach(int argc
, char **argv
)
6619 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6623 * zpool detach [-f] <pool> <device>
6625 * -f Force detach of <device>, even if DTLs argue against it
6626 * (not supported yet)
6628 * Detach a device from a mirror. The operation will be refused if <device>
6629 * is the last device in the mirror, or if the DTLs indicate that this device
6630 * has the only valid copy of some data.
6633 zpool_do_detach(int argc
, char **argv
)
6636 char *poolname
, *path
;
6637 zpool_handle_t
*zhp
;
6641 while ((c
= getopt(argc
, argv
, "")) != -1) {
6644 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6653 /* get pool name and check number of arguments */
6655 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6660 (void) fprintf(stderr
,
6661 gettext("missing <device> specification\n"));
6668 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6671 ret
= zpool_vdev_detach(zhp
, path
);
6679 * zpool split [-gLnP] [-o prop=val] ...
6681 * [-R altroot] <pool> <newpool> [<device> ...]
6683 * -g Display guid for individual vdev name.
6684 * -L Follow links when resolving vdev path name.
6685 * -n Do not split the pool, but display the resulting layout if
6686 * it were to be split.
6687 * -o Set property=value, or set mount options.
6688 * -P Display full path for vdev name.
6689 * -R Mount the split-off pool under an alternate root.
6690 * -l Load encryption keys while importing.
6692 * Splits the named pool and gives it the new pool name. Devices to be split
6693 * off may be listed, provided that no more than one device is specified
6694 * per top-level vdev mirror. The newly split pool is left in an exported
6695 * state unless -R is specified.
6697 * Restrictions: the top-level of the pool pool must only be made up of
6698 * mirrors; all devices in the pool must be healthy; no device may be
6699 * undergoing a resilvering operation.
6702 zpool_do_split(int argc
, char **argv
)
6704 char *srcpool
, *newpool
, *propval
;
6705 char *mntopts
= NULL
;
6708 boolean_t loadkeys
= B_FALSE
;
6709 zpool_handle_t
*zhp
;
6710 nvlist_t
*config
, *props
= NULL
;
6712 flags
.dryrun
= B_FALSE
;
6713 flags
.import
= B_FALSE
;
6714 flags
.name_flags
= 0;
6717 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6720 flags
.name_flags
|= VDEV_NAME_GUID
;
6723 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6726 flags
.import
= B_TRUE
;
6728 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6729 &props
, B_TRUE
) != 0) {
6738 flags
.dryrun
= B_TRUE
;
6741 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6744 if (add_prop_list(optarg
, propval
,
6745 &props
, B_TRUE
) != 0) {
6754 flags
.name_flags
|= VDEV_NAME_PATH
;
6757 (void) fprintf(stderr
, gettext("missing argument for "
6758 "'%c' option\n"), optopt
);
6762 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6769 if (!flags
.import
&& mntopts
!= NULL
) {
6770 (void) fprintf(stderr
, gettext("setting mntopts is only "
6771 "valid when importing the pool\n"));
6775 if (!flags
.import
&& loadkeys
) {
6776 (void) fprintf(stderr
, gettext("loading keys is only "
6777 "valid when importing the pool\n"));
6785 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6789 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6799 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6804 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6805 if (config
== NULL
) {
6809 (void) printf(gettext("would create '%s' with the "
6810 "following layout:\n\n"), newpool
);
6811 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6813 print_vdev_tree(NULL
, "dedup", config
, 0,
6814 VDEV_ALLOC_BIAS_DEDUP
, 0);
6815 print_vdev_tree(NULL
, "special", config
, 0,
6816 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6822 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6823 nvlist_free(config
);
6829 * The split was successful. Now we need to open the new
6830 * pool and import it.
6832 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6833 nvlist_free(config
);
6839 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6844 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6845 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6847 (void) fprintf(stderr
, gettext("Split was successful, but "
6848 "the datasets could not all be mounted\n"));
6849 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6850 "different altroot\n"), "zpool import");
6853 nvlist_free(config
);
6862 * zpool online <pool> <device> ...
6865 zpool_do_online(int argc
, char **argv
)
6869 zpool_handle_t
*zhp
;
6871 vdev_state_t newstate
;
6875 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6878 flags
|= ZFS_ONLINE_EXPAND
;
6881 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6890 /* get pool name and check number of arguments */
6892 (void) fprintf(stderr
, gettext("missing pool name\n"));
6896 (void) fprintf(stderr
, gettext("missing device name\n"));
6902 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6905 for (i
= 1; i
< argc
; i
++) {
6906 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6907 if (newstate
!= VDEV_STATE_HEALTHY
) {
6908 (void) printf(gettext("warning: device '%s' "
6909 "onlined, but remains in faulted state\n"),
6911 if (newstate
== VDEV_STATE_FAULTED
)
6912 (void) printf(gettext("use 'zpool "
6913 "clear' to restore a faulted "
6916 (void) printf(gettext("use 'zpool "
6917 "replace' to replace devices "
6918 "that are no longer present\n"));
6931 * zpool offline [-ft] <pool> <device> ...
6933 * -f Force the device into a faulted state.
6935 * -t Only take the device off-line temporarily. The offline/faulted
6936 * state will not be persistent across reboots.
6939 zpool_do_offline(int argc
, char **argv
)
6943 zpool_handle_t
*zhp
;
6945 boolean_t istmp
= B_FALSE
;
6946 boolean_t fault
= B_FALSE
;
6949 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6958 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6967 /* get pool name and check number of arguments */
6969 (void) fprintf(stderr
, gettext("missing pool name\n"));
6973 (void) fprintf(stderr
, gettext("missing device name\n"));
6979 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6982 for (i
= 1; i
< argc
; i
++) {
6984 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6986 if (istmp
== B_FALSE
) {
6987 /* Force the fault to persist across imports */
6988 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6990 aux
= VDEV_AUX_EXTERNAL
;
6993 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6996 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
7007 * zpool clear <pool> [device]
7009 * Clear all errors associated with a pool or a particular device.
7012 zpool_do_clear(int argc
, char **argv
)
7016 boolean_t dryrun
= B_FALSE
;
7017 boolean_t do_rewind
= B_FALSE
;
7018 boolean_t xtreme_rewind
= B_FALSE
;
7019 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
7020 nvlist_t
*policy
= NULL
;
7021 zpool_handle_t
*zhp
;
7022 char *pool
, *device
;
7025 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
7034 xtreme_rewind
= B_TRUE
;
7037 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7047 (void) fprintf(stderr
, gettext("missing pool name\n"));
7052 (void) fprintf(stderr
, gettext("too many arguments\n"));
7056 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
7057 (void) fprintf(stderr
,
7058 gettext("-n or -X only meaningful with -F\n"));
7062 rewind_policy
= ZPOOL_TRY_REWIND
;
7064 rewind_policy
= ZPOOL_DO_REWIND
;
7066 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
7068 /* In future, further rewind policy choices can be passed along here */
7069 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
7070 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
7071 rewind_policy
) != 0) {
7076 device
= argc
== 2 ? argv
[1] : NULL
;
7078 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
7079 nvlist_free(policy
);
7083 if (zpool_clear(zhp
, device
, policy
) != 0)
7088 nvlist_free(policy
);
7094 * zpool reguid <pool>
7097 zpool_do_reguid(int argc
, char **argv
)
7101 zpool_handle_t
*zhp
;
7105 while ((c
= getopt(argc
, argv
, "")) != -1) {
7108 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7117 /* get pool name and check number of arguments */
7119 (void) fprintf(stderr
, gettext("missing pool name\n"));
7124 (void) fprintf(stderr
, gettext("too many arguments\n"));
7129 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7132 ret
= zpool_reguid(zhp
);
7140 * zpool reopen <pool>
7142 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7145 zpool_do_reopen(int argc
, char **argv
)
7149 boolean_t scrub_restart
= B_TRUE
;
7152 while ((c
= getopt(argc
, argv
, "n")) != -1) {
7155 scrub_restart
= B_FALSE
;
7158 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7167 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7168 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7169 B_FALSE
, zpool_reopen_one
, &scrub_restart
);
7174 typedef struct scrub_cbdata
{
7176 pool_scrub_cmd_t cb_scrub_cmd
;
7180 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7182 nvlist_t
*config
, *nvroot
;
7184 config
= zpool_get_config(zhp
, NULL
);
7186 if (config
!= NULL
) {
7187 pool_checkpoint_stat_t
*pcs
= NULL
;
7190 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7191 (void) nvlist_lookup_uint64_array(nvroot
,
7192 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7194 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7197 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7198 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7206 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7208 scrub_cbdata_t
*cb
= data
;
7212 * Ignore faulted pools.
7214 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7215 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7216 "currently unavailable\n"), zpool_get_name(zhp
));
7220 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7222 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7223 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7224 (void) printf(gettext("warning: will not scrub state that "
7225 "belongs to the checkpoint of pool '%s'\n"),
7226 zpool_get_name(zhp
));
7233 wait_callback(zpool_handle_t
*zhp
, void *data
)
7235 zpool_wait_activity_t
*act
= data
;
7236 return (zpool_wait(zhp
, *act
));
7240 * zpool scrub [-s | -p] [-w] <pool> ...
7242 * -s Stop. Stops any in-progress scrub.
7243 * -p Pause. Pause in-progress scrub.
7244 * -w Wait. Blocks until scrub has completed.
7247 zpool_do_scrub(int argc
, char **argv
)
7251 boolean_t wait
= B_FALSE
;
7254 cb
.cb_type
= POOL_SCAN_SCRUB
;
7255 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7258 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7261 cb
.cb_type
= POOL_SCAN_NONE
;
7264 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7270 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7276 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7277 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7278 (void) fprintf(stderr
, gettext("invalid option combination: "
7279 "-s and -p are mutually exclusive\n"));
7283 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7284 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7285 (void) fprintf(stderr
, gettext("invalid option combination: "
7286 "-w cannot be used with -p or -s\n"));
7294 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7298 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7299 B_FALSE
, scrub_callback
, &cb
);
7301 if (wait
&& !error
) {
7302 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7303 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7304 B_FALSE
, wait_callback
, &act
);
7311 * zpool resilver <pool> ...
7313 * Restarts any in-progress resilver
7316 zpool_do_resilver(int argc
, char **argv
)
7321 cb
.cb_type
= POOL_SCAN_RESILVER
;
7322 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7325 while ((c
= getopt(argc
, argv
, "")) != -1) {
7328 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7338 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7342 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7343 B_FALSE
, scrub_callback
, &cb
));
7347 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7349 * -c Cancel. Ends any in-progress trim.
7350 * -d Secure trim. Requires kernel and device support.
7351 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7352 * adding a multiplier suffix such as 'k' or 'm'.
7353 * -s Suspend. TRIM can then be restarted with no flags.
7354 * -w Wait. Blocks until trimming has completed.
7357 zpool_do_trim(int argc
, char **argv
)
7359 struct option long_options
[] = {
7360 {"cancel", no_argument
, NULL
, 'c'},
7361 {"secure", no_argument
, NULL
, 'd'},
7362 {"rate", required_argument
, NULL
, 'r'},
7363 {"suspend", no_argument
, NULL
, 's'},
7364 {"wait", no_argument
, NULL
, 'w'},
7368 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7370 boolean_t secure
= B_FALSE
;
7371 boolean_t wait
= B_FALSE
;
7374 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7378 if (cmd_type
!= POOL_TRIM_START
&&
7379 cmd_type
!= POOL_TRIM_CANCEL
) {
7380 (void) fprintf(stderr
, gettext("-c cannot be "
7381 "combined with other options\n"));
7384 cmd_type
= POOL_TRIM_CANCEL
;
7387 if (cmd_type
!= POOL_TRIM_START
) {
7388 (void) fprintf(stderr
, gettext("-d cannot be "
7389 "combined with the -c or -s options\n"));
7395 if (cmd_type
!= POOL_TRIM_START
) {
7396 (void) fprintf(stderr
, gettext("-r cannot be "
7397 "combined with the -c or -s options\n"));
7400 if (zfs_nicestrtonum(g_zfs
, optarg
, &rate
) == -1) {
7401 (void) fprintf(stderr
, "%s: %s\n",
7402 gettext("invalid value for rate"),
7403 libzfs_error_description(g_zfs
));
7408 if (cmd_type
!= POOL_TRIM_START
&&
7409 cmd_type
!= POOL_TRIM_SUSPEND
) {
7410 (void) fprintf(stderr
, gettext("-s cannot be "
7411 "combined with other options\n"));
7414 cmd_type
= POOL_TRIM_SUSPEND
;
7421 (void) fprintf(stderr
,
7422 gettext("invalid option '%c'\n"), optopt
);
7424 (void) fprintf(stderr
,
7425 gettext("invalid option '%s'\n"),
7436 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7441 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7442 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7447 char *poolname
= argv
[0];
7448 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7452 trimflags_t trim_flags
= {
7458 nvlist_t
*vdevs
= fnvlist_alloc();
7460 /* no individual leaf vdevs specified, so add them all */
7461 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7462 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7463 ZPOOL_CONFIG_VDEV_TREE
);
7464 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7465 trim_flags
.fullpool
= B_TRUE
;
7467 trim_flags
.fullpool
= B_FALSE
;
7468 for (int i
= 1; i
< argc
; i
++) {
7469 fnvlist_add_boolean(vdevs
, argv
[i
]);
7473 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7475 fnvlist_free(vdevs
);
7482 * Converts a total number of seconds to a human readable string broken
7483 * down in to days/hours/minutes/seconds.
7486 secs_to_dhms(uint64_t total
, char *buf
)
7488 uint64_t days
= total
/ 60 / 60 / 24;
7489 uint64_t hours
= (total
/ 60 / 60) % 24;
7490 uint64_t mins
= (total
/ 60) % 60;
7491 uint64_t secs
= (total
% 60);
7494 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7495 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7496 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7498 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7499 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7500 (u_longlong_t
)secs
);
7505 * Print out detailed scrub status.
7508 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7510 time_t start
, end
, pause
;
7511 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7512 uint64_t elapsed
, scan_rate
, issue_rate
;
7513 double fraction_done
;
7514 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7515 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7518 printf_color(ANSI_BOLD
, gettext("scan:"));
7521 /* If there's never been a scan, there's not much to say. */
7522 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7523 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7524 (void) printf(gettext("none requested\n"));
7528 start
= ps
->pss_start_time
;
7529 end
= ps
->pss_end_time
;
7530 pause
= ps
->pss_pass_scrub_pause
;
7532 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7534 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7535 ps
->pss_func
== POOL_SCAN_RESILVER
);
7537 /* Scan is finished or canceled. */
7538 if (ps
->pss_state
== DSS_FINISHED
) {
7539 secs_to_dhms(end
- start
, time_buf
);
7541 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7542 (void) printf(gettext("scrub repaired %s "
7543 "in %s with %llu errors on %s"), processed_buf
,
7544 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7546 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7547 (void) printf(gettext("resilvered %s "
7548 "in %s with %llu errors on %s"), processed_buf
,
7549 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7553 } else if (ps
->pss_state
== DSS_CANCELED
) {
7554 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7555 (void) printf(gettext("scrub canceled on %s"),
7557 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7558 (void) printf(gettext("resilver canceled on %s"),
7564 assert(ps
->pss_state
== DSS_SCANNING
);
7566 /* Scan is in progress. Resilvers can't be paused. */
7567 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7569 (void) printf(gettext("scrub in progress since %s"),
7572 (void) printf(gettext("scrub paused since %s"),
7574 (void) printf(gettext("\tscrub started on %s"),
7577 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7578 (void) printf(gettext("resilver in progress since %s"),
7582 scanned
= ps
->pss_examined
;
7583 pass_scanned
= ps
->pss_pass_exam
;
7584 issued
= ps
->pss_issued
;
7585 pass_issued
= ps
->pss_pass_issued
;
7586 total
= ps
->pss_to_examine
;
7588 /* we are only done with a block once we have issued the IO for it */
7589 fraction_done
= (double)issued
/ total
;
7591 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7592 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7593 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7594 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7596 scan_rate
= pass_scanned
/ elapsed
;
7597 issue_rate
= pass_issued
/ elapsed
;
7598 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7599 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7600 secs_to_dhms(total_secs_left
, time_buf
);
7602 /* format all of the numbers we will be reporting */
7603 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7604 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7605 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7606 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7607 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7609 /* do not print estimated time if we have a paused scrub */
7611 (void) printf(gettext("\t%s scanned at %s/s, "
7612 "%s issued at %s/s, %s total\n"),
7613 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7615 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7616 scanned_buf
, issued_buf
, total_buf
);
7619 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7620 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7621 processed_buf
, 100 * fraction_done
);
7622 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7623 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7624 processed_buf
, 100 * fraction_done
);
7628 if (total_secs_left
!= UINT64_MAX
&&
7629 issue_rate
>= 10 * 1024 * 1024) {
7630 (void) printf(gettext(", %s to go\n"), time_buf
);
7632 (void) printf(gettext(", no estimated "
7633 "completion time\n"));
7636 (void) printf(gettext("\n"));
7641 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7643 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7647 printf_color(ANSI_BOLD
, gettext("scan:"));
7650 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7651 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7652 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7653 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7654 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7655 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7656 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7657 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7658 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7659 (bytes_est
+ 1), 100);
7661 /* Format all of the numbers we will be reporting */
7662 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7663 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7664 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7665 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7666 sizeof (bytes_scanned_buf
));
7667 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7668 sizeof (bytes_issued_buf
));
7669 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7670 sizeof (bytes_rebuilt_buf
));
7671 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7672 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7673 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7675 time_t start
= vrs
->vrs_start_time
;
7676 time_t end
= vrs
->vrs_end_time
;
7678 /* Rebuild is finished or canceled. */
7679 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7680 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7681 (void) printf(gettext("resilvered (%s) %s in %s "
7682 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7683 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7685 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7686 (void) printf(gettext("resilver (%s) canceled on %s"),
7687 vdev_name
, ctime(&end
));
7689 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7690 (void) printf(gettext("resilver (%s) in progress since %s"),
7691 vdev_name
, ctime(&start
));
7694 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7696 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7697 MAX(scan_rate
, 1), time_buf
);
7699 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7700 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7701 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7702 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7703 bytes_rebuilt_buf
, scan_pct
);
7705 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7706 if (scan_rate
>= 10 * 1024 * 1024) {
7707 (void) printf(gettext(", %s to go\n"), time_buf
);
7709 (void) printf(gettext(", no estimated "
7710 "completion time\n"));
7713 (void) printf(gettext("\n"));
7718 * Print rebuild status for top-level vdevs.
7721 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7726 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7727 &child
, &children
) != 0)
7730 for (uint_t c
= 0; c
< children
; c
++) {
7731 vdev_rebuild_stat_t
*vrs
;
7734 if (nvlist_lookup_uint64_array(child
[c
],
7735 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7736 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7737 child
[c
], VDEV_NAME_TYPE_ID
);
7738 print_rebuild_status_impl(vrs
, name
);
7745 * As we don't scrub checkpointed blocks, we want to warn the user that we
7746 * skipped scanning some blocks if a checkpoint exists or existed at any
7747 * time during the scan. If a sequential instead of healing reconstruction
7748 * was performed then the blocks were reconstructed. However, their checksums
7749 * have not been verified so we still print the warning.
7752 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7754 if (ps
== NULL
|| pcs
== NULL
)
7757 if (pcs
->pcs_state
== CS_NONE
||
7758 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7761 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7763 if (ps
->pss_state
== DSS_NONE
)
7766 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7767 ps
->pss_end_time
< pcs
->pcs_start_time
)
7770 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7771 (void) printf(gettext(" scan warning: skipped blocks "
7772 "that are only referenced by the checkpoint.\n"));
7774 assert(ps
->pss_state
== DSS_SCANNING
);
7775 (void) printf(gettext(" scan warning: skipping blocks "
7776 "that are only referenced by the checkpoint.\n"));
7781 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7782 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7783 * the last completed (or cancelled) rebuild.
7786 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7790 boolean_t rebuilding
= B_FALSE
;
7791 uint64_t end_time
= 0;
7793 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7794 &child
, &children
) != 0)
7797 for (uint_t c
= 0; c
< children
; c
++) {
7798 vdev_rebuild_stat_t
*vrs
;
7801 if (nvlist_lookup_uint64_array(child
[c
],
7802 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7804 if (vrs
->vrs_end_time
> end_time
)
7805 end_time
= vrs
->vrs_end_time
;
7807 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7808 rebuilding
= B_TRUE
;
7815 if (rebuild_end_time
!= NULL
)
7816 *rebuild_end_time
= end_time
;
7818 return (rebuilding
);
7822 * Print the scan status.
7825 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7827 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7828 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7829 boolean_t active_resilver
= B_FALSE
;
7830 pool_checkpoint_stat_t
*pcs
= NULL
;
7831 pool_scan_stat_t
*ps
= NULL
;
7834 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7835 (uint64_t **)&ps
, &c
) == 0) {
7836 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7837 resilver_end_time
= ps
->pss_end_time
;
7838 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7841 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7842 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7845 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7846 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7848 /* Always print the scrub status when available. */
7850 print_scan_scrub_resilver_status(ps
);
7853 * When there is an active resilver or rebuild print its status.
7854 * Otherwise print the status of the last resilver or rebuild.
7856 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7857 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7858 print_scan_scrub_resilver_status(ps
);
7859 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7860 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7861 print_rebuild_status(zhp
, nvroot
);
7864 (void) nvlist_lookup_uint64_array(nvroot
,
7865 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7866 print_checkpoint_scan_warning(ps
, pcs
);
7870 * Print out detailed removal status.
7873 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7875 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7877 nvlist_t
*config
, *nvroot
;
7882 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7886 * Determine name of vdev.
7888 config
= zpool_get_config(zhp
, NULL
);
7889 nvroot
= fnvlist_lookup_nvlist(config
,
7890 ZPOOL_CONFIG_VDEV_TREE
);
7891 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7892 &child
, &children
) == 0);
7893 assert(prs
->prs_removing_vdev
< children
);
7894 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7895 child
[prs
->prs_removing_vdev
], B_TRUE
);
7897 printf_color(ANSI_BOLD
, gettext("remove: "));
7899 start
= prs
->prs_start_time
;
7900 end
= prs
->prs_end_time
;
7901 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7904 * Removal is finished or canceled.
7906 if (prs
->prs_state
== DSS_FINISHED
) {
7907 uint64_t minutes_taken
= (end
- start
) / 60;
7909 (void) printf(gettext("Removal of vdev %llu copied %s "
7910 "in %lluh%um, completed on %s"),
7911 (longlong_t
)prs
->prs_removing_vdev
,
7913 (u_longlong_t
)(minutes_taken
/ 60),
7914 (uint_t
)(minutes_taken
% 60),
7915 ctime((time_t *)&end
));
7916 } else if (prs
->prs_state
== DSS_CANCELED
) {
7917 (void) printf(gettext("Removal of %s canceled on %s"),
7918 vdev_name
, ctime(&end
));
7920 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7921 double fraction_done
;
7924 assert(prs
->prs_state
== DSS_SCANNING
);
7927 * Removal is in progress.
7929 (void) printf(gettext(
7930 "Evacuation of %s in progress since %s"),
7931 vdev_name
, ctime(&start
));
7933 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7934 total
= prs
->prs_to_copy
;
7935 fraction_done
= (double)copied
/ total
;
7937 /* elapsed time for this pass */
7938 elapsed
= time(NULL
) - prs
->prs_start_time
;
7939 elapsed
= elapsed
> 0 ? elapsed
: 1;
7940 rate
= copied
/ elapsed
;
7941 rate
= rate
> 0 ? rate
: 1;
7942 mins_left
= ((total
- copied
) / rate
) / 60;
7943 hours_left
= mins_left
/ 60;
7945 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7946 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7947 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7950 * do not print estimated time if hours_left is more than
7953 (void) printf(gettext(
7954 "\t%s copied out of %s at %s/s, %.2f%% done"),
7955 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7956 if (hours_left
< (30 * 24)) {
7957 (void) printf(gettext(", %lluh%um to go\n"),
7958 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7960 (void) printf(gettext(
7961 ", (copy is slow, no estimated time)\n"));
7966 if (prs
->prs_mapping_memory
> 0) {
7968 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7969 (void) printf(gettext(
7970 "\t%s memory used for removed device mappings\n"),
7976 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7981 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7984 (void) printf(gettext("checkpoint: "));
7986 start
= pcs
->pcs_start_time
;
7987 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7989 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7990 char *date
= ctime(&start
);
7993 * ctime() adds a newline at the end of the generated
7994 * string, thus the weird format specifier and the
7995 * strlen() call used to chop it off from the output.
7997 (void) printf(gettext("created %.*s, consumes %s\n"),
7998 (int)(strlen(date
) - 1), date
, space_buf
);
8002 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
8004 (void) printf(gettext("discarding, %s remaining.\n"),
8009 print_error_log(zpool_handle_t
*zhp
)
8011 nvlist_t
*nverrlist
= NULL
;
8014 size_t len
= MAXPATHLEN
* 2;
8016 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
8019 (void) printf("errors: Permanent errors have been "
8020 "detected in the following files:\n\n");
8022 pathname
= safe_malloc(len
);
8024 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
8026 uint64_t dsobj
, obj
;
8028 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
8029 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
8031 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
8033 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
8034 (void) printf("%7s %s\n", "", pathname
);
8037 nvlist_free(nverrlist
);
8041 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
8050 (void) printf(gettext("\tspares\n"));
8052 for (i
= 0; i
< nspares
; i
++) {
8053 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
8055 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
8061 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
8070 (void) printf(gettext("\tcache\n"));
8072 for (i
= 0; i
< nl2cache
; i
++) {
8073 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
8075 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
8082 print_dedup_stats(nvlist_t
*config
)
8084 ddt_histogram_t
*ddh
;
8088 char dspace
[6], mspace
[6];
8091 * If the pool was faulted then we may not have been able to
8092 * obtain the config. Otherwise, if we have anything in the dedup
8093 * table continue processing the stats.
8095 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
8096 (uint64_t **)&ddo
, &c
) != 0)
8099 (void) printf("\n");
8100 (void) printf(gettext(" dedup: "));
8101 if (ddo
->ddo_count
== 0) {
8102 (void) printf(gettext("no DDT entries\n"));
8106 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
8107 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
8108 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8109 (u_longlong_t
)ddo
->ddo_count
,
8113 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
8114 (uint64_t **)&dds
, &c
) == 0);
8115 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
8116 (uint64_t **)&ddh
, &c
) == 0);
8117 zpool_dump_ddt(dds
, ddh
);
8121 * Display a summary of pool status. Displays a summary such as:
8125 * reason: One or more devices ...
8126 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8132 * When given the '-v' option, we print out the complete config. If the '-e'
8133 * option is specified, then we print out error rate information as well.
8136 status_callback(zpool_handle_t
*zhp
, void *data
)
8138 status_cbdata_t
*cbp
= data
;
8139 nvlist_t
*config
, *nvroot
;
8141 zpool_status_t reason
;
8142 zpool_errata_t errata
;
8147 config
= zpool_get_config(zhp
, NULL
);
8148 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
8153 * If we were given 'zpool status -x', only report those pools with
8156 if (cbp
->cb_explain
&&
8157 (reason
== ZPOOL_STATUS_OK
||
8158 reason
== ZPOOL_STATUS_VERSION_OLDER
||
8159 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
8160 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
8161 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
8162 if (!cbp
->cb_allpools
) {
8163 (void) printf(gettext("pool '%s' is healthy\n"),
8164 zpool_get_name(zhp
));
8166 cbp
->cb_first
= B_FALSE
;
8172 cbp
->cb_first
= B_FALSE
;
8174 (void) printf("\n");
8176 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8177 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8178 (uint64_t **)&vs
, &c
) == 0);
8180 health
= zpool_get_state_str(zhp
);
8183 printf_color(ANSI_BOLD
, gettext("pool:"));
8184 printf(" %s\n", zpool_get_name(zhp
));
8186 printf_color(ANSI_BOLD
, gettext("state: "));
8188 printf_color(health_str_to_color(health
), "%s", health
);
8193 case ZPOOL_STATUS_MISSING_DEV_R
:
8194 printf_color(ANSI_BOLD
, gettext("status: "));
8195 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8196 "not be opened. Sufficient replicas exist for\n\tthe pool "
8197 "to continue functioning in a degraded state.\n"));
8198 printf_color(ANSI_BOLD
, gettext("action: "));
8199 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8200 "and online it using 'zpool online'.\n"));
8203 case ZPOOL_STATUS_MISSING_DEV_NR
:
8204 printf_color(ANSI_BOLD
, gettext("status: "));
8205 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8206 "not be opened. There are insufficient\n\treplicas for the"
8207 " pool to continue functioning.\n"));
8208 printf_color(ANSI_BOLD
, gettext("action: "));
8209 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8210 "and online it using 'zpool online'.\n"));
8213 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8214 printf_color(ANSI_BOLD
, gettext("status: "));
8215 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8216 "not be used because the label is missing or\n\tinvalid. "
8217 "Sufficient replicas exist for the pool to continue\n\t"
8218 "functioning in a degraded state.\n"));
8219 printf_color(ANSI_BOLD
, gettext("action: "));
8220 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8221 "'zpool replace'.\n"));
8224 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8225 printf_color(ANSI_BOLD
, gettext("status: "));
8226 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8227 "not be used because the label is missing \n\tor invalid. "
8228 "There are insufficient replicas for the pool to "
8229 "continue\n\tfunctioning.\n"));
8230 zpool_explain_recover(zpool_get_handle(zhp
),
8231 zpool_get_name(zhp
), reason
, config
);
8234 case ZPOOL_STATUS_FAILING_DEV
:
8235 printf_color(ANSI_BOLD
, gettext("status: "));
8236 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8237 "experienced an unrecoverable error. An\n\tattempt was "
8238 "made to correct the error. Applications are "
8240 printf_color(ANSI_BOLD
, gettext("action: "));
8241 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8242 "device needs to be replaced, and clear the errors\n\tusing"
8243 " 'zpool clear' or replace the device with 'zpool "
8247 case ZPOOL_STATUS_OFFLINE_DEV
:
8248 printf_color(ANSI_BOLD
, gettext("status: "));
8249 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8250 "been taken offline by the administrator.\n\tSufficient "
8251 "replicas exist for the pool to continue functioning in "
8252 "a\n\tdegraded state.\n"));
8253 printf_color(ANSI_BOLD
, gettext("action: "));
8254 printf_color(ANSI_YELLOW
, gettext("Online the device "
8255 "using 'zpool online' or replace the device with\n\t'zpool "
8259 case ZPOOL_STATUS_REMOVED_DEV
:
8260 printf_color(ANSI_BOLD
, gettext("status: "));
8261 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8262 "been removed by the administrator.\n\tSufficient "
8263 "replicas exist for the pool to continue functioning in "
8264 "a\n\tdegraded state.\n"));
8265 printf_color(ANSI_BOLD
, gettext("action: "));
8266 printf_color(ANSI_YELLOW
, gettext("Online the device "
8267 "using zpool online' or replace the device with\n\t'zpool "
8271 case ZPOOL_STATUS_RESILVERING
:
8272 case ZPOOL_STATUS_REBUILDING
:
8273 printf_color(ANSI_BOLD
, gettext("status: "));
8274 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8275 "currently being resilvered. The pool will\n\tcontinue "
8276 "to function, possibly in a degraded state.\n"));
8277 printf_color(ANSI_BOLD
, gettext("action: "));
8278 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8282 case ZPOOL_STATUS_REBUILD_SCRUB
:
8283 printf_color(ANSI_BOLD
, gettext("status: "));
8284 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8285 "been sequentially resilvered, scrubbing\n\tthe pool "
8286 "is recommended.\n"));
8287 printf_color(ANSI_BOLD
, gettext("action: "));
8288 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8289 "verify all data checksums.\n"));
8292 case ZPOOL_STATUS_CORRUPT_DATA
:
8293 printf_color(ANSI_BOLD
, gettext("status: "));
8294 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8295 "experienced an error resulting in data\n\tcorruption. "
8296 "Applications may be affected.\n"));
8297 printf_color(ANSI_BOLD
, gettext("action: "));
8298 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8299 " if possible. Otherwise restore the\n\tentire pool from "
8303 case ZPOOL_STATUS_CORRUPT_POOL
:
8304 printf_color(ANSI_BOLD
, gettext("status: "));
8305 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8306 "corrupted and the pool cannot be opened.\n"));
8307 zpool_explain_recover(zpool_get_handle(zhp
),
8308 zpool_get_name(zhp
), reason
, config
);
8311 case ZPOOL_STATUS_VERSION_OLDER
:
8312 printf_color(ANSI_BOLD
, gettext("status: "));
8313 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8314 "a legacy on-disk format. The pool can\n\tstill be used, "
8315 "but some features are unavailable.\n"));
8316 printf_color(ANSI_BOLD
, gettext("action: "));
8317 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8318 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8319 "longer be accessible on software that does not support\n\t"
8320 "feature flags.\n"));
8323 case ZPOOL_STATUS_VERSION_NEWER
:
8324 printf_color(ANSI_BOLD
, gettext("status: "));
8325 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8326 "to a newer, incompatible on-disk version.\n\tThe pool "
8327 "cannot be accessed on this system.\n"));
8328 printf_color(ANSI_BOLD
, gettext("action: "));
8329 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8330 "system running more recent software, or\n\trestore the "
8331 "pool from backup.\n"));
8334 case ZPOOL_STATUS_FEAT_DISABLED
:
8335 printf_color(ANSI_BOLD
, gettext("status: "));
8336 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8337 "requested features are not enabled on the pool.\n\t"
8338 "The pool can still be used, but some features are "
8340 printf_color(ANSI_BOLD
, gettext("action: "));
8341 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8342 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8343 "longer be accessible by software that does not support\n\t"
8344 "the features. See zpool-features(7) for details.\n"));
8347 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8348 printf_color(ANSI_BOLD
, gettext("status: "));
8349 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8350 "compatibility list specified, but it could not be\n\t"
8351 "read/parsed at this time. The pool can still be used, "
8352 "but this\n\tshould be investigated.\n"));
8353 printf_color(ANSI_BOLD
, gettext("action: "));
8354 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8355 "'compatibility' property against the\n\t"
8356 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8357 ZPOOL_DATA_COMPAT_D
".\n"));
8360 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
8361 printf_color(ANSI_BOLD
, gettext("status: "));
8362 printf_color(ANSI_YELLOW
, gettext("One or more features "
8363 "are enabled on the pool despite not being\n\t"
8364 "requested by the 'compatibility' property.\n"));
8365 printf_color(ANSI_BOLD
, gettext("action: "));
8366 printf_color(ANSI_YELLOW
, gettext("Consider setting "
8367 "'compatibility' to an appropriate value, or\n\t"
8368 "adding needed features to the relevant file in\n\t"
8369 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
8372 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8373 printf_color(ANSI_BOLD
, gettext("status: "));
8374 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8375 "on this system because it uses the\n\tfollowing feature(s)"
8376 " not supported on this system:\n"));
8377 zpool_print_unsup_feat(config
);
8378 (void) printf("\n");
8379 printf_color(ANSI_BOLD
, gettext("action: "));
8380 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8381 "system that supports the required feature(s),\n\tor "
8382 "restore the pool from backup.\n"));
8385 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8386 printf_color(ANSI_BOLD
, gettext("status: "));
8387 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8388 "accessed in read-only mode on this system. It\n\tcannot be"
8389 " accessed in read-write mode because it uses the "
8390 "following\n\tfeature(s) not supported on this system:\n"));
8391 zpool_print_unsup_feat(config
);
8392 (void) printf("\n");
8393 printf_color(ANSI_BOLD
, gettext("action: "));
8394 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8395 "in read-write mode. Import the pool with\n"
8396 "\t\"-o readonly=on\", access the pool from a system that "
8397 "supports the\n\trequired feature(s), or restore the "
8398 "pool from backup.\n"));
8401 case ZPOOL_STATUS_FAULTED_DEV_R
:
8402 printf_color(ANSI_BOLD
, gettext("status: "));
8403 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8404 "faulted in response to persistent errors.\n\tSufficient "
8405 "replicas exist for the pool to continue functioning "
8406 "in a\n\tdegraded state.\n"));
8407 printf_color(ANSI_BOLD
, gettext("action: "));
8408 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8409 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8412 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8413 printf_color(ANSI_BOLD
, gettext("status: "));
8414 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8415 "faulted in response to persistent errors. There are "
8416 "insufficient replicas for the pool to\n\tcontinue "
8418 printf_color(ANSI_BOLD
, gettext("action: "));
8419 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8420 "pool from a backup source. Manually marking the device\n"
8421 "\trepaired using 'zpool clear' may allow some data "
8422 "to be recovered.\n"));
8425 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8426 printf_color(ANSI_BOLD
, gettext("status: "));
8427 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8428 "because multihost writes failed or were delayed;\n\t"
8429 "another system could import the pool undetected.\n"));
8430 printf_color(ANSI_BOLD
, gettext("action: "));
8431 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8432 " are connected, then reboot your system and\n\timport the "
8436 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8437 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8438 printf_color(ANSI_BOLD
, gettext("status: "));
8439 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8440 "faulted in response to IO failures.\n"));
8441 printf_color(ANSI_BOLD
, gettext("action: "));
8442 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8443 "devices are connected, then run 'zpool clear'.\n"));
8446 case ZPOOL_STATUS_BAD_LOG
:
8447 printf_color(ANSI_BOLD
, gettext("status: "));
8448 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8449 "could not be read.\n"
8450 "\tWaiting for administrator intervention to fix the "
8451 "faulted pool.\n"));
8452 printf_color(ANSI_BOLD
, gettext("action: "));
8453 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8454 "device(s) and run 'zpool online',\n"
8455 "\tor ignore the intent log records by running "
8456 "'zpool clear'.\n"));
8459 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8460 (void) printf(gettext("status: One or more devices are "
8461 "configured to use a non-native block size.\n"
8462 "\tExpect reduced performance.\n"));
8463 (void) printf(gettext("action: Replace affected devices with "
8464 "devices that support the\n\tconfigured block size, or "
8465 "migrate data to a properly configured\n\tpool.\n"));
8468 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8469 printf_color(ANSI_BOLD
, gettext("status: "));
8470 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8471 " and system hostid on imported pool.\n\tThis pool was "
8472 "previously imported into a system with a different "
8473 "hostid,\n\tand then was verbatim imported into this "
8475 printf_color(ANSI_BOLD
, gettext("action: "));
8476 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8477 "systems on which it is imported.\n"
8478 "\tThen import it to correct the mismatch.\n"));
8481 case ZPOOL_STATUS_ERRATA
:
8482 printf_color(ANSI_BOLD
, gettext("status: "));
8483 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8487 case ZPOOL_ERRATA_NONE
:
8490 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8491 printf_color(ANSI_BOLD
, gettext("action: "));
8492 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8493 " run 'zpool scrub'.\n"));
8496 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8497 (void) printf(gettext("\tExisting encrypted datasets "
8498 "contain an on-disk incompatibility\n\twhich "
8499 "needs to be corrected.\n"));
8500 printf_color(ANSI_BOLD
, gettext("action: "));
8501 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8502 " backup existing encrypted datasets to new\n\t"
8503 "encrypted datasets and destroy the old ones. "
8504 "'zfs mount -o ro' can\n\tbe used to temporarily "
8505 "mount existing encrypted datasets readonly.\n"));
8508 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8509 (void) printf(gettext("\tExisting encrypted snapshots "
8510 "and bookmarks contain an on-disk\n\tincompat"
8511 "ibility. This may cause on-disk corruption if "
8512 "they are used\n\twith 'zfs recv'.\n"));
8513 printf_color(ANSI_BOLD
, gettext("action: "));
8514 printf_color(ANSI_YELLOW
, gettext("To correct the"
8515 "issue, enable the bookmark_v2 feature. No "
8516 "additional\n\taction is needed if there are no "
8517 "encrypted snapshots or bookmarks.\n\tIf preserving"
8518 "the encrypted snapshots and bookmarks is required,"
8519 " use\n\ta non-raw send to backup and restore them."
8520 " Alternately, they may be\n\tremoved to resolve "
8521 "the incompatibility.\n"));
8526 * All errata which allow the pool to be imported
8527 * must contain an action message.
8535 * The remaining errors can't actually be generated, yet.
8537 assert(reason
== ZPOOL_STATUS_OK
);
8540 if (msgid
!= NULL
) {
8542 printf_color(ANSI_BOLD
, gettext("see:"));
8544 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8548 if (config
!= NULL
) {
8550 nvlist_t
**spares
, **l2cache
;
8551 uint_t nspares
, nl2cache
;
8552 pool_checkpoint_stat_t
*pcs
= NULL
;
8553 pool_removal_stat_t
*prs
= NULL
;
8555 print_scan_status(zhp
, nvroot
);
8557 (void) nvlist_lookup_uint64_array(nvroot
,
8558 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8559 print_removal_status(zhp
, prs
);
8561 (void) nvlist_lookup_uint64_array(nvroot
,
8562 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8563 print_checkpoint_status(pcs
);
8565 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8566 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8567 if (cbp
->cb_namewidth
< 10)
8568 cbp
->cb_namewidth
= 10;
8570 color_start(ANSI_BOLD
);
8571 (void) printf(gettext("config:\n\n"));
8572 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8573 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8577 if (cbp
->cb_print_slow_ios
) {
8578 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8581 if (cbp
->vcdl
!= NULL
)
8582 print_cmd_columns(cbp
->vcdl
, 0);
8586 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8589 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8590 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8591 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8593 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8594 &l2cache
, &nl2cache
) == 0)
8595 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8597 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8598 &spares
, &nspares
) == 0)
8599 print_spares(zhp
, cbp
, spares
, nspares
);
8601 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8603 nvlist_t
*nverrlist
= NULL
;
8606 * If the approximate error count is small, get a
8607 * precise count by fetching the entire log and
8608 * uniquifying the results.
8610 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8611 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8616 while ((elem
= nvlist_next_nvpair(nverrlist
,
8621 nvlist_free(nverrlist
);
8623 (void) printf("\n");
8626 (void) printf(gettext("errors: No known data "
8628 else if (!cbp
->cb_verbose
)
8629 (void) printf(gettext("errors: %llu data "
8630 "errors, use '-v' for a list\n"),
8631 (u_longlong_t
)nerr
);
8633 print_error_log(zhp
);
8636 if (cbp
->cb_dedup_stats
)
8637 print_dedup_stats(config
);
8639 (void) printf(gettext("config: The configuration cannot be "
8647 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8648 * [interval [count]]
8650 * -c CMD For each vdev, run command CMD
8651 * -i Display vdev initialization status.
8652 * -g Display guid for individual vdev name.
8653 * -L Follow links when resolving vdev path name.
8654 * -p Display values in parsable (exact) format.
8655 * -P Display full path for vdev name.
8656 * -s Display slow IOs column.
8657 * -v Display complete error logs
8658 * -x Display only pools with potential problems
8659 * -D Display dedup status (undocumented)
8660 * -t Display vdev TRIM status.
8661 * -T Display a timestamp in date(1) or Unix format
8663 * Describes the health status of all pools or some subset.
8666 zpool_do_status(int argc
, char **argv
)
8671 unsigned long count
= 0;
8672 status_cbdata_t cb
= { 0 };
8676 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8681 gettext("Can't set -c flag twice\n"));
8685 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8686 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8687 fprintf(stderr
, gettext(
8688 "Can't run -c, disabled by "
8689 "ZPOOL_SCRIPTS_ENABLED.\n"));
8693 if ((getuid() <= 0 || geteuid() <= 0) &&
8694 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8695 fprintf(stderr
, gettext(
8696 "Can't run -c with root privileges "
8697 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8703 cb
.cb_print_vdev_init
= B_TRUE
;
8706 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8709 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8712 cb
.cb_literal
= B_TRUE
;
8715 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8718 cb
.cb_print_slow_ios
= B_TRUE
;
8721 cb
.cb_verbose
= B_TRUE
;
8724 cb
.cb_explain
= B_TRUE
;
8727 cb
.cb_dedup_stats
= B_TRUE
;
8730 cb
.cb_print_vdev_trim
= B_TRUE
;
8733 get_timestamp_arg(*optarg
);
8736 if (optopt
== 'c') {
8737 print_zpool_script_list("status");
8741 gettext("invalid option '%c'\n"), optopt
);
8750 get_interval_count(&argc
, argv
, &interval
, &count
);
8753 cb
.cb_allpools
= B_TRUE
;
8755 cb
.cb_first
= B_TRUE
;
8756 cb
.cb_print_status
= B_TRUE
;
8759 if (timestamp_fmt
!= NODATE
)
8760 print_timestamp(timestamp_fmt
);
8763 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8766 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8767 cb
.cb_literal
, status_callback
, &cb
);
8769 if (cb
.vcdl
!= NULL
)
8770 free_vdev_cmd_data_list(cb
.vcdl
);
8772 if (argc
== 0 && cb
.cb_count
== 0)
8773 (void) fprintf(stderr
, gettext("no pools available\n"));
8774 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8775 (void) printf(gettext("all pools are healthy\n"));
8783 if (count
!= 0 && --count
== 0)
8786 (void) fsleep(interval
);
8792 typedef struct upgrade_cbdata
{
8795 uint64_t cb_version
;
8800 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8802 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8803 int *count
= (int *)unsupp_fs
;
8805 if (zfs_version
> ZPL_VERSION
) {
8806 (void) printf(gettext("%s (v%d) is not supported by this "
8807 "implementation of ZFS.\n"),
8808 zfs_get_name(zhp
), zfs_version
);
8812 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8820 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8824 uint64_t oldversion
;
8827 config
= zpool_get_config(zhp
, NULL
);
8828 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8831 char compat
[ZFS_MAXPROPLEN
];
8832 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8833 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8836 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8837 assert(oldversion
< version
);
8839 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8844 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8845 "to %d unsupported filesystems (max v%d).\n"),
8846 unsupp_fs
, (int)ZPL_VERSION
);
8850 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
8851 (void) fprintf(stderr
, gettext("Upgrade not performed because "
8852 "'compatibility' property set to '"
8853 ZPOOL_COMPAT_LEGACY
"'.\n"));
8857 ret
= zpool_upgrade(zhp
, version
);
8861 if (version
>= SPA_VERSION_FEATURES
) {
8862 (void) printf(gettext("Successfully upgraded "
8863 "'%s' from version %llu to feature flags.\n"),
8864 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8866 (void) printf(gettext("Successfully upgraded "
8867 "'%s' from version %llu to version %llu.\n"),
8868 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8869 (u_longlong_t
)version
);
8876 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8879 boolean_t firstff
= B_TRUE
;
8880 nvlist_t
*enabled
= zpool_get_features(zhp
);
8882 char compat
[ZFS_MAXPROPLEN
];
8883 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8884 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8887 boolean_t requested_features
[SPA_FEATURES
];
8888 if (zpool_do_load_compat(compat
, requested_features
) !=
8889 ZPOOL_COMPATIBILITY_OK
)
8893 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8894 const char *fname
= spa_feature_table
[i
].fi_uname
;
8895 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8897 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8900 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
8902 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8903 ret
= zpool_set_prop(zhp
, propname
,
8904 ZFS_FEATURE_ENABLED
);
8912 (void) printf(gettext("Enabled the "
8913 "following features on '%s':\n"),
8914 zpool_get_name(zhp
));
8917 (void) printf(gettext(" %s\n"), fname
);
8928 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8930 upgrade_cbdata_t
*cbp
= arg
;
8933 boolean_t modified_pool
= B_FALSE
;
8936 config
= zpool_get_config(zhp
, NULL
);
8937 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8940 assert(SPA_VERSION_IS_SUPPORTED(version
));
8942 if (version
< cbp
->cb_version
) {
8943 cbp
->cb_first
= B_FALSE
;
8944 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8947 modified_pool
= B_TRUE
;
8950 * If they did "zpool upgrade -a", then we could
8951 * be doing ioctls to different pools. We need
8952 * to log this history once to each pool, and bypass
8953 * the normal history logging that happens in main().
8955 (void) zpool_log_history(g_zfs
, history_str
);
8956 log_history
= B_FALSE
;
8959 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8961 ret
= upgrade_enable_all(zhp
, &count
);
8966 cbp
->cb_first
= B_FALSE
;
8967 modified_pool
= B_TRUE
;
8971 if (modified_pool
) {
8972 (void) printf("\n");
8973 (void) after_zpool_upgrade(zhp
);
8980 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8982 upgrade_cbdata_t
*cbp
= arg
;
8986 config
= zpool_get_config(zhp
, NULL
);
8987 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8990 assert(SPA_VERSION_IS_SUPPORTED(version
));
8992 if (version
< SPA_VERSION_FEATURES
) {
8993 if (cbp
->cb_first
) {
8994 (void) printf(gettext("The following pools are "
8995 "formatted with legacy version numbers and can\n"
8996 "be upgraded to use feature flags. After "
8997 "being upgraded, these pools\nwill no "
8998 "longer be accessible by software that does not "
8999 "support feature\nflags.\n\n"
9000 "Note that setting a pool's 'compatibility' "
9001 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
9002 "inhibit upgrades.\n\n"));
9003 (void) printf(gettext("VER POOL\n"));
9004 (void) printf(gettext("--- ------------\n"));
9005 cbp
->cb_first
= B_FALSE
;
9008 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
9009 zpool_get_name(zhp
));
9016 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
9018 upgrade_cbdata_t
*cbp
= arg
;
9022 config
= zpool_get_config(zhp
, NULL
);
9023 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9026 if (version
>= SPA_VERSION_FEATURES
) {
9028 boolean_t poolfirst
= B_TRUE
;
9029 nvlist_t
*enabled
= zpool_get_features(zhp
);
9031 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9032 const char *fguid
= spa_feature_table
[i
].fi_guid
;
9033 const char *fname
= spa_feature_table
[i
].fi_uname
;
9035 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
9038 if (!nvlist_exists(enabled
, fguid
)) {
9039 if (cbp
->cb_first
) {
9040 (void) printf(gettext("\nSome "
9041 "supported features are not "
9042 "enabled on the following pools. "
9043 "Once a\nfeature is enabled the "
9044 "pool may become incompatible with "
9045 "software\nthat does not support "
9047 "zpool-features(7) for "
9049 "Note that the pool "
9050 "'compatibility' feature can be "
9051 "used to inhibit\nfeature "
9053 (void) printf(gettext("POOL "
9055 (void) printf(gettext("------"
9057 cbp
->cb_first
= B_FALSE
;
9061 (void) printf(gettext("%s\n"),
9062 zpool_get_name(zhp
));
9063 poolfirst
= B_FALSE
;
9066 (void) printf(gettext(" %s\n"), fname
);
9069 * If they did "zpool upgrade -a", then we could
9070 * be doing ioctls to different pools. We need
9071 * to log this history once to each pool, and bypass
9072 * the normal history logging that happens in main().
9074 (void) zpool_log_history(g_zfs
, history_str
);
9075 log_history
= B_FALSE
;
9083 upgrade_one(zpool_handle_t
*zhp
, void *data
)
9085 boolean_t modified_pool
= B_FALSE
;
9086 upgrade_cbdata_t
*cbp
= data
;
9087 uint64_t cur_version
;
9090 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
9091 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
9092 "Pool 'log' must be renamed using export and import"
9097 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
9098 if (cur_version
> cbp
->cb_version
) {
9099 (void) printf(gettext("Pool '%s' is already formatted "
9100 "using more current version '%llu'.\n\n"),
9101 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
9105 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
9106 (void) printf(gettext("Pool '%s' is already formatted "
9107 "using version %llu.\n\n"), zpool_get_name(zhp
),
9108 (u_longlong_t
)cbp
->cb_version
);
9112 if (cur_version
!= cbp
->cb_version
) {
9113 modified_pool
= B_TRUE
;
9114 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9119 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9121 ret
= upgrade_enable_all(zhp
, &count
);
9126 modified_pool
= B_TRUE
;
9127 } else if (cur_version
== SPA_VERSION
) {
9128 (void) printf(gettext("Pool '%s' already has all "
9129 "supported and requested features enabled.\n"),
9130 zpool_get_name(zhp
));
9134 if (modified_pool
) {
9135 (void) printf("\n");
9136 (void) after_zpool_upgrade(zhp
);
9145 * zpool upgrade [-V version] <-a | pool ...>
9147 * With no arguments, display downrev'd ZFS pool available for upgrade.
9148 * Individual pools can be upgraded by specifying the pool, and '-a' will
9149 * upgrade all pools.
9152 zpool_do_upgrade(int argc
, char **argv
)
9155 upgrade_cbdata_t cb
= { 0 };
9157 boolean_t showversions
= B_FALSE
;
9158 boolean_t upgradeall
= B_FALSE
;
9163 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
9166 upgradeall
= B_TRUE
;
9169 showversions
= B_TRUE
;
9172 cb
.cb_version
= strtoll(optarg
, &end
, 10);
9174 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
9175 (void) fprintf(stderr
,
9176 gettext("invalid version '%s'\n"), optarg
);
9181 (void) fprintf(stderr
, gettext("missing argument for "
9182 "'%c' option\n"), optopt
);
9186 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9197 if (cb
.cb_version
== 0) {
9198 cb
.cb_version
= SPA_VERSION
;
9199 } else if (!upgradeall
&& argc
== 0) {
9200 (void) fprintf(stderr
, gettext("-V option is "
9201 "incompatible with other arguments\n"));
9206 if (upgradeall
|| argc
!= 0) {
9207 (void) fprintf(stderr
, gettext("-v option is "
9208 "incompatible with other arguments\n"));
9211 } else if (upgradeall
) {
9213 (void) fprintf(stderr
, gettext("-a option should not "
9214 "be used along with a pool name\n"));
9219 (void) printf("%s", gettext("This system supports ZFS pool feature "
9224 (void) printf(gettext("The following features are "
9226 (void) printf(gettext("FEAT DESCRIPTION\n"));
9227 (void) printf("----------------------------------------------"
9228 "---------------\n");
9229 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9230 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9231 if (!fi
->fi_zfs_mod_supported
)
9234 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9235 " (read-only compatible)" : "";
9237 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9238 (void) printf(" %s\n", fi
->fi_desc
);
9240 (void) printf("\n");
9242 (void) printf(gettext("The following legacy versions are also "
9244 (void) printf(gettext("VER DESCRIPTION\n"));
9245 (void) printf("--- -----------------------------------------"
9246 "---------------\n");
9247 (void) printf(gettext(" 1 Initial ZFS version\n"));
9248 (void) printf(gettext(" 2 Ditto blocks "
9249 "(replicated metadata)\n"));
9250 (void) printf(gettext(" 3 Hot spares and double parity "
9252 (void) printf(gettext(" 4 zpool history\n"));
9253 (void) printf(gettext(" 5 Compression using the gzip "
9255 (void) printf(gettext(" 6 bootfs pool property\n"));
9256 (void) printf(gettext(" 7 Separate intent log devices\n"));
9257 (void) printf(gettext(" 8 Delegated administration\n"));
9258 (void) printf(gettext(" 9 refquota and refreservation "
9260 (void) printf(gettext(" 10 Cache devices\n"));
9261 (void) printf(gettext(" 11 Improved scrub performance\n"));
9262 (void) printf(gettext(" 12 Snapshot properties\n"));
9263 (void) printf(gettext(" 13 snapused property\n"));
9264 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9265 (void) printf(gettext(" 15 user/group space accounting\n"));
9266 (void) printf(gettext(" 16 stmf property support\n"));
9267 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9268 (void) printf(gettext(" 18 Snapshot user holds\n"));
9269 (void) printf(gettext(" 19 Log device removal\n"));
9270 (void) printf(gettext(" 20 Compression using zle "
9271 "(zero-length encoding)\n"));
9272 (void) printf(gettext(" 21 Deduplication\n"));
9273 (void) printf(gettext(" 22 Received properties\n"));
9274 (void) printf(gettext(" 23 Slim ZIL\n"));
9275 (void) printf(gettext(" 24 System attributes\n"));
9276 (void) printf(gettext(" 25 Improved scrub stats\n"));
9277 (void) printf(gettext(" 26 Improved snapshot deletion "
9279 (void) printf(gettext(" 27 Improved snapshot creation "
9281 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9282 (void) printf(gettext("\nFor more information on a particular "
9283 "version, including supported releases,\n"));
9284 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9285 } else if (argc
== 0 && upgradeall
) {
9286 cb
.cb_first
= B_TRUE
;
9287 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9288 if (ret
== 0 && cb
.cb_first
) {
9289 if (cb
.cb_version
== SPA_VERSION
) {
9290 (void) printf(gettext("All pools are already "
9291 "formatted using feature flags.\n\n"));
9292 (void) printf(gettext("Every feature flags "
9293 "pool already has all supported and "
9294 "requested features enabled.\n"));
9296 (void) printf(gettext("All pools are already "
9297 "formatted with version %llu or higher.\n"),
9298 (u_longlong_t
)cb
.cb_version
);
9301 } else if (argc
== 0) {
9302 cb
.cb_first
= B_TRUE
;
9303 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9307 (void) printf(gettext("All pools are formatted "
9308 "using feature flags.\n\n"));
9310 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9311 "for a list of available legacy versions.\n"));
9314 cb
.cb_first
= B_TRUE
;
9315 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9319 (void) printf(gettext("Every feature flags pool has "
9320 "all supported and requested features enabled.\n"));
9322 (void) printf(gettext("\n"));
9325 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9326 B_FALSE
, upgrade_one
, &cb
);
9332 typedef struct hist_cbdata
{
9339 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9345 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9346 &records
, &numrecords
) == 0);
9347 for (i
= 0; i
< numrecords
; i
++) {
9348 nvlist_t
*rec
= records
[i
];
9351 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9355 tsec
= fnvlist_lookup_uint64(records
[i
],
9357 (void) localtime_r(&tsec
, &t
);
9358 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9361 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9362 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9363 ZPOOL_HIST_ELAPSED_NS
);
9364 (void) snprintf(tbuf
+ strlen(tbuf
),
9365 sizeof (tbuf
) - strlen(tbuf
),
9366 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9369 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9370 (void) printf("%s %s", tbuf
,
9371 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9372 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9374 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9377 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9378 (void) printf("%s unrecognized record:\n",
9380 dump_nvlist(rec
, 4);
9383 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9384 zfs_history_event_names
[ievent
],
9385 (longlong_t
)fnvlist_lookup_uint64(
9386 rec
, ZPOOL_HIST_TXG
),
9387 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9388 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9391 (void) printf("%s [txg:%lld] %s", tbuf
,
9392 (longlong_t
)fnvlist_lookup_uint64(
9393 rec
, ZPOOL_HIST_TXG
),
9394 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9395 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9396 (void) printf(" %s (%llu)",
9397 fnvlist_lookup_string(rec
,
9399 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9402 (void) printf(" %s", fnvlist_lookup_string(rec
,
9403 ZPOOL_HIST_INT_STR
));
9404 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9407 (void) printf("%s ioctl %s\n", tbuf
,
9408 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9409 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9410 (void) printf(" input:\n");
9411 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9412 ZPOOL_HIST_INPUT_NVL
), 8);
9414 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9415 (void) printf(" output:\n");
9416 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9417 ZPOOL_HIST_OUTPUT_NVL
), 8);
9419 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9420 (void) printf(" output nvlist omitted; "
9421 "original size: %lldKB\n",
9422 (longlong_t
)fnvlist_lookup_int64(rec
,
9423 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9425 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9426 (void) printf(" errno: %lld\n",
9427 (longlong_t
)fnvlist_lookup_int64(rec
,
9433 (void) printf("%s unrecognized record:\n", tbuf
);
9434 dump_nvlist(rec
, 4);
9438 (void) printf("\n");
9441 (void) printf(" [");
9442 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9443 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9444 struct passwd
*pwd
= getpwuid(who
);
9445 (void) printf("user %d ", (int)who
);
9447 (void) printf("(%s) ", pwd
->pw_name
);
9449 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9450 (void) printf("on %s",
9451 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9453 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9454 (void) printf(":%s",
9455 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9459 (void) printf("\n");
9464 * Print out the command history for a specific pool.
9467 get_history_one(zpool_handle_t
*zhp
, void *data
)
9471 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9473 boolean_t eof
= B_FALSE
;
9475 cb
->first
= B_FALSE
;
9477 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9480 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9483 print_history_records(nvhis
, cb
);
9486 (void) printf("\n");
9492 * zpool history <pool>
9494 * Displays the history of commands that modified pools.
9497 zpool_do_history(int argc
, char **argv
)
9499 hist_cbdata_t cbdata
= { 0 };
9503 cbdata
.first
= B_TRUE
;
9505 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9508 cbdata
.longfmt
= B_TRUE
;
9511 cbdata
.internal
= B_TRUE
;
9514 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9522 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9523 B_FALSE
, get_history_one
, &cbdata
);
9525 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9526 (void) fprintf(stderr
, gettext("no pools available\n"));
9533 typedef struct ev_opts
{
9538 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9542 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9544 char ctime_str
[26], str
[32], *ptr
;
9548 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9549 memset(str
, ' ', 32);
9550 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9551 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9552 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9553 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9554 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9556 (void) printf(gettext("%s\t"), str
);
9558 (void) printf(gettext("%s "), str
);
9560 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9561 (void) printf(gettext("%s\n"), ptr
);
9565 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9569 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9570 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9572 data_type_t type
= nvpair_type(nvp
);
9573 const char *name
= nvpair_name(nvp
);
9583 printf(gettext("%*s%s = "), depth
, "", name
);
9586 case DATA_TYPE_BOOLEAN
:
9587 printf(gettext("%s"), "1");
9590 case DATA_TYPE_BOOLEAN_VALUE
:
9591 (void) nvpair_value_boolean_value(nvp
, &b
);
9592 printf(gettext("%s"), b
? "1" : "0");
9595 case DATA_TYPE_BYTE
:
9596 (void) nvpair_value_byte(nvp
, &i8
);
9597 printf(gettext("0x%x"), i8
);
9600 case DATA_TYPE_INT8
:
9601 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9602 printf(gettext("0x%x"), i8
);
9605 case DATA_TYPE_UINT8
:
9606 (void) nvpair_value_uint8(nvp
, &i8
);
9607 printf(gettext("0x%x"), i8
);
9610 case DATA_TYPE_INT16
:
9611 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9612 printf(gettext("0x%x"), i16
);
9615 case DATA_TYPE_UINT16
:
9616 (void) nvpair_value_uint16(nvp
, &i16
);
9617 printf(gettext("0x%x"), i16
);
9620 case DATA_TYPE_INT32
:
9621 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9622 printf(gettext("0x%x"), i32
);
9625 case DATA_TYPE_UINT32
:
9626 (void) nvpair_value_uint32(nvp
, &i32
);
9627 printf(gettext("0x%x"), i32
);
9630 case DATA_TYPE_INT64
:
9631 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9632 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9635 case DATA_TYPE_UINT64
:
9636 (void) nvpair_value_uint64(nvp
, &i64
);
9638 * translate vdev state values to readable
9639 * strings to aide zpool events consumers
9642 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9644 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9645 printf(gettext("\"%s\" (0x%llx)"),
9646 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9649 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9653 case DATA_TYPE_HRTIME
:
9654 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9655 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9658 case DATA_TYPE_STRING
:
9659 (void) nvpair_value_string(nvp
, &str
);
9660 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9663 case DATA_TYPE_NVLIST
:
9664 printf(gettext("(embedded nvlist)\n"));
9665 (void) nvpair_value_nvlist(nvp
, &cnv
);
9666 zpool_do_events_nvprint(cnv
, depth
+ 8);
9667 printf(gettext("%*s(end %s)"), depth
, "", name
);
9670 case DATA_TYPE_NVLIST_ARRAY
: {
9674 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9675 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9676 for (i
= 0; i
< nelem
; i
++) {
9677 printf(gettext("%*s%s[%d] = %s\n"),
9678 depth
, "", name
, i
, "(embedded nvlist)");
9679 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9680 printf(gettext("%*s(end %s[%i])\n"),
9681 depth
, "", name
, i
);
9683 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9687 case DATA_TYPE_INT8_ARRAY
: {
9691 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9692 for (i
= 0; i
< nelem
; i
++)
9693 printf(gettext("0x%x "), val
[i
]);
9698 case DATA_TYPE_UINT8_ARRAY
: {
9702 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9703 for (i
= 0; i
< nelem
; i
++)
9704 printf(gettext("0x%x "), val
[i
]);
9709 case DATA_TYPE_INT16_ARRAY
: {
9713 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9714 for (i
= 0; i
< nelem
; i
++)
9715 printf(gettext("0x%x "), val
[i
]);
9720 case DATA_TYPE_UINT16_ARRAY
: {
9724 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9725 for (i
= 0; i
< nelem
; i
++)
9726 printf(gettext("0x%x "), val
[i
]);
9731 case DATA_TYPE_INT32_ARRAY
: {
9735 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9736 for (i
= 0; i
< nelem
; i
++)
9737 printf(gettext("0x%x "), val
[i
]);
9742 case DATA_TYPE_UINT32_ARRAY
: {
9746 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9747 for (i
= 0; i
< nelem
; i
++)
9748 printf(gettext("0x%x "), val
[i
]);
9753 case DATA_TYPE_INT64_ARRAY
: {
9757 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9758 for (i
= 0; i
< nelem
; i
++)
9759 printf(gettext("0x%llx "),
9760 (u_longlong_t
)val
[i
]);
9765 case DATA_TYPE_UINT64_ARRAY
: {
9769 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9770 for (i
= 0; i
< nelem
; i
++)
9771 printf(gettext("0x%llx "),
9772 (u_longlong_t
)val
[i
]);
9777 case DATA_TYPE_STRING_ARRAY
: {
9781 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9782 for (i
= 0; i
< nelem
; i
++)
9783 printf(gettext("\"%s\" "),
9784 str
[i
] ? str
[i
] : "<NULL>");
9789 case DATA_TYPE_BOOLEAN_ARRAY
:
9790 case DATA_TYPE_BYTE_ARRAY
:
9791 case DATA_TYPE_DOUBLE
:
9792 case DATA_TYPE_DONTCARE
:
9793 case DATA_TYPE_UNKNOWN
:
9794 printf(gettext("<unknown>"));
9798 printf(gettext("\n"));
9803 zpool_do_events_next(ev_opts_t
*opts
)
9806 int zevent_fd
, ret
, dropped
;
9809 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9810 VERIFY(zevent_fd
>= 0);
9812 if (!opts
->scripted
)
9813 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9816 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9817 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9818 if (ret
|| nvl
== NULL
)
9822 (void) printf(gettext("dropped %d events\n"), dropped
);
9824 if (strlen(opts
->poolname
) > 0 &&
9825 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9826 strcmp(opts
->poolname
, pool
) != 0)
9829 zpool_do_events_short(nvl
, opts
);
9831 if (opts
->verbose
) {
9832 zpool_do_events_nvprint(nvl
, 8);
9833 printf(gettext("\n"));
9835 (void) fflush(stdout
);
9840 VERIFY(0 == close(zevent_fd
));
9846 zpool_do_events_clear(void)
9850 ret
= zpool_events_clear(g_zfs
, &count
);
9852 (void) printf(gettext("cleared %d events\n"), count
);
9858 * zpool events [-vHf [pool] | -c]
9860 * Displays events logs by ZFS.
9863 zpool_do_events(int argc
, char **argv
)
9865 ev_opts_t opts
= { 0 };
9870 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9885 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9894 (void) fprintf(stderr
, gettext("too many arguments\n"));
9896 } else if (argc
== 1) {
9897 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9898 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9899 (void) fprintf(stderr
,
9900 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9905 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9907 (void) fprintf(stderr
,
9908 gettext("invalid options combined with -c\n"));
9913 ret
= zpool_do_events_clear();
9915 ret
= zpool_do_events_next(&opts
);
9921 get_callback_vdev(zpool_handle_t
*zhp
, char *vdevname
, void *data
)
9923 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9924 char value
[ZFS_MAXPROPLEN
];
9925 zprop_source_t srctype
;
9927 for (zprop_list_t
*pl
= cbp
->cb_proplist
; pl
!= NULL
;
9931 * If the first property is pool name, it is a special
9932 * placeholder that we can skip. This will also skip
9933 * over the name property when 'all' is specified.
9935 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9936 pl
== cbp
->cb_proplist
)
9939 if (pl
->pl_prop
== ZPROP_INVAL
) {
9940 prop_name
= pl
->pl_user_prop
;
9942 prop_name
= (char *)vdev_prop_to_name(pl
->pl_prop
);
9944 if (zpool_get_vdev_prop(zhp
, vdevname
, pl
->pl_prop
,
9945 prop_name
, value
, sizeof (value
), &srctype
,
9946 cbp
->cb_literal
) == 0) {
9947 zprop_print_one_property(vdevname
, cbp
, prop_name
,
9948 value
, srctype
, NULL
, NULL
);
9956 get_callback_vdev_width_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9958 zpool_handle_t
*zhp
= zhp_data
;
9959 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9960 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9961 cbp
->cb_vdevs
.cb_name_flags
);
9964 /* Adjust the column widths for the vdev properties */
9965 ret
= vdev_expand_proplist(zhp
, vdevname
, &cbp
->cb_proplist
);
9971 get_callback_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9973 zpool_handle_t
*zhp
= zhp_data
;
9974 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9975 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9976 cbp
->cb_vdevs
.cb_name_flags
);
9979 /* Display the properties */
9980 ret
= get_callback_vdev(zhp
, vdevname
, data
);
9986 get_callback(zpool_handle_t
*zhp
, void *data
)
9988 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9989 char value
[MAXNAMELEN
];
9990 zprop_source_t srctype
;
9994 if (cbp
->cb_type
== ZFS_TYPE_VDEV
) {
9995 if (strcmp(cbp
->cb_vdevs
.cb_names
[0], "all-vdevs") == 0) {
9996 for_each_vdev(zhp
, get_callback_vdev_width_cb
, data
);
9997 for_each_vdev(zhp
, get_callback_vdev_cb
, data
);
9999 /* Adjust column widths for vdev properties */
10000 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10002 vdev_expand_proplist(zhp
,
10003 cbp
->cb_vdevs
.cb_names
[vid
],
10004 &cbp
->cb_proplist
);
10006 /* Display the properties */
10007 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10009 get_callback_vdev(zhp
,
10010 cbp
->cb_vdevs
.cb_names
[vid
], data
);
10014 assert(cbp
->cb_type
== ZFS_TYPE_POOL
);
10015 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
10017 * Skip the special fake placeholder. This will also
10018 * skip over the name property when 'all' is specified.
10020 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
10021 pl
== cbp
->cb_proplist
)
10024 if (pl
->pl_prop
== ZPROP_INVAL
&&
10025 (zpool_prop_feature(pl
->pl_user_prop
) ||
10026 zpool_prop_unsupported(pl
->pl_user_prop
))) {
10027 srctype
= ZPROP_SRC_LOCAL
;
10029 if (zpool_prop_get_feature(zhp
,
10030 pl
->pl_user_prop
, value
,
10031 sizeof (value
)) == 0) {
10032 zprop_print_one_property(
10033 zpool_get_name(zhp
), cbp
,
10034 pl
->pl_user_prop
, value
, srctype
,
10038 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
10039 sizeof (value
), &srctype
,
10040 cbp
->cb_literal
) != 0)
10043 zprop_print_one_property(zpool_get_name(zhp
),
10044 cbp
, zpool_prop_to_name(pl
->pl_prop
),
10045 value
, srctype
, NULL
, NULL
);
10054 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10056 * -H Scripted mode. Don't display headers, and separate properties
10058 * -o List of columns to display. Defaults to
10059 * "name,property,value,source".
10060 * -p Display values in parsable (exact) format.
10062 * Get properties of pools in the system. Output space statistics
10063 * for each one as well as other attributes.
10066 zpool_do_get(int argc
, char **argv
)
10068 zprop_get_cbdata_t cb
= { 0 };
10069 zprop_list_t fake_name
= { 0 };
10072 char *propstr
= NULL
;
10074 cb
.cb_first
= B_TRUE
;
10077 * Set up default columns and sources.
10079 cb
.cb_sources
= ZPROP_SRC_ALL
;
10080 cb
.cb_columns
[0] = GET_COL_NAME
;
10081 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
10082 cb
.cb_columns
[2] = GET_COL_VALUE
;
10083 cb
.cb_columns
[3] = GET_COL_SOURCE
;
10084 cb
.cb_type
= ZFS_TYPE_POOL
;
10085 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10086 current_prop_type
= cb
.cb_type
;
10088 /* check options */
10089 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
10092 cb
.cb_literal
= B_TRUE
;
10095 cb
.cb_scripted
= B_TRUE
;
10098 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
10101 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10102 static const char *const col_opts
[] =
10103 { "name", "property", "value", "source",
10105 static const zfs_get_column_t col_cols
[] =
10106 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
10109 if (i
== ZFS_GET_NCOLS
- 1) {
10110 (void) fprintf(stderr
, gettext("too "
10111 "many fields given to -o "
10116 for (c
= 0; c
< ARRAY_SIZE(col_opts
); ++c
)
10117 if (strcmp(tok
, col_opts
[c
]) == 0)
10120 (void) fprintf(stderr
,
10121 gettext("invalid column name '%s'\n"), tok
);
10127 (void) fprintf(stderr
,
10128 gettext("\"all\" conflicts "
10129 "with specific fields "
10130 "given to -o option\n"));
10134 memcpy(cb
.cb_columns
, col_cols
,
10135 sizeof (col_cols
));
10136 i
= ZFS_GET_NCOLS
- 1;
10138 cb
.cb_columns
[i
++] = col_cols
[c
];
10142 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10152 (void) fprintf(stderr
, gettext("missing property "
10157 /* Properties list is needed later by zprop_get_list() */
10164 /* No args, so just print the defaults. */
10165 } else if (are_all_pools(argc
, argv
)) {
10166 /* All the args are pool names */
10167 } else if (are_all_pools(1, argv
)) {
10168 /* The first arg is a pool name */
10169 if ((argc
== 2 && strcmp(argv
[1], "all-vdevs") == 0) ||
10170 are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10172 /* ... and the rest are vdev names */
10173 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10174 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
10175 cb
.cb_type
= ZFS_TYPE_VDEV
;
10176 argc
= 1; /* One pool to process */
10178 fprintf(stderr
, gettext("Expected a list of vdevs in"
10179 " \"%s\", but got:\n"), argv
[0]);
10180 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
10181 argv
[0], &cb
.cb_vdevs
);
10182 fprintf(stderr
, "\n");
10188 * The first arg isn't a pool name,
10190 fprintf(stderr
, gettext("missing pool name.\n"));
10191 fprintf(stderr
, "\n");
10196 if (zprop_get_list(g_zfs
, propstr
, &cb
.cb_proplist
,
10197 cb
.cb_type
) != 0) {
10198 /* Use correct list of valid properties (pool or vdev) */
10199 current_prop_type
= cb
.cb_type
;
10203 if (cb
.cb_proplist
!= NULL
) {
10204 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
10205 fake_name
.pl_width
= strlen(gettext("NAME"));
10206 fake_name
.pl_next
= cb
.cb_proplist
;
10207 cb
.cb_proplist
= &fake_name
;
10210 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_type
,
10211 cb
.cb_literal
, get_callback
, &cb
);
10213 if (cb
.cb_proplist
== &fake_name
)
10214 zprop_free_list(fake_name
.pl_next
);
10216 zprop_free_list(cb
.cb_proplist
);
10221 typedef struct set_cbdata
{
10224 zfs_type_t cb_type
;
10225 vdev_cbdata_t cb_vdevs
;
10226 boolean_t cb_any_successful
;
10230 set_pool_callback(zpool_handle_t
*zhp
, set_cbdata_t
*cb
)
10234 /* Check if we have out-of-bounds features */
10235 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
10236 boolean_t features
[SPA_FEATURES
];
10237 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
10238 ZPOOL_COMPATIBILITY_OK
)
10241 nvlist_t
*enabled
= zpool_get_features(zhp
);
10243 for (i
= 0; i
< SPA_FEATURES
; i
++) {
10244 const char *fguid
= spa_feature_table
[i
].fi_guid
;
10245 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
10248 if (i
< SPA_FEATURES
)
10249 (void) fprintf(stderr
, gettext("Warning: one or "
10250 "more features already enabled on pool '%s'\n"
10251 "are not present in this compatibility set.\n"),
10252 zpool_get_name(zhp
));
10255 /* if we're setting a feature, check it's in compatibility set */
10256 if (zpool_prop_feature(cb
->cb_propname
) &&
10257 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
10258 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
10261 if (zfeature_lookup_name(fname
, &f
) == 0) {
10262 char compat
[ZFS_MAXPROPLEN
];
10263 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
10264 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
10267 boolean_t features
[SPA_FEATURES
];
10268 if (zpool_do_load_compat(compat
, features
) !=
10269 ZPOOL_COMPATIBILITY_OK
) {
10270 (void) fprintf(stderr
, gettext("Error: "
10271 "cannot enable feature '%s' on pool '%s'\n"
10272 "because the pool's 'compatibility' "
10273 "property cannot be parsed.\n"),
10274 fname
, zpool_get_name(zhp
));
10278 if (!features
[f
]) {
10279 (void) fprintf(stderr
, gettext("Error: "
10280 "cannot enable feature '%s' on pool '%s'\n"
10281 "as it is not specified in this pool's "
10282 "current compatibility set.\n"
10283 "Consider setting 'compatibility' to a "
10284 "less restrictive set, or to 'off'.\n"),
10285 fname
, zpool_get_name(zhp
));
10291 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
10297 set_callback(zpool_handle_t
*zhp
, void *data
)
10300 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
10302 if (cb
->cb_type
== ZFS_TYPE_VDEV
) {
10303 error
= zpool_set_vdev_prop(zhp
, *cb
->cb_vdevs
.cb_names
,
10304 cb
->cb_propname
, cb
->cb_value
);
10306 assert(cb
->cb_type
== ZFS_TYPE_POOL
);
10307 error
= set_pool_callback(zhp
, cb
);
10310 cb
->cb_any_successful
= !error
;
10315 zpool_do_set(int argc
, char **argv
)
10317 set_cbdata_t cb
= { 0 };
10320 current_prop_type
= ZFS_TYPE_POOL
;
10321 if (argc
> 1 && argv
[1][0] == '-') {
10322 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10328 (void) fprintf(stderr
, gettext("missing property=value "
10334 (void) fprintf(stderr
, gettext("missing pool name\n"));
10339 (void) fprintf(stderr
, gettext("too many pool names\n"));
10343 cb
.cb_propname
= argv
[1];
10344 cb
.cb_type
= ZFS_TYPE_POOL
;
10345 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10346 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
10347 if (cb
.cb_value
== NULL
) {
10348 (void) fprintf(stderr
, gettext("missing value in "
10349 "property=value argument\n"));
10353 *(cb
.cb_value
) = '\0';
10358 if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
10359 /* Argument is a vdev */
10360 cb
.cb_vdevs
.cb_names
= argv
;
10361 cb
.cb_vdevs
.cb_names_count
= 1;
10362 cb
.cb_type
= ZFS_TYPE_VDEV
;
10363 argc
= 0; /* No pools to process */
10364 } else if (are_all_pools(1, argv
)) {
10365 /* The first arg is a pool name */
10366 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10368 /* 2nd argument is a vdev */
10369 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10370 cb
.cb_vdevs
.cb_names_count
= 1;
10371 cb
.cb_type
= ZFS_TYPE_VDEV
;
10372 argc
= 1; /* One pool to process */
10373 } else if (argc
> 1) {
10374 (void) fprintf(stderr
,
10375 gettext("too many pool names\n"));
10380 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
10381 B_FALSE
, set_callback
, &cb
);
10386 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10388 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
10390 uint64_t bytes_remaining
;
10392 uint_t c
, children
;
10395 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
10396 activity
== ZPOOL_WAIT_TRIM
);
10398 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
10399 (uint64_t **)&vs
, &c
) == 0);
10401 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
10402 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
10403 bytes_remaining
= vs
->vs_initialize_bytes_est
-
10404 vs
->vs_initialize_bytes_done
;
10405 else if (activity
== ZPOOL_WAIT_TRIM
&&
10406 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
10407 bytes_remaining
= vs
->vs_trim_bytes_est
-
10408 vs
->vs_trim_bytes_done
;
10410 bytes_remaining
= 0;
10412 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10413 &child
, &children
) != 0)
10416 for (c
= 0; c
< children
; c
++)
10417 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
10419 return (bytes_remaining
);
10422 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10424 vdev_activity_top_remaining(nvlist_t
*nv
)
10426 uint64_t bytes_remaining
= 0;
10431 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10432 &child
, &children
) != 0)
10435 for (uint_t c
= 0; c
< children
; c
++) {
10436 vdev_rebuild_stat_t
*vrs
;
10439 error
= nvlist_lookup_uint64_array(child
[c
],
10440 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
10442 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
10443 bytes_remaining
+= (vrs
->vrs_bytes_est
-
10444 vrs
->vrs_bytes_rebuilt
);
10449 return (bytes_remaining
);
10452 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10454 vdev_any_spare_replacing(nvlist_t
*nv
)
10457 uint_t c
, children
;
10460 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
10462 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
10463 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
10464 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
10468 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10469 &child
, &children
) != 0)
10472 for (c
= 0; c
< children
; c
++) {
10473 if (vdev_any_spare_replacing(child
[c
]))
10480 typedef struct wait_data
{
10482 boolean_t wd_scripted
;
10483 boolean_t wd_exact
;
10484 boolean_t wd_headers_once
;
10485 boolean_t wd_should_exit
;
10486 /* Which activities to wait for */
10487 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10489 pthread_cond_t wd_cv
;
10490 pthread_mutex_t wd_mutex
;
10494 * Print to stdout a single line, containing one column for each activity that
10495 * we are waiting for specifying how many bytes of work are left for that
10499 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10501 nvlist_t
*config
, *nvroot
;
10504 pool_checkpoint_stat_t
*pcs
= NULL
;
10505 pool_scan_stat_t
*pss
= NULL
;
10506 pool_removal_stat_t
*prs
= NULL
;
10507 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
10508 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10509 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10511 /* Calculate the width of each column */
10512 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10514 * Make sure we have enough space in the col for pretty-printed
10515 * numbers and for the column header, and then leave a couple
10516 * spaces between cols for readability.
10518 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10521 /* Print header if appropriate */
10522 int term_height
= terminal_height();
10523 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10524 row
% (term_height
-1) == 0);
10525 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10526 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10527 if (wd
->wd_enabled
[i
])
10528 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10530 (void) printf("\n");
10533 /* Bytes of work remaining in each activity */
10534 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10536 bytes_rem
[ZPOOL_WAIT_FREE
] =
10537 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10539 config
= zpool_get_config(zhp
, NULL
);
10540 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10542 (void) nvlist_lookup_uint64_array(nvroot
,
10543 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10544 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10545 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10547 (void) nvlist_lookup_uint64_array(nvroot
,
10548 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10549 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10550 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10553 (void) nvlist_lookup_uint64_array(nvroot
,
10554 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10555 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10556 pss
->pss_pass_scrub_pause
== 0) {
10557 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10558 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10559 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10561 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10562 } else if (check_rebuilding(nvroot
, NULL
)) {
10563 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10564 vdev_activity_top_remaining(nvroot
);
10567 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10568 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10569 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10570 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10573 * A replace finishes after resilvering finishes, so the amount of work
10574 * left for a replace is the same as for resilvering.
10576 * It isn't quite correct to say that if we have any 'spare' or
10577 * 'replacing' vdevs and a resilver is happening, then a replace is in
10578 * progress, like we do here. When a hot spare is used, the faulted vdev
10579 * is not removed after the hot spare is resilvered, so parent 'spare'
10580 * vdev is not removed either. So we could have a 'spare' vdev, but be
10581 * resilvering for a different reason. However, we use it as a heuristic
10582 * because we don't have access to the DTLs, which could tell us whether
10583 * or not we have really finished resilvering a hot spare.
10585 if (vdev_any_spare_replacing(nvroot
))
10586 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10588 if (timestamp_fmt
!= NODATE
)
10589 print_timestamp(timestamp_fmt
);
10591 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10593 if (!wd
->wd_enabled
[i
])
10597 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10600 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10602 if (wd
->wd_scripted
)
10603 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10605 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10607 (void) printf("\n");
10608 (void) fflush(stdout
);
10612 wait_status_thread(void *arg
)
10614 wait_data_t
*wd
= (wait_data_t
*)arg
;
10615 zpool_handle_t
*zhp
;
10617 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10618 return (void *)(1);
10620 for (int row
= 0; ; row
++) {
10622 struct timespec timeout
;
10624 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10626 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10627 zpool_props_refresh(zhp
) != 0) {
10629 return (void *)(uintptr_t)(missing
? 0 : 1);
10632 print_wait_status_row(wd
, zhp
, row
);
10634 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10635 long nanos
= timeout
.tv_nsec
+
10636 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10637 if (nanos
>= NANOSEC
) {
10639 timeout
.tv_nsec
= nanos
- NANOSEC
;
10641 timeout
.tv_nsec
= nanos
;
10643 pthread_mutex_lock(&wd
->wd_mutex
);
10644 if (!wd
->wd_should_exit
)
10645 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10647 pthread_mutex_unlock(&wd
->wd_mutex
);
10649 break; /* signaled by main thread */
10650 } else if (ret
!= ETIMEDOUT
) {
10651 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10652 "failed: %s\n"), strerror(ret
));
10654 return (void *)(uintptr_t)(1);
10659 return (void *)(0);
10663 zpool_do_wait(int argc
, char **argv
)
10665 boolean_t verbose
= B_FALSE
;
10667 unsigned long count
;
10668 pthread_t status_thr
;
10670 zpool_handle_t
*zhp
;
10673 wd
.wd_scripted
= B_FALSE
;
10674 wd
.wd_exact
= B_FALSE
;
10675 wd
.wd_headers_once
= B_FALSE
;
10676 wd
.wd_should_exit
= B_FALSE
;
10678 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10679 pthread_cond_init(&wd
.wd_cv
, NULL
);
10681 /* By default, wait for all types of activity. */
10682 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10683 wd
.wd_enabled
[i
] = B_TRUE
;
10685 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10688 wd
.wd_scripted
= B_TRUE
;
10691 wd
.wd_headers_once
= B_TRUE
;
10694 wd
.wd_exact
= B_TRUE
;
10697 get_timestamp_arg(*optarg
);
10700 /* Reset activities array */
10701 memset(&wd
.wd_enabled
, 0, sizeof (wd
.wd_enabled
));
10703 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10704 static const char *const col_opts
[] = {
10705 "discard", "free", "initialize", "replace",
10706 "remove", "resilver", "scrub", "trim" };
10708 for (i
= 0; i
< ARRAY_SIZE(col_opts
); ++i
)
10709 if (strcmp(tok
, col_opts
[i
]) == 0) {
10710 wd
.wd_enabled
[i
] = B_TRUE
;
10714 (void) fprintf(stderr
,
10715 gettext("invalid activity '%s'\n"), tok
);
10721 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10730 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10732 /* This subcmd only accepts an interval, not a count */
10733 (void) fprintf(stderr
, gettext("too many arguments\n"));
10737 if (wd
.wd_interval
!= 0)
10741 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10745 (void) fprintf(stderr
, gettext("too many arguments\n"));
10749 wd
.wd_poolname
= argv
[0];
10751 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10756 * We use a separate thread for printing status updates because
10757 * the main thread will call lzc_wait(), which blocks as long
10758 * as an activity is in progress, which can be a long time.
10760 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10762 (void) fprintf(stderr
, gettext("failed to create status"
10763 "thread: %s\n"), strerror(errno
));
10770 * Loop over all activities that we are supposed to wait for until none
10771 * of them are in progress. Note that this means we can end up waiting
10772 * for more activities to complete than just those that were in progress
10773 * when we began waiting; if an activity we are interested in begins
10774 * while we are waiting for another activity, we will wait for both to
10775 * complete before exiting.
10778 boolean_t missing
= B_FALSE
;
10779 boolean_t any_waited
= B_FALSE
;
10781 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10784 if (!wd
.wd_enabled
[i
])
10787 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10788 if (error
!= 0 || missing
)
10791 any_waited
= (any_waited
|| waited
);
10794 if (error
!= 0 || missing
|| !any_waited
)
10802 pthread_mutex_lock(&wd
.wd_mutex
);
10803 wd
.wd_should_exit
= B_TRUE
;
10804 pthread_cond_signal(&wd
.wd_cv
);
10805 pthread_mutex_unlock(&wd
.wd_mutex
);
10806 (void) pthread_join(status_thr
, (void *)&status
);
10811 pthread_mutex_destroy(&wd
.wd_mutex
);
10812 pthread_cond_destroy(&wd
.wd_cv
);
10817 find_command_idx(char *command
, int *idx
)
10821 for (i
= 0; i
< NCOMMAND
; i
++) {
10822 if (command_table
[i
].name
== NULL
)
10825 if (strcmp(command
, command_table
[i
].name
) == 0) {
10834 * Display version message
10837 zpool_do_version(int argc
, char **argv
)
10839 (void) argc
, (void) argv
;
10841 if (zfs_version_print() == -1)
10848 * Do zpool_load_compat() and print error message on failure
10850 static zpool_compat_status_t
10851 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
10855 zpool_compat_status_t ret
;
10857 ret
= zpool_load_compat(compat
, list
, report
, 1024);
10860 case ZPOOL_COMPATIBILITY_OK
:
10863 case ZPOOL_COMPATIBILITY_NOFILES
:
10864 case ZPOOL_COMPATIBILITY_BADFILE
:
10865 case ZPOOL_COMPATIBILITY_BADTOKEN
:
10866 (void) fprintf(stderr
, "Error: %s\n", report
);
10869 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
10870 (void) fprintf(stderr
, "Warning: %s\n", report
);
10871 ret
= ZPOOL_COMPATIBILITY_OK
;
10878 main(int argc
, char **argv
)
10885 (void) setlocale(LC_ALL
, "");
10886 (void) setlocale(LC_NUMERIC
, "C");
10887 (void) textdomain(TEXT_DOMAIN
);
10893 * Make sure the user has specified some command.
10896 (void) fprintf(stderr
, gettext("missing command\n"));
10903 * Special case '-?'
10905 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10909 * Special case '-V|--version'
10911 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10912 return (zpool_do_version(argc
, argv
));
10914 if ((g_zfs
= libzfs_init()) == NULL
) {
10915 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10919 libzfs_print_on_error(g_zfs
, B_TRUE
);
10921 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10924 * Many commands modify input strings for string parsing reasons.
10925 * We create a copy to protect the original argv.
10927 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10928 for (i
= 0; i
< argc
; i
++)
10929 newargv
[i
] = strdup(argv
[i
]);
10930 newargv
[argc
] = NULL
;
10933 * Run the appropriate command.
10935 if (find_command_idx(cmdname
, &i
) == 0) {
10936 current_command
= &command_table
[i
];
10937 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10938 } else if (strchr(cmdname
, '=')) {
10939 verify(find_command_idx("set", &i
) == 0);
10940 current_command
= &command_table
[i
];
10941 ret
= command_table
[i
].func(argc
, newargv
);
10942 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10944 * 'freeze' is a vile debugging abomination, so we treat
10947 zfs_cmd_t zc
= {"\0"};
10949 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10950 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10952 (void) fprintf(stderr
,
10953 gettext("failed to freeze pool: %d\n"), errno
);
10959 (void) fprintf(stderr
, gettext("unrecognized "
10960 "command '%s'\n"), cmdname
);
10965 for (i
= 0; i
< argc
; i
++)
10969 if (ret
== 0 && log_history
)
10970 (void) zpool_log_history(g_zfs
, history_str
);
10972 libzfs_fini(g_zfs
);
10975 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10976 * for the purposes of running ::findleaks.
10978 if (getenv("ZFS_ABORT") != NULL
) {
10979 (void) printf("dumping core by request\n");