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"
425 "\tset <vdev_property=value> <pool> <vdev>\n"));
427 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
428 "\t [-o property=value] <pool> <newpool> "
429 "[<device> ...]\n"));
431 return (gettext("\treguid <pool>\n"));
433 return (gettext("\tsync [pool] ...\n"));
435 return (gettext("\tversion\n"));
437 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
438 "<pool> [interval]\n"));
440 __builtin_unreachable();
445 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
451 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
455 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
458 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
459 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
460 fnvlist_add_boolean(res
, path
);
466 for (i
= 0; i
< children
; i
++) {
467 zpool_collect_leaves(zhp
, child
[i
], res
);
472 * Callback routine that will print out a pool property value.
475 print_pool_prop_cb(int prop
, void *cb
)
479 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
481 if (zpool_prop_readonly(prop
))
482 (void) fprintf(fp
, " NO ");
484 (void) fprintf(fp
, " YES ");
486 if (zpool_prop_values(prop
) == NULL
)
487 (void) fprintf(fp
, "-\n");
489 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
495 * Callback routine that will print out a vdev property value.
498 print_vdev_prop_cb(int prop
, void *cb
)
502 (void) fprintf(fp
, "\t%-19s ", vdev_prop_to_name(prop
));
504 if (vdev_prop_readonly(prop
))
505 (void) fprintf(fp
, " NO ");
507 (void) fprintf(fp
, " YES ");
509 if (vdev_prop_values(prop
) == NULL
)
510 (void) fprintf(fp
, "-\n");
512 (void) fprintf(fp
, "%s\n", vdev_prop_values(prop
));
518 * Display usage message. If we're inside a command, display only the usage for
519 * that command. Otherwise, iterate over the entire command table and display
520 * a complete usage message.
522 static __attribute__((noreturn
)) void
523 usage(boolean_t requested
)
525 FILE *fp
= requested
? stdout
: stderr
;
527 if (current_command
== NULL
) {
530 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
532 gettext("where 'command' is one of the following:\n\n"));
534 for (i
= 0; i
< NCOMMAND
; i
++) {
535 if (command_table
[i
].name
== NULL
)
536 (void) fprintf(fp
, "\n");
538 (void) fprintf(fp
, "%s",
539 get_usage(command_table
[i
].usage
));
542 (void) fprintf(fp
, gettext("usage:\n"));
543 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
546 if (current_command
!= NULL
&&
547 current_prop_type
!= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
) &&
548 ((strcmp(current_command
->name
, "set") == 0) ||
549 (strcmp(current_command
->name
, "get") == 0) ||
550 (strcmp(current_command
->name
, "list") == 0))) {
552 (void) fprintf(fp
, "%s",
553 gettext("\nthe following properties are supported:\n"));
555 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
556 "PROPERTY", "EDIT", "VALUES");
558 /* Iterate over all properties */
559 if (current_prop_type
== ZFS_TYPE_POOL
) {
560 (void) zprop_iter(print_pool_prop_cb
, fp
, B_FALSE
,
561 B_TRUE
, current_prop_type
);
563 (void) fprintf(fp
, "\t%-19s ", "feature@...");
564 (void) fprintf(fp
, "YES "
565 "disabled | enabled | active\n");
567 (void) fprintf(fp
, gettext("\nThe feature@ properties "
568 "must be appended with a feature name.\n"
569 "See zpool-features(7).\n"));
570 } else if (current_prop_type
== ZFS_TYPE_VDEV
) {
571 (void) zprop_iter(print_vdev_prop_cb
, fp
, B_FALSE
,
572 B_TRUE
, current_prop_type
);
577 * See comments at end of main().
579 if (getenv("ZFS_ABORT") != NULL
) {
580 (void) printf("dumping core by request\n");
584 exit(requested
? 0 : 2);
588 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
589 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
592 * -c Cancel. Ends active initializing.
593 * -s Suspend. Initializing can then be restarted with no flags.
594 * -w Wait. Blocks until initializing has completed.
597 zpool_do_initialize(int argc
, char **argv
)
604 boolean_t wait
= B_FALSE
;
606 struct option long_options
[] = {
607 {"cancel", no_argument
, NULL
, 'c'},
608 {"suspend", no_argument
, NULL
, 's'},
609 {"wait", no_argument
, NULL
, 'w'},
613 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
614 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
617 if (cmd_type
!= POOL_INITIALIZE_START
&&
618 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
619 (void) fprintf(stderr
, gettext("-c cannot be "
620 "combined with other options\n"));
623 cmd_type
= POOL_INITIALIZE_CANCEL
;
626 if (cmd_type
!= POOL_INITIALIZE_START
&&
627 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
628 (void) fprintf(stderr
, gettext("-s cannot be "
629 "combined with other options\n"));
632 cmd_type
= POOL_INITIALIZE_SUSPEND
;
639 (void) fprintf(stderr
,
640 gettext("invalid option '%c'\n"), optopt
);
642 (void) fprintf(stderr
,
643 gettext("invalid option '%s'\n"),
654 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
659 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
660 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
666 zhp
= zpool_open(g_zfs
, poolname
);
670 vdevs
= fnvlist_alloc();
672 /* no individual leaf vdevs specified, so add them all */
673 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
674 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
675 ZPOOL_CONFIG_VDEV_TREE
);
676 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
678 for (int i
= 1; i
< argc
; i
++) {
679 fnvlist_add_boolean(vdevs
, argv
[i
]);
684 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
686 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
695 * print a pool vdev config for dry runs
698 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
699 const char *match
, int name_flags
)
704 boolean_t printed
= B_FALSE
;
706 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
707 &child
, &children
) != 0) {
709 (void) printf("\t%*s%s\n", indent
, "", name
);
713 for (c
= 0; c
< children
; c
++) {
714 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
715 const char *class = "";
717 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
720 if (is_hole
== B_TRUE
) {
724 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
727 class = VDEV_ALLOC_BIAS_LOG
;
728 (void) nvlist_lookup_string(child
[c
],
729 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
730 if (strcmp(match
, class) != 0)
733 if (!printed
&& name
!= NULL
) {
734 (void) printf("\t%*s%s\n", indent
, "", name
);
737 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
738 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
745 * Print the list of l2cache devices for dry runs.
748 print_cache_list(nvlist_t
*nv
, int indent
)
753 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
754 &child
, &children
) == 0 && children
> 0) {
755 (void) printf("\t%*s%s\n", indent
, "", "cache");
759 for (c
= 0; c
< children
; c
++) {
762 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
763 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
769 * Print the list of spares for dry runs.
772 print_spare_list(nvlist_t
*nv
, int indent
)
777 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
778 &child
, &children
) == 0 && children
> 0) {
779 (void) printf("\t%*s%s\n", indent
, "", "spares");
783 for (c
= 0; c
< children
; c
++) {
786 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
787 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
793 prop_list_contains_feature(nvlist_t
*proplist
)
796 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
797 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
798 if (zpool_prop_feature(nvpair_name(nvp
)))
805 * Add a property pair (name, string-value) into a property nvlist.
808 add_prop_list(const char *propname
, const char *propval
, nvlist_t
**props
,
811 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
816 if (*props
== NULL
&&
817 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
818 (void) fprintf(stderr
,
819 gettext("internal error: out of memory\n"));
826 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
828 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
830 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
831 (!zpool_prop_feature(propname
) &&
832 !zpool_prop_vdev(propname
))) {
833 (void) fprintf(stderr
, gettext("property '%s' is "
834 "not a valid pool or vdev property\n"), propname
);
839 * feature@ properties and version should not be specified
842 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
843 nvlist_exists(proplist
, vname
)) ||
844 (prop
== ZPOOL_PROP_VERSION
&&
845 prop_list_contains_feature(proplist
))) {
846 (void) fprintf(stderr
, gettext("'feature@' and "
847 "'version' properties cannot be specified "
853 * if version is specified, only "legacy" compatibility
856 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
857 strcmp(propval
, ZPOOL_COMPAT_LEGACY
) != 0 &&
858 nvlist_exists(proplist
, vname
)) ||
859 (prop
== ZPOOL_PROP_VERSION
&&
860 nvlist_exists(proplist
, cname
) &&
861 strcmp(fnvlist_lookup_string(proplist
, cname
),
862 ZPOOL_COMPAT_LEGACY
) != 0)) {
863 (void) fprintf(stderr
, gettext("when 'version' is "
864 "specified, the 'compatibility' feature may only "
865 "be set to '" ZPOOL_COMPAT_LEGACY
"'\n"));
869 if (zpool_prop_feature(propname
) || zpool_prop_vdev(propname
))
872 normnm
= zpool_prop_to_name(prop
);
874 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
876 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
878 normnm
= zfs_prop_to_name(fsprop
);
879 } else if (zfs_prop_user(propname
) ||
880 zfs_prop_userquota(propname
)) {
883 (void) fprintf(stderr
, gettext("property '%s' is "
884 "not a valid filesystem property\n"), propname
);
889 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
890 prop
!= ZPOOL_PROP_CACHEFILE
) {
891 (void) fprintf(stderr
, gettext("property '%s' "
892 "specified multiple times\n"), propname
);
896 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
897 (void) fprintf(stderr
, gettext("internal "
898 "error: out of memory\n"));
906 * Set a default property pair (name, string-value) in a property nvlist
909 add_prop_list_default(const char *propname
, const char *propval
,
914 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
917 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
921 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
923 * -f Force addition of devices, even if they appear in use
924 * -g Display guid for individual vdev name.
925 * -L Follow links when resolving vdev path name.
926 * -n Do not add the devices, but display the resulting layout if
927 * they were to be added.
928 * -o Set property=value.
929 * -P Display full path for vdev name.
931 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
932 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
936 zpool_do_add(int argc
, char **argv
)
938 boolean_t force
= B_FALSE
;
939 boolean_t dryrun
= B_FALSE
;
947 nvlist_t
*props
= NULL
;
951 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
957 name_flags
|= VDEV_NAME_GUID
;
960 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
966 if ((propval
= strchr(optarg
, '=')) == NULL
) {
967 (void) fprintf(stderr
, gettext("missing "
968 "'=' for -o option\n"));
974 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
975 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
979 name_flags
|= VDEV_NAME_PATH
;
982 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
991 /* get pool name and check number of arguments */
993 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
997 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1006 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1009 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
1010 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
1016 /* unless manually specified use "ashift" pool property (if set) */
1017 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
1020 char strval
[ZPOOL_MAXPROPLEN
];
1022 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
1023 if (src
!= ZPROP_SRC_DEFAULT
) {
1024 (void) sprintf(strval
, "%" PRId32
, intval
);
1025 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
1026 &props
, B_TRUE
) == 0);
1030 /* pass off to make_root_vdev for processing */
1031 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
1033 if (nvroot
== NULL
) {
1039 nvlist_t
*poolnvroot
;
1040 nvlist_t
**l2child
, **sparechild
;
1041 uint_t l2children
, sparechildren
, c
;
1043 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1045 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1048 (void) printf(gettext("would update '%s' to the following "
1049 "configuration:\n\n"), zpool_get_name(zhp
));
1051 /* print original main pool and new tree */
1052 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1053 name_flags
| VDEV_NAME_TYPE_ID
);
1054 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1056 /* print other classes: 'dedup', 'special', and 'log' */
1057 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1058 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1059 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1060 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1061 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1062 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1063 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1064 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1067 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1068 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1069 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1070 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1071 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1072 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1073 print_vdev_tree(zhp
, "special", nvroot
, 0,
1074 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1077 if (num_logs(poolnvroot
) > 0) {
1078 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1079 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1080 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1081 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1082 } else if (num_logs(nvroot
) > 0) {
1083 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1084 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1087 /* Do the same for the caches */
1088 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1089 &l2child
, &l2children
) == 0 && l2children
) {
1091 (void) printf(gettext("\tcache\n"));
1092 for (c
= 0; c
< l2children
; c
++) {
1093 vname
= zpool_vdev_name(g_zfs
, NULL
,
1094 l2child
[c
], name_flags
);
1095 (void) printf("\t %s\n", vname
);
1099 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1100 &l2child
, &l2children
) == 0 && l2children
) {
1102 (void) printf(gettext("\tcache\n"));
1103 for (c
= 0; c
< l2children
; c
++) {
1104 vname
= zpool_vdev_name(g_zfs
, NULL
,
1105 l2child
[c
], name_flags
);
1106 (void) printf("\t %s\n", vname
);
1110 /* And finally the spares */
1111 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1112 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1114 (void) printf(gettext("\tspares\n"));
1115 for (c
= 0; c
< sparechildren
; c
++) {
1116 vname
= zpool_vdev_name(g_zfs
, NULL
,
1117 sparechild
[c
], name_flags
);
1118 (void) printf("\t %s\n", vname
);
1122 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1123 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1125 (void) printf(gettext("\tspares\n"));
1126 for (c
= 0; c
< sparechildren
; c
++) {
1127 vname
= zpool_vdev_name(g_zfs
, NULL
,
1128 sparechild
[c
], name_flags
);
1129 (void) printf("\t %s\n", vname
);
1136 ret
= (zpool_add(zhp
, nvroot
) != 0);
1140 nvlist_free(nvroot
);
1147 * zpool remove [-npsw] <pool> <vdev> ...
1149 * Removes the given vdev from the pool.
1152 zpool_do_remove(int argc
, char **argv
)
1156 zpool_handle_t
*zhp
= NULL
;
1157 boolean_t stop
= B_FALSE
;
1159 boolean_t noop
= B_FALSE
;
1160 boolean_t parsable
= B_FALSE
;
1161 boolean_t wait
= B_FALSE
;
1164 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1179 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1188 /* get pool name and check number of arguments */
1190 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1196 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1201 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1207 (void) fprintf(stderr
, gettext("too many arguments\n"));
1210 if (zpool_vdev_remove_cancel(zhp
) != 0)
1213 (void) fprintf(stderr
, gettext("invalid option "
1214 "combination: -w cannot be used with -s\n"));
1219 (void) fprintf(stderr
, gettext("missing device\n"));
1223 for (i
= 1; i
< argc
; i
++) {
1227 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1233 (void) printf("%s %llu\n",
1234 argv
[i
], (unsigned long long)size
);
1237 zfs_nicenum(size
, valstr
,
1239 (void) printf("Memory that will be "
1240 "used after removing %s: %s\n",
1244 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1249 if (ret
== 0 && wait
)
1250 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1258 * Return 1 if a vdev is active (being used in a pool)
1259 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1261 * This is useful for checking if a disk in an active pool is offlined or
1265 vdev_is_active(char *vdev_path
)
1268 fd
= open(vdev_path
, O_EXCL
);
1270 return (1); /* cant open O_EXCL - disk is active */
1274 return (0); /* disk is inactive in the pool */
1278 * zpool labelclear [-f] <vdev>
1280 * -f Force clearing the label for the vdevs which are members of
1281 * the exported or foreign pools.
1283 * Verifies that the vdev is not active and zeros out the label information
1287 zpool_do_labelclear(int argc
, char **argv
)
1289 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"));
1323 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1326 * If we cannot open an absolute path, we quit.
1327 * Otherwise if the provided vdev name doesn't point to a file,
1328 * try prepending expected disk paths and partition numbers.
1330 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1332 if (vdev
[0] == '/') {
1333 (void) fprintf(stderr
, gettext("failed to open "
1334 "%s: %s\n"), vdev
, strerror(errno
));
1338 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1339 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1340 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1344 if (error
|| ((fd
= open(vdev
, O_RDWR
)) < 0)) {
1345 if (errno
== ENOENT
) {
1346 (void) fprintf(stderr
, gettext(
1347 "failed to find device %s, try "
1348 "specifying absolute path instead\n"),
1353 (void) fprintf(stderr
, gettext("failed to open %s:"
1354 " %s\n"), vdev
, strerror(errno
));
1360 * Flush all dirty pages for the block device. This should not be
1361 * fatal when the device does not support BLKFLSBUF as would be the
1362 * case for a file vdev.
1364 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1365 (void) fprintf(stderr
, gettext("failed to invalidate "
1366 "cache for %s: %s\n"), vdev
, strerror(errno
));
1368 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1369 (void) fprintf(stderr
,
1370 gettext("failed to read label from %s\n"), vdev
);
1374 nvlist_free(config
);
1376 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1378 (void) fprintf(stderr
,
1379 gettext("failed to check state for %s\n"), vdev
);
1389 case POOL_STATE_ACTIVE
:
1390 case POOL_STATE_SPARE
:
1391 case POOL_STATE_L2CACHE
:
1393 * We allow the user to call 'zpool offline -f'
1394 * on an offlined disk in an active pool. We can check if
1395 * the disk is online by calling vdev_is_active().
1397 if (force
&& !vdev_is_active(vdev
))
1400 (void) fprintf(stderr
, gettext(
1401 "%s is a member (%s) of pool \"%s\""),
1402 vdev
, zpool_pool_state_to_name(state
), name
);
1405 (void) fprintf(stderr
, gettext(
1406 ". Offline the disk first to clear its label."));
1412 case POOL_STATE_EXPORTED
:
1415 (void) fprintf(stderr
, gettext(
1416 "use '-f' to override the following error:\n"
1417 "%s is a member of exported pool \"%s\"\n"),
1422 case POOL_STATE_POTENTIALLY_ACTIVE
:
1425 (void) fprintf(stderr
, gettext(
1426 "use '-f' to override the following error:\n"
1427 "%s is a member of potentially active pool \"%s\"\n"),
1432 case POOL_STATE_DESTROYED
:
1433 /* inuse should never be set for a destroyed pool */
1439 ret
= zpool_clear_label(fd
);
1441 (void) fprintf(stderr
,
1442 gettext("failed to clear label for %s\n"), vdev
);
1453 * zpool create [-fnd] [-o property=value] ...
1454 * [-O file-system-property=value] ...
1455 * [-R root] [-m mountpoint] <pool> <dev> ...
1457 * -f Force creation, even if devices appear in use
1458 * -n Do not create the pool, but display the resulting layout if it
1459 * were to be created.
1460 * -R Create a pool under an alternate root
1461 * -m Set default mountpoint for the root dataset. By default it's
1463 * -o Set property=value.
1464 * -o Set feature@feature=enabled|disabled.
1465 * -d Don't automatically enable all supported pool features
1466 * (individual features can be enabled with -o).
1467 * -O Set fsproperty=value in the pool's root file system
1469 * Creates the named pool according to the given vdev specification. The
1470 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1471 * Once we get the nvlist back from make_root_vdev(), we either print out the
1472 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1475 zpool_do_create(int argc
, char **argv
)
1477 boolean_t force
= B_FALSE
;
1478 boolean_t dryrun
= B_FALSE
;
1479 boolean_t enable_pool_features
= B_TRUE
;
1482 nvlist_t
*nvroot
= NULL
;
1486 char *altroot
= NULL
;
1487 char *compat
= NULL
;
1488 char *mountpoint
= NULL
;
1489 nvlist_t
*fsprops
= NULL
;
1490 nvlist_t
*props
= NULL
;
1494 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1503 enable_pool_features
= B_FALSE
;
1507 if (add_prop_list(zpool_prop_to_name(
1508 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1510 if (add_prop_list_default(zpool_prop_to_name(
1511 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1515 /* Equivalent to -O mountpoint=optarg */
1516 mountpoint
= optarg
;
1519 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1520 (void) fprintf(stderr
, gettext("missing "
1521 "'=' for -o option\n"));
1527 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1531 * If the user is creating a pool that doesn't support
1532 * feature flags, don't enable any features.
1534 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1538 ver
= strtoull(propval
, &end
, 10);
1540 ver
< SPA_VERSION_FEATURES
) {
1541 enable_pool_features
= B_FALSE
;
1544 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1546 if (zpool_name_to_prop(optarg
) ==
1547 ZPOOL_PROP_COMPATIBILITY
)
1551 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1552 (void) fprintf(stderr
, gettext("missing "
1553 "'=' for -O option\n"));
1560 * Mountpoints are checked and then added later.
1561 * Uniquely among properties, they can be specified
1562 * more than once, to avoid conflict with -m.
1564 if (0 == strcmp(optarg
,
1565 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1566 mountpoint
= propval
;
1567 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1574 * Sanity check temporary pool name.
1576 if (strchr(optarg
, '/') != NULL
) {
1577 (void) fprintf(stderr
, gettext("cannot create "
1578 "'%s': invalid character '/' in temporary "
1580 (void) fprintf(stderr
, gettext("use 'zfs "
1581 "create' to create a dataset\n"));
1585 if (add_prop_list(zpool_prop_to_name(
1586 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1588 if (add_prop_list_default(zpool_prop_to_name(
1589 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1594 (void) fprintf(stderr
, gettext("missing argument for "
1595 "'%c' option\n"), optopt
);
1598 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1607 /* get pool name and check number of arguments */
1609 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1613 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1620 * As a special case, check for use of '/' in the name, and direct the
1621 * user to use 'zfs create' instead.
1623 if (strchr(poolname
, '/') != NULL
) {
1624 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1625 "character '/' in pool name\n"), poolname
);
1626 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1627 "create a dataset\n"));
1631 /* pass off to make_root_vdev for bulk processing */
1632 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1633 argc
- 1, argv
+ 1);
1637 /* make_root_vdev() allows 0 toplevel children if there are spares */
1638 if (!zfs_allocatable_devs(nvroot
)) {
1639 (void) fprintf(stderr
, gettext("invalid vdev "
1640 "specification: at least one toplevel vdev must be "
1645 if (altroot
!= NULL
&& altroot
[0] != '/') {
1646 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1647 "must be an absolute path\n"), altroot
);
1652 * Check the validity of the mountpoint and direct the user to use the
1653 * '-m' mountpoint option if it looks like its in use.
1655 if (mountpoint
== NULL
||
1656 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1657 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1658 char buf
[MAXPATHLEN
];
1661 if (mountpoint
&& mountpoint
[0] != '/') {
1662 (void) fprintf(stderr
, gettext("invalid mountpoint "
1663 "'%s': must be an absolute path, 'legacy', or "
1664 "'none'\n"), mountpoint
);
1668 if (mountpoint
== NULL
) {
1669 if (altroot
!= NULL
)
1670 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1673 (void) snprintf(buf
, sizeof (buf
), "/%s",
1676 if (altroot
!= NULL
)
1677 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1678 altroot
, mountpoint
);
1680 (void) snprintf(buf
, sizeof (buf
), "%s",
1684 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1685 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1686 "%s\n"), buf
, strerror(errno
));
1687 (void) fprintf(stderr
, gettext("use '-m' "
1688 "option to provide a different default\n"));
1693 while (count
< 3 && readdir(dirp
) != NULL
)
1695 (void) closedir(dirp
);
1698 (void) fprintf(stderr
, gettext("mountpoint "
1699 "'%s' exists and is not empty\n"), buf
);
1700 (void) fprintf(stderr
, gettext("use '-m' "
1701 "option to provide a "
1702 "different default\n"));
1709 * Now that the mountpoint's validity has been checked, ensure that
1710 * the property is set appropriately prior to creating the pool.
1712 if (mountpoint
!= NULL
) {
1713 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1714 mountpoint
, &fsprops
, B_FALSE
);
1722 * For a dry run invocation, print out a basic message and run
1723 * through all the vdevs in the list and print out in an
1724 * appropriate hierarchy.
1726 (void) printf(gettext("would create '%s' with the "
1727 "following layout:\n\n"), poolname
);
1729 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1730 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1731 VDEV_ALLOC_BIAS_DEDUP
, 0);
1732 print_vdev_tree(NULL
, "special", nvroot
, 0,
1733 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1734 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1735 VDEV_ALLOC_BIAS_LOG
, 0);
1736 print_cache_list(nvroot
, 0);
1737 print_spare_list(nvroot
, 0);
1742 * Load in feature set.
1743 * Note: if compatibility property not given, we'll have
1744 * NULL, which means 'all features'.
1746 boolean_t requested_features
[SPA_FEATURES
];
1747 if (zpool_do_load_compat(compat
, requested_features
) !=
1748 ZPOOL_COMPATIBILITY_OK
)
1752 * props contains list of features to enable.
1754 * - remove it if feature@name=disabled
1755 * - leave it there if feature@name=enabled
1757 * - enable_pool_features (ie: no '-d' or '-o version')
1758 * - it's supported by the kernel module
1759 * - it's in the requested feature set
1760 * - warn if it's enabled but not in compat
1762 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1763 char propname
[MAXPATHLEN
];
1764 const char *propval
;
1765 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1767 (void) snprintf(propname
, sizeof (propname
),
1768 "feature@%s", feat
->fi_uname
);
1770 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1772 ZFS_FEATURE_DISABLED
) == 0) {
1773 (void) nvlist_remove_all(props
,
1775 } else if (strcmp(propval
,
1776 ZFS_FEATURE_ENABLED
) == 0 &&
1777 !requested_features
[i
]) {
1778 (void) fprintf(stderr
, gettext(
1779 "Warning: feature \"%s\" enabled "
1780 "but is not in specified "
1781 "'compatibility' feature set.\n"),
1785 enable_pool_features
&&
1786 feat
->fi_zfs_mod_supported
&&
1787 requested_features
[i
]) {
1788 ret
= add_prop_list(propname
,
1789 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1796 if (zpool_create(g_zfs
, poolname
,
1797 nvroot
, props
, fsprops
) == 0) {
1798 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1799 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1801 if (zfs_mount(pool
, NULL
, 0) == 0) {
1802 ret
= zfs_share(pool
, NULL
);
1803 zfs_commit_shares(NULL
);
1807 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1808 (void) fprintf(stderr
, gettext("pool name may have "
1814 nvlist_free(nvroot
);
1815 nvlist_free(fsprops
);
1819 nvlist_free(fsprops
);
1826 * zpool destroy <pool>
1828 * -f Forcefully unmount any datasets
1830 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1833 zpool_do_destroy(int argc
, char **argv
)
1835 boolean_t force
= B_FALSE
;
1838 zpool_handle_t
*zhp
;
1842 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1848 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1857 /* check arguments */
1859 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1863 (void) fprintf(stderr
, gettext("too many arguments\n"));
1869 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1871 * As a special case, check for use of '/' in the name, and
1872 * direct the user to use 'zfs destroy' instead.
1874 if (strchr(pool
, '/') != NULL
)
1875 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1876 "destroy a dataset\n"));
1880 if (zpool_disable_datasets(zhp
, force
) != 0) {
1881 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1882 "could not unmount datasets\n"), zpool_get_name(zhp
));
1887 /* The history must be logged as part of the export */
1888 log_history
= B_FALSE
;
1890 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1897 typedef struct export_cbdata
{
1899 boolean_t hardforce
;
1906 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1908 export_cbdata_t
*cb
= data
;
1910 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1913 /* The history must be logged as part of the export */
1914 log_history
= B_FALSE
;
1916 if (cb
->hardforce
) {
1917 if (zpool_export_force(zhp
, history_str
) != 0)
1919 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1927 * zpool export [-f] <pool> ...
1929 * -a Export all pools
1930 * -f Forcefully unmount datasets
1932 * Export the given pools. By default, the command will attempt to cleanly
1933 * unmount any active datasets within the pool. If the '-f' flag is specified,
1934 * then the datasets will be forcefully unmounted.
1937 zpool_do_export(int argc
, char **argv
)
1940 boolean_t do_all
= B_FALSE
;
1941 boolean_t force
= B_FALSE
;
1942 boolean_t hardforce
= B_FALSE
;
1946 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1958 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1965 cb
.hardforce
= hardforce
;
1971 (void) fprintf(stderr
, gettext("too many arguments\n"));
1975 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1976 ZFS_TYPE_POOL
, B_FALSE
, zpool_export_one
, &cb
));
1979 /* check arguments */
1981 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1985 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
1986 B_FALSE
, zpool_export_one
, &cb
);
1992 * Given a vdev configuration, determine the maximum width needed for the device
1996 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1999 static const char *const subtypes
[] =
2000 {ZPOOL_CONFIG_SPARES
, ZPOOL_CONFIG_L2CACHE
, ZPOOL_CONFIG_CHILDREN
};
2002 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
2003 max
= MAX(strlen(name
) + depth
, max
);
2008 for (size_t i
= 0; i
< ARRAY_SIZE(subtypes
); ++i
)
2009 if (nvlist_lookup_nvlist_array(nv
, subtypes
[i
],
2010 &child
, &children
) == 0)
2011 for (uint_t c
= 0; c
< children
; ++c
)
2012 max
= MAX(max_width(zhp
, child
[c
], depth
+ 2,
2013 max
, name_flags
), max
);
2018 typedef struct spare_cbdata
{
2020 zpool_handle_t
*cb_zhp
;
2024 find_vdev(nvlist_t
*nv
, uint64_t search
)
2030 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
2034 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2035 &child
, &children
) == 0) {
2036 for (c
= 0; c
< children
; c
++)
2037 if (find_vdev(child
[c
], search
))
2045 find_spare(zpool_handle_t
*zhp
, void *data
)
2047 spare_cbdata_t
*cbp
= data
;
2048 nvlist_t
*config
, *nvroot
;
2050 config
= zpool_get_config(zhp
, NULL
);
2051 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2054 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
2063 typedef struct status_cbdata
{
2067 boolean_t cb_allpools
;
2068 boolean_t cb_verbose
;
2069 boolean_t cb_literal
;
2070 boolean_t cb_explain
;
2072 boolean_t cb_dedup_stats
;
2073 boolean_t cb_print_status
;
2074 boolean_t cb_print_slow_ios
;
2075 boolean_t cb_print_vdev_init
;
2076 boolean_t cb_print_vdev_trim
;
2077 vdev_cmd_data_list_t
*vcdl
;
2080 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2082 is_blank_str(const char *str
)
2084 for (; str
!= NULL
&& *str
!= '\0'; ++str
)
2090 /* Print command output lines for specific vdev in a specific pool */
2092 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, const char *path
)
2094 vdev_cmd_data_t
*data
;
2098 for (i
= 0; i
< vcdl
->count
; i
++) {
2099 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2100 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2101 /* Not the vdev we're looking for */
2105 data
= &vcdl
->data
[i
];
2106 /* Print out all the output values for this vdev */
2107 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2109 /* Does this vdev have values for this column? */
2110 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2111 if (strcmp(data
->cols
[k
],
2112 vcdl
->uniq_cols
[j
]) == 0) {
2113 /* yes it does, record the value */
2114 val
= data
->lines
[k
];
2119 * Mark empty values with dashes to make output
2122 if (val
== NULL
|| is_blank_str(val
))
2125 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2126 if (j
< vcdl
->uniq_cols_cnt
- 1)
2130 /* Print out any values that aren't in a column at the end */
2131 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2132 /* Did we have any columns? If so print a spacer. */
2133 if (vcdl
->uniq_cols_cnt
> 0)
2136 val
= data
->lines
[j
];
2137 fputs(val
?: "", stdout
);
2144 * Print vdev initialization status for leaves
2147 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2150 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2151 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2152 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2153 !vs
->vs_scan_removing
) {
2156 struct tm zaction_ts
;
2158 time_t t
= vs
->vs_initialize_action_time
;
2159 int initialize_pct
= 100;
2160 if (vs
->vs_initialize_state
!=
2161 VDEV_INITIALIZE_COMPLETE
) {
2162 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2163 100 / (vs
->vs_initialize_bytes_est
+ 1));
2166 (void) localtime_r(&t
, &zaction_ts
);
2167 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2169 switch (vs
->vs_initialize_state
) {
2170 case VDEV_INITIALIZE_SUSPENDED
:
2171 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2172 gettext("suspended, started at"), tbuf
);
2174 case VDEV_INITIALIZE_ACTIVE
:
2175 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2176 gettext("started at"), tbuf
);
2178 case VDEV_INITIALIZE_COMPLETE
:
2179 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2180 gettext("completed at"), tbuf
);
2184 (void) printf(gettext(" (%d%% initialized%s)"),
2185 initialize_pct
, zbuf
);
2187 (void) printf(gettext(" (uninitialized)"));
2189 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2190 (void) printf(gettext(" (initializing)"));
2195 * Print vdev TRIM status for leaves
2198 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2201 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2202 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2203 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2204 !vs
->vs_scan_removing
) {
2207 struct tm zaction_ts
;
2209 time_t t
= vs
->vs_trim_action_time
;
2211 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2212 trim_pct
= (vs
->vs_trim_bytes_done
*
2213 100 / (vs
->vs_trim_bytes_est
+ 1));
2216 (void) localtime_r(&t
, &zaction_ts
);
2217 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2219 switch (vs
->vs_trim_state
) {
2220 case VDEV_TRIM_SUSPENDED
:
2221 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2222 gettext("suspended, started at"), tbuf
);
2224 case VDEV_TRIM_ACTIVE
:
2225 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2226 gettext("started at"), tbuf
);
2228 case VDEV_TRIM_COMPLETE
:
2229 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2230 gettext("completed at"), tbuf
);
2234 (void) printf(gettext(" (%d%% trimmed%s)"),
2236 } else if (vs
->vs_trim_notsup
) {
2237 (void) printf(gettext(" (trim unsupported)"));
2239 (void) printf(gettext(" (untrimmed)"));
2241 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2242 (void) printf(gettext(" (trimming)"));
2247 * Return the color associated with a health string. This includes returning
2248 * NULL for no color change.
2251 health_str_to_color(const char *health
)
2253 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2254 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2255 strcmp(health
, gettext("UNAVAIL")) == 0) {
2259 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2260 strcmp(health
, gettext("DEGRADED")) == 0 ||
2261 strcmp(health
, gettext("REMOVED")) == 0) {
2262 return (ANSI_YELLOW
);
2269 * Print out configuration state as requested by status_callback.
2272 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2273 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2275 nvlist_t
**child
, *root
;
2276 uint_t c
, i
, vsc
, children
;
2277 pool_scan_stat_t
*ps
= NULL
;
2279 char rbuf
[6], wbuf
[6], cbuf
[6];
2281 uint64_t notpresent
;
2282 spare_cbdata_t spare_cb
;
2285 const char *path
= NULL
;
2286 const char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2288 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2289 &child
, &children
) != 0)
2292 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2293 (uint64_t **)&vs
, &vsc
) == 0);
2295 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2297 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2300 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2304 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2307 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2308 state
= gettext("INUSE");
2309 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2310 state
= gettext("AVAIL");
2313 printf_color(health_str_to_color(state
),
2314 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2318 if (vs
->vs_read_errors
)
2321 if (vs
->vs_write_errors
)
2324 if (vs
->vs_checksum_errors
)
2327 if (cb
->cb_literal
) {
2329 printf_color(rcolor
, "%5llu",
2330 (u_longlong_t
)vs
->vs_read_errors
);
2332 printf_color(wcolor
, "%5llu",
2333 (u_longlong_t
)vs
->vs_write_errors
);
2335 printf_color(ccolor
, "%5llu",
2336 (u_longlong_t
)vs
->vs_checksum_errors
);
2338 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2339 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2340 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2343 printf_color(rcolor
, "%5s", rbuf
);
2345 printf_color(wcolor
, "%5s", wbuf
);
2347 printf_color(ccolor
, "%5s", cbuf
);
2349 if (cb
->cb_print_slow_ios
) {
2350 if (children
== 0) {
2351 /* Only leafs vdevs have slow IOs */
2352 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2355 snprintf(rbuf
, sizeof (rbuf
), "-");
2359 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2361 printf(" %5s", rbuf
);
2365 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2366 ¬present
) == 0) {
2367 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2368 (void) printf(" %s %s", gettext("was"), path
);
2369 } else if (vs
->vs_aux
!= 0) {
2371 color_start(ANSI_RED
);
2372 switch (vs
->vs_aux
) {
2373 case VDEV_AUX_OPEN_FAILED
:
2374 (void) printf(gettext("cannot open"));
2377 case VDEV_AUX_BAD_GUID_SUM
:
2378 (void) printf(gettext("missing device"));
2381 case VDEV_AUX_NO_REPLICAS
:
2382 (void) printf(gettext("insufficient replicas"));
2385 case VDEV_AUX_VERSION_NEWER
:
2386 (void) printf(gettext("newer version"));
2389 case VDEV_AUX_UNSUP_FEAT
:
2390 (void) printf(gettext("unsupported feature(s)"));
2393 case VDEV_AUX_ASHIFT_TOO_BIG
:
2394 (void) printf(gettext("unsupported minimum blocksize"));
2397 case VDEV_AUX_SPARED
:
2398 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2399 &spare_cb
.cb_guid
) == 0);
2400 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2401 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2402 zpool_get_name(zhp
)) == 0)
2403 (void) printf(gettext("currently in "
2406 (void) printf(gettext("in use by "
2408 zpool_get_name(spare_cb
.cb_zhp
));
2409 zpool_close(spare_cb
.cb_zhp
);
2411 (void) printf(gettext("currently in use"));
2415 case VDEV_AUX_ERR_EXCEEDED
:
2416 (void) printf(gettext("too many errors"));
2419 case VDEV_AUX_IO_FAILURE
:
2420 (void) printf(gettext("experienced I/O failures"));
2423 case VDEV_AUX_BAD_LOG
:
2424 (void) printf(gettext("bad intent log"));
2427 case VDEV_AUX_EXTERNAL
:
2428 (void) printf(gettext("external device fault"));
2431 case VDEV_AUX_SPLIT_POOL
:
2432 (void) printf(gettext("split into new pool"));
2435 case VDEV_AUX_ACTIVE
:
2436 (void) printf(gettext("currently in use"));
2439 case VDEV_AUX_CHILDREN_OFFLINE
:
2440 (void) printf(gettext("all children offline"));
2443 case VDEV_AUX_BAD_LABEL
:
2444 (void) printf(gettext("invalid label"));
2448 (void) printf(gettext("corrupted data"));
2452 } else if (children
== 0 && !isspare
&&
2453 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2454 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2455 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2457 gettext(" block size: %dB configured, %dB native"),
2458 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2461 if (vs
->vs_scan_removing
!= 0) {
2462 (void) printf(gettext(" (removing)"));
2463 } else if (VDEV_STAT_VALID(vs_noalloc
, vsc
) && vs
->vs_noalloc
!= 0) {
2464 (void) printf(gettext(" (non-allocating)"));
2467 /* The root vdev has the scrub/resilver stats */
2468 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2469 ZPOOL_CONFIG_VDEV_TREE
);
2470 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2471 (uint64_t **)&ps
, &c
);
2474 * If you force fault a drive that's resilvering, its scan stats can
2475 * get frozen in time, giving the false impression that it's
2476 * being resilvered. That's why we check the state to see if the vdev
2477 * is healthy before reporting "resilvering" or "repairing".
2479 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0 &&
2480 vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2481 if (vs
->vs_scan_processed
!= 0) {
2482 (void) printf(gettext(" (%s)"),
2483 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2484 "resilvering" : "repairing");
2485 } else if (vs
->vs_resilver_deferred
) {
2486 (void) printf(gettext(" (awaiting resilver)"));
2490 /* The top-level vdevs have the rebuild stats */
2491 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2492 children
== 0 && vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2493 if (vs
->vs_rebuild_processed
!= 0) {
2494 (void) printf(gettext(" (resilvering)"));
2498 if (cb
->vcdl
!= NULL
) {
2499 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2501 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2505 /* Display vdev initialization and trim status for leaves. */
2506 if (children
== 0) {
2507 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2508 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2511 (void) printf("\n");
2513 for (c
= 0; c
< children
; c
++) {
2514 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2516 /* Don't print logs or holes here */
2517 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2519 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2521 if (islog
|| ishole
)
2523 /* Only print normal classes here */
2524 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2527 /* Provide vdev_rebuild_stats to children if available */
2529 (void) nvlist_lookup_uint64_array(nv
,
2530 ZPOOL_CONFIG_REBUILD_STATS
,
2531 (uint64_t **)&vrs
, &i
);
2534 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2535 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2536 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2543 * Print the configuration of an exported pool. Iterate over all vdevs in the
2544 * pool, printing out the name and status for each one.
2547 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2556 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2557 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2558 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2561 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2562 (uint64_t **)&vs
, &c
) == 0);
2564 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2565 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2567 if (vs
->vs_aux
!= 0) {
2570 switch (vs
->vs_aux
) {
2571 case VDEV_AUX_OPEN_FAILED
:
2572 (void) printf(gettext("cannot open"));
2575 case VDEV_AUX_BAD_GUID_SUM
:
2576 (void) printf(gettext("missing device"));
2579 case VDEV_AUX_NO_REPLICAS
:
2580 (void) printf(gettext("insufficient replicas"));
2583 case VDEV_AUX_VERSION_NEWER
:
2584 (void) printf(gettext("newer version"));
2587 case VDEV_AUX_UNSUP_FEAT
:
2588 (void) printf(gettext("unsupported feature(s)"));
2591 case VDEV_AUX_ERR_EXCEEDED
:
2592 (void) printf(gettext("too many errors"));
2595 case VDEV_AUX_ACTIVE
:
2596 (void) printf(gettext("currently in use"));
2599 case VDEV_AUX_CHILDREN_OFFLINE
:
2600 (void) printf(gettext("all children offline"));
2603 case VDEV_AUX_BAD_LABEL
:
2604 (void) printf(gettext("invalid label"));
2608 (void) printf(gettext("corrupted data"));
2612 (void) printf("\n");
2614 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2615 &child
, &children
) != 0)
2618 for (c
= 0; c
< children
; c
++) {
2619 uint64_t is_log
= B_FALSE
;
2621 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2625 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2628 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2629 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2630 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2634 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2635 &child
, &children
) == 0) {
2636 (void) printf(gettext("\tcache\n"));
2637 for (c
= 0; c
< children
; c
++) {
2638 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2640 (void) printf("\t %s\n", vname
);
2645 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2646 &child
, &children
) == 0) {
2647 (void) printf(gettext("\tspares\n"));
2648 for (c
= 0; c
< children
; c
++) {
2649 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2651 (void) printf("\t %s\n", vname
);
2658 * Print specialized class vdevs.
2660 * These are recorded as top level vdevs in the main pool child array
2661 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2662 * print_status_config() or print_import_config() to print the top level
2663 * class vdevs then any of their children (eg mirrored slogs) are printed
2664 * recursively - which works because only the top level vdev is marked.
2667 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2672 boolean_t printed
= B_FALSE
;
2674 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2676 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2680 for (c
= 0; c
< children
; c
++) {
2681 uint64_t is_log
= B_FALSE
;
2682 const char *bias
= NULL
;
2683 const char *type
= NULL
;
2685 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2689 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
2691 (void) nvlist_lookup_string(child
[c
],
2692 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2693 (void) nvlist_lookup_string(child
[c
],
2694 ZPOOL_CONFIG_TYPE
, &type
);
2697 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2699 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2703 (void) printf("\t%s\t\n", gettext(class));
2707 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2708 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2709 if (cb
->cb_print_status
)
2710 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2713 print_import_config(cb
, name
, child
[c
], 2);
2719 * Display the status for the given pool.
2722 show_import(nvlist_t
*config
, boolean_t report_error
)
2724 uint64_t pool_state
;
2728 uint64_t hostid
= 0;
2730 const char *hostname
= "unknown";
2731 nvlist_t
*nvroot
, *nvinfo
;
2732 zpool_status_t reason
;
2733 zpool_errata_t errata
;
2736 const char *comment
;
2737 status_cbdata_t cb
= { 0 };
2739 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2741 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2743 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2745 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2748 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2749 (uint64_t **)&vs
, &vsc
) == 0);
2750 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2752 reason
= zpool_import_status(config
, &msgid
, &errata
);
2755 * If we're importing using a cachefile, then we won't report any
2756 * errors unless we are in the scan phase of the import.
2758 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
2761 (void) printf(gettext(" pool: %s\n"), name
);
2762 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2763 (void) printf(gettext(" state: %s"), health
);
2764 if (pool_state
== POOL_STATE_DESTROYED
)
2765 (void) printf(gettext(" (DESTROYED)"));
2766 (void) printf("\n");
2769 case ZPOOL_STATUS_MISSING_DEV_R
:
2770 case ZPOOL_STATUS_MISSING_DEV_NR
:
2771 case ZPOOL_STATUS_BAD_GUID_SUM
:
2772 printf_color(ANSI_BOLD
, gettext("status: "));
2773 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2774 "missing from the system.\n"));
2777 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2778 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2779 printf_color(ANSI_BOLD
, gettext("status: "));
2780 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2781 " corrupted data.\n"));
2784 case ZPOOL_STATUS_CORRUPT_DATA
:
2786 gettext(" status: The pool data is corrupted.\n"));
2789 case ZPOOL_STATUS_OFFLINE_DEV
:
2790 printf_color(ANSI_BOLD
, gettext("status: "));
2791 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2792 "are offlined.\n"));
2795 case ZPOOL_STATUS_CORRUPT_POOL
:
2796 printf_color(ANSI_BOLD
, gettext("status: "));
2797 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2801 case ZPOOL_STATUS_VERSION_OLDER
:
2802 printf_color(ANSI_BOLD
, gettext("status: "));
2803 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2804 "a legacy on-disk version.\n"));
2807 case ZPOOL_STATUS_VERSION_NEWER
:
2808 printf_color(ANSI_BOLD
, gettext("status: "));
2809 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2810 "an incompatible version.\n"));
2813 case ZPOOL_STATUS_FEAT_DISABLED
:
2814 printf_color(ANSI_BOLD
, gettext("status: "));
2815 printf_color(ANSI_YELLOW
, gettext("Some supported "
2816 "features are not enabled on the pool.\n\t"
2817 "(Note that they may be intentionally disabled "
2818 "if the\n\t'compatibility' property is set.)\n"));
2821 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2822 printf_color(ANSI_BOLD
, gettext("status: "));
2823 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2824 "the file(s) indicated by the 'compatibility'\n"
2828 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
2829 printf_color(ANSI_BOLD
, gettext("status: "));
2830 printf_color(ANSI_YELLOW
, gettext("One or more features "
2831 "are enabled on the pool despite not being\n"
2832 "requested by the 'compatibility' property.\n"));
2835 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2836 printf_color(ANSI_BOLD
, gettext("status: "));
2837 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2838 "feature(s) not supported on this system:\n"));
2839 color_start(ANSI_YELLOW
);
2840 zpool_print_unsup_feat(config
);
2844 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2845 printf_color(ANSI_BOLD
, gettext("status: "));
2846 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2847 "accessed in read-only mode on this system. It\n\tcannot be"
2848 " accessed in read-write mode because it uses the "
2849 "following\n\tfeature(s) not supported on this system:\n"));
2850 color_start(ANSI_YELLOW
);
2851 zpool_print_unsup_feat(config
);
2855 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2856 printf_color(ANSI_BOLD
, gettext("status: "));
2857 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2858 "imported by another system.\n"));
2861 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2862 printf_color(ANSI_BOLD
, gettext("status: "));
2863 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2864 "multihost property on. It cannot\n\tbe safely imported "
2865 "when the system hostid is not set.\n"));
2868 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2869 printf_color(ANSI_BOLD
, gettext("status: "));
2870 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2871 "by another system.\n"));
2874 case ZPOOL_STATUS_FAULTED_DEV_R
:
2875 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2876 printf_color(ANSI_BOLD
, gettext("status: "));
2877 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2881 case ZPOOL_STATUS_BAD_LOG
:
2882 printf_color(ANSI_BOLD
, gettext("status: "));
2883 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2887 case ZPOOL_STATUS_RESILVERING
:
2888 case ZPOOL_STATUS_REBUILDING
:
2889 printf_color(ANSI_BOLD
, gettext("status: "));
2890 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2891 "being resilvered.\n"));
2894 case ZPOOL_STATUS_ERRATA
:
2895 printf_color(ANSI_BOLD
, gettext("status: "));
2896 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2900 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2901 printf_color(ANSI_BOLD
, gettext("status: "));
2902 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2903 "configured to use a non-native block size.\n"
2904 "\tExpect reduced performance.\n"));
2909 * No other status can be seen when importing pools.
2911 assert(reason
== ZPOOL_STATUS_OK
);
2915 * Print out an action according to the overall state of the pool.
2917 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2918 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2919 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2920 (void) printf(gettext(" action: The pool can be "
2921 "imported using its name or numeric identifier, "
2922 "though\n\tsome features will not be available "
2923 "without an explicit 'zpool upgrade'.\n"));
2924 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
2925 (void) printf(gettext(" action: The pool can be "
2926 "imported using its name or numeric\n\tidentifier, "
2927 "though the file(s) indicated by its "
2928 "'compatibility'\n\tproperty cannot be parsed at "
2930 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2931 (void) printf(gettext(" action: The pool can be "
2932 "imported using its name or numeric "
2933 "identifier and\n\tthe '-f' flag.\n"));
2934 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2936 case ZPOOL_ERRATA_NONE
:
2939 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2940 (void) printf(gettext(" action: The pool can "
2941 "be imported using its name or numeric "
2942 "identifier,\n\thowever there is a compat"
2943 "ibility issue which should be corrected"
2944 "\n\tby running 'zpool scrub'\n"));
2947 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2948 (void) printf(gettext(" action: The pool can"
2949 "not be imported with this version of ZFS "
2950 "due to\n\tan active asynchronous destroy. "
2951 "Revert to an earlier version\n\tand "
2952 "allow the destroy to complete before "
2956 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2957 (void) printf(gettext(" action: Existing "
2958 "encrypted datasets contain an on-disk "
2959 "incompatibility, which\n\tneeds to be "
2960 "corrected. Backup these datasets to new "
2961 "encrypted datasets\n\tand destroy the "
2965 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2966 (void) printf(gettext(" action: Existing "
2967 "encrypted snapshots and bookmarks contain "
2968 "an on-disk\n\tincompatibility. This may "
2969 "cause on-disk corruption if they are used"
2970 "\n\twith 'zfs recv'. To correct the "
2971 "issue, enable the bookmark_v2 feature.\n\t"
2972 "No additional action is needed if there "
2973 "are no encrypted snapshots or\n\t"
2974 "bookmarks. If preserving the encrypted "
2975 "snapshots and bookmarks is\n\trequired, "
2976 "use a non-raw send to backup and restore "
2977 "them. Alternately,\n\tthey may be removed"
2978 " to resolve the incompatibility.\n"));
2982 * All errata must contain an action message.
2987 (void) printf(gettext(" action: The pool can be "
2988 "imported using its name or numeric "
2991 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2992 (void) printf(gettext(" action: The pool can be imported "
2993 "despite missing or damaged devices. The\n\tfault "
2994 "tolerance of the pool may be compromised if imported.\n"));
2997 case ZPOOL_STATUS_VERSION_NEWER
:
2998 (void) printf(gettext(" action: The pool cannot be "
2999 "imported. Access the pool on a system running "
3000 "newer\n\tsoftware, or recreate the pool from "
3003 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
3004 printf_color(ANSI_BOLD
, gettext("action: "));
3005 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3006 "imported. Access the pool on a system that "
3007 "supports\n\tthe required feature(s), or recreate "
3008 "the pool from backup.\n"));
3010 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3011 printf_color(ANSI_BOLD
, gettext("action: "));
3012 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3013 "imported in read-write mode. Import the pool "
3015 "\t\"-o readonly=on\", access the pool on a system "
3016 "that supports the\n\trequired feature(s), or "
3017 "recreate the pool from backup.\n"));
3019 case ZPOOL_STATUS_MISSING_DEV_R
:
3020 case ZPOOL_STATUS_MISSING_DEV_NR
:
3021 case ZPOOL_STATUS_BAD_GUID_SUM
:
3022 (void) printf(gettext(" action: The pool cannot be "
3023 "imported. Attach the missing\n\tdevices and try "
3026 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3027 VERIFY0(nvlist_lookup_nvlist(config
,
3028 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
3030 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3031 hostname
= fnvlist_lookup_string(nvinfo
,
3032 ZPOOL_CONFIG_MMP_HOSTNAME
);
3034 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3035 hostid
= fnvlist_lookup_uint64(nvinfo
,
3036 ZPOOL_CONFIG_MMP_HOSTID
);
3038 (void) printf(gettext(" action: The pool must be "
3039 "exported from %s (hostid=%"PRIx64
")\n\tbefore it "
3040 "can be safely imported.\n"), hostname
, hostid
);
3042 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3043 (void) printf(gettext(" action: Set a unique system "
3044 "hostid with the zgenhostid(8) command.\n"));
3047 (void) printf(gettext(" action: The pool cannot be "
3048 "imported due to damaged devices or data.\n"));
3052 /* Print the comment attached to the pool. */
3053 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3054 (void) printf(gettext("comment: %s\n"), comment
);
3057 * If the state is "closed" or "can't open", and the aux state
3058 * is "corrupt data":
3060 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
3061 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
3062 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
3063 if (pool_state
== POOL_STATE_DESTROYED
)
3064 (void) printf(gettext("\tThe pool was destroyed, "
3065 "but can be imported using the '-Df' flags.\n"));
3066 else if (pool_state
!= POOL_STATE_EXPORTED
)
3067 (void) printf(gettext("\tThe pool may be active on "
3068 "another system, but can be imported using\n\t"
3069 "the '-f' flag.\n"));
3072 if (msgid
!= NULL
) {
3073 (void) printf(gettext(
3074 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3078 (void) printf(gettext(" config:\n\n"));
3080 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
3082 if (cb
.cb_namewidth
< 10)
3083 cb
.cb_namewidth
= 10;
3085 print_import_config(&cb
, name
, nvroot
, 0);
3087 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
3088 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
3089 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
3091 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
3092 (void) printf(gettext("\n\tAdditional devices are known to "
3093 "be part of this pool, though their\n\texact "
3094 "configuration cannot be determined.\n"));
3100 zfs_force_import_required(nvlist_t
*config
)
3103 uint64_t hostid
= 0;
3106 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
3107 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
3109 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3112 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3113 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3114 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3115 ZPOOL_CONFIG_MMP_STATE
);
3117 if (mmp_state
!= MMP_STATE_INACTIVE
)
3125 * Perform the import for the given configuration. This passes the heavy
3126 * lifting off to zpool_import_props(), and then mounts the datasets contained
3130 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3131 nvlist_t
*props
, int flags
)
3134 zpool_handle_t
*zhp
;
3138 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3139 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3141 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3142 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3143 "is formatted using an unsupported ZFS version\n"), name
);
3145 } else if (zfs_force_import_required(config
) &&
3146 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3147 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3150 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3151 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3152 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3153 ZPOOL_CONFIG_MMP_STATE
);
3155 if (mmp_state
== MMP_STATE_ACTIVE
) {
3156 const char *hostname
= "<unknown>";
3157 uint64_t hostid
= 0;
3159 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3160 hostname
= fnvlist_lookup_string(nvinfo
,
3161 ZPOOL_CONFIG_MMP_HOSTNAME
);
3163 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3164 hostid
= fnvlist_lookup_uint64(nvinfo
,
3165 ZPOOL_CONFIG_MMP_HOSTID
);
3167 (void) fprintf(stderr
, gettext("cannot import '%s': "
3168 "pool is imported on %s (hostid: "
3169 "0x%"PRIx64
")\nExport the pool on the other "
3170 "system, then run 'zpool import'.\n"),
3171 name
, hostname
, hostid
);
3172 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3173 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3174 "pool has the multihost property on and the\n"
3175 "system's hostid is not set. Set a unique hostid "
3176 "with the zgenhostid(8) command.\n"), name
);
3178 const char *hostname
= "<unknown>";
3179 time_t timestamp
= 0;
3180 uint64_t hostid
= 0;
3182 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3183 hostname
= fnvlist_lookup_string(config
,
3184 ZPOOL_CONFIG_HOSTNAME
);
3186 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3187 timestamp
= fnvlist_lookup_uint64(config
,
3188 ZPOOL_CONFIG_TIMESTAMP
);
3190 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3191 hostid
= fnvlist_lookup_uint64(config
,
3192 ZPOOL_CONFIG_HOSTID
);
3194 (void) fprintf(stderr
, gettext("cannot import '%s': "
3195 "pool was previously in use from another system.\n"
3196 "Last accessed by %s (hostid=%"PRIx64
") at %s"
3197 "The pool can be imported, use 'zpool import -f' "
3198 "to import the pool.\n"), name
, hostname
,
3199 hostid
, ctime(×tamp
));
3205 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3208 if (newname
!= NULL
)
3211 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3215 * Loading keys is best effort. We don't want to return immediately
3216 * if it fails but we do want to give the error to the caller.
3218 if (flags
& ZFS_IMPORT_LOAD_KEYS
&&
3219 zfs_crypto_attempt_load_keys(g_zfs
, name
) != 0)
3222 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3223 !(flags
& ZFS_IMPORT_ONLY
) &&
3224 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3234 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3235 char *orig_name
, char *new_name
,
3236 boolean_t do_destroyed
, boolean_t pool_specified
, boolean_t do_all
,
3237 importargs_t
*import
)
3239 nvlist_t
*config
= NULL
;
3240 nvlist_t
*found_config
= NULL
;
3241 uint64_t pool_state
;
3244 * At this point we have a list of import candidate configs. Even if
3245 * we were searching by pool name or guid, we still need to
3246 * post-process the list to deal with pool state and possible
3250 nvpair_t
*elem
= NULL
;
3251 boolean_t first
= B_TRUE
;
3252 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3254 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3256 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3258 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3260 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3263 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3264 import
->policy
) == 0);
3266 if (!pool_specified
) {
3270 (void) fputc('\n', stdout
);
3273 err
|= do_import(config
, NULL
, mntopts
,
3277 * If we're importing from cachefile, then
3278 * we don't want to report errors until we
3279 * are in the scan phase of the import. If
3280 * we get an error, then we return that error
3281 * to invoke the scan phase.
3283 if (import
->cachefile
&& !import
->scan
)
3284 err
= show_import(config
, B_FALSE
);
3286 (void) show_import(config
, B_TRUE
);
3288 } else if (import
->poolname
!= NULL
) {
3292 * We are searching for a pool based on name.
3294 verify(nvlist_lookup_string(config
,
3295 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3297 if (strcmp(name
, import
->poolname
) == 0) {
3298 if (found_config
!= NULL
) {
3299 (void) fprintf(stderr
, gettext(
3300 "cannot import '%s': more than "
3301 "one matching pool\n"),
3303 (void) fprintf(stderr
, gettext(
3304 "import by numeric ID instead\n"));
3307 found_config
= config
;
3313 * Search for a pool by guid.
3315 verify(nvlist_lookup_uint64(config
,
3316 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3318 if (guid
== import
->guid
)
3319 found_config
= config
;
3324 * If we were searching for a specific pool, verify that we found a
3325 * pool, and then do the import.
3327 if (pool_specified
&& err
== 0) {
3328 if (found_config
== NULL
) {
3329 (void) fprintf(stderr
, gettext("cannot import '%s': "
3330 "no such pool available\n"), orig_name
);
3333 err
|= do_import(found_config
, new_name
,
3334 mntopts
, props
, flags
);
3339 * If we were just looking for pools, report an error if none were
3342 if (!pool_specified
&& first
)
3343 (void) fprintf(stderr
,
3344 gettext("no pools available to import\n"));
3348 typedef struct target_exists_args
{
3349 const char *poolname
;
3351 } target_exists_args_t
;
3354 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3356 target_exists_args_t
*args
= data
;
3357 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3363 if (args
->poolname
!= NULL
) {
3364 const char *pool_name
;
3366 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3368 if (strcmp(pool_name
, args
->poolname
) == 0)
3373 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3375 if (pool_guid
== args
->poolguid
)
3383 * zpool checkpoint <pool>
3384 * checkpoint --discard <pool>
3386 * -d Discard the checkpoint from a checkpointed
3389 * -w Wait for discarding a checkpoint to complete.
3392 * Checkpoints the specified pool, by taking a "snapshot" of its
3393 * current state. A pool can only have one checkpoint at a time.
3396 zpool_do_checkpoint(int argc
, char **argv
)
3398 boolean_t discard
, wait
;
3400 zpool_handle_t
*zhp
;
3403 struct option long_options
[] = {
3404 {"discard", no_argument
, NULL
, 'd'},
3405 {"wait", no_argument
, NULL
, 'w'},
3411 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3420 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3426 if (wait
&& !discard
) {
3427 (void) fprintf(stderr
, gettext("--wait only valid when "
3428 "--discard also specified\n"));
3436 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3441 (void) fprintf(stderr
, gettext("too many arguments\n"));
3447 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3448 /* As a special case, check for use of '/' in the name */
3449 if (strchr(pool
, '/') != NULL
)
3450 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3451 "doesn't work on datasets. To save the state "
3452 "of a dataset from a specific point in time "
3453 "please use 'zfs snapshot'\n"));
3458 err
= (zpool_discard_checkpoint(zhp
) != 0);
3459 if (err
== 0 && wait
)
3460 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3462 err
= (zpool_checkpoint(zhp
) != 0);
3470 #define CHECKPOINT_OPT 1024
3473 * zpool import [-d dir] [-D]
3474 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3475 * [-d dir | -c cachefile | -s] [-f] -a
3476 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3477 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3480 * -c Read pool information from a cachefile instead of searching
3481 * devices. If importing from a cachefile config fails, then
3482 * fallback to searching for devices only in the directories that
3483 * exist in the cachefile.
3485 * -d Scan in a specific directory, other than /dev/. More than
3486 * one directory can be specified using multiple '-d' options.
3488 * -D Scan for previously destroyed pools or import all or only
3489 * specified destroyed pools.
3491 * -R Temporarily import the pool, with all mountpoints relative to
3492 * the given root. The pool will remain exported when the machine
3495 * -V Import even in the presence of faulted vdevs. This is an
3496 * intentionally undocumented option for testing purposes, and
3497 * treats the pool configuration as complete, leaving any bad
3498 * vdevs in the FAULTED state. In other words, it does verbatim
3501 * -f Force import, even if it appears that the pool is active.
3503 * -F Attempt rewind if necessary.
3505 * -n See if rewind would work, but don't actually rewind.
3507 * -N Import the pool but don't mount datasets.
3509 * -T Specify a starting txg to use for import. This option is
3510 * intentionally undocumented option for testing purposes.
3512 * -a Import all pools found.
3514 * -l Load encryption keys while importing.
3516 * -o Set property=value and/or temporary mount options (without '=').
3518 * -s Scan using the default search path, the libblkid cache will
3521 * --rewind-to-checkpoint
3522 * Import the pool and revert back to the checkpoint.
3524 * The import command scans for pools to import, and import pools based on pool
3525 * name and GUID. The pool can also be renamed as part of the import process.
3528 zpool_do_import(int argc
, char **argv
)
3530 char **searchdirs
= NULL
;
3531 char *env
, *envdup
= NULL
;
3535 nvlist_t
*pools
= NULL
;
3536 boolean_t do_all
= B_FALSE
;
3537 boolean_t do_destroyed
= B_FALSE
;
3538 char *mntopts
= NULL
;
3539 uint64_t searchguid
= 0;
3540 char *searchname
= NULL
;
3542 nvlist_t
*policy
= NULL
;
3543 nvlist_t
*props
= NULL
;
3544 int flags
= ZFS_IMPORT_NORMAL
;
3545 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3546 boolean_t dryrun
= B_FALSE
;
3547 boolean_t do_rewind
= B_FALSE
;
3548 boolean_t xtreme_rewind
= B_FALSE
;
3549 boolean_t do_scan
= B_FALSE
;
3550 boolean_t pool_exists
= B_FALSE
;
3551 boolean_t pool_specified
= B_FALSE
;
3552 uint64_t txg
= -1ULL;
3553 char *cachefile
= NULL
;
3554 importargs_t idata
= { 0 };
3557 struct option long_options
[] = {
3558 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3563 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3564 long_options
, NULL
)) != -1) {
3573 searchdirs
= safe_realloc(searchdirs
,
3574 (nsearch
+ 1) * sizeof (char *));
3575 searchdirs
[nsearch
++] = optarg
;
3578 do_destroyed
= B_TRUE
;
3581 flags
|= ZFS_IMPORT_ANY_HOST
;
3587 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3590 flags
|= ZFS_IMPORT_MISSING_LOG
;
3596 flags
|= ZFS_IMPORT_ONLY
;
3599 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3602 if (add_prop_list(optarg
, propval
,
3610 if (add_prop_list(zpool_prop_to_name(
3611 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3613 if (add_prop_list_default(zpool_prop_to_name(
3614 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3621 flags
|= ZFS_IMPORT_TEMP_NAME
;
3622 if (add_prop_list_default(zpool_prop_to_name(
3623 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3629 txg
= strtoull(optarg
, &endptr
, 0);
3630 if (errno
!= 0 || *endptr
!= '\0') {
3631 (void) fprintf(stderr
,
3632 gettext("invalid txg value\n"));
3635 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3638 flags
|= ZFS_IMPORT_VERBATIM
;
3641 xtreme_rewind
= B_TRUE
;
3643 case CHECKPOINT_OPT
:
3644 flags
|= ZFS_IMPORT_CHECKPOINT
;
3647 (void) fprintf(stderr
, gettext("missing argument for "
3648 "'%c' option\n"), optopt
);
3652 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3661 if (cachefile
&& nsearch
!= 0) {
3662 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3666 if (cachefile
&& do_scan
) {
3667 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
3671 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3672 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3676 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3677 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3682 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3683 (void) fprintf(stderr
,
3684 gettext("-n or -X only meaningful with -F\n"));
3688 rewind_policy
= ZPOOL_TRY_REWIND
;
3690 rewind_policy
= ZPOOL_DO_REWIND
;
3692 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3694 /* In the future, we can capture further policy and include it here */
3695 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3696 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3697 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3698 rewind_policy
) != 0)
3701 /* check argument count */
3704 (void) fprintf(stderr
, gettext("too many arguments\n"));
3709 (void) fprintf(stderr
, gettext("too many arguments\n"));
3715 * Check for the effective uid. We do this explicitly here because
3716 * otherwise any attempt to discover pools will silently fail.
3718 if (argc
== 0 && geteuid() != 0) {
3719 (void) fprintf(stderr
, gettext("cannot "
3720 "discover pools: permission denied\n"));
3724 nvlist_free(policy
);
3729 * Depending on the arguments given, we do one of the following:
3731 * <none> Iterate through all pools and display information about
3734 * -a Iterate through all pools and try to import each one.
3736 * <id> Find the pool that corresponds to the given GUID/pool
3737 * name and import that one.
3739 * -D Above options applies only to destroyed pools.
3745 searchguid
= strtoull(argv
[0], &endptr
, 10);
3746 if (errno
!= 0 || *endptr
!= '\0') {
3747 searchname
= argv
[0];
3750 pool_specified
= B_TRUE
;
3753 * User specified a name or guid. Ensure it's unique.
3755 target_exists_args_t search
= {searchname
, searchguid
};
3756 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3760 * Check the environment for the preferred search path.
3762 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3763 char *dir
, *tmp
= NULL
;
3765 envdup
= strdup(env
);
3767 for (dir
= strtok_r(envdup
, ":", &tmp
);
3769 dir
= strtok_r(NULL
, ":", &tmp
)) {
3770 searchdirs
= safe_realloc(searchdirs
,
3771 (nsearch
+ 1) * sizeof (char *));
3772 searchdirs
[nsearch
++] = dir
;
3776 idata
.path
= searchdirs
;
3777 idata
.paths
= nsearch
;
3778 idata
.poolname
= searchname
;
3779 idata
.guid
= searchguid
;
3780 idata
.cachefile
= cachefile
;
3781 idata
.scan
= do_scan
;
3782 idata
.policy
= policy
;
3784 libpc_handle_t lpch
= {
3785 .lpc_lib_handle
= g_zfs
,
3786 .lpc_ops
= &libzfs_config_ops
,
3787 .lpc_printerr
= B_TRUE
3789 pools
= zpool_search_import(&lpch
, &idata
);
3791 if (pools
!= NULL
&& pool_exists
&&
3792 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3793 (void) fprintf(stderr
, gettext("cannot import '%s': "
3794 "a pool with that name already exists\n"),
3796 (void) fprintf(stderr
, gettext("use the form '%s "
3797 "<pool | id> <newpool>' to give it a new name\n"),
3800 } else if (pools
== NULL
&& pool_exists
) {
3801 (void) fprintf(stderr
, gettext("cannot import '%s': "
3802 "a pool with that name is already created/imported,\n"),
3804 (void) fprintf(stderr
, gettext("and no additional pools "
3805 "with that name were found\n"));
3807 } else if (pools
== NULL
) {
3809 (void) fprintf(stderr
, gettext("cannot import '%s': "
3810 "no such pool available\n"), argv
[0]);
3818 nvlist_free(policy
);
3824 err
= import_pools(pools
, props
, mntopts
, flags
,
3825 argc
>= 1 ? argv
[0] : NULL
,
3826 argc
>= 2 ? argv
[1] : NULL
,
3827 do_destroyed
, pool_specified
, do_all
, &idata
);
3830 * If we're using the cachefile and we failed to import, then
3831 * fallback to scanning the directory for pools that match
3832 * those in the cachefile.
3834 if (err
!= 0 && cachefile
!= NULL
) {
3835 (void) printf(gettext("cachefile import failed, retrying\n"));
3838 * We use the scan flag to gather the directories that exist
3839 * in the cachefile. If we need to fallback to searching for
3840 * the pool config, we will only search devices in these
3843 idata
.scan
= B_TRUE
;
3845 pools
= zpool_search_import(&lpch
, &idata
);
3847 err
= import_pools(pools
, props
, mntopts
, flags
,
3848 argc
>= 1 ? argv
[0] : NULL
,
3849 argc
>= 2 ? argv
[1] : NULL
,
3850 do_destroyed
, pool_specified
, do_all
, &idata
);
3856 nvlist_free(policy
);
3860 return (err
? 1 : 0);
3864 * zpool sync [-f] [pool] ...
3866 * -f (undocumented) force uberblock (and config including zpool cache file)
3869 * Sync the specified pool(s).
3870 * Without arguments "zpool sync" will sync all pools.
3871 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3875 zpool_do_sync(int argc
, char **argv
)
3878 boolean_t force
= B_FALSE
;
3881 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3887 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3896 /* if argc == 0 we will execute zpool_sync_one on all pools */
3897 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
3898 B_FALSE
, zpool_sync_one
, &force
);
3903 typedef struct iostat_cbdata
{
3907 boolean_t cb_verbose
;
3908 boolean_t cb_literal
;
3909 boolean_t cb_scripted
;
3910 zpool_list_t
*cb_list
;
3911 vdev_cmd_data_list_t
*vcdl
;
3912 vdev_cbdata_t cb_vdevs
;
3916 typedef struct name_and_columns
{
3917 const char *name
; /* Column name */
3918 unsigned int columns
; /* Center name to this number of columns */
3919 } name_and_columns_t
;
3921 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
3923 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3925 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3927 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3928 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3930 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3931 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3932 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL
}},
3933 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3934 {"asyncq_wait", 2}, {NULL
}},
3935 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3936 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3937 {"trim", 2}, {"rebuild", 2}, {NULL
}},
3940 /* Shorthand - if "columns" field not set, default to 1 column */
3941 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3943 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3945 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3946 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3948 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3949 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3950 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
3951 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3952 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3954 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3955 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3956 {"ind"}, {"agg"}, {NULL
}},
3959 static const char *histo_to_title
[] = {
3960 [IOS_L_HISTO
] = "latency",
3961 [IOS_RQ_HISTO
] = "req_size",
3965 * Return the number of labels in a null-terminated name_and_columns_t
3970 label_array_len(const name_and_columns_t
*labels
)
3974 while (labels
[i
].name
)
3981 * Return the number of strings in a null-terminated string array.
3984 * const char foo[] = {"bar", "baz", NULL}
3989 str_array_len(const char *array
[])
4000 * Return a default column width for default/latency/queue columns. This does
4001 * not include histograms, which have their columns autosized.
4004 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
4006 unsigned long column_width
= 5; /* Normal niceprint */
4007 static unsigned long widths
[] = {
4009 * Choose some sane default column sizes for printing the
4012 [IOS_DEFAULT
] = 15, /* 1PB capacity */
4013 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
4014 [IOS_QUEUES
] = 6, /* 1M queue entries */
4015 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
4016 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
4020 column_width
= widths
[type
];
4022 return (column_width
);
4026 * Print the column labels, i.e:
4028 * capacity operations bandwidth
4029 * alloc free read write read write ...
4031 * If force_column_width is set, use it for the column width. If not set, use
4032 * the default column width.
4035 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4036 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
4039 int text_start
, rw_column_width
, spaces_to_end
;
4040 uint64_t flags
= cb
->cb_flags
;
4042 unsigned int column_width
= force_column_width
;
4044 /* For each bit set in flags */
4045 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4046 idx
= lowbit64(f
) - 1;
4047 if (!force_column_width
)
4048 column_width
= default_column_width(cb
, idx
);
4049 /* Print our top labels centered over "read write" label. */
4050 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
4051 const char *name
= labels
[idx
][i
].name
;
4053 * We treat labels[][].columns == 0 as shorthand
4054 * for one column. It makes writing out the label
4055 * tables more concise.
4057 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
4058 unsigned int slen
= strlen(name
);
4060 rw_column_width
= (column_width
* columns
) +
4061 (2 * (columns
- 1));
4063 text_start
= (int)((rw_column_width
) / columns
-
4068 printf(" "); /* Two spaces between columns */
4070 /* Space from beginning of column to label */
4071 for (s
= 0; s
< text_start
; s
++)
4076 /* Print space after label to end of column */
4077 spaces_to_end
= rw_column_width
- text_start
- slen
;
4078 if (spaces_to_end
< 0)
4081 for (s
= 0; s
< spaces_to_end
; s
++)
4089 * print_cmd_columns - Print custom column titles from -c
4091 * If the user specified the "zpool status|iostat -c" then print their custom
4092 * column titles in the header. For example, print_cmd_columns() would print
4093 * the " col1 col2" part of this:
4095 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4097 * capacity operations bandwidth
4098 * pool alloc free read write read write col1 col2
4099 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4100 * mypool 269K 1008M 0 0 107 946
4101 * mirror 269K 1008M 0 0 107 946
4102 * sdb - - 0 0 102 473 val1 val2
4103 * sdc - - 0 0 5 473 val1 val2
4104 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4107 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4110 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4112 if (vcdl
->count
== 0 || data
== NULL
)
4116 * Each vdev cmd should have the same column names unless the user did
4117 * something weird with their cmd. Just take the column names from the
4118 * first vdev and assume it works for all of them.
4120 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4123 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4126 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4127 vcdl
->uniq_cols
[i
]);
4134 * Utility function to print out a line of dashes like:
4136 * -------------------------------- ----- ----- ----- ----- -----
4138 * ...or a dashed named-row line like:
4144 * @force_column_width If non-zero, use the value as the column width.
4145 * Otherwise use the default column widths.
4147 * @name: Print a dashed named-row line starting
4148 * with @name. Otherwise, print a regular
4152 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4156 unsigned int namewidth
;
4157 uint64_t flags
= cb
->cb_flags
;
4160 const name_and_columns_t
*labels
;
4164 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4165 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4166 } else if (cb
->cb_vdevs
.cb_names_count
) {
4172 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4173 name
? strlen(name
) : 0);
4177 printf("%-*s", namewidth
, name
);
4179 for (i
= 0; i
< namewidth
; i
++)
4183 /* For each bit in flags */
4184 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4185 unsigned int column_width
;
4186 idx
= lowbit64(f
) - 1;
4187 if (force_column_width
)
4188 column_width
= force_column_width
;
4190 column_width
= default_column_width(cb
, idx
);
4192 labels
= iostat_bottom_labels
[idx
];
4193 for (i
= 0; i
< label_array_len(labels
); i
++) {
4195 printf(" %*s-", column_width
- 1, " ");
4197 printf(" %.*s", column_width
,
4198 "--------------------");
4205 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4206 unsigned int force_column_width
)
4208 print_iostat_dashes(cb
, force_column_width
, NULL
);
4212 print_iostat_separator(iostat_cbdata_t
*cb
)
4214 print_iostat_separator_impl(cb
, 0);
4218 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4219 const char *histo_vdev_name
)
4221 unsigned int namewidth
;
4224 color_start(ANSI_BOLD
);
4226 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4227 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4228 } else if (cb
->cb_vdevs
.cb_names_count
) {
4234 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4235 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4237 if (histo_vdev_name
)
4238 printf("%-*s", namewidth
, histo_vdev_name
);
4240 printf("%*s", namewidth
, "");
4243 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4246 printf("%-*s", namewidth
, title
);
4248 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4249 if (cb
->vcdl
!= NULL
)
4250 print_cmd_columns(cb
->vcdl
, 0);
4254 print_iostat_separator_impl(cb
, force_column_width
);
4256 if (cb
->vcdl
!= NULL
)
4257 print_cmd_columns(cb
->vcdl
, 1);
4265 print_iostat_header(iostat_cbdata_t
*cb
)
4267 print_iostat_header_impl(cb
, 0, NULL
);
4271 * Prints a size string (i.e. 120M) with the suffix ("M") colored
4272 * by order of magnitude. Uses column_size to add padding.
4275 print_stat_color(char *statbuf
, unsigned int column_size
)
4278 if (*statbuf
== '0') {
4279 color_start(ANSI_GRAY
);
4283 for (; *statbuf
; statbuf
++) {
4284 if (*statbuf
== 'K') color_start(ANSI_GREEN
);
4285 else if (*statbuf
== 'M') color_start(ANSI_YELLOW
);
4286 else if (*statbuf
== 'G') color_start(ANSI_RED
);
4287 else if (*statbuf
== 'T') color_start(ANSI_BOLD_BLUE
);
4288 else if (*statbuf
== 'P') color_start(ANSI_MAGENTA
);
4289 else if (*statbuf
== 'E') color_start(ANSI_CYAN
);
4290 fputc(*statbuf
, stdout
);
4291 if (--column_size
<= 0)
4296 for (; column_size
> 0; column_size
--)
4301 * Display a single statistic.
4304 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4305 unsigned int column_size
, boolean_t scripted
)
4309 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4312 printf("\t%s", buf
);
4314 print_stat_color(buf
, column_size
);
4318 * Calculate the default vdev stats
4320 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4321 * stats into calcvs.
4324 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4325 vdev_stat_t
*calcvs
)
4329 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4330 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4331 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4333 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4334 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4338 * Internal representation of the extended iostats data.
4340 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4341 * or single uint64_t's. We make both look like arrays to make them easier
4342 * to process. In order to make single uint64_t's look like arrays, we set
4343 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4344 * we can just use *data and count.
4348 uint_t count
; /* Number of entries in data[] */
4349 uint64_t __data
; /* Only used when data is a single uint64_t */
4353 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4357 for (i
= 0; i
< len
; i
++)
4358 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4364 * Helper function to lookup a uint64_t array or uint64_t value and store its
4365 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4366 * it look like a one element array to make it easier to process.
4369 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4370 struct stat_array
*nva
)
4375 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4376 switch (nvpair_type(tmp
)) {
4377 case DATA_TYPE_UINT64_ARRAY
:
4378 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4380 case DATA_TYPE_UINT64
:
4381 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4382 nva
->data
= &nva
->__data
;
4386 /* Not a uint64_t */
4395 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4396 * subtract them, and return the results in a newly allocated stat_array.
4397 * You must free the returned array after you are done with it with
4398 * free_calc_stats().
4400 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4403 static struct stat_array
*
4404 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4407 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4408 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4410 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4412 /* Extract our extended stats nvlist from the main list */
4413 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4416 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4420 newnva
= safe_malloc(alloc_size
);
4421 oldnva
= safe_malloc(alloc_size
);
4422 calcnva
= safe_malloc(alloc_size
);
4424 for (j
= 0; j
< len
; j
++) {
4425 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4427 calcnva
[j
].count
= newnva
[j
].count
;
4428 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4429 calcnva
[j
].data
= safe_malloc(alloc_size
);
4430 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4433 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4435 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4436 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4445 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4448 for (i
= 0; i
< len
; i
++)
4455 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4456 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4462 enum zfs_nicenum_format format
;
4463 unsigned int buckets
;
4464 unsigned int start_bucket
;
4467 format
= ZFS_NICENUM_RAW
;
4469 format
= ZFS_NICENUM_1024
;
4471 /* All these histos are the same size, so just use nva[0].count */
4472 buckets
= nva
[0].count
;
4474 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4475 /* Start at 512 - req size should never be lower than this */
4481 for (j
= start_bucket
; j
< buckets
; j
++) {
4482 /* Print histogram bucket label */
4483 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4484 /* Ending range of this bucket */
4485 val
= (1UL << (j
+ 1)) - 1;
4486 zfs_nicetime(val
, buf
, sizeof (buf
));
4488 /* Request size (starting range of bucket) */
4490 zfs_nicenum(val
, buf
, sizeof (buf
));
4493 if (cb
->cb_scripted
)
4494 printf("%llu", (u_longlong_t
)val
);
4496 printf("%-*s", namewidth
, buf
);
4498 /* Print the values on the line */
4499 for (i
= 0; i
< len
; i
++) {
4500 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4501 column_width
, cb
->cb_scripted
);
4508 print_solid_separator(unsigned int length
)
4516 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4517 nvlist_t
*newnv
, double scale
, const char *name
)
4519 unsigned int column_width
;
4520 unsigned int namewidth
;
4521 unsigned int entire_width
;
4522 enum iostat_type type
;
4523 struct stat_array
*nva
;
4525 unsigned int names_len
;
4527 /* What type of histo are we? */
4528 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4530 /* Get NULL-terminated array of nvlist names for our histo */
4531 names
= vsx_type_to_nvlist
[type
];
4532 names_len
= str_array_len(names
); /* num of names */
4534 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4536 if (cb
->cb_literal
) {
4537 column_width
= MAX(5,
4538 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4543 namewidth
= MAX(cb
->cb_namewidth
,
4544 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4547 * Calculate the entire line width of what we're printing. The
4548 * +2 is for the two spaces between columns:
4552 /* |___| <---------- column_width */
4554 /* |__________| <--- entire_width */
4556 entire_width
= namewidth
+ (column_width
+ 2) *
4557 label_array_len(iostat_bottom_labels
[type
]);
4559 if (cb
->cb_scripted
)
4560 printf("%s\n", name
);
4562 print_iostat_header_impl(cb
, column_width
, name
);
4564 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4567 free_calc_stats(nva
, names_len
);
4568 if (!cb
->cb_scripted
)
4569 print_solid_separator(entire_width
);
4573 * Calculate the average latency of a power-of-two latency histogram
4576 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4579 uint64_t count
= 0, total
= 0;
4581 for (i
= 0; i
< buckets
; i
++) {
4583 * Our buckets are power-of-two latency ranges. Use the
4584 * midpoint latency of each bucket to calculate the average.
4592 if (histo
[i
] != 0) {
4593 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4598 /* Prevent divide by zero */
4599 return (count
== 0 ? 0 : total
/ count
);
4603 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*newnv
)
4605 const char *names
[] = {
4606 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4607 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4608 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4609 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4610 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4611 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4612 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4613 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4614 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4615 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4616 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4617 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4618 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE
,
4619 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
4622 struct stat_array
*nva
;
4624 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4625 enum zfs_nicenum_format format
;
4627 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4630 format
= ZFS_NICENUM_RAW
;
4632 format
= ZFS_NICENUM_1024
;
4634 for (int i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4635 uint64_t val
= nva
[i
].data
[0];
4636 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4639 free_calc_stats(nva
, ARRAY_SIZE(names
));
4643 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4648 const char *names
[] = {
4649 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4650 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4651 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4652 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4653 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4654 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4655 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4656 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4657 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4658 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4659 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
4661 struct stat_array
*nva
;
4663 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4664 enum zfs_nicenum_format format
;
4666 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4669 format
= ZFS_NICENUM_RAWTIME
;
4671 format
= ZFS_NICENUM_TIME
;
4673 /* Print our avg latencies on the line */
4674 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4675 /* Compute average latency for a latency histo */
4676 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4677 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4679 free_calc_stats(nva
, ARRAY_SIZE(names
));
4683 * Print default statistics (capacity/operations/bandwidth)
4686 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4688 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4689 enum zfs_nicenum_format format
;
4690 char na
; /* char to print for "not applicable" values */
4692 if (cb
->cb_literal
) {
4693 format
= ZFS_NICENUM_RAW
;
4696 format
= ZFS_NICENUM_1024
;
4700 /* only toplevel vdevs have capacity stats */
4701 if (vs
->vs_space
== 0) {
4702 if (cb
->cb_scripted
)
4703 printf("\t%c\t%c", na
, na
);
4705 printf(" %*c %*c", column_width
, na
, column_width
,
4708 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4710 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4711 column_width
, cb
->cb_scripted
);
4714 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4715 format
, column_width
, cb
->cb_scripted
);
4716 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4717 format
, column_width
, cb
->cb_scripted
);
4718 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4719 format
, column_width
, cb
->cb_scripted
);
4720 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4721 format
, column_width
, cb
->cb_scripted
);
4724 static const char *const class_name
[] = {
4725 VDEV_ALLOC_BIAS_DEDUP
,
4726 VDEV_ALLOC_BIAS_SPECIAL
,
4727 VDEV_ALLOC_CLASS_LOGS
4731 * Print out all the statistics for the given vdev. This can either be the
4732 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4733 * is a verbose output, and we don't want to display the toplevel pool stats.
4735 * Returns the number of stat lines printed.
4738 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4739 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4741 nvlist_t
**oldchild
, **newchild
;
4742 uint_t c
, children
, oldchildren
;
4743 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4744 vdev_stat_t zerovs
= { 0 };
4751 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4754 calcvs
= safe_malloc(sizeof (*calcvs
));
4756 if (oldnv
!= NULL
) {
4757 verify(nvlist_lookup_uint64_array(oldnv
,
4758 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4763 /* Do we only want to see a specific vdev? */
4764 for (i
= 0; i
< cb
->cb_vdevs
.cb_names_count
; i
++) {
4765 /* Yes we do. Is this the vdev? */
4766 if (strcmp(name
, cb
->cb_vdevs
.cb_names
[i
]) == 0) {
4768 * This is our vdev. Since it is the only vdev we
4769 * will be displaying, make depth = 0 so that it
4770 * doesn't get indented.
4777 if (cb
->cb_vdevs
.cb_names_count
&& (i
== cb
->cb_vdevs
.cb_names_count
)) {
4778 /* Couldn't match the name */
4783 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4784 (uint64_t **)&newvs
, &c
) == 0);
4787 * Print the vdev name unless it's is a histogram. Histograms
4788 * display the vdev name in the header itself.
4790 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4791 if (cb
->cb_scripted
) {
4794 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4795 (void) printf("%*s%s", depth
, "", name
);
4797 (void) printf("%*s%s%*s", depth
, "", name
,
4798 (int)(cb
->cb_namewidth
- strlen(name
) -
4803 /* Calculate our scaling factor */
4804 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4805 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4807 * If we specify printing histograms with no time interval, then
4808 * print the histogram numbers over the entire lifetime of the
4816 scale
= (double)NANOSEC
/ tdelta
;
4819 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4820 calc_default_iostats(oldvs
, newvs
, calcvs
);
4821 print_iostat_default(calcvs
, cb
, scale
);
4823 if (cb
->cb_flags
& IOS_LATENCY_M
)
4824 print_iostat_latency(cb
, oldnv
, newnv
);
4825 if (cb
->cb_flags
& IOS_QUEUES_M
)
4826 print_iostat_queues(cb
, newnv
);
4827 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4829 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4832 if (cb
->vcdl
!= NULL
) {
4834 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4837 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4841 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4850 if (!cb
->cb_verbose
)
4853 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4854 &newchild
, &children
) != 0)
4858 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4859 &oldchild
, &oldchildren
) != 0)
4862 children
= MIN(oldchildren
, children
);
4866 * print normal top-level devices
4868 for (c
= 0; c
< children
; c
++) {
4869 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4871 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4874 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4877 if (ishole
|| islog
)
4880 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4883 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4884 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
4885 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4886 newchild
[c
], cb
, depth
+ 2);
4891 * print all other top-level devices
4893 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
4894 boolean_t printed
= B_FALSE
;
4896 for (c
= 0; c
< children
; c
++) {
4897 uint64_t islog
= B_FALSE
;
4898 const char *bias
= NULL
;
4899 const char *type
= NULL
;
4901 (void) nvlist_lookup_uint64(newchild
[c
],
4902 ZPOOL_CONFIG_IS_LOG
, &islog
);
4904 bias
= VDEV_ALLOC_CLASS_LOGS
;
4906 (void) nvlist_lookup_string(newchild
[c
],
4907 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4908 (void) nvlist_lookup_string(newchild
[c
],
4909 ZPOOL_CONFIG_TYPE
, &type
);
4911 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4913 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4917 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4919 !cb
->cb_vdevs
.cb_names
) {
4920 print_iostat_dashes(cb
, 0,
4927 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4928 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
4929 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4930 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4936 * Include level 2 ARC devices in iostat output
4938 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4939 &newchild
, &children
) != 0)
4943 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4944 &oldchild
, &oldchildren
) != 0)
4947 children
= MIN(oldchildren
, children
);
4951 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4952 !cb
->cb_vdevs
.cb_names
) {
4953 print_iostat_dashes(cb
, 0, "cache");
4957 for (c
= 0; c
< children
; c
++) {
4958 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4959 cb
->cb_vdevs
.cb_name_flags
);
4960 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4961 : NULL
, newchild
[c
], cb
, depth
+ 2);
4970 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4972 iostat_cbdata_t
*cb
= data
;
4976 * If the pool has disappeared, remove it from the list and continue.
4978 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4982 pool_list_remove(cb
->cb_list
, zhp
);
4988 * Callback to print out the iostats for the given pool.
4991 print_iostat(zpool_handle_t
*zhp
, void *data
)
4993 iostat_cbdata_t
*cb
= data
;
4994 nvlist_t
*oldconfig
, *newconfig
;
4995 nvlist_t
*oldnvroot
, *newnvroot
;
4998 newconfig
= zpool_get_config(zhp
, &oldconfig
);
5000 if (cb
->cb_iteration
== 1)
5003 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
5006 if (oldconfig
== NULL
)
5009 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
5012 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
5014 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
5015 !cb
->cb_scripted
&& cb
->cb_verbose
&&
5016 !cb
->cb_vdevs
.cb_names_count
) {
5017 print_iostat_separator(cb
);
5018 if (cb
->vcdl
!= NULL
) {
5019 print_cmd_columns(cb
->vcdl
, 1);
5034 if (isatty(STDOUT_FILENO
)) {
5035 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
5037 columns
= ws
.ws_col
;
5046 * Return the required length of the pool/vdev name column. The minimum
5047 * allowed width and output formatting flags must be provided.
5050 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
5052 nvlist_t
*config
, *nvroot
;
5053 int width
= min_width
;
5055 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
5056 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5058 size_t poolname_len
= strlen(zpool_get_name(zhp
));
5059 if (verbose
== B_FALSE
) {
5060 width
= MAX(poolname_len
, min_width
);
5062 width
= MAX(poolname_len
,
5063 max_width(zhp
, nvroot
, 0, min_width
, flags
));
5071 * Parse the input string, get the 'interval' and 'count' value if there is one.
5074 get_interval_count(int *argcp
, char **argv
, float *iv
,
5078 unsigned long count
= 0;
5082 * Determine if the last argument is an integer or a pool name
5084 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5088 interval
= strtof(argv
[argc
- 1], &end
);
5090 if (*end
== '\0' && errno
== 0) {
5091 if (interval
== 0) {
5092 (void) fprintf(stderr
, gettext(
5093 "interval cannot be zero\n"));
5097 * Ignore the last parameter
5102 * If this is not a valid number, just plow on. The
5103 * user will get a more informative error message later
5111 * If the last argument is also an integer, then we have both a count
5114 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5119 interval
= strtof(argv
[argc
- 1], &end
);
5121 if (*end
== '\0' && errno
== 0) {
5122 if (interval
== 0) {
5123 (void) fprintf(stderr
, gettext(
5124 "interval cannot be zero\n"));
5129 * Ignore the last parameter
5143 get_timestamp_arg(char c
)
5146 timestamp_fmt
= UDATE
;
5148 timestamp_fmt
= DDATE
;
5154 * Return stat flags that are supported by all pools by both the module and
5155 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5156 * It will get ANDed down until only the flags that are supported on all pools
5160 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5162 uint64_t *mask
= data
;
5163 nvlist_t
*config
, *nvroot
, *nvx
;
5167 config
= zpool_get_config(zhp
, NULL
);
5168 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5171 /* Default stats are always supported, but for completeness.. */
5172 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5173 flags
|= IOS_DEFAULT_M
;
5175 /* Get our extended stats nvlist from the main list */
5176 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5179 * No extended stats; they're probably running an older
5180 * module. No big deal, we support that too.
5185 /* For each extended stat, make sure all its nvpairs are supported */
5186 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5187 if (!vsx_type_to_nvlist
[j
][0])
5190 /* Start off by assuming the flag is supported, then check */
5191 flags
|= (1ULL << j
);
5192 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5193 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5194 /* flag isn't supported */
5195 flags
= flags
& ~(1ULL << j
);
5201 *mask
= *mask
& flags
;
5206 * Return a bitmask of stats that are supported on all pools by both the module
5210 get_stat_flags(zpool_list_t
*list
)
5215 * get_stat_flags_cb() will lop off bits from "mask" until only the
5216 * flags that are supported on all pools remain.
5218 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5223 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5226 is_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *cb_data
)
5229 vdev_cbdata_t
*cb
= cb_data
;
5230 zpool_handle_t
*zhp
= zhp_data
;
5232 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
5235 return (guid
== zpool_vdev_path_to_guid(zhp
, cb
->cb_names
[0]));
5239 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5242 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5244 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5248 * Check if vdevs are in a pool
5250 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5251 * return 0. If pool_name is NULL, then search all pools.
5254 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5262 if ((argc
== 0) || !*argv
)
5268 /* Temporarily hijack cb_names for a second... */
5269 tmp_name
= cb
->cb_names
;
5271 /* Go though our list of prospective vdev names */
5272 for (i
= 0; i
< argc
; i
++) {
5273 cb
->cb_names
= argv
+ i
;
5275 /* Is this name a vdev in our pools? */
5276 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5277 ZFS_TYPE_POOL
, B_FALSE
, is_vdev
, cb
);
5284 cb
->cb_names
= tmp_name
;
5290 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5293 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5300 * Do we have a pool named *name? If so, return 1, otherwise 0.
5305 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, ZFS_TYPE_POOL
, B_FALSE
,
5309 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5311 are_all_pools(int argc
, char **argv
)
5313 if ((argc
== 0) || !*argv
)
5317 if (!is_pool(argv
[argc
]))
5324 * Helper function to print out vdev/pool names we can't resolve. Used for an
5328 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5334 for (i
= 0; i
< argc
; i
++) {
5338 str
= gettext("pool");
5339 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5340 str
= gettext("vdev in this pool");
5341 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5342 str
= gettext("vdev in another pool");
5344 str
= gettext("unknown");
5346 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5351 * Same as get_interval_count(), but with additional checks to not misinterpret
5352 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5353 * cb.cb_vdevs.cb_name_flags.
5356 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5357 unsigned long *count
, iostat_cbdata_t
*cb
)
5359 char **tmpargv
= argv
;
5360 int argc_for_interval
= 0;
5362 /* Is the last arg an interval value? Or a guid? */
5363 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
,
5366 * The last arg is not a guid, so it's probably an
5369 argc_for_interval
++;
5372 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
,
5375 * The 2nd to last arg is not a guid, so it's probably
5376 * an interval value.
5378 argc_for_interval
++;
5382 /* Point to our list of possible intervals */
5383 tmpargv
= &argv
[*argc
- argc_for_interval
];
5385 *argc
= *argc
- argc_for_interval
;
5386 get_interval_count(&argc_for_interval
, tmpargv
,
5391 * Floating point sleep(). Allows you to pass in a floating point value for
5397 struct timespec req
;
5398 req
.tv_sec
= floor(sec
);
5399 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5400 nanosleep(&req
, NULL
);
5404 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5405 * if we were unable to determine its size.
5408 terminal_height(void)
5412 if (isatty(STDOUT_FILENO
) == 0)
5415 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5416 return (win
.ws_row
);
5422 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5425 * name: Short name of the script ('iostat').
5426 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5429 print_zpool_script_help(char *name
, char *path
)
5431 char *argv
[] = {path
, (char *)"-h", NULL
};
5432 char **lines
= NULL
;
5436 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5438 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5440 libzfs_free_str_array(lines
, lines_cnt
);
5444 for (int i
= 0; i
< lines_cnt
; i
++)
5445 if (!is_blank_str(lines
[i
]))
5446 printf(" %-14s %s\n", name
, lines
[i
]);
5448 libzfs_free_str_array(lines
, lines_cnt
);
5452 * Go though the zpool status/iostat -c scripts in the user's path, run their
5453 * help option (-h), and print out the results.
5456 print_zpool_dir_scripts(char *dirpath
)
5460 char fullpath
[MAXPATHLEN
];
5461 struct stat dir_stat
;
5463 if ((dir
= opendir(dirpath
)) != NULL
) {
5464 /* print all the files and directories within directory */
5465 while ((ent
= readdir(dir
)) != NULL
) {
5466 if (snprintf(fullpath
, sizeof (fullpath
), "%s/%s",
5467 dirpath
, ent
->d_name
) >= sizeof (fullpath
)) {
5468 (void) fprintf(stderr
,
5469 gettext("internal error: "
5470 "ZPOOL_SCRIPTS_PATH too large.\n"));
5474 /* Print the scripts */
5475 if (stat(fullpath
, &dir_stat
) == 0)
5476 if (dir_stat
.st_mode
& S_IXUSR
&&
5477 S_ISREG(dir_stat
.st_mode
))
5478 print_zpool_script_help(ent
->d_name
,
5486 * Print out help text for all zpool status/iostat -c scripts.
5489 print_zpool_script_list(const char *subcommand
)
5491 char *dir
, *sp
, *tmp
;
5493 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5495 sp
= zpool_get_cmd_search_path();
5499 for (dir
= strtok_r(sp
, ":", &tmp
);
5501 dir
= strtok_r(NULL
, ":", &tmp
))
5502 print_zpool_dir_scripts(dir
);
5508 * Set the minimum pool/vdev name column width. The width must be at least 10,
5509 * but may be as large as the column width - 42 so it still fits on one line.
5510 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5513 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5515 iostat_cbdata_t
*cb
= data
;
5516 int width
, available_width
;
5519 * get_namewidth() returns the maximum width of any name in that column
5520 * for any pool/vdev/device line that will be output.
5522 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
5523 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
5526 * The width we are calculating is the width of the header and also the
5527 * padding width for names that are less than maximum width. The stats
5528 * take up 42 characters, so the width available for names is:
5530 available_width
= get_columns() - 42;
5533 * If the maximum width fits on a screen, then great! Make everything
5534 * line up by justifying all lines to the same width. If that max
5535 * width is larger than what's available, the name plus stats won't fit
5536 * on one line, and justifying to that width would cause every line to
5537 * wrap on the screen. We only want lines with long names to wrap.
5538 * Limit the padding to what won't wrap.
5540 if (width
> available_width
)
5541 width
= available_width
;
5544 * And regardless of whatever the screen width is (get_columns can
5545 * return 0 if the width is not known or less than 42 for a narrow
5546 * terminal) have the width be a minimum of 10.
5551 /* Save the calculated width */
5552 cb
->cb_namewidth
= width
;
5558 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5559 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5560 * [interval [count]]
5562 * -c CMD For each vdev, run command CMD
5563 * -g Display guid for individual vdev name.
5564 * -L Follow links when resolving vdev path name.
5565 * -P Display full path for vdev name.
5566 * -v Display statistics for individual vdevs
5568 * -p Display values in parsable (exact) format.
5569 * -H Scripted mode. Don't display headers, and separate properties
5571 * -l Display average latency
5572 * -q Display queue depths
5573 * -w Display latency histograms
5574 * -r Display request size histogram
5575 * -T Display a timestamp in date(1) or Unix format
5576 * -n Only print headers once
5578 * This command can be tricky because we want to be able to deal with pool
5579 * creation/destruction as well as vdev configuration changes. The bulk of this
5580 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5581 * on pool_list_update() to detect the addition of new pools. Configuration
5582 * changes are all handled within libzfs.
5585 zpool_do_iostat(int argc
, char **argv
)
5591 unsigned long count
= 0;
5594 boolean_t verbose
= B_FALSE
;
5595 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5596 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5597 boolean_t omit_since_boot
= B_FALSE
;
5598 boolean_t guid
= B_FALSE
;
5599 boolean_t follow_links
= B_FALSE
;
5600 boolean_t full_name
= B_FALSE
;
5601 boolean_t headers_once
= B_FALSE
;
5602 iostat_cbdata_t cb
= { 0 };
5605 /* Used for printing error message */
5606 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5607 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5609 uint64_t unsupported_flags
;
5612 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5617 gettext("Can't set -c flag twice\n"));
5621 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5622 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5623 fprintf(stderr
, gettext(
5624 "Can't run -c, disabled by "
5625 "ZPOOL_SCRIPTS_ENABLED.\n"));
5629 if ((getuid() <= 0 || geteuid() <= 0) &&
5630 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5631 fprintf(stderr
, gettext(
5632 "Can't run -c with root privileges "
5633 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5643 follow_links
= B_TRUE
;
5649 get_timestamp_arg(*optarg
);
5673 omit_since_boot
= B_TRUE
;
5676 headers_once
= B_TRUE
;
5682 if (optopt
== 'c') {
5683 print_zpool_script_list("iostat");
5687 gettext("invalid option '%c'\n"), optopt
);
5696 cb
.cb_literal
= parsable
;
5697 cb
.cb_scripted
= scripted
;
5700 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_GUID
;
5702 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5704 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_PATH
;
5705 cb
.cb_iteration
= 0;
5706 cb
.cb_namewidth
= 0;
5707 cb
.cb_verbose
= verbose
;
5709 /* Get our interval and count values (if any) */
5711 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5714 get_interval_count(&argc
, argv
, &interval
, &count
);
5718 /* No args, so just print the defaults. */
5719 } else if (are_all_pools(argc
, argv
)) {
5720 /* All the args are pool names */
5721 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
5722 /* All the args are vdevs */
5723 cb
.cb_vdevs
.cb_names
= argv
;
5724 cb
.cb_vdevs
.cb_names_count
= argc
;
5725 argc
= 0; /* No pools to process */
5726 } else if (are_all_pools(1, argv
)) {
5727 /* The first arg is a pool name */
5728 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
5730 /* ...and the rest are vdev names */
5731 cb
.cb_vdevs
.cb_names
= argv
+ 1;
5732 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
5733 argc
= 1; /* One pool to process */
5735 fprintf(stderr
, gettext("Expected either a list of "));
5736 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5737 fprintf(stderr
, " \"%s\", ", argv
[0]);
5738 fprintf(stderr
, gettext("but got:\n"));
5739 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5740 argv
[0], &cb
.cb_vdevs
);
5741 fprintf(stderr
, "\n");
5747 * The args don't make sense. The first arg isn't a pool name,
5748 * nor are all the args vdevs.
5750 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5751 fprintf(stderr
, "\n");
5755 if (cb
.cb_vdevs
.cb_names_count
!= 0) {
5757 * If user specified vdevs, it implies verbose.
5759 cb
.cb_verbose
= B_TRUE
;
5763 * Construct the list of all interesting pools.
5766 if ((list
= pool_list_get(argc
, argv
, NULL
, ZFS_TYPE_POOL
, parsable
,
5770 if (pool_list_count(list
) == 0 && argc
!= 0) {
5771 pool_list_free(list
);
5775 if (pool_list_count(list
) == 0 && interval
== 0) {
5776 pool_list_free(list
);
5777 (void) fprintf(stderr
, gettext("no pools available\n"));
5781 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5782 pool_list_free(list
);
5783 (void) fprintf(stderr
,
5784 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5789 if (l_histo
&& rq_histo
) {
5790 pool_list_free(list
);
5791 (void) fprintf(stderr
,
5792 gettext("Only one of [-r|-w] can be passed at a time\n"));
5798 * Enter the main iostat loop.
5804 * Histograms tables look out of place when you try to display
5805 * them with the other stats, so make a rule that you can only
5806 * print histograms by themselves.
5808 cb
.cb_flags
= IOS_L_HISTO_M
;
5809 } else if (rq_histo
) {
5810 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5812 cb
.cb_flags
= IOS_DEFAULT_M
;
5814 cb
.cb_flags
|= IOS_LATENCY_M
;
5816 cb
.cb_flags
|= IOS_QUEUES_M
;
5820 * See if the module supports all the stats we want to display.
5822 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5823 if (unsupported_flags
) {
5827 gettext("The loaded zfs module doesn't support:"));
5829 /* for each bit set in unsupported_flags */
5830 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5831 idx
= lowbit64(f
) - 1;
5832 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5835 fprintf(stderr
, ". Try running a newer module.\n");
5836 pool_list_free(list
);
5842 if ((npools
= pool_list_count(list
)) == 0)
5843 (void) fprintf(stderr
, gettext("no pools available\n"));
5846 * If this is the first iteration and -y was supplied
5847 * we skip any printing.
5849 boolean_t skip
= (omit_since_boot
&&
5850 cb
.cb_iteration
== 0);
5853 * Refresh all statistics. This is done as an
5854 * explicit step before calculating the maximum name
5855 * width, so that any * configuration changes are
5856 * properly accounted for.
5858 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5862 * Iterate over all pools to determine the maximum width
5863 * for the pool / device name column across all pools.
5865 cb
.cb_namewidth
= 0;
5866 (void) pool_list_iter(list
, B_FALSE
,
5867 get_namewidth_iostat
, &cb
);
5869 if (timestamp_fmt
!= NODATE
)
5870 print_timestamp(timestamp_fmt
);
5872 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5873 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5874 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5875 argv
, cmd
, g_zfs
, cb
.cb_vdevs
.cb_names
,
5876 cb
.cb_vdevs
.cb_names_count
,
5877 cb
.cb_vdevs
.cb_name_flags
);
5884 * Check terminal size so we can print headers
5885 * even when terminal window has its height
5888 winheight
= terminal_height();
5890 * Are we connected to TTY? If not, headers_once
5891 * should be true, to avoid breaking scripts.
5894 headers_once
= B_TRUE
;
5897 * If it's the first time and we're not skipping it,
5898 * or either skip or verbose mode, print the header.
5900 * The histogram code explicitly prints its header on
5901 * every vdev, so skip this for histograms.
5903 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5904 (skip
!= verbose
) ||
5906 (cb
.cb_iteration
% winheight
) == 0)) &&
5907 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5909 print_iostat_header(&cb
);
5912 (void) fsleep(interval
);
5916 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5919 * If there's more than one pool, and we're not in
5920 * verbose mode (which prints a separator for us),
5921 * then print a separator.
5923 * In addition, if we're printing specific vdevs then
5924 * we also want an ending separator.
5926 if (((npools
> 1 && !verbose
&&
5927 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5928 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5929 cb
.cb_vdevs
.cb_names_count
)) &&
5931 print_iostat_separator(&cb
);
5932 if (cb
.vcdl
!= NULL
)
5933 print_cmd_columns(cb
.vcdl
, 1);
5937 if (cb
.vcdl
!= NULL
)
5938 free_vdev_cmd_data_list(cb
.vcdl
);
5943 * Flush the output so that redirection to a file isn't buffered
5946 (void) fflush(stdout
);
5951 if (count
!= 0 && --count
== 0)
5954 (void) fsleep(interval
);
5957 pool_list_free(list
);
5962 typedef struct list_cbdata
{
5963 boolean_t cb_verbose
;
5966 boolean_t cb_scripted
;
5967 zprop_list_t
*cb_proplist
;
5968 boolean_t cb_literal
;
5973 * Given a list of columns to display, output appropriate headers for each one.
5976 print_header(list_cbdata_t
*cb
)
5978 zprop_list_t
*pl
= cb
->cb_proplist
;
5979 char headerbuf
[ZPOOL_MAXPROPLEN
];
5981 boolean_t first
= B_TRUE
;
5982 boolean_t right_justify
;
5985 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5986 width
= pl
->pl_width
;
5987 if (first
&& cb
->cb_verbose
) {
5989 * Reset the width to accommodate the verbose listing
5992 width
= cb
->cb_namewidth
;
5996 (void) fputs(" ", stdout
);
6000 right_justify
= B_FALSE
;
6001 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6002 header
= zpool_prop_column_name(pl
->pl_prop
);
6003 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6007 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
6008 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
6009 headerbuf
[i
] = '\0';
6013 if (pl
->pl_next
== NULL
&& !right_justify
)
6014 (void) fputs(header
, stdout
);
6015 else if (right_justify
)
6016 (void) printf("%*s", (int)width
, header
);
6018 (void) printf("%-*s", (int)width
, header
);
6021 (void) fputc('\n', stdout
);
6025 * Given a pool and a list of properties, print out all the properties according
6026 * to the described layout. Used by zpool_do_list().
6029 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
6031 zprop_list_t
*pl
= cb
->cb_proplist
;
6032 boolean_t first
= B_TRUE
;
6033 char property
[ZPOOL_MAXPROPLEN
];
6034 const char *propstr
;
6035 boolean_t right_justify
;
6038 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6040 width
= pl
->pl_width
;
6041 if (first
&& cb
->cb_verbose
) {
6043 * Reset the width to accommodate the verbose listing
6046 width
= cb
->cb_namewidth
;
6050 if (cb
->cb_scripted
)
6051 (void) fputc('\t', stdout
);
6053 (void) fputs(" ", stdout
);
6058 right_justify
= B_FALSE
;
6059 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6060 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
6061 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
6066 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6067 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
6068 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
6069 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
6070 sizeof (property
)) == 0) {
6078 * If this is being called in scripted mode, or if this is the
6079 * last column and it is left-justified, don't include a width
6082 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
6083 (void) fputs(propstr
, stdout
);
6084 else if (right_justify
)
6085 (void) printf("%*s", (int)width
, propstr
);
6087 (void) printf("%-*s", (int)width
, propstr
);
6090 (void) fputc('\n', stdout
);
6094 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
6095 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
6099 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6102 case ZPOOL_PROP_SIZE
:
6103 case ZPOOL_PROP_EXPANDSZ
:
6104 case ZPOOL_PROP_CHECKPOINT
:
6105 case ZPOOL_PROP_DEDUPRATIO
:
6107 (void) strlcpy(propval
, "-", sizeof (propval
));
6109 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6112 case ZPOOL_PROP_FRAGMENTATION
:
6113 if (value
== ZFS_FRAG_INVALID
) {
6114 (void) strlcpy(propval
, "-", sizeof (propval
));
6115 } else if (format
== ZFS_NICENUM_RAW
) {
6116 (void) snprintf(propval
, sizeof (propval
), "%llu",
6117 (unsigned long long)value
);
6119 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6120 (unsigned long long)value
);
6123 case ZPOOL_PROP_CAPACITY
:
6124 /* capacity value is in parts-per-10,000 (aka permyriad) */
6125 if (format
== ZFS_NICENUM_RAW
)
6126 (void) snprintf(propval
, sizeof (propval
), "%llu",
6127 (unsigned long long)value
/ 100);
6129 (void) snprintf(propval
, sizeof (propval
),
6130 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6131 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6133 case ZPOOL_PROP_HEALTH
:
6135 (void) strlcpy(propval
, str
, sizeof (propval
));
6138 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6142 (void) strlcpy(propval
, "-", sizeof (propval
));
6145 (void) printf("\t%s", propval
);
6147 (void) printf(" %*s", (int)width
, propval
);
6151 * print static default line per vdev
6152 * not compatible with '-o' <proplist> option
6155 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6156 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
6162 boolean_t scripted
= cb
->cb_scripted
;
6163 uint64_t islog
= B_FALSE
;
6164 const char *dashes
= "%-*s - - - - "
6167 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
6168 (uint64_t **)&vs
, &c
) == 0);
6171 boolean_t toplevel
= (vs
->vs_space
!= 0);
6173 enum zfs_nicenum_format format
;
6177 format
= ZFS_NICENUM_RAW
;
6179 format
= ZFS_NICENUM_1024
;
6181 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
6185 (void) printf("\t%s", name
);
6186 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
6187 (void) printf("%*s%s", depth
, "", name
);
6189 (void) printf("%*s%s%*s", depth
, "", name
,
6190 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
6193 * Print the properties for the individual vdevs. Some
6194 * properties are only applicable to toplevel vdevs. The
6195 * 'toplevel' boolean value is passed to the print_one_column()
6196 * to indicate that the value is valid.
6198 if (VDEV_STAT_VALID(vs_pspace
, c
) && vs
->vs_pspace
)
6199 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_pspace
, NULL
,
6200 scripted
, B_TRUE
, format
);
6202 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
,
6203 scripted
, toplevel
, format
);
6204 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6205 scripted
, toplevel
, format
);
6206 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6207 NULL
, scripted
, toplevel
, format
);
6208 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6209 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6210 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6211 scripted
, B_TRUE
, format
);
6212 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6213 vs
->vs_fragmentation
, NULL
, scripted
,
6214 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6216 cap
= (vs
->vs_space
== 0) ? 0 :
6217 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6218 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6219 scripted
, toplevel
, format
);
6220 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6221 scripted
, toplevel
, format
);
6222 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6224 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6226 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6229 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6231 (void) fputc('\n', stdout
);
6234 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6235 &child
, &children
) != 0)
6238 /* list the normal vdevs first */
6239 for (c
= 0; c
< children
; c
++) {
6240 uint64_t ishole
= B_FALSE
;
6242 if (nvlist_lookup_uint64(child
[c
],
6243 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6246 if (nvlist_lookup_uint64(child
[c
],
6247 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6250 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6253 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6254 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6255 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6259 /* list the classes: 'logs', 'dedup', and 'special' */
6260 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
6261 boolean_t printed
= B_FALSE
;
6263 for (c
= 0; c
< children
; c
++) {
6264 const char *bias
= NULL
;
6265 const char *type
= NULL
;
6267 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6268 &islog
) == 0 && islog
) {
6269 bias
= VDEV_ALLOC_CLASS_LOGS
;
6271 (void) nvlist_lookup_string(child
[c
],
6272 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6273 (void) nvlist_lookup_string(child
[c
],
6274 ZPOOL_CONFIG_TYPE
, &type
);
6276 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6278 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6282 /* LINTED E_SEC_PRINTF_VAR_FMT */
6283 (void) printf(dashes
, cb
->cb_namewidth
,
6287 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6288 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6289 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6295 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6296 &child
, &children
) == 0 && children
> 0) {
6297 /* LINTED E_SEC_PRINTF_VAR_FMT */
6298 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6299 for (c
= 0; c
< children
; c
++) {
6300 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6302 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6308 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6309 &children
) == 0 && children
> 0) {
6310 /* LINTED E_SEC_PRINTF_VAR_FMT */
6311 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6312 for (c
= 0; c
< children
; c
++) {
6313 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6315 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6323 * Generic callback function to list a pool.
6326 list_callback(zpool_handle_t
*zhp
, void *data
)
6328 list_cbdata_t
*cbp
= data
;
6330 print_pool(zhp
, cbp
);
6332 if (cbp
->cb_verbose
) {
6333 nvlist_t
*config
, *nvroot
;
6335 config
= zpool_get_config(zhp
, NULL
);
6336 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6338 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6345 * Set the minimum pool/vdev name column width. The width must be at least 9,
6346 * but may be as large as needed.
6349 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6351 list_cbdata_t
*cb
= data
;
6354 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
6355 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
6360 cb
->cb_namewidth
= width
;
6366 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6368 * -g Display guid for individual vdev name.
6369 * -H Scripted mode. Don't display headers, and separate properties
6371 * -L Follow links when resolving vdev path name.
6372 * -o List of properties to display. Defaults to
6373 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6374 * "dedupratio,health,altroot"
6375 * -p Display values in parsable (exact) format.
6376 * -P Display full path for vdev name.
6377 * -T Display a timestamp in date(1) or Unix format
6379 * List all pools in the system, whether or not they're healthy. Output space
6380 * statistics for each one, as well as health status summary.
6383 zpool_do_list(int argc
, char **argv
)
6387 list_cbdata_t cb
= { 0 };
6388 static char default_props
[] =
6389 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6390 "capacity,dedupratio,health,altroot";
6391 char *props
= default_props
;
6393 unsigned long count
= 0;
6395 boolean_t first
= B_TRUE
;
6396 current_prop_type
= ZFS_TYPE_POOL
;
6399 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6402 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6405 cb
.cb_scripted
= B_TRUE
;
6408 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6414 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6417 cb
.cb_literal
= B_TRUE
;
6420 get_timestamp_arg(*optarg
);
6423 cb
.cb_verbose
= B_TRUE
;
6424 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6427 (void) fprintf(stderr
, gettext("missing argument for "
6428 "'%c' option\n"), optopt
);
6432 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6441 get_interval_count(&argc
, argv
, &interval
, &count
);
6443 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6447 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6448 ZFS_TYPE_POOL
, cb
.cb_literal
, &ret
)) == NULL
)
6451 if (pool_list_count(list
) == 0)
6454 cb
.cb_namewidth
= 0;
6455 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6457 if (timestamp_fmt
!= NODATE
)
6458 print_timestamp(timestamp_fmt
);
6460 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6464 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6469 if (count
!= 0 && --count
== 0)
6472 pool_list_free(list
);
6473 (void) fsleep(interval
);
6476 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6477 (void) printf(gettext("no pools available\n"));
6481 pool_list_free(list
);
6482 zprop_free_list(cb
.cb_proplist
);
6487 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6489 boolean_t force
= B_FALSE
;
6490 boolean_t rebuild
= B_FALSE
;
6491 boolean_t wait
= B_FALSE
;
6494 char *poolname
, *old_disk
, *new_disk
;
6495 zpool_handle_t
*zhp
;
6496 nvlist_t
*props
= NULL
;
6501 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6507 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6508 (void) fprintf(stderr
, gettext("missing "
6509 "'=' for -o option\n"));
6515 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6516 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6526 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6535 /* get pool name and check number of arguments */
6537 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6544 (void) fprintf(stderr
,
6545 gettext("missing <device> specification\n"));
6553 (void) fprintf(stderr
,
6554 gettext("missing <new_device> specification\n"));
6557 new_disk
= old_disk
;
6567 (void) fprintf(stderr
, gettext("too many arguments\n"));
6571 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6576 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6577 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6584 /* unless manually specified use "ashift" pool property (if set) */
6585 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6588 char strval
[ZPOOL_MAXPROPLEN
];
6590 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6591 if (src
!= ZPROP_SRC_DEFAULT
) {
6592 (void) sprintf(strval
, "%" PRId32
, intval
);
6593 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6594 &props
, B_TRUE
) == 0);
6598 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6600 if (nvroot
== NULL
) {
6606 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6609 if (ret
== 0 && wait
)
6610 ret
= zpool_wait(zhp
,
6611 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6614 nvlist_free(nvroot
);
6621 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6623 * -f Force attach, even if <new_device> appears to be in use.
6624 * -s Use sequential instead of healing reconstruction for resilver.
6625 * -o Set property=value.
6626 * -w Wait for replacing to complete before returning
6628 * Replace <device> with <new_device>.
6631 zpool_do_replace(int argc
, char **argv
)
6633 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6637 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6639 * -f Force attach, even if <new_device> appears to be in use.
6640 * -s Use sequential instead of healing reconstruction for resilver.
6641 * -o Set property=value.
6642 * -w Wait for resilvering to complete before returning
6644 * Attach <new_device> to the mirror containing <device>. If <device> is not
6645 * part of a mirror, then <device> will be transformed into a mirror of
6646 * <device> and <new_device>. In either case, <new_device> will begin life
6647 * with a DTL of [0, now], and will immediately begin to resilver itself.
6650 zpool_do_attach(int argc
, char **argv
)
6652 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6656 * zpool detach [-f] <pool> <device>
6658 * -f Force detach of <device>, even if DTLs argue against it
6659 * (not supported yet)
6661 * Detach a device from a mirror. The operation will be refused if <device>
6662 * is the last device in the mirror, or if the DTLs indicate that this device
6663 * has the only valid copy of some data.
6666 zpool_do_detach(int argc
, char **argv
)
6669 char *poolname
, *path
;
6670 zpool_handle_t
*zhp
;
6674 while ((c
= getopt(argc
, argv
, "")) != -1) {
6677 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6686 /* get pool name and check number of arguments */
6688 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6693 (void) fprintf(stderr
,
6694 gettext("missing <device> specification\n"));
6701 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6704 ret
= zpool_vdev_detach(zhp
, path
);
6712 * zpool split [-gLnP] [-o prop=val] ...
6714 * [-R altroot] <pool> <newpool> [<device> ...]
6716 * -g Display guid for individual vdev name.
6717 * -L Follow links when resolving vdev path name.
6718 * -n Do not split the pool, but display the resulting layout if
6719 * it were to be split.
6720 * -o Set property=value, or set mount options.
6721 * -P Display full path for vdev name.
6722 * -R Mount the split-off pool under an alternate root.
6723 * -l Load encryption keys while importing.
6725 * Splits the named pool and gives it the new pool name. Devices to be split
6726 * off may be listed, provided that no more than one device is specified
6727 * per top-level vdev mirror. The newly split pool is left in an exported
6728 * state unless -R is specified.
6730 * Restrictions: the top-level of the pool pool must only be made up of
6731 * mirrors; all devices in the pool must be healthy; no device may be
6732 * undergoing a resilvering operation.
6735 zpool_do_split(int argc
, char **argv
)
6737 char *srcpool
, *newpool
, *propval
;
6738 char *mntopts
= NULL
;
6741 boolean_t loadkeys
= B_FALSE
;
6742 zpool_handle_t
*zhp
;
6743 nvlist_t
*config
, *props
= NULL
;
6745 flags
.dryrun
= B_FALSE
;
6746 flags
.import
= B_FALSE
;
6747 flags
.name_flags
= 0;
6750 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6753 flags
.name_flags
|= VDEV_NAME_GUID
;
6756 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6759 flags
.import
= B_TRUE
;
6761 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6762 &props
, B_TRUE
) != 0) {
6771 flags
.dryrun
= B_TRUE
;
6774 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6777 if (add_prop_list(optarg
, propval
,
6778 &props
, B_TRUE
) != 0) {
6787 flags
.name_flags
|= VDEV_NAME_PATH
;
6790 (void) fprintf(stderr
, gettext("missing argument for "
6791 "'%c' option\n"), optopt
);
6795 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6802 if (!flags
.import
&& mntopts
!= NULL
) {
6803 (void) fprintf(stderr
, gettext("setting mntopts is only "
6804 "valid when importing the pool\n"));
6808 if (!flags
.import
&& loadkeys
) {
6809 (void) fprintf(stderr
, gettext("loading keys is only "
6810 "valid when importing the pool\n"));
6818 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6822 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6832 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6837 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6838 if (config
== NULL
) {
6842 (void) printf(gettext("would create '%s' with the "
6843 "following layout:\n\n"), newpool
);
6844 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6846 print_vdev_tree(NULL
, "dedup", config
, 0,
6847 VDEV_ALLOC_BIAS_DEDUP
, 0);
6848 print_vdev_tree(NULL
, "special", config
, 0,
6849 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6855 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6856 nvlist_free(config
);
6862 * The split was successful. Now we need to open the new
6863 * pool and import it.
6865 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6866 nvlist_free(config
);
6872 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6877 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6878 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6880 (void) fprintf(stderr
, gettext("Split was successful, but "
6881 "the datasets could not all be mounted\n"));
6882 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6883 "different altroot\n"), "zpool import");
6886 nvlist_free(config
);
6895 * zpool online <pool> <device> ...
6898 zpool_do_online(int argc
, char **argv
)
6902 zpool_handle_t
*zhp
;
6904 vdev_state_t newstate
;
6908 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6911 flags
|= ZFS_ONLINE_EXPAND
;
6914 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6923 /* get pool name and check number of arguments */
6925 (void) fprintf(stderr
, gettext("missing pool name\n"));
6929 (void) fprintf(stderr
, gettext("missing device name\n"));
6935 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6938 for (i
= 1; i
< argc
; i
++) {
6939 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6940 if (newstate
!= VDEV_STATE_HEALTHY
) {
6941 (void) printf(gettext("warning: device '%s' "
6942 "onlined, but remains in faulted state\n"),
6944 if (newstate
== VDEV_STATE_FAULTED
)
6945 (void) printf(gettext("use 'zpool "
6946 "clear' to restore a faulted "
6949 (void) printf(gettext("use 'zpool "
6950 "replace' to replace devices "
6951 "that are no longer present\n"));
6964 * zpool offline [-ft] <pool> <device> ...
6966 * -f Force the device into a faulted state.
6968 * -t Only take the device off-line temporarily. The offline/faulted
6969 * state will not be persistent across reboots.
6972 zpool_do_offline(int argc
, char **argv
)
6976 zpool_handle_t
*zhp
;
6978 boolean_t istmp
= B_FALSE
;
6979 boolean_t fault
= B_FALSE
;
6982 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6991 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7000 /* get pool name and check number of arguments */
7002 (void) fprintf(stderr
, gettext("missing pool name\n"));
7006 (void) fprintf(stderr
, gettext("missing device name\n"));
7012 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7015 for (i
= 1; i
< argc
; i
++) {
7017 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
7019 if (istmp
== B_FALSE
) {
7020 /* Force the fault to persist across imports */
7021 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
7023 aux
= VDEV_AUX_EXTERNAL
;
7026 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
7029 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
7040 * zpool clear <pool> [device]
7042 * Clear all errors associated with a pool or a particular device.
7045 zpool_do_clear(int argc
, char **argv
)
7049 boolean_t dryrun
= B_FALSE
;
7050 boolean_t do_rewind
= B_FALSE
;
7051 boolean_t xtreme_rewind
= B_FALSE
;
7052 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
7053 nvlist_t
*policy
= NULL
;
7054 zpool_handle_t
*zhp
;
7055 char *pool
, *device
;
7058 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
7067 xtreme_rewind
= B_TRUE
;
7070 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7080 (void) fprintf(stderr
, gettext("missing pool name\n"));
7085 (void) fprintf(stderr
, gettext("too many arguments\n"));
7089 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
7090 (void) fprintf(stderr
,
7091 gettext("-n or -X only meaningful with -F\n"));
7095 rewind_policy
= ZPOOL_TRY_REWIND
;
7097 rewind_policy
= ZPOOL_DO_REWIND
;
7099 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
7101 /* In future, further rewind policy choices can be passed along here */
7102 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
7103 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
7104 rewind_policy
) != 0) {
7109 device
= argc
== 2 ? argv
[1] : NULL
;
7111 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
7112 nvlist_free(policy
);
7116 if (zpool_clear(zhp
, device
, policy
) != 0)
7121 nvlist_free(policy
);
7127 * zpool reguid <pool>
7130 zpool_do_reguid(int argc
, char **argv
)
7134 zpool_handle_t
*zhp
;
7138 while ((c
= getopt(argc
, argv
, "")) != -1) {
7141 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7150 /* get pool name and check number of arguments */
7152 (void) fprintf(stderr
, gettext("missing pool name\n"));
7157 (void) fprintf(stderr
, gettext("too many arguments\n"));
7162 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7165 ret
= zpool_reguid(zhp
);
7173 * zpool reopen <pool>
7175 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7178 zpool_do_reopen(int argc
, char **argv
)
7182 boolean_t scrub_restart
= B_TRUE
;
7185 while ((c
= getopt(argc
, argv
, "n")) != -1) {
7188 scrub_restart
= B_FALSE
;
7191 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7200 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7201 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7202 B_FALSE
, zpool_reopen_one
, &scrub_restart
);
7207 typedef struct scrub_cbdata
{
7209 pool_scrub_cmd_t cb_scrub_cmd
;
7213 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7215 nvlist_t
*config
, *nvroot
;
7217 config
= zpool_get_config(zhp
, NULL
);
7219 if (config
!= NULL
) {
7220 pool_checkpoint_stat_t
*pcs
= NULL
;
7223 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7224 (void) nvlist_lookup_uint64_array(nvroot
,
7225 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7227 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7230 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7231 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7239 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7241 scrub_cbdata_t
*cb
= data
;
7245 * Ignore faulted pools.
7247 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7248 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7249 "currently unavailable\n"), zpool_get_name(zhp
));
7253 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7255 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7256 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7257 (void) printf(gettext("warning: will not scrub state that "
7258 "belongs to the checkpoint of pool '%s'\n"),
7259 zpool_get_name(zhp
));
7266 wait_callback(zpool_handle_t
*zhp
, void *data
)
7268 zpool_wait_activity_t
*act
= data
;
7269 return (zpool_wait(zhp
, *act
));
7273 * zpool scrub [-s | -p] [-w] <pool> ...
7275 * -s Stop. Stops any in-progress scrub.
7276 * -p Pause. Pause in-progress scrub.
7277 * -w Wait. Blocks until scrub has completed.
7280 zpool_do_scrub(int argc
, char **argv
)
7284 boolean_t wait
= B_FALSE
;
7287 cb
.cb_type
= POOL_SCAN_SCRUB
;
7288 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7291 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7294 cb
.cb_type
= POOL_SCAN_NONE
;
7297 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7303 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7309 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7310 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7311 (void) fprintf(stderr
, gettext("invalid option combination: "
7312 "-s and -p are mutually exclusive\n"));
7316 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7317 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7318 (void) fprintf(stderr
, gettext("invalid option combination: "
7319 "-w cannot be used with -p or -s\n"));
7327 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7331 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7332 B_FALSE
, scrub_callback
, &cb
);
7334 if (wait
&& !error
) {
7335 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7336 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7337 B_FALSE
, wait_callback
, &act
);
7344 * zpool resilver <pool> ...
7346 * Restarts any in-progress resilver
7349 zpool_do_resilver(int argc
, char **argv
)
7354 cb
.cb_type
= POOL_SCAN_RESILVER
;
7355 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7358 while ((c
= getopt(argc
, argv
, "")) != -1) {
7361 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7371 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7375 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7376 B_FALSE
, scrub_callback
, &cb
));
7380 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7382 * -c Cancel. Ends any in-progress trim.
7383 * -d Secure trim. Requires kernel and device support.
7384 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7385 * adding a multiplier suffix such as 'k' or 'm'.
7386 * -s Suspend. TRIM can then be restarted with no flags.
7387 * -w Wait. Blocks until trimming has completed.
7390 zpool_do_trim(int argc
, char **argv
)
7392 struct option long_options
[] = {
7393 {"cancel", no_argument
, NULL
, 'c'},
7394 {"secure", no_argument
, NULL
, 'd'},
7395 {"rate", required_argument
, NULL
, 'r'},
7396 {"suspend", no_argument
, NULL
, 's'},
7397 {"wait", no_argument
, NULL
, 'w'},
7401 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7403 boolean_t secure
= B_FALSE
;
7404 boolean_t wait
= B_FALSE
;
7407 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7411 if (cmd_type
!= POOL_TRIM_START
&&
7412 cmd_type
!= POOL_TRIM_CANCEL
) {
7413 (void) fprintf(stderr
, gettext("-c cannot be "
7414 "combined with other options\n"));
7417 cmd_type
= POOL_TRIM_CANCEL
;
7420 if (cmd_type
!= POOL_TRIM_START
) {
7421 (void) fprintf(stderr
, gettext("-d cannot be "
7422 "combined with the -c or -s options\n"));
7428 if (cmd_type
!= POOL_TRIM_START
) {
7429 (void) fprintf(stderr
, gettext("-r cannot be "
7430 "combined with the -c or -s options\n"));
7433 if (zfs_nicestrtonum(g_zfs
, optarg
, &rate
) == -1) {
7434 (void) fprintf(stderr
, "%s: %s\n",
7435 gettext("invalid value for rate"),
7436 libzfs_error_description(g_zfs
));
7441 if (cmd_type
!= POOL_TRIM_START
&&
7442 cmd_type
!= POOL_TRIM_SUSPEND
) {
7443 (void) fprintf(stderr
, gettext("-s cannot be "
7444 "combined with other options\n"));
7447 cmd_type
= POOL_TRIM_SUSPEND
;
7454 (void) fprintf(stderr
,
7455 gettext("invalid option '%c'\n"), optopt
);
7457 (void) fprintf(stderr
,
7458 gettext("invalid option '%s'\n"),
7469 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7474 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7475 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7480 char *poolname
= argv
[0];
7481 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7485 trimflags_t trim_flags
= {
7491 nvlist_t
*vdevs
= fnvlist_alloc();
7493 /* no individual leaf vdevs specified, so add them all */
7494 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7495 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7496 ZPOOL_CONFIG_VDEV_TREE
);
7497 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7498 trim_flags
.fullpool
= B_TRUE
;
7500 trim_flags
.fullpool
= B_FALSE
;
7501 for (int i
= 1; i
< argc
; i
++) {
7502 fnvlist_add_boolean(vdevs
, argv
[i
]);
7506 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7508 fnvlist_free(vdevs
);
7515 * Converts a total number of seconds to a human readable string broken
7516 * down in to days/hours/minutes/seconds.
7519 secs_to_dhms(uint64_t total
, char *buf
)
7521 uint64_t days
= total
/ 60 / 60 / 24;
7522 uint64_t hours
= (total
/ 60 / 60) % 24;
7523 uint64_t mins
= (total
/ 60) % 60;
7524 uint64_t secs
= (total
% 60);
7527 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7528 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7529 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7531 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7532 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7533 (u_longlong_t
)secs
);
7538 * Print out detailed scrub status.
7541 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7543 time_t start
, end
, pause
;
7544 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7545 uint64_t elapsed
, scan_rate
, issue_rate
;
7546 double fraction_done
;
7547 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7548 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7551 printf_color(ANSI_BOLD
, gettext("scan:"));
7554 /* If there's never been a scan, there's not much to say. */
7555 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7556 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7557 (void) printf(gettext("none requested\n"));
7561 start
= ps
->pss_start_time
;
7562 end
= ps
->pss_end_time
;
7563 pause
= ps
->pss_pass_scrub_pause
;
7565 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7567 int is_resilver
= ps
->pss_func
== POOL_SCAN_RESILVER
;
7568 int is_scrub
= ps
->pss_func
== POOL_SCAN_SCRUB
;
7569 assert(is_resilver
|| is_scrub
);
7571 /* Scan is finished or canceled. */
7572 if (ps
->pss_state
== DSS_FINISHED
) {
7573 secs_to_dhms(end
- start
, time_buf
);
7576 (void) printf(gettext("scrub repaired %s "
7577 "in %s with %llu errors on %s"), processed_buf
,
7578 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7580 } else if (is_resilver
) {
7581 (void) printf(gettext("resilvered %s "
7582 "in %s with %llu errors on %s"), processed_buf
,
7583 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7587 } else if (ps
->pss_state
== DSS_CANCELED
) {
7589 (void) printf(gettext("scrub canceled on %s"),
7591 } else if (is_resilver
) {
7592 (void) printf(gettext("resilver canceled on %s"),
7598 assert(ps
->pss_state
== DSS_SCANNING
);
7600 /* Scan is in progress. Resilvers can't be paused. */
7603 (void) printf(gettext("scrub in progress since %s"),
7606 (void) printf(gettext("scrub paused since %s"),
7608 (void) printf(gettext("\tscrub started on %s"),
7611 } else if (is_resilver
) {
7612 (void) printf(gettext("resilver in progress since %s"),
7616 scanned
= ps
->pss_examined
;
7617 pass_scanned
= ps
->pss_pass_exam
;
7618 issued
= ps
->pss_issued
;
7619 pass_issued
= ps
->pss_pass_issued
;
7620 total
= ps
->pss_to_examine
;
7622 /* we are only done with a block once we have issued the IO for it */
7623 fraction_done
= (double)issued
/ total
;
7625 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7626 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7627 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7628 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7630 scan_rate
= pass_scanned
/ elapsed
;
7631 issue_rate
= pass_issued
/ elapsed
;
7632 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7633 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7634 secs_to_dhms(total_secs_left
, time_buf
);
7636 /* format all of the numbers we will be reporting */
7637 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7638 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7639 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7640 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7641 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7643 /* do not print estimated time if we have a paused scrub */
7645 (void) printf(gettext("\t%s scanned at %s/s, "
7646 "%s issued at %s/s, %s total\n"),
7647 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7649 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7650 scanned_buf
, issued_buf
, total_buf
);
7654 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7655 processed_buf
, 100 * fraction_done
);
7656 } else if (is_scrub
) {
7657 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7658 processed_buf
, 100 * fraction_done
);
7663 * Only provide an estimate iff:
7664 * 1) the time remaining is valid, and
7665 * 2) the issue rate exceeds 10 MB/s, and
7667 * a) a resilver which has started repairs, or
7668 * b) a scrub which has entered the issue phase.
7670 if (total_secs_left
!= UINT64_MAX
&&
7671 issue_rate
>= 10 * 1024 * 1024 &&
7672 ((is_resilver
&& ps
->pss_processed
> 0) ||
7673 (is_scrub
&& issued
> 0))) {
7674 (void) printf(gettext(", %s to go\n"), time_buf
);
7676 (void) printf(gettext(", no estimated "
7677 "completion time\n"));
7680 (void) printf(gettext("\n"));
7685 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7687 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7691 printf_color(ANSI_BOLD
, gettext("scan:"));
7694 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7695 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7696 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7697 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7698 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7699 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7700 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7701 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7702 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7703 (bytes_est
+ 1), 100);
7705 /* Format all of the numbers we will be reporting */
7706 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7707 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7708 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7709 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7710 sizeof (bytes_scanned_buf
));
7711 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7712 sizeof (bytes_issued_buf
));
7713 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7714 sizeof (bytes_rebuilt_buf
));
7715 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7716 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7717 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7719 time_t start
= vrs
->vrs_start_time
;
7720 time_t end
= vrs
->vrs_end_time
;
7722 /* Rebuild is finished or canceled. */
7723 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7724 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7725 (void) printf(gettext("resilvered (%s) %s in %s "
7726 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7727 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7729 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7730 (void) printf(gettext("resilver (%s) canceled on %s"),
7731 vdev_name
, ctime(&end
));
7733 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7734 (void) printf(gettext("resilver (%s) in progress since %s"),
7735 vdev_name
, ctime(&start
));
7738 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7740 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7741 MAX(scan_rate
, 1), time_buf
);
7743 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7744 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7745 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7746 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7747 bytes_rebuilt_buf
, scan_pct
);
7749 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7750 if (scan_rate
>= 10 * 1024 * 1024) {
7751 (void) printf(gettext(", %s to go\n"), time_buf
);
7753 (void) printf(gettext(", no estimated "
7754 "completion time\n"));
7757 (void) printf(gettext("\n"));
7762 * Print rebuild status for top-level vdevs.
7765 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7770 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7771 &child
, &children
) != 0)
7774 for (uint_t c
= 0; c
< children
; c
++) {
7775 vdev_rebuild_stat_t
*vrs
;
7778 if (nvlist_lookup_uint64_array(child
[c
],
7779 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7780 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7781 child
[c
], VDEV_NAME_TYPE_ID
);
7782 print_rebuild_status_impl(vrs
, name
);
7789 * As we don't scrub checkpointed blocks, we want to warn the user that we
7790 * skipped scanning some blocks if a checkpoint exists or existed at any
7791 * time during the scan. If a sequential instead of healing reconstruction
7792 * was performed then the blocks were reconstructed. However, their checksums
7793 * have not been verified so we still print the warning.
7796 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7798 if (ps
== NULL
|| pcs
== NULL
)
7801 if (pcs
->pcs_state
== CS_NONE
||
7802 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7805 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7807 if (ps
->pss_state
== DSS_NONE
)
7810 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7811 ps
->pss_end_time
< pcs
->pcs_start_time
)
7814 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7815 (void) printf(gettext(" scan warning: skipped blocks "
7816 "that are only referenced by the checkpoint.\n"));
7818 assert(ps
->pss_state
== DSS_SCANNING
);
7819 (void) printf(gettext(" scan warning: skipping blocks "
7820 "that are only referenced by the checkpoint.\n"));
7825 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7826 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7827 * the last completed (or cancelled) rebuild.
7830 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7834 boolean_t rebuilding
= B_FALSE
;
7835 uint64_t end_time
= 0;
7837 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7838 &child
, &children
) != 0)
7841 for (uint_t c
= 0; c
< children
; c
++) {
7842 vdev_rebuild_stat_t
*vrs
;
7845 if (nvlist_lookup_uint64_array(child
[c
],
7846 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7848 if (vrs
->vrs_end_time
> end_time
)
7849 end_time
= vrs
->vrs_end_time
;
7851 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7852 rebuilding
= B_TRUE
;
7859 if (rebuild_end_time
!= NULL
)
7860 *rebuild_end_time
= end_time
;
7862 return (rebuilding
);
7866 * Print the scan status.
7869 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7871 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7872 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7873 boolean_t active_resilver
= B_FALSE
;
7874 pool_checkpoint_stat_t
*pcs
= NULL
;
7875 pool_scan_stat_t
*ps
= NULL
;
7878 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7879 (uint64_t **)&ps
, &c
) == 0) {
7880 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7881 resilver_end_time
= ps
->pss_end_time
;
7882 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7885 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7886 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7889 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7890 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7892 /* Always print the scrub status when available. */
7894 print_scan_scrub_resilver_status(ps
);
7897 * When there is an active resilver or rebuild print its status.
7898 * Otherwise print the status of the last resilver or rebuild.
7900 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7901 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7902 print_scan_scrub_resilver_status(ps
);
7903 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7904 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7905 print_rebuild_status(zhp
, nvroot
);
7908 (void) nvlist_lookup_uint64_array(nvroot
,
7909 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7910 print_checkpoint_scan_warning(ps
, pcs
);
7914 * Print out detailed removal status.
7917 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7919 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7921 nvlist_t
*config
, *nvroot
;
7926 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7930 * Determine name of vdev.
7932 config
= zpool_get_config(zhp
, NULL
);
7933 nvroot
= fnvlist_lookup_nvlist(config
,
7934 ZPOOL_CONFIG_VDEV_TREE
);
7935 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7936 &child
, &children
) == 0);
7937 assert(prs
->prs_removing_vdev
< children
);
7938 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7939 child
[prs
->prs_removing_vdev
], B_TRUE
);
7941 printf_color(ANSI_BOLD
, gettext("remove: "));
7943 start
= prs
->prs_start_time
;
7944 end
= prs
->prs_end_time
;
7945 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7948 * Removal is finished or canceled.
7950 if (prs
->prs_state
== DSS_FINISHED
) {
7951 uint64_t minutes_taken
= (end
- start
) / 60;
7953 (void) printf(gettext("Removal of vdev %llu copied %s "
7954 "in %lluh%um, completed on %s"),
7955 (longlong_t
)prs
->prs_removing_vdev
,
7957 (u_longlong_t
)(minutes_taken
/ 60),
7958 (uint_t
)(minutes_taken
% 60),
7959 ctime((time_t *)&end
));
7960 } else if (prs
->prs_state
== DSS_CANCELED
) {
7961 (void) printf(gettext("Removal of %s canceled on %s"),
7962 vdev_name
, ctime(&end
));
7964 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7965 double fraction_done
;
7968 assert(prs
->prs_state
== DSS_SCANNING
);
7971 * Removal is in progress.
7973 (void) printf(gettext(
7974 "Evacuation of %s in progress since %s"),
7975 vdev_name
, ctime(&start
));
7977 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7978 total
= prs
->prs_to_copy
;
7979 fraction_done
= (double)copied
/ total
;
7981 /* elapsed time for this pass */
7982 elapsed
= time(NULL
) - prs
->prs_start_time
;
7983 elapsed
= elapsed
> 0 ? elapsed
: 1;
7984 rate
= copied
/ elapsed
;
7985 rate
= rate
> 0 ? rate
: 1;
7986 mins_left
= ((total
- copied
) / rate
) / 60;
7987 hours_left
= mins_left
/ 60;
7989 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7990 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7991 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7994 * do not print estimated time if hours_left is more than
7997 (void) printf(gettext(
7998 "\t%s copied out of %s at %s/s, %.2f%% done"),
7999 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
8000 if (hours_left
< (30 * 24)) {
8001 (void) printf(gettext(", %lluh%um to go\n"),
8002 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
8004 (void) printf(gettext(
8005 ", (copy is slow, no estimated time)\n"));
8010 if (prs
->prs_mapping_memory
> 0) {
8012 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
8013 (void) printf(gettext(
8014 "\t%s memory used for removed device mappings\n"),
8020 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
8025 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
8028 (void) printf(gettext("checkpoint: "));
8030 start
= pcs
->pcs_start_time
;
8031 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
8033 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
8034 char *date
= ctime(&start
);
8037 * ctime() adds a newline at the end of the generated
8038 * string, thus the weird format specifier and the
8039 * strlen() call used to chop it off from the output.
8041 (void) printf(gettext("created %.*s, consumes %s\n"),
8042 (int)(strlen(date
) - 1), date
, space_buf
);
8046 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
8048 (void) printf(gettext("discarding, %s remaining.\n"),
8053 print_error_log(zpool_handle_t
*zhp
)
8055 nvlist_t
*nverrlist
= NULL
;
8058 size_t len
= MAXPATHLEN
* 2;
8060 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
8063 (void) printf("errors: Permanent errors have been "
8064 "detected in the following files:\n\n");
8066 pathname
= safe_malloc(len
);
8068 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
8070 uint64_t dsobj
, obj
;
8072 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
8073 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
8075 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
8077 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
8078 (void) printf("%7s %s\n", "", pathname
);
8081 nvlist_free(nverrlist
);
8085 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
8094 (void) printf(gettext("\tspares\n"));
8096 for (i
= 0; i
< nspares
; i
++) {
8097 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
8099 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
8105 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
8114 (void) printf(gettext("\tcache\n"));
8116 for (i
= 0; i
< nl2cache
; i
++) {
8117 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
8119 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
8126 print_dedup_stats(nvlist_t
*config
)
8128 ddt_histogram_t
*ddh
;
8132 char dspace
[6], mspace
[6];
8135 * If the pool was faulted then we may not have been able to
8136 * obtain the config. Otherwise, if we have anything in the dedup
8137 * table continue processing the stats.
8139 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
8140 (uint64_t **)&ddo
, &c
) != 0)
8143 (void) printf("\n");
8144 (void) printf(gettext(" dedup: "));
8145 if (ddo
->ddo_count
== 0) {
8146 (void) printf(gettext("no DDT entries\n"));
8150 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
8151 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
8152 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8153 (u_longlong_t
)ddo
->ddo_count
,
8157 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
8158 (uint64_t **)&dds
, &c
) == 0);
8159 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
8160 (uint64_t **)&ddh
, &c
) == 0);
8161 zpool_dump_ddt(dds
, ddh
);
8165 * Display a summary of pool status. Displays a summary such as:
8169 * reason: One or more devices ...
8170 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8176 * When given the '-v' option, we print out the complete config. If the '-e'
8177 * option is specified, then we print out error rate information as well.
8180 status_callback(zpool_handle_t
*zhp
, void *data
)
8182 status_cbdata_t
*cbp
= data
;
8183 nvlist_t
*config
, *nvroot
;
8185 zpool_status_t reason
;
8186 zpool_errata_t errata
;
8191 config
= zpool_get_config(zhp
, NULL
);
8192 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
8197 * If we were given 'zpool status -x', only report those pools with
8200 if (cbp
->cb_explain
&&
8201 (reason
== ZPOOL_STATUS_OK
||
8202 reason
== ZPOOL_STATUS_VERSION_OLDER
||
8203 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
8204 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
8205 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
8206 if (!cbp
->cb_allpools
) {
8207 (void) printf(gettext("pool '%s' is healthy\n"),
8208 zpool_get_name(zhp
));
8210 cbp
->cb_first
= B_FALSE
;
8216 cbp
->cb_first
= B_FALSE
;
8218 (void) printf("\n");
8220 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8221 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8222 (uint64_t **)&vs
, &c
) == 0);
8224 health
= zpool_get_state_str(zhp
);
8227 printf_color(ANSI_BOLD
, gettext("pool:"));
8228 printf(" %s\n", zpool_get_name(zhp
));
8230 printf_color(ANSI_BOLD
, gettext("state: "));
8232 printf_color(health_str_to_color(health
), "%s", health
);
8234 fputc('\n', stdout
);
8237 case ZPOOL_STATUS_MISSING_DEV_R
:
8238 printf_color(ANSI_BOLD
, gettext("status: "));
8239 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8240 "not be opened. Sufficient replicas exist for\n\tthe pool "
8241 "to continue functioning in a degraded state.\n"));
8242 printf_color(ANSI_BOLD
, gettext("action: "));
8243 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8244 "and online it using 'zpool online'.\n"));
8247 case ZPOOL_STATUS_MISSING_DEV_NR
:
8248 printf_color(ANSI_BOLD
, gettext("status: "));
8249 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8250 "not be opened. There are insufficient\n\treplicas for the"
8251 " pool to continue functioning.\n"));
8252 printf_color(ANSI_BOLD
, gettext("action: "));
8253 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8254 "and online it using 'zpool online'.\n"));
8257 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8258 printf_color(ANSI_BOLD
, gettext("status: "));
8259 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8260 "not be used because the label is missing or\n\tinvalid. "
8261 "Sufficient replicas exist for the pool to continue\n\t"
8262 "functioning in a degraded state.\n"));
8263 printf_color(ANSI_BOLD
, gettext("action: "));
8264 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8265 "'zpool replace'.\n"));
8268 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8269 printf_color(ANSI_BOLD
, gettext("status: "));
8270 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8271 "not be used because the label is missing \n\tor invalid. "
8272 "There are insufficient replicas for the pool to "
8273 "continue\n\tfunctioning.\n"));
8274 zpool_explain_recover(zpool_get_handle(zhp
),
8275 zpool_get_name(zhp
), reason
, config
);
8278 case ZPOOL_STATUS_FAILING_DEV
:
8279 printf_color(ANSI_BOLD
, gettext("status: "));
8280 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8281 "experienced an unrecoverable error. An\n\tattempt was "
8282 "made to correct the error. Applications are "
8284 printf_color(ANSI_BOLD
, gettext("action: "));
8285 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8286 "device needs to be replaced, and clear the errors\n\tusing"
8287 " 'zpool clear' or replace the device with 'zpool "
8291 case ZPOOL_STATUS_OFFLINE_DEV
:
8292 printf_color(ANSI_BOLD
, gettext("status: "));
8293 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8294 "been taken offline by the administrator.\n\tSufficient "
8295 "replicas exist for the pool to continue functioning in "
8296 "a\n\tdegraded state.\n"));
8297 printf_color(ANSI_BOLD
, gettext("action: "));
8298 printf_color(ANSI_YELLOW
, gettext("Online the device "
8299 "using 'zpool online' or replace the device with\n\t'zpool "
8303 case ZPOOL_STATUS_REMOVED_DEV
:
8304 printf_color(ANSI_BOLD
, gettext("status: "));
8305 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8306 "been removed by the administrator.\n\tSufficient "
8307 "replicas exist for the pool to continue functioning in "
8308 "a\n\tdegraded state.\n"));
8309 printf_color(ANSI_BOLD
, gettext("action: "));
8310 printf_color(ANSI_YELLOW
, gettext("Online the device "
8311 "using zpool online' or replace the device with\n\t'zpool "
8315 case ZPOOL_STATUS_RESILVERING
:
8316 case ZPOOL_STATUS_REBUILDING
:
8317 printf_color(ANSI_BOLD
, gettext("status: "));
8318 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8319 "currently being resilvered. The pool will\n\tcontinue "
8320 "to function, possibly in a degraded state.\n"));
8321 printf_color(ANSI_BOLD
, gettext("action: "));
8322 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8326 case ZPOOL_STATUS_REBUILD_SCRUB
:
8327 printf_color(ANSI_BOLD
, gettext("status: "));
8328 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8329 "been sequentially resilvered, scrubbing\n\tthe pool "
8330 "is recommended.\n"));
8331 printf_color(ANSI_BOLD
, gettext("action: "));
8332 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8333 "verify all data checksums.\n"));
8336 case ZPOOL_STATUS_CORRUPT_DATA
:
8337 printf_color(ANSI_BOLD
, gettext("status: "));
8338 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8339 "experienced an error resulting in data\n\tcorruption. "
8340 "Applications may be affected.\n"));
8341 printf_color(ANSI_BOLD
, gettext("action: "));
8342 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8343 " if possible. Otherwise restore the\n\tentire pool from "
8347 case ZPOOL_STATUS_CORRUPT_POOL
:
8348 printf_color(ANSI_BOLD
, gettext("status: "));
8349 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8350 "corrupted and the pool cannot be opened.\n"));
8351 zpool_explain_recover(zpool_get_handle(zhp
),
8352 zpool_get_name(zhp
), reason
, config
);
8355 case ZPOOL_STATUS_VERSION_OLDER
:
8356 printf_color(ANSI_BOLD
, gettext("status: "));
8357 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8358 "a legacy on-disk format. The pool can\n\tstill be used, "
8359 "but some features are unavailable.\n"));
8360 printf_color(ANSI_BOLD
, gettext("action: "));
8361 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8362 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8363 "longer be accessible on software that does not support\n\t"
8364 "feature flags.\n"));
8367 case ZPOOL_STATUS_VERSION_NEWER
:
8368 printf_color(ANSI_BOLD
, gettext("status: "));
8369 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8370 "to a newer, incompatible on-disk version.\n\tThe pool "
8371 "cannot be accessed on this system.\n"));
8372 printf_color(ANSI_BOLD
, gettext("action: "));
8373 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8374 "system running more recent software, or\n\trestore the "
8375 "pool from backup.\n"));
8378 case ZPOOL_STATUS_FEAT_DISABLED
:
8379 printf_color(ANSI_BOLD
, gettext("status: "));
8380 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8381 "requested features are not enabled on the pool.\n\t"
8382 "The pool can still be used, but some features are "
8384 printf_color(ANSI_BOLD
, gettext("action: "));
8385 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8386 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8387 "longer be accessible by software that does not support\n\t"
8388 "the features. See zpool-features(7) for details.\n"));
8391 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8392 printf_color(ANSI_BOLD
, gettext("status: "));
8393 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8394 "compatibility list specified, but it could not be\n\t"
8395 "read/parsed at this time. The pool can still be used, "
8396 "but this\n\tshould be investigated.\n"));
8397 printf_color(ANSI_BOLD
, gettext("action: "));
8398 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8399 "'compatibility' property against the\n\t"
8400 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8401 ZPOOL_DATA_COMPAT_D
".\n"));
8404 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
8405 printf_color(ANSI_BOLD
, gettext("status: "));
8406 printf_color(ANSI_YELLOW
, gettext("One or more features "
8407 "are enabled on the pool despite not being\n\t"
8408 "requested by the 'compatibility' property.\n"));
8409 printf_color(ANSI_BOLD
, gettext("action: "));
8410 printf_color(ANSI_YELLOW
, gettext("Consider setting "
8411 "'compatibility' to an appropriate value, or\n\t"
8412 "adding needed features to the relevant file in\n\t"
8413 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
8416 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8417 printf_color(ANSI_BOLD
, gettext("status: "));
8418 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8419 "on this system because it uses the\n\tfollowing feature(s)"
8420 " not supported on this system:\n"));
8421 zpool_print_unsup_feat(config
);
8422 (void) printf("\n");
8423 printf_color(ANSI_BOLD
, gettext("action: "));
8424 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8425 "system that supports the required feature(s),\n\tor "
8426 "restore the pool from backup.\n"));
8429 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8430 printf_color(ANSI_BOLD
, gettext("status: "));
8431 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8432 "accessed in read-only mode on this system. It\n\tcannot be"
8433 " accessed in read-write mode because it uses the "
8434 "following\n\tfeature(s) not supported on this system:\n"));
8435 zpool_print_unsup_feat(config
);
8436 (void) printf("\n");
8437 printf_color(ANSI_BOLD
, gettext("action: "));
8438 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8439 "in read-write mode. Import the pool with\n"
8440 "\t\"-o readonly=on\", access the pool from a system that "
8441 "supports the\n\trequired feature(s), or restore the "
8442 "pool from backup.\n"));
8445 case ZPOOL_STATUS_FAULTED_DEV_R
:
8446 printf_color(ANSI_BOLD
, gettext("status: "));
8447 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8448 "faulted in response to persistent errors.\n\tSufficient "
8449 "replicas exist for the pool to continue functioning "
8450 "in a\n\tdegraded state.\n"));
8451 printf_color(ANSI_BOLD
, gettext("action: "));
8452 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8453 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8456 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8457 printf_color(ANSI_BOLD
, gettext("status: "));
8458 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8459 "faulted in response to persistent errors. There are "
8460 "insufficient replicas for the pool to\n\tcontinue "
8462 printf_color(ANSI_BOLD
, gettext("action: "));
8463 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8464 "pool from a backup source. Manually marking the device\n"
8465 "\trepaired using 'zpool clear' may allow some data "
8466 "to be recovered.\n"));
8469 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8470 printf_color(ANSI_BOLD
, gettext("status: "));
8471 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8472 "because multihost writes failed or were delayed;\n\t"
8473 "another system could import the pool undetected.\n"));
8474 printf_color(ANSI_BOLD
, gettext("action: "));
8475 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8476 " are connected, then reboot your system and\n\timport the "
8480 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8481 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8482 printf_color(ANSI_BOLD
, gettext("status: "));
8483 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8484 "faulted in response to IO failures.\n"));
8485 printf_color(ANSI_BOLD
, gettext("action: "));
8486 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8487 "devices are connected, then run 'zpool clear'.\n"));
8490 case ZPOOL_STATUS_BAD_LOG
:
8491 printf_color(ANSI_BOLD
, gettext("status: "));
8492 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8493 "could not be read.\n"
8494 "\tWaiting for administrator intervention to fix the "
8495 "faulted pool.\n"));
8496 printf_color(ANSI_BOLD
, gettext("action: "));
8497 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8498 "device(s) and run 'zpool online',\n"
8499 "\tor ignore the intent log records by running "
8500 "'zpool clear'.\n"));
8503 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8504 (void) printf(gettext("status: One or more devices are "
8505 "configured to use a non-native block size.\n"
8506 "\tExpect reduced performance.\n"));
8507 (void) printf(gettext("action: Replace affected devices with "
8508 "devices that support the\n\tconfigured block size, or "
8509 "migrate data to a properly configured\n\tpool.\n"));
8512 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8513 printf_color(ANSI_BOLD
, gettext("status: "));
8514 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8515 " and system hostid on imported pool.\n\tThis pool was "
8516 "previously imported into a system with a different "
8517 "hostid,\n\tand then was verbatim imported into this "
8519 printf_color(ANSI_BOLD
, gettext("action: "));
8520 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8521 "systems on which it is imported.\n"
8522 "\tThen import it to correct the mismatch.\n"));
8525 case ZPOOL_STATUS_ERRATA
:
8526 printf_color(ANSI_BOLD
, gettext("status: "));
8527 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8531 case ZPOOL_ERRATA_NONE
:
8534 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8535 printf_color(ANSI_BOLD
, gettext("action: "));
8536 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8537 " run 'zpool scrub'.\n"));
8540 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8541 (void) printf(gettext("\tExisting encrypted datasets "
8542 "contain an on-disk incompatibility\n\twhich "
8543 "needs to be corrected.\n"));
8544 printf_color(ANSI_BOLD
, gettext("action: "));
8545 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8546 " backup existing encrypted datasets to new\n\t"
8547 "encrypted datasets and destroy the old ones. "
8548 "'zfs mount -o ro' can\n\tbe used to temporarily "
8549 "mount existing encrypted datasets readonly.\n"));
8552 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8553 (void) printf(gettext("\tExisting encrypted snapshots "
8554 "and bookmarks contain an on-disk\n\tincompat"
8555 "ibility. This may cause on-disk corruption if "
8556 "they are used\n\twith 'zfs recv'.\n"));
8557 printf_color(ANSI_BOLD
, gettext("action: "));
8558 printf_color(ANSI_YELLOW
, gettext("To correct the"
8559 "issue, enable the bookmark_v2 feature. No "
8560 "additional\n\taction is needed if there are no "
8561 "encrypted snapshots or bookmarks.\n\tIf preserving"
8562 "the encrypted snapshots and bookmarks is required,"
8563 " use\n\ta non-raw send to backup and restore them."
8564 " Alternately, they may be\n\tremoved to resolve "
8565 "the incompatibility.\n"));
8570 * All errata which allow the pool to be imported
8571 * must contain an action message.
8579 * The remaining errors can't actually be generated, yet.
8581 assert(reason
== ZPOOL_STATUS_OK
);
8584 if (msgid
!= NULL
) {
8586 printf_color(ANSI_BOLD
, gettext("see:"));
8588 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8592 if (config
!= NULL
) {
8594 nvlist_t
**spares
, **l2cache
;
8595 uint_t nspares
, nl2cache
;
8596 pool_checkpoint_stat_t
*pcs
= NULL
;
8597 pool_removal_stat_t
*prs
= NULL
;
8599 print_scan_status(zhp
, nvroot
);
8601 (void) nvlist_lookup_uint64_array(nvroot
,
8602 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8603 print_removal_status(zhp
, prs
);
8605 (void) nvlist_lookup_uint64_array(nvroot
,
8606 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8607 print_checkpoint_status(pcs
);
8609 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8610 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8611 if (cbp
->cb_namewidth
< 10)
8612 cbp
->cb_namewidth
= 10;
8614 color_start(ANSI_BOLD
);
8615 (void) printf(gettext("config:\n\n"));
8616 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8617 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8621 if (cbp
->cb_print_slow_ios
) {
8622 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8625 if (cbp
->vcdl
!= NULL
)
8626 print_cmd_columns(cbp
->vcdl
, 0);
8630 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8633 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8634 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8635 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8637 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8638 &l2cache
, &nl2cache
) == 0)
8639 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8641 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8642 &spares
, &nspares
) == 0)
8643 print_spares(zhp
, cbp
, spares
, nspares
);
8645 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8647 (void) printf("\n");
8649 (void) printf(gettext(
8650 "errors: No known data errors\n"));
8651 } else if (!cbp
->cb_verbose
) {
8652 (void) printf(gettext("errors: %llu data "
8653 "errors, use '-v' for a list\n"),
8654 (u_longlong_t
)nerr
);
8656 print_error_log(zhp
);
8660 if (cbp
->cb_dedup_stats
)
8661 print_dedup_stats(config
);
8663 (void) printf(gettext("config: The configuration cannot be "
8671 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8672 * [interval [count]]
8674 * -c CMD For each vdev, run command CMD
8675 * -i Display vdev initialization status.
8676 * -g Display guid for individual vdev name.
8677 * -L Follow links when resolving vdev path name.
8678 * -p Display values in parsable (exact) format.
8679 * -P Display full path for vdev name.
8680 * -s Display slow IOs column.
8681 * -v Display complete error logs
8682 * -x Display only pools with potential problems
8683 * -D Display dedup status (undocumented)
8684 * -t Display vdev TRIM status.
8685 * -T Display a timestamp in date(1) or Unix format
8687 * Describes the health status of all pools or some subset.
8690 zpool_do_status(int argc
, char **argv
)
8695 unsigned long count
= 0;
8696 status_cbdata_t cb
= { 0 };
8700 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8705 gettext("Can't set -c flag twice\n"));
8709 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8710 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8711 fprintf(stderr
, gettext(
8712 "Can't run -c, disabled by "
8713 "ZPOOL_SCRIPTS_ENABLED.\n"));
8717 if ((getuid() <= 0 || geteuid() <= 0) &&
8718 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8719 fprintf(stderr
, gettext(
8720 "Can't run -c with root privileges "
8721 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8727 cb
.cb_print_vdev_init
= B_TRUE
;
8730 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8733 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8736 cb
.cb_literal
= B_TRUE
;
8739 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8742 cb
.cb_print_slow_ios
= B_TRUE
;
8745 cb
.cb_verbose
= B_TRUE
;
8748 cb
.cb_explain
= B_TRUE
;
8751 cb
.cb_dedup_stats
= B_TRUE
;
8754 cb
.cb_print_vdev_trim
= B_TRUE
;
8757 get_timestamp_arg(*optarg
);
8760 if (optopt
== 'c') {
8761 print_zpool_script_list("status");
8765 gettext("invalid option '%c'\n"), optopt
);
8774 get_interval_count(&argc
, argv
, &interval
, &count
);
8777 cb
.cb_allpools
= B_TRUE
;
8779 cb
.cb_first
= B_TRUE
;
8780 cb
.cb_print_status
= B_TRUE
;
8783 if (timestamp_fmt
!= NODATE
)
8784 print_timestamp(timestamp_fmt
);
8787 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8790 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
8791 cb
.cb_literal
, status_callback
, &cb
);
8793 if (cb
.vcdl
!= NULL
)
8794 free_vdev_cmd_data_list(cb
.vcdl
);
8796 if (argc
== 0 && cb
.cb_count
== 0)
8797 (void) fprintf(stderr
, gettext("no pools available\n"));
8798 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8799 (void) printf(gettext("all pools are healthy\n"));
8807 if (count
!= 0 && --count
== 0)
8810 (void) fsleep(interval
);
8816 typedef struct upgrade_cbdata
{
8819 uint64_t cb_version
;
8824 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8826 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8827 int *count
= (int *)unsupp_fs
;
8829 if (zfs_version
> ZPL_VERSION
) {
8830 (void) printf(gettext("%s (v%d) is not supported by this "
8831 "implementation of ZFS.\n"),
8832 zfs_get_name(zhp
), zfs_version
);
8836 zfs_iter_filesystems(zhp
, 0, check_unsupp_fs
, unsupp_fs
);
8844 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8848 uint64_t oldversion
;
8851 config
= zpool_get_config(zhp
, NULL
);
8852 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8855 char compat
[ZFS_MAXPROPLEN
];
8856 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8857 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8860 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8861 assert(oldversion
< version
);
8863 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8868 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8869 "to %d unsupported filesystems (max v%d).\n"),
8870 unsupp_fs
, (int)ZPL_VERSION
);
8874 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
8875 (void) fprintf(stderr
, gettext("Upgrade not performed because "
8876 "'compatibility' property set to '"
8877 ZPOOL_COMPAT_LEGACY
"'.\n"));
8881 ret
= zpool_upgrade(zhp
, version
);
8885 if (version
>= SPA_VERSION_FEATURES
) {
8886 (void) printf(gettext("Successfully upgraded "
8887 "'%s' from version %llu to feature flags.\n"),
8888 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8890 (void) printf(gettext("Successfully upgraded "
8891 "'%s' from version %llu to version %llu.\n"),
8892 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8893 (u_longlong_t
)version
);
8900 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8903 boolean_t firstff
= B_TRUE
;
8904 nvlist_t
*enabled
= zpool_get_features(zhp
);
8906 char compat
[ZFS_MAXPROPLEN
];
8907 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8908 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8911 boolean_t requested_features
[SPA_FEATURES
];
8912 if (zpool_do_load_compat(compat
, requested_features
) !=
8913 ZPOOL_COMPATIBILITY_OK
)
8917 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8918 const char *fname
= spa_feature_table
[i
].fi_uname
;
8919 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8921 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8924 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
8926 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8927 ret
= zpool_set_prop(zhp
, propname
,
8928 ZFS_FEATURE_ENABLED
);
8936 (void) printf(gettext("Enabled the "
8937 "following features on '%s':\n"),
8938 zpool_get_name(zhp
));
8941 (void) printf(gettext(" %s\n"), fname
);
8952 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8954 upgrade_cbdata_t
*cbp
= arg
;
8957 boolean_t modified_pool
= B_FALSE
;
8960 config
= zpool_get_config(zhp
, NULL
);
8961 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8964 assert(SPA_VERSION_IS_SUPPORTED(version
));
8966 if (version
< cbp
->cb_version
) {
8967 cbp
->cb_first
= B_FALSE
;
8968 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8971 modified_pool
= B_TRUE
;
8974 * If they did "zpool upgrade -a", then we could
8975 * be doing ioctls to different pools. We need
8976 * to log this history once to each pool, and bypass
8977 * the normal history logging that happens in main().
8979 (void) zpool_log_history(g_zfs
, history_str
);
8980 log_history
= B_FALSE
;
8983 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8985 ret
= upgrade_enable_all(zhp
, &count
);
8990 cbp
->cb_first
= B_FALSE
;
8991 modified_pool
= B_TRUE
;
8995 if (modified_pool
) {
8996 (void) printf("\n");
8997 (void) after_zpool_upgrade(zhp
);
9004 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
9006 upgrade_cbdata_t
*cbp
= arg
;
9010 config
= zpool_get_config(zhp
, NULL
);
9011 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9014 assert(SPA_VERSION_IS_SUPPORTED(version
));
9016 if (version
< SPA_VERSION_FEATURES
) {
9017 if (cbp
->cb_first
) {
9018 (void) printf(gettext("The following pools are "
9019 "formatted with legacy version numbers and can\n"
9020 "be upgraded to use feature flags. After "
9021 "being upgraded, these pools\nwill no "
9022 "longer be accessible by software that does not "
9023 "support feature\nflags.\n\n"
9024 "Note that setting a pool's 'compatibility' "
9025 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
9026 "inhibit upgrades.\n\n"));
9027 (void) printf(gettext("VER POOL\n"));
9028 (void) printf(gettext("--- ------------\n"));
9029 cbp
->cb_first
= B_FALSE
;
9032 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
9033 zpool_get_name(zhp
));
9040 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
9042 upgrade_cbdata_t
*cbp
= arg
;
9046 config
= zpool_get_config(zhp
, NULL
);
9047 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9050 if (version
>= SPA_VERSION_FEATURES
) {
9052 boolean_t poolfirst
= B_TRUE
;
9053 nvlist_t
*enabled
= zpool_get_features(zhp
);
9055 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9056 const char *fguid
= spa_feature_table
[i
].fi_guid
;
9057 const char *fname
= spa_feature_table
[i
].fi_uname
;
9059 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
9062 if (!nvlist_exists(enabled
, fguid
)) {
9063 if (cbp
->cb_first
) {
9064 (void) printf(gettext("\nSome "
9065 "supported features are not "
9066 "enabled on the following pools. "
9067 "Once a\nfeature is enabled the "
9068 "pool may become incompatible with "
9069 "software\nthat does not support "
9071 "zpool-features(7) for "
9073 "Note that the pool "
9074 "'compatibility' feature can be "
9075 "used to inhibit\nfeature "
9077 (void) printf(gettext("POOL "
9079 (void) printf(gettext("------"
9081 cbp
->cb_first
= B_FALSE
;
9085 (void) printf(gettext("%s\n"),
9086 zpool_get_name(zhp
));
9087 poolfirst
= B_FALSE
;
9090 (void) printf(gettext(" %s\n"), fname
);
9093 * If they did "zpool upgrade -a", then we could
9094 * be doing ioctls to different pools. We need
9095 * to log this history once to each pool, and bypass
9096 * the normal history logging that happens in main().
9098 (void) zpool_log_history(g_zfs
, history_str
);
9099 log_history
= B_FALSE
;
9107 upgrade_one(zpool_handle_t
*zhp
, void *data
)
9109 boolean_t modified_pool
= B_FALSE
;
9110 upgrade_cbdata_t
*cbp
= data
;
9111 uint64_t cur_version
;
9114 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
9115 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
9116 "Pool 'log' must be renamed using export and import"
9121 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
9122 if (cur_version
> cbp
->cb_version
) {
9123 (void) printf(gettext("Pool '%s' is already formatted "
9124 "using more current version '%llu'.\n\n"),
9125 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
9129 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
9130 (void) printf(gettext("Pool '%s' is already formatted "
9131 "using version %llu.\n\n"), zpool_get_name(zhp
),
9132 (u_longlong_t
)cbp
->cb_version
);
9136 if (cur_version
!= cbp
->cb_version
) {
9137 modified_pool
= B_TRUE
;
9138 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9143 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9145 ret
= upgrade_enable_all(zhp
, &count
);
9150 modified_pool
= B_TRUE
;
9151 } else if (cur_version
== SPA_VERSION
) {
9152 (void) printf(gettext("Pool '%s' already has all "
9153 "supported and requested features enabled.\n"),
9154 zpool_get_name(zhp
));
9158 if (modified_pool
) {
9159 (void) printf("\n");
9160 (void) after_zpool_upgrade(zhp
);
9169 * zpool upgrade [-V version] <-a | pool ...>
9171 * With no arguments, display downrev'd ZFS pool available for upgrade.
9172 * Individual pools can be upgraded by specifying the pool, and '-a' will
9173 * upgrade all pools.
9176 zpool_do_upgrade(int argc
, char **argv
)
9179 upgrade_cbdata_t cb
= { 0 };
9181 boolean_t showversions
= B_FALSE
;
9182 boolean_t upgradeall
= B_FALSE
;
9187 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
9190 upgradeall
= B_TRUE
;
9193 showversions
= B_TRUE
;
9196 cb
.cb_version
= strtoll(optarg
, &end
, 10);
9198 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
9199 (void) fprintf(stderr
,
9200 gettext("invalid version '%s'\n"), optarg
);
9205 (void) fprintf(stderr
, gettext("missing argument for "
9206 "'%c' option\n"), optopt
);
9210 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9221 if (cb
.cb_version
== 0) {
9222 cb
.cb_version
= SPA_VERSION
;
9223 } else if (!upgradeall
&& argc
== 0) {
9224 (void) fprintf(stderr
, gettext("-V option is "
9225 "incompatible with other arguments\n"));
9230 if (upgradeall
|| argc
!= 0) {
9231 (void) fprintf(stderr
, gettext("-v option is "
9232 "incompatible with other arguments\n"));
9235 } else if (upgradeall
) {
9237 (void) fprintf(stderr
, gettext("-a option should not "
9238 "be used along with a pool name\n"));
9243 (void) printf("%s", gettext("This system supports ZFS pool feature "
9248 (void) printf(gettext("The following features are "
9250 (void) printf(gettext("FEAT DESCRIPTION\n"));
9251 (void) printf("----------------------------------------------"
9252 "---------------\n");
9253 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9254 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9255 if (!fi
->fi_zfs_mod_supported
)
9258 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9259 " (read-only compatible)" : "";
9261 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9262 (void) printf(" %s\n", fi
->fi_desc
);
9264 (void) printf("\n");
9266 (void) printf(gettext("The following legacy versions are also "
9268 (void) printf(gettext("VER DESCRIPTION\n"));
9269 (void) printf("--- -----------------------------------------"
9270 "---------------\n");
9271 (void) printf(gettext(" 1 Initial ZFS version\n"));
9272 (void) printf(gettext(" 2 Ditto blocks "
9273 "(replicated metadata)\n"));
9274 (void) printf(gettext(" 3 Hot spares and double parity "
9276 (void) printf(gettext(" 4 zpool history\n"));
9277 (void) printf(gettext(" 5 Compression using the gzip "
9279 (void) printf(gettext(" 6 bootfs pool property\n"));
9280 (void) printf(gettext(" 7 Separate intent log devices\n"));
9281 (void) printf(gettext(" 8 Delegated administration\n"));
9282 (void) printf(gettext(" 9 refquota and refreservation "
9284 (void) printf(gettext(" 10 Cache devices\n"));
9285 (void) printf(gettext(" 11 Improved scrub performance\n"));
9286 (void) printf(gettext(" 12 Snapshot properties\n"));
9287 (void) printf(gettext(" 13 snapused property\n"));
9288 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9289 (void) printf(gettext(" 15 user/group space accounting\n"));
9290 (void) printf(gettext(" 16 stmf property support\n"));
9291 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9292 (void) printf(gettext(" 18 Snapshot user holds\n"));
9293 (void) printf(gettext(" 19 Log device removal\n"));
9294 (void) printf(gettext(" 20 Compression using zle "
9295 "(zero-length encoding)\n"));
9296 (void) printf(gettext(" 21 Deduplication\n"));
9297 (void) printf(gettext(" 22 Received properties\n"));
9298 (void) printf(gettext(" 23 Slim ZIL\n"));
9299 (void) printf(gettext(" 24 System attributes\n"));
9300 (void) printf(gettext(" 25 Improved scrub stats\n"));
9301 (void) printf(gettext(" 26 Improved snapshot deletion "
9303 (void) printf(gettext(" 27 Improved snapshot creation "
9305 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9306 (void) printf(gettext("\nFor more information on a particular "
9307 "version, including supported releases,\n"));
9308 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9309 } else if (argc
== 0 && upgradeall
) {
9310 cb
.cb_first
= B_TRUE
;
9311 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9312 if (ret
== 0 && cb
.cb_first
) {
9313 if (cb
.cb_version
== SPA_VERSION
) {
9314 (void) printf(gettext("All pools are already "
9315 "formatted using feature flags.\n\n"));
9316 (void) printf(gettext("Every feature flags "
9317 "pool already has all supported and "
9318 "requested features enabled.\n"));
9320 (void) printf(gettext("All pools are already "
9321 "formatted with version %llu or higher.\n"),
9322 (u_longlong_t
)cb
.cb_version
);
9325 } else if (argc
== 0) {
9326 cb
.cb_first
= B_TRUE
;
9327 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9331 (void) printf(gettext("All pools are formatted "
9332 "using feature flags.\n\n"));
9334 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9335 "for a list of available legacy versions.\n"));
9338 cb
.cb_first
= B_TRUE
;
9339 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9343 (void) printf(gettext("Every feature flags pool has "
9344 "all supported and requested features enabled.\n"));
9346 (void) printf(gettext("\n"));
9349 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9350 B_FALSE
, upgrade_one
, &cb
);
9356 typedef struct hist_cbdata
{
9363 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9369 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9370 &records
, &numrecords
) == 0);
9371 for (i
= 0; i
< numrecords
; i
++) {
9372 nvlist_t
*rec
= records
[i
];
9375 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9379 tsec
= fnvlist_lookup_uint64(records
[i
],
9381 (void) localtime_r(&tsec
, &t
);
9382 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9385 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9386 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9387 ZPOOL_HIST_ELAPSED_NS
);
9388 (void) snprintf(tbuf
+ strlen(tbuf
),
9389 sizeof (tbuf
) - strlen(tbuf
),
9390 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9393 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9394 (void) printf("%s %s", tbuf
,
9395 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9396 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9398 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9401 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9402 (void) printf("%s unrecognized record:\n",
9404 dump_nvlist(rec
, 4);
9407 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9408 zfs_history_event_names
[ievent
],
9409 (longlong_t
)fnvlist_lookup_uint64(
9410 rec
, ZPOOL_HIST_TXG
),
9411 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9412 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9415 (void) printf("%s [txg:%lld] %s", tbuf
,
9416 (longlong_t
)fnvlist_lookup_uint64(
9417 rec
, ZPOOL_HIST_TXG
),
9418 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9419 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9420 (void) printf(" %s (%llu)",
9421 fnvlist_lookup_string(rec
,
9423 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9426 (void) printf(" %s", fnvlist_lookup_string(rec
,
9427 ZPOOL_HIST_INT_STR
));
9428 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9431 (void) printf("%s ioctl %s\n", tbuf
,
9432 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9433 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9434 (void) printf(" input:\n");
9435 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9436 ZPOOL_HIST_INPUT_NVL
), 8);
9438 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9439 (void) printf(" output:\n");
9440 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9441 ZPOOL_HIST_OUTPUT_NVL
), 8);
9443 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9444 (void) printf(" output nvlist omitted; "
9445 "original size: %lldKB\n",
9446 (longlong_t
)fnvlist_lookup_int64(rec
,
9447 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9449 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9450 (void) printf(" errno: %lld\n",
9451 (longlong_t
)fnvlist_lookup_int64(rec
,
9457 (void) printf("%s unrecognized record:\n", tbuf
);
9458 dump_nvlist(rec
, 4);
9462 (void) printf("\n");
9465 (void) printf(" [");
9466 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9467 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9468 struct passwd
*pwd
= getpwuid(who
);
9469 (void) printf("user %d ", (int)who
);
9471 (void) printf("(%s) ", pwd
->pw_name
);
9473 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9474 (void) printf("on %s",
9475 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9477 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9478 (void) printf(":%s",
9479 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9483 (void) printf("\n");
9488 * Print out the command history for a specific pool.
9491 get_history_one(zpool_handle_t
*zhp
, void *data
)
9495 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9497 boolean_t eof
= B_FALSE
;
9499 cb
->first
= B_FALSE
;
9501 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9504 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9507 print_history_records(nvhis
, cb
);
9510 (void) printf("\n");
9516 * zpool history <pool>
9518 * Displays the history of commands that modified pools.
9521 zpool_do_history(int argc
, char **argv
)
9523 hist_cbdata_t cbdata
= { 0 };
9527 cbdata
.first
= B_TRUE
;
9529 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9532 cbdata
.longfmt
= B_TRUE
;
9535 cbdata
.internal
= B_TRUE
;
9538 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9546 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9547 B_FALSE
, get_history_one
, &cbdata
);
9549 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9550 (void) fprintf(stderr
, gettext("no pools available\n"));
9557 typedef struct ev_opts
{
9562 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9566 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9568 char ctime_str
[26], str
[32];
9573 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9574 memset(str
, ' ', 32);
9575 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9576 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9577 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9578 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9579 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9581 (void) printf(gettext("%s\t"), str
);
9583 (void) printf(gettext("%s "), str
);
9585 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9586 (void) printf(gettext("%s\n"), ptr
);
9590 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9594 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9595 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9597 data_type_t type
= nvpair_type(nvp
);
9598 const char *name
= nvpair_name(nvp
);
9608 printf(gettext("%*s%s = "), depth
, "", name
);
9611 case DATA_TYPE_BOOLEAN
:
9612 printf(gettext("%s"), "1");
9615 case DATA_TYPE_BOOLEAN_VALUE
:
9616 (void) nvpair_value_boolean_value(nvp
, &b
);
9617 printf(gettext("%s"), b
? "1" : "0");
9620 case DATA_TYPE_BYTE
:
9621 (void) nvpair_value_byte(nvp
, &i8
);
9622 printf(gettext("0x%x"), i8
);
9625 case DATA_TYPE_INT8
:
9626 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9627 printf(gettext("0x%x"), i8
);
9630 case DATA_TYPE_UINT8
:
9631 (void) nvpair_value_uint8(nvp
, &i8
);
9632 printf(gettext("0x%x"), i8
);
9635 case DATA_TYPE_INT16
:
9636 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9637 printf(gettext("0x%x"), i16
);
9640 case DATA_TYPE_UINT16
:
9641 (void) nvpair_value_uint16(nvp
, &i16
);
9642 printf(gettext("0x%x"), i16
);
9645 case DATA_TYPE_INT32
:
9646 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9647 printf(gettext("0x%x"), i32
);
9650 case DATA_TYPE_UINT32
:
9651 (void) nvpair_value_uint32(nvp
, &i32
);
9652 printf(gettext("0x%x"), i32
);
9655 case DATA_TYPE_INT64
:
9656 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9657 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9660 case DATA_TYPE_UINT64
:
9661 (void) nvpair_value_uint64(nvp
, &i64
);
9663 * translate vdev state values to readable
9664 * strings to aide zpool events consumers
9667 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9669 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9670 printf(gettext("\"%s\" (0x%llx)"),
9671 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9674 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9678 case DATA_TYPE_HRTIME
:
9679 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9680 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9683 case DATA_TYPE_STRING
:
9684 (void) nvpair_value_string(nvp
, &str
);
9685 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9688 case DATA_TYPE_NVLIST
:
9689 printf(gettext("(embedded nvlist)\n"));
9690 (void) nvpair_value_nvlist(nvp
, &cnv
);
9691 zpool_do_events_nvprint(cnv
, depth
+ 8);
9692 printf(gettext("%*s(end %s)"), depth
, "", name
);
9695 case DATA_TYPE_NVLIST_ARRAY
: {
9699 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9700 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9701 for (i
= 0; i
< nelem
; i
++) {
9702 printf(gettext("%*s%s[%d] = %s\n"),
9703 depth
, "", name
, i
, "(embedded nvlist)");
9704 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9705 printf(gettext("%*s(end %s[%i])\n"),
9706 depth
, "", name
, i
);
9708 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9712 case DATA_TYPE_INT8_ARRAY
: {
9716 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9717 for (i
= 0; i
< nelem
; i
++)
9718 printf(gettext("0x%x "), val
[i
]);
9723 case DATA_TYPE_UINT8_ARRAY
: {
9727 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9728 for (i
= 0; i
< nelem
; i
++)
9729 printf(gettext("0x%x "), val
[i
]);
9734 case DATA_TYPE_INT16_ARRAY
: {
9738 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9739 for (i
= 0; i
< nelem
; i
++)
9740 printf(gettext("0x%x "), val
[i
]);
9745 case DATA_TYPE_UINT16_ARRAY
: {
9749 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9750 for (i
= 0; i
< nelem
; i
++)
9751 printf(gettext("0x%x "), val
[i
]);
9756 case DATA_TYPE_INT32_ARRAY
: {
9760 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9761 for (i
= 0; i
< nelem
; i
++)
9762 printf(gettext("0x%x "), val
[i
]);
9767 case DATA_TYPE_UINT32_ARRAY
: {
9771 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9772 for (i
= 0; i
< nelem
; i
++)
9773 printf(gettext("0x%x "), val
[i
]);
9778 case DATA_TYPE_INT64_ARRAY
: {
9782 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9783 for (i
= 0; i
< nelem
; i
++)
9784 printf(gettext("0x%llx "),
9785 (u_longlong_t
)val
[i
]);
9790 case DATA_TYPE_UINT64_ARRAY
: {
9794 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9795 for (i
= 0; i
< nelem
; i
++)
9796 printf(gettext("0x%llx "),
9797 (u_longlong_t
)val
[i
]);
9802 case DATA_TYPE_STRING_ARRAY
: {
9806 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9807 for (i
= 0; i
< nelem
; i
++)
9808 printf(gettext("\"%s\" "),
9809 str
[i
] ? str
[i
] : "<NULL>");
9814 case DATA_TYPE_BOOLEAN_ARRAY
:
9815 case DATA_TYPE_BYTE_ARRAY
:
9816 case DATA_TYPE_DOUBLE
:
9817 case DATA_TYPE_DONTCARE
:
9818 case DATA_TYPE_UNKNOWN
:
9819 printf(gettext("<unknown>"));
9823 printf(gettext("\n"));
9828 zpool_do_events_next(ev_opts_t
*opts
)
9831 int zevent_fd
, ret
, dropped
;
9834 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9835 VERIFY(zevent_fd
>= 0);
9837 if (!opts
->scripted
)
9838 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9841 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9842 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9843 if (ret
|| nvl
== NULL
)
9847 (void) printf(gettext("dropped %d events\n"), dropped
);
9849 if (strlen(opts
->poolname
) > 0 &&
9850 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9851 strcmp(opts
->poolname
, pool
) != 0)
9854 zpool_do_events_short(nvl
, opts
);
9856 if (opts
->verbose
) {
9857 zpool_do_events_nvprint(nvl
, 8);
9858 printf(gettext("\n"));
9860 (void) fflush(stdout
);
9865 VERIFY(0 == close(zevent_fd
));
9871 zpool_do_events_clear(void)
9875 ret
= zpool_events_clear(g_zfs
, &count
);
9877 (void) printf(gettext("cleared %d events\n"), count
);
9883 * zpool events [-vHf [pool] | -c]
9885 * Displays events logs by ZFS.
9888 zpool_do_events(int argc
, char **argv
)
9890 ev_opts_t opts
= { 0 };
9895 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9910 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9919 (void) fprintf(stderr
, gettext("too many arguments\n"));
9921 } else if (argc
== 1) {
9922 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9923 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9924 (void) fprintf(stderr
,
9925 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9930 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9932 (void) fprintf(stderr
,
9933 gettext("invalid options combined with -c\n"));
9938 ret
= zpool_do_events_clear();
9940 ret
= zpool_do_events_next(&opts
);
9946 get_callback_vdev(zpool_handle_t
*zhp
, char *vdevname
, void *data
)
9948 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9949 char value
[ZFS_MAXPROPLEN
];
9950 zprop_source_t srctype
;
9952 for (zprop_list_t
*pl
= cbp
->cb_proplist
; pl
!= NULL
;
9956 * If the first property is pool name, it is a special
9957 * placeholder that we can skip. This will also skip
9958 * over the name property when 'all' is specified.
9960 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9961 pl
== cbp
->cb_proplist
)
9964 if (pl
->pl_prop
== ZPROP_INVAL
) {
9965 prop_name
= pl
->pl_user_prop
;
9967 prop_name
= (char *)vdev_prop_to_name(pl
->pl_prop
);
9969 if (zpool_get_vdev_prop(zhp
, vdevname
, pl
->pl_prop
,
9970 prop_name
, value
, sizeof (value
), &srctype
,
9971 cbp
->cb_literal
) == 0) {
9972 zprop_print_one_property(vdevname
, cbp
, prop_name
,
9973 value
, srctype
, NULL
, NULL
);
9981 get_callback_vdev_width_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9983 zpool_handle_t
*zhp
= zhp_data
;
9984 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9985 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
9986 cbp
->cb_vdevs
.cb_name_flags
);
9989 /* Adjust the column widths for the vdev properties */
9990 ret
= vdev_expand_proplist(zhp
, vdevname
, &cbp
->cb_proplist
);
9996 get_callback_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
9998 zpool_handle_t
*zhp
= zhp_data
;
9999 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
10000 char *vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
10001 cbp
->cb_vdevs
.cb_name_flags
);
10004 /* Display the properties */
10005 ret
= get_callback_vdev(zhp
, vdevname
, data
);
10011 get_callback(zpool_handle_t
*zhp
, void *data
)
10013 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
10014 char value
[MAXNAMELEN
];
10015 zprop_source_t srctype
;
10019 if (cbp
->cb_type
== ZFS_TYPE_VDEV
) {
10020 if (strcmp(cbp
->cb_vdevs
.cb_names
[0], "all-vdevs") == 0) {
10021 for_each_vdev(zhp
, get_callback_vdev_width_cb
, data
);
10022 for_each_vdev(zhp
, get_callback_vdev_cb
, data
);
10024 /* Adjust column widths for vdev properties */
10025 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10027 vdev_expand_proplist(zhp
,
10028 cbp
->cb_vdevs
.cb_names
[vid
],
10029 &cbp
->cb_proplist
);
10031 /* Display the properties */
10032 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10034 get_callback_vdev(zhp
,
10035 cbp
->cb_vdevs
.cb_names
[vid
], data
);
10039 assert(cbp
->cb_type
== ZFS_TYPE_POOL
);
10040 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
10042 * Skip the special fake placeholder. This will also
10043 * skip over the name property when 'all' is specified.
10045 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
10046 pl
== cbp
->cb_proplist
)
10049 if (pl
->pl_prop
== ZPROP_INVAL
&&
10050 (zpool_prop_feature(pl
->pl_user_prop
) ||
10051 zpool_prop_unsupported(pl
->pl_user_prop
))) {
10052 srctype
= ZPROP_SRC_LOCAL
;
10054 if (zpool_prop_get_feature(zhp
,
10055 pl
->pl_user_prop
, value
,
10056 sizeof (value
)) == 0) {
10057 zprop_print_one_property(
10058 zpool_get_name(zhp
), cbp
,
10059 pl
->pl_user_prop
, value
, srctype
,
10063 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
10064 sizeof (value
), &srctype
,
10065 cbp
->cb_literal
) != 0)
10068 zprop_print_one_property(zpool_get_name(zhp
),
10069 cbp
, zpool_prop_to_name(pl
->pl_prop
),
10070 value
, srctype
, NULL
, NULL
);
10079 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10081 * -H Scripted mode. Don't display headers, and separate properties
10083 * -o List of columns to display. Defaults to
10084 * "name,property,value,source".
10085 * -p Display values in parsable (exact) format.
10087 * Get properties of pools in the system. Output space statistics
10088 * for each one as well as other attributes.
10091 zpool_do_get(int argc
, char **argv
)
10093 zprop_get_cbdata_t cb
= { 0 };
10094 zprop_list_t fake_name
= { 0 };
10097 char *propstr
= NULL
;
10099 cb
.cb_first
= B_TRUE
;
10102 * Set up default columns and sources.
10104 cb
.cb_sources
= ZPROP_SRC_ALL
;
10105 cb
.cb_columns
[0] = GET_COL_NAME
;
10106 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
10107 cb
.cb_columns
[2] = GET_COL_VALUE
;
10108 cb
.cb_columns
[3] = GET_COL_SOURCE
;
10109 cb
.cb_type
= ZFS_TYPE_POOL
;
10110 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10111 current_prop_type
= cb
.cb_type
;
10113 /* check options */
10114 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
10117 cb
.cb_literal
= B_TRUE
;
10120 cb
.cb_scripted
= B_TRUE
;
10123 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
10126 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10127 static const char *const col_opts
[] =
10128 { "name", "property", "value", "source",
10130 static const zfs_get_column_t col_cols
[] =
10131 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
10134 if (i
== ZFS_GET_NCOLS
- 1) {
10135 (void) fprintf(stderr
, gettext("too "
10136 "many fields given to -o "
10141 for (c
= 0; c
< ARRAY_SIZE(col_opts
); ++c
)
10142 if (strcmp(tok
, col_opts
[c
]) == 0)
10145 (void) fprintf(stderr
,
10146 gettext("invalid column name '%s'\n"), tok
);
10152 (void) fprintf(stderr
,
10153 gettext("\"all\" conflicts "
10154 "with specific fields "
10155 "given to -o option\n"));
10159 memcpy(cb
.cb_columns
, col_cols
,
10160 sizeof (col_cols
));
10161 i
= ZFS_GET_NCOLS
- 1;
10163 cb
.cb_columns
[i
++] = col_cols
[c
];
10167 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10177 (void) fprintf(stderr
, gettext("missing property "
10182 /* Properties list is needed later by zprop_get_list() */
10189 /* No args, so just print the defaults. */
10190 } else if (are_all_pools(argc
, argv
)) {
10191 /* All the args are pool names */
10192 } else if (are_all_pools(1, argv
)) {
10193 /* The first arg is a pool name */
10194 if ((argc
== 2 && strcmp(argv
[1], "all-vdevs") == 0) ||
10195 are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10197 /* ... and the rest are vdev names */
10198 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10199 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
10200 cb
.cb_type
= ZFS_TYPE_VDEV
;
10201 argc
= 1; /* One pool to process */
10203 fprintf(stderr
, gettext("Expected a list of vdevs in"
10204 " \"%s\", but got:\n"), argv
[0]);
10205 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
10206 argv
[0], &cb
.cb_vdevs
);
10207 fprintf(stderr
, "\n");
10213 * The first arg isn't a pool name,
10215 fprintf(stderr
, gettext("missing pool name.\n"));
10216 fprintf(stderr
, "\n");
10221 if (zprop_get_list(g_zfs
, propstr
, &cb
.cb_proplist
,
10222 cb
.cb_type
) != 0) {
10223 /* Use correct list of valid properties (pool or vdev) */
10224 current_prop_type
= cb
.cb_type
;
10228 if (cb
.cb_proplist
!= NULL
) {
10229 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
10230 fake_name
.pl_width
= strlen(gettext("NAME"));
10231 fake_name
.pl_next
= cb
.cb_proplist
;
10232 cb
.cb_proplist
= &fake_name
;
10235 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_type
,
10236 cb
.cb_literal
, get_callback
, &cb
);
10238 if (cb
.cb_proplist
== &fake_name
)
10239 zprop_free_list(fake_name
.pl_next
);
10241 zprop_free_list(cb
.cb_proplist
);
10246 typedef struct set_cbdata
{
10249 zfs_type_t cb_type
;
10250 vdev_cbdata_t cb_vdevs
;
10251 boolean_t cb_any_successful
;
10255 set_pool_callback(zpool_handle_t
*zhp
, set_cbdata_t
*cb
)
10259 /* Check if we have out-of-bounds features */
10260 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
10261 boolean_t features
[SPA_FEATURES
];
10262 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
10263 ZPOOL_COMPATIBILITY_OK
)
10266 nvlist_t
*enabled
= zpool_get_features(zhp
);
10268 for (i
= 0; i
< SPA_FEATURES
; i
++) {
10269 const char *fguid
= spa_feature_table
[i
].fi_guid
;
10270 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
10273 if (i
< SPA_FEATURES
)
10274 (void) fprintf(stderr
, gettext("Warning: one or "
10275 "more features already enabled on pool '%s'\n"
10276 "are not present in this compatibility set.\n"),
10277 zpool_get_name(zhp
));
10280 /* if we're setting a feature, check it's in compatibility set */
10281 if (zpool_prop_feature(cb
->cb_propname
) &&
10282 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
10283 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
10286 if (zfeature_lookup_name(fname
, &f
) == 0) {
10287 char compat
[ZFS_MAXPROPLEN
];
10288 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
10289 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
10292 boolean_t features
[SPA_FEATURES
];
10293 if (zpool_do_load_compat(compat
, features
) !=
10294 ZPOOL_COMPATIBILITY_OK
) {
10295 (void) fprintf(stderr
, gettext("Error: "
10296 "cannot enable feature '%s' on pool '%s'\n"
10297 "because the pool's 'compatibility' "
10298 "property cannot be parsed.\n"),
10299 fname
, zpool_get_name(zhp
));
10303 if (!features
[f
]) {
10304 (void) fprintf(stderr
, gettext("Error: "
10305 "cannot enable feature '%s' on pool '%s'\n"
10306 "as it is not specified in this pool's "
10307 "current compatibility set.\n"
10308 "Consider setting 'compatibility' to a "
10309 "less restrictive set, or to 'off'.\n"),
10310 fname
, zpool_get_name(zhp
));
10316 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
10322 set_callback(zpool_handle_t
*zhp
, void *data
)
10325 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
10327 if (cb
->cb_type
== ZFS_TYPE_VDEV
) {
10328 error
= zpool_set_vdev_prop(zhp
, *cb
->cb_vdevs
.cb_names
,
10329 cb
->cb_propname
, cb
->cb_value
);
10331 assert(cb
->cb_type
== ZFS_TYPE_POOL
);
10332 error
= set_pool_callback(zhp
, cb
);
10335 cb
->cb_any_successful
= !error
;
10340 zpool_do_set(int argc
, char **argv
)
10342 set_cbdata_t cb
= { 0 };
10345 current_prop_type
= ZFS_TYPE_POOL
;
10346 if (argc
> 1 && argv
[1][0] == '-') {
10347 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10353 (void) fprintf(stderr
, gettext("missing property=value "
10359 (void) fprintf(stderr
, gettext("missing pool name\n"));
10364 (void) fprintf(stderr
, gettext("too many pool names\n"));
10368 cb
.cb_propname
= argv
[1];
10369 cb
.cb_type
= ZFS_TYPE_POOL
;
10370 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10371 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
10372 if (cb
.cb_value
== NULL
) {
10373 (void) fprintf(stderr
, gettext("missing value in "
10374 "property=value argument\n"));
10378 *(cb
.cb_value
) = '\0';
10383 /* argv[0] is pool name */
10384 if (!is_pool(argv
[0])) {
10385 (void) fprintf(stderr
,
10386 gettext("cannot open '%s': is not a pool\n"), argv
[0]);
10390 /* argv[1], when supplied, is vdev name */
10392 if (!are_vdevs_in_pool(1, argv
+ 1, argv
[0], &cb
.cb_vdevs
)) {
10393 (void) fprintf(stderr
, gettext(
10394 "cannot find '%s' in '%s': device not in pool\n"),
10398 cb
.cb_vdevs
.cb_names
= argv
+ 1;
10399 cb
.cb_vdevs
.cb_names_count
= 1;
10400 cb
.cb_type
= ZFS_TYPE_VDEV
;
10403 error
= for_each_pool(1, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
10404 B_FALSE
, set_callback
, &cb
);
10409 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10411 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
10413 uint64_t bytes_remaining
;
10415 uint_t c
, children
;
10418 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
10419 activity
== ZPOOL_WAIT_TRIM
);
10421 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
10422 (uint64_t **)&vs
, &c
) == 0);
10424 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
10425 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
10426 bytes_remaining
= vs
->vs_initialize_bytes_est
-
10427 vs
->vs_initialize_bytes_done
;
10428 else if (activity
== ZPOOL_WAIT_TRIM
&&
10429 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
10430 bytes_remaining
= vs
->vs_trim_bytes_est
-
10431 vs
->vs_trim_bytes_done
;
10433 bytes_remaining
= 0;
10435 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10436 &child
, &children
) != 0)
10439 for (c
= 0; c
< children
; c
++)
10440 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
10442 return (bytes_remaining
);
10445 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10447 vdev_activity_top_remaining(nvlist_t
*nv
)
10449 uint64_t bytes_remaining
= 0;
10454 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10455 &child
, &children
) != 0)
10458 for (uint_t c
= 0; c
< children
; c
++) {
10459 vdev_rebuild_stat_t
*vrs
;
10462 error
= nvlist_lookup_uint64_array(child
[c
],
10463 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
10465 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
10466 bytes_remaining
+= (vrs
->vrs_bytes_est
-
10467 vrs
->vrs_bytes_rebuilt
);
10472 return (bytes_remaining
);
10475 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10477 vdev_any_spare_replacing(nvlist_t
*nv
)
10480 uint_t c
, children
;
10481 const char *vdev_type
;
10483 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
10485 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
10486 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
10487 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
10491 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10492 &child
, &children
) != 0)
10495 for (c
= 0; c
< children
; c
++) {
10496 if (vdev_any_spare_replacing(child
[c
]))
10503 typedef struct wait_data
{
10505 boolean_t wd_scripted
;
10506 boolean_t wd_exact
;
10507 boolean_t wd_headers_once
;
10508 boolean_t wd_should_exit
;
10509 /* Which activities to wait for */
10510 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10512 pthread_cond_t wd_cv
;
10513 pthread_mutex_t wd_mutex
;
10517 * Print to stdout a single line, containing one column for each activity that
10518 * we are waiting for specifying how many bytes of work are left for that
10522 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10524 nvlist_t
*config
, *nvroot
;
10527 pool_checkpoint_stat_t
*pcs
= NULL
;
10528 pool_scan_stat_t
*pss
= NULL
;
10529 pool_removal_stat_t
*prs
= NULL
;
10530 const char *const headers
[] = {"DISCARD", "FREE", "INITIALIZE",
10531 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10532 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10534 /* Calculate the width of each column */
10535 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10537 * Make sure we have enough space in the col for pretty-printed
10538 * numbers and for the column header, and then leave a couple
10539 * spaces between cols for readability.
10541 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10544 /* Print header if appropriate */
10545 int term_height
= terminal_height();
10546 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10547 row
% (term_height
-1) == 0);
10548 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10549 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10550 if (wd
->wd_enabled
[i
])
10551 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10553 (void) fputc('\n', stdout
);
10556 /* Bytes of work remaining in each activity */
10557 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10559 bytes_rem
[ZPOOL_WAIT_FREE
] =
10560 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10562 config
= zpool_get_config(zhp
, NULL
);
10563 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10565 (void) nvlist_lookup_uint64_array(nvroot
,
10566 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10567 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10568 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10570 (void) nvlist_lookup_uint64_array(nvroot
,
10571 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10572 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10573 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10576 (void) nvlist_lookup_uint64_array(nvroot
,
10577 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10578 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10579 pss
->pss_pass_scrub_pause
== 0) {
10580 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10581 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10582 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10584 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10585 } else if (check_rebuilding(nvroot
, NULL
)) {
10586 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10587 vdev_activity_top_remaining(nvroot
);
10590 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10591 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10592 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10593 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10596 * A replace finishes after resilvering finishes, so the amount of work
10597 * left for a replace is the same as for resilvering.
10599 * It isn't quite correct to say that if we have any 'spare' or
10600 * 'replacing' vdevs and a resilver is happening, then a replace is in
10601 * progress, like we do here. When a hot spare is used, the faulted vdev
10602 * is not removed after the hot spare is resilvered, so parent 'spare'
10603 * vdev is not removed either. So we could have a 'spare' vdev, but be
10604 * resilvering for a different reason. However, we use it as a heuristic
10605 * because we don't have access to the DTLs, which could tell us whether
10606 * or not we have really finished resilvering a hot spare.
10608 if (vdev_any_spare_replacing(nvroot
))
10609 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10611 if (timestamp_fmt
!= NODATE
)
10612 print_timestamp(timestamp_fmt
);
10614 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10616 if (!wd
->wd_enabled
[i
])
10620 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10623 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10625 if (wd
->wd_scripted
)
10626 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10628 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10630 (void) printf("\n");
10631 (void) fflush(stdout
);
10635 wait_status_thread(void *arg
)
10637 wait_data_t
*wd
= (wait_data_t
*)arg
;
10638 zpool_handle_t
*zhp
;
10640 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10641 return (void *)(1);
10643 for (int row
= 0; ; row
++) {
10645 struct timespec timeout
;
10647 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10649 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10650 zpool_props_refresh(zhp
) != 0) {
10652 return (void *)(uintptr_t)(missing
? 0 : 1);
10655 print_wait_status_row(wd
, zhp
, row
);
10657 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10658 long nanos
= timeout
.tv_nsec
+
10659 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10660 if (nanos
>= NANOSEC
) {
10662 timeout
.tv_nsec
= nanos
- NANOSEC
;
10664 timeout
.tv_nsec
= nanos
;
10666 pthread_mutex_lock(&wd
->wd_mutex
);
10667 if (!wd
->wd_should_exit
)
10668 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10670 pthread_mutex_unlock(&wd
->wd_mutex
);
10672 break; /* signaled by main thread */
10673 } else if (ret
!= ETIMEDOUT
) {
10674 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10675 "failed: %s\n"), strerror(ret
));
10677 return (void *)(uintptr_t)(1);
10682 return (void *)(0);
10686 zpool_do_wait(int argc
, char **argv
)
10688 boolean_t verbose
= B_FALSE
;
10690 unsigned long count
;
10691 pthread_t status_thr
;
10693 zpool_handle_t
*zhp
;
10696 wd
.wd_scripted
= B_FALSE
;
10697 wd
.wd_exact
= B_FALSE
;
10698 wd
.wd_headers_once
= B_FALSE
;
10699 wd
.wd_should_exit
= B_FALSE
;
10701 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10702 pthread_cond_init(&wd
.wd_cv
, NULL
);
10704 /* By default, wait for all types of activity. */
10705 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10706 wd
.wd_enabled
[i
] = B_TRUE
;
10708 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10711 wd
.wd_scripted
= B_TRUE
;
10714 wd
.wd_headers_once
= B_TRUE
;
10717 wd
.wd_exact
= B_TRUE
;
10720 get_timestamp_arg(*optarg
);
10723 /* Reset activities array */
10724 memset(&wd
.wd_enabled
, 0, sizeof (wd
.wd_enabled
));
10726 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10727 static const char *const col_opts
[] = {
10728 "discard", "free", "initialize", "replace",
10729 "remove", "resilver", "scrub", "trim" };
10731 for (i
= 0; i
< ARRAY_SIZE(col_opts
); ++i
)
10732 if (strcmp(tok
, col_opts
[i
]) == 0) {
10733 wd
.wd_enabled
[i
] = B_TRUE
;
10737 (void) fprintf(stderr
,
10738 gettext("invalid activity '%s'\n"), tok
);
10744 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10753 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10755 /* This subcmd only accepts an interval, not a count */
10756 (void) fprintf(stderr
, gettext("too many arguments\n"));
10760 if (wd
.wd_interval
!= 0)
10764 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10768 (void) fprintf(stderr
, gettext("too many arguments\n"));
10772 wd
.wd_poolname
= argv
[0];
10774 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10779 * We use a separate thread for printing status updates because
10780 * the main thread will call lzc_wait(), which blocks as long
10781 * as an activity is in progress, which can be a long time.
10783 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10785 (void) fprintf(stderr
, gettext("failed to create status"
10786 "thread: %s\n"), strerror(errno
));
10793 * Loop over all activities that we are supposed to wait for until none
10794 * of them are in progress. Note that this means we can end up waiting
10795 * for more activities to complete than just those that were in progress
10796 * when we began waiting; if an activity we are interested in begins
10797 * while we are waiting for another activity, we will wait for both to
10798 * complete before exiting.
10801 boolean_t missing
= B_FALSE
;
10802 boolean_t any_waited
= B_FALSE
;
10804 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10807 if (!wd
.wd_enabled
[i
])
10810 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10811 if (error
!= 0 || missing
)
10814 any_waited
= (any_waited
|| waited
);
10817 if (error
!= 0 || missing
|| !any_waited
)
10825 pthread_mutex_lock(&wd
.wd_mutex
);
10826 wd
.wd_should_exit
= B_TRUE
;
10827 pthread_cond_signal(&wd
.wd_cv
);
10828 pthread_mutex_unlock(&wd
.wd_mutex
);
10829 (void) pthread_join(status_thr
, (void *)&status
);
10834 pthread_mutex_destroy(&wd
.wd_mutex
);
10835 pthread_cond_destroy(&wd
.wd_cv
);
10840 find_command_idx(const char *command
, int *idx
)
10842 for (int i
= 0; i
< NCOMMAND
; ++i
) {
10843 if (command_table
[i
].name
== NULL
)
10846 if (strcmp(command
, command_table
[i
].name
) == 0) {
10855 * Display version message
10858 zpool_do_version(int argc
, char **argv
)
10860 (void) argc
, (void) argv
;
10861 return (zfs_version_print() != 0);
10865 * Do zpool_load_compat() and print error message on failure
10867 static zpool_compat_status_t
10868 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
10872 zpool_compat_status_t ret
;
10874 ret
= zpool_load_compat(compat
, list
, report
, 1024);
10877 case ZPOOL_COMPATIBILITY_OK
:
10880 case ZPOOL_COMPATIBILITY_NOFILES
:
10881 case ZPOOL_COMPATIBILITY_BADFILE
:
10882 case ZPOOL_COMPATIBILITY_BADTOKEN
:
10883 (void) fprintf(stderr
, "Error: %s\n", report
);
10886 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
10887 (void) fprintf(stderr
, "Warning: %s\n", report
);
10888 ret
= ZPOOL_COMPATIBILITY_OK
;
10895 main(int argc
, char **argv
)
10902 (void) setlocale(LC_ALL
, "");
10903 (void) setlocale(LC_NUMERIC
, "C");
10904 (void) textdomain(TEXT_DOMAIN
);
10910 * Make sure the user has specified some command.
10913 (void) fprintf(stderr
, gettext("missing command\n"));
10920 * Special case '-?'
10922 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10926 * Special case '-V|--version'
10928 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10929 return (zpool_do_version(argc
, argv
));
10931 if ((g_zfs
= libzfs_init()) == NULL
) {
10932 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10936 libzfs_print_on_error(g_zfs
, B_TRUE
);
10938 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10941 * Many commands modify input strings for string parsing reasons.
10942 * We create a copy to protect the original argv.
10944 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
10945 for (i
= 0; i
< argc
; i
++)
10946 newargv
[i
] = strdup(argv
[i
]);
10947 newargv
[argc
] = NULL
;
10950 * Run the appropriate command.
10952 if (find_command_idx(cmdname
, &i
) == 0) {
10953 current_command
= &command_table
[i
];
10954 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10955 } else if (strchr(cmdname
, '=')) {
10956 verify(find_command_idx("set", &i
) == 0);
10957 current_command
= &command_table
[i
];
10958 ret
= command_table
[i
].func(argc
, newargv
);
10959 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10961 * 'freeze' is a vile debugging abomination, so we treat
10964 zfs_cmd_t zc
= {"\0"};
10966 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10967 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10969 (void) fprintf(stderr
,
10970 gettext("failed to freeze pool: %d\n"), errno
);
10976 (void) fprintf(stderr
, gettext("unrecognized "
10977 "command '%s'\n"), cmdname
);
10982 for (i
= 0; i
< argc
; i
++)
10986 if (ret
== 0 && log_history
)
10987 (void) zpool_log_history(g_zfs
, history_str
);
10989 libzfs_fini(g_zfs
);
10992 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10993 * for the purposes of running ::findleaks.
10995 if (getenv("ZFS_ABORT") != NULL
) {
10996 (void) printf("dumping core by request\n");