4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
58 #include <sys/fs/zfs.h>
60 #include <sys/systeminfo.h>
61 #include <sys/fm/fs/zfs.h>
62 #include <sys/fm/util.h>
63 #include <sys/fm/protocol.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/mount.h>
66 #include <sys/sysmacros.h>
73 #include "zpool_util.h"
74 #include "zfs_comutil.h"
75 #include "zfeature_common.h"
77 #include "statcommon.h"
79 libzfs_handle_t
*g_zfs
;
81 static int zpool_do_create(int, char **);
82 static int zpool_do_destroy(int, char **);
84 static int zpool_do_add(int, char **);
85 static int zpool_do_remove(int, char **);
86 static int zpool_do_labelclear(int, char **);
88 static int zpool_do_checkpoint(int, char **);
90 static int zpool_do_list(int, char **);
91 static int zpool_do_iostat(int, char **);
92 static int zpool_do_status(int, char **);
94 static int zpool_do_online(int, char **);
95 static int zpool_do_offline(int, char **);
96 static int zpool_do_clear(int, char **);
97 static int zpool_do_reopen(int, char **);
99 static int zpool_do_reguid(int, char **);
101 static int zpool_do_attach(int, char **);
102 static int zpool_do_detach(int, char **);
103 static int zpool_do_replace(int, char **);
104 static int zpool_do_split(int, char **);
106 static int zpool_do_initialize(int, char **);
107 static int zpool_do_scrub(int, char **);
108 static int zpool_do_resilver(int, char **);
109 static int zpool_do_trim(int, char **);
111 static int zpool_do_import(int, char **);
112 static int zpool_do_export(int, char **);
114 static int zpool_do_upgrade(int, char **);
116 static int zpool_do_history(int, char **);
117 static int zpool_do_events(int, char **);
119 static int zpool_do_get(int, char **);
120 static int zpool_do_set(int, char **);
122 static int zpool_do_sync(int, char **);
124 static int zpool_do_version(int, char **);
126 static int zpool_do_wait(int, char **);
128 static zpool_compat_status_t
zpool_do_load_compat(
129 const char *, boolean_t
*);
132 * These libumem hooks provide a reasonable set of defaults for the allocator's
133 * debugging facilities.
138 _umem_debug_init(void)
140 return ("default,verbose"); /* $UMEM_DEBUG setting */
144 _umem_logging_init(void)
146 return ("fail,contents"); /* $UMEM_LOGGING setting */
187 * Flags for stats to display with "zpool iostats"
195 IOS_COUNT
, /* always last element */
198 /* iostat_type entries as bitmasks */
199 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
200 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
201 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
202 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
203 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
205 /* Mask of all the histo bits */
206 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
209 * Lookup table for iostat flags to nvlist names. Basically a list
210 * of all the nvlists a flag requires. Also specifies the order in
211 * which data gets printed in zpool iostat.
213 static const char *vsx_type_to_nvlist
[IOS_COUNT
][13] = {
215 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
216 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
217 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
218 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
221 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
222 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
227 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
228 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
229 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
230 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
231 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
234 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
235 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
236 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
237 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
238 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
239 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
242 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
243 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
244 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
245 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
246 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
247 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
248 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
249 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
250 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
251 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
252 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
253 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
259 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
260 * Right now, only one histo bit is ever set at one time, so we can
261 * just do a highbit64(a)
263 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
265 typedef struct zpool_command
{
267 int (*func
)(int, char **);
272 * Master command table. Each ZFS command has a name, associated function, and
273 * usage message. The usage messages need to be internationalized, so we have
274 * to have a function to return the usage message based on a command index.
276 * These commands are organized according to how they are displayed in the usage
277 * message. An empty command (one with a NULL name) indicates an empty line in
278 * the generic usage message.
280 static zpool_command_t command_table
[] = {
281 { "version", zpool_do_version
, HELP_VERSION
},
283 { "create", zpool_do_create
, HELP_CREATE
},
284 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
286 { "add", zpool_do_add
, HELP_ADD
},
287 { "remove", zpool_do_remove
, HELP_REMOVE
},
289 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
291 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
293 { "list", zpool_do_list
, HELP_LIST
},
294 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
295 { "status", zpool_do_status
, HELP_STATUS
},
297 { "online", zpool_do_online
, HELP_ONLINE
},
298 { "offline", zpool_do_offline
, HELP_OFFLINE
},
299 { "clear", zpool_do_clear
, HELP_CLEAR
},
300 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
302 { "attach", zpool_do_attach
, HELP_ATTACH
},
303 { "detach", zpool_do_detach
, HELP_DETACH
},
304 { "replace", zpool_do_replace
, HELP_REPLACE
},
305 { "split", zpool_do_split
, HELP_SPLIT
},
307 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
308 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
309 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
310 { "trim", zpool_do_trim
, HELP_TRIM
},
312 { "import", zpool_do_import
, HELP_IMPORT
},
313 { "export", zpool_do_export
, HELP_EXPORT
},
314 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
315 { "reguid", zpool_do_reguid
, HELP_REGUID
},
317 { "history", zpool_do_history
, HELP_HISTORY
},
318 { "events", zpool_do_events
, HELP_EVENTS
},
320 { "get", zpool_do_get
, HELP_GET
},
321 { "set", zpool_do_set
, HELP_SET
},
322 { "sync", zpool_do_sync
, HELP_SYNC
},
324 { "wait", zpool_do_wait
, HELP_WAIT
},
327 #define NCOMMAND (ARRAY_SIZE(command_table))
329 #define VDEV_ALLOC_CLASS_LOGS "logs"
331 static zpool_command_t
*current_command
;
332 static char history_str
[HIS_MAX_RECORD_LEN
];
333 static boolean_t log_history
= B_TRUE
;
334 static uint_t timestamp_fmt
= NODATE
;
337 get_usage(zpool_help_t idx
)
341 return (gettext("\tadd [-fgLnP] [-o property=value] "
342 "<pool> <vdev> ...\n"));
344 return (gettext("\tattach [-fsw] [-o property=value] "
345 "<pool> <device> <new-device>\n"));
347 return (gettext("\tclear [-nF] <pool> [device]\n"));
349 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
350 "\t [-O file-system-property=value] ... \n"
351 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
352 case HELP_CHECKPOINT
:
353 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
355 return (gettext("\tdestroy [-f] <pool>\n"));
357 return (gettext("\tdetach <pool> <device>\n"));
359 return (gettext("\texport [-af] <pool> ...\n"));
361 return (gettext("\thistory [-il] [<pool>] ...\n"));
363 return (gettext("\timport [-d dir] [-D]\n"
364 "\timport [-o mntopts] [-o property=value] ... \n"
365 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
366 "[-R root] [-F [-n]] -a\n"
367 "\timport [-o mntopts] [-o property=value] ... \n"
368 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
369 "[-R root] [-F [-n]]\n"
370 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
372 return (gettext("\tiostat [[[-c [script1,script2,...]"
373 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
374 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
375 " [[-n] interval [count]]\n"));
376 case HELP_LABELCLEAR
:
377 return (gettext("\tlabelclear [-f] <vdev>\n"));
379 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
380 "[-T d|u] [pool] ... \n"
381 "\t [interval [count]]\n"));
383 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
385 return (gettext("\tonline [-e] <pool> <device> ...\n"));
387 return (gettext("\treplace [-fsw] [-o property=value] "
388 "<pool> <device> [new-device]\n"));
390 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
392 return (gettext("\treopen [-n] <pool>\n"));
393 case HELP_INITIALIZE
:
394 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
395 "[<device> ...]\n"));
397 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
399 return (gettext("\tresilver <pool> ...\n"));
401 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
402 "[<device> ...]\n"));
404 return (gettext("\tstatus [-c [script1,script2,...]] "
405 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
406 "\t [interval [count]]\n"));
408 return (gettext("\tupgrade\n"
410 "\tupgrade [-V version] <-a | pool ...>\n"));
412 return (gettext("\tevents [-vHf [pool] | -c]\n"));
414 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
415 "<\"all\" | property[,...]> <pool> ...\n"));
417 return (gettext("\tset <property=value> <pool> \n"));
419 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
420 "\t [-o property=value] <pool> <newpool> "
421 "[<device> ...]\n"));
423 return (gettext("\treguid <pool>\n"));
425 return (gettext("\tsync [pool] ...\n"));
427 return (gettext("\tversion\n"));
429 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
430 "<pool> [interval]\n"));
438 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
444 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
448 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
451 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
452 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
453 fnvlist_add_boolean(res
, path
);
459 for (i
= 0; i
< children
; i
++) {
460 zpool_collect_leaves(zhp
, child
[i
], res
);
465 * Callback routine that will print out a pool property value.
468 print_prop_cb(int prop
, void *cb
)
472 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
474 if (zpool_prop_readonly(prop
))
475 (void) fprintf(fp
, " NO ");
477 (void) fprintf(fp
, " YES ");
479 if (zpool_prop_values(prop
) == NULL
)
480 (void) fprintf(fp
, "-\n");
482 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
488 * Display usage message. If we're inside a command, display only the usage for
489 * that command. Otherwise, iterate over the entire command table and display
490 * a complete usage message.
493 usage(boolean_t requested
)
495 FILE *fp
= requested
? stdout
: stderr
;
497 if (current_command
== NULL
) {
500 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
502 gettext("where 'command' is one of the following:\n\n"));
504 for (i
= 0; i
< NCOMMAND
; i
++) {
505 if (command_table
[i
].name
== NULL
)
506 (void) fprintf(fp
, "\n");
508 (void) fprintf(fp
, "%s",
509 get_usage(command_table
[i
].usage
));
512 (void) fprintf(fp
, gettext("usage:\n"));
513 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
516 if (current_command
!= NULL
&&
517 ((strcmp(current_command
->name
, "set") == 0) ||
518 (strcmp(current_command
->name
, "get") == 0) ||
519 (strcmp(current_command
->name
, "list") == 0))) {
522 gettext("\nthe following properties are supported:\n"));
524 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
525 "PROPERTY", "EDIT", "VALUES");
527 /* Iterate over all properties */
528 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
531 (void) fprintf(fp
, "\t%-19s ", "feature@...");
532 (void) fprintf(fp
, "YES disabled | enabled | active\n");
534 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
535 "appended with a feature name.\nSee zpool-features(5).\n"));
539 * See comments at end of main().
541 if (getenv("ZFS_ABORT") != NULL
) {
542 (void) printf("dumping core by request\n");
546 exit(requested
? 0 : 2);
550 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
551 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
554 * -c Cancel. Ends active initializing.
555 * -s Suspend. Initializing can then be restarted with no flags.
556 * -w Wait. Blocks until initializing has completed.
559 zpool_do_initialize(int argc
, char **argv
)
566 boolean_t wait
= B_FALSE
;
568 struct option long_options
[] = {
569 {"cancel", no_argument
, NULL
, 'c'},
570 {"suspend", no_argument
, NULL
, 's'},
571 {"wait", no_argument
, NULL
, 'w'},
575 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
576 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
579 if (cmd_type
!= POOL_INITIALIZE_START
&&
580 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
581 (void) fprintf(stderr
, gettext("-c cannot be "
582 "combined with other options\n"));
585 cmd_type
= POOL_INITIALIZE_CANCEL
;
588 if (cmd_type
!= POOL_INITIALIZE_START
&&
589 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
590 (void) fprintf(stderr
, gettext("-s cannot be "
591 "combined with other options\n"));
594 cmd_type
= POOL_INITIALIZE_SUSPEND
;
601 (void) fprintf(stderr
,
602 gettext("invalid option '%c'\n"), optopt
);
604 (void) fprintf(stderr
,
605 gettext("invalid option '%s'\n"),
616 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
621 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
622 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
628 zhp
= zpool_open(g_zfs
, poolname
);
632 vdevs
= fnvlist_alloc();
634 /* no individual leaf vdevs specified, so add them all */
635 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
636 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
637 ZPOOL_CONFIG_VDEV_TREE
);
638 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
640 for (int i
= 1; i
< argc
; i
++) {
641 fnvlist_add_boolean(vdevs
, argv
[i
]);
646 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
648 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
657 * print a pool vdev config for dry runs
660 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
661 const char *match
, int name_flags
)
666 boolean_t printed
= B_FALSE
;
668 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
669 &child
, &children
) != 0) {
671 (void) printf("\t%*s%s\n", indent
, "", name
);
675 for (c
= 0; c
< children
; c
++) {
676 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
679 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
682 if (is_hole
== B_TRUE
) {
686 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
689 class = VDEV_ALLOC_BIAS_LOG
;
690 (void) nvlist_lookup_string(child
[c
],
691 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
692 if (strcmp(match
, class) != 0)
695 if (!printed
&& name
!= NULL
) {
696 (void) printf("\t%*s%s\n", indent
, "", name
);
699 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
700 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
707 * Print the list of l2cache devices for dry runs.
710 print_cache_list(nvlist_t
*nv
, int indent
)
715 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
716 &child
, &children
) == 0 && children
> 0) {
717 (void) printf("\t%*s%s\n", indent
, "", "cache");
721 for (c
= 0; c
< children
; c
++) {
724 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
725 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
731 * Print the list of spares for dry runs.
734 print_spare_list(nvlist_t
*nv
, int indent
)
739 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
740 &child
, &children
) == 0 && children
> 0) {
741 (void) printf("\t%*s%s\n", indent
, "", "spares");
745 for (c
= 0; c
< children
; c
++) {
748 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
749 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
755 prop_list_contains_feature(nvlist_t
*proplist
)
758 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
759 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
760 if (zpool_prop_feature(nvpair_name(nvp
)))
767 * Add a property pair (name, string-value) into a property nvlist.
770 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
773 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
778 if (*props
== NULL
&&
779 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
780 (void) fprintf(stderr
,
781 gettext("internal error: out of memory\n"));
788 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
790 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
792 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
793 !zpool_prop_feature(propname
)) {
794 (void) fprintf(stderr
, gettext("property '%s' is "
795 "not a valid pool property\n"), propname
);
800 * feature@ properties and version should not be specified
803 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
804 nvlist_exists(proplist
, vname
)) ||
805 (prop
== ZPOOL_PROP_VERSION
&&
806 prop_list_contains_feature(proplist
))) {
807 (void) fprintf(stderr
, gettext("'feature@' and "
808 "'version' properties cannot be specified "
814 * compatibility property and version should not be specified
817 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
818 nvlist_exists(proplist
, vname
)) ||
819 (prop
== ZPOOL_PROP_VERSION
&&
820 nvlist_exists(proplist
, fname
))) {
821 (void) fprintf(stderr
, gettext("'compatibility' and "
822 "'version' properties cannot be specified "
827 if (zpool_prop_feature(propname
))
830 normnm
= zpool_prop_to_name(prop
);
832 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
834 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
836 normnm
= zfs_prop_to_name(fsprop
);
837 } else if (zfs_prop_user(propname
) ||
838 zfs_prop_userquota(propname
)) {
841 (void) fprintf(stderr
, gettext("property '%s' is "
842 "not a valid filesystem property\n"), propname
);
847 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
848 prop
!= ZPOOL_PROP_CACHEFILE
) {
849 (void) fprintf(stderr
, gettext("property '%s' "
850 "specified multiple times\n"), propname
);
854 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
855 (void) fprintf(stderr
, gettext("internal "
856 "error: out of memory\n"));
864 * Set a default property pair (name, string-value) in a property nvlist
867 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
872 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
875 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
879 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
881 * -f Force addition of devices, even if they appear in use
882 * -g Display guid for individual vdev name.
883 * -L Follow links when resolving vdev path name.
884 * -n Do not add the devices, but display the resulting layout if
885 * they were to be added.
886 * -o Set property=value.
887 * -P Display full path for vdev name.
889 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
890 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
894 zpool_do_add(int argc
, char **argv
)
896 boolean_t force
= B_FALSE
;
897 boolean_t dryrun
= B_FALSE
;
905 nvlist_t
*props
= NULL
;
909 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
915 name_flags
|= VDEV_NAME_GUID
;
918 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
924 if ((propval
= strchr(optarg
, '=')) == NULL
) {
925 (void) fprintf(stderr
, gettext("missing "
926 "'=' for -o option\n"));
932 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
933 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
937 name_flags
|= VDEV_NAME_PATH
;
940 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
949 /* get pool name and check number of arguments */
951 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
955 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
964 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
967 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
968 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
974 /* unless manually specified use "ashift" pool property (if set) */
975 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
978 char strval
[ZPOOL_MAXPROPLEN
];
980 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
981 if (src
!= ZPROP_SRC_DEFAULT
) {
982 (void) sprintf(strval
, "%" PRId32
, intval
);
983 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
984 &props
, B_TRUE
) == 0);
988 /* pass off to make_root_vdev for processing */
989 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
991 if (nvroot
== NULL
) {
997 nvlist_t
*poolnvroot
;
998 nvlist_t
**l2child
, **sparechild
;
999 uint_t l2children
, sparechildren
, c
;
1001 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1003 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1006 (void) printf(gettext("would update '%s' to the following "
1007 "configuration:\n\n"), zpool_get_name(zhp
));
1009 /* print original main pool and new tree */
1010 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1011 name_flags
| VDEV_NAME_TYPE_ID
);
1012 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1014 /* print other classes: 'dedup', 'special', and 'log' */
1015 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1016 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1017 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1018 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1019 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1020 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1021 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1022 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1025 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1026 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1027 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1028 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1029 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1030 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1031 print_vdev_tree(zhp
, "special", nvroot
, 0,
1032 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1035 if (num_logs(poolnvroot
) > 0) {
1036 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1037 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1038 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1039 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1040 } else if (num_logs(nvroot
) > 0) {
1041 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1042 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1045 /* Do the same for the caches */
1046 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1047 &l2child
, &l2children
) == 0 && l2children
) {
1049 (void) printf(gettext("\tcache\n"));
1050 for (c
= 0; c
< l2children
; c
++) {
1051 vname
= zpool_vdev_name(g_zfs
, NULL
,
1052 l2child
[c
], name_flags
);
1053 (void) printf("\t %s\n", vname
);
1057 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1058 &l2child
, &l2children
) == 0 && l2children
) {
1060 (void) printf(gettext("\tcache\n"));
1061 for (c
= 0; c
< l2children
; c
++) {
1062 vname
= zpool_vdev_name(g_zfs
, NULL
,
1063 l2child
[c
], name_flags
);
1064 (void) printf("\t %s\n", vname
);
1068 /* And finaly the spares */
1069 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1070 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1072 (void) printf(gettext("\tspares\n"));
1073 for (c
= 0; c
< sparechildren
; c
++) {
1074 vname
= zpool_vdev_name(g_zfs
, NULL
,
1075 sparechild
[c
], name_flags
);
1076 (void) printf("\t %s\n", vname
);
1080 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1081 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1083 (void) printf(gettext("\tspares\n"));
1084 for (c
= 0; c
< sparechildren
; c
++) {
1085 vname
= zpool_vdev_name(g_zfs
, NULL
,
1086 sparechild
[c
], name_flags
);
1087 (void) printf("\t %s\n", vname
);
1094 ret
= (zpool_add(zhp
, nvroot
) != 0);
1098 nvlist_free(nvroot
);
1105 * zpool remove [-npsw] <pool> <vdev> ...
1107 * Removes the given vdev from the pool.
1110 zpool_do_remove(int argc
, char **argv
)
1114 zpool_handle_t
*zhp
= NULL
;
1115 boolean_t stop
= B_FALSE
;
1117 boolean_t noop
= B_FALSE
;
1118 boolean_t parsable
= B_FALSE
;
1119 boolean_t wait
= B_FALSE
;
1122 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1137 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1146 /* get pool name and check number of arguments */
1148 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1154 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1158 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1164 (void) fprintf(stderr
, gettext("too many arguments\n"));
1167 if (zpool_vdev_remove_cancel(zhp
) != 0)
1170 (void) fprintf(stderr
, gettext("invalid option "
1171 "combination: -w cannot be used with -s\n"));
1176 (void) fprintf(stderr
, gettext("missing device\n"));
1180 for (i
= 1; i
< argc
; i
++) {
1184 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1190 (void) printf("%s %llu\n",
1191 argv
[i
], (unsigned long long)size
);
1194 zfs_nicenum(size
, valstr
,
1196 (void) printf("Memory that will be "
1197 "used after removing %s: %s\n",
1201 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1206 if (ret
== 0 && wait
)
1207 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1215 * zpool labelclear [-f] <vdev>
1217 * -f Force clearing the label for the vdevs which are members of
1218 * the exported or foreign pools.
1220 * Verifies that the vdev is not active and zeros out the label information
1224 zpool_do_labelclear(int argc
, char **argv
)
1226 char vdev
[MAXPATHLEN
];
1229 int c
, fd
= -1, ret
= 0;
1232 boolean_t inuse
= B_FALSE
;
1233 boolean_t force
= B_FALSE
;
1236 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1242 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1253 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1257 (void) fprintf(stderr
, gettext("too many arguments\n"));
1262 * Check if we were given absolute path and use it as is.
1263 * Otherwise if the provided vdev name doesn't point to a file,
1264 * try prepending expected disk paths and partition numbers.
1266 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1267 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1270 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1271 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1272 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1276 if (error
|| (stat(vdev
, &st
) != 0)) {
1277 (void) fprintf(stderr
, gettext(
1278 "failed to find device %s, try specifying absolute "
1279 "path instead\n"), argv
[0]);
1284 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1285 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1286 vdev
, strerror(errno
));
1291 * Flush all dirty pages for the block device. This should not be
1292 * fatal when the device does not support BLKFLSBUF as would be the
1293 * case for a file vdev.
1295 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1296 (void) fprintf(stderr
, gettext("failed to invalidate "
1297 "cache for %s: %s\n"), vdev
, strerror(errno
));
1299 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1300 (void) fprintf(stderr
,
1301 gettext("failed to read label from %s\n"), vdev
);
1305 nvlist_free(config
);
1307 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1309 (void) fprintf(stderr
,
1310 gettext("failed to check state for %s\n"), vdev
);
1320 case POOL_STATE_ACTIVE
:
1321 case POOL_STATE_SPARE
:
1322 case POOL_STATE_L2CACHE
:
1323 (void) fprintf(stderr
, gettext(
1324 "%s is a member (%s) of pool \"%s\"\n"),
1325 vdev
, zpool_pool_state_to_name(state
), name
);
1329 case POOL_STATE_EXPORTED
:
1332 (void) fprintf(stderr
, gettext(
1333 "use '-f' to override the following error:\n"
1334 "%s is a member of exported pool \"%s\"\n"),
1339 case POOL_STATE_POTENTIALLY_ACTIVE
:
1342 (void) fprintf(stderr
, gettext(
1343 "use '-f' to override the following error:\n"
1344 "%s is a member of potentially active pool \"%s\"\n"),
1349 case POOL_STATE_DESTROYED
:
1350 /* inuse should never be set for a destroyed pool */
1356 ret
= zpool_clear_label(fd
);
1358 (void) fprintf(stderr
,
1359 gettext("failed to clear label for %s\n"), vdev
);
1370 * zpool create [-fnd] [-o property=value] ...
1371 * [-O file-system-property=value] ...
1372 * [-R root] [-m mountpoint] <pool> <dev> ...
1374 * -f Force creation, even if devices appear in use
1375 * -n Do not create the pool, but display the resulting layout if it
1376 * were to be created.
1377 * -R Create a pool under an alternate root
1378 * -m Set default mountpoint for the root dataset. By default it's
1380 * -o Set property=value.
1381 * -o Set feature@feature=enabled|disabled.
1382 * -d Don't automatically enable all supported pool features
1383 * (individual features can be enabled with -o).
1384 * -O Set fsproperty=value in the pool's root file system
1386 * Creates the named pool according to the given vdev specification. The
1387 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1388 * Once we get the nvlist back from make_root_vdev(), we either print out the
1389 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1392 zpool_do_create(int argc
, char **argv
)
1394 boolean_t force
= B_FALSE
;
1395 boolean_t dryrun
= B_FALSE
;
1396 boolean_t enable_pool_features
= B_TRUE
;
1399 nvlist_t
*nvroot
= NULL
;
1403 char *altroot
= NULL
;
1404 char *compat
= NULL
;
1405 char *mountpoint
= NULL
;
1406 nvlist_t
*fsprops
= NULL
;
1407 nvlist_t
*props
= NULL
;
1411 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1420 enable_pool_features
= B_FALSE
;
1424 if (add_prop_list(zpool_prop_to_name(
1425 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1427 if (add_prop_list_default(zpool_prop_to_name(
1428 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1432 /* Equivalent to -O mountpoint=optarg */
1433 mountpoint
= optarg
;
1436 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1437 (void) fprintf(stderr
, gettext("missing "
1438 "'=' for -o option\n"));
1444 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1448 * If the user is creating a pool that doesn't support
1449 * feature flags, don't enable any features.
1451 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1455 ver
= strtoull(propval
, &end
, 10);
1457 ver
< SPA_VERSION_FEATURES
) {
1458 enable_pool_features
= B_FALSE
;
1461 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1463 if (zpool_name_to_prop(optarg
) ==
1464 ZPOOL_PROP_COMPATIBILITY
)
1468 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1469 (void) fprintf(stderr
, gettext("missing "
1470 "'=' for -O option\n"));
1477 * Mountpoints are checked and then added later.
1478 * Uniquely among properties, they can be specified
1479 * more than once, to avoid conflict with -m.
1481 if (0 == strcmp(optarg
,
1482 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1483 mountpoint
= propval
;
1484 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1491 * Sanity check temporary pool name.
1493 if (strchr(optarg
, '/') != NULL
) {
1494 (void) fprintf(stderr
, gettext("cannot create "
1495 "'%s': invalid character '/' in temporary "
1497 (void) fprintf(stderr
, gettext("use 'zfs "
1498 "create' to create a dataset\n"));
1502 if (add_prop_list(zpool_prop_to_name(
1503 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1505 if (add_prop_list_default(zpool_prop_to_name(
1506 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1511 (void) fprintf(stderr
, gettext("missing argument for "
1512 "'%c' option\n"), optopt
);
1515 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1524 /* get pool name and check number of arguments */
1526 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1530 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1537 * As a special case, check for use of '/' in the name, and direct the
1538 * user to use 'zfs create' instead.
1540 if (strchr(poolname
, '/') != NULL
) {
1541 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1542 "character '/' in pool name\n"), poolname
);
1543 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1544 "create a dataset\n"));
1548 /* pass off to make_root_vdev for bulk processing */
1549 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1550 argc
- 1, argv
+ 1);
1554 /* make_root_vdev() allows 0 toplevel children if there are spares */
1555 if (!zfs_allocatable_devs(nvroot
)) {
1556 (void) fprintf(stderr
, gettext("invalid vdev "
1557 "specification: at least one toplevel vdev must be "
1562 if (altroot
!= NULL
&& altroot
[0] != '/') {
1563 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1564 "must be an absolute path\n"), altroot
);
1569 * Check the validity of the mountpoint and direct the user to use the
1570 * '-m' mountpoint option if it looks like its in use.
1572 if (mountpoint
== NULL
||
1573 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1574 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1575 char buf
[MAXPATHLEN
];
1578 if (mountpoint
&& mountpoint
[0] != '/') {
1579 (void) fprintf(stderr
, gettext("invalid mountpoint "
1580 "'%s': must be an absolute path, 'legacy', or "
1581 "'none'\n"), mountpoint
);
1585 if (mountpoint
== NULL
) {
1586 if (altroot
!= NULL
)
1587 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1590 (void) snprintf(buf
, sizeof (buf
), "/%s",
1593 if (altroot
!= NULL
)
1594 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1595 altroot
, mountpoint
);
1597 (void) snprintf(buf
, sizeof (buf
), "%s",
1601 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1602 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1603 "%s\n"), buf
, strerror(errno
));
1604 (void) fprintf(stderr
, gettext("use '-m' "
1605 "option to provide a different default\n"));
1610 while (count
< 3 && readdir(dirp
) != NULL
)
1612 (void) closedir(dirp
);
1615 (void) fprintf(stderr
, gettext("mountpoint "
1616 "'%s' exists and is not empty\n"), buf
);
1617 (void) fprintf(stderr
, gettext("use '-m' "
1618 "option to provide a "
1619 "different default\n"));
1626 * Now that the mountpoint's validity has been checked, ensure that
1627 * the property is set appropriately prior to creating the pool.
1629 if (mountpoint
!= NULL
) {
1630 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1631 mountpoint
, &fsprops
, B_FALSE
);
1639 * For a dry run invocation, print out a basic message and run
1640 * through all the vdevs in the list and print out in an
1641 * appropriate hierarchy.
1643 (void) printf(gettext("would create '%s' with the "
1644 "following layout:\n\n"), poolname
);
1646 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1647 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1648 VDEV_ALLOC_BIAS_DEDUP
, 0);
1649 print_vdev_tree(NULL
, "special", nvroot
, 0,
1650 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1651 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1652 VDEV_ALLOC_BIAS_LOG
, 0);
1653 print_cache_list(nvroot
, 0);
1654 print_spare_list(nvroot
, 0);
1659 * Load in feature set.
1660 * Note: if compatibility property not given, we'll have
1661 * NULL, which means 'all features'.
1663 boolean_t requested_features
[SPA_FEATURES
];
1664 if (zpool_do_load_compat(compat
, requested_features
) !=
1665 ZPOOL_COMPATIBILITY_OK
)
1669 * props contains list of features to enable.
1671 * - remove it if feature@name=disabled
1672 * - leave it there if feature@name=enabled
1674 * - enable_pool_features (ie: no '-d' or '-o version')
1675 * - it's supported by the kernel module
1676 * - it's in the requested feature set
1678 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1679 char propname
[MAXPATHLEN
];
1681 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1683 (void) snprintf(propname
, sizeof (propname
),
1684 "feature@%s", feat
->fi_uname
);
1686 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1687 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1688 (void) nvlist_remove_all(props
,
1691 enable_pool_features
&&
1692 feat
->fi_zfs_mod_supported
&&
1693 requested_features
[i
]) {
1694 ret
= add_prop_list(propname
,
1695 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1702 if (zpool_create(g_zfs
, poolname
,
1703 nvroot
, props
, fsprops
) == 0) {
1704 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1705 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1707 if (zfs_mount(pool
, NULL
, 0) == 0) {
1708 ret
= zfs_shareall(pool
);
1709 zfs_commit_all_shares();
1713 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1714 (void) fprintf(stderr
, gettext("pool name may have "
1720 nvlist_free(nvroot
);
1721 nvlist_free(fsprops
);
1725 nvlist_free(fsprops
);
1732 * zpool destroy <pool>
1734 * -f Forcefully unmount any datasets
1736 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1739 zpool_do_destroy(int argc
, char **argv
)
1741 boolean_t force
= B_FALSE
;
1744 zpool_handle_t
*zhp
;
1748 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1754 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1763 /* check arguments */
1765 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1769 (void) fprintf(stderr
, gettext("too many arguments\n"));
1775 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1777 * As a special case, check for use of '/' in the name, and
1778 * direct the user to use 'zfs destroy' instead.
1780 if (strchr(pool
, '/') != NULL
)
1781 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1782 "destroy a dataset\n"));
1786 if (zpool_disable_datasets(zhp
, force
) != 0) {
1787 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1788 "could not unmount datasets\n"), zpool_get_name(zhp
));
1793 /* The history must be logged as part of the export */
1794 log_history
= B_FALSE
;
1796 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1803 typedef struct export_cbdata
{
1805 boolean_t hardforce
;
1812 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1814 export_cbdata_t
*cb
= data
;
1816 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1819 /* The history must be logged as part of the export */
1820 log_history
= B_FALSE
;
1822 if (cb
->hardforce
) {
1823 if (zpool_export_force(zhp
, history_str
) != 0)
1825 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1833 * zpool export [-f] <pool> ...
1835 * -a Export all pools
1836 * -f Forcefully unmount datasets
1838 * Export the given pools. By default, the command will attempt to cleanly
1839 * unmount any active datasets within the pool. If the '-f' flag is specified,
1840 * then the datasets will be forcefully unmounted.
1843 zpool_do_export(int argc
, char **argv
)
1846 boolean_t do_all
= B_FALSE
;
1847 boolean_t force
= B_FALSE
;
1848 boolean_t hardforce
= B_FALSE
;
1852 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1864 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1871 cb
.hardforce
= hardforce
;
1877 (void) fprintf(stderr
, gettext("too many arguments\n"));
1881 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1882 B_FALSE
, zpool_export_one
, &cb
));
1885 /* check arguments */
1887 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1891 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
, zpool_export_one
,
1898 * Given a vdev configuration, determine the maximum width needed for the device
1902 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1910 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1911 if (strlen(name
) + depth
> max
)
1912 max
= strlen(name
) + depth
;
1916 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1917 &child
, &children
) == 0) {
1918 for (c
= 0; c
< children
; c
++)
1919 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1920 max
, name_flags
)) > max
)
1924 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1925 &child
, &children
) == 0) {
1926 for (c
= 0; c
< children
; c
++)
1927 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1928 max
, name_flags
)) > max
)
1932 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1933 &child
, &children
) == 0) {
1934 for (c
= 0; c
< children
; c
++)
1935 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1936 max
, name_flags
)) > max
)
1943 typedef struct spare_cbdata
{
1945 zpool_handle_t
*cb_zhp
;
1949 find_vdev(nvlist_t
*nv
, uint64_t search
)
1955 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1959 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1960 &child
, &children
) == 0) {
1961 for (c
= 0; c
< children
; c
++)
1962 if (find_vdev(child
[c
], search
))
1970 find_spare(zpool_handle_t
*zhp
, void *data
)
1972 spare_cbdata_t
*cbp
= data
;
1973 nvlist_t
*config
, *nvroot
;
1975 config
= zpool_get_config(zhp
, NULL
);
1976 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1979 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
1988 typedef struct status_cbdata
{
1992 boolean_t cb_allpools
;
1993 boolean_t cb_verbose
;
1994 boolean_t cb_literal
;
1995 boolean_t cb_explain
;
1997 boolean_t cb_dedup_stats
;
1998 boolean_t cb_print_status
;
1999 boolean_t cb_print_slow_ios
;
2000 boolean_t cb_print_vdev_init
;
2001 boolean_t cb_print_vdev_trim
;
2002 vdev_cmd_data_list_t
*vcdl
;
2005 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2007 is_blank_str(char *str
)
2009 while (str
!= NULL
&& *str
!= '\0') {
2017 /* Print command output lines for specific vdev in a specific pool */
2019 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
2021 vdev_cmd_data_t
*data
;
2025 for (i
= 0; i
< vcdl
->count
; i
++) {
2026 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2027 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2028 /* Not the vdev we're looking for */
2032 data
= &vcdl
->data
[i
];
2033 /* Print out all the output values for this vdev */
2034 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2036 /* Does this vdev have values for this column? */
2037 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2038 if (strcmp(data
->cols
[k
],
2039 vcdl
->uniq_cols
[j
]) == 0) {
2040 /* yes it does, record the value */
2041 val
= data
->lines
[k
];
2046 * Mark empty values with dashes to make output
2049 if (val
== NULL
|| is_blank_str(val
))
2052 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2053 if (j
< vcdl
->uniq_cols_cnt
- 1)
2057 /* Print out any values that aren't in a column at the end */
2058 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2059 /* Did we have any columns? If so print a spacer. */
2060 if (vcdl
->uniq_cols_cnt
> 0)
2063 val
= data
->lines
[j
];
2064 printf("%s", val
? val
: "");
2071 * Print vdev initialization status for leaves
2074 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2077 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2078 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2079 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2080 !vs
->vs_scan_removing
) {
2083 struct tm zaction_ts
;
2085 time_t t
= vs
->vs_initialize_action_time
;
2086 int initialize_pct
= 100;
2087 if (vs
->vs_initialize_state
!=
2088 VDEV_INITIALIZE_COMPLETE
) {
2089 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2090 100 / (vs
->vs_initialize_bytes_est
+ 1));
2093 (void) localtime_r(&t
, &zaction_ts
);
2094 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2096 switch (vs
->vs_initialize_state
) {
2097 case VDEV_INITIALIZE_SUSPENDED
:
2098 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2099 gettext("suspended, started at"), tbuf
);
2101 case VDEV_INITIALIZE_ACTIVE
:
2102 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2103 gettext("started at"), tbuf
);
2105 case VDEV_INITIALIZE_COMPLETE
:
2106 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2107 gettext("completed at"), tbuf
);
2111 (void) printf(gettext(" (%d%% initialized%s)"),
2112 initialize_pct
, zbuf
);
2114 (void) printf(gettext(" (uninitialized)"));
2116 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2117 (void) printf(gettext(" (initializing)"));
2122 * Print vdev TRIM status for leaves
2125 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2128 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2129 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2130 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2131 !vs
->vs_scan_removing
) {
2134 struct tm zaction_ts
;
2136 time_t t
= vs
->vs_trim_action_time
;
2138 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2139 trim_pct
= (vs
->vs_trim_bytes_done
*
2140 100 / (vs
->vs_trim_bytes_est
+ 1));
2143 (void) localtime_r(&t
, &zaction_ts
);
2144 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2146 switch (vs
->vs_trim_state
) {
2147 case VDEV_TRIM_SUSPENDED
:
2148 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2149 gettext("suspended, started at"), tbuf
);
2151 case VDEV_TRIM_ACTIVE
:
2152 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2153 gettext("started at"), tbuf
);
2155 case VDEV_TRIM_COMPLETE
:
2156 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2157 gettext("completed at"), tbuf
);
2161 (void) printf(gettext(" (%d%% trimmed%s)"),
2163 } else if (vs
->vs_trim_notsup
) {
2164 (void) printf(gettext(" (trim unsupported)"));
2166 (void) printf(gettext(" (untrimmed)"));
2168 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2169 (void) printf(gettext(" (trimming)"));
2174 * Return the color associated with a health string. This includes returning
2175 * NULL for no color change.
2178 health_str_to_color(const char *health
)
2180 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2181 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2182 strcmp(health
, gettext("UNAVAIL")) == 0) {
2186 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2187 strcmp(health
, gettext("DEGRADED")) == 0 ||
2188 strcmp(health
, gettext("REMOVED")) == 0) {
2189 return (ANSI_YELLOW
);
2196 * Print out configuration state as requested by status_callback.
2199 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2200 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2202 nvlist_t
**child
, *root
;
2203 uint_t c
, i
, vsc
, children
;
2204 pool_scan_stat_t
*ps
= NULL
;
2206 char rbuf
[6], wbuf
[6], cbuf
[6];
2208 uint64_t notpresent
;
2209 spare_cbdata_t spare_cb
;
2213 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2215 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2216 &child
, &children
) != 0)
2219 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2220 (uint64_t **)&vs
, &vsc
) == 0);
2222 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2224 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2227 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2231 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2234 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2235 state
= gettext("INUSE");
2236 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2237 state
= gettext("AVAIL");
2240 printf_color(health_str_to_color(state
),
2241 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2245 if (vs
->vs_read_errors
)
2248 if (vs
->vs_write_errors
)
2251 if (vs
->vs_checksum_errors
)
2254 if (cb
->cb_literal
) {
2256 printf_color(rcolor
, "%5llu",
2257 (u_longlong_t
)vs
->vs_read_errors
);
2259 printf_color(wcolor
, "%5llu",
2260 (u_longlong_t
)vs
->vs_write_errors
);
2262 printf_color(ccolor
, "%5llu",
2263 (u_longlong_t
)vs
->vs_checksum_errors
);
2265 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2266 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2267 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2270 printf_color(rcolor
, "%5s", rbuf
);
2272 printf_color(wcolor
, "%5s", wbuf
);
2274 printf_color(ccolor
, "%5s", cbuf
);
2276 if (cb
->cb_print_slow_ios
) {
2277 if (children
== 0) {
2278 /* Only leafs vdevs have slow IOs */
2279 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2282 snprintf(rbuf
, sizeof (rbuf
), "-");
2286 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2288 printf(" %5s", rbuf
);
2292 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2293 ¬present
) == 0) {
2294 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2295 (void) printf(" %s %s", gettext("was"), path
);
2296 } else if (vs
->vs_aux
!= 0) {
2298 color_start(ANSI_RED
);
2299 switch (vs
->vs_aux
) {
2300 case VDEV_AUX_OPEN_FAILED
:
2301 (void) printf(gettext("cannot open"));
2304 case VDEV_AUX_BAD_GUID_SUM
:
2305 (void) printf(gettext("missing device"));
2308 case VDEV_AUX_NO_REPLICAS
:
2309 (void) printf(gettext("insufficient replicas"));
2312 case VDEV_AUX_VERSION_NEWER
:
2313 (void) printf(gettext("newer version"));
2316 case VDEV_AUX_UNSUP_FEAT
:
2317 (void) printf(gettext("unsupported feature(s)"));
2320 case VDEV_AUX_ASHIFT_TOO_BIG
:
2321 (void) printf(gettext("unsupported minimum blocksize"));
2324 case VDEV_AUX_SPARED
:
2325 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2326 &spare_cb
.cb_guid
) == 0);
2327 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2328 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2329 zpool_get_name(zhp
)) == 0)
2330 (void) printf(gettext("currently in "
2333 (void) printf(gettext("in use by "
2335 zpool_get_name(spare_cb
.cb_zhp
));
2336 zpool_close(spare_cb
.cb_zhp
);
2338 (void) printf(gettext("currently in use"));
2342 case VDEV_AUX_ERR_EXCEEDED
:
2343 (void) printf(gettext("too many errors"));
2346 case VDEV_AUX_IO_FAILURE
:
2347 (void) printf(gettext("experienced I/O failures"));
2350 case VDEV_AUX_BAD_LOG
:
2351 (void) printf(gettext("bad intent log"));
2354 case VDEV_AUX_EXTERNAL
:
2355 (void) printf(gettext("external device fault"));
2358 case VDEV_AUX_SPLIT_POOL
:
2359 (void) printf(gettext("split into new pool"));
2362 case VDEV_AUX_ACTIVE
:
2363 (void) printf(gettext("currently in use"));
2366 case VDEV_AUX_CHILDREN_OFFLINE
:
2367 (void) printf(gettext("all children offline"));
2371 (void) printf(gettext("corrupted data"));
2375 } else if (children
== 0 && !isspare
&&
2376 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2377 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2378 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2380 gettext(" block size: %dB configured, %dB native"),
2381 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2384 /* The root vdev has the scrub/resilver stats */
2385 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2386 ZPOOL_CONFIG_VDEV_TREE
);
2387 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2388 (uint64_t **)&ps
, &c
);
2390 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2391 if (vs
->vs_scan_processed
!= 0) {
2392 (void) printf(gettext(" (%s)"),
2393 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2394 "resilvering" : "repairing");
2395 } else if (vs
->vs_resilver_deferred
) {
2396 (void) printf(gettext(" (awaiting resilver)"));
2400 /* The top-level vdevs have the rebuild stats */
2401 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2403 if (vs
->vs_rebuild_processed
!= 0) {
2404 (void) printf(gettext(" (resilvering)"));
2408 if (cb
->vcdl
!= NULL
) {
2409 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2411 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2415 /* Display vdev initialization and trim status for leaves. */
2416 if (children
== 0) {
2417 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2418 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2421 (void) printf("\n");
2423 for (c
= 0; c
< children
; c
++) {
2424 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2426 /* Don't print logs or holes here */
2427 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2429 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2431 if (islog
|| ishole
)
2433 /* Only print normal classes here */
2434 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2437 /* Provide vdev_rebuild_stats to children if available */
2439 (void) nvlist_lookup_uint64_array(nv
,
2440 ZPOOL_CONFIG_REBUILD_STATS
,
2441 (uint64_t **)&vrs
, &i
);
2444 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2445 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2446 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2453 * Print the configuration of an exported pool. Iterate over all vdevs in the
2454 * pool, printing out the name and status for each one.
2457 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2465 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2466 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2467 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2470 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2471 (uint64_t **)&vs
, &c
) == 0);
2473 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2474 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2476 if (vs
->vs_aux
!= 0) {
2479 switch (vs
->vs_aux
) {
2480 case VDEV_AUX_OPEN_FAILED
:
2481 (void) printf(gettext("cannot open"));
2484 case VDEV_AUX_BAD_GUID_SUM
:
2485 (void) printf(gettext("missing device"));
2488 case VDEV_AUX_NO_REPLICAS
:
2489 (void) printf(gettext("insufficient replicas"));
2492 case VDEV_AUX_VERSION_NEWER
:
2493 (void) printf(gettext("newer version"));
2496 case VDEV_AUX_UNSUP_FEAT
:
2497 (void) printf(gettext("unsupported feature(s)"));
2500 case VDEV_AUX_ERR_EXCEEDED
:
2501 (void) printf(gettext("too many errors"));
2504 case VDEV_AUX_ACTIVE
:
2505 (void) printf(gettext("currently in use"));
2508 case VDEV_AUX_CHILDREN_OFFLINE
:
2509 (void) printf(gettext("all children offline"));
2513 (void) printf(gettext("corrupted data"));
2517 (void) printf("\n");
2519 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2520 &child
, &children
) != 0)
2523 for (c
= 0; c
< children
; c
++) {
2524 uint64_t is_log
= B_FALSE
;
2526 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2530 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2533 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2534 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2535 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2539 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2540 &child
, &children
) == 0) {
2541 (void) printf(gettext("\tcache\n"));
2542 for (c
= 0; c
< children
; c
++) {
2543 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2545 (void) printf("\t %s\n", vname
);
2550 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2551 &child
, &children
) == 0) {
2552 (void) printf(gettext("\tspares\n"));
2553 for (c
= 0; c
< children
; c
++) {
2554 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2556 (void) printf("\t %s\n", vname
);
2563 * Print specialized class vdevs.
2565 * These are recorded as top level vdevs in the main pool child array
2566 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2567 * print_status_config() or print_import_config() to print the top level
2568 * class vdevs then any of their children (eg mirrored slogs) are printed
2569 * recursively - which works because only the top level vdev is marked.
2572 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2577 boolean_t printed
= B_FALSE
;
2579 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2581 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2585 for (c
= 0; c
< children
; c
++) {
2586 uint64_t is_log
= B_FALSE
;
2590 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2594 bias
= VDEV_ALLOC_CLASS_LOGS
;
2596 (void) nvlist_lookup_string(child
[c
],
2597 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2598 (void) nvlist_lookup_string(child
[c
],
2599 ZPOOL_CONFIG_TYPE
, &type
);
2602 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2604 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2608 (void) printf("\t%s\t\n", gettext(class));
2612 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2613 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2614 if (cb
->cb_print_status
)
2615 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2618 print_import_config(cb
, name
, child
[c
], 2);
2624 * Display the status for the given pool.
2627 show_import(nvlist_t
*config
)
2629 uint64_t pool_state
;
2633 uint64_t hostid
= 0;
2635 char *hostname
= "unknown";
2636 nvlist_t
*nvroot
, *nvinfo
;
2637 zpool_status_t reason
;
2638 zpool_errata_t errata
;
2642 status_cbdata_t cb
= { 0 };
2644 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2646 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2648 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2650 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2653 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2654 (uint64_t **)&vs
, &vsc
) == 0);
2655 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2657 reason
= zpool_import_status(config
, &msgid
, &errata
);
2659 (void) printf(gettext(" pool: %s\n"), name
);
2660 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2661 (void) printf(gettext(" state: %s"), health
);
2662 if (pool_state
== POOL_STATE_DESTROYED
)
2663 (void) printf(gettext(" (DESTROYED)"));
2664 (void) printf("\n");
2667 case ZPOOL_STATUS_MISSING_DEV_R
:
2668 case ZPOOL_STATUS_MISSING_DEV_NR
:
2669 case ZPOOL_STATUS_BAD_GUID_SUM
:
2670 printf_color(ANSI_BOLD
, gettext("status: "));
2671 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2672 "missing from the system.\n"));
2675 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2676 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2677 printf_color(ANSI_BOLD
, gettext("status: "));
2678 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2679 " corrupted data.\n"));
2682 case ZPOOL_STATUS_CORRUPT_DATA
:
2684 gettext(" status: The pool data is corrupted.\n"));
2687 case ZPOOL_STATUS_OFFLINE_DEV
:
2688 printf_color(ANSI_BOLD
, gettext("status: "));
2689 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2690 "are offlined.\n"));
2693 case ZPOOL_STATUS_CORRUPT_POOL
:
2694 printf_color(ANSI_BOLD
, gettext("status: "));
2695 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2699 case ZPOOL_STATUS_VERSION_OLDER
:
2700 printf_color(ANSI_BOLD
, gettext("status: "));
2701 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2702 "a legacy on-disk version.\n"));
2705 case ZPOOL_STATUS_VERSION_NEWER
:
2706 printf_color(ANSI_BOLD
, gettext("status: "));
2707 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2708 "an incompatible version.\n"));
2711 case ZPOOL_STATUS_FEAT_DISABLED
:
2712 printf_color(ANSI_BOLD
, gettext("status: "));
2713 printf_color(ANSI_YELLOW
, gettext("Some supported and "
2714 "requested features are not enabled on the pool.\n"));
2717 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2718 printf_color(ANSI_BOLD
, gettext("status: "));
2719 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2720 "the file(s) indicated by the 'compatibility'\n"
2724 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2725 printf_color(ANSI_BOLD
, gettext("status: "));
2726 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2727 "feature(s) not supported on this system:\n"));
2728 color_start(ANSI_YELLOW
);
2729 zpool_print_unsup_feat(config
);
2733 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2734 printf_color(ANSI_BOLD
, gettext("status: "));
2735 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2736 "accessed in read-only mode on this system. It\n\tcannot be"
2737 " accessed in read-write mode because it uses the "
2738 "following\n\tfeature(s) not supported on this system:\n"));
2739 color_start(ANSI_YELLOW
);
2740 zpool_print_unsup_feat(config
);
2744 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2745 printf_color(ANSI_BOLD
, gettext("status: "));
2746 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2747 "imported by another system.\n"));
2750 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2751 printf_color(ANSI_BOLD
, gettext("status: "));
2752 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2753 "multihost property on. It cannot\n\tbe safely imported "
2754 "when the system hostid is not set.\n"));
2757 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2758 printf_color(ANSI_BOLD
, gettext("status: "));
2759 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2760 "by another system.\n"));
2763 case ZPOOL_STATUS_FAULTED_DEV_R
:
2764 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2765 printf_color(ANSI_BOLD
, gettext("status: "));
2766 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2770 case ZPOOL_STATUS_BAD_LOG
:
2771 printf_color(ANSI_BOLD
, gettext("status: "));
2772 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2776 case ZPOOL_STATUS_RESILVERING
:
2777 case ZPOOL_STATUS_REBUILDING
:
2778 printf_color(ANSI_BOLD
, gettext("status: "));
2779 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2780 "being resilvered.\n"));
2783 case ZPOOL_STATUS_ERRATA
:
2784 printf_color(ANSI_BOLD
, gettext("status: "));
2785 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2789 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2790 printf_color(ANSI_BOLD
, gettext("status: "));
2791 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2792 "configured to use a non-native block size.\n"
2793 "\tExpect reduced performance.\n"));
2798 * No other status can be seen when importing pools.
2800 assert(reason
== ZPOOL_STATUS_OK
);
2804 * Print out an action according to the overall state of the pool.
2806 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2807 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2808 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2809 (void) printf(gettext(" action: The pool can be "
2810 "imported using its name or numeric identifier, "
2811 "though\n\tsome features will not be available "
2812 "without an explicit 'zpool upgrade'.\n"));
2813 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
2814 (void) printf(gettext(" action: The pool can be "
2815 "imported using its name or numeric\n\tidentifier, "
2816 "though the file(s) indicated by its "
2817 "'compatibility'\n\tproperty cannot be parsed at "
2819 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2820 (void) printf(gettext(" action: The pool can be "
2821 "imported using its name or numeric "
2822 "identifier and\n\tthe '-f' flag.\n"));
2823 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2825 case ZPOOL_ERRATA_NONE
:
2828 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2829 (void) printf(gettext(" action: The pool can "
2830 "be imported using its name or numeric "
2831 "identifier,\n\thowever there is a compat"
2832 "ibility issue which should be corrected"
2833 "\n\tby running 'zpool scrub'\n"));
2836 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2837 (void) printf(gettext(" action: The pool can"
2838 "not be imported with this version of ZFS "
2839 "due to\n\tan active asynchronous destroy. "
2840 "Revert to an earlier version\n\tand "
2841 "allow the destroy to complete before "
2845 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2846 (void) printf(gettext(" action: Existing "
2847 "encrypted datasets contain an on-disk "
2848 "incompatibility, which\n\tneeds to be "
2849 "corrected. Backup these datasets to new "
2850 "encrypted datasets\n\tand destroy the "
2854 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2855 (void) printf(gettext(" action: Existing "
2856 "encrypted snapshots and bookmarks contain "
2857 "an on-disk\n\tincompatibility. This may "
2858 "cause on-disk corruption if they are used"
2859 "\n\twith 'zfs recv'. To correct the "
2860 "issue, enable the bookmark_v2 feature.\n\t"
2861 "No additional action is needed if there "
2862 "are no encrypted snapshots or\n\t"
2863 "bookmarks. If preserving the encrypted "
2864 "snapshots and bookmarks is\n\trequired, "
2865 "use a non-raw send to backup and restore "
2866 "them. Alternately,\n\tthey may be removed"
2867 " to resolve the incompatibility.\n"));
2871 * All errata must contain an action message.
2876 (void) printf(gettext(" action: The pool can be "
2877 "imported using its name or numeric "
2880 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2881 (void) printf(gettext(" action: The pool can be imported "
2882 "despite missing or damaged devices. The\n\tfault "
2883 "tolerance of the pool may be compromised if imported.\n"));
2886 case ZPOOL_STATUS_VERSION_NEWER
:
2887 (void) printf(gettext(" action: The pool cannot be "
2888 "imported. Access the pool on a system running "
2889 "newer\n\tsoftware, or recreate the pool from "
2892 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2893 printf_color(ANSI_BOLD
, gettext("action: "));
2894 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2895 "imported. Access the pool on a system that "
2896 "supports\n\tthe required feature(s), or recreate "
2897 "the pool from backup.\n"));
2899 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2900 printf_color(ANSI_BOLD
, gettext("action: "));
2901 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2902 "imported in read-write mode. Import the pool "
2904 "\t\"-o readonly=on\", access the pool on a system "
2905 "that supports the\n\trequired feature(s), or "
2906 "recreate the pool from backup.\n"));
2908 case ZPOOL_STATUS_MISSING_DEV_R
:
2909 case ZPOOL_STATUS_MISSING_DEV_NR
:
2910 case ZPOOL_STATUS_BAD_GUID_SUM
:
2911 (void) printf(gettext(" action: The pool cannot be "
2912 "imported. Attach the missing\n\tdevices and try "
2915 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2916 VERIFY0(nvlist_lookup_nvlist(config
,
2917 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2919 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2920 hostname
= fnvlist_lookup_string(nvinfo
,
2921 ZPOOL_CONFIG_MMP_HOSTNAME
);
2923 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2924 hostid
= fnvlist_lookup_uint64(nvinfo
,
2925 ZPOOL_CONFIG_MMP_HOSTID
);
2927 (void) printf(gettext(" action: The pool must be "
2928 "exported from %s (hostid=%lx)\n\tbefore it "
2929 "can be safely imported.\n"), hostname
,
2930 (unsigned long) hostid
);
2932 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2933 (void) printf(gettext(" action: Set a unique system "
2934 "hostid with the zgenhostid(8) command.\n"));
2937 (void) printf(gettext(" action: The pool cannot be "
2938 "imported due to damaged devices or data.\n"));
2942 /* Print the comment attached to the pool. */
2943 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2944 (void) printf(gettext("comment: %s\n"), comment
);
2947 * If the state is "closed" or "can't open", and the aux state
2948 * is "corrupt data":
2950 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2951 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2952 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2953 if (pool_state
== POOL_STATE_DESTROYED
)
2954 (void) printf(gettext("\tThe pool was destroyed, "
2955 "but can be imported using the '-Df' flags.\n"));
2956 else if (pool_state
!= POOL_STATE_EXPORTED
)
2957 (void) printf(gettext("\tThe pool may be active on "
2958 "another system, but can be imported using\n\t"
2959 "the '-f' flag.\n"));
2962 if (msgid
!= NULL
) {
2963 (void) printf(gettext(
2964 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
2968 (void) printf(gettext(" config:\n\n"));
2970 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
2972 if (cb
.cb_namewidth
< 10)
2973 cb
.cb_namewidth
= 10;
2975 print_import_config(&cb
, name
, nvroot
, 0);
2977 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
2978 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
2979 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
2981 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
2982 (void) printf(gettext("\n\tAdditional devices are known to "
2983 "be part of this pool, though their\n\texact "
2984 "configuration cannot be determined.\n"));
2989 zfs_force_import_required(nvlist_t
*config
)
2992 uint64_t hostid
= 0;
2995 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
2996 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
2998 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3001 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3002 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3003 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3004 ZPOOL_CONFIG_MMP_STATE
);
3006 if (mmp_state
!= MMP_STATE_INACTIVE
)
3014 * Perform the import for the given configuration. This passes the heavy
3015 * lifting off to zpool_import_props(), and then mounts the datasets contained
3019 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3020 nvlist_t
*props
, int flags
)
3023 zpool_handle_t
*zhp
;
3027 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3028 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3030 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3031 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3032 "is formatted using an unsupported ZFS version\n"), name
);
3034 } else if (zfs_force_import_required(config
) &&
3035 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3036 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3039 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3040 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3041 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3042 ZPOOL_CONFIG_MMP_STATE
);
3044 if (mmp_state
== MMP_STATE_ACTIVE
) {
3045 char *hostname
= "<unknown>";
3046 uint64_t hostid
= 0;
3048 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3049 hostname
= fnvlist_lookup_string(nvinfo
,
3050 ZPOOL_CONFIG_MMP_HOSTNAME
);
3052 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3053 hostid
= fnvlist_lookup_uint64(nvinfo
,
3054 ZPOOL_CONFIG_MMP_HOSTID
);
3056 (void) fprintf(stderr
, gettext("cannot import '%s': "
3057 "pool is imported on %s (hostid: "
3058 "0x%lx)\nExport the pool on the other system, "
3059 "then run 'zpool import'.\n"),
3060 name
, hostname
, (unsigned long) hostid
);
3061 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3062 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3063 "pool has the multihost property on and the\n"
3064 "system's hostid is not set. Set a unique hostid "
3065 "with the zgenhostid(8) command.\n"), name
);
3067 char *hostname
= "<unknown>";
3068 uint64_t timestamp
= 0;
3069 uint64_t hostid
= 0;
3071 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3072 hostname
= fnvlist_lookup_string(config
,
3073 ZPOOL_CONFIG_HOSTNAME
);
3075 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3076 timestamp
= fnvlist_lookup_uint64(config
,
3077 ZPOOL_CONFIG_TIMESTAMP
);
3079 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3080 hostid
= fnvlist_lookup_uint64(config
,
3081 ZPOOL_CONFIG_HOSTID
);
3083 (void) fprintf(stderr
, gettext("cannot import '%s': "
3084 "pool was previously in use from another system.\n"
3085 "Last accessed by %s (hostid=%lx) at %s"
3086 "The pool can be imported, use 'zpool import -f' "
3087 "to import the pool.\n"), name
, hostname
,
3088 (unsigned long)hostid
, ctime((time_t *)×tamp
));
3094 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3097 if (newname
!= NULL
)
3098 name
= (char *)newname
;
3100 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3104 * Loading keys is best effort. We don't want to return immediately
3105 * if it fails but we do want to give the error to the caller.
3107 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
3108 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
3113 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3114 !(flags
& ZFS_IMPORT_ONLY
) &&
3115 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3124 typedef struct target_exists_args
{
3125 const char *poolname
;
3127 } target_exists_args_t
;
3130 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3132 target_exists_args_t
*args
= data
;
3133 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3139 if (args
->poolname
!= NULL
) {
3142 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3144 if (strcmp(pool_name
, args
->poolname
) == 0)
3149 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3151 if (pool_guid
== args
->poolguid
)
3159 * zpool checkpoint <pool>
3160 * checkpoint --discard <pool>
3162 * -d Discard the checkpoint from a checkpointed
3165 * -w Wait for discarding a checkpoint to complete.
3168 * Checkpoints the specified pool, by taking a "snapshot" of its
3169 * current state. A pool can only have one checkpoint at a time.
3172 zpool_do_checkpoint(int argc
, char **argv
)
3174 boolean_t discard
, wait
;
3176 zpool_handle_t
*zhp
;
3179 struct option long_options
[] = {
3180 {"discard", no_argument
, NULL
, 'd'},
3181 {"wait", no_argument
, NULL
, 'w'},
3187 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3196 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3202 if (wait
&& !discard
) {
3203 (void) fprintf(stderr
, gettext("--wait only valid when "
3204 "--discard also specified\n"));
3212 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3217 (void) fprintf(stderr
, gettext("too many arguments\n"));
3223 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3224 /* As a special case, check for use of '/' in the name */
3225 if (strchr(pool
, '/') != NULL
)
3226 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3227 "doesn't work on datasets. To save the state "
3228 "of a dataset from a specific point in time "
3229 "please use 'zfs snapshot'\n"));
3234 err
= (zpool_discard_checkpoint(zhp
) != 0);
3235 if (err
== 0 && wait
)
3236 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3238 err
= (zpool_checkpoint(zhp
) != 0);
3246 #define CHECKPOINT_OPT 1024
3249 * zpool import [-d dir] [-D]
3250 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3251 * [-d dir | -c cachefile] [-f] -a
3252 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3253 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
3255 * -c Read pool information from a cachefile instead of searching
3258 * -d Scan in a specific directory, other than /dev/. More than
3259 * one directory can be specified using multiple '-d' options.
3261 * -D Scan for previously destroyed pools or import all or only
3262 * specified destroyed pools.
3264 * -R Temporarily import the pool, with all mountpoints relative to
3265 * the given root. The pool will remain exported when the machine
3268 * -V Import even in the presence of faulted vdevs. This is an
3269 * intentionally undocumented option for testing purposes, and
3270 * treats the pool configuration as complete, leaving any bad
3271 * vdevs in the FAULTED state. In other words, it does verbatim
3274 * -f Force import, even if it appears that the pool is active.
3276 * -F Attempt rewind if necessary.
3278 * -n See if rewind would work, but don't actually rewind.
3280 * -N Import the pool but don't mount datasets.
3282 * -T Specify a starting txg to use for import. This option is
3283 * intentionally undocumented option for testing purposes.
3285 * -a Import all pools found.
3287 * -l Load encryption keys while importing.
3289 * -o Set property=value and/or temporary mount options (without '=').
3291 * -s Scan using the default search path, the libblkid cache will
3294 * --rewind-to-checkpoint
3295 * Import the pool and revert back to the checkpoint.
3297 * The import command scans for pools to import, and import pools based on pool
3298 * name and GUID. The pool can also be renamed as part of the import process.
3301 zpool_do_import(int argc
, char **argv
)
3303 char **searchdirs
= NULL
;
3304 char *env
, *envdup
= NULL
;
3308 nvlist_t
*pools
= NULL
;
3309 boolean_t do_all
= B_FALSE
;
3310 boolean_t do_destroyed
= B_FALSE
;
3311 char *mntopts
= NULL
;
3314 uint64_t searchguid
= 0;
3315 char *searchname
= NULL
;
3317 nvlist_t
*found_config
;
3318 nvlist_t
*policy
= NULL
;
3319 nvlist_t
*props
= NULL
;
3321 int flags
= ZFS_IMPORT_NORMAL
;
3322 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3323 boolean_t dryrun
= B_FALSE
;
3324 boolean_t do_rewind
= B_FALSE
;
3325 boolean_t xtreme_rewind
= B_FALSE
;
3326 boolean_t do_scan
= B_FALSE
;
3327 boolean_t pool_exists
= B_FALSE
;
3328 uint64_t pool_state
, txg
= -1ULL;
3329 char *cachefile
= NULL
;
3330 importargs_t idata
= { 0 };
3333 struct option long_options
[] = {
3334 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3339 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3340 long_options
, NULL
)) != -1) {
3349 if (searchdirs
== NULL
) {
3350 searchdirs
= safe_malloc(sizeof (char *));
3352 char **tmp
= safe_malloc((nsearch
+ 1) *
3354 bcopy(searchdirs
, tmp
, nsearch
*
3359 searchdirs
[nsearch
++] = optarg
;
3362 do_destroyed
= B_TRUE
;
3365 flags
|= ZFS_IMPORT_ANY_HOST
;
3371 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3374 flags
|= ZFS_IMPORT_MISSING_LOG
;
3380 flags
|= ZFS_IMPORT_ONLY
;
3383 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3386 if (add_prop_list(optarg
, propval
,
3394 if (add_prop_list(zpool_prop_to_name(
3395 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3397 if (add_prop_list_default(zpool_prop_to_name(
3398 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3405 flags
|= ZFS_IMPORT_TEMP_NAME
;
3406 if (add_prop_list_default(zpool_prop_to_name(
3407 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3413 txg
= strtoull(optarg
, &endptr
, 0);
3414 if (errno
!= 0 || *endptr
!= '\0') {
3415 (void) fprintf(stderr
,
3416 gettext("invalid txg value\n"));
3419 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3422 flags
|= ZFS_IMPORT_VERBATIM
;
3425 xtreme_rewind
= B_TRUE
;
3427 case CHECKPOINT_OPT
:
3428 flags
|= ZFS_IMPORT_CHECKPOINT
;
3431 (void) fprintf(stderr
, gettext("missing argument for "
3432 "'%c' option\n"), optopt
);
3436 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3445 if (cachefile
&& nsearch
!= 0) {
3446 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3450 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3451 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3455 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3456 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3461 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3462 (void) fprintf(stderr
,
3463 gettext("-n or -X only meaningful with -F\n"));
3467 rewind_policy
= ZPOOL_TRY_REWIND
;
3469 rewind_policy
= ZPOOL_DO_REWIND
;
3471 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3473 /* In the future, we can capture further policy and include it here */
3474 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3475 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3476 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3477 rewind_policy
) != 0)
3480 /* check argument count */
3483 (void) fprintf(stderr
, gettext("too many arguments\n"));
3488 (void) fprintf(stderr
, gettext("too many arguments\n"));
3494 * Check for the effective uid. We do this explicitly here because
3495 * otherwise any attempt to discover pools will silently fail.
3497 if (argc
== 0 && geteuid() != 0) {
3498 (void) fprintf(stderr
, gettext("cannot "
3499 "discover pools: permission denied\n"));
3500 if (searchdirs
!= NULL
)
3504 nvlist_free(policy
);
3509 * Depending on the arguments given, we do one of the following:
3511 * <none> Iterate through all pools and display information about
3514 * -a Iterate through all pools and try to import each one.
3516 * <id> Find the pool that corresponds to the given GUID/pool
3517 * name and import that one.
3519 * -D Above options applies only to destroyed pools.
3525 searchguid
= strtoull(argv
[0], &endptr
, 10);
3526 if (errno
!= 0 || *endptr
!= '\0') {
3527 searchname
= argv
[0];
3530 found_config
= NULL
;
3533 * User specified a name or guid. Ensure it's unique.
3535 target_exists_args_t search
= {searchname
, searchguid
};
3536 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3540 * Check the environment for the preferred search path.
3542 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3545 envdup
= strdup(env
);
3547 dir
= strtok(envdup
, ":");
3548 while (dir
!= NULL
) {
3549 if (searchdirs
== NULL
) {
3550 searchdirs
= safe_malloc(sizeof (char *));
3552 char **tmp
= safe_malloc((nsearch
+ 1) *
3554 bcopy(searchdirs
, tmp
, nsearch
*
3559 searchdirs
[nsearch
++] = dir
;
3560 dir
= strtok(NULL
, ":");
3564 idata
.path
= searchdirs
;
3565 idata
.paths
= nsearch
;
3566 idata
.poolname
= searchname
;
3567 idata
.guid
= searchguid
;
3568 idata
.cachefile
= cachefile
;
3569 idata
.scan
= do_scan
;
3570 idata
.policy
= policy
;
3572 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3574 if (pools
!= NULL
&& pool_exists
&&
3575 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3576 (void) fprintf(stderr
, gettext("cannot import '%s': "
3577 "a pool with that name already exists\n"),
3579 (void) fprintf(stderr
, gettext("use the form '%s "
3580 "<pool | id> <newpool>' to give it a new name\n"),
3583 } else if (pools
== NULL
&& pool_exists
) {
3584 (void) fprintf(stderr
, gettext("cannot import '%s': "
3585 "a pool with that name is already created/imported,\n"),
3587 (void) fprintf(stderr
, gettext("and no additional pools "
3588 "with that name were found\n"));
3590 } else if (pools
== NULL
) {
3592 (void) fprintf(stderr
, gettext("cannot import '%s': "
3593 "no such pool available\n"), argv
[0]);
3599 if (searchdirs
!= NULL
)
3603 nvlist_free(policy
);
3610 * At this point we have a list of import candidate configs. Even if
3611 * we were searching by pool name or guid, we still need to
3612 * post-process the list to deal with pool state and possible
3618 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3620 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3622 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3624 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3626 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3629 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3636 (void) printf("\n");
3639 err
|= do_import(config
, NULL
, mntopts
,
3642 show_import(config
);
3644 } else if (searchname
!= NULL
) {
3648 * We are searching for a pool based on name.
3650 verify(nvlist_lookup_string(config
,
3651 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3653 if (strcmp(name
, searchname
) == 0) {
3654 if (found_config
!= NULL
) {
3655 (void) fprintf(stderr
, gettext(
3656 "cannot import '%s': more than "
3657 "one matching pool\n"), searchname
);
3658 (void) fprintf(stderr
, gettext(
3659 "import by numeric ID instead\n"));
3662 found_config
= config
;
3668 * Search for a pool by guid.
3670 verify(nvlist_lookup_uint64(config
,
3671 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3673 if (guid
== searchguid
)
3674 found_config
= config
;
3679 * If we were searching for a specific pool, verify that we found a
3680 * pool, and then do the import.
3682 if (argc
!= 0 && err
== 0) {
3683 if (found_config
== NULL
) {
3684 (void) fprintf(stderr
, gettext("cannot import '%s': "
3685 "no such pool available\n"), argv
[0]);
3688 err
|= do_import(found_config
, argc
== 1 ? NULL
:
3689 argv
[1], mntopts
, props
, flags
);
3694 * If we were just looking for pools, report an error if none were
3697 if (argc
== 0 && first
)
3698 (void) fprintf(stderr
,
3699 gettext("no pools available to import\n"));
3704 nvlist_free(policy
);
3705 if (searchdirs
!= NULL
)
3710 return (err
? 1 : 0);
3714 * zpool sync [-f] [pool] ...
3716 * -f (undocumented) force uberblock (and config including zpool cache file)
3719 * Sync the specified pool(s).
3720 * Without arguments "zpool sync" will sync all pools.
3721 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3725 zpool_do_sync(int argc
, char **argv
)
3728 boolean_t force
= B_FALSE
;
3731 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3737 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3746 /* if argc == 0 we will execute zpool_sync_one on all pools */
3747 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
, zpool_sync_one
,
3753 typedef struct iostat_cbdata
{
3758 char **cb_vdev_names
; /* Only show these vdevs */
3759 unsigned int cb_vdev_names_count
;
3760 boolean_t cb_verbose
;
3761 boolean_t cb_literal
;
3762 boolean_t cb_scripted
;
3763 zpool_list_t
*cb_list
;
3764 vdev_cmd_data_list_t
*vcdl
;
3768 typedef struct name_and_columns
{
3769 const char *name
; /* Column name */
3770 unsigned int columns
; /* Center name to this number of columns */
3771 } name_and_columns_t
;
3773 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */
3775 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3777 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3779 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3780 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL
}},
3781 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3782 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3783 {"trimq_write", 2}, {NULL
}},
3784 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3785 {"asyncq_wait", 2}, {NULL
}},
3786 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3787 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3788 {"trim", 2}, {NULL
}},
3791 /* Shorthand - if "columns" field not set, default to 1 column */
3792 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3794 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3796 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3797 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL
}},
3798 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3799 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3800 {"pend"}, {"activ"}, {NULL
}},
3801 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3802 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL
}},
3803 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3804 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3807 static const char *histo_to_title
[] = {
3808 [IOS_L_HISTO
] = "latency",
3809 [IOS_RQ_HISTO
] = "req_size",
3813 * Return the number of labels in a null-terminated name_and_columns_t
3818 label_array_len(const name_and_columns_t
*labels
)
3822 while (labels
[i
].name
)
3829 * Return the number of strings in a null-terminated string array.
3832 * const char foo[] = {"bar", "baz", NULL}
3837 str_array_len(const char *array
[])
3848 * Return a default column width for default/latency/queue columns. This does
3849 * not include histograms, which have their columns autosized.
3852 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3854 unsigned long column_width
= 5; /* Normal niceprint */
3855 static unsigned long widths
[] = {
3857 * Choose some sane default column sizes for printing the
3860 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3861 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3862 [IOS_QUEUES
] = 6, /* 1M queue entries */
3863 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
3864 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
3868 column_width
= widths
[type
];
3870 return (column_width
);
3874 * Print the column labels, i.e:
3876 * capacity operations bandwidth
3877 * alloc free read write read write ...
3879 * If force_column_width is set, use it for the column width. If not set, use
3880 * the default column width.
3883 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3884 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3887 int text_start
, rw_column_width
, spaces_to_end
;
3888 uint64_t flags
= cb
->cb_flags
;
3890 unsigned int column_width
= force_column_width
;
3892 /* For each bit set in flags */
3893 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3894 idx
= lowbit64(f
) - 1;
3895 if (!force_column_width
)
3896 column_width
= default_column_width(cb
, idx
);
3897 /* Print our top labels centered over "read write" label. */
3898 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3899 const char *name
= labels
[idx
][i
].name
;
3901 * We treat labels[][].columns == 0 as shorthand
3902 * for one column. It makes writing out the label
3903 * tables more concise.
3905 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3906 unsigned int slen
= strlen(name
);
3908 rw_column_width
= (column_width
* columns
) +
3909 (2 * (columns
- 1));
3911 text_start
= (int)((rw_column_width
) / columns
-
3916 printf(" "); /* Two spaces between columns */
3918 /* Space from beginning of column to label */
3919 for (s
= 0; s
< text_start
; s
++)
3924 /* Print space after label to end of column */
3925 spaces_to_end
= rw_column_width
- text_start
- slen
;
3926 if (spaces_to_end
< 0)
3929 for (s
= 0; s
< spaces_to_end
; s
++)
3937 * print_cmd_columns - Print custom column titles from -c
3939 * If the user specified the "zpool status|iostat -c" then print their custom
3940 * column titles in the header. For example, print_cmd_columns() would print
3941 * the " col1 col2" part of this:
3943 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
3945 * capacity operations bandwidth
3946 * pool alloc free read write read write col1 col2
3947 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3948 * mypool 269K 1008M 0 0 107 946
3949 * mirror 269K 1008M 0 0 107 946
3950 * sdb - - 0 0 102 473 val1 val2
3951 * sdc - - 0 0 5 473 val1 val2
3952 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
3955 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
3958 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
3960 if (vcdl
->count
== 0 || data
== NULL
)
3964 * Each vdev cmd should have the same column names unless the user did
3965 * something weird with their cmd. Just take the column names from the
3966 * first vdev and assume it works for all of them.
3968 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
3971 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
3974 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
3975 vcdl
->uniq_cols
[i
]);
3982 * Utility function to print out a line of dashes like:
3984 * -------------------------------- ----- ----- ----- ----- -----
3986 * ...or a dashed named-row line like:
3992 * @force_column_width If non-zero, use the value as the column width.
3993 * Otherwise use the default column widths.
3995 * @name: Print a dashed named-row line starting
3996 * with @name. Otherwise, print a regular
4000 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4004 unsigned int namewidth
;
4005 uint64_t flags
= cb
->cb_flags
;
4008 const name_and_columns_t
*labels
;
4012 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4013 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4014 } else if (cb
->cb_vdev_names_count
) {
4020 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4021 name
? strlen(name
) : 0);
4025 printf("%-*s", namewidth
, name
);
4027 for (i
= 0; i
< namewidth
; i
++)
4031 /* For each bit in flags */
4032 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4033 unsigned int column_width
;
4034 idx
= lowbit64(f
) - 1;
4035 if (force_column_width
)
4036 column_width
= force_column_width
;
4038 column_width
= default_column_width(cb
, idx
);
4040 labels
= iostat_bottom_labels
[idx
];
4041 for (i
= 0; i
< label_array_len(labels
); i
++) {
4043 printf(" %*s-", column_width
- 1, " ");
4045 printf(" %.*s", column_width
,
4046 "--------------------");
4053 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4054 unsigned int force_column_width
)
4056 print_iostat_dashes(cb
, force_column_width
, NULL
);
4060 print_iostat_separator(iostat_cbdata_t
*cb
)
4062 print_iostat_separator_impl(cb
, 0);
4066 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4067 const char *histo_vdev_name
)
4069 unsigned int namewidth
;
4072 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4073 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4074 } else if (cb
->cb_vdev_names_count
) {
4080 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4081 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4083 if (histo_vdev_name
)
4084 printf("%-*s", namewidth
, histo_vdev_name
);
4086 printf("%*s", namewidth
, "");
4089 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4092 printf("%-*s", namewidth
, title
);
4094 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4095 if (cb
->vcdl
!= NULL
)
4096 print_cmd_columns(cb
->vcdl
, 0);
4100 print_iostat_separator_impl(cb
, force_column_width
);
4102 if (cb
->vcdl
!= NULL
)
4103 print_cmd_columns(cb
->vcdl
, 1);
4109 print_iostat_header(iostat_cbdata_t
*cb
)
4111 print_iostat_header_impl(cb
, 0, NULL
);
4116 * Display a single statistic.
4119 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4120 unsigned int column_size
, boolean_t scripted
)
4124 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4127 printf("\t%s", buf
);
4129 printf(" %*s", column_size
, buf
);
4133 * Calculate the default vdev stats
4135 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4136 * stats into calcvs.
4139 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4140 vdev_stat_t
*calcvs
)
4144 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4145 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4146 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4148 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4149 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4153 * Internal representation of the extended iostats data.
4155 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4156 * or single uint64_t's. We make both look like arrays to make them easier
4157 * to process. In order to make single uint64_t's look like arrays, we set
4158 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4159 * we can just use *data and count.
4163 uint_t count
; /* Number of entries in data[] */
4164 uint64_t __data
; /* Only used when data is a single uint64_t */
4168 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4172 for (i
= 0; i
< len
; i
++)
4173 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4179 * Helper function to lookup a uint64_t array or uint64_t value and store its
4180 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4181 * it look like a one element array to make it easier to process.
4184 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4185 struct stat_array
*nva
)
4190 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4191 switch (nvpair_type(tmp
)) {
4192 case DATA_TYPE_UINT64_ARRAY
:
4193 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4195 case DATA_TYPE_UINT64
:
4196 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4197 nva
->data
= &nva
->__data
;
4201 /* Not a uint64_t */
4210 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4211 * subtract them, and return the results in a newly allocated stat_array.
4212 * You must free the returned array after you are done with it with
4213 * free_calc_stats().
4215 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4218 static struct stat_array
*
4219 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4222 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4223 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4225 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4227 /* Extract our extended stats nvlist from the main list */
4228 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4231 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4235 newnva
= safe_malloc(alloc_size
);
4236 oldnva
= safe_malloc(alloc_size
);
4237 calcnva
= safe_malloc(alloc_size
);
4239 for (j
= 0; j
< len
; j
++) {
4240 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4242 calcnva
[j
].count
= newnva
[j
].count
;
4243 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4244 calcnva
[j
].data
= safe_malloc(alloc_size
);
4245 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4248 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4250 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4251 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4260 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4263 for (i
= 0; i
< len
; i
++)
4270 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4271 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4277 enum zfs_nicenum_format format
;
4278 unsigned int buckets
;
4279 unsigned int start_bucket
;
4282 format
= ZFS_NICENUM_RAW
;
4284 format
= ZFS_NICENUM_1024
;
4286 /* All these histos are the same size, so just use nva[0].count */
4287 buckets
= nva
[0].count
;
4289 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4290 /* Start at 512 - req size should never be lower than this */
4296 for (j
= start_bucket
; j
< buckets
; j
++) {
4297 /* Print histogram bucket label */
4298 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4299 /* Ending range of this bucket */
4300 val
= (1UL << (j
+ 1)) - 1;
4301 zfs_nicetime(val
, buf
, sizeof (buf
));
4303 /* Request size (starting range of bucket) */
4305 zfs_nicenum(val
, buf
, sizeof (buf
));
4308 if (cb
->cb_scripted
)
4309 printf("%llu", (u_longlong_t
)val
);
4311 printf("%-*s", namewidth
, buf
);
4313 /* Print the values on the line */
4314 for (i
= 0; i
< len
; i
++) {
4315 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4316 column_width
, cb
->cb_scripted
);
4323 print_solid_separator(unsigned int length
)
4331 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4332 nvlist_t
*newnv
, double scale
, const char *name
)
4334 unsigned int column_width
;
4335 unsigned int namewidth
;
4336 unsigned int entire_width
;
4337 enum iostat_type type
;
4338 struct stat_array
*nva
;
4340 unsigned int names_len
;
4342 /* What type of histo are we? */
4343 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4345 /* Get NULL-terminated array of nvlist names for our histo */
4346 names
= vsx_type_to_nvlist
[type
];
4347 names_len
= str_array_len(names
); /* num of names */
4349 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4351 if (cb
->cb_literal
) {
4352 column_width
= MAX(5,
4353 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4358 namewidth
= MAX(cb
->cb_namewidth
,
4359 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4362 * Calculate the entire line width of what we're printing. The
4363 * +2 is for the two spaces between columns:
4367 /* |___| <---------- column_width */
4369 /* |__________| <--- entire_width */
4371 entire_width
= namewidth
+ (column_width
+ 2) *
4372 label_array_len(iostat_bottom_labels
[type
]);
4374 if (cb
->cb_scripted
)
4375 printf("%s\n", name
);
4377 print_iostat_header_impl(cb
, column_width
, name
);
4379 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4382 free_calc_stats(nva
, names_len
);
4383 if (!cb
->cb_scripted
)
4384 print_solid_separator(entire_width
);
4388 * Calculate the average latency of a power-of-two latency histogram
4391 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4394 uint64_t count
= 0, total
= 0;
4396 for (i
= 0; i
< buckets
; i
++) {
4398 * Our buckets are power-of-two latency ranges. Use the
4399 * midpoint latency of each bucket to calculate the average.
4407 if (histo
[i
] != 0) {
4408 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4413 /* Prevent divide by zero */
4414 return (count
== 0 ? 0 : total
/ count
);
4418 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4423 const char *names
[] = {
4424 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4425 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4426 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4427 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4428 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4429 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4430 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4431 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4432 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4433 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4434 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4435 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4438 struct stat_array
*nva
;
4440 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4441 enum zfs_nicenum_format format
;
4443 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4446 format
= ZFS_NICENUM_RAW
;
4448 format
= ZFS_NICENUM_1024
;
4450 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4451 val
= nva
[i
].data
[0];
4452 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4455 free_calc_stats(nva
, ARRAY_SIZE(names
));
4459 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4464 const char *names
[] = {
4465 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4466 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4467 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4468 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4469 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4470 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4471 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4472 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4473 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4474 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4476 struct stat_array
*nva
;
4478 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4479 enum zfs_nicenum_format format
;
4481 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4484 format
= ZFS_NICENUM_RAWTIME
;
4486 format
= ZFS_NICENUM_TIME
;
4488 /* Print our avg latencies on the line */
4489 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4490 /* Compute average latency for a latency histo */
4491 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4492 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4494 free_calc_stats(nva
, ARRAY_SIZE(names
));
4498 * Print default statistics (capacity/operations/bandwidth)
4501 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4503 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4504 enum zfs_nicenum_format format
;
4505 char na
; /* char to print for "not applicable" values */
4507 if (cb
->cb_literal
) {
4508 format
= ZFS_NICENUM_RAW
;
4511 format
= ZFS_NICENUM_1024
;
4515 /* only toplevel vdevs have capacity stats */
4516 if (vs
->vs_space
== 0) {
4517 if (cb
->cb_scripted
)
4518 printf("\t%c\t%c", na
, na
);
4520 printf(" %*c %*c", column_width
, na
, column_width
,
4523 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4525 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4526 column_width
, cb
->cb_scripted
);
4529 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4530 format
, column_width
, cb
->cb_scripted
);
4531 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4532 format
, column_width
, cb
->cb_scripted
);
4533 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4534 format
, column_width
, cb
->cb_scripted
);
4535 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4536 format
, column_width
, cb
->cb_scripted
);
4539 static const char *class_name
[] = {
4540 VDEV_ALLOC_BIAS_DEDUP
,
4541 VDEV_ALLOC_BIAS_SPECIAL
,
4542 VDEV_ALLOC_CLASS_LOGS
4546 * Print out all the statistics for the given vdev. This can either be the
4547 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4548 * is a verbose output, and we don't want to display the toplevel pool stats.
4550 * Returns the number of stat lines printed.
4553 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4554 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4556 nvlist_t
**oldchild
, **newchild
;
4557 uint_t c
, children
, oldchildren
;
4558 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4559 vdev_stat_t zerovs
= { 0 };
4566 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4569 calcvs
= safe_malloc(sizeof (*calcvs
));
4571 if (oldnv
!= NULL
) {
4572 verify(nvlist_lookup_uint64_array(oldnv
,
4573 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4578 /* Do we only want to see a specific vdev? */
4579 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
4580 /* Yes we do. Is this the vdev? */
4581 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
4583 * This is our vdev. Since it is the only vdev we
4584 * will be displaying, make depth = 0 so that it
4585 * doesn't get indented.
4592 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
4593 /* Couldn't match the name */
4598 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4599 (uint64_t **)&newvs
, &c
) == 0);
4602 * Print the vdev name unless it's is a histogram. Histograms
4603 * display the vdev name in the header itself.
4605 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4606 if (cb
->cb_scripted
) {
4609 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4610 (void) printf("%*s%s", depth
, "", name
);
4612 (void) printf("%*s%s%*s", depth
, "", name
,
4613 (int)(cb
->cb_namewidth
- strlen(name
) -
4618 /* Calculate our scaling factor */
4619 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4620 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4622 * If we specify printing histograms with no time interval, then
4623 * print the histogram numbers over the entire lifetime of the
4631 scale
= (double)NANOSEC
/ tdelta
;
4634 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4635 calc_default_iostats(oldvs
, newvs
, calcvs
);
4636 print_iostat_default(calcvs
, cb
, scale
);
4638 if (cb
->cb_flags
& IOS_LATENCY_M
)
4639 print_iostat_latency(cb
, oldnv
, newnv
);
4640 if (cb
->cb_flags
& IOS_QUEUES_M
)
4641 print_iostat_queues(cb
, oldnv
, newnv
);
4642 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4644 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4647 if (cb
->vcdl
!= NULL
) {
4649 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4652 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4656 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4665 if (!cb
->cb_verbose
)
4668 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4669 &newchild
, &children
) != 0)
4673 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4674 &oldchild
, &oldchildren
) != 0)
4677 children
= MIN(oldchildren
, children
);
4681 * print normal top-level devices
4683 for (c
= 0; c
< children
; c
++) {
4684 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4686 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4689 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4692 if (ishole
|| islog
)
4695 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4698 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4700 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4701 newchild
[c
], cb
, depth
+ 2);
4706 * print all other top-level devices
4708 for (uint_t n
= 0; n
< 3; n
++) {
4709 boolean_t printed
= B_FALSE
;
4711 for (c
= 0; c
< children
; c
++) {
4712 uint64_t islog
= B_FALSE
;
4716 (void) nvlist_lookup_uint64(newchild
[c
],
4717 ZPOOL_CONFIG_IS_LOG
, &islog
);
4719 bias
= VDEV_ALLOC_CLASS_LOGS
;
4721 (void) nvlist_lookup_string(newchild
[c
],
4722 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4723 (void) nvlist_lookup_string(newchild
[c
],
4724 ZPOOL_CONFIG_TYPE
, &type
);
4726 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4728 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4732 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4733 !cb
->cb_scripted
&& !cb
->cb_vdev_names
) {
4734 print_iostat_dashes(cb
, 0,
4741 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4743 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4744 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4750 * Include level 2 ARC devices in iostat output
4752 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4753 &newchild
, &children
) != 0)
4757 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4758 &oldchild
, &oldchildren
) != 0)
4761 children
= MIN(oldchildren
, children
);
4765 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4766 !cb
->cb_vdev_names
) {
4767 print_iostat_dashes(cb
, 0, "cache");
4771 for (c
= 0; c
< children
; c
++) {
4772 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4774 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4775 : NULL
, newchild
[c
], cb
, depth
+ 2);
4784 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4786 iostat_cbdata_t
*cb
= data
;
4790 * If the pool has disappeared, remove it from the list and continue.
4792 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4796 pool_list_remove(cb
->cb_list
, zhp
);
4802 * Callback to print out the iostats for the given pool.
4805 print_iostat(zpool_handle_t
*zhp
, void *data
)
4807 iostat_cbdata_t
*cb
= data
;
4808 nvlist_t
*oldconfig
, *newconfig
;
4809 nvlist_t
*oldnvroot
, *newnvroot
;
4812 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4814 if (cb
->cb_iteration
== 1)
4817 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4820 if (oldconfig
== NULL
)
4823 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4826 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4828 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4829 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4830 print_iostat_separator(cb
);
4831 if (cb
->vcdl
!= NULL
) {
4832 print_cmd_columns(cb
->vcdl
, 1);
4847 if (isatty(STDOUT_FILENO
)) {
4848 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4850 columns
= ws
.ws_col
;
4859 * Return the required length of the pool/vdev name column. The minimum
4860 * allowed width and output formatting flags must be provided.
4863 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
4865 nvlist_t
*config
, *nvroot
;
4866 int width
= min_width
;
4868 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4869 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4871 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4872 if (verbose
== B_FALSE
) {
4873 width
= MAX(poolname_len
, min_width
);
4875 width
= MAX(poolname_len
,
4876 max_width(zhp
, nvroot
, 0, min_width
, flags
));
4884 * Parse the input string, get the 'interval' and 'count' value if there is one.
4887 get_interval_count(int *argcp
, char **argv
, float *iv
,
4891 unsigned long count
= 0;
4895 * Determine if the last argument is an integer or a pool name
4897 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4901 interval
= strtof(argv
[argc
- 1], &end
);
4903 if (*end
== '\0' && errno
== 0) {
4904 if (interval
== 0) {
4905 (void) fprintf(stderr
, gettext("interval "
4906 "cannot be zero\n"));
4910 * Ignore the last parameter
4915 * If this is not a valid number, just plow on. The
4916 * user will get a more informative error message later
4924 * If the last argument is also an integer, then we have both a count
4927 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4932 interval
= strtof(argv
[argc
- 1], &end
);
4934 if (*end
== '\0' && errno
== 0) {
4935 if (interval
== 0) {
4936 (void) fprintf(stderr
, gettext("interval "
4937 "cannot be zero\n"));
4942 * Ignore the last parameter
4956 get_timestamp_arg(char c
)
4959 timestamp_fmt
= UDATE
;
4961 timestamp_fmt
= DDATE
;
4967 * Return stat flags that are supported by all pools by both the module and
4968 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
4969 * It will get ANDed down until only the flags that are supported on all pools
4973 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
4975 uint64_t *mask
= data
;
4976 nvlist_t
*config
, *nvroot
, *nvx
;
4980 config
= zpool_get_config(zhp
, NULL
);
4981 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4984 /* Default stats are always supported, but for completeness.. */
4985 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
4986 flags
|= IOS_DEFAULT_M
;
4988 /* Get our extended stats nvlist from the main list */
4989 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4992 * No extended stats; they're probably running an older
4993 * module. No big deal, we support that too.
4998 /* For each extended stat, make sure all its nvpairs are supported */
4999 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5000 if (!vsx_type_to_nvlist
[j
][0])
5003 /* Start off by assuming the flag is supported, then check */
5004 flags
|= (1ULL << j
);
5005 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5006 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5007 /* flag isn't supported */
5008 flags
= flags
& ~(1ULL << j
);
5014 *mask
= *mask
& flags
;
5019 * Return a bitmask of stats that are supported on all pools by both the module
5023 get_stat_flags(zpool_list_t
*list
)
5028 * get_stat_flags_cb() will lop off bits from "mask" until only the
5029 * flags that are supported on all pools remain.
5031 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5036 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
5039 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
5041 iostat_cbdata_t
*cb
= cb_data
;
5045 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
5047 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
5048 ret
= 1; /* match */
5055 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
5058 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5060 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5064 * Check if vdevs are in a pool
5066 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5067 * return 0. If pool_name is NULL, then search all pools.
5070 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5071 iostat_cbdata_t
*cb
)
5078 if ((argc
== 0) || !*argv
)
5084 /* Temporarily hijack cb_vdev_names for a second... */
5085 tmp_name
= cb
->cb_vdev_names
;
5087 /* Go though our list of prospective vdev names */
5088 for (i
= 0; i
< argc
; i
++) {
5089 cb
->cb_vdev_names
= argv
+ i
;
5091 /* Is this name a vdev in our pools? */
5092 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5093 B_FALSE
, is_vdev
, cb
);
5100 cb
->cb_vdev_names
= tmp_name
;
5106 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5109 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5116 * Do we have a pool named *name? If so, return 1, otherwise 0.
5121 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, B_FALSE
, is_pool_cb
,
5125 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5127 are_all_pools(int argc
, char **argv
)
5129 if ((argc
== 0) || !*argv
)
5133 if (!is_pool(argv
[argc
]))
5140 * Helper function to print out vdev/pool names we can't resolve. Used for an
5144 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5145 iostat_cbdata_t
*cb
)
5150 for (i
= 0; i
< argc
; i
++) {
5154 str
= gettext("pool");
5155 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5156 str
= gettext("vdev in this pool");
5157 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5158 str
= gettext("vdev in another pool");
5160 str
= gettext("unknown");
5162 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5167 * Same as get_interval_count(), but with additional checks to not misinterpret
5168 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5172 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5173 unsigned long *count
, iostat_cbdata_t
*cb
)
5175 char **tmpargv
= argv
;
5176 int argc_for_interval
= 0;
5178 /* Is the last arg an interval value? Or a guid? */
5179 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
5181 * The last arg is not a guid, so it's probably an
5184 argc_for_interval
++;
5187 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
5189 * The 2nd to last arg is not a guid, so it's probably
5190 * an interval value.
5192 argc_for_interval
++;
5196 /* Point to our list of possible intervals */
5197 tmpargv
= &argv
[*argc
- argc_for_interval
];
5199 *argc
= *argc
- argc_for_interval
;
5200 get_interval_count(&argc_for_interval
, tmpargv
,
5205 * Floating point sleep(). Allows you to pass in a floating point value for
5211 struct timespec req
;
5212 req
.tv_sec
= floor(sec
);
5213 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5214 nanosleep(&req
, NULL
);
5218 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5219 * if we were unable to determine its size.
5222 terminal_height(void)
5226 if (isatty(STDOUT_FILENO
) == 0)
5229 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5230 return (win
.ws_row
);
5236 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5239 * name: Short name of the script ('iostat').
5240 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5243 print_zpool_script_help(char *name
, char *path
)
5245 char *argv
[] = {path
, "-h", NULL
};
5246 char **lines
= NULL
;
5250 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5252 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5254 libzfs_free_str_array(lines
, lines_cnt
);
5258 for (int i
= 0; i
< lines_cnt
; i
++)
5259 if (!is_blank_str(lines
[i
]))
5260 printf(" %-14s %s\n", name
, lines
[i
]);
5262 libzfs_free_str_array(lines
, lines_cnt
);
5266 * Go though the zpool status/iostat -c scripts in the user's path, run their
5267 * help option (-h), and print out the results.
5270 print_zpool_dir_scripts(char *dirpath
)
5274 char fullpath
[MAXPATHLEN
];
5275 struct stat dir_stat
;
5277 if ((dir
= opendir(dirpath
)) != NULL
) {
5278 /* print all the files and directories within directory */
5279 while ((ent
= readdir(dir
)) != NULL
) {
5280 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5282 /* Print the scripts */
5283 if (stat(fullpath
, &dir_stat
) == 0)
5284 if (dir_stat
.st_mode
& S_IXUSR
&&
5285 S_ISREG(dir_stat
.st_mode
))
5286 print_zpool_script_help(ent
->d_name
,
5294 * Print out help text for all zpool status/iostat -c scripts.
5297 print_zpool_script_list(char *subcommand
)
5301 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5303 sp
= zpool_get_cmd_search_path();
5307 dir
= strtok(sp
, ":");
5308 while (dir
!= NULL
) {
5309 print_zpool_dir_scripts(dir
);
5310 dir
= strtok(NULL
, ":");
5317 * Set the minimum pool/vdev name column width. The width must be at least 10,
5318 * but may be as large as the column width - 42 so it still fits on one line.
5319 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5322 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5324 iostat_cbdata_t
*cb
= data
;
5325 int width
, available_width
;
5328 * get_namewidth() returns the maximum width of any name in that column
5329 * for any pool/vdev/device line that will be output.
5331 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
5335 * The width we are calculating is the width of the header and also the
5336 * padding width for names that are less than maximum width. The stats
5337 * take up 42 characters, so the width available for names is:
5339 available_width
= get_columns() - 42;
5342 * If the maximum width fits on a screen, then great! Make everything
5343 * line up by justifying all lines to the same width. If that max
5344 * width is larger than what's available, the name plus stats won't fit
5345 * on one line, and justifying to that width would cause every line to
5346 * wrap on the screen. We only want lines with long names to wrap.
5347 * Limit the padding to what won't wrap.
5349 if (width
> available_width
)
5350 width
= available_width
;
5353 * And regardless of whatever the screen width is (get_columns can
5354 * return 0 if the width is not known or less than 42 for a narrow
5355 * terminal) have the width be a minimum of 10.
5360 /* Save the calculated width */
5361 cb
->cb_namewidth
= width
;
5367 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5368 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5369 * [interval [count]]
5371 * -c CMD For each vdev, run command CMD
5372 * -g Display guid for individual vdev name.
5373 * -L Follow links when resolving vdev path name.
5374 * -P Display full path for vdev name.
5375 * -v Display statistics for individual vdevs
5377 * -p Display values in parsable (exact) format.
5378 * -H Scripted mode. Don't display headers, and separate properties
5380 * -l Display average latency
5381 * -q Display queue depths
5382 * -w Display latency histograms
5383 * -r Display request size histogram
5384 * -T Display a timestamp in date(1) or Unix format
5385 * -n Only print headers once
5387 * This command can be tricky because we want to be able to deal with pool
5388 * creation/destruction as well as vdev configuration changes. The bulk of this
5389 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5390 * on pool_list_update() to detect the addition of new pools. Configuration
5391 * changes are all handled within libzfs.
5394 zpool_do_iostat(int argc
, char **argv
)
5400 unsigned long count
= 0;
5403 boolean_t verbose
= B_FALSE
;
5404 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5405 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5406 boolean_t omit_since_boot
= B_FALSE
;
5407 boolean_t guid
= B_FALSE
;
5408 boolean_t follow_links
= B_FALSE
;
5409 boolean_t full_name
= B_FALSE
;
5410 boolean_t headers_once
= B_FALSE
;
5411 iostat_cbdata_t cb
= { 0 };
5414 /* Used for printing error message */
5415 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5416 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5418 uint64_t unsupported_flags
;
5421 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5426 gettext("Can't set -c flag twice\n"));
5430 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5431 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5432 fprintf(stderr
, gettext(
5433 "Can't run -c, disabled by "
5434 "ZPOOL_SCRIPTS_ENABLED.\n"));
5438 if ((getuid() <= 0 || geteuid() <= 0) &&
5439 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5440 fprintf(stderr
, gettext(
5441 "Can't run -c with root privileges "
5442 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5452 follow_links
= B_TRUE
;
5458 get_timestamp_arg(*optarg
);
5482 omit_since_boot
= B_TRUE
;
5485 headers_once
= B_TRUE
;
5491 if (optopt
== 'c') {
5492 print_zpool_script_list("iostat");
5496 gettext("invalid option '%c'\n"), optopt
);
5505 cb
.cb_literal
= parsable
;
5506 cb
.cb_scripted
= scripted
;
5509 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5511 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5513 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5514 cb
.cb_iteration
= 0;
5515 cb
.cb_namewidth
= 0;
5516 cb
.cb_verbose
= verbose
;
5518 /* Get our interval and count values (if any) */
5520 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5523 get_interval_count(&argc
, argv
, &interval
, &count
);
5527 /* No args, so just print the defaults. */
5528 } else if (are_all_pools(argc
, argv
)) {
5529 /* All the args are pool names */
5530 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
5531 /* All the args are vdevs */
5532 cb
.cb_vdev_names
= argv
;
5533 cb
.cb_vdev_names_count
= argc
;
5534 argc
= 0; /* No pools to process */
5535 } else if (are_all_pools(1, argv
)) {
5536 /* The first arg is a pool name */
5537 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
5538 /* ...and the rest are vdev names */
5539 cb
.cb_vdev_names
= argv
+ 1;
5540 cb
.cb_vdev_names_count
= argc
- 1;
5541 argc
= 1; /* One pool to process */
5543 fprintf(stderr
, gettext("Expected either a list of "));
5544 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5545 fprintf(stderr
, " \"%s\", ", argv
[0]);
5546 fprintf(stderr
, gettext("but got:\n"));
5547 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5549 fprintf(stderr
, "\n");
5555 * The args don't make sense. The first arg isn't a pool name,
5556 * nor are all the args vdevs.
5558 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5559 fprintf(stderr
, "\n");
5563 if (cb
.cb_vdev_names_count
!= 0) {
5565 * If user specified vdevs, it implies verbose.
5567 cb
.cb_verbose
= B_TRUE
;
5571 * Construct the list of all interesting pools.
5574 if ((list
= pool_list_get(argc
, argv
, NULL
, parsable
, &ret
)) == NULL
)
5577 if (pool_list_count(list
) == 0 && argc
!= 0) {
5578 pool_list_free(list
);
5582 if (pool_list_count(list
) == 0 && interval
== 0) {
5583 pool_list_free(list
);
5584 (void) fprintf(stderr
, gettext("no pools available\n"));
5588 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5589 pool_list_free(list
);
5590 (void) fprintf(stderr
,
5591 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5596 if (l_histo
&& rq_histo
) {
5597 pool_list_free(list
);
5598 (void) fprintf(stderr
,
5599 gettext("Only one of [-r|-w] can be passed at a time\n"));
5605 * Enter the main iostat loop.
5611 * Histograms tables look out of place when you try to display
5612 * them with the other stats, so make a rule that you can only
5613 * print histograms by themselves.
5615 cb
.cb_flags
= IOS_L_HISTO_M
;
5616 } else if (rq_histo
) {
5617 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5619 cb
.cb_flags
= IOS_DEFAULT_M
;
5621 cb
.cb_flags
|= IOS_LATENCY_M
;
5623 cb
.cb_flags
|= IOS_QUEUES_M
;
5627 * See if the module supports all the stats we want to display.
5629 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5630 if (unsupported_flags
) {
5634 gettext("The loaded zfs module doesn't support:"));
5636 /* for each bit set in unsupported_flags */
5637 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5638 idx
= lowbit64(f
) - 1;
5639 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5642 fprintf(stderr
, ". Try running a newer module.\n");
5643 pool_list_free(list
);
5649 if ((npools
= pool_list_count(list
)) == 0)
5650 (void) fprintf(stderr
, gettext("no pools available\n"));
5653 * If this is the first iteration and -y was supplied
5654 * we skip any printing.
5656 boolean_t skip
= (omit_since_boot
&&
5657 cb
.cb_iteration
== 0);
5660 * Refresh all statistics. This is done as an
5661 * explicit step before calculating the maximum name
5662 * width, so that any * configuration changes are
5663 * properly accounted for.
5665 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5669 * Iterate over all pools to determine the maximum width
5670 * for the pool / device name column across all pools.
5672 cb
.cb_namewidth
= 0;
5673 (void) pool_list_iter(list
, B_FALSE
,
5674 get_namewidth_iostat
, &cb
);
5676 if (timestamp_fmt
!= NODATE
)
5677 print_timestamp(timestamp_fmt
);
5679 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5680 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5681 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5682 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
5683 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
5690 * Check terminal size so we can print headers
5691 * even when terminal window has its height
5694 winheight
= terminal_height();
5696 * Are we connected to TTY? If not, headers_once
5697 * should be true, to avoid breaking scripts.
5700 headers_once
= B_TRUE
;
5703 * If it's the first time and we're not skipping it,
5704 * or either skip or verbose mode, print the header.
5706 * The histogram code explicitly prints its header on
5707 * every vdev, so skip this for histograms.
5709 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5710 (skip
!= verbose
) ||
5712 (cb
.cb_iteration
% winheight
) == 0)) &&
5713 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5715 print_iostat_header(&cb
);
5718 (void) fsleep(interval
);
5722 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5725 * If there's more than one pool, and we're not in
5726 * verbose mode (which prints a separator for us),
5727 * then print a separator.
5729 * In addition, if we're printing specific vdevs then
5730 * we also want an ending separator.
5732 if (((npools
> 1 && !verbose
&&
5733 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5734 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5735 cb
.cb_vdev_names_count
)) &&
5737 print_iostat_separator(&cb
);
5738 if (cb
.vcdl
!= NULL
)
5739 print_cmd_columns(cb
.vcdl
, 1);
5743 if (cb
.vcdl
!= NULL
)
5744 free_vdev_cmd_data_list(cb
.vcdl
);
5749 * Flush the output so that redirection to a file isn't buffered
5752 (void) fflush(stdout
);
5757 if (count
!= 0 && --count
== 0)
5760 (void) fsleep(interval
);
5763 pool_list_free(list
);
5768 typedef struct list_cbdata
{
5769 boolean_t cb_verbose
;
5772 boolean_t cb_scripted
;
5773 zprop_list_t
*cb_proplist
;
5774 boolean_t cb_literal
;
5779 * Given a list of columns to display, output appropriate headers for each one.
5782 print_header(list_cbdata_t
*cb
)
5784 zprop_list_t
*pl
= cb
->cb_proplist
;
5785 char headerbuf
[ZPOOL_MAXPROPLEN
];
5787 boolean_t first
= B_TRUE
;
5788 boolean_t right_justify
;
5791 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5792 width
= pl
->pl_width
;
5793 if (first
&& cb
->cb_verbose
) {
5795 * Reset the width to accommodate the verbose listing
5798 width
= cb
->cb_namewidth
;
5806 right_justify
= B_FALSE
;
5807 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5808 header
= zpool_prop_column_name(pl
->pl_prop
);
5809 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5813 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5814 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5815 headerbuf
[i
] = '\0';
5819 if (pl
->pl_next
== NULL
&& !right_justify
)
5820 (void) printf("%s", header
);
5821 else if (right_justify
)
5822 (void) printf("%*s", (int)width
, header
);
5824 (void) printf("%-*s", (int)width
, header
);
5827 (void) printf("\n");
5831 * Given a pool and a list of properties, print out all the properties according
5832 * to the described layout. Used by zpool_do_list().
5835 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5837 zprop_list_t
*pl
= cb
->cb_proplist
;
5838 boolean_t first
= B_TRUE
;
5839 char property
[ZPOOL_MAXPROPLEN
];
5841 boolean_t right_justify
;
5844 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5846 width
= pl
->pl_width
;
5847 if (first
&& cb
->cb_verbose
) {
5849 * Reset the width to accommodate the verbose listing
5852 width
= cb
->cb_namewidth
;
5856 if (cb
->cb_scripted
)
5857 (void) printf("\t");
5864 right_justify
= B_FALSE
;
5865 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5866 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5867 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5872 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5873 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5874 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5875 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5876 sizeof (property
)) == 0) {
5884 * If this is being called in scripted mode, or if this is the
5885 * last column and it is left-justified, don't include a width
5888 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5889 (void) printf("%s", propstr
);
5890 else if (right_justify
)
5891 (void) printf("%*s", (int)width
, propstr
);
5893 (void) printf("%-*s", (int)width
, propstr
);
5896 (void) printf("\n");
5900 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
5901 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
5905 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
5908 case ZPOOL_PROP_EXPANDSZ
:
5909 case ZPOOL_PROP_CHECKPOINT
:
5910 case ZPOOL_PROP_DEDUPRATIO
:
5912 (void) strlcpy(propval
, "-", sizeof (propval
));
5914 zfs_nicenum_format(value
, propval
, sizeof (propval
),
5917 case ZPOOL_PROP_FRAGMENTATION
:
5918 if (value
== ZFS_FRAG_INVALID
) {
5919 (void) strlcpy(propval
, "-", sizeof (propval
));
5920 } else if (format
== ZFS_NICENUM_RAW
) {
5921 (void) snprintf(propval
, sizeof (propval
), "%llu",
5922 (unsigned long long)value
);
5924 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
5925 (unsigned long long)value
);
5928 case ZPOOL_PROP_CAPACITY
:
5929 /* capacity value is in parts-per-10,000 (aka permyriad) */
5930 if (format
== ZFS_NICENUM_RAW
)
5931 (void) snprintf(propval
, sizeof (propval
), "%llu",
5932 (unsigned long long)value
/ 100);
5934 (void) snprintf(propval
, sizeof (propval
),
5935 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
5936 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
5938 case ZPOOL_PROP_HEALTH
:
5940 snprintf(propval
, sizeof (propval
), "%-*s", (int)width
, str
);
5943 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
5947 (void) strlcpy(propval
, "-", sizeof (propval
));
5950 (void) printf("\t%s", propval
);
5952 (void) printf(" %*s", (int)width
, propval
);
5956 * print static default line per vdev
5957 * not compatible with '-o' <proplist> option
5960 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
5961 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
5967 boolean_t scripted
= cb
->cb_scripted
;
5968 uint64_t islog
= B_FALSE
;
5969 char *dashes
= "%-*s - - - - "
5972 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
5973 (uint64_t **)&vs
, &c
) == 0);
5976 boolean_t toplevel
= (vs
->vs_space
!= 0);
5978 enum zfs_nicenum_format format
;
5982 format
= ZFS_NICENUM_RAW
;
5984 format
= ZFS_NICENUM_1024
;
5986 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
5990 (void) printf("\t%s", name
);
5991 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
5992 (void) printf("%*s%s", depth
, "", name
);
5994 (void) printf("%*s%s%*s", depth
, "", name
,
5995 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
5998 * Print the properties for the individual vdevs. Some
5999 * properties are only applicable to toplevel vdevs. The
6000 * 'toplevel' boolean value is passed to the print_one_column()
6001 * to indicate that the value is valid.
6003 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
6005 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6006 scripted
, toplevel
, format
);
6007 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6008 NULL
, scripted
, toplevel
, format
);
6009 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6010 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6011 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6012 scripted
, B_TRUE
, format
);
6013 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6014 vs
->vs_fragmentation
, NULL
, scripted
,
6015 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6017 cap
= (vs
->vs_space
== 0) ? 0 :
6018 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6019 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6020 scripted
, toplevel
, format
);
6021 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6022 scripted
, toplevel
, format
);
6023 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6025 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6027 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6030 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6032 (void) printf("\n");
6035 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6036 &child
, &children
) != 0)
6039 /* list the normal vdevs first */
6040 for (c
= 0; c
< children
; c
++) {
6041 uint64_t ishole
= B_FALSE
;
6043 if (nvlist_lookup_uint64(child
[c
],
6044 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6047 if (nvlist_lookup_uint64(child
[c
],
6048 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6051 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6054 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6056 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6060 /* list the classes: 'logs', 'dedup', and 'special' */
6061 for (uint_t n
= 0; n
< 3; n
++) {
6062 boolean_t printed
= B_FALSE
;
6064 for (c
= 0; c
< children
; c
++) {
6068 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6069 &islog
) == 0 && islog
) {
6070 bias
= VDEV_ALLOC_CLASS_LOGS
;
6072 (void) nvlist_lookup_string(child
[c
],
6073 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6074 (void) nvlist_lookup_string(child
[c
],
6075 ZPOOL_CONFIG_TYPE
, &type
);
6077 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6079 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6083 /* LINTED E_SEC_PRINTF_VAR_FMT */
6084 (void) printf(dashes
, cb
->cb_namewidth
,
6088 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6090 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6096 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6097 &child
, &children
) == 0 && children
> 0) {
6098 /* LINTED E_SEC_PRINTF_VAR_FMT */
6099 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6100 for (c
= 0; c
< children
; c
++) {
6101 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6103 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6109 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6110 &children
) == 0 && children
> 0) {
6111 /* LINTED E_SEC_PRINTF_VAR_FMT */
6112 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6113 for (c
= 0; c
< children
; c
++) {
6114 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6116 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6124 * Generic callback function to list a pool.
6127 list_callback(zpool_handle_t
*zhp
, void *data
)
6129 list_cbdata_t
*cbp
= data
;
6131 print_pool(zhp
, cbp
);
6133 if (cbp
->cb_verbose
) {
6134 nvlist_t
*config
, *nvroot
;
6136 config
= zpool_get_config(zhp
, NULL
);
6137 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6139 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6146 * Set the minimum pool/vdev name column width. The width must be at least 9,
6147 * but may be as large as needed.
6150 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6152 list_cbdata_t
*cb
= data
;
6155 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6161 cb
->cb_namewidth
= width
;
6167 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6169 * -g Display guid for individual vdev name.
6170 * -H Scripted mode. Don't display headers, and separate properties
6172 * -L Follow links when resolving vdev path name.
6173 * -o List of properties to display. Defaults to
6174 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6175 * "dedupratio,health,altroot"
6176 * -p Display values in parsable (exact) format.
6177 * -P Display full path for vdev name.
6178 * -T Display a timestamp in date(1) or Unix format
6180 * List all pools in the system, whether or not they're healthy. Output space
6181 * statistics for each one, as well as health status summary.
6184 zpool_do_list(int argc
, char **argv
)
6188 list_cbdata_t cb
= { 0 };
6189 static char default_props
[] =
6190 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6191 "capacity,dedupratio,health,altroot";
6192 char *props
= default_props
;
6194 unsigned long count
= 0;
6196 boolean_t first
= B_TRUE
;
6199 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6202 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6205 cb
.cb_scripted
= B_TRUE
;
6208 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6214 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6217 cb
.cb_literal
= B_TRUE
;
6220 get_timestamp_arg(*optarg
);
6223 cb
.cb_verbose
= B_TRUE
;
6224 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6227 (void) fprintf(stderr
, gettext("missing argument for "
6228 "'%c' option\n"), optopt
);
6232 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6241 get_interval_count(&argc
, argv
, &interval
, &count
);
6243 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6247 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6248 cb
.cb_literal
, &ret
)) == NULL
)
6251 if (pool_list_count(list
) == 0)
6254 cb
.cb_namewidth
= 0;
6255 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6257 if (timestamp_fmt
!= NODATE
)
6258 print_timestamp(timestamp_fmt
);
6260 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6264 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6269 if (count
!= 0 && --count
== 0)
6272 pool_list_free(list
);
6273 (void) fsleep(interval
);
6276 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6277 (void) printf(gettext("no pools available\n"));
6281 pool_list_free(list
);
6282 zprop_free_list(cb
.cb_proplist
);
6287 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6289 boolean_t force
= B_FALSE
;
6290 boolean_t rebuild
= B_FALSE
;
6291 boolean_t wait
= B_FALSE
;
6294 char *poolname
, *old_disk
, *new_disk
;
6295 zpool_handle_t
*zhp
;
6296 nvlist_t
*props
= NULL
;
6301 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6307 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6308 (void) fprintf(stderr
, gettext("missing "
6309 "'=' for -o option\n"));
6315 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6316 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6326 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6335 /* get pool name and check number of arguments */
6337 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6344 (void) fprintf(stderr
,
6345 gettext("missing <device> specification\n"));
6353 (void) fprintf(stderr
,
6354 gettext("missing <new_device> specification\n"));
6357 new_disk
= old_disk
;
6367 (void) fprintf(stderr
, gettext("too many arguments\n"));
6371 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6376 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6377 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6384 /* unless manually specified use "ashift" pool property (if set) */
6385 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6388 char strval
[ZPOOL_MAXPROPLEN
];
6390 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6391 if (src
!= ZPROP_SRC_DEFAULT
) {
6392 (void) sprintf(strval
, "%" PRId32
, intval
);
6393 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6394 &props
, B_TRUE
) == 0);
6398 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6400 if (nvroot
== NULL
) {
6406 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6409 if (ret
== 0 && wait
)
6410 ret
= zpool_wait(zhp
,
6411 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6414 nvlist_free(nvroot
);
6421 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6423 * -f Force attach, even if <new_device> appears to be in use.
6424 * -s Use sequential instead of healing reconstruction for resilver.
6425 * -o Set property=value.
6426 * -w Wait for replacing to complete before returning
6428 * Replace <device> with <new_device>.
6432 zpool_do_replace(int argc
, char **argv
)
6434 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6438 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6440 * -f Force attach, even if <new_device> appears to be in use.
6441 * -s Use sequential instead of healing reconstruction for resilver.
6442 * -o Set property=value.
6443 * -w Wait for resilvering to complete before returning
6445 * Attach <new_device> to the mirror containing <device>. If <device> is not
6446 * part of a mirror, then <device> will be transformed into a mirror of
6447 * <device> and <new_device>. In either case, <new_device> will begin life
6448 * with a DTL of [0, now], and will immediately begin to resilver itself.
6451 zpool_do_attach(int argc
, char **argv
)
6453 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6457 * zpool detach [-f] <pool> <device>
6459 * -f Force detach of <device>, even if DTLs argue against it
6460 * (not supported yet)
6462 * Detach a device from a mirror. The operation will be refused if <device>
6463 * is the last device in the mirror, or if the DTLs indicate that this device
6464 * has the only valid copy of some data.
6468 zpool_do_detach(int argc
, char **argv
)
6471 char *poolname
, *path
;
6472 zpool_handle_t
*zhp
;
6476 while ((c
= getopt(argc
, argv
, "")) != -1) {
6479 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6488 /* get pool name and check number of arguments */
6490 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6495 (void) fprintf(stderr
,
6496 gettext("missing <device> specification\n"));
6503 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6506 ret
= zpool_vdev_detach(zhp
, path
);
6514 * zpool split [-gLnP] [-o prop=val] ...
6516 * [-R altroot] <pool> <newpool> [<device> ...]
6518 * -g Display guid for individual vdev name.
6519 * -L Follow links when resolving vdev path name.
6520 * -n Do not split the pool, but display the resulting layout if
6521 * it were to be split.
6522 * -o Set property=value, or set mount options.
6523 * -P Display full path for vdev name.
6524 * -R Mount the split-off pool under an alternate root.
6525 * -l Load encryption keys while importing.
6527 * Splits the named pool and gives it the new pool name. Devices to be split
6528 * off may be listed, provided that no more than one device is specified
6529 * per top-level vdev mirror. The newly split pool is left in an exported
6530 * state unless -R is specified.
6532 * Restrictions: the top-level of the pool pool must only be made up of
6533 * mirrors; all devices in the pool must be healthy; no device may be
6534 * undergoing a resilvering operation.
6537 zpool_do_split(int argc
, char **argv
)
6539 char *srcpool
, *newpool
, *propval
;
6540 char *mntopts
= NULL
;
6543 boolean_t loadkeys
= B_FALSE
;
6544 zpool_handle_t
*zhp
;
6545 nvlist_t
*config
, *props
= NULL
;
6547 flags
.dryrun
= B_FALSE
;
6548 flags
.import
= B_FALSE
;
6549 flags
.name_flags
= 0;
6552 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6555 flags
.name_flags
|= VDEV_NAME_GUID
;
6558 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6561 flags
.import
= B_TRUE
;
6563 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6564 &props
, B_TRUE
) != 0) {
6573 flags
.dryrun
= B_TRUE
;
6576 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6579 if (add_prop_list(optarg
, propval
,
6580 &props
, B_TRUE
) != 0) {
6589 flags
.name_flags
|= VDEV_NAME_PATH
;
6592 (void) fprintf(stderr
, gettext("missing argument for "
6593 "'%c' option\n"), optopt
);
6597 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6604 if (!flags
.import
&& mntopts
!= NULL
) {
6605 (void) fprintf(stderr
, gettext("setting mntopts is only "
6606 "valid when importing the pool\n"));
6610 if (!flags
.import
&& loadkeys
) {
6611 (void) fprintf(stderr
, gettext("loading keys is only "
6612 "valid when importing the pool\n"));
6620 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6624 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6634 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6639 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6640 if (config
== NULL
) {
6644 (void) printf(gettext("would create '%s' with the "
6645 "following layout:\n\n"), newpool
);
6646 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6648 print_vdev_tree(NULL
, "dedup", config
, 0,
6649 VDEV_ALLOC_BIAS_DEDUP
, 0);
6650 print_vdev_tree(NULL
, "special", config
, 0,
6651 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6657 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6658 nvlist_free(config
);
6664 * The split was successful. Now we need to open the new
6665 * pool and import it.
6667 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6668 nvlist_free(config
);
6674 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6679 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6680 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6682 (void) fprintf(stderr
, gettext("Split was successful, but "
6683 "the datasets could not all be mounted\n"));
6684 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6685 "different altroot\n"), "zpool import");
6688 nvlist_free(config
);
6697 * zpool online <pool> <device> ...
6700 zpool_do_online(int argc
, char **argv
)
6704 zpool_handle_t
*zhp
;
6706 vdev_state_t newstate
;
6710 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6713 flags
|= ZFS_ONLINE_EXPAND
;
6716 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6725 /* get pool name and check number of arguments */
6727 (void) fprintf(stderr
, gettext("missing pool name\n"));
6731 (void) fprintf(stderr
, gettext("missing device name\n"));
6737 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6740 for (i
= 1; i
< argc
; i
++) {
6741 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6742 if (newstate
!= VDEV_STATE_HEALTHY
) {
6743 (void) printf(gettext("warning: device '%s' "
6744 "onlined, but remains in faulted state\n"),
6746 if (newstate
== VDEV_STATE_FAULTED
)
6747 (void) printf(gettext("use 'zpool "
6748 "clear' to restore a faulted "
6751 (void) printf(gettext("use 'zpool "
6752 "replace' to replace devices "
6753 "that are no longer present\n"));
6766 * zpool offline [-ft] <pool> <device> ...
6768 * -f Force the device into a faulted state.
6770 * -t Only take the device off-line temporarily. The offline/faulted
6771 * state will not be persistent across reboots.
6775 zpool_do_offline(int argc
, char **argv
)
6779 zpool_handle_t
*zhp
;
6781 boolean_t istmp
= B_FALSE
;
6782 boolean_t fault
= B_FALSE
;
6785 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6794 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6803 /* get pool name and check number of arguments */
6805 (void) fprintf(stderr
, gettext("missing pool name\n"));
6809 (void) fprintf(stderr
, gettext("missing device name\n"));
6815 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6818 for (i
= 1; i
< argc
; i
++) {
6820 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6822 if (istmp
== B_FALSE
) {
6823 /* Force the fault to persist across imports */
6824 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6826 aux
= VDEV_AUX_EXTERNAL
;
6829 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6832 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
6843 * zpool clear <pool> [device]
6845 * Clear all errors associated with a pool or a particular device.
6848 zpool_do_clear(int argc
, char **argv
)
6852 boolean_t dryrun
= B_FALSE
;
6853 boolean_t do_rewind
= B_FALSE
;
6854 boolean_t xtreme_rewind
= B_FALSE
;
6855 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
6856 nvlist_t
*policy
= NULL
;
6857 zpool_handle_t
*zhp
;
6858 char *pool
, *device
;
6861 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
6870 xtreme_rewind
= B_TRUE
;
6873 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6883 (void) fprintf(stderr
, gettext("missing pool name\n"));
6888 (void) fprintf(stderr
, gettext("too many arguments\n"));
6892 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
6893 (void) fprintf(stderr
,
6894 gettext("-n or -X only meaningful with -F\n"));
6898 rewind_policy
= ZPOOL_TRY_REWIND
;
6900 rewind_policy
= ZPOOL_DO_REWIND
;
6902 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
6904 /* In future, further rewind policy choices can be passed along here */
6905 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
6906 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
6907 rewind_policy
) != 0) {
6912 device
= argc
== 2 ? argv
[1] : NULL
;
6914 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
6915 nvlist_free(policy
);
6919 if (zpool_clear(zhp
, device
, policy
) != 0)
6924 nvlist_free(policy
);
6930 * zpool reguid <pool>
6933 zpool_do_reguid(int argc
, char **argv
)
6937 zpool_handle_t
*zhp
;
6941 while ((c
= getopt(argc
, argv
, "")) != -1) {
6944 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6953 /* get pool name and check number of arguments */
6955 (void) fprintf(stderr
, gettext("missing pool name\n"));
6960 (void) fprintf(stderr
, gettext("too many arguments\n"));
6965 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6968 ret
= zpool_reguid(zhp
);
6976 * zpool reopen <pool>
6978 * Reopen the pool so that the kernel can update the sizes of all vdevs.
6981 zpool_do_reopen(int argc
, char **argv
)
6985 boolean_t scrub_restart
= B_TRUE
;
6988 while ((c
= getopt(argc
, argv
, "n")) != -1) {
6991 scrub_restart
= B_FALSE
;
6994 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7003 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7004 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
, zpool_reopen_one
,
7010 typedef struct scrub_cbdata
{
7012 pool_scrub_cmd_t cb_scrub_cmd
;
7016 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7018 nvlist_t
*config
, *nvroot
;
7020 config
= zpool_get_config(zhp
, NULL
);
7022 if (config
!= NULL
) {
7023 pool_checkpoint_stat_t
*pcs
= NULL
;
7026 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7027 (void) nvlist_lookup_uint64_array(nvroot
,
7028 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7030 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7033 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7034 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7042 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7044 scrub_cbdata_t
*cb
= data
;
7048 * Ignore faulted pools.
7050 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7051 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7052 "currently unavailable\n"), zpool_get_name(zhp
));
7056 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7058 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7059 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7060 (void) printf(gettext("warning: will not scrub state that "
7061 "belongs to the checkpoint of pool '%s'\n"),
7062 zpool_get_name(zhp
));
7069 wait_callback(zpool_handle_t
*zhp
, void *data
)
7071 zpool_wait_activity_t
*act
= data
;
7072 return (zpool_wait(zhp
, *act
));
7076 * zpool scrub [-s | -p] [-w] <pool> ...
7078 * -s Stop. Stops any in-progress scrub.
7079 * -p Pause. Pause in-progress scrub.
7080 * -w Wait. Blocks until scrub has completed.
7083 zpool_do_scrub(int argc
, char **argv
)
7087 boolean_t wait
= B_FALSE
;
7090 cb
.cb_type
= POOL_SCAN_SCRUB
;
7091 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7094 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7097 cb
.cb_type
= POOL_SCAN_NONE
;
7100 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7106 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7112 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7113 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7114 (void) fprintf(stderr
, gettext("invalid option combination: "
7115 "-s and -p are mutually exclusive\n"));
7119 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7120 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7121 (void) fprintf(stderr
, gettext("invalid option combination: "
7122 "-w cannot be used with -p or -s\n"));
7130 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7134 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7135 scrub_callback
, &cb
);
7137 if (wait
&& !error
) {
7138 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7139 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7140 wait_callback
, &act
);
7147 * zpool resilver <pool> ...
7149 * Restarts any in-progress resilver
7152 zpool_do_resilver(int argc
, char **argv
)
7157 cb
.cb_type
= POOL_SCAN_RESILVER
;
7158 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7161 while ((c
= getopt(argc
, argv
, "")) != -1) {
7164 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7174 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7178 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7179 scrub_callback
, &cb
));
7183 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7185 * -c Cancel. Ends any in-progress trim.
7186 * -d Secure trim. Requires kernel and device support.
7187 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7188 * adding a multiplier suffix such as 'k' or 'm'.
7189 * -s Suspend. TRIM can then be restarted with no flags.
7190 * -w Wait. Blocks until trimming has completed.
7193 zpool_do_trim(int argc
, char **argv
)
7195 struct option long_options
[] = {
7196 {"cancel", no_argument
, NULL
, 'c'},
7197 {"secure", no_argument
, NULL
, 'd'},
7198 {"rate", required_argument
, NULL
, 'r'},
7199 {"suspend", no_argument
, NULL
, 's'},
7200 {"wait", no_argument
, NULL
, 'w'},
7204 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7206 boolean_t secure
= B_FALSE
;
7207 boolean_t wait
= B_FALSE
;
7210 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7214 if (cmd_type
!= POOL_TRIM_START
&&
7215 cmd_type
!= POOL_TRIM_CANCEL
) {
7216 (void) fprintf(stderr
, gettext("-c cannot be "
7217 "combined with other options\n"));
7220 cmd_type
= POOL_TRIM_CANCEL
;
7223 if (cmd_type
!= POOL_TRIM_START
) {
7224 (void) fprintf(stderr
, gettext("-d cannot be "
7225 "combined with the -c or -s options\n"));
7231 if (cmd_type
!= POOL_TRIM_START
) {
7232 (void) fprintf(stderr
, gettext("-r cannot be "
7233 "combined with the -c or -s options\n"));
7236 if (zfs_nicestrtonum(NULL
, optarg
, &rate
) == -1) {
7237 (void) fprintf(stderr
,
7238 gettext("invalid value for rate\n"));
7243 if (cmd_type
!= POOL_TRIM_START
&&
7244 cmd_type
!= POOL_TRIM_SUSPEND
) {
7245 (void) fprintf(stderr
, gettext("-s cannot be "
7246 "combined with other options\n"));
7249 cmd_type
= POOL_TRIM_SUSPEND
;
7256 (void) fprintf(stderr
,
7257 gettext("invalid option '%c'\n"), optopt
);
7259 (void) fprintf(stderr
,
7260 gettext("invalid option '%s'\n"),
7271 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7276 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7277 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7282 char *poolname
= argv
[0];
7283 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7287 trimflags_t trim_flags
= {
7293 nvlist_t
*vdevs
= fnvlist_alloc();
7295 /* no individual leaf vdevs specified, so add them all */
7296 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7297 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7298 ZPOOL_CONFIG_VDEV_TREE
);
7299 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7300 trim_flags
.fullpool
= B_TRUE
;
7302 trim_flags
.fullpool
= B_FALSE
;
7303 for (int i
= 1; i
< argc
; i
++) {
7304 fnvlist_add_boolean(vdevs
, argv
[i
]);
7308 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7310 fnvlist_free(vdevs
);
7317 * Converts a total number of seconds to a human readable string broken
7318 * down in to days/hours/minutes/seconds.
7321 secs_to_dhms(uint64_t total
, char *buf
)
7323 uint64_t days
= total
/ 60 / 60 / 24;
7324 uint64_t hours
= (total
/ 60 / 60) % 24;
7325 uint64_t mins
= (total
/ 60) % 60;
7326 uint64_t secs
= (total
% 60);
7329 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7330 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7331 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7333 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7334 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7335 (u_longlong_t
)secs
);
7340 * Print out detailed scrub status.
7343 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7345 time_t start
, end
, pause
;
7346 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7347 uint64_t elapsed
, scan_rate
, issue_rate
;
7348 double fraction_done
;
7349 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7350 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7353 printf_color(ANSI_BOLD
, gettext("scan:"));
7356 /* If there's never been a scan, there's not much to say. */
7357 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7358 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7359 (void) printf(gettext("none requested\n"));
7363 start
= ps
->pss_start_time
;
7364 end
= ps
->pss_end_time
;
7365 pause
= ps
->pss_pass_scrub_pause
;
7367 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7369 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7370 ps
->pss_func
== POOL_SCAN_RESILVER
);
7372 /* Scan is finished or canceled. */
7373 if (ps
->pss_state
== DSS_FINISHED
) {
7374 secs_to_dhms(end
- start
, time_buf
);
7376 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7377 (void) printf(gettext("scrub repaired %s "
7378 "in %s with %llu errors on %s"), processed_buf
,
7379 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7381 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7382 (void) printf(gettext("resilvered %s "
7383 "in %s with %llu errors on %s"), processed_buf
,
7384 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7388 } else if (ps
->pss_state
== DSS_CANCELED
) {
7389 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7390 (void) printf(gettext("scrub canceled on %s"),
7392 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7393 (void) printf(gettext("resilver canceled on %s"),
7399 assert(ps
->pss_state
== DSS_SCANNING
);
7401 /* Scan is in progress. Resilvers can't be paused. */
7402 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7404 (void) printf(gettext("scrub in progress since %s"),
7407 (void) printf(gettext("scrub paused since %s"),
7409 (void) printf(gettext("\tscrub started on %s"),
7412 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7413 (void) printf(gettext("resilver in progress since %s"),
7417 scanned
= ps
->pss_examined
;
7418 pass_scanned
= ps
->pss_pass_exam
;
7419 issued
= ps
->pss_issued
;
7420 pass_issued
= ps
->pss_pass_issued
;
7421 total
= ps
->pss_to_examine
;
7423 /* we are only done with a block once we have issued the IO for it */
7424 fraction_done
= (double)issued
/ total
;
7426 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7427 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7428 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7429 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7431 scan_rate
= pass_scanned
/ elapsed
;
7432 issue_rate
= pass_issued
/ elapsed
;
7433 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7434 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7435 secs_to_dhms(total_secs_left
, time_buf
);
7437 /* format all of the numbers we will be reporting */
7438 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7439 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7440 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7441 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7442 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7444 /* do not print estimated time if we have a paused scrub */
7446 (void) printf(gettext("\t%s scanned at %s/s, "
7447 "%s issued at %s/s, %s total\n"),
7448 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7450 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7451 scanned_buf
, issued_buf
, total_buf
);
7454 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7455 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7456 processed_buf
, 100 * fraction_done
);
7457 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7458 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7459 processed_buf
, 100 * fraction_done
);
7463 if (total_secs_left
!= UINT64_MAX
&&
7464 issue_rate
>= 10 * 1024 * 1024) {
7465 (void) printf(gettext(", %s to go\n"), time_buf
);
7467 (void) printf(gettext(", no estimated "
7468 "completion time\n"));
7471 (void) printf(gettext("\n"));
7476 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7478 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7482 printf_color(ANSI_BOLD
, gettext("scan:"));
7485 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7486 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7487 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7488 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7489 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7490 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7491 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7492 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7493 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7494 (bytes_est
+ 1), 100);
7496 /* Format all of the numbers we will be reporting */
7497 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7498 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7499 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7500 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7501 sizeof (bytes_scanned_buf
));
7502 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7503 sizeof (bytes_issued_buf
));
7504 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7505 sizeof (bytes_rebuilt_buf
));
7506 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7507 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7508 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7510 time_t start
= vrs
->vrs_start_time
;
7511 time_t end
= vrs
->vrs_end_time
;
7513 /* Rebuild is finished or canceled. */
7514 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7515 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7516 (void) printf(gettext("resilvered (%s) %s in %s "
7517 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7518 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7520 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7521 (void) printf(gettext("resilver (%s) canceled on %s"),
7522 vdev_name
, ctime(&end
));
7524 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7525 (void) printf(gettext("resilver (%s) in progress since %s"),
7526 vdev_name
, ctime(&start
));
7529 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7531 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7532 MAX(scan_rate
, 1), time_buf
);
7534 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7535 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7536 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7537 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7538 bytes_rebuilt_buf
, scan_pct
);
7540 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7541 if (scan_rate
>= 10 * 1024 * 1024) {
7542 (void) printf(gettext(", %s to go\n"), time_buf
);
7544 (void) printf(gettext(", no estimated "
7545 "completion time\n"));
7548 (void) printf(gettext("\n"));
7553 * Print rebuild status for top-level vdevs.
7556 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7561 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7562 &child
, &children
) != 0)
7565 for (uint_t c
= 0; c
< children
; c
++) {
7566 vdev_rebuild_stat_t
*vrs
;
7569 if (nvlist_lookup_uint64_array(child
[c
],
7570 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7571 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7572 child
[c
], VDEV_NAME_TYPE_ID
);
7573 print_rebuild_status_impl(vrs
, name
);
7580 * As we don't scrub checkpointed blocks, we want to warn the user that we
7581 * skipped scanning some blocks if a checkpoint exists or existed at any
7582 * time during the scan. If a sequential instead of healing reconstruction
7583 * was performed then the blocks were reconstructed. However, their checksums
7584 * have not been verified so we still print the warning.
7587 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7589 if (ps
== NULL
|| pcs
== NULL
)
7592 if (pcs
->pcs_state
== CS_NONE
||
7593 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7596 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7598 if (ps
->pss_state
== DSS_NONE
)
7601 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7602 ps
->pss_end_time
< pcs
->pcs_start_time
)
7605 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7606 (void) printf(gettext(" scan warning: skipped blocks "
7607 "that are only referenced by the checkpoint.\n"));
7609 assert(ps
->pss_state
== DSS_SCANNING
);
7610 (void) printf(gettext(" scan warning: skipping blocks "
7611 "that are only referenced by the checkpoint.\n"));
7616 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7617 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7618 * the last completed (or cancelled) rebuild.
7621 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7625 boolean_t rebuilding
= B_FALSE
;
7626 uint64_t end_time
= 0;
7628 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7629 &child
, &children
) != 0)
7632 for (uint_t c
= 0; c
< children
; c
++) {
7633 vdev_rebuild_stat_t
*vrs
;
7636 if (nvlist_lookup_uint64_array(child
[c
],
7637 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7639 if (vrs
->vrs_end_time
> end_time
)
7640 end_time
= vrs
->vrs_end_time
;
7642 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7643 rebuilding
= B_TRUE
;
7650 if (rebuild_end_time
!= NULL
)
7651 *rebuild_end_time
= end_time
;
7653 return (rebuilding
);
7657 * Print the scan status.
7660 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7662 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7663 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7664 boolean_t active_resilver
= B_FALSE
;
7665 pool_checkpoint_stat_t
*pcs
= NULL
;
7666 pool_scan_stat_t
*ps
= NULL
;
7669 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7670 (uint64_t **)&ps
, &c
) == 0) {
7671 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7672 resilver_end_time
= ps
->pss_end_time
;
7673 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7676 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7677 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7680 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7681 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7683 /* Always print the scrub status when available. */
7685 print_scan_scrub_resilver_status(ps
);
7688 * When there is an active resilver or rebuild print its status.
7689 * Otherwise print the status of the last resilver or rebuild.
7691 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7692 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7693 print_scan_scrub_resilver_status(ps
);
7694 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7695 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7696 print_rebuild_status(zhp
, nvroot
);
7699 (void) nvlist_lookup_uint64_array(nvroot
,
7700 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7701 print_checkpoint_scan_warning(ps
, pcs
);
7705 * Print out detailed removal status.
7708 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7710 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7712 nvlist_t
*config
, *nvroot
;
7717 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7721 * Determine name of vdev.
7723 config
= zpool_get_config(zhp
, NULL
);
7724 nvroot
= fnvlist_lookup_nvlist(config
,
7725 ZPOOL_CONFIG_VDEV_TREE
);
7726 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7727 &child
, &children
) == 0);
7728 assert(prs
->prs_removing_vdev
< children
);
7729 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7730 child
[prs
->prs_removing_vdev
], B_TRUE
);
7732 printf_color(ANSI_BOLD
, gettext("remove: "));
7734 start
= prs
->prs_start_time
;
7735 end
= prs
->prs_end_time
;
7736 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7739 * Removal is finished or canceled.
7741 if (prs
->prs_state
== DSS_FINISHED
) {
7742 uint64_t minutes_taken
= (end
- start
) / 60;
7744 (void) printf(gettext("Removal of vdev %llu copied %s "
7745 "in %lluh%um, completed on %s"),
7746 (longlong_t
)prs
->prs_removing_vdev
,
7748 (u_longlong_t
)(minutes_taken
/ 60),
7749 (uint_t
)(minutes_taken
% 60),
7750 ctime((time_t *)&end
));
7751 } else if (prs
->prs_state
== DSS_CANCELED
) {
7752 (void) printf(gettext("Removal of %s canceled on %s"),
7753 vdev_name
, ctime(&end
));
7755 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7756 double fraction_done
;
7759 assert(prs
->prs_state
== DSS_SCANNING
);
7762 * Removal is in progress.
7764 (void) printf(gettext(
7765 "Evacuation of %s in progress since %s"),
7766 vdev_name
, ctime(&start
));
7768 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7769 total
= prs
->prs_to_copy
;
7770 fraction_done
= (double)copied
/ total
;
7772 /* elapsed time for this pass */
7773 elapsed
= time(NULL
) - prs
->prs_start_time
;
7774 elapsed
= elapsed
> 0 ? elapsed
: 1;
7775 rate
= copied
/ elapsed
;
7776 rate
= rate
> 0 ? rate
: 1;
7777 mins_left
= ((total
- copied
) / rate
) / 60;
7778 hours_left
= mins_left
/ 60;
7780 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7781 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7782 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7785 * do not print estimated time if hours_left is more than
7788 (void) printf(gettext(" %s copied out of %s at %s/s, "
7790 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7791 if (hours_left
< (30 * 24)) {
7792 (void) printf(gettext(", %lluh%um to go\n"),
7793 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7795 (void) printf(gettext(
7796 ", (copy is slow, no estimated time)\n"));
7801 if (prs
->prs_mapping_memory
> 0) {
7803 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7804 (void) printf(gettext(" %s memory used for "
7805 "removed device mappings\n"),
7811 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7816 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7819 (void) printf(gettext("checkpoint: "));
7821 start
= pcs
->pcs_start_time
;
7822 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7824 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7825 char *date
= ctime(&start
);
7828 * ctime() adds a newline at the end of the generated
7829 * string, thus the weird format specifier and the
7830 * strlen() call used to chop it off from the output.
7832 (void) printf(gettext("created %.*s, consumes %s\n"),
7833 (int)(strlen(date
) - 1), date
, space_buf
);
7837 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7839 (void) printf(gettext("discarding, %s remaining.\n"),
7844 print_error_log(zpool_handle_t
*zhp
)
7846 nvlist_t
*nverrlist
= NULL
;
7849 size_t len
= MAXPATHLEN
* 2;
7851 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
7854 (void) printf("errors: Permanent errors have been "
7855 "detected in the following files:\n\n");
7857 pathname
= safe_malloc(len
);
7859 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
7861 uint64_t dsobj
, obj
;
7863 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
7864 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
7866 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
7868 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
7869 (void) printf("%7s %s\n", "", pathname
);
7872 nvlist_free(nverrlist
);
7876 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
7885 (void) printf(gettext("\tspares\n"));
7887 for (i
= 0; i
< nspares
; i
++) {
7888 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
7890 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
7896 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
7905 (void) printf(gettext("\tcache\n"));
7907 for (i
= 0; i
< nl2cache
; i
++) {
7908 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
7910 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
7917 print_dedup_stats(nvlist_t
*config
)
7919 ddt_histogram_t
*ddh
;
7923 char dspace
[6], mspace
[6];
7926 * If the pool was faulted then we may not have been able to
7927 * obtain the config. Otherwise, if we have anything in the dedup
7928 * table continue processing the stats.
7930 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
7931 (uint64_t **)&ddo
, &c
) != 0)
7934 (void) printf("\n");
7935 (void) printf(gettext(" dedup: "));
7936 if (ddo
->ddo_count
== 0) {
7937 (void) printf(gettext("no DDT entries\n"));
7941 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
7942 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
7943 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
7944 (u_longlong_t
)ddo
->ddo_count
,
7948 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
7949 (uint64_t **)&dds
, &c
) == 0);
7950 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
7951 (uint64_t **)&ddh
, &c
) == 0);
7952 zpool_dump_ddt(dds
, ddh
);
7956 * Display a summary of pool status. Displays a summary such as:
7960 * reason: One or more devices ...
7961 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
7967 * When given the '-v' option, we print out the complete config. If the '-e'
7968 * option is specified, then we print out error rate information as well.
7971 status_callback(zpool_handle_t
*zhp
, void *data
)
7973 status_cbdata_t
*cbp
= data
;
7974 nvlist_t
*config
, *nvroot
;
7976 zpool_status_t reason
;
7977 zpool_errata_t errata
;
7982 config
= zpool_get_config(zhp
, NULL
);
7983 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
7988 * If we were given 'zpool status -x', only report those pools with
7991 if (cbp
->cb_explain
&&
7992 (reason
== ZPOOL_STATUS_OK
||
7993 reason
== ZPOOL_STATUS_VERSION_OLDER
||
7994 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
7995 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
)) {
7996 if (!cbp
->cb_allpools
) {
7997 (void) printf(gettext("pool '%s' is healthy\n"),
7998 zpool_get_name(zhp
));
8000 cbp
->cb_first
= B_FALSE
;
8006 cbp
->cb_first
= B_FALSE
;
8008 (void) printf("\n");
8010 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8011 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8012 (uint64_t **)&vs
, &c
) == 0);
8014 health
= zpool_get_state_str(zhp
);
8017 printf_color(ANSI_BOLD
, gettext("pool:"));
8018 printf(" %s\n", zpool_get_name(zhp
));
8020 printf_color(ANSI_BOLD
, gettext("state: "));
8022 printf_color(health_str_to_color(health
), "%s", health
);
8027 case ZPOOL_STATUS_MISSING_DEV_R
:
8028 printf_color(ANSI_BOLD
, gettext("status: "));
8029 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8030 "not be opened. Sufficient replicas exist for\n\tthe pool "
8031 "to continue functioning in a degraded state.\n"));
8032 printf_color(ANSI_BOLD
, gettext("action: "));
8033 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8034 "and online it using 'zpool online'.\n"));
8037 case ZPOOL_STATUS_MISSING_DEV_NR
:
8038 printf_color(ANSI_BOLD
, gettext("status: "));
8039 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8040 "not be opened. There are insufficient\n\treplicas for the"
8041 " pool to continue functioning.\n"));
8042 printf_color(ANSI_BOLD
, gettext("action: "));
8043 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8044 "and online it using 'zpool online'.\n"));
8047 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8048 printf_color(ANSI_BOLD
, gettext("status: "));
8049 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8050 "not be used because the label is missing or\n\tinvalid. "
8051 "Sufficient replicas exist for the pool to continue\n\t"
8052 "functioning in a degraded state.\n"));
8053 printf_color(ANSI_BOLD
, gettext("action: "));
8054 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8055 "'zpool replace'.\n"));
8058 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8059 printf_color(ANSI_BOLD
, gettext("status: "));
8060 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8061 "not be used because the label is missing \n\tor invalid. "
8062 "There are insufficient replicas for the pool to "
8063 "continue\n\tfunctioning.\n"));
8064 zpool_explain_recover(zpool_get_handle(zhp
),
8065 zpool_get_name(zhp
), reason
, config
);
8068 case ZPOOL_STATUS_FAILING_DEV
:
8069 printf_color(ANSI_BOLD
, gettext("status: "));
8070 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8071 "experienced an unrecoverable error. An\n\tattempt was "
8072 "made to correct the error. Applications are "
8074 printf_color(ANSI_BOLD
, gettext("action: "));
8075 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8076 "device needs to be replaced, and clear the errors\n\tusing"
8077 " 'zpool clear' or replace the device with 'zpool "
8081 case ZPOOL_STATUS_OFFLINE_DEV
:
8082 printf_color(ANSI_BOLD
, gettext("status: "));
8083 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8084 "been taken offline by the administrator.\n\tSufficient "
8085 "replicas exist for the pool to continue functioning in "
8086 "a\n\tdegraded state.\n"));
8087 printf_color(ANSI_BOLD
, gettext("action: "));
8088 printf_color(ANSI_YELLOW
, gettext("Online the device "
8089 "using 'zpool online' or replace the device with\n\t'zpool "
8093 case ZPOOL_STATUS_REMOVED_DEV
:
8094 printf_color(ANSI_BOLD
, gettext("status: "));
8095 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8096 "been removed by the administrator.\n\tSufficient "
8097 "replicas exist for the pool to continue functioning in "
8098 "a\n\tdegraded state.\n"));
8099 printf_color(ANSI_BOLD
, gettext("action: "));
8100 printf_color(ANSI_YELLOW
, gettext("Online the device "
8101 "using zpool online' or replace the device with\n\t'zpool "
8105 case ZPOOL_STATUS_RESILVERING
:
8106 case ZPOOL_STATUS_REBUILDING
:
8107 printf_color(ANSI_BOLD
, gettext("status: "));
8108 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8109 "currently being resilvered. The pool will\n\tcontinue "
8110 "to function, possibly in a degraded state.\n"));
8111 printf_color(ANSI_BOLD
, gettext("action: "));
8112 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8116 case ZPOOL_STATUS_REBUILD_SCRUB
:
8117 printf_color(ANSI_BOLD
, gettext("status: "));
8118 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8119 "been sequentially resilvered, scrubbing\n\tthe pool "
8120 "is recommended.\n"));
8121 printf_color(ANSI_BOLD
, gettext("action: "));
8122 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8123 "verify all data checksums.\n"));
8126 case ZPOOL_STATUS_CORRUPT_DATA
:
8127 printf_color(ANSI_BOLD
, gettext("status: "));
8128 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8129 "experienced an error resulting in data\n\tcorruption. "
8130 "Applications may be affected.\n"));
8131 printf_color(ANSI_BOLD
, gettext("action: "));
8132 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8133 " if possible. Otherwise restore the\n\tentire pool from "
8137 case ZPOOL_STATUS_CORRUPT_POOL
:
8138 printf_color(ANSI_BOLD
, gettext("status: "));
8139 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8140 "corrupted and the pool cannot be opened.\n"));
8141 zpool_explain_recover(zpool_get_handle(zhp
),
8142 zpool_get_name(zhp
), reason
, config
);
8145 case ZPOOL_STATUS_VERSION_OLDER
:
8146 printf_color(ANSI_BOLD
, gettext("status: "));
8147 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8148 "a legacy on-disk format. The pool can\n\tstill be used, "
8149 "but some features are unavailable.\n"));
8150 printf_color(ANSI_BOLD
, gettext("action: "));
8151 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8152 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8153 "longer be accessible on software that does not support\n\t"
8154 "feature flags.\n"));
8157 case ZPOOL_STATUS_VERSION_NEWER
:
8158 printf_color(ANSI_BOLD
, gettext("status: "));
8159 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8160 "to a newer, incompatible on-disk version.\n\tThe pool "
8161 "cannot be accessed on this system.\n"));
8162 printf_color(ANSI_BOLD
, gettext("action: "));
8163 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8164 "system running more recent software, or\n\trestore the "
8165 "pool from backup.\n"));
8168 case ZPOOL_STATUS_FEAT_DISABLED
:
8169 printf_color(ANSI_BOLD
, gettext("status: "));
8170 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8171 "requested features are not enabled on the pool.\n\t"
8172 "The pool can still be used, but some features are "
8174 printf_color(ANSI_BOLD
, gettext("action: "));
8175 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8176 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8177 "longer be accessible by software that does not support\n\t"
8178 "the features. See zpool-features(5) for details.\n"));
8181 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8182 printf_color(ANSI_BOLD
, gettext("status: "));
8183 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8184 "compatibility list specified, but it could not be\n\t"
8185 "read/parsed at this time. The pool can still be used, "
8186 "but this\n\tshould be investigated.\n"));
8187 printf_color(ANSI_BOLD
, gettext("action: "));
8188 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8189 "'compatibility' property against the\n\t"
8190 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8191 ZPOOL_DATA_COMPAT_D
".\n"));
8194 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8195 printf_color(ANSI_BOLD
, gettext("status: "));
8196 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8197 "on this system because it uses the\n\tfollowing feature(s)"
8198 " not supported on this system:\n"));
8199 zpool_print_unsup_feat(config
);
8200 (void) printf("\n");
8201 printf_color(ANSI_BOLD
, gettext("action: "));
8202 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8203 "system that supports the required feature(s),\n\tor "
8204 "restore the pool from backup.\n"));
8207 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8208 printf_color(ANSI_BOLD
, gettext("status: "));
8209 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8210 "accessed in read-only mode on this system. It\n\tcannot be"
8211 " accessed in read-write mode because it uses the "
8212 "following\n\tfeature(s) not supported on this system:\n"));
8213 zpool_print_unsup_feat(config
);
8214 (void) printf("\n");
8215 printf_color(ANSI_BOLD
, gettext("action: "));
8216 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8217 "in read-write mode. Import the pool with\n"
8218 "\t\"-o readonly=on\", access the pool from a system that "
8219 "supports the\n\trequired feature(s), or restore the "
8220 "pool from backup.\n"));
8223 case ZPOOL_STATUS_FAULTED_DEV_R
:
8224 printf_color(ANSI_BOLD
, gettext("status: "));
8225 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8226 "faulted in response to persistent errors.\n\tSufficient "
8227 "replicas exist for the pool to continue functioning "
8228 "in a\n\tdegraded state.\n"));
8229 printf_color(ANSI_BOLD
, gettext("action: "));
8230 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8231 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8234 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8235 printf_color(ANSI_BOLD
, gettext("status: "));
8236 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8237 "faulted in response to persistent errors. There are "
8238 "insufficient replicas for the pool to\n\tcontinue "
8240 printf_color(ANSI_BOLD
, gettext("action: "));
8241 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8242 "pool from a backup source. Manually marking the device\n"
8243 "\trepaired using 'zpool clear' may allow some data "
8244 "to be recovered.\n"));
8247 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8248 printf_color(ANSI_BOLD
, gettext("status: "));
8249 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8250 "because multihost writes failed or were delayed;\n\t"
8251 "another system could import the pool undetected.\n"));
8252 printf_color(ANSI_BOLD
, gettext("action: "));
8253 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8254 " are connected, then reboot your system and\n\timport the "
8258 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8259 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8260 printf_color(ANSI_BOLD
, gettext("status: "));
8261 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8262 "faulted in response to IO failures.\n"));
8263 printf_color(ANSI_BOLD
, gettext("action: "));
8264 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8265 "devices are connected, then run 'zpool clear'.\n"));
8268 case ZPOOL_STATUS_BAD_LOG
:
8269 printf_color(ANSI_BOLD
, gettext("status: "));
8270 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8271 "could not be read.\n"
8272 "\tWaiting for administrator intervention to fix the "
8273 "faulted pool.\n"));
8274 printf_color(ANSI_BOLD
, gettext("action: "));
8275 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8276 "device(s) and run 'zpool online',\n"
8277 "\tor ignore the intent log records by running "
8278 "'zpool clear'.\n"));
8281 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8282 (void) printf(gettext("status: One or more devices are "
8283 "configured to use a non-native block size.\n"
8284 "\tExpect reduced performance.\n"));
8285 (void) printf(gettext("action: Replace affected devices with "
8286 "devices that support the\n\tconfigured block size, or "
8287 "migrate data to a properly configured\n\tpool.\n"));
8290 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8291 printf_color(ANSI_BOLD
, gettext("status: "));
8292 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8293 " and system hostid on imported pool.\n\tThis pool was "
8294 "previously imported into a system with a different "
8295 "hostid,\n\tand then was verbatim imported into this "
8297 printf_color(ANSI_BOLD
, gettext("action: "));
8298 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8299 "systems on which it is imported.\n"
8300 "\tThen import it to correct the mismatch.\n"));
8303 case ZPOOL_STATUS_ERRATA
:
8304 printf_color(ANSI_BOLD
, gettext("status: "));
8305 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8309 case ZPOOL_ERRATA_NONE
:
8312 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8313 printf_color(ANSI_BOLD
, gettext("action: "));
8314 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8315 " run 'zpool scrub'.\n"));
8318 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8319 (void) printf(gettext("\tExisting encrypted datasets "
8320 "contain an on-disk incompatibility\n\twhich "
8321 "needs to be corrected.\n"));
8322 printf_color(ANSI_BOLD
, gettext("action: "));
8323 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8324 " backup existing encrypted datasets to new\n\t"
8325 "encrypted datasets and destroy the old ones. "
8326 "'zfs mount -o ro' can\n\tbe used to temporarily "
8327 "mount existing encrypted datasets readonly.\n"));
8330 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8331 (void) printf(gettext("\tExisting encrypted snapshots "
8332 "and bookmarks contain an on-disk\n\tincompat"
8333 "ibility. This may cause on-disk corruption if "
8334 "they are used\n\twith 'zfs recv'.\n"));
8335 printf_color(ANSI_BOLD
, gettext("action: "));
8336 printf_color(ANSI_YELLOW
, gettext("To correct the"
8337 "issue, enable the bookmark_v2 feature. No "
8338 "additional\n\taction is needed if there are no "
8339 "encrypted snapshots or bookmarks.\n\tIf preserving"
8340 "the encrypted snapshots and bookmarks is required,"
8341 " use\n\ta non-raw send to backup and restore them."
8342 " Alternately, they may be\n\tremoved to resolve "
8343 "the incompatibility.\n"));
8348 * All errata which allow the pool to be imported
8349 * must contain an action message.
8357 * The remaining errors can't actually be generated, yet.
8359 assert(reason
== ZPOOL_STATUS_OK
);
8362 if (msgid
!= NULL
) {
8364 printf_color(ANSI_BOLD
, gettext("see:"));
8366 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8370 if (config
!= NULL
) {
8372 nvlist_t
**spares
, **l2cache
;
8373 uint_t nspares
, nl2cache
;
8374 pool_checkpoint_stat_t
*pcs
= NULL
;
8375 pool_removal_stat_t
*prs
= NULL
;
8377 print_scan_status(zhp
, nvroot
);
8379 (void) nvlist_lookup_uint64_array(nvroot
,
8380 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8381 print_removal_status(zhp
, prs
);
8383 (void) nvlist_lookup_uint64_array(nvroot
,
8384 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8385 print_checkpoint_status(pcs
);
8387 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8388 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8389 if (cbp
->cb_namewidth
< 10)
8390 cbp
->cb_namewidth
= 10;
8392 color_start(ANSI_BOLD
);
8393 (void) printf(gettext("config:\n\n"));
8394 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8395 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8399 if (cbp
->cb_print_slow_ios
) {
8400 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8403 if (cbp
->vcdl
!= NULL
)
8404 print_cmd_columns(cbp
->vcdl
, 0);
8408 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8411 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8412 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8413 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8415 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8416 &l2cache
, &nl2cache
) == 0)
8417 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8419 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8420 &spares
, &nspares
) == 0)
8421 print_spares(zhp
, cbp
, spares
, nspares
);
8423 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8425 nvlist_t
*nverrlist
= NULL
;
8428 * If the approximate error count is small, get a
8429 * precise count by fetching the entire log and
8430 * uniquifying the results.
8432 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8433 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8438 while ((elem
= nvlist_next_nvpair(nverrlist
,
8443 nvlist_free(nverrlist
);
8445 (void) printf("\n");
8448 (void) printf(gettext("errors: No known data "
8450 else if (!cbp
->cb_verbose
)
8451 (void) printf(gettext("errors: %llu data "
8452 "errors, use '-v' for a list\n"),
8453 (u_longlong_t
)nerr
);
8455 print_error_log(zhp
);
8458 if (cbp
->cb_dedup_stats
)
8459 print_dedup_stats(config
);
8461 (void) printf(gettext("config: The configuration cannot be "
8469 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8470 * [interval [count]]
8472 * -c CMD For each vdev, run command CMD
8473 * -i Display vdev initialization status.
8474 * -g Display guid for individual vdev name.
8475 * -L Follow links when resolving vdev path name.
8476 * -p Display values in parsable (exact) format.
8477 * -P Display full path for vdev name.
8478 * -s Display slow IOs column.
8479 * -v Display complete error logs
8480 * -x Display only pools with potential problems
8481 * -D Display dedup status (undocumented)
8482 * -t Display vdev TRIM status.
8483 * -T Display a timestamp in date(1) or Unix format
8485 * Describes the health status of all pools or some subset.
8488 zpool_do_status(int argc
, char **argv
)
8493 unsigned long count
= 0;
8494 status_cbdata_t cb
= { 0 };
8498 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8503 gettext("Can't set -c flag twice\n"));
8507 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8508 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8509 fprintf(stderr
, gettext(
8510 "Can't run -c, disabled by "
8511 "ZPOOL_SCRIPTS_ENABLED.\n"));
8515 if ((getuid() <= 0 || geteuid() <= 0) &&
8516 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8517 fprintf(stderr
, gettext(
8518 "Can't run -c with root privileges "
8519 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8525 cb
.cb_print_vdev_init
= B_TRUE
;
8528 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8531 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8534 cb
.cb_literal
= B_TRUE
;
8537 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8540 cb
.cb_print_slow_ios
= B_TRUE
;
8543 cb
.cb_verbose
= B_TRUE
;
8546 cb
.cb_explain
= B_TRUE
;
8549 cb
.cb_dedup_stats
= B_TRUE
;
8552 cb
.cb_print_vdev_trim
= B_TRUE
;
8555 get_timestamp_arg(*optarg
);
8558 if (optopt
== 'c') {
8559 print_zpool_script_list("status");
8563 gettext("invalid option '%c'\n"), optopt
);
8572 get_interval_count(&argc
, argv
, &interval
, &count
);
8575 cb
.cb_allpools
= B_TRUE
;
8577 cb
.cb_first
= B_TRUE
;
8578 cb
.cb_print_status
= B_TRUE
;
8581 if (timestamp_fmt
!= NODATE
)
8582 print_timestamp(timestamp_fmt
);
8585 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8588 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, cb
.cb_literal
,
8589 status_callback
, &cb
);
8591 if (cb
.vcdl
!= NULL
)
8592 free_vdev_cmd_data_list(cb
.vcdl
);
8594 if (argc
== 0 && cb
.cb_count
== 0)
8595 (void) fprintf(stderr
, gettext("no pools available\n"));
8596 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8597 (void) printf(gettext("all pools are healthy\n"));
8605 if (count
!= 0 && --count
== 0)
8608 (void) fsleep(interval
);
8614 typedef struct upgrade_cbdata
{
8617 uint64_t cb_version
;
8622 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8624 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8625 int *count
= (int *)unsupp_fs
;
8627 if (zfs_version
> ZPL_VERSION
) {
8628 (void) printf(gettext("%s (v%d) is not supported by this "
8629 "implementation of ZFS.\n"),
8630 zfs_get_name(zhp
), zfs_version
);
8634 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8642 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8646 uint64_t oldversion
;
8649 config
= zpool_get_config(zhp
, NULL
);
8650 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8653 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8654 assert(oldversion
< version
);
8656 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8661 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8662 "to %d unsupported filesystems (max v%d).\n"),
8663 unsupp_fs
, (int)ZPL_VERSION
);
8667 ret
= zpool_upgrade(zhp
, version
);
8671 if (version
>= SPA_VERSION_FEATURES
) {
8672 (void) printf(gettext("Successfully upgraded "
8673 "'%s' from version %llu to feature flags.\n"),
8674 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8676 (void) printf(gettext("Successfully upgraded "
8677 "'%s' from version %llu to version %llu.\n"),
8678 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8679 (u_longlong_t
)version
);
8686 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8689 boolean_t firstff
= B_TRUE
;
8690 nvlist_t
*enabled
= zpool_get_features(zhp
);
8692 char compat
[ZFS_MAXPROPLEN
];
8693 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8694 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8697 boolean_t requested_features
[SPA_FEATURES
];
8698 if (zpool_do_load_compat(compat
, requested_features
) !=
8699 ZPOOL_COMPATIBILITY_OK
)
8703 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8704 const char *fname
= spa_feature_table
[i
].fi_uname
;
8705 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8707 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8710 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
8712 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8713 ret
= zpool_set_prop(zhp
, propname
,
8714 ZFS_FEATURE_ENABLED
);
8722 (void) printf(gettext("Enabled the "
8723 "following features on '%s':\n"),
8724 zpool_get_name(zhp
));
8727 (void) printf(gettext(" %s\n"), fname
);
8738 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8740 upgrade_cbdata_t
*cbp
= arg
;
8743 boolean_t printnl
= B_FALSE
;
8746 config
= zpool_get_config(zhp
, NULL
);
8747 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8750 assert(SPA_VERSION_IS_SUPPORTED(version
));
8752 if (version
< cbp
->cb_version
) {
8753 cbp
->cb_first
= B_FALSE
;
8754 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8760 * If they did "zpool upgrade -a", then we could
8761 * be doing ioctls to different pools. We need
8762 * to log this history once to each pool, and bypass
8763 * the normal history logging that happens in main().
8765 (void) zpool_log_history(g_zfs
, history_str
);
8766 log_history
= B_FALSE
;
8769 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8771 ret
= upgrade_enable_all(zhp
, &count
);
8776 cbp
->cb_first
= B_FALSE
;
8782 (void) printf(gettext("\n"));
8789 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8791 upgrade_cbdata_t
*cbp
= arg
;
8795 config
= zpool_get_config(zhp
, NULL
);
8796 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8799 assert(SPA_VERSION_IS_SUPPORTED(version
));
8801 if (version
< SPA_VERSION_FEATURES
) {
8802 if (cbp
->cb_first
) {
8803 (void) printf(gettext("The following pools are "
8804 "formatted with legacy version numbers and can\n"
8805 "be upgraded to use feature flags. After "
8806 "being upgraded, these pools\nwill no "
8807 "longer be accessible by software that does not "
8808 "support feature\nflags.\n\n"));
8809 (void) printf(gettext("VER POOL\n"));
8810 (void) printf(gettext("--- ------------\n"));
8811 cbp
->cb_first
= B_FALSE
;
8814 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
8815 zpool_get_name(zhp
));
8822 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
8824 upgrade_cbdata_t
*cbp
= arg
;
8828 config
= zpool_get_config(zhp
, NULL
);
8829 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8832 if (version
>= SPA_VERSION_FEATURES
) {
8834 boolean_t poolfirst
= B_TRUE
;
8835 nvlist_t
*enabled
= zpool_get_features(zhp
);
8837 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8838 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8839 const char *fname
= spa_feature_table
[i
].fi_uname
;
8841 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8844 if (!nvlist_exists(enabled
, fguid
)) {
8845 if (cbp
->cb_first
) {
8846 (void) printf(gettext("\nSome "
8847 "supported features are not "
8848 "enabled on the following pools. "
8849 "Once a\nfeature is enabled the "
8850 "pool may become incompatible with "
8851 "software\nthat does not support "
8853 "zpool-features(5) for "
8855 (void) printf(gettext("POOL "
8857 (void) printf(gettext("------"
8859 cbp
->cb_first
= B_FALSE
;
8863 (void) printf(gettext("%s\n"),
8864 zpool_get_name(zhp
));
8865 poolfirst
= B_FALSE
;
8868 (void) printf(gettext(" %s\n"), fname
);
8871 * If they did "zpool upgrade -a", then we could
8872 * be doing ioctls to different pools. We need
8873 * to log this history once to each pool, and bypass
8874 * the normal history logging that happens in main().
8876 (void) zpool_log_history(g_zfs
, history_str
);
8877 log_history
= B_FALSE
;
8886 upgrade_one(zpool_handle_t
*zhp
, void *data
)
8888 boolean_t printnl
= B_FALSE
;
8889 upgrade_cbdata_t
*cbp
= data
;
8890 uint64_t cur_version
;
8893 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
8894 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
8895 "Pool 'log' must be renamed using export and import"
8900 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
8901 if (cur_version
> cbp
->cb_version
) {
8902 (void) printf(gettext("Pool '%s' is already formatted "
8903 "using more current version '%llu'.\n\n"),
8904 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
8908 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
8909 (void) printf(gettext("Pool '%s' is already formatted "
8910 "using version %llu.\n\n"), zpool_get_name(zhp
),
8911 (u_longlong_t
)cbp
->cb_version
);
8915 if (cur_version
!= cbp
->cb_version
) {
8917 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8922 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8924 ret
= upgrade_enable_all(zhp
, &count
);
8930 } else if (cur_version
== SPA_VERSION
) {
8931 (void) printf(gettext("Pool '%s' already has all "
8932 "supported and requested features enabled.\n"),
8933 zpool_get_name(zhp
));
8938 (void) printf(gettext("\n"));
8947 * zpool upgrade [-V version] <-a | pool ...>
8949 * With no arguments, display downrev'd ZFS pool available for upgrade.
8950 * Individual pools can be upgraded by specifying the pool, and '-a' will
8951 * upgrade all pools.
8954 zpool_do_upgrade(int argc
, char **argv
)
8957 upgrade_cbdata_t cb
= { 0 };
8959 boolean_t showversions
= B_FALSE
;
8960 boolean_t upgradeall
= B_FALSE
;
8965 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
8968 upgradeall
= B_TRUE
;
8971 showversions
= B_TRUE
;
8974 cb
.cb_version
= strtoll(optarg
, &end
, 10);
8976 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
8977 (void) fprintf(stderr
,
8978 gettext("invalid version '%s'\n"), optarg
);
8983 (void) fprintf(stderr
, gettext("missing argument for "
8984 "'%c' option\n"), optopt
);
8988 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
8999 if (cb
.cb_version
== 0) {
9000 cb
.cb_version
= SPA_VERSION
;
9001 } else if (!upgradeall
&& argc
== 0) {
9002 (void) fprintf(stderr
, gettext("-V option is "
9003 "incompatible with other arguments\n"));
9008 if (upgradeall
|| argc
!= 0) {
9009 (void) fprintf(stderr
, gettext("-v option is "
9010 "incompatible with other arguments\n"));
9013 } else if (upgradeall
) {
9015 (void) fprintf(stderr
, gettext("-a option should not "
9016 "be used along with a pool name\n"));
9021 (void) printf(gettext("This system supports ZFS pool feature "
9026 (void) printf(gettext("The following features are "
9028 (void) printf(gettext("FEAT DESCRIPTION\n"));
9029 (void) printf("----------------------------------------------"
9030 "---------------\n");
9031 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9032 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9033 if (!fi
->fi_zfs_mod_supported
)
9036 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9037 " (read-only compatible)" : "";
9039 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9040 (void) printf(" %s\n", fi
->fi_desc
);
9042 (void) printf("\n");
9044 (void) printf(gettext("The following legacy versions are also "
9046 (void) printf(gettext("VER DESCRIPTION\n"));
9047 (void) printf("--- -----------------------------------------"
9048 "---------------\n");
9049 (void) printf(gettext(" 1 Initial ZFS version\n"));
9050 (void) printf(gettext(" 2 Ditto blocks "
9051 "(replicated metadata)\n"));
9052 (void) printf(gettext(" 3 Hot spares and double parity "
9054 (void) printf(gettext(" 4 zpool history\n"));
9055 (void) printf(gettext(" 5 Compression using the gzip "
9057 (void) printf(gettext(" 6 bootfs pool property\n"));
9058 (void) printf(gettext(" 7 Separate intent log devices\n"));
9059 (void) printf(gettext(" 8 Delegated administration\n"));
9060 (void) printf(gettext(" 9 refquota and refreservation "
9062 (void) printf(gettext(" 10 Cache devices\n"));
9063 (void) printf(gettext(" 11 Improved scrub performance\n"));
9064 (void) printf(gettext(" 12 Snapshot properties\n"));
9065 (void) printf(gettext(" 13 snapused property\n"));
9066 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9067 (void) printf(gettext(" 15 user/group space accounting\n"));
9068 (void) printf(gettext(" 16 stmf property support\n"));
9069 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9070 (void) printf(gettext(" 18 Snapshot user holds\n"));
9071 (void) printf(gettext(" 19 Log device removal\n"));
9072 (void) printf(gettext(" 20 Compression using zle "
9073 "(zero-length encoding)\n"));
9074 (void) printf(gettext(" 21 Deduplication\n"));
9075 (void) printf(gettext(" 22 Received properties\n"));
9076 (void) printf(gettext(" 23 Slim ZIL\n"));
9077 (void) printf(gettext(" 24 System attributes\n"));
9078 (void) printf(gettext(" 25 Improved scrub stats\n"));
9079 (void) printf(gettext(" 26 Improved snapshot deletion "
9081 (void) printf(gettext(" 27 Improved snapshot creation "
9083 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9084 (void) printf(gettext("\nFor more information on a particular "
9085 "version, including supported releases,\n"));
9086 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9087 } else if (argc
== 0 && upgradeall
) {
9088 cb
.cb_first
= B_TRUE
;
9089 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9090 if (ret
== 0 && cb
.cb_first
) {
9091 if (cb
.cb_version
== SPA_VERSION
) {
9092 (void) printf(gettext("All pools are already "
9093 "formatted using feature flags.\n\n"));
9094 (void) printf(gettext("Every feature flags "
9095 "pool already has all supported and "
9096 "requested features enabled.\n"));
9098 (void) printf(gettext("All pools are already "
9099 "formatted with version %llu or higher.\n"),
9100 (u_longlong_t
)cb
.cb_version
);
9103 } else if (argc
== 0) {
9104 cb
.cb_first
= B_TRUE
;
9105 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9109 (void) printf(gettext("All pools are formatted "
9110 "using feature flags.\n\n"));
9112 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9113 "for a list of available legacy versions.\n"));
9116 cb
.cb_first
= B_TRUE
;
9117 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9121 (void) printf(gettext("Every feature flags pool has "
9122 "all supported and requested features enabled.\n"));
9124 (void) printf(gettext("\n"));
9127 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
,
9134 typedef struct hist_cbdata
{
9141 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9147 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9148 &records
, &numrecords
) == 0);
9149 for (i
= 0; i
< numrecords
; i
++) {
9150 nvlist_t
*rec
= records
[i
];
9153 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9157 tsec
= fnvlist_lookup_uint64(records
[i
],
9159 (void) localtime_r(&tsec
, &t
);
9160 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9163 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9164 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9165 ZPOOL_HIST_ELAPSED_NS
);
9166 (void) snprintf(tbuf
+ strlen(tbuf
),
9167 sizeof (tbuf
) - strlen(tbuf
),
9168 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9171 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9172 (void) printf("%s %s", tbuf
,
9173 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9174 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9176 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9179 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9180 (void) printf("%s unrecognized record:\n",
9182 dump_nvlist(rec
, 4);
9185 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9186 zfs_history_event_names
[ievent
],
9187 (longlong_t
)fnvlist_lookup_uint64(
9188 rec
, ZPOOL_HIST_TXG
),
9189 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9190 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9193 (void) printf("%s [txg:%lld] %s", tbuf
,
9194 (longlong_t
)fnvlist_lookup_uint64(
9195 rec
, ZPOOL_HIST_TXG
),
9196 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9197 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9198 (void) printf(" %s (%llu)",
9199 fnvlist_lookup_string(rec
,
9201 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9204 (void) printf(" %s", fnvlist_lookup_string(rec
,
9205 ZPOOL_HIST_INT_STR
));
9206 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9209 (void) printf("%s ioctl %s\n", tbuf
,
9210 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9211 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9212 (void) printf(" input:\n");
9213 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9214 ZPOOL_HIST_INPUT_NVL
), 8);
9216 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9217 (void) printf(" output:\n");
9218 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9219 ZPOOL_HIST_OUTPUT_NVL
), 8);
9221 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9222 (void) printf(" output nvlist omitted; "
9223 "original size: %lldKB\n",
9224 (longlong_t
)fnvlist_lookup_int64(rec
,
9225 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9227 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9228 (void) printf(" errno: %lld\n",
9229 (longlong_t
)fnvlist_lookup_int64(rec
,
9235 (void) printf("%s unrecognized record:\n", tbuf
);
9236 dump_nvlist(rec
, 4);
9240 (void) printf("\n");
9243 (void) printf(" [");
9244 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9245 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9246 struct passwd
*pwd
= getpwuid(who
);
9247 (void) printf("user %d ", (int)who
);
9249 (void) printf("(%s) ", pwd
->pw_name
);
9251 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9252 (void) printf("on %s",
9253 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9255 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9256 (void) printf(":%s",
9257 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9261 (void) printf("\n");
9266 * Print out the command history for a specific pool.
9269 get_history_one(zpool_handle_t
*zhp
, void *data
)
9273 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9275 boolean_t eof
= B_FALSE
;
9277 cb
->first
= B_FALSE
;
9279 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9282 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9285 print_history_records(nvhis
, cb
);
9288 (void) printf("\n");
9294 * zpool history <pool>
9296 * Displays the history of commands that modified pools.
9299 zpool_do_history(int argc
, char **argv
)
9301 hist_cbdata_t cbdata
= { 0 };
9305 cbdata
.first
= B_TRUE
;
9307 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9310 cbdata
.longfmt
= B_TRUE
;
9313 cbdata
.internal
= B_TRUE
;
9316 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9324 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
, get_history_one
,
9327 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9328 (void) fprintf(stderr
, gettext("no pools available\n"));
9335 typedef struct ev_opts
{
9340 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9344 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9346 char ctime_str
[26], str
[32], *ptr
;
9350 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9351 memset(str
, ' ', 32);
9352 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9353 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9354 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9355 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9356 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9358 (void) printf(gettext("%s\t"), str
);
9360 (void) printf(gettext("%s "), str
);
9362 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9363 (void) printf(gettext("%s\n"), ptr
);
9367 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9371 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9372 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9374 data_type_t type
= nvpair_type(nvp
);
9375 const char *name
= nvpair_name(nvp
);
9385 printf(gettext("%*s%s = "), depth
, "", name
);
9388 case DATA_TYPE_BOOLEAN
:
9389 printf(gettext("%s"), "1");
9392 case DATA_TYPE_BOOLEAN_VALUE
:
9393 (void) nvpair_value_boolean_value(nvp
, &b
);
9394 printf(gettext("%s"), b
? "1" : "0");
9397 case DATA_TYPE_BYTE
:
9398 (void) nvpair_value_byte(nvp
, &i8
);
9399 printf(gettext("0x%x"), i8
);
9402 case DATA_TYPE_INT8
:
9403 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9404 printf(gettext("0x%x"), i8
);
9407 case DATA_TYPE_UINT8
:
9408 (void) nvpair_value_uint8(nvp
, &i8
);
9409 printf(gettext("0x%x"), i8
);
9412 case DATA_TYPE_INT16
:
9413 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9414 printf(gettext("0x%x"), i16
);
9417 case DATA_TYPE_UINT16
:
9418 (void) nvpair_value_uint16(nvp
, &i16
);
9419 printf(gettext("0x%x"), i16
);
9422 case DATA_TYPE_INT32
:
9423 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9424 printf(gettext("0x%x"), i32
);
9427 case DATA_TYPE_UINT32
:
9428 (void) nvpair_value_uint32(nvp
, &i32
);
9429 printf(gettext("0x%x"), i32
);
9432 case DATA_TYPE_INT64
:
9433 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9434 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9437 case DATA_TYPE_UINT64
:
9438 (void) nvpair_value_uint64(nvp
, &i64
);
9440 * translate vdev state values to readable
9441 * strings to aide zpool events consumers
9444 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9446 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9447 printf(gettext("\"%s\" (0x%llx)"),
9448 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9451 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9455 case DATA_TYPE_HRTIME
:
9456 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9457 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9460 case DATA_TYPE_STRING
:
9461 (void) nvpair_value_string(nvp
, &str
);
9462 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9465 case DATA_TYPE_NVLIST
:
9466 printf(gettext("(embedded nvlist)\n"));
9467 (void) nvpair_value_nvlist(nvp
, &cnv
);
9468 zpool_do_events_nvprint(cnv
, depth
+ 8);
9469 printf(gettext("%*s(end %s)"), depth
, "", name
);
9472 case DATA_TYPE_NVLIST_ARRAY
: {
9476 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9477 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9478 for (i
= 0; i
< nelem
; i
++) {
9479 printf(gettext("%*s%s[%d] = %s\n"),
9480 depth
, "", name
, i
, "(embedded nvlist)");
9481 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9482 printf(gettext("%*s(end %s[%i])\n"),
9483 depth
, "", name
, i
);
9485 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9489 case DATA_TYPE_INT8_ARRAY
: {
9493 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9494 for (i
= 0; i
< nelem
; i
++)
9495 printf(gettext("0x%x "), val
[i
]);
9500 case DATA_TYPE_UINT8_ARRAY
: {
9504 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9505 for (i
= 0; i
< nelem
; i
++)
9506 printf(gettext("0x%x "), val
[i
]);
9511 case DATA_TYPE_INT16_ARRAY
: {
9515 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9516 for (i
= 0; i
< nelem
; i
++)
9517 printf(gettext("0x%x "), val
[i
]);
9522 case DATA_TYPE_UINT16_ARRAY
: {
9526 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9527 for (i
= 0; i
< nelem
; i
++)
9528 printf(gettext("0x%x "), val
[i
]);
9533 case DATA_TYPE_INT32_ARRAY
: {
9537 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9538 for (i
= 0; i
< nelem
; i
++)
9539 printf(gettext("0x%x "), val
[i
]);
9544 case DATA_TYPE_UINT32_ARRAY
: {
9548 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9549 for (i
= 0; i
< nelem
; i
++)
9550 printf(gettext("0x%x "), val
[i
]);
9555 case DATA_TYPE_INT64_ARRAY
: {
9559 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9560 for (i
= 0; i
< nelem
; i
++)
9561 printf(gettext("0x%llx "),
9562 (u_longlong_t
)val
[i
]);
9567 case DATA_TYPE_UINT64_ARRAY
: {
9571 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9572 for (i
= 0; i
< nelem
; i
++)
9573 printf(gettext("0x%llx "),
9574 (u_longlong_t
)val
[i
]);
9579 case DATA_TYPE_STRING_ARRAY
: {
9583 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9584 for (i
= 0; i
< nelem
; i
++)
9585 printf(gettext("\"%s\" "),
9586 str
[i
] ? str
[i
] : "<NULL>");
9591 case DATA_TYPE_BOOLEAN_ARRAY
:
9592 case DATA_TYPE_BYTE_ARRAY
:
9593 case DATA_TYPE_DOUBLE
:
9594 case DATA_TYPE_DONTCARE
:
9595 case DATA_TYPE_UNKNOWN
:
9596 printf(gettext("<unknown>"));
9600 printf(gettext("\n"));
9605 zpool_do_events_next(ev_opts_t
*opts
)
9608 int zevent_fd
, ret
, dropped
;
9611 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9612 VERIFY(zevent_fd
>= 0);
9614 if (!opts
->scripted
)
9615 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9618 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9619 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9620 if (ret
|| nvl
== NULL
)
9624 (void) printf(gettext("dropped %d events\n"), dropped
);
9626 if (strlen(opts
->poolname
) > 0 &&
9627 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9628 strcmp(opts
->poolname
, pool
) != 0)
9631 zpool_do_events_short(nvl
, opts
);
9633 if (opts
->verbose
) {
9634 zpool_do_events_nvprint(nvl
, 8);
9635 printf(gettext("\n"));
9637 (void) fflush(stdout
);
9642 VERIFY(0 == close(zevent_fd
));
9648 zpool_do_events_clear(ev_opts_t
*opts
)
9652 ret
= zpool_events_clear(g_zfs
, &count
);
9654 (void) printf(gettext("cleared %d events\n"), count
);
9660 * zpool events [-vHf [pool] | -c]
9662 * Displays events logs by ZFS.
9665 zpool_do_events(int argc
, char **argv
)
9667 ev_opts_t opts
= { 0 };
9672 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9687 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9696 (void) fprintf(stderr
, gettext("too many arguments\n"));
9698 } else if (argc
== 1) {
9699 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9700 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9701 (void) fprintf(stderr
,
9702 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9707 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9709 (void) fprintf(stderr
,
9710 gettext("invalid options combined with -c\n"));
9715 ret
= zpool_do_events_clear(&opts
);
9717 ret
= zpool_do_events_next(&opts
);
9723 get_callback(zpool_handle_t
*zhp
, void *data
)
9725 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9726 char value
[MAXNAMELEN
];
9727 zprop_source_t srctype
;
9730 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
9733 * Skip the special fake placeholder. This will also skip
9734 * over the name property when 'all' is specified.
9736 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9737 pl
== cbp
->cb_proplist
)
9740 if (pl
->pl_prop
== ZPROP_INVAL
&&
9741 (zpool_prop_feature(pl
->pl_user_prop
) ||
9742 zpool_prop_unsupported(pl
->pl_user_prop
))) {
9743 srctype
= ZPROP_SRC_LOCAL
;
9745 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
9746 value
, sizeof (value
)) == 0) {
9747 zprop_print_one_property(zpool_get_name(zhp
),
9748 cbp
, pl
->pl_user_prop
, value
, srctype
,
9752 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
9753 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
9756 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
9757 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
9765 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
9767 * -H Scripted mode. Don't display headers, and separate properties
9769 * -o List of columns to display. Defaults to
9770 * "name,property,value,source".
9771 * -p Display values in parsable (exact) format.
9773 * Get properties of pools in the system. Output space statistics
9774 * for each one as well as other attributes.
9777 zpool_do_get(int argc
, char **argv
)
9779 zprop_get_cbdata_t cb
= { 0 };
9780 zprop_list_t fake_name
= { 0 };
9785 cb
.cb_first
= B_TRUE
;
9788 * Set up default columns and sources.
9790 cb
.cb_sources
= ZPROP_SRC_ALL
;
9791 cb
.cb_columns
[0] = GET_COL_NAME
;
9792 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9793 cb
.cb_columns
[2] = GET_COL_VALUE
;
9794 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9795 cb
.cb_type
= ZFS_TYPE_POOL
;
9798 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
9801 cb
.cb_literal
= B_TRUE
;
9804 cb
.cb_scripted
= B_TRUE
;
9807 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
9809 while (*optarg
!= '\0') {
9810 static char *col_subopts
[] =
9811 { "name", "property", "value", "source",
9814 if (i
== ZFS_GET_NCOLS
) {
9815 (void) fprintf(stderr
, gettext("too "
9816 "many fields given to -o "
9821 switch (getsubopt(&optarg
, col_subopts
,
9824 cb
.cb_columns
[i
++] = GET_COL_NAME
;
9827 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
9830 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
9833 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
9837 (void) fprintf(stderr
,
9838 gettext("\"all\" conflicts "
9839 "with specific fields "
9840 "given to -o option\n"));
9843 cb
.cb_columns
[0] = GET_COL_NAME
;
9844 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9845 cb
.cb_columns
[2] = GET_COL_VALUE
;
9846 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9850 (void) fprintf(stderr
,
9851 gettext("invalid column name "
9858 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9868 (void) fprintf(stderr
, gettext("missing property "
9873 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
9874 ZFS_TYPE_POOL
) != 0)
9880 if (cb
.cb_proplist
!= NULL
) {
9881 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
9882 fake_name
.pl_width
= strlen(gettext("NAME"));
9883 fake_name
.pl_next
= cb
.cb_proplist
;
9884 cb
.cb_proplist
= &fake_name
;
9887 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_literal
,
9890 if (cb
.cb_proplist
== &fake_name
)
9891 zprop_free_list(fake_name
.pl_next
);
9893 zprop_free_list(cb
.cb_proplist
);
9898 typedef struct set_cbdata
{
9901 boolean_t cb_any_successful
;
9905 set_callback(zpool_handle_t
*zhp
, void *data
)
9908 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
9910 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
9913 cb
->cb_any_successful
= B_TRUE
;
9919 zpool_do_set(int argc
, char **argv
)
9921 set_cbdata_t cb
= { 0 };
9924 if (argc
> 1 && argv
[1][0] == '-') {
9925 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9931 (void) fprintf(stderr
, gettext("missing property=value "
9937 (void) fprintf(stderr
, gettext("missing pool name\n"));
9942 (void) fprintf(stderr
, gettext("too many pool names\n"));
9946 cb
.cb_propname
= argv
[1];
9947 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
9948 if (cb
.cb_value
== NULL
) {
9949 (void) fprintf(stderr
, gettext("missing value in "
9950 "property=value argument\n"));
9954 *(cb
.cb_value
) = '\0';
9957 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
, B_FALSE
,
9963 /* Add up the total number of bytes left to initialize/trim across all vdevs */
9965 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
9967 uint64_t bytes_remaining
;
9972 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
9973 activity
== ZPOOL_WAIT_TRIM
);
9975 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
9976 (uint64_t **)&vs
, &c
) == 0);
9978 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
9979 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
9980 bytes_remaining
= vs
->vs_initialize_bytes_est
-
9981 vs
->vs_initialize_bytes_done
;
9982 else if (activity
== ZPOOL_WAIT_TRIM
&&
9983 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
9984 bytes_remaining
= vs
->vs_trim_bytes_est
-
9985 vs
->vs_trim_bytes_done
;
9987 bytes_remaining
= 0;
9989 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
9990 &child
, &children
) != 0)
9993 for (c
= 0; c
< children
; c
++)
9994 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
9996 return (bytes_remaining
);
9999 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10001 vdev_activity_top_remaining(nvlist_t
*nv
)
10003 uint64_t bytes_remaining
= 0;
10008 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10009 &child
, &children
) != 0)
10012 for (uint_t c
= 0; c
< children
; c
++) {
10013 vdev_rebuild_stat_t
*vrs
;
10016 error
= nvlist_lookup_uint64_array(child
[c
],
10017 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
10019 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
10020 bytes_remaining
+= (vrs
->vrs_bytes_est
-
10021 vrs
->vrs_bytes_rebuilt
);
10026 return (bytes_remaining
);
10029 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10031 vdev_any_spare_replacing(nvlist_t
*nv
)
10034 uint_t c
, children
;
10037 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
10039 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
10040 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
10041 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
10045 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10046 &child
, &children
) != 0)
10049 for (c
= 0; c
< children
; c
++) {
10050 if (vdev_any_spare_replacing(child
[c
]))
10057 typedef struct wait_data
{
10059 boolean_t wd_scripted
;
10060 boolean_t wd_exact
;
10061 boolean_t wd_headers_once
;
10062 boolean_t wd_should_exit
;
10063 /* Which activities to wait for */
10064 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10066 pthread_cond_t wd_cv
;
10067 pthread_mutex_t wd_mutex
;
10071 * Print to stdout a single line, containing one column for each activity that
10072 * we are waiting for specifying how many bytes of work are left for that
10076 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10078 nvlist_t
*config
, *nvroot
;
10081 pool_checkpoint_stat_t
*pcs
= NULL
;
10082 pool_scan_stat_t
*pss
= NULL
;
10083 pool_removal_stat_t
*prs
= NULL
;
10084 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
10085 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10086 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10088 /* Calculate the width of each column */
10089 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10091 * Make sure we have enough space in the col for pretty-printed
10092 * numbers and for the column header, and then leave a couple
10093 * spaces between cols for readability.
10095 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10098 /* Print header if appropriate */
10099 int term_height
= terminal_height();
10100 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10101 row
% (term_height
-1) == 0);
10102 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10103 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10104 if (wd
->wd_enabled
[i
])
10105 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10107 (void) printf("\n");
10110 /* Bytes of work remaining in each activity */
10111 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10113 bytes_rem
[ZPOOL_WAIT_FREE
] =
10114 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10116 config
= zpool_get_config(zhp
, NULL
);
10117 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10119 (void) nvlist_lookup_uint64_array(nvroot
,
10120 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10121 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10122 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10124 (void) nvlist_lookup_uint64_array(nvroot
,
10125 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10126 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10127 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10130 (void) nvlist_lookup_uint64_array(nvroot
,
10131 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10132 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10133 pss
->pss_pass_scrub_pause
== 0) {
10134 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10135 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10136 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10138 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10139 } else if (check_rebuilding(nvroot
, NULL
)) {
10140 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10141 vdev_activity_top_remaining(nvroot
);
10144 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10145 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10146 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10147 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10150 * A replace finishes after resilvering finishes, so the amount of work
10151 * left for a replace is the same as for resilvering.
10153 * It isn't quite correct to say that if we have any 'spare' or
10154 * 'replacing' vdevs and a resilver is happening, then a replace is in
10155 * progress, like we do here. When a hot spare is used, the faulted vdev
10156 * is not removed after the hot spare is resilvered, so parent 'spare'
10157 * vdev is not removed either. So we could have a 'spare' vdev, but be
10158 * resilvering for a different reason. However, we use it as a heuristic
10159 * because we don't have access to the DTLs, which could tell us whether
10160 * or not we have really finished resilvering a hot spare.
10162 if (vdev_any_spare_replacing(nvroot
))
10163 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10165 if (timestamp_fmt
!= NODATE
)
10166 print_timestamp(timestamp_fmt
);
10168 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10170 if (!wd
->wd_enabled
[i
])
10174 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10177 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10179 if (wd
->wd_scripted
)
10180 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10182 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10184 (void) printf("\n");
10185 (void) fflush(stdout
);
10189 wait_status_thread(void *arg
)
10191 wait_data_t
*wd
= (wait_data_t
*)arg
;
10192 zpool_handle_t
*zhp
;
10194 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10195 return (void *)(1);
10197 for (int row
= 0; ; row
++) {
10199 struct timespec timeout
;
10201 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10203 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10204 zpool_props_refresh(zhp
) != 0) {
10206 return (void *)(uintptr_t)(missing
? 0 : 1);
10209 print_wait_status_row(wd
, zhp
, row
);
10211 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10212 long nanos
= timeout
.tv_nsec
+
10213 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10214 if (nanos
>= NANOSEC
) {
10216 timeout
.tv_nsec
= nanos
- NANOSEC
;
10218 timeout
.tv_nsec
= nanos
;
10220 pthread_mutex_lock(&wd
->wd_mutex
);
10221 if (!wd
->wd_should_exit
)
10222 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10224 pthread_mutex_unlock(&wd
->wd_mutex
);
10226 break; /* signaled by main thread */
10227 } else if (ret
!= ETIMEDOUT
) {
10228 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10229 "failed: %s\n"), strerror(ret
));
10231 return (void *)(uintptr_t)(1);
10236 return (void *)(0);
10240 zpool_do_wait(int argc
, char **argv
)
10242 boolean_t verbose
= B_FALSE
;
10246 unsigned long count
;
10247 pthread_t status_thr
;
10249 zpool_handle_t
*zhp
;
10252 wd
.wd_scripted
= B_FALSE
;
10253 wd
.wd_exact
= B_FALSE
;
10254 wd
.wd_headers_once
= B_FALSE
;
10255 wd
.wd_should_exit
= B_FALSE
;
10257 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10258 pthread_cond_init(&wd
.wd_cv
, NULL
);
10260 /* By default, wait for all types of activity. */
10261 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10262 wd
.wd_enabled
[i
] = B_TRUE
;
10264 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10267 wd
.wd_scripted
= B_TRUE
;
10270 wd
.wd_headers_once
= B_TRUE
;
10273 wd
.wd_exact
= B_TRUE
;
10276 get_timestamp_arg(*optarg
);
10280 static char *col_subopts
[] = { "discard", "free",
10281 "initialize", "replace", "remove", "resilver",
10282 "scrub", "trim", NULL
};
10284 /* Reset activities array */
10285 bzero(&wd
.wd_enabled
, sizeof (wd
.wd_enabled
));
10286 while (*optarg
!= '\0') {
10287 int activity
= getsubopt(&optarg
, col_subopts
,
10290 if (activity
< 0) {
10291 (void) fprintf(stderr
,
10292 gettext("invalid activity '%s'\n"),
10297 wd
.wd_enabled
[activity
] = B_TRUE
;
10302 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10311 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10313 /* This subcmd only accepts an interval, not a count */
10314 (void) fprintf(stderr
, gettext("too many arguments\n"));
10318 if (wd
.wd_interval
!= 0)
10322 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10326 (void) fprintf(stderr
, gettext("too many arguments\n"));
10330 wd
.wd_poolname
= argv
[0];
10332 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10337 * We use a separate thread for printing status updates because
10338 * the main thread will call lzc_wait(), which blocks as long
10339 * as an activity is in progress, which can be a long time.
10341 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10343 (void) fprintf(stderr
, gettext("failed to create status"
10344 "thread: %s\n"), strerror(errno
));
10351 * Loop over all activities that we are supposed to wait for until none
10352 * of them are in progress. Note that this means we can end up waiting
10353 * for more activities to complete than just those that were in progress
10354 * when we began waiting; if an activity we are interested in begins
10355 * while we are waiting for another activity, we will wait for both to
10356 * complete before exiting.
10359 boolean_t missing
= B_FALSE
;
10360 boolean_t any_waited
= B_FALSE
;
10362 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10365 if (!wd
.wd_enabled
[i
])
10368 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10369 if (error
!= 0 || missing
)
10372 any_waited
= (any_waited
|| waited
);
10375 if (error
!= 0 || missing
|| !any_waited
)
10383 pthread_mutex_lock(&wd
.wd_mutex
);
10384 wd
.wd_should_exit
= B_TRUE
;
10385 pthread_cond_signal(&wd
.wd_cv
);
10386 pthread_mutex_unlock(&wd
.wd_mutex
);
10387 (void) pthread_join(status_thr
, (void *)&status
);
10392 pthread_mutex_destroy(&wd
.wd_mutex
);
10393 pthread_cond_destroy(&wd
.wd_cv
);
10398 find_command_idx(char *command
, int *idx
)
10402 for (i
= 0; i
< NCOMMAND
; i
++) {
10403 if (command_table
[i
].name
== NULL
)
10406 if (strcmp(command
, command_table
[i
].name
) == 0) {
10415 * Display version message
10418 zpool_do_version(int argc
, char **argv
)
10420 if (zfs_version_print() == -1)
10427 * Do zpool_load_compat() and print error message on failure
10429 static zpool_compat_status_t
10430 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
10432 char badword
[ZFS_MAXPROPLEN
];
10433 char badfile
[MAXPATHLEN
];
10434 zpool_compat_status_t ret
;
10436 switch (ret
= zpool_load_compat(compat
, list
, badword
, badfile
)) {
10437 case ZPOOL_COMPATIBILITY_OK
:
10439 case ZPOOL_COMPATIBILITY_READERR
:
10440 (void) fprintf(stderr
, gettext("error reading compatibility "
10441 "file '%s'\n"), badfile
);
10443 case ZPOOL_COMPATIBILITY_BADFILE
:
10444 (void) fprintf(stderr
, gettext("compatibility file '%s' "
10445 "too large or not newline-terminated\n"), badfile
);
10447 case ZPOOL_COMPATIBILITY_BADWORD
:
10448 (void) fprintf(stderr
, gettext("unknown feature '%s' in "
10449 "compatibility file '%s'\n"), badword
, badfile
);
10451 case ZPOOL_COMPATIBILITY_NOFILES
:
10452 (void) fprintf(stderr
, gettext("no compatibility files "
10460 main(int argc
, char **argv
)
10467 (void) setlocale(LC_ALL
, "");
10468 (void) setlocale(LC_NUMERIC
, "C");
10469 (void) textdomain(TEXT_DOMAIN
);
10475 * Make sure the user has specified some command.
10478 (void) fprintf(stderr
, gettext("missing command\n"));
10485 * Special case '-?'
10487 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10491 * Special case '-V|--version'
10493 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10494 return (zpool_do_version(argc
, argv
));
10496 if ((g_zfs
= libzfs_init()) == NULL
) {
10497 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10501 libzfs_print_on_error(g_zfs
, B_TRUE
);
10503 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10506 * Many commands modify input strings for string parsing reasons.
10507 * We create a copy to protect the original argv.
10509 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10510 for (i
= 0; i
< argc
; i
++)
10511 newargv
[i
] = strdup(argv
[i
]);
10512 newargv
[argc
] = NULL
;
10515 * Run the appropriate command.
10517 if (find_command_idx(cmdname
, &i
) == 0) {
10518 current_command
= &command_table
[i
];
10519 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10520 } else if (strchr(cmdname
, '=')) {
10521 verify(find_command_idx("set", &i
) == 0);
10522 current_command
= &command_table
[i
];
10523 ret
= command_table
[i
].func(argc
, newargv
);
10524 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10526 * 'freeze' is a vile debugging abomination, so we treat
10529 zfs_cmd_t zc
= {"\0"};
10531 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10532 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10534 (void) fprintf(stderr
,
10535 gettext("failed to freeze pool: %d\n"), errno
);
10541 (void) fprintf(stderr
, gettext("unrecognized "
10542 "command '%s'\n"), cmdname
);
10547 for (i
= 0; i
< argc
; i
++)
10551 if (ret
== 0 && log_history
)
10552 (void) zpool_log_history(g_zfs
, history_str
);
10554 libzfs_fini(g_zfs
);
10557 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10558 * for the purposes of running ::findleaks.
10560 if (getenv("ZFS_ABORT") != NULL
) {
10561 (void) printf("dumping core by request\n");