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