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