4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
35 * Copyright (c) 2021, Klara Inc.
36 * Copyright [2021] Hewlett Packard Enterprise Development LP
59 #include <sys/fs/zfs.h>
61 #include <sys/systeminfo.h>
62 #include <sys/fm/fs/zfs.h>
63 #include <sys/fm/util.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/mount.h>
67 #include <sys/sysmacros.h>
74 #include "zpool_util.h"
75 #include "zfs_comutil.h"
76 #include "zfeature_common.h"
78 #include "statcommon.h"
80 libzfs_handle_t
*g_zfs
;
82 static int zpool_do_create(int, char **);
83 static int zpool_do_destroy(int, char **);
85 static int zpool_do_add(int, char **);
86 static int zpool_do_remove(int, char **);
87 static int zpool_do_labelclear(int, char **);
89 static int zpool_do_checkpoint(int, char **);
91 static int zpool_do_list(int, char **);
92 static int zpool_do_iostat(int, char **);
93 static int zpool_do_status(int, char **);
95 static int zpool_do_online(int, char **);
96 static int zpool_do_offline(int, char **);
97 static int zpool_do_clear(int, char **);
98 static int zpool_do_reopen(int, char **);
100 static int zpool_do_reguid(int, char **);
102 static int zpool_do_attach(int, char **);
103 static int zpool_do_detach(int, char **);
104 static int zpool_do_replace(int, char **);
105 static int zpool_do_split(int, char **);
107 static int zpool_do_initialize(int, char **);
108 static int zpool_do_scrub(int, char **);
109 static int zpool_do_resilver(int, char **);
110 static int zpool_do_trim(int, char **);
112 static int zpool_do_import(int, char **);
113 static int zpool_do_export(int, char **);
115 static int zpool_do_upgrade(int, char **);
117 static int zpool_do_history(int, char **);
118 static int zpool_do_events(int, char **);
120 static int zpool_do_get(int, char **);
121 static int zpool_do_set(int, char **);
123 static int zpool_do_sync(int, char **);
125 static int zpool_do_version(int, char **);
127 static int zpool_do_wait(int, char **);
129 static int zpool_do_help(int argc
, char **argv
);
131 static zpool_compat_status_t
zpool_do_load_compat(
132 const char *, boolean_t
*);
135 * These libumem hooks provide a reasonable set of defaults for the allocator's
136 * debugging facilities.
141 _umem_debug_init(void)
143 return ("default,verbose"); /* $UMEM_DEBUG setting */
147 _umem_logging_init(void)
149 return ("fail,contents"); /* $UMEM_LOGGING setting */
190 * Flags for stats to display with "zpool iostats"
198 IOS_COUNT
, /* always last element */
201 /* iostat_type entries as bitmasks */
202 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
203 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
204 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
205 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
206 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
208 /* Mask of all the histo bits */
209 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
212 * Lookup table for iostat flags to nvlist names. Basically a list
213 * of all the nvlists a flag requires. Also specifies the order in
214 * which data gets printed in zpool iostat.
216 static const char *vsx_type_to_nvlist
[IOS_COUNT
][15] = {
218 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
219 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
220 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
221 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
222 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
223 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
224 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
225 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
226 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
227 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
228 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
231 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
232 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
233 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
234 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
235 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
236 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
239 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
240 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
241 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
242 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
243 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
244 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
245 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
248 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO
,
249 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO
,
250 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO
,
251 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO
,
252 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO
,
253 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO
,
254 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO
,
255 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO
,
256 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO
,
257 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO
,
258 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO
,
259 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO
,
260 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO
,
261 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO
,
267 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
268 * Right now, only one histo bit is ever set at one time, so we can
269 * just do a highbit64(a)
271 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
273 typedef struct zpool_command
{
275 int (*func
)(int, char **);
280 * Master command table. Each ZFS command has a name, associated function, and
281 * usage message. The usage messages need to be internationalized, so we have
282 * to have a function to return the usage message based on a command index.
284 * These commands are organized according to how they are displayed in the usage
285 * message. An empty command (one with a NULL name) indicates an empty line in
286 * the generic usage message.
288 static zpool_command_t command_table
[] = {
289 { "version", zpool_do_version
, HELP_VERSION
},
291 { "create", zpool_do_create
, HELP_CREATE
},
292 { "destroy", zpool_do_destroy
, HELP_DESTROY
},
294 { "add", zpool_do_add
, HELP_ADD
},
295 { "remove", zpool_do_remove
, HELP_REMOVE
},
297 { "labelclear", zpool_do_labelclear
, HELP_LABELCLEAR
},
299 { "checkpoint", zpool_do_checkpoint
, HELP_CHECKPOINT
},
301 { "list", zpool_do_list
, HELP_LIST
},
302 { "iostat", zpool_do_iostat
, HELP_IOSTAT
},
303 { "status", zpool_do_status
, HELP_STATUS
},
305 { "online", zpool_do_online
, HELP_ONLINE
},
306 { "offline", zpool_do_offline
, HELP_OFFLINE
},
307 { "clear", zpool_do_clear
, HELP_CLEAR
},
308 { "reopen", zpool_do_reopen
, HELP_REOPEN
},
310 { "attach", zpool_do_attach
, HELP_ATTACH
},
311 { "detach", zpool_do_detach
, HELP_DETACH
},
312 { "replace", zpool_do_replace
, HELP_REPLACE
},
313 { "split", zpool_do_split
, HELP_SPLIT
},
315 { "initialize", zpool_do_initialize
, HELP_INITIALIZE
},
316 { "resilver", zpool_do_resilver
, HELP_RESILVER
},
317 { "scrub", zpool_do_scrub
, HELP_SCRUB
},
318 { "trim", zpool_do_trim
, HELP_TRIM
},
320 { "import", zpool_do_import
, HELP_IMPORT
},
321 { "export", zpool_do_export
, HELP_EXPORT
},
322 { "upgrade", zpool_do_upgrade
, HELP_UPGRADE
},
323 { "reguid", zpool_do_reguid
, HELP_REGUID
},
325 { "history", zpool_do_history
, HELP_HISTORY
},
326 { "events", zpool_do_events
, HELP_EVENTS
},
328 { "get", zpool_do_get
, HELP_GET
},
329 { "set", zpool_do_set
, HELP_SET
},
330 { "sync", zpool_do_sync
, HELP_SYNC
},
332 { "wait", zpool_do_wait
, HELP_WAIT
},
335 #define NCOMMAND (ARRAY_SIZE(command_table))
337 #define VDEV_ALLOC_CLASS_LOGS "logs"
339 static zpool_command_t
*current_command
;
340 static zfs_type_t current_prop_type
= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
);
341 static char history_str
[HIS_MAX_RECORD_LEN
];
342 static boolean_t log_history
= B_TRUE
;
343 static uint_t timestamp_fmt
= NODATE
;
346 get_usage(zpool_help_t idx
)
350 return (gettext("\tadd [-fgLnP] [-o property=value] "
351 "<pool> <vdev> ...\n"));
353 return (gettext("\tattach [-fsw] [-o property=value] "
354 "<pool> <device> <new-device>\n"));
356 return (gettext("\tclear [[--power]|[-nF]] <pool> [device]\n"));
358 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
359 "\t [-O file-system-property=value] ... \n"
360 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
361 case HELP_CHECKPOINT
:
362 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
364 return (gettext("\tdestroy [-f] <pool>\n"));
366 return (gettext("\tdetach <pool> <device>\n"));
368 return (gettext("\texport [-af] <pool> ...\n"));
370 return (gettext("\thistory [-il] [<pool>] ...\n"));
372 return (gettext("\timport [-d dir] [-D]\n"
373 "\timport [-o mntopts] [-o property=value] ... \n"
374 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
375 "[-R root] [-F [-n]] -a\n"
376 "\timport [-o mntopts] [-o property=value] ... \n"
377 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
378 "[-R root] [-F [-n]]\n"
379 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
381 return (gettext("\tiostat [[[-c [script1,script2,...]"
382 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
383 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
384 " [[-n] interval [count]]\n"));
385 case HELP_LABELCLEAR
:
386 return (gettext("\tlabelclear [-f] <vdev>\n"));
388 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
389 "[-T d|u] [pool] ... \n"
390 "\t [interval [count]]\n"));
392 return (gettext("\toffline [--power]|[[-f][-t]] <pool> "
395 return (gettext("\tonline [--power][-e] <pool> <device> "
398 return (gettext("\treplace [-fsw] [-o property=value] "
399 "<pool> <device> [new-device]\n"));
401 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
403 return (gettext("\treopen [-n] <pool>\n"));
404 case HELP_INITIALIZE
:
405 return (gettext("\tinitialize [-c | -s | -u] [-w] <pool> "
406 "[<device> ...]\n"));
408 return (gettext("\tscrub [-s | -p] [-w] [-e] <pool> ...\n"));
410 return (gettext("\tresilver <pool> ...\n"));
412 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
413 "[<device> ...]\n"));
415 return (gettext("\tstatus [--power] [-c [script1,script2,...]] "
416 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
417 "\t [interval [count]]\n"));
419 return (gettext("\tupgrade\n"
421 "\tupgrade [-V version] <-a | pool ...>\n"));
423 return (gettext("\tevents [-vHf [pool] | -c]\n"));
425 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
426 "<\"all\" | property[,...]> <pool> ...\n"));
428 return (gettext("\tset <property=value> <pool>\n"
429 "\tset <vdev_property=value> <pool> <vdev>\n"));
431 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
432 "\t [-o property=value] <pool> <newpool> "
433 "[<device> ...]\n"));
435 return (gettext("\treguid <pool>\n"));
437 return (gettext("\tsync [pool] ...\n"));
439 return (gettext("\tversion\n"));
441 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
442 "<pool> [interval]\n"));
444 __builtin_unreachable();
449 zpool_collect_leaves(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, nvlist_t
*res
)
455 (void) nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
459 char *path
= zpool_vdev_name(g_zfs
, zhp
, nvroot
,
462 if (strcmp(path
, VDEV_TYPE_INDIRECT
) != 0 &&
463 strcmp(path
, VDEV_TYPE_HOLE
) != 0)
464 fnvlist_add_boolean(res
, path
);
470 for (i
= 0; i
< children
; i
++) {
471 zpool_collect_leaves(zhp
, child
[i
], res
);
476 * Callback routine that will print out a pool property value.
479 print_pool_prop_cb(int prop
, void *cb
)
483 (void) fprintf(fp
, "\t%-19s ", zpool_prop_to_name(prop
));
485 if (zpool_prop_readonly(prop
))
486 (void) fprintf(fp
, " NO ");
488 (void) fprintf(fp
, " YES ");
490 if (zpool_prop_values(prop
) == NULL
)
491 (void) fprintf(fp
, "-\n");
493 (void) fprintf(fp
, "%s\n", zpool_prop_values(prop
));
499 * Callback routine that will print out a vdev property value.
502 print_vdev_prop_cb(int prop
, void *cb
)
506 (void) fprintf(fp
, "\t%-19s ", vdev_prop_to_name(prop
));
508 if (vdev_prop_readonly(prop
))
509 (void) fprintf(fp
, " NO ");
511 (void) fprintf(fp
, " YES ");
513 if (vdev_prop_values(prop
) == NULL
)
514 (void) fprintf(fp
, "-\n");
516 (void) fprintf(fp
, "%s\n", vdev_prop_values(prop
));
522 * Given a leaf vdev name like 'L5' return its VDEV_CONFIG_PATH like
523 * '/dev/disk/by-vdev/L5'.
526 vdev_name_to_path(zpool_handle_t
*zhp
, char *vdev
)
528 nvlist_t
*vdev_nv
= zpool_find_vdev(zhp
, vdev
, NULL
, NULL
, NULL
);
529 if (vdev_nv
== NULL
) {
532 return (fnvlist_lookup_string(vdev_nv
, ZPOOL_CONFIG_PATH
));
536 zpool_power_on(zpool_handle_t
*zhp
, char *vdev
)
538 return (zpool_power(zhp
, vdev
, B_TRUE
));
542 zpool_power_on_and_disk_wait(zpool_handle_t
*zhp
, char *vdev
)
546 rc
= zpool_power_on(zhp
, vdev
);
550 zpool_disk_wait(vdev_name_to_path(zhp
, vdev
));
556 zpool_power_on_pool_and_wait_for_devices(zpool_handle_t
*zhp
)
559 const char *path
= NULL
;
562 /* Power up all the devices first */
563 FOR_EACH_REAL_LEAF_VDEV(zhp
, nv
) {
564 path
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
);
566 rc
= zpool_power_on(zhp
, (char *)path
);
574 * Wait for their devices to show up. Since we powered them on
575 * at roughly the same time, they should all come online around
578 FOR_EACH_REAL_LEAF_VDEV(zhp
, nv
) {
579 path
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
);
580 zpool_disk_wait(path
);
587 zpool_power_off(zpool_handle_t
*zhp
, char *vdev
)
589 return (zpool_power(zhp
, vdev
, B_FALSE
));
593 * Display usage message. If we're inside a command, display only the usage for
594 * that command. Otherwise, iterate over the entire command table and display
595 * a complete usage message.
597 static __attribute__((noreturn
)) void
598 usage(boolean_t requested
)
600 FILE *fp
= requested
? stdout
: stderr
;
602 if (current_command
== NULL
) {
605 (void) fprintf(fp
, gettext("usage: zpool command args ...\n"));
607 gettext("where 'command' is one of the following:\n\n"));
609 for (i
= 0; i
< NCOMMAND
; i
++) {
610 if (command_table
[i
].name
== NULL
)
611 (void) fprintf(fp
, "\n");
613 (void) fprintf(fp
, "%s",
614 get_usage(command_table
[i
].usage
));
618 gettext("\nFor further help on a command or topic, "
619 "run: %s\n"), "zpool help [<topic>]");
621 (void) fprintf(fp
, gettext("usage:\n"));
622 (void) fprintf(fp
, "%s", get_usage(current_command
->usage
));
625 if (current_command
!= NULL
&&
626 current_prop_type
!= (ZFS_TYPE_POOL
| ZFS_TYPE_VDEV
) &&
627 ((strcmp(current_command
->name
, "set") == 0) ||
628 (strcmp(current_command
->name
, "get") == 0) ||
629 (strcmp(current_command
->name
, "list") == 0))) {
631 (void) fprintf(fp
, "%s",
632 gettext("\nthe following properties are supported:\n"));
634 (void) fprintf(fp
, "\n\t%-19s %s %s\n\n",
635 "PROPERTY", "EDIT", "VALUES");
637 /* Iterate over all properties */
638 if (current_prop_type
== ZFS_TYPE_POOL
) {
639 (void) zprop_iter(print_pool_prop_cb
, fp
, B_FALSE
,
640 B_TRUE
, current_prop_type
);
642 (void) fprintf(fp
, "\t%-19s ", "feature@...");
643 (void) fprintf(fp
, "YES "
644 "disabled | enabled | active\n");
646 (void) fprintf(fp
, gettext("\nThe feature@ properties "
647 "must be appended with a feature name.\n"
648 "See zpool-features(7).\n"));
649 } else if (current_prop_type
== ZFS_TYPE_VDEV
) {
650 (void) zprop_iter(print_vdev_prop_cb
, fp
, B_FALSE
,
651 B_TRUE
, current_prop_type
);
656 * See comments at end of main().
658 if (getenv("ZFS_ABORT") != NULL
) {
659 (void) printf("dumping core by request\n");
663 exit(requested
? 0 : 2);
667 * zpool initialize [-c | -s | -u] [-w] <pool> [<vdev> ...]
668 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
671 * -c Cancel. Ends active initializing.
672 * -s Suspend. Initializing can then be restarted with no flags.
673 * -u Uninitialize. Clears initialization state.
674 * -w Wait. Blocks until initializing has completed.
677 zpool_do_initialize(int argc
, char **argv
)
684 boolean_t wait
= B_FALSE
;
686 struct option long_options
[] = {
687 {"cancel", no_argument
, NULL
, 'c'},
688 {"suspend", no_argument
, NULL
, 's'},
689 {"uninit", no_argument
, NULL
, 'u'},
690 {"wait", no_argument
, NULL
, 'w'},
694 pool_initialize_func_t cmd_type
= POOL_INITIALIZE_START
;
695 while ((c
= getopt_long(argc
, argv
, "csuw", long_options
,
699 if (cmd_type
!= POOL_INITIALIZE_START
&&
700 cmd_type
!= POOL_INITIALIZE_CANCEL
) {
701 (void) fprintf(stderr
, gettext("-c cannot be "
702 "combined with other options\n"));
705 cmd_type
= POOL_INITIALIZE_CANCEL
;
708 if (cmd_type
!= POOL_INITIALIZE_START
&&
709 cmd_type
!= POOL_INITIALIZE_SUSPEND
) {
710 (void) fprintf(stderr
, gettext("-s cannot be "
711 "combined with other options\n"));
714 cmd_type
= POOL_INITIALIZE_SUSPEND
;
717 if (cmd_type
!= POOL_INITIALIZE_START
&&
718 cmd_type
!= POOL_INITIALIZE_UNINIT
) {
719 (void) fprintf(stderr
, gettext("-u cannot be "
720 "combined with other options\n"));
723 cmd_type
= POOL_INITIALIZE_UNINIT
;
730 (void) fprintf(stderr
,
731 gettext("invalid option '%c'\n"), optopt
);
733 (void) fprintf(stderr
,
734 gettext("invalid option '%s'\n"),
745 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
750 if (wait
&& (cmd_type
!= POOL_INITIALIZE_START
)) {
751 (void) fprintf(stderr
, gettext("-w cannot be used with -c, -s"
757 zhp
= zpool_open(g_zfs
, poolname
);
761 vdevs
= fnvlist_alloc();
763 /* no individual leaf vdevs specified, so add them all */
764 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
765 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
766 ZPOOL_CONFIG_VDEV_TREE
);
767 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
769 for (int i
= 1; i
< argc
; i
++) {
770 fnvlist_add_boolean(vdevs
, argv
[i
]);
775 err
= zpool_initialize_wait(zhp
, cmd_type
, vdevs
);
777 err
= zpool_initialize(zhp
, cmd_type
, vdevs
);
786 * print a pool vdev config for dry runs
789 print_vdev_tree(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
, int indent
,
790 const char *match
, int name_flags
)
795 boolean_t printed
= B_FALSE
;
797 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
798 &child
, &children
) != 0) {
800 (void) printf("\t%*s%s\n", indent
, "", name
);
804 for (c
= 0; c
< children
; c
++) {
805 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
806 const char *class = "";
808 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
811 if (is_hole
== B_TRUE
) {
815 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
818 class = VDEV_ALLOC_BIAS_LOG
;
819 (void) nvlist_lookup_string(child
[c
],
820 ZPOOL_CONFIG_ALLOCATION_BIAS
, &class);
821 if (strcmp(match
, class) != 0)
824 if (!printed
&& name
!= NULL
) {
825 (void) printf("\t%*s%s\n", indent
, "", name
);
828 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
], name_flags
);
829 print_vdev_tree(zhp
, vname
, child
[c
], indent
+ 2, "",
836 * Print the list of l2cache devices for dry runs.
839 print_cache_list(nvlist_t
*nv
, int indent
)
844 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
845 &child
, &children
) == 0 && children
> 0) {
846 (void) printf("\t%*s%s\n", indent
, "", "cache");
850 for (c
= 0; c
< children
; c
++) {
853 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
854 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
860 * Print the list of spares for dry runs.
863 print_spare_list(nvlist_t
*nv
, int indent
)
868 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
869 &child
, &children
) == 0 && children
> 0) {
870 (void) printf("\t%*s%s\n", indent
, "", "spares");
874 for (c
= 0; c
< children
; c
++) {
877 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
], 0);
878 (void) printf("\t%*s%s\n", indent
+ 2, "", vname
);
884 prop_list_contains_feature(nvlist_t
*proplist
)
887 for (nvp
= nvlist_next_nvpair(proplist
, NULL
); NULL
!= nvp
;
888 nvp
= nvlist_next_nvpair(proplist
, nvp
)) {
889 if (zpool_prop_feature(nvpair_name(nvp
)))
896 * Add a property pair (name, string-value) into a property nvlist.
899 add_prop_list(const char *propname
, const char *propval
, nvlist_t
**props
,
902 zpool_prop_t prop
= ZPOOL_PROP_INVAL
;
907 if (*props
== NULL
&&
908 nvlist_alloc(props
, NV_UNIQUE_NAME
, 0) != 0) {
909 (void) fprintf(stderr
,
910 gettext("internal error: out of memory\n"));
917 const char *vname
= zpool_prop_to_name(ZPOOL_PROP_VERSION
);
919 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY
);
921 if ((prop
= zpool_name_to_prop(propname
)) == ZPOOL_PROP_INVAL
&&
922 (!zpool_prop_feature(propname
) &&
923 !zpool_prop_vdev(propname
))) {
924 (void) fprintf(stderr
, gettext("property '%s' is "
925 "not a valid pool or vdev property\n"), propname
);
930 * feature@ properties and version should not be specified
933 if ((prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
) &&
934 nvlist_exists(proplist
, vname
)) ||
935 (prop
== ZPOOL_PROP_VERSION
&&
936 prop_list_contains_feature(proplist
))) {
937 (void) fprintf(stderr
, gettext("'feature@' and "
938 "'version' properties cannot be specified "
944 * if version is specified, only "legacy" compatibility
947 if ((prop
== ZPOOL_PROP_COMPATIBILITY
&&
948 strcmp(propval
, ZPOOL_COMPAT_LEGACY
) != 0 &&
949 nvlist_exists(proplist
, vname
)) ||
950 (prop
== ZPOOL_PROP_VERSION
&&
951 nvlist_exists(proplist
, cname
) &&
952 strcmp(fnvlist_lookup_string(proplist
, cname
),
953 ZPOOL_COMPAT_LEGACY
) != 0)) {
954 (void) fprintf(stderr
, gettext("when 'version' is "
955 "specified, the 'compatibility' feature may only "
956 "be set to '" ZPOOL_COMPAT_LEGACY
"'\n"));
960 if (zpool_prop_feature(propname
) || zpool_prop_vdev(propname
))
963 normnm
= zpool_prop_to_name(prop
);
965 zfs_prop_t fsprop
= zfs_name_to_prop(propname
);
967 if (zfs_prop_valid_for_type(fsprop
, ZFS_TYPE_FILESYSTEM
,
969 normnm
= zfs_prop_to_name(fsprop
);
970 } else if (zfs_prop_user(propname
) ||
971 zfs_prop_userquota(propname
)) {
974 (void) fprintf(stderr
, gettext("property '%s' is "
975 "not a valid filesystem property\n"), propname
);
980 if (nvlist_lookup_string(proplist
, normnm
, &strval
) == 0 &&
981 prop
!= ZPOOL_PROP_CACHEFILE
) {
982 (void) fprintf(stderr
, gettext("property '%s' "
983 "specified multiple times\n"), propname
);
987 if (nvlist_add_string(proplist
, normnm
, propval
) != 0) {
988 (void) fprintf(stderr
, gettext("internal "
989 "error: out of memory\n"));
997 * Set a default property pair (name, string-value) in a property nvlist
1000 add_prop_list_default(const char *propname
, const char *propval
,
1005 if (nvlist_lookup_string(*props
, propname
, &pval
) == 0)
1008 return (add_prop_list(propname
, propval
, props
, B_TRUE
));
1012 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
1014 * -f Force addition of devices, even if they appear in use
1015 * -g Display guid for individual vdev name.
1016 * -L Follow links when resolving vdev path name.
1017 * -n Do not add the devices, but display the resulting layout if
1018 * they were to be added.
1019 * -o Set property=value.
1020 * -P Display full path for vdev name.
1022 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
1023 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
1027 zpool_do_add(int argc
, char **argv
)
1029 boolean_t force
= B_FALSE
;
1030 boolean_t dryrun
= B_FALSE
;
1036 zpool_handle_t
*zhp
;
1038 nvlist_t
*props
= NULL
;
1042 while ((c
= getopt(argc
, argv
, "fgLno:P")) != -1) {
1048 name_flags
|= VDEV_NAME_GUID
;
1051 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
1057 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1058 (void) fprintf(stderr
, gettext("missing "
1059 "'=' for -o option\n"));
1065 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
1066 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
1070 name_flags
|= VDEV_NAME_PATH
;
1073 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1082 /* get pool name and check number of arguments */
1084 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1088 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1097 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1100 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
1101 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
1107 /* unless manually specified use "ashift" pool property (if set) */
1108 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
1111 char strval
[ZPOOL_MAXPROPLEN
];
1113 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
1114 if (src
!= ZPROP_SRC_DEFAULT
) {
1115 (void) sprintf(strval
, "%" PRId32
, intval
);
1116 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
1117 &props
, B_TRUE
) == 0);
1121 /* pass off to make_root_vdev for processing */
1122 nvroot
= make_root_vdev(zhp
, props
, force
, !force
, B_FALSE
, dryrun
,
1124 if (nvroot
== NULL
) {
1130 nvlist_t
*poolnvroot
;
1131 nvlist_t
**l2child
, **sparechild
;
1132 uint_t l2children
, sparechildren
, c
;
1134 boolean_t hadcache
= B_FALSE
, hadspare
= B_FALSE
;
1136 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1139 (void) printf(gettext("would update '%s' to the following "
1140 "configuration:\n\n"), zpool_get_name(zhp
));
1142 /* print original main pool and new tree */
1143 print_vdev_tree(zhp
, poolname
, poolnvroot
, 0, "",
1144 name_flags
| VDEV_NAME_TYPE_ID
);
1145 print_vdev_tree(zhp
, NULL
, nvroot
, 0, "", name_flags
);
1147 /* print other classes: 'dedup', 'special', and 'log' */
1148 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1149 print_vdev_tree(zhp
, "dedup", poolnvroot
, 0,
1150 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1151 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1152 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1153 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_DEDUP
)) {
1154 print_vdev_tree(zhp
, "dedup", nvroot
, 0,
1155 VDEV_ALLOC_BIAS_DEDUP
, name_flags
);
1158 if (zfs_special_devs(poolnvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1159 print_vdev_tree(zhp
, "special", poolnvroot
, 0,
1160 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1161 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1162 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1163 } else if (zfs_special_devs(nvroot
, VDEV_ALLOC_BIAS_SPECIAL
)) {
1164 print_vdev_tree(zhp
, "special", nvroot
, 0,
1165 VDEV_ALLOC_BIAS_SPECIAL
, name_flags
);
1168 if (num_logs(poolnvroot
) > 0) {
1169 print_vdev_tree(zhp
, "logs", poolnvroot
, 0,
1170 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1171 print_vdev_tree(zhp
, NULL
, nvroot
, 0,
1172 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1173 } else if (num_logs(nvroot
) > 0) {
1174 print_vdev_tree(zhp
, "logs", nvroot
, 0,
1175 VDEV_ALLOC_BIAS_LOG
, name_flags
);
1178 /* Do the same for the caches */
1179 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_L2CACHE
,
1180 &l2child
, &l2children
) == 0 && l2children
) {
1182 (void) printf(gettext("\tcache\n"));
1183 for (c
= 0; c
< l2children
; c
++) {
1184 vname
= zpool_vdev_name(g_zfs
, NULL
,
1185 l2child
[c
], name_flags
);
1186 (void) printf("\t %s\n", vname
);
1190 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1191 &l2child
, &l2children
) == 0 && l2children
) {
1193 (void) printf(gettext("\tcache\n"));
1194 for (c
= 0; c
< l2children
; c
++) {
1195 vname
= zpool_vdev_name(g_zfs
, NULL
,
1196 l2child
[c
], name_flags
);
1197 (void) printf("\t %s\n", vname
);
1201 /* And finally the spares */
1202 if (nvlist_lookup_nvlist_array(poolnvroot
, ZPOOL_CONFIG_SPARES
,
1203 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1205 (void) printf(gettext("\tspares\n"));
1206 for (c
= 0; c
< sparechildren
; c
++) {
1207 vname
= zpool_vdev_name(g_zfs
, NULL
,
1208 sparechild
[c
], name_flags
);
1209 (void) printf("\t %s\n", vname
);
1213 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1214 &sparechild
, &sparechildren
) == 0 && sparechildren
> 0) {
1216 (void) printf(gettext("\tspares\n"));
1217 for (c
= 0; c
< sparechildren
; c
++) {
1218 vname
= zpool_vdev_name(g_zfs
, NULL
,
1219 sparechild
[c
], name_flags
);
1220 (void) printf("\t %s\n", vname
);
1227 ret
= (zpool_add(zhp
, nvroot
) != 0);
1231 nvlist_free(nvroot
);
1238 * zpool remove [-npsw] <pool> <vdev> ...
1240 * Removes the given vdev from the pool.
1243 zpool_do_remove(int argc
, char **argv
)
1247 zpool_handle_t
*zhp
= NULL
;
1248 boolean_t stop
= B_FALSE
;
1250 boolean_t noop
= B_FALSE
;
1251 boolean_t parsable
= B_FALSE
;
1252 boolean_t wait
= B_FALSE
;
1255 while ((c
= getopt(argc
, argv
, "npsw")) != -1) {
1270 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1279 /* get pool name and check number of arguments */
1281 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1287 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
1292 (void) fprintf(stderr
, gettext("stop request ignored\n"));
1298 (void) fprintf(stderr
, gettext("too many arguments\n"));
1301 if (zpool_vdev_remove_cancel(zhp
) != 0)
1304 (void) fprintf(stderr
, gettext("invalid option "
1305 "combination: -w cannot be used with -s\n"));
1310 (void) fprintf(stderr
, gettext("missing device\n"));
1314 for (i
= 1; i
< argc
; i
++) {
1318 if (zpool_vdev_indirect_size(zhp
, argv
[i
],
1324 (void) printf("%s %llu\n",
1325 argv
[i
], (unsigned long long)size
);
1328 zfs_nicenum(size
, valstr
,
1330 (void) printf("Memory that will be "
1331 "used after removing %s: %s\n",
1335 if (zpool_vdev_remove(zhp
, argv
[i
]) != 0)
1340 if (ret
== 0 && wait
)
1341 ret
= zpool_wait(zhp
, ZPOOL_WAIT_REMOVE
);
1349 * Return 1 if a vdev is active (being used in a pool)
1350 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1352 * This is useful for checking if a disk in an active pool is offlined or
1356 vdev_is_active(char *vdev_path
)
1359 fd
= open(vdev_path
, O_EXCL
);
1361 return (1); /* cant open O_EXCL - disk is active */
1365 return (0); /* disk is inactive in the pool */
1369 * zpool labelclear [-f] <vdev>
1371 * -f Force clearing the label for the vdevs which are members of
1372 * the exported or foreign pools.
1374 * Verifies that the vdev is not active and zeros out the label information
1378 zpool_do_labelclear(int argc
, char **argv
)
1380 char vdev
[MAXPATHLEN
];
1382 int c
, fd
= -1, ret
= 0;
1385 boolean_t inuse
= B_FALSE
;
1386 boolean_t force
= B_FALSE
;
1389 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1395 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1406 (void) fprintf(stderr
, gettext("missing vdev name\n"));
1410 (void) fprintf(stderr
, gettext("too many arguments\n"));
1414 (void) strlcpy(vdev
, argv
[0], sizeof (vdev
));
1417 * If we cannot open an absolute path, we quit.
1418 * Otherwise if the provided vdev name doesn't point to a file,
1419 * try prepending expected disk paths and partition numbers.
1421 if ((fd
= open(vdev
, O_RDWR
)) < 0) {
1423 if (vdev
[0] == '/') {
1424 (void) fprintf(stderr
, gettext("failed to open "
1425 "%s: %s\n"), vdev
, strerror(errno
));
1429 error
= zfs_resolve_shortname(argv
[0], vdev
, MAXPATHLEN
);
1430 if (error
== 0 && zfs_dev_is_whole_disk(vdev
)) {
1431 if (zfs_append_partition(vdev
, MAXPATHLEN
) == -1)
1435 if (error
|| ((fd
= open(vdev
, O_RDWR
)) < 0)) {
1436 if (errno
== ENOENT
) {
1437 (void) fprintf(stderr
, gettext(
1438 "failed to find device %s, try "
1439 "specifying absolute path instead\n"),
1444 (void) fprintf(stderr
, gettext("failed to open %s:"
1445 " %s\n"), vdev
, strerror(errno
));
1451 * Flush all dirty pages for the block device. This should not be
1452 * fatal when the device does not support BLKFLSBUF as would be the
1453 * case for a file vdev.
1455 if ((zfs_dev_flush(fd
) != 0) && (errno
!= ENOTTY
))
1456 (void) fprintf(stderr
, gettext("failed to invalidate "
1457 "cache for %s: %s\n"), vdev
, strerror(errno
));
1459 if (zpool_read_label(fd
, &config
, NULL
) != 0) {
1460 (void) fprintf(stderr
,
1461 gettext("failed to read label from %s\n"), vdev
);
1465 nvlist_free(config
);
1467 ret
= zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
);
1469 (void) fprintf(stderr
,
1470 gettext("failed to check state for %s\n"), vdev
);
1480 case POOL_STATE_ACTIVE
:
1481 case POOL_STATE_SPARE
:
1482 case POOL_STATE_L2CACHE
:
1484 * We allow the user to call 'zpool offline -f'
1485 * on an offlined disk in an active pool. We can check if
1486 * the disk is online by calling vdev_is_active().
1488 if (force
&& !vdev_is_active(vdev
))
1491 (void) fprintf(stderr
, gettext(
1492 "%s is a member (%s) of pool \"%s\""),
1493 vdev
, zpool_pool_state_to_name(state
), name
);
1496 (void) fprintf(stderr
, gettext(
1497 ". Offline the disk first to clear its label."));
1503 case POOL_STATE_EXPORTED
:
1506 (void) fprintf(stderr
, gettext(
1507 "use '-f' to override the following error:\n"
1508 "%s is a member of exported pool \"%s\"\n"),
1513 case POOL_STATE_POTENTIALLY_ACTIVE
:
1516 (void) fprintf(stderr
, gettext(
1517 "use '-f' to override the following error:\n"
1518 "%s is a member of potentially active pool \"%s\"\n"),
1523 case POOL_STATE_DESTROYED
:
1524 /* inuse should never be set for a destroyed pool */
1530 ret
= zpool_clear_label(fd
);
1532 (void) fprintf(stderr
,
1533 gettext("failed to clear label for %s\n"), vdev
);
1544 * zpool create [-fnd] [-o property=value] ...
1545 * [-O file-system-property=value] ...
1546 * [-R root] [-m mountpoint] <pool> <dev> ...
1548 * -f Force creation, even if devices appear in use
1549 * -n Do not create the pool, but display the resulting layout if it
1550 * were to be created.
1551 * -R Create a pool under an alternate root
1552 * -m Set default mountpoint for the root dataset. By default it's
1554 * -o Set property=value.
1555 * -o Set feature@feature=enabled|disabled.
1556 * -d Don't automatically enable all supported pool features
1557 * (individual features can be enabled with -o).
1558 * -O Set fsproperty=value in the pool's root file system
1560 * Creates the named pool according to the given vdev specification. The
1561 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1562 * Once we get the nvlist back from make_root_vdev(), we either print out the
1563 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1566 zpool_do_create(int argc
, char **argv
)
1568 boolean_t force
= B_FALSE
;
1569 boolean_t dryrun
= B_FALSE
;
1570 boolean_t enable_pool_features
= B_TRUE
;
1573 nvlist_t
*nvroot
= NULL
;
1577 char *altroot
= NULL
;
1578 char *compat
= NULL
;
1579 char *mountpoint
= NULL
;
1580 nvlist_t
*fsprops
= NULL
;
1581 nvlist_t
*props
= NULL
;
1585 while ((c
= getopt(argc
, argv
, ":fndR:m:o:O:t:")) != -1) {
1594 enable_pool_features
= B_FALSE
;
1598 if (add_prop_list(zpool_prop_to_name(
1599 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
1601 if (add_prop_list_default(zpool_prop_to_name(
1602 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1606 /* Equivalent to -O mountpoint=optarg */
1607 mountpoint
= optarg
;
1610 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1611 (void) fprintf(stderr
, gettext("missing "
1612 "'=' for -o option\n"));
1618 if (add_prop_list(optarg
, propval
, &props
, B_TRUE
))
1622 * If the user is creating a pool that doesn't support
1623 * feature flags, don't enable any features.
1625 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_VERSION
) {
1629 ver
= strtoull(propval
, &end
, 10);
1631 ver
< SPA_VERSION_FEATURES
) {
1632 enable_pool_features
= B_FALSE
;
1635 if (zpool_name_to_prop(optarg
) == ZPOOL_PROP_ALTROOT
)
1637 if (zpool_name_to_prop(optarg
) ==
1638 ZPOOL_PROP_COMPATIBILITY
)
1642 if ((propval
= strchr(optarg
, '=')) == NULL
) {
1643 (void) fprintf(stderr
, gettext("missing "
1644 "'=' for -O option\n"));
1651 * Mountpoints are checked and then added later.
1652 * Uniquely among properties, they can be specified
1653 * more than once, to avoid conflict with -m.
1655 if (0 == strcmp(optarg
,
1656 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
))) {
1657 mountpoint
= propval
;
1658 } else if (add_prop_list(optarg
, propval
, &fsprops
,
1665 * Sanity check temporary pool name.
1667 if (strchr(optarg
, '/') != NULL
) {
1668 (void) fprintf(stderr
, gettext("cannot create "
1669 "'%s': invalid character '/' in temporary "
1671 (void) fprintf(stderr
, gettext("use 'zfs "
1672 "create' to create a dataset\n"));
1676 if (add_prop_list(zpool_prop_to_name(
1677 ZPOOL_PROP_TNAME
), optarg
, &props
, B_TRUE
))
1679 if (add_prop_list_default(zpool_prop_to_name(
1680 ZPOOL_PROP_CACHEFILE
), "none", &props
))
1685 (void) fprintf(stderr
, gettext("missing argument for "
1686 "'%c' option\n"), optopt
);
1689 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1698 /* get pool name and check number of arguments */
1700 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
1704 (void) fprintf(stderr
, gettext("missing vdev specification\n"));
1711 * As a special case, check for use of '/' in the name, and direct the
1712 * user to use 'zfs create' instead.
1714 if (strchr(poolname
, '/') != NULL
) {
1715 (void) fprintf(stderr
, gettext("cannot create '%s': invalid "
1716 "character '/' in pool name\n"), poolname
);
1717 (void) fprintf(stderr
, gettext("use 'zfs create' to "
1718 "create a dataset\n"));
1722 /* pass off to make_root_vdev for bulk processing */
1723 nvroot
= make_root_vdev(NULL
, props
, force
, !force
, B_FALSE
, dryrun
,
1724 argc
- 1, argv
+ 1);
1728 /* make_root_vdev() allows 0 toplevel children if there are spares */
1729 if (!zfs_allocatable_devs(nvroot
)) {
1730 (void) fprintf(stderr
, gettext("invalid vdev "
1731 "specification: at least one toplevel vdev must be "
1736 if (altroot
!= NULL
&& altroot
[0] != '/') {
1737 (void) fprintf(stderr
, gettext("invalid alternate root '%s': "
1738 "must be an absolute path\n"), altroot
);
1743 * Check the validity of the mountpoint and direct the user to use the
1744 * '-m' mountpoint option if it looks like its in use.
1746 if (mountpoint
== NULL
||
1747 (strcmp(mountpoint
, ZFS_MOUNTPOINT_LEGACY
) != 0 &&
1748 strcmp(mountpoint
, ZFS_MOUNTPOINT_NONE
) != 0)) {
1749 char buf
[MAXPATHLEN
];
1752 if (mountpoint
&& mountpoint
[0] != '/') {
1753 (void) fprintf(stderr
, gettext("invalid mountpoint "
1754 "'%s': must be an absolute path, 'legacy', or "
1755 "'none'\n"), mountpoint
);
1759 if (mountpoint
== NULL
) {
1760 if (altroot
!= NULL
)
1761 (void) snprintf(buf
, sizeof (buf
), "%s/%s",
1764 (void) snprintf(buf
, sizeof (buf
), "/%s",
1767 if (altroot
!= NULL
)
1768 (void) snprintf(buf
, sizeof (buf
), "%s%s",
1769 altroot
, mountpoint
);
1771 (void) snprintf(buf
, sizeof (buf
), "%s",
1775 if ((dirp
= opendir(buf
)) == NULL
&& errno
!= ENOENT
) {
1776 (void) fprintf(stderr
, gettext("mountpoint '%s' : "
1777 "%s\n"), buf
, strerror(errno
));
1778 (void) fprintf(stderr
, gettext("use '-m' "
1779 "option to provide a different default\n"));
1784 while (count
< 3 && readdir(dirp
) != NULL
)
1786 (void) closedir(dirp
);
1789 (void) fprintf(stderr
, gettext("mountpoint "
1790 "'%s' exists and is not empty\n"), buf
);
1791 (void) fprintf(stderr
, gettext("use '-m' "
1792 "option to provide a "
1793 "different default\n"));
1800 * Now that the mountpoint's validity has been checked, ensure that
1801 * the property is set appropriately prior to creating the pool.
1803 if (mountpoint
!= NULL
) {
1804 ret
= add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1805 mountpoint
, &fsprops
, B_FALSE
);
1813 * For a dry run invocation, print out a basic message and run
1814 * through all the vdevs in the list and print out in an
1815 * appropriate hierarchy.
1817 (void) printf(gettext("would create '%s' with the "
1818 "following layout:\n\n"), poolname
);
1820 print_vdev_tree(NULL
, poolname
, nvroot
, 0, "", 0);
1821 print_vdev_tree(NULL
, "dedup", nvroot
, 0,
1822 VDEV_ALLOC_BIAS_DEDUP
, 0);
1823 print_vdev_tree(NULL
, "special", nvroot
, 0,
1824 VDEV_ALLOC_BIAS_SPECIAL
, 0);
1825 print_vdev_tree(NULL
, "logs", nvroot
, 0,
1826 VDEV_ALLOC_BIAS_LOG
, 0);
1827 print_cache_list(nvroot
, 0);
1828 print_spare_list(nvroot
, 0);
1833 * Load in feature set.
1834 * Note: if compatibility property not given, we'll have
1835 * NULL, which means 'all features'.
1837 boolean_t requested_features
[SPA_FEATURES
];
1838 if (zpool_do_load_compat(compat
, requested_features
) !=
1839 ZPOOL_COMPATIBILITY_OK
)
1843 * props contains list of features to enable.
1845 * - remove it if feature@name=disabled
1846 * - leave it there if feature@name=enabled
1848 * - enable_pool_features (ie: no '-d' or '-o version')
1849 * - it's supported by the kernel module
1850 * - it's in the requested feature set
1851 * - warn if it's enabled but not in compat
1853 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
1854 char propname
[MAXPATHLEN
];
1855 const char *propval
;
1856 zfeature_info_t
*feat
= &spa_feature_table
[i
];
1858 (void) snprintf(propname
, sizeof (propname
),
1859 "feature@%s", feat
->fi_uname
);
1861 if (!nvlist_lookup_string(props
, propname
, &propval
)) {
1863 ZFS_FEATURE_DISABLED
) == 0) {
1864 (void) nvlist_remove_all(props
,
1866 } else if (strcmp(propval
,
1867 ZFS_FEATURE_ENABLED
) == 0 &&
1868 !requested_features
[i
]) {
1869 (void) fprintf(stderr
, gettext(
1870 "Warning: feature \"%s\" enabled "
1871 "but is not in specified "
1872 "'compatibility' feature set.\n"),
1876 enable_pool_features
&&
1877 feat
->fi_zfs_mod_supported
&&
1878 requested_features
[i
]) {
1879 ret
= add_prop_list(propname
,
1880 ZFS_FEATURE_ENABLED
, &props
, B_TRUE
);
1887 if (zpool_create(g_zfs
, poolname
,
1888 nvroot
, props
, fsprops
) == 0) {
1889 zfs_handle_t
*pool
= zfs_open(g_zfs
,
1890 tname
? tname
: poolname
, ZFS_TYPE_FILESYSTEM
);
1892 if (zfs_mount(pool
, NULL
, 0) == 0) {
1893 ret
= zfs_share(pool
, NULL
);
1894 zfs_commit_shares(NULL
);
1898 } else if (libzfs_errno(g_zfs
) == EZFS_INVALIDNAME
) {
1899 (void) fprintf(stderr
, gettext("pool name may have "
1905 nvlist_free(nvroot
);
1906 nvlist_free(fsprops
);
1910 nvlist_free(fsprops
);
1917 * zpool destroy <pool>
1919 * -f Forcefully unmount any datasets
1921 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1924 zpool_do_destroy(int argc
, char **argv
)
1926 boolean_t force
= B_FALSE
;
1929 zpool_handle_t
*zhp
;
1933 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1939 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
1948 /* check arguments */
1950 (void) fprintf(stderr
, gettext("missing pool argument\n"));
1954 (void) fprintf(stderr
, gettext("too many arguments\n"));
1960 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
1962 * As a special case, check for use of '/' in the name, and
1963 * direct the user to use 'zfs destroy' instead.
1965 if (strchr(pool
, '/') != NULL
)
1966 (void) fprintf(stderr
, gettext("use 'zfs destroy' to "
1967 "destroy a dataset\n"));
1971 if (zpool_disable_datasets(zhp
, force
) != 0) {
1972 (void) fprintf(stderr
, gettext("could not destroy '%s': "
1973 "could not unmount datasets\n"), zpool_get_name(zhp
));
1978 /* The history must be logged as part of the export */
1979 log_history
= B_FALSE
;
1981 ret
= (zpool_destroy(zhp
, history_str
) != 0);
1988 typedef struct export_cbdata
{
1990 boolean_t hardforce
;
1997 zpool_export_one(zpool_handle_t
*zhp
, void *data
)
1999 export_cbdata_t
*cb
= data
;
2001 if (zpool_disable_datasets(zhp
, cb
->force
) != 0)
2004 /* The history must be logged as part of the export */
2005 log_history
= B_FALSE
;
2007 if (cb
->hardforce
) {
2008 if (zpool_export_force(zhp
, history_str
) != 0)
2010 } else if (zpool_export(zhp
, cb
->force
, history_str
) != 0) {
2018 * zpool export [-f] <pool> ...
2020 * -a Export all pools
2021 * -f Forcefully unmount datasets
2023 * Export the given pools. By default, the command will attempt to cleanly
2024 * unmount any active datasets within the pool. If the '-f' flag is specified,
2025 * then the datasets will be forcefully unmounted.
2028 zpool_do_export(int argc
, char **argv
)
2031 boolean_t do_all
= B_FALSE
;
2032 boolean_t force
= B_FALSE
;
2033 boolean_t hardforce
= B_FALSE
;
2037 while ((c
= getopt(argc
, argv
, "afF")) != -1) {
2049 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
2056 cb
.hardforce
= hardforce
;
2062 (void) fprintf(stderr
, gettext("too many arguments\n"));
2066 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
,
2067 ZFS_TYPE_POOL
, B_FALSE
, zpool_export_one
, &cb
));
2070 /* check arguments */
2072 (void) fprintf(stderr
, gettext("missing pool argument\n"));
2076 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
2077 B_FALSE
, zpool_export_one
, &cb
);
2083 * Given a vdev configuration, determine the maximum width needed for the device
2087 max_width(zpool_handle_t
*zhp
, nvlist_t
*nv
, int depth
, int max
,
2090 static const char *const subtypes
[] =
2091 {ZPOOL_CONFIG_SPARES
, ZPOOL_CONFIG_L2CACHE
, ZPOOL_CONFIG_CHILDREN
};
2093 char *name
= zpool_vdev_name(g_zfs
, zhp
, nv
, name_flags
);
2094 max
= MAX(strlen(name
) + depth
, max
);
2099 for (size_t i
= 0; i
< ARRAY_SIZE(subtypes
); ++i
)
2100 if (nvlist_lookup_nvlist_array(nv
, subtypes
[i
],
2101 &child
, &children
) == 0)
2102 for (uint_t c
= 0; c
< children
; ++c
)
2103 max
= MAX(max_width(zhp
, child
[c
], depth
+ 2,
2104 max
, name_flags
), max
);
2109 typedef struct spare_cbdata
{
2111 zpool_handle_t
*cb_zhp
;
2115 find_vdev(nvlist_t
*nv
, uint64_t search
)
2121 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) == 0 &&
2125 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2126 &child
, &children
) == 0) {
2127 for (c
= 0; c
< children
; c
++)
2128 if (find_vdev(child
[c
], search
))
2136 find_spare(zpool_handle_t
*zhp
, void *data
)
2138 spare_cbdata_t
*cbp
= data
;
2139 nvlist_t
*config
, *nvroot
;
2141 config
= zpool_get_config(zhp
, NULL
);
2142 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2145 if (find_vdev(nvroot
, cbp
->cb_guid
)) {
2154 typedef struct status_cbdata
{
2158 boolean_t cb_allpools
;
2159 boolean_t cb_verbose
;
2160 boolean_t cb_literal
;
2161 boolean_t cb_explain
;
2163 boolean_t cb_dedup_stats
;
2164 boolean_t cb_print_unhealthy
;
2165 boolean_t cb_print_status
;
2166 boolean_t cb_print_slow_ios
;
2167 boolean_t cb_print_vdev_init
;
2168 boolean_t cb_print_vdev_trim
;
2169 vdev_cmd_data_list_t
*vcdl
;
2170 boolean_t cb_print_power
;
2173 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2175 is_blank_str(const char *str
)
2177 for (; str
!= NULL
&& *str
!= '\0'; ++str
)
2183 /* Print command output lines for specific vdev in a specific pool */
2185 zpool_print_cmd(vdev_cmd_data_list_t
*vcdl
, const char *pool
, const char *path
)
2187 vdev_cmd_data_t
*data
;
2191 for (i
= 0; i
< vcdl
->count
; i
++) {
2192 if ((strcmp(vcdl
->data
[i
].path
, path
) != 0) ||
2193 (strcmp(vcdl
->data
[i
].pool
, pool
) != 0)) {
2194 /* Not the vdev we're looking for */
2198 data
= &vcdl
->data
[i
];
2199 /* Print out all the output values for this vdev */
2200 for (j
= 0; j
< vcdl
->uniq_cols_cnt
; j
++) {
2202 /* Does this vdev have values for this column? */
2203 for (int k
= 0; k
< data
->cols_cnt
; k
++) {
2204 if (strcmp(data
->cols
[k
],
2205 vcdl
->uniq_cols
[j
]) == 0) {
2206 /* yes it does, record the value */
2207 val
= data
->lines
[k
];
2212 * Mark empty values with dashes to make output
2215 if (val
== NULL
|| is_blank_str(val
))
2218 printf("%*s", vcdl
->uniq_cols_width
[j
], val
);
2219 if (j
< vcdl
->uniq_cols_cnt
- 1)
2223 /* Print out any values that aren't in a column at the end */
2224 for (j
= data
->cols_cnt
; j
< data
->lines_cnt
; j
++) {
2225 /* Did we have any columns? If so print a spacer. */
2226 if (vcdl
->uniq_cols_cnt
> 0)
2229 val
= data
->lines
[j
];
2230 fputs(val
?: "", stdout
);
2237 * Print vdev initialization status for leaves
2240 print_status_initialize(vdev_stat_t
*vs
, boolean_t verbose
)
2243 if ((vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
||
2244 vs
->vs_initialize_state
== VDEV_INITIALIZE_SUSPENDED
||
2245 vs
->vs_initialize_state
== VDEV_INITIALIZE_COMPLETE
) &&
2246 !vs
->vs_scan_removing
) {
2249 struct tm zaction_ts
;
2251 time_t t
= vs
->vs_initialize_action_time
;
2252 int initialize_pct
= 100;
2253 if (vs
->vs_initialize_state
!=
2254 VDEV_INITIALIZE_COMPLETE
) {
2255 initialize_pct
= (vs
->vs_initialize_bytes_done
*
2256 100 / (vs
->vs_initialize_bytes_est
+ 1));
2259 (void) localtime_r(&t
, &zaction_ts
);
2260 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2262 switch (vs
->vs_initialize_state
) {
2263 case VDEV_INITIALIZE_SUSPENDED
:
2264 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2265 gettext("suspended, started at"), tbuf
);
2267 case VDEV_INITIALIZE_ACTIVE
:
2268 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2269 gettext("started at"), tbuf
);
2271 case VDEV_INITIALIZE_COMPLETE
:
2272 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2273 gettext("completed at"), tbuf
);
2277 (void) printf(gettext(" (%d%% initialized%s)"),
2278 initialize_pct
, zbuf
);
2280 (void) printf(gettext(" (uninitialized)"));
2282 } else if (vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
2283 (void) printf(gettext(" (initializing)"));
2288 * Print vdev TRIM status for leaves
2291 print_status_trim(vdev_stat_t
*vs
, boolean_t verbose
)
2294 if ((vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
||
2295 vs
->vs_trim_state
== VDEV_TRIM_SUSPENDED
||
2296 vs
->vs_trim_state
== VDEV_TRIM_COMPLETE
) &&
2297 !vs
->vs_scan_removing
) {
2300 struct tm zaction_ts
;
2302 time_t t
= vs
->vs_trim_action_time
;
2304 if (vs
->vs_trim_state
!= VDEV_TRIM_COMPLETE
) {
2305 trim_pct
= (vs
->vs_trim_bytes_done
*
2306 100 / (vs
->vs_trim_bytes_est
+ 1));
2309 (void) localtime_r(&t
, &zaction_ts
);
2310 (void) strftime(tbuf
, sizeof (tbuf
), "%c", &zaction_ts
);
2312 switch (vs
->vs_trim_state
) {
2313 case VDEV_TRIM_SUSPENDED
:
2314 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2315 gettext("suspended, started at"), tbuf
);
2317 case VDEV_TRIM_ACTIVE
:
2318 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2319 gettext("started at"), tbuf
);
2321 case VDEV_TRIM_COMPLETE
:
2322 (void) snprintf(zbuf
, sizeof (zbuf
), ", %s %s",
2323 gettext("completed at"), tbuf
);
2327 (void) printf(gettext(" (%d%% trimmed%s)"),
2329 } else if (vs
->vs_trim_notsup
) {
2330 (void) printf(gettext(" (trim unsupported)"));
2332 (void) printf(gettext(" (untrimmed)"));
2334 } else if (vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
) {
2335 (void) printf(gettext(" (trimming)"));
2340 * Return the color associated with a health string. This includes returning
2341 * NULL for no color change.
2344 health_str_to_color(const char *health
)
2346 if (strcmp(health
, gettext("FAULTED")) == 0 ||
2347 strcmp(health
, gettext("SUSPENDED")) == 0 ||
2348 strcmp(health
, gettext("UNAVAIL")) == 0) {
2352 if (strcmp(health
, gettext("OFFLINE")) == 0 ||
2353 strcmp(health
, gettext("DEGRADED")) == 0 ||
2354 strcmp(health
, gettext("REMOVED")) == 0) {
2355 return (ANSI_YELLOW
);
2362 * Called for each leaf vdev. Returns 0 if the vdev is healthy.
2363 * A vdev is unhealthy if any of the following are true:
2364 * 1) there are read, write, or checksum errors,
2365 * 2) its state is not ONLINE, or
2366 * 3) slow IO reporting was requested (-s) and there are slow IOs.
2369 vdev_health_check_cb(void *hdl_data
, nvlist_t
*nv
, void *data
)
2371 status_cbdata_t
*cb
= data
;
2376 if (nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2377 (uint64_t **)&vs
, &vsc
) != 0)
2380 if (vs
->vs_checksum_errors
|| vs
->vs_read_errors
||
2381 vs
->vs_write_errors
|| vs
->vs_state
!= VDEV_STATE_HEALTHY
)
2384 if (cb
->cb_print_slow_ios
&& vs
->vs_slow_ios
)
2391 * Print out configuration state as requested by status_callback.
2394 print_status_config(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, const char *name
,
2395 nvlist_t
*nv
, int depth
, boolean_t isspare
, vdev_rebuild_stat_t
*vrs
)
2397 nvlist_t
**child
, *root
;
2398 uint_t c
, i
, vsc
, children
;
2399 pool_scan_stat_t
*ps
= NULL
;
2401 char rbuf
[6], wbuf
[6], cbuf
[6];
2403 uint64_t notpresent
;
2404 spare_cbdata_t spare_cb
;
2407 const char *path
= NULL
;
2408 const char *rcolor
= NULL
, *wcolor
= NULL
, *ccolor
= NULL
,
2411 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2412 &child
, &children
) != 0)
2415 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2416 (uint64_t **)&vs
, &vsc
) == 0);
2418 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2420 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2423 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2427 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2430 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
2431 state
= gettext("INUSE");
2432 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
2433 state
= gettext("AVAIL");
2437 * If '-e' is specified then top-level vdevs and their children
2438 * can be pruned if all of their leaves are healthy.
2440 if (cb
->cb_print_unhealthy
&& depth
> 0 &&
2441 for_each_vdev_in_nvlist(nv
, vdev_health_check_cb
, cb
) == 0) {
2445 printf_color(health_str_to_color(state
),
2446 "\t%*s%-*s %-8s", depth
, "", cb
->cb_namewidth
- depth
,
2450 if (vs
->vs_read_errors
)
2453 if (vs
->vs_write_errors
)
2456 if (vs
->vs_checksum_errors
)
2459 if (vs
->vs_slow_ios
)
2462 if (cb
->cb_literal
) {
2464 printf_color(rcolor
, "%5llu",
2465 (u_longlong_t
)vs
->vs_read_errors
);
2467 printf_color(wcolor
, "%5llu",
2468 (u_longlong_t
)vs
->vs_write_errors
);
2470 printf_color(ccolor
, "%5llu",
2471 (u_longlong_t
)vs
->vs_checksum_errors
);
2473 zfs_nicenum(vs
->vs_read_errors
, rbuf
, sizeof (rbuf
));
2474 zfs_nicenum(vs
->vs_write_errors
, wbuf
, sizeof (wbuf
));
2475 zfs_nicenum(vs
->vs_checksum_errors
, cbuf
,
2478 printf_color(rcolor
, "%5s", rbuf
);
2480 printf_color(wcolor
, "%5s", wbuf
);
2482 printf_color(ccolor
, "%5s", cbuf
);
2484 if (cb
->cb_print_slow_ios
) {
2485 if (children
== 0) {
2486 /* Only leafs vdevs have slow IOs */
2487 zfs_nicenum(vs
->vs_slow_ios
, rbuf
,
2490 snprintf(rbuf
, sizeof (rbuf
), "-");
2494 printf_color(scolor
, " %5llu",
2495 (u_longlong_t
)vs
->vs_slow_ios
);
2497 printf_color(scolor
, " %5s", rbuf
);
2499 if (cb
->cb_print_power
) {
2500 if (children
== 0) {
2501 /* Only leaf vdevs have physical slots */
2502 switch (zpool_power_current_state(zhp
, (char *)
2503 fnvlist_lookup_string(nv
,
2504 ZPOOL_CONFIG_PATH
))) {
2506 printf_color(ANSI_RED
, " %5s",
2510 printf(" %5s", gettext("on"));
2513 printf(" %5s", "-");
2516 printf(" %5s", "-");
2521 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
2522 ¬present
) == 0) {
2523 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0);
2524 (void) printf(" %s %s", gettext("was"), path
);
2525 } else if (vs
->vs_aux
!= 0) {
2527 color_start(ANSI_RED
);
2528 switch (vs
->vs_aux
) {
2529 case VDEV_AUX_OPEN_FAILED
:
2530 (void) printf(gettext("cannot open"));
2533 case VDEV_AUX_BAD_GUID_SUM
:
2534 (void) printf(gettext("missing device"));
2537 case VDEV_AUX_NO_REPLICAS
:
2538 (void) printf(gettext("insufficient replicas"));
2541 case VDEV_AUX_VERSION_NEWER
:
2542 (void) printf(gettext("newer version"));
2545 case VDEV_AUX_UNSUP_FEAT
:
2546 (void) printf(gettext("unsupported feature(s)"));
2549 case VDEV_AUX_ASHIFT_TOO_BIG
:
2550 (void) printf(gettext("unsupported minimum blocksize"));
2553 case VDEV_AUX_SPARED
:
2554 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2555 &spare_cb
.cb_guid
) == 0);
2556 if (zpool_iter(g_zfs
, find_spare
, &spare_cb
) == 1) {
2557 if (strcmp(zpool_get_name(spare_cb
.cb_zhp
),
2558 zpool_get_name(zhp
)) == 0)
2559 (void) printf(gettext("currently in "
2562 (void) printf(gettext("in use by "
2564 zpool_get_name(spare_cb
.cb_zhp
));
2565 zpool_close(spare_cb
.cb_zhp
);
2567 (void) printf(gettext("currently in use"));
2571 case VDEV_AUX_ERR_EXCEEDED
:
2572 if (vs
->vs_read_errors
+ vs
->vs_write_errors
+
2573 vs
->vs_checksum_errors
== 0 && children
== 0 &&
2574 vs
->vs_slow_ios
> 0) {
2575 (void) printf(gettext("too many slow I/Os"));
2577 (void) printf(gettext("too many errors"));
2581 case VDEV_AUX_IO_FAILURE
:
2582 (void) printf(gettext("experienced I/O failures"));
2585 case VDEV_AUX_BAD_LOG
:
2586 (void) printf(gettext("bad intent log"));
2589 case VDEV_AUX_EXTERNAL
:
2590 (void) printf(gettext("external device fault"));
2593 case VDEV_AUX_SPLIT_POOL
:
2594 (void) printf(gettext("split into new pool"));
2597 case VDEV_AUX_ACTIVE
:
2598 (void) printf(gettext("currently in use"));
2601 case VDEV_AUX_CHILDREN_OFFLINE
:
2602 (void) printf(gettext("all children offline"));
2605 case VDEV_AUX_BAD_LABEL
:
2606 (void) printf(gettext("invalid label"));
2610 (void) printf(gettext("corrupted data"));
2614 } else if (children
== 0 && !isspare
&&
2615 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL
&&
2616 VDEV_STAT_VALID(vs_physical_ashift
, vsc
) &&
2617 vs
->vs_configured_ashift
< vs
->vs_physical_ashift
) {
2619 gettext(" block size: %dB configured, %dB native"),
2620 1 << vs
->vs_configured_ashift
, 1 << vs
->vs_physical_ashift
);
2623 if (vs
->vs_scan_removing
!= 0) {
2624 (void) printf(gettext(" (removing)"));
2625 } else if (VDEV_STAT_VALID(vs_noalloc
, vsc
) && vs
->vs_noalloc
!= 0) {
2626 (void) printf(gettext(" (non-allocating)"));
2629 /* The root vdev has the scrub/resilver stats */
2630 root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2631 ZPOOL_CONFIG_VDEV_TREE
);
2632 (void) nvlist_lookup_uint64_array(root
, ZPOOL_CONFIG_SCAN_STATS
,
2633 (uint64_t **)&ps
, &c
);
2636 * If you force fault a drive that's resilvering, its scan stats can
2637 * get frozen in time, giving the false impression that it's
2638 * being resilvered. That's why we check the state to see if the vdev
2639 * is healthy before reporting "resilvering" or "repairing".
2641 if (ps
!= NULL
&& ps
->pss_state
== DSS_SCANNING
&& children
== 0 &&
2642 vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2643 if (vs
->vs_scan_processed
!= 0) {
2644 (void) printf(gettext(" (%s)"),
2645 (ps
->pss_func
== POOL_SCAN_RESILVER
) ?
2646 "resilvering" : "repairing");
2647 } else if (vs
->vs_resilver_deferred
) {
2648 (void) printf(gettext(" (awaiting resilver)"));
2652 /* The top-level vdevs have the rebuild stats */
2653 if (vrs
!= NULL
&& vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
&&
2654 children
== 0 && vs
->vs_state
== VDEV_STATE_HEALTHY
) {
2655 if (vs
->vs_rebuild_processed
!= 0) {
2656 (void) printf(gettext(" (resilvering)"));
2660 if (cb
->vcdl
!= NULL
) {
2661 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
2663 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
2667 /* Display vdev initialization and trim status for leaves. */
2668 if (children
== 0) {
2669 print_status_initialize(vs
, cb
->cb_print_vdev_init
);
2670 print_status_trim(vs
, cb
->cb_print_vdev_trim
);
2673 (void) printf("\n");
2675 for (c
= 0; c
< children
; c
++) {
2676 uint64_t islog
= B_FALSE
, ishole
= B_FALSE
;
2678 /* Don't print logs or holes here */
2679 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2681 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2683 if (islog
|| ishole
)
2685 /* Only print normal classes here */
2686 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2689 /* Provide vdev_rebuild_stats to children if available */
2691 (void) nvlist_lookup_uint64_array(nv
,
2692 ZPOOL_CONFIG_REBUILD_STATS
,
2693 (uint64_t **)&vrs
, &i
);
2696 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2697 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2698 print_status_config(zhp
, cb
, vname
, child
[c
], depth
+ 2,
2705 * Print the configuration of an exported pool. Iterate over all vdevs in the
2706 * pool, printing out the name and status for each one.
2709 print_import_config(status_cbdata_t
*cb
, const char *name
, nvlist_t
*nv
,
2718 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
2719 if (strcmp(type
, VDEV_TYPE_MISSING
) == 0 ||
2720 strcmp(type
, VDEV_TYPE_HOLE
) == 0)
2723 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
2724 (uint64_t **)&vs
, &c
) == 0);
2726 (void) printf("\t%*s%-*s", depth
, "", cb
->cb_namewidth
- depth
, name
);
2727 (void) printf(" %s", zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
));
2729 if (vs
->vs_aux
!= 0) {
2732 switch (vs
->vs_aux
) {
2733 case VDEV_AUX_OPEN_FAILED
:
2734 (void) printf(gettext("cannot open"));
2737 case VDEV_AUX_BAD_GUID_SUM
:
2738 (void) printf(gettext("missing device"));
2741 case VDEV_AUX_NO_REPLICAS
:
2742 (void) printf(gettext("insufficient replicas"));
2745 case VDEV_AUX_VERSION_NEWER
:
2746 (void) printf(gettext("newer version"));
2749 case VDEV_AUX_UNSUP_FEAT
:
2750 (void) printf(gettext("unsupported feature(s)"));
2753 case VDEV_AUX_ERR_EXCEEDED
:
2754 (void) printf(gettext("too many errors"));
2757 case VDEV_AUX_ACTIVE
:
2758 (void) printf(gettext("currently in use"));
2761 case VDEV_AUX_CHILDREN_OFFLINE
:
2762 (void) printf(gettext("all children offline"));
2765 case VDEV_AUX_BAD_LABEL
:
2766 (void) printf(gettext("invalid label"));
2770 (void) printf(gettext("corrupted data"));
2774 (void) printf("\n");
2776 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2777 &child
, &children
) != 0)
2780 for (c
= 0; c
< children
; c
++) {
2781 uint64_t is_log
= B_FALSE
;
2783 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2787 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
2790 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2791 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2792 print_import_config(cb
, vname
, child
[c
], depth
+ 2);
2796 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2797 &child
, &children
) == 0) {
2798 (void) printf(gettext("\tcache\n"));
2799 for (c
= 0; c
< children
; c
++) {
2800 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2802 (void) printf("\t %s\n", vname
);
2807 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2808 &child
, &children
) == 0) {
2809 (void) printf(gettext("\tspares\n"));
2810 for (c
= 0; c
< children
; c
++) {
2811 vname
= zpool_vdev_name(g_zfs
, NULL
, child
[c
],
2813 (void) printf("\t %s\n", vname
);
2820 * Print specialized class vdevs.
2822 * These are recorded as top level vdevs in the main pool child array
2823 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2824 * print_status_config() or print_import_config() to print the top level
2825 * class vdevs then any of their children (eg mirrored slogs) are printed
2826 * recursively - which works because only the top level vdev is marked.
2829 print_class_vdevs(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
*nv
,
2834 boolean_t printed
= B_FALSE
;
2836 assert(zhp
!= NULL
|| !cb
->cb_verbose
);
2838 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
, &child
,
2842 for (c
= 0; c
< children
; c
++) {
2843 uint64_t is_log
= B_FALSE
;
2844 const char *bias
= NULL
;
2845 const char *type
= NULL
;
2847 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2851 bias
= (char *)VDEV_ALLOC_CLASS_LOGS
;
2853 (void) nvlist_lookup_string(child
[c
],
2854 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
2855 (void) nvlist_lookup_string(child
[c
],
2856 ZPOOL_CONFIG_TYPE
, &type
);
2859 if (bias
== NULL
|| strcmp(bias
, class) != 0)
2861 if (!is_log
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
2865 (void) printf("\t%s\t\n", gettext(class));
2869 char *name
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
2870 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
2871 if (cb
->cb_print_status
)
2872 print_status_config(zhp
, cb
, name
, child
[c
], 2,
2875 print_import_config(cb
, name
, child
[c
], 2);
2881 * Display the status for the given pool.
2884 show_import(nvlist_t
*config
, boolean_t report_error
)
2886 uint64_t pool_state
;
2890 uint64_t hostid
= 0;
2892 const char *hostname
= "unknown";
2893 nvlist_t
*nvroot
, *nvinfo
;
2894 zpool_status_t reason
;
2895 zpool_errata_t errata
;
2898 const char *comment
;
2899 status_cbdata_t cb
= { 0 };
2901 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
2903 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
2905 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
2907 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2910 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
2911 (uint64_t **)&vs
, &vsc
) == 0);
2912 health
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
2914 reason
= zpool_import_status(config
, &msgid
, &errata
);
2917 * If we're importing using a cachefile, then we won't report any
2918 * errors unless we are in the scan phase of the import.
2920 if (reason
!= ZPOOL_STATUS_OK
&& !report_error
)
2923 (void) printf(gettext(" pool: %s\n"), name
);
2924 (void) printf(gettext(" id: %llu\n"), (u_longlong_t
)guid
);
2925 (void) printf(gettext(" state: %s"), health
);
2926 if (pool_state
== POOL_STATE_DESTROYED
)
2927 (void) printf(gettext(" (DESTROYED)"));
2928 (void) printf("\n");
2931 case ZPOOL_STATUS_MISSING_DEV_R
:
2932 case ZPOOL_STATUS_MISSING_DEV_NR
:
2933 case ZPOOL_STATUS_BAD_GUID_SUM
:
2934 printf_color(ANSI_BOLD
, gettext("status: "));
2935 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
2936 "missing from the system.\n"));
2939 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
2940 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
2941 printf_color(ANSI_BOLD
, gettext("status: "));
2942 printf_color(ANSI_YELLOW
, gettext("One or more devices contains"
2943 " corrupted data.\n"));
2946 case ZPOOL_STATUS_CORRUPT_DATA
:
2948 gettext(" status: The pool data is corrupted.\n"));
2951 case ZPOOL_STATUS_OFFLINE_DEV
:
2952 printf_color(ANSI_BOLD
, gettext("status: "));
2953 printf_color(ANSI_YELLOW
, gettext("One or more devices "
2954 "are offlined.\n"));
2957 case ZPOOL_STATUS_CORRUPT_POOL
:
2958 printf_color(ANSI_BOLD
, gettext("status: "));
2959 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
2963 case ZPOOL_STATUS_VERSION_OLDER
:
2964 printf_color(ANSI_BOLD
, gettext("status: "));
2965 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2966 "a legacy on-disk version.\n"));
2969 case ZPOOL_STATUS_VERSION_NEWER
:
2970 printf_color(ANSI_BOLD
, gettext("status: "));
2971 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
2972 "an incompatible version.\n"));
2975 case ZPOOL_STATUS_FEAT_DISABLED
:
2976 printf_color(ANSI_BOLD
, gettext("status: "));
2977 printf_color(ANSI_YELLOW
, gettext("Some supported "
2978 "features are not enabled on the pool.\n\t"
2979 "(Note that they may be intentionally disabled "
2980 "if the\n\t'compatibility' property is set.)\n"));
2983 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
2984 printf_color(ANSI_BOLD
, gettext("status: "));
2985 printf_color(ANSI_YELLOW
, gettext("Error reading or parsing "
2986 "the file(s) indicated by the 'compatibility'\n"
2990 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
2991 printf_color(ANSI_BOLD
, gettext("status: "));
2992 printf_color(ANSI_YELLOW
, gettext("One or more features "
2993 "are enabled on the pool despite not being\n"
2994 "requested by the 'compatibility' property.\n"));
2997 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
2998 printf_color(ANSI_BOLD
, gettext("status: "));
2999 printf_color(ANSI_YELLOW
, gettext("The pool uses the following "
3000 "feature(s) not supported on this system:\n"));
3001 color_start(ANSI_YELLOW
);
3002 zpool_print_unsup_feat(config
);
3006 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3007 printf_color(ANSI_BOLD
, gettext("status: "));
3008 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
3009 "accessed in read-only mode on this system. It\n\tcannot be"
3010 " accessed in read-write mode because it uses the "
3011 "following\n\tfeature(s) not supported on this system:\n"));
3012 color_start(ANSI_YELLOW
);
3013 zpool_print_unsup_feat(config
);
3017 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3018 printf_color(ANSI_BOLD
, gettext("status: "));
3019 printf_color(ANSI_YELLOW
, gettext("The pool is currently "
3020 "imported by another system.\n"));
3023 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3024 printf_color(ANSI_BOLD
, gettext("status: "));
3025 printf_color(ANSI_YELLOW
, gettext("The pool has the "
3026 "multihost property on. It cannot\n\tbe safely imported "
3027 "when the system hostid is not set.\n"));
3030 case ZPOOL_STATUS_HOSTID_MISMATCH
:
3031 printf_color(ANSI_BOLD
, gettext("status: "));
3032 printf_color(ANSI_YELLOW
, gettext("The pool was last accessed "
3033 "by another system.\n"));
3036 case ZPOOL_STATUS_FAULTED_DEV_R
:
3037 case ZPOOL_STATUS_FAULTED_DEV_NR
:
3038 printf_color(ANSI_BOLD
, gettext("status: "));
3039 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
3043 case ZPOOL_STATUS_BAD_LOG
:
3044 printf_color(ANSI_BOLD
, gettext("status: "));
3045 printf_color(ANSI_YELLOW
, gettext("An intent log record cannot "
3049 case ZPOOL_STATUS_RESILVERING
:
3050 case ZPOOL_STATUS_REBUILDING
:
3051 printf_color(ANSI_BOLD
, gettext("status: "));
3052 printf_color(ANSI_YELLOW
, gettext("One or more devices were "
3053 "being resilvered.\n"));
3056 case ZPOOL_STATUS_ERRATA
:
3057 printf_color(ANSI_BOLD
, gettext("status: "));
3058 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
3062 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
3063 printf_color(ANSI_BOLD
, gettext("status: "));
3064 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
3065 "configured to use a non-native block size.\n"
3066 "\tExpect reduced performance.\n"));
3071 * No other status can be seen when importing pools.
3073 assert(reason
== ZPOOL_STATUS_OK
);
3077 * Print out an action according to the overall state of the pool.
3079 if (vs
->vs_state
== VDEV_STATE_HEALTHY
) {
3080 if (reason
== ZPOOL_STATUS_VERSION_OLDER
||
3081 reason
== ZPOOL_STATUS_FEAT_DISABLED
) {
3082 (void) printf(gettext(" action: The pool can be "
3083 "imported using its name or numeric identifier, "
3084 "though\n\tsome features will not be available "
3085 "without an explicit 'zpool upgrade'.\n"));
3086 } else if (reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
) {
3087 (void) printf(gettext(" action: The pool can be "
3088 "imported using its name or numeric\n\tidentifier, "
3089 "though the file(s) indicated by its "
3090 "'compatibility'\n\tproperty cannot be parsed at "
3092 } else if (reason
== ZPOOL_STATUS_HOSTID_MISMATCH
) {
3093 (void) printf(gettext(" action: The pool can be "
3094 "imported using its name or numeric "
3095 "identifier and\n\tthe '-f' flag.\n"));
3096 } else if (reason
== ZPOOL_STATUS_ERRATA
) {
3098 case ZPOOL_ERRATA_NONE
:
3101 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
3102 (void) printf(gettext(" action: The pool can "
3103 "be imported using its name or numeric "
3104 "identifier,\n\thowever there is a compat"
3105 "ibility issue which should be corrected"
3106 "\n\tby running 'zpool scrub'\n"));
3109 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
:
3110 (void) printf(gettext(" action: The pool can"
3111 "not be imported with this version of ZFS "
3112 "due to\n\tan active asynchronous destroy. "
3113 "Revert to an earlier version\n\tand "
3114 "allow the destroy to complete before "
3118 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
3119 (void) printf(gettext(" action: Existing "
3120 "encrypted datasets contain an on-disk "
3121 "incompatibility, which\n\tneeds to be "
3122 "corrected. Backup these datasets to new "
3123 "encrypted datasets\n\tand destroy the "
3127 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
3128 (void) printf(gettext(" action: Existing "
3129 "encrypted snapshots and bookmarks contain "
3130 "an on-disk\n\tincompatibility. This may "
3131 "cause on-disk corruption if they are used"
3132 "\n\twith 'zfs recv'. To correct the "
3133 "issue, enable the bookmark_v2 feature.\n\t"
3134 "No additional action is needed if there "
3135 "are no encrypted snapshots or\n\t"
3136 "bookmarks. If preserving the encrypted "
3137 "snapshots and bookmarks is\n\trequired, "
3138 "use a non-raw send to backup and restore "
3139 "them. Alternately,\n\tthey may be removed"
3140 " to resolve the incompatibility.\n"));
3144 * All errata must contain an action message.
3149 (void) printf(gettext(" action: The pool can be "
3150 "imported using its name or numeric "
3153 } else if (vs
->vs_state
== VDEV_STATE_DEGRADED
) {
3154 (void) printf(gettext(" action: The pool can be imported "
3155 "despite missing or damaged devices. The\n\tfault "
3156 "tolerance of the pool may be compromised if imported.\n"));
3159 case ZPOOL_STATUS_VERSION_NEWER
:
3160 (void) printf(gettext(" action: The pool cannot be "
3161 "imported. Access the pool on a system running "
3162 "newer\n\tsoftware, or recreate the pool from "
3165 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
3166 printf_color(ANSI_BOLD
, gettext("action: "));
3167 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3168 "imported. Access the pool on a system that "
3169 "supports\n\tthe required feature(s), or recreate "
3170 "the pool from backup.\n"));
3172 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
3173 printf_color(ANSI_BOLD
, gettext("action: "));
3174 printf_color(ANSI_YELLOW
, gettext("The pool cannot be "
3175 "imported in read-write mode. Import the pool "
3177 "\t\"-o readonly=on\", access the pool on a system "
3178 "that supports the\n\trequired feature(s), or "
3179 "recreate the pool from backup.\n"));
3181 case ZPOOL_STATUS_MISSING_DEV_R
:
3182 case ZPOOL_STATUS_MISSING_DEV_NR
:
3183 case ZPOOL_STATUS_BAD_GUID_SUM
:
3184 (void) printf(gettext(" action: The pool cannot be "
3185 "imported. Attach the missing\n\tdevices and try "
3188 case ZPOOL_STATUS_HOSTID_ACTIVE
:
3189 VERIFY0(nvlist_lookup_nvlist(config
,
3190 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
));
3192 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3193 hostname
= fnvlist_lookup_string(nvinfo
,
3194 ZPOOL_CONFIG_MMP_HOSTNAME
);
3196 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3197 hostid
= fnvlist_lookup_uint64(nvinfo
,
3198 ZPOOL_CONFIG_MMP_HOSTID
);
3200 (void) printf(gettext(" action: The pool must be "
3201 "exported from %s (hostid=%"PRIx64
")\n\tbefore it "
3202 "can be safely imported.\n"), hostname
, hostid
);
3204 case ZPOOL_STATUS_HOSTID_REQUIRED
:
3205 (void) printf(gettext(" action: Set a unique system "
3206 "hostid with the zgenhostid(8) command.\n"));
3209 (void) printf(gettext(" action: The pool cannot be "
3210 "imported due to damaged devices or data.\n"));
3214 /* Print the comment attached to the pool. */
3215 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3216 (void) printf(gettext("comment: %s\n"), comment
);
3219 * If the state is "closed" or "can't open", and the aux state
3220 * is "corrupt data":
3222 if (((vs
->vs_state
== VDEV_STATE_CLOSED
) ||
3223 (vs
->vs_state
== VDEV_STATE_CANT_OPEN
)) &&
3224 (vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)) {
3225 if (pool_state
== POOL_STATE_DESTROYED
)
3226 (void) printf(gettext("\tThe pool was destroyed, "
3227 "but can be imported using the '-Df' flags.\n"));
3228 else if (pool_state
!= POOL_STATE_EXPORTED
)
3229 (void) printf(gettext("\tThe pool may be active on "
3230 "another system, but can be imported using\n\t"
3231 "the '-f' flag.\n"));
3234 if (msgid
!= NULL
) {
3235 (void) printf(gettext(
3236 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3240 (void) printf(gettext(" config:\n\n"));
3242 cb
.cb_namewidth
= max_width(NULL
, nvroot
, 0, strlen(name
),
3244 if (cb
.cb_namewidth
< 10)
3245 cb
.cb_namewidth
= 10;
3247 print_import_config(&cb
, name
, nvroot
, 0);
3249 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
3250 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
3251 print_class_vdevs(NULL
, &cb
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
3253 if (reason
== ZPOOL_STATUS_BAD_GUID_SUM
) {
3254 (void) printf(gettext("\n\tAdditional devices are known to "
3255 "be part of this pool, though their\n\texact "
3256 "configuration cannot be determined.\n"));
3262 zfs_force_import_required(nvlist_t
*config
)
3265 uint64_t hostid
= 0;
3268 state
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
);
3269 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3272 * The hostid on LOAD_INFO comes from the MOS label via
3273 * spa_tryimport(). If its not there then we're likely talking to an
3274 * older kernel, so use the top one, which will be from the label
3275 * discovered in zpool_find_import(), or if a cachefile is in use, the
3278 if (nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_HOSTID
, &hostid
) != 0)
3279 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
,
3282 if (state
!= POOL_STATE_EXPORTED
&& hostid
!= get_system_hostid())
3285 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
)) {
3286 mmp_state_t mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3287 ZPOOL_CONFIG_MMP_STATE
);
3289 if (mmp_state
!= MMP_STATE_INACTIVE
)
3297 * Perform the import for the given configuration. This passes the heavy
3298 * lifting off to zpool_import_props(), and then mounts the datasets contained
3302 do_import(nvlist_t
*config
, const char *newname
, const char *mntopts
,
3303 nvlist_t
*props
, int flags
)
3307 zpool_handle_t
*zhp
;
3311 name
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
3312 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3314 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
3315 (void) fprintf(stderr
, gettext("cannot import '%s': pool "
3316 "is formatted using an unsupported ZFS version\n"), name
);
3318 } else if (zfs_force_import_required(config
) &&
3319 !(flags
& ZFS_IMPORT_ANY_HOST
)) {
3320 mmp_state_t mmp_state
= MMP_STATE_INACTIVE
;
3323 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3324 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_STATE
))
3325 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
3326 ZPOOL_CONFIG_MMP_STATE
);
3328 if (mmp_state
== MMP_STATE_ACTIVE
) {
3329 const char *hostname
= "<unknown>";
3330 uint64_t hostid
= 0;
3332 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTNAME
))
3333 hostname
= fnvlist_lookup_string(nvinfo
,
3334 ZPOOL_CONFIG_MMP_HOSTNAME
);
3336 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_HOSTID
))
3337 hostid
= fnvlist_lookup_uint64(nvinfo
,
3338 ZPOOL_CONFIG_MMP_HOSTID
);
3340 (void) fprintf(stderr
, gettext("cannot import '%s': "
3341 "pool is imported on %s (hostid: "
3342 "0x%"PRIx64
")\nExport the pool on the other "
3343 "system, then run 'zpool import'.\n"),
3344 name
, hostname
, hostid
);
3345 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
3346 (void) fprintf(stderr
, gettext("Cannot import '%s': "
3347 "pool has the multihost property on and the\n"
3348 "system's hostid is not set. Set a unique hostid "
3349 "with the zgenhostid(8) command.\n"), name
);
3351 const char *hostname
= "<unknown>";
3352 time_t timestamp
= 0;
3353 uint64_t hostid
= 0;
3355 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_HOSTNAME
))
3356 hostname
= fnvlist_lookup_string(nvinfo
,
3357 ZPOOL_CONFIG_HOSTNAME
);
3358 else if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTNAME
))
3359 hostname
= fnvlist_lookup_string(config
,
3360 ZPOOL_CONFIG_HOSTNAME
);
3362 if (nvlist_exists(config
, ZPOOL_CONFIG_TIMESTAMP
))
3363 timestamp
= fnvlist_lookup_uint64(config
,
3364 ZPOOL_CONFIG_TIMESTAMP
);
3366 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_HOSTID
))
3367 hostid
= fnvlist_lookup_uint64(nvinfo
,
3368 ZPOOL_CONFIG_HOSTID
);
3369 else if (nvlist_exists(config
, ZPOOL_CONFIG_HOSTID
))
3370 hostid
= fnvlist_lookup_uint64(config
,
3371 ZPOOL_CONFIG_HOSTID
);
3373 (void) fprintf(stderr
, gettext("cannot import '%s': "
3374 "pool was previously in use from another system.\n"
3375 "Last accessed by %s (hostid=%"PRIx64
") at %s"
3376 "The pool can be imported, use 'zpool import -f' "
3377 "to import the pool.\n"), name
, hostname
,
3378 hostid
, ctime(×tamp
));
3384 if (zpool_import_props(g_zfs
, config
, newname
, props
, flags
) != 0)
3387 if (newname
!= NULL
)
3390 if ((zhp
= zpool_open_canfail(g_zfs
, name
)) == NULL
)
3394 * Loading keys is best effort. We don't want to return immediately
3395 * if it fails but we do want to give the error to the caller.
3397 if (flags
& ZFS_IMPORT_LOAD_KEYS
&&
3398 zfs_crypto_attempt_load_keys(g_zfs
, name
) != 0)
3401 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
&&
3402 !(flags
& ZFS_IMPORT_ONLY
)) {
3403 ms_status
= zpool_enable_datasets(zhp
, mntopts
, 0);
3404 if (ms_status
== EZFS_SHAREFAILED
) {
3405 (void) fprintf(stderr
, gettext("Import was "
3406 "successful, but unable to share some datasets"));
3407 } else if (ms_status
== EZFS_MOUNTFAILED
) {
3408 (void) fprintf(stderr
, gettext("Import was "
3409 "successful, but unable to mount some datasets"));
3418 import_pools(nvlist_t
*pools
, nvlist_t
*props
, char *mntopts
, int flags
,
3419 char *orig_name
, char *new_name
,
3420 boolean_t do_destroyed
, boolean_t pool_specified
, boolean_t do_all
,
3421 importargs_t
*import
)
3423 nvlist_t
*config
= NULL
;
3424 nvlist_t
*found_config
= NULL
;
3425 uint64_t pool_state
;
3428 * At this point we have a list of import candidate configs. Even if
3429 * we were searching by pool name or guid, we still need to
3430 * post-process the list to deal with pool state and possible
3434 nvpair_t
*elem
= NULL
;
3435 boolean_t first
= B_TRUE
;
3436 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
3438 verify(nvpair_value_nvlist(elem
, &config
) == 0);
3440 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
3442 if (!do_destroyed
&& pool_state
== POOL_STATE_DESTROYED
)
3444 if (do_destroyed
&& pool_state
!= POOL_STATE_DESTROYED
)
3447 verify(nvlist_add_nvlist(config
, ZPOOL_LOAD_POLICY
,
3448 import
->policy
) == 0);
3450 if (!pool_specified
) {
3454 (void) fputc('\n', stdout
);
3457 err
|= do_import(config
, NULL
, mntopts
,
3461 * If we're importing from cachefile, then
3462 * we don't want to report errors until we
3463 * are in the scan phase of the import. If
3464 * we get an error, then we return that error
3465 * to invoke the scan phase.
3467 if (import
->cachefile
&& !import
->scan
)
3468 err
= show_import(config
, B_FALSE
);
3470 (void) show_import(config
, B_TRUE
);
3472 } else if (import
->poolname
!= NULL
) {
3476 * We are searching for a pool based on name.
3478 verify(nvlist_lookup_string(config
,
3479 ZPOOL_CONFIG_POOL_NAME
, &name
) == 0);
3481 if (strcmp(name
, import
->poolname
) == 0) {
3482 if (found_config
!= NULL
) {
3483 (void) fprintf(stderr
, gettext(
3484 "cannot import '%s': more than "
3485 "one matching pool\n"),
3487 (void) fprintf(stderr
, gettext(
3488 "import by numeric ID instead\n"));
3491 found_config
= config
;
3497 * Search for a pool by guid.
3499 verify(nvlist_lookup_uint64(config
,
3500 ZPOOL_CONFIG_POOL_GUID
, &guid
) == 0);
3502 if (guid
== import
->guid
)
3503 found_config
= config
;
3508 * If we were searching for a specific pool, verify that we found a
3509 * pool, and then do the import.
3511 if (pool_specified
&& err
== 0) {
3512 if (found_config
== NULL
) {
3513 (void) fprintf(stderr
, gettext("cannot import '%s': "
3514 "no such pool available\n"), orig_name
);
3517 err
|= do_import(found_config
, new_name
,
3518 mntopts
, props
, flags
);
3523 * If we were just looking for pools, report an error if none were
3526 if (!pool_specified
&& first
)
3527 (void) fprintf(stderr
,
3528 gettext("no pools available to import\n"));
3532 typedef struct target_exists_args
{
3533 const char *poolname
;
3535 } target_exists_args_t
;
3538 name_or_guid_exists(zpool_handle_t
*zhp
, void *data
)
3540 target_exists_args_t
*args
= data
;
3541 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
3547 if (args
->poolname
!= NULL
) {
3548 const char *pool_name
;
3550 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3552 if (strcmp(pool_name
, args
->poolname
) == 0)
3557 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3559 if (pool_guid
== args
->poolguid
)
3567 * zpool checkpoint <pool>
3568 * checkpoint --discard <pool>
3570 * -d Discard the checkpoint from a checkpointed
3573 * -w Wait for discarding a checkpoint to complete.
3576 * Checkpoints the specified pool, by taking a "snapshot" of its
3577 * current state. A pool can only have one checkpoint at a time.
3580 zpool_do_checkpoint(int argc
, char **argv
)
3582 boolean_t discard
, wait
;
3584 zpool_handle_t
*zhp
;
3587 struct option long_options
[] = {
3588 {"discard", no_argument
, NULL
, 'd'},
3589 {"wait", no_argument
, NULL
, 'w'},
3595 while ((c
= getopt_long(argc
, argv
, ":dw", long_options
, NULL
)) != -1) {
3604 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3610 if (wait
&& !discard
) {
3611 (void) fprintf(stderr
, gettext("--wait only valid when "
3612 "--discard also specified\n"));
3620 (void) fprintf(stderr
, gettext("missing pool argument\n"));
3625 (void) fprintf(stderr
, gettext("too many arguments\n"));
3631 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
) {
3632 /* As a special case, check for use of '/' in the name */
3633 if (strchr(pool
, '/') != NULL
)
3634 (void) fprintf(stderr
, gettext("'zpool checkpoint' "
3635 "doesn't work on datasets. To save the state "
3636 "of a dataset from a specific point in time "
3637 "please use 'zfs snapshot'\n"));
3642 err
= (zpool_discard_checkpoint(zhp
) != 0);
3643 if (err
== 0 && wait
)
3644 err
= zpool_wait(zhp
, ZPOOL_WAIT_CKPT_DISCARD
);
3646 err
= (zpool_checkpoint(zhp
) != 0);
3654 #define CHECKPOINT_OPT 1024
3657 * zpool import [-d dir] [-D]
3658 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3659 * [-d dir | -c cachefile | -s] [-f] -a
3660 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3661 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3664 * -c Read pool information from a cachefile instead of searching
3665 * devices. If importing from a cachefile config fails, then
3666 * fallback to searching for devices only in the directories that
3667 * exist in the cachefile.
3669 * -d Scan in a specific directory, other than /dev/. More than
3670 * one directory can be specified using multiple '-d' options.
3672 * -D Scan for previously destroyed pools or import all or only
3673 * specified destroyed pools.
3675 * -R Temporarily import the pool, with all mountpoints relative to
3676 * the given root. The pool will remain exported when the machine
3679 * -V Import even in the presence of faulted vdevs. This is an
3680 * intentionally undocumented option for testing purposes, and
3681 * treats the pool configuration as complete, leaving any bad
3682 * vdevs in the FAULTED state. In other words, it does verbatim
3685 * -f Force import, even if it appears that the pool is active.
3687 * -F Attempt rewind if necessary.
3689 * -n See if rewind would work, but don't actually rewind.
3691 * -N Import the pool but don't mount datasets.
3693 * -T Specify a starting txg to use for import. This option is
3694 * intentionally undocumented option for testing purposes.
3696 * -a Import all pools found.
3698 * -l Load encryption keys while importing.
3700 * -o Set property=value and/or temporary mount options (without '=').
3702 * -s Scan using the default search path, the libblkid cache will
3705 * --rewind-to-checkpoint
3706 * Import the pool and revert back to the checkpoint.
3708 * The import command scans for pools to import, and import pools based on pool
3709 * name and GUID. The pool can also be renamed as part of the import process.
3712 zpool_do_import(int argc
, char **argv
)
3714 char **searchdirs
= NULL
;
3715 char *env
, *envdup
= NULL
;
3719 nvlist_t
*pools
= NULL
;
3720 boolean_t do_all
= B_FALSE
;
3721 boolean_t do_destroyed
= B_FALSE
;
3722 char *mntopts
= NULL
;
3723 uint64_t searchguid
= 0;
3724 char *searchname
= NULL
;
3726 nvlist_t
*policy
= NULL
;
3727 nvlist_t
*props
= NULL
;
3728 int flags
= ZFS_IMPORT_NORMAL
;
3729 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
3730 boolean_t dryrun
= B_FALSE
;
3731 boolean_t do_rewind
= B_FALSE
;
3732 boolean_t xtreme_rewind
= B_FALSE
;
3733 boolean_t do_scan
= B_FALSE
;
3734 boolean_t pool_exists
= B_FALSE
;
3735 boolean_t pool_specified
= B_FALSE
;
3736 uint64_t txg
= -1ULL;
3737 char *cachefile
= NULL
;
3738 importargs_t idata
= { 0 };
3741 struct option long_options
[] = {
3742 {"rewind-to-checkpoint", no_argument
, NULL
, CHECKPOINT_OPT
},
3747 while ((c
= getopt_long(argc
, argv
, ":aCc:d:DEfFlmnNo:R:stT:VX",
3748 long_options
, NULL
)) != -1) {
3757 searchdirs
= safe_realloc(searchdirs
,
3758 (nsearch
+ 1) * sizeof (char *));
3759 searchdirs
[nsearch
++] = optarg
;
3762 do_destroyed
= B_TRUE
;
3765 flags
|= ZFS_IMPORT_ANY_HOST
;
3771 flags
|= ZFS_IMPORT_LOAD_KEYS
;
3774 flags
|= ZFS_IMPORT_MISSING_LOG
;
3780 flags
|= ZFS_IMPORT_ONLY
;
3783 if ((propval
= strchr(optarg
, '=')) != NULL
) {
3786 if (add_prop_list(optarg
, propval
,
3794 if (add_prop_list(zpool_prop_to_name(
3795 ZPOOL_PROP_ALTROOT
), optarg
, &props
, B_TRUE
))
3797 if (add_prop_list_default(zpool_prop_to_name(
3798 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3805 flags
|= ZFS_IMPORT_TEMP_NAME
;
3806 if (add_prop_list_default(zpool_prop_to_name(
3807 ZPOOL_PROP_CACHEFILE
), "none", &props
))
3813 txg
= strtoull(optarg
, &endptr
, 0);
3814 if (errno
!= 0 || *endptr
!= '\0') {
3815 (void) fprintf(stderr
,
3816 gettext("invalid txg value\n"));
3819 rewind_policy
= ZPOOL_DO_REWIND
| ZPOOL_EXTREME_REWIND
;
3822 flags
|= ZFS_IMPORT_VERBATIM
;
3825 xtreme_rewind
= B_TRUE
;
3827 case CHECKPOINT_OPT
:
3828 flags
|= ZFS_IMPORT_CHECKPOINT
;
3831 (void) fprintf(stderr
, gettext("missing argument for "
3832 "'%c' option\n"), optopt
);
3836 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
3845 if (cachefile
&& nsearch
!= 0) {
3846 (void) fprintf(stderr
, gettext("-c is incompatible with -d\n"));
3850 if (cachefile
&& do_scan
) {
3851 (void) fprintf(stderr
, gettext("-c is incompatible with -s\n"));
3855 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && (flags
& ZFS_IMPORT_ONLY
)) {
3856 (void) fprintf(stderr
, gettext("-l is incompatible with -N\n"));
3860 if ((flags
& ZFS_IMPORT_LOAD_KEYS
) && !do_all
&& argc
== 0) {
3861 (void) fprintf(stderr
, gettext("-l is only meaningful during "
3866 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
3867 (void) fprintf(stderr
,
3868 gettext("-n or -X only meaningful with -F\n"));
3872 rewind_policy
= ZPOOL_TRY_REWIND
;
3874 rewind_policy
= ZPOOL_DO_REWIND
;
3876 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
3878 /* In the future, we can capture further policy and include it here */
3879 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
3880 nvlist_add_uint64(policy
, ZPOOL_LOAD_REQUEST_TXG
, txg
) != 0 ||
3881 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
3882 rewind_policy
) != 0)
3885 /* check argument count */
3888 (void) fprintf(stderr
, gettext("too many arguments\n"));
3893 (void) fprintf(stderr
, gettext("too many arguments\n"));
3899 * Check for the effective uid. We do this explicitly here because
3900 * otherwise any attempt to discover pools will silently fail.
3902 if (argc
== 0 && geteuid() != 0) {
3903 (void) fprintf(stderr
, gettext("cannot "
3904 "discover pools: permission denied\n"));
3908 nvlist_free(policy
);
3913 * Depending on the arguments given, we do one of the following:
3915 * <none> Iterate through all pools and display information about
3918 * -a Iterate through all pools and try to import each one.
3920 * <id> Find the pool that corresponds to the given GUID/pool
3921 * name and import that one.
3923 * -D Above options applies only to destroyed pools.
3929 searchguid
= strtoull(argv
[0], &endptr
, 10);
3930 if (errno
!= 0 || *endptr
!= '\0') {
3931 searchname
= argv
[0];
3934 pool_specified
= B_TRUE
;
3937 * User specified a name or guid. Ensure it's unique.
3939 target_exists_args_t search
= {searchname
, searchguid
};
3940 pool_exists
= zpool_iter(g_zfs
, name_or_guid_exists
, &search
);
3944 * Check the environment for the preferred search path.
3946 if ((searchdirs
== NULL
) && (env
= getenv("ZPOOL_IMPORT_PATH"))) {
3947 char *dir
, *tmp
= NULL
;
3949 envdup
= strdup(env
);
3951 for (dir
= strtok_r(envdup
, ":", &tmp
);
3953 dir
= strtok_r(NULL
, ":", &tmp
)) {
3954 searchdirs
= safe_realloc(searchdirs
,
3955 (nsearch
+ 1) * sizeof (char *));
3956 searchdirs
[nsearch
++] = dir
;
3960 idata
.path
= searchdirs
;
3961 idata
.paths
= nsearch
;
3962 idata
.poolname
= searchname
;
3963 idata
.guid
= searchguid
;
3964 idata
.cachefile
= cachefile
;
3965 idata
.scan
= do_scan
;
3966 idata
.policy
= policy
;
3968 libpc_handle_t lpch
= {
3969 .lpc_lib_handle
= g_zfs
,
3970 .lpc_ops
= &libzfs_config_ops
,
3971 .lpc_printerr
= B_TRUE
3973 pools
= zpool_search_import(&lpch
, &idata
);
3975 if (pools
!= NULL
&& pool_exists
&&
3976 (argc
== 1 || strcmp(argv
[0], argv
[1]) == 0)) {
3977 (void) fprintf(stderr
, gettext("cannot import '%s': "
3978 "a pool with that name already exists\n"),
3980 (void) fprintf(stderr
, gettext("use the form '%s "
3981 "<pool | id> <newpool>' to give it a new name\n"),
3984 } else if (pools
== NULL
&& pool_exists
) {
3985 (void) fprintf(stderr
, gettext("cannot import '%s': "
3986 "a pool with that name is already created/imported,\n"),
3988 (void) fprintf(stderr
, gettext("and no additional pools "
3989 "with that name were found\n"));
3991 } else if (pools
== NULL
) {
3993 (void) fprintf(stderr
, gettext("cannot import '%s': "
3994 "no such pool available\n"), argv
[0]);
4002 nvlist_free(policy
);
4008 err
= import_pools(pools
, props
, mntopts
, flags
,
4009 argc
>= 1 ? argv
[0] : NULL
,
4010 argc
>= 2 ? argv
[1] : NULL
,
4011 do_destroyed
, pool_specified
, do_all
, &idata
);
4014 * If we're using the cachefile and we failed to import, then
4015 * fallback to scanning the directory for pools that match
4016 * those in the cachefile.
4018 if (err
!= 0 && cachefile
!= NULL
) {
4019 (void) printf(gettext("cachefile import failed, retrying\n"));
4022 * We use the scan flag to gather the directories that exist
4023 * in the cachefile. If we need to fallback to searching for
4024 * the pool config, we will only search devices in these
4027 idata
.scan
= B_TRUE
;
4029 pools
= zpool_search_import(&lpch
, &idata
);
4031 err
= import_pools(pools
, props
, mntopts
, flags
,
4032 argc
>= 1 ? argv
[0] : NULL
,
4033 argc
>= 2 ? argv
[1] : NULL
,
4034 do_destroyed
, pool_specified
, do_all
, &idata
);
4040 nvlist_free(policy
);
4044 return (err
? 1 : 0);
4048 * zpool sync [-f] [pool] ...
4050 * -f (undocumented) force uberblock (and config including zpool cache file)
4053 * Sync the specified pool(s).
4054 * Without arguments "zpool sync" will sync all pools.
4055 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
4059 zpool_do_sync(int argc
, char **argv
)
4062 boolean_t force
= B_FALSE
;
4065 while ((ret
= getopt(argc
, argv
, "f")) != -1) {
4071 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
4080 /* if argc == 0 we will execute zpool_sync_one on all pools */
4081 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
4082 B_FALSE
, zpool_sync_one
, &force
);
4087 typedef struct iostat_cbdata
{
4091 boolean_t cb_verbose
;
4092 boolean_t cb_literal
;
4093 boolean_t cb_scripted
;
4094 zpool_list_t
*cb_list
;
4095 vdev_cmd_data_list_t
*vcdl
;
4096 vdev_cbdata_t cb_vdevs
;
4100 typedef struct name_and_columns
{
4101 const char *name
; /* Column name */
4102 unsigned int columns
; /* Center name to this number of columns */
4103 } name_and_columns_t
;
4105 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
4107 static const name_and_columns_t iostat_top_labels
[][IOSTAT_MAX_LABELS
] =
4109 [IOS_DEFAULT
] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
4111 [IOS_LATENCY
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
4112 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
4114 [IOS_QUEUES
] = {{"syncq_read", 2}, {"syncq_write", 2},
4115 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
4116 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL
}},
4117 [IOS_L_HISTO
] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
4118 {"asyncq_wait", 2}, {NULL
}},
4119 [IOS_RQ_HISTO
] = {{"sync_read", 2}, {"sync_write", 2},
4120 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
4121 {"trim", 2}, {"rebuild", 2}, {NULL
}},
4124 /* Shorthand - if "columns" field not set, default to 1 column */
4125 static const name_and_columns_t iostat_bottom_labels
[][IOSTAT_MAX_LABELS
] =
4127 [IOS_DEFAULT
] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
4129 [IOS_LATENCY
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
4130 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
4132 [IOS_QUEUES
] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
4133 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
4134 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL
}},
4135 [IOS_L_HISTO
] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
4136 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
4138 [IOS_RQ_HISTO
] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
4139 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
4140 {"ind"}, {"agg"}, {NULL
}},
4143 static const char *histo_to_title
[] = {
4144 [IOS_L_HISTO
] = "latency",
4145 [IOS_RQ_HISTO
] = "req_size",
4149 * Return the number of labels in a null-terminated name_and_columns_t
4154 label_array_len(const name_and_columns_t
*labels
)
4158 while (labels
[i
].name
)
4165 * Return the number of strings in a null-terminated string array.
4168 * const char foo[] = {"bar", "baz", NULL}
4173 str_array_len(const char *array
[])
4184 * Return a default column width for default/latency/queue columns. This does
4185 * not include histograms, which have their columns autosized.
4188 default_column_width(iostat_cbdata_t
*cb
, enum iostat_type type
)
4190 unsigned long column_width
= 5; /* Normal niceprint */
4191 static unsigned long widths
[] = {
4193 * Choose some sane default column sizes for printing the
4196 [IOS_DEFAULT
] = 15, /* 1PB capacity */
4197 [IOS_LATENCY
] = 10, /* 1B ns = 10sec */
4198 [IOS_QUEUES
] = 6, /* 1M queue entries */
4199 [IOS_L_HISTO
] = 10, /* 1B ns = 10sec */
4200 [IOS_RQ_HISTO
] = 6, /* 1M queue entries */
4204 column_width
= widths
[type
];
4206 return (column_width
);
4210 * Print the column labels, i.e:
4212 * capacity operations bandwidth
4213 * alloc free read write read write ...
4215 * If force_column_width is set, use it for the column width. If not set, use
4216 * the default column width.
4219 print_iostat_labels(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4220 const name_and_columns_t labels
[][IOSTAT_MAX_LABELS
])
4223 int text_start
, rw_column_width
, spaces_to_end
;
4224 uint64_t flags
= cb
->cb_flags
;
4226 unsigned int column_width
= force_column_width
;
4228 /* For each bit set in flags */
4229 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4230 idx
= lowbit64(f
) - 1;
4231 if (!force_column_width
)
4232 column_width
= default_column_width(cb
, idx
);
4233 /* Print our top labels centered over "read write" label. */
4234 for (i
= 0; i
< label_array_len(labels
[idx
]); i
++) {
4235 const char *name
= labels
[idx
][i
].name
;
4237 * We treat labels[][].columns == 0 as shorthand
4238 * for one column. It makes writing out the label
4239 * tables more concise.
4241 unsigned int columns
= MAX(1, labels
[idx
][i
].columns
);
4242 unsigned int slen
= strlen(name
);
4244 rw_column_width
= (column_width
* columns
) +
4245 (2 * (columns
- 1));
4247 text_start
= (int)((rw_column_width
) / columns
-
4252 printf(" "); /* Two spaces between columns */
4254 /* Space from beginning of column to label */
4255 for (s
= 0; s
< text_start
; s
++)
4260 /* Print space after label to end of column */
4261 spaces_to_end
= rw_column_width
- text_start
- slen
;
4262 if (spaces_to_end
< 0)
4265 for (s
= 0; s
< spaces_to_end
; s
++)
4273 * print_cmd_columns - Print custom column titles from -c
4275 * If the user specified the "zpool status|iostat -c" then print their custom
4276 * column titles in the header. For example, print_cmd_columns() would print
4277 * the " col1 col2" part of this:
4279 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4281 * capacity operations bandwidth
4282 * pool alloc free read write read write col1 col2
4283 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4284 * mypool 269K 1008M 0 0 107 946
4285 * mirror 269K 1008M 0 0 107 946
4286 * sdb - - 0 0 102 473 val1 val2
4287 * sdc - - 0 0 5 473 val1 val2
4288 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4291 print_cmd_columns(vdev_cmd_data_list_t
*vcdl
, int use_dashes
)
4294 vdev_cmd_data_t
*data
= &vcdl
->data
[0];
4296 if (vcdl
->count
== 0 || data
== NULL
)
4300 * Each vdev cmd should have the same column names unless the user did
4301 * something weird with their cmd. Just take the column names from the
4302 * first vdev and assume it works for all of them.
4304 for (i
= 0; i
< vcdl
->uniq_cols_cnt
; i
++) {
4307 for (j
= 0; j
< vcdl
->uniq_cols_width
[i
]; j
++)
4310 printf_color(ANSI_BOLD
, "%*s", vcdl
->uniq_cols_width
[i
],
4311 vcdl
->uniq_cols
[i
]);
4318 * Utility function to print out a line of dashes like:
4320 * -------------------------------- ----- ----- ----- ----- -----
4322 * ...or a dashed named-row line like:
4328 * @force_column_width If non-zero, use the value as the column width.
4329 * Otherwise use the default column widths.
4331 * @name: Print a dashed named-row line starting
4332 * with @name. Otherwise, print a regular
4336 print_iostat_dashes(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4340 unsigned int namewidth
;
4341 uint64_t flags
= cb
->cb_flags
;
4344 const name_and_columns_t
*labels
;
4348 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4349 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4350 } else if (cb
->cb_vdevs
.cb_names_count
) {
4356 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4357 name
? strlen(name
) : 0);
4361 printf("%-*s", namewidth
, name
);
4363 for (i
= 0; i
< namewidth
; i
++)
4367 /* For each bit in flags */
4368 for (f
= flags
; f
; f
&= ~(1ULL << idx
)) {
4369 unsigned int column_width
;
4370 idx
= lowbit64(f
) - 1;
4371 if (force_column_width
)
4372 column_width
= force_column_width
;
4374 column_width
= default_column_width(cb
, idx
);
4376 labels
= iostat_bottom_labels
[idx
];
4377 for (i
= 0; i
< label_array_len(labels
); i
++) {
4379 printf(" %*s-", column_width
- 1, " ");
4381 printf(" %.*s", column_width
,
4382 "--------------------");
4389 print_iostat_separator_impl(iostat_cbdata_t
*cb
,
4390 unsigned int force_column_width
)
4392 print_iostat_dashes(cb
, force_column_width
, NULL
);
4396 print_iostat_separator(iostat_cbdata_t
*cb
)
4398 print_iostat_separator_impl(cb
, 0);
4402 print_iostat_header_impl(iostat_cbdata_t
*cb
, unsigned int force_column_width
,
4403 const char *histo_vdev_name
)
4405 unsigned int namewidth
;
4408 color_start(ANSI_BOLD
);
4410 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
4411 title
= histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)];
4412 } else if (cb
->cb_vdevs
.cb_names_count
) {
4418 namewidth
= MAX(MAX(strlen(title
), cb
->cb_namewidth
),
4419 histo_vdev_name
? strlen(histo_vdev_name
) : 0);
4421 if (histo_vdev_name
)
4422 printf("%-*s", namewidth
, histo_vdev_name
);
4424 printf("%*s", namewidth
, "");
4427 print_iostat_labels(cb
, force_column_width
, iostat_top_labels
);
4430 printf("%-*s", namewidth
, title
);
4432 print_iostat_labels(cb
, force_column_width
, iostat_bottom_labels
);
4433 if (cb
->vcdl
!= NULL
)
4434 print_cmd_columns(cb
->vcdl
, 0);
4438 print_iostat_separator_impl(cb
, force_column_width
);
4440 if (cb
->vcdl
!= NULL
)
4441 print_cmd_columns(cb
->vcdl
, 1);
4449 print_iostat_header(iostat_cbdata_t
*cb
)
4451 print_iostat_header_impl(cb
, 0, NULL
);
4455 * Prints a size string (i.e. 120M) with the suffix ("M") colored
4456 * by order of magnitude. Uses column_size to add padding.
4459 print_stat_color(const char *statbuf
, unsigned int column_size
)
4462 size_t len
= strlen(statbuf
);
4463 while (len
< column_size
) {
4467 if (*statbuf
== '0') {
4468 color_start(ANSI_GRAY
);
4471 for (; *statbuf
; statbuf
++) {
4472 if (*statbuf
== 'K') color_start(ANSI_GREEN
);
4473 else if (*statbuf
== 'M') color_start(ANSI_YELLOW
);
4474 else if (*statbuf
== 'G') color_start(ANSI_RED
);
4475 else if (*statbuf
== 'T') color_start(ANSI_BOLD_BLUE
);
4476 else if (*statbuf
== 'P') color_start(ANSI_MAGENTA
);
4477 else if (*statbuf
== 'E') color_start(ANSI_CYAN
);
4478 fputc(*statbuf
, stdout
);
4479 if (--column_size
<= 0)
4487 * Display a single statistic.
4490 print_one_stat(uint64_t value
, enum zfs_nicenum_format format
,
4491 unsigned int column_size
, boolean_t scripted
)
4495 zfs_nicenum_format(value
, buf
, sizeof (buf
), format
);
4498 printf("\t%s", buf
);
4500 print_stat_color(buf
, column_size
);
4504 * Calculate the default vdev stats
4506 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4507 * stats into calcvs.
4510 calc_default_iostats(vdev_stat_t
*oldvs
, vdev_stat_t
*newvs
,
4511 vdev_stat_t
*calcvs
)
4515 memcpy(calcvs
, newvs
, sizeof (*calcvs
));
4516 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_ops
); i
++)
4517 calcvs
->vs_ops
[i
] = (newvs
->vs_ops
[i
] - oldvs
->vs_ops
[i
]);
4519 for (i
= 0; i
< ARRAY_SIZE(calcvs
->vs_bytes
); i
++)
4520 calcvs
->vs_bytes
[i
] = (newvs
->vs_bytes
[i
] - oldvs
->vs_bytes
[i
]);
4524 * Internal representation of the extended iostats data.
4526 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4527 * or single uint64_t's. We make both look like arrays to make them easier
4528 * to process. In order to make single uint64_t's look like arrays, we set
4529 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4530 * we can just use *data and count.
4534 uint_t count
; /* Number of entries in data[] */
4535 uint64_t __data
; /* Only used when data is a single uint64_t */
4539 stat_histo_max(struct stat_array
*nva
, unsigned int len
)
4543 for (i
= 0; i
< len
; i
++)
4544 max
= MAX(max
, array64_max(nva
[i
].data
, nva
[i
].count
));
4550 * Helper function to lookup a uint64_t array or uint64_t value and store its
4551 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4552 * it look like a one element array to make it easier to process.
4555 nvpair64_to_stat_array(nvlist_t
*nvl
, const char *name
,
4556 struct stat_array
*nva
)
4561 verify(nvlist_lookup_nvpair(nvl
, name
, &tmp
) == 0);
4562 switch (nvpair_type(tmp
)) {
4563 case DATA_TYPE_UINT64_ARRAY
:
4564 ret
= nvpair_value_uint64_array(tmp
, &nva
->data
, &nva
->count
);
4566 case DATA_TYPE_UINT64
:
4567 ret
= nvpair_value_uint64(tmp
, &nva
->__data
);
4568 nva
->data
= &nva
->__data
;
4572 /* Not a uint64_t */
4581 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4582 * subtract them, and return the results in a newly allocated stat_array.
4583 * You must free the returned array after you are done with it with
4584 * free_calc_stats().
4586 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4589 static struct stat_array
*
4590 calc_and_alloc_stats_ex(const char **names
, unsigned int len
, nvlist_t
*oldnv
,
4593 nvlist_t
*oldnvx
= NULL
, *newnvx
;
4594 struct stat_array
*oldnva
, *newnva
, *calcnva
;
4596 unsigned int alloc_size
= (sizeof (struct stat_array
)) * len
;
4598 /* Extract our extended stats nvlist from the main list */
4599 verify(nvlist_lookup_nvlist(newnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4602 verify(nvlist_lookup_nvlist(oldnv
, ZPOOL_CONFIG_VDEV_STATS_EX
,
4606 newnva
= safe_malloc(alloc_size
);
4607 oldnva
= safe_malloc(alloc_size
);
4608 calcnva
= safe_malloc(alloc_size
);
4610 for (j
= 0; j
< len
; j
++) {
4611 verify(nvpair64_to_stat_array(newnvx
, names
[j
],
4613 calcnva
[j
].count
= newnva
[j
].count
;
4614 alloc_size
= calcnva
[j
].count
* sizeof (calcnva
[j
].data
[0]);
4615 calcnva
[j
].data
= safe_malloc(alloc_size
);
4616 memcpy(calcnva
[j
].data
, newnva
[j
].data
, alloc_size
);
4619 verify(nvpair64_to_stat_array(oldnvx
, names
[j
],
4621 for (i
= 0; i
< oldnva
[j
].count
; i
++)
4622 calcnva
[j
].data
[i
] -= oldnva
[j
].data
[i
];
4631 free_calc_stats(struct stat_array
*nva
, unsigned int len
)
4634 for (i
= 0; i
< len
; i
++)
4641 print_iostat_histo(struct stat_array
*nva
, unsigned int len
,
4642 iostat_cbdata_t
*cb
, unsigned int column_width
, unsigned int namewidth
,
4648 enum zfs_nicenum_format format
;
4649 unsigned int buckets
;
4650 unsigned int start_bucket
;
4653 format
= ZFS_NICENUM_RAW
;
4655 format
= ZFS_NICENUM_1024
;
4657 /* All these histos are the same size, so just use nva[0].count */
4658 buckets
= nva
[0].count
;
4660 if (cb
->cb_flags
& IOS_RQ_HISTO_M
) {
4661 /* Start at 512 - req size should never be lower than this */
4667 for (j
= start_bucket
; j
< buckets
; j
++) {
4668 /* Print histogram bucket label */
4669 if (cb
->cb_flags
& IOS_L_HISTO_M
) {
4670 /* Ending range of this bucket */
4671 val
= (1UL << (j
+ 1)) - 1;
4672 zfs_nicetime(val
, buf
, sizeof (buf
));
4674 /* Request size (starting range of bucket) */
4676 zfs_nicenum(val
, buf
, sizeof (buf
));
4679 if (cb
->cb_scripted
)
4680 printf("%llu", (u_longlong_t
)val
);
4682 printf("%-*s", namewidth
, buf
);
4684 /* Print the values on the line */
4685 for (i
= 0; i
< len
; i
++) {
4686 print_one_stat(nva
[i
].data
[j
] * scale
, format
,
4687 column_width
, cb
->cb_scripted
);
4694 print_solid_separator(unsigned int length
)
4702 print_iostat_histos(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4703 nvlist_t
*newnv
, double scale
, const char *name
)
4705 unsigned int column_width
;
4706 unsigned int namewidth
;
4707 unsigned int entire_width
;
4708 enum iostat_type type
;
4709 struct stat_array
*nva
;
4711 unsigned int names_len
;
4713 /* What type of histo are we? */
4714 type
= IOS_HISTO_IDX(cb
->cb_flags
);
4716 /* Get NULL-terminated array of nvlist names for our histo */
4717 names
= vsx_type_to_nvlist
[type
];
4718 names_len
= str_array_len(names
); /* num of names */
4720 nva
= calc_and_alloc_stats_ex(names
, names_len
, oldnv
, newnv
);
4722 if (cb
->cb_literal
) {
4723 column_width
= MAX(5,
4724 (unsigned int) log10(stat_histo_max(nva
, names_len
)) + 1);
4729 namewidth
= MAX(cb
->cb_namewidth
,
4730 strlen(histo_to_title
[IOS_HISTO_IDX(cb
->cb_flags
)]));
4733 * Calculate the entire line width of what we're printing. The
4734 * +2 is for the two spaces between columns:
4738 /* |___| <---------- column_width */
4740 /* |__________| <--- entire_width */
4742 entire_width
= namewidth
+ (column_width
+ 2) *
4743 label_array_len(iostat_bottom_labels
[type
]);
4745 if (cb
->cb_scripted
)
4746 printf("%s\n", name
);
4748 print_iostat_header_impl(cb
, column_width
, name
);
4750 print_iostat_histo(nva
, names_len
, cb
, column_width
,
4753 free_calc_stats(nva
, names_len
);
4754 if (!cb
->cb_scripted
)
4755 print_solid_separator(entire_width
);
4759 * Calculate the average latency of a power-of-two latency histogram
4762 single_histo_average(uint64_t *histo
, unsigned int buckets
)
4765 uint64_t count
= 0, total
= 0;
4767 for (i
= 0; i
< buckets
; i
++) {
4769 * Our buckets are power-of-two latency ranges. Use the
4770 * midpoint latency of each bucket to calculate the average.
4778 if (histo
[i
] != 0) {
4779 total
+= histo
[i
] * (((1UL << i
) + ((1UL << i
)/2)));
4784 /* Prevent divide by zero */
4785 return (count
== 0 ? 0 : total
/ count
);
4789 print_iostat_queues(iostat_cbdata_t
*cb
, nvlist_t
*newnv
)
4791 const char *names
[] = {
4792 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE
,
4793 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE
,
4794 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE
,
4795 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE
,
4796 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE
,
4797 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE
,
4798 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE
,
4799 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE
,
4800 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE
,
4801 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE
,
4802 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE
,
4803 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE
,
4804 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE
,
4805 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE
,
4808 struct stat_array
*nva
;
4810 unsigned int column_width
= default_column_width(cb
, IOS_QUEUES
);
4811 enum zfs_nicenum_format format
;
4813 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), NULL
, newnv
);
4816 format
= ZFS_NICENUM_RAW
;
4818 format
= ZFS_NICENUM_1024
;
4820 for (int i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4821 uint64_t val
= nva
[i
].data
[0];
4822 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4825 free_calc_stats(nva
, ARRAY_SIZE(names
));
4829 print_iostat_latency(iostat_cbdata_t
*cb
, nvlist_t
*oldnv
,
4834 const char *names
[] = {
4835 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO
,
4836 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO
,
4837 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO
,
4838 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO
,
4839 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO
,
4840 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO
,
4841 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO
,
4842 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO
,
4843 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO
,
4844 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO
,
4845 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO
,
4847 struct stat_array
*nva
;
4849 unsigned int column_width
= default_column_width(cb
, IOS_LATENCY
);
4850 enum zfs_nicenum_format format
;
4852 nva
= calc_and_alloc_stats_ex(names
, ARRAY_SIZE(names
), oldnv
, newnv
);
4855 format
= ZFS_NICENUM_RAWTIME
;
4857 format
= ZFS_NICENUM_TIME
;
4859 /* Print our avg latencies on the line */
4860 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
4861 /* Compute average latency for a latency histo */
4862 val
= single_histo_average(nva
[i
].data
, nva
[i
].count
);
4863 print_one_stat(val
, format
, column_width
, cb
->cb_scripted
);
4865 free_calc_stats(nva
, ARRAY_SIZE(names
));
4869 * Print default statistics (capacity/operations/bandwidth)
4872 print_iostat_default(vdev_stat_t
*vs
, iostat_cbdata_t
*cb
, double scale
)
4874 unsigned int column_width
= default_column_width(cb
, IOS_DEFAULT
);
4875 enum zfs_nicenum_format format
;
4876 char na
; /* char to print for "not applicable" values */
4878 if (cb
->cb_literal
) {
4879 format
= ZFS_NICENUM_RAW
;
4882 format
= ZFS_NICENUM_1024
;
4886 /* only toplevel vdevs have capacity stats */
4887 if (vs
->vs_space
== 0) {
4888 if (cb
->cb_scripted
)
4889 printf("\t%c\t%c", na
, na
);
4891 printf(" %*c %*c", column_width
, na
, column_width
,
4894 print_one_stat(vs
->vs_alloc
, format
, column_width
,
4896 print_one_stat(vs
->vs_space
- vs
->vs_alloc
, format
,
4897 column_width
, cb
->cb_scripted
);
4900 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_READ
] * scale
),
4901 format
, column_width
, cb
->cb_scripted
);
4902 print_one_stat((uint64_t)(vs
->vs_ops
[ZIO_TYPE_WRITE
] * scale
),
4903 format
, column_width
, cb
->cb_scripted
);
4904 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_READ
] * scale
),
4905 format
, column_width
, cb
->cb_scripted
);
4906 print_one_stat((uint64_t)(vs
->vs_bytes
[ZIO_TYPE_WRITE
] * scale
),
4907 format
, column_width
, cb
->cb_scripted
);
4910 static const char *const class_name
[] = {
4911 VDEV_ALLOC_BIAS_DEDUP
,
4912 VDEV_ALLOC_BIAS_SPECIAL
,
4913 VDEV_ALLOC_CLASS_LOGS
4917 * Print out all the statistics for the given vdev. This can either be the
4918 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4919 * is a verbose output, and we don't want to display the toplevel pool stats.
4921 * Returns the number of stat lines printed.
4924 print_vdev_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*oldnv
,
4925 nvlist_t
*newnv
, iostat_cbdata_t
*cb
, int depth
)
4927 nvlist_t
**oldchild
, **newchild
;
4928 uint_t c
, children
, oldchildren
;
4929 vdev_stat_t
*oldvs
, *newvs
, *calcvs
;
4930 vdev_stat_t zerovs
= { 0 };
4937 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
4940 calcvs
= safe_malloc(sizeof (*calcvs
));
4942 if (oldnv
!= NULL
) {
4943 verify(nvlist_lookup_uint64_array(oldnv
,
4944 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&oldvs
, &c
) == 0);
4949 /* Do we only want to see a specific vdev? */
4950 for (i
= 0; i
< cb
->cb_vdevs
.cb_names_count
; i
++) {
4951 /* Yes we do. Is this the vdev? */
4952 if (strcmp(name
, cb
->cb_vdevs
.cb_names
[i
]) == 0) {
4954 * This is our vdev. Since it is the only vdev we
4955 * will be displaying, make depth = 0 so that it
4956 * doesn't get indented.
4963 if (cb
->cb_vdevs
.cb_names_count
&& (i
== cb
->cb_vdevs
.cb_names_count
)) {
4964 /* Couldn't match the name */
4969 verify(nvlist_lookup_uint64_array(newnv
, ZPOOL_CONFIG_VDEV_STATS
,
4970 (uint64_t **)&newvs
, &c
) == 0);
4973 * Print the vdev name unless it's is a histogram. Histograms
4974 * display the vdev name in the header itself.
4976 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4977 if (cb
->cb_scripted
) {
4980 if (strlen(name
) + depth
> cb
->cb_namewidth
)
4981 (void) printf("%*s%s", depth
, "", name
);
4983 (void) printf("%*s%s%*s", depth
, "", name
,
4984 (int)(cb
->cb_namewidth
- strlen(name
) -
4989 /* Calculate our scaling factor */
4990 tdelta
= newvs
->vs_timestamp
- oldvs
->vs_timestamp
;
4991 if ((oldvs
->vs_timestamp
== 0) && (cb
->cb_flags
& IOS_ANYHISTO_M
)) {
4993 * If we specify printing histograms with no time interval, then
4994 * print the histogram numbers over the entire lifetime of the
5002 scale
= (double)NANOSEC
/ tdelta
;
5005 if (cb
->cb_flags
& IOS_DEFAULT_M
) {
5006 calc_default_iostats(oldvs
, newvs
, calcvs
);
5007 print_iostat_default(calcvs
, cb
, scale
);
5009 if (cb
->cb_flags
& IOS_LATENCY_M
)
5010 print_iostat_latency(cb
, oldnv
, newnv
);
5011 if (cb
->cb_flags
& IOS_QUEUES_M
)
5012 print_iostat_queues(cb
, newnv
);
5013 if (cb
->cb_flags
& IOS_ANYHISTO_M
) {
5015 print_iostat_histos(cb
, oldnv
, newnv
, scale
, name
);
5018 if (cb
->vcdl
!= NULL
) {
5020 if (nvlist_lookup_string(newnv
, ZPOOL_CONFIG_PATH
,
5023 zpool_print_cmd(cb
->vcdl
, zpool_get_name(zhp
), path
);
5027 if (!(cb
->cb_flags
& IOS_ANYHISTO_M
))
5036 if (!cb
->cb_verbose
)
5039 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_CHILDREN
,
5040 &newchild
, &children
) != 0)
5044 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_CHILDREN
,
5045 &oldchild
, &oldchildren
) != 0)
5048 children
= MIN(oldchildren
, children
);
5052 * print normal top-level devices
5054 for (c
= 0; c
< children
; c
++) {
5055 uint64_t ishole
= B_FALSE
, islog
= B_FALSE
;
5057 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_HOLE
,
5060 (void) nvlist_lookup_uint64(newchild
[c
], ZPOOL_CONFIG_IS_LOG
,
5063 if (ishole
|| islog
)
5066 if (nvlist_exists(newchild
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
5069 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
5070 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
5071 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
] : NULL
,
5072 newchild
[c
], cb
, depth
+ 2);
5077 * print all other top-level devices
5079 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
5080 boolean_t printed
= B_FALSE
;
5082 for (c
= 0; c
< children
; c
++) {
5083 uint64_t islog
= B_FALSE
;
5084 const char *bias
= NULL
;
5085 const char *type
= NULL
;
5087 (void) nvlist_lookup_uint64(newchild
[c
],
5088 ZPOOL_CONFIG_IS_LOG
, &islog
);
5090 bias
= VDEV_ALLOC_CLASS_LOGS
;
5092 (void) nvlist_lookup_string(newchild
[c
],
5093 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
5094 (void) nvlist_lookup_string(newchild
[c
],
5095 ZPOOL_CONFIG_TYPE
, &type
);
5097 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
5099 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
5103 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) &&
5105 !cb
->cb_vdevs
.cb_names
) {
5106 print_iostat_dashes(cb
, 0,
5113 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
5114 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
);
5115 ret
+= print_vdev_stats(zhp
, vname
, oldnv
?
5116 oldchild
[c
] : NULL
, newchild
[c
], cb
, depth
+ 2);
5122 * Include level 2 ARC devices in iostat output
5124 if (nvlist_lookup_nvlist_array(newnv
, ZPOOL_CONFIG_L2CACHE
,
5125 &newchild
, &children
) != 0)
5129 if (nvlist_lookup_nvlist_array(oldnv
, ZPOOL_CONFIG_L2CACHE
,
5130 &oldchild
, &oldchildren
) != 0)
5133 children
= MIN(oldchildren
, children
);
5137 if ((!(cb
->cb_flags
& IOS_ANYHISTO_M
)) && !cb
->cb_scripted
&&
5138 !cb
->cb_vdevs
.cb_names
) {
5139 print_iostat_dashes(cb
, 0, "cache");
5143 for (c
= 0; c
< children
; c
++) {
5144 vname
= zpool_vdev_name(g_zfs
, zhp
, newchild
[c
],
5145 cb
->cb_vdevs
.cb_name_flags
);
5146 ret
+= print_vdev_stats(zhp
, vname
, oldnv
? oldchild
[c
]
5147 : NULL
, newchild
[c
], cb
, depth
+ 2);
5156 refresh_iostat(zpool_handle_t
*zhp
, void *data
)
5158 iostat_cbdata_t
*cb
= data
;
5162 * If the pool has disappeared, remove it from the list and continue.
5164 if (zpool_refresh_stats(zhp
, &missing
) != 0)
5168 pool_list_remove(cb
->cb_list
, zhp
);
5174 * Callback to print out the iostats for the given pool.
5177 print_iostat(zpool_handle_t
*zhp
, void *data
)
5179 iostat_cbdata_t
*cb
= data
;
5180 nvlist_t
*oldconfig
, *newconfig
;
5181 nvlist_t
*oldnvroot
, *newnvroot
;
5184 newconfig
= zpool_get_config(zhp
, &oldconfig
);
5186 if (cb
->cb_iteration
== 1)
5189 verify(nvlist_lookup_nvlist(newconfig
, ZPOOL_CONFIG_VDEV_TREE
,
5192 if (oldconfig
== NULL
)
5195 verify(nvlist_lookup_nvlist(oldconfig
, ZPOOL_CONFIG_VDEV_TREE
,
5198 ret
= print_vdev_stats(zhp
, zpool_get_name(zhp
), oldnvroot
, newnvroot
,
5200 if ((ret
!= 0) && !(cb
->cb_flags
& IOS_ANYHISTO_M
) &&
5201 !cb
->cb_scripted
&& cb
->cb_verbose
&&
5202 !cb
->cb_vdevs
.cb_names_count
) {
5203 print_iostat_separator(cb
);
5204 if (cb
->vcdl
!= NULL
) {
5205 print_cmd_columns(cb
->vcdl
, 1);
5220 if (isatty(STDOUT_FILENO
)) {
5221 error
= ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
);
5223 columns
= ws
.ws_col
;
5232 * Return the required length of the pool/vdev name column. The minimum
5233 * allowed width and output formatting flags must be provided.
5236 get_namewidth(zpool_handle_t
*zhp
, int min_width
, int flags
, boolean_t verbose
)
5238 nvlist_t
*config
, *nvroot
;
5239 int width
= min_width
;
5241 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
5242 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5244 size_t poolname_len
= strlen(zpool_get_name(zhp
));
5245 if (verbose
== B_FALSE
) {
5246 width
= MAX(poolname_len
, min_width
);
5248 width
= MAX(poolname_len
,
5249 max_width(zhp
, nvroot
, 0, min_width
, flags
));
5257 * Parse the input string, get the 'interval' and 'count' value if there is one.
5260 get_interval_count(int *argcp
, char **argv
, float *iv
,
5264 unsigned long count
= 0;
5268 * Determine if the last argument is an integer or a pool name
5270 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5274 interval
= strtof(argv
[argc
- 1], &end
);
5276 if (*end
== '\0' && errno
== 0) {
5277 if (interval
== 0) {
5278 (void) fprintf(stderr
, gettext(
5279 "interval cannot be zero\n"));
5283 * Ignore the last parameter
5288 * If this is not a valid number, just plow on. The
5289 * user will get a more informative error message later
5297 * If the last argument is also an integer, then we have both a count
5300 if (argc
> 0 && zfs_isnumber(argv
[argc
- 1])) {
5305 interval
= strtof(argv
[argc
- 1], &end
);
5307 if (*end
== '\0' && errno
== 0) {
5308 if (interval
== 0) {
5309 (void) fprintf(stderr
, gettext(
5310 "interval cannot be zero\n"));
5315 * Ignore the last parameter
5329 get_timestamp_arg(char c
)
5332 timestamp_fmt
= UDATE
;
5334 timestamp_fmt
= DDATE
;
5340 * Return stat flags that are supported by all pools by both the module and
5341 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5342 * It will get ANDed down until only the flags that are supported on all pools
5346 get_stat_flags_cb(zpool_handle_t
*zhp
, void *data
)
5348 uint64_t *mask
= data
;
5349 nvlist_t
*config
, *nvroot
, *nvx
;
5353 config
= zpool_get_config(zhp
, NULL
);
5354 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
5357 /* Default stats are always supported, but for completeness.. */
5358 if (nvlist_exists(nvroot
, ZPOOL_CONFIG_VDEV_STATS
))
5359 flags
|= IOS_DEFAULT_M
;
5361 /* Get our extended stats nvlist from the main list */
5362 if (nvlist_lookup_nvlist(nvroot
, ZPOOL_CONFIG_VDEV_STATS_EX
,
5365 * No extended stats; they're probably running an older
5366 * module. No big deal, we support that too.
5371 /* For each extended stat, make sure all its nvpairs are supported */
5372 for (j
= 0; j
< ARRAY_SIZE(vsx_type_to_nvlist
); j
++) {
5373 if (!vsx_type_to_nvlist
[j
][0])
5376 /* Start off by assuming the flag is supported, then check */
5377 flags
|= (1ULL << j
);
5378 for (i
= 0; vsx_type_to_nvlist
[j
][i
]; i
++) {
5379 if (!nvlist_exists(nvx
, vsx_type_to_nvlist
[j
][i
])) {
5380 /* flag isn't supported */
5381 flags
= flags
& ~(1ULL << j
);
5387 *mask
= *mask
& flags
;
5392 * Return a bitmask of stats that are supported on all pools by both the module
5396 get_stat_flags(zpool_list_t
*list
)
5401 * get_stat_flags_cb() will lop off bits from "mask" until only the
5402 * flags that are supported on all pools remain.
5404 pool_list_iter(list
, B_FALSE
, get_stat_flags_cb
, &mask
);
5409 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5412 is_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *cb_data
)
5415 vdev_cbdata_t
*cb
= cb_data
;
5416 zpool_handle_t
*zhp
= zhp_data
;
5418 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
5421 return (guid
== zpool_vdev_path_to_guid(zhp
, cb
->cb_names
[0]));
5425 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5428 is_vdev(zpool_handle_t
*zhp
, void *cb_data
)
5430 return (for_each_vdev(zhp
, is_vdev_cb
, cb_data
));
5434 * Check if vdevs are in a pool
5436 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5437 * return 0. If pool_name is NULL, then search all pools.
5440 are_vdevs_in_pool(int argc
, char **argv
, char *pool_name
,
5448 if ((argc
== 0) || !*argv
)
5454 /* Temporarily hijack cb_names for a second... */
5455 tmp_name
= cb
->cb_names
;
5457 /* Go though our list of prospective vdev names */
5458 for (i
= 0; i
< argc
; i
++) {
5459 cb
->cb_names
= argv
+ i
;
5461 /* Is this name a vdev in our pools? */
5462 ret
= for_each_pool(pool_count
, &pool_name
, B_TRUE
, NULL
,
5463 ZFS_TYPE_POOL
, B_FALSE
, is_vdev
, cb
);
5470 cb
->cb_names
= tmp_name
;
5476 is_pool_cb(zpool_handle_t
*zhp
, void *data
)
5479 if (strcmp(name
, zpool_get_name(zhp
)) == 0)
5486 * Do we have a pool named *name? If so, return 1, otherwise 0.
5491 return (for_each_pool(0, NULL
, B_TRUE
, NULL
, ZFS_TYPE_POOL
, B_FALSE
,
5495 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5497 are_all_pools(int argc
, char **argv
)
5499 if ((argc
== 0) || !*argv
)
5503 if (!is_pool(argv
[argc
]))
5510 * Helper function to print out vdev/pool names we can't resolve. Used for an
5514 error_list_unresolved_vdevs(int argc
, char **argv
, char *pool_name
,
5520 for (i
= 0; i
< argc
; i
++) {
5524 str
= gettext("pool");
5525 else if (are_vdevs_in_pool(1, &name
, pool_name
, cb
))
5526 str
= gettext("vdev in this pool");
5527 else if (are_vdevs_in_pool(1, &name
, NULL
, cb
))
5528 str
= gettext("vdev in another pool");
5530 str
= gettext("unknown");
5532 fprintf(stderr
, "\t%s (%s)\n", name
, str
);
5537 * Same as get_interval_count(), but with additional checks to not misinterpret
5538 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5539 * cb.cb_vdevs.cb_name_flags.
5542 get_interval_count_filter_guids(int *argc
, char **argv
, float *interval
,
5543 unsigned long *count
, iostat_cbdata_t
*cb
)
5545 char **tmpargv
= argv
;
5546 int argc_for_interval
= 0;
5548 /* Is the last arg an interval value? Or a guid? */
5549 if (*argc
>= 1 && !are_vdevs_in_pool(1, &argv
[*argc
- 1], NULL
,
5552 * The last arg is not a guid, so it's probably an
5555 argc_for_interval
++;
5558 !are_vdevs_in_pool(1, &argv
[*argc
- 2], NULL
,
5561 * The 2nd to last arg is not a guid, so it's probably
5562 * an interval value.
5564 argc_for_interval
++;
5568 /* Point to our list of possible intervals */
5569 tmpargv
= &argv
[*argc
- argc_for_interval
];
5571 *argc
= *argc
- argc_for_interval
;
5572 get_interval_count(&argc_for_interval
, tmpargv
,
5577 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5578 * if we were unable to determine its size.
5581 terminal_height(void)
5585 if (isatty(STDOUT_FILENO
) == 0)
5588 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 && win
.ws_row
> 0)
5589 return (win
.ws_row
);
5595 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5598 * name: Short name of the script ('iostat').
5599 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5602 print_zpool_script_help(char *name
, char *path
)
5604 char *argv
[] = {path
, (char *)"-h", NULL
};
5605 char **lines
= NULL
;
5609 rc
= libzfs_run_process_get_stdout_nopath(path
, argv
, NULL
, &lines
,
5611 if (rc
!= 0 || lines
== NULL
|| lines_cnt
<= 0) {
5613 libzfs_free_str_array(lines
, lines_cnt
);
5617 for (int i
= 0; i
< lines_cnt
; i
++)
5618 if (!is_blank_str(lines
[i
]))
5619 printf(" %-14s %s\n", name
, lines
[i
]);
5621 libzfs_free_str_array(lines
, lines_cnt
);
5625 * Go though the zpool status/iostat -c scripts in the user's path, run their
5626 * help option (-h), and print out the results.
5629 print_zpool_dir_scripts(char *dirpath
)
5633 char fullpath
[MAXPATHLEN
];
5634 struct stat dir_stat
;
5636 if ((dir
= opendir(dirpath
)) != NULL
) {
5637 /* print all the files and directories within directory */
5638 while ((ent
= readdir(dir
)) != NULL
) {
5639 if (snprintf(fullpath
, sizeof (fullpath
), "%s/%s",
5640 dirpath
, ent
->d_name
) >= sizeof (fullpath
)) {
5641 (void) fprintf(stderr
,
5642 gettext("internal error: "
5643 "ZPOOL_SCRIPTS_PATH too large.\n"));
5647 /* Print the scripts */
5648 if (stat(fullpath
, &dir_stat
) == 0)
5649 if (dir_stat
.st_mode
& S_IXUSR
&&
5650 S_ISREG(dir_stat
.st_mode
))
5651 print_zpool_script_help(ent
->d_name
,
5659 * Print out help text for all zpool status/iostat -c scripts.
5662 print_zpool_script_list(const char *subcommand
)
5664 char *dir
, *sp
, *tmp
;
5666 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand
);
5668 sp
= zpool_get_cmd_search_path();
5672 for (dir
= strtok_r(sp
, ":", &tmp
);
5674 dir
= strtok_r(NULL
, ":", &tmp
))
5675 print_zpool_dir_scripts(dir
);
5681 * Set the minimum pool/vdev name column width. The width must be at least 10,
5682 * but may be as large as the column width - 42 so it still fits on one line.
5683 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5686 get_namewidth_iostat(zpool_handle_t
*zhp
, void *data
)
5688 iostat_cbdata_t
*cb
= data
;
5689 int width
, available_width
;
5692 * get_namewidth() returns the maximum width of any name in that column
5693 * for any pool/vdev/device line that will be output.
5695 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
5696 cb
->cb_vdevs
.cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
5699 * The width we are calculating is the width of the header and also the
5700 * padding width for names that are less than maximum width. The stats
5701 * take up 42 characters, so the width available for names is:
5703 available_width
= get_columns() - 42;
5706 * If the maximum width fits on a screen, then great! Make everything
5707 * line up by justifying all lines to the same width. If that max
5708 * width is larger than what's available, the name plus stats won't fit
5709 * on one line, and justifying to that width would cause every line to
5710 * wrap on the screen. We only want lines with long names to wrap.
5711 * Limit the padding to what won't wrap.
5713 if (width
> available_width
)
5714 width
= available_width
;
5717 * And regardless of whatever the screen width is (get_columns can
5718 * return 0 if the width is not known or less than 42 for a narrow
5719 * terminal) have the width be a minimum of 10.
5724 /* Save the calculated width */
5725 cb
->cb_namewidth
= width
;
5731 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5732 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5733 * [interval [count]]
5735 * -c CMD For each vdev, run command CMD
5736 * -g Display guid for individual vdev name.
5737 * -L Follow links when resolving vdev path name.
5738 * -P Display full path for vdev name.
5739 * -v Display statistics for individual vdevs
5741 * -p Display values in parsable (exact) format.
5742 * -H Scripted mode. Don't display headers, and separate properties
5744 * -l Display average latency
5745 * -q Display queue depths
5746 * -w Display latency histograms
5747 * -r Display request size histogram
5748 * -T Display a timestamp in date(1) or Unix format
5749 * -n Only print headers once
5751 * This command can be tricky because we want to be able to deal with pool
5752 * creation/destruction as well as vdev configuration changes. The bulk of this
5753 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5754 * on pool_list_update() to detect the addition of new pools. Configuration
5755 * changes are all handled within libzfs.
5758 zpool_do_iostat(int argc
, char **argv
)
5764 unsigned long count
= 0;
5767 boolean_t verbose
= B_FALSE
;
5768 boolean_t latency
= B_FALSE
, l_histo
= B_FALSE
, rq_histo
= B_FALSE
;
5769 boolean_t queues
= B_FALSE
, parsable
= B_FALSE
, scripted
= B_FALSE
;
5770 boolean_t omit_since_boot
= B_FALSE
;
5771 boolean_t guid
= B_FALSE
;
5772 boolean_t follow_links
= B_FALSE
;
5773 boolean_t full_name
= B_FALSE
;
5774 boolean_t headers_once
= B_FALSE
;
5775 iostat_cbdata_t cb
= { 0 };
5778 /* Used for printing error message */
5779 const char flag_to_arg
[] = {[IOS_LATENCY
] = 'l', [IOS_QUEUES
] = 'q',
5780 [IOS_L_HISTO
] = 'w', [IOS_RQ_HISTO
] = 'r'};
5782 uint64_t unsupported_flags
;
5785 while ((c
= getopt(argc
, argv
, "c:gLPT:vyhplqrwnH")) != -1) {
5790 gettext("Can't set -c flag twice\n"));
5794 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
5795 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5796 fprintf(stderr
, gettext(
5797 "Can't run -c, disabled by "
5798 "ZPOOL_SCRIPTS_ENABLED.\n"));
5802 if ((getuid() <= 0 || geteuid() <= 0) &&
5803 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5804 fprintf(stderr
, gettext(
5805 "Can't run -c with root privileges "
5806 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5816 follow_links
= B_TRUE
;
5822 get_timestamp_arg(*optarg
);
5846 omit_since_boot
= B_TRUE
;
5849 headers_once
= B_TRUE
;
5855 if (optopt
== 'c') {
5856 print_zpool_script_list("iostat");
5860 gettext("invalid option '%c'\n"), optopt
);
5869 cb
.cb_literal
= parsable
;
5870 cb
.cb_scripted
= scripted
;
5873 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_GUID
;
5875 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
5877 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_PATH
;
5878 cb
.cb_iteration
= 0;
5879 cb
.cb_namewidth
= 0;
5880 cb
.cb_verbose
= verbose
;
5882 /* Get our interval and count values (if any) */
5884 get_interval_count_filter_guids(&argc
, argv
, &interval
,
5887 get_interval_count(&argc
, argv
, &interval
, &count
);
5891 /* No args, so just print the defaults. */
5892 } else if (are_all_pools(argc
, argv
)) {
5893 /* All the args are pool names */
5894 } else if (are_vdevs_in_pool(argc
, argv
, NULL
, &cb
.cb_vdevs
)) {
5895 /* All the args are vdevs */
5896 cb
.cb_vdevs
.cb_names
= argv
;
5897 cb
.cb_vdevs
.cb_names_count
= argc
;
5898 argc
= 0; /* No pools to process */
5899 } else if (are_all_pools(1, argv
)) {
5900 /* The first arg is a pool name */
5901 if (are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
5903 /* ...and the rest are vdev names */
5904 cb
.cb_vdevs
.cb_names
= argv
+ 1;
5905 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
5906 argc
= 1; /* One pool to process */
5908 fprintf(stderr
, gettext("Expected either a list of "));
5909 fprintf(stderr
, gettext("pools, or list of vdevs in"));
5910 fprintf(stderr
, " \"%s\", ", argv
[0]);
5911 fprintf(stderr
, gettext("but got:\n"));
5912 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
5913 argv
[0], &cb
.cb_vdevs
);
5914 fprintf(stderr
, "\n");
5920 * The args don't make sense. The first arg isn't a pool name,
5921 * nor are all the args vdevs.
5923 fprintf(stderr
, gettext("Unable to parse pools/vdevs list.\n"));
5924 fprintf(stderr
, "\n");
5928 if (cb
.cb_vdevs
.cb_names_count
!= 0) {
5930 * If user specified vdevs, it implies verbose.
5932 cb
.cb_verbose
= B_TRUE
;
5936 * Construct the list of all interesting pools.
5939 if ((list
= pool_list_get(argc
, argv
, NULL
, ZFS_TYPE_POOL
, parsable
,
5943 if (pool_list_count(list
) == 0 && argc
!= 0) {
5944 pool_list_free(list
);
5948 if (pool_list_count(list
) == 0 && interval
== 0) {
5949 pool_list_free(list
);
5950 (void) fprintf(stderr
, gettext("no pools available\n"));
5954 if ((l_histo
|| rq_histo
) && (cmd
!= NULL
|| latency
|| queues
)) {
5955 pool_list_free(list
);
5956 (void) fprintf(stderr
,
5957 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5962 if (l_histo
&& rq_histo
) {
5963 pool_list_free(list
);
5964 (void) fprintf(stderr
,
5965 gettext("Only one of [-r|-w] can be passed at a time\n"));
5971 * Enter the main iostat loop.
5977 * Histograms tables look out of place when you try to display
5978 * them with the other stats, so make a rule that you can only
5979 * print histograms by themselves.
5981 cb
.cb_flags
= IOS_L_HISTO_M
;
5982 } else if (rq_histo
) {
5983 cb
.cb_flags
= IOS_RQ_HISTO_M
;
5985 cb
.cb_flags
= IOS_DEFAULT_M
;
5987 cb
.cb_flags
|= IOS_LATENCY_M
;
5989 cb
.cb_flags
|= IOS_QUEUES_M
;
5993 * See if the module supports all the stats we want to display.
5995 unsupported_flags
= cb
.cb_flags
& ~get_stat_flags(list
);
5996 if (unsupported_flags
) {
6000 gettext("The loaded zfs module doesn't support:"));
6002 /* for each bit set in unsupported_flags */
6003 for (f
= unsupported_flags
; f
; f
&= ~(1ULL << idx
)) {
6004 idx
= lowbit64(f
) - 1;
6005 fprintf(stderr
, " -%c", flag_to_arg
[idx
]);
6008 fprintf(stderr
, ". Try running a newer module.\n");
6009 pool_list_free(list
);
6015 if ((npools
= pool_list_count(list
)) == 0)
6016 (void) fprintf(stderr
, gettext("no pools available\n"));
6019 * If this is the first iteration and -y was supplied
6020 * we skip any printing.
6022 boolean_t skip
= (omit_since_boot
&&
6023 cb
.cb_iteration
== 0);
6026 * Refresh all statistics. This is done as an
6027 * explicit step before calculating the maximum name
6028 * width, so that any * configuration changes are
6029 * properly accounted for.
6031 (void) pool_list_iter(list
, B_FALSE
, refresh_iostat
,
6035 * Iterate over all pools to determine the maximum width
6036 * for the pool / device name column across all pools.
6038 cb
.cb_namewidth
= 0;
6039 (void) pool_list_iter(list
, B_FALSE
,
6040 get_namewidth_iostat
, &cb
);
6042 if (timestamp_fmt
!= NODATE
)
6043 print_timestamp(timestamp_fmt
);
6045 if (cmd
!= NULL
&& cb
.cb_verbose
&&
6046 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) {
6047 cb
.vcdl
= all_pools_for_each_vdev_run(argc
,
6048 argv
, cmd
, g_zfs
, cb
.cb_vdevs
.cb_names
,
6049 cb
.cb_vdevs
.cb_names_count
,
6050 cb
.cb_vdevs
.cb_name_flags
);
6057 * Check terminal size so we can print headers
6058 * even when terminal window has its height
6061 winheight
= terminal_height();
6063 * Are we connected to TTY? If not, headers_once
6064 * should be true, to avoid breaking scripts.
6067 headers_once
= B_TRUE
;
6070 * If it's the first time and we're not skipping it,
6071 * or either skip or verbose mode, print the header.
6073 * The histogram code explicitly prints its header on
6074 * every vdev, so skip this for histograms.
6076 if (((++cb
.cb_iteration
== 1 && !skip
) ||
6077 (skip
!= verbose
) ||
6079 (cb
.cb_iteration
% winheight
) == 0)) &&
6080 (!(cb
.cb_flags
& IOS_ANYHISTO_M
)) &&
6082 print_iostat_header(&cb
);
6085 (void) fflush(stdout
);
6086 (void) fsleep(interval
);
6090 pool_list_iter(list
, B_FALSE
, print_iostat
, &cb
);
6093 * If there's more than one pool, and we're not in
6094 * verbose mode (which prints a separator for us),
6095 * then print a separator.
6097 * In addition, if we're printing specific vdevs then
6098 * we also want an ending separator.
6100 if (((npools
> 1 && !verbose
&&
6101 !(cb
.cb_flags
& IOS_ANYHISTO_M
)) ||
6102 (!(cb
.cb_flags
& IOS_ANYHISTO_M
) &&
6103 cb
.cb_vdevs
.cb_names_count
)) &&
6105 print_iostat_separator(&cb
);
6106 if (cb
.vcdl
!= NULL
)
6107 print_cmd_columns(cb
.vcdl
, 1);
6111 if (cb
.vcdl
!= NULL
)
6112 free_vdev_cmd_data_list(cb
.vcdl
);
6119 if (count
!= 0 && --count
== 0)
6122 (void) fflush(stdout
);
6123 (void) fsleep(interval
);
6126 pool_list_free(list
);
6131 typedef struct list_cbdata
{
6132 boolean_t cb_verbose
;
6135 boolean_t cb_scripted
;
6136 zprop_list_t
*cb_proplist
;
6137 boolean_t cb_literal
;
6142 * Given a list of columns to display, output appropriate headers for each one.
6145 print_header(list_cbdata_t
*cb
)
6147 zprop_list_t
*pl
= cb
->cb_proplist
;
6148 char headerbuf
[ZPOOL_MAXPROPLEN
];
6150 boolean_t first
= B_TRUE
;
6151 boolean_t right_justify
;
6154 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6155 width
= pl
->pl_width
;
6156 if (first
&& cb
->cb_verbose
) {
6158 * Reset the width to accommodate the verbose listing
6161 width
= cb
->cb_namewidth
;
6165 (void) fputs(" ", stdout
);
6169 right_justify
= B_FALSE
;
6170 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6171 header
= zpool_prop_column_name(pl
->pl_prop
);
6172 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6176 for (i
= 0; pl
->pl_user_prop
[i
] != '\0'; i
++)
6177 headerbuf
[i
] = toupper(pl
->pl_user_prop
[i
]);
6178 headerbuf
[i
] = '\0';
6182 if (pl
->pl_next
== NULL
&& !right_justify
)
6183 (void) fputs(header
, stdout
);
6184 else if (right_justify
)
6185 (void) printf("%*s", (int)width
, header
);
6187 (void) printf("%-*s", (int)width
, header
);
6190 (void) fputc('\n', stdout
);
6194 * Given a pool and a list of properties, print out all the properties according
6195 * to the described layout. Used by zpool_do_list().
6198 print_pool(zpool_handle_t
*zhp
, list_cbdata_t
*cb
)
6200 zprop_list_t
*pl
= cb
->cb_proplist
;
6201 boolean_t first
= B_TRUE
;
6202 char property
[ZPOOL_MAXPROPLEN
];
6203 const char *propstr
;
6204 boolean_t right_justify
;
6207 for (; pl
!= NULL
; pl
= pl
->pl_next
) {
6209 width
= pl
->pl_width
;
6210 if (first
&& cb
->cb_verbose
) {
6212 * Reset the width to accommodate the verbose listing
6215 width
= cb
->cb_namewidth
;
6219 if (cb
->cb_scripted
)
6220 (void) fputc('\t', stdout
);
6222 (void) fputs(" ", stdout
);
6227 right_justify
= B_FALSE
;
6228 if (pl
->pl_prop
!= ZPROP_USERPROP
) {
6229 if (zpool_get_prop(zhp
, pl
->pl_prop
, property
,
6230 sizeof (property
), NULL
, cb
->cb_literal
) != 0)
6235 right_justify
= zpool_prop_align_right(pl
->pl_prop
);
6236 } else if ((zpool_prop_feature(pl
->pl_user_prop
) ||
6237 zpool_prop_unsupported(pl
->pl_user_prop
)) &&
6238 zpool_prop_get_feature(zhp
, pl
->pl_user_prop
, property
,
6239 sizeof (property
)) == 0) {
6241 } else if (zfs_prop_user(pl
->pl_user_prop
) &&
6242 zpool_get_userprop(zhp
, pl
->pl_user_prop
, property
,
6243 sizeof (property
), NULL
) == 0) {
6250 * If this is being called in scripted mode, or if this is the
6251 * last column and it is left-justified, don't include a width
6254 if (cb
->cb_scripted
|| (pl
->pl_next
== NULL
&& !right_justify
))
6255 (void) fputs(propstr
, stdout
);
6256 else if (right_justify
)
6257 (void) printf("%*s", (int)width
, propstr
);
6259 (void) printf("%-*s", (int)width
, propstr
);
6262 (void) fputc('\n', stdout
);
6266 print_one_column(zpool_prop_t prop
, uint64_t value
, const char *str
,
6267 boolean_t scripted
, boolean_t valid
, enum zfs_nicenum_format format
)
6271 size_t width
= zprop_width(prop
, &fixed
, ZFS_TYPE_POOL
);
6274 case ZPOOL_PROP_SIZE
:
6275 case ZPOOL_PROP_EXPANDSZ
:
6276 case ZPOOL_PROP_CHECKPOINT
:
6277 case ZPOOL_PROP_DEDUPRATIO
:
6279 (void) strlcpy(propval
, "-", sizeof (propval
));
6281 zfs_nicenum_format(value
, propval
, sizeof (propval
),
6284 case ZPOOL_PROP_FRAGMENTATION
:
6285 if (value
== ZFS_FRAG_INVALID
) {
6286 (void) strlcpy(propval
, "-", sizeof (propval
));
6287 } else if (format
== ZFS_NICENUM_RAW
) {
6288 (void) snprintf(propval
, sizeof (propval
), "%llu",
6289 (unsigned long long)value
);
6291 (void) snprintf(propval
, sizeof (propval
), "%llu%%",
6292 (unsigned long long)value
);
6295 case ZPOOL_PROP_CAPACITY
:
6296 /* capacity value is in parts-per-10,000 (aka permyriad) */
6297 if (format
== ZFS_NICENUM_RAW
)
6298 (void) snprintf(propval
, sizeof (propval
), "%llu",
6299 (unsigned long long)value
/ 100);
6301 (void) snprintf(propval
, sizeof (propval
),
6302 value
< 1000 ? "%1.2f%%" : value
< 10000 ?
6303 "%2.1f%%" : "%3.0f%%", value
/ 100.0);
6305 case ZPOOL_PROP_HEALTH
:
6307 (void) strlcpy(propval
, str
, sizeof (propval
));
6310 zfs_nicenum_format(value
, propval
, sizeof (propval
), format
);
6314 (void) strlcpy(propval
, "-", sizeof (propval
));
6317 (void) printf("\t%s", propval
);
6319 (void) printf(" %*s", (int)width
, propval
);
6323 * print static default line per vdev
6324 * not compatible with '-o' <proplist> option
6327 print_list_stats(zpool_handle_t
*zhp
, const char *name
, nvlist_t
*nv
,
6328 list_cbdata_t
*cb
, int depth
, boolean_t isspare
)
6334 boolean_t scripted
= cb
->cb_scripted
;
6335 uint64_t islog
= B_FALSE
;
6336 const char *dashes
= "%-*s - - - - "
6339 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
6340 (uint64_t **)&vs
, &c
) == 0);
6343 boolean_t toplevel
= (vs
->vs_space
!= 0);
6345 enum zfs_nicenum_format format
;
6349 format
= ZFS_NICENUM_RAW
;
6351 format
= ZFS_NICENUM_1024
;
6353 if (strcmp(name
, VDEV_TYPE_INDIRECT
) == 0)
6357 (void) printf("\t%s", name
);
6358 else if (strlen(name
) + depth
> cb
->cb_namewidth
)
6359 (void) printf("%*s%s", depth
, "", name
);
6361 (void) printf("%*s%s%*s", depth
, "", name
,
6362 (int)(cb
->cb_namewidth
- strlen(name
) - depth
), "");
6365 * Print the properties for the individual vdevs. Some
6366 * properties are only applicable to toplevel vdevs. The
6367 * 'toplevel' boolean value is passed to the print_one_column()
6368 * to indicate that the value is valid.
6370 if (VDEV_STAT_VALID(vs_pspace
, c
) && vs
->vs_pspace
)
6371 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_pspace
, NULL
,
6372 scripted
, B_TRUE
, format
);
6374 print_one_column(ZPOOL_PROP_SIZE
, vs
->vs_space
, NULL
,
6375 scripted
, toplevel
, format
);
6376 print_one_column(ZPOOL_PROP_ALLOCATED
, vs
->vs_alloc
, NULL
,
6377 scripted
, toplevel
, format
);
6378 print_one_column(ZPOOL_PROP_FREE
, vs
->vs_space
- vs
->vs_alloc
,
6379 NULL
, scripted
, toplevel
, format
);
6380 print_one_column(ZPOOL_PROP_CHECKPOINT
,
6381 vs
->vs_checkpoint_space
, NULL
, scripted
, toplevel
, format
);
6382 print_one_column(ZPOOL_PROP_EXPANDSZ
, vs
->vs_esize
, NULL
,
6383 scripted
, B_TRUE
, format
);
6384 print_one_column(ZPOOL_PROP_FRAGMENTATION
,
6385 vs
->vs_fragmentation
, NULL
, scripted
,
6386 (vs
->vs_fragmentation
!= ZFS_FRAG_INVALID
&& toplevel
),
6388 cap
= (vs
->vs_space
== 0) ? 0 :
6389 (vs
->vs_alloc
* 10000 / vs
->vs_space
);
6390 print_one_column(ZPOOL_PROP_CAPACITY
, cap
, NULL
,
6391 scripted
, toplevel
, format
);
6392 print_one_column(ZPOOL_PROP_DEDUPRATIO
, 0, NULL
,
6393 scripted
, toplevel
, format
);
6394 state
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
6396 if (vs
->vs_aux
== VDEV_AUX_SPARED
)
6398 else if (vs
->vs_state
== VDEV_STATE_HEALTHY
)
6401 print_one_column(ZPOOL_PROP_HEALTH
, 0, state
, scripted
,
6403 (void) fputc('\n', stdout
);
6406 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
6407 &child
, &children
) != 0)
6410 /* list the normal vdevs first */
6411 for (c
= 0; c
< children
; c
++) {
6412 uint64_t ishole
= B_FALSE
;
6414 if (nvlist_lookup_uint64(child
[c
],
6415 ZPOOL_CONFIG_IS_HOLE
, &ishole
) == 0 && ishole
)
6418 if (nvlist_lookup_uint64(child
[c
],
6419 ZPOOL_CONFIG_IS_LOG
, &islog
) == 0 && islog
)
6422 if (nvlist_exists(child
[c
], ZPOOL_CONFIG_ALLOCATION_BIAS
))
6425 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6426 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6427 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2, B_FALSE
);
6431 /* list the classes: 'logs', 'dedup', and 'special' */
6432 for (uint_t n
= 0; n
< ARRAY_SIZE(class_name
); n
++) {
6433 boolean_t printed
= B_FALSE
;
6435 for (c
= 0; c
< children
; c
++) {
6436 const char *bias
= NULL
;
6437 const char *type
= NULL
;
6439 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
6440 &islog
) == 0 && islog
) {
6441 bias
= VDEV_ALLOC_CLASS_LOGS
;
6443 (void) nvlist_lookup_string(child
[c
],
6444 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
);
6445 (void) nvlist_lookup_string(child
[c
],
6446 ZPOOL_CONFIG_TYPE
, &type
);
6448 if (bias
== NULL
|| strcmp(bias
, class_name
[n
]) != 0)
6450 if (!islog
&& strcmp(type
, VDEV_TYPE_INDIRECT
) == 0)
6454 /* LINTED E_SEC_PRINTF_VAR_FMT */
6455 (void) printf(dashes
, cb
->cb_namewidth
,
6459 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6460 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
6461 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6467 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
6468 &child
, &children
) == 0 && children
> 0) {
6469 /* LINTED E_SEC_PRINTF_VAR_FMT */
6470 (void) printf(dashes
, cb
->cb_namewidth
, "cache");
6471 for (c
= 0; c
< children
; c
++) {
6472 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6474 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6480 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
, &child
,
6481 &children
) == 0 && children
> 0) {
6482 /* LINTED E_SEC_PRINTF_VAR_FMT */
6483 (void) printf(dashes
, cb
->cb_namewidth
, "spare");
6484 for (c
= 0; c
< children
; c
++) {
6485 vname
= zpool_vdev_name(g_zfs
, zhp
, child
[c
],
6487 print_list_stats(zhp
, vname
, child
[c
], cb
, depth
+ 2,
6495 * Generic callback function to list a pool.
6498 list_callback(zpool_handle_t
*zhp
, void *data
)
6500 list_cbdata_t
*cbp
= data
;
6502 print_pool(zhp
, cbp
);
6504 if (cbp
->cb_verbose
) {
6505 nvlist_t
*config
, *nvroot
;
6507 config
= zpool_get_config(zhp
, NULL
);
6508 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6510 print_list_stats(zhp
, NULL
, nvroot
, cbp
, 0, B_FALSE
);
6517 * Set the minimum pool/vdev name column width. The width must be at least 9,
6518 * but may be as large as needed.
6521 get_namewidth_list(zpool_handle_t
*zhp
, void *data
)
6523 list_cbdata_t
*cb
= data
;
6526 width
= get_namewidth(zhp
, cb
->cb_namewidth
,
6527 cb
->cb_name_flags
| VDEV_NAME_TYPE_ID
, cb
->cb_verbose
);
6532 cb
->cb_namewidth
= width
;
6538 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6540 * -g Display guid for individual vdev name.
6541 * -H Scripted mode. Don't display headers, and separate properties
6543 * -L Follow links when resolving vdev path name.
6544 * -o List of properties to display. Defaults to
6545 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6546 * "dedupratio,health,altroot"
6547 * -p Display values in parsable (exact) format.
6548 * -P Display full path for vdev name.
6549 * -T Display a timestamp in date(1) or Unix format
6551 * List all pools in the system, whether or not they're healthy. Output space
6552 * statistics for each one, as well as health status summary.
6555 zpool_do_list(int argc
, char **argv
)
6559 list_cbdata_t cb
= { 0 };
6560 static char default_props
[] =
6561 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6562 "capacity,dedupratio,health,altroot";
6563 char *props
= default_props
;
6565 unsigned long count
= 0;
6567 boolean_t first
= B_TRUE
;
6568 current_prop_type
= ZFS_TYPE_POOL
;
6571 while ((c
= getopt(argc
, argv
, ":gHLo:pPT:v")) != -1) {
6574 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
6577 cb
.cb_scripted
= B_TRUE
;
6580 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6586 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
6589 cb
.cb_literal
= B_TRUE
;
6592 get_timestamp_arg(*optarg
);
6595 cb
.cb_verbose
= B_TRUE
;
6596 cb
.cb_namewidth
= 8; /* 8 until precalc is avail */
6599 (void) fprintf(stderr
, gettext("missing argument for "
6600 "'%c' option\n"), optopt
);
6604 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6613 get_interval_count(&argc
, argv
, &interval
, &count
);
6615 if (zprop_get_list(g_zfs
, props
, &cb
.cb_proplist
, ZFS_TYPE_POOL
) != 0)
6619 if ((list
= pool_list_get(argc
, argv
, &cb
.cb_proplist
,
6620 ZFS_TYPE_POOL
, cb
.cb_literal
, &ret
)) == NULL
)
6623 if (pool_list_count(list
) == 0)
6626 cb
.cb_namewidth
= 0;
6627 (void) pool_list_iter(list
, B_FALSE
, get_namewidth_list
, &cb
);
6629 if (timestamp_fmt
!= NODATE
)
6630 print_timestamp(timestamp_fmt
);
6632 if (!cb
.cb_scripted
&& (first
|| cb
.cb_verbose
)) {
6636 ret
= pool_list_iter(list
, B_TRUE
, list_callback
, &cb
);
6641 if (count
!= 0 && --count
== 0)
6644 pool_list_free(list
);
6646 (void) fflush(stdout
);
6647 (void) fsleep(interval
);
6650 if (argc
== 0 && !cb
.cb_scripted
&& pool_list_count(list
) == 0) {
6651 (void) printf(gettext("no pools available\n"));
6655 pool_list_free(list
);
6656 zprop_free_list(cb
.cb_proplist
);
6661 zpool_do_attach_or_replace(int argc
, char **argv
, int replacing
)
6663 boolean_t force
= B_FALSE
;
6664 boolean_t rebuild
= B_FALSE
;
6665 boolean_t wait
= B_FALSE
;
6668 char *poolname
, *old_disk
, *new_disk
;
6669 zpool_handle_t
*zhp
;
6670 nvlist_t
*props
= NULL
;
6675 while ((c
= getopt(argc
, argv
, "fo:sw")) != -1) {
6681 if ((propval
= strchr(optarg
, '=')) == NULL
) {
6682 (void) fprintf(stderr
, gettext("missing "
6683 "'=' for -o option\n"));
6689 if ((strcmp(optarg
, ZPOOL_CONFIG_ASHIFT
) != 0) ||
6690 (add_prop_list(optarg
, propval
, &props
, B_TRUE
)))
6700 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6709 /* get pool name and check number of arguments */
6711 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6718 (void) fprintf(stderr
,
6719 gettext("missing <device> specification\n"));
6727 (void) fprintf(stderr
,
6728 gettext("missing <new_device> specification\n"));
6731 new_disk
= old_disk
;
6741 (void) fprintf(stderr
, gettext("too many arguments\n"));
6745 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
6750 if (zpool_get_config(zhp
, NULL
) == NULL
) {
6751 (void) fprintf(stderr
, gettext("pool '%s' is unavailable\n"),
6758 /* unless manually specified use "ashift" pool property (if set) */
6759 if (!nvlist_exists(props
, ZPOOL_CONFIG_ASHIFT
)) {
6762 char strval
[ZPOOL_MAXPROPLEN
];
6764 intval
= zpool_get_prop_int(zhp
, ZPOOL_PROP_ASHIFT
, &src
);
6765 if (src
!= ZPROP_SRC_DEFAULT
) {
6766 (void) sprintf(strval
, "%" PRId32
, intval
);
6767 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT
, strval
,
6768 &props
, B_TRUE
) == 0);
6772 nvroot
= make_root_vdev(zhp
, props
, force
, B_FALSE
, replacing
, B_FALSE
,
6774 if (nvroot
== NULL
) {
6780 ret
= zpool_vdev_attach(zhp
, old_disk
, new_disk
, nvroot
, replacing
,
6783 if (ret
== 0 && wait
) {
6784 zpool_wait_activity_t activity
= ZPOOL_WAIT_RESILVER
;
6785 char raidz_prefix
[] = "raidz";
6787 activity
= ZPOOL_WAIT_REPLACE
;
6788 } else if (strncmp(old_disk
,
6789 raidz_prefix
, strlen(raidz_prefix
)) == 0) {
6790 activity
= ZPOOL_WAIT_RAIDZ_EXPAND
;
6792 ret
= zpool_wait(zhp
, activity
);
6796 nvlist_free(nvroot
);
6803 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6805 * -f Force attach, even if <new_device> appears to be in use.
6806 * -s Use sequential instead of healing reconstruction for resilver.
6807 * -o Set property=value.
6808 * -w Wait for replacing to complete before returning
6810 * Replace <device> with <new_device>.
6813 zpool_do_replace(int argc
, char **argv
)
6815 return (zpool_do_attach_or_replace(argc
, argv
, B_TRUE
));
6819 * zpool attach [-fsw] [-o property=value] <pool> <device>|<vdev> <new_device>
6821 * -f Force attach, even if <new_device> appears to be in use.
6822 * -s Use sequential instead of healing reconstruction for resilver.
6823 * -o Set property=value.
6824 * -w Wait for resilvering (mirror) or expansion (raidz) to complete
6827 * Attach <new_device> to a <device> or <vdev>, where the vdev can be of type
6828 * mirror or raidz. If <device> is not part of a mirror, then <device> will
6829 * be transformed into a mirror of <device> and <new_device>. When a mirror
6830 * is involved, <new_device> will begin life with a DTL of [0, now], and will
6831 * immediately begin to resilver itself. For the raidz case, a expansion will
6832 * commence and reflow the raidz data across all the disks including the
6836 zpool_do_attach(int argc
, char **argv
)
6838 return (zpool_do_attach_or_replace(argc
, argv
, B_FALSE
));
6842 * zpool detach [-f] <pool> <device>
6844 * -f Force detach of <device>, even if DTLs argue against it
6845 * (not supported yet)
6847 * Detach a device from a mirror. The operation will be refused if <device>
6848 * is the last device in the mirror, or if the DTLs indicate that this device
6849 * has the only valid copy of some data.
6852 zpool_do_detach(int argc
, char **argv
)
6855 char *poolname
, *path
;
6856 zpool_handle_t
*zhp
;
6860 while ((c
= getopt(argc
, argv
, "")) != -1) {
6863 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6872 /* get pool name and check number of arguments */
6874 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
6879 (void) fprintf(stderr
,
6880 gettext("missing <device> specification\n"));
6887 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
6890 ret
= zpool_vdev_detach(zhp
, path
);
6898 * zpool split [-gLnP] [-o prop=val] ...
6900 * [-R altroot] <pool> <newpool> [<device> ...]
6902 * -g Display guid for individual vdev name.
6903 * -L Follow links when resolving vdev path name.
6904 * -n Do not split the pool, but display the resulting layout if
6905 * it were to be split.
6906 * -o Set property=value, or set mount options.
6907 * -P Display full path for vdev name.
6908 * -R Mount the split-off pool under an alternate root.
6909 * -l Load encryption keys while importing.
6911 * Splits the named pool and gives it the new pool name. Devices to be split
6912 * off may be listed, provided that no more than one device is specified
6913 * per top-level vdev mirror. The newly split pool is left in an exported
6914 * state unless -R is specified.
6916 * Restrictions: the top-level of the pool pool must only be made up of
6917 * mirrors; all devices in the pool must be healthy; no device may be
6918 * undergoing a resilvering operation.
6921 zpool_do_split(int argc
, char **argv
)
6923 char *srcpool
, *newpool
, *propval
;
6924 char *mntopts
= NULL
;
6928 boolean_t loadkeys
= B_FALSE
;
6929 zpool_handle_t
*zhp
;
6930 nvlist_t
*config
, *props
= NULL
;
6932 flags
.dryrun
= B_FALSE
;
6933 flags
.import
= B_FALSE
;
6934 flags
.name_flags
= 0;
6937 while ((c
= getopt(argc
, argv
, ":gLR:lno:P")) != -1) {
6940 flags
.name_flags
|= VDEV_NAME_GUID
;
6943 flags
.name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
6946 flags
.import
= B_TRUE
;
6948 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), optarg
,
6949 &props
, B_TRUE
) != 0) {
6958 flags
.dryrun
= B_TRUE
;
6961 if ((propval
= strchr(optarg
, '=')) != NULL
) {
6964 if (add_prop_list(optarg
, propval
,
6965 &props
, B_TRUE
) != 0) {
6974 flags
.name_flags
|= VDEV_NAME_PATH
;
6977 (void) fprintf(stderr
, gettext("missing argument for "
6978 "'%c' option\n"), optopt
);
6982 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
6989 if (!flags
.import
&& mntopts
!= NULL
) {
6990 (void) fprintf(stderr
, gettext("setting mntopts is only "
6991 "valid when importing the pool\n"));
6995 if (!flags
.import
&& loadkeys
) {
6996 (void) fprintf(stderr
, gettext("loading keys is only "
6997 "valid when importing the pool\n"));
7005 (void) fprintf(stderr
, gettext("Missing pool name\n"));
7009 (void) fprintf(stderr
, gettext("Missing new pool name\n"));
7019 if ((zhp
= zpool_open(g_zfs
, srcpool
)) == NULL
) {
7024 config
= split_mirror_vdev(zhp
, newpool
, props
, flags
, argc
, argv
);
7025 if (config
== NULL
) {
7029 (void) printf(gettext("would create '%s' with the "
7030 "following layout:\n\n"), newpool
);
7031 print_vdev_tree(NULL
, newpool
, config
, 0, "",
7033 print_vdev_tree(NULL
, "dedup", config
, 0,
7034 VDEV_ALLOC_BIAS_DEDUP
, 0);
7035 print_vdev_tree(NULL
, "special", config
, 0,
7036 VDEV_ALLOC_BIAS_SPECIAL
, 0);
7042 if (ret
!= 0 || flags
.dryrun
|| !flags
.import
) {
7043 nvlist_free(config
);
7049 * The split was successful. Now we need to open the new
7050 * pool and import it.
7052 if ((zhp
= zpool_open_canfail(g_zfs
, newpool
)) == NULL
) {
7053 nvlist_free(config
);
7059 ret
= zfs_crypto_attempt_load_keys(g_zfs
, newpool
);
7064 if (zpool_get_state(zhp
) != POOL_STATE_UNAVAIL
) {
7065 ms_status
= zpool_enable_datasets(zhp
, mntopts
, 0);
7066 if (ms_status
== EZFS_SHAREFAILED
) {
7067 (void) fprintf(stderr
, gettext("Split was successful, "
7068 "datasets are mounted but sharing of some datasets "
7070 } else if (ms_status
== EZFS_MOUNTFAILED
) {
7071 (void) fprintf(stderr
, gettext("Split was successful"
7072 ", but some datasets could not be mounted\n"));
7073 (void) fprintf(stderr
, gettext("Try doing '%s' with a "
7074 "different altroot\n"), "zpool import");
7078 nvlist_free(config
);
7084 #define POWER_OPT 1024
7087 * zpool online [--power] <pool> <device> ...
7089 * --power: Power on the enclosure slot to the drive (if possible)
7092 zpool_do_online(int argc
, char **argv
)
7096 zpool_handle_t
*zhp
;
7098 vdev_state_t newstate
;
7100 boolean_t is_power_on
= B_FALSE
;
7101 struct option long_options
[] = {
7102 {"power", no_argument
, NULL
, POWER_OPT
},
7107 while ((c
= getopt_long(argc
, argv
, "e", long_options
, NULL
)) != -1) {
7110 flags
|= ZFS_ONLINE_EXPAND
;
7113 is_power_on
= B_TRUE
;
7116 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7122 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
7123 is_power_on
= B_TRUE
;
7128 /* get pool name and check number of arguments */
7130 (void) fprintf(stderr
, gettext("missing pool name\n"));
7134 (void) fprintf(stderr
, gettext("missing device name\n"));
7140 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7143 for (i
= 1; i
< argc
; i
++) {
7144 vdev_state_t oldstate
;
7145 boolean_t avail_spare
, l2cache
;
7149 rc
= zpool_power_on_and_disk_wait(zhp
, argv
[i
]);
7150 if (rc
== ENOTSUP
) {
7151 (void) fprintf(stderr
,
7152 gettext("Power control not supported\n"));
7158 nvlist_t
*tgt
= zpool_find_vdev(zhp
, argv
[i
], &avail_spare
,
7165 oldstate
= ((vdev_stat_t
*)fnvlist_lookup_uint64_array(tgt
,
7166 ZPOOL_CONFIG_VDEV_STATS
, &vsc
))->vs_state
;
7167 if (zpool_vdev_online(zhp
, argv
[i
], flags
, &newstate
) == 0) {
7168 if (newstate
!= VDEV_STATE_HEALTHY
) {
7169 (void) printf(gettext("warning: device '%s' "
7170 "onlined, but remains in faulted state\n"),
7172 if (newstate
== VDEV_STATE_FAULTED
)
7173 (void) printf(gettext("use 'zpool "
7174 "clear' to restore a faulted "
7177 (void) printf(gettext("use 'zpool "
7178 "replace' to replace devices "
7179 "that are no longer present\n"));
7180 if ((flags
& ZFS_ONLINE_EXPAND
)) {
7181 (void) printf(gettext("%s: failed "
7182 "to expand usable space on "
7183 "unhealthy device '%s'\n"),
7184 (oldstate
>= VDEV_STATE_DEGRADED
?
7185 "error" : "warning"), argv
[i
]);
7186 if (oldstate
>= VDEV_STATE_DEGRADED
) {
7203 * zpool offline [-ft]|[--power] <pool> <device> ...
7206 * -f Force the device into a faulted state.
7208 * -t Only take the device off-line temporarily. The offline/faulted
7209 * state will not be persistent across reboots.
7211 * --power Power off the enclosure slot to the drive (if possible)
7214 zpool_do_offline(int argc
, char **argv
)
7218 zpool_handle_t
*zhp
;
7220 boolean_t istmp
= B_FALSE
;
7221 boolean_t fault
= B_FALSE
;
7222 boolean_t is_power_off
= B_FALSE
;
7224 struct option long_options
[] = {
7225 {"power", no_argument
, NULL
, POWER_OPT
},
7230 while ((c
= getopt_long(argc
, argv
, "ft", long_options
, NULL
)) != -1) {
7239 is_power_off
= B_TRUE
;
7242 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7248 if (is_power_off
&& fault
) {
7249 (void) fprintf(stderr
,
7250 gettext("-0 and -f cannot be used together\n"));
7255 if (is_power_off
&& istmp
) {
7256 (void) fprintf(stderr
,
7257 gettext("-0 and -t cannot be used together\n"));
7265 /* get pool name and check number of arguments */
7267 (void) fprintf(stderr
, gettext("missing pool name\n"));
7271 (void) fprintf(stderr
, gettext("missing device name\n"));
7277 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7280 for (i
= 1; i
< argc
; i
++) {
7281 uint64_t guid
= zpool_vdev_path_to_guid(zhp
, argv
[i
]);
7284 * Note: we have to power off first, then set REMOVED,
7285 * or else zpool_vdev_set_removed_state() returns
7288 ret
= zpool_power_off(zhp
, argv
[i
]);
7290 (void) fprintf(stderr
, "%s %s %d\n",
7291 gettext("unable to power off slot for"),
7294 zpool_vdev_set_removed_state(zhp
, guid
, VDEV_AUX_NONE
);
7298 if (istmp
== B_FALSE
) {
7299 /* Force the fault to persist across imports */
7300 aux
= VDEV_AUX_EXTERNAL_PERSIST
;
7302 aux
= VDEV_AUX_EXTERNAL
;
7305 if (guid
== 0 || zpool_vdev_fault(zhp
, guid
, aux
) != 0)
7308 if (zpool_vdev_offline(zhp
, argv
[i
], istmp
) != 0)
7319 * zpool clear [-nF]|[--power] <pool> [device]
7321 * Clear all errors associated with a pool or a particular device.
7324 zpool_do_clear(int argc
, char **argv
)
7328 boolean_t dryrun
= B_FALSE
;
7329 boolean_t do_rewind
= B_FALSE
;
7330 boolean_t xtreme_rewind
= B_FALSE
;
7331 boolean_t is_power_on
= B_FALSE
;
7332 uint32_t rewind_policy
= ZPOOL_NO_REWIND
;
7333 nvlist_t
*policy
= NULL
;
7334 zpool_handle_t
*zhp
;
7335 char *pool
, *device
;
7337 struct option long_options
[] = {
7338 {"power", no_argument
, NULL
, POWER_OPT
},
7343 while ((c
= getopt_long(argc
, argv
, "FnX", long_options
,
7353 xtreme_rewind
= B_TRUE
;
7356 is_power_on
= B_TRUE
;
7359 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7365 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
7366 is_power_on
= B_TRUE
;
7372 (void) fprintf(stderr
, gettext("missing pool name\n"));
7377 (void) fprintf(stderr
, gettext("too many arguments\n"));
7381 if ((dryrun
|| xtreme_rewind
) && !do_rewind
) {
7382 (void) fprintf(stderr
,
7383 gettext("-n or -X only meaningful with -F\n"));
7387 rewind_policy
= ZPOOL_TRY_REWIND
;
7389 rewind_policy
= ZPOOL_DO_REWIND
;
7391 rewind_policy
|= ZPOOL_EXTREME_REWIND
;
7393 /* In future, further rewind policy choices can be passed along here */
7394 if (nvlist_alloc(&policy
, NV_UNIQUE_NAME
, 0) != 0 ||
7395 nvlist_add_uint32(policy
, ZPOOL_LOAD_REWIND_POLICY
,
7396 rewind_policy
) != 0) {
7401 device
= argc
== 2 ? argv
[1] : NULL
;
7403 if ((zhp
= zpool_open_canfail(g_zfs
, pool
)) == NULL
) {
7404 nvlist_free(policy
);
7409 if (device
== NULL
) {
7410 zpool_power_on_pool_and_wait_for_devices(zhp
);
7412 zpool_power_on_and_disk_wait(zhp
, device
);
7416 if (zpool_clear(zhp
, device
, policy
) != 0)
7421 nvlist_free(policy
);
7427 * zpool reguid <pool>
7430 zpool_do_reguid(int argc
, char **argv
)
7434 zpool_handle_t
*zhp
;
7438 while ((c
= getopt(argc
, argv
, "")) != -1) {
7441 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7450 /* get pool name and check number of arguments */
7452 (void) fprintf(stderr
, gettext("missing pool name\n"));
7457 (void) fprintf(stderr
, gettext("too many arguments\n"));
7462 if ((zhp
= zpool_open(g_zfs
, poolname
)) == NULL
)
7465 ret
= zpool_reguid(zhp
);
7473 * zpool reopen <pool>
7475 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7478 zpool_do_reopen(int argc
, char **argv
)
7482 boolean_t scrub_restart
= B_TRUE
;
7485 while ((c
= getopt(argc
, argv
, "n")) != -1) {
7488 scrub_restart
= B_FALSE
;
7491 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7500 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7501 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7502 B_FALSE
, zpool_reopen_one
, &scrub_restart
);
7507 typedef struct scrub_cbdata
{
7509 pool_scrub_cmd_t cb_scrub_cmd
;
7513 zpool_has_checkpoint(zpool_handle_t
*zhp
)
7515 nvlist_t
*config
, *nvroot
;
7517 config
= zpool_get_config(zhp
, NULL
);
7519 if (config
!= NULL
) {
7520 pool_checkpoint_stat_t
*pcs
= NULL
;
7523 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
7524 (void) nvlist_lookup_uint64_array(nvroot
,
7525 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
7527 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
7530 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
||
7531 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
7539 scrub_callback(zpool_handle_t
*zhp
, void *data
)
7541 scrub_cbdata_t
*cb
= data
;
7545 * Ignore faulted pools.
7547 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
7548 (void) fprintf(stderr
, gettext("cannot scan '%s': pool is "
7549 "currently unavailable\n"), zpool_get_name(zhp
));
7553 err
= zpool_scan(zhp
, cb
->cb_type
, cb
->cb_scrub_cmd
);
7555 if (err
== 0 && zpool_has_checkpoint(zhp
) &&
7556 cb
->cb_type
== POOL_SCAN_SCRUB
) {
7557 (void) printf(gettext("warning: will not scrub state that "
7558 "belongs to the checkpoint of pool '%s'\n"),
7559 zpool_get_name(zhp
));
7566 wait_callback(zpool_handle_t
*zhp
, void *data
)
7568 zpool_wait_activity_t
*act
= data
;
7569 return (zpool_wait(zhp
, *act
));
7573 * zpool scrub [-s | -p] [-w] [-e] <pool> ...
7575 * -e Only scrub blocks in the error log.
7576 * -s Stop. Stops any in-progress scrub.
7577 * -p Pause. Pause in-progress scrub.
7578 * -w Wait. Blocks until scrub has completed.
7581 zpool_do_scrub(int argc
, char **argv
)
7585 boolean_t wait
= B_FALSE
;
7588 cb
.cb_type
= POOL_SCAN_SCRUB
;
7589 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7591 boolean_t is_error_scrub
= B_FALSE
;
7592 boolean_t is_pause
= B_FALSE
;
7593 boolean_t is_stop
= B_FALSE
;
7596 while ((c
= getopt(argc
, argv
, "spwe")) != -1) {
7599 is_error_scrub
= B_TRUE
;
7611 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7617 if (is_pause
&& is_stop
) {
7618 (void) fprintf(stderr
, gettext("invalid option "
7619 "combination :-s and -p are mutually exclusive\n"));
7623 cb
.cb_type
= POOL_SCAN_ERRORSCRUB
;
7626 cb
.cb_scrub_cmd
= POOL_SCRUB_PAUSE
;
7627 } else if (is_stop
) {
7628 cb
.cb_type
= POOL_SCAN_NONE
;
7630 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7634 if (wait
&& (cb
.cb_type
== POOL_SCAN_NONE
||
7635 cb
.cb_scrub_cmd
== POOL_SCRUB_PAUSE
)) {
7636 (void) fprintf(stderr
, gettext("invalid option combination: "
7637 "-w cannot be used with -p or -s\n"));
7645 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7649 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7650 B_FALSE
, scrub_callback
, &cb
);
7652 if (wait
&& !error
) {
7653 zpool_wait_activity_t act
= ZPOOL_WAIT_SCRUB
;
7654 error
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7655 B_FALSE
, wait_callback
, &act
);
7662 * zpool resilver <pool> ...
7664 * Restarts any in-progress resilver
7667 zpool_do_resilver(int argc
, char **argv
)
7672 cb
.cb_type
= POOL_SCAN_RESILVER
;
7673 cb
.cb_scrub_cmd
= POOL_SCRUB_NORMAL
;
7676 while ((c
= getopt(argc
, argv
, "")) != -1) {
7679 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
7689 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7693 return (for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
7694 B_FALSE
, scrub_callback
, &cb
));
7698 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7700 * -c Cancel. Ends any in-progress trim.
7701 * -d Secure trim. Requires kernel and device support.
7702 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7703 * adding a multiplier suffix such as 'k' or 'm'.
7704 * -s Suspend. TRIM can then be restarted with no flags.
7705 * -w Wait. Blocks until trimming has completed.
7708 zpool_do_trim(int argc
, char **argv
)
7710 struct option long_options
[] = {
7711 {"cancel", no_argument
, NULL
, 'c'},
7712 {"secure", no_argument
, NULL
, 'd'},
7713 {"rate", required_argument
, NULL
, 'r'},
7714 {"suspend", no_argument
, NULL
, 's'},
7715 {"wait", no_argument
, NULL
, 'w'},
7719 pool_trim_func_t cmd_type
= POOL_TRIM_START
;
7721 boolean_t secure
= B_FALSE
;
7722 boolean_t wait
= B_FALSE
;
7725 while ((c
= getopt_long(argc
, argv
, "cdr:sw", long_options
, NULL
))
7729 if (cmd_type
!= POOL_TRIM_START
&&
7730 cmd_type
!= POOL_TRIM_CANCEL
) {
7731 (void) fprintf(stderr
, gettext("-c cannot be "
7732 "combined with other options\n"));
7735 cmd_type
= POOL_TRIM_CANCEL
;
7738 if (cmd_type
!= POOL_TRIM_START
) {
7739 (void) fprintf(stderr
, gettext("-d cannot be "
7740 "combined with the -c or -s options\n"));
7746 if (cmd_type
!= POOL_TRIM_START
) {
7747 (void) fprintf(stderr
, gettext("-r cannot be "
7748 "combined with the -c or -s options\n"));
7751 if (zfs_nicestrtonum(g_zfs
, optarg
, &rate
) == -1) {
7752 (void) fprintf(stderr
, "%s: %s\n",
7753 gettext("invalid value for rate"),
7754 libzfs_error_description(g_zfs
));
7759 if (cmd_type
!= POOL_TRIM_START
&&
7760 cmd_type
!= POOL_TRIM_SUSPEND
) {
7761 (void) fprintf(stderr
, gettext("-s cannot be "
7762 "combined with other options\n"));
7765 cmd_type
= POOL_TRIM_SUSPEND
;
7772 (void) fprintf(stderr
,
7773 gettext("invalid option '%c'\n"), optopt
);
7775 (void) fprintf(stderr
,
7776 gettext("invalid option '%s'\n"),
7787 (void) fprintf(stderr
, gettext("missing pool name argument\n"));
7792 if (wait
&& (cmd_type
!= POOL_TRIM_START
)) {
7793 (void) fprintf(stderr
, gettext("-w cannot be used with -c or "
7798 char *poolname
= argv
[0];
7799 zpool_handle_t
*zhp
= zpool_open(g_zfs
, poolname
);
7803 trimflags_t trim_flags
= {
7809 nvlist_t
*vdevs
= fnvlist_alloc();
7811 /* no individual leaf vdevs specified, so add them all */
7812 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
7813 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
7814 ZPOOL_CONFIG_VDEV_TREE
);
7815 zpool_collect_leaves(zhp
, nvroot
, vdevs
);
7816 trim_flags
.fullpool
= B_TRUE
;
7818 trim_flags
.fullpool
= B_FALSE
;
7819 for (int i
= 1; i
< argc
; i
++) {
7820 fnvlist_add_boolean(vdevs
, argv
[i
]);
7824 int error
= zpool_trim(zhp
, cmd_type
, vdevs
, &trim_flags
);
7826 fnvlist_free(vdevs
);
7833 * Converts a total number of seconds to a human readable string broken
7834 * down in to days/hours/minutes/seconds.
7837 secs_to_dhms(uint64_t total
, char *buf
)
7839 uint64_t days
= total
/ 60 / 60 / 24;
7840 uint64_t hours
= (total
/ 60 / 60) % 24;
7841 uint64_t mins
= (total
/ 60) % 60;
7842 uint64_t secs
= (total
% 60);
7845 (void) sprintf(buf
, "%llu days %02llu:%02llu:%02llu",
7846 (u_longlong_t
)days
, (u_longlong_t
)hours
,
7847 (u_longlong_t
)mins
, (u_longlong_t
)secs
);
7849 (void) sprintf(buf
, "%02llu:%02llu:%02llu",
7850 (u_longlong_t
)hours
, (u_longlong_t
)mins
,
7851 (u_longlong_t
)secs
);
7856 * Print out detailed error scrub status.
7859 print_err_scrub_status(pool_scan_stat_t
*ps
)
7861 time_t start
, end
, pause
;
7862 uint64_t total_secs_left
;
7863 uint64_t secs_left
, mins_left
, hours_left
, days_left
;
7864 uint64_t examined
, to_be_examined
;
7866 if (ps
== NULL
|| ps
->pss_error_scrub_func
!= POOL_SCAN_ERRORSCRUB
) {
7870 (void) printf(gettext(" scrub: "));
7872 start
= ps
->pss_error_scrub_start
;
7873 end
= ps
->pss_error_scrub_end
;
7874 pause
= ps
->pss_pass_error_scrub_pause
;
7875 examined
= ps
->pss_error_scrub_examined
;
7876 to_be_examined
= ps
->pss_error_scrub_to_be_examined
;
7878 assert(ps
->pss_error_scrub_func
== POOL_SCAN_ERRORSCRUB
);
7880 if (ps
->pss_error_scrub_state
== DSS_FINISHED
) {
7881 total_secs_left
= end
- start
;
7882 days_left
= total_secs_left
/ 60 / 60 / 24;
7883 hours_left
= (total_secs_left
/ 60 / 60) % 24;
7884 mins_left
= (total_secs_left
/ 60) % 60;
7885 secs_left
= (total_secs_left
% 60);
7887 (void) printf(gettext("scrubbed %llu error blocks in %llu days "
7888 "%02llu:%02llu:%02llu on %s"), (u_longlong_t
)examined
,
7889 (u_longlong_t
)days_left
, (u_longlong_t
)hours_left
,
7890 (u_longlong_t
)mins_left
, (u_longlong_t
)secs_left
,
7894 } else if (ps
->pss_error_scrub_state
== DSS_CANCELED
) {
7895 (void) printf(gettext("error scrub canceled on %s"),
7899 assert(ps
->pss_error_scrub_state
== DSS_ERRORSCRUBBING
);
7901 /* Error scrub is in progress. */
7903 (void) printf(gettext("error scrub in progress since %s"),
7906 (void) printf(gettext("error scrub paused since %s"),
7908 (void) printf(gettext("\terror scrub started on %s"),
7912 double fraction_done
= (double)examined
/ (to_be_examined
+ examined
);
7913 (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error"
7914 " blocks"), 100 * fraction_done
, (u_longlong_t
)examined
);
7916 (void) printf("\n");
7920 * Print out detailed scrub status.
7923 print_scan_scrub_resilver_status(pool_scan_stat_t
*ps
)
7925 time_t start
, end
, pause
;
7926 uint64_t pass_scanned
, scanned
, pass_issued
, issued
, total_s
, total_i
;
7927 uint64_t elapsed
, scan_rate
, issue_rate
;
7928 double fraction_done
;
7929 char processed_buf
[7], scanned_buf
[7], issued_buf
[7], total_s_buf
[7];
7930 char total_i_buf
[7], srate_buf
[7], irate_buf
[7], time_buf
[32];
7933 printf_color(ANSI_BOLD
, gettext("scan:"));
7936 /* If there's never been a scan, there's not much to say. */
7937 if (ps
== NULL
|| ps
->pss_func
== POOL_SCAN_NONE
||
7938 ps
->pss_func
>= POOL_SCAN_FUNCS
) {
7939 (void) printf(gettext("none requested\n"));
7943 start
= ps
->pss_start_time
;
7944 end
= ps
->pss_end_time
;
7945 pause
= ps
->pss_pass_scrub_pause
;
7947 zfs_nicebytes(ps
->pss_processed
, processed_buf
, sizeof (processed_buf
));
7949 int is_resilver
= ps
->pss_func
== POOL_SCAN_RESILVER
;
7950 int is_scrub
= ps
->pss_func
== POOL_SCAN_SCRUB
;
7951 assert(is_resilver
|| is_scrub
);
7953 /* Scan is finished or canceled. */
7954 if (ps
->pss_state
== DSS_FINISHED
) {
7955 secs_to_dhms(end
- start
, time_buf
);
7958 (void) printf(gettext("scrub repaired %s "
7959 "in %s with %llu errors on %s"), processed_buf
,
7960 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7962 } else if (is_resilver
) {
7963 (void) printf(gettext("resilvered %s "
7964 "in %s with %llu errors on %s"), processed_buf
,
7965 time_buf
, (u_longlong_t
)ps
->pss_errors
,
7969 } else if (ps
->pss_state
== DSS_CANCELED
) {
7971 (void) printf(gettext("scrub canceled on %s"),
7973 } else if (is_resilver
) {
7974 (void) printf(gettext("resilver canceled on %s"),
7980 assert(ps
->pss_state
== DSS_SCANNING
);
7982 /* Scan is in progress. Resilvers can't be paused. */
7985 (void) printf(gettext("scrub in progress since %s"),
7988 (void) printf(gettext("scrub paused since %s"),
7990 (void) printf(gettext("\tscrub started on %s"),
7993 } else if (is_resilver
) {
7994 (void) printf(gettext("resilver in progress since %s"),
7998 scanned
= ps
->pss_examined
;
7999 pass_scanned
= ps
->pss_pass_exam
;
8000 issued
= ps
->pss_issued
;
8001 pass_issued
= ps
->pss_pass_issued
;
8002 total_s
= ps
->pss_to_examine
;
8003 total_i
= ps
->pss_to_examine
- ps
->pss_skipped
;
8005 /* we are only done with a block once we have issued the IO for it */
8006 fraction_done
= (double)issued
/ total_i
;
8008 /* elapsed time for this pass, rounding up to 1 if it's 0 */
8009 elapsed
= time(NULL
) - ps
->pss_pass_start
;
8010 elapsed
-= ps
->pss_pass_scrub_spent_paused
;
8011 elapsed
= (elapsed
!= 0) ? elapsed
: 1;
8013 scan_rate
= pass_scanned
/ elapsed
;
8014 issue_rate
= pass_issued
/ elapsed
;
8016 /* format all of the numbers we will be reporting */
8017 zfs_nicebytes(scanned
, scanned_buf
, sizeof (scanned_buf
));
8018 zfs_nicebytes(issued
, issued_buf
, sizeof (issued_buf
));
8019 zfs_nicebytes(total_s
, total_s_buf
, sizeof (total_s_buf
));
8020 zfs_nicebytes(total_i
, total_i_buf
, sizeof (total_i_buf
));
8022 /* do not print estimated time if we have a paused scrub */
8023 (void) printf(gettext("\t%s / %s scanned"), scanned_buf
, total_s_buf
);
8024 if (pause
== 0 && scan_rate
> 0) {
8025 zfs_nicebytes(scan_rate
, srate_buf
, sizeof (srate_buf
));
8026 (void) printf(gettext(" at %s/s"), srate_buf
);
8028 (void) printf(gettext(", %s / %s issued"), issued_buf
, total_i_buf
);
8029 if (pause
== 0 && issue_rate
> 0) {
8030 zfs_nicebytes(issue_rate
, irate_buf
, sizeof (irate_buf
));
8031 (void) printf(gettext(" at %s/s"), irate_buf
);
8033 (void) printf(gettext("\n"));
8036 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
8037 processed_buf
, 100 * fraction_done
);
8038 } else if (is_scrub
) {
8039 (void) printf(gettext("\t%s repaired, %.2f%% done"),
8040 processed_buf
, 100 * fraction_done
);
8045 * Only provide an estimate iff:
8046 * 1) we haven't yet issued all we expected, and
8047 * 2) the issue rate exceeds 10 MB/s, and
8049 * a) a resilver which has started repairs, or
8050 * b) a scrub which has entered the issue phase.
8052 if (total_i
>= issued
&& issue_rate
>= 10 * 1024 * 1024 &&
8053 ((is_resilver
&& ps
->pss_processed
> 0) ||
8054 (is_scrub
&& issued
> 0))) {
8055 secs_to_dhms((total_i
- issued
) / issue_rate
, time_buf
);
8056 (void) printf(gettext(", %s to go\n"), time_buf
);
8058 (void) printf(gettext(", no estimated "
8059 "completion time\n"));
8062 (void) printf(gettext("\n"));
8067 print_rebuild_status_impl(vdev_rebuild_stat_t
*vrs
, uint_t c
, char *vdev_name
)
8069 if (vrs
== NULL
|| vrs
->vrs_state
== VDEV_REBUILD_NONE
)
8073 printf_color(ANSI_BOLD
, gettext("scan:"));
8076 uint64_t bytes_scanned
= vrs
->vrs_bytes_scanned
;
8077 uint64_t bytes_issued
= vrs
->vrs_bytes_issued
;
8078 uint64_t bytes_rebuilt
= vrs
->vrs_bytes_rebuilt
;
8079 uint64_t bytes_est_s
= vrs
->vrs_bytes_est
;
8080 uint64_t bytes_est_i
= vrs
->vrs_bytes_est
;
8081 if (c
> offsetof(vdev_rebuild_stat_t
, vrs_pass_bytes_skipped
) / 8)
8082 bytes_est_i
-= vrs
->vrs_pass_bytes_skipped
;
8083 uint64_t scan_rate
= (vrs
->vrs_pass_bytes_scanned
/
8084 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
8085 uint64_t issue_rate
= (vrs
->vrs_pass_bytes_issued
/
8086 (vrs
->vrs_pass_time_ms
+ 1)) * 1000;
8087 double scan_pct
= MIN((double)bytes_scanned
* 100 /
8088 (bytes_est_s
+ 1), 100);
8090 /* Format all of the numbers we will be reporting */
8091 char bytes_scanned_buf
[7], bytes_issued_buf
[7];
8092 char bytes_rebuilt_buf
[7], bytes_est_s_buf
[7], bytes_est_i_buf
[7];
8093 char scan_rate_buf
[7], issue_rate_buf
[7], time_buf
[32];
8094 zfs_nicebytes(bytes_scanned
, bytes_scanned_buf
,
8095 sizeof (bytes_scanned_buf
));
8096 zfs_nicebytes(bytes_issued
, bytes_issued_buf
,
8097 sizeof (bytes_issued_buf
));
8098 zfs_nicebytes(bytes_rebuilt
, bytes_rebuilt_buf
,
8099 sizeof (bytes_rebuilt_buf
));
8100 zfs_nicebytes(bytes_est_s
, bytes_est_s_buf
, sizeof (bytes_est_s_buf
));
8101 zfs_nicebytes(bytes_est_i
, bytes_est_i_buf
, sizeof (bytes_est_i_buf
));
8103 time_t start
= vrs
->vrs_start_time
;
8104 time_t end
= vrs
->vrs_end_time
;
8106 /* Rebuild is finished or canceled. */
8107 if (vrs
->vrs_state
== VDEV_REBUILD_COMPLETE
) {
8108 secs_to_dhms(vrs
->vrs_scan_time_ms
/ 1000, time_buf
);
8109 (void) printf(gettext("resilvered (%s) %s in %s "
8110 "with %llu errors on %s"), vdev_name
, bytes_rebuilt_buf
,
8111 time_buf
, (u_longlong_t
)vrs
->vrs_errors
, ctime(&end
));
8113 } else if (vrs
->vrs_state
== VDEV_REBUILD_CANCELED
) {
8114 (void) printf(gettext("resilver (%s) canceled on %s"),
8115 vdev_name
, ctime(&end
));
8117 } else if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
8118 (void) printf(gettext("resilver (%s) in progress since %s"),
8119 vdev_name
, ctime(&start
));
8122 assert(vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
);
8124 (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf
,
8126 if (scan_rate
> 0) {
8127 zfs_nicebytes(scan_rate
, scan_rate_buf
, sizeof (scan_rate_buf
));
8128 (void) printf(gettext(" at %s/s"), scan_rate_buf
);
8130 (void) printf(gettext(", %s / %s issued"), bytes_issued_buf
,
8132 if (issue_rate
> 0) {
8133 zfs_nicebytes(issue_rate
, issue_rate_buf
,
8134 sizeof (issue_rate_buf
));
8135 (void) printf(gettext(" at %s/s"), issue_rate_buf
);
8137 (void) printf(gettext("\n"));
8139 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
8140 bytes_rebuilt_buf
, scan_pct
);
8142 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
8143 if (bytes_est_s
>= bytes_scanned
&&
8144 scan_rate
>= 10 * 1024 * 1024) {
8145 secs_to_dhms((bytes_est_s
- bytes_scanned
) / scan_rate
,
8147 (void) printf(gettext(", %s to go\n"), time_buf
);
8149 (void) printf(gettext(", no estimated "
8150 "completion time\n"));
8153 (void) printf(gettext("\n"));
8158 * Print rebuild status for top-level vdevs.
8161 print_rebuild_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
8166 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
8167 &child
, &children
) != 0)
8170 for (uint_t c
= 0; c
< children
; c
++) {
8171 vdev_rebuild_stat_t
*vrs
;
8174 if (nvlist_lookup_uint64_array(child
[c
],
8175 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
8176 char *name
= zpool_vdev_name(g_zfs
, zhp
,
8177 child
[c
], VDEV_NAME_TYPE_ID
);
8178 print_rebuild_status_impl(vrs
, i
, name
);
8185 * As we don't scrub checkpointed blocks, we want to warn the user that we
8186 * skipped scanning some blocks if a checkpoint exists or existed at any
8187 * time during the scan. If a sequential instead of healing reconstruction
8188 * was performed then the blocks were reconstructed. However, their checksums
8189 * have not been verified so we still print the warning.
8192 print_checkpoint_scan_warning(pool_scan_stat_t
*ps
, pool_checkpoint_stat_t
*pcs
)
8194 if (ps
== NULL
|| pcs
== NULL
)
8197 if (pcs
->pcs_state
== CS_NONE
||
8198 pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
8201 assert(pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
);
8203 if (ps
->pss_state
== DSS_NONE
)
8206 if ((ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) &&
8207 ps
->pss_end_time
< pcs
->pcs_start_time
)
8210 if (ps
->pss_state
== DSS_FINISHED
|| ps
->pss_state
== DSS_CANCELED
) {
8211 (void) printf(gettext(" scan warning: skipped blocks "
8212 "that are only referenced by the checkpoint.\n"));
8214 assert(ps
->pss_state
== DSS_SCANNING
);
8215 (void) printf(gettext(" scan warning: skipping blocks "
8216 "that are only referenced by the checkpoint.\n"));
8221 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
8222 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
8223 * the last completed (or cancelled) rebuild.
8226 check_rebuilding(nvlist_t
*nvroot
, uint64_t *rebuild_end_time
)
8230 boolean_t rebuilding
= B_FALSE
;
8231 uint64_t end_time
= 0;
8233 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
8234 &child
, &children
) != 0)
8237 for (uint_t c
= 0; c
< children
; c
++) {
8238 vdev_rebuild_stat_t
*vrs
;
8241 if (nvlist_lookup_uint64_array(child
[c
],
8242 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
) == 0) {
8244 if (vrs
->vrs_end_time
> end_time
)
8245 end_time
= vrs
->vrs_end_time
;
8247 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
8248 rebuilding
= B_TRUE
;
8255 if (rebuild_end_time
!= NULL
)
8256 *rebuild_end_time
= end_time
;
8258 return (rebuilding
);
8262 * Print the scan status.
8265 print_scan_status(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
8267 uint64_t rebuild_end_time
= 0, resilver_end_time
= 0;
8268 boolean_t have_resilver
= B_FALSE
, have_scrub
= B_FALSE
;
8269 boolean_t have_errorscrub
= B_FALSE
;
8270 boolean_t active_resilver
= B_FALSE
;
8271 pool_checkpoint_stat_t
*pcs
= NULL
;
8272 pool_scan_stat_t
*ps
= NULL
;
8274 time_t scrub_start
= 0, errorscrub_start
= 0;
8276 if (nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
8277 (uint64_t **)&ps
, &c
) == 0) {
8278 if (ps
->pss_func
== POOL_SCAN_RESILVER
) {
8279 resilver_end_time
= ps
->pss_end_time
;
8280 active_resilver
= (ps
->pss_state
== DSS_SCANNING
);
8283 have_resilver
= (ps
->pss_func
== POOL_SCAN_RESILVER
);
8284 have_scrub
= (ps
->pss_func
== POOL_SCAN_SCRUB
);
8285 scrub_start
= ps
->pss_start_time
;
8286 if (c
> offsetof(pool_scan_stat_t
,
8287 pss_pass_error_scrub_pause
) / 8) {
8288 have_errorscrub
= (ps
->pss_error_scrub_func
==
8289 POOL_SCAN_ERRORSCRUB
);
8290 errorscrub_start
= ps
->pss_error_scrub_start
;
8294 boolean_t active_rebuild
= check_rebuilding(nvroot
, &rebuild_end_time
);
8295 boolean_t have_rebuild
= (active_rebuild
|| (rebuild_end_time
> 0));
8297 /* Always print the scrub status when available. */
8298 if (have_scrub
&& scrub_start
> errorscrub_start
)
8299 print_scan_scrub_resilver_status(ps
);
8300 else if (have_errorscrub
&& errorscrub_start
>= scrub_start
)
8301 print_err_scrub_status(ps
);
8304 * When there is an active resilver or rebuild print its status.
8305 * Otherwise print the status of the last resilver or rebuild.
8307 if (active_resilver
|| (!active_rebuild
&& have_resilver
&&
8308 resilver_end_time
&& resilver_end_time
> rebuild_end_time
)) {
8309 print_scan_scrub_resilver_status(ps
);
8310 } else if (active_rebuild
|| (!active_resilver
&& have_rebuild
&&
8311 rebuild_end_time
&& rebuild_end_time
> resilver_end_time
)) {
8312 print_rebuild_status(zhp
, nvroot
);
8315 (void) nvlist_lookup_uint64_array(nvroot
,
8316 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
8317 print_checkpoint_scan_warning(ps
, pcs
);
8321 * Print out detailed removal status.
8324 print_removal_status(zpool_handle_t
*zhp
, pool_removal_stat_t
*prs
)
8326 char copied_buf
[7], examined_buf
[7], total_buf
[7], rate_buf
[7];
8328 nvlist_t
*config
, *nvroot
;
8333 if (prs
== NULL
|| prs
->prs_state
== DSS_NONE
)
8337 * Determine name of vdev.
8339 config
= zpool_get_config(zhp
, NULL
);
8340 nvroot
= fnvlist_lookup_nvlist(config
,
8341 ZPOOL_CONFIG_VDEV_TREE
);
8342 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
8343 &child
, &children
) == 0);
8344 assert(prs
->prs_removing_vdev
< children
);
8345 vdev_name
= zpool_vdev_name(g_zfs
, zhp
,
8346 child
[prs
->prs_removing_vdev
], B_TRUE
);
8348 printf_color(ANSI_BOLD
, gettext("remove: "));
8350 start
= prs
->prs_start_time
;
8351 end
= prs
->prs_end_time
;
8352 zfs_nicenum(prs
->prs_copied
, copied_buf
, sizeof (copied_buf
));
8355 * Removal is finished or canceled.
8357 if (prs
->prs_state
== DSS_FINISHED
) {
8358 uint64_t minutes_taken
= (end
- start
) / 60;
8360 (void) printf(gettext("Removal of vdev %llu copied %s "
8361 "in %lluh%um, completed on %s"),
8362 (longlong_t
)prs
->prs_removing_vdev
,
8364 (u_longlong_t
)(minutes_taken
/ 60),
8365 (uint_t
)(minutes_taken
% 60),
8366 ctime((time_t *)&end
));
8367 } else if (prs
->prs_state
== DSS_CANCELED
) {
8368 (void) printf(gettext("Removal of %s canceled on %s"),
8369 vdev_name
, ctime(&end
));
8371 uint64_t copied
, total
, elapsed
, mins_left
, hours_left
;
8372 double fraction_done
;
8375 assert(prs
->prs_state
== DSS_SCANNING
);
8378 * Removal is in progress.
8380 (void) printf(gettext(
8381 "Evacuation of %s in progress since %s"),
8382 vdev_name
, ctime(&start
));
8384 copied
= prs
->prs_copied
> 0 ? prs
->prs_copied
: 1;
8385 total
= prs
->prs_to_copy
;
8386 fraction_done
= (double)copied
/ total
;
8388 /* elapsed time for this pass */
8389 elapsed
= time(NULL
) - prs
->prs_start_time
;
8390 elapsed
= elapsed
> 0 ? elapsed
: 1;
8391 rate
= copied
/ elapsed
;
8392 rate
= rate
> 0 ? rate
: 1;
8393 mins_left
= ((total
- copied
) / rate
) / 60;
8394 hours_left
= mins_left
/ 60;
8396 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
8397 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
8398 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
8401 * do not print estimated time if hours_left is more than
8404 (void) printf(gettext(
8405 "\t%s copied out of %s at %s/s, %.2f%% done"),
8406 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
8407 if (hours_left
< (30 * 24)) {
8408 (void) printf(gettext(", %lluh%um to go\n"),
8409 (u_longlong_t
)hours_left
, (uint_t
)(mins_left
% 60));
8411 (void) printf(gettext(
8412 ", (copy is slow, no estimated time)\n"));
8417 if (prs
->prs_mapping_memory
> 0) {
8419 zfs_nicenum(prs
->prs_mapping_memory
, mem_buf
, sizeof (mem_buf
));
8420 (void) printf(gettext(
8421 "\t%s memory used for removed device mappings\n"),
8427 * Print out detailed raidz expansion status.
8430 print_raidz_expand_status(zpool_handle_t
*zhp
, pool_raidz_expand_stat_t
*pres
)
8434 if (pres
== NULL
|| pres
->pres_state
== DSS_NONE
)
8438 * Determine name of vdev.
8440 nvlist_t
*config
= zpool_get_config(zhp
, NULL
);
8441 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(config
,
8442 ZPOOL_CONFIG_VDEV_TREE
);
8445 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
8446 &child
, &children
) == 0);
8447 assert(pres
->pres_expanding_vdev
< children
);
8449 printf_color(ANSI_BOLD
, gettext("expand: "));
8451 time_t start
= pres
->pres_start_time
;
8452 time_t end
= pres
->pres_end_time
;
8454 zpool_vdev_name(g_zfs
, zhp
, child
[pres
->pres_expanding_vdev
], 0);
8455 zfs_nicenum(pres
->pres_reflowed
, copied_buf
, sizeof (copied_buf
));
8458 * Expansion is finished or canceled.
8460 if (pres
->pres_state
== DSS_FINISHED
) {
8462 secs_to_dhms(end
- start
, time_buf
);
8464 (void) printf(gettext("expanded %s-%u copied %s in %s, "
8465 "on %s"), vname
, (int)pres
->pres_expanding_vdev
,
8466 copied_buf
, time_buf
, ctime((time_t *)&end
));
8468 char examined_buf
[7], total_buf
[7], rate_buf
[7];
8469 uint64_t copied
, total
, elapsed
, secs_left
;
8470 double fraction_done
;
8473 assert(pres
->pres_state
== DSS_SCANNING
);
8476 * Expansion is in progress.
8478 (void) printf(gettext(
8479 "expansion of %s-%u in progress since %s"),
8480 vname
, (int)pres
->pres_expanding_vdev
, ctime(&start
));
8482 copied
= pres
->pres_reflowed
> 0 ? pres
->pres_reflowed
: 1;
8483 total
= pres
->pres_to_reflow
;
8484 fraction_done
= (double)copied
/ total
;
8486 /* elapsed time for this pass */
8487 elapsed
= time(NULL
) - pres
->pres_start_time
;
8488 elapsed
= elapsed
> 0 ? elapsed
: 1;
8489 rate
= copied
/ elapsed
;
8490 rate
= rate
> 0 ? rate
: 1;
8491 secs_left
= (total
- copied
) / rate
;
8493 zfs_nicenum(copied
, examined_buf
, sizeof (examined_buf
));
8494 zfs_nicenum(total
, total_buf
, sizeof (total_buf
));
8495 zfs_nicenum(rate
, rate_buf
, sizeof (rate_buf
));
8498 * do not print estimated time if hours_left is more than
8501 (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"),
8502 examined_buf
, total_buf
, rate_buf
, 100 * fraction_done
);
8503 if (pres
->pres_waiting_for_resilver
) {
8504 (void) printf(gettext(", paused for resilver or "
8506 } else if (secs_left
< (30 * 24 * 3600)) {
8508 secs_to_dhms(secs_left
, time_buf
);
8509 (void) printf(gettext(", %s to go\n"), time_buf
);
8511 (void) printf(gettext(
8512 ", (copy is slow, no estimated time)\n"));
8518 print_checkpoint_status(pool_checkpoint_stat_t
*pcs
)
8523 if (pcs
== NULL
|| pcs
->pcs_state
== CS_NONE
)
8526 (void) printf(gettext("checkpoint: "));
8528 start
= pcs
->pcs_start_time
;
8529 zfs_nicenum(pcs
->pcs_space
, space_buf
, sizeof (space_buf
));
8531 if (pcs
->pcs_state
== CS_CHECKPOINT_EXISTS
) {
8532 char *date
= ctime(&start
);
8535 * ctime() adds a newline at the end of the generated
8536 * string, thus the weird format specifier and the
8537 * strlen() call used to chop it off from the output.
8539 (void) printf(gettext("created %.*s, consumes %s\n"),
8540 (int)(strlen(date
) - 1), date
, space_buf
);
8544 assert(pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
);
8546 (void) printf(gettext("discarding, %s remaining.\n"),
8551 print_error_log(zpool_handle_t
*zhp
)
8553 nvlist_t
*nverrlist
= NULL
;
8556 size_t len
= MAXPATHLEN
* 2;
8558 if (zpool_get_errlog(zhp
, &nverrlist
) != 0)
8561 (void) printf("errors: Permanent errors have been "
8562 "detected in the following files:\n\n");
8564 pathname
= safe_malloc(len
);
8566 while ((elem
= nvlist_next_nvpair(nverrlist
, elem
)) != NULL
) {
8568 uint64_t dsobj
, obj
;
8570 verify(nvpair_value_nvlist(elem
, &nv
) == 0);
8571 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_DATASET
,
8573 verify(nvlist_lookup_uint64(nv
, ZPOOL_ERR_OBJECT
,
8575 zpool_obj_to_path(zhp
, dsobj
, obj
, pathname
, len
);
8576 (void) printf("%7s %s\n", "", pathname
);
8579 nvlist_free(nverrlist
);
8583 print_spares(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**spares
,
8592 (void) printf(gettext("\tspares\n"));
8594 for (i
= 0; i
< nspares
; i
++) {
8595 name
= zpool_vdev_name(g_zfs
, zhp
, spares
[i
],
8597 print_status_config(zhp
, cb
, name
, spares
[i
], 2, B_TRUE
, NULL
);
8603 print_l2cache(zpool_handle_t
*zhp
, status_cbdata_t
*cb
, nvlist_t
**l2cache
,
8612 (void) printf(gettext("\tcache\n"));
8614 for (i
= 0; i
< nl2cache
; i
++) {
8615 name
= zpool_vdev_name(g_zfs
, zhp
, l2cache
[i
],
8617 print_status_config(zhp
, cb
, name
, l2cache
[i
], 2,
8624 print_dedup_stats(nvlist_t
*config
)
8626 ddt_histogram_t
*ddh
;
8630 char dspace
[6], mspace
[6];
8633 * If the pool was faulted then we may not have been able to
8634 * obtain the config. Otherwise, if we have anything in the dedup
8635 * table continue processing the stats.
8637 if (nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_OBJ_STATS
,
8638 (uint64_t **)&ddo
, &c
) != 0)
8641 (void) printf("\n");
8642 (void) printf(gettext(" dedup: "));
8643 if (ddo
->ddo_count
== 0) {
8644 (void) printf(gettext("no DDT entries\n"));
8648 zfs_nicebytes(ddo
->ddo_dspace
, dspace
, sizeof (dspace
));
8649 zfs_nicebytes(ddo
->ddo_mspace
, mspace
, sizeof (mspace
));
8650 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8651 (u_longlong_t
)ddo
->ddo_count
,
8655 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_STATS
,
8656 (uint64_t **)&dds
, &c
) == 0);
8657 verify(nvlist_lookup_uint64_array(config
, ZPOOL_CONFIG_DDT_HISTOGRAM
,
8658 (uint64_t **)&ddh
, &c
) == 0);
8659 zpool_dump_ddt(dds
, ddh
);
8663 * Display a summary of pool status. Displays a summary such as:
8667 * reason: One or more devices ...
8668 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8674 * When given the '-v' option, we print out the complete config. If the '-e'
8675 * option is specified, then we print out error rate information as well.
8678 status_callback(zpool_handle_t
*zhp
, void *data
)
8680 status_cbdata_t
*cbp
= data
;
8681 nvlist_t
*config
, *nvroot
;
8683 zpool_status_t reason
;
8684 zpool_errata_t errata
;
8689 config
= zpool_get_config(zhp
, NULL
);
8690 reason
= zpool_get_status(zhp
, &msgid
, &errata
);
8695 * If we were given 'zpool status -x', only report those pools with
8698 if (cbp
->cb_explain
&&
8699 (reason
== ZPOOL_STATUS_OK
||
8700 reason
== ZPOOL_STATUS_VERSION_OLDER
||
8701 reason
== ZPOOL_STATUS_FEAT_DISABLED
||
8702 reason
== ZPOOL_STATUS_COMPATIBILITY_ERR
||
8703 reason
== ZPOOL_STATUS_INCOMPATIBLE_FEAT
)) {
8704 if (!cbp
->cb_allpools
) {
8705 (void) printf(gettext("pool '%s' is healthy\n"),
8706 zpool_get_name(zhp
));
8708 cbp
->cb_first
= B_FALSE
;
8714 cbp
->cb_first
= B_FALSE
;
8716 (void) printf("\n");
8718 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
8719 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
8720 (uint64_t **)&vs
, &c
) == 0);
8722 health
= zpool_get_state_str(zhp
);
8725 printf_color(ANSI_BOLD
, gettext("pool:"));
8726 printf(" %s\n", zpool_get_name(zhp
));
8728 printf_color(ANSI_BOLD
, gettext("state: "));
8730 printf_color(health_str_to_color(health
), "%s", health
);
8732 fputc('\n', stdout
);
8735 case ZPOOL_STATUS_MISSING_DEV_R
:
8736 printf_color(ANSI_BOLD
, gettext("status: "));
8737 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8738 "not be opened. Sufficient replicas exist for\n\tthe pool "
8739 "to continue functioning in a degraded state.\n"));
8740 printf_color(ANSI_BOLD
, gettext("action: "));
8741 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8742 "and online it using 'zpool online'.\n"));
8745 case ZPOOL_STATUS_MISSING_DEV_NR
:
8746 printf_color(ANSI_BOLD
, gettext("status: "));
8747 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8748 "not be opened. There are insufficient\n\treplicas for the"
8749 " pool to continue functioning.\n"));
8750 printf_color(ANSI_BOLD
, gettext("action: "));
8751 printf_color(ANSI_YELLOW
, gettext("Attach the missing device "
8752 "and online it using 'zpool online'.\n"));
8755 case ZPOOL_STATUS_CORRUPT_LABEL_R
:
8756 printf_color(ANSI_BOLD
, gettext("status: "));
8757 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8758 "not be used because the label is missing or\n\tinvalid. "
8759 "Sufficient replicas exist for the pool to continue\n\t"
8760 "functioning in a degraded state.\n"));
8761 printf_color(ANSI_BOLD
, gettext("action: "));
8762 printf_color(ANSI_YELLOW
, gettext("Replace the device using "
8763 "'zpool replace'.\n"));
8766 case ZPOOL_STATUS_CORRUPT_LABEL_NR
:
8767 printf_color(ANSI_BOLD
, gettext("status: "));
8768 printf_color(ANSI_YELLOW
, gettext("One or more devices could "
8769 "not be used because the label is missing \n\tor invalid. "
8770 "There are insufficient replicas for the pool to "
8771 "continue\n\tfunctioning.\n"));
8772 zpool_explain_recover(zpool_get_handle(zhp
),
8773 zpool_get_name(zhp
), reason
, config
);
8776 case ZPOOL_STATUS_FAILING_DEV
:
8777 printf_color(ANSI_BOLD
, gettext("status: "));
8778 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8779 "experienced an unrecoverable error. An\n\tattempt was "
8780 "made to correct the error. Applications are "
8782 printf_color(ANSI_BOLD
, gettext("action: "));
8783 printf_color(ANSI_YELLOW
, gettext("Determine if the "
8784 "device needs to be replaced, and clear the errors\n\tusing"
8785 " 'zpool clear' or replace the device with 'zpool "
8789 case ZPOOL_STATUS_OFFLINE_DEV
:
8790 printf_color(ANSI_BOLD
, gettext("status: "));
8791 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8792 "been taken offline by the administrator.\n\tSufficient "
8793 "replicas exist for the pool to continue functioning in "
8794 "a\n\tdegraded state.\n"));
8795 printf_color(ANSI_BOLD
, gettext("action: "));
8796 printf_color(ANSI_YELLOW
, gettext("Online the device "
8797 "using 'zpool online' or replace the device with\n\t'zpool "
8801 case ZPOOL_STATUS_REMOVED_DEV
:
8802 printf_color(ANSI_BOLD
, gettext("status: "));
8803 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8804 "been removed by the administrator.\n\tSufficient "
8805 "replicas exist for the pool to continue functioning in "
8806 "a\n\tdegraded state.\n"));
8807 printf_color(ANSI_BOLD
, gettext("action: "));
8808 printf_color(ANSI_YELLOW
, gettext("Online the device "
8809 "using zpool online' or replace the device with\n\t'zpool "
8813 case ZPOOL_STATUS_RESILVERING
:
8814 case ZPOOL_STATUS_REBUILDING
:
8815 printf_color(ANSI_BOLD
, gettext("status: "));
8816 printf_color(ANSI_YELLOW
, gettext("One or more devices is "
8817 "currently being resilvered. The pool will\n\tcontinue "
8818 "to function, possibly in a degraded state.\n"));
8819 printf_color(ANSI_BOLD
, gettext("action: "));
8820 printf_color(ANSI_YELLOW
, gettext("Wait for the resilver to "
8824 case ZPOOL_STATUS_REBUILD_SCRUB
:
8825 printf_color(ANSI_BOLD
, gettext("status: "));
8826 printf_color(ANSI_YELLOW
, gettext("One or more devices have "
8827 "been sequentially resilvered, scrubbing\n\tthe pool "
8828 "is recommended.\n"));
8829 printf_color(ANSI_BOLD
, gettext("action: "));
8830 printf_color(ANSI_YELLOW
, gettext("Use 'zpool scrub' to "
8831 "verify all data checksums.\n"));
8834 case ZPOOL_STATUS_CORRUPT_DATA
:
8835 printf_color(ANSI_BOLD
, gettext("status: "));
8836 printf_color(ANSI_YELLOW
, gettext("One or more devices has "
8837 "experienced an error resulting in data\n\tcorruption. "
8838 "Applications may be affected.\n"));
8839 printf_color(ANSI_BOLD
, gettext("action: "));
8840 printf_color(ANSI_YELLOW
, gettext("Restore the file in question"
8841 " if possible. Otherwise restore the\n\tentire pool from "
8845 case ZPOOL_STATUS_CORRUPT_POOL
:
8846 printf_color(ANSI_BOLD
, gettext("status: "));
8847 printf_color(ANSI_YELLOW
, gettext("The pool metadata is "
8848 "corrupted and the pool cannot be opened.\n"));
8849 zpool_explain_recover(zpool_get_handle(zhp
),
8850 zpool_get_name(zhp
), reason
, config
);
8853 case ZPOOL_STATUS_VERSION_OLDER
:
8854 printf_color(ANSI_BOLD
, gettext("status: "));
8855 printf_color(ANSI_YELLOW
, gettext("The pool is formatted using "
8856 "a legacy on-disk format. The pool can\n\tstill be used, "
8857 "but some features are unavailable.\n"));
8858 printf_color(ANSI_BOLD
, gettext("action: "));
8859 printf_color(ANSI_YELLOW
, gettext("Upgrade the pool using "
8860 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8861 "longer be accessible on software that does not support\n\t"
8862 "feature flags.\n"));
8865 case ZPOOL_STATUS_VERSION_NEWER
:
8866 printf_color(ANSI_BOLD
, gettext("status: "));
8867 printf_color(ANSI_YELLOW
, gettext("The pool has been upgraded "
8868 "to a newer, incompatible on-disk version.\n\tThe pool "
8869 "cannot be accessed on this system.\n"));
8870 printf_color(ANSI_BOLD
, gettext("action: "));
8871 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8872 "system running more recent software, or\n\trestore the "
8873 "pool from backup.\n"));
8876 case ZPOOL_STATUS_FEAT_DISABLED
:
8877 printf_color(ANSI_BOLD
, gettext("status: "));
8878 printf_color(ANSI_YELLOW
, gettext("Some supported and "
8879 "requested features are not enabled on the pool.\n\t"
8880 "The pool can still be used, but some features are "
8882 printf_color(ANSI_BOLD
, gettext("action: "));
8883 printf_color(ANSI_YELLOW
, gettext("Enable all features using "
8884 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8885 "longer be accessible by software that does not support\n\t"
8886 "the features. See zpool-features(7) for details.\n"));
8889 case ZPOOL_STATUS_COMPATIBILITY_ERR
:
8890 printf_color(ANSI_BOLD
, gettext("status: "));
8891 printf_color(ANSI_YELLOW
, gettext("This pool has a "
8892 "compatibility list specified, but it could not be\n\t"
8893 "read/parsed at this time. The pool can still be used, "
8894 "but this\n\tshould be investigated.\n"));
8895 printf_color(ANSI_BOLD
, gettext("action: "));
8896 printf_color(ANSI_YELLOW
, gettext("Check the value of the "
8897 "'compatibility' property against the\n\t"
8898 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D
" or "
8899 ZPOOL_DATA_COMPAT_D
".\n"));
8902 case ZPOOL_STATUS_INCOMPATIBLE_FEAT
:
8903 printf_color(ANSI_BOLD
, gettext("status: "));
8904 printf_color(ANSI_YELLOW
, gettext("One or more features "
8905 "are enabled on the pool despite not being\n\t"
8906 "requested by the 'compatibility' property.\n"));
8907 printf_color(ANSI_BOLD
, gettext("action: "));
8908 printf_color(ANSI_YELLOW
, gettext("Consider setting "
8909 "'compatibility' to an appropriate value, or\n\t"
8910 "adding needed features to the relevant file in\n\t"
8911 ZPOOL_SYSCONF_COMPAT_D
" or " ZPOOL_DATA_COMPAT_D
".\n"));
8914 case ZPOOL_STATUS_UNSUP_FEAT_READ
:
8915 printf_color(ANSI_BOLD
, gettext("status: "));
8916 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8917 "on this system because it uses the\n\tfollowing feature(s)"
8918 " not supported on this system:\n"));
8919 zpool_print_unsup_feat(config
);
8920 (void) printf("\n");
8921 printf_color(ANSI_BOLD
, gettext("action: "));
8922 printf_color(ANSI_YELLOW
, gettext("Access the pool from a "
8923 "system that supports the required feature(s),\n\tor "
8924 "restore the pool from backup.\n"));
8927 case ZPOOL_STATUS_UNSUP_FEAT_WRITE
:
8928 printf_color(ANSI_BOLD
, gettext("status: "));
8929 printf_color(ANSI_YELLOW
, gettext("The pool can only be "
8930 "accessed in read-only mode on this system. It\n\tcannot be"
8931 " accessed in read-write mode because it uses the "
8932 "following\n\tfeature(s) not supported on this system:\n"));
8933 zpool_print_unsup_feat(config
);
8934 (void) printf("\n");
8935 printf_color(ANSI_BOLD
, gettext("action: "));
8936 printf_color(ANSI_YELLOW
, gettext("The pool cannot be accessed "
8937 "in read-write mode. Import the pool with\n"
8938 "\t\"-o readonly=on\", access the pool from a system that "
8939 "supports the\n\trequired feature(s), or restore the "
8940 "pool from backup.\n"));
8943 case ZPOOL_STATUS_FAULTED_DEV_R
:
8944 printf_color(ANSI_BOLD
, gettext("status: "));
8945 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8946 "faulted in response to persistent errors.\n\tSufficient "
8947 "replicas exist for the pool to continue functioning "
8948 "in a\n\tdegraded state.\n"));
8949 printf_color(ANSI_BOLD
, gettext("action: "));
8950 printf_color(ANSI_YELLOW
, gettext("Replace the faulted device, "
8951 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8954 case ZPOOL_STATUS_FAULTED_DEV_NR
:
8955 printf_color(ANSI_BOLD
, gettext("status: "));
8956 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8957 "faulted in response to persistent errors. There are "
8958 "insufficient replicas for the pool to\n\tcontinue "
8960 printf_color(ANSI_BOLD
, gettext("action: "));
8961 printf_color(ANSI_YELLOW
, gettext("Destroy and re-create the "
8962 "pool from a backup source. Manually marking the device\n"
8963 "\trepaired using 'zpool clear' may allow some data "
8964 "to be recovered.\n"));
8967 case ZPOOL_STATUS_IO_FAILURE_MMP
:
8968 printf_color(ANSI_BOLD
, gettext("status: "));
8969 printf_color(ANSI_YELLOW
, gettext("The pool is suspended "
8970 "because multihost writes failed or were delayed;\n\t"
8971 "another system could import the pool undetected.\n"));
8972 printf_color(ANSI_BOLD
, gettext("action: "));
8973 printf_color(ANSI_YELLOW
, gettext("Make sure the pool's devices"
8974 " are connected, then reboot your system and\n\timport the "
8978 case ZPOOL_STATUS_IO_FAILURE_WAIT
:
8979 case ZPOOL_STATUS_IO_FAILURE_CONTINUE
:
8980 printf_color(ANSI_BOLD
, gettext("status: "));
8981 printf_color(ANSI_YELLOW
, gettext("One or more devices are "
8982 "faulted in response to IO failures.\n"));
8983 printf_color(ANSI_BOLD
, gettext("action: "));
8984 printf_color(ANSI_YELLOW
, gettext("Make sure the affected "
8985 "devices are connected, then run 'zpool clear'.\n"));
8988 case ZPOOL_STATUS_BAD_LOG
:
8989 printf_color(ANSI_BOLD
, gettext("status: "));
8990 printf_color(ANSI_YELLOW
, gettext("An intent log record "
8991 "could not be read.\n"
8992 "\tWaiting for administrator intervention to fix the "
8993 "faulted pool.\n"));
8994 printf_color(ANSI_BOLD
, gettext("action: "));
8995 printf_color(ANSI_YELLOW
, gettext("Either restore the affected "
8996 "device(s) and run 'zpool online',\n"
8997 "\tor ignore the intent log records by running "
8998 "'zpool clear'.\n"));
9001 case ZPOOL_STATUS_NON_NATIVE_ASHIFT
:
9002 (void) printf(gettext("status: One or more devices are "
9003 "configured to use a non-native block size.\n"
9004 "\tExpect reduced performance.\n"));
9005 (void) printf(gettext("action: Replace affected devices with "
9006 "devices that support the\n\tconfigured block size, or "
9007 "migrate data to a properly configured\n\tpool.\n"));
9010 case ZPOOL_STATUS_HOSTID_MISMATCH
:
9011 printf_color(ANSI_BOLD
, gettext("status: "));
9012 printf_color(ANSI_YELLOW
, gettext("Mismatch between pool hostid"
9013 " and system hostid on imported pool.\n\tThis pool was "
9014 "previously imported into a system with a different "
9015 "hostid,\n\tand then was verbatim imported into this "
9017 printf_color(ANSI_BOLD
, gettext("action: "));
9018 printf_color(ANSI_YELLOW
, gettext("Export this pool on all "
9019 "systems on which it is imported.\n"
9020 "\tThen import it to correct the mismatch.\n"));
9023 case ZPOOL_STATUS_ERRATA
:
9024 printf_color(ANSI_BOLD
, gettext("status: "));
9025 printf_color(ANSI_YELLOW
, gettext("Errata #%d detected.\n"),
9029 case ZPOOL_ERRATA_NONE
:
9032 case ZPOOL_ERRATA_ZOL_2094_SCRUB
:
9033 printf_color(ANSI_BOLD
, gettext("action: "));
9034 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
9035 " run 'zpool scrub'.\n"));
9038 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
:
9039 (void) printf(gettext("\tExisting encrypted datasets "
9040 "contain an on-disk incompatibility\n\twhich "
9041 "needs to be corrected.\n"));
9042 printf_color(ANSI_BOLD
, gettext("action: "));
9043 printf_color(ANSI_YELLOW
, gettext("To correct the issue"
9044 " backup existing encrypted datasets to new\n\t"
9045 "encrypted datasets and destroy the old ones. "
9046 "'zfs mount -o ro' can\n\tbe used to temporarily "
9047 "mount existing encrypted datasets readonly.\n"));
9050 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
:
9051 (void) printf(gettext("\tExisting encrypted snapshots "
9052 "and bookmarks contain an on-disk\n\tincompat"
9053 "ibility. This may cause on-disk corruption if "
9054 "they are used\n\twith 'zfs recv'.\n"));
9055 printf_color(ANSI_BOLD
, gettext("action: "));
9056 printf_color(ANSI_YELLOW
, gettext("To correct the"
9057 "issue, enable the bookmark_v2 feature. No "
9058 "additional\n\taction is needed if there are no "
9059 "encrypted snapshots or bookmarks.\n\tIf preserving"
9060 "the encrypted snapshots and bookmarks is required,"
9061 " use\n\ta non-raw send to backup and restore them."
9062 " Alternately, they may be\n\tremoved to resolve "
9063 "the incompatibility.\n"));
9068 * All errata which allow the pool to be imported
9069 * must contain an action message.
9077 * The remaining errors can't actually be generated, yet.
9079 assert(reason
== ZPOOL_STATUS_OK
);
9082 if (msgid
!= NULL
) {
9084 printf_color(ANSI_BOLD
, gettext("see:"));
9086 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
9090 if (config
!= NULL
) {
9092 nvlist_t
**spares
, **l2cache
;
9093 uint_t nspares
, nl2cache
;
9095 print_scan_status(zhp
, nvroot
);
9097 pool_removal_stat_t
*prs
= NULL
;
9098 (void) nvlist_lookup_uint64_array(nvroot
,
9099 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
9100 print_removal_status(zhp
, prs
);
9102 pool_checkpoint_stat_t
*pcs
= NULL
;
9103 (void) nvlist_lookup_uint64_array(nvroot
,
9104 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
9105 print_checkpoint_status(pcs
);
9107 pool_raidz_expand_stat_t
*pres
= NULL
;
9108 (void) nvlist_lookup_uint64_array(nvroot
,
9109 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS
, (uint64_t **)&pres
, &c
);
9110 print_raidz_expand_status(zhp
, pres
);
9112 cbp
->cb_namewidth
= max_width(zhp
, nvroot
, 0, 0,
9113 cbp
->cb_name_flags
| VDEV_NAME_TYPE_ID
);
9114 if (cbp
->cb_namewidth
< 10)
9115 cbp
->cb_namewidth
= 10;
9117 color_start(ANSI_BOLD
);
9118 (void) printf(gettext("config:\n\n"));
9119 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
9120 cbp
->cb_namewidth
, "NAME", "STATE", "READ", "WRITE",
9124 if (cbp
->cb_print_slow_ios
) {
9125 printf_color(ANSI_BOLD
, " %5s", gettext("SLOW"));
9128 if (cbp
->cb_print_power
) {
9129 printf_color(ANSI_BOLD
, " %5s", gettext("POWER"));
9132 if (cbp
->vcdl
!= NULL
)
9133 print_cmd_columns(cbp
->vcdl
, 0);
9137 print_status_config(zhp
, cbp
, zpool_get_name(zhp
), nvroot
, 0,
9140 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_DEDUP
);
9141 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_BIAS_SPECIAL
);
9142 print_class_vdevs(zhp
, cbp
, nvroot
, VDEV_ALLOC_CLASS_LOGS
);
9144 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
9145 &l2cache
, &nl2cache
) == 0)
9146 print_l2cache(zhp
, cbp
, l2cache
, nl2cache
);
9148 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
9149 &spares
, &nspares
) == 0)
9150 print_spares(zhp
, cbp
, spares
, nspares
);
9152 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
9154 (void) printf("\n");
9156 (void) printf(gettext(
9157 "errors: No known data errors\n"));
9158 } else if (!cbp
->cb_verbose
) {
9159 color_start(ANSI_RED
);
9160 (void) printf(gettext("errors: %llu data "
9161 "errors, use '-v' for a list\n"),
9162 (u_longlong_t
)nerr
);
9165 print_error_log(zhp
);
9169 if (cbp
->cb_dedup_stats
)
9170 print_dedup_stats(config
);
9172 (void) printf(gettext("config: The configuration cannot be "
9180 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [--power] [-T d|u] ...
9181 * [pool] [interval [count]]
9183 * -c CMD For each vdev, run command CMD
9184 * -e Display only unhealthy vdevs
9185 * -i Display vdev initialization status.
9186 * -g Display guid for individual vdev name.
9187 * -L Follow links when resolving vdev path name.
9188 * -p Display values in parsable (exact) format.
9189 * -P Display full path for vdev name.
9190 * -s Display slow IOs column.
9191 * -v Display complete error logs
9192 * -x Display only pools with potential problems
9193 * -D Display dedup status (undocumented)
9194 * -t Display vdev TRIM status.
9195 * -T Display a timestamp in date(1) or Unix format
9196 * --power Display vdev enclosure slot power status
9198 * Describes the health status of all pools or some subset.
9201 zpool_do_status(int argc
, char **argv
)
9206 unsigned long count
= 0;
9207 status_cbdata_t cb
= { 0 };
9210 struct option long_options
[] = {
9211 {"power", no_argument
, NULL
, POWER_OPT
},
9216 while ((c
= getopt_long(argc
, argv
, "c:eigLpPsvxDtT:", long_options
,
9222 gettext("Can't set -c flag twice\n"));
9226 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL
&&
9227 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
9228 fprintf(stderr
, gettext(
9229 "Can't run -c, disabled by "
9230 "ZPOOL_SCRIPTS_ENABLED.\n"));
9234 if ((getuid() <= 0 || geteuid() <= 0) &&
9235 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
9236 fprintf(stderr
, gettext(
9237 "Can't run -c with root privileges "
9238 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
9244 cb
.cb_print_unhealthy
= B_TRUE
;
9247 cb
.cb_print_vdev_init
= B_TRUE
;
9250 cb
.cb_name_flags
|= VDEV_NAME_GUID
;
9253 cb
.cb_name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
9256 cb
.cb_literal
= B_TRUE
;
9259 cb
.cb_name_flags
|= VDEV_NAME_PATH
;
9262 cb
.cb_print_slow_ios
= B_TRUE
;
9265 cb
.cb_verbose
= B_TRUE
;
9268 cb
.cb_explain
= B_TRUE
;
9271 cb
.cb_dedup_stats
= B_TRUE
;
9274 cb
.cb_print_vdev_trim
= B_TRUE
;
9277 get_timestamp_arg(*optarg
);
9280 cb
.cb_print_power
= B_TRUE
;
9283 if (optopt
== 'c') {
9284 print_zpool_script_list("status");
9288 gettext("invalid option '%c'\n"), optopt
);
9297 get_interval_count(&argc
, argv
, &interval
, &count
);
9300 cb
.cb_allpools
= B_TRUE
;
9302 cb
.cb_first
= B_TRUE
;
9303 cb
.cb_print_status
= B_TRUE
;
9306 if (timestamp_fmt
!= NODATE
)
9307 print_timestamp(timestamp_fmt
);
9310 cb
.vcdl
= all_pools_for_each_vdev_run(argc
, argv
, cmd
,
9313 ret
= for_each_pool(argc
, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
9314 cb
.cb_literal
, status_callback
, &cb
);
9316 if (cb
.vcdl
!= NULL
)
9317 free_vdev_cmd_data_list(cb
.vcdl
);
9319 if (argc
== 0 && cb
.cb_count
== 0)
9320 (void) fprintf(stderr
, gettext("no pools available\n"));
9321 else if (cb
.cb_explain
&& cb
.cb_first
&& cb
.cb_allpools
)
9322 (void) printf(gettext("all pools are healthy\n"));
9330 if (count
!= 0 && --count
== 0)
9333 (void) fflush(stdout
);
9334 (void) fsleep(interval
);
9340 typedef struct upgrade_cbdata
{
9343 uint64_t cb_version
;
9348 check_unsupp_fs(zfs_handle_t
*zhp
, void *unsupp_fs
)
9350 int zfs_version
= (int)zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
9351 int *count
= (int *)unsupp_fs
;
9353 if (zfs_version
> ZPL_VERSION
) {
9354 (void) printf(gettext("%s (v%d) is not supported by this "
9355 "implementation of ZFS.\n"),
9356 zfs_get_name(zhp
), zfs_version
);
9360 zfs_iter_filesystems_v2(zhp
, 0, check_unsupp_fs
, unsupp_fs
);
9368 upgrade_version(zpool_handle_t
*zhp
, uint64_t version
)
9372 uint64_t oldversion
;
9375 config
= zpool_get_config(zhp
, NULL
);
9376 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9379 char compat
[ZFS_MAXPROPLEN
];
9380 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
9381 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
9384 assert(SPA_VERSION_IS_SUPPORTED(oldversion
));
9385 assert(oldversion
< version
);
9387 ret
= zfs_iter_root(zpool_get_handle(zhp
), check_unsupp_fs
, &unsupp_fs
);
9392 (void) fprintf(stderr
, gettext("Upgrade not performed due "
9393 "to %d unsupported filesystems (max v%d).\n"),
9394 unsupp_fs
, (int)ZPL_VERSION
);
9398 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
9399 (void) fprintf(stderr
, gettext("Upgrade not performed because "
9400 "'compatibility' property set to '"
9401 ZPOOL_COMPAT_LEGACY
"'.\n"));
9405 ret
= zpool_upgrade(zhp
, version
);
9409 if (version
>= SPA_VERSION_FEATURES
) {
9410 (void) printf(gettext("Successfully upgraded "
9411 "'%s' from version %llu to feature flags.\n"),
9412 zpool_get_name(zhp
), (u_longlong_t
)oldversion
);
9414 (void) printf(gettext("Successfully upgraded "
9415 "'%s' from version %llu to version %llu.\n"),
9416 zpool_get_name(zhp
), (u_longlong_t
)oldversion
,
9417 (u_longlong_t
)version
);
9424 upgrade_enable_all(zpool_handle_t
*zhp
, int *countp
)
9427 boolean_t firstff
= B_TRUE
;
9428 nvlist_t
*enabled
= zpool_get_features(zhp
);
9430 char compat
[ZFS_MAXPROPLEN
];
9431 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
, compat
,
9432 ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
9435 boolean_t requested_features
[SPA_FEATURES
];
9436 if (zpool_do_load_compat(compat
, requested_features
) !=
9437 ZPOOL_COMPATIBILITY_OK
)
9441 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9442 const char *fname
= spa_feature_table
[i
].fi_uname
;
9443 const char *fguid
= spa_feature_table
[i
].fi_guid
;
9445 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
9448 if (!nvlist_exists(enabled
, fguid
) && requested_features
[i
]) {
9450 verify(-1 != asprintf(&propname
, "feature@%s", fname
));
9451 ret
= zpool_set_prop(zhp
, propname
,
9452 ZFS_FEATURE_ENABLED
);
9460 (void) printf(gettext("Enabled the "
9461 "following features on '%s':\n"),
9462 zpool_get_name(zhp
));
9465 (void) printf(gettext(" %s\n"), fname
);
9476 upgrade_cb(zpool_handle_t
*zhp
, void *arg
)
9478 upgrade_cbdata_t
*cbp
= arg
;
9481 boolean_t modified_pool
= B_FALSE
;
9484 config
= zpool_get_config(zhp
, NULL
);
9485 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9488 assert(SPA_VERSION_IS_SUPPORTED(version
));
9490 if (version
< cbp
->cb_version
) {
9491 cbp
->cb_first
= B_FALSE
;
9492 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9495 modified_pool
= B_TRUE
;
9498 * If they did "zpool upgrade -a", then we could
9499 * be doing ioctls to different pools. We need
9500 * to log this history once to each pool, and bypass
9501 * the normal history logging that happens in main().
9503 (void) zpool_log_history(g_zfs
, history_str
);
9504 log_history
= B_FALSE
;
9507 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9509 ret
= upgrade_enable_all(zhp
, &count
);
9514 cbp
->cb_first
= B_FALSE
;
9515 modified_pool
= B_TRUE
;
9519 if (modified_pool
) {
9520 (void) printf("\n");
9521 (void) after_zpool_upgrade(zhp
);
9528 upgrade_list_older_cb(zpool_handle_t
*zhp
, void *arg
)
9530 upgrade_cbdata_t
*cbp
= arg
;
9534 config
= zpool_get_config(zhp
, NULL
);
9535 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9538 assert(SPA_VERSION_IS_SUPPORTED(version
));
9540 if (version
< SPA_VERSION_FEATURES
) {
9541 if (cbp
->cb_first
) {
9542 (void) printf(gettext("The following pools are "
9543 "formatted with legacy version numbers and can\n"
9544 "be upgraded to use feature flags. After "
9545 "being upgraded, these pools\nwill no "
9546 "longer be accessible by software that does not "
9547 "support feature\nflags.\n\n"
9548 "Note that setting a pool's 'compatibility' "
9549 "feature to '" ZPOOL_COMPAT_LEGACY
"' will\n"
9550 "inhibit upgrades.\n\n"));
9551 (void) printf(gettext("VER POOL\n"));
9552 (void) printf(gettext("--- ------------\n"));
9553 cbp
->cb_first
= B_FALSE
;
9556 (void) printf("%2llu %s\n", (u_longlong_t
)version
,
9557 zpool_get_name(zhp
));
9564 upgrade_list_disabled_cb(zpool_handle_t
*zhp
, void *arg
)
9566 upgrade_cbdata_t
*cbp
= arg
;
9570 config
= zpool_get_config(zhp
, NULL
);
9571 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
9574 if (version
>= SPA_VERSION_FEATURES
) {
9576 boolean_t poolfirst
= B_TRUE
;
9577 nvlist_t
*enabled
= zpool_get_features(zhp
);
9579 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9580 const char *fguid
= spa_feature_table
[i
].fi_guid
;
9581 const char *fname
= spa_feature_table
[i
].fi_uname
;
9583 if (!spa_feature_table
[i
].fi_zfs_mod_supported
)
9586 if (!nvlist_exists(enabled
, fguid
)) {
9587 if (cbp
->cb_first
) {
9588 (void) printf(gettext("\nSome "
9589 "supported features are not "
9590 "enabled on the following pools. "
9591 "Once a\nfeature is enabled the "
9592 "pool may become incompatible with "
9593 "software\nthat does not support "
9595 "zpool-features(7) for "
9597 "Note that the pool "
9598 "'compatibility' feature can be "
9599 "used to inhibit\nfeature "
9601 (void) printf(gettext("POOL "
9603 (void) printf(gettext("------"
9605 cbp
->cb_first
= B_FALSE
;
9609 (void) printf(gettext("%s\n"),
9610 zpool_get_name(zhp
));
9611 poolfirst
= B_FALSE
;
9614 (void) printf(gettext(" %s\n"), fname
);
9617 * If they did "zpool upgrade -a", then we could
9618 * be doing ioctls to different pools. We need
9619 * to log this history once to each pool, and bypass
9620 * the normal history logging that happens in main().
9622 (void) zpool_log_history(g_zfs
, history_str
);
9623 log_history
= B_FALSE
;
9631 upgrade_one(zpool_handle_t
*zhp
, void *data
)
9633 boolean_t modified_pool
= B_FALSE
;
9634 upgrade_cbdata_t
*cbp
= data
;
9635 uint64_t cur_version
;
9638 if (strcmp("log", zpool_get_name(zhp
)) == 0) {
9639 (void) fprintf(stderr
, gettext("'log' is now a reserved word\n"
9640 "Pool 'log' must be renamed using export and import"
9645 cur_version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
9646 if (cur_version
> cbp
->cb_version
) {
9647 (void) printf(gettext("Pool '%s' is already formatted "
9648 "using more current version '%llu'.\n\n"),
9649 zpool_get_name(zhp
), (u_longlong_t
)cur_version
);
9653 if (cbp
->cb_version
!= SPA_VERSION
&& cur_version
== cbp
->cb_version
) {
9654 (void) printf(gettext("Pool '%s' is already formatted "
9655 "using version %llu.\n\n"), zpool_get_name(zhp
),
9656 (u_longlong_t
)cbp
->cb_version
);
9660 if (cur_version
!= cbp
->cb_version
) {
9661 modified_pool
= B_TRUE
;
9662 ret
= upgrade_version(zhp
, cbp
->cb_version
);
9667 if (cbp
->cb_version
>= SPA_VERSION_FEATURES
) {
9669 ret
= upgrade_enable_all(zhp
, &count
);
9674 modified_pool
= B_TRUE
;
9675 } else if (cur_version
== SPA_VERSION
) {
9676 (void) printf(gettext("Pool '%s' already has all "
9677 "supported and requested features enabled.\n"),
9678 zpool_get_name(zhp
));
9682 if (modified_pool
) {
9683 (void) printf("\n");
9684 (void) after_zpool_upgrade(zhp
);
9693 * zpool upgrade [-V version] <-a | pool ...>
9695 * With no arguments, display downrev'd ZFS pool available for upgrade.
9696 * Individual pools can be upgraded by specifying the pool, and '-a' will
9697 * upgrade all pools.
9700 zpool_do_upgrade(int argc
, char **argv
)
9703 upgrade_cbdata_t cb
= { 0 };
9705 boolean_t showversions
= B_FALSE
;
9706 boolean_t upgradeall
= B_FALSE
;
9711 while ((c
= getopt(argc
, argv
, ":avV:")) != -1) {
9714 upgradeall
= B_TRUE
;
9717 showversions
= B_TRUE
;
9720 cb
.cb_version
= strtoll(optarg
, &end
, 10);
9722 !SPA_VERSION_IS_SUPPORTED(cb
.cb_version
)) {
9723 (void) fprintf(stderr
,
9724 gettext("invalid version '%s'\n"), optarg
);
9729 (void) fprintf(stderr
, gettext("missing argument for "
9730 "'%c' option\n"), optopt
);
9734 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
9745 if (cb
.cb_version
== 0) {
9746 cb
.cb_version
= SPA_VERSION
;
9747 } else if (!upgradeall
&& argc
== 0) {
9748 (void) fprintf(stderr
, gettext("-V option is "
9749 "incompatible with other arguments\n"));
9754 if (upgradeall
|| argc
!= 0) {
9755 (void) fprintf(stderr
, gettext("-v option is "
9756 "incompatible with other arguments\n"));
9759 } else if (upgradeall
) {
9761 (void) fprintf(stderr
, gettext("-a option should not "
9762 "be used along with a pool name\n"));
9767 (void) printf("%s", gettext("This system supports ZFS pool feature "
9772 (void) printf(gettext("The following features are "
9774 (void) printf(gettext("FEAT DESCRIPTION\n"));
9775 (void) printf("----------------------------------------------"
9776 "---------------\n");
9777 for (i
= 0; i
< SPA_FEATURES
; i
++) {
9778 zfeature_info_t
*fi
= &spa_feature_table
[i
];
9779 if (!fi
->fi_zfs_mod_supported
)
9782 (fi
->fi_flags
& ZFEATURE_FLAG_READONLY_COMPAT
) ?
9783 " (read-only compatible)" : "";
9785 (void) printf("%-37s%s\n", fi
->fi_uname
, ro
);
9786 (void) printf(" %s\n", fi
->fi_desc
);
9788 (void) printf("\n");
9790 (void) printf(gettext("The following legacy versions are also "
9792 (void) printf(gettext("VER DESCRIPTION\n"));
9793 (void) printf("--- -----------------------------------------"
9794 "---------------\n");
9795 (void) printf(gettext(" 1 Initial ZFS version\n"));
9796 (void) printf(gettext(" 2 Ditto blocks "
9797 "(replicated metadata)\n"));
9798 (void) printf(gettext(" 3 Hot spares and double parity "
9800 (void) printf(gettext(" 4 zpool history\n"));
9801 (void) printf(gettext(" 5 Compression using the gzip "
9803 (void) printf(gettext(" 6 bootfs pool property\n"));
9804 (void) printf(gettext(" 7 Separate intent log devices\n"));
9805 (void) printf(gettext(" 8 Delegated administration\n"));
9806 (void) printf(gettext(" 9 refquota and refreservation "
9808 (void) printf(gettext(" 10 Cache devices\n"));
9809 (void) printf(gettext(" 11 Improved scrub performance\n"));
9810 (void) printf(gettext(" 12 Snapshot properties\n"));
9811 (void) printf(gettext(" 13 snapused property\n"));
9812 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9813 (void) printf(gettext(" 15 user/group space accounting\n"));
9814 (void) printf(gettext(" 16 stmf property support\n"));
9815 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9816 (void) printf(gettext(" 18 Snapshot user holds\n"));
9817 (void) printf(gettext(" 19 Log device removal\n"));
9818 (void) printf(gettext(" 20 Compression using zle "
9819 "(zero-length encoding)\n"));
9820 (void) printf(gettext(" 21 Deduplication\n"));
9821 (void) printf(gettext(" 22 Received properties\n"));
9822 (void) printf(gettext(" 23 Slim ZIL\n"));
9823 (void) printf(gettext(" 24 System attributes\n"));
9824 (void) printf(gettext(" 25 Improved scrub stats\n"));
9825 (void) printf(gettext(" 26 Improved snapshot deletion "
9827 (void) printf(gettext(" 27 Improved snapshot creation "
9829 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9830 (void) printf(gettext("\nFor more information on a particular "
9831 "version, including supported releases,\n"));
9832 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9833 } else if (argc
== 0 && upgradeall
) {
9834 cb
.cb_first
= B_TRUE
;
9835 ret
= zpool_iter(g_zfs
, upgrade_cb
, &cb
);
9836 if (ret
== 0 && cb
.cb_first
) {
9837 if (cb
.cb_version
== SPA_VERSION
) {
9838 (void) printf(gettext("All pools are already "
9839 "formatted using feature flags.\n\n"));
9840 (void) printf(gettext("Every feature flags "
9841 "pool already has all supported and "
9842 "requested features enabled.\n"));
9844 (void) printf(gettext("All pools are already "
9845 "formatted with version %llu or higher.\n"),
9846 (u_longlong_t
)cb
.cb_version
);
9849 } else if (argc
== 0) {
9850 cb
.cb_first
= B_TRUE
;
9851 ret
= zpool_iter(g_zfs
, upgrade_list_older_cb
, &cb
);
9855 (void) printf(gettext("All pools are formatted "
9856 "using feature flags.\n\n"));
9858 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9859 "for a list of available legacy versions.\n"));
9862 cb
.cb_first
= B_TRUE
;
9863 ret
= zpool_iter(g_zfs
, upgrade_list_disabled_cb
, &cb
);
9867 (void) printf(gettext("Every feature flags pool has "
9868 "all supported and requested features enabled.\n"));
9870 (void) printf(gettext("\n"));
9873 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
9874 B_FALSE
, upgrade_one
, &cb
);
9880 typedef struct hist_cbdata
{
9887 print_history_records(nvlist_t
*nvhis
, hist_cbdata_t
*cb
)
9893 verify(nvlist_lookup_nvlist_array(nvhis
, ZPOOL_HIST_RECORD
,
9894 &records
, &numrecords
) == 0);
9895 for (i
= 0; i
< numrecords
; i
++) {
9896 nvlist_t
*rec
= records
[i
];
9899 if (nvlist_exists(rec
, ZPOOL_HIST_TIME
)) {
9903 tsec
= fnvlist_lookup_uint64(records
[i
],
9905 (void) localtime_r(&tsec
, &t
);
9906 (void) strftime(tbuf
, sizeof (tbuf
), "%F.%T", &t
);
9909 if (nvlist_exists(rec
, ZPOOL_HIST_ELAPSED_NS
)) {
9910 uint64_t elapsed_ns
= fnvlist_lookup_int64(records
[i
],
9911 ZPOOL_HIST_ELAPSED_NS
);
9912 (void) snprintf(tbuf
+ strlen(tbuf
),
9913 sizeof (tbuf
) - strlen(tbuf
),
9914 " (%lldms)", (long long)elapsed_ns
/ 1000 / 1000);
9917 if (nvlist_exists(rec
, ZPOOL_HIST_CMD
)) {
9918 (void) printf("%s %s", tbuf
,
9919 fnvlist_lookup_string(rec
, ZPOOL_HIST_CMD
));
9920 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_EVENT
)) {
9922 fnvlist_lookup_uint64(rec
, ZPOOL_HIST_INT_EVENT
);
9925 if (ievent
>= ZFS_NUM_LEGACY_HISTORY_EVENTS
) {
9926 (void) printf("%s unrecognized record:\n",
9928 dump_nvlist(rec
, 4);
9931 (void) printf("%s [internal %s txg:%lld] %s", tbuf
,
9932 zfs_history_event_names
[ievent
],
9933 (longlong_t
)fnvlist_lookup_uint64(
9934 rec
, ZPOOL_HIST_TXG
),
9935 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_STR
));
9936 } else if (nvlist_exists(rec
, ZPOOL_HIST_INT_NAME
)) {
9939 (void) printf("%s [txg:%lld] %s", tbuf
,
9940 (longlong_t
)fnvlist_lookup_uint64(
9941 rec
, ZPOOL_HIST_TXG
),
9942 fnvlist_lookup_string(rec
, ZPOOL_HIST_INT_NAME
));
9943 if (nvlist_exists(rec
, ZPOOL_HIST_DSNAME
)) {
9944 (void) printf(" %s (%llu)",
9945 fnvlist_lookup_string(rec
,
9947 (u_longlong_t
)fnvlist_lookup_uint64(rec
,
9950 (void) printf(" %s", fnvlist_lookup_string(rec
,
9951 ZPOOL_HIST_INT_STR
));
9952 } else if (nvlist_exists(rec
, ZPOOL_HIST_IOCTL
)) {
9955 (void) printf("%s ioctl %s\n", tbuf
,
9956 fnvlist_lookup_string(rec
, ZPOOL_HIST_IOCTL
));
9957 if (nvlist_exists(rec
, ZPOOL_HIST_INPUT_NVL
)) {
9958 (void) printf(" input:\n");
9959 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9960 ZPOOL_HIST_INPUT_NVL
), 8);
9962 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_NVL
)) {
9963 (void) printf(" output:\n");
9964 dump_nvlist(fnvlist_lookup_nvlist(rec
,
9965 ZPOOL_HIST_OUTPUT_NVL
), 8);
9967 if (nvlist_exists(rec
, ZPOOL_HIST_OUTPUT_SIZE
)) {
9968 (void) printf(" output nvlist omitted; "
9969 "original size: %lldKB\n",
9970 (longlong_t
)fnvlist_lookup_int64(rec
,
9971 ZPOOL_HIST_OUTPUT_SIZE
) / 1024);
9973 if (nvlist_exists(rec
, ZPOOL_HIST_ERRNO
)) {
9974 (void) printf(" errno: %lld\n",
9975 (longlong_t
)fnvlist_lookup_int64(rec
,
9981 (void) printf("%s unrecognized record:\n", tbuf
);
9982 dump_nvlist(rec
, 4);
9986 (void) printf("\n");
9989 (void) printf(" [");
9990 if (nvlist_exists(rec
, ZPOOL_HIST_WHO
)) {
9991 uid_t who
= fnvlist_lookup_uint64(rec
, ZPOOL_HIST_WHO
);
9992 struct passwd
*pwd
= getpwuid(who
);
9993 (void) printf("user %d ", (int)who
);
9995 (void) printf("(%s) ", pwd
->pw_name
);
9997 if (nvlist_exists(rec
, ZPOOL_HIST_HOST
)) {
9998 (void) printf("on %s",
9999 fnvlist_lookup_string(rec
, ZPOOL_HIST_HOST
));
10001 if (nvlist_exists(rec
, ZPOOL_HIST_ZONE
)) {
10002 (void) printf(":%s",
10003 fnvlist_lookup_string(rec
, ZPOOL_HIST_ZONE
));
10006 (void) printf("]");
10007 (void) printf("\n");
10012 * Print out the command history for a specific pool.
10015 get_history_one(zpool_handle_t
*zhp
, void *data
)
10019 hist_cbdata_t
*cb
= (hist_cbdata_t
*)data
;
10021 boolean_t eof
= B_FALSE
;
10023 cb
->first
= B_FALSE
;
10025 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp
));
10028 if ((ret
= zpool_get_history(zhp
, &nvhis
, &off
, &eof
)) != 0)
10031 print_history_records(nvhis
, cb
);
10032 nvlist_free(nvhis
);
10034 (void) printf("\n");
10040 * zpool history <pool>
10042 * Displays the history of commands that modified pools.
10045 zpool_do_history(int argc
, char **argv
)
10047 hist_cbdata_t cbdata
= { 0 };
10051 cbdata
.first
= B_TRUE
;
10052 /* check options */
10053 while ((c
= getopt(argc
, argv
, "li")) != -1) {
10056 cbdata
.longfmt
= B_TRUE
;
10059 cbdata
.internal
= B_TRUE
;
10062 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10070 ret
= for_each_pool(argc
, argv
, B_FALSE
, NULL
, ZFS_TYPE_POOL
,
10071 B_FALSE
, get_history_one
, &cbdata
);
10073 if (argc
== 0 && cbdata
.first
== B_TRUE
) {
10074 (void) fprintf(stderr
, gettext("no pools available\n"));
10081 typedef struct ev_opts
{
10086 char poolname
[ZFS_MAX_DATASET_NAME_LEN
];
10090 zpool_do_events_short(nvlist_t
*nvl
, ev_opts_t
*opts
)
10092 char ctime_str
[26], str
[32];
10097 verify(nvlist_lookup_int64_array(nvl
, FM_EREPORT_TIME
, &tv
, &n
) == 0);
10098 memset(str
, ' ', 32);
10099 (void) ctime_r((const time_t *)&tv
[0], ctime_str
);
10100 (void) memcpy(str
, ctime_str
+4, 6); /* 'Jun 30' */
10101 (void) memcpy(str
+7, ctime_str
+20, 4); /* '1993' */
10102 (void) memcpy(str
+12, ctime_str
+11, 8); /* '21:49:08' */
10103 (void) sprintf(str
+20, ".%09lld", (longlong_t
)tv
[1]); /* '.123456789' */
10104 if (opts
->scripted
)
10105 (void) printf(gettext("%s\t"), str
);
10107 (void) printf(gettext("%s "), str
);
10109 verify(nvlist_lookup_string(nvl
, FM_CLASS
, &ptr
) == 0);
10110 (void) printf(gettext("%s\n"), ptr
);
10114 zpool_do_events_nvprint(nvlist_t
*nvl
, int depth
)
10118 for (nvp
= nvlist_next_nvpair(nvl
, NULL
);
10119 nvp
!= NULL
; nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
10121 data_type_t type
= nvpair_type(nvp
);
10122 const char *name
= nvpair_name(nvp
);
10132 printf(gettext("%*s%s = "), depth
, "", name
);
10135 case DATA_TYPE_BOOLEAN
:
10136 printf(gettext("%s"), "1");
10139 case DATA_TYPE_BOOLEAN_VALUE
:
10140 (void) nvpair_value_boolean_value(nvp
, &b
);
10141 printf(gettext("%s"), b
? "1" : "0");
10144 case DATA_TYPE_BYTE
:
10145 (void) nvpair_value_byte(nvp
, &i8
);
10146 printf(gettext("0x%x"), i8
);
10149 case DATA_TYPE_INT8
:
10150 (void) nvpair_value_int8(nvp
, (void *)&i8
);
10151 printf(gettext("0x%x"), i8
);
10154 case DATA_TYPE_UINT8
:
10155 (void) nvpair_value_uint8(nvp
, &i8
);
10156 printf(gettext("0x%x"), i8
);
10159 case DATA_TYPE_INT16
:
10160 (void) nvpair_value_int16(nvp
, (void *)&i16
);
10161 printf(gettext("0x%x"), i16
);
10164 case DATA_TYPE_UINT16
:
10165 (void) nvpair_value_uint16(nvp
, &i16
);
10166 printf(gettext("0x%x"), i16
);
10169 case DATA_TYPE_INT32
:
10170 (void) nvpair_value_int32(nvp
, (void *)&i32
);
10171 printf(gettext("0x%x"), i32
);
10174 case DATA_TYPE_UINT32
:
10175 (void) nvpair_value_uint32(nvp
, &i32
);
10176 printf(gettext("0x%x"), i32
);
10179 case DATA_TYPE_INT64
:
10180 (void) nvpair_value_int64(nvp
, (void *)&i64
);
10181 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
10184 case DATA_TYPE_UINT64
:
10185 (void) nvpair_value_uint64(nvp
, &i64
);
10187 * translate vdev state values to readable
10188 * strings to aide zpool events consumers
10191 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE
) == 0 ||
10193 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE
) == 0) {
10194 printf(gettext("\"%s\" (0x%llx)"),
10195 zpool_state_to_name(i64
, VDEV_AUX_NONE
),
10196 (u_longlong_t
)i64
);
10198 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
10202 case DATA_TYPE_HRTIME
:
10203 (void) nvpair_value_hrtime(nvp
, (void *)&i64
);
10204 printf(gettext("0x%llx"), (u_longlong_t
)i64
);
10207 case DATA_TYPE_STRING
:
10208 (void) nvpair_value_string(nvp
, &str
);
10209 printf(gettext("\"%s\""), str
? str
: "<NULL>");
10212 case DATA_TYPE_NVLIST
:
10213 printf(gettext("(embedded nvlist)\n"));
10214 (void) nvpair_value_nvlist(nvp
, &cnv
);
10215 zpool_do_events_nvprint(cnv
, depth
+ 8);
10216 printf(gettext("%*s(end %s)"), depth
, "", name
);
10219 case DATA_TYPE_NVLIST_ARRAY
: {
10223 (void) nvpair_value_nvlist_array(nvp
, &val
, &nelem
);
10224 printf(gettext("(%d embedded nvlists)\n"), nelem
);
10225 for (i
= 0; i
< nelem
; i
++) {
10226 printf(gettext("%*s%s[%d] = %s\n"),
10227 depth
, "", name
, i
, "(embedded nvlist)");
10228 zpool_do_events_nvprint(val
[i
], depth
+ 8);
10229 printf(gettext("%*s(end %s[%i])\n"),
10230 depth
, "", name
, i
);
10232 printf(gettext("%*s(end %s)\n"), depth
, "", name
);
10236 case DATA_TYPE_INT8_ARRAY
: {
10240 (void) nvpair_value_int8_array(nvp
, &val
, &nelem
);
10241 for (i
= 0; i
< nelem
; i
++)
10242 printf(gettext("0x%x "), val
[i
]);
10247 case DATA_TYPE_UINT8_ARRAY
: {
10251 (void) nvpair_value_uint8_array(nvp
, &val
, &nelem
);
10252 for (i
= 0; i
< nelem
; i
++)
10253 printf(gettext("0x%x "), val
[i
]);
10258 case DATA_TYPE_INT16_ARRAY
: {
10262 (void) nvpair_value_int16_array(nvp
, &val
, &nelem
);
10263 for (i
= 0; i
< nelem
; i
++)
10264 printf(gettext("0x%x "), val
[i
]);
10269 case DATA_TYPE_UINT16_ARRAY
: {
10273 (void) nvpair_value_uint16_array(nvp
, &val
, &nelem
);
10274 for (i
= 0; i
< nelem
; i
++)
10275 printf(gettext("0x%x "), val
[i
]);
10280 case DATA_TYPE_INT32_ARRAY
: {
10284 (void) nvpair_value_int32_array(nvp
, &val
, &nelem
);
10285 for (i
= 0; i
< nelem
; i
++)
10286 printf(gettext("0x%x "), val
[i
]);
10291 case DATA_TYPE_UINT32_ARRAY
: {
10295 (void) nvpair_value_uint32_array(nvp
, &val
, &nelem
);
10296 for (i
= 0; i
< nelem
; i
++)
10297 printf(gettext("0x%x "), val
[i
]);
10302 case DATA_TYPE_INT64_ARRAY
: {
10306 (void) nvpair_value_int64_array(nvp
, &val
, &nelem
);
10307 for (i
= 0; i
< nelem
; i
++)
10308 printf(gettext("0x%llx "),
10309 (u_longlong_t
)val
[i
]);
10314 case DATA_TYPE_UINT64_ARRAY
: {
10318 (void) nvpair_value_uint64_array(nvp
, &val
, &nelem
);
10319 for (i
= 0; i
< nelem
; i
++)
10320 printf(gettext("0x%llx "),
10321 (u_longlong_t
)val
[i
]);
10326 case DATA_TYPE_STRING_ARRAY
: {
10330 (void) nvpair_value_string_array(nvp
, &str
, &nelem
);
10331 for (i
= 0; i
< nelem
; i
++)
10332 printf(gettext("\"%s\" "),
10333 str
[i
] ? str
[i
] : "<NULL>");
10338 case DATA_TYPE_BOOLEAN_ARRAY
:
10339 case DATA_TYPE_BYTE_ARRAY
:
10340 case DATA_TYPE_DOUBLE
:
10341 case DATA_TYPE_DONTCARE
:
10342 case DATA_TYPE_UNKNOWN
:
10343 printf(gettext("<unknown>"));
10347 printf(gettext("\n"));
10352 zpool_do_events_next(ev_opts_t
*opts
)
10355 int zevent_fd
, ret
, dropped
;
10358 zevent_fd
= open(ZFS_DEV
, O_RDWR
);
10359 VERIFY(zevent_fd
>= 0);
10361 if (!opts
->scripted
)
10362 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
10365 ret
= zpool_events_next(g_zfs
, &nvl
, &dropped
,
10366 (opts
->follow
? ZEVENT_NONE
: ZEVENT_NONBLOCK
), zevent_fd
);
10367 if (ret
|| nvl
== NULL
)
10371 (void) printf(gettext("dropped %d events\n"), dropped
);
10373 if (strlen(opts
->poolname
) > 0 &&
10374 nvlist_lookup_string(nvl
, FM_FMRI_ZFS_POOL
, &pool
) == 0 &&
10375 strcmp(opts
->poolname
, pool
) != 0)
10378 zpool_do_events_short(nvl
, opts
);
10380 if (opts
->verbose
) {
10381 zpool_do_events_nvprint(nvl
, 8);
10382 printf(gettext("\n"));
10384 (void) fflush(stdout
);
10389 VERIFY(0 == close(zevent_fd
));
10395 zpool_do_events_clear(void)
10399 ret
= zpool_events_clear(g_zfs
, &count
);
10401 (void) printf(gettext("cleared %d events\n"), count
);
10407 * zpool events [-vHf [pool] | -c]
10409 * Displays events logs by ZFS.
10412 zpool_do_events(int argc
, char **argv
)
10414 ev_opts_t opts
= { 0 };
10418 /* check options */
10419 while ((c
= getopt(argc
, argv
, "vHfc")) != -1) {
10434 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10443 (void) fprintf(stderr
, gettext("too many arguments\n"));
10445 } else if (argc
== 1) {
10446 (void) strlcpy(opts
.poolname
, argv
[0], sizeof (opts
.poolname
));
10447 if (!zfs_name_valid(opts
.poolname
, ZFS_TYPE_POOL
)) {
10448 (void) fprintf(stderr
,
10449 gettext("invalid pool name '%s'\n"), opts
.poolname
);
10454 if ((argc
== 1 || opts
.verbose
|| opts
.scripted
|| opts
.follow
) &&
10456 (void) fprintf(stderr
,
10457 gettext("invalid options combined with -c\n"));
10462 ret
= zpool_do_events_clear();
10464 ret
= zpool_do_events_next(&opts
);
10470 get_callback_vdev(zpool_handle_t
*zhp
, char *vdevname
, void *data
)
10472 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
10473 char value
[ZFS_MAXPROPLEN
];
10474 zprop_source_t srctype
;
10476 for (zprop_list_t
*pl
= cbp
->cb_proplist
; pl
!= NULL
;
10477 pl
= pl
->pl_next
) {
10480 * If the first property is pool name, it is a special
10481 * placeholder that we can skip. This will also skip
10482 * over the name property when 'all' is specified.
10484 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
10485 pl
== cbp
->cb_proplist
)
10488 if (pl
->pl_prop
== ZPROP_INVAL
) {
10489 prop_name
= pl
->pl_user_prop
;
10491 prop_name
= (char *)vdev_prop_to_name(pl
->pl_prop
);
10493 if (zpool_get_vdev_prop(zhp
, vdevname
, pl
->pl_prop
,
10494 prop_name
, value
, sizeof (value
), &srctype
,
10495 cbp
->cb_literal
) == 0) {
10496 zprop_print_one_property(vdevname
, cbp
, prop_name
,
10497 value
, srctype
, NULL
, NULL
);
10505 get_callback_vdev_cb(void *zhp_data
, nvlist_t
*nv
, void *data
)
10507 zpool_handle_t
*zhp
= zhp_data
;
10508 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
10514 * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the
10515 * pool name for display purposes, which is not desired. Fallback to
10516 * zpool_vdev_name() when not dealing with the root vdev.
10518 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
10519 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
10520 vdevname
= strdup("root-0");
10522 vdevname
= zpool_vdev_name(g_zfs
, zhp
, nv
,
10523 cbp
->cb_vdevs
.cb_name_flags
);
10525 (void) vdev_expand_proplist(zhp
, vdevname
, &cbp
->cb_proplist
);
10527 ret
= get_callback_vdev(zhp
, vdevname
, data
);
10535 get_callback(zpool_handle_t
*zhp
, void *data
)
10537 zprop_get_cbdata_t
*cbp
= (zprop_get_cbdata_t
*)data
;
10538 char value
[ZFS_MAXPROPLEN
];
10539 zprop_source_t srctype
;
10543 if (cbp
->cb_type
== ZFS_TYPE_VDEV
) {
10544 if (strcmp(cbp
->cb_vdevs
.cb_names
[0], "all-vdevs") == 0) {
10545 for_each_vdev(zhp
, get_callback_vdev_cb
, data
);
10547 /* Adjust column widths for vdev properties */
10548 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10550 vdev_expand_proplist(zhp
,
10551 cbp
->cb_vdevs
.cb_names
[vid
],
10552 &cbp
->cb_proplist
);
10554 /* Display the properties */
10555 for (vid
= 0; vid
< cbp
->cb_vdevs
.cb_names_count
;
10557 get_callback_vdev(zhp
,
10558 cbp
->cb_vdevs
.cb_names
[vid
], data
);
10562 assert(cbp
->cb_type
== ZFS_TYPE_POOL
);
10563 for (pl
= cbp
->cb_proplist
; pl
!= NULL
; pl
= pl
->pl_next
) {
10565 * Skip the special fake placeholder. This will also
10566 * skip over the name property when 'all' is specified.
10568 if (pl
->pl_prop
== ZPOOL_PROP_NAME
&&
10569 pl
== cbp
->cb_proplist
)
10572 if (pl
->pl_prop
== ZPROP_INVAL
&&
10573 zfs_prop_user(pl
->pl_user_prop
)) {
10574 srctype
= ZPROP_SRC_LOCAL
;
10576 if (zpool_get_userprop(zhp
, pl
->pl_user_prop
,
10577 value
, sizeof (value
), &srctype
) != 0)
10580 zprop_print_one_property(zpool_get_name(zhp
),
10581 cbp
, pl
->pl_user_prop
, value
, srctype
,
10583 } else if (pl
->pl_prop
== ZPROP_INVAL
&&
10584 (zpool_prop_feature(pl
->pl_user_prop
) ||
10585 zpool_prop_unsupported(pl
->pl_user_prop
))) {
10586 srctype
= ZPROP_SRC_LOCAL
;
10588 if (zpool_prop_get_feature(zhp
,
10589 pl
->pl_user_prop
, value
,
10590 sizeof (value
)) == 0) {
10591 zprop_print_one_property(
10592 zpool_get_name(zhp
), cbp
,
10593 pl
->pl_user_prop
, value
, srctype
,
10597 if (zpool_get_prop(zhp
, pl
->pl_prop
, value
,
10598 sizeof (value
), &srctype
,
10599 cbp
->cb_literal
) != 0)
10602 zprop_print_one_property(zpool_get_name(zhp
),
10603 cbp
, zpool_prop_to_name(pl
->pl_prop
),
10604 value
, srctype
, NULL
, NULL
);
10613 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10615 * -H Scripted mode. Don't display headers, and separate properties
10617 * -o List of columns to display. Defaults to
10618 * "name,property,value,source".
10619 * -p Display values in parsable (exact) format.
10621 * Get properties of pools in the system. Output space statistics
10622 * for each one as well as other attributes.
10625 zpool_do_get(int argc
, char **argv
)
10627 zprop_get_cbdata_t cb
= { 0 };
10628 zprop_list_t fake_name
= { 0 };
10631 char *propstr
= NULL
;
10634 cb
.cb_first
= B_TRUE
;
10637 * Set up default columns and sources.
10639 cb
.cb_sources
= ZPROP_SRC_ALL
;
10640 cb
.cb_columns
[0] = GET_COL_NAME
;
10641 cb
.cb_columns
[1] = GET_COL_PROPERTY
;
10642 cb
.cb_columns
[2] = GET_COL_VALUE
;
10643 cb
.cb_columns
[3] = GET_COL_SOURCE
;
10644 cb
.cb_type
= ZFS_TYPE_POOL
;
10645 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10646 current_prop_type
= cb
.cb_type
;
10648 /* check options */
10649 while ((c
= getopt(argc
, argv
, ":Hpo:")) != -1) {
10652 cb
.cb_literal
= B_TRUE
;
10655 cb
.cb_scripted
= B_TRUE
;
10658 memset(&cb
.cb_columns
, 0, sizeof (cb
.cb_columns
));
10661 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
10662 static const char *const col_opts
[] =
10663 { "name", "property", "value", "source",
10665 static const zfs_get_column_t col_cols
[] =
10666 { GET_COL_NAME
, GET_COL_PROPERTY
, GET_COL_VALUE
,
10669 if (i
== ZFS_GET_NCOLS
- 1) {
10670 (void) fprintf(stderr
, gettext("too "
10671 "many fields given to -o "
10676 for (c
= 0; c
< ARRAY_SIZE(col_opts
); ++c
)
10677 if (strcmp(tok
, col_opts
[c
]) == 0)
10680 (void) fprintf(stderr
,
10681 gettext("invalid column name '%s'\n"), tok
);
10687 (void) fprintf(stderr
,
10688 gettext("\"all\" conflicts "
10689 "with specific fields "
10690 "given to -o option\n"));
10694 memcpy(cb
.cb_columns
, col_cols
,
10695 sizeof (col_cols
));
10696 i
= ZFS_GET_NCOLS
- 1;
10698 cb
.cb_columns
[i
++] = col_cols
[c
];
10702 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10712 (void) fprintf(stderr
, gettext("missing property "
10717 /* Properties list is needed later by zprop_get_list() */
10724 /* No args, so just print the defaults. */
10725 } else if (are_all_pools(argc
, argv
)) {
10726 /* All the args are pool names */
10727 } else if (are_all_pools(1, argv
)) {
10728 /* The first arg is a pool name */
10729 if ((argc
== 2 && strcmp(argv
[1], "all-vdevs") == 0) ||
10730 (argc
== 2 && strcmp(argv
[1], "root") == 0) ||
10731 are_vdevs_in_pool(argc
- 1, argv
+ 1, argv
[0],
10734 if (strcmp(argv
[1], "root") == 0)
10735 vdev
= strdup("root-0");
10737 vdev
= strdup(argv
[1]);
10739 /* ... and the rest are vdev names */
10740 cb
.cb_vdevs
.cb_names
= &vdev
;
10741 cb
.cb_vdevs
.cb_names_count
= argc
- 1;
10742 cb
.cb_type
= ZFS_TYPE_VDEV
;
10743 argc
= 1; /* One pool to process */
10745 fprintf(stderr
, gettext("Expected a list of vdevs in"
10746 " \"%s\", but got:\n"), argv
[0]);
10747 error_list_unresolved_vdevs(argc
- 1, argv
+ 1,
10748 argv
[0], &cb
.cb_vdevs
);
10749 fprintf(stderr
, "\n");
10755 * The first arg isn't a pool name,
10757 fprintf(stderr
, gettext("missing pool name.\n"));
10758 fprintf(stderr
, "\n");
10763 if (zprop_get_list(g_zfs
, propstr
, &cb
.cb_proplist
,
10764 cb
.cb_type
) != 0) {
10765 /* Use correct list of valid properties (pool or vdev) */
10766 current_prop_type
= cb
.cb_type
;
10770 if (cb
.cb_proplist
!= NULL
) {
10771 fake_name
.pl_prop
= ZPOOL_PROP_NAME
;
10772 fake_name
.pl_width
= strlen(gettext("NAME"));
10773 fake_name
.pl_next
= cb
.cb_proplist
;
10774 cb
.cb_proplist
= &fake_name
;
10777 ret
= for_each_pool(argc
, argv
, B_TRUE
, &cb
.cb_proplist
, cb
.cb_type
,
10778 cb
.cb_literal
, get_callback
, &cb
);
10780 if (cb
.cb_proplist
== &fake_name
)
10781 zprop_free_list(fake_name
.pl_next
);
10783 zprop_free_list(cb
.cb_proplist
);
10791 typedef struct set_cbdata
{
10794 zfs_type_t cb_type
;
10795 vdev_cbdata_t cb_vdevs
;
10796 boolean_t cb_any_successful
;
10800 set_pool_callback(zpool_handle_t
*zhp
, set_cbdata_t
*cb
)
10804 /* Check if we have out-of-bounds features */
10805 if (strcmp(cb
->cb_propname
, ZPOOL_CONFIG_COMPATIBILITY
) == 0) {
10806 boolean_t features
[SPA_FEATURES
];
10807 if (zpool_do_load_compat(cb
->cb_value
, features
) !=
10808 ZPOOL_COMPATIBILITY_OK
)
10811 nvlist_t
*enabled
= zpool_get_features(zhp
);
10813 for (i
= 0; i
< SPA_FEATURES
; i
++) {
10814 const char *fguid
= spa_feature_table
[i
].fi_guid
;
10815 if (nvlist_exists(enabled
, fguid
) && !features
[i
])
10818 if (i
< SPA_FEATURES
)
10819 (void) fprintf(stderr
, gettext("Warning: one or "
10820 "more features already enabled on pool '%s'\n"
10821 "are not present in this compatibility set.\n"),
10822 zpool_get_name(zhp
));
10825 /* if we're setting a feature, check it's in compatibility set */
10826 if (zpool_prop_feature(cb
->cb_propname
) &&
10827 strcmp(cb
->cb_value
, ZFS_FEATURE_ENABLED
) == 0) {
10828 char *fname
= strchr(cb
->cb_propname
, '@') + 1;
10831 if (zfeature_lookup_name(fname
, &f
) == 0) {
10832 char compat
[ZFS_MAXPROPLEN
];
10833 if (zpool_get_prop(zhp
, ZPOOL_PROP_COMPATIBILITY
,
10834 compat
, ZFS_MAXPROPLEN
, NULL
, B_FALSE
) != 0)
10837 boolean_t features
[SPA_FEATURES
];
10838 if (zpool_do_load_compat(compat
, features
) !=
10839 ZPOOL_COMPATIBILITY_OK
) {
10840 (void) fprintf(stderr
, gettext("Error: "
10841 "cannot enable feature '%s' on pool '%s'\n"
10842 "because the pool's 'compatibility' "
10843 "property cannot be parsed.\n"),
10844 fname
, zpool_get_name(zhp
));
10848 if (!features
[f
]) {
10849 (void) fprintf(stderr
, gettext("Error: "
10850 "cannot enable feature '%s' on pool '%s'\n"
10851 "as it is not specified in this pool's "
10852 "current compatibility set.\n"
10853 "Consider setting 'compatibility' to a "
10854 "less restrictive set, or to 'off'.\n"),
10855 fname
, zpool_get_name(zhp
));
10861 error
= zpool_set_prop(zhp
, cb
->cb_propname
, cb
->cb_value
);
10867 set_callback(zpool_handle_t
*zhp
, void *data
)
10870 set_cbdata_t
*cb
= (set_cbdata_t
*)data
;
10872 if (cb
->cb_type
== ZFS_TYPE_VDEV
) {
10873 error
= zpool_set_vdev_prop(zhp
, *cb
->cb_vdevs
.cb_names
,
10874 cb
->cb_propname
, cb
->cb_value
);
10876 assert(cb
->cb_type
== ZFS_TYPE_POOL
);
10877 error
= set_pool_callback(zhp
, cb
);
10880 cb
->cb_any_successful
= !error
;
10885 zpool_do_set(int argc
, char **argv
)
10887 set_cbdata_t cb
= { 0 };
10891 current_prop_type
= ZFS_TYPE_POOL
;
10892 if (argc
> 1 && argv
[1][0] == '-') {
10893 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
10899 (void) fprintf(stderr
, gettext("missing property=value "
10905 (void) fprintf(stderr
, gettext("missing pool name\n"));
10910 (void) fprintf(stderr
, gettext("too many pool names\n"));
10914 cb
.cb_propname
= argv
[1];
10915 cb
.cb_type
= ZFS_TYPE_POOL
;
10916 cb
.cb_vdevs
.cb_name_flags
|= VDEV_NAME_TYPE_ID
;
10917 cb
.cb_value
= strchr(cb
.cb_propname
, '=');
10918 if (cb
.cb_value
== NULL
) {
10919 (void) fprintf(stderr
, gettext("missing value in "
10920 "property=value argument\n"));
10924 *(cb
.cb_value
) = '\0';
10929 /* argv[0] is pool name */
10930 if (!is_pool(argv
[0])) {
10931 (void) fprintf(stderr
,
10932 gettext("cannot open '%s': is not a pool\n"), argv
[0]);
10936 /* argv[1], when supplied, is vdev name */
10939 if (strcmp(argv
[1], "root") == 0)
10940 vdev
= strdup("root-0");
10942 vdev
= strdup(argv
[1]);
10944 if (!are_vdevs_in_pool(1, &vdev
, argv
[0], &cb
.cb_vdevs
)) {
10945 (void) fprintf(stderr
, gettext(
10946 "cannot find '%s' in '%s': device not in pool\n"),
10951 cb
.cb_vdevs
.cb_names
= &vdev
;
10952 cb
.cb_vdevs
.cb_names_count
= 1;
10953 cb
.cb_type
= ZFS_TYPE_VDEV
;
10956 error
= for_each_pool(1, argv
, B_TRUE
, NULL
, ZFS_TYPE_POOL
,
10957 B_FALSE
, set_callback
, &cb
);
10965 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10967 vdev_activity_remaining(nvlist_t
*nv
, zpool_wait_activity_t activity
)
10969 uint64_t bytes_remaining
;
10971 uint_t c
, children
;
10974 assert(activity
== ZPOOL_WAIT_INITIALIZE
||
10975 activity
== ZPOOL_WAIT_TRIM
);
10977 verify(nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
10978 (uint64_t **)&vs
, &c
) == 0);
10980 if (activity
== ZPOOL_WAIT_INITIALIZE
&&
10981 vs
->vs_initialize_state
== VDEV_INITIALIZE_ACTIVE
)
10982 bytes_remaining
= vs
->vs_initialize_bytes_est
-
10983 vs
->vs_initialize_bytes_done
;
10984 else if (activity
== ZPOOL_WAIT_TRIM
&&
10985 vs
->vs_trim_state
== VDEV_TRIM_ACTIVE
)
10986 bytes_remaining
= vs
->vs_trim_bytes_est
-
10987 vs
->vs_trim_bytes_done
;
10989 bytes_remaining
= 0;
10991 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
10992 &child
, &children
) != 0)
10995 for (c
= 0; c
< children
; c
++)
10996 bytes_remaining
+= vdev_activity_remaining(child
[c
], activity
);
10998 return (bytes_remaining
);
11001 /* Add up the total number of bytes left to rebuild across top-level vdevs */
11003 vdev_activity_top_remaining(nvlist_t
*nv
)
11005 uint64_t bytes_remaining
= 0;
11010 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
11011 &child
, &children
) != 0)
11014 for (uint_t c
= 0; c
< children
; c
++) {
11015 vdev_rebuild_stat_t
*vrs
;
11018 error
= nvlist_lookup_uint64_array(child
[c
],
11019 ZPOOL_CONFIG_REBUILD_STATS
, (uint64_t **)&vrs
, &i
);
11021 if (vrs
->vrs_state
== VDEV_REBUILD_ACTIVE
) {
11022 bytes_remaining
+= (vrs
->vrs_bytes_est
-
11023 vrs
->vrs_bytes_rebuilt
);
11028 return (bytes_remaining
);
11031 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
11033 vdev_any_spare_replacing(nvlist_t
*nv
)
11036 uint_t c
, children
;
11037 const char *vdev_type
;
11039 (void) nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &vdev_type
);
11041 if (strcmp(vdev_type
, VDEV_TYPE_REPLACING
) == 0 ||
11042 strcmp(vdev_type
, VDEV_TYPE_SPARE
) == 0 ||
11043 strcmp(vdev_type
, VDEV_TYPE_DRAID_SPARE
) == 0) {
11047 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
11048 &child
, &children
) != 0)
11051 for (c
= 0; c
< children
; c
++) {
11052 if (vdev_any_spare_replacing(child
[c
]))
11059 typedef struct wait_data
{
11061 boolean_t wd_scripted
;
11062 boolean_t wd_exact
;
11063 boolean_t wd_headers_once
;
11064 boolean_t wd_should_exit
;
11065 /* Which activities to wait for */
11066 boolean_t wd_enabled
[ZPOOL_WAIT_NUM_ACTIVITIES
];
11068 pthread_cond_t wd_cv
;
11069 pthread_mutex_t wd_mutex
;
11073 * Print to stdout a single line, containing one column for each activity that
11074 * we are waiting for specifying how many bytes of work are left for that
11078 print_wait_status_row(wait_data_t
*wd
, zpool_handle_t
*zhp
, int row
)
11080 nvlist_t
*config
, *nvroot
;
11083 pool_checkpoint_stat_t
*pcs
= NULL
;
11084 pool_scan_stat_t
*pss
= NULL
;
11085 pool_removal_stat_t
*prs
= NULL
;
11086 pool_raidz_expand_stat_t
*pres
= NULL
;
11087 const char *const headers
[] = {"DISCARD", "FREE", "INITIALIZE",
11088 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"};
11089 int col_widths
[ZPOOL_WAIT_NUM_ACTIVITIES
];
11091 /* Calculate the width of each column */
11092 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
11094 * Make sure we have enough space in the col for pretty-printed
11095 * numbers and for the column header, and then leave a couple
11096 * spaces between cols for readability.
11098 col_widths
[i
] = MAX(strlen(headers
[i
]), 6) + 2;
11101 if (timestamp_fmt
!= NODATE
)
11102 print_timestamp(timestamp_fmt
);
11104 /* Print header if appropriate */
11105 int term_height
= terminal_height();
11106 boolean_t reprint_header
= (!wd
->wd_headers_once
&& term_height
> 0 &&
11107 row
% (term_height
-1) == 0);
11108 if (!wd
->wd_scripted
&& (row
== 0 || reprint_header
)) {
11109 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
11110 if (wd
->wd_enabled
[i
])
11111 (void) printf("%*s", col_widths
[i
], headers
[i
]);
11113 (void) fputc('\n', stdout
);
11116 /* Bytes of work remaining in each activity */
11117 int64_t bytes_rem
[ZPOOL_WAIT_NUM_ACTIVITIES
] = {0};
11119 bytes_rem
[ZPOOL_WAIT_FREE
] =
11120 zpool_get_prop_int(zhp
, ZPOOL_PROP_FREEING
, NULL
);
11122 config
= zpool_get_config(zhp
, NULL
);
11123 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
11125 (void) nvlist_lookup_uint64_array(nvroot
,
11126 ZPOOL_CONFIG_CHECKPOINT_STATS
, (uint64_t **)&pcs
, &c
);
11127 if (pcs
!= NULL
&& pcs
->pcs_state
== CS_CHECKPOINT_DISCARDING
)
11128 bytes_rem
[ZPOOL_WAIT_CKPT_DISCARD
] = pcs
->pcs_space
;
11130 (void) nvlist_lookup_uint64_array(nvroot
,
11131 ZPOOL_CONFIG_REMOVAL_STATS
, (uint64_t **)&prs
, &c
);
11132 if (prs
!= NULL
&& prs
->prs_state
== DSS_SCANNING
)
11133 bytes_rem
[ZPOOL_WAIT_REMOVE
] = prs
->prs_to_copy
-
11136 (void) nvlist_lookup_uint64_array(nvroot
,
11137 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&pss
, &c
);
11138 if (pss
!= NULL
&& pss
->pss_state
== DSS_SCANNING
&&
11139 pss
->pss_pass_scrub_pause
== 0) {
11140 int64_t rem
= pss
->pss_to_examine
- pss
->pss_issued
;
11141 if (pss
->pss_func
== POOL_SCAN_SCRUB
)
11142 bytes_rem
[ZPOOL_WAIT_SCRUB
] = rem
;
11144 bytes_rem
[ZPOOL_WAIT_RESILVER
] = rem
;
11145 } else if (check_rebuilding(nvroot
, NULL
)) {
11146 bytes_rem
[ZPOOL_WAIT_RESILVER
] =
11147 vdev_activity_top_remaining(nvroot
);
11150 (void) nvlist_lookup_uint64_array(nvroot
,
11151 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS
, (uint64_t **)&pres
, &c
);
11152 if (pres
!= NULL
&& pres
->pres_state
== DSS_SCANNING
) {
11153 int64_t rem
= pres
->pres_to_reflow
- pres
->pres_reflowed
;
11154 bytes_rem
[ZPOOL_WAIT_RAIDZ_EXPAND
] = rem
;
11157 bytes_rem
[ZPOOL_WAIT_INITIALIZE
] =
11158 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_INITIALIZE
);
11159 bytes_rem
[ZPOOL_WAIT_TRIM
] =
11160 vdev_activity_remaining(nvroot
, ZPOOL_WAIT_TRIM
);
11163 * A replace finishes after resilvering finishes, so the amount of work
11164 * left for a replace is the same as for resilvering.
11166 * It isn't quite correct to say that if we have any 'spare' or
11167 * 'replacing' vdevs and a resilver is happening, then a replace is in
11168 * progress, like we do here. When a hot spare is used, the faulted vdev
11169 * is not removed after the hot spare is resilvered, so parent 'spare'
11170 * vdev is not removed either. So we could have a 'spare' vdev, but be
11171 * resilvering for a different reason. However, we use it as a heuristic
11172 * because we don't have access to the DTLs, which could tell us whether
11173 * or not we have really finished resilvering a hot spare.
11175 if (vdev_any_spare_replacing(nvroot
))
11176 bytes_rem
[ZPOOL_WAIT_REPLACE
] = bytes_rem
[ZPOOL_WAIT_RESILVER
];
11178 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
11180 if (!wd
->wd_enabled
[i
])
11183 if (wd
->wd_exact
) {
11184 (void) snprintf(buf
, sizeof (buf
), "%" PRIi64
,
11187 zfs_nicenum(bytes_rem
[i
], buf
, sizeof (buf
));
11190 if (wd
->wd_scripted
)
11191 (void) printf(i
== 0 ? "%s" : "\t%s", buf
);
11193 (void) printf(" %*s", col_widths
[i
] - 1, buf
);
11195 (void) printf("\n");
11196 (void) fflush(stdout
);
11200 wait_status_thread(void *arg
)
11202 wait_data_t
*wd
= (wait_data_t
*)arg
;
11203 zpool_handle_t
*zhp
;
11205 if ((zhp
= zpool_open(g_zfs
, wd
->wd_poolname
)) == NULL
)
11206 return (void *)(1);
11208 for (int row
= 0; ; row
++) {
11210 struct timespec timeout
;
11212 (void) clock_gettime(CLOCK_REALTIME
, &timeout
);
11214 if (zpool_refresh_stats(zhp
, &missing
) != 0 || missing
||
11215 zpool_props_refresh(zhp
) != 0) {
11217 return (void *)(uintptr_t)(missing
? 0 : 1);
11220 print_wait_status_row(wd
, zhp
, row
);
11222 timeout
.tv_sec
+= floor(wd
->wd_interval
);
11223 long nanos
= timeout
.tv_nsec
+
11224 (wd
->wd_interval
- floor(wd
->wd_interval
)) * NANOSEC
;
11225 if (nanos
>= NANOSEC
) {
11227 timeout
.tv_nsec
= nanos
- NANOSEC
;
11229 timeout
.tv_nsec
= nanos
;
11231 pthread_mutex_lock(&wd
->wd_mutex
);
11232 if (!wd
->wd_should_exit
)
11233 ret
= pthread_cond_timedwait(&wd
->wd_cv
, &wd
->wd_mutex
,
11235 pthread_mutex_unlock(&wd
->wd_mutex
);
11237 break; /* signaled by main thread */
11238 } else if (ret
!= ETIMEDOUT
) {
11239 (void) fprintf(stderr
, gettext("pthread_cond_timedwait "
11240 "failed: %s\n"), strerror(ret
));
11242 return (void *)(uintptr_t)(1);
11247 return (void *)(0);
11251 zpool_do_wait(int argc
, char **argv
)
11253 boolean_t verbose
= B_FALSE
;
11255 unsigned long count
;
11256 pthread_t status_thr
;
11258 zpool_handle_t
*zhp
;
11261 wd
.wd_scripted
= B_FALSE
;
11262 wd
.wd_exact
= B_FALSE
;
11263 wd
.wd_headers_once
= B_FALSE
;
11264 wd
.wd_should_exit
= B_FALSE
;
11266 pthread_mutex_init(&wd
.wd_mutex
, NULL
);
11267 pthread_cond_init(&wd
.wd_cv
, NULL
);
11269 /* By default, wait for all types of activity. */
11270 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++)
11271 wd
.wd_enabled
[i
] = B_TRUE
;
11273 while ((c
= getopt(argc
, argv
, "HpT:t:")) != -1) {
11276 wd
.wd_scripted
= B_TRUE
;
11279 wd
.wd_headers_once
= B_TRUE
;
11282 wd
.wd_exact
= B_TRUE
;
11285 get_timestamp_arg(*optarg
);
11288 /* Reset activities array */
11289 memset(&wd
.wd_enabled
, 0, sizeof (wd
.wd_enabled
));
11291 for (char *tok
; (tok
= strsep(&optarg
, ",")); ) {
11292 static const char *const col_opts
[] = {
11293 "discard", "free", "initialize", "replace",
11294 "remove", "resilver", "scrub", "trim",
11297 for (i
= 0; i
< ARRAY_SIZE(col_opts
); ++i
)
11298 if (strcmp(tok
, col_opts
[i
]) == 0) {
11299 wd
.wd_enabled
[i
] = B_TRUE
;
11303 (void) fprintf(stderr
,
11304 gettext("invalid activity '%s'\n"), tok
);
11310 (void) fprintf(stderr
, gettext("invalid option '%c'\n"),
11319 get_interval_count(&argc
, argv
, &wd
.wd_interval
, &count
);
11321 /* This subcmd only accepts an interval, not a count */
11322 (void) fprintf(stderr
, gettext("too many arguments\n"));
11326 if (wd
.wd_interval
!= 0)
11330 (void) fprintf(stderr
, gettext("missing 'pool' argument\n"));
11334 (void) fprintf(stderr
, gettext("too many arguments\n"));
11338 wd
.wd_poolname
= argv
[0];
11340 if ((zhp
= zpool_open(g_zfs
, wd
.wd_poolname
)) == NULL
)
11345 * We use a separate thread for printing status updates because
11346 * the main thread will call lzc_wait(), which blocks as long
11347 * as an activity is in progress, which can be a long time.
11349 if (pthread_create(&status_thr
, NULL
, wait_status_thread
, &wd
)
11351 (void) fprintf(stderr
, gettext("failed to create status"
11352 "thread: %s\n"), strerror(errno
));
11359 * Loop over all activities that we are supposed to wait for until none
11360 * of them are in progress. Note that this means we can end up waiting
11361 * for more activities to complete than just those that were in progress
11362 * when we began waiting; if an activity we are interested in begins
11363 * while we are waiting for another activity, we will wait for both to
11364 * complete before exiting.
11367 boolean_t missing
= B_FALSE
;
11368 boolean_t any_waited
= B_FALSE
;
11370 for (i
= 0; i
< ZPOOL_WAIT_NUM_ACTIVITIES
; i
++) {
11373 if (!wd
.wd_enabled
[i
])
11376 error
= zpool_wait_status(zhp
, i
, &missing
, &waited
);
11377 if (error
!= 0 || missing
)
11380 any_waited
= (any_waited
|| waited
);
11383 if (error
!= 0 || missing
|| !any_waited
)
11391 pthread_mutex_lock(&wd
.wd_mutex
);
11392 wd
.wd_should_exit
= B_TRUE
;
11393 pthread_cond_signal(&wd
.wd_cv
);
11394 pthread_mutex_unlock(&wd
.wd_mutex
);
11395 (void) pthread_join(status_thr
, (void *)&status
);
11400 pthread_mutex_destroy(&wd
.wd_mutex
);
11401 pthread_cond_destroy(&wd
.wd_cv
);
11406 find_command_idx(const char *command
, int *idx
)
11408 for (int i
= 0; i
< NCOMMAND
; ++i
) {
11409 if (command_table
[i
].name
== NULL
)
11412 if (strcmp(command
, command_table
[i
].name
) == 0) {
11421 * Display version message
11424 zpool_do_version(int argc
, char **argv
)
11426 (void) argc
, (void) argv
;
11427 return (zfs_version_print() != 0);
11430 /* Display documentation */
11432 zpool_do_help(int argc
, char **argv
)
11434 char page
[MAXNAMELEN
];
11435 if (argc
< 3 || strcmp(argv
[2], "zpool") == 0)
11436 strcpy(page
, "zpool");
11437 else if (strcmp(argv
[2], "concepts") == 0 ||
11438 strcmp(argv
[2], "props") == 0)
11439 snprintf(page
, sizeof (page
), "zpool%s", argv
[2]);
11441 snprintf(page
, sizeof (page
), "zpool-%s", argv
[2]);
11443 execlp("man", "man", page
, NULL
);
11445 fprintf(stderr
, "couldn't run man program: %s", strerror(errno
));
11450 * Do zpool_load_compat() and print error message on failure
11452 static zpool_compat_status_t
11453 zpool_do_load_compat(const char *compat
, boolean_t
*list
)
11457 zpool_compat_status_t ret
;
11459 ret
= zpool_load_compat(compat
, list
, report
, 1024);
11462 case ZPOOL_COMPATIBILITY_OK
:
11465 case ZPOOL_COMPATIBILITY_NOFILES
:
11466 case ZPOOL_COMPATIBILITY_BADFILE
:
11467 case ZPOOL_COMPATIBILITY_BADTOKEN
:
11468 (void) fprintf(stderr
, "Error: %s\n", report
);
11471 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
11472 (void) fprintf(stderr
, "Warning: %s\n", report
);
11473 ret
= ZPOOL_COMPATIBILITY_OK
;
11480 main(int argc
, char **argv
)
11487 (void) setlocale(LC_ALL
, "");
11488 (void) setlocale(LC_NUMERIC
, "C");
11489 (void) textdomain(TEXT_DOMAIN
);
11495 * Make sure the user has specified some command.
11498 (void) fprintf(stderr
, gettext("missing command\n"));
11505 * Special case '-?'
11507 if ((strcmp(cmdname
, "-?") == 0) || strcmp(cmdname
, "--help") == 0)
11511 * Special case '-V|--version'
11513 if ((strcmp(cmdname
, "-V") == 0) || (strcmp(cmdname
, "--version") == 0))
11514 return (zpool_do_version(argc
, argv
));
11517 * Special case 'help'
11519 if (strcmp(cmdname
, "help") == 0)
11520 return (zpool_do_help(argc
, argv
));
11522 if ((g_zfs
= libzfs_init()) == NULL
) {
11523 (void) fprintf(stderr
, "%s\n", libzfs_error_init(errno
));
11527 libzfs_print_on_error(g_zfs
, B_TRUE
);
11529 zfs_save_arguments(argc
, argv
, history_str
, sizeof (history_str
));
11532 * Many commands modify input strings for string parsing reasons.
11533 * We create a copy to protect the original argv.
11535 newargv
= safe_malloc((argc
+ 1) * sizeof (newargv
[0]));
11536 for (i
= 0; i
< argc
; i
++)
11537 newargv
[i
] = strdup(argv
[i
]);
11538 newargv
[argc
] = NULL
;
11541 * Run the appropriate command.
11543 if (find_command_idx(cmdname
, &i
) == 0) {
11544 current_command
= &command_table
[i
];
11545 ret
= command_table
[i
].func(argc
- 1, newargv
+ 1);
11546 } else if (strchr(cmdname
, '=')) {
11547 verify(find_command_idx("set", &i
) == 0);
11548 current_command
= &command_table
[i
];
11549 ret
= command_table
[i
].func(argc
, newargv
);
11550 } else if (strcmp(cmdname
, "freeze") == 0 && argc
== 3) {
11552 * 'freeze' is a vile debugging abomination, so we treat
11555 zfs_cmd_t zc
= {"\0"};
11557 (void) strlcpy(zc
.zc_name
, argv
[2], sizeof (zc
.zc_name
));
11558 ret
= zfs_ioctl(g_zfs
, ZFS_IOC_POOL_FREEZE
, &zc
);
11560 (void) fprintf(stderr
,
11561 gettext("failed to freeze pool: %d\n"), errno
);
11567 (void) fprintf(stderr
, gettext("unrecognized "
11568 "command '%s'\n"), cmdname
);
11573 for (i
= 0; i
< argc
; i
++)
11577 if (ret
== 0 && log_history
)
11578 (void) zpool_log_history(g_zfs
, history_str
);
11580 libzfs_fini(g_zfs
);
11583 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
11584 * for the purposes of running ::findleaks.
11586 if (getenv("ZFS_ABORT") != NULL
) {
11587 (void) printf("dumping core by request\n");