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 https://opensource.org/licenses/CDDL-1.0.
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))) {
551 (void) fprintf(fp
, "%s",
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
;
714 char *class = (char *)"";
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 = (char *)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
, const 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
, const char *propval
,
913 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
916 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
920 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
922 * -f Force addition of devices, even if they appear in use
923 * -g Display guid for individual vdev name.
924 * -L Follow links when resolving vdev path name.
925 * -n Do not add the devices, but display the resulting layout if
926 * they were to be added.
927 * -o Set property=value.
928 * -P Display full path for vdev name.
930 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
931 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
935 zpool_do_add(int argc
, char **argv
)
937 boolean_t force
= B_FALSE
;
938 boolean_t dryrun
= B_FALSE
;
946 nvlist_t
*props
= NULL
;
950 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
956 name_flags
|= VDEV_NAME_GUID
;
959 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
965 if ((propval
= strchr(optarg
, '=')) == NULL
) {
966 (void) fprintf(stderr
, gettext("missing "
967 "'=' for -o option\n"));
973 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
974 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
978 name_flags
|= VDEV_NAME_PATH
;
981 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
990 /* get pool name and check number of arguments */
992 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
996 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1005 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1008 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
1009 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
1015 /* unless manually specified use "ashift" pool property (if set) */
1016 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
1019 char strval
[ZPOOL_MAXPROPLEN
];
1021 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
1022 if (src
!= ZPROP_SRC_DEFAULT
) {
1023 (void) sprintf(strval
, "%" PRId32
, intval
);
1024 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
1025 &props
, B_TRUE
) == 0);
1029 /* pass off to make_root_vdev for processing */
1030 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
1032 if (nvroot
== NULL
) {
1038 nvlist_t
*poolnvroot
;
1039 nvlist_t
**l2child
, **sparechild
;
1040 uint_t l2children
, sparechildren
, c
;
1042 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1044 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1047 (void) printf(gettext("would update '%s' to the following "
1048 "configuration:\n\n"), zpool_get_name(zhp
));
1050 /* print original main pool and new tree */
1051 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1052 name_flags
| VDEV_NAME_TYPE_ID
);
1053 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1055 /* print other classes: 'dedup', 'special', and 'log' */
1056 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1057 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1058 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1059 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1060 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1061 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1062 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1063 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1066 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1067 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1068 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1069 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1070 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1071 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1072 print_vdev_tree(zhp
, "special", nvroot
, 0,
1073 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1076 if (num_logs(poolnvroot
) > 0) {
1077 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1078 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1079 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1080 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1081 } else if (num_logs(nvroot
) > 0) {
1082 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1083 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1086 /* Do the same for the caches */
1087 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1088 &l2child
, &l2children
) == 0 && l2children
) {
1090 (void) printf(gettext("\tcache\n"));
1091 for (c
= 0; c
< l2children
; c
++) {
1092 vname
= zpool_vdev_name(g_zfs
, NULL
,
1093 l2child
[c
], name_flags
);
1094 (void) printf("\t %s\n", vname
);
1098 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1099 &l2child
, &l2children
) == 0 && l2children
) {
1101 (void) printf(gettext("\tcache\n"));
1102 for (c
= 0; c
< l2children
; c
++) {
1103 vname
= zpool_vdev_name(g_zfs
, NULL
,
1104 l2child
[c
], name_flags
);
1105 (void) printf("\t %s\n", vname
);
1109 /* And finally the spares */
1110 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1111 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1113 (void) printf(gettext("\tspares\n"));
1114 for (c
= 0; c
< sparechildren
; c
++) {
1115 vname
= zpool_vdev_name(g_zfs
, NULL
,
1116 sparechild
[c
], name_flags
);
1117 (void) printf("\t %s\n", vname
);
1121 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1122 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1124 (void) printf(gettext("\tspares\n"));
1125 for (c
= 0; c
< sparechildren
; c
++) {
1126 vname
= zpool_vdev_name(g_zfs
, NULL
,
1127 sparechild
[c
], name_flags
);
1128 (void) printf("\t %s\n", vname
);
1135 ret
= (zpool_add(zhp
, nvroot
) != 0);
1139 nvlist_free(nvroot
);
1146 * zpool remove [-npsw] <pool> <vdev> ...
1148 * Removes the given vdev from the pool.
1151 zpool_do_remove(int argc
, char **argv
)
1155 zpool_handle_t
*zhp
= NULL
;
1156 boolean_t stop
= B_FALSE
;
1158 boolean_t noop
= B_FALSE
;
1159 boolean_t parsable
= B_FALSE
;
1160 boolean_t wait
= B_FALSE
;
1163 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1178 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1187 /* get pool name and check number of arguments */
1189 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1195 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1200 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1206 (void) fprintf(stderr
, gettext("too many arguments\n"));
1209 if (zpool_vdev_remove_cancel(zhp
) != 0)
1212 (void) fprintf(stderr
, gettext("invalid option "
1213 "combination: -w cannot be used with -s\n"));
1218 (void) fprintf(stderr
, gettext("missing device\n"));
1222 for (i
= 1; i
< argc
; i
++) {
1226 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1232 (void) printf("%s %llu\n",
1233 argv
[i
], (unsigned long long)size
);
1236 zfs_nicenum(size
, valstr
,
1238 (void) printf("Memory that will be "
1239 "used after removing %s: %s\n",
1243 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1248 if (ret
== 0 && wait
)
1249 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1257 * Return 1 if a vdev is active (being used in a pool)
1258 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1260 * This is useful for checking if a disk in an active pool is offlined or
1264 vdev_is_active(char *vdev_path
)
1267 fd
= open(vdev_path
, O_EXCL
);
1269 return (1); /* cant open O_EXCL - disk is active */
1273 return (0); /* disk is inactive in the pool */
1277 * zpool labelclear [-f] <vdev>
1279 * -f Force clearing the label for the vdevs which are members of
1280 * the exported or foreign pools.
1282 * Verifies that the vdev is not active and zeros out the label information
1286 zpool_do_labelclear(int argc
, char **argv
)
1288 char vdev
[MAXPATHLEN
];
1291 int c
, fd
= -1, ret
= 0;
1294 boolean_t inuse
= B_FALSE
;
1295 boolean_t force
= B_FALSE
;
1298 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1304 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1315 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1319 (void) fprintf(stderr
, gettext("too many arguments\n"));
1324 * Check if we were given absolute path and use it as is.
1325 * Otherwise if the provided vdev name doesn't point to a file,
1326 * try prepending expected disk paths and partition numbers.
1328 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1329 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1332 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1333 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1334 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1338 if (error
|| (stat(vdev
, &st
) != 0)) {
1339 (void) fprintf(stderr
, gettext(
1340 "failed to find device %s, try specifying absolute "
1341 "path instead\n"), argv
[0]);
1346 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1347 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1348 vdev
, strerror(errno
));
1353 * Flush all dirty pages for the block device. This should not be
1354 * fatal when the device does not support BLKFLSBUF as would be the
1355 * case for a file vdev.
1357 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1358 (void) fprintf(stderr
, gettext("failed to invalidate "
1359 "cache for %s: %s\n"), vdev
, strerror(errno
));
1361 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1362 (void) fprintf(stderr
,
1363 gettext("failed to read label from %s\n"), vdev
);
1367 nvlist_free(config
);
1369 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1371 (void) fprintf(stderr
,
1372 gettext("failed to check state for %s\n"), vdev
);
1382 case POOL_STATE_ACTIVE
:
1383 case POOL_STATE_SPARE
:
1384 case POOL_STATE_L2CACHE
:
1386 * We allow the user to call 'zpool offline -f'
1387 * on an offlined disk in an active pool. We can check if
1388 * the disk is online by calling vdev_is_active().
1390 if (force
&& !vdev_is_active(vdev
))
1393 (void) fprintf(stderr
, gettext(
1394 "%s is a member (%s) of pool \"%s\""),
1395 vdev
, zpool_pool_state_to_name(state
), name
);
1398 (void) fprintf(stderr
, gettext(
1399 ". Offline the disk first to clear its label."));
1405 case POOL_STATE_EXPORTED
:
1408 (void) fprintf(stderr
, gettext(
1409 "use '-f' to override the following error:\n"
1410 "%s is a member of exported pool \"%s\"\n"),
1415 case POOL_STATE_POTENTIALLY_ACTIVE
:
1418 (void) fprintf(stderr
, gettext(
1419 "use '-f' to override the following error:\n"
1420 "%s is a member of potentially active pool \"%s\"\n"),
1425 case POOL_STATE_DESTROYED
:
1426 /* inuse should never be set for a destroyed pool */
1432 ret
= zpool_clear_label(fd
);
1434 (void) fprintf(stderr
,
1435 gettext("failed to clear label for %s\n"), vdev
);
1446 * zpool create [-fnd] [-o property=value] ...
1447 * [-O file-system-property=value] ...
1448 * [-R root] [-m mountpoint] <pool> <dev> ...
1450 * -f Force creation, even if devices appear in use
1451 * -n Do not create the pool, but display the resulting layout if it
1452 * were to be created.
1453 * -R Create a pool under an alternate root
1454 * -m Set default mountpoint for the root dataset. By default it's
1456 * -o Set property=value.
1457 * -o Set feature@feature=enabled|disabled.
1458 * -d Don't automatically enable all supported pool features
1459 * (individual features can be enabled with -o).
1460 * -O Set fsproperty=value in the pool's root file system
1462 * Creates the named pool according to the given vdev specification. The
1463 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1464 * Once we get the nvlist back from make_root_vdev(), we either print out the
1465 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1468 zpool_do_create(int argc
, char **argv
)
1470 boolean_t force
= B_FALSE
;
1471 boolean_t dryrun
= B_FALSE
;
1472 boolean_t enable_pool_features
= B_TRUE
;
1475 nvlist_t
*nvroot
= NULL
;
1479 char *altroot
= NULL
;
1480 char *compat
= NULL
;
1481 char *mountpoint
= NULL
;
1482 nvlist_t
*fsprops
= NULL
;
1483 nvlist_t
*props
= NULL
;
1487 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1496 enable_pool_features
= B_FALSE
;
1500 if (add_prop_list(zpool_prop_to_name(
1501 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1503 if (add_prop_list_default(zpool_prop_to_name(
1504 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1508 /* Equivalent to -O mountpoint=optarg */
1509 mountpoint
= optarg
;
1512 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1513 (void) fprintf(stderr
, gettext("missing "
1514 "'=' for -o option\n"));
1520 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1524 * If the user is creating a pool that doesn't support
1525 * feature flags, don't enable any features.
1527 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1531 ver
= strtoull(propval
, &end
, 10);
1533 ver
< SPA_VERSION_FEATURES
) {
1534 enable_pool_features
= B_FALSE
;
1537 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1539 if (zpool_name_to_prop(optarg
) ==
1540 ZPOOL_PROP_COMPATIBILITY
)
1544 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1545 (void) fprintf(stderr
, gettext("missing "
1546 "'=' for -O option\n"));
1553 * Mountpoints are checked and then added later.
1554 * Uniquely among properties, they can be specified
1555 * more than once, to avoid conflict with -m.
1557 if (0 == strcmp(optarg
,
1558 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1559 mountpoint
= propval
;
1560 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1567 * Sanity check temporary pool name.
1569 if (strchr(optarg
, '/') != NULL
) {
1570 (void) fprintf(stderr
, gettext("cannot create "
1571 "'%s': invalid character '/' in temporary "
1573 (void) fprintf(stderr
, gettext("use 'zfs "
1574 "create' to create a dataset\n"));
1578 if (add_prop_list(zpool_prop_to_name(
1579 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1581 if (add_prop_list_default(zpool_prop_to_name(
1582 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1587 (void) fprintf(stderr
, gettext("missing argument for "
1588 "'%c' option\n"), optopt
);
1591 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1600 /* get pool name and check number of arguments */
1602 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1606 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1613 * As a special case, check for use of '/' in the name, and direct the
1614 * user to use 'zfs create' instead.
1616 if (strchr(poolname
, '/') != NULL
) {
1617 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1618 "character '/' in pool name\n"), poolname
);
1619 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1620 "create a dataset\n"));
1624 /* pass off to make_root_vdev for bulk processing */
1625 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1626 argc
- 1, argv
+ 1);
1630 /* make_root_vdev() allows 0 toplevel children if there are spares */
1631 if (!zfs_allocatable_devs(nvroot
)) {
1632 (void) fprintf(stderr
, gettext("invalid vdev "
1633 "specification: at least one toplevel vdev must be "
1638 if (altroot
!= NULL
&& altroot
[0] != '/') {
1639 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1640 "must be an absolute path\n"), altroot
);
1645 * Check the validity of the mountpoint and direct the user to use the
1646 * '-m' mountpoint option if it looks like its in use.
1648 if (mountpoint
== NULL
||
1649 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1650 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1651 char buf
[MAXPATHLEN
];
1654 if (mountpoint
&& mountpoint
[0] != '/') {
1655 (void) fprintf(stderr
, gettext("invalid mountpoint "
1656 "'%s': must be an absolute path, 'legacy', or "
1657 "'none'\n"), mountpoint
);
1661 if (mountpoint
== NULL
) {
1662 if (altroot
!= NULL
)
1663 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1666 (void) snprintf(buf
, sizeof (buf
), "/%s",
1669 if (altroot
!= NULL
)
1670 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1671 altroot
, mountpoint
);
1673 (void) snprintf(buf
, sizeof (buf
), "%s",
1677 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1678 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1679 "%s\n"), buf
, strerror(errno
));
1680 (void) fprintf(stderr
, gettext("use '-m' "
1681 "option to provide a different default\n"));
1686 while (count
< 3 && readdir(dirp
) != NULL
)
1688 (void) closedir(dirp
);
1691 (void) fprintf(stderr
, gettext("mountpoint "
1692 "'%s' exists and is not empty\n"), buf
);
1693 (void) fprintf(stderr
, gettext("use '-m' "
1694 "option to provide a "
1695 "different default\n"));
1702 * Now that the mountpoint's validity has been checked, ensure that
1703 * the property is set appropriately prior to creating the pool.
1705 if (mountpoint
!= NULL
) {
1706 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1707 mountpoint
, &fsprops
, B_FALSE
);
1715 * For a dry run invocation, print out a basic message and run
1716 * through all the vdevs in the list and print out in an
1717 * appropriate hierarchy.
1719 (void) printf(gettext("would create '%s' with the "
1720 "following layout:\n\n"), poolname
);
1722 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1723 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1724 VDEV_ALLOC_BIAS_DEDUP
, 0);
1725 print_vdev_tree(NULL
, "special", nvroot
, 0,
1726 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1727 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1728 VDEV_ALLOC_BIAS_LOG
, 0);
1729 print_cache_list(nvroot
, 0);
1730 print_spare_list(nvroot
, 0);
1735 * Load in feature set.
1736 * Note: if compatibility property not given, we'll have
1737 * NULL, which means 'all features'.
1739 boolean_t requested_features
[SPA_FEATURES
];
1740 if (zpool_do_load_compat(compat
, requested_features
) !=
1741 ZPOOL_COMPATIBILITY_OK
)
1745 * props contains list of features to enable.
1747 * - remove it if feature@name=disabled
1748 * - leave it there if feature@name=enabled
1750 * - enable_pool_features (ie: no '-d' or '-o version')
1751 * - it's supported by the kernel module
1752 * - it's in the requested feature set
1753 * - warn if it's enabled but not in compat
1755 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1756 char propname
[MAXPATHLEN
];
1758 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1760 (void) snprintf(propname
, sizeof (propname
),
1761 "feature@%s", feat
->fi_uname
);
1763 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1765 ZFS_FEATURE_DISABLED
) == 0) {
1766 (void) nvlist_remove_all(props
,
1768 } else if (strcmp(propval
,
1769 ZFS_FEATURE_ENABLED
) == 0 &&
1770 !requested_features
[i
]) {
1771 (void) fprintf(stderr
, gettext(
1772 "Warning: feature \"%s\" enabled "
1773 "but is not in specified "
1774 "'compatibility' feature set.\n"),
1778 enable_pool_features
&&
1779 feat
->fi_zfs_mod_supported
&&
1780 requested_features
[i
]) {
1781 ret
= add_prop_list(propname
,
1782 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1789 if (zpool_create(g_zfs
, poolname
,
1790 nvroot
, props
, fsprops
) == 0) {
1791 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1792 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1794 if (zfs_mount(pool
, NULL
, 0) == 0) {
1795 ret
= zfs_share(pool
, NULL
);
1796 zfs_commit_shares(NULL
);
1800 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1801 (void) fprintf(stderr
, gettext("pool name may have "
1807 nvlist_free(nvroot
);
1808 nvlist_free(fsprops
);
1812 nvlist_free(fsprops
);
1819 * zpool destroy <pool>
1821 * -f Forcefully unmount any datasets
1823 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1826 zpool_do_destroy(int argc
, char **argv
)
1828 boolean_t force
= B_FALSE
;
1831 zpool_handle_t
*zhp
;
1835 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1841 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1850 /* check arguments */
1852 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1856 (void) fprintf(stderr
, gettext("too many arguments\n"));
1862 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1864 * As a special case, check for use of '/' in the name, and
1865 * direct the user to use 'zfs destroy' instead.
1867 if (strchr(pool
, '/') != NULL
)
1868 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1869 "destroy a dataset\n"));
1873 if (zpool_disable_datasets(zhp
, force
) != 0) {
1874 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1875 "could not unmount datasets\n"), zpool_get_name(zhp
));
1880 /* The history must be logged as part of the export */
1881 log_history
= B_FALSE
;
1883 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1890 typedef struct export_cbdata
{
1892 boolean_t hardforce
;
1899 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1901 export_cbdata_t
*cb
= data
;
1903 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1906 /* The history must be logged as part of the export */
1907 log_history
= B_FALSE
;
1909 if (cb
->hardforce
) {
1910 if (zpool_export_force(zhp
, history_str
) != 0)
1912 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1920 * zpool export [-f] <pool> ...
1922 * -a Export all pools
1923 * -f Forcefully unmount datasets
1925 * Export the given pools. By default, the command will attempt to cleanly
1926 * unmount any active datasets within the pool. If the '-f' flag is specified,
1927 * then the datasets will be forcefully unmounted.
1930 zpool_do_export(int argc
, char **argv
)
1933 boolean_t do_all
= B_FALSE
;
1934 boolean_t force
= B_FALSE
;
1935 boolean_t hardforce
= B_FALSE
;
1939 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1951 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1958 cb
.hardforce
= hardforce
;
1964 (void) fprintf(stderr
, gettext("too many arguments\n"));
1968 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1969 ZFS_TYPE_POOL
, B_FALSE
, zpool_export_one
, &cb
));
1972 /* check arguments */
1974 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1978 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
1979 B_FALSE
, zpool_export_one
, &cb
);
1985 * Given a vdev configuration, determine the maximum width needed for the device
1989 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1992 static const char *const subtypes
[] =
1993 {ZPOOL_CONFIG_SPARES
, ZPOOL_CONFIG_L2CACHE
, ZPOOL_CONFIG_CHILDREN
};
1995 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1996 max
= MAX(strlen(name
) + depth
, max
);
2001 for (size_t i
= 0; i
< ARRAY_SIZE(subtypes
); ++i
)
2002 if (nvlist_lookup_nvlist_array(nv
, subtypes
[i
],
2003 &child
, &children
) == 0)
2004 for (uint_t c
= 0; c
< children
; ++c
)
2005 max
= MAX(max_width(zhp
, child
[c
], depth
+ 2,
2006 max
, name_flags
), max
);
2011 typedef struct spare_cbdata
{
2013 zpool_handle_t
*cb_zhp
;
2017 find_vdev(nvlist_t
*nv
, uint64_t search
)
2023 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
2027 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2028 &child
, &children
) == 0) {
2029 for (c
= 0; c
< children
; c
++)
2030 if (find_vdev(child
[c
], search
))
2038 find_spare(zpool_handle_t
*zhp
, void *data
)
2040 spare_cbdata_t
*cbp
= data
;
2041 nvlist_t
*config
, *nvroot
;
2043 config
= zpool_get_config(zhp
, NULL
);
2044 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2047 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
2056 typedef struct status_cbdata
{
2060 boolean_t cb_allpools
;
2061 boolean_t cb_verbose
;
2062 boolean_t cb_literal
;
2063 boolean_t cb_explain
;
2065 boolean_t cb_dedup_stats
;
2066 boolean_t cb_print_status
;
2067 boolean_t cb_print_slow_ios
;
2068 boolean_t cb_print_vdev_init
;
2069 boolean_t cb_print_vdev_trim
;
2070 vdev_cmd_data_list_t
*vcdl
;
2073 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2075 is_blank_str(const char *str
)
2077 for (; str
!= NULL
&& *str
!= '\0'; ++str
)
2083 /* Print command output lines for specific vdev in a specific pool */
2085 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
2087 vdev_cmd_data_t
*data
;
2091 for (i
= 0; i
< vcdl
->count
; i
++) {
2092 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2093 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2094 /* Not the vdev we're looking for */
2098 data
= &vcdl
->data
[i
];
2099 /* Print out all the output values for this vdev */
2100 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2102 /* Does this vdev have values for this column? */
2103 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2104 if (strcmp(data
->cols
[k
],
2105 vcdl
->uniq_cols
[j
]) == 0) {
2106 /* yes it does, record the value */
2107 val
= data
->lines
[k
];
2112 * Mark empty values with dashes to make output
2115 if (val
== NULL
|| is_blank_str(val
))
2118 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2119 if (j
< vcdl
->uniq_cols_cnt
- 1)
2123 /* Print out any values that aren't in a column at the end */
2124 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2125 /* Did we have any columns? If so print a spacer. */
2126 if (vcdl
->uniq_cols_cnt
> 0)
2129 val
= data
->lines
[j
];
2130 fputs(val
?: "", stdout
);
2137 * Print vdev initialization status for leaves
2140 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2143 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2144 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2145 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2146 !vs
->vs_scan_removing
) {
2149 struct tm zaction_ts
;
2151 time_t t
= vs
->vs_initialize_action_time
;
2152 int initialize_pct
= 100;
2153 if (vs
->vs_initialize_state
!=
2154 VDEV_INITIALIZE_COMPLETE
) {
2155 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2156 100 / (vs
->vs_initialize_bytes_est
+ 1));
2159 (void) localtime_r(&t
, &zaction_ts
);
2160 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2162 switch (vs
->vs_initialize_state
) {
2163 case VDEV_INITIALIZE_SUSPENDED
:
2164 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2165 gettext("suspended, started at"), tbuf
);
2167 case VDEV_INITIALIZE_ACTIVE
:
2168 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2169 gettext("started at"), tbuf
);
2171 case VDEV_INITIALIZE_COMPLETE
:
2172 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2173 gettext("completed at"), tbuf
);
2177 (void) printf(gettext(" (%d%% initialized%s)"),
2178 initialize_pct
, zbuf
);
2180 (void) printf(gettext(" (uninitialized)"));
2182 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2183 (void) printf(gettext(" (initializing)"));
2188 * Print vdev TRIM status for leaves
2191 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2194 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2195 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2196 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2197 !vs
->vs_scan_removing
) {
2200 struct tm zaction_ts
;
2202 time_t t
= vs
->vs_trim_action_time
;
2204 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2205 trim_pct
= (vs
->vs_trim_bytes_done
*
2206 100 / (vs
->vs_trim_bytes_est
+ 1));
2209 (void) localtime_r(&t
, &zaction_ts
);
2210 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2212 switch (vs
->vs_trim_state
) {
2213 case VDEV_TRIM_SUSPENDED
:
2214 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2215 gettext("suspended, started at"), tbuf
);
2217 case VDEV_TRIM_ACTIVE
:
2218 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2219 gettext("started at"), tbuf
);
2221 case VDEV_TRIM_COMPLETE
:
2222 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2223 gettext("completed at"), tbuf
);
2227 (void) printf(gettext(" (%d%% trimmed%s)"),
2229 } else if (vs
->vs_trim_notsup
) {
2230 (void) printf(gettext(" (trim unsupported)"));
2232 (void) printf(gettext(" (untrimmed)"));
2234 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2235 (void) printf(gettext(" (trimming)"));
2240 * Return the color associated with a health string. This includes returning
2241 * NULL for no color change.
2244 health_str_to_color(const char *health
)
2246 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2247 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2248 strcmp(health
, gettext("UNAVAIL")) == 0) {
2252 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2253 strcmp(health
, gettext("DEGRADED")) == 0 ||
2254 strcmp(health
, gettext("REMOVED")) == 0) {
2255 return (ANSI_YELLOW
);
2262 * Print out configuration state as requested by status_callback.
2265 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2266 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2268 nvlist_t
**child
, *root
;
2269 uint_t c
, i
, vsc
, children
;
2270 pool_scan_stat_t
*ps
= NULL
;
2272 char rbuf
[6], wbuf
[6], cbuf
[6];
2274 uint64_t notpresent
;
2275 spare_cbdata_t spare_cb
;
2279 const char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2281 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2282 &child
, &children
) != 0)
2285 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2286 (uint64_t **)&vs
, &vsc
) == 0);
2288 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2290 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2293 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2297 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2300 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2301 state
= gettext("INUSE");
2302 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2303 state
= gettext("AVAIL");
2306 printf_color(health_str_to_color(state
),
2307 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2311 if (vs
->vs_read_errors
)
2314 if (vs
->vs_write_errors
)
2317 if (vs
->vs_checksum_errors
)
2320 if (cb
->cb_literal
) {
2322 printf_color(rcolor
, "%5llu",
2323 (u_longlong_t
)vs
->vs_read_errors
);
2325 printf_color(wcolor
, "%5llu",
2326 (u_longlong_t
)vs
->vs_write_errors
);
2328 printf_color(ccolor
, "%5llu",
2329 (u_longlong_t
)vs
->vs_checksum_errors
);
2331 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2332 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2333 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2336 printf_color(rcolor
, "%5s", rbuf
);
2338 printf_color(wcolor
, "%5s", wbuf
);
2340 printf_color(ccolor
, "%5s", cbuf
);
2342 if (cb
->cb_print_slow_ios
) {
2343 if (children
== 0) {
2344 /* Only leafs vdevs have slow IOs */
2345 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2348 snprintf(rbuf
, sizeof (rbuf
), "-");
2352 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2354 printf(" %5s", rbuf
);
2358 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2359 ¬present
) == 0) {
2360 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2361 (void) printf(" %s %s", gettext("was"), path
);
2362 } else if (vs
->vs_aux
!= 0) {
2364 color_start(ANSI_RED
);
2365 switch (vs
->vs_aux
) {
2366 case VDEV_AUX_OPEN_FAILED
:
2367 (void) printf(gettext("cannot open"));
2370 case VDEV_AUX_BAD_GUID_SUM
:
2371 (void) printf(gettext("missing device"));
2374 case VDEV_AUX_NO_REPLICAS
:
2375 (void) printf(gettext("insufficient replicas"));
2378 case VDEV_AUX_VERSION_NEWER
:
2379 (void) printf(gettext("newer version"));
2382 case VDEV_AUX_UNSUP_FEAT
:
2383 (void) printf(gettext("unsupported feature(s)"));
2386 case VDEV_AUX_ASHIFT_TOO_BIG
:
2387 (void) printf(gettext("unsupported minimum blocksize"));
2390 case VDEV_AUX_SPARED
:
2391 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2392 &spare_cb
.cb_guid
) == 0);
2393 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2394 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2395 zpool_get_name(zhp
)) == 0)
2396 (void) printf(gettext("currently in "
2399 (void) printf(gettext("in use by "
2401 zpool_get_name(spare_cb
.cb_zhp
));
2402 zpool_close(spare_cb
.cb_zhp
);
2404 (void) printf(gettext("currently in use"));
2408 case VDEV_AUX_ERR_EXCEEDED
:
2409 (void) printf(gettext("too many errors"));
2412 case VDEV_AUX_IO_FAILURE
:
2413 (void) printf(gettext("experienced I/O failures"));
2416 case VDEV_AUX_BAD_LOG
:
2417 (void) printf(gettext("bad intent log"));
2420 case VDEV_AUX_EXTERNAL
:
2421 (void) printf(gettext("external device fault"));
2424 case VDEV_AUX_SPLIT_POOL
:
2425 (void) printf(gettext("split into new pool"));
2428 case VDEV_AUX_ACTIVE
:
2429 (void) printf(gettext("currently in use"));
2432 case VDEV_AUX_CHILDREN_OFFLINE
:
2433 (void) printf(gettext("all children offline"));
2436 case VDEV_AUX_BAD_LABEL
:
2437 (void) printf(gettext("invalid label"));
2441 (void) printf(gettext("corrupted data"));
2445 } else if (children
== 0 && !isspare
&&
2446 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2447 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2448 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2450 gettext(" block size: %dB configured, %dB native"),
2451 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2454 if (vs
->vs_scan_removing
!= 0) {
2455 (void) printf(gettext(" (removing)"));
2456 } else if (VDEV_STAT_VALID(vs_noalloc
, vsc
) && vs
->vs_noalloc
!= 0) {
2457 (void) printf(gettext(" (non-allocating)"));
2460 /* The root vdev has the scrub/resilver stats */
2461 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2462 ZPOOL_CONFIG_VDEV_TREE
);
2463 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2464 (uint64_t **)&ps
, &c
);
2467 * If you force fault a drive that's resilvering, its scan stats can
2468 * get frozen in time, giving the false impression that it's
2469 * being resilvered. That's why we check the state to see if the vdev
2470 * is healthy before reporting "resilvering" or "repairing".
2472 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0 &&
2473 vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2474 if (vs
->vs_scan_processed
!= 0) {
2475 (void) printf(gettext(" (%s)"),
2476 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2477 "resilvering" : "repairing");
2478 } else if (vs
->vs_resilver_deferred
) {
2479 (void) printf(gettext(" (awaiting resilver)"));
2483 /* The top-level vdevs have the rebuild stats */
2484 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2485 children
== 0 && vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2486 if (vs
->vs_rebuild_processed
!= 0) {
2487 (void) printf(gettext(" (resilvering)"));
2491 if (cb
->vcdl
!= NULL
) {
2492 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2494 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2498 /* Display vdev initialization and trim status for leaves. */
2499 if (children
== 0) {
2500 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2501 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2504 (void) printf("\n");
2506 for (c
= 0; c
< children
; c
++) {
2507 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2509 /* Don't print logs or holes here */
2510 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2512 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2514 if (islog
|| ishole
)
2516 /* Only print normal classes here */
2517 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2520 /* Provide vdev_rebuild_stats to children if available */
2522 (void) nvlist_lookup_uint64_array(nv
,
2523 ZPOOL_CONFIG_REBUILD_STATS
,
2524 (uint64_t **)&vrs
, &i
);
2527 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2528 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2529 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2536 * Print the configuration of an exported pool. Iterate over all vdevs in the
2537 * pool, printing out the name and status for each one.
2540 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2548 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2549 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2550 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2553 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2554 (uint64_t **)&vs
, &c
) == 0);
2556 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2557 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2559 if (vs
->vs_aux
!= 0) {
2562 switch (vs
->vs_aux
) {
2563 case VDEV_AUX_OPEN_FAILED
:
2564 (void) printf(gettext("cannot open"));
2567 case VDEV_AUX_BAD_GUID_SUM
:
2568 (void) printf(gettext("missing device"));
2571 case VDEV_AUX_NO_REPLICAS
:
2572 (void) printf(gettext("insufficient replicas"));
2575 case VDEV_AUX_VERSION_NEWER
:
2576 (void) printf(gettext("newer version"));
2579 case VDEV_AUX_UNSUP_FEAT
:
2580 (void) printf(gettext("unsupported feature(s)"));
2583 case VDEV_AUX_ERR_EXCEEDED
:
2584 (void) printf(gettext("too many errors"));
2587 case VDEV_AUX_ACTIVE
:
2588 (void) printf(gettext("currently in use"));
2591 case VDEV_AUX_CHILDREN_OFFLINE
:
2592 (void) printf(gettext("all children offline"));
2595 case VDEV_AUX_BAD_LABEL
:
2596 (void) printf(gettext("invalid label"));
2600 (void) printf(gettext("corrupted data"));
2604 (void) printf("\n");
2606 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2607 &child
, &children
) != 0)
2610 for (c
= 0; c
< children
; c
++) {
2611 uint64_t is_log
= B_FALSE
;
2613 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2617 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2620 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2621 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2622 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2626 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2627 &child
, &children
) == 0) {
2628 (void) printf(gettext("\tcache\n"));
2629 for (c
= 0; c
< children
; c
++) {
2630 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2632 (void) printf("\t %s\n", vname
);
2637 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2638 &child
, &children
) == 0) {
2639 (void) printf(gettext("\tspares\n"));
2640 for (c
= 0; c
< children
; c
++) {
2641 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2643 (void) printf("\t %s\n", vname
);
2650 * Print specialized class vdevs.
2652 * These are recorded as top level vdevs in the main pool child array
2653 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2654 * print_status_config() or print_import_config() to print the top level
2655 * class vdevs then any of their children (eg mirrored slogs) are printed
2656 * recursively - which works because only the top level vdev is marked.
2659 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2664 boolean_t printed
= B_FALSE
;
2666 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2668 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2672 for (c
= 0; c
< children
; c
++) {
2673 uint64_t is_log
= B_FALSE
;
2677 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2681 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
2683 (void) nvlist_lookup_string(child
[c
],
2684 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2685 (void) nvlist_lookup_string(child
[c
],
2686 ZPOOL_CONFIG_TYPE
, &type
);
2689 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2691 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2695 (void) printf("\t%s\t\n", gettext(class));
2699 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2700 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2701 if (cb
->cb_print_status
)
2702 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2705 print_import_config(cb
, name
, child
[c
], 2);
2711 * Display the status for the given pool.
2714 show_import(nvlist_t
*config
, boolean_t report_error
)
2716 uint64_t pool_state
;
2720 uint64_t hostid
= 0;
2722 const char *hostname
= "unknown";
2723 nvlist_t
*nvroot
, *nvinfo
;
2724 zpool_status_t reason
;
2725 zpool_errata_t errata
;
2729 status_cbdata_t cb
= { 0 };
2731 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2733 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2735 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2737 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2740 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2741 (uint64_t **)&vs
, &vsc
) == 0);
2742 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2744 reason
= zpool_import_status(config
, &msgid
, &errata
);
2747 * If we're importing using a cachefile, then we won't report any
2748 * errors unless we are in the scan phase of the import.
2750 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
2753 (void) printf(gettext(" pool: %s\n"), name
);
2754 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2755 (void) printf(gettext(" state: %s"), health
);
2756 if (pool_state
== POOL_STATE_DESTROYED
)
2757 (void) printf(gettext(" (DESTROYED)"));
2758 (void) printf("\n");
2761 case ZPOOL_STATUS_MISSING_DEV_R
:
2762 case ZPOOL_STATUS_MISSING_DEV_NR
:
2763 case ZPOOL_STATUS_BAD_GUID_SUM
:
2764 printf_color(ANSI_BOLD
, gettext("status: "));
2765 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2766 "missing from the system.\n"));
2769 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2770 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2771 printf_color(ANSI_BOLD
, gettext("status: "));
2772 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2773 " corrupted data.\n"));
2776 case ZPOOL_STATUS_CORRUPT_DATA
:
2778 gettext(" status: The pool data is corrupted.\n"));
2781 case ZPOOL_STATUS_OFFLINE_DEV
:
2782 printf_color(ANSI_BOLD
, gettext("status: "));
2783 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2784 "are offlined.\n"));
2787 case ZPOOL_STATUS_CORRUPT_POOL
:
2788 printf_color(ANSI_BOLD
, gettext("status: "));
2789 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2793 case ZPOOL_STATUS_VERSION_OLDER
:
2794 printf_color(ANSI_BOLD
, gettext("status: "));
2795 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2796 "a legacy on-disk version.\n"));
2799 case ZPOOL_STATUS_VERSION_NEWER
:
2800 printf_color(ANSI_BOLD
, gettext("status: "));
2801 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2802 "an incompatible version.\n"));
2805 case ZPOOL_STATUS_FEAT_DISABLED
:
2806 printf_color(ANSI_BOLD
, gettext("status: "));
2807 printf_color(ANSI_YELLOW
, gettext("Some supported "
2808 "features are not enabled on the pool.\n\t"
2809 "(Note that they may be intentionally disabled "
2810 "if the\n\t'compatibility' property is set.)\n"));
2813 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2814 printf_color(ANSI_BOLD
, gettext("status: "));
2815 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2816 "the file(s) indicated by the 'compatibility'\n"
2820 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
2821 printf_color(ANSI_BOLD
, gettext("status: "));
2822 printf_color(ANSI_YELLOW
, gettext("One or more features "
2823 "are enabled on the pool despite not being\n"
2824 "requested by the 'compatibility' property.\n"));
2827 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2828 printf_color(ANSI_BOLD
, gettext("status: "));
2829 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2830 "feature(s) not supported on this system:\n"));
2831 color_start(ANSI_YELLOW
);
2832 zpool_print_unsup_feat(config
);
2836 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2837 printf_color(ANSI_BOLD
, gettext("status: "));
2838 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2839 "accessed in read-only mode on this system. It\n\tcannot be"
2840 " accessed in read-write mode because it uses the "
2841 "following\n\tfeature(s) not supported on this system:\n"));
2842 color_start(ANSI_YELLOW
);
2843 zpool_print_unsup_feat(config
);
2847 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2848 printf_color(ANSI_BOLD
, gettext("status: "));
2849 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2850 "imported by another system.\n"));
2853 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2854 printf_color(ANSI_BOLD
, gettext("status: "));
2855 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2856 "multihost property on. It cannot\n\tbe safely imported "
2857 "when the system hostid is not set.\n"));
2860 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2861 printf_color(ANSI_BOLD
, gettext("status: "));
2862 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2863 "by another system.\n"));
2866 case ZPOOL_STATUS_FAULTED_DEV_R
:
2867 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2868 printf_color(ANSI_BOLD
, gettext("status: "));
2869 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2873 case ZPOOL_STATUS_BAD_LOG
:
2874 printf_color(ANSI_BOLD
, gettext("status: "));
2875 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2879 case ZPOOL_STATUS_RESILVERING
:
2880 case ZPOOL_STATUS_REBUILDING
:
2881 printf_color(ANSI_BOLD
, gettext("status: "));
2882 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2883 "being resilvered.\n"));
2886 case ZPOOL_STATUS_ERRATA
:
2887 printf_color(ANSI_BOLD
, gettext("status: "));
2888 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2892 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2893 printf_color(ANSI_BOLD
, gettext("status: "));
2894 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2895 "configured to use a non-native block size.\n"
2896 "\tExpect reduced performance.\n"));
2901 * No other status can be seen when importing pools.
2903 assert(reason
== ZPOOL_STATUS_OK
);
2907 * Print out an action according to the overall state of the pool.
2909 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2910 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2911 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2912 (void) printf(gettext(" action: The pool can be "
2913 "imported using its name or numeric identifier, "
2914 "though\n\tsome features will not be available "
2915 "without an explicit 'zpool upgrade'.\n"));
2916 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
2917 (void) printf(gettext(" action: The pool can be "
2918 "imported using its name or numeric\n\tidentifier, "
2919 "though the file(s) indicated by its "
2920 "'compatibility'\n\tproperty cannot be parsed at "
2922 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2923 (void) printf(gettext(" action: The pool can be "
2924 "imported using its name or numeric "
2925 "identifier and\n\tthe '-f' flag.\n"));
2926 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2928 case ZPOOL_ERRATA_NONE
:
2931 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2932 (void) printf(gettext(" action: The pool can "
2933 "be imported using its name or numeric "
2934 "identifier,\n\thowever there is a compat"
2935 "ibility issue which should be corrected"
2936 "\n\tby running 'zpool scrub'\n"));
2939 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2940 (void) printf(gettext(" action: The pool can"
2941 "not be imported with this version of ZFS "
2942 "due to\n\tan active asynchronous destroy. "
2943 "Revert to an earlier version\n\tand "
2944 "allow the destroy to complete before "
2948 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2949 (void) printf(gettext(" action: Existing "
2950 "encrypted datasets contain an on-disk "
2951 "incompatibility, which\n\tneeds to be "
2952 "corrected. Backup these datasets to new "
2953 "encrypted datasets\n\tand destroy the "
2957 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2958 (void) printf(gettext(" action: Existing "
2959 "encrypted snapshots and bookmarks contain "
2960 "an on-disk\n\tincompatibility. This may "
2961 "cause on-disk corruption if they are used"
2962 "\n\twith 'zfs recv'. To correct the "
2963 "issue, enable the bookmark_v2 feature.\n\t"
2964 "No additional action is needed if there "
2965 "are no encrypted snapshots or\n\t"
2966 "bookmarks. If preserving the encrypted "
2967 "snapshots and bookmarks is\n\trequired, "
2968 "use a non-raw send to backup and restore "
2969 "them. Alternately,\n\tthey may be removed"
2970 " to resolve the incompatibility.\n"));
2974 * All errata must contain an action message.
2979 (void) printf(gettext(" action: The pool can be "
2980 "imported using its name or numeric "
2983 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2984 (void) printf(gettext(" action: The pool can be imported "
2985 "despite missing or damaged devices. The\n\tfault "
2986 "tolerance of the pool may be compromised if imported.\n"));
2989 case ZPOOL_STATUS_VERSION_NEWER
:
2990 (void) printf(gettext(" action: The pool cannot be "
2991 "imported. Access the pool on a system running "
2992 "newer\n\tsoftware, or recreate the pool from "
2995 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2996 printf_color(ANSI_BOLD
, gettext("action: "));
2997 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2998 "imported. Access the pool on a system that "
2999 "supports\n\tthe required feature(s), or recreate "
3000 "the pool from backup.\n"));
3002 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3003 printf_color(ANSI_BOLD
, gettext("action: "));
3004 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3005 "imported in read-write mode. Import the pool "
3007 "\t\"-o readonly=on\", access the pool on a system "
3008 "that supports the\n\trequired feature(s), or "
3009 "recreate the pool from backup.\n"));
3011 case ZPOOL_STATUS_MISSING_DEV_R
:
3012 case ZPOOL_STATUS_MISSING_DEV_NR
:
3013 case ZPOOL_STATUS_BAD_GUID_SUM
:
3014 (void) printf(gettext(" action: The pool cannot be "
3015 "imported. Attach the missing\n\tdevices and try "
3018 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3019 VERIFY0(nvlist_lookup_nvlist(config
,
3020 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
3022 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3023 hostname
= fnvlist_lookup_string(nvinfo
,
3024 ZPOOL_CONFIG_MMP_HOSTNAME
);
3026 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3027 hostid
= fnvlist_lookup_uint64(nvinfo
,
3028 ZPOOL_CONFIG_MMP_HOSTID
);
3030 (void) printf(gettext(" action: The pool must be "
3031 "exported from %s (hostid=%"PRIx64
")\n\tbefore it "
3032 "can be safely imported.\n"), hostname
, hostid
);
3034 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3035 (void) printf(gettext(" action: Set a unique system "
3036 "hostid with the zgenhostid(8) command.\n"));
3039 (void) printf(gettext(" action: The pool cannot be "
3040 "imported due to damaged devices or data.\n"));
3044 /* Print the comment attached to the pool. */
3045 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3046 (void) printf(gettext("comment: %s\n"), comment
);
3049 * If the state is "closed" or "can't open", and the aux state
3050 * is "corrupt data":
3052 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
3053 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
3054 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
3055 if (pool_state
== POOL_STATE_DESTROYED
)
3056 (void) printf(gettext("\tThe pool was destroyed, "
3057 "but can be imported using the '-Df' flags.\n"));
3058 else if (pool_state
!= POOL_STATE_EXPORTED
)
3059 (void) printf(gettext("\tThe pool may be active on "
3060 "another system, but can be imported using\n\t"
3061 "the '-f' flag.\n"));
3064 if (msgid
!= NULL
) {
3065 (void) printf(gettext(
3066 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3070 (void) printf(gettext(" config:\n\n"));
3072 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
3074 if (cb
.cb_namewidth
< 10)
3075 cb
.cb_namewidth
= 10;
3077 print_import_config(&cb
, name
, nvroot
, 0);
3079 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
3080 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
3081 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
3083 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
3084 (void) printf(gettext("\n\tAdditional devices are known to "
3085 "be part of this pool, though their\n\texact "
3086 "configuration cannot be determined.\n"));
3092 zfs_force_import_required(nvlist_t
*config
)
3095 uint64_t hostid
= 0;
3098 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
3099 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
3101 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3104 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3105 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3106 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3107 ZPOOL_CONFIG_MMP_STATE
);
3109 if (mmp_state
!= MMP_STATE_INACTIVE
)
3117 * Perform the import for the given configuration. This passes the heavy
3118 * lifting off to zpool_import_props(), and then mounts the datasets contained
3122 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3123 nvlist_t
*props
, int flags
)
3126 zpool_handle_t
*zhp
;
3130 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3131 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3133 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3134 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3135 "is formatted using an unsupported ZFS version\n"), name
);
3137 } else if (zfs_force_import_required(config
) &&
3138 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3139 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3142 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3143 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3144 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3145 ZPOOL_CONFIG_MMP_STATE
);
3147 if (mmp_state
== MMP_STATE_ACTIVE
) {
3148 const char *hostname
= "<unknown>";
3149 uint64_t hostid
= 0;
3151 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3152 hostname
= fnvlist_lookup_string(nvinfo
,
3153 ZPOOL_CONFIG_MMP_HOSTNAME
);
3155 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3156 hostid
= fnvlist_lookup_uint64(nvinfo
,
3157 ZPOOL_CONFIG_MMP_HOSTID
);
3159 (void) fprintf(stderr
, gettext("cannot import '%s': "
3160 "pool is imported on %s (hostid: "
3161 "0x%"PRIx64
")\nExport the pool on the other "
3162 "system, then run 'zpool import'.\n"),
3163 name
, hostname
, hostid
);
3164 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3165 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3166 "pool has the multihost property on and the\n"
3167 "system's hostid is not set. Set a unique hostid "
3168 "with the zgenhostid(8) command.\n"), name
);
3170 const char *hostname
= "<unknown>";
3171 time_t timestamp
= 0;
3172 uint64_t hostid
= 0;
3174 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3175 hostname
= fnvlist_lookup_string(config
,
3176 ZPOOL_CONFIG_HOSTNAME
);
3178 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3179 timestamp
= fnvlist_lookup_uint64(config
,
3180 ZPOOL_CONFIG_TIMESTAMP
);
3182 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3183 hostid
= fnvlist_lookup_uint64(config
,
3184 ZPOOL_CONFIG_HOSTID
);
3186 (void) fprintf(stderr
, gettext("cannot import '%s': "
3187 "pool was previously in use from another system.\n"
3188 "Last accessed by %s (hostid=%"PRIx64
") at %s"
3189 "The pool can be imported, use 'zpool import -f' "
3190 "to import the pool.\n"), name
, hostname
,
3191 hostid
, ctime(×tamp
));
3197 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3200 if (newname
!= NULL
)
3203 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3207 * Loading keys is best effort. We don't want to return immediately
3208 * if it fails but we do want to give the error to the caller.
3210 if (flags
& ZFS_IMPORT_LOAD_KEYS
&&
3211 zfs_crypto_attempt_load_keys(g_zfs
, name
) != 0)
3214 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3215 !(flags
& ZFS_IMPORT_ONLY
) &&
3216 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3226 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3227 char *orig_name
, char *new_name
,
3228 boolean_t do_destroyed
, boolean_t pool_specified
, boolean_t do_all
,
3229 importargs_t
*import
)
3231 nvlist_t
*config
= NULL
;
3232 nvlist_t
*found_config
= NULL
;
3233 uint64_t pool_state
;
3236 * At this point we have a list of import candidate configs. Even if
3237 * we were searching by pool name or guid, we still need to
3238 * post-process the list to deal with pool state and possible
3242 nvpair_t
*elem
= NULL
;
3243 boolean_t first
= B_TRUE
;
3244 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3246 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3248 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3250 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3252 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3255 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3256 import
->policy
) == 0);
3258 if (!pool_specified
) {
3262 (void) fputc('\n', stdout
);
3265 err
|= do_import(config
, NULL
, mntopts
,
3269 * If we're importing from cachefile, then
3270 * we don't want to report errors until we
3271 * are in the scan phase of the import. If
3272 * we get an error, then we return that error
3273 * to invoke the scan phase.
3275 if (import
->cachefile
&& !import
->scan
)
3276 err
= show_import(config
, B_FALSE
);
3278 (void) show_import(config
, B_TRUE
);
3280 } else if (import
->poolname
!= NULL
) {
3284 * We are searching for a pool based on name.
3286 verify(nvlist_lookup_string(config
,
3287 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3289 if (strcmp(name
, import
->poolname
) == 0) {
3290 if (found_config
!= NULL
) {
3291 (void) fprintf(stderr
, gettext(
3292 "cannot import '%s': more than "
3293 "one matching pool\n"),
3295 (void) fprintf(stderr
, gettext(
3296 "import by numeric ID instead\n"));
3299 found_config
= config
;
3305 * Search for a pool by guid.
3307 verify(nvlist_lookup_uint64(config
,
3308 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3310 if (guid
== import
->guid
)
3311 found_config
= config
;
3316 * If we were searching for a specific pool, verify that we found a
3317 * pool, and then do the import.
3319 if (pool_specified
&& err
== 0) {
3320 if (found_config
== NULL
) {
3321 (void) fprintf(stderr
, gettext("cannot import '%s': "
3322 "no such pool available\n"), orig_name
);
3325 err
|= do_import(found_config
, new_name
,
3326 mntopts
, props
, flags
);
3331 * If we were just looking for pools, report an error if none were
3334 if (!pool_specified
&& first
)
3335 (void) fprintf(stderr
,
3336 gettext("no pools available to import\n"));
3340 typedef struct target_exists_args
{
3341 const char *poolname
;
3343 } target_exists_args_t
;
3346 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3348 target_exists_args_t
*args
= data
;
3349 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3355 if (args
->poolname
!= NULL
) {
3358 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3360 if (strcmp(pool_name
, args
->poolname
) == 0)
3365 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3367 if (pool_guid
== args
->poolguid
)
3375 * zpool checkpoint <pool>
3376 * checkpoint --discard <pool>
3378 * -d Discard the checkpoint from a checkpointed
3381 * -w Wait for discarding a checkpoint to complete.
3384 * Checkpoints the specified pool, by taking a "snapshot" of its
3385 * current state. A pool can only have one checkpoint at a time.
3388 zpool_do_checkpoint(int argc
, char **argv
)
3390 boolean_t discard
, wait
;
3392 zpool_handle_t
*zhp
;
3395 struct option long_options
[] = {
3396 {"discard", no_argument
, NULL
, 'd'},
3397 {"wait", no_argument
, NULL
, 'w'},
3403 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3412 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3418 if (wait
&& !discard
) {
3419 (void) fprintf(stderr
, gettext("--wait only valid when "
3420 "--discard also specified\n"));
3428 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3433 (void) fprintf(stderr
, gettext("too many arguments\n"));
3439 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3440 /* As a special case, check for use of '/' in the name */
3441 if (strchr(pool
, '/') != NULL
)
3442 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3443 "doesn't work on datasets. To save the state "
3444 "of a dataset from a specific point in time "
3445 "please use 'zfs snapshot'\n"));
3450 err
= (zpool_discard_checkpoint(zhp
) != 0);
3451 if (err
== 0 && wait
)
3452 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3454 err
= (zpool_checkpoint(zhp
) != 0);
3462 #define CHECKPOINT_OPT 1024
3465 * zpool import [-d dir] [-D]
3466 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3467 * [-d dir | -c cachefile | -s] [-f] -a
3468 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3469 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3472 * -c Read pool information from a cachefile instead of searching
3473 * devices. If importing from a cachefile config fails, then
3474 * fallback to searching for devices only in the directories that
3475 * exist in the cachefile.
3477 * -d Scan in a specific directory, other than /dev/. More than
3478 * one directory can be specified using multiple '-d' options.
3480 * -D Scan for previously destroyed pools or import all or only
3481 * specified destroyed pools.
3483 * -R Temporarily import the pool, with all mountpoints relative to
3484 * the given root. The pool will remain exported when the machine
3487 * -V Import even in the presence of faulted vdevs. This is an
3488 * intentionally undocumented option for testing purposes, and
3489 * treats the pool configuration as complete, leaving any bad
3490 * vdevs in the FAULTED state. In other words, it does verbatim
3493 * -f Force import, even if it appears that the pool is active.
3495 * -F Attempt rewind if necessary.
3497 * -n See if rewind would work, but don't actually rewind.
3499 * -N Import the pool but don't mount datasets.
3501 * -T Specify a starting txg to use for import. This option is
3502 * intentionally undocumented option for testing purposes.
3504 * -a Import all pools found.
3506 * -l Load encryption keys while importing.
3508 * -o Set property=value and/or temporary mount options (without '=').
3510 * -s Scan using the default search path, the libblkid cache will
3513 * --rewind-to-checkpoint
3514 * Import the pool and revert back to the checkpoint.
3516 * The import command scans for pools to import, and import pools based on pool
3517 * name and GUID. The pool can also be renamed as part of the import process.
3520 zpool_do_import(int argc
, char **argv
)
3522 char **searchdirs
= NULL
;
3523 char *env
, *envdup
= NULL
;
3527 nvlist_t
*pools
= NULL
;
3528 boolean_t do_all
= B_FALSE
;
3529 boolean_t do_destroyed
= B_FALSE
;
3530 char *mntopts
= NULL
;
3531 uint64_t searchguid
= 0;
3532 char *searchname
= NULL
;
3534 nvlist_t
*policy
= NULL
;
3535 nvlist_t
*props
= NULL
;
3536 int flags
= ZFS_IMPORT_NORMAL
;
3537 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3538 boolean_t dryrun
= B_FALSE
;
3539 boolean_t do_rewind
= B_FALSE
;
3540 boolean_t xtreme_rewind
= B_FALSE
;
3541 boolean_t do_scan
= B_FALSE
;
3542 boolean_t pool_exists
= B_FALSE
;
3543 boolean_t pool_specified
= B_FALSE
;
3544 uint64_t txg
= -1ULL;
3545 char *cachefile
= NULL
;
3546 importargs_t idata
= { 0 };
3549 struct option long_options
[] = {
3550 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3555 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3556 long_options
, NULL
)) != -1) {
3565 searchdirs
= safe_realloc(searchdirs
,
3566 (nsearch
+ 1) * sizeof (char *));
3567 searchdirs
[nsearch
++] = optarg
;
3570 do_destroyed
= B_TRUE
;
3573 flags
|= ZFS_IMPORT_ANY_HOST
;
3579 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3582 flags
|= ZFS_IMPORT_MISSING_LOG
;
3588 flags
|= ZFS_IMPORT_ONLY
;
3591 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3594 if (add_prop_list(optarg
, propval
,
3602 if (add_prop_list(zpool_prop_to_name(
3603 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3605 if (add_prop_list_default(zpool_prop_to_name(
3606 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3613 flags
|= ZFS_IMPORT_TEMP_NAME
;
3614 if (add_prop_list_default(zpool_prop_to_name(
3615 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3621 txg
= strtoull(optarg
, &endptr
, 0);
3622 if (errno
!= 0 || *endptr
!= '\0') {
3623 (void) fprintf(stderr
,
3624 gettext("invalid txg value\n"));
3627 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3630 flags
|= ZFS_IMPORT_VERBATIM
;
3633 xtreme_rewind
= B_TRUE
;
3635 case CHECKPOINT_OPT
:
3636 flags
|= ZFS_IMPORT_CHECKPOINT
;
3639 (void) fprintf(stderr
, gettext("missing argument for "
3640 "'%c' option\n"), optopt
);
3644 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3653 if (cachefile
&& nsearch
!= 0) {
3654 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3658 if (cachefile
&& do_scan
) {
3659 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
3663 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3664 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3668 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3669 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3674 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3675 (void) fprintf(stderr
,
3676 gettext("-n or -X only meaningful with -F\n"));
3680 rewind_policy
= ZPOOL_TRY_REWIND
;
3682 rewind_policy
= ZPOOL_DO_REWIND
;
3684 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3686 /* In the future, we can capture further policy and include it here */
3687 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3688 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3689 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3690 rewind_policy
) != 0)
3693 /* check argument count */
3696 (void) fprintf(stderr
, gettext("too many arguments\n"));
3701 (void) fprintf(stderr
, gettext("too many arguments\n"));
3707 * Check for the effective uid. We do this explicitly here because
3708 * otherwise any attempt to discover pools will silently fail.
3710 if (argc
== 0 && geteuid() != 0) {
3711 (void) fprintf(stderr
, gettext("cannot "
3712 "discover pools: permission denied\n"));
3716 nvlist_free(policy
);
3721 * Depending on the arguments given, we do one of the following:
3723 * <none> Iterate through all pools and display information about
3726 * -a Iterate through all pools and try to import each one.
3728 * <id> Find the pool that corresponds to the given GUID/pool
3729 * name and import that one.
3731 * -D Above options applies only to destroyed pools.
3737 searchguid
= strtoull(argv
[0], &endptr
, 10);
3738 if (errno
!= 0 || *endptr
!= '\0') {
3739 searchname
= argv
[0];
3742 pool_specified
= B_TRUE
;
3745 * User specified a name or guid. Ensure it's unique.
3747 target_exists_args_t search
= {searchname
, searchguid
};
3748 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3752 * Check the environment for the preferred search path.
3754 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3755 char *dir
, *tmp
= NULL
;
3757 envdup
= strdup(env
);
3759 for (dir
= strtok_r(envdup
, ":", &tmp
);
3761 dir
= strtok_r(NULL
, ":", &tmp
)) {
3762 searchdirs
= safe_realloc(searchdirs
,
3763 (nsearch
+ 1) * sizeof (char *));
3764 searchdirs
[nsearch
++] = dir
;
3768 idata
.path
= searchdirs
;
3769 idata
.paths
= nsearch
;
3770 idata
.poolname
= searchname
;
3771 idata
.guid
= searchguid
;
3772 idata
.cachefile
= cachefile
;
3773 idata
.scan
= do_scan
;
3774 idata
.policy
= policy
;
3776 libpc_handle_t lpch
= {
3777 .lpc_lib_handle
= g_zfs
,
3778 .lpc_ops
= &libzfs_config_ops
,
3779 .lpc_printerr
= B_TRUE
3781 pools
= zpool_search_import(&lpch
, &idata
);
3783 if (pools
!= NULL
&& pool_exists
&&
3784 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3785 (void) fprintf(stderr
, gettext("cannot import '%s': "
3786 "a pool with that name already exists\n"),
3788 (void) fprintf(stderr
, gettext("use the form '%s "
3789 "<pool | id> <newpool>' to give it a new name\n"),
3792 } else if (pools
== NULL
&& pool_exists
) {
3793 (void) fprintf(stderr
, gettext("cannot import '%s': "
3794 "a pool with that name is already created/imported,\n"),
3796 (void) fprintf(stderr
, gettext("and no additional pools "
3797 "with that name were found\n"));
3799 } else if (pools
== NULL
) {
3801 (void) fprintf(stderr
, gettext("cannot import '%s': "
3802 "no such pool available\n"), argv
[0]);
3810 nvlist_free(policy
);
3816 err
= import_pools(pools
, props
, mntopts
, flags
,
3817 argc
>= 1 ? argv
[0] : NULL
,
3818 argc
>= 2 ? argv
[1] : NULL
,
3819 do_destroyed
, pool_specified
, do_all
, &idata
);
3822 * If we're using the cachefile and we failed to import, then
3823 * fallback to scanning the directory for pools that match
3824 * those in the cachefile.
3826 if (err
!= 0 && cachefile
!= NULL
) {
3827 (void) printf(gettext("cachefile import failed, retrying\n"));
3830 * We use the scan flag to gather the directories that exist
3831 * in the cachefile. If we need to fallback to searching for
3832 * the pool config, we will only search devices in these
3835 idata
.scan
= B_TRUE
;
3837 pools
= zpool_search_import(&lpch
, &idata
);
3839 err
= import_pools(pools
, props
, mntopts
, flags
,
3840 argc
>= 1 ? argv
[0] : NULL
,
3841 argc
>= 2 ? argv
[1] : NULL
,
3842 do_destroyed
, pool_specified
, do_all
, &idata
);
3848 nvlist_free(policy
);
3852 return (err
? 1 : 0);
3856 * zpool sync [-f] [pool] ...
3858 * -f (undocumented) force uberblock (and config including zpool cache file)
3861 * Sync the specified pool(s).
3862 * Without arguments "zpool sync" will sync all pools.
3863 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3867 zpool_do_sync(int argc
, char **argv
)
3870 boolean_t force
= B_FALSE
;
3873 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3879 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3888 /* if argc == 0 we will execute zpool_sync_one on all pools */
3889 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
3890 B_FALSE
, zpool_sync_one
, &force
);
3895 typedef struct iostat_cbdata
{
3899 boolean_t cb_verbose
;
3900 boolean_t cb_literal
;
3901 boolean_t cb_scripted
;
3902 zpool_list_t
*cb_list
;
3903 vdev_cmd_data_list_t
*vcdl
;
3904 vdev_cbdata_t cb_vdevs
;
3908 typedef struct name_and_columns
{
3909 const char *name
; /* Column name */
3910 unsigned int columns
; /* Center name to this number of columns */
3911 } name_and_columns_t
;
3913 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
3915 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3917 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3919 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3920 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3922 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3923 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3924 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL
}},
3925 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3926 {"asyncq_wait", 2}, {NULL
}},
3927 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3928 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3929 {"trim", 2}, {"rebuild", 2}, {NULL
}},
3932 /* Shorthand - if "columns" field not set, default to 1 column */
3933 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3935 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3937 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3938 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3940 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3941 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3942 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
3943 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3944 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3946 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3947 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3948 {"ind"}, {"agg"}, {NULL
}},
3951 static const char *histo_to_title
[] = {
3952 [IOS_L_HISTO
] = "latency",
3953 [IOS_RQ_HISTO
] = "req_size",
3957 * Return the number of labels in a null-terminated name_and_columns_t
3962 label_array_len(const name_and_columns_t
*labels
)
3966 while (labels
[i
].name
)
3973 * Return the number of strings in a null-terminated string array.
3976 * const char foo[] = {"bar", "baz", NULL}
3981 str_array_len(const char *array
[])
3992 * Return a default column width for default/latency/queue columns. This does
3993 * not include histograms, which have their columns autosized.
3996 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3998 unsigned long column_width
= 5; /* Normal niceprint */
3999 static unsigned long widths
[] = {
4001 * Choose some sane default column sizes for printing the
4004 [IOS_DEFAULT
] = 15, /* 1PB capacity */
4005 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
4006 [IOS_QUEUES
] = 6, /* 1M queue entries */
4007 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
4008 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
4012 column_width
= widths
[type
];
4014 return (column_width
);
4018 * Print the column labels, i.e:
4020 * capacity operations bandwidth
4021 * alloc free read write read write ...
4023 * If force_column_width is set, use it for the column width. If not set, use
4024 * the default column width.
4027 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4028 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
4031 int text_start
, rw_column_width
, spaces_to_end
;
4032 uint64_t flags
= cb
->cb_flags
;
4034 unsigned int column_width
= force_column_width
;
4036 /* For each bit set in flags */
4037 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4038 idx
= lowbit64(f
) - 1;
4039 if (!force_column_width
)
4040 column_width
= default_column_width(cb
, idx
);
4041 /* Print our top labels centered over "read write" label. */
4042 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
4043 const char *name
= labels
[idx
][i
].name
;
4045 * We treat labels[][].columns == 0 as shorthand
4046 * for one column. It makes writing out the label
4047 * tables more concise.
4049 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
4050 unsigned int slen
= strlen(name
);
4052 rw_column_width
= (column_width
* columns
) +
4053 (2 * (columns
- 1));
4055 text_start
= (int)((rw_column_width
) / columns
-
4060 printf(" "); /* Two spaces between columns */
4062 /* Space from beginning of column to label */
4063 for (s
= 0; s
< text_start
; s
++)
4068 /* Print space after label to end of column */
4069 spaces_to_end
= rw_column_width
- text_start
- slen
;
4070 if (spaces_to_end
< 0)
4073 for (s
= 0; s
< spaces_to_end
; s
++)
4081 * print_cmd_columns - Print custom column titles from -c
4083 * If the user specified the "zpool status|iostat -c" then print their custom
4084 * column titles in the header. For example, print_cmd_columns() would print
4085 * the " col1 col2" part of this:
4087 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4089 * capacity operations bandwidth
4090 * pool alloc free read write read write col1 col2
4091 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4092 * mypool 269K 1008M 0 0 107 946
4093 * mirror 269K 1008M 0 0 107 946
4094 * sdb - - 0 0 102 473 val1 val2
4095 * sdc - - 0 0 5 473 val1 val2
4096 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4099 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4102 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4104 if (vcdl
->count
== 0 || data
== NULL
)
4108 * Each vdev cmd should have the same column names unless the user did
4109 * something weird with their cmd. Just take the column names from the
4110 * first vdev and assume it works for all of them.
4112 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4115 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4118 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4119 vcdl
->uniq_cols
[i
]);
4126 * Utility function to print out a line of dashes like:
4128 * -------------------------------- ----- ----- ----- ----- -----
4130 * ...or a dashed named-row line like:
4136 * @force_column_width If non-zero, use the value as the column width.
4137 * Otherwise use the default column widths.
4139 * @name: Print a dashed named-row line starting
4140 * with @name. Otherwise, print a regular
4144 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4148 unsigned int namewidth
;
4149 uint64_t flags
= cb
->cb_flags
;
4152 const name_and_columns_t
*labels
;
4156 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4157 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4158 } else if (cb
->cb_vdevs
.cb_names_count
) {
4164 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4165 name
? strlen(name
) : 0);
4169 printf("%-*s", namewidth
, name
);
4171 for (i
= 0; i
< namewidth
; i
++)
4175 /* For each bit in flags */
4176 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4177 unsigned int column_width
;
4178 idx
= lowbit64(f
) - 1;
4179 if (force_column_width
)
4180 column_width
= force_column_width
;
4182 column_width
= default_column_width(cb
, idx
);
4184 labels
= iostat_bottom_labels
[idx
];
4185 for (i
= 0; i
< label_array_len(labels
); i
++) {
4187 printf(" %*s-", column_width
- 1, " ");
4189 printf(" %.*s", column_width
,
4190 "--------------------");
4197 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4198 unsigned int force_column_width
)
4200 print_iostat_dashes(cb
, force_column_width
, NULL
);
4204 print_iostat_separator(iostat_cbdata_t
*cb
)
4206 print_iostat_separator_impl(cb
, 0);
4210 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4211 const char *histo_vdev_name
)
4213 unsigned int namewidth
;
4216 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4217 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4218 } else if (cb
->cb_vdevs
.cb_names_count
) {
4224 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4225 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4227 if (histo_vdev_name
)
4228 printf("%-*s", namewidth
, histo_vdev_name
);
4230 printf("%*s", namewidth
, "");
4233 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4236 printf("%-*s", namewidth
, title
);
4238 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4239 if (cb
->vcdl
!= NULL
)
4240 print_cmd_columns(cb
->vcdl
, 0);
4244 print_iostat_separator_impl(cb
, force_column_width
);
4246 if (cb
->vcdl
!= NULL
)
4247 print_cmd_columns(cb
->vcdl
, 1);
4253 print_iostat_header(iostat_cbdata_t
*cb
)
4255 print_iostat_header_impl(cb
, 0, NULL
);
4260 * Display a single statistic.
4263 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4264 unsigned int column_size
, boolean_t scripted
)
4268 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4271 printf("\t%s", buf
);
4273 printf(" %*s", column_size
, buf
);
4277 * Calculate the default vdev stats
4279 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4280 * stats into calcvs.
4283 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4284 vdev_stat_t
*calcvs
)
4288 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4289 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4290 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4292 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4293 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4297 * Internal representation of the extended iostats data.
4299 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4300 * or single uint64_t's. We make both look like arrays to make them easier
4301 * to process. In order to make single uint64_t's look like arrays, we set
4302 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4303 * we can just use *data and count.
4307 uint_t count
; /* Number of entries in data[] */
4308 uint64_t __data
; /* Only used when data is a single uint64_t */
4312 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4316 for (i
= 0; i
< len
; i
++)
4317 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4323 * Helper function to lookup a uint64_t array or uint64_t value and store its
4324 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4325 * it look like a one element array to make it easier to process.
4328 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4329 struct stat_array
*nva
)
4334 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4335 switch (nvpair_type(tmp
)) {
4336 case DATA_TYPE_UINT64_ARRAY
:
4337 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4339 case DATA_TYPE_UINT64
:
4340 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4341 nva
->data
= &nva
->__data
;
4345 /* Not a uint64_t */
4354 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4355 * subtract them, and return the results in a newly allocated stat_array.
4356 * You must free the returned array after you are done with it with
4357 * free_calc_stats().
4359 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4362 static struct stat_array
*
4363 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4366 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4367 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4369 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4371 /* Extract our extended stats nvlist from the main list */
4372 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4375 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4379 newnva
= safe_malloc(alloc_size
);
4380 oldnva
= safe_malloc(alloc_size
);
4381 calcnva
= safe_malloc(alloc_size
);
4383 for (j
= 0; j
< len
; j
++) {
4384 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4386 calcnva
[j
].count
= newnva
[j
].count
;
4387 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4388 calcnva
[j
].data
= safe_malloc(alloc_size
);
4389 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4392 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4394 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4395 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4404 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4407 for (i
= 0; i
< len
; i
++)
4414 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4415 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4421 enum zfs_nicenum_format format
;
4422 unsigned int buckets
;
4423 unsigned int start_bucket
;
4426 format
= ZFS_NICENUM_RAW
;
4428 format
= ZFS_NICENUM_1024
;
4430 /* All these histos are the same size, so just use nva[0].count */
4431 buckets
= nva
[0].count
;
4433 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4434 /* Start at 512 - req size should never be lower than this */
4440 for (j
= start_bucket
; j
< buckets
; j
++) {
4441 /* Print histogram bucket label */
4442 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4443 /* Ending range of this bucket */
4444 val
= (1UL << (j
+ 1)) - 1;
4445 zfs_nicetime(val
, buf
, sizeof (buf
));
4447 /* Request size (starting range of bucket) */
4449 zfs_nicenum(val
, buf
, sizeof (buf
));
4452 if (cb
->cb_scripted
)
4453 printf("%llu", (u_longlong_t
)val
);
4455 printf("%-*s", namewidth
, buf
);
4457 /* Print the values on the line */
4458 for (i
= 0; i
< len
; i
++) {
4459 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4460 column_width
, cb
->cb_scripted
);
4467 print_solid_separator(unsigned int length
)
4475 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4476 nvlist_t
*newnv
, double scale
, const char *name
)
4478 unsigned int column_width
;
4479 unsigned int namewidth
;
4480 unsigned int entire_width
;
4481 enum iostat_type type
;
4482 struct stat_array
*nva
;
4484 unsigned int names_len
;
4486 /* What type of histo are we? */
4487 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4489 /* Get NULL-terminated array of nvlist names for our histo */
4490 names
= vsx_type_to_nvlist
[type
];
4491 names_len
= str_array_len(names
); /* num of names */
4493 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4495 if (cb
->cb_literal
) {
4496 column_width
= MAX(5,
4497 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4502 namewidth
= MAX(cb
->cb_namewidth
,
4503 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4506 * Calculate the entire line width of what we're printing. The
4507 * +2 is for the two spaces between columns:
4511 /* |___| <---------- column_width */
4513 /* |__________| <--- entire_width */
4515 entire_width
= namewidth
+ (column_width
+ 2) *
4516 label_array_len(iostat_bottom_labels
[type
]);
4518 if (cb
->cb_scripted
)
4519 printf("%s\n", name
);
4521 print_iostat_header_impl(cb
, column_width
, name
);
4523 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4526 free_calc_stats(nva
, names_len
);
4527 if (!cb
->cb_scripted
)
4528 print_solid_separator(entire_width
);
4532 * Calculate the average latency of a power-of-two latency histogram
4535 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4538 uint64_t count
= 0, total
= 0;
4540 for (i
= 0; i
< buckets
; i
++) {
4542 * Our buckets are power-of-two latency ranges. Use the
4543 * midpoint latency of each bucket to calculate the average.
4551 if (histo
[i
] != 0) {
4552 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4557 /* Prevent divide by zero */
4558 return (count
== 0 ? 0 : total
/ count
);
4562 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*newnv
)
4564 const char *names
[] = {
4565 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4566 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4567 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4568 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4569 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4570 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4571 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4572 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4573 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4574 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4575 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4576 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4577 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE
,
4578 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
4581 struct stat_array
*nva
;
4583 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4584 enum zfs_nicenum_format format
;
4586 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4589 format
= ZFS_NICENUM_RAW
;
4591 format
= ZFS_NICENUM_1024
;
4593 for (int i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4594 uint64_t val
= nva
[i
].data
[0];
4595 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4598 free_calc_stats(nva
, ARRAY_SIZE(names
));
4602 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4607 const char *names
[] = {
4608 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4609 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4610 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4611 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4612 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4613 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4614 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4615 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4616 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4617 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4618 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
4620 struct stat_array
*nva
;
4622 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4623 enum zfs_nicenum_format format
;
4625 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4628 format
= ZFS_NICENUM_RAWTIME
;
4630 format
= ZFS_NICENUM_TIME
;
4632 /* Print our avg latencies on the line */
4633 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4634 /* Compute average latency for a latency histo */
4635 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4636 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4638 free_calc_stats(nva
, ARRAY_SIZE(names
));
4642 * Print default statistics (capacity/operations/bandwidth)
4645 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4647 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4648 enum zfs_nicenum_format format
;
4649 char na
; /* char to print for "not applicable" values */
4651 if (cb
->cb_literal
) {
4652 format
= ZFS_NICENUM_RAW
;
4655 format
= ZFS_NICENUM_1024
;
4659 /* only toplevel vdevs have capacity stats */
4660 if (vs
->vs_space
== 0) {
4661 if (cb
->cb_scripted
)
4662 printf("\t%c\t%c", na
, na
);
4664 printf(" %*c %*c", column_width
, na
, column_width
,
4667 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4669 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4670 column_width
, cb
->cb_scripted
);
4673 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4674 format
, column_width
, cb
->cb_scripted
);
4675 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4676 format
, column_width
, cb
->cb_scripted
);
4677 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4678 format
, column_width
, cb
->cb_scripted
);
4679 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4680 format
, column_width
, cb
->cb_scripted
);
4683 static const char *const class_name
[] = {
4684 VDEV_ALLOC_BIAS_DEDUP
,
4685 VDEV_ALLOC_BIAS_SPECIAL
,
4686 VDEV_ALLOC_CLASS_LOGS
4690 * Print out all the statistics for the given vdev. This can either be the
4691 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4692 * is a verbose output, and we don't want to display the toplevel pool stats.
4694 * Returns the number of stat lines printed.
4697 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4698 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4700 nvlist_t
**oldchild
, **newchild
;
4701 uint_t c
, children
, oldchildren
;
4702 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4703 vdev_stat_t zerovs
= { 0 };
4710 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4713 calcvs
= safe_malloc(sizeof (*calcvs
));
4715 if (oldnv
!= NULL
) {
4716 verify(nvlist_lookup_uint64_array(oldnv
,
4717 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4722 /* Do we only want to see a specific vdev? */
4723 for (i
= 0; i
< cb
->cb_vdevs
.cb_names_count
; i
++) {
4724 /* Yes we do. Is this the vdev? */
4725 if (strcmp(name
, cb
->cb_vdevs
.cb_names
[i
]) == 0) {
4727 * This is our vdev. Since it is the only vdev we
4728 * will be displaying, make depth = 0 so that it
4729 * doesn't get indented.
4736 if (cb
->cb_vdevs
.cb_names_count
&& (i
== cb
->cb_vdevs
.cb_names_count
)) {
4737 /* Couldn't match the name */
4742 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4743 (uint64_t **)&newvs
, &c
) == 0);
4746 * Print the vdev name unless it's is a histogram. Histograms
4747 * display the vdev name in the header itself.
4749 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4750 if (cb
->cb_scripted
) {
4753 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4754 (void) printf("%*s%s", depth
, "", name
);
4756 (void) printf("%*s%s%*s", depth
, "", name
,
4757 (int)(cb
->cb_namewidth
- strlen(name
) -
4762 /* Calculate our scaling factor */
4763 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4764 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4766 * If we specify printing histograms with no time interval, then
4767 * print the histogram numbers over the entire lifetime of the
4775 scale
= (double)NANOSEC
/ tdelta
;
4778 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4779 calc_default_iostats(oldvs
, newvs
, calcvs
);
4780 print_iostat_default(calcvs
, cb
, scale
);
4782 if (cb
->cb_flags
& IOS_LATENCY_M
)
4783 print_iostat_latency(cb
, oldnv
, newnv
);
4784 if (cb
->cb_flags
& IOS_QUEUES_M
)
4785 print_iostat_queues(cb
, newnv
);
4786 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4788 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4791 if (cb
->vcdl
!= NULL
) {
4793 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4796 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4800 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4809 if (!cb
->cb_verbose
)
4812 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4813 &newchild
, &children
) != 0)
4817 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4818 &oldchild
, &oldchildren
) != 0)
4821 children
= MIN(oldchildren
, children
);
4825 * print normal top-level devices
4827 for (c
= 0; c
< children
; c
++) {
4828 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4830 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4833 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4836 if (ishole
|| islog
)
4839 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4842 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4843 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
4844 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4845 newchild
[c
], cb
, depth
+ 2);
4850 * print all other top-level devices
4852 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
4853 boolean_t printed
= B_FALSE
;
4855 for (c
= 0; c
< children
; c
++) {
4856 uint64_t islog
= B_FALSE
;
4860 (void) nvlist_lookup_uint64(newchild
[c
],
4861 ZPOOL_CONFIG_IS_LOG
, &islog
);
4863 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
4865 (void) nvlist_lookup_string(newchild
[c
],
4866 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4867 (void) nvlist_lookup_string(newchild
[c
],
4868 ZPOOL_CONFIG_TYPE
, &type
);
4870 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4872 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4876 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4878 !cb
->cb_vdevs
.cb_names
) {
4879 print_iostat_dashes(cb
, 0,
4886 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4887 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
4888 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4889 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4895 * Include level 2 ARC devices in iostat output
4897 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4898 &newchild
, &children
) != 0)
4902 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4903 &oldchild
, &oldchildren
) != 0)
4906 children
= MIN(oldchildren
, children
);
4910 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4911 !cb
->cb_vdevs
.cb_names
) {
4912 print_iostat_dashes(cb
, 0, "cache");
4916 for (c
= 0; c
< children
; c
++) {
4917 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4918 cb
->cb_vdevs
.cb_name_flags
);
4919 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4920 : NULL
, newchild
[c
], cb
, depth
+ 2);
4929 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4931 iostat_cbdata_t
*cb
= data
;
4935 * If the pool has disappeared, remove it from the list and continue.
4937 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4941 pool_list_remove(cb
->cb_list
, zhp
);
4947 * Callback to print out the iostats for the given pool.
4950 print_iostat(zpool_handle_t
*zhp
, void *data
)
4952 iostat_cbdata_t
*cb
= data
;
4953 nvlist_t
*oldconfig
, *newconfig
;
4954 nvlist_t
*oldnvroot
, *newnvroot
;
4957 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4959 if (cb
->cb_iteration
== 1)
4962 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4965 if (oldconfig
== NULL
)
4968 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4971 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4973 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4974 !cb
->cb_scripted
&& cb
->cb_verbose
&&
4975 !cb
->cb_vdevs
.cb_names_count
) {
4976 print_iostat_separator(cb
);
4977 if (cb
->vcdl
!= NULL
) {
4978 print_cmd_columns(cb
->vcdl
, 1);
4993 if (isatty(STDOUT_FILENO
)) {
4994 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4996 columns
= ws
.ws_col
;
5005 * Return the required length of the pool/vdev name column. The minimum
5006 * allowed width and output formatting flags must be provided.
5009 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
5011 nvlist_t
*config
, *nvroot
;
5012 int width
= min_width
;
5014 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
5015 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5017 size_t poolname_len
= strlen(zpool_get_name(zhp
));
5018 if (verbose
== B_FALSE
) {
5019 width
= MAX(poolname_len
, min_width
);
5021 width
= MAX(poolname_len
,
5022 max_width(zhp
, nvroot
, 0, min_width
, flags
));
5030 * Parse the input string, get the 'interval' and 'count' value if there is one.
5033 get_interval_count(int *argcp
, char **argv
, float *iv
,
5037 unsigned long count
= 0;
5041 * Determine if the last argument is an integer or a pool name
5043 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5047 interval
= strtof(argv
[argc
- 1], &end
);
5049 if (*end
== '\0' && errno
== 0) {
5050 if (interval
== 0) {
5051 (void) fprintf(stderr
, gettext(
5052 "interval cannot be zero\n"));
5056 * Ignore the last parameter
5061 * If this is not a valid number, just plow on. The
5062 * user will get a more informative error message later
5070 * If the last argument is also an integer, then we have both a count
5073 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5078 interval
= strtof(argv
[argc
- 1], &end
);
5080 if (*end
== '\0' && errno
== 0) {
5081 if (interval
== 0) {
5082 (void) fprintf(stderr
, gettext(
5083 "interval cannot be zero\n"));
5088 * Ignore the last parameter
5102 get_timestamp_arg(char c
)
5105 timestamp_fmt
= UDATE
;
5107 timestamp_fmt
= DDATE
;
5113 * Return stat flags that are supported by all pools by both the module and
5114 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5115 * It will get ANDed down until only the flags that are supported on all pools
5119 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5121 uint64_t *mask
= data
;
5122 nvlist_t
*config
, *nvroot
, *nvx
;
5126 config
= zpool_get_config(zhp
, NULL
);
5127 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5130 /* Default stats are always supported, but for completeness.. */
5131 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5132 flags
|= IOS_DEFAULT_M
;
5134 /* Get our extended stats nvlist from the main list */
5135 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5138 * No extended stats; they're probably running an older
5139 * module. No big deal, we support that too.
5144 /* For each extended stat, make sure all its nvpairs are supported */
5145 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5146 if (!vsx_type_to_nvlist
[j
][0])
5149 /* Start off by assuming the flag is supported, then check */
5150 flags
|= (1ULL << j
);
5151 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5152 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5153 /* flag isn't supported */
5154 flags
= flags
& ~(1ULL << j
);
5160 *mask
= *mask
& flags
;
5165 * Return a bitmask of stats that are supported on all pools by both the module
5169 get_stat_flags(zpool_list_t
*list
)
5174 * get_stat_flags_cb() will lop off bits from "mask" until only the
5175 * flags that are supported on all pools remain.
5177 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5182 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5185 is_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *cb_data
)
5187 vdev_cbdata_t
*cb
= cb_data
;
5189 int ret
= 1; /* assume match */
5190 zpool_handle_t
*zhp
= zhp_data
;
5192 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
5194 if (strcmp(name
, cb
->cb_names
[0])) {
5196 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, VDEV_NAME_GUID
);
5197 ret
= (strcmp(name
, cb
->cb_names
[0]) == 0);
5205 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5208 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5210 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5214 * Check if vdevs are in a pool
5216 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5217 * return 0. If pool_name is NULL, then search all pools.
5220 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5228 if ((argc
== 0) || !*argv
)
5234 /* Temporarily hijack cb_names for a second... */
5235 tmp_name
= cb
->cb_names
;
5237 /* Go though our list of prospective vdev names */
5238 for (i
= 0; i
< argc
; i
++) {
5239 cb
->cb_names
= argv
+ i
;
5241 /* Is this name a vdev in our pools? */
5242 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5243 ZFS_TYPE_POOL
, B_FALSE
, is_vdev
, cb
);
5250 cb
->cb_names
= tmp_name
;
5256 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5259 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5266 * Do we have a pool named *name? If so, return 1, otherwise 0.
5271 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, ZFS_TYPE_POOL
, B_FALSE
,
5275 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5277 are_all_pools(int argc
, char **argv
)
5279 if ((argc
== 0) || !*argv
)
5283 if (!is_pool(argv
[argc
]))
5290 * Helper function to print out vdev/pool names we can't resolve. Used for an
5294 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5300 for (i
= 0; i
< argc
; i
++) {
5304 str
= gettext("pool");
5305 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5306 str
= gettext("vdev in this pool");
5307 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5308 str
= gettext("vdev in another pool");
5310 str
= gettext("unknown");
5312 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5317 * Same as get_interval_count(), but with additional checks to not misinterpret
5318 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5319 * cb.cb_vdevs.cb_name_flags.
5322 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5323 unsigned long *count
, iostat_cbdata_t
*cb
)
5325 char **tmpargv
= argv
;
5326 int argc_for_interval
= 0;
5328 /* Is the last arg an interval value? Or a guid? */
5329 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
,
5332 * The last arg is not a guid, so it's probably an
5335 argc_for_interval
++;
5338 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
,
5341 * The 2nd to last arg is not a guid, so it's probably
5342 * an interval value.
5344 argc_for_interval
++;
5348 /* Point to our list of possible intervals */
5349 tmpargv
= &argv
[*argc
- argc_for_interval
];
5351 *argc
= *argc
- argc_for_interval
;
5352 get_interval_count(&argc_for_interval
, tmpargv
,
5357 * Floating point sleep(). Allows you to pass in a floating point value for
5363 struct timespec req
;
5364 req
.tv_sec
= floor(sec
);
5365 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5366 nanosleep(&req
, NULL
);
5370 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5371 * if we were unable to determine its size.
5374 terminal_height(void)
5378 if (isatty(STDOUT_FILENO
) == 0)
5381 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5382 return (win
.ws_row
);
5388 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5391 * name: Short name of the script ('iostat').
5392 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5395 print_zpool_script_help(char *name
, char *path
)
5397 char *argv
[] = {path
, (char *)"-h", NULL
};
5398 char **lines
= NULL
;
5402 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5404 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5406 libzfs_free_str_array(lines
, lines_cnt
);
5410 for (int i
= 0; i
< lines_cnt
; i
++)
5411 if (!is_blank_str(lines
[i
]))
5412 printf(" %-14s %s\n", name
, lines
[i
]);
5414 libzfs_free_str_array(lines
, lines_cnt
);
5418 * Go though the zpool status/iostat -c scripts in the user's path, run their
5419 * help option (-h), and print out the results.
5422 print_zpool_dir_scripts(char *dirpath
)
5426 char fullpath
[MAXPATHLEN
];
5427 struct stat dir_stat
;
5429 if ((dir
= opendir(dirpath
)) != NULL
) {
5430 /* print all the files and directories within directory */
5431 while ((ent
= readdir(dir
)) != NULL
) {
5432 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5434 /* Print the scripts */
5435 if (stat(fullpath
, &dir_stat
) == 0)
5436 if (dir_stat
.st_mode
& S_IXUSR
&&
5437 S_ISREG(dir_stat
.st_mode
))
5438 print_zpool_script_help(ent
->d_name
,
5446 * Print out help text for all zpool status/iostat -c scripts.
5449 print_zpool_script_list(const char *subcommand
)
5451 char *dir
, *sp
, *tmp
;
5453 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5455 sp
= zpool_get_cmd_search_path();
5459 for (dir
= strtok_r(sp
, ":", &tmp
);
5461 dir
= strtok_r(NULL
, ":", &tmp
))
5462 print_zpool_dir_scripts(dir
);
5468 * Set the minimum pool/vdev name column width. The width must be at least 10,
5469 * but may be as large as the column width - 42 so it still fits on one line.
5470 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5473 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5475 iostat_cbdata_t
*cb
= data
;
5476 int width
, available_width
;
5479 * get_namewidth() returns the maximum width of any name in that column
5480 * for any pool/vdev/device line that will be output.
5482 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
5483 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
5486 * The width we are calculating is the width of the header and also the
5487 * padding width for names that are less than maximum width. The stats
5488 * take up 42 characters, so the width available for names is:
5490 available_width
= get_columns() - 42;
5493 * If the maximum width fits on a screen, then great! Make everything
5494 * line up by justifying all lines to the same width. If that max
5495 * width is larger than what's available, the name plus stats won't fit
5496 * on one line, and justifying to that width would cause every line to
5497 * wrap on the screen. We only want lines with long names to wrap.
5498 * Limit the padding to what won't wrap.
5500 if (width
> available_width
)
5501 width
= available_width
;
5504 * And regardless of whatever the screen width is (get_columns can
5505 * return 0 if the width is not known or less than 42 for a narrow
5506 * terminal) have the width be a minimum of 10.
5511 /* Save the calculated width */
5512 cb
->cb_namewidth
= width
;
5518 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5519 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5520 * [interval [count]]
5522 * -c CMD For each vdev, run command CMD
5523 * -g Display guid for individual vdev name.
5524 * -L Follow links when resolving vdev path name.
5525 * -P Display full path for vdev name.
5526 * -v Display statistics for individual vdevs
5528 * -p Display values in parsable (exact) format.
5529 * -H Scripted mode. Don't display headers, and separate properties
5531 * -l Display average latency
5532 * -q Display queue depths
5533 * -w Display latency histograms
5534 * -r Display request size histogram
5535 * -T Display a timestamp in date(1) or Unix format
5536 * -n Only print headers once
5538 * This command can be tricky because we want to be able to deal with pool
5539 * creation/destruction as well as vdev configuration changes. The bulk of this
5540 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5541 * on pool_list_update() to detect the addition of new pools. Configuration
5542 * changes are all handled within libzfs.
5545 zpool_do_iostat(int argc
, char **argv
)
5551 unsigned long count
= 0;
5554 boolean_t verbose
= B_FALSE
;
5555 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5556 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5557 boolean_t omit_since_boot
= B_FALSE
;
5558 boolean_t guid
= B_FALSE
;
5559 boolean_t follow_links
= B_FALSE
;
5560 boolean_t full_name
= B_FALSE
;
5561 boolean_t headers_once
= B_FALSE
;
5562 iostat_cbdata_t cb
= { 0 };
5565 /* Used for printing error message */
5566 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5567 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5569 uint64_t unsupported_flags
;
5572 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5577 gettext("Can't set -c flag twice\n"));
5581 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5582 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5583 fprintf(stderr
, gettext(
5584 "Can't run -c, disabled by "
5585 "ZPOOL_SCRIPTS_ENABLED.\n"));
5589 if ((getuid() <= 0 || geteuid() <= 0) &&
5590 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5591 fprintf(stderr
, gettext(
5592 "Can't run -c with root privileges "
5593 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5603 follow_links
= B_TRUE
;
5609 get_timestamp_arg(*optarg
);
5633 omit_since_boot
= B_TRUE
;
5636 headers_once
= B_TRUE
;
5642 if (optopt
== 'c') {
5643 print_zpool_script_list("iostat");
5647 gettext("invalid option '%c'\n"), optopt
);
5656 cb
.cb_literal
= parsable
;
5657 cb
.cb_scripted
= scripted
;
5660 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_GUID
;
5662 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5664 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_PATH
;
5665 cb
.cb_iteration
= 0;
5666 cb
.cb_namewidth
= 0;
5667 cb
.cb_verbose
= verbose
;
5669 /* Get our interval and count values (if any) */
5671 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5674 get_interval_count(&argc
, argv
, &interval
, &count
);
5678 /* No args, so just print the defaults. */
5679 } else if (are_all_pools(argc
, argv
)) {
5680 /* All the args are pool names */
5681 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
5682 /* All the args are vdevs */
5683 cb
.cb_vdevs
.cb_names
= argv
;
5684 cb
.cb_vdevs
.cb_names_count
= argc
;
5685 argc
= 0; /* No pools to process */
5686 } else if (are_all_pools(1, argv
)) {
5687 /* The first arg is a pool name */
5688 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
5690 /* ...and the rest are vdev names */
5691 cb
.cb_vdevs
.cb_names
= argv
+ 1;
5692 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
5693 argc
= 1; /* One pool to process */
5695 fprintf(stderr
, gettext("Expected either a list of "));
5696 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5697 fprintf(stderr
, " \"%s\", ", argv
[0]);
5698 fprintf(stderr
, gettext("but got:\n"));
5699 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5700 argv
[0], &cb
.cb_vdevs
);
5701 fprintf(stderr
, "\n");
5707 * The args don't make sense. The first arg isn't a pool name,
5708 * nor are all the args vdevs.
5710 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5711 fprintf(stderr
, "\n");
5715 if (cb
.cb_vdevs
.cb_names_count
!= 0) {
5717 * If user specified vdevs, it implies verbose.
5719 cb
.cb_verbose
= B_TRUE
;
5723 * Construct the list of all interesting pools.
5726 if ((list
= pool_list_get(argc
, argv
, NULL
, ZFS_TYPE_POOL
, parsable
,
5730 if (pool_list_count(list
) == 0 && argc
!= 0) {
5731 pool_list_free(list
);
5735 if (pool_list_count(list
) == 0 && interval
== 0) {
5736 pool_list_free(list
);
5737 (void) fprintf(stderr
, gettext("no pools available\n"));
5741 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5742 pool_list_free(list
);
5743 (void) fprintf(stderr
,
5744 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5749 if (l_histo
&& rq_histo
) {
5750 pool_list_free(list
);
5751 (void) fprintf(stderr
,
5752 gettext("Only one of [-r|-w] can be passed at a time\n"));
5758 * Enter the main iostat loop.
5764 * Histograms tables look out of place when you try to display
5765 * them with the other stats, so make a rule that you can only
5766 * print histograms by themselves.
5768 cb
.cb_flags
= IOS_L_HISTO_M
;
5769 } else if (rq_histo
) {
5770 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5772 cb
.cb_flags
= IOS_DEFAULT_M
;
5774 cb
.cb_flags
|= IOS_LATENCY_M
;
5776 cb
.cb_flags
|= IOS_QUEUES_M
;
5780 * See if the module supports all the stats we want to display.
5782 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5783 if (unsupported_flags
) {
5787 gettext("The loaded zfs module doesn't support:"));
5789 /* for each bit set in unsupported_flags */
5790 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5791 idx
= lowbit64(f
) - 1;
5792 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5795 fprintf(stderr
, ". Try running a newer module.\n");
5796 pool_list_free(list
);
5802 if ((npools
= pool_list_count(list
)) == 0)
5803 (void) fprintf(stderr
, gettext("no pools available\n"));
5806 * If this is the first iteration and -y was supplied
5807 * we skip any printing.
5809 boolean_t skip
= (omit_since_boot
&&
5810 cb
.cb_iteration
== 0);
5813 * Refresh all statistics. This is done as an
5814 * explicit step before calculating the maximum name
5815 * width, so that any * configuration changes are
5816 * properly accounted for.
5818 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5822 * Iterate over all pools to determine the maximum width
5823 * for the pool / device name column across all pools.
5825 cb
.cb_namewidth
= 0;
5826 (void) pool_list_iter(list
, B_FALSE
,
5827 get_namewidth_iostat
, &cb
);
5829 if (timestamp_fmt
!= NODATE
)
5830 print_timestamp(timestamp_fmt
);
5832 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5833 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5834 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5835 argv
, cmd
, g_zfs
, cb
.cb_vdevs
.cb_names
,
5836 cb
.cb_vdevs
.cb_names_count
,
5837 cb
.cb_vdevs
.cb_name_flags
);
5844 * Check terminal size so we can print headers
5845 * even when terminal window has its height
5848 winheight
= terminal_height();
5850 * Are we connected to TTY? If not, headers_once
5851 * should be true, to avoid breaking scripts.
5854 headers_once
= B_TRUE
;
5857 * If it's the first time and we're not skipping it,
5858 * or either skip or verbose mode, print the header.
5860 * The histogram code explicitly prints its header on
5861 * every vdev, so skip this for histograms.
5863 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5864 (skip
!= verbose
) ||
5866 (cb
.cb_iteration
% winheight
) == 0)) &&
5867 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5869 print_iostat_header(&cb
);
5872 (void) fsleep(interval
);
5876 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5879 * If there's more than one pool, and we're not in
5880 * verbose mode (which prints a separator for us),
5881 * then print a separator.
5883 * In addition, if we're printing specific vdevs then
5884 * we also want an ending separator.
5886 if (((npools
> 1 && !verbose
&&
5887 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5888 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5889 cb
.cb_vdevs
.cb_names_count
)) &&
5891 print_iostat_separator(&cb
);
5892 if (cb
.vcdl
!= NULL
)
5893 print_cmd_columns(cb
.vcdl
, 1);
5897 if (cb
.vcdl
!= NULL
)
5898 free_vdev_cmd_data_list(cb
.vcdl
);
5903 * Flush the output so that redirection to a file isn't buffered
5906 (void) fflush(stdout
);
5911 if (count
!= 0 && --count
== 0)
5914 (void) fsleep(interval
);
5917 pool_list_free(list
);
5922 typedef struct list_cbdata
{
5923 boolean_t cb_verbose
;
5926 boolean_t cb_scripted
;
5927 zprop_list_t
*cb_proplist
;
5928 boolean_t cb_literal
;
5933 * Given a list of columns to display, output appropriate headers for each one.
5936 print_header(list_cbdata_t
*cb
)
5938 zprop_list_t
*pl
= cb
->cb_proplist
;
5939 char headerbuf
[ZPOOL_MAXPROPLEN
];
5941 boolean_t first
= B_TRUE
;
5942 boolean_t right_justify
;
5945 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5946 width
= pl
->pl_width
;
5947 if (first
&& cb
->cb_verbose
) {
5949 * Reset the width to accommodate the verbose listing
5952 width
= cb
->cb_namewidth
;
5956 (void) fputs(" ", stdout
);
5960 right_justify
= B_FALSE
;
5961 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
5962 header
= zpool_prop_column_name(pl
->pl_prop
);
5963 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5967 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5968 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5969 headerbuf
[i
] = '\0';
5973 if (pl
->pl_next
== NULL
&& !right_justify
)
5974 (void) fputs(header
, stdout
);
5975 else if (right_justify
)
5976 (void) printf("%*s", (int)width
, header
);
5978 (void) printf("%-*s", (int)width
, header
);
5981 (void) fputc('\n', stdout
);
5985 * Given a pool and a list of properties, print out all the properties according
5986 * to the described layout. Used by zpool_do_list().
5989 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5991 zprop_list_t
*pl
= cb
->cb_proplist
;
5992 boolean_t first
= B_TRUE
;
5993 char property
[ZPOOL_MAXPROPLEN
];
5994 const char *propstr
;
5995 boolean_t right_justify
;
5998 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6000 width
= pl
->pl_width
;
6001 if (first
&& cb
->cb_verbose
) {
6003 * Reset the width to accommodate the verbose listing
6006 width
= cb
->cb_namewidth
;
6010 if (cb
->cb_scripted
)
6011 (void) fputc('\t', stdout
);
6013 (void) fputs(" ", stdout
);
6018 right_justify
= B_FALSE
;
6019 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6020 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
6021 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
6026 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6027 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
6028 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
6029 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
6030 sizeof (property
)) == 0) {
6038 * If this is being called in scripted mode, or if this is the
6039 * last column and it is left-justified, don't include a width
6042 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
6043 (void) fputs(propstr
, stdout
);
6044 else if (right_justify
)
6045 (void) printf("%*s", (int)width
, propstr
);
6047 (void) printf("%-*s", (int)width
, propstr
);
6050 (void) fputc('\n', stdout
);
6054 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
6055 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
6059 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6062 case ZPOOL_PROP_SIZE
:
6063 case ZPOOL_PROP_EXPANDSZ
:
6064 case ZPOOL_PROP_CHECKPOINT
:
6065 case ZPOOL_PROP_DEDUPRATIO
:
6067 (void) strlcpy(propval
, "-", sizeof (propval
));
6069 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6072 case ZPOOL_PROP_FRAGMENTATION
:
6073 if (value
== ZFS_FRAG_INVALID
) {
6074 (void) strlcpy(propval
, "-", sizeof (propval
));
6075 } else if (format
== ZFS_NICENUM_RAW
) {
6076 (void) snprintf(propval
, sizeof (propval
), "%llu",
6077 (unsigned long long)value
);
6079 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6080 (unsigned long long)value
);
6083 case ZPOOL_PROP_CAPACITY
:
6084 /* capacity value is in parts-per-10,000 (aka permyriad) */
6085 if (format
== ZFS_NICENUM_RAW
)
6086 (void) snprintf(propval
, sizeof (propval
), "%llu",
6087 (unsigned long long)value
/ 100);
6089 (void) snprintf(propval
, sizeof (propval
),
6090 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6091 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6093 case ZPOOL_PROP_HEALTH
:
6095 (void) strlcpy(propval
, str
, sizeof (propval
));
6098 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6102 (void) strlcpy(propval
, "-", sizeof (propval
));
6105 (void) printf("\t%s", propval
);
6107 (void) printf(" %*s", (int)width
, propval
);
6111 * print static default line per vdev
6112 * not compatible with '-o' <proplist> option
6115 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6116 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
6122 boolean_t scripted
= cb
->cb_scripted
;
6123 uint64_t islog
= B_FALSE
;
6124 const char *dashes
= "%-*s - - - - "
6127 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
6128 (uint64_t **)&vs
, &c
) == 0);
6131 boolean_t toplevel
= (vs
->vs_space
!= 0);
6133 enum zfs_nicenum_format format
;
6137 format
= ZFS_NICENUM_RAW
;
6139 format
= ZFS_NICENUM_1024
;
6141 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
6145 (void) printf("\t%s", name
);
6146 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
6147 (void) printf("%*s%s", depth
, "", name
);
6149 (void) printf("%*s%s%*s", depth
, "", name
,
6150 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
6153 * Print the properties for the individual vdevs. Some
6154 * properties are only applicable to toplevel vdevs. The
6155 * 'toplevel' boolean value is passed to the print_one_column()
6156 * to indicate that the value is valid.
6158 if (VDEV_STAT_VALID(vs_pspace
, c
) && vs
->vs_pspace
)
6159 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_pspace
, NULL
,
6160 scripted
, B_TRUE
, format
);
6162 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
,
6163 scripted
, toplevel
, format
);
6164 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6165 scripted
, toplevel
, format
);
6166 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6167 NULL
, scripted
, toplevel
, format
);
6168 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6169 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6170 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6171 scripted
, B_TRUE
, format
);
6172 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6173 vs
->vs_fragmentation
, NULL
, scripted
,
6174 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6176 cap
= (vs
->vs_space
== 0) ? 0 :
6177 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6178 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6179 scripted
, toplevel
, format
);
6180 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6181 scripted
, toplevel
, format
);
6182 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6184 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6186 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6189 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6191 (void) fputc('\n', stdout
);
6194 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6195 &child
, &children
) != 0)
6198 /* list the normal vdevs first */
6199 for (c
= 0; c
< children
; c
++) {
6200 uint64_t ishole
= B_FALSE
;
6202 if (nvlist_lookup_uint64(child
[c
],
6203 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6206 if (nvlist_lookup_uint64(child
[c
],
6207 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6210 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6213 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6214 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6215 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6219 /* list the classes: 'logs', 'dedup', and 'special' */
6220 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
6221 boolean_t printed
= B_FALSE
;
6223 for (c
= 0; c
< children
; c
++) {
6227 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6228 &islog
) == 0 && islog
) {
6229 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
6231 (void) nvlist_lookup_string(child
[c
],
6232 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6233 (void) nvlist_lookup_string(child
[c
],
6234 ZPOOL_CONFIG_TYPE
, &type
);
6236 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6238 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6242 /* LINTED E_SEC_PRINTF_VAR_FMT */
6243 (void) printf(dashes
, cb
->cb_namewidth
,
6247 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6248 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6249 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6255 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6256 &child
, &children
) == 0 && children
> 0) {
6257 /* LINTED E_SEC_PRINTF_VAR_FMT */
6258 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6259 for (c
= 0; c
< children
; c
++) {
6260 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6262 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6268 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6269 &children
) == 0 && children
> 0) {
6270 /* LINTED E_SEC_PRINTF_VAR_FMT */
6271 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6272 for (c
= 0; c
< children
; c
++) {
6273 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6275 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6283 * Generic callback function to list a pool.
6286 list_callback(zpool_handle_t
*zhp
, void *data
)
6288 list_cbdata_t
*cbp
= data
;
6290 print_pool(zhp
, cbp
);
6292 if (cbp
->cb_verbose
) {
6293 nvlist_t
*config
, *nvroot
;
6295 config
= zpool_get_config(zhp
, NULL
);
6296 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6298 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6305 * Set the minimum pool/vdev name column width. The width must be at least 9,
6306 * but may be as large as needed.
6309 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6311 list_cbdata_t
*cb
= data
;
6314 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
6315 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
6320 cb
->cb_namewidth
= width
;
6326 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6328 * -g Display guid for individual vdev name.
6329 * -H Scripted mode. Don't display headers, and separate properties
6331 * -L Follow links when resolving vdev path name.
6332 * -o List of properties to display. Defaults to
6333 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6334 * "dedupratio,health,altroot"
6335 * -p Display values in parsable (exact) format.
6336 * -P Display full path for vdev name.
6337 * -T Display a timestamp in date(1) or Unix format
6339 * List all pools in the system, whether or not they're healthy. Output space
6340 * statistics for each one, as well as health status summary.
6343 zpool_do_list(int argc
, char **argv
)
6347 list_cbdata_t cb
= { 0 };
6348 static char default_props
[] =
6349 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6350 "capacity,dedupratio,health,altroot";
6351 char *props
= default_props
;
6353 unsigned long count
= 0;
6355 boolean_t first
= B_TRUE
;
6356 current_prop_type
= ZFS_TYPE_POOL
;
6359 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6362 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6365 cb
.cb_scripted
= B_TRUE
;
6368 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6374 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6377 cb
.cb_literal
= B_TRUE
;
6380 get_timestamp_arg(*optarg
);
6383 cb
.cb_verbose
= B_TRUE
;
6384 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6387 (void) fprintf(stderr
, gettext("missing argument for "
6388 "'%c' option\n"), optopt
);
6392 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6401 get_interval_count(&argc
, argv
, &interval
, &count
);
6403 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6407 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6408 ZFS_TYPE_POOL
, cb
.cb_literal
, &ret
)) == NULL
)
6411 if (pool_list_count(list
) == 0)
6414 cb
.cb_namewidth
= 0;
6415 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6417 if (timestamp_fmt
!= NODATE
)
6418 print_timestamp(timestamp_fmt
);
6420 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6424 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6429 if (count
!= 0 && --count
== 0)
6432 pool_list_free(list
);
6433 (void) fsleep(interval
);
6436 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6437 (void) printf(gettext("no pools available\n"));
6441 pool_list_free(list
);
6442 zprop_free_list(cb
.cb_proplist
);
6447 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6449 boolean_t force
= B_FALSE
;
6450 boolean_t rebuild
= B_FALSE
;
6451 boolean_t wait
= B_FALSE
;
6454 char *poolname
, *old_disk
, *new_disk
;
6455 zpool_handle_t
*zhp
;
6456 nvlist_t
*props
= NULL
;
6461 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6467 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6468 (void) fprintf(stderr
, gettext("missing "
6469 "'=' for -o option\n"));
6475 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6476 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6486 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6495 /* get pool name and check number of arguments */
6497 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6504 (void) fprintf(stderr
,
6505 gettext("missing <device> specification\n"));
6513 (void) fprintf(stderr
,
6514 gettext("missing <new_device> specification\n"));
6517 new_disk
= old_disk
;
6527 (void) fprintf(stderr
, gettext("too many arguments\n"));
6531 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6536 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6537 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6544 /* unless manually specified use "ashift" pool property (if set) */
6545 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6548 char strval
[ZPOOL_MAXPROPLEN
];
6550 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6551 if (src
!= ZPROP_SRC_DEFAULT
) {
6552 (void) sprintf(strval
, "%" PRId32
, intval
);
6553 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6554 &props
, B_TRUE
) == 0);
6558 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6560 if (nvroot
== NULL
) {
6566 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6569 if (ret
== 0 && wait
)
6570 ret
= zpool_wait(zhp
,
6571 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6574 nvlist_free(nvroot
);
6581 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6583 * -f Force attach, even if <new_device> appears to be in use.
6584 * -s Use sequential instead of healing reconstruction for resilver.
6585 * -o Set property=value.
6586 * -w Wait for replacing to complete before returning
6588 * Replace <device> with <new_device>.
6591 zpool_do_replace(int argc
, char **argv
)
6593 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6597 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6599 * -f Force attach, even if <new_device> appears to be in use.
6600 * -s Use sequential instead of healing reconstruction for resilver.
6601 * -o Set property=value.
6602 * -w Wait for resilvering to complete before returning
6604 * Attach <new_device> to the mirror containing <device>. If <device> is not
6605 * part of a mirror, then <device> will be transformed into a mirror of
6606 * <device> and <new_device>. In either case, <new_device> will begin life
6607 * with a DTL of [0, now], and will immediately begin to resilver itself.
6610 zpool_do_attach(int argc
, char **argv
)
6612 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6616 * zpool detach [-f] <pool> <device>
6618 * -f Force detach of <device>, even if DTLs argue against it
6619 * (not supported yet)
6621 * Detach a device from a mirror. The operation will be refused if <device>
6622 * is the last device in the mirror, or if the DTLs indicate that this device
6623 * has the only valid copy of some data.
6626 zpool_do_detach(int argc
, char **argv
)
6629 char *poolname
, *path
;
6630 zpool_handle_t
*zhp
;
6634 while ((c
= getopt(argc
, argv
, "")) != -1) {
6637 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6646 /* get pool name and check number of arguments */
6648 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6653 (void) fprintf(stderr
,
6654 gettext("missing <device> specification\n"));
6661 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6664 ret
= zpool_vdev_detach(zhp
, path
);
6672 * zpool split [-gLnP] [-o prop=val] ...
6674 * [-R altroot] <pool> <newpool> [<device> ...]
6676 * -g Display guid for individual vdev name.
6677 * -L Follow links when resolving vdev path name.
6678 * -n Do not split the pool, but display the resulting layout if
6679 * it were to be split.
6680 * -o Set property=value, or set mount options.
6681 * -P Display full path for vdev name.
6682 * -R Mount the split-off pool under an alternate root.
6683 * -l Load encryption keys while importing.
6685 * Splits the named pool and gives it the new pool name. Devices to be split
6686 * off may be listed, provided that no more than one device is specified
6687 * per top-level vdev mirror. The newly split pool is left in an exported
6688 * state unless -R is specified.
6690 * Restrictions: the top-level of the pool pool must only be made up of
6691 * mirrors; all devices in the pool must be healthy; no device may be
6692 * undergoing a resilvering operation.
6695 zpool_do_split(int argc
, char **argv
)
6697 char *srcpool
, *newpool
, *propval
;
6698 char *mntopts
= NULL
;
6701 boolean_t loadkeys
= B_FALSE
;
6702 zpool_handle_t
*zhp
;
6703 nvlist_t
*config
, *props
= NULL
;
6705 flags
.dryrun
= B_FALSE
;
6706 flags
.import
= B_FALSE
;
6707 flags
.name_flags
= 0;
6710 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6713 flags
.name_flags
|= VDEV_NAME_GUID
;
6716 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6719 flags
.import
= B_TRUE
;
6721 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6722 &props
, B_TRUE
) != 0) {
6731 flags
.dryrun
= B_TRUE
;
6734 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6737 if (add_prop_list(optarg
, propval
,
6738 &props
, B_TRUE
) != 0) {
6747 flags
.name_flags
|= VDEV_NAME_PATH
;
6750 (void) fprintf(stderr
, gettext("missing argument for "
6751 "'%c' option\n"), optopt
);
6755 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6762 if (!flags
.import
&& mntopts
!= NULL
) {
6763 (void) fprintf(stderr
, gettext("setting mntopts is only "
6764 "valid when importing the pool\n"));
6768 if (!flags
.import
&& loadkeys
) {
6769 (void) fprintf(stderr
, gettext("loading keys is only "
6770 "valid when importing the pool\n"));
6778 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6782 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6792 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6797 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6798 if (config
== NULL
) {
6802 (void) printf(gettext("would create '%s' with the "
6803 "following layout:\n\n"), newpool
);
6804 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6806 print_vdev_tree(NULL
, "dedup", config
, 0,
6807 VDEV_ALLOC_BIAS_DEDUP
, 0);
6808 print_vdev_tree(NULL
, "special", config
, 0,
6809 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6815 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6816 nvlist_free(config
);
6822 * The split was successful. Now we need to open the new
6823 * pool and import it.
6825 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6826 nvlist_free(config
);
6832 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6837 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6838 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6840 (void) fprintf(stderr
, gettext("Split was successful, but "
6841 "the datasets could not all be mounted\n"));
6842 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6843 "different altroot\n"), "zpool import");
6846 nvlist_free(config
);
6855 * zpool online <pool> <device> ...
6858 zpool_do_online(int argc
, char **argv
)
6862 zpool_handle_t
*zhp
;
6864 vdev_state_t newstate
;
6868 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6871 flags
|= ZFS_ONLINE_EXPAND
;
6874 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6883 /* get pool name and check number of arguments */
6885 (void) fprintf(stderr
, gettext("missing pool name\n"));
6889 (void) fprintf(stderr
, gettext("missing device name\n"));
6895 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6898 for (i
= 1; i
< argc
; i
++) {
6899 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6900 if (newstate
!= VDEV_STATE_HEALTHY
) {
6901 (void) printf(gettext("warning: device '%s' "
6902 "onlined, but remains in faulted state\n"),
6904 if (newstate
== VDEV_STATE_FAULTED
)
6905 (void) printf(gettext("use 'zpool "
6906 "clear' to restore a faulted "
6909 (void) printf(gettext("use 'zpool "
6910 "replace' to replace devices "
6911 "that are no longer present\n"));
6924 * zpool offline [-ft] <pool> <device> ...
6926 * -f Force the device into a faulted state.
6928 * -t Only take the device off-line temporarily. The offline/faulted
6929 * state will not be persistent across reboots.
6932 zpool_do_offline(int argc
, char **argv
)
6936 zpool_handle_t
*zhp
;
6938 boolean_t istmp
= B_FALSE
;
6939 boolean_t fault
= B_FALSE
;
6942 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6951 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6960 /* get pool name and check number of arguments */
6962 (void) fprintf(stderr
, gettext("missing pool name\n"));
6966 (void) fprintf(stderr
, gettext("missing device name\n"));
6972 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6975 for (i
= 1; i
< argc
; i
++) {
6977 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6979 if (istmp
== B_FALSE
) {
6980 /* Force the fault to persist across imports */
6981 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6983 aux
= VDEV_AUX_EXTERNAL
;
6986 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6989 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
7000 * zpool clear <pool> [device]
7002 * Clear all errors associated with a pool or a particular device.
7005 zpool_do_clear(int argc
, char **argv
)
7009 boolean_t dryrun
= B_FALSE
;
7010 boolean_t do_rewind
= B_FALSE
;
7011 boolean_t xtreme_rewind
= B_FALSE
;
7012 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
7013 nvlist_t
*policy
= NULL
;
7014 zpool_handle_t
*zhp
;
7015 char *pool
, *device
;
7018 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
7027 xtreme_rewind
= B_TRUE
;
7030 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7040 (void) fprintf(stderr
, gettext("missing pool name\n"));
7045 (void) fprintf(stderr
, gettext("too many arguments\n"));
7049 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
7050 (void) fprintf(stderr
,
7051 gettext("-n or -X only meaningful with -F\n"));
7055 rewind_policy
= ZPOOL_TRY_REWIND
;
7057 rewind_policy
= ZPOOL_DO_REWIND
;
7059 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
7061 /* In future, further rewind policy choices can be passed along here */
7062 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
7063 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
7064 rewind_policy
) != 0) {
7069 device
= argc
== 2 ? argv
[1] : NULL
;
7071 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
7072 nvlist_free(policy
);
7076 if (zpool_clear(zhp
, device
, policy
) != 0)
7081 nvlist_free(policy
);
7087 * zpool reguid <pool>
7090 zpool_do_reguid(int argc
, char **argv
)
7094 zpool_handle_t
*zhp
;
7098 while ((c
= getopt(argc
, argv
, "")) != -1) {
7101 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7110 /* get pool name and check number of arguments */
7112 (void) fprintf(stderr
, gettext("missing pool name\n"));
7117 (void) fprintf(stderr
, gettext("too many arguments\n"));
7122 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7125 ret
= zpool_reguid(zhp
);
7133 * zpool reopen <pool>
7135 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7138 zpool_do_reopen(int argc
, char **argv
)
7142 boolean_t scrub_restart
= B_TRUE
;
7145 while ((c
= getopt(argc
, argv
, "n")) != -1) {
7148 scrub_restart
= B_FALSE
;
7151 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7160 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7161 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7162 B_FALSE
, zpool_reopen_one
, &scrub_restart
);
7167 typedef struct scrub_cbdata
{
7169 pool_scrub_cmd_t cb_scrub_cmd
;
7173 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7175 nvlist_t
*config
, *nvroot
;
7177 config
= zpool_get_config(zhp
, NULL
);
7179 if (config
!= NULL
) {
7180 pool_checkpoint_stat_t
*pcs
= NULL
;
7183 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7184 (void) nvlist_lookup_uint64_array(nvroot
,
7185 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7187 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7190 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7191 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7199 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7201 scrub_cbdata_t
*cb
= data
;
7205 * Ignore faulted pools.
7207 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7208 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7209 "currently unavailable\n"), zpool_get_name(zhp
));
7213 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7215 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7216 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7217 (void) printf(gettext("warning: will not scrub state that "
7218 "belongs to the checkpoint of pool '%s'\n"),
7219 zpool_get_name(zhp
));
7226 wait_callback(zpool_handle_t
*zhp
, void *data
)
7228 zpool_wait_activity_t
*act
= data
;
7229 return (zpool_wait(zhp
, *act
));
7233 * zpool scrub [-s | -p] [-w] <pool> ...
7235 * -s Stop. Stops any in-progress scrub.
7236 * -p Pause. Pause in-progress scrub.
7237 * -w Wait. Blocks until scrub has completed.
7240 zpool_do_scrub(int argc
, char **argv
)
7244 boolean_t wait
= B_FALSE
;
7247 cb
.cb_type
= POOL_SCAN_SCRUB
;
7248 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7251 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7254 cb
.cb_type
= POOL_SCAN_NONE
;
7257 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7263 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7269 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7270 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7271 (void) fprintf(stderr
, gettext("invalid option combination: "
7272 "-s and -p are mutually exclusive\n"));
7276 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7277 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7278 (void) fprintf(stderr
, gettext("invalid option combination: "
7279 "-w cannot be used with -p or -s\n"));
7287 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7291 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7292 B_FALSE
, scrub_callback
, &cb
);
7294 if (wait
&& !error
) {
7295 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7296 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7297 B_FALSE
, wait_callback
, &act
);
7304 * zpool resilver <pool> ...
7306 * Restarts any in-progress resilver
7309 zpool_do_resilver(int argc
, char **argv
)
7314 cb
.cb_type
= POOL_SCAN_RESILVER
;
7315 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7318 while ((c
= getopt(argc
, argv
, "")) != -1) {
7321 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7331 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7335 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7336 B_FALSE
, scrub_callback
, &cb
));
7340 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7342 * -c Cancel. Ends any in-progress trim.
7343 * -d Secure trim. Requires kernel and device support.
7344 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7345 * adding a multiplier suffix such as 'k' or 'm'.
7346 * -s Suspend. TRIM can then be restarted with no flags.
7347 * -w Wait. Blocks until trimming has completed.
7350 zpool_do_trim(int argc
, char **argv
)
7352 struct option long_options
[] = {
7353 {"cancel", no_argument
, NULL
, 'c'},
7354 {"secure", no_argument
, NULL
, 'd'},
7355 {"rate", required_argument
, NULL
, 'r'},
7356 {"suspend", no_argument
, NULL
, 's'},
7357 {"wait", no_argument
, NULL
, 'w'},
7361 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7363 boolean_t secure
= B_FALSE
;
7364 boolean_t wait
= B_FALSE
;
7367 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7371 if (cmd_type
!= POOL_TRIM_START
&&
7372 cmd_type
!= POOL_TRIM_CANCEL
) {
7373 (void) fprintf(stderr
, gettext("-c cannot be "
7374 "combined with other options\n"));
7377 cmd_type
= POOL_TRIM_CANCEL
;
7380 if (cmd_type
!= POOL_TRIM_START
) {
7381 (void) fprintf(stderr
, gettext("-d cannot be "
7382 "combined with the -c or -s options\n"));
7388 if (cmd_type
!= POOL_TRIM_START
) {
7389 (void) fprintf(stderr
, gettext("-r cannot be "
7390 "combined with the -c or -s options\n"));
7393 if (zfs_nicestrtonum(g_zfs
, optarg
, &rate
) == -1) {
7394 (void) fprintf(stderr
, "%s: %s\n",
7395 gettext("invalid value for rate"),
7396 libzfs_error_description(g_zfs
));
7401 if (cmd_type
!= POOL_TRIM_START
&&
7402 cmd_type
!= POOL_TRIM_SUSPEND
) {
7403 (void) fprintf(stderr
, gettext("-s cannot be "
7404 "combined with other options\n"));
7407 cmd_type
= POOL_TRIM_SUSPEND
;
7414 (void) fprintf(stderr
,
7415 gettext("invalid option '%c'\n"), optopt
);
7417 (void) fprintf(stderr
,
7418 gettext("invalid option '%s'\n"),
7429 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7434 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7435 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7440 char *poolname
= argv
[0];
7441 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7445 trimflags_t trim_flags
= {
7451 nvlist_t
*vdevs
= fnvlist_alloc();
7453 /* no individual leaf vdevs specified, so add them all */
7454 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7455 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7456 ZPOOL_CONFIG_VDEV_TREE
);
7457 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7458 trim_flags
.fullpool
= B_TRUE
;
7460 trim_flags
.fullpool
= B_FALSE
;
7461 for (int i
= 1; i
< argc
; i
++) {
7462 fnvlist_add_boolean(vdevs
, argv
[i
]);
7466 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7468 fnvlist_free(vdevs
);
7475 * Converts a total number of seconds to a human readable string broken
7476 * down in to days/hours/minutes/seconds.
7479 secs_to_dhms(uint64_t total
, char *buf
)
7481 uint64_t days
= total
/ 60 / 60 / 24;
7482 uint64_t hours
= (total
/ 60 / 60) % 24;
7483 uint64_t mins
= (total
/ 60) % 60;
7484 uint64_t secs
= (total
% 60);
7487 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7488 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7489 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7491 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7492 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7493 (u_longlong_t
)secs
);
7498 * Print out detailed scrub status.
7501 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7503 time_t start
, end
, pause
;
7504 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7505 uint64_t elapsed
, scan_rate
, issue_rate
;
7506 double fraction_done
;
7507 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7508 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7511 printf_color(ANSI_BOLD
, gettext("scan:"));
7514 /* If there's never been a scan, there's not much to say. */
7515 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7516 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7517 (void) printf(gettext("none requested\n"));
7521 start
= ps
->pss_start_time
;
7522 end
= ps
->pss_end_time
;
7523 pause
= ps
->pss_pass_scrub_pause
;
7525 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7527 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7528 ps
->pss_func
== POOL_SCAN_RESILVER
);
7530 /* Scan is finished or canceled. */
7531 if (ps
->pss_state
== DSS_FINISHED
) {
7532 secs_to_dhms(end
- start
, time_buf
);
7534 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7535 (void) printf(gettext("scrub repaired %s "
7536 "in %s with %llu errors on %s"), processed_buf
,
7537 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7539 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7540 (void) printf(gettext("resilvered %s "
7541 "in %s with %llu errors on %s"), processed_buf
,
7542 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7546 } else if (ps
->pss_state
== DSS_CANCELED
) {
7547 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7548 (void) printf(gettext("scrub canceled on %s"),
7550 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7551 (void) printf(gettext("resilver canceled on %s"),
7557 assert(ps
->pss_state
== DSS_SCANNING
);
7559 /* Scan is in progress. Resilvers can't be paused. */
7560 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7562 (void) printf(gettext("scrub in progress since %s"),
7565 (void) printf(gettext("scrub paused since %s"),
7567 (void) printf(gettext("\tscrub started on %s"),
7570 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7571 (void) printf(gettext("resilver in progress since %s"),
7575 scanned
= ps
->pss_examined
;
7576 pass_scanned
= ps
->pss_pass_exam
;
7577 issued
= ps
->pss_issued
;
7578 pass_issued
= ps
->pss_pass_issued
;
7579 total
= ps
->pss_to_examine
;
7581 /* we are only done with a block once we have issued the IO for it */
7582 fraction_done
= (double)issued
/ total
;
7584 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7585 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7586 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7587 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7589 scan_rate
= pass_scanned
/ elapsed
;
7590 issue_rate
= pass_issued
/ elapsed
;
7591 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7592 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7593 secs_to_dhms(total_secs_left
, time_buf
);
7595 /* format all of the numbers we will be reporting */
7596 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7597 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7598 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7599 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7600 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7602 /* do not print estimated time if we have a paused scrub */
7604 (void) printf(gettext("\t%s scanned at %s/s, "
7605 "%s issued at %s/s, %s total\n"),
7606 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7608 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7609 scanned_buf
, issued_buf
, total_buf
);
7612 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7613 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7614 processed_buf
, 100 * fraction_done
);
7615 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7616 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7617 processed_buf
, 100 * fraction_done
);
7621 if (total_secs_left
!= UINT64_MAX
&&
7622 issue_rate
>= 10 * 1024 * 1024) {
7623 (void) printf(gettext(", %s to go\n"), time_buf
);
7625 (void) printf(gettext(", no estimated "
7626 "completion time\n"));
7629 (void) printf(gettext("\n"));
7634 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7636 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7640 printf_color(ANSI_BOLD
, gettext("scan:"));
7643 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7644 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7645 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7646 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7647 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7648 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7649 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7650 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7651 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7652 (bytes_est
+ 1), 100);
7654 /* Format all of the numbers we will be reporting */
7655 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7656 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7657 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7658 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7659 sizeof (bytes_scanned_buf
));
7660 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7661 sizeof (bytes_issued_buf
));
7662 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7663 sizeof (bytes_rebuilt_buf
));
7664 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7665 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7666 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7668 time_t start
= vrs
->vrs_start_time
;
7669 time_t end
= vrs
->vrs_end_time
;
7671 /* Rebuild is finished or canceled. */
7672 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7673 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7674 (void) printf(gettext("resilvered (%s) %s in %s "
7675 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7676 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7678 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7679 (void) printf(gettext("resilver (%s) canceled on %s"),
7680 vdev_name
, ctime(&end
));
7682 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7683 (void) printf(gettext("resilver (%s) in progress since %s"),
7684 vdev_name
, ctime(&start
));
7687 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7689 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7690 MAX(scan_rate
, 1), time_buf
);
7692 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7693 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7694 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7695 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7696 bytes_rebuilt_buf
, scan_pct
);
7698 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7699 if (scan_rate
>= 10 * 1024 * 1024) {
7700 (void) printf(gettext(", %s to go\n"), time_buf
);
7702 (void) printf(gettext(", no estimated "
7703 "completion time\n"));
7706 (void) printf(gettext("\n"));
7711 * Print rebuild status for top-level vdevs.
7714 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7719 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7720 &child
, &children
) != 0)
7723 for (uint_t c
= 0; c
< children
; c
++) {
7724 vdev_rebuild_stat_t
*vrs
;
7727 if (nvlist_lookup_uint64_array(child
[c
],
7728 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7729 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7730 child
[c
], VDEV_NAME_TYPE_ID
);
7731 print_rebuild_status_impl(vrs
, name
);
7738 * As we don't scrub checkpointed blocks, we want to warn the user that we
7739 * skipped scanning some blocks if a checkpoint exists or existed at any
7740 * time during the scan. If a sequential instead of healing reconstruction
7741 * was performed then the blocks were reconstructed. However, their checksums
7742 * have not been verified so we still print the warning.
7745 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7747 if (ps
== NULL
|| pcs
== NULL
)
7750 if (pcs
->pcs_state
== CS_NONE
||
7751 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7754 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7756 if (ps
->pss_state
== DSS_NONE
)
7759 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7760 ps
->pss_end_time
< pcs
->pcs_start_time
)
7763 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7764 (void) printf(gettext(" scan warning: skipped blocks "
7765 "that are only referenced by the checkpoint.\n"));
7767 assert(ps
->pss_state
== DSS_SCANNING
);
7768 (void) printf(gettext(" scan warning: skipping blocks "
7769 "that are only referenced by the checkpoint.\n"));
7774 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7775 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7776 * the last completed (or cancelled) rebuild.
7779 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7783 boolean_t rebuilding
= B_FALSE
;
7784 uint64_t end_time
= 0;
7786 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7787 &child
, &children
) != 0)
7790 for (uint_t c
= 0; c
< children
; c
++) {
7791 vdev_rebuild_stat_t
*vrs
;
7794 if (nvlist_lookup_uint64_array(child
[c
],
7795 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7797 if (vrs
->vrs_end_time
> end_time
)
7798 end_time
= vrs
->vrs_end_time
;
7800 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7801 rebuilding
= B_TRUE
;
7808 if (rebuild_end_time
!= NULL
)
7809 *rebuild_end_time
= end_time
;
7811 return (rebuilding
);
7815 * Print the scan status.
7818 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7820 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7821 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7822 boolean_t active_resilver
= B_FALSE
;
7823 pool_checkpoint_stat_t
*pcs
= NULL
;
7824 pool_scan_stat_t
*ps
= NULL
;
7827 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7828 (uint64_t **)&ps
, &c
) == 0) {
7829 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7830 resilver_end_time
= ps
->pss_end_time
;
7831 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7834 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7835 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7838 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7839 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7841 /* Always print the scrub status when available. */
7843 print_scan_scrub_resilver_status(ps
);
7846 * When there is an active resilver or rebuild print its status.
7847 * Otherwise print the status of the last resilver or rebuild.
7849 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7850 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7851 print_scan_scrub_resilver_status(ps
);
7852 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7853 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7854 print_rebuild_status(zhp
, nvroot
);
7857 (void) nvlist_lookup_uint64_array(nvroot
,
7858 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7859 print_checkpoint_scan_warning(ps
, pcs
);
7863 * Print out detailed removal status.
7866 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7868 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7870 nvlist_t
*config
, *nvroot
;
7875 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7879 * Determine name of vdev.
7881 config
= zpool_get_config(zhp
, NULL
);
7882 nvroot
= fnvlist_lookup_nvlist(config
,
7883 ZPOOL_CONFIG_VDEV_TREE
);
7884 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7885 &child
, &children
) == 0);
7886 assert(prs
->prs_removing_vdev
< children
);
7887 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7888 child
[prs
->prs_removing_vdev
], B_TRUE
);
7890 printf_color(ANSI_BOLD
, gettext("remove: "));
7892 start
= prs
->prs_start_time
;
7893 end
= prs
->prs_end_time
;
7894 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7897 * Removal is finished or canceled.
7899 if (prs
->prs_state
== DSS_FINISHED
) {
7900 uint64_t minutes_taken
= (end
- start
) / 60;
7902 (void) printf(gettext("Removal of vdev %llu copied %s "
7903 "in %lluh%um, completed on %s"),
7904 (longlong_t
)prs
->prs_removing_vdev
,
7906 (u_longlong_t
)(minutes_taken
/ 60),
7907 (uint_t
)(minutes_taken
% 60),
7908 ctime((time_t *)&end
));
7909 } else if (prs
->prs_state
== DSS_CANCELED
) {
7910 (void) printf(gettext("Removal of %s canceled on %s"),
7911 vdev_name
, ctime(&end
));
7913 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7914 double fraction_done
;
7917 assert(prs
->prs_state
== DSS_SCANNING
);
7920 * Removal is in progress.
7922 (void) printf(gettext(
7923 "Evacuation of %s in progress since %s"),
7924 vdev_name
, ctime(&start
));
7926 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7927 total
= prs
->prs_to_copy
;
7928 fraction_done
= (double)copied
/ total
;
7930 /* elapsed time for this pass */
7931 elapsed
= time(NULL
) - prs
->prs_start_time
;
7932 elapsed
= elapsed
> 0 ? elapsed
: 1;
7933 rate
= copied
/ elapsed
;
7934 rate
= rate
> 0 ? rate
: 1;
7935 mins_left
= ((total
- copied
) / rate
) / 60;
7936 hours_left
= mins_left
/ 60;
7938 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7939 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7940 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7943 * do not print estimated time if hours_left is more than
7946 (void) printf(gettext(
7947 "\t%s copied out of %s at %s/s, %.2f%% done"),
7948 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7949 if (hours_left
< (30 * 24)) {
7950 (void) printf(gettext(", %lluh%um to go\n"),
7951 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7953 (void) printf(gettext(
7954 ", (copy is slow, no estimated time)\n"));
7959 if (prs
->prs_mapping_memory
> 0) {
7961 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7962 (void) printf(gettext(
7963 "\t%s memory used for removed device mappings\n"),
7969 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7974 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7977 (void) printf(gettext("checkpoint: "));
7979 start
= pcs
->pcs_start_time
;
7980 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7982 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7983 char *date
= ctime(&start
);
7986 * ctime() adds a newline at the end of the generated
7987 * string, thus the weird format specifier and the
7988 * strlen() call used to chop it off from the output.
7990 (void) printf(gettext("created %.*s, consumes %s\n"),
7991 (int)(strlen(date
) - 1), date
, space_buf
);
7995 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7997 (void) printf(gettext("discarding, %s remaining.\n"),
8002 print_error_log(zpool_handle_t
*zhp
)
8004 nvlist_t
*nverrlist
= NULL
;
8007 size_t len
= MAXPATHLEN
* 2;
8009 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
8012 (void) printf("errors: Permanent errors have been "
8013 "detected in the following files:\n\n");
8015 pathname
= safe_malloc(len
);
8017 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
8019 uint64_t dsobj
, obj
;
8021 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
8022 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
8024 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
8026 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
8027 (void) printf("%7s %s\n", "", pathname
);
8030 nvlist_free(nverrlist
);
8034 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
8043 (void) printf(gettext("\tspares\n"));
8045 for (i
= 0; i
< nspares
; i
++) {
8046 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
8048 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
8054 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
8063 (void) printf(gettext("\tcache\n"));
8065 for (i
= 0; i
< nl2cache
; i
++) {
8066 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
8068 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
8075 print_dedup_stats(nvlist_t
*config
)
8077 ddt_histogram_t
*ddh
;
8081 char dspace
[6], mspace
[6];
8084 * If the pool was faulted then we may not have been able to
8085 * obtain the config. Otherwise, if we have anything in the dedup
8086 * table continue processing the stats.
8088 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
8089 (uint64_t **)&ddo
, &c
) != 0)
8092 (void) printf("\n");
8093 (void) printf(gettext(" dedup: "));
8094 if (ddo
->ddo_count
== 0) {
8095 (void) printf(gettext("no DDT entries\n"));
8099 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
8100 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
8101 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8102 (u_longlong_t
)ddo
->ddo_count
,
8106 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
8107 (uint64_t **)&dds
, &c
) == 0);
8108 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
8109 (uint64_t **)&ddh
, &c
) == 0);
8110 zpool_dump_ddt(dds
, ddh
);
8114 * Display a summary of pool status. Displays a summary such as:
8118 * reason: One or more devices ...
8119 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8125 * When given the '-v' option, we print out the complete config. If the '-e'
8126 * option is specified, then we print out error rate information as well.
8129 status_callback(zpool_handle_t
*zhp
, void *data
)
8131 status_cbdata_t
*cbp
= data
;
8132 nvlist_t
*config
, *nvroot
;
8134 zpool_status_t reason
;
8135 zpool_errata_t errata
;
8140 config
= zpool_get_config(zhp
, NULL
);
8141 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
8146 * If we were given 'zpool status -x', only report those pools with
8149 if (cbp
->cb_explain
&&
8150 (reason
== ZPOOL_STATUS_OK
||
8151 reason
== ZPOOL_STATUS_VERSION_OLDER
||
8152 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
8153 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
8154 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
8155 if (!cbp
->cb_allpools
) {
8156 (void) printf(gettext("pool '%s' is healthy\n"),
8157 zpool_get_name(zhp
));
8159 cbp
->cb_first
= B_FALSE
;
8165 cbp
->cb_first
= B_FALSE
;
8167 (void) printf("\n");
8169 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8170 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8171 (uint64_t **)&vs
, &c
) == 0);
8173 health
= zpool_get_state_str(zhp
);
8176 printf_color(ANSI_BOLD
, gettext("pool:"));
8177 printf(" %s\n", zpool_get_name(zhp
));
8179 printf_color(ANSI_BOLD
, gettext("state: "));
8181 printf_color(health_str_to_color(health
), "%s", health
);
8183 fputc('\n', stdout
);
8186 case ZPOOL_STATUS_MISSING_DEV_R
:
8187 printf_color(ANSI_BOLD
, gettext("status: "));
8188 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8189 "not be opened. Sufficient replicas exist for\n\tthe pool "
8190 "to continue functioning in a degraded state.\n"));
8191 printf_color(ANSI_BOLD
, gettext("action: "));
8192 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8193 "and online it using 'zpool online'.\n"));
8196 case ZPOOL_STATUS_MISSING_DEV_NR
:
8197 printf_color(ANSI_BOLD
, gettext("status: "));
8198 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8199 "not be opened. There are insufficient\n\treplicas for the"
8200 " pool to continue functioning.\n"));
8201 printf_color(ANSI_BOLD
, gettext("action: "));
8202 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8203 "and online it using 'zpool online'.\n"));
8206 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8207 printf_color(ANSI_BOLD
, gettext("status: "));
8208 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8209 "not be used because the label is missing or\n\tinvalid. "
8210 "Sufficient replicas exist for the pool to continue\n\t"
8211 "functioning in a degraded state.\n"));
8212 printf_color(ANSI_BOLD
, gettext("action: "));
8213 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8214 "'zpool replace'.\n"));
8217 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8218 printf_color(ANSI_BOLD
, gettext("status: "));
8219 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8220 "not be used because the label is missing \n\tor invalid. "
8221 "There are insufficient replicas for the pool to "
8222 "continue\n\tfunctioning.\n"));
8223 zpool_explain_recover(zpool_get_handle(zhp
),
8224 zpool_get_name(zhp
), reason
, config
);
8227 case ZPOOL_STATUS_FAILING_DEV
:
8228 printf_color(ANSI_BOLD
, gettext("status: "));
8229 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8230 "experienced an unrecoverable error. An\n\tattempt was "
8231 "made to correct the error. Applications are "
8233 printf_color(ANSI_BOLD
, gettext("action: "));
8234 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8235 "device needs to be replaced, and clear the errors\n\tusing"
8236 " 'zpool clear' or replace the device with 'zpool "
8240 case ZPOOL_STATUS_OFFLINE_DEV
:
8241 printf_color(ANSI_BOLD
, gettext("status: "));
8242 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8243 "been taken offline by the administrator.\n\tSufficient "
8244 "replicas exist for the pool to continue functioning in "
8245 "a\n\tdegraded state.\n"));
8246 printf_color(ANSI_BOLD
, gettext("action: "));
8247 printf_color(ANSI_YELLOW
, gettext("Online the device "
8248 "using 'zpool online' or replace the device with\n\t'zpool "
8252 case ZPOOL_STATUS_REMOVED_DEV
:
8253 printf_color(ANSI_BOLD
, gettext("status: "));
8254 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8255 "been removed by the administrator.\n\tSufficient "
8256 "replicas exist for the pool to continue functioning in "
8257 "a\n\tdegraded state.\n"));
8258 printf_color(ANSI_BOLD
, gettext("action: "));
8259 printf_color(ANSI_YELLOW
, gettext("Online the device "
8260 "using zpool online' or replace the device with\n\t'zpool "
8264 case ZPOOL_STATUS_RESILVERING
:
8265 case ZPOOL_STATUS_REBUILDING
:
8266 printf_color(ANSI_BOLD
, gettext("status: "));
8267 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8268 "currently being resilvered. The pool will\n\tcontinue "
8269 "to function, possibly in a degraded state.\n"));
8270 printf_color(ANSI_BOLD
, gettext("action: "));
8271 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8275 case ZPOOL_STATUS_REBUILD_SCRUB
:
8276 printf_color(ANSI_BOLD
, gettext("status: "));
8277 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8278 "been sequentially resilvered, scrubbing\n\tthe pool "
8279 "is recommended.\n"));
8280 printf_color(ANSI_BOLD
, gettext("action: "));
8281 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8282 "verify all data checksums.\n"));
8285 case ZPOOL_STATUS_CORRUPT_DATA
:
8286 printf_color(ANSI_BOLD
, gettext("status: "));
8287 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8288 "experienced an error resulting in data\n\tcorruption. "
8289 "Applications may be affected.\n"));
8290 printf_color(ANSI_BOLD
, gettext("action: "));
8291 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8292 " if possible. Otherwise restore the\n\tentire pool from "
8296 case ZPOOL_STATUS_CORRUPT_POOL
:
8297 printf_color(ANSI_BOLD
, gettext("status: "));
8298 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8299 "corrupted and the pool cannot be opened.\n"));
8300 zpool_explain_recover(zpool_get_handle(zhp
),
8301 zpool_get_name(zhp
), reason
, config
);
8304 case ZPOOL_STATUS_VERSION_OLDER
:
8305 printf_color(ANSI_BOLD
, gettext("status: "));
8306 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8307 "a legacy on-disk format. The pool can\n\tstill be used, "
8308 "but some features are unavailable.\n"));
8309 printf_color(ANSI_BOLD
, gettext("action: "));
8310 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8311 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8312 "longer be accessible on software that does not support\n\t"
8313 "feature flags.\n"));
8316 case ZPOOL_STATUS_VERSION_NEWER
:
8317 printf_color(ANSI_BOLD
, gettext("status: "));
8318 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8319 "to a newer, incompatible on-disk version.\n\tThe pool "
8320 "cannot be accessed on this system.\n"));
8321 printf_color(ANSI_BOLD
, gettext("action: "));
8322 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8323 "system running more recent software, or\n\trestore the "
8324 "pool from backup.\n"));
8327 case ZPOOL_STATUS_FEAT_DISABLED
:
8328 printf_color(ANSI_BOLD
, gettext("status: "));
8329 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8330 "requested features are not enabled on the pool.\n\t"
8331 "The pool can still be used, but some features are "
8333 printf_color(ANSI_BOLD
, gettext("action: "));
8334 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8335 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8336 "longer be accessible by software that does not support\n\t"
8337 "the features. See zpool-features(7) for details.\n"));
8340 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8341 printf_color(ANSI_BOLD
, gettext("status: "));
8342 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8343 "compatibility list specified, but it could not be\n\t"
8344 "read/parsed at this time. The pool can still be used, "
8345 "but this\n\tshould be investigated.\n"));
8346 printf_color(ANSI_BOLD
, gettext("action: "));
8347 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8348 "'compatibility' property against the\n\t"
8349 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8350 ZPOOL_DATA_COMPAT_D
".\n"));
8353 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
8354 printf_color(ANSI_BOLD
, gettext("status: "));
8355 printf_color(ANSI_YELLOW
, gettext("One or more features "
8356 "are enabled on the pool despite not being\n\t"
8357 "requested by the 'compatibility' property.\n"));
8358 printf_color(ANSI_BOLD
, gettext("action: "));
8359 printf_color(ANSI_YELLOW
, gettext("Consider setting "
8360 "'compatibility' to an appropriate value, or\n\t"
8361 "adding needed features to the relevant file in\n\t"
8362 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
8365 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8366 printf_color(ANSI_BOLD
, gettext("status: "));
8367 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8368 "on this system because it uses the\n\tfollowing feature(s)"
8369 " not supported on this system:\n"));
8370 zpool_print_unsup_feat(config
);
8371 (void) printf("\n");
8372 printf_color(ANSI_BOLD
, gettext("action: "));
8373 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8374 "system that supports the required feature(s),\n\tor "
8375 "restore the pool from backup.\n"));
8378 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8379 printf_color(ANSI_BOLD
, gettext("status: "));
8380 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8381 "accessed in read-only mode on this system. It\n\tcannot be"
8382 " accessed in read-write mode because it uses the "
8383 "following\n\tfeature(s) not supported on this system:\n"));
8384 zpool_print_unsup_feat(config
);
8385 (void) printf("\n");
8386 printf_color(ANSI_BOLD
, gettext("action: "));
8387 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8388 "in read-write mode. Import the pool with\n"
8389 "\t\"-o readonly=on\", access the pool from a system that "
8390 "supports the\n\trequired feature(s), or restore the "
8391 "pool from backup.\n"));
8394 case ZPOOL_STATUS_FAULTED_DEV_R
:
8395 printf_color(ANSI_BOLD
, gettext("status: "));
8396 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8397 "faulted in response to persistent errors.\n\tSufficient "
8398 "replicas exist for the pool to continue functioning "
8399 "in a\n\tdegraded state.\n"));
8400 printf_color(ANSI_BOLD
, gettext("action: "));
8401 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8402 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8405 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8406 printf_color(ANSI_BOLD
, gettext("status: "));
8407 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8408 "faulted in response to persistent errors. There are "
8409 "insufficient replicas for the pool to\n\tcontinue "
8411 printf_color(ANSI_BOLD
, gettext("action: "));
8412 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8413 "pool from a backup source. Manually marking the device\n"
8414 "\trepaired using 'zpool clear' may allow some data "
8415 "to be recovered.\n"));
8418 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8419 printf_color(ANSI_BOLD
, gettext("status: "));
8420 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8421 "because multihost writes failed or were delayed;\n\t"
8422 "another system could import the pool undetected.\n"));
8423 printf_color(ANSI_BOLD
, gettext("action: "));
8424 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8425 " are connected, then reboot your system and\n\timport the "
8429 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8430 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8431 printf_color(ANSI_BOLD
, gettext("status: "));
8432 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8433 "faulted in response to IO failures.\n"));
8434 printf_color(ANSI_BOLD
, gettext("action: "));
8435 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8436 "devices are connected, then run 'zpool clear'.\n"));
8439 case ZPOOL_STATUS_BAD_LOG
:
8440 printf_color(ANSI_BOLD
, gettext("status: "));
8441 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8442 "could not be read.\n"
8443 "\tWaiting for administrator intervention to fix the "
8444 "faulted pool.\n"));
8445 printf_color(ANSI_BOLD
, gettext("action: "));
8446 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8447 "device(s) and run 'zpool online',\n"
8448 "\tor ignore the intent log records by running "
8449 "'zpool clear'.\n"));
8452 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8453 (void) printf(gettext("status: One or more devices are "
8454 "configured to use a non-native block size.\n"
8455 "\tExpect reduced performance.\n"));
8456 (void) printf(gettext("action: Replace affected devices with "
8457 "devices that support the\n\tconfigured block size, or "
8458 "migrate data to a properly configured\n\tpool.\n"));
8461 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8462 printf_color(ANSI_BOLD
, gettext("status: "));
8463 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8464 " and system hostid on imported pool.\n\tThis pool was "
8465 "previously imported into a system with a different "
8466 "hostid,\n\tand then was verbatim imported into this "
8468 printf_color(ANSI_BOLD
, gettext("action: "));
8469 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8470 "systems on which it is imported.\n"
8471 "\tThen import it to correct the mismatch.\n"));
8474 case ZPOOL_STATUS_ERRATA
:
8475 printf_color(ANSI_BOLD
, gettext("status: "));
8476 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8480 case ZPOOL_ERRATA_NONE
:
8483 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8484 printf_color(ANSI_BOLD
, gettext("action: "));
8485 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8486 " run 'zpool scrub'.\n"));
8489 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8490 (void) printf(gettext("\tExisting encrypted datasets "
8491 "contain an on-disk incompatibility\n\twhich "
8492 "needs to be corrected.\n"));
8493 printf_color(ANSI_BOLD
, gettext("action: "));
8494 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8495 " backup existing encrypted datasets to new\n\t"
8496 "encrypted datasets and destroy the old ones. "
8497 "'zfs mount -o ro' can\n\tbe used to temporarily "
8498 "mount existing encrypted datasets readonly.\n"));
8501 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8502 (void) printf(gettext("\tExisting encrypted snapshots "
8503 "and bookmarks contain an on-disk\n\tincompat"
8504 "ibility. This may cause on-disk corruption if "
8505 "they are used\n\twith 'zfs recv'.\n"));
8506 printf_color(ANSI_BOLD
, gettext("action: "));
8507 printf_color(ANSI_YELLOW
, gettext("To correct the"
8508 "issue, enable the bookmark_v2 feature. No "
8509 "additional\n\taction is needed if there are no "
8510 "encrypted snapshots or bookmarks.\n\tIf preserving"
8511 "the encrypted snapshots and bookmarks is required,"
8512 " use\n\ta non-raw send to backup and restore them."
8513 " Alternately, they may be\n\tremoved to resolve "
8514 "the incompatibility.\n"));
8519 * All errata which allow the pool to be imported
8520 * must contain an action message.
8528 * The remaining errors can't actually be generated, yet.
8530 assert(reason
== ZPOOL_STATUS_OK
);
8533 if (msgid
!= NULL
) {
8535 printf_color(ANSI_BOLD
, gettext("see:"));
8537 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8541 if (config
!= NULL
) {
8543 nvlist_t
**spares
, **l2cache
;
8544 uint_t nspares
, nl2cache
;
8545 pool_checkpoint_stat_t
*pcs
= NULL
;
8546 pool_removal_stat_t
*prs
= NULL
;
8548 print_scan_status(zhp
, nvroot
);
8550 (void) nvlist_lookup_uint64_array(nvroot
,
8551 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8552 print_removal_status(zhp
, prs
);
8554 (void) nvlist_lookup_uint64_array(nvroot
,
8555 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8556 print_checkpoint_status(pcs
);
8558 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8559 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8560 if (cbp
->cb_namewidth
< 10)
8561 cbp
->cb_namewidth
= 10;
8563 color_start(ANSI_BOLD
);
8564 (void) printf(gettext("config:\n\n"));
8565 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8566 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8570 if (cbp
->cb_print_slow_ios
) {
8571 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8574 if (cbp
->vcdl
!= NULL
)
8575 print_cmd_columns(cbp
->vcdl
, 0);
8579 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8582 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8583 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8584 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8586 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8587 &l2cache
, &nl2cache
) == 0)
8588 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8590 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8591 &spares
, &nspares
) == 0)
8592 print_spares(zhp
, cbp
, spares
, nspares
);
8594 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8596 nvlist_t
*nverrlist
= NULL
;
8599 * If the approximate error count is small, get a
8600 * precise count by fetching the entire log and
8601 * uniquifying the results.
8603 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8604 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8609 while ((elem
= nvlist_next_nvpair(nverrlist
,
8614 nvlist_free(nverrlist
);
8616 (void) printf("\n");
8619 (void) printf(gettext("errors: No known data "
8621 else if (!cbp
->cb_verbose
)
8622 (void) printf(gettext("errors: %llu data "
8623 "errors, use '-v' for a list\n"),
8624 (u_longlong_t
)nerr
);
8626 print_error_log(zhp
);
8629 if (cbp
->cb_dedup_stats
)
8630 print_dedup_stats(config
);
8632 (void) printf(gettext("config: The configuration cannot be "
8640 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8641 * [interval [count]]
8643 * -c CMD For each vdev, run command CMD
8644 * -i Display vdev initialization status.
8645 * -g Display guid for individual vdev name.
8646 * -L Follow links when resolving vdev path name.
8647 * -p Display values in parsable (exact) format.
8648 * -P Display full path for vdev name.
8649 * -s Display slow IOs column.
8650 * -v Display complete error logs
8651 * -x Display only pools with potential problems
8652 * -D Display dedup status (undocumented)
8653 * -t Display vdev TRIM status.
8654 * -T Display a timestamp in date(1) or Unix format
8656 * Describes the health status of all pools or some subset.
8659 zpool_do_status(int argc
, char **argv
)
8664 unsigned long count
= 0;
8665 status_cbdata_t cb
= { 0 };
8669 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8674 gettext("Can't set -c flag twice\n"));
8678 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8679 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8680 fprintf(stderr
, gettext(
8681 "Can't run -c, disabled by "
8682 "ZPOOL_SCRIPTS_ENABLED.\n"));
8686 if ((getuid() <= 0 || geteuid() <= 0) &&
8687 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8688 fprintf(stderr
, gettext(
8689 "Can't run -c with root privileges "
8690 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8696 cb
.cb_print_vdev_init
= B_TRUE
;
8699 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8702 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8705 cb
.cb_literal
= B_TRUE
;
8708 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8711 cb
.cb_print_slow_ios
= B_TRUE
;
8714 cb
.cb_verbose
= B_TRUE
;
8717 cb
.cb_explain
= B_TRUE
;
8720 cb
.cb_dedup_stats
= B_TRUE
;
8723 cb
.cb_print_vdev_trim
= B_TRUE
;
8726 get_timestamp_arg(*optarg
);
8729 if (optopt
== 'c') {
8730 print_zpool_script_list("status");
8734 gettext("invalid option '%c'\n"), optopt
);
8743 get_interval_count(&argc
, argv
, &interval
, &count
);
8746 cb
.cb_allpools
= B_TRUE
;
8748 cb
.cb_first
= B_TRUE
;
8749 cb
.cb_print_status
= B_TRUE
;
8752 if (timestamp_fmt
!= NODATE
)
8753 print_timestamp(timestamp_fmt
);
8756 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8759 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8760 cb
.cb_literal
, status_callback
, &cb
);
8762 if (cb
.vcdl
!= NULL
)
8763 free_vdev_cmd_data_list(cb
.vcdl
);
8765 if (argc
== 0 && cb
.cb_count
== 0)
8766 (void) fprintf(stderr
, gettext("no pools available\n"));
8767 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8768 (void) printf(gettext("all pools are healthy\n"));
8776 if (count
!= 0 && --count
== 0)
8779 (void) fsleep(interval
);
8785 typedef struct upgrade_cbdata
{
8788 uint64_t cb_version
;
8793 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8795 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8796 int *count
= (int *)unsupp_fs
;
8798 if (zfs_version
> ZPL_VERSION
) {
8799 (void) printf(gettext("%s (v%d) is not supported by this "
8800 "implementation of ZFS.\n"),
8801 zfs_get_name(zhp
), zfs_version
);
8805 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8813 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8817 uint64_t oldversion
;
8820 config
= zpool_get_config(zhp
, NULL
);
8821 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8824 char compat
[ZFS_MAXPROPLEN
];
8825 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8826 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8829 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8830 assert(oldversion
< version
);
8832 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8837 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8838 "to %d unsupported filesystems (max v%d).\n"),
8839 unsupp_fs
, (int)ZPL_VERSION
);
8843 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
8844 (void) fprintf(stderr
, gettext("Upgrade not performed because "
8845 "'compatibility' property set to '"
8846 ZPOOL_COMPAT_LEGACY
"'.\n"));
8850 ret
= zpool_upgrade(zhp
, version
);
8854 if (version
>= SPA_VERSION_FEATURES
) {
8855 (void) printf(gettext("Successfully upgraded "
8856 "'%s' from version %llu to feature flags.\n"),
8857 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8859 (void) printf(gettext("Successfully upgraded "
8860 "'%s' from version %llu to version %llu.\n"),
8861 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8862 (u_longlong_t
)version
);
8869 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8872 boolean_t firstff
= B_TRUE
;
8873 nvlist_t
*enabled
= zpool_get_features(zhp
);
8875 char compat
[ZFS_MAXPROPLEN
];
8876 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8877 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8880 boolean_t requested_features
[SPA_FEATURES
];
8881 if (zpool_do_load_compat(compat
, requested_features
) !=
8882 ZPOOL_COMPATIBILITY_OK
)
8886 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8887 const char *fname
= spa_feature_table
[i
].fi_uname
;
8888 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8890 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8893 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
8895 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8896 ret
= zpool_set_prop(zhp
, propname
,
8897 ZFS_FEATURE_ENABLED
);
8905 (void) printf(gettext("Enabled the "
8906 "following features on '%s':\n"),
8907 zpool_get_name(zhp
));
8910 (void) printf(gettext(" %s\n"), fname
);
8921 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8923 upgrade_cbdata_t
*cbp
= arg
;
8926 boolean_t modified_pool
= B_FALSE
;
8929 config
= zpool_get_config(zhp
, NULL
);
8930 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8933 assert(SPA_VERSION_IS_SUPPORTED(version
));
8935 if (version
< cbp
->cb_version
) {
8936 cbp
->cb_first
= B_FALSE
;
8937 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8940 modified_pool
= B_TRUE
;
8943 * If they did "zpool upgrade -a", then we could
8944 * be doing ioctls to different pools. We need
8945 * to log this history once to each pool, and bypass
8946 * the normal history logging that happens in main().
8948 (void) zpool_log_history(g_zfs
, history_str
);
8949 log_history
= B_FALSE
;
8952 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8954 ret
= upgrade_enable_all(zhp
, &count
);
8959 cbp
->cb_first
= B_FALSE
;
8960 modified_pool
= B_TRUE
;
8964 if (modified_pool
) {
8965 (void) printf("\n");
8966 (void) after_zpool_upgrade(zhp
);
8973 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8975 upgrade_cbdata_t
*cbp
= arg
;
8979 config
= zpool_get_config(zhp
, NULL
);
8980 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8983 assert(SPA_VERSION_IS_SUPPORTED(version
));
8985 if (version
< SPA_VERSION_FEATURES
) {
8986 if (cbp
->cb_first
) {
8987 (void) printf(gettext("The following pools are "
8988 "formatted with legacy version numbers and can\n"
8989 "be upgraded to use feature flags. After "
8990 "being upgraded, these pools\nwill no "
8991 "longer be accessible by software that does not "
8992 "support feature\nflags.\n\n"
8993 "Note that setting a pool's 'compatibility' "
8994 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
8995 "inhibit upgrades.\n\n"));
8996 (void) printf(gettext("VER POOL\n"));
8997 (void) printf(gettext("--- ------------\n"));
8998 cbp
->cb_first
= B_FALSE
;
9001 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
9002 zpool_get_name(zhp
));
9009 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
9011 upgrade_cbdata_t
*cbp
= arg
;
9015 config
= zpool_get_config(zhp
, NULL
);
9016 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9019 if (version
>= SPA_VERSION_FEATURES
) {
9021 boolean_t poolfirst
= B_TRUE
;
9022 nvlist_t
*enabled
= zpool_get_features(zhp
);
9024 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9025 const char *fguid
= spa_feature_table
[i
].fi_guid
;
9026 const char *fname
= spa_feature_table
[i
].fi_uname
;
9028 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
9031 if (!nvlist_exists(enabled
, fguid
)) {
9032 if (cbp
->cb_first
) {
9033 (void) printf(gettext("\nSome "
9034 "supported features are not "
9035 "enabled on the following pools. "
9036 "Once a\nfeature is enabled the "
9037 "pool may become incompatible with "
9038 "software\nthat does not support "
9040 "zpool-features(7) for "
9042 "Note that the pool "
9043 "'compatibility' feature can be "
9044 "used to inhibit\nfeature "
9046 (void) printf(gettext("POOL "
9048 (void) printf(gettext("------"
9050 cbp
->cb_first
= B_FALSE
;
9054 (void) printf(gettext("%s\n"),
9055 zpool_get_name(zhp
));
9056 poolfirst
= B_FALSE
;
9059 (void) printf(gettext(" %s\n"), fname
);
9062 * If they did "zpool upgrade -a", then we could
9063 * be doing ioctls to different pools. We need
9064 * to log this history once to each pool, and bypass
9065 * the normal history logging that happens in main().
9067 (void) zpool_log_history(g_zfs
, history_str
);
9068 log_history
= B_FALSE
;
9076 upgrade_one(zpool_handle_t
*zhp
, void *data
)
9078 boolean_t modified_pool
= B_FALSE
;
9079 upgrade_cbdata_t
*cbp
= data
;
9080 uint64_t cur_version
;
9083 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
9084 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
9085 "Pool 'log' must be renamed using export and import"
9090 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
9091 if (cur_version
> cbp
->cb_version
) {
9092 (void) printf(gettext("Pool '%s' is already formatted "
9093 "using more current version '%llu'.\n\n"),
9094 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
9098 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
9099 (void) printf(gettext("Pool '%s' is already formatted "
9100 "using version %llu.\n\n"), zpool_get_name(zhp
),
9101 (u_longlong_t
)cbp
->cb_version
);
9105 if (cur_version
!= cbp
->cb_version
) {
9106 modified_pool
= B_TRUE
;
9107 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9112 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9114 ret
= upgrade_enable_all(zhp
, &count
);
9119 modified_pool
= B_TRUE
;
9120 } else if (cur_version
== SPA_VERSION
) {
9121 (void) printf(gettext("Pool '%s' already has all "
9122 "supported and requested features enabled.\n"),
9123 zpool_get_name(zhp
));
9127 if (modified_pool
) {
9128 (void) printf("\n");
9129 (void) after_zpool_upgrade(zhp
);
9138 * zpool upgrade [-V version] <-a | pool ...>
9140 * With no arguments, display downrev'd ZFS pool available for upgrade.
9141 * Individual pools can be upgraded by specifying the pool, and '-a' will
9142 * upgrade all pools.
9145 zpool_do_upgrade(int argc
, char **argv
)
9148 upgrade_cbdata_t cb
= { 0 };
9150 boolean_t showversions
= B_FALSE
;
9151 boolean_t upgradeall
= B_FALSE
;
9156 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
9159 upgradeall
= B_TRUE
;
9162 showversions
= B_TRUE
;
9165 cb
.cb_version
= strtoll(optarg
, &end
, 10);
9167 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
9168 (void) fprintf(stderr
,
9169 gettext("invalid version '%s'\n"), optarg
);
9174 (void) fprintf(stderr
, gettext("missing argument for "
9175 "'%c' option\n"), optopt
);
9179 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9190 if (cb
.cb_version
== 0) {
9191 cb
.cb_version
= SPA_VERSION
;
9192 } else if (!upgradeall
&& argc
== 0) {
9193 (void) fprintf(stderr
, gettext("-V option is "
9194 "incompatible with other arguments\n"));
9199 if (upgradeall
|| argc
!= 0) {
9200 (void) fprintf(stderr
, gettext("-v option is "
9201 "incompatible with other arguments\n"));
9204 } else if (upgradeall
) {
9206 (void) fprintf(stderr
, gettext("-a option should not "
9207 "be used along with a pool name\n"));
9212 (void) printf("%s", gettext("This system supports ZFS pool feature "
9217 (void) printf(gettext("The following features are "
9219 (void) printf(gettext("FEAT DESCRIPTION\n"));
9220 (void) printf("----------------------------------------------"
9221 "---------------\n");
9222 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9223 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9224 if (!fi
->fi_zfs_mod_supported
)
9227 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9228 " (read-only compatible)" : "";
9230 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9231 (void) printf(" %s\n", fi
->fi_desc
);
9233 (void) printf("\n");
9235 (void) printf(gettext("The following legacy versions are also "
9237 (void) printf(gettext("VER DESCRIPTION\n"));
9238 (void) printf("--- -----------------------------------------"
9239 "---------------\n");
9240 (void) printf(gettext(" 1 Initial ZFS version\n"));
9241 (void) printf(gettext(" 2 Ditto blocks "
9242 "(replicated metadata)\n"));
9243 (void) printf(gettext(" 3 Hot spares and double parity "
9245 (void) printf(gettext(" 4 zpool history\n"));
9246 (void) printf(gettext(" 5 Compression using the gzip "
9248 (void) printf(gettext(" 6 bootfs pool property\n"));
9249 (void) printf(gettext(" 7 Separate intent log devices\n"));
9250 (void) printf(gettext(" 8 Delegated administration\n"));
9251 (void) printf(gettext(" 9 refquota and refreservation "
9253 (void) printf(gettext(" 10 Cache devices\n"));
9254 (void) printf(gettext(" 11 Improved scrub performance\n"));
9255 (void) printf(gettext(" 12 Snapshot properties\n"));
9256 (void) printf(gettext(" 13 snapused property\n"));
9257 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9258 (void) printf(gettext(" 15 user/group space accounting\n"));
9259 (void) printf(gettext(" 16 stmf property support\n"));
9260 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9261 (void) printf(gettext(" 18 Snapshot user holds\n"));
9262 (void) printf(gettext(" 19 Log device removal\n"));
9263 (void) printf(gettext(" 20 Compression using zle "
9264 "(zero-length encoding)\n"));
9265 (void) printf(gettext(" 21 Deduplication\n"));
9266 (void) printf(gettext(" 22 Received properties\n"));
9267 (void) printf(gettext(" 23 Slim ZIL\n"));
9268 (void) printf(gettext(" 24 System attributes\n"));
9269 (void) printf(gettext(" 25 Improved scrub stats\n"));
9270 (void) printf(gettext(" 26 Improved snapshot deletion "
9272 (void) printf(gettext(" 27 Improved snapshot creation "
9274 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9275 (void) printf(gettext("\nFor more information on a particular "
9276 "version, including supported releases,\n"));
9277 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9278 } else if (argc
== 0 && upgradeall
) {
9279 cb
.cb_first
= B_TRUE
;
9280 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9281 if (ret
== 0 && cb
.cb_first
) {
9282 if (cb
.cb_version
== SPA_VERSION
) {
9283 (void) printf(gettext("All pools are already "
9284 "formatted using feature flags.\n\n"));
9285 (void) printf(gettext("Every feature flags "
9286 "pool already has all supported and "
9287 "requested features enabled.\n"));
9289 (void) printf(gettext("All pools are already "
9290 "formatted with version %llu or higher.\n"),
9291 (u_longlong_t
)cb
.cb_version
);
9294 } else if (argc
== 0) {
9295 cb
.cb_first
= B_TRUE
;
9296 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9300 (void) printf(gettext("All pools are formatted "
9301 "using feature flags.\n\n"));
9303 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9304 "for a list of available legacy versions.\n"));
9307 cb
.cb_first
= B_TRUE
;
9308 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9312 (void) printf(gettext("Every feature flags pool has "
9313 "all supported and requested features enabled.\n"));
9315 (void) printf(gettext("\n"));
9318 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9319 B_FALSE
, upgrade_one
, &cb
);
9325 typedef struct hist_cbdata
{
9332 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9338 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9339 &records
, &numrecords
) == 0);
9340 for (i
= 0; i
< numrecords
; i
++) {
9341 nvlist_t
*rec
= records
[i
];
9344 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9348 tsec
= fnvlist_lookup_uint64(records
[i
],
9350 (void) localtime_r(&tsec
, &t
);
9351 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9354 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9355 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9356 ZPOOL_HIST_ELAPSED_NS
);
9357 (void) snprintf(tbuf
+ strlen(tbuf
),
9358 sizeof (tbuf
) - strlen(tbuf
),
9359 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9362 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9363 (void) printf("%s %s", tbuf
,
9364 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9365 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9367 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9370 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9371 (void) printf("%s unrecognized record:\n",
9373 dump_nvlist(rec
, 4);
9376 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9377 zfs_history_event_names
[ievent
],
9378 (longlong_t
)fnvlist_lookup_uint64(
9379 rec
, ZPOOL_HIST_TXG
),
9380 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9381 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9384 (void) printf("%s [txg:%lld] %s", tbuf
,
9385 (longlong_t
)fnvlist_lookup_uint64(
9386 rec
, ZPOOL_HIST_TXG
),
9387 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9388 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9389 (void) printf(" %s (%llu)",
9390 fnvlist_lookup_string(rec
,
9392 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9395 (void) printf(" %s", fnvlist_lookup_string(rec
,
9396 ZPOOL_HIST_INT_STR
));
9397 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9400 (void) printf("%s ioctl %s\n", tbuf
,
9401 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9402 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9403 (void) printf(" input:\n");
9404 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9405 ZPOOL_HIST_INPUT_NVL
), 8);
9407 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9408 (void) printf(" output:\n");
9409 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9410 ZPOOL_HIST_OUTPUT_NVL
), 8);
9412 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9413 (void) printf(" output nvlist omitted; "
9414 "original size: %lldKB\n",
9415 (longlong_t
)fnvlist_lookup_int64(rec
,
9416 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9418 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9419 (void) printf(" errno: %lld\n",
9420 (longlong_t
)fnvlist_lookup_int64(rec
,
9426 (void) printf("%s unrecognized record:\n", tbuf
);
9427 dump_nvlist(rec
, 4);
9431 (void) printf("\n");
9434 (void) printf(" [");
9435 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9436 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9437 struct passwd
*pwd
= getpwuid(who
);
9438 (void) printf("user %d ", (int)who
);
9440 (void) printf("(%s) ", pwd
->pw_name
);
9442 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9443 (void) printf("on %s",
9444 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9446 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9447 (void) printf(":%s",
9448 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9452 (void) printf("\n");
9457 * Print out the command history for a specific pool.
9460 get_history_one(zpool_handle_t
*zhp
, void *data
)
9464 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9466 boolean_t eof
= B_FALSE
;
9468 cb
->first
= B_FALSE
;
9470 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9473 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9476 print_history_records(nvhis
, cb
);
9479 (void) printf("\n");
9485 * zpool history <pool>
9487 * Displays the history of commands that modified pools.
9490 zpool_do_history(int argc
, char **argv
)
9492 hist_cbdata_t cbdata
= { 0 };
9496 cbdata
.first
= B_TRUE
;
9498 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9501 cbdata
.longfmt
= B_TRUE
;
9504 cbdata
.internal
= B_TRUE
;
9507 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9515 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9516 B_FALSE
, get_history_one
, &cbdata
);
9518 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9519 (void) fprintf(stderr
, gettext("no pools available\n"));
9526 typedef struct ev_opts
{
9531 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9535 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9537 char ctime_str
[26], str
[32], *ptr
;
9541 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9542 memset(str
, ' ', 32);
9543 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9544 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9545 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9546 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9547 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9549 (void) printf(gettext("%s\t"), str
);
9551 (void) printf(gettext("%s "), str
);
9553 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9554 (void) printf(gettext("%s\n"), ptr
);
9558 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9562 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9563 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9565 data_type_t type
= nvpair_type(nvp
);
9566 const char *name
= nvpair_name(nvp
);
9576 printf(gettext("%*s%s = "), depth
, "", name
);
9579 case DATA_TYPE_BOOLEAN
:
9580 printf(gettext("%s"), "1");
9583 case DATA_TYPE_BOOLEAN_VALUE
:
9584 (void) nvpair_value_boolean_value(nvp
, &b
);
9585 printf(gettext("%s"), b
? "1" : "0");
9588 case DATA_TYPE_BYTE
:
9589 (void) nvpair_value_byte(nvp
, &i8
);
9590 printf(gettext("0x%x"), i8
);
9593 case DATA_TYPE_INT8
:
9594 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9595 printf(gettext("0x%x"), i8
);
9598 case DATA_TYPE_UINT8
:
9599 (void) nvpair_value_uint8(nvp
, &i8
);
9600 printf(gettext("0x%x"), i8
);
9603 case DATA_TYPE_INT16
:
9604 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9605 printf(gettext("0x%x"), i16
);
9608 case DATA_TYPE_UINT16
:
9609 (void) nvpair_value_uint16(nvp
, &i16
);
9610 printf(gettext("0x%x"), i16
);
9613 case DATA_TYPE_INT32
:
9614 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9615 printf(gettext("0x%x"), i32
);
9618 case DATA_TYPE_UINT32
:
9619 (void) nvpair_value_uint32(nvp
, &i32
);
9620 printf(gettext("0x%x"), i32
);
9623 case DATA_TYPE_INT64
:
9624 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9625 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9628 case DATA_TYPE_UINT64
:
9629 (void) nvpair_value_uint64(nvp
, &i64
);
9631 * translate vdev state values to readable
9632 * strings to aide zpool events consumers
9635 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9637 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9638 printf(gettext("\"%s\" (0x%llx)"),
9639 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9642 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9646 case DATA_TYPE_HRTIME
:
9647 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9648 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9651 case DATA_TYPE_STRING
:
9652 (void) nvpair_value_string(nvp
, &str
);
9653 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9656 case DATA_TYPE_NVLIST
:
9657 printf(gettext("(embedded nvlist)\n"));
9658 (void) nvpair_value_nvlist(nvp
, &cnv
);
9659 zpool_do_events_nvprint(cnv
, depth
+ 8);
9660 printf(gettext("%*s(end %s)"), depth
, "", name
);
9663 case DATA_TYPE_NVLIST_ARRAY
: {
9667 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9668 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9669 for (i
= 0; i
< nelem
; i
++) {
9670 printf(gettext("%*s%s[%d] = %s\n"),
9671 depth
, "", name
, i
, "(embedded nvlist)");
9672 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9673 printf(gettext("%*s(end %s[%i])\n"),
9674 depth
, "", name
, i
);
9676 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9680 case DATA_TYPE_INT8_ARRAY
: {
9684 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9685 for (i
= 0; i
< nelem
; i
++)
9686 printf(gettext("0x%x "), val
[i
]);
9691 case DATA_TYPE_UINT8_ARRAY
: {
9695 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9696 for (i
= 0; i
< nelem
; i
++)
9697 printf(gettext("0x%x "), val
[i
]);
9702 case DATA_TYPE_INT16_ARRAY
: {
9706 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9707 for (i
= 0; i
< nelem
; i
++)
9708 printf(gettext("0x%x "), val
[i
]);
9713 case DATA_TYPE_UINT16_ARRAY
: {
9717 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9718 for (i
= 0; i
< nelem
; i
++)
9719 printf(gettext("0x%x "), val
[i
]);
9724 case DATA_TYPE_INT32_ARRAY
: {
9728 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9729 for (i
= 0; i
< nelem
; i
++)
9730 printf(gettext("0x%x "), val
[i
]);
9735 case DATA_TYPE_UINT32_ARRAY
: {
9739 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9740 for (i
= 0; i
< nelem
; i
++)
9741 printf(gettext("0x%x "), val
[i
]);
9746 case DATA_TYPE_INT64_ARRAY
: {
9750 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9751 for (i
= 0; i
< nelem
; i
++)
9752 printf(gettext("0x%llx "),
9753 (u_longlong_t
)val
[i
]);
9758 case DATA_TYPE_UINT64_ARRAY
: {
9762 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9763 for (i
= 0; i
< nelem
; i
++)
9764 printf(gettext("0x%llx "),
9765 (u_longlong_t
)val
[i
]);
9770 case DATA_TYPE_STRING_ARRAY
: {
9774 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9775 for (i
= 0; i
< nelem
; i
++)
9776 printf(gettext("\"%s\" "),
9777 str
[i
] ? str
[i
] : "<NULL>");
9782 case DATA_TYPE_BOOLEAN_ARRAY
:
9783 case DATA_TYPE_BYTE_ARRAY
:
9784 case DATA_TYPE_DOUBLE
:
9785 case DATA_TYPE_DONTCARE
:
9786 case DATA_TYPE_UNKNOWN
:
9787 printf(gettext("<unknown>"));
9791 printf(gettext("\n"));
9796 zpool_do_events_next(ev_opts_t
*opts
)
9799 int zevent_fd
, ret
, dropped
;
9802 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9803 VERIFY(zevent_fd
>= 0);
9805 if (!opts
->scripted
)
9806 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9809 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9810 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9811 if (ret
|| nvl
== NULL
)
9815 (void) printf(gettext("dropped %d events\n"), dropped
);
9817 if (strlen(opts
->poolname
) > 0 &&
9818 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9819 strcmp(opts
->poolname
, pool
) != 0)
9822 zpool_do_events_short(nvl
, opts
);
9824 if (opts
->verbose
) {
9825 zpool_do_events_nvprint(nvl
, 8);
9826 printf(gettext("\n"));
9828 (void) fflush(stdout
);
9833 VERIFY(0 == close(zevent_fd
));
9839 zpool_do_events_clear(void)
9843 ret
= zpool_events_clear(g_zfs
, &count
);
9845 (void) printf(gettext("cleared %d events\n"), count
);
9851 * zpool events [-vHf [pool] | -c]
9853 * Displays events logs by ZFS.
9856 zpool_do_events(int argc
, char **argv
)
9858 ev_opts_t opts
= { 0 };
9863 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9878 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9887 (void) fprintf(stderr
, gettext("too many arguments\n"));
9889 } else if (argc
== 1) {
9890 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9891 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9892 (void) fprintf(stderr
,
9893 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9898 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9900 (void) fprintf(stderr
,
9901 gettext("invalid options combined with -c\n"));
9906 ret
= zpool_do_events_clear();
9908 ret
= zpool_do_events_next(&opts
);
9914 get_callback_vdev(zpool_handle_t
*zhp
, char *vdevname
, void *data
)
9916 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9917 char value
[ZFS_MAXPROPLEN
];
9918 zprop_source_t srctype
;
9920 for (zprop_list_t
*pl
= cbp
->cb_proplist
; pl
!= NULL
;
9924 * If the first property is pool name, it is a special
9925 * placeholder that we can skip. This will also skip
9926 * over the name property when 'all' is specified.
9928 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9929 pl
== cbp
->cb_proplist
)
9932 if (pl
->pl_prop
== ZPROP_INVAL
) {
9933 prop_name
= pl
->pl_user_prop
;
9935 prop_name
= (char *)vdev_prop_to_name(pl
->pl_prop
);
9937 if (zpool_get_vdev_prop(zhp
, vdevname
, pl
->pl_prop
,
9938 prop_name
, value
, sizeof (value
), &srctype
,
9939 cbp
->cb_literal
) == 0) {
9940 zprop_print_one_property(vdevname
, cbp
, prop_name
,
9941 value
, srctype
, NULL
, NULL
);
9949 get_callback_vdev_width_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9951 zpool_handle_t
*zhp
= zhp_data
;
9952 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9953 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9954 cbp
->cb_vdevs
.cb_name_flags
);
9957 /* Adjust the column widths for the vdev properties */
9958 ret
= vdev_expand_proplist(zhp
, vdevname
, &cbp
->cb_proplist
);
9964 get_callback_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9966 zpool_handle_t
*zhp
= zhp_data
;
9967 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9968 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9969 cbp
->cb_vdevs
.cb_name_flags
);
9972 /* Display the properties */
9973 ret
= get_callback_vdev(zhp
, vdevname
, data
);
9979 get_callback(zpool_handle_t
*zhp
, void *data
)
9981 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9982 char value
[MAXNAMELEN
];
9983 zprop_source_t srctype
;
9987 if (cbp
->cb_type
== ZFS_TYPE_VDEV
) {
9988 if (strcmp(cbp
->cb_vdevs
.cb_names
[0], "all-vdevs") == 0) {
9989 for_each_vdev(zhp
, get_callback_vdev_width_cb
, data
);
9990 for_each_vdev(zhp
, get_callback_vdev_cb
, data
);
9992 /* Adjust column widths for vdev properties */
9993 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
9995 vdev_expand_proplist(zhp
,
9996 cbp
->cb_vdevs
.cb_names
[vid
],
9999 /* Display the properties */
10000 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10002 get_callback_vdev(zhp
,
10003 cbp
->cb_vdevs
.cb_names
[vid
], data
);
10007 assert(cbp
->cb_type
== ZFS_TYPE_POOL
);
10008 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
10010 * Skip the special fake placeholder. This will also
10011 * skip over the name property when 'all' is specified.
10013 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
10014 pl
== cbp
->cb_proplist
)
10017 if (pl
->pl_prop
== ZPROP_INVAL
&&
10018 (zpool_prop_feature(pl
->pl_user_prop
) ||
10019 zpool_prop_unsupported(pl
->pl_user_prop
))) {
10020 srctype
= ZPROP_SRC_LOCAL
;
10022 if (zpool_prop_get_feature(zhp
,
10023 pl
->pl_user_prop
, value
,
10024 sizeof (value
)) == 0) {
10025 zprop_print_one_property(
10026 zpool_get_name(zhp
), cbp
,
10027 pl
->pl_user_prop
, value
, srctype
,
10031 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
10032 sizeof (value
), &srctype
,
10033 cbp
->cb_literal
) != 0)
10036 zprop_print_one_property(zpool_get_name(zhp
),
10037 cbp
, zpool_prop_to_name(pl
->pl_prop
),
10038 value
, srctype
, NULL
, NULL
);
10047 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10049 * -H Scripted mode. Don't display headers, and separate properties
10051 * -o List of columns to display. Defaults to
10052 * "name,property,value,source".
10053 * -p Display values in parsable (exact) format.
10055 * Get properties of pools in the system. Output space statistics
10056 * for each one as well as other attributes.
10059 zpool_do_get(int argc
, char **argv
)
10061 zprop_get_cbdata_t cb
= { 0 };
10062 zprop_list_t fake_name
= { 0 };
10065 char *propstr
= NULL
;
10067 cb
.cb_first
= B_TRUE
;
10070 * Set up default columns and sources.
10072 cb
.cb_sources
= ZPROP_SRC_ALL
;
10073 cb
.cb_columns
[0] = GET_COL_NAME
;
10074 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
10075 cb
.cb_columns
[2] = GET_COL_VALUE
;
10076 cb
.cb_columns
[3] = GET_COL_SOURCE
;
10077 cb
.cb_type
= ZFS_TYPE_POOL
;
10078 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10079 current_prop_type
= cb
.cb_type
;
10081 /* check options */
10082 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
10085 cb
.cb_literal
= B_TRUE
;
10088 cb
.cb_scripted
= B_TRUE
;
10091 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
10094 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10095 static const char *const col_opts
[] =
10096 { "name", "property", "value", "source",
10098 static const zfs_get_column_t col_cols
[] =
10099 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
10102 if (i
== ZFS_GET_NCOLS
- 1) {
10103 (void) fprintf(stderr
, gettext("too "
10104 "many fields given to -o "
10109 for (c
= 0; c
< ARRAY_SIZE(col_opts
); ++c
)
10110 if (strcmp(tok
, col_opts
[c
]) == 0)
10113 (void) fprintf(stderr
,
10114 gettext("invalid column name '%s'\n"), tok
);
10120 (void) fprintf(stderr
,
10121 gettext("\"all\" conflicts "
10122 "with specific fields "
10123 "given to -o option\n"));
10127 memcpy(cb
.cb_columns
, col_cols
,
10128 sizeof (col_cols
));
10129 i
= ZFS_GET_NCOLS
- 1;
10131 cb
.cb_columns
[i
++] = col_cols
[c
];
10135 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10145 (void) fprintf(stderr
, gettext("missing property "
10150 /* Properties list is needed later by zprop_get_list() */
10157 /* No args, so just print the defaults. */
10158 } else if (are_all_pools(argc
, argv
)) {
10159 /* All the args are pool names */
10160 } else if (are_all_pools(1, argv
)) {
10161 /* The first arg is a pool name */
10162 if ((argc
== 2 && strcmp(argv
[1], "all-vdevs") == 0) ||
10163 are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10165 /* ... and the rest are vdev names */
10166 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10167 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
10168 cb
.cb_type
= ZFS_TYPE_VDEV
;
10169 argc
= 1; /* One pool to process */
10171 fprintf(stderr
, gettext("Expected a list of vdevs in"
10172 " \"%s\", but got:\n"), argv
[0]);
10173 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
10174 argv
[0], &cb
.cb_vdevs
);
10175 fprintf(stderr
, "\n");
10181 * The first arg isn't a pool name,
10183 fprintf(stderr
, gettext("missing pool name.\n"));
10184 fprintf(stderr
, "\n");
10189 if (zprop_get_list(g_zfs
, propstr
, &cb
.cb_proplist
,
10190 cb
.cb_type
) != 0) {
10191 /* Use correct list of valid properties (pool or vdev) */
10192 current_prop_type
= cb
.cb_type
;
10196 if (cb
.cb_proplist
!= NULL
) {
10197 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
10198 fake_name
.pl_width
= strlen(gettext("NAME"));
10199 fake_name
.pl_next
= cb
.cb_proplist
;
10200 cb
.cb_proplist
= &fake_name
;
10203 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_type
,
10204 cb
.cb_literal
, get_callback
, &cb
);
10206 if (cb
.cb_proplist
== &fake_name
)
10207 zprop_free_list(fake_name
.pl_next
);
10209 zprop_free_list(cb
.cb_proplist
);
10214 typedef struct set_cbdata
{
10217 zfs_type_t cb_type
;
10218 vdev_cbdata_t cb_vdevs
;
10219 boolean_t cb_any_successful
;
10223 set_pool_callback(zpool_handle_t
*zhp
, set_cbdata_t
*cb
)
10227 /* Check if we have out-of-bounds features */
10228 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
10229 boolean_t features
[SPA_FEATURES
];
10230 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
10231 ZPOOL_COMPATIBILITY_OK
)
10234 nvlist_t
*enabled
= zpool_get_features(zhp
);
10236 for (i
= 0; i
< SPA_FEATURES
; i
++) {
10237 const char *fguid
= spa_feature_table
[i
].fi_guid
;
10238 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
10241 if (i
< SPA_FEATURES
)
10242 (void) fprintf(stderr
, gettext("Warning: one or "
10243 "more features already enabled on pool '%s'\n"
10244 "are not present in this compatibility set.\n"),
10245 zpool_get_name(zhp
));
10248 /* if we're setting a feature, check it's in compatibility set */
10249 if (zpool_prop_feature(cb
->cb_propname
) &&
10250 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
10251 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
10254 if (zfeature_lookup_name(fname
, &f
) == 0) {
10255 char compat
[ZFS_MAXPROPLEN
];
10256 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
10257 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
10260 boolean_t features
[SPA_FEATURES
];
10261 if (zpool_do_load_compat(compat
, features
) !=
10262 ZPOOL_COMPATIBILITY_OK
) {
10263 (void) fprintf(stderr
, gettext("Error: "
10264 "cannot enable feature '%s' on pool '%s'\n"
10265 "because the pool's 'compatibility' "
10266 "property cannot be parsed.\n"),
10267 fname
, zpool_get_name(zhp
));
10271 if (!features
[f
]) {
10272 (void) fprintf(stderr
, gettext("Error: "
10273 "cannot enable feature '%s' on pool '%s'\n"
10274 "as it is not specified in this pool's "
10275 "current compatibility set.\n"
10276 "Consider setting 'compatibility' to a "
10277 "less restrictive set, or to 'off'.\n"),
10278 fname
, zpool_get_name(zhp
));
10284 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
10290 set_callback(zpool_handle_t
*zhp
, void *data
)
10293 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
10295 if (cb
->cb_type
== ZFS_TYPE_VDEV
) {
10296 error
= zpool_set_vdev_prop(zhp
, *cb
->cb_vdevs
.cb_names
,
10297 cb
->cb_propname
, cb
->cb_value
);
10299 assert(cb
->cb_type
== ZFS_TYPE_POOL
);
10300 error
= set_pool_callback(zhp
, cb
);
10303 cb
->cb_any_successful
= !error
;
10308 zpool_do_set(int argc
, char **argv
)
10310 set_cbdata_t cb
= { 0 };
10313 current_prop_type
= ZFS_TYPE_POOL
;
10314 if (argc
> 1 && argv
[1][0] == '-') {
10315 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10321 (void) fprintf(stderr
, gettext("missing property=value "
10327 (void) fprintf(stderr
, gettext("missing pool name\n"));
10332 (void) fprintf(stderr
, gettext("too many pool names\n"));
10336 cb
.cb_propname
= argv
[1];
10337 cb
.cb_type
= ZFS_TYPE_POOL
;
10338 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10339 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
10340 if (cb
.cb_value
== NULL
) {
10341 (void) fprintf(stderr
, gettext("missing value in "
10342 "property=value argument\n"));
10346 *(cb
.cb_value
) = '\0';
10351 if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
10352 /* Argument is a vdev */
10353 cb
.cb_vdevs
.cb_names
= argv
;
10354 cb
.cb_vdevs
.cb_names_count
= 1;
10355 cb
.cb_type
= ZFS_TYPE_VDEV
;
10356 argc
= 0; /* No pools to process */
10357 } else if (are_all_pools(1, argv
)) {
10358 /* The first arg is a pool name */
10359 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10361 /* 2nd argument is a vdev */
10362 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10363 cb
.cb_vdevs
.cb_names_count
= 1;
10364 cb
.cb_type
= ZFS_TYPE_VDEV
;
10365 argc
= 1; /* One pool to process */
10366 } else if (argc
> 1) {
10367 (void) fprintf(stderr
,
10368 gettext("too many pool names\n"));
10373 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
10374 B_FALSE
, set_callback
, &cb
);
10379 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10381 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
10383 uint64_t bytes_remaining
;
10385 uint_t c
, children
;
10388 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
10389 activity
== ZPOOL_WAIT_TRIM
);
10391 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
10392 (uint64_t **)&vs
, &c
) == 0);
10394 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
10395 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
10396 bytes_remaining
= vs
->vs_initialize_bytes_est
-
10397 vs
->vs_initialize_bytes_done
;
10398 else if (activity
== ZPOOL_WAIT_TRIM
&&
10399 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
10400 bytes_remaining
= vs
->vs_trim_bytes_est
-
10401 vs
->vs_trim_bytes_done
;
10403 bytes_remaining
= 0;
10405 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10406 &child
, &children
) != 0)
10409 for (c
= 0; c
< children
; c
++)
10410 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
10412 return (bytes_remaining
);
10415 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10417 vdev_activity_top_remaining(nvlist_t
*nv
)
10419 uint64_t bytes_remaining
= 0;
10424 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10425 &child
, &children
) != 0)
10428 for (uint_t c
= 0; c
< children
; c
++) {
10429 vdev_rebuild_stat_t
*vrs
;
10432 error
= nvlist_lookup_uint64_array(child
[c
],
10433 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
10435 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
10436 bytes_remaining
+= (vrs
->vrs_bytes_est
-
10437 vrs
->vrs_bytes_rebuilt
);
10442 return (bytes_remaining
);
10445 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10447 vdev_any_spare_replacing(nvlist_t
*nv
)
10450 uint_t c
, children
;
10453 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
10455 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
10456 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
10457 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
10461 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10462 &child
, &children
) != 0)
10465 for (c
= 0; c
< children
; c
++) {
10466 if (vdev_any_spare_replacing(child
[c
]))
10473 typedef struct wait_data
{
10475 boolean_t wd_scripted
;
10476 boolean_t wd_exact
;
10477 boolean_t wd_headers_once
;
10478 boolean_t wd_should_exit
;
10479 /* Which activities to wait for */
10480 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10482 pthread_cond_t wd_cv
;
10483 pthread_mutex_t wd_mutex
;
10487 * Print to stdout a single line, containing one column for each activity that
10488 * we are waiting for specifying how many bytes of work are left for that
10492 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10494 nvlist_t
*config
, *nvroot
;
10497 pool_checkpoint_stat_t
*pcs
= NULL
;
10498 pool_scan_stat_t
*pss
= NULL
;
10499 pool_removal_stat_t
*prs
= NULL
;
10500 const char *const headers
[] = {"DISCARD", "FREE", "INITIALIZE",
10501 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10502 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10504 /* Calculate the width of each column */
10505 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10507 * Make sure we have enough space in the col for pretty-printed
10508 * numbers and for the column header, and then leave a couple
10509 * spaces between cols for readability.
10511 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10514 /* Print header if appropriate */
10515 int term_height
= terminal_height();
10516 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10517 row
% (term_height
-1) == 0);
10518 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10519 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10520 if (wd
->wd_enabled
[i
])
10521 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10523 (void) fputc('\n', stdout
);
10526 /* Bytes of work remaining in each activity */
10527 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10529 bytes_rem
[ZPOOL_WAIT_FREE
] =
10530 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10532 config
= zpool_get_config(zhp
, NULL
);
10533 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10535 (void) nvlist_lookup_uint64_array(nvroot
,
10536 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10537 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10538 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10540 (void) nvlist_lookup_uint64_array(nvroot
,
10541 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10542 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10543 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10546 (void) nvlist_lookup_uint64_array(nvroot
,
10547 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10548 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10549 pss
->pss_pass_scrub_pause
== 0) {
10550 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10551 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10552 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10554 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10555 } else if (check_rebuilding(nvroot
, NULL
)) {
10556 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10557 vdev_activity_top_remaining(nvroot
);
10560 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10561 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10562 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10563 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10566 * A replace finishes after resilvering finishes, so the amount of work
10567 * left for a replace is the same as for resilvering.
10569 * It isn't quite correct to say that if we have any 'spare' or
10570 * 'replacing' vdevs and a resilver is happening, then a replace is in
10571 * progress, like we do here. When a hot spare is used, the faulted vdev
10572 * is not removed after the hot spare is resilvered, so parent 'spare'
10573 * vdev is not removed either. So we could have a 'spare' vdev, but be
10574 * resilvering for a different reason. However, we use it as a heuristic
10575 * because we don't have access to the DTLs, which could tell us whether
10576 * or not we have really finished resilvering a hot spare.
10578 if (vdev_any_spare_replacing(nvroot
))
10579 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10581 if (timestamp_fmt
!= NODATE
)
10582 print_timestamp(timestamp_fmt
);
10584 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10586 if (!wd
->wd_enabled
[i
])
10590 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10593 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10595 if (wd
->wd_scripted
)
10596 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10598 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10600 (void) printf("\n");
10601 (void) fflush(stdout
);
10605 wait_status_thread(void *arg
)
10607 wait_data_t
*wd
= (wait_data_t
*)arg
;
10608 zpool_handle_t
*zhp
;
10610 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10611 return (void *)(1);
10613 for (int row
= 0; ; row
++) {
10615 struct timespec timeout
;
10617 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10619 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10620 zpool_props_refresh(zhp
) != 0) {
10622 return (void *)(uintptr_t)(missing
? 0 : 1);
10625 print_wait_status_row(wd
, zhp
, row
);
10627 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10628 long nanos
= timeout
.tv_nsec
+
10629 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10630 if (nanos
>= NANOSEC
) {
10632 timeout
.tv_nsec
= nanos
- NANOSEC
;
10634 timeout
.tv_nsec
= nanos
;
10636 pthread_mutex_lock(&wd
->wd_mutex
);
10637 if (!wd
->wd_should_exit
)
10638 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10640 pthread_mutex_unlock(&wd
->wd_mutex
);
10642 break; /* signaled by main thread */
10643 } else if (ret
!= ETIMEDOUT
) {
10644 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10645 "failed: %s\n"), strerror(ret
));
10647 return (void *)(uintptr_t)(1);
10652 return (void *)(0);
10656 zpool_do_wait(int argc
, char **argv
)
10658 boolean_t verbose
= B_FALSE
;
10660 unsigned long count
;
10661 pthread_t status_thr
;
10663 zpool_handle_t
*zhp
;
10666 wd
.wd_scripted
= B_FALSE
;
10667 wd
.wd_exact
= B_FALSE
;
10668 wd
.wd_headers_once
= B_FALSE
;
10669 wd
.wd_should_exit
= B_FALSE
;
10671 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10672 pthread_cond_init(&wd
.wd_cv
, NULL
);
10674 /* By default, wait for all types of activity. */
10675 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10676 wd
.wd_enabled
[i
] = B_TRUE
;
10678 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10681 wd
.wd_scripted
= B_TRUE
;
10684 wd
.wd_headers_once
= B_TRUE
;
10687 wd
.wd_exact
= B_TRUE
;
10690 get_timestamp_arg(*optarg
);
10693 /* Reset activities array */
10694 memset(&wd
.wd_enabled
, 0, sizeof (wd
.wd_enabled
));
10696 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10697 static const char *const col_opts
[] = {
10698 "discard", "free", "initialize", "replace",
10699 "remove", "resilver", "scrub", "trim" };
10701 for (i
= 0; i
< ARRAY_SIZE(col_opts
); ++i
)
10702 if (strcmp(tok
, col_opts
[i
]) == 0) {
10703 wd
.wd_enabled
[i
] = B_TRUE
;
10707 (void) fprintf(stderr
,
10708 gettext("invalid activity '%s'\n"), tok
);
10714 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10723 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10725 /* This subcmd only accepts an interval, not a count */
10726 (void) fprintf(stderr
, gettext("too many arguments\n"));
10730 if (wd
.wd_interval
!= 0)
10734 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10738 (void) fprintf(stderr
, gettext("too many arguments\n"));
10742 wd
.wd_poolname
= argv
[0];
10744 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10749 * We use a separate thread for printing status updates because
10750 * the main thread will call lzc_wait(), which blocks as long
10751 * as an activity is in progress, which can be a long time.
10753 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10755 (void) fprintf(stderr
, gettext("failed to create status"
10756 "thread: %s\n"), strerror(errno
));
10763 * Loop over all activities that we are supposed to wait for until none
10764 * of them are in progress. Note that this means we can end up waiting
10765 * for more activities to complete than just those that were in progress
10766 * when we began waiting; if an activity we are interested in begins
10767 * while we are waiting for another activity, we will wait for both to
10768 * complete before exiting.
10771 boolean_t missing
= B_FALSE
;
10772 boolean_t any_waited
= B_FALSE
;
10774 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10777 if (!wd
.wd_enabled
[i
])
10780 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10781 if (error
!= 0 || missing
)
10784 any_waited
= (any_waited
|| waited
);
10787 if (error
!= 0 || missing
|| !any_waited
)
10795 pthread_mutex_lock(&wd
.wd_mutex
);
10796 wd
.wd_should_exit
= B_TRUE
;
10797 pthread_cond_signal(&wd
.wd_cv
);
10798 pthread_mutex_unlock(&wd
.wd_mutex
);
10799 (void) pthread_join(status_thr
, (void *)&status
);
10804 pthread_mutex_destroy(&wd
.wd_mutex
);
10805 pthread_cond_destroy(&wd
.wd_cv
);
10810 find_command_idx(const char *command
, int *idx
)
10812 for (int i
= 0; i
< NCOMMAND
; ++i
) {
10813 if (command_table
[i
].name
== NULL
)
10816 if (strcmp(command
, command_table
[i
].name
) == 0) {
10825 * Display version message
10828 zpool_do_version(int argc
, char **argv
)
10830 (void) argc
, (void) argv
;
10831 return (zfs_version_print() != 0);
10835 * Do zpool_load_compat() and print error message on failure
10837 static zpool_compat_status_t
10838 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
10842 zpool_compat_status_t ret
;
10844 ret
= zpool_load_compat(compat
, list
, report
, 1024);
10847 case ZPOOL_COMPATIBILITY_OK
:
10850 case ZPOOL_COMPATIBILITY_NOFILES
:
10851 case ZPOOL_COMPATIBILITY_BADFILE
:
10852 case ZPOOL_COMPATIBILITY_BADTOKEN
:
10853 (void) fprintf(stderr
, "Error: %s\n", report
);
10856 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
10857 (void) fprintf(stderr
, "Warning: %s\n", report
);
10858 ret
= ZPOOL_COMPATIBILITY_OK
;
10865 main(int argc
, char **argv
)
10872 (void) setlocale(LC_ALL
, "");
10873 (void) setlocale(LC_NUMERIC
, "C");
10874 (void) textdomain(TEXT_DOMAIN
);
10880 * Make sure the user has specified some command.
10883 (void) fprintf(stderr
, gettext("missing command\n"));
10890 * Special case '-?'
10892 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10896 * Special case '-V|--version'
10898 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10899 return (zpool_do_version(argc
, argv
));
10901 if ((g_zfs
= libzfs_init()) == NULL
) {
10902 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10906 libzfs_print_on_error(g_zfs
, B_TRUE
);
10908 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10911 * Many commands modify input strings for string parsing reasons.
10912 * We create a copy to protect the original argv.
10914 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
10915 for (i
= 0; i
< argc
; i
++)
10916 newargv
[i
] = strdup(argv
[i
]);
10917 newargv
[argc
] = NULL
;
10920 * Run the appropriate command.
10922 if (find_command_idx(cmdname
, &i
) == 0) {
10923 current_command
= &command_table
[i
];
10924 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10925 } else if (strchr(cmdname
, '=')) {
10926 verify(find_command_idx("set", &i
) == 0);
10927 current_command
= &command_table
[i
];
10928 ret
= command_table
[i
].func(argc
, newargv
);
10929 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10931 * 'freeze' is a vile debugging abomination, so we treat
10934 zfs_cmd_t zc
= {"\0"};
10936 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10937 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10939 (void) fprintf(stderr
,
10940 gettext("failed to freeze pool: %d\n"), errno
);
10946 (void) fprintf(stderr
, gettext("unrecognized "
10947 "command '%s'\n"), cmdname
);
10952 for (i
= 0; i
< argc
; i
++)
10956 if (ret
== 0 && log_history
)
10957 (void) zpool_log_history(g_zfs
, history_str
);
10959 libzfs_fini(g_zfs
);
10962 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10963 * for the purposes of running ::findleaks.
10965 if (getenv("ZFS_ABORT") != NULL
) {
10966 (void) printf("dumping core by request\n");