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