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