]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/zpool/zpool_main.c
0872671f428fa363a7464fa9922a35ccfc747dda
[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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 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 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
30 * Copyright (c) 2017 Datto Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2017, Intel Corporation.
33 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
34 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
35 * Copyright (c) 2021, Klara Inc.
36 * Copyright [2021] Hewlett Packard Enterprise Development LP
37 */
38
39 #include <assert.h>
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <getopt.h>
45 #include <libgen.h>
46 #include <libintl.h>
47 #include <libuutil.h>
48 #include <locale.h>
49 #include <pthread.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55 #include <pwd.h>
56 #include <zone.h>
57 #include <sys/wait.h>
58 #include <zfs_prop.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/stat.h>
61 #include <sys/systeminfo.h>
62 #include <sys/fm/fs/zfs.h>
63 #include <sys/fm/util.h>
64 #include <sys/fm/protocol.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/mount.h>
67 #include <sys/sysmacros.h>
68
69 #include <math.h>
70
71 #include <libzfs.h>
72 #include <libzutil.h>
73
74 #include "zpool_util.h"
75 #include "zfs_comutil.h"
76 #include "zfeature_common.h"
77
78 #include "statcommon.h"
79
80 libzfs_handle_t *g_zfs;
81
82 static int zpool_do_create(int, char **);
83 static int zpool_do_destroy(int, char **);
84
85 static int zpool_do_add(int, char **);
86 static int zpool_do_remove(int, char **);
87 static int zpool_do_labelclear(int, char **);
88
89 static int zpool_do_checkpoint(int, char **);
90
91 static int zpool_do_list(int, char **);
92 static int zpool_do_iostat(int, char **);
93 static int zpool_do_status(int, char **);
94
95 static int zpool_do_online(int, char **);
96 static int zpool_do_offline(int, char **);
97 static int zpool_do_clear(int, char **);
98 static int zpool_do_reopen(int, char **);
99
100 static int zpool_do_reguid(int, char **);
101
102 static int zpool_do_attach(int, char **);
103 static int zpool_do_detach(int, char **);
104 static int zpool_do_replace(int, char **);
105 static int zpool_do_split(int, char **);
106
107 static int zpool_do_initialize(int, char **);
108 static int zpool_do_scrub(int, char **);
109 static int zpool_do_resilver(int, char **);
110 static int zpool_do_trim(int, char **);
111
112 static int zpool_do_import(int, char **);
113 static int zpool_do_export(int, char **);
114
115 static int zpool_do_upgrade(int, char **);
116
117 static int zpool_do_history(int, char **);
118 static int zpool_do_events(int, char **);
119
120 static int zpool_do_get(int, char **);
121 static int zpool_do_set(int, char **);
122
123 static int zpool_do_sync(int, char **);
124
125 static int zpool_do_version(int, char **);
126
127 static int zpool_do_wait(int, char **);
128
129 static zpool_compat_status_t zpool_do_load_compat(
130 const char *, boolean_t *);
131
132 /*
133 * These libumem hooks provide a reasonable set of defaults for the allocator's
134 * debugging facilities.
135 */
136
137 #ifdef DEBUG
138 const char *
139 _umem_debug_init(void)
140 {
141 return ("default,verbose"); /* $UMEM_DEBUG setting */
142 }
143
144 const char *
145 _umem_logging_init(void)
146 {
147 return ("fail,contents"); /* $UMEM_LOGGING setting */
148 }
149 #endif
150
151 typedef enum {
152 HELP_ADD,
153 HELP_ATTACH,
154 HELP_CLEAR,
155 HELP_CREATE,
156 HELP_CHECKPOINT,
157 HELP_DESTROY,
158 HELP_DETACH,
159 HELP_EXPORT,
160 HELP_HISTORY,
161 HELP_IMPORT,
162 HELP_IOSTAT,
163 HELP_LABELCLEAR,
164 HELP_LIST,
165 HELP_OFFLINE,
166 HELP_ONLINE,
167 HELP_REPLACE,
168 HELP_REMOVE,
169 HELP_INITIALIZE,
170 HELP_SCRUB,
171 HELP_RESILVER,
172 HELP_TRIM,
173 HELP_STATUS,
174 HELP_UPGRADE,
175 HELP_EVENTS,
176 HELP_GET,
177 HELP_SET,
178 HELP_SPLIT,
179 HELP_SYNC,
180 HELP_REGUID,
181 HELP_REOPEN,
182 HELP_VERSION,
183 HELP_WAIT
184 } zpool_help_t;
185
186
187 /*
188 * Flags for stats to display with "zpool iostats"
189 */
190 enum iostat_type {
191 IOS_DEFAULT = 0,
192 IOS_LATENCY = 1,
193 IOS_QUEUES = 2,
194 IOS_L_HISTO = 3,
195 IOS_RQ_HISTO = 4,
196 IOS_COUNT, /* always last element */
197 };
198
199 /* iostat_type entries as bitmasks */
200 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
201 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
202 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
203 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
204 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
205
206 /* Mask of all the histo bits */
207 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
208
209 /*
210 * Lookup table for iostat flags to nvlist names. Basically a list
211 * of all the nvlists a flag requires. Also specifies the order in
212 * which data gets printed in zpool iostat.
213 */
214 static const char *vsx_type_to_nvlist[IOS_COUNT][15] = {
215 [IOS_L_HISTO] = {
216 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
217 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
218 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
219 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
220 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
221 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
222 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
223 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
224 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
225 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
226 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
227 NULL},
228 [IOS_LATENCY] = {
229 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
230 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
231 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
232 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
233 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
234 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
235 NULL},
236 [IOS_QUEUES] = {
237 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
238 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
239 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
240 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
241 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
242 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
243 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
244 NULL},
245 [IOS_RQ_HISTO] = {
246 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
247 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
248 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
249 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
250 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
251 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
252 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
253 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
254 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
255 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
256 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
257 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
258 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
259 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
260 NULL},
261 };
262
263
264 /*
265 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
266 * Right now, only one histo bit is ever set at one time, so we can
267 * just do a highbit64(a)
268 */
269 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
270
271 typedef struct zpool_command {
272 const char *name;
273 int (*func)(int, char **);
274 zpool_help_t usage;
275 } zpool_command_t;
276
277 /*
278 * Master command table. Each ZFS command has a name, associated function, and
279 * usage message. The usage messages need to be internationalized, so we have
280 * to have a function to return the usage message based on a command index.
281 *
282 * These commands are organized according to how they are displayed in the usage
283 * message. An empty command (one with a NULL name) indicates an empty line in
284 * the generic usage message.
285 */
286 static zpool_command_t command_table[] = {
287 { "version", zpool_do_version, HELP_VERSION },
288 { NULL },
289 { "create", zpool_do_create, HELP_CREATE },
290 { "destroy", zpool_do_destroy, HELP_DESTROY },
291 { NULL },
292 { "add", zpool_do_add, HELP_ADD },
293 { "remove", zpool_do_remove, HELP_REMOVE },
294 { NULL },
295 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
296 { NULL },
297 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
298 { NULL },
299 { "list", zpool_do_list, HELP_LIST },
300 { "iostat", zpool_do_iostat, HELP_IOSTAT },
301 { "status", zpool_do_status, HELP_STATUS },
302 { NULL },
303 { "online", zpool_do_online, HELP_ONLINE },
304 { "offline", zpool_do_offline, HELP_OFFLINE },
305 { "clear", zpool_do_clear, HELP_CLEAR },
306 { "reopen", zpool_do_reopen, HELP_REOPEN },
307 { NULL },
308 { "attach", zpool_do_attach, HELP_ATTACH },
309 { "detach", zpool_do_detach, HELP_DETACH },
310 { "replace", zpool_do_replace, HELP_REPLACE },
311 { "split", zpool_do_split, HELP_SPLIT },
312 { NULL },
313 { "initialize", zpool_do_initialize, HELP_INITIALIZE },
314 { "resilver", zpool_do_resilver, HELP_RESILVER },
315 { "scrub", zpool_do_scrub, HELP_SCRUB },
316 { "trim", zpool_do_trim, HELP_TRIM },
317 { NULL },
318 { "import", zpool_do_import, HELP_IMPORT },
319 { "export", zpool_do_export, HELP_EXPORT },
320 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
321 { "reguid", zpool_do_reguid, HELP_REGUID },
322 { NULL },
323 { "history", zpool_do_history, HELP_HISTORY },
324 { "events", zpool_do_events, HELP_EVENTS },
325 { NULL },
326 { "get", zpool_do_get, HELP_GET },
327 { "set", zpool_do_set, HELP_SET },
328 { "sync", zpool_do_sync, HELP_SYNC },
329 { NULL },
330 { "wait", zpool_do_wait, HELP_WAIT },
331 };
332
333 #define NCOMMAND (ARRAY_SIZE(command_table))
334
335 #define VDEV_ALLOC_CLASS_LOGS "logs"
336
337 static zpool_command_t *current_command;
338 static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
339 static char history_str[HIS_MAX_RECORD_LEN];
340 static boolean_t log_history = B_TRUE;
341 static uint_t timestamp_fmt = NODATE;
342
343 static const char *
344 get_usage(zpool_help_t idx)
345 {
346 switch (idx) {
347 case HELP_ADD:
348 return (gettext("\tadd [-fgLnP] [-o property=value] "
349 "<pool> <vdev> ...\n"));
350 case HELP_ATTACH:
351 return (gettext("\tattach [-fsw] [-o property=value] "
352 "<pool> <device> <new-device>\n"));
353 case HELP_CLEAR:
354 return (gettext("\tclear [-nF] <pool> [device]\n"));
355 case HELP_CREATE:
356 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
357 "\t [-O file-system-property=value] ... \n"
358 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
359 case HELP_CHECKPOINT:
360 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
361 case HELP_DESTROY:
362 return (gettext("\tdestroy [-f] <pool>\n"));
363 case HELP_DETACH:
364 return (gettext("\tdetach <pool> <device>\n"));
365 case HELP_EXPORT:
366 return (gettext("\texport [-af] <pool> ...\n"));
367 case HELP_HISTORY:
368 return (gettext("\thistory [-il] [<pool>] ...\n"));
369 case HELP_IMPORT:
370 return (gettext("\timport [-d dir] [-D]\n"
371 "\timport [-o mntopts] [-o property=value] ... \n"
372 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
373 "[-R root] [-F [-n]] -a\n"
374 "\timport [-o mntopts] [-o property=value] ... \n"
375 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
376 "[-R root] [-F [-n]]\n"
377 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
378 case HELP_IOSTAT:
379 return (gettext("\tiostat [[[-c [script1,script2,...]"
380 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
381 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
382 " [[-n] interval [count]]\n"));
383 case HELP_LABELCLEAR:
384 return (gettext("\tlabelclear [-f] <vdev>\n"));
385 case HELP_LIST:
386 return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
387 "[-T d|u] [pool] ... \n"
388 "\t [interval [count]]\n"));
389 case HELP_OFFLINE:
390 return (gettext("\toffline [-f] [-t] <pool> <device> ...\n"));
391 case HELP_ONLINE:
392 return (gettext("\tonline [-e] <pool> <device> ...\n"));
393 case HELP_REPLACE:
394 return (gettext("\treplace [-fsw] [-o property=value] "
395 "<pool> <device> [new-device]\n"));
396 case HELP_REMOVE:
397 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
398 case HELP_REOPEN:
399 return (gettext("\treopen [-n] <pool>\n"));
400 case HELP_INITIALIZE:
401 return (gettext("\tinitialize [-c | -s] [-w] <pool> "
402 "[<device> ...]\n"));
403 case HELP_SCRUB:
404 return (gettext("\tscrub [-s | -p] [-w] <pool> ...\n"));
405 case HELP_RESILVER:
406 return (gettext("\tresilver <pool> ...\n"));
407 case HELP_TRIM:
408 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
409 "[<device> ...]\n"));
410 case HELP_STATUS:
411 return (gettext("\tstatus [-c [script1,script2,...]] "
412 "[-igLpPstvxD] [-T d|u] [pool] ... \n"
413 "\t [interval [count]]\n"));
414 case HELP_UPGRADE:
415 return (gettext("\tupgrade\n"
416 "\tupgrade -v\n"
417 "\tupgrade [-V version] <-a | pool ...>\n"));
418 case HELP_EVENTS:
419 return (gettext("\tevents [-vHf [pool] | -c]\n"));
420 case HELP_GET:
421 return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
422 "<\"all\" | property[,...]> <pool> ...\n"));
423 case HELP_SET:
424 return (gettext("\tset <property=value> <pool> \n"));
425 case HELP_SPLIT:
426 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
427 "\t [-o property=value] <pool> <newpool> "
428 "[<device> ...]\n"));
429 case HELP_REGUID:
430 return (gettext("\treguid <pool>\n"));
431 case HELP_SYNC:
432 return (gettext("\tsync [pool] ...\n"));
433 case HELP_VERSION:
434 return (gettext("\tversion\n"));
435 case HELP_WAIT:
436 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
437 "<pool> [interval]\n"));
438 default:
439 __builtin_unreachable();
440 }
441 }
442
443 static void
444 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
445 {
446 uint_t children = 0;
447 nvlist_t **child;
448 uint_t i;
449
450 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
451 &child, &children);
452
453 if (children == 0) {
454 char *path = zpool_vdev_name(g_zfs, zhp, nvroot,
455 VDEV_NAME_PATH);
456
457 if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 &&
458 strcmp(path, VDEV_TYPE_HOLE) != 0)
459 fnvlist_add_boolean(res, path);
460
461 free(path);
462 return;
463 }
464
465 for (i = 0; i < children; i++) {
466 zpool_collect_leaves(zhp, child[i], res);
467 }
468 }
469
470 /*
471 * Callback routine that will print out a pool property value.
472 */
473 static int
474 print_pool_prop_cb(int prop, void *cb)
475 {
476 FILE *fp = cb;
477
478 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
479
480 if (zpool_prop_readonly(prop))
481 (void) fprintf(fp, " NO ");
482 else
483 (void) fprintf(fp, " YES ");
484
485 if (zpool_prop_values(prop) == NULL)
486 (void) fprintf(fp, "-\n");
487 else
488 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
489
490 return (ZPROP_CONT);
491 }
492
493 /*
494 * Callback routine that will print out a vdev property value.
495 */
496 static int
497 print_vdev_prop_cb(int prop, void *cb)
498 {
499 FILE *fp = cb;
500
501 (void) fprintf(fp, "\t%-19s ", vdev_prop_to_name(prop));
502
503 if (vdev_prop_readonly(prop))
504 (void) fprintf(fp, " NO ");
505 else
506 (void) fprintf(fp, " YES ");
507
508 if (vdev_prop_values(prop) == NULL)
509 (void) fprintf(fp, "-\n");
510 else
511 (void) fprintf(fp, "%s\n", vdev_prop_values(prop));
512
513 return (ZPROP_CONT);
514 }
515
516 /*
517 * Display usage message. If we're inside a command, display only the usage for
518 * that command. Otherwise, iterate over the entire command table and display
519 * a complete usage message.
520 */
521 static __attribute__((noreturn)) void
522 usage(boolean_t requested)
523 {
524 FILE *fp = requested ? stdout : stderr;
525
526 if (current_command == NULL) {
527 int i;
528
529 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
530 (void) fprintf(fp,
531 gettext("where 'command' is one of the following:\n\n"));
532
533 for (i = 0; i < NCOMMAND; i++) {
534 if (command_table[i].name == NULL)
535 (void) fprintf(fp, "\n");
536 else
537 (void) fprintf(fp, "%s",
538 get_usage(command_table[i].usage));
539 }
540 } else {
541 (void) fprintf(fp, gettext("usage:\n"));
542 (void) fprintf(fp, "%s", get_usage(current_command->usage));
543 }
544
545 if (current_command != NULL &&
546 current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) &&
547 ((strcmp(current_command->name, "set") == 0) ||
548 (strcmp(current_command->name, "get") == 0) ||
549 (strcmp(current_command->name, "list") == 0))) {
550
551 (void) fprintf(fp, "%s",
552 gettext("\nthe following properties are supported:\n"));
553
554 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
555 "PROPERTY", "EDIT", "VALUES");
556
557 /* Iterate over all properties */
558 if (current_prop_type == ZFS_TYPE_POOL) {
559 (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE,
560 B_TRUE, current_prop_type);
561
562 (void) fprintf(fp, "\t%-19s ", "feature@...");
563 (void) fprintf(fp, "YES "
564 "disabled | enabled | active\n");
565
566 (void) fprintf(fp, gettext("\nThe feature@ properties "
567 "must be appended with a feature name.\n"
568 "See zpool-features(7).\n"));
569 } else if (current_prop_type == ZFS_TYPE_VDEV) {
570 (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE,
571 B_TRUE, current_prop_type);
572 }
573 }
574
575 /*
576 * See comments at end of main().
577 */
578 if (getenv("ZFS_ABORT") != NULL) {
579 (void) printf("dumping core by request\n");
580 abort();
581 }
582
583 exit(requested ? 0 : 2);
584 }
585
586 /*
587 * zpool initialize [-c | -s] [-w] <pool> [<vdev> ...]
588 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
589 * if none specified.
590 *
591 * -c Cancel. Ends active initializing.
592 * -s Suspend. Initializing can then be restarted with no flags.
593 * -w Wait. Blocks until initializing has completed.
594 */
595 int
596 zpool_do_initialize(int argc, char **argv)
597 {
598 int c;
599 char *poolname;
600 zpool_handle_t *zhp;
601 nvlist_t *vdevs;
602 int err = 0;
603 boolean_t wait = B_FALSE;
604
605 struct option long_options[] = {
606 {"cancel", no_argument, NULL, 'c'},
607 {"suspend", no_argument, NULL, 's'},
608 {"wait", no_argument, NULL, 'w'},
609 {0, 0, 0, 0}
610 };
611
612 pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
613 while ((c = getopt_long(argc, argv, "csw", long_options, NULL)) != -1) {
614 switch (c) {
615 case 'c':
616 if (cmd_type != POOL_INITIALIZE_START &&
617 cmd_type != POOL_INITIALIZE_CANCEL) {
618 (void) fprintf(stderr, gettext("-c cannot be "
619 "combined with other options\n"));
620 usage(B_FALSE);
621 }
622 cmd_type = POOL_INITIALIZE_CANCEL;
623 break;
624 case 's':
625 if (cmd_type != POOL_INITIALIZE_START &&
626 cmd_type != POOL_INITIALIZE_SUSPEND) {
627 (void) fprintf(stderr, gettext("-s cannot be "
628 "combined with other options\n"));
629 usage(B_FALSE);
630 }
631 cmd_type = POOL_INITIALIZE_SUSPEND;
632 break;
633 case 'w':
634 wait = B_TRUE;
635 break;
636 case '?':
637 if (optopt != 0) {
638 (void) fprintf(stderr,
639 gettext("invalid option '%c'\n"), optopt);
640 } else {
641 (void) fprintf(stderr,
642 gettext("invalid option '%s'\n"),
643 argv[optind - 1]);
644 }
645 usage(B_FALSE);
646 }
647 }
648
649 argc -= optind;
650 argv += optind;
651
652 if (argc < 1) {
653 (void) fprintf(stderr, gettext("missing pool name argument\n"));
654 usage(B_FALSE);
655 return (-1);
656 }
657
658 if (wait && (cmd_type != POOL_INITIALIZE_START)) {
659 (void) fprintf(stderr, gettext("-w cannot be used with -c or "
660 "-s\n"));
661 usage(B_FALSE);
662 }
663
664 poolname = argv[0];
665 zhp = zpool_open(g_zfs, poolname);
666 if (zhp == NULL)
667 return (-1);
668
669 vdevs = fnvlist_alloc();
670 if (argc == 1) {
671 /* no individual leaf vdevs specified, so add them all */
672 nvlist_t *config = zpool_get_config(zhp, NULL);
673 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
674 ZPOOL_CONFIG_VDEV_TREE);
675 zpool_collect_leaves(zhp, nvroot, vdevs);
676 } else {
677 for (int i = 1; i < argc; i++) {
678 fnvlist_add_boolean(vdevs, argv[i]);
679 }
680 }
681
682 if (wait)
683 err = zpool_initialize_wait(zhp, cmd_type, vdevs);
684 else
685 err = zpool_initialize(zhp, cmd_type, vdevs);
686
687 fnvlist_free(vdevs);
688 zpool_close(zhp);
689
690 return (err);
691 }
692
693 /*
694 * print a pool vdev config for dry runs
695 */
696 static void
697 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
698 const char *match, int name_flags)
699 {
700 nvlist_t **child;
701 uint_t c, children;
702 char *vname;
703 boolean_t printed = B_FALSE;
704
705 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
706 &child, &children) != 0) {
707 if (name != NULL)
708 (void) printf("\t%*s%s\n", indent, "", name);
709 return;
710 }
711
712 for (c = 0; c < children; c++) {
713 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
714 char *class = (char *)"";
715
716 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
717 &is_hole);
718
719 if (is_hole == B_TRUE) {
720 continue;
721 }
722
723 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
724 &is_log);
725 if (is_log)
726 class = (char *)VDEV_ALLOC_BIAS_LOG;
727 (void) nvlist_lookup_string(child[c],
728 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
729 if (strcmp(match, class) != 0)
730 continue;
731
732 if (!printed && name != NULL) {
733 (void) printf("\t%*s%s\n", indent, "", name);
734 printed = B_TRUE;
735 }
736 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
737 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
738 name_flags);
739 free(vname);
740 }
741 }
742
743 /*
744 * Print the list of l2cache devices for dry runs.
745 */
746 static void
747 print_cache_list(nvlist_t *nv, int indent)
748 {
749 nvlist_t **child;
750 uint_t c, children;
751
752 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
753 &child, &children) == 0 && children > 0) {
754 (void) printf("\t%*s%s\n", indent, "", "cache");
755 } else {
756 return;
757 }
758 for (c = 0; c < children; c++) {
759 char *vname;
760
761 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
762 (void) printf("\t%*s%s\n", indent + 2, "", vname);
763 free(vname);
764 }
765 }
766
767 /*
768 * Print the list of spares for dry runs.
769 */
770 static void
771 print_spare_list(nvlist_t *nv, int indent)
772 {
773 nvlist_t **child;
774 uint_t c, children;
775
776 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
777 &child, &children) == 0 && children > 0) {
778 (void) printf("\t%*s%s\n", indent, "", "spares");
779 } else {
780 return;
781 }
782 for (c = 0; c < children; c++) {
783 char *vname;
784
785 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
786 (void) printf("\t%*s%s\n", indent + 2, "", vname);
787 free(vname);
788 }
789 }
790
791 static boolean_t
792 prop_list_contains_feature(nvlist_t *proplist)
793 {
794 nvpair_t *nvp;
795 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
796 nvp = nvlist_next_nvpair(proplist, nvp)) {
797 if (zpool_prop_feature(nvpair_name(nvp)))
798 return (B_TRUE);
799 }
800 return (B_FALSE);
801 }
802
803 /*
804 * Add a property pair (name, string-value) into a property nvlist.
805 */
806 static int
807 add_prop_list(const char *propname, const char *propval, nvlist_t **props,
808 boolean_t poolprop)
809 {
810 zpool_prop_t prop = ZPOOL_PROP_INVAL;
811 nvlist_t *proplist;
812 const char *normnm;
813 char *strval;
814
815 if (*props == NULL &&
816 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
817 (void) fprintf(stderr,
818 gettext("internal error: out of memory\n"));
819 return (1);
820 }
821
822 proplist = *props;
823
824 if (poolprop) {
825 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
826 const char *cname =
827 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY);
828
829 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
830 (!zpool_prop_feature(propname) &&
831 !zpool_prop_vdev(propname))) {
832 (void) fprintf(stderr, gettext("property '%s' is "
833 "not a valid pool or vdev property\n"), propname);
834 return (2);
835 }
836
837 /*
838 * feature@ properties and version should not be specified
839 * at the same time.
840 */
841 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
842 nvlist_exists(proplist, vname)) ||
843 (prop == ZPOOL_PROP_VERSION &&
844 prop_list_contains_feature(proplist))) {
845 (void) fprintf(stderr, gettext("'feature@' and "
846 "'version' properties cannot be specified "
847 "together\n"));
848 return (2);
849 }
850
851 /*
852 * if version is specified, only "legacy" compatibility
853 * may be requested
854 */
855 if ((prop == ZPOOL_PROP_COMPATIBILITY &&
856 strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 &&
857 nvlist_exists(proplist, vname)) ||
858 (prop == ZPOOL_PROP_VERSION &&
859 nvlist_exists(proplist, cname) &&
860 strcmp(fnvlist_lookup_string(proplist, cname),
861 ZPOOL_COMPAT_LEGACY) != 0)) {
862 (void) fprintf(stderr, gettext("when 'version' is "
863 "specified, the 'compatibility' feature may only "
864 "be set to '" ZPOOL_COMPAT_LEGACY "'\n"));
865 return (2);
866 }
867
868 if (zpool_prop_feature(propname) || zpool_prop_vdev(propname))
869 normnm = propname;
870 else
871 normnm = zpool_prop_to_name(prop);
872 } else {
873 zfs_prop_t fsprop = zfs_name_to_prop(propname);
874
875 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
876 B_FALSE)) {
877 normnm = zfs_prop_to_name(fsprop);
878 } else if (zfs_prop_user(propname) ||
879 zfs_prop_userquota(propname)) {
880 normnm = propname;
881 } else {
882 (void) fprintf(stderr, gettext("property '%s' is "
883 "not a valid filesystem property\n"), propname);
884 return (2);
885 }
886 }
887
888 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
889 prop != ZPOOL_PROP_CACHEFILE) {
890 (void) fprintf(stderr, gettext("property '%s' "
891 "specified multiple times\n"), propname);
892 return (2);
893 }
894
895 if (nvlist_add_string(proplist, normnm, propval) != 0) {
896 (void) fprintf(stderr, gettext("internal "
897 "error: out of memory\n"));
898 return (1);
899 }
900
901 return (0);
902 }
903
904 /*
905 * Set a default property pair (name, string-value) in a property nvlist
906 */
907 static int
908 add_prop_list_default(const char *propname, const char *propval,
909 nvlist_t **props)
910 {
911 char *pval;
912
913 if (nvlist_lookup_string(*props, propname, &pval) == 0)
914 return (0);
915
916 return (add_prop_list(propname, propval, props, B_TRUE));
917 }
918
919 /*
920 * zpool add [-fgLnP] [-o property=value] <pool> <vdev> ...
921 *
922 * -f Force addition of devices, even if they appear in use
923 * -g Display guid for individual vdev name.
924 * -L Follow links when resolving vdev path name.
925 * -n Do not add the devices, but display the resulting layout if
926 * they were to be added.
927 * -o Set property=value.
928 * -P Display full path for vdev name.
929 *
930 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
931 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
932 * libzfs.
933 */
934 int
935 zpool_do_add(int argc, char **argv)
936 {
937 boolean_t force = B_FALSE;
938 boolean_t dryrun = B_FALSE;
939 int name_flags = 0;
940 int c;
941 nvlist_t *nvroot;
942 char *poolname;
943 int ret;
944 zpool_handle_t *zhp;
945 nvlist_t *config;
946 nvlist_t *props = NULL;
947 char *propval;
948
949 /* check options */
950 while ((c = getopt(argc, argv, "fgLno:P")) != -1) {
951 switch (c) {
952 case 'f':
953 force = B_TRUE;
954 break;
955 case 'g':
956 name_flags |= VDEV_NAME_GUID;
957 break;
958 case 'L':
959 name_flags |= VDEV_NAME_FOLLOW_LINKS;
960 break;
961 case 'n':
962 dryrun = B_TRUE;
963 break;
964 case 'o':
965 if ((propval = strchr(optarg, '=')) == NULL) {
966 (void) fprintf(stderr, gettext("missing "
967 "'=' for -o option\n"));
968 usage(B_FALSE);
969 }
970 *propval = '\0';
971 propval++;
972
973 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
974 (add_prop_list(optarg, propval, &props, B_TRUE)))
975 usage(B_FALSE);
976 break;
977 case 'P':
978 name_flags |= VDEV_NAME_PATH;
979 break;
980 case '?':
981 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
982 optopt);
983 usage(B_FALSE);
984 }
985 }
986
987 argc -= optind;
988 argv += optind;
989
990 /* get pool name and check number of arguments */
991 if (argc < 1) {
992 (void) fprintf(stderr, gettext("missing pool name argument\n"));
993 usage(B_FALSE);
994 }
995 if (argc < 2) {
996 (void) fprintf(stderr, gettext("missing vdev specification\n"));
997 usage(B_FALSE);
998 }
999
1000 poolname = argv[0];
1001
1002 argc--;
1003 argv++;
1004
1005 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1006 return (1);
1007
1008 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
1009 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
1010 poolname);
1011 zpool_close(zhp);
1012 return (1);
1013 }
1014
1015 /* unless manually specified use "ashift" pool property (if set) */
1016 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
1017 int intval;
1018 zprop_source_t src;
1019 char strval[ZPOOL_MAXPROPLEN];
1020
1021 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
1022 if (src != ZPROP_SRC_DEFAULT) {
1023 (void) sprintf(strval, "%" PRId32, intval);
1024 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
1025 &props, B_TRUE) == 0);
1026 }
1027 }
1028
1029 /* pass off to make_root_vdev for processing */
1030 nvroot = make_root_vdev(zhp, props, force, !force, B_FALSE, dryrun,
1031 argc, argv);
1032 if (nvroot == NULL) {
1033 zpool_close(zhp);
1034 return (1);
1035 }
1036
1037 if (dryrun) {
1038 nvlist_t *poolnvroot;
1039 nvlist_t **l2child, **sparechild;
1040 uint_t l2children, sparechildren, c;
1041 char *vname;
1042 boolean_t hadcache = B_FALSE, hadspare = B_FALSE;
1043
1044 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1045 &poolnvroot) == 0);
1046
1047 (void) printf(gettext("would update '%s' to the following "
1048 "configuration:\n\n"), zpool_get_name(zhp));
1049
1050 /* print original main pool and new tree */
1051 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
1052 name_flags | VDEV_NAME_TYPE_ID);
1053 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
1054
1055 /* print other classes: 'dedup', 'special', and 'log' */
1056 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1057 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
1058 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1059 print_vdev_tree(zhp, NULL, nvroot, 0,
1060 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1061 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1062 print_vdev_tree(zhp, "dedup", nvroot, 0,
1063 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1064 }
1065
1066 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1067 print_vdev_tree(zhp, "special", poolnvroot, 0,
1068 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1069 print_vdev_tree(zhp, NULL, nvroot, 0,
1070 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1071 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1072 print_vdev_tree(zhp, "special", nvroot, 0,
1073 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1074 }
1075
1076 if (num_logs(poolnvroot) > 0) {
1077 print_vdev_tree(zhp, "logs", poolnvroot, 0,
1078 VDEV_ALLOC_BIAS_LOG, name_flags);
1079 print_vdev_tree(zhp, NULL, nvroot, 0,
1080 VDEV_ALLOC_BIAS_LOG, name_flags);
1081 } else if (num_logs(nvroot) > 0) {
1082 print_vdev_tree(zhp, "logs", nvroot, 0,
1083 VDEV_ALLOC_BIAS_LOG, name_flags);
1084 }
1085
1086 /* Do the same for the caches */
1087 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
1088 &l2child, &l2children) == 0 && l2children) {
1089 hadcache = B_TRUE;
1090 (void) printf(gettext("\tcache\n"));
1091 for (c = 0; c < l2children; c++) {
1092 vname = zpool_vdev_name(g_zfs, NULL,
1093 l2child[c], name_flags);
1094 (void) printf("\t %s\n", vname);
1095 free(vname);
1096 }
1097 }
1098 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1099 &l2child, &l2children) == 0 && l2children) {
1100 if (!hadcache)
1101 (void) printf(gettext("\tcache\n"));
1102 for (c = 0; c < l2children; c++) {
1103 vname = zpool_vdev_name(g_zfs, NULL,
1104 l2child[c], name_flags);
1105 (void) printf("\t %s\n", vname);
1106 free(vname);
1107 }
1108 }
1109 /* And finally the spares */
1110 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES,
1111 &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1112 hadspare = B_TRUE;
1113 (void) printf(gettext("\tspares\n"));
1114 for (c = 0; c < sparechildren; c++) {
1115 vname = zpool_vdev_name(g_zfs, NULL,
1116 sparechild[c], name_flags);
1117 (void) printf("\t %s\n", vname);
1118 free(vname);
1119 }
1120 }
1121 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1122 &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1123 if (!hadspare)
1124 (void) printf(gettext("\tspares\n"));
1125 for (c = 0; c < sparechildren; c++) {
1126 vname = zpool_vdev_name(g_zfs, NULL,
1127 sparechild[c], name_flags);
1128 (void) printf("\t %s\n", vname);
1129 free(vname);
1130 }
1131 }
1132
1133 ret = 0;
1134 } else {
1135 ret = (zpool_add(zhp, nvroot) != 0);
1136 }
1137
1138 nvlist_free(props);
1139 nvlist_free(nvroot);
1140 zpool_close(zhp);
1141
1142 return (ret);
1143 }
1144
1145 /*
1146 * zpool remove [-npsw] <pool> <vdev> ...
1147 *
1148 * Removes the given vdev from the pool.
1149 */
1150 int
1151 zpool_do_remove(int argc, char **argv)
1152 {
1153 char *poolname;
1154 int i, ret = 0;
1155 zpool_handle_t *zhp = NULL;
1156 boolean_t stop = B_FALSE;
1157 int c;
1158 boolean_t noop = B_FALSE;
1159 boolean_t parsable = B_FALSE;
1160 boolean_t wait = B_FALSE;
1161
1162 /* check options */
1163 while ((c = getopt(argc, argv, "npsw")) != -1) {
1164 switch (c) {
1165 case 'n':
1166 noop = B_TRUE;
1167 break;
1168 case 'p':
1169 parsable = B_TRUE;
1170 break;
1171 case 's':
1172 stop = B_TRUE;
1173 break;
1174 case 'w':
1175 wait = B_TRUE;
1176 break;
1177 case '?':
1178 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1179 optopt);
1180 usage(B_FALSE);
1181 }
1182 }
1183
1184 argc -= optind;
1185 argv += optind;
1186
1187 /* get pool name and check number of arguments */
1188 if (argc < 1) {
1189 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1190 usage(B_FALSE);
1191 }
1192
1193 poolname = argv[0];
1194
1195 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1196 return (1);
1197
1198 if (stop && noop) {
1199 zpool_close(zhp);
1200 (void) fprintf(stderr, gettext("stop request ignored\n"));
1201 return (0);
1202 }
1203
1204 if (stop) {
1205 if (argc > 1) {
1206 (void) fprintf(stderr, gettext("too many arguments\n"));
1207 usage(B_FALSE);
1208 }
1209 if (zpool_vdev_remove_cancel(zhp) != 0)
1210 ret = 1;
1211 if (wait) {
1212 (void) fprintf(stderr, gettext("invalid option "
1213 "combination: -w cannot be used with -s\n"));
1214 usage(B_FALSE);
1215 }
1216 } else {
1217 if (argc < 2) {
1218 (void) fprintf(stderr, gettext("missing device\n"));
1219 usage(B_FALSE);
1220 }
1221
1222 for (i = 1; i < argc; i++) {
1223 if (noop) {
1224 uint64_t size;
1225
1226 if (zpool_vdev_indirect_size(zhp, argv[i],
1227 &size) != 0) {
1228 ret = 1;
1229 break;
1230 }
1231 if (parsable) {
1232 (void) printf("%s %llu\n",
1233 argv[i], (unsigned long long)size);
1234 } else {
1235 char valstr[32];
1236 zfs_nicenum(size, valstr,
1237 sizeof (valstr));
1238 (void) printf("Memory that will be "
1239 "used after removing %s: %s\n",
1240 argv[i], valstr);
1241 }
1242 } else {
1243 if (zpool_vdev_remove(zhp, argv[i]) != 0)
1244 ret = 1;
1245 }
1246 }
1247
1248 if (ret == 0 && wait)
1249 ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE);
1250 }
1251 zpool_close(zhp);
1252
1253 return (ret);
1254 }
1255
1256 /*
1257 * Return 1 if a vdev is active (being used in a pool)
1258 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1259 *
1260 * This is useful for checking if a disk in an active pool is offlined or
1261 * faulted.
1262 */
1263 static int
1264 vdev_is_active(char *vdev_path)
1265 {
1266 int fd;
1267 fd = open(vdev_path, O_EXCL);
1268 if (fd < 0) {
1269 return (1); /* cant open O_EXCL - disk is active */
1270 }
1271
1272 close(fd);
1273 return (0); /* disk is inactive in the pool */
1274 }
1275
1276 /*
1277 * zpool labelclear [-f] <vdev>
1278 *
1279 * -f Force clearing the label for the vdevs which are members of
1280 * the exported or foreign pools.
1281 *
1282 * Verifies that the vdev is not active and zeros out the label information
1283 * on the device.
1284 */
1285 int
1286 zpool_do_labelclear(int argc, char **argv)
1287 {
1288 char vdev[MAXPATHLEN];
1289 char *name = NULL;
1290 struct stat st;
1291 int c, fd = -1, ret = 0;
1292 nvlist_t *config;
1293 pool_state_t state;
1294 boolean_t inuse = B_FALSE;
1295 boolean_t force = B_FALSE;
1296
1297 /* check options */
1298 while ((c = getopt(argc, argv, "f")) != -1) {
1299 switch (c) {
1300 case 'f':
1301 force = B_TRUE;
1302 break;
1303 default:
1304 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1305 optopt);
1306 usage(B_FALSE);
1307 }
1308 }
1309
1310 argc -= optind;
1311 argv += optind;
1312
1313 /* get vdev name */
1314 if (argc < 1) {
1315 (void) fprintf(stderr, gettext("missing vdev name\n"));
1316 usage(B_FALSE);
1317 }
1318 if (argc > 1) {
1319 (void) fprintf(stderr, gettext("too many arguments\n"));
1320 usage(B_FALSE);
1321 }
1322
1323 /*
1324 * Check if we were given absolute path and use it as is.
1325 * Otherwise if the provided vdev name doesn't point to a file,
1326 * try prepending expected disk paths and partition numbers.
1327 */
1328 (void) strlcpy(vdev, argv[0], sizeof (vdev));
1329 if (vdev[0] != '/' && stat(vdev, &st) != 0) {
1330 int error;
1331
1332 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
1333 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
1334 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
1335 error = ENOENT;
1336 }
1337
1338 if (error || (stat(vdev, &st) != 0)) {
1339 (void) fprintf(stderr, gettext(
1340 "failed to find device %s, try specifying absolute "
1341 "path instead\n"), argv[0]);
1342 return (1);
1343 }
1344 }
1345
1346 if ((fd = open(vdev, O_RDWR)) < 0) {
1347 (void) fprintf(stderr, gettext("failed to open %s: %s\n"),
1348 vdev, strerror(errno));
1349 return (1);
1350 }
1351
1352 /*
1353 * Flush all dirty pages for the block device. This should not be
1354 * fatal when the device does not support BLKFLSBUF as would be the
1355 * case for a file vdev.
1356 */
1357 if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY))
1358 (void) fprintf(stderr, gettext("failed to invalidate "
1359 "cache for %s: %s\n"), vdev, strerror(errno));
1360
1361 if (zpool_read_label(fd, &config, NULL) != 0) {
1362 (void) fprintf(stderr,
1363 gettext("failed to read label from %s\n"), vdev);
1364 ret = 1;
1365 goto errout;
1366 }
1367 nvlist_free(config);
1368
1369 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1370 if (ret != 0) {
1371 (void) fprintf(stderr,
1372 gettext("failed to check state for %s\n"), vdev);
1373 ret = 1;
1374 goto errout;
1375 }
1376
1377 if (!inuse)
1378 goto wipe_label;
1379
1380 switch (state) {
1381 default:
1382 case POOL_STATE_ACTIVE:
1383 case POOL_STATE_SPARE:
1384 case POOL_STATE_L2CACHE:
1385 /*
1386 * We allow the user to call 'zpool offline -f'
1387 * on an offlined disk in an active pool. We can check if
1388 * the disk is online by calling vdev_is_active().
1389 */
1390 if (force && !vdev_is_active(vdev))
1391 break;
1392
1393 (void) fprintf(stderr, gettext(
1394 "%s is a member (%s) of pool \"%s\""),
1395 vdev, zpool_pool_state_to_name(state), name);
1396
1397 if (force) {
1398 (void) fprintf(stderr, gettext(
1399 ". Offline the disk first to clear its label."));
1400 }
1401 printf("\n");
1402 ret = 1;
1403 goto errout;
1404
1405 case POOL_STATE_EXPORTED:
1406 if (force)
1407 break;
1408 (void) fprintf(stderr, gettext(
1409 "use '-f' to override the following error:\n"
1410 "%s is a member of exported pool \"%s\"\n"),
1411 vdev, name);
1412 ret = 1;
1413 goto errout;
1414
1415 case POOL_STATE_POTENTIALLY_ACTIVE:
1416 if (force)
1417 break;
1418 (void) fprintf(stderr, gettext(
1419 "use '-f' to override the following error:\n"
1420 "%s is a member of potentially active pool \"%s\"\n"),
1421 vdev, name);
1422 ret = 1;
1423 goto errout;
1424
1425 case POOL_STATE_DESTROYED:
1426 /* inuse should never be set for a destroyed pool */
1427 assert(0);
1428 break;
1429 }
1430
1431 wipe_label:
1432 ret = zpool_clear_label(fd);
1433 if (ret != 0) {
1434 (void) fprintf(stderr,
1435 gettext("failed to clear label for %s\n"), vdev);
1436 }
1437
1438 errout:
1439 free(name);
1440 (void) close(fd);
1441
1442 return (ret);
1443 }
1444
1445 /*
1446 * zpool create [-fnd] [-o property=value] ...
1447 * [-O file-system-property=value] ...
1448 * [-R root] [-m mountpoint] <pool> <dev> ...
1449 *
1450 * -f Force creation, even if devices appear in use
1451 * -n Do not create the pool, but display the resulting layout if it
1452 * were to be created.
1453 * -R Create a pool under an alternate root
1454 * -m Set default mountpoint for the root dataset. By default it's
1455 * '/<pool>'
1456 * -o Set property=value.
1457 * -o Set feature@feature=enabled|disabled.
1458 * -d Don't automatically enable all supported pool features
1459 * (individual features can be enabled with -o).
1460 * -O Set fsproperty=value in the pool's root file system
1461 *
1462 * Creates the named pool according to the given vdev specification. The
1463 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1464 * Once we get the nvlist back from make_root_vdev(), we either print out the
1465 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1466 */
1467 int
1468 zpool_do_create(int argc, char **argv)
1469 {
1470 boolean_t force = B_FALSE;
1471 boolean_t dryrun = B_FALSE;
1472 boolean_t enable_pool_features = B_TRUE;
1473
1474 int c;
1475 nvlist_t *nvroot = NULL;
1476 char *poolname;
1477 char *tname = NULL;
1478 int ret = 1;
1479 char *altroot = NULL;
1480 char *compat = NULL;
1481 char *mountpoint = NULL;
1482 nvlist_t *fsprops = NULL;
1483 nvlist_t *props = NULL;
1484 char *propval;
1485
1486 /* check options */
1487 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1488 switch (c) {
1489 case 'f':
1490 force = B_TRUE;
1491 break;
1492 case 'n':
1493 dryrun = B_TRUE;
1494 break;
1495 case 'd':
1496 enable_pool_features = B_FALSE;
1497 break;
1498 case 'R':
1499 altroot = optarg;
1500 if (add_prop_list(zpool_prop_to_name(
1501 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
1502 goto errout;
1503 if (add_prop_list_default(zpool_prop_to_name(
1504 ZPOOL_PROP_CACHEFILE), "none", &props))
1505 goto errout;
1506 break;
1507 case 'm':
1508 /* Equivalent to -O mountpoint=optarg */
1509 mountpoint = optarg;
1510 break;
1511 case 'o':
1512 if ((propval = strchr(optarg, '=')) == NULL) {
1513 (void) fprintf(stderr, gettext("missing "
1514 "'=' for -o option\n"));
1515 goto errout;
1516 }
1517 *propval = '\0';
1518 propval++;
1519
1520 if (add_prop_list(optarg, propval, &props, B_TRUE))
1521 goto errout;
1522
1523 /*
1524 * If the user is creating a pool that doesn't support
1525 * feature flags, don't enable any features.
1526 */
1527 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
1528 char *end;
1529 u_longlong_t ver;
1530
1531 ver = strtoull(propval, &end, 10);
1532 if (*end == '\0' &&
1533 ver < SPA_VERSION_FEATURES) {
1534 enable_pool_features = B_FALSE;
1535 }
1536 }
1537 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
1538 altroot = propval;
1539 if (zpool_name_to_prop(optarg) ==
1540 ZPOOL_PROP_COMPATIBILITY)
1541 compat = propval;
1542 break;
1543 case 'O':
1544 if ((propval = strchr(optarg, '=')) == NULL) {
1545 (void) fprintf(stderr, gettext("missing "
1546 "'=' for -O option\n"));
1547 goto errout;
1548 }
1549 *propval = '\0';
1550 propval++;
1551
1552 /*
1553 * Mountpoints are checked and then added later.
1554 * Uniquely among properties, they can be specified
1555 * more than once, to avoid conflict with -m.
1556 */
1557 if (0 == strcmp(optarg,
1558 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
1559 mountpoint = propval;
1560 } else if (add_prop_list(optarg, propval, &fsprops,
1561 B_FALSE)) {
1562 goto errout;
1563 }
1564 break;
1565 case 't':
1566 /*
1567 * Sanity check temporary pool name.
1568 */
1569 if (strchr(optarg, '/') != NULL) {
1570 (void) fprintf(stderr, gettext("cannot create "
1571 "'%s': invalid character '/' in temporary "
1572 "name\n"), optarg);
1573 (void) fprintf(stderr, gettext("use 'zfs "
1574 "create' to create a dataset\n"));
1575 goto errout;
1576 }
1577
1578 if (add_prop_list(zpool_prop_to_name(
1579 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
1580 goto errout;
1581 if (add_prop_list_default(zpool_prop_to_name(
1582 ZPOOL_PROP_CACHEFILE), "none", &props))
1583 goto errout;
1584 tname = optarg;
1585 break;
1586 case ':':
1587 (void) fprintf(stderr, gettext("missing argument for "
1588 "'%c' option\n"), optopt);
1589 goto badusage;
1590 case '?':
1591 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1592 optopt);
1593 goto badusage;
1594 }
1595 }
1596
1597 argc -= optind;
1598 argv += optind;
1599
1600 /* get pool name and check number of arguments */
1601 if (argc < 1) {
1602 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1603 goto badusage;
1604 }
1605 if (argc < 2) {
1606 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1607 goto badusage;
1608 }
1609
1610 poolname = argv[0];
1611
1612 /*
1613 * As a special case, check for use of '/' in the name, and direct the
1614 * user to use 'zfs create' instead.
1615 */
1616 if (strchr(poolname, '/') != NULL) {
1617 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
1618 "character '/' in pool name\n"), poolname);
1619 (void) fprintf(stderr, gettext("use 'zfs create' to "
1620 "create a dataset\n"));
1621 goto errout;
1622 }
1623
1624 /* pass off to make_root_vdev for bulk processing */
1625 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
1626 argc - 1, argv + 1);
1627 if (nvroot == NULL)
1628 goto errout;
1629
1630 /* make_root_vdev() allows 0 toplevel children if there are spares */
1631 if (!zfs_allocatable_devs(nvroot)) {
1632 (void) fprintf(stderr, gettext("invalid vdev "
1633 "specification: at least one toplevel vdev must be "
1634 "specified\n"));
1635 goto errout;
1636 }
1637
1638 if (altroot != NULL && altroot[0] != '/') {
1639 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
1640 "must be an absolute path\n"), altroot);
1641 goto errout;
1642 }
1643
1644 /*
1645 * Check the validity of the mountpoint and direct the user to use the
1646 * '-m' mountpoint option if it looks like its in use.
1647 */
1648 if (mountpoint == NULL ||
1649 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
1650 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
1651 char buf[MAXPATHLEN];
1652 DIR *dirp;
1653
1654 if (mountpoint && mountpoint[0] != '/') {
1655 (void) fprintf(stderr, gettext("invalid mountpoint "
1656 "'%s': must be an absolute path, 'legacy', or "
1657 "'none'\n"), mountpoint);
1658 goto errout;
1659 }
1660
1661 if (mountpoint == NULL) {
1662 if (altroot != NULL)
1663 (void) snprintf(buf, sizeof (buf), "%s/%s",
1664 altroot, poolname);
1665 else
1666 (void) snprintf(buf, sizeof (buf), "/%s",
1667 poolname);
1668 } else {
1669 if (altroot != NULL)
1670 (void) snprintf(buf, sizeof (buf), "%s%s",
1671 altroot, mountpoint);
1672 else
1673 (void) snprintf(buf, sizeof (buf), "%s",
1674 mountpoint);
1675 }
1676
1677 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
1678 (void) fprintf(stderr, gettext("mountpoint '%s' : "
1679 "%s\n"), buf, strerror(errno));
1680 (void) fprintf(stderr, gettext("use '-m' "
1681 "option to provide a different default\n"));
1682 goto errout;
1683 } else if (dirp) {
1684 int count = 0;
1685
1686 while (count < 3 && readdir(dirp) != NULL)
1687 count++;
1688 (void) closedir(dirp);
1689
1690 if (count > 2) {
1691 (void) fprintf(stderr, gettext("mountpoint "
1692 "'%s' exists and is not empty\n"), buf);
1693 (void) fprintf(stderr, gettext("use '-m' "
1694 "option to provide a "
1695 "different default\n"));
1696 goto errout;
1697 }
1698 }
1699 }
1700
1701 /*
1702 * Now that the mountpoint's validity has been checked, ensure that
1703 * the property is set appropriately prior to creating the pool.
1704 */
1705 if (mountpoint != NULL) {
1706 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1707 mountpoint, &fsprops, B_FALSE);
1708 if (ret != 0)
1709 goto errout;
1710 }
1711
1712 ret = 1;
1713 if (dryrun) {
1714 /*
1715 * For a dry run invocation, print out a basic message and run
1716 * through all the vdevs in the list and print out in an
1717 * appropriate hierarchy.
1718 */
1719 (void) printf(gettext("would create '%s' with the "
1720 "following layout:\n\n"), poolname);
1721
1722 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
1723 print_vdev_tree(NULL, "dedup", nvroot, 0,
1724 VDEV_ALLOC_BIAS_DEDUP, 0);
1725 print_vdev_tree(NULL, "special", nvroot, 0,
1726 VDEV_ALLOC_BIAS_SPECIAL, 0);
1727 print_vdev_tree(NULL, "logs", nvroot, 0,
1728 VDEV_ALLOC_BIAS_LOG, 0);
1729 print_cache_list(nvroot, 0);
1730 print_spare_list(nvroot, 0);
1731
1732 ret = 0;
1733 } else {
1734 /*
1735 * Load in feature set.
1736 * Note: if compatibility property not given, we'll have
1737 * NULL, which means 'all features'.
1738 */
1739 boolean_t requested_features[SPA_FEATURES];
1740 if (zpool_do_load_compat(compat, requested_features) !=
1741 ZPOOL_COMPATIBILITY_OK)
1742 goto errout;
1743
1744 /*
1745 * props contains list of features to enable.
1746 * For each feature:
1747 * - remove it if feature@name=disabled
1748 * - leave it there if feature@name=enabled
1749 * - add it if:
1750 * - enable_pool_features (ie: no '-d' or '-o version')
1751 * - it's supported by the kernel module
1752 * - it's in the requested feature set
1753 * - warn if it's enabled but not in compat
1754 */
1755 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
1756 char propname[MAXPATHLEN];
1757 char *propval;
1758 zfeature_info_t *feat = &spa_feature_table[i];
1759
1760 (void) snprintf(propname, sizeof (propname),
1761 "feature@%s", feat->fi_uname);
1762
1763 if (!nvlist_lookup_string(props, propname, &propval)) {
1764 if (strcmp(propval,
1765 ZFS_FEATURE_DISABLED) == 0) {
1766 (void) nvlist_remove_all(props,
1767 propname);
1768 } else if (strcmp(propval,
1769 ZFS_FEATURE_ENABLED) == 0 &&
1770 !requested_features[i]) {
1771 (void) fprintf(stderr, gettext(
1772 "Warning: feature \"%s\" enabled "
1773 "but is not in specified "
1774 "'compatibility' feature set.\n"),
1775 feat->fi_uname);
1776 }
1777 } else if (
1778 enable_pool_features &&
1779 feat->fi_zfs_mod_supported &&
1780 requested_features[i]) {
1781 ret = add_prop_list(propname,
1782 ZFS_FEATURE_ENABLED, &props, B_TRUE);
1783 if (ret != 0)
1784 goto errout;
1785 }
1786 }
1787
1788 ret = 1;
1789 if (zpool_create(g_zfs, poolname,
1790 nvroot, props, fsprops) == 0) {
1791 zfs_handle_t *pool = zfs_open(g_zfs,
1792 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
1793 if (pool != NULL) {
1794 if (zfs_mount(pool, NULL, 0) == 0) {
1795 ret = zfs_share(pool, NULL);
1796 zfs_commit_shares(NULL);
1797 }
1798 zfs_close(pool);
1799 }
1800 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
1801 (void) fprintf(stderr, gettext("pool name may have "
1802 "been omitted\n"));
1803 }
1804 }
1805
1806 errout:
1807 nvlist_free(nvroot);
1808 nvlist_free(fsprops);
1809 nvlist_free(props);
1810 return (ret);
1811 badusage:
1812 nvlist_free(fsprops);
1813 nvlist_free(props);
1814 usage(B_FALSE);
1815 return (2);
1816 }
1817
1818 /*
1819 * zpool destroy <pool>
1820 *
1821 * -f Forcefully unmount any datasets
1822 *
1823 * Destroy the given pool. Automatically unmounts any datasets in the pool.
1824 */
1825 int
1826 zpool_do_destroy(int argc, char **argv)
1827 {
1828 boolean_t force = B_FALSE;
1829 int c;
1830 char *pool;
1831 zpool_handle_t *zhp;
1832 int ret;
1833
1834 /* check options */
1835 while ((c = getopt(argc, argv, "f")) != -1) {
1836 switch (c) {
1837 case 'f':
1838 force = B_TRUE;
1839 break;
1840 case '?':
1841 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1842 optopt);
1843 usage(B_FALSE);
1844 }
1845 }
1846
1847 argc -= optind;
1848 argv += optind;
1849
1850 /* check arguments */
1851 if (argc < 1) {
1852 (void) fprintf(stderr, gettext("missing pool argument\n"));
1853 usage(B_FALSE);
1854 }
1855 if (argc > 1) {
1856 (void) fprintf(stderr, gettext("too many arguments\n"));
1857 usage(B_FALSE);
1858 }
1859
1860 pool = argv[0];
1861
1862 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
1863 /*
1864 * As a special case, check for use of '/' in the name, and
1865 * direct the user to use 'zfs destroy' instead.
1866 */
1867 if (strchr(pool, '/') != NULL)
1868 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
1869 "destroy a dataset\n"));
1870 return (1);
1871 }
1872
1873 if (zpool_disable_datasets(zhp, force) != 0) {
1874 (void) fprintf(stderr, gettext("could not destroy '%s': "
1875 "could not unmount datasets\n"), zpool_get_name(zhp));
1876 zpool_close(zhp);
1877 return (1);
1878 }
1879
1880 /* The history must be logged as part of the export */
1881 log_history = B_FALSE;
1882
1883 ret = (zpool_destroy(zhp, history_str) != 0);
1884
1885 zpool_close(zhp);
1886
1887 return (ret);
1888 }
1889
1890 typedef struct export_cbdata {
1891 boolean_t force;
1892 boolean_t hardforce;
1893 } export_cbdata_t;
1894
1895 /*
1896 * Export one pool
1897 */
1898 static int
1899 zpool_export_one(zpool_handle_t *zhp, void *data)
1900 {
1901 export_cbdata_t *cb = data;
1902
1903 if (zpool_disable_datasets(zhp, cb->force) != 0)
1904 return (1);
1905
1906 /* The history must be logged as part of the export */
1907 log_history = B_FALSE;
1908
1909 if (cb->hardforce) {
1910 if (zpool_export_force(zhp, history_str) != 0)
1911 return (1);
1912 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
1913 return (1);
1914 }
1915
1916 return (0);
1917 }
1918
1919 /*
1920 * zpool export [-f] <pool> ...
1921 *
1922 * -a Export all pools
1923 * -f Forcefully unmount datasets
1924 *
1925 * Export the given pools. By default, the command will attempt to cleanly
1926 * unmount any active datasets within the pool. If the '-f' flag is specified,
1927 * then the datasets will be forcefully unmounted.
1928 */
1929 int
1930 zpool_do_export(int argc, char **argv)
1931 {
1932 export_cbdata_t cb;
1933 boolean_t do_all = B_FALSE;
1934 boolean_t force = B_FALSE;
1935 boolean_t hardforce = B_FALSE;
1936 int c, ret;
1937
1938 /* check options */
1939 while ((c = getopt(argc, argv, "afF")) != -1) {
1940 switch (c) {
1941 case 'a':
1942 do_all = B_TRUE;
1943 break;
1944 case 'f':
1945 force = B_TRUE;
1946 break;
1947 case 'F':
1948 hardforce = B_TRUE;
1949 break;
1950 case '?':
1951 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1952 optopt);
1953 usage(B_FALSE);
1954 }
1955 }
1956
1957 cb.force = force;
1958 cb.hardforce = hardforce;
1959 argc -= optind;
1960 argv += optind;
1961
1962 if (do_all) {
1963 if (argc != 0) {
1964 (void) fprintf(stderr, gettext("too many arguments\n"));
1965 usage(B_FALSE);
1966 }
1967
1968 return (for_each_pool(argc, argv, B_TRUE, NULL,
1969 ZFS_TYPE_POOL, B_FALSE, zpool_export_one, &cb));
1970 }
1971
1972 /* check arguments */
1973 if (argc < 1) {
1974 (void) fprintf(stderr, gettext("missing pool argument\n"));
1975 usage(B_FALSE);
1976 }
1977
1978 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
1979 B_FALSE, zpool_export_one, &cb);
1980
1981 return (ret);
1982 }
1983
1984 /*
1985 * Given a vdev configuration, determine the maximum width needed for the device
1986 * name column.
1987 */
1988 static int
1989 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
1990 int name_flags)
1991 {
1992 static const char *const subtypes[] =
1993 {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN};
1994
1995 char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
1996 max = MAX(strlen(name) + depth, max);
1997 free(name);
1998
1999 nvlist_t **child;
2000 uint_t children;
2001 for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i)
2002 if (nvlist_lookup_nvlist_array(nv, subtypes[i],
2003 &child, &children) == 0)
2004 for (uint_t c = 0; c < children; ++c)
2005 max = MAX(max_width(zhp, child[c], depth + 2,
2006 max, name_flags), max);
2007
2008 return (max);
2009 }
2010
2011 typedef struct spare_cbdata {
2012 uint64_t cb_guid;
2013 zpool_handle_t *cb_zhp;
2014 } spare_cbdata_t;
2015
2016 static boolean_t
2017 find_vdev(nvlist_t *nv, uint64_t search)
2018 {
2019 uint64_t guid;
2020 nvlist_t **child;
2021 uint_t c, children;
2022
2023 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
2024 search == guid)
2025 return (B_TRUE);
2026
2027 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2028 &child, &children) == 0) {
2029 for (c = 0; c < children; c++)
2030 if (find_vdev(child[c], search))
2031 return (B_TRUE);
2032 }
2033
2034 return (B_FALSE);
2035 }
2036
2037 static int
2038 find_spare(zpool_handle_t *zhp, void *data)
2039 {
2040 spare_cbdata_t *cbp = data;
2041 nvlist_t *config, *nvroot;
2042
2043 config = zpool_get_config(zhp, NULL);
2044 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2045 &nvroot) == 0);
2046
2047 if (find_vdev(nvroot, cbp->cb_guid)) {
2048 cbp->cb_zhp = zhp;
2049 return (1);
2050 }
2051
2052 zpool_close(zhp);
2053 return (0);
2054 }
2055
2056 typedef struct status_cbdata {
2057 int cb_count;
2058 int cb_name_flags;
2059 int cb_namewidth;
2060 boolean_t cb_allpools;
2061 boolean_t cb_verbose;
2062 boolean_t cb_literal;
2063 boolean_t cb_explain;
2064 boolean_t cb_first;
2065 boolean_t cb_dedup_stats;
2066 boolean_t cb_print_status;
2067 boolean_t cb_print_slow_ios;
2068 boolean_t cb_print_vdev_init;
2069 boolean_t cb_print_vdev_trim;
2070 vdev_cmd_data_list_t *vcdl;
2071 } status_cbdata_t;
2072
2073 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2074 static boolean_t
2075 is_blank_str(const char *str)
2076 {
2077 for (; str != NULL && *str != '\0'; ++str)
2078 if (!isblank(*str))
2079 return (B_FALSE);
2080 return (B_TRUE);
2081 }
2082
2083 /* Print command output lines for specific vdev in a specific pool */
2084 static void
2085 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, char *path)
2086 {
2087 vdev_cmd_data_t *data;
2088 int i, j;
2089 const char *val;
2090
2091 for (i = 0; i < vcdl->count; i++) {
2092 if ((strcmp(vcdl->data[i].path, path) != 0) ||
2093 (strcmp(vcdl->data[i].pool, pool) != 0)) {
2094 /* Not the vdev we're looking for */
2095 continue;
2096 }
2097
2098 data = &vcdl->data[i];
2099 /* Print out all the output values for this vdev */
2100 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
2101 val = NULL;
2102 /* Does this vdev have values for this column? */
2103 for (int k = 0; k < data->cols_cnt; k++) {
2104 if (strcmp(data->cols[k],
2105 vcdl->uniq_cols[j]) == 0) {
2106 /* yes it does, record the value */
2107 val = data->lines[k];
2108 break;
2109 }
2110 }
2111 /*
2112 * Mark empty values with dashes to make output
2113 * awk-able.
2114 */
2115 if (val == NULL || is_blank_str(val))
2116 val = "-";
2117
2118 printf("%*s", vcdl->uniq_cols_width[j], val);
2119 if (j < vcdl->uniq_cols_cnt - 1)
2120 fputs(" ", stdout);
2121 }
2122
2123 /* Print out any values that aren't in a column at the end */
2124 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
2125 /* Did we have any columns? If so print a spacer. */
2126 if (vcdl->uniq_cols_cnt > 0)
2127 fputs(" ", stdout);
2128
2129 val = data->lines[j];
2130 fputs(val ?: "", stdout);
2131 }
2132 break;
2133 }
2134 }
2135
2136 /*
2137 * Print vdev initialization status for leaves
2138 */
2139 static void
2140 print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
2141 {
2142 if (verbose) {
2143 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
2144 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
2145 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
2146 !vs->vs_scan_removing) {
2147 char zbuf[1024];
2148 char tbuf[256];
2149 struct tm zaction_ts;
2150
2151 time_t t = vs->vs_initialize_action_time;
2152 int initialize_pct = 100;
2153 if (vs->vs_initialize_state !=
2154 VDEV_INITIALIZE_COMPLETE) {
2155 initialize_pct = (vs->vs_initialize_bytes_done *
2156 100 / (vs->vs_initialize_bytes_est + 1));
2157 }
2158
2159 (void) localtime_r(&t, &zaction_ts);
2160 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
2161
2162 switch (vs->vs_initialize_state) {
2163 case VDEV_INITIALIZE_SUSPENDED:
2164 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2165 gettext("suspended, started at"), tbuf);
2166 break;
2167 case VDEV_INITIALIZE_ACTIVE:
2168 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2169 gettext("started at"), tbuf);
2170 break;
2171 case VDEV_INITIALIZE_COMPLETE:
2172 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2173 gettext("completed at"), tbuf);
2174 break;
2175 }
2176
2177 (void) printf(gettext(" (%d%% initialized%s)"),
2178 initialize_pct, zbuf);
2179 } else {
2180 (void) printf(gettext(" (uninitialized)"));
2181 }
2182 } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) {
2183 (void) printf(gettext(" (initializing)"));
2184 }
2185 }
2186
2187 /*
2188 * Print vdev TRIM status for leaves
2189 */
2190 static void
2191 print_status_trim(vdev_stat_t *vs, boolean_t verbose)
2192 {
2193 if (verbose) {
2194 if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE ||
2195 vs->vs_trim_state == VDEV_TRIM_SUSPENDED ||
2196 vs->vs_trim_state == VDEV_TRIM_COMPLETE) &&
2197 !vs->vs_scan_removing) {
2198 char zbuf[1024];
2199 char tbuf[256];
2200 struct tm zaction_ts;
2201
2202 time_t t = vs->vs_trim_action_time;
2203 int trim_pct = 100;
2204 if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) {
2205 trim_pct = (vs->vs_trim_bytes_done *
2206 100 / (vs->vs_trim_bytes_est + 1));
2207 }
2208
2209 (void) localtime_r(&t, &zaction_ts);
2210 (void) strftime(tbuf, sizeof (tbuf), "%c", &zaction_ts);
2211
2212 switch (vs->vs_trim_state) {
2213 case VDEV_TRIM_SUSPENDED:
2214 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2215 gettext("suspended, started at"), tbuf);
2216 break;
2217 case VDEV_TRIM_ACTIVE:
2218 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2219 gettext("started at"), tbuf);
2220 break;
2221 case VDEV_TRIM_COMPLETE:
2222 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2223 gettext("completed at"), tbuf);
2224 break;
2225 }
2226
2227 (void) printf(gettext(" (%d%% trimmed%s)"),
2228 trim_pct, zbuf);
2229 } else if (vs->vs_trim_notsup) {
2230 (void) printf(gettext(" (trim unsupported)"));
2231 } else {
2232 (void) printf(gettext(" (untrimmed)"));
2233 }
2234 } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) {
2235 (void) printf(gettext(" (trimming)"));
2236 }
2237 }
2238
2239 /*
2240 * Return the color associated with a health string. This includes returning
2241 * NULL for no color change.
2242 */
2243 static const char *
2244 health_str_to_color(const char *health)
2245 {
2246 if (strcmp(health, gettext("FAULTED")) == 0 ||
2247 strcmp(health, gettext("SUSPENDED")) == 0 ||
2248 strcmp(health, gettext("UNAVAIL")) == 0) {
2249 return (ANSI_RED);
2250 }
2251
2252 if (strcmp(health, gettext("OFFLINE")) == 0 ||
2253 strcmp(health, gettext("DEGRADED")) == 0 ||
2254 strcmp(health, gettext("REMOVED")) == 0) {
2255 return (ANSI_YELLOW);
2256 }
2257
2258 return (NULL);
2259 }
2260
2261 /*
2262 * Print out configuration state as requested by status_callback.
2263 */
2264 static void
2265 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
2266 nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs)
2267 {
2268 nvlist_t **child, *root;
2269 uint_t c, i, vsc, children;
2270 pool_scan_stat_t *ps = NULL;
2271 vdev_stat_t *vs;
2272 char rbuf[6], wbuf[6], cbuf[6];
2273 char *vname;
2274 uint64_t notpresent;
2275 spare_cbdata_t spare_cb;
2276 const char *state;
2277 char *type;
2278 char *path = NULL;
2279 const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL;
2280
2281 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2282 &child, &children) != 0)
2283 children = 0;
2284
2285 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2286 (uint64_t **)&vs, &vsc) == 0);
2287
2288 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2289
2290 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2291 return;
2292
2293 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2294
2295 if (isspare) {
2296 /*
2297 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2298 * online drives.
2299 */
2300 if (vs->vs_aux == VDEV_AUX_SPARED)
2301 state = gettext("INUSE");
2302 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2303 state = gettext("AVAIL");
2304 }
2305
2306 printf_color(health_str_to_color(state),
2307 "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
2308 name, state);
2309
2310 if (!isspare) {
2311 if (vs->vs_read_errors)
2312 rcolor = ANSI_RED;
2313
2314 if (vs->vs_write_errors)
2315 wcolor = ANSI_RED;
2316
2317 if (vs->vs_checksum_errors)
2318 ccolor = ANSI_RED;
2319
2320 if (cb->cb_literal) {
2321 fputc(' ', stdout);
2322 printf_color(rcolor, "%5llu",
2323 (u_longlong_t)vs->vs_read_errors);
2324 fputc(' ', stdout);
2325 printf_color(wcolor, "%5llu",
2326 (u_longlong_t)vs->vs_write_errors);
2327 fputc(' ', stdout);
2328 printf_color(ccolor, "%5llu",
2329 (u_longlong_t)vs->vs_checksum_errors);
2330 } else {
2331 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2332 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2333 zfs_nicenum(vs->vs_checksum_errors, cbuf,
2334 sizeof (cbuf));
2335 fputc(' ', stdout);
2336 printf_color(rcolor, "%5s", rbuf);
2337 fputc(' ', stdout);
2338 printf_color(wcolor, "%5s", wbuf);
2339 fputc(' ', stdout);
2340 printf_color(ccolor, "%5s", cbuf);
2341 }
2342 if (cb->cb_print_slow_ios) {
2343 if (children == 0) {
2344 /* Only leafs vdevs have slow IOs */
2345 zfs_nicenum(vs->vs_slow_ios, rbuf,
2346 sizeof (rbuf));
2347 } else {
2348 snprintf(rbuf, sizeof (rbuf), "-");
2349 }
2350
2351 if (cb->cb_literal)
2352 printf(" %5llu", (u_longlong_t)vs->vs_slow_ios);
2353 else
2354 printf(" %5s", rbuf);
2355 }
2356 }
2357
2358 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2359 &notpresent) == 0) {
2360 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
2361 (void) printf(" %s %s", gettext("was"), path);
2362 } else if (vs->vs_aux != 0) {
2363 (void) printf(" ");
2364 color_start(ANSI_RED);
2365 switch (vs->vs_aux) {
2366 case VDEV_AUX_OPEN_FAILED:
2367 (void) printf(gettext("cannot open"));
2368 break;
2369
2370 case VDEV_AUX_BAD_GUID_SUM:
2371 (void) printf(gettext("missing device"));
2372 break;
2373
2374 case VDEV_AUX_NO_REPLICAS:
2375 (void) printf(gettext("insufficient replicas"));
2376 break;
2377
2378 case VDEV_AUX_VERSION_NEWER:
2379 (void) printf(gettext("newer version"));
2380 break;
2381
2382 case VDEV_AUX_UNSUP_FEAT:
2383 (void) printf(gettext("unsupported feature(s)"));
2384 break;
2385
2386 case VDEV_AUX_ASHIFT_TOO_BIG:
2387 (void) printf(gettext("unsupported minimum blocksize"));
2388 break;
2389
2390 case VDEV_AUX_SPARED:
2391 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2392 &spare_cb.cb_guid) == 0);
2393 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
2394 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
2395 zpool_get_name(zhp)) == 0)
2396 (void) printf(gettext("currently in "
2397 "use"));
2398 else
2399 (void) printf(gettext("in use by "
2400 "pool '%s'"),
2401 zpool_get_name(spare_cb.cb_zhp));
2402 zpool_close(spare_cb.cb_zhp);
2403 } else {
2404 (void) printf(gettext("currently in use"));
2405 }
2406 break;
2407
2408 case VDEV_AUX_ERR_EXCEEDED:
2409 (void) printf(gettext("too many errors"));
2410 break;
2411
2412 case VDEV_AUX_IO_FAILURE:
2413 (void) printf(gettext("experienced I/O failures"));
2414 break;
2415
2416 case VDEV_AUX_BAD_LOG:
2417 (void) printf(gettext("bad intent log"));
2418 break;
2419
2420 case VDEV_AUX_EXTERNAL:
2421 (void) printf(gettext("external device fault"));
2422 break;
2423
2424 case VDEV_AUX_SPLIT_POOL:
2425 (void) printf(gettext("split into new pool"));
2426 break;
2427
2428 case VDEV_AUX_ACTIVE:
2429 (void) printf(gettext("currently in use"));
2430 break;
2431
2432 case VDEV_AUX_CHILDREN_OFFLINE:
2433 (void) printf(gettext("all children offline"));
2434 break;
2435
2436 case VDEV_AUX_BAD_LABEL:
2437 (void) printf(gettext("invalid label"));
2438 break;
2439
2440 default:
2441 (void) printf(gettext("corrupted data"));
2442 break;
2443 }
2444 color_end();
2445 } else if (children == 0 && !isspare &&
2446 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL &&
2447 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
2448 vs->vs_configured_ashift < vs->vs_physical_ashift) {
2449 (void) printf(
2450 gettext(" block size: %dB configured, %dB native"),
2451 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
2452 }
2453
2454 if (vs->vs_scan_removing != 0) {
2455 (void) printf(gettext(" (removing)"));
2456 } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) {
2457 (void) printf(gettext(" (non-allocating)"));
2458 }
2459
2460 /* The root vdev has the scrub/resilver stats */
2461 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2462 ZPOOL_CONFIG_VDEV_TREE);
2463 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
2464 (uint64_t **)&ps, &c);
2465
2466 /*
2467 * If you force fault a drive that's resilvering, its scan stats can
2468 * get frozen in time, giving the false impression that it's
2469 * being resilvered. That's why we check the state to see if the vdev
2470 * is healthy before reporting "resilvering" or "repairing".
2471 */
2472 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 &&
2473 vs->vs_state == VDEV_STATE_HEALTHY) {
2474 if (vs->vs_scan_processed != 0) {
2475 (void) printf(gettext(" (%s)"),
2476 (ps->pss_func == POOL_SCAN_RESILVER) ?
2477 "resilvering" : "repairing");
2478 } else if (vs->vs_resilver_deferred) {
2479 (void) printf(gettext(" (awaiting resilver)"));
2480 }
2481 }
2482
2483 /* The top-level vdevs have the rebuild stats */
2484 if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE &&
2485 children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) {
2486 if (vs->vs_rebuild_processed != 0) {
2487 (void) printf(gettext(" (resilvering)"));
2488 }
2489 }
2490
2491 if (cb->vcdl != NULL) {
2492 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2493 printf(" ");
2494 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
2495 }
2496 }
2497
2498 /* Display vdev initialization and trim status for leaves. */
2499 if (children == 0) {
2500 print_status_initialize(vs, cb->cb_print_vdev_init);
2501 print_status_trim(vs, cb->cb_print_vdev_trim);
2502 }
2503
2504 (void) printf("\n");
2505
2506 for (c = 0; c < children; c++) {
2507 uint64_t islog = B_FALSE, ishole = B_FALSE;
2508
2509 /* Don't print logs or holes here */
2510 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2511 &islog);
2512 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2513 &ishole);
2514 if (islog || ishole)
2515 continue;
2516 /* Only print normal classes here */
2517 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2518 continue;
2519
2520 /* Provide vdev_rebuild_stats to children if available */
2521 if (vrs == NULL) {
2522 (void) nvlist_lookup_uint64_array(nv,
2523 ZPOOL_CONFIG_REBUILD_STATS,
2524 (uint64_t **)&vrs, &i);
2525 }
2526
2527 vname = zpool_vdev_name(g_zfs, zhp, child[c],
2528 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2529 print_status_config(zhp, cb, vname, child[c], depth + 2,
2530 isspare, vrs);
2531 free(vname);
2532 }
2533 }
2534
2535 /*
2536 * Print the configuration of an exported pool. Iterate over all vdevs in the
2537 * pool, printing out the name and status for each one.
2538 */
2539 static void
2540 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
2541 int depth)
2542 {
2543 nvlist_t **child;
2544 uint_t c, children;
2545 vdev_stat_t *vs;
2546 char *type, *vname;
2547
2548 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2549 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
2550 strcmp(type, VDEV_TYPE_HOLE) == 0)
2551 return;
2552
2553 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2554 (uint64_t **)&vs, &c) == 0);
2555
2556 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
2557 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
2558
2559 if (vs->vs_aux != 0) {
2560 (void) printf(" ");
2561
2562 switch (vs->vs_aux) {
2563 case VDEV_AUX_OPEN_FAILED:
2564 (void) printf(gettext("cannot open"));
2565 break;
2566
2567 case VDEV_AUX_BAD_GUID_SUM:
2568 (void) printf(gettext("missing device"));
2569 break;
2570
2571 case VDEV_AUX_NO_REPLICAS:
2572 (void) printf(gettext("insufficient replicas"));
2573 break;
2574
2575 case VDEV_AUX_VERSION_NEWER:
2576 (void) printf(gettext("newer version"));
2577 break;
2578
2579 case VDEV_AUX_UNSUP_FEAT:
2580 (void) printf(gettext("unsupported feature(s)"));
2581 break;
2582
2583 case VDEV_AUX_ERR_EXCEEDED:
2584 (void) printf(gettext("too many errors"));
2585 break;
2586
2587 case VDEV_AUX_ACTIVE:
2588 (void) printf(gettext("currently in use"));
2589 break;
2590
2591 case VDEV_AUX_CHILDREN_OFFLINE:
2592 (void) printf(gettext("all children offline"));
2593 break;
2594
2595 case VDEV_AUX_BAD_LABEL:
2596 (void) printf(gettext("invalid label"));
2597 break;
2598
2599 default:
2600 (void) printf(gettext("corrupted data"));
2601 break;
2602 }
2603 }
2604 (void) printf("\n");
2605
2606 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2607 &child, &children) != 0)
2608 return;
2609
2610 for (c = 0; c < children; c++) {
2611 uint64_t is_log = B_FALSE;
2612
2613 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2614 &is_log);
2615 if (is_log)
2616 continue;
2617 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
2618 continue;
2619
2620 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2621 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2622 print_import_config(cb, vname, child[c], depth + 2);
2623 free(vname);
2624 }
2625
2626 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2627 &child, &children) == 0) {
2628 (void) printf(gettext("\tcache\n"));
2629 for (c = 0; c < children; c++) {
2630 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2631 cb->cb_name_flags);
2632 (void) printf("\t %s\n", vname);
2633 free(vname);
2634 }
2635 }
2636
2637 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2638 &child, &children) == 0) {
2639 (void) printf(gettext("\tspares\n"));
2640 for (c = 0; c < children; c++) {
2641 vname = zpool_vdev_name(g_zfs, NULL, child[c],
2642 cb->cb_name_flags);
2643 (void) printf("\t %s\n", vname);
2644 free(vname);
2645 }
2646 }
2647 }
2648
2649 /*
2650 * Print specialized class vdevs.
2651 *
2652 * These are recorded as top level vdevs in the main pool child array
2653 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
2654 * print_status_config() or print_import_config() to print the top level
2655 * class vdevs then any of their children (eg mirrored slogs) are printed
2656 * recursively - which works because only the top level vdev is marked.
2657 */
2658 static void
2659 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
2660 const char *class)
2661 {
2662 uint_t c, children;
2663 nvlist_t **child;
2664 boolean_t printed = B_FALSE;
2665
2666 assert(zhp != NULL || !cb->cb_verbose);
2667
2668 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
2669 &children) != 0)
2670 return;
2671
2672 for (c = 0; c < children; c++) {
2673 uint64_t is_log = B_FALSE;
2674 char *bias = NULL;
2675 char *type = NULL;
2676
2677 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2678 &is_log);
2679
2680 if (is_log) {
2681 bias = (char *)VDEV_ALLOC_CLASS_LOGS;
2682 } else {
2683 (void) nvlist_lookup_string(child[c],
2684 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
2685 (void) nvlist_lookup_string(child[c],
2686 ZPOOL_CONFIG_TYPE, &type);
2687 }
2688
2689 if (bias == NULL || strcmp(bias, class) != 0)
2690 continue;
2691 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2692 continue;
2693
2694 if (!printed) {
2695 (void) printf("\t%s\t\n", gettext(class));
2696 printed = B_TRUE;
2697 }
2698
2699 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
2700 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
2701 if (cb->cb_print_status)
2702 print_status_config(zhp, cb, name, child[c], 2,
2703 B_FALSE, NULL);
2704 else
2705 print_import_config(cb, name, child[c], 2);
2706 free(name);
2707 }
2708 }
2709
2710 /*
2711 * Display the status for the given pool.
2712 */
2713 static int
2714 show_import(nvlist_t *config, boolean_t report_error)
2715 {
2716 uint64_t pool_state;
2717 vdev_stat_t *vs;
2718 char *name;
2719 uint64_t guid;
2720 uint64_t hostid = 0;
2721 const char *msgid;
2722 const char *hostname = "unknown";
2723 nvlist_t *nvroot, *nvinfo;
2724 zpool_status_t reason;
2725 zpool_errata_t errata;
2726 const char *health;
2727 uint_t vsc;
2728 char *comment;
2729 status_cbdata_t cb = { 0 };
2730
2731 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2732 &name) == 0);
2733 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2734 &guid) == 0);
2735 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2736 &pool_state) == 0);
2737 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2738 &nvroot) == 0);
2739
2740 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
2741 (uint64_t **)&vs, &vsc) == 0);
2742 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2743
2744 reason = zpool_import_status(config, &msgid, &errata);
2745
2746 /*
2747 * If we're importing using a cachefile, then we won't report any
2748 * errors unless we are in the scan phase of the import.
2749 */
2750 if (reason != ZPOOL_STATUS_OK && !report_error)
2751 return (reason);
2752
2753 (void) printf(gettext(" pool: %s\n"), name);
2754 (void) printf(gettext(" id: %llu\n"), (u_longlong_t)guid);
2755 (void) printf(gettext(" state: %s"), health);
2756 if (pool_state == POOL_STATE_DESTROYED)
2757 (void) printf(gettext(" (DESTROYED)"));
2758 (void) printf("\n");
2759
2760 switch (reason) {
2761 case ZPOOL_STATUS_MISSING_DEV_R:
2762 case ZPOOL_STATUS_MISSING_DEV_NR:
2763 case ZPOOL_STATUS_BAD_GUID_SUM:
2764 printf_color(ANSI_BOLD, gettext("status: "));
2765 printf_color(ANSI_YELLOW, gettext("One or more devices are "
2766 "missing from the system.\n"));
2767 break;
2768
2769 case ZPOOL_STATUS_CORRUPT_LABEL_R:
2770 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
2771 printf_color(ANSI_BOLD, gettext("status: "));
2772 printf_color(ANSI_YELLOW, gettext("One or more devices contains"
2773 " corrupted data.\n"));
2774 break;
2775
2776 case ZPOOL_STATUS_CORRUPT_DATA:
2777 (void) printf(
2778 gettext(" status: The pool data is corrupted.\n"));
2779 break;
2780
2781 case ZPOOL_STATUS_OFFLINE_DEV:
2782 printf_color(ANSI_BOLD, gettext("status: "));
2783 printf_color(ANSI_YELLOW, gettext("One or more devices "
2784 "are offlined.\n"));
2785 break;
2786
2787 case ZPOOL_STATUS_CORRUPT_POOL:
2788 printf_color(ANSI_BOLD, gettext("status: "));
2789 printf_color(ANSI_YELLOW, gettext("The pool metadata is "
2790 "corrupted.\n"));
2791 break;
2792
2793 case ZPOOL_STATUS_VERSION_OLDER:
2794 printf_color(ANSI_BOLD, gettext("status: "));
2795 printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
2796 "a legacy on-disk version.\n"));
2797 break;
2798
2799 case ZPOOL_STATUS_VERSION_NEWER:
2800 printf_color(ANSI_BOLD, gettext("status: "));
2801 printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
2802 "an incompatible version.\n"));
2803 break;
2804
2805 case ZPOOL_STATUS_FEAT_DISABLED:
2806 printf_color(ANSI_BOLD, gettext("status: "));
2807 printf_color(ANSI_YELLOW, gettext("Some supported "
2808 "features are not enabled on the pool.\n\t"
2809 "(Note that they may be intentionally disabled "
2810 "if the\n\t'compatibility' property is set.)\n"));
2811 break;
2812
2813 case ZPOOL_STATUS_COMPATIBILITY_ERR:
2814 printf_color(ANSI_BOLD, gettext("status: "));
2815 printf_color(ANSI_YELLOW, gettext("Error reading or parsing "
2816 "the file(s) indicated by the 'compatibility'\n"
2817 "property.\n"));
2818 break;
2819
2820 case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
2821 printf_color(ANSI_BOLD, gettext("status: "));
2822 printf_color(ANSI_YELLOW, gettext("One or more features "
2823 "are enabled on the pool despite not being\n"
2824 "requested by the 'compatibility' property.\n"));
2825 break;
2826
2827 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2828 printf_color(ANSI_BOLD, gettext("status: "));
2829 printf_color(ANSI_YELLOW, gettext("The pool uses the following "
2830 "feature(s) not supported on this system:\n"));
2831 color_start(ANSI_YELLOW);
2832 zpool_print_unsup_feat(config);
2833 color_end();
2834 break;
2835
2836 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
2837 printf_color(ANSI_BOLD, gettext("status: "));
2838 printf_color(ANSI_YELLOW, gettext("The pool can only be "
2839 "accessed in read-only mode on this system. It\n\tcannot be"
2840 " accessed in read-write mode because it uses the "
2841 "following\n\tfeature(s) not supported on this system:\n"));
2842 color_start(ANSI_YELLOW);
2843 zpool_print_unsup_feat(config);
2844 color_end();
2845 break;
2846
2847 case ZPOOL_STATUS_HOSTID_ACTIVE:
2848 printf_color(ANSI_BOLD, gettext("status: "));
2849 printf_color(ANSI_YELLOW, gettext("The pool is currently "
2850 "imported by another system.\n"));
2851 break;
2852
2853 case ZPOOL_STATUS_HOSTID_REQUIRED:
2854 printf_color(ANSI_BOLD, gettext("status: "));
2855 printf_color(ANSI_YELLOW, gettext("The pool has the "
2856 "multihost property on. It cannot\n\tbe safely imported "
2857 "when the system hostid is not set.\n"));
2858 break;
2859
2860 case ZPOOL_STATUS_HOSTID_MISMATCH:
2861 printf_color(ANSI_BOLD, gettext("status: "));
2862 printf_color(ANSI_YELLOW, gettext("The pool was last accessed "
2863 "by another system.\n"));
2864 break;
2865
2866 case ZPOOL_STATUS_FAULTED_DEV_R:
2867 case ZPOOL_STATUS_FAULTED_DEV_NR:
2868 printf_color(ANSI_BOLD, gettext("status: "));
2869 printf_color(ANSI_YELLOW, gettext("One or more devices are "
2870 "faulted.\n"));
2871 break;
2872
2873 case ZPOOL_STATUS_BAD_LOG:
2874 printf_color(ANSI_BOLD, gettext("status: "));
2875 printf_color(ANSI_YELLOW, gettext("An intent log record cannot "
2876 "be read.\n"));
2877 break;
2878
2879 case ZPOOL_STATUS_RESILVERING:
2880 case ZPOOL_STATUS_REBUILDING:
2881 printf_color(ANSI_BOLD, gettext("status: "));
2882 printf_color(ANSI_YELLOW, gettext("One or more devices were "
2883 "being resilvered.\n"));
2884 break;
2885
2886 case ZPOOL_STATUS_ERRATA:
2887 printf_color(ANSI_BOLD, gettext("status: "));
2888 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"),
2889 errata);
2890 break;
2891
2892 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
2893 printf_color(ANSI_BOLD, gettext("status: "));
2894 printf_color(ANSI_YELLOW, gettext("One or more devices are "
2895 "configured to use a non-native block size.\n"
2896 "\tExpect reduced performance.\n"));
2897 break;
2898
2899 default:
2900 /*
2901 * No other status can be seen when importing pools.
2902 */
2903 assert(reason == ZPOOL_STATUS_OK);
2904 }
2905
2906 /*
2907 * Print out an action according to the overall state of the pool.
2908 */
2909 if (vs->vs_state == VDEV_STATE_HEALTHY) {
2910 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
2911 reason == ZPOOL_STATUS_FEAT_DISABLED) {
2912 (void) printf(gettext(" action: The pool can be "
2913 "imported using its name or numeric identifier, "
2914 "though\n\tsome features will not be available "
2915 "without an explicit 'zpool upgrade'.\n"));
2916 } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) {
2917 (void) printf(gettext(" action: The pool can be "
2918 "imported using its name or numeric\n\tidentifier, "
2919 "though the file(s) indicated by its "
2920 "'compatibility'\n\tproperty cannot be parsed at "
2921 "this time.\n"));
2922 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
2923 (void) printf(gettext(" action: The pool can be "
2924 "imported using its name or numeric "
2925 "identifier and\n\tthe '-f' flag.\n"));
2926 } else if (reason == ZPOOL_STATUS_ERRATA) {
2927 switch (errata) {
2928 case ZPOOL_ERRATA_NONE:
2929 break;
2930
2931 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
2932 (void) printf(gettext(" action: The pool can "
2933 "be imported using its name or numeric "
2934 "identifier,\n\thowever there is a compat"
2935 "ibility issue which should be corrected"
2936 "\n\tby running 'zpool scrub'\n"));
2937 break;
2938
2939 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
2940 (void) printf(gettext(" action: The pool can"
2941 "not be imported with this version of ZFS "
2942 "due to\n\tan active asynchronous destroy. "
2943 "Revert to an earlier version\n\tand "
2944 "allow the destroy to complete before "
2945 "updating.\n"));
2946 break;
2947
2948 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
2949 (void) printf(gettext(" action: Existing "
2950 "encrypted datasets contain an on-disk "
2951 "incompatibility, which\n\tneeds to be "
2952 "corrected. Backup these datasets to new "
2953 "encrypted datasets\n\tand destroy the "
2954 "old ones.\n"));
2955 break;
2956
2957 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
2958 (void) printf(gettext(" action: Existing "
2959 "encrypted snapshots and bookmarks contain "
2960 "an on-disk\n\tincompatibility. This may "
2961 "cause on-disk corruption if they are used"
2962 "\n\twith 'zfs recv'. To correct the "
2963 "issue, enable the bookmark_v2 feature.\n\t"
2964 "No additional action is needed if there "
2965 "are no encrypted snapshots or\n\t"
2966 "bookmarks. If preserving the encrypted "
2967 "snapshots and bookmarks is\n\trequired, "
2968 "use a non-raw send to backup and restore "
2969 "them. Alternately,\n\tthey may be removed"
2970 " to resolve the incompatibility.\n"));
2971 break;
2972 default:
2973 /*
2974 * All errata must contain an action message.
2975 */
2976 assert(0);
2977 }
2978 } else {
2979 (void) printf(gettext(" action: The pool can be "
2980 "imported using its name or numeric "
2981 "identifier.\n"));
2982 }
2983 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
2984 (void) printf(gettext(" action: The pool can be imported "
2985 "despite missing or damaged devices. The\n\tfault "
2986 "tolerance of the pool may be compromised if imported.\n"));
2987 } else {
2988 switch (reason) {
2989 case ZPOOL_STATUS_VERSION_NEWER:
2990 (void) printf(gettext(" action: The pool cannot be "
2991 "imported. Access the pool on a system running "
2992 "newer\n\tsoftware, or recreate the pool from "
2993 "backup.\n"));
2994 break;
2995 case ZPOOL_STATUS_UNSUP_FEAT_READ:
2996 printf_color(ANSI_BOLD, gettext("action: "));
2997 printf_color(ANSI_YELLOW, gettext("The pool cannot be "
2998 "imported. Access the pool on a system that "
2999 "supports\n\tthe required feature(s), or recreate "
3000 "the pool from backup.\n"));
3001 break;
3002 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
3003 printf_color(ANSI_BOLD, gettext("action: "));
3004 printf_color(ANSI_YELLOW, gettext("The pool cannot be "
3005 "imported in read-write mode. Import the pool "
3006 "with\n"
3007 "\t\"-o readonly=on\", access the pool on a system "
3008 "that supports the\n\trequired feature(s), or "
3009 "recreate the pool from backup.\n"));
3010 break;
3011 case ZPOOL_STATUS_MISSING_DEV_R:
3012 case ZPOOL_STATUS_MISSING_DEV_NR:
3013 case ZPOOL_STATUS_BAD_GUID_SUM:
3014 (void) printf(gettext(" action: The pool cannot be "
3015 "imported. Attach the missing\n\tdevices and try "
3016 "again.\n"));
3017 break;
3018 case ZPOOL_STATUS_HOSTID_ACTIVE:
3019 VERIFY0(nvlist_lookup_nvlist(config,
3020 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
3021
3022 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3023 hostname = fnvlist_lookup_string(nvinfo,
3024 ZPOOL_CONFIG_MMP_HOSTNAME);
3025
3026 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3027 hostid = fnvlist_lookup_uint64(nvinfo,
3028 ZPOOL_CONFIG_MMP_HOSTID);
3029
3030 (void) printf(gettext(" action: The pool must be "
3031 "exported from %s (hostid=%"PRIx64")\n\tbefore it "
3032 "can be safely imported.\n"), hostname, hostid);
3033 break;
3034 case ZPOOL_STATUS_HOSTID_REQUIRED:
3035 (void) printf(gettext(" action: Set a unique system "
3036 "hostid with the zgenhostid(8) command.\n"));
3037 break;
3038 default:
3039 (void) printf(gettext(" action: The pool cannot be "
3040 "imported due to damaged devices or data.\n"));
3041 }
3042 }
3043
3044 /* Print the comment attached to the pool. */
3045 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
3046 (void) printf(gettext("comment: %s\n"), comment);
3047
3048 /*
3049 * If the state is "closed" or "can't open", and the aux state
3050 * is "corrupt data":
3051 */
3052 if (((vs->vs_state == VDEV_STATE_CLOSED) ||
3053 (vs->vs_state == VDEV_STATE_CANT_OPEN)) &&
3054 (vs->vs_aux == VDEV_AUX_CORRUPT_DATA)) {
3055 if (pool_state == POOL_STATE_DESTROYED)
3056 (void) printf(gettext("\tThe pool was destroyed, "
3057 "but can be imported using the '-Df' flags.\n"));
3058 else if (pool_state != POOL_STATE_EXPORTED)
3059 (void) printf(gettext("\tThe pool may be active on "
3060 "another system, but can be imported using\n\t"
3061 "the '-f' flag.\n"));
3062 }
3063
3064 if (msgid != NULL) {
3065 (void) printf(gettext(
3066 " see: https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3067 msgid);
3068 }
3069
3070 (void) printf(gettext(" config:\n\n"));
3071
3072 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
3073 VDEV_NAME_TYPE_ID);
3074 if (cb.cb_namewidth < 10)
3075 cb.cb_namewidth = 10;
3076
3077 print_import_config(&cb, name, nvroot, 0);
3078
3079 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
3080 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
3081 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
3082
3083 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
3084 (void) printf(gettext("\n\tAdditional devices are known to "
3085 "be part of this pool, though their\n\texact "
3086 "configuration cannot be determined.\n"));
3087 }
3088 return (0);
3089 }
3090
3091 static boolean_t
3092 zfs_force_import_required(nvlist_t *config)
3093 {
3094 uint64_t state;
3095 uint64_t hostid = 0;
3096 nvlist_t *nvinfo;
3097
3098 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
3099 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
3100
3101 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
3102 return (B_TRUE);
3103
3104 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3105 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
3106 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
3107 ZPOOL_CONFIG_MMP_STATE);
3108
3109 if (mmp_state != MMP_STATE_INACTIVE)
3110 return (B_TRUE);
3111 }
3112
3113 return (B_FALSE);
3114 }
3115
3116 /*
3117 * Perform the import for the given configuration. This passes the heavy
3118 * lifting off to zpool_import_props(), and then mounts the datasets contained
3119 * within the pool.
3120 */
3121 static int
3122 do_import(nvlist_t *config, const char *newname, const char *mntopts,
3123 nvlist_t *props, int flags)
3124 {
3125 int ret = 0;
3126 zpool_handle_t *zhp;
3127 const char *name;
3128 uint64_t version;
3129
3130 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
3131 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
3132
3133 if (!SPA_VERSION_IS_SUPPORTED(version)) {
3134 (void) fprintf(stderr, gettext("cannot import '%s': pool "
3135 "is formatted using an unsupported ZFS version\n"), name);
3136 return (1);
3137 } else if (zfs_force_import_required(config) &&
3138 !(flags & ZFS_IMPORT_ANY_HOST)) {
3139 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
3140 nvlist_t *nvinfo;
3141
3142 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3143 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
3144 mmp_state = fnvlist_lookup_uint64(nvinfo,
3145 ZPOOL_CONFIG_MMP_STATE);
3146
3147 if (mmp_state == MMP_STATE_ACTIVE) {
3148 const char *hostname = "<unknown>";
3149 uint64_t hostid = 0;
3150
3151 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3152 hostname = fnvlist_lookup_string(nvinfo,
3153 ZPOOL_CONFIG_MMP_HOSTNAME);
3154
3155 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3156 hostid = fnvlist_lookup_uint64(nvinfo,
3157 ZPOOL_CONFIG_MMP_HOSTID);
3158
3159 (void) fprintf(stderr, gettext("cannot import '%s': "
3160 "pool is imported on %s (hostid: "
3161 "0x%"PRIx64")\nExport the pool on the other "
3162 "system, then run 'zpool import'.\n"),
3163 name, hostname, hostid);
3164 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
3165 (void) fprintf(stderr, gettext("Cannot import '%s': "
3166 "pool has the multihost property on and the\n"
3167 "system's hostid is not set. Set a unique hostid "
3168 "with the zgenhostid(8) command.\n"), name);
3169 } else {
3170 const char *hostname = "<unknown>";
3171 time_t timestamp = 0;
3172 uint64_t hostid = 0;
3173
3174 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
3175 hostname = fnvlist_lookup_string(config,
3176 ZPOOL_CONFIG_HOSTNAME);
3177
3178 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
3179 timestamp = fnvlist_lookup_uint64(config,
3180 ZPOOL_CONFIG_TIMESTAMP);
3181
3182 if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
3183 hostid = fnvlist_lookup_uint64(config,
3184 ZPOOL_CONFIG_HOSTID);
3185
3186 (void) fprintf(stderr, gettext("cannot import '%s': "
3187 "pool was previously in use from another system.\n"
3188 "Last accessed by %s (hostid=%"PRIx64") at %s"
3189 "The pool can be imported, use 'zpool import -f' "
3190 "to import the pool.\n"), name, hostname,
3191 hostid, ctime(&timestamp));
3192 }
3193
3194 return (1);
3195 }
3196
3197 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
3198 return (1);
3199
3200 if (newname != NULL)
3201 name = newname;
3202
3203 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
3204 return (1);
3205
3206 /*
3207 * Loading keys is best effort. We don't want to return immediately
3208 * if it fails but we do want to give the error to the caller.
3209 */
3210 if (flags & ZFS_IMPORT_LOAD_KEYS &&
3211 zfs_crypto_attempt_load_keys(g_zfs, name) != 0)
3212 ret = 1;
3213
3214 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3215 !(flags & ZFS_IMPORT_ONLY) &&
3216 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
3217 zpool_close(zhp);
3218 return (1);
3219 }
3220
3221 zpool_close(zhp);
3222 return (ret);
3223 }
3224
3225 static int
3226 import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
3227 char *orig_name, char *new_name,
3228 boolean_t do_destroyed, boolean_t pool_specified, boolean_t do_all,
3229 importargs_t *import)
3230 {
3231 nvlist_t *config = NULL;
3232 nvlist_t *found_config = NULL;
3233 uint64_t pool_state;
3234
3235 /*
3236 * At this point we have a list of import candidate configs. Even if
3237 * we were searching by pool name or guid, we still need to
3238 * post-process the list to deal with pool state and possible
3239 * duplicate names.
3240 */
3241 int err = 0;
3242 nvpair_t *elem = NULL;
3243 boolean_t first = B_TRUE;
3244 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3245
3246 verify(nvpair_value_nvlist(elem, &config) == 0);
3247
3248 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3249 &pool_state) == 0);
3250 if (!do_destroyed && pool_state == POOL_STATE_DESTROYED)
3251 continue;
3252 if (do_destroyed && pool_state != POOL_STATE_DESTROYED)
3253 continue;
3254
3255 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
3256 import->policy) == 0);
3257
3258 if (!pool_specified) {
3259 if (first)
3260 first = B_FALSE;
3261 else if (!do_all)
3262 (void) fputc('\n', stdout);
3263
3264 if (do_all) {
3265 err |= do_import(config, NULL, mntopts,
3266 props, flags);
3267 } else {
3268 /*
3269 * If we're importing from cachefile, then
3270 * we don't want to report errors until we
3271 * are in the scan phase of the import. If
3272 * we get an error, then we return that error
3273 * to invoke the scan phase.
3274 */
3275 if (import->cachefile && !import->scan)
3276 err = show_import(config, B_FALSE);
3277 else
3278 (void) show_import(config, B_TRUE);
3279 }
3280 } else if (import->poolname != NULL) {
3281 char *name;
3282
3283 /*
3284 * We are searching for a pool based on name.
3285 */
3286 verify(nvlist_lookup_string(config,
3287 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
3288
3289 if (strcmp(name, import->poolname) == 0) {
3290 if (found_config != NULL) {
3291 (void) fprintf(stderr, gettext(
3292 "cannot import '%s': more than "
3293 "one matching pool\n"),
3294 import->poolname);
3295 (void) fprintf(stderr, gettext(
3296 "import by numeric ID instead\n"));
3297 err = B_TRUE;
3298 }
3299 found_config = config;
3300 }
3301 } else {
3302 uint64_t guid;
3303
3304 /*
3305 * Search for a pool by guid.
3306 */
3307 verify(nvlist_lookup_uint64(config,
3308 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
3309
3310 if (guid == import->guid)
3311 found_config = config;
3312 }
3313 }
3314
3315 /*
3316 * If we were searching for a specific pool, verify that we found a
3317 * pool, and then do the import.
3318 */
3319 if (pool_specified && err == 0) {
3320 if (found_config == NULL) {
3321 (void) fprintf(stderr, gettext("cannot import '%s': "
3322 "no such pool available\n"), orig_name);
3323 err = B_TRUE;
3324 } else {
3325 err |= do_import(found_config, new_name,
3326 mntopts, props, flags);
3327 }
3328 }
3329
3330 /*
3331 * If we were just looking for pools, report an error if none were
3332 * found.
3333 */
3334 if (!pool_specified && first)
3335 (void) fprintf(stderr,
3336 gettext("no pools available to import\n"));
3337 return (err);
3338 }
3339
3340 typedef struct target_exists_args {
3341 const char *poolname;
3342 uint64_t poolguid;
3343 } target_exists_args_t;
3344
3345 static int
3346 name_or_guid_exists(zpool_handle_t *zhp, void *data)
3347 {
3348 target_exists_args_t *args = data;
3349 nvlist_t *config = zpool_get_config(zhp, NULL);
3350 int found = 0;
3351
3352 if (config == NULL)
3353 return (0);
3354
3355 if (args->poolname != NULL) {
3356 char *pool_name;
3357
3358 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3359 &pool_name) == 0);
3360 if (strcmp(pool_name, args->poolname) == 0)
3361 found = 1;
3362 } else {
3363 uint64_t pool_guid;
3364
3365 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3366 &pool_guid) == 0);
3367 if (pool_guid == args->poolguid)
3368 found = 1;
3369 }
3370 zpool_close(zhp);
3371
3372 return (found);
3373 }
3374 /*
3375 * zpool checkpoint <pool>
3376 * checkpoint --discard <pool>
3377 *
3378 * -d Discard the checkpoint from a checkpointed
3379 * --discard pool.
3380 *
3381 * -w Wait for discarding a checkpoint to complete.
3382 * --wait
3383 *
3384 * Checkpoints the specified pool, by taking a "snapshot" of its
3385 * current state. A pool can only have one checkpoint at a time.
3386 */
3387 int
3388 zpool_do_checkpoint(int argc, char **argv)
3389 {
3390 boolean_t discard, wait;
3391 char *pool;
3392 zpool_handle_t *zhp;
3393 int c, err;
3394
3395 struct option long_options[] = {
3396 {"discard", no_argument, NULL, 'd'},
3397 {"wait", no_argument, NULL, 'w'},
3398 {0, 0, 0, 0}
3399 };
3400
3401 discard = B_FALSE;
3402 wait = B_FALSE;
3403 while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) {
3404 switch (c) {
3405 case 'd':
3406 discard = B_TRUE;
3407 break;
3408 case 'w':
3409 wait = B_TRUE;
3410 break;
3411 case '?':
3412 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 optopt);
3414 usage(B_FALSE);
3415 }
3416 }
3417
3418 if (wait && !discard) {
3419 (void) fprintf(stderr, gettext("--wait only valid when "
3420 "--discard also specified\n"));
3421 usage(B_FALSE);
3422 }
3423
3424 argc -= optind;
3425 argv += optind;
3426
3427 if (argc < 1) {
3428 (void) fprintf(stderr, gettext("missing pool argument\n"));
3429 usage(B_FALSE);
3430 }
3431
3432 if (argc > 1) {
3433 (void) fprintf(stderr, gettext("too many arguments\n"));
3434 usage(B_FALSE);
3435 }
3436
3437 pool = argv[0];
3438
3439 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
3440 /* As a special case, check for use of '/' in the name */
3441 if (strchr(pool, '/') != NULL)
3442 (void) fprintf(stderr, gettext("'zpool checkpoint' "
3443 "doesn't work on datasets. To save the state "
3444 "of a dataset from a specific point in time "
3445 "please use 'zfs snapshot'\n"));
3446 return (1);
3447 }
3448
3449 if (discard) {
3450 err = (zpool_discard_checkpoint(zhp) != 0);
3451 if (err == 0 && wait)
3452 err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD);
3453 } else {
3454 err = (zpool_checkpoint(zhp) != 0);
3455 }
3456
3457 zpool_close(zhp);
3458
3459 return (err);
3460 }
3461
3462 #define CHECKPOINT_OPT 1024
3463
3464 /*
3465 * zpool import [-d dir] [-D]
3466 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3467 * [-d dir | -c cachefile | -s] [-f] -a
3468 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
3469 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
3470 * [newpool]
3471 *
3472 * -c Read pool information from a cachefile instead of searching
3473 * devices. If importing from a cachefile config fails, then
3474 * fallback to searching for devices only in the directories that
3475 * exist in the cachefile.
3476 *
3477 * -d Scan in a specific directory, other than /dev/. More than
3478 * one directory can be specified using multiple '-d' options.
3479 *
3480 * -D Scan for previously destroyed pools or import all or only
3481 * specified destroyed pools.
3482 *
3483 * -R Temporarily import the pool, with all mountpoints relative to
3484 * the given root. The pool will remain exported when the machine
3485 * is rebooted.
3486 *
3487 * -V Import even in the presence of faulted vdevs. This is an
3488 * intentionally undocumented option for testing purposes, and
3489 * treats the pool configuration as complete, leaving any bad
3490 * vdevs in the FAULTED state. In other words, it does verbatim
3491 * import.
3492 *
3493 * -f Force import, even if it appears that the pool is active.
3494 *
3495 * -F Attempt rewind if necessary.
3496 *
3497 * -n See if rewind would work, but don't actually rewind.
3498 *
3499 * -N Import the pool but don't mount datasets.
3500 *
3501 * -T Specify a starting txg to use for import. This option is
3502 * intentionally undocumented option for testing purposes.
3503 *
3504 * -a Import all pools found.
3505 *
3506 * -l Load encryption keys while importing.
3507 *
3508 * -o Set property=value and/or temporary mount options (without '=').
3509 *
3510 * -s Scan using the default search path, the libblkid cache will
3511 * not be consulted.
3512 *
3513 * --rewind-to-checkpoint
3514 * Import the pool and revert back to the checkpoint.
3515 *
3516 * The import command scans for pools to import, and import pools based on pool
3517 * name and GUID. The pool can also be renamed as part of the import process.
3518 */
3519 int
3520 zpool_do_import(int argc, char **argv)
3521 {
3522 char **searchdirs = NULL;
3523 char *env, *envdup = NULL;
3524 int nsearch = 0;
3525 int c;
3526 int err = 0;
3527 nvlist_t *pools = NULL;
3528 boolean_t do_all = B_FALSE;
3529 boolean_t do_destroyed = B_FALSE;
3530 char *mntopts = NULL;
3531 uint64_t searchguid = 0;
3532 char *searchname = NULL;
3533 char *propval;
3534 nvlist_t *policy = NULL;
3535 nvlist_t *props = NULL;
3536 int flags = ZFS_IMPORT_NORMAL;
3537 uint32_t rewind_policy = ZPOOL_NO_REWIND;
3538 boolean_t dryrun = B_FALSE;
3539 boolean_t do_rewind = B_FALSE;
3540 boolean_t xtreme_rewind = B_FALSE;
3541 boolean_t do_scan = B_FALSE;
3542 boolean_t pool_exists = B_FALSE;
3543 boolean_t pool_specified = B_FALSE;
3544 uint64_t txg = -1ULL;
3545 char *cachefile = NULL;
3546 importargs_t idata = { 0 };
3547 char *endptr;
3548
3549 struct option long_options[] = {
3550 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
3551 {0, 0, 0, 0}
3552 };
3553
3554 /* check options */
3555 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
3556 long_options, NULL)) != -1) {
3557 switch (c) {
3558 case 'a':
3559 do_all = B_TRUE;
3560 break;
3561 case 'c':
3562 cachefile = optarg;
3563 break;
3564 case 'd':
3565 searchdirs = safe_realloc(searchdirs,
3566 (nsearch + 1) * sizeof (char *));
3567 searchdirs[nsearch++] = optarg;
3568 break;
3569 case 'D':
3570 do_destroyed = B_TRUE;
3571 break;
3572 case 'f':
3573 flags |= ZFS_IMPORT_ANY_HOST;
3574 break;
3575 case 'F':
3576 do_rewind = B_TRUE;
3577 break;
3578 case 'l':
3579 flags |= ZFS_IMPORT_LOAD_KEYS;
3580 break;
3581 case 'm':
3582 flags |= ZFS_IMPORT_MISSING_LOG;
3583 break;
3584 case 'n':
3585 dryrun = B_TRUE;
3586 break;
3587 case 'N':
3588 flags |= ZFS_IMPORT_ONLY;
3589 break;
3590 case 'o':
3591 if ((propval = strchr(optarg, '=')) != NULL) {
3592 *propval = '\0';
3593 propval++;
3594 if (add_prop_list(optarg, propval,
3595 &props, B_TRUE))
3596 goto error;
3597 } else {
3598 mntopts = optarg;
3599 }
3600 break;
3601 case 'R':
3602 if (add_prop_list(zpool_prop_to_name(
3603 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
3604 goto error;
3605 if (add_prop_list_default(zpool_prop_to_name(
3606 ZPOOL_PROP_CACHEFILE), "none", &props))
3607 goto error;
3608 break;
3609 case 's':
3610 do_scan = B_TRUE;
3611 break;
3612 case 't':
3613 flags |= ZFS_IMPORT_TEMP_NAME;
3614 if (add_prop_list_default(zpool_prop_to_name(
3615 ZPOOL_PROP_CACHEFILE), "none", &props))
3616 goto error;
3617 break;
3618
3619 case 'T':
3620 errno = 0;
3621 txg = strtoull(optarg, &endptr, 0);
3622 if (errno != 0 || *endptr != '\0') {
3623 (void) fprintf(stderr,
3624 gettext("invalid txg value\n"));
3625 usage(B_FALSE);
3626 }
3627 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
3628 break;
3629 case 'V':
3630 flags |= ZFS_IMPORT_VERBATIM;
3631 break;
3632 case 'X':
3633 xtreme_rewind = B_TRUE;
3634 break;
3635 case CHECKPOINT_OPT:
3636 flags |= ZFS_IMPORT_CHECKPOINT;
3637 break;
3638 case ':':
3639 (void) fprintf(stderr, gettext("missing argument for "
3640 "'%c' option\n"), optopt);
3641 usage(B_FALSE);
3642 break;
3643 case '?':
3644 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3645 optopt);
3646 usage(B_FALSE);
3647 }
3648 }
3649
3650 argc -= optind;
3651 argv += optind;
3652
3653 if (cachefile && nsearch != 0) {
3654 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
3655 usage(B_FALSE);
3656 }
3657
3658 if (cachefile && do_scan) {
3659 (void) fprintf(stderr, gettext("-c is incompatible with -s\n"));
3660 usage(B_FALSE);
3661 }
3662
3663 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
3664 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
3665 usage(B_FALSE);
3666 }
3667
3668 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
3669 (void) fprintf(stderr, gettext("-l is only meaningful during "
3670 "an import\n"));
3671 usage(B_FALSE);
3672 }
3673
3674 if ((dryrun || xtreme_rewind) && !do_rewind) {
3675 (void) fprintf(stderr,
3676 gettext("-n or -X only meaningful with -F\n"));
3677 usage(B_FALSE);
3678 }
3679 if (dryrun)
3680 rewind_policy = ZPOOL_TRY_REWIND;
3681 else if (do_rewind)
3682 rewind_policy = ZPOOL_DO_REWIND;
3683 if (xtreme_rewind)
3684 rewind_policy |= ZPOOL_EXTREME_REWIND;
3685
3686 /* In the future, we can capture further policy and include it here */
3687 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
3688 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
3689 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
3690 rewind_policy) != 0)
3691 goto error;
3692
3693 /* check argument count */
3694 if (do_all) {
3695 if (argc != 0) {
3696 (void) fprintf(stderr, gettext("too many arguments\n"));
3697 usage(B_FALSE);
3698 }
3699 } else {
3700 if (argc > 2) {
3701 (void) fprintf(stderr, gettext("too many arguments\n"));
3702 usage(B_FALSE);
3703 }
3704 }
3705
3706 /*
3707 * Check for the effective uid. We do this explicitly here because
3708 * otherwise any attempt to discover pools will silently fail.
3709 */
3710 if (argc == 0 && geteuid() != 0) {
3711 (void) fprintf(stderr, gettext("cannot "
3712 "discover pools: permission denied\n"));
3713
3714 free(searchdirs);
3715 nvlist_free(props);
3716 nvlist_free(policy);
3717 return (1);
3718 }
3719
3720 /*
3721 * Depending on the arguments given, we do one of the following:
3722 *
3723 * <none> Iterate through all pools and display information about
3724 * each one.
3725 *
3726 * -a Iterate through all pools and try to import each one.
3727 *
3728 * <id> Find the pool that corresponds to the given GUID/pool
3729 * name and import that one.
3730 *
3731 * -D Above options applies only to destroyed pools.
3732 */
3733 if (argc != 0) {
3734 char *endptr;
3735
3736 errno = 0;
3737 searchguid = strtoull(argv[0], &endptr, 10);
3738 if (errno != 0 || *endptr != '\0') {
3739 searchname = argv[0];
3740 searchguid = 0;
3741 }
3742 pool_specified = B_TRUE;
3743
3744 /*
3745 * User specified a name or guid. Ensure it's unique.
3746 */
3747 target_exists_args_t search = {searchname, searchguid};
3748 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search);
3749 }
3750
3751 /*
3752 * Check the environment for the preferred search path.
3753 */
3754 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
3755 char *dir, *tmp = NULL;
3756
3757 envdup = strdup(env);
3758
3759 for (dir = strtok_r(envdup, ":", &tmp);
3760 dir != NULL;
3761 dir = strtok_r(NULL, ":", &tmp)) {
3762 searchdirs = safe_realloc(searchdirs,
3763 (nsearch + 1) * sizeof (char *));
3764 searchdirs[nsearch++] = dir;
3765 }
3766 }
3767
3768 idata.path = searchdirs;
3769 idata.paths = nsearch;
3770 idata.poolname = searchname;
3771 idata.guid = searchguid;
3772 idata.cachefile = cachefile;
3773 idata.scan = do_scan;
3774 idata.policy = policy;
3775
3776 libpc_handle_t lpch = {
3777 .lpc_lib_handle = g_zfs,
3778 .lpc_ops = &libzfs_config_ops,
3779 .lpc_printerr = B_TRUE
3780 };
3781 pools = zpool_search_import(&lpch, &idata);
3782
3783 if (pools != NULL && pool_exists &&
3784 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
3785 (void) fprintf(stderr, gettext("cannot import '%s': "
3786 "a pool with that name already exists\n"),
3787 argv[0]);
3788 (void) fprintf(stderr, gettext("use the form '%s "
3789 "<pool | id> <newpool>' to give it a new name\n"),
3790 "zpool import");
3791 err = 1;
3792 } else if (pools == NULL && pool_exists) {
3793 (void) fprintf(stderr, gettext("cannot import '%s': "
3794 "a pool with that name is already created/imported,\n"),
3795 argv[0]);
3796 (void) fprintf(stderr, gettext("and no additional pools "
3797 "with that name were found\n"));
3798 err = 1;
3799 } else if (pools == NULL) {
3800 if (argc != 0) {
3801 (void) fprintf(stderr, gettext("cannot import '%s': "
3802 "no such pool available\n"), argv[0]);
3803 }
3804 err = 1;
3805 }
3806
3807 if (err == 1) {
3808 free(searchdirs);
3809 free(envdup);
3810 nvlist_free(policy);
3811 nvlist_free(pools);
3812 nvlist_free(props);
3813 return (1);
3814 }
3815
3816 err = import_pools(pools, props, mntopts, flags,
3817 argc >= 1 ? argv[0] : NULL,
3818 argc >= 2 ? argv[1] : NULL,
3819 do_destroyed, pool_specified, do_all, &idata);
3820
3821 /*
3822 * If we're using the cachefile and we failed to import, then
3823 * fallback to scanning the directory for pools that match
3824 * those in the cachefile.
3825 */
3826 if (err != 0 && cachefile != NULL) {
3827 (void) printf(gettext("cachefile import failed, retrying\n"));
3828
3829 /*
3830 * We use the scan flag to gather the directories that exist
3831 * in the cachefile. If we need to fallback to searching for
3832 * the pool config, we will only search devices in these
3833 * directories.
3834 */
3835 idata.scan = B_TRUE;
3836 nvlist_free(pools);
3837 pools = zpool_search_import(&lpch, &idata);
3838
3839 err = import_pools(pools, props, mntopts, flags,
3840 argc >= 1 ? argv[0] : NULL,
3841 argc >= 2 ? argv[1] : NULL,
3842 do_destroyed, pool_specified, do_all, &idata);
3843 }
3844
3845 error:
3846 nvlist_free(props);
3847 nvlist_free(pools);
3848 nvlist_free(policy);
3849 free(searchdirs);
3850 free(envdup);
3851
3852 return (err ? 1 : 0);
3853 }
3854
3855 /*
3856 * zpool sync [-f] [pool] ...
3857 *
3858 * -f (undocumented) force uberblock (and config including zpool cache file)
3859 * update.
3860 *
3861 * Sync the specified pool(s).
3862 * Without arguments "zpool sync" will sync all pools.
3863 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
3864 *
3865 */
3866 static int
3867 zpool_do_sync(int argc, char **argv)
3868 {
3869 int ret;
3870 boolean_t force = B_FALSE;
3871
3872 /* check options */
3873 while ((ret = getopt(argc, argv, "f")) != -1) {
3874 switch (ret) {
3875 case 'f':
3876 force = B_TRUE;
3877 break;
3878 case '?':
3879 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3880 optopt);
3881 usage(B_FALSE);
3882 }
3883 }
3884
3885 argc -= optind;
3886 argv += optind;
3887
3888 /* if argc == 0 we will execute zpool_sync_one on all pools */
3889 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
3890 B_FALSE, zpool_sync_one, &force);
3891
3892 return (ret);
3893 }
3894
3895 typedef struct iostat_cbdata {
3896 uint64_t cb_flags;
3897 int cb_namewidth;
3898 int cb_iteration;
3899 boolean_t cb_verbose;
3900 boolean_t cb_literal;
3901 boolean_t cb_scripted;
3902 zpool_list_t *cb_list;
3903 vdev_cmd_data_list_t *vcdl;
3904 vdev_cbdata_t cb_vdevs;
3905 } iostat_cbdata_t;
3906
3907 /* iostat labels */
3908 typedef struct name_and_columns {
3909 const char *name; /* Column name */
3910 unsigned int columns; /* Center name to this number of columns */
3911 } name_and_columns_t;
3912
3913 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
3914
3915 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
3916 {
3917 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
3918 {NULL}},
3919 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3920 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
3921 {NULL}},
3922 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
3923 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
3924 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}},
3925 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
3926 {"asyncq_wait", 2}, {NULL}},
3927 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
3928 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
3929 {"trim", 2}, {"rebuild", 2}, {NULL}},
3930 };
3931
3932 /* Shorthand - if "columns" field not set, default to 1 column */
3933 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
3934 {
3935 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
3936 {"write"}, {NULL}},
3937 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3938 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
3939 {NULL}},
3940 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
3941 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
3942 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
3943 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
3944 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
3945 {NULL}},
3946 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3947 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
3948 {"ind"}, {"agg"}, {NULL}},
3949 };
3950
3951 static const char *histo_to_title[] = {
3952 [IOS_L_HISTO] = "latency",
3953 [IOS_RQ_HISTO] = "req_size",
3954 };
3955
3956 /*
3957 * Return the number of labels in a null-terminated name_and_columns_t
3958 * array.
3959 *
3960 */
3961 static unsigned int
3962 label_array_len(const name_and_columns_t *labels)
3963 {
3964 int i = 0;
3965
3966 while (labels[i].name)
3967 i++;
3968
3969 return (i);
3970 }
3971
3972 /*
3973 * Return the number of strings in a null-terminated string array.
3974 * For example:
3975 *
3976 * const char foo[] = {"bar", "baz", NULL}
3977 *
3978 * returns 2
3979 */
3980 static uint64_t
3981 str_array_len(const char *array[])
3982 {
3983 uint64_t i = 0;
3984 while (array[i])
3985 i++;
3986
3987 return (i);
3988 }
3989
3990
3991 /*
3992 * Return a default column width for default/latency/queue columns. This does
3993 * not include histograms, which have their columns autosized.
3994 */
3995 static unsigned int
3996 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
3997 {
3998 unsigned long column_width = 5; /* Normal niceprint */
3999 static unsigned long widths[] = {
4000 /*
4001 * Choose some sane default column sizes for printing the
4002 * raw numbers.
4003 */
4004 [IOS_DEFAULT] = 15, /* 1PB capacity */
4005 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
4006 [IOS_QUEUES] = 6, /* 1M queue entries */
4007 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */
4008 [IOS_RQ_HISTO] = 6, /* 1M queue entries */
4009 };
4010
4011 if (cb->cb_literal)
4012 column_width = widths[type];
4013
4014 return (column_width);
4015 }
4016
4017 /*
4018 * Print the column labels, i.e:
4019 *
4020 * capacity operations bandwidth
4021 * alloc free read write read write ...
4022 *
4023 * If force_column_width is set, use it for the column width. If not set, use
4024 * the default column width.
4025 */
4026 static void
4027 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
4028 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
4029 {
4030 int i, idx, s;
4031 int text_start, rw_column_width, spaces_to_end;
4032 uint64_t flags = cb->cb_flags;
4033 uint64_t f;
4034 unsigned int column_width = force_column_width;
4035
4036 /* For each bit set in flags */
4037 for (f = flags; f; f &= ~(1ULL << idx)) {
4038 idx = lowbit64(f) - 1;
4039 if (!force_column_width)
4040 column_width = default_column_width(cb, idx);
4041 /* Print our top labels centered over "read write" label. */
4042 for (i = 0; i < label_array_len(labels[idx]); i++) {
4043 const char *name = labels[idx][i].name;
4044 /*
4045 * We treat labels[][].columns == 0 as shorthand
4046 * for one column. It makes writing out the label
4047 * tables more concise.
4048 */
4049 unsigned int columns = MAX(1, labels[idx][i].columns);
4050 unsigned int slen = strlen(name);
4051
4052 rw_column_width = (column_width * columns) +
4053 (2 * (columns - 1));
4054
4055 text_start = (int)((rw_column_width) / columns -
4056 slen / columns);
4057 if (text_start < 0)
4058 text_start = 0;
4059
4060 printf(" "); /* Two spaces between columns */
4061
4062 /* Space from beginning of column to label */
4063 for (s = 0; s < text_start; s++)
4064 printf(" ");
4065
4066 printf("%s", name);
4067
4068 /* Print space after label to end of column */
4069 spaces_to_end = rw_column_width - text_start - slen;
4070 if (spaces_to_end < 0)
4071 spaces_to_end = 0;
4072
4073 for (s = 0; s < spaces_to_end; s++)
4074 printf(" ");
4075 }
4076 }
4077 }
4078
4079
4080 /*
4081 * print_cmd_columns - Print custom column titles from -c
4082 *
4083 * If the user specified the "zpool status|iostat -c" then print their custom
4084 * column titles in the header. For example, print_cmd_columns() would print
4085 * the " col1 col2" part of this:
4086 *
4087 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4088 * ...
4089 * capacity operations bandwidth
4090 * pool alloc free read write read write col1 col2
4091 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4092 * mypool 269K 1008M 0 0 107 946
4093 * mirror 269K 1008M 0 0 107 946
4094 * sdb - - 0 0 102 473 val1 val2
4095 * sdc - - 0 0 5 473 val1 val2
4096 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4097 */
4098 static void
4099 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
4100 {
4101 int i, j;
4102 vdev_cmd_data_t *data = &vcdl->data[0];
4103
4104 if (vcdl->count == 0 || data == NULL)
4105 return;
4106
4107 /*
4108 * Each vdev cmd should have the same column names unless the user did
4109 * something weird with their cmd. Just take the column names from the
4110 * first vdev and assume it works for all of them.
4111 */
4112 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
4113 printf(" ");
4114 if (use_dashes) {
4115 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
4116 printf("-");
4117 } else {
4118 printf_color(ANSI_BOLD, "%*s", vcdl->uniq_cols_width[i],
4119 vcdl->uniq_cols[i]);
4120 }
4121 }
4122 }
4123
4124
4125 /*
4126 * Utility function to print out a line of dashes like:
4127 *
4128 * -------------------------------- ----- ----- ----- ----- -----
4129 *
4130 * ...or a dashed named-row line like:
4131 *
4132 * logs - - - - -
4133 *
4134 * @cb: iostat data
4135 *
4136 * @force_column_width If non-zero, use the value as the column width.
4137 * Otherwise use the default column widths.
4138 *
4139 * @name: Print a dashed named-row line starting
4140 * with @name. Otherwise, print a regular
4141 * dashed line.
4142 */
4143 static void
4144 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
4145 const char *name)
4146 {
4147 int i;
4148 unsigned int namewidth;
4149 uint64_t flags = cb->cb_flags;
4150 uint64_t f;
4151 int idx;
4152 const name_and_columns_t *labels;
4153 const char *title;
4154
4155
4156 if (cb->cb_flags & IOS_ANYHISTO_M) {
4157 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
4158 } else if (cb->cb_vdevs.cb_names_count) {
4159 title = "vdev";
4160 } else {
4161 title = "pool";
4162 }
4163
4164 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4165 name ? strlen(name) : 0);
4166
4167
4168 if (name) {
4169 printf("%-*s", namewidth, name);
4170 } else {
4171 for (i = 0; i < namewidth; i++)
4172 (void) printf("-");
4173 }
4174
4175 /* For each bit in flags */
4176 for (f = flags; f; f &= ~(1ULL << idx)) {
4177 unsigned int column_width;
4178 idx = lowbit64(f) - 1;
4179 if (force_column_width)
4180 column_width = force_column_width;
4181 else
4182 column_width = default_column_width(cb, idx);
4183
4184 labels = iostat_bottom_labels[idx];
4185 for (i = 0; i < label_array_len(labels); i++) {
4186 if (name)
4187 printf(" %*s-", column_width - 1, " ");
4188 else
4189 printf(" %.*s", column_width,
4190 "--------------------");
4191 }
4192 }
4193 }
4194
4195
4196 static void
4197 print_iostat_separator_impl(iostat_cbdata_t *cb,
4198 unsigned int force_column_width)
4199 {
4200 print_iostat_dashes(cb, force_column_width, NULL);
4201 }
4202
4203 static void
4204 print_iostat_separator(iostat_cbdata_t *cb)
4205 {
4206 print_iostat_separator_impl(cb, 0);
4207 }
4208
4209 static void
4210 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
4211 const char *histo_vdev_name)
4212 {
4213 unsigned int namewidth;
4214 const char *title;
4215
4216 if (cb->cb_flags & IOS_ANYHISTO_M) {
4217 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
4218 } else if (cb->cb_vdevs.cb_names_count) {
4219 title = "vdev";
4220 } else {
4221 title = "pool";
4222 }
4223
4224 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4225 histo_vdev_name ? strlen(histo_vdev_name) : 0);
4226
4227 if (histo_vdev_name)
4228 printf("%-*s", namewidth, histo_vdev_name);
4229 else
4230 printf("%*s", namewidth, "");
4231
4232
4233 print_iostat_labels(cb, force_column_width, iostat_top_labels);
4234 printf("\n");
4235
4236 printf("%-*s", namewidth, title);
4237
4238 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
4239 if (cb->vcdl != NULL)
4240 print_cmd_columns(cb->vcdl, 0);
4241
4242 printf("\n");
4243
4244 print_iostat_separator_impl(cb, force_column_width);
4245
4246 if (cb->vcdl != NULL)
4247 print_cmd_columns(cb->vcdl, 1);
4248
4249 printf("\n");
4250 }
4251
4252 static void
4253 print_iostat_header(iostat_cbdata_t *cb)
4254 {
4255 print_iostat_header_impl(cb, 0, NULL);
4256 }
4257
4258
4259 /*
4260 * Display a single statistic.
4261 */
4262 static void
4263 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
4264 unsigned int column_size, boolean_t scripted)
4265 {
4266 char buf[64];
4267
4268 zfs_nicenum_format(value, buf, sizeof (buf), format);
4269
4270 if (scripted)
4271 printf("\t%s", buf);
4272 else
4273 printf(" %*s", column_size, buf);
4274 }
4275
4276 /*
4277 * Calculate the default vdev stats
4278 *
4279 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
4280 * stats into calcvs.
4281 */
4282 static void
4283 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
4284 vdev_stat_t *calcvs)
4285 {
4286 int i;
4287
4288 memcpy(calcvs, newvs, sizeof (*calcvs));
4289 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
4290 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
4291
4292 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
4293 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
4294 }
4295
4296 /*
4297 * Internal representation of the extended iostats data.
4298 *
4299 * The extended iostat stats are exported in nvlists as either uint64_t arrays
4300 * or single uint64_t's. We make both look like arrays to make them easier
4301 * to process. In order to make single uint64_t's look like arrays, we set
4302 * __data to the stat data, and then set *data = &__data with count = 1. Then,
4303 * we can just use *data and count.
4304 */
4305 struct stat_array {
4306 uint64_t *data;
4307 uint_t count; /* Number of entries in data[] */
4308 uint64_t __data; /* Only used when data is a single uint64_t */
4309 };
4310
4311 static uint64_t
4312 stat_histo_max(struct stat_array *nva, unsigned int len)
4313 {
4314 uint64_t max = 0;
4315 int i;
4316 for (i = 0; i < len; i++)
4317 max = MAX(max, array64_max(nva[i].data, nva[i].count));
4318
4319 return (max);
4320 }
4321
4322 /*
4323 * Helper function to lookup a uint64_t array or uint64_t value and store its
4324 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
4325 * it look like a one element array to make it easier to process.
4326 */
4327 static int
4328 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
4329 struct stat_array *nva)
4330 {
4331 nvpair_t *tmp;
4332 int ret;
4333
4334 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
4335 switch (nvpair_type(tmp)) {
4336 case DATA_TYPE_UINT64_ARRAY:
4337 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
4338 break;
4339 case DATA_TYPE_UINT64:
4340 ret = nvpair_value_uint64(tmp, &nva->__data);
4341 nva->data = &nva->__data;
4342 nva->count = 1;
4343 break;
4344 default:
4345 /* Not a uint64_t */
4346 ret = EINVAL;
4347 break;
4348 }
4349
4350 return (ret);
4351 }
4352
4353 /*
4354 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
4355 * subtract them, and return the results in a newly allocated stat_array.
4356 * You must free the returned array after you are done with it with
4357 * free_calc_stats().
4358 *
4359 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
4360 * values.
4361 */
4362 static struct stat_array *
4363 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
4364 nvlist_t *newnv)
4365 {
4366 nvlist_t *oldnvx = NULL, *newnvx;
4367 struct stat_array *oldnva, *newnva, *calcnva;
4368 int i, j;
4369 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
4370
4371 /* Extract our extended stats nvlist from the main list */
4372 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
4373 &newnvx) == 0);
4374 if (oldnv) {
4375 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
4376 &oldnvx) == 0);
4377 }
4378
4379 newnva = safe_malloc(alloc_size);
4380 oldnva = safe_malloc(alloc_size);
4381 calcnva = safe_malloc(alloc_size);
4382
4383 for (j = 0; j < len; j++) {
4384 verify(nvpair64_to_stat_array(newnvx, names[j],
4385 &newnva[j]) == 0);
4386 calcnva[j].count = newnva[j].count;
4387 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
4388 calcnva[j].data = safe_malloc(alloc_size);
4389 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
4390
4391 if (oldnvx) {
4392 verify(nvpair64_to_stat_array(oldnvx, names[j],
4393 &oldnva[j]) == 0);
4394 for (i = 0; i < oldnva[j].count; i++)
4395 calcnva[j].data[i] -= oldnva[j].data[i];
4396 }
4397 }
4398 free(newnva);
4399 free(oldnva);
4400 return (calcnva);
4401 }
4402
4403 static void
4404 free_calc_stats(struct stat_array *nva, unsigned int len)
4405 {
4406 int i;
4407 for (i = 0; i < len; i++)
4408 free(nva[i].data);
4409
4410 free(nva);
4411 }
4412
4413 static void
4414 print_iostat_histo(struct stat_array *nva, unsigned int len,
4415 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
4416 double scale)
4417 {
4418 int i, j;
4419 char buf[6];
4420 uint64_t val;
4421 enum zfs_nicenum_format format;
4422 unsigned int buckets;
4423 unsigned int start_bucket;
4424
4425 if (cb->cb_literal)
4426 format = ZFS_NICENUM_RAW;
4427 else
4428 format = ZFS_NICENUM_1024;
4429
4430 /* All these histos are the same size, so just use nva[0].count */
4431 buckets = nva[0].count;
4432
4433 if (cb->cb_flags & IOS_RQ_HISTO_M) {
4434 /* Start at 512 - req size should never be lower than this */
4435 start_bucket = 9;
4436 } else {
4437 start_bucket = 0;
4438 }
4439
4440 for (j = start_bucket; j < buckets; j++) {
4441 /* Print histogram bucket label */
4442 if (cb->cb_flags & IOS_L_HISTO_M) {
4443 /* Ending range of this bucket */
4444 val = (1UL << (j + 1)) - 1;
4445 zfs_nicetime(val, buf, sizeof (buf));
4446 } else {
4447 /* Request size (starting range of bucket) */
4448 val = (1UL << j);
4449 zfs_nicenum(val, buf, sizeof (buf));
4450 }
4451
4452 if (cb->cb_scripted)
4453 printf("%llu", (u_longlong_t)val);
4454 else
4455 printf("%-*s", namewidth, buf);
4456
4457 /* Print the values on the line */
4458 for (i = 0; i < len; i++) {
4459 print_one_stat(nva[i].data[j] * scale, format,
4460 column_width, cb->cb_scripted);
4461 }
4462 printf("\n");
4463 }
4464 }
4465
4466 static void
4467 print_solid_separator(unsigned int length)
4468 {
4469 while (length--)
4470 printf("-");
4471 printf("\n");
4472 }
4473
4474 static void
4475 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
4476 nvlist_t *newnv, double scale, const char *name)
4477 {
4478 unsigned int column_width;
4479 unsigned int namewidth;
4480 unsigned int entire_width;
4481 enum iostat_type type;
4482 struct stat_array *nva;
4483 const char **names;
4484 unsigned int names_len;
4485
4486 /* What type of histo are we? */
4487 type = IOS_HISTO_IDX(cb->cb_flags);
4488
4489 /* Get NULL-terminated array of nvlist names for our histo */
4490 names = vsx_type_to_nvlist[type];
4491 names_len = str_array_len(names); /* num of names */
4492
4493 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
4494
4495 if (cb->cb_literal) {
4496 column_width = MAX(5,
4497 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
4498 } else {
4499 column_width = 5;
4500 }
4501
4502 namewidth = MAX(cb->cb_namewidth,
4503 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
4504
4505 /*
4506 * Calculate the entire line width of what we're printing. The
4507 * +2 is for the two spaces between columns:
4508 */
4509 /* read write */
4510 /* ----- ----- */
4511 /* |___| <---------- column_width */
4512 /* */
4513 /* |__________| <--- entire_width */
4514 /* */
4515 entire_width = namewidth + (column_width + 2) *
4516 label_array_len(iostat_bottom_labels[type]);
4517
4518 if (cb->cb_scripted)
4519 printf("%s\n", name);
4520 else
4521 print_iostat_header_impl(cb, column_width, name);
4522
4523 print_iostat_histo(nva, names_len, cb, column_width,
4524 namewidth, scale);
4525
4526 free_calc_stats(nva, names_len);
4527 if (!cb->cb_scripted)
4528 print_solid_separator(entire_width);
4529 }
4530
4531 /*
4532 * Calculate the average latency of a power-of-two latency histogram
4533 */
4534 static uint64_t
4535 single_histo_average(uint64_t *histo, unsigned int buckets)
4536 {
4537 int i;
4538 uint64_t count = 0, total = 0;
4539
4540 for (i = 0; i < buckets; i++) {
4541 /*
4542 * Our buckets are power-of-two latency ranges. Use the
4543 * midpoint latency of each bucket to calculate the average.
4544 * For example:
4545 *
4546 * Bucket Midpoint
4547 * 8ns-15ns: 12ns
4548 * 16ns-31ns: 24ns
4549 * ...
4550 */
4551 if (histo[i] != 0) {
4552 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
4553 count += histo[i];
4554 }
4555 }
4556
4557 /* Prevent divide by zero */
4558 return (count == 0 ? 0 : total / count);
4559 }
4560
4561 static void
4562 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv)
4563 {
4564 const char *names[] = {
4565 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
4566 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
4567 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
4568 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
4569 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
4570 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
4571 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
4572 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
4573 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
4574 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
4575 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
4576 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
4577 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
4578 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
4579 };
4580
4581 struct stat_array *nva;
4582
4583 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
4584 enum zfs_nicenum_format format;
4585
4586 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
4587
4588 if (cb->cb_literal)
4589 format = ZFS_NICENUM_RAW;
4590 else
4591 format = ZFS_NICENUM_1024;
4592
4593 for (int i = 0; i < ARRAY_SIZE(names); i++) {
4594 uint64_t val = nva[i].data[0];
4595 print_one_stat(val, format, column_width, cb->cb_scripted);
4596 }
4597
4598 free_calc_stats(nva, ARRAY_SIZE(names));
4599 }
4600
4601 static void
4602 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
4603 nvlist_t *newnv)
4604 {
4605 int i;
4606 uint64_t val;
4607 const char *names[] = {
4608 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
4609 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
4610 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
4611 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
4612 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
4613 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
4614 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
4615 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
4616 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
4617 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
4618 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
4619 };
4620 struct stat_array *nva;
4621
4622 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
4623 enum zfs_nicenum_format format;
4624
4625 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
4626
4627 if (cb->cb_literal)
4628 format = ZFS_NICENUM_RAWTIME;
4629 else
4630 format = ZFS_NICENUM_TIME;
4631
4632 /* Print our avg latencies on the line */
4633 for (i = 0; i < ARRAY_SIZE(names); i++) {
4634 /* Compute average latency for a latency histo */
4635 val = single_histo_average(nva[i].data, nva[i].count);
4636 print_one_stat(val, format, column_width, cb->cb_scripted);
4637 }
4638 free_calc_stats(nva, ARRAY_SIZE(names));
4639 }
4640
4641 /*
4642 * Print default statistics (capacity/operations/bandwidth)
4643 */
4644 static void
4645 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
4646 {
4647 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
4648 enum zfs_nicenum_format format;
4649 char na; /* char to print for "not applicable" values */
4650
4651 if (cb->cb_literal) {
4652 format = ZFS_NICENUM_RAW;
4653 na = '0';
4654 } else {
4655 format = ZFS_NICENUM_1024;
4656 na = '-';
4657 }
4658
4659 /* only toplevel vdevs have capacity stats */
4660 if (vs->vs_space == 0) {
4661 if (cb->cb_scripted)
4662 printf("\t%c\t%c", na, na);
4663 else
4664 printf(" %*c %*c", column_width, na, column_width,
4665 na);
4666 } else {
4667 print_one_stat(vs->vs_alloc, format, column_width,
4668 cb->cb_scripted);
4669 print_one_stat(vs->vs_space - vs->vs_alloc, format,
4670 column_width, cb->cb_scripted);
4671 }
4672
4673 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
4674 format, column_width, cb->cb_scripted);
4675 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
4676 format, column_width, cb->cb_scripted);
4677 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
4678 format, column_width, cb->cb_scripted);
4679 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
4680 format, column_width, cb->cb_scripted);
4681 }
4682
4683 static const char *const class_name[] = {
4684 VDEV_ALLOC_BIAS_DEDUP,
4685 VDEV_ALLOC_BIAS_SPECIAL,
4686 VDEV_ALLOC_CLASS_LOGS
4687 };
4688
4689 /*
4690 * Print out all the statistics for the given vdev. This can either be the
4691 * toplevel configuration, or called recursively. If 'name' is NULL, then this
4692 * is a verbose output, and we don't want to display the toplevel pool stats.
4693 *
4694 * Returns the number of stat lines printed.
4695 */
4696 static unsigned int
4697 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
4698 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
4699 {
4700 nvlist_t **oldchild, **newchild;
4701 uint_t c, children, oldchildren;
4702 vdev_stat_t *oldvs, *newvs, *calcvs;
4703 vdev_stat_t zerovs = { 0 };
4704 char *vname;
4705 int i;
4706 int ret = 0;
4707 uint64_t tdelta;
4708 double scale;
4709
4710 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
4711 return (ret);
4712
4713 calcvs = safe_malloc(sizeof (*calcvs));
4714
4715 if (oldnv != NULL) {
4716 verify(nvlist_lookup_uint64_array(oldnv,
4717 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
4718 } else {
4719 oldvs = &zerovs;
4720 }
4721
4722 /* Do we only want to see a specific vdev? */
4723 for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) {
4724 /* Yes we do. Is this the vdev? */
4725 if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) {
4726 /*
4727 * This is our vdev. Since it is the only vdev we
4728 * will be displaying, make depth = 0 so that it
4729 * doesn't get indented.
4730 */
4731 depth = 0;
4732 break;
4733 }
4734 }
4735
4736 if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) {
4737 /* Couldn't match the name */
4738 goto children;
4739 }
4740
4741
4742 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
4743 (uint64_t **)&newvs, &c) == 0);
4744
4745 /*
4746 * Print the vdev name unless it's is a histogram. Histograms
4747 * display the vdev name in the header itself.
4748 */
4749 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
4750 if (cb->cb_scripted) {
4751 printf("%s", name);
4752 } else {
4753 if (strlen(name) + depth > cb->cb_namewidth)
4754 (void) printf("%*s%s", depth, "", name);
4755 else
4756 (void) printf("%*s%s%*s", depth, "", name,
4757 (int)(cb->cb_namewidth - strlen(name) -
4758 depth), "");
4759 }
4760 }
4761
4762 /* Calculate our scaling factor */
4763 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
4764 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
4765 /*
4766 * If we specify printing histograms with no time interval, then
4767 * print the histogram numbers over the entire lifetime of the
4768 * vdev.
4769 */
4770 scale = 1;
4771 } else {
4772 if (tdelta == 0)
4773 scale = 1.0;
4774 else
4775 scale = (double)NANOSEC / tdelta;
4776 }
4777
4778 if (cb->cb_flags & IOS_DEFAULT_M) {
4779 calc_default_iostats(oldvs, newvs, calcvs);
4780 print_iostat_default(calcvs, cb, scale);
4781 }
4782 if (cb->cb_flags & IOS_LATENCY_M)
4783 print_iostat_latency(cb, oldnv, newnv);
4784 if (cb->cb_flags & IOS_QUEUES_M)
4785 print_iostat_queues(cb, newnv);
4786 if (cb->cb_flags & IOS_ANYHISTO_M) {
4787 printf("\n");
4788 print_iostat_histos(cb, oldnv, newnv, scale, name);
4789 }
4790
4791 if (cb->vcdl != NULL) {
4792 char *path;
4793 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
4794 &path) == 0) {
4795 printf(" ");
4796 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
4797 }
4798 }
4799
4800 if (!(cb->cb_flags & IOS_ANYHISTO_M))
4801 printf("\n");
4802
4803 ret++;
4804
4805 children:
4806
4807 free(calcvs);
4808
4809 if (!cb->cb_verbose)
4810 return (ret);
4811
4812 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
4813 &newchild, &children) != 0)
4814 return (ret);
4815
4816 if (oldnv) {
4817 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
4818 &oldchild, &oldchildren) != 0)
4819 return (ret);
4820
4821 children = MIN(oldchildren, children);
4822 }
4823
4824 /*
4825 * print normal top-level devices
4826 */
4827 for (c = 0; c < children; c++) {
4828 uint64_t ishole = B_FALSE, islog = B_FALSE;
4829
4830 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
4831 &ishole);
4832
4833 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
4834 &islog);
4835
4836 if (ishole || islog)
4837 continue;
4838
4839 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
4840 continue;
4841
4842 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4843 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
4844 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
4845 newchild[c], cb, depth + 2);
4846 free(vname);
4847 }
4848
4849 /*
4850 * print all other top-level devices
4851 */
4852 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
4853 boolean_t printed = B_FALSE;
4854
4855 for (c = 0; c < children; c++) {
4856 uint64_t islog = B_FALSE;
4857 char *bias = NULL;
4858 char *type = NULL;
4859
4860 (void) nvlist_lookup_uint64(newchild[c],
4861 ZPOOL_CONFIG_IS_LOG, &islog);
4862 if (islog) {
4863 bias = (char *)VDEV_ALLOC_CLASS_LOGS;
4864 } else {
4865 (void) nvlist_lookup_string(newchild[c],
4866 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
4867 (void) nvlist_lookup_string(newchild[c],
4868 ZPOOL_CONFIG_TYPE, &type);
4869 }
4870 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
4871 continue;
4872 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
4873 continue;
4874
4875 if (!printed) {
4876 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
4877 !cb->cb_scripted &&
4878 !cb->cb_vdevs.cb_names) {
4879 print_iostat_dashes(cb, 0,
4880 class_name[n]);
4881 }
4882 printf("\n");
4883 printed = B_TRUE;
4884 }
4885
4886 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4887 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
4888 ret += print_vdev_stats(zhp, vname, oldnv ?
4889 oldchild[c] : NULL, newchild[c], cb, depth + 2);
4890 free(vname);
4891 }
4892 }
4893
4894 /*
4895 * Include level 2 ARC devices in iostat output
4896 */
4897 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
4898 &newchild, &children) != 0)
4899 return (ret);
4900
4901 if (oldnv) {
4902 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
4903 &oldchild, &oldchildren) != 0)
4904 return (ret);
4905
4906 children = MIN(oldchildren, children);
4907 }
4908
4909 if (children > 0) {
4910 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
4911 !cb->cb_vdevs.cb_names) {
4912 print_iostat_dashes(cb, 0, "cache");
4913 }
4914 printf("\n");
4915
4916 for (c = 0; c < children; c++) {
4917 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
4918 cb->cb_vdevs.cb_name_flags);
4919 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
4920 : NULL, newchild[c], cb, depth + 2);
4921 free(vname);
4922 }
4923 }
4924
4925 return (ret);
4926 }
4927
4928 static int
4929 refresh_iostat(zpool_handle_t *zhp, void *data)
4930 {
4931 iostat_cbdata_t *cb = data;
4932 boolean_t missing;
4933
4934 /*
4935 * If the pool has disappeared, remove it from the list and continue.
4936 */
4937 if (zpool_refresh_stats(zhp, &missing) != 0)
4938 return (-1);
4939
4940 if (missing)
4941 pool_list_remove(cb->cb_list, zhp);
4942
4943 return (0);
4944 }
4945
4946 /*
4947 * Callback to print out the iostats for the given pool.
4948 */
4949 static int
4950 print_iostat(zpool_handle_t *zhp, void *data)
4951 {
4952 iostat_cbdata_t *cb = data;
4953 nvlist_t *oldconfig, *newconfig;
4954 nvlist_t *oldnvroot, *newnvroot;
4955 int ret;
4956
4957 newconfig = zpool_get_config(zhp, &oldconfig);
4958
4959 if (cb->cb_iteration == 1)
4960 oldconfig = NULL;
4961
4962 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
4963 &newnvroot) == 0);
4964
4965 if (oldconfig == NULL)
4966 oldnvroot = NULL;
4967 else
4968 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
4969 &oldnvroot) == 0);
4970
4971 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
4972 cb, 0);
4973 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
4974 !cb->cb_scripted && cb->cb_verbose &&
4975 !cb->cb_vdevs.cb_names_count) {
4976 print_iostat_separator(cb);
4977 if (cb->vcdl != NULL) {
4978 print_cmd_columns(cb->vcdl, 1);
4979 }
4980 printf("\n");
4981 }
4982
4983 return (ret);
4984 }
4985
4986 static int
4987 get_columns(void)
4988 {
4989 struct winsize ws;
4990 int columns = 80;
4991 int error;
4992
4993 if (isatty(STDOUT_FILENO)) {
4994 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
4995 if (error == 0)
4996 columns = ws.ws_col;
4997 } else {
4998 columns = 999;
4999 }
5000
5001 return (columns);
5002 }
5003
5004 /*
5005 * Return the required length of the pool/vdev name column. The minimum
5006 * allowed width and output formatting flags must be provided.
5007 */
5008 static int
5009 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
5010 {
5011 nvlist_t *config, *nvroot;
5012 int width = min_width;
5013
5014 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
5015 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5016 &nvroot) == 0);
5017 size_t poolname_len = strlen(zpool_get_name(zhp));
5018 if (verbose == B_FALSE) {
5019 width = MAX(poolname_len, min_width);
5020 } else {
5021 width = MAX(poolname_len,
5022 max_width(zhp, nvroot, 0, min_width, flags));
5023 }
5024 }
5025
5026 return (width);
5027 }
5028
5029 /*
5030 * Parse the input string, get the 'interval' and 'count' value if there is one.
5031 */
5032 static void
5033 get_interval_count(int *argcp, char **argv, float *iv,
5034 unsigned long *cnt)
5035 {
5036 float interval = 0;
5037 unsigned long count = 0;
5038 int argc = *argcp;
5039
5040 /*
5041 * Determine if the last argument is an integer or a pool name
5042 */
5043 if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
5044 char *end;
5045
5046 errno = 0;
5047 interval = strtof(argv[argc - 1], &end);
5048
5049 if (*end == '\0' && errno == 0) {
5050 if (interval == 0) {
5051 (void) fprintf(stderr, gettext(
5052 "interval cannot be zero\n"));
5053 usage(B_FALSE);
5054 }
5055 /*
5056 * Ignore the last parameter
5057 */
5058 argc--;
5059 } else {
5060 /*
5061 * If this is not a valid number, just plow on. The
5062 * user will get a more informative error message later
5063 * on.
5064 */
5065 interval = 0;
5066 }
5067 }
5068
5069 /*
5070 * If the last argument is also an integer, then we have both a count
5071 * and an interval.
5072 */
5073 if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
5074 char *end;
5075
5076 errno = 0;
5077 count = interval;
5078 interval = strtof(argv[argc - 1], &end);
5079
5080 if (*end == '\0' && errno == 0) {
5081 if (interval == 0) {
5082 (void) fprintf(stderr, gettext(
5083 "interval cannot be zero\n"));
5084 usage(B_FALSE);
5085 }
5086
5087 /*
5088 * Ignore the last parameter
5089 */
5090 argc--;
5091 } else {
5092 interval = 0;
5093 }
5094 }
5095
5096 *iv = interval;
5097 *cnt = count;
5098 *argcp = argc;
5099 }
5100
5101 static void
5102 get_timestamp_arg(char c)
5103 {
5104 if (c == 'u')
5105 timestamp_fmt = UDATE;
5106 else if (c == 'd')
5107 timestamp_fmt = DDATE;
5108 else
5109 usage(B_FALSE);
5110 }
5111
5112 /*
5113 * Return stat flags that are supported by all pools by both the module and
5114 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5115 * It will get ANDed down until only the flags that are supported on all pools
5116 * remain.
5117 */
5118 static int
5119 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
5120 {
5121 uint64_t *mask = data;
5122 nvlist_t *config, *nvroot, *nvx;
5123 uint64_t flags = 0;
5124 int i, j;
5125
5126 config = zpool_get_config(zhp, NULL);
5127 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5128 &nvroot) == 0);
5129
5130 /* Default stats are always supported, but for completeness.. */
5131 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
5132 flags |= IOS_DEFAULT_M;
5133
5134 /* Get our extended stats nvlist from the main list */
5135 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
5136 &nvx) != 0) {
5137 /*
5138 * No extended stats; they're probably running an older
5139 * module. No big deal, we support that too.
5140 */
5141 goto end;
5142 }
5143
5144 /* For each extended stat, make sure all its nvpairs are supported */
5145 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
5146 if (!vsx_type_to_nvlist[j][0])
5147 continue;
5148
5149 /* Start off by assuming the flag is supported, then check */
5150 flags |= (1ULL << j);
5151 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
5152 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
5153 /* flag isn't supported */
5154 flags = flags & ~(1ULL << j);
5155 break;
5156 }
5157 }
5158 }
5159 end:
5160 *mask = *mask & flags;
5161 return (0);
5162 }
5163
5164 /*
5165 * Return a bitmask of stats that are supported on all pools by both the module
5166 * and zpool iostat.
5167 */
5168 static uint64_t
5169 get_stat_flags(zpool_list_t *list)
5170 {
5171 uint64_t mask = -1;
5172
5173 /*
5174 * get_stat_flags_cb() will lop off bits from "mask" until only the
5175 * flags that are supported on all pools remain.
5176 */
5177 pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
5178 return (mask);
5179 }
5180
5181 /*
5182 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
5183 */
5184 static int
5185 is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data)
5186 {
5187 vdev_cbdata_t *cb = cb_data;
5188 char *name = NULL;
5189 int ret = 1; /* assume match */
5190 zpool_handle_t *zhp = zhp_data;
5191
5192 name = zpool_vdev_name(g_zfs, zhp, nv, cb->cb_name_flags);
5193
5194 if (strcmp(name, cb->cb_names[0])) {
5195 free(name);
5196 name = zpool_vdev_name(g_zfs, zhp, nv, VDEV_NAME_GUID);
5197 ret = (strcmp(name, cb->cb_names[0]) == 0);
5198 }
5199 free(name);
5200
5201 return (ret);
5202 }
5203
5204 /*
5205 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
5206 */
5207 static int
5208 is_vdev(zpool_handle_t *zhp, void *cb_data)
5209 {
5210 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
5211 }
5212
5213 /*
5214 * Check if vdevs are in a pool
5215 *
5216 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
5217 * return 0. If pool_name is NULL, then search all pools.
5218 */
5219 static int
5220 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
5221 vdev_cbdata_t *cb)
5222 {
5223 char **tmp_name;
5224 int ret = 0;
5225 int i;
5226 int pool_count = 0;
5227
5228 if ((argc == 0) || !*argv)
5229 return (0);
5230
5231 if (pool_name)
5232 pool_count = 1;
5233
5234 /* Temporarily hijack cb_names for a second... */
5235 tmp_name = cb->cb_names;
5236
5237 /* Go though our list of prospective vdev names */
5238 for (i = 0; i < argc; i++) {
5239 cb->cb_names = argv + i;
5240
5241 /* Is this name a vdev in our pools? */
5242 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
5243 ZFS_TYPE_POOL, B_FALSE, is_vdev, cb);
5244 if (!ret) {
5245 /* No match */
5246 break;
5247 }
5248 }
5249
5250 cb->cb_names = tmp_name;
5251
5252 return (ret);
5253 }
5254
5255 static int
5256 is_pool_cb(zpool_handle_t *zhp, void *data)
5257 {
5258 char *name = data;
5259 if (strcmp(name, zpool_get_name(zhp)) == 0)
5260 return (1);
5261
5262 return (0);
5263 }
5264
5265 /*
5266 * Do we have a pool named *name? If so, return 1, otherwise 0.
5267 */
5268 static int
5269 is_pool(char *name)
5270 {
5271 return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE,
5272 is_pool_cb, name));
5273 }
5274
5275 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
5276 static int
5277 are_all_pools(int argc, char **argv)
5278 {
5279 if ((argc == 0) || !*argv)
5280 return (0);
5281
5282 while (--argc >= 0)
5283 if (!is_pool(argv[argc]))
5284 return (0);
5285
5286 return (1);
5287 }
5288
5289 /*
5290 * Helper function to print out vdev/pool names we can't resolve. Used for an
5291 * error message.
5292 */
5293 static void
5294 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
5295 vdev_cbdata_t *cb)
5296 {
5297 int i;
5298 char *name;
5299 char *str;
5300 for (i = 0; i < argc; i++) {
5301 name = argv[i];
5302
5303 if (is_pool(name))
5304 str = gettext("pool");
5305 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
5306 str = gettext("vdev in this pool");
5307 else if (are_vdevs_in_pool(1, &name, NULL, cb))
5308 str = gettext("vdev in another pool");
5309 else
5310 str = gettext("unknown");
5311
5312 fprintf(stderr, "\t%s (%s)\n", name, str);
5313 }
5314 }
5315
5316 /*
5317 * Same as get_interval_count(), but with additional checks to not misinterpret
5318 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
5319 * cb.cb_vdevs.cb_name_flags.
5320 */
5321 static void
5322 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
5323 unsigned long *count, iostat_cbdata_t *cb)
5324 {
5325 char **tmpargv = argv;
5326 int argc_for_interval = 0;
5327
5328 /* Is the last arg an interval value? Or a guid? */
5329 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL,
5330 &cb->cb_vdevs)) {
5331 /*
5332 * The last arg is not a guid, so it's probably an
5333 * interval value.
5334 */
5335 argc_for_interval++;
5336
5337 if (*argc >= 2 &&
5338 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL,
5339 &cb->cb_vdevs)) {
5340 /*
5341 * The 2nd to last arg is not a guid, so it's probably
5342 * an interval value.
5343 */
5344 argc_for_interval++;
5345 }
5346 }
5347
5348 /* Point to our list of possible intervals */
5349 tmpargv = &argv[*argc - argc_for_interval];
5350
5351 *argc = *argc - argc_for_interval;
5352 get_interval_count(&argc_for_interval, tmpargv,
5353 interval, count);
5354 }
5355
5356 /*
5357 * Floating point sleep(). Allows you to pass in a floating point value for
5358 * seconds.
5359 */
5360 static void
5361 fsleep(float sec)
5362 {
5363 struct timespec req;
5364 req.tv_sec = floor(sec);
5365 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
5366 nanosleep(&req, NULL);
5367 }
5368
5369 /*
5370 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
5371 * if we were unable to determine its size.
5372 */
5373 static int
5374 terminal_height(void)
5375 {
5376 struct winsize win;
5377
5378 if (isatty(STDOUT_FILENO) == 0)
5379 return (-1);
5380
5381 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0)
5382 return (win.ws_row);
5383
5384 return (-1);
5385 }
5386
5387 /*
5388 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
5389 * print the result.
5390 *
5391 * name: Short name of the script ('iostat').
5392 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
5393 */
5394 static void
5395 print_zpool_script_help(char *name, char *path)
5396 {
5397 char *argv[] = {path, (char *)"-h", NULL};
5398 char **lines = NULL;
5399 int lines_cnt = 0;
5400 int rc;
5401
5402 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
5403 &lines_cnt);
5404 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
5405 if (lines != NULL)
5406 libzfs_free_str_array(lines, lines_cnt);
5407 return;
5408 }
5409
5410 for (int i = 0; i < lines_cnt; i++)
5411 if (!is_blank_str(lines[i]))
5412 printf(" %-14s %s\n", name, lines[i]);
5413
5414 libzfs_free_str_array(lines, lines_cnt);
5415 }
5416
5417 /*
5418 * Go though the zpool status/iostat -c scripts in the user's path, run their
5419 * help option (-h), and print out the results.
5420 */
5421 static void
5422 print_zpool_dir_scripts(char *dirpath)
5423 {
5424 DIR *dir;
5425 struct dirent *ent;
5426 char fullpath[MAXPATHLEN];
5427 struct stat dir_stat;
5428
5429 if ((dir = opendir(dirpath)) != NULL) {
5430 /* print all the files and directories within directory */
5431 while ((ent = readdir(dir)) != NULL) {
5432 if (snprintf(fullpath, sizeof (fullpath), "%s/%s",
5433 dirpath, ent->d_name) >= sizeof (fullpath)) {
5434 (void) fprintf(stderr,
5435 gettext("internal error: "
5436 "ZPOOL_SCRIPTS_PATH too large.\n"));
5437 exit(1);
5438 }
5439
5440 /* Print the scripts */
5441 if (stat(fullpath, &dir_stat) == 0)
5442 if (dir_stat.st_mode & S_IXUSR &&
5443 S_ISREG(dir_stat.st_mode))
5444 print_zpool_script_help(ent->d_name,
5445 fullpath);
5446 }
5447 closedir(dir);
5448 }
5449 }
5450
5451 /*
5452 * Print out help text for all zpool status/iostat -c scripts.
5453 */
5454 static void
5455 print_zpool_script_list(const char *subcommand)
5456 {
5457 char *dir, *sp, *tmp;
5458
5459 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
5460
5461 sp = zpool_get_cmd_search_path();
5462 if (sp == NULL)
5463 return;
5464
5465 for (dir = strtok_r(sp, ":", &tmp);
5466 dir != NULL;
5467 dir = strtok_r(NULL, ":", &tmp))
5468 print_zpool_dir_scripts(dir);
5469
5470 free(sp);
5471 }
5472
5473 /*
5474 * Set the minimum pool/vdev name column width. The width must be at least 10,
5475 * but may be as large as the column width - 42 so it still fits on one line.
5476 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
5477 */
5478 static int
5479 get_namewidth_iostat(zpool_handle_t *zhp, void *data)
5480 {
5481 iostat_cbdata_t *cb = data;
5482 int width, available_width;
5483
5484 /*
5485 * get_namewidth() returns the maximum width of any name in that column
5486 * for any pool/vdev/device line that will be output.
5487 */
5488 width = get_namewidth(zhp, cb->cb_namewidth,
5489 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
5490
5491 /*
5492 * The width we are calculating is the width of the header and also the
5493 * padding width for names that are less than maximum width. The stats
5494 * take up 42 characters, so the width available for names is:
5495 */
5496 available_width = get_columns() - 42;
5497
5498 /*
5499 * If the maximum width fits on a screen, then great! Make everything
5500 * line up by justifying all lines to the same width. If that max
5501 * width is larger than what's available, the name plus stats won't fit
5502 * on one line, and justifying to that width would cause every line to
5503 * wrap on the screen. We only want lines with long names to wrap.
5504 * Limit the padding to what won't wrap.
5505 */
5506 if (width > available_width)
5507 width = available_width;
5508
5509 /*
5510 * And regardless of whatever the screen width is (get_columns can
5511 * return 0 if the width is not known or less than 42 for a narrow
5512 * terminal) have the width be a minimum of 10.
5513 */
5514 if (width < 10)
5515 width = 10;
5516
5517 /* Save the calculated width */
5518 cb->cb_namewidth = width;
5519
5520 return (0);
5521 }
5522
5523 /*
5524 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
5525 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
5526 * [interval [count]]
5527 *
5528 * -c CMD For each vdev, run command CMD
5529 * -g Display guid for individual vdev name.
5530 * -L Follow links when resolving vdev path name.
5531 * -P Display full path for vdev name.
5532 * -v Display statistics for individual vdevs
5533 * -h Display help
5534 * -p Display values in parsable (exact) format.
5535 * -H Scripted mode. Don't display headers, and separate properties
5536 * by a single tab.
5537 * -l Display average latency
5538 * -q Display queue depths
5539 * -w Display latency histograms
5540 * -r Display request size histogram
5541 * -T Display a timestamp in date(1) or Unix format
5542 * -n Only print headers once
5543 *
5544 * This command can be tricky because we want to be able to deal with pool
5545 * creation/destruction as well as vdev configuration changes. The bulk of this
5546 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
5547 * on pool_list_update() to detect the addition of new pools. Configuration
5548 * changes are all handled within libzfs.
5549 */
5550 int
5551 zpool_do_iostat(int argc, char **argv)
5552 {
5553 int c;
5554 int ret;
5555 int npools;
5556 float interval = 0;
5557 unsigned long count = 0;
5558 int winheight = 24;
5559 zpool_list_t *list;
5560 boolean_t verbose = B_FALSE;
5561 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
5562 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
5563 boolean_t omit_since_boot = B_FALSE;
5564 boolean_t guid = B_FALSE;
5565 boolean_t follow_links = B_FALSE;
5566 boolean_t full_name = B_FALSE;
5567 boolean_t headers_once = B_FALSE;
5568 iostat_cbdata_t cb = { 0 };
5569 char *cmd = NULL;
5570
5571 /* Used for printing error message */
5572 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
5573 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
5574
5575 uint64_t unsupported_flags;
5576
5577 /* check options */
5578 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) {
5579 switch (c) {
5580 case 'c':
5581 if (cmd != NULL) {
5582 fprintf(stderr,
5583 gettext("Can't set -c flag twice\n"));
5584 exit(1);
5585 }
5586
5587 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
5588 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
5589 fprintf(stderr, gettext(
5590 "Can't run -c, disabled by "
5591 "ZPOOL_SCRIPTS_ENABLED.\n"));
5592 exit(1);
5593 }
5594
5595 if ((getuid() <= 0 || geteuid() <= 0) &&
5596 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
5597 fprintf(stderr, gettext(
5598 "Can't run -c with root privileges "
5599 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
5600 exit(1);
5601 }
5602 cmd = optarg;
5603 verbose = B_TRUE;
5604 break;
5605 case 'g':
5606 guid = B_TRUE;
5607 break;
5608 case 'L':
5609 follow_links = B_TRUE;
5610 break;
5611 case 'P':
5612 full_name = B_TRUE;
5613 break;
5614 case 'T':
5615 get_timestamp_arg(*optarg);
5616 break;
5617 case 'v':
5618 verbose = B_TRUE;
5619 break;
5620 case 'p':
5621 parsable = B_TRUE;
5622 break;
5623 case 'l':
5624 latency = B_TRUE;
5625 break;
5626 case 'q':
5627 queues = B_TRUE;
5628 break;
5629 case 'H':
5630 scripted = B_TRUE;
5631 break;
5632 case 'w':
5633 l_histo = B_TRUE;
5634 break;
5635 case 'r':
5636 rq_histo = B_TRUE;
5637 break;
5638 case 'y':
5639 omit_since_boot = B_TRUE;
5640 break;
5641 case 'n':
5642 headers_once = B_TRUE;
5643 break;
5644 case 'h':
5645 usage(B_FALSE);
5646 break;
5647 case '?':
5648 if (optopt == 'c') {
5649 print_zpool_script_list("iostat");
5650 exit(0);
5651 } else {
5652 fprintf(stderr,
5653 gettext("invalid option '%c'\n"), optopt);
5654 }
5655 usage(B_FALSE);
5656 }
5657 }
5658
5659 argc -= optind;
5660 argv += optind;
5661
5662 cb.cb_literal = parsable;
5663 cb.cb_scripted = scripted;
5664
5665 if (guid)
5666 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID;
5667 if (follow_links)
5668 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
5669 if (full_name)
5670 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH;
5671 cb.cb_iteration = 0;
5672 cb.cb_namewidth = 0;
5673 cb.cb_verbose = verbose;
5674
5675 /* Get our interval and count values (if any) */
5676 if (guid) {
5677 get_interval_count_filter_guids(&argc, argv, &interval,
5678 &count, &cb);
5679 } else {
5680 get_interval_count(&argc, argv, &interval, &count);
5681 }
5682
5683 if (argc == 0) {
5684 /* No args, so just print the defaults. */
5685 } else if (are_all_pools(argc, argv)) {
5686 /* All the args are pool names */
5687 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
5688 /* All the args are vdevs */
5689 cb.cb_vdevs.cb_names = argv;
5690 cb.cb_vdevs.cb_names_count = argc;
5691 argc = 0; /* No pools to process */
5692 } else if (are_all_pools(1, argv)) {
5693 /* The first arg is a pool name */
5694 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
5695 &cb.cb_vdevs)) {
5696 /* ...and the rest are vdev names */
5697 cb.cb_vdevs.cb_names = argv + 1;
5698 cb.cb_vdevs.cb_names_count = argc - 1;
5699 argc = 1; /* One pool to process */
5700 } else {
5701 fprintf(stderr, gettext("Expected either a list of "));
5702 fprintf(stderr, gettext("pools, or list of vdevs in"));
5703 fprintf(stderr, " \"%s\", ", argv[0]);
5704 fprintf(stderr, gettext("but got:\n"));
5705 error_list_unresolved_vdevs(argc - 1, argv + 1,
5706 argv[0], &cb.cb_vdevs);
5707 fprintf(stderr, "\n");
5708 usage(B_FALSE);
5709 return (1);
5710 }
5711 } else {
5712 /*
5713 * The args don't make sense. The first arg isn't a pool name,
5714 * nor are all the args vdevs.
5715 */
5716 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
5717 fprintf(stderr, "\n");
5718 return (1);
5719 }
5720
5721 if (cb.cb_vdevs.cb_names_count != 0) {
5722 /*
5723 * If user specified vdevs, it implies verbose.
5724 */
5725 cb.cb_verbose = B_TRUE;
5726 }
5727
5728 /*
5729 * Construct the list of all interesting pools.
5730 */
5731 ret = 0;
5732 if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable,
5733 &ret)) == NULL)
5734 return (1);
5735
5736 if (pool_list_count(list) == 0 && argc != 0) {
5737 pool_list_free(list);
5738 return (1);
5739 }
5740
5741 if (pool_list_count(list) == 0 && interval == 0) {
5742 pool_list_free(list);
5743 (void) fprintf(stderr, gettext("no pools available\n"));
5744 return (1);
5745 }
5746
5747 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
5748 pool_list_free(list);
5749 (void) fprintf(stderr,
5750 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
5751 usage(B_FALSE);
5752 return (1);
5753 }
5754
5755 if (l_histo && rq_histo) {
5756 pool_list_free(list);
5757 (void) fprintf(stderr,
5758 gettext("Only one of [-r|-w] can be passed at a time\n"));
5759 usage(B_FALSE);
5760 return (1);
5761 }
5762
5763 /*
5764 * Enter the main iostat loop.
5765 */
5766 cb.cb_list = list;
5767
5768 if (l_histo) {
5769 /*
5770 * Histograms tables look out of place when you try to display
5771 * them with the other stats, so make a rule that you can only
5772 * print histograms by themselves.
5773 */
5774 cb.cb_flags = IOS_L_HISTO_M;
5775 } else if (rq_histo) {
5776 cb.cb_flags = IOS_RQ_HISTO_M;
5777 } else {
5778 cb.cb_flags = IOS_DEFAULT_M;
5779 if (latency)
5780 cb.cb_flags |= IOS_LATENCY_M;
5781 if (queues)
5782 cb.cb_flags |= IOS_QUEUES_M;
5783 }
5784
5785 /*
5786 * See if the module supports all the stats we want to display.
5787 */
5788 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
5789 if (unsupported_flags) {
5790 uint64_t f;
5791 int idx;
5792 fprintf(stderr,
5793 gettext("The loaded zfs module doesn't support:"));
5794
5795 /* for each bit set in unsupported_flags */
5796 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
5797 idx = lowbit64(f) - 1;
5798 fprintf(stderr, " -%c", flag_to_arg[idx]);
5799 }
5800
5801 fprintf(stderr, ". Try running a newer module.\n");
5802 pool_list_free(list);
5803
5804 return (1);
5805 }
5806
5807 for (;;) {
5808 if ((npools = pool_list_count(list)) == 0)
5809 (void) fprintf(stderr, gettext("no pools available\n"));
5810 else {
5811 /*
5812 * If this is the first iteration and -y was supplied
5813 * we skip any printing.
5814 */
5815 boolean_t skip = (omit_since_boot &&
5816 cb.cb_iteration == 0);
5817
5818 /*
5819 * Refresh all statistics. This is done as an
5820 * explicit step before calculating the maximum name
5821 * width, so that any * configuration changes are
5822 * properly accounted for.
5823 */
5824 (void) pool_list_iter(list, B_FALSE, refresh_iostat,
5825 &cb);
5826
5827 /*
5828 * Iterate over all pools to determine the maximum width
5829 * for the pool / device name column across all pools.
5830 */
5831 cb.cb_namewidth = 0;
5832 (void) pool_list_iter(list, B_FALSE,
5833 get_namewidth_iostat, &cb);
5834
5835 if (timestamp_fmt != NODATE)
5836 print_timestamp(timestamp_fmt);
5837
5838 if (cmd != NULL && cb.cb_verbose &&
5839 !(cb.cb_flags & IOS_ANYHISTO_M)) {
5840 cb.vcdl = all_pools_for_each_vdev_run(argc,
5841 argv, cmd, g_zfs, cb.cb_vdevs.cb_names,
5842 cb.cb_vdevs.cb_names_count,
5843 cb.cb_vdevs.cb_name_flags);
5844 } else {
5845 cb.vcdl = NULL;
5846 }
5847
5848
5849 /*
5850 * Check terminal size so we can print headers
5851 * even when terminal window has its height
5852 * changed.
5853 */
5854 winheight = terminal_height();
5855 /*
5856 * Are we connected to TTY? If not, headers_once
5857 * should be true, to avoid breaking scripts.
5858 */
5859 if (winheight < 0)
5860 headers_once = B_TRUE;
5861
5862 /*
5863 * If it's the first time and we're not skipping it,
5864 * or either skip or verbose mode, print the header.
5865 *
5866 * The histogram code explicitly prints its header on
5867 * every vdev, so skip this for histograms.
5868 */
5869 if (((++cb.cb_iteration == 1 && !skip) ||
5870 (skip != verbose) ||
5871 (!headers_once &&
5872 (cb.cb_iteration % winheight) == 0)) &&
5873 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
5874 !cb.cb_scripted)
5875 print_iostat_header(&cb);
5876
5877 if (skip) {
5878 (void) fsleep(interval);
5879 continue;
5880 }
5881
5882 pool_list_iter(list, B_FALSE, print_iostat, &cb);
5883
5884 /*
5885 * If there's more than one pool, and we're not in
5886 * verbose mode (which prints a separator for us),
5887 * then print a separator.
5888 *
5889 * In addition, if we're printing specific vdevs then
5890 * we also want an ending separator.
5891 */
5892 if (((npools > 1 && !verbose &&
5893 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
5894 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
5895 cb.cb_vdevs.cb_names_count)) &&
5896 !cb.cb_scripted) {
5897 print_iostat_separator(&cb);
5898 if (cb.vcdl != NULL)
5899 print_cmd_columns(cb.vcdl, 1);
5900 printf("\n");
5901 }
5902
5903 if (cb.vcdl != NULL)
5904 free_vdev_cmd_data_list(cb.vcdl);
5905
5906 }
5907
5908 /*
5909 * Flush the output so that redirection to a file isn't buffered
5910 * indefinitely.
5911 */
5912 (void) fflush(stdout);
5913
5914 if (interval == 0)
5915 break;
5916
5917 if (count != 0 && --count == 0)
5918 break;
5919
5920 (void) fsleep(interval);
5921 }
5922
5923 pool_list_free(list);
5924
5925 return (ret);
5926 }
5927
5928 typedef struct list_cbdata {
5929 boolean_t cb_verbose;
5930 int cb_name_flags;
5931 int cb_namewidth;
5932 boolean_t cb_scripted;
5933 zprop_list_t *cb_proplist;
5934 boolean_t cb_literal;
5935 } list_cbdata_t;
5936
5937
5938 /*
5939 * Given a list of columns to display, output appropriate headers for each one.
5940 */
5941 static void
5942 print_header(list_cbdata_t *cb)
5943 {
5944 zprop_list_t *pl = cb->cb_proplist;
5945 char headerbuf[ZPOOL_MAXPROPLEN];
5946 const char *header;
5947 boolean_t first = B_TRUE;
5948 boolean_t right_justify;
5949 size_t width = 0;
5950
5951 for (; pl != NULL; pl = pl->pl_next) {
5952 width = pl->pl_width;
5953 if (first && cb->cb_verbose) {
5954 /*
5955 * Reset the width to accommodate the verbose listing
5956 * of devices.
5957 */
5958 width = cb->cb_namewidth;
5959 }
5960
5961 if (!first)
5962 (void) fputs(" ", stdout);
5963 else
5964 first = B_FALSE;
5965
5966 right_justify = B_FALSE;
5967 if (pl->pl_prop != ZPROP_USERPROP) {
5968 header = zpool_prop_column_name(pl->pl_prop);
5969 right_justify = zpool_prop_align_right(pl->pl_prop);
5970 } else {
5971 int i;
5972
5973 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5974 headerbuf[i] = toupper(pl->pl_user_prop[i]);
5975 headerbuf[i] = '\0';
5976 header = headerbuf;
5977 }
5978
5979 if (pl->pl_next == NULL && !right_justify)
5980 (void) fputs(header, stdout);
5981 else if (right_justify)
5982 (void) printf("%*s", (int)width, header);
5983 else
5984 (void) printf("%-*s", (int)width, header);
5985 }
5986
5987 (void) fputc('\n', stdout);
5988 }
5989
5990 /*
5991 * Given a pool and a list of properties, print out all the properties according
5992 * to the described layout. Used by zpool_do_list().
5993 */
5994 static void
5995 print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
5996 {
5997 zprop_list_t *pl = cb->cb_proplist;
5998 boolean_t first = B_TRUE;
5999 char property[ZPOOL_MAXPROPLEN];
6000 const char *propstr;
6001 boolean_t right_justify;
6002 size_t width;
6003
6004 for (; pl != NULL; pl = pl->pl_next) {
6005
6006 width = pl->pl_width;
6007 if (first && cb->cb_verbose) {
6008 /*
6009 * Reset the width to accommodate the verbose listing
6010 * of devices.
6011 */
6012 width = cb->cb_namewidth;
6013 }
6014
6015 if (!first) {
6016 if (cb->cb_scripted)
6017 (void) fputc('\t', stdout);
6018 else
6019 (void) fputs(" ", stdout);
6020 } else {
6021 first = B_FALSE;
6022 }
6023
6024 right_justify = B_FALSE;
6025 if (pl->pl_prop != ZPROP_USERPROP) {
6026 if (zpool_get_prop(zhp, pl->pl_prop, property,
6027 sizeof (property), NULL, cb->cb_literal) != 0)
6028 propstr = "-";
6029 else
6030 propstr = property;
6031
6032 right_justify = zpool_prop_align_right(pl->pl_prop);
6033 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
6034 zpool_prop_unsupported(pl->pl_user_prop)) &&
6035 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
6036 sizeof (property)) == 0) {
6037 propstr = property;
6038 } else {
6039 propstr = "-";
6040 }
6041
6042
6043 /*
6044 * If this is being called in scripted mode, or if this is the
6045 * last column and it is left-justified, don't include a width
6046 * format specifier.
6047 */
6048 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
6049 (void) fputs(propstr, stdout);
6050 else if (right_justify)
6051 (void) printf("%*s", (int)width, propstr);
6052 else
6053 (void) printf("%-*s", (int)width, propstr);
6054 }
6055
6056 (void) fputc('\n', stdout);
6057 }
6058
6059 static void
6060 print_one_column(zpool_prop_t prop, uint64_t value, const char *str,
6061 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format)
6062 {
6063 char propval[64];
6064 boolean_t fixed;
6065 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
6066
6067 switch (prop) {
6068 case ZPOOL_PROP_SIZE:
6069 case ZPOOL_PROP_EXPANDSZ:
6070 case ZPOOL_PROP_CHECKPOINT:
6071 case ZPOOL_PROP_DEDUPRATIO:
6072 if (value == 0)
6073 (void) strlcpy(propval, "-", sizeof (propval));
6074 else
6075 zfs_nicenum_format(value, propval, sizeof (propval),
6076 format);
6077 break;
6078 case ZPOOL_PROP_FRAGMENTATION:
6079 if (value == ZFS_FRAG_INVALID) {
6080 (void) strlcpy(propval, "-", sizeof (propval));
6081 } else if (format == ZFS_NICENUM_RAW) {
6082 (void) snprintf(propval, sizeof (propval), "%llu",
6083 (unsigned long long)value);
6084 } else {
6085 (void) snprintf(propval, sizeof (propval), "%llu%%",
6086 (unsigned long long)value);
6087 }
6088 break;
6089 case ZPOOL_PROP_CAPACITY:
6090 /* capacity value is in parts-per-10,000 (aka permyriad) */
6091 if (format == ZFS_NICENUM_RAW)
6092 (void) snprintf(propval, sizeof (propval), "%llu",
6093 (unsigned long long)value / 100);
6094 else
6095 (void) snprintf(propval, sizeof (propval),
6096 value < 1000 ? "%1.2f%%" : value < 10000 ?
6097 "%2.1f%%" : "%3.0f%%", value / 100.0);
6098 break;
6099 case ZPOOL_PROP_HEALTH:
6100 width = 8;
6101 (void) strlcpy(propval, str, sizeof (propval));
6102 break;
6103 default:
6104 zfs_nicenum_format(value, propval, sizeof (propval), format);
6105 }
6106
6107 if (!valid)
6108 (void) strlcpy(propval, "-", sizeof (propval));
6109
6110 if (scripted)
6111 (void) printf("\t%s", propval);
6112 else
6113 (void) printf(" %*s", (int)width, propval);
6114 }
6115
6116 /*
6117 * print static default line per vdev
6118 * not compatible with '-o' <proplist> option
6119 */
6120 static void
6121 print_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
6122 list_cbdata_t *cb, int depth, boolean_t isspare)
6123 {
6124 nvlist_t **child;
6125 vdev_stat_t *vs;
6126 uint_t c, children;
6127 char *vname;
6128 boolean_t scripted = cb->cb_scripted;
6129 uint64_t islog = B_FALSE;
6130 const char *dashes = "%-*s - - - - "
6131 "- - - - -\n";
6132
6133 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
6134 (uint64_t **)&vs, &c) == 0);
6135
6136 if (name != NULL) {
6137 boolean_t toplevel = (vs->vs_space != 0);
6138 uint64_t cap;
6139 enum zfs_nicenum_format format;
6140 const char *state;
6141
6142 if (cb->cb_literal)
6143 format = ZFS_NICENUM_RAW;
6144 else
6145 format = ZFS_NICENUM_1024;
6146
6147 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
6148 return;
6149
6150 if (scripted)
6151 (void) printf("\t%s", name);
6152 else if (strlen(name) + depth > cb->cb_namewidth)
6153 (void) printf("%*s%s", depth, "", name);
6154 else
6155 (void) printf("%*s%s%*s", depth, "", name,
6156 (int)(cb->cb_namewidth - strlen(name) - depth), "");
6157
6158 /*
6159 * Print the properties for the individual vdevs. Some
6160 * properties are only applicable to toplevel vdevs. The
6161 * 'toplevel' boolean value is passed to the print_one_column()
6162 * to indicate that the value is valid.
6163 */
6164 if (VDEV_STAT_VALID(vs_pspace, c) && vs->vs_pspace)
6165 print_one_column(ZPOOL_PROP_SIZE, vs->vs_pspace, NULL,
6166 scripted, B_TRUE, format);
6167 else
6168 print_one_column(ZPOOL_PROP_SIZE, vs->vs_space, NULL,
6169 scripted, toplevel, format);
6170 print_one_column(ZPOOL_PROP_ALLOCATED, vs->vs_alloc, NULL,
6171 scripted, toplevel, format);
6172 print_one_column(ZPOOL_PROP_FREE, vs->vs_space - vs->vs_alloc,
6173 NULL, scripted, toplevel, format);
6174 print_one_column(ZPOOL_PROP_CHECKPOINT,
6175 vs->vs_checkpoint_space, NULL, scripted, toplevel, format);
6176 print_one_column(ZPOOL_PROP_EXPANDSZ, vs->vs_esize, NULL,
6177 scripted, B_TRUE, format);
6178 print_one_column(ZPOOL_PROP_FRAGMENTATION,
6179 vs->vs_fragmentation, NULL, scripted,
6180 (vs->vs_fragmentation != ZFS_FRAG_INVALID && toplevel),
6181 format);
6182 cap = (vs->vs_space == 0) ? 0 :
6183 (vs->vs_alloc * 10000 / vs->vs_space);
6184 print_one_column(ZPOOL_PROP_CAPACITY, cap, NULL,
6185 scripted, toplevel, format);
6186 print_one_column(ZPOOL_PROP_DEDUPRATIO, 0, NULL,
6187 scripted, toplevel, format);
6188 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
6189 if (isspare) {
6190 if (vs->vs_aux == VDEV_AUX_SPARED)
6191 state = "INUSE";
6192 else if (vs->vs_state == VDEV_STATE_HEALTHY)
6193 state = "AVAIL";
6194 }
6195 print_one_column(ZPOOL_PROP_HEALTH, 0, state, scripted,
6196 B_TRUE, format);
6197 (void) fputc('\n', stdout);
6198 }
6199
6200 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
6201 &child, &children) != 0)
6202 return;
6203
6204 /* list the normal vdevs first */
6205 for (c = 0; c < children; c++) {
6206 uint64_t ishole = B_FALSE;
6207
6208 if (nvlist_lookup_uint64(child[c],
6209 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
6210 continue;
6211
6212 if (nvlist_lookup_uint64(child[c],
6213 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
6214 continue;
6215
6216 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
6217 continue;
6218
6219 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6220 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
6221 print_list_stats(zhp, vname, child[c], cb, depth + 2, B_FALSE);
6222 free(vname);
6223 }
6224
6225 /* list the classes: 'logs', 'dedup', and 'special' */
6226 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
6227 boolean_t printed = B_FALSE;
6228
6229 for (c = 0; c < children; c++) {
6230 char *bias = NULL;
6231 char *type = NULL;
6232
6233 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
6234 &islog) == 0 && islog) {
6235 bias = (char *)VDEV_ALLOC_CLASS_LOGS;
6236 } else {
6237 (void) nvlist_lookup_string(child[c],
6238 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
6239 (void) nvlist_lookup_string(child[c],
6240 ZPOOL_CONFIG_TYPE, &type);
6241 }
6242 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
6243 continue;
6244 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
6245 continue;
6246
6247 if (!printed) {
6248 /* LINTED E_SEC_PRINTF_VAR_FMT */
6249 (void) printf(dashes, cb->cb_namewidth,
6250 class_name[n]);
6251 printed = B_TRUE;
6252 }
6253 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6254 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
6255 print_list_stats(zhp, vname, child[c], cb, depth + 2,
6256 B_FALSE);
6257 free(vname);
6258 }
6259 }
6260
6261 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
6262 &child, &children) == 0 && children > 0) {
6263 /* LINTED E_SEC_PRINTF_VAR_FMT */
6264 (void) printf(dashes, cb->cb_namewidth, "cache");
6265 for (c = 0; c < children; c++) {
6266 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6267 cb->cb_name_flags);
6268 print_list_stats(zhp, vname, child[c], cb, depth + 2,
6269 B_FALSE);
6270 free(vname);
6271 }
6272 }
6273
6274 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
6275 &children) == 0 && children > 0) {
6276 /* LINTED E_SEC_PRINTF_VAR_FMT */
6277 (void) printf(dashes, cb->cb_namewidth, "spare");
6278 for (c = 0; c < children; c++) {
6279 vname = zpool_vdev_name(g_zfs, zhp, child[c],
6280 cb->cb_name_flags);
6281 print_list_stats(zhp, vname, child[c], cb, depth + 2,
6282 B_TRUE);
6283 free(vname);
6284 }
6285 }
6286 }
6287
6288 /*
6289 * Generic callback function to list a pool.
6290 */
6291 static int
6292 list_callback(zpool_handle_t *zhp, void *data)
6293 {
6294 list_cbdata_t *cbp = data;
6295
6296 print_pool(zhp, cbp);
6297
6298 if (cbp->cb_verbose) {
6299 nvlist_t *config, *nvroot;
6300
6301 config = zpool_get_config(zhp, NULL);
6302 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
6303 &nvroot) == 0);
6304 print_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE);
6305 }
6306
6307 return (0);
6308 }
6309
6310 /*
6311 * Set the minimum pool/vdev name column width. The width must be at least 9,
6312 * but may be as large as needed.
6313 */
6314 static int
6315 get_namewidth_list(zpool_handle_t *zhp, void *data)
6316 {
6317 list_cbdata_t *cb = data;
6318 int width;
6319
6320 width = get_namewidth(zhp, cb->cb_namewidth,
6321 cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
6322
6323 if (width < 9)
6324 width = 9;
6325
6326 cb->cb_namewidth = width;
6327
6328 return (0);
6329 }
6330
6331 /*
6332 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
6333 *
6334 * -g Display guid for individual vdev name.
6335 * -H Scripted mode. Don't display headers, and separate properties
6336 * by a single tab.
6337 * -L Follow links when resolving vdev path name.
6338 * -o List of properties to display. Defaults to
6339 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
6340 * "dedupratio,health,altroot"
6341 * -p Display values in parsable (exact) format.
6342 * -P Display full path for vdev name.
6343 * -T Display a timestamp in date(1) or Unix format
6344 *
6345 * List all pools in the system, whether or not they're healthy. Output space
6346 * statistics for each one, as well as health status summary.
6347 */
6348 int
6349 zpool_do_list(int argc, char **argv)
6350 {
6351 int c;
6352 int ret = 0;
6353 list_cbdata_t cb = { 0 };
6354 static char default_props[] =
6355 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
6356 "capacity,dedupratio,health,altroot";
6357 char *props = default_props;
6358 float interval = 0;
6359 unsigned long count = 0;
6360 zpool_list_t *list;
6361 boolean_t first = B_TRUE;
6362 current_prop_type = ZFS_TYPE_POOL;
6363
6364 /* check options */
6365 while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
6366 switch (c) {
6367 case 'g':
6368 cb.cb_name_flags |= VDEV_NAME_GUID;
6369 break;
6370 case 'H':
6371 cb.cb_scripted = B_TRUE;
6372 break;
6373 case 'L':
6374 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6375 break;
6376 case 'o':
6377 props = optarg;
6378 break;
6379 case 'P':
6380 cb.cb_name_flags |= VDEV_NAME_PATH;
6381 break;
6382 case 'p':
6383 cb.cb_literal = B_TRUE;
6384 break;
6385 case 'T':
6386 get_timestamp_arg(*optarg);
6387 break;
6388 case 'v':
6389 cb.cb_verbose = B_TRUE;
6390 cb.cb_namewidth = 8; /* 8 until precalc is avail */
6391 break;
6392 case ':':
6393 (void) fprintf(stderr, gettext("missing argument for "
6394 "'%c' option\n"), optopt);
6395 usage(B_FALSE);
6396 break;
6397 case '?':
6398 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6399 optopt);
6400 usage(B_FALSE);
6401 }
6402 }
6403
6404 argc -= optind;
6405 argv += optind;
6406
6407 get_interval_count(&argc, argv, &interval, &count);
6408
6409 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
6410 usage(B_FALSE);
6411
6412 for (;;) {
6413 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
6414 ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL)
6415 return (1);
6416
6417 if (pool_list_count(list) == 0)
6418 break;
6419
6420 cb.cb_namewidth = 0;
6421 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb);
6422
6423 if (timestamp_fmt != NODATE)
6424 print_timestamp(timestamp_fmt);
6425
6426 if (!cb.cb_scripted && (first || cb.cb_verbose)) {
6427 print_header(&cb);
6428 first = B_FALSE;
6429 }
6430 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
6431
6432 if (interval == 0)
6433 break;
6434
6435 if (count != 0 && --count == 0)
6436 break;
6437
6438 pool_list_free(list);
6439 (void) fsleep(interval);
6440 }
6441
6442 if (argc == 0 && !cb.cb_scripted && pool_list_count(list) == 0) {
6443 (void) printf(gettext("no pools available\n"));
6444 ret = 0;
6445 }
6446
6447 pool_list_free(list);
6448 zprop_free_list(cb.cb_proplist);
6449 return (ret);
6450 }
6451
6452 static int
6453 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
6454 {
6455 boolean_t force = B_FALSE;
6456 boolean_t rebuild = B_FALSE;
6457 boolean_t wait = B_FALSE;
6458 int c;
6459 nvlist_t *nvroot;
6460 char *poolname, *old_disk, *new_disk;
6461 zpool_handle_t *zhp;
6462 nvlist_t *props = NULL;
6463 char *propval;
6464 int ret;
6465
6466 /* check options */
6467 while ((c = getopt(argc, argv, "fo:sw")) != -1) {
6468 switch (c) {
6469 case 'f':
6470 force = B_TRUE;
6471 break;
6472 case 'o':
6473 if ((propval = strchr(optarg, '=')) == NULL) {
6474 (void) fprintf(stderr, gettext("missing "
6475 "'=' for -o option\n"));
6476 usage(B_FALSE);
6477 }
6478 *propval = '\0';
6479 propval++;
6480
6481 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
6482 (add_prop_list(optarg, propval, &props, B_TRUE)))
6483 usage(B_FALSE);
6484 break;
6485 case 's':
6486 rebuild = B_TRUE;
6487 break;
6488 case 'w':
6489 wait = B_TRUE;
6490 break;
6491 case '?':
6492 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6493 optopt);
6494 usage(B_FALSE);
6495 }
6496 }
6497
6498 argc -= optind;
6499 argv += optind;
6500
6501 /* get pool name and check number of arguments */
6502 if (argc < 1) {
6503 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6504 usage(B_FALSE);
6505 }
6506
6507 poolname = argv[0];
6508
6509 if (argc < 2) {
6510 (void) fprintf(stderr,
6511 gettext("missing <device> specification\n"));
6512 usage(B_FALSE);
6513 }
6514
6515 old_disk = argv[1];
6516
6517 if (argc < 3) {
6518 if (!replacing) {
6519 (void) fprintf(stderr,
6520 gettext("missing <new_device> specification\n"));
6521 usage(B_FALSE);
6522 }
6523 new_disk = old_disk;
6524 argc -= 1;
6525 argv += 1;
6526 } else {
6527 new_disk = argv[2];
6528 argc -= 2;
6529 argv += 2;
6530 }
6531
6532 if (argc > 1) {
6533 (void) fprintf(stderr, gettext("too many arguments\n"));
6534 usage(B_FALSE);
6535 }
6536
6537 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
6538 nvlist_free(props);
6539 return (1);
6540 }
6541
6542 if (zpool_get_config(zhp, NULL) == NULL) {
6543 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
6544 poolname);
6545 zpool_close(zhp);
6546 nvlist_free(props);
6547 return (1);
6548 }
6549
6550 /* unless manually specified use "ashift" pool property (if set) */
6551 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
6552 int intval;
6553 zprop_source_t src;
6554 char strval[ZPOOL_MAXPROPLEN];
6555
6556 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
6557 if (src != ZPROP_SRC_DEFAULT) {
6558 (void) sprintf(strval, "%" PRId32, intval);
6559 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
6560 &props, B_TRUE) == 0);
6561 }
6562 }
6563
6564 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
6565 argc, argv);
6566 if (nvroot == NULL) {
6567 zpool_close(zhp);
6568 nvlist_free(props);
6569 return (1);
6570 }
6571
6572 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing,
6573 rebuild);
6574
6575 if (ret == 0 && wait)
6576 ret = zpool_wait(zhp,
6577 replacing ? ZPOOL_WAIT_REPLACE : ZPOOL_WAIT_RESILVER);
6578
6579 nvlist_free(props);
6580 nvlist_free(nvroot);
6581 zpool_close(zhp);
6582
6583 return (ret);
6584 }
6585
6586 /*
6587 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
6588 *
6589 * -f Force attach, even if <new_device> appears to be in use.
6590 * -s Use sequential instead of healing reconstruction for resilver.
6591 * -o Set property=value.
6592 * -w Wait for replacing to complete before returning
6593 *
6594 * Replace <device> with <new_device>.
6595 */
6596 int
6597 zpool_do_replace(int argc, char **argv)
6598 {
6599 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
6600 }
6601
6602 /*
6603 * zpool attach [-fsw] [-o property=value] <pool> <device> <new_device>
6604 *
6605 * -f Force attach, even if <new_device> appears to be in use.
6606 * -s Use sequential instead of healing reconstruction for resilver.
6607 * -o Set property=value.
6608 * -w Wait for resilvering to complete before returning
6609 *
6610 * Attach <new_device> to the mirror containing <device>. If <device> is not
6611 * part of a mirror, then <device> will be transformed into a mirror of
6612 * <device> and <new_device>. In either case, <new_device> will begin life
6613 * with a DTL of [0, now], and will immediately begin to resilver itself.
6614 */
6615 int
6616 zpool_do_attach(int argc, char **argv)
6617 {
6618 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
6619 }
6620
6621 /*
6622 * zpool detach [-f] <pool> <device>
6623 *
6624 * -f Force detach of <device>, even if DTLs argue against it
6625 * (not supported yet)
6626 *
6627 * Detach a device from a mirror. The operation will be refused if <device>
6628 * is the last device in the mirror, or if the DTLs indicate that this device
6629 * has the only valid copy of some data.
6630 */
6631 int
6632 zpool_do_detach(int argc, char **argv)
6633 {
6634 int c;
6635 char *poolname, *path;
6636 zpool_handle_t *zhp;
6637 int ret;
6638
6639 /* check options */
6640 while ((c = getopt(argc, argv, "")) != -1) {
6641 switch (c) {
6642 case '?':
6643 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6644 optopt);
6645 usage(B_FALSE);
6646 }
6647 }
6648
6649 argc -= optind;
6650 argv += optind;
6651
6652 /* get pool name and check number of arguments */
6653 if (argc < 1) {
6654 (void) fprintf(stderr, gettext("missing pool name argument\n"));
6655 usage(B_FALSE);
6656 }
6657
6658 if (argc < 2) {
6659 (void) fprintf(stderr,
6660 gettext("missing <device> specification\n"));
6661 usage(B_FALSE);
6662 }
6663
6664 poolname = argv[0];
6665 path = argv[1];
6666
6667 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6668 return (1);
6669
6670 ret = zpool_vdev_detach(zhp, path);
6671
6672 zpool_close(zhp);
6673
6674 return (ret);
6675 }
6676
6677 /*
6678 * zpool split [-gLnP] [-o prop=val] ...
6679 * [-o mntopt] ...
6680 * [-R altroot] <pool> <newpool> [<device> ...]
6681 *
6682 * -g Display guid for individual vdev name.
6683 * -L Follow links when resolving vdev path name.
6684 * -n Do not split the pool, but display the resulting layout if
6685 * it were to be split.
6686 * -o Set property=value, or set mount options.
6687 * -P Display full path for vdev name.
6688 * -R Mount the split-off pool under an alternate root.
6689 * -l Load encryption keys while importing.
6690 *
6691 * Splits the named pool and gives it the new pool name. Devices to be split
6692 * off may be listed, provided that no more than one device is specified
6693 * per top-level vdev mirror. The newly split pool is left in an exported
6694 * state unless -R is specified.
6695 *
6696 * Restrictions: the top-level of the pool pool must only be made up of
6697 * mirrors; all devices in the pool must be healthy; no device may be
6698 * undergoing a resilvering operation.
6699 */
6700 int
6701 zpool_do_split(int argc, char **argv)
6702 {
6703 char *srcpool, *newpool, *propval;
6704 char *mntopts = NULL;
6705 splitflags_t flags;
6706 int c, ret = 0;
6707 boolean_t loadkeys = B_FALSE;
6708 zpool_handle_t *zhp;
6709 nvlist_t *config, *props = NULL;
6710
6711 flags.dryrun = B_FALSE;
6712 flags.import = B_FALSE;
6713 flags.name_flags = 0;
6714
6715 /* check options */
6716 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
6717 switch (c) {
6718 case 'g':
6719 flags.name_flags |= VDEV_NAME_GUID;
6720 break;
6721 case 'L':
6722 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
6723 break;
6724 case 'R':
6725 flags.import = B_TRUE;
6726 if (add_prop_list(
6727 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
6728 &props, B_TRUE) != 0) {
6729 nvlist_free(props);
6730 usage(B_FALSE);
6731 }
6732 break;
6733 case 'l':
6734 loadkeys = B_TRUE;
6735 break;
6736 case 'n':
6737 flags.dryrun = B_TRUE;
6738 break;
6739 case 'o':
6740 if ((propval = strchr(optarg, '=')) != NULL) {
6741 *propval = '\0';
6742 propval++;
6743 if (add_prop_list(optarg, propval,
6744 &props, B_TRUE) != 0) {
6745 nvlist_free(props);
6746 usage(B_FALSE);
6747 }
6748 } else {
6749 mntopts = optarg;
6750 }
6751 break;
6752 case 'P':
6753 flags.name_flags |= VDEV_NAME_PATH;
6754 break;
6755 case ':':
6756 (void) fprintf(stderr, gettext("missing argument for "
6757 "'%c' option\n"), optopt);
6758 usage(B_FALSE);
6759 break;
6760 case '?':
6761 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6762 optopt);
6763 usage(B_FALSE);
6764 break;
6765 }
6766 }
6767
6768 if (!flags.import && mntopts != NULL) {
6769 (void) fprintf(stderr, gettext("setting mntopts is only "
6770 "valid when importing the pool\n"));
6771 usage(B_FALSE);
6772 }
6773
6774 if (!flags.import && loadkeys) {
6775 (void) fprintf(stderr, gettext("loading keys is only "
6776 "valid when importing the pool\n"));
6777 usage(B_FALSE);
6778 }
6779
6780 argc -= optind;
6781 argv += optind;
6782
6783 if (argc < 1) {
6784 (void) fprintf(stderr, gettext("Missing pool name\n"));
6785 usage(B_FALSE);
6786 }
6787 if (argc < 2) {
6788 (void) fprintf(stderr, gettext("Missing new pool name\n"));
6789 usage(B_FALSE);
6790 }
6791
6792 srcpool = argv[0];
6793 newpool = argv[1];
6794
6795 argc -= 2;
6796 argv += 2;
6797
6798 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
6799 nvlist_free(props);
6800 return (1);
6801 }
6802
6803 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
6804 if (config == NULL) {
6805 ret = 1;
6806 } else {
6807 if (flags.dryrun) {
6808 (void) printf(gettext("would create '%s' with the "
6809 "following layout:\n\n"), newpool);
6810 print_vdev_tree(NULL, newpool, config, 0, "",
6811 flags.name_flags);
6812 print_vdev_tree(NULL, "dedup", config, 0,
6813 VDEV_ALLOC_BIAS_DEDUP, 0);
6814 print_vdev_tree(NULL, "special", config, 0,
6815 VDEV_ALLOC_BIAS_SPECIAL, 0);
6816 }
6817 }
6818
6819 zpool_close(zhp);
6820
6821 if (ret != 0 || flags.dryrun || !flags.import) {
6822 nvlist_free(config);
6823 nvlist_free(props);
6824 return (ret);
6825 }
6826
6827 /*
6828 * The split was successful. Now we need to open the new
6829 * pool and import it.
6830 */
6831 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
6832 nvlist_free(config);
6833 nvlist_free(props);
6834 return (1);
6835 }
6836
6837 if (loadkeys) {
6838 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
6839 if (ret != 0)
6840 ret = 1;
6841 }
6842
6843 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
6844 zpool_enable_datasets(zhp, mntopts, 0) != 0) {
6845 ret = 1;
6846 (void) fprintf(stderr, gettext("Split was successful, but "
6847 "the datasets could not all be mounted\n"));
6848 (void) fprintf(stderr, gettext("Try doing '%s' with a "
6849 "different altroot\n"), "zpool import");
6850 }
6851 zpool_close(zhp);
6852 nvlist_free(config);
6853 nvlist_free(props);
6854
6855 return (ret);
6856 }
6857
6858
6859
6860 /*
6861 * zpool online <pool> <device> ...
6862 */
6863 int
6864 zpool_do_online(int argc, char **argv)
6865 {
6866 int c, i;
6867 char *poolname;
6868 zpool_handle_t *zhp;
6869 int ret = 0;
6870 vdev_state_t newstate;
6871 int flags = 0;
6872
6873 /* check options */
6874 while ((c = getopt(argc, argv, "e")) != -1) {
6875 switch (c) {
6876 case 'e':
6877 flags |= ZFS_ONLINE_EXPAND;
6878 break;
6879 case '?':
6880 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6881 optopt);
6882 usage(B_FALSE);
6883 }
6884 }
6885
6886 argc -= optind;
6887 argv += optind;
6888
6889 /* get pool name and check number of arguments */
6890 if (argc < 1) {
6891 (void) fprintf(stderr, gettext("missing pool name\n"));
6892 usage(B_FALSE);
6893 }
6894 if (argc < 2) {
6895 (void) fprintf(stderr, gettext("missing device name\n"));
6896 usage(B_FALSE);
6897 }
6898
6899 poolname = argv[0];
6900
6901 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6902 return (1);
6903
6904 for (i = 1; i < argc; i++) {
6905 if (zpool_vdev_online(zhp, argv[i], flags, &newstate) == 0) {
6906 if (newstate != VDEV_STATE_HEALTHY) {
6907 (void) printf(gettext("warning: device '%s' "
6908 "onlined, but remains in faulted state\n"),
6909 argv[i]);
6910 if (newstate == VDEV_STATE_FAULTED)
6911 (void) printf(gettext("use 'zpool "
6912 "clear' to restore a faulted "
6913 "device\n"));
6914 else
6915 (void) printf(gettext("use 'zpool "
6916 "replace' to replace devices "
6917 "that are no longer present\n"));
6918 }
6919 } else {
6920 ret = 1;
6921 }
6922 }
6923
6924 zpool_close(zhp);
6925
6926 return (ret);
6927 }
6928
6929 /*
6930 * zpool offline [-ft] <pool> <device> ...
6931 *
6932 * -f Force the device into a faulted state.
6933 *
6934 * -t Only take the device off-line temporarily. The offline/faulted
6935 * state will not be persistent across reboots.
6936 */
6937 int
6938 zpool_do_offline(int argc, char **argv)
6939 {
6940 int c, i;
6941 char *poolname;
6942 zpool_handle_t *zhp;
6943 int ret = 0;
6944 boolean_t istmp = B_FALSE;
6945 boolean_t fault = B_FALSE;
6946
6947 /* check options */
6948 while ((c = getopt(argc, argv, "ft")) != -1) {
6949 switch (c) {
6950 case 'f':
6951 fault = B_TRUE;
6952 break;
6953 case 't':
6954 istmp = B_TRUE;
6955 break;
6956 case '?':
6957 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6958 optopt);
6959 usage(B_FALSE);
6960 }
6961 }
6962
6963 argc -= optind;
6964 argv += optind;
6965
6966 /* get pool name and check number of arguments */
6967 if (argc < 1) {
6968 (void) fprintf(stderr, gettext("missing pool name\n"));
6969 usage(B_FALSE);
6970 }
6971 if (argc < 2) {
6972 (void) fprintf(stderr, gettext("missing device name\n"));
6973 usage(B_FALSE);
6974 }
6975
6976 poolname = argv[0];
6977
6978 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
6979 return (1);
6980
6981 for (i = 1; i < argc; i++) {
6982 if (fault) {
6983 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
6984 vdev_aux_t aux;
6985 if (istmp == B_FALSE) {
6986 /* Force the fault to persist across imports */
6987 aux = VDEV_AUX_EXTERNAL_PERSIST;
6988 } else {
6989 aux = VDEV_AUX_EXTERNAL;
6990 }
6991
6992 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
6993 ret = 1;
6994 } else {
6995 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
6996 ret = 1;
6997 }
6998 }
6999
7000 zpool_close(zhp);
7001
7002 return (ret);
7003 }
7004
7005 /*
7006 * zpool clear <pool> [device]
7007 *
7008 * Clear all errors associated with a pool or a particular device.
7009 */
7010 int
7011 zpool_do_clear(int argc, char **argv)
7012 {
7013 int c;
7014 int ret = 0;
7015 boolean_t dryrun = B_FALSE;
7016 boolean_t do_rewind = B_FALSE;
7017 boolean_t xtreme_rewind = B_FALSE;
7018 uint32_t rewind_policy = ZPOOL_NO_REWIND;
7019 nvlist_t *policy = NULL;
7020 zpool_handle_t *zhp;
7021 char *pool, *device;
7022
7023 /* check options */
7024 while ((c = getopt(argc, argv, "FnX")) != -1) {
7025 switch (c) {
7026 case 'F':
7027 do_rewind = B_TRUE;
7028 break;
7029 case 'n':
7030 dryrun = B_TRUE;
7031 break;
7032 case 'X':
7033 xtreme_rewind = B_TRUE;
7034 break;
7035 case '?':
7036 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7037 optopt);
7038 usage(B_FALSE);
7039 }
7040 }
7041
7042 argc -= optind;
7043 argv += optind;
7044
7045 if (argc < 1) {
7046 (void) fprintf(stderr, gettext("missing pool name\n"));
7047 usage(B_FALSE);
7048 }
7049
7050 if (argc > 2) {
7051 (void) fprintf(stderr, gettext("too many arguments\n"));
7052 usage(B_FALSE);
7053 }
7054
7055 if ((dryrun || xtreme_rewind) && !do_rewind) {
7056 (void) fprintf(stderr,
7057 gettext("-n or -X only meaningful with -F\n"));
7058 usage(B_FALSE);
7059 }
7060 if (dryrun)
7061 rewind_policy = ZPOOL_TRY_REWIND;
7062 else if (do_rewind)
7063 rewind_policy = ZPOOL_DO_REWIND;
7064 if (xtreme_rewind)
7065 rewind_policy |= ZPOOL_EXTREME_REWIND;
7066
7067 /* In future, further rewind policy choices can be passed along here */
7068 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
7069 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
7070 rewind_policy) != 0) {
7071 return (1);
7072 }
7073
7074 pool = argv[0];
7075 device = argc == 2 ? argv[1] : NULL;
7076
7077 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
7078 nvlist_free(policy);
7079 return (1);
7080 }
7081
7082 if (zpool_clear(zhp, device, policy) != 0)
7083 ret = 1;
7084
7085 zpool_close(zhp);
7086
7087 nvlist_free(policy);
7088
7089 return (ret);
7090 }
7091
7092 /*
7093 * zpool reguid <pool>
7094 */
7095 int
7096 zpool_do_reguid(int argc, char **argv)
7097 {
7098 int c;
7099 char *poolname;
7100 zpool_handle_t *zhp;
7101 int ret = 0;
7102
7103 /* check options */
7104 while ((c = getopt(argc, argv, "")) != -1) {
7105 switch (c) {
7106 case '?':
7107 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7108 optopt);
7109 usage(B_FALSE);
7110 }
7111 }
7112
7113 argc -= optind;
7114 argv += optind;
7115
7116 /* get pool name and check number of arguments */
7117 if (argc < 1) {
7118 (void) fprintf(stderr, gettext("missing pool name\n"));
7119 usage(B_FALSE);
7120 }
7121
7122 if (argc > 1) {
7123 (void) fprintf(stderr, gettext("too many arguments\n"));
7124 usage(B_FALSE);
7125 }
7126
7127 poolname = argv[0];
7128 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
7129 return (1);
7130
7131 ret = zpool_reguid(zhp);
7132
7133 zpool_close(zhp);
7134 return (ret);
7135 }
7136
7137
7138 /*
7139 * zpool reopen <pool>
7140 *
7141 * Reopen the pool so that the kernel can update the sizes of all vdevs.
7142 */
7143 int
7144 zpool_do_reopen(int argc, char **argv)
7145 {
7146 int c;
7147 int ret = 0;
7148 boolean_t scrub_restart = B_TRUE;
7149
7150 /* check options */
7151 while ((c = getopt(argc, argv, "n")) != -1) {
7152 switch (c) {
7153 case 'n':
7154 scrub_restart = B_FALSE;
7155 break;
7156 case '?':
7157 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7158 optopt);
7159 usage(B_FALSE);
7160 }
7161 }
7162
7163 argc -= optind;
7164 argv += optind;
7165
7166 /* if argc == 0 we will execute zpool_reopen_one on all pools */
7167 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7168 B_FALSE, zpool_reopen_one, &scrub_restart);
7169
7170 return (ret);
7171 }
7172
7173 typedef struct scrub_cbdata {
7174 int cb_type;
7175 pool_scrub_cmd_t cb_scrub_cmd;
7176 } scrub_cbdata_t;
7177
7178 static boolean_t
7179 zpool_has_checkpoint(zpool_handle_t *zhp)
7180 {
7181 nvlist_t *config, *nvroot;
7182
7183 config = zpool_get_config(zhp, NULL);
7184
7185 if (config != NULL) {
7186 pool_checkpoint_stat_t *pcs = NULL;
7187 uint_t c;
7188
7189 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
7190 (void) nvlist_lookup_uint64_array(nvroot,
7191 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7192
7193 if (pcs == NULL || pcs->pcs_state == CS_NONE)
7194 return (B_FALSE);
7195
7196 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
7197 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
7198 return (B_TRUE);
7199 }
7200
7201 return (B_FALSE);
7202 }
7203
7204 static int
7205 scrub_callback(zpool_handle_t *zhp, void *data)
7206 {
7207 scrub_cbdata_t *cb = data;
7208 int err;
7209
7210 /*
7211 * Ignore faulted pools.
7212 */
7213 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
7214 (void) fprintf(stderr, gettext("cannot scan '%s': pool is "
7215 "currently unavailable\n"), zpool_get_name(zhp));
7216 return (1);
7217 }
7218
7219 err = zpool_scan(zhp, cb->cb_type, cb->cb_scrub_cmd);
7220
7221 if (err == 0 && zpool_has_checkpoint(zhp) &&
7222 cb->cb_type == POOL_SCAN_SCRUB) {
7223 (void) printf(gettext("warning: will not scrub state that "
7224 "belongs to the checkpoint of pool '%s'\n"),
7225 zpool_get_name(zhp));
7226 }
7227
7228 return (err != 0);
7229 }
7230
7231 static int
7232 wait_callback(zpool_handle_t *zhp, void *data)
7233 {
7234 zpool_wait_activity_t *act = data;
7235 return (zpool_wait(zhp, *act));
7236 }
7237
7238 /*
7239 * zpool scrub [-s | -p] [-w] <pool> ...
7240 *
7241 * -s Stop. Stops any in-progress scrub.
7242 * -p Pause. Pause in-progress scrub.
7243 * -w Wait. Blocks until scrub has completed.
7244 */
7245 int
7246 zpool_do_scrub(int argc, char **argv)
7247 {
7248 int c;
7249 scrub_cbdata_t cb;
7250 boolean_t wait = B_FALSE;
7251 int error;
7252
7253 cb.cb_type = POOL_SCAN_SCRUB;
7254 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
7255
7256 /* check options */
7257 while ((c = getopt(argc, argv, "spw")) != -1) {
7258 switch (c) {
7259 case 's':
7260 cb.cb_type = POOL_SCAN_NONE;
7261 break;
7262 case 'p':
7263 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
7264 break;
7265 case 'w':
7266 wait = B_TRUE;
7267 break;
7268 case '?':
7269 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7270 optopt);
7271 usage(B_FALSE);
7272 }
7273 }
7274
7275 if (cb.cb_type == POOL_SCAN_NONE &&
7276 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE) {
7277 (void) fprintf(stderr, gettext("invalid option combination: "
7278 "-s and -p are mutually exclusive\n"));
7279 usage(B_FALSE);
7280 }
7281
7282 if (wait && (cb.cb_type == POOL_SCAN_NONE ||
7283 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) {
7284 (void) fprintf(stderr, gettext("invalid option combination: "
7285 "-w cannot be used with -p or -s\n"));
7286 usage(B_FALSE);
7287 }
7288
7289 argc -= optind;
7290 argv += optind;
7291
7292 if (argc < 1) {
7293 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7294 usage(B_FALSE);
7295 }
7296
7297 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7298 B_FALSE, scrub_callback, &cb);
7299
7300 if (wait && !error) {
7301 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB;
7302 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7303 B_FALSE, wait_callback, &act);
7304 }
7305
7306 return (error);
7307 }
7308
7309 /*
7310 * zpool resilver <pool> ...
7311 *
7312 * Restarts any in-progress resilver
7313 */
7314 int
7315 zpool_do_resilver(int argc, char **argv)
7316 {
7317 int c;
7318 scrub_cbdata_t cb;
7319
7320 cb.cb_type = POOL_SCAN_RESILVER;
7321 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
7322
7323 /* check options */
7324 while ((c = getopt(argc, argv, "")) != -1) {
7325 switch (c) {
7326 case '?':
7327 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7328 optopt);
7329 usage(B_FALSE);
7330 }
7331 }
7332
7333 argc -= optind;
7334 argv += optind;
7335
7336 if (argc < 1) {
7337 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7338 usage(B_FALSE);
7339 }
7340
7341 return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
7342 B_FALSE, scrub_callback, &cb));
7343 }
7344
7345 /*
7346 * zpool trim [-d] [-r <rate>] [-c | -s] <pool> [<device> ...]
7347 *
7348 * -c Cancel. Ends any in-progress trim.
7349 * -d Secure trim. Requires kernel and device support.
7350 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
7351 * adding a multiplier suffix such as 'k' or 'm'.
7352 * -s Suspend. TRIM can then be restarted with no flags.
7353 * -w Wait. Blocks until trimming has completed.
7354 */
7355 int
7356 zpool_do_trim(int argc, char **argv)
7357 {
7358 struct option long_options[] = {
7359 {"cancel", no_argument, NULL, 'c'},
7360 {"secure", no_argument, NULL, 'd'},
7361 {"rate", required_argument, NULL, 'r'},
7362 {"suspend", no_argument, NULL, 's'},
7363 {"wait", no_argument, NULL, 'w'},
7364 {0, 0, 0, 0}
7365 };
7366
7367 pool_trim_func_t cmd_type = POOL_TRIM_START;
7368 uint64_t rate = 0;
7369 boolean_t secure = B_FALSE;
7370 boolean_t wait = B_FALSE;
7371
7372 int c;
7373 while ((c = getopt_long(argc, argv, "cdr:sw", long_options, NULL))
7374 != -1) {
7375 switch (c) {
7376 case 'c':
7377 if (cmd_type != POOL_TRIM_START &&
7378 cmd_type != POOL_TRIM_CANCEL) {
7379 (void) fprintf(stderr, gettext("-c cannot be "
7380 "combined with other options\n"));
7381 usage(B_FALSE);
7382 }
7383 cmd_type = POOL_TRIM_CANCEL;
7384 break;
7385 case 'd':
7386 if (cmd_type != POOL_TRIM_START) {
7387 (void) fprintf(stderr, gettext("-d cannot be "
7388 "combined with the -c or -s options\n"));
7389 usage(B_FALSE);
7390 }
7391 secure = B_TRUE;
7392 break;
7393 case 'r':
7394 if (cmd_type != POOL_TRIM_START) {
7395 (void) fprintf(stderr, gettext("-r cannot be "
7396 "combined with the -c or -s options\n"));
7397 usage(B_FALSE);
7398 }
7399 if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) {
7400 (void) fprintf(stderr, "%s: %s\n",
7401 gettext("invalid value for rate"),
7402 libzfs_error_description(g_zfs));
7403 usage(B_FALSE);
7404 }
7405 break;
7406 case 's':
7407 if (cmd_type != POOL_TRIM_START &&
7408 cmd_type != POOL_TRIM_SUSPEND) {
7409 (void) fprintf(stderr, gettext("-s cannot be "
7410 "combined with other options\n"));
7411 usage(B_FALSE);
7412 }
7413 cmd_type = POOL_TRIM_SUSPEND;
7414 break;
7415 case 'w':
7416 wait = B_TRUE;
7417 break;
7418 case '?':
7419 if (optopt != 0) {
7420 (void) fprintf(stderr,
7421 gettext("invalid option '%c'\n"), optopt);
7422 } else {
7423 (void) fprintf(stderr,
7424 gettext("invalid option '%s'\n"),
7425 argv[optind - 1]);
7426 }
7427 usage(B_FALSE);
7428 }
7429 }
7430
7431 argc -= optind;
7432 argv += optind;
7433
7434 if (argc < 1) {
7435 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7436 usage(B_FALSE);
7437 return (-1);
7438 }
7439
7440 if (wait && (cmd_type != POOL_TRIM_START)) {
7441 (void) fprintf(stderr, gettext("-w cannot be used with -c or "
7442 "-s\n"));
7443 usage(B_FALSE);
7444 }
7445
7446 char *poolname = argv[0];
7447 zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
7448 if (zhp == NULL)
7449 return (-1);
7450
7451 trimflags_t trim_flags = {
7452 .secure = secure,
7453 .rate = rate,
7454 .wait = wait,
7455 };
7456
7457 nvlist_t *vdevs = fnvlist_alloc();
7458 if (argc == 1) {
7459 /* no individual leaf vdevs specified, so add them all */
7460 nvlist_t *config = zpool_get_config(zhp, NULL);
7461 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
7462 ZPOOL_CONFIG_VDEV_TREE);
7463 zpool_collect_leaves(zhp, nvroot, vdevs);
7464 trim_flags.fullpool = B_TRUE;
7465 } else {
7466 trim_flags.fullpool = B_FALSE;
7467 for (int i = 1; i < argc; i++) {
7468 fnvlist_add_boolean(vdevs, argv[i]);
7469 }
7470 }
7471
7472 int error = zpool_trim(zhp, cmd_type, vdevs, &trim_flags);
7473
7474 fnvlist_free(vdevs);
7475 zpool_close(zhp);
7476
7477 return (error);
7478 }
7479
7480 /*
7481 * Converts a total number of seconds to a human readable string broken
7482 * down in to days/hours/minutes/seconds.
7483 */
7484 static void
7485 secs_to_dhms(uint64_t total, char *buf)
7486 {
7487 uint64_t days = total / 60 / 60 / 24;
7488 uint64_t hours = (total / 60 / 60) % 24;
7489 uint64_t mins = (total / 60) % 60;
7490 uint64_t secs = (total % 60);
7491
7492 if (days > 0) {
7493 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu",
7494 (u_longlong_t)days, (u_longlong_t)hours,
7495 (u_longlong_t)mins, (u_longlong_t)secs);
7496 } else {
7497 (void) sprintf(buf, "%02llu:%02llu:%02llu",
7498 (u_longlong_t)hours, (u_longlong_t)mins,
7499 (u_longlong_t)secs);
7500 }
7501 }
7502
7503 /*
7504 * Print out detailed scrub status.
7505 */
7506 static void
7507 print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
7508 {
7509 time_t start, end, pause;
7510 uint64_t pass_scanned, scanned, pass_issued, issued, total;
7511 uint64_t elapsed, scan_rate, issue_rate;
7512 double fraction_done;
7513 char processed_buf[7], scanned_buf[7], issued_buf[7], total_buf[7];
7514 char srate_buf[7], irate_buf[7], time_buf[32];
7515
7516 printf(" ");
7517 printf_color(ANSI_BOLD, gettext("scan:"));
7518 printf(" ");
7519
7520 /* If there's never been a scan, there's not much to say. */
7521 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
7522 ps->pss_func >= POOL_SCAN_FUNCS) {
7523 (void) printf(gettext("none requested\n"));
7524 return;
7525 }
7526
7527 start = ps->pss_start_time;
7528 end = ps->pss_end_time;
7529 pause = ps->pss_pass_scrub_pause;
7530
7531 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
7532
7533 assert(ps->pss_func == POOL_SCAN_SCRUB ||
7534 ps->pss_func == POOL_SCAN_RESILVER);
7535
7536 /* Scan is finished or canceled. */
7537 if (ps->pss_state == DSS_FINISHED) {
7538 secs_to_dhms(end - start, time_buf);
7539
7540 if (ps->pss_func == POOL_SCAN_SCRUB) {
7541 (void) printf(gettext("scrub repaired %s "
7542 "in %s with %llu errors on %s"), processed_buf,
7543 time_buf, (u_longlong_t)ps->pss_errors,
7544 ctime(&end));
7545 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
7546 (void) printf(gettext("resilvered %s "
7547 "in %s with %llu errors on %s"), processed_buf,
7548 time_buf, (u_longlong_t)ps->pss_errors,
7549 ctime(&end));
7550 }
7551 return;
7552 } else if (ps->pss_state == DSS_CANCELED) {
7553 if (ps->pss_func == POOL_SCAN_SCRUB) {
7554 (void) printf(gettext("scrub canceled on %s"),
7555 ctime(&end));
7556 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
7557 (void) printf(gettext("resilver canceled on %s"),
7558 ctime(&end));
7559 }
7560 return;
7561 }
7562
7563 assert(ps->pss_state == DSS_SCANNING);
7564
7565 /* Scan is in progress. Resilvers can't be paused. */
7566 if (ps->pss_func == POOL_SCAN_SCRUB) {
7567 if (pause == 0) {
7568 (void) printf(gettext("scrub in progress since %s"),
7569 ctime(&start));
7570 } else {
7571 (void) printf(gettext("scrub paused since %s"),
7572 ctime(&pause));
7573 (void) printf(gettext("\tscrub started on %s"),
7574 ctime(&start));
7575 }
7576 } else if (ps->pss_func == POOL_SCAN_RESILVER) {
7577 (void) printf(gettext("resilver in progress since %s"),
7578 ctime(&start));
7579 }
7580
7581 scanned = ps->pss_examined;
7582 pass_scanned = ps->pss_pass_exam;
7583 issued = ps->pss_issued;
7584 pass_issued = ps->pss_pass_issued;
7585 total = ps->pss_to_examine;
7586
7587 /* we are only done with a block once we have issued the IO for it */
7588 fraction_done = (double)issued / total;
7589
7590 /* elapsed time for this pass, rounding up to 1 if it's 0 */
7591 elapsed = time(NULL) - ps->pss_pass_start;
7592 elapsed -= ps->pss_pass_scrub_spent_paused;
7593 elapsed = (elapsed != 0) ? elapsed : 1;
7594
7595 scan_rate = pass_scanned / elapsed;
7596 issue_rate = pass_issued / elapsed;
7597 uint64_t total_secs_left = (issue_rate != 0 && total >= issued) ?
7598 ((total - issued) / issue_rate) : UINT64_MAX;
7599 secs_to_dhms(total_secs_left, time_buf);
7600
7601 /* format all of the numbers we will be reporting */
7602 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
7603 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
7604 zfs_nicebytes(total, total_buf, sizeof (total_buf));
7605 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
7606 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
7607
7608 /* do not print estimated time if we have a paused scrub */
7609 if (pause == 0) {
7610 (void) printf(gettext("\t%s scanned at %s/s, "
7611 "%s issued at %s/s, %s total\n"),
7612 scanned_buf, srate_buf, issued_buf, irate_buf, total_buf);
7613 } else {
7614 (void) printf(gettext("\t%s scanned, %s issued, %s total\n"),
7615 scanned_buf, issued_buf, total_buf);
7616 }
7617
7618 if (ps->pss_func == POOL_SCAN_RESILVER) {
7619 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7620 processed_buf, 100 * fraction_done);
7621 } else if (ps->pss_func == POOL_SCAN_SCRUB) {
7622 (void) printf(gettext("\t%s repaired, %.2f%% done"),
7623 processed_buf, 100 * fraction_done);
7624 }
7625
7626 if (pause == 0) {
7627 if (total_secs_left != UINT64_MAX &&
7628 issue_rate >= 10 * 1024 * 1024) {
7629 (void) printf(gettext(", %s to go\n"), time_buf);
7630 } else {
7631 (void) printf(gettext(", no estimated "
7632 "completion time\n"));
7633 }
7634 } else {
7635 (void) printf(gettext("\n"));
7636 }
7637 }
7638
7639 static void
7640 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, char *vdev_name)
7641 {
7642 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE)
7643 return;
7644
7645 printf(" ");
7646 printf_color(ANSI_BOLD, gettext("scan:"));
7647 printf(" ");
7648
7649 uint64_t bytes_scanned = vrs->vrs_bytes_scanned;
7650 uint64_t bytes_issued = vrs->vrs_bytes_issued;
7651 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt;
7652 uint64_t bytes_est = vrs->vrs_bytes_est;
7653 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned /
7654 (vrs->vrs_pass_time_ms + 1)) * 1000;
7655 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued /
7656 (vrs->vrs_pass_time_ms + 1)) * 1000;
7657 double scan_pct = MIN((double)bytes_scanned * 100 /
7658 (bytes_est + 1), 100);
7659
7660 /* Format all of the numbers we will be reporting */
7661 char bytes_scanned_buf[7], bytes_issued_buf[7];
7662 char bytes_rebuilt_buf[7], bytes_est_buf[7];
7663 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32];
7664 zfs_nicebytes(bytes_scanned, bytes_scanned_buf,
7665 sizeof (bytes_scanned_buf));
7666 zfs_nicebytes(bytes_issued, bytes_issued_buf,
7667 sizeof (bytes_issued_buf));
7668 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf,
7669 sizeof (bytes_rebuilt_buf));
7670 zfs_nicebytes(bytes_est, bytes_est_buf, sizeof (bytes_est_buf));
7671 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf));
7672 zfs_nicebytes(issue_rate, issue_rate_buf, sizeof (issue_rate_buf));
7673
7674 time_t start = vrs->vrs_start_time;
7675 time_t end = vrs->vrs_end_time;
7676
7677 /* Rebuild is finished or canceled. */
7678 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) {
7679 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf);
7680 (void) printf(gettext("resilvered (%s) %s in %s "
7681 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf,
7682 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end));
7683 return;
7684 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) {
7685 (void) printf(gettext("resilver (%s) canceled on %s"),
7686 vdev_name, ctime(&end));
7687 return;
7688 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7689 (void) printf(gettext("resilver (%s) in progress since %s"),
7690 vdev_name, ctime(&start));
7691 }
7692
7693 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE);
7694
7695 secs_to_dhms(MAX((int64_t)bytes_est - (int64_t)bytes_scanned, 0) /
7696 MAX(scan_rate, 1), time_buf);
7697
7698 (void) printf(gettext("\t%s scanned at %s/s, %s issued %s/s, "
7699 "%s total\n"), bytes_scanned_buf, scan_rate_buf,
7700 bytes_issued_buf, issue_rate_buf, bytes_est_buf);
7701 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
7702 bytes_rebuilt_buf, scan_pct);
7703
7704 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7705 if (scan_rate >= 10 * 1024 * 1024) {
7706 (void) printf(gettext(", %s to go\n"), time_buf);
7707 } else {
7708 (void) printf(gettext(", no estimated "
7709 "completion time\n"));
7710 }
7711 } else {
7712 (void) printf(gettext("\n"));
7713 }
7714 }
7715
7716 /*
7717 * Print rebuild status for top-level vdevs.
7718 */
7719 static void
7720 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot)
7721 {
7722 nvlist_t **child;
7723 uint_t children;
7724
7725 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7726 &child, &children) != 0)
7727 children = 0;
7728
7729 for (uint_t c = 0; c < children; c++) {
7730 vdev_rebuild_stat_t *vrs;
7731 uint_t i;
7732
7733 if (nvlist_lookup_uint64_array(child[c],
7734 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
7735 char *name = zpool_vdev_name(g_zfs, zhp,
7736 child[c], VDEV_NAME_TYPE_ID);
7737 print_rebuild_status_impl(vrs, name);
7738 free(name);
7739 }
7740 }
7741 }
7742
7743 /*
7744 * As we don't scrub checkpointed blocks, we want to warn the user that we
7745 * skipped scanning some blocks if a checkpoint exists or existed at any
7746 * time during the scan. If a sequential instead of healing reconstruction
7747 * was performed then the blocks were reconstructed. However, their checksums
7748 * have not been verified so we still print the warning.
7749 */
7750 static void
7751 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
7752 {
7753 if (ps == NULL || pcs == NULL)
7754 return;
7755
7756 if (pcs->pcs_state == CS_NONE ||
7757 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
7758 return;
7759
7760 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
7761
7762 if (ps->pss_state == DSS_NONE)
7763 return;
7764
7765 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
7766 ps->pss_end_time < pcs->pcs_start_time)
7767 return;
7768
7769 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
7770 (void) printf(gettext(" scan warning: skipped blocks "
7771 "that are only referenced by the checkpoint.\n"));
7772 } else {
7773 assert(ps->pss_state == DSS_SCANNING);
7774 (void) printf(gettext(" scan warning: skipping blocks "
7775 "that are only referenced by the checkpoint.\n"));
7776 }
7777 }
7778
7779 /*
7780 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
7781 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
7782 * the last completed (or cancelled) rebuild.
7783 */
7784 static boolean_t
7785 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time)
7786 {
7787 nvlist_t **child;
7788 uint_t children;
7789 boolean_t rebuilding = B_FALSE;
7790 uint64_t end_time = 0;
7791
7792 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7793 &child, &children) != 0)
7794 children = 0;
7795
7796 for (uint_t c = 0; c < children; c++) {
7797 vdev_rebuild_stat_t *vrs;
7798 uint_t i;
7799
7800 if (nvlist_lookup_uint64_array(child[c],
7801 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
7802
7803 if (vrs->vrs_end_time > end_time)
7804 end_time = vrs->vrs_end_time;
7805
7806 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
7807 rebuilding = B_TRUE;
7808 end_time = 0;
7809 break;
7810 }
7811 }
7812 }
7813
7814 if (rebuild_end_time != NULL)
7815 *rebuild_end_time = end_time;
7816
7817 return (rebuilding);
7818 }
7819
7820 /*
7821 * Print the scan status.
7822 */
7823 static void
7824 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot)
7825 {
7826 uint64_t rebuild_end_time = 0, resilver_end_time = 0;
7827 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE;
7828 boolean_t active_resilver = B_FALSE;
7829 pool_checkpoint_stat_t *pcs = NULL;
7830 pool_scan_stat_t *ps = NULL;
7831 uint_t c;
7832
7833 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
7834 (uint64_t **)&ps, &c) == 0) {
7835 if (ps->pss_func == POOL_SCAN_RESILVER) {
7836 resilver_end_time = ps->pss_end_time;
7837 active_resilver = (ps->pss_state == DSS_SCANNING);
7838 }
7839
7840 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER);
7841 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB);
7842 }
7843
7844 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time);
7845 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0));
7846
7847 /* Always print the scrub status when available. */
7848 if (have_scrub)
7849 print_scan_scrub_resilver_status(ps);
7850
7851 /*
7852 * When there is an active resilver or rebuild print its status.
7853 * Otherwise print the status of the last resilver or rebuild.
7854 */
7855 if (active_resilver || (!active_rebuild && have_resilver &&
7856 resilver_end_time && resilver_end_time > rebuild_end_time)) {
7857 print_scan_scrub_resilver_status(ps);
7858 } else if (active_rebuild || (!active_resilver && have_rebuild &&
7859 rebuild_end_time && rebuild_end_time > resilver_end_time)) {
7860 print_rebuild_status(zhp, nvroot);
7861 }
7862
7863 (void) nvlist_lookup_uint64_array(nvroot,
7864 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
7865 print_checkpoint_scan_warning(ps, pcs);
7866 }
7867
7868 /*
7869 * Print out detailed removal status.
7870 */
7871 static void
7872 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
7873 {
7874 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
7875 time_t start, end;
7876 nvlist_t *config, *nvroot;
7877 nvlist_t **child;
7878 uint_t children;
7879 char *vdev_name;
7880
7881 if (prs == NULL || prs->prs_state == DSS_NONE)
7882 return;
7883
7884 /*
7885 * Determine name of vdev.
7886 */
7887 config = zpool_get_config(zhp, NULL);
7888 nvroot = fnvlist_lookup_nvlist(config,
7889 ZPOOL_CONFIG_VDEV_TREE);
7890 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7891 &child, &children) == 0);
7892 assert(prs->prs_removing_vdev < children);
7893 vdev_name = zpool_vdev_name(g_zfs, zhp,
7894 child[prs->prs_removing_vdev], B_TRUE);
7895
7896 printf_color(ANSI_BOLD, gettext("remove: "));
7897
7898 start = prs->prs_start_time;
7899 end = prs->prs_end_time;
7900 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
7901
7902 /*
7903 * Removal is finished or canceled.
7904 */
7905 if (prs->prs_state == DSS_FINISHED) {
7906 uint64_t minutes_taken = (end - start) / 60;
7907
7908 (void) printf(gettext("Removal of vdev %llu copied %s "
7909 "in %lluh%um, completed on %s"),
7910 (longlong_t)prs->prs_removing_vdev,
7911 copied_buf,
7912 (u_longlong_t)(minutes_taken / 60),
7913 (uint_t)(minutes_taken % 60),
7914 ctime((time_t *)&end));
7915 } else if (prs->prs_state == DSS_CANCELED) {
7916 (void) printf(gettext("Removal of %s canceled on %s"),
7917 vdev_name, ctime(&end));
7918 } else {
7919 uint64_t copied, total, elapsed, mins_left, hours_left;
7920 double fraction_done;
7921 uint_t rate;
7922
7923 assert(prs->prs_state == DSS_SCANNING);
7924
7925 /*
7926 * Removal is in progress.
7927 */
7928 (void) printf(gettext(
7929 "Evacuation of %s in progress since %s"),
7930 vdev_name, ctime(&start));
7931
7932 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
7933 total = prs->prs_to_copy;
7934 fraction_done = (double)copied / total;
7935
7936 /* elapsed time for this pass */
7937 elapsed = time(NULL) - prs->prs_start_time;
7938 elapsed = elapsed > 0 ? elapsed : 1;
7939 rate = copied / elapsed;
7940 rate = rate > 0 ? rate : 1;
7941 mins_left = ((total - copied) / rate) / 60;
7942 hours_left = mins_left / 60;
7943
7944 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
7945 zfs_nicenum(total, total_buf, sizeof (total_buf));
7946 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
7947
7948 /*
7949 * do not print estimated time if hours_left is more than
7950 * 30 days
7951 */
7952 (void) printf(gettext(
7953 "\t%s copied out of %s at %s/s, %.2f%% done"),
7954 examined_buf, total_buf, rate_buf, 100 * fraction_done);
7955 if (hours_left < (30 * 24)) {
7956 (void) printf(gettext(", %lluh%um to go\n"),
7957 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
7958 } else {
7959 (void) printf(gettext(
7960 ", (copy is slow, no estimated time)\n"));
7961 }
7962 }
7963 free(vdev_name);
7964
7965 if (prs->prs_mapping_memory > 0) {
7966 char mem_buf[7];
7967 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
7968 (void) printf(gettext(
7969 "\t%s memory used for removed device mappings\n"),
7970 mem_buf);
7971 }
7972 }
7973
7974 static void
7975 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
7976 {
7977 time_t start;
7978 char space_buf[7];
7979
7980 if (pcs == NULL || pcs->pcs_state == CS_NONE)
7981 return;
7982
7983 (void) printf(gettext("checkpoint: "));
7984
7985 start = pcs->pcs_start_time;
7986 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
7987
7988 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
7989 char *date = ctime(&start);
7990
7991 /*
7992 * ctime() adds a newline at the end of the generated
7993 * string, thus the weird format specifier and the
7994 * strlen() call used to chop it off from the output.
7995 */
7996 (void) printf(gettext("created %.*s, consumes %s\n"),
7997 (int)(strlen(date) - 1), date, space_buf);
7998 return;
7999 }
8000
8001 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
8002
8003 (void) printf(gettext("discarding, %s remaining.\n"),
8004 space_buf);
8005 }
8006
8007 static void
8008 print_error_log(zpool_handle_t *zhp)
8009 {
8010 nvlist_t *nverrlist = NULL;
8011 nvpair_t *elem;
8012 char *pathname;
8013 size_t len = MAXPATHLEN * 2;
8014
8015 if (zpool_get_errlog(zhp, &nverrlist) != 0)
8016 return;
8017
8018 (void) printf("errors: Permanent errors have been "
8019 "detected in the following files:\n\n");
8020
8021 pathname = safe_malloc(len);
8022 elem = NULL;
8023 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
8024 nvlist_t *nv;
8025 uint64_t dsobj, obj;
8026
8027 verify(nvpair_value_nvlist(elem, &nv) == 0);
8028 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
8029 &dsobj) == 0);
8030 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
8031 &obj) == 0);
8032 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
8033 (void) printf("%7s %s\n", "", pathname);
8034 }
8035 free(pathname);
8036 nvlist_free(nverrlist);
8037 }
8038
8039 static void
8040 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
8041 uint_t nspares)
8042 {
8043 uint_t i;
8044 char *name;
8045
8046 if (nspares == 0)
8047 return;
8048
8049 (void) printf(gettext("\tspares\n"));
8050
8051 for (i = 0; i < nspares; i++) {
8052 name = zpool_vdev_name(g_zfs, zhp, spares[i],
8053 cb->cb_name_flags);
8054 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL);
8055 free(name);
8056 }
8057 }
8058
8059 static void
8060 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
8061 uint_t nl2cache)
8062 {
8063 uint_t i;
8064 char *name;
8065
8066 if (nl2cache == 0)
8067 return;
8068
8069 (void) printf(gettext("\tcache\n"));
8070
8071 for (i = 0; i < nl2cache; i++) {
8072 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
8073 cb->cb_name_flags);
8074 print_status_config(zhp, cb, name, l2cache[i], 2,
8075 B_FALSE, NULL);
8076 free(name);
8077 }
8078 }
8079
8080 static void
8081 print_dedup_stats(nvlist_t *config)
8082 {
8083 ddt_histogram_t *ddh;
8084 ddt_stat_t *dds;
8085 ddt_object_t *ddo;
8086 uint_t c;
8087 char dspace[6], mspace[6];
8088
8089 /*
8090 * If the pool was faulted then we may not have been able to
8091 * obtain the config. Otherwise, if we have anything in the dedup
8092 * table continue processing the stats.
8093 */
8094 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
8095 (uint64_t **)&ddo, &c) != 0)
8096 return;
8097
8098 (void) printf("\n");
8099 (void) printf(gettext(" dedup: "));
8100 if (ddo->ddo_count == 0) {
8101 (void) printf(gettext("no DDT entries\n"));
8102 return;
8103 }
8104
8105 zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
8106 zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
8107 (void) printf("DDT entries %llu, size %s on disk, %s in core\n",
8108 (u_longlong_t)ddo->ddo_count,
8109 dspace,
8110 mspace);
8111
8112 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
8113 (uint64_t **)&dds, &c) == 0);
8114 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
8115 (uint64_t **)&ddh, &c) == 0);
8116 zpool_dump_ddt(dds, ddh);
8117 }
8118
8119 /*
8120 * Display a summary of pool status. Displays a summary such as:
8121 *
8122 * pool: tank
8123 * status: DEGRADED
8124 * reason: One or more devices ...
8125 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
8126 * config:
8127 * mirror DEGRADED
8128 * c1t0d0 OK
8129 * c2t0d0 UNAVAIL
8130 *
8131 * When given the '-v' option, we print out the complete config. If the '-e'
8132 * option is specified, then we print out error rate information as well.
8133 */
8134 static int
8135 status_callback(zpool_handle_t *zhp, void *data)
8136 {
8137 status_cbdata_t *cbp = data;
8138 nvlist_t *config, *nvroot;
8139 const char *msgid;
8140 zpool_status_t reason;
8141 zpool_errata_t errata;
8142 const char *health;
8143 uint_t c;
8144 vdev_stat_t *vs;
8145
8146 config = zpool_get_config(zhp, NULL);
8147 reason = zpool_get_status(zhp, &msgid, &errata);
8148
8149 cbp->cb_count++;
8150
8151 /*
8152 * If we were given 'zpool status -x', only report those pools with
8153 * problems.
8154 */
8155 if (cbp->cb_explain &&
8156 (reason == ZPOOL_STATUS_OK ||
8157 reason == ZPOOL_STATUS_VERSION_OLDER ||
8158 reason == ZPOOL_STATUS_FEAT_DISABLED ||
8159 reason == ZPOOL_STATUS_COMPATIBILITY_ERR ||
8160 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) {
8161 if (!cbp->cb_allpools) {
8162 (void) printf(gettext("pool '%s' is healthy\n"),
8163 zpool_get_name(zhp));
8164 if (cbp->cb_first)
8165 cbp->cb_first = B_FALSE;
8166 }
8167 return (0);
8168 }
8169
8170 if (cbp->cb_first)
8171 cbp->cb_first = B_FALSE;
8172 else
8173 (void) printf("\n");
8174
8175 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
8176 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
8177 (uint64_t **)&vs, &c) == 0);
8178
8179 health = zpool_get_state_str(zhp);
8180
8181 printf(" ");
8182 printf_color(ANSI_BOLD, gettext("pool:"));
8183 printf(" %s\n", zpool_get_name(zhp));
8184 fputc(' ', stdout);
8185 printf_color(ANSI_BOLD, gettext("state: "));
8186
8187 printf_color(health_str_to_color(health), "%s", health);
8188
8189 fputc('\n', stdout);
8190
8191 switch (reason) {
8192 case ZPOOL_STATUS_MISSING_DEV_R:
8193 printf_color(ANSI_BOLD, gettext("status: "));
8194 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8195 "not be opened. Sufficient replicas exist for\n\tthe pool "
8196 "to continue functioning in a degraded state.\n"));
8197 printf_color(ANSI_BOLD, gettext("action: "));
8198 printf_color(ANSI_YELLOW, gettext("Attach the missing device "
8199 "and online it using 'zpool online'.\n"));
8200 break;
8201
8202 case ZPOOL_STATUS_MISSING_DEV_NR:
8203 printf_color(ANSI_BOLD, gettext("status: "));
8204 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8205 "not be opened. There are insufficient\n\treplicas for the"
8206 " pool to continue functioning.\n"));
8207 printf_color(ANSI_BOLD, gettext("action: "));
8208 printf_color(ANSI_YELLOW, gettext("Attach the missing device "
8209 "and online it using 'zpool online'.\n"));
8210 break;
8211
8212 case ZPOOL_STATUS_CORRUPT_LABEL_R:
8213 printf_color(ANSI_BOLD, gettext("status: "));
8214 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8215 "not be used because the label is missing or\n\tinvalid. "
8216 "Sufficient replicas exist for the pool to continue\n\t"
8217 "functioning in a degraded state.\n"));
8218 printf_color(ANSI_BOLD, gettext("action: "));
8219 printf_color(ANSI_YELLOW, gettext("Replace the device using "
8220 "'zpool replace'.\n"));
8221 break;
8222
8223 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
8224 printf_color(ANSI_BOLD, gettext("status: "));
8225 printf_color(ANSI_YELLOW, gettext("One or more devices could "
8226 "not be used because the label is missing \n\tor invalid. "
8227 "There are insufficient replicas for the pool to "
8228 "continue\n\tfunctioning.\n"));
8229 zpool_explain_recover(zpool_get_handle(zhp),
8230 zpool_get_name(zhp), reason, config);
8231 break;
8232
8233 case ZPOOL_STATUS_FAILING_DEV:
8234 printf_color(ANSI_BOLD, gettext("status: "));
8235 printf_color(ANSI_YELLOW, gettext("One or more devices has "
8236 "experienced an unrecoverable error. An\n\tattempt was "
8237 "made to correct the error. Applications are "
8238 "unaffected.\n"));
8239 printf_color(ANSI_BOLD, gettext("action: "));
8240 printf_color(ANSI_YELLOW, gettext("Determine if the "
8241 "device needs to be replaced, and clear the errors\n\tusing"
8242 " 'zpool clear' or replace the device with 'zpool "
8243 "replace'.\n"));
8244 break;
8245
8246 case ZPOOL_STATUS_OFFLINE_DEV:
8247 printf_color(ANSI_BOLD, gettext("status: "));
8248 printf_color(ANSI_YELLOW, gettext("One or more devices has "
8249 "been taken offline by the administrator.\n\tSufficient "
8250 "replicas exist for the pool to continue functioning in "
8251 "a\n\tdegraded state.\n"));
8252 printf_color(ANSI_BOLD, gettext("action: "));
8253 printf_color(ANSI_YELLOW, gettext("Online the device "
8254 "using 'zpool online' or replace the device with\n\t'zpool "
8255 "replace'.\n"));
8256 break;
8257
8258 case ZPOOL_STATUS_REMOVED_DEV:
8259 printf_color(ANSI_BOLD, gettext("status: "));
8260 printf_color(ANSI_YELLOW, gettext("One or more devices has "
8261 "been removed by the administrator.\n\tSufficient "
8262 "replicas exist for the pool to continue functioning in "
8263 "a\n\tdegraded state.\n"));
8264 printf_color(ANSI_BOLD, gettext("action: "));
8265 printf_color(ANSI_YELLOW, gettext("Online the device "
8266 "using zpool online' or replace the device with\n\t'zpool "
8267 "replace'.\n"));
8268 break;
8269
8270 case ZPOOL_STATUS_RESILVERING:
8271 case ZPOOL_STATUS_REBUILDING:
8272 printf_color(ANSI_BOLD, gettext("status: "));
8273 printf_color(ANSI_YELLOW, gettext("One or more devices is "
8274 "currently being resilvered. The pool will\n\tcontinue "
8275 "to function, possibly in a degraded state.\n"));
8276 printf_color(ANSI_BOLD, gettext("action: "));
8277 printf_color(ANSI_YELLOW, gettext("Wait for the resilver to "
8278 "complete.\n"));
8279 break;
8280
8281 case ZPOOL_STATUS_REBUILD_SCRUB:
8282 printf_color(ANSI_BOLD, gettext("status: "));
8283 printf_color(ANSI_YELLOW, gettext("One or more devices have "
8284 "been sequentially resilvered, scrubbing\n\tthe pool "
8285 "is recommended.\n"));
8286 printf_color(ANSI_BOLD, gettext("action: "));
8287 printf_color(ANSI_YELLOW, gettext("Use 'zpool scrub' to "
8288 "verify all data checksums.\n"));
8289 break;
8290
8291 case ZPOOL_STATUS_CORRUPT_DATA:
8292 printf_color(ANSI_BOLD, gettext("status: "));
8293 printf_color(ANSI_YELLOW, gettext("One or more devices has "
8294 "experienced an error resulting in data\n\tcorruption. "
8295 "Applications may be affected.\n"));
8296 printf_color(ANSI_BOLD, gettext("action: "));
8297 printf_color(ANSI_YELLOW, gettext("Restore the file in question"
8298 " if possible. Otherwise restore the\n\tentire pool from "
8299 "backup.\n"));
8300 break;
8301
8302 case ZPOOL_STATUS_CORRUPT_POOL:
8303 printf_color(ANSI_BOLD, gettext("status: "));
8304 printf_color(ANSI_YELLOW, gettext("The pool metadata is "
8305 "corrupted and the pool cannot be opened.\n"));
8306 zpool_explain_recover(zpool_get_handle(zhp),
8307 zpool_get_name(zhp), reason, config);
8308 break;
8309
8310 case ZPOOL_STATUS_VERSION_OLDER:
8311 printf_color(ANSI_BOLD, gettext("status: "));
8312 printf_color(ANSI_YELLOW, gettext("The pool is formatted using "
8313 "a legacy on-disk format. The pool can\n\tstill be used, "
8314 "but some features are unavailable.\n"));
8315 printf_color(ANSI_BOLD, gettext("action: "));
8316 printf_color(ANSI_YELLOW, gettext("Upgrade the pool using "
8317 "'zpool upgrade'. Once this is done, the\n\tpool will no "
8318 "longer be accessible on software that does not support\n\t"
8319 "feature flags.\n"));
8320 break;
8321
8322 case ZPOOL_STATUS_VERSION_NEWER:
8323 printf_color(ANSI_BOLD, gettext("status: "));
8324 printf_color(ANSI_YELLOW, gettext("The pool has been upgraded "
8325 "to a newer, incompatible on-disk version.\n\tThe pool "
8326 "cannot be accessed on this system.\n"));
8327 printf_color(ANSI_BOLD, gettext("action: "));
8328 printf_color(ANSI_YELLOW, gettext("Access the pool from a "
8329 "system running more recent software, or\n\trestore the "
8330 "pool from backup.\n"));
8331 break;
8332
8333 case ZPOOL_STATUS_FEAT_DISABLED:
8334 printf_color(ANSI_BOLD, gettext("status: "));
8335 printf_color(ANSI_YELLOW, gettext("Some supported and "
8336 "requested features are not enabled on the pool.\n\t"
8337 "The pool can still be used, but some features are "
8338 "unavailable.\n"));
8339 printf_color(ANSI_BOLD, gettext("action: "));
8340 printf_color(ANSI_YELLOW, gettext("Enable all features using "
8341 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
8342 "longer be accessible by software that does not support\n\t"
8343 "the features. See zpool-features(7) for details.\n"));
8344 break;
8345
8346 case ZPOOL_STATUS_COMPATIBILITY_ERR:
8347 printf_color(ANSI_BOLD, gettext("status: "));
8348 printf_color(ANSI_YELLOW, gettext("This pool has a "
8349 "compatibility list specified, but it could not be\n\t"
8350 "read/parsed at this time. The pool can still be used, "
8351 "but this\n\tshould be investigated.\n"));
8352 printf_color(ANSI_BOLD, gettext("action: "));
8353 printf_color(ANSI_YELLOW, gettext("Check the value of the "
8354 "'compatibility' property against the\n\t"
8355 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or "
8356 ZPOOL_DATA_COMPAT_D ".\n"));
8357 break;
8358
8359 case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
8360 printf_color(ANSI_BOLD, gettext("status: "));
8361 printf_color(ANSI_YELLOW, gettext("One or more features "
8362 "are enabled on the pool despite not being\n\t"
8363 "requested by the 'compatibility' property.\n"));
8364 printf_color(ANSI_BOLD, gettext("action: "));
8365 printf_color(ANSI_YELLOW, gettext("Consider setting "
8366 "'compatibility' to an appropriate value, or\n\t"
8367 "adding needed features to the relevant file in\n\t"
8368 ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n"));
8369 break;
8370
8371 case ZPOOL_STATUS_UNSUP_FEAT_READ:
8372 printf_color(ANSI_BOLD, gettext("status: "));
8373 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed "
8374 "on this system because it uses the\n\tfollowing feature(s)"
8375 " not supported on this system:\n"));
8376 zpool_print_unsup_feat(config);
8377 (void) printf("\n");
8378 printf_color(ANSI_BOLD, gettext("action: "));
8379 printf_color(ANSI_YELLOW, gettext("Access the pool from a "
8380 "system that supports the required feature(s),\n\tor "
8381 "restore the pool from backup.\n"));
8382 break;
8383
8384 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
8385 printf_color(ANSI_BOLD, gettext("status: "));
8386 printf_color(ANSI_YELLOW, gettext("The pool can only be "
8387 "accessed in read-only mode on this system. It\n\tcannot be"
8388 " accessed in read-write mode because it uses the "
8389 "following\n\tfeature(s) not supported on this system:\n"));
8390 zpool_print_unsup_feat(config);
8391 (void) printf("\n");
8392 printf_color(ANSI_BOLD, gettext("action: "));
8393 printf_color(ANSI_YELLOW, gettext("The pool cannot be accessed "
8394 "in read-write mode. Import the pool with\n"
8395 "\t\"-o readonly=on\", access the pool from a system that "
8396 "supports the\n\trequired feature(s), or restore the "
8397 "pool from backup.\n"));
8398 break;
8399
8400 case ZPOOL_STATUS_FAULTED_DEV_R:
8401 printf_color(ANSI_BOLD, gettext("status: "));
8402 printf_color(ANSI_YELLOW, gettext("One or more devices are "
8403 "faulted in response to persistent errors.\n\tSufficient "
8404 "replicas exist for the pool to continue functioning "
8405 "in a\n\tdegraded state.\n"));
8406 printf_color(ANSI_BOLD, gettext("action: "));
8407 printf_color(ANSI_YELLOW, gettext("Replace the faulted device, "
8408 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
8409 break;
8410
8411 case ZPOOL_STATUS_FAULTED_DEV_NR:
8412 printf_color(ANSI_BOLD, gettext("status: "));
8413 printf_color(ANSI_YELLOW, gettext("One or more devices are "
8414 "faulted in response to persistent errors. There are "
8415 "insufficient replicas for the pool to\n\tcontinue "
8416 "functioning.\n"));
8417 printf_color(ANSI_BOLD, gettext("action: "));
8418 printf_color(ANSI_YELLOW, gettext("Destroy and re-create the "
8419 "pool from a backup source. Manually marking the device\n"
8420 "\trepaired using 'zpool clear' may allow some data "
8421 "to be recovered.\n"));
8422 break;
8423
8424 case ZPOOL_STATUS_IO_FAILURE_MMP:
8425 printf_color(ANSI_BOLD, gettext("status: "));
8426 printf_color(ANSI_YELLOW, gettext("The pool is suspended "
8427 "because multihost writes failed or were delayed;\n\t"
8428 "another system could import the pool undetected.\n"));
8429 printf_color(ANSI_BOLD, gettext("action: "));
8430 printf_color(ANSI_YELLOW, gettext("Make sure the pool's devices"
8431 " are connected, then reboot your system and\n\timport the "
8432 "pool.\n"));
8433 break;
8434
8435 case ZPOOL_STATUS_IO_FAILURE_WAIT:
8436 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
8437 printf_color(ANSI_BOLD, gettext("status: "));
8438 printf_color(ANSI_YELLOW, gettext("One or more devices are "
8439 "faulted in response to IO failures.\n"));
8440 printf_color(ANSI_BOLD, gettext("action: "));
8441 printf_color(ANSI_YELLOW, gettext("Make sure the affected "
8442 "devices are connected, then run 'zpool clear'.\n"));
8443 break;
8444
8445 case ZPOOL_STATUS_BAD_LOG:
8446 printf_color(ANSI_BOLD, gettext("status: "));
8447 printf_color(ANSI_YELLOW, gettext("An intent log record "
8448 "could not be read.\n"
8449 "\tWaiting for administrator intervention to fix the "
8450 "faulted pool.\n"));
8451 printf_color(ANSI_BOLD, gettext("action: "));
8452 printf_color(ANSI_YELLOW, gettext("Either restore the affected "
8453 "device(s) and run 'zpool online',\n"
8454 "\tor ignore the intent log records by running "
8455 "'zpool clear'.\n"));
8456 break;
8457
8458 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
8459 (void) printf(gettext("status: One or more devices are "
8460 "configured to use a non-native block size.\n"
8461 "\tExpect reduced performance.\n"));
8462 (void) printf(gettext("action: Replace affected devices with "
8463 "devices that support the\n\tconfigured block size, or "
8464 "migrate data to a properly configured\n\tpool.\n"));
8465 break;
8466
8467 case ZPOOL_STATUS_HOSTID_MISMATCH:
8468 printf_color(ANSI_BOLD, gettext("status: "));
8469 printf_color(ANSI_YELLOW, gettext("Mismatch between pool hostid"
8470 " and system hostid on imported pool.\n\tThis pool was "
8471 "previously imported into a system with a different "
8472 "hostid,\n\tand then was verbatim imported into this "
8473 "system.\n"));
8474 printf_color(ANSI_BOLD, gettext("action: "));
8475 printf_color(ANSI_YELLOW, gettext("Export this pool on all "
8476 "systems on which it is imported.\n"
8477 "\tThen import it to correct the mismatch.\n"));
8478 break;
8479
8480 case ZPOOL_STATUS_ERRATA:
8481 printf_color(ANSI_BOLD, gettext("status: "));
8482 printf_color(ANSI_YELLOW, gettext("Errata #%d detected.\n"),
8483 errata);
8484
8485 switch (errata) {
8486 case ZPOOL_ERRATA_NONE:
8487 break;
8488
8489 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
8490 printf_color(ANSI_BOLD, gettext("action: "));
8491 printf_color(ANSI_YELLOW, gettext("To correct the issue"
8492 " run 'zpool scrub'.\n"));
8493 break;
8494
8495 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
8496 (void) printf(gettext("\tExisting encrypted datasets "
8497 "contain an on-disk incompatibility\n\twhich "
8498 "needs to be corrected.\n"));
8499 printf_color(ANSI_BOLD, gettext("action: "));
8500 printf_color(ANSI_YELLOW, gettext("To correct the issue"
8501 " backup existing encrypted datasets to new\n\t"
8502 "encrypted datasets and destroy the old ones. "
8503 "'zfs mount -o ro' can\n\tbe used to temporarily "
8504 "mount existing encrypted datasets readonly.\n"));
8505 break;
8506
8507 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
8508 (void) printf(gettext("\tExisting encrypted snapshots "
8509 "and bookmarks contain an on-disk\n\tincompat"
8510 "ibility. This may cause on-disk corruption if "
8511 "they are used\n\twith 'zfs recv'.\n"));
8512 printf_color(ANSI_BOLD, gettext("action: "));
8513 printf_color(ANSI_YELLOW, gettext("To correct the"
8514 "issue, enable the bookmark_v2 feature. No "
8515 "additional\n\taction is needed if there are no "
8516 "encrypted snapshots or bookmarks.\n\tIf preserving"
8517 "the encrypted snapshots and bookmarks is required,"
8518 " use\n\ta non-raw send to backup and restore them."
8519 " Alternately, they may be\n\tremoved to resolve "
8520 "the incompatibility.\n"));
8521 break;
8522
8523 default:
8524 /*
8525 * All errata which allow the pool to be imported
8526 * must contain an action message.
8527 */
8528 assert(0);
8529 }
8530 break;
8531
8532 default:
8533 /*
8534 * The remaining errors can't actually be generated, yet.
8535 */
8536 assert(reason == ZPOOL_STATUS_OK);
8537 }
8538
8539 if (msgid != NULL) {
8540 printf(" ");
8541 printf_color(ANSI_BOLD, gettext("see:"));
8542 printf(gettext(
8543 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
8544 msgid);
8545 }
8546
8547 if (config != NULL) {
8548 uint64_t nerr;
8549 nvlist_t **spares, **l2cache;
8550 uint_t nspares, nl2cache;
8551 pool_checkpoint_stat_t *pcs = NULL;
8552 pool_removal_stat_t *prs = NULL;
8553
8554 print_scan_status(zhp, nvroot);
8555
8556 (void) nvlist_lookup_uint64_array(nvroot,
8557 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
8558 print_removal_status(zhp, prs);
8559
8560 (void) nvlist_lookup_uint64_array(nvroot,
8561 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
8562 print_checkpoint_status(pcs);
8563
8564 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
8565 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
8566 if (cbp->cb_namewidth < 10)
8567 cbp->cb_namewidth = 10;
8568
8569 color_start(ANSI_BOLD);
8570 (void) printf(gettext("config:\n\n"));
8571 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
8572 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
8573 "CKSUM");
8574 color_end();
8575
8576 if (cbp->cb_print_slow_ios) {
8577 printf_color(ANSI_BOLD, " %5s", gettext("SLOW"));
8578 }
8579
8580 if (cbp->vcdl != NULL)
8581 print_cmd_columns(cbp->vcdl, 0);
8582
8583 printf("\n");
8584
8585 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
8586 B_FALSE, NULL);
8587
8588 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
8589 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
8590 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
8591
8592 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
8593 &l2cache, &nl2cache) == 0)
8594 print_l2cache(zhp, cbp, l2cache, nl2cache);
8595
8596 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
8597 &spares, &nspares) == 0)
8598 print_spares(zhp, cbp, spares, nspares);
8599
8600 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
8601 &nerr) == 0) {
8602 nvlist_t *nverrlist = NULL;
8603
8604 /*
8605 * If the approximate error count is small, get a
8606 * precise count by fetching the entire log and
8607 * uniquifying the results.
8608 */
8609 if (nerr > 0 && nerr < 100 && !cbp->cb_verbose &&
8610 zpool_get_errlog(zhp, &nverrlist) == 0) {
8611 nvpair_t *elem;
8612
8613 elem = NULL;
8614 nerr = 0;
8615 while ((elem = nvlist_next_nvpair(nverrlist,
8616 elem)) != NULL) {
8617 nerr++;
8618 }
8619 }
8620 nvlist_free(nverrlist);
8621
8622 (void) printf("\n");
8623
8624 if (nerr == 0)
8625 (void) printf(gettext("errors: No known data "
8626 "errors\n"));
8627 else if (!cbp->cb_verbose)
8628 (void) printf(gettext("errors: %llu data "
8629 "errors, use '-v' for a list\n"),
8630 (u_longlong_t)nerr);
8631 else
8632 print_error_log(zhp);
8633 }
8634
8635 if (cbp->cb_dedup_stats)
8636 print_dedup_stats(config);
8637 } else {
8638 (void) printf(gettext("config: The configuration cannot be "
8639 "determined.\n"));
8640 }
8641
8642 return (0);
8643 }
8644
8645 /*
8646 * zpool status [-c [script1,script2,...]] [-igLpPstvx] [-T d|u] [pool] ...
8647 * [interval [count]]
8648 *
8649 * -c CMD For each vdev, run command CMD
8650 * -i Display vdev initialization status.
8651 * -g Display guid for individual vdev name.
8652 * -L Follow links when resolving vdev path name.
8653 * -p Display values in parsable (exact) format.
8654 * -P Display full path for vdev name.
8655 * -s Display slow IOs column.
8656 * -v Display complete error logs
8657 * -x Display only pools with potential problems
8658 * -D Display dedup status (undocumented)
8659 * -t Display vdev TRIM status.
8660 * -T Display a timestamp in date(1) or Unix format
8661 *
8662 * Describes the health status of all pools or some subset.
8663 */
8664 int
8665 zpool_do_status(int argc, char **argv)
8666 {
8667 int c;
8668 int ret;
8669 float interval = 0;
8670 unsigned long count = 0;
8671 status_cbdata_t cb = { 0 };
8672 char *cmd = NULL;
8673
8674 /* check options */
8675 while ((c = getopt(argc, argv, "c:igLpPsvxDtT:")) != -1) {
8676 switch (c) {
8677 case 'c':
8678 if (cmd != NULL) {
8679 fprintf(stderr,
8680 gettext("Can't set -c flag twice\n"));
8681 exit(1);
8682 }
8683
8684 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
8685 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
8686 fprintf(stderr, gettext(
8687 "Can't run -c, disabled by "
8688 "ZPOOL_SCRIPTS_ENABLED.\n"));
8689 exit(1);
8690 }
8691
8692 if ((getuid() <= 0 || geteuid() <= 0) &&
8693 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
8694 fprintf(stderr, gettext(
8695 "Can't run -c with root privileges "
8696 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
8697 exit(1);
8698 }
8699 cmd = optarg;
8700 break;
8701 case 'i':
8702 cb.cb_print_vdev_init = B_TRUE;
8703 break;
8704 case 'g':
8705 cb.cb_name_flags |= VDEV_NAME_GUID;
8706 break;
8707 case 'L':
8708 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
8709 break;
8710 case 'p':
8711 cb.cb_literal = B_TRUE;
8712 break;
8713 case 'P':
8714 cb.cb_name_flags |= VDEV_NAME_PATH;
8715 break;
8716 case 's':
8717 cb.cb_print_slow_ios = B_TRUE;
8718 break;
8719 case 'v':
8720 cb.cb_verbose = B_TRUE;
8721 break;
8722 case 'x':
8723 cb.cb_explain = B_TRUE;
8724 break;
8725 case 'D':
8726 cb.cb_dedup_stats = B_TRUE;
8727 break;
8728 case 't':
8729 cb.cb_print_vdev_trim = B_TRUE;
8730 break;
8731 case 'T':
8732 get_timestamp_arg(*optarg);
8733 break;
8734 case '?':
8735 if (optopt == 'c') {
8736 print_zpool_script_list("status");
8737 exit(0);
8738 } else {
8739 fprintf(stderr,
8740 gettext("invalid option '%c'\n"), optopt);
8741 }
8742 usage(B_FALSE);
8743 }
8744 }
8745
8746 argc -= optind;
8747 argv += optind;
8748
8749 get_interval_count(&argc, argv, &interval, &count);
8750
8751 if (argc == 0)
8752 cb.cb_allpools = B_TRUE;
8753
8754 cb.cb_first = B_TRUE;
8755 cb.cb_print_status = B_TRUE;
8756
8757 for (;;) {
8758 if (timestamp_fmt != NODATE)
8759 print_timestamp(timestamp_fmt);
8760
8761 if (cmd != NULL)
8762 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
8763 NULL, NULL, 0, 0);
8764
8765 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8766 cb.cb_literal, status_callback, &cb);
8767
8768 if (cb.vcdl != NULL)
8769 free_vdev_cmd_data_list(cb.vcdl);
8770
8771 if (argc == 0 && cb.cb_count == 0)
8772 (void) fprintf(stderr, gettext("no pools available\n"));
8773 else if (cb.cb_explain && cb.cb_first && cb.cb_allpools)
8774 (void) printf(gettext("all pools are healthy\n"));
8775
8776 if (ret != 0)
8777 return (ret);
8778
8779 if (interval == 0)
8780 break;
8781
8782 if (count != 0 && --count == 0)
8783 break;
8784
8785 (void) fsleep(interval);
8786 }
8787
8788 return (0);
8789 }
8790
8791 typedef struct upgrade_cbdata {
8792 int cb_first;
8793 int cb_argc;
8794 uint64_t cb_version;
8795 char **cb_argv;
8796 } upgrade_cbdata_t;
8797
8798 static int
8799 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
8800 {
8801 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
8802 int *count = (int *)unsupp_fs;
8803
8804 if (zfs_version > ZPL_VERSION) {
8805 (void) printf(gettext("%s (v%d) is not supported by this "
8806 "implementation of ZFS.\n"),
8807 zfs_get_name(zhp), zfs_version);
8808 (*count)++;
8809 }
8810
8811 zfs_iter_filesystems(zhp, check_unsupp_fs, unsupp_fs);
8812
8813 zfs_close(zhp);
8814
8815 return (0);
8816 }
8817
8818 static int
8819 upgrade_version(zpool_handle_t *zhp, uint64_t version)
8820 {
8821 int ret;
8822 nvlist_t *config;
8823 uint64_t oldversion;
8824 int unsupp_fs = 0;
8825
8826 config = zpool_get_config(zhp, NULL);
8827 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8828 &oldversion) == 0);
8829
8830 char compat[ZFS_MAXPROPLEN];
8831 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
8832 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
8833 compat[0] = '\0';
8834
8835 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
8836 assert(oldversion < version);
8837
8838 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
8839 if (ret != 0)
8840 return (ret);
8841
8842 if (unsupp_fs) {
8843 (void) fprintf(stderr, gettext("Upgrade not performed due "
8844 "to %d unsupported filesystems (max v%d).\n"),
8845 unsupp_fs, (int)ZPL_VERSION);
8846 return (1);
8847 }
8848
8849 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
8850 (void) fprintf(stderr, gettext("Upgrade not performed because "
8851 "'compatibility' property set to '"
8852 ZPOOL_COMPAT_LEGACY "'.\n"));
8853 return (1);
8854 }
8855
8856 ret = zpool_upgrade(zhp, version);
8857 if (ret != 0)
8858 return (ret);
8859
8860 if (version >= SPA_VERSION_FEATURES) {
8861 (void) printf(gettext("Successfully upgraded "
8862 "'%s' from version %llu to feature flags.\n"),
8863 zpool_get_name(zhp), (u_longlong_t)oldversion);
8864 } else {
8865 (void) printf(gettext("Successfully upgraded "
8866 "'%s' from version %llu to version %llu.\n"),
8867 zpool_get_name(zhp), (u_longlong_t)oldversion,
8868 (u_longlong_t)version);
8869 }
8870
8871 return (0);
8872 }
8873
8874 static int
8875 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
8876 {
8877 int i, ret, count;
8878 boolean_t firstff = B_TRUE;
8879 nvlist_t *enabled = zpool_get_features(zhp);
8880
8881 char compat[ZFS_MAXPROPLEN];
8882 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
8883 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
8884 compat[0] = '\0';
8885
8886 boolean_t requested_features[SPA_FEATURES];
8887 if (zpool_do_load_compat(compat, requested_features) !=
8888 ZPOOL_COMPATIBILITY_OK)
8889 return (-1);
8890
8891 count = 0;
8892 for (i = 0; i < SPA_FEATURES; i++) {
8893 const char *fname = spa_feature_table[i].fi_uname;
8894 const char *fguid = spa_feature_table[i].fi_guid;
8895
8896 if (!spa_feature_table[i].fi_zfs_mod_supported)
8897 continue;
8898
8899 if (!nvlist_exists(enabled, fguid) && requested_features[i]) {
8900 char *propname;
8901 verify(-1 != asprintf(&propname, "feature@%s", fname));
8902 ret = zpool_set_prop(zhp, propname,
8903 ZFS_FEATURE_ENABLED);
8904 if (ret != 0) {
8905 free(propname);
8906 return (ret);
8907 }
8908 count++;
8909
8910 if (firstff) {
8911 (void) printf(gettext("Enabled the "
8912 "following features on '%s':\n"),
8913 zpool_get_name(zhp));
8914 firstff = B_FALSE;
8915 }
8916 (void) printf(gettext(" %s\n"), fname);
8917 free(propname);
8918 }
8919 }
8920
8921 if (countp != NULL)
8922 *countp = count;
8923 return (0);
8924 }
8925
8926 static int
8927 upgrade_cb(zpool_handle_t *zhp, void *arg)
8928 {
8929 upgrade_cbdata_t *cbp = arg;
8930 nvlist_t *config;
8931 uint64_t version;
8932 boolean_t modified_pool = B_FALSE;
8933 int ret;
8934
8935 config = zpool_get_config(zhp, NULL);
8936 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8937 &version) == 0);
8938
8939 assert(SPA_VERSION_IS_SUPPORTED(version));
8940
8941 if (version < cbp->cb_version) {
8942 cbp->cb_first = B_FALSE;
8943 ret = upgrade_version(zhp, cbp->cb_version);
8944 if (ret != 0)
8945 return (ret);
8946 modified_pool = B_TRUE;
8947
8948 /*
8949 * If they did "zpool upgrade -a", then we could
8950 * be doing ioctls to different pools. We need
8951 * to log this history once to each pool, and bypass
8952 * the normal history logging that happens in main().
8953 */
8954 (void) zpool_log_history(g_zfs, history_str);
8955 log_history = B_FALSE;
8956 }
8957
8958 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
8959 int count;
8960 ret = upgrade_enable_all(zhp, &count);
8961 if (ret != 0)
8962 return (ret);
8963
8964 if (count > 0) {
8965 cbp->cb_first = B_FALSE;
8966 modified_pool = B_TRUE;
8967 }
8968 }
8969
8970 if (modified_pool) {
8971 (void) printf("\n");
8972 (void) after_zpool_upgrade(zhp);
8973 }
8974
8975 return (0);
8976 }
8977
8978 static int
8979 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
8980 {
8981 upgrade_cbdata_t *cbp = arg;
8982 nvlist_t *config;
8983 uint64_t version;
8984
8985 config = zpool_get_config(zhp, NULL);
8986 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
8987 &version) == 0);
8988
8989 assert(SPA_VERSION_IS_SUPPORTED(version));
8990
8991 if (version < SPA_VERSION_FEATURES) {
8992 if (cbp->cb_first) {
8993 (void) printf(gettext("The following pools are "
8994 "formatted with legacy version numbers and can\n"
8995 "be upgraded to use feature flags. After "
8996 "being upgraded, these pools\nwill no "
8997 "longer be accessible by software that does not "
8998 "support feature\nflags.\n\n"
8999 "Note that setting a pool's 'compatibility' "
9000 "feature to '" ZPOOL_COMPAT_LEGACY "' will\n"
9001 "inhibit upgrades.\n\n"));
9002 (void) printf(gettext("VER POOL\n"));
9003 (void) printf(gettext("--- ------------\n"));
9004 cbp->cb_first = B_FALSE;
9005 }
9006
9007 (void) printf("%2llu %s\n", (u_longlong_t)version,
9008 zpool_get_name(zhp));
9009 }
9010
9011 return (0);
9012 }
9013
9014 static int
9015 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
9016 {
9017 upgrade_cbdata_t *cbp = arg;
9018 nvlist_t *config;
9019 uint64_t version;
9020
9021 config = zpool_get_config(zhp, NULL);
9022 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
9023 &version) == 0);
9024
9025 if (version >= SPA_VERSION_FEATURES) {
9026 int i;
9027 boolean_t poolfirst = B_TRUE;
9028 nvlist_t *enabled = zpool_get_features(zhp);
9029
9030 for (i = 0; i < SPA_FEATURES; i++) {
9031 const char *fguid = spa_feature_table[i].fi_guid;
9032 const char *fname = spa_feature_table[i].fi_uname;
9033
9034 if (!spa_feature_table[i].fi_zfs_mod_supported)
9035 continue;
9036
9037 if (!nvlist_exists(enabled, fguid)) {
9038 if (cbp->cb_first) {
9039 (void) printf(gettext("\nSome "
9040 "supported features are not "
9041 "enabled on the following pools. "
9042 "Once a\nfeature is enabled the "
9043 "pool may become incompatible with "
9044 "software\nthat does not support "
9045 "the feature. See "
9046 "zpool-features(7) for "
9047 "details.\n\n"
9048 "Note that the pool "
9049 "'compatibility' feature can be "
9050 "used to inhibit\nfeature "
9051 "upgrades.\n\n"));
9052 (void) printf(gettext("POOL "
9053 "FEATURE\n"));
9054 (void) printf(gettext("------"
9055 "---------\n"));
9056 cbp->cb_first = B_FALSE;
9057 }
9058
9059 if (poolfirst) {
9060 (void) printf(gettext("%s\n"),
9061 zpool_get_name(zhp));
9062 poolfirst = B_FALSE;
9063 }
9064
9065 (void) printf(gettext(" %s\n"), fname);
9066 }
9067 /*
9068 * If they did "zpool upgrade -a", then we could
9069 * be doing ioctls to different pools. We need
9070 * to log this history once to each pool, and bypass
9071 * the normal history logging that happens in main().
9072 */
9073 (void) zpool_log_history(g_zfs, history_str);
9074 log_history = B_FALSE;
9075 }
9076 }
9077
9078 return (0);
9079 }
9080
9081 static int
9082 upgrade_one(zpool_handle_t *zhp, void *data)
9083 {
9084 boolean_t modified_pool = B_FALSE;
9085 upgrade_cbdata_t *cbp = data;
9086 uint64_t cur_version;
9087 int ret;
9088
9089 if (strcmp("log", zpool_get_name(zhp)) == 0) {
9090 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
9091 "Pool 'log' must be renamed using export and import"
9092 " to upgrade.\n"));
9093 return (1);
9094 }
9095
9096 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
9097 if (cur_version > cbp->cb_version) {
9098 (void) printf(gettext("Pool '%s' is already formatted "
9099 "using more current version '%llu'.\n\n"),
9100 zpool_get_name(zhp), (u_longlong_t)cur_version);
9101 return (0);
9102 }
9103
9104 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
9105 (void) printf(gettext("Pool '%s' is already formatted "
9106 "using version %llu.\n\n"), zpool_get_name(zhp),
9107 (u_longlong_t)cbp->cb_version);
9108 return (0);
9109 }
9110
9111 if (cur_version != cbp->cb_version) {
9112 modified_pool = B_TRUE;
9113 ret = upgrade_version(zhp, cbp->cb_version);
9114 if (ret != 0)
9115 return (ret);
9116 }
9117
9118 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
9119 int count = 0;
9120 ret = upgrade_enable_all(zhp, &count);
9121 if (ret != 0)
9122 return (ret);
9123
9124 if (count != 0) {
9125 modified_pool = B_TRUE;
9126 } else if (cur_version == SPA_VERSION) {
9127 (void) printf(gettext("Pool '%s' already has all "
9128 "supported and requested features enabled.\n"),
9129 zpool_get_name(zhp));
9130 }
9131 }
9132
9133 if (modified_pool) {
9134 (void) printf("\n");
9135 (void) after_zpool_upgrade(zhp);
9136 }
9137
9138 return (0);
9139 }
9140
9141 /*
9142 * zpool upgrade
9143 * zpool upgrade -v
9144 * zpool upgrade [-V version] <-a | pool ...>
9145 *
9146 * With no arguments, display downrev'd ZFS pool available for upgrade.
9147 * Individual pools can be upgraded by specifying the pool, and '-a' will
9148 * upgrade all pools.
9149 */
9150 int
9151 zpool_do_upgrade(int argc, char **argv)
9152 {
9153 int c;
9154 upgrade_cbdata_t cb = { 0 };
9155 int ret = 0;
9156 boolean_t showversions = B_FALSE;
9157 boolean_t upgradeall = B_FALSE;
9158 char *end;
9159
9160
9161 /* check options */
9162 while ((c = getopt(argc, argv, ":avV:")) != -1) {
9163 switch (c) {
9164 case 'a':
9165 upgradeall = B_TRUE;
9166 break;
9167 case 'v':
9168 showversions = B_TRUE;
9169 break;
9170 case 'V':
9171 cb.cb_version = strtoll(optarg, &end, 10);
9172 if (*end != '\0' ||
9173 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
9174 (void) fprintf(stderr,
9175 gettext("invalid version '%s'\n"), optarg);
9176 usage(B_FALSE);
9177 }
9178 break;
9179 case ':':
9180 (void) fprintf(stderr, gettext("missing argument for "
9181 "'%c' option\n"), optopt);
9182 usage(B_FALSE);
9183 break;
9184 case '?':
9185 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9186 optopt);
9187 usage(B_FALSE);
9188 }
9189 }
9190
9191 cb.cb_argc = argc;
9192 cb.cb_argv = argv;
9193 argc -= optind;
9194 argv += optind;
9195
9196 if (cb.cb_version == 0) {
9197 cb.cb_version = SPA_VERSION;
9198 } else if (!upgradeall && argc == 0) {
9199 (void) fprintf(stderr, gettext("-V option is "
9200 "incompatible with other arguments\n"));
9201 usage(B_FALSE);
9202 }
9203
9204 if (showversions) {
9205 if (upgradeall || argc != 0) {
9206 (void) fprintf(stderr, gettext("-v option is "
9207 "incompatible with other arguments\n"));
9208 usage(B_FALSE);
9209 }
9210 } else if (upgradeall) {
9211 if (argc != 0) {
9212 (void) fprintf(stderr, gettext("-a option should not "
9213 "be used along with a pool name\n"));
9214 usage(B_FALSE);
9215 }
9216 }
9217
9218 (void) printf("%s", gettext("This system supports ZFS pool feature "
9219 "flags.\n\n"));
9220 if (showversions) {
9221 int i;
9222
9223 (void) printf(gettext("The following features are "
9224 "supported:\n\n"));
9225 (void) printf(gettext("FEAT DESCRIPTION\n"));
9226 (void) printf("----------------------------------------------"
9227 "---------------\n");
9228 for (i = 0; i < SPA_FEATURES; i++) {
9229 zfeature_info_t *fi = &spa_feature_table[i];
9230 if (!fi->fi_zfs_mod_supported)
9231 continue;
9232 const char *ro =
9233 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
9234 " (read-only compatible)" : "";
9235
9236 (void) printf("%-37s%s\n", fi->fi_uname, ro);
9237 (void) printf(" %s\n", fi->fi_desc);
9238 }
9239 (void) printf("\n");
9240
9241 (void) printf(gettext("The following legacy versions are also "
9242 "supported:\n\n"));
9243 (void) printf(gettext("VER DESCRIPTION\n"));
9244 (void) printf("--- -----------------------------------------"
9245 "---------------\n");
9246 (void) printf(gettext(" 1 Initial ZFS version\n"));
9247 (void) printf(gettext(" 2 Ditto blocks "
9248 "(replicated metadata)\n"));
9249 (void) printf(gettext(" 3 Hot spares and double parity "
9250 "RAID-Z\n"));
9251 (void) printf(gettext(" 4 zpool history\n"));
9252 (void) printf(gettext(" 5 Compression using the gzip "
9253 "algorithm\n"));
9254 (void) printf(gettext(" 6 bootfs pool property\n"));
9255 (void) printf(gettext(" 7 Separate intent log devices\n"));
9256 (void) printf(gettext(" 8 Delegated administration\n"));
9257 (void) printf(gettext(" 9 refquota and refreservation "
9258 "properties\n"));
9259 (void) printf(gettext(" 10 Cache devices\n"));
9260 (void) printf(gettext(" 11 Improved scrub performance\n"));
9261 (void) printf(gettext(" 12 Snapshot properties\n"));
9262 (void) printf(gettext(" 13 snapused property\n"));
9263 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
9264 (void) printf(gettext(" 15 user/group space accounting\n"));
9265 (void) printf(gettext(" 16 stmf property support\n"));
9266 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
9267 (void) printf(gettext(" 18 Snapshot user holds\n"));
9268 (void) printf(gettext(" 19 Log device removal\n"));
9269 (void) printf(gettext(" 20 Compression using zle "
9270 "(zero-length encoding)\n"));
9271 (void) printf(gettext(" 21 Deduplication\n"));
9272 (void) printf(gettext(" 22 Received properties\n"));
9273 (void) printf(gettext(" 23 Slim ZIL\n"));
9274 (void) printf(gettext(" 24 System attributes\n"));
9275 (void) printf(gettext(" 25 Improved scrub stats\n"));
9276 (void) printf(gettext(" 26 Improved snapshot deletion "
9277 "performance\n"));
9278 (void) printf(gettext(" 27 Improved snapshot creation "
9279 "performance\n"));
9280 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
9281 (void) printf(gettext("\nFor more information on a particular "
9282 "version, including supported releases,\n"));
9283 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
9284 } else if (argc == 0 && upgradeall) {
9285 cb.cb_first = B_TRUE;
9286 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
9287 if (ret == 0 && cb.cb_first) {
9288 if (cb.cb_version == SPA_VERSION) {
9289 (void) printf(gettext("All pools are already "
9290 "formatted using feature flags.\n\n"));
9291 (void) printf(gettext("Every feature flags "
9292 "pool already has all supported and "
9293 "requested features enabled.\n"));
9294 } else {
9295 (void) printf(gettext("All pools are already "
9296 "formatted with version %llu or higher.\n"),
9297 (u_longlong_t)cb.cb_version);
9298 }
9299 }
9300 } else if (argc == 0) {
9301 cb.cb_first = B_TRUE;
9302 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
9303 assert(ret == 0);
9304
9305 if (cb.cb_first) {
9306 (void) printf(gettext("All pools are formatted "
9307 "using feature flags.\n\n"));
9308 } else {
9309 (void) printf(gettext("\nUse 'zpool upgrade -v' "
9310 "for a list of available legacy versions.\n"));
9311 }
9312
9313 cb.cb_first = B_TRUE;
9314 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
9315 assert(ret == 0);
9316
9317 if (cb.cb_first) {
9318 (void) printf(gettext("Every feature flags pool has "
9319 "all supported and requested features enabled.\n"));
9320 } else {
9321 (void) printf(gettext("\n"));
9322 }
9323 } else {
9324 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
9325 B_FALSE, upgrade_one, &cb);
9326 }
9327
9328 return (ret);
9329 }
9330
9331 typedef struct hist_cbdata {
9332 boolean_t first;
9333 boolean_t longfmt;
9334 boolean_t internal;
9335 } hist_cbdata_t;
9336
9337 static void
9338 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
9339 {
9340 nvlist_t **records;
9341 uint_t numrecords;
9342 int i;
9343
9344 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
9345 &records, &numrecords) == 0);
9346 for (i = 0; i < numrecords; i++) {
9347 nvlist_t *rec = records[i];
9348 char tbuf[64] = "";
9349
9350 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
9351 time_t tsec;
9352 struct tm t;
9353
9354 tsec = fnvlist_lookup_uint64(records[i],
9355 ZPOOL_HIST_TIME);
9356 (void) localtime_r(&tsec, &t);
9357 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
9358 }
9359
9360 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) {
9361 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i],
9362 ZPOOL_HIST_ELAPSED_NS);
9363 (void) snprintf(tbuf + strlen(tbuf),
9364 sizeof (tbuf) - strlen(tbuf),
9365 " (%lldms)", (long long)elapsed_ns / 1000 / 1000);
9366 }
9367
9368 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
9369 (void) printf("%s %s", tbuf,
9370 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
9371 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
9372 int ievent =
9373 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
9374 if (!cb->internal)
9375 continue;
9376 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
9377 (void) printf("%s unrecognized record:\n",
9378 tbuf);
9379 dump_nvlist(rec, 4);
9380 continue;
9381 }
9382 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
9383 zfs_history_event_names[ievent],
9384 (longlong_t)fnvlist_lookup_uint64(
9385 rec, ZPOOL_HIST_TXG),
9386 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
9387 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
9388 if (!cb->internal)
9389 continue;
9390 (void) printf("%s [txg:%lld] %s", tbuf,
9391 (longlong_t)fnvlist_lookup_uint64(
9392 rec, ZPOOL_HIST_TXG),
9393 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
9394 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
9395 (void) printf(" %s (%llu)",
9396 fnvlist_lookup_string(rec,
9397 ZPOOL_HIST_DSNAME),
9398 (u_longlong_t)fnvlist_lookup_uint64(rec,
9399 ZPOOL_HIST_DSID));
9400 }
9401 (void) printf(" %s", fnvlist_lookup_string(rec,
9402 ZPOOL_HIST_INT_STR));
9403 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
9404 if (!cb->internal)
9405 continue;
9406 (void) printf("%s ioctl %s\n", tbuf,
9407 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
9408 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
9409 (void) printf(" input:\n");
9410 dump_nvlist(fnvlist_lookup_nvlist(rec,
9411 ZPOOL_HIST_INPUT_NVL), 8);
9412 }
9413 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
9414 (void) printf(" output:\n");
9415 dump_nvlist(fnvlist_lookup_nvlist(rec,
9416 ZPOOL_HIST_OUTPUT_NVL), 8);
9417 }
9418 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) {
9419 (void) printf(" output nvlist omitted; "
9420 "original size: %lldKB\n",
9421 (longlong_t)fnvlist_lookup_int64(rec,
9422 ZPOOL_HIST_OUTPUT_SIZE) / 1024);
9423 }
9424 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
9425 (void) printf(" errno: %lld\n",
9426 (longlong_t)fnvlist_lookup_int64(rec,
9427 ZPOOL_HIST_ERRNO));
9428 }
9429 } else {
9430 if (!cb->internal)
9431 continue;
9432 (void) printf("%s unrecognized record:\n", tbuf);
9433 dump_nvlist(rec, 4);
9434 }
9435
9436 if (!cb->longfmt) {
9437 (void) printf("\n");
9438 continue;
9439 }
9440 (void) printf(" [");
9441 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
9442 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
9443 struct passwd *pwd = getpwuid(who);
9444 (void) printf("user %d ", (int)who);
9445 if (pwd != NULL)
9446 (void) printf("(%s) ", pwd->pw_name);
9447 }
9448 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
9449 (void) printf("on %s",
9450 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
9451 }
9452 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
9453 (void) printf(":%s",
9454 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
9455 }
9456
9457 (void) printf("]");
9458 (void) printf("\n");
9459 }
9460 }
9461
9462 /*
9463 * Print out the command history for a specific pool.
9464 */
9465 static int
9466 get_history_one(zpool_handle_t *zhp, void *data)
9467 {
9468 nvlist_t *nvhis;
9469 int ret;
9470 hist_cbdata_t *cb = (hist_cbdata_t *)data;
9471 uint64_t off = 0;
9472 boolean_t eof = B_FALSE;
9473
9474 cb->first = B_FALSE;
9475
9476 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
9477
9478 while (!eof) {
9479 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0)
9480 return (ret);
9481
9482 print_history_records(nvhis, cb);
9483 nvlist_free(nvhis);
9484 }
9485 (void) printf("\n");
9486
9487 return (ret);
9488 }
9489
9490 /*
9491 * zpool history <pool>
9492 *
9493 * Displays the history of commands that modified pools.
9494 */
9495 int
9496 zpool_do_history(int argc, char **argv)
9497 {
9498 hist_cbdata_t cbdata = { 0 };
9499 int ret;
9500 int c;
9501
9502 cbdata.first = B_TRUE;
9503 /* check options */
9504 while ((c = getopt(argc, argv, "li")) != -1) {
9505 switch (c) {
9506 case 'l':
9507 cbdata.longfmt = B_TRUE;
9508 break;
9509 case 'i':
9510 cbdata.internal = B_TRUE;
9511 break;
9512 case '?':
9513 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9514 optopt);
9515 usage(B_FALSE);
9516 }
9517 }
9518 argc -= optind;
9519 argv += optind;
9520
9521 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
9522 B_FALSE, get_history_one, &cbdata);
9523
9524 if (argc == 0 && cbdata.first == B_TRUE) {
9525 (void) fprintf(stderr, gettext("no pools available\n"));
9526 return (0);
9527 }
9528
9529 return (ret);
9530 }
9531
9532 typedef struct ev_opts {
9533 int verbose;
9534 int scripted;
9535 int follow;
9536 int clear;
9537 char poolname[ZFS_MAX_DATASET_NAME_LEN];
9538 } ev_opts_t;
9539
9540 static void
9541 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
9542 {
9543 char ctime_str[26], str[32], *ptr;
9544 int64_t *tv;
9545 uint_t n;
9546
9547 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
9548 memset(str, ' ', 32);
9549 (void) ctime_r((const time_t *)&tv[0], ctime_str);
9550 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */
9551 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */
9552 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */
9553 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
9554 if (opts->scripted)
9555 (void) printf(gettext("%s\t"), str);
9556 else
9557 (void) printf(gettext("%s "), str);
9558
9559 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
9560 (void) printf(gettext("%s\n"), ptr);
9561 }
9562
9563 static void
9564 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
9565 {
9566 nvpair_t *nvp;
9567
9568 for (nvp = nvlist_next_nvpair(nvl, NULL);
9569 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
9570
9571 data_type_t type = nvpair_type(nvp);
9572 const char *name = nvpair_name(nvp);
9573
9574 boolean_t b;
9575 uint8_t i8;
9576 uint16_t i16;
9577 uint32_t i32;
9578 uint64_t i64;
9579 char *str;
9580 nvlist_t *cnv;
9581
9582 printf(gettext("%*s%s = "), depth, "", name);
9583
9584 switch (type) {
9585 case DATA_TYPE_BOOLEAN:
9586 printf(gettext("%s"), "1");
9587 break;
9588
9589 case DATA_TYPE_BOOLEAN_VALUE:
9590 (void) nvpair_value_boolean_value(nvp, &b);
9591 printf(gettext("%s"), b ? "1" : "0");
9592 break;
9593
9594 case DATA_TYPE_BYTE:
9595 (void) nvpair_value_byte(nvp, &i8);
9596 printf(gettext("0x%x"), i8);
9597 break;
9598
9599 case DATA_TYPE_INT8:
9600 (void) nvpair_value_int8(nvp, (void *)&i8);
9601 printf(gettext("0x%x"), i8);
9602 break;
9603
9604 case DATA_TYPE_UINT8:
9605 (void) nvpair_value_uint8(nvp, &i8);
9606 printf(gettext("0x%x"), i8);
9607 break;
9608
9609 case DATA_TYPE_INT16:
9610 (void) nvpair_value_int16(nvp, (void *)&i16);
9611 printf(gettext("0x%x"), i16);
9612 break;
9613
9614 case DATA_TYPE_UINT16:
9615 (void) nvpair_value_uint16(nvp, &i16);
9616 printf(gettext("0x%x"), i16);
9617 break;
9618
9619 case DATA_TYPE_INT32:
9620 (void) nvpair_value_int32(nvp, (void *)&i32);
9621 printf(gettext("0x%x"), i32);
9622 break;
9623
9624 case DATA_TYPE_UINT32:
9625 (void) nvpair_value_uint32(nvp, &i32);
9626 printf(gettext("0x%x"), i32);
9627 break;
9628
9629 case DATA_TYPE_INT64:
9630 (void) nvpair_value_int64(nvp, (void *)&i64);
9631 printf(gettext("0x%llx"), (u_longlong_t)i64);
9632 break;
9633
9634 case DATA_TYPE_UINT64:
9635 (void) nvpair_value_uint64(nvp, &i64);
9636 /*
9637 * translate vdev state values to readable
9638 * strings to aide zpool events consumers
9639 */
9640 if (strcmp(name,
9641 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
9642 strcmp(name,
9643 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
9644 printf(gettext("\"%s\" (0x%llx)"),
9645 zpool_state_to_name(i64, VDEV_AUX_NONE),
9646 (u_longlong_t)i64);
9647 } else {
9648 printf(gettext("0x%llx"), (u_longlong_t)i64);
9649 }
9650 break;
9651
9652 case DATA_TYPE_HRTIME:
9653 (void) nvpair_value_hrtime(nvp, (void *)&i64);
9654 printf(gettext("0x%llx"), (u_longlong_t)i64);
9655 break;
9656
9657 case DATA_TYPE_STRING:
9658 (void) nvpair_value_string(nvp, &str);
9659 printf(gettext("\"%s\""), str ? str : "<NULL>");
9660 break;
9661
9662 case DATA_TYPE_NVLIST:
9663 printf(gettext("(embedded nvlist)\n"));
9664 (void) nvpair_value_nvlist(nvp, &cnv);
9665 zpool_do_events_nvprint(cnv, depth + 8);
9666 printf(gettext("%*s(end %s)"), depth, "", name);
9667 break;
9668
9669 case DATA_TYPE_NVLIST_ARRAY: {
9670 nvlist_t **val;
9671 uint_t i, nelem;
9672
9673 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
9674 printf(gettext("(%d embedded nvlists)\n"), nelem);
9675 for (i = 0; i < nelem; i++) {
9676 printf(gettext("%*s%s[%d] = %s\n"),
9677 depth, "", name, i, "(embedded nvlist)");
9678 zpool_do_events_nvprint(val[i], depth + 8);
9679 printf(gettext("%*s(end %s[%i])\n"),
9680 depth, "", name, i);
9681 }
9682 printf(gettext("%*s(end %s)\n"), depth, "", name);
9683 }
9684 break;
9685
9686 case DATA_TYPE_INT8_ARRAY: {
9687 int8_t *val;
9688 uint_t i, nelem;
9689
9690 (void) nvpair_value_int8_array(nvp, &val, &nelem);
9691 for (i = 0; i < nelem; i++)
9692 printf(gettext("0x%x "), val[i]);
9693
9694 break;
9695 }
9696
9697 case DATA_TYPE_UINT8_ARRAY: {
9698 uint8_t *val;
9699 uint_t i, nelem;
9700
9701 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
9702 for (i = 0; i < nelem; i++)
9703 printf(gettext("0x%x "), val[i]);
9704
9705 break;
9706 }
9707
9708 case DATA_TYPE_INT16_ARRAY: {
9709 int16_t *val;
9710 uint_t i, nelem;
9711
9712 (void) nvpair_value_int16_array(nvp, &val, &nelem);
9713 for (i = 0; i < nelem; i++)
9714 printf(gettext("0x%x "), val[i]);
9715
9716 break;
9717 }
9718
9719 case DATA_TYPE_UINT16_ARRAY: {
9720 uint16_t *val;
9721 uint_t i, nelem;
9722
9723 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
9724 for (i = 0; i < nelem; i++)
9725 printf(gettext("0x%x "), val[i]);
9726
9727 break;
9728 }
9729
9730 case DATA_TYPE_INT32_ARRAY: {
9731 int32_t *val;
9732 uint_t i, nelem;
9733
9734 (void) nvpair_value_int32_array(nvp, &val, &nelem);
9735 for (i = 0; i < nelem; i++)
9736 printf(gettext("0x%x "), val[i]);
9737
9738 break;
9739 }
9740
9741 case DATA_TYPE_UINT32_ARRAY: {
9742 uint32_t *val;
9743 uint_t i, nelem;
9744
9745 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
9746 for (i = 0; i < nelem; i++)
9747 printf(gettext("0x%x "), val[i]);
9748
9749 break;
9750 }
9751
9752 case DATA_TYPE_INT64_ARRAY: {
9753 int64_t *val;
9754 uint_t i, nelem;
9755
9756 (void) nvpair_value_int64_array(nvp, &val, &nelem);
9757 for (i = 0; i < nelem; i++)
9758 printf(gettext("0x%llx "),
9759 (u_longlong_t)val[i]);
9760
9761 break;
9762 }
9763
9764 case DATA_TYPE_UINT64_ARRAY: {
9765 uint64_t *val;
9766 uint_t i, nelem;
9767
9768 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
9769 for (i = 0; i < nelem; i++)
9770 printf(gettext("0x%llx "),
9771 (u_longlong_t)val[i]);
9772
9773 break;
9774 }
9775
9776 case DATA_TYPE_STRING_ARRAY: {
9777 char **str;
9778 uint_t i, nelem;
9779
9780 (void) nvpair_value_string_array(nvp, &str, &nelem);
9781 for (i = 0; i < nelem; i++)
9782 printf(gettext("\"%s\" "),
9783 str[i] ? str[i] : "<NULL>");
9784
9785 break;
9786 }
9787
9788 case DATA_TYPE_BOOLEAN_ARRAY:
9789 case DATA_TYPE_BYTE_ARRAY:
9790 case DATA_TYPE_DOUBLE:
9791 case DATA_TYPE_DONTCARE:
9792 case DATA_TYPE_UNKNOWN:
9793 printf(gettext("<unknown>"));
9794 break;
9795 }
9796
9797 printf(gettext("\n"));
9798 }
9799 }
9800
9801 static int
9802 zpool_do_events_next(ev_opts_t *opts)
9803 {
9804 nvlist_t *nvl;
9805 int zevent_fd, ret, dropped;
9806 char *pool;
9807
9808 zevent_fd = open(ZFS_DEV, O_RDWR);
9809 VERIFY(zevent_fd >= 0);
9810
9811 if (!opts->scripted)
9812 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
9813
9814 while (1) {
9815 ret = zpool_events_next(g_zfs, &nvl, &dropped,
9816 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
9817 if (ret || nvl == NULL)
9818 break;
9819
9820 if (dropped > 0)
9821 (void) printf(gettext("dropped %d events\n"), dropped);
9822
9823 if (strlen(opts->poolname) > 0 &&
9824 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
9825 strcmp(opts->poolname, pool) != 0)
9826 continue;
9827
9828 zpool_do_events_short(nvl, opts);
9829
9830 if (opts->verbose) {
9831 zpool_do_events_nvprint(nvl, 8);
9832 printf(gettext("\n"));
9833 }
9834 (void) fflush(stdout);
9835
9836 nvlist_free(nvl);
9837 }
9838
9839 VERIFY(0 == close(zevent_fd));
9840
9841 return (ret);
9842 }
9843
9844 static int
9845 zpool_do_events_clear(void)
9846 {
9847 int count, ret;
9848
9849 ret = zpool_events_clear(g_zfs, &count);
9850 if (!ret)
9851 (void) printf(gettext("cleared %d events\n"), count);
9852
9853 return (ret);
9854 }
9855
9856 /*
9857 * zpool events [-vHf [pool] | -c]
9858 *
9859 * Displays events logs by ZFS.
9860 */
9861 int
9862 zpool_do_events(int argc, char **argv)
9863 {
9864 ev_opts_t opts = { 0 };
9865 int ret;
9866 int c;
9867
9868 /* check options */
9869 while ((c = getopt(argc, argv, "vHfc")) != -1) {
9870 switch (c) {
9871 case 'v':
9872 opts.verbose = 1;
9873 break;
9874 case 'H':
9875 opts.scripted = 1;
9876 break;
9877 case 'f':
9878 opts.follow = 1;
9879 break;
9880 case 'c':
9881 opts.clear = 1;
9882 break;
9883 case '?':
9884 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
9885 optopt);
9886 usage(B_FALSE);
9887 }
9888 }
9889 argc -= optind;
9890 argv += optind;
9891
9892 if (argc > 1) {
9893 (void) fprintf(stderr, gettext("too many arguments\n"));
9894 usage(B_FALSE);
9895 } else if (argc == 1) {
9896 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
9897 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
9898 (void) fprintf(stderr,
9899 gettext("invalid pool name '%s'\n"), opts.poolname);
9900 usage(B_FALSE);
9901 }
9902 }
9903
9904 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
9905 opts.clear) {
9906 (void) fprintf(stderr,
9907 gettext("invalid options combined with -c\n"));
9908 usage(B_FALSE);
9909 }
9910
9911 if (opts.clear)
9912 ret = zpool_do_events_clear();
9913 else
9914 ret = zpool_do_events_next(&opts);
9915
9916 return (ret);
9917 }
9918
9919 static int
9920 get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data)
9921 {
9922 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9923 char value[ZFS_MAXPROPLEN];
9924 zprop_source_t srctype;
9925
9926 for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL;
9927 pl = pl->pl_next) {
9928 char *prop_name;
9929 /*
9930 * If the first property is pool name, it is a special
9931 * placeholder that we can skip. This will also skip
9932 * over the name property when 'all' is specified.
9933 */
9934 if (pl->pl_prop == ZPOOL_PROP_NAME &&
9935 pl == cbp->cb_proplist)
9936 continue;
9937
9938 if (pl->pl_prop == ZPROP_INVAL) {
9939 prop_name = pl->pl_user_prop;
9940 } else {
9941 prop_name = (char *)vdev_prop_to_name(pl->pl_prop);
9942 }
9943 if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop,
9944 prop_name, value, sizeof (value), &srctype,
9945 cbp->cb_literal) == 0) {
9946 zprop_print_one_property(vdevname, cbp, prop_name,
9947 value, srctype, NULL, NULL);
9948 }
9949 }
9950
9951 return (0);
9952 }
9953
9954 static int
9955 get_callback_vdev_width_cb(void *zhp_data, nvlist_t *nv, void *data)
9956 {
9957 zpool_handle_t *zhp = zhp_data;
9958 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9959 char *vdevname = zpool_vdev_name(g_zfs, zhp, nv,
9960 cbp->cb_vdevs.cb_name_flags);
9961 int ret;
9962
9963 /* Adjust the column widths for the vdev properties */
9964 ret = vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist);
9965
9966 return (ret);
9967 }
9968
9969 static int
9970 get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data)
9971 {
9972 zpool_handle_t *zhp = zhp_data;
9973 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9974 char *vdevname = zpool_vdev_name(g_zfs, zhp, nv,
9975 cbp->cb_vdevs.cb_name_flags);
9976 int ret;
9977
9978 /* Display the properties */
9979 ret = get_callback_vdev(zhp, vdevname, data);
9980
9981 return (ret);
9982 }
9983
9984 static int
9985 get_callback(zpool_handle_t *zhp, void *data)
9986 {
9987 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
9988 char value[MAXNAMELEN];
9989 zprop_source_t srctype;
9990 zprop_list_t *pl;
9991 int vid;
9992
9993 if (cbp->cb_type == ZFS_TYPE_VDEV) {
9994 if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) {
9995 for_each_vdev(zhp, get_callback_vdev_width_cb, data);
9996 for_each_vdev(zhp, get_callback_vdev_cb, data);
9997 } else {
9998 /* Adjust column widths for vdev properties */
9999 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
10000 vid++) {
10001 vdev_expand_proplist(zhp,
10002 cbp->cb_vdevs.cb_names[vid],
10003 &cbp->cb_proplist);
10004 }
10005 /* Display the properties */
10006 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
10007 vid++) {
10008 get_callback_vdev(zhp,
10009 cbp->cb_vdevs.cb_names[vid], data);
10010 }
10011 }
10012 } else {
10013 assert(cbp->cb_type == ZFS_TYPE_POOL);
10014 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
10015 /*
10016 * Skip the special fake placeholder. This will also
10017 * skip over the name property when 'all' is specified.
10018 */
10019 if (pl->pl_prop == ZPOOL_PROP_NAME &&
10020 pl == cbp->cb_proplist)
10021 continue;
10022
10023 if (pl->pl_prop == ZPROP_INVAL &&
10024 (zpool_prop_feature(pl->pl_user_prop) ||
10025 zpool_prop_unsupported(pl->pl_user_prop))) {
10026 srctype = ZPROP_SRC_LOCAL;
10027
10028 if (zpool_prop_get_feature(zhp,
10029 pl->pl_user_prop, value,
10030 sizeof (value)) == 0) {
10031 zprop_print_one_property(
10032 zpool_get_name(zhp), cbp,
10033 pl->pl_user_prop, value, srctype,
10034 NULL, NULL);
10035 }
10036 } else {
10037 if (zpool_get_prop(zhp, pl->pl_prop, value,
10038 sizeof (value), &srctype,
10039 cbp->cb_literal) != 0)
10040 continue;
10041
10042 zprop_print_one_property(zpool_get_name(zhp),
10043 cbp, zpool_prop_to_name(pl->pl_prop),
10044 value, srctype, NULL, NULL);
10045 }
10046 }
10047 }
10048
10049 return (0);
10050 }
10051
10052 /*
10053 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
10054 *
10055 * -H Scripted mode. Don't display headers, and separate properties
10056 * by a single tab.
10057 * -o List of columns to display. Defaults to
10058 * "name,property,value,source".
10059 * -p Display values in parsable (exact) format.
10060 *
10061 * Get properties of pools in the system. Output space statistics
10062 * for each one as well as other attributes.
10063 */
10064 int
10065 zpool_do_get(int argc, char **argv)
10066 {
10067 zprop_get_cbdata_t cb = { 0 };
10068 zprop_list_t fake_name = { 0 };
10069 int ret;
10070 int c, i;
10071 char *propstr = NULL;
10072
10073 cb.cb_first = B_TRUE;
10074
10075 /*
10076 * Set up default columns and sources.
10077 */
10078 cb.cb_sources = ZPROP_SRC_ALL;
10079 cb.cb_columns[0] = GET_COL_NAME;
10080 cb.cb_columns[1] = GET_COL_PROPERTY;
10081 cb.cb_columns[2] = GET_COL_VALUE;
10082 cb.cb_columns[3] = GET_COL_SOURCE;
10083 cb.cb_type = ZFS_TYPE_POOL;
10084 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
10085 current_prop_type = cb.cb_type;
10086
10087 /* check options */
10088 while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
10089 switch (c) {
10090 case 'p':
10091 cb.cb_literal = B_TRUE;
10092 break;
10093 case 'H':
10094 cb.cb_scripted = B_TRUE;
10095 break;
10096 case 'o':
10097 memset(&cb.cb_columns, 0, sizeof (cb.cb_columns));
10098 i = 0;
10099
10100 for (char *tok; (tok = strsep(&optarg, ",")); ) {
10101 static const char *const col_opts[] =
10102 { "name", "property", "value", "source",
10103 "all" };
10104 static const zfs_get_column_t col_cols[] =
10105 { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE,
10106 GET_COL_SOURCE };
10107
10108 if (i == ZFS_GET_NCOLS - 1) {
10109 (void) fprintf(stderr, gettext("too "
10110 "many fields given to -o "
10111 "option\n"));
10112 usage(B_FALSE);
10113 }
10114
10115 for (c = 0; c < ARRAY_SIZE(col_opts); ++c)
10116 if (strcmp(tok, col_opts[c]) == 0)
10117 goto found;
10118
10119 (void) fprintf(stderr,
10120 gettext("invalid column name '%s'\n"), tok);
10121 usage(B_FALSE);
10122
10123 found:
10124 if (c >= 4) {
10125 if (i > 0) {
10126 (void) fprintf(stderr,
10127 gettext("\"all\" conflicts "
10128 "with specific fields "
10129 "given to -o option\n"));
10130 usage(B_FALSE);
10131 }
10132
10133 memcpy(cb.cb_columns, col_cols,
10134 sizeof (col_cols));
10135 i = ZFS_GET_NCOLS - 1;
10136 } else
10137 cb.cb_columns[i++] = col_cols[c];
10138 }
10139 break;
10140 case '?':
10141 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
10142 optopt);
10143 usage(B_FALSE);
10144 }
10145 }
10146
10147 argc -= optind;
10148 argv += optind;
10149
10150 if (argc < 1) {
10151 (void) fprintf(stderr, gettext("missing property "
10152 "argument\n"));
10153 usage(B_FALSE);
10154 }
10155
10156 /* Properties list is needed later by zprop_get_list() */
10157 propstr = argv[0];
10158
10159 argc--;
10160 argv++;
10161
10162 if (argc == 0) {
10163 /* No args, so just print the defaults. */
10164 } else if (are_all_pools(argc, argv)) {
10165 /* All the args are pool names */
10166 } else if (are_all_pools(1, argv)) {
10167 /* The first arg is a pool name */
10168 if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) ||
10169 are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
10170 &cb.cb_vdevs)) {
10171 /* ... and the rest are vdev names */
10172 cb.cb_vdevs.cb_names = argv + 1;
10173 cb.cb_vdevs.cb_names_count = argc - 1;
10174 cb.cb_type = ZFS_TYPE_VDEV;
10175 argc = 1; /* One pool to process */
10176 } else {
10177 fprintf(stderr, gettext("Expected a list of vdevs in"
10178 " \"%s\", but got:\n"), argv[0]);
10179 error_list_unresolved_vdevs(argc - 1, argv + 1,
10180 argv[0], &cb.cb_vdevs);
10181 fprintf(stderr, "\n");
10182 usage(B_FALSE);
10183 return (1);
10184 }
10185 } else {
10186 /*
10187 * The first arg isn't a pool name,
10188 */
10189 fprintf(stderr, gettext("missing pool name.\n"));
10190 fprintf(stderr, "\n");
10191 usage(B_FALSE);
10192 return (1);
10193 }
10194
10195 if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist,
10196 cb.cb_type) != 0) {
10197 /* Use correct list of valid properties (pool or vdev) */
10198 current_prop_type = cb.cb_type;
10199 usage(B_FALSE);
10200 }
10201
10202 if (cb.cb_proplist != NULL) {
10203 fake_name.pl_prop = ZPOOL_PROP_NAME;
10204 fake_name.pl_width = strlen(gettext("NAME"));
10205 fake_name.pl_next = cb.cb_proplist;
10206 cb.cb_proplist = &fake_name;
10207 }
10208
10209 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type,
10210 cb.cb_literal, get_callback, &cb);
10211
10212 if (cb.cb_proplist == &fake_name)
10213 zprop_free_list(fake_name.pl_next);
10214 else
10215 zprop_free_list(cb.cb_proplist);
10216
10217 return (ret);
10218 }
10219
10220 typedef struct set_cbdata {
10221 char *cb_propname;
10222 char *cb_value;
10223 zfs_type_t cb_type;
10224 vdev_cbdata_t cb_vdevs;
10225 boolean_t cb_any_successful;
10226 } set_cbdata_t;
10227
10228 static int
10229 set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb)
10230 {
10231 int error;
10232
10233 /* Check if we have out-of-bounds features */
10234 if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) {
10235 boolean_t features[SPA_FEATURES];
10236 if (zpool_do_load_compat(cb->cb_value, features) !=
10237 ZPOOL_COMPATIBILITY_OK)
10238 return (-1);
10239
10240 nvlist_t *enabled = zpool_get_features(zhp);
10241 spa_feature_t i;
10242 for (i = 0; i < SPA_FEATURES; i++) {
10243 const char *fguid = spa_feature_table[i].fi_guid;
10244 if (nvlist_exists(enabled, fguid) && !features[i])
10245 break;
10246 }
10247 if (i < SPA_FEATURES)
10248 (void) fprintf(stderr, gettext("Warning: one or "
10249 "more features already enabled on pool '%s'\n"
10250 "are not present in this compatibility set.\n"),
10251 zpool_get_name(zhp));
10252 }
10253
10254 /* if we're setting a feature, check it's in compatibility set */
10255 if (zpool_prop_feature(cb->cb_propname) &&
10256 strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) {
10257 char *fname = strchr(cb->cb_propname, '@') + 1;
10258 spa_feature_t f;
10259
10260 if (zfeature_lookup_name(fname, &f) == 0) {
10261 char compat[ZFS_MAXPROPLEN];
10262 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY,
10263 compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
10264 compat[0] = '\0';
10265
10266 boolean_t features[SPA_FEATURES];
10267 if (zpool_do_load_compat(compat, features) !=
10268 ZPOOL_COMPATIBILITY_OK) {
10269 (void) fprintf(stderr, gettext("Error: "
10270 "cannot enable feature '%s' on pool '%s'\n"
10271 "because the pool's 'compatibility' "
10272 "property cannot be parsed.\n"),
10273 fname, zpool_get_name(zhp));
10274 return (-1);
10275 }
10276
10277 if (!features[f]) {
10278 (void) fprintf(stderr, gettext("Error: "
10279 "cannot enable feature '%s' on pool '%s'\n"
10280 "as it is not specified in this pool's "
10281 "current compatibility set.\n"
10282 "Consider setting 'compatibility' to a "
10283 "less restrictive set, or to 'off'.\n"),
10284 fname, zpool_get_name(zhp));
10285 return (-1);
10286 }
10287 }
10288 }
10289
10290 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
10291
10292 return (error);
10293 }
10294
10295 static int
10296 set_callback(zpool_handle_t *zhp, void *data)
10297 {
10298 int error;
10299 set_cbdata_t *cb = (set_cbdata_t *)data;
10300
10301 if (cb->cb_type == ZFS_TYPE_VDEV) {
10302 error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names,
10303 cb->cb_propname, cb->cb_value);
10304 } else {
10305 assert(cb->cb_type == ZFS_TYPE_POOL);
10306 error = set_pool_callback(zhp, cb);
10307 }
10308
10309 cb->cb_any_successful = !error;
10310 return (error);
10311 }
10312
10313 int
10314 zpool_do_set(int argc, char **argv)
10315 {
10316 set_cbdata_t cb = { 0 };
10317 int error;
10318
10319 current_prop_type = ZFS_TYPE_POOL;
10320 if (argc > 1 && argv[1][0] == '-') {
10321 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
10322 argv[1][1]);
10323 usage(B_FALSE);
10324 }
10325
10326 if (argc < 2) {
10327 (void) fprintf(stderr, gettext("missing property=value "
10328 "argument\n"));
10329 usage(B_FALSE);
10330 }
10331
10332 if (argc < 3) {
10333 (void) fprintf(stderr, gettext("missing pool name\n"));
10334 usage(B_FALSE);
10335 }
10336
10337 if (argc > 4) {
10338 (void) fprintf(stderr, gettext("too many pool names\n"));
10339 usage(B_FALSE);
10340 }
10341
10342 cb.cb_propname = argv[1];
10343 cb.cb_type = ZFS_TYPE_POOL;
10344 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
10345 cb.cb_value = strchr(cb.cb_propname, '=');
10346 if (cb.cb_value == NULL) {
10347 (void) fprintf(stderr, gettext("missing value in "
10348 "property=value argument\n"));
10349 usage(B_FALSE);
10350 }
10351
10352 *(cb.cb_value) = '\0';
10353 cb.cb_value++;
10354 argc -= 2;
10355 argv += 2;
10356
10357 if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
10358 /* Argument is a vdev */
10359 cb.cb_vdevs.cb_names = argv;
10360 cb.cb_vdevs.cb_names_count = 1;
10361 cb.cb_type = ZFS_TYPE_VDEV;
10362 argc = 0; /* No pools to process */
10363 } else if (are_all_pools(1, argv)) {
10364 /* The first arg is a pool name */
10365 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
10366 &cb.cb_vdevs)) {
10367 /* 2nd argument is a vdev */
10368 cb.cb_vdevs.cb_names = argv + 1;
10369 cb.cb_vdevs.cb_names_count = 1;
10370 cb.cb_type = ZFS_TYPE_VDEV;
10371 argc = 1; /* One pool to process */
10372 } else if (argc > 1) {
10373 (void) fprintf(stderr,
10374 gettext("too many pool names\n"));
10375 usage(B_FALSE);
10376 }
10377 }
10378
10379 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
10380 B_FALSE, set_callback, &cb);
10381
10382 return (error);
10383 }
10384
10385 /* Add up the total number of bytes left to initialize/trim across all vdevs */
10386 static uint64_t
10387 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity)
10388 {
10389 uint64_t bytes_remaining;
10390 nvlist_t **child;
10391 uint_t c, children;
10392 vdev_stat_t *vs;
10393
10394 assert(activity == ZPOOL_WAIT_INITIALIZE ||
10395 activity == ZPOOL_WAIT_TRIM);
10396
10397 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
10398 (uint64_t **)&vs, &c) == 0);
10399
10400 if (activity == ZPOOL_WAIT_INITIALIZE &&
10401 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE)
10402 bytes_remaining = vs->vs_initialize_bytes_est -
10403 vs->vs_initialize_bytes_done;
10404 else if (activity == ZPOOL_WAIT_TRIM &&
10405 vs->vs_trim_state == VDEV_TRIM_ACTIVE)
10406 bytes_remaining = vs->vs_trim_bytes_est -
10407 vs->vs_trim_bytes_done;
10408 else
10409 bytes_remaining = 0;
10410
10411 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10412 &child, &children) != 0)
10413 children = 0;
10414
10415 for (c = 0; c < children; c++)
10416 bytes_remaining += vdev_activity_remaining(child[c], activity);
10417
10418 return (bytes_remaining);
10419 }
10420
10421 /* Add up the total number of bytes left to rebuild across top-level vdevs */
10422 static uint64_t
10423 vdev_activity_top_remaining(nvlist_t *nv)
10424 {
10425 uint64_t bytes_remaining = 0;
10426 nvlist_t **child;
10427 uint_t children;
10428 int error;
10429
10430 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10431 &child, &children) != 0)
10432 children = 0;
10433
10434 for (uint_t c = 0; c < children; c++) {
10435 vdev_rebuild_stat_t *vrs;
10436 uint_t i;
10437
10438 error = nvlist_lookup_uint64_array(child[c],
10439 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i);
10440 if (error == 0) {
10441 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
10442 bytes_remaining += (vrs->vrs_bytes_est -
10443 vrs->vrs_bytes_rebuilt);
10444 }
10445 }
10446 }
10447
10448 return (bytes_remaining);
10449 }
10450
10451 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
10452 static boolean_t
10453 vdev_any_spare_replacing(nvlist_t *nv)
10454 {
10455 nvlist_t **child;
10456 uint_t c, children;
10457 char *vdev_type;
10458
10459 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type);
10460
10461 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 ||
10462 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 ||
10463 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) {
10464 return (B_TRUE);
10465 }
10466
10467 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
10468 &child, &children) != 0)
10469 children = 0;
10470
10471 for (c = 0; c < children; c++) {
10472 if (vdev_any_spare_replacing(child[c]))
10473 return (B_TRUE);
10474 }
10475
10476 return (B_FALSE);
10477 }
10478
10479 typedef struct wait_data {
10480 char *wd_poolname;
10481 boolean_t wd_scripted;
10482 boolean_t wd_exact;
10483 boolean_t wd_headers_once;
10484 boolean_t wd_should_exit;
10485 /* Which activities to wait for */
10486 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES];
10487 float wd_interval;
10488 pthread_cond_t wd_cv;
10489 pthread_mutex_t wd_mutex;
10490 } wait_data_t;
10491
10492 /*
10493 * Print to stdout a single line, containing one column for each activity that
10494 * we are waiting for specifying how many bytes of work are left for that
10495 * activity.
10496 */
10497 static void
10498 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
10499 {
10500 nvlist_t *config, *nvroot;
10501 uint_t c;
10502 int i;
10503 pool_checkpoint_stat_t *pcs = NULL;
10504 pool_scan_stat_t *pss = NULL;
10505 pool_removal_stat_t *prs = NULL;
10506 const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE",
10507 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM"};
10508 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES];
10509
10510 /* Calculate the width of each column */
10511 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10512 /*
10513 * Make sure we have enough space in the col for pretty-printed
10514 * numbers and for the column header, and then leave a couple
10515 * spaces between cols for readability.
10516 */
10517 col_widths[i] = MAX(strlen(headers[i]), 6) + 2;
10518 }
10519
10520 /* Print header if appropriate */
10521 int term_height = terminal_height();
10522 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 &&
10523 row % (term_height-1) == 0);
10524 if (!wd->wd_scripted && (row == 0 || reprint_header)) {
10525 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10526 if (wd->wd_enabled[i])
10527 (void) printf("%*s", col_widths[i], headers[i]);
10528 }
10529 (void) fputc('\n', stdout);
10530 }
10531
10532 /* Bytes of work remaining in each activity */
10533 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0};
10534
10535 bytes_rem[ZPOOL_WAIT_FREE] =
10536 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL);
10537
10538 config = zpool_get_config(zhp, NULL);
10539 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
10540
10541 (void) nvlist_lookup_uint64_array(nvroot,
10542 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
10543 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
10544 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space;
10545
10546 (void) nvlist_lookup_uint64_array(nvroot,
10547 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
10548 if (prs != NULL && prs->prs_state == DSS_SCANNING)
10549 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy -
10550 prs->prs_copied;
10551
10552 (void) nvlist_lookup_uint64_array(nvroot,
10553 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c);
10554 if (pss != NULL && pss->pss_state == DSS_SCANNING &&
10555 pss->pss_pass_scrub_pause == 0) {
10556 int64_t rem = pss->pss_to_examine - pss->pss_issued;
10557 if (pss->pss_func == POOL_SCAN_SCRUB)
10558 bytes_rem[ZPOOL_WAIT_SCRUB] = rem;
10559 else
10560 bytes_rem[ZPOOL_WAIT_RESILVER] = rem;
10561 } else if (check_rebuilding(nvroot, NULL)) {
10562 bytes_rem[ZPOOL_WAIT_RESILVER] =
10563 vdev_activity_top_remaining(nvroot);
10564 }
10565
10566 bytes_rem[ZPOOL_WAIT_INITIALIZE] =
10567 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE);
10568 bytes_rem[ZPOOL_WAIT_TRIM] =
10569 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM);
10570
10571 /*
10572 * A replace finishes after resilvering finishes, so the amount of work
10573 * left for a replace is the same as for resilvering.
10574 *
10575 * It isn't quite correct to say that if we have any 'spare' or
10576 * 'replacing' vdevs and a resilver is happening, then a replace is in
10577 * progress, like we do here. When a hot spare is used, the faulted vdev
10578 * is not removed after the hot spare is resilvered, so parent 'spare'
10579 * vdev is not removed either. So we could have a 'spare' vdev, but be
10580 * resilvering for a different reason. However, we use it as a heuristic
10581 * because we don't have access to the DTLs, which could tell us whether
10582 * or not we have really finished resilvering a hot spare.
10583 */
10584 if (vdev_any_spare_replacing(nvroot))
10585 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER];
10586
10587 if (timestamp_fmt != NODATE)
10588 print_timestamp(timestamp_fmt);
10589
10590 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10591 char buf[64];
10592 if (!wd->wd_enabled[i])
10593 continue;
10594
10595 if (wd->wd_exact)
10596 (void) snprintf(buf, sizeof (buf), "%" PRIi64,
10597 bytes_rem[i]);
10598 else
10599 zfs_nicenum(bytes_rem[i], buf, sizeof (buf));
10600
10601 if (wd->wd_scripted)
10602 (void) printf(i == 0 ? "%s" : "\t%s", buf);
10603 else
10604 (void) printf(" %*s", col_widths[i] - 1, buf);
10605 }
10606 (void) printf("\n");
10607 (void) fflush(stdout);
10608 }
10609
10610 static void *
10611 wait_status_thread(void *arg)
10612 {
10613 wait_data_t *wd = (wait_data_t *)arg;
10614 zpool_handle_t *zhp;
10615
10616 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL)
10617 return (void *)(1);
10618
10619 for (int row = 0; ; row++) {
10620 boolean_t missing;
10621 struct timespec timeout;
10622 int ret = 0;
10623 (void) clock_gettime(CLOCK_REALTIME, &timeout);
10624
10625 if (zpool_refresh_stats(zhp, &missing) != 0 || missing ||
10626 zpool_props_refresh(zhp) != 0) {
10627 zpool_close(zhp);
10628 return (void *)(uintptr_t)(missing ? 0 : 1);
10629 }
10630
10631 print_wait_status_row(wd, zhp, row);
10632
10633 timeout.tv_sec += floor(wd->wd_interval);
10634 long nanos = timeout.tv_nsec +
10635 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC;
10636 if (nanos >= NANOSEC) {
10637 timeout.tv_sec++;
10638 timeout.tv_nsec = nanos - NANOSEC;
10639 } else {
10640 timeout.tv_nsec = nanos;
10641 }
10642 pthread_mutex_lock(&wd->wd_mutex);
10643 if (!wd->wd_should_exit)
10644 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex,
10645 &timeout);
10646 pthread_mutex_unlock(&wd->wd_mutex);
10647 if (ret == 0) {
10648 break; /* signaled by main thread */
10649 } else if (ret != ETIMEDOUT) {
10650 (void) fprintf(stderr, gettext("pthread_cond_timedwait "
10651 "failed: %s\n"), strerror(ret));
10652 zpool_close(zhp);
10653 return (void *)(uintptr_t)(1);
10654 }
10655 }
10656
10657 zpool_close(zhp);
10658 return (void *)(0);
10659 }
10660
10661 int
10662 zpool_do_wait(int argc, char **argv)
10663 {
10664 boolean_t verbose = B_FALSE;
10665 int c, i;
10666 unsigned long count;
10667 pthread_t status_thr;
10668 int error = 0;
10669 zpool_handle_t *zhp;
10670
10671 wait_data_t wd;
10672 wd.wd_scripted = B_FALSE;
10673 wd.wd_exact = B_FALSE;
10674 wd.wd_headers_once = B_FALSE;
10675 wd.wd_should_exit = B_FALSE;
10676
10677 pthread_mutex_init(&wd.wd_mutex, NULL);
10678 pthread_cond_init(&wd.wd_cv, NULL);
10679
10680 /* By default, wait for all types of activity. */
10681 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++)
10682 wd.wd_enabled[i] = B_TRUE;
10683
10684 while ((c = getopt(argc, argv, "HpT:t:")) != -1) {
10685 switch (c) {
10686 case 'H':
10687 wd.wd_scripted = B_TRUE;
10688 break;
10689 case 'n':
10690 wd.wd_headers_once = B_TRUE;
10691 break;
10692 case 'p':
10693 wd.wd_exact = B_TRUE;
10694 break;
10695 case 'T':
10696 get_timestamp_arg(*optarg);
10697 break;
10698 case 't':
10699 /* Reset activities array */
10700 memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled));
10701
10702 for (char *tok; (tok = strsep(&optarg, ",")); ) {
10703 static const char *const col_opts[] = {
10704 "discard", "free", "initialize", "replace",
10705 "remove", "resilver", "scrub", "trim" };
10706
10707 for (i = 0; i < ARRAY_SIZE(col_opts); ++i)
10708 if (strcmp(tok, col_opts[i]) == 0) {
10709 wd.wd_enabled[i] = B_TRUE;
10710 goto found;
10711 }
10712
10713 (void) fprintf(stderr,
10714 gettext("invalid activity '%s'\n"), tok);
10715 usage(B_FALSE);
10716 found:;
10717 }
10718 break;
10719 case '?':
10720 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
10721 optopt);
10722 usage(B_FALSE);
10723 }
10724 }
10725
10726 argc -= optind;
10727 argv += optind;
10728
10729 get_interval_count(&argc, argv, &wd.wd_interval, &count);
10730 if (count != 0) {
10731 /* This subcmd only accepts an interval, not a count */
10732 (void) fprintf(stderr, gettext("too many arguments\n"));
10733 usage(B_FALSE);
10734 }
10735
10736 if (wd.wd_interval != 0)
10737 verbose = B_TRUE;
10738
10739 if (argc < 1) {
10740 (void) fprintf(stderr, gettext("missing 'pool' argument\n"));
10741 usage(B_FALSE);
10742 }
10743 if (argc > 1) {
10744 (void) fprintf(stderr, gettext("too many arguments\n"));
10745 usage(B_FALSE);
10746 }
10747
10748 wd.wd_poolname = argv[0];
10749
10750 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL)
10751 return (1);
10752
10753 if (verbose) {
10754 /*
10755 * We use a separate thread for printing status updates because
10756 * the main thread will call lzc_wait(), which blocks as long
10757 * as an activity is in progress, which can be a long time.
10758 */
10759 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd)
10760 != 0) {
10761 (void) fprintf(stderr, gettext("failed to create status"
10762 "thread: %s\n"), strerror(errno));
10763 zpool_close(zhp);
10764 return (1);
10765 }
10766 }
10767
10768 /*
10769 * Loop over all activities that we are supposed to wait for until none
10770 * of them are in progress. Note that this means we can end up waiting
10771 * for more activities to complete than just those that were in progress
10772 * when we began waiting; if an activity we are interested in begins
10773 * while we are waiting for another activity, we will wait for both to
10774 * complete before exiting.
10775 */
10776 for (;;) {
10777 boolean_t missing = B_FALSE;
10778 boolean_t any_waited = B_FALSE;
10779
10780 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
10781 boolean_t waited;
10782
10783 if (!wd.wd_enabled[i])
10784 continue;
10785
10786 error = zpool_wait_status(zhp, i, &missing, &waited);
10787 if (error != 0 || missing)
10788 break;
10789
10790 any_waited = (any_waited || waited);
10791 }
10792
10793 if (error != 0 || missing || !any_waited)
10794 break;
10795 }
10796
10797 zpool_close(zhp);
10798
10799 if (verbose) {
10800 uintptr_t status;
10801 pthread_mutex_lock(&wd.wd_mutex);
10802 wd.wd_should_exit = B_TRUE;
10803 pthread_cond_signal(&wd.wd_cv);
10804 pthread_mutex_unlock(&wd.wd_mutex);
10805 (void) pthread_join(status_thr, (void *)&status);
10806 if (status != 0)
10807 error = status;
10808 }
10809
10810 pthread_mutex_destroy(&wd.wd_mutex);
10811 pthread_cond_destroy(&wd.wd_cv);
10812 return (error);
10813 }
10814
10815 static int
10816 find_command_idx(const char *command, int *idx)
10817 {
10818 for (int i = 0; i < NCOMMAND; ++i) {
10819 if (command_table[i].name == NULL)
10820 continue;
10821
10822 if (strcmp(command, command_table[i].name) == 0) {
10823 *idx = i;
10824 return (0);
10825 }
10826 }
10827 return (1);
10828 }
10829
10830 /*
10831 * Display version message
10832 */
10833 static int
10834 zpool_do_version(int argc, char **argv)
10835 {
10836 (void) argc, (void) argv;
10837 return (zfs_version_print() != 0);
10838 }
10839
10840 /*
10841 * Do zpool_load_compat() and print error message on failure
10842 */
10843 static zpool_compat_status_t
10844 zpool_do_load_compat(const char *compat, boolean_t *list)
10845 {
10846 char report[1024];
10847
10848 zpool_compat_status_t ret;
10849
10850 ret = zpool_load_compat(compat, list, report, 1024);
10851 switch (ret) {
10852
10853 case ZPOOL_COMPATIBILITY_OK:
10854 break;
10855
10856 case ZPOOL_COMPATIBILITY_NOFILES:
10857 case ZPOOL_COMPATIBILITY_BADFILE:
10858 case ZPOOL_COMPATIBILITY_BADTOKEN:
10859 (void) fprintf(stderr, "Error: %s\n", report);
10860 break;
10861
10862 case ZPOOL_COMPATIBILITY_WARNTOKEN:
10863 (void) fprintf(stderr, "Warning: %s\n", report);
10864 ret = ZPOOL_COMPATIBILITY_OK;
10865 break;
10866 }
10867 return (ret);
10868 }
10869
10870 int
10871 main(int argc, char **argv)
10872 {
10873 int ret = 0;
10874 int i = 0;
10875 char *cmdname;
10876 char **newargv;
10877
10878 (void) setlocale(LC_ALL, "");
10879 (void) setlocale(LC_NUMERIC, "C");
10880 (void) textdomain(TEXT_DOMAIN);
10881 srand(time(NULL));
10882
10883 opterr = 0;
10884
10885 /*
10886 * Make sure the user has specified some command.
10887 */
10888 if (argc < 2) {
10889 (void) fprintf(stderr, gettext("missing command\n"));
10890 usage(B_FALSE);
10891 }
10892
10893 cmdname = argv[1];
10894
10895 /*
10896 * Special case '-?'
10897 */
10898 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
10899 usage(B_TRUE);
10900
10901 /*
10902 * Special case '-V|--version'
10903 */
10904 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
10905 return (zpool_do_version(argc, argv));
10906
10907 if ((g_zfs = libzfs_init()) == NULL) {
10908 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
10909 return (1);
10910 }
10911
10912 libzfs_print_on_error(g_zfs, B_TRUE);
10913
10914 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
10915
10916 /*
10917 * Many commands modify input strings for string parsing reasons.
10918 * We create a copy to protect the original argv.
10919 */
10920 newargv = safe_malloc((argc + 1) * sizeof (newargv[0]));
10921 for (i = 0; i < argc; i++)
10922 newargv[i] = strdup(argv[i]);
10923 newargv[argc] = NULL;
10924
10925 /*
10926 * Run the appropriate command.
10927 */
10928 if (find_command_idx(cmdname, &i) == 0) {
10929 current_command = &command_table[i];
10930 ret = command_table[i].func(argc - 1, newargv + 1);
10931 } else if (strchr(cmdname, '=')) {
10932 verify(find_command_idx("set", &i) == 0);
10933 current_command = &command_table[i];
10934 ret = command_table[i].func(argc, newargv);
10935 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
10936 /*
10937 * 'freeze' is a vile debugging abomination, so we treat
10938 * it as such.
10939 */
10940 zfs_cmd_t zc = {"\0"};
10941
10942 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
10943 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
10944 if (ret != 0) {
10945 (void) fprintf(stderr,
10946 gettext("failed to freeze pool: %d\n"), errno);
10947 ret = 1;
10948 }
10949
10950 log_history = 0;
10951 } else {
10952 (void) fprintf(stderr, gettext("unrecognized "
10953 "command '%s'\n"), cmdname);
10954 usage(B_FALSE);
10955 ret = 1;
10956 }
10957
10958 for (i = 0; i < argc; i++)
10959 free(newargv[i]);
10960 free(newargv);
10961
10962 if (ret == 0 && log_history)
10963 (void) zpool_log_history(g_zfs, history_str);
10964
10965 libzfs_fini(g_zfs);
10966
10967 /*
10968 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
10969 * for the purposes of running ::findleaks.
10970 */
10971 if (getenv("ZFS_ABORT") != NULL) {
10972 (void) printf("dumping core by request\n");
10973 abort();
10974 }
10975
10976 return (ret);
10977 }