4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
35 * Copyright [2021] Hewlett Packard Enterprise Development LP
59 #include <sys/fs/zfs.h>
61 #include <sys/systeminfo.h>
62 #include <sys/fm/fs/zfs.h>
63 #include <sys/fm/util.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/mount.h>
67 #include <sys/sysmacros.h>
74 #include "zpool_util.h"
75 #include "zfs_comutil.h"
76 #include "zfeature_common.h"
78 #include "statcommon.h"
80 libzfs_handle_t
*g_zfs
;
82 static int zpool_do_create(int, char **);
83 static int zpool_do_destroy(int, char **);
85 static int zpool_do_add(int, char **);
86 static int zpool_do_remove(int, char **);
87 static int zpool_do_labelclear(int, char **);
89 static int zpool_do_checkpoint(int, char **);
91 static int zpool_do_list(int, char **);
92 static int zpool_do_iostat(int, char **);
93 static int zpool_do_status(int, char **);
95 static int zpool_do_online(int, char **);
96 static int zpool_do_offline(int, char **);
97 static int zpool_do_clear(int, char **);
98 static int zpool_do_reopen(int, char **);
100 static int zpool_do_reguid(int, char **);
102 static int zpool_do_attach(int, char **);
103 static int zpool_do_detach(int, char **);
104 static int zpool_do_replace(int, char **);
105 static int zpool_do_split(int, char **);
107 static int zpool_do_initialize(int, char **);
108 static int zpool_do_scrub(int, char **);
109 static int zpool_do_resilver(int, char **);
110 static int zpool_do_trim(int, char **);
112 static int zpool_do_import(int, char **);
113 static int zpool_do_export(int, char **);
115 static int zpool_do_upgrade(int, char **);
117 static int zpool_do_history(int, char **);
118 static int zpool_do_events(int, char **);
120 static int zpool_do_get(int, char **);
121 static int zpool_do_set(int, char **);
123 static int zpool_do_sync(int, char **);
125 static int zpool_do_version(int, char **);
127 static int zpool_do_wait(int, char **);
129 static zpool_compat_status_t
zpool_do_load_compat(
130 const char *, boolean_t
*);
133 * These libumem hooks provide a reasonable set of defaults for the allocator's
134 * debugging facilities.
139 _umem_debug_init(void)
141 return ("default,verbose"); /* $UMEM_DEBUG setting */
145 _umem_logging_init(void)
147 return ("fail,contents"); /* $UMEM_LOGGING setting */
188 * Flags for stats to display with "zpool iostats"
196 IOS_COUNT
, /* always last element */
199 /* iostat_type entries as bitmasks */
200 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
201 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
202 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
203 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
204 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
206 /* Mask of all the histo bits */
207 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
210 * Lookup table for iostat flags to nvlist names. Basically a list
211 * of all the nvlists a flag requires. Also specifies the order in
212 * which data gets printed in zpool iostat.
214 static const char *vsx_type_to_nvlist
[IOS_COUNT
][13] = {
216 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
217 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
218 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
221 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
222 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
225 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
228 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
229 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
230 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
231 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
232 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
235 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
236 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
237 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
238 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
239 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
240 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
243 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
244 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
245 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
246 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
247 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
248 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
249 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
250 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
251 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
252 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
253 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
254 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
260 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
261 * Right now, only one histo bit is ever set at one time, so we can
262 * just do a highbit64(a)
264 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
266 typedef struct zpool_command
{
268 int (*func
)(int, char **);
273 * Master command table. Each ZFS command has a name, associated function, and
274 * usage message. The usage messages need to be internationalized, so we have
275 * to have a function to return the usage message based on a command index.
277 * These commands are organized according to how they are displayed in the usage
278 * message. An empty command (one with a NULL name) indicates an empty line in
279 * the generic usage message.
281 static zpool_command_t command_table
[] = {
282 { "version", zpool_do_version
, HELP_VERSION
},
284 { "create", zpool_do_create
, HELP_CREATE
},
285 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
287 { "add", zpool_do_add
, HELP_ADD
},
288 { "remove", zpool_do_remove
, HELP_REMOVE
},
290 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
292 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
294 { "list", zpool_do_list
, HELP_LIST
},
295 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
296 { "status", zpool_do_status
, HELP_STATUS
},
298 { "online", zpool_do_online
, HELP_ONLINE
},
299 { "offline", zpool_do_offline
, HELP_OFFLINE
},
300 { "clear", zpool_do_clear
, HELP_CLEAR
},
301 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
303 { "attach", zpool_do_attach
, HELP_ATTACH
},
304 { "detach", zpool_do_detach
, HELP_DETACH
},
305 { "replace", zpool_do_replace
, HELP_REPLACE
},
306 { "split", zpool_do_split
, HELP_SPLIT
},
308 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
309 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
310 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
311 { "trim", zpool_do_trim
, HELP_TRIM
},
313 { "import", zpool_do_import
, HELP_IMPORT
},
314 { "export", zpool_do_export
, HELP_EXPORT
},
315 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
316 { "reguid", zpool_do_reguid
, HELP_REGUID
},
318 { "history", zpool_do_history
, HELP_HISTORY
},
319 { "events", zpool_do_events
, HELP_EVENTS
},
321 { "get", zpool_do_get
, HELP_GET
},
322 { "set", zpool_do_set
, HELP_SET
},
323 { "sync", zpool_do_sync
, HELP_SYNC
},
325 { "wait", zpool_do_wait
, HELP_WAIT
},
328 #define NCOMMAND (ARRAY_SIZE(command_table))
330 #define VDEV_ALLOC_CLASS_LOGS "logs"
332 static zpool_command_t
*current_command
;
333 static char history_str
[HIS_MAX_RECORD_LEN
];
334 static boolean_t log_history
= B_TRUE
;
335 static uint_t timestamp_fmt
= NODATE
;
338 get_usage(zpool_help_t idx
)
342 return (gettext("\tadd [-fgLnP] [-o property=value] "
343 "<pool> <vdev> ...\n"));
345 return (gettext("\tattach [-fsw] [-o property=value] "
346 "<pool> <device> <new-device>\n"));
348 return (gettext("\tclear [-nF] <pool> [device]\n"));
350 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
351 "\t [-O file-system-property=value] ... \n"
352 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
353 case HELP_CHECKPOINT
:
354 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
356 return (gettext("\tdestroy [-f] <pool>\n"));
358 return (gettext("\tdetach <pool> <device>\n"));
360 return (gettext("\texport [-af] <pool> ...\n"));
362 return (gettext("\thistory [-il] [<pool>] ...\n"));
364 return (gettext("\timport [-d dir] [-D]\n"
365 "\timport [-o mntopts] [-o property=value] ... \n"
366 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
367 "[-R root] [-F [-n]] -a\n"
368 "\timport [-o mntopts] [-o property=value] ... \n"
369 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
370 "[-R root] [-F [-n]]\n"
371 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
373 return (gettext("\tiostat [[[-c [script1,script2,...]"
374 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
375 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
376 " [[-n] interval [count]]\n"));
377 case HELP_LABELCLEAR
:
378 return (gettext("\tlabelclear [-f] <vdev>\n"));
380 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
381 "[-T d|u] [pool] ... \n"
382 "\t [interval [count]]\n"));
384 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
386 return (gettext("\tonline [-e] <pool> <device> ...\n"));
388 return (gettext("\treplace [-fsw] [-o property=value] "
389 "<pool> <device> [new-device]\n"));
391 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
393 return (gettext("\treopen [-n] <pool>\n"));
394 case HELP_INITIALIZE
:
395 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
396 "[<device> ...]\n"));
398 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
400 return (gettext("\tresilver <pool> ...\n"));
402 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
403 "[<device> ...]\n"));
405 return (gettext("\tstatus [-c [script1,script2,...]] "
406 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
407 "\t [interval [count]]\n"));
409 return (gettext("\tupgrade\n"
411 "\tupgrade [-V version] <-a | pool ...>\n"));
413 return (gettext("\tevents [-vHf [pool] | -c]\n"));
415 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
416 "<\"all\" | property[,...]> <pool> ...\n"));
418 return (gettext("\tset <property=value> <pool> \n"));
420 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
421 "\t [-o property=value] <pool> <newpool> "
422 "[<device> ...]\n"));
424 return (gettext("\treguid <pool>\n"));
426 return (gettext("\tsync [pool] ...\n"));
428 return (gettext("\tversion\n"));
430 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
431 "<pool> [interval]\n"));
439 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
445 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
449 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
452 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
453 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
454 fnvlist_add_boolean(res
, path
);
460 for (i
= 0; i
< children
; i
++) {
461 zpool_collect_leaves(zhp
, child
[i
], res
);
466 * Callback routine that will print out a pool property value.
469 print_prop_cb(int prop
, void *cb
)
473 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
475 if (zpool_prop_readonly(prop
))
476 (void) fprintf(fp
, " NO ");
478 (void) fprintf(fp
, " YES ");
480 if (zpool_prop_values(prop
) == NULL
)
481 (void) fprintf(fp
, "-\n");
483 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
489 * Display usage message. If we're inside a command, display only the usage for
490 * that command. Otherwise, iterate over the entire command table and display
491 * a complete usage message.
494 usage(boolean_t requested
)
496 FILE *fp
= requested
? stdout
: stderr
;
498 if (current_command
== NULL
) {
501 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
503 gettext("where 'command' is one of the following:\n\n"));
505 for (i
= 0; i
< NCOMMAND
; i
++) {
506 if (command_table
[i
].name
== NULL
)
507 (void) fprintf(fp
, "\n");
509 (void) fprintf(fp
, "%s",
510 get_usage(command_table
[i
].usage
));
513 (void) fprintf(fp
, gettext("usage:\n"));
514 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
517 if (current_command
!= NULL
&&
518 ((strcmp(current_command
->name
, "set") == 0) ||
519 (strcmp(current_command
->name
, "get") == 0) ||
520 (strcmp(current_command
->name
, "list") == 0))) {
523 gettext("\nthe following properties are supported:\n"));
525 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
526 "PROPERTY", "EDIT", "VALUES");
528 /* Iterate over all properties */
529 (void) zprop_iter(print_prop_cb
, fp
, B_FALSE
, B_TRUE
,
532 (void) fprintf(fp
, "\t%-19s ", "feature@...");
533 (void) fprintf(fp
, "YES disabled | enabled | active\n");
535 (void) fprintf(fp
, gettext("\nThe feature@ properties must be "
536 "appended with a feature name.\nSee zpool-features(5).\n"));
540 * See comments at end of main().
542 if (getenv("ZFS_ABORT") != NULL
) {
543 (void) printf("dumping core by request\n");
547 exit(requested
? 0 : 2);
551 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
552 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
555 * -c Cancel. Ends active initializing.
556 * -s Suspend. Initializing can then be restarted with no flags.
557 * -w Wait. Blocks until initializing has completed.
560 zpool_do_initialize(int argc
, char **argv
)
567 boolean_t wait
= B_FALSE
;
569 struct option long_options
[] = {
570 {"cancel", no_argument
, NULL
, 'c'},
571 {"suspend", no_argument
, NULL
, 's'},
572 {"wait", no_argument
, NULL
, 'w'},
576 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
577 while ((c
= getopt_long(argc
, argv
, "csw", long_options
, NULL
)) != -1) {
580 if (cmd_type
!= POOL_INITIALIZE_START
&&
581 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
582 (void) fprintf(stderr
, gettext("-c cannot be "
583 "combined with other options\n"));
586 cmd_type
= POOL_INITIALIZE_CANCEL
;
589 if (cmd_type
!= POOL_INITIALIZE_START
&&
590 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
591 (void) fprintf(stderr
, gettext("-s cannot be "
592 "combined with other options\n"));
595 cmd_type
= POOL_INITIALIZE_SUSPEND
;
602 (void) fprintf(stderr
,
603 gettext("invalid option '%c'\n"), optopt
);
605 (void) fprintf(stderr
,
606 gettext("invalid option '%s'\n"),
617 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
622 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
623 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
629 zhp
= zpool_open(g_zfs
, poolname
);
633 vdevs
= fnvlist_alloc();
635 /* no individual leaf vdevs specified, so add them all */
636 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
637 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
638 ZPOOL_CONFIG_VDEV_TREE
);
639 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
641 for (int i
= 1; i
< argc
; i
++) {
642 fnvlist_add_boolean(vdevs
, argv
[i
]);
647 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
649 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
658 * print a pool vdev config for dry runs
661 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
662 const char *match
, int name_flags
)
667 boolean_t printed
= B_FALSE
;
669 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
670 &child
, &children
) != 0) {
672 (void) printf("\t%*s%s\n", indent
, "", name
);
676 for (c
= 0; c
< children
; c
++) {
677 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
680 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
683 if (is_hole
== B_TRUE
) {
687 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
690 class = VDEV_ALLOC_BIAS_LOG
;
691 (void) nvlist_lookup_string(child
[c
],
692 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
693 if (strcmp(match
, class) != 0)
696 if (!printed
&& name
!= NULL
) {
697 (void) printf("\t%*s%s\n", indent
, "", name
);
700 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
701 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
708 * Print the list of l2cache devices for dry runs.
711 print_cache_list(nvlist_t
*nv
, int indent
)
716 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
717 &child
, &children
) == 0 && children
> 0) {
718 (void) printf("\t%*s%s\n", indent
, "", "cache");
722 for (c
= 0; c
< children
; c
++) {
725 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
726 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
732 * Print the list of spares for dry runs.
735 print_spare_list(nvlist_t
*nv
, int indent
)
740 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
741 &child
, &children
) == 0 && children
> 0) {
742 (void) printf("\t%*s%s\n", indent
, "", "spares");
746 for (c
= 0; c
< children
; c
++) {
749 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
750 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
756 prop_list_contains_feature(nvlist_t
*proplist
)
759 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
760 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
761 if (zpool_prop_feature(nvpair_name(nvp
)))
768 * Add a property pair (name, string-value) into a property nvlist.
771 add_prop_list(const char *propname
, char *propval
, nvlist_t
**props
,
774 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
779 if (*props
== NULL
&&
780 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
781 (void) fprintf(stderr
,
782 gettext("internal error: out of memory\n"));
789 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
791 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
793 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
794 !zpool_prop_feature(propname
)) {
795 (void) fprintf(stderr
, gettext("property '%s' is "
796 "not a valid pool property\n"), propname
);
801 * feature@ properties and version should not be specified
804 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
805 nvlist_exists(proplist
, vname
)) ||
806 (prop
== ZPOOL_PROP_VERSION
&&
807 prop_list_contains_feature(proplist
))) {
808 (void) fprintf(stderr
, gettext("'feature@' and "
809 "'version' properties cannot be specified "
815 * if version is specified, only "legacy" compatibility
818 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
819 strcmp(propval
, ZPOOL_COMPAT_LEGACY
) != 0 &&
820 nvlist_exists(proplist
, vname
)) ||
821 (prop
== ZPOOL_PROP_VERSION
&&
822 nvlist_exists(proplist
, cname
) &&
823 strcmp(fnvlist_lookup_string(proplist
, cname
),
824 ZPOOL_COMPAT_LEGACY
) != 0)) {
825 (void) fprintf(stderr
, gettext("when 'version' is "
826 "specified, the 'compatibility' feature may only "
827 "be set to '" ZPOOL_COMPAT_LEGACY
"'\n"));
831 if (zpool_prop_feature(propname
))
834 normnm
= zpool_prop_to_name(prop
);
836 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
838 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
840 normnm
= zfs_prop_to_name(fsprop
);
841 } else if (zfs_prop_user(propname
) ||
842 zfs_prop_userquota(propname
)) {
845 (void) fprintf(stderr
, gettext("property '%s' is "
846 "not a valid filesystem property\n"), propname
);
851 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
852 prop
!= ZPOOL_PROP_CACHEFILE
) {
853 (void) fprintf(stderr
, gettext("property '%s' "
854 "specified multiple times\n"), propname
);
858 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
859 (void) fprintf(stderr
, gettext("internal "
860 "error: out of memory\n"));
868 * Set a default property pair (name, string-value) in a property nvlist
871 add_prop_list_default(const char *propname
, char *propval
, nvlist_t
**props
,
876 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
879 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
883 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
885 * -f Force addition of devices, even if they appear in use
886 * -g Display guid for individual vdev name.
887 * -L Follow links when resolving vdev path name.
888 * -n Do not add the devices, but display the resulting layout if
889 * they were to be added.
890 * -o Set property=value.
891 * -P Display full path for vdev name.
893 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
894 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
898 zpool_do_add(int argc
, char **argv
)
900 boolean_t force
= B_FALSE
;
901 boolean_t dryrun
= B_FALSE
;
909 nvlist_t
*props
= NULL
;
913 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
919 name_flags
|= VDEV_NAME_GUID
;
922 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
928 if ((propval
= strchr(optarg
, '=')) == NULL
) {
929 (void) fprintf(stderr
, gettext("missing "
930 "'=' for -o option\n"));
936 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
937 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
941 name_flags
|= VDEV_NAME_PATH
;
944 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
953 /* get pool name and check number of arguments */
955 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
959 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
968 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
971 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
972 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
978 /* unless manually specified use "ashift" pool property (if set) */
979 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
982 char strval
[ZPOOL_MAXPROPLEN
];
984 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
985 if (src
!= ZPROP_SRC_DEFAULT
) {
986 (void) sprintf(strval
, "%" PRId32
, intval
);
987 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
988 &props
, B_TRUE
) == 0);
992 /* pass off to make_root_vdev for processing */
993 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
995 if (nvroot
== NULL
) {
1001 nvlist_t
*poolnvroot
;
1002 nvlist_t
**l2child
, **sparechild
;
1003 uint_t l2children
, sparechildren
, c
;
1005 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1007 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1010 (void) printf(gettext("would update '%s' to the following "
1011 "configuration:\n\n"), zpool_get_name(zhp
));
1013 /* print original main pool and new tree */
1014 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1015 name_flags
| VDEV_NAME_TYPE_ID
);
1016 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1018 /* print other classes: 'dedup', 'special', and 'log' */
1019 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1020 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1021 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1022 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1023 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1024 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1025 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1026 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1029 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1030 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1031 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1032 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1033 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1034 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1035 print_vdev_tree(zhp
, "special", nvroot
, 0,
1036 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1039 if (num_logs(poolnvroot
) > 0) {
1040 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1041 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1042 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1043 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1044 } else if (num_logs(nvroot
) > 0) {
1045 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1046 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1049 /* Do the same for the caches */
1050 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1051 &l2child
, &l2children
) == 0 && l2children
) {
1053 (void) printf(gettext("\tcache\n"));
1054 for (c
= 0; c
< l2children
; c
++) {
1055 vname
= zpool_vdev_name(g_zfs
, NULL
,
1056 l2child
[c
], name_flags
);
1057 (void) printf("\t %s\n", vname
);
1061 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1062 &l2child
, &l2children
) == 0 && l2children
) {
1064 (void) printf(gettext("\tcache\n"));
1065 for (c
= 0; c
< l2children
; c
++) {
1066 vname
= zpool_vdev_name(g_zfs
, NULL
,
1067 l2child
[c
], name_flags
);
1068 (void) printf("\t %s\n", vname
);
1072 /* And finally the spares */
1073 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1074 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1076 (void) printf(gettext("\tspares\n"));
1077 for (c
= 0; c
< sparechildren
; c
++) {
1078 vname
= zpool_vdev_name(g_zfs
, NULL
,
1079 sparechild
[c
], name_flags
);
1080 (void) printf("\t %s\n", vname
);
1084 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1085 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1087 (void) printf(gettext("\tspares\n"));
1088 for (c
= 0; c
< sparechildren
; c
++) {
1089 vname
= zpool_vdev_name(g_zfs
, NULL
,
1090 sparechild
[c
], name_flags
);
1091 (void) printf("\t %s\n", vname
);
1098 ret
= (zpool_add(zhp
, nvroot
) != 0);
1102 nvlist_free(nvroot
);
1109 * zpool remove [-npsw] <pool> <vdev> ...
1111 * Removes the given vdev from the pool.
1114 zpool_do_remove(int argc
, char **argv
)
1118 zpool_handle_t
*zhp
= NULL
;
1119 boolean_t stop
= B_FALSE
;
1121 boolean_t noop
= B_FALSE
;
1122 boolean_t parsable
= B_FALSE
;
1123 boolean_t wait
= B_FALSE
;
1126 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1141 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1150 /* get pool name and check number of arguments */
1152 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1158 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1162 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1168 (void) fprintf(stderr
, gettext("too many arguments\n"));
1171 if (zpool_vdev_remove_cancel(zhp
) != 0)
1174 (void) fprintf(stderr
, gettext("invalid option "
1175 "combination: -w cannot be used with -s\n"));
1180 (void) fprintf(stderr
, gettext("missing device\n"));
1184 for (i
= 1; i
< argc
; i
++) {
1188 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1194 (void) printf("%s %llu\n",
1195 argv
[i
], (unsigned long long)size
);
1198 zfs_nicenum(size
, valstr
,
1200 (void) printf("Memory that will be "
1201 "used after removing %s: %s\n",
1205 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1210 if (ret
== 0 && wait
)
1211 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1219 * zpool labelclear [-f] <vdev>
1221 * -f Force clearing the label for the vdevs which are members of
1222 * the exported or foreign pools.
1224 * Verifies that the vdev is not active and zeros out the label information
1228 zpool_do_labelclear(int argc
, char **argv
)
1230 char vdev
[MAXPATHLEN
];
1233 int c
, fd
= -1, ret
= 0;
1236 boolean_t inuse
= B_FALSE
;
1237 boolean_t force
= B_FALSE
;
1240 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1246 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1257 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1261 (void) fprintf(stderr
, gettext("too many arguments\n"));
1266 * Check if we were given absolute path and use it as is.
1267 * Otherwise if the provided vdev name doesn't point to a file,
1268 * try prepending expected disk paths and partition numbers.
1270 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1271 if (vdev
[0] != '/' && stat(vdev
, &st
) != 0) {
1274 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1275 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1276 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1280 if (error
|| (stat(vdev
, &st
) != 0)) {
1281 (void) fprintf(stderr
, gettext(
1282 "failed to find device %s, try specifying absolute "
1283 "path instead\n"), argv
[0]);
1288 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1289 (void) fprintf(stderr
, gettext("failed to open %s: %s\n"),
1290 vdev
, strerror(errno
));
1295 * Flush all dirty pages for the block device. This should not be
1296 * fatal when the device does not support BLKFLSBUF as would be the
1297 * case for a file vdev.
1299 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1300 (void) fprintf(stderr
, gettext("failed to invalidate "
1301 "cache for %s: %s\n"), vdev
, strerror(errno
));
1303 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1304 (void) fprintf(stderr
,
1305 gettext("failed to read label from %s\n"), vdev
);
1309 nvlist_free(config
);
1311 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1313 (void) fprintf(stderr
,
1314 gettext("failed to check state for %s\n"), vdev
);
1324 case POOL_STATE_ACTIVE
:
1325 case POOL_STATE_SPARE
:
1326 case POOL_STATE_L2CACHE
:
1327 (void) fprintf(stderr
, gettext(
1328 "%s is a member (%s) of pool \"%s\"\n"),
1329 vdev
, zpool_pool_state_to_name(state
), name
);
1333 case POOL_STATE_EXPORTED
:
1336 (void) fprintf(stderr
, gettext(
1337 "use '-f' to override the following error:\n"
1338 "%s is a member of exported pool \"%s\"\n"),
1343 case POOL_STATE_POTENTIALLY_ACTIVE
:
1346 (void) fprintf(stderr
, gettext(
1347 "use '-f' to override the following error:\n"
1348 "%s is a member of potentially active pool \"%s\"\n"),
1353 case POOL_STATE_DESTROYED
:
1354 /* inuse should never be set for a destroyed pool */
1360 ret
= zpool_clear_label(fd
);
1362 (void) fprintf(stderr
,
1363 gettext("failed to clear label for %s\n"), vdev
);
1374 * zpool create [-fnd] [-o property=value] ...
1375 * [-O file-system-property=value] ...
1376 * [-R root] [-m mountpoint] <pool> <dev> ...
1378 * -f Force creation, even if devices appear in use
1379 * -n Do not create the pool, but display the resulting layout if it
1380 * were to be created.
1381 * -R Create a pool under an alternate root
1382 * -m Set default mountpoint for the root dataset. By default it's
1384 * -o Set property=value.
1385 * -o Set feature@feature=enabled|disabled.
1386 * -d Don't automatically enable all supported pool features
1387 * (individual features can be enabled with -o).
1388 * -O Set fsproperty=value in the pool's root file system
1390 * Creates the named pool according to the given vdev specification. The
1391 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1392 * Once we get the nvlist back from make_root_vdev(), we either print out the
1393 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1396 zpool_do_create(int argc
, char **argv
)
1398 boolean_t force
= B_FALSE
;
1399 boolean_t dryrun
= B_FALSE
;
1400 boolean_t enable_pool_features
= B_TRUE
;
1403 nvlist_t
*nvroot
= NULL
;
1407 char *altroot
= NULL
;
1408 char *compat
= NULL
;
1409 char *mountpoint
= NULL
;
1410 nvlist_t
*fsprops
= NULL
;
1411 nvlist_t
*props
= NULL
;
1415 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1424 enable_pool_features
= B_FALSE
;
1428 if (add_prop_list(zpool_prop_to_name(
1429 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1431 if (add_prop_list_default(zpool_prop_to_name(
1432 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1436 /* Equivalent to -O mountpoint=optarg */
1437 mountpoint
= optarg
;
1440 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1441 (void) fprintf(stderr
, gettext("missing "
1442 "'=' for -o option\n"));
1448 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1452 * If the user is creating a pool that doesn't support
1453 * feature flags, don't enable any features.
1455 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1459 ver
= strtoull(propval
, &end
, 10);
1461 ver
< SPA_VERSION_FEATURES
) {
1462 enable_pool_features
= B_FALSE
;
1465 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1467 if (zpool_name_to_prop(optarg
) ==
1468 ZPOOL_PROP_COMPATIBILITY
)
1472 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1473 (void) fprintf(stderr
, gettext("missing "
1474 "'=' for -O option\n"));
1481 * Mountpoints are checked and then added later.
1482 * Uniquely among properties, they can be specified
1483 * more than once, to avoid conflict with -m.
1485 if (0 == strcmp(optarg
,
1486 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1487 mountpoint
= propval
;
1488 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1495 * Sanity check temporary pool name.
1497 if (strchr(optarg
, '/') != NULL
) {
1498 (void) fprintf(stderr
, gettext("cannot create "
1499 "'%s': invalid character '/' in temporary "
1501 (void) fprintf(stderr
, gettext("use 'zfs "
1502 "create' to create a dataset\n"));
1506 if (add_prop_list(zpool_prop_to_name(
1507 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1509 if (add_prop_list_default(zpool_prop_to_name(
1510 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
1515 (void) fprintf(stderr
, gettext("missing argument for "
1516 "'%c' option\n"), optopt
);
1519 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1528 /* get pool name and check number of arguments */
1530 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1534 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1541 * As a special case, check for use of '/' in the name, and direct the
1542 * user to use 'zfs create' instead.
1544 if (strchr(poolname
, '/') != NULL
) {
1545 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1546 "character '/' in pool name\n"), poolname
);
1547 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1548 "create a dataset\n"));
1552 /* pass off to make_root_vdev for bulk processing */
1553 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1554 argc
- 1, argv
+ 1);
1558 /* make_root_vdev() allows 0 toplevel children if there are spares */
1559 if (!zfs_allocatable_devs(nvroot
)) {
1560 (void) fprintf(stderr
, gettext("invalid vdev "
1561 "specification: at least one toplevel vdev must be "
1566 if (altroot
!= NULL
&& altroot
[0] != '/') {
1567 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1568 "must be an absolute path\n"), altroot
);
1573 * Check the validity of the mountpoint and direct the user to use the
1574 * '-m' mountpoint option if it looks like its in use.
1576 if (mountpoint
== NULL
||
1577 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1578 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1579 char buf
[MAXPATHLEN
];
1582 if (mountpoint
&& mountpoint
[0] != '/') {
1583 (void) fprintf(stderr
, gettext("invalid mountpoint "
1584 "'%s': must be an absolute path, 'legacy', or "
1585 "'none'\n"), mountpoint
);
1589 if (mountpoint
== NULL
) {
1590 if (altroot
!= NULL
)
1591 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1594 (void) snprintf(buf
, sizeof (buf
), "/%s",
1597 if (altroot
!= NULL
)
1598 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1599 altroot
, mountpoint
);
1601 (void) snprintf(buf
, sizeof (buf
), "%s",
1605 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1606 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1607 "%s\n"), buf
, strerror(errno
));
1608 (void) fprintf(stderr
, gettext("use '-m' "
1609 "option to provide a different default\n"));
1614 while (count
< 3 && readdir(dirp
) != NULL
)
1616 (void) closedir(dirp
);
1619 (void) fprintf(stderr
, gettext("mountpoint "
1620 "'%s' exists and is not empty\n"), buf
);
1621 (void) fprintf(stderr
, gettext("use '-m' "
1622 "option to provide a "
1623 "different default\n"));
1630 * Now that the mountpoint's validity has been checked, ensure that
1631 * the property is set appropriately prior to creating the pool.
1633 if (mountpoint
!= NULL
) {
1634 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1635 mountpoint
, &fsprops
, B_FALSE
);
1643 * For a dry run invocation, print out a basic message and run
1644 * through all the vdevs in the list and print out in an
1645 * appropriate hierarchy.
1647 (void) printf(gettext("would create '%s' with the "
1648 "following layout:\n\n"), poolname
);
1650 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1651 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1652 VDEV_ALLOC_BIAS_DEDUP
, 0);
1653 print_vdev_tree(NULL
, "special", nvroot
, 0,
1654 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1655 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1656 VDEV_ALLOC_BIAS_LOG
, 0);
1657 print_cache_list(nvroot
, 0);
1658 print_spare_list(nvroot
, 0);
1663 * Load in feature set.
1664 * Note: if compatibility property not given, we'll have
1665 * NULL, which means 'all features'.
1667 boolean_t requested_features
[SPA_FEATURES
];
1668 if (zpool_do_load_compat(compat
, requested_features
) !=
1669 ZPOOL_COMPATIBILITY_OK
)
1673 * props contains list of features to enable.
1675 * - remove it if feature@name=disabled
1676 * - leave it there if feature@name=enabled
1678 * - enable_pool_features (ie: no '-d' or '-o version')
1679 * - it's supported by the kernel module
1680 * - it's in the requested feature set
1681 * - warn if it's enabled but not in compat
1683 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1684 char propname
[MAXPATHLEN
];
1686 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1688 (void) snprintf(propname
, sizeof (propname
),
1689 "feature@%s", feat
->fi_uname
);
1691 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1692 if (strcmp(propval
, ZFS_FEATURE_DISABLED
) == 0)
1693 (void) nvlist_remove_all(props
,
1696 ZFS_FEATURE_ENABLED
) == 0 &&
1697 !requested_features
[i
])
1698 (void) fprintf(stderr
, gettext(
1699 "Warning: feature \"%s\" enabled "
1700 "but is not in specified "
1701 "'compatibility' feature set.\n"),
1704 enable_pool_features
&&
1705 feat
->fi_zfs_mod_supported
&&
1706 requested_features
[i
]) {
1707 ret
= add_prop_list(propname
,
1708 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1715 if (zpool_create(g_zfs
, poolname
,
1716 nvroot
, props
, fsprops
) == 0) {
1717 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1718 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1720 if (zfs_mount(pool
, NULL
, 0) == 0) {
1721 ret
= zfs_shareall(pool
);
1722 zfs_commit_all_shares();
1726 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1727 (void) fprintf(stderr
, gettext("pool name may have "
1733 nvlist_free(nvroot
);
1734 nvlist_free(fsprops
);
1738 nvlist_free(fsprops
);
1745 * zpool destroy <pool>
1747 * -f Forcefully unmount any datasets
1749 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1752 zpool_do_destroy(int argc
, char **argv
)
1754 boolean_t force
= B_FALSE
;
1757 zpool_handle_t
*zhp
;
1761 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1767 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1776 /* check arguments */
1778 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1782 (void) fprintf(stderr
, gettext("too many arguments\n"));
1788 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1790 * As a special case, check for use of '/' in the name, and
1791 * direct the user to use 'zfs destroy' instead.
1793 if (strchr(pool
, '/') != NULL
)
1794 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1795 "destroy a dataset\n"));
1799 if (zpool_disable_datasets(zhp
, force
) != 0) {
1800 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1801 "could not unmount datasets\n"), zpool_get_name(zhp
));
1806 /* The history must be logged as part of the export */
1807 log_history
= B_FALSE
;
1809 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1816 typedef struct export_cbdata
{
1818 boolean_t hardforce
;
1825 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1827 export_cbdata_t
*cb
= data
;
1829 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
1832 /* The history must be logged as part of the export */
1833 log_history
= B_FALSE
;
1835 if (cb
->hardforce
) {
1836 if (zpool_export_force(zhp
, history_str
) != 0)
1838 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
1846 * zpool export [-f] <pool> ...
1848 * -a Export all pools
1849 * -f Forcefully unmount datasets
1851 * Export the given pools. By default, the command will attempt to cleanly
1852 * unmount any active datasets within the pool. If the '-f' flag is specified,
1853 * then the datasets will be forcefully unmounted.
1856 zpool_do_export(int argc
, char **argv
)
1859 boolean_t do_all
= B_FALSE
;
1860 boolean_t force
= B_FALSE
;
1861 boolean_t hardforce
= B_FALSE
;
1865 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
1877 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1884 cb
.hardforce
= hardforce
;
1890 (void) fprintf(stderr
, gettext("too many arguments\n"));
1894 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
1895 B_FALSE
, zpool_export_one
, &cb
));
1898 /* check arguments */
1900 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1904 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
, zpool_export_one
,
1911 * Given a vdev configuration, determine the maximum width needed for the device
1915 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
1923 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
1924 if (strlen(name
) + depth
> max
)
1925 max
= strlen(name
) + depth
;
1929 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1930 &child
, &children
) == 0) {
1931 for (c
= 0; c
< children
; c
++)
1932 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1933 max
, name_flags
)) > max
)
1937 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1938 &child
, &children
) == 0) {
1939 for (c
= 0; c
< children
; c
++)
1940 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1941 max
, name_flags
)) > max
)
1945 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1946 &child
, &children
) == 0) {
1947 for (c
= 0; c
< children
; c
++)
1948 if ((ret
= max_width(zhp
, child
[c
], depth
+ 2,
1949 max
, name_flags
)) > max
)
1956 typedef struct spare_cbdata
{
1958 zpool_handle_t
*cb_zhp
;
1962 find_vdev(nvlist_t
*nv
, uint64_t search
)
1968 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
1972 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1973 &child
, &children
) == 0) {
1974 for (c
= 0; c
< children
; c
++)
1975 if (find_vdev(child
[c
], search
))
1983 find_spare(zpool_handle_t
*zhp
, void *data
)
1985 spare_cbdata_t
*cbp
= data
;
1986 nvlist_t
*config
, *nvroot
;
1988 config
= zpool_get_config(zhp
, NULL
);
1989 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1992 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
2001 typedef struct status_cbdata
{
2005 boolean_t cb_allpools
;
2006 boolean_t cb_verbose
;
2007 boolean_t cb_literal
;
2008 boolean_t cb_explain
;
2010 boolean_t cb_dedup_stats
;
2011 boolean_t cb_print_status
;
2012 boolean_t cb_print_slow_ios
;
2013 boolean_t cb_print_vdev_init
;
2014 boolean_t cb_print_vdev_trim
;
2015 vdev_cmd_data_list_t
*vcdl
;
2018 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2020 is_blank_str(char *str
)
2022 while (str
!= NULL
&& *str
!= '\0') {
2030 /* Print command output lines for specific vdev in a specific pool */
2032 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, char *path
)
2034 vdev_cmd_data_t
*data
;
2038 for (i
= 0; i
< vcdl
->count
; i
++) {
2039 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2040 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2041 /* Not the vdev we're looking for */
2045 data
= &vcdl
->data
[i
];
2046 /* Print out all the output values for this vdev */
2047 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2049 /* Does this vdev have values for this column? */
2050 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2051 if (strcmp(data
->cols
[k
],
2052 vcdl
->uniq_cols
[j
]) == 0) {
2053 /* yes it does, record the value */
2054 val
= data
->lines
[k
];
2059 * Mark empty values with dashes to make output
2062 if (val
== NULL
|| is_blank_str(val
))
2065 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2066 if (j
< vcdl
->uniq_cols_cnt
- 1)
2070 /* Print out any values that aren't in a column at the end */
2071 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2072 /* Did we have any columns? If so print a spacer. */
2073 if (vcdl
->uniq_cols_cnt
> 0)
2076 val
= data
->lines
[j
];
2077 printf("%s", val
? val
: "");
2084 * Print vdev initialization status for leaves
2087 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2090 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2091 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2092 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2093 !vs
->vs_scan_removing
) {
2096 struct tm zaction_ts
;
2098 time_t t
= vs
->vs_initialize_action_time
;
2099 int initialize_pct
= 100;
2100 if (vs
->vs_initialize_state
!=
2101 VDEV_INITIALIZE_COMPLETE
) {
2102 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2103 100 / (vs
->vs_initialize_bytes_est
+ 1));
2106 (void) localtime_r(&t
, &zaction_ts
);
2107 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2109 switch (vs
->vs_initialize_state
) {
2110 case VDEV_INITIALIZE_SUSPENDED
:
2111 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2112 gettext("suspended, started at"), tbuf
);
2114 case VDEV_INITIALIZE_ACTIVE
:
2115 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2116 gettext("started at"), tbuf
);
2118 case VDEV_INITIALIZE_COMPLETE
:
2119 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2120 gettext("completed at"), tbuf
);
2124 (void) printf(gettext(" (%d%% initialized%s)"),
2125 initialize_pct
, zbuf
);
2127 (void) printf(gettext(" (uninitialized)"));
2129 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2130 (void) printf(gettext(" (initializing)"));
2135 * Print vdev TRIM status for leaves
2138 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2141 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2142 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2143 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2144 !vs
->vs_scan_removing
) {
2147 struct tm zaction_ts
;
2149 time_t t
= vs
->vs_trim_action_time
;
2151 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2152 trim_pct
= (vs
->vs_trim_bytes_done
*
2153 100 / (vs
->vs_trim_bytes_est
+ 1));
2156 (void) localtime_r(&t
, &zaction_ts
);
2157 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2159 switch (vs
->vs_trim_state
) {
2160 case VDEV_TRIM_SUSPENDED
:
2161 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2162 gettext("suspended, started at"), tbuf
);
2164 case VDEV_TRIM_ACTIVE
:
2165 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2166 gettext("started at"), tbuf
);
2168 case VDEV_TRIM_COMPLETE
:
2169 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2170 gettext("completed at"), tbuf
);
2174 (void) printf(gettext(" (%d%% trimmed%s)"),
2176 } else if (vs
->vs_trim_notsup
) {
2177 (void) printf(gettext(" (trim unsupported)"));
2179 (void) printf(gettext(" (untrimmed)"));
2181 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2182 (void) printf(gettext(" (trimming)"));
2187 * Return the color associated with a health string. This includes returning
2188 * NULL for no color change.
2191 health_str_to_color(const char *health
)
2193 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2194 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2195 strcmp(health
, gettext("UNAVAIL")) == 0) {
2199 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2200 strcmp(health
, gettext("DEGRADED")) == 0 ||
2201 strcmp(health
, gettext("REMOVED")) == 0) {
2202 return (ANSI_YELLOW
);
2209 * Print out configuration state as requested by status_callback.
2212 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2213 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2215 nvlist_t
**child
, *root
;
2216 uint_t c
, i
, vsc
, children
;
2217 pool_scan_stat_t
*ps
= NULL
;
2219 char rbuf
[6], wbuf
[6], cbuf
[6];
2221 uint64_t notpresent
;
2222 spare_cbdata_t spare_cb
;
2226 char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
;
2228 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2229 &child
, &children
) != 0)
2232 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2233 (uint64_t **)&vs
, &vsc
) == 0);
2235 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2237 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2240 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2244 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2247 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2248 state
= gettext("INUSE");
2249 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2250 state
= gettext("AVAIL");
2253 printf_color(health_str_to_color(state
),
2254 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2258 if (vs
->vs_read_errors
)
2261 if (vs
->vs_write_errors
)
2264 if (vs
->vs_checksum_errors
)
2267 if (cb
->cb_literal
) {
2269 printf_color(rcolor
, "%5llu",
2270 (u_longlong_t
)vs
->vs_read_errors
);
2272 printf_color(wcolor
, "%5llu",
2273 (u_longlong_t
)vs
->vs_write_errors
);
2275 printf_color(ccolor
, "%5llu",
2276 (u_longlong_t
)vs
->vs_checksum_errors
);
2278 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2279 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2280 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2283 printf_color(rcolor
, "%5s", rbuf
);
2285 printf_color(wcolor
, "%5s", wbuf
);
2287 printf_color(ccolor
, "%5s", cbuf
);
2289 if (cb
->cb_print_slow_ios
) {
2290 if (children
== 0) {
2291 /* Only leafs vdevs have slow IOs */
2292 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2295 snprintf(rbuf
, sizeof (rbuf
), "-");
2299 printf(" %5llu", (u_longlong_t
)vs
->vs_slow_ios
);
2301 printf(" %5s", rbuf
);
2305 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2306 ¬present
) == 0) {
2307 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2308 (void) printf(" %s %s", gettext("was"), path
);
2309 } else if (vs
->vs_aux
!= 0) {
2311 color_start(ANSI_RED
);
2312 switch (vs
->vs_aux
) {
2313 case VDEV_AUX_OPEN_FAILED
:
2314 (void) printf(gettext("cannot open"));
2317 case VDEV_AUX_BAD_GUID_SUM
:
2318 (void) printf(gettext("missing device"));
2321 case VDEV_AUX_NO_REPLICAS
:
2322 (void) printf(gettext("insufficient replicas"));
2325 case VDEV_AUX_VERSION_NEWER
:
2326 (void) printf(gettext("newer version"));
2329 case VDEV_AUX_UNSUP_FEAT
:
2330 (void) printf(gettext("unsupported feature(s)"));
2333 case VDEV_AUX_ASHIFT_TOO_BIG
:
2334 (void) printf(gettext("unsupported minimum blocksize"));
2337 case VDEV_AUX_SPARED
:
2338 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2339 &spare_cb
.cb_guid
) == 0);
2340 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2341 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2342 zpool_get_name(zhp
)) == 0)
2343 (void) printf(gettext("currently in "
2346 (void) printf(gettext("in use by "
2348 zpool_get_name(spare_cb
.cb_zhp
));
2349 zpool_close(spare_cb
.cb_zhp
);
2351 (void) printf(gettext("currently in use"));
2355 case VDEV_AUX_ERR_EXCEEDED
:
2356 (void) printf(gettext("too many errors"));
2359 case VDEV_AUX_IO_FAILURE
:
2360 (void) printf(gettext("experienced I/O failures"));
2363 case VDEV_AUX_BAD_LOG
:
2364 (void) printf(gettext("bad intent log"));
2367 case VDEV_AUX_EXTERNAL
:
2368 (void) printf(gettext("external device fault"));
2371 case VDEV_AUX_SPLIT_POOL
:
2372 (void) printf(gettext("split into new pool"));
2375 case VDEV_AUX_ACTIVE
:
2376 (void) printf(gettext("currently in use"));
2379 case VDEV_AUX_CHILDREN_OFFLINE
:
2380 (void) printf(gettext("all children offline"));
2383 case VDEV_AUX_BAD_LABEL
:
2384 (void) printf(gettext("invalid label"));
2388 (void) printf(gettext("corrupted data"));
2392 } else if (children
== 0 && !isspare
&&
2393 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2394 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2395 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2397 gettext(" block size: %dB configured, %dB native"),
2398 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2401 /* The root vdev has the scrub/resilver stats */
2402 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2403 ZPOOL_CONFIG_VDEV_TREE
);
2404 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2405 (uint64_t **)&ps
, &c
);
2407 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0) {
2408 if (vs
->vs_scan_processed
!= 0) {
2409 (void) printf(gettext(" (%s)"),
2410 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2411 "resilvering" : "repairing");
2412 } else if (vs
->vs_resilver_deferred
) {
2413 (void) printf(gettext(" (awaiting resilver)"));
2417 /* The top-level vdevs have the rebuild stats */
2418 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2420 if (vs
->vs_rebuild_processed
!= 0) {
2421 (void) printf(gettext(" (resilvering)"));
2425 if (cb
->vcdl
!= NULL
) {
2426 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2428 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2432 /* Display vdev initialization and trim status for leaves. */
2433 if (children
== 0) {
2434 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2435 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2438 (void) printf("\n");
2440 for (c
= 0; c
< children
; c
++) {
2441 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2443 /* Don't print logs or holes here */
2444 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2446 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2448 if (islog
|| ishole
)
2450 /* Only print normal classes here */
2451 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2454 /* Provide vdev_rebuild_stats to children if available */
2456 (void) nvlist_lookup_uint64_array(nv
,
2457 ZPOOL_CONFIG_REBUILD_STATS
,
2458 (uint64_t **)&vrs
, &i
);
2461 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2462 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2463 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2470 * Print the configuration of an exported pool. Iterate over all vdevs in the
2471 * pool, printing out the name and status for each one.
2474 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2482 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2483 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2484 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2487 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2488 (uint64_t **)&vs
, &c
) == 0);
2490 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2491 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2493 if (vs
->vs_aux
!= 0) {
2496 switch (vs
->vs_aux
) {
2497 case VDEV_AUX_OPEN_FAILED
:
2498 (void) printf(gettext("cannot open"));
2501 case VDEV_AUX_BAD_GUID_SUM
:
2502 (void) printf(gettext("missing device"));
2505 case VDEV_AUX_NO_REPLICAS
:
2506 (void) printf(gettext("insufficient replicas"));
2509 case VDEV_AUX_VERSION_NEWER
:
2510 (void) printf(gettext("newer version"));
2513 case VDEV_AUX_UNSUP_FEAT
:
2514 (void) printf(gettext("unsupported feature(s)"));
2517 case VDEV_AUX_ERR_EXCEEDED
:
2518 (void) printf(gettext("too many errors"));
2521 case VDEV_AUX_ACTIVE
:
2522 (void) printf(gettext("currently in use"));
2525 case VDEV_AUX_CHILDREN_OFFLINE
:
2526 (void) printf(gettext("all children offline"));
2529 case VDEV_AUX_BAD_LABEL
:
2530 (void) printf(gettext("invalid label"));
2534 (void) printf(gettext("corrupted data"));
2538 (void) printf("\n");
2540 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2541 &child
, &children
) != 0)
2544 for (c
= 0; c
< children
; c
++) {
2545 uint64_t is_log
= B_FALSE
;
2547 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2551 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2554 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2555 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2556 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2560 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2561 &child
, &children
) == 0) {
2562 (void) printf(gettext("\tcache\n"));
2563 for (c
= 0; c
< children
; c
++) {
2564 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2566 (void) printf("\t %s\n", vname
);
2571 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2572 &child
, &children
) == 0) {
2573 (void) printf(gettext("\tspares\n"));
2574 for (c
= 0; c
< children
; c
++) {
2575 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2577 (void) printf("\t %s\n", vname
);
2584 * Print specialized class vdevs.
2586 * These are recorded as top level vdevs in the main pool child array
2587 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2588 * print_status_config() or print_import_config() to print the top level
2589 * class vdevs then any of their children (eg mirrored slogs) are printed
2590 * recursively - which works because only the top level vdev is marked.
2593 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2598 boolean_t printed
= B_FALSE
;
2600 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2602 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2606 for (c
= 0; c
< children
; c
++) {
2607 uint64_t is_log
= B_FALSE
;
2611 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2615 bias
= VDEV_ALLOC_CLASS_LOGS
;
2617 (void) nvlist_lookup_string(child
[c
],
2618 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2619 (void) nvlist_lookup_string(child
[c
],
2620 ZPOOL_CONFIG_TYPE
, &type
);
2623 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2625 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2629 (void) printf("\t%s\t\n", gettext(class));
2633 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2634 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2635 if (cb
->cb_print_status
)
2636 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2639 print_import_config(cb
, name
, child
[c
], 2);
2645 * Display the status for the given pool.
2648 show_import(nvlist_t
*config
, boolean_t report_error
)
2650 uint64_t pool_state
;
2654 uint64_t hostid
= 0;
2656 char *hostname
= "unknown";
2657 nvlist_t
*nvroot
, *nvinfo
;
2658 zpool_status_t reason
;
2659 zpool_errata_t errata
;
2663 status_cbdata_t cb
= { 0 };
2665 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2667 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2669 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2671 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2674 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2675 (uint64_t **)&vs
, &vsc
) == 0);
2676 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2678 reason
= zpool_import_status(config
, &msgid
, &errata
);
2681 * If we're importing using a cachefile, then we won't report any
2682 * errors unless we are in the scan phase of the import.
2684 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
2687 (void) printf(gettext(" pool: %s\n"), name
);
2688 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2689 (void) printf(gettext(" state: %s"), health
);
2690 if (pool_state
== POOL_STATE_DESTROYED
)
2691 (void) printf(gettext(" (DESTROYED)"));
2692 (void) printf("\n");
2695 case ZPOOL_STATUS_MISSING_DEV_R
:
2696 case ZPOOL_STATUS_MISSING_DEV_NR
:
2697 case ZPOOL_STATUS_BAD_GUID_SUM
:
2698 printf_color(ANSI_BOLD
, gettext("status: "));
2699 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2700 "missing from the system.\n"));
2703 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2704 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2705 printf_color(ANSI_BOLD
, gettext("status: "));
2706 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2707 " corrupted data.\n"));
2710 case ZPOOL_STATUS_CORRUPT_DATA
:
2712 gettext(" status: The pool data is corrupted.\n"));
2715 case ZPOOL_STATUS_OFFLINE_DEV
:
2716 printf_color(ANSI_BOLD
, gettext("status: "));
2717 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2718 "are offlined.\n"));
2721 case ZPOOL_STATUS_CORRUPT_POOL
:
2722 printf_color(ANSI_BOLD
, gettext("status: "));
2723 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2727 case ZPOOL_STATUS_VERSION_OLDER
:
2728 printf_color(ANSI_BOLD
, gettext("status: "));
2729 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2730 "a legacy on-disk version.\n"));
2733 case ZPOOL_STATUS_VERSION_NEWER
:
2734 printf_color(ANSI_BOLD
, gettext("status: "));
2735 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2736 "an incompatible version.\n"));
2739 case ZPOOL_STATUS_FEAT_DISABLED
:
2740 printf_color(ANSI_BOLD
, gettext("status: "));
2741 printf_color(ANSI_YELLOW
, gettext("Some supported "
2742 "features are not enabled on the pool.\n\t"
2743 "(Note that they may be intentionally disabled "
2744 "if the\n\t'compatibility' property is set.)\n"));
2747 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2748 printf_color(ANSI_BOLD
, gettext("status: "));
2749 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2750 "the file(s) indicated by the 'compatibility'\n"
2754 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
2755 printf_color(ANSI_BOLD
, gettext("status: "));
2756 printf_color(ANSI_YELLOW
, gettext("One or more features "
2757 "are enabled on the pool despite not being\n"
2758 "requested by the 'compatibility' property.\n"));
2761 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2762 printf_color(ANSI_BOLD
, gettext("status: "));
2763 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
2764 "feature(s) not supported on this system:\n"));
2765 color_start(ANSI_YELLOW
);
2766 zpool_print_unsup_feat(config
);
2770 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2771 printf_color(ANSI_BOLD
, gettext("status: "));
2772 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
2773 "accessed in read-only mode on this system. It\n\tcannot be"
2774 " accessed in read-write mode because it uses the "
2775 "following\n\tfeature(s) not supported on this system:\n"));
2776 color_start(ANSI_YELLOW
);
2777 zpool_print_unsup_feat(config
);
2781 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2782 printf_color(ANSI_BOLD
, gettext("status: "));
2783 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
2784 "imported by another system.\n"));
2787 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2788 printf_color(ANSI_BOLD
, gettext("status: "));
2789 printf_color(ANSI_YELLOW
, gettext("The pool has the "
2790 "multihost property on. It cannot\n\tbe safely imported "
2791 "when the system hostid is not set.\n"));
2794 case ZPOOL_STATUS_HOSTID_MISMATCH
:
2795 printf_color(ANSI_BOLD
, gettext("status: "));
2796 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
2797 "by another system.\n"));
2800 case ZPOOL_STATUS_FAULTED_DEV_R
:
2801 case ZPOOL_STATUS_FAULTED_DEV_NR
:
2802 printf_color(ANSI_BOLD
, gettext("status: "));
2803 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2807 case ZPOOL_STATUS_BAD_LOG
:
2808 printf_color(ANSI_BOLD
, gettext("status: "));
2809 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
2813 case ZPOOL_STATUS_RESILVERING
:
2814 case ZPOOL_STATUS_REBUILDING
:
2815 printf_color(ANSI_BOLD
, gettext("status: "));
2816 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
2817 "being resilvered.\n"));
2820 case ZPOOL_STATUS_ERRATA
:
2821 printf_color(ANSI_BOLD
, gettext("status: "));
2822 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
2826 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
2827 printf_color(ANSI_BOLD
, gettext("status: "));
2828 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2829 "configured to use a non-native block size.\n"
2830 "\tExpect reduced performance.\n"));
2835 * No other status can be seen when importing pools.
2837 assert(reason
== ZPOOL_STATUS_OK
);
2841 * Print out an action according to the overall state of the pool.
2843 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2844 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
2845 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
2846 (void) printf(gettext(" action: The pool can be "
2847 "imported using its name or numeric identifier, "
2848 "though\n\tsome features will not be available "
2849 "without an explicit 'zpool upgrade'.\n"));
2850 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
2851 (void) printf(gettext(" action: The pool can be "
2852 "imported using its name or numeric\n\tidentifier, "
2853 "though the file(s) indicated by its "
2854 "'compatibility'\n\tproperty cannot be parsed at "
2856 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
2857 (void) printf(gettext(" action: The pool can be "
2858 "imported using its name or numeric "
2859 "identifier and\n\tthe '-f' flag.\n"));
2860 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
2862 case ZPOOL_ERRATA_NONE
:
2865 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
2866 (void) printf(gettext(" action: The pool can "
2867 "be imported using its name or numeric "
2868 "identifier,\n\thowever there is a compat"
2869 "ibility issue which should be corrected"
2870 "\n\tby running 'zpool scrub'\n"));
2873 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
2874 (void) printf(gettext(" action: The pool can"
2875 "not be imported with this version of ZFS "
2876 "due to\n\tan active asynchronous destroy. "
2877 "Revert to an earlier version\n\tand "
2878 "allow the destroy to complete before "
2882 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
2883 (void) printf(gettext(" action: Existing "
2884 "encrypted datasets contain an on-disk "
2885 "incompatibility, which\n\tneeds to be "
2886 "corrected. Backup these datasets to new "
2887 "encrypted datasets\n\tand destroy the "
2891 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
2892 (void) printf(gettext(" action: Existing "
2893 "encrypted snapshots and bookmarks contain "
2894 "an on-disk\n\tincompatibility. This may "
2895 "cause on-disk corruption if they are used"
2896 "\n\twith 'zfs recv'. To correct the "
2897 "issue, enable the bookmark_v2 feature.\n\t"
2898 "No additional action is needed if there "
2899 "are no encrypted snapshots or\n\t"
2900 "bookmarks. If preserving the encrypted "
2901 "snapshots and bookmarks is\n\trequired, "
2902 "use a non-raw send to backup and restore "
2903 "them. Alternately,\n\tthey may be removed"
2904 " to resolve the incompatibility.\n"));
2908 * All errata must contain an action message.
2913 (void) printf(gettext(" action: The pool can be "
2914 "imported using its name or numeric "
2917 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
2918 (void) printf(gettext(" action: The pool can be imported "
2919 "despite missing or damaged devices. The\n\tfault "
2920 "tolerance of the pool may be compromised if imported.\n"));
2923 case ZPOOL_STATUS_VERSION_NEWER
:
2924 (void) printf(gettext(" action: The pool cannot be "
2925 "imported. Access the pool on a system running "
2926 "newer\n\tsoftware, or recreate the pool from "
2929 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2930 printf_color(ANSI_BOLD
, gettext("action: "));
2931 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2932 "imported. Access the pool on a system that "
2933 "supports\n\tthe required feature(s), or recreate "
2934 "the pool from backup.\n"));
2936 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
2937 printf_color(ANSI_BOLD
, gettext("action: "));
2938 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
2939 "imported in read-write mode. Import the pool "
2941 "\t\"-o readonly=on\", access the pool on a system "
2942 "that supports the\n\trequired feature(s), or "
2943 "recreate the pool from backup.\n"));
2945 case ZPOOL_STATUS_MISSING_DEV_R
:
2946 case ZPOOL_STATUS_MISSING_DEV_NR
:
2947 case ZPOOL_STATUS_BAD_GUID_SUM
:
2948 (void) printf(gettext(" action: The pool cannot be "
2949 "imported. Attach the missing\n\tdevices and try "
2952 case ZPOOL_STATUS_HOSTID_ACTIVE
:
2953 VERIFY0(nvlist_lookup_nvlist(config
,
2954 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
2956 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
2957 hostname
= fnvlist_lookup_string(nvinfo
,
2958 ZPOOL_CONFIG_MMP_HOSTNAME
);
2960 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
2961 hostid
= fnvlist_lookup_uint64(nvinfo
,
2962 ZPOOL_CONFIG_MMP_HOSTID
);
2964 (void) printf(gettext(" action: The pool must be "
2965 "exported from %s (hostid=%lx)\n\tbefore it "
2966 "can be safely imported.\n"), hostname
,
2967 (unsigned long) hostid
);
2969 case ZPOOL_STATUS_HOSTID_REQUIRED
:
2970 (void) printf(gettext(" action: Set a unique system "
2971 "hostid with the zgenhostid(8) command.\n"));
2974 (void) printf(gettext(" action: The pool cannot be "
2975 "imported due to damaged devices or data.\n"));
2979 /* Print the comment attached to the pool. */
2980 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2981 (void) printf(gettext("comment: %s\n"), comment
);
2984 * If the state is "closed" or "can't open", and the aux state
2985 * is "corrupt data":
2987 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
2988 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
2989 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
2990 if (pool_state
== POOL_STATE_DESTROYED
)
2991 (void) printf(gettext("\tThe pool was destroyed, "
2992 "but can be imported using the '-Df' flags.\n"));
2993 else if (pool_state
!= POOL_STATE_EXPORTED
)
2994 (void) printf(gettext("\tThe pool may be active on "
2995 "another system, but can be imported using\n\t"
2996 "the '-f' flag.\n"));
2999 if (msgid
!= NULL
) {
3000 (void) printf(gettext(
3001 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3005 (void) printf(gettext(" config:\n\n"));
3007 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
3009 if (cb
.cb_namewidth
< 10)
3010 cb
.cb_namewidth
= 10;
3012 print_import_config(&cb
, name
, nvroot
, 0);
3014 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
3015 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
3016 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
3018 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
3019 (void) printf(gettext("\n\tAdditional devices are known to "
3020 "be part of this pool, though their\n\texact "
3021 "configuration cannot be determined.\n"));
3027 zfs_force_import_required(nvlist_t
*config
)
3030 uint64_t hostid
= 0;
3033 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
3034 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
3036 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3039 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3040 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3041 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3042 ZPOOL_CONFIG_MMP_STATE
);
3044 if (mmp_state
!= MMP_STATE_INACTIVE
)
3052 * Perform the import for the given configuration. This passes the heavy
3053 * lifting off to zpool_import_props(), and then mounts the datasets contained
3057 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3058 nvlist_t
*props
, int flags
)
3061 zpool_handle_t
*zhp
;
3065 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3066 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3068 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3069 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3070 "is formatted using an unsupported ZFS version\n"), name
);
3072 } else if (zfs_force_import_required(config
) &&
3073 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3074 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3077 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3078 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3079 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3080 ZPOOL_CONFIG_MMP_STATE
);
3082 if (mmp_state
== MMP_STATE_ACTIVE
) {
3083 char *hostname
= "<unknown>";
3084 uint64_t hostid
= 0;
3086 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3087 hostname
= fnvlist_lookup_string(nvinfo
,
3088 ZPOOL_CONFIG_MMP_HOSTNAME
);
3090 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3091 hostid
= fnvlist_lookup_uint64(nvinfo
,
3092 ZPOOL_CONFIG_MMP_HOSTID
);
3094 (void) fprintf(stderr
, gettext("cannot import '%s': "
3095 "pool is imported on %s (hostid: "
3096 "0x%lx)\nExport the pool on the other system, "
3097 "then run 'zpool import'.\n"),
3098 name
, hostname
, (unsigned long) hostid
);
3099 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3100 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3101 "pool has the multihost property on and the\n"
3102 "system's hostid is not set. Set a unique hostid "
3103 "with the zgenhostid(8) command.\n"), name
);
3105 char *hostname
= "<unknown>";
3106 uint64_t timestamp
= 0;
3107 uint64_t hostid
= 0;
3109 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3110 hostname
= fnvlist_lookup_string(config
,
3111 ZPOOL_CONFIG_HOSTNAME
);
3113 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3114 timestamp
= fnvlist_lookup_uint64(config
,
3115 ZPOOL_CONFIG_TIMESTAMP
);
3117 if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3118 hostid
= fnvlist_lookup_uint64(config
,
3119 ZPOOL_CONFIG_HOSTID
);
3121 (void) fprintf(stderr
, gettext("cannot import '%s': "
3122 "pool was previously in use from another system.\n"
3123 "Last accessed by %s (hostid=%lx) at %s"
3124 "The pool can be imported, use 'zpool import -f' "
3125 "to import the pool.\n"), name
, hostname
,
3126 (unsigned long)hostid
, ctime((time_t *)×tamp
));
3132 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3135 if (newname
!= NULL
)
3136 name
= (char *)newname
;
3138 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3142 * Loading keys is best effort. We don't want to return immediately
3143 * if it fails but we do want to give the error to the caller.
3145 if (flags
& ZFS_IMPORT_LOAD_KEYS
) {
3146 ret
= zfs_crypto_attempt_load_keys(g_zfs
, name
);
3151 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3152 !(flags
& ZFS_IMPORT_ONLY
) &&
3153 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
3163 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3164 char *orig_name
, char *new_name
,
3165 boolean_t do_destroyed
, boolean_t pool_specified
, boolean_t do_all
,
3166 importargs_t
*import
)
3168 nvlist_t
*config
= NULL
;
3169 nvlist_t
*found_config
= NULL
;
3170 uint64_t pool_state
;
3173 * At this point we have a list of import candidate configs. Even if
3174 * we were searching by pool name or guid, we still need to
3175 * post-process the list to deal with pool state and possible
3179 nvpair_t
*elem
= NULL
;
3180 boolean_t first
= B_TRUE
;
3181 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3183 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3185 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3187 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3189 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3192 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3193 import
->policy
) == 0);
3195 if (!pool_specified
) {
3199 (void) printf("\n");
3202 err
|= do_import(config
, NULL
, mntopts
,
3206 * If we're importing from cachefile, then
3207 * we don't want to report errors until we
3208 * are in the scan phase of the import. If
3209 * we get an error, then we return that error
3210 * to invoke the scan phase.
3212 if (import
->cachefile
&& !import
->scan
)
3213 err
= show_import(config
, B_FALSE
);
3215 (void) show_import(config
, B_TRUE
);
3217 } else if (import
->poolname
!= NULL
) {
3221 * We are searching for a pool based on name.
3223 verify(nvlist_lookup_string(config
,
3224 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3226 if (strcmp(name
, import
->poolname
) == 0) {
3227 if (found_config
!= NULL
) {
3228 (void) fprintf(stderr
, gettext(
3229 "cannot import '%s': more than "
3230 "one matching pool\n"),
3232 (void) fprintf(stderr
, gettext(
3233 "import by numeric ID instead\n"));
3236 found_config
= config
;
3242 * Search for a pool by guid.
3244 verify(nvlist_lookup_uint64(config
,
3245 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3247 if (guid
== import
->guid
)
3248 found_config
= config
;
3253 * If we were searching for a specific pool, verify that we found a
3254 * pool, and then do the import.
3256 if (pool_specified
&& err
== 0) {
3257 if (found_config
== NULL
) {
3258 (void) fprintf(stderr
, gettext("cannot import '%s': "
3259 "no such pool available\n"), orig_name
);
3262 err
|= do_import(found_config
, new_name
,
3263 mntopts
, props
, flags
);
3268 * If we were just looking for pools, report an error if none were
3271 if (!pool_specified
&& first
)
3272 (void) fprintf(stderr
,
3273 gettext("no pools available to import\n"));
3277 typedef struct target_exists_args
{
3278 const char *poolname
;
3280 } target_exists_args_t
;
3283 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3285 target_exists_args_t
*args
= data
;
3286 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3292 if (args
->poolname
!= NULL
) {
3295 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3297 if (strcmp(pool_name
, args
->poolname
) == 0)
3302 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3304 if (pool_guid
== args
->poolguid
)
3312 * zpool checkpoint <pool>
3313 * checkpoint --discard <pool>
3315 * -d Discard the checkpoint from a checkpointed
3318 * -w Wait for discarding a checkpoint to complete.
3321 * Checkpoints the specified pool, by taking a "snapshot" of its
3322 * current state. A pool can only have one checkpoint at a time.
3325 zpool_do_checkpoint(int argc
, char **argv
)
3327 boolean_t discard
, wait
;
3329 zpool_handle_t
*zhp
;
3332 struct option long_options
[] = {
3333 {"discard", no_argument
, NULL
, 'd'},
3334 {"wait", no_argument
, NULL
, 'w'},
3340 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3349 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3355 if (wait
&& !discard
) {
3356 (void) fprintf(stderr
, gettext("--wait only valid when "
3357 "--discard also specified\n"));
3365 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3370 (void) fprintf(stderr
, gettext("too many arguments\n"));
3376 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3377 /* As a special case, check for use of '/' in the name */
3378 if (strchr(pool
, '/') != NULL
)
3379 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3380 "doesn't work on datasets. To save the state "
3381 "of a dataset from a specific point in time "
3382 "please use 'zfs snapshot'\n"));
3387 err
= (zpool_discard_checkpoint(zhp
) != 0);
3388 if (err
== 0 && wait
)
3389 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3391 err
= (zpool_checkpoint(zhp
) != 0);
3399 #define CHECKPOINT_OPT 1024
3402 * zpool import [-d dir] [-D]
3403 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3404 * [-d dir | -c cachefile | -s] [-f] -a
3405 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3406 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3409 * -c Read pool information from a cachefile instead of searching
3410 * devices. If importing from a cachefile config fails, then
3411 * fallback to searching for devices only in the directories that
3412 * exist in the cachefile.
3414 * -d Scan in a specific directory, other than /dev/. More than
3415 * one directory can be specified using multiple '-d' options.
3417 * -D Scan for previously destroyed pools or import all or only
3418 * specified destroyed pools.
3420 * -R Temporarily import the pool, with all mountpoints relative to
3421 * the given root. The pool will remain exported when the machine
3424 * -V Import even in the presence of faulted vdevs. This is an
3425 * intentionally undocumented option for testing purposes, and
3426 * treats the pool configuration as complete, leaving any bad
3427 * vdevs in the FAULTED state. In other words, it does verbatim
3430 * -f Force import, even if it appears that the pool is active.
3432 * -F Attempt rewind if necessary.
3434 * -n See if rewind would work, but don't actually rewind.
3436 * -N Import the pool but don't mount datasets.
3438 * -T Specify a starting txg to use for import. This option is
3439 * intentionally undocumented option for testing purposes.
3441 * -a Import all pools found.
3443 * -l Load encryption keys while importing.
3445 * -o Set property=value and/or temporary mount options (without '=').
3447 * -s Scan using the default search path, the libblkid cache will
3450 * --rewind-to-checkpoint
3451 * Import the pool and revert back to the checkpoint.
3453 * The import command scans for pools to import, and import pools based on pool
3454 * name and GUID. The pool can also be renamed as part of the import process.
3457 zpool_do_import(int argc
, char **argv
)
3459 char **searchdirs
= NULL
;
3460 char *env
, *envdup
= NULL
;
3464 nvlist_t
*pools
= NULL
;
3465 boolean_t do_all
= B_FALSE
;
3466 boolean_t do_destroyed
= B_FALSE
;
3467 char *mntopts
= NULL
;
3468 uint64_t searchguid
= 0;
3469 char *searchname
= NULL
;
3471 nvlist_t
*policy
= NULL
;
3472 nvlist_t
*props
= NULL
;
3473 int flags
= ZFS_IMPORT_NORMAL
;
3474 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3475 boolean_t dryrun
= B_FALSE
;
3476 boolean_t do_rewind
= B_FALSE
;
3477 boolean_t xtreme_rewind
= B_FALSE
;
3478 boolean_t do_scan
= B_FALSE
;
3479 boolean_t pool_exists
= B_FALSE
;
3480 boolean_t pool_specified
= B_FALSE
;
3481 uint64_t txg
= -1ULL;
3482 char *cachefile
= NULL
;
3483 importargs_t idata
= { 0 };
3486 struct option long_options
[] = {
3487 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3492 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3493 long_options
, NULL
)) != -1) {
3502 if (searchdirs
== NULL
) {
3503 searchdirs
= safe_malloc(sizeof (char *));
3505 char **tmp
= safe_malloc((nsearch
+ 1) *
3507 bcopy(searchdirs
, tmp
, nsearch
*
3512 searchdirs
[nsearch
++] = optarg
;
3515 do_destroyed
= B_TRUE
;
3518 flags
|= ZFS_IMPORT_ANY_HOST
;
3524 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3527 flags
|= ZFS_IMPORT_MISSING_LOG
;
3533 flags
|= ZFS_IMPORT_ONLY
;
3536 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3539 if (add_prop_list(optarg
, propval
,
3547 if (add_prop_list(zpool_prop_to_name(
3548 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3550 if (add_prop_list_default(zpool_prop_to_name(
3551 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3558 flags
|= ZFS_IMPORT_TEMP_NAME
;
3559 if (add_prop_list_default(zpool_prop_to_name(
3560 ZPOOL_PROP_CACHEFILE
), "none", &props
, B_TRUE
))
3566 txg
= strtoull(optarg
, &endptr
, 0);
3567 if (errno
!= 0 || *endptr
!= '\0') {
3568 (void) fprintf(stderr
,
3569 gettext("invalid txg value\n"));
3572 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3575 flags
|= ZFS_IMPORT_VERBATIM
;
3578 xtreme_rewind
= B_TRUE
;
3580 case CHECKPOINT_OPT
:
3581 flags
|= ZFS_IMPORT_CHECKPOINT
;
3584 (void) fprintf(stderr
, gettext("missing argument for "
3585 "'%c' option\n"), optopt
);
3589 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3598 if (cachefile
&& nsearch
!= 0) {
3599 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3603 if (cachefile
&& do_scan
) {
3604 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
3608 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3609 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3613 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3614 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3619 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3620 (void) fprintf(stderr
,
3621 gettext("-n or -X only meaningful with -F\n"));
3625 rewind_policy
= ZPOOL_TRY_REWIND
;
3627 rewind_policy
= ZPOOL_DO_REWIND
;
3629 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3631 /* In the future, we can capture further policy and include it here */
3632 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3633 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3634 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3635 rewind_policy
) != 0)
3638 /* check argument count */
3641 (void) fprintf(stderr
, gettext("too many arguments\n"));
3646 (void) fprintf(stderr
, gettext("too many arguments\n"));
3652 * Check for the effective uid. We do this explicitly here because
3653 * otherwise any attempt to discover pools will silently fail.
3655 if (argc
== 0 && geteuid() != 0) {
3656 (void) fprintf(stderr
, gettext("cannot "
3657 "discover pools: permission denied\n"));
3658 if (searchdirs
!= NULL
)
3662 nvlist_free(policy
);
3667 * Depending on the arguments given, we do one of the following:
3669 * <none> Iterate through all pools and display information about
3672 * -a Iterate through all pools and try to import each one.
3674 * <id> Find the pool that corresponds to the given GUID/pool
3675 * name and import that one.
3677 * -D Above options applies only to destroyed pools.
3683 searchguid
= strtoull(argv
[0], &endptr
, 10);
3684 if (errno
!= 0 || *endptr
!= '\0') {
3685 searchname
= argv
[0];
3688 pool_specified
= B_TRUE
;
3691 * User specified a name or guid. Ensure it's unique.
3693 target_exists_args_t search
= {searchname
, searchguid
};
3694 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3698 * Check the environment for the preferred search path.
3700 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3703 envdup
= strdup(env
);
3705 dir
= strtok(envdup
, ":");
3706 while (dir
!= NULL
) {
3707 if (searchdirs
== NULL
) {
3708 searchdirs
= safe_malloc(sizeof (char *));
3710 char **tmp
= safe_malloc((nsearch
+ 1) *
3712 bcopy(searchdirs
, tmp
, nsearch
*
3717 searchdirs
[nsearch
++] = dir
;
3718 dir
= strtok(NULL
, ":");
3722 idata
.path
= searchdirs
;
3723 idata
.paths
= nsearch
;
3724 idata
.poolname
= searchname
;
3725 idata
.guid
= searchguid
;
3726 idata
.cachefile
= cachefile
;
3727 idata
.scan
= do_scan
;
3728 idata
.policy
= policy
;
3730 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3732 if (pools
!= NULL
&& pool_exists
&&
3733 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3734 (void) fprintf(stderr
, gettext("cannot import '%s': "
3735 "a pool with that name already exists\n"),
3737 (void) fprintf(stderr
, gettext("use the form '%s "
3738 "<pool | id> <newpool>' to give it a new name\n"),
3741 } else if (pools
== NULL
&& pool_exists
) {
3742 (void) fprintf(stderr
, gettext("cannot import '%s': "
3743 "a pool with that name is already created/imported,\n"),
3745 (void) fprintf(stderr
, gettext("and no additional pools "
3746 "with that name were found\n"));
3748 } else if (pools
== NULL
) {
3750 (void) fprintf(stderr
, gettext("cannot import '%s': "
3751 "no such pool available\n"), argv
[0]);
3757 if (searchdirs
!= NULL
)
3761 nvlist_free(policy
);
3767 err
= import_pools(pools
, props
, mntopts
, flags
, argv
[0],
3768 argc
== 1 ? NULL
: argv
[1], do_destroyed
, pool_specified
,
3772 * If we're using the cachefile and we failed to import, then
3773 * fallback to scanning the directory for pools that match
3774 * those in the cachefile.
3776 if (err
!= 0 && cachefile
!= NULL
) {
3777 (void) printf(gettext("cachefile import failed, retrying\n"));
3780 * We use the scan flag to gather the directories that exist
3781 * in the cachefile. If we need to fallback to searching for
3782 * the pool config, we will only search devices in these
3785 idata
.scan
= B_TRUE
;
3787 pools
= zpool_search_import(g_zfs
, &idata
, &libzfs_config_ops
);
3789 err
= import_pools(pools
, props
, mntopts
, flags
, argv
[0],
3790 argc
== 1 ? NULL
: argv
[1], do_destroyed
, pool_specified
,
3797 nvlist_free(policy
);
3798 if (searchdirs
!= NULL
)
3803 return (err
? 1 : 0);
3807 * zpool sync [-f] [pool] ...
3809 * -f (undocumented) force uberblock (and config including zpool cache file)
3812 * Sync the specified pool(s).
3813 * Without arguments "zpool sync" will sync all pools.
3814 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3818 zpool_do_sync(int argc
, char **argv
)
3821 boolean_t force
= B_FALSE
;
3824 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
3830 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3839 /* if argc == 0 we will execute zpool_sync_one on all pools */
3840 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
, zpool_sync_one
,
3846 typedef struct iostat_cbdata
{
3851 char **cb_vdev_names
; /* Only show these vdevs */
3852 unsigned int cb_vdev_names_count
;
3853 boolean_t cb_verbose
;
3854 boolean_t cb_literal
;
3855 boolean_t cb_scripted
;
3856 zpool_list_t
*cb_list
;
3857 vdev_cmd_data_list_t
*vcdl
;
3861 typedef struct name_and_columns
{
3862 const char *name
; /* Column name */
3863 unsigned int columns
; /* Center name to this number of columns */
3864 } name_and_columns_t
;
3866 #define IOSTAT_MAX_LABELS 13 /* Max number of labels on one line */
3868 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
3870 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3872 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3873 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {NULL
}},
3874 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
3875 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3876 {"trimq_write", 2}, {NULL
}},
3877 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3878 {"asyncq_wait", 2}, {NULL
}},
3879 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
3880 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3881 {"trim", 2}, {NULL
}},
3884 /* Shorthand - if "columns" field not set, default to 1 column */
3885 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
3887 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3889 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3890 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {NULL
}},
3891 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3892 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3893 {"pend"}, {"activ"}, {NULL
}},
3894 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3895 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {NULL
}},
3896 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3897 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"}, {NULL
}},
3900 static const char *histo_to_title
[] = {
3901 [IOS_L_HISTO
] = "latency",
3902 [IOS_RQ_HISTO
] = "req_size",
3906 * Return the number of labels in a null-terminated name_and_columns_t
3911 label_array_len(const name_and_columns_t
*labels
)
3915 while (labels
[i
].name
)
3922 * Return the number of strings in a null-terminated string array.
3925 * const char foo[] = {"bar", "baz", NULL}
3930 str_array_len(const char *array
[])
3941 * Return a default column width for default/latency/queue columns. This does
3942 * not include histograms, which have their columns autosized.
3945 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
3947 unsigned long column_width
= 5; /* Normal niceprint */
3948 static unsigned long widths
[] = {
3950 * Choose some sane default column sizes for printing the
3953 [IOS_DEFAULT
] = 15, /* 1PB capacity */
3954 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
3955 [IOS_QUEUES
] = 6, /* 1M queue entries */
3956 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
3957 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
3961 column_width
= widths
[type
];
3963 return (column_width
);
3967 * Print the column labels, i.e:
3969 * capacity operations bandwidth
3970 * alloc free read write read write ...
3972 * If force_column_width is set, use it for the column width. If not set, use
3973 * the default column width.
3976 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
3977 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
3980 int text_start
, rw_column_width
, spaces_to_end
;
3981 uint64_t flags
= cb
->cb_flags
;
3983 unsigned int column_width
= force_column_width
;
3985 /* For each bit set in flags */
3986 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
3987 idx
= lowbit64(f
) - 1;
3988 if (!force_column_width
)
3989 column_width
= default_column_width(cb
, idx
);
3990 /* Print our top labels centered over "read write" label. */
3991 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
3992 const char *name
= labels
[idx
][i
].name
;
3994 * We treat labels[][].columns == 0 as shorthand
3995 * for one column. It makes writing out the label
3996 * tables more concise.
3998 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
3999 unsigned int slen
= strlen(name
);
4001 rw_column_width
= (column_width
* columns
) +
4002 (2 * (columns
- 1));
4004 text_start
= (int)((rw_column_width
) / columns
-
4009 printf(" "); /* Two spaces between columns */
4011 /* Space from beginning of column to label */
4012 for (s
= 0; s
< text_start
; s
++)
4017 /* Print space after label to end of column */
4018 spaces_to_end
= rw_column_width
- text_start
- slen
;
4019 if (spaces_to_end
< 0)
4022 for (s
= 0; s
< spaces_to_end
; s
++)
4030 * print_cmd_columns - Print custom column titles from -c
4032 * If the user specified the "zpool status|iostat -c" then print their custom
4033 * column titles in the header. For example, print_cmd_columns() would print
4034 * the " col1 col2" part of this:
4036 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4038 * capacity operations bandwidth
4039 * pool alloc free read write read write col1 col2
4040 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4041 * mypool 269K 1008M 0 0 107 946
4042 * mirror 269K 1008M 0 0 107 946
4043 * sdb - - 0 0 102 473 val1 val2
4044 * sdc - - 0 0 5 473 val1 val2
4045 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4048 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4051 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4053 if (vcdl
->count
== 0 || data
== NULL
)
4057 * Each vdev cmd should have the same column names unless the user did
4058 * something weird with their cmd. Just take the column names from the
4059 * first vdev and assume it works for all of them.
4061 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4064 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4067 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4068 vcdl
->uniq_cols
[i
]);
4075 * Utility function to print out a line of dashes like:
4077 * -------------------------------- ----- ----- ----- ----- -----
4079 * ...or a dashed named-row line like:
4085 * @force_column_width If non-zero, use the value as the column width.
4086 * Otherwise use the default column widths.
4088 * @name: Print a dashed named-row line starting
4089 * with @name. Otherwise, print a regular
4093 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4097 unsigned int namewidth
;
4098 uint64_t flags
= cb
->cb_flags
;
4101 const name_and_columns_t
*labels
;
4105 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4106 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4107 } else if (cb
->cb_vdev_names_count
) {
4113 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4114 name
? strlen(name
) : 0);
4118 printf("%-*s", namewidth
, name
);
4120 for (i
= 0; i
< namewidth
; i
++)
4124 /* For each bit in flags */
4125 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4126 unsigned int column_width
;
4127 idx
= lowbit64(f
) - 1;
4128 if (force_column_width
)
4129 column_width
= force_column_width
;
4131 column_width
= default_column_width(cb
, idx
);
4133 labels
= iostat_bottom_labels
[idx
];
4134 for (i
= 0; i
< label_array_len(labels
); i
++) {
4136 printf(" %*s-", column_width
- 1, " ");
4138 printf(" %.*s", column_width
,
4139 "--------------------");
4146 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4147 unsigned int force_column_width
)
4149 print_iostat_dashes(cb
, force_column_width
, NULL
);
4153 print_iostat_separator(iostat_cbdata_t
*cb
)
4155 print_iostat_separator_impl(cb
, 0);
4159 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4160 const char *histo_vdev_name
)
4162 unsigned int namewidth
;
4165 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4166 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4167 } else if (cb
->cb_vdev_names_count
) {
4173 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4174 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4176 if (histo_vdev_name
)
4177 printf("%-*s", namewidth
, histo_vdev_name
);
4179 printf("%*s", namewidth
, "");
4182 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4185 printf("%-*s", namewidth
, title
);
4187 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4188 if (cb
->vcdl
!= NULL
)
4189 print_cmd_columns(cb
->vcdl
, 0);
4193 print_iostat_separator_impl(cb
, force_column_width
);
4195 if (cb
->vcdl
!= NULL
)
4196 print_cmd_columns(cb
->vcdl
, 1);
4202 print_iostat_header(iostat_cbdata_t
*cb
)
4204 print_iostat_header_impl(cb
, 0, NULL
);
4209 * Display a single statistic.
4212 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4213 unsigned int column_size
, boolean_t scripted
)
4217 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4220 printf("\t%s", buf
);
4222 printf(" %*s", column_size
, buf
);
4226 * Calculate the default vdev stats
4228 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4229 * stats into calcvs.
4232 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4233 vdev_stat_t
*calcvs
)
4237 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4238 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4239 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4241 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4242 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4246 * Internal representation of the extended iostats data.
4248 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4249 * or single uint64_t's. We make both look like arrays to make them easier
4250 * to process. In order to make single uint64_t's look like arrays, we set
4251 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4252 * we can just use *data and count.
4256 uint_t count
; /* Number of entries in data[] */
4257 uint64_t __data
; /* Only used when data is a single uint64_t */
4261 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4265 for (i
= 0; i
< len
; i
++)
4266 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4272 * Helper function to lookup a uint64_t array or uint64_t value and store its
4273 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4274 * it look like a one element array to make it easier to process.
4277 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4278 struct stat_array
*nva
)
4283 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4284 switch (nvpair_type(tmp
)) {
4285 case DATA_TYPE_UINT64_ARRAY
:
4286 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4288 case DATA_TYPE_UINT64
:
4289 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4290 nva
->data
= &nva
->__data
;
4294 /* Not a uint64_t */
4303 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4304 * subtract them, and return the results in a newly allocated stat_array.
4305 * You must free the returned array after you are done with it with
4306 * free_calc_stats().
4308 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4311 static struct stat_array
*
4312 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4315 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4316 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4318 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4320 /* Extract our extended stats nvlist from the main list */
4321 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4324 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4328 newnva
= safe_malloc(alloc_size
);
4329 oldnva
= safe_malloc(alloc_size
);
4330 calcnva
= safe_malloc(alloc_size
);
4332 for (j
= 0; j
< len
; j
++) {
4333 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4335 calcnva
[j
].count
= newnva
[j
].count
;
4336 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4337 calcnva
[j
].data
= safe_malloc(alloc_size
);
4338 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4341 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4343 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4344 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4353 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4356 for (i
= 0; i
< len
; i
++)
4363 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4364 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4370 enum zfs_nicenum_format format
;
4371 unsigned int buckets
;
4372 unsigned int start_bucket
;
4375 format
= ZFS_NICENUM_RAW
;
4377 format
= ZFS_NICENUM_1024
;
4379 /* All these histos are the same size, so just use nva[0].count */
4380 buckets
= nva
[0].count
;
4382 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4383 /* Start at 512 - req size should never be lower than this */
4389 for (j
= start_bucket
; j
< buckets
; j
++) {
4390 /* Print histogram bucket label */
4391 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4392 /* Ending range of this bucket */
4393 val
= (1UL << (j
+ 1)) - 1;
4394 zfs_nicetime(val
, buf
, sizeof (buf
));
4396 /* Request size (starting range of bucket) */
4398 zfs_nicenum(val
, buf
, sizeof (buf
));
4401 if (cb
->cb_scripted
)
4402 printf("%llu", (u_longlong_t
)val
);
4404 printf("%-*s", namewidth
, buf
);
4406 /* Print the values on the line */
4407 for (i
= 0; i
< len
; i
++) {
4408 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4409 column_width
, cb
->cb_scripted
);
4416 print_solid_separator(unsigned int length
)
4424 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4425 nvlist_t
*newnv
, double scale
, const char *name
)
4427 unsigned int column_width
;
4428 unsigned int namewidth
;
4429 unsigned int entire_width
;
4430 enum iostat_type type
;
4431 struct stat_array
*nva
;
4433 unsigned int names_len
;
4435 /* What type of histo are we? */
4436 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4438 /* Get NULL-terminated array of nvlist names for our histo */
4439 names
= vsx_type_to_nvlist
[type
];
4440 names_len
= str_array_len(names
); /* num of names */
4442 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4444 if (cb
->cb_literal
) {
4445 column_width
= MAX(5,
4446 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4451 namewidth
= MAX(cb
->cb_namewidth
,
4452 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4455 * Calculate the entire line width of what we're printing. The
4456 * +2 is for the two spaces between columns:
4460 /* |___| <---------- column_width */
4462 /* |__________| <--- entire_width */
4464 entire_width
= namewidth
+ (column_width
+ 2) *
4465 label_array_len(iostat_bottom_labels
[type
]);
4467 if (cb
->cb_scripted
)
4468 printf("%s\n", name
);
4470 print_iostat_header_impl(cb
, column_width
, name
);
4472 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4475 free_calc_stats(nva
, names_len
);
4476 if (!cb
->cb_scripted
)
4477 print_solid_separator(entire_width
);
4481 * Calculate the average latency of a power-of-two latency histogram
4484 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4487 uint64_t count
= 0, total
= 0;
4489 for (i
= 0; i
< buckets
; i
++) {
4491 * Our buckets are power-of-two latency ranges. Use the
4492 * midpoint latency of each bucket to calculate the average.
4500 if (histo
[i
] != 0) {
4501 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4506 /* Prevent divide by zero */
4507 return (count
== 0 ? 0 : total
/ count
);
4511 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4516 const char *names
[] = {
4517 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4518 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4519 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4520 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4521 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4522 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4523 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4524 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4525 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4526 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4527 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4528 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4531 struct stat_array
*nva
;
4533 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4534 enum zfs_nicenum_format format
;
4536 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4539 format
= ZFS_NICENUM_RAW
;
4541 format
= ZFS_NICENUM_1024
;
4543 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4544 val
= nva
[i
].data
[0];
4545 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4548 free_calc_stats(nva
, ARRAY_SIZE(names
));
4552 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4557 const char *names
[] = {
4558 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4559 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4560 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4561 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4562 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4563 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4564 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4565 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4566 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4567 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4569 struct stat_array
*nva
;
4571 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4572 enum zfs_nicenum_format format
;
4574 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4577 format
= ZFS_NICENUM_RAWTIME
;
4579 format
= ZFS_NICENUM_TIME
;
4581 /* Print our avg latencies on the line */
4582 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4583 /* Compute average latency for a latency histo */
4584 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4585 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4587 free_calc_stats(nva
, ARRAY_SIZE(names
));
4591 * Print default statistics (capacity/operations/bandwidth)
4594 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4596 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4597 enum zfs_nicenum_format format
;
4598 char na
; /* char to print for "not applicable" values */
4600 if (cb
->cb_literal
) {
4601 format
= ZFS_NICENUM_RAW
;
4604 format
= ZFS_NICENUM_1024
;
4608 /* only toplevel vdevs have capacity stats */
4609 if (vs
->vs_space
== 0) {
4610 if (cb
->cb_scripted
)
4611 printf("\t%c\t%c", na
, na
);
4613 printf(" %*c %*c", column_width
, na
, column_width
,
4616 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4618 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4619 column_width
, cb
->cb_scripted
);
4622 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4623 format
, column_width
, cb
->cb_scripted
);
4624 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4625 format
, column_width
, cb
->cb_scripted
);
4626 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4627 format
, column_width
, cb
->cb_scripted
);
4628 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4629 format
, column_width
, cb
->cb_scripted
);
4632 static const char *class_name
[] = {
4633 VDEV_ALLOC_BIAS_DEDUP
,
4634 VDEV_ALLOC_BIAS_SPECIAL
,
4635 VDEV_ALLOC_CLASS_LOGS
4639 * Print out all the statistics for the given vdev. This can either be the
4640 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4641 * is a verbose output, and we don't want to display the toplevel pool stats.
4643 * Returns the number of stat lines printed.
4646 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4647 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4649 nvlist_t
**oldchild
, **newchild
;
4650 uint_t c
, children
, oldchildren
;
4651 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4652 vdev_stat_t zerovs
= { 0 };
4659 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4662 calcvs
= safe_malloc(sizeof (*calcvs
));
4664 if (oldnv
!= NULL
) {
4665 verify(nvlist_lookup_uint64_array(oldnv
,
4666 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4671 /* Do we only want to see a specific vdev? */
4672 for (i
= 0; i
< cb
->cb_vdev_names_count
; i
++) {
4673 /* Yes we do. Is this the vdev? */
4674 if (strcmp(name
, cb
->cb_vdev_names
[i
]) == 0) {
4676 * This is our vdev. Since it is the only vdev we
4677 * will be displaying, make depth = 0 so that it
4678 * doesn't get indented.
4685 if (cb
->cb_vdev_names_count
&& (i
== cb
->cb_vdev_names_count
)) {
4686 /* Couldn't match the name */
4691 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4692 (uint64_t **)&newvs
, &c
) == 0);
4695 * Print the vdev name unless it's is a histogram. Histograms
4696 * display the vdev name in the header itself.
4698 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4699 if (cb
->cb_scripted
) {
4702 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4703 (void) printf("%*s%s", depth
, "", name
);
4705 (void) printf("%*s%s%*s", depth
, "", name
,
4706 (int)(cb
->cb_namewidth
- strlen(name
) -
4711 /* Calculate our scaling factor */
4712 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4713 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4715 * If we specify printing histograms with no time interval, then
4716 * print the histogram numbers over the entire lifetime of the
4724 scale
= (double)NANOSEC
/ tdelta
;
4727 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
4728 calc_default_iostats(oldvs
, newvs
, calcvs
);
4729 print_iostat_default(calcvs
, cb
, scale
);
4731 if (cb
->cb_flags
& IOS_LATENCY_M
)
4732 print_iostat_latency(cb
, oldnv
, newnv
);
4733 if (cb
->cb_flags
& IOS_QUEUES_M
)
4734 print_iostat_queues(cb
, oldnv
, newnv
);
4735 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4737 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
4740 if (cb
->vcdl
!= NULL
) {
4742 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
4745 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
4749 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
4758 if (!cb
->cb_verbose
)
4761 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
4762 &newchild
, &children
) != 0)
4766 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
4767 &oldchild
, &oldchildren
) != 0)
4770 children
= MIN(oldchildren
, children
);
4774 * print normal top-level devices
4776 for (c
= 0; c
< children
; c
++) {
4777 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
4779 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
4782 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
4785 if (ishole
|| islog
)
4788 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
4791 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4793 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
4794 newchild
[c
], cb
, depth
+ 2);
4799 * print all other top-level devices
4801 for (uint_t n
= 0; n
< 3; n
++) {
4802 boolean_t printed
= B_FALSE
;
4804 for (c
= 0; c
< children
; c
++) {
4805 uint64_t islog
= B_FALSE
;
4809 (void) nvlist_lookup_uint64(newchild
[c
],
4810 ZPOOL_CONFIG_IS_LOG
, &islog
);
4812 bias
= VDEV_ALLOC_CLASS_LOGS
;
4814 (void) nvlist_lookup_string(newchild
[c
],
4815 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
4816 (void) nvlist_lookup_string(newchild
[c
],
4817 ZPOOL_CONFIG_TYPE
, &type
);
4819 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
4821 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
4825 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
4826 !cb
->cb_scripted
&& !cb
->cb_vdev_names
) {
4827 print_iostat_dashes(cb
, 0,
4834 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4836 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
4837 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
4843 * Include level 2 ARC devices in iostat output
4845 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
4846 &newchild
, &children
) != 0)
4850 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
4851 &oldchild
, &oldchildren
) != 0)
4854 children
= MIN(oldchildren
, children
);
4858 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
4859 !cb
->cb_vdev_names
) {
4860 print_iostat_dashes(cb
, 0, "cache");
4864 for (c
= 0; c
< children
; c
++) {
4865 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
4867 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
4868 : NULL
, newchild
[c
], cb
, depth
+ 2);
4877 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
4879 iostat_cbdata_t
*cb
= data
;
4883 * If the pool has disappeared, remove it from the list and continue.
4885 if (zpool_refresh_stats(zhp
, &missing
) != 0)
4889 pool_list_remove(cb
->cb_list
, zhp
);
4895 * Callback to print out the iostats for the given pool.
4898 print_iostat(zpool_handle_t
*zhp
, void *data
)
4900 iostat_cbdata_t
*cb
= data
;
4901 nvlist_t
*oldconfig
, *newconfig
;
4902 nvlist_t
*oldnvroot
, *newnvroot
;
4905 newconfig
= zpool_get_config(zhp
, &oldconfig
);
4907 if (cb
->cb_iteration
== 1)
4910 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4913 if (oldconfig
== NULL
)
4916 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
4919 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
4921 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
4922 !cb
->cb_scripted
&& cb
->cb_verbose
&& !cb
->cb_vdev_names_count
) {
4923 print_iostat_separator(cb
);
4924 if (cb
->vcdl
!= NULL
) {
4925 print_cmd_columns(cb
->vcdl
, 1);
4940 if (isatty(STDOUT_FILENO
)) {
4941 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
4943 columns
= ws
.ws_col
;
4952 * Return the required length of the pool/vdev name column. The minimum
4953 * allowed width and output formatting flags must be provided.
4956 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
4958 nvlist_t
*config
, *nvroot
;
4959 int width
= min_width
;
4961 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
4962 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4964 unsigned int poolname_len
= strlen(zpool_get_name(zhp
));
4965 if (verbose
== B_FALSE
) {
4966 width
= MAX(poolname_len
, min_width
);
4968 width
= MAX(poolname_len
,
4969 max_width(zhp
, nvroot
, 0, min_width
, flags
));
4977 * Parse the input string, get the 'interval' and 'count' value if there is one.
4980 get_interval_count(int *argcp
, char **argv
, float *iv
,
4984 unsigned long count
= 0;
4988 * Determine if the last argument is an integer or a pool name
4990 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
4994 interval
= strtof(argv
[argc
- 1], &end
);
4996 if (*end
== '\0' && errno
== 0) {
4997 if (interval
== 0) {
4998 (void) fprintf(stderr
, gettext(
4999 "interval cannot be zero\n"));
5003 * Ignore the last parameter
5008 * If this is not a valid number, just plow on. The
5009 * user will get a more informative error message later
5017 * If the last argument is also an integer, then we have both a count
5020 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5025 interval
= strtof(argv
[argc
- 1], &end
);
5027 if (*end
== '\0' && errno
== 0) {
5028 if (interval
== 0) {
5029 (void) fprintf(stderr
, gettext(
5030 "interval cannot be zero\n"));
5035 * Ignore the last parameter
5049 get_timestamp_arg(char c
)
5052 timestamp_fmt
= UDATE
;
5054 timestamp_fmt
= DDATE
;
5060 * Return stat flags that are supported by all pools by both the module and
5061 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5062 * It will get ANDed down until only the flags that are supported on all pools
5066 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5068 uint64_t *mask
= data
;
5069 nvlist_t
*config
, *nvroot
, *nvx
;
5073 config
= zpool_get_config(zhp
, NULL
);
5074 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5077 /* Default stats are always supported, but for completeness.. */
5078 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5079 flags
|= IOS_DEFAULT_M
;
5081 /* Get our extended stats nvlist from the main list */
5082 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5085 * No extended stats; they're probably running an older
5086 * module. No big deal, we support that too.
5091 /* For each extended stat, make sure all its nvpairs are supported */
5092 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5093 if (!vsx_type_to_nvlist
[j
][0])
5096 /* Start off by assuming the flag is supported, then check */
5097 flags
|= (1ULL << j
);
5098 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5099 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5100 /* flag isn't supported */
5101 flags
= flags
& ~(1ULL << j
);
5107 *mask
= *mask
& flags
;
5112 * Return a bitmask of stats that are supported on all pools by both the module
5116 get_stat_flags(zpool_list_t
*list
)
5121 * get_stat_flags_cb() will lop off bits from "mask" until only the
5122 * flags that are supported on all pools remain.
5124 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5129 * Return 1 if cb_data->cb_vdev_names[0] is this vdev's name, 0 otherwise.
5132 is_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_data
)
5134 iostat_cbdata_t
*cb
= cb_data
;
5138 name
= zpool_vdev_name(g_zfs
, zhp
, nv
, cb
->cb_name_flags
);
5140 if (strcmp(name
, cb
->cb_vdev_names
[0]) == 0)
5141 ret
= 1; /* match */
5148 * Returns 1 if cb_data->cb_vdev_names[0] is a vdev name, 0 otherwise.
5151 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5153 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5157 * Check if vdevs are in a pool
5159 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5160 * return 0. If pool_name is NULL, then search all pools.
5163 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5164 iostat_cbdata_t
*cb
)
5171 if ((argc
== 0) || !*argv
)
5177 /* Temporarily hijack cb_vdev_names for a second... */
5178 tmp_name
= cb
->cb_vdev_names
;
5180 /* Go though our list of prospective vdev names */
5181 for (i
= 0; i
< argc
; i
++) {
5182 cb
->cb_vdev_names
= argv
+ i
;
5184 /* Is this name a vdev in our pools? */
5185 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5186 B_FALSE
, is_vdev
, cb
);
5193 cb
->cb_vdev_names
= tmp_name
;
5199 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5202 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5209 * Do we have a pool named *name? If so, return 1, otherwise 0.
5214 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, B_FALSE
, is_pool_cb
,
5218 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5220 are_all_pools(int argc
, char **argv
)
5222 if ((argc
== 0) || !*argv
)
5226 if (!is_pool(argv
[argc
]))
5233 * Helper function to print out vdev/pool names we can't resolve. Used for an
5237 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5238 iostat_cbdata_t
*cb
)
5243 for (i
= 0; i
< argc
; i
++) {
5247 str
= gettext("pool");
5248 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5249 str
= gettext("vdev in this pool");
5250 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5251 str
= gettext("vdev in another pool");
5253 str
= gettext("unknown");
5255 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5260 * Same as get_interval_count(), but with additional checks to not misinterpret
5261 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5265 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5266 unsigned long *count
, iostat_cbdata_t
*cb
)
5268 char **tmpargv
= argv
;
5269 int argc_for_interval
= 0;
5271 /* Is the last arg an interval value? Or a guid? */
5272 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
, cb
)) {
5274 * The last arg is not a guid, so it's probably an
5277 argc_for_interval
++;
5280 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
, cb
)) {
5282 * The 2nd to last arg is not a guid, so it's probably
5283 * an interval value.
5285 argc_for_interval
++;
5289 /* Point to our list of possible intervals */
5290 tmpargv
= &argv
[*argc
- argc_for_interval
];
5292 *argc
= *argc
- argc_for_interval
;
5293 get_interval_count(&argc_for_interval
, tmpargv
,
5298 * Floating point sleep(). Allows you to pass in a floating point value for
5304 struct timespec req
;
5305 req
.tv_sec
= floor(sec
);
5306 req
.tv_nsec
= (sec
- (float)req
.tv_sec
) * NANOSEC
;
5307 nanosleep(&req
, NULL
);
5311 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5312 * if we were unable to determine its size.
5315 terminal_height(void)
5319 if (isatty(STDOUT_FILENO
) == 0)
5322 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5323 return (win
.ws_row
);
5329 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5332 * name: Short name of the script ('iostat').
5333 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5336 print_zpool_script_help(char *name
, char *path
)
5338 char *argv
[] = {path
, "-h", NULL
};
5339 char **lines
= NULL
;
5343 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5345 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5347 libzfs_free_str_array(lines
, lines_cnt
);
5351 for (int i
= 0; i
< lines_cnt
; i
++)
5352 if (!is_blank_str(lines
[i
]))
5353 printf(" %-14s %s\n", name
, lines
[i
]);
5355 libzfs_free_str_array(lines
, lines_cnt
);
5359 * Go though the zpool status/iostat -c scripts in the user's path, run their
5360 * help option (-h), and print out the results.
5363 print_zpool_dir_scripts(char *dirpath
)
5367 char fullpath
[MAXPATHLEN
];
5368 struct stat dir_stat
;
5370 if ((dir
= opendir(dirpath
)) != NULL
) {
5371 /* print all the files and directories within directory */
5372 while ((ent
= readdir(dir
)) != NULL
) {
5373 sprintf(fullpath
, "%s/%s", dirpath
, ent
->d_name
);
5375 /* Print the scripts */
5376 if (stat(fullpath
, &dir_stat
) == 0)
5377 if (dir_stat
.st_mode
& S_IXUSR
&&
5378 S_ISREG(dir_stat
.st_mode
))
5379 print_zpool_script_help(ent
->d_name
,
5387 * Print out help text for all zpool status/iostat -c scripts.
5390 print_zpool_script_list(char *subcommand
)
5394 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5396 sp
= zpool_get_cmd_search_path();
5400 dir
= strtok(sp
, ":");
5401 while (dir
!= NULL
) {
5402 print_zpool_dir_scripts(dir
);
5403 dir
= strtok(NULL
, ":");
5410 * Set the minimum pool/vdev name column width. The width must be at least 10,
5411 * but may be as large as the column width - 42 so it still fits on one line.
5412 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5415 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5417 iostat_cbdata_t
*cb
= data
;
5418 int width
, available_width
;
5421 * get_namewidth() returns the maximum width of any name in that column
5422 * for any pool/vdev/device line that will be output.
5424 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
5428 * The width we are calculating is the width of the header and also the
5429 * padding width for names that are less than maximum width. The stats
5430 * take up 42 characters, so the width available for names is:
5432 available_width
= get_columns() - 42;
5435 * If the maximum width fits on a screen, then great! Make everything
5436 * line up by justifying all lines to the same width. If that max
5437 * width is larger than what's available, the name plus stats won't fit
5438 * on one line, and justifying to that width would cause every line to
5439 * wrap on the screen. We only want lines with long names to wrap.
5440 * Limit the padding to what won't wrap.
5442 if (width
> available_width
)
5443 width
= available_width
;
5446 * And regardless of whatever the screen width is (get_columns can
5447 * return 0 if the width is not known or less than 42 for a narrow
5448 * terminal) have the width be a minimum of 10.
5453 /* Save the calculated width */
5454 cb
->cb_namewidth
= width
;
5460 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5461 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5462 * [interval [count]]
5464 * -c CMD For each vdev, run command CMD
5465 * -g Display guid for individual vdev name.
5466 * -L Follow links when resolving vdev path name.
5467 * -P Display full path for vdev name.
5468 * -v Display statistics for individual vdevs
5470 * -p Display values in parsable (exact) format.
5471 * -H Scripted mode. Don't display headers, and separate properties
5473 * -l Display average latency
5474 * -q Display queue depths
5475 * -w Display latency histograms
5476 * -r Display request size histogram
5477 * -T Display a timestamp in date(1) or Unix format
5478 * -n Only print headers once
5480 * This command can be tricky because we want to be able to deal with pool
5481 * creation/destruction as well as vdev configuration changes. The bulk of this
5482 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5483 * on pool_list_update() to detect the addition of new pools. Configuration
5484 * changes are all handled within libzfs.
5487 zpool_do_iostat(int argc
, char **argv
)
5493 unsigned long count
= 0;
5496 boolean_t verbose
= B_FALSE
;
5497 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5498 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5499 boolean_t omit_since_boot
= B_FALSE
;
5500 boolean_t guid
= B_FALSE
;
5501 boolean_t follow_links
= B_FALSE
;
5502 boolean_t full_name
= B_FALSE
;
5503 boolean_t headers_once
= B_FALSE
;
5504 iostat_cbdata_t cb
= { 0 };
5507 /* Used for printing error message */
5508 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5509 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5511 uint64_t unsupported_flags
;
5514 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5519 gettext("Can't set -c flag twice\n"));
5523 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5524 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5525 fprintf(stderr
, gettext(
5526 "Can't run -c, disabled by "
5527 "ZPOOL_SCRIPTS_ENABLED.\n"));
5531 if ((getuid() <= 0 || geteuid() <= 0) &&
5532 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5533 fprintf(stderr
, gettext(
5534 "Can't run -c with root privileges "
5535 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5545 follow_links
= B_TRUE
;
5551 get_timestamp_arg(*optarg
);
5575 omit_since_boot
= B_TRUE
;
5578 headers_once
= B_TRUE
;
5584 if (optopt
== 'c') {
5585 print_zpool_script_list("iostat");
5589 gettext("invalid option '%c'\n"), optopt
);
5598 cb
.cb_literal
= parsable
;
5599 cb
.cb_scripted
= scripted
;
5602 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
5604 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5606 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
5607 cb
.cb_iteration
= 0;
5608 cb
.cb_namewidth
= 0;
5609 cb
.cb_verbose
= verbose
;
5611 /* Get our interval and count values (if any) */
5613 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5616 get_interval_count(&argc
, argv
, &interval
, &count
);
5620 /* No args, so just print the defaults. */
5621 } else if (are_all_pools(argc
, argv
)) {
5622 /* All the args are pool names */
5623 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
)) {
5624 /* All the args are vdevs */
5625 cb
.cb_vdev_names
= argv
;
5626 cb
.cb_vdev_names_count
= argc
;
5627 argc
= 0; /* No pools to process */
5628 } else if (are_all_pools(1, argv
)) {
5629 /* The first arg is a pool name */
5630 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0], &cb
)) {
5631 /* ...and the rest are vdev names */
5632 cb
.cb_vdev_names
= argv
+ 1;
5633 cb
.cb_vdev_names_count
= argc
- 1;
5634 argc
= 1; /* One pool to process */
5636 fprintf(stderr
, gettext("Expected either a list of "));
5637 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5638 fprintf(stderr
, " \"%s\", ", argv
[0]);
5639 fprintf(stderr
, gettext("but got:\n"));
5640 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5642 fprintf(stderr
, "\n");
5648 * The args don't make sense. The first arg isn't a pool name,
5649 * nor are all the args vdevs.
5651 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5652 fprintf(stderr
, "\n");
5656 if (cb
.cb_vdev_names_count
!= 0) {
5658 * If user specified vdevs, it implies verbose.
5660 cb
.cb_verbose
= B_TRUE
;
5664 * Construct the list of all interesting pools.
5667 if ((list
= pool_list_get(argc
, argv
, NULL
, parsable
, &ret
)) == NULL
)
5670 if (pool_list_count(list
) == 0 && argc
!= 0) {
5671 pool_list_free(list
);
5675 if (pool_list_count(list
) == 0 && interval
== 0) {
5676 pool_list_free(list
);
5677 (void) fprintf(stderr
, gettext("no pools available\n"));
5681 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5682 pool_list_free(list
);
5683 (void) fprintf(stderr
,
5684 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5689 if (l_histo
&& rq_histo
) {
5690 pool_list_free(list
);
5691 (void) fprintf(stderr
,
5692 gettext("Only one of [-r|-w] can be passed at a time\n"));
5698 * Enter the main iostat loop.
5704 * Histograms tables look out of place when you try to display
5705 * them with the other stats, so make a rule that you can only
5706 * print histograms by themselves.
5708 cb
.cb_flags
= IOS_L_HISTO_M
;
5709 } else if (rq_histo
) {
5710 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5712 cb
.cb_flags
= IOS_DEFAULT_M
;
5714 cb
.cb_flags
|= IOS_LATENCY_M
;
5716 cb
.cb_flags
|= IOS_QUEUES_M
;
5720 * See if the module supports all the stats we want to display.
5722 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5723 if (unsupported_flags
) {
5727 gettext("The loaded zfs module doesn't support:"));
5729 /* for each bit set in unsupported_flags */
5730 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
5731 idx
= lowbit64(f
) - 1;
5732 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
5735 fprintf(stderr
, ". Try running a newer module.\n");
5736 pool_list_free(list
);
5742 if ((npools
= pool_list_count(list
)) == 0)
5743 (void) fprintf(stderr
, gettext("no pools available\n"));
5746 * If this is the first iteration and -y was supplied
5747 * we skip any printing.
5749 boolean_t skip
= (omit_since_boot
&&
5750 cb
.cb_iteration
== 0);
5753 * Refresh all statistics. This is done as an
5754 * explicit step before calculating the maximum name
5755 * width, so that any * configuration changes are
5756 * properly accounted for.
5758 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
5762 * Iterate over all pools to determine the maximum width
5763 * for the pool / device name column across all pools.
5765 cb
.cb_namewidth
= 0;
5766 (void) pool_list_iter(list
, B_FALSE
,
5767 get_namewidth_iostat
, &cb
);
5769 if (timestamp_fmt
!= NODATE
)
5770 print_timestamp(timestamp_fmt
);
5772 if (cmd
!= NULL
&& cb
.cb_verbose
&&
5773 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
5774 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
5775 argv
, cmd
, g_zfs
, cb
.cb_vdev_names
,
5776 cb
.cb_vdev_names_count
, cb
.cb_name_flags
);
5783 * Check terminal size so we can print headers
5784 * even when terminal window has its height
5787 winheight
= terminal_height();
5789 * Are we connected to TTY? If not, headers_once
5790 * should be true, to avoid breaking scripts.
5793 headers_once
= B_TRUE
;
5796 * If it's the first time and we're not skipping it,
5797 * or either skip or verbose mode, print the header.
5799 * The histogram code explicitly prints its header on
5800 * every vdev, so skip this for histograms.
5802 if (((++cb
.cb_iteration
== 1 && !skip
) ||
5803 (skip
!= verbose
) ||
5805 (cb
.cb_iteration
% winheight
) == 0)) &&
5806 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
5808 print_iostat_header(&cb
);
5811 (void) fsleep(interval
);
5815 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
5818 * If there's more than one pool, and we're not in
5819 * verbose mode (which prints a separator for us),
5820 * then print a separator.
5822 * In addition, if we're printing specific vdevs then
5823 * we also want an ending separator.
5825 if (((npools
> 1 && !verbose
&&
5826 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
5827 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
5828 cb
.cb_vdev_names_count
)) &&
5830 print_iostat_separator(&cb
);
5831 if (cb
.vcdl
!= NULL
)
5832 print_cmd_columns(cb
.vcdl
, 1);
5836 if (cb
.vcdl
!= NULL
)
5837 free_vdev_cmd_data_list(cb
.vcdl
);
5842 * Flush the output so that redirection to a file isn't buffered
5845 (void) fflush(stdout
);
5850 if (count
!= 0 && --count
== 0)
5853 (void) fsleep(interval
);
5856 pool_list_free(list
);
5861 typedef struct list_cbdata
{
5862 boolean_t cb_verbose
;
5865 boolean_t cb_scripted
;
5866 zprop_list_t
*cb_proplist
;
5867 boolean_t cb_literal
;
5872 * Given a list of columns to display, output appropriate headers for each one.
5875 print_header(list_cbdata_t
*cb
)
5877 zprop_list_t
*pl
= cb
->cb_proplist
;
5878 char headerbuf
[ZPOOL_MAXPROPLEN
];
5880 boolean_t first
= B_TRUE
;
5881 boolean_t right_justify
;
5884 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5885 width
= pl
->pl_width
;
5886 if (first
&& cb
->cb_verbose
) {
5888 * Reset the width to accommodate the verbose listing
5891 width
= cb
->cb_namewidth
;
5899 right_justify
= B_FALSE
;
5900 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5901 header
= zpool_prop_column_name(pl
->pl_prop
);
5902 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5906 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
5907 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
5908 headerbuf
[i
] = '\0';
5912 if (pl
->pl_next
== NULL
&& !right_justify
)
5913 (void) printf("%s", header
);
5914 else if (right_justify
)
5915 (void) printf("%*s", (int)width
, header
);
5917 (void) printf("%-*s", (int)width
, header
);
5920 (void) printf("\n");
5924 * Given a pool and a list of properties, print out all the properties according
5925 * to the described layout. Used by zpool_do_list().
5928 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
5930 zprop_list_t
*pl
= cb
->cb_proplist
;
5931 boolean_t first
= B_TRUE
;
5932 char property
[ZPOOL_MAXPROPLEN
];
5934 boolean_t right_justify
;
5937 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
5939 width
= pl
->pl_width
;
5940 if (first
&& cb
->cb_verbose
) {
5942 * Reset the width to accommodate the verbose listing
5945 width
= cb
->cb_namewidth
;
5949 if (cb
->cb_scripted
)
5950 (void) printf("\t");
5957 right_justify
= B_FALSE
;
5958 if (pl
->pl_prop
!= ZPROP_INVAL
) {
5959 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
5960 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
5965 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
5966 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
5967 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
5968 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
5969 sizeof (property
)) == 0) {
5977 * If this is being called in scripted mode, or if this is the
5978 * last column and it is left-justified, don't include a width
5981 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
5982 (void) printf("%s", propstr
);
5983 else if (right_justify
)
5984 (void) printf("%*s", (int)width
, propstr
);
5986 (void) printf("%-*s", (int)width
, propstr
);
5989 (void) printf("\n");
5993 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
5994 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
5998 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6001 case ZPOOL_PROP_EXPANDSZ
:
6002 case ZPOOL_PROP_CHECKPOINT
:
6003 case ZPOOL_PROP_DEDUPRATIO
:
6005 (void) strlcpy(propval
, "-", sizeof (propval
));
6007 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6010 case ZPOOL_PROP_FRAGMENTATION
:
6011 if (value
== ZFS_FRAG_INVALID
) {
6012 (void) strlcpy(propval
, "-", sizeof (propval
));
6013 } else if (format
== ZFS_NICENUM_RAW
) {
6014 (void) snprintf(propval
, sizeof (propval
), "%llu",
6015 (unsigned long long)value
);
6017 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6018 (unsigned long long)value
);
6021 case ZPOOL_PROP_CAPACITY
:
6022 /* capacity value is in parts-per-10,000 (aka permyriad) */
6023 if (format
== ZFS_NICENUM_RAW
)
6024 (void) snprintf(propval
, sizeof (propval
), "%llu",
6025 (unsigned long long)value
/ 100);
6027 (void) snprintf(propval
, sizeof (propval
),
6028 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6029 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6031 case ZPOOL_PROP_HEALTH
:
6033 (void) strlcpy(propval
, str
, sizeof (propval
));
6036 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6040 (void) strlcpy(propval
, "-", sizeof (propval
));
6043 (void) printf("\t%s", propval
);
6045 (void) printf(" %*s", (int)width
, propval
);
6049 * print static default line per vdev
6050 * not compatible with '-o' <proplist> option
6053 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6054 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
6060 boolean_t scripted
= cb
->cb_scripted
;
6061 uint64_t islog
= B_FALSE
;
6062 char *dashes
= "%-*s - - - - "
6065 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
6066 (uint64_t **)&vs
, &c
) == 0);
6069 boolean_t toplevel
= (vs
->vs_space
!= 0);
6071 enum zfs_nicenum_format format
;
6075 format
= ZFS_NICENUM_RAW
;
6077 format
= ZFS_NICENUM_1024
;
6079 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
6083 (void) printf("\t%s", name
);
6084 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
6085 (void) printf("%*s%s", depth
, "", name
);
6087 (void) printf("%*s%s%*s", depth
, "", name
,
6088 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
6091 * Print the properties for the individual vdevs. Some
6092 * properties are only applicable to toplevel vdevs. The
6093 * 'toplevel' boolean value is passed to the print_one_column()
6094 * to indicate that the value is valid.
6096 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
, scripted
,
6098 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6099 scripted
, toplevel
, format
);
6100 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6101 NULL
, scripted
, toplevel
, format
);
6102 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6103 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6104 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6105 scripted
, B_TRUE
, format
);
6106 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6107 vs
->vs_fragmentation
, NULL
, scripted
,
6108 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6110 cap
= (vs
->vs_space
== 0) ? 0 :
6111 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6112 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6113 scripted
, toplevel
, format
);
6114 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6115 scripted
, toplevel
, format
);
6116 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6118 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6120 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6123 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6125 (void) printf("\n");
6128 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6129 &child
, &children
) != 0)
6132 /* list the normal vdevs first */
6133 for (c
= 0; c
< children
; c
++) {
6134 uint64_t ishole
= B_FALSE
;
6136 if (nvlist_lookup_uint64(child
[c
],
6137 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6140 if (nvlist_lookup_uint64(child
[c
],
6141 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6144 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6147 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6149 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6153 /* list the classes: 'logs', 'dedup', and 'special' */
6154 for (uint_t n
= 0; n
< 3; n
++) {
6155 boolean_t printed
= B_FALSE
;
6157 for (c
= 0; c
< children
; c
++) {
6161 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6162 &islog
) == 0 && islog
) {
6163 bias
= VDEV_ALLOC_CLASS_LOGS
;
6165 (void) nvlist_lookup_string(child
[c
],
6166 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6167 (void) nvlist_lookup_string(child
[c
],
6168 ZPOOL_CONFIG_TYPE
, &type
);
6170 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6172 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6176 /* LINTED E_SEC_PRINTF_VAR_FMT */
6177 (void) printf(dashes
, cb
->cb_namewidth
,
6181 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6183 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6189 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6190 &child
, &children
) == 0 && children
> 0) {
6191 /* LINTED E_SEC_PRINTF_VAR_FMT */
6192 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6193 for (c
= 0; c
< children
; c
++) {
6194 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6196 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6202 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6203 &children
) == 0 && children
> 0) {
6204 /* LINTED E_SEC_PRINTF_VAR_FMT */
6205 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6206 for (c
= 0; c
< children
; c
++) {
6207 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6209 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6217 * Generic callback function to list a pool.
6220 list_callback(zpool_handle_t
*zhp
, void *data
)
6222 list_cbdata_t
*cbp
= data
;
6224 print_pool(zhp
, cbp
);
6226 if (cbp
->cb_verbose
) {
6227 nvlist_t
*config
, *nvroot
;
6229 config
= zpool_get_config(zhp
, NULL
);
6230 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6232 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6239 * Set the minimum pool/vdev name column width. The width must be at least 9,
6240 * but may be as large as needed.
6243 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6245 list_cbdata_t
*cb
= data
;
6248 width
= get_namewidth(zhp
, cb
->cb_namewidth
, cb
->cb_name_flags
,
6254 cb
->cb_namewidth
= width
;
6260 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6262 * -g Display guid for individual vdev name.
6263 * -H Scripted mode. Don't display headers, and separate properties
6265 * -L Follow links when resolving vdev path name.
6266 * -o List of properties to display. Defaults to
6267 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6268 * "dedupratio,health,altroot"
6269 * -p Display values in parsable (exact) format.
6270 * -P Display full path for vdev name.
6271 * -T Display a timestamp in date(1) or Unix format
6273 * List all pools in the system, whether or not they're healthy. Output space
6274 * statistics for each one, as well as health status summary.
6277 zpool_do_list(int argc
, char **argv
)
6281 list_cbdata_t cb
= { 0 };
6282 static char default_props
[] =
6283 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6284 "capacity,dedupratio,health,altroot";
6285 char *props
= default_props
;
6287 unsigned long count
= 0;
6289 boolean_t first
= B_TRUE
;
6292 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6295 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6298 cb
.cb_scripted
= B_TRUE
;
6301 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6307 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6310 cb
.cb_literal
= B_TRUE
;
6313 get_timestamp_arg(*optarg
);
6316 cb
.cb_verbose
= B_TRUE
;
6317 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6320 (void) fprintf(stderr
, gettext("missing argument for "
6321 "'%c' option\n"), optopt
);
6325 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6334 get_interval_count(&argc
, argv
, &interval
, &count
);
6336 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6340 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6341 cb
.cb_literal
, &ret
)) == NULL
)
6344 if (pool_list_count(list
) == 0)
6347 cb
.cb_namewidth
= 0;
6348 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6350 if (timestamp_fmt
!= NODATE
)
6351 print_timestamp(timestamp_fmt
);
6353 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6357 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6362 if (count
!= 0 && --count
== 0)
6365 pool_list_free(list
);
6366 (void) fsleep(interval
);
6369 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6370 (void) printf(gettext("no pools available\n"));
6374 pool_list_free(list
);
6375 zprop_free_list(cb
.cb_proplist
);
6380 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6382 boolean_t force
= B_FALSE
;
6383 boolean_t rebuild
= B_FALSE
;
6384 boolean_t wait
= B_FALSE
;
6387 char *poolname
, *old_disk
, *new_disk
;
6388 zpool_handle_t
*zhp
;
6389 nvlist_t
*props
= NULL
;
6394 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6400 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6401 (void) fprintf(stderr
, gettext("missing "
6402 "'=' for -o option\n"));
6408 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6409 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6419 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6428 /* get pool name and check number of arguments */
6430 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6437 (void) fprintf(stderr
,
6438 gettext("missing <device> specification\n"));
6446 (void) fprintf(stderr
,
6447 gettext("missing <new_device> specification\n"));
6450 new_disk
= old_disk
;
6460 (void) fprintf(stderr
, gettext("too many arguments\n"));
6464 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6469 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6470 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6477 /* unless manually specified use "ashift" pool property (if set) */
6478 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6481 char strval
[ZPOOL_MAXPROPLEN
];
6483 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6484 if (src
!= ZPROP_SRC_DEFAULT
) {
6485 (void) sprintf(strval
, "%" PRId32
, intval
);
6486 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6487 &props
, B_TRUE
) == 0);
6491 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6493 if (nvroot
== NULL
) {
6499 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6502 if (ret
== 0 && wait
)
6503 ret
= zpool_wait(zhp
,
6504 replacing
? ZPOOL_WAIT_REPLACE
: ZPOOL_WAIT_RESILVER
);
6507 nvlist_free(nvroot
);
6514 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6516 * -f Force attach, even if <new_device> appears to be in use.
6517 * -s Use sequential instead of healing reconstruction for resilver.
6518 * -o Set property=value.
6519 * -w Wait for replacing to complete before returning
6521 * Replace <device> with <new_device>.
6525 zpool_do_replace(int argc
, char **argv
)
6527 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6531 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6533 * -f Force attach, even if <new_device> appears to be in use.
6534 * -s Use sequential instead of healing reconstruction for resilver.
6535 * -o Set property=value.
6536 * -w Wait for resilvering to complete before returning
6538 * Attach <new_device> to the mirror containing <device>. If <device> is not
6539 * part of a mirror, then <device> will be transformed into a mirror of
6540 * <device> and <new_device>. In either case, <new_device> will begin life
6541 * with a DTL of [0, now], and will immediately begin to resilver itself.
6544 zpool_do_attach(int argc
, char **argv
)
6546 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6550 * zpool detach [-f] <pool> <device>
6552 * -f Force detach of <device>, even if DTLs argue against it
6553 * (not supported yet)
6555 * Detach a device from a mirror. The operation will be refused if <device>
6556 * is the last device in the mirror, or if the DTLs indicate that this device
6557 * has the only valid copy of some data.
6561 zpool_do_detach(int argc
, char **argv
)
6564 char *poolname
, *path
;
6565 zpool_handle_t
*zhp
;
6569 while ((c
= getopt(argc
, argv
, "")) != -1) {
6572 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6581 /* get pool name and check number of arguments */
6583 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6588 (void) fprintf(stderr
,
6589 gettext("missing <device> specification\n"));
6596 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6599 ret
= zpool_vdev_detach(zhp
, path
);
6607 * zpool split [-gLnP] [-o prop=val] ...
6609 * [-R altroot] <pool> <newpool> [<device> ...]
6611 * -g Display guid for individual vdev name.
6612 * -L Follow links when resolving vdev path name.
6613 * -n Do not split the pool, but display the resulting layout if
6614 * it were to be split.
6615 * -o Set property=value, or set mount options.
6616 * -P Display full path for vdev name.
6617 * -R Mount the split-off pool under an alternate root.
6618 * -l Load encryption keys while importing.
6620 * Splits the named pool and gives it the new pool name. Devices to be split
6621 * off may be listed, provided that no more than one device is specified
6622 * per top-level vdev mirror. The newly split pool is left in an exported
6623 * state unless -R is specified.
6625 * Restrictions: the top-level of the pool pool must only be made up of
6626 * mirrors; all devices in the pool must be healthy; no device may be
6627 * undergoing a resilvering operation.
6630 zpool_do_split(int argc
, char **argv
)
6632 char *srcpool
, *newpool
, *propval
;
6633 char *mntopts
= NULL
;
6636 boolean_t loadkeys
= B_FALSE
;
6637 zpool_handle_t
*zhp
;
6638 nvlist_t
*config
, *props
= NULL
;
6640 flags
.dryrun
= B_FALSE
;
6641 flags
.import
= B_FALSE
;
6642 flags
.name_flags
= 0;
6645 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6648 flags
.name_flags
|= VDEV_NAME_GUID
;
6651 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6654 flags
.import
= B_TRUE
;
6656 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6657 &props
, B_TRUE
) != 0) {
6666 flags
.dryrun
= B_TRUE
;
6669 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6672 if (add_prop_list(optarg
, propval
,
6673 &props
, B_TRUE
) != 0) {
6682 flags
.name_flags
|= VDEV_NAME_PATH
;
6685 (void) fprintf(stderr
, gettext("missing argument for "
6686 "'%c' option\n"), optopt
);
6690 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6697 if (!flags
.import
&& mntopts
!= NULL
) {
6698 (void) fprintf(stderr
, gettext("setting mntopts is only "
6699 "valid when importing the pool\n"));
6703 if (!flags
.import
&& loadkeys
) {
6704 (void) fprintf(stderr
, gettext("loading keys is only "
6705 "valid when importing the pool\n"));
6713 (void) fprintf(stderr
, gettext("Missing pool name\n"));
6717 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
6727 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
6732 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
6733 if (config
== NULL
) {
6737 (void) printf(gettext("would create '%s' with the "
6738 "following layout:\n\n"), newpool
);
6739 print_vdev_tree(NULL
, newpool
, config
, 0, "",
6741 print_vdev_tree(NULL
, "dedup", config
, 0,
6742 VDEV_ALLOC_BIAS_DEDUP
, 0);
6743 print_vdev_tree(NULL
, "special", config
, 0,
6744 VDEV_ALLOC_BIAS_SPECIAL
, 0);
6750 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
6751 nvlist_free(config
);
6757 * The split was successful. Now we need to open the new
6758 * pool and import it.
6760 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
6761 nvlist_free(config
);
6767 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
6772 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
6773 zpool_enable_datasets(zhp
, mntopts
, 0) != 0) {
6775 (void) fprintf(stderr
, gettext("Split was successful, but "
6776 "the datasets could not all be mounted\n"));
6777 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
6778 "different altroot\n"), "zpool import");
6781 nvlist_free(config
);
6790 * zpool online <pool> <device> ...
6793 zpool_do_online(int argc
, char **argv
)
6797 zpool_handle_t
*zhp
;
6799 vdev_state_t newstate
;
6803 while ((c
= getopt(argc
, argv
, "e")) != -1) {
6806 flags
|= ZFS_ONLINE_EXPAND
;
6809 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6818 /* get pool name and check number of arguments */
6820 (void) fprintf(stderr
, gettext("missing pool name\n"));
6824 (void) fprintf(stderr
, gettext("missing device name\n"));
6830 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6833 for (i
= 1; i
< argc
; i
++) {
6834 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
6835 if (newstate
!= VDEV_STATE_HEALTHY
) {
6836 (void) printf(gettext("warning: device '%s' "
6837 "onlined, but remains in faulted state\n"),
6839 if (newstate
== VDEV_STATE_FAULTED
)
6840 (void) printf(gettext("use 'zpool "
6841 "clear' to restore a faulted "
6844 (void) printf(gettext("use 'zpool "
6845 "replace' to replace devices "
6846 "that are no longer present\n"));
6859 * zpool offline [-ft] <pool> <device> ...
6861 * -f Force the device into a faulted state.
6863 * -t Only take the device off-line temporarily. The offline/faulted
6864 * state will not be persistent across reboots.
6868 zpool_do_offline(int argc
, char **argv
)
6872 zpool_handle_t
*zhp
;
6874 boolean_t istmp
= B_FALSE
;
6875 boolean_t fault
= B_FALSE
;
6878 while ((c
= getopt(argc
, argv
, "ft")) != -1) {
6887 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6896 /* get pool name and check number of arguments */
6898 (void) fprintf(stderr
, gettext("missing pool name\n"));
6902 (void) fprintf(stderr
, gettext("missing device name\n"));
6908 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6911 for (i
= 1; i
< argc
; i
++) {
6913 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
6915 if (istmp
== B_FALSE
) {
6916 /* Force the fault to persist across imports */
6917 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
6919 aux
= VDEV_AUX_EXTERNAL
;
6922 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
6925 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
6936 * zpool clear <pool> [device]
6938 * Clear all errors associated with a pool or a particular device.
6941 zpool_do_clear(int argc
, char **argv
)
6945 boolean_t dryrun
= B_FALSE
;
6946 boolean_t do_rewind
= B_FALSE
;
6947 boolean_t xtreme_rewind
= B_FALSE
;
6948 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
6949 nvlist_t
*policy
= NULL
;
6950 zpool_handle_t
*zhp
;
6951 char *pool
, *device
;
6954 while ((c
= getopt(argc
, argv
, "FnX")) != -1) {
6963 xtreme_rewind
= B_TRUE
;
6966 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6976 (void) fprintf(stderr
, gettext("missing pool name\n"));
6981 (void) fprintf(stderr
, gettext("too many arguments\n"));
6985 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
6986 (void) fprintf(stderr
,
6987 gettext("-n or -X only meaningful with -F\n"));
6991 rewind_policy
= ZPOOL_TRY_REWIND
;
6993 rewind_policy
= ZPOOL_DO_REWIND
;
6995 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
6997 /* In future, further rewind policy choices can be passed along here */
6998 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
6999 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
7000 rewind_policy
) != 0) {
7005 device
= argc
== 2 ? argv
[1] : NULL
;
7007 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
7008 nvlist_free(policy
);
7012 if (zpool_clear(zhp
, device
, policy
) != 0)
7017 nvlist_free(policy
);
7023 * zpool reguid <pool>
7026 zpool_do_reguid(int argc
, char **argv
)
7030 zpool_handle_t
*zhp
;
7034 while ((c
= getopt(argc
, argv
, "")) != -1) {
7037 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7046 /* get pool name and check number of arguments */
7048 (void) fprintf(stderr
, gettext("missing pool name\n"));
7053 (void) fprintf(stderr
, gettext("too many arguments\n"));
7058 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7061 ret
= zpool_reguid(zhp
);
7069 * zpool reopen <pool>
7071 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7074 zpool_do_reopen(int argc
, char **argv
)
7078 boolean_t scrub_restart
= B_TRUE
;
7081 while ((c
= getopt(argc
, argv
, "n")) != -1) {
7084 scrub_restart
= B_FALSE
;
7087 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7096 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7097 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
, zpool_reopen_one
,
7103 typedef struct scrub_cbdata
{
7105 pool_scrub_cmd_t cb_scrub_cmd
;
7109 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7111 nvlist_t
*config
, *nvroot
;
7113 config
= zpool_get_config(zhp
, NULL
);
7115 if (config
!= NULL
) {
7116 pool_checkpoint_stat_t
*pcs
= NULL
;
7119 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7120 (void) nvlist_lookup_uint64_array(nvroot
,
7121 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7123 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7126 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7127 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7135 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7137 scrub_cbdata_t
*cb
= data
;
7141 * Ignore faulted pools.
7143 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7144 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7145 "currently unavailable\n"), zpool_get_name(zhp
));
7149 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7151 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7152 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7153 (void) printf(gettext("warning: will not scrub state that "
7154 "belongs to the checkpoint of pool '%s'\n"),
7155 zpool_get_name(zhp
));
7162 wait_callback(zpool_handle_t
*zhp
, void *data
)
7164 zpool_wait_activity_t
*act
= data
;
7165 return (zpool_wait(zhp
, *act
));
7169 * zpool scrub [-s | -p] [-w] <pool> ...
7171 * -s Stop. Stops any in-progress scrub.
7172 * -p Pause. Pause in-progress scrub.
7173 * -w Wait. Blocks until scrub has completed.
7176 zpool_do_scrub(int argc
, char **argv
)
7180 boolean_t wait
= B_FALSE
;
7183 cb
.cb_type
= POOL_SCAN_SCRUB
;
7184 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7187 while ((c
= getopt(argc
, argv
, "spw")) != -1) {
7190 cb
.cb_type
= POOL_SCAN_NONE
;
7193 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7199 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7205 if (cb
.cb_type
== POOL_SCAN_NONE
&&
7206 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
) {
7207 (void) fprintf(stderr
, gettext("invalid option combination: "
7208 "-s and -p are mutually exclusive\n"));
7212 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7213 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7214 (void) fprintf(stderr
, gettext("invalid option combination: "
7215 "-w cannot be used with -p or -s\n"));
7223 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7227 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7228 scrub_callback
, &cb
);
7230 if (wait
&& !error
) {
7231 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7232 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7233 wait_callback
, &act
);
7240 * zpool resilver <pool> ...
7242 * Restarts any in-progress resilver
7245 zpool_do_resilver(int argc
, char **argv
)
7250 cb
.cb_type
= POOL_SCAN_RESILVER
;
7251 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7254 while ((c
= getopt(argc
, argv
, "")) != -1) {
7257 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7267 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7271 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, B_FALSE
,
7272 scrub_callback
, &cb
));
7276 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7278 * -c Cancel. Ends any in-progress trim.
7279 * -d Secure trim. Requires kernel and device support.
7280 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7281 * adding a multiplier suffix such as 'k' or 'm'.
7282 * -s Suspend. TRIM can then be restarted with no flags.
7283 * -w Wait. Blocks until trimming has completed.
7286 zpool_do_trim(int argc
, char **argv
)
7288 struct option long_options
[] = {
7289 {"cancel", no_argument
, NULL
, 'c'},
7290 {"secure", no_argument
, NULL
, 'd'},
7291 {"rate", required_argument
, NULL
, 'r'},
7292 {"suspend", no_argument
, NULL
, 's'},
7293 {"wait", no_argument
, NULL
, 'w'},
7297 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7299 boolean_t secure
= B_FALSE
;
7300 boolean_t wait
= B_FALSE
;
7303 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7307 if (cmd_type
!= POOL_TRIM_START
&&
7308 cmd_type
!= POOL_TRIM_CANCEL
) {
7309 (void) fprintf(stderr
, gettext("-c cannot be "
7310 "combined with other options\n"));
7313 cmd_type
= POOL_TRIM_CANCEL
;
7316 if (cmd_type
!= POOL_TRIM_START
) {
7317 (void) fprintf(stderr
, gettext("-d cannot be "
7318 "combined with the -c or -s options\n"));
7324 if (cmd_type
!= POOL_TRIM_START
) {
7325 (void) fprintf(stderr
, gettext("-r cannot be "
7326 "combined with the -c or -s options\n"));
7329 if (zfs_nicestrtonum(NULL
, optarg
, &rate
) == -1) {
7330 (void) fprintf(stderr
,
7331 gettext("invalid value for rate\n"));
7336 if (cmd_type
!= POOL_TRIM_START
&&
7337 cmd_type
!= POOL_TRIM_SUSPEND
) {
7338 (void) fprintf(stderr
, gettext("-s cannot be "
7339 "combined with other options\n"));
7342 cmd_type
= POOL_TRIM_SUSPEND
;
7349 (void) fprintf(stderr
,
7350 gettext("invalid option '%c'\n"), optopt
);
7352 (void) fprintf(stderr
,
7353 gettext("invalid option '%s'\n"),
7364 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7369 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7370 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7375 char *poolname
= argv
[0];
7376 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7380 trimflags_t trim_flags
= {
7386 nvlist_t
*vdevs
= fnvlist_alloc();
7388 /* no individual leaf vdevs specified, so add them all */
7389 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7390 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7391 ZPOOL_CONFIG_VDEV_TREE
);
7392 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7393 trim_flags
.fullpool
= B_TRUE
;
7395 trim_flags
.fullpool
= B_FALSE
;
7396 for (int i
= 1; i
< argc
; i
++) {
7397 fnvlist_add_boolean(vdevs
, argv
[i
]);
7401 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7403 fnvlist_free(vdevs
);
7410 * Converts a total number of seconds to a human readable string broken
7411 * down in to days/hours/minutes/seconds.
7414 secs_to_dhms(uint64_t total
, char *buf
)
7416 uint64_t days
= total
/ 60 / 60 / 24;
7417 uint64_t hours
= (total
/ 60 / 60) % 24;
7418 uint64_t mins
= (total
/ 60) % 60;
7419 uint64_t secs
= (total
% 60);
7422 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7423 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7424 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7426 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7427 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7428 (u_longlong_t
)secs
);
7433 * Print out detailed scrub status.
7436 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7438 time_t start
, end
, pause
;
7439 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total
;
7440 uint64_t elapsed
, scan_rate
, issue_rate
;
7441 double fraction_done
;
7442 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_buf
[7];
7443 char srate_buf
[7], irate_buf
[7], time_buf
[32];
7446 printf_color(ANSI_BOLD
, gettext("scan:"));
7449 /* If there's never been a scan, there's not much to say. */
7450 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7451 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7452 (void) printf(gettext("none requested\n"));
7456 start
= ps
->pss_start_time
;
7457 end
= ps
->pss_end_time
;
7458 pause
= ps
->pss_pass_scrub_pause
;
7460 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7462 assert(ps
->pss_func
== POOL_SCAN_SCRUB
||
7463 ps
->pss_func
== POOL_SCAN_RESILVER
);
7465 /* Scan is finished or canceled. */
7466 if (ps
->pss_state
== DSS_FINISHED
) {
7467 secs_to_dhms(end
- start
, time_buf
);
7469 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7470 (void) printf(gettext("scrub repaired %s "
7471 "in %s with %llu errors on %s"), processed_buf
,
7472 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7474 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7475 (void) printf(gettext("resilvered %s "
7476 "in %s with %llu errors on %s"), processed_buf
,
7477 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7481 } else if (ps
->pss_state
== DSS_CANCELED
) {
7482 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7483 (void) printf(gettext("scrub canceled on %s"),
7485 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7486 (void) printf(gettext("resilver canceled on %s"),
7492 assert(ps
->pss_state
== DSS_SCANNING
);
7494 /* Scan is in progress. Resilvers can't be paused. */
7495 if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7497 (void) printf(gettext("scrub in progress since %s"),
7500 (void) printf(gettext("scrub paused since %s"),
7502 (void) printf(gettext("\tscrub started on %s"),
7505 } else if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7506 (void) printf(gettext("resilver in progress since %s"),
7510 scanned
= ps
->pss_examined
;
7511 pass_scanned
= ps
->pss_pass_exam
;
7512 issued
= ps
->pss_issued
;
7513 pass_issued
= ps
->pss_pass_issued
;
7514 total
= ps
->pss_to_examine
;
7516 /* we are only done with a block once we have issued the IO for it */
7517 fraction_done
= (double)issued
/ total
;
7519 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7520 elapsed
= time(NULL
) - ps
->pss_pass_start
;
7521 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
7522 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
7524 scan_rate
= pass_scanned
/ elapsed
;
7525 issue_rate
= pass_issued
/ elapsed
;
7526 uint64_t total_secs_left
= (issue_rate
!= 0 && total
>= issued
) ?
7527 ((total
- issued
) / issue_rate
) : UINT64_MAX
;
7528 secs_to_dhms(total_secs_left
, time_buf
);
7530 /* format all of the numbers we will be reporting */
7531 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
7532 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
7533 zfs_nicebytes(total
, total_buf
, sizeof (total_buf
));
7534 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
7535 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
7537 /* do not print estimated time if we have a paused scrub */
7539 (void) printf(gettext("\t%s scanned at %s/s, "
7540 "%s issued at %s/s, %s total\n"),
7541 scanned_buf
, srate_buf
, issued_buf
, irate_buf
, total_buf
);
7543 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7544 scanned_buf
, issued_buf
, total_buf
);
7547 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7548 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7549 processed_buf
, 100 * fraction_done
);
7550 } else if (ps
->pss_func
== POOL_SCAN_SCRUB
) {
7551 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7552 processed_buf
, 100 * fraction_done
);
7556 if (total_secs_left
!= UINT64_MAX
&&
7557 issue_rate
>= 10 * 1024 * 1024) {
7558 (void) printf(gettext(", %s to go\n"), time_buf
);
7560 (void) printf(gettext(", no estimated "
7561 "completion time\n"));
7564 (void) printf(gettext("\n"));
7569 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, char *vdev_name
)
7571 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
7575 printf_color(ANSI_BOLD
, gettext("scan:"));
7578 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
7579 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
7580 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
7581 uint64_t bytes_est
= vrs
->vrs_bytes_est
;
7582 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
7583 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7584 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
7585 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
7586 double scan_pct
= MIN((double)bytes_scanned
* 100 /
7587 (bytes_est
+ 1), 100);
7589 /* Format all of the numbers we will be reporting */
7590 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
7591 char bytes_rebuilt_buf
[7], bytes_est_buf
[7];
7592 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
7593 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
7594 sizeof (bytes_scanned_buf
));
7595 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
7596 sizeof (bytes_issued_buf
));
7597 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
7598 sizeof (bytes_rebuilt_buf
));
7599 zfs_nicebytes(bytes_est
, bytes_est_buf
, sizeof (bytes_est_buf
));
7600 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
7601 zfs_nicebytes(issue_rate
, issue_rate_buf
, sizeof (issue_rate_buf
));
7603 time_t start
= vrs
->vrs_start_time
;
7604 time_t end
= vrs
->vrs_end_time
;
7606 /* Rebuild is finished or canceled. */
7607 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
7608 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
7609 (void) printf(gettext("resilvered (%s) %s in %s "
7610 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
7611 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
7613 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
7614 (void) printf(gettext("resilver (%s) canceled on %s"),
7615 vdev_name
, ctime(&end
));
7617 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7618 (void) printf(gettext("resilver (%s) in progress since %s"),
7619 vdev_name
, ctime(&start
));
7622 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
7624 secs_to_dhms(MAX((int64_t)bytes_est
- (int64_t)bytes_scanned
, 0) /
7625 MAX(scan_rate
, 1), time_buf
);
7627 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7628 "%s total\n"), bytes_scanned_buf
, scan_rate_buf
,
7629 bytes_issued_buf
, issue_rate_buf
, bytes_est_buf
);
7630 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7631 bytes_rebuilt_buf
, scan_pct
);
7633 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7634 if (scan_rate
>= 10 * 1024 * 1024) {
7635 (void) printf(gettext(", %s to go\n"), time_buf
);
7637 (void) printf(gettext(", no estimated "
7638 "completion time\n"));
7641 (void) printf(gettext("\n"));
7646 * Print rebuild status for top-level vdevs.
7649 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7654 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7655 &child
, &children
) != 0)
7658 for (uint_t c
= 0; c
< children
; c
++) {
7659 vdev_rebuild_stat_t
*vrs
;
7662 if (nvlist_lookup_uint64_array(child
[c
],
7663 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7664 char *name
= zpool_vdev_name(g_zfs
, zhp
,
7665 child
[c
], VDEV_NAME_TYPE_ID
);
7666 print_rebuild_status_impl(vrs
, name
);
7673 * As we don't scrub checkpointed blocks, we want to warn the user that we
7674 * skipped scanning some blocks if a checkpoint exists or existed at any
7675 * time during the scan. If a sequential instead of healing reconstruction
7676 * was performed then the blocks were reconstructed. However, their checksums
7677 * have not been verified so we still print the warning.
7680 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
7682 if (ps
== NULL
|| pcs
== NULL
)
7685 if (pcs
->pcs_state
== CS_NONE
||
7686 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
7689 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
7691 if (ps
->pss_state
== DSS_NONE
)
7694 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
7695 ps
->pss_end_time
< pcs
->pcs_start_time
)
7698 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
7699 (void) printf(gettext(" scan warning: skipped blocks "
7700 "that are only referenced by the checkpoint.\n"));
7702 assert(ps
->pss_state
== DSS_SCANNING
);
7703 (void) printf(gettext(" scan warning: skipping blocks "
7704 "that are only referenced by the checkpoint.\n"));
7709 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7710 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7711 * the last completed (or cancelled) rebuild.
7714 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
7718 boolean_t rebuilding
= B_FALSE
;
7719 uint64_t end_time
= 0;
7721 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7722 &child
, &children
) != 0)
7725 for (uint_t c
= 0; c
< children
; c
++) {
7726 vdev_rebuild_stat_t
*vrs
;
7729 if (nvlist_lookup_uint64_array(child
[c
],
7730 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
7732 if (vrs
->vrs_end_time
> end_time
)
7733 end_time
= vrs
->vrs_end_time
;
7735 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
7736 rebuilding
= B_TRUE
;
7743 if (rebuild_end_time
!= NULL
)
7744 *rebuild_end_time
= end_time
;
7746 return (rebuilding
);
7750 * Print the scan status.
7753 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
7755 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
7756 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
7757 boolean_t active_resilver
= B_FALSE
;
7758 pool_checkpoint_stat_t
*pcs
= NULL
;
7759 pool_scan_stat_t
*ps
= NULL
;
7762 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
7763 (uint64_t **)&ps
, &c
) == 0) {
7764 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
7765 resilver_end_time
= ps
->pss_end_time
;
7766 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
7769 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
7770 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
7773 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
7774 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
7776 /* Always print the scrub status when available. */
7778 print_scan_scrub_resilver_status(ps
);
7781 * When there is an active resilver or rebuild print its status.
7782 * Otherwise print the status of the last resilver or rebuild.
7784 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
7785 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
7786 print_scan_scrub_resilver_status(ps
);
7787 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
7788 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
7789 print_rebuild_status(zhp
, nvroot
);
7792 (void) nvlist_lookup_uint64_array(nvroot
,
7793 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7794 print_checkpoint_scan_warning(ps
, pcs
);
7798 * Print out detailed removal status.
7801 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
7803 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
7805 nvlist_t
*config
, *nvroot
;
7810 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
7814 * Determine name of vdev.
7816 config
= zpool_get_config(zhp
, NULL
);
7817 nvroot
= fnvlist_lookup_nvlist(config
,
7818 ZPOOL_CONFIG_VDEV_TREE
);
7819 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
7820 &child
, &children
) == 0);
7821 assert(prs
->prs_removing_vdev
< children
);
7822 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
7823 child
[prs
->prs_removing_vdev
], B_TRUE
);
7825 printf_color(ANSI_BOLD
, gettext("remove: "));
7827 start
= prs
->prs_start_time
;
7828 end
= prs
->prs_end_time
;
7829 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
7832 * Removal is finished or canceled.
7834 if (prs
->prs_state
== DSS_FINISHED
) {
7835 uint64_t minutes_taken
= (end
- start
) / 60;
7837 (void) printf(gettext("Removal of vdev %llu copied %s "
7838 "in %lluh%um, completed on %s"),
7839 (longlong_t
)prs
->prs_removing_vdev
,
7841 (u_longlong_t
)(minutes_taken
/ 60),
7842 (uint_t
)(minutes_taken
% 60),
7843 ctime((time_t *)&end
));
7844 } else if (prs
->prs_state
== DSS_CANCELED
) {
7845 (void) printf(gettext("Removal of %s canceled on %s"),
7846 vdev_name
, ctime(&end
));
7848 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
7849 double fraction_done
;
7852 assert(prs
->prs_state
== DSS_SCANNING
);
7855 * Removal is in progress.
7857 (void) printf(gettext(
7858 "Evacuation of %s in progress since %s"),
7859 vdev_name
, ctime(&start
));
7861 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
7862 total
= prs
->prs_to_copy
;
7863 fraction_done
= (double)copied
/ total
;
7865 /* elapsed time for this pass */
7866 elapsed
= time(NULL
) - prs
->prs_start_time
;
7867 elapsed
= elapsed
> 0 ? elapsed
: 1;
7868 rate
= copied
/ elapsed
;
7869 rate
= rate
> 0 ? rate
: 1;
7870 mins_left
= ((total
- copied
) / rate
) / 60;
7871 hours_left
= mins_left
/ 60;
7873 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
7874 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
7875 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
7878 * do not print estimated time if hours_left is more than
7881 (void) printf(gettext(
7882 "\t%s copied out of %s at %s/s, %.2f%% done"),
7883 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
7884 if (hours_left
< (30 * 24)) {
7885 (void) printf(gettext(", %lluh%um to go\n"),
7886 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
7888 (void) printf(gettext(
7889 ", (copy is slow, no estimated time)\n"));
7894 if (prs
->prs_mapping_memory
> 0) {
7896 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
7897 (void) printf(gettext(
7898 "\t%s memory used for removed device mappings\n"),
7904 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
7909 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7912 (void) printf(gettext("checkpoint: "));
7914 start
= pcs
->pcs_start_time
;
7915 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
7917 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
7918 char *date
= ctime(&start
);
7921 * ctime() adds a newline at the end of the generated
7922 * string, thus the weird format specifier and the
7923 * strlen() call used to chop it off from the output.
7925 (void) printf(gettext("created %.*s, consumes %s\n"),
7926 (int)(strlen(date
) - 1), date
, space_buf
);
7930 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7932 (void) printf(gettext("discarding, %s remaining.\n"),
7937 print_error_log(zpool_handle_t
*zhp
)
7939 nvlist_t
*nverrlist
= NULL
;
7942 size_t len
= MAXPATHLEN
* 2;
7944 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
7947 (void) printf("errors: Permanent errors have been "
7948 "detected in the following files:\n\n");
7950 pathname
= safe_malloc(len
);
7952 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
7954 uint64_t dsobj
, obj
;
7956 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
7957 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
7959 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
7961 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
7962 (void) printf("%7s %s\n", "", pathname
);
7965 nvlist_free(nverrlist
);
7969 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
7978 (void) printf(gettext("\tspares\n"));
7980 for (i
= 0; i
< nspares
; i
++) {
7981 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
7983 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
7989 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
7998 (void) printf(gettext("\tcache\n"));
8000 for (i
= 0; i
< nl2cache
; i
++) {
8001 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
8003 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
8010 print_dedup_stats(nvlist_t
*config
)
8012 ddt_histogram_t
*ddh
;
8016 char dspace
[6], mspace
[6];
8019 * If the pool was faulted then we may not have been able to
8020 * obtain the config. Otherwise, if we have anything in the dedup
8021 * table continue processing the stats.
8023 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
8024 (uint64_t **)&ddo
, &c
) != 0)
8027 (void) printf("\n");
8028 (void) printf(gettext(" dedup: "));
8029 if (ddo
->ddo_count
== 0) {
8030 (void) printf(gettext("no DDT entries\n"));
8034 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
8035 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
8036 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8037 (u_longlong_t
)ddo
->ddo_count
,
8041 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
8042 (uint64_t **)&dds
, &c
) == 0);
8043 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
8044 (uint64_t **)&ddh
, &c
) == 0);
8045 zpool_dump_ddt(dds
, ddh
);
8049 * Display a summary of pool status. Displays a summary such as:
8053 * reason: One or more devices ...
8054 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8060 * When given the '-v' option, we print out the complete config. If the '-e'
8061 * option is specified, then we print out error rate information as well.
8064 status_callback(zpool_handle_t
*zhp
, void *data
)
8066 status_cbdata_t
*cbp
= data
;
8067 nvlist_t
*config
, *nvroot
;
8069 zpool_status_t reason
;
8070 zpool_errata_t errata
;
8075 config
= zpool_get_config(zhp
, NULL
);
8076 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
8081 * If we were given 'zpool status -x', only report those pools with
8084 if (cbp
->cb_explain
&&
8085 (reason
== ZPOOL_STATUS_OK
||
8086 reason
== ZPOOL_STATUS_VERSION_OLDER
||
8087 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
8088 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
8089 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
8090 if (!cbp
->cb_allpools
) {
8091 (void) printf(gettext("pool '%s' is healthy\n"),
8092 zpool_get_name(zhp
));
8094 cbp
->cb_first
= B_FALSE
;
8100 cbp
->cb_first
= B_FALSE
;
8102 (void) printf("\n");
8104 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8105 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8106 (uint64_t **)&vs
, &c
) == 0);
8108 health
= zpool_get_state_str(zhp
);
8111 printf_color(ANSI_BOLD
, gettext("pool:"));
8112 printf(" %s\n", zpool_get_name(zhp
));
8114 printf_color(ANSI_BOLD
, gettext("state: "));
8116 printf_color(health_str_to_color(health
), "%s", health
);
8121 case ZPOOL_STATUS_MISSING_DEV_R
:
8122 printf_color(ANSI_BOLD
, gettext("status: "));
8123 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8124 "not be opened. Sufficient replicas exist for\n\tthe pool "
8125 "to continue functioning in a degraded state.\n"));
8126 printf_color(ANSI_BOLD
, gettext("action: "));
8127 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8128 "and online it using 'zpool online'.\n"));
8131 case ZPOOL_STATUS_MISSING_DEV_NR
:
8132 printf_color(ANSI_BOLD
, gettext("status: "));
8133 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8134 "not be opened. There are insufficient\n\treplicas for the"
8135 " pool to continue functioning.\n"));
8136 printf_color(ANSI_BOLD
, gettext("action: "));
8137 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8138 "and online it using 'zpool online'.\n"));
8141 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8142 printf_color(ANSI_BOLD
, gettext("status: "));
8143 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8144 "not be used because the label is missing or\n\tinvalid. "
8145 "Sufficient replicas exist for the pool to continue\n\t"
8146 "functioning in a degraded state.\n"));
8147 printf_color(ANSI_BOLD
, gettext("action: "));
8148 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8149 "'zpool replace'.\n"));
8152 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8153 printf_color(ANSI_BOLD
, gettext("status: "));
8154 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8155 "not be used because the label is missing \n\tor invalid. "
8156 "There are insufficient replicas for the pool to "
8157 "continue\n\tfunctioning.\n"));
8158 zpool_explain_recover(zpool_get_handle(zhp
),
8159 zpool_get_name(zhp
), reason
, config
);
8162 case ZPOOL_STATUS_FAILING_DEV
:
8163 printf_color(ANSI_BOLD
, gettext("status: "));
8164 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8165 "experienced an unrecoverable error. An\n\tattempt was "
8166 "made to correct the error. Applications are "
8168 printf_color(ANSI_BOLD
, gettext("action: "));
8169 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8170 "device needs to be replaced, and clear the errors\n\tusing"
8171 " 'zpool clear' or replace the device with 'zpool "
8175 case ZPOOL_STATUS_OFFLINE_DEV
:
8176 printf_color(ANSI_BOLD
, gettext("status: "));
8177 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8178 "been taken offline by the administrator.\n\tSufficient "
8179 "replicas exist for the pool to continue functioning in "
8180 "a\n\tdegraded state.\n"));
8181 printf_color(ANSI_BOLD
, gettext("action: "));
8182 printf_color(ANSI_YELLOW
, gettext("Online the device "
8183 "using 'zpool online' or replace the device with\n\t'zpool "
8187 case ZPOOL_STATUS_REMOVED_DEV
:
8188 printf_color(ANSI_BOLD
, gettext("status: "));
8189 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8190 "been removed by the administrator.\n\tSufficient "
8191 "replicas exist for the pool to continue functioning in "
8192 "a\n\tdegraded state.\n"));
8193 printf_color(ANSI_BOLD
, gettext("action: "));
8194 printf_color(ANSI_YELLOW
, gettext("Online the device "
8195 "using zpool online' or replace the device with\n\t'zpool "
8199 case ZPOOL_STATUS_RESILVERING
:
8200 case ZPOOL_STATUS_REBUILDING
:
8201 printf_color(ANSI_BOLD
, gettext("status: "));
8202 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8203 "currently being resilvered. The pool will\n\tcontinue "
8204 "to function, possibly in a degraded state.\n"));
8205 printf_color(ANSI_BOLD
, gettext("action: "));
8206 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8210 case ZPOOL_STATUS_REBUILD_SCRUB
:
8211 printf_color(ANSI_BOLD
, gettext("status: "));
8212 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8213 "been sequentially resilvered, scrubbing\n\tthe pool "
8214 "is recommended.\n"));
8215 printf_color(ANSI_BOLD
, gettext("action: "));
8216 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8217 "verify all data checksums.\n"));
8220 case ZPOOL_STATUS_CORRUPT_DATA
:
8221 printf_color(ANSI_BOLD
, gettext("status: "));
8222 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8223 "experienced an error resulting in data\n\tcorruption. "
8224 "Applications may be affected.\n"));
8225 printf_color(ANSI_BOLD
, gettext("action: "));
8226 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8227 " if possible. Otherwise restore the\n\tentire pool from "
8231 case ZPOOL_STATUS_CORRUPT_POOL
:
8232 printf_color(ANSI_BOLD
, gettext("status: "));
8233 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8234 "corrupted and the pool cannot be opened.\n"));
8235 zpool_explain_recover(zpool_get_handle(zhp
),
8236 zpool_get_name(zhp
), reason
, config
);
8239 case ZPOOL_STATUS_VERSION_OLDER
:
8240 printf_color(ANSI_BOLD
, gettext("status: "));
8241 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8242 "a legacy on-disk format. The pool can\n\tstill be used, "
8243 "but some features are unavailable.\n"));
8244 printf_color(ANSI_BOLD
, gettext("action: "));
8245 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8246 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8247 "longer be accessible on software that does not support\n\t"
8248 "feature flags.\n"));
8251 case ZPOOL_STATUS_VERSION_NEWER
:
8252 printf_color(ANSI_BOLD
, gettext("status: "));
8253 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8254 "to a newer, incompatible on-disk version.\n\tThe pool "
8255 "cannot be accessed on this system.\n"));
8256 printf_color(ANSI_BOLD
, gettext("action: "));
8257 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8258 "system running more recent software, or\n\trestore the "
8259 "pool from backup.\n"));
8262 case ZPOOL_STATUS_FEAT_DISABLED
:
8263 printf_color(ANSI_BOLD
, gettext("status: "));
8264 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8265 "requested features are not enabled on the pool.\n\t"
8266 "The pool can still be used, but some features are "
8268 printf_color(ANSI_BOLD
, gettext("action: "));
8269 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8270 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8271 "longer be accessible by software that does not support\n\t"
8272 "the features. See zpool-features(5) for details.\n"));
8275 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8276 printf_color(ANSI_BOLD
, gettext("status: "));
8277 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8278 "compatibility list specified, but it could not be\n\t"
8279 "read/parsed at this time. The pool can still be used, "
8280 "but this\n\tshould be investigated.\n"));
8281 printf_color(ANSI_BOLD
, gettext("action: "));
8282 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8283 "'compatibility' property against the\n\t"
8284 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8285 ZPOOL_DATA_COMPAT_D
".\n"));
8288 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
8289 printf_color(ANSI_BOLD
, gettext("status: "));
8290 printf_color(ANSI_YELLOW
, gettext("One or more features "
8291 "are enabled on the pool despite not being\n\t"
8292 "requested by the 'compatibility' property.\n"));
8293 printf_color(ANSI_BOLD
, gettext("action: "));
8294 printf_color(ANSI_YELLOW
, gettext("Consider setting "
8295 "'compatibility' to an appropriate value, or\n\t"
8296 "adding needed features to the relevant file in\n\t"
8297 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
8300 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8301 printf_color(ANSI_BOLD
, gettext("status: "));
8302 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8303 "on this system because it uses the\n\tfollowing feature(s)"
8304 " not supported on this system:\n"));
8305 zpool_print_unsup_feat(config
);
8306 (void) printf("\n");
8307 printf_color(ANSI_BOLD
, gettext("action: "));
8308 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8309 "system that supports the required feature(s),\n\tor "
8310 "restore the pool from backup.\n"));
8313 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8314 printf_color(ANSI_BOLD
, gettext("status: "));
8315 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8316 "accessed in read-only mode on this system. It\n\tcannot be"
8317 " accessed in read-write mode because it uses the "
8318 "following\n\tfeature(s) not supported on this system:\n"));
8319 zpool_print_unsup_feat(config
);
8320 (void) printf("\n");
8321 printf_color(ANSI_BOLD
, gettext("action: "));
8322 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8323 "in read-write mode. Import the pool with\n"
8324 "\t\"-o readonly=on\", access the pool from a system that "
8325 "supports the\n\trequired feature(s), or restore the "
8326 "pool from backup.\n"));
8329 case ZPOOL_STATUS_FAULTED_DEV_R
:
8330 printf_color(ANSI_BOLD
, gettext("status: "));
8331 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8332 "faulted in response to persistent errors.\n\tSufficient "
8333 "replicas exist for the pool to continue functioning "
8334 "in a\n\tdegraded state.\n"));
8335 printf_color(ANSI_BOLD
, gettext("action: "));
8336 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8337 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8340 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8341 printf_color(ANSI_BOLD
, gettext("status: "));
8342 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8343 "faulted in response to persistent errors. There are "
8344 "insufficient replicas for the pool to\n\tcontinue "
8346 printf_color(ANSI_BOLD
, gettext("action: "));
8347 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8348 "pool from a backup source. Manually marking the device\n"
8349 "\trepaired using 'zpool clear' may allow some data "
8350 "to be recovered.\n"));
8353 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8354 printf_color(ANSI_BOLD
, gettext("status: "));
8355 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8356 "because multihost writes failed or were delayed;\n\t"
8357 "another system could import the pool undetected.\n"));
8358 printf_color(ANSI_BOLD
, gettext("action: "));
8359 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8360 " are connected, then reboot your system and\n\timport the "
8364 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8365 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8366 printf_color(ANSI_BOLD
, gettext("status: "));
8367 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8368 "faulted in response to IO failures.\n"));
8369 printf_color(ANSI_BOLD
, gettext("action: "));
8370 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8371 "devices are connected, then run 'zpool clear'.\n"));
8374 case ZPOOL_STATUS_BAD_LOG
:
8375 printf_color(ANSI_BOLD
, gettext("status: "));
8376 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8377 "could not be read.\n"
8378 "\tWaiting for administrator intervention to fix the "
8379 "faulted pool.\n"));
8380 printf_color(ANSI_BOLD
, gettext("action: "));
8381 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8382 "device(s) and run 'zpool online',\n"
8383 "\tor ignore the intent log records by running "
8384 "'zpool clear'.\n"));
8387 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
8388 (void) printf(gettext("status: One or more devices are "
8389 "configured to use a non-native block size.\n"
8390 "\tExpect reduced performance.\n"));
8391 (void) printf(gettext("action: Replace affected devices with "
8392 "devices that support the\n\tconfigured block size, or "
8393 "migrate data to a properly configured\n\tpool.\n"));
8396 case ZPOOL_STATUS_HOSTID_MISMATCH
:
8397 printf_color(ANSI_BOLD
, gettext("status: "));
8398 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
8399 " and system hostid on imported pool.\n\tThis pool was "
8400 "previously imported into a system with a different "
8401 "hostid,\n\tand then was verbatim imported into this "
8403 printf_color(ANSI_BOLD
, gettext("action: "));
8404 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
8405 "systems on which it is imported.\n"
8406 "\tThen import it to correct the mismatch.\n"));
8409 case ZPOOL_STATUS_ERRATA
:
8410 printf_color(ANSI_BOLD
, gettext("status: "));
8411 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
8415 case ZPOOL_ERRATA_NONE
:
8418 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
8419 printf_color(ANSI_BOLD
, gettext("action: "));
8420 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8421 " run 'zpool scrub'.\n"));
8424 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
8425 (void) printf(gettext("\tExisting encrypted datasets "
8426 "contain an on-disk incompatibility\n\twhich "
8427 "needs to be corrected.\n"));
8428 printf_color(ANSI_BOLD
, gettext("action: "));
8429 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
8430 " backup existing encrypted datasets to new\n\t"
8431 "encrypted datasets and destroy the old ones. "
8432 "'zfs mount -o ro' can\n\tbe used to temporarily "
8433 "mount existing encrypted datasets readonly.\n"));
8436 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
8437 (void) printf(gettext("\tExisting encrypted snapshots "
8438 "and bookmarks contain an on-disk\n\tincompat"
8439 "ibility. This may cause on-disk corruption if "
8440 "they are used\n\twith 'zfs recv'.\n"));
8441 printf_color(ANSI_BOLD
, gettext("action: "));
8442 printf_color(ANSI_YELLOW
, gettext("To correct the"
8443 "issue, enable the bookmark_v2 feature. No "
8444 "additional\n\taction is needed if there are no "
8445 "encrypted snapshots or bookmarks.\n\tIf preserving"
8446 "the encrypted snapshots and bookmarks is required,"
8447 " use\n\ta non-raw send to backup and restore them."
8448 " Alternately, they may be\n\tremoved to resolve "
8449 "the incompatibility.\n"));
8454 * All errata which allow the pool to be imported
8455 * must contain an action message.
8463 * The remaining errors can't actually be generated, yet.
8465 assert(reason
== ZPOOL_STATUS_OK
);
8468 if (msgid
!= NULL
) {
8470 printf_color(ANSI_BOLD
, gettext("see:"));
8472 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8476 if (config
!= NULL
) {
8478 nvlist_t
**spares
, **l2cache
;
8479 uint_t nspares
, nl2cache
;
8480 pool_checkpoint_stat_t
*pcs
= NULL
;
8481 pool_removal_stat_t
*prs
= NULL
;
8483 print_scan_status(zhp
, nvroot
);
8485 (void) nvlist_lookup_uint64_array(nvroot
,
8486 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
8487 print_removal_status(zhp
, prs
);
8489 (void) nvlist_lookup_uint64_array(nvroot
,
8490 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8491 print_checkpoint_status(pcs
);
8493 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
8494 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
8495 if (cbp
->cb_namewidth
< 10)
8496 cbp
->cb_namewidth
= 10;
8498 color_start(ANSI_BOLD
);
8499 (void) printf(gettext("config:\n\n"));
8500 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8501 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
8505 if (cbp
->cb_print_slow_ios
) {
8506 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
8509 if (cbp
->vcdl
!= NULL
)
8510 print_cmd_columns(cbp
->vcdl
, 0);
8514 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
8517 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
8518 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
8519 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
8521 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
8522 &l2cache
, &nl2cache
) == 0)
8523 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
8525 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
8526 &spares
, &nspares
) == 0)
8527 print_spares(zhp
, cbp
, spares
, nspares
);
8529 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
8531 nvlist_t
*nverrlist
= NULL
;
8534 * If the approximate error count is small, get a
8535 * precise count by fetching the entire log and
8536 * uniquifying the results.
8538 if (nerr
> 0 && nerr
< 100 && !cbp
->cb_verbose
&&
8539 zpool_get_errlog(zhp
, &nverrlist
) == 0) {
8544 while ((elem
= nvlist_next_nvpair(nverrlist
,
8549 nvlist_free(nverrlist
);
8551 (void) printf("\n");
8554 (void) printf(gettext("errors: No known data "
8556 else if (!cbp
->cb_verbose
)
8557 (void) printf(gettext("errors: %llu data "
8558 "errors, use '-v' for a list\n"),
8559 (u_longlong_t
)nerr
);
8561 print_error_log(zhp
);
8564 if (cbp
->cb_dedup_stats
)
8565 print_dedup_stats(config
);
8567 (void) printf(gettext("config: The configuration cannot be "
8575 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8576 * [interval [count]]
8578 * -c CMD For each vdev, run command CMD
8579 * -i Display vdev initialization status.
8580 * -g Display guid for individual vdev name.
8581 * -L Follow links when resolving vdev path name.
8582 * -p Display values in parsable (exact) format.
8583 * -P Display full path for vdev name.
8584 * -s Display slow IOs column.
8585 * -v Display complete error logs
8586 * -x Display only pools with potential problems
8587 * -D Display dedup status (undocumented)
8588 * -t Display vdev TRIM status.
8589 * -T Display a timestamp in date(1) or Unix format
8591 * Describes the health status of all pools or some subset.
8594 zpool_do_status(int argc
, char **argv
)
8599 unsigned long count
= 0;
8600 status_cbdata_t cb
= { 0 };
8604 while ((c
= getopt(argc
, argv
, "c:igLpPsvxDtT:")) != -1) {
8609 gettext("Can't set -c flag twice\n"));
8613 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
8614 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8615 fprintf(stderr
, gettext(
8616 "Can't run -c, disabled by "
8617 "ZPOOL_SCRIPTS_ENABLED.\n"));
8621 if ((getuid() <= 0 || geteuid() <= 0) &&
8622 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8623 fprintf(stderr
, gettext(
8624 "Can't run -c with root privileges "
8625 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8631 cb
.cb_print_vdev_init
= B_TRUE
;
8634 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
8637 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
8640 cb
.cb_literal
= B_TRUE
;
8643 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
8646 cb
.cb_print_slow_ios
= B_TRUE
;
8649 cb
.cb_verbose
= B_TRUE
;
8652 cb
.cb_explain
= B_TRUE
;
8655 cb
.cb_dedup_stats
= B_TRUE
;
8658 cb
.cb_print_vdev_trim
= B_TRUE
;
8661 get_timestamp_arg(*optarg
);
8664 if (optopt
== 'c') {
8665 print_zpool_script_list("status");
8669 gettext("invalid option '%c'\n"), optopt
);
8678 get_interval_count(&argc
, argv
, &interval
, &count
);
8681 cb
.cb_allpools
= B_TRUE
;
8683 cb
.cb_first
= B_TRUE
;
8684 cb
.cb_print_status
= B_TRUE
;
8687 if (timestamp_fmt
!= NODATE
)
8688 print_timestamp(timestamp_fmt
);
8691 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
8694 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, cb
.cb_literal
,
8695 status_callback
, &cb
);
8697 if (cb
.vcdl
!= NULL
)
8698 free_vdev_cmd_data_list(cb
.vcdl
);
8700 if (argc
== 0 && cb
.cb_count
== 0)
8701 (void) fprintf(stderr
, gettext("no pools available\n"));
8702 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
8703 (void) printf(gettext("all pools are healthy\n"));
8711 if (count
!= 0 && --count
== 0)
8714 (void) fsleep(interval
);
8720 typedef struct upgrade_cbdata
{
8723 uint64_t cb_version
;
8728 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
8730 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
8731 int *count
= (int *)unsupp_fs
;
8733 if (zfs_version
> ZPL_VERSION
) {
8734 (void) printf(gettext("%s (v%d) is not supported by this "
8735 "implementation of ZFS.\n"),
8736 zfs_get_name(zhp
), zfs_version
);
8740 zfs_iter_filesystems(zhp
, check_unsupp_fs
, unsupp_fs
);
8748 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
8752 uint64_t oldversion
;
8755 config
= zpool_get_config(zhp
, NULL
);
8756 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8759 char compat
[ZFS_MAXPROPLEN
];
8760 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8761 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8764 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
8765 assert(oldversion
< version
);
8767 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
8772 (void) fprintf(stderr
, gettext("Upgrade not performed due "
8773 "to %d unsupported filesystems (max v%d).\n"),
8774 unsupp_fs
, (int)ZPL_VERSION
);
8778 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
8779 (void) fprintf(stderr
, gettext("Upgrade not performed because "
8780 "'compatibility' property set to '"
8781 ZPOOL_COMPAT_LEGACY
"'.\n"));
8785 ret
= zpool_upgrade(zhp
, version
);
8789 if (version
>= SPA_VERSION_FEATURES
) {
8790 (void) printf(gettext("Successfully upgraded "
8791 "'%s' from version %llu to feature flags.\n"),
8792 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
8794 (void) printf(gettext("Successfully upgraded "
8795 "'%s' from version %llu to version %llu.\n"),
8796 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
8797 (u_longlong_t
)version
);
8804 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
8807 boolean_t firstff
= B_TRUE
;
8808 nvlist_t
*enabled
= zpool_get_features(zhp
);
8810 char compat
[ZFS_MAXPROPLEN
];
8811 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
8812 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
8815 boolean_t requested_features
[SPA_FEATURES
];
8816 if (zpool_do_load_compat(compat
, requested_features
) !=
8817 ZPOOL_COMPATIBILITY_OK
)
8821 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8822 const char *fname
= spa_feature_table
[i
].fi_uname
;
8823 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8825 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8828 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
8830 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
8831 ret
= zpool_set_prop(zhp
, propname
,
8832 ZFS_FEATURE_ENABLED
);
8840 (void) printf(gettext("Enabled the "
8841 "following features on '%s':\n"),
8842 zpool_get_name(zhp
));
8845 (void) printf(gettext(" %s\n"), fname
);
8856 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
8858 upgrade_cbdata_t
*cbp
= arg
;
8861 boolean_t printnl
= B_FALSE
;
8864 config
= zpool_get_config(zhp
, NULL
);
8865 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8868 assert(SPA_VERSION_IS_SUPPORTED(version
));
8870 if (version
< cbp
->cb_version
) {
8871 cbp
->cb_first
= B_FALSE
;
8872 ret
= upgrade_version(zhp
, cbp
->cb_version
);
8878 * If they did "zpool upgrade -a", then we could
8879 * be doing ioctls to different pools. We need
8880 * to log this history once to each pool, and bypass
8881 * the normal history logging that happens in main().
8883 (void) zpool_log_history(g_zfs
, history_str
);
8884 log_history
= B_FALSE
;
8887 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
8889 ret
= upgrade_enable_all(zhp
, &count
);
8894 cbp
->cb_first
= B_FALSE
;
8900 (void) printf(gettext("\n"));
8907 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
8909 upgrade_cbdata_t
*cbp
= arg
;
8913 config
= zpool_get_config(zhp
, NULL
);
8914 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8917 assert(SPA_VERSION_IS_SUPPORTED(version
));
8919 if (version
< SPA_VERSION_FEATURES
) {
8920 if (cbp
->cb_first
) {
8921 (void) printf(gettext("The following pools are "
8922 "formatted with legacy version numbers and can\n"
8923 "be upgraded to use feature flags. After "
8924 "being upgraded, these pools\nwill no "
8925 "longer be accessible by software that does not "
8926 "support feature\nflags.\n\n"
8927 "Note that setting a pool's 'compatibility' "
8928 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
8929 "inhibit upgrades.\n\n"));
8930 (void) printf(gettext("VER POOL\n"));
8931 (void) printf(gettext("--- ------------\n"));
8932 cbp
->cb_first
= B_FALSE
;
8935 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
8936 zpool_get_name(zhp
));
8943 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
8945 upgrade_cbdata_t
*cbp
= arg
;
8949 config
= zpool_get_config(zhp
, NULL
);
8950 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
8953 if (version
>= SPA_VERSION_FEATURES
) {
8955 boolean_t poolfirst
= B_TRUE
;
8956 nvlist_t
*enabled
= zpool_get_features(zhp
);
8958 for (i
= 0; i
< SPA_FEATURES
; i
++) {
8959 const char *fguid
= spa_feature_table
[i
].fi_guid
;
8960 const char *fname
= spa_feature_table
[i
].fi_uname
;
8962 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
8965 if (!nvlist_exists(enabled
, fguid
)) {
8966 if (cbp
->cb_first
) {
8967 (void) printf(gettext("\nSome "
8968 "supported features are not "
8969 "enabled on the following pools. "
8970 "Once a\nfeature is enabled the "
8971 "pool may become incompatible with "
8972 "software\nthat does not support "
8974 "zpool-features(5) for "
8976 "Note that the pool "
8977 "'compatibility' feature can be "
8978 "used to inhibit\nfeature "
8980 (void) printf(gettext("POOL "
8982 (void) printf(gettext("------"
8984 cbp
->cb_first
= B_FALSE
;
8988 (void) printf(gettext("%s\n"),
8989 zpool_get_name(zhp
));
8990 poolfirst
= B_FALSE
;
8993 (void) printf(gettext(" %s\n"), fname
);
8996 * If they did "zpool upgrade -a", then we could
8997 * be doing ioctls to different pools. We need
8998 * to log this history once to each pool, and bypass
8999 * the normal history logging that happens in main().
9001 (void) zpool_log_history(g_zfs
, history_str
);
9002 log_history
= B_FALSE
;
9011 upgrade_one(zpool_handle_t
*zhp
, void *data
)
9013 boolean_t printnl
= B_FALSE
;
9014 upgrade_cbdata_t
*cbp
= data
;
9015 uint64_t cur_version
;
9018 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
9019 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
9020 "Pool 'log' must be renamed using export and import"
9025 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
9026 if (cur_version
> cbp
->cb_version
) {
9027 (void) printf(gettext("Pool '%s' is already formatted "
9028 "using more current version '%llu'.\n\n"),
9029 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
9033 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
9034 (void) printf(gettext("Pool '%s' is already formatted "
9035 "using version %llu.\n\n"), zpool_get_name(zhp
),
9036 (u_longlong_t
)cbp
->cb_version
);
9040 if (cur_version
!= cbp
->cb_version
) {
9042 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9047 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9049 ret
= upgrade_enable_all(zhp
, &count
);
9055 } else if (cur_version
== SPA_VERSION
) {
9056 (void) printf(gettext("Pool '%s' already has all "
9057 "supported and requested features enabled.\n"),
9058 zpool_get_name(zhp
));
9063 (void) printf(gettext("\n"));
9072 * zpool upgrade [-V version] <-a | pool ...>
9074 * With no arguments, display downrev'd ZFS pool available for upgrade.
9075 * Individual pools can be upgraded by specifying the pool, and '-a' will
9076 * upgrade all pools.
9079 zpool_do_upgrade(int argc
, char **argv
)
9082 upgrade_cbdata_t cb
= { 0 };
9084 boolean_t showversions
= B_FALSE
;
9085 boolean_t upgradeall
= B_FALSE
;
9090 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
9093 upgradeall
= B_TRUE
;
9096 showversions
= B_TRUE
;
9099 cb
.cb_version
= strtoll(optarg
, &end
, 10);
9101 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
9102 (void) fprintf(stderr
,
9103 gettext("invalid version '%s'\n"), optarg
);
9108 (void) fprintf(stderr
, gettext("missing argument for "
9109 "'%c' option\n"), optopt
);
9113 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9124 if (cb
.cb_version
== 0) {
9125 cb
.cb_version
= SPA_VERSION
;
9126 } else if (!upgradeall
&& argc
== 0) {
9127 (void) fprintf(stderr
, gettext("-V option is "
9128 "incompatible with other arguments\n"));
9133 if (upgradeall
|| argc
!= 0) {
9134 (void) fprintf(stderr
, gettext("-v option is "
9135 "incompatible with other arguments\n"));
9138 } else if (upgradeall
) {
9140 (void) fprintf(stderr
, gettext("-a option should not "
9141 "be used along with a pool name\n"));
9146 (void) printf(gettext("This system supports ZFS pool feature "
9151 (void) printf(gettext("The following features are "
9153 (void) printf(gettext("FEAT DESCRIPTION\n"));
9154 (void) printf("----------------------------------------------"
9155 "---------------\n");
9156 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9157 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9158 if (!fi
->fi_zfs_mod_supported
)
9161 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9162 " (read-only compatible)" : "";
9164 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9165 (void) printf(" %s\n", fi
->fi_desc
);
9167 (void) printf("\n");
9169 (void) printf(gettext("The following legacy versions are also "
9171 (void) printf(gettext("VER DESCRIPTION\n"));
9172 (void) printf("--- -----------------------------------------"
9173 "---------------\n");
9174 (void) printf(gettext(" 1 Initial ZFS version\n"));
9175 (void) printf(gettext(" 2 Ditto blocks "
9176 "(replicated metadata)\n"));
9177 (void) printf(gettext(" 3 Hot spares and double parity "
9179 (void) printf(gettext(" 4 zpool history\n"));
9180 (void) printf(gettext(" 5 Compression using the gzip "
9182 (void) printf(gettext(" 6 bootfs pool property\n"));
9183 (void) printf(gettext(" 7 Separate intent log devices\n"));
9184 (void) printf(gettext(" 8 Delegated administration\n"));
9185 (void) printf(gettext(" 9 refquota and refreservation "
9187 (void) printf(gettext(" 10 Cache devices\n"));
9188 (void) printf(gettext(" 11 Improved scrub performance\n"));
9189 (void) printf(gettext(" 12 Snapshot properties\n"));
9190 (void) printf(gettext(" 13 snapused property\n"));
9191 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9192 (void) printf(gettext(" 15 user/group space accounting\n"));
9193 (void) printf(gettext(" 16 stmf property support\n"));
9194 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9195 (void) printf(gettext(" 18 Snapshot user holds\n"));
9196 (void) printf(gettext(" 19 Log device removal\n"));
9197 (void) printf(gettext(" 20 Compression using zle "
9198 "(zero-length encoding)\n"));
9199 (void) printf(gettext(" 21 Deduplication\n"));
9200 (void) printf(gettext(" 22 Received properties\n"));
9201 (void) printf(gettext(" 23 Slim ZIL\n"));
9202 (void) printf(gettext(" 24 System attributes\n"));
9203 (void) printf(gettext(" 25 Improved scrub stats\n"));
9204 (void) printf(gettext(" 26 Improved snapshot deletion "
9206 (void) printf(gettext(" 27 Improved snapshot creation "
9208 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9209 (void) printf(gettext("\nFor more information on a particular "
9210 "version, including supported releases,\n"));
9211 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9212 } else if (argc
== 0 && upgradeall
) {
9213 cb
.cb_first
= B_TRUE
;
9214 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9215 if (ret
== 0 && cb
.cb_first
) {
9216 if (cb
.cb_version
== SPA_VERSION
) {
9217 (void) printf(gettext("All pools are already "
9218 "formatted using feature flags.\n\n"));
9219 (void) printf(gettext("Every feature flags "
9220 "pool already has all supported and "
9221 "requested features enabled.\n"));
9223 (void) printf(gettext("All pools are already "
9224 "formatted with version %llu or higher.\n"),
9225 (u_longlong_t
)cb
.cb_version
);
9228 } else if (argc
== 0) {
9229 cb
.cb_first
= B_TRUE
;
9230 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9234 (void) printf(gettext("All pools are formatted "
9235 "using feature flags.\n\n"));
9237 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9238 "for a list of available legacy versions.\n"));
9241 cb
.cb_first
= B_TRUE
;
9242 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9246 (void) printf(gettext("Every feature flags pool has "
9247 "all supported and requested features enabled.\n"));
9249 (void) printf(gettext("\n"));
9252 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
,
9259 typedef struct hist_cbdata
{
9266 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9272 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9273 &records
, &numrecords
) == 0);
9274 for (i
= 0; i
< numrecords
; i
++) {
9275 nvlist_t
*rec
= records
[i
];
9278 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9282 tsec
= fnvlist_lookup_uint64(records
[i
],
9284 (void) localtime_r(&tsec
, &t
);
9285 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9288 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9289 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9290 ZPOOL_HIST_ELAPSED_NS
);
9291 (void) snprintf(tbuf
+ strlen(tbuf
),
9292 sizeof (tbuf
) - strlen(tbuf
),
9293 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9296 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9297 (void) printf("%s %s", tbuf
,
9298 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9299 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9301 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9304 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9305 (void) printf("%s unrecognized record:\n",
9307 dump_nvlist(rec
, 4);
9310 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9311 zfs_history_event_names
[ievent
],
9312 (longlong_t
)fnvlist_lookup_uint64(
9313 rec
, ZPOOL_HIST_TXG
),
9314 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9315 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9318 (void) printf("%s [txg:%lld] %s", tbuf
,
9319 (longlong_t
)fnvlist_lookup_uint64(
9320 rec
, ZPOOL_HIST_TXG
),
9321 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9322 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9323 (void) printf(" %s (%llu)",
9324 fnvlist_lookup_string(rec
,
9326 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9329 (void) printf(" %s", fnvlist_lookup_string(rec
,
9330 ZPOOL_HIST_INT_STR
));
9331 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9334 (void) printf("%s ioctl %s\n", tbuf
,
9335 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9336 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9337 (void) printf(" input:\n");
9338 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9339 ZPOOL_HIST_INPUT_NVL
), 8);
9341 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9342 (void) printf(" output:\n");
9343 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9344 ZPOOL_HIST_OUTPUT_NVL
), 8);
9346 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9347 (void) printf(" output nvlist omitted; "
9348 "original size: %lldKB\n",
9349 (longlong_t
)fnvlist_lookup_int64(rec
,
9350 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9352 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9353 (void) printf(" errno: %lld\n",
9354 (longlong_t
)fnvlist_lookup_int64(rec
,
9360 (void) printf("%s unrecognized record:\n", tbuf
);
9361 dump_nvlist(rec
, 4);
9365 (void) printf("\n");
9368 (void) printf(" [");
9369 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9370 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9371 struct passwd
*pwd
= getpwuid(who
);
9372 (void) printf("user %d ", (int)who
);
9374 (void) printf("(%s) ", pwd
->pw_name
);
9376 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9377 (void) printf("on %s",
9378 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
9380 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
9381 (void) printf(":%s",
9382 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
9386 (void) printf("\n");
9391 * Print out the command history for a specific pool.
9394 get_history_one(zpool_handle_t
*zhp
, void *data
)
9398 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
9400 boolean_t eof
= B_FALSE
;
9402 cb
->first
= B_FALSE
;
9404 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
9407 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
9410 print_history_records(nvhis
, cb
);
9413 (void) printf("\n");
9419 * zpool history <pool>
9421 * Displays the history of commands that modified pools.
9424 zpool_do_history(int argc
, char **argv
)
9426 hist_cbdata_t cbdata
= { 0 };
9430 cbdata
.first
= B_TRUE
;
9432 while ((c
= getopt(argc
, argv
, "li")) != -1) {
9435 cbdata
.longfmt
= B_TRUE
;
9438 cbdata
.internal
= B_TRUE
;
9441 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9449 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, B_FALSE
, get_history_one
,
9452 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
9453 (void) fprintf(stderr
, gettext("no pools available\n"));
9460 typedef struct ev_opts
{
9465 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
9469 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
9471 char ctime_str
[26], str
[32], *ptr
;
9475 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
9476 memset(str
, ' ', 32);
9477 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
9478 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
9479 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
9480 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
9481 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
9483 (void) printf(gettext("%s\t"), str
);
9485 (void) printf(gettext("%s "), str
);
9487 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
9488 (void) printf(gettext("%s\n"), ptr
);
9492 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
9496 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
9497 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
9499 data_type_t type
= nvpair_type(nvp
);
9500 const char *name
= nvpair_name(nvp
);
9510 printf(gettext("%*s%s = "), depth
, "", name
);
9513 case DATA_TYPE_BOOLEAN
:
9514 printf(gettext("%s"), "1");
9517 case DATA_TYPE_BOOLEAN_VALUE
:
9518 (void) nvpair_value_boolean_value(nvp
, &b
);
9519 printf(gettext("%s"), b
? "1" : "0");
9522 case DATA_TYPE_BYTE
:
9523 (void) nvpair_value_byte(nvp
, &i8
);
9524 printf(gettext("0x%x"), i8
);
9527 case DATA_TYPE_INT8
:
9528 (void) nvpair_value_int8(nvp
, (void *)&i8
);
9529 printf(gettext("0x%x"), i8
);
9532 case DATA_TYPE_UINT8
:
9533 (void) nvpair_value_uint8(nvp
, &i8
);
9534 printf(gettext("0x%x"), i8
);
9537 case DATA_TYPE_INT16
:
9538 (void) nvpair_value_int16(nvp
, (void *)&i16
);
9539 printf(gettext("0x%x"), i16
);
9542 case DATA_TYPE_UINT16
:
9543 (void) nvpair_value_uint16(nvp
, &i16
);
9544 printf(gettext("0x%x"), i16
);
9547 case DATA_TYPE_INT32
:
9548 (void) nvpair_value_int32(nvp
, (void *)&i32
);
9549 printf(gettext("0x%x"), i32
);
9552 case DATA_TYPE_UINT32
:
9553 (void) nvpair_value_uint32(nvp
, &i32
);
9554 printf(gettext("0x%x"), i32
);
9557 case DATA_TYPE_INT64
:
9558 (void) nvpair_value_int64(nvp
, (void *)&i64
);
9559 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9562 case DATA_TYPE_UINT64
:
9563 (void) nvpair_value_uint64(nvp
, &i64
);
9565 * translate vdev state values to readable
9566 * strings to aide zpool events consumers
9569 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
9571 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
9572 printf(gettext("\"%s\" (0x%llx)"),
9573 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
9576 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9580 case DATA_TYPE_HRTIME
:
9581 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
9582 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
9585 case DATA_TYPE_STRING
:
9586 (void) nvpair_value_string(nvp
, &str
);
9587 printf(gettext("\"%s\""), str
? str
: "<NULL>");
9590 case DATA_TYPE_NVLIST
:
9591 printf(gettext("(embedded nvlist)\n"));
9592 (void) nvpair_value_nvlist(nvp
, &cnv
);
9593 zpool_do_events_nvprint(cnv
, depth
+ 8);
9594 printf(gettext("%*s(end %s)"), depth
, "", name
);
9597 case DATA_TYPE_NVLIST_ARRAY
: {
9601 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
9602 printf(gettext("(%d embedded nvlists)\n"), nelem
);
9603 for (i
= 0; i
< nelem
; i
++) {
9604 printf(gettext("%*s%s[%d] = %s\n"),
9605 depth
, "", name
, i
, "(embedded nvlist)");
9606 zpool_do_events_nvprint(val
[i
], depth
+ 8);
9607 printf(gettext("%*s(end %s[%i])\n"),
9608 depth
, "", name
, i
);
9610 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
9614 case DATA_TYPE_INT8_ARRAY
: {
9618 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
9619 for (i
= 0; i
< nelem
; i
++)
9620 printf(gettext("0x%x "), val
[i
]);
9625 case DATA_TYPE_UINT8_ARRAY
: {
9629 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
9630 for (i
= 0; i
< nelem
; i
++)
9631 printf(gettext("0x%x "), val
[i
]);
9636 case DATA_TYPE_INT16_ARRAY
: {
9640 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
9641 for (i
= 0; i
< nelem
; i
++)
9642 printf(gettext("0x%x "), val
[i
]);
9647 case DATA_TYPE_UINT16_ARRAY
: {
9651 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
9652 for (i
= 0; i
< nelem
; i
++)
9653 printf(gettext("0x%x "), val
[i
]);
9658 case DATA_TYPE_INT32_ARRAY
: {
9662 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
9663 for (i
= 0; i
< nelem
; i
++)
9664 printf(gettext("0x%x "), val
[i
]);
9669 case DATA_TYPE_UINT32_ARRAY
: {
9673 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
9674 for (i
= 0; i
< nelem
; i
++)
9675 printf(gettext("0x%x "), val
[i
]);
9680 case DATA_TYPE_INT64_ARRAY
: {
9684 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
9685 for (i
= 0; i
< nelem
; i
++)
9686 printf(gettext("0x%llx "),
9687 (u_longlong_t
)val
[i
]);
9692 case DATA_TYPE_UINT64_ARRAY
: {
9696 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
9697 for (i
= 0; i
< nelem
; i
++)
9698 printf(gettext("0x%llx "),
9699 (u_longlong_t
)val
[i
]);
9704 case DATA_TYPE_STRING_ARRAY
: {
9708 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
9709 for (i
= 0; i
< nelem
; i
++)
9710 printf(gettext("\"%s\" "),
9711 str
[i
] ? str
[i
] : "<NULL>");
9716 case DATA_TYPE_BOOLEAN_ARRAY
:
9717 case DATA_TYPE_BYTE_ARRAY
:
9718 case DATA_TYPE_DOUBLE
:
9719 case DATA_TYPE_DONTCARE
:
9720 case DATA_TYPE_UNKNOWN
:
9721 printf(gettext("<unknown>"));
9725 printf(gettext("\n"));
9730 zpool_do_events_next(ev_opts_t
*opts
)
9733 int zevent_fd
, ret
, dropped
;
9736 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
9737 VERIFY(zevent_fd
>= 0);
9739 if (!opts
->scripted
)
9740 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9743 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
9744 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
9745 if (ret
|| nvl
== NULL
)
9749 (void) printf(gettext("dropped %d events\n"), dropped
);
9751 if (strlen(opts
->poolname
) > 0 &&
9752 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
9753 strcmp(opts
->poolname
, pool
) != 0)
9756 zpool_do_events_short(nvl
, opts
);
9758 if (opts
->verbose
) {
9759 zpool_do_events_nvprint(nvl
, 8);
9760 printf(gettext("\n"));
9762 (void) fflush(stdout
);
9767 VERIFY(0 == close(zevent_fd
));
9773 zpool_do_events_clear(ev_opts_t
*opts
)
9777 ret
= zpool_events_clear(g_zfs
, &count
);
9779 (void) printf(gettext("cleared %d events\n"), count
);
9785 * zpool events [-vHf [pool] | -c]
9787 * Displays events logs by ZFS.
9790 zpool_do_events(int argc
, char **argv
)
9792 ev_opts_t opts
= { 0 };
9797 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
9812 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9821 (void) fprintf(stderr
, gettext("too many arguments\n"));
9823 } else if (argc
== 1) {
9824 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
9825 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
9826 (void) fprintf(stderr
,
9827 gettext("invalid pool name '%s'\n"), opts
.poolname
);
9832 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
9834 (void) fprintf(stderr
,
9835 gettext("invalid options combined with -c\n"));
9840 ret
= zpool_do_events_clear(&opts
);
9842 ret
= zpool_do_events_next(&opts
);
9848 get_callback(zpool_handle_t
*zhp
, void *data
)
9850 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
9851 char value
[MAXNAMELEN
];
9852 zprop_source_t srctype
;
9855 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
9858 * Skip the special fake placeholder. This will also skip
9859 * over the name property when 'all' is specified.
9861 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
9862 pl
== cbp
->cb_proplist
)
9865 if (pl
->pl_prop
== ZPROP_INVAL
&&
9866 (zpool_prop_feature(pl
->pl_user_prop
) ||
9867 zpool_prop_unsupported(pl
->pl_user_prop
))) {
9868 srctype
= ZPROP_SRC_LOCAL
;
9870 if (zpool_prop_get_feature(zhp
, pl
->pl_user_prop
,
9871 value
, sizeof (value
)) == 0) {
9872 zprop_print_one_property(zpool_get_name(zhp
),
9873 cbp
, pl
->pl_user_prop
, value
, srctype
,
9877 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
9878 sizeof (value
), &srctype
, cbp
->cb_literal
) != 0)
9881 zprop_print_one_property(zpool_get_name(zhp
), cbp
,
9882 zpool_prop_to_name(pl
->pl_prop
), value
, srctype
,
9890 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
9892 * -H Scripted mode. Don't display headers, and separate properties
9894 * -o List of columns to display. Defaults to
9895 * "name,property,value,source".
9896 * -p Display values in parsable (exact) format.
9898 * Get properties of pools in the system. Output space statistics
9899 * for each one as well as other attributes.
9902 zpool_do_get(int argc
, char **argv
)
9904 zprop_get_cbdata_t cb
= { 0 };
9905 zprop_list_t fake_name
= { 0 };
9910 cb
.cb_first
= B_TRUE
;
9913 * Set up default columns and sources.
9915 cb
.cb_sources
= ZPROP_SRC_ALL
;
9916 cb
.cb_columns
[0] = GET_COL_NAME
;
9917 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9918 cb
.cb_columns
[2] = GET_COL_VALUE
;
9919 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9920 cb
.cb_type
= ZFS_TYPE_POOL
;
9923 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
9926 cb
.cb_literal
= B_TRUE
;
9929 cb
.cb_scripted
= B_TRUE
;
9932 bzero(&cb
.cb_columns
, sizeof (cb
.cb_columns
));
9934 while (*optarg
!= '\0') {
9935 static char *col_subopts
[] =
9936 { "name", "property", "value", "source",
9939 if (i
== ZFS_GET_NCOLS
) {
9940 (void) fprintf(stderr
, gettext("too "
9941 "many fields given to -o "
9946 switch (getsubopt(&optarg
, col_subopts
,
9949 cb
.cb_columns
[i
++] = GET_COL_NAME
;
9952 cb
.cb_columns
[i
++] = GET_COL_PROPERTY
;
9955 cb
.cb_columns
[i
++] = GET_COL_VALUE
;
9958 cb
.cb_columns
[i
++] = GET_COL_SOURCE
;
9962 (void) fprintf(stderr
,
9963 gettext("\"all\" conflicts "
9964 "with specific fields "
9965 "given to -o option\n"));
9968 cb
.cb_columns
[0] = GET_COL_NAME
;
9969 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
9970 cb
.cb_columns
[2] = GET_COL_VALUE
;
9971 cb
.cb_columns
[3] = GET_COL_SOURCE
;
9975 (void) fprintf(stderr
,
9976 gettext("invalid column name "
9983 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9993 (void) fprintf(stderr
, gettext("missing property "
9998 if (zprop_get_list(g_zfs
, argv
[0], &cb
.cb_proplist
,
9999 ZFS_TYPE_POOL
) != 0)
10005 if (cb
.cb_proplist
!= NULL
) {
10006 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
10007 fake_name
.pl_width
= strlen(gettext("NAME"));
10008 fake_name
.pl_next
= cb
.cb_proplist
;
10009 cb
.cb_proplist
= &fake_name
;
10012 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_literal
,
10013 get_callback
, &cb
);
10015 if (cb
.cb_proplist
== &fake_name
)
10016 zprop_free_list(fake_name
.pl_next
);
10018 zprop_free_list(cb
.cb_proplist
);
10023 typedef struct set_cbdata
{
10026 boolean_t cb_any_successful
;
10030 set_callback(zpool_handle_t
*zhp
, void *data
)
10033 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
10035 /* Check if we have out-of-bounds features */
10036 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
10037 boolean_t features
[SPA_FEATURES
];
10038 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
10039 ZPOOL_COMPATIBILITY_OK
)
10042 nvlist_t
*enabled
= zpool_get_features(zhp
);
10044 for (i
= 0; i
< SPA_FEATURES
; i
++) {
10045 const char *fguid
= spa_feature_table
[i
].fi_guid
;
10046 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
10049 if (i
< SPA_FEATURES
)
10050 (void) fprintf(stderr
, gettext("Warning: one or "
10051 "more features already enabled on pool '%s'\n"
10052 "are not present in this compatibility set.\n"),
10053 zpool_get_name(zhp
));
10056 /* if we're setting a feature, check it's in compatibility set */
10057 if (zpool_prop_feature(cb
->cb_propname
) &&
10058 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
10059 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
10062 if (zfeature_lookup_name(fname
, &f
) == 0) {
10063 char compat
[ZFS_MAXPROPLEN
];
10064 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
10065 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
10068 boolean_t features
[SPA_FEATURES
];
10069 if (zpool_do_load_compat(compat
, features
) !=
10070 ZPOOL_COMPATIBILITY_OK
) {
10071 (void) fprintf(stderr
, gettext("Error: "
10072 "cannot enable feature '%s' on pool '%s'\n"
10073 "because the pool's 'compatibility' "
10074 "property cannot be parsed.\n"),
10075 fname
, zpool_get_name(zhp
));
10079 if (!features
[f
]) {
10080 (void) fprintf(stderr
, gettext("Error: "
10081 "cannot enable feature '%s' on pool '%s'\n"
10082 "as it is not specified in this pool's "
10083 "current compatibility set.\n"
10084 "Consider setting 'compatibility' to a "
10085 "less restrictive set, or to 'off'.\n"),
10086 fname
, zpool_get_name(zhp
));
10092 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
10095 cb
->cb_any_successful
= B_TRUE
;
10101 zpool_do_set(int argc
, char **argv
)
10103 set_cbdata_t cb
= { 0 };
10106 if (argc
> 1 && argv
[1][0] == '-') {
10107 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10113 (void) fprintf(stderr
, gettext("missing property=value "
10119 (void) fprintf(stderr
, gettext("missing pool name\n"));
10124 (void) fprintf(stderr
, gettext("too many pool names\n"));
10128 cb
.cb_propname
= argv
[1];
10129 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
10130 if (cb
.cb_value
== NULL
) {
10131 (void) fprintf(stderr
, gettext("missing value in "
10132 "property=value argument\n"));
10136 *(cb
.cb_value
) = '\0';
10139 error
= for_each_pool(argc
- 2, argv
+ 2, B_TRUE
, NULL
, B_FALSE
,
10140 set_callback
, &cb
);
10145 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10147 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
10149 uint64_t bytes_remaining
;
10151 uint_t c
, children
;
10154 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
10155 activity
== ZPOOL_WAIT_TRIM
);
10157 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
10158 (uint64_t **)&vs
, &c
) == 0);
10160 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
10161 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
10162 bytes_remaining
= vs
->vs_initialize_bytes_est
-
10163 vs
->vs_initialize_bytes_done
;
10164 else if (activity
== ZPOOL_WAIT_TRIM
&&
10165 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
10166 bytes_remaining
= vs
->vs_trim_bytes_est
-
10167 vs
->vs_trim_bytes_done
;
10169 bytes_remaining
= 0;
10171 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10172 &child
, &children
) != 0)
10175 for (c
= 0; c
< children
; c
++)
10176 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
10178 return (bytes_remaining
);
10181 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10183 vdev_activity_top_remaining(nvlist_t
*nv
)
10185 uint64_t bytes_remaining
= 0;
10190 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10191 &child
, &children
) != 0)
10194 for (uint_t c
= 0; c
< children
; c
++) {
10195 vdev_rebuild_stat_t
*vrs
;
10198 error
= nvlist_lookup_uint64_array(child
[c
],
10199 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
10201 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
10202 bytes_remaining
+= (vrs
->vrs_bytes_est
-
10203 vrs
->vrs_bytes_rebuilt
);
10208 return (bytes_remaining
);
10211 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10213 vdev_any_spare_replacing(nvlist_t
*nv
)
10216 uint_t c
, children
;
10219 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
10221 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
10222 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
10223 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
10227 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10228 &child
, &children
) != 0)
10231 for (c
= 0; c
< children
; c
++) {
10232 if (vdev_any_spare_replacing(child
[c
]))
10239 typedef struct wait_data
{
10241 boolean_t wd_scripted
;
10242 boolean_t wd_exact
;
10243 boolean_t wd_headers_once
;
10244 boolean_t wd_should_exit
;
10245 /* Which activities to wait for */
10246 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10248 pthread_cond_t wd_cv
;
10249 pthread_mutex_t wd_mutex
;
10253 * Print to stdout a single line, containing one column for each activity that
10254 * we are waiting for specifying how many bytes of work are left for that
10258 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
10260 nvlist_t
*config
, *nvroot
;
10263 pool_checkpoint_stat_t
*pcs
= NULL
;
10264 pool_scan_stat_t
*pss
= NULL
;
10265 pool_removal_stat_t
*prs
= NULL
;
10266 char *headers
[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
10267 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10268 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
10270 /* Calculate the width of each column */
10271 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10273 * Make sure we have enough space in the col for pretty-printed
10274 * numbers and for the column header, and then leave a couple
10275 * spaces between cols for readability.
10277 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
10280 /* Print header if appropriate */
10281 int term_height
= terminal_height();
10282 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
10283 row
% (term_height
-1) == 0);
10284 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
10285 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10286 if (wd
->wd_enabled
[i
])
10287 (void) printf("%*s", col_widths
[i
], headers
[i
]);
10289 (void) printf("\n");
10292 /* Bytes of work remaining in each activity */
10293 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
10295 bytes_rem
[ZPOOL_WAIT_FREE
] =
10296 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
10298 config
= zpool_get_config(zhp
, NULL
);
10299 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
10301 (void) nvlist_lookup_uint64_array(nvroot
,
10302 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
10303 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
10304 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
10306 (void) nvlist_lookup_uint64_array(nvroot
,
10307 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
10308 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
10309 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
10312 (void) nvlist_lookup_uint64_array(nvroot
,
10313 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
10314 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
10315 pss
->pss_pass_scrub_pause
== 0) {
10316 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
10317 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
10318 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
10320 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
10321 } else if (check_rebuilding(nvroot
, NULL
)) {
10322 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
10323 vdev_activity_top_remaining(nvroot
);
10326 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
10327 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
10328 bytes_rem
[ZPOOL_WAIT_TRIM
] =
10329 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
10332 * A replace finishes after resilvering finishes, so the amount of work
10333 * left for a replace is the same as for resilvering.
10335 * It isn't quite correct to say that if we have any 'spare' or
10336 * 'replacing' vdevs and a resilver is happening, then a replace is in
10337 * progress, like we do here. When a hot spare is used, the faulted vdev
10338 * is not removed after the hot spare is resilvered, so parent 'spare'
10339 * vdev is not removed either. So we could have a 'spare' vdev, but be
10340 * resilvering for a different reason. However, we use it as a heuristic
10341 * because we don't have access to the DTLs, which could tell us whether
10342 * or not we have really finished resilvering a hot spare.
10344 if (vdev_any_spare_replacing(nvroot
))
10345 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
10347 if (timestamp_fmt
!= NODATE
)
10348 print_timestamp(timestamp_fmt
);
10350 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10352 if (!wd
->wd_enabled
[i
])
10356 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
10359 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
10361 if (wd
->wd_scripted
)
10362 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
10364 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
10366 (void) printf("\n");
10367 (void) fflush(stdout
);
10371 wait_status_thread(void *arg
)
10373 wait_data_t
*wd
= (wait_data_t
*)arg
;
10374 zpool_handle_t
*zhp
;
10376 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
10377 return (void *)(1);
10379 for (int row
= 0; ; row
++) {
10381 struct timespec timeout
;
10383 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
10385 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
10386 zpool_props_refresh(zhp
) != 0) {
10388 return (void *)(uintptr_t)(missing
? 0 : 1);
10391 print_wait_status_row(wd
, zhp
, row
);
10393 timeout
.tv_sec
+= floor(wd
->wd_interval
);
10394 long nanos
= timeout
.tv_nsec
+
10395 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
10396 if (nanos
>= NANOSEC
) {
10398 timeout
.tv_nsec
= nanos
- NANOSEC
;
10400 timeout
.tv_nsec
= nanos
;
10402 pthread_mutex_lock(&wd
->wd_mutex
);
10403 if (!wd
->wd_should_exit
)
10404 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
10406 pthread_mutex_unlock(&wd
->wd_mutex
);
10408 break; /* signaled by main thread */
10409 } else if (ret
!= ETIMEDOUT
) {
10410 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
10411 "failed: %s\n"), strerror(ret
));
10413 return (void *)(uintptr_t)(1);
10418 return (void *)(0);
10422 zpool_do_wait(int argc
, char **argv
)
10424 boolean_t verbose
= B_FALSE
;
10428 unsigned long count
;
10429 pthread_t status_thr
;
10431 zpool_handle_t
*zhp
;
10434 wd
.wd_scripted
= B_FALSE
;
10435 wd
.wd_exact
= B_FALSE
;
10436 wd
.wd_headers_once
= B_FALSE
;
10437 wd
.wd_should_exit
= B_FALSE
;
10439 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
10440 pthread_cond_init(&wd
.wd_cv
, NULL
);
10442 /* By default, wait for all types of activity. */
10443 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
10444 wd
.wd_enabled
[i
] = B_TRUE
;
10446 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
10449 wd
.wd_scripted
= B_TRUE
;
10452 wd
.wd_headers_once
= B_TRUE
;
10455 wd
.wd_exact
= B_TRUE
;
10458 get_timestamp_arg(*optarg
);
10462 static char *col_subopts
[] = { "discard", "free",
10463 "initialize", "replace", "remove", "resilver",
10464 "scrub", "trim", NULL
};
10466 /* Reset activities array */
10467 bzero(&wd
.wd_enabled
, sizeof (wd
.wd_enabled
));
10468 while (*optarg
!= '\0') {
10469 int activity
= getsubopt(&optarg
, col_subopts
,
10472 if (activity
< 0) {
10473 (void) fprintf(stderr
,
10474 gettext("invalid activity '%s'\n"),
10479 wd
.wd_enabled
[activity
] = B_TRUE
;
10484 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10493 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
10495 /* This subcmd only accepts an interval, not a count */
10496 (void) fprintf(stderr
, gettext("too many arguments\n"));
10500 if (wd
.wd_interval
!= 0)
10504 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
10508 (void) fprintf(stderr
, gettext("too many arguments\n"));
10512 wd
.wd_poolname
= argv
[0];
10514 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
10519 * We use a separate thread for printing status updates because
10520 * the main thread will call lzc_wait(), which blocks as long
10521 * as an activity is in progress, which can be a long time.
10523 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
10525 (void) fprintf(stderr
, gettext("failed to create status"
10526 "thread: %s\n"), strerror(errno
));
10533 * Loop over all activities that we are supposed to wait for until none
10534 * of them are in progress. Note that this means we can end up waiting
10535 * for more activities to complete than just those that were in progress
10536 * when we began waiting; if an activity we are interested in begins
10537 * while we are waiting for another activity, we will wait for both to
10538 * complete before exiting.
10541 boolean_t missing
= B_FALSE
;
10542 boolean_t any_waited
= B_FALSE
;
10544 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
10547 if (!wd
.wd_enabled
[i
])
10550 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
10551 if (error
!= 0 || missing
)
10554 any_waited
= (any_waited
|| waited
);
10557 if (error
!= 0 || missing
|| !any_waited
)
10565 pthread_mutex_lock(&wd
.wd_mutex
);
10566 wd
.wd_should_exit
= B_TRUE
;
10567 pthread_cond_signal(&wd
.wd_cv
);
10568 pthread_mutex_unlock(&wd
.wd_mutex
);
10569 (void) pthread_join(status_thr
, (void *)&status
);
10574 pthread_mutex_destroy(&wd
.wd_mutex
);
10575 pthread_cond_destroy(&wd
.wd_cv
);
10580 find_command_idx(char *command
, int *idx
)
10584 for (i
= 0; i
< NCOMMAND
; i
++) {
10585 if (command_table
[i
].name
== NULL
)
10588 if (strcmp(command
, command_table
[i
].name
) == 0) {
10597 * Display version message
10600 zpool_do_version(int argc
, char **argv
)
10602 if (zfs_version_print() == -1)
10609 * Do zpool_load_compat() and print error message on failure
10611 static zpool_compat_status_t
10612 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
10616 zpool_compat_status_t ret
;
10618 ret
= zpool_load_compat(compat
, list
, report
, 1024);
10621 case ZPOOL_COMPATIBILITY_OK
:
10624 case ZPOOL_COMPATIBILITY_NOFILES
:
10625 case ZPOOL_COMPATIBILITY_BADFILE
:
10626 case ZPOOL_COMPATIBILITY_BADTOKEN
:
10627 (void) fprintf(stderr
, "Error: %s\n", report
);
10630 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
10631 (void) fprintf(stderr
, "Warning: %s\n", report
);
10632 ret
= ZPOOL_COMPATIBILITY_OK
;
10639 main(int argc
, char **argv
)
10646 (void) setlocale(LC_ALL
, "");
10647 (void) setlocale(LC_NUMERIC
, "C");
10648 (void) textdomain(TEXT_DOMAIN
);
10654 * Make sure the user has specified some command.
10657 (void) fprintf(stderr
, gettext("missing command\n"));
10664 * Special case '-?'
10666 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
10670 * Special case '-V|--version'
10672 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
10673 return (zpool_do_version(argc
, argv
));
10675 if ((g_zfs
= libzfs_init()) == NULL
) {
10676 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
10680 libzfs_print_on_error(g_zfs
, B_TRUE
);
10682 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
10685 * Many commands modify input strings for string parsing reasons.
10686 * We create a copy to protect the original argv.
10688 newargv
= malloc((argc
+ 1) * sizeof (newargv
[0]));
10689 for (i
= 0; i
< argc
; i
++)
10690 newargv
[i
] = strdup(argv
[i
]);
10691 newargv
[argc
] = NULL
;
10694 * Run the appropriate command.
10696 if (find_command_idx(cmdname
, &i
) == 0) {
10697 current_command
= &command_table
[i
];
10698 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
10699 } else if (strchr(cmdname
, '=')) {
10700 verify(find_command_idx("set", &i
) == 0);
10701 current_command
= &command_table
[i
];
10702 ret
= command_table
[i
].func(argc
, newargv
);
10703 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
10705 * 'freeze' is a vile debugging abomination, so we treat
10708 zfs_cmd_t zc
= {"\0"};
10710 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
10711 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
10713 (void) fprintf(stderr
,
10714 gettext("failed to freeze pool: %d\n"), errno
);
10720 (void) fprintf(stderr
, gettext("unrecognized "
10721 "command '%s'\n"), cmdname
);
10726 for (i
= 0; i
< argc
; i
++)
10730 if (ret
== 0 && log_history
)
10731 (void) zpool_log_history(g_zfs
, history_str
);
10733 libzfs_fini(g_zfs
);
10736 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10737 * for the purposes of running ::findleaks.
10739 if (getenv("ZFS_ABORT") != NULL
) {
10740 (void) printf("dumping core by request\n");