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