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