Illumos 5351, 5352 - scrub pauses
[zfs.git] / cmd / zpool / zpool_main.c
blobbf323ee087ddb4bd2a24fb67fc28cc89a173b7a9
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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) 2011, 2014 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.
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 #include <sys/zfs_ioctl.h>
54 #include <libzfs.h>
56 #include "zpool_util.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 #include "statcommon.h"
62 static int zpool_do_create(int, char **);
63 static int zpool_do_destroy(int, char **);
65 static int zpool_do_add(int, char **);
66 static int zpool_do_remove(int, char **);
67 static int zpool_do_labelclear(int, char **);
69 static int zpool_do_list(int, char **);
70 static int zpool_do_iostat(int, char **);
71 static int zpool_do_status(int, char **);
73 static int zpool_do_online(int, char **);
74 static int zpool_do_offline(int, char **);
75 static int zpool_do_clear(int, char **);
76 static int zpool_do_reopen(int, char **);
78 static int zpool_do_reguid(int, char **);
80 static int zpool_do_attach(int, char **);
81 static int zpool_do_detach(int, char **);
82 static int zpool_do_replace(int, char **);
83 static int zpool_do_split(int, char **);
85 static int zpool_do_scrub(int, char **);
87 static int zpool_do_import(int, char **);
88 static int zpool_do_export(int, char **);
90 static int zpool_do_upgrade(int, char **);
92 static int zpool_do_history(int, char **);
93 static int zpool_do_events(int, char **);
95 static int zpool_do_get(int, char **);
96 static int zpool_do_set(int, char **);
99 * These libumem hooks provide a reasonable set of defaults for the allocator's
100 * debugging facilities.
103 #ifdef DEBUG
104 const char *
105 _umem_debug_init(void)
107 return ("default,verbose"); /* $UMEM_DEBUG setting */
110 const char *
111 _umem_logging_init(void)
113 return ("fail,contents"); /* $UMEM_LOGGING setting */
115 #endif
117 typedef enum {
118 HELP_ADD,
119 HELP_ATTACH,
120 HELP_CLEAR,
121 HELP_CREATE,
122 HELP_DESTROY,
123 HELP_DETACH,
124 HELP_EXPORT,
125 HELP_HISTORY,
126 HELP_IMPORT,
127 HELP_IOSTAT,
128 HELP_LABELCLEAR,
129 HELP_LIST,
130 HELP_OFFLINE,
131 HELP_ONLINE,
132 HELP_REPLACE,
133 HELP_REMOVE,
134 HELP_SCRUB,
135 HELP_STATUS,
136 HELP_UPGRADE,
137 HELP_EVENTS,
138 HELP_GET,
139 HELP_SET,
140 HELP_SPLIT,
141 HELP_REGUID,
142 HELP_REOPEN
143 } zpool_help_t;
146 typedef struct zpool_command {
147 const char *name;
148 int (*func)(int, char **);
149 zpool_help_t usage;
150 } zpool_command_t;
153 * Master command table. Each ZFS command has a name, associated function, and
154 * usage message. The usage messages need to be internationalized, so we have
155 * to have a function to return the usage message based on a command index.
157 * These commands are organized according to how they are displayed in the usage
158 * message. An empty command (one with a NULL name) indicates an empty line in
159 * the generic usage message.
161 static zpool_command_t command_table[] = {
162 { "create", zpool_do_create, HELP_CREATE },
163 { "destroy", zpool_do_destroy, HELP_DESTROY },
164 { NULL },
165 { "add", zpool_do_add, HELP_ADD },
166 { "remove", zpool_do_remove, HELP_REMOVE },
167 { NULL },
168 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
169 { NULL },
170 { "list", zpool_do_list, HELP_LIST },
171 { "iostat", zpool_do_iostat, HELP_IOSTAT },
172 { "status", zpool_do_status, HELP_STATUS },
173 { NULL },
174 { "online", zpool_do_online, HELP_ONLINE },
175 { "offline", zpool_do_offline, HELP_OFFLINE },
176 { "clear", zpool_do_clear, HELP_CLEAR },
177 { "reopen", zpool_do_reopen, HELP_REOPEN },
178 { NULL },
179 { "attach", zpool_do_attach, HELP_ATTACH },
180 { "detach", zpool_do_detach, HELP_DETACH },
181 { "replace", zpool_do_replace, HELP_REPLACE },
182 { "split", zpool_do_split, HELP_SPLIT },
183 { NULL },
184 { "scrub", zpool_do_scrub, HELP_SCRUB },
185 { NULL },
186 { "import", zpool_do_import, HELP_IMPORT },
187 { "export", zpool_do_export, HELP_EXPORT },
188 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
189 { "reguid", zpool_do_reguid, HELP_REGUID },
190 { NULL },
191 { "history", zpool_do_history, HELP_HISTORY },
192 { "events", zpool_do_events, HELP_EVENTS },
193 { NULL },
194 { "get", zpool_do_get, HELP_GET },
195 { "set", zpool_do_set, HELP_SET },
198 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
200 static zpool_command_t *current_command;
201 static char history_str[HIS_MAX_RECORD_LEN];
202 static boolean_t log_history = B_TRUE;
203 static uint_t timestamp_fmt = NODATE;
205 static const char *
206 get_usage(zpool_help_t idx) {
207 switch (idx) {
208 case HELP_ADD:
209 return (gettext("\tadd [-fn] [-o property=value] "
210 "<pool> <vdev> ...\n"));
211 case HELP_ATTACH:
212 return (gettext("\tattach [-f] [-o property=value] "
213 "<pool> <device> <new-device>\n"));
214 case HELP_CLEAR:
215 return (gettext("\tclear [-nF] <pool> [device]\n"));
216 case HELP_CREATE:
217 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
218 "\t [-O file-system-property=value] ... \n"
219 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
220 case HELP_DESTROY:
221 return (gettext("\tdestroy [-f] <pool>\n"));
222 case HELP_DETACH:
223 return (gettext("\tdetach <pool> <device>\n"));
224 case HELP_EXPORT:
225 return (gettext("\texport [-af] <pool> ...\n"));
226 case HELP_HISTORY:
227 return (gettext("\thistory [-il] [<pool>] ...\n"));
228 case HELP_IMPORT:
229 return (gettext("\timport [-d dir] [-D]\n"
230 "\timport [-d dir | -c cachefile] [-F [-n]] <pool | id>\n"
231 "\timport [-o mntopts] [-o property=value] ... \n"
232 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
233 "[-R root] [-F [-n]] -a\n"
234 "\timport [-o mntopts] [-o property=value] ... \n"
235 "\t [-d dir | -c cachefile] [-D] [-f] [-m] [-N] "
236 "[-R root] [-F [-n]]\n"
237 "\t <pool | id> [newpool]\n"));
238 case HELP_IOSTAT:
239 return (gettext("\tiostat [-v] [-T d|u] [pool] ... [interval "
240 "[count]]\n"));
241 case HELP_LABELCLEAR:
242 return (gettext("\tlabelclear [-f] <vdev>\n"));
243 case HELP_LIST:
244 return (gettext("\tlist [-Hv] [-o property[,...]] "
245 "[-T d|u] [pool] ... [interval [count]]\n"));
246 case HELP_OFFLINE:
247 return (gettext("\toffline [-t] <pool> <device> ...\n"));
248 case HELP_ONLINE:
249 return (gettext("\tonline <pool> <device> ...\n"));
250 case HELP_REPLACE:
251 return (gettext("\treplace [-f] [-o property=value] "
252 "<pool> <device> [new-device]\n"));
253 case HELP_REMOVE:
254 return (gettext("\tremove <pool> <device> ...\n"));
255 case HELP_REOPEN:
256 return (gettext("\treopen <pool>\n"));
257 case HELP_SCRUB:
258 return (gettext("\tscrub [-s] <pool> ...\n"));
259 case HELP_STATUS:
260 return (gettext("\tstatus [-vxD] [-T d|u] [pool] ... [interval "
261 "[count]]\n"));
262 case HELP_UPGRADE:
263 return (gettext("\tupgrade\n"
264 "\tupgrade -v\n"
265 "\tupgrade [-V version] <-a | pool ...>\n"));
266 case HELP_EVENTS:
267 return (gettext("\tevents [-vHfc]\n"));
268 case HELP_GET:
269 return (gettext("\tget [-pH] <\"all\" | property[,...]> "
270 "<pool> ...\n"));
271 case HELP_SET:
272 return (gettext("\tset <property=value> <pool> \n"));
273 case HELP_SPLIT:
274 return (gettext("\tsplit [-n] [-R altroot] [-o mntopts]\n"
275 "\t [-o property=value] <pool> <newpool> "
276 "[<device> ...]\n"));
277 case HELP_REGUID:
278 return (gettext("\treguid <pool>\n"));
281 abort();
282 /* NOTREACHED */
287 * Callback routine that will print out a pool property value.
289 static int
290 print_prop_cb(int prop, void *cb)
292 FILE *fp = cb;
294 (void) fprintf(fp, "\t%-15s ", zpool_prop_to_name(prop));
296 if (zpool_prop_readonly(prop))
297 (void) fprintf(fp, " NO ");
298 else
299 (void) fprintf(fp, " YES ");
301 if (zpool_prop_values(prop) == NULL)
302 (void) fprintf(fp, "-\n");
303 else
304 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
306 return (ZPROP_CONT);
310 * Display usage message. If we're inside a command, display only the usage for
311 * that command. Otherwise, iterate over the entire command table and display
312 * a complete usage message.
314 void
315 usage(boolean_t requested)
317 FILE *fp = requested ? stdout : stderr;
319 if (current_command == NULL) {
320 int i;
322 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
323 (void) fprintf(fp,
324 gettext("where 'command' is one of the following:\n\n"));
326 for (i = 0; i < NCOMMAND; i++) {
327 if (command_table[i].name == NULL)
328 (void) fprintf(fp, "\n");
329 else
330 (void) fprintf(fp, "%s",
331 get_usage(command_table[i].usage));
333 } else {
334 (void) fprintf(fp, gettext("usage:\n"));
335 (void) fprintf(fp, "%s", get_usage(current_command->usage));
338 if (current_command != NULL &&
339 ((strcmp(current_command->name, "set") == 0) ||
340 (strcmp(current_command->name, "get") == 0) ||
341 (strcmp(current_command->name, "list") == 0))) {
343 (void) fprintf(fp,
344 gettext("\nthe following properties are supported:\n"));
346 (void) fprintf(fp, "\n\t%-15s %s %s\n\n",
347 "PROPERTY", "EDIT", "VALUES");
349 /* Iterate over all properties */
350 (void) zprop_iter(print_prop_cb, fp, B_FALSE, B_TRUE,
351 ZFS_TYPE_POOL);
353 (void) fprintf(fp, "\t%-15s ", "feature@...");
354 (void) fprintf(fp, "YES disabled | enabled | active\n");
356 (void) fprintf(fp, gettext("\nThe feature@ properties must be "
357 "appended with a feature name.\nSee zpool-features(5).\n"));
361 * See comments at end of main().
363 if (getenv("ZFS_ABORT") != NULL) {
364 (void) printf("dumping core by request\n");
365 abort();
368 exit(requested ? 0 : 2);
371 void
372 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
373 boolean_t print_logs)
375 nvlist_t **child;
376 uint_t c, children;
377 char *vname;
379 if (name != NULL)
380 (void) printf("\t%*s%s\n", indent, "", name);
382 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
383 &child, &children) != 0)
384 return;
386 for (c = 0; c < children; c++) {
387 uint64_t is_log = B_FALSE;
389 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
390 &is_log);
391 if ((is_log && !print_logs) || (!is_log && print_logs))
392 continue;
394 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
395 print_vdev_tree(zhp, vname, child[c], indent + 2,
396 B_FALSE);
397 free(vname);
401 static boolean_t
402 prop_list_contains_feature(nvlist_t *proplist)
404 nvpair_t *nvp;
405 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
406 nvp = nvlist_next_nvpair(proplist, nvp)) {
407 if (zpool_prop_feature(nvpair_name(nvp)))
408 return (B_TRUE);
410 return (B_FALSE);
414 * Add a property pair (name, string-value) into a property nvlist.
416 static int
417 add_prop_list(const char *propname, char *propval, nvlist_t **props,
418 boolean_t poolprop)
420 zpool_prop_t prop = ZPROP_INVAL;
421 zfs_prop_t fprop;
422 nvlist_t *proplist;
423 const char *normnm;
424 char *strval;
426 if (*props == NULL &&
427 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
428 (void) fprintf(stderr,
429 gettext("internal error: out of memory\n"));
430 return (1);
433 proplist = *props;
435 if (poolprop) {
436 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
438 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL &&
439 !zpool_prop_feature(propname)) {
440 (void) fprintf(stderr, gettext("property '%s' is "
441 "not a valid pool property\n"), propname);
442 return (2);
446 * feature@ properties and version should not be specified
447 * at the same time.
449 if ((prop == ZPROP_INVAL && zpool_prop_feature(propname) &&
450 nvlist_exists(proplist, vname)) ||
451 (prop == ZPOOL_PROP_VERSION &&
452 prop_list_contains_feature(proplist))) {
453 (void) fprintf(stderr, gettext("'feature@' and "
454 "'version' properties cannot be specified "
455 "together\n"));
456 return (2);
460 if (zpool_prop_feature(propname))
461 normnm = propname;
462 else
463 normnm = zpool_prop_to_name(prop);
464 } else {
465 if ((fprop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
466 normnm = zfs_prop_to_name(fprop);
467 } else {
468 normnm = propname;
472 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
473 prop != ZPOOL_PROP_CACHEFILE) {
474 (void) fprintf(stderr, gettext("property '%s' "
475 "specified multiple times\n"), propname);
476 return (2);
479 if (nvlist_add_string(proplist, normnm, propval) != 0) {
480 (void) fprintf(stderr, gettext("internal "
481 "error: out of memory\n"));
482 return (1);
485 return (0);
489 * Set a default property pair (name, string-value) in a property nvlist
491 static int
492 add_prop_list_default(const char *propname, char *propval, nvlist_t **props,
493 boolean_t poolprop)
495 char *pval;
497 if (nvlist_lookup_string(*props, propname, &pval) == 0)
498 return (0);
500 return (add_prop_list(propname, propval, props, B_TRUE));
504 * zpool add [-fn] [-o property=value] <pool> <vdev> ...
506 * -f Force addition of devices, even if they appear in use
507 * -n Do not add the devices, but display the resulting layout if
508 * they were to be added.
509 * -o Set property=value.
511 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
512 * handled by get_vdev_spec(), which constructs the nvlist needed to pass to
513 * libzfs.
516 zpool_do_add(int argc, char **argv)
518 boolean_t force = B_FALSE;
519 boolean_t dryrun = B_FALSE;
520 int c;
521 nvlist_t *nvroot;
522 char *poolname;
523 int ret;
524 zpool_handle_t *zhp;
525 nvlist_t *config;
526 nvlist_t *props = NULL;
527 char *propval;
529 /* check options */
530 while ((c = getopt(argc, argv, "fno:")) != -1) {
531 switch (c) {
532 case 'f':
533 force = B_TRUE;
534 break;
535 case 'n':
536 dryrun = B_TRUE;
537 break;
538 case 'o':
539 if ((propval = strchr(optarg, '=')) == NULL) {
540 (void) fprintf(stderr, gettext("missing "
541 "'=' for -o option\n"));
542 usage(B_FALSE);
544 *propval = '\0';
545 propval++;
547 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
548 (add_prop_list(optarg, propval, &props, B_TRUE)))
549 usage(B_FALSE);
550 break;
551 case '?':
552 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
553 optopt);
554 usage(B_FALSE);
558 argc -= optind;
559 argv += optind;
561 /* get pool name and check number of arguments */
562 if (argc < 1) {
563 (void) fprintf(stderr, gettext("missing pool name argument\n"));
564 usage(B_FALSE);
566 if (argc < 2) {
567 (void) fprintf(stderr, gettext("missing vdev specification\n"));
568 usage(B_FALSE);
571 poolname = argv[0];
573 argc--;
574 argv++;
576 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
577 return (1);
579 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
580 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
581 poolname);
582 zpool_close(zhp);
583 return (1);
586 /* pass off to get_vdev_spec for processing */
587 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
588 argc, argv);
589 if (nvroot == NULL) {
590 zpool_close(zhp);
591 return (1);
594 if (dryrun) {
595 nvlist_t *poolnvroot;
596 nvlist_t **l2child;
597 uint_t l2children, c;
598 char *vname;
599 boolean_t hadcache = B_FALSE;
601 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
602 &poolnvroot) == 0);
604 (void) printf(gettext("would update '%s' to the following "
605 "configuration:\n"), zpool_get_name(zhp));
607 /* print original main pool and new tree */
608 print_vdev_tree(zhp, poolname, poolnvroot, 0, B_FALSE);
609 print_vdev_tree(zhp, NULL, nvroot, 0, B_FALSE);
611 /* Do the same for the logs */
612 if (num_logs(poolnvroot) > 0) {
613 print_vdev_tree(zhp, "logs", poolnvroot, 0, B_TRUE);
614 print_vdev_tree(zhp, NULL, nvroot, 0, B_TRUE);
615 } else if (num_logs(nvroot) > 0) {
616 print_vdev_tree(zhp, "logs", nvroot, 0, B_TRUE);
619 /* Do the same for the caches */
620 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
621 &l2child, &l2children) == 0 && l2children) {
622 hadcache = B_TRUE;
623 (void) printf(gettext("\tcache\n"));
624 for (c = 0; c < l2children; c++) {
625 vname = zpool_vdev_name(g_zfs, NULL,
626 l2child[c], B_FALSE);
627 (void) printf("\t %s\n", vname);
628 free(vname);
631 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
632 &l2child, &l2children) == 0 && l2children) {
633 if (!hadcache)
634 (void) printf(gettext("\tcache\n"));
635 for (c = 0; c < l2children; c++) {
636 vname = zpool_vdev_name(g_zfs, NULL,
637 l2child[c], B_FALSE);
638 (void) printf("\t %s\n", vname);
639 free(vname);
643 ret = 0;
644 } else {
645 ret = (zpool_add(zhp, nvroot) != 0);
648 nvlist_free(props);
649 nvlist_free(nvroot);
650 zpool_close(zhp);
652 return (ret);
656 * zpool remove <pool> <vdev> ...
658 * Removes the given vdev from the pool. Currently, this supports removing
659 * spares, cache, and log devices from the pool.
662 zpool_do_remove(int argc, char **argv)
664 char *poolname;
665 int i, ret = 0;
666 zpool_handle_t *zhp;
668 argc--;
669 argv++;
671 /* get pool name and check number of arguments */
672 if (argc < 1) {
673 (void) fprintf(stderr, gettext("missing pool name argument\n"));
674 usage(B_FALSE);
676 if (argc < 2) {
677 (void) fprintf(stderr, gettext("missing device\n"));
678 usage(B_FALSE);
681 poolname = argv[0];
683 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
684 return (1);
686 for (i = 1; i < argc; i++) {
687 if (zpool_vdev_remove(zhp, argv[i]) != 0)
688 ret = 1;
691 return (ret);
695 * zpool labelclear <vdev>
697 * Verifies that the vdev is not active and zeros out the label information
698 * on the device.
701 zpool_do_labelclear(int argc, char **argv)
703 char *vdev, *name;
704 int c, fd = -1, ret = 0;
705 pool_state_t state;
706 boolean_t inuse = B_FALSE;
707 boolean_t force = B_FALSE;
709 /* check options */
710 while ((c = getopt(argc, argv, "f")) != -1) {
711 switch (c) {
712 case 'f':
713 force = B_TRUE;
714 break;
715 default:
716 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
717 optopt);
718 usage(B_FALSE);
722 argc -= optind;
723 argv += optind;
725 /* get vdev name */
726 if (argc < 1) {
727 (void) fprintf(stderr, gettext("missing vdev device name\n"));
728 usage(B_FALSE);
731 vdev = argv[0];
732 if ((fd = open(vdev, O_RDWR)) < 0) {
733 (void) fprintf(stderr, gettext("Unable to open %s\n"), vdev);
734 return (B_FALSE);
737 name = NULL;
738 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0) {
739 if (force)
740 goto wipe_label;
742 (void) fprintf(stderr,
743 gettext("Unable to determine pool state for %s\n"
744 "Use -f to force the clearing any label data\n"), vdev);
746 return (1);
749 if (inuse) {
750 switch (state) {
751 default:
752 case POOL_STATE_ACTIVE:
753 case POOL_STATE_SPARE:
754 case POOL_STATE_L2CACHE:
755 (void) fprintf(stderr,
756 gettext("labelclear operation failed.\n"
757 "\tVdev %s is a member (%s), of pool \"%s\".\n"
758 "\tTo remove label information from this device, "
759 "export or destroy\n\tthe pool, or remove %s from "
760 "the configuration of this pool\n\tand retry the "
761 "labelclear operation.\n"),
762 vdev, zpool_pool_state_to_name(state), name, vdev);
763 ret = 1;
764 goto errout;
766 case POOL_STATE_EXPORTED:
767 if (force)
768 break;
770 (void) fprintf(stderr,
771 gettext("labelclear operation failed.\n\tVdev "
772 "%s is a member of the exported pool \"%s\".\n"
773 "\tUse \"zpool labelclear -f %s\" to force the "
774 "removal of label\n\tinformation.\n"),
775 vdev, name, vdev);
776 ret = 1;
777 goto errout;
779 case POOL_STATE_POTENTIALLY_ACTIVE:
780 if (force)
781 break;
783 (void) fprintf(stderr,
784 gettext("labelclear operation failed.\n"
785 "\tVdev %s is a member of the pool \"%s\".\n"
786 "\tThis pool is unknown to this system, but may "
787 "be active on\n\tanother system. Use "
788 "\'zpool labelclear -f %s\' to force the\n"
789 "\tremoval of label information.\n"),
790 vdev, name, vdev);
791 ret = 1;
792 goto errout;
794 case POOL_STATE_DESTROYED:
795 /* inuse should never be set for a destroyed pool... */
796 break;
800 wipe_label:
801 if (zpool_clear_label(fd) != 0) {
802 (void) fprintf(stderr,
803 gettext("Label clear failed on vdev %s\n"), vdev);
804 ret = 1;
807 errout:
808 close(fd);
809 if (name != NULL)
810 free(name);
812 return (ret);
816 * zpool create [-fnd] [-o property=value] ...
817 * [-O file-system-property=value] ...
818 * [-R root] [-m mountpoint] <pool> <dev> ...
820 * -f Force creation, even if devices appear in use
821 * -n Do not create the pool, but display the resulting layout if it
822 * were to be created.
823 * -R Create a pool under an alternate root
824 * -m Set default mountpoint for the root dataset. By default it's
825 * '/<pool>'
826 * -o Set property=value.
827 * -d Don't automatically enable all supported pool features
828 * (individual features can be enabled with -o).
829 * -O Set fsproperty=value in the pool's root file system
831 * Creates the named pool according to the given vdev specification. The
832 * bulk of the vdev processing is done in get_vdev_spec() in zpool_vdev.c. Once
833 * we get the nvlist back from get_vdev_spec(), we either print out the contents
834 * (if '-n' was specified), or pass it to libzfs to do the creation.
837 zpool_do_create(int argc, char **argv)
839 boolean_t force = B_FALSE;
840 boolean_t dryrun = B_FALSE;
841 boolean_t enable_all_pool_feat = B_TRUE;
842 int c;
843 nvlist_t *nvroot = NULL;
844 char *poolname;
845 char *tname = NULL;
846 int ret = 1;
847 char *altroot = NULL;
848 char *mountpoint = NULL;
849 nvlist_t *fsprops = NULL;
850 nvlist_t *props = NULL;
851 char *propval;
853 /* check options */
854 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
855 switch (c) {
856 case 'f':
857 force = B_TRUE;
858 break;
859 case 'n':
860 dryrun = B_TRUE;
861 break;
862 case 'd':
863 enable_all_pool_feat = B_FALSE;
864 break;
865 case 'R':
866 altroot = optarg;
867 if (add_prop_list(zpool_prop_to_name(
868 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
869 goto errout;
870 if (add_prop_list_default(zpool_prop_to_name(
871 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
872 goto errout;
873 break;
874 case 'm':
875 /* Equivalent to -O mountpoint=optarg */
876 mountpoint = optarg;
877 break;
878 case 'o':
879 if ((propval = strchr(optarg, '=')) == NULL) {
880 (void) fprintf(stderr, gettext("missing "
881 "'=' for -o option\n"));
882 goto errout;
884 *propval = '\0';
885 propval++;
887 if (add_prop_list(optarg, propval, &props, B_TRUE))
888 goto errout;
891 * If the user is creating a pool that doesn't support
892 * feature flags, don't enable any features.
894 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
895 char *end;
896 u_longlong_t ver;
898 ver = strtoull(propval, &end, 10);
899 if (*end == '\0' &&
900 ver < SPA_VERSION_FEATURES) {
901 enable_all_pool_feat = B_FALSE;
904 break;
905 case 'O':
906 if ((propval = strchr(optarg, '=')) == NULL) {
907 (void) fprintf(stderr, gettext("missing "
908 "'=' for -O option\n"));
909 goto errout;
911 *propval = '\0';
912 propval++;
915 * Mountpoints are checked and then added later.
916 * Uniquely among properties, they can be specified
917 * more than once, to avoid conflict with -m.
919 if (0 == strcmp(optarg,
920 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
921 mountpoint = propval;
922 } else if (add_prop_list(optarg, propval, &fsprops,
923 B_FALSE)) {
924 goto errout;
926 break;
927 case 't':
929 * Sanity check temporary pool name.
931 if (strchr(optarg, '/') != NULL) {
932 (void) fprintf(stderr, gettext("cannot create "
933 "'%s': invalid character '/' in temporary "
934 "name\n"), optarg);
935 (void) fprintf(stderr, gettext("use 'zfs "
936 "create' to create a dataset\n"));
937 goto errout;
940 if (add_prop_list(zpool_prop_to_name(
941 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
942 goto errout;
943 if (add_prop_list_default(zpool_prop_to_name(
944 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
945 goto errout;
946 tname = optarg;
947 break;
948 case ':':
949 (void) fprintf(stderr, gettext("missing argument for "
950 "'%c' option\n"), optopt);
951 goto badusage;
952 case '?':
953 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
954 optopt);
955 goto badusage;
959 argc -= optind;
960 argv += optind;
962 /* get pool name and check number of arguments */
963 if (argc < 1) {
964 (void) fprintf(stderr, gettext("missing pool name argument\n"));
965 goto badusage;
967 if (argc < 2) {
968 (void) fprintf(stderr, gettext("missing vdev specification\n"));
969 goto badusage;
972 poolname = argv[0];
975 * As a special case, check for use of '/' in the name, and direct the
976 * user to use 'zfs create' instead.
978 if (strchr(poolname, '/') != NULL) {
979 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
980 "character '/' in pool name\n"), poolname);
981 (void) fprintf(stderr, gettext("use 'zfs create' to "
982 "create a dataset\n"));
983 goto errout;
986 /* pass off to get_vdev_spec for bulk processing */
987 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
988 argc - 1, argv + 1);
989 if (nvroot == NULL)
990 goto errout;
992 /* make_root_vdev() allows 0 toplevel children if there are spares */
993 if (!zfs_allocatable_devs(nvroot)) {
994 (void) fprintf(stderr, gettext("invalid vdev "
995 "specification: at least one toplevel vdev must be "
996 "specified\n"));
997 goto errout;
1000 if (altroot != NULL && altroot[0] != '/') {
1001 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1002 "must be an absolute path\n"), altroot);
1003 goto errout;
1007 * Check the validity of the mountpoint and direct the user to use the
1008 * '-m' mountpoint option if it looks like its in use.
1010 if (mountpoint == NULL ||
1011 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1012 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1013 char buf[MAXPATHLEN];
1014 DIR *dirp;
1016 if (mountpoint && mountpoint[0] != '/') {
1017 (void) fprintf(stderr, gettext("invalid mountpoint "
1018 "'%s': must be an absolute path, 'legacy', or "
1019 "'none'\n"), mountpoint);
1020 goto errout;
1023 if (mountpoint == NULL) {
1024 if (altroot != NULL)
1025 (void) snprintf(buf, sizeof (buf), "%s/%s",
1026 altroot, poolname);
1027 else
1028 (void) snprintf(buf, sizeof (buf), "/%s",
1029 poolname);
1030 } else {
1031 if (altroot != NULL)
1032 (void) snprintf(buf, sizeof (buf), "%s%s",
1033 altroot, mountpoint);
1034 else
1035 (void) snprintf(buf, sizeof (buf), "%s",
1036 mountpoint);
1039 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1040 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1041 "%s\n"), buf, strerror(errno));
1042 (void) fprintf(stderr, gettext("use '-m' "
1043 "option to provide a different default\n"));
1044 goto errout;
1045 } else if (dirp) {
1046 int count = 0;
1048 while (count < 3 && readdir(dirp) != NULL)
1049 count++;
1050 (void) closedir(dirp);
1052 if (count > 2) {
1053 (void) fprintf(stderr, gettext("mountpoint "
1054 "'%s' exists and is not empty\n"), buf);
1055 (void) fprintf(stderr, gettext("use '-m' "
1056 "option to provide a "
1057 "different default\n"));
1058 goto errout;
1064 * Now that the mountpoint's validity has been checked, ensure that
1065 * the property is set appropriately prior to creating the pool.
1067 if (mountpoint != NULL) {
1068 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1069 mountpoint, &fsprops, B_FALSE);
1070 if (ret != 0)
1071 goto errout;
1074 ret = 1;
1075 if (dryrun) {
1077 * For a dry run invocation, print out a basic message and run
1078 * through all the vdevs in the list and print out in an
1079 * appropriate hierarchy.
1081 (void) printf(gettext("would create '%s' with the "
1082 "following layout:\n\n"), poolname);
1084 print_vdev_tree(NULL, poolname, nvroot, 0, B_FALSE);
1085 if (num_logs(nvroot) > 0)
1086 print_vdev_tree(NULL, "logs", nvroot, 0, B_TRUE);
1088 ret = 0;
1089 } else {
1091 * Hand off to libzfs.
1093 if (enable_all_pool_feat) {
1094 spa_feature_t i;
1095 for (i = 0; i < SPA_FEATURES; i++) {
1096 char propname[MAXPATHLEN];
1097 zfeature_info_t *feat = &spa_feature_table[i];
1099 (void) snprintf(propname, sizeof (propname),
1100 "feature@%s", feat->fi_uname);
1103 * Skip feature if user specified it manually
1104 * on the command line.
1106 if (nvlist_exists(props, propname))
1107 continue;
1109 ret = add_prop_list(propname,
1110 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1111 if (ret != 0)
1112 goto errout;
1116 ret = 1;
1117 if (zpool_create(g_zfs, poolname,
1118 nvroot, props, fsprops) == 0) {
1119 zfs_handle_t *pool = zfs_open(g_zfs,
1120 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1121 if (pool != NULL) {
1122 if (zfs_mount(pool, NULL, 0) == 0)
1123 ret = zfs_shareall(pool);
1124 zfs_close(pool);
1126 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1127 (void) fprintf(stderr, gettext("pool name may have "
1128 "been omitted\n"));
1132 errout:
1133 nvlist_free(nvroot);
1134 nvlist_free(fsprops);
1135 nvlist_free(props);
1136 return (ret);
1137 badusage:
1138 nvlist_free(fsprops);
1139 nvlist_free(props);
1140 usage(B_FALSE);
1141 return (2);
1145 * zpool destroy <pool>
1147 * -f Forcefully unmount any datasets
1149 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1152 zpool_do_destroy(int argc, char **argv)
1154 boolean_t force = B_FALSE;
1155 int c;
1156 char *pool;
1157 zpool_handle_t *zhp;
1158 int ret;
1160 /* check options */
1161 while ((c = getopt(argc, argv, "f")) != -1) {
1162 switch (c) {
1163 case 'f':
1164 force = B_TRUE;
1165 break;
1166 case '?':
1167 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1168 optopt);
1169 usage(B_FALSE);
1173 argc -= optind;
1174 argv += optind;
1176 /* check arguments */
1177 if (argc < 1) {
1178 (void) fprintf(stderr, gettext("missing pool argument\n"));
1179 usage(B_FALSE);
1181 if (argc > 1) {
1182 (void) fprintf(stderr, gettext("too many arguments\n"));
1183 usage(B_FALSE);
1186 pool = argv[0];
1188 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1190 * As a special case, check for use of '/' in the name, and
1191 * direct the user to use 'zfs destroy' instead.
1193 if (strchr(pool, '/') != NULL)
1194 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1195 "destroy a dataset\n"));
1196 return (1);
1199 if (zpool_disable_datasets(zhp, force) != 0) {
1200 (void) fprintf(stderr, gettext("could not destroy '%s': "
1201 "could not unmount datasets\n"), zpool_get_name(zhp));
1202 return (1);
1205 /* The history must be logged as part of the export */
1206 log_history = B_FALSE;
1208 ret = (zpool_destroy(zhp, history_str) != 0);
1210 zpool_close(zhp);
1212 return (ret);
1215 typedef struct export_cbdata {
1216 boolean_t force;
1217 boolean_t hardforce;
1218 } export_cbdata_t;
1221 * Export one pool
1224 zpool_export_one(zpool_handle_t *zhp, void *data)
1226 export_cbdata_t *cb = data;
1228 if (zpool_disable_datasets(zhp, cb->force) != 0)
1229 return (1);
1231 /* The history must be logged as part of the export */
1232 log_history = B_FALSE;
1234 if (cb->hardforce) {
1235 if (zpool_export_force(zhp, history_str) != 0)
1236 return (1);
1237 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1238 return (1);
1241 return (0);
1245 * zpool export [-f] <pool> ...
1247 * -a Export all pools
1248 * -f Forcefully unmount datasets
1250 * Export the given pools. By default, the command will attempt to cleanly
1251 * unmount any active datasets within the pool. If the '-f' flag is specified,
1252 * then the datasets will be forcefully unmounted.
1255 zpool_do_export(int argc, char **argv)
1257 export_cbdata_t cb;
1258 boolean_t do_all = B_FALSE;
1259 boolean_t force = B_FALSE;
1260 boolean_t hardforce = B_FALSE;
1261 int c, ret;
1263 /* check options */
1264 while ((c = getopt(argc, argv, "afF")) != -1) {
1265 switch (c) {
1266 case 'a':
1267 do_all = B_TRUE;
1268 break;
1269 case 'f':
1270 force = B_TRUE;
1271 break;
1272 case 'F':
1273 hardforce = B_TRUE;
1274 break;
1275 case '?':
1276 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1277 optopt);
1278 usage(B_FALSE);
1282 cb.force = force;
1283 cb.hardforce = hardforce;
1284 argc -= optind;
1285 argv += optind;
1287 if (do_all) {
1288 if (argc != 0) {
1289 (void) fprintf(stderr, gettext("too many arguments\n"));
1290 usage(B_FALSE);
1293 return (for_each_pool(argc, argv, B_TRUE, NULL,
1294 zpool_export_one, &cb));
1297 /* check arguments */
1298 if (argc < 1) {
1299 (void) fprintf(stderr, gettext("missing pool argument\n"));
1300 usage(B_FALSE);
1303 ret = for_each_pool(argc, argv, B_TRUE, NULL, zpool_export_one, &cb);
1305 return (ret);
1309 * Given a vdev configuration, determine the maximum width needed for the device
1310 * name column.
1312 static int
1313 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max)
1315 char *name = zpool_vdev_name(g_zfs, zhp, nv, B_TRUE);
1316 nvlist_t **child;
1317 uint_t c, children;
1318 int ret;
1320 if (strlen(name) + depth > max)
1321 max = strlen(name) + depth;
1323 free(name);
1325 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1326 &child, &children) == 0) {
1327 for (c = 0; c < children; c++)
1328 if ((ret = max_width(zhp, child[c], depth + 2,
1329 max)) > max)
1330 max = ret;
1333 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1334 &child, &children) == 0) {
1335 for (c = 0; c < children; c++)
1336 if ((ret = max_width(zhp, child[c], depth + 2,
1337 max)) > max)
1338 max = ret;
1341 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1342 &child, &children) == 0) {
1343 for (c = 0; c < children; c++)
1344 if ((ret = max_width(zhp, child[c], depth + 2,
1345 max)) > max)
1346 max = ret;
1350 return (max);
1353 typedef struct spare_cbdata {
1354 uint64_t cb_guid;
1355 zpool_handle_t *cb_zhp;
1356 } spare_cbdata_t;
1358 static boolean_t
1359 find_vdev(nvlist_t *nv, uint64_t search)
1361 uint64_t guid;
1362 nvlist_t **child;
1363 uint_t c, children;
1365 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1366 search == guid)
1367 return (B_TRUE);
1369 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1370 &child, &children) == 0) {
1371 for (c = 0; c < children; c++)
1372 if (find_vdev(child[c], search))
1373 return (B_TRUE);
1376 return (B_FALSE);
1379 static int
1380 find_spare(zpool_handle_t *zhp, void *data)
1382 spare_cbdata_t *cbp = data;
1383 nvlist_t *config, *nvroot;
1385 config = zpool_get_config(zhp, NULL);
1386 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1387 &nvroot) == 0);
1389 if (find_vdev(nvroot, cbp->cb_guid)) {
1390 cbp->cb_zhp = zhp;
1391 return (1);
1394 zpool_close(zhp);
1395 return (0);
1399 * Print out configuration state as requested by status_callback.
1401 void
1402 print_status_config(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
1403 int namewidth, int depth, boolean_t isspare)
1405 nvlist_t **child;
1406 uint_t c, children;
1407 pool_scan_stat_t *ps = NULL;
1408 vdev_stat_t *vs;
1409 char rbuf[6], wbuf[6], cbuf[6];
1410 char *vname;
1411 uint64_t notpresent;
1412 spare_cbdata_t cb;
1413 char *state;
1415 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1416 &child, &children) != 0)
1417 children = 0;
1419 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1420 (uint64_t **)&vs, &c) == 0);
1422 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1423 if (isspare) {
1425 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
1426 * online drives.
1428 if (vs->vs_aux == VDEV_AUX_SPARED)
1429 state = "INUSE";
1430 else if (vs->vs_state == VDEV_STATE_HEALTHY)
1431 state = "AVAIL";
1434 (void) printf("\t%*s%-*s %-8s", depth, "", namewidth - depth,
1435 name, state);
1437 if (!isspare) {
1438 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
1439 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
1440 zfs_nicenum(vs->vs_checksum_errors, cbuf, sizeof (cbuf));
1441 (void) printf(" %5s %5s %5s", rbuf, wbuf, cbuf);
1444 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1445 &notpresent) == 0) {
1446 char *path;
1447 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1448 (void) printf(" was %s", path);
1449 } else if (vs->vs_aux != 0) {
1450 (void) printf(" ");
1452 switch (vs->vs_aux) {
1453 case VDEV_AUX_OPEN_FAILED:
1454 (void) printf(gettext("cannot open"));
1455 break;
1457 case VDEV_AUX_BAD_GUID_SUM:
1458 (void) printf(gettext("missing device"));
1459 break;
1461 case VDEV_AUX_NO_REPLICAS:
1462 (void) printf(gettext("insufficient replicas"));
1463 break;
1465 case VDEV_AUX_VERSION_NEWER:
1466 (void) printf(gettext("newer version"));
1467 break;
1469 case VDEV_AUX_UNSUP_FEAT:
1470 (void) printf(gettext("unsupported feature(s)"));
1471 break;
1473 case VDEV_AUX_SPARED:
1474 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1475 &cb.cb_guid) == 0);
1476 if (zpool_iter(g_zfs, find_spare, &cb) == 1) {
1477 if (strcmp(zpool_get_name(cb.cb_zhp),
1478 zpool_get_name(zhp)) == 0)
1479 (void) printf(gettext("currently in "
1480 "use"));
1481 else
1482 (void) printf(gettext("in use by "
1483 "pool '%s'"),
1484 zpool_get_name(cb.cb_zhp));
1485 zpool_close(cb.cb_zhp);
1486 } else {
1487 (void) printf(gettext("currently in use"));
1489 break;
1491 case VDEV_AUX_ERR_EXCEEDED:
1492 (void) printf(gettext("too many errors"));
1493 break;
1495 case VDEV_AUX_IO_FAILURE:
1496 (void) printf(gettext("experienced I/O failures"));
1497 break;
1499 case VDEV_AUX_BAD_LOG:
1500 (void) printf(gettext("bad intent log"));
1501 break;
1503 case VDEV_AUX_EXTERNAL:
1504 (void) printf(gettext("external device fault"));
1505 break;
1507 case VDEV_AUX_SPLIT_POOL:
1508 (void) printf(gettext("split into new pool"));
1509 break;
1511 default:
1512 (void) printf(gettext("corrupted data"));
1513 break;
1517 (void) nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_SCAN_STATS,
1518 (uint64_t **)&ps, &c);
1520 if (ps && ps->pss_state == DSS_SCANNING &&
1521 vs->vs_scan_processed != 0 && children == 0) {
1522 (void) printf(gettext(" (%s)"),
1523 (ps->pss_func == POOL_SCAN_RESILVER) ?
1524 "resilvering" : "repairing");
1527 (void) printf("\n");
1529 for (c = 0; c < children; c++) {
1530 uint64_t islog = B_FALSE, ishole = B_FALSE;
1532 /* Don't print logs or holes here */
1533 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1534 &islog);
1535 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
1536 &ishole);
1537 if (islog || ishole)
1538 continue;
1539 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1540 print_status_config(zhp, vname, child[c],
1541 namewidth, depth + 2, isspare);
1542 free(vname);
1548 * Print the configuration of an exported pool. Iterate over all vdevs in the
1549 * pool, printing out the name and status for each one.
1551 void
1552 print_import_config(const char *name, nvlist_t *nv, int namewidth, int depth)
1554 nvlist_t **child;
1555 uint_t c, children;
1556 vdev_stat_t *vs;
1557 char *type, *vname;
1559 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1560 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
1561 strcmp(type, VDEV_TYPE_HOLE) == 0)
1562 return;
1564 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
1565 (uint64_t **)&vs, &c) == 0);
1567 (void) printf("\t%*s%-*s", depth, "", namewidth - depth, name);
1568 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
1570 if (vs->vs_aux != 0) {
1571 (void) printf(" ");
1573 switch (vs->vs_aux) {
1574 case VDEV_AUX_OPEN_FAILED:
1575 (void) printf(gettext("cannot open"));
1576 break;
1578 case VDEV_AUX_BAD_GUID_SUM:
1579 (void) printf(gettext("missing device"));
1580 break;
1582 case VDEV_AUX_NO_REPLICAS:
1583 (void) printf(gettext("insufficient replicas"));
1584 break;
1586 case VDEV_AUX_VERSION_NEWER:
1587 (void) printf(gettext("newer version"));
1588 break;
1590 case VDEV_AUX_UNSUP_FEAT:
1591 (void) printf(gettext("unsupported feature(s)"));
1592 break;
1594 case VDEV_AUX_ERR_EXCEEDED:
1595 (void) printf(gettext("too many errors"));
1596 break;
1598 default:
1599 (void) printf(gettext("corrupted data"));
1600 break;
1603 (void) printf("\n");
1605 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1606 &child, &children) != 0)
1607 return;
1609 for (c = 0; c < children; c++) {
1610 uint64_t is_log = B_FALSE;
1612 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1613 &is_log);
1614 if (is_log)
1615 continue;
1617 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_TRUE);
1618 print_import_config(vname, child[c], namewidth, depth + 2);
1619 free(vname);
1622 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1623 &child, &children) == 0) {
1624 (void) printf(gettext("\tcache\n"));
1625 for (c = 0; c < children; c++) {
1626 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1627 (void) printf("\t %s\n", vname);
1628 free(vname);
1632 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1633 &child, &children) == 0) {
1634 (void) printf(gettext("\tspares\n"));
1635 for (c = 0; c < children; c++) {
1636 vname = zpool_vdev_name(g_zfs, NULL, child[c], B_FALSE);
1637 (void) printf("\t %s\n", vname);
1638 free(vname);
1644 * Print log vdevs.
1645 * Logs are recorded as top level vdevs in the main pool child array
1646 * but with "is_log" set to 1. We use either print_status_config() or
1647 * print_import_config() to print the top level logs then any log
1648 * children (eg mirrored slogs) are printed recursively - which
1649 * works because only the top level vdev is marked "is_log"
1651 static void
1652 print_logs(zpool_handle_t *zhp, nvlist_t *nv, int namewidth, boolean_t verbose)
1654 uint_t c, children;
1655 nvlist_t **child;
1657 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
1658 &children) != 0)
1659 return;
1661 (void) printf(gettext("\tlogs\n"));
1663 for (c = 0; c < children; c++) {
1664 uint64_t is_log = B_FALSE;
1665 char *name;
1667 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1668 &is_log);
1669 if (!is_log)
1670 continue;
1671 name = zpool_vdev_name(g_zfs, zhp, child[c], B_TRUE);
1672 if (verbose)
1673 print_status_config(zhp, name, child[c], namewidth,
1674 2, B_FALSE);
1675 else
1676 print_import_config(name, child[c], namewidth, 2);
1677 free(name);
1682 * Display the status for the given pool.
1684 static void
1685 show_import(nvlist_t *config)
1687 uint64_t pool_state;
1688 vdev_stat_t *vs;
1689 char *name;
1690 uint64_t guid;
1691 char *msgid;
1692 nvlist_t *nvroot;
1693 zpool_status_t reason;
1694 zpool_errata_t errata;
1695 const char *health;
1696 uint_t vsc;
1697 int namewidth;
1698 char *comment;
1700 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1701 &name) == 0);
1702 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1703 &guid) == 0);
1704 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1705 &pool_state) == 0);
1706 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1707 &nvroot) == 0);
1709 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1710 (uint64_t **)&vs, &vsc) == 0);
1711 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1713 reason = zpool_import_status(config, &msgid, &errata);
1715 (void) printf(gettext(" pool: %s\n"), name);
1716 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1717 (void) printf(gettext(" state: %s"), health);
1718 if (pool_state == POOL_STATE_DESTROYED)
1719 (void) printf(gettext(" (DESTROYED)"));
1720 (void) printf("\n");
1722 switch (reason) {
1723 case ZPOOL_STATUS_MISSING_DEV_R:
1724 case ZPOOL_STATUS_MISSING_DEV_NR:
1725 case ZPOOL_STATUS_BAD_GUID_SUM:
1726 (void) printf(gettext(" status: One or more devices are "
1727 "missing from the system.\n"));
1728 break;
1730 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1731 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1732 (void) printf(gettext(" status: One or more devices contains "
1733 "corrupted data.\n"));
1734 break;
1736 case ZPOOL_STATUS_CORRUPT_DATA:
1737 (void) printf(
1738 gettext(" status: The pool data is corrupted.\n"));
1739 break;
1741 case ZPOOL_STATUS_OFFLINE_DEV:
1742 (void) printf(gettext(" status: One or more devices "
1743 "are offlined.\n"));
1744 break;
1746 case ZPOOL_STATUS_CORRUPT_POOL:
1747 (void) printf(gettext(" status: The pool metadata is "
1748 "corrupted.\n"));
1749 break;
1751 case ZPOOL_STATUS_VERSION_OLDER:
1752 (void) printf(gettext(" status: The pool is formatted using a "
1753 "legacy on-disk version.\n"));
1754 break;
1756 case ZPOOL_STATUS_VERSION_NEWER:
1757 (void) printf(gettext(" status: The pool is formatted using an "
1758 "incompatible version.\n"));
1759 break;
1761 case ZPOOL_STATUS_FEAT_DISABLED:
1762 (void) printf(gettext(" status: Some supported features are "
1763 "not enabled on the pool.\n"));
1764 break;
1766 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1767 (void) printf(gettext("status: The pool uses the following "
1768 "feature(s) not supported on this sytem:\n"));
1769 zpool_print_unsup_feat(config);
1770 break;
1772 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1773 (void) printf(gettext("status: The pool can only be accessed "
1774 "in read-only mode on this system. It\n\tcannot be "
1775 "accessed in read-write mode because it uses the "
1776 "following\n\tfeature(s) not supported on this system:\n"));
1777 zpool_print_unsup_feat(config);
1778 break;
1780 case ZPOOL_STATUS_HOSTID_MISMATCH:
1781 (void) printf(gettext(" status: The pool was last accessed by "
1782 "another system.\n"));
1783 break;
1785 case ZPOOL_STATUS_FAULTED_DEV_R:
1786 case ZPOOL_STATUS_FAULTED_DEV_NR:
1787 (void) printf(gettext(" status: One or more devices are "
1788 "faulted.\n"));
1789 break;
1791 case ZPOOL_STATUS_BAD_LOG:
1792 (void) printf(gettext(" status: An intent log record cannot be "
1793 "read.\n"));
1794 break;
1796 case ZPOOL_STATUS_RESILVERING:
1797 (void) printf(gettext(" status: One or more devices were being "
1798 "resilvered.\n"));
1799 break;
1801 case ZPOOL_STATUS_ERRATA:
1802 (void) printf(gettext(" status: Errata #%d detected.\n"),
1803 errata);
1804 break;
1806 default:
1808 * No other status can be seen when importing pools.
1810 assert(reason == ZPOOL_STATUS_OK);
1814 * Print out an action according to the overall state of the pool.
1816 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1817 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1818 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1819 (void) printf(gettext(" action: The pool can be "
1820 "imported using its name or numeric identifier, "
1821 "though\n\tsome features will not be available "
1822 "without an explicit 'zpool upgrade'.\n"));
1823 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1824 (void) printf(gettext(" action: The pool can be "
1825 "imported using its name or numeric "
1826 "identifier and\n\tthe '-f' flag.\n"));
1827 } else if (reason == ZPOOL_STATUS_ERRATA) {
1828 switch (errata) {
1829 case ZPOOL_ERRATA_NONE:
1830 break;
1832 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
1833 (void) printf(gettext(" action: The pool can "
1834 "be imported using its name or numeric "
1835 "identifier,\n\thowever there is a compat"
1836 "ibility issue which should be corrected"
1837 "\n\tby running 'zpool scrub'\n"));
1838 break;
1840 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
1841 (void) printf(gettext(" action: The pool can"
1842 "not be imported with this version of ZFS "
1843 "due to\n\tan active asynchronous destroy. "
1844 "Revert to an earlier version\n\tand "
1845 "allow the destroy to complete before "
1846 "updating.\n"));
1847 break;
1849 default:
1851 * All errata must contain an action message.
1853 assert(0);
1855 } else {
1856 (void) printf(gettext(" action: The pool can be "
1857 "imported using its name or numeric "
1858 "identifier.\n"));
1860 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1861 (void) printf(gettext(" action: The pool can be imported "
1862 "despite missing or damaged devices. The\n\tfault "
1863 "tolerance of the pool may be compromised if imported.\n"));
1864 } else {
1865 switch (reason) {
1866 case ZPOOL_STATUS_VERSION_NEWER:
1867 (void) printf(gettext(" action: The pool cannot be "
1868 "imported. Access the pool on a system running "
1869 "newer\n\tsoftware, or recreate the pool from "
1870 "backup.\n"));
1871 break;
1872 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1873 (void) printf(gettext("action: The pool cannot be "
1874 "imported. Access the pool on a system that "
1875 "supports\n\tthe required feature(s), or recreate "
1876 "the pool from backup.\n"));
1877 break;
1878 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1879 (void) printf(gettext("action: The pool cannot be "
1880 "imported in read-write mode. Import the pool "
1881 "with\n"
1882 "\t\"-o readonly=on\", access the pool on a system "
1883 "that supports the\n\trequired feature(s), or "
1884 "recreate the pool from backup.\n"));
1885 break;
1886 case ZPOOL_STATUS_MISSING_DEV_R:
1887 case ZPOOL_STATUS_MISSING_DEV_NR:
1888 case ZPOOL_STATUS_BAD_GUID_SUM:
1889 (void) printf(gettext(" action: The pool cannot be "
1890 "imported. Attach the missing\n\tdevices and try "
1891 "again.\n"));
1892 break;
1893 default:
1894 (void) printf(gettext(" action: The pool cannot be "
1895 "imported due to damaged devices or data.\n"));
1899 /* Print the comment attached to the pool. */
1900 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1901 (void) printf(gettext("comment: %s\n"), comment);
1904 * If the state is "closed" or "can't open", and the aux state
1905 * is "corrupt data":
1907 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1908 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1909 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1910 if (pool_state == POOL_STATE_DESTROYED)
1911 (void) printf(gettext("\tThe pool was destroyed, "
1912 "but can be imported using the '-Df' flags.\n"));
1913 else if (pool_state != POOL_STATE_EXPORTED)
1914 (void) printf(gettext("\tThe pool may be active on "
1915 "another system, but can be imported using\n\t"
1916 "the '-f' flag.\n"));
1919 if (msgid != NULL)
1920 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1921 msgid);
1923 (void) printf(gettext(" config:\n\n"));
1925 namewidth = max_width(NULL, nvroot, 0, 0);
1926 if (namewidth < 10)
1927 namewidth = 10;
1929 print_import_config(name, nvroot, namewidth, 0);
1930 if (num_logs(nvroot) > 0)
1931 print_logs(NULL, nvroot, namewidth, B_FALSE);
1933 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1934 (void) printf(gettext("\n\tAdditional devices are known to "
1935 "be part of this pool, though their\n\texact "
1936 "configuration cannot be determined.\n"));
1941 * Perform the import for the given configuration. This passes the heavy
1942 * lifting off to zpool_import_props(), and then mounts the datasets contained
1943 * within the pool.
1945 static int
1946 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1947 nvlist_t *props, int flags)
1949 zpool_handle_t *zhp;
1950 char *name;
1951 uint64_t state;
1952 uint64_t version;
1954 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1955 &name) == 0);
1957 verify(nvlist_lookup_uint64(config,
1958 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1959 verify(nvlist_lookup_uint64(config,
1960 ZPOOL_CONFIG_VERSION, &version) == 0);
1961 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1962 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1963 "is formatted using an unsupported ZFS version\n"), name);
1964 return (1);
1965 } else if (state != POOL_STATE_EXPORTED &&
1966 !(flags & ZFS_IMPORT_ANY_HOST)) {
1967 uint64_t hostid = 0;
1968 unsigned long system_hostid = get_system_hostid();
1970 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1971 &hostid);
1973 if (hostid != 0 && (unsigned long)hostid != system_hostid) {
1974 char *hostname;
1975 uint64_t timestamp;
1976 time_t t;
1978 verify(nvlist_lookup_string(config,
1979 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1980 verify(nvlist_lookup_uint64(config,
1981 ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1982 t = timestamp;
1983 (void) fprintf(stderr, gettext("cannot import "
1984 "'%s': pool may be in use from other "
1985 "system, it was last accessed by %s "
1986 "(hostid: 0x%lx) on %s"), name, hostname,
1987 (unsigned long)hostid,
1988 asctime(localtime(&t)));
1989 (void) fprintf(stderr, gettext("use '-f' to "
1990 "import anyway\n"));
1991 return (1);
1995 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1996 return (1);
1998 if (newname != NULL)
1999 name = (char *)newname;
2001 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
2002 return (1);
2004 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
2005 !(flags & ZFS_IMPORT_ONLY) &&
2006 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
2007 zpool_close(zhp);
2008 return (1);
2011 zpool_close(zhp);
2012 return (0);
2016 * zpool import [-d dir] [-D]
2017 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2018 * [-d dir | -c cachefile] [-f] -a
2019 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
2020 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
2022 * -c Read pool information from a cachefile instead of searching
2023 * devices.
2025 * -d Scan in a specific directory, other than /dev/. More than
2026 * one directory can be specified using multiple '-d' options.
2028 * -D Scan for previously destroyed pools or import all or only
2029 * specified destroyed pools.
2031 * -R Temporarily import the pool, with all mountpoints relative to
2032 * the given root. The pool will remain exported when the machine
2033 * is rebooted.
2035 * -V Import even in the presence of faulted vdevs. This is an
2036 * intentionally undocumented option for testing purposes, and
2037 * treats the pool configuration as complete, leaving any bad
2038 * vdevs in the FAULTED state. In other words, it does verbatim
2039 * import.
2041 * -f Force import, even if it appears that the pool is active.
2043 * -F Attempt rewind if necessary.
2045 * -n See if rewind would work, but don't actually rewind.
2047 * -N Import the pool but don't mount datasets.
2049 * -T Specify a starting txg to use for import. This option is
2050 * intentionally undocumented option for testing purposes.
2052 * -a Import all pools found.
2054 * -o Set property=value and/or temporary mount options (without '=').
2056 * The import command scans for pools to import, and import pools based on pool
2057 * name and GUID. The pool can also be renamed as part of the import process.
2060 zpool_do_import(int argc, char **argv)
2062 char **searchdirs = NULL;
2063 char *env, *envdup = NULL;
2064 int nsearch = 0;
2065 int c;
2066 int err = 0;
2067 nvlist_t *pools = NULL;
2068 boolean_t do_all = B_FALSE;
2069 boolean_t do_destroyed = B_FALSE;
2070 char *mntopts = NULL;
2071 nvpair_t *elem;
2072 nvlist_t *config;
2073 uint64_t searchguid = 0;
2074 char *searchname = NULL;
2075 char *propval;
2076 nvlist_t *found_config;
2077 nvlist_t *policy = NULL;
2078 nvlist_t *props = NULL;
2079 boolean_t first;
2080 int flags = ZFS_IMPORT_NORMAL;
2081 uint32_t rewind_policy = ZPOOL_NO_REWIND;
2082 boolean_t dryrun = B_FALSE;
2083 boolean_t do_rewind = B_FALSE;
2084 boolean_t xtreme_rewind = B_FALSE;
2085 uint64_t pool_state, txg = -1ULL;
2086 char *cachefile = NULL;
2087 importargs_t idata = { 0 };
2088 char *endptr;
2090 /* check options */
2091 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:R:tT:VX")) != -1) {
2092 switch (c) {
2093 case 'a':
2094 do_all = B_TRUE;
2095 break;
2096 case 'c':
2097 cachefile = optarg;
2098 break;
2099 case 'd':
2100 if (searchdirs == NULL) {
2101 searchdirs = safe_malloc(sizeof (char *));
2102 } else {
2103 char **tmp = safe_malloc((nsearch + 1) *
2104 sizeof (char *));
2105 bcopy(searchdirs, tmp, nsearch *
2106 sizeof (char *));
2107 free(searchdirs);
2108 searchdirs = tmp;
2110 searchdirs[nsearch++] = optarg;
2111 break;
2112 case 'D':
2113 do_destroyed = B_TRUE;
2114 break;
2115 case 'f':
2116 flags |= ZFS_IMPORT_ANY_HOST;
2117 break;
2118 case 'F':
2119 do_rewind = B_TRUE;
2120 break;
2121 case 'm':
2122 flags |= ZFS_IMPORT_MISSING_LOG;
2123 break;
2124 case 'n':
2125 dryrun = B_TRUE;
2126 break;
2127 case 'N':
2128 flags |= ZFS_IMPORT_ONLY;
2129 break;
2130 case 'o':
2131 if ((propval = strchr(optarg, '=')) != NULL) {
2132 *propval = '\0';
2133 propval++;
2134 if (add_prop_list(optarg, propval,
2135 &props, B_TRUE))
2136 goto error;
2137 } else {
2138 mntopts = optarg;
2140 break;
2141 case 'R':
2142 if (add_prop_list(zpool_prop_to_name(
2143 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2144 goto error;
2145 if (add_prop_list_default(zpool_prop_to_name(
2146 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2147 goto error;
2148 break;
2149 case 't':
2150 flags |= ZFS_IMPORT_TEMP_NAME;
2151 if (add_prop_list_default(zpool_prop_to_name(
2152 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2153 goto error;
2154 break;
2156 case 'T':
2157 errno = 0;
2158 txg = strtoull(optarg, &endptr, 0);
2159 if (errno != 0 || *endptr != '\0') {
2160 (void) fprintf(stderr,
2161 gettext("invalid txg value\n"));
2162 usage(B_FALSE);
2164 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2165 break;
2166 case 'V':
2167 flags |= ZFS_IMPORT_VERBATIM;
2168 break;
2169 case 'X':
2170 xtreme_rewind = B_TRUE;
2171 break;
2172 case ':':
2173 (void) fprintf(stderr, gettext("missing argument for "
2174 "'%c' option\n"), optopt);
2175 usage(B_FALSE);
2176 break;
2177 case '?':
2178 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2179 optopt);
2180 usage(B_FALSE);
2184 argc -= optind;
2185 argv += optind;
2187 if (cachefile && nsearch != 0) {
2188 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2189 usage(B_FALSE);
2192 if ((dryrun || xtreme_rewind) && !do_rewind) {
2193 (void) fprintf(stderr,
2194 gettext("-n or -X only meaningful with -F\n"));
2195 usage(B_FALSE);
2197 if (dryrun)
2198 rewind_policy = ZPOOL_TRY_REWIND;
2199 else if (do_rewind)
2200 rewind_policy = ZPOOL_DO_REWIND;
2201 if (xtreme_rewind)
2202 rewind_policy |= ZPOOL_EXTREME_REWIND;
2204 /* In the future, we can capture further policy and include it here */
2205 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2206 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2207 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2208 goto error;
2210 /* check argument count */
2211 if (do_all) {
2212 if (argc != 0) {
2213 (void) fprintf(stderr, gettext("too many arguments\n"));
2214 usage(B_FALSE);
2216 } else {
2217 if (argc > 2) {
2218 (void) fprintf(stderr, gettext("too many arguments\n"));
2219 usage(B_FALSE);
2223 * Check for the SYS_CONFIG privilege. We do this explicitly
2224 * here because otherwise any attempt to discover pools will
2225 * silently fail.
2227 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2228 (void) fprintf(stderr, gettext("cannot "
2229 "discover pools: permission denied\n"));
2230 if (searchdirs != NULL)
2231 free(searchdirs);
2233 nvlist_free(policy);
2234 return (1);
2239 * Depending on the arguments given, we do one of the following:
2241 * <none> Iterate through all pools and display information about
2242 * each one.
2244 * -a Iterate through all pools and try to import each one.
2246 * <id> Find the pool that corresponds to the given GUID/pool
2247 * name and import that one.
2249 * -D Above options applies only to destroyed pools.
2251 if (argc != 0) {
2252 char *endptr;
2254 errno = 0;
2255 searchguid = strtoull(argv[0], &endptr, 10);
2256 if (errno != 0 || *endptr != '\0')
2257 searchname = argv[0];
2258 found_config = NULL;
2261 * User specified a name or guid. Ensure it's unique.
2263 idata.unique = B_TRUE;
2267 * Check the environment for the preferred search path.
2269 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2270 char *dir;
2272 envdup = strdup(env);
2274 dir = strtok(envdup, ":");
2275 while (dir != NULL) {
2276 if (searchdirs == NULL) {
2277 searchdirs = safe_malloc(sizeof (char *));
2278 } else {
2279 char **tmp = safe_malloc((nsearch + 1) *
2280 sizeof (char *));
2281 bcopy(searchdirs, tmp, nsearch *
2282 sizeof (char *));
2283 free(searchdirs);
2284 searchdirs = tmp;
2286 searchdirs[nsearch++] = dir;
2287 dir = strtok(NULL, ":");
2291 idata.path = searchdirs;
2292 idata.paths = nsearch;
2293 idata.poolname = searchname;
2294 idata.guid = searchguid;
2295 idata.cachefile = cachefile;
2297 pools = zpool_search_import(g_zfs, &idata);
2299 if (pools != NULL && idata.exists &&
2300 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2301 (void) fprintf(stderr, gettext("cannot import '%s': "
2302 "a pool with that name already exists\n"),
2303 argv[0]);
2304 (void) fprintf(stderr, gettext("use the form '%s "
2305 "<pool | id> <newpool>' to give it a new name\n"),
2306 "zpool import");
2307 err = 1;
2308 } else if (pools == NULL && idata.exists) {
2309 (void) fprintf(stderr, gettext("cannot import '%s': "
2310 "a pool with that name is already created/imported,\n"),
2311 argv[0]);
2312 (void) fprintf(stderr, gettext("and no additional pools "
2313 "with that name were found\n"));
2314 err = 1;
2315 } else if (pools == NULL) {
2316 if (argc != 0) {
2317 (void) fprintf(stderr, gettext("cannot import '%s': "
2318 "no such pool available\n"), argv[0]);
2320 err = 1;
2323 if (err == 1) {
2324 if (searchdirs != NULL)
2325 free(searchdirs);
2326 if (envdup != NULL)
2327 free(envdup);
2328 nvlist_free(policy);
2329 return (1);
2333 * At this point we have a list of import candidate configs. Even if
2334 * we were searching by pool name or guid, we still need to
2335 * post-process the list to deal with pool state and possible
2336 * duplicate names.
2338 err = 0;
2339 elem = NULL;
2340 first = B_TRUE;
2341 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2343 verify(nvpair_value_nvlist(elem, &config) == 0);
2345 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2346 &pool_state) == 0);
2347 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2348 continue;
2349 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2350 continue;
2352 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2353 policy) == 0);
2355 if (argc == 0) {
2356 if (first)
2357 first = B_FALSE;
2358 else if (!do_all)
2359 (void) printf("\n");
2361 if (do_all) {
2362 err |= do_import(config, NULL, mntopts,
2363 props, flags);
2364 } else {
2365 show_import(config);
2367 } else if (searchname != NULL) {
2368 char *name;
2371 * We are searching for a pool based on name.
2373 verify(nvlist_lookup_string(config,
2374 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2376 if (strcmp(name, searchname) == 0) {
2377 if (found_config != NULL) {
2378 (void) fprintf(stderr, gettext(
2379 "cannot import '%s': more than "
2380 "one matching pool\n"), searchname);
2381 (void) fprintf(stderr, gettext(
2382 "import by numeric ID instead\n"));
2383 err = B_TRUE;
2385 found_config = config;
2387 } else {
2388 uint64_t guid;
2391 * Search for a pool by guid.
2393 verify(nvlist_lookup_uint64(config,
2394 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2396 if (guid == searchguid)
2397 found_config = config;
2402 * If we were searching for a specific pool, verify that we found a
2403 * pool, and then do the import.
2405 if (argc != 0 && err == 0) {
2406 if (found_config == NULL) {
2407 (void) fprintf(stderr, gettext("cannot import '%s': "
2408 "no such pool available\n"), argv[0]);
2409 err = B_TRUE;
2410 } else {
2411 err |= do_import(found_config, argc == 1 ? NULL :
2412 argv[1], mntopts, props, flags);
2417 * If we were just looking for pools, report an error if none were
2418 * found.
2420 if (argc == 0 && first)
2421 (void) fprintf(stderr,
2422 gettext("no pools available to import\n"));
2424 error:
2425 nvlist_free(props);
2426 nvlist_free(pools);
2427 nvlist_free(policy);
2428 if (searchdirs != NULL)
2429 free(searchdirs);
2430 if (envdup != NULL)
2431 free(envdup);
2433 return (err ? 1 : 0);
2436 typedef struct iostat_cbdata {
2437 boolean_t cb_verbose;
2438 int cb_namewidth;
2439 int cb_iteration;
2440 zpool_list_t *cb_list;
2441 } iostat_cbdata_t;
2443 static void
2444 print_iostat_separator(iostat_cbdata_t *cb)
2446 int i = 0;
2448 for (i = 0; i < cb->cb_namewidth; i++)
2449 (void) printf("-");
2450 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2453 static void
2454 print_iostat_header(iostat_cbdata_t *cb)
2456 (void) printf("%*s capacity operations bandwidth\n",
2457 cb->cb_namewidth, "");
2458 (void) printf("%-*s alloc free read write read write\n",
2459 cb->cb_namewidth, "pool");
2460 print_iostat_separator(cb);
2464 * Display a single statistic.
2466 static void
2467 print_one_stat(uint64_t value)
2469 char buf[64];
2471 zfs_nicenum(value, buf, sizeof (buf));
2472 (void) printf(" %5s", buf);
2476 * Print out all the statistics for the given vdev. This can either be the
2477 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2478 * is a verbose output, and we don't want to display the toplevel pool stats.
2480 void
2481 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2482 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2484 nvlist_t **oldchild, **newchild;
2485 uint_t c, children;
2486 vdev_stat_t *oldvs, *newvs;
2487 vdev_stat_t zerovs = { 0 };
2488 uint64_t tdelta;
2489 double scale;
2490 char *vname;
2492 if (oldnv != NULL) {
2493 verify(nvlist_lookup_uint64_array(oldnv,
2494 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2495 } else {
2496 oldvs = &zerovs;
2499 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2500 (uint64_t **)&newvs, &c) == 0);
2502 if (strlen(name) + depth > cb->cb_namewidth)
2503 (void) printf("%*s%s", depth, "", name);
2504 else
2505 (void) printf("%*s%s%*s", depth, "", name,
2506 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2508 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2510 if (tdelta == 0)
2511 scale = 1.0;
2512 else
2513 scale = (double)NANOSEC / tdelta;
2515 /* only toplevel vdevs have capacity stats */
2516 if (newvs->vs_space == 0) {
2517 (void) printf(" - -");
2518 } else {
2519 print_one_stat(newvs->vs_alloc);
2520 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2523 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2524 oldvs->vs_ops[ZIO_TYPE_READ])));
2526 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2527 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2529 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2530 oldvs->vs_bytes[ZIO_TYPE_READ])));
2532 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2533 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2535 (void) printf("\n");
2537 if (!cb->cb_verbose)
2538 return;
2540 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2541 &newchild, &children) != 0)
2542 return;
2544 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2545 &oldchild, &c) != 0)
2546 return;
2548 for (c = 0; c < children; c++) {
2549 uint64_t ishole = B_FALSE, islog = B_FALSE;
2551 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2552 &ishole);
2554 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2555 &islog);
2557 if (ishole || islog)
2558 continue;
2560 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2561 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2562 newchild[c], cb, depth + 2);
2563 free(vname);
2567 * Log device section
2570 if (num_logs(newnv) > 0) {
2571 (void) printf("%-*s - - - - - "
2572 "-\n", cb->cb_namewidth, "logs");
2574 for (c = 0; c < children; c++) {
2575 uint64_t islog = B_FALSE;
2576 (void) nvlist_lookup_uint64(newchild[c],
2577 ZPOOL_CONFIG_IS_LOG, &islog);
2579 if (islog) {
2580 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2581 B_FALSE);
2582 print_vdev_stats(zhp, vname, oldnv ?
2583 oldchild[c] : NULL, newchild[c],
2584 cb, depth + 2);
2585 free(vname);
2592 * Include level 2 ARC devices in iostat output
2594 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2595 &newchild, &children) != 0)
2596 return;
2598 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2599 &oldchild, &c) != 0)
2600 return;
2602 if (children > 0) {
2603 (void) printf("%-*s - - - - - "
2604 "-\n", cb->cb_namewidth, "cache");
2605 for (c = 0; c < children; c++) {
2606 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2607 B_FALSE);
2608 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2609 newchild[c], cb, depth + 2);
2610 free(vname);
2615 static int
2616 refresh_iostat(zpool_handle_t *zhp, void *data)
2618 iostat_cbdata_t *cb = data;
2619 boolean_t missing;
2622 * If the pool has disappeared, remove it from the list and continue.
2624 if (zpool_refresh_stats(zhp, &missing) != 0)
2625 return (-1);
2627 if (missing)
2628 pool_list_remove(cb->cb_list, zhp);
2630 return (0);
2634 * Callback to print out the iostats for the given pool.
2637 print_iostat(zpool_handle_t *zhp, void *data)
2639 iostat_cbdata_t *cb = data;
2640 nvlist_t *oldconfig, *newconfig;
2641 nvlist_t *oldnvroot, *newnvroot;
2643 newconfig = zpool_get_config(zhp, &oldconfig);
2645 if (cb->cb_iteration == 1)
2646 oldconfig = NULL;
2648 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2649 &newnvroot) == 0);
2651 if (oldconfig == NULL)
2652 oldnvroot = NULL;
2653 else
2654 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2655 &oldnvroot) == 0);
2658 * Print out the statistics for the pool.
2660 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2662 if (cb->cb_verbose)
2663 print_iostat_separator(cb);
2665 return (0);
2668 static int
2669 get_columns(void)
2671 struct winsize ws;
2672 int columns = 80;
2673 int error;
2675 if (isatty(STDOUT_FILENO)) {
2676 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2677 if (error == 0)
2678 columns = ws.ws_col;
2679 } else {
2680 columns = 999;
2683 return (columns);
2687 get_namewidth(zpool_handle_t *zhp, void *data)
2689 iostat_cbdata_t *cb = data;
2690 nvlist_t *config, *nvroot;
2691 int columns;
2693 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2694 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2695 &nvroot) == 0);
2696 if (!cb->cb_verbose)
2697 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2698 else
2699 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2700 cb->cb_namewidth);
2704 * The width must be at least 10, but may be as large as the
2705 * column width - 42 so that we can still fit in one line.
2707 columns = get_columns();
2709 if (cb->cb_namewidth < 10)
2710 cb->cb_namewidth = 10;
2711 if (cb->cb_namewidth > columns - 42)
2712 cb->cb_namewidth = columns - 42;
2714 return (0);
2718 * Parse the input string, get the 'interval' and 'count' value if there is one.
2720 static void
2721 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2722 unsigned long *cnt)
2724 unsigned long interval = 0, count = 0;
2725 int argc = *argcp;
2728 * Determine if the last argument is an integer or a pool name
2730 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2731 char *end;
2733 errno = 0;
2734 interval = strtoul(argv[argc - 1], &end, 10);
2736 if (*end == '\0' && errno == 0) {
2737 if (interval == 0) {
2738 (void) fprintf(stderr, gettext("interval "
2739 "cannot be zero\n"));
2740 usage(B_FALSE);
2743 * Ignore the last parameter
2745 argc--;
2746 } else {
2748 * If this is not a valid number, just plow on. The
2749 * user will get a more informative error message later
2750 * on.
2752 interval = 0;
2757 * If the last argument is also an integer, then we have both a count
2758 * and an interval.
2760 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2761 char *end;
2763 errno = 0;
2764 count = interval;
2765 interval = strtoul(argv[argc - 1], &end, 10);
2767 if (*end == '\0' && errno == 0) {
2768 if (interval == 0) {
2769 (void) fprintf(stderr, gettext("interval "
2770 "cannot be zero\n"));
2771 usage(B_FALSE);
2775 * Ignore the last parameter
2777 argc--;
2778 } else {
2779 interval = 0;
2783 *iv = interval;
2784 *cnt = count;
2785 *argcp = argc;
2788 static void
2789 get_timestamp_arg(char c)
2791 if (c == 'u')
2792 timestamp_fmt = UDATE;
2793 else if (c == 'd')
2794 timestamp_fmt = DDATE;
2795 else
2796 usage(B_FALSE);
2800 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2802 * -v Display statistics for individual vdevs
2803 * -T Display a timestamp in date(1) or Unix format
2805 * This command can be tricky because we want to be able to deal with pool
2806 * creation/destruction as well as vdev configuration changes. The bulk of this
2807 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2808 * on pool_list_update() to detect the addition of new pools. Configuration
2809 * changes are all handled within libzfs.
2812 zpool_do_iostat(int argc, char **argv)
2814 int c;
2815 int ret;
2816 int npools;
2817 unsigned long interval = 0, count = 0;
2818 zpool_list_t *list;
2819 boolean_t verbose = B_FALSE;
2820 iostat_cbdata_t cb;
2822 /* check options */
2823 while ((c = getopt(argc, argv, "T:v")) != -1) {
2824 switch (c) {
2825 case 'T':
2826 get_timestamp_arg(*optarg);
2827 break;
2828 case 'v':
2829 verbose = B_TRUE;
2830 break;
2831 case '?':
2832 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2833 optopt);
2834 usage(B_FALSE);
2838 argc -= optind;
2839 argv += optind;
2841 get_interval_count(&argc, argv, &interval, &count);
2844 * Construct the list of all interesting pools.
2846 ret = 0;
2847 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2848 return (1);
2850 if (pool_list_count(list) == 0 && argc != 0) {
2851 pool_list_free(list);
2852 return (1);
2855 if (pool_list_count(list) == 0 && interval == 0) {
2856 pool_list_free(list);
2857 (void) fprintf(stderr, gettext("no pools available\n"));
2858 return (1);
2862 * Enter the main iostat loop.
2864 cb.cb_list = list;
2865 cb.cb_verbose = verbose;
2866 cb.cb_iteration = 0;
2867 cb.cb_namewidth = 0;
2869 for (;;) {
2870 pool_list_update(list);
2872 if ((npools = pool_list_count(list)) == 0)
2873 (void) fprintf(stderr, gettext("no pools available\n"));
2874 else {
2876 * Refresh all statistics. This is done as an
2877 * explicit step before calculating the maximum name
2878 * width, so that any * configuration changes are
2879 * properly accounted for.
2881 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2882 &cb);
2885 * Iterate over all pools to determine the maximum width
2886 * for the pool / device name column across all pools.
2888 cb.cb_namewidth = 0;
2889 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2890 &cb);
2892 if (timestamp_fmt != NODATE)
2893 print_timestamp(timestamp_fmt);
2896 * If it's the first time, or verbose mode, print the
2897 * header.
2899 if (++cb.cb_iteration == 1 || verbose)
2900 print_iostat_header(&cb);
2902 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2905 * If there's more than one pool, and we're not in
2906 * verbose mode (which prints a separator for us),
2907 * then print a separator.
2909 if (npools > 1 && !verbose)
2910 print_iostat_separator(&cb);
2912 if (verbose)
2913 (void) printf("\n");
2917 * Flush the output so that redirection to a file isn't buffered
2918 * indefinitely.
2920 (void) fflush(stdout);
2922 if (interval == 0)
2923 break;
2925 if (count != 0 && --count == 0)
2926 break;
2928 (void) sleep(interval);
2931 pool_list_free(list);
2933 return (ret);
2936 typedef struct list_cbdata {
2937 boolean_t cb_verbose;
2938 int cb_namewidth;
2939 boolean_t cb_scripted;
2940 zprop_list_t *cb_proplist;
2941 } list_cbdata_t;
2944 * Given a list of columns to display, output appropriate headers for each one.
2946 static void
2947 print_header(list_cbdata_t *cb)
2949 zprop_list_t *pl = cb->cb_proplist;
2950 char headerbuf[ZPOOL_MAXPROPLEN];
2951 const char *header;
2952 boolean_t first = B_TRUE;
2953 boolean_t right_justify;
2954 size_t width = 0;
2956 for (; pl != NULL; pl = pl->pl_next) {
2957 width = pl->pl_width;
2958 if (first && cb->cb_verbose) {
2960 * Reset the width to accommodate the verbose listing
2961 * of devices.
2963 width = cb->cb_namewidth;
2966 if (!first)
2967 (void) printf(" ");
2968 else
2969 first = B_FALSE;
2971 right_justify = B_FALSE;
2972 if (pl->pl_prop != ZPROP_INVAL) {
2973 header = zpool_prop_column_name(pl->pl_prop);
2974 right_justify = zpool_prop_align_right(pl->pl_prop);
2975 } else {
2976 int i;
2978 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2979 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2980 headerbuf[i] = '\0';
2981 header = headerbuf;
2984 if (pl->pl_next == NULL && !right_justify)
2985 (void) printf("%s", header);
2986 else if (right_justify)
2987 (void) printf("%*s", (int)width, header);
2988 else
2989 (void) printf("%-*s", (int)width, header);
2992 (void) printf("\n");
2996 * Given a pool and a list of properties, print out all the properties according
2997 * to the described layout.
2999 static void
3000 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
3002 zprop_list_t *pl = cb->cb_proplist;
3003 boolean_t first = B_TRUE;
3004 char property[ZPOOL_MAXPROPLEN];
3005 char *propstr;
3006 boolean_t right_justify;
3007 size_t width;
3009 for (; pl != NULL; pl = pl->pl_next) {
3011 width = pl->pl_width;
3012 if (first && cb->cb_verbose) {
3014 * Reset the width to accommodate the verbose listing
3015 * of devices.
3017 width = cb->cb_namewidth;
3020 if (!first) {
3021 if (cb->cb_scripted)
3022 (void) printf("\t");
3023 else
3024 (void) printf(" ");
3025 } else {
3026 first = B_FALSE;
3029 right_justify = B_FALSE;
3030 if (pl->pl_prop != ZPROP_INVAL) {
3031 if (zpool_get_prop(zhp, pl->pl_prop, property,
3032 sizeof (property), NULL) != 0)
3033 propstr = "-";
3034 else
3035 propstr = property;
3037 right_justify = zpool_prop_align_right(pl->pl_prop);
3038 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
3039 zpool_prop_unsupported(pl->pl_user_prop)) &&
3040 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
3041 sizeof (property)) == 0) {
3042 propstr = property;
3043 } else {
3044 propstr = "-";
3049 * If this is being called in scripted mode, or if this is the
3050 * last column and it is left-justified, don't include a width
3051 * format specifier.
3053 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3054 (void) printf("%s", propstr);
3055 else if (right_justify)
3056 (void) printf("%*s", (int)width, propstr);
3057 else
3058 (void) printf("%-*s", (int)width, propstr);
3061 (void) printf("\n");
3064 static void
3065 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted,
3066 boolean_t valid)
3068 char propval[64];
3069 boolean_t fixed;
3070 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
3072 switch (prop) {
3073 case ZPOOL_PROP_EXPANDSZ:
3074 if (value == 0)
3075 (void) strlcpy(propval, "-", sizeof (propval));
3076 else
3077 zfs_nicenum(value, propval, sizeof (propval));
3078 break;
3079 case ZPOOL_PROP_FRAGMENTATION:
3080 if (value == ZFS_FRAG_INVALID) {
3081 (void) strlcpy(propval, "-", sizeof (propval));
3082 } else {
3083 (void) snprintf(propval, sizeof (propval), "%llu%%",
3084 (unsigned long long)value);
3086 break;
3087 case ZPOOL_PROP_CAPACITY:
3088 (void) snprintf(propval, sizeof (propval), "%llu%%",
3089 (unsigned long long)value);
3090 break;
3091 default:
3092 zfs_nicenum(value, propval, sizeof (propval));
3095 if (!valid)
3096 (void) strlcpy(propval, "-", sizeof (propval));
3098 if (scripted)
3099 (void) printf("\t%s", propval);
3100 else
3101 (void) printf(" %*s", (int)width, propval);
3104 void
3105 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
3106 list_cbdata_t *cb, int depth)
3108 nvlist_t **child;
3109 vdev_stat_t *vs;
3110 uint_t c, children;
3111 char *vname;
3112 boolean_t scripted = cb->cb_scripted;
3114 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3115 (uint64_t **)&vs, &c) == 0);
3117 if (name != NULL) {
3118 boolean_t toplevel = (vs->vs_space != 0);
3119 uint64_t cap;
3121 if (scripted)
3122 (void) printf("\t%s", name);
3123 else if (strlen(name) + depth > cb->cb_namewidth)
3124 (void) printf("%*s%s", depth, "", name);
3125 else
3126 (void) printf("%*s%s%*s", depth, "", name,
3127 (int)(cb->cb_namewidth - strlen(name) - depth), "");
3130 * Print the properties for the individual vdevs. Some
3131 * properties are only applicable to toplevel vdevs. The
3132 * 'toplevel' boolean value is passed to the print_one_column()
3133 * to indicate that the value is valid.
3135 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, scripted,
3136 toplevel);
3137 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, scripted,
3138 toplevel);
3139 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
3140 scripted, toplevel);
3141 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, scripted,
3142 B_TRUE);
3143 print_one_column(ZPOOL_PROP_FRAGMENTATION,
3144 vs->vs_fragmentation, scripted,
3145 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel));
3146 cap = (vs->vs_space == 0) ? 0 :
3147 (vs->vs_alloc * 100 / vs->vs_space);
3148 print_one_column(ZPOOL_PROP_CAPACITY, cap, scripted, toplevel);
3149 (void) printf("\n");
3152 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3153 &child, &children) != 0)
3154 return;
3156 for (c = 0; c < children; c++) {
3157 uint64_t ishole = B_FALSE;
3159 if (nvlist_lookup_uint64(child[c],
3160 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3161 continue;
3163 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3164 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3165 free(vname);
3169 * Include level 2 ARC devices in iostat output
3171 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3172 &child, &children) != 0)
3173 return;
3175 if (children > 0) {
3176 (void) printf("%-*s - - - - - "
3177 "-\n", cb->cb_namewidth, "cache");
3178 for (c = 0; c < children; c++) {
3179 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3180 B_FALSE);
3181 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3182 free(vname);
3189 * Generic callback function to list a pool.
3192 list_callback(zpool_handle_t *zhp, void *data)
3194 list_cbdata_t *cbp = data;
3195 nvlist_t *config;
3196 nvlist_t *nvroot;
3198 config = zpool_get_config(zhp, NULL);
3200 print_pool(zhp, cbp);
3201 if (!cbp->cb_verbose)
3202 return (0);
3204 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3205 &nvroot) == 0);
3206 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3208 return (0);
3212 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3214 * -H Scripted mode. Don't display headers, and separate properties
3215 * by a single tab.
3216 * -o List of properties to display. Defaults to
3217 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
3218 * "dedupratio,health,altroot"
3219 * -T Display a timestamp in date(1) or Unix format
3221 * List all pools in the system, whether or not they're healthy. Output space
3222 * statistics for each one, as well as health status summary.
3225 zpool_do_list(int argc, char **argv)
3227 int c;
3228 int ret = 0;
3229 list_cbdata_t cb = { 0 };
3230 static char default_props[] =
3231 "name,size,allocated,free,expandsize,fragmentation,capacity,"
3232 "dedupratio,health,altroot";
3233 char *props = default_props;
3234 unsigned long interval = 0, count = 0;
3235 zpool_list_t *list;
3236 boolean_t first = B_TRUE;
3238 /* check options */
3239 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3240 switch (c) {
3241 case 'H':
3242 cb.cb_scripted = B_TRUE;
3243 break;
3244 case 'o':
3245 props = optarg;
3246 break;
3247 case 'T':
3248 get_timestamp_arg(*optarg);
3249 break;
3250 case 'v':
3251 cb.cb_verbose = B_TRUE;
3252 break;
3253 case ':':
3254 (void) fprintf(stderr, gettext("missing argument for "
3255 "'%c' option\n"), optopt);
3256 usage(B_FALSE);
3257 break;
3258 case '?':
3259 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3260 optopt);
3261 usage(B_FALSE);
3265 argc -= optind;
3266 argv += optind;
3268 get_interval_count(&argc, argv, &interval, &count);
3270 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3271 usage(B_FALSE);
3273 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3274 return (1);
3276 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3277 (void) printf(gettext("no pools available\n"));
3278 zprop_free_list(cb.cb_proplist);
3279 return (0);
3282 for (;;) {
3283 pool_list_update(list);
3285 if (pool_list_count(list) == 0)
3286 break;
3288 if (timestamp_fmt != NODATE)
3289 print_timestamp(timestamp_fmt);
3291 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3292 print_header(&cb);
3293 first = B_FALSE;
3295 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3297 if (interval == 0)
3298 break;
3300 if (count != 0 && --count == 0)
3301 break;
3303 (void) sleep(interval);
3306 zprop_free_list(cb.cb_proplist);
3307 return (ret);
3310 static int
3311 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3313 boolean_t force = B_FALSE;
3314 int c;
3315 nvlist_t *nvroot;
3316 char *poolname, *old_disk, *new_disk;
3317 zpool_handle_t *zhp;
3318 nvlist_t *props = NULL;
3319 char *propval;
3320 int ret;
3322 /* check options */
3323 while ((c = getopt(argc, argv, "fo:")) != -1) {
3324 switch (c) {
3325 case 'f':
3326 force = B_TRUE;
3327 break;
3328 case 'o':
3329 if ((propval = strchr(optarg, '=')) == NULL) {
3330 (void) fprintf(stderr, gettext("missing "
3331 "'=' for -o option\n"));
3332 usage(B_FALSE);
3334 *propval = '\0';
3335 propval++;
3337 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3338 (add_prop_list(optarg, propval, &props, B_TRUE)))
3339 usage(B_FALSE);
3340 break;
3341 case '?':
3342 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3343 optopt);
3344 usage(B_FALSE);
3348 argc -= optind;
3349 argv += optind;
3351 /* get pool name and check number of arguments */
3352 if (argc < 1) {
3353 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3354 usage(B_FALSE);
3357 poolname = argv[0];
3359 if (argc < 2) {
3360 (void) fprintf(stderr,
3361 gettext("missing <device> specification\n"));
3362 usage(B_FALSE);
3365 old_disk = argv[1];
3367 if (argc < 3) {
3368 if (!replacing) {
3369 (void) fprintf(stderr,
3370 gettext("missing <new_device> specification\n"));
3371 usage(B_FALSE);
3373 new_disk = old_disk;
3374 argc -= 1;
3375 argv += 1;
3376 } else {
3377 new_disk = argv[2];
3378 argc -= 2;
3379 argv += 2;
3382 if (argc > 1) {
3383 (void) fprintf(stderr, gettext("too many arguments\n"));
3384 usage(B_FALSE);
3387 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3388 return (1);
3390 if (zpool_get_config(zhp, NULL) == NULL) {
3391 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3392 poolname);
3393 zpool_close(zhp);
3394 return (1);
3397 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3398 argc, argv);
3399 if (nvroot == NULL) {
3400 zpool_close(zhp);
3401 return (1);
3404 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3406 nvlist_free(nvroot);
3407 zpool_close(zhp);
3409 return (ret);
3413 * zpool replace [-f] <pool> <device> <new_device>
3415 * -f Force attach, even if <new_device> appears to be in use.
3417 * Replace <device> with <new_device>.
3419 /* ARGSUSED */
3421 zpool_do_replace(int argc, char **argv)
3423 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3427 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3429 * -f Force attach, even if <new_device> appears to be in use.
3430 * -o Set property=value.
3432 * Attach <new_device> to the mirror containing <device>. If <device> is not
3433 * part of a mirror, then <device> will be transformed into a mirror of
3434 * <device> and <new_device>. In either case, <new_device> will begin life
3435 * with a DTL of [0, now], and will immediately begin to resilver itself.
3438 zpool_do_attach(int argc, char **argv)
3440 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3444 * zpool detach [-f] <pool> <device>
3446 * -f Force detach of <device>, even if DTLs argue against it
3447 * (not supported yet)
3449 * Detach a device from a mirror. The operation will be refused if <device>
3450 * is the last device in the mirror, or if the DTLs indicate that this device
3451 * has the only valid copy of some data.
3453 /* ARGSUSED */
3455 zpool_do_detach(int argc, char **argv)
3457 int c;
3458 char *poolname, *path;
3459 zpool_handle_t *zhp;
3460 int ret;
3462 /* check options */
3463 while ((c = getopt(argc, argv, "f")) != -1) {
3464 switch (c) {
3465 case 'f':
3466 case '?':
3467 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3468 optopt);
3469 usage(B_FALSE);
3473 argc -= optind;
3474 argv += optind;
3476 /* get pool name and check number of arguments */
3477 if (argc < 1) {
3478 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3479 usage(B_FALSE);
3482 if (argc < 2) {
3483 (void) fprintf(stderr,
3484 gettext("missing <device> specification\n"));
3485 usage(B_FALSE);
3488 poolname = argv[0];
3489 path = argv[1];
3491 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3492 return (1);
3494 ret = zpool_vdev_detach(zhp, path);
3496 zpool_close(zhp);
3498 return (ret);
3502 * zpool split [-n] [-o prop=val] ...
3503 * [-o mntopt] ...
3504 * [-R altroot] <pool> <newpool> [<device> ...]
3506 * -n Do not split the pool, but display the resulting layout if
3507 * it were to be split.
3508 * -o Set property=value, or set mount options.
3509 * -R Mount the split-off pool under an alternate root.
3511 * Splits the named pool and gives it the new pool name. Devices to be split
3512 * off may be listed, provided that no more than one device is specified
3513 * per top-level vdev mirror. The newly split pool is left in an exported
3514 * state unless -R is specified.
3516 * Restrictions: the top-level of the pool pool must only be made up of
3517 * mirrors; all devices in the pool must be healthy; no device may be
3518 * undergoing a resilvering operation.
3521 zpool_do_split(int argc, char **argv)
3523 char *srcpool, *newpool, *propval;
3524 char *mntopts = NULL;
3525 splitflags_t flags;
3526 int c, ret = 0;
3527 zpool_handle_t *zhp;
3528 nvlist_t *config, *props = NULL;
3530 flags.dryrun = B_FALSE;
3531 flags.import = B_FALSE;
3533 /* check options */
3534 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3535 switch (c) {
3536 case 'R':
3537 flags.import = B_TRUE;
3538 if (add_prop_list(
3539 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3540 &props, B_TRUE) != 0) {
3541 if (props)
3542 nvlist_free(props);
3543 usage(B_FALSE);
3545 break;
3546 case 'n':
3547 flags.dryrun = B_TRUE;
3548 break;
3549 case 'o':
3550 if ((propval = strchr(optarg, '=')) != NULL) {
3551 *propval = '\0';
3552 propval++;
3553 if (add_prop_list(optarg, propval,
3554 &props, B_TRUE) != 0) {
3555 if (props)
3556 nvlist_free(props);
3557 usage(B_FALSE);
3559 } else {
3560 mntopts = optarg;
3562 break;
3563 case ':':
3564 (void) fprintf(stderr, gettext("missing argument for "
3565 "'%c' option\n"), optopt);
3566 usage(B_FALSE);
3567 break;
3568 case '?':
3569 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3570 optopt);
3571 usage(B_FALSE);
3572 break;
3576 if (!flags.import && mntopts != NULL) {
3577 (void) fprintf(stderr, gettext("setting mntopts is only "
3578 "valid when importing the pool\n"));
3579 usage(B_FALSE);
3582 argc -= optind;
3583 argv += optind;
3585 if (argc < 1) {
3586 (void) fprintf(stderr, gettext("Missing pool name\n"));
3587 usage(B_FALSE);
3589 if (argc < 2) {
3590 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3591 usage(B_FALSE);
3594 srcpool = argv[0];
3595 newpool = argv[1];
3597 argc -= 2;
3598 argv += 2;
3600 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3601 return (1);
3603 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3604 if (config == NULL) {
3605 ret = 1;
3606 } else {
3607 if (flags.dryrun) {
3608 (void) printf(gettext("would create '%s' with the "
3609 "following layout:\n\n"), newpool);
3610 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3612 nvlist_free(config);
3615 zpool_close(zhp);
3617 if (ret != 0 || flags.dryrun || !flags.import)
3618 return (ret);
3621 * The split was successful. Now we need to open the new
3622 * pool and import it.
3624 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3625 return (1);
3626 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3627 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3628 ret = 1;
3629 (void) fprintf(stderr, gettext("Split was successful, but "
3630 "the datasets could not all be mounted\n"));
3631 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3632 "different altroot\n"), "zpool import");
3634 zpool_close(zhp);
3636 return (ret);
3642 * zpool online <pool> <device> ...
3645 zpool_do_online(int argc, char **argv)
3647 int c, i;
3648 char *poolname;
3649 zpool_handle_t *zhp;
3650 int ret = 0;
3651 vdev_state_t newstate;
3652 int flags = 0;
3654 /* check options */
3655 while ((c = getopt(argc, argv, "et")) != -1) {
3656 switch (c) {
3657 case 'e':
3658 flags |= ZFS_ONLINE_EXPAND;
3659 break;
3660 case 't':
3661 case '?':
3662 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3663 optopt);
3664 usage(B_FALSE);
3668 argc -= optind;
3669 argv += optind;
3671 /* get pool name and check number of arguments */
3672 if (argc < 1) {
3673 (void) fprintf(stderr, gettext("missing pool name\n"));
3674 usage(B_FALSE);
3676 if (argc < 2) {
3677 (void) fprintf(stderr, gettext("missing device name\n"));
3678 usage(B_FALSE);
3681 poolname = argv[0];
3683 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3684 return (1);
3686 for (i = 1; i < argc; i++) {
3687 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3688 if (newstate != VDEV_STATE_HEALTHY) {
3689 (void) printf(gettext("warning: device '%s' "
3690 "onlined, but remains in faulted state\n"),
3691 argv[i]);
3692 if (newstate == VDEV_STATE_FAULTED)
3693 (void) printf(gettext("use 'zpool "
3694 "clear' to restore a faulted "
3695 "device\n"));
3696 else
3697 (void) printf(gettext("use 'zpool "
3698 "replace' to replace devices "
3699 "that are no longer present\n"));
3701 } else {
3702 ret = 1;
3706 zpool_close(zhp);
3708 return (ret);
3712 * zpool offline [-ft] <pool> <device> ...
3714 * -f Force the device into the offline state, even if doing
3715 * so would appear to compromise pool availability.
3716 * (not supported yet)
3718 * -t Only take the device off-line temporarily. The offline
3719 * state will not be persistent across reboots.
3721 /* ARGSUSED */
3723 zpool_do_offline(int argc, char **argv)
3725 int c, i;
3726 char *poolname;
3727 zpool_handle_t *zhp;
3728 int ret = 0;
3729 boolean_t istmp = B_FALSE;
3731 /* check options */
3732 while ((c = getopt(argc, argv, "ft")) != -1) {
3733 switch (c) {
3734 case 't':
3735 istmp = B_TRUE;
3736 break;
3737 case 'f':
3738 case '?':
3739 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3740 optopt);
3741 usage(B_FALSE);
3745 argc -= optind;
3746 argv += optind;
3748 /* get pool name and check number of arguments */
3749 if (argc < 1) {
3750 (void) fprintf(stderr, gettext("missing pool name\n"));
3751 usage(B_FALSE);
3753 if (argc < 2) {
3754 (void) fprintf(stderr, gettext("missing device name\n"));
3755 usage(B_FALSE);
3758 poolname = argv[0];
3760 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3761 return (1);
3763 for (i = 1; i < argc; i++) {
3764 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3765 ret = 1;
3768 zpool_close(zhp);
3770 return (ret);
3774 * zpool clear <pool> [device]
3776 * Clear all errors associated with a pool or a particular device.
3779 zpool_do_clear(int argc, char **argv)
3781 int c;
3782 int ret = 0;
3783 boolean_t dryrun = B_FALSE;
3784 boolean_t do_rewind = B_FALSE;
3785 boolean_t xtreme_rewind = B_FALSE;
3786 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3787 nvlist_t *policy = NULL;
3788 zpool_handle_t *zhp;
3789 char *pool, *device;
3791 /* check options */
3792 while ((c = getopt(argc, argv, "FnX")) != -1) {
3793 switch (c) {
3794 case 'F':
3795 do_rewind = B_TRUE;
3796 break;
3797 case 'n':
3798 dryrun = B_TRUE;
3799 break;
3800 case 'X':
3801 xtreme_rewind = B_TRUE;
3802 break;
3803 case '?':
3804 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3805 optopt);
3806 usage(B_FALSE);
3810 argc -= optind;
3811 argv += optind;
3813 if (argc < 1) {
3814 (void) fprintf(stderr, gettext("missing pool name\n"));
3815 usage(B_FALSE);
3818 if (argc > 2) {
3819 (void) fprintf(stderr, gettext("too many arguments\n"));
3820 usage(B_FALSE);
3823 if ((dryrun || xtreme_rewind) && !do_rewind) {
3824 (void) fprintf(stderr,
3825 gettext("-n or -X only meaningful with -F\n"));
3826 usage(B_FALSE);
3828 if (dryrun)
3829 rewind_policy = ZPOOL_TRY_REWIND;
3830 else if (do_rewind)
3831 rewind_policy = ZPOOL_DO_REWIND;
3832 if (xtreme_rewind)
3833 rewind_policy |= ZPOOL_EXTREME_REWIND;
3835 /* In future, further rewind policy choices can be passed along here */
3836 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3837 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3838 return (1);
3840 pool = argv[0];
3841 device = argc == 2 ? argv[1] : NULL;
3843 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3844 nvlist_free(policy);
3845 return (1);
3848 if (zpool_clear(zhp, device, policy) != 0)
3849 ret = 1;
3851 zpool_close(zhp);
3853 nvlist_free(policy);
3855 return (ret);
3859 * zpool reguid <pool>
3862 zpool_do_reguid(int argc, char **argv)
3864 int c;
3865 char *poolname;
3866 zpool_handle_t *zhp;
3867 int ret = 0;
3869 /* check options */
3870 while ((c = getopt(argc, argv, "")) != -1) {
3871 switch (c) {
3872 case '?':
3873 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3874 optopt);
3875 usage(B_FALSE);
3879 argc -= optind;
3880 argv += optind;
3882 /* get pool name and check number of arguments */
3883 if (argc < 1) {
3884 (void) fprintf(stderr, gettext("missing pool name\n"));
3885 usage(B_FALSE);
3888 if (argc > 1) {
3889 (void) fprintf(stderr, gettext("too many arguments\n"));
3890 usage(B_FALSE);
3893 poolname = argv[0];
3894 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3895 return (1);
3897 ret = zpool_reguid(zhp);
3899 zpool_close(zhp);
3900 return (ret);
3905 * zpool reopen <pool>
3907 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3910 zpool_do_reopen(int argc, char **argv)
3912 int c;
3913 int ret = 0;
3914 zpool_handle_t *zhp;
3915 char *pool;
3917 /* check options */
3918 while ((c = getopt(argc, argv, "")) != -1) {
3919 switch (c) {
3920 case '?':
3921 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3922 optopt);
3923 usage(B_FALSE);
3927 argc--;
3928 argv++;
3930 if (argc < 1) {
3931 (void) fprintf(stderr, gettext("missing pool name\n"));
3932 usage(B_FALSE);
3935 if (argc > 1) {
3936 (void) fprintf(stderr, gettext("too many arguments\n"));
3937 usage(B_FALSE);
3940 pool = argv[0];
3941 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3942 return (1);
3944 ret = zpool_reopen(zhp);
3945 zpool_close(zhp);
3946 return (ret);
3949 typedef struct scrub_cbdata {
3950 int cb_type;
3951 int cb_argc;
3952 char **cb_argv;
3953 } scrub_cbdata_t;
3956 scrub_callback(zpool_handle_t *zhp, void *data)
3958 scrub_cbdata_t *cb = data;
3959 int err;
3962 * Ignore faulted pools.
3964 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3965 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3966 "currently unavailable\n"), zpool_get_name(zhp));
3967 return (1);
3970 err = zpool_scan(zhp, cb->cb_type);
3972 return (err != 0);
3976 * zpool scrub [-s] <pool> ...
3978 * -s Stop. Stops any in-progress scrub.
3981 zpool_do_scrub(int argc, char **argv)
3983 int c;
3984 scrub_cbdata_t cb;
3986 cb.cb_type = POOL_SCAN_SCRUB;
3988 /* check options */
3989 while ((c = getopt(argc, argv, "s")) != -1) {
3990 switch (c) {
3991 case 's':
3992 cb.cb_type = POOL_SCAN_NONE;
3993 break;
3994 case '?':
3995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3996 optopt);
3997 usage(B_FALSE);
4001 cb.cb_argc = argc;
4002 cb.cb_argv = argv;
4003 argc -= optind;
4004 argv += optind;
4006 if (argc < 1) {
4007 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4008 usage(B_FALSE);
4011 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
4014 typedef struct status_cbdata {
4015 int cb_count;
4016 boolean_t cb_allpools;
4017 boolean_t cb_verbose;
4018 boolean_t cb_explain;
4019 boolean_t cb_first;
4020 boolean_t cb_dedup_stats;
4021 } status_cbdata_t;
4024 * Print out detailed scrub status.
4026 void
4027 print_scan_status(pool_scan_stat_t *ps)
4029 time_t start, end;
4030 uint64_t elapsed, mins_left, hours_left;
4031 uint64_t pass_exam, examined, total;
4032 uint_t rate;
4033 double fraction_done;
4034 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
4036 (void) printf(gettext(" scan: "));
4038 /* If there's never been a scan, there's not much to say. */
4039 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
4040 ps->pss_func >= POOL_SCAN_FUNCS) {
4041 (void) printf(gettext("none requested\n"));
4042 return;
4045 start = ps->pss_start_time;
4046 end = ps->pss_end_time;
4047 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
4049 assert(ps->pss_func == POOL_SCAN_SCRUB ||
4050 ps->pss_func == POOL_SCAN_RESILVER);
4052 * Scan is finished or canceled.
4054 if (ps->pss_state == DSS_FINISHED) {
4055 uint64_t minutes_taken = (end - start) / 60;
4056 char *fmt = NULL;
4058 if (ps->pss_func == POOL_SCAN_SCRUB) {
4059 fmt = gettext("scrub repaired %s in %lluh%um with "
4060 "%llu errors on %s");
4061 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4062 fmt = gettext("resilvered %s in %lluh%um with "
4063 "%llu errors on %s");
4065 /* LINTED */
4066 (void) printf(fmt, processed_buf,
4067 (u_longlong_t)(minutes_taken / 60),
4068 (uint_t)(minutes_taken % 60),
4069 (u_longlong_t)ps->pss_errors,
4070 ctime((time_t *)&end));
4071 return;
4072 } else if (ps->pss_state == DSS_CANCELED) {
4073 if (ps->pss_func == POOL_SCAN_SCRUB) {
4074 (void) printf(gettext("scrub canceled on %s"),
4075 ctime(&end));
4076 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4077 (void) printf(gettext("resilver canceled on %s"),
4078 ctime(&end));
4080 return;
4083 assert(ps->pss_state == DSS_SCANNING);
4086 * Scan is in progress.
4088 if (ps->pss_func == POOL_SCAN_SCRUB) {
4089 (void) printf(gettext("scrub in progress since %s"),
4090 ctime(&start));
4091 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
4092 (void) printf(gettext("resilver in progress since %s"),
4093 ctime(&start));
4096 examined = ps->pss_examined ? ps->pss_examined : 1;
4097 total = ps->pss_to_examine;
4098 fraction_done = (double)examined / total;
4100 /* elapsed time for this pass */
4101 elapsed = time(NULL) - ps->pss_pass_start;
4102 elapsed = elapsed ? elapsed : 1;
4103 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
4104 rate = pass_exam / elapsed;
4105 rate = rate ? rate : 1;
4106 mins_left = ((total - examined) / rate) / 60;
4107 hours_left = mins_left / 60;
4109 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
4110 zfs_nicenum(total, total_buf, sizeof (total_buf));
4111 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
4114 * do not print estimated time if hours_left is more than 30 days
4116 (void) printf(gettext(" %s scanned out of %s at %s/s"),
4117 examined_buf, total_buf, rate_buf);
4118 if (hours_left < (30 * 24)) {
4119 (void) printf(gettext(", %lluh%um to go\n"),
4120 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
4121 } else {
4122 (void) printf(gettext(
4123 ", (scan is slow, no estimated time)\n"));
4126 if (ps->pss_func == POOL_SCAN_RESILVER) {
4127 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
4128 processed_buf, 100 * fraction_done);
4129 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
4130 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
4131 processed_buf, 100 * fraction_done);
4135 static void
4136 print_error_log(zpool_handle_t *zhp)
4138 nvlist_t *nverrlist = NULL;
4139 nvpair_t *elem;
4140 char *pathname;
4141 size_t len = MAXPATHLEN * 2;
4143 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4144 (void) printf("errors: List of errors unavailable "
4145 "(insufficient privileges)\n");
4146 return;
4149 (void) printf("errors: Permanent errors have been "
4150 "detected in the following files:\n\n");
4152 pathname = safe_malloc(len);
4153 elem = NULL;
4154 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4155 nvlist_t *nv;
4156 uint64_t dsobj, obj;
4158 verify(nvpair_value_nvlist(elem, &nv) == 0);
4159 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4160 &dsobj) == 0);
4161 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4162 &obj) == 0);
4163 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4164 (void) printf("%7s %s\n", "", pathname);
4166 free(pathname);
4167 nvlist_free(nverrlist);
4170 static void
4171 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4172 int namewidth)
4174 uint_t i;
4175 char *name;
4177 if (nspares == 0)
4178 return;
4180 (void) printf(gettext("\tspares\n"));
4182 for (i = 0; i < nspares; i++) {
4183 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4184 print_status_config(zhp, name, spares[i],
4185 namewidth, 2, B_TRUE);
4186 free(name);
4190 static void
4191 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4192 int namewidth)
4194 uint_t i;
4195 char *name;
4197 if (nl2cache == 0)
4198 return;
4200 (void) printf(gettext("\tcache\n"));
4202 for (i = 0; i < nl2cache; i++) {
4203 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4204 print_status_config(zhp, name, l2cache[i],
4205 namewidth, 2, B_FALSE);
4206 free(name);
4210 static void
4211 print_dedup_stats(nvlist_t *config)
4213 ddt_histogram_t *ddh;
4214 ddt_stat_t *dds;
4215 ddt_object_t *ddo;
4216 uint_t c;
4219 * If the pool was faulted then we may not have been able to
4220 * obtain the config. Otherwise, if we have anything in the dedup
4221 * table continue processing the stats.
4223 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4224 (uint64_t **)&ddo, &c) != 0)
4225 return;
4227 (void) printf("\n");
4228 (void) printf(gettext(" dedup: "));
4229 if (ddo->ddo_count == 0) {
4230 (void) printf(gettext("no DDT entries\n"));
4231 return;
4234 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4235 (u_longlong_t)ddo->ddo_count,
4236 (u_longlong_t)ddo->ddo_dspace,
4237 (u_longlong_t)ddo->ddo_mspace);
4239 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4240 (uint64_t **)&dds, &c) == 0);
4241 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4242 (uint64_t **)&ddh, &c) == 0);
4243 zpool_dump_ddt(dds, ddh);
4247 * Display a summary of pool status. Displays a summary such as:
4249 * pool: tank
4250 * status: DEGRADED
4251 * reason: One or more devices ...
4252 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4253 * config:
4254 * mirror DEGRADED
4255 * c1t0d0 OK
4256 * c2t0d0 UNAVAIL
4258 * When given the '-v' option, we print out the complete config. If the '-e'
4259 * option is specified, then we print out error rate information as well.
4262 status_callback(zpool_handle_t *zhp, void *data)
4264 status_cbdata_t *cbp = data;
4265 nvlist_t *config, *nvroot;
4266 char *msgid;
4267 zpool_status_t reason;
4268 zpool_errata_t errata;
4269 const char *health;
4270 uint_t c;
4271 vdev_stat_t *vs;
4273 config = zpool_get_config(zhp, NULL);
4274 reason = zpool_get_status(zhp, &msgid, &errata);
4276 cbp->cb_count++;
4279 * If we were given 'zpool status -x', only report those pools with
4280 * problems.
4282 if (cbp->cb_explain &&
4283 (reason == ZPOOL_STATUS_OK ||
4284 reason == ZPOOL_STATUS_VERSION_OLDER ||
4285 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4286 if (!cbp->cb_allpools) {
4287 (void) printf(gettext("pool '%s' is healthy\n"),
4288 zpool_get_name(zhp));
4289 if (cbp->cb_first)
4290 cbp->cb_first = B_FALSE;
4292 return (0);
4295 if (cbp->cb_first)
4296 cbp->cb_first = B_FALSE;
4297 else
4298 (void) printf("\n");
4300 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4301 &nvroot) == 0);
4302 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4303 (uint64_t **)&vs, &c) == 0);
4304 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4306 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4307 (void) printf(gettext(" state: %s\n"), health);
4309 switch (reason) {
4310 case ZPOOL_STATUS_MISSING_DEV_R:
4311 (void) printf(gettext("status: One or more devices could not "
4312 "be opened. Sufficient replicas exist for\n\tthe pool to "
4313 "continue functioning in a degraded state.\n"));
4314 (void) printf(gettext("action: Attach the missing device and "
4315 "online it using 'zpool online'.\n"));
4316 break;
4318 case ZPOOL_STATUS_MISSING_DEV_NR:
4319 (void) printf(gettext("status: One or more devices could not "
4320 "be opened. There are insufficient\n\treplicas for the "
4321 "pool to continue functioning.\n"));
4322 (void) printf(gettext("action: Attach the missing device and "
4323 "online it using 'zpool online'.\n"));
4324 break;
4326 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4327 (void) printf(gettext("status: One or more devices could not "
4328 "be used because the label is missing or\n\tinvalid. "
4329 "Sufficient replicas exist for the pool to continue\n\t"
4330 "functioning in a degraded state.\n"));
4331 (void) printf(gettext("action: Replace the device using "
4332 "'zpool replace'.\n"));
4333 break;
4335 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4336 (void) printf(gettext("status: One or more devices could not "
4337 "be used because the label is missing \n\tor invalid. "
4338 "There are insufficient replicas for the pool to "
4339 "continue\n\tfunctioning.\n"));
4340 zpool_explain_recover(zpool_get_handle(zhp),
4341 zpool_get_name(zhp), reason, config);
4342 break;
4344 case ZPOOL_STATUS_FAILING_DEV:
4345 (void) printf(gettext("status: One or more devices has "
4346 "experienced an unrecoverable error. An\n\tattempt was "
4347 "made to correct the error. Applications are "
4348 "unaffected.\n"));
4349 (void) printf(gettext("action: Determine if the device needs "
4350 "to be replaced, and clear the errors\n\tusing "
4351 "'zpool clear' or replace the device with 'zpool "
4352 "replace'.\n"));
4353 break;
4355 case ZPOOL_STATUS_OFFLINE_DEV:
4356 (void) printf(gettext("status: One or more devices has "
4357 "been taken offline by the administrator.\n\tSufficient "
4358 "replicas exist for the pool to continue functioning in "
4359 "a\n\tdegraded state.\n"));
4360 (void) printf(gettext("action: Online the device using "
4361 "'zpool online' or replace the device with\n\t'zpool "
4362 "replace'.\n"));
4363 break;
4365 case ZPOOL_STATUS_REMOVED_DEV:
4366 (void) printf(gettext("status: One or more devices has "
4367 "been removed by the administrator.\n\tSufficient "
4368 "replicas exist for the pool to continue functioning in "
4369 "a\n\tdegraded state.\n"));
4370 (void) printf(gettext("action: Online the device using "
4371 "'zpool online' or replace the device with\n\t'zpool "
4372 "replace'.\n"));
4373 break;
4375 case ZPOOL_STATUS_RESILVERING:
4376 (void) printf(gettext("status: One or more devices is "
4377 "currently being resilvered. The pool will\n\tcontinue "
4378 "to function, possibly in a degraded state.\n"));
4379 (void) printf(gettext("action: Wait for the resilver to "
4380 "complete.\n"));
4381 break;
4383 case ZPOOL_STATUS_CORRUPT_DATA:
4384 (void) printf(gettext("status: One or more devices has "
4385 "experienced an error resulting in data\n\tcorruption. "
4386 "Applications may be affected.\n"));
4387 (void) printf(gettext("action: Restore the file in question "
4388 "if possible. Otherwise restore the\n\tentire pool from "
4389 "backup.\n"));
4390 break;
4392 case ZPOOL_STATUS_CORRUPT_POOL:
4393 (void) printf(gettext("status: The pool metadata is corrupted "
4394 "and the pool cannot be opened.\n"));
4395 zpool_explain_recover(zpool_get_handle(zhp),
4396 zpool_get_name(zhp), reason, config);
4397 break;
4399 case ZPOOL_STATUS_VERSION_OLDER:
4400 (void) printf(gettext("status: The pool is formatted using a "
4401 "legacy on-disk format. The pool can\n\tstill be used, "
4402 "but some features are unavailable.\n"));
4403 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4404 "upgrade'. Once this is done, the\n\tpool will no longer "
4405 "be accessible on software that does not support\n\t"
4406 "feature flags.\n"));
4407 break;
4409 case ZPOOL_STATUS_VERSION_NEWER:
4410 (void) printf(gettext("status: The pool has been upgraded to a "
4411 "newer, incompatible on-disk version.\n\tThe pool cannot "
4412 "be accessed on this system.\n"));
4413 (void) printf(gettext("action: Access the pool from a system "
4414 "running more recent software, or\n\trestore the pool from "
4415 "backup.\n"));
4416 break;
4418 case ZPOOL_STATUS_FEAT_DISABLED:
4419 (void) printf(gettext("status: Some supported features are not "
4420 "enabled on the pool. The pool can\n\tstill be used, but "
4421 "some features are unavailable.\n"));
4422 (void) printf(gettext("action: Enable all features using "
4423 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4424 "longer be accessible by software that does not support\n\t"
4425 "the features. See zpool-features(5) for details.\n"));
4426 break;
4428 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4429 (void) printf(gettext("status: The pool cannot be accessed on "
4430 "this system because it uses the\n\tfollowing feature(s) "
4431 "not supported on this system:\n"));
4432 zpool_print_unsup_feat(config);
4433 (void) printf("\n");
4434 (void) printf(gettext("action: Access the pool from a system "
4435 "that supports the required feature(s),\n\tor restore the "
4436 "pool from backup.\n"));
4437 break;
4439 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4440 (void) printf(gettext("status: The pool can only be accessed "
4441 "in read-only mode on this system. It\n\tcannot be "
4442 "accessed in read-write mode because it uses the "
4443 "following\n\tfeature(s) not supported on this system:\n"));
4444 zpool_print_unsup_feat(config);
4445 (void) printf("\n");
4446 (void) printf(gettext("action: The pool cannot be accessed in "
4447 "read-write mode. Import the pool with\n"
4448 "\t\"-o readonly=on\", access the pool from a system that "
4449 "supports the\n\trequired feature(s), or restore the "
4450 "pool from backup.\n"));
4451 break;
4453 case ZPOOL_STATUS_FAULTED_DEV_R:
4454 (void) printf(gettext("status: One or more devices are "
4455 "faulted in response to persistent errors.\n\tSufficient "
4456 "replicas exist for the pool to continue functioning "
4457 "in a\n\tdegraded state.\n"));
4458 (void) printf(gettext("action: Replace the faulted device, "
4459 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4460 break;
4462 case ZPOOL_STATUS_FAULTED_DEV_NR:
4463 (void) printf(gettext("status: One or more devices are "
4464 "faulted in response to persistent errors. There are "
4465 "insufficient replicas for the pool to\n\tcontinue "
4466 "functioning.\n"));
4467 (void) printf(gettext("action: Destroy and re-create the pool "
4468 "from a backup source. Manually marking the device\n"
4469 "\trepaired using 'zpool clear' may allow some data "
4470 "to be recovered.\n"));
4471 break;
4473 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4474 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4475 (void) printf(gettext("status: One or more devices are "
4476 "faulted in response to IO failures.\n"));
4477 (void) printf(gettext("action: Make sure the affected devices "
4478 "are connected, then run 'zpool clear'.\n"));
4479 break;
4481 case ZPOOL_STATUS_BAD_LOG:
4482 (void) printf(gettext("status: An intent log record "
4483 "could not be read.\n"
4484 "\tWaiting for adminstrator intervention to fix the "
4485 "faulted pool.\n"));
4486 (void) printf(gettext("action: Either restore the affected "
4487 "device(s) and run 'zpool online',\n"
4488 "\tor ignore the intent log records by running "
4489 "'zpool clear'.\n"));
4490 break;
4492 case ZPOOL_STATUS_HOSTID_MISMATCH:
4493 (void) printf(gettext("status: Mismatch between pool hostid "
4494 "and system hostid on imported pool.\n\tThis pool was "
4495 "previously imported into a system with a different "
4496 "hostid,\n\tand then was verbatim imported into this "
4497 "system.\n"));
4498 (void) printf(gettext("action: Export this pool on all systems "
4499 "on which it is imported.\n"
4500 "\tThen import it to correct the mismatch.\n"));
4501 break;
4503 case ZPOOL_STATUS_ERRATA:
4504 (void) printf(gettext("status: Errata #%d detected.\n"),
4505 errata);
4507 switch (errata) {
4508 case ZPOOL_ERRATA_NONE:
4509 break;
4511 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
4512 (void) printf(gettext("action: To correct the issue "
4513 "run 'zpool scrub'.\n"));
4514 break;
4516 default:
4518 * All errata which allow the pool to be imported
4519 * must contain an action message.
4521 assert(0);
4523 break;
4525 default:
4527 * The remaining errors can't actually be generated, yet.
4529 assert(reason == ZPOOL_STATUS_OK);
4532 if (msgid != NULL)
4533 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4534 msgid);
4536 if (config != NULL) {
4537 int namewidth;
4538 uint64_t nerr;
4539 nvlist_t **spares, **l2cache;
4540 uint_t nspares, nl2cache;
4541 pool_scan_stat_t *ps = NULL;
4543 (void) nvlist_lookup_uint64_array(nvroot,
4544 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4545 print_scan_status(ps);
4547 namewidth = max_width(zhp, nvroot, 0, 0);
4548 if (namewidth < 10)
4549 namewidth = 10;
4551 (void) printf(gettext("config:\n\n"));
4552 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4553 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4554 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4555 namewidth, 0, B_FALSE);
4557 if (num_logs(nvroot) > 0)
4558 print_logs(zhp, nvroot, namewidth, B_TRUE);
4559 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4560 &l2cache, &nl2cache) == 0)
4561 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4563 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4564 &spares, &nspares) == 0)
4565 print_spares(zhp, spares, nspares, namewidth);
4567 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4568 &nerr) == 0) {
4569 nvlist_t *nverrlist = NULL;
4572 * If the approximate error count is small, get a
4573 * precise count by fetching the entire log and
4574 * uniquifying the results.
4576 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4577 zpool_get_errlog(zhp, &nverrlist) == 0) {
4578 nvpair_t *elem;
4580 elem = NULL;
4581 nerr = 0;
4582 while ((elem = nvlist_next_nvpair(nverrlist,
4583 elem)) != NULL) {
4584 nerr++;
4587 nvlist_free(nverrlist);
4589 (void) printf("\n");
4591 if (nerr == 0)
4592 (void) printf(gettext("errors: No known data "
4593 "errors\n"));
4594 else if (!cbp->cb_verbose)
4595 (void) printf(gettext("errors: %llu data "
4596 "errors, use '-v' for a list\n"),
4597 (u_longlong_t)nerr);
4598 else
4599 print_error_log(zhp);
4602 if (cbp->cb_dedup_stats)
4603 print_dedup_stats(config);
4604 } else {
4605 (void) printf(gettext("config: The configuration cannot be "
4606 "determined.\n"));
4609 return (0);
4613 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4615 * -v Display complete error logs
4616 * -x Display only pools with potential problems
4617 * -D Display dedup status (undocumented)
4618 * -T Display a timestamp in date(1) or Unix format
4620 * Describes the health status of all pools or some subset.
4623 zpool_do_status(int argc, char **argv)
4625 int c;
4626 int ret;
4627 unsigned long interval = 0, count = 0;
4628 status_cbdata_t cb = { 0 };
4630 /* check options */
4631 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4632 switch (c) {
4633 case 'v':
4634 cb.cb_verbose = B_TRUE;
4635 break;
4636 case 'x':
4637 cb.cb_explain = B_TRUE;
4638 break;
4639 case 'D':
4640 cb.cb_dedup_stats = B_TRUE;
4641 break;
4642 case 'T':
4643 get_timestamp_arg(*optarg);
4644 break;
4645 case '?':
4646 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4647 optopt);
4648 usage(B_FALSE);
4652 argc -= optind;
4653 argv += optind;
4655 get_interval_count(&argc, argv, &interval, &count);
4657 if (argc == 0)
4658 cb.cb_allpools = B_TRUE;
4660 cb.cb_first = B_TRUE;
4662 for (;;) {
4663 if (timestamp_fmt != NODATE)
4664 print_timestamp(timestamp_fmt);
4666 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4667 status_callback, &cb);
4669 if (argc == 0 && cb.cb_count == 0)
4670 (void) fprintf(stderr, gettext("no pools available\n"));
4671 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4672 (void) printf(gettext("all pools are healthy\n"));
4674 if (ret != 0)
4675 return (ret);
4677 if (interval == 0)
4678 break;
4680 if (count != 0 && --count == 0)
4681 break;
4683 (void) sleep(interval);
4686 return (0);
4689 typedef struct upgrade_cbdata {
4690 int cb_first;
4691 int cb_argc;
4692 uint64_t cb_version;
4693 char **cb_argv;
4694 } upgrade_cbdata_t;
4696 static int
4697 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
4699 int zfs_version = (int) zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
4700 int *count = (int *)unsupp_fs;
4702 if (zfs_version > ZPL_VERSION) {
4703 (void) printf(gettext("%s (v%d) is not supported by this "
4704 "implementation of ZFS.\n"),
4705 zfs_get_name(zhp), zfs_version);
4706 (*count)++;
4709 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
4711 zfs_close(zhp);
4713 return (0);
4716 static int
4717 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4719 int ret;
4720 nvlist_t *config;
4721 uint64_t oldversion;
4722 int unsupp_fs = 0;
4724 config = zpool_get_config(zhp, NULL);
4725 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4726 &oldversion) == 0);
4728 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4729 assert(oldversion < version);
4731 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
4732 if (ret != 0)
4733 return (ret);
4735 if (unsupp_fs) {
4736 (void) fprintf(stderr, gettext("Upgrade not performed due "
4737 "to %d unsupported filesystems (max v%d).\n"),
4738 unsupp_fs, (int) ZPL_VERSION);
4739 return (1);
4742 ret = zpool_upgrade(zhp, version);
4743 if (ret != 0)
4744 return (ret);
4746 if (version >= SPA_VERSION_FEATURES) {
4747 (void) printf(gettext("Successfully upgraded "
4748 "'%s' from version %llu to feature flags.\n"),
4749 zpool_get_name(zhp), (u_longlong_t) oldversion);
4750 } else {
4751 (void) printf(gettext("Successfully upgraded "
4752 "'%s' from version %llu to version %llu.\n"),
4753 zpool_get_name(zhp), (u_longlong_t) oldversion,
4754 (u_longlong_t) version);
4757 return (0);
4760 static int
4761 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4763 int i, ret, count;
4764 boolean_t firstff = B_TRUE;
4765 nvlist_t *enabled = zpool_get_features(zhp);
4767 count = 0;
4768 for (i = 0; i < SPA_FEATURES; i++) {
4769 const char *fname = spa_feature_table[i].fi_uname;
4770 const char *fguid = spa_feature_table[i].fi_guid;
4771 if (!nvlist_exists(enabled, fguid)) {
4772 char *propname;
4773 verify(-1 != asprintf(&propname, "feature@%s", fname));
4774 ret = zpool_set_prop(zhp, propname,
4775 ZFS_FEATURE_ENABLED);
4776 if (ret != 0) {
4777 free(propname);
4778 return (ret);
4780 count++;
4782 if (firstff) {
4783 (void) printf(gettext("Enabled the "
4784 "following features on '%s':\n"),
4785 zpool_get_name(zhp));
4786 firstff = B_FALSE;
4788 (void) printf(gettext(" %s\n"), fname);
4789 free(propname);
4793 if (countp != NULL)
4794 *countp = count;
4795 return (0);
4798 static int
4799 upgrade_cb(zpool_handle_t *zhp, void *arg)
4801 upgrade_cbdata_t *cbp = arg;
4802 nvlist_t *config;
4803 uint64_t version;
4804 boolean_t printnl = B_FALSE;
4805 int ret;
4807 config = zpool_get_config(zhp, NULL);
4808 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4809 &version) == 0);
4811 assert(SPA_VERSION_IS_SUPPORTED(version));
4813 if (version < cbp->cb_version) {
4814 cbp->cb_first = B_FALSE;
4815 ret = upgrade_version(zhp, cbp->cb_version);
4816 if (ret != 0)
4817 return (ret);
4818 printnl = B_TRUE;
4821 * If they did "zpool upgrade -a", then we could
4822 * be doing ioctls to different pools. We need
4823 * to log this history once to each pool, and bypass
4824 * the normal history logging that happens in main().
4826 (void) zpool_log_history(g_zfs, history_str);
4827 log_history = B_FALSE;
4830 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4831 int count;
4832 ret = upgrade_enable_all(zhp, &count);
4833 if (ret != 0)
4834 return (ret);
4836 if (count > 0) {
4837 cbp->cb_first = B_FALSE;
4838 printnl = B_TRUE;
4842 if (printnl) {
4843 (void) printf(gettext("\n"));
4846 return (0);
4849 static int
4850 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4852 upgrade_cbdata_t *cbp = arg;
4853 nvlist_t *config;
4854 uint64_t version;
4856 config = zpool_get_config(zhp, NULL);
4857 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4858 &version) == 0);
4860 assert(SPA_VERSION_IS_SUPPORTED(version));
4862 if (version < SPA_VERSION_FEATURES) {
4863 if (cbp->cb_first) {
4864 (void) printf(gettext("The following pools are "
4865 "formatted with legacy version numbers and can\n"
4866 "be upgraded to use feature flags. After "
4867 "being upgraded, these pools\nwill no "
4868 "longer be accessible by software that does not "
4869 "support feature\nflags.\n\n"));
4870 (void) printf(gettext("VER POOL\n"));
4871 (void) printf(gettext("--- ------------\n"));
4872 cbp->cb_first = B_FALSE;
4875 (void) printf("%2llu %s\n", (u_longlong_t)version,
4876 zpool_get_name(zhp));
4879 return (0);
4882 static int
4883 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4885 upgrade_cbdata_t *cbp = arg;
4886 nvlist_t *config;
4887 uint64_t version;
4889 config = zpool_get_config(zhp, NULL);
4890 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4891 &version) == 0);
4893 if (version >= SPA_VERSION_FEATURES) {
4894 int i;
4895 boolean_t poolfirst = B_TRUE;
4896 nvlist_t *enabled = zpool_get_features(zhp);
4898 for (i = 0; i < SPA_FEATURES; i++) {
4899 const char *fguid = spa_feature_table[i].fi_guid;
4900 const char *fname = spa_feature_table[i].fi_uname;
4901 if (!nvlist_exists(enabled, fguid)) {
4902 if (cbp->cb_first) {
4903 (void) printf(gettext("\nSome "
4904 "supported features are not "
4905 "enabled on the following pools. "
4906 "Once a\nfeature is enabled the "
4907 "pool may become incompatible with "
4908 "software\nthat does not support "
4909 "the feature. See "
4910 "zpool-features(5) for "
4911 "details.\n\n"));
4912 (void) printf(gettext("POOL "
4913 "FEATURE\n"));
4914 (void) printf(gettext("------"
4915 "---------\n"));
4916 cbp->cb_first = B_FALSE;
4919 if (poolfirst) {
4920 (void) printf(gettext("%s\n"),
4921 zpool_get_name(zhp));
4922 poolfirst = B_FALSE;
4925 (void) printf(gettext(" %s\n"), fname);
4928 * If they did "zpool upgrade -a", then we could
4929 * be doing ioctls to different pools. We need
4930 * to log this history once to each pool, and bypass
4931 * the normal history logging that happens in main().
4933 (void) zpool_log_history(g_zfs, history_str);
4934 log_history = B_FALSE;
4938 return (0);
4941 /* ARGSUSED */
4942 static int
4943 upgrade_one(zpool_handle_t *zhp, void *data)
4945 boolean_t printnl = B_FALSE;
4946 upgrade_cbdata_t *cbp = data;
4947 uint64_t cur_version;
4948 int ret;
4950 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4951 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
4952 "Pool 'log' must be renamed using export and import"
4953 " to upgrade.\n"));
4954 return (1);
4957 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4958 if (cur_version > cbp->cb_version) {
4959 (void) printf(gettext("Pool '%s' is already formatted "
4960 "using more current version '%llu'.\n\n"),
4961 zpool_get_name(zhp), (u_longlong_t) cur_version);
4962 return (0);
4965 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4966 (void) printf(gettext("Pool '%s' is already formatted "
4967 "using version %llu.\n\n"), zpool_get_name(zhp),
4968 (u_longlong_t) cbp->cb_version);
4969 return (0);
4972 if (cur_version != cbp->cb_version) {
4973 printnl = B_TRUE;
4974 ret = upgrade_version(zhp, cbp->cb_version);
4975 if (ret != 0)
4976 return (ret);
4979 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4980 int count = 0;
4981 ret = upgrade_enable_all(zhp, &count);
4982 if (ret != 0)
4983 return (ret);
4985 if (count != 0) {
4986 printnl = B_TRUE;
4987 } else if (cur_version == SPA_VERSION) {
4988 (void) printf(gettext("Pool '%s' already has all "
4989 "supported features enabled.\n"),
4990 zpool_get_name(zhp));
4994 if (printnl) {
4995 (void) printf(gettext("\n"));
4998 return (0);
5002 * zpool upgrade
5003 * zpool upgrade -v
5004 * zpool upgrade [-V version] <-a | pool ...>
5006 * With no arguments, display downrev'd ZFS pool available for upgrade.
5007 * Individual pools can be upgraded by specifying the pool, and '-a' will
5008 * upgrade all pools.
5011 zpool_do_upgrade(int argc, char **argv)
5013 int c;
5014 upgrade_cbdata_t cb = { 0 };
5015 int ret = 0;
5016 boolean_t showversions = B_FALSE;
5017 boolean_t upgradeall = B_FALSE;
5018 char *end;
5021 /* check options */
5022 while ((c = getopt(argc, argv, ":avV:")) != -1) {
5023 switch (c) {
5024 case 'a':
5025 upgradeall = B_TRUE;
5026 break;
5027 case 'v':
5028 showversions = B_TRUE;
5029 break;
5030 case 'V':
5031 cb.cb_version = strtoll(optarg, &end, 10);
5032 if (*end != '\0' ||
5033 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
5034 (void) fprintf(stderr,
5035 gettext("invalid version '%s'\n"), optarg);
5036 usage(B_FALSE);
5038 break;
5039 case ':':
5040 (void) fprintf(stderr, gettext("missing argument for "
5041 "'%c' option\n"), optopt);
5042 usage(B_FALSE);
5043 break;
5044 case '?':
5045 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5046 optopt);
5047 usage(B_FALSE);
5051 cb.cb_argc = argc;
5052 cb.cb_argv = argv;
5053 argc -= optind;
5054 argv += optind;
5056 if (cb.cb_version == 0) {
5057 cb.cb_version = SPA_VERSION;
5058 } else if (!upgradeall && argc == 0) {
5059 (void) fprintf(stderr, gettext("-V option is "
5060 "incompatible with other arguments\n"));
5061 usage(B_FALSE);
5064 if (showversions) {
5065 if (upgradeall || argc != 0) {
5066 (void) fprintf(stderr, gettext("-v option is "
5067 "incompatible with other arguments\n"));
5068 usage(B_FALSE);
5070 } else if (upgradeall) {
5071 if (argc != 0) {
5072 (void) fprintf(stderr, gettext("-a option should not "
5073 "be used along with a pool name\n"));
5074 usage(B_FALSE);
5078 (void) printf(gettext("This system supports ZFS pool feature "
5079 "flags.\n\n"));
5080 if (showversions) {
5081 int i;
5083 (void) printf(gettext("The following features are "
5084 "supported:\n\n"));
5085 (void) printf(gettext("FEAT DESCRIPTION\n"));
5086 (void) printf("----------------------------------------------"
5087 "---------------\n");
5088 for (i = 0; i < SPA_FEATURES; i++) {
5089 zfeature_info_t *fi = &spa_feature_table[i];
5090 const char *ro = fi->fi_can_readonly ?
5091 " (read-only compatible)" : "";
5093 (void) printf("%-37s%s\n", fi->fi_uname, ro);
5094 (void) printf(" %s\n", fi->fi_desc);
5096 (void) printf("\n");
5098 (void) printf(gettext("The following legacy versions are also "
5099 "supported:\n\n"));
5100 (void) printf(gettext("VER DESCRIPTION\n"));
5101 (void) printf("--- -----------------------------------------"
5102 "---------------\n");
5103 (void) printf(gettext(" 1 Initial ZFS version\n"));
5104 (void) printf(gettext(" 2 Ditto blocks "
5105 "(replicated metadata)\n"));
5106 (void) printf(gettext(" 3 Hot spares and double parity "
5107 "RAID-Z\n"));
5108 (void) printf(gettext(" 4 zpool history\n"));
5109 (void) printf(gettext(" 5 Compression using the gzip "
5110 "algorithm\n"));
5111 (void) printf(gettext(" 6 bootfs pool property\n"));
5112 (void) printf(gettext(" 7 Separate intent log devices\n"));
5113 (void) printf(gettext(" 8 Delegated administration\n"));
5114 (void) printf(gettext(" 9 refquota and refreservation "
5115 "properties\n"));
5116 (void) printf(gettext(" 10 Cache devices\n"));
5117 (void) printf(gettext(" 11 Improved scrub performance\n"));
5118 (void) printf(gettext(" 12 Snapshot properties\n"));
5119 (void) printf(gettext(" 13 snapused property\n"));
5120 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
5121 (void) printf(gettext(" 15 user/group space accounting\n"));
5122 (void) printf(gettext(" 16 stmf property support\n"));
5123 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
5124 (void) printf(gettext(" 18 Snapshot user holds\n"));
5125 (void) printf(gettext(" 19 Log device removal\n"));
5126 (void) printf(gettext(" 20 Compression using zle "
5127 "(zero-length encoding)\n"));
5128 (void) printf(gettext(" 21 Deduplication\n"));
5129 (void) printf(gettext(" 22 Received properties\n"));
5130 (void) printf(gettext(" 23 Slim ZIL\n"));
5131 (void) printf(gettext(" 24 System attributes\n"));
5132 (void) printf(gettext(" 25 Improved scrub stats\n"));
5133 (void) printf(gettext(" 26 Improved snapshot deletion "
5134 "performance\n"));
5135 (void) printf(gettext(" 27 Improved snapshot creation "
5136 "performance\n"));
5137 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
5138 (void) printf(gettext("\nFor more information on a particular "
5139 "version, including supported releases,\n"));
5140 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
5141 } else if (argc == 0 && upgradeall) {
5142 cb.cb_first = B_TRUE;
5143 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
5144 if (ret == 0 && cb.cb_first) {
5145 if (cb.cb_version == SPA_VERSION) {
5146 (void) printf(gettext("All pools are already "
5147 "formatted using feature flags.\n\n"));
5148 (void) printf(gettext("Every feature flags "
5149 "pool already has all supported features "
5150 "enabled.\n"));
5151 } else {
5152 (void) printf(gettext("All pools are already "
5153 "formatted with version %llu or higher.\n"),
5154 (u_longlong_t) cb.cb_version);
5157 } else if (argc == 0) {
5158 cb.cb_first = B_TRUE;
5159 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
5160 assert(ret == 0);
5162 if (cb.cb_first) {
5163 (void) printf(gettext("All pools are formatted "
5164 "using feature flags.\n\n"));
5165 } else {
5166 (void) printf(gettext("\nUse 'zpool upgrade -v' "
5167 "for a list of available legacy versions.\n"));
5170 cb.cb_first = B_TRUE;
5171 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
5172 assert(ret == 0);
5174 if (cb.cb_first) {
5175 (void) printf(gettext("Every feature flags pool has "
5176 "all supported features enabled.\n"));
5177 } else {
5178 (void) printf(gettext("\n"));
5180 } else {
5181 ret = for_each_pool(argc, argv, B_FALSE, NULL,
5182 upgrade_one, &cb);
5185 return (ret);
5188 typedef struct hist_cbdata {
5189 boolean_t first;
5190 boolean_t longfmt;
5191 boolean_t internal;
5192 } hist_cbdata_t;
5195 * Print out the command history for a specific pool.
5197 static int
5198 get_history_one(zpool_handle_t *zhp, void *data)
5200 nvlist_t *nvhis;
5201 nvlist_t **records;
5202 uint_t numrecords;
5203 int ret, i;
5204 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5206 cb->first = B_FALSE;
5208 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5210 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5211 return (ret);
5213 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5214 &records, &numrecords) == 0);
5215 for (i = 0; i < numrecords; i++) {
5216 nvlist_t *rec = records[i];
5217 char tbuf[30] = "";
5219 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5220 time_t tsec;
5221 struct tm t;
5223 tsec = fnvlist_lookup_uint64(records[i],
5224 ZPOOL_HIST_TIME);
5225 (void) localtime_r(&tsec, &t);
5226 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5229 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5230 (void) printf("%s %s", tbuf,
5231 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5232 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5233 int ievent =
5234 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5235 if (!cb->internal)
5236 continue;
5237 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5238 (void) printf("%s unrecognized record:\n",
5239 tbuf);
5240 dump_nvlist(rec, 4);
5241 continue;
5243 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5244 zfs_history_event_names[ievent],
5245 (longlong_t) fnvlist_lookup_uint64(
5246 rec, ZPOOL_HIST_TXG),
5247 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5248 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5249 if (!cb->internal)
5250 continue;
5251 (void) printf("%s [txg:%lld] %s", tbuf,
5252 (longlong_t) fnvlist_lookup_uint64(
5253 rec, ZPOOL_HIST_TXG),
5254 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5255 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5256 (void) printf(" %s (%llu)",
5257 fnvlist_lookup_string(rec,
5258 ZPOOL_HIST_DSNAME),
5259 (u_longlong_t)fnvlist_lookup_uint64(rec,
5260 ZPOOL_HIST_DSID));
5262 (void) printf(" %s", fnvlist_lookup_string(rec,
5263 ZPOOL_HIST_INT_STR));
5264 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5265 if (!cb->internal)
5266 continue;
5267 (void) printf("%s ioctl %s\n", tbuf,
5268 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5269 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5270 (void) printf(" input:\n");
5271 dump_nvlist(fnvlist_lookup_nvlist(rec,
5272 ZPOOL_HIST_INPUT_NVL), 8);
5274 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5275 (void) printf(" output:\n");
5276 dump_nvlist(fnvlist_lookup_nvlist(rec,
5277 ZPOOL_HIST_OUTPUT_NVL), 8);
5279 } else {
5280 if (!cb->internal)
5281 continue;
5282 (void) printf("%s unrecognized record:\n", tbuf);
5283 dump_nvlist(rec, 4);
5286 if (!cb->longfmt) {
5287 (void) printf("\n");
5288 continue;
5290 (void) printf(" [");
5291 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5292 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5293 struct passwd *pwd = getpwuid(who);
5294 (void) printf("user %d ", (int)who);
5295 if (pwd != NULL)
5296 (void) printf("(%s) ", pwd->pw_name);
5298 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5299 (void) printf("on %s",
5300 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5302 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5303 (void) printf(":%s",
5304 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5307 (void) printf("]");
5308 (void) printf("\n");
5310 (void) printf("\n");
5311 nvlist_free(nvhis);
5313 return (ret);
5317 * zpool history <pool>
5319 * Displays the history of commands that modified pools.
5322 zpool_do_history(int argc, char **argv)
5324 hist_cbdata_t cbdata = { 0 };
5325 int ret;
5326 int c;
5328 cbdata.first = B_TRUE;
5329 /* check options */
5330 while ((c = getopt(argc, argv, "li")) != -1) {
5331 switch (c) {
5332 case 'l':
5333 cbdata.longfmt = B_TRUE;
5334 break;
5335 case 'i':
5336 cbdata.internal = B_TRUE;
5337 break;
5338 case '?':
5339 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5340 optopt);
5341 usage(B_FALSE);
5344 argc -= optind;
5345 argv += optind;
5347 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5348 &cbdata);
5350 if (argc == 0 && cbdata.first == B_TRUE) {
5351 (void) fprintf(stderr, gettext("no pools available\n"));
5352 return (0);
5355 return (ret);
5358 typedef struct ev_opts {
5359 int verbose;
5360 int scripted;
5361 int follow;
5362 int clear;
5363 } ev_opts_t;
5365 static void
5366 zpool_do_events_short(nvlist_t *nvl)
5368 char ctime_str[26], str[32], *ptr;
5369 int64_t *tv;
5370 uint_t n;
5372 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5373 memset(str, ' ', 32);
5374 (void) ctime_r((const time_t *)&tv[0], ctime_str);
5375 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5376 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5377 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5378 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
5379 (void) printf(gettext("%s "), str);
5381 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5382 (void) printf(gettext("%s\n"), ptr);
5385 static void
5386 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5388 nvpair_t *nvp;
5390 for (nvp = nvlist_next_nvpair(nvl, NULL);
5391 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5393 data_type_t type = nvpair_type(nvp);
5394 const char *name = nvpair_name(nvp);
5396 boolean_t b;
5397 uint8_t i8;
5398 uint16_t i16;
5399 uint32_t i32;
5400 uint64_t i64;
5401 char *str;
5402 nvlist_t *cnv;
5404 printf(gettext("%*s%s = "), depth, "", name);
5406 switch (type) {
5407 case DATA_TYPE_BOOLEAN:
5408 printf(gettext("%s"), "1");
5409 break;
5411 case DATA_TYPE_BOOLEAN_VALUE:
5412 (void) nvpair_value_boolean_value(nvp, &b);
5413 printf(gettext("%s"), b ? "1" : "0");
5414 break;
5416 case DATA_TYPE_BYTE:
5417 (void) nvpair_value_byte(nvp, &i8);
5418 printf(gettext("0x%x"), i8);
5419 break;
5421 case DATA_TYPE_INT8:
5422 (void) nvpair_value_int8(nvp, (void *)&i8);
5423 printf(gettext("0x%x"), i8);
5424 break;
5426 case DATA_TYPE_UINT8:
5427 (void) nvpair_value_uint8(nvp, &i8);
5428 printf(gettext("0x%x"), i8);
5429 break;
5431 case DATA_TYPE_INT16:
5432 (void) nvpair_value_int16(nvp, (void *)&i16);
5433 printf(gettext("0x%x"), i16);
5434 break;
5436 case DATA_TYPE_UINT16:
5437 (void) nvpair_value_uint16(nvp, &i16);
5438 printf(gettext("0x%x"), i16);
5439 break;
5441 case DATA_TYPE_INT32:
5442 (void) nvpair_value_int32(nvp, (void *)&i32);
5443 printf(gettext("0x%x"), i32);
5444 break;
5446 case DATA_TYPE_UINT32:
5447 (void) nvpair_value_uint32(nvp, &i32);
5448 printf(gettext("0x%x"), i32);
5449 break;
5451 case DATA_TYPE_INT64:
5452 (void) nvpair_value_int64(nvp, (void *)&i64);
5453 printf(gettext("0x%llx"), (u_longlong_t)i64);
5454 break;
5456 case DATA_TYPE_UINT64:
5457 (void) nvpair_value_uint64(nvp, &i64);
5458 printf(gettext("0x%llx"), (u_longlong_t)i64);
5459 break;
5461 case DATA_TYPE_HRTIME:
5462 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5463 printf(gettext("0x%llx"), (u_longlong_t)i64);
5464 break;
5466 case DATA_TYPE_STRING:
5467 (void) nvpair_value_string(nvp, &str);
5468 printf(gettext("\"%s\""), str ? str : "<NULL>");
5469 break;
5471 case DATA_TYPE_NVLIST:
5472 printf(gettext("(embedded nvlist)\n"));
5473 (void) nvpair_value_nvlist(nvp, &cnv);
5474 zpool_do_events_nvprint(cnv, depth + 8);
5475 printf(gettext("%*s(end %s)"), depth, "", name);
5476 break;
5478 case DATA_TYPE_NVLIST_ARRAY: {
5479 nvlist_t **val;
5480 uint_t i, nelem;
5482 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5483 printf(gettext("(%d embedded nvlists)\n"), nelem);
5484 for (i = 0; i < nelem; i++) {
5485 printf(gettext("%*s%s[%d] = %s\n"),
5486 depth, "", name, i, "(embedded nvlist)");
5487 zpool_do_events_nvprint(val[i], depth + 8);
5488 printf(gettext("%*s(end %s[%i])\n"),
5489 depth, "", name, i);
5491 printf(gettext("%*s(end %s)\n"), depth, "", name);
5493 break;
5495 case DATA_TYPE_INT8_ARRAY: {
5496 int8_t *val;
5497 uint_t i, nelem;
5499 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5500 for (i = 0; i < nelem; i++)
5501 printf(gettext("0x%x "), val[i]);
5503 break;
5506 case DATA_TYPE_UINT8_ARRAY: {
5507 uint8_t *val;
5508 uint_t i, nelem;
5510 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5511 for (i = 0; i < nelem; i++)
5512 printf(gettext("0x%x "), val[i]);
5514 break;
5517 case DATA_TYPE_INT16_ARRAY: {
5518 int16_t *val;
5519 uint_t i, nelem;
5521 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5522 for (i = 0; i < nelem; i++)
5523 printf(gettext("0x%x "), val[i]);
5525 break;
5528 case DATA_TYPE_UINT16_ARRAY: {
5529 uint16_t *val;
5530 uint_t i, nelem;
5532 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5533 for (i = 0; i < nelem; i++)
5534 printf(gettext("0x%x "), val[i]);
5536 break;
5539 case DATA_TYPE_INT32_ARRAY: {
5540 int32_t *val;
5541 uint_t i, nelem;
5543 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5544 for (i = 0; i < nelem; i++)
5545 printf(gettext("0x%x "), val[i]);
5547 break;
5550 case DATA_TYPE_UINT32_ARRAY: {
5551 uint32_t *val;
5552 uint_t i, nelem;
5554 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5555 for (i = 0; i < nelem; i++)
5556 printf(gettext("0x%x "), val[i]);
5558 break;
5561 case DATA_TYPE_INT64_ARRAY: {
5562 int64_t *val;
5563 uint_t i, nelem;
5565 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5566 for (i = 0; i < nelem; i++)
5567 printf(gettext("0x%llx "),
5568 (u_longlong_t)val[i]);
5570 break;
5573 case DATA_TYPE_UINT64_ARRAY: {
5574 uint64_t *val;
5575 uint_t i, nelem;
5577 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5578 for (i = 0; i < nelem; i++)
5579 printf(gettext("0x%llx "),
5580 (u_longlong_t)val[i]);
5582 break;
5585 case DATA_TYPE_STRING_ARRAY: {
5586 char **str;
5587 uint_t i, nelem;
5589 (void) nvpair_value_string_array(nvp, &str, &nelem);
5590 for (i = 0; i < nelem; i++)
5591 printf(gettext("\"%s\" "),
5592 str[i] ? str[i] : "<NULL>");
5594 break;
5597 case DATA_TYPE_BOOLEAN_ARRAY:
5598 case DATA_TYPE_BYTE_ARRAY:
5599 case DATA_TYPE_DOUBLE:
5600 case DATA_TYPE_UNKNOWN:
5601 printf(gettext("<unknown>"));
5602 break;
5605 printf(gettext("\n"));
5609 static int
5610 zpool_do_events_next(ev_opts_t *opts)
5612 nvlist_t *nvl;
5613 int zevent_fd, ret, dropped;
5615 zevent_fd = open(ZFS_DEV, O_RDWR);
5616 VERIFY(zevent_fd >= 0);
5618 if (!opts->scripted)
5619 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5621 while (1) {
5622 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5623 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
5624 if (ret || nvl == NULL)
5625 break;
5627 if (dropped > 0)
5628 (void) printf(gettext("dropped %d events\n"), dropped);
5630 zpool_do_events_short(nvl);
5632 if (opts->verbose) {
5633 zpool_do_events_nvprint(nvl, 8);
5634 printf(gettext("\n"));
5636 (void) fflush(stdout);
5638 nvlist_free(nvl);
5641 VERIFY(0 == close(zevent_fd));
5643 return (ret);
5646 static int
5647 zpool_do_events_clear(ev_opts_t *opts)
5649 int count, ret;
5651 ret = zpool_events_clear(g_zfs, &count);
5652 if (!ret)
5653 (void) printf(gettext("cleared %d events\n"), count);
5655 return (ret);
5659 * zpool events [-vfc]
5661 * Displays events logs by ZFS.
5664 zpool_do_events(int argc, char **argv)
5666 ev_opts_t opts = { 0 };
5667 int ret;
5668 int c;
5670 /* check options */
5671 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5672 switch (c) {
5673 case 'v':
5674 opts.verbose = 1;
5675 break;
5676 case 'H':
5677 opts.scripted = 1;
5678 break;
5679 case 'f':
5680 opts.follow = 1;
5681 break;
5682 case 'c':
5683 opts.clear = 1;
5684 break;
5685 case '?':
5686 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5687 optopt);
5688 usage(B_FALSE);
5691 argc -= optind;
5692 argv += optind;
5694 if (opts.clear)
5695 ret = zpool_do_events_clear(&opts);
5696 else
5697 ret = zpool_do_events_next(&opts);
5699 return (ret);
5702 static int
5703 get_callback(zpool_handle_t *zhp, void *data)
5705 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5706 char value[MAXNAMELEN];
5707 zprop_source_t srctype;
5708 zprop_list_t *pl;
5710 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5713 * Skip the special fake placeholder. This will also skip
5714 * over the name property when 'all' is specified.
5716 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5717 pl == cbp->cb_proplist)
5718 continue;
5720 if (pl->pl_prop == ZPROP_INVAL &&
5721 (zpool_prop_feature(pl->pl_user_prop) ||
5722 zpool_prop_unsupported(pl->pl_user_prop))) {
5723 srctype = ZPROP_SRC_LOCAL;
5725 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5726 value, sizeof (value)) == 0) {
5727 zprop_print_one_property(zpool_get_name(zhp),
5728 cbp, pl->pl_user_prop, value, srctype,
5729 NULL, NULL);
5731 } else {
5732 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5733 sizeof (value), &srctype, cbp->cb_literal) != 0)
5734 continue;
5736 zprop_print_one_property(zpool_get_name(zhp), cbp,
5737 zpool_prop_to_name(pl->pl_prop), value, srctype,
5738 NULL, NULL);
5741 return (0);
5745 zpool_do_get(int argc, char **argv)
5747 zprop_get_cbdata_t cb = { 0 };
5748 zprop_list_t fake_name = { 0 };
5749 int c, ret;
5751 /* check options */
5752 while ((c = getopt(argc, argv, "pH")) != -1) {
5753 switch (c) {
5754 case 'p':
5755 cb.cb_literal = B_TRUE;
5756 break;
5758 case 'H':
5759 cb.cb_scripted = B_TRUE;
5760 break;
5762 case '?':
5763 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5764 optopt);
5765 usage(B_FALSE);
5769 argc -= optind;
5770 argv += optind;
5772 if (argc < 1) {
5773 (void) fprintf(stderr, gettext("missing property "
5774 "argument\n"));
5775 usage(B_FALSE);
5778 cb.cb_first = B_TRUE;
5779 cb.cb_sources = ZPROP_SRC_ALL;
5780 cb.cb_columns[0] = GET_COL_NAME;
5781 cb.cb_columns[1] = GET_COL_PROPERTY;
5782 cb.cb_columns[2] = GET_COL_VALUE;
5783 cb.cb_columns[3] = GET_COL_SOURCE;
5784 cb.cb_type = ZFS_TYPE_POOL;
5786 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5787 usage(B_FALSE);
5789 argc--;
5790 argv++;
5792 if (cb.cb_proplist != NULL) {
5793 fake_name.pl_prop = ZPOOL_PROP_NAME;
5794 fake_name.pl_width = strlen(gettext("NAME"));
5795 fake_name.pl_next = cb.cb_proplist;
5796 cb.cb_proplist = &fake_name;
5799 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5800 get_callback, &cb);
5802 if (cb.cb_proplist == &fake_name)
5803 zprop_free_list(fake_name.pl_next);
5804 else
5805 zprop_free_list(cb.cb_proplist);
5807 return (ret);
5810 typedef struct set_cbdata {
5811 char *cb_propname;
5812 char *cb_value;
5813 boolean_t cb_any_successful;
5814 } set_cbdata_t;
5817 set_callback(zpool_handle_t *zhp, void *data)
5819 int error;
5820 set_cbdata_t *cb = (set_cbdata_t *)data;
5822 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5824 if (!error)
5825 cb->cb_any_successful = B_TRUE;
5827 return (error);
5831 zpool_do_set(int argc, char **argv)
5833 set_cbdata_t cb = { 0 };
5834 int error;
5836 if (argc > 1 && argv[1][0] == '-') {
5837 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5838 argv[1][1]);
5839 usage(B_FALSE);
5842 if (argc < 2) {
5843 (void) fprintf(stderr, gettext("missing property=value "
5844 "argument\n"));
5845 usage(B_FALSE);
5848 if (argc < 3) {
5849 (void) fprintf(stderr, gettext("missing pool name\n"));
5850 usage(B_FALSE);
5853 if (argc > 3) {
5854 (void) fprintf(stderr, gettext("too many pool names\n"));
5855 usage(B_FALSE);
5858 cb.cb_propname = argv[1];
5859 cb.cb_value = strchr(cb.cb_propname, '=');
5860 if (cb.cb_value == NULL) {
5861 (void) fprintf(stderr, gettext("missing value in "
5862 "property=value argument\n"));
5863 usage(B_FALSE);
5866 *(cb.cb_value) = '\0';
5867 cb.cb_value++;
5869 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5870 set_callback, &cb);
5872 return (error);
5875 static int
5876 find_command_idx(char *command, int *idx)
5878 int i;
5880 for (i = 0; i < NCOMMAND; i++) {
5881 if (command_table[i].name == NULL)
5882 continue;
5884 if (strcmp(command, command_table[i].name) == 0) {
5885 *idx = i;
5886 return (0);
5889 return (1);
5893 main(int argc, char **argv)
5895 int ret;
5896 int i = 0;
5897 char *cmdname;
5899 (void) setlocale(LC_ALL, "");
5900 (void) textdomain(TEXT_DOMAIN);
5902 dprintf_setup(&argc, argv);
5904 opterr = 0;
5907 * Make sure the user has specified some command.
5909 if (argc < 2) {
5910 (void) fprintf(stderr, gettext("missing command\n"));
5911 usage(B_FALSE);
5914 cmdname = argv[1];
5917 * Special case '-?'
5919 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
5920 usage(B_TRUE);
5922 if ((g_zfs = libzfs_init()) == NULL)
5923 return (1);
5925 libzfs_print_on_error(g_zfs, B_TRUE);
5927 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5930 * Run the appropriate command.
5932 if (find_command_idx(cmdname, &i) == 0) {
5933 current_command = &command_table[i];
5934 ret = command_table[i].func(argc - 1, argv + 1);
5935 } else if (strchr(cmdname, '=')) {
5936 verify(find_command_idx("set", &i) == 0);
5937 current_command = &command_table[i];
5938 ret = command_table[i].func(argc, argv);
5939 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5941 * 'freeze' is a vile debugging abomination, so we treat
5942 * it as such.
5944 char buf[16384];
5945 int fd = open(ZFS_DEV, O_RDWR);
5946 (void) strcpy((void *)buf, argv[2]);
5947 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5948 } else {
5949 (void) fprintf(stderr, gettext("unrecognized "
5950 "command '%s'\n"), cmdname);
5951 usage(B_FALSE);
5952 ret = 1;
5955 if (ret == 0 && log_history)
5956 (void) zpool_log_history(g_zfs, history_str);
5958 libzfs_fini(g_zfs);
5961 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5962 * for the purposes of running ::findleaks.
5964 if (getenv("ZFS_ABORT") != NULL) {
5965 (void) printf("dumping core by request\n");
5966 abort();
5969 return (ret);