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