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