]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/zpool/zpool_main.c
Illumos #3745, #3811
[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 int reason;
1612 const char *health;
1613 uint_t vsc;
1614 int namewidth;
1615 char *comment;
1616
1617 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1618 &name) == 0);
1619 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1620 &guid) == 0);
1621 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1622 &pool_state) == 0);
1623 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1624 &nvroot) == 0);
1625
1626 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
1627 (uint64_t **)&vs, &vsc) == 0);
1628 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
1629
1630 reason = zpool_import_status(config, &msgid);
1631
1632 (void) printf(gettext(" pool: %s\n"), name);
1633 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
1634 (void) printf(gettext(" state: %s"), health);
1635 if (pool_state == POOL_STATE_DESTROYED)
1636 (void) printf(gettext(" (DESTROYED)"));
1637 (void) printf("\n");
1638
1639 switch (reason) {
1640 case ZPOOL_STATUS_MISSING_DEV_R:
1641 case ZPOOL_STATUS_MISSING_DEV_NR:
1642 case ZPOOL_STATUS_BAD_GUID_SUM:
1643 (void) printf(gettext(" status: One or more devices are "
1644 "missing from the system.\n"));
1645 break;
1646
1647 case ZPOOL_STATUS_CORRUPT_LABEL_R:
1648 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
1649 (void) printf(gettext(" status: One or more devices contains "
1650 "corrupted data.\n"));
1651 break;
1652
1653 case ZPOOL_STATUS_CORRUPT_DATA:
1654 (void) printf(
1655 gettext(" status: The pool data is corrupted.\n"));
1656 break;
1657
1658 case ZPOOL_STATUS_OFFLINE_DEV:
1659 (void) printf(gettext(" status: One or more devices "
1660 "are offlined.\n"));
1661 break;
1662
1663 case ZPOOL_STATUS_CORRUPT_POOL:
1664 (void) printf(gettext(" status: The pool metadata is "
1665 "corrupted.\n"));
1666 break;
1667
1668 case ZPOOL_STATUS_VERSION_OLDER:
1669 (void) printf(gettext(" status: The pool is formatted using a "
1670 "legacy on-disk version.\n"));
1671 break;
1672
1673 case ZPOOL_STATUS_VERSION_NEWER:
1674 (void) printf(gettext(" status: The pool is formatted using an "
1675 "incompatible version.\n"));
1676 break;
1677
1678 case ZPOOL_STATUS_FEAT_DISABLED:
1679 (void) printf(gettext(" status: Some supported features are "
1680 "not enabled on the pool.\n"));
1681 break;
1682
1683 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1684 (void) printf(gettext("status: The pool uses the following "
1685 "feature(s) not supported on this sytem:\n"));
1686 zpool_print_unsup_feat(config);
1687 break;
1688
1689 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1690 (void) printf(gettext("status: The pool can only be accessed "
1691 "in read-only mode on this system. It\n\tcannot be "
1692 "accessed in read-write mode because it uses the "
1693 "following\n\tfeature(s) not supported on this system:\n"));
1694 zpool_print_unsup_feat(config);
1695 break;
1696
1697 case ZPOOL_STATUS_HOSTID_MISMATCH:
1698 (void) printf(gettext(" status: The pool was last accessed by "
1699 "another system.\n"));
1700 break;
1701
1702 case ZPOOL_STATUS_FAULTED_DEV_R:
1703 case ZPOOL_STATUS_FAULTED_DEV_NR:
1704 (void) printf(gettext(" status: One or more devices are "
1705 "faulted.\n"));
1706 break;
1707
1708 case ZPOOL_STATUS_BAD_LOG:
1709 (void) printf(gettext(" status: An intent log record cannot be "
1710 "read.\n"));
1711 break;
1712
1713 case ZPOOL_STATUS_RESILVERING:
1714 (void) printf(gettext(" status: One or more devices were being "
1715 "resilvered.\n"));
1716 break;
1717
1718 default:
1719 /*
1720 * No other status can be seen when importing pools.
1721 */
1722 assert(reason == ZPOOL_STATUS_OK);
1723 }
1724
1725 /*
1726 * Print out an action according to the overall state of the pool.
1727 */
1728 if (vs->vs_state == VDEV_STATE_HEALTHY) {
1729 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
1730 reason == ZPOOL_STATUS_FEAT_DISABLED) {
1731 (void) printf(gettext(" action: The pool can be "
1732 "imported using its name or numeric identifier, "
1733 "though\n\tsome features will not be available "
1734 "without an explicit 'zpool upgrade'.\n"));
1735 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
1736 (void) printf(gettext(" action: The pool can be "
1737 "imported using its name or numeric "
1738 "identifier and\n\tthe '-f' flag.\n"));
1739 } else {
1740 (void) printf(gettext(" action: The pool can be "
1741 "imported using its name or numeric "
1742 "identifier.\n"));
1743 }
1744 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
1745 (void) printf(gettext(" action: The pool can be imported "
1746 "despite missing or damaged devices. The\n\tfault "
1747 "tolerance of the pool may be compromised if imported.\n"));
1748 } else {
1749 switch (reason) {
1750 case ZPOOL_STATUS_VERSION_NEWER:
1751 (void) printf(gettext(" action: The pool cannot be "
1752 "imported. Access the pool on a system running "
1753 "newer\n\tsoftware, or recreate the pool from "
1754 "backup.\n"));
1755 break;
1756 case ZPOOL_STATUS_UNSUP_FEAT_READ:
1757 (void) printf(gettext("action: The pool cannot be "
1758 "imported. Access the pool on a system that "
1759 "supports\n\tthe required feature(s), or recreate "
1760 "the pool from backup.\n"));
1761 break;
1762 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
1763 (void) printf(gettext("action: The pool cannot be "
1764 "imported in read-write mode. Import the pool "
1765 "with\n"
1766 "\t\"-o readonly=on\", access the pool on a system "
1767 "that supports the\n\trequired feature(s), or "
1768 "recreate the pool from backup.\n"));
1769 break;
1770 case ZPOOL_STATUS_MISSING_DEV_R:
1771 case ZPOOL_STATUS_MISSING_DEV_NR:
1772 case ZPOOL_STATUS_BAD_GUID_SUM:
1773 (void) printf(gettext(" action: The pool cannot be "
1774 "imported. Attach the missing\n\tdevices and try "
1775 "again.\n"));
1776 break;
1777 default:
1778 (void) printf(gettext(" action: The pool cannot be "
1779 "imported due to damaged devices or data.\n"));
1780 }
1781 }
1782
1783 /* Print the comment attached to the pool. */
1784 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1785 (void) printf(gettext("comment: %s\n"), comment);
1786
1787 /*
1788 * If the state is "closed" or "can't open", and the aux state
1789 * is "corrupt data":
1790 */
1791 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
1792 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
1793 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
1794 if (pool_state == POOL_STATE_DESTROYED)
1795 (void) printf(gettext("\tThe pool was destroyed, "
1796 "but can be imported using the '-Df' flags.\n"));
1797 else if (pool_state != POOL_STATE_EXPORTED)
1798 (void) printf(gettext("\tThe pool may be active on "
1799 "another system, but can be imported using\n\t"
1800 "the '-f' flag.\n"));
1801 }
1802
1803 if (msgid != NULL)
1804 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
1805 msgid);
1806
1807 (void) printf(gettext(" config:\n\n"));
1808
1809 namewidth = max_width(NULL, nvroot, 0, 0);
1810 if (namewidth < 10)
1811 namewidth = 10;
1812
1813 print_import_config(name, nvroot, namewidth, 0);
1814 if (num_logs(nvroot) > 0)
1815 print_logs(NULL, nvroot, namewidth, B_FALSE);
1816
1817 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
1818 (void) printf(gettext("\n\tAdditional devices are known to "
1819 "be part of this pool, though their\n\texact "
1820 "configuration cannot be determined.\n"));
1821 }
1822 }
1823
1824 /*
1825 * Perform the import for the given configuration. This passes the heavy
1826 * lifting off to zpool_import_props(), and then mounts the datasets contained
1827 * within the pool.
1828 */
1829 static int
1830 do_import(nvlist_t *config, const char *newname, const char *mntopts,
1831 nvlist_t *props, int flags)
1832 {
1833 zpool_handle_t *zhp;
1834 char *name;
1835 uint64_t state;
1836 uint64_t version;
1837
1838 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1839 &name) == 0);
1840
1841 verify(nvlist_lookup_uint64(config,
1842 ZPOOL_CONFIG_POOL_STATE, &state) == 0);
1843 verify(nvlist_lookup_uint64(config,
1844 ZPOOL_CONFIG_VERSION, &version) == 0);
1845 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1846 (void) fprintf(stderr, gettext("cannot import '%s': pool "
1847 "is formatted using an unsupported ZFS version\n"), name);
1848 return (1);
1849 } else if (state != POOL_STATE_EXPORTED &&
1850 !(flags & ZFS_IMPORT_ANY_HOST)) {
1851 uint64_t hostid;
1852
1853 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
1854 &hostid) == 0) {
1855 unsigned long system_hostid = gethostid() & 0xffffffff;
1856
1857 if ((unsigned long)hostid != system_hostid) {
1858 char *hostname;
1859 uint64_t timestamp;
1860 time_t t;
1861
1862 verify(nvlist_lookup_string(config,
1863 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1864 verify(nvlist_lookup_uint64(config,
1865 ZPOOL_CONFIG_TIMESTAMP, &timestamp) == 0);
1866 t = timestamp;
1867 (void) fprintf(stderr, gettext("cannot import "
1868 "'%s': pool may be in use from other "
1869 "system, it was last accessed by %s "
1870 "(hostid: 0x%lx) on %s"), name, hostname,
1871 (unsigned long)hostid,
1872 asctime(localtime(&t)));
1873 (void) fprintf(stderr, gettext("use '-f' to "
1874 "import anyway\n"));
1875 return (1);
1876 }
1877 } else {
1878 (void) fprintf(stderr, gettext("cannot import '%s': "
1879 "pool may be in use from other system\n"), name);
1880 (void) fprintf(stderr, gettext("use '-f' to import "
1881 "anyway\n"));
1882 return (1);
1883 }
1884 }
1885
1886 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
1887 return (1);
1888
1889 if (newname != NULL)
1890 name = (char *)newname;
1891
1892 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
1893 return (1);
1894
1895 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
1896 !(flags & ZFS_IMPORT_ONLY) &&
1897 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
1898 zpool_close(zhp);
1899 return (1);
1900 }
1901
1902 zpool_close(zhp);
1903 return (0);
1904 }
1905
1906 /*
1907 * zpool import [-d dir] [-D]
1908 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1909 * [-d dir | -c cachefile] [-f] -a
1910 * import [-o mntopts] [-o prop=value] ... [-R root] [-D]
1911 * [-d dir | -c cachefile] [-f] [-n] [-F] <pool | id> [newpool]
1912 *
1913 * -c Read pool information from a cachefile instead of searching
1914 * devices.
1915 *
1916 * -d Scan in a specific directory, other than /dev/. More than
1917 * one directory can be specified using multiple '-d' options.
1918 *
1919 * -D Scan for previously destroyed pools or import all or only
1920 * specified destroyed pools.
1921 *
1922 * -R Temporarily import the pool, with all mountpoints relative to
1923 * the given root. The pool will remain exported when the machine
1924 * is rebooted.
1925 *
1926 * -V Import even in the presence of faulted vdevs. This is an
1927 * intentionally undocumented option for testing purposes, and
1928 * treats the pool configuration as complete, leaving any bad
1929 * vdevs in the FAULTED state. In other words, it does verbatim
1930 * import.
1931 *
1932 * -f Force import, even if it appears that the pool is active.
1933 *
1934 * -F Attempt rewind if necessary.
1935 *
1936 * -n See if rewind would work, but don't actually rewind.
1937 *
1938 * -N Import the pool but don't mount datasets.
1939 *
1940 * -T Specify a starting txg to use for import. This option is
1941 * intentionally undocumented option for testing purposes.
1942 *
1943 * -a Import all pools found.
1944 *
1945 * -o Set property=value and/or temporary mount options (without '=').
1946 *
1947 * The import command scans for pools to import, and import pools based on pool
1948 * name and GUID. The pool can also be renamed as part of the import process.
1949 */
1950 int
1951 zpool_do_import(int argc, char **argv)
1952 {
1953 char **searchdirs = NULL;
1954 char *env, *envdup = NULL;
1955 int nsearch = 0;
1956 int c;
1957 int err = 0;
1958 nvlist_t *pools = NULL;
1959 boolean_t do_all = B_FALSE;
1960 boolean_t do_destroyed = B_FALSE;
1961 char *mntopts = NULL;
1962 nvpair_t *elem;
1963 nvlist_t *config;
1964 uint64_t searchguid = 0;
1965 char *searchname = NULL;
1966 char *propval;
1967 nvlist_t *found_config;
1968 nvlist_t *policy = NULL;
1969 nvlist_t *props = NULL;
1970 boolean_t first;
1971 int flags = ZFS_IMPORT_NORMAL;
1972 uint32_t rewind_policy = ZPOOL_NO_REWIND;
1973 boolean_t dryrun = B_FALSE;
1974 boolean_t do_rewind = B_FALSE;
1975 boolean_t xtreme_rewind = B_FALSE;
1976 uint64_t pool_state, txg = -1ULL;
1977 char *cachefile = NULL;
1978 importargs_t idata = { 0 };
1979 char *endptr;
1980
1981 /* check options */
1982 while ((c = getopt(argc, argv, ":aCc:d:DEfFmnNo:rR:T:VX")) != -1) {
1983 switch (c) {
1984 case 'a':
1985 do_all = B_TRUE;
1986 break;
1987 case 'c':
1988 cachefile = optarg;
1989 break;
1990 case 'd':
1991 if (searchdirs == NULL) {
1992 searchdirs = safe_malloc(sizeof (char *));
1993 } else {
1994 char **tmp = safe_malloc((nsearch + 1) *
1995 sizeof (char *));
1996 bcopy(searchdirs, tmp, nsearch *
1997 sizeof (char *));
1998 free(searchdirs);
1999 searchdirs = tmp;
2000 }
2001 searchdirs[nsearch++] = optarg;
2002 break;
2003 case 'D':
2004 do_destroyed = B_TRUE;
2005 break;
2006 case 'f':
2007 flags |= ZFS_IMPORT_ANY_HOST;
2008 break;
2009 case 'F':
2010 do_rewind = B_TRUE;
2011 break;
2012 case 'm':
2013 flags |= ZFS_IMPORT_MISSING_LOG;
2014 break;
2015 case 'n':
2016 dryrun = B_TRUE;
2017 break;
2018 case 'N':
2019 flags |= ZFS_IMPORT_ONLY;
2020 break;
2021 case 'o':
2022 if ((propval = strchr(optarg, '=')) != NULL) {
2023 *propval = '\0';
2024 propval++;
2025 if (add_prop_list(optarg, propval,
2026 &props, B_TRUE))
2027 goto error;
2028 } else {
2029 mntopts = optarg;
2030 }
2031 break;
2032 case 'R':
2033 if (add_prop_list(zpool_prop_to_name(
2034 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2035 goto error;
2036 if (nvlist_lookup_string(props,
2037 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
2038 &propval) == 0)
2039 break;
2040 if (add_prop_list(zpool_prop_to_name(
2041 ZPOOL_PROP_CACHEFILE), "none", &props, B_TRUE))
2042 goto error;
2043 break;
2044 case 'T':
2045 errno = 0;
2046 txg = strtoull(optarg, &endptr, 10);
2047 if (errno != 0 || *endptr != '\0') {
2048 (void) fprintf(stderr,
2049 gettext("invalid txg value\n"));
2050 usage(B_FALSE);
2051 }
2052 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
2053 break;
2054 case 'V':
2055 flags |= ZFS_IMPORT_VERBATIM;
2056 break;
2057 case 'X':
2058 xtreme_rewind = B_TRUE;
2059 break;
2060 case ':':
2061 (void) fprintf(stderr, gettext("missing argument for "
2062 "'%c' option\n"), optopt);
2063 usage(B_FALSE);
2064 break;
2065 case '?':
2066 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2067 optopt);
2068 usage(B_FALSE);
2069 }
2070 }
2071
2072 argc -= optind;
2073 argv += optind;
2074
2075 if (cachefile && nsearch != 0) {
2076 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
2077 usage(B_FALSE);
2078 }
2079
2080 if ((dryrun || xtreme_rewind) && !do_rewind) {
2081 (void) fprintf(stderr,
2082 gettext("-n or -X only meaningful with -F\n"));
2083 usage(B_FALSE);
2084 }
2085 if (dryrun)
2086 rewind_policy = ZPOOL_TRY_REWIND;
2087 else if (do_rewind)
2088 rewind_policy = ZPOOL_DO_REWIND;
2089 if (xtreme_rewind)
2090 rewind_policy |= ZPOOL_EXTREME_REWIND;
2091
2092 /* In the future, we can capture further policy and include it here */
2093 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
2094 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, txg) != 0 ||
2095 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
2096 goto error;
2097
2098 /* check argument count */
2099 if (do_all) {
2100 if (argc != 0) {
2101 (void) fprintf(stderr, gettext("too many arguments\n"));
2102 usage(B_FALSE);
2103 }
2104 } else {
2105 if (argc > 2) {
2106 (void) fprintf(stderr, gettext("too many arguments\n"));
2107 usage(B_FALSE);
2108 }
2109
2110 /*
2111 * Check for the SYS_CONFIG privilege. We do this explicitly
2112 * here because otherwise any attempt to discover pools will
2113 * silently fail.
2114 */
2115 if (argc == 0 && !priv_ineffect(PRIV_SYS_CONFIG)) {
2116 (void) fprintf(stderr, gettext("cannot "
2117 "discover pools: permission denied\n"));
2118 if (searchdirs != NULL)
2119 free(searchdirs);
2120
2121 nvlist_free(policy);
2122 return (1);
2123 }
2124 }
2125
2126 /*
2127 * Depending on the arguments given, we do one of the following:
2128 *
2129 * <none> Iterate through all pools and display information about
2130 * each one.
2131 *
2132 * -a Iterate through all pools and try to import each one.
2133 *
2134 * <id> Find the pool that corresponds to the given GUID/pool
2135 * name and import that one.
2136 *
2137 * -D Above options applies only to destroyed pools.
2138 */
2139 if (argc != 0) {
2140 char *endptr;
2141
2142 errno = 0;
2143 searchguid = strtoull(argv[0], &endptr, 10);
2144 if (errno != 0 || *endptr != '\0')
2145 searchname = argv[0];
2146 found_config = NULL;
2147
2148 /*
2149 * User specified a name or guid. Ensure it's unique.
2150 */
2151 idata.unique = B_TRUE;
2152 }
2153
2154 /*
2155 * Check the environment for the preferred search path.
2156 */
2157 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
2158 char *dir;
2159
2160 envdup = strdup(env);
2161
2162 dir = strtok(envdup, ":");
2163 while (dir != NULL) {
2164 if (searchdirs == NULL) {
2165 searchdirs = safe_malloc(sizeof (char *));
2166 } else {
2167 char **tmp = safe_malloc((nsearch + 1) *
2168 sizeof (char *));
2169 bcopy(searchdirs, tmp, nsearch *
2170 sizeof (char *));
2171 free(searchdirs);
2172 searchdirs = tmp;
2173 }
2174 searchdirs[nsearch++] = dir;
2175 dir = strtok(NULL, ":");
2176 }
2177 }
2178
2179 idata.path = searchdirs;
2180 idata.paths = nsearch;
2181 idata.poolname = searchname;
2182 idata.guid = searchguid;
2183 idata.cachefile = cachefile;
2184
2185 pools = zpool_search_import(g_zfs, &idata);
2186
2187 if (pools != NULL && idata.exists &&
2188 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
2189 (void) fprintf(stderr, gettext("cannot import '%s': "
2190 "a pool with that name already exists\n"),
2191 argv[0]);
2192 (void) fprintf(stderr, gettext("use the form '%s "
2193 "<pool | id> <newpool>' to give it a new name\n"),
2194 "zpool import");
2195 err = 1;
2196 } else if (pools == NULL && idata.exists) {
2197 (void) fprintf(stderr, gettext("cannot import '%s': "
2198 "a pool with that name is already created/imported,\n"),
2199 argv[0]);
2200 (void) fprintf(stderr, gettext("and no additional pools "
2201 "with that name were found\n"));
2202 err = 1;
2203 } else if (pools == NULL) {
2204 if (argc != 0) {
2205 (void) fprintf(stderr, gettext("cannot import '%s': "
2206 "no such pool available\n"), argv[0]);
2207 }
2208 err = 1;
2209 }
2210
2211 if (err == 1) {
2212 if (searchdirs != NULL)
2213 free(searchdirs);
2214 if (envdup != NULL)
2215 free(envdup);
2216 nvlist_free(policy);
2217 return (1);
2218 }
2219
2220 /*
2221 * At this point we have a list of import candidate configs. Even if
2222 * we were searching by pool name or guid, we still need to
2223 * post-process the list to deal with pool state and possible
2224 * duplicate names.
2225 */
2226 err = 0;
2227 elem = NULL;
2228 first = B_TRUE;
2229 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2230
2231 verify(nvpair_value_nvlist(elem, &config) == 0);
2232
2233 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2234 &pool_state) == 0);
2235 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
2236 continue;
2237 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
2238 continue;
2239
2240 verify(nvlist_add_nvlist(config, ZPOOL_REWIND_POLICY,
2241 policy) == 0);
2242
2243 if (argc == 0) {
2244 if (first)
2245 first = B_FALSE;
2246 else if (!do_all)
2247 (void) printf("\n");
2248
2249 if (do_all) {
2250 err |= do_import(config, NULL, mntopts,
2251 props, flags);
2252 } else {
2253 show_import(config);
2254 }
2255 } else if (searchname != NULL) {
2256 char *name;
2257
2258 /*
2259 * We are searching for a pool based on name.
2260 */
2261 verify(nvlist_lookup_string(config,
2262 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
2263
2264 if (strcmp(name, searchname) == 0) {
2265 if (found_config != NULL) {
2266 (void) fprintf(stderr, gettext(
2267 "cannot import '%s': more than "
2268 "one matching pool\n"), searchname);
2269 (void) fprintf(stderr, gettext(
2270 "import by numeric ID instead\n"));
2271 err = B_TRUE;
2272 }
2273 found_config = config;
2274 }
2275 } else {
2276 uint64_t guid;
2277
2278 /*
2279 * Search for a pool by guid.
2280 */
2281 verify(nvlist_lookup_uint64(config,
2282 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
2283
2284 if (guid == searchguid)
2285 found_config = config;
2286 }
2287 }
2288
2289 /*
2290 * If we were searching for a specific pool, verify that we found a
2291 * pool, and then do the import.
2292 */
2293 if (argc != 0 && err == 0) {
2294 if (found_config == NULL) {
2295 (void) fprintf(stderr, gettext("cannot import '%s': "
2296 "no such pool available\n"), argv[0]);
2297 err = B_TRUE;
2298 } else {
2299 err |= do_import(found_config, argc == 1 ? NULL :
2300 argv[1], mntopts, props, flags);
2301 }
2302 }
2303
2304 /*
2305 * If we were just looking for pools, report an error if none were
2306 * found.
2307 */
2308 if (argc == 0 && first)
2309 (void) fprintf(stderr,
2310 gettext("no pools available to import\n"));
2311
2312 error:
2313 nvlist_free(props);
2314 nvlist_free(pools);
2315 nvlist_free(policy);
2316 if (searchdirs != NULL)
2317 free(searchdirs);
2318 if (envdup != NULL)
2319 free(envdup);
2320
2321 return (err ? 1 : 0);
2322 }
2323
2324 typedef struct iostat_cbdata {
2325 boolean_t cb_verbose;
2326 int cb_namewidth;
2327 int cb_iteration;
2328 zpool_list_t *cb_list;
2329 } iostat_cbdata_t;
2330
2331 static void
2332 print_iostat_separator(iostat_cbdata_t *cb)
2333 {
2334 int i = 0;
2335
2336 for (i = 0; i < cb->cb_namewidth; i++)
2337 (void) printf("-");
2338 (void) printf(" ----- ----- ----- ----- ----- -----\n");
2339 }
2340
2341 static void
2342 print_iostat_header(iostat_cbdata_t *cb)
2343 {
2344 (void) printf("%*s capacity operations bandwidth\n",
2345 cb->cb_namewidth, "");
2346 (void) printf("%-*s alloc free read write read write\n",
2347 cb->cb_namewidth, "pool");
2348 print_iostat_separator(cb);
2349 }
2350
2351 /*
2352 * Display a single statistic.
2353 */
2354 static void
2355 print_one_stat(uint64_t value)
2356 {
2357 char buf[64];
2358
2359 zfs_nicenum(value, buf, sizeof (buf));
2360 (void) printf(" %5s", buf);
2361 }
2362
2363 /*
2364 * Print out all the statistics for the given vdev. This can either be the
2365 * toplevel configuration, or called recursively. If 'name' is NULL, then this
2366 * is a verbose output, and we don't want to display the toplevel pool stats.
2367 */
2368 void
2369 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
2370 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
2371 {
2372 nvlist_t **oldchild, **newchild;
2373 uint_t c, children;
2374 vdev_stat_t *oldvs, *newvs;
2375 vdev_stat_t zerovs = { 0 };
2376 uint64_t tdelta;
2377 double scale;
2378 char *vname;
2379
2380 if (oldnv != NULL) {
2381 verify(nvlist_lookup_uint64_array(oldnv,
2382 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
2383 } else {
2384 oldvs = &zerovs;
2385 }
2386
2387 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
2388 (uint64_t **)&newvs, &c) == 0);
2389
2390 if (strlen(name) + depth > cb->cb_namewidth)
2391 (void) printf("%*s%s", depth, "", name);
2392 else
2393 (void) printf("%*s%s%*s", depth, "", name,
2394 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2395
2396 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
2397
2398 if (tdelta == 0)
2399 scale = 1.0;
2400 else
2401 scale = (double)NANOSEC / tdelta;
2402
2403 /* only toplevel vdevs have capacity stats */
2404 if (newvs->vs_space == 0) {
2405 (void) printf(" - -");
2406 } else {
2407 print_one_stat(newvs->vs_alloc);
2408 print_one_stat(newvs->vs_space - newvs->vs_alloc);
2409 }
2410
2411 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_READ] -
2412 oldvs->vs_ops[ZIO_TYPE_READ])));
2413
2414 print_one_stat((uint64_t)(scale * (newvs->vs_ops[ZIO_TYPE_WRITE] -
2415 oldvs->vs_ops[ZIO_TYPE_WRITE])));
2416
2417 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_READ] -
2418 oldvs->vs_bytes[ZIO_TYPE_READ])));
2419
2420 print_one_stat((uint64_t)(scale * (newvs->vs_bytes[ZIO_TYPE_WRITE] -
2421 oldvs->vs_bytes[ZIO_TYPE_WRITE])));
2422
2423 (void) printf("\n");
2424
2425 if (!cb->cb_verbose)
2426 return;
2427
2428 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
2429 &newchild, &children) != 0)
2430 return;
2431
2432 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
2433 &oldchild, &c) != 0)
2434 return;
2435
2436 for (c = 0; c < children; c++) {
2437 uint64_t ishole = B_FALSE, islog = B_FALSE;
2438
2439 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
2440 &ishole);
2441
2442 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
2443 &islog);
2444
2445 if (ishole || islog)
2446 continue;
2447
2448 vname = zpool_vdev_name(g_zfs, zhp, newchild[c], B_FALSE);
2449 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2450 newchild[c], cb, depth + 2);
2451 free(vname);
2452 }
2453
2454 /*
2455 * Log device section
2456 */
2457
2458 if (num_logs(newnv) > 0) {
2459 (void) printf("%-*s - - - - - "
2460 "-\n", cb->cb_namewidth, "logs");
2461
2462 for (c = 0; c < children; c++) {
2463 uint64_t islog = B_FALSE;
2464 (void) nvlist_lookup_uint64(newchild[c],
2465 ZPOOL_CONFIG_IS_LOG, &islog);
2466
2467 if (islog) {
2468 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2469 B_FALSE);
2470 print_vdev_stats(zhp, vname, oldnv ?
2471 oldchild[c] : NULL, newchild[c],
2472 cb, depth + 2);
2473 free(vname);
2474 }
2475 }
2476
2477 }
2478
2479 /*
2480 * Include level 2 ARC devices in iostat output
2481 */
2482 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
2483 &newchild, &children) != 0)
2484 return;
2485
2486 if (oldnv && nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
2487 &oldchild, &c) != 0)
2488 return;
2489
2490 if (children > 0) {
2491 (void) printf("%-*s - - - - - "
2492 "-\n", cb->cb_namewidth, "cache");
2493 for (c = 0; c < children; c++) {
2494 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
2495 B_FALSE);
2496 print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
2497 newchild[c], cb, depth + 2);
2498 free(vname);
2499 }
2500 }
2501 }
2502
2503 static int
2504 refresh_iostat(zpool_handle_t *zhp, void *data)
2505 {
2506 iostat_cbdata_t *cb = data;
2507 boolean_t missing;
2508
2509 /*
2510 * If the pool has disappeared, remove it from the list and continue.
2511 */
2512 if (zpool_refresh_stats(zhp, &missing) != 0)
2513 return (-1);
2514
2515 if (missing)
2516 pool_list_remove(cb->cb_list, zhp);
2517
2518 return (0);
2519 }
2520
2521 /*
2522 * Callback to print out the iostats for the given pool.
2523 */
2524 int
2525 print_iostat(zpool_handle_t *zhp, void *data)
2526 {
2527 iostat_cbdata_t *cb = data;
2528 nvlist_t *oldconfig, *newconfig;
2529 nvlist_t *oldnvroot, *newnvroot;
2530
2531 newconfig = zpool_get_config(zhp, &oldconfig);
2532
2533 if (cb->cb_iteration == 1)
2534 oldconfig = NULL;
2535
2536 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
2537 &newnvroot) == 0);
2538
2539 if (oldconfig == NULL)
2540 oldnvroot = NULL;
2541 else
2542 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
2543 &oldnvroot) == 0);
2544
2545 /*
2546 * Print out the statistics for the pool.
2547 */
2548 print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot, cb, 0);
2549
2550 if (cb->cb_verbose)
2551 print_iostat_separator(cb);
2552
2553 return (0);
2554 }
2555
2556 static int
2557 get_columns(void)
2558 {
2559 struct winsize ws;
2560 int columns = 80;
2561 int error;
2562
2563 if (isatty(STDOUT_FILENO)) {
2564 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
2565 if (error == 0)
2566 columns = ws.ws_col;
2567 } else {
2568 columns = 999;
2569 }
2570
2571 return columns;
2572 }
2573
2574 int
2575 get_namewidth(zpool_handle_t *zhp, void *data)
2576 {
2577 iostat_cbdata_t *cb = data;
2578 nvlist_t *config, *nvroot;
2579 int columns;
2580
2581 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
2582 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2583 &nvroot) == 0);
2584 if (!cb->cb_verbose)
2585 cb->cb_namewidth = strlen(zpool_get_name(zhp));
2586 else
2587 cb->cb_namewidth = max_width(zhp, nvroot, 0,
2588 cb->cb_namewidth);
2589 }
2590
2591 /*
2592 * The width must be at least 10, but may be as large as the
2593 * column width - 42 so that we can still fit in one line.
2594 */
2595 columns = get_columns();
2596
2597 if (cb->cb_namewidth < 10)
2598 cb->cb_namewidth = 10;
2599 if (cb->cb_namewidth > columns - 42)
2600 cb->cb_namewidth = columns - 42;
2601
2602 return (0);
2603 }
2604
2605 /*
2606 * Parse the input string, get the 'interval' and 'count' value if there is one.
2607 */
2608 static void
2609 get_interval_count(int *argcp, char **argv, unsigned long *iv,
2610 unsigned long *cnt)
2611 {
2612 unsigned long interval = 0, count = 0;
2613 int argc = *argcp;
2614
2615 /*
2616 * Determine if the last argument is an integer or a pool name
2617 */
2618 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2619 char *end;
2620
2621 errno = 0;
2622 interval = strtoul(argv[argc - 1], &end, 10);
2623
2624 if (*end == '\0' && errno == 0) {
2625 if (interval == 0) {
2626 (void) fprintf(stderr, gettext("interval "
2627 "cannot be zero\n"));
2628 usage(B_FALSE);
2629 }
2630 /*
2631 * Ignore the last parameter
2632 */
2633 argc--;
2634 } else {
2635 /*
2636 * If this is not a valid number, just plow on. The
2637 * user will get a more informative error message later
2638 * on.
2639 */
2640 interval = 0;
2641 }
2642 }
2643
2644 /*
2645 * If the last argument is also an integer, then we have both a count
2646 * and an interval.
2647 */
2648 if (argc > 0 && isdigit(argv[argc - 1][0])) {
2649 char *end;
2650
2651 errno = 0;
2652 count = interval;
2653 interval = strtoul(argv[argc - 1], &end, 10);
2654
2655 if (*end == '\0' && errno == 0) {
2656 if (interval == 0) {
2657 (void) fprintf(stderr, gettext("interval "
2658 "cannot be zero\n"));
2659 usage(B_FALSE);
2660 }
2661
2662 /*
2663 * Ignore the last parameter
2664 */
2665 argc--;
2666 } else {
2667 interval = 0;
2668 }
2669 }
2670
2671 *iv = interval;
2672 *cnt = count;
2673 *argcp = argc;
2674 }
2675
2676 static void
2677 get_timestamp_arg(char c)
2678 {
2679 if (c == 'u')
2680 timestamp_fmt = UDATE;
2681 else if (c == 'd')
2682 timestamp_fmt = DDATE;
2683 else
2684 usage(B_FALSE);
2685 }
2686
2687 /*
2688 * zpool iostat [-v] [-T d|u] [pool] ... [interval [count]]
2689 *
2690 * -v Display statistics for individual vdevs
2691 * -T Display a timestamp in date(1) or Unix format
2692 *
2693 * This command can be tricky because we want to be able to deal with pool
2694 * creation/destruction as well as vdev configuration changes. The bulk of this
2695 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
2696 * on pool_list_update() to detect the addition of new pools. Configuration
2697 * changes are all handled within libzfs.
2698 */
2699 int
2700 zpool_do_iostat(int argc, char **argv)
2701 {
2702 int c;
2703 int ret;
2704 int npools;
2705 unsigned long interval = 0, count = 0;
2706 zpool_list_t *list;
2707 boolean_t verbose = B_FALSE;
2708 iostat_cbdata_t cb;
2709
2710 /* check options */
2711 while ((c = getopt(argc, argv, "T:v")) != -1) {
2712 switch (c) {
2713 case 'T':
2714 get_timestamp_arg(*optarg);
2715 break;
2716 case 'v':
2717 verbose = B_TRUE;
2718 break;
2719 case '?':
2720 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2721 optopt);
2722 usage(B_FALSE);
2723 }
2724 }
2725
2726 argc -= optind;
2727 argv += optind;
2728
2729 get_interval_count(&argc, argv, &interval, &count);
2730
2731 /*
2732 * Construct the list of all interesting pools.
2733 */
2734 ret = 0;
2735 if ((list = pool_list_get(argc, argv, NULL, &ret)) == NULL)
2736 return (1);
2737
2738 if (pool_list_count(list) == 0 && argc != 0) {
2739 pool_list_free(list);
2740 return (1);
2741 }
2742
2743 if (pool_list_count(list) == 0 && interval == 0) {
2744 pool_list_free(list);
2745 (void) fprintf(stderr, gettext("no pools available\n"));
2746 return (1);
2747 }
2748
2749 /*
2750 * Enter the main iostat loop.
2751 */
2752 cb.cb_list = list;
2753 cb.cb_verbose = verbose;
2754 cb.cb_iteration = 0;
2755 cb.cb_namewidth = 0;
2756
2757 for (;;) {
2758 pool_list_update(list);
2759
2760 if ((npools = pool_list_count(list)) == 0)
2761 (void) fprintf(stderr, gettext("no pools available\n"));
2762 else {
2763 /*
2764 * Refresh all statistics. This is done as an
2765 * explicit step before calculating the maximum name
2766 * width, so that any * configuration changes are
2767 * properly accounted for.
2768 */
2769 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
2770 &cb);
2771
2772 /*
2773 * Iterate over all pools to determine the maximum width
2774 * for the pool / device name column across all pools.
2775 */
2776 cb.cb_namewidth = 0;
2777 (void) pool_list_iter(list, B_FALSE, get_namewidth,
2778 &cb);
2779
2780 if (timestamp_fmt != NODATE)
2781 print_timestamp(timestamp_fmt);
2782
2783 /*
2784 * If it's the first time, or verbose mode, print the
2785 * header.
2786 */
2787 if (++cb.cb_iteration == 1 || verbose)
2788 print_iostat_header(&cb);
2789
2790 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
2791
2792 /*
2793 * If there's more than one pool, and we're not in
2794 * verbose mode (which prints a separator for us),
2795 * then print a separator.
2796 */
2797 if (npools > 1 && !verbose)
2798 print_iostat_separator(&cb);
2799
2800 if (verbose)
2801 (void) printf("\n");
2802 }
2803
2804 /*
2805 * Flush the output so that redirection to a file isn't buffered
2806 * indefinitely.
2807 */
2808 (void) fflush(stdout);
2809
2810 if (interval == 0)
2811 break;
2812
2813 if (count != 0 && --count == 0)
2814 break;
2815
2816 (void) sleep(interval);
2817 }
2818
2819 pool_list_free(list);
2820
2821 return (ret);
2822 }
2823
2824 typedef struct list_cbdata {
2825 boolean_t cb_verbose;
2826 int cb_namewidth;
2827 boolean_t cb_scripted;
2828 zprop_list_t *cb_proplist;
2829 } list_cbdata_t;
2830
2831 /*
2832 * Given a list of columns to display, output appropriate headers for each one.
2833 */
2834 static void
2835 print_header(list_cbdata_t *cb)
2836 {
2837 zprop_list_t *pl = cb->cb_proplist;
2838 char headerbuf[ZPOOL_MAXPROPLEN];
2839 const char *header;
2840 boolean_t first = B_TRUE;
2841 boolean_t right_justify;
2842 size_t width = 0;
2843
2844 for (; pl != NULL; pl = pl->pl_next) {
2845 width = pl->pl_width;
2846 if (first && cb->cb_verbose) {
2847 /*
2848 * Reset the width to accommodate the verbose listing
2849 * of devices.
2850 */
2851 width = cb->cb_namewidth;
2852 }
2853
2854 if (!first)
2855 (void) printf(" ");
2856 else
2857 first = B_FALSE;
2858
2859 right_justify = B_FALSE;
2860 if (pl->pl_prop != ZPROP_INVAL) {
2861 header = zpool_prop_column_name(pl->pl_prop);
2862 right_justify = zpool_prop_align_right(pl->pl_prop);
2863 } else {
2864 int i;
2865
2866 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2867 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2868 headerbuf[i] = '\0';
2869 header = headerbuf;
2870 }
2871
2872 if (pl->pl_next == NULL && !right_justify)
2873 (void) printf("%s", header);
2874 else if (right_justify)
2875 (void) printf("%*s", (int)width, header);
2876 else
2877 (void) printf("%-*s", (int)width, header);
2878 }
2879
2880 (void) printf("\n");
2881 }
2882
2883 /*
2884 * Given a pool and a list of properties, print out all the properties according
2885 * to the described layout.
2886 */
2887 static void
2888 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
2889 {
2890 zprop_list_t *pl = cb->cb_proplist;
2891 boolean_t first = B_TRUE;
2892 char property[ZPOOL_MAXPROPLEN];
2893 char *propstr;
2894 boolean_t right_justify;
2895 size_t width;
2896
2897 for (; pl != NULL; pl = pl->pl_next) {
2898
2899 width = pl->pl_width;
2900 if (first && cb->cb_verbose) {
2901 /*
2902 * Reset the width to accommodate the verbose listing
2903 * of devices.
2904 */
2905 width = cb->cb_namewidth;
2906 }
2907
2908 if (!first) {
2909 if (cb->cb_scripted)
2910 (void) printf("\t");
2911 else
2912 (void) printf(" ");
2913 } else {
2914 first = B_FALSE;
2915 }
2916
2917 right_justify = B_FALSE;
2918 if (pl->pl_prop != ZPROP_INVAL) {
2919 if (pl->pl_prop == ZPOOL_PROP_EXPANDSZ &&
2920 zpool_get_prop_int(zhp, pl->pl_prop, NULL) == 0)
2921 propstr = "-";
2922 else if (zpool_get_prop(zhp, pl->pl_prop, property,
2923 sizeof (property), NULL) != 0)
2924 propstr = "-";
2925 else
2926 propstr = property;
2927
2928 right_justify = zpool_prop_align_right(pl->pl_prop);
2929 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
2930 zpool_prop_unsupported(pl->pl_user_prop)) &&
2931 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
2932 sizeof (property)) == 0) {
2933 propstr = property;
2934 } else {
2935 propstr = "-";
2936 }
2937
2938
2939 /*
2940 * If this is being called in scripted mode, or if this is the
2941 * last column and it is left-justified, don't include a width
2942 * format specifier.
2943 */
2944 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2945 (void) printf("%s", propstr);
2946 else if (right_justify)
2947 (void) printf("%*s", (int)width, propstr);
2948 else
2949 (void) printf("%-*s", (int)width, propstr);
2950 }
2951
2952 (void) printf("\n");
2953 }
2954
2955 static void
2956 print_one_column(zpool_prop_t prop, uint64_t value, boolean_t scripted)
2957 {
2958 char propval[64];
2959 boolean_t fixed;
2960 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
2961
2962 zfs_nicenum(value, propval, sizeof (propval));
2963
2964 if (prop == ZPOOL_PROP_EXPANDSZ && value == 0)
2965 (void) strlcpy(propval, "-", sizeof (propval));
2966
2967 if (scripted)
2968 (void) printf("\t%s", propval);
2969 else
2970 (void) printf(" %*s", (int)width, propval);
2971 }
2972
2973 void
2974 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
2975 list_cbdata_t *cb, int depth)
2976 {
2977 nvlist_t **child;
2978 vdev_stat_t *vs;
2979 uint_t c, children;
2980 char *vname;
2981 boolean_t scripted = cb->cb_scripted;
2982
2983 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2984 (uint64_t **)&vs, &c) == 0);
2985
2986 if (name != NULL) {
2987 if (scripted)
2988 (void) printf("\t%s", name);
2989 else if (strlen(name) + depth > cb->cb_namewidth)
2990 (void) printf("%*s%s", depth, "", name);
2991 else
2992 (void) printf("%*s%s%*s", depth, "", name,
2993 (int)(cb->cb_namewidth - strlen(name) - depth), "");
2994
2995 /* only toplevel vdevs have capacity stats */
2996 if (vs->vs_space == 0) {
2997 if (scripted)
2998 (void) printf("\t-\t-\t-");
2999 else
3000 (void) printf(" - - -");
3001 } else {
3002 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space,
3003 scripted);
3004 print_one_column(ZPOOL_PROP_CAPACITY, vs->vs_alloc,
3005 scripted);
3006 print_one_column(ZPOOL_PROP_FREE,
3007 vs->vs_space - vs->vs_alloc, scripted);
3008 }
3009 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize,
3010 scripted);
3011 (void) printf("\n");
3012 }
3013
3014 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3015 &child, &children) != 0)
3016 return;
3017
3018 for (c = 0; c < children; c++) {
3019 uint64_t ishole = B_FALSE;
3020
3021 if (nvlist_lookup_uint64(child[c],
3022 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
3023 continue;
3024
3025 vname = zpool_vdev_name(g_zfs, zhp, child[c], B_FALSE);
3026 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3027 free(vname);
3028 }
3029
3030 /*
3031 * Include level 2 ARC devices in iostat output
3032 */
3033 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3034 &child, &children) != 0)
3035 return;
3036
3037 if (children > 0) {
3038 (void) printf("%-*s - - - - - "
3039 "-\n", cb->cb_namewidth, "cache");
3040 for (c = 0; c < children; c++) {
3041 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3042 B_FALSE);
3043 print_list_stats(zhp, vname, child[c], cb, depth + 2);
3044 free(vname);
3045 }
3046 }
3047 }
3048
3049
3050 /*
3051 * Generic callback function to list a pool.
3052 */
3053 int
3054 list_callback(zpool_handle_t *zhp, void *data)
3055 {
3056 list_cbdata_t *cbp = data;
3057 nvlist_t *config;
3058 nvlist_t *nvroot;
3059
3060 config = zpool_get_config(zhp, NULL);
3061
3062 print_pool(zhp, cbp);
3063 if (!cbp->cb_verbose)
3064 return (0);
3065
3066 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3067 &nvroot) == 0);
3068 print_list_stats(zhp, NULL, nvroot, cbp, 0);
3069
3070 return (0);
3071 }
3072
3073 /*
3074 * zpool list [-H] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3075 *
3076 * -H Scripted mode. Don't display headers, and separate properties
3077 * by a single tab.
3078 * -o List of properties to display. Defaults to
3079 * "name,size,allocated,free,capacity,health,altroot"
3080 * -T Display a timestamp in date(1) or Unix format
3081 *
3082 * List all pools in the system, whether or not they're healthy. Output space
3083 * statistics for each one, as well as health status summary.
3084 */
3085 int
3086 zpool_do_list(int argc, char **argv)
3087 {
3088 int c;
3089 int ret = 0;
3090 list_cbdata_t cb = { 0 };
3091 static char default_props[] =
3092 "name,size,allocated,free,capacity,dedupratio,"
3093 "health,altroot";
3094 char *props = default_props;
3095 unsigned long interval = 0, count = 0;
3096 zpool_list_t *list;
3097 boolean_t first = B_TRUE;
3098
3099 /* check options */
3100 while ((c = getopt(argc, argv, ":Ho:T:v")) != -1) {
3101 switch (c) {
3102 case 'H':
3103 cb.cb_scripted = B_TRUE;
3104 break;
3105 case 'o':
3106 props = optarg;
3107 break;
3108 case 'T':
3109 get_timestamp_arg(*optarg);
3110 break;
3111 case 'v':
3112 cb.cb_verbose = B_TRUE;
3113 break;
3114 case ':':
3115 (void) fprintf(stderr, gettext("missing argument for "
3116 "'%c' option\n"), optopt);
3117 usage(B_FALSE);
3118 break;
3119 case '?':
3120 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3121 optopt);
3122 usage(B_FALSE);
3123 }
3124 }
3125
3126 argc -= optind;
3127 argv += optind;
3128
3129 get_interval_count(&argc, argv, &interval, &count);
3130
3131 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
3132 usage(B_FALSE);
3133
3134 if ((list = pool_list_get(argc, argv, &cb.cb_proplist, &ret)) == NULL)
3135 return (1);
3136
3137 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
3138 (void) printf(gettext("no pools available\n"));
3139 zprop_free_list(cb.cb_proplist);
3140 return (0);
3141 }
3142
3143 for (;;) {
3144 pool_list_update(list);
3145
3146 if (pool_list_count(list) == 0)
3147 break;
3148
3149 if (timestamp_fmt != NODATE)
3150 print_timestamp(timestamp_fmt);
3151
3152 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
3153 print_header(&cb);
3154 first = B_FALSE;
3155 }
3156 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
3157
3158 if (interval == 0)
3159 break;
3160
3161 if (count != 0 && --count == 0)
3162 break;
3163
3164 (void) sleep(interval);
3165 }
3166
3167 zprop_free_list(cb.cb_proplist);
3168 return (ret);
3169 }
3170
3171 static int
3172 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
3173 {
3174 boolean_t force = B_FALSE;
3175 int c;
3176 nvlist_t *nvroot;
3177 char *poolname, *old_disk, *new_disk;
3178 zpool_handle_t *zhp;
3179 nvlist_t *props = NULL;
3180 char *propval;
3181 int ret;
3182
3183 /* check options */
3184 while ((c = getopt(argc, argv, "fo:")) != -1) {
3185 switch (c) {
3186 case 'f':
3187 force = B_TRUE;
3188 break;
3189 case 'o':
3190 if ((propval = strchr(optarg, '=')) == NULL) {
3191 (void) fprintf(stderr, gettext("missing "
3192 "'=' for -o option\n"));
3193 usage(B_FALSE);
3194 }
3195 *propval = '\0';
3196 propval++;
3197
3198 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
3199 (add_prop_list(optarg, propval, &props, B_TRUE)))
3200 usage(B_FALSE);
3201 break;
3202 case '?':
3203 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3204 optopt);
3205 usage(B_FALSE);
3206 }
3207 }
3208
3209 argc -= optind;
3210 argv += optind;
3211
3212 /* get pool name and check number of arguments */
3213 if (argc < 1) {
3214 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3215 usage(B_FALSE);
3216 }
3217
3218 poolname = argv[0];
3219
3220 if (argc < 2) {
3221 (void) fprintf(stderr,
3222 gettext("missing <device> specification\n"));
3223 usage(B_FALSE);
3224 }
3225
3226 old_disk = argv[1];
3227
3228 if (argc < 3) {
3229 if (!replacing) {
3230 (void) fprintf(stderr,
3231 gettext("missing <new_device> specification\n"));
3232 usage(B_FALSE);
3233 }
3234 new_disk = old_disk;
3235 argc -= 1;
3236 argv += 1;
3237 } else {
3238 new_disk = argv[2];
3239 argc -= 2;
3240 argv += 2;
3241 }
3242
3243 if (argc > 1) {
3244 (void) fprintf(stderr, gettext("too many arguments\n"));
3245 usage(B_FALSE);
3246 }
3247
3248 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3249 return (1);
3250
3251 if (zpool_get_config(zhp, NULL) == NULL) {
3252 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
3253 poolname);
3254 zpool_close(zhp);
3255 return (1);
3256 }
3257
3258 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
3259 argc, argv);
3260 if (nvroot == NULL) {
3261 zpool_close(zhp);
3262 return (1);
3263 }
3264
3265 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing);
3266
3267 nvlist_free(nvroot);
3268 zpool_close(zhp);
3269
3270 return (ret);
3271 }
3272
3273 /*
3274 * zpool replace [-f] <pool> <device> <new_device>
3275 *
3276 * -f Force attach, even if <new_device> appears to be in use.
3277 *
3278 * Replace <device> with <new_device>.
3279 */
3280 /* ARGSUSED */
3281 int
3282 zpool_do_replace(int argc, char **argv)
3283 {
3284 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
3285 }
3286
3287 /*
3288 * zpool attach [-f] [-o property=value] <pool> <device> <new_device>
3289 *
3290 * -f Force attach, even if <new_device> appears to be in use.
3291 * -o Set property=value.
3292 *
3293 * Attach <new_device> to the mirror containing <device>. If <device> is not
3294 * part of a mirror, then <device> will be transformed into a mirror of
3295 * <device> and <new_device>. In either case, <new_device> will begin life
3296 * with a DTL of [0, now], and will immediately begin to resilver itself.
3297 */
3298 int
3299 zpool_do_attach(int argc, char **argv)
3300 {
3301 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
3302 }
3303
3304 /*
3305 * zpool detach [-f] <pool> <device>
3306 *
3307 * -f Force detach of <device>, even if DTLs argue against it
3308 * (not supported yet)
3309 *
3310 * Detach a device from a mirror. The operation will be refused if <device>
3311 * is the last device in the mirror, or if the DTLs indicate that this device
3312 * has the only valid copy of some data.
3313 */
3314 /* ARGSUSED */
3315 int
3316 zpool_do_detach(int argc, char **argv)
3317 {
3318 int c;
3319 char *poolname, *path;
3320 zpool_handle_t *zhp;
3321 int ret;
3322
3323 /* check options */
3324 while ((c = getopt(argc, argv, "f")) != -1) {
3325 switch (c) {
3326 case 'f':
3327 case '?':
3328 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3329 optopt);
3330 usage(B_FALSE);
3331 }
3332 }
3333
3334 argc -= optind;
3335 argv += optind;
3336
3337 /* get pool name and check number of arguments */
3338 if (argc < 1) {
3339 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3340 usage(B_FALSE);
3341 }
3342
3343 if (argc < 2) {
3344 (void) fprintf(stderr,
3345 gettext("missing <device> specification\n"));
3346 usage(B_FALSE);
3347 }
3348
3349 poolname = argv[0];
3350 path = argv[1];
3351
3352 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3353 return (1);
3354
3355 ret = zpool_vdev_detach(zhp, path);
3356
3357 zpool_close(zhp);
3358
3359 return (ret);
3360 }
3361
3362 /*
3363 * zpool split [-n] [-o prop=val] ...
3364 * [-o mntopt] ...
3365 * [-R altroot] <pool> <newpool> [<device> ...]
3366 *
3367 * -n Do not split the pool, but display the resulting layout if
3368 * it were to be split.
3369 * -o Set property=value, or set mount options.
3370 * -R Mount the split-off pool under an alternate root.
3371 *
3372 * Splits the named pool and gives it the new pool name. Devices to be split
3373 * off may be listed, provided that no more than one device is specified
3374 * per top-level vdev mirror. The newly split pool is left in an exported
3375 * state unless -R is specified.
3376 *
3377 * Restrictions: the top-level of the pool pool must only be made up of
3378 * mirrors; all devices in the pool must be healthy; no device may be
3379 * undergoing a resilvering operation.
3380 */
3381 int
3382 zpool_do_split(int argc, char **argv)
3383 {
3384 char *srcpool, *newpool, *propval;
3385 char *mntopts = NULL;
3386 splitflags_t flags;
3387 int c, ret = 0;
3388 zpool_handle_t *zhp;
3389 nvlist_t *config, *props = NULL;
3390
3391 flags.dryrun = B_FALSE;
3392 flags.import = B_FALSE;
3393
3394 /* check options */
3395 while ((c = getopt(argc, argv, ":R:no:")) != -1) {
3396 switch (c) {
3397 case 'R':
3398 flags.import = B_TRUE;
3399 if (add_prop_list(
3400 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
3401 &props, B_TRUE) != 0) {
3402 if (props)
3403 nvlist_free(props);
3404 usage(B_FALSE);
3405 }
3406 break;
3407 case 'n':
3408 flags.dryrun = B_TRUE;
3409 break;
3410 case 'o':
3411 if ((propval = strchr(optarg, '=')) != NULL) {
3412 *propval = '\0';
3413 propval++;
3414 if (add_prop_list(optarg, propval,
3415 &props, B_TRUE) != 0) {
3416 if (props)
3417 nvlist_free(props);
3418 usage(B_FALSE);
3419 }
3420 } else {
3421 mntopts = optarg;
3422 }
3423 break;
3424 case ':':
3425 (void) fprintf(stderr, gettext("missing argument for "
3426 "'%c' option\n"), optopt);
3427 usage(B_FALSE);
3428 break;
3429 case '?':
3430 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3431 optopt);
3432 usage(B_FALSE);
3433 break;
3434 }
3435 }
3436
3437 if (!flags.import && mntopts != NULL) {
3438 (void) fprintf(stderr, gettext("setting mntopts is only "
3439 "valid when importing the pool\n"));
3440 usage(B_FALSE);
3441 }
3442
3443 argc -= optind;
3444 argv += optind;
3445
3446 if (argc < 1) {
3447 (void) fprintf(stderr, gettext("Missing pool name\n"));
3448 usage(B_FALSE);
3449 }
3450 if (argc < 2) {
3451 (void) fprintf(stderr, gettext("Missing new pool name\n"));
3452 usage(B_FALSE);
3453 }
3454
3455 srcpool = argv[0];
3456 newpool = argv[1];
3457
3458 argc -= 2;
3459 argv += 2;
3460
3461 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL)
3462 return (1);
3463
3464 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
3465 if (config == NULL) {
3466 ret = 1;
3467 } else {
3468 if (flags.dryrun) {
3469 (void) printf(gettext("would create '%s' with the "
3470 "following layout:\n\n"), newpool);
3471 print_vdev_tree(NULL, newpool, config, 0, B_FALSE);
3472 }
3473 nvlist_free(config);
3474 }
3475
3476 zpool_close(zhp);
3477
3478 if (ret != 0 || flags.dryrun || !flags.import)
3479 return (ret);
3480
3481 /*
3482 * The split was successful. Now we need to open the new
3483 * pool and import it.
3484 */
3485 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL)
3486 return (1);
3487 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3488 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3489 ret = 1;
3490 (void) fprintf(stderr, gettext("Split was successful, but "
3491 "the datasets could not all be mounted\n"));
3492 (void) fprintf(stderr, gettext("Try doing '%s' with a "
3493 "different altroot\n"), "zpool import");
3494 }
3495 zpool_close(zhp);
3496
3497 return (ret);
3498 }
3499
3500
3501
3502 /*
3503 * zpool online <pool> <device> ...
3504 */
3505 int
3506 zpool_do_online(int argc, char **argv)
3507 {
3508 int c, i;
3509 char *poolname;
3510 zpool_handle_t *zhp;
3511 int ret = 0;
3512 vdev_state_t newstate;
3513 int flags = 0;
3514
3515 /* check options */
3516 while ((c = getopt(argc, argv, "et")) != -1) {
3517 switch (c) {
3518 case 'e':
3519 flags |= ZFS_ONLINE_EXPAND;
3520 break;
3521 case 't':
3522 case '?':
3523 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3524 optopt);
3525 usage(B_FALSE);
3526 }
3527 }
3528
3529 argc -= optind;
3530 argv += optind;
3531
3532 /* get pool name and check number of arguments */
3533 if (argc < 1) {
3534 (void) fprintf(stderr, gettext("missing pool name\n"));
3535 usage(B_FALSE);
3536 }
3537 if (argc < 2) {
3538 (void) fprintf(stderr, gettext("missing device name\n"));
3539 usage(B_FALSE);
3540 }
3541
3542 poolname = argv[0];
3543
3544 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3545 return (1);
3546
3547 for (i = 1; i < argc; i++) {
3548 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
3549 if (newstate != VDEV_STATE_HEALTHY) {
3550 (void) printf(gettext("warning: device '%s' "
3551 "onlined, but remains in faulted state\n"),
3552 argv[i]);
3553 if (newstate == VDEV_STATE_FAULTED)
3554 (void) printf(gettext("use 'zpool "
3555 "clear' to restore a faulted "
3556 "device\n"));
3557 else
3558 (void) printf(gettext("use 'zpool "
3559 "replace' to replace devices "
3560 "that are no longer present\n"));
3561 }
3562 } else {
3563 ret = 1;
3564 }
3565 }
3566
3567 zpool_close(zhp);
3568
3569 return (ret);
3570 }
3571
3572 /*
3573 * zpool offline [-ft] <pool> <device> ...
3574 *
3575 * -f Force the device into the offline state, even if doing
3576 * so would appear to compromise pool availability.
3577 * (not supported yet)
3578 *
3579 * -t Only take the device off-line temporarily. The offline
3580 * state will not be persistent across reboots.
3581 */
3582 /* ARGSUSED */
3583 int
3584 zpool_do_offline(int argc, char **argv)
3585 {
3586 int c, i;
3587 char *poolname;
3588 zpool_handle_t *zhp;
3589 int ret = 0;
3590 boolean_t istmp = B_FALSE;
3591
3592 /* check options */
3593 while ((c = getopt(argc, argv, "ft")) != -1) {
3594 switch (c) {
3595 case 't':
3596 istmp = B_TRUE;
3597 break;
3598 case 'f':
3599 case '?':
3600 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3601 optopt);
3602 usage(B_FALSE);
3603 }
3604 }
3605
3606 argc -= optind;
3607 argv += optind;
3608
3609 /* get pool name and check number of arguments */
3610 if (argc < 1) {
3611 (void) fprintf(stderr, gettext("missing pool name\n"));
3612 usage(B_FALSE);
3613 }
3614 if (argc < 2) {
3615 (void) fprintf(stderr, gettext("missing device name\n"));
3616 usage(B_FALSE);
3617 }
3618
3619 poolname = argv[0];
3620
3621 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3622 return (1);
3623
3624 for (i = 1; i < argc; i++) {
3625 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
3626 ret = 1;
3627 }
3628
3629 zpool_close(zhp);
3630
3631 return (ret);
3632 }
3633
3634 /*
3635 * zpool clear <pool> [device]
3636 *
3637 * Clear all errors associated with a pool or a particular device.
3638 */
3639 int
3640 zpool_do_clear(int argc, char **argv)
3641 {
3642 int c;
3643 int ret = 0;
3644 boolean_t dryrun = B_FALSE;
3645 boolean_t do_rewind = B_FALSE;
3646 boolean_t xtreme_rewind = B_FALSE;
3647 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3648 nvlist_t *policy = NULL;
3649 zpool_handle_t *zhp;
3650 char *pool, *device;
3651
3652 /* check options */
3653 while ((c = getopt(argc, argv, "FnX")) != -1) {
3654 switch (c) {
3655 case 'F':
3656 do_rewind = B_TRUE;
3657 break;
3658 case 'n':
3659 dryrun = B_TRUE;
3660 break;
3661 case 'X':
3662 xtreme_rewind = B_TRUE;
3663 break;
3664 case '?':
3665 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3666 optopt);
3667 usage(B_FALSE);
3668 }
3669 }
3670
3671 argc -= optind;
3672 argv += optind;
3673
3674 if (argc < 1) {
3675 (void) fprintf(stderr, gettext("missing pool name\n"));
3676 usage(B_FALSE);
3677 }
3678
3679 if (argc > 2) {
3680 (void) fprintf(stderr, gettext("too many arguments\n"));
3681 usage(B_FALSE);
3682 }
3683
3684 if ((dryrun || xtreme_rewind) && !do_rewind) {
3685 (void) fprintf(stderr,
3686 gettext("-n or -X only meaningful with -F\n"));
3687 usage(B_FALSE);
3688 }
3689 if (dryrun)
3690 rewind_policy = ZPOOL_TRY_REWIND;
3691 else if (do_rewind)
3692 rewind_policy = ZPOOL_DO_REWIND;
3693 if (xtreme_rewind)
3694 rewind_policy |= ZPOOL_EXTREME_REWIND;
3695
3696 /* In future, further rewind policy choices can be passed along here */
3697 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3698 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind_policy) != 0)
3699 return (1);
3700
3701 pool = argv[0];
3702 device = argc == 2 ? argv[1] : NULL;
3703
3704 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
3705 nvlist_free(policy);
3706 return (1);
3707 }
3708
3709 if (zpool_clear(zhp, device, policy) != 0)
3710 ret = 1;
3711
3712 zpool_close(zhp);
3713
3714 nvlist_free(policy);
3715
3716 return (ret);
3717 }
3718
3719 /*
3720 * zpool reguid <pool>
3721 */
3722 int
3723 zpool_do_reguid(int argc, char **argv)
3724 {
3725 int c;
3726 char *poolname;
3727 zpool_handle_t *zhp;
3728 int ret = 0;
3729
3730 /* check options */
3731 while ((c = getopt(argc, argv, "")) != -1) {
3732 switch (c) {
3733 case '?':
3734 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3735 optopt);
3736 usage(B_FALSE);
3737 }
3738 }
3739
3740 argc -= optind;
3741 argv += optind;
3742
3743 /* get pool name and check number of arguments */
3744 if (argc < 1) {
3745 (void) fprintf(stderr, gettext("missing pool name\n"));
3746 usage(B_FALSE);
3747 }
3748
3749 if (argc > 1) {
3750 (void) fprintf(stderr, gettext("too many arguments\n"));
3751 usage(B_FALSE);
3752 }
3753
3754 poolname = argv[0];
3755 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
3756 return (1);
3757
3758 ret = zpool_reguid(zhp);
3759
3760 zpool_close(zhp);
3761 return (ret);
3762 }
3763
3764
3765 /*
3766 * zpool reopen <pool>
3767 *
3768 * Reopen the pool so that the kernel can update the sizes of all vdevs.
3769 */
3770 int
3771 zpool_do_reopen(int argc, char **argv)
3772 {
3773 int c;
3774 int ret = 0;
3775 zpool_handle_t *zhp;
3776 char *pool;
3777
3778 /* check options */
3779 while ((c = getopt(argc, argv, "")) != -1) {
3780 switch (c) {
3781 case '?':
3782 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3783 optopt);
3784 usage(B_FALSE);
3785 }
3786 }
3787
3788 argc--;
3789 argv++;
3790
3791 if (argc < 1) {
3792 (void) fprintf(stderr, gettext("missing pool name\n"));
3793 usage(B_FALSE);
3794 }
3795
3796 if (argc > 1) {
3797 (void) fprintf(stderr, gettext("too many arguments\n"));
3798 usage(B_FALSE);
3799 }
3800
3801 pool = argv[0];
3802 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL)
3803 return (1);
3804
3805 ret = zpool_reopen(zhp);
3806 zpool_close(zhp);
3807 return (ret);
3808 }
3809
3810 typedef struct scrub_cbdata {
3811 int cb_type;
3812 int cb_argc;
3813 char **cb_argv;
3814 } scrub_cbdata_t;
3815
3816 int
3817 scrub_callback(zpool_handle_t *zhp, void *data)
3818 {
3819 scrub_cbdata_t *cb = data;
3820 int err;
3821
3822 /*
3823 * Ignore faulted pools.
3824 */
3825 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
3826 (void) fprintf(stderr, gettext("cannot scrub '%s': pool is "
3827 "currently unavailable\n"), zpool_get_name(zhp));
3828 return (1);
3829 }
3830
3831 err = zpool_scan(zhp, cb->cb_type);
3832
3833 return (err != 0);
3834 }
3835
3836 /*
3837 * zpool scrub [-s] <pool> ...
3838 *
3839 * -s Stop. Stops any in-progress scrub.
3840 */
3841 int
3842 zpool_do_scrub(int argc, char **argv)
3843 {
3844 int c;
3845 scrub_cbdata_t cb;
3846
3847 cb.cb_type = POOL_SCAN_SCRUB;
3848
3849 /* check options */
3850 while ((c = getopt(argc, argv, "s")) != -1) {
3851 switch (c) {
3852 case 's':
3853 cb.cb_type = POOL_SCAN_NONE;
3854 break;
3855 case '?':
3856 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3857 optopt);
3858 usage(B_FALSE);
3859 }
3860 }
3861
3862 cb.cb_argc = argc;
3863 cb.cb_argv = argv;
3864 argc -= optind;
3865 argv += optind;
3866
3867 if (argc < 1) {
3868 (void) fprintf(stderr, gettext("missing pool name argument\n"));
3869 usage(B_FALSE);
3870 }
3871
3872 return (for_each_pool(argc, argv, B_TRUE, NULL, scrub_callback, &cb));
3873 }
3874
3875 typedef struct status_cbdata {
3876 int cb_count;
3877 boolean_t cb_allpools;
3878 boolean_t cb_verbose;
3879 boolean_t cb_explain;
3880 boolean_t cb_first;
3881 boolean_t cb_dedup_stats;
3882 } status_cbdata_t;
3883
3884 /*
3885 * Print out detailed scrub status.
3886 */
3887 void
3888 print_scan_status(pool_scan_stat_t *ps)
3889 {
3890 time_t start, end;
3891 uint64_t elapsed, mins_left, hours_left;
3892 uint64_t pass_exam, examined, total;
3893 uint_t rate;
3894 double fraction_done;
3895 char processed_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
3896
3897 (void) printf(gettext(" scan: "));
3898
3899 /* If there's never been a scan, there's not much to say. */
3900 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
3901 ps->pss_func >= POOL_SCAN_FUNCS) {
3902 (void) printf(gettext("none requested\n"));
3903 return;
3904 }
3905
3906 start = ps->pss_start_time;
3907 end = ps->pss_end_time;
3908 zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
3909
3910 assert(ps->pss_func == POOL_SCAN_SCRUB ||
3911 ps->pss_func == POOL_SCAN_RESILVER);
3912 /*
3913 * Scan is finished or canceled.
3914 */
3915 if (ps->pss_state == DSS_FINISHED) {
3916 uint64_t minutes_taken = (end - start) / 60;
3917 char *fmt = NULL;
3918
3919 if (ps->pss_func == POOL_SCAN_SCRUB) {
3920 fmt = gettext("scrub repaired %s in %lluh%um with "
3921 "%llu errors on %s");
3922 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3923 fmt = gettext("resilvered %s in %lluh%um with "
3924 "%llu errors on %s");
3925 }
3926 /* LINTED */
3927 (void) printf(fmt, processed_buf,
3928 (u_longlong_t)(minutes_taken / 60),
3929 (uint_t)(minutes_taken % 60),
3930 (u_longlong_t)ps->pss_errors,
3931 ctime((time_t *)&end));
3932 return;
3933 } else if (ps->pss_state == DSS_CANCELED) {
3934 if (ps->pss_func == POOL_SCAN_SCRUB) {
3935 (void) printf(gettext("scrub canceled on %s"),
3936 ctime(&end));
3937 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3938 (void) printf(gettext("resilver canceled on %s"),
3939 ctime(&end));
3940 }
3941 return;
3942 }
3943
3944 assert(ps->pss_state == DSS_SCANNING);
3945
3946 /*
3947 * Scan is in progress.
3948 */
3949 if (ps->pss_func == POOL_SCAN_SCRUB) {
3950 (void) printf(gettext("scrub in progress since %s"),
3951 ctime(&start));
3952 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
3953 (void) printf(gettext("resilver in progress since %s"),
3954 ctime(&start));
3955 }
3956
3957 examined = ps->pss_examined ? ps->pss_examined : 1;
3958 total = ps->pss_to_examine;
3959 fraction_done = (double)examined / total;
3960
3961 /* elapsed time for this pass */
3962 elapsed = time(NULL) - ps->pss_pass_start;
3963 elapsed = elapsed ? elapsed : 1;
3964 pass_exam = ps->pss_pass_exam ? ps->pss_pass_exam : 1;
3965 rate = pass_exam / elapsed;
3966 rate = rate ? rate : 1;
3967 mins_left = ((total - examined) / rate) / 60;
3968 hours_left = mins_left / 60;
3969
3970 zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
3971 zfs_nicenum(total, total_buf, sizeof (total_buf));
3972 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
3973
3974 /*
3975 * do not print estimated time if hours_left is more than 30 days
3976 */
3977 (void) printf(gettext(" %s scanned out of %s at %s/s"),
3978 examined_buf, total_buf, rate_buf);
3979 if (hours_left < (30 * 24)) {
3980 (void) printf(gettext(", %lluh%um to go\n"),
3981 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
3982 } else {
3983 (void) printf(gettext(
3984 ", (scan is slow, no estimated time)\n"));
3985 }
3986
3987 if (ps->pss_func == POOL_SCAN_RESILVER) {
3988 (void) printf(gettext(" %s resilvered, %.2f%% done\n"),
3989 processed_buf, 100 * fraction_done);
3990 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
3991 (void) printf(gettext(" %s repaired, %.2f%% done\n"),
3992 processed_buf, 100 * fraction_done);
3993 }
3994 }
3995
3996 static void
3997 print_error_log(zpool_handle_t *zhp)
3998 {
3999 nvlist_t *nverrlist = NULL;
4000 nvpair_t *elem;
4001 char *pathname;
4002 size_t len = MAXPATHLEN * 2;
4003
4004 if (zpool_get_errlog(zhp, &nverrlist) != 0) {
4005 (void) printf("errors: List of errors unavailable "
4006 "(insufficient privileges)\n");
4007 return;
4008 }
4009
4010 (void) printf("errors: Permanent errors have been "
4011 "detected in the following files:\n\n");
4012
4013 pathname = safe_malloc(len);
4014 elem = NULL;
4015 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
4016 nvlist_t *nv;
4017 uint64_t dsobj, obj;
4018
4019 verify(nvpair_value_nvlist(elem, &nv) == 0);
4020 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
4021 &dsobj) == 0);
4022 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
4023 &obj) == 0);
4024 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
4025 (void) printf("%7s %s\n", "", pathname);
4026 }
4027 free(pathname);
4028 nvlist_free(nverrlist);
4029 }
4030
4031 static void
4032 print_spares(zpool_handle_t *zhp, nvlist_t **spares, uint_t nspares,
4033 int namewidth)
4034 {
4035 uint_t i;
4036 char *name;
4037
4038 if (nspares == 0)
4039 return;
4040
4041 (void) printf(gettext("\tspares\n"));
4042
4043 for (i = 0; i < nspares; i++) {
4044 name = zpool_vdev_name(g_zfs, zhp, spares[i], B_FALSE);
4045 print_status_config(zhp, name, spares[i],
4046 namewidth, 2, B_TRUE);
4047 free(name);
4048 }
4049 }
4050
4051 static void
4052 print_l2cache(zpool_handle_t *zhp, nvlist_t **l2cache, uint_t nl2cache,
4053 int namewidth)
4054 {
4055 uint_t i;
4056 char *name;
4057
4058 if (nl2cache == 0)
4059 return;
4060
4061 (void) printf(gettext("\tcache\n"));
4062
4063 for (i = 0; i < nl2cache; i++) {
4064 name = zpool_vdev_name(g_zfs, zhp, l2cache[i], B_FALSE);
4065 print_status_config(zhp, name, l2cache[i],
4066 namewidth, 2, B_FALSE);
4067 free(name);
4068 }
4069 }
4070
4071 static void
4072 print_dedup_stats(nvlist_t *config)
4073 {
4074 ddt_histogram_t *ddh;
4075 ddt_stat_t *dds;
4076 ddt_object_t *ddo;
4077 uint_t c;
4078
4079 /*
4080 * If the pool was faulted then we may not have been able to
4081 * obtain the config. Otherwise, if we have anything in the dedup
4082 * table continue processing the stats.
4083 */
4084 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
4085 (uint64_t **)&ddo, &c) != 0)
4086 return;
4087
4088 (void) printf("\n");
4089 (void) printf(gettext(" dedup: "));
4090 if (ddo->ddo_count == 0) {
4091 (void) printf(gettext("no DDT entries\n"));
4092 return;
4093 }
4094
4095 (void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
4096 (u_longlong_t)ddo->ddo_count,
4097 (u_longlong_t)ddo->ddo_dspace,
4098 (u_longlong_t)ddo->ddo_mspace);
4099
4100 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
4101 (uint64_t **)&dds, &c) == 0);
4102 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
4103 (uint64_t **)&ddh, &c) == 0);
4104 zpool_dump_ddt(dds, ddh);
4105 }
4106
4107 /*
4108 * Display a summary of pool status. Displays a summary such as:
4109 *
4110 * pool: tank
4111 * status: DEGRADED
4112 * reason: One or more devices ...
4113 * see: http://zfsonlinux.org/msg/ZFS-xxxx-01
4114 * config:
4115 * mirror DEGRADED
4116 * c1t0d0 OK
4117 * c2t0d0 UNAVAIL
4118 *
4119 * When given the '-v' option, we print out the complete config. If the '-e'
4120 * option is specified, then we print out error rate information as well.
4121 */
4122 int
4123 status_callback(zpool_handle_t *zhp, void *data)
4124 {
4125 status_cbdata_t *cbp = data;
4126 nvlist_t *config, *nvroot;
4127 char *msgid;
4128 int reason;
4129 const char *health;
4130 uint_t c;
4131 vdev_stat_t *vs;
4132
4133 config = zpool_get_config(zhp, NULL);
4134 reason = zpool_get_status(zhp, &msgid);
4135
4136 cbp->cb_count++;
4137
4138 /*
4139 * If we were given 'zpool status -x', only report those pools with
4140 * problems.
4141 */
4142 if (cbp->cb_explain &&
4143 (reason == ZPOOL_STATUS_OK ||
4144 reason == ZPOOL_STATUS_VERSION_OLDER ||
4145 reason == ZPOOL_STATUS_FEAT_DISABLED)) {
4146 if (!cbp->cb_allpools) {
4147 (void) printf(gettext("pool '%s' is healthy\n"),
4148 zpool_get_name(zhp));
4149 if (cbp->cb_first)
4150 cbp->cb_first = B_FALSE;
4151 }
4152 return (0);
4153 }
4154
4155 if (cbp->cb_first)
4156 cbp->cb_first = B_FALSE;
4157 else
4158 (void) printf("\n");
4159
4160 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4161 &nvroot) == 0);
4162 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
4163 (uint64_t **)&vs, &c) == 0);
4164 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
4165
4166 (void) printf(gettext(" pool: %s\n"), zpool_get_name(zhp));
4167 (void) printf(gettext(" state: %s\n"), health);
4168
4169 switch (reason) {
4170 case ZPOOL_STATUS_MISSING_DEV_R:
4171 (void) printf(gettext("status: One or more devices could not "
4172 "be opened. Sufficient replicas exist for\n\tthe pool to "
4173 "continue functioning in a degraded state.\n"));
4174 (void) printf(gettext("action: Attach the missing device and "
4175 "online it using 'zpool online'.\n"));
4176 break;
4177
4178 case ZPOOL_STATUS_MISSING_DEV_NR:
4179 (void) printf(gettext("status: One or more devices could not "
4180 "be opened. There are insufficient\n\treplicas for the "
4181 "pool to continue functioning.\n"));
4182 (void) printf(gettext("action: Attach the missing device and "
4183 "online it using 'zpool online'.\n"));
4184 break;
4185
4186 case ZPOOL_STATUS_CORRUPT_LABEL_R:
4187 (void) printf(gettext("status: One or more devices could not "
4188 "be used because the label is missing or\n\tinvalid. "
4189 "Sufficient replicas exist for the pool to continue\n\t"
4190 "functioning in a degraded state.\n"));
4191 (void) printf(gettext("action: Replace the device using "
4192 "'zpool replace'.\n"));
4193 break;
4194
4195 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
4196 (void) printf(gettext("status: One or more devices could not "
4197 "be used because the label is missing \n\tor invalid. "
4198 "There are insufficient replicas for the pool to "
4199 "continue\n\tfunctioning.\n"));
4200 zpool_explain_recover(zpool_get_handle(zhp),
4201 zpool_get_name(zhp), reason, config);
4202 break;
4203
4204 case ZPOOL_STATUS_FAILING_DEV:
4205 (void) printf(gettext("status: One or more devices has "
4206 "experienced an unrecoverable error. An\n\tattempt was "
4207 "made to correct the error. Applications are "
4208 "unaffected.\n"));
4209 (void) printf(gettext("action: Determine if the device needs "
4210 "to be replaced, and clear the errors\n\tusing "
4211 "'zpool clear' or replace the device with 'zpool "
4212 "replace'.\n"));
4213 break;
4214
4215 case ZPOOL_STATUS_OFFLINE_DEV:
4216 (void) printf(gettext("status: One or more devices has "
4217 "been taken offline by the administrator.\n\tSufficient "
4218 "replicas exist for the pool to continue functioning in "
4219 "a\n\tdegraded state.\n"));
4220 (void) printf(gettext("action: Online the device using "
4221 "'zpool online' or replace the device with\n\t'zpool "
4222 "replace'.\n"));
4223 break;
4224
4225 case ZPOOL_STATUS_REMOVED_DEV:
4226 (void) printf(gettext("status: One or more devices has "
4227 "been removed by the administrator.\n\tSufficient "
4228 "replicas exist for the pool to continue functioning in "
4229 "a\n\tdegraded state.\n"));
4230 (void) printf(gettext("action: Online the device using "
4231 "'zpool online' or replace the device with\n\t'zpool "
4232 "replace'.\n"));
4233 break;
4234
4235 case ZPOOL_STATUS_RESILVERING:
4236 (void) printf(gettext("status: One or more devices is "
4237 "currently being resilvered. The pool will\n\tcontinue "
4238 "to function, possibly in a degraded state.\n"));
4239 (void) printf(gettext("action: Wait for the resilver to "
4240 "complete.\n"));
4241 break;
4242
4243 case ZPOOL_STATUS_CORRUPT_DATA:
4244 (void) printf(gettext("status: One or more devices has "
4245 "experienced an error resulting in data\n\tcorruption. "
4246 "Applications may be affected.\n"));
4247 (void) printf(gettext("action: Restore the file in question "
4248 "if possible. Otherwise restore the\n\tentire pool from "
4249 "backup.\n"));
4250 break;
4251
4252 case ZPOOL_STATUS_CORRUPT_POOL:
4253 (void) printf(gettext("status: The pool metadata is corrupted "
4254 "and the pool cannot be opened.\n"));
4255 zpool_explain_recover(zpool_get_handle(zhp),
4256 zpool_get_name(zhp), reason, config);
4257 break;
4258
4259 case ZPOOL_STATUS_VERSION_OLDER:
4260 (void) printf(gettext("status: The pool is formatted using a "
4261 "legacy on-disk format. The pool can\n\tstill be used, "
4262 "but some features are unavailable.\n"));
4263 (void) printf(gettext("action: Upgrade the pool using 'zpool "
4264 "upgrade'. Once this is done, the\n\tpool will no longer "
4265 "be accessible on software that does not support\n\t"
4266 "feature flags.\n"));
4267 break;
4268
4269 case ZPOOL_STATUS_VERSION_NEWER:
4270 (void) printf(gettext("status: The pool has been upgraded to a "
4271 "newer, incompatible on-disk version.\n\tThe pool cannot "
4272 "be accessed on this system.\n"));
4273 (void) printf(gettext("action: Access the pool from a system "
4274 "running more recent software, or\n\trestore the pool from "
4275 "backup.\n"));
4276 break;
4277
4278 case ZPOOL_STATUS_FEAT_DISABLED:
4279 (void) printf(gettext("status: Some supported features are not "
4280 "enabled on the pool. The pool can\n\tstill be used, but "
4281 "some features are unavailable.\n"));
4282 (void) printf(gettext("action: Enable all features using "
4283 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
4284 "longer be accessible by software that does not support\n\t"
4285 "the features. See zpool-features(5) for details.\n"));
4286 break;
4287
4288 case ZPOOL_STATUS_UNSUP_FEAT_READ:
4289 (void) printf(gettext("status: The pool cannot be accessed on "
4290 "this system because it uses the\n\tfollowing feature(s) "
4291 "not supported on this system:\n"));
4292 zpool_print_unsup_feat(config);
4293 (void) printf("\n");
4294 (void) printf(gettext("action: Access the pool from a system "
4295 "that supports the required feature(s),\n\tor restore the "
4296 "pool from backup.\n"));
4297 break;
4298
4299 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
4300 (void) printf(gettext("status: The pool can only be accessed "
4301 "in read-only mode on this system. It\n\tcannot be "
4302 "accessed in read-write mode because it uses the "
4303 "following\n\tfeature(s) not supported on this system:\n"));
4304 zpool_print_unsup_feat(config);
4305 (void) printf("\n");
4306 (void) printf(gettext("action: The pool cannot be accessed in "
4307 "read-write mode. Import the pool with\n"
4308 "\t\"-o readonly=on\", access the pool from a system that "
4309 "supports the\n\trequired feature(s), or restore the "
4310 "pool from backup.\n"));
4311 break;
4312
4313 case ZPOOL_STATUS_FAULTED_DEV_R:
4314 (void) printf(gettext("status: One or more devices are "
4315 "faulted in response to persistent errors.\n\tSufficient "
4316 "replicas exist for the pool to continue functioning "
4317 "in a\n\tdegraded state.\n"));
4318 (void) printf(gettext("action: Replace the faulted device, "
4319 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
4320 break;
4321
4322 case ZPOOL_STATUS_FAULTED_DEV_NR:
4323 (void) printf(gettext("status: One or more devices are "
4324 "faulted in response to persistent errors. There are "
4325 "insufficient replicas for the pool to\n\tcontinue "
4326 "functioning.\n"));
4327 (void) printf(gettext("action: Destroy and re-create the pool "
4328 "from a backup source. Manually marking the device\n"
4329 "\trepaired using 'zpool clear' may allow some data "
4330 "to be recovered.\n"));
4331 break;
4332
4333 case ZPOOL_STATUS_IO_FAILURE_WAIT:
4334 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
4335 (void) printf(gettext("status: One or more devices are "
4336 "faulted in response to IO failures.\n"));
4337 (void) printf(gettext("action: Make sure the affected devices "
4338 "are connected, then run 'zpool clear'.\n"));
4339 break;
4340
4341 case ZPOOL_STATUS_BAD_LOG:
4342 (void) printf(gettext("status: An intent log record "
4343 "could not be read.\n"
4344 "\tWaiting for adminstrator intervention to fix the "
4345 "faulted pool.\n"));
4346 (void) printf(gettext("action: Either restore the affected "
4347 "device(s) and run 'zpool online',\n"
4348 "\tor ignore the intent log records by running "
4349 "'zpool clear'.\n"));
4350 break;
4351
4352 default:
4353 /*
4354 * The remaining errors can't actually be generated, yet.
4355 */
4356 assert(reason == ZPOOL_STATUS_OK);
4357 }
4358
4359 if (msgid != NULL)
4360 (void) printf(gettext(" see: http://zfsonlinux.org/msg/%s\n"),
4361 msgid);
4362
4363 if (config != NULL) {
4364 int namewidth;
4365 uint64_t nerr;
4366 nvlist_t **spares, **l2cache;
4367 uint_t nspares, nl2cache;
4368 pool_scan_stat_t *ps = NULL;
4369
4370 (void) nvlist_lookup_uint64_array(nvroot,
4371 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &c);
4372 print_scan_status(ps);
4373
4374 namewidth = max_width(zhp, nvroot, 0, 0);
4375 if (namewidth < 10)
4376 namewidth = 10;
4377
4378 (void) printf(gettext("config:\n\n"));
4379 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s\n"), namewidth,
4380 "NAME", "STATE", "READ", "WRITE", "CKSUM");
4381 print_status_config(zhp, zpool_get_name(zhp), nvroot,
4382 namewidth, 0, B_FALSE);
4383
4384 if (num_logs(nvroot) > 0)
4385 print_logs(zhp, nvroot, namewidth, B_TRUE);
4386 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4387 &l2cache, &nl2cache) == 0)
4388 print_l2cache(zhp, l2cache, nl2cache, namewidth);
4389
4390 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4391 &spares, &nspares) == 0)
4392 print_spares(zhp, spares, nspares, namewidth);
4393
4394 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
4395 &nerr) == 0) {
4396 nvlist_t *nverrlist = NULL;
4397
4398 /*
4399 * If the approximate error count is small, get a
4400 * precise count by fetching the entire log and
4401 * uniquifying the results.
4402 */
4403 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
4404 zpool_get_errlog(zhp, &nverrlist) == 0) {
4405 nvpair_t *elem;
4406
4407 elem = NULL;
4408 nerr = 0;
4409 while ((elem = nvlist_next_nvpair(nverrlist,
4410 elem)) != NULL) {
4411 nerr++;
4412 }
4413 }
4414 nvlist_free(nverrlist);
4415
4416 (void) printf("\n");
4417
4418 if (nerr == 0)
4419 (void) printf(gettext("errors: No known data "
4420 "errors\n"));
4421 else if (!cbp->cb_verbose)
4422 (void) printf(gettext("errors: %llu data "
4423 "errors, use '-v' for a list\n"),
4424 (u_longlong_t)nerr);
4425 else
4426 print_error_log(zhp);
4427 }
4428
4429 if (cbp->cb_dedup_stats)
4430 print_dedup_stats(config);
4431 } else {
4432 (void) printf(gettext("config: The configuration cannot be "
4433 "determined.\n"));
4434 }
4435
4436 return (0);
4437 }
4438
4439 /*
4440 * zpool status [-vx] [-T d|u] [pool] ... [interval [count]]
4441 *
4442 * -v Display complete error logs
4443 * -x Display only pools with potential problems
4444 * -D Display dedup status (undocumented)
4445 * -T Display a timestamp in date(1) or Unix format
4446 *
4447 * Describes the health status of all pools or some subset.
4448 */
4449 int
4450 zpool_do_status(int argc, char **argv)
4451 {
4452 int c;
4453 int ret;
4454 unsigned long interval = 0, count = 0;
4455 status_cbdata_t cb = { 0 };
4456
4457 /* check options */
4458 while ((c = getopt(argc, argv, "vxDT:")) != -1) {
4459 switch (c) {
4460 case 'v':
4461 cb.cb_verbose = B_TRUE;
4462 break;
4463 case 'x':
4464 cb.cb_explain = B_TRUE;
4465 break;
4466 case 'D':
4467 cb.cb_dedup_stats = B_TRUE;
4468 break;
4469 case 'T':
4470 get_timestamp_arg(*optarg);
4471 break;
4472 case '?':
4473 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4474 optopt);
4475 usage(B_FALSE);
4476 }
4477 }
4478
4479 argc -= optind;
4480 argv += optind;
4481
4482 get_interval_count(&argc, argv, &interval, &count);
4483
4484 if (argc == 0)
4485 cb.cb_allpools = B_TRUE;
4486
4487 cb.cb_first = B_TRUE;
4488
4489 for (;;) {
4490 if (timestamp_fmt != NODATE)
4491 print_timestamp(timestamp_fmt);
4492
4493 ret = for_each_pool(argc, argv, B_TRUE, NULL,
4494 status_callback, &cb);
4495
4496 if (argc == 0 && cb.cb_count == 0)
4497 (void) fprintf(stderr, gettext("no pools available\n"));
4498 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
4499 (void) printf(gettext("all pools are healthy\n"));
4500
4501 if (ret != 0)
4502 return (ret);
4503
4504 if (interval == 0)
4505 break;
4506
4507 if (count != 0 && --count == 0)
4508 break;
4509
4510 (void) sleep(interval);
4511 }
4512
4513 return (0);
4514 }
4515
4516 typedef struct upgrade_cbdata {
4517 int cb_first;
4518 int cb_argc;
4519 uint64_t cb_version;
4520 char **cb_argv;
4521 } upgrade_cbdata_t;
4522
4523 static int
4524 upgrade_version(zpool_handle_t *zhp, uint64_t version)
4525 {
4526 int ret;
4527 nvlist_t *config;
4528 uint64_t oldversion;
4529
4530 config = zpool_get_config(zhp, NULL);
4531 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4532 &oldversion) == 0);
4533
4534 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
4535 assert(oldversion < version);
4536
4537 ret = zpool_upgrade(zhp, version);
4538 if (ret != 0)
4539 return (ret);
4540
4541 if (version >= SPA_VERSION_FEATURES) {
4542 (void) printf(gettext("Successfully upgraded "
4543 "'%s' from version %llu to feature flags.\n"),
4544 zpool_get_name(zhp), (u_longlong_t) oldversion);
4545 } else {
4546 (void) printf(gettext("Successfully upgraded "
4547 "'%s' from version %llu to version %llu.\n"),
4548 zpool_get_name(zhp), (u_longlong_t) oldversion,
4549 (u_longlong_t) version);
4550 }
4551
4552 return (0);
4553 }
4554
4555 static int
4556 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
4557 {
4558 int i, ret, count;
4559 boolean_t firstff = B_TRUE;
4560 nvlist_t *enabled = zpool_get_features(zhp);
4561
4562 count = 0;
4563 for (i = 0; i < SPA_FEATURES; i++) {
4564 const char *fname = spa_feature_table[i].fi_uname;
4565 const char *fguid = spa_feature_table[i].fi_guid;
4566 if (!nvlist_exists(enabled, fguid)) {
4567 char *propname;
4568 verify(-1 != asprintf(&propname, "feature@%s", fname));
4569 ret = zpool_set_prop(zhp, propname,
4570 ZFS_FEATURE_ENABLED);
4571 if (ret != 0) {
4572 free(propname);
4573 return (ret);
4574 }
4575 count++;
4576
4577 if (firstff) {
4578 (void) printf(gettext("Enabled the "
4579 "following features on '%s':\n"),
4580 zpool_get_name(zhp));
4581 firstff = B_FALSE;
4582 }
4583 (void) printf(gettext(" %s\n"), fname);
4584 free(propname);
4585 }
4586 }
4587
4588 if (countp != NULL)
4589 *countp = count;
4590 return (0);
4591 }
4592
4593 static int
4594 upgrade_cb(zpool_handle_t *zhp, void *arg)
4595 {
4596 upgrade_cbdata_t *cbp = arg;
4597 nvlist_t *config;
4598 uint64_t version;
4599 boolean_t printnl = B_FALSE;
4600 int ret;
4601
4602 config = zpool_get_config(zhp, NULL);
4603 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4604 &version) == 0);
4605
4606 assert(SPA_VERSION_IS_SUPPORTED(version));
4607
4608 if (version < cbp->cb_version) {
4609 cbp->cb_first = B_FALSE;
4610 ret = upgrade_version(zhp, cbp->cb_version);
4611 if (ret != 0)
4612 return (ret);
4613 printnl = B_TRUE;
4614
4615 /*
4616 * If they did "zpool upgrade -a", then we could
4617 * be doing ioctls to different pools. We need
4618 * to log this history once to each pool, and bypass
4619 * the normal history logging that happens in main().
4620 */
4621 (void) zpool_log_history(g_zfs, history_str);
4622 log_history = B_FALSE;
4623 }
4624
4625 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4626 int count;
4627 ret = upgrade_enable_all(zhp, &count);
4628 if (ret != 0)
4629 return (ret);
4630
4631 if (count > 0) {
4632 cbp->cb_first = B_FALSE;
4633 printnl = B_TRUE;
4634 }
4635 }
4636
4637 if (printnl) {
4638 (void) printf(gettext("\n"));
4639 }
4640
4641 return (0);
4642 }
4643
4644 static int
4645 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
4646 {
4647 upgrade_cbdata_t *cbp = arg;
4648 nvlist_t *config;
4649 uint64_t version;
4650
4651 config = zpool_get_config(zhp, NULL);
4652 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4653 &version) == 0);
4654
4655 assert(SPA_VERSION_IS_SUPPORTED(version));
4656
4657 if (version < SPA_VERSION_FEATURES) {
4658 if (cbp->cb_first) {
4659 (void) printf(gettext("The following pools are "
4660 "formatted with legacy version numbers and can\n"
4661 "be upgraded to use feature flags. After "
4662 "being upgraded, these pools\nwill no "
4663 "longer be accessible by software that does not "
4664 "support feature\nflags.\n\n"));
4665 (void) printf(gettext("VER POOL\n"));
4666 (void) printf(gettext("--- ------------\n"));
4667 cbp->cb_first = B_FALSE;
4668 }
4669
4670 (void) printf("%2llu %s\n", (u_longlong_t)version,
4671 zpool_get_name(zhp));
4672 }
4673
4674 return (0);
4675 }
4676
4677 static int
4678 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
4679 {
4680 upgrade_cbdata_t *cbp = arg;
4681 nvlist_t *config;
4682 uint64_t version;
4683
4684 config = zpool_get_config(zhp, NULL);
4685 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4686 &version) == 0);
4687
4688 if (version >= SPA_VERSION_FEATURES) {
4689 int i;
4690 boolean_t poolfirst = B_TRUE;
4691 nvlist_t *enabled = zpool_get_features(zhp);
4692
4693 for (i = 0; i < SPA_FEATURES; i++) {
4694 const char *fguid = spa_feature_table[i].fi_guid;
4695 const char *fname = spa_feature_table[i].fi_uname;
4696 if (!nvlist_exists(enabled, fguid)) {
4697 if (cbp->cb_first) {
4698 (void) printf(gettext("\nSome "
4699 "supported features are not "
4700 "enabled on the following pools. "
4701 "Once a\nfeature is enabled the "
4702 "pool may become incompatible with "
4703 "software\nthat does not support "
4704 "the feature. See "
4705 "zpool-features(5) for "
4706 "details.\n\n"));
4707 (void) printf(gettext("POOL "
4708 "FEATURE\n"));
4709 (void) printf(gettext("------"
4710 "---------\n"));
4711 cbp->cb_first = B_FALSE;
4712 }
4713
4714 if (poolfirst) {
4715 (void) printf(gettext("%s\n"),
4716 zpool_get_name(zhp));
4717 poolfirst = B_FALSE;
4718 }
4719
4720 (void) printf(gettext(" %s\n"), fname);
4721 }
4722 /*
4723 * If they did "zpool upgrade -a", then we could
4724 * be doing ioctls to different pools. We need
4725 * to log this history once to each pool, and bypass
4726 * the normal history logging that happens in main().
4727 */
4728 (void) zpool_log_history(g_zfs, history_str);
4729 log_history = B_FALSE;
4730 }
4731 }
4732
4733 return (0);
4734 }
4735
4736 /* ARGSUSED */
4737 static int
4738 upgrade_one(zpool_handle_t *zhp, void *data)
4739 {
4740 boolean_t printnl = B_FALSE;
4741 upgrade_cbdata_t *cbp = data;
4742 uint64_t cur_version;
4743 int ret;
4744
4745 if (strcmp("log", zpool_get_name(zhp)) == 0) {
4746 (void) printf(gettext("'log' is now a reserved word\n"
4747 "Pool 'log' must be renamed using export and import"
4748 " to upgrade.\n"));
4749 return (1);
4750 }
4751
4752 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4753 if (cur_version > cbp->cb_version) {
4754 (void) printf(gettext("Pool '%s' is already formatted "
4755 "using more current version '%llu'.\n\n"),
4756 zpool_get_name(zhp), (u_longlong_t) cur_version);
4757 return (0);
4758 }
4759
4760 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
4761 (void) printf(gettext("Pool '%s' is already formatted "
4762 "using version %llu.\n\n"), zpool_get_name(zhp),
4763 (u_longlong_t) cbp->cb_version);
4764 return (0);
4765 }
4766
4767 if (cur_version != cbp->cb_version) {
4768 printnl = B_TRUE;
4769 ret = upgrade_version(zhp, cbp->cb_version);
4770 if (ret != 0)
4771 return (ret);
4772 }
4773
4774 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
4775 int count = 0;
4776 ret = upgrade_enable_all(zhp, &count);
4777 if (ret != 0)
4778 return (ret);
4779
4780 if (count != 0) {
4781 printnl = B_TRUE;
4782 } else if (cur_version == SPA_VERSION) {
4783 (void) printf(gettext("Pool '%s' already has all "
4784 "supported features enabled.\n"),
4785 zpool_get_name(zhp));
4786 }
4787 }
4788
4789 if (printnl) {
4790 (void) printf(gettext("\n"));
4791 }
4792
4793 return (0);
4794 }
4795
4796 /*
4797 * zpool upgrade
4798 * zpool upgrade -v
4799 * zpool upgrade [-V version] <-a | pool ...>
4800 *
4801 * With no arguments, display downrev'd ZFS pool available for upgrade.
4802 * Individual pools can be upgraded by specifying the pool, and '-a' will
4803 * upgrade all pools.
4804 */
4805 int
4806 zpool_do_upgrade(int argc, char **argv)
4807 {
4808 int c;
4809 upgrade_cbdata_t cb = { 0 };
4810 int ret = 0;
4811 boolean_t showversions = B_FALSE;
4812 boolean_t upgradeall = B_FALSE;
4813 char *end;
4814
4815
4816 /* check options */
4817 while ((c = getopt(argc, argv, ":avV:")) != -1) {
4818 switch (c) {
4819 case 'a':
4820 upgradeall = B_TRUE;
4821 break;
4822 case 'v':
4823 showversions = B_TRUE;
4824 break;
4825 case 'V':
4826 cb.cb_version = strtoll(optarg, &end, 10);
4827 if (*end != '\0' ||
4828 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
4829 (void) fprintf(stderr,
4830 gettext("invalid version '%s'\n"), optarg);
4831 usage(B_FALSE);
4832 }
4833 break;
4834 case ':':
4835 (void) fprintf(stderr, gettext("missing argument for "
4836 "'%c' option\n"), optopt);
4837 usage(B_FALSE);
4838 break;
4839 case '?':
4840 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4841 optopt);
4842 usage(B_FALSE);
4843 }
4844 }
4845
4846 cb.cb_argc = argc;
4847 cb.cb_argv = argv;
4848 argc -= optind;
4849 argv += optind;
4850
4851 if (cb.cb_version == 0) {
4852 cb.cb_version = SPA_VERSION;
4853 } else if (!upgradeall && argc == 0) {
4854 (void) fprintf(stderr, gettext("-V option is "
4855 "incompatible with other arguments\n"));
4856 usage(B_FALSE);
4857 }
4858
4859 if (showversions) {
4860 if (upgradeall || argc != 0) {
4861 (void) fprintf(stderr, gettext("-v option is "
4862 "incompatible with other arguments\n"));
4863 usage(B_FALSE);
4864 }
4865 } else if (upgradeall) {
4866 if (argc != 0) {
4867 (void) fprintf(stderr, gettext("-a option should not "
4868 "be used along with a pool name\n"));
4869 usage(B_FALSE);
4870 }
4871 }
4872
4873 (void) printf(gettext("This system supports ZFS pool feature "
4874 "flags.\n\n"));
4875 if (showversions) {
4876 int i;
4877
4878 (void) printf(gettext("The following features are "
4879 "supported:\n\n"));
4880 (void) printf(gettext("FEAT DESCRIPTION\n"));
4881 (void) printf("----------------------------------------------"
4882 "---------------\n");
4883 for (i = 0; i < SPA_FEATURES; i++) {
4884 zfeature_info_t *fi = &spa_feature_table[i];
4885 const char *ro = fi->fi_can_readonly ?
4886 " (read-only compatible)" : "";
4887
4888 (void) printf("%-37s%s\n", fi->fi_uname, ro);
4889 (void) printf(" %s\n", fi->fi_desc);
4890 }
4891 (void) printf("\n");
4892
4893 (void) printf(gettext("The following legacy versions are also "
4894 "supported:\n\n"));
4895 (void) printf(gettext("VER DESCRIPTION\n"));
4896 (void) printf("--- -----------------------------------------"
4897 "---------------\n");
4898 (void) printf(gettext(" 1 Initial ZFS version\n"));
4899 (void) printf(gettext(" 2 Ditto blocks "
4900 "(replicated metadata)\n"));
4901 (void) printf(gettext(" 3 Hot spares and double parity "
4902 "RAID-Z\n"));
4903 (void) printf(gettext(" 4 zpool history\n"));
4904 (void) printf(gettext(" 5 Compression using the gzip "
4905 "algorithm\n"));
4906 (void) printf(gettext(" 6 bootfs pool property\n"));
4907 (void) printf(gettext(" 7 Separate intent log devices\n"));
4908 (void) printf(gettext(" 8 Delegated administration\n"));
4909 (void) printf(gettext(" 9 refquota and refreservation "
4910 "properties\n"));
4911 (void) printf(gettext(" 10 Cache devices\n"));
4912 (void) printf(gettext(" 11 Improved scrub performance\n"));
4913 (void) printf(gettext(" 12 Snapshot properties\n"));
4914 (void) printf(gettext(" 13 snapused property\n"));
4915 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
4916 (void) printf(gettext(" 15 user/group space accounting\n"));
4917 (void) printf(gettext(" 16 stmf property support\n"));
4918 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
4919 (void) printf(gettext(" 18 Snapshot user holds\n"));
4920 (void) printf(gettext(" 19 Log device removal\n"));
4921 (void) printf(gettext(" 20 Compression using zle "
4922 "(zero-length encoding)\n"));
4923 (void) printf(gettext(" 21 Deduplication\n"));
4924 (void) printf(gettext(" 22 Received properties\n"));
4925 (void) printf(gettext(" 23 Slim ZIL\n"));
4926 (void) printf(gettext(" 24 System attributes\n"));
4927 (void) printf(gettext(" 25 Improved scrub stats\n"));
4928 (void) printf(gettext(" 26 Improved snapshot deletion "
4929 "performance\n"));
4930 (void) printf(gettext(" 27 Improved snapshot creation "
4931 "performance\n"));
4932 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
4933 (void) printf(gettext("\nFor more information on a particular "
4934 "version, including supported releases,\n"));
4935 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
4936 } else if (argc == 0 && upgradeall) {
4937 cb.cb_first = B_TRUE;
4938 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
4939 if (ret == 0 && cb.cb_first) {
4940 if (cb.cb_version == SPA_VERSION) {
4941 (void) printf(gettext("All pools are already "
4942 "formatted using feature flags.\n\n"));
4943 (void) printf(gettext("Every feature flags "
4944 "pool already has all supported features "
4945 "enabled.\n"));
4946 } else {
4947 (void) printf(gettext("All pools are already "
4948 "formatted with version %llu or higher.\n"),
4949 (u_longlong_t) cb.cb_version);
4950 }
4951 }
4952 } else if (argc == 0) {
4953 cb.cb_first = B_TRUE;
4954 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
4955 assert(ret == 0);
4956
4957 if (cb.cb_first) {
4958 (void) printf(gettext("All pools are formatted "
4959 "using feature flags.\n\n"));
4960 } else {
4961 (void) printf(gettext("\nUse 'zpool upgrade -v' "
4962 "for a list of available legacy versions.\n"));
4963 }
4964
4965 cb.cb_first = B_TRUE;
4966 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
4967 assert(ret == 0);
4968
4969 if (cb.cb_first) {
4970 (void) printf(gettext("Every feature flags pool has "
4971 "all supported features enabled.\n"));
4972 } else {
4973 (void) printf(gettext("\n"));
4974 }
4975 } else {
4976 ret = for_each_pool(argc, argv, B_FALSE, NULL,
4977 upgrade_one, &cb);
4978 }
4979
4980 return (ret);
4981 }
4982
4983 typedef struct hist_cbdata {
4984 boolean_t first;
4985 boolean_t longfmt;
4986 boolean_t internal;
4987 } hist_cbdata_t;
4988
4989 /*
4990 * Print out the command history for a specific pool.
4991 */
4992 static int
4993 get_history_one(zpool_handle_t *zhp, void *data)
4994 {
4995 nvlist_t *nvhis;
4996 nvlist_t **records;
4997 uint_t numrecords;
4998 int ret, i;
4999 hist_cbdata_t *cb = (hist_cbdata_t *)data;
5000
5001 cb->first = B_FALSE;
5002
5003 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
5004
5005 if ((ret = zpool_get_history(zhp, &nvhis)) != 0)
5006 return (ret);
5007
5008 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
5009 &records, &numrecords) == 0);
5010 for (i = 0; i < numrecords; i++) {
5011 nvlist_t *rec = records[i];
5012 char tbuf[30] = "";
5013
5014 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
5015 time_t tsec;
5016 struct tm t;
5017
5018 tsec = fnvlist_lookup_uint64(records[i],
5019 ZPOOL_HIST_TIME);
5020 (void) localtime_r(&tsec, &t);
5021 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
5022 }
5023
5024 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
5025 (void) printf("%s %s", tbuf,
5026 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
5027 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
5028 int ievent =
5029 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
5030 if (!cb->internal)
5031 continue;
5032 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
5033 (void) printf("%s unrecognized record:\n",
5034 tbuf);
5035 dump_nvlist(rec, 4);
5036 continue;
5037 }
5038 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
5039 zfs_history_event_names[ievent],
5040 (long long int)fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5041 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
5042 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
5043 if (!cb->internal)
5044 continue;
5045 (void) printf("%s [txg:%lld] %s", tbuf,
5046 (long long int)fnvlist_lookup_uint64(rec, ZPOOL_HIST_TXG),
5047 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
5048 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
5049 (void) printf(" %s (%llu)",
5050 fnvlist_lookup_string(rec,
5051 ZPOOL_HIST_DSNAME),
5052 (long long unsigned int)fnvlist_lookup_uint64(rec,
5053 ZPOOL_HIST_DSID));
5054 }
5055 (void) printf(" %s", fnvlist_lookup_string(rec,
5056 ZPOOL_HIST_INT_STR));
5057 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
5058 if (!cb->internal)
5059 continue;
5060 (void) printf("%s ioctl %s\n", tbuf,
5061 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
5062 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
5063 (void) printf(" input:\n");
5064 dump_nvlist(fnvlist_lookup_nvlist(rec,
5065 ZPOOL_HIST_INPUT_NVL), 8);
5066 }
5067 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
5068 (void) printf(" output:\n");
5069 dump_nvlist(fnvlist_lookup_nvlist(rec,
5070 ZPOOL_HIST_OUTPUT_NVL), 8);
5071 }
5072 } else {
5073 if (!cb->internal)
5074 continue;
5075 (void) printf("%s unrecognized record:\n", tbuf);
5076 dump_nvlist(rec, 4);
5077 }
5078
5079 if (!cb->longfmt) {
5080 (void) printf("\n");
5081 continue;
5082 }
5083 (void) printf(" [");
5084 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
5085 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
5086 struct passwd *pwd = getpwuid(who);
5087 (void) printf("user %d ", (int)who);
5088 if (pwd != NULL)
5089 (void) printf("(%s) ", pwd->pw_name);
5090 }
5091 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
5092 (void) printf("on %s",
5093 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
5094 }
5095 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
5096 (void) printf(":%s",
5097 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
5098 }
5099
5100 (void) printf("]");
5101 (void) printf("\n");
5102 }
5103 (void) printf("\n");
5104 nvlist_free(nvhis);
5105
5106 return (ret);
5107 }
5108
5109 /*
5110 * zpool history <pool>
5111 *
5112 * Displays the history of commands that modified pools.
5113 */
5114 int
5115 zpool_do_history(int argc, char **argv)
5116 {
5117 hist_cbdata_t cbdata = { 0 };
5118 int ret;
5119 int c;
5120
5121 cbdata.first = B_TRUE;
5122 /* check options */
5123 while ((c = getopt(argc, argv, "li")) != -1) {
5124 switch (c) {
5125 case 'l':
5126 cbdata.longfmt = B_TRUE;
5127 break;
5128 case 'i':
5129 cbdata.internal = B_TRUE;
5130 break;
5131 case '?':
5132 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5133 optopt);
5134 usage(B_FALSE);
5135 }
5136 }
5137 argc -= optind;
5138 argv += optind;
5139
5140 ret = for_each_pool(argc, argv, B_FALSE, NULL, get_history_one,
5141 &cbdata);
5142
5143 if (argc == 0 && cbdata.first == B_TRUE) {
5144 (void) fprintf(stderr, gettext("no pools available\n"));
5145 return (0);
5146 }
5147
5148 return (ret);
5149 }
5150
5151 typedef struct ev_opts {
5152 int verbose;
5153 int scripted;
5154 int follow;
5155 int clear;
5156 } ev_opts_t;
5157
5158 static void
5159 zpool_do_events_short(nvlist_t *nvl)
5160 {
5161 char ctime_str[26], str[32], *ptr;
5162 int64_t *tv;
5163 uint_t n;
5164
5165 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
5166 memset(str, ' ', 32);
5167 (void) ctime_r((const time_t *)&tv[0], ctime_str);
5168 (void) strncpy(str, ctime_str+4, 6); /* 'Jun 30' */
5169 (void) strncpy(str+7, ctime_str+20, 4); /* '1993' */
5170 (void) strncpy(str+12, ctime_str+11, 8); /* '21:49:08' */
5171 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]);/* '.123456789' */
5172 (void) printf(gettext("%s "), str);
5173
5174 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
5175 (void) printf(gettext("%s\n"), ptr);
5176 }
5177
5178 static void
5179 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
5180 {
5181 nvpair_t *nvp;
5182
5183 for (nvp = nvlist_next_nvpair(nvl, NULL);
5184 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
5185
5186 data_type_t type = nvpair_type(nvp);
5187 const char *name = nvpair_name(nvp);
5188
5189 boolean_t b;
5190 uint8_t i8;
5191 uint16_t i16;
5192 uint32_t i32;
5193 uint64_t i64;
5194 char *str;
5195 nvlist_t *cnv;
5196
5197 printf(gettext("%*s%s = "), depth, "", name);
5198
5199 switch (type) {
5200 case DATA_TYPE_BOOLEAN:
5201 printf(gettext("%s"), "1");
5202 break;
5203
5204 case DATA_TYPE_BOOLEAN_VALUE:
5205 (void) nvpair_value_boolean_value(nvp, &b);
5206 printf(gettext("%s"), b ? "1" : "0");
5207 break;
5208
5209 case DATA_TYPE_BYTE:
5210 (void) nvpair_value_byte(nvp, &i8);
5211 printf(gettext("0x%x"), i8);
5212 break;
5213
5214 case DATA_TYPE_INT8:
5215 (void) nvpair_value_int8(nvp, (void *)&i8);
5216 printf(gettext("0x%x"), i8);
5217 break;
5218
5219 case DATA_TYPE_UINT8:
5220 (void) nvpair_value_uint8(nvp, &i8);
5221 printf(gettext("0x%x"), i8);
5222 break;
5223
5224 case DATA_TYPE_INT16:
5225 (void) nvpair_value_int16(nvp, (void *)&i16);
5226 printf(gettext("0x%x"), i16);
5227 break;
5228
5229 case DATA_TYPE_UINT16:
5230 (void) nvpair_value_uint16(nvp, &i16);
5231 printf(gettext("0x%x"), i16);
5232 break;
5233
5234 case DATA_TYPE_INT32:
5235 (void) nvpair_value_int32(nvp, (void *)&i32);
5236 printf(gettext("0x%x"), i32);
5237 break;
5238
5239 case DATA_TYPE_UINT32:
5240 (void) nvpair_value_uint32(nvp, &i32);
5241 printf(gettext("0x%x"), i32);
5242 break;
5243
5244 case DATA_TYPE_INT64:
5245 (void) nvpair_value_int64(nvp, (void *)&i64);
5246 printf(gettext("0x%llx"), (u_longlong_t)i64);
5247 break;
5248
5249 case DATA_TYPE_UINT64:
5250 (void) nvpair_value_uint64(nvp, &i64);
5251 printf(gettext("0x%llx"), (u_longlong_t)i64);
5252 break;
5253
5254 case DATA_TYPE_HRTIME:
5255 (void) nvpair_value_hrtime(nvp, (void *)&i64);
5256 printf(gettext("0x%llx"), (u_longlong_t)i64);
5257 break;
5258
5259 case DATA_TYPE_STRING:
5260 (void) nvpair_value_string(nvp, &str);
5261 printf(gettext("\"%s\""), str ? str : "<NULL>");
5262 break;
5263
5264 case DATA_TYPE_NVLIST:
5265 printf(gettext("(embedded nvlist)\n"));
5266 (void) nvpair_value_nvlist(nvp, &cnv);
5267 zpool_do_events_nvprint(cnv, depth + 8);
5268 printf(gettext("%*s(end %s)"), depth, "", name);
5269 break;
5270
5271 case DATA_TYPE_NVLIST_ARRAY: {
5272 nvlist_t **val;
5273 uint_t i, nelem;
5274
5275 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
5276 printf(gettext("(%d embedded nvlists)\n"), nelem);
5277 for (i = 0; i < nelem; i++) {
5278 printf(gettext("%*s%s[%d] = %s\n"),
5279 depth, "", name, i, "(embedded nvlist)");
5280 zpool_do_events_nvprint(val[i], depth + 8);
5281 printf(gettext("%*s(end %s[%i])\n"),
5282 depth, "", name, i);
5283 }
5284 printf(gettext("%*s(end %s)\n"), depth, "", name);
5285 }
5286 break;
5287
5288 case DATA_TYPE_INT8_ARRAY: {
5289 int8_t *val;
5290 uint_t i, nelem;
5291
5292 (void) nvpair_value_int8_array(nvp, &val, &nelem);
5293 for (i = 0; i < nelem; i++)
5294 printf(gettext("0x%x "), val[i]);
5295
5296 break;
5297 }
5298
5299 case DATA_TYPE_UINT8_ARRAY: {
5300 uint8_t *val;
5301 uint_t i, nelem;
5302
5303 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
5304 for (i = 0; i < nelem; i++)
5305 printf(gettext("0x%x "), val[i]);
5306
5307 break;
5308 }
5309
5310 case DATA_TYPE_INT16_ARRAY: {
5311 int16_t *val;
5312 uint_t i, nelem;
5313
5314 (void) nvpair_value_int16_array(nvp, &val, &nelem);
5315 for (i = 0; i < nelem; i++)
5316 printf(gettext("0x%x "), val[i]);
5317
5318 break;
5319 }
5320
5321 case DATA_TYPE_UINT16_ARRAY: {
5322 uint16_t *val;
5323 uint_t i, nelem;
5324
5325 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
5326 for (i = 0; i < nelem; i++)
5327 printf(gettext("0x%x "), val[i]);
5328
5329 break;
5330 }
5331
5332 case DATA_TYPE_INT32_ARRAY: {
5333 int32_t *val;
5334 uint_t i, nelem;
5335
5336 (void) nvpair_value_int32_array(nvp, &val, &nelem);
5337 for (i = 0; i < nelem; i++)
5338 printf(gettext("0x%x "), val[i]);
5339
5340 break;
5341 }
5342
5343 case DATA_TYPE_UINT32_ARRAY: {
5344 uint32_t *val;
5345 uint_t i, nelem;
5346
5347 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
5348 for (i = 0; i < nelem; i++)
5349 printf(gettext("0x%x "), val[i]);
5350
5351 break;
5352 }
5353
5354 case DATA_TYPE_INT64_ARRAY: {
5355 int64_t *val;
5356 uint_t i, nelem;
5357
5358 (void) nvpair_value_int64_array(nvp, &val, &nelem);
5359 for (i = 0; i < nelem; i++)
5360 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
5361
5362 break;
5363 }
5364
5365 case DATA_TYPE_UINT64_ARRAY: {
5366 uint64_t *val;
5367 uint_t i, nelem;
5368
5369 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
5370 for (i = 0; i < nelem; i++)
5371 printf(gettext("0x%llx "), (u_longlong_t)val[i]);
5372
5373 break;
5374 }
5375
5376 case DATA_TYPE_STRING_ARRAY:
5377 case DATA_TYPE_BOOLEAN_ARRAY:
5378 case DATA_TYPE_BYTE_ARRAY:
5379 case DATA_TYPE_DOUBLE:
5380 case DATA_TYPE_UNKNOWN:
5381 printf(gettext("<unknown>"));
5382 break;
5383 }
5384
5385 printf(gettext("\n"));
5386 }
5387 }
5388
5389 static int
5390 zpool_do_events_next(ev_opts_t *opts)
5391 {
5392 nvlist_t *nvl;
5393 int cleanup_fd, ret, dropped;
5394
5395 cleanup_fd = open(ZFS_DEV, O_RDWR);
5396 VERIFY(cleanup_fd >= 0);
5397
5398 if (!opts->scripted)
5399 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
5400
5401 while (1) {
5402 ret = zpool_events_next(g_zfs, &nvl, &dropped,
5403 !!opts->follow, cleanup_fd);
5404 if (ret || nvl == NULL)
5405 break;
5406
5407 if (dropped > 0)
5408 (void) printf(gettext("dropped %d events\n"), dropped);
5409
5410 zpool_do_events_short(nvl);
5411
5412 if (opts->verbose) {
5413 zpool_do_events_nvprint(nvl, 8);
5414 printf(gettext("\n"));
5415 }
5416 (void) fflush(stdout);
5417
5418 nvlist_free(nvl);
5419 }
5420
5421 VERIFY(0 == close(cleanup_fd));
5422
5423 return (ret);
5424 }
5425
5426 static int
5427 zpool_do_events_clear(ev_opts_t *opts)
5428 {
5429 int count, ret;
5430
5431 ret = zpool_events_clear(g_zfs, &count);
5432 if (!ret)
5433 (void) printf(gettext("cleared %d events\n"), count);
5434
5435 return (ret);
5436 }
5437
5438 /*
5439 * zpool events [-vfc]
5440 *
5441 * Displays events logs by ZFS.
5442 */
5443 int
5444 zpool_do_events(int argc, char **argv)
5445 {
5446 ev_opts_t opts = { 0 };
5447 int ret;
5448 int c;
5449
5450 /* check options */
5451 while ((c = getopt(argc, argv, "vHfc")) != -1) {
5452 switch (c) {
5453 case 'v':
5454 opts.verbose = 1;
5455 break;
5456 case 'H':
5457 opts.scripted = 1;
5458 break;
5459 case 'f':
5460 opts.follow = 1;
5461 break;
5462 case 'c':
5463 opts.clear = 1;
5464 break;
5465 case '?':
5466 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5467 optopt);
5468 usage(B_FALSE);
5469 }
5470 }
5471 argc -= optind;
5472 argv += optind;
5473
5474 if (opts.clear)
5475 ret = zpool_do_events_clear(&opts);
5476 else
5477 ret = zpool_do_events_next(&opts);
5478
5479 return ret;
5480 }
5481
5482 static int
5483 get_callback(zpool_handle_t *zhp, void *data)
5484 {
5485 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
5486 char value[MAXNAMELEN];
5487 zprop_source_t srctype;
5488 zprop_list_t *pl;
5489
5490 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
5491
5492 /*
5493 * Skip the special fake placeholder. This will also skip
5494 * over the name property when 'all' is specified.
5495 */
5496 if (pl->pl_prop == ZPOOL_PROP_NAME &&
5497 pl == cbp->cb_proplist)
5498 continue;
5499
5500 if (pl->pl_prop == ZPROP_INVAL &&
5501 (zpool_prop_feature(pl->pl_user_prop) ||
5502 zpool_prop_unsupported(pl->pl_user_prop))) {
5503 srctype = ZPROP_SRC_LOCAL;
5504
5505 if (zpool_prop_get_feature(zhp, pl->pl_user_prop,
5506 value, sizeof (value)) == 0) {
5507 zprop_print_one_property(zpool_get_name(zhp),
5508 cbp, pl->pl_user_prop, value, srctype,
5509 NULL, NULL);
5510 }
5511 } else {
5512 if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5513 sizeof (value), &srctype, cbp->cb_literal) != 0)
5514 continue;
5515
5516 zprop_print_one_property(zpool_get_name(zhp), cbp,
5517 zpool_prop_to_name(pl->pl_prop), value, srctype,
5518 NULL, NULL);
5519 }
5520 }
5521 return (0);
5522 }
5523
5524 int
5525 zpool_do_get(int argc, char **argv)
5526 {
5527 zprop_get_cbdata_t cb = { 0 };
5528 zprop_list_t fake_name = { 0 };
5529 int c, ret;
5530
5531 /* check options */
5532 while ((c = getopt(argc, argv, "p")) != -1) {
5533 switch (c) {
5534 case 'p':
5535 cb.cb_literal = B_TRUE;
5536 break;
5537
5538 case '?':
5539 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5540 optopt);
5541 usage(B_FALSE);
5542 }
5543 }
5544
5545 argc -= optind;
5546 argv += optind;
5547
5548 if (argc < 1) {
5549 (void) fprintf(stderr, gettext("missing property "
5550 "argument\n"));
5551 usage(B_FALSE);
5552 }
5553
5554 cb.cb_first = B_TRUE;
5555 cb.cb_sources = ZPROP_SRC_ALL;
5556 cb.cb_columns[0] = GET_COL_NAME;
5557 cb.cb_columns[1] = GET_COL_PROPERTY;
5558 cb.cb_columns[2] = GET_COL_VALUE;
5559 cb.cb_columns[3] = GET_COL_SOURCE;
5560 cb.cb_type = ZFS_TYPE_POOL;
5561
5562 if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
5563 usage(B_FALSE);
5564
5565 argc--;
5566 argv++;
5567
5568 if (cb.cb_proplist != NULL) {
5569 fake_name.pl_prop = ZPOOL_PROP_NAME;
5570 fake_name.pl_width = strlen(gettext("NAME"));
5571 fake_name.pl_next = cb.cb_proplist;
5572 cb.cb_proplist = &fake_name;
5573 }
5574
5575 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist,
5576 get_callback, &cb);
5577
5578 if (cb.cb_proplist == &fake_name)
5579 zprop_free_list(fake_name.pl_next);
5580 else
5581 zprop_free_list(cb.cb_proplist);
5582
5583 return (ret);
5584 }
5585
5586 typedef struct set_cbdata {
5587 char *cb_propname;
5588 char *cb_value;
5589 boolean_t cb_any_successful;
5590 } set_cbdata_t;
5591
5592 int
5593 set_callback(zpool_handle_t *zhp, void *data)
5594 {
5595 int error;
5596 set_cbdata_t *cb = (set_cbdata_t *)data;
5597
5598 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
5599
5600 if (!error)
5601 cb->cb_any_successful = B_TRUE;
5602
5603 return (error);
5604 }
5605
5606 int
5607 zpool_do_set(int argc, char **argv)
5608 {
5609 set_cbdata_t cb = { 0 };
5610 int error;
5611
5612 if (argc > 1 && argv[1][0] == '-') {
5613 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5614 argv[1][1]);
5615 usage(B_FALSE);
5616 }
5617
5618 if (argc < 2) {
5619 (void) fprintf(stderr, gettext("missing property=value "
5620 "argument\n"));
5621 usage(B_FALSE);
5622 }
5623
5624 if (argc < 3) {
5625 (void) fprintf(stderr, gettext("missing pool name\n"));
5626 usage(B_FALSE);
5627 }
5628
5629 if (argc > 3) {
5630 (void) fprintf(stderr, gettext("too many pool names\n"));
5631 usage(B_FALSE);
5632 }
5633
5634 cb.cb_propname = argv[1];
5635 cb.cb_value = strchr(cb.cb_propname, '=');
5636 if (cb.cb_value == NULL) {
5637 (void) fprintf(stderr, gettext("missing value in "
5638 "property=value argument\n"));
5639 usage(B_FALSE);
5640 }
5641
5642 *(cb.cb_value) = '\0';
5643 cb.cb_value++;
5644
5645 error = for_each_pool(argc - 2, argv + 2, B_TRUE, NULL,
5646 set_callback, &cb);
5647
5648 return (error);
5649 }
5650
5651 static int
5652 find_command_idx(char *command, int *idx)
5653 {
5654 int i;
5655
5656 for (i = 0; i < NCOMMAND; i++) {
5657 if (command_table[i].name == NULL)
5658 continue;
5659
5660 if (strcmp(command, command_table[i].name) == 0) {
5661 *idx = i;
5662 return (0);
5663 }
5664 }
5665 return (1);
5666 }
5667
5668 int
5669 main(int argc, char **argv)
5670 {
5671 int ret;
5672 int i = 0;
5673 char *cmdname;
5674
5675 (void) setlocale(LC_ALL, "");
5676 (void) textdomain(TEXT_DOMAIN);
5677
5678 opterr = 0;
5679
5680 /*
5681 * Make sure the user has specified some command.
5682 */
5683 if (argc < 2) {
5684 (void) fprintf(stderr, gettext("missing command\n"));
5685 usage(B_FALSE);
5686 }
5687
5688 cmdname = argv[1];
5689
5690 /*
5691 * Special case '-?'
5692 */
5693 if ((strcmp(cmdname, "-?") == 0) ||
5694 strcmp(cmdname, "--help") == 0)
5695 usage(B_TRUE);
5696
5697 if ((g_zfs = libzfs_init()) == NULL)
5698 return (1);
5699
5700 libzfs_print_on_error(g_zfs, B_TRUE);
5701
5702 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
5703
5704 /*
5705 * Run the appropriate command.
5706 */
5707 if (find_command_idx(cmdname, &i) == 0) {
5708 current_command = &command_table[i];
5709 ret = command_table[i].func(argc - 1, argv + 1);
5710 } else if (strchr(cmdname, '=')) {
5711 verify(find_command_idx("set", &i) == 0);
5712 current_command = &command_table[i];
5713 ret = command_table[i].func(argc, argv);
5714 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
5715 /*
5716 * 'freeze' is a vile debugging abomination, so we treat
5717 * it as such.
5718 */
5719 char buf[16384];
5720 int fd = open(ZFS_DEV, O_RDWR);
5721 (void) strcpy((void *)buf, argv[2]);
5722 return (!!ioctl(fd, ZFS_IOC_POOL_FREEZE, buf));
5723 } else {
5724 (void) fprintf(stderr, gettext("unrecognized "
5725 "command '%s'\n"), cmdname);
5726 usage(B_FALSE);
5727 ret = 1;
5728 }
5729
5730 if (ret == 0 && log_history)
5731 (void) zpool_log_history(g_zfs, history_str);
5732
5733 libzfs_fini(g_zfs);
5734
5735 /*
5736 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
5737 * for the purposes of running ::findleaks.
5738 */
5739 if (getenv("ZFS_ABORT") != NULL) {
5740 (void) printf("dumping core by request\n");
5741 abort();
5742 }
5743
5744 return (ret);
5745 }