]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zpool/zpool_main.c
Add zpool_events_seek() functionality
[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.
1bd201e7 25 * Copyright (c) 2012 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.
34dc7c2f
BB
28 */
29
34dc7c2f
BB
30#include <assert.h>
31#include <ctype.h>
32#include <dirent.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <libgen.h>
36#include <libintl.h>
37#include <libuutil.h>
38#include <locale.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <strings.h>
43#include <unistd.h>
44#include <priv.h>
45#include <pwd.h>
46#include <zone.h>
1bd201e7 47#include <zfs_prop.h>
34dc7c2f 48#include <sys/fs/zfs.h>
34dc7c2f 49#include <sys/stat.h>
26685276
BB
50#include <sys/fm/util.h>
51#include <sys/fm/protocol.h>
34dc7c2f
BB
52
53#include <libzfs.h>
54
55#include "zpool_util.h"
56#include "zfs_comutil.h"
9ae529ec 57#include "zfeature_common.h"
34dc7c2f 58
428870ff
BB
59#include "statcommon.h"
60
34dc7c2f
BB
61static int zpool_do_create(int, char **);
62static int zpool_do_destroy(int, char **);
63
64static int zpool_do_add(int, char **);
65static int zpool_do_remove(int, char **);
131cc95c 66static int zpool_do_labelclear(int, char **);
34dc7c2f
BB
67
68static int zpool_do_list(int, char **);
69static int zpool_do_iostat(int, char **);
70static int zpool_do_status(int, char **);
71
72static int zpool_do_online(int, char **);
73static int zpool_do_offline(int, char **);
74static int zpool_do_clear(int, char **);
1bd201e7 75static int zpool_do_reopen(int, char **);
34dc7c2f 76
3541dc6d
GA
77static int zpool_do_reguid(int, char **);
78
34dc7c2f
BB
79static int zpool_do_attach(int, char **);
80static int zpool_do_detach(int, char **);
81static int zpool_do_replace(int, char **);
428870ff 82static int zpool_do_split(int, char **);
34dc7c2f
BB
83
84static int zpool_do_scrub(int, char **);
85
86static int zpool_do_import(int, char **);
87static int zpool_do_export(int, char **);
88
89static int zpool_do_upgrade(int, char **);
90
91static int zpool_do_history(int, char **);
26685276 92static int zpool_do_events(int, char **);
34dc7c2f
BB
93
94static int zpool_do_get(int, char **);
95static int zpool_do_set(int, char **);
96
97/*
98 * These libumem hooks provide a reasonable set of defaults for the allocator's
99 * debugging facilities.
100 */
b128c09f
BB
101
102#ifdef DEBUG
34dc7c2f
BB
103const char *
104_umem_debug_init(void)
105{
106 return ("default,verbose"); /* $UMEM_DEBUG setting */
107}
108
109const char *
110_umem_logging_init(void)
111{
112 return ("fail,contents"); /* $UMEM_LOGGING setting */
113}
b128c09f 114#endif
34dc7c2f
BB
115
116typedef enum {
117 HELP_ADD,
118 HELP_ATTACH,
119 HELP_CLEAR,
120 HELP_CREATE,
121 HELP_DESTROY,
122 HELP_DETACH,
123 HELP_EXPORT,
124 HELP_HISTORY,
125 HELP_IMPORT,
126 HELP_IOSTAT,
131cc95c 127 HELP_LABELCLEAR,
34dc7c2f
BB
128 HELP_LIST,
129 HELP_OFFLINE,
130 HELP_ONLINE,
131 HELP_REPLACE,
132 HELP_REMOVE,
133 HELP_SCRUB,
134 HELP_STATUS,
135 HELP_UPGRADE,
26685276 136 HELP_EVENTS,
34dc7c2f 137 HELP_GET,
428870ff 138 HELP_SET,
3541dc6d 139 HELP_SPLIT,
1bd201e7
CS
140 HELP_REGUID,
141 HELP_REOPEN
34dc7c2f
BB
142} zpool_help_t;
143
144
145typedef struct zpool_command {
146 const char *name;
147 int (*func)(int, char **);
148 zpool_help_t usage;
149} zpool_command_t;
150
151/*
152 * Master command table. Each ZFS command has a name, associated function, and
153 * usage message. The usage messages need to be internationalized, so we have
154 * to have a function to return the usage message based on a command index.
155 *
156 * These commands are organized according to how they are displayed in the usage
157 * message. An empty command (one with a NULL name) indicates an empty line in
158 * the generic usage message.
159 */
160static zpool_command_t command_table[] = {
161 { "create", zpool_do_create, HELP_CREATE },
162 { "destroy", zpool_do_destroy, HELP_DESTROY },
163 { NULL },
164 { "add", zpool_do_add, HELP_ADD },
165 { "remove", zpool_do_remove, HELP_REMOVE },
166 { NULL },
131cc95c
DK
167 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
168 { NULL },
34dc7c2f
BB
169 { "list", zpool_do_list, HELP_LIST },
170 { "iostat", zpool_do_iostat, HELP_IOSTAT },
171 { "status", zpool_do_status, HELP_STATUS },
172 { NULL },
173 { "online", zpool_do_online, HELP_ONLINE },
174 { "offline", zpool_do_offline, HELP_OFFLINE },
175 { "clear", zpool_do_clear, HELP_CLEAR },
1bd201e7 176 { "reopen", zpool_do_reopen, HELP_REOPEN },
34dc7c2f
BB
177 { NULL },
178 { "attach", zpool_do_attach, HELP_ATTACH },
179 { "detach", zpool_do_detach, HELP_DETACH },
180 { "replace", zpool_do_replace, HELP_REPLACE },
428870ff 181 { "split", zpool_do_split, HELP_SPLIT },
34dc7c2f
BB
182 { NULL },
183 { "scrub", zpool_do_scrub, HELP_SCRUB },
184 { NULL },
185 { "import", zpool_do_import, HELP_IMPORT },
186 { "export", zpool_do_export, HELP_EXPORT },
187 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
3541dc6d 188 { "reguid", zpool_do_reguid, HELP_REGUID },
34dc7c2f
BB
189 { NULL },
190 { "history", zpool_do_history, HELP_HISTORY },
26685276
BB
191 { "events", zpool_do_events, HELP_EVENTS },
192 { NULL },
34dc7c2f
BB
193 { "get", zpool_do_get, HELP_GET },
194 { "set", zpool_do_set, HELP_SET },
195};
196
197#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
198
6f1ffb06 199static zpool_command_t *current_command;
34dc7c2f 200static char history_str[HIS_MAX_RECORD_LEN];
6f1ffb06 201static boolean_t log_history = B_TRUE;
428870ff
BB
202static uint_t timestamp_fmt = NODATE;
203
34dc7c2f
BB
204static const char *
205get_usage(zpool_help_t idx) {
206 switch (idx) {
207 case HELP_ADD:
df831108
CP
208 return (gettext("\tadd [-fn] [-o property=value] "
209 "<pool> <vdev> ...\n"));
34dc7c2f 210 case HELP_ATTACH:
df831108
CP
211 return (gettext("\tattach [-f] [-o property=value] "
212 "<pool> <device> <new-device>\n"));
34dc7c2f 213 case HELP_CLEAR:
428870ff 214 return (gettext("\tclear [-nF] <pool> [device]\n"));
34dc7c2f 215 case HELP_CREATE:
9ae529ec 216 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
b128c09f 217 "\t [-O file-system-property=value] ... \n"
34dc7c2f
BB
218 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
219 case HELP_DESTROY:
220 return (gettext("\tdestroy [-f] <pool>\n"));
221 case HELP_DETACH:
222 return (gettext("\tdetach <pool> <device>\n"));
223 case HELP_EXPORT:
224 return (gettext("\texport [-f] <pool> ...\n"));
225 case HELP_HISTORY:
226 return (gettext("\thistory [-il] [<pool>] ...\n"));
227 case HELP_IMPORT:
228 return (gettext("\timport [-d dir] [-D]\n"
572e2857 229 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
34dc7c2f 230 "\timport [-o mntopts] [-o property=value] ... \n"
572e2857
BB
231 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
232 "[-R root] [-F [-n]] -a\n"
34dc7c2f 233 "\timport [-o mntopts] [-o property=value] ... \n"
572e2857
BB
234 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
235 "[-R root] [-F [-n]]\n"
236 "\t <pool | id> [newpool]\n"));
34dc7c2f 237 case HELP_IOSTAT:
428870ff 238 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
34dc7c2f 239 "[count]]\n"));
131cc95c
DK
240 case HELP_LABELCLEAR:
241 return (gettext("\tlabelclear [-f] <vdev>\n"));
34dc7c2f 242 case HELP_LIST:
da91c901 243 return (gettext("\tlist [-Hv] [-o property[,...]] "
428870ff 244 "[-T d|u] [pool] ... [interval [count]]\n"));
34dc7c2f
BB
245 case HELP_OFFLINE:
246 return (gettext("\toffline [-t] <pool> <device> ...\n"));
247 case HELP_ONLINE:
248 return (gettext("\tonline <pool> <device> ...\n"));
249 case HELP_REPLACE:
250 return (gettext("\treplace [-f] <pool> <device> "
251 "[new-device]\n"));
252 case HELP_REMOVE:
253 return (gettext("\tremove <pool> <device> ...\n"));
1bd201e7 254 case HELP_REOPEN:
5853fe79 255 return (gettext("\treopen <pool>\n"));
34dc7c2f
BB
256 case HELP_SCRUB:
257 return (gettext("\tscrub [-s] <pool> ...\n"));
258 case HELP_STATUS:
2e2ddc30 259 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
428870ff 260 "[count]]\n"));
34dc7c2f
BB
261 case HELP_UPGRADE:
262 return (gettext("\tupgrade\n"
263 "\tupgrade -v\n"
264 "\tupgrade [-V version] <-a | pool ...>\n"));
26685276 265 case HELP_EVENTS:
c5343ba7 266 return (gettext("\tevents [-vHfc]\n"));
34dc7c2f 267 case HELP_GET:
d65e7381 268 return (gettext("\tget [-p] <\"all\" | property[,...]> "
34dc7c2f
BB
269 "<pool> ...\n"));
270 case HELP_SET:
271 return (gettext("\tset <property=value> <pool> \n"));
428870ff
BB
272 case HELP_SPLIT:
273 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
274 "\t [-o property=value] <pool> <newpool> "
275 "[<device> ...]\n"));
3541dc6d
GA
276 case HELP_REGUID:
277 return (gettext("\treguid <pool>\n"));
34dc7c2f
BB
278 }
279
280 abort();
281 /* NOTREACHED */
282}
283
284
285/*
286 * Callback routine that will print out a pool property value.
287 */
288static int
289print_prop_cb(int prop, void *cb)
290{
291 FILE *fp = cb;
292
428870ff 293 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
34dc7c2f
BB
294
295 if (zpool_prop_readonly(prop))
296 (void) fprintf(fp, " NO ");
297 else
428870ff 298 (void) fprintf(fp, " YES ");
34dc7c2f
BB
299
300 if (zpool_prop_values(prop) == NULL)
301 (void) fprintf(fp, "-\n");
302 else
303 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
304
305 return (ZPROP_CONT);
306}
307
308/*
309 * Display usage message. If we're inside a command, display only the usage for
310 * that command. Otherwise, iterate over the entire command table and display
311 * a complete usage message.
312 */
313void
314usage(boolean_t requested)
315{
316 FILE *fp = requested ? stdout : stderr;
317
318 if (current_command == NULL) {
319 int i;
320
321 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
322 (void) fprintf(fp,
323 gettext("where 'command' is one of the following:\n\n"));
324
325 for (i = 0; i < NCOMMAND; i++) {
326 if (command_table[i].name == NULL)
327 (void) fprintf(fp, "\n");
328 else
329 (void) fprintf(fp, "%s",
330 get_usage(command_table[i].usage));
331 }
332 } else {
333 (void) fprintf(fp, gettext("usage:\n"));
334 (void) fprintf(fp, "%s", get_usage(current_command->usage));
335 }
336
337 if (current_command != NULL &&
338 ((strcmp(current_command->name, "set") == 0) ||
339 (strcmp(current_command->name, "get") == 0) ||
340 (strcmp(current_command->name, "list") == 0))) {
341
342 (void) fprintf(fp,
343 gettext("\nthe following properties are supported:\n"));
344
428870ff 345 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
34dc7c2f
BB
346 "PROPERTY", "EDIT", "VALUES");
347
348 /* Iterate over all properties */
349 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
350 ZFS_TYPE_POOL);
9ae529ec
CS
351
352 (void) fprintf(fp, "\t%-15s ", "feature@...");
353 (void) fprintf(fp, "YES disabled | enabled | active\n");
354
355 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
356 "appended with a feature name.\nSee zpool-features(5).\n"));
34dc7c2f
BB
357 }
358
359 /*
360 * See comments at end of main().
361 */
362 if (getenv("ZFS_ABORT") != NULL) {
363 (void) printf("dumping core by request\n");
364 abort();
365 }
366
367 exit(requested ? 0 : 2);
368}
369
370void
371print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
372 boolean_t print_logs)
373{
374 nvlist_t **child;
375 uint_t c, children;
376 char *vname;
377
378 if (name != NULL)
379 (void) printf("\t%*s%s\n", indent, "", name);
380
381 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
382 &child, &children) != 0)
383 return;
384
385 for (c = 0; c < children; c++) {
386 uint64_t is_log = B_FALSE;
387
388 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
389 &is_log);
390 if ((is_log && !print_logs) || (!is_log && print_logs))
391 continue;
392
428870ff 393 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
34dc7c2f
BB
394 print_vdev_tree(zhp, vname, child[c], indent + 2,
395 B_FALSE);
396 free(vname);
397 }
398}
399
b9b24bb4
CS
400static boolean_t
401prop_list_contains_feature(nvlist_t *proplist)
402{
403 nvpair_t *nvp;
404 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
405 nvp = nvlist_next_nvpair(proplist, nvp)) {
406 if (zpool_prop_feature(nvpair_name(nvp)))
407 return (B_TRUE);
408 }
409 return (B_FALSE);
410}
411
34dc7c2f
BB
412/*
413 * Add a property pair (name, string-value) into a property nvlist.
414 */
415static int
b128c09f
BB
416add_prop_list(const char *propname, char *propval, nvlist_t **props,
417 boolean_t poolprop)
34dc7c2f 418{
b128c09f
BB
419 zpool_prop_t prop = ZPROP_INVAL;
420 zfs_prop_t fprop;
34dc7c2f 421 nvlist_t *proplist;
b128c09f
BB
422 const char *normnm;
423 char *strval;
34dc7c2f
BB
424
425 if (*props == NULL &&
426 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
427 (void) fprintf(stderr,
428 gettext("internal error: out of memory\n"));
429 return (1);
430 }
431
432 proplist = *props;
433
b128c09f 434 if (poolprop) {
b9b24bb4
CS
435 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
436
9ae529ec
CS
437 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
438 !zpool_prop_feature(propname)) {
b128c09f
BB
439 (void) fprintf(stderr, gettext("property '%s' is "
440 "not a valid pool property\n"), propname);
441 return (2);
442 }
b9b24bb4
CS
443
444 /*
445 * feature@ properties and version should not be specified
446 * at the same time.
447 */
448 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
449 nvlist_exists(proplist, vname)) ||
450 (prop == ZPOOL_PROP_VERSION &&
451 prop_list_contains_feature(proplist))) {
452 (void) fprintf(stderr, gettext("'feature@' and "
453 "'version' properties cannot be specified "
454 "together\n"));
455 return (2);
456 }
457
458
9ae529ec
CS
459 if (zpool_prop_feature(propname))
460 normnm = propname;
461 else
462 normnm = zpool_prop_to_name(prop);
b128c09f 463 } else {
9babb374
BB
464 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
465 normnm = zfs_prop_to_name(fprop);
466 } else {
467 normnm = propname;
b128c09f 468 }
34dc7c2f
BB
469 }
470
b128c09f
BB
471 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
472 prop != ZPOOL_PROP_CACHEFILE) {
34dc7c2f
BB
473 (void) fprintf(stderr, gettext("property '%s' "
474 "specified multiple times\n"), propname);
475 return (2);
476 }
477
b128c09f 478 if (nvlist_add_string(proplist, normnm, propval) != 0) {
34dc7c2f
BB
479 (void) fprintf(stderr, gettext("internal "
480 "error: out of memory\n"));
481 return (1);
482 }
483
484 return (0);
485}
486
487/*
df831108 488 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
34dc7c2f
BB
489 *
490 * -f Force addition of devices, even if they appear in use
491 * -n Do not add the devices, but display the resulting layout if
492 * they were to be added.
df831108 493 * -o Set property=value.
34dc7c2f
BB
494 *
495 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
496 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
497 * libzfs.
498 */
499int
500zpool_do_add(int argc, char **argv)
501{
502 boolean_t force = B_FALSE;
503 boolean_t dryrun = B_FALSE;
504 int c;
505 nvlist_t *nvroot;
506 char *poolname;
507 int ret;
508 zpool_handle_t *zhp;
509 nvlist_t *config;
df831108
CP
510 nvlist_t *props = NULL;
511 char *propval;
34dc7c2f
BB
512
513 /* check options */
df831108 514 while ((c = getopt(argc, argv, "fno:")) != -1) {
34dc7c2f
BB
515 switch (c) {
516 case 'f':
517 force = B_TRUE;
518 break;
519 case 'n':
520 dryrun = B_TRUE;
521 break;
df831108
CP
522 case 'o':
523 if ((propval = strchr(optarg, '=')) == NULL) {
524 (void) fprintf(stderr, gettext("missing "
525 "'=' for -o option\n"));
526 usage(B_FALSE);
527 }
528 *propval = '\0';
529 propval++;
530
531 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
532 (add_prop_list(optarg, propval, &props, B_TRUE)))
533 usage(B_FALSE);
534 break;
34dc7c2f
BB
535 case '?':
536 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
537 optopt);
538 usage(B_FALSE);
539 }
540 }
541
542 argc -= optind;
543 argv += optind;
544
545 /* get pool name and check number of arguments */
546 if (argc < 1) {
547 (void) fprintf(stderr, gettext("missing pool name argument\n"));
548 usage(B_FALSE);
549 }
550 if (argc < 2) {
551 (void) fprintf(stderr, gettext("missing vdev specification\n"));
552 usage(B_FALSE);
553 }
554
555 poolname = argv[0];
556
557 argc--;
558 argv++;
559
560 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
561 return (1);
562
563 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
564 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
565 poolname);
566 zpool_close(zhp);
567 return (1);
568 }
569
570 /* pass off to get_vdev_spec for processing */
df831108 571 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
b128c09f 572 argc, argv);
34dc7c2f
BB
573 if (nvroot == NULL) {
574 zpool_close(zhp);
575 return (1);
576 }
577
578 if (dryrun) {
579 nvlist_t *poolnvroot;
580
581 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
582 &poolnvroot) == 0);
583
584 (void) printf(gettext("would update '%s' to the following "
585 "configuration:\n"), zpool_get_name(zhp));
586
587 /* print original main pool and new tree */
588 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
589 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
590
591 /* Do the same for the logs */
592 if (num_logs(poolnvroot) > 0) {
593 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
594 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
595 } else if (num_logs(nvroot) > 0) {
596 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
597 }
598
599 ret = 0;
600 } else {
601 ret = (zpool_add(zhp, nvroot) != 0);
602 }
603
df831108 604 nvlist_free(props);
34dc7c2f
BB
605 nvlist_free(nvroot);
606 zpool_close(zhp);
607
608 return (ret);
609}
610
611/*
428870ff 612 * zpool remove <pool> <vdev> ...
34dc7c2f 613 *
428870ff
BB
614 * Removes the given vdev from the pool. Currently, this supports removing
615 * spares, cache, and log devices from the pool.
34dc7c2f
BB
616 */
617int
618zpool_do_remove(int argc, char **argv)
619{
620 char *poolname;
621 int i, ret = 0;
622 zpool_handle_t *zhp;
623
624 argc--;
625 argv++;
626
627 /* get pool name and check number of arguments */
628 if (argc < 1) {
629 (void) fprintf(stderr, gettext("missing pool name argument\n"));
630 usage(B_FALSE);
631 }
632 if (argc < 2) {
633 (void) fprintf(stderr, gettext("missing device\n"));
634 usage(B_FALSE);
635 }
636
637 poolname = argv[0];
638
639 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
640 return (1);
641
642 for (i = 1; i < argc; i++) {
643 if (zpool_vdev_remove(zhp, argv[i]) != 0)
644 ret = 1;
645 }
646
647 return (ret);
648}
649
131cc95c
DK
650/*
651 * zpool labelclear <vdev>
652 *
653 * Verifies that the vdev is not active and zeros out the label information
654 * on the device.
655 */
656int
657zpool_do_labelclear(int argc, char **argv)
658{
659 char *vdev, *name;
660 int c, fd = -1, ret = 0;
661 pool_state_t state;
662 boolean_t inuse = B_FALSE;
663 boolean_t force = B_FALSE;
664
665 /* check options */
666 while ((c = getopt(argc, argv, "f")) != -1) {
667 switch (c) {
668 case 'f':
669 force = B_TRUE;
670 break;
671 default:
672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
673 optopt);
674 usage(B_FALSE);
675 }
676 }
677
678 argc -= optind;
679 argv += optind;
680
681 /* get vdev name */
682 if (argc < 1) {
683 (void) fprintf(stderr, gettext("missing vdev device name\n"));
684 usage(B_FALSE);
685 }
686
687 vdev = argv[0];
688 if ((fd = open(vdev, O_RDWR)) < 0) {
689 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
690 return (B_FALSE);
691 }
692
693 name = NULL;
694 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
695 if (force)
696 goto wipe_label;
697
698 (void) fprintf(stderr,
699 gettext("Unable to determine pool state for %s\n"
700 "Use -f to force the clearing any label data\n"), vdev);
701
702 return (1);
703 }
704
705 if (inuse) {
706 switch (state) {
707 default:
708 case POOL_STATE_ACTIVE:
709 case POOL_STATE_SPARE:
710 case POOL_STATE_L2CACHE:
711 (void) fprintf(stderr,
712 gettext("labelclear operation failed.\n"
713 "\tVdev %s is a member (%s), of pool \"%s\".\n"
714 "\tTo remove label information from this device, "
715 "export or destroy\n\tthe pool, or remove %s from "
716 "the configuration of this pool\n\tand retry the "
717 "labelclear operation.\n"),
718 vdev, zpool_pool_state_to_name(state), name, vdev);
719 ret = 1;
720 goto errout;
721
722 case POOL_STATE_EXPORTED:
723 if (force)
724 break;
725
726 (void) fprintf(stderr,
727 gettext("labelclear operation failed.\n\tVdev "
728 "%s is a member of the exported pool \"%s\".\n"
729 "\tUse \"zpool labelclear -f %s\" to force the "
730 "removal of label\n\tinformation.\n"),
731 vdev, name, vdev);
732 ret = 1;
733 goto errout;
734
735 case POOL_STATE_POTENTIALLY_ACTIVE:
736 if (force)
737 break;
738
739 (void) fprintf(stderr,
740 gettext("labelclear operation failed.\n"
741 "\tVdev %s is a member of the pool \"%s\".\n"
742 "\tThis pool is unknown to this system, but may "
743 "be active on\n\tanother system. Use "
744 "\'zpool labelclear -f %s\' to force the\n"
745 "\tremoval of label information.\n"),
746 vdev, name, vdev);
747 ret = 1;
748 goto errout;
749
750 case POOL_STATE_DESTROYED:
751 /* inuse should never be set for a destroyed pool... */
752 break;
753 }
754 }
755
756wipe_label:
757 if (zpool_clear_label(fd) != 0) {
758 (void) fprintf(stderr,
759 gettext("Label clear failed on vdev %s\n"), vdev);
760 ret = 1;
761 }
762
763errout:
764 close(fd);
765 if (name != NULL)
766 free(name);
767
768 return (ret);
769}
770
34dc7c2f 771/*
9ae529ec 772 * zpool create [-fnd] [-o property=value] ...
b128c09f
BB
773 * [-O file-system-property=value] ...
774 * [-R root] [-m mountpoint] <pool> <dev> ...
34dc7c2f
BB
775 *
776 * -f Force creation, even if devices appear in use
777 * -n Do not create the pool, but display the resulting layout if it
778 * were to be created.
779 * -R Create a pool under an alternate root
780 * -m Set default mountpoint for the root dataset. By default it's
9ae529ec 781 * '/<pool>'
34dc7c2f 782 * -o Set property=value.
9ae529ec
CS
783 * -d Don't automatically enable all supported pool features
784 * (individual features can be enabled with -o).
b128c09f 785 * -O Set fsproperty=value in the pool's root file system
34dc7c2f
BB
786 *
787 * Creates the named pool according to the given vdev specification. The
788 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
789 * we get the nvlist back from get_vdev_spec(), we either print out the contents
790 * (if '-n' was specified), or pass it to libzfs to do the creation.
791 */
792int
793zpool_do_create(int argc, char **argv)
794{
795 boolean_t force = B_FALSE;
796 boolean_t dryrun = B_FALSE;
9ae529ec 797 boolean_t enable_all_pool_feat = B_TRUE;
34dc7c2f
BB
798 int c;
799 nvlist_t *nvroot = NULL;
800 char *poolname;
801 int ret = 1;
802 char *altroot = NULL;
803 char *mountpoint = NULL;
b128c09f 804 nvlist_t *fsprops = NULL;
34dc7c2f
BB
805 nvlist_t *props = NULL;
806 char *propval;
807
808 /* check options */
9ae529ec 809 while ((c = getopt(argc, argv, ":fndR:m:o:O:")) != -1) {
34dc7c2f
BB
810 switch (c) {
811 case 'f':
812 force = B_TRUE;
813 break;
814 case 'n':
815 dryrun = B_TRUE;
816 break;
9ae529ec
CS
817 case 'd':
818 enable_all_pool_feat = B_FALSE;
819 break;
34dc7c2f
BB
820 case 'R':
821 altroot = optarg;
822 if (add_prop_list(zpool_prop_to_name(
b128c09f 823 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
34dc7c2f
BB
824 goto errout;
825 if (nvlist_lookup_string(props,
826 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
827 &propval) == 0)
828 break;
829 if (add_prop_list(zpool_prop_to_name(
b128c09f 830 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
34dc7c2f
BB
831 goto errout;
832 break;
833 case 'm':
7bc7f250 834 /* Equivalent to -O mountpoint=optarg */
34dc7c2f
BB
835 mountpoint = optarg;
836 break;
837 case 'o':
838 if ((propval = strchr(optarg, '=')) == NULL) {
839 (void) fprintf(stderr, gettext("missing "
840 "'=' for -o option\n"));
841 goto errout;
842 }
843 *propval = '\0';
844 propval++;
845
b128c09f
BB
846 if (add_prop_list(optarg, propval, &props, B_TRUE))
847 goto errout;
9ae529ec
CS
848
849 /*
850 * If the user is creating a pool that doesn't support
851 * feature flags, don't enable any features.
852 */
853 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
854 char *end;
855 u_longlong_t ver;
856
857 ver = strtoull(propval, &end, 10);
858 if (*end == '\0' &&
859 ver < SPA_VERSION_FEATURES) {
860 enable_all_pool_feat = B_FALSE;
861 }
862 }
b128c09f
BB
863 break;
864 case 'O':
865 if ((propval = strchr(optarg, '=')) == NULL) {
866 (void) fprintf(stderr, gettext("missing "
867 "'=' for -O option\n"));
868 goto errout;
869 }
870 *propval = '\0';
871 propval++;
872
7bc7f250
WA
873 /*
874 * Mountpoints are checked and then added later.
875 * Uniquely among properties, they can be specified
876 * more than once, to avoid conflict with -m.
877 */
878 if (0 == strcmp(optarg,
879 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
880 mountpoint = propval;
881 } else if (add_prop_list(optarg, propval, &fsprops,
882 B_FALSE)) {
34dc7c2f 883 goto errout;
7bc7f250 884 }
34dc7c2f
BB
885 break;
886 case ':':
887 (void) fprintf(stderr, gettext("missing argument for "
888 "'%c' option\n"), optopt);
889 goto badusage;
890 case '?':
891 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
892 optopt);
893 goto badusage;
894 }
895 }
896
897 argc -= optind;
898 argv += optind;
899
900 /* get pool name and check number of arguments */
901 if (argc < 1) {
902 (void) fprintf(stderr, gettext("missing pool name argument\n"));
903 goto badusage;
904 }
905 if (argc < 2) {
906 (void) fprintf(stderr, gettext("missing vdev specification\n"));
907 goto badusage;
908 }
909
910 poolname = argv[0];
911
912 /*
913 * As a special case, check for use of '/' in the name, and direct the
914 * user to use 'zfs create' instead.
915 */
916 if (strchr(poolname, '/') != NULL) {
917 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
918 "character '/' in pool name\n"), poolname);
919 (void) fprintf(stderr, gettext("use 'zfs create' to "
920 "create a dataset\n"));
921 goto errout;
922 }
923
924 /* pass off to get_vdev_spec for bulk processing */
df30f566 925 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
b128c09f 926 argc - 1, argv + 1);
34dc7c2f 927 if (nvroot == NULL)
b128c09f 928 goto errout;
34dc7c2f
BB
929
930 /* make_root_vdev() allows 0 toplevel children if there are spares */
931 if (!zfs_allocatable_devs(nvroot)) {
932 (void) fprintf(stderr, gettext("invalid vdev "
933 "specification: at least one toplevel vdev must be "
934 "specified\n"));
935 goto errout;
936 }
937
34dc7c2f
BB
938 if (altroot != NULL && altroot[0] != '/') {
939 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
940 "must be an absolute path\n"), altroot);
941 goto errout;
942 }
943
944 /*
945 * Check the validity of the mountpoint and direct the user to use the
946 * '-m' mountpoint option if it looks like its in use.
947 */
948 if (mountpoint == NULL ||
949 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
950 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
951 char buf[MAXPATHLEN];
952 DIR *dirp;
953
954 if (mountpoint && mountpoint[0] != '/') {
955 (void) fprintf(stderr, gettext("invalid mountpoint "
956 "'%s': must be an absolute path, 'legacy', or "
957 "'none'\n"), mountpoint);
958 goto errout;
959 }
960
961 if (mountpoint == NULL) {
962 if (altroot != NULL)
963 (void) snprintf(buf, sizeof (buf), "%s/%s",
964 altroot, poolname);
965 else
966 (void) snprintf(buf, sizeof (buf), "/%s",
967 poolname);
968 } else {
969 if (altroot != NULL)
970 (void) snprintf(buf, sizeof (buf), "%s%s",
971 altroot, mountpoint);
972 else
973 (void) snprintf(buf, sizeof (buf), "%s",
974 mountpoint);
975 }
976
977 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
978 (void) fprintf(stderr, gettext("mountpoint '%s' : "
979 "%s\n"), buf, strerror(errno));
980 (void) fprintf(stderr, gettext("use '-m' "
981 "option to provide a different default\n"));
982 goto errout;
983 } else if (dirp) {
984 int count = 0;
985
986 while (count < 3 && readdir(dirp) != NULL)
987 count++;
988 (void) closedir(dirp);
989
990 if (count > 2) {
991 (void) fprintf(stderr, gettext("mountpoint "
992 "'%s' exists and is not empty\n"), buf);
993 (void) fprintf(stderr, gettext("use '-m' "
994 "option to provide a "
995 "different default\n"));
996 goto errout;
997 }
998 }
999 }
1000
7bc7f250
WA
1001 /*
1002 * Now that the mountpoint's validity has been checked, ensure that
1003 * the property is set appropriately prior to creating the pool.
1004 */
1005 if (mountpoint != NULL) {
1006 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1007 mountpoint, &fsprops, B_FALSE);
1008 if (ret != 0)
1009 goto errout;
1010 }
1011
1012 ret = 1;
34dc7c2f
BB
1013 if (dryrun) {
1014 /*
1015 * For a dry run invocation, print out a basic message and run
1016 * through all the vdevs in the list and print out in an
1017 * appropriate hierarchy.
1018 */
1019 (void) printf(gettext("would create '%s' with the "
1020 "following layout:\n\n"), poolname);
1021
1022 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1023 if (num_logs(nvroot) > 0)
1024 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1025
1026 ret = 0;
1027 } else {
1028 /*
1029 * Hand off to libzfs.
1030 */
9ae529ec
CS
1031 if (enable_all_pool_feat) {
1032 int i;
1033 for (i = 0; i < SPA_FEATURES; i++) {
1034 char propname[MAXPATHLEN];
1035 zfeature_info_t *feat = &spa_feature_table[i];
1036
1037 (void) snprintf(propname, sizeof (propname),
1038 "feature@%s", feat->fi_uname);
1039
1040 /*
1041 * Skip feature if user specified it manually
1042 * on the command line.
1043 */
1044 if (nvlist_exists(props, propname))
1045 continue;
1046
7bc7f250
WA
1047 ret = add_prop_list(propname,
1048 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1049 if (ret != 0)
9ae529ec
CS
1050 goto errout;
1051 }
1052 }
7bc7f250
WA
1053
1054 ret = 1;
b128c09f
BB
1055 if (zpool_create(g_zfs, poolname,
1056 nvroot, props, fsprops) == 0) {
34dc7c2f
BB
1057 zfs_handle_t *pool = zfs_open(g_zfs, poolname,
1058 ZFS_TYPE_FILESYSTEM);
1059 if (pool != NULL) {
34dc7c2f
BB
1060 if (zfs_mount(pool, NULL, 0) == 0)
1061 ret = zfs_shareall(pool);
1062 zfs_close(pool);
1063 }
1064 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1065 (void) fprintf(stderr, gettext("pool name may have "
1066 "been omitted\n"));
1067 }
1068 }
1069
1070errout:
1071 nvlist_free(nvroot);
b128c09f 1072 nvlist_free(fsprops);
34dc7c2f
BB
1073 nvlist_free(props);
1074 return (ret);
1075badusage:
b128c09f 1076 nvlist_free(fsprops);
34dc7c2f
BB
1077 nvlist_free(props);
1078 usage(B_FALSE);
1079 return (2);
1080}
1081
1082/*
1083 * zpool destroy <pool>
1084 *
1085 * -f Forcefully unmount any datasets
1086 *
1087 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1088 */
1089int
1090zpool_do_destroy(int argc, char **argv)
1091{
1092 boolean_t force = B_FALSE;
1093 int c;
1094 char *pool;
1095 zpool_handle_t *zhp;
1096 int ret;
1097
1098 /* check options */
1099 while ((c = getopt(argc, argv, "f")) != -1) {
1100 switch (c) {
1101 case 'f':
1102 force = B_TRUE;
1103 break;
1104 case '?':
1105 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1106 optopt);
1107 usage(B_FALSE);
1108 }
1109 }
1110
1111 argc -= optind;
1112 argv += optind;
1113
1114 /* check arguments */
1115 if (argc < 1) {
1116 (void) fprintf(stderr, gettext("missing pool argument\n"));
1117 usage(B_FALSE);
1118 }
1119 if (argc > 1) {
1120 (void) fprintf(stderr, gettext("too many arguments\n"));
1121 usage(B_FALSE);
1122 }
1123
1124 pool = argv[0];
1125
1126 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1127 /*
1128 * As a special case, check for use of '/' in the name, and
1129 * direct the user to use 'zfs destroy' instead.
1130 */
1131 if (strchr(pool, '/') != NULL)
1132 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1133 "destroy a dataset\n"));
1134 return (1);
1135 }
1136
1137 if (zpool_disable_datasets(zhp, force) != 0) {
1138 (void) fprintf(stderr, gettext("could not destroy '%s': "
1139 "could not unmount datasets\n"), zpool_get_name(zhp));
1140 return (1);
1141 }
1142
6f1ffb06
MA
1143 /* The history must be logged as part of the export */
1144 log_history = B_FALSE;
1145
1146 ret = (zpool_destroy(zhp, history_str) != 0);
34dc7c2f
BB
1147
1148 zpool_close(zhp);
1149
1150 return (ret);
1151}
1152
1153/*
1154 * zpool export [-f] <pool> ...
1155 *
1156 * -f Forcefully unmount datasets
1157 *
1158 * Export the given pools. By default, the command will attempt to cleanly
1159 * unmount any active datasets within the pool. If the '-f' flag is specified,
1160 * then the datasets will be forcefully unmounted.
1161 */
1162int
1163zpool_do_export(int argc, char **argv)
1164{
1165 boolean_t force = B_FALSE;
fb5f0bc8 1166 boolean_t hardforce = B_FALSE;
34dc7c2f
BB
1167 int c;
1168 zpool_handle_t *zhp;
1169 int ret;
1170 int i;
1171
1172 /* check options */
fb5f0bc8 1173 while ((c = getopt(argc, argv, "fF")) != -1) {
34dc7c2f
BB
1174 switch (c) {
1175 case 'f':
1176 force = B_TRUE;
1177 break;
fb5f0bc8
BB
1178 case 'F':
1179 hardforce = B_TRUE;
1180 break;
34dc7c2f
BB
1181 case '?':
1182 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1183 optopt);
1184 usage(B_FALSE);
1185 }
1186 }
1187
1188 argc -= optind;
1189 argv += optind;
1190
1191 /* check arguments */
1192 if (argc < 1) {
1193 (void) fprintf(stderr, gettext("missing pool argument\n"));
1194 usage(B_FALSE);
1195 }
1196
1197 ret = 0;
1198 for (i = 0; i < argc; i++) {
1199 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) == NULL) {
1200 ret = 1;
1201 continue;
1202 }
1203
1204 if (zpool_disable_datasets(zhp, force) != 0) {
1205 ret = 1;
1206 zpool_close(zhp);
1207 continue;
1208 }
1209
6f1ffb06
MA
1210 /* The history must be logged as part of the export */
1211 log_history = B_FALSE;
1212
fb5f0bc8 1213 if (hardforce) {
6f1ffb06 1214 if (zpool_export_force(zhp, history_str) != 0)
fb5f0bc8 1215 ret = 1;
6f1ffb06 1216 } else if (zpool_export(zhp, force, history_str) != 0) {
34dc7c2f 1217 ret = 1;
fb5f0bc8 1218 }
34dc7c2f
BB
1219
1220 zpool_close(zhp);
1221 }
1222
1223 return (ret);
1224}
1225
1226/*
1227 * Given a vdev configuration, determine the maximum width needed for the device
1228 * name column.
1229 */
1230static int
1231max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1232{
428870ff 1233 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
34dc7c2f
BB
1234 nvlist_t **child;
1235 uint_t c, children;
1236 int ret;
1237
1238 if (strlen(name) + depth > max)
1239 max = strlen(name) + depth;
1240
1241 free(name);
1242
1243 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1244 &child, &children) == 0) {
1245 for (c = 0; c < children; c++)
1246 if ((ret = max_width(zhp, child[c], depth + 2,
1247 max)) > max)
1248 max = ret;
1249 }
1250
1251 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1252 &child, &children) == 0) {
1253 for (c = 0; c < children; c++)
1254 if ((ret = max_width(zhp, child[c], depth + 2,
1255 max)) > max)
1256 max = ret;
1257 }
1258
1259 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1260 &child, &children) == 0) {
1261 for (c = 0; c < children; c++)
1262 if ((ret = max_width(zhp, child[c], depth + 2,
1263 max)) > max)
1264 max = ret;
1265 }
1266
1267
1268 return (max);
1269}
1270
9babb374
BB
1271typedef struct spare_cbdata {
1272 uint64_t cb_guid;
1273 zpool_handle_t *cb_zhp;
1274} spare_cbdata_t;
1275
1276static boolean_t
1277find_vdev(nvlist_t *nv, uint64_t search)
1278{
1279 uint64_t guid;
1280 nvlist_t **child;
1281 uint_t c, children;
1282
1283 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1284 search == guid)
1285 return (B_TRUE);
1286
1287 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1288 &child, &children) == 0) {
1289 for (c = 0; c < children; c++)
1290 if (find_vdev(child[c], search))
1291 return (B_TRUE);
1292 }
1293
1294 return (B_FALSE);
1295}
1296
1297static int
1298find_spare(zpool_handle_t *zhp, void *data)
1299{
1300 spare_cbdata_t *cbp = data;
1301 nvlist_t *config, *nvroot;
1302
1303 config = zpool_get_config(zhp, NULL);
1304 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1305 &nvroot) == 0);
1306
1307 if (find_vdev(nvroot, cbp->cb_guid)) {
1308 cbp->cb_zhp = zhp;
1309 return (1);
1310 }
1311
1312 zpool_close(zhp);
1313 return (0);
1314}
1315
1316/*
1317 * Print out configuration state as requested by status_callback.
1318 */
1319void
1320print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1321 int namewidth, int depth, boolean_t isspare)
1322{
1323 nvlist_t **child;
1324 uint_t c, children;
428870ff 1325 pool_scan_stat_t *ps = NULL;
9babb374 1326 vdev_stat_t *vs;
428870ff 1327 char rbuf[6], wbuf[6], cbuf[6];
9babb374
BB
1328 char *vname;
1329 uint64_t notpresent;
1330 spare_cbdata_t cb;
1331 char *state;
1332
9babb374
BB
1333 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1334 &child, &children) != 0)
1335 children = 0;
1336
428870ff
BB
1337 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1338 (uint64_t **)&vs, &c) == 0);
1339
9babb374
BB
1340 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1341 if (isspare) {
1342 /*
1343 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1344 * online drives.
1345 */
1346 if (vs->vs_aux == VDEV_AUX_SPARED)
1347 state = "INUSE";
1348 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1349 state = "AVAIL";
1350 }
1351
1352 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1353 name, state);
1354
1355 if (!isspare) {
1356 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1357 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1358 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1359 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1360 }
1361
1362 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1363 &notpresent) == 0) {
1364 char *path;
1365 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1366 (void) printf(" was %s", path);
1367 } else if (vs->vs_aux != 0) {
1368 (void) printf(" ");
1369
1370 switch (vs->vs_aux) {
1371 case VDEV_AUX_OPEN_FAILED:
1372 (void) printf(gettext("cannot open"));
1373 break;
1374
1375 case VDEV_AUX_BAD_GUID_SUM:
1376 (void) printf(gettext("missing device"));
1377 break;
1378
1379 case VDEV_AUX_NO_REPLICAS:
1380 (void) printf(gettext("insufficient replicas"));
1381 break;
1382
1383 case VDEV_AUX_VERSION_NEWER:
1384 (void) printf(gettext("newer version"));
1385 break;
1386
9ae529ec
CS
1387 case VDEV_AUX_UNSUP_FEAT:
1388 (void) printf(gettext("unsupported feature(s)"));
1389 break;
1390
9babb374
BB
1391 case VDEV_AUX_SPARED:
1392 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1393 &cb.cb_guid) == 0);
1394 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1395 if (strcmp(zpool_get_name(cb.cb_zhp),
1396 zpool_get_name(zhp)) == 0)
1397 (void) printf(gettext("currently in "
1398 "use"));
1399 else
1400 (void) printf(gettext("in use by "
1401 "pool '%s'"),
1402 zpool_get_name(cb.cb_zhp));
1403 zpool_close(cb.cb_zhp);
1404 } else {
1405 (void) printf(gettext("currently in use"));
1406 }
1407 break;
1408
1409 case VDEV_AUX_ERR_EXCEEDED:
1410 (void) printf(gettext("too many errors"));
1411 break;
1412
1413 case VDEV_AUX_IO_FAILURE:
1414 (void) printf(gettext("experienced I/O failures"));
1415 break;
1416
1417 case VDEV_AUX_BAD_LOG:
1418 (void) printf(gettext("bad intent log"));
1419 break;
1420
428870ff
BB
1421 case VDEV_AUX_EXTERNAL:
1422 (void) printf(gettext("external device fault"));
1423 break;
1424
1425 case VDEV_AUX_SPLIT_POOL:
1426 (void) printf(gettext("split into new pool"));
1427 break;
1428
9babb374
BB
1429 default:
1430 (void) printf(gettext("corrupted data"));
1431 break;
1432 }
428870ff
BB
1433 }
1434
1435 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1436 (uint64_t **)&ps, &c);
1437
1438 if (ps && ps->pss_state == DSS_SCANNING &&
1439 vs->vs_scan_processed != 0 && children == 0) {
1440 (void) printf(gettext(" (%s)"),
1441 (ps->pss_func == POOL_SCAN_RESILVER) ?
1442 "resilvering" : "repairing");
9babb374
BB
1443 }
1444
1445 (void) printf("\n");
1446
1447 for (c = 0; c < children; c++) {
428870ff 1448 uint64_t islog = B_FALSE, ishole = B_FALSE;
9babb374 1449
428870ff 1450 /* Don't print logs or holes here */
9babb374 1451 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
428870ff
BB
1452 &islog);
1453 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1454 &ishole);
1455 if (islog || ishole)
9babb374 1456 continue;
428870ff 1457 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
9babb374
BB
1458 print_status_config(zhp, vname, child[c],
1459 namewidth, depth + 2, isspare);
1460 free(vname);
1461 }
1462}
1463
34dc7c2f
BB
1464
1465/*
1466 * Print the configuration of an exported pool. Iterate over all vdevs in the
1467 * pool, printing out the name and status for each one.
1468 */
1469void
9babb374 1470print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
34dc7c2f
BB
1471{
1472 nvlist_t **child;
1473 uint_t c, children;
1474 vdev_stat_t *vs;
1475 char *type, *vname;
1476
1477 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
428870ff
BB
1478 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1479 strcmp(type, VDEV_TYPE_HOLE) == 0)
34dc7c2f
BB
1480 return;
1481
428870ff 1482 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
1483 (uint64_t **)&vs, &c) == 0);
1484
1485 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1486 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1487
1488 if (vs->vs_aux != 0) {
1489 (void) printf(" ");
1490
1491 switch (vs->vs_aux) {
1492 case VDEV_AUX_OPEN_FAILED:
1493 (void) printf(gettext("cannot open"));
1494 break;
1495
1496 case VDEV_AUX_BAD_GUID_SUM:
1497 (void) printf(gettext("missing device"));
1498 break;
1499
1500 case VDEV_AUX_NO_REPLICAS:
1501 (void) printf(gettext("insufficient replicas"));
1502 break;
1503
1504 case VDEV_AUX_VERSION_NEWER:
1505 (void) printf(gettext("newer version"));
1506 break;
1507
9ae529ec
CS
1508 case VDEV_AUX_UNSUP_FEAT:
1509 (void) printf(gettext("unsupported feature(s)"));
1510 break;
1511
34dc7c2f
BB
1512 case VDEV_AUX_ERR_EXCEEDED:
1513 (void) printf(gettext("too many errors"));
1514 break;
1515
1516 default:
1517 (void) printf(gettext("corrupted data"));
1518 break;
1519 }
1520 }
1521 (void) printf("\n");
1522
1523 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1524 &child, &children) != 0)
1525 return;
1526
1527 for (c = 0; c < children; c++) {
1528 uint64_t is_log = B_FALSE;
1529
1530 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1531 &is_log);
9babb374 1532 if (is_log)
34dc7c2f
BB
1533 continue;
1534
428870ff 1535 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
9babb374 1536 print_import_config(vname, child[c], namewidth, depth + 2);
34dc7c2f
BB
1537 free(vname);
1538 }
1539
1540 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1541 &child, &children) == 0) {
1542 (void) printf(gettext("\tcache\n"));
1543 for (c = 0; c < children; c++) {
428870ff 1544 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
34dc7c2f
BB
1545 (void) printf("\t %s\n", vname);
1546 free(vname);
1547 }
1548 }
1549
1550 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1551 &child, &children) == 0) {
1552 (void) printf(gettext("\tspares\n"));
1553 for (c = 0; c < children; c++) {
428870ff 1554 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
34dc7c2f
BB
1555 (void) printf("\t %s\n", vname);
1556 free(vname);
1557 }
1558 }
1559}
1560
9babb374
BB
1561/*
1562 * Print log vdevs.
1563 * Logs are recorded as top level vdevs in the main pool child array
1564 * but with "is_log" set to 1. We use either print_status_config() or
1565 * print_import_config() to print the top level logs then any log
1566 * children (eg mirrored slogs) are printed recursively - which
1567 * works because only the top level vdev is marked "is_log"
1568 */
1569static void
1570print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1571{
1572 uint_t c, children;
1573 nvlist_t **child;
1574
1575 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1576 &children) != 0)
1577 return;
1578
1579 (void) printf(gettext("\tlogs\n"));
1580
1581 for (c = 0; c < children; c++) {
1582 uint64_t is_log = B_FALSE;
1583 char *name;
1584
1585 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1586 &is_log);
1587 if (!is_log)
1588 continue;
428870ff 1589 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
9babb374
BB
1590 if (verbose)
1591 print_status_config(zhp, name, child[c], namewidth,
1592 2, B_FALSE);
1593 else
1594 print_import_config(name, child[c], namewidth, 2);
1595 free(name);
1596 }
1597}
428870ff 1598
34dc7c2f
BB
1599/*
1600 * Display the status for the given pool.
1601 */
1602static void
1603show_import(nvlist_t *config)
1604{
1605 uint64_t pool_state;
1606 vdev_stat_t *vs;
1607 char *name;
1608 uint64_t guid;
1609 char *msgid;
1610 nvlist_t *nvroot;
731782ec 1611 zpool_status_t reason;
ffe9d382 1612 zpool_errata_t errata;
34dc7c2f
BB
1613 const char *health;
1614 uint_t vsc;
1615 int namewidth;
d96eb2b1 1616 char *comment;
34dc7c2f
BB
1617
1618 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1619 &name) == 0);
1620 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1621 &guid) == 0);
1622 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1623 &pool_state) == 0);
1624 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1625 &nvroot) == 0);
1626
428870ff 1627 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
1628 (uint64_t **)&vs, &vsc) == 0);
1629 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1630
ffe9d382 1631 reason = zpool_import_status(config, &msgid, &errata);
34dc7c2f 1632
d96eb2b1
DM
1633 (void) printf(gettext(" pool: %s\n"), name);
1634 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1635 (void) printf(gettext(" state: %s"), health);
34dc7c2f
BB
1636 if (pool_state == POOL_STATE_DESTROYED)
1637 (void) printf(gettext(" (DESTROYED)"));
1638 (void) printf("\n");
1639
1640 switch (reason) {
1641 case ZPOOL_STATUS_MISSING_DEV_R:
1642 case ZPOOL_STATUS_MISSING_DEV_NR:
1643 case ZPOOL_STATUS_BAD_GUID_SUM:
d96eb2b1
DM
1644 (void) printf(gettext(" status: One or more devices are "
1645 "missing from the system.\n"));
34dc7c2f
BB
1646 break;
1647
1648 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1649 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
d96eb2b1 1650 (void) printf(gettext(" status: One or more devices contains "
34dc7c2f
BB
1651 "corrupted data.\n"));
1652 break;
1653
1654 case ZPOOL_STATUS_CORRUPT_DATA:
d96eb2b1
DM
1655 (void) printf(
1656 gettext(" status: The pool data is corrupted.\n"));
34dc7c2f
BB
1657 break;
1658
1659 case ZPOOL_STATUS_OFFLINE_DEV:
d96eb2b1 1660 (void) printf(gettext(" status: One or more devices "
34dc7c2f
BB
1661 "are offlined.\n"));
1662 break;
1663
1664 case ZPOOL_STATUS_CORRUPT_POOL:
d96eb2b1 1665 (void) printf(gettext(" status: The pool metadata is "
34dc7c2f
BB
1666 "corrupted.\n"));
1667 break;
1668
1669 case ZPOOL_STATUS_VERSION_OLDER:
b9b24bb4
CS
1670 (void) printf(gettext(" status: The pool is formatted using a "
1671 "legacy on-disk version.\n"));
34dc7c2f
BB
1672 break;
1673
1674 case ZPOOL_STATUS_VERSION_NEWER:
d96eb2b1 1675 (void) printf(gettext(" status: The pool is formatted using an "
34dc7c2f
BB
1676 "incompatible version.\n"));
1677 break;
b128c09f 1678
b9b24bb4
CS
1679 case ZPOOL_STATUS_FEAT_DISABLED:
1680 (void) printf(gettext(" status: Some supported features are "
1681 "not enabled on the pool.\n"));
1682 break;
1683
9ae529ec
CS
1684 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1685 (void) printf(gettext("status: The pool uses the following "
1686 "feature(s) not supported on this sytem:\n"));
1687 zpool_print_unsup_feat(config);
1688 break;
1689
1690 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1691 (void) printf(gettext("status: The pool can only be accessed "
1692 "in read-only mode on this system. It\n\tcannot be "
1693 "accessed in read-write mode because it uses the "
1694 "following\n\tfeature(s) not supported on this system:\n"));
1695 zpool_print_unsup_feat(config);
1696 break;
1697
34dc7c2f 1698 case ZPOOL_STATUS_HOSTID_MISMATCH:
d96eb2b1 1699 (void) printf(gettext(" status: The pool was last accessed by "
34dc7c2f
BB
1700 "another system.\n"));
1701 break;
b128c09f 1702
34dc7c2f
BB
1703 case ZPOOL_STATUS_FAULTED_DEV_R:
1704 case ZPOOL_STATUS_FAULTED_DEV_NR:
d96eb2b1 1705 (void) printf(gettext(" status: One or more devices are "
34dc7c2f
BB
1706 "faulted.\n"));
1707 break;
1708
b128c09f 1709 case ZPOOL_STATUS_BAD_LOG:
d96eb2b1 1710 (void) printf(gettext(" status: An intent log record cannot be "
b128c09f
BB
1711 "read.\n"));
1712 break;
1713
428870ff 1714 case ZPOOL_STATUS_RESILVERING:
d96eb2b1 1715 (void) printf(gettext(" status: One or more devices were being "
428870ff
BB
1716 "resilvered.\n"));
1717 break;
1718
ffe9d382
BB
1719 case ZPOOL_STATUS_ERRATA:
1720 (void) printf(gettext(" status: Errata #%d detected.\n"),
1721 errata);
1722 break;
1723
34dc7c2f
BB
1724 default:
1725 /*
1726 * No other status can be seen when importing pools.
1727 */
1728 assert(reason == ZPOOL_STATUS_OK);
1729 }
1730
1731 /*
1732 * Print out an action according to the overall state of the pool.
1733 */
1734 if (vs->vs_state == VDEV_STATE_HEALTHY) {
b9b24bb4
CS
1735 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1736 reason == ZPOOL_STATUS_FEAT_DISABLED) {
d96eb2b1 1737 (void) printf(gettext(" action: The pool can be "
34dc7c2f
BB
1738 "imported using its name or numeric identifier, "
1739 "though\n\tsome features will not be available "
1740 "without an explicit 'zpool upgrade'.\n"));
b9b24bb4 1741 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
d96eb2b1 1742 (void) printf(gettext(" action: The pool can be "
34dc7c2f
BB
1743 "imported using its name or numeric "
1744 "identifier and\n\tthe '-f' flag.\n"));
ffe9d382
BB
1745 } else if (reason == ZPOOL_STATUS_ERRATA) {
1746 switch (errata) {
1747 case ZPOOL_ERRATA_NONE:
1748 break;
1749
4f2dcb3e
RY
1750 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1751 (void) printf(gettext(" action: The pool can "
1752 "be imported using its name or numeric "
1753 "identifier,\n\thowever there is a compat"
1754 "ibility issue which should be corrected"
1755 "\n\tby running 'zpool scrub'\n"));
1756 break;
1757
1758 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1759 (void) printf(gettext(" action: The pool can"
1760 "not be imported with this version of ZFS "
1761 "due to\n\tan active asynchronous destroy. "
1762 "Revert to an earlier version\n\tand "
1763 "allow the destroy to complete before "
1764 "updating.\n"));
1765 break;
1766
ffe9d382
BB
1767 default:
1768 /*
1769 * All errata must contain an action message.
1770 */
1771 assert(0);
1772 }
b9b24bb4 1773 } else {
d96eb2b1 1774 (void) printf(gettext(" action: The pool can be "
34dc7c2f
BB
1775 "imported using its name or numeric "
1776 "identifier.\n"));
b9b24bb4 1777 }
34dc7c2f 1778 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
d96eb2b1 1779 (void) printf(gettext(" action: The pool can be imported "
34dc7c2f
BB
1780 "despite missing or damaged devices. The\n\tfault "
1781 "tolerance of the pool may be compromised if imported.\n"));
1782 } else {
1783 switch (reason) {
1784 case ZPOOL_STATUS_VERSION_NEWER:
d96eb2b1 1785 (void) printf(gettext(" action: The pool cannot be "
34dc7c2f
BB
1786 "imported. Access the pool on a system running "
1787 "newer\n\tsoftware, or recreate the pool from "
1788 "backup.\n"));
1789 break;
9ae529ec
CS
1790 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1791 (void) printf(gettext("action: The pool cannot be "
1792 "imported. Access the pool on a system that "
1793 "supports\n\tthe required feature(s), or recreate "
1794 "the pool from backup.\n"));
1795 break;
1796 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1797 (void) printf(gettext("action: The pool cannot be "
1798 "imported in read-write mode. Import the pool "
1799 "with\n"
1800 "\t\"-o readonly=on\", access the pool on a system "
1801 "that supports the\n\trequired feature(s), or "
1802 "recreate the pool from backup.\n"));
1803 break;
34dc7c2f
BB
1804 case ZPOOL_STATUS_MISSING_DEV_R:
1805 case ZPOOL_STATUS_MISSING_DEV_NR:
1806 case ZPOOL_STATUS_BAD_GUID_SUM:
d96eb2b1 1807 (void) printf(gettext(" action: The pool cannot be "
34dc7c2f
BB
1808 "imported. Attach the missing\n\tdevices and try "
1809 "again.\n"));
1810 break;
1811 default:
d96eb2b1 1812 (void) printf(gettext(" action: The pool cannot be "
34dc7c2f
BB
1813 "imported due to damaged devices or data.\n"));
1814 }
1815 }
1816
d96eb2b1
DM
1817 /* Print the comment attached to the pool. */
1818 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1819 (void) printf(gettext("comment: %s\n"), comment);
1820
34dc7c2f
BB
1821 /*
1822 * If the state is "closed" or "can't open", and the aux state
1823 * is "corrupt data":
1824 */
1825 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1826 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1827 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1828 if (pool_state == POOL_STATE_DESTROYED)
1829 (void) printf(gettext("\tThe pool was destroyed, "
1830 "but can be imported using the '-Df' flags.\n"));
1831 else if (pool_state != POOL_STATE_EXPORTED)
1832 (void) printf(gettext("\tThe pool may be active on "
1833 "another system, but can be imported using\n\t"
1834 "the '-f' flag.\n"));
1835 }
1836
1837 if (msgid != NULL)
3cee2262 1838 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
34dc7c2f
BB
1839 msgid);
1840
d96eb2b1 1841 (void) printf(gettext(" config:\n\n"));
34dc7c2f
BB
1842
1843 namewidth = max_width(NULL, nvroot, 0, 0);
1844 if (namewidth < 10)
1845 namewidth = 10;
1846
9babb374
BB
1847 print_import_config(name, nvroot, namewidth, 0);
1848 if (num_logs(nvroot) > 0)
1849 print_logs(NULL, nvroot, namewidth, B_FALSE);
34dc7c2f
BB
1850
1851 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1852 (void) printf(gettext("\n\tAdditional devices are known to "
1853 "be part of this pool, though their\n\texact "
1854 "configuration cannot be determined.\n"));
1855 }
1856}
1857
1858/*
1859 * Perform the import for the given configuration. This passes the heavy
1860 * lifting off to zpool_import_props(), and then mounts the datasets contained
1861 * within the pool.
1862 */
1863static int
1864do_import(nvlist_t *config, const char *newname, const char *mntopts,
572e2857 1865 nvlist_t *props, int flags)
34dc7c2f
BB
1866{
1867 zpool_handle_t *zhp;
1868 char *name;
1869 uint64_t state;
1870 uint64_t version;
34dc7c2f
BB
1871
1872 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1873 &name) == 0);
1874
1875 verify(nvlist_lookup_uint64(config,
1876 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1877 verify(nvlist_lookup_uint64(config,
1878 ZPOOL_CONFIG_VERSION, &version) == 0);
9ae529ec 1879 if (!SPA_VERSION_IS_SUPPORTED(version)) {
34dc7c2f 1880 (void) fprintf(stderr, gettext("cannot import '%s': pool "
9ae529ec 1881 "is formatted using an unsupported ZFS version\n"), name);
34dc7c2f 1882 return (1);
572e2857
BB
1883 } else if (state != POOL_STATE_EXPORTED &&
1884 !(flags & ZFS_IMPORT_ANY_HOST)) {
34dc7c2f
BB
1885 uint64_t hostid;
1886
1887 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1888 &hostid) == 0) {
492b8e9e
DH
1889 unsigned long system_hostid = gethostid() & 0xffffffff;
1890
1891 if ((unsigned long)hostid != system_hostid) {
34dc7c2f
BB
1892 char *hostname;
1893 uint64_t timestamp;
1894 time_t t;
1895
1896 verify(nvlist_lookup_string(config,
1897 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1898 verify(nvlist_lookup_uint64(config,
1899 ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1900 t = timestamp;
1901 (void) fprintf(stderr, gettext("cannot import "
1902 "'%s': pool may be in use from other "
1903 "system, it was last accessed by %s "
1904 "(hostid: 0x%lx) on %s"), name, hostname,
1905 (unsigned long)hostid,
1906 asctime(localtime(&t)));
1907 (void) fprintf(stderr, gettext("use '-f' to "
1908 "import anyway\n"));
1909 return (1);
1910 }
1911 } else {
1912 (void) fprintf(stderr, gettext("cannot import '%s': "
1913 "pool may be in use from other system\n"), name);
1914 (void) fprintf(stderr, gettext("use '-f' to import "
1915 "anyway\n"));
1916 return (1);
1917 }
1918 }
1919
572e2857 1920 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
34dc7c2f
BB
1921 return (1);
1922
1923 if (newname != NULL)
1924 name = (char *)newname;
1925
45d1cae3
BB
1926 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1927 return (1);
34dc7c2f 1928
d164b209 1929 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
572e2857 1930 !(flags & ZFS_IMPORT_ONLY) &&
d164b209 1931 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
34dc7c2f
BB
1932 zpool_close(zhp);
1933 return (1);
1934 }
1935
1936 zpool_close(zhp);
428870ff 1937 return (0);
34dc7c2f
BB
1938}
1939
1940/*
1941 * zpool import [-d dir] [-D]
1942 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1943 * [-d dir | -c cachefile] [-f] -a
1944 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
428870ff 1945 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
34dc7c2f
BB
1946 *
1947 * -c Read pool information from a cachefile instead of searching
1948 * devices.
1949 *
d603ed6c 1950 * -d Scan in a specific directory, other than /dev/. More than
34dc7c2f
BB
1951 * one directory can be specified using multiple '-d' options.
1952 *
1953 * -D Scan for previously destroyed pools or import all or only
1954 * specified destroyed pools.
1955 *
1956 * -R Temporarily import the pool, with all mountpoints relative to
1957 * the given root. The pool will remain exported when the machine
1958 * is rebooted.
1959 *
428870ff 1960 * -V Import even in the presence of faulted vdevs. This is an
b128c09f
BB
1961 * intentionally undocumented option for testing purposes, and
1962 * treats the pool configuration as complete, leaving any bad
45d1cae3
BB
1963 * vdevs in the FAULTED state. In other words, it does verbatim
1964 * import.
b128c09f 1965 *
428870ff
BB
1966 * -f Force import, even if it appears that the pool is active.
1967 *
1968 * -F Attempt rewind if necessary.
1969 *
1970 * -n See if rewind would work, but don't actually rewind.
1971 *
572e2857
BB
1972 * -N Import the pool but don't mount datasets.
1973 *
1974 * -T Specify a starting txg to use for import. This option is
1975 * intentionally undocumented option for testing purposes.
1976 *
34dc7c2f
BB
1977 * -a Import all pools found.
1978 *
1979 * -o Set property=value and/or temporary mount options (without '=').
1980 *
1981 * The import command scans for pools to import, and import pools based on pool
1982 * name and GUID. The pool can also be renamed as part of the import process.
1983 */
1984int
1985zpool_do_import(int argc, char **argv)
1986{
1987 char **searchdirs = NULL;
44867b6d 1988 char *env, *envdup = NULL;
34dc7c2f
BB
1989 int nsearch = 0;
1990 int c;
428870ff 1991 int err = 0;
34dc7c2f
BB
1992 nvlist_t *pools = NULL;
1993 boolean_t do_all = B_FALSE;
1994 boolean_t do_destroyed = B_FALSE;
1995 char *mntopts = NULL;
34dc7c2f
BB
1996 nvpair_t *elem;
1997 nvlist_t *config;
b128c09f
BB
1998 uint64_t searchguid = 0;
1999 char *searchname = NULL;
34dc7c2f
BB
2000 char *propval;
2001 nvlist_t *found_config;
428870ff 2002 nvlist_t *policy = NULL;
34dc7c2f
BB
2003 nvlist_t *props = NULL;
2004 boolean_t first;
572e2857 2005 int flags = ZFS_IMPORT_NORMAL;
428870ff
BB
2006 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2007 boolean_t dryrun = B_FALSE;
2008 boolean_t do_rewind = B_FALSE;
2009 boolean_t xtreme_rewind = B_FALSE;
572e2857 2010 uint64_t pool_state, txg = -1ULL;
34dc7c2f 2011 char *cachefile = NULL;
428870ff 2012 importargs_t idata = { 0 };
572e2857 2013 char *endptr;
34dc7c2f
BB
2014
2015 /* check options */
26b42f3f 2016 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
34dc7c2f
BB
2017 switch (c) {
2018 case 'a':
2019 do_all = B_TRUE;
2020 break;
2021 case 'c':
2022 cachefile = optarg;
2023 break;
2024 case 'd':
2025 if (searchdirs == NULL) {
2026 searchdirs = safe_malloc(sizeof (char *));
2027 } else {
2028 char **tmp = safe_malloc((nsearch + 1) *
2029 sizeof (char *));
2030 bcopy(searchdirs, tmp, nsearch *
2031 sizeof (char *));
2032 free(searchdirs);
2033 searchdirs = tmp;
2034 }
2035 searchdirs[nsearch++] = optarg;
2036 break;
2037 case 'D':
2038 do_destroyed = B_TRUE;
2039 break;
2040 case 'f':
572e2857 2041 flags |= ZFS_IMPORT_ANY_HOST;
34dc7c2f 2042 break;
b128c09f 2043 case 'F':
428870ff
BB
2044 do_rewind = B_TRUE;
2045 break;
572e2857
BB
2046 case 'm':
2047 flags |= ZFS_IMPORT_MISSING_LOG;
2048 break;
428870ff
BB
2049 case 'n':
2050 dryrun = B_TRUE;
b128c09f 2051 break;
572e2857
BB
2052 case 'N':
2053 flags |= ZFS_IMPORT_ONLY;
2054 break;
34dc7c2f
BB
2055 case 'o':
2056 if ((propval = strchr(optarg, '=')) != NULL) {
2057 *propval = '\0';
2058 propval++;
b128c09f
BB
2059 if (add_prop_list(optarg, propval,
2060 &props, B_TRUE))
34dc7c2f
BB
2061 goto error;
2062 } else {
2063 mntopts = optarg;
2064 }
2065 break;
2066 case 'R':
2067 if (add_prop_list(zpool_prop_to_name(
b128c09f 2068 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
34dc7c2f
BB
2069 goto error;
2070 if (nvlist_lookup_string(props,
2071 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2072 &propval) == 0)
2073 break;
2074 if (add_prop_list(zpool_prop_to_name(
b128c09f 2075 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
34dc7c2f
BB
2076 goto error;
2077 break;
26b42f3f
RY
2078 case 't':
2079 flags |= ZFS_IMPORT_TEMP_NAME;
2080 break;
2081
572e2857
BB
2082 case 'T':
2083 errno = 0;
2084 txg = strtoull(optarg, &endptr, 10);
2085 if (errno != 0 || *endptr != '\0') {
2086 (void) fprintf(stderr,
2087 gettext("invalid txg value\n"));
2088 usage(B_FALSE);
2089 }
2090 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2091 break;
428870ff 2092 case 'V':
572e2857 2093 flags |= ZFS_IMPORT_VERBATIM;
428870ff
BB
2094 break;
2095 case 'X':
2096 xtreme_rewind = B_TRUE;
2097 break;
34dc7c2f
BB
2098 case ':':
2099 (void) fprintf(stderr, gettext("missing argument for "
2100 "'%c' option\n"), optopt);
2101 usage(B_FALSE);
2102 break;
2103 case '?':
2104 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2105 optopt);
2106 usage(B_FALSE);
2107 }
2108 }
2109
2110 argc -= optind;
2111 argv += optind;
2112
2113 if (cachefile && nsearch != 0) {
2114 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2115 usage(B_FALSE);
2116 }
2117
428870ff
BB
2118 if ((dryrun || xtreme_rewind) && !do_rewind) {
2119 (void) fprintf(stderr,
2120 gettext("-n or -X only meaningful with -F\n"));
2121 usage(B_FALSE);
2122 }
2123 if (dryrun)
2124 rewind_policy = ZPOOL_TRY_REWIND;
2125 else if (do_rewind)
2126 rewind_policy = ZPOOL_DO_REWIND;
2127 if (xtreme_rewind)
2128 rewind_policy |= ZPOOL_EXTREME_REWIND;
2129
2130 /* In the future, we can capture further policy and include it here */
2131 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
572e2857 2132 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
428870ff
BB
2133 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2134 goto error;
2135
34dc7c2f
BB
2136 /* check argument count */
2137 if (do_all) {
2138 if (argc != 0) {
2139 (void) fprintf(stderr, gettext("too many arguments\n"));
2140 usage(B_FALSE);
2141 }
2142 } else {
2143 if (argc > 2) {
2144 (void) fprintf(stderr, gettext("too many arguments\n"));
2145 usage(B_FALSE);
2146 }
2147
2148 /*
2149 * Check for the SYS_CONFIG privilege. We do this explicitly
2150 * here because otherwise any attempt to discover pools will
2151 * silently fail.
2152 */
2153 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2154 (void) fprintf(stderr, gettext("cannot "
2155 "discover pools: permission denied\n"));
d603ed6c
BB
2156 if (searchdirs != NULL)
2157 free(searchdirs);
2158
428870ff 2159 nvlist_free(policy);
34dc7c2f
BB
2160 return (1);
2161 }
2162 }
2163
34dc7c2f 2164 /*
34dc7c2f
BB
2165 * Depending on the arguments given, we do one of the following:
2166 *
2167 * <none> Iterate through all pools and display information about
2168 * each one.
2169 *
2170 * -a Iterate through all pools and try to import each one.
2171 *
2172 * <id> Find the pool that corresponds to the given GUID/pool
2173 * name and import that one.
2174 *
2175 * -D Above options applies only to destroyed pools.
2176 */
2177 if (argc != 0) {
2178 char *endptr;
2179
2180 errno = 0;
2181 searchguid = strtoull(argv[0], &endptr, 10);
2182 if (errno != 0 || *endptr != '\0')
2183 searchname = argv[0];
34dc7c2f 2184 found_config = NULL;
34dc7c2f 2185
b128c09f 2186 /*
428870ff 2187 * User specified a name or guid. Ensure it's unique.
b128c09f 2188 */
428870ff 2189 idata.unique = B_TRUE;
b128c09f
BB
2190 }
2191
44867b6d
BB
2192 /*
2193 * Check the environment for the preferred search path.
2194 */
2195 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2196 char *dir;
2197
2198 envdup = strdup(env);
2199
2200 dir = strtok(envdup, ":");
2201 while (dir != NULL) {
2202 if (searchdirs == NULL) {
2203 searchdirs = safe_malloc(sizeof (char *));
2204 } else {
2205 char **tmp = safe_malloc((nsearch + 1) *
2206 sizeof (char *));
2207 bcopy(searchdirs, tmp, nsearch *
2208 sizeof (char *));
2209 free(searchdirs);
2210 searchdirs = tmp;
2211 }
2212 searchdirs[nsearch++] = dir;
2213 dir = strtok(NULL, ":");
2214 }
2215 }
428870ff
BB
2216
2217 idata.path = searchdirs;
2218 idata.paths = nsearch;
2219 idata.poolname = searchname;
2220 idata.guid = searchguid;
2221 idata.cachefile = cachefile;
2222
2223 pools = zpool_search_import(g_zfs, &idata);
2224
2225 if (pools != NULL && idata.exists &&
2226 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2227 (void) fprintf(stderr, gettext("cannot import '%s': "
2228 "a pool with that name already exists\n"),
2229 argv[0]);
2230 (void) fprintf(stderr, gettext("use the form '%s "
2231 "<pool | id> <newpool>' to give it a new name\n"),
2232 "zpool import");
2233 err = 1;
2234 } else if (pools == NULL && idata.exists) {
2235 (void) fprintf(stderr, gettext("cannot import '%s': "
2236 "a pool with that name is already created/imported,\n"),
2237 argv[0]);
2238 (void) fprintf(stderr, gettext("and no additional pools "
2239 "with that name were found\n"));
2240 err = 1;
2241 } else if (pools == NULL) {
b128c09f
BB
2242 if (argc != 0) {
2243 (void) fprintf(stderr, gettext("cannot import '%s': "
2244 "no such pool available\n"), argv[0]);
2245 }
428870ff
BB
2246 err = 1;
2247 }
2248
2249 if (err == 1) {
d603ed6c
BB
2250 if (searchdirs != NULL)
2251 free(searchdirs);
44867b6d
BB
2252 if (envdup != NULL)
2253 free(envdup);
428870ff 2254 nvlist_free(policy);
b128c09f
BB
2255 return (1);
2256 }
2257
2258 /*
2259 * At this point we have a list of import candidate configs. Even if
2260 * we were searching by pool name or guid, we still need to
2261 * post-process the list to deal with pool state and possible
2262 * duplicate names.
2263 */
34dc7c2f
BB
2264 err = 0;
2265 elem = NULL;
2266 first = B_TRUE;
2267 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2268
2269 verify(nvpair_value_nvlist(elem, &config) == 0);
2270
2271 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2272 &pool_state) == 0);
2273 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2274 continue;
2275 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2276 continue;
2277
428870ff
BB
2278 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2279 policy) == 0);
2280
34dc7c2f
BB
2281 if (argc == 0) {
2282 if (first)
2283 first = B_FALSE;
2284 else if (!do_all)
2285 (void) printf("\n");
2286
428870ff 2287 if (do_all) {
34dc7c2f 2288 err |= do_import(config, NULL, mntopts,
572e2857 2289 props, flags);
428870ff 2290 } else {
34dc7c2f 2291 show_import(config);
428870ff 2292 }
34dc7c2f
BB
2293 } else if (searchname != NULL) {
2294 char *name;
2295
2296 /*
2297 * We are searching for a pool based on name.
2298 */
2299 verify(nvlist_lookup_string(config,
2300 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2301
2302 if (strcmp(name, searchname) == 0) {
2303 if (found_config != NULL) {
2304 (void) fprintf(stderr, gettext(
2305 "cannot import '%s': more than "
2306 "one matching pool\n"), searchname);
2307 (void) fprintf(stderr, gettext(
2308 "import by numeric ID instead\n"));
2309 err = B_TRUE;
2310 }
2311 found_config = config;
2312 }
2313 } else {
2314 uint64_t guid;
2315
2316 /*
2317 * Search for a pool by guid.
2318 */
2319 verify(nvlist_lookup_uint64(config,
2320 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2321
2322 if (guid == searchguid)
2323 found_config = config;
2324 }
2325 }
2326
2327 /*
2328 * If we were searching for a specific pool, verify that we found a
2329 * pool, and then do the import.
2330 */
2331 if (argc != 0 && err == 0) {
2332 if (found_config == NULL) {
2333 (void) fprintf(stderr, gettext("cannot import '%s': "
2334 "no such pool available\n"), argv[0]);
2335 err = B_TRUE;
2336 } else {
2337 err |= do_import(found_config, argc == 1 ? NULL :
572e2857 2338 argv[1], mntopts, props, flags);
34dc7c2f
BB
2339 }
2340 }
2341
2342 /*
2343 * If we were just looking for pools, report an error if none were
2344 * found.
2345 */
2346 if (argc == 0 && first)
2347 (void) fprintf(stderr,
2348 gettext("no pools available to import\n"));
2349
2350error:
2351 nvlist_free(props);
2352 nvlist_free(pools);
428870ff 2353 nvlist_free(policy);
d603ed6c
BB
2354 if (searchdirs != NULL)
2355 free(searchdirs);
44867b6d
BB
2356 if (envdup != NULL)
2357 free(envdup);
34dc7c2f
BB
2358
2359 return (err ? 1 : 0);
2360}
2361
2362typedef struct iostat_cbdata {
1bd201e7 2363 boolean_t cb_verbose;
34dc7c2f 2364 int cb_namewidth;
1bd201e7
CS
2365 int cb_iteration;
2366 zpool_list_t *cb_list;
34dc7c2f
BB
2367} iostat_cbdata_t;
2368
2369static void
2370print_iostat_separator(iostat_cbdata_t *cb)
2371{
2372 int i = 0;
2373
2374 for (i = 0; i < cb->cb_namewidth; i++)
2375 (void) printf("-");
2376 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2377}
2378
2379static void
2380print_iostat_header(iostat_cbdata_t *cb)
2381{
2382 (void) printf("%*s capacity operations bandwidth\n",
2383 cb->cb_namewidth, "");
428870ff 2384 (void) printf("%-*s alloc free read write read write\n",
34dc7c2f
BB
2385 cb->cb_namewidth, "pool");
2386 print_iostat_separator(cb);
2387}
2388
2389/*
2390 * Display a single statistic.
2391 */
2392static void
2393print_one_stat(uint64_t value)
2394{
2395 char buf[64];
2396
2397 zfs_nicenum(value, buf, sizeof (buf));
2398 (void) printf(" %5s", buf);
2399}
2400
2401/*
2402 * Print out all the statistics for the given vdev. This can either be the
2403 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2404 * is a verbose output, and we don't want to display the toplevel pool stats.
2405 */
2406void
2407print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2408 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2409{
2410 nvlist_t **oldchild, **newchild;
2411 uint_t c, children;
2412 vdev_stat_t *oldvs, *newvs;
2413 vdev_stat_t zerovs = { 0 };
2414 uint64_t tdelta;
2415 double scale;
2416 char *vname;
2417
2418 if (oldnv != NULL) {
428870ff
BB
2419 verify(nvlist_lookup_uint64_array(oldnv,
2420 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
34dc7c2f
BB
2421 } else {
2422 oldvs = &zerovs;
2423 }
2424
428870ff 2425 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
2426 (uint64_t **)&newvs, &c) == 0);
2427
2428 if (strlen(name) + depth > cb->cb_namewidth)
2429 (void) printf("%*s%s", depth, "", name);
2430 else
2431 (void) printf("%*s%s%*s", depth, "", name,
2432 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2433
2434 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2435
2436 if (tdelta == 0)
2437 scale = 1.0;
2438 else
2439 scale = (double)NANOSEC / tdelta;
2440
2441 /* only toplevel vdevs have capacity stats */
2442 if (newvs->vs_space == 0) {
2443 (void) printf(" - -");
2444 } else {
2445 print_one_stat(newvs->vs_alloc);
2446 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2447 }
2448
2449 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2450 oldvs->vs_ops[ZIO_TYPE_READ])));
2451
2452 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2453 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2454
2455 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2456 oldvs->vs_bytes[ZIO_TYPE_READ])));
2457
2458 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2459 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2460
2461 (void) printf("\n");
2462
2463 if (!cb->cb_verbose)
2464 return;
2465
2466 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2467 &newchild, &children) != 0)
2468 return;
2469
2470 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2471 &oldchild, &c) != 0)
2472 return;
2473
2474 for (c = 0; c < children; c++) {
187632dc 2475 uint64_t ishole = B_FALSE, islog = B_FALSE;
428870ff 2476
187632dc
MH
2477 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2478 &ishole);
2479
2480 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2481 &islog);
2482
2483 if (ishole || islog)
428870ff
BB
2484 continue;
2485
2486 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
34dc7c2f
BB
2487 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2488 newchild[c], cb, depth + 2);
2489 free(vname);
2490 }
2491
187632dc
MH
2492 /*
2493 * Log device section
2494 */
2495
2496 if (num_logs(newnv) > 0) {
2497 (void) printf("%-*s - - - - - "
2498 "-\n", cb->cb_namewidth, "logs");
2499
2500 for (c = 0; c < children; c++) {
2501 uint64_t islog = B_FALSE;
2502 (void) nvlist_lookup_uint64(newchild[c],
2503 ZPOOL_CONFIG_IS_LOG, &islog);
2504
2505 if (islog) {
2506 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2507 B_FALSE);
2508 print_vdev_stats(zhp, vname, oldnv ?
2509 oldchild[c] : NULL, newchild[c],
2510 cb, depth + 2);
2511 free(vname);
2512 }
2513 }
2514
2515 }
2516
34dc7c2f
BB
2517 /*
2518 * Include level 2 ARC devices in iostat output
2519 */
2520 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2521 &newchild, &children) != 0)
2522 return;
2523
2524 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2525 &oldchild, &c) != 0)
2526 return;
2527
2528 if (children > 0) {
2529 (void) printf("%-*s - - - - - "
2530 "-\n", cb->cb_namewidth, "cache");
2531 for (c = 0; c < children; c++) {
428870ff
BB
2532 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2533 B_FALSE);
34dc7c2f
BB
2534 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2535 newchild[c], cb, depth + 2);
2536 free(vname);
2537 }
2538 }
2539}
2540
2541static int
2542refresh_iostat(zpool_handle_t *zhp, void *data)
2543{
2544 iostat_cbdata_t *cb = data;
2545 boolean_t missing;
2546
2547 /*
2548 * If the pool has disappeared, remove it from the list and continue.
2549 */
2550 if (zpool_refresh_stats(zhp, &missing) != 0)
2551 return (-1);
2552
2553 if (missing)
2554 pool_list_remove(cb->cb_list, zhp);
2555
2556 return (0);
2557}
2558
2559/*
2560 * Callback to print out the iostats for the given pool.
2561 */
2562int
2563print_iostat(zpool_handle_t *zhp, void *data)
2564{
2565 iostat_cbdata_t *cb = data;
2566 nvlist_t *oldconfig, *newconfig;
2567 nvlist_t *oldnvroot, *newnvroot;
2568
2569 newconfig = zpool_get_config(zhp, &oldconfig);
2570
2571 if (cb->cb_iteration == 1)
2572 oldconfig = NULL;
2573
2574 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2575 &newnvroot) == 0);
2576
2577 if (oldconfig == NULL)
2578 oldnvroot = NULL;
2579 else
2580 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2581 &oldnvroot) == 0);
2582
2583 /*
2584 * Print out the statistics for the pool.
2585 */
2586 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2587
2588 if (cb->cb_verbose)
2589 print_iostat_separator(cb);
2590
2591 return (0);
2592}
2593
9fc60702
CS
2594static int
2595get_columns(void)
2596{
2597 struct winsize ws;
2598 int columns = 80;
2599 int error;
2600
2601 if (isatty(STDOUT_FILENO)) {
2602 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2603 if (error == 0)
2604 columns = ws.ws_col;
2605 } else {
2606 columns = 999;
2607 }
2608
d1d7e268 2609 return (columns);
9fc60702
CS
2610}
2611
34dc7c2f
BB
2612int
2613get_namewidth(zpool_handle_t *zhp, void *data)
2614{
2615 iostat_cbdata_t *cb = data;
2616 nvlist_t *config, *nvroot;
9fc60702 2617 int columns;
34dc7c2f
BB
2618
2619 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2620 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2621 &nvroot) == 0);
2622 if (!cb->cb_verbose)
2623 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2624 else
95bcd51e
FW
2625 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2626 cb->cb_namewidth);
34dc7c2f
BB
2627 }
2628
2629 /*
9fc60702
CS
2630 * The width must be at least 10, but may be as large as the
2631 * column width - 42 so that we can still fit in one line.
34dc7c2f 2632 */
9fc60702
CS
2633 columns = get_columns();
2634
34dc7c2f
BB
2635 if (cb->cb_namewidth < 10)
2636 cb->cb_namewidth = 10;
9fc60702
CS
2637 if (cb->cb_namewidth > columns - 42)
2638 cb->cb_namewidth = columns - 42;
34dc7c2f
BB
2639
2640 return (0);
2641}
2642
2643/*
428870ff 2644 * Parse the input string, get the 'interval' and 'count' value if there is one.
34dc7c2f 2645 */
428870ff
BB
2646static void
2647get_interval_count(int *argcp, char **argv, unsigned long *iv,
2648 unsigned long *cnt)
34dc7c2f 2649{
34dc7c2f 2650 unsigned long interval = 0, count = 0;
1fde1e37 2651 int argc = *argcp;
34dc7c2f
BB
2652
2653 /*
2654 * Determine if the last argument is an integer or a pool name
2655 */
2656 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2657 char *end;
2658
2659 errno = 0;
2660 interval = strtoul(argv[argc - 1], &end, 10);
2661
2662 if (*end == '\0' && errno == 0) {
2663 if (interval == 0) {
2664 (void) fprintf(stderr, gettext("interval "
2665 "cannot be zero\n"));
2666 usage(B_FALSE);
2667 }
34dc7c2f
BB
2668 /*
2669 * Ignore the last parameter
2670 */
2671 argc--;
2672 } else {
2673 /*
2674 * If this is not a valid number, just plow on. The
2675 * user will get a more informative error message later
2676 * on.
2677 */
2678 interval = 0;
2679 }
2680 }
2681
2682 /*
2683 * If the last argument is also an integer, then we have both a count
428870ff 2684 * and an interval.
34dc7c2f
BB
2685 */
2686 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2687 char *end;
2688
2689 errno = 0;
2690 count = interval;
2691 interval = strtoul(argv[argc - 1], &end, 10);
2692
2693 if (*end == '\0' && errno == 0) {
2694 if (interval == 0) {
2695 (void) fprintf(stderr, gettext("interval "
2696 "cannot be zero\n"));
2697 usage(B_FALSE);
2698 }
2699
2700 /*
2701 * Ignore the last parameter
2702 */
2703 argc--;
2704 } else {
2705 interval = 0;
2706 }
2707 }
2708
428870ff
BB
2709 *iv = interval;
2710 *cnt = count;
2711 *argcp = argc;
2712}
2713
2714static void
2715get_timestamp_arg(char c)
2716{
2717 if (c == 'u')
2718 timestamp_fmt = UDATE;
2719 else if (c == 'd')
2720 timestamp_fmt = DDATE;
2721 else
2722 usage(B_FALSE);
2723}
2724
2725/*
2726 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2727 *
2728 * -v Display statistics for individual vdevs
2729 * -T Display a timestamp in date(1) or Unix format
2730 *
2731 * This command can be tricky because we want to be able to deal with pool
2732 * creation/destruction as well as vdev configuration changes. The bulk of this
2733 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2734 * on pool_list_update() to detect the addition of new pools. Configuration
2735 * changes are all handled within libzfs.
2736 */
2737int
2738zpool_do_iostat(int argc, char **argv)
2739{
2740 int c;
2741 int ret;
2742 int npools;
2743 unsigned long interval = 0, count = 0;
2744 zpool_list_t *list;
2745 boolean_t verbose = B_FALSE;
2746 iostat_cbdata_t cb;
2747
2748 /* check options */
2749 while ((c = getopt(argc, argv, "T:v")) != -1) {
2750 switch (c) {
2751 case 'T':
2752 get_timestamp_arg(*optarg);
2753 break;
2754 case 'v':
2755 verbose = B_TRUE;
2756 break;
2757 case '?':
2758 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2759 optopt);
2760 usage(B_FALSE);
2761 }
2762 }
2763
2764 argc -= optind;
2765 argv += optind;
2766
2767 get_interval_count(&argc, argv, &interval, &count);
2768
34dc7c2f
BB
2769 /*
2770 * Construct the list of all interesting pools.
2771 */
2772 ret = 0;
2773 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2774 return (1);
2775
2776 if (pool_list_count(list) == 0 && argc != 0) {
2777 pool_list_free(list);
2778 return (1);
2779 }
2780
2781 if (pool_list_count(list) == 0 && interval == 0) {
2782 pool_list_free(list);
2783 (void) fprintf(stderr, gettext("no pools available\n"));
2784 return (1);
2785 }
2786
2787 /*
2788 * Enter the main iostat loop.
2789 */
2790 cb.cb_list = list;
2791 cb.cb_verbose = verbose;
2792 cb.cb_iteration = 0;
2793 cb.cb_namewidth = 0;
2794
2795 for (;;) {
2796 pool_list_update(list);
2797
2798 if ((npools = pool_list_count(list)) == 0)
42cb3819 2799 (void) fprintf(stderr, gettext("no pools available\n"));
5a521059
PJ
2800 else {
2801 /*
2802 * Refresh all statistics. This is done as an
2803 * explicit step before calculating the maximum name
2804 * width, so that any * configuration changes are
2805 * properly accounted for.
2806 */
2807 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2808 &cb);
34dc7c2f 2809
5a521059
PJ
2810 /*
2811 * Iterate over all pools to determine the maximum width
2812 * for the pool / device name column across all pools.
2813 */
2814 cb.cb_namewidth = 0;
2815 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2816 &cb);
34dc7c2f 2817
5a521059
PJ
2818 if (timestamp_fmt != NODATE)
2819 print_timestamp(timestamp_fmt);
428870ff 2820
5a521059
PJ
2821 /*
2822 * If it's the first time, or verbose mode, print the
2823 * header.
2824 */
2825 if (++cb.cb_iteration == 1 || verbose)
2826 print_iostat_header(&cb);
34dc7c2f 2827
5a521059 2828 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
34dc7c2f 2829
5a521059
PJ
2830 /*
2831 * If there's more than one pool, and we're not in
2832 * verbose mode (which prints a separator for us),
2833 * then print a separator.
2834 */
2835 if (npools > 1 && !verbose)
2836 print_iostat_separator(&cb);
34dc7c2f 2837
5a521059
PJ
2838 if (verbose)
2839 (void) printf("\n");
2840 }
34dc7c2f
BB
2841
2842 /*
2843 * Flush the output so that redirection to a file isn't buffered
2844 * indefinitely.
2845 */
2846 (void) fflush(stdout);
2847
2848 if (interval == 0)
2849 break;
2850
2851 if (count != 0 && --count == 0)
2852 break;
2853
2854 (void) sleep(interval);
2855 }
2856
2857 pool_list_free(list);
2858
2859 return (ret);
2860}
2861
2862typedef struct list_cbdata {
1bd201e7
CS
2863 boolean_t cb_verbose;
2864 int cb_namewidth;
34dc7c2f 2865 boolean_t cb_scripted;
34dc7c2f
BB
2866 zprop_list_t *cb_proplist;
2867} list_cbdata_t;
2868
2869/*
2870 * Given a list of columns to display, output appropriate headers for each one.
2871 */
2872static void
1bd201e7 2873print_header(list_cbdata_t *cb)
34dc7c2f 2874{
1bd201e7 2875 zprop_list_t *pl = cb->cb_proplist;
9ae529ec 2876 char headerbuf[ZPOOL_MAXPROPLEN];
34dc7c2f
BB
2877 const char *header;
2878 boolean_t first = B_TRUE;
2879 boolean_t right_justify;
1bd201e7 2880 size_t width = 0;
34dc7c2f
BB
2881
2882 for (; pl != NULL; pl = pl->pl_next) {
1bd201e7
CS
2883 width = pl->pl_width;
2884 if (first && cb->cb_verbose) {
2885 /*
2886 * Reset the width to accommodate the verbose listing
2887 * of devices.
2888 */
2889 width = cb->cb_namewidth;
2890 }
2891
34dc7c2f
BB
2892 if (!first)
2893 (void) printf(" ");
2894 else
2895 first = B_FALSE;
2896
9ae529ec
CS
2897 right_justify = B_FALSE;
2898 if (pl->pl_prop != ZPROP_INVAL) {
2899 header = zpool_prop_column_name(pl->pl_prop);
2900 right_justify = zpool_prop_align_right(pl->pl_prop);
2901 } else {
2902 int i;
2903
2904 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2905 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2906 headerbuf[i] = '\0';
2907 header = headerbuf;
2908 }
34dc7c2f
BB
2909
2910 if (pl->pl_next == NULL && !right_justify)
2911 (void) printf("%s", header);
2912 else if (right_justify)
1bd201e7 2913 (void) printf("%*s", (int)width, header);
34dc7c2f 2914 else
1bd201e7 2915 (void) printf("%-*s", (int)width, header);
34dc7c2f
BB
2916 }
2917
2918 (void) printf("\n");
2919}
2920
2921/*
2922 * Given a pool and a list of properties, print out all the properties according
2923 * to the described layout.
2924 */
2925static void
1bd201e7 2926print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
34dc7c2f 2927{
1bd201e7 2928 zprop_list_t *pl = cb->cb_proplist;
34dc7c2f
BB
2929 boolean_t first = B_TRUE;
2930 char property[ZPOOL_MAXPROPLEN];
2931 char *propstr;
2932 boolean_t right_justify;
1bd201e7 2933 size_t width;
34dc7c2f
BB
2934
2935 for (; pl != NULL; pl = pl->pl_next) {
1bd201e7
CS
2936
2937 width = pl->pl_width;
2938 if (first && cb->cb_verbose) {
2939 /*
2940 * Reset the width to accommodate the verbose listing
2941 * of devices.
2942 */
2943 width = cb->cb_namewidth;
2944 }
2945
34dc7c2f 2946 if (!first) {
1bd201e7 2947 if (cb->cb_scripted)
34dc7c2f
BB
2948 (void) printf("\t");
2949 else
2950 (void) printf(" ");
2951 } else {
2952 first = B_FALSE;
2953 }
2954
2955 right_justify = B_FALSE;
2956 if (pl->pl_prop != ZPROP_INVAL) {
1bd201e7
CS
2957 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2958 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2959 propstr = "-";
2960 else if (zpool_get_prop(zhp, pl->pl_prop, property,
34dc7c2f
BB
2961 sizeof (property), NULL) != 0)
2962 propstr = "-";
2963 else
2964 propstr = property;
2965
2966 right_justify = zpool_prop_align_right(pl->pl_prop);
9ae529ec
CS
2967 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2968 zpool_prop_unsupported(pl->pl_user_prop)) &&
2969 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2970 sizeof (property)) == 0) {
2971 propstr = property;
34dc7c2f
BB
2972 } else {
2973 propstr = "-";
2974 }
2975
34dc7c2f
BB
2976
2977 /*
2978 * If this is being called in scripted mode, or if this is the
2979 * last column and it is left-justified, don't include a width
2980 * format specifier.
2981 */
1bd201e7 2982 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
34dc7c2f
BB
2983 (void) printf("%s", propstr);
2984 else if (right_justify)
1bd201e7 2985 (void) printf("%*s", (int)width, propstr);
34dc7c2f 2986 else
1bd201e7 2987 (void) printf("%-*s", (int)width, propstr);
34dc7c2f
BB
2988 }
2989
2990 (void) printf("\n");
2991}
2992
1bd201e7
CS
2993static void
2994print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2995{
2996 char propval[64];
2997 boolean_t fixed;
2998 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2999
3000 zfs_nicenum(value, propval, sizeof (propval));
3001
3002 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
3003 (void) strlcpy(propval, "-", sizeof (propval));
3004
3005 if (scripted)
3006 (void) printf("\t%s", propval);
3007 else
3008 (void) printf(" %*s", (int)width, propval);
3009}
3010
3011void
3012print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3013 list_cbdata_t *cb, int depth)
3014{
3015 nvlist_t **child;
3016 vdev_stat_t *vs;
3017 uint_t c, children;
3018 char *vname;
3019 boolean_t scripted = cb->cb_scripted;
3020
3021 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3022 (uint64_t **)&vs, &c) == 0);
3023
3024 if (name != NULL) {
3025 if (scripted)
3026 (void) printf("\t%s", name);
3027 else if (strlen(name) + depth > cb->cb_namewidth)
3028 (void) printf("%*s%s", depth, "", name);
3029 else
3030 (void) printf("%*s%s%*s", depth, "", name,
3031 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3032
3033 /* only toplevel vdevs have capacity stats */
3034 if (vs->vs_space == 0) {
3035 if (scripted)
3036 (void) printf("\t-\t-\t-");
3037 else
3038 (void) printf(" - - -");
3039 } else {
3040 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
3041 scripted);
3042 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
3043 scripted);
3044 print_one_column(ZPOOL_PROP_FREE,
3045 vs->vs_space - vs->vs_alloc, scripted);
3046 }
3047 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
3048 scripted);
3049 (void) printf("\n");
3050 }
3051
3052 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3053 &child, &children) != 0)
3054 return;
3055
3056 for (c = 0; c < children; c++) {
3057 uint64_t ishole = B_FALSE;
3058
3059 if (nvlist_lookup_uint64(child[c],
3060 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3061 continue;
3062
3063 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3064 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3065 free(vname);
3066 }
3067
3068 /*
3069 * Include level 2 ARC devices in iostat output
3070 */
3071 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3072 &child, &children) != 0)
3073 return;
3074
3075 if (children > 0) {
3076 (void) printf("%-*s - - - - - "
3077 "-\n", cb->cb_namewidth, "cache");
3078 for (c = 0; c < children; c++) {
3079 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3080 B_FALSE);
3081 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3082 free(vname);
3083 }
3084 }
3085}
3086
3087
34dc7c2f
BB
3088/*
3089 * Generic callback function to list a pool.
3090 */
3091int
3092list_callback(zpool_handle_t *zhp, void *data)
3093{
3094 list_cbdata_t *cbp = data;
1bd201e7
CS
3095 nvlist_t *config;
3096 nvlist_t *nvroot;
34dc7c2f 3097
1bd201e7 3098 config = zpool_get_config(zhp, NULL);
34dc7c2f 3099
1bd201e7
CS
3100 print_pool(zhp, cbp);
3101 if (!cbp->cb_verbose)
3102 return (0);
3103
3104 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3105 &nvroot) == 0);
3106 print_list_stats(zhp, NULL, nvroot, cbp, 0);
34dc7c2f
BB
3107
3108 return (0);
3109}
3110
3111/*
428870ff 3112 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
34dc7c2f
BB
3113 *
3114 * -H Scripted mode. Don't display headers, and separate properties
3115 * by a single tab.
3116 * -o List of properties to display. Defaults to
428870ff
BB
3117 * "name,size,allocated,free,capacity,health,altroot"
3118 * -T Display a timestamp in date(1) or Unix format
34dc7c2f
BB
3119 *
3120 * List all pools in the system, whether or not they're healthy. Output space
3121 * statistics for each one, as well as health status summary.
3122 */
3123int
3124zpool_do_list(int argc, char **argv)
3125{
3126 int c;
cd72af9c 3127 int ret = 0;
34dc7c2f
BB
3128 list_cbdata_t cb = { 0 };
3129 static char default_props[] =
1bd201e7
CS
3130 "name,size,allocated,free,capacity,dedupratio,"
3131 "health,altroot";
34dc7c2f 3132 char *props = default_props;
428870ff 3133 unsigned long interval = 0, count = 0;
1bd201e7
CS
3134 zpool_list_t *list;
3135 boolean_t first = B_TRUE;
34dc7c2f
BB
3136
3137 /* check options */
1bd201e7 3138 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
34dc7c2f
BB
3139 switch (c) {
3140 case 'H':
3141 cb.cb_scripted = B_TRUE;
3142 break;
3143 case 'o':
3144 props = optarg;
3145 break;
428870ff
BB
3146 case 'T':
3147 get_timestamp_arg(*optarg);
3148 break;
1bd201e7
CS
3149 case 'v':
3150 cb.cb_verbose = B_TRUE;
3151 break;
34dc7c2f
BB
3152 case ':':
3153 (void) fprintf(stderr, gettext("missing argument for "
3154 "'%c' option\n"), optopt);
3155 usage(B_FALSE);
3156 break;
3157 case '?':
3158 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3159 optopt);
3160 usage(B_FALSE);
3161 }
3162 }
3163
3164 argc -= optind;
3165 argv += optind;
3166
428870ff
BB
3167 get_interval_count(&argc, argv, &interval, &count);
3168
34dc7c2f
BB
3169 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3170 usage(B_FALSE);
3171
1bd201e7
CS
3172 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3173 return (1);
3174
3175 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3176 (void) printf(gettext("no pools available\n"));
3177 zprop_free_list(cb.cb_proplist);
3178 return (0);
3179 }
34dc7c2f 3180
428870ff 3181 for (;;) {
1bd201e7
CS
3182 pool_list_update(list);
3183
3184 if (pool_list_count(list) == 0)
3185 break;
34dc7c2f 3186
428870ff
BB
3187 if (timestamp_fmt != NODATE)
3188 print_timestamp(timestamp_fmt);
34dc7c2f 3189
1bd201e7
CS
3190 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3191 print_header(&cb);
3192 first = B_FALSE;
428870ff 3193 }
1bd201e7 3194 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
428870ff
BB
3195
3196 if (interval == 0)
3197 break;
3198
3199 if (count != 0 && --count == 0)
3200 break;
3201
3202 (void) sleep(interval);
34dc7c2f
BB
3203 }
3204
428870ff 3205 zprop_free_list(cb.cb_proplist);
34dc7c2f
BB
3206 return (ret);
3207}
3208
34dc7c2f
BB
3209static int
3210zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3211{
3212 boolean_t force = B_FALSE;
3213 int c;
3214 nvlist_t *nvroot;
3215 char *poolname, *old_disk, *new_disk;
3216 zpool_handle_t *zhp;
df831108
CP
3217 nvlist_t *props = NULL;
3218 char *propval;
34dc7c2f
BB
3219 int ret;
3220
3221 /* check options */
4588bf57 3222 while ((c = getopt(argc, argv, "fo:")) != -1) {
34dc7c2f
BB
3223 switch (c) {
3224 case 'f':
3225 force = B_TRUE;
3226 break;
df831108
CP
3227 case 'o':
3228 if ((propval = strchr(optarg, '=')) == NULL) {
3229 (void) fprintf(stderr, gettext("missing "
3230 "'=' for -o option\n"));
3231 usage(B_FALSE);
3232 }
3233 *propval = '\0';
3234 propval++;
3235
3236 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3237 (add_prop_list(optarg, propval, &props, B_TRUE)))
3238 usage(B_FALSE);
3239 break;
34dc7c2f
BB
3240 case '?':
3241 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3242 optopt);
3243 usage(B_FALSE);
3244 }
3245 }
3246
3247 argc -= optind;
3248 argv += optind;
3249
3250 /* get pool name and check number of arguments */
3251 if (argc < 1) {
3252 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3253 usage(B_FALSE);
3254 }
3255
3256 poolname = argv[0];
3257
3258 if (argc < 2) {
3259 (void) fprintf(stderr,
3260 gettext("missing <device> specification\n"));
3261 usage(B_FALSE);
3262 }
3263
3264 old_disk = argv[1];
3265
3266 if (argc < 3) {
3267 if (!replacing) {
3268 (void) fprintf(stderr,
3269 gettext("missing <new_device> specification\n"));
3270 usage(B_FALSE);
3271 }
3272 new_disk = old_disk;
3273 argc -= 1;
3274 argv += 1;
3275 } else {
3276 new_disk = argv[2];
3277 argc -= 2;
3278 argv += 2;
3279 }
3280
3281 if (argc > 1) {
3282 (void) fprintf(stderr, gettext("too many arguments\n"));
3283 usage(B_FALSE);
3284 }
3285
3286 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3287 return (1);
3288
3289 if (zpool_get_config(zhp, NULL) == NULL) {
3290 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3291 poolname);
3292 zpool_close(zhp);
3293 return (1);
3294 }
3295
df831108 3296 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
b128c09f 3297 argc, argv);
34dc7c2f
BB
3298 if (nvroot == NULL) {
3299 zpool_close(zhp);
3300 return (1);
3301 }
3302
3303 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3304
3305 nvlist_free(nvroot);
3306 zpool_close(zhp);
3307
3308 return (ret);
3309}
3310
3311/*
3312 * zpool replace [-f] <pool> <device> <new_device>
3313 *
3314 * -f Force attach, even if <new_device> appears to be in use.
3315 *
3316 * Replace <device> with <new_device>.
3317 */
3318/* ARGSUSED */
3319int
3320zpool_do_replace(int argc, char **argv)
3321{
3322 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3323}
3324
3325/*
df831108 3326 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
34dc7c2f
BB
3327 *
3328 * -f Force attach, even if <new_device> appears to be in use.
df831108 3329 * -o Set property=value.
34dc7c2f
BB
3330 *
3331 * Attach <new_device> to the mirror containing <device>. If <device> is not
3332 * part of a mirror, then <device> will be transformed into a mirror of
3333 * <device> and <new_device>. In either case, <new_device> will begin life
3334 * with a DTL of [0, now], and will immediately begin to resilver itself.
3335 */
3336int
3337zpool_do_attach(int argc, char **argv)
3338{
3339 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3340}
3341
3342/*
3343 * zpool detach [-f] <pool> <device>
3344 *
3345 * -f Force detach of <device>, even if DTLs argue against it
3346 * (not supported yet)
3347 *
3348 * Detach a device from a mirror. The operation will be refused if <device>
3349 * is the last device in the mirror, or if the DTLs indicate that this device
3350 * has the only valid copy of some data.
3351 */
3352/* ARGSUSED */
3353int
3354zpool_do_detach(int argc, char **argv)
3355{
3356 int c;
3357 char *poolname, *path;
3358 zpool_handle_t *zhp;
3359 int ret;
3360
3361 /* check options */
3362 while ((c = getopt(argc, argv, "f")) != -1) {
3363 switch (c) {
3364 case 'f':
3365 case '?':
3366 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3367 optopt);
3368 usage(B_FALSE);
3369 }
3370 }
3371
3372 argc -= optind;
3373 argv += optind;
3374
3375 /* get pool name and check number of arguments */
3376 if (argc < 1) {
3377 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3378 usage(B_FALSE);
3379 }
3380
3381 if (argc < 2) {
3382 (void) fprintf(stderr,
3383 gettext("missing <device> specification\n"));
3384 usage(B_FALSE);
3385 }
3386
3387 poolname = argv[0];
3388 path = argv[1];
3389
3390 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3391 return (1);
3392
3393 ret = zpool_vdev_detach(zhp, path);
3394
3395 zpool_close(zhp);
3396
3397 return (ret);
3398}
3399
428870ff
BB
3400/*
3401 * zpool split [-n] [-o prop=val] ...
3402 * [-o mntopt] ...
3403 * [-R altroot] <pool> <newpool> [<device> ...]
3404 *
3405 * -n Do not split the pool, but display the resulting layout if
3406 * it were to be split.
3407 * -o Set property=value, or set mount options.
3408 * -R Mount the split-off pool under an alternate root.
3409 *
3410 * Splits the named pool and gives it the new pool name. Devices to be split
3411 * off may be listed, provided that no more than one device is specified
3412 * per top-level vdev mirror. The newly split pool is left in an exported
3413 * state unless -R is specified.
3414 *
3415 * Restrictions: the top-level of the pool pool must only be made up of
3416 * mirrors; all devices in the pool must be healthy; no device may be
3417 * undergoing a resilvering operation.
3418 */
3419int
3420zpool_do_split(int argc, char **argv)
3421{
3422 char *srcpool, *newpool, *propval;
3423 char *mntopts = NULL;
3424 splitflags_t flags;
3425 int c, ret = 0;
3426 zpool_handle_t *zhp;
3427 nvlist_t *config, *props = NULL;
3428
3429 flags.dryrun = B_FALSE;
3430 flags.import = B_FALSE;
3431
3432 /* check options */
3433 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3434 switch (c) {
3435 case 'R':
3436 flags.import = B_TRUE;
3437 if (add_prop_list(
3438 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3439 &props, B_TRUE) != 0) {
3440 if (props)
3441 nvlist_free(props);
3442 usage(B_FALSE);
3443 }
3444 break;
3445 case 'n':
3446 flags.dryrun = B_TRUE;
3447 break;
3448 case 'o':
3449 if ((propval = strchr(optarg, '=')) != NULL) {
3450 *propval = '\0';
3451 propval++;
3452 if (add_prop_list(optarg, propval,
3453 &props, B_TRUE) != 0) {
3454 if (props)
3455 nvlist_free(props);
3456 usage(B_FALSE);
3457 }
3458 } else {
3459 mntopts = optarg;
3460 }
3461 break;
3462 case ':':
3463 (void) fprintf(stderr, gettext("missing argument for "
3464 "'%c' option\n"), optopt);
3465 usage(B_FALSE);
3466 break;
3467 case '?':
3468 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3469 optopt);
3470 usage(B_FALSE);
3471 break;
3472 }
3473 }
3474
3475 if (!flags.import && mntopts != NULL) {
3476 (void) fprintf(stderr, gettext("setting mntopts is only "
3477 "valid when importing the pool\n"));
3478 usage(B_FALSE);
3479 }
3480
3481 argc -= optind;
3482 argv += optind;
3483
3484 if (argc < 1) {
3485 (void) fprintf(stderr, gettext("Missing pool name\n"));
3486 usage(B_FALSE);
3487 }
3488 if (argc < 2) {
3489 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3490 usage(B_FALSE);
3491 }
3492
3493 srcpool = argv[0];
3494 newpool = argv[1];
3495
3496 argc -= 2;
3497 argv += 2;
3498
3499 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3500 return (1);
3501
3502 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3503 if (config == NULL) {
3504 ret = 1;
3505 } else {
3506 if (flags.dryrun) {
3507 (void) printf(gettext("would create '%s' with the "
3508 "following layout:\n\n"), newpool);
3509 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3510 }
3511 nvlist_free(config);
3512 }
3513
3514 zpool_close(zhp);
3515
3516 if (ret != 0 || flags.dryrun || !flags.import)
3517 return (ret);
3518
3519 /*
3520 * The split was successful. Now we need to open the new
3521 * pool and import it.
3522 */
3523 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3524 return (1);
3525 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3526 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3527 ret = 1;
af909a10 3528 (void) fprintf(stderr, gettext("Split was successful, but "
428870ff
BB
3529 "the datasets could not all be mounted\n"));
3530 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3531 "different altroot\n"), "zpool import");
3532 }
3533 zpool_close(zhp);
3534
3535 return (ret);
3536}
3537
3538
3539
34dc7c2f
BB
3540/*
3541 * zpool online <pool> <device> ...
3542 */
3543int
3544zpool_do_online(int argc, char **argv)
3545{
3546 int c, i;
3547 char *poolname;
3548 zpool_handle_t *zhp;
3549 int ret = 0;
3550 vdev_state_t newstate;
9babb374 3551 int flags = 0;
34dc7c2f
BB
3552
3553 /* check options */
9babb374 3554 while ((c = getopt(argc, argv, "et")) != -1) {
34dc7c2f 3555 switch (c) {
9babb374
BB
3556 case 'e':
3557 flags |= ZFS_ONLINE_EXPAND;
3558 break;
34dc7c2f
BB
3559 case 't':
3560 case '?':
3561 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3562 optopt);
3563 usage(B_FALSE);
3564 }
3565 }
3566
3567 argc -= optind;
3568 argv += optind;
3569
3570 /* get pool name and check number of arguments */
3571 if (argc < 1) {
3572 (void) fprintf(stderr, gettext("missing pool name\n"));
3573 usage(B_FALSE);
3574 }
3575 if (argc < 2) {
3576 (void) fprintf(stderr, gettext("missing device name\n"));
3577 usage(B_FALSE);
3578 }
3579
3580 poolname = argv[0];
3581
3582 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3583 return (1);
3584
3585 for (i = 1; i < argc; i++) {
9babb374 3586 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
34dc7c2f
BB
3587 if (newstate != VDEV_STATE_HEALTHY) {
3588 (void) printf(gettext("warning: device '%s' "
3589 "onlined, but remains in faulted state\n"),
3590 argv[i]);
3591 if (newstate == VDEV_STATE_FAULTED)
3592 (void) printf(gettext("use 'zpool "
3593 "clear' to restore a faulted "
3594 "device\n"));
3595 else
3596 (void) printf(gettext("use 'zpool "
3597 "replace' to replace devices "
3598 "that are no longer present\n"));
3599 }
3600 } else {
3601 ret = 1;
3602 }
3603 }
3604
3605 zpool_close(zhp);
3606
3607 return (ret);
3608}
3609
3610/*
3611 * zpool offline [-ft] <pool> <device> ...
3612 *
3613 * -f Force the device into the offline state, even if doing
3614 * so would appear to compromise pool availability.
3615 * (not supported yet)
3616 *
3617 * -t Only take the device off-line temporarily. The offline
3618 * state will not be persistent across reboots.
3619 */
3620/* ARGSUSED */
3621int
3622zpool_do_offline(int argc, char **argv)
3623{
3624 int c, i;
3625 char *poolname;
3626 zpool_handle_t *zhp;
3627 int ret = 0;
3628 boolean_t istmp = B_FALSE;
3629
3630 /* check options */
3631 while ((c = getopt(argc, argv, "ft")) != -1) {
3632 switch (c) {
3633 case 't':
3634 istmp = B_TRUE;
3635 break;
3636 case 'f':
3637 case '?':
3638 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3639 optopt);
3640 usage(B_FALSE);
3641 }
3642 }
3643
3644 argc -= optind;
3645 argv += optind;
3646
3647 /* get pool name and check number of arguments */
3648 if (argc < 1) {
3649 (void) fprintf(stderr, gettext("missing pool name\n"));
3650 usage(B_FALSE);
3651 }
3652 if (argc < 2) {
3653 (void) fprintf(stderr, gettext("missing device name\n"));
3654 usage(B_FALSE);
3655 }
3656
3657 poolname = argv[0];
3658
3659 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3660 return (1);
3661
3662 for (i = 1; i < argc; i++) {
3663 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3664 ret = 1;
3665 }
3666
3667 zpool_close(zhp);
3668
3669 return (ret);
3670}
3671
3672/*
3673 * zpool clear <pool> [device]
3674 *
3675 * Clear all errors associated with a pool or a particular device.
3676 */
3677int
3678zpool_do_clear(int argc, char **argv)
3679{
428870ff 3680 int c;
34dc7c2f 3681 int ret = 0;
428870ff
BB
3682 boolean_t dryrun = B_FALSE;
3683 boolean_t do_rewind = B_FALSE;
3684 boolean_t xtreme_rewind = B_FALSE;
3685 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3686 nvlist_t *policy = NULL;
34dc7c2f
BB
3687 zpool_handle_t *zhp;
3688 char *pool, *device;
3689
428870ff
BB
3690 /* check options */
3691 while ((c = getopt(argc, argv, "FnX")) != -1) {
3692 switch (c) {
3693 case 'F':
3694 do_rewind = B_TRUE;
3695 break;
3696 case 'n':
3697 dryrun = B_TRUE;
3698 break;
3699 case 'X':
3700 xtreme_rewind = B_TRUE;
3701 break;
3702 case '?':
3703 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3704 optopt);
3705 usage(B_FALSE);
3706 }
3707 }
3708
3709 argc -= optind;
3710 argv += optind;
3711
3712 if (argc < 1) {
34dc7c2f
BB
3713 (void) fprintf(stderr, gettext("missing pool name\n"));
3714 usage(B_FALSE);
3715 }
3716
428870ff 3717 if (argc > 2) {
34dc7c2f
BB
3718 (void) fprintf(stderr, gettext("too many arguments\n"));
3719 usage(B_FALSE);
3720 }
3721
428870ff
BB
3722 if ((dryrun || xtreme_rewind) && !do_rewind) {
3723 (void) fprintf(stderr,
3724 gettext("-n or -X only meaningful with -F\n"));
3725 usage(B_FALSE);
3726 }
3727 if (dryrun)
3728 rewind_policy = ZPOOL_TRY_REWIND;
3729 else if (do_rewind)
3730 rewind_policy = ZPOOL_DO_REWIND;
3731 if (xtreme_rewind)
3732 rewind_policy |= ZPOOL_EXTREME_REWIND;
3733
3734 /* In future, further rewind policy choices can be passed along here */
3735 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3736 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3737 return (1);
3738
3739 pool = argv[0];
3740 device = argc == 2 ? argv[1] : NULL;
34dc7c2f 3741
428870ff
BB
3742 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3743 nvlist_free(policy);
34dc7c2f 3744 return (1);
428870ff 3745 }
34dc7c2f 3746
428870ff 3747 if (zpool_clear(zhp, device, policy) != 0)
34dc7c2f
BB
3748 ret = 1;
3749
3750 zpool_close(zhp);
3751
428870ff
BB
3752 nvlist_free(policy);
3753
34dc7c2f
BB
3754 return (ret);
3755}
3756
3541dc6d
GA
3757/*
3758 * zpool reguid <pool>
3759 */
3760int
3761zpool_do_reguid(int argc, char **argv)
3762{
3763 int c;
3764 char *poolname;
3765 zpool_handle_t *zhp;
3766 int ret = 0;
3767
3768 /* check options */
3769 while ((c = getopt(argc, argv, "")) != -1) {
3770 switch (c) {
3771 case '?':
3772 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3773 optopt);
3774 usage(B_FALSE);
3775 }
3776 }
3777
3778 argc -= optind;
3779 argv += optind;
3780
3781 /* get pool name and check number of arguments */
3782 if (argc < 1) {
3783 (void) fprintf(stderr, gettext("missing pool name\n"));
3784 usage(B_FALSE);
3785 }
3786
3787 if (argc > 1) {
3788 (void) fprintf(stderr, gettext("too many arguments\n"));
3789 usage(B_FALSE);
3790 }
3791
3792 poolname = argv[0];
3793 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3794 return (1);
3795
3796 ret = zpool_reguid(zhp);
3797
3798 zpool_close(zhp);
3799 return (ret);
3800}
3801
3802
1bd201e7
CS
3803/*
3804 * zpool reopen <pool>
3805 *
3806 * Reopen the pool so that the kernel can update the sizes of all vdevs.
1bd201e7
CS
3807 */
3808int
3809zpool_do_reopen(int argc, char **argv)
3810{
5853fe79 3811 int c;
1bd201e7
CS
3812 int ret = 0;
3813 zpool_handle_t *zhp;
3814 char *pool;
3815
5853fe79
GW
3816 /* check options */
3817 while ((c = getopt(argc, argv, "")) != -1) {
3818 switch (c) {
3819 case '?':
3820 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3821 optopt);
3822 usage(B_FALSE);
3823 }
3824 }
3825
1bd201e7
CS
3826 argc--;
3827 argv++;
3828
5853fe79
GW
3829 if (argc < 1) {
3830 (void) fprintf(stderr, gettext("missing pool name\n"));
3831 usage(B_FALSE);
3832 }
3833
3834 if (argc > 1) {
3835 (void) fprintf(stderr, gettext("too many arguments\n"));
3836 usage(B_FALSE);
3837 }
1bd201e7
CS
3838
3839 pool = argv[0];
3840 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3841 return (1);
3842
3843 ret = zpool_reopen(zhp);
3844 zpool_close(zhp);
3845 return (ret);
3846}
3847
34dc7c2f
BB
3848typedef struct scrub_cbdata {
3849 int cb_type;
3850 int cb_argc;
3851 char **cb_argv;
3852} scrub_cbdata_t;
3853
3854int
3855scrub_callback(zpool_handle_t *zhp, void *data)
3856{
3857 scrub_cbdata_t *cb = data;
3858 int err;
3859
3860 /*
3861 * Ignore faulted pools.
3862 */
3863 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3864 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3865 "currently unavailable\n"), zpool_get_name(zhp));
3866 return (1);
3867 }
3868
428870ff 3869 err = zpool_scan(zhp, cb->cb_type);
34dc7c2f
BB
3870
3871 return (err != 0);
3872}
3873
3874/*
3875 * zpool scrub [-s] <pool> ...
3876 *
3877 * -s Stop. Stops any in-progress scrub.
3878 */
3879int
3880zpool_do_scrub(int argc, char **argv)
3881{
3882 int c;
3883 scrub_cbdata_t cb;
3884
428870ff 3885 cb.cb_type = POOL_SCAN_SCRUB;
34dc7c2f
BB
3886
3887 /* check options */
3888 while ((c = getopt(argc, argv, "s")) != -1) {
3889 switch (c) {
3890 case 's':
428870ff 3891 cb.cb_type = POOL_SCAN_NONE;
34dc7c2f
BB
3892 break;
3893 case '?':
3894 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3895 optopt);
3896 usage(B_FALSE);
3897 }
3898 }
3899
3900 cb.cb_argc = argc;
3901 cb.cb_argv = argv;
3902 argc -= optind;
3903 argv += optind;
3904
3905 if (argc < 1) {
3906 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3907 usage(B_FALSE);
3908 }
3909
3910 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3911}
3912
3913typedef struct status_cbdata {
3914 int cb_count;
3915 boolean_t cb_allpools;
3916 boolean_t cb_verbose;
3917 boolean_t cb_explain;
3918 boolean_t cb_first;
428870ff 3919 boolean_t cb_dedup_stats;
34dc7c2f
BB
3920} status_cbdata_t;
3921
3922/*
3923 * Print out detailed scrub status.
3924 */
3925void
428870ff 3926print_scan_status(pool_scan_stat_t *ps)
34dc7c2f 3927{
428870ff 3928 time_t start, end;
572e2857 3929 uint64_t elapsed, mins_left, hours_left;
428870ff
BB
3930 uint64_t pass_exam, examined, total;
3931 uint_t rate;
34dc7c2f 3932 double fraction_done;
428870ff 3933 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
34dc7c2f 3934
24024589 3935 (void) printf(gettext(" scan: "));
34dc7c2f 3936
428870ff
BB
3937 /* If there's never been a scan, there's not much to say. */
3938 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3939 ps->pss_func >= POOL_SCAN_FUNCS) {
34dc7c2f
BB
3940 (void) printf(gettext("none requested\n"));
3941 return;
3942 }
3943
428870ff
BB
3944 start = ps->pss_start_time;
3945 end = ps->pss_end_time;
3946 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
34dc7c2f 3947
428870ff
BB
3948 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3949 ps->pss_func == POOL_SCAN_RESILVER);
3950 /*
3951 * Scan is finished or canceled.
3952 */
3953 if (ps->pss_state == DSS_FINISHED) {
3954 uint64_t minutes_taken = (end - start) / 60;
d4ed6673 3955 char *fmt = NULL;
428870ff
BB
3956
3957 if (ps->pss_func == POOL_SCAN_SCRUB) {
3958 fmt = gettext("scrub repaired %s in %lluh%um with "
3959 "%llu errors on %s");
3960 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3961 fmt = gettext("resilvered %s in %lluh%um with "
3962 "%llu errors on %s");
3963 }
3964 /* LINTED */
3965 (void) printf(fmt, processed_buf,
34dc7c2f
BB
3966 (u_longlong_t)(minutes_taken / 60),
3967 (uint_t)(minutes_taken % 60),
428870ff
BB
3968 (u_longlong_t)ps->pss_errors,
3969 ctime((time_t *)&end));
3970 return;
3971 } else if (ps->pss_state == DSS_CANCELED) {
3972 if (ps->pss_func == POOL_SCAN_SCRUB) {
3973 (void) printf(gettext("scrub canceled on %s"),
3974 ctime(&end));
3975 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3976 (void) printf(gettext("resilver canceled on %s"),
3977 ctime(&end));
3978 }
34dc7c2f
BB
3979 return;
3980 }
3981
428870ff
BB
3982 assert(ps->pss_state == DSS_SCANNING);
3983
3984 /*
3985 * Scan is in progress.
3986 */
3987 if (ps->pss_func == POOL_SCAN_SCRUB) {
3988 (void) printf(gettext("scrub in progress since %s"),
3989 ctime(&start));
3990 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3991 (void) printf(gettext("resilver in progress since %s"),
3992 ctime(&start));
3993 }
34dc7c2f 3994
428870ff
BB
3995 examined = ps->pss_examined ? ps->pss_examined : 1;
3996 total = ps->pss_to_examine;
34dc7c2f 3997 fraction_done = (double)examined / total;
428870ff
BB
3998
3999 /* elapsed time for this pass */
4000 elapsed = time(NULL) - ps->pss_pass_start;
4001 elapsed = elapsed ? elapsed : 1;
4002 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4003 rate = pass_exam / elapsed;
4004 rate = rate ? rate : 1;
4005 mins_left = ((total - examined) / rate) / 60;
572e2857 4006 hours_left = mins_left / 60;
428870ff
BB
4007
4008 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4009 zfs_nicenum(total, total_buf, sizeof (total_buf));
4010 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4011
572e2857
BB
4012 /*
4013 * do not print estimated time if hours_left is more than 30 days
4014 */
4015 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4016 examined_buf, total_buf, rate_buf);
4017 if (hours_left < (30 * 24)) {
4018 (void) printf(gettext(", %lluh%um to go\n"),
4019 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4020 } else {
4021 (void) printf(gettext(
4022 ", (scan is slow, no estimated time)\n"));
4023 }
428870ff
BB
4024
4025 if (ps->pss_func == POOL_SCAN_RESILVER) {
4026 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4027 processed_buf, 100 * fraction_done);
4028 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4029 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4030 processed_buf, 100 * fraction_done);
4031 }
34dc7c2f
BB
4032}
4033
34dc7c2f
BB
4034static void
4035print_error_log(zpool_handle_t *zhp)
4036{
4037 nvlist_t *nverrlist = NULL;
4038 nvpair_t *elem;
4039 char *pathname;
4040 size_t len = MAXPATHLEN * 2;
4041
4042 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4043 (void) printf("errors: List of errors unavailable "
4044 "(insufficient privileges)\n");
4045 return;
4046 }
4047
4048 (void) printf("errors: Permanent errors have been "
4049 "detected in the following files:\n\n");
4050
4051 pathname = safe_malloc(len);
4052 elem = NULL;
4053 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4054 nvlist_t *nv;
4055 uint64_t dsobj, obj;
4056
4057 verify(nvpair_value_nvlist(elem, &nv) == 0);
4058 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4059 &dsobj) == 0);
4060 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4061 &obj) == 0);
4062 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4063 (void) printf("%7s %s\n", "", pathname);
4064 }
4065 free(pathname);
4066 nvlist_free(nverrlist);
4067}
4068
4069static void
4070print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4071 int namewidth)
4072{
4073 uint_t i;
4074 char *name;
4075
4076 if (nspares == 0)
4077 return;
4078
4079 (void) printf(gettext("\tspares\n"));
4080
4081 for (i = 0; i < nspares; i++) {
428870ff 4082 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
34dc7c2f 4083 print_status_config(zhp, name, spares[i],
9babb374 4084 namewidth, 2, B_TRUE);
34dc7c2f
BB
4085 free(name);
4086 }
4087}
4088
4089static void
4090print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4091 int namewidth)
4092{
4093 uint_t i;
4094 char *name;
4095
4096 if (nl2cache == 0)
4097 return;
4098
4099 (void) printf(gettext("\tcache\n"));
4100
4101 for (i = 0; i < nl2cache; i++) {
428870ff 4102 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
34dc7c2f 4103 print_status_config(zhp, name, l2cache[i],
9babb374 4104 namewidth, 2, B_FALSE);
34dc7c2f
BB
4105 free(name);
4106 }
4107}
4108
428870ff
BB
4109static void
4110print_dedup_stats(nvlist_t *config)
4111{
4112 ddt_histogram_t *ddh;
4113 ddt_stat_t *dds;
4114 ddt_object_t *ddo;
4115 uint_t c;
4116
4117 /*
4118 * If the pool was faulted then we may not have been able to
32a9872b 4119 * obtain the config. Otherwise, if we have anything in the dedup
428870ff
BB
4120 * table continue processing the stats.
4121 */
4122 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
24024589 4123 (uint64_t **)&ddo, &c) != 0)
428870ff
BB
4124 return;
4125
4126 (void) printf("\n");
24024589
YP
4127 (void) printf(gettext(" dedup: "));
4128 if (ddo->ddo_count == 0) {
4129 (void) printf(gettext("no DDT entries\n"));
4130 return;
4131 }
4132
428870ff
BB
4133 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4134 (u_longlong_t)ddo->ddo_count,
4135 (u_longlong_t)ddo->ddo_dspace,
4136 (u_longlong_t)ddo->ddo_mspace);
4137
4138 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4139 (uint64_t **)&dds, &c) == 0);
4140 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4141 (uint64_t **)&ddh, &c) == 0);
4142 zpool_dump_ddt(dds, ddh);
4143}
4144
34dc7c2f
BB
4145/*
4146 * Display a summary of pool status. Displays a summary such as:
4147 *
4148 * pool: tank
4149 * status: DEGRADED
4150 * reason: One or more devices ...
3cee2262 4151 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
34dc7c2f
BB
4152 * config:
4153 * mirror DEGRADED
4154 * c1t0d0 OK
4155 * c2t0d0 UNAVAIL
4156 *
4157 * When given the '-v' option, we print out the complete config. If the '-e'
4158 * option is specified, then we print out error rate information as well.
4159 */
4160int
4161status_callback(zpool_handle_t *zhp, void *data)
4162{
4163 status_cbdata_t *cbp = data;
4164 nvlist_t *config, *nvroot;
4165 char *msgid;
731782ec 4166 zpool_status_t reason;
ffe9d382 4167 zpool_errata_t errata;
34dc7c2f
BB
4168 const char *health;
4169 uint_t c;
4170 vdev_stat_t *vs;
4171
4172 config = zpool_get_config(zhp, NULL);
ffe9d382 4173 reason = zpool_get_status(zhp, &msgid, &errata);
34dc7c2f
BB
4174
4175 cbp->cb_count++;
4176
4177 /*
4178 * If we were given 'zpool status -x', only report those pools with
4179 * problems.
4180 */
c5b247f3
TC
4181 if (cbp->cb_explain &&
4182 (reason == ZPOOL_STATUS_OK ||
4183 reason == ZPOOL_STATUS_VERSION_OLDER ||
4184 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
34dc7c2f
BB
4185 if (!cbp->cb_allpools) {
4186 (void) printf(gettext("pool '%s' is healthy\n"),
4187 zpool_get_name(zhp));
4188 if (cbp->cb_first)
4189 cbp->cb_first = B_FALSE;
4190 }
4191 return (0);
4192 }
4193
4194 if (cbp->cb_first)
4195 cbp->cb_first = B_FALSE;
4196 else
4197 (void) printf("\n");
4198
4199 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4200 &nvroot) == 0);
428870ff 4201 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
4202 (uint64_t **)&vs, &c) == 0);
4203 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4204
4205 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4206 (void) printf(gettext(" state: %s\n"), health);
4207
4208 switch (reason) {
4209 case ZPOOL_STATUS_MISSING_DEV_R:
4210 (void) printf(gettext("status: One or more devices could not "
4211 "be opened. Sufficient replicas exist for\n\tthe pool to "
4212 "continue functioning in a degraded state.\n"));
4213 (void) printf(gettext("action: Attach the missing device and "
4214 "online it using 'zpool online'.\n"));
4215 break;
4216
4217 case ZPOOL_STATUS_MISSING_DEV_NR:
4218 (void) printf(gettext("status: One or more devices could not "
4219 "be opened. There are insufficient\n\treplicas for the "
4220 "pool to continue functioning.\n"));
4221 (void) printf(gettext("action: Attach the missing device and "
4222 "online it using 'zpool online'.\n"));
4223 break;
4224
4225 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4226 (void) printf(gettext("status: One or more devices could not "
4227 "be used because the label is missing or\n\tinvalid. "
4228 "Sufficient replicas exist for the pool to continue\n\t"
4229 "functioning in a degraded state.\n"));
4230 (void) printf(gettext("action: Replace the device using "
4231 "'zpool replace'.\n"));
4232 break;
4233
4234 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4235 (void) printf(gettext("status: One or more devices could not "
4236 "be used because the label is missing \n\tor invalid. "
4237 "There are insufficient replicas for the pool to "
4238 "continue\n\tfunctioning.\n"));
428870ff
BB
4239 zpool_explain_recover(zpool_get_handle(zhp),
4240 zpool_get_name(zhp), reason, config);
34dc7c2f
BB
4241 break;
4242
4243 case ZPOOL_STATUS_FAILING_DEV:
4244 (void) printf(gettext("status: One or more devices has "
4245 "experienced an unrecoverable error. An\n\tattempt was "
4246 "made to correct the error. Applications are "
4247 "unaffected.\n"));
4248 (void) printf(gettext("action: Determine if the device needs "
4249 "to be replaced, and clear the errors\n\tusing "
4250 "'zpool clear' or replace the device with 'zpool "
4251 "replace'.\n"));
4252 break;
4253
4254 case ZPOOL_STATUS_OFFLINE_DEV:
4255 (void) printf(gettext("status: One or more devices has "
4256 "been taken offline by the administrator.\n\tSufficient "
4257 "replicas exist for the pool to continue functioning in "
4258 "a\n\tdegraded state.\n"));
4259 (void) printf(gettext("action: Online the device using "
4260 "'zpool online' or replace the device with\n\t'zpool "
4261 "replace'.\n"));
4262 break;
4263
45d1cae3
BB
4264 case ZPOOL_STATUS_REMOVED_DEV:
4265 (void) printf(gettext("status: One or more devices has "
4266 "been removed by the administrator.\n\tSufficient "
4267 "replicas exist for the pool to continue functioning in "
4268 "a\n\tdegraded state.\n"));
4269 (void) printf(gettext("action: Online the device using "
4270 "'zpool online' or replace the device with\n\t'zpool "
4271 "replace'.\n"));
4272 break;
4273
34dc7c2f
BB
4274 case ZPOOL_STATUS_RESILVERING:
4275 (void) printf(gettext("status: One or more devices is "
4276 "currently being resilvered. The pool will\n\tcontinue "
4277 "to function, possibly in a degraded state.\n"));
4278 (void) printf(gettext("action: Wait for the resilver to "
4279 "complete.\n"));
4280 break;
4281
4282 case ZPOOL_STATUS_CORRUPT_DATA:
4283 (void) printf(gettext("status: One or more devices has "
4284 "experienced an error resulting in data\n\tcorruption. "
4285 "Applications may be affected.\n"));
4286 (void) printf(gettext("action: Restore the file in question "
4287 "if possible. Otherwise restore the\n\tentire pool from "
4288 "backup.\n"));
4289 break;
4290
4291 case ZPOOL_STATUS_CORRUPT_POOL:
4292 (void) printf(gettext("status: The pool metadata is corrupted "
4293 "and the pool cannot be opened.\n"));
428870ff
BB
4294 zpool_explain_recover(zpool_get_handle(zhp),
4295 zpool_get_name(zhp), reason, config);
34dc7c2f
BB
4296 break;
4297
4298 case ZPOOL_STATUS_VERSION_OLDER:
b9b24bb4
CS
4299 (void) printf(gettext("status: The pool is formatted using a "
4300 "legacy on-disk format. The pool can\n\tstill be used, "
4301 "but some features are unavailable.\n"));
34dc7c2f
BB
4302 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4303 "upgrade'. Once this is done, the\n\tpool will no longer "
f52b31ea
BB
4304 "be accessible on software that does not support\n\t"
4305 "feature flags.\n"));
34dc7c2f
BB
4306 break;
4307
4308 case ZPOOL_STATUS_VERSION_NEWER:
4309 (void) printf(gettext("status: The pool has been upgraded to a "
4310 "newer, incompatible on-disk version.\n\tThe pool cannot "
4311 "be accessed on this system.\n"));
4312 (void) printf(gettext("action: Access the pool from a system "
4313 "running more recent software, or\n\trestore the pool from "
4314 "backup.\n"));
4315 break;
4316
b9b24bb4
CS
4317 case ZPOOL_STATUS_FEAT_DISABLED:
4318 (void) printf(gettext("status: Some supported features are not "
4319 "enabled on the pool. The pool can\n\tstill be used, but "
4320 "some features are unavailable.\n"));
4321 (void) printf(gettext("action: Enable all features using "
4322 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4323 "longer be accessible by software that does not support\n\t"
4324 "the features. See zpool-features(5) for details.\n"));
4325 break;
4326
9ae529ec
CS
4327 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4328 (void) printf(gettext("status: The pool cannot be accessed on "
4329 "this system because it uses the\n\tfollowing feature(s) "
4330 "not supported on this system:\n"));
4331 zpool_print_unsup_feat(config);
4332 (void) printf("\n");
4333 (void) printf(gettext("action: Access the pool from a system "
4334 "that supports the required feature(s),\n\tor restore the "
4335 "pool from backup.\n"));
4336 break;
4337
4338 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4339 (void) printf(gettext("status: The pool can only be accessed "
4340 "in read-only mode on this system. It\n\tcannot be "
4341 "accessed in read-write mode because it uses the "
4342 "following\n\tfeature(s) not supported on this system:\n"));
4343 zpool_print_unsup_feat(config);
4344 (void) printf("\n");
4345 (void) printf(gettext("action: The pool cannot be accessed in "
4346 "read-write mode. Import the pool with\n"
4347 "\t\"-o readonly=on\", access the pool from a system that "
4348 "supports the\n\trequired feature(s), or restore the "
4349 "pool from backup.\n"));
4350 break;
4351
34dc7c2f
BB
4352 case ZPOOL_STATUS_FAULTED_DEV_R:
4353 (void) printf(gettext("status: One or more devices are "
4354 "faulted in response to persistent errors.\n\tSufficient "
4355 "replicas exist for the pool to continue functioning "
4356 "in a\n\tdegraded state.\n"));
4357 (void) printf(gettext("action: Replace the faulted device, "
4358 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4359 break;
4360
4361 case ZPOOL_STATUS_FAULTED_DEV_NR:
4362 (void) printf(gettext("status: One or more devices are "
4363 "faulted in response to persistent errors. There are "
4364 "insufficient replicas for the pool to\n\tcontinue "
4365 "functioning.\n"));
4366 (void) printf(gettext("action: Destroy and re-create the pool "
4367 "from a backup source. Manually marking the device\n"
4368 "\trepaired using 'zpool clear' may allow some data "
4369 "to be recovered.\n"));
4370 break;
4371
b128c09f
BB
4372 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4373 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4374 (void) printf(gettext("status: One or more devices are "
4375 "faulted in response to IO failures.\n"));
4376 (void) printf(gettext("action: Make sure the affected devices "
4377 "are connected, then run 'zpool clear'.\n"));
4378 break;
4379
4380 case ZPOOL_STATUS_BAD_LOG:
4381 (void) printf(gettext("status: An intent log record "
4382 "could not be read.\n"
4383 "\tWaiting for adminstrator intervention to fix the "
4384 "faulted pool.\n"));
4385 (void) printf(gettext("action: Either restore the affected "
4386 "device(s) and run 'zpool online',\n"
4387 "\tor ignore the intent log records by running "
4388 "'zpool clear'.\n"));
4389 break;
4390
ffe9d382
BB
4391 case ZPOOL_STATUS_ERRATA:
4392 (void) printf(gettext("status: Errata #%d detected.\n"),
4393 errata);
4394
4395 switch (errata) {
4396 case ZPOOL_ERRATA_NONE:
4397 break;
4398
4f2dcb3e
RY
4399 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
4400 (void) printf(gettext("action: To correct the issue "
4401 "run 'zpool scrub'.\n"));
4402 break;
4403
ffe9d382
BB
4404 default:
4405 /*
4406 * All errata which allow the pool to be imported
4407 * must contain an action message.
4408 */
4409 assert(0);
4410 }
4411 break;
4412
34dc7c2f
BB
4413 default:
4414 /*
4415 * The remaining errors can't actually be generated, yet.
4416 */
4417 assert(reason == ZPOOL_STATUS_OK);
4418 }
4419
4420 if (msgid != NULL)
3cee2262 4421 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
34dc7c2f
BB
4422 msgid);
4423
4424 if (config != NULL) {
4425 int namewidth;
4426 uint64_t nerr;
4427 nvlist_t **spares, **l2cache;
4428 uint_t nspares, nl2cache;
428870ff 4429 pool_scan_stat_t *ps = NULL;
34dc7c2f 4430
428870ff
BB
4431 (void) nvlist_lookup_uint64_array(nvroot,
4432 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4433 print_scan_status(ps);
34dc7c2f
BB
4434
4435 namewidth = max_width(zhp, nvroot, 0, 0);
4436 if (namewidth < 10)
4437 namewidth = 10;
4438
4439 (void) printf(gettext("config:\n\n"));
4440 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4441 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4442 print_status_config(zhp, zpool_get_name(zhp), nvroot,
9babb374 4443 namewidth, 0, B_FALSE);
34dc7c2f 4444
9babb374
BB
4445 if (num_logs(nvroot) > 0)
4446 print_logs(zhp, nvroot, namewidth, B_TRUE);
34dc7c2f
BB
4447 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4448 &l2cache, &nl2cache) == 0)
4449 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4450
4451 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4452 &spares, &nspares) == 0)
4453 print_spares(zhp, spares, nspares, namewidth);
4454
4455 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4456 &nerr) == 0) {
4457 nvlist_t *nverrlist = NULL;
4458
4459 /*
4460 * If the approximate error count is small, get a
4461 * precise count by fetching the entire log and
4462 * uniquifying the results.
4463 */
4464 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4465 zpool_get_errlog(zhp, &nverrlist) == 0) {
4466 nvpair_t *elem;
4467
4468 elem = NULL;
4469 nerr = 0;
4470 while ((elem = nvlist_next_nvpair(nverrlist,
4471 elem)) != NULL) {
4472 nerr++;
4473 }
4474 }
4475 nvlist_free(nverrlist);
4476
4477 (void) printf("\n");
4478
4479 if (nerr == 0)
4480 (void) printf(gettext("errors: No known data "
4481 "errors\n"));
4482 else if (!cbp->cb_verbose)
4483 (void) printf(gettext("errors: %llu data "
4484 "errors, use '-v' for a list\n"),
4485 (u_longlong_t)nerr);
4486 else
4487 print_error_log(zhp);
4488 }
428870ff
BB
4489
4490 if (cbp->cb_dedup_stats)
4491 print_dedup_stats(config);
34dc7c2f
BB
4492 } else {
4493 (void) printf(gettext("config: The configuration cannot be "
4494 "determined.\n"));
4495 }
4496
4497 return (0);
4498}
4499
4500/*
428870ff 4501 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
34dc7c2f
BB
4502 *
4503 * -v Display complete error logs
4504 * -x Display only pools with potential problems
428870ff
BB
4505 * -D Display dedup status (undocumented)
4506 * -T Display a timestamp in date(1) or Unix format
34dc7c2f
BB
4507 *
4508 * Describes the health status of all pools or some subset.
4509 */
4510int
4511zpool_do_status(int argc, char **argv)
4512{
4513 int c;
4514 int ret;
428870ff 4515 unsigned long interval = 0, count = 0;
34dc7c2f
BB
4516 status_cbdata_t cb = { 0 };
4517
4518 /* check options */
428870ff 4519 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
34dc7c2f
BB
4520 switch (c) {
4521 case 'v':
4522 cb.cb_verbose = B_TRUE;
4523 break;
4524 case 'x':
4525 cb.cb_explain = B_TRUE;
4526 break;
428870ff
BB
4527 case 'D':
4528 cb.cb_dedup_stats = B_TRUE;
4529 break;
4530 case 'T':
4531 get_timestamp_arg(*optarg);
4532 break;
34dc7c2f
BB
4533 case '?':
4534 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4535 optopt);
4536 usage(B_FALSE);
4537 }
4538 }
4539
4540 argc -= optind;
4541 argv += optind;
4542
428870ff 4543 get_interval_count(&argc, argv, &interval, &count);
34dc7c2f
BB
4544
4545 if (argc == 0)
4546 cb.cb_allpools = B_TRUE;
4547
428870ff 4548 cb.cb_first = B_TRUE;
34dc7c2f 4549
428870ff
BB
4550 for (;;) {
4551 if (timestamp_fmt != NODATE)
4552 print_timestamp(timestamp_fmt);
34dc7c2f 4553
428870ff
BB
4554 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4555 status_callback, &cb);
4556
4557 if (argc == 0 && cb.cb_count == 0)
42cb3819 4558 (void) fprintf(stderr, gettext("no pools available\n"));
428870ff
BB
4559 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4560 (void) printf(gettext("all pools are healthy\n"));
4561
4562 if (ret != 0)
4563 return (ret);
4564
4565 if (interval == 0)
4566 break;
4567
4568 if (count != 0 && --count == 0)
4569 break;
4570
4571 (void) sleep(interval);
4572 }
4573
4574 return (0);
34dc7c2f
BB
4575}
4576
4577typedef struct upgrade_cbdata {
34dc7c2f 4578 int cb_first;
34dc7c2f
BB
4579 int cb_argc;
4580 uint64_t cb_version;
4581 char **cb_argv;
4582} upgrade_cbdata_t;
4583
b9b24bb4
CS
4584static int
4585upgrade_version(zpool_handle_t *zhp, uint64_t version)
4586{
4587 int ret;
4588 nvlist_t *config;
4589 uint64_t oldversion;
4590
4591 config = zpool_get_config(zhp, NULL);
4592 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4593 &oldversion) == 0);
4594
4595 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4596 assert(oldversion < version);
4597
4598 ret = zpool_upgrade(zhp, version);
4599 if (ret != 0)
4600 return (ret);
4601
4602 if (version >= SPA_VERSION_FEATURES) {
4603 (void) printf(gettext("Successfully upgraded "
4604 "'%s' from version %llu to feature flags.\n"),
4605 zpool_get_name(zhp), (u_longlong_t) oldversion);
4606 } else {
4607 (void) printf(gettext("Successfully upgraded "
4608 "'%s' from version %llu to version %llu.\n"),
4609 zpool_get_name(zhp), (u_longlong_t) oldversion,
4610 (u_longlong_t) version);
4611 }
4612
4613 return (0);
4614}
4615
4616static int
4617upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4618{
4619 int i, ret, count;
4620 boolean_t firstff = B_TRUE;
4621 nvlist_t *enabled = zpool_get_features(zhp);
4622
4623 count = 0;
4624 for (i = 0; i < SPA_FEATURES; i++) {
4625 const char *fname = spa_feature_table[i].fi_uname;
4626 const char *fguid = spa_feature_table[i].fi_guid;
4627 if (!nvlist_exists(enabled, fguid)) {
4628 char *propname;
4629 verify(-1 != asprintf(&propname, "feature@%s", fname));
4630 ret = zpool_set_prop(zhp, propname,
4631 ZFS_FEATURE_ENABLED);
4632 if (ret != 0) {
4633 free(propname);
4634 return (ret);
4635 }
4636 count++;
4637
4638 if (firstff) {
4639 (void) printf(gettext("Enabled the "
4640 "following features on '%s':\n"),
4641 zpool_get_name(zhp));
4642 firstff = B_FALSE;
4643 }
4644 (void) printf(gettext(" %s\n"), fname);
4645 free(propname);
4646 }
4647 }
4648
4649 if (countp != NULL)
4650 *countp = count;
4651 return (0);
4652}
4653
34dc7c2f
BB
4654static int
4655upgrade_cb(zpool_handle_t *zhp, void *arg)
4656{
4657 upgrade_cbdata_t *cbp = arg;
4658 nvlist_t *config;
4659 uint64_t version;
b9b24bb4
CS
4660 boolean_t printnl = B_FALSE;
4661 int ret;
34dc7c2f
BB
4662
4663 config = zpool_get_config(zhp, NULL);
4664 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4665 &version) == 0);
4666
b9b24bb4 4667 assert(SPA_VERSION_IS_SUPPORTED(version));
34dc7c2f 4668
b9b24bb4
CS
4669 if (version < cbp->cb_version) {
4670 cbp->cb_first = B_FALSE;
4671 ret = upgrade_version(zhp, cbp->cb_version);
4672 if (ret != 0)
4673 return (ret);
4674 printnl = B_TRUE;
4675
b9b24bb4
CS
4676 /*
4677 * If they did "zpool upgrade -a", then we could
4678 * be doing ioctls to different pools. We need
4679 * to log this history once to each pool, and bypass
4680 * the normal history logging that happens in main().
4681 */
4682 (void) zpool_log_history(g_zfs, history_str);
4683 log_history = B_FALSE;
b9b24bb4
CS
4684 }
4685
4686 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4687 int count;
4688 ret = upgrade_enable_all(zhp, &count);
4689 if (ret != 0)
4690 return (ret);
4691
4692 if (count > 0) {
34dc7c2f 4693 cbp->cb_first = B_FALSE;
b9b24bb4 4694 printnl = B_TRUE;
34dc7c2f 4695 }
b9b24bb4 4696 }
34dc7c2f 4697
b9b24bb4
CS
4698 if (printnl) {
4699 (void) printf(gettext("\n"));
4700 }
4701
4702 return (0);
4703}
4704
4705static int
4706upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4707{
4708 upgrade_cbdata_t *cbp = arg;
4709 nvlist_t *config;
4710 uint64_t version;
4711
4712 config = zpool_get_config(zhp, NULL);
4713 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4714 &version) == 0);
4715
4716 assert(SPA_VERSION_IS_SUPPORTED(version));
4717
4718 if (version < SPA_VERSION_FEATURES) {
34dc7c2f
BB
4719 if (cbp->cb_first) {
4720 (void) printf(gettext("The following pools are "
b9b24bb4
CS
4721 "formatted with legacy version numbers and can\n"
4722 "be upgraded to use feature flags. After "
4723 "being upgraded, these pools\nwill no "
4724 "longer be accessible by software that does not "
4725 "support feature\nflags.\n\n"));
34dc7c2f
BB
4726 (void) printf(gettext("VER POOL\n"));
4727 (void) printf(gettext("--- ------------\n"));
4728 cbp->cb_first = B_FALSE;
4729 }
4730
4731 (void) printf("%2llu %s\n", (u_longlong_t)version,
4732 zpool_get_name(zhp));
4733 }
4734
b9b24bb4
CS
4735 return (0);
4736}
4737
4738static int
4739upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4740{
4741 upgrade_cbdata_t *cbp = arg;
4742 nvlist_t *config;
4743 uint64_t version;
4744
4745 config = zpool_get_config(zhp, NULL);
4746 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4747 &version) == 0);
4748
4749 if (version >= SPA_VERSION_FEATURES) {
4750 int i;
4751 boolean_t poolfirst = B_TRUE;
4752 nvlist_t *enabled = zpool_get_features(zhp);
4753
4754 for (i = 0; i < SPA_FEATURES; i++) {
4755 const char *fguid = spa_feature_table[i].fi_guid;
4756 const char *fname = spa_feature_table[i].fi_uname;
4757 if (!nvlist_exists(enabled, fguid)) {
4758 if (cbp->cb_first) {
4759 (void) printf(gettext("\nSome "
4760 "supported features are not "
4761 "enabled on the following pools. "
4762 "Once a\nfeature is enabled the "
4763 "pool may become incompatible with "
4764 "software\nthat does not support "
4765 "the feature. See "
4766 "zpool-features(5) for "
4767 "details.\n\n"));
4768 (void) printf(gettext("POOL "
4769 "FEATURE\n"));
4770 (void) printf(gettext("------"
4771 "---------\n"));
4772 cbp->cb_first = B_FALSE;
4773 }
4774
4775 if (poolfirst) {
4776 (void) printf(gettext("%s\n"),
4777 zpool_get_name(zhp));
4778 poolfirst = B_FALSE;
4779 }
4780
4781 (void) printf(gettext(" %s\n"), fname);
4782 }
6f1ffb06
MA
4783 /*
4784 * If they did "zpool upgrade -a", then we could
4785 * be doing ioctls to different pools. We need
4786 * to log this history once to each pool, and bypass
4787 * the normal history logging that happens in main().
4788 */
4789 (void) zpool_log_history(g_zfs, history_str);
4790 log_history = B_FALSE;
b9b24bb4
CS
4791 }
4792 }
4793
4794 return (0);
34dc7c2f
BB
4795}
4796
4797/* ARGSUSED */
4798static int
4799upgrade_one(zpool_handle_t *zhp, void *data)
4800{
b9b24bb4 4801 boolean_t printnl = B_FALSE;
34dc7c2f
BB
4802 upgrade_cbdata_t *cbp = data;
4803 uint64_t cur_version;
4804 int ret;
4805
4806 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4807 (void) printf(gettext("'log' is now a reserved word\n"
4808 "Pool 'log' must be renamed using export and import"
4809 " to upgrade.\n"));
4810 return (1);
4811 }
4812
4813 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4814 if (cur_version > cbp->cb_version) {
4815 (void) printf(gettext("Pool '%s' is already formatted "
b9b24bb4 4816 "using more current version '%llu'.\n\n"),
b8864a23 4817 zpool_get_name(zhp), (u_longlong_t) cur_version);
34dc7c2f
BB
4818 return (0);
4819 }
b9b24bb4
CS
4820
4821 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
34dc7c2f 4822 (void) printf(gettext("Pool '%s' is already formatted "
b9b24bb4
CS
4823 "using version %llu.\n\n"), zpool_get_name(zhp),
4824 (u_longlong_t) cbp->cb_version);
34dc7c2f
BB
4825 return (0);
4826 }
4827
b9b24bb4
CS
4828 if (cur_version != cbp->cb_version) {
4829 printnl = B_TRUE;
4830 ret = upgrade_version(zhp, cbp->cb_version);
4831 if (ret != 0)
4832 return (ret);
4833 }
34dc7c2f 4834
b9b24bb4
CS
4835 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4836 int count = 0;
4837 ret = upgrade_enable_all(zhp, &count);
4838 if (ret != 0)
4839 return (ret);
4840
4841 if (count != 0) {
4842 printnl = B_TRUE;
4843 } else if (cur_version == SPA_VERSION) {
4844 (void) printf(gettext("Pool '%s' already has all "
4845 "supported features enabled.\n"),
4846 zpool_get_name(zhp));
4847 }
4848 }
4849
4850 if (printnl) {
4851 (void) printf(gettext("\n"));
34dc7c2f
BB
4852 }
4853
b9b24bb4 4854 return (0);
34dc7c2f
BB
4855}
4856
4857/*
4858 * zpool upgrade
4859 * zpool upgrade -v
4860 * zpool upgrade [-V version] <-a | pool ...>
4861 *
4862 * With no arguments, display downrev'd ZFS pool available for upgrade.
4863 * Individual pools can be upgraded by specifying the pool, and '-a' will
4864 * upgrade all pools.
4865 */
4866int
4867zpool_do_upgrade(int argc, char **argv)
4868{
4869 int c;
4870 upgrade_cbdata_t cb = { 0 };
4871 int ret = 0;
4872 boolean_t showversions = B_FALSE;
b9b24bb4 4873 boolean_t upgradeall = B_FALSE;
34dc7c2f
BB
4874 char *end;
4875
4876
4877 /* check options */
9babb374 4878 while ((c = getopt(argc, argv, ":avV:")) != -1) {
34dc7c2f
BB
4879 switch (c) {
4880 case 'a':
b9b24bb4 4881 upgradeall = B_TRUE;
34dc7c2f
BB
4882 break;
4883 case 'v':
4884 showversions = B_TRUE;
4885 break;
4886 case 'V':
4887 cb.cb_version = strtoll(optarg, &end, 10);
9ae529ec
CS
4888 if (*end != '\0' ||
4889 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
34dc7c2f
BB
4890 (void) fprintf(stderr,
4891 gettext("invalid version '%s'\n"), optarg);
4892 usage(B_FALSE);
4893 }
4894 break;
9babb374
BB
4895 case ':':
4896 (void) fprintf(stderr, gettext("missing argument for "
4897 "'%c' option\n"), optopt);
4898 usage(B_FALSE);
4899 break;
34dc7c2f
BB
4900 case '?':
4901 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4902 optopt);
4903 usage(B_FALSE);
4904 }
4905 }
4906
4907 cb.cb_argc = argc;
4908 cb.cb_argv = argv;
4909 argc -= optind;
4910 argv += optind;
4911
4912 if (cb.cb_version == 0) {
4913 cb.cb_version = SPA_VERSION;
b9b24bb4 4914 } else if (!upgradeall && argc == 0) {
34dc7c2f
BB
4915 (void) fprintf(stderr, gettext("-V option is "
4916 "incompatible with other arguments\n"));
4917 usage(B_FALSE);
4918 }
4919
4920 if (showversions) {
b9b24bb4 4921 if (upgradeall || argc != 0) {
34dc7c2f
BB
4922 (void) fprintf(stderr, gettext("-v option is "
4923 "incompatible with other arguments\n"));
4924 usage(B_FALSE);
4925 }
b9b24bb4 4926 } else if (upgradeall) {
34dc7c2f
BB
4927 if (argc != 0) {
4928 (void) fprintf(stderr, gettext("-a option should not "
4929 "be used along with a pool name\n"));
4930 usage(B_FALSE);
4931 }
4932 }
4933
9ae529ec
CS
4934 (void) printf(gettext("This system supports ZFS pool feature "
4935 "flags.\n\n"));
34dc7c2f 4936 if (showversions) {
b9b24bb4
CS
4937 int i;
4938
4939 (void) printf(gettext("The following features are "
4940 "supported:\n\n"));
4941 (void) printf(gettext("FEAT DESCRIPTION\n"));
4942 (void) printf("----------------------------------------------"
4943 "---------------\n");
4944 for (i = 0; i < SPA_FEATURES; i++) {
4945 zfeature_info_t *fi = &spa_feature_table[i];
4946 const char *ro = fi->fi_can_readonly ?
4947 " (read-only compatible)" : "";
4948
4949 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4950 (void) printf(" %s\n", fi->fi_desc);
4951 }
4952 (void) printf("\n");
4953
4954 (void) printf(gettext("The following legacy versions are also "
34dc7c2f
BB
4955 "supported:\n\n"));
4956 (void) printf(gettext("VER DESCRIPTION\n"));
4957 (void) printf("--- -----------------------------------------"
4958 "---------------\n");
4959 (void) printf(gettext(" 1 Initial ZFS version\n"));
4960 (void) printf(gettext(" 2 Ditto blocks "
4961 "(replicated metadata)\n"));
4962 (void) printf(gettext(" 3 Hot spares and double parity "
4963 "RAID-Z\n"));
4964 (void) printf(gettext(" 4 zpool history\n"));
4965 (void) printf(gettext(" 5 Compression using the gzip "
4966 "algorithm\n"));
4967 (void) printf(gettext(" 6 bootfs pool property\n"));
4968 (void) printf(gettext(" 7 Separate intent log devices\n"));
4969 (void) printf(gettext(" 8 Delegated administration\n"));
4970 (void) printf(gettext(" 9 refquota and refreservation "
4971 "properties\n"));
4972 (void) printf(gettext(" 10 Cache devices\n"));
b128c09f
BB
4973 (void) printf(gettext(" 11 Improved scrub performance\n"));
4974 (void) printf(gettext(" 12 Snapshot properties\n"));
4975 (void) printf(gettext(" 13 snapused property\n"));
9babb374
BB
4976 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4977 (void) printf(gettext(" 15 user/group space accounting\n"));
4978 (void) printf(gettext(" 16 stmf property support\n"));
45d1cae3 4979 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
428870ff
BB
4980 (void) printf(gettext(" 18 Snapshot user holds\n"));
4981 (void) printf(gettext(" 19 Log device removal\n"));
4982 (void) printf(gettext(" 20 Compression using zle "
4983 "(zero-length encoding)\n"));
4984 (void) printf(gettext(" 21 Deduplication\n"));
4985 (void) printf(gettext(" 22 Received properties\n"));
4986 (void) printf(gettext(" 23 Slim ZIL\n"));
4987 (void) printf(gettext(" 24 System attributes\n"));
4988 (void) printf(gettext(" 25 Improved scrub stats\n"));
4989 (void) printf(gettext(" 26 Improved snapshot deletion "
4990 "performance\n"));
572e2857
BB
4991 (void) printf(gettext(" 27 Improved snapshot creation "
4992 "performance\n"));
4993 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
428870ff
BB
4994 (void) printf(gettext("\nFor more information on a particular "
4995 "version, including supported releases,\n"));
4996 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
b9b24bb4
CS
4997 } else if (argc == 0 && upgradeall) {
4998 cb.cb_first = B_TRUE;
34dc7c2f 4999 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
b9b24bb4
CS
5000 if (ret == 0 && cb.cb_first) {
5001 if (cb.cb_version == SPA_VERSION) {
5002 (void) printf(gettext("All pools are already "
5003 "formatted using feature flags.\n\n"));
5004 (void) printf(gettext("Every feature flags "
5005 "pool already has all supported features "
5006 "enabled.\n"));
5007 } else {
5008 (void) printf(gettext("All pools are already "
5009 "formatted with version %llu or higher.\n"),
5010 (u_longlong_t) cb.cb_version);
34dc7c2f
BB
5011 }
5012 }
b9b24bb4
CS
5013 } else if (argc == 0) {
5014 cb.cb_first = B_TRUE;
5015 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5016 assert(ret == 0);
34dc7c2f 5017
b9b24bb4
CS
5018 if (cb.cb_first) {
5019 (void) printf(gettext("All pools are formatted "
5020 "using feature flags.\n\n"));
5021 } else {
5022 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5023 "for a list of available legacy versions.\n"));
5024 }
5025
5026 cb.cb_first = B_TRUE;
5027 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5028 assert(ret == 0);
5029
5030 if (cb.cb_first) {
5031 (void) printf(gettext("Every feature flags pool has "
5032 "all supported features enabled.\n"));
5033 } else {
5034 (void) printf(gettext("\n"));
34dc7c2f
BB
5035 }
5036 } else {
5037 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5038 upgrade_one, &cb);
5039 }
5040
5041 return (ret);
5042}
5043
5044typedef struct hist_cbdata {
5045 boolean_t first;
6f1ffb06
MA
5046 boolean_t longfmt;
5047 boolean_t internal;
34dc7c2f
BB
5048} hist_cbdata_t;
5049
34dc7c2f
BB
5050/*
5051 * Print out the command history for a specific pool.
5052 */
5053static int
5054get_history_one(zpool_handle_t *zhp, void *data)
5055{
5056 nvlist_t *nvhis;
5057 nvlist_t **records;
5058 uint_t numrecords;
34dc7c2f 5059 int ret, i;
34dc7c2f 5060 hist_cbdata_t *cb = (hist_cbdata_t *)data;
34dc7c2f
BB
5061
5062 cb->first = B_FALSE;
5063
5064 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5065
5066 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5067 return (ret);
5068
5069 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5070 &records, &numrecords) == 0);
5071 for (i = 0; i < numrecords; i++) {
6f1ffb06
MA
5072 nvlist_t *rec = records[i];
5073 char tbuf[30] = "";
34dc7c2f 5074
6f1ffb06
MA
5075 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5076 time_t tsec;
5077 struct tm t;
34dc7c2f 5078
6f1ffb06
MA
5079 tsec = fnvlist_lookup_uint64(records[i],
5080 ZPOOL_HIST_TIME);
5081 (void) localtime_r(&tsec, &t);
5082 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5083 }
5084
5085 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5086 (void) printf("%s %s", tbuf,
5087 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5088 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5089 int ievent =
5090 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5091 if (!cb->internal)
34dc7c2f 5092 continue;
6f1ffb06
MA
5093 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5094 (void) printf("%s unrecognized record:\n",
5095 tbuf);
5096 dump_nvlist(rec, 4);
34dc7c2f 5097 continue;
6f1ffb06
MA
5098 }
5099 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5100 zfs_history_event_names[ievent],
d1d7e268
MK
5101 (longlong_t) fnvlist_lookup_uint64(
5102 rec, ZPOOL_HIST_TXG),
6f1ffb06
MA
5103 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5104 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5105 if (!cb->internal)
5106 continue;
5107 (void) printf("%s [txg:%lld] %s", tbuf,
d1d7e268
MK
5108 (longlong_t) fnvlist_lookup_uint64(
5109 rec, ZPOOL_HIST_TXG),
6f1ffb06
MA
5110 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5111 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5112 (void) printf(" %s (%llu)",
5113 fnvlist_lookup_string(rec,
5114 ZPOOL_HIST_DSNAME),
d1d7e268 5115 (u_longlong_t)fnvlist_lookup_uint64(rec,
6f1ffb06
MA
5116 ZPOOL_HIST_DSID));
5117 }
5118 (void) printf(" %s", fnvlist_lookup_string(rec,
5119 ZPOOL_HIST_INT_STR));
5120 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5121 if (!cb->internal)
5122 continue;
5123 (void) printf("%s ioctl %s\n", tbuf,
5124 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5125 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5126 (void) printf(" input:\n");
5127 dump_nvlist(fnvlist_lookup_nvlist(rec,
5128 ZPOOL_HIST_INPUT_NVL), 8);
5129 }
5130 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5131 (void) printf(" output:\n");
5132 dump_nvlist(fnvlist_lookup_nvlist(rec,
5133 ZPOOL_HIST_OUTPUT_NVL), 8);
5134 }
5135 } else {
5136 if (!cb->internal)
5137 continue;
5138 (void) printf("%s unrecognized record:\n", tbuf);
5139 dump_nvlist(rec, 4);
34dc7c2f 5140 }
34dc7c2f
BB
5141
5142 if (!cb->longfmt) {
5143 (void) printf("\n");
5144 continue;
5145 }
5146 (void) printf(" [");
6f1ffb06
MA
5147 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5148 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5149 struct passwd *pwd = getpwuid(who);
5150 (void) printf("user %d ", (int)who);
5151 if (pwd != NULL)
5152 (void) printf("(%s) ", pwd->pw_name);
34dc7c2f 5153 }
6f1ffb06
MA
5154 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5155 (void) printf("on %s",
5156 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
34dc7c2f 5157 }
6f1ffb06
MA
5158 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5159 (void) printf(":%s",
5160 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
34dc7c2f
BB
5161 }
5162
5163 (void) printf("]");
5164 (void) printf("\n");
5165 }
5166 (void) printf("\n");
5167 nvlist_free(nvhis);
5168
5169 return (ret);
5170}
5171
5172/*
5173 * zpool history <pool>
5174 *
5175 * Displays the history of commands that modified pools.
5176 */
34dc7c2f
BB
5177int
5178zpool_do_history(int argc, char **argv)
5179{
5180 hist_cbdata_t cbdata = { 0 };
5181 int ret;
5182 int c;
5183
5184 cbdata.first = B_TRUE;
5185 /* check options */
5186 while ((c = getopt(argc, argv, "li")) != -1) {
5187 switch (c) {
5188 case 'l':
6f1ffb06 5189 cbdata.longfmt = B_TRUE;
34dc7c2f
BB
5190 break;
5191 case 'i':
6f1ffb06 5192 cbdata.internal = B_TRUE;
34dc7c2f
BB
5193 break;
5194 case '?':
5195 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5196 optopt);
5197 usage(B_FALSE);
5198 }
5199 }
5200 argc -= optind;
5201 argv += optind;
5202
5203 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5204 &cbdata);
5205
5206 if (argc == 0 && cbdata.first == B_TRUE) {
42cb3819 5207 (void) fprintf(stderr, gettext("no pools available\n"));
34dc7c2f
BB
5208 return (0);
5209 }
5210
5211 return (ret);
5212}
5213
26685276
BB
5214typedef struct ev_opts {
5215 int verbose;
c5343ba7 5216 int scripted;
26685276
BB
5217 int follow;
5218 int clear;
5219} ev_opts_t;
5220
5221static void
5222zpool_do_events_short(nvlist_t *nvl)
5223{
5224 char ctime_str[26], str[32], *ptr;
5225 int64_t *tv;
5226 uint_t n;
5227
5228 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5229 memset(str, ' ', 32);
5230 (void) ctime_r((const time_t *)&tv[0], ctime_str);
d1d7e268
MK
5231 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5232 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5233 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5234 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
26685276
BB
5235 (void) printf(gettext("%s "), str);
5236
5237 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5238 (void) printf(gettext("%s\n"), ptr);
5239}
5240
5241static void
5242zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5243{
5244 nvpair_t *nvp;
5245
5246 for (nvp = nvlist_next_nvpair(nvl, NULL);
5247 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5248
5249 data_type_t type = nvpair_type(nvp);
5250 const char *name = nvpair_name(nvp);
5251
5252 boolean_t b;
5253 uint8_t i8;
5254 uint16_t i16;
5255 uint32_t i32;
5256 uint64_t i64;
5257 char *str;
5258 nvlist_t *cnv;
5259
5260 printf(gettext("%*s%s = "), depth, "", name);
5261
5262 switch (type) {
5263 case DATA_TYPE_BOOLEAN:
5264 printf(gettext("%s"), "1");
5265 break;
5266
5267 case DATA_TYPE_BOOLEAN_VALUE:
5268 (void) nvpair_value_boolean_value(nvp, &b);
5269 printf(gettext("%s"), b ? "1" : "0");
5270 break;
5271
5272 case DATA_TYPE_BYTE:
5273 (void) nvpair_value_byte(nvp, &i8);
5274 printf(gettext("0x%x"), i8);
5275 break;
5276
5277 case DATA_TYPE_INT8:
5278 (void) nvpair_value_int8(nvp, (void *)&i8);
5279 printf(gettext("0x%x"), i8);
5280 break;
5281
5282 case DATA_TYPE_UINT8:
5283 (void) nvpair_value_uint8(nvp, &i8);
5284 printf(gettext("0x%x"), i8);
5285 break;
5286
5287 case DATA_TYPE_INT16:
5288 (void) nvpair_value_int16(nvp, (void *)&i16);
5289 printf(gettext("0x%x"), i16);
5290 break;
5291
5292 case DATA_TYPE_UINT16:
5293 (void) nvpair_value_uint16(nvp, &i16);
5294 printf(gettext("0x%x"), i16);
5295 break;
5296
5297 case DATA_TYPE_INT32:
5298 (void) nvpair_value_int32(nvp, (void *)&i32);
5299 printf(gettext("0x%x"), i32);
5300 break;
5301
5302 case DATA_TYPE_UINT32:
5303 (void) nvpair_value_uint32(nvp, &i32);
5304 printf(gettext("0x%x"), i32);
5305 break;
5306
5307 case DATA_TYPE_INT64:
5308 (void) nvpair_value_int64(nvp, (void *)&i64);
5309 printf(gettext("0x%llx"), (u_longlong_t)i64);
5310 break;
5311
5312 case DATA_TYPE_UINT64:
5313 (void) nvpair_value_uint64(nvp, &i64);
5314 printf(gettext("0x%llx"), (u_longlong_t)i64);
5315 break;
5316
5317 case DATA_TYPE_HRTIME:
5318 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5319 printf(gettext("0x%llx"), (u_longlong_t)i64);
5320 break;
5321
5322 case DATA_TYPE_STRING:
5323 (void) nvpair_value_string(nvp, &str);
5324 printf(gettext("\"%s\""), str ? str : "<NULL>");
5325 break;
5326
5327 case DATA_TYPE_NVLIST:
5328 printf(gettext("(embedded nvlist)\n"));
5329 (void) nvpair_value_nvlist(nvp, &cnv);
5330 zpool_do_events_nvprint(cnv, depth + 8);
c5343ba7 5331 printf(gettext("%*s(end %s)"), depth, "", name);
26685276
BB
5332 break;
5333
5334 case DATA_TYPE_NVLIST_ARRAY: {
5335 nvlist_t **val;
5336 uint_t i, nelem;
5337
5338 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5339 printf(gettext("(%d embedded nvlists)\n"), nelem);
5340 for (i = 0; i < nelem; i++) {
5341 printf(gettext("%*s%s[%d] = %s\n"),
d1d7e268 5342 depth, "", name, i, "(embedded nvlist)");
26685276
BB
5343 zpool_do_events_nvprint(val[i], depth + 8);
5344 printf(gettext("%*s(end %s[%i])\n"),
d1d7e268 5345 depth, "", name, i);
26685276
BB
5346 }
5347 printf(gettext("%*s(end %s)\n"), depth, "", name);
5348 }
5349 break;
5350
5351 case DATA_TYPE_INT8_ARRAY: {
5352 int8_t *val;
5353 uint_t i, nelem;
5354
5355 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5356 for (i = 0; i < nelem; i++)
5357 printf(gettext("0x%x "), val[i]);
5358
5359 break;
5360 }
5361
5362 case DATA_TYPE_UINT8_ARRAY: {
5363 uint8_t *val;
5364 uint_t i, nelem;
5365
5366 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5367 for (i = 0; i < nelem; i++)
5368 printf(gettext("0x%x "), val[i]);
5369
5370 break;
5371 }
5372
5373 case DATA_TYPE_INT16_ARRAY: {
5374 int16_t *val;
5375 uint_t i, nelem;
5376
5377 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5378 for (i = 0; i < nelem; i++)
5379 printf(gettext("0x%x "), val[i]);
5380
5381 break;
5382 }
5383
5384 case DATA_TYPE_UINT16_ARRAY: {
5385 uint16_t *val;
5386 uint_t i, nelem;
5387
5388 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5389 for (i = 0; i < nelem; i++)
5390 printf(gettext("0x%x "), val[i]);
5391
5392 break;
5393 }
5394
5395 case DATA_TYPE_INT32_ARRAY: {
5396 int32_t *val;
5397 uint_t i, nelem;
5398
5399 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5400 for (i = 0; i < nelem; i++)
5401 printf(gettext("0x%x "), val[i]);
5402
5403 break;
5404 }
5405
5406 case DATA_TYPE_UINT32_ARRAY: {
5407 uint32_t *val;
5408 uint_t i, nelem;
5409
5410 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5411 for (i = 0; i < nelem; i++)
5412 printf(gettext("0x%x "), val[i]);
5413
5414 break;
5415 }
5416
5417 case DATA_TYPE_INT64_ARRAY: {
5418 int64_t *val;
5419 uint_t i, nelem;
5420
5421 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5422 for (i = 0; i < nelem; i++)
d1d7e268
MK
5423 printf(gettext("0x%llx "),
5424 (u_longlong_t)val[i]);
26685276
BB
5425
5426 break;
5427 }
5428
5429 case DATA_TYPE_UINT64_ARRAY: {
5430 uint64_t *val;
5431 uint_t i, nelem;
5432
5433 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5434 for (i = 0; i < nelem; i++)
d1d7e268
MK
5435 printf(gettext("0x%llx "),
5436 (u_longlong_t)val[i]);
26685276
BB
5437
5438 break;
5439 }
5440
5441 case DATA_TYPE_STRING_ARRAY:
5442 case DATA_TYPE_BOOLEAN_ARRAY:
5443 case DATA_TYPE_BYTE_ARRAY:
5444 case DATA_TYPE_DOUBLE:
5445 case DATA_TYPE_UNKNOWN:
5446 printf(gettext("<unknown>"));
5447 break;
5448 }
5449
5450 printf(gettext("\n"));
5451 }
5452}
5453
5454static int
5455zpool_do_events_next(ev_opts_t *opts)
5456{
5457 nvlist_t *nvl;
5458 int cleanup_fd, ret, dropped;
5459
d1d7e268
MK
5460 cleanup_fd = open(ZFS_DEV, O_RDWR);
5461 VERIFY(cleanup_fd >= 0);
26685276 5462
c5343ba7
BB
5463 if (!opts->scripted)
5464 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
26685276
BB
5465
5466 while (1) {
5467 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5468 !!opts->follow, cleanup_fd);
5469 if (ret || nvl == NULL)
5470 break;
5471
5472 if (dropped > 0)
5473 (void) printf(gettext("dropped %d events\n"), dropped);
5474
5475 zpool_do_events_short(nvl);
5476
5477 if (opts->verbose) {
5478 zpool_do_events_nvprint(nvl, 8);
5479 printf(gettext("\n"));
5480 }
50fe577d 5481 (void) fflush(stdout);
26685276
BB
5482
5483 nvlist_free(nvl);
5484 }
5485
d1d7e268 5486 VERIFY(0 == close(cleanup_fd));
26685276
BB
5487
5488 return (ret);
5489}
5490
5491static int
5492zpool_do_events_clear(ev_opts_t *opts)
5493{
5494 int count, ret;
5495
5496 ret = zpool_events_clear(g_zfs, &count);
5497 if (!ret)
5498 (void) printf(gettext("cleared %d events\n"), count);
5499
5500 return (ret);
5501}
5502
5503/*
5504 * zpool events [-vfc]
5505 *
5506 * Displays events logs by ZFS.
5507 */
5508int
5509zpool_do_events(int argc, char **argv)
5510{
5511 ev_opts_t opts = { 0 };
5512 int ret;
5513 int c;
5514
5515 /* check options */
c5343ba7 5516 while ((c = getopt(argc, argv, "vHfc")) != -1) {
26685276
BB
5517 switch (c) {
5518 case 'v':
5519 opts.verbose = 1;
c5343ba7
BB
5520 break;
5521 case 'H':
5522 opts.scripted = 1;
26685276
BB
5523 break;
5524 case 'f':
5525 opts.follow = 1;
5526 break;
5527 case 'c':
5528 opts.clear = 1;
5529 break;
5530 case '?':
5531 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5532 optopt);
5533 usage(B_FALSE);
5534 }
5535 }
5536 argc -= optind;
5537 argv += optind;
5538
5539 if (opts.clear)
5540 ret = zpool_do_events_clear(&opts);
5541 else
5542 ret = zpool_do_events_next(&opts);
5543
d1d7e268 5544 return (ret);
26685276
BB
5545}
5546
34dc7c2f
BB
5547static int
5548get_callback(zpool_handle_t *zhp, void *data)
5549{
5550 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5551 char value[MAXNAMELEN];
5552 zprop_source_t srctype;
5553 zprop_list_t *pl;
5554
5555 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5556
5557 /*
5558 * Skip the special fake placeholder. This will also skip
5559 * over the name property when 'all' is specified.
5560 */
5561 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5562 pl == cbp->cb_proplist)
5563 continue;
5564
9ae529ec
CS
5565 if (pl->pl_prop == ZPROP_INVAL &&
5566 (zpool_prop_feature(pl->pl_user_prop) ||
5567 zpool_prop_unsupported(pl->pl_user_prop))) {
5568 srctype = ZPROP_SRC_LOCAL;
34dc7c2f 5569
9ae529ec
CS
5570 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5571 value, sizeof (value)) == 0) {
5572 zprop_print_one_property(zpool_get_name(zhp),
5573 cbp, pl->pl_user_prop, value, srctype,
5574 NULL, NULL);
5575 }
5576 } else {
d65e7381
RE
5577 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5578 sizeof (value), &srctype, cbp->cb_literal) != 0)
9ae529ec
CS
5579 continue;
5580
5581 zprop_print_one_property(zpool_get_name(zhp), cbp,
5582 zpool_prop_to_name(pl->pl_prop), value, srctype,
5583 NULL, NULL);
5584 }
34dc7c2f
BB
5585 }
5586 return (0);
5587}
5588
5589int
5590zpool_do_get(int argc, char **argv)
5591{
5592 zprop_get_cbdata_t cb = { 0 };
5593 zprop_list_t fake_name = { 0 };
d65e7381 5594 int c, ret;
34dc7c2f 5595
d65e7381
RE
5596 /* check options */
5597 while ((c = getopt(argc, argv, "p")) != -1) {
5598 switch (c) {
5599 case 'p':
5600 cb.cb_literal = B_TRUE;
5601 break;
5602
5603 case '?':
5604 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5605 optopt);
5606 usage(B_FALSE);
5607 }
5608 }
5609
5610 argc -= optind;
5611 argv += optind;
5612
5613 if (argc < 1) {
9ae529ec
CS
5614 (void) fprintf(stderr, gettext("missing property "
5615 "argument\n"));
34dc7c2f 5616 usage(B_FALSE);
9ae529ec 5617 }
34dc7c2f
BB
5618
5619 cb.cb_first = B_TRUE;
5620 cb.cb_sources = ZPROP_SRC_ALL;
5621 cb.cb_columns[0] = GET_COL_NAME;
5622 cb.cb_columns[1] = GET_COL_PROPERTY;
5623 cb.cb_columns[2] = GET_COL_VALUE;
5624 cb.cb_columns[3] = GET_COL_SOURCE;
5625 cb.cb_type = ZFS_TYPE_POOL;
5626
d65e7381 5627 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
34dc7c2f
BB
5628 usage(B_FALSE);
5629
d65e7381
RE
5630 argc--;
5631 argv++;
5632
34dc7c2f
BB
5633 if (cb.cb_proplist != NULL) {
5634 fake_name.pl_prop = ZPOOL_PROP_NAME;
5635 fake_name.pl_width = strlen(gettext("NAME"));
5636 fake_name.pl_next = cb.cb_proplist;
5637 cb.cb_proplist = &fake_name;
5638 }
5639
d65e7381 5640 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
34dc7c2f
BB
5641 get_callback, &cb);
5642
5643 if (cb.cb_proplist == &fake_name)
5644 zprop_free_list(fake_name.pl_next);
5645 else
5646 zprop_free_list(cb.cb_proplist);
5647
5648 return (ret);
5649}
5650
5651typedef struct set_cbdata {
5652 char *cb_propname;
5653 char *cb_value;
5654 boolean_t cb_any_successful;
5655} set_cbdata_t;
5656
5657int
5658set_callback(zpool_handle_t *zhp, void *data)
5659{
5660 int error;
5661 set_cbdata_t *cb = (set_cbdata_t *)data;
5662
5663 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5664
5665 if (!error)
5666 cb->cb_any_successful = B_TRUE;
5667
5668 return (error);
5669}
5670
5671int
5672zpool_do_set(int argc, char **argv)
5673{
5674 set_cbdata_t cb = { 0 };
5675 int error;
5676
5677 if (argc > 1 && argv[1][0] == '-') {
5678 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5679 argv[1][1]);
5680 usage(B_FALSE);
5681 }
5682
5683 if (argc < 2) {
5684 (void) fprintf(stderr, gettext("missing property=value "
5685 "argument\n"));
5686 usage(B_FALSE);
5687 }
5688
5689 if (argc < 3) {
5690 (void) fprintf(stderr, gettext("missing pool name\n"));
5691 usage(B_FALSE);
5692 }
5693
5694 if (argc > 3) {
5695 (void) fprintf(stderr, gettext("too many pool names\n"));
5696 usage(B_FALSE);
5697 }
5698
5699 cb.cb_propname = argv[1];
5700 cb.cb_value = strchr(cb.cb_propname, '=');
5701 if (cb.cb_value == NULL) {
5702 (void) fprintf(stderr, gettext("missing value in "
5703 "property=value argument\n"));
5704 usage(B_FALSE);
5705 }
5706
5707 *(cb.cb_value) = '\0';
5708 cb.cb_value++;
5709
5710 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5711 set_callback, &cb);
5712
5713 return (error);
5714}
5715
5716static int
5717find_command_idx(char *command, int *idx)
5718{
5719 int i;
5720
5721 for (i = 0; i < NCOMMAND; i++) {
5722 if (command_table[i].name == NULL)
5723 continue;
5724
5725 if (strcmp(command, command_table[i].name) == 0) {
5726 *idx = i;
5727 return (0);
5728 }
5729 }
5730 return (1);
5731}
5732
5733int
5734main(int argc, char **argv)
5735{
5736 int ret;
d4ed6673 5737 int i = 0;
34dc7c2f
BB
5738 char *cmdname;
5739
5740 (void) setlocale(LC_ALL, "");
5741 (void) textdomain(TEXT_DOMAIN);
5742
34dc7c2f
BB
5743 opterr = 0;
5744
5745 /*
5746 * Make sure the user has specified some command.
5747 */
5748 if (argc < 2) {
5749 (void) fprintf(stderr, gettext("missing command\n"));
5750 usage(B_FALSE);
5751 }
5752
5753 cmdname = argv[1];
5754
5755 /*
5756 * Special case '-?'
5757 */
d1d7e268 5758 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
34dc7c2f
BB
5759 usage(B_TRUE);
5760
9b020fd9
BB
5761 if ((g_zfs = libzfs_init()) == NULL)
5762 return (1);
5763
5764 libzfs_print_on_error(g_zfs, B_TRUE);
5765
6f1ffb06 5766 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
34dc7c2f
BB
5767
5768 /*
5769 * Run the appropriate command.
5770 */
5771 if (find_command_idx(cmdname, &i) == 0) {
5772 current_command = &command_table[i];
5773 ret = command_table[i].func(argc - 1, argv + 1);
5774 } else if (strchr(cmdname, '=')) {
5775 verify(find_command_idx("set", &i) == 0);
5776 current_command = &command_table[i];
5777 ret = command_table[i].func(argc, argv);
5778 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5779 /*
5780 * 'freeze' is a vile debugging abomination, so we treat
5781 * it as such.
5782 */
5783 char buf[16384];
5784 int fd = open(ZFS_DEV, O_RDWR);
5785 (void) strcpy((void *)buf, argv[2]);
5786 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5787 } else {
5788 (void) fprintf(stderr, gettext("unrecognized "
5789 "command '%s'\n"), cmdname);
5790 usage(B_FALSE);
d4ed6673 5791 ret = 1;
34dc7c2f
BB
5792 }
5793
6f1ffb06
MA
5794 if (ret == 0 && log_history)
5795 (void) zpool_log_history(g_zfs, history_str);
5796
34dc7c2f
BB
5797 libzfs_fini(g_zfs);
5798
5799 /*
5800 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5801 * for the purposes of running ::findleaks.
5802 */
5803 if (getenv("ZFS_ABORT") != NULL) {
5804 (void) printf("dumping core by request\n");
5805 abort();
5806 }
5807
5808 return (ret);
5809}