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