]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zpool/zpool_main.c
Revert "zfs list: Allow more fields in ZFS_ITER_SIMPLE mode"
[mirror_zfs.git] / cmd / zpool / zpool_main.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
95bcd51e 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
c15d36c6 25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
95bcd51e 26 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
df831108 27 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
eaa52d32 28 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
8e896579 29 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
bec1067d 30 * Copyright (c) 2017 Datto Inc.
d3f2cd7e 31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
cc99f275 32 * Copyright (c) 2017, Intel Corporation.
e55db32a 33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
658fb802 34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
2a673e76 35 * Copyright (c) 2021, Klara Inc.
dce1bf99 36 * Copyright [2021] Hewlett Packard Enterprise Development LP
34dc7c2f
BB
37 */
38
34dc7c2f
BB
39#include <assert.h>
40#include <ctype.h>
41#include <dirent.h>
42#include <errno.h>
43#include <fcntl.h>
d2734cce 44#include <getopt.h>
34dc7c2f
BB
45#include <libgen.h>
46#include <libintl.h>
47#include <libuutil.h>
48#include <locale.h>
e60e158e 49#include <pthread.h>
34dc7c2f
BB
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <strings.h>
e60e158e 54#include <time.h>
34dc7c2f 55#include <unistd.h>
34dc7c2f
BB
56#include <pwd.h>
57#include <zone.h>
d6418de0 58#include <sys/wait.h>
1bd201e7 59#include <zfs_prop.h>
34dc7c2f 60#include <sys/fs/zfs.h>
34dc7c2f 61#include <sys/stat.h>
46364cb2 62#include <sys/systeminfo.h>
d02ca379 63#include <sys/fm/fs/zfs.h>
26685276
BB
64#include <sys/fm/util.h>
65#include <sys/fm/protocol.h>
8c7aa0cf 66#include <sys/zfs_ioctl.h>
b5256303 67#include <sys/mount.h>
d4a72f23
TC
68#include <sys/sysmacros.h>
69
193a37cb 70#include <math.h>
34dc7c2f
BB
71
72#include <libzfs.h>
e89f1295 73#include <libzutil.h>
34dc7c2f
BB
74
75#include "zpool_util.h"
76#include "zfs_comutil.h"
9ae529ec 77#include "zfeature_common.h"
34dc7c2f 78
428870ff
BB
79#include "statcommon.h"
80
af09c050
RD
81libzfs_handle_t *g_zfs;
82
34dc7c2f
BB
83static int zpool_do_create(int, char **);
84static int zpool_do_destroy(int, char **);
85
86static int zpool_do_add(int, char **);
87static int zpool_do_remove(int, char **);
131cc95c 88static int zpool_do_labelclear(int, char **);
34dc7c2f 89
d2734cce
SD
90static int zpool_do_checkpoint(int, char **);
91
34dc7c2f
BB
92static int zpool_do_list(int, char **);
93static int zpool_do_iostat(int, char **);
94static int zpool_do_status(int, char **);
95
96static int zpool_do_online(int, char **);
97static int zpool_do_offline(int, char **);
98static int zpool_do_clear(int, char **);
1bd201e7 99static int zpool_do_reopen(int, char **);
34dc7c2f 100
3541dc6d
GA
101static int zpool_do_reguid(int, char **);
102
34dc7c2f
BB
103static int zpool_do_attach(int, char **);
104static int zpool_do_detach(int, char **);
105static int zpool_do_replace(int, char **);
428870ff 106static int zpool_do_split(int, char **);
34dc7c2f 107
619f0976 108static int zpool_do_initialize(int, char **);
34dc7c2f 109static int zpool_do_scrub(int, char **);
80a91e74 110static int zpool_do_resilver(int, char **);
1b939560 111static int zpool_do_trim(int, char **);
34dc7c2f
BB
112
113static int zpool_do_import(int, char **);
114static int zpool_do_export(int, char **);
115
116static int zpool_do_upgrade(int, char **);
117
118static int zpool_do_history(int, char **);
26685276 119static int zpool_do_events(int, char **);
34dc7c2f
BB
120
121static int zpool_do_get(int, char **);
122static int zpool_do_set(int, char **);
123
bec1067d
AP
124static int zpool_do_sync(int, char **);
125
50478c6d
T
126static int zpool_do_version(int, char **);
127
e60e158e
JG
128static int zpool_do_wait(int, char **);
129
658fb802
CB
130static zpool_compat_status_t zpool_do_load_compat(
131 const char *, boolean_t *);
132
34dc7c2f
BB
133/*
134 * These libumem hooks provide a reasonable set of defaults for the allocator's
135 * debugging facilities.
136 */
b128c09f
BB
137
138#ifdef DEBUG
34dc7c2f
BB
139const char *
140_umem_debug_init(void)
141{
142 return ("default,verbose"); /* $UMEM_DEBUG setting */
143}
144
145const char *
146_umem_logging_init(void)
147{
148 return ("fail,contents"); /* $UMEM_LOGGING setting */
149}
b128c09f 150#endif
34dc7c2f
BB
151
152typedef enum {
153 HELP_ADD,
154 HELP_ATTACH,
155 HELP_CLEAR,
156 HELP_CREATE,
d2734cce 157 HELP_CHECKPOINT,
34dc7c2f
BB
158 HELP_DESTROY,
159 HELP_DETACH,
160 HELP_EXPORT,
161 HELP_HISTORY,
162 HELP_IMPORT,
163 HELP_IOSTAT,
131cc95c 164 HELP_LABELCLEAR,
34dc7c2f
BB
165 HELP_LIST,
166 HELP_OFFLINE,
167 HELP_ONLINE,
168 HELP_REPLACE,
169 HELP_REMOVE,
619f0976 170 HELP_INITIALIZE,
34dc7c2f 171 HELP_SCRUB,
80a91e74 172 HELP_RESILVER,
1b939560 173 HELP_TRIM,
34dc7c2f
BB
174 HELP_STATUS,
175 HELP_UPGRADE,
26685276 176 HELP_EVENTS,
34dc7c2f 177 HELP_GET,
428870ff 178 HELP_SET,
3541dc6d 179 HELP_SPLIT,
bec1067d 180 HELP_SYNC,
1bd201e7 181 HELP_REGUID,
50478c6d 182 HELP_REOPEN,
e60e158e
JG
183 HELP_VERSION,
184 HELP_WAIT
34dc7c2f
BB
185} zpool_help_t;
186
187
193a37cb
TH
188/*
189 * Flags for stats to display with "zpool iostats"
190 */
191enum iostat_type {
192 IOS_DEFAULT = 0,
193 IOS_LATENCY = 1,
194 IOS_QUEUES = 2,
195 IOS_L_HISTO = 3,
7e945072 196 IOS_RQ_HISTO = 4,
193a37cb
TH
197 IOS_COUNT, /* always last element */
198};
199
200/* iostat_type entries as bitmasks */
201#define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
202#define IOS_LATENCY_M (1ULL << IOS_LATENCY)
203#define IOS_QUEUES_M (1ULL << IOS_QUEUES)
204#define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
7e945072
TH
205#define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
206
207/* Mask of all the histo bits */
208#define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
209
210/*
211 * Lookup table for iostat flags to nvlist names. Basically a list
212 * of all the nvlists a flag requires. Also specifies the order in
213 * which data gets printed in zpool iostat.
214 */
00888c08 215static const char *vsx_type_to_nvlist[IOS_COUNT][15] = {
7e945072
TH
216 [IOS_L_HISTO] = {
217 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
218 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
219 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
220 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
221 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
222 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
223 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
224 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
225 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
1b939560 226 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
00888c08 227 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
7e945072
TH
228 NULL},
229 [IOS_LATENCY] = {
230 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
231 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
232 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
233 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
1b939560 234 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
00888c08 235 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
7e945072
TH
236 NULL},
237 [IOS_QUEUES] = {
238 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
239 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
240 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
241 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
242 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
1b939560 243 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
00888c08 244 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
7e945072
TH
245 NULL},
246 [IOS_RQ_HISTO] = {
247 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
248 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
249 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
250 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
251 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
252 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
253 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
254 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
255 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
256 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
1b939560
BB
257 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
258 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
00888c08
TB
259 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
260 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
7e945072
TH
261 NULL},
262};
263
264
265/*
266 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
267 * Right now, only one histo bit is ever set at one time, so we can
268 * just do a highbit64(a)
269 */
270#define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
193a37cb 271
34dc7c2f
BB
272typedef struct zpool_command {
273 const char *name;
274 int (*func)(int, char **);
275 zpool_help_t usage;
276} zpool_command_t;
277
278/*
279 * Master command table. Each ZFS command has a name, associated function, and
280 * usage message. The usage messages need to be internationalized, so we have
281 * to have a function to return the usage message based on a command index.
282 *
283 * These commands are organized according to how they are displayed in the usage
284 * message. An empty command (one with a NULL name) indicates an empty line in
285 * the generic usage message.
286 */
287static zpool_command_t command_table[] = {
50478c6d
T
288 { "version", zpool_do_version, HELP_VERSION },
289 { NULL },
34dc7c2f
BB
290 { "create", zpool_do_create, HELP_CREATE },
291 { "destroy", zpool_do_destroy, HELP_DESTROY },
292 { NULL },
293 { "add", zpool_do_add, HELP_ADD },
294 { "remove", zpool_do_remove, HELP_REMOVE },
295 { NULL },
131cc95c
DK
296 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
297 { NULL },
d2734cce
SD
298 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
299 { NULL },
34dc7c2f
BB
300 { "list", zpool_do_list, HELP_LIST },
301 { "iostat", zpool_do_iostat, HELP_IOSTAT },
302 { "status", zpool_do_status, HELP_STATUS },
303 { NULL },
304 { "online", zpool_do_online, HELP_ONLINE },
305 { "offline", zpool_do_offline, HELP_OFFLINE },
306 { "clear", zpool_do_clear, HELP_CLEAR },
1bd201e7 307 { "reopen", zpool_do_reopen, HELP_REOPEN },
34dc7c2f
BB
308 { NULL },
309 { "attach", zpool_do_attach, HELP_ATTACH },
310 { "detach", zpool_do_detach, HELP_DETACH },
311 { "replace", zpool_do_replace, HELP_REPLACE },
428870ff 312 { "split", zpool_do_split, HELP_SPLIT },
34dc7c2f 313 { NULL },
619f0976 314 { "initialize", zpool_do_initialize, HELP_INITIALIZE },
80a91e74 315 { "resilver", zpool_do_resilver, HELP_RESILVER },
1b939560
BB
316 { "scrub", zpool_do_scrub, HELP_SCRUB },
317 { "trim", zpool_do_trim, HELP_TRIM },
34dc7c2f
BB
318 { NULL },
319 { "import", zpool_do_import, HELP_IMPORT },
320 { "export", zpool_do_export, HELP_EXPORT },
321 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
3541dc6d 322 { "reguid", zpool_do_reguid, HELP_REGUID },
34dc7c2f
BB
323 { NULL },
324 { "history", zpool_do_history, HELP_HISTORY },
26685276
BB
325 { "events", zpool_do_events, HELP_EVENTS },
326 { NULL },
34dc7c2f
BB
327 { "get", zpool_do_get, HELP_GET },
328 { "set", zpool_do_set, HELP_SET },
bec1067d 329 { "sync", zpool_do_sync, HELP_SYNC },
e60e158e
JG
330 { NULL },
331 { "wait", zpool_do_wait, HELP_WAIT },
34dc7c2f
BB
332};
333
193a37cb 334#define NCOMMAND (ARRAY_SIZE(command_table))
34dc7c2f 335
cc99f275
DB
336#define VDEV_ALLOC_CLASS_LOGS "logs"
337
6f1ffb06 338static zpool_command_t *current_command;
2a673e76 339static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
34dc7c2f 340static char history_str[HIS_MAX_RECORD_LEN];
6f1ffb06 341static boolean_t log_history = B_TRUE;
428870ff
BB
342static uint_t timestamp_fmt = NODATE;
343
34dc7c2f 344static const char *
e9aa730c
GM
345get_usage(zpool_help_t idx)
346{
34dc7c2f
BB
347 switch (idx) {
348 case HELP_ADD:
a77f29f9 349 return (gettext("\tadd [-fgLnP] [-o property=value] "
df831108 350 "<pool> <vdev> ...\n"));
34dc7c2f 351 case HELP_ATTACH:
9a49d3f3 352 return (gettext("\tattach [-fsw] [-o property=value] "
df831108 353 "<pool> <device> <new-device>\n"));
34dc7c2f 354 case HELP_CLEAR:
428870ff 355 return (gettext("\tclear [-nF] <pool> [device]\n"));
34dc7c2f 356 case HELP_CREATE:
9ae529ec 357 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
b128c09f 358 "\t [-O file-system-property=value] ... \n"
34dc7c2f 359 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
d2734cce 360 case HELP_CHECKPOINT:
e60e158e 361 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
34dc7c2f
BB
362 case HELP_DESTROY:
363 return (gettext("\tdestroy [-f] <pool>\n"));
364 case HELP_DETACH:
365 return (gettext("\tdetach <pool> <device>\n"));
366 case HELP_EXPORT:
859735c0 367 return (gettext("\texport [-af] <pool> ...\n"));
34dc7c2f
BB
368 case HELP_HISTORY:
369 return (gettext("\thistory [-il] [<pool>] ...\n"));
370 case HELP_IMPORT:
371 return (gettext("\timport [-d dir] [-D]\n"
372 "\timport [-o mntopts] [-o property=value] ... \n"
b5256303 373 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
572e2857 374 "[-R root] [-F [-n]] -a\n"
34dc7c2f 375 "\timport [-o mntopts] [-o property=value] ... \n"
b5256303 376 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
572e2857 377 "[-R root] [-F [-n]]\n"
d2734cce 378 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
34dc7c2f 379 case HELP_IOSTAT:
d6418de0
TH
380 return (gettext("\tiostat [[[-c [script1,script2,...]"
381 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
382 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
8fccfa8e 383 " [[-n] interval [count]]\n"));
131cc95c
DK
384 case HELP_LABELCLEAR:
385 return (gettext("\tlabelclear [-f] <vdev>\n"));
34dc7c2f 386 case HELP_LIST:
2a8b84b7 387 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
63f88c12 388 "[-T d|u] [pool] ... \n"
389 "\t [interval [count]]\n"));
34dc7c2f 390 case HELP_OFFLINE:
4a283c7f 391 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
34dc7c2f 392 case HELP_ONLINE:
aebfb848 393 return (gettext("\tonline [-e] <pool> <device> ...\n"));
34dc7c2f 394 case HELP_REPLACE:
9a49d3f3 395 return (gettext("\treplace [-fsw] [-o property=value] "
628668a3 396 "<pool> <device> [new-device]\n"));
34dc7c2f 397 case HELP_REMOVE:
e60e158e 398 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
1bd201e7 399 case HELP_REOPEN:
d3f2cd7e 400 return (gettext("\treopen [-n] <pool>\n"));
619f0976 401 case HELP_INITIALIZE:
e60e158e 402 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
a769fb53 403 "[<device> ...]\n"));
34dc7c2f 404 case HELP_SCRUB:
e60e158e 405 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
80a91e74
TC
406 case HELP_RESILVER:
407 return (gettext("\tresilver <pool> ...\n"));
1b939560 408 case HELP_TRIM:
2288d419 409 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
1b939560 410 "[<device> ...]\n"));
34dc7c2f 411 case HELP_STATUS:
ad796b8a 412 return (gettext("\tstatus [-c [script1,script2,...]] "
43977948 413 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
63f88c12 414 "\t [interval [count]]\n"));
34dc7c2f
BB
415 case HELP_UPGRADE:
416 return (gettext("\tupgrade\n"
417 "\tupgrade -v\n"
418 "\tupgrade [-V version] <-a | pool ...>\n"));
26685276 419 case HELP_EVENTS:
88f9c939 420 return (gettext("\tevents [-vHf [pool] | -c]\n"));
34dc7c2f 421 case HELP_GET:
2a8b84b7
AS
422 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
423 "<\"all\" | property[,...]> <pool> ...\n"));
34dc7c2f
BB
424 case HELP_SET:
425 return (gettext("\tset <property=value> <pool> \n"));
428870ff 426 case HELP_SPLIT:
b5256303 427 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
428870ff
BB
428 "\t [-o property=value] <pool> <newpool> "
429 "[<device> ...]\n"));
3541dc6d
GA
430 case HELP_REGUID:
431 return (gettext("\treguid <pool>\n"));
bec1067d
AP
432 case HELP_SYNC:
433 return (gettext("\tsync [pool] ...\n"));
50478c6d
T
434 case HELP_VERSION:
435 return (gettext("\tversion\n"));
e60e158e
JG
436 case HELP_WAIT:
437 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
438 "<pool> [interval]\n"));
90f1c3c9
AZ
439 default:
440 __builtin_unreachable();
34dc7c2f 441 }
34dc7c2f
BB
442}
443
619f0976
GW
444static void
445zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
446{
447 uint_t children = 0;
448 nvlist_t **child;
449 uint_t i;
450
451 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
452 &child, &children);
453
454 if (children == 0) {
1b939560
BB
455 char *path = zpool_vdev_name(g_zfs, zhp, nvroot,
456 VDEV_NAME_PATH);
457
a84c92f9
NH
458 if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 &&
459 strcmp(path, VDEV_TYPE_HOLE) != 0)
1b939560
BB
460 fnvlist_add_boolean(res, path);
461
619f0976
GW
462 free(path);
463 return;
464 }
465
466 for (i = 0; i < children; i++) {
467 zpool_collect_leaves(zhp, child[i], res);
468 }
469}
34dc7c2f
BB
470
471/*
472 * Callback routine that will print out a pool property value.
473 */
474static int
2a673e76 475print_pool_prop_cb(int prop, void *cb)
34dc7c2f
BB
476{
477 FILE *fp = cb;
478
cc99f275 479 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
34dc7c2f
BB
480
481 if (zpool_prop_readonly(prop))
482 (void) fprintf(fp, " NO ");
483 else
428870ff 484 (void) fprintf(fp, " YES ");
34dc7c2f
BB
485
486 if (zpool_prop_values(prop) == NULL)
487 (void) fprintf(fp, "-\n");
488 else
489 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
490
491 return (ZPROP_CONT);
492}
493
2a673e76
AJ
494/*
495 * Callback routine that will print out a vdev property value.
496 */
497static int
498print_vdev_prop_cb(int prop, void *cb)
499{
500 FILE *fp = cb;
501
502 (void) fprintf(fp, "\t%-19s ", vdev_prop_to_name(prop));
503
504 if (vdev_prop_readonly(prop))
505 (void) fprintf(fp, " NO ");
506 else
507 (void) fprintf(fp, " YES ");
508
509 if (vdev_prop_values(prop) == NULL)
510 (void) fprintf(fp, "-\n");
511 else
512 (void) fprintf(fp, "%s\n", vdev_prop_values(prop));
513
514 return (ZPROP_CONT);
515}
516
34dc7c2f
BB
517/*
518 * Display usage message. If we're inside a command, display only the usage for
519 * that command. Otherwise, iterate over the entire command table and display
520 * a complete usage message.
521 */
65c7cc49 522static void
34dc7c2f
BB
523usage(boolean_t requested)
524{
525 FILE *fp = requested ? stdout : stderr;
526
527 if (current_command == NULL) {
528 int i;
529
530 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
531 (void) fprintf(fp,
532 gettext("where 'command' is one of the following:\n\n"));
533
534 for (i = 0; i < NCOMMAND; i++) {
535 if (command_table[i].name == NULL)
536 (void) fprintf(fp, "\n");
537 else
538 (void) fprintf(fp, "%s",
539 get_usage(command_table[i].usage));
540 }
541 } else {
542 (void) fprintf(fp, gettext("usage:\n"));
543 (void) fprintf(fp, "%s", get_usage(current_command->usage));
544 }
545
546 if (current_command != NULL &&
2a673e76 547 current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) &&
34dc7c2f
BB
548 ((strcmp(current_command->name, "set") == 0) ||
549 (strcmp(current_command->name, "get") == 0) ||
550 (strcmp(current_command->name, "list") == 0))) {
551
552 (void) fprintf(fp,
553 gettext("\nthe following properties are supported:\n"));
554
cc99f275 555 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
34dc7c2f
BB
556 "PROPERTY", "EDIT", "VALUES");
557
558 /* Iterate over all properties */
2a673e76
AJ
559 if (current_prop_type == ZFS_TYPE_POOL) {
560 (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE,
561 B_TRUE, current_prop_type);
9ae529ec 562
2a673e76
AJ
563 (void) fprintf(fp, "\t%-19s ", "feature@...");
564 (void) fprintf(fp, "YES "
565 "disabled | enabled | active\n");
9ae529ec 566
2a673e76
AJ
567 (void) fprintf(fp, gettext("\nThe feature@ properties "
568 "must be appended with a feature name.\n"
569 "See zpool-features(7).\n"));
570 } else if (current_prop_type == ZFS_TYPE_VDEV) {
571 (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE,
572 B_TRUE, current_prop_type);
573 }
34dc7c2f
BB
574 }
575
576 /*
577 * See comments at end of main().
578 */
579 if (getenv("ZFS_ABORT") != NULL) {
580 (void) printf("dumping core by request\n");
581 abort();
582 }
583
584 exit(requested ? 0 : 2);
585}
586
619f0976 587/*
e60e158e 588 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
619f0976
GW
589 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
590 * if none specified.
591 *
592 * -c Cancel. Ends active initializing.
593 * -s Suspend. Initializing can then be restarted with no flags.
e60e158e 594 * -w Wait. Blocks until initializing has completed.
619f0976
GW
595 */
596int
597zpool_do_initialize(int argc, char **argv)
598{
599 int c;
600 char *poolname;
601 zpool_handle_t *zhp;
602 nvlist_t *vdevs;
603 int err = 0;
e60e158e 604 boolean_t wait = B_FALSE;
619f0976
GW
605
606 struct option long_options[] = {
607 {"cancel", no_argument, NULL, 'c'},
608 {"suspend", no_argument, NULL, 's'},
e60e158e 609 {"wait", no_argument, NULL, 'w'},
619f0976
GW
610 {0, 0, 0, 0}
611 };
612
1b939560 613 pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
e60e158e 614 while ((c = getopt_long(argc, argv, "csw", long_options, NULL)) != -1) {
619f0976
GW
615 switch (c) {
616 case 'c':
1b939560 617 if (cmd_type != POOL_INITIALIZE_START &&
a769fb53 618 cmd_type != POOL_INITIALIZE_CANCEL) {
619f0976
GW
619 (void) fprintf(stderr, gettext("-c cannot be "
620 "combined with other options\n"));
621 usage(B_FALSE);
622 }
623 cmd_type = POOL_INITIALIZE_CANCEL;
624 break;
625 case 's':
1b939560 626 if (cmd_type != POOL_INITIALIZE_START &&
a769fb53 627 cmd_type != POOL_INITIALIZE_SUSPEND) {
619f0976
GW
628 (void) fprintf(stderr, gettext("-s cannot be "
629 "combined with other options\n"));
630 usage(B_FALSE);
631 }
632 cmd_type = POOL_INITIALIZE_SUSPEND;
633 break;
e60e158e
JG
634 case 'w':
635 wait = B_TRUE;
636 break;
619f0976
GW
637 case '?':
638 if (optopt != 0) {
639 (void) fprintf(stderr,
640 gettext("invalid option '%c'\n"), optopt);
641 } else {
642 (void) fprintf(stderr,
643 gettext("invalid option '%s'\n"),
644 argv[optind - 1]);
645 }
646 usage(B_FALSE);
647 }
648 }
649
650 argc -= optind;
651 argv += optind;
652
653 if (argc < 1) {
654 (void) fprintf(stderr, gettext("missing pool name argument\n"));
655 usage(B_FALSE);
656 return (-1);
657 }
658
e60e158e
JG
659 if (wait && (cmd_type != POOL_INITIALIZE_START)) {
660 (void) fprintf(stderr, gettext("-w cannot be used with -c or "
661 "-s\n"));
662 usage(B_FALSE);
663 }
664
619f0976
GW
665 poolname = argv[0];
666 zhp = zpool_open(g_zfs, poolname);
667 if (zhp == NULL)
668 return (-1);
669
670 vdevs = fnvlist_alloc();
671 if (argc == 1) {
672 /* no individual leaf vdevs specified, so add them all */
673 nvlist_t *config = zpool_get_config(zhp, NULL);
674 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
675 ZPOOL_CONFIG_VDEV_TREE);
676 zpool_collect_leaves(zhp, nvroot, vdevs);
677 } else {
1b939560 678 for (int i = 1; i < argc; i++) {
619f0976
GW
679 fnvlist_add_boolean(vdevs, argv[i]);
680 }
681 }
682
e60e158e
JG
683 if (wait)
684 err = zpool_initialize_wait(zhp, cmd_type, vdevs);
685 else
686 err = zpool_initialize(zhp, cmd_type, vdevs);
619f0976
GW
687
688 fnvlist_free(vdevs);
689 zpool_close(zhp);
690
691 return (err);
692}
693
cc99f275
DB
694/*
695 * print a pool vdev config for dry runs
696 */
697static void
34dc7c2f 698print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
cc99f275 699 const char *match, int name_flags)
34dc7c2f
BB
700{
701 nvlist_t **child;
702 uint_t c, children;
703 char *vname;
cc99f275 704 boolean_t printed = B_FALSE;
34dc7c2f
BB
705
706 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
cc99f275
DB
707 &child, &children) != 0) {
708 if (name != NULL)
709 (void) printf("\t%*s%s\n", indent, "", name);
34dc7c2f 710 return;
cc99f275 711 }
34dc7c2f
BB
712
713 for (c = 0; c < children; c++) {
0cb40fa3 714 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
cc99f275 715 char *class = "";
34dc7c2f 716
0cb40fa3
AF
717 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
718 &is_hole);
719
720 if (is_hole == B_TRUE) {
721 continue;
722 }
723
34dc7c2f
BB
724 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
725 &is_log);
cc99f275
DB
726 if (is_log)
727 class = VDEV_ALLOC_BIAS_LOG;
728 (void) nvlist_lookup_string(child[c],
729 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
730 if (strcmp(match, class) != 0)
34dc7c2f
BB
731 continue;
732
cc99f275
DB
733 if (!printed && name != NULL) {
734 (void) printf("\t%*s%s\n", indent, "", name);
735 printed = B_TRUE;
736 }
d2f3e292 737 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
cc99f275
DB
738 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
739 name_flags);
34dc7c2f
BB
740 free(vname);
741 }
742}
743
0cb40fa3
AF
744/*
745 * Print the list of l2cache devices for dry runs.
746 */
747static void
748print_cache_list(nvlist_t *nv, int indent)
749{
750 nvlist_t **child;
751 uint_t c, children;
752
753 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
754 &child, &children) == 0 && children > 0) {
755 (void) printf("\t%*s%s\n", indent, "", "cache");
756 } else {
757 return;
758 }
759 for (c = 0; c < children; c++) {
760 char *vname;
761
762 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
763 (void) printf("\t%*s%s\n", indent + 2, "", vname);
764 free(vname);
765 }
766}
767
768/*
769 * Print the list of spares for dry runs.
770 */
771static void
772print_spare_list(nvlist_t *nv, int indent)
773{
774 nvlist_t **child;
775 uint_t c, children;
776
777 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
778 &child, &children) == 0 && children > 0) {
779 (void) printf("\t%*s%s\n", indent, "", "spares");
780 } else {
781 return;
782 }
783 for (c = 0; c < children; c++) {
784 char *vname;
785
786 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
787 (void) printf("\t%*s%s\n", indent + 2, "", vname);
788 free(vname);
789 }
790}
791
b9b24bb4
CS
792static boolean_t
793prop_list_contains_feature(nvlist_t *proplist)
794{
795 nvpair_t *nvp;
796 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
797 nvp = nvlist_next_nvpair(proplist, nvp)) {
798 if (zpool_prop_feature(nvpair_name(nvp)))
799 return (B_TRUE);
800 }
801 return (B_FALSE);
802}
803
34dc7c2f
BB
804/*
805 * Add a property pair (name, string-value) into a property nvlist.
806 */
807static int
b128c09f
BB
808add_prop_list(const char *propname, char *propval, nvlist_t **props,
809 boolean_t poolprop)
34dc7c2f 810{
31864e3d 811 zpool_prop_t prop = ZPOOL_PROP_INVAL;
34dc7c2f 812 nvlist_t *proplist;
b128c09f
BB
813 const char *normnm;
814 char *strval;
34dc7c2f
BB
815
816 if (*props == NULL &&
817 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
818 (void) fprintf(stderr,
819 gettext("internal error: out of memory\n"));
820 return (1);
821 }
822
823 proplist = *props;
824
b128c09f 825 if (poolprop) {
b9b24bb4 826 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
e086db16 827 const char *cname =
658fb802 828 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY);
b9b24bb4 829
31864e3d 830 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
2a673e76
AJ
831 (!zpool_prop_feature(propname) &&
832 !zpool_prop_vdev(propname))) {
b128c09f 833 (void) fprintf(stderr, gettext("property '%s' is "
2a673e76 834 "not a valid pool or vdev property\n"), propname);
b128c09f
BB
835 return (2);
836 }
b9b24bb4
CS
837
838 /*
839 * feature@ properties and version should not be specified
840 * at the same time.
841 */
31864e3d 842 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
b9b24bb4
CS
843 nvlist_exists(proplist, vname)) ||
844 (prop == ZPOOL_PROP_VERSION &&
845 prop_list_contains_feature(proplist))) {
846 (void) fprintf(stderr, gettext("'feature@' and "
847 "'version' properties cannot be specified "
848 "together\n"));
849 return (2);
850 }
851
658fb802 852 /*
e086db16
CB
853 * if version is specified, only "legacy" compatibility
854 * may be requested
658fb802
CB
855 */
856 if ((prop == ZPOOL_PROP_COMPATIBILITY &&
e086db16 857 strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 &&
658fb802
CB
858 nvlist_exists(proplist, vname)) ||
859 (prop == ZPOOL_PROP_VERSION &&
e086db16
CB
860 nvlist_exists(proplist, cname) &&
861 strcmp(fnvlist_lookup_string(proplist, cname),
862 ZPOOL_COMPAT_LEGACY) != 0)) {
863 (void) fprintf(stderr, gettext("when 'version' is "
864 "specified, the 'compatibility' feature may only "
865 "be set to '" ZPOOL_COMPAT_LEGACY "'\n"));
658fb802
CB
866 return (2);
867 }
b9b24bb4 868
2a673e76 869 if (zpool_prop_feature(propname) || zpool_prop_vdev(propname))
9ae529ec
CS
870 normnm = propname;
871 else
872 normnm = zpool_prop_to_name(prop);
b128c09f 873 } else {
5140a58f 874 zfs_prop_t fsprop = zfs_name_to_prop(propname);
875
876 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
877 B_FALSE)) {
878 normnm = zfs_prop_to_name(fsprop);
879 } else if (zfs_prop_user(propname) ||
880 zfs_prop_userquota(propname)) {
9babb374 881 normnm = propname;
5140a58f 882 } else {
883 (void) fprintf(stderr, gettext("property '%s' is "
884 "not a valid filesystem property\n"), propname);
885 return (2);
b128c09f 886 }
34dc7c2f
BB
887 }
888
b128c09f
BB
889 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
890 prop != ZPOOL_PROP_CACHEFILE) {
34dc7c2f
BB
891 (void) fprintf(stderr, gettext("property '%s' "
892 "specified multiple times\n"), propname);
893 return (2);
894 }
895
b128c09f 896 if (nvlist_add_string(proplist, normnm, propval) != 0) {
34dc7c2f
BB
897 (void) fprintf(stderr, gettext("internal "
898 "error: out of memory\n"));
899 return (1);
900 }
901
902 return (0);
903}
904
2f3ec900
RY
905/*
906 * Set a default property pair (name, string-value) in a property nvlist
907 */
908static int
e40ca391 909add_prop_list_default(const char *propname, char *propval, nvlist_t **props)
2f3ec900
RY
910{
911 char *pval;
912
913 if (nvlist_lookup_string(*props, propname, &pval) == 0)
914 return (0);
915
916 return (add_prop_list(propname, propval, props, B_TRUE));
917}
918
34dc7c2f 919/*
a77f29f9 920 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
34dc7c2f
BB
921 *
922 * -f Force addition of devices, even if they appear in use
d2f3e292
RY
923 * -g Display guid for individual vdev name.
924 * -L Follow links when resolving vdev path name.
34dc7c2f
BB
925 * -n Do not add the devices, but display the resulting layout if
926 * they were to be added.
df831108 927 * -o Set property=value.
a77f29f9 928 * -P Display full path for vdev name.
34dc7c2f
BB
929 *
930 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
227d3793 931 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
34dc7c2f
BB
932 * libzfs.
933 */
934int
935zpool_do_add(int argc, char **argv)
936{
937 boolean_t force = B_FALSE;
938 boolean_t dryrun = B_FALSE;
d2f3e292 939 int name_flags = 0;
34dc7c2f
BB
940 int c;
941 nvlist_t *nvroot;
942 char *poolname;
943 int ret;
944 zpool_handle_t *zhp;
945 nvlist_t *config;
df831108
CP
946 nvlist_t *props = NULL;
947 char *propval;
34dc7c2f
BB
948
949 /* check options */
a77f29f9 950 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
34dc7c2f
BB
951 switch (c) {
952 case 'f':
953 force = B_TRUE;
954 break;
d2f3e292
RY
955 case 'g':
956 name_flags |= VDEV_NAME_GUID;
957 break;
958 case 'L':
959 name_flags |= VDEV_NAME_FOLLOW_LINKS;
960 break;
34dc7c2f
BB
961 case 'n':
962 dryrun = B_TRUE;
963 break;
df831108
CP
964 case 'o':
965 if ((propval = strchr(optarg, '=')) == NULL) {
966 (void) fprintf(stderr, gettext("missing "
967 "'=' for -o option\n"));
968 usage(B_FALSE);
969 }
970 *propval = '\0';
971 propval++;
972
973 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
974 (add_prop_list(optarg, propval, &props, B_TRUE)))
975 usage(B_FALSE);
976 break;
a77f29f9 977 case 'P':
d2f3e292
RY
978 name_flags |= VDEV_NAME_PATH;
979 break;
34dc7c2f
BB
980 case '?':
981 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
982 optopt);
983 usage(B_FALSE);
984 }
985 }
986
987 argc -= optind;
988 argv += optind;
989
990 /* get pool name and check number of arguments */
991 if (argc < 1) {
992 (void) fprintf(stderr, gettext("missing pool name argument\n"));
993 usage(B_FALSE);
994 }
995 if (argc < 2) {
996 (void) fprintf(stderr, gettext("missing vdev specification\n"));
997 usage(B_FALSE);
998 }
999
1000 poolname = argv[0];
1001
1002 argc--;
1003 argv++;
1004
1005 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1006 return (1);
1007
1008 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
1009 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
1010 poolname);
1011 zpool_close(zhp);
1012 return (1);
1013 }
1014
dddef7d6 1015 /* unless manually specified use "ashift" pool property (if set) */
1016 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
1017 int intval;
1018 zprop_source_t src;
1019 char strval[ZPOOL_MAXPROPLEN];
1020
1021 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
1022 if (src != ZPROP_SRC_DEFAULT) {
1023 (void) sprintf(strval, "%" PRId32, intval);
1024 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
1025 &props, B_TRUE) == 0);
1026 }
1027 }
1028
227d3793 1029 /* pass off to make_root_vdev for processing */
df831108 1030 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
b128c09f 1031 argc, argv);
34dc7c2f
BB
1032 if (nvroot == NULL) {
1033 zpool_close(zhp);
1034 return (1);
1035 }
1036
1037 if (dryrun) {
1038 nvlist_t *poolnvroot;
0cb40fa3
AF
1039 nvlist_t **l2child, **sparechild;
1040 uint_t l2children, sparechildren, c;
e02b533e 1041 char *vname;
0cb40fa3 1042 boolean_t hadcache = B_FALSE, hadspare = B_FALSE;
34dc7c2f
BB
1043
1044 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1045 &poolnvroot) == 0);
1046
1047 (void) printf(gettext("would update '%s' to the following "
0cb40fa3 1048 "configuration:\n\n"), zpool_get_name(zhp));
34dc7c2f
BB
1049
1050 /* print original main pool and new tree */
cc99f275
DB
1051 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
1052 name_flags | VDEV_NAME_TYPE_ID);
1053 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
1054
1055 /* print other classes: 'dedup', 'special', and 'log' */
c24fa4b1 1056 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1057 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
1058 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1059 print_vdev_tree(zhp, NULL, nvroot, 0,
1060 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1061 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1062 print_vdev_tree(zhp, "dedup", nvroot, 0,
1063 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1064 }
cc99f275 1065
c24fa4b1 1066 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1067 print_vdev_tree(zhp, "special", poolnvroot, 0,
1068 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1069 print_vdev_tree(zhp, NULL, nvroot, 0,
1070 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1071 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1072 print_vdev_tree(zhp, "special", nvroot, 0,
1073 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1074 }
cc99f275 1075
c24fa4b1 1076 if (num_logs(poolnvroot) > 0) {
1077 print_vdev_tree(zhp, "logs", poolnvroot, 0,
1078 VDEV_ALLOC_BIAS_LOG, name_flags);
1079 print_vdev_tree(zhp, NULL, nvroot, 0,
1080 VDEV_ALLOC_BIAS_LOG, name_flags);
1081 } else if (num_logs(nvroot) > 0) {
1082 print_vdev_tree(zhp, "logs", nvroot, 0,
1083 VDEV_ALLOC_BIAS_LOG, name_flags);
1084 }
34dc7c2f 1085
e02b533e
TC
1086 /* Do the same for the caches */
1087 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
1088 &l2child, &l2children) == 0 && l2children) {
1089 hadcache = B_TRUE;
1090 (void) printf(gettext("\tcache\n"));
1091 for (c = 0; c < l2children; c++) {
1092 vname = zpool_vdev_name(g_zfs, NULL,
d2f3e292 1093 l2child[c], name_flags);
e02b533e
TC
1094 (void) printf("\t %s\n", vname);
1095 free(vname);
1096 }
1097 }
1098 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1099 &l2child, &l2children) == 0 && l2children) {
1100 if (!hadcache)
1101 (void) printf(gettext("\tcache\n"));
1102 for (c = 0; c < l2children; c++) {
1103 vname = zpool_vdev_name(g_zfs, NULL,
d2f3e292 1104 l2child[c], name_flags);
e02b533e
TC
1105 (void) printf("\t %s\n", vname);
1106 free(vname);
1107 }
1108 }
bf169e9f 1109 /* And finally the spares */
0cb40fa3
AF
1110 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES,
1111 &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1112 hadspare = B_TRUE;
1113 (void) printf(gettext("\tspares\n"));
1114 for (c = 0; c < sparechildren; c++) {
1115 vname = zpool_vdev_name(g_zfs, NULL,
1116 sparechild[c], name_flags);
1117 (void) printf("\t %s\n", vname);
1118 free(vname);
1119 }
1120 }
1121 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1122 &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1123 if (!hadspare)
1124 (void) printf(gettext("\tspares\n"));
1125 for (c = 0; c < sparechildren; c++) {
1126 vname = zpool_vdev_name(g_zfs, NULL,
1127 sparechild[c], name_flags);
1128 (void) printf("\t %s\n", vname);
1129 free(vname);
1130 }
1131 }
e02b533e 1132
34dc7c2f
BB
1133 ret = 0;
1134 } else {
1135 ret = (zpool_add(zhp, nvroot) != 0);
1136 }
1137
df831108 1138 nvlist_free(props);
34dc7c2f
BB
1139 nvlist_free(nvroot);
1140 zpool_close(zhp);
1141
1142 return (ret);
1143}
1144
1145/*
e60e158e 1146 * zpool remove [-npsw] <pool> <vdev> ...
34dc7c2f 1147 *
a1d477c2 1148 * Removes the given vdev from the pool.
34dc7c2f
BB
1149 */
1150int
1151zpool_do_remove(int argc, char **argv)
1152{
1153 char *poolname;
1154 int i, ret = 0;
884385a0 1155 zpool_handle_t *zhp = NULL;
a1d477c2 1156 boolean_t stop = B_FALSE;
0869b74a 1157 int c;
a1d477c2
MA
1158 boolean_t noop = B_FALSE;
1159 boolean_t parsable = B_FALSE;
e60e158e 1160 boolean_t wait = B_FALSE;
34dc7c2f 1161
a1d477c2 1162 /* check options */
e60e158e 1163 while ((c = getopt(argc, argv, "npsw")) != -1) {
a1d477c2
MA
1164 switch (c) {
1165 case 'n':
1166 noop = B_TRUE;
1167 break;
1168 case 'p':
1169 parsable = B_TRUE;
1170 break;
1171 case 's':
1172 stop = B_TRUE;
1173 break;
e60e158e
JG
1174 case 'w':
1175 wait = B_TRUE;
1176 break;
a1d477c2
MA
1177 case '?':
1178 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1179 optopt);
1180 usage(B_FALSE);
1181 }
1182 }
1183
1184 argc -= optind;
1185 argv += optind;
34dc7c2f
BB
1186
1187 /* get pool name and check number of arguments */
1188 if (argc < 1) {
1189 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1190 usage(B_FALSE);
1191 }
34dc7c2f
BB
1192
1193 poolname = argv[0];
1194
1195 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1196 return (1);
1197
a1d477c2
MA
1198 if (stop && noop) {
1199 (void) fprintf(stderr, gettext("stop request ignored\n"));
1200 return (0);
1201 }
1202
1203 if (stop) {
1204 if (argc > 1) {
1205 (void) fprintf(stderr, gettext("too many arguments\n"));
1206 usage(B_FALSE);
1207 }
1208 if (zpool_vdev_remove_cancel(zhp) != 0)
34dc7c2f 1209 ret = 1;
e60e158e
JG
1210 if (wait) {
1211 (void) fprintf(stderr, gettext("invalid option "
1212 "combination: -w cannot be used with -s\n"));
1213 usage(B_FALSE);
1214 }
a1d477c2
MA
1215 } else {
1216 if (argc < 2) {
1217 (void) fprintf(stderr, gettext("missing device\n"));
1218 usage(B_FALSE);
1219 }
1220
1221 for (i = 1; i < argc; i++) {
1222 if (noop) {
1223 uint64_t size;
1224
1225 if (zpool_vdev_indirect_size(zhp, argv[i],
1226 &size) != 0) {
1227 ret = 1;
1228 break;
1229 }
1230 if (parsable) {
1231 (void) printf("%s %llu\n",
1232 argv[i], (unsigned long long)size);
1233 } else {
1234 char valstr[32];
1235 zfs_nicenum(size, valstr,
1236 sizeof (valstr));
1237 (void) printf("Memory that will be "
1238 "used after removing %s: %s\n",
1239 argv[i], valstr);
1240 }
1241 } else {
1242 if (zpool_vdev_remove(zhp, argv[i]) != 0)
1243 ret = 1;
1244 }
1245 }
e60e158e
JG
1246
1247 if (ret == 0 && wait)
1248 ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE);
34dc7c2f 1249 }
884385a0 1250 zpool_close(zhp);
34dc7c2f
BB
1251
1252 return (ret);
1253}
1254
4dc11532
TH
1255/*
1256 * Return 1 if a vdev is active (being used in a pool)
1257 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1258 *
1259 * This is useful for checking if a disk in an active pool is offlined or
1260 * faulted.
1261 */
1262static int
1263vdev_is_active(char *vdev_path)
1264{
1265 int fd;
1266 fd = open(vdev_path, O_EXCL);
1267 if (fd < 0) {
1268 return (1); /* cant open O_EXCL - disk is active */
1269 }
1270
1271 close(fd);
1272 return (0); /* disk is inactive in the pool */
1273}
1274
131cc95c 1275/*
dbb38f66
YP
1276 * zpool labelclear [-f] <vdev>
1277 *
1278 * -f Force clearing the label for the vdevs which are members of
1279 * the exported or foreign pools.
131cc95c
DK
1280 *
1281 * Verifies that the vdev is not active and zeros out the label information
1282 * on the device.
1283 */
1284int
1285zpool_do_labelclear(int argc, char **argv)
1286{
dbb38f66
YP
1287 char vdev[MAXPATHLEN];
1288 char *name = NULL;
1289 struct stat st;
131cc95c 1290 int c, fd = -1, ret = 0;
dbb38f66 1291 nvlist_t *config;
131cc95c
DK
1292 pool_state_t state;
1293 boolean_t inuse = B_FALSE;
1294 boolean_t force = B_FALSE;
1295
1296 /* check options */
1297 while ((c = getopt(argc, argv, "f")) != -1) {
1298 switch (c) {
1299 case 'f':
1300 force = B_TRUE;
1301 break;
1302 default:
1303 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1304 optopt);
1305 usage(B_FALSE);
1306 }
1307 }
1308
1309 argc -= optind;
1310 argv += optind;
1311
1312 /* get vdev name */
1313 if (argc < 1) {
dbb38f66
YP
1314 (void) fprintf(stderr, gettext("missing vdev name\n"));
1315 usage(B_FALSE);
1316 }
1317 if (argc > 1) {
1318 (void) fprintf(stderr, gettext("too many arguments\n"));
131cc95c
DK
1319 usage(B_FALSE);
1320 }
1321
dbb38f66
YP
1322 /*
1323 * Check if we were given absolute path and use it as is.
1324 * Otherwise if the provided vdev name doesn't point to a file,
1325 * try prepending expected disk paths and partition numbers.
1326 */
1327 (void) strlcpy(vdev, argv[0], sizeof (vdev));
1328 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
1329 int error;
1330
1331 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
1332 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
1333 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
1334 error = ENOENT;
1335 }
1336
1337 if (error || (stat(vdev, &st) != 0)) {
1338 (void) fprintf(stderr, gettext(
1339 "failed to find device %s, try specifying absolute "
1340 "path instead\n"), argv[0]);
1341 return (1);
1342 }
131cc95c
DK
1343 }
1344
dbb38f66
YP
1345 if ((fd = open(vdev, O_RDWR)) < 0) {
1346 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
1347 vdev, strerror(errno));
1348 return (1);
1349 }
131cc95c 1350
066da71e
BB
1351 /*
1352 * Flush all dirty pages for the block device. This should not be
1353 * fatal when the device does not support BLKFLSBUF as would be the
1354 * case for a file vdev.
1355 */
d46f0deb 1356 if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY))
a167aa7c
GDN
1357 (void) fprintf(stderr, gettext("failed to invalidate "
1358 "cache for %s: %s\n"), vdev, strerror(errno));
1359
066da71e 1360 if (zpool_read_label(fd, &config, NULL) != 0) {
131cc95c 1361 (void) fprintf(stderr,
066da71e 1362 gettext("failed to read label from %s\n"), vdev);
c721ba43
TH
1363 ret = 1;
1364 goto errout;
dbb38f66
YP
1365 }
1366 nvlist_free(config);
131cc95c 1367
dbb38f66
YP
1368 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1369 if (ret != 0) {
1370 (void) fprintf(stderr,
1371 gettext("failed to check state for %s\n"), vdev);
c721ba43
TH
1372 ret = 1;
1373 goto errout;
131cc95c
DK
1374 }
1375
dbb38f66
YP
1376 if (!inuse)
1377 goto wipe_label;
1378
1379 switch (state) {
1380 default:
1381 case POOL_STATE_ACTIVE:
1382 case POOL_STATE_SPARE:
1383 case POOL_STATE_L2CACHE:
4dc11532
TH
1384 /*
1385 * We allow the user to call 'zpool offline -f'
1386 * on an offlined disk in an active pool. We can check if
1387 * the disk is online by calling vdev_is_active().
1388 */
1389 if (force && !vdev_is_active(vdev))
1390 break;
1391
dbb38f66 1392 (void) fprintf(stderr, gettext(
4dc11532 1393 "%s is a member (%s) of pool \"%s\""),
dbb38f66 1394 vdev, zpool_pool_state_to_name(state), name);
4dc11532
TH
1395
1396 if (force) {
1397 (void) fprintf(stderr, gettext(
1398 ". Offline the disk first to clear its label."));
1399 }
1400 printf("\n");
dbb38f66
YP
1401 ret = 1;
1402 goto errout;
131cc95c 1403
dbb38f66
YP
1404 case POOL_STATE_EXPORTED:
1405 if (force)
131cc95c 1406 break;
dbb38f66
YP
1407 (void) fprintf(stderr, gettext(
1408 "use '-f' to override the following error:\n"
1409 "%s is a member of exported pool \"%s\"\n"),
1410 vdev, name);
1411 ret = 1;
1412 goto errout;
1413
1414 case POOL_STATE_POTENTIALLY_ACTIVE:
1415 if (force)
1416 break;
1417 (void) fprintf(stderr, gettext(
1418 "use '-f' to override the following error:\n"
1419 "%s is a member of potentially active pool \"%s\"\n"),
1420 vdev, name);
1421 ret = 1;
1422 goto errout;
1423
1424 case POOL_STATE_DESTROYED:
1425 /* inuse should never be set for a destroyed pool */
1426 assert(0);
1427 break;
131cc95c
DK
1428 }
1429
1430wipe_label:
dbb38f66
YP
1431 ret = zpool_clear_label(fd);
1432 if (ret != 0) {
131cc95c 1433 (void) fprintf(stderr,
dbb38f66 1434 gettext("failed to clear label for %s\n"), vdev);
131cc95c
DK
1435 }
1436
1437errout:
dbb38f66
YP
1438 free(name);
1439 (void) close(fd);
131cc95c
DK
1440
1441 return (ret);
1442}
1443
34dc7c2f 1444/*
9ae529ec 1445 * zpool create [-fnd] [-o property=value] ...
b128c09f
BB
1446 * [-O file-system-property=value] ...
1447 * [-R root] [-m mountpoint] <pool> <dev> ...
34dc7c2f
BB
1448 *
1449 * -f Force creation, even if devices appear in use
1450 * -n Do not create the pool, but display the resulting layout if it
1451 * were to be created.
1452 * -R Create a pool under an alternate root
1453 * -m Set default mountpoint for the root dataset. By default it's
9ae529ec 1454 * '/<pool>'
34dc7c2f 1455 * -o Set property=value.
e4010f27 1456 * -o Set feature@feature=enabled|disabled.
9ae529ec
CS
1457 * -d Don't automatically enable all supported pool features
1458 * (individual features can be enabled with -o).
b128c09f 1459 * -O Set fsproperty=value in the pool's root file system
34dc7c2f
BB
1460 *
1461 * Creates the named pool according to the given vdev specification. The
227d3793
RM
1462 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1463 * Once we get the nvlist back from make_root_vdev(), we either print out the
1464 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
34dc7c2f
BB
1465 */
1466int
1467zpool_do_create(int argc, char **argv)
1468{
1469 boolean_t force = B_FALSE;
1470 boolean_t dryrun = B_FALSE;
658fb802
CB
1471 boolean_t enable_pool_features = B_TRUE;
1472
34dc7c2f
BB
1473 int c;
1474 nvlist_t *nvroot = NULL;
1475 char *poolname;
023bbe6f 1476 char *tname = NULL;
34dc7c2f
BB
1477 int ret = 1;
1478 char *altroot = NULL;
658fb802 1479 char *compat = NULL;
34dc7c2f 1480 char *mountpoint = NULL;
b128c09f 1481 nvlist_t *fsprops = NULL;
34dc7c2f
BB
1482 nvlist_t *props = NULL;
1483 char *propval;
1484
1485 /* check options */
83e9986f 1486 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
34dc7c2f
BB
1487 switch (c) {
1488 case 'f':
1489 force = B_TRUE;
1490 break;
1491 case 'n':
1492 dryrun = B_TRUE;
1493 break;
9ae529ec 1494 case 'd':
658fb802 1495 enable_pool_features = B_FALSE;
9ae529ec 1496 break;
34dc7c2f
BB
1497 case 'R':
1498 altroot = optarg;
1499 if (add_prop_list(zpool_prop_to_name(
b128c09f 1500 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
34dc7c2f 1501 goto errout;
2f3ec900 1502 if (add_prop_list_default(zpool_prop_to_name(
e40ca391 1503 ZPOOL_PROP_CACHEFILE), "none", &props))
34dc7c2f
BB
1504 goto errout;
1505 break;
1506 case 'm':
7bc7f250 1507 /* Equivalent to -O mountpoint=optarg */
34dc7c2f
BB
1508 mountpoint = optarg;
1509 break;
1510 case 'o':
1511 if ((propval = strchr(optarg, '=')) == NULL) {
1512 (void) fprintf(stderr, gettext("missing "
1513 "'=' for -o option\n"));
1514 goto errout;
1515 }
1516 *propval = '\0';
1517 propval++;
1518
b128c09f
BB
1519 if (add_prop_list(optarg, propval, &props, B_TRUE))
1520 goto errout;
9ae529ec
CS
1521
1522 /*
1523 * If the user is creating a pool that doesn't support
1524 * feature flags, don't enable any features.
1525 */
1526 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1527 char *end;
1528 u_longlong_t ver;
1529
1530 ver = strtoull(propval, &end, 10);
1531 if (*end == '\0' &&
1532 ver < SPA_VERSION_FEATURES) {
658fb802 1533 enable_pool_features = B_FALSE;
9ae529ec
CS
1534 }
1535 }
3ac2794c
BB
1536 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1537 altroot = propval;
658fb802
CB
1538 if (zpool_name_to_prop(optarg) ==
1539 ZPOOL_PROP_COMPATIBILITY)
1540 compat = propval;
b128c09f
BB
1541 break;
1542 case 'O':
1543 if ((propval = strchr(optarg, '=')) == NULL) {
1544 (void) fprintf(stderr, gettext("missing "
1545 "'=' for -O option\n"));
1546 goto errout;
1547 }
1548 *propval = '\0';
1549 propval++;
1550
7bc7f250
WA
1551 /*
1552 * Mountpoints are checked and then added later.
1553 * Uniquely among properties, they can be specified
1554 * more than once, to avoid conflict with -m.
1555 */
1556 if (0 == strcmp(optarg,
1557 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1558 mountpoint = propval;
1559 } else if (add_prop_list(optarg, propval, &fsprops,
1560 B_FALSE)) {
34dc7c2f 1561 goto errout;
7bc7f250 1562 }
34dc7c2f 1563 break;
83e9986f
RY
1564 case 't':
1565 /*
1566 * Sanity check temporary pool name.
1567 */
1568 if (strchr(optarg, '/') != NULL) {
1569 (void) fprintf(stderr, gettext("cannot create "
1570 "'%s': invalid character '/' in temporary "
1571 "name\n"), optarg);
1572 (void) fprintf(stderr, gettext("use 'zfs "
1573 "create' to create a dataset\n"));
1574 goto errout;
1575 }
1576
1577 if (add_prop_list(zpool_prop_to_name(
1578 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1579 goto errout;
1580 if (add_prop_list_default(zpool_prop_to_name(
e40ca391 1581 ZPOOL_PROP_CACHEFILE), "none", &props))
83e9986f 1582 goto errout;
023bbe6f 1583 tname = optarg;
83e9986f 1584 break;
34dc7c2f
BB
1585 case ':':
1586 (void) fprintf(stderr, gettext("missing argument for "
1587 "'%c' option\n"), optopt);
1588 goto badusage;
1589 case '?':
1590 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1591 optopt);
1592 goto badusage;
1593 }
1594 }
1595
1596 argc -= optind;
1597 argv += optind;
1598
1599 /* get pool name and check number of arguments */
1600 if (argc < 1) {
1601 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1602 goto badusage;
1603 }
1604 if (argc < 2) {
1605 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1606 goto badusage;
1607 }
1608
1609 poolname = argv[0];
1610
1611 /*
1612 * As a special case, check for use of '/' in the name, and direct the
1613 * user to use 'zfs create' instead.
1614 */
1615 if (strchr(poolname, '/') != NULL) {
1616 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1617 "character '/' in pool name\n"), poolname);
1618 (void) fprintf(stderr, gettext("use 'zfs create' to "
1619 "create a dataset\n"));
1620 goto errout;
1621 }
1622
227d3793 1623 /* pass off to make_root_vdev for bulk processing */
df30f566 1624 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
b128c09f 1625 argc - 1, argv + 1);
34dc7c2f 1626 if (nvroot == NULL)
b128c09f 1627 goto errout;
34dc7c2f
BB
1628
1629 /* make_root_vdev() allows 0 toplevel children if there are spares */
1630 if (!zfs_allocatable_devs(nvroot)) {
1631 (void) fprintf(stderr, gettext("invalid vdev "
1632 "specification: at least one toplevel vdev must be "
1633 "specified\n"));
1634 goto errout;
1635 }
1636
34dc7c2f
BB
1637 if (altroot != NULL && altroot[0] != '/') {
1638 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1639 "must be an absolute path\n"), altroot);
1640 goto errout;
1641 }
1642
1643 /*
1644 * Check the validity of the mountpoint and direct the user to use the
1645 * '-m' mountpoint option if it looks like its in use.
1646 */
1647 if (mountpoint == NULL ||
1648 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1649 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1650 char buf[MAXPATHLEN];
1651 DIR *dirp;
1652
1653 if (mountpoint && mountpoint[0] != '/') {
1654 (void) fprintf(stderr, gettext("invalid mountpoint "
1655 "'%s': must be an absolute path, 'legacy', or "
1656 "'none'\n"), mountpoint);
1657 goto errout;
1658 }
1659
1660 if (mountpoint == NULL) {
1661 if (altroot != NULL)
1662 (void) snprintf(buf, sizeof (buf), "%s/%s",
1663 altroot, poolname);
1664 else
1665 (void) snprintf(buf, sizeof (buf), "/%s",
1666 poolname);
1667 } else {
1668 if (altroot != NULL)
1669 (void) snprintf(buf, sizeof (buf), "%s%s",
1670 altroot, mountpoint);
1671 else
1672 (void) snprintf(buf, sizeof (buf), "%s",
1673 mountpoint);
1674 }
1675
1676 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1677 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1678 "%s\n"), buf, strerror(errno));
1679 (void) fprintf(stderr, gettext("use '-m' "
1680 "option to provide a different default\n"));
1681 goto errout;
1682 } else if (dirp) {
1683 int count = 0;
1684
1685 while (count < 3 && readdir(dirp) != NULL)
1686 count++;
1687 (void) closedir(dirp);
1688
1689 if (count > 2) {
1690 (void) fprintf(stderr, gettext("mountpoint "
1691 "'%s' exists and is not empty\n"), buf);
1692 (void) fprintf(stderr, gettext("use '-m' "
1693 "option to provide a "
1694 "different default\n"));
1695 goto errout;
1696 }
1697 }
1698 }
1699
7bc7f250
WA
1700 /*
1701 * Now that the mountpoint's validity has been checked, ensure that
1702 * the property is set appropriately prior to creating the pool.
1703 */
1704 if (mountpoint != NULL) {
1705 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1706 mountpoint, &fsprops, B_FALSE);
1707 if (ret != 0)
1708 goto errout;
1709 }
1710
1711 ret = 1;
34dc7c2f
BB
1712 if (dryrun) {
1713 /*
1714 * For a dry run invocation, print out a basic message and run
1715 * through all the vdevs in the list and print out in an
1716 * appropriate hierarchy.
1717 */
1718 (void) printf(gettext("would create '%s' with the "
1719 "following layout:\n\n"), poolname);
1720
cc99f275
DB
1721 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1722 print_vdev_tree(NULL, "dedup", nvroot, 0,
1723 VDEV_ALLOC_BIAS_DEDUP, 0);
1724 print_vdev_tree(NULL, "special", nvroot, 0,
1725 VDEV_ALLOC_BIAS_SPECIAL, 0);
1726 print_vdev_tree(NULL, "logs", nvroot, 0,
1727 VDEV_ALLOC_BIAS_LOG, 0);
0cb40fa3
AF
1728 print_cache_list(nvroot, 0);
1729 print_spare_list(nvroot, 0);
34dc7c2f
BB
1730
1731 ret = 0;
1732 } else {
1733 /*
658fb802
CB
1734 * Load in feature set.
1735 * Note: if compatibility property not given, we'll have
1736 * NULL, which means 'all features'.
34dc7c2f 1737 */
658fb802
CB
1738 boolean_t requested_features[SPA_FEATURES];
1739 if (zpool_do_load_compat(compat, requested_features) !=
1740 ZPOOL_COMPATIBILITY_OK)
1741 goto errout;
1742
1743 /*
1744 * props contains list of features to enable.
1745 * For each feature:
1746 * - remove it if feature@name=disabled
1747 * - leave it there if feature@name=enabled
1748 * - add it if:
1749 * - enable_pool_features (ie: no '-d' or '-o version')
1750 * - it's supported by the kernel module
1751 * - it's in the requested feature set
e086db16 1752 * - warn if it's enabled but not in compat
658fb802
CB
1753 */
1754 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
e4010f27 1755 char propname[MAXPATHLEN];
1756 char *propval;
1757 zfeature_info_t *feat = &spa_feature_table[i];
9ae529ec 1758
e4010f27 1759 (void) snprintf(propname, sizeof (propname),
1760 "feature@%s", feat->fi_uname);
1761
e4010f27 1762 if (!nvlist_lookup_string(props, propname, &propval)) {
1763 if (strcmp(propval, ZFS_FEATURE_DISABLED) == 0)
1764 (void) nvlist_remove_all(props,
1765 propname);
e086db16
CB
1766 if (strcmp(propval,
1767 ZFS_FEATURE_ENABLED) == 0 &&
1768 !requested_features[i])
1769 (void) fprintf(stderr, gettext(
1770 "Warning: feature \"%s\" enabled "
1771 "but is not in specified "
1772 "'compatibility' feature set.\n"),
1773 feat->fi_uname);
658fb802
CB
1774 } else if (
1775 enable_pool_features &&
1776 feat->fi_zfs_mod_supported &&
1777 requested_features[i]) {
7bc7f250
WA
1778 ret = add_prop_list(propname,
1779 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1780 if (ret != 0)
9ae529ec
CS
1781 goto errout;
1782 }
1783 }
7bc7f250
WA
1784
1785 ret = 1;
b128c09f
BB
1786 if (zpool_create(g_zfs, poolname,
1787 nvroot, props, fsprops) == 0) {
023bbe6f 1788 zfs_handle_t *pool = zfs_open(g_zfs,
1789 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
34dc7c2f 1790 if (pool != NULL) {
c15d36c6 1791 if (zfs_mount(pool, NULL, 0) == 0) {
34dc7c2f 1792 ret = zfs_shareall(pool);
c15d36c6
GW
1793 zfs_commit_all_shares();
1794 }
34dc7c2f
BB
1795 zfs_close(pool);
1796 }
1797 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1798 (void) fprintf(stderr, gettext("pool name may have "
1799 "been omitted\n"));
1800 }
1801 }
1802
1803errout:
1804 nvlist_free(nvroot);
b128c09f 1805 nvlist_free(fsprops);
34dc7c2f
BB
1806 nvlist_free(props);
1807 return (ret);
1808badusage:
b128c09f 1809 nvlist_free(fsprops);
34dc7c2f
BB
1810 nvlist_free(props);
1811 usage(B_FALSE);
1812 return (2);
1813}
1814
1815/*
1816 * zpool destroy <pool>
1817 *
1818 * -f Forcefully unmount any datasets
1819 *
1820 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1821 */
1822int
1823zpool_do_destroy(int argc, char **argv)
1824{
1825 boolean_t force = B_FALSE;
1826 int c;
1827 char *pool;
1828 zpool_handle_t *zhp;
1829 int ret;
1830
1831 /* check options */
1832 while ((c = getopt(argc, argv, "f")) != -1) {
1833 switch (c) {
1834 case 'f':
1835 force = B_TRUE;
1836 break;
1837 case '?':
1838 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1839 optopt);
1840 usage(B_FALSE);
1841 }
1842 }
1843
1844 argc -= optind;
1845 argv += optind;
1846
1847 /* check arguments */
1848 if (argc < 1) {
1849 (void) fprintf(stderr, gettext("missing pool argument\n"));
1850 usage(B_FALSE);
1851 }
1852 if (argc > 1) {
1853 (void) fprintf(stderr, gettext("too many arguments\n"));
1854 usage(B_FALSE);
1855 }
1856
1857 pool = argv[0];
1858
1859 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1860 /*
1861 * As a special case, check for use of '/' in the name, and
1862 * direct the user to use 'zfs destroy' instead.
1863 */
1864 if (strchr(pool, '/') != NULL)
1865 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1866 "destroy a dataset\n"));
1867 return (1);
1868 }
1869
1870 if (zpool_disable_datasets(zhp, force) != 0) {
1871 (void) fprintf(stderr, gettext("could not destroy '%s': "
1872 "could not unmount datasets\n"), zpool_get_name(zhp));
a425f5bf 1873 zpool_close(zhp);
34dc7c2f
BB
1874 return (1);
1875 }
1876
6f1ffb06
MA
1877 /* The history must be logged as part of the export */
1878 log_history = B_FALSE;
1879
1880 ret = (zpool_destroy(zhp, history_str) != 0);
34dc7c2f
BB
1881
1882 zpool_close(zhp);
1883
1884 return (ret);
1885}
1886
859735c0
TF
1887typedef struct export_cbdata {
1888 boolean_t force;
1889 boolean_t hardforce;
1890} export_cbdata_t;
1891
1892/*
1893 * Export one pool
1894 */
65c7cc49 1895static int
859735c0
TF
1896zpool_export_one(zpool_handle_t *zhp, void *data)
1897{
1898 export_cbdata_t *cb = data;
1899
1900 if (zpool_disable_datasets(zhp, cb->force) != 0)
1901 return (1);
1902
1903 /* The history must be logged as part of the export */
1904 log_history = B_FALSE;
1905
1906 if (cb->hardforce) {
1907 if (zpool_export_force(zhp, history_str) != 0)
1908 return (1);
1909 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1910 return (1);
1911 }
1912
1913 return (0);
1914}
1915
34dc7c2f
BB
1916/*
1917 * zpool export [-f] <pool> ...
1918 *
859735c0 1919 * -a Export all pools
34dc7c2f
BB
1920 * -f Forcefully unmount datasets
1921 *
1922 * Export the given pools. By default, the command will attempt to cleanly
1923 * unmount any active datasets within the pool. If the '-f' flag is specified,
1924 * then the datasets will be forcefully unmounted.
1925 */
1926int
1927zpool_do_export(int argc, char **argv)
1928{
859735c0
TF
1929 export_cbdata_t cb;
1930 boolean_t do_all = B_FALSE;
34dc7c2f 1931 boolean_t force = B_FALSE;
fb5f0bc8 1932 boolean_t hardforce = B_FALSE;
859735c0 1933 int c, ret;
34dc7c2f
BB
1934
1935 /* check options */
859735c0 1936 while ((c = getopt(argc, argv, "afF")) != -1) {
34dc7c2f 1937 switch (c) {
859735c0
TF
1938 case 'a':
1939 do_all = B_TRUE;
1940 break;
34dc7c2f
BB
1941 case 'f':
1942 force = B_TRUE;
1943 break;
fb5f0bc8
BB
1944 case 'F':
1945 hardforce = B_TRUE;
1946 break;
34dc7c2f
BB
1947 case '?':
1948 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1949 optopt);
1950 usage(B_FALSE);
1951 }
1952 }
1953
859735c0
TF
1954 cb.force = force;
1955 cb.hardforce = hardforce;
34dc7c2f
BB
1956 argc -= optind;
1957 argv += optind;
1958
859735c0
TF
1959 if (do_all) {
1960 if (argc != 0) {
1961 (void) fprintf(stderr, gettext("too many arguments\n"));
1962 usage(B_FALSE);
1963 }
1964
1965 return (for_each_pool(argc, argv, B_TRUE, NULL,
2a673e76 1966 ZFS_TYPE_POOL, B_FALSE, zpool_export_one, &cb));
859735c0
TF
1967 }
1968
34dc7c2f
BB
1969 /* check arguments */
1970 if (argc < 1) {
1971 (void) fprintf(stderr, gettext("missing pool argument\n"));
1972 usage(B_FALSE);
1973 }
1974
2a673e76
AJ
1975 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
1976 B_FALSE, zpool_export_one, &cb);
34dc7c2f
BB
1977
1978 return (ret);
1979}
1980
1981/*
1982 * Given a vdev configuration, determine the maximum width needed for the device
1983 * name column.
1984 */
1985static int
d2f3e292
RY
1986max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1987 int name_flags)
34dc7c2f 1988{
d2f3e292 1989 char *name;
34dc7c2f
BB
1990 nvlist_t **child;
1991 uint_t c, children;
1992 int ret;
1993
5f20c145 1994 name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
34dc7c2f
BB
1995 if (strlen(name) + depth > max)
1996 max = strlen(name) + depth;
1997
1998 free(name);
1999
2000 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2001 &child, &children) == 0) {
2002 for (c = 0; c < children; c++)
2003 if ((ret = max_width(zhp, child[c], depth + 2,
d2f3e292 2004 max, name_flags)) > max)
34dc7c2f
BB
2005 max = ret;
2006 }
2007
2008 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2009 &child, &children) == 0) {
2010 for (c = 0; c < children; c++)
2011 if ((ret = max_width(zhp, child[c], depth + 2,
d2f3e292 2012 max, name_flags)) > max)
34dc7c2f
BB
2013 max = ret;
2014 }
2015
2016 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2017 &child, &children) == 0) {
2018 for (c = 0; c < children; c++)
2019 if ((ret = max_width(zhp, child[c], depth + 2,
d2f3e292 2020 max, name_flags)) > max)
34dc7c2f
BB
2021 max = ret;
2022 }
2023
34dc7c2f
BB
2024 return (max);
2025}
2026
9babb374
BB
2027typedef struct spare_cbdata {
2028 uint64_t cb_guid;
2029 zpool_handle_t *cb_zhp;
2030} spare_cbdata_t;
2031
2032static boolean_t
2033find_vdev(nvlist_t *nv, uint64_t search)
2034{
2035 uint64_t guid;
2036 nvlist_t **child;
2037 uint_t c, children;
2038
2039 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2040 search == guid)
2041 return (B_TRUE);
2042
2043 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2044 &child, &children) == 0) {
2045 for (c = 0; c < children; c++)
2046 if (find_vdev(child[c], search))
2047 return (B_TRUE);
2048 }
2049
2050 return (B_FALSE);
2051}
2052
2053static int
2054find_spare(zpool_handle_t *zhp, void *data)
2055{
2056 spare_cbdata_t *cbp = data;
2057 nvlist_t *config, *nvroot;
2058
2059 config = zpool_get_config(zhp, NULL);
2060 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2061 &nvroot) == 0);
2062
2063 if (find_vdev(nvroot, cbp->cb_guid)) {
2064 cbp->cb_zhp = zhp;
2065 return (1);
2066 }
2067
2068 zpool_close(zhp);
2069 return (0);
2070}
2071
fea33e4e
HJ
2072typedef struct status_cbdata {
2073 int cb_count;
2074 int cb_name_flags;
2075 int cb_namewidth;
2076 boolean_t cb_allpools;
2077 boolean_t cb_verbose;
ad796b8a 2078 boolean_t cb_literal;
fea33e4e
HJ
2079 boolean_t cb_explain;
2080 boolean_t cb_first;
2081 boolean_t cb_dedup_stats;
2082 boolean_t cb_print_status;
ad796b8a 2083 boolean_t cb_print_slow_ios;
a769fb53 2084 boolean_t cb_print_vdev_init;
1b939560 2085 boolean_t cb_print_vdev_trim;
8720e9e7 2086 vdev_cmd_data_list_t *vcdl;
fea33e4e
HJ
2087} status_cbdata_t;
2088
d6418de0
TH
2089/* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2090static int
2091is_blank_str(char *str)
2092{
2093 while (str != NULL && *str != '\0') {
2094 if (!isblank(*str))
2095 return (0);
2096 str++;
2097 }
2098 return (1);
2099}
2100
2101/* Print command output lines for specific vdev in a specific pool */
8720e9e7
TH
2102static void
2103zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
2104{
d6418de0
TH
2105 vdev_cmd_data_t *data;
2106 int i, j;
2107 char *val;
2108
8720e9e7 2109 for (i = 0; i < vcdl->count; i++) {
d6418de0
TH
2110 if ((strcmp(vcdl->data[i].path, path) != 0) ||
2111 (strcmp(vcdl->data[i].pool, pool) != 0)) {
2112 /* Not the vdev we're looking for */
2113 continue;
8720e9e7 2114 }
d6418de0
TH
2115
2116 data = &vcdl->data[i];
2117 /* Print out all the output values for this vdev */
2118 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
2119 val = NULL;
2120 /* Does this vdev have values for this column? */
2121 for (int k = 0; k < data->cols_cnt; k++) {
2122 if (strcmp(data->cols[k],
2123 vcdl->uniq_cols[j]) == 0) {
2124 /* yes it does, record the value */
2125 val = data->lines[k];
2126 break;
2127 }
2128 }
2129 /*
2130 * Mark empty values with dashes to make output
2131 * awk-able.
2132 */
7454d2bb 2133 if (val == NULL || is_blank_str(val))
d6418de0
TH
2134 val = "-";
2135
2136 printf("%*s", vcdl->uniq_cols_width[j], val);
2137 if (j < vcdl->uniq_cols_cnt - 1)
2138 printf(" ");
2139 }
2140
2141 /* Print out any values that aren't in a column at the end */
2142 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
2143 /* Did we have any columns? If so print a spacer. */
2144 if (vcdl->uniq_cols_cnt > 0)
2145 printf(" ");
2146
2147 val = data->lines[j];
2148 printf("%s", val ? val : "");
2149 }
2150 break;
8720e9e7
TH
2151 }
2152}
2153
1b939560
BB
2154/*
2155 * Print vdev initialization status for leaves
2156 */
2157static void
2158print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
2159{
2160 if (verbose) {
2161 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
2162 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
2163 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
2164 !vs->vs_scan_removing) {
2165 char zbuf[1024];
2166 char tbuf[256];
2167 struct tm zaction_ts;
2168
2169 time_t t = vs->vs_initialize_action_time;
2170 int initialize_pct = 100;
2171 if (vs->vs_initialize_state !=
2172 VDEV_INITIALIZE_COMPLETE) {
2173 initialize_pct = (vs->vs_initialize_bytes_done *
2174 100 / (vs->vs_initialize_bytes_est + 1));
2175 }
2176
2177 (void) localtime_r(&t, &zaction_ts);
2178 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
2179
2180 switch (vs->vs_initialize_state) {
2181 case VDEV_INITIALIZE_SUSPENDED:
2182 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2183 gettext("suspended, started at"), tbuf);
2184 break;
2185 case VDEV_INITIALIZE_ACTIVE:
2186 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2187 gettext("started at"), tbuf);
2188 break;
2189 case VDEV_INITIALIZE_COMPLETE:
2190 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2191 gettext("completed at"), tbuf);
2192 break;
2193 }
2194
2195 (void) printf(gettext(" (%d%% initialized%s)"),
2196 initialize_pct, zbuf);
2197 } else {
2198 (void) printf(gettext(" (uninitialized)"));
2199 }
2200 } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) {
2201 (void) printf(gettext(" (initializing)"));
2202 }
2203}
2204
2205/*
2206 * Print vdev TRIM status for leaves
2207 */
2208static void
2209print_status_trim(vdev_stat_t *vs, boolean_t verbose)
2210{
2211 if (verbose) {
2212 if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE ||
2213 vs->vs_trim_state == VDEV_TRIM_SUSPENDED ||
2214 vs->vs_trim_state == VDEV_TRIM_COMPLETE) &&
2215 !vs->vs_scan_removing) {
2216 char zbuf[1024];
2217 char tbuf[256];
2218 struct tm zaction_ts;
2219
2220 time_t t = vs->vs_trim_action_time;
2221 int trim_pct = 100;
2222 if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) {
2223 trim_pct = (vs->vs_trim_bytes_done *
2224 100 / (vs->vs_trim_bytes_est + 1));
2225 }
2226
2227 (void) localtime_r(&t, &zaction_ts);
2228 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
2229
2230 switch (vs->vs_trim_state) {
2231 case VDEV_TRIM_SUSPENDED:
2232 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2233 gettext("suspended, started at"), tbuf);
2234 break;
2235 case VDEV_TRIM_ACTIVE:
2236 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2237 gettext("started at"), tbuf);
2238 break;
2239 case VDEV_TRIM_COMPLETE:
2240 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2241 gettext("completed at"), tbuf);
2242 break;
2243 }
2244
2245 (void) printf(gettext(" (%d%% trimmed%s)"),
2246 trim_pct, zbuf);
2247 } else if (vs->vs_trim_notsup) {
2248 (void) printf(gettext(" (trim unsupported)"));
2249 } else {
2250 (void) printf(gettext(" (untrimmed)"));
2251 }
2252 } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) {
2253 (void) printf(gettext(" (trimming)"));
2254 }
2255}
2256
9fb2771a
TH
2257/*
2258 * Return the color associated with a health string. This includes returning
2259 * NULL for no color change.
2260 */
2261static char *
2262health_str_to_color(const char *health)
2263{
2264 if (strcmp(health, gettext("FAULTED")) == 0 ||
2265 strcmp(health, gettext("SUSPENDED")) == 0 ||
2266 strcmp(health, gettext("UNAVAIL")) == 0) {
2267 return (ANSI_RED);
2268 }
2269
2270 if (strcmp(health, gettext("OFFLINE")) == 0 ||
2271 strcmp(health, gettext("DEGRADED")) == 0 ||
2272 strcmp(health, gettext("REMOVED")) == 0) {
2273 return (ANSI_YELLOW);
2274 }
2275
2276 return (NULL);
2277}
2278
9babb374
BB
2279/*
2280 * Print out configuration state as requested by status_callback.
2281 */
d2f3e292 2282static void
fea33e4e 2283print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
9a49d3f3 2284 nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs)
9babb374 2285{
2ef0f8c3 2286 nvlist_t **child, *root;
6fe3498c 2287 uint_t c, i, vsc, children;
428870ff 2288 pool_scan_stat_t *ps = NULL;
9babb374 2289 vdev_stat_t *vs;
428870ff 2290 char rbuf[6], wbuf[6], cbuf[6];
9babb374
BB
2291 char *vname;
2292 uint64_t notpresent;
fea33e4e 2293 spare_cbdata_t spare_cb;
6b8655ad 2294 const char *state;
a1d477c2 2295 char *type;
8720e9e7 2296 char *path = NULL;
9fb2771a 2297 char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL;
9babb374 2298
9babb374
BB
2299 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2300 &child, &children) != 0)
2301 children = 0;
2302
428870ff 2303 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
6fe3498c 2304 (uint64_t **)&vs, &vsc) == 0);
428870ff 2305
a1d477c2
MA
2306 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2307
2308 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2309 return;
2310
9babb374 2311 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
9fb2771a 2312
9babb374
BB
2313 if (isspare) {
2314 /*
2315 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2316 * online drives.
2317 */
2318 if (vs->vs_aux == VDEV_AUX_SPARED)
9fb2771a 2319 state = gettext("INUSE");
9babb374 2320 else if (vs->vs_state == VDEV_STATE_HEALTHY)
9fb2771a 2321 state = gettext("AVAIL");
9babb374
BB
2322 }
2323
9fb2771a
TH
2324 printf_color(health_str_to_color(state),
2325 "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
9babb374
BB
2326 name, state);
2327
2328 if (!isspare) {
9fb2771a
TH
2329 if (vs->vs_read_errors)
2330 rcolor = ANSI_RED;
2331
2332 if (vs->vs_write_errors)
2333 wcolor = ANSI_RED;
2334
2335 if (vs->vs_checksum_errors)
2336 ccolor = ANSI_RED;
2337
ad796b8a 2338 if (cb->cb_literal) {
9fb2771a
TH
2339 printf(" ");
2340 printf_color(rcolor, "%5llu",
2341 (u_longlong_t)vs->vs_read_errors);
2342 printf(" ");
2343 printf_color(wcolor, "%5llu",
2344 (u_longlong_t)vs->vs_write_errors);
2345 printf(" ");
2346 printf_color(ccolor, "%5llu",
ad796b8a
TH
2347 (u_longlong_t)vs->vs_checksum_errors);
2348 } else {
2349 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2350 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2351 zfs_nicenum(vs->vs_checksum_errors, cbuf,
2352 sizeof (cbuf));
9fb2771a
TH
2353 printf(" ");
2354 printf_color(rcolor, "%5s", rbuf);
2355 printf(" ");
2356 printf_color(wcolor, "%5s", wbuf);
2357 printf(" ");
2358 printf_color(ccolor, "%5s", cbuf);
ad796b8a 2359 }
ad796b8a
TH
2360 if (cb->cb_print_slow_ios) {
2361 if (children == 0) {
2362 /* Only leafs vdevs have slow IOs */
2363 zfs_nicenum(vs->vs_slow_ios, rbuf,
2364 sizeof (rbuf));
2365 } else {
2366 snprintf(rbuf, sizeof (rbuf), "-");
2367 }
2368
2369 if (cb->cb_literal)
2370 printf(" %5llu", (u_longlong_t)vs->vs_slow_ios);
2371 else
2372 printf(" %5s", rbuf);
2373 }
9babb374
BB
2374 }
2375
2376 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2377 &notpresent) == 0) {
9babb374 2378 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
9fb2771a 2379 (void) printf(" %s %s", gettext("was"), path);
9babb374
BB
2380 } else if (vs->vs_aux != 0) {
2381 (void) printf(" ");
9fb2771a 2382 color_start(ANSI_RED);
9babb374
BB
2383 switch (vs->vs_aux) {
2384 case VDEV_AUX_OPEN_FAILED:
2385 (void) printf(gettext("cannot open"));
2386 break;
2387
2388 case VDEV_AUX_BAD_GUID_SUM:
2389 (void) printf(gettext("missing device"));
2390 break;
2391
2392 case VDEV_AUX_NO_REPLICAS:
2393 (void) printf(gettext("insufficient replicas"));
2394 break;
2395
2396 case VDEV_AUX_VERSION_NEWER:
2397 (void) printf(gettext("newer version"));
2398 break;
2399
9ae529ec
CS
2400 case VDEV_AUX_UNSUP_FEAT:
2401 (void) printf(gettext("unsupported feature(s)"));
2402 break;
2403
6fe3498c
RM
2404 case VDEV_AUX_ASHIFT_TOO_BIG:
2405 (void) printf(gettext("unsupported minimum blocksize"));
2406 break;
2407
9babb374
BB
2408 case VDEV_AUX_SPARED:
2409 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
fea33e4e
HJ
2410 &spare_cb.cb_guid) == 0);
2411 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
2412 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
9babb374
BB
2413 zpool_get_name(zhp)) == 0)
2414 (void) printf(gettext("currently in "
2415 "use"));
2416 else
2417 (void) printf(gettext("in use by "
2418 "pool '%s'"),
fea33e4e
HJ
2419 zpool_get_name(spare_cb.cb_zhp));
2420 zpool_close(spare_cb.cb_zhp);
9babb374
BB
2421 } else {
2422 (void) printf(gettext("currently in use"));
2423 }
2424 break;
2425
2426 case VDEV_AUX_ERR_EXCEEDED:
2427 (void) printf(gettext("too many errors"));
2428 break;
2429
2430 case VDEV_AUX_IO_FAILURE:
2431 (void) printf(gettext("experienced I/O failures"));
2432 break;
2433
2434 case VDEV_AUX_BAD_LOG:
2435 (void) printf(gettext("bad intent log"));
2436 break;
2437
428870ff
BB
2438 case VDEV_AUX_EXTERNAL:
2439 (void) printf(gettext("external device fault"));
2440 break;
2441
2442 case VDEV_AUX_SPLIT_POOL:
2443 (void) printf(gettext("split into new pool"));
2444 break;
2445
379ca9cf
OF
2446 case VDEV_AUX_ACTIVE:
2447 (void) printf(gettext("currently in use"));
4265a929 2448 break;
379ca9cf 2449
6cb8e530
PZ
2450 case VDEV_AUX_CHILDREN_OFFLINE:
2451 (void) printf(gettext("all children offline"));
2452 break;
2453
dce1bf99
VKV
2454 case VDEV_AUX_BAD_LABEL:
2455 (void) printf(gettext("invalid label"));
2456 break;
2457
9babb374
BB
2458 default:
2459 (void) printf(gettext("corrupted data"));
2460 break;
2461 }
9fb2771a 2462 color_end();
3928ec53
CS
2463 } else if (children == 0 && !isspare &&
2464 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL &&
2465 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
2466 vs->vs_configured_ashift < vs->vs_physical_ashift) {
2467 (void) printf(
2468 gettext(" block size: %dB configured, %dB native"),
2469 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
428870ff
BB
2470 }
2471
2a673e76
AJ
2472 if (vs->vs_scan_removing != 0) {
2473 (void) printf(gettext(" (removing)"));
2474 } else if (vs->vs_noalloc != 0) {
2475 (void) printf(gettext(" (non-allocating)"));
2476 }
2477
2ef0f8c3
TH
2478 /* The root vdev has the scrub/resilver stats */
2479 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2480 ZPOOL_CONFIG_VDEV_TREE);
2481 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
428870ff
BB
2482 (uint64_t **)&ps, &c);
2483
80a91e74
TC
2484 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0) {
2485 if (vs->vs_scan_processed != 0) {
2486 (void) printf(gettext(" (%s)"),
2487 (ps->pss_func == POOL_SCAN_RESILVER) ?
2488 "resilvering" : "repairing");
2489 } else if (vs->vs_resilver_deferred) {
2490 (void) printf(gettext(" (awaiting resilver)"));
2491 }
9babb374
BB
2492 }
2493
9a49d3f3
BB
2494 /* The top-level vdevs have the rebuild stats */
2495 if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE &&
2496 children == 0) {
2497 if (vs->vs_rebuild_processed != 0) {
2498 (void) printf(gettext(" (resilvering)"));
2499 }
2500 }
2501
8720e9e7
TH
2502 if (cb->vcdl != NULL) {
2503 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2504 printf(" ");
2505 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
2506 }
2507 }
2508
b2255edc 2509 /* Display vdev initialization and trim status for leaves. */
1b939560
BB
2510 if (children == 0) {
2511 print_status_initialize(vs, cb->cb_print_vdev_init);
2512 print_status_trim(vs, cb->cb_print_vdev_trim);
619f0976
GW
2513 }
2514
9babb374
BB
2515 (void) printf("\n");
2516
2517 for (c = 0; c < children; c++) {
428870ff 2518 uint64_t islog = B_FALSE, ishole = B_FALSE;
9babb374 2519
428870ff 2520 /* Don't print logs or holes here */
9babb374 2521 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
428870ff
BB
2522 &islog);
2523 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2524 &ishole);
2525 if (islog || ishole)
9babb374 2526 continue;
cc99f275
DB
2527 /* Only print normal classes here */
2528 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2529 continue;
2530
9a49d3f3
BB
2531 /* Provide vdev_rebuild_stats to children if available */
2532 if (vrs == NULL) {
2533 (void) nvlist_lookup_uint64_array(nv,
2534 ZPOOL_CONFIG_REBUILD_STATS,
2535 (uint64_t **)&vrs, &i);
2536 }
2537
d2f3e292 2538 vname = zpool_vdev_name(g_zfs, zhp, child[c],
fea33e4e
HJ
2539 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2540 print_status_config(zhp, cb, vname, child[c], depth + 2,
9a49d3f3 2541 isspare, vrs);
9babb374
BB
2542 free(vname);
2543 }
2544}
2545
34dc7c2f
BB
2546/*
2547 * Print the configuration of an exported pool. Iterate over all vdevs in the
2548 * pool, printing out the name and status for each one.
2549 */
d2f3e292 2550static void
fea33e4e
HJ
2551print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
2552 int depth)
34dc7c2f
BB
2553{
2554 nvlist_t **child;
2555 uint_t c, children;
2556 vdev_stat_t *vs;
2557 char *type, *vname;
2558
2559 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
428870ff
BB
2560 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
2561 strcmp(type, VDEV_TYPE_HOLE) == 0)
34dc7c2f
BB
2562 return;
2563
428870ff 2564 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
2565 (uint64_t **)&vs, &c) == 0);
2566
fea33e4e 2567 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
34dc7c2f
BB
2568 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
2569
2570 if (vs->vs_aux != 0) {
2571 (void) printf(" ");
2572
2573 switch (vs->vs_aux) {
2574 case VDEV_AUX_OPEN_FAILED:
2575 (void) printf(gettext("cannot open"));
2576 break;
2577
2578 case VDEV_AUX_BAD_GUID_SUM:
2579 (void) printf(gettext("missing device"));
2580 break;
2581
2582 case VDEV_AUX_NO_REPLICAS:
2583 (void) printf(gettext("insufficient replicas"));
2584 break;
2585
2586 case VDEV_AUX_VERSION_NEWER:
2587 (void) printf(gettext("newer version"));
2588 break;
2589
9ae529ec
CS
2590 case VDEV_AUX_UNSUP_FEAT:
2591 (void) printf(gettext("unsupported feature(s)"));
2592 break;
2593
34dc7c2f
BB
2594 case VDEV_AUX_ERR_EXCEEDED:
2595 (void) printf(gettext("too many errors"));
2596 break;
2597
379ca9cf
OF
2598 case VDEV_AUX_ACTIVE:
2599 (void) printf(gettext("currently in use"));
2600 break;
2601
6cb8e530
PZ
2602 case VDEV_AUX_CHILDREN_OFFLINE:
2603 (void) printf(gettext("all children offline"));
2604 break;
2605
dce1bf99
VKV
2606 case VDEV_AUX_BAD_LABEL:
2607 (void) printf(gettext("invalid label"));
2608 break;
2609
34dc7c2f
BB
2610 default:
2611 (void) printf(gettext("corrupted data"));
2612 break;
2613 }
2614 }
2615 (void) printf("\n");
2616
2617 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2618 &child, &children) != 0)
2619 return;
2620
2621 for (c = 0; c < children; c++) {
2622 uint64_t is_log = B_FALSE;
2623
2624 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2625 &is_log);
9babb374 2626 if (is_log)
34dc7c2f 2627 continue;
cc99f275
DB
2628 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2629 continue;
34dc7c2f 2630
d2f3e292 2631 vname = zpool_vdev_name(g_zfs, NULL, child[c],
fea33e4e
HJ
2632 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2633 print_import_config(cb, vname, child[c], depth + 2);
34dc7c2f
BB
2634 free(vname);
2635 }
2636
2637 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2638 &child, &children) == 0) {
2639 (void) printf(gettext("\tcache\n"));
2640 for (c = 0; c < children; c++) {
d2f3e292 2641 vname = zpool_vdev_name(g_zfs, NULL, child[c],
fea33e4e 2642 cb->cb_name_flags);
34dc7c2f
BB
2643 (void) printf("\t %s\n", vname);
2644 free(vname);
2645 }
2646 }
2647
2648 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2649 &child, &children) == 0) {
2650 (void) printf(gettext("\tspares\n"));
2651 for (c = 0; c < children; c++) {
d2f3e292 2652 vname = zpool_vdev_name(g_zfs, NULL, child[c],
fea33e4e 2653 cb->cb_name_flags);
34dc7c2f
BB
2654 (void) printf("\t %s\n", vname);
2655 free(vname);
2656 }
2657 }
2658}
2659
9babb374 2660/*
cc99f275
DB
2661 * Print specialized class vdevs.
2662 *
2663 * These are recorded as top level vdevs in the main pool child array
2664 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2665 * print_status_config() or print_import_config() to print the top level
2666 * class vdevs then any of their children (eg mirrored slogs) are printed
2667 * recursively - which works because only the top level vdev is marked.
9babb374
BB
2668 */
2669static void
cc99f275
DB
2670print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
2671 const char *class)
9babb374
BB
2672{
2673 uint_t c, children;
2674 nvlist_t **child;
cc99f275
DB
2675 boolean_t printed = B_FALSE;
2676
2677 assert(zhp != NULL || !cb->cb_verbose);
9babb374
BB
2678
2679 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2680 &children) != 0)
2681 return;
2682
9babb374
BB
2683 for (c = 0; c < children; c++) {
2684 uint64_t is_log = B_FALSE;
cc99f275
DB
2685 char *bias = NULL;
2686 char *type = NULL;
9babb374
BB
2687
2688 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2689 &is_log);
cc99f275
DB
2690
2691 if (is_log) {
2692 bias = VDEV_ALLOC_CLASS_LOGS;
2693 } else {
2694 (void) nvlist_lookup_string(child[c],
2695 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
2696 (void) nvlist_lookup_string(child[c],
2697 ZPOOL_CONFIG_TYPE, &type);
2698 }
2699
2700 if (bias == NULL || strcmp(bias, class) != 0)
2701 continue;
2702 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
9babb374 2703 continue;
cc99f275
DB
2704
2705 if (!printed) {
2706 (void) printf("\t%s\t\n", gettext(class));
2707 printed = B_TRUE;
2708 }
2709
2710 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
fea33e4e
HJ
2711 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2712 if (cb->cb_print_status)
2713 print_status_config(zhp, cb, name, child[c], 2,
9a49d3f3 2714 B_FALSE, NULL);
9babb374 2715 else
fea33e4e 2716 print_import_config(cb, name, child[c], 2);
9babb374
BB
2717 free(name);
2718 }
2719}
428870ff 2720
34dc7c2f
BB
2721/*
2722 * Display the status for the given pool.
2723 */
0936981d
GW
2724static int
2725show_import(nvlist_t *config, boolean_t report_error)
34dc7c2f
BB
2726{
2727 uint64_t pool_state;
2728 vdev_stat_t *vs;
2729 char *name;
2730 uint64_t guid;
379ca9cf 2731 uint64_t hostid = 0;
34dc7c2f 2732 char *msgid;
379ca9cf
OF
2733 char *hostname = "unknown";
2734 nvlist_t *nvroot, *nvinfo;
731782ec 2735 zpool_status_t reason;
ffe9d382 2736 zpool_errata_t errata;
34dc7c2f
BB
2737 const char *health;
2738 uint_t vsc;
d96eb2b1 2739 char *comment;
fea33e4e 2740 status_cbdata_t cb = { 0 };
34dc7c2f
BB
2741
2742 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2743 &name) == 0);
2744 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2745 &guid) == 0);
2746 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2747 &pool_state) == 0);
2748 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2749 &nvroot) == 0);
2750
428870ff 2751 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
2752 (uint64_t **)&vs, &vsc) == 0);
2753 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2754
ffe9d382 2755 reason = zpool_import_status(config, &msgid, &errata);
34dc7c2f 2756
0936981d
GW
2757 /*
2758 * If we're importing using a cachefile, then we won't report any
2759 * errors unless we are in the scan phase of the import.
2760 */
2761 if (reason != ZPOOL_STATUS_OK && !report_error)
2762 return (reason);
2763
d96eb2b1
DM
2764 (void) printf(gettext(" pool: %s\n"), name);
2765 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
2766 (void) printf(gettext(" state: %s"), health);
34dc7c2f
BB
2767 if (pool_state == POOL_STATE_DESTROYED)
2768 (void) printf(gettext(" (DESTROYED)"));
2769 (void) printf("\n");
2770
2771 switch (reason) {
2772 case ZPOOL_STATUS_MISSING_DEV_R:
2773 case ZPOOL_STATUS_MISSING_DEV_NR:
2774 case ZPOOL_STATUS_BAD_GUID_SUM:
9fb2771a
TH
2775 printf_color(ANSI_BOLD, gettext("status: "));
2776 printf_color(ANSI_YELLOW, gettext("One or more devices are "
d96eb2b1 2777 "missing from the system.\n"));
34dc7c2f
BB
2778 break;
2779
2780 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2781 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
9fb2771a
TH
2782 printf_color(ANSI_BOLD, gettext("status: "));
2783 printf_color(ANSI_YELLOW, gettext("One or more devices contains"
2784 " corrupted data.\n"));
34dc7c2f
BB
2785 break;
2786
2787 case ZPOOL_STATUS_CORRUPT_DATA:
d96eb2b1
DM
2788 (void) printf(
2789 gettext(" status: The pool data is corrupted.\n"));
34dc7c2f
BB
2790 break;
2791
2792 case ZPOOL_STATUS_OFFLINE_DEV:
9fb2771a
TH
2793 printf_color(ANSI_BOLD, gettext("status: "));
2794 printf_color(ANSI_YELLOW, gettext("One or more devices "
34dc7c2f
BB
2795 "are offlined.\n"));
2796 break;
2797
2798 case ZPOOL_STATUS_CORRUPT_POOL:
9fb2771a
TH
2799 printf_color(ANSI_BOLD, gettext("status: "));
2800 printf_color(ANSI_YELLOW, gettext("The pool metadata is "
34dc7c2f
BB
2801 "corrupted.\n"));
2802 break;
2803
2804 case ZPOOL_STATUS_VERSION_OLDER:
9fb2771a
TH
2805 printf_color(ANSI_BOLD, gettext("status: "));
2806 printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
2807 "a legacy on-disk version.\n"));
34dc7c2f
BB
2808 break;
2809
2810 case ZPOOL_STATUS_VERSION_NEWER:
9fb2771a
TH
2811 printf_color(ANSI_BOLD, gettext("status: "));
2812 printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
2813 "an incompatible version.\n"));
34dc7c2f 2814 break;
b128c09f 2815
b9b24bb4 2816 case ZPOOL_STATUS_FEAT_DISABLED:
9fb2771a 2817 printf_color(ANSI_BOLD, gettext("status: "));
e086db16
CB
2818 printf_color(ANSI_YELLOW, gettext("Some supported "
2819 "features are not enabled on the pool.\n\t"
2820 "(Note that they may be intentionally disabled "
2821 "if the\n\t'compatibility' property is set.)\n"));
658fb802
CB
2822 break;
2823
2824 case ZPOOL_STATUS_COMPATIBILITY_ERR:
2825 printf_color(ANSI_BOLD, gettext("status: "));
2826 printf_color(ANSI_YELLOW, gettext("Error reading or parsing "
2827 "the file(s) indicated by the 'compatibility'\n"
2828 "property.\n"));
b9b24bb4
CS
2829 break;
2830
e086db16
CB
2831 case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
2832 printf_color(ANSI_BOLD, gettext("status: "));
2833 printf_color(ANSI_YELLOW, gettext("One or more features "
2834 "are enabled on the pool despite not being\n"
2835 "requested by the 'compatibility' property.\n"));
2836 break;
2837
9ae529ec 2838 case ZPOOL_STATUS_UNSUP_FEAT_READ:
9fb2771a
TH
2839 printf_color(ANSI_BOLD, gettext("status: "));
2840 printf_color(ANSI_YELLOW, gettext("The pool uses the following "
4e33ba4c 2841 "feature(s) not supported on this system:\n"));
9fb2771a 2842 color_start(ANSI_YELLOW);
9ae529ec 2843 zpool_print_unsup_feat(config);
9fb2771a 2844 color_end();
9ae529ec
CS
2845 break;
2846
2847 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
9fb2771a
TH
2848 printf_color(ANSI_BOLD, gettext("status: "));
2849 printf_color(ANSI_YELLOW, gettext("The pool can only be "
2850 "accessed in read-only mode on this system. It\n\tcannot be"
2851 " accessed in read-write mode because it uses the "
9ae529ec 2852 "following\n\tfeature(s) not supported on this system:\n"));
9fb2771a 2853 color_start(ANSI_YELLOW);
9ae529ec 2854 zpool_print_unsup_feat(config);
9fb2771a 2855 color_end();
9ae529ec
CS
2856 break;
2857
379ca9cf 2858 case ZPOOL_STATUS_HOSTID_ACTIVE:
9fb2771a
TH
2859 printf_color(ANSI_BOLD, gettext("status: "));
2860 printf_color(ANSI_YELLOW, gettext("The pool is currently "
379ca9cf
OF
2861 "imported by another system.\n"));
2862 break;
2863
2864 case ZPOOL_STATUS_HOSTID_REQUIRED:
9fb2771a
TH
2865 printf_color(ANSI_BOLD, gettext("status: "));
2866 printf_color(ANSI_YELLOW, gettext("The pool has the "
379ca9cf
OF
2867 "multihost property on. It cannot\n\tbe safely imported "
2868 "when the system hostid is not set.\n"));
2869 break;
2870
34dc7c2f 2871 case ZPOOL_STATUS_HOSTID_MISMATCH:
9fb2771a
TH
2872 printf_color(ANSI_BOLD, gettext("status: "));
2873 printf_color(ANSI_YELLOW, gettext("The pool was last accessed "
2874 "by another system.\n"));
34dc7c2f 2875 break;
b128c09f 2876
34dc7c2f
BB
2877 case ZPOOL_STATUS_FAULTED_DEV_R:
2878 case ZPOOL_STATUS_FAULTED_DEV_NR:
9fb2771a
TH
2879 printf_color(ANSI_BOLD, gettext("status: "));
2880 printf_color(ANSI_YELLOW, gettext("One or more devices are "
34dc7c2f
BB
2881 "faulted.\n"));
2882 break;
2883
b128c09f 2884 case ZPOOL_STATUS_BAD_LOG:
9fb2771a
TH
2885 printf_color(ANSI_BOLD, gettext("status: "));
2886 printf_color(ANSI_YELLOW, gettext("An intent log record cannot "
2887 "be read.\n"));
b128c09f
BB
2888 break;
2889
428870ff 2890 case ZPOOL_STATUS_RESILVERING:
9a49d3f3 2891 case ZPOOL_STATUS_REBUILDING:
9fb2771a
TH
2892 printf_color(ANSI_BOLD, gettext("status: "));
2893 printf_color(ANSI_YELLOW, gettext("One or more devices were "
2894 "being resilvered.\n"));
428870ff
BB
2895 break;
2896
ffe9d382 2897 case ZPOOL_STATUS_ERRATA:
9fb2771a
TH
2898 printf_color(ANSI_BOLD, gettext("status: "));
2899 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"),
ffe9d382
BB
2900 errata);
2901 break;
2902
c494aa7f
GW
2903 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
2904 printf_color(ANSI_BOLD, gettext("status: "));
2905 printf_color(ANSI_YELLOW, gettext("One or more devices are "
2906 "configured to use a non-native block size.\n"
2907 "\tExpect reduced performance.\n"));
2908 break;
2909
34dc7c2f
BB
2910 default:
2911 /*
2912 * No other status can be seen when importing pools.
2913 */
2914 assert(reason == ZPOOL_STATUS_OK);
2915 }
2916
2917 /*
2918 * Print out an action according to the overall state of the pool.
2919 */
2920 if (vs->vs_state == VDEV_STATE_HEALTHY) {
b9b24bb4
CS
2921 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2922 reason == ZPOOL_STATUS_FEAT_DISABLED) {
d96eb2b1 2923 (void) printf(gettext(" action: The pool can be "
34dc7c2f
BB
2924 "imported using its name or numeric identifier, "
2925 "though\n\tsome features will not be available "
2926 "without an explicit 'zpool upgrade'.\n"));
658fb802
CB
2927 } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) {
2928 (void) printf(gettext(" action: The pool can be "
2929 "imported using its name or numeric\n\tidentifier, "
2930 "though the file(s) indicated by its "
2931 "'compatibility'\n\tproperty cannot be parsed at "
2932 "this time.\n"));
b9b24bb4 2933 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
d96eb2b1 2934 (void) printf(gettext(" action: The pool can be "
34dc7c2f
BB
2935 "imported using its name or numeric "
2936 "identifier and\n\tthe '-f' flag.\n"));
ffe9d382
BB
2937 } else if (reason == ZPOOL_STATUS_ERRATA) {
2938 switch (errata) {
2939 case ZPOOL_ERRATA_NONE:
2940 break;
2941
4f2dcb3e
RY
2942 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2943 (void) printf(gettext(" action: The pool can "
2944 "be imported using its name or numeric "
2945 "identifier,\n\thowever there is a compat"
2946 "ibility issue which should be corrected"
2947 "\n\tby running 'zpool scrub'\n"));
2948 break;
2949
2950 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2951 (void) printf(gettext(" action: The pool can"
2952 "not be imported with this version of ZFS "
2953 "due to\n\tan active asynchronous destroy. "
2954 "Revert to an earlier version\n\tand "
2955 "allow the destroy to complete before "
2956 "updating.\n"));
2957 break;
2958
ae76f45c
TC
2959 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2960 (void) printf(gettext(" action: Existing "
2961 "encrypted datasets contain an on-disk "
2962 "incompatibility, which\n\tneeds to be "
2963 "corrected. Backup these datasets to new "
2964 "encrypted datasets\n\tand destroy the "
2965 "old ones.\n"));
2966 break;
2967
f00ab3f2 2968 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
b3b60984
JS
2969 (void) printf(gettext(" action: Existing "
2970 "encrypted snapshots and bookmarks contain "
2971 "an on-disk\n\tincompatibility. This may "
e2dddb7e
JS
2972 "cause on-disk corruption if they are used"
2973 "\n\twith 'zfs recv'. To correct the "
2974 "issue, enable the bookmark_v2 feature.\n\t"
2975 "No additional action is needed if there "
2976 "are no encrypted snapshots or\n\t"
2977 "bookmarks. If preserving the encrypted "
2978 "snapshots and bookmarks is\n\trequired, "
2979 "use a non-raw send to backup and restore "
2980 "them. Alternately,\n\tthey may be removed"
2981 " to resolve the incompatibility.\n"));
f00ab3f2 2982 break;
ffe9d382
BB
2983 default:
2984 /*
2985 * All errata must contain an action message.
2986 */
2987 assert(0);
2988 }
b9b24bb4 2989 } else {
d96eb2b1 2990 (void) printf(gettext(" action: The pool can be "
34dc7c2f
BB
2991 "imported using its name or numeric "
2992 "identifier.\n"));
b9b24bb4 2993 }
34dc7c2f 2994 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
d96eb2b1 2995 (void) printf(gettext(" action: The pool can be imported "
34dc7c2f
BB
2996 "despite missing or damaged devices. The\n\tfault "
2997 "tolerance of the pool may be compromised if imported.\n"));
2998 } else {
2999 switch (reason) {
3000 case ZPOOL_STATUS_VERSION_NEWER:
d96eb2b1 3001 (void) printf(gettext(" action: The pool cannot be "
34dc7c2f
BB
3002 "imported. Access the pool on a system running "
3003 "newer\n\tsoftware, or recreate the pool from "
3004 "backup.\n"));
3005 break;
9ae529ec 3006 case ZPOOL_STATUS_UNSUP_FEAT_READ:
9fb2771a
TH
3007 printf_color(ANSI_BOLD, gettext("action: "));
3008 printf_color(ANSI_YELLOW, gettext("The pool cannot be "
9ae529ec
CS
3009 "imported. Access the pool on a system that "
3010 "supports\n\tthe required feature(s), or recreate "
3011 "the pool from backup.\n"));
3012 break;
3013 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
9fb2771a
TH
3014 printf_color(ANSI_BOLD, gettext("action: "));
3015 printf_color(ANSI_YELLOW, gettext("The pool cannot be "
9ae529ec
CS
3016 "imported in read-write mode. Import the pool "
3017 "with\n"
3018 "\t\"-o readonly=on\", access the pool on a system "
3019 "that supports the\n\trequired feature(s), or "
3020 "recreate the pool from backup.\n"));
3021 break;
34dc7c2f
BB
3022 case ZPOOL_STATUS_MISSING_DEV_R:
3023 case ZPOOL_STATUS_MISSING_DEV_NR:
3024 case ZPOOL_STATUS_BAD_GUID_SUM:
d96eb2b1 3025 (void) printf(gettext(" action: The pool cannot be "
34dc7c2f
BB
3026 "imported. Attach the missing\n\tdevices and try "
3027 "again.\n"));
3028 break;
379ca9cf
OF
3029 case ZPOOL_STATUS_HOSTID_ACTIVE:
3030 VERIFY0(nvlist_lookup_nvlist(config,
3031 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
3032
3033 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3034 hostname = fnvlist_lookup_string(nvinfo,
3035 ZPOOL_CONFIG_MMP_HOSTNAME);
3036
3037 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3038 hostid = fnvlist_lookup_uint64(nvinfo,
3039 ZPOOL_CONFIG_MMP_HOSTID);
3040
3041 (void) printf(gettext(" action: The pool must be "
3042 "exported from %s (hostid=%lx)\n\tbefore it "
3043 "can be safely imported.\n"), hostname,
3044 (unsigned long) hostid);
3045 break;
3046 case ZPOOL_STATUS_HOSTID_REQUIRED:
3047 (void) printf(gettext(" action: Set a unique system "
b9373170 3048 "hostid with the zgenhostid(8) command.\n"));
379ca9cf 3049 break;
34dc7c2f 3050 default:
d96eb2b1 3051 (void) printf(gettext(" action: The pool cannot be "
34dc7c2f
BB
3052 "imported due to damaged devices or data.\n"));
3053 }
3054 }
3055
d96eb2b1
DM
3056 /* Print the comment attached to the pool. */
3057 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
3058 (void) printf(gettext("comment: %s\n"), comment);
3059
34dc7c2f
BB
3060 /*
3061 * If the state is "closed" or "can't open", and the aux state
3062 * is "corrupt data":
3063 */
3064 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
3065 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
3066 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
3067 if (pool_state == POOL_STATE_DESTROYED)
3068 (void) printf(gettext("\tThe pool was destroyed, "
3069 "but can be imported using the '-Df' flags.\n"));
3070 else if (pool_state != POOL_STATE_EXPORTED)
3071 (void) printf(gettext("\tThe pool may be active on "
3072 "another system, but can be imported using\n\t"
3073 "the '-f' flag.\n"));
3074 }
3075
e458fcca
BB
3076 if (msgid != NULL) {
3077 (void) printf(gettext(
a2f944a1
RM
3078 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3079 msgid);
e458fcca 3080 }
34dc7c2f 3081
d96eb2b1 3082 (void) printf(gettext(" config:\n\n"));
34dc7c2f 3083
2df9ad1c
GG
3084 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
3085 VDEV_NAME_TYPE_ID);
fea33e4e
HJ
3086 if (cb.cb_namewidth < 10)
3087 cb.cb_namewidth = 10;
34dc7c2f 3088
fea33e4e 3089 print_import_config(&cb, name, nvroot, 0);
cc99f275
DB
3090
3091 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
3092 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
3093 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
34dc7c2f
BB
3094
3095 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
3096 (void) printf(gettext("\n\tAdditional devices are known to "
3097 "be part of this pool, though their\n\texact "
3098 "configuration cannot be determined.\n"));
3099 }
0936981d 3100 return (0);
34dc7c2f
BB
3101}
3102
379ca9cf
OF
3103static boolean_t
3104zfs_force_import_required(nvlist_t *config)
3105{
3106 uint64_t state;
3107 uint64_t hostid = 0;
3108 nvlist_t *nvinfo;
3109
3110 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
3111 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
3112
3113 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
3114 return (B_TRUE);
3115
3116 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3117 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
3118 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
3119 ZPOOL_CONFIG_MMP_STATE);
3120
3121 if (mmp_state != MMP_STATE_INACTIVE)
3122 return (B_TRUE);
3123 }
3124
3125 return (B_FALSE);
3126}
3127
34dc7c2f
BB
3128/*
3129 * Perform the import for the given configuration. This passes the heavy
3130 * lifting off to zpool_import_props(), and then mounts the datasets contained
3131 * within the pool.
3132 */
3133static int
3134do_import(nvlist_t *config, const char *newname, const char *mntopts,
572e2857 3135 nvlist_t *props, int flags)
34dc7c2f 3136{
b5256303 3137 int ret = 0;
34dc7c2f
BB
3138 zpool_handle_t *zhp;
3139 char *name;
34dc7c2f 3140 uint64_t version;
34dc7c2f 3141
379ca9cf 3142 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
379ca9cf 3143 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
34dc7c2f 3144
9ae529ec 3145 if (!SPA_VERSION_IS_SUPPORTED(version)) {
34dc7c2f 3146 (void) fprintf(stderr, gettext("cannot import '%s': pool "
9ae529ec 3147 "is formatted using an unsupported ZFS version\n"), name);
34dc7c2f 3148 return (1);
379ca9cf 3149 } else if (zfs_force_import_required(config) &&
572e2857 3150 !(flags & ZFS_IMPORT_ANY_HOST)) {
379ca9cf
OF
3151 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
3152 nvlist_t *nvinfo;
3153
3154 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3155 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
3156 mmp_state = fnvlist_lookup_uint64(nvinfo,
3157 ZPOOL_CONFIG_MMP_STATE);
3158
3159 if (mmp_state == MMP_STATE_ACTIVE) {
3160 char *hostname = "<unknown>";
3161 uint64_t hostid = 0;
3162
3163 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3164 hostname = fnvlist_lookup_string(nvinfo,
3165 ZPOOL_CONFIG_MMP_HOSTNAME);
3166
3167 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3168 hostid = fnvlist_lookup_uint64(nvinfo,
3169 ZPOOL_CONFIG_MMP_HOSTID);
3170
3171 (void) fprintf(stderr, gettext("cannot import '%s': "
3172 "pool is imported on %s (hostid: "
3173 "0x%lx)\nExport the pool on the other system, "
3174 "then run 'zpool import'.\n"),
3175 name, hostname, (unsigned long) hostid);
3176 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
3177 (void) fprintf(stderr, gettext("Cannot import '%s': "
3178 "pool has the multihost property on and the\n"
3179 "system's hostid is not set. Set a unique hostid "
b9373170 3180 "with the zgenhostid(8) command.\n"), name);
379ca9cf
OF
3181 } else {
3182 char *hostname = "<unknown>";
3183 uint64_t timestamp = 0;
3184 uint64_t hostid = 0;
3185
3186 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
3187 hostname = fnvlist_lookup_string(config,
3188 ZPOOL_CONFIG_HOSTNAME);
3189
3190 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
3191 timestamp = fnvlist_lookup_uint64(config,
3192 ZPOOL_CONFIG_TIMESTAMP);
3193
3194 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
3195 hostid = fnvlist_lookup_uint64(config,
3196 ZPOOL_CONFIG_HOSTID);
3197
3198 (void) fprintf(stderr, gettext("cannot import '%s': "
3199 "pool was previously in use from another system.\n"
3200 "Last accessed by %s (hostid=%lx) at %s"
3201 "The pool can be imported, use 'zpool import -f' "
3202 "to import the pool.\n"), name, hostname,
3203 (unsigned long)hostid, ctime((time_t *)&timestamp));
34dc7c2f 3204 }
379ca9cf
OF
3205
3206 return (1);
34dc7c2f
BB
3207 }
3208
572e2857 3209 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
34dc7c2f
BB
3210 return (1);
3211
3212 if (newname != NULL)
3213 name = (char *)newname;
3214
45d1cae3
BB
3215 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
3216 return (1);
34dc7c2f 3217
b5256303
TC
3218 /*
3219 * Loading keys is best effort. We don't want to return immediately
3220 * if it fails but we do want to give the error to the caller.
3221 */
3222 if (flags & ZFS_IMPORT_LOAD_KEYS) {
3223 ret = zfs_crypto_attempt_load_keys(g_zfs, name);
3224 if (ret != 0)
3225 ret = 1;
3226 }
3227
d164b209 3228 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
572e2857 3229 !(flags & ZFS_IMPORT_ONLY) &&
d164b209 3230 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
34dc7c2f
BB
3231 zpool_close(zhp);
3232 return (1);
3233 }
3234
3235 zpool_close(zhp);
b5256303 3236 return (ret);
34dc7c2f
BB
3237}
3238
0936981d
GW
3239static int
3240import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
3241 char *orig_name, char *new_name,
3242 boolean_t do_destroyed, boolean_t pool_specified, boolean_t do_all,
3243 importargs_t *import)
3244{
3245 nvlist_t *config = NULL;
3246 nvlist_t *found_config = NULL;
3247 uint64_t pool_state;
3248
3249 /*
3250 * At this point we have a list of import candidate configs. Even if
3251 * we were searching by pool name or guid, we still need to
3252 * post-process the list to deal with pool state and possible
3253 * duplicate names.
3254 */
3255 int err = 0;
3256 nvpair_t *elem = NULL;
3257 boolean_t first = B_TRUE;
3258 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3259
3260 verify(nvpair_value_nvlist(elem, &config) == 0);
3261
3262 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3263 &pool_state) == 0);
3264 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
3265 continue;
3266 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
3267 continue;
3268
3269 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
3270 import->policy) == 0);
3271
3272 if (!pool_specified) {
3273 if (first)
3274 first = B_FALSE;
3275 else if (!do_all)
3276 (void) printf("\n");
3277
3278 if (do_all) {
3279 err |= do_import(config, NULL, mntopts,
3280 props, flags);
3281 } else {
3282 /*
3283 * If we're importing from cachefile, then
3284 * we don't want to report errors until we
3285 * are in the scan phase of the import. If
3286 * we get an error, then we return that error
3287 * to invoke the scan phase.
3288 */
3289 if (import->cachefile && !import->scan)
3290 err = show_import(config, B_FALSE);
3291 else
3292 (void) show_import(config, B_TRUE);
3293 }
3294 } else if (import->poolname != NULL) {
3295 char *name;
3296
3297 /*
3298 * We are searching for a pool based on name.
3299 */
3300 verify(nvlist_lookup_string(config,
3301 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
3302
3303 if (strcmp(name, import->poolname) == 0) {
3304 if (found_config != NULL) {
3305 (void) fprintf(stderr, gettext(
3306 "cannot import '%s': more than "
3307 "one matching pool\n"),
3308 import->poolname);
3309 (void) fprintf(stderr, gettext(
3310 "import by numeric ID instead\n"));
3311 err = B_TRUE;
3312 }
3313 found_config = config;
3314 }
3315 } else {
3316 uint64_t guid;
3317
3318 /*
3319 * Search for a pool by guid.
3320 */
3321 verify(nvlist_lookup_uint64(config,
3322 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
3323
3324 if (guid == import->guid)
3325 found_config = config;
3326 }
3327 }
3328
3329 /*
3330 * If we were searching for a specific pool, verify that we found a
3331 * pool, and then do the import.
3332 */
3333 if (pool_specified && err == 0) {
3334 if (found_config == NULL) {
3335 (void) fprintf(stderr, gettext("cannot import '%s': "
3336 "no such pool available\n"), orig_name);
3337 err = B_TRUE;
3338 } else {
3339 err |= do_import(found_config, new_name,
3340 mntopts, props, flags);
3341 }
3342 }
3343
3344 /*
3345 * If we were just looking for pools, report an error if none were
3346 * found.
3347 */
3348 if (!pool_specified && first)
3349 (void) fprintf(stderr,
3350 gettext("no pools available to import\n"));
3351 return (err);
3352}
3353
e89f1295
DB
3354typedef struct target_exists_args {
3355 const char *poolname;
3356 uint64_t poolguid;
3357} target_exists_args_t;
3358
3359static int
3360name_or_guid_exists(zpool_handle_t *zhp, void *data)
3361{
3362 target_exists_args_t *args = data;
3363 nvlist_t *config = zpool_get_config(zhp, NULL);
3364 int found = 0;
3365
3366 if (config == NULL)
3367 return (0);
3368
3369 if (args->poolname != NULL) {
3370 char *pool_name;
3371
3372 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3373 &pool_name) == 0);
3374 if (strcmp(pool_name, args->poolname) == 0)
3375 found = 1;
3376 } else {
3377 uint64_t pool_guid;
3378
3379 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3380 &pool_guid) == 0);
3381 if (pool_guid == args->poolguid)
3382 found = 1;
3383 }
3384 zpool_close(zhp);
3385
3386 return (found);
3387}
d2734cce
SD
3388/*
3389 * zpool checkpoint <pool>
3390 * checkpoint --discard <pool>
3391 *
3392 * -d Discard the checkpoint from a checkpointed
3393 * --discard pool.
3394 *
e60e158e
JG
3395 * -w Wait for discarding a checkpoint to complete.
3396 * --wait
3397 *
d2734cce
SD
3398 * Checkpoints the specified pool, by taking a "snapshot" of its
3399 * current state. A pool can only have one checkpoint at a time.
3400 */
3401int
3402zpool_do_checkpoint(int argc, char **argv)
3403{
e60e158e 3404 boolean_t discard, wait;
d2734cce
SD
3405 char *pool;
3406 zpool_handle_t *zhp;
3407 int c, err;
3408
3409 struct option long_options[] = {
3410 {"discard", no_argument, NULL, 'd'},
e60e158e 3411 {"wait", no_argument, NULL, 'w'},
d2734cce
SD
3412 {0, 0, 0, 0}
3413 };
3414
3415 discard = B_FALSE;
e60e158e
JG
3416 wait = B_FALSE;
3417 while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) {
d2734cce
SD
3418 switch (c) {
3419 case 'd':
3420 discard = B_TRUE;
3421 break;
e60e158e
JG
3422 case 'w':
3423 wait = B_TRUE;
3424 break;
d2734cce
SD
3425 case '?':
3426 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3427 optopt);
3428 usage(B_FALSE);
3429 }
3430 }
3431
e60e158e
JG
3432 if (wait && !discard) {
3433 (void) fprintf(stderr, gettext("--wait only valid when "
3434 "--discard also specified\n"));
3435 usage(B_FALSE);
3436 }
3437
d2734cce
SD
3438 argc -= optind;
3439 argv += optind;
3440
3441 if (argc < 1) {
3442 (void) fprintf(stderr, gettext("missing pool argument\n"));
3443 usage(B_FALSE);
3444 }
3445
3446 if (argc > 1) {
3447 (void) fprintf(stderr, gettext("too many arguments\n"));
3448 usage(B_FALSE);
3449 }
3450
3451 pool = argv[0];
3452
3453 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
3454 /* As a special case, check for use of '/' in the name */
3455 if (strchr(pool, '/') != NULL)
3456 (void) fprintf(stderr, gettext("'zpool checkpoint' "
3457 "doesn't work on datasets. To save the state "
3458 "of a dataset from a specific point in time "
3459 "please use 'zfs snapshot'\n"));
3460 return (1);
3461 }
3462
e60e158e 3463 if (discard) {
d2734cce 3464 err = (zpool_discard_checkpoint(zhp) != 0);
e60e158e
JG
3465 if (err == 0 && wait)
3466 err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD);
3467 } else {
d2734cce 3468 err = (zpool_checkpoint(zhp) != 0);
e60e158e 3469 }
d2734cce
SD
3470
3471 zpool_close(zhp);
3472
3473 return (err);
3474}
3475
3476#define CHECKPOINT_OPT 1024
3477
34dc7c2f
BB
3478/*
3479 * zpool import [-d dir] [-D]
b5256303 3480 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
0936981d 3481 * [-d dir | -c cachefile | -s] [-f] -a
b5256303 3482 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
0936981d
GW
3483 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3484 * [newpool]
34dc7c2f 3485 *
0936981d
GW
3486 * -c Read pool information from a cachefile instead of searching
3487 * devices. If importing from a cachefile config fails, then
3488 * fallback to searching for devices only in the directories that
3489 * exist in the cachefile.
34dc7c2f 3490 *
0936981d 3491 * -d Scan in a specific directory, other than /dev/. More than
34dc7c2f
BB
3492 * one directory can be specified using multiple '-d' options.
3493 *
0936981d
GW
3494 * -D Scan for previously destroyed pools or import all or only
3495 * specified destroyed pools.
34dc7c2f 3496 *
0936981d 3497 * -R Temporarily import the pool, with all mountpoints relative to
34dc7c2f
BB
3498 * the given root. The pool will remain exported when the machine
3499 * is rebooted.
3500 *
0936981d
GW
3501 * -V Import even in the presence of faulted vdevs. This is an
3502 * intentionally undocumented option for testing purposes, and
3503 * treats the pool configuration as complete, leaving any bad
45d1cae3
BB
3504 * vdevs in the FAULTED state. In other words, it does verbatim
3505 * import.
b128c09f 3506 *
0936981d 3507 * -f Force import, even if it appears that the pool is active.
428870ff 3508 *
0936981d 3509 * -F Attempt rewind if necessary.
428870ff 3510 *
0936981d 3511 * -n See if rewind would work, but don't actually rewind.
428870ff 3512 *
0936981d 3513 * -N Import the pool but don't mount datasets.
572e2857 3514 *
0936981d
GW
3515 * -T Specify a starting txg to use for import. This option is
3516 * intentionally undocumented option for testing purposes.
572e2857 3517 *
0936981d 3518 * -a Import all pools found.
34dc7c2f 3519 *
0936981d 3520 * -l Load encryption keys while importing.
b5256303 3521 *
0936981d 3522 * -o Set property=value and/or temporary mount options (without '=').
34dc7c2f 3523 *
0936981d
GW
3524 * -s Scan using the default search path, the libblkid cache will
3525 * not be consulted.
7d11e37e 3526 *
0936981d
GW
3527 * --rewind-to-checkpoint
3528 * Import the pool and revert back to the checkpoint.
d2734cce 3529 *
34dc7c2f
BB
3530 * The import command scans for pools to import, and import pools based on pool
3531 * name and GUID. The pool can also be renamed as part of the import process.
3532 */
3533int
3534zpool_do_import(int argc, char **argv)
3535{
3536 char **searchdirs = NULL;
44867b6d 3537 char *env, *envdup = NULL;
34dc7c2f
BB
3538 int nsearch = 0;
3539 int c;
428870ff 3540 int err = 0;
34dc7c2f
BB
3541 nvlist_t *pools = NULL;
3542 boolean_t do_all = B_FALSE;
3543 boolean_t do_destroyed = B_FALSE;
3544 char *mntopts = NULL;
b128c09f
BB
3545 uint64_t searchguid = 0;
3546 char *searchname = NULL;
34dc7c2f 3547 char *propval;
428870ff 3548 nvlist_t *policy = NULL;
34dc7c2f 3549 nvlist_t *props = NULL;
572e2857 3550 int flags = ZFS_IMPORT_NORMAL;
428870ff
BB
3551 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3552 boolean_t dryrun = B_FALSE;
3553 boolean_t do_rewind = B_FALSE;
3554 boolean_t xtreme_rewind = B_FALSE;
7d11e37e 3555 boolean_t do_scan = B_FALSE;
e89f1295 3556 boolean_t pool_exists = B_FALSE;
0936981d
GW
3557 boolean_t pool_specified = B_FALSE;
3558 uint64_t txg = -1ULL;
34dc7c2f 3559 char *cachefile = NULL;
428870ff 3560 importargs_t idata = { 0 };
572e2857 3561 char *endptr;
34dc7c2f 3562
d2734cce
SD
3563 struct option long_options[] = {
3564 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
3565 {0, 0, 0, 0}
3566 };
3567
34dc7c2f 3568 /* check options */
d2734cce
SD
3569 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
3570 long_options, NULL)) != -1) {
34dc7c2f
BB
3571 switch (c) {
3572 case 'a':
3573 do_all = B_TRUE;
3574 break;
3575 case 'c':
3576 cachefile = optarg;
3577 break;
3578 case 'd':
a281f769
AZ
3579 searchdirs = safe_realloc(searchdirs,
3580 (nsearch + 1) * sizeof (char *));
34dc7c2f
BB
3581 searchdirs[nsearch++] = optarg;
3582 break;
3583 case 'D':
3584 do_destroyed = B_TRUE;
3585 break;
3586 case 'f':
572e2857 3587 flags |= ZFS_IMPORT_ANY_HOST;
34dc7c2f 3588 break;
b128c09f 3589 case 'F':
428870ff
BB
3590 do_rewind = B_TRUE;
3591 break;
b5256303
TC
3592 case 'l':
3593 flags |= ZFS_IMPORT_LOAD_KEYS;
3594 break;
572e2857
BB
3595 case 'm':
3596 flags |= ZFS_IMPORT_MISSING_LOG;
3597 break;
428870ff
BB
3598 case 'n':
3599 dryrun = B_TRUE;
b128c09f 3600 break;
572e2857
BB
3601 case 'N':
3602 flags |= ZFS_IMPORT_ONLY;
3603 break;
34dc7c2f
BB
3604 case 'o':
3605 if ((propval = strchr(optarg, '=')) != NULL) {
3606 *propval = '\0';
3607 propval++;
b128c09f
BB
3608 if (add_prop_list(optarg, propval,
3609 &props, B_TRUE))
34dc7c2f
BB
3610 goto error;
3611 } else {
3612 mntopts = optarg;
3613 }
3614 break;
3615 case 'R':
3616 if (add_prop_list(zpool_prop_to_name(
b128c09f 3617 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
34dc7c2f 3618 goto error;
2f3ec900 3619 if (add_prop_list_default(zpool_prop_to_name(
e40ca391 3620 ZPOOL_PROP_CACHEFILE), "none", &props))
34dc7c2f
BB
3621 goto error;
3622 break;
7d11e37e
BB
3623 case 's':
3624 do_scan = B_TRUE;
3625 break;
26b42f3f
RY
3626 case 't':
3627 flags |= ZFS_IMPORT_TEMP_NAME;
00d2a8c9 3628 if (add_prop_list_default(zpool_prop_to_name(
e40ca391 3629 ZPOOL_PROP_CACHEFILE), "none", &props))
00d2a8c9 3630 goto error;
26b42f3f
RY
3631 break;
3632
572e2857
BB
3633 case 'T':
3634 errno = 0;
dea377c0 3635 txg = strtoull(optarg, &endptr, 0);
572e2857
BB
3636 if (errno != 0 || *endptr != '\0') {
3637 (void) fprintf(stderr,
3638 gettext("invalid txg value\n"));
3639 usage(B_FALSE);
3640 }
3641 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
3642 break;
428870ff 3643 case 'V':
572e2857 3644 flags |= ZFS_IMPORT_VERBATIM;
428870ff
BB
3645 break;
3646 case 'X':
3647 xtreme_rewind = B_TRUE;
3648 break;
d2734cce
SD
3649 case CHECKPOINT_OPT:
3650 flags |= ZFS_IMPORT_CHECKPOINT;
3651 break;
34dc7c2f
BB
3652 case ':':
3653 (void) fprintf(stderr, gettext("missing argument for "
3654 "'%c' option\n"), optopt);
3655 usage(B_FALSE);
3656 break;
3657 case '?':
3658 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3659 optopt);
3660 usage(B_FALSE);
3661 }
3662 }
3663
3664 argc -= optind;
3665 argv += optind;
3666
3667 if (cachefile && nsearch != 0) {
3668 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
3669 usage(B_FALSE);
3670 }
3671
0936981d
GW
3672 if (cachefile && do_scan) {
3673 (void) fprintf(stderr, gettext("-c is incompatible with -s\n"));
3674 usage(B_FALSE);
3675 }
3676
b5256303
TC
3677 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
3678 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
3679 usage(B_FALSE);
3680 }
3681
3682 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
3683 (void) fprintf(stderr, gettext("-l is only meaningful during "
3684 "an import\n"));
3685 usage(B_FALSE);
3686 }
3687
428870ff
BB
3688 if ((dryrun || xtreme_rewind) && !do_rewind) {
3689 (void) fprintf(stderr,
3690 gettext("-n or -X only meaningful with -F\n"));
3691 usage(B_FALSE);
3692 }
3693 if (dryrun)
3694 rewind_policy = ZPOOL_TRY_REWIND;
3695 else if (do_rewind)
3696 rewind_policy = ZPOOL_DO_REWIND;
3697 if (xtreme_rewind)
3698 rewind_policy |= ZPOOL_EXTREME_REWIND;
3699
3700 /* In the future, we can capture further policy and include it here */
3701 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
8a393be3
PZ
3702 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
3703 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
3704 rewind_policy) != 0)
428870ff
BB
3705 goto error;
3706
34dc7c2f
BB
3707 /* check argument count */
3708 if (do_all) {
3709 if (argc != 0) {
3710 (void) fprintf(stderr, gettext("too many arguments\n"));
3711 usage(B_FALSE);
3712 }
3713 } else {
3714 if (argc > 2) {
3715 (void) fprintf(stderr, gettext("too many arguments\n"));
3716 usage(B_FALSE);
3717 }
f74b821a 3718 }
34dc7c2f 3719
f74b821a
BB
3720 /*
3721 * Check for the effective uid. We do this explicitly here because
3722 * otherwise any attempt to discover pools will silently fail.
3723 */
3724 if (argc == 0 && geteuid() != 0) {
3725 (void) fprintf(stderr, gettext("cannot "
3726 "discover pools: permission denied\n"));
3727 if (searchdirs != NULL)
3728 free(searchdirs);
d603ed6c 3729
a425f5bf 3730 nvlist_free(props);
f74b821a
BB
3731 nvlist_free(policy);
3732 return (1);
34dc7c2f
BB
3733 }
3734
34dc7c2f 3735 /*
34dc7c2f
BB
3736 * Depending on the arguments given, we do one of the following:
3737 *
3738 * <none> Iterate through all pools and display information about
3739 * each one.
3740 *
3741 * -a Iterate through all pools and try to import each one.
3742 *
3743 * <id> Find the pool that corresponds to the given GUID/pool
3744 * name and import that one.
3745 *
3746 * -D Above options applies only to destroyed pools.
3747 */
3748 if (argc != 0) {
3749 char *endptr;
3750
3751 errno = 0;
3752 searchguid = strtoull(argv[0], &endptr, 10);
eaa52d32 3753 if (errno != 0 || *endptr != '\0') {
34dc7c2f 3754 searchname = argv[0];
eaa52d32
PJ
3755 searchguid = 0;
3756 }
0936981d 3757 pool_specified = B_TRUE;
34dc7c2f 3758
b128c09f 3759 /*
428870ff 3760 * User specified a name or guid. Ensure it's unique.
b128c09f 3761 */
e89f1295
DB
3762 target_exists_args_t search = {searchname, searchguid};
3763 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search);
b128c09f
BB
3764 }
3765
44867b6d
BB
3766 /*
3767 * Check the environment for the preferred search path.
3768 */
3769 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
a281f769 3770 char *dir, *tmp = NULL;
44867b6d
BB
3771
3772 envdup = strdup(env);
3773
a281f769
AZ
3774 for (dir = strtok_r(envdup, ":", &tmp);
3775 dir != NULL;
3776 dir = strtok_r(NULL, ":", &tmp)) {
3777 searchdirs = safe_realloc(searchdirs,
3778 (nsearch + 1) * sizeof (char *));
44867b6d 3779 searchdirs[nsearch++] = dir;
44867b6d
BB
3780 }
3781 }
428870ff
BB
3782
3783 idata.path = searchdirs;
3784 idata.paths = nsearch;
3785 idata.poolname = searchname;
3786 idata.guid = searchguid;
3787 idata.cachefile = cachefile;
7d11e37e 3788 idata.scan = do_scan;
6cb8e530 3789 idata.policy = policy;
428870ff 3790
e89f1295 3791 pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops);
428870ff 3792
e89f1295 3793 if (pools != NULL && pool_exists &&
428870ff
BB
3794 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
3795 (void) fprintf(stderr, gettext("cannot import '%s': "
3796 "a pool with that name already exists\n"),
3797 argv[0]);
3798 (void) fprintf(stderr, gettext("use the form '%s "
3799 "<pool | id> <newpool>' to give it a new name\n"),
3800 "zpool import");
3801 err = 1;
e89f1295 3802 } else if (pools == NULL && pool_exists) {
428870ff
BB
3803 (void) fprintf(stderr, gettext("cannot import '%s': "
3804 "a pool with that name is already created/imported,\n"),
3805 argv[0]);
3806 (void) fprintf(stderr, gettext("and no additional pools "
3807 "with that name were found\n"));
3808 err = 1;
3809 } else if (pools == NULL) {
b128c09f
BB
3810 if (argc != 0) {
3811 (void) fprintf(stderr, gettext("cannot import '%s': "
3812 "no such pool available\n"), argv[0]);
3813 }
428870ff
BB
3814 err = 1;
3815 }
3816
3817 if (err == 1) {
a281f769
AZ
3818 free(searchdirs);
3819 free(envdup);
428870ff 3820 nvlist_free(policy);
a425f5bf 3821 nvlist_free(pools);
3822 nvlist_free(props);
b128c09f
BB
3823 return (1);
3824 }
3825
d8381f50
MA
3826 err = import_pools(pools, props, mntopts, flags,
3827 argc >= 1 ? argv[0] : NULL,
3828 argc >= 2 ? argv[1] : NULL,
3829 do_destroyed, pool_specified, do_all, &idata);
0936981d 3830
b128c09f 3831 /*
0936981d
GW
3832 * If we're using the cachefile and we failed to import, then
3833 * fallback to scanning the directory for pools that match
3834 * those in the cachefile.
b128c09f 3835 */
0936981d
GW
3836 if (err != 0 && cachefile != NULL) {
3837 (void) printf(gettext("cachefile import failed, retrying\n"));
34dc7c2f 3838
0936981d
GW
3839 /*
3840 * We use the scan flag to gather the directories that exist
3841 * in the cachefile. If we need to fallback to searching for
3842 * the pool config, we will only search devices in these
3843 * directories.
3844 */
3845 idata.scan = B_TRUE;
3846 nvlist_free(pools);
3847 pools = zpool_search_import(g_zfs, &idata, &libzfs_config_ops);
34dc7c2f 3848
d8381f50
MA
3849 err = import_pools(pools, props, mntopts, flags,
3850 argc >= 1 ? argv[0] : NULL,
3851 argc >= 2 ? argv[1] : NULL,
3852 do_destroyed, pool_specified, do_all, &idata);
34dc7c2f
BB
3853 }
3854
34dc7c2f
BB
3855error:
3856 nvlist_free(props);
3857 nvlist_free(pools);
428870ff 3858 nvlist_free(policy);
a281f769
AZ
3859 free(searchdirs);
3860 free(envdup);
34dc7c2f
BB
3861
3862 return (err ? 1 : 0);
3863}
3864
bec1067d
AP
3865/*
3866 * zpool sync [-f] [pool] ...
3867 *
3868 * -f (undocumented) force uberblock (and config including zpool cache file)
3869 * update.
3870 *
3871 * Sync the specified pool(s).
3872 * Without arguments "zpool sync" will sync all pools.
3873 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3874 *
3875 */
3876static int
3877zpool_do_sync(int argc, char **argv)
3878{
3879 int ret;
3880 boolean_t force = B_FALSE;
3881
3882 /* check options */
3883 while ((ret = getopt(argc, argv, "f")) != -1) {
3884 switch (ret) {
3885 case 'f':
3886 force = B_TRUE;
3887 break;
3888 case '?':
3889 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3890 optopt);
3891 usage(B_FALSE);
3892 }
3893 }
3894
3895 argc -= optind;
3896 argv += optind;
3897
3898 /* if argc == 0 we will execute zpool_sync_one on all pools */
2a673e76
AJ
3899 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
3900 B_FALSE, zpool_sync_one, &force);
bec1067d
AP
3901
3902 return (ret);
3903}
3904
34dc7c2f 3905typedef struct iostat_cbdata {
193a37cb 3906 uint64_t cb_flags;
34dc7c2f 3907 int cb_namewidth;
1bd201e7 3908 int cb_iteration;
193a37cb
TH
3909 boolean_t cb_verbose;
3910 boolean_t cb_literal;
3911 boolean_t cb_scripted;
1bd201e7 3912 zpool_list_t *cb_list;
8720e9e7 3913 vdev_cmd_data_list_t *vcdl;
2a673e76 3914 vdev_cbdata_t cb_vdevs;
34dc7c2f
BB
3915} iostat_cbdata_t;
3916
193a37cb
TH
3917/* iostat labels */
3918typedef struct name_and_columns {
3919 const char *name; /* Column name */
3920 unsigned int columns; /* Center name to this number of columns */
3921} name_and_columns_t;
3922
00888c08 3923#define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
193a37cb
TH
3924
3925static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
3926{
3927 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3928 {NULL}},
3929 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
00888c08
TB
3930 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3931 {NULL}},
193a37cb
TH
3932 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
3933 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
00888c08 3934 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}},
57dc41de
TH
3935 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3936 {"asyncq_wait", 2}, {NULL}},
7e945072 3937 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
1b939560 3938 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
00888c08 3939 {"trim", 2}, {"rebuild", 2}, {NULL}},
193a37cb
TH
3940};
3941
3942/* Shorthand - if "columns" field not set, default to 1 column */
3943static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
3944{
3945 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3946 {"write"}, {NULL}},
3947 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
00888c08
TB
3948 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3949 {NULL}},
193a37cb 3950 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
1b939560 3951 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
00888c08 3952 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
193a37cb 3953 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
00888c08
TB
3954 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3955 {NULL}},
7e945072 3956 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
00888c08
TB
3957 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3958 {"ind"}, {"agg"}, {NULL}},
7e945072
TH
3959};
3960
3961static const char *histo_to_title[] = {
3962 [IOS_L_HISTO] = "latency",
3963 [IOS_RQ_HISTO] = "req_size",
193a37cb
TH
3964};
3965
3966/*
3967 * Return the number of labels in a null-terminated name_and_columns_t
3968 * array.
3969 *
3970 */
3971static unsigned int
3972label_array_len(const name_and_columns_t *labels)
3973{
3974 int i = 0;
3975
3976 while (labels[i].name)
3977 i++;
3978
3979 return (i);
3980}
3981
7e945072
TH
3982/*
3983 * Return the number of strings in a null-terminated string array.
3984 * For example:
3985 *
3986 * const char foo[] = {"bar", "baz", NULL}
3987 *
3988 * returns 2
3989 */
3990static uint64_t
3991str_array_len(const char *array[])
3992{
3993 uint64_t i = 0;
3994 while (array[i])
3995 i++;
3996
3997 return (i);
3998}
3999
4000
193a37cb
TH
4001/*
4002 * Return a default column width for default/latency/queue columns. This does
4003 * not include histograms, which have their columns autosized.
4004 */
4005static unsigned int
4006default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
4007{
4008 unsigned long column_width = 5; /* Normal niceprint */
4009 static unsigned long widths[] = {
4010 /*
4011 * Choose some sane default column sizes for printing the
4012 * raw numbers.
4013 */
4014 [IOS_DEFAULT] = 15, /* 1PB capacity */
4015 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
4016 [IOS_QUEUES] = 6, /* 1M queue entries */
1b939560
BB
4017 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */
4018 [IOS_RQ_HISTO] = 6, /* 1M queue entries */
193a37cb
TH
4019 };
4020
4021 if (cb->cb_literal)
4022 column_width = widths[type];
4023
4024 return (column_width);
4025}
4026
4027/*
4028 * Print the column labels, i.e:
4029 *
4030 * capacity operations bandwidth
4031 * alloc free read write read write ...
4032 *
4033 * If force_column_width is set, use it for the column width. If not set, use
4034 * the default column width.
4035 */
65c7cc49 4036static void
193a37cb
TH
4037print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
4038 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
4039{
4040 int i, idx, s;
1b939560 4041 int text_start, rw_column_width, spaces_to_end;
193a37cb
TH
4042 uint64_t flags = cb->cb_flags;
4043 uint64_t f;
4044 unsigned int column_width = force_column_width;
4045
4046 /* For each bit set in flags */
4047 for (f = flags; f; f &= ~(1ULL << idx)) {
4048 idx = lowbit64(f) - 1;
4049 if (!force_column_width)
4050 column_width = default_column_width(cb, idx);
4051 /* Print our top labels centered over "read write" label. */
4052 for (i = 0; i < label_array_len(labels[idx]); i++) {
4053 const char *name = labels[idx][i].name;
4054 /*
4055 * We treat labels[][].columns == 0 as shorthand
4056 * for one column. It makes writing out the label
4057 * tables more concise.
4058 */
4059 unsigned int columns = MAX(1, labels[idx][i].columns);
4060 unsigned int slen = strlen(name);
4061
4062 rw_column_width = (column_width * columns) +
4063 (2 * (columns - 1));
4064
1b939560
BB
4065 text_start = (int)((rw_column_width) / columns -
4066 slen / columns);
4067 if (text_start < 0)
4068 text_start = 0;
193a37cb
TH
4069
4070 printf(" "); /* Two spaces between columns */
4071
4072 /* Space from beginning of column to label */
4073 for (s = 0; s < text_start; s++)
4074 printf(" ");
4075
4076 printf("%s", name);
4077
4078 /* Print space after label to end of column */
4079 spaces_to_end = rw_column_width - text_start - slen;
1b939560
BB
4080 if (spaces_to_end < 0)
4081 spaces_to_end = 0;
4082
193a37cb
TH
4083 for (s = 0; s < spaces_to_end; s++)
4084 printf(" ");
193a37cb
TH
4085 }
4086 }
193a37cb
TH
4087}
4088
d6418de0
TH
4089
4090/*
4091 * print_cmd_columns - Print custom column titles from -c
4092 *
4093 * If the user specified the "zpool status|iostat -c" then print their custom
4094 * column titles in the header. For example, print_cmd_columns() would print
4095 * the " col1 col2" part of this:
4096 *
4097 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4098 * ...
4099 * capacity operations bandwidth
4100 * pool alloc free read write read write col1 col2
4101 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4102 * mypool 269K 1008M 0 0 107 946
4103 * mirror 269K 1008M 0 0 107 946
4104 * sdb - - 0 0 102 473 val1 val2
4105 * sdc - - 0 0 5 473 val1 val2
4106 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4107 */
65c7cc49 4108static void
d6418de0
TH
4109print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
4110{
4111 int i, j;
4112 vdev_cmd_data_t *data = &vcdl->data[0];
4113
4114 if (vcdl->count == 0 || data == NULL)
4115 return;
4116
4117 /*
4118 * Each vdev cmd should have the same column names unless the user did
4119 * something weird with their cmd. Just take the column names from the
4120 * first vdev and assume it works for all of them.
4121 */
4122 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
4123 printf(" ");
4124 if (use_dashes) {
4125 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
4126 printf("-");
4127 } else {
9fb2771a 4128 printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i],
d6418de0
TH
4129 vcdl->uniq_cols[i]);
4130 }
4131 }
4132}
4133
4134
193a37cb
TH
4135/*
4136 * Utility function to print out a line of dashes like:
4137 *
4138 * -------------------------------- ----- ----- ----- ----- -----
4139 *
4140 * ...or a dashed named-row line like:
4141 *
4142 * logs - - - - -
4143 *
4144 * @cb: iostat data
4145 *
4146 * @force_column_width If non-zero, use the value as the column width.
4147 * Otherwise use the default column widths.
4148 *
4149 * @name: Print a dashed named-row line starting
4150 * with @name. Otherwise, print a regular
4151 * dashed line.
4152 */
4153static void
4154print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
4155 const char *name)
4156{
4157 int i;
4158 unsigned int namewidth;
4159 uint64_t flags = cb->cb_flags;
4160 uint64_t f;
4161 int idx;
4162 const name_and_columns_t *labels;
7e945072
TH
4163 const char *title;
4164
4165
4166 if (cb->cb_flags & IOS_ANYHISTO_M) {
4167 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2a673e76 4168 } else if (cb->cb_vdevs.cb_names_count) {
7e945072
TH
4169 title = "vdev";
4170 } else {
4171 title = "pool";
4172 }
4173
4174 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4175 name ? strlen(name) : 0);
193a37cb 4176
193a37cb
TH
4177
4178 if (name) {
193a37cb
TH
4179 printf("%-*s", namewidth, name);
4180 } else {
4181 for (i = 0; i < namewidth; i++)
4182 (void) printf("-");
4183 }
4184
4185 /* For each bit in flags */
4186 for (f = flags; f; f &= ~(1ULL << idx)) {
4187 unsigned int column_width;
4188 idx = lowbit64(f) - 1;
4189 if (force_column_width)
4190 column_width = force_column_width;
4191 else
4192 column_width = default_column_width(cb, idx);
4193
4194 labels = iostat_bottom_labels[idx];
4195 for (i = 0; i < label_array_len(labels); i++) {
4196 if (name)
4197 printf(" %*s-", column_width - 1, " ");
4198 else
4199 printf(" %.*s", column_width,
4200 "--------------------");
4201 }
4202 }
193a37cb
TH
4203}
4204
4205
4206static void
4207print_iostat_separator_impl(iostat_cbdata_t *cb,
4208 unsigned int force_column_width)
4209{
4210 print_iostat_dashes(cb, force_column_width, NULL);
4211}
4212
34dc7c2f
BB
4213static void
4214print_iostat_separator(iostat_cbdata_t *cb)
4215{
193a37cb
TH
4216 print_iostat_separator_impl(cb, 0);
4217}
4218
4219static void
4220print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
4221 const char *histo_vdev_name)
4222{
4223 unsigned int namewidth;
7e945072
TH
4224 const char *title;
4225
4226 if (cb->cb_flags & IOS_ANYHISTO_M) {
4227 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
2a673e76 4228 } else if (cb->cb_vdevs.cb_names_count) {
7e945072
TH
4229 title = "vdev";
4230 } else {
4231 title = "pool";
4232 }
34dc7c2f 4233
7e945072
TH
4234 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4235 histo_vdev_name ? strlen(histo_vdev_name) : 0);
193a37cb 4236
7e945072 4237 if (histo_vdev_name)
193a37cb
TH
4238 printf("%-*s", namewidth, histo_vdev_name);
4239 else
4240 printf("%*s", namewidth, "");
4241
7e945072 4242
193a37cb 4243 print_iostat_labels(cb, force_column_width, iostat_top_labels);
d6418de0 4244 printf("\n");
193a37cb 4245
7e945072 4246 printf("%-*s", namewidth, title);
193a37cb
TH
4247
4248 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
d6418de0
TH
4249 if (cb->vcdl != NULL)
4250 print_cmd_columns(cb->vcdl, 0);
4251
4252 printf("\n");
193a37cb
TH
4253
4254 print_iostat_separator_impl(cb, force_column_width);
d6418de0
TH
4255
4256 if (cb->vcdl != NULL)
4257 print_cmd_columns(cb->vcdl, 1);
4258
4259 printf("\n");
34dc7c2f
BB
4260}
4261
4262static void
4263print_iostat_header(iostat_cbdata_t *cb)
4264{
193a37cb 4265 print_iostat_header_impl(cb, 0, NULL);
34dc7c2f
BB
4266}
4267
193a37cb 4268
34dc7c2f
BB
4269/*
4270 * Display a single statistic.
4271 */
4272static void
193a37cb
TH
4273print_one_stat(uint64_t value, enum zfs_nicenum_format format,
4274 unsigned int column_size, boolean_t scripted)
34dc7c2f
BB
4275{
4276 char buf[64];
4277
193a37cb
TH
4278 zfs_nicenum_format(value, buf, sizeof (buf), format);
4279
4280 if (scripted)
4281 printf("\t%s", buf);
4282 else
4283 printf(" %*s", column_size, buf);
4284}
4285
4286/*
4287 * Calculate the default vdev stats
4288 *
4289 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4290 * stats into calcvs.
4291 */
4292static void
4293calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
4294 vdev_stat_t *calcvs)
4295{
4296 int i;
4297
4298 memcpy(calcvs, newvs, sizeof (*calcvs));
4299 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
4300 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
4301
4302 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
4303 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
4304}
4305
4306/*
4307 * Internal representation of the extended iostats data.
4308 *
4309 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4310 * or single uint64_t's. We make both look like arrays to make them easier
4311 * to process. In order to make single uint64_t's look like arrays, we set
4312 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4313 * we can just use *data and count.
4314 */
4315struct stat_array {
4316 uint64_t *data;
4317 uint_t count; /* Number of entries in data[] */
4318 uint64_t __data; /* Only used when data is a single uint64_t */
4319};
4320
4321static uint64_t
4ea3f864
GM
4322stat_histo_max(struct stat_array *nva, unsigned int len)
4323{
193a37cb
TH
4324 uint64_t max = 0;
4325 int i;
4326 for (i = 0; i < len; i++)
4327 max = MAX(max, array64_max(nva[i].data, nva[i].count));
4328
4329 return (max);
4330}
4331
4332/*
4333 * Helper function to lookup a uint64_t array or uint64_t value and store its
4334 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4335 * it look like a one element array to make it easier to process.
4336 */
4337static int
4338nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
4ea3f864
GM
4339 struct stat_array *nva)
4340{
193a37cb
TH
4341 nvpair_t *tmp;
4342 int ret;
4343
4344 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
4345 switch (nvpair_type(tmp)) {
4346 case DATA_TYPE_UINT64_ARRAY:
4347 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
4348 break;
4349 case DATA_TYPE_UINT64:
4350 ret = nvpair_value_uint64(tmp, &nva->__data);
4351 nva->data = &nva->__data;
4352 nva->count = 1;
4353 break;
4354 default:
4355 /* Not a uint64_t */
4356 ret = EINVAL;
4357 break;
4358 }
4359
4360 return (ret);
4361}
4362
4363/*
4364 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4365 * subtract them, and return the results in a newly allocated stat_array.
4366 * You must free the returned array after you are done with it with
4367 * free_calc_stats().
4368 *
4369 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4370 * values.
4371 */
4372static struct stat_array *
4373calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
4374 nvlist_t *newnv)
4375{
4376 nvlist_t *oldnvx = NULL, *newnvx;
4377 struct stat_array *oldnva, *newnva, *calcnva;
4378 int i, j;
4379 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
4380
4381 /* Extract our extended stats nvlist from the main list */
4382 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
4383 &newnvx) == 0);
4384 if (oldnv) {
4385 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
4386 &oldnvx) == 0);
4387 }
4388
4389 newnva = safe_malloc(alloc_size);
4390 oldnva = safe_malloc(alloc_size);
4391 calcnva = safe_malloc(alloc_size);
4392
4393 for (j = 0; j < len; j++) {
4394 verify(nvpair64_to_stat_array(newnvx, names[j],
4395 &newnva[j]) == 0);
4396 calcnva[j].count = newnva[j].count;
4397 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
4398 calcnva[j].data = safe_malloc(alloc_size);
4399 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
4400
4401 if (oldnvx) {
4402 verify(nvpair64_to_stat_array(oldnvx, names[j],
4403 &oldnva[j]) == 0);
4404 for (i = 0; i < oldnva[j].count; i++)
4405 calcnva[j].data[i] -= oldnva[j].data[i];
4406 }
4407 }
4408 free(newnva);
4409 free(oldnva);
4410 return (calcnva);
4411}
4412
4413static void
4414free_calc_stats(struct stat_array *nva, unsigned int len)
4415{
4416 int i;
4417 for (i = 0; i < len; i++)
4418 free(nva[i].data);
4419
4420 free(nva);
4421}
4422
4423static void
4424print_iostat_histo(struct stat_array *nva, unsigned int len,
4425 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
4426 double scale)
4427{
4428 int i, j;
4429 char buf[6];
4430 uint64_t val;
4431 enum zfs_nicenum_format format;
4432 unsigned int buckets;
7e945072 4433 unsigned int start_bucket;
193a37cb
TH
4434
4435 if (cb->cb_literal)
4436 format = ZFS_NICENUM_RAW;
4437 else
4438 format = ZFS_NICENUM_1024;
4439
4440 /* All these histos are the same size, so just use nva[0].count */
4441 buckets = nva[0].count;
4442
7e945072
TH
4443 if (cb->cb_flags & IOS_RQ_HISTO_M) {
4444 /* Start at 512 - req size should never be lower than this */
4445 start_bucket = 9;
4446 } else {
4447 start_bucket = 0;
4448 }
193a37cb 4449
7e945072 4450 for (j = start_bucket; j < buckets; j++) {
193a37cb 4451 /* Print histogram bucket label */
7e945072
TH
4452 if (cb->cb_flags & IOS_L_HISTO_M) {
4453 /* Ending range of this bucket */
4454 val = (1UL << (j + 1)) - 1;
4455 zfs_nicetime(val, buf, sizeof (buf));
4456 } else {
4457 /* Request size (starting range of bucket) */
4458 val = (1UL << j);
4459 zfs_nicenum(val, buf, sizeof (buf));
4460 }
4461
193a37cb 4462 if (cb->cb_scripted)
02730c33 4463 printf("%llu", (u_longlong_t)val);
193a37cb
TH
4464 else
4465 printf("%-*s", namewidth, buf);
4466
4467 /* Print the values on the line */
4468 for (i = 0; i < len; i++) {
4469 print_one_stat(nva[i].data[j] * scale, format,
4470 column_width, cb->cb_scripted);
4471 }
4472 printf("\n");
4473 }
4474}
4475
4476static void
4477print_solid_separator(unsigned int length)
4478{
4479 while (length--)
4480 printf("-");
4481 printf("\n");
4482}
4483
4484static void
4485print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
4486 nvlist_t *newnv, double scale, const char *name)
4487{
4488 unsigned int column_width;
4489 unsigned int namewidth;
4490 unsigned int entire_width;
7e945072 4491 enum iostat_type type;
193a37cb 4492 struct stat_array *nva;
7e945072
TH
4493 const char **names;
4494 unsigned int names_len;
4495
4496 /* What type of histo are we? */
4497 type = IOS_HISTO_IDX(cb->cb_flags);
4498
4499 /* Get NULL-terminated array of nvlist names for our histo */
4500 names = vsx_type_to_nvlist[type];
4501 names_len = str_array_len(names); /* num of names */
4502
4503 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
193a37cb
TH
4504
4505 if (cb->cb_literal) {
4506 column_width = MAX(5,
7e945072 4507 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
193a37cb
TH
4508 } else {
4509 column_width = 5;
4510 }
4511
7e945072
TH
4512 namewidth = MAX(cb->cb_namewidth,
4513 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
193a37cb
TH
4514
4515 /*
4516 * Calculate the entire line width of what we're printing. The
4517 * +2 is for the two spaces between columns:
4518 */
4519 /* read write */
4520 /* ----- ----- */
4521 /* |___| <---------- column_width */
4522 /* */
4523 /* |__________| <--- entire_width */
4524 /* */
4525 entire_width = namewidth + (column_width + 2) *
7e945072 4526 label_array_len(iostat_bottom_labels[type]);
193a37cb
TH
4527
4528 if (cb->cb_scripted)
4529 printf("%s\n", name);
4530 else
4531 print_iostat_header_impl(cb, column_width, name);
4532
7e945072 4533 print_iostat_histo(nva, names_len, cb, column_width,
193a37cb
TH
4534 namewidth, scale);
4535
7e945072 4536 free_calc_stats(nva, names_len);
193a37cb
TH
4537 if (!cb->cb_scripted)
4538 print_solid_separator(entire_width);
4539}
4540
4541/*
4542 * Calculate the average latency of a power-of-two latency histogram
4543 */
4544static uint64_t
4545single_histo_average(uint64_t *histo, unsigned int buckets)
4546{
4547 int i;
4548 uint64_t count = 0, total = 0;
4549
4550 for (i = 0; i < buckets; i++) {
4551 /*
4552 * Our buckets are power-of-two latency ranges. Use the
4553 * midpoint latency of each bucket to calculate the average.
4554 * For example:
4555 *
4556 * Bucket Midpoint
4557 * 8ns-15ns: 12ns
4558 * 16ns-31ns: 24ns
4559 * ...
4560 */
4561 if (histo[i] != 0) {
4562 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
4563 count += histo[i];
4564 }
4565 }
4566
4567 /* Prevent divide by zero */
4568 return (count == 0 ? 0 : total / count);
4569}
4570
4571static void
e40ca391 4572print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv)
193a37cb 4573{
193a37cb
TH
4574 const char *names[] = {
4575 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
4576 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
4577 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
4578 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
4579 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
4580 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
4581 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
4582 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
4583 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
4584 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
1b939560
BB
4585 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
4586 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
00888c08
TB
4587 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
4588 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
193a37cb
TH
4589 };
4590
4591 struct stat_array *nva;
4592
4593 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
4594 enum zfs_nicenum_format format;
4595
4596 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
4597
4598 if (cb->cb_literal)
4599 format = ZFS_NICENUM_RAW;
4600 else
4601 format = ZFS_NICENUM_1024;
4602
e40ca391
AZ
4603 for (int i = 0; i < ARRAY_SIZE(names); i++) {
4604 uint64_t val = nva[i].data[0];
193a37cb
TH
4605 print_one_stat(val, format, column_width, cb->cb_scripted);
4606 }
4607
4608 free_calc_stats(nva, ARRAY_SIZE(names));
4609}
4610
4611static void
4612print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
b954e36e 4613 nvlist_t *newnv)
193a37cb
TH
4614{
4615 int i;
4616 uint64_t val;
4617 const char *names[] = {
4618 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
4619 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
4620 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
4621 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
4622 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
4623 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
4624 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
4625 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
4626 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
1b939560 4627 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
00888c08 4628 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
193a37cb
TH
4629 };
4630 struct stat_array *nva;
4631
4632 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
4633 enum zfs_nicenum_format format;
4634
4635 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
4636
4637 if (cb->cb_literal)
29eb4942 4638 format = ZFS_NICENUM_RAWTIME;
193a37cb
TH
4639 else
4640 format = ZFS_NICENUM_TIME;
4641
4642 /* Print our avg latencies on the line */
4643 for (i = 0; i < ARRAY_SIZE(names); i++) {
4644 /* Compute average latency for a latency histo */
b954e36e 4645 val = single_histo_average(nva[i].data, nva[i].count);
193a37cb
TH
4646 print_one_stat(val, format, column_width, cb->cb_scripted);
4647 }
4648 free_calc_stats(nva, ARRAY_SIZE(names));
4649}
4650
4651/*
4652 * Print default statistics (capacity/operations/bandwidth)
4653 */
4654static void
4655print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
4656{
4657 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
4658 enum zfs_nicenum_format format;
4659 char na; /* char to print for "not applicable" values */
4660
4661 if (cb->cb_literal) {
4662 format = ZFS_NICENUM_RAW;
4663 na = '0';
4664 } else {
4665 format = ZFS_NICENUM_1024;
4666 na = '-';
4667 }
4668
4669 /* only toplevel vdevs have capacity stats */
4670 if (vs->vs_space == 0) {
4671 if (cb->cb_scripted)
4672 printf("\t%c\t%c", na, na);
4673 else
4674 printf(" %*c %*c", column_width, na, column_width,
4675 na);
4676 } else {
4677 print_one_stat(vs->vs_alloc, format, column_width,
4678 cb->cb_scripted);
4679 print_one_stat(vs->vs_space - vs->vs_alloc, format,
4680 column_width, cb->cb_scripted);
4681 }
4682
4683 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
4684 format, column_width, cb->cb_scripted);
4685 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
4686 format, column_width, cb->cb_scripted);
4687 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
4688 format, column_width, cb->cb_scripted);
4689 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
4690 format, column_width, cb->cb_scripted);
34dc7c2f
BB
4691}
4692
cc99f275
DB
4693static const char *class_name[] = {
4694 VDEV_ALLOC_BIAS_DEDUP,
4695 VDEV_ALLOC_BIAS_SPECIAL,
4696 VDEV_ALLOC_CLASS_LOGS
4697};
4698
34dc7c2f
BB
4699/*
4700 * Print out all the statistics for the given vdev. This can either be the
4701 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4702 * is a verbose output, and we don't want to display the toplevel pool stats.
193a37cb
TH
4703 *
4704 * Returns the number of stat lines printed.
34dc7c2f 4705 */
cc99f275 4706static unsigned int
34dc7c2f
BB
4707print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
4708 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
4709{
4710 nvlist_t **oldchild, **newchild;
674b8934 4711 uint_t c, children, oldchildren;
193a37cb 4712 vdev_stat_t *oldvs, *newvs, *calcvs;
34dc7c2f 4713 vdev_stat_t zerovs = { 0 };
193a37cb
TH
4714 char *vname;
4715 int i;
4716 int ret = 0;
34dc7c2f
BB
4717 uint64_t tdelta;
4718 double scale;
193a37cb 4719
a1d477c2
MA
4720 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
4721 return (ret);
4722
75c62019
JCML
4723 calcvs = safe_malloc(sizeof (*calcvs));
4724
34dc7c2f 4725 if (oldnv != NULL) {
428870ff
BB
4726 verify(nvlist_lookup_uint64_array(oldnv,
4727 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
34dc7c2f
BB
4728 } else {
4729 oldvs = &zerovs;
4730 }
4731
193a37cb 4732 /* Do we only want to see a specific vdev? */
2a673e76 4733 for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) {
193a37cb 4734 /* Yes we do. Is this the vdev? */
2a673e76 4735 if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) {
193a37cb
TH
4736 /*
4737 * This is our vdev. Since it is the only vdev we
4738 * will be displaying, make depth = 0 so that it
4739 * doesn't get indented.
4740 */
4741 depth = 0;
4742 break;
4743 }
4744 }
4745
2a673e76 4746 if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) {
193a37cb
TH
4747 /* Couldn't match the name */
4748 goto children;
4749 }
4750
4751
428870ff 4752 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
4753 (uint64_t **)&newvs, &c) == 0);
4754
193a37cb
TH
4755 /*
4756 * Print the vdev name unless it's is a histogram. Histograms
4757 * display the vdev name in the header itself.
4758 */
7e945072 4759 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
193a37cb
TH
4760 if (cb->cb_scripted) {
4761 printf("%s", name);
4762 } else {
4763 if (strlen(name) + depth > cb->cb_namewidth)
4764 (void) printf("%*s%s", depth, "", name);
4765 else
4766 (void) printf("%*s%s%*s", depth, "", name,
4767 (int)(cb->cb_namewidth - strlen(name) -
4768 depth), "");
4769 }
4770 }
34dc7c2f 4771
193a37cb 4772 /* Calculate our scaling factor */
34dc7c2f 4773 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
7e945072 4774 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
193a37cb
TH
4775 /*
4776 * If we specify printing histograms with no time interval, then
4777 * print the histogram numbers over the entire lifetime of the
4778 * vdev.
4779 */
4780 scale = 1;
34dc7c2f 4781 } else {
193a37cb
TH
4782 if (tdelta == 0)
4783 scale = 1.0;
4784 else
4785 scale = (double)NANOSEC / tdelta;
34dc7c2f
BB
4786 }
4787
193a37cb
TH
4788 if (cb->cb_flags & IOS_DEFAULT_M) {
4789 calc_default_iostats(oldvs, newvs, calcvs);
4790 print_iostat_default(calcvs, cb, scale);
4791 }
4792 if (cb->cb_flags & IOS_LATENCY_M)
b954e36e 4793 print_iostat_latency(cb, oldnv, newnv);
193a37cb 4794 if (cb->cb_flags & IOS_QUEUES_M)
e40ca391 4795 print_iostat_queues(cb, newnv);
7e945072 4796 if (cb->cb_flags & IOS_ANYHISTO_M) {
193a37cb
TH
4797 printf("\n");
4798 print_iostat_histos(cb, oldnv, newnv, scale, name);
4799 }
34dc7c2f 4800
8720e9e7
TH
4801 if (cb->vcdl != NULL) {
4802 char *path;
4803 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
4804 &path) == 0) {
d6418de0 4805 printf(" ");
8720e9e7 4806 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
8720e9e7
TH
4807 }
4808 }
4809
7e945072 4810 if (!(cb->cb_flags & IOS_ANYHISTO_M))
193a37cb 4811 printf("\n");
34dc7c2f 4812
193a37cb 4813 ret++;
34dc7c2f 4814
193a37cb 4815children:
92bc9278 4816
4817 free(calcvs);
4818
34dc7c2f 4819 if (!cb->cb_verbose)
193a37cb 4820 return (ret);
34dc7c2f
BB
4821
4822 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
4823 &newchild, &children) != 0)
193a37cb 4824 return (ret);
34dc7c2f 4825
674b8934
TH
4826 if (oldnv) {
4827 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
4828 &oldchild, &oldchildren) != 0)
4829 return (ret);
4830
4831 children = MIN(oldchildren, children);
4832 }
34dc7c2f 4833
cc99f275
DB
4834 /*
4835 * print normal top-level devices
4836 */
34dc7c2f 4837 for (c = 0; c < children; c++) {
187632dc 4838 uint64_t ishole = B_FALSE, islog = B_FALSE;
428870ff 4839
187632dc
MH
4840 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
4841 &ishole);
4842
4843 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
4844 &islog);
4845
4846 if (ishole || islog)
428870ff
BB
4847 continue;
4848
cc99f275
DB
4849 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
4850 continue;
4851
d2f3e292 4852 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2a673e76 4853 cb->cb_vdevs.cb_name_flags);
193a37cb 4854 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
34dc7c2f
BB
4855 newchild[c], cb, depth + 2);
4856 free(vname);
4857 }
4858
187632dc 4859 /*
cc99f275 4860 * print all other top-level devices
187632dc 4861 */
cc99f275
DB
4862 for (uint_t n = 0; n < 3; n++) {
4863 boolean_t printed = B_FALSE;
187632dc
MH
4864
4865 for (c = 0; c < children; c++) {
4866 uint64_t islog = B_FALSE;
cc99f275
DB
4867 char *bias = NULL;
4868 char *type = NULL;
4869
187632dc
MH
4870 (void) nvlist_lookup_uint64(newchild[c],
4871 ZPOOL_CONFIG_IS_LOG, &islog);
187632dc 4872 if (islog) {
cc99f275
DB
4873 bias = VDEV_ALLOC_CLASS_LOGS;
4874 } else {
4875 (void) nvlist_lookup_string(newchild[c],
4876 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
4877 (void) nvlist_lookup_string(newchild[c],
4878 ZPOOL_CONFIG_TYPE, &type);
187632dc 4879 }
cc99f275
DB
4880 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
4881 continue;
4882 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
4883 continue;
187632dc 4884
cc99f275
DB
4885 if (!printed) {
4886 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
2a673e76
AJ
4887 !cb->cb_scripted &&
4888 !cb->cb_vdevs.cb_names) {
cc99f275
DB
4889 print_iostat_dashes(cb, 0,
4890 class_name[n]);
4891 }
4892 printf("\n");
4893 printed = B_TRUE;
4894 }
4895
4896 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2a673e76 4897 cb->cb_vdevs.cb_name_flags);
cc99f275
DB
4898 ret += print_vdev_stats(zhp, vname, oldnv ?
4899 oldchild[c] : NULL, newchild[c], cb, depth + 2);
4900 free(vname);
4901 }
187632dc
MH
4902 }
4903
34dc7c2f
BB
4904 /*
4905 * Include level 2 ARC devices in iostat output
4906 */
4907 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
4908 &newchild, &children) != 0)
193a37cb 4909 return (ret);
34dc7c2f 4910
674b8934
TH
4911 if (oldnv) {
4912 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
4913 &oldchild, &oldchildren) != 0)
4914 return (ret);
4915
4916 children = MIN(oldchildren, children);
4917 }
34dc7c2f
BB
4918
4919 if (children > 0) {
7e945072 4920 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
2a673e76 4921 !cb->cb_vdevs.cb_names) {
193a37cb
TH
4922 print_iostat_dashes(cb, 0, "cache");
4923 }
5034557b 4924 printf("\n");
193a37cb 4925
34dc7c2f 4926 for (c = 0; c < children; c++) {
428870ff 4927 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2a673e76 4928 cb->cb_vdevs.cb_name_flags);
193a37cb
TH
4929 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
4930 : NULL, newchild[c], cb, depth + 2);
34dc7c2f
BB
4931 free(vname);
4932 }
4933 }
193a37cb
TH
4934
4935 return (ret);
34dc7c2f
BB
4936}
4937
4938static int
4939refresh_iostat(zpool_handle_t *zhp, void *data)
4940{
4941 iostat_cbdata_t *cb = data;
4942 boolean_t missing;
4943
4944 /*
4945 * If the pool has disappeared, remove it from the list and continue.
4946 */
4947 if (zpool_refresh_stats(zhp, &missing) != 0)
4948 return (-1);
4949
4950 if (missing)
4951 pool_list_remove(cb->cb_list, zhp);
4952
4953 return (0);
4954}
4955
4956/*
4957 * Callback to print out the iostats for the given pool.
4958 */
65c7cc49 4959static int
34dc7c2f
BB
4960print_iostat(zpool_handle_t *zhp, void *data)
4961{
4962 iostat_cbdata_t *cb = data;
4963 nvlist_t *oldconfig, *newconfig;
4964 nvlist_t *oldnvroot, *newnvroot;
193a37cb 4965 int ret;
34dc7c2f
BB
4966
4967 newconfig = zpool_get_config(zhp, &oldconfig);
4968
4969 if (cb->cb_iteration == 1)
4970 oldconfig = NULL;
4971
4972 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
4973 &newnvroot) == 0);
4974
4975 if (oldconfig == NULL)
4976 oldnvroot = NULL;
4977 else
4978 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
4979 &oldnvroot) == 0);
4980
193a37cb 4981 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
02730c33 4982 cb, 0);
7e945072 4983 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
2a673e76
AJ
4984 !cb->cb_scripted && cb->cb_verbose &&
4985 !cb->cb_vdevs.cb_names_count) {
7e945072 4986 print_iostat_separator(cb);
d6418de0
TH
4987 if (cb->vcdl != NULL) {
4988 print_cmd_columns(cb->vcdl, 1);
4989 }
4990 printf("\n");
7e945072 4991 }
34dc7c2f 4992
193a37cb 4993 return (ret);
34dc7c2f
BB
4994}
4995
9fc60702
CS
4996static int
4997get_columns(void)
4998{
4999 struct winsize ws;
5000 int columns = 80;
5001 int error;
5002
5003 if (isatty(STDOUT_FILENO)) {
5004 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
5005 if (error == 0)
5006 columns = ws.ws_col;
5007 } else {
5008 columns = 999;
5009 }
5010
d1d7e268 5011 return (columns);
9fc60702
CS
5012}
5013
c5eea0ab
BB
5014/*
5015 * Return the required length of the pool/vdev name column. The minimum
5016 * allowed width and output formatting flags must be provided.
5017 */
5018static int
5019get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
34dc7c2f 5020{
34dc7c2f 5021 nvlist_t *config, *nvroot;
c5eea0ab 5022 int width = min_width;
34dc7c2f
BB
5023
5024 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
5025 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5026 &nvroot) == 0);
193a37cb 5027 unsigned int poolname_len = strlen(zpool_get_name(zhp));
c5eea0ab
BB
5028 if (verbose == B_FALSE) {
5029 width = MAX(poolname_len, min_width);
5030 } else {
5031 width = MAX(poolname_len,
5032 max_width(zhp, nvroot, 0, min_width, flags));
5033 }
34dc7c2f 5034 }
9fc60702 5035
c5eea0ab 5036 return (width);
34dc7c2f
BB
5037}
5038
5039/*
428870ff 5040 * Parse the input string, get the 'interval' and 'count' value if there is one.
34dc7c2f 5041 */
428870ff 5042static void
193a37cb 5043get_interval_count(int *argcp, char **argv, float *iv,
428870ff 5044 unsigned long *cnt)
34dc7c2f 5045{
193a37cb
TH
5046 float interval = 0;
5047 unsigned long count = 0;
1fde1e37 5048 int argc = *argcp;
34dc7c2f
BB
5049
5050 /*
5051 * Determine if the last argument is an integer or a pool name
5052 */
3283f137 5053 if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
34dc7c2f
BB
5054 char *end;
5055
5056 errno = 0;
193a37cb 5057 interval = strtof(argv[argc - 1], &end);
34dc7c2f
BB
5058
5059 if (*end == '\0' && errno == 0) {
5060 if (interval == 0) {
61b50107
AZ
5061 (void) fprintf(stderr, gettext(
5062 "interval cannot be zero\n"));
34dc7c2f
BB
5063 usage(B_FALSE);
5064 }
34dc7c2f
BB
5065 /*
5066 * Ignore the last parameter
5067 */
5068 argc--;
5069 } else {
5070 /*
5071 * If this is not a valid number, just plow on. The
5072 * user will get a more informative error message later
5073 * on.
5074 */
5075 interval = 0;
5076 }
5077 }
5078
5079 /*
5080 * If the last argument is also an integer, then we have both a count
428870ff 5081 * and an interval.
34dc7c2f 5082 */
3283f137 5083 if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
34dc7c2f
BB
5084 char *end;
5085
5086 errno = 0;
5087 count = interval;
193a37cb 5088 interval = strtof(argv[argc - 1], &end);
34dc7c2f
BB
5089
5090 if (*end == '\0' && errno == 0) {
5091 if (interval == 0) {
61b50107
AZ
5092 (void) fprintf(stderr, gettext(
5093 "interval cannot be zero\n"));
34dc7c2f
BB
5094 usage(B_FALSE);
5095 }
5096
5097 /*
5098 * Ignore the last parameter
5099 */
5100 argc--;
5101 } else {
5102 interval = 0;
5103 }
5104 }
5105
428870ff
BB
5106 *iv = interval;
5107 *cnt = count;
5108 *argcp = argc;
5109}
5110
5111static void
5112get_timestamp_arg(char c)
5113{
5114 if (c == 'u')
5115 timestamp_fmt = UDATE;
5116 else if (c == 'd')
5117 timestamp_fmt = DDATE;
5118 else
5119 usage(B_FALSE);
5120}
5121
5122/*
193a37cb
TH
5123 * Return stat flags that are supported by all pools by both the module and
5124 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5125 * It will get ANDed down until only the flags that are supported on all pools
5126 * remain.
5127 */
5128static int
5129get_stat_flags_cb(zpool_handle_t *zhp, void *data)
5130{
5131 uint64_t *mask = data;
5132 nvlist_t *config, *nvroot, *nvx;
5133 uint64_t flags = 0;
5134 int i, j;
5135
193a37cb
TH
5136 config = zpool_get_config(zhp, NULL);
5137 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5138 &nvroot) == 0);
5139
5140 /* Default stats are always supported, but for completeness.. */
5141 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
5142 flags |= IOS_DEFAULT_M;
5143
5144 /* Get our extended stats nvlist from the main list */
5145 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
5146 &nvx) != 0) {
5147 /*
5148 * No extended stats; they're probably running an older
5149 * module. No big deal, we support that too.
5150 */
5151 goto end;
5152 }
5153
5154 /* For each extended stat, make sure all its nvpairs are supported */
5155 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
5156 if (!vsx_type_to_nvlist[j][0])
5157 continue;
5158
5159 /* Start off by assuming the flag is supported, then check */
5160 flags |= (1ULL << j);
5161 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
5162 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
5163 /* flag isn't supported */
5164 flags = flags & ~(1ULL << j);
5165 break;
5166 }
5167 }
5168 }
5169end:
5170 *mask = *mask & flags;
5171 return (0);
5172}
5173
5174/*
5175 * Return a bitmask of stats that are supported on all pools by both the module
5176 * and zpool iostat.
5177 */
5178static uint64_t
5179get_stat_flags(zpool_list_t *list)
5180{
5181 uint64_t mask = -1;
5182
5183 /*
5184 * get_stat_flags_cb() will lop off bits from "mask" until only the
5185 * flags that are supported on all pools remain.
5186 */
5187 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
5188 return (mask);
5189}
5190
5191/*
2a673e76 5192 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
193a37cb
TH
5193 */
5194static int
2a8430a2 5195is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data)
193a37cb 5196{
2a673e76 5197 vdev_cbdata_t *cb = cb_data;
92bc9278 5198 char *name = NULL;
2a673e76 5199 int ret = 1; /* assume match */
2a8430a2 5200 zpool_handle_t *zhp = zhp_data;
193a37cb
TH
5201
5202 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
5203
2a673e76
AJ
5204 if (strcmp(name, cb->cb_names[0])) {
5205 free(name);
5206 name = zpool_vdev_name(g_zfs, zhp, nv, VDEV_NAME_GUID);
5207 ret = (strcmp(name, cb->cb_names[0]) == 0);
5208 }
92bc9278 5209 free(name);
193a37cb 5210
92bc9278 5211 return (ret);
193a37cb
TH
5212}
5213
5214/*
2a673e76 5215 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
193a37cb
TH
5216 */
5217static int
5218is_vdev(zpool_handle_t *zhp, void *cb_data)
5219{
5220 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
5221}
5222
5223/*
5224 * Check if vdevs are in a pool
5225 *
5226 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5227 * return 0. If pool_name is NULL, then search all pools.
5228 */
5229static int
5230are_vdevs_in_pool(int argc, char **argv, char *pool_name,
2a673e76 5231 vdev_cbdata_t *cb)
193a37cb
TH
5232{
5233 char **tmp_name;
5234 int ret = 0;
5235 int i;
5236 int pool_count = 0;
5237
5238 if ((argc == 0) || !*argv)
5239 return (0);
5240
5241 if (pool_name)
5242 pool_count = 1;
5243
2a673e76
AJ
5244 /* Temporarily hijack cb_names for a second... */
5245 tmp_name = cb->cb_names;
193a37cb
TH
5246
5247 /* Go though our list of prospective vdev names */
5248 for (i = 0; i < argc; i++) {
2a673e76 5249 cb->cb_names = argv + i;
193a37cb
TH
5250
5251 /* Is this name a vdev in our pools? */
5252 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
2a673e76 5253 ZFS_TYPE_POOL, B_FALSE, is_vdev, cb);
193a37cb
TH
5254 if (!ret) {
5255 /* No match */
5256 break;
5257 }
5258 }
5259
2a673e76 5260 cb->cb_names = tmp_name;
193a37cb
TH
5261
5262 return (ret);
5263}
5264
5265static int
5266is_pool_cb(zpool_handle_t *zhp, void *data)
5267{
5268 char *name = data;
5269 if (strcmp(name, zpool_get_name(zhp)) == 0)
5270 return (1);
5271
5272 return (0);
5273}
5274
5275/*
5276 * Do we have a pool named *name? If so, return 1, otherwise 0.
5277 */
5278static int
5279is_pool(char *name)
5280{
2a673e76
AJ
5281 return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE,
5282 is_pool_cb, name));
193a37cb
TH
5283}
5284
5285/* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5286static int
4ea3f864
GM
5287are_all_pools(int argc, char **argv)
5288{
193a37cb
TH
5289 if ((argc == 0) || !*argv)
5290 return (0);
5291
5292 while (--argc >= 0)
5293 if (!is_pool(argv[argc]))
5294 return (0);
5295
5296 return (1);
5297}
5298
5299/*
5300 * Helper function to print out vdev/pool names we can't resolve. Used for an
5301 * error message.
5302 */
5303static void
5304error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
2a673e76 5305 vdev_cbdata_t *cb)
193a37cb
TH
5306{
5307 int i;
5308 char *name;
5309 char *str;
5310 for (i = 0; i < argc; i++) {
5311 name = argv[i];
5312
5313 if (is_pool(name))
5314 str = gettext("pool");
5315 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
5316 str = gettext("vdev in this pool");
5317 else if (are_vdevs_in_pool(1, &name, NULL, cb))
5318 str = gettext("vdev in another pool");
5319 else
5320 str = gettext("unknown");
5321
5322 fprintf(stderr, "\t%s (%s)\n", name, str);
5323 }
5324}
5325
5326/*
5327 * Same as get_interval_count(), but with additional checks to not misinterpret
5328 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
2a673e76 5329 * cb.cb_vdevs.cb_name_flags.
193a37cb
TH
5330 */
5331static void
5332get_interval_count_filter_guids(int *argc, char **argv, float *interval,
5333 unsigned long *count, iostat_cbdata_t *cb)
5334{
5335 char **tmpargv = argv;
5336 int argc_for_interval = 0;
5337
5338 /* Is the last arg an interval value? Or a guid? */
2a673e76
AJ
5339 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL,
5340 &cb->cb_vdevs)) {
193a37cb
TH
5341 /*
5342 * The last arg is not a guid, so it's probably an
5343 * interval value.
5344 */
5345 argc_for_interval++;
5346
5347 if (*argc >= 2 &&
2a673e76
AJ
5348 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL,
5349 &cb->cb_vdevs)) {
193a37cb
TH
5350 /*
5351 * The 2nd to last arg is not a guid, so it's probably
5352 * an interval value.
5353 */
5354 argc_for_interval++;
5355 }
5356 }
5357
5358 /* Point to our list of possible intervals */
5359 tmpargv = &argv[*argc - argc_for_interval];
5360
5361 *argc = *argc - argc_for_interval;
5362 get_interval_count(&argc_for_interval, tmpargv,
5363 interval, count);
5364}
5365
5366/*
5367 * Floating point sleep(). Allows you to pass in a floating point value for
5368 * seconds.
5369 */
5370static void
4ea3f864
GM
5371fsleep(float sec)
5372{
193a37cb
TH
5373 struct timespec req;
5374 req.tv_sec = floor(sec);
5375 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
5376 nanosleep(&req, NULL);
5377}
5378
e60e158e
JG
5379/*
5380 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5381 * if we were unable to determine its size.
5382 */
5383static int
5384terminal_height(void)
5385{
5386 struct winsize win;
5387
5388 if (isatty(STDOUT_FILENO) == 0)
5389 return (-1);
5390
5391 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0)
5392 return (win.ws_row);
5393
5394 return (-1);
5395}
5396
d6418de0
TH
5397/*
5398 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5399 * print the result.
5400 *
5401 * name: Short name of the script ('iostat').
5402 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5403 */
5404static void
5405print_zpool_script_help(char *name, char *path)
5406{
5407 char *argv[] = {path, "-h", NULL};
5408 char **lines = NULL;
5409 int lines_cnt = 0;
5410 int rc;
5411
5412 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
5413 &lines_cnt);
3e2ecae0
GDN
5414 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
5415 if (lines != NULL)
5416 libzfs_free_str_array(lines, lines_cnt);
d6418de0 5417 return;
3e2ecae0 5418 }
d6418de0
TH
5419
5420 for (int i = 0; i < lines_cnt; i++)
5421 if (!is_blank_str(lines[i]))
5422 printf(" %-14s %s\n", name, lines[i]);
5423
5424 libzfs_free_str_array(lines, lines_cnt);
5425}
5426
193a37cb 5427/*
099700d9 5428 * Go though the zpool status/iostat -c scripts in the user's path, run their
d6418de0
TH
5429 * help option (-h), and print out the results.
5430 */
5431static void
099700d9 5432print_zpool_dir_scripts(char *dirpath)
d6418de0
TH
5433{
5434 DIR *dir;
5435 struct dirent *ent;
5436 char fullpath[MAXPATHLEN];
5437 struct stat dir_stat;
5438
099700d9 5439 if ((dir = opendir(dirpath)) != NULL) {
d6418de0
TH
5440 /* print all the files and directories within directory */
5441 while ((ent = readdir(dir)) != NULL) {
099700d9 5442 sprintf(fullpath, "%s/%s", dirpath, ent->d_name);
d6418de0
TH
5443
5444 /* Print the scripts */
5445 if (stat(fullpath, &dir_stat) == 0)
5446 if (dir_stat.st_mode & S_IXUSR &&
5447 S_ISREG(dir_stat.st_mode))
5448 print_zpool_script_help(ent->d_name,
5449 fullpath);
5450 }
d6418de0 5451 closedir(dir);
d6418de0
TH
5452 }
5453}
5454
099700d9
GDN
5455/*
5456 * Print out help text for all zpool status/iostat -c scripts.
5457 */
5458static void
5459print_zpool_script_list(char *subcommand)
5460{
1ce6d70c 5461 char *dir, *sp, *tmp;
099700d9
GDN
5462
5463 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
5464
5465 sp = zpool_get_cmd_search_path();
5466 if (sp == NULL)
5467 return;
5468
1ce6d70c
AZ
5469 for (dir = strtok_r(sp, ":", &tmp);
5470 dir != NULL;
5471 dir = strtok_r(NULL, ":", &tmp))
099700d9 5472 print_zpool_dir_scripts(dir);
099700d9
GDN
5473
5474 free(sp);
5475}
5476
c5eea0ab
BB
5477/*
5478 * Set the minimum pool/vdev name column width. The width must be at least 10,
5479 * but may be as large as the column width - 42 so it still fits on one line.
a36bad17 5480 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
c5eea0ab
BB
5481 */
5482static int
5483get_namewidth_iostat(zpool_handle_t *zhp, void *data)
5484{
5485 iostat_cbdata_t *cb = data;
a36bad17 5486 int width, available_width;
c5eea0ab 5487
a36bad17
PP
5488 /*
5489 * get_namewidth() returns the maximum width of any name in that column
5490 * for any pool/vdev/device line that will be output.
5491 */
2a673e76 5492 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_vdevs.cb_name_flags,
c5eea0ab 5493 cb->cb_verbose);
c5eea0ab 5494
a36bad17
PP
5495 /*
5496 * The width we are calculating is the width of the header and also the
5497 * padding width for names that are less than maximum width. The stats
5498 * take up 42 characters, so the width available for names is:
5499 */
5500 available_width = get_columns() - 42;
5501
5502 /*
5503 * If the maximum width fits on a screen, then great! Make everything
5504 * line up by justifying all lines to the same width. If that max
5505 * width is larger than what's available, the name plus stats won't fit
5506 * on one line, and justifying to that width would cause every line to
5507 * wrap on the screen. We only want lines with long names to wrap.
5508 * Limit the padding to what won't wrap.
5509 */
5510 if (width > available_width)
5511 width = available_width;
5512
5513 /*
5514 * And regardless of whatever the screen width is (get_columns can
5515 * return 0 if the width is not known or less than 42 for a narrow
5516 * terminal) have the width be a minimum of 10.
5517 */
c5eea0ab
BB
5518 if (width < 10)
5519 width = 10;
c5eea0ab 5520
a36bad17 5521 /* Save the calculated width */
c5eea0ab
BB
5522 cb->cb_namewidth = width;
5523
5524 return (0);
5525}
5526
d6418de0
TH
5527/*
5528 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5529 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5530 * [interval [count]]
428870ff 5531 *
8720e9e7 5532 * -c CMD For each vdev, run command CMD
d2f3e292
RY
5533 * -g Display guid for individual vdev name.
5534 * -L Follow links when resolving vdev path name.
a77f29f9 5535 * -P Display full path for vdev name.
428870ff 5536 * -v Display statistics for individual vdevs
193a37cb
TH
5537 * -h Display help
5538 * -p Display values in parsable (exact) format.
5539 * -H Scripted mode. Don't display headers, and separate properties
5540 * by a single tab.
5541 * -l Display average latency
5542 * -q Display queue depths
7e945072
TH
5543 * -w Display latency histograms
5544 * -r Display request size histogram
428870ff 5545 * -T Display a timestamp in date(1) or Unix format
8fccfa8e 5546 * -n Only print headers once
428870ff
BB
5547 *
5548 * This command can be tricky because we want to be able to deal with pool
5549 * creation/destruction as well as vdev configuration changes. The bulk of this
5550 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5551 * on pool_list_update() to detect the addition of new pools. Configuration
5552 * changes are all handled within libzfs.
5553 */
5554int
5555zpool_do_iostat(int argc, char **argv)
5556{
5557 int c;
5558 int ret;
5559 int npools;
193a37cb
TH
5560 float interval = 0;
5561 unsigned long count = 0;
8fccfa8e 5562 int winheight = 24;
428870ff
BB
5563 zpool_list_t *list;
5564 boolean_t verbose = B_FALSE;
7e945072 5565 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
3491d6eb 5566 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
41092124 5567 boolean_t omit_since_boot = B_FALSE;
d2f3e292
RY
5568 boolean_t guid = B_FALSE;
5569 boolean_t follow_links = B_FALSE;
5570 boolean_t full_name = B_FALSE;
8fccfa8e 5571 boolean_t headers_once = B_FALSE;
d2f3e292 5572 iostat_cbdata_t cb = { 0 };
8720e9e7 5573 char *cmd = NULL;
428870ff 5574
193a37cb
TH
5575 /* Used for printing error message */
5576 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
7e945072 5577 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
193a37cb
TH
5578
5579 uint64_t unsupported_flags;
5580
428870ff 5581 /* check options */
8fccfa8e 5582 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) {
428870ff 5583 switch (c) {
8720e9e7 5584 case 'c':
d6418de0
TH
5585 if (cmd != NULL) {
5586 fprintf(stderr,
5587 gettext("Can't set -c flag twice\n"));
5588 exit(1);
5589 }
099700d9
GDN
5590
5591 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
5592 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5593 fprintf(stderr, gettext(
5594 "Can't run -c, disabled by "
5595 "ZPOOL_SCRIPTS_ENABLED.\n"));
5596 exit(1);
5597 }
5598
d6418de0
TH
5599 if ((getuid() <= 0 || geteuid() <= 0) &&
5600 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5601 fprintf(stderr, gettext(
5602 "Can't run -c with root privileges "
5603 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5604 exit(1);
5605 }
8720e9e7 5606 cmd = optarg;
d6418de0 5607 verbose = B_TRUE;
8720e9e7 5608 break;
d2f3e292
RY
5609 case 'g':
5610 guid = B_TRUE;
5611 break;
5612 case 'L':
5613 follow_links = B_TRUE;
5614 break;
a77f29f9 5615 case 'P':
d2f3e292
RY
5616 full_name = B_TRUE;
5617 break;
428870ff
BB
5618 case 'T':
5619 get_timestamp_arg(*optarg);
5620 break;
5621 case 'v':
5622 verbose = B_TRUE;
5623 break;
193a37cb 5624 case 'p':
3491d6eb 5625 parsable = B_TRUE;
193a37cb
TH
5626 break;
5627 case 'l':
5628 latency = B_TRUE;
5629 break;
5630 case 'q':
5631 queues = B_TRUE;
5632 break;
5633 case 'H':
5634 scripted = B_TRUE;
5635 break;
5636 case 'w':
7e945072
TH
5637 l_histo = B_TRUE;
5638 break;
5639 case 'r':
5640 rq_histo = B_TRUE;
193a37cb 5641 break;
41092124
HM
5642 case 'y':
5643 omit_since_boot = B_TRUE;
5644 break;
8fccfa8e
DW
5645 case 'n':
5646 headers_once = B_TRUE;
5647 break;
193a37cb
TH
5648 case 'h':
5649 usage(B_FALSE);
5650 break;
428870ff 5651 case '?':
1528bfdb 5652 if (optopt == 'c') {
099700d9 5653 print_zpool_script_list("iostat");
d6418de0 5654 exit(0);
1528bfdb
TH
5655 } else {
5656 fprintf(stderr,
5657 gettext("invalid option '%c'\n"), optopt);
5658 }
428870ff
BB
5659 usage(B_FALSE);
5660 }
5661 }
5662
5663 argc -= optind;
5664 argv += optind;
5665
3491d6eb 5666 cb.cb_literal = parsable;
193a37cb
TH
5667 cb.cb_scripted = scripted;
5668
5669 if (guid)
2a673e76 5670 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID;
193a37cb 5671 if (follow_links)
2a673e76 5672 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
193a37cb 5673 if (full_name)
2a673e76 5674 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH;
193a37cb
TH
5675 cb.cb_iteration = 0;
5676 cb.cb_namewidth = 0;
5677 cb.cb_verbose = verbose;
5678
5679 /* Get our interval and count values (if any) */
5680 if (guid) {
5681 get_interval_count_filter_guids(&argc, argv, &interval,
5682 &count, &cb);
5683 } else {
5684 get_interval_count(&argc, argv, &interval, &count);
5685 }
5686
5687 if (argc == 0) {
5688 /* No args, so just print the defaults. */
5689 } else if (are_all_pools(argc, argv)) {
5690 /* All the args are pool names */
2a673e76 5691 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
193a37cb 5692 /* All the args are vdevs */
2a673e76
AJ
5693 cb.cb_vdevs.cb_names = argv;
5694 cb.cb_vdevs.cb_names_count = argc;
193a37cb
TH
5695 argc = 0; /* No pools to process */
5696 } else if (are_all_pools(1, argv)) {
5697 /* The first arg is a pool name */
2a673e76
AJ
5698 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
5699 &cb.cb_vdevs)) {
193a37cb 5700 /* ...and the rest are vdev names */
2a673e76
AJ
5701 cb.cb_vdevs.cb_names = argv + 1;
5702 cb.cb_vdevs.cb_names_count = argc - 1;
193a37cb
TH
5703 argc = 1; /* One pool to process */
5704 } else {
5705 fprintf(stderr, gettext("Expected either a list of "));
5706 fprintf(stderr, gettext("pools, or list of vdevs in"));
5707 fprintf(stderr, " \"%s\", ", argv[0]);
5708 fprintf(stderr, gettext("but got:\n"));
5709 error_list_unresolved_vdevs(argc - 1, argv + 1,
2a673e76 5710 argv[0], &cb.cb_vdevs);
193a37cb
TH
5711 fprintf(stderr, "\n");
5712 usage(B_FALSE);
5713 return (1);
5714 }
5715 } else {
5716 /*
5717 * The args don't make sense. The first arg isn't a pool name,
5718 * nor are all the args vdevs.
5719 */
5720 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
5721 fprintf(stderr, "\n");
5722 return (1);
5723 }
5724
2a673e76 5725 if (cb.cb_vdevs.cb_names_count != 0) {
193a37cb
TH
5726 /*
5727 * If user specified vdevs, it implies verbose.
5728 */
5729 cb.cb_verbose = B_TRUE;
5730 }
428870ff 5731
34dc7c2f
BB
5732 /*
5733 * Construct the list of all interesting pools.
5734 */
5735 ret = 0;
2a673e76
AJ
5736 if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable,
5737 &ret)) == NULL)
34dc7c2f
BB
5738 return (1);
5739
5740 if (pool_list_count(list) == 0 && argc != 0) {
5741 pool_list_free(list);
5742 return (1);
5743 }
5744
5745 if (pool_list_count(list) == 0 && interval == 0) {
5746 pool_list_free(list);
5747 (void) fprintf(stderr, gettext("no pools available\n"));
5748 return (1);
5749 }
5750
d6418de0 5751 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
7e945072
TH
5752 pool_list_free(list);
5753 (void) fprintf(stderr,
d6418de0 5754 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
7e945072
TH
5755 usage(B_FALSE);
5756 return (1);
5757 }
5758
5759 if (l_histo && rq_histo) {
193a37cb
TH
5760 pool_list_free(list);
5761 (void) fprintf(stderr,
7e945072 5762 gettext("Only one of [-r|-w] can be passed at a time\n"));
193a37cb
TH
5763 usage(B_FALSE);
5764 return (1);
5765 }
5766
34dc7c2f
BB
5767 /*
5768 * Enter the main iostat loop.
5769 */
5770 cb.cb_list = list;
193a37cb 5771
7e945072 5772 if (l_histo) {
193a37cb
TH
5773 /*
5774 * Histograms tables look out of place when you try to display
5775 * them with the other stats, so make a rule that you can only
5776 * print histograms by themselves.
5777 */
5778 cb.cb_flags = IOS_L_HISTO_M;
7e945072
TH
5779 } else if (rq_histo) {
5780 cb.cb_flags = IOS_RQ_HISTO_M;
193a37cb
TH
5781 } else {
5782 cb.cb_flags = IOS_DEFAULT_M;
5783 if (latency)
5784 cb.cb_flags |= IOS_LATENCY_M;
5785 if (queues)
5786 cb.cb_flags |= IOS_QUEUES_M;
5787 }
5788
5789 /*
5790 * See if the module supports all the stats we want to display.
5791 */
5792 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
5793 if (unsupported_flags) {
5794 uint64_t f;
5795 int idx;
5796 fprintf(stderr,
5797 gettext("The loaded zfs module doesn't support:"));
5798
5799 /* for each bit set in unsupported_flags */
5800 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
5801 idx = lowbit64(f) - 1;
5802 fprintf(stderr, " -%c", flag_to_arg[idx]);
5803 }
5804
02730c33 5805 fprintf(stderr, ". Try running a newer module.\n");
193a37cb
TH
5806 pool_list_free(list);
5807
5808 return (1);
5809 }
5810
34dc7c2f 5811 for (;;) {
34dc7c2f 5812 if ((npools = pool_list_count(list)) == 0)
42cb3819 5813 (void) fprintf(stderr, gettext("no pools available\n"));
5a521059 5814 else {
41092124
HM
5815 /*
5816 * If this is the first iteration and -y was supplied
5817 * we skip any printing.
5818 */
5819 boolean_t skip = (omit_since_boot &&
193a37cb 5820 cb.cb_iteration == 0);
41092124 5821
5a521059
PJ
5822 /*
5823 * Refresh all statistics. This is done as an
5824 * explicit step before calculating the maximum name
5825 * width, so that any * configuration changes are
5826 * properly accounted for.
5827 */
5828 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
193a37cb 5829 &cb);
34dc7c2f 5830
5a521059
PJ
5831 /*
5832 * Iterate over all pools to determine the maximum width
5833 * for the pool / device name column across all pools.
5834 */
5835 cb.cb_namewidth = 0;
c5eea0ab
BB
5836 (void) pool_list_iter(list, B_FALSE,
5837 get_namewidth_iostat, &cb);
34dc7c2f 5838
5a521059
PJ
5839 if (timestamp_fmt != NODATE)
5840 print_timestamp(timestamp_fmt);
428870ff 5841
d6418de0
TH
5842 if (cmd != NULL && cb.cb_verbose &&
5843 !(cb.cb_flags & IOS_ANYHISTO_M)) {
5844 cb.vcdl = all_pools_for_each_vdev_run(argc,
2a673e76
AJ
5845 argv, cmd, g_zfs, cb.cb_vdevs.cb_names,
5846 cb.cb_vdevs.cb_names_count,
5847 cb.cb_vdevs.cb_name_flags);
d6418de0
TH
5848 } else {
5849 cb.vcdl = NULL;
5850 }
5851
8fccfa8e
DW
5852
5853 /*
5854 * Check terminal size so we can print headers
5855 * even when terminal window has its height
5856 * changed.
5857 */
e60e158e
JG
5858 winheight = terminal_height();
5859 /*
5860 * Are we connected to TTY? If not, headers_once
5861 * should be true, to avoid breaking scripts.
5862 */
5863 if (winheight < 0)
5864 headers_once = B_TRUE;
8fccfa8e 5865
5a521059 5866 /*
41092124
HM
5867 * If it's the first time and we're not skipping it,
5868 * or either skip or verbose mode, print the header.
193a37cb
TH
5869 *
5870 * The histogram code explicitly prints its header on
5871 * every vdev, so skip this for histograms.
5a521059 5872 */
193a37cb 5873 if (((++cb.cb_iteration == 1 && !skip) ||
8fccfa8e
DW
5874 (skip != verbose) ||
5875 (!headers_once &&
5876 (cb.cb_iteration % winheight) == 0)) &&
7e945072 5877 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
193a37cb 5878 !cb.cb_scripted)
5a521059 5879 print_iostat_header(&cb);
34dc7c2f 5880
41092124 5881 if (skip) {
193a37cb 5882 (void) fsleep(interval);
41092124
HM
5883 continue;
5884 }
5885
193a37cb 5886 pool_list_iter(list, B_FALSE, print_iostat, &cb);
34dc7c2f 5887
5a521059
PJ
5888 /*
5889 * If there's more than one pool, and we're not in
5890 * verbose mode (which prints a separator for us),
5891 * then print a separator.
193a37cb
TH
5892 *
5893 * In addition, if we're printing specific vdevs then
5894 * we also want an ending separator.
5a521059 5895 */
193a37cb 5896 if (((npools > 1 && !verbose &&
7e945072
TH
5897 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
5898 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
2a673e76 5899 cb.cb_vdevs.cb_names_count)) &&
193a37cb 5900 !cb.cb_scripted) {
5a521059 5901 print_iostat_separator(&cb);
d6418de0
TH
5902 if (cb.vcdl != NULL)
5903 print_cmd_columns(cb.vcdl, 1);
5904 printf("\n");
193a37cb 5905 }
d6418de0
TH
5906
5907 if (cb.vcdl != NULL)
5908 free_vdev_cmd_data_list(cb.vcdl);
5909
5a521059 5910 }
34dc7c2f
BB
5911
5912 /*
5913 * Flush the output so that redirection to a file isn't buffered
5914 * indefinitely.
5915 */
5916 (void) fflush(stdout);
5917
5918 if (interval == 0)
5919 break;
5920
5921 if (count != 0 && --count == 0)
5922 break;
5923
193a37cb 5924 (void) fsleep(interval);
34dc7c2f
BB
5925 }
5926
5927 pool_list_free(list);
5928
5929 return (ret);
5930}
5931
5932typedef struct list_cbdata {
1bd201e7 5933 boolean_t cb_verbose;
d2f3e292 5934 int cb_name_flags;
1bd201e7 5935 int cb_namewidth;
34dc7c2f 5936 boolean_t cb_scripted;
34dc7c2f 5937 zprop_list_t *cb_proplist;
2a8b84b7 5938 boolean_t cb_literal;
34dc7c2f
BB
5939} list_cbdata_t;
5940
cc99f275 5941
34dc7c2f
BB
5942/*
5943 * Given a list of columns to display, output appropriate headers for each one.
5944 */
5945static void
1bd201e7 5946print_header(list_cbdata_t *cb)
34dc7c2f 5947{
1bd201e7 5948 zprop_list_t *pl = cb->cb_proplist;
9ae529ec 5949 char headerbuf[ZPOOL_MAXPROPLEN];
34dc7c2f
BB
5950 const char *header;
5951 boolean_t first = B_TRUE;
5952 boolean_t right_justify;
1bd201e7 5953 size_t width = 0;
34dc7c2f
BB
5954
5955 for (; pl != NULL; pl = pl->pl_next) {
1bd201e7
CS
5956 width = pl->pl_width;
5957 if (first && cb->cb_verbose) {
5958 /*
5959 * Reset the width to accommodate the verbose listing
5960 * of devices.
5961 */
5962 width = cb->cb_namewidth;
5963 }
5964
34dc7c2f
BB
5965 if (!first)
5966 (void) printf(" ");
5967 else
5968 first = B_FALSE;
5969
9ae529ec
CS
5970 right_justify = B_FALSE;
5971 if (pl->pl_prop != ZPROP_INVAL) {
5972 header = zpool_prop_column_name(pl->pl_prop);
5973 right_justify = zpool_prop_align_right(pl->pl_prop);
5974 } else {
5975 int i;
5976
5977 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5978 headerbuf[i] = toupper(pl->pl_user_prop[i]);
5979 headerbuf[i] = '\0';
5980 header = headerbuf;
5981 }
34dc7c2f
BB
5982
5983 if (pl->pl_next == NULL && !right_justify)
5984 (void) printf("%s", header);
5985 else if (right_justify)
1bd201e7 5986 (void) printf("%*s", (int)width, header);
34dc7c2f 5987 else
1bd201e7 5988 (void) printf("%-*s", (int)width, header);
34dc7c2f
BB
5989 }
5990
5991 (void) printf("\n");
5992}
5993
5994/*
5995 * Given a pool and a list of properties, print out all the properties according
cc99f275 5996 * to the described layout. Used by zpool_do_list().
34dc7c2f
BB
5997 */
5998static void
1bd201e7 5999print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
34dc7c2f 6000{
1bd201e7 6001 zprop_list_t *pl = cb->cb_proplist;
34dc7c2f
BB
6002 boolean_t first = B_TRUE;
6003 char property[ZPOOL_MAXPROPLEN];
6004 char *propstr;
6005 boolean_t right_justify;
1bd201e7 6006 size_t width;
34dc7c2f
BB
6007
6008 for (; pl != NULL; pl = pl->pl_next) {
1bd201e7
CS
6009
6010 width = pl->pl_width;
6011 if (first && cb->cb_verbose) {
6012 /*
6013 * Reset the width to accommodate the verbose listing
6014 * of devices.
6015 */
6016 width = cb->cb_namewidth;
6017 }
6018
34dc7c2f 6019 if (!first) {
1bd201e7 6020 if (cb->cb_scripted)
34dc7c2f
BB
6021 (void) printf("\t");
6022 else
6023 (void) printf(" ");
6024 } else {
6025 first = B_FALSE;
6026 }
6027
6028 right_justify = B_FALSE;
6029 if (pl->pl_prop != ZPROP_INVAL) {
a05dfd00 6030 if (zpool_get_prop(zhp, pl->pl_prop, property,
2a8b84b7 6031 sizeof (property), NULL, cb->cb_literal) != 0)
34dc7c2f
BB
6032 propstr = "-";
6033 else
6034 propstr = property;
6035
6036 right_justify = zpool_prop_align_right(pl->pl_prop);
9ae529ec
CS
6037 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
6038 zpool_prop_unsupported(pl->pl_user_prop)) &&
6039 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
6040 sizeof (property)) == 0) {
6041 propstr = property;
34dc7c2f
BB
6042 } else {
6043 propstr = "-";
6044 }
6045
34dc7c2f
BB
6046
6047 /*
6048 * If this is being called in scripted mode, or if this is the
6049 * last column and it is left-justified, don't include a width
6050 * format specifier.
6051 */
1bd201e7 6052 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
34dc7c2f
BB
6053 (void) printf("%s", propstr);
6054 else if (right_justify)
1bd201e7 6055 (void) printf("%*s", (int)width, propstr);
34dc7c2f 6056 else
1bd201e7 6057 (void) printf("%-*s", (int)width, propstr);
34dc7c2f
BB
6058 }
6059
6060 (void) printf("\n");
6061}
6062
1bd201e7 6063static void
c5eea0ab
BB
6064print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
6065 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format)
1bd201e7
CS
6066{
6067 char propval[64];
6068 boolean_t fixed;
6069 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
6070
a05dfd00
GW
6071 switch (prop) {
6072 case ZPOOL_PROP_EXPANDSZ:
d2734cce 6073 case ZPOOL_PROP_CHECKPOINT:
c5eea0ab 6074 case ZPOOL_PROP_DEDUPRATIO:
a05dfd00
GW
6075 if (value == 0)
6076 (void) strlcpy(propval, "-", sizeof (propval));
6077 else
bc2d8093
CE
6078 zfs_nicenum_format(value, propval, sizeof (propval),
6079 format);
a05dfd00
GW
6080 break;
6081 case ZPOOL_PROP_FRAGMENTATION:
6082 if (value == ZFS_FRAG_INVALID) {
6083 (void) strlcpy(propval, "-", sizeof (propval));
bc2d8093
CE
6084 } else if (format == ZFS_NICENUM_RAW) {
6085 (void) snprintf(propval, sizeof (propval), "%llu",
6086 (unsigned long long)value);
a05dfd00
GW
6087 } else {
6088 (void) snprintf(propval, sizeof (propval), "%llu%%",
6089 (unsigned long long)value);
6090 }
6091 break;
6092 case ZPOOL_PROP_CAPACITY:
cc99f275 6093 /* capacity value is in parts-per-10,000 (aka permyriad) */
bc2d8093
CE
6094 if (format == ZFS_NICENUM_RAW)
6095 (void) snprintf(propval, sizeof (propval), "%llu",
cc99f275 6096 (unsigned long long)value / 100);
bc2d8093 6097 else
cc99f275
DB
6098 (void) snprintf(propval, sizeof (propval),
6099 value < 1000 ? "%1.2f%%" : value < 10000 ?
6100 "%2.1f%%" : "%3.0f%%", value / 100.0);
a05dfd00 6101 break;
c5eea0ab
BB
6102 case ZPOOL_PROP_HEALTH:
6103 width = 8;
96904d87 6104 (void) strlcpy(propval, str, sizeof (propval));
c5eea0ab 6105 break;
a05dfd00 6106 default:
bc2d8093 6107 zfs_nicenum_format(value, propval, sizeof (propval), format);
a05dfd00
GW
6108 }
6109
6110 if (!valid)
6111 (void) strlcpy(propval, "-", sizeof (propval));
1bd201e7
CS
6112
6113 if (scripted)
6114 (void) printf("\t%s", propval);
6115 else
6116 (void) printf(" %*s", (int)width, propval);
6117}
6118
cc99f275
DB
6119/*
6120 * print static default line per vdev
6121 * not compatible with '-o' <proplist> option
6122 */
65c7cc49 6123static void
1bd201e7 6124print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
c5eea0ab 6125 list_cbdata_t *cb, int depth, boolean_t isspare)
1bd201e7
CS
6126{
6127 nvlist_t **child;
6128 vdev_stat_t *vs;
6129 uint_t c, children;
6130 char *vname;
6131 boolean_t scripted = cb->cb_scripted;
8e4c5c9a 6132 uint64_t islog = B_FALSE;
c5eea0ab
BB
6133 char *dashes = "%-*s - - - - "
6134 "- - - - -\n";
1bd201e7
CS
6135
6136 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
6137 (uint64_t **)&vs, &c) == 0);
6138
6139 if (name != NULL) {
a05dfd00
GW
6140 boolean_t toplevel = (vs->vs_space != 0);
6141 uint64_t cap;
bc2d8093 6142 enum zfs_nicenum_format format;
c5eea0ab 6143 const char *state;
bc2d8093
CE
6144
6145 if (cb->cb_literal)
6146 format = ZFS_NICENUM_RAW;
6147 else
6148 format = ZFS_NICENUM_1024;
a05dfd00 6149
a1d477c2
MA
6150 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
6151 return;
6152
1bd201e7
CS
6153 if (scripted)
6154 (void) printf("\t%s", name);
6155 else if (strlen(name) + depth > cb->cb_namewidth)
6156 (void) printf("%*s%s", depth, "", name);
6157 else
6158 (void) printf("%*s%s%*s", depth, "", name,
6159 (int)(cb->cb_namewidth - strlen(name) - depth), "");
6160
a05dfd00
GW
6161 /*
6162 * Print the properties for the individual vdevs. Some
6163 * properties are only applicable to toplevel vdevs. The
6164 * 'toplevel' boolean value is passed to the print_one_column()
6165 * to indicate that the value is valid.
6166 */
c5eea0ab 6167 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL, scripted,
bc2d8093 6168 toplevel, format);
c5eea0ab 6169 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL,
bc2d8093 6170 scripted, toplevel, format);
c5eea0ab
BB
6171 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
6172 NULL, scripted, toplevel, format);
d2734cce 6173 print_one_column(ZPOOL_PROP_CHECKPOINT,
c5eea0ab
BB
6174 vs->vs_checkpoint_space, NULL, scripted, toplevel, format);
6175 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL,
6176 scripted, B_TRUE, format);
a05dfd00 6177 print_one_column(ZPOOL_PROP_FRAGMENTATION,
c5eea0ab 6178 vs->vs_fragmentation, NULL, scripted,
bc2d8093
CE
6179 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
6180 format);
a05dfd00 6181 cap = (vs->vs_space == 0) ? 0 :
cc99f275 6182 (vs->vs_alloc * 10000 / vs->vs_space);
c5eea0ab
BB
6183 print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL,
6184 scripted, toplevel, format);
6185 print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL,
6186 scripted, toplevel, format);
6187 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6188 if (isspare) {
6189 if (vs->vs_aux == VDEV_AUX_SPARED)
6190 state = "INUSE";
6191 else if (vs->vs_state == VDEV_STATE_HEALTHY)
6192 state = "AVAIL";
6193 }
6194 print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted,
6195 B_TRUE, format);
1bd201e7
CS
6196 (void) printf("\n");
6197 }
6198
6199 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
6200 &child, &children) != 0)
6201 return;
6202
cc99f275 6203 /* list the normal vdevs first */
1bd201e7
CS
6204 for (c = 0; c < children; c++) {
6205 uint64_t ishole = B_FALSE;
6206
6207 if (nvlist_lookup_uint64(child[c],
6208 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
6209 continue;
6210
8e4c5c9a 6211 if (nvlist_lookup_uint64(child[c],
cc99f275
DB
6212 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
6213 continue;
6214
6215 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
8e4c5c9a 6216 continue;
8e4c5c9a 6217
d2f3e292
RY
6218 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6219 cb->cb_name_flags);
c5eea0ab 6220 print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE);
1bd201e7
CS
6221 free(vname);
6222 }
6223
cc99f275
DB
6224 /* list the classes: 'logs', 'dedup', and 'special' */
6225 for (uint_t n = 0; n < 3; n++) {
6226 boolean_t printed = B_FALSE;
6227
8e4c5c9a 6228 for (c = 0; c < children; c++) {
cc99f275
DB
6229 char *bias = NULL;
6230 char *type = NULL;
6231
8e4c5c9a 6232 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
cc99f275
DB
6233 &islog) == 0 && islog) {
6234 bias = VDEV_ALLOC_CLASS_LOGS;
6235 } else {
6236 (void) nvlist_lookup_string(child[c],
6237 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
6238 (void) nvlist_lookup_string(child[c],
6239 ZPOOL_CONFIG_TYPE, &type);
6240 }
6241 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
8e4c5c9a 6242 continue;
cc99f275
DB
6243 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
6244 continue;
6245
6246 if (!printed) {
6247 /* LINTED E_SEC_PRINTF_VAR_FMT */
6248 (void) printf(dashes, cb->cb_namewidth,
6249 class_name[n]);
6250 printed = B_TRUE;
6251 }
d2f3e292
RY
6252 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6253 cb->cb_name_flags);
c5eea0ab
BB
6254 print_list_stats(zhp, vname, child[c], cb, depth + 2,
6255 B_FALSE);
8e4c5c9a
JWK
6256 free(vname);
6257 }
6258 }
6259
1bd201e7 6260 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
8e4c5c9a
JWK
6261 &child, &children) == 0 && children > 0) {
6262 /* LINTED E_SEC_PRINTF_VAR_FMT */
6263 (void) printf(dashes, cb->cb_namewidth, "cache");
6264 for (c = 0; c < children; c++) {
d2f3e292
RY
6265 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6266 cb->cb_name_flags);
c5eea0ab
BB
6267 print_list_stats(zhp, vname, child[c], cb, depth + 2,
6268 B_FALSE);
8e4c5c9a
JWK
6269 free(vname);
6270 }
6271 }
1bd201e7 6272
8e4c5c9a
JWK
6273 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
6274 &children) == 0 && children > 0) {
6275 /* LINTED E_SEC_PRINTF_VAR_FMT */
6276 (void) printf(dashes, cb->cb_namewidth, "spare");
1bd201e7 6277 for (c = 0; c < children; c++) {
d2f3e292
RY
6278 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6279 cb->cb_name_flags);
c5eea0ab
BB
6280 print_list_stats(zhp, vname, child[c], cb, depth + 2,
6281 B_TRUE);
1bd201e7
CS
6282 free(vname);
6283 }
6284 }
6285}
6286
34dc7c2f
BB
6287/*
6288 * Generic callback function to list a pool.
6289 */
65c7cc49 6290static int
34dc7c2f
BB
6291list_callback(zpool_handle_t *zhp, void *data)
6292{
6293 list_cbdata_t *cbp = data;
6294
c5eea0ab 6295 print_pool(zhp, cbp);
34dc7c2f 6296
cc99f275 6297 if (cbp->cb_verbose) {
c5eea0ab 6298 nvlist_t *config, *nvroot;
cc99f275 6299
c5eea0ab 6300 config = zpool_get_config(zhp, NULL);
cc99f275
DB
6301 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6302 &nvroot) == 0);
c5eea0ab 6303 print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE);
cc99f275
DB
6304 }
6305
c5eea0ab
BB
6306 return (0);
6307}
cc99f275 6308
c5eea0ab
BB
6309/*
6310 * Set the minimum pool/vdev name column width. The width must be at least 9,
6311 * but may be as large as needed.
6312 */
6313static int
6314get_namewidth_list(zpool_handle_t *zhp, void *data)
6315{
6316 list_cbdata_t *cb = data;
6317 int width;
6318
6319 width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
6320 cb->cb_verbose);
1bd201e7 6321
c5eea0ab
BB
6322 if (width < 9)
6323 width = 9;
6324
6325 cb->cb_namewidth = width;
34dc7c2f
BB
6326
6327 return (0);
6328}
6329
6330/*
2a8b84b7 6331 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
34dc7c2f 6332 *
d2f3e292 6333 * -g Display guid for individual vdev name.
34dc7c2f
BB
6334 * -H Scripted mode. Don't display headers, and separate properties
6335 * by a single tab.
d2f3e292 6336 * -L Follow links when resolving vdev path name.
34dc7c2f 6337 * -o List of properties to display. Defaults to
a05dfd00
GW
6338 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6339 * "dedupratio,health,altroot"
c5eea0ab 6340 * -p Display values in parsable (exact) format.
a77f29f9 6341 * -P Display full path for vdev name.
428870ff 6342 * -T Display a timestamp in date(1) or Unix format
34dc7c2f
BB
6343 *
6344 * List all pools in the system, whether or not they're healthy. Output space
6345 * statistics for each one, as well as health status summary.
6346 */
6347int
6348zpool_do_list(int argc, char **argv)
6349{
6350 int c;
cd72af9c 6351 int ret = 0;
34dc7c2f
BB
6352 list_cbdata_t cb = { 0 };
6353 static char default_props[] =
d2734cce
SD
6354 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6355 "capacity,dedupratio,health,altroot";
34dc7c2f 6356 char *props = default_props;
193a37cb
TH
6357 float interval = 0;
6358 unsigned long count = 0;
1bd201e7
CS
6359 zpool_list_t *list;
6360 boolean_t first = B_TRUE;
2a673e76 6361 current_prop_type = ZFS_TYPE_POOL;
34dc7c2f
BB
6362
6363 /* check options */
2a8b84b7 6364 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
34dc7c2f 6365 switch (c) {
d2f3e292
RY
6366 case 'g':
6367 cb.cb_name_flags |= VDEV_NAME_GUID;
6368 break;
34dc7c2f
BB
6369 case 'H':
6370 cb.cb_scripted = B_TRUE;
6371 break;
d2f3e292
RY
6372 case 'L':
6373 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6374 break;
34dc7c2f
BB
6375 case 'o':
6376 props = optarg;
6377 break;
a77f29f9 6378 case 'P':
d2f3e292
RY
6379 cb.cb_name_flags |= VDEV_NAME_PATH;
6380 break;
2a8b84b7
AS
6381 case 'p':
6382 cb.cb_literal = B_TRUE;
6383 break;
428870ff
BB
6384 case 'T':
6385 get_timestamp_arg(*optarg);
6386 break;
1bd201e7
CS
6387 case 'v':
6388 cb.cb_verbose = B_TRUE;
cc99f275 6389 cb.cb_namewidth = 8; /* 8 until precalc is avail */
1bd201e7 6390 break;
34dc7c2f
BB
6391 case ':':
6392 (void) fprintf(stderr, gettext("missing argument for "
6393 "'%c' option\n"), optopt);
6394 usage(B_FALSE);
6395 break;
6396 case '?':
6397 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6398 optopt);
6399 usage(B_FALSE);
6400 }
6401 }
6402
6403 argc -= optind;
6404 argv += optind;
6405
428870ff
BB
6406 get_interval_count(&argc, argv, &interval, &count);
6407
34dc7c2f
BB
6408 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
6409 usage(B_FALSE);
6410
428870ff 6411 for (;;) {
3e43edd2 6412 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
2a673e76 6413 ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL)
3e43edd2 6414 return (1);
1bd201e7
CS
6415
6416 if (pool_list_count(list) == 0)
6417 break;
34dc7c2f 6418
c5eea0ab
BB
6419 cb.cb_namewidth = 0;
6420 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb);
6421
428870ff
BB
6422 if (timestamp_fmt != NODATE)
6423 print_timestamp(timestamp_fmt);
34dc7c2f 6424
1bd201e7
CS
6425 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
6426 print_header(&cb);
6427 first = B_FALSE;
428870ff 6428 }
1bd201e7 6429 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
428870ff
BB
6430
6431 if (interval == 0)
6432 break;
6433
6434 if (count != 0 && --count == 0)
6435 break;
6436
3e43edd2 6437 pool_list_free(list);
193a37cb 6438 (void) fsleep(interval);
34dc7c2f
BB
6439 }
6440
3e43edd2
GW
6441 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
6442 (void) printf(gettext("no pools available\n"));
6443 ret = 0;
6444 }
6445
6446 pool_list_free(list);
428870ff 6447 zprop_free_list(cb.cb_proplist);
34dc7c2f
BB
6448 return (ret);
6449}
6450
34dc7c2f
BB
6451static int
6452zpool_do_attach_or_replace(int argc, char **argv, int replacing)
6453{
6454 boolean_t force = B_FALSE;
9a49d3f3 6455 boolean_t rebuild = B_FALSE;
e60e158e 6456 boolean_t wait = B_FALSE;
34dc7c2f
BB
6457 int c;
6458 nvlist_t *nvroot;
6459 char *poolname, *old_disk, *new_disk;
6460 zpool_handle_t *zhp;
df831108
CP
6461 nvlist_t *props = NULL;
6462 char *propval;
34dc7c2f
BB
6463 int ret;
6464
6465 /* check options */
9a49d3f3 6466 while ((c = getopt(argc, argv, "fo:sw")) != -1) {
34dc7c2f
BB
6467 switch (c) {
6468 case 'f':
6469 force = B_TRUE;
6470 break;
df831108
CP
6471 case 'o':
6472 if ((propval = strchr(optarg, '=')) == NULL) {
6473 (void) fprintf(stderr, gettext("missing "
6474 "'=' for -o option\n"));
6475 usage(B_FALSE);
6476 }
6477 *propval = '\0';
6478 propval++;
6479
6480 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
6481 (add_prop_list(optarg, propval, &props, B_TRUE)))
6482 usage(B_FALSE);
6483 break;
9a49d3f3
BB
6484 case 's':
6485 rebuild = B_TRUE;
6486 break;
e60e158e
JG
6487 case 'w':
6488 wait = B_TRUE;
6489 break;
34dc7c2f
BB
6490 case '?':
6491 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6492 optopt);
6493 usage(B_FALSE);
6494 }
6495 }
6496
6497 argc -= optind;
6498 argv += optind;
6499
6500 /* get pool name and check number of arguments */
6501 if (argc < 1) {
6502 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6503 usage(B_FALSE);
6504 }
6505
6506 poolname = argv[0];
6507
6508 if (argc < 2) {
6509 (void) fprintf(stderr,
6510 gettext("missing <device> specification\n"));
6511 usage(B_FALSE);
6512 }
6513
6514 old_disk = argv[1];
6515
6516 if (argc < 3) {
6517 if (!replacing) {
6518 (void) fprintf(stderr,
6519 gettext("missing <new_device> specification\n"));
6520 usage(B_FALSE);
6521 }
6522 new_disk = old_disk;
6523 argc -= 1;
6524 argv += 1;
6525 } else {
6526 new_disk = argv[2];
6527 argc -= 2;
6528 argv += 2;
6529 }
6530
6531 if (argc > 1) {
6532 (void) fprintf(stderr, gettext("too many arguments\n"));
6533 usage(B_FALSE);
6534 }
6535
a425f5bf 6536 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
6537 nvlist_free(props);
34dc7c2f 6538 return (1);
a425f5bf 6539 }
34dc7c2f
BB
6540
6541 if (zpool_get_config(zhp, NULL) == NULL) {
6542 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
6543 poolname);
6544 zpool_close(zhp);
a425f5bf 6545 nvlist_free(props);
34dc7c2f
BB
6546 return (1);
6547 }
6548
dddef7d6 6549 /* unless manually specified use "ashift" pool property (if set) */
6550 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
6551 int intval;
6552 zprop_source_t src;
6553 char strval[ZPOOL_MAXPROPLEN];
6554
6555 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
6556 if (src != ZPROP_SRC_DEFAULT) {
6557 (void) sprintf(strval, "%" PRId32, intval);
6558 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
6559 &props, B_TRUE) == 0);
6560 }
6561 }
6562
df831108 6563 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
b128c09f 6564 argc, argv);
34dc7c2f
BB
6565 if (nvroot == NULL) {
6566 zpool_close(zhp);
a425f5bf 6567 nvlist_free(props);
34dc7c2f
BB
6568 return (1);
6569 }
6570
9a49d3f3
BB
6571 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing,
6572 rebuild);
34dc7c2f 6573
e60e158e
JG
6574 if (ret == 0 && wait)
6575 ret = zpool_wait(zhp,
6576 replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER);
6577
a425f5bf 6578 nvlist_free(props);
34dc7c2f
BB
6579 nvlist_free(nvroot);
6580 zpool_close(zhp);
6581
6582 return (ret);
6583}
6584
6585/*
9a49d3f3 6586 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
34dc7c2f
BB
6587 *
6588 * -f Force attach, even if <new_device> appears to be in use.
9a49d3f3 6589 * -s Use sequential instead of healing reconstruction for resilver.
e60e158e
JG
6590 * -o Set property=value.
6591 * -w Wait for replacing to complete before returning
34dc7c2f
BB
6592 *
6593 * Replace <device> with <new_device>.
6594 */
34dc7c2f
BB
6595int
6596zpool_do_replace(int argc, char **argv)
6597{
6598 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
6599}
6600
6601/*
9a49d3f3 6602 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
34dc7c2f
BB
6603 *
6604 * -f Force attach, even if <new_device> appears to be in use.
9a49d3f3 6605 * -s Use sequential instead of healing reconstruction for resilver.
df831108 6606 * -o Set property=value.
e60e158e 6607 * -w Wait for resilvering to complete before returning
34dc7c2f
BB
6608 *
6609 * Attach <new_device> to the mirror containing <device>. If <device> is not
6610 * part of a mirror, then <device> will be transformed into a mirror of
6611 * <device> and <new_device>. In either case, <new_device> will begin life
6612 * with a DTL of [0, now], and will immediately begin to resilver itself.
6613 */
6614int
6615zpool_do_attach(int argc, char **argv)
6616{
6617 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
6618}
6619
6620/*
6621 * zpool detach [-f] <pool> <device>
6622 *
6623 * -f Force detach of <device>, even if DTLs argue against it
6624 * (not supported yet)
6625 *
6626 * Detach a device from a mirror. The operation will be refused if <device>
6627 * is the last device in the mirror, or if the DTLs indicate that this device
6628 * has the only valid copy of some data.
6629 */
34dc7c2f
BB
6630int
6631zpool_do_detach(int argc, char **argv)
6632{
6633 int c;
6634 char *poolname, *path;
6635 zpool_handle_t *zhp;
6636 int ret;
6637
6638 /* check options */
d02186ee 6639 while ((c = getopt(argc, argv, "")) != -1) {
34dc7c2f 6640 switch (c) {
34dc7c2f
BB
6641 case '?':
6642 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6643 optopt);
6644 usage(B_FALSE);
6645 }
6646 }
6647
6648 argc -= optind;
6649 argv += optind;
6650
6651 /* get pool name and check number of arguments */
6652 if (argc < 1) {
6653 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6654 usage(B_FALSE);
6655 }
6656
6657 if (argc < 2) {
6658 (void) fprintf(stderr,
6659 gettext("missing <device> specification\n"));
6660 usage(B_FALSE);
6661 }
6662
6663 poolname = argv[0];
6664 path = argv[1];
6665
6666 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6667 return (1);
6668
6669 ret = zpool_vdev_detach(zhp, path);
6670
6671 zpool_close(zhp);
6672
6673 return (ret);
6674}
6675
428870ff 6676/*
a77f29f9 6677 * zpool split [-gLnP] [-o prop=val] ...
428870ff
BB
6678 * [-o mntopt] ...
6679 * [-R altroot] <pool> <newpool> [<device> ...]
6680 *
d2f3e292
RY
6681 * -g Display guid for individual vdev name.
6682 * -L Follow links when resolving vdev path name.
428870ff
BB
6683 * -n Do not split the pool, but display the resulting layout if
6684 * it were to be split.
6685 * -o Set property=value, or set mount options.
a77f29f9 6686 * -P Display full path for vdev name.
428870ff 6687 * -R Mount the split-off pool under an alternate root.
b5256303 6688 * -l Load encryption keys while importing.
428870ff
BB
6689 *
6690 * Splits the named pool and gives it the new pool name. Devices to be split
6691 * off may be listed, provided that no more than one device is specified
6692 * per top-level vdev mirror. The newly split pool is left in an exported
6693 * state unless -R is specified.
6694 *
6695 * Restrictions: the top-level of the pool pool must only be made up of
6696 * mirrors; all devices in the pool must be healthy; no device may be
6697 * undergoing a resilvering operation.
6698 */
6699int
6700zpool_do_split(int argc, char **argv)
6701{
6702 char *srcpool, *newpool, *propval;
6703 char *mntopts = NULL;
6704 splitflags_t flags;
6705 int c, ret = 0;
b5256303 6706 boolean_t loadkeys = B_FALSE;
428870ff
BB
6707 zpool_handle_t *zhp;
6708 nvlist_t *config, *props = NULL;
6709
6710 flags.dryrun = B_FALSE;
6711 flags.import = B_FALSE;
d2f3e292 6712 flags.name_flags = 0;
428870ff
BB
6713
6714 /* check options */
b5256303 6715 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
428870ff 6716 switch (c) {
d2f3e292
RY
6717 case 'g':
6718 flags.name_flags |= VDEV_NAME_GUID;
6719 break;
6720 case 'L':
6721 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
6722 break;
428870ff
BB
6723 case 'R':
6724 flags.import = B_TRUE;
6725 if (add_prop_list(
6726 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
6727 &props, B_TRUE) != 0) {
8a5fc748 6728 nvlist_free(props);
428870ff
BB
6729 usage(B_FALSE);
6730 }
6731 break;
b5256303
TC
6732 case 'l':
6733 loadkeys = B_TRUE;
6734 break;
428870ff
BB
6735 case 'n':
6736 flags.dryrun = B_TRUE;
6737 break;
6738 case 'o':
6739 if ((propval = strchr(optarg, '=')) != NULL) {
6740 *propval = '\0';
6741 propval++;
6742 if (add_prop_list(optarg, propval,
6743 &props, B_TRUE) != 0) {
8a5fc748 6744 nvlist_free(props);
428870ff
BB
6745 usage(B_FALSE);
6746 }
6747 } else {
6748 mntopts = optarg;
6749 }
6750 break;
a77f29f9 6751 case 'P':
d2f3e292
RY
6752 flags.name_flags |= VDEV_NAME_PATH;
6753 break;
428870ff
BB
6754 case ':':
6755 (void) fprintf(stderr, gettext("missing argument for "
6756 "'%c' option\n"), optopt);
6757 usage(B_FALSE);
6758 break;
6759 case '?':
6760 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6761 optopt);
6762 usage(B_FALSE);
6763 break;
6764 }
6765 }
6766
6767 if (!flags.import && mntopts != NULL) {
6768 (void) fprintf(stderr, gettext("setting mntopts is only "
6769 "valid when importing the pool\n"));
6770 usage(B_FALSE);
6771 }
6772
b5256303
TC
6773 if (!flags.import && loadkeys) {
6774 (void) fprintf(stderr, gettext("loading keys is only "
6775 "valid when importing the pool\n"));
6776 usage(B_FALSE);
6777 }
6778
428870ff
BB
6779 argc -= optind;
6780 argv += optind;
6781
6782 if (argc < 1) {
6783 (void) fprintf(stderr, gettext("Missing pool name\n"));
6784 usage(B_FALSE);
6785 }
6786 if (argc < 2) {
6787 (void) fprintf(stderr, gettext("Missing new pool name\n"));
6788 usage(B_FALSE);
6789 }
6790
6791 srcpool = argv[0];
6792 newpool = argv[1];
6793
6794 argc -= 2;
6795 argv += 2;
6796
a425f5bf 6797 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
6798 nvlist_free(props);
428870ff 6799 return (1);
a425f5bf 6800 }
428870ff
BB
6801
6802 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
6803 if (config == NULL) {
6804 ret = 1;
6805 } else {
6806 if (flags.dryrun) {
6807 (void) printf(gettext("would create '%s' with the "
6808 "following layout:\n\n"), newpool);
cc99f275 6809 print_vdev_tree(NULL, newpool, config, 0, "",
d2f3e292 6810 flags.name_flags);
0cb40fa3
AF
6811 print_vdev_tree(NULL, "dedup", config, 0,
6812 VDEV_ALLOC_BIAS_DEDUP, 0);
6813 print_vdev_tree(NULL, "special", config, 0,
6814 VDEV_ALLOC_BIAS_SPECIAL, 0);
428870ff 6815 }
428870ff
BB
6816 }
6817
6818 zpool_close(zhp);
6819
a425f5bf 6820 if (ret != 0 || flags.dryrun || !flags.import) {
6821 nvlist_free(config);
6822 nvlist_free(props);
428870ff 6823 return (ret);
a425f5bf 6824 }
428870ff
BB
6825
6826 /*
6827 * The split was successful. Now we need to open the new
6828 * pool and import it.
6829 */
a425f5bf 6830 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
6831 nvlist_free(config);
6832 nvlist_free(props);
428870ff 6833 return (1);
a425f5bf 6834 }
b5256303
TC
6835
6836 if (loadkeys) {
6837 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
6838 if (ret != 0)
6839 ret = 1;
6840 }
6841
428870ff
BB
6842 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
6843 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
6844 ret = 1;
af909a10 6845 (void) fprintf(stderr, gettext("Split was successful, but "
428870ff
BB
6846 "the datasets could not all be mounted\n"));
6847 (void) fprintf(stderr, gettext("Try doing '%s' with a "
6848 "different altroot\n"), "zpool import");
6849 }
6850 zpool_close(zhp);
a425f5bf 6851 nvlist_free(config);
6852 nvlist_free(props);
428870ff
BB
6853
6854 return (ret);
6855}
6856
6857
6858
34dc7c2f
BB
6859/*
6860 * zpool online <pool> <device> ...
6861 */
6862int
6863zpool_do_online(int argc, char **argv)
6864{
6865 int c, i;
6866 char *poolname;
6867 zpool_handle_t *zhp;
6868 int ret = 0;
6869 vdev_state_t newstate;
9babb374 6870 int flags = 0;
34dc7c2f
BB
6871
6872 /* check options */
d02186ee 6873 while ((c = getopt(argc, argv, "e")) != -1) {
34dc7c2f 6874 switch (c) {
9babb374
BB
6875 case 'e':
6876 flags |= ZFS_ONLINE_EXPAND;
6877 break;
34dc7c2f
BB
6878 case '?':
6879 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6880 optopt);
6881 usage(B_FALSE);
6882 }
6883 }
6884
6885 argc -= optind;
6886 argv += optind;
6887
6888 /* get pool name and check number of arguments */
6889 if (argc < 1) {
6890 (void) fprintf(stderr, gettext("missing pool name\n"));
6891 usage(B_FALSE);
6892 }
6893 if (argc < 2) {
6894 (void) fprintf(stderr, gettext("missing device name\n"));
6895 usage(B_FALSE);
6896 }
6897
6898 poolname = argv[0];
6899
6900 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6901 return (1);
6902
6903 for (i = 1; i < argc; i++) {
9babb374 6904 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
34dc7c2f
BB
6905 if (newstate != VDEV_STATE_HEALTHY) {
6906 (void) printf(gettext("warning: device '%s' "
6907 "onlined, but remains in faulted state\n"),
6908 argv[i]);
6909 if (newstate == VDEV_STATE_FAULTED)
6910 (void) printf(gettext("use 'zpool "
6911 "clear' to restore a faulted "
6912 "device\n"));
6913 else
6914 (void) printf(gettext("use 'zpool "
6915 "replace' to replace devices "
6916 "that are no longer present\n"));
6917 }
6918 } else {
6919 ret = 1;
6920 }
6921 }
6922
6923 zpool_close(zhp);
6924
6925 return (ret);
6926}
6927
6928/*
6929 * zpool offline [-ft] <pool> <device> ...
6930 *
4a283c7f 6931 * -f Force the device into a faulted state.
34dc7c2f 6932 *
4a283c7f 6933 * -t Only take the device off-line temporarily. The offline/faulted
34dc7c2f
BB
6934 * state will not be persistent across reboots.
6935 */
34dc7c2f
BB
6936int
6937zpool_do_offline(int argc, char **argv)
6938{
6939 int c, i;
6940 char *poolname;
6941 zpool_handle_t *zhp;
6942 int ret = 0;
6943 boolean_t istmp = B_FALSE;
4a283c7f 6944 boolean_t fault = B_FALSE;
34dc7c2f
BB
6945
6946 /* check options */
6947 while ((c = getopt(argc, argv, "ft")) != -1) {
6948 switch (c) {
4a283c7f
TH
6949 case 'f':
6950 fault = B_TRUE;
6951 break;
34dc7c2f
BB
6952 case 't':
6953 istmp = B_TRUE;
6954 break;
34dc7c2f
BB
6955 case '?':
6956 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6957 optopt);
6958 usage(B_FALSE);
6959 }
6960 }
6961
6962 argc -= optind;
6963 argv += optind;
6964
6965 /* get pool name and check number of arguments */
6966 if (argc < 1) {
6967 (void) fprintf(stderr, gettext("missing pool name\n"));
6968 usage(B_FALSE);
6969 }
6970 if (argc < 2) {
6971 (void) fprintf(stderr, gettext("missing device name\n"));
6972 usage(B_FALSE);
6973 }
6974
6975 poolname = argv[0];
6976
6977 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6978 return (1);
6979
6980 for (i = 1; i < argc; i++) {
4a283c7f
TH
6981 if (fault) {
6982 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
6983 vdev_aux_t aux;
6984 if (istmp == B_FALSE) {
6985 /* Force the fault to persist across imports */
6986 aux = VDEV_AUX_EXTERNAL_PERSIST;
6987 } else {
6988 aux = VDEV_AUX_EXTERNAL;
6989 }
6990
6991 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
6992 ret = 1;
6993 } else {
6994 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
6995 ret = 1;
6996 }
34dc7c2f
BB
6997 }
6998
6999 zpool_close(zhp);
7000
7001 return (ret);
7002}
7003
7004/*
7005 * zpool clear <pool> [device]
7006 *
7007 * Clear all errors associated with a pool or a particular device.
7008 */
7009int
7010zpool_do_clear(int argc, char **argv)
7011{
428870ff 7012 int c;
34dc7c2f 7013 int ret = 0;
428870ff
BB
7014 boolean_t dryrun = B_FALSE;
7015 boolean_t do_rewind = B_FALSE;
7016 boolean_t xtreme_rewind = B_FALSE;
7017 uint32_t rewind_policy = ZPOOL_NO_REWIND;
7018 nvlist_t *policy = NULL;
34dc7c2f
BB
7019 zpool_handle_t *zhp;
7020 char *pool, *device;
7021
428870ff
BB
7022 /* check options */
7023 while ((c = getopt(argc, argv, "FnX")) != -1) {
7024 switch (c) {
7025 case 'F':
7026 do_rewind = B_TRUE;
7027 break;
7028 case 'n':
7029 dryrun = B_TRUE;
7030 break;
7031 case 'X':
7032 xtreme_rewind = B_TRUE;
7033 break;
7034 case '?':
7035 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7036 optopt);
7037 usage(B_FALSE);
7038 }
7039 }
7040
7041 argc -= optind;
7042 argv += optind;
7043
7044 if (argc < 1) {
34dc7c2f
BB
7045 (void) fprintf(stderr, gettext("missing pool name\n"));
7046 usage(B_FALSE);
7047 }
7048
428870ff 7049 if (argc > 2) {
34dc7c2f
BB
7050 (void) fprintf(stderr, gettext("too many arguments\n"));
7051 usage(B_FALSE);
7052 }
7053
428870ff
BB
7054 if ((dryrun || xtreme_rewind) && !do_rewind) {
7055 (void) fprintf(stderr,
7056 gettext("-n or -X only meaningful with -F\n"));
7057 usage(B_FALSE);
7058 }
7059 if (dryrun)
7060 rewind_policy = ZPOOL_TRY_REWIND;
7061 else if (do_rewind)
7062 rewind_policy = ZPOOL_DO_REWIND;
7063 if (xtreme_rewind)
7064 rewind_policy |= ZPOOL_EXTREME_REWIND;
7065
7066 /* In future, further rewind policy choices can be passed along here */
7067 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
8a393be3
PZ
7068 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
7069 rewind_policy) != 0) {
428870ff 7070 return (1);
8a393be3 7071 }
428870ff
BB
7072
7073 pool = argv[0];
7074 device = argc == 2 ? argv[1] : NULL;
34dc7c2f 7075
428870ff
BB
7076 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
7077 nvlist_free(policy);
34dc7c2f 7078 return (1);
428870ff 7079 }
34dc7c2f 7080
428870ff 7081 if (zpool_clear(zhp, device, policy) != 0)
34dc7c2f
BB
7082 ret = 1;
7083
7084 zpool_close(zhp);
7085
428870ff
BB
7086 nvlist_free(policy);
7087
34dc7c2f
BB
7088 return (ret);
7089}
7090
3541dc6d
GA
7091/*
7092 * zpool reguid <pool>
7093 */
7094int
7095zpool_do_reguid(int argc, char **argv)
7096{
7097 int c;
7098 char *poolname;
7099 zpool_handle_t *zhp;
7100 int ret = 0;
7101
7102 /* check options */
7103 while ((c = getopt(argc, argv, "")) != -1) {
7104 switch (c) {
7105 case '?':
7106 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7107 optopt);
7108 usage(B_FALSE);
7109 }
7110 }
7111
7112 argc -= optind;
7113 argv += optind;
7114
7115 /* get pool name and check number of arguments */
7116 if (argc < 1) {
7117 (void) fprintf(stderr, gettext("missing pool name\n"));
7118 usage(B_FALSE);
7119 }
7120
7121 if (argc > 1) {
7122 (void) fprintf(stderr, gettext("too many arguments\n"));
7123 usage(B_FALSE);
7124 }
7125
7126 poolname = argv[0];
7127 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
7128 return (1);
7129
7130 ret = zpool_reguid(zhp);
7131
7132 zpool_close(zhp);
7133 return (ret);
7134}
7135
7136
1bd201e7
CS
7137/*
7138 * zpool reopen <pool>
7139 *
7140 * Reopen the pool so that the kernel can update the sizes of all vdevs.
1bd201e7
CS
7141 */
7142int
7143zpool_do_reopen(int argc, char **argv)
7144{
5853fe79 7145 int c;
1bd201e7 7146 int ret = 0;
d3f2cd7e 7147 boolean_t scrub_restart = B_TRUE;
1bd201e7 7148
5853fe79 7149 /* check options */
d3f2cd7e 7150 while ((c = getopt(argc, argv, "n")) != -1) {
5853fe79 7151 switch (c) {
d3f2cd7e
AB
7152 case 'n':
7153 scrub_restart = B_FALSE;
7154 break;
5853fe79
GW
7155 case '?':
7156 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7157 optopt);
7158 usage(B_FALSE);
7159 }
7160 }
7161
d3f2cd7e
AB
7162 argc -= optind;
7163 argv += optind;
1bd201e7 7164
d3f2cd7e 7165 /* if argc == 0 we will execute zpool_reopen_one on all pools */
2a673e76
AJ
7166 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7167 B_FALSE, zpool_reopen_one, &scrub_restart);
1bd201e7 7168
1bd201e7
CS
7169 return (ret);
7170}
7171
34dc7c2f
BB
7172typedef struct scrub_cbdata {
7173 int cb_type;
0ea05c64 7174 pool_scrub_cmd_t cb_scrub_cmd;
34dc7c2f
BB
7175} scrub_cbdata_t;
7176
d2734cce
SD
7177static boolean_t
7178zpool_has_checkpoint(zpool_handle_t *zhp)
7179{
7180 nvlist_t *config, *nvroot;
7181
7182 config = zpool_get_config(zhp, NULL);
7183
7184 if (config != NULL) {
7185 pool_checkpoint_stat_t *pcs = NULL;
7186 uint_t c;
7187
7188 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
7189 (void) nvlist_lookup_uint64_array(nvroot,
7190 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7191
7192 if (pcs == NULL || pcs->pcs_state == CS_NONE)
7193 return (B_FALSE);
7194
7195 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
7196 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
7197 return (B_TRUE);
7198 }
7199
7200 return (B_FALSE);
7201}
7202
65c7cc49 7203static int
34dc7c2f
BB
7204scrub_callback(zpool_handle_t *zhp, void *data)
7205{
7206 scrub_cbdata_t *cb = data;
7207 int err;
7208
7209 /*
7210 * Ignore faulted pools.
7211 */
7212 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
80a91e74 7213 (void) fprintf(stderr, gettext("cannot scan '%s': pool is "
34dc7c2f
BB
7214 "currently unavailable\n"), zpool_get_name(zhp));
7215 return (1);
7216 }
7217
0ea05c64 7218 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
34dc7c2f 7219
d2734cce
SD
7220 if (err == 0 && zpool_has_checkpoint(zhp) &&
7221 cb->cb_type == POOL_SCAN_SCRUB) {
7222 (void) printf(gettext("warning: will not scrub state that "
7223 "belongs to the checkpoint of pool '%s'\n"),
7224 zpool_get_name(zhp));
7225 }
7226
34dc7c2f
BB
7227 return (err != 0);
7228}
7229
e60e158e
JG
7230static int
7231wait_callback(zpool_handle_t *zhp, void *data)
7232{
7233 zpool_wait_activity_t *act = data;
7234 return (zpool_wait(zhp, *act));
7235}
7236
34dc7c2f 7237/*
e60e158e 7238 * zpool scrub [-s | -p] [-w] <pool> ...
34dc7c2f
BB
7239 *
7240 * -s Stop. Stops any in-progress scrub.
0ea05c64 7241 * -p Pause. Pause in-progress scrub.
e60e158e 7242 * -w Wait. Blocks until scrub has completed.
34dc7c2f
BB
7243 */
7244int
7245zpool_do_scrub(int argc, char **argv)
7246{
7247 int c;
7248 scrub_cbdata_t cb;
e60e158e
JG
7249 boolean_t wait = B_FALSE;
7250 int error;
34dc7c2f 7251
428870ff 7252 cb.cb_type = POOL_SCAN_SCRUB;
0ea05c64 7253 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
34dc7c2f
BB
7254
7255 /* check options */
e60e158e 7256 while ((c = getopt(argc, argv, "spw")) != -1) {
34dc7c2f
BB
7257 switch (c) {
7258 case 's':
428870ff 7259 cb.cb_type = POOL_SCAN_NONE;
34dc7c2f 7260 break;
0ea05c64
AP
7261 case 'p':
7262 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
7263 break;
e60e158e
JG
7264 case 'w':
7265 wait = B_TRUE;
7266 break;
34dc7c2f
BB
7267 case '?':
7268 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7269 optopt);
7270 usage(B_FALSE);
7271 }
7272 }
7273
0ea05c64
AP
7274 if (cb.cb_type == POOL_SCAN_NONE &&
7275 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
7276 (void) fprintf(stderr, gettext("invalid option combination: "
7277 "-s and -p are mutually exclusive\n"));
7278 usage(B_FALSE);
7279 }
7280
e60e158e
JG
7281 if (wait && (cb.cb_type == POOL_SCAN_NONE ||
7282 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) {
7283 (void) fprintf(stderr, gettext("invalid option combination: "
7284 "-w cannot be used with -p or -s\n"));
7285 usage(B_FALSE);
7286 }
7287
34dc7c2f
BB
7288 argc -= optind;
7289 argv += optind;
7290
7291 if (argc < 1) {
7292 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7293 usage(B_FALSE);
7294 }
7295
2a673e76
AJ
7296 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7297 B_FALSE, scrub_callback, &cb);
e60e158e
JG
7298
7299 if (wait && !error) {
7300 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB;
2a673e76
AJ
7301 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7302 B_FALSE, wait_callback, &act);
e60e158e
JG
7303 }
7304
7305 return (error);
34dc7c2f
BB
7306}
7307
80a91e74
TC
7308/*
7309 * zpool resilver <pool> ...
7310 *
7311 * Restarts any in-progress resilver
7312 */
7313int
7314zpool_do_resilver(int argc, char **argv)
7315{
7316 int c;
7317 scrub_cbdata_t cb;
7318
7319 cb.cb_type = POOL_SCAN_RESILVER;
7320 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
80a91e74
TC
7321
7322 /* check options */
7323 while ((c = getopt(argc, argv, "")) != -1) {
7324 switch (c) {
7325 case '?':
7326 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7327 optopt);
7328 usage(B_FALSE);
7329 }
7330 }
7331
7332 argc -= optind;
7333 argv += optind;
7334
7335 if (argc < 1) {
7336 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7337 usage(B_FALSE);
7338 }
7339
2a673e76
AJ
7340 return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7341 B_FALSE, scrub_callback, &cb));
80a91e74
TC
7342}
7343
1b939560
BB
7344/*
7345 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7346 *
7347 * -c Cancel. Ends any in-progress trim.
7348 * -d Secure trim. Requires kernel and device support.
7349 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7350 * adding a multiplier suffix such as 'k' or 'm'.
7351 * -s Suspend. TRIM can then be restarted with no flags.
2288d419 7352 * -w Wait. Blocks until trimming has completed.
1b939560
BB
7353 */
7354int
7355zpool_do_trim(int argc, char **argv)
7356{
7357 struct option long_options[] = {
7358 {"cancel", no_argument, NULL, 'c'},
7359 {"secure", no_argument, NULL, 'd'},
7360 {"rate", required_argument, NULL, 'r'},
7361 {"suspend", no_argument, NULL, 's'},
2288d419 7362 {"wait", no_argument, NULL, 'w'},
1b939560
BB
7363 {0, 0, 0, 0}
7364 };
7365
7366 pool_trim_func_t cmd_type = POOL_TRIM_START;
7367 uint64_t rate = 0;
7368 boolean_t secure = B_FALSE;
2288d419 7369 boolean_t wait = B_FALSE;
1b939560
BB
7370
7371 int c;
2288d419 7372 while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL))
1b939560
BB
7373 != -1) {
7374 switch (c) {
7375 case 'c':
7376 if (cmd_type != POOL_TRIM_START &&
7377 cmd_type != POOL_TRIM_CANCEL) {
7378 (void) fprintf(stderr, gettext("-c cannot be "
7379 "combined with other options\n"));
7380 usage(B_FALSE);
7381 }
7382 cmd_type = POOL_TRIM_CANCEL;
7383 break;
7384 case 'd':
7385 if (cmd_type != POOL_TRIM_START) {
7386 (void) fprintf(stderr, gettext("-d cannot be "
7387 "combined with the -c or -s options\n"));
7388 usage(B_FALSE);
7389 }
7390 secure = B_TRUE;
7391 break;
7392 case 'r':
7393 if (cmd_type != POOL_TRIM_START) {
7394 (void) fprintf(stderr, gettext("-r cannot be "
7395 "combined with the -c or -s options\n"));
7396 usage(B_FALSE);
7397 }
a4cecfbd
TS
7398 if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) {
7399 (void) fprintf(stderr, "%s: %s\n",
7400 gettext("invalid value for rate"),
7401 libzfs_error_description(g_zfs));
1b939560
BB
7402 usage(B_FALSE);
7403 }
7404 break;
7405 case 's':
7406 if (cmd_type != POOL_TRIM_START &&
7407 cmd_type != POOL_TRIM_SUSPEND) {
7408 (void) fprintf(stderr, gettext("-s cannot be "
7409 "combined with other options\n"));
7410 usage(B_FALSE);
7411 }
7412 cmd_type = POOL_TRIM_SUSPEND;
7413 break;
2288d419
BB
7414 case 'w':
7415 wait = B_TRUE;
7416 break;
1b939560
BB
7417 case '?':
7418 if (optopt != 0) {
7419 (void) fprintf(stderr,
7420 gettext("invalid option '%c'\n"), optopt);
7421 } else {
7422 (void) fprintf(stderr,
7423 gettext("invalid option '%s'\n"),
7424 argv[optind - 1]);
7425 }
7426 usage(B_FALSE);
7427 }
7428 }
7429
7430 argc -= optind;
7431 argv += optind;
7432
7433 if (argc < 1) {
7434 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7435 usage(B_FALSE);
7436 return (-1);
7437 }
7438
2288d419
BB
7439 if (wait && (cmd_type != POOL_TRIM_START)) {
7440 (void) fprintf(stderr, gettext("-w cannot be used with -c or "
7441 "-s\n"));
7442 usage(B_FALSE);
7443 }
7444
1b939560
BB
7445 char *poolname = argv[0];
7446 zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
7447 if (zhp == NULL)
7448 return (-1);
7449
7450 trimflags_t trim_flags = {
7451 .secure = secure,
7452 .rate = rate,
2288d419 7453 .wait = wait,
1b939560
BB
7454 };
7455
7456 nvlist_t *vdevs = fnvlist_alloc();
7457 if (argc == 1) {
7458 /* no individual leaf vdevs specified, so add them all */
7459 nvlist_t *config = zpool_get_config(zhp, NULL);
7460 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
7461 ZPOOL_CONFIG_VDEV_TREE);
7462 zpool_collect_leaves(zhp, nvroot, vdevs);
7463 trim_flags.fullpool = B_TRUE;
7464 } else {
7465 trim_flags.fullpool = B_FALSE;
7466 for (int i = 1; i < argc; i++) {
7467 fnvlist_add_boolean(vdevs, argv[i]);
7468 }
7469 }
7470
7471 int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags);
7472
7473 fnvlist_free(vdevs);
7474 zpool_close(zhp);
7475
7476 return (error);
7477}
80a91e74 7478
9a49d3f3
BB
7479/*
7480 * Converts a total number of seconds to a human readable string broken
7481 * down in to days/hours/minutes/seconds.
7482 */
7483static void
7484secs_to_dhms(uint64_t total, char *buf)
7485{
7486 uint64_t days = total / 60 / 60 / 24;
7487 uint64_t hours = (total / 60 / 60) % 24;
7488 uint64_t mins = (total / 60) % 60;
7489 uint64_t secs = (total % 60);
7490
7491 if (days > 0) {
7492 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu",
7493 (u_longlong_t)days, (u_longlong_t)hours,
7494 (u_longlong_t)mins, (u_longlong_t)secs);
7495 } else {
7496 (void) sprintf(buf, "%02llu:%02llu:%02llu",
7497 (u_longlong_t)hours, (u_longlong_t)mins,
7498 (u_longlong_t)secs);
7499 }
7500}
7501
34dc7c2f
BB
7502/*
7503 * Print out detailed scrub status.
7504 */
a1d477c2 7505static void
9a49d3f3 7506print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
34dc7c2f 7507{
0ea05c64 7508 time_t start, end, pause;
d4a72f23 7509 uint64_t pass_scanned, scanned, pass_issued, issued, total;
9a49d3f3 7510 uint64_t elapsed, scan_rate, issue_rate;
34dc7c2f 7511 double fraction_done;
d4a72f23 7512 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
9a49d3f3 7513 char srate_buf[7], irate_buf[7], time_buf[32];
34dc7c2f 7514
9fb2771a
TH
7515 printf(" ");
7516 printf_color(ANSI_BOLD, gettext("scan:"));
7517 printf(" ");
34dc7c2f 7518
428870ff
BB
7519 /* If there's never been a scan, there's not much to say. */
7520 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
7521 ps->pss_func >= POOL_SCAN_FUNCS) {
34dc7c2f
BB
7522 (void) printf(gettext("none requested\n"));
7523 return;
7524 }
7525
428870ff
BB
7526 start = ps->pss_start_time;
7527 end = ps->pss_end_time;
0ea05c64 7528 pause = ps->pss_pass_scrub_pause;
d4a72f23 7529
e7fbeb60 7530 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
34dc7c2f 7531
428870ff
BB
7532 assert(ps->pss_func == POOL_SCAN_SCRUB ||
7533 ps->pss_func == POOL_SCAN_RESILVER);
d4a72f23
TC
7534
7535 /* Scan is finished or canceled. */
428870ff 7536 if (ps->pss_state == DSS_FINISHED) {
9a49d3f3 7537 secs_to_dhms(end - start, time_buf);
428870ff
BB
7538
7539 if (ps->pss_func == POOL_SCAN_SCRUB) {
d4a72f23 7540 (void) printf(gettext("scrub repaired %s "
9a49d3f3
BB
7541 "in %s with %llu errors on %s"), processed_buf,
7542 time_buf, (u_longlong_t)ps->pss_errors,
7543 ctime(&end));
428870ff 7544 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
d4a72f23 7545 (void) printf(gettext("resilvered %s "
9a49d3f3
BB
7546 "in %s with %llu errors on %s"), processed_buf,
7547 time_buf, (u_longlong_t)ps->pss_errors,
7548 ctime(&end));
428870ff 7549 }
428870ff
BB
7550 return;
7551 } else if (ps->pss_state == DSS_CANCELED) {
7552 if (ps->pss_func == POOL_SCAN_SCRUB) {
7553 (void) printf(gettext("scrub canceled on %s"),
7554 ctime(&end));
7555 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
7556 (void) printf(gettext("resilver canceled on %s"),
7557 ctime(&end));
7558 }
34dc7c2f
BB
7559 return;
7560 }
7561
428870ff
BB
7562 assert(ps->pss_state == DSS_SCANNING);
7563
d4a72f23 7564 /* Scan is in progress. Resilvers can't be paused. */
428870ff 7565 if (ps->pss_func == POOL_SCAN_SCRUB) {
0ea05c64
AP
7566 if (pause == 0) {
7567 (void) printf(gettext("scrub in progress since %s"),
7568 ctime(&start));
7569 } else {
d4a72f23
TC
7570 (void) printf(gettext("scrub paused since %s"),
7571 ctime(&pause));
7572 (void) printf(gettext("\tscrub started on %s"),
0ea05c64
AP
7573 ctime(&start));
7574 }
428870ff
BB
7575 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
7576 (void) printf(gettext("resilver in progress since %s"),
7577 ctime(&start));
7578 }
34dc7c2f 7579
d4a72f23
TC
7580 scanned = ps->pss_examined;
7581 pass_scanned = ps->pss_pass_exam;
7582 issued = ps->pss_issued;
7583 pass_issued = ps->pss_pass_issued;
428870ff 7584 total = ps->pss_to_examine;
428870ff 7585
d4a72f23
TC
7586 /* we are only done with a block once we have issued the IO for it */
7587 fraction_done = (double)issued / total;
7588
7589 /* elapsed time for this pass, rounding up to 1 if it's 0 */
428870ff 7590 elapsed = time(NULL) - ps->pss_pass_start;
0ea05c64 7591 elapsed -= ps->pss_pass_scrub_spent_paused;
d4a72f23
TC
7592 elapsed = (elapsed != 0) ? elapsed : 1;
7593
7594 scan_rate = pass_scanned / elapsed;
7595 issue_rate = pass_issued / elapsed;
9a49d3f3 7596 uint64_t total_secs_left = (issue_rate != 0 && total >= issued) ?
d4a72f23 7597 ((total - issued) / issue_rate) : UINT64_MAX;
9a49d3f3 7598 secs_to_dhms(total_secs_left, time_buf);
d4a72f23
TC
7599
7600 /* format all of the numbers we will be reporting */
7601 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
7602 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
e7fbeb60 7603 zfs_nicebytes(total, total_buf, sizeof (total_buf));
d4a72f23
TC
7604 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
7605 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
428870ff 7606
d4a72f23 7607 /* do not print estimated time if we have a paused scrub */
0ea05c64 7608 if (pause == 0) {
d4a72f23
TC
7609 (void) printf(gettext("\t%s scanned at %s/s, "
7610 "%s issued at %s/s, %s total\n"),
7611 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
572e2857 7612 } else {
d4a72f23
TC
7613 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7614 scanned_buf, issued_buf, total_buf);
572e2857 7615 }
428870ff
BB
7616
7617 if (ps->pss_func == POOL_SCAN_RESILVER) {
d4a72f23 7618 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
428870ff
BB
7619 processed_buf, 100 * fraction_done);
7620 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
d4a72f23 7621 (void) printf(gettext("\t%s repaired, %.2f%% done"),
428870ff
BB
7622 processed_buf, 100 * fraction_done);
7623 }
d4a72f23
TC
7624
7625 if (pause == 0) {
85bdc684
TC
7626 if (total_secs_left != UINT64_MAX &&
7627 issue_rate >= 10 * 1024 * 1024) {
9a49d3f3
BB
7628 (void) printf(gettext(", %s to go\n"), time_buf);
7629 } else {
7630 (void) printf(gettext(", no estimated "
7631 "completion time\n"));
7632 }
7633 } else {
7634 (void) printf(gettext("\n"));
7635 }
7636}
7637
7638static void
7639print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
7640{
7641 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE)
7642 return;
7643
7644 printf(" ");
7645 printf_color(ANSI_BOLD, gettext("scan:"));
7646 printf(" ");
7647
7648 uint64_t bytes_scanned = vrs->vrs_bytes_scanned;
7649 uint64_t bytes_issued = vrs->vrs_bytes_issued;
7650 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt;
7651 uint64_t bytes_est = vrs->vrs_bytes_est;
7652 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned /
7653 (vrs->vrs_pass_time_ms + 1)) * 1000;
7654 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued /
7655 (vrs->vrs_pass_time_ms + 1)) * 1000;
7656 double scan_pct = MIN((double)bytes_scanned * 100 /
7657 (bytes_est + 1), 100);
7658
7659 /* Format all of the numbers we will be reporting */
7660 char bytes_scanned_buf[7], bytes_issued_buf[7];
7661 char bytes_rebuilt_buf[7], bytes_est_buf[7];
7662 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32];
7663 zfs_nicebytes(bytes_scanned, bytes_scanned_buf,
7664 sizeof (bytes_scanned_buf));
7665 zfs_nicebytes(bytes_issued, bytes_issued_buf,
7666 sizeof (bytes_issued_buf));
7667 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf,
7668 sizeof (bytes_rebuilt_buf));
7669 zfs_nicebytes(bytes_est, bytes_est_buf, sizeof (bytes_est_buf));
7670 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf));
7671 zfs_nicebytes(issue_rate, issue_rate_buf, sizeof (issue_rate_buf));
7672
7673 time_t start = vrs->vrs_start_time;
7674 time_t end = vrs->vrs_end_time;
7675
7676 /* Rebuild is finished or canceled. */
7677 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) {
7678 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf);
7679 (void) printf(gettext("resilvered (%s) %s in %s "
7680 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf,
7681 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end));
7682 return;
7683 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) {
7684 (void) printf(gettext("resilver (%s) canceled on %s"),
7685 vdev_name, ctime(&end));
7686 return;
7687 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7688 (void) printf(gettext("resilver (%s) in progress since %s"),
7689 vdev_name, ctime(&start));
7690 }
7691
7692 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE);
7693
7694 secs_to_dhms(MAX((int64_t)bytes_est - (int64_t)bytes_scanned, 0) /
7695 MAX(scan_rate, 1), time_buf);
7696
7697 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7698 "%s total\n"), bytes_scanned_buf, scan_rate_buf,
7699 bytes_issued_buf, issue_rate_buf, bytes_est_buf);
7700 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7701 bytes_rebuilt_buf, scan_pct);
7702
7703 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7704 if (scan_rate >= 10 * 1024 * 1024) {
7705 (void) printf(gettext(", %s to go\n"), time_buf);
d4a72f23
TC
7706 } else {
7707 (void) printf(gettext(", no estimated "
7708 "completion time\n"));
7709 }
7710 } else {
7711 (void) printf(gettext("\n"));
7712 }
34dc7c2f
BB
7713}
7714
d2734cce 7715/*
9a49d3f3
BB
7716 * Print rebuild status for top-level vdevs.
7717 */
7718static void
7719print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot)
7720{
7721 nvlist_t **child;
7722 uint_t children;
7723
7724 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7725 &child, &children) != 0)
7726 children = 0;
7727
7728 for (uint_t c = 0; c < children; c++) {
7729 vdev_rebuild_stat_t *vrs;
7730 uint_t i;
7731
7732 if (nvlist_lookup_uint64_array(child[c],
7733 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
7734 char *name = zpool_vdev_name(g_zfs, zhp,
7735 child[c], VDEV_NAME_TYPE_ID);
7736 print_rebuild_status_impl(vrs, name);
7737 free(name);
7738 }
7739 }
7740}
7741
7742/*
7743 * As we don't scrub checkpointed blocks, we want to warn the user that we
7744 * skipped scanning some blocks if a checkpoint exists or existed at any
7745 * time during the scan. If a sequential instead of healing reconstruction
7746 * was performed then the blocks were reconstructed. However, their checksums
7747 * have not been verified so we still print the warning.
d2734cce
SD
7748 */
7749static void
7750print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
7751{
7752 if (ps == NULL || pcs == NULL)
7753 return;
7754
7755 if (pcs->pcs_state == CS_NONE ||
7756 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
7757 return;
7758
7759 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
7760
7761 if (ps->pss_state == DSS_NONE)
7762 return;
7763
7764 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
7765 ps->pss_end_time < pcs->pcs_start_time)
7766 return;
7767
7768 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
7769 (void) printf(gettext(" scan warning: skipped blocks "
7770 "that are only referenced by the checkpoint.\n"));
7771 } else {
7772 assert(ps->pss_state == DSS_SCANNING);
7773 (void) printf(gettext(" scan warning: skipping blocks "
7774 "that are only referenced by the checkpoint.\n"));
7775 }
7776}
7777
9a49d3f3
BB
7778/*
7779 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7780 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7781 * the last completed (or cancelled) rebuild.
7782 */
7783static boolean_t
7784check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time)
7785{
7786 nvlist_t **child;
7787 uint_t children;
7788 boolean_t rebuilding = B_FALSE;
7789 uint64_t end_time = 0;
7790
7791 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7792 &child, &children) != 0)
7793 children = 0;
7794
7795 for (uint_t c = 0; c < children; c++) {
7796 vdev_rebuild_stat_t *vrs;
7797 uint_t i;
7798
7799 if (nvlist_lookup_uint64_array(child[c],
7800 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
7801
7802 if (vrs->vrs_end_time > end_time)
7803 end_time = vrs->vrs_end_time;
7804
7805 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7806 rebuilding = B_TRUE;
7807 end_time = 0;
7808 break;
7809 }
7810 }
7811 }
7812
7813 if (rebuild_end_time != NULL)
7814 *rebuild_end_time = end_time;
7815
7816 return (rebuilding);
7817}
7818
7819/*
7820 * Print the scan status.
7821 */
7822static void
7823print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot)
7824{
7825 uint64_t rebuild_end_time = 0, resilver_end_time = 0;
7826 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE;
7827 boolean_t active_resilver = B_FALSE;
7828 pool_checkpoint_stat_t *pcs = NULL;
7829 pool_scan_stat_t *ps = NULL;
7830 uint_t c;
7831
7832 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
7833 (uint64_t **)&ps, &c) == 0) {
7834 if (ps->pss_func == POOL_SCAN_RESILVER) {
7835 resilver_end_time = ps->pss_end_time;
7836 active_resilver = (ps->pss_state == DSS_SCANNING);
7837 }
7838
7839 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER);
7840 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB);
7841 }
7842
7843 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time);
7844 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0));
7845
7846 /* Always print the scrub status when available. */
7847 if (have_scrub)
7848 print_scan_scrub_resilver_status(ps);
7849
7850 /*
7851 * When there is an active resilver or rebuild print its status.
7852 * Otherwise print the status of the last resilver or rebuild.
7853 */
7854 if (active_resilver || (!active_rebuild && have_resilver &&
7855 resilver_end_time && resilver_end_time > rebuild_end_time)) {
7856 print_scan_scrub_resilver_status(ps);
7857 } else if (active_rebuild || (!active_resilver && have_rebuild &&
7858 rebuild_end_time && rebuild_end_time > resilver_end_time)) {
7859 print_rebuild_status(zhp, nvroot);
7860 }
7861
7862 (void) nvlist_lookup_uint64_array(nvroot,
7863 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7864 print_checkpoint_scan_warning(ps, pcs);
7865}
7866
a1d477c2
MA
7867/*
7868 * Print out detailed removal status.
7869 */
7870static void
7871print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
7872{
7873 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
7874 time_t start, end;
7875 nvlist_t *config, *nvroot;
7876 nvlist_t **child;
7877 uint_t children;
7878 char *vdev_name;
7879
7880 if (prs == NULL || prs->prs_state == DSS_NONE)
7881 return;
7882
7883 /*
7884 * Determine name of vdev.
7885 */
7886 config = zpool_get_config(zhp, NULL);
7887 nvroot = fnvlist_lookup_nvlist(config,
7888 ZPOOL_CONFIG_VDEV_TREE);
7889 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7890 &child, &children) == 0);
7891 assert(prs->prs_removing_vdev < children);
7892 vdev_name = zpool_vdev_name(g_zfs, zhp,
7893 child[prs->prs_removing_vdev], B_TRUE);
7894
95a78a03 7895 printf_color(ANSI_BOLD, gettext("remove: "));
a1d477c2
MA
7896
7897 start = prs->prs_start_time;
7898 end = prs->prs_end_time;
7899 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
7900
7901 /*
7902 * Removal is finished or canceled.
7903 */
7904 if (prs->prs_state == DSS_FINISHED) {
7905 uint64_t minutes_taken = (end - start) / 60;
7906
7907 (void) printf(gettext("Removal of vdev %llu copied %s "
7908 "in %lluh%um, completed on %s"),
7909 (longlong_t)prs->prs_removing_vdev,
7910 copied_buf,
7911 (u_longlong_t)(minutes_taken / 60),
7912 (uint_t)(minutes_taken % 60),
7913 ctime((time_t *)&end));
7914 } else if (prs->prs_state == DSS_CANCELED) {
7915 (void) printf(gettext("Removal of %s canceled on %s"),
7916 vdev_name, ctime(&end));
7917 } else {
7918 uint64_t copied, total, elapsed, mins_left, hours_left;
7919 double fraction_done;
7920 uint_t rate;
7921
7922 assert(prs->prs_state == DSS_SCANNING);
7923
7924 /*
7925 * Removal is in progress.
7926 */
7927 (void) printf(gettext(
7928 "Evacuation of %s in progress since %s"),
7929 vdev_name, ctime(&start));
7930
7931 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
7932 total = prs->prs_to_copy;
7933 fraction_done = (double)copied / total;
7934
7935 /* elapsed time for this pass */
7936 elapsed = time(NULL) - prs->prs_start_time;
7937 elapsed = elapsed > 0 ? elapsed : 1;
7938 rate = copied / elapsed;
7939 rate = rate > 0 ? rate : 1;
7940 mins_left = ((total - copied) / rate) / 60;
7941 hours_left = mins_left / 60;
7942
7943 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
7944 zfs_nicenum(total, total_buf, sizeof (total_buf));
7945 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
7946
7947 /*
7948 * do not print estimated time if hours_left is more than
7949 * 30 days
7950 */
fd1c366f
TL
7951 (void) printf(gettext(
7952 "\t%s copied out of %s at %s/s, %.2f%% done"),
a1d477c2
MA
7953 examined_buf, total_buf, rate_buf, 100 * fraction_done);
7954 if (hours_left < (30 * 24)) {
7955 (void) printf(gettext(", %lluh%um to go\n"),
7956 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
7957 } else {
7958 (void) printf(gettext(
7959 ", (copy is slow, no estimated time)\n"));
7960 }
7961 }
75c62019 7962 free(vdev_name);
a1d477c2
MA
7963
7964 if (prs->prs_mapping_memory > 0) {
7965 char mem_buf[7];
7966 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
fd1c366f
TL
7967 (void) printf(gettext(
7968 "\t%s memory used for removed device mappings\n"),
a1d477c2
MA
7969 mem_buf);
7970 }
7971}
7972
d2734cce
SD
7973static void
7974print_checkpoint_status(pool_checkpoint_stat_t *pcs)
7975{
7976 time_t start;
7977 char space_buf[7];
7978
7979 if (pcs == NULL || pcs->pcs_state == CS_NONE)
7980 return;
7981
7982 (void) printf(gettext("checkpoint: "));
7983
7984 start = pcs->pcs_start_time;
7985 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
7986
7987 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
7988 char *date = ctime(&start);
7989
7990 /*
7991 * ctime() adds a newline at the end of the generated
7992 * string, thus the weird format specifier and the
7993 * strlen() call used to chop it off from the output.
7994 */
7995 (void) printf(gettext("created %.*s, consumes %s\n"),
7996 (int)(strlen(date) - 1), date, space_buf);
7997 return;
7998 }
7999
8000 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
8001
8002 (void) printf(gettext("discarding, %s remaining.\n"),
8003 space_buf);
8004}
8005
34dc7c2f
BB
8006static void
8007print_error_log(zpool_handle_t *zhp)
8008{
8009 nvlist_t *nverrlist = NULL;
8010 nvpair_t *elem;
8011 char *pathname;
8012 size_t len = MAXPATHLEN * 2;
8013
56a6054d 8014 if (zpool_get_errlog(zhp, &nverrlist) != 0)
34dc7c2f 8015 return;
34dc7c2f
BB
8016
8017 (void) printf("errors: Permanent errors have been "
8018 "detected in the following files:\n\n");
8019
8020 pathname = safe_malloc(len);
8021 elem = NULL;
8022 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
8023 nvlist_t *nv;
8024 uint64_t dsobj, obj;
8025
8026 verify(nvpair_value_nvlist(elem, &nv) == 0);
8027 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
8028 &dsobj) == 0);
8029 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
8030 &obj) == 0);
8031 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
8032 (void) printf("%7s %s\n", "", pathname);
8033 }
8034 free(pathname);
8035 nvlist_free(nverrlist);
8036}
8037
8038static void
fea33e4e
HJ
8039print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
8040 uint_t nspares)
34dc7c2f
BB
8041{
8042 uint_t i;
8043 char *name;
8044
8045 if (nspares == 0)
8046 return;
8047
8048 (void) printf(gettext("\tspares\n"));
8049
8050 for (i = 0; i < nspares; i++) {
fea33e4e
HJ
8051 name = zpool_vdev_name(g_zfs, zhp, spares[i],
8052 cb->cb_name_flags);
9a49d3f3 8053 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL);
34dc7c2f
BB
8054 free(name);
8055 }
8056}
8057
8058static void
fea33e4e
HJ
8059print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
8060 uint_t nl2cache)
34dc7c2f
BB
8061{
8062 uint_t i;
8063 char *name;
8064
8065 if (nl2cache == 0)
8066 return;
8067
8068 (void) printf(gettext("\tcache\n"));
8069
8070 for (i = 0; i < nl2cache; i++) {
fea33e4e
HJ
8071 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
8072 cb->cb_name_flags);
9a49d3f3
BB
8073 print_status_config(zhp, cb, name, l2cache[i], 2,
8074 B_FALSE, NULL);
34dc7c2f
BB
8075 free(name);
8076 }
8077}
8078
428870ff
BB
8079static void
8080print_dedup_stats(nvlist_t *config)
8081{
8082 ddt_histogram_t *ddh;
8083 ddt_stat_t *dds;
8084 ddt_object_t *ddo;
8085 uint_t c;
e7fbeb60 8086 char dspace[6], mspace[6];
428870ff
BB
8087
8088 /*
8089 * If the pool was faulted then we may not have been able to
32a9872b 8090 * obtain the config. Otherwise, if we have anything in the dedup
428870ff
BB
8091 * table continue processing the stats.
8092 */
8093 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
24024589 8094 (uint64_t **)&ddo, &c) != 0)
428870ff
BB
8095 return;
8096
8097 (void) printf("\n");
24024589
YP
8098 (void) printf(gettext(" dedup: "));
8099 if (ddo->ddo_count == 0) {
8100 (void) printf(gettext("no DDT entries\n"));
8101 return;
8102 }
8103
e7fbeb60 8104 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
8105 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
8106 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
428870ff 8107 (u_longlong_t)ddo->ddo_count,
e7fbeb60 8108 dspace,
8109 mspace);
428870ff
BB
8110
8111 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
8112 (uint64_t **)&dds, &c) == 0);
8113 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
8114 (uint64_t **)&ddh, &c) == 0);
8115 zpool_dump_ddt(dds, ddh);
8116}
8117
34dc7c2f
BB
8118/*
8119 * Display a summary of pool status. Displays a summary such as:
8120 *
8121 * pool: tank
8122 * status: DEGRADED
8123 * reason: One or more devices ...
a2f944a1 8124 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
34dc7c2f
BB
8125 * config:
8126 * mirror DEGRADED
8127 * c1t0d0 OK
8128 * c2t0d0 UNAVAIL
8129 *
8130 * When given the '-v' option, we print out the complete config. If the '-e'
8131 * option is specified, then we print out error rate information as well.
8132 */
65c7cc49 8133static int
34dc7c2f
BB
8134status_callback(zpool_handle_t *zhp, void *data)
8135{
8136 status_cbdata_t *cbp = data;
8137 nvlist_t *config, *nvroot;
8138 char *msgid;
731782ec 8139 zpool_status_t reason;
ffe9d382 8140 zpool_errata_t errata;
34dc7c2f
BB
8141 const char *health;
8142 uint_t c;
8143 vdev_stat_t *vs;
8144
8145 config = zpool_get_config(zhp, NULL);
ffe9d382 8146 reason = zpool_get_status(zhp, &msgid, &errata);
34dc7c2f
BB
8147
8148 cbp->cb_count++;
8149
8150 /*
8151 * If we were given 'zpool status -x', only report those pools with
8152 * problems.
8153 */
c5b247f3
TC
8154 if (cbp->cb_explain &&
8155 (reason == ZPOOL_STATUS_OK ||
8156 reason == ZPOOL_STATUS_VERSION_OLDER ||
658fb802 8157 reason == ZPOOL_STATUS_FEAT_DISABLED ||
e086db16
CB
8158 reason == ZPOOL_STATUS_COMPATIBILITY_ERR ||
8159 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) {
34dc7c2f
BB
8160 if (!cbp->cb_allpools) {
8161 (void) printf(gettext("pool '%s' is healthy\n"),
8162 zpool_get_name(zhp));
8163 if (cbp->cb_first)
8164 cbp->cb_first = B_FALSE;
8165 }
8166 return (0);
8167 }
8168
8169 if (cbp->cb_first)
8170 cbp->cb_first = B_FALSE;
8171 else
8172 (void) printf("\n");
8173
a1d477c2 8174 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
428870ff 8175 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f 8176 (uint64_t **)&vs, &c) == 0);
f0ed6c74
TH
8177
8178 health = zpool_get_state_str(zhp);
34dc7c2f 8179
9fb2771a
TH
8180 printf(" ");
8181 printf_color(ANSI_BOLD, gettext("pool:"));
8182 printf(" %s\n", zpool_get_name(zhp));
8183 printf(" ");
8184 printf_color(ANSI_BOLD, gettext("state: "));
8185
8186 printf_color(health_str_to_color(health), "%s", health);
8187
8188 printf("\n");
34dc7c2f
BB
8189
8190 switch (reason) {
8191 case ZPOOL_STATUS_MISSING_DEV_R:
9fb2771a
TH
8192 printf_color(ANSI_BOLD, gettext("status: "));
8193 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8194 "not be opened. Sufficient replicas exist for\n\tthe pool "
8195 "to continue functioning in a degraded state.\n"));
8196 printf_color(ANSI_BOLD, gettext("action: "));
8197 printf_color(ANSI_YELLOW, gettext("Attach the missing device "
8198 "and online it using 'zpool online'.\n"));
34dc7c2f
BB
8199 break;
8200
8201 case ZPOOL_STATUS_MISSING_DEV_NR:
9fb2771a
TH
8202 printf_color(ANSI_BOLD, gettext("status: "));
8203 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8204 "not be opened. There are insufficient\n\treplicas for the"
8205 " pool to continue functioning.\n"));
8206 printf_color(ANSI_BOLD, gettext("action: "));
8207 printf_color(ANSI_YELLOW, gettext("Attach the missing device "
8208 "and online it using 'zpool online'.\n"));
34dc7c2f
BB
8209 break;
8210
8211 case ZPOOL_STATUS_CORRUPT_LABEL_R:
9fb2771a
TH
8212 printf_color(ANSI_BOLD, gettext("status: "));
8213 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8214 "not be used because the label is missing or\n\tinvalid. "
34dc7c2f
BB
8215 "Sufficient replicas exist for the pool to continue\n\t"
8216 "functioning in a degraded state.\n"));
9fb2771a
TH
8217 printf_color(ANSI_BOLD, gettext("action: "));
8218 printf_color(ANSI_YELLOW, gettext("Replace the device using "
34dc7c2f
BB
8219 "'zpool replace'.\n"));
8220 break;
8221
8222 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
9fb2771a
TH
8223 printf_color(ANSI_BOLD, gettext("status: "));
8224 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8225 "not be used because the label is missing \n\tor invalid. "
34dc7c2f
BB
8226 "There are insufficient replicas for the pool to "
8227 "continue\n\tfunctioning.\n"));
428870ff
BB
8228 zpool_explain_recover(zpool_get_handle(zhp),
8229 zpool_get_name(zhp), reason, config);
34dc7c2f
BB
8230 break;
8231
8232 case ZPOOL_STATUS_FAILING_DEV:
9fb2771a
TH
8233 printf_color(ANSI_BOLD, gettext("status: "));
8234 printf_color(ANSI_YELLOW, gettext("One or more devices has "
34dc7c2f
BB
8235 "experienced an unrecoverable error. An\n\tattempt was "
8236 "made to correct the error. Applications are "
8237 "unaffected.\n"));
9fb2771a
TH
8238 printf_color(ANSI_BOLD, gettext("action: "));
8239 printf_color(ANSI_YELLOW, gettext("Determine if the "
8240 "device needs to be replaced, and clear the errors\n\tusing"
8241 " 'zpool clear' or replace the device with 'zpool "
34dc7c2f
BB
8242 "replace'.\n"));
8243 break;
8244
8245 case ZPOOL_STATUS_OFFLINE_DEV:
9fb2771a
TH
8246 printf_color(ANSI_BOLD, gettext("status: "));
8247 printf_color(ANSI_YELLOW, gettext("One or more devices has "
34dc7c2f
BB
8248 "been taken offline by the administrator.\n\tSufficient "
8249 "replicas exist for the pool to continue functioning in "
8250 "a\n\tdegraded state.\n"));
9fb2771a
TH
8251 printf_color(ANSI_BOLD, gettext("action: "));
8252 printf_color(ANSI_YELLOW, gettext("Online the device "
8253 "using 'zpool online' or replace the device with\n\t'zpool "
34dc7c2f
BB
8254 "replace'.\n"));
8255 break;
8256
45d1cae3 8257 case ZPOOL_STATUS_REMOVED_DEV:
9fb2771a
TH
8258 printf_color(ANSI_BOLD, gettext("status: "));
8259 printf_color(ANSI_YELLOW, gettext("One or more devices has "
45d1cae3
BB
8260 "been removed by the administrator.\n\tSufficient "
8261 "replicas exist for the pool to continue functioning in "
8262 "a\n\tdegraded state.\n"));
9fb2771a
TH
8263 printf_color(ANSI_BOLD, gettext("action: "));
8264 printf_color(ANSI_YELLOW, gettext("Online the device "
8265 "using zpool online' or replace the device with\n\t'zpool "
45d1cae3
BB
8266 "replace'.\n"));
8267 break;
8268
34dc7c2f 8269 case ZPOOL_STATUS_RESILVERING:
9a49d3f3 8270 case ZPOOL_STATUS_REBUILDING:
9fb2771a
TH
8271 printf_color(ANSI_BOLD, gettext("status: "));
8272 printf_color(ANSI_YELLOW, gettext("One or more devices is "
34dc7c2f
BB
8273 "currently being resilvered. The pool will\n\tcontinue "
8274 "to function, possibly in a degraded state.\n"));
9fb2771a
TH
8275 printf_color(ANSI_BOLD, gettext("action: "));
8276 printf_color(ANSI_YELLOW, gettext("Wait for the resilver to "
34dc7c2f
BB
8277 "complete.\n"));
8278 break;
8279
9a49d3f3
BB
8280 case ZPOOL_STATUS_REBUILD_SCRUB:
8281 printf_color(ANSI_BOLD, gettext("status: "));
8282 printf_color(ANSI_YELLOW, gettext("One or more devices have "
8283 "been sequentially resilvered, scrubbing\n\tthe pool "
8284 "is recommended.\n"));
8285 printf_color(ANSI_BOLD, gettext("action: "));
8286 printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to "
8287 "verify all data checksums.\n"));
8288 break;
8289
34dc7c2f 8290 case ZPOOL_STATUS_CORRUPT_DATA:
9fb2771a
TH
8291 printf_color(ANSI_BOLD, gettext("status: "));
8292 printf_color(ANSI_YELLOW, gettext("One or more devices has "
34dc7c2f
BB
8293 "experienced an error resulting in data\n\tcorruption. "
8294 "Applications may be affected.\n"));
9fb2771a
TH
8295 printf_color(ANSI_BOLD, gettext("action: "));
8296 printf_color(ANSI_YELLOW, gettext("Restore the file in question"
8297 " if possible. Otherwise restore the\n\tentire pool from "
34dc7c2f
BB
8298 "backup.\n"));
8299 break;
8300
8301 case ZPOOL_STATUS_CORRUPT_POOL:
9fb2771a
TH
8302 printf_color(ANSI_BOLD, gettext("status: "));
8303 printf_color(ANSI_YELLOW, gettext("The pool metadata is "
8304 "corrupted and the pool cannot be opened.\n"));
428870ff
BB
8305 zpool_explain_recover(zpool_get_handle(zhp),
8306 zpool_get_name(zhp), reason, config);
34dc7c2f
BB
8307 break;
8308
8309 case ZPOOL_STATUS_VERSION_OLDER:
9fb2771a
TH
8310 printf_color(ANSI_BOLD, gettext("status: "));
8311 printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
8312 "a legacy on-disk format. The pool can\n\tstill be used, "
b9b24bb4 8313 "but some features are unavailable.\n"));
9fb2771a
TH
8314 printf_color(ANSI_BOLD, gettext("action: "));
8315 printf_color(ANSI_YELLOW, gettext("Upgrade the pool using "
8316 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8317 "longer be accessible on software that does not support\n\t"
f52b31ea 8318 "feature flags.\n"));
34dc7c2f
BB
8319 break;
8320
8321 case ZPOOL_STATUS_VERSION_NEWER:
9fb2771a
TH
8322 printf_color(ANSI_BOLD, gettext("status: "));
8323 printf_color(ANSI_YELLOW, gettext("The pool has been upgraded "
8324 "to a newer, incompatible on-disk version.\n\tThe pool "
8325 "cannot be accessed on this system.\n"));
8326 printf_color(ANSI_BOLD, gettext("action: "));
8327 printf_color(ANSI_YELLOW, gettext("Access the pool from a "
8328 "system running more recent software, or\n\trestore the "
8329 "pool from backup.\n"));
34dc7c2f
BB
8330 break;
8331
b9b24bb4 8332 case ZPOOL_STATUS_FEAT_DISABLED:
9fb2771a 8333 printf_color(ANSI_BOLD, gettext("status: "));
658fb802
CB
8334 printf_color(ANSI_YELLOW, gettext("Some supported and "
8335 "requested features are not enabled on the pool.\n\t"
8336 "The pool can still be used, but some features are "
8337 "unavailable.\n"));
9fb2771a
TH
8338 printf_color(ANSI_BOLD, gettext("action: "));
8339 printf_color(ANSI_YELLOW, gettext("Enable all features using "
b9b24bb4
CS
8340 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8341 "longer be accessible by software that does not support\n\t"
2badb345 8342 "the features. See zpool-features(7) for details.\n"));
b9b24bb4
CS
8343 break;
8344
658fb802
CB
8345 case ZPOOL_STATUS_COMPATIBILITY_ERR:
8346 printf_color(ANSI_BOLD, gettext("status: "));
8347 printf_color(ANSI_YELLOW, gettext("This pool has a "
8348 "compatibility list specified, but it could not be\n\t"
8349 "read/parsed at this time. The pool can still be used, "
8350 "but this\n\tshould be investigated.\n"));
8351 printf_color(ANSI_BOLD, gettext("action: "));
8352 printf_color(ANSI_YELLOW, gettext("Check the value of the "
8353 "'compatibility' property against the\n\t"
8354 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or "
8355 ZPOOL_DATA_COMPAT_D ".\n"));
8356 break;
8357
e086db16
CB
8358 case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
8359 printf_color(ANSI_BOLD, gettext("status: "));
8360 printf_color(ANSI_YELLOW, gettext("One or more features "
8361 "are enabled on the pool despite not being\n\t"
8362 "requested by the 'compatibility' property.\n"));
8363 printf_color(ANSI_BOLD, gettext("action: "));
8364 printf_color(ANSI_YELLOW, gettext("Consider setting "
8365 "'compatibility' to an appropriate value, or\n\t"
8366 "adding needed features to the relevant file in\n\t"
8367 ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n"));
8368 break;
8369
9ae529ec 8370 case ZPOOL_STATUS_UNSUP_FEAT_READ:
9fb2771a
TH
8371 printf_color(ANSI_BOLD, gettext("status: "));
8372 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed "
8373 "on this system because it uses the\n\tfollowing feature(s)"
8374 " not supported on this system:\n"));
9ae529ec
CS
8375 zpool_print_unsup_feat(config);
8376 (void) printf("\n");
9fb2771a
TH
8377 printf_color(ANSI_BOLD, gettext("action: "));
8378 printf_color(ANSI_YELLOW, gettext("Access the pool from a "
8379 "system that supports the required feature(s),\n\tor "
8380 "restore the pool from backup.\n"));
9ae529ec
CS
8381 break;
8382
8383 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
9fb2771a
TH
8384 printf_color(ANSI_BOLD, gettext("status: "));
8385 printf_color(ANSI_YELLOW, gettext("The pool can only be "
8386 "accessed in read-only mode on this system. It\n\tcannot be"
8387 " accessed in read-write mode because it uses the "
9ae529ec
CS
8388 "following\n\tfeature(s) not supported on this system:\n"));
8389 zpool_print_unsup_feat(config);
8390 (void) printf("\n");
9fb2771a
TH
8391 printf_color(ANSI_BOLD, gettext("action: "));
8392 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed "
8393 "in read-write mode. Import the pool with\n"
9ae529ec
CS
8394 "\t\"-o readonly=on\", access the pool from a system that "
8395 "supports the\n\trequired feature(s), or restore the "
8396 "pool from backup.\n"));
8397 break;
8398
34dc7c2f 8399 case ZPOOL_STATUS_FAULTED_DEV_R:
9fb2771a
TH
8400 printf_color(ANSI_BOLD, gettext("status: "));
8401 printf_color(ANSI_YELLOW, gettext("One or more devices are "
34dc7c2f
BB
8402 "faulted in response to persistent errors.\n\tSufficient "
8403 "replicas exist for the pool to continue functioning "
8404 "in a\n\tdegraded state.\n"));
9fb2771a
TH
8405 printf_color(ANSI_BOLD, gettext("action: "));
8406 printf_color(ANSI_YELLOW, gettext("Replace the faulted device, "
34dc7c2f
BB
8407 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8408 break;
8409
8410 case ZPOOL_STATUS_FAULTED_DEV_NR:
9fb2771a
TH
8411 printf_color(ANSI_BOLD, gettext("status: "));
8412 printf_color(ANSI_YELLOW, gettext("One or more devices are "
34dc7c2f
BB
8413 "faulted in response to persistent errors. There are "
8414 "insufficient replicas for the pool to\n\tcontinue "
8415 "functioning.\n"));
9fb2771a
TH
8416 printf_color(ANSI_BOLD, gettext("action: "));
8417 printf_color(ANSI_YELLOW, gettext("Destroy and re-create the "
8418 "pool from a backup source. Manually marking the device\n"
34dc7c2f
BB
8419 "\trepaired using 'zpool clear' may allow some data "
8420 "to be recovered.\n"));
8421 break;
8422
cec3a0a1 8423 case ZPOOL_STATUS_IO_FAILURE_MMP:
9fb2771a
TH
8424 printf_color(ANSI_BOLD, gettext("status: "));
8425 printf_color(ANSI_YELLOW, gettext("The pool is suspended "
8426 "because multihost writes failed or were delayed;\n\t"
8427 "another system could import the pool undetected.\n"));
8428 printf_color(ANSI_BOLD, gettext("action: "));
8429 printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices"
8430 " are connected, then reboot your system and\n\timport the "
cec3a0a1
OF
8431 "pool.\n"));
8432 break;
8433
b128c09f
BB
8434 case ZPOOL_STATUS_IO_FAILURE_WAIT:
8435 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
9fb2771a
TH
8436 printf_color(ANSI_BOLD, gettext("status: "));
8437 printf_color(ANSI_YELLOW, gettext("One or more devices are "
b128c09f 8438 "faulted in response to IO failures.\n"));
9fb2771a
TH
8439 printf_color(ANSI_BOLD, gettext("action: "));
8440 printf_color(ANSI_YELLOW, gettext("Make sure the affected "
8441 "devices are connected, then run 'zpool clear'.\n"));
b128c09f
BB
8442 break;
8443
8444 case ZPOOL_STATUS_BAD_LOG:
9fb2771a
TH
8445 printf_color(ANSI_BOLD, gettext("status: "));
8446 printf_color(ANSI_YELLOW, gettext("An intent log record "
b128c09f 8447 "could not be read.\n"
2627e752 8448 "\tWaiting for administrator intervention to fix the "
b128c09f 8449 "faulted pool.\n"));
9fb2771a
TH
8450 printf_color(ANSI_BOLD, gettext("action: "));
8451 printf_color(ANSI_YELLOW, gettext("Either restore the affected "
b128c09f
BB
8452 "device(s) and run 'zpool online',\n"
8453 "\tor ignore the intent log records by running "
8454 "'zpool clear'.\n"));
8455 break;
8456
6fe3498c
RM
8457 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
8458 (void) printf(gettext("status: One or more devices are "
8459 "configured to use a non-native block size.\n"
8460 "\tExpect reduced performance.\n"));
8461 (void) printf(gettext("action: Replace affected devices with "
8462 "devices that support the\n\tconfigured block size, or "
8463 "migrate data to a properly configured\n\tpool.\n"));
8464 break;
8465
1cbae971 8466 case ZPOOL_STATUS_HOSTID_MISMATCH:
9fb2771a
TH
8467 printf_color(ANSI_BOLD, gettext("status: "));
8468 printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid"
8469 " and system hostid on imported pool.\n\tThis pool was "
1cbae971
RY
8470 "previously imported into a system with a different "
8471 "hostid,\n\tand then was verbatim imported into this "
8472 "system.\n"));
9fb2771a
TH
8473 printf_color(ANSI_BOLD, gettext("action: "));
8474 printf_color(ANSI_YELLOW, gettext("Export this pool on all "
8475 "systems on which it is imported.\n"
1cbae971
RY
8476 "\tThen import it to correct the mismatch.\n"));
8477 break;
8478
ffe9d382 8479 case ZPOOL_STATUS_ERRATA:
9fb2771a
TH
8480 printf_color(ANSI_BOLD, gettext("status: "));
8481 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"),
ffe9d382
BB
8482 errata);
8483
8484 switch (errata) {
8485 case ZPOOL_ERRATA_NONE:
8486 break;
8487
4f2dcb3e 8488 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
9fb2771a
TH
8489 printf_color(ANSI_BOLD, gettext("action: "));
8490 printf_color(ANSI_YELLOW, gettext("To correct the issue"
8491 " run 'zpool scrub'.\n"));
4f2dcb3e
RY
8492 break;
8493
ae76f45c
TC
8494 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
8495 (void) printf(gettext("\tExisting encrypted datasets "
8496 "contain an on-disk incompatibility\n\twhich "
8497 "needs to be corrected.\n"));
9fb2771a
TH
8498 printf_color(ANSI_BOLD, gettext("action: "));
8499 printf_color(ANSI_YELLOW, gettext("To correct the issue"
8500 " backup existing encrypted datasets to new\n\t"
ae76f45c
TC
8501 "encrypted datasets and destroy the old ones. "
8502 "'zfs mount -o ro' can\n\tbe used to temporarily "
8503 "mount existing encrypted datasets readonly.\n"));
8504 break;
8505
f00ab3f2 8506 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
e2dddb7e
JS
8507 (void) printf(gettext("\tExisting encrypted snapshots "
8508 "and bookmarks contain an on-disk\n\tincompat"
8509 "ibility. This may cause on-disk corruption if "
8510 "they are used\n\twith 'zfs recv'.\n"));
9fb2771a
TH
8511 printf_color(ANSI_BOLD, gettext("action: "));
8512 printf_color(ANSI_YELLOW, gettext("To correct the"
8513 "issue, enable the bookmark_v2 feature. No "
8514 "additional\n\taction is needed if there are no "
8515 "encrypted snapshots or bookmarks.\n\tIf preserving"
8516 "the encrypted snapshots and bookmarks is required,"
8517 " use\n\ta non-raw send to backup and restore them."
8518 " Alternately, they may be\n\tremoved to resolve "
e2dddb7e 8519 "the incompatibility.\n"));
f00ab3f2
TC
8520 break;
8521
ffe9d382
BB
8522 default:
8523 /*
8524 * All errata which allow the pool to be imported
8525 * must contain an action message.
8526 */
8527 assert(0);
8528 }
8529 break;
8530
34dc7c2f
BB
8531 default:
8532 /*
8533 * The remaining errors can't actually be generated, yet.
8534 */
8535 assert(reason == ZPOOL_STATUS_OK);
8536 }
8537
9fb2771a
TH
8538 if (msgid != NULL) {
8539 printf(" ");
8540 printf_color(ANSI_BOLD, gettext("see:"));
a2f944a1
RM
8541 printf(gettext(
8542 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8543 msgid);
9fb2771a 8544 }
34dc7c2f
BB
8545
8546 if (config != NULL) {
34dc7c2f
BB
8547 uint64_t nerr;
8548 nvlist_t **spares, **l2cache;
8549 uint_t nspares, nl2cache;
d2734cce 8550 pool_checkpoint_stat_t *pcs = NULL;
a1d477c2 8551 pool_removal_stat_t *prs = NULL;
34dc7c2f 8552
9a49d3f3
BB
8553 print_scan_status(zhp, nvroot);
8554
a1d477c2
MA
8555 (void) nvlist_lookup_uint64_array(nvroot,
8556 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
8557 print_removal_status(zhp, prs);
9a49d3f3
BB
8558
8559 (void) nvlist_lookup_uint64_array(nvroot,
8560 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
d2734cce 8561 print_checkpoint_status(pcs);
a1d477c2 8562
fea33e4e 8563 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
5f20c145 8564 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
fea33e4e
HJ
8565 if (cbp->cb_namewidth < 10)
8566 cbp->cb_namewidth = 10;
34dc7c2f 8567
9fb2771a 8568 color_start(ANSI_BOLD);
34dc7c2f 8569 (void) printf(gettext("config:\n\n"));
d6418de0 8570 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
fea33e4e
HJ
8571 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
8572 "CKSUM");
9fb2771a 8573 color_end();
d6418de0 8574
9fb2771a
TH
8575 if (cbp->cb_print_slow_ios) {
8576 printf_color(ANSI_BOLD, " %5s", gettext("SLOW"));
8577 }
ad796b8a 8578
d6418de0
TH
8579 if (cbp->vcdl != NULL)
8580 print_cmd_columns(cbp->vcdl, 0);
8581
8582 printf("\n");
cc99f275 8583
fea33e4e 8584 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
9a49d3f3 8585 B_FALSE, NULL);
34dc7c2f 8586
cc99f275
DB
8587 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
8588 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
8589 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
8590
34dc7c2f
BB
8591 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
8592 &l2cache, &nl2cache) == 0)
fea33e4e 8593 print_l2cache(zhp, cbp, l2cache, nl2cache);
34dc7c2f
BB
8594
8595 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
8596 &spares, &nspares) == 0)
fea33e4e 8597 print_spares(zhp, cbp, spares, nspares);
34dc7c2f
BB
8598
8599 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
8600 &nerr) == 0) {
8601 nvlist_t *nverrlist = NULL;
8602
8603 /*
8604 * If the approximate error count is small, get a
8605 * precise count by fetching the entire log and
8606 * uniquifying the results.
8607 */
8608 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
8609 zpool_get_errlog(zhp, &nverrlist) == 0) {
8610 nvpair_t *elem;
8611
8612 elem = NULL;
8613 nerr = 0;
8614 while ((elem = nvlist_next_nvpair(nverrlist,
8615 elem)) != NULL) {
8616 nerr++;
8617 }
8618 }
8619 nvlist_free(nverrlist);
8620
8621 (void) printf("\n");
8622
8623 if (nerr == 0)
8624 (void) printf(gettext("errors: No known data "
8625 "errors\n"));
8626 else if (!cbp->cb_verbose)
8627 (void) printf(gettext("errors: %llu data "
8628 "errors, use '-v' for a list\n"),
8629 (u_longlong_t)nerr);
8630 else
8631 print_error_log(zhp);
8632 }
428870ff
BB
8633
8634 if (cbp->cb_dedup_stats)
8635 print_dedup_stats(config);
34dc7c2f
BB
8636 } else {
8637 (void) printf(gettext("config: The configuration cannot be "
8638 "determined.\n"));
8639 }
8640
8641 return (0);
8642}
8643
8644/*
1b939560 8645 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
d6418de0 8646 * [interval [count]]
34dc7c2f 8647 *
8720e9e7 8648 * -c CMD For each vdev, run command CMD
a769fb53 8649 * -i Display vdev initialization status.
d2f3e292
RY
8650 * -g Display guid for individual vdev name.
8651 * -L Follow links when resolving vdev path name.
ad796b8a 8652 * -p Display values in parsable (exact) format.
a77f29f9 8653 * -P Display full path for vdev name.
ad796b8a 8654 * -s Display slow IOs column.
34dc7c2f
BB
8655 * -v Display complete error logs
8656 * -x Display only pools with potential problems
428870ff 8657 * -D Display dedup status (undocumented)
1b939560 8658 * -t Display vdev TRIM status.
428870ff 8659 * -T Display a timestamp in date(1) or Unix format
34dc7c2f
BB
8660 *
8661 * Describes the health status of all pools or some subset.
8662 */
8663int
8664zpool_do_status(int argc, char **argv)
8665{
8666 int c;
8667 int ret;
193a37cb
TH
8668 float interval = 0;
8669 unsigned long count = 0;
34dc7c2f 8670 status_cbdata_t cb = { 0 };
8720e9e7 8671 char *cmd = NULL;
34dc7c2f
BB
8672
8673 /* check options */
1b939560 8674 while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) {
34dc7c2f 8675 switch (c) {
8720e9e7 8676 case 'c':
d6418de0
TH
8677 if (cmd != NULL) {
8678 fprintf(stderr,
8679 gettext("Can't set -c flag twice\n"));
8680 exit(1);
8681 }
099700d9
GDN
8682
8683 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
8684 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8685 fprintf(stderr, gettext(
8686 "Can't run -c, disabled by "
8687 "ZPOOL_SCRIPTS_ENABLED.\n"));
8688 exit(1);
8689 }
8690
d6418de0
TH
8691 if ((getuid() <= 0 || geteuid() <= 0) &&
8692 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8693 fprintf(stderr, gettext(
8694 "Can't run -c with root privileges "
8695 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8696 exit(1);
8697 }
8720e9e7
TH
8698 cmd = optarg;
8699 break;
a769fb53
BB
8700 case 'i':
8701 cb.cb_print_vdev_init = B_TRUE;
8702 break;
d2f3e292
RY
8703 case 'g':
8704 cb.cb_name_flags |= VDEV_NAME_GUID;
8705 break;
8706 case 'L':
8707 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
8708 break;
ad796b8a
TH
8709 case 'p':
8710 cb.cb_literal = B_TRUE;
8711 break;
a77f29f9 8712 case 'P':
d2f3e292
RY
8713 cb.cb_name_flags |= VDEV_NAME_PATH;
8714 break;
ad796b8a
TH
8715 case 's':
8716 cb.cb_print_slow_ios = B_TRUE;
8717 break;
34dc7c2f
BB
8718 case 'v':
8719 cb.cb_verbose = B_TRUE;
8720 break;
8721 case 'x':
8722 cb.cb_explain = B_TRUE;
8723 break;
428870ff
BB
8724 case 'D':
8725 cb.cb_dedup_stats = B_TRUE;
8726 break;
1b939560
BB
8727 case 't':
8728 cb.cb_print_vdev_trim = B_TRUE;
8729 break;
428870ff
BB
8730 case 'T':
8731 get_timestamp_arg(*optarg);
8732 break;
34dc7c2f 8733 case '?':
1528bfdb 8734 if (optopt == 'c') {
099700d9 8735 print_zpool_script_list("status");
d6418de0 8736 exit(0);
1528bfdb
TH
8737 } else {
8738 fprintf(stderr,
8739 gettext("invalid option '%c'\n"), optopt);
8740 }
34dc7c2f
BB
8741 usage(B_FALSE);
8742 }
8743 }
8744
8745 argc -= optind;
8746 argv += optind;
8747
428870ff 8748 get_interval_count(&argc, argv, &interval, &count);
34dc7c2f
BB
8749
8750 if (argc == 0)
8751 cb.cb_allpools = B_TRUE;
8752
428870ff 8753 cb.cb_first = B_TRUE;
fea33e4e 8754 cb.cb_print_status = B_TRUE;
34dc7c2f 8755
428870ff
BB
8756 for (;;) {
8757 if (timestamp_fmt != NODATE)
8758 print_timestamp(timestamp_fmt);
34dc7c2f 8759
8720e9e7 8760 if (cmd != NULL)
1528bfdb
TH
8761 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
8762 NULL, NULL, 0, 0);
8720e9e7 8763
2a673e76
AJ
8764 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8765 cb.cb_literal, status_callback, &cb);
428870ff 8766
8720e9e7
TH
8767 if (cb.vcdl != NULL)
8768 free_vdev_cmd_data_list(cb.vcdl);
8769
428870ff 8770 if (argc == 0 && cb.cb_count == 0)
42cb3819 8771 (void) fprintf(stderr, gettext("no pools available\n"));
428870ff
BB
8772 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
8773 (void) printf(gettext("all pools are healthy\n"));
8774
8775 if (ret != 0)
8776 return (ret);
8777
8778 if (interval == 0)
8779 break;
8780
8781 if (count != 0 && --count == 0)
8782 break;
8783
193a37cb 8784 (void) fsleep(interval);
428870ff
BB
8785 }
8786
8787 return (0);
34dc7c2f
BB
8788}
8789
8790typedef struct upgrade_cbdata {
34dc7c2f 8791 int cb_first;
34dc7c2f
BB
8792 int cb_argc;
8793 uint64_t cb_version;
8794 char **cb_argv;
8795} upgrade_cbdata_t;
8796
287be44f
DS
8797static int
8798check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
8799{
02730c33 8800 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
287be44f
DS
8801 int *count = (int *)unsupp_fs;
8802
8803 if (zfs_version > ZPL_VERSION) {
8804 (void) printf(gettext("%s (v%d) is not supported by this "
8805 "implementation of ZFS.\n"),
8806 zfs_get_name(zhp), zfs_version);
8807 (*count)++;
8808 }
8809
399b9819 8810 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
287be44f
DS
8811
8812 zfs_close(zhp);
8813
8814 return (0);
8815}
8816
b9b24bb4
CS
8817static int
8818upgrade_version(zpool_handle_t *zhp, uint64_t version)
8819{
8820 int ret;
8821 nvlist_t *config;
8822 uint64_t oldversion;
287be44f 8823 int unsupp_fs = 0;
b9b24bb4
CS
8824
8825 config = zpool_get_config(zhp, NULL);
8826 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8827 &oldversion) == 0);
8828
e086db16
CB
8829 char compat[ZFS_MAXPROPLEN];
8830 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
8831 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
8832 compat[0] = '\0';
8833
b9b24bb4
CS
8834 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
8835 assert(oldversion < version);
8836
287be44f
DS
8837 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
8838 if (ret != 0)
8839 return (ret);
8840
8841 if (unsupp_fs) {
5c7afad4
BB
8842 (void) fprintf(stderr, gettext("Upgrade not performed due "
8843 "to %d unsupported filesystems (max v%d).\n"),
02730c33 8844 unsupp_fs, (int)ZPL_VERSION);
287be44f
DS
8845 return (1);
8846 }
8847
e086db16
CB
8848 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
8849 (void) fprintf(stderr, gettext("Upgrade not performed because "
8850 "'compatibility' property set to '"
8851 ZPOOL_COMPAT_LEGACY "'.\n"));
8852 return (1);
8853 }
8854
b9b24bb4
CS
8855 ret = zpool_upgrade(zhp, version);
8856 if (ret != 0)
8857 return (ret);
8858
8859 if (version >= SPA_VERSION_FEATURES) {
8860 (void) printf(gettext("Successfully upgraded "
8861 "'%s' from version %llu to feature flags.\n"),
02730c33 8862 zpool_get_name(zhp), (u_longlong_t)oldversion);
b9b24bb4
CS
8863 } else {
8864 (void) printf(gettext("Successfully upgraded "
8865 "'%s' from version %llu to version %llu.\n"),
02730c33
BB
8866 zpool_get_name(zhp), (u_longlong_t)oldversion,
8867 (u_longlong_t)version);
b9b24bb4
CS
8868 }
8869
8870 return (0);
8871}
8872
8873static int
8874upgrade_enable_all(zpool_handle_t *zhp, int *countp)
8875{
8876 int i, ret, count;
8877 boolean_t firstff = B_TRUE;
8878 nvlist_t *enabled = zpool_get_features(zhp);
8879
658fb802
CB
8880 char compat[ZFS_MAXPROPLEN];
8881 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
8882 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
8883 compat[0] = '\0';
8884
8885 boolean_t requested_features[SPA_FEATURES];
8886 if (zpool_do_load_compat(compat, requested_features) !=
8887 ZPOOL_COMPATIBILITY_OK)
8888 return (-1);
8889
b9b24bb4
CS
8890 count = 0;
8891 for (i = 0; i < SPA_FEATURES; i++) {
8892 const char *fname = spa_feature_table[i].fi_uname;
8893 const char *fguid = spa_feature_table[i].fi_guid;
35ec5179
BB
8894
8895 if (!spa_feature_table[i].fi_zfs_mod_supported)
8896 continue;
8897
658fb802 8898 if (!nvlist_exists(enabled, fguid) && requested_features[i]) {
b9b24bb4
CS
8899 char *propname;
8900 verify(-1 != asprintf(&propname, "feature@%s", fname));
8901 ret = zpool_set_prop(zhp, propname,
8902 ZFS_FEATURE_ENABLED);
8903 if (ret != 0) {
8904 free(propname);
8905 return (ret);
8906 }
8907 count++;
8908
8909 if (firstff) {
8910 (void) printf(gettext("Enabled the "
8911 "following features on '%s':\n"),
8912 zpool_get_name(zhp));
8913 firstff = B_FALSE;
8914 }
8915 (void) printf(gettext(" %s\n"), fname);
8916 free(propname);
8917 }
8918 }
8919
8920 if (countp != NULL)
8921 *countp = count;
8922 return (0);
8923}
8924
34dc7c2f
BB
8925static int
8926upgrade_cb(zpool_handle_t *zhp, void *arg)
8927{
8928 upgrade_cbdata_t *cbp = arg;
8929 nvlist_t *config;
8930 uint64_t version;
65d9212a 8931 boolean_t modified_pool = B_FALSE;
b9b24bb4 8932 int ret;
34dc7c2f
BB
8933
8934 config = zpool_get_config(zhp, NULL);
8935 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8936 &version) == 0);
8937
b9b24bb4 8938 assert(SPA_VERSION_IS_SUPPORTED(version));
34dc7c2f 8939
b9b24bb4
CS
8940 if (version < cbp->cb_version) {
8941 cbp->cb_first = B_FALSE;
8942 ret = upgrade_version(zhp, cbp->cb_version);
8943 if (ret != 0)
8944 return (ret);
65d9212a 8945 modified_pool = B_TRUE;
b9b24bb4 8946
b9b24bb4
CS
8947 /*
8948 * If they did "zpool upgrade -a", then we could
8949 * be doing ioctls to different pools. We need
8950 * to log this history once to each pool, and bypass
8951 * the normal history logging that happens in main().
8952 */
8953 (void) zpool_log_history(g_zfs, history_str);
8954 log_history = B_FALSE;
b9b24bb4
CS
8955 }
8956
8957 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
8958 int count;
8959 ret = upgrade_enable_all(zhp, &count);
8960 if (ret != 0)
8961 return (ret);
8962
8963 if (count > 0) {
34dc7c2f 8964 cbp->cb_first = B_FALSE;
65d9212a 8965 modified_pool = B_TRUE;
34dc7c2f 8966 }
b9b24bb4 8967 }
34dc7c2f 8968
65d9212a 8969 if (modified_pool) {
8970 (void) printf("\n");
8971 (void) after_zpool_upgrade(zhp);
b9b24bb4
CS
8972 }
8973
8974 return (0);
8975}
8976
8977static int
8978upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
8979{
8980 upgrade_cbdata_t *cbp = arg;
8981 nvlist_t *config;
8982 uint64_t version;
8983
8984 config = zpool_get_config(zhp, NULL);
8985 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8986 &version) == 0);
8987
8988 assert(SPA_VERSION_IS_SUPPORTED(version));
8989
8990 if (version < SPA_VERSION_FEATURES) {
34dc7c2f
BB
8991 if (cbp->cb_first) {
8992 (void) printf(gettext("The following pools are "
b9b24bb4
CS
8993 "formatted with legacy version numbers and can\n"
8994 "be upgraded to use feature flags. After "
8995 "being upgraded, these pools\nwill no "
8996 "longer be accessible by software that does not "
e086db16
CB
8997 "support feature\nflags.\n\n"
8998 "Note that setting a pool's 'compatibility' "
8999 "feature to '" ZPOOL_COMPAT_LEGACY "' will\n"
9000 "inhibit upgrades.\n\n"));
34dc7c2f
BB
9001 (void) printf(gettext("VER POOL\n"));
9002 (void) printf(gettext("--- ------------\n"));
9003 cbp->cb_first = B_FALSE;
9004 }
9005
9006 (void) printf("%2llu %s\n", (u_longlong_t)version,
9007 zpool_get_name(zhp));
9008 }
9009
b9b24bb4
CS
9010 return (0);
9011}
9012
9013static int
9014upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
9015{
9016 upgrade_cbdata_t *cbp = arg;
9017 nvlist_t *config;
9018 uint64_t version;
9019
9020 config = zpool_get_config(zhp, NULL);
9021 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
9022 &version) == 0);
9023
9024 if (version >= SPA_VERSION_FEATURES) {
9025 int i;
9026 boolean_t poolfirst = B_TRUE;
9027 nvlist_t *enabled = zpool_get_features(zhp);
9028
9029 for (i = 0; i < SPA_FEATURES; i++) {
9030 const char *fguid = spa_feature_table[i].fi_guid;
9031 const char *fname = spa_feature_table[i].fi_uname;
35ec5179
BB
9032
9033 if (!spa_feature_table[i].fi_zfs_mod_supported)
9034 continue;
9035
b9b24bb4
CS
9036 if (!nvlist_exists(enabled, fguid)) {
9037 if (cbp->cb_first) {
9038 (void) printf(gettext("\nSome "
9039 "supported features are not "
9040 "enabled on the following pools. "
9041 "Once a\nfeature is enabled the "
9042 "pool may become incompatible with "
9043 "software\nthat does not support "
9044 "the feature. See "
2badb345 9045 "zpool-features(7) for "
e086db16
CB
9046 "details.\n\n"
9047 "Note that the pool "
9048 "'compatibility' feature can be "
9049 "used to inhibit\nfeature "
9050 "upgrades.\n\n"));
b9b24bb4
CS
9051 (void) printf(gettext("POOL "
9052 "FEATURE\n"));
9053 (void) printf(gettext("------"
9054 "---------\n"));
9055 cbp->cb_first = B_FALSE;
9056 }
9057
9058 if (poolfirst) {
9059 (void) printf(gettext("%s\n"),
9060 zpool_get_name(zhp));
9061 poolfirst = B_FALSE;
9062 }
9063
9064 (void) printf(gettext(" %s\n"), fname);
9065 }
6f1ffb06
MA
9066 /*
9067 * If they did "zpool upgrade -a", then we could
9068 * be doing ioctls to different pools. We need
9069 * to log this history once to each pool, and bypass
9070 * the normal history logging that happens in main().
9071 */
9072 (void) zpool_log_history(g_zfs, history_str);
9073 log_history = B_FALSE;
b9b24bb4
CS
9074 }
9075 }
9076
9077 return (0);
34dc7c2f
BB
9078}
9079
34dc7c2f
BB
9080static int
9081upgrade_one(zpool_handle_t *zhp, void *data)
9082{
65d9212a 9083 boolean_t modified_pool = B_FALSE;
34dc7c2f
BB
9084 upgrade_cbdata_t *cbp = data;
9085 uint64_t cur_version;
9086 int ret;
9087
9088 if (strcmp("log", zpool_get_name(zhp)) == 0) {
c66989ba 9089 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
34dc7c2f
BB
9090 "Pool 'log' must be renamed using export and import"
9091 " to upgrade.\n"));
9092 return (1);
9093 }
9094
9095 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
9096 if (cur_version > cbp->cb_version) {
9097 (void) printf(gettext("Pool '%s' is already formatted "
b9b24bb4 9098 "using more current version '%llu'.\n\n"),
02730c33 9099 zpool_get_name(zhp), (u_longlong_t)cur_version);
34dc7c2f
BB
9100 return (0);
9101 }
b9b24bb4
CS
9102
9103 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
34dc7c2f 9104 (void) printf(gettext("Pool '%s' is already formatted "
b9b24bb4 9105 "using version %llu.\n\n"), zpool_get_name(zhp),
02730c33 9106 (u_longlong_t)cbp->cb_version);
34dc7c2f
BB
9107 return (0);
9108 }
9109
b9b24bb4 9110 if (cur_version != cbp->cb_version) {
65d9212a 9111 modified_pool = B_TRUE;
b9b24bb4
CS
9112 ret = upgrade_version(zhp, cbp->cb_version);
9113 if (ret != 0)
9114 return (ret);
9115 }
34dc7c2f 9116
b9b24bb4
CS
9117 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
9118 int count = 0;
9119 ret = upgrade_enable_all(zhp, &count);
9120 if (ret != 0)
9121 return (ret);
9122
9123 if (count != 0) {
65d9212a 9124 modified_pool = B_TRUE;
b9b24bb4
CS
9125 } else if (cur_version == SPA_VERSION) {
9126 (void) printf(gettext("Pool '%s' already has all "
658fb802 9127 "supported and requested features enabled.\n"),
b9b24bb4
CS
9128 zpool_get_name(zhp));
9129 }
9130 }
9131
65d9212a 9132 if (modified_pool) {
9133 (void) printf("\n");
9134 (void) after_zpool_upgrade(zhp);
34dc7c2f
BB
9135 }
9136
b9b24bb4 9137 return (0);
34dc7c2f
BB
9138}
9139
9140/*
9141 * zpool upgrade
9142 * zpool upgrade -v
9143 * zpool upgrade [-V version] <-a | pool ...>
9144 *
9145 * With no arguments, display downrev'd ZFS pool available for upgrade.
9146 * Individual pools can be upgraded by specifying the pool, and '-a' will
9147 * upgrade all pools.
9148 */
9149int
9150zpool_do_upgrade(int argc, char **argv)
9151{
9152 int c;
9153 upgrade_cbdata_t cb = { 0 };
9154 int ret = 0;
9155 boolean_t showversions = B_FALSE;
b9b24bb4 9156 boolean_t upgradeall = B_FALSE;
34dc7c2f
BB
9157 char *end;
9158
9159
9160 /* check options */
9babb374 9161 while ((c = getopt(argc, argv, ":avV:")) != -1) {
34dc7c2f
BB
9162 switch (c) {
9163 case 'a':
b9b24bb4 9164 upgradeall = B_TRUE;
34dc7c2f
BB
9165 break;
9166 case 'v':
9167 showversions = B_TRUE;
9168 break;
9169 case 'V':
9170 cb.cb_version = strtoll(optarg, &end, 10);
9ae529ec
CS
9171 if (*end != '\0' ||
9172 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
34dc7c2f
BB
9173 (void) fprintf(stderr,
9174 gettext("invalid version '%s'\n"), optarg);
9175 usage(B_FALSE);
9176 }
9177 break;
9babb374
BB
9178 case ':':
9179 (void) fprintf(stderr, gettext("missing argument for "
9180 "'%c' option\n"), optopt);
9181 usage(B_FALSE);
9182 break;
34dc7c2f
BB
9183 case '?':
9184 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9185 optopt);
9186 usage(B_FALSE);
9187 }
9188 }
9189
9190 cb.cb_argc = argc;
9191 cb.cb_argv = argv;
9192 argc -= optind;
9193 argv += optind;
9194
9195 if (cb.cb_version == 0) {
9196 cb.cb_version = SPA_VERSION;
b9b24bb4 9197 } else if (!upgradeall && argc == 0) {
34dc7c2f
BB
9198 (void) fprintf(stderr, gettext("-V option is "
9199 "incompatible with other arguments\n"));
9200 usage(B_FALSE);
9201 }
9202
9203 if (showversions) {
b9b24bb4 9204 if (upgradeall || argc != 0) {
34dc7c2f
BB
9205 (void) fprintf(stderr, gettext("-v option is "
9206 "incompatible with other arguments\n"));
9207 usage(B_FALSE);
9208 }
b9b24bb4 9209 } else if (upgradeall) {
34dc7c2f
BB
9210 if (argc != 0) {
9211 (void) fprintf(stderr, gettext("-a option should not "
9212 "be used along with a pool name\n"));
9213 usage(B_FALSE);
9214 }
9215 }
9216
9ae529ec
CS
9217 (void) printf(gettext("This system supports ZFS pool feature "
9218 "flags.\n\n"));
34dc7c2f 9219 if (showversions) {
b9b24bb4
CS
9220 int i;
9221
9222 (void) printf(gettext("The following features are "
9223 "supported:\n\n"));
9224 (void) printf(gettext("FEAT DESCRIPTION\n"));
9225 (void) printf("----------------------------------------------"
9226 "---------------\n");
9227 for (i = 0; i < SPA_FEATURES; i++) {
9228 zfeature_info_t *fi = &spa_feature_table[i];
03ef8f09
MM
9229 if (!fi->fi_zfs_mod_supported)
9230 continue;
241b5415
MA
9231 const char *ro =
9232 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
b9b24bb4
CS
9233 " (read-only compatible)" : "";
9234
9235 (void) printf("%-37s%s\n", fi->fi_uname, ro);
9236 (void) printf(" %s\n", fi->fi_desc);
9237 }
9238 (void) printf("\n");
9239
9240 (void) printf(gettext("The following legacy versions are also "
34dc7c2f
BB
9241 "supported:\n\n"));
9242 (void) printf(gettext("VER DESCRIPTION\n"));
9243 (void) printf("--- -----------------------------------------"
9244 "---------------\n");
9245 (void) printf(gettext(" 1 Initial ZFS version\n"));
9246 (void) printf(gettext(" 2 Ditto blocks "
9247 "(replicated metadata)\n"));
9248 (void) printf(gettext(" 3 Hot spares and double parity "
9249 "RAID-Z\n"));
9250 (void) printf(gettext(" 4 zpool history\n"));
9251 (void) printf(gettext(" 5 Compression using the gzip "
9252 "algorithm\n"));
9253 (void) printf(gettext(" 6 bootfs pool property\n"));
9254 (void) printf(gettext(" 7 Separate intent log devices\n"));
9255 (void) printf(gettext(" 8 Delegated administration\n"));
9256 (void) printf(gettext(" 9 refquota and refreservation "
9257 "properties\n"));
9258 (void) printf(gettext(" 10 Cache devices\n"));
b128c09f
BB
9259 (void) printf(gettext(" 11 Improved scrub performance\n"));
9260 (void) printf(gettext(" 12 Snapshot properties\n"));
9261 (void) printf(gettext(" 13 snapused property\n"));
9babb374
BB
9262 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9263 (void) printf(gettext(" 15 user/group space accounting\n"));
9264 (void) printf(gettext(" 16 stmf property support\n"));
45d1cae3 9265 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
428870ff
BB
9266 (void) printf(gettext(" 18 Snapshot user holds\n"));
9267 (void) printf(gettext(" 19 Log device removal\n"));
9268 (void) printf(gettext(" 20 Compression using zle "
9269 "(zero-length encoding)\n"));
9270 (void) printf(gettext(" 21 Deduplication\n"));
9271 (void) printf(gettext(" 22 Received properties\n"));
9272 (void) printf(gettext(" 23 Slim ZIL\n"));
9273 (void) printf(gettext(" 24 System attributes\n"));
9274 (void) printf(gettext(" 25 Improved scrub stats\n"));
9275 (void) printf(gettext(" 26 Improved snapshot deletion "
9276 "performance\n"));
572e2857
BB
9277 (void) printf(gettext(" 27 Improved snapshot creation "
9278 "performance\n"));
9279 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
428870ff
BB
9280 (void) printf(gettext("\nFor more information on a particular "
9281 "version, including supported releases,\n"));
9282 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
b9b24bb4
CS
9283 } else if (argc == 0 && upgradeall) {
9284 cb.cb_first = B_TRUE;
34dc7c2f 9285 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
b9b24bb4
CS
9286 if (ret == 0 && cb.cb_first) {
9287 if (cb.cb_version == SPA_VERSION) {
9288 (void) printf(gettext("All pools are already "
9289 "formatted using feature flags.\n\n"));
9290 (void) printf(gettext("Every feature flags "
658fb802
CB
9291 "pool already has all supported and "
9292 "requested features enabled.\n"));
b9b24bb4
CS
9293 } else {
9294 (void) printf(gettext("All pools are already "
9295 "formatted with version %llu or higher.\n"),
02730c33 9296 (u_longlong_t)cb.cb_version);
34dc7c2f
BB
9297 }
9298 }
b9b24bb4
CS
9299 } else if (argc == 0) {
9300 cb.cb_first = B_TRUE;
9301 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
9302 assert(ret == 0);
34dc7c2f 9303
b9b24bb4
CS
9304 if (cb.cb_first) {
9305 (void) printf(gettext("All pools are formatted "
9306 "using feature flags.\n\n"));
9307 } else {
9308 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9309 "for a list of available legacy versions.\n"));
9310 }
9311
9312 cb.cb_first = B_TRUE;
9313 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
9314 assert(ret == 0);
9315
9316 if (cb.cb_first) {
9317 (void) printf(gettext("Every feature flags pool has "
658fb802 9318 "all supported and requested features enabled.\n"));
b9b24bb4
CS
9319 } else {
9320 (void) printf(gettext("\n"));
34dc7c2f
BB
9321 }
9322 } else {
2a673e76
AJ
9323 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
9324 B_FALSE, upgrade_one, &cb);
34dc7c2f
BB
9325 }
9326
9327 return (ret);
9328}
9329
9330typedef struct hist_cbdata {
9331 boolean_t first;
6f1ffb06
MA
9332 boolean_t longfmt;
9333 boolean_t internal;
34dc7c2f
BB
9334} hist_cbdata_t;
9335
7125a109
CC
9336static void
9337print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
34dc7c2f 9338{
34dc7c2f
BB
9339 nvlist_t **records;
9340 uint_t numrecords;
7125a109 9341 int i;
34dc7c2f
BB
9342
9343 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
9344 &records, &numrecords) == 0);
9345 for (i = 0; i < numrecords; i++) {
6f1ffb06 9346 nvlist_t *rec = records[i];
2ac90457 9347 char tbuf[64] = "";
34dc7c2f 9348
6f1ffb06
MA
9349 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
9350 time_t tsec;
9351 struct tm t;
34dc7c2f 9352
6f1ffb06
MA
9353 tsec = fnvlist_lookup_uint64(records[i],
9354 ZPOOL_HIST_TIME);
9355 (void) localtime_r(&tsec, &t);
9356 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
9357 }
9358
2ac90457
MA
9359 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) {
9360 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i],
9361 ZPOOL_HIST_ELAPSED_NS);
9362 (void) snprintf(tbuf + strlen(tbuf),
9363 sizeof (tbuf) - strlen(tbuf),
9364 " (%lldms)", (long long)elapsed_ns / 1000 / 1000);
9365 }
9366
6f1ffb06
MA
9367 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
9368 (void) printf("%s %s", tbuf,
9369 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
9370 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
9371 int ievent =
9372 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
9373 if (!cb->internal)
34dc7c2f 9374 continue;
6f1ffb06
MA
9375 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
9376 (void) printf("%s unrecognized record:\n",
9377 tbuf);
9378 dump_nvlist(rec, 4);
34dc7c2f 9379 continue;
6f1ffb06
MA
9380 }
9381 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
9382 zfs_history_event_names[ievent],
02730c33 9383 (longlong_t)fnvlist_lookup_uint64(
d1d7e268 9384 rec, ZPOOL_HIST_TXG),
6f1ffb06
MA
9385 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
9386 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
9387 if (!cb->internal)
9388 continue;
9389 (void) printf("%s [txg:%lld] %s", tbuf,
02730c33 9390 (longlong_t)fnvlist_lookup_uint64(
d1d7e268 9391 rec, ZPOOL_HIST_TXG),
6f1ffb06
MA
9392 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
9393 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
9394 (void) printf(" %s (%llu)",
9395 fnvlist_lookup_string(rec,
9396 ZPOOL_HIST_DSNAME),
d1d7e268 9397 (u_longlong_t)fnvlist_lookup_uint64(rec,
6f1ffb06
MA
9398 ZPOOL_HIST_DSID));
9399 }
9400 (void) printf(" %s", fnvlist_lookup_string(rec,
9401 ZPOOL_HIST_INT_STR));
9402 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
9403 if (!cb->internal)
9404 continue;
9405 (void) printf("%s ioctl %s\n", tbuf,
9406 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
9407 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
9408 (void) printf(" input:\n");
9409 dump_nvlist(fnvlist_lookup_nvlist(rec,
9410 ZPOOL_HIST_INPUT_NVL), 8);
9411 }
9412 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
9413 (void) printf(" output:\n");
9414 dump_nvlist(fnvlist_lookup_nvlist(rec,
9415 ZPOOL_HIST_OUTPUT_NVL), 8);
9416 }
d66aab7c
MA
9417 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) {
9418 (void) printf(" output nvlist omitted; "
9419 "original size: %lldKB\n",
9420 (longlong_t)fnvlist_lookup_int64(rec,
9421 ZPOOL_HIST_OUTPUT_SIZE) / 1024);
9422 }
d99a0153
CW
9423 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
9424 (void) printf(" errno: %lld\n",
9425 (longlong_t)fnvlist_lookup_int64(rec,
9426 ZPOOL_HIST_ERRNO));
9427 }
6f1ffb06
MA
9428 } else {
9429 if (!cb->internal)
9430 continue;
9431 (void) printf("%s unrecognized record:\n", tbuf);
9432 dump_nvlist(rec, 4);
34dc7c2f 9433 }
34dc7c2f
BB
9434
9435 if (!cb->longfmt) {
9436 (void) printf("\n");
9437 continue;
9438 }
9439 (void) printf(" [");
6f1ffb06
MA
9440 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
9441 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
9442 struct passwd *pwd = getpwuid(who);
9443 (void) printf("user %d ", (int)who);
9444 if (pwd != NULL)
9445 (void) printf("(%s) ", pwd->pw_name);
34dc7c2f 9446 }
6f1ffb06
MA
9447 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
9448 (void) printf("on %s",
9449 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
34dc7c2f 9450 }
6f1ffb06
MA
9451 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
9452 (void) printf(":%s",
9453 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
34dc7c2f
BB
9454 }
9455
9456 (void) printf("]");
9457 (void) printf("\n");
9458 }
7125a109
CC
9459}
9460
9461/*
9462 * Print out the command history for a specific pool.
9463 */
9464static int
9465get_history_one(zpool_handle_t *zhp, void *data)
9466{
9467 nvlist_t *nvhis;
9468 int ret;
9469 hist_cbdata_t *cb = (hist_cbdata_t *)data;
9470 uint64_t off = 0;
9471 boolean_t eof = B_FALSE;
9472
9473 cb->first = B_FALSE;
9474
9475 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
9476
9477 while (!eof) {
9478 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0)
9479 return (ret);
9480
9481 print_history_records(nvhis, cb);
9482 nvlist_free(nvhis);
9483 }
34dc7c2f 9484 (void) printf("\n");
34dc7c2f
BB
9485
9486 return (ret);
9487}
9488
9489/*
9490 * zpool history <pool>
9491 *
9492 * Displays the history of commands that modified pools.
9493 */
34dc7c2f
BB
9494int
9495zpool_do_history(int argc, char **argv)
9496{
9497 hist_cbdata_t cbdata = { 0 };
9498 int ret;
9499 int c;
9500
9501 cbdata.first = B_TRUE;
9502 /* check options */
9503 while ((c = getopt(argc, argv, "li")) != -1) {
9504 switch (c) {
9505 case 'l':
6f1ffb06 9506 cbdata.longfmt = B_TRUE;
34dc7c2f
BB
9507 break;
9508 case 'i':
6f1ffb06 9509 cbdata.internal = B_TRUE;
34dc7c2f
BB
9510 break;
9511 case '?':
9512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9513 optopt);
9514 usage(B_FALSE);
9515 }
9516 }
9517 argc -= optind;
9518 argv += optind;
9519
2a673e76
AJ
9520 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
9521 B_FALSE, get_history_one, &cbdata);
34dc7c2f
BB
9522
9523 if (argc == 0 && cbdata.first == B_TRUE) {
42cb3819 9524 (void) fprintf(stderr, gettext("no pools available\n"));
34dc7c2f
BB
9525 return (0);
9526 }
9527
9528 return (ret);
9529}
9530
26685276
BB
9531typedef struct ev_opts {
9532 int verbose;
c5343ba7 9533 int scripted;
26685276
BB
9534 int follow;
9535 int clear;
88f9c939 9536 char poolname[ZFS_MAX_DATASET_NAME_LEN];
26685276
BB
9537} ev_opts_t;
9538
9539static void
1e1c3980 9540zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
26685276
BB
9541{
9542 char ctime_str[26], str[32], *ptr;
9543 int64_t *tv;
9544 uint_t n;
9545
9546 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
9547 memset(str, ' ', 32);
9548 (void) ctime_r((const time_t *)&tv[0], ctime_str);
7cd723e6
GA
9549 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */
9550 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */
9551 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */
d1d7e268 9552 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
1e1c3980
SH
9553 if (opts->scripted)
9554 (void) printf(gettext("%s\t"), str);
9555 else
9556 (void) printf(gettext("%s "), str);
26685276
BB
9557
9558 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
9559 (void) printf(gettext("%s\n"), ptr);
9560}
9561
9562static void
9563zpool_do_events_nvprint(nvlist_t *nvl, int depth)
9564{
9565 nvpair_t *nvp;
9566
9567 for (nvp = nvlist_next_nvpair(nvl, NULL);
9568 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
9569
9570 data_type_t type = nvpair_type(nvp);
9571 const char *name = nvpair_name(nvp);
9572
9573 boolean_t b;
9574 uint8_t i8;
9575 uint16_t i16;
9576 uint32_t i32;
9577 uint64_t i64;
9578 char *str;
9579 nvlist_t *cnv;
9580
9581 printf(gettext("%*s%s = "), depth, "", name);
9582
9583 switch (type) {
9584 case DATA_TYPE_BOOLEAN:
9585 printf(gettext("%s"), "1");
9586 break;
9587
9588 case DATA_TYPE_BOOLEAN_VALUE:
9589 (void) nvpair_value_boolean_value(nvp, &b);
9590 printf(gettext("%s"), b ? "1" : "0");
9591 break;
9592
9593 case DATA_TYPE_BYTE:
9594 (void) nvpair_value_byte(nvp, &i8);
9595 printf(gettext("0x%x"), i8);
9596 break;
9597
9598 case DATA_TYPE_INT8:
9599 (void) nvpair_value_int8(nvp, (void *)&i8);
9600 printf(gettext("0x%x"), i8);
9601 break;
9602
9603 case DATA_TYPE_UINT8:
9604 (void) nvpair_value_uint8(nvp, &i8);
9605 printf(gettext("0x%x"), i8);
9606 break;
9607
9608 case DATA_TYPE_INT16:
9609 (void) nvpair_value_int16(nvp, (void *)&i16);
9610 printf(gettext("0x%x"), i16);
9611 break;
9612
9613 case DATA_TYPE_UINT16:
9614 (void) nvpair_value_uint16(nvp, &i16);
9615 printf(gettext("0x%x"), i16);
9616 break;
9617
9618 case DATA_TYPE_INT32:
9619 (void) nvpair_value_int32(nvp, (void *)&i32);
9620 printf(gettext("0x%x"), i32);
9621 break;
9622
9623 case DATA_TYPE_UINT32:
9624 (void) nvpair_value_uint32(nvp, &i32);
9625 printf(gettext("0x%x"), i32);
9626 break;
9627
9628 case DATA_TYPE_INT64:
9629 (void) nvpair_value_int64(nvp, (void *)&i64);
9630 printf(gettext("0x%llx"), (u_longlong_t)i64);
9631 break;
9632
9633 case DATA_TYPE_UINT64:
9634 (void) nvpair_value_uint64(nvp, &i64);
d02ca379
DB
9635 /*
9636 * translate vdev state values to readable
9637 * strings to aide zpool events consumers
9638 */
9639 if (strcmp(name,
9640 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
9641 strcmp(name,
9642 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
9643 printf(gettext("\"%s\" (0x%llx)"),
9644 zpool_state_to_name(i64, VDEV_AUX_NONE),
9645 (u_longlong_t)i64);
9646 } else {
9647 printf(gettext("0x%llx"), (u_longlong_t)i64);
9648 }
26685276
BB
9649 break;
9650
9651 case DATA_TYPE_HRTIME:
9652 (void) nvpair_value_hrtime(nvp, (void *)&i64);
9653 printf(gettext("0x%llx"), (u_longlong_t)i64);
9654 break;
9655
9656 case DATA_TYPE_STRING:
9657 (void) nvpair_value_string(nvp, &str);
9658 printf(gettext("\"%s\""), str ? str : "<NULL>");
9659 break;
9660
9661 case DATA_TYPE_NVLIST:
9662 printf(gettext("(embedded nvlist)\n"));
9663 (void) nvpair_value_nvlist(nvp, &cnv);
9664 zpool_do_events_nvprint(cnv, depth + 8);
c5343ba7 9665 printf(gettext("%*s(end %s)"), depth, "", name);
26685276
BB
9666 break;
9667
9668 case DATA_TYPE_NVLIST_ARRAY: {
9669 nvlist_t **val;
9670 uint_t i, nelem;
9671
9672 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
9673 printf(gettext("(%d embedded nvlists)\n"), nelem);
9674 for (i = 0; i < nelem; i++) {
9675 printf(gettext("%*s%s[%d] = %s\n"),
d1d7e268 9676 depth, "", name, i, "(embedded nvlist)");
26685276
BB
9677 zpool_do_events_nvprint(val[i], depth + 8);
9678 printf(gettext("%*s(end %s[%i])\n"),
d1d7e268 9679 depth, "", name, i);
26685276
BB
9680 }
9681 printf(gettext("%*s(end %s)\n"), depth, "", name);
9682 }
9683 break;
9684
9685 case DATA_TYPE_INT8_ARRAY: {
9686 int8_t *val;
9687 uint_t i, nelem;
9688
9689 (void) nvpair_value_int8_array(nvp, &val, &nelem);
9690 for (i = 0; i < nelem; i++)
9691 printf(gettext("0x%x "), val[i]);
9692
9693 break;
9694 }
9695
9696 case DATA_TYPE_UINT8_ARRAY: {
9697 uint8_t *val;
9698 uint_t i, nelem;
9699
9700 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
9701 for (i = 0; i < nelem; i++)
9702 printf(gettext("0x%x "), val[i]);
9703
9704 break;
9705 }
9706
9707 case DATA_TYPE_INT16_ARRAY: {
9708 int16_t *val;
9709 uint_t i, nelem;
9710
9711 (void) nvpair_value_int16_array(nvp, &val, &nelem);
9712 for (i = 0; i < nelem; i++)
9713 printf(gettext("0x%x "), val[i]);
9714
9715 break;
9716 }
9717
9718 case DATA_TYPE_UINT16_ARRAY: {
9719 uint16_t *val;
9720 uint_t i, nelem;
9721
9722 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
9723 for (i = 0; i < nelem; i++)
9724 printf(gettext("0x%x "), val[i]);
9725
9726 break;
9727 }
9728
9729 case DATA_TYPE_INT32_ARRAY: {
9730 int32_t *val;
9731 uint_t i, nelem;
9732
9733 (void) nvpair_value_int32_array(nvp, &val, &nelem);
9734 for (i = 0; i < nelem; i++)
9735 printf(gettext("0x%x "), val[i]);
9736
9737 break;
9738 }
9739
9740 case DATA_TYPE_UINT32_ARRAY: {
9741 uint32_t *val;
9742 uint_t i, nelem;
9743
9744 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
9745 for (i = 0; i < nelem; i++)
9746 printf(gettext("0x%x "), val[i]);
9747
9748 break;
9749 }
9750
9751 case DATA_TYPE_INT64_ARRAY: {
9752 int64_t *val;
9753 uint_t i, nelem;
9754
9755 (void) nvpair_value_int64_array(nvp, &val, &nelem);
9756 for (i = 0; i < nelem; i++)
d1d7e268
MK
9757 printf(gettext("0x%llx "),
9758 (u_longlong_t)val[i]);
26685276
BB
9759
9760 break;
9761 }
9762
9763 case DATA_TYPE_UINT64_ARRAY: {
9764 uint64_t *val;
9765 uint_t i, nelem;
9766
9767 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
9768 for (i = 0; i < nelem; i++)
d1d7e268
MK
9769 printf(gettext("0x%llx "),
9770 (u_longlong_t)val[i]);
26685276
BB
9771
9772 break;
9773 }
9774
d21705ea
BB
9775 case DATA_TYPE_STRING_ARRAY: {
9776 char **str;
9777 uint_t i, nelem;
9778
9779 (void) nvpair_value_string_array(nvp, &str, &nelem);
9780 for (i = 0; i < nelem; i++)
9781 printf(gettext("\"%s\" "),
9782 str[i] ? str[i] : "<NULL>");
9783
9784 break;
9785 }
9786
26685276
BB
9787 case DATA_TYPE_BOOLEAN_ARRAY:
9788 case DATA_TYPE_BYTE_ARRAY:
9789 case DATA_TYPE_DOUBLE:
6b64382b 9790 case DATA_TYPE_DONTCARE:
26685276
BB
9791 case DATA_TYPE_UNKNOWN:
9792 printf(gettext("<unknown>"));
9793 break;
9794 }
9795
9796 printf(gettext("\n"));
9797 }
9798}
9799
9800static int
9801zpool_do_events_next(ev_opts_t *opts)
9802{
9803 nvlist_t *nvl;
9b101a73 9804 int zevent_fd, ret, dropped;
88f9c939 9805 char *pool;
26685276 9806
9b101a73
BB
9807 zevent_fd = open(ZFS_DEV, O_RDWR);
9808 VERIFY(zevent_fd >= 0);
26685276 9809
c5343ba7
BB
9810 if (!opts->scripted)
9811 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
26685276
BB
9812
9813 while (1) {
9814 ret = zpool_events_next(g_zfs, &nvl, &dropped,
8c7aa0cf 9815 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
26685276
BB
9816 if (ret || nvl == NULL)
9817 break;
9818
9819 if (dropped > 0)
9820 (void) printf(gettext("dropped %d events\n"), dropped);
9821
88f9c939 9822 if (strlen(opts->poolname) > 0 &&
9823 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
9824 strcmp(opts->poolname, pool) != 0)
9825 continue;
9826
1e1c3980 9827 zpool_do_events_short(nvl, opts);
26685276
BB
9828
9829 if (opts->verbose) {
9830 zpool_do_events_nvprint(nvl, 8);
9831 printf(gettext("\n"));
9832 }
50fe577d 9833 (void) fflush(stdout);
26685276
BB
9834
9835 nvlist_free(nvl);
9836 }
9837
9b101a73 9838 VERIFY(0 == close(zevent_fd));
26685276
BB
9839
9840 return (ret);
9841}
9842
9843static int
e40ca391 9844zpool_do_events_clear(void)
26685276
BB
9845{
9846 int count, ret;
9847
9848 ret = zpool_events_clear(g_zfs, &count);
9849 if (!ret)
9850 (void) printf(gettext("cleared %d events\n"), count);
9851
9852 return (ret);
9853}
9854
9855/*
88f9c939 9856 * zpool events [-vHf [pool] | -c]
26685276
BB
9857 *
9858 * Displays events logs by ZFS.
9859 */
9860int
9861zpool_do_events(int argc, char **argv)
9862{
9863 ev_opts_t opts = { 0 };
9864 int ret;
9865 int c;
9866
9867 /* check options */
c5343ba7 9868 while ((c = getopt(argc, argv, "vHfc")) != -1) {
26685276
BB
9869 switch (c) {
9870 case 'v':
9871 opts.verbose = 1;
c5343ba7
BB
9872 break;
9873 case 'H':
9874 opts.scripted = 1;
26685276
BB
9875 break;
9876 case 'f':
9877 opts.follow = 1;
9878 break;
9879 case 'c':
9880 opts.clear = 1;
9881 break;
9882 case '?':
9883 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9884 optopt);
9885 usage(B_FALSE);
9886 }
9887 }
9888 argc -= optind;
9889 argv += optind;
9890
88f9c939 9891 if (argc > 1) {
9892 (void) fprintf(stderr, gettext("too many arguments\n"));
9893 usage(B_FALSE);
9894 } else if (argc == 1) {
9895 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
9896 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
9897 (void) fprintf(stderr,
9898 gettext("invalid pool name '%s'\n"), opts.poolname);
9899 usage(B_FALSE);
9900 }
9901 }
9902
9903 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
9904 opts.clear) {
9905 (void) fprintf(stderr,
9906 gettext("invalid options combined with -c\n"));
9907 usage(B_FALSE);
9908 }
9909
26685276 9910 if (opts.clear)
e40ca391 9911 ret = zpool_do_events_clear();
26685276
BB
9912 else
9913 ret = zpool_do_events_next(&opts);
9914
d1d7e268 9915 return (ret);
26685276
BB
9916}
9917
34dc7c2f 9918static int
2a673e76 9919get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data)
34dc7c2f
BB
9920{
9921 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
2a673e76 9922 char value[ZFS_MAXPROPLEN];
34dc7c2f 9923 zprop_source_t srctype;
34dc7c2f 9924
2a673e76
AJ
9925 for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL;
9926 pl = pl->pl_next) {
9927 char *prop_name;
34dc7c2f 9928 /*
2a673e76
AJ
9929 * If the first property is pool name, it is a special
9930 * placeholder that we can skip. This will also skip
34dc7c2f
BB
9931 * over the name property when 'all' is specified.
9932 */
9933 if (pl->pl_prop == ZPOOL_PROP_NAME &&
9934 pl == cbp->cb_proplist)
9935 continue;
9936
2a673e76
AJ
9937 if (pl->pl_prop == ZPROP_INVAL) {
9938 prop_name = pl->pl_user_prop;
9939 } else {
9940 prop_name = (char *)vdev_prop_to_name(pl->pl_prop);
9941 }
9942 if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop,
9943 prop_name, value, sizeof (value), &srctype,
9944 cbp->cb_literal) == 0) {
9945 zprop_print_one_property(vdevname, cbp, prop_name,
9946 value, srctype, NULL, NULL);
9947 }
9948 }
34dc7c2f 9949
2a673e76
AJ
9950 return (0);
9951}
9952
9953static int
9954get_callback_vdev_width_cb(void *zhp_data, nvlist_t *nv, void *data)
9955{
9956 zpool_handle_t *zhp = zhp_data;
9957 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9958 char *vdevname = zpool_vdev_name(g_zfs, zhp, nv,
9959 cbp->cb_vdevs.cb_name_flags);
9960 int ret;
9961
9962 /* Adjust the column widths for the vdev properties */
9963 ret = vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist);
9964
9965 return (ret);
9966}
9967
9968static int
9969get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data)
9970{
9971 zpool_handle_t *zhp = zhp_data;
9972 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9973 char *vdevname = zpool_vdev_name(g_zfs, zhp, nv,
9974 cbp->cb_vdevs.cb_name_flags);
9975 int ret;
9976
9977 /* Display the properties */
9978 ret = get_callback_vdev(zhp, vdevname, data);
9979
9980 return (ret);
9981}
9982
9983static int
9984get_callback(zpool_handle_t *zhp, void *data)
9985{
9986 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9987 char value[MAXNAMELEN];
9988 zprop_source_t srctype;
9989 zprop_list_t *pl;
9990 int vid;
9991
9992 if (cbp->cb_type == ZFS_TYPE_VDEV) {
9993 if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) {
9994 for_each_vdev(zhp, get_callback_vdev_width_cb, data);
9995 for_each_vdev(zhp, get_callback_vdev_cb, data);
9ae529ec 9996 } else {
2a673e76
AJ
9997 /* Adjust column widths for vdev properties */
9998 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
9999 vid++) {
10000 vdev_expand_proplist(zhp,
10001 cbp->cb_vdevs.cb_names[vid],
10002 &cbp->cb_proplist);
10003 }
10004 /* Display the properties */
10005 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
10006 vid++) {
10007 get_callback_vdev(zhp,
10008 cbp->cb_vdevs.cb_names[vid], data);
10009 }
10010 }
10011 } else {
10012 assert(cbp->cb_type == ZFS_TYPE_POOL);
10013 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
10014 /*
10015 * Skip the special fake placeholder. This will also
10016 * skip over the name property when 'all' is specified.
10017 */
10018 if (pl->pl_prop == ZPOOL_PROP_NAME &&
10019 pl == cbp->cb_proplist)
9ae529ec
CS
10020 continue;
10021
2a673e76
AJ
10022 if (pl->pl_prop == ZPROP_INVAL &&
10023 (zpool_prop_feature(pl->pl_user_prop) ||
10024 zpool_prop_unsupported(pl->pl_user_prop))) {
10025 srctype = ZPROP_SRC_LOCAL;
10026
10027 if (zpool_prop_get_feature(zhp,
10028 pl->pl_user_prop, value,
10029 sizeof (value)) == 0) {
10030 zprop_print_one_property(
10031 zpool_get_name(zhp), cbp,
10032 pl->pl_user_prop, value, srctype,
10033 NULL, NULL);
10034 }
10035 } else {
10036 if (zpool_get_prop(zhp, pl->pl_prop, value,
10037 sizeof (value), &srctype,
10038 cbp->cb_literal) != 0)
10039 continue;
10040
10041 zprop_print_one_property(zpool_get_name(zhp),
10042 cbp, zpool_prop_to_name(pl->pl_prop),
10043 value, srctype, NULL, NULL);
10044 }
9ae529ec 10045 }
34dc7c2f 10046 }
2a673e76 10047
34dc7c2f
BB
10048 return (0);
10049}
10050
2a8b84b7
AS
10051/*
10052 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10053 *
10054 * -H Scripted mode. Don't display headers, and separate properties
10055 * by a single tab.
10056 * -o List of columns to display. Defaults to
10057 * "name,property,value,source".
4e33ba4c 10058 * -p Display values in parsable (exact) format.
2a8b84b7
AS
10059 *
10060 * Get properties of pools in the system. Output space statistics
10061 * for each one as well as other attributes.
10062 */
34dc7c2f
BB
10063int
10064zpool_do_get(int argc, char **argv)
10065{
10066 zprop_get_cbdata_t cb = { 0 };
10067 zprop_list_t fake_name = { 0 };
2a8b84b7
AS
10068 int ret;
10069 int c, i;
10070 char *value;
2a673e76 10071 char *propstr = NULL;
2a8b84b7
AS
10072
10073 cb.cb_first = B_TRUE;
10074
10075 /*
10076 * Set up default columns and sources.
10077 */
10078 cb.cb_sources = ZPROP_SRC_ALL;
10079 cb.cb_columns[0] = GET_COL_NAME;
10080 cb.cb_columns[1] = GET_COL_PROPERTY;
10081 cb.cb_columns[2] = GET_COL_VALUE;
10082 cb.cb_columns[3] = GET_COL_SOURCE;
10083 cb.cb_type = ZFS_TYPE_POOL;
2a673e76
AJ
10084 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
10085 current_prop_type = cb.cb_type;
34dc7c2f 10086
d65e7381 10087 /* check options */
2a8b84b7 10088 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
d65e7381
RE
10089 switch (c) {
10090 case 'p':
10091 cb.cb_literal = B_TRUE;
10092 break;
79eb71dc
TF
10093 case 'H':
10094 cb.cb_scripted = B_TRUE;
10095 break;
2a8b84b7
AS
10096 case 'o':
10097 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
10098 i = 0;
10099 while (*optarg != '\0') {
10100 static char *col_subopts[] =
10101 { "name", "property", "value", "source",
10102 "all", NULL };
10103
10104 if (i == ZFS_GET_NCOLS) {
10105 (void) fprintf(stderr, gettext("too "
10106 "many fields given to -o "
10107 "option\n"));
10108 usage(B_FALSE);
10109 }
79eb71dc 10110
2a8b84b7
AS
10111 switch (getsubopt(&optarg, col_subopts,
10112 &value)) {
10113 case 0:
10114 cb.cb_columns[i++] = GET_COL_NAME;
10115 break;
10116 case 1:
10117 cb.cb_columns[i++] = GET_COL_PROPERTY;
10118 break;
10119 case 2:
10120 cb.cb_columns[i++] = GET_COL_VALUE;
10121 break;
10122 case 3:
10123 cb.cb_columns[i++] = GET_COL_SOURCE;
10124 break;
10125 case 4:
10126 if (i > 0) {
10127 (void) fprintf(stderr,
10128 gettext("\"all\" conflicts "
10129 "with specific fields "
10130 "given to -o option\n"));
10131 usage(B_FALSE);
10132 }
10133 cb.cb_columns[0] = GET_COL_NAME;
10134 cb.cb_columns[1] = GET_COL_PROPERTY;
10135 cb.cb_columns[2] = GET_COL_VALUE;
10136 cb.cb_columns[3] = GET_COL_SOURCE;
10137 i = ZFS_GET_NCOLS;
10138 break;
10139 default:
10140 (void) fprintf(stderr,
10141 gettext("invalid column name "
10142 "'%s'\n"), value);
10143 usage(B_FALSE);
10144 }
10145 }
10146 break;
d65e7381
RE
10147 case '?':
10148 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
10149 optopt);
10150 usage(B_FALSE);
10151 }
10152 }
10153
10154 argc -= optind;
10155 argv += optind;
10156
10157 if (argc < 1) {
9ae529ec
CS
10158 (void) fprintf(stderr, gettext("missing property "
10159 "argument\n"));
34dc7c2f 10160 usage(B_FALSE);
9ae529ec 10161 }
34dc7c2f 10162
2a673e76
AJ
10163 /* Properties list is needed later by zprop_get_list() */
10164 propstr = argv[0];
34dc7c2f 10165
d65e7381
RE
10166 argc--;
10167 argv++;
10168
2a673e76
AJ
10169 if (argc == 0) {
10170 /* No args, so just print the defaults. */
10171 } else if (are_all_pools(argc, argv)) {
10172 /* All the args are pool names */
10173 } else if (are_all_pools(1, argv)) {
10174 /* The first arg is a pool name */
10175 if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) ||
10176 are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
10177 &cb.cb_vdevs)) {
10178 /* ... and the rest are vdev names */
10179 cb.cb_vdevs.cb_names = argv + 1;
10180 cb.cb_vdevs.cb_names_count = argc - 1;
10181 cb.cb_type = ZFS_TYPE_VDEV;
10182 argc = 1; /* One pool to process */
10183 } else {
10184 fprintf(stderr, gettext("Expected a list of vdevs in"
10185 " \"%s\", but got:\n"), argv[0]);
10186 error_list_unresolved_vdevs(argc - 1, argv + 1,
10187 argv[0], &cb.cb_vdevs);
10188 fprintf(stderr, "\n");
10189 usage(B_FALSE);
10190 return (1);
10191 }
10192 } else {
10193 /*
10194 * The first arg isn't a pool name,
10195 */
10196 fprintf(stderr, gettext("missing pool name.\n"));
10197 fprintf(stderr, "\n");
10198 usage(B_FALSE);
10199 return (1);
10200 }
10201
10202 if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist,
10203 cb.cb_type) != 0) {
10204 /* Use correct list of valid properties (pool or vdev) */
10205 current_prop_type = cb.cb_type;
10206 usage(B_FALSE);
10207 }
10208
34dc7c2f
BB
10209 if (cb.cb_proplist != NULL) {
10210 fake_name.pl_prop = ZPOOL_PROP_NAME;
10211 fake_name.pl_width = strlen(gettext("NAME"));
10212 fake_name.pl_next = cb.cb_proplist;
10213 cb.cb_proplist = &fake_name;
10214 }
10215
2a673e76
AJ
10216 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type,
10217 cb.cb_literal, get_callback, &cb);
34dc7c2f
BB
10218
10219 if (cb.cb_proplist == &fake_name)
10220 zprop_free_list(fake_name.pl_next);
10221 else
10222 zprop_free_list(cb.cb_proplist);
10223
10224 return (ret);
10225}
10226
10227typedef struct set_cbdata {
10228 char *cb_propname;
10229 char *cb_value;
2a673e76
AJ
10230 zfs_type_t cb_type;
10231 vdev_cbdata_t cb_vdevs;
34dc7c2f
BB
10232 boolean_t cb_any_successful;
10233} set_cbdata_t;
10234
65c7cc49 10235static int
2a673e76 10236set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb)
34dc7c2f
BB
10237{
10238 int error;
34dc7c2f 10239
e086db16
CB
10240 /* Check if we have out-of-bounds features */
10241 if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) {
10242 boolean_t features[SPA_FEATURES];
10243 if (zpool_do_load_compat(cb->cb_value, features) !=
10244 ZPOOL_COMPATIBILITY_OK)
10245 return (-1);
10246
10247 nvlist_t *enabled = zpool_get_features(zhp);
10248 spa_feature_t i;
10249 for (i = 0; i < SPA_FEATURES; i++) {
10250 const char *fguid = spa_feature_table[i].fi_guid;
10251 if (nvlist_exists(enabled, fguid) && !features[i])
10252 break;
10253 }
10254 if (i < SPA_FEATURES)
10255 (void) fprintf(stderr, gettext("Warning: one or "
10256 "more features already enabled on pool '%s'\n"
10257 "are not present in this compatibility set.\n"),
10258 zpool_get_name(zhp));
10259 }
10260
10261 /* if we're setting a feature, check it's in compatibility set */
10262 if (zpool_prop_feature(cb->cb_propname) &&
10263 strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) {
10264 char *fname = strchr(cb->cb_propname, '@') + 1;
10265 spa_feature_t f;
10266
10267 if (zfeature_lookup_name(fname, &f) == 0) {
10268 char compat[ZFS_MAXPROPLEN];
10269 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY,
10270 compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
10271 compat[0] = '\0';
10272
10273 boolean_t features[SPA_FEATURES];
10274 if (zpool_do_load_compat(compat, features) !=
10275 ZPOOL_COMPATIBILITY_OK) {
10276 (void) fprintf(stderr, gettext("Error: "
10277 "cannot enable feature '%s' on pool '%s'\n"
10278 "because the pool's 'compatibility' "
10279 "property cannot be parsed.\n"),
10280 fname, zpool_get_name(zhp));
10281 return (-1);
10282 }
10283
10284 if (!features[f]) {
10285 (void) fprintf(stderr, gettext("Error: "
10286 "cannot enable feature '%s' on pool '%s'\n"
10287 "as it is not specified in this pool's "
10288 "current compatibility set.\n"
10289 "Consider setting 'compatibility' to a "
10290 "less restrictive set, or to 'off'.\n"),
10291 fname, zpool_get_name(zhp));
10292 return (-1);
10293 }
10294 }
10295 }
10296
34dc7c2f
BB
10297 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
10298
2a673e76
AJ
10299 return (error);
10300}
10301
10302static int
10303set_callback(zpool_handle_t *zhp, void *data)
10304{
10305 int error;
10306 set_cbdata_t *cb = (set_cbdata_t *)data;
10307
10308 if (cb->cb_type == ZFS_TYPE_VDEV) {
10309 error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names,
10310 cb->cb_propname, cb->cb_value);
10311 } else {
10312 assert(cb->cb_type == ZFS_TYPE_POOL);
10313 error = set_pool_callback(zhp, cb);
10314 }
34dc7c2f 10315
2a673e76 10316 cb->cb_any_successful = !error;
34dc7c2f
BB
10317 return (error);
10318}
10319
10320int
10321zpool_do_set(int argc, char **argv)
10322{
10323 set_cbdata_t cb = { 0 };
10324 int error;
10325
2a673e76 10326 current_prop_type = ZFS_TYPE_POOL;
34dc7c2f
BB
10327 if (argc > 1 && argv[1][0] == '-') {
10328 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
10329 argv[1][1]);
10330 usage(B_FALSE);
10331 }
10332
10333 if (argc < 2) {
10334 (void) fprintf(stderr, gettext("missing property=value "
10335 "argument\n"));
10336 usage(B_FALSE);
10337 }
10338
10339 if (argc < 3) {
10340 (void) fprintf(stderr, gettext("missing pool name\n"));
10341 usage(B_FALSE);
10342 }
10343
2a673e76 10344 if (argc > 4) {
34dc7c2f
BB
10345 (void) fprintf(stderr, gettext("too many pool names\n"));
10346 usage(B_FALSE);
10347 }
10348
10349 cb.cb_propname = argv[1];
2a673e76
AJ
10350 cb.cb_type = ZFS_TYPE_POOL;
10351 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
34dc7c2f
BB
10352 cb.cb_value = strchr(cb.cb_propname, '=');
10353 if (cb.cb_value == NULL) {
10354 (void) fprintf(stderr, gettext("missing value in "
10355 "property=value argument\n"));
10356 usage(B_FALSE);
10357 }
10358
10359 *(cb.cb_value) = '\0';
10360 cb.cb_value++;
2a673e76
AJ
10361 argc -= 2;
10362 argv += 2;
10363
10364 if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
10365 /* Argument is a vdev */
10366 cb.cb_vdevs.cb_names = argv;
10367 cb.cb_vdevs.cb_names_count = 1;
10368 cb.cb_type = ZFS_TYPE_VDEV;
10369 argc = 0; /* No pools to process */
10370 } else if (are_all_pools(1, argv)) {
10371 /* The first arg is a pool name */
10372 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
10373 &cb.cb_vdevs)) {
10374 /* 2nd argument is a vdev */
10375 cb.cb_vdevs.cb_names = argv + 1;
10376 cb.cb_vdevs.cb_names_count = 1;
10377 cb.cb_type = ZFS_TYPE_VDEV;
10378 argc = 1; /* One pool to process */
10379 } else if (argc > 1) {
10380 (void) fprintf(stderr,
10381 gettext("too many pool names\n"));
10382 usage(B_FALSE);
10383 }
10384 }
34dc7c2f 10385
2a673e76
AJ
10386 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
10387 B_FALSE, set_callback, &cb);
34dc7c2f
BB
10388
10389 return (error);
10390}
2288d419
BB
10391
10392/* Add up the total number of bytes left to initialize/trim across all vdevs */
e60e158e 10393static uint64_t
2288d419 10394vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity)
e60e158e
JG
10395{
10396 uint64_t bytes_remaining;
10397 nvlist_t **child;
10398 uint_t c, children;
10399 vdev_stat_t *vs;
10400
2288d419
BB
10401 assert(activity == ZPOOL_WAIT_INITIALIZE ||
10402 activity == ZPOOL_WAIT_TRIM);
10403
e60e158e
JG
10404 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
10405 (uint64_t **)&vs, &c) == 0);
10406
2288d419
BB
10407 if (activity == ZPOOL_WAIT_INITIALIZE &&
10408 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE)
e60e158e
JG
10409 bytes_remaining = vs->vs_initialize_bytes_est -
10410 vs->vs_initialize_bytes_done;
2288d419
BB
10411 else if (activity == ZPOOL_WAIT_TRIM &&
10412 vs->vs_trim_state == VDEV_TRIM_ACTIVE)
10413 bytes_remaining = vs->vs_trim_bytes_est -
10414 vs->vs_trim_bytes_done;
e60e158e
JG
10415 else
10416 bytes_remaining = 0;
10417
10418 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10419 &child, &children) != 0)
10420 children = 0;
10421
10422 for (c = 0; c < children; c++)
2288d419 10423 bytes_remaining += vdev_activity_remaining(child[c], activity);
e60e158e
JG
10424
10425 return (bytes_remaining);
10426}
10427
9a49d3f3
BB
10428/* Add up the total number of bytes left to rebuild across top-level vdevs */
10429static uint64_t
10430vdev_activity_top_remaining(nvlist_t *nv)
10431{
10432 uint64_t bytes_remaining = 0;
10433 nvlist_t **child;
10434 uint_t children;
10435 int error;
10436
10437 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10438 &child, &children) != 0)
10439 children = 0;
10440
10441 for (uint_t c = 0; c < children; c++) {
10442 vdev_rebuild_stat_t *vrs;
10443 uint_t i;
10444
10445 error = nvlist_lookup_uint64_array(child[c],
10446 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i);
10447 if (error == 0) {
10448 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
10449 bytes_remaining += (vrs->vrs_bytes_est -
10450 vrs->vrs_bytes_rebuilt);
10451 }
10452 }
10453 }
10454
10455 return (bytes_remaining);
10456}
10457
e60e158e
JG
10458/* Whether any vdevs are 'spare' or 'replacing' vdevs */
10459static boolean_t
10460vdev_any_spare_replacing(nvlist_t *nv)
10461{
10462 nvlist_t **child;
10463 uint_t c, children;
10464 char *vdev_type;
10465
10466 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type);
10467
10468 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 ||
b2255edc
BB
10469 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 ||
10470 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) {
e60e158e
JG
10471 return (B_TRUE);
10472 }
10473
10474 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10475 &child, &children) != 0)
10476 children = 0;
10477
10478 for (c = 0; c < children; c++) {
10479 if (vdev_any_spare_replacing(child[c]))
10480 return (B_TRUE);
10481 }
10482
10483 return (B_FALSE);
10484}
10485
10486typedef struct wait_data {
10487 char *wd_poolname;
10488 boolean_t wd_scripted;
10489 boolean_t wd_exact;
10490 boolean_t wd_headers_once;
d553fb9b 10491 boolean_t wd_should_exit;
e60e158e
JG
10492 /* Which activities to wait for */
10493 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES];
10494 float wd_interval;
d553fb9b
JL
10495 pthread_cond_t wd_cv;
10496 pthread_mutex_t wd_mutex;
e60e158e
JG
10497} wait_data_t;
10498
10499/*
10500 * Print to stdout a single line, containing one column for each activity that
10501 * we are waiting for specifying how many bytes of work are left for that
10502 * activity.
10503 */
10504static void
10505print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
10506{
10507 nvlist_t *config, *nvroot;
10508 uint_t c;
10509 int i;
10510 pool_checkpoint_stat_t *pcs = NULL;
10511 pool_scan_stat_t *pss = NULL;
10512 pool_removal_stat_t *prs = NULL;
10513 char *headers[] = {"DISCARD", "FREE", "INITIALIZE", "REPLACE",
2288d419 10514 "REMOVE", "RESILVER", "SCRUB", "TRIM"};
e60e158e
JG
10515 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES];
10516
10517 /* Calculate the width of each column */
10518 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10519 /*
10520 * Make sure we have enough space in the col for pretty-printed
10521 * numbers and for the column header, and then leave a couple
10522 * spaces between cols for readability.
10523 */
10524 col_widths[i] = MAX(strlen(headers[i]), 6) + 2;
10525 }
10526
10527 /* Print header if appropriate */
10528 int term_height = terminal_height();
10529 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 &&
10530 row % (term_height-1) == 0);
10531 if (!wd->wd_scripted && (row == 0 || reprint_header)) {
10532 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10533 if (wd->wd_enabled[i])
10534 (void) printf("%*s", col_widths[i], headers[i]);
10535 }
10536 (void) printf("\n");
10537 }
10538
10539 /* Bytes of work remaining in each activity */
10540 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0};
10541
10542 bytes_rem[ZPOOL_WAIT_FREE] =
10543 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL);
10544
10545 config = zpool_get_config(zhp, NULL);
10546 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
10547
10548 (void) nvlist_lookup_uint64_array(nvroot,
10549 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
10550 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
10551 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space;
10552
10553 (void) nvlist_lookup_uint64_array(nvroot,
10554 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
10555 if (prs != NULL && prs->prs_state == DSS_SCANNING)
10556 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy -
10557 prs->prs_copied;
10558
10559 (void) nvlist_lookup_uint64_array(nvroot,
10560 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c);
10561 if (pss != NULL && pss->pss_state == DSS_SCANNING &&
10562 pss->pss_pass_scrub_pause == 0) {
10563 int64_t rem = pss->pss_to_examine - pss->pss_issued;
10564 if (pss->pss_func == POOL_SCAN_SCRUB)
10565 bytes_rem[ZPOOL_WAIT_SCRUB] = rem;
10566 else
10567 bytes_rem[ZPOOL_WAIT_RESILVER] = rem;
9a49d3f3
BB
10568 } else if (check_rebuilding(nvroot, NULL)) {
10569 bytes_rem[ZPOOL_WAIT_RESILVER] =
10570 vdev_activity_top_remaining(nvroot);
e60e158e
JG
10571 }
10572
2288d419
BB
10573 bytes_rem[ZPOOL_WAIT_INITIALIZE] =
10574 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE);
10575 bytes_rem[ZPOOL_WAIT_TRIM] =
10576 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM);
e60e158e
JG
10577
10578 /*
10579 * A replace finishes after resilvering finishes, so the amount of work
10580 * left for a replace is the same as for resilvering.
10581 *
10582 * It isn't quite correct to say that if we have any 'spare' or
10583 * 'replacing' vdevs and a resilver is happening, then a replace is in
10584 * progress, like we do here. When a hot spare is used, the faulted vdev
10585 * is not removed after the hot spare is resilvered, so parent 'spare'
10586 * vdev is not removed either. So we could have a 'spare' vdev, but be
10587 * resilvering for a different reason. However, we use it as a heuristic
10588 * because we don't have access to the DTLs, which could tell us whether
10589 * or not we have really finished resilvering a hot spare.
10590 */
10591 if (vdev_any_spare_replacing(nvroot))
10592 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER];
10593
10594 if (timestamp_fmt != NODATE)
10595 print_timestamp(timestamp_fmt);
10596
10597 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10598 char buf[64];
10599 if (!wd->wd_enabled[i])
10600 continue;
10601
10602 if (wd->wd_exact)
10603 (void) snprintf(buf, sizeof (buf), "%" PRIi64,
10604 bytes_rem[i]);
10605 else
10606 zfs_nicenum(bytes_rem[i], buf, sizeof (buf));
10607
10608 if (wd->wd_scripted)
10609 (void) printf(i == 0 ? "%s" : "\t%s", buf);
10610 else
10611 (void) printf(" %*s", col_widths[i] - 1, buf);
10612 }
10613 (void) printf("\n");
10614 (void) fflush(stdout);
10615}
10616
65c7cc49 10617static void *
e60e158e
JG
10618wait_status_thread(void *arg)
10619{
10620 wait_data_t *wd = (wait_data_t *)arg;
10621 zpool_handle_t *zhp;
10622
10623 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL)
10624 return (void *)(1);
10625
10626 for (int row = 0; ; row++) {
10627 boolean_t missing;
10628 struct timespec timeout;
d553fb9b 10629 int ret = 0;
e60e158e
JG
10630 (void) clock_gettime(CLOCK_REALTIME, &timeout);
10631
10632 if (zpool_refresh_stats(zhp, &missing) != 0 || missing ||
10633 zpool_props_refresh(zhp) != 0) {
10634 zpool_close(zhp);
10635 return (void *)(uintptr_t)(missing ? 0 : 1);
10636 }
10637
10638 print_wait_status_row(wd, zhp, row);
10639
10640 timeout.tv_sec += floor(wd->wd_interval);
10641 long nanos = timeout.tv_nsec +
10642 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC;
10643 if (nanos >= NANOSEC) {
10644 timeout.tv_sec++;
10645 timeout.tv_nsec = nanos - NANOSEC;
10646 } else {
10647 timeout.tv_nsec = nanos;
10648 }
d553fb9b
JL
10649 pthread_mutex_lock(&wd->wd_mutex);
10650 if (!wd->wd_should_exit)
10651 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex,
10652 &timeout);
10653 pthread_mutex_unlock(&wd->wd_mutex);
10654 if (ret == 0) {
e60e158e 10655 break; /* signaled by main thread */
d553fb9b
JL
10656 } else if (ret != ETIMEDOUT) {
10657 (void) fprintf(stderr, gettext("pthread_cond_timedwait "
10658 "failed: %s\n"), strerror(ret));
e60e158e
JG
10659 zpool_close(zhp);
10660 return (void *)(uintptr_t)(1);
10661 }
10662 }
10663
10664 zpool_close(zhp);
10665 return (void *)(0);
10666}
10667
10668int
10669zpool_do_wait(int argc, char **argv)
10670{
10671 boolean_t verbose = B_FALSE;
fb188409 10672 int c;
e60e158e
JG
10673 char *value;
10674 int i;
10675 unsigned long count;
10676 pthread_t status_thr;
10677 int error = 0;
10678 zpool_handle_t *zhp;
10679
10680 wait_data_t wd;
10681 wd.wd_scripted = B_FALSE;
10682 wd.wd_exact = B_FALSE;
10683 wd.wd_headers_once = B_FALSE;
d553fb9b 10684 wd.wd_should_exit = B_FALSE;
e60e158e 10685
d553fb9b
JL
10686 pthread_mutex_init(&wd.wd_mutex, NULL);
10687 pthread_cond_init(&wd.wd_cv, NULL);
e60e158e
JG
10688
10689 /* By default, wait for all types of activity. */
10690 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++)
10691 wd.wd_enabled[i] = B_TRUE;
10692
10693 while ((c = getopt(argc, argv, "HpT:t:")) != -1) {
10694 switch (c) {
10695 case 'H':
10696 wd.wd_scripted = B_TRUE;
10697 break;
10698 case 'n':
10699 wd.wd_headers_once = B_TRUE;
10700 break;
10701 case 'p':
10702 wd.wd_exact = B_TRUE;
10703 break;
10704 case 'T':
10705 get_timestamp_arg(*optarg);
10706 break;
10707 case 't':
10708 {
10709 static char *col_subopts[] = { "discard", "free",
10710 "initialize", "replace", "remove", "resilver",
2288d419 10711 "scrub", "trim", NULL };
e60e158e
JG
10712
10713 /* Reset activities array */
10714 bzero(&wd.wd_enabled, sizeof (wd.wd_enabled));
10715 while (*optarg != '\0') {
10716 int activity = getsubopt(&optarg, col_subopts,
10717 &value);
10718
10719 if (activity < 0) {
10720 (void) fprintf(stderr,
10721 gettext("invalid activity '%s'\n"),
10722 value);
10723 usage(B_FALSE);
10724 }
10725
10726 wd.wd_enabled[activity] = B_TRUE;
10727 }
10728 break;
10729 }
10730 case '?':
10731 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
10732 optopt);
10733 usage(B_FALSE);
10734 }
10735 }
10736
10737 argc -= optind;
10738 argv += optind;
10739
10740 get_interval_count(&argc, argv, &wd.wd_interval, &count);
10741 if (count != 0) {
10742 /* This subcmd only accepts an interval, not a count */
10743 (void) fprintf(stderr, gettext("too many arguments\n"));
10744 usage(B_FALSE);
10745 }
10746
10747 if (wd.wd_interval != 0)
10748 verbose = B_TRUE;
10749
10750 if (argc < 1) {
10751 (void) fprintf(stderr, gettext("missing 'pool' argument\n"));
10752 usage(B_FALSE);
10753 }
10754 if (argc > 1) {
10755 (void) fprintf(stderr, gettext("too many arguments\n"));
10756 usage(B_FALSE);
10757 }
10758
10759 wd.wd_poolname = argv[0];
10760
10761 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL)
10762 return (1);
10763
10764 if (verbose) {
10765 /*
10766 * We use a separate thread for printing status updates because
10767 * the main thread will call lzc_wait(), which blocks as long
10768 * as an activity is in progress, which can be a long time.
10769 */
10770 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd)
10771 != 0) {
10772 (void) fprintf(stderr, gettext("failed to create status"
10773 "thread: %s\n"), strerror(errno));
10774 zpool_close(zhp);
10775 return (1);
10776 }
10777 }
10778
10779 /*
10780 * Loop over all activities that we are supposed to wait for until none
10781 * of them are in progress. Note that this means we can end up waiting
10782 * for more activities to complete than just those that were in progress
10783 * when we began waiting; if an activity we are interested in begins
10784 * while we are waiting for another activity, we will wait for both to
10785 * complete before exiting.
10786 */
10787 for (;;) {
10788 boolean_t missing = B_FALSE;
10789 boolean_t any_waited = B_FALSE;
10790
10791 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10792 boolean_t waited;
10793
10794 if (!wd.wd_enabled[i])
10795 continue;
10796
10797 error = zpool_wait_status(zhp, i, &missing, &waited);
10798 if (error != 0 || missing)
10799 break;
10800
10801 any_waited = (any_waited || waited);
10802 }
10803
10804 if (error != 0 || missing || !any_waited)
10805 break;
10806 }
10807
10808 zpool_close(zhp);
10809
10810 if (verbose) {
10811 uintptr_t status;
d553fb9b
JL
10812 pthread_mutex_lock(&wd.wd_mutex);
10813 wd.wd_should_exit = B_TRUE;
10814 pthread_cond_signal(&wd.wd_cv);
10815 pthread_mutex_unlock(&wd.wd_mutex);
e60e158e
JG
10816 (void) pthread_join(status_thr, (void *)&status);
10817 if (status != 0)
10818 error = status;
10819 }
10820
d553fb9b
JL
10821 pthread_mutex_destroy(&wd.wd_mutex);
10822 pthread_cond_destroy(&wd.wd_cv);
e60e158e
JG
10823 return (error);
10824}
34dc7c2f
BB
10825
10826static int
10827find_command_idx(char *command, int *idx)
10828{
10829 int i;
10830
10831 for (i = 0; i < NCOMMAND; i++) {
10832 if (command_table[i].name == NULL)
10833 continue;
10834
10835 if (strcmp(command, command_table[i].name) == 0) {
10836 *idx = i;
10837 return (0);
10838 }
10839 }
10840 return (1);
10841}
10842
50478c6d
T
10843/*
10844 * Display version message
10845 */
10846static int
10847zpool_do_version(int argc, char **argv)
10848{
e40ca391
AZ
10849 (void) argc, (void) argv;
10850
50478c6d
T
10851 if (zfs_version_print() == -1)
10852 return (1);
10853
10854 return (0);
10855}
10856
658fb802
CB
10857/*
10858 * Do zpool_load_compat() and print error message on failure
10859 */
10860static zpool_compat_status_t
10861zpool_do_load_compat(const char *compat, boolean_t *list)
10862{
e086db16
CB
10863 char report[1024];
10864
658fb802
CB
10865 zpool_compat_status_t ret;
10866
e086db16
CB
10867 ret = zpool_load_compat(compat, list, report, 1024);
10868 switch (ret) {
10869
658fb802
CB
10870 case ZPOOL_COMPATIBILITY_OK:
10871 break;
e086db16
CB
10872
10873 case ZPOOL_COMPATIBILITY_NOFILES:
658fb802 10874 case ZPOOL_COMPATIBILITY_BADFILE:
e086db16
CB
10875 case ZPOOL_COMPATIBILITY_BADTOKEN:
10876 (void) fprintf(stderr, "Error: %s\n", report);
658fb802 10877 break;
e086db16
CB
10878
10879 case ZPOOL_COMPATIBILITY_WARNTOKEN:
10880 (void) fprintf(stderr, "Warning: %s\n", report);
10881 ret = ZPOOL_COMPATIBILITY_OK;
658fb802
CB
10882 break;
10883 }
10884 return (ret);
10885}
10886
34dc7c2f
BB
10887int
10888main(int argc, char **argv)
10889{
8e896579 10890 int ret = 0;
d4ed6673 10891 int i = 0;
34dc7c2f 10892 char *cmdname;
edc05fdb 10893 char **newargv;
34dc7c2f
BB
10894
10895 (void) setlocale(LC_ALL, "");
c2c7ca0d 10896 (void) setlocale(LC_NUMERIC, "C");
34dc7c2f 10897 (void) textdomain(TEXT_DOMAIN);
5b4136bd 10898 srand(time(NULL));
34dc7c2f 10899
34dc7c2f
BB
10900 opterr = 0;
10901
10902 /*
10903 * Make sure the user has specified some command.
10904 */
10905 if (argc < 2) {
10906 (void) fprintf(stderr, gettext("missing command\n"));
10907 usage(B_FALSE);
10908 }
10909
10910 cmdname = argv[1];
10911
10912 /*
10913 * Special case '-?'
10914 */
d1d7e268 10915 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
34dc7c2f
BB
10916 usage(B_TRUE);
10917
50478c6d
T
10918 /*
10919 * Special case '-V|--version'
10920 */
10921 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
10922 return (zpool_do_version(argc, argv));
10923
65037d9b 10924 if ((g_zfs = libzfs_init()) == NULL) {
afc8f0a6 10925 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
9b020fd9 10926 return (1);
65037d9b 10927 }
9b020fd9
BB
10928
10929 libzfs_print_on_error(g_zfs, B_TRUE);
10930
6f1ffb06 10931 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
34dc7c2f 10932
edc05fdb
D
10933 /*
10934 * Many commands modify input strings for string parsing reasons.
10935 * We create a copy to protect the original argv.
10936 */
10937 newargv = malloc((argc + 1) * sizeof (newargv[0]));
10938 for (i = 0; i < argc; i++)
10939 newargv[i] = strdup(argv[i]);
10940 newargv[argc] = NULL;
10941
34dc7c2f
BB
10942 /*
10943 * Run the appropriate command.
10944 */
10945 if (find_command_idx(cmdname, &i) == 0) {
10946 current_command = &command_table[i];
edc05fdb 10947 ret = command_table[i].func(argc - 1, newargv + 1);
34dc7c2f
BB
10948 } else if (strchr(cmdname, '=')) {
10949 verify(find_command_idx("set", &i) == 0);
10950 current_command = &command_table[i];
edc05fdb 10951 ret = command_table[i].func(argc, newargv);
34dc7c2f
BB
10952 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
10953 /*
10954 * 'freeze' is a vile debugging abomination, so we treat
10955 * it as such.
10956 */
4807c0ba
TC
10957 zfs_cmd_t zc = {"\0"};
10958
10959 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
10960 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
10961 if (ret != 0) {
10962 (void) fprintf(stderr,
10963 gettext("failed to freeze pool: %d\n"), errno);
10964 ret = 1;
10965 }
10966
10967 log_history = 0;
34dc7c2f
BB
10968 } else {
10969 (void) fprintf(stderr, gettext("unrecognized "
10970 "command '%s'\n"), cmdname);
10971 usage(B_FALSE);
d4ed6673 10972 ret = 1;
34dc7c2f
BB
10973 }
10974
edc05fdb
D
10975 for (i = 0; i < argc; i++)
10976 free(newargv[i]);
10977 free(newargv);
10978
6f1ffb06
MA
10979 if (ret == 0 && log_history)
10980 (void) zpool_log_history(g_zfs, history_str);
10981
34dc7c2f
BB
10982 libzfs_fini(g_zfs);
10983
10984 /*
10985 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10986 * for the purposes of running ::findleaks.
10987 */
10988 if (getenv("ZFS_ABORT") != NULL) {
10989 (void) printf("dumping core by request\n");
10990 abort();
10991 }
10992
10993 return (ret);
10994}