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