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