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