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