1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
27 * Copyright (c) 2012 by Frederik Wessels. All rights reserved.
28 * Copyright (c) 2012 by Cyril Plisko. All rights reserved.
29 * Copyright (c) 2013 by Prasad Joshi (sTec). All rights reserved.
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
31 * Copyright (c) 2017 Datto Inc.
32 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
33 * Copyright (c) 2017, Intel Corporation.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
36 * Copyright (c) 2021, 2023, 2025, Klara, Inc.
37 * Copyright (c) 2021, 2025 Hewlett Packard Enterprise Development LP.
38 */
39
40 #include <assert.h>
41 #include <ctype.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <getopt.h>
46 #include <inttypes.h>
47 #include <libgen.h>
48 #include <libintl.h>
49 #include <locale.h>
50 #include <pthread.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <termios.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <pwd.h>
58 #include <zone.h>
59 #include <sys/wait.h>
60 #include <zfs_prop.h>
61 #include <sys/fs/zfs.h>
62 #include <sys/stat.h>
63 #include <sys/systeminfo.h>
64 #include <sys/fm/fs/zfs.h>
65 #include <sys/fm/util.h>
66 #include <sys/fm/protocol.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/mount.h>
69 #include <sys/sysmacros.h>
70 #include <string.h>
71 #include <math.h>
72
73 #include <libzfs.h>
74 #include <libzutil.h>
75
76 #include "zpool_util.h"
77 #include "zfs_comutil.h"
78 #include "zfeature_common.h"
79 #include "zfs_valstr.h"
80
81 #include "statcommon.h"
82
83 libzfs_handle_t *g_zfs;
84
85 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */
86
87 static int zpool_do_create(int, char **);
88 static int zpool_do_destroy(int, char **);
89
90 static int zpool_do_add(int, char **);
91 static int zpool_do_remove(int, char **);
92 static int zpool_do_labelclear(int, char **);
93
94 static int zpool_do_checkpoint(int, char **);
95 static int zpool_do_prefetch(int, char **);
96
97 static int zpool_do_list(int, char **);
98 static int zpool_do_iostat(int, char **);
99 static int zpool_do_status(int, char **);
100
101 static int zpool_do_online(int, char **);
102 static int zpool_do_offline(int, char **);
103 static int zpool_do_clear(int, char **);
104 static int zpool_do_reopen(int, char **);
105
106 static int zpool_do_reguid(int, char **);
107
108 static int zpool_do_attach(int, char **);
109 static int zpool_do_detach(int, char **);
110 static int zpool_do_replace(int, char **);
111 static int zpool_do_split(int, char **);
112
113 static int zpool_do_initialize(int, char **);
114 static int zpool_do_scrub(int, char **);
115 static int zpool_do_resilver(int, char **);
116 static int zpool_do_trim(int, char **);
117
118 static int zpool_do_import(int, char **);
119 static int zpool_do_export(int, char **);
120
121 static int zpool_do_upgrade(int, char **);
122
123 static int zpool_do_history(int, char **);
124 static int zpool_do_events(int, char **);
125
126 static int zpool_do_get(int, char **);
127 static int zpool_do_set(int, char **);
128
129 static int zpool_do_sync(int, char **);
130
131 static int zpool_do_version(int, char **);
132
133 static int zpool_do_wait(int, char **);
134
135 static int zpool_do_ddt_prune(int, char **);
136
137 static int zpool_do_help(int argc, char **argv);
138
139 static zpool_compat_status_t zpool_do_load_compat(
140 const char *, boolean_t *);
141
142 enum zpool_options {
143 ZPOOL_OPTION_POWER = 1024,
144 ZPOOL_OPTION_ALLOW_INUSE,
145 ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH,
146 ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH,
147 ZPOOL_OPTION_POOL_KEY_GUID,
148 ZPOOL_OPTION_JSON_NUMS_AS_INT,
149 ZPOOL_OPTION_JSON_FLAT_VDEVS
150 };
151
152 /*
153 * These libumem hooks provide a reasonable set of defaults for the allocator's
154 * debugging facilities.
155 */
156
157 #ifdef DEBUG
158 const char *
_umem_debug_init(void)159 _umem_debug_init(void)
160 {
161 return ("default,verbose"); /* $UMEM_DEBUG setting */
162 }
163
164 const char *
_umem_logging_init(void)165 _umem_logging_init(void)
166 {
167 return ("fail,contents"); /* $UMEM_LOGGING setting */
168 }
169 #endif
170
171 typedef enum {
172 HELP_ADD,
173 HELP_ATTACH,
174 HELP_CLEAR,
175 HELP_CREATE,
176 HELP_CHECKPOINT,
177 HELP_DDT_PRUNE,
178 HELP_DESTROY,
179 HELP_DETACH,
180 HELP_EXPORT,
181 HELP_HISTORY,
182 HELP_IMPORT,
183 HELP_IOSTAT,
184 HELP_LABELCLEAR,
185 HELP_LIST,
186 HELP_OFFLINE,
187 HELP_ONLINE,
188 HELP_PREFETCH,
189 HELP_REPLACE,
190 HELP_REMOVE,
191 HELP_INITIALIZE,
192 HELP_SCRUB,
193 HELP_RESILVER,
194 HELP_TRIM,
195 HELP_STATUS,
196 HELP_UPGRADE,
197 HELP_EVENTS,
198 HELP_GET,
199 HELP_SET,
200 HELP_SPLIT,
201 HELP_SYNC,
202 HELP_REGUID,
203 HELP_REOPEN,
204 HELP_VERSION,
205 HELP_WAIT
206 } zpool_help_t;
207
208
209 /*
210 * Flags for stats to display with "zpool iostats"
211 */
212 enum iostat_type {
213 IOS_DEFAULT = 0,
214 IOS_LATENCY = 1,
215 IOS_QUEUES = 2,
216 IOS_L_HISTO = 3,
217 IOS_RQ_HISTO = 4,
218 IOS_COUNT, /* always last element */
219 };
220
221 /* iostat_type entries as bitmasks */
222 #define IOS_DEFAULT_M (1ULL << IOS_DEFAULT)
223 #define IOS_LATENCY_M (1ULL << IOS_LATENCY)
224 #define IOS_QUEUES_M (1ULL << IOS_QUEUES)
225 #define IOS_L_HISTO_M (1ULL << IOS_L_HISTO)
226 #define IOS_RQ_HISTO_M (1ULL << IOS_RQ_HISTO)
227
228 /* Mask of all the histo bits */
229 #define IOS_ANYHISTO_M (IOS_L_HISTO_M | IOS_RQ_HISTO_M)
230
231 /*
232 * Lookup table for iostat flags to nvlist names. Basically a list
233 * of all the nvlists a flag requires. Also specifies the order in
234 * which data gets printed in zpool iostat.
235 */
236 static const char *vsx_type_to_nvlist[IOS_COUNT][15] = {
237 [IOS_L_HISTO] = {
238 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
239 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
240 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
241 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
242 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
243 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
244 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
245 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
246 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
247 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
248 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
249 NULL},
250 [IOS_LATENCY] = {
251 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
252 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
253 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
254 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
255 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
256 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
257 NULL},
258 [IOS_QUEUES] = {
259 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
260 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
261 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
262 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
263 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
264 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
265 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
266 NULL},
267 [IOS_RQ_HISTO] = {
268 ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
269 ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
270 ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
271 ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
272 ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
273 ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
274 ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
275 ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
276 ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
277 ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
278 ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
279 ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
280 ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
281 ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
282 NULL},
283 };
284
285 static const char *pool_scan_func_str[] = {
286 "NONE",
287 "SCRUB",
288 "RESILVER",
289 "ERRORSCRUB"
290 };
291
292 static const char *pool_scan_state_str[] = {
293 "NONE",
294 "SCANNING",
295 "FINISHED",
296 "CANCELED",
297 "ERRORSCRUBBING"
298 };
299
300 static const char *vdev_rebuild_state_str[] = {
301 "NONE",
302 "ACTIVE",
303 "CANCELED",
304 "COMPLETE"
305 };
306
307 static const char *checkpoint_state_str[] = {
308 "NONE",
309 "EXISTS",
310 "DISCARDING"
311 };
312
313 static const char *vdev_state_str[] = {
314 "UNKNOWN",
315 "CLOSED",
316 "OFFLINE",
317 "REMOVED",
318 "CANT_OPEN",
319 "FAULTED",
320 "DEGRADED",
321 "ONLINE"
322 };
323
324 static const char *vdev_aux_str[] = {
325 "NONE",
326 "OPEN_FAILED",
327 "CORRUPT_DATA",
328 "NO_REPLICAS",
329 "BAD_GUID_SUM",
330 "TOO_SMALL",
331 "BAD_LABEL",
332 "VERSION_NEWER",
333 "VERSION_OLDER",
334 "UNSUP_FEAT",
335 "SPARED",
336 "ERR_EXCEEDED",
337 "IO_FAILURE",
338 "BAD_LOG",
339 "EXTERNAL",
340 "SPLIT_POOL",
341 "BAD_ASHIFT",
342 "EXTERNAL_PERSIST",
343 "ACTIVE",
344 "CHILDREN_OFFLINE",
345 "ASHIFT_TOO_BIG"
346 };
347
348 static const char *vdev_init_state_str[] = {
349 "NONE",
350 "ACTIVE",
351 "CANCELED",
352 "SUSPENDED",
353 "COMPLETE"
354 };
355
356 static const char *vdev_trim_state_str[] = {
357 "NONE",
358 "ACTIVE",
359 "CANCELED",
360 "SUSPENDED",
361 "COMPLETE"
362 };
363
364 #define ZFS_NICE_TIMESTAMP 100
365
366 /*
367 * Given a cb->cb_flags with a histogram bit set, return the iostat_type.
368 * Right now, only one histo bit is ever set at one time, so we can
369 * just do a highbit64(a)
370 */
371 #define IOS_HISTO_IDX(a) (highbit64(a & IOS_ANYHISTO_M) - 1)
372
373 typedef struct zpool_command {
374 const char *name;
375 int (*func)(int, char **);
376 zpool_help_t usage;
377 } zpool_command_t;
378
379 /*
380 * Master command table. Each ZFS command has a name, associated function, and
381 * usage message. The usage messages need to be internationalized, so we have
382 * to have a function to return the usage message based on a command index.
383 *
384 * These commands are organized according to how they are displayed in the usage
385 * message. An empty command (one with a NULL name) indicates an empty line in
386 * the generic usage message.
387 */
388 static zpool_command_t command_table[] = {
389 { "version", zpool_do_version, HELP_VERSION },
390 { NULL },
391 { "create", zpool_do_create, HELP_CREATE },
392 { "destroy", zpool_do_destroy, HELP_DESTROY },
393 { NULL },
394 { "add", zpool_do_add, HELP_ADD },
395 { "remove", zpool_do_remove, HELP_REMOVE },
396 { NULL },
397 { "labelclear", zpool_do_labelclear, HELP_LABELCLEAR },
398 { NULL },
399 { "checkpoint", zpool_do_checkpoint, HELP_CHECKPOINT },
400 { "prefetch", zpool_do_prefetch, HELP_PREFETCH },
401 { NULL },
402 { "list", zpool_do_list, HELP_LIST },
403 { "iostat", zpool_do_iostat, HELP_IOSTAT },
404 { "status", zpool_do_status, HELP_STATUS },
405 { NULL },
406 { "online", zpool_do_online, HELP_ONLINE },
407 { "offline", zpool_do_offline, HELP_OFFLINE },
408 { "clear", zpool_do_clear, HELP_CLEAR },
409 { "reopen", zpool_do_reopen, HELP_REOPEN },
410 { NULL },
411 { "attach", zpool_do_attach, HELP_ATTACH },
412 { "detach", zpool_do_detach, HELP_DETACH },
413 { "replace", zpool_do_replace, HELP_REPLACE },
414 { "split", zpool_do_split, HELP_SPLIT },
415 { NULL },
416 { "initialize", zpool_do_initialize, HELP_INITIALIZE },
417 { "resilver", zpool_do_resilver, HELP_RESILVER },
418 { "scrub", zpool_do_scrub, HELP_SCRUB },
419 { "trim", zpool_do_trim, HELP_TRIM },
420 { NULL },
421 { "import", zpool_do_import, HELP_IMPORT },
422 { "export", zpool_do_export, HELP_EXPORT },
423 { "upgrade", zpool_do_upgrade, HELP_UPGRADE },
424 { "reguid", zpool_do_reguid, HELP_REGUID },
425 { NULL },
426 { "history", zpool_do_history, HELP_HISTORY },
427 { "events", zpool_do_events, HELP_EVENTS },
428 { NULL },
429 { "get", zpool_do_get, HELP_GET },
430 { "set", zpool_do_set, HELP_SET },
431 { "sync", zpool_do_sync, HELP_SYNC },
432 { NULL },
433 { "wait", zpool_do_wait, HELP_WAIT },
434 { NULL },
435 { "ddtprune", zpool_do_ddt_prune, HELP_DDT_PRUNE },
436 };
437
438 #define NCOMMAND (ARRAY_SIZE(command_table))
439
440 #define VDEV_ALLOC_CLASS_LOGS "logs"
441
442 #define MAX_CMD_LEN 256
443
444 static zpool_command_t *current_command;
445 static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
446 static char history_str[HIS_MAX_RECORD_LEN];
447 static boolean_t log_history = B_TRUE;
448 static uint_t timestamp_fmt = NODATE;
449
450 static const char *
get_usage(zpool_help_t idx)451 get_usage(zpool_help_t idx)
452 {
453 switch (idx) {
454 case HELP_ADD:
455 return (gettext("\tadd [-afgLnP] [-o property=value] "
456 "<pool> <vdev> ...\n"));
457 case HELP_ATTACH:
458 return (gettext("\tattach [-fsw] [-o property=value] "
459 "<pool> <vdev> <new-device>\n"));
460 case HELP_CLEAR:
461 return (gettext("\tclear [[--power]|[-nF]] <pool> [device]\n"));
462 case HELP_CREATE:
463 return (gettext("\tcreate [-fnd] [-o property=value] ... \n"
464 "\t [-O file-system-property=value] ... \n"
465 "\t [-m mountpoint] [-R root] <pool> <vdev> ...\n"));
466 case HELP_CHECKPOINT:
467 return (gettext("\tcheckpoint [-d [-w]] <pool> ...\n"));
468 case HELP_DESTROY:
469 return (gettext("\tdestroy [-f] <pool>\n"));
470 case HELP_DETACH:
471 return (gettext("\tdetach <pool> <device>\n"));
472 case HELP_EXPORT:
473 return (gettext("\texport [-af] <pool> ...\n"));
474 case HELP_HISTORY:
475 return (gettext("\thistory [-il] [<pool>] ...\n"));
476 case HELP_IMPORT:
477 return (gettext("\timport [-d dir] [-D]\n"
478 "\timport [-o mntopts] [-o property=value] ... \n"
479 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
480 "[-R root] [-F [-n]] -a\n"
481 "\timport [-o mntopts] [-o property=value] ... \n"
482 "\t [-d dir | -c cachefile] [-D] [-l] [-f] [-m] [-N] "
483 "[-R root] [-F [-n]]\n"
484 "\t [--rewind-to-checkpoint] <pool | id> [newpool]\n"));
485 case HELP_IOSTAT:
486 return (gettext("\tiostat [[[-c [script1,script2,...]"
487 "[-lq]]|[-rw]] [-T d | u] [-ghHLpPvy]\n"
488 "\t [[pool ...]|[pool vdev ...]|[vdev ...]]"
489 " [[-n] interval [count]]\n"));
490 case HELP_LABELCLEAR:
491 return (gettext("\tlabelclear [-f] <vdev>\n"));
492 case HELP_LIST:
493 return (gettext("\tlist [-gHLpPv] [-o property[,...]] [-j "
494 "[--json-int, --json-pool-key-guid]] ...\n"
495 "\t [-T d|u] [pool] [interval [count]]\n"));
496 case HELP_PREFETCH:
497 return (gettext("\tprefetch [-t <type>] <pool>\n"));
498 case HELP_OFFLINE:
499 return (gettext("\toffline [--power]|[[-f][-t]] <pool> "
500 "<device> ...\n"));
501 case HELP_ONLINE:
502 return (gettext("\tonline [--power][-e] <pool> <device> "
503 "...\n"));
504 case HELP_REPLACE:
505 return (gettext("\treplace [-fsw] [-o property=value] "
506 "<pool> <device> [new-device]\n"));
507 case HELP_REMOVE:
508 return (gettext("\tremove [-npsw] <pool> <device> ...\n"));
509 case HELP_REOPEN:
510 return (gettext("\treopen [-n] <pool>\n"));
511 case HELP_INITIALIZE:
512 return (gettext("\tinitialize [-c | -s | -u] [-w] <-a | <pool> "
513 "[<device> ...]>\n"));
514 case HELP_SCRUB:
515 return (gettext("\tscrub [-e | -s | -p | -C | -E | -S] [-w] "
516 "<-a | <pool> [<pool> ...]>\n"));
517 case HELP_RESILVER:
518 return (gettext("\tresilver <pool> ...\n"));
519 case HELP_TRIM:
520 return (gettext("\ttrim [-dw] [-r <rate>] [-c | -s] "
521 "<-a | <pool> [<device> ...]>\n"));
522 case HELP_STATUS:
523 return (gettext("\tstatus [-DdegiLPpstvx] "
524 "[-c script1[,script2,...]] ...\n"
525 "\t [-j|--json [--json-flat-vdevs] [--json-int] "
526 "[--json-pool-key-guid]] ...\n"
527 "\t [-T d|u] [--power] [pool] [interval [count]]\n"));
528 case HELP_UPGRADE:
529 return (gettext("\tupgrade\n"
530 "\tupgrade -v\n"
531 "\tupgrade [-V version] <-a | pool ...>\n"));
532 case HELP_EVENTS:
533 return (gettext("\tevents [-vHf [pool] | -c]\n"));
534 case HELP_GET:
535 return (gettext("\tget [-Hp] [-j [--json-int, "
536 "--json-pool-key-guid]] ...\n"
537 "\t [-o \"all\" | field[,...]] "
538 "<\"all\" | property[,...]> <pool> ...\n"));
539 case HELP_SET:
540 return (gettext("\tset <property=value> <pool>\n"
541 "\tset <vdev_property=value> <pool> <vdev>\n"));
542 case HELP_SPLIT:
543 return (gettext("\tsplit [-gLnPl] [-R altroot] [-o mntopts]\n"
544 "\t [-o property=value] <pool> <newpool> "
545 "[<device> ...]\n"));
546 case HELP_REGUID:
547 return (gettext("\treguid [-g guid] <pool>\n"));
548 case HELP_SYNC:
549 return (gettext("\tsync [pool] ...\n"));
550 case HELP_VERSION:
551 return (gettext("\tversion [-j]\n"));
552 case HELP_WAIT:
553 return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
554 "<pool> [interval]\n"));
555 case HELP_DDT_PRUNE:
556 return (gettext("\tddtprune -d|-p <amount> <pool>\n"));
557 default:
558 __builtin_unreachable();
559 }
560 }
561
562 /*
563 * Callback routine that will print out a pool property value.
564 */
565 static int
print_pool_prop_cb(int prop,void * cb)566 print_pool_prop_cb(int prop, void *cb)
567 {
568 FILE *fp = cb;
569
570 (void) fprintf(fp, "\t%-19s ", zpool_prop_to_name(prop));
571
572 if (zpool_prop_readonly(prop))
573 (void) fprintf(fp, " NO ");
574 else
575 (void) fprintf(fp, " YES ");
576
577 if (zpool_prop_values(prop) == NULL)
578 (void) fprintf(fp, "-\n");
579 else
580 (void) fprintf(fp, "%s\n", zpool_prop_values(prop));
581
582 return (ZPROP_CONT);
583 }
584
585 /*
586 * Callback routine that will print out a vdev property value.
587 */
588 static int
print_vdev_prop_cb(int prop,void * cb)589 print_vdev_prop_cb(int prop, void *cb)
590 {
591 FILE *fp = cb;
592
593 (void) fprintf(fp, "\t%-19s ", vdev_prop_to_name(prop));
594
595 if (vdev_prop_readonly(prop))
596 (void) fprintf(fp, " NO ");
597 else
598 (void) fprintf(fp, " YES ");
599
600 if (vdev_prop_values(prop) == NULL)
601 (void) fprintf(fp, "-\n");
602 else
603 (void) fprintf(fp, "%s\n", vdev_prop_values(prop));
604
605 return (ZPROP_CONT);
606 }
607
608 /*
609 * Given a leaf vdev name like 'L5' return its VDEV_CONFIG_PATH like
610 * '/dev/disk/by-vdev/L5'.
611 */
612 static const char *
vdev_name_to_path(zpool_handle_t * zhp,char * vdev)613 vdev_name_to_path(zpool_handle_t *zhp, char *vdev)
614 {
615 nvlist_t *vdev_nv = zpool_find_vdev(zhp, vdev, NULL, NULL, NULL);
616 if (vdev_nv == NULL) {
617 return (NULL);
618 }
619 return (fnvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_PATH));
620 }
621
622 static int
zpool_power_on(zpool_handle_t * zhp,char * vdev)623 zpool_power_on(zpool_handle_t *zhp, char *vdev)
624 {
625 return (zpool_power(zhp, vdev, B_TRUE));
626 }
627
628 static int
zpool_power_on_and_disk_wait(zpool_handle_t * zhp,char * vdev)629 zpool_power_on_and_disk_wait(zpool_handle_t *zhp, char *vdev)
630 {
631 int rc;
632
633 rc = zpool_power_on(zhp, vdev);
634 if (rc != 0)
635 return (rc);
636
637 (void) zpool_disk_wait(vdev_name_to_path(zhp, vdev));
638
639 return (0);
640 }
641
642 static int
zpool_power_on_pool_and_wait_for_devices(zpool_handle_t * zhp)643 zpool_power_on_pool_and_wait_for_devices(zpool_handle_t *zhp)
644 {
645 nvlist_t *nv;
646 const char *path = NULL;
647 int rc;
648
649 /* Power up all the devices first */
650 FOR_EACH_REAL_LEAF_VDEV(zhp, nv) {
651 path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
652 if (path != NULL) {
653 rc = zpool_power_on(zhp, (char *)path);
654 if (rc != 0) {
655 return (rc);
656 }
657 }
658 }
659
660 /*
661 * Wait for their devices to show up. Since we powered them on
662 * at roughly the same time, they should all come online around
663 * the same time.
664 */
665 FOR_EACH_REAL_LEAF_VDEV(zhp, nv) {
666 path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
667 (void) zpool_disk_wait(path);
668 }
669
670 return (0);
671 }
672
673 static int
zpool_power_off(zpool_handle_t * zhp,char * vdev)674 zpool_power_off(zpool_handle_t *zhp, char *vdev)
675 {
676 return (zpool_power(zhp, vdev, B_FALSE));
677 }
678
679 /*
680 * Display usage message. If we're inside a command, display only the usage for
681 * that command. Otherwise, iterate over the entire command table and display
682 * a complete usage message.
683 */
684 static __attribute__((noreturn)) void
usage(boolean_t requested)685 usage(boolean_t requested)
686 {
687 FILE *fp = requested ? stdout : stderr;
688
689 if (current_command == NULL) {
690 int i;
691
692 (void) fprintf(fp, gettext("usage: zpool command args ...\n"));
693 (void) fprintf(fp,
694 gettext("where 'command' is one of the following:\n\n"));
695
696 for (i = 0; i < NCOMMAND; i++) {
697 if (command_table[i].name == NULL)
698 (void) fprintf(fp, "\n");
699 else
700 (void) fprintf(fp, "%s",
701 get_usage(command_table[i].usage));
702 }
703
704 (void) fprintf(fp,
705 gettext("\nFor further help on a command or topic, "
706 "run: %s\n"), "zpool help [<topic>]");
707 } else {
708 (void) fprintf(fp, gettext("usage:\n"));
709 (void) fprintf(fp, "%s", get_usage(current_command->usage));
710 }
711
712 if (current_command != NULL &&
713 current_prop_type != (ZFS_TYPE_POOL | ZFS_TYPE_VDEV) &&
714 ((strcmp(current_command->name, "set") == 0) ||
715 (strcmp(current_command->name, "get") == 0) ||
716 (strcmp(current_command->name, "list") == 0))) {
717
718 (void) fprintf(fp, "%s",
719 gettext("\nthe following properties are supported:\n"));
720
721 (void) fprintf(fp, "\n\t%-19s %s %s\n\n",
722 "PROPERTY", "EDIT", "VALUES");
723
724 /* Iterate over all properties */
725 if (current_prop_type == ZFS_TYPE_POOL) {
726 (void) zprop_iter(print_pool_prop_cb, fp, B_FALSE,
727 B_TRUE, current_prop_type);
728
729 (void) fprintf(fp, "\t%-19s ", "feature@...");
730 (void) fprintf(fp, "YES "
731 "disabled | enabled | active\n");
732
733 (void) fprintf(fp, gettext("\nThe feature@ properties "
734 "must be appended with a feature name.\n"
735 "See zpool-features(7).\n"));
736 } else if (current_prop_type == ZFS_TYPE_VDEV) {
737 (void) zprop_iter(print_vdev_prop_cb, fp, B_FALSE,
738 B_TRUE, current_prop_type);
739 }
740 }
741
742 /*
743 * See comments at end of main().
744 */
745 if (getenv("ZFS_ABORT") != NULL) {
746 (void) printf("dumping core by request\n");
747 abort();
748 }
749
750 exit(requested ? 0 : 2);
751 }
752
753 /*
754 * zpool initialize [-c | -s | -u] [-w] <-a | pool> [<vdev> ...]
755 * Initialize all unused blocks in the specified vdevs, or all vdevs in the pool
756 * if none specified.
757 *
758 * -a Use all pools.
759 * -c Cancel. Ends active initializing.
760 * -s Suspend. Initializing can then be restarted with no flags.
761 * -u Uninitialize. Clears initialization state.
762 * -w Wait. Blocks until initializing has completed.
763 */
764 int
zpool_do_initialize(int argc,char ** argv)765 zpool_do_initialize(int argc, char **argv)
766 {
767 int c;
768 char *poolname;
769 zpool_handle_t *zhp;
770 int err = 0;
771 boolean_t wait = B_FALSE;
772 boolean_t initialize_all = B_FALSE;
773
774 struct option long_options[] = {
775 {"cancel", no_argument, NULL, 'c'},
776 {"suspend", no_argument, NULL, 's'},
777 {"uninit", no_argument, NULL, 'u'},
778 {"wait", no_argument, NULL, 'w'},
779 {"all", no_argument, NULL, 'a'},
780 {0, 0, 0, 0}
781 };
782
783 pool_initialize_func_t cmd_type = POOL_INITIALIZE_START;
784 while ((c = getopt_long(argc, argv, "acsuw", long_options,
785 NULL)) != -1) {
786 switch (c) {
787 case 'a':
788 initialize_all = B_TRUE;
789 break;
790 case 'c':
791 if (cmd_type != POOL_INITIALIZE_START &&
792 cmd_type != POOL_INITIALIZE_CANCEL) {
793 (void) fprintf(stderr, gettext("-c cannot be "
794 "combined with other options\n"));
795 usage(B_FALSE);
796 }
797 cmd_type = POOL_INITIALIZE_CANCEL;
798 break;
799 case 's':
800 if (cmd_type != POOL_INITIALIZE_START &&
801 cmd_type != POOL_INITIALIZE_SUSPEND) {
802 (void) fprintf(stderr, gettext("-s cannot be "
803 "combined with other options\n"));
804 usage(B_FALSE);
805 }
806 cmd_type = POOL_INITIALIZE_SUSPEND;
807 break;
808 case 'u':
809 if (cmd_type != POOL_INITIALIZE_START &&
810 cmd_type != POOL_INITIALIZE_UNINIT) {
811 (void) fprintf(stderr, gettext("-u cannot be "
812 "combined with other options\n"));
813 usage(B_FALSE);
814 }
815 cmd_type = POOL_INITIALIZE_UNINIT;
816 break;
817 case 'w':
818 wait = B_TRUE;
819 break;
820 case '?':
821 if (optopt != 0) {
822 (void) fprintf(stderr,
823 gettext("invalid option '%c'\n"), optopt);
824 } else {
825 (void) fprintf(stderr,
826 gettext("invalid option '%s'\n"),
827 argv[optind - 1]);
828 }
829 usage(B_FALSE);
830 }
831 }
832
833 argc -= optind;
834 argv += optind;
835
836 initialize_cbdata_t cbdata = {
837 .wait = wait,
838 .cmd_type = cmd_type
839 };
840
841 if (initialize_all && argc > 0) {
842 (void) fprintf(stderr, gettext("-a cannot be combined with "
843 "individual pools or vdevs\n"));
844 usage(B_FALSE);
845 }
846
847 if (argc < 1 && !initialize_all) {
848 (void) fprintf(stderr, gettext("missing pool name argument\n"));
849 usage(B_FALSE);
850 }
851
852 if (wait && (cmd_type != POOL_INITIALIZE_START)) {
853 (void) fprintf(stderr, gettext("-w cannot be used with -c, -s"
854 "or -u\n"));
855 usage(B_FALSE);
856 }
857
858 if (argc == 0 && initialize_all) {
859 /* Initilize each pool recursively */
860 err = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
861 B_FALSE, zpool_initialize_one, &cbdata);
862 return (err);
863 } else if (argc == 1) {
864 /* no individual leaf vdevs specified, initialize the pool */
865 poolname = argv[0];
866 zhp = zpool_open(g_zfs, poolname);
867 if (zhp == NULL)
868 return (-1);
869 err = zpool_initialize_one(zhp, &cbdata);
870 } else {
871 /* individual leaf vdevs specified, initialize them */
872 poolname = argv[0];
873 zhp = zpool_open(g_zfs, poolname);
874 if (zhp == NULL)
875 return (-1);
876 nvlist_t *vdevs = fnvlist_alloc();
877 for (int i = 1; i < argc; i++) {
878 fnvlist_add_boolean(vdevs, argv[i]);
879 }
880 if (wait)
881 err = zpool_initialize_wait(zhp, cmd_type, vdevs);
882 else
883 err = zpool_initialize(zhp, cmd_type, vdevs);
884 fnvlist_free(vdevs);
885 }
886
887 zpool_close(zhp);
888
889 return (err);
890 }
891
892 /*
893 * print a pool vdev config for dry runs
894 */
895 static void
print_vdev_tree(zpool_handle_t * zhp,const char * name,nvlist_t * nv,int indent,const char * match,int name_flags)896 print_vdev_tree(zpool_handle_t *zhp, const char *name, nvlist_t *nv, int indent,
897 const char *match, int name_flags)
898 {
899 nvlist_t **child;
900 uint_t c, children;
901 char *vname;
902 boolean_t printed = B_FALSE;
903
904 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
905 &child, &children) != 0) {
906 if (name != NULL)
907 (void) printf("\t%*s%s\n", indent, "", name);
908 return;
909 }
910
911 for (c = 0; c < children; c++) {
912 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
913 const char *class = "";
914
915 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
916 &is_hole);
917
918 if (is_hole == B_TRUE) {
919 continue;
920 }
921
922 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
923 &is_log);
924 if (is_log)
925 class = VDEV_ALLOC_BIAS_LOG;
926 (void) nvlist_lookup_string(child[c],
927 ZPOOL_CONFIG_ALLOCATION_BIAS, &class);
928 if (strcmp(match, class) != 0)
929 continue;
930
931 if (!printed && name != NULL) {
932 (void) printf("\t%*s%s\n", indent, "", name);
933 printed = B_TRUE;
934 }
935 vname = zpool_vdev_name(g_zfs, zhp, child[c], name_flags);
936 print_vdev_tree(zhp, vname, child[c], indent + 2, "",
937 name_flags);
938 free(vname);
939 }
940 }
941
942 /*
943 * Print the list of l2cache devices for dry runs.
944 */
945 static void
print_cache_list(nvlist_t * nv,int indent)946 print_cache_list(nvlist_t *nv, int indent)
947 {
948 nvlist_t **child;
949 uint_t c, children;
950
951 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
952 &child, &children) == 0 && children > 0) {
953 (void) printf("\t%*s%s\n", indent, "", "cache");
954 } else {
955 return;
956 }
957 for (c = 0; c < children; c++) {
958 char *vname;
959
960 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
961 (void) printf("\t%*s%s\n", indent + 2, "", vname);
962 free(vname);
963 }
964 }
965
966 /*
967 * Print the list of spares for dry runs.
968 */
969 static void
print_spare_list(nvlist_t * nv,int indent)970 print_spare_list(nvlist_t *nv, int indent)
971 {
972 nvlist_t **child;
973 uint_t c, children;
974
975 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
976 &child, &children) == 0 && children > 0) {
977 (void) printf("\t%*s%s\n", indent, "", "spares");
978 } else {
979 return;
980 }
981 for (c = 0; c < children; c++) {
982 char *vname;
983
984 vname = zpool_vdev_name(g_zfs, NULL, child[c], 0);
985 (void) printf("\t%*s%s\n", indent + 2, "", vname);
986 free(vname);
987 }
988 }
989
990 typedef struct spare_cbdata {
991 uint64_t cb_guid;
992 zpool_handle_t *cb_zhp;
993 } spare_cbdata_t;
994
995 static boolean_t
find_vdev(nvlist_t * nv,uint64_t search)996 find_vdev(nvlist_t *nv, uint64_t search)
997 {
998 uint64_t guid;
999 nvlist_t **child;
1000 uint_t c, children;
1001
1002 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0 &&
1003 search == guid)
1004 return (B_TRUE);
1005
1006 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1007 &child, &children) == 0) {
1008 for (c = 0; c < children; c++)
1009 if (find_vdev(child[c], search))
1010 return (B_TRUE);
1011 }
1012
1013 return (B_FALSE);
1014 }
1015
1016 static int
find_spare(zpool_handle_t * zhp,void * data)1017 find_spare(zpool_handle_t *zhp, void *data)
1018 {
1019 spare_cbdata_t *cbp = data;
1020 nvlist_t *config, *nvroot;
1021
1022 config = zpool_get_config(zhp, NULL);
1023 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1024 &nvroot) == 0);
1025
1026 if (find_vdev(nvroot, cbp->cb_guid)) {
1027 cbp->cb_zhp = zhp;
1028 return (1);
1029 }
1030
1031 zpool_close(zhp);
1032 return (0);
1033 }
1034
1035 static void
nice_num_str_nvlist(nvlist_t * item,const char * key,uint64_t value,boolean_t literal,boolean_t as_int,int format)1036 nice_num_str_nvlist(nvlist_t *item, const char *key, uint64_t value,
1037 boolean_t literal, boolean_t as_int, int format)
1038 {
1039 char buf[256];
1040
1041 if (literal) {
1042 if (!as_int)
1043 (void) snprintf(buf, 256, "%llu", (u_longlong_t)value);
1044 } else {
1045 switch (format) {
1046 case ZFS_NICENUM_1024:
1047 zfs_nicenum_format(value, buf, 256, ZFS_NICENUM_1024);
1048 break;
1049 case ZFS_NICENUM_BYTES:
1050 zfs_nicenum_format(value, buf, 256, ZFS_NICENUM_BYTES);
1051 break;
1052 case ZFS_NICENUM_TIME:
1053 zfs_nicenum_format(value, buf, 256, ZFS_NICENUM_TIME);
1054 break;
1055 case ZFS_NICE_TIMESTAMP:
1056 format_timestamp(value, buf, 256);
1057 break;
1058 default:
1059 fprintf(stderr, "Invalid number format");
1060 exit(1);
1061 }
1062 }
1063 if (as_int)
1064 fnvlist_add_uint64(item, key, value);
1065 else
1066 fnvlist_add_string(item, key, buf);
1067 }
1068
1069 /*
1070 * Generates an nvlist with output version for every command based on params.
1071 * Purpose of this is to add a version of JSON output, considering the schema
1072 * format might be updated for each command in future.
1073 *
1074 * Schema:
1075 *
1076 * "output_version": {
1077 * "command": string,
1078 * "vers_major": integer,
1079 * "vers_minor": integer,
1080 * }
1081 */
1082 static nvlist_t *
zpool_json_schema(int maj_v,int min_v)1083 zpool_json_schema(int maj_v, int min_v)
1084 {
1085 char cmd[MAX_CMD_LEN];
1086 nvlist_t *sch = fnvlist_alloc();
1087 nvlist_t *ov = fnvlist_alloc();
1088
1089 (void) snprintf(cmd, MAX_CMD_LEN, "zpool %s", current_command->name);
1090 fnvlist_add_string(ov, "command", cmd);
1091 fnvlist_add_uint32(ov, "vers_major", maj_v);
1092 fnvlist_add_uint32(ov, "vers_minor", min_v);
1093 fnvlist_add_nvlist(sch, "output_version", ov);
1094 fnvlist_free(ov);
1095 return (sch);
1096 }
1097
1098 static void
fill_pool_info(nvlist_t * list,zpool_handle_t * zhp,boolean_t addtype,boolean_t as_int)1099 fill_pool_info(nvlist_t *list, zpool_handle_t *zhp, boolean_t addtype,
1100 boolean_t as_int)
1101 {
1102 nvlist_t *config = zpool_get_config(zhp, NULL);
1103 uint64_t guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
1104 uint64_t txg = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG);
1105
1106 fnvlist_add_string(list, "name", zpool_get_name(zhp));
1107 if (addtype)
1108 fnvlist_add_string(list, "type", "POOL");
1109 fnvlist_add_string(list, "state", zpool_get_state_str(zhp));
1110 if (as_int) {
1111 if (guid)
1112 fnvlist_add_uint64(list, ZPOOL_CONFIG_POOL_GUID, guid);
1113 if (txg)
1114 fnvlist_add_uint64(list, ZPOOL_CONFIG_POOL_TXG, txg);
1115 fnvlist_add_uint64(list, "spa_version", SPA_VERSION);
1116 fnvlist_add_uint64(list, "zpl_version", ZPL_VERSION);
1117 } else {
1118 char value[ZFS_MAXPROPLEN];
1119 if (guid) {
1120 (void) snprintf(value, ZFS_MAXPROPLEN, "%llu",
1121 (u_longlong_t)guid);
1122 fnvlist_add_string(list, ZPOOL_CONFIG_POOL_GUID, value);
1123 }
1124 if (txg) {
1125 (void) snprintf(value, ZFS_MAXPROPLEN, "%llu",
1126 (u_longlong_t)txg);
1127 fnvlist_add_string(list, ZPOOL_CONFIG_POOL_TXG, value);
1128 }
1129 fnvlist_add_string(list, "spa_version", SPA_VERSION_STRING);
1130 fnvlist_add_string(list, "zpl_version", ZPL_VERSION_STRING);
1131 }
1132 }
1133
1134 static void
used_by_other(zpool_handle_t * zhp,nvlist_t * nvdev,nvlist_t * list)1135 used_by_other(zpool_handle_t *zhp, nvlist_t *nvdev, nvlist_t *list)
1136 {
1137 spare_cbdata_t spare_cb;
1138 verify(nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_GUID,
1139 &spare_cb.cb_guid) == 0);
1140 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
1141 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
1142 zpool_get_name(zhp)) != 0) {
1143 fnvlist_add_string(list, "used_by",
1144 zpool_get_name(spare_cb.cb_zhp));
1145 }
1146 zpool_close(spare_cb.cb_zhp);
1147 }
1148 }
1149
1150 static void
fill_vdev_info(nvlist_t * list,zpool_handle_t * zhp,char * name,boolean_t addtype,boolean_t as_int)1151 fill_vdev_info(nvlist_t *list, zpool_handle_t *zhp, char *name,
1152 boolean_t addtype, boolean_t as_int)
1153 {
1154 boolean_t l2c = B_FALSE;
1155 const char *path, *phys, *devid, *bias = NULL;
1156 uint64_t hole = 0, log = 0, spare = 0;
1157 vdev_stat_t *vs;
1158 uint_t c;
1159 nvlist_t *nvdev;
1160 nvlist_t *nvdev_parent = NULL;
1161 char *_name;
1162
1163 if (strcmp(name, zpool_get_name(zhp)) != 0)
1164 _name = name;
1165 else
1166 _name = (char *)"root-0";
1167
1168 nvdev = zpool_find_vdev(zhp, _name, NULL, &l2c, NULL);
1169
1170 fnvlist_add_string(list, "name", name);
1171 if (addtype)
1172 fnvlist_add_string(list, "type", "VDEV");
1173 if (nvdev) {
1174 const char *type = fnvlist_lookup_string(nvdev,
1175 ZPOOL_CONFIG_TYPE);
1176 if (type)
1177 fnvlist_add_string(list, "vdev_type", type);
1178 uint64_t guid = fnvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_GUID);
1179 if (guid) {
1180 if (as_int) {
1181 fnvlist_add_uint64(list, "guid", guid);
1182 } else {
1183 char buf[ZFS_MAXPROPLEN];
1184 (void) snprintf(buf, ZFS_MAXPROPLEN, "%llu",
1185 (u_longlong_t)guid);
1186 fnvlist_add_string(list, "guid", buf);
1187 }
1188 }
1189 if (nvlist_lookup_string(nvdev, ZPOOL_CONFIG_PATH, &path) == 0)
1190 fnvlist_add_string(list, "path", path);
1191 if (nvlist_lookup_string(nvdev, ZPOOL_CONFIG_PHYS_PATH,
1192 &phys) == 0)
1193 fnvlist_add_string(list, "phys_path", phys);
1194 if (nvlist_lookup_string(nvdev, ZPOOL_CONFIG_DEVID,
1195 &devid) == 0)
1196 fnvlist_add_string(list, "devid", devid);
1197 (void) nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_IS_LOG, &log);
1198 (void) nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_IS_SPARE,
1199 &spare);
1200 (void) nvlist_lookup_uint64(nvdev, ZPOOL_CONFIG_IS_HOLE, &hole);
1201 if (hole)
1202 fnvlist_add_string(list, "class", VDEV_TYPE_HOLE);
1203 else if (l2c)
1204 fnvlist_add_string(list, "class", VDEV_TYPE_L2CACHE);
1205 else if (spare)
1206 fnvlist_add_string(list, "class", VDEV_TYPE_SPARE);
1207 else if (log)
1208 fnvlist_add_string(list, "class", VDEV_TYPE_LOG);
1209 else {
1210 (void) nvlist_lookup_string(nvdev,
1211 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
1212 if (bias != NULL)
1213 fnvlist_add_string(list, "class", bias);
1214 else {
1215 nvdev_parent = NULL;
1216 nvdev_parent = zpool_find_parent_vdev(zhp,
1217 _name, NULL, NULL, NULL);
1218
1219 /*
1220 * With a mirrored special device, the parent
1221 * "mirror" vdev will have
1222 * ZPOOL_CONFIG_ALLOCATION_BIAS set to "special"
1223 * not the leaf vdevs. If we're a leaf vdev
1224 * in that case we need to look at our parent
1225 * to see if they're "special" to know if we
1226 * are "special" too.
1227 */
1228 if (nvdev_parent) {
1229 (void) nvlist_lookup_string(
1230 nvdev_parent,
1231 ZPOOL_CONFIG_ALLOCATION_BIAS,
1232 &bias);
1233 }
1234 if (bias != NULL)
1235 fnvlist_add_string(list, "class", bias);
1236 else
1237 fnvlist_add_string(list, "class",
1238 "normal");
1239 }
1240 }
1241 if (nvlist_lookup_uint64_array(nvdev, ZPOOL_CONFIG_VDEV_STATS,
1242 (uint64_t **)&vs, &c) == 0) {
1243 fnvlist_add_string(list, "state",
1244 vdev_state_str[vs->vs_state]);
1245 }
1246 }
1247 }
1248
1249 static boolean_t
prop_list_contains_feature(nvlist_t * proplist)1250 prop_list_contains_feature(nvlist_t *proplist)
1251 {
1252 nvpair_t *nvp;
1253 for (nvp = nvlist_next_nvpair(proplist, NULL); NULL != nvp;
1254 nvp = nvlist_next_nvpair(proplist, nvp)) {
1255 if (zpool_prop_feature(nvpair_name(nvp)))
1256 return (B_TRUE);
1257 }
1258 return (B_FALSE);
1259 }
1260
1261 /*
1262 * Add a property pair (name, string-value) into a property nvlist.
1263 */
1264 static int
add_prop_list(const char * propname,const char * propval,nvlist_t ** props,boolean_t poolprop)1265 add_prop_list(const char *propname, const char *propval, nvlist_t **props,
1266 boolean_t poolprop)
1267 {
1268 zpool_prop_t prop = ZPOOL_PROP_INVAL;
1269 nvlist_t *proplist;
1270 const char *normnm;
1271 const char *strval;
1272
1273 if (*props == NULL &&
1274 nvlist_alloc(props, NV_UNIQUE_NAME, 0) != 0) {
1275 (void) fprintf(stderr,
1276 gettext("internal error: out of memory\n"));
1277 return (1);
1278 }
1279
1280 proplist = *props;
1281
1282 if (poolprop) {
1283 const char *vname = zpool_prop_to_name(ZPOOL_PROP_VERSION);
1284 const char *cname =
1285 zpool_prop_to_name(ZPOOL_PROP_COMPATIBILITY);
1286
1287 if ((prop = zpool_name_to_prop(propname)) == ZPOOL_PROP_INVAL &&
1288 (!zpool_prop_feature(propname) &&
1289 !zpool_prop_vdev(propname))) {
1290 (void) fprintf(stderr, gettext("property '%s' is "
1291 "not a valid pool or vdev property\n"), propname);
1292 return (2);
1293 }
1294
1295 /*
1296 * feature@ properties and version should not be specified
1297 * at the same time.
1298 */
1299 if ((prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname) &&
1300 nvlist_exists(proplist, vname)) ||
1301 (prop == ZPOOL_PROP_VERSION &&
1302 prop_list_contains_feature(proplist))) {
1303 (void) fprintf(stderr, gettext("'feature@' and "
1304 "'version' properties cannot be specified "
1305 "together\n"));
1306 return (2);
1307 }
1308
1309 /*
1310 * if version is specified, only "legacy" compatibility
1311 * may be requested
1312 */
1313 if ((prop == ZPOOL_PROP_COMPATIBILITY &&
1314 strcmp(propval, ZPOOL_COMPAT_LEGACY) != 0 &&
1315 nvlist_exists(proplist, vname)) ||
1316 (prop == ZPOOL_PROP_VERSION &&
1317 nvlist_exists(proplist, cname) &&
1318 strcmp(fnvlist_lookup_string(proplist, cname),
1319 ZPOOL_COMPAT_LEGACY) != 0)) {
1320 (void) fprintf(stderr, gettext("when 'version' is "
1321 "specified, the 'compatibility' feature may only "
1322 "be set to '" ZPOOL_COMPAT_LEGACY "'\n"));
1323 return (2);
1324 }
1325
1326 if (zpool_prop_feature(propname) || zpool_prop_vdev(propname))
1327 normnm = propname;
1328 else
1329 normnm = zpool_prop_to_name(prop);
1330 } else {
1331 zfs_prop_t fsprop = zfs_name_to_prop(propname);
1332
1333 if (zfs_prop_valid_for_type(fsprop, ZFS_TYPE_FILESYSTEM,
1334 B_FALSE)) {
1335 normnm = zfs_prop_to_name(fsprop);
1336 } else if (zfs_prop_user(propname) ||
1337 zfs_prop_userquota(propname)) {
1338 normnm = propname;
1339 } else {
1340 (void) fprintf(stderr, gettext("property '%s' is "
1341 "not a valid filesystem property\n"), propname);
1342 return (2);
1343 }
1344 }
1345
1346 if (nvlist_lookup_string(proplist, normnm, &strval) == 0 &&
1347 prop != ZPOOL_PROP_CACHEFILE) {
1348 (void) fprintf(stderr, gettext("property '%s' "
1349 "specified multiple times\n"), propname);
1350 return (2);
1351 }
1352
1353 if (nvlist_add_string(proplist, normnm, propval) != 0) {
1354 (void) fprintf(stderr, gettext("internal "
1355 "error: out of memory\n"));
1356 return (1);
1357 }
1358
1359 return (0);
1360 }
1361
1362 /*
1363 * Set a default property pair (name, string-value) in a property nvlist
1364 */
1365 static int
add_prop_list_default(const char * propname,const char * propval,nvlist_t ** props)1366 add_prop_list_default(const char *propname, const char *propval,
1367 nvlist_t **props)
1368 {
1369 const char *pval;
1370
1371 if (nvlist_lookup_string(*props, propname, &pval) == 0)
1372 return (0);
1373
1374 return (add_prop_list(propname, propval, props, B_TRUE));
1375 }
1376
1377 /*
1378 * zpool add [-afgLnP] [-o property=value] <pool> <vdev> ...
1379 *
1380 * -a Disable the ashift validation checks
1381 * -f Force addition of devices, even if they appear in use
1382 * -g Display guid for individual vdev name.
1383 * -L Follow links when resolving vdev path name.
1384 * -n Do not add the devices, but display the resulting layout if
1385 * they were to be added.
1386 * -o Set property=value.
1387 * -P Display full path for vdev name.
1388 *
1389 * Adds the given vdevs to 'pool'. As with create, the bulk of this work is
1390 * handled by make_root_vdev(), which constructs the nvlist needed to pass to
1391 * libzfs.
1392 */
1393 int
zpool_do_add(int argc,char ** argv)1394 zpool_do_add(int argc, char **argv)
1395 {
1396 boolean_t check_replication = B_TRUE;
1397 boolean_t check_inuse = B_TRUE;
1398 boolean_t dryrun = B_FALSE;
1399 boolean_t check_ashift = B_TRUE;
1400 boolean_t force = B_FALSE;
1401 int name_flags = 0;
1402 int c;
1403 nvlist_t *nvroot;
1404 char *poolname;
1405 int ret;
1406 zpool_handle_t *zhp;
1407 nvlist_t *config;
1408 nvlist_t *props = NULL;
1409 char *propval;
1410
1411 struct option long_options[] = {
1412 {"allow-in-use", no_argument, NULL, ZPOOL_OPTION_ALLOW_INUSE},
1413 {"allow-replication-mismatch", no_argument, NULL,
1414 ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH},
1415 {"allow-ashift-mismatch", no_argument, NULL,
1416 ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH},
1417 {0, 0, 0, 0}
1418 };
1419
1420 /* check options */
1421 while ((c = getopt_long(argc, argv, "fgLno:P", long_options, NULL))
1422 != -1) {
1423 switch (c) {
1424 case 'f':
1425 force = B_TRUE;
1426 break;
1427 case 'g':
1428 name_flags |= VDEV_NAME_GUID;
1429 break;
1430 case 'L':
1431 name_flags |= VDEV_NAME_FOLLOW_LINKS;
1432 break;
1433 case 'n':
1434 dryrun = B_TRUE;
1435 break;
1436 case 'o':
1437 if ((propval = strchr(optarg, '=')) == NULL) {
1438 (void) fprintf(stderr, gettext("missing "
1439 "'=' for -o option\n"));
1440 usage(B_FALSE);
1441 }
1442 *propval = '\0';
1443 propval++;
1444
1445 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
1446 (add_prop_list(optarg, propval, &props, B_TRUE)))
1447 usage(B_FALSE);
1448 break;
1449 case 'P':
1450 name_flags |= VDEV_NAME_PATH;
1451 break;
1452 case ZPOOL_OPTION_ALLOW_INUSE:
1453 check_inuse = B_FALSE;
1454 break;
1455 case ZPOOL_OPTION_ALLOW_REPLICATION_MISMATCH:
1456 check_replication = B_FALSE;
1457 break;
1458 case ZPOOL_OPTION_ALLOW_ASHIFT_MISMATCH:
1459 check_ashift = B_FALSE;
1460 break;
1461 case '?':
1462 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1463 optopt);
1464 usage(B_FALSE);
1465 }
1466 }
1467
1468 argc -= optind;
1469 argv += optind;
1470
1471 /* get pool name and check number of arguments */
1472 if (argc < 1) {
1473 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1474 usage(B_FALSE);
1475 }
1476 if (argc < 2) {
1477 (void) fprintf(stderr, gettext("missing vdev specification\n"));
1478 usage(B_FALSE);
1479 }
1480
1481 if (force) {
1482 if (!check_inuse || !check_replication || !check_ashift) {
1483 (void) fprintf(stderr, gettext("'-f' option is not "
1484 "allowed with '--allow-replication-mismatch', "
1485 "'--allow-ashift-mismatch', or "
1486 "'--allow-in-use'\n"));
1487 usage(B_FALSE);
1488 }
1489 check_inuse = B_FALSE;
1490 check_replication = B_FALSE;
1491 check_ashift = B_FALSE;
1492 }
1493
1494 poolname = argv[0];
1495
1496 argc--;
1497 argv++;
1498
1499 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1500 return (1);
1501
1502 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
1503 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
1504 poolname);
1505 zpool_close(zhp);
1506 return (1);
1507 }
1508
1509 /* unless manually specified use "ashift" pool property (if set) */
1510 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
1511 int intval;
1512 zprop_source_t src;
1513 char strval[ZPOOL_MAXPROPLEN];
1514
1515 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
1516 if (src != ZPROP_SRC_DEFAULT) {
1517 (void) sprintf(strval, "%" PRId32, intval);
1518 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
1519 &props, B_TRUE) == 0);
1520 }
1521 }
1522
1523 /* pass off to make_root_vdev for processing */
1524 nvroot = make_root_vdev(zhp, props, !check_inuse,
1525 check_replication, B_FALSE, dryrun, argc, argv);
1526 if (nvroot == NULL) {
1527 zpool_close(zhp);
1528 return (1);
1529 }
1530
1531 if (dryrun) {
1532 nvlist_t *poolnvroot;
1533 nvlist_t **l2child, **sparechild;
1534 uint_t l2children, sparechildren, c;
1535 char *vname;
1536 boolean_t hadcache = B_FALSE, hadspare = B_FALSE;
1537
1538 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1539 &poolnvroot) == 0);
1540
1541 (void) printf(gettext("would update '%s' to the following "
1542 "configuration:\n\n"), zpool_get_name(zhp));
1543
1544 /* print original main pool and new tree */
1545 print_vdev_tree(zhp, poolname, poolnvroot, 0, "",
1546 name_flags | VDEV_NAME_TYPE_ID);
1547 print_vdev_tree(zhp, NULL, nvroot, 0, "", name_flags);
1548
1549 /* print other classes: 'dedup', 'special', and 'log' */
1550 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1551 print_vdev_tree(zhp, "dedup", poolnvroot, 0,
1552 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1553 print_vdev_tree(zhp, NULL, nvroot, 0,
1554 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1555 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_DEDUP)) {
1556 print_vdev_tree(zhp, "dedup", nvroot, 0,
1557 VDEV_ALLOC_BIAS_DEDUP, name_flags);
1558 }
1559
1560 if (zfs_special_devs(poolnvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1561 print_vdev_tree(zhp, "special", poolnvroot, 0,
1562 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1563 print_vdev_tree(zhp, NULL, nvroot, 0,
1564 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1565 } else if (zfs_special_devs(nvroot, VDEV_ALLOC_BIAS_SPECIAL)) {
1566 print_vdev_tree(zhp, "special", nvroot, 0,
1567 VDEV_ALLOC_BIAS_SPECIAL, name_flags);
1568 }
1569
1570 if (num_logs(poolnvroot) > 0) {
1571 print_vdev_tree(zhp, "logs", poolnvroot, 0,
1572 VDEV_ALLOC_BIAS_LOG, name_flags);
1573 print_vdev_tree(zhp, NULL, nvroot, 0,
1574 VDEV_ALLOC_BIAS_LOG, name_flags);
1575 } else if (num_logs(nvroot) > 0) {
1576 print_vdev_tree(zhp, "logs", nvroot, 0,
1577 VDEV_ALLOC_BIAS_LOG, name_flags);
1578 }
1579
1580 /* Do the same for the caches */
1581 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_L2CACHE,
1582 &l2child, &l2children) == 0 && l2children) {
1583 hadcache = B_TRUE;
1584 (void) printf(gettext("\tcache\n"));
1585 for (c = 0; c < l2children; c++) {
1586 vname = zpool_vdev_name(g_zfs, NULL,
1587 l2child[c], name_flags);
1588 (void) printf("\t %s\n", vname);
1589 free(vname);
1590 }
1591 }
1592 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1593 &l2child, &l2children) == 0 && l2children) {
1594 if (!hadcache)
1595 (void) printf(gettext("\tcache\n"));
1596 for (c = 0; c < l2children; c++) {
1597 vname = zpool_vdev_name(g_zfs, NULL,
1598 l2child[c], name_flags);
1599 (void) printf("\t %s\n", vname);
1600 free(vname);
1601 }
1602 }
1603 /* And finally the spares */
1604 if (nvlist_lookup_nvlist_array(poolnvroot, ZPOOL_CONFIG_SPARES,
1605 &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1606 hadspare = B_TRUE;
1607 (void) printf(gettext("\tspares\n"));
1608 for (c = 0; c < sparechildren; c++) {
1609 vname = zpool_vdev_name(g_zfs, NULL,
1610 sparechild[c], name_flags);
1611 (void) printf("\t %s\n", vname);
1612 free(vname);
1613 }
1614 }
1615 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1616 &sparechild, &sparechildren) == 0 && sparechildren > 0) {
1617 if (!hadspare)
1618 (void) printf(gettext("\tspares\n"));
1619 for (c = 0; c < sparechildren; c++) {
1620 vname = zpool_vdev_name(g_zfs, NULL,
1621 sparechild[c], name_flags);
1622 (void) printf("\t %s\n", vname);
1623 free(vname);
1624 }
1625 }
1626
1627 ret = 0;
1628 } else {
1629 ret = (zpool_add(zhp, nvroot, check_ashift) != 0);
1630 }
1631
1632 nvlist_free(props);
1633 nvlist_free(nvroot);
1634 zpool_close(zhp);
1635
1636 return (ret);
1637 }
1638
1639 /*
1640 * zpool remove [-npsw] <pool> <vdev> ...
1641 *
1642 * Removes the given vdev from the pool.
1643 */
1644 int
zpool_do_remove(int argc,char ** argv)1645 zpool_do_remove(int argc, char **argv)
1646 {
1647 char *poolname;
1648 int i, ret = 0;
1649 zpool_handle_t *zhp = NULL;
1650 boolean_t stop = B_FALSE;
1651 int c;
1652 boolean_t noop = B_FALSE;
1653 boolean_t parsable = B_FALSE;
1654 boolean_t wait = B_FALSE;
1655
1656 /* check options */
1657 while ((c = getopt(argc, argv, "npsw")) != -1) {
1658 switch (c) {
1659 case 'n':
1660 noop = B_TRUE;
1661 break;
1662 case 'p':
1663 parsable = B_TRUE;
1664 break;
1665 case 's':
1666 stop = B_TRUE;
1667 break;
1668 case 'w':
1669 wait = B_TRUE;
1670 break;
1671 case '?':
1672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1673 optopt);
1674 usage(B_FALSE);
1675 }
1676 }
1677
1678 argc -= optind;
1679 argv += optind;
1680
1681 /* get pool name and check number of arguments */
1682 if (argc < 1) {
1683 (void) fprintf(stderr, gettext("missing pool name argument\n"));
1684 usage(B_FALSE);
1685 }
1686
1687 poolname = argv[0];
1688
1689 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
1690 return (1);
1691
1692 if (stop && noop) {
1693 zpool_close(zhp);
1694 (void) fprintf(stderr, gettext("stop request ignored\n"));
1695 return (0);
1696 }
1697
1698 if (stop) {
1699 if (argc > 1) {
1700 (void) fprintf(stderr, gettext("too many arguments\n"));
1701 usage(B_FALSE);
1702 }
1703 if (zpool_vdev_remove_cancel(zhp) != 0)
1704 ret = 1;
1705 if (wait) {
1706 (void) fprintf(stderr, gettext("invalid option "
1707 "combination: -w cannot be used with -s\n"));
1708 usage(B_FALSE);
1709 }
1710 } else {
1711 if (argc < 2) {
1712 (void) fprintf(stderr, gettext("missing device\n"));
1713 usage(B_FALSE);
1714 }
1715
1716 for (i = 1; i < argc; i++) {
1717 if (noop) {
1718 uint64_t size;
1719
1720 if (zpool_vdev_indirect_size(zhp, argv[i],
1721 &size) != 0) {
1722 ret = 1;
1723 break;
1724 }
1725 if (parsable) {
1726 (void) printf("%s %llu\n",
1727 argv[i], (unsigned long long)size);
1728 } else {
1729 char valstr[32];
1730 zfs_nicenum(size, valstr,
1731 sizeof (valstr));
1732 (void) printf("Memory that will be "
1733 "used after removing %s: %s\n",
1734 argv[i], valstr);
1735 }
1736 } else {
1737 if (zpool_vdev_remove(zhp, argv[i]) != 0)
1738 ret = 1;
1739 }
1740 }
1741
1742 if (ret == 0 && wait)
1743 ret = zpool_wait(zhp, ZPOOL_WAIT_REMOVE);
1744 }
1745 zpool_close(zhp);
1746
1747 return (ret);
1748 }
1749
1750 /*
1751 * Return 1 if a vdev is active (being used in a pool)
1752 * Return 0 if a vdev is inactive (offlined or faulted, or not in active pool)
1753 *
1754 * This is useful for checking if a disk in an active pool is offlined or
1755 * faulted.
1756 */
1757 static int
vdev_is_active(char * vdev_path)1758 vdev_is_active(char *vdev_path)
1759 {
1760 int fd;
1761 fd = open(vdev_path, O_EXCL);
1762 if (fd < 0) {
1763 return (1); /* cant open O_EXCL - disk is active */
1764 }
1765
1766 (void) close(fd);
1767 return (0); /* disk is inactive in the pool */
1768 }
1769
1770 /*
1771 * zpool labelclear [-f] <vdev>
1772 *
1773 * -f Force clearing the label for the vdevs which are members of
1774 * the exported or foreign pools.
1775 *
1776 * Verifies that the vdev is not active and zeros out the label information
1777 * on the device.
1778 */
1779 int
zpool_do_labelclear(int argc,char ** argv)1780 zpool_do_labelclear(int argc, char **argv)
1781 {
1782 char vdev[MAXPATHLEN];
1783 char *name = NULL;
1784 int c, fd, ret = 0;
1785 nvlist_t *config;
1786 pool_state_t state;
1787 boolean_t inuse = B_FALSE;
1788 boolean_t force = B_FALSE;
1789
1790 /* check options */
1791 while ((c = getopt(argc, argv, "f")) != -1) {
1792 switch (c) {
1793 case 'f':
1794 force = B_TRUE;
1795 break;
1796 default:
1797 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1798 optopt);
1799 usage(B_FALSE);
1800 }
1801 }
1802
1803 argc -= optind;
1804 argv += optind;
1805
1806 /* get vdev name */
1807 if (argc < 1) {
1808 (void) fprintf(stderr, gettext("missing vdev name\n"));
1809 usage(B_FALSE);
1810 }
1811 if (argc > 1) {
1812 (void) fprintf(stderr, gettext("too many arguments\n"));
1813 usage(B_FALSE);
1814 }
1815
1816 (void) strlcpy(vdev, argv[0], sizeof (vdev));
1817
1818 /*
1819 * If we cannot open an absolute path, we quit.
1820 * Otherwise if the provided vdev name doesn't point to a file,
1821 * try prepending expected disk paths and partition numbers.
1822 */
1823 if ((fd = open(vdev, O_RDWR)) < 0) {
1824 int error;
1825 if (vdev[0] == '/') {
1826 (void) fprintf(stderr, gettext("failed to open "
1827 "%s: %s\n"), vdev, strerror(errno));
1828 return (1);
1829 }
1830
1831 error = zfs_resolve_shortname(argv[0], vdev, MAXPATHLEN);
1832 if (error == 0 && zfs_dev_is_whole_disk(vdev)) {
1833 if (zfs_append_partition(vdev, MAXPATHLEN) == -1)
1834 error = ENOENT;
1835 }
1836
1837 if (error || ((fd = open(vdev, O_RDWR)) < 0)) {
1838 if (errno == ENOENT) {
1839 (void) fprintf(stderr, gettext(
1840 "failed to find device %s, try "
1841 "specifying absolute path instead\n"),
1842 argv[0]);
1843 return (1);
1844 }
1845
1846 (void) fprintf(stderr, gettext("failed to open %s:"
1847 " %s\n"), vdev, strerror(errno));
1848 return (1);
1849 }
1850 }
1851
1852 /*
1853 * Flush all dirty pages for the block device. This should not be
1854 * fatal when the device does not support BLKFLSBUF as would be the
1855 * case for a file vdev.
1856 */
1857 if ((zfs_dev_flush(fd) != 0) && (errno != ENOTTY))
1858 (void) fprintf(stderr, gettext("failed to invalidate "
1859 "cache for %s: %s\n"), vdev, strerror(errno));
1860
1861 if (zpool_read_label(fd, &config, NULL) != 0) {
1862 (void) fprintf(stderr,
1863 gettext("failed to read label from %s\n"), vdev);
1864 ret = 1;
1865 goto errout;
1866 }
1867 nvlist_free(config);
1868
1869 ret = zpool_in_use(g_zfs, fd, &state, &name, &inuse);
1870 if (ret != 0) {
1871 (void) fprintf(stderr,
1872 gettext("failed to check state for %s\n"), vdev);
1873 ret = 1;
1874 goto errout;
1875 }
1876
1877 if (!inuse)
1878 goto wipe_label;
1879
1880 switch (state) {
1881 default:
1882 case POOL_STATE_ACTIVE:
1883 case POOL_STATE_SPARE:
1884 case POOL_STATE_L2CACHE:
1885 /*
1886 * We allow the user to call 'zpool offline -f'
1887 * on an offlined disk in an active pool. We can check if
1888 * the disk is online by calling vdev_is_active().
1889 */
1890 if (force && !vdev_is_active(vdev))
1891 break;
1892
1893 (void) fprintf(stderr, gettext(
1894 "%s is a member (%s) of pool \"%s\""),
1895 vdev, zpool_pool_state_to_name(state), name);
1896
1897 if (force) {
1898 (void) fprintf(stderr, gettext(
1899 ". Offline the disk first to clear its label."));
1900 }
1901 printf("\n");
1902 ret = 1;
1903 goto errout;
1904
1905 case POOL_STATE_EXPORTED:
1906 if (force)
1907 break;
1908 (void) fprintf(stderr, gettext(
1909 "use '-f' to override the following error:\n"
1910 "%s is a member of exported pool \"%s\"\n"),
1911 vdev, name);
1912 ret = 1;
1913 goto errout;
1914
1915 case POOL_STATE_POTENTIALLY_ACTIVE:
1916 if (force)
1917 break;
1918 (void) fprintf(stderr, gettext(
1919 "use '-f' to override the following error:\n"
1920 "%s is a member of potentially active pool \"%s\"\n"),
1921 vdev, name);
1922 ret = 1;
1923 goto errout;
1924
1925 case POOL_STATE_DESTROYED:
1926 /* inuse should never be set for a destroyed pool */
1927 assert(0);
1928 break;
1929 }
1930
1931 wipe_label:
1932 ret = zpool_clear_label(fd);
1933 if (ret != 0) {
1934 (void) fprintf(stderr,
1935 gettext("failed to clear label for %s\n"), vdev);
1936 }
1937
1938 errout:
1939 free(name);
1940 (void) close(fd);
1941
1942 return (ret);
1943 }
1944
1945 /*
1946 * zpool create [-fnd] [-o property=value] ...
1947 * [-O file-system-property=value] ...
1948 * [-R root] [-m mountpoint] <pool> <dev> ...
1949 *
1950 * -f Force creation, even if devices appear in use
1951 * -n Do not create the pool, but display the resulting layout if it
1952 * were to be created.
1953 * -R Create a pool under an alternate root
1954 * -m Set default mountpoint for the root dataset. By default it's
1955 * '/<pool>'
1956 * -o Set property=value.
1957 * -o Set feature@feature=enabled|disabled.
1958 * -d Don't automatically enable all supported pool features
1959 * (individual features can be enabled with -o).
1960 * -O Set fsproperty=value in the pool's root file system
1961 *
1962 * Creates the named pool according to the given vdev specification. The
1963 * bulk of the vdev processing is done in make_root_vdev() in zpool_vdev.c.
1964 * Once we get the nvlist back from make_root_vdev(), we either print out the
1965 * contents (if '-n' was specified), or pass it to libzfs to do the creation.
1966 */
1967 int
zpool_do_create(int argc,char ** argv)1968 zpool_do_create(int argc, char **argv)
1969 {
1970 boolean_t force = B_FALSE;
1971 boolean_t dryrun = B_FALSE;
1972 boolean_t enable_pool_features = B_TRUE;
1973
1974 int c;
1975 nvlist_t *nvroot = NULL;
1976 char *poolname;
1977 char *tname = NULL;
1978 int ret = 1;
1979 char *altroot = NULL;
1980 char *compat = NULL;
1981 char *mountpoint = NULL;
1982 nvlist_t *fsprops = NULL;
1983 nvlist_t *props = NULL;
1984 char *propval;
1985
1986 /* check options */
1987 while ((c = getopt(argc, argv, ":fndR:m:o:O:t:")) != -1) {
1988 switch (c) {
1989 case 'f':
1990 force = B_TRUE;
1991 break;
1992 case 'n':
1993 dryrun = B_TRUE;
1994 break;
1995 case 'd':
1996 enable_pool_features = B_FALSE;
1997 break;
1998 case 'R':
1999 altroot = optarg;
2000 if (add_prop_list(zpool_prop_to_name(
2001 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
2002 goto errout;
2003 if (add_prop_list_default(zpool_prop_to_name(
2004 ZPOOL_PROP_CACHEFILE), "none", &props))
2005 goto errout;
2006 break;
2007 case 'm':
2008 /* Equivalent to -O mountpoint=optarg */
2009 mountpoint = optarg;
2010 break;
2011 case 'o':
2012 if ((propval = strchr(optarg, '=')) == NULL) {
2013 (void) fprintf(stderr, gettext("missing "
2014 "'=' for -o option\n"));
2015 goto errout;
2016 }
2017 *propval = '\0';
2018 propval++;
2019
2020 if (add_prop_list(optarg, propval, &props, B_TRUE))
2021 goto errout;
2022
2023 /*
2024 * If the user is creating a pool that doesn't support
2025 * feature flags, don't enable any features.
2026 */
2027 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_VERSION) {
2028 char *end;
2029 u_longlong_t ver;
2030
2031 ver = strtoull(propval, &end, 0);
2032 if (*end == '\0' &&
2033 ver < SPA_VERSION_FEATURES) {
2034 enable_pool_features = B_FALSE;
2035 }
2036 }
2037 if (zpool_name_to_prop(optarg) == ZPOOL_PROP_ALTROOT)
2038 altroot = propval;
2039 if (zpool_name_to_prop(optarg) ==
2040 ZPOOL_PROP_COMPATIBILITY)
2041 compat = propval;
2042 break;
2043 case 'O':
2044 if ((propval = strchr(optarg, '=')) == NULL) {
2045 (void) fprintf(stderr, gettext("missing "
2046 "'=' for -O option\n"));
2047 goto errout;
2048 }
2049 *propval = '\0';
2050 propval++;
2051
2052 /*
2053 * Mountpoints are checked and then added later.
2054 * Uniquely among properties, they can be specified
2055 * more than once, to avoid conflict with -m.
2056 */
2057 if (0 == strcmp(optarg,
2058 zfs_prop_to_name(ZFS_PROP_MOUNTPOINT))) {
2059 mountpoint = propval;
2060 } else if (add_prop_list(optarg, propval, &fsprops,
2061 B_FALSE)) {
2062 goto errout;
2063 }
2064 break;
2065 case 't':
2066 /*
2067 * Sanity check temporary pool name.
2068 */
2069 if (strchr(optarg, '/') != NULL) {
2070 (void) fprintf(stderr, gettext("cannot create "
2071 "'%s': invalid character '/' in temporary "
2072 "name\n"), optarg);
2073 (void) fprintf(stderr, gettext("use 'zfs "
2074 "create' to create a dataset\n"));
2075 goto errout;
2076 }
2077
2078 if (add_prop_list(zpool_prop_to_name(
2079 ZPOOL_PROP_TNAME), optarg, &props, B_TRUE))
2080 goto errout;
2081 if (add_prop_list_default(zpool_prop_to_name(
2082 ZPOOL_PROP_CACHEFILE), "none", &props))
2083 goto errout;
2084 tname = optarg;
2085 break;
2086 case ':':
2087 (void) fprintf(stderr, gettext("missing argument for "
2088 "'%c' option\n"), optopt);
2089 goto badusage;
2090 case '?':
2091 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2092 optopt);
2093 goto badusage;
2094 }
2095 }
2096
2097 argc -= optind;
2098 argv += optind;
2099
2100 /* get pool name and check number of arguments */
2101 if (argc < 1) {
2102 (void) fprintf(stderr, gettext("missing pool name argument\n"));
2103 goto badusage;
2104 }
2105 if (argc < 2) {
2106 (void) fprintf(stderr, gettext("missing vdev specification\n"));
2107 goto badusage;
2108 }
2109
2110 poolname = argv[0];
2111
2112 /*
2113 * As a special case, check for use of '/' in the name, and direct the
2114 * user to use 'zfs create' instead.
2115 */
2116 if (strchr(poolname, '/') != NULL) {
2117 (void) fprintf(stderr, gettext("cannot create '%s': invalid "
2118 "character '/' in pool name\n"), poolname);
2119 (void) fprintf(stderr, gettext("use 'zfs create' to "
2120 "create a dataset\n"));
2121 goto errout;
2122 }
2123
2124 /* pass off to make_root_vdev for bulk processing */
2125 nvroot = make_root_vdev(NULL, props, force, !force, B_FALSE, dryrun,
2126 argc - 1, argv + 1);
2127 if (nvroot == NULL)
2128 goto errout;
2129
2130 /* make_root_vdev() allows 0 toplevel children if there are spares */
2131 if (!zfs_allocatable_devs(nvroot)) {
2132 (void) fprintf(stderr, gettext("invalid vdev "
2133 "specification: at least one toplevel vdev must be "
2134 "specified\n"));
2135 goto errout;
2136 }
2137
2138 if (altroot != NULL && altroot[0] != '/') {
2139 (void) fprintf(stderr, gettext("invalid alternate root '%s': "
2140 "must be an absolute path\n"), altroot);
2141 goto errout;
2142 }
2143
2144 /*
2145 * Check the validity of the mountpoint and direct the user to use the
2146 * '-m' mountpoint option if it looks like its in use.
2147 */
2148 if (mountpoint == NULL ||
2149 (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 &&
2150 strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) != 0)) {
2151 char buf[MAXPATHLEN];
2152 DIR *dirp;
2153
2154 if (mountpoint && mountpoint[0] != '/') {
2155 (void) fprintf(stderr, gettext("invalid mountpoint "
2156 "'%s': must be an absolute path, 'legacy', or "
2157 "'none'\n"), mountpoint);
2158 goto errout;
2159 }
2160
2161 if (mountpoint == NULL) {
2162 if (altroot != NULL)
2163 (void) snprintf(buf, sizeof (buf), "%s/%s",
2164 altroot, poolname);
2165 else
2166 (void) snprintf(buf, sizeof (buf), "/%s",
2167 poolname);
2168 } else {
2169 if (altroot != NULL)
2170 (void) snprintf(buf, sizeof (buf), "%s%s",
2171 altroot, mountpoint);
2172 else
2173 (void) snprintf(buf, sizeof (buf), "%s",
2174 mountpoint);
2175 }
2176
2177 if ((dirp = opendir(buf)) == NULL && errno != ENOENT) {
2178 (void) fprintf(stderr, gettext("mountpoint '%s' : "
2179 "%s\n"), buf, strerror(errno));
2180 (void) fprintf(stderr, gettext("use '-m' "
2181 "option to provide a different default\n"));
2182 goto errout;
2183 } else if (dirp) {
2184 int count = 0;
2185
2186 while (count < 3 && readdir(dirp) != NULL)
2187 count++;
2188 (void) closedir(dirp);
2189
2190 if (count > 2) {
2191 (void) fprintf(stderr, gettext("mountpoint "
2192 "'%s' exists and is not empty\n"), buf);
2193 (void) fprintf(stderr, gettext("use '-m' "
2194 "option to provide a "
2195 "different default\n"));
2196 goto errout;
2197 }
2198 }
2199 }
2200
2201 /*
2202 * Now that the mountpoint's validity has been checked, ensure that
2203 * the property is set appropriately prior to creating the pool.
2204 */
2205 if (mountpoint != NULL) {
2206 ret = add_prop_list(zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
2207 mountpoint, &fsprops, B_FALSE);
2208 if (ret != 0)
2209 goto errout;
2210 }
2211
2212 ret = 1;
2213 if (dryrun) {
2214 /*
2215 * For a dry run invocation, print out a basic message and run
2216 * through all the vdevs in the list and print out in an
2217 * appropriate hierarchy.
2218 */
2219 (void) printf(gettext("would create '%s' with the "
2220 "following layout:\n\n"), poolname);
2221
2222 print_vdev_tree(NULL, poolname, nvroot, 0, "", 0);
2223 print_vdev_tree(NULL, "dedup", nvroot, 0,
2224 VDEV_ALLOC_BIAS_DEDUP, 0);
2225 print_vdev_tree(NULL, "special", nvroot, 0,
2226 VDEV_ALLOC_BIAS_SPECIAL, 0);
2227 print_vdev_tree(NULL, "logs", nvroot, 0,
2228 VDEV_ALLOC_BIAS_LOG, 0);
2229 print_cache_list(nvroot, 0);
2230 print_spare_list(nvroot, 0);
2231
2232 ret = 0;
2233 } else {
2234 /*
2235 * Load in feature set.
2236 * Note: if compatibility property not given, we'll have
2237 * NULL, which means 'all features'.
2238 */
2239 boolean_t requested_features[SPA_FEATURES];
2240 if (zpool_do_load_compat(compat, requested_features) !=
2241 ZPOOL_COMPATIBILITY_OK)
2242 goto errout;
2243
2244 /*
2245 * props contains list of features to enable.
2246 * For each feature:
2247 * - remove it if feature@name=disabled
2248 * - leave it there if feature@name=enabled
2249 * - add it if:
2250 * - enable_pool_features (ie: no '-d' or '-o version')
2251 * - it's supported by the kernel module
2252 * - it's in the requested feature set
2253 * - warn if it's enabled but not in compat
2254 */
2255 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2256 char propname[MAXPATHLEN];
2257 const char *propval;
2258 zfeature_info_t *feat = &spa_feature_table[i];
2259
2260 (void) snprintf(propname, sizeof (propname),
2261 "feature@%s", feat->fi_uname);
2262
2263 if (!nvlist_lookup_string(props, propname, &propval)) {
2264 if (strcmp(propval,
2265 ZFS_FEATURE_DISABLED) == 0) {
2266 (void) nvlist_remove_all(props,
2267 propname);
2268 } else if (strcmp(propval,
2269 ZFS_FEATURE_ENABLED) == 0 &&
2270 !requested_features[i]) {
2271 (void) fprintf(stderr, gettext(
2272 "Warning: feature \"%s\" enabled "
2273 "but is not in specified "
2274 "'compatibility' feature set.\n"),
2275 feat->fi_uname);
2276 }
2277 } else if (
2278 enable_pool_features &&
2279 feat->fi_zfs_mod_supported &&
2280 requested_features[i]) {
2281 ret = add_prop_list(propname,
2282 ZFS_FEATURE_ENABLED, &props, B_TRUE);
2283 if (ret != 0)
2284 goto errout;
2285 }
2286 }
2287
2288 ret = 1;
2289 if (zpool_create(g_zfs, poolname,
2290 nvroot, props, fsprops) == 0) {
2291 zfs_handle_t *pool = zfs_open(g_zfs,
2292 tname ? tname : poolname, ZFS_TYPE_FILESYSTEM);
2293 if (pool != NULL) {
2294 if (zfs_mount(pool, NULL, 0) == 0) {
2295 ret = zfs_share(pool, NULL);
2296 zfs_commit_shares(NULL);
2297 }
2298 zfs_close(pool);
2299 }
2300 } else if (libzfs_errno(g_zfs) == EZFS_INVALIDNAME) {
2301 (void) fprintf(stderr, gettext("pool name may have "
2302 "been omitted\n"));
2303 }
2304 }
2305
2306 errout:
2307 nvlist_free(nvroot);
2308 nvlist_free(fsprops);
2309 nvlist_free(props);
2310 return (ret);
2311 badusage:
2312 nvlist_free(fsprops);
2313 nvlist_free(props);
2314 usage(B_FALSE);
2315 return (2);
2316 }
2317
2318 /*
2319 * zpool destroy <pool>
2320 *
2321 * -f Forcefully unmount any datasets
2322 *
2323 * Destroy the given pool. Automatically unmounts any datasets in the pool.
2324 */
2325 int
zpool_do_destroy(int argc,char ** argv)2326 zpool_do_destroy(int argc, char **argv)
2327 {
2328 boolean_t force = B_FALSE;
2329 int c;
2330 char *pool;
2331 zpool_handle_t *zhp;
2332 int ret;
2333
2334 /* check options */
2335 while ((c = getopt(argc, argv, "f")) != -1) {
2336 switch (c) {
2337 case 'f':
2338 force = B_TRUE;
2339 break;
2340 case '?':
2341 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2342 optopt);
2343 usage(B_FALSE);
2344 }
2345 }
2346
2347 argc -= optind;
2348 argv += optind;
2349
2350 /* check arguments */
2351 if (argc < 1) {
2352 (void) fprintf(stderr, gettext("missing pool argument\n"));
2353 usage(B_FALSE);
2354 }
2355 if (argc > 1) {
2356 (void) fprintf(stderr, gettext("too many arguments\n"));
2357 usage(B_FALSE);
2358 }
2359
2360 pool = argv[0];
2361
2362 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
2363 /*
2364 * As a special case, check for use of '/' in the name, and
2365 * direct the user to use 'zfs destroy' instead.
2366 */
2367 if (strchr(pool, '/') != NULL)
2368 (void) fprintf(stderr, gettext("use 'zfs destroy' to "
2369 "destroy a dataset\n"));
2370 return (1);
2371 }
2372
2373 if (zpool_disable_datasets(zhp, force) != 0) {
2374 (void) fprintf(stderr, gettext("could not destroy '%s': "
2375 "could not unmount datasets\n"), zpool_get_name(zhp));
2376 zpool_close(zhp);
2377 return (1);
2378 }
2379
2380 /* The history must be logged as part of the export */
2381 log_history = B_FALSE;
2382
2383 ret = (zpool_destroy(zhp, history_str) != 0);
2384
2385 zpool_close(zhp);
2386
2387 return (ret);
2388 }
2389
2390 typedef struct export_cbdata {
2391 taskq_t *taskq;
2392 pthread_mutex_t mnttab_lock;
2393 boolean_t force;
2394 boolean_t hardforce;
2395 int retval;
2396 } export_cbdata_t;
2397
2398
2399 typedef struct {
2400 char *aea_poolname;
2401 export_cbdata_t *aea_cbdata;
2402 } async_export_args_t;
2403
2404 /*
2405 * Export one pool
2406 */
2407 static int
zpool_export_one(zpool_handle_t * zhp,void * data)2408 zpool_export_one(zpool_handle_t *zhp, void *data)
2409 {
2410 export_cbdata_t *cb = data;
2411
2412 /*
2413 * zpool_disable_datasets() is not thread-safe for mnttab access.
2414 * So we serialize access here for 'zpool export -a' parallel case.
2415 */
2416 if (cb->taskq != NULL)
2417 (void) pthread_mutex_lock(&cb->mnttab_lock);
2418
2419 int retval = zpool_disable_datasets(zhp, cb->force);
2420
2421 if (cb->taskq != NULL)
2422 (void) pthread_mutex_unlock(&cb->mnttab_lock);
2423
2424 if (retval)
2425 return (1);
2426
2427 if (cb->hardforce) {
2428 if (zpool_export_force(zhp, history_str) != 0)
2429 return (1);
2430 } else if (zpool_export(zhp, cb->force, history_str) != 0) {
2431 return (1);
2432 }
2433
2434 return (0);
2435 }
2436
2437 /*
2438 * Asynchronous export request
2439 */
2440 static void
zpool_export_task(void * arg)2441 zpool_export_task(void *arg)
2442 {
2443 async_export_args_t *aea = arg;
2444
2445 zpool_handle_t *zhp = zpool_open(g_zfs, aea->aea_poolname);
2446 if (zhp != NULL) {
2447 int ret = zpool_export_one(zhp, aea->aea_cbdata);
2448 if (ret != 0)
2449 aea->aea_cbdata->retval = ret;
2450 zpool_close(zhp);
2451 } else {
2452 aea->aea_cbdata->retval = 1;
2453 }
2454
2455 free(aea->aea_poolname);
2456 free(aea);
2457 }
2458
2459 /*
2460 * Process an export request in parallel
2461 */
2462 static int
zpool_export_one_async(zpool_handle_t * zhp,void * data)2463 zpool_export_one_async(zpool_handle_t *zhp, void *data)
2464 {
2465 taskq_t *tq = ((export_cbdata_t *)data)->taskq;
2466 async_export_args_t *aea = safe_malloc(sizeof (async_export_args_t));
2467
2468 /* save pool name since zhp will go out of scope */
2469 aea->aea_poolname = strdup(zpool_get_name(zhp));
2470 aea->aea_cbdata = data;
2471
2472 /* ship off actual export to another thread */
2473 if (taskq_dispatch(tq, zpool_export_task, (void *)aea,
2474 TQ_SLEEP) == TASKQID_INVALID)
2475 return (errno); /* unlikely */
2476 else
2477 return (0);
2478 }
2479
2480 /*
2481 * zpool export [-f] <pool> ...
2482 *
2483 * -a Export all pools
2484 * -f Forcefully unmount datasets
2485 *
2486 * Export the given pools. By default, the command will attempt to cleanly
2487 * unmount any active datasets within the pool. If the '-f' flag is specified,
2488 * then the datasets will be forcefully unmounted.
2489 */
2490 int
zpool_do_export(int argc,char ** argv)2491 zpool_do_export(int argc, char **argv)
2492 {
2493 export_cbdata_t cb;
2494 boolean_t do_all = B_FALSE;
2495 boolean_t force = B_FALSE;
2496 boolean_t hardforce = B_FALSE;
2497 int c, ret;
2498
2499 /* check options */
2500 while ((c = getopt(argc, argv, "afF")) != -1) {
2501 switch (c) {
2502 case 'a':
2503 do_all = B_TRUE;
2504 break;
2505 case 'f':
2506 force = B_TRUE;
2507 break;
2508 case 'F':
2509 hardforce = B_TRUE;
2510 break;
2511 case '?':
2512 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2513 optopt);
2514 usage(B_FALSE);
2515 }
2516 }
2517
2518 cb.force = force;
2519 cb.hardforce = hardforce;
2520 cb.taskq = NULL;
2521 cb.retval = 0;
2522 argc -= optind;
2523 argv += optind;
2524
2525 /* The history will be logged as part of the export itself */
2526 log_history = B_FALSE;
2527
2528 if (do_all) {
2529 if (argc != 0) {
2530 (void) fprintf(stderr, gettext("too many arguments\n"));
2531 usage(B_FALSE);
2532 }
2533
2534 cb.taskq = taskq_create("zpool_export",
2535 5 * sysconf(_SC_NPROCESSORS_ONLN), minclsyspri, 1, INT_MAX,
2536 TASKQ_DYNAMIC);
2537 (void) pthread_mutex_init(&cb.mnttab_lock, NULL);
2538
2539 /* Asynchronously call zpool_export_one using thread pool */
2540 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
2541 B_FALSE, zpool_export_one_async, &cb);
2542
2543 taskq_wait(cb.taskq);
2544 taskq_destroy(cb.taskq);
2545 (void) pthread_mutex_destroy(&cb.mnttab_lock);
2546
2547 return (ret | cb.retval);
2548 }
2549
2550 /* check arguments */
2551 if (argc < 1) {
2552 (void) fprintf(stderr, gettext("missing pool argument\n"));
2553 usage(B_FALSE);
2554 }
2555
2556 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
2557 B_FALSE, zpool_export_one, &cb);
2558
2559 return (ret);
2560 }
2561
2562 /*
2563 * Given a vdev configuration, determine the maximum width needed for the device
2564 * name column.
2565 */
2566 static int
max_width(zpool_handle_t * zhp,nvlist_t * nv,int depth,int max,int name_flags)2567 max_width(zpool_handle_t *zhp, nvlist_t *nv, int depth, int max,
2568 int name_flags)
2569 {
2570 static const char *const subtypes[] =
2571 {ZPOOL_CONFIG_SPARES, ZPOOL_CONFIG_L2CACHE, ZPOOL_CONFIG_CHILDREN};
2572
2573 char *name = zpool_vdev_name(g_zfs, zhp, nv, name_flags);
2574 max = MAX(strlen(name) + depth, max);
2575 free(name);
2576
2577 nvlist_t **child;
2578 uint_t children;
2579 for (size_t i = 0; i < ARRAY_SIZE(subtypes); ++i)
2580 if (nvlist_lookup_nvlist_array(nv, subtypes[i],
2581 &child, &children) == 0)
2582 for (uint_t c = 0; c < children; ++c)
2583 max = MAX(max_width(zhp, child[c], depth + 2,
2584 max, name_flags), max);
2585
2586 return (max);
2587 }
2588
2589 typedef struct status_cbdata {
2590 int cb_count;
2591 int cb_name_flags;
2592 int cb_namewidth;
2593 boolean_t cb_allpools;
2594 boolean_t cb_verbose;
2595 boolean_t cb_literal;
2596 boolean_t cb_explain;
2597 boolean_t cb_first;
2598 boolean_t cb_dedup_stats;
2599 boolean_t cb_print_unhealthy;
2600 boolean_t cb_print_status;
2601 boolean_t cb_print_slow_ios;
2602 boolean_t cb_print_dio_verify;
2603 boolean_t cb_print_vdev_init;
2604 boolean_t cb_print_vdev_trim;
2605 vdev_cmd_data_list_t *vcdl;
2606 boolean_t cb_print_power;
2607 boolean_t cb_json;
2608 boolean_t cb_flat_vdevs;
2609 nvlist_t *cb_jsobj;
2610 boolean_t cb_json_as_int;
2611 boolean_t cb_json_pool_key_guid;
2612 } status_cbdata_t;
2613
2614 /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */
2615 static boolean_t
is_blank_str(const char * str)2616 is_blank_str(const char *str)
2617 {
2618 for (; str != NULL && *str != '\0'; ++str)
2619 if (!isblank(*str))
2620 return (B_FALSE);
2621 return (B_TRUE);
2622 }
2623
2624 static void
zpool_nvlist_cmd(vdev_cmd_data_list_t * vcdl,const char * pool,const char * path,nvlist_t * item)2625 zpool_nvlist_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path,
2626 nvlist_t *item)
2627 {
2628 vdev_cmd_data_t *data;
2629 int i, j, k = 1;
2630 char tmp[256];
2631 const char *val;
2632
2633 for (i = 0; i < vcdl->count; i++) {
2634 if ((strcmp(vcdl->data[i].path, path) != 0) ||
2635 (strcmp(vcdl->data[i].pool, pool) != 0))
2636 continue;
2637
2638 data = &vcdl->data[i];
2639 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
2640 val = NULL;
2641 for (int k = 0; k < data->cols_cnt; k++) {
2642 if (strcmp(data->cols[k],
2643 vcdl->uniq_cols[j]) == 0) {
2644 val = data->lines[k];
2645 break;
2646 }
2647 }
2648 if (val == NULL || is_blank_str(val))
2649 val = "-";
2650 fnvlist_add_string(item, vcdl->uniq_cols[j], val);
2651 }
2652
2653 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
2654 if (data->lines[j]) {
2655 (void) snprintf(tmp, 256, "extra_%d", k++);
2656 fnvlist_add_string(item, tmp,
2657 data->lines[j]);
2658 }
2659 }
2660 break;
2661 }
2662 }
2663
2664 /* Print command output lines for specific vdev in a specific pool */
2665 static void
zpool_print_cmd(vdev_cmd_data_list_t * vcdl,const char * pool,const char * path)2666 zpool_print_cmd(vdev_cmd_data_list_t *vcdl, const char *pool, const char *path)
2667 {
2668 vdev_cmd_data_t *data;
2669 int i, j;
2670 const char *val;
2671
2672 for (i = 0; i < vcdl->count; i++) {
2673 if ((strcmp(vcdl->data[i].path, path) != 0) ||
2674 (strcmp(vcdl->data[i].pool, pool) != 0)) {
2675 /* Not the vdev we're looking for */
2676 continue;
2677 }
2678
2679 data = &vcdl->data[i];
2680 /* Print out all the output values for this vdev */
2681 for (j = 0; j < vcdl->uniq_cols_cnt; j++) {
2682 val = NULL;
2683 /* Does this vdev have values for this column? */
2684 for (int k = 0; k < data->cols_cnt; k++) {
2685 if (strcmp(data->cols[k],
2686 vcdl->uniq_cols[j]) == 0) {
2687 /* yes it does, record the value */
2688 val = data->lines[k];
2689 break;
2690 }
2691 }
2692 /*
2693 * Mark empty values with dashes to make output
2694 * awk-able.
2695 */
2696 if (val == NULL || is_blank_str(val))
2697 val = "-";
2698
2699 printf("%*s", vcdl->uniq_cols_width[j], val);
2700 if (j < vcdl->uniq_cols_cnt - 1)
2701 (void) fputs(" ", stdout);
2702 }
2703
2704 /* Print out any values that aren't in a column at the end */
2705 for (j = data->cols_cnt; j < data->lines_cnt; j++) {
2706 /* Did we have any columns? If so print a spacer. */
2707 if (vcdl->uniq_cols_cnt > 0)
2708 (void) fputs(" ", stdout);
2709
2710 val = data->lines[j];
2711 (void) fputs(val ?: "", stdout);
2712 }
2713 break;
2714 }
2715 }
2716
2717 /*
2718 * Print vdev initialization status for leaves
2719 */
2720 static void
print_status_initialize(vdev_stat_t * vs,boolean_t verbose)2721 print_status_initialize(vdev_stat_t *vs, boolean_t verbose)
2722 {
2723 if (verbose) {
2724 if ((vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE ||
2725 vs->vs_initialize_state == VDEV_INITIALIZE_SUSPENDED ||
2726 vs->vs_initialize_state == VDEV_INITIALIZE_COMPLETE) &&
2727 !vs->vs_scan_removing) {
2728 char zbuf[1024];
2729 char tbuf[256];
2730
2731 time_t t = vs->vs_initialize_action_time;
2732 int initialize_pct = 100;
2733 if (vs->vs_initialize_state !=
2734 VDEV_INITIALIZE_COMPLETE) {
2735 initialize_pct = (vs->vs_initialize_bytes_done *
2736 100 / (vs->vs_initialize_bytes_est + 1));
2737 }
2738
2739 (void) ctime_r(&t, tbuf);
2740 tbuf[24] = 0;
2741
2742 switch (vs->vs_initialize_state) {
2743 case VDEV_INITIALIZE_SUSPENDED:
2744 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2745 gettext("suspended, started at"), tbuf);
2746 break;
2747 case VDEV_INITIALIZE_ACTIVE:
2748 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2749 gettext("started at"), tbuf);
2750 break;
2751 case VDEV_INITIALIZE_COMPLETE:
2752 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2753 gettext("completed at"), tbuf);
2754 break;
2755 }
2756
2757 (void) printf(gettext(" (%d%% initialized%s)"),
2758 initialize_pct, zbuf);
2759 } else {
2760 (void) printf(gettext(" (uninitialized)"));
2761 }
2762 } else if (vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE) {
2763 (void) printf(gettext(" (initializing)"));
2764 }
2765 }
2766
2767 /*
2768 * Print vdev TRIM status for leaves
2769 */
2770 static void
print_status_trim(vdev_stat_t * vs,boolean_t verbose)2771 print_status_trim(vdev_stat_t *vs, boolean_t verbose)
2772 {
2773 if (verbose) {
2774 if ((vs->vs_trim_state == VDEV_TRIM_ACTIVE ||
2775 vs->vs_trim_state == VDEV_TRIM_SUSPENDED ||
2776 vs->vs_trim_state == VDEV_TRIM_COMPLETE) &&
2777 !vs->vs_scan_removing) {
2778 char zbuf[1024];
2779 char tbuf[256];
2780
2781 time_t t = vs->vs_trim_action_time;
2782 int trim_pct = 100;
2783 if (vs->vs_trim_state != VDEV_TRIM_COMPLETE) {
2784 trim_pct = (vs->vs_trim_bytes_done *
2785 100 / (vs->vs_trim_bytes_est + 1));
2786 }
2787
2788 (void) ctime_r(&t, tbuf);
2789 tbuf[24] = 0;
2790
2791 switch (vs->vs_trim_state) {
2792 case VDEV_TRIM_SUSPENDED:
2793 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2794 gettext("suspended, started at"), tbuf);
2795 break;
2796 case VDEV_TRIM_ACTIVE:
2797 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2798 gettext("started at"), tbuf);
2799 break;
2800 case VDEV_TRIM_COMPLETE:
2801 (void) snprintf(zbuf, sizeof (zbuf), ", %s %s",
2802 gettext("completed at"), tbuf);
2803 break;
2804 }
2805
2806 (void) printf(gettext(" (%d%% trimmed%s)"),
2807 trim_pct, zbuf);
2808 } else if (vs->vs_trim_notsup) {
2809 (void) printf(gettext(" (trim unsupported)"));
2810 } else {
2811 (void) printf(gettext(" (untrimmed)"));
2812 }
2813 } else if (vs->vs_trim_state == VDEV_TRIM_ACTIVE) {
2814 (void) printf(gettext(" (trimming)"));
2815 }
2816 }
2817
2818 /*
2819 * Return the color associated with a health string. This includes returning
2820 * NULL for no color change.
2821 */
2822 static const char *
health_str_to_color(const char * health)2823 health_str_to_color(const char *health)
2824 {
2825 if (strcmp(health, gettext("FAULTED")) == 0 ||
2826 strcmp(health, gettext("SUSPENDED")) == 0 ||
2827 strcmp(health, gettext("UNAVAIL")) == 0) {
2828 return (ANSI_RED);
2829 }
2830
2831 if (strcmp(health, gettext("OFFLINE")) == 0 ||
2832 strcmp(health, gettext("DEGRADED")) == 0 ||
2833 strcmp(health, gettext("REMOVED")) == 0) {
2834 return (ANSI_YELLOW);
2835 }
2836
2837 return (NULL);
2838 }
2839
2840 /*
2841 * Called for each leaf vdev. Returns 0 if the vdev is healthy.
2842 * A vdev is unhealthy if any of the following are true:
2843 * 1) there are read, write, or checksum errors,
2844 * 2) its state is not ONLINE, or
2845 * 3) slow IO reporting was requested (-s) and there are slow IOs.
2846 */
2847 static int
vdev_health_check_cb(void * hdl_data,nvlist_t * nv,void * data)2848 vdev_health_check_cb(void *hdl_data, nvlist_t *nv, void *data)
2849 {
2850 status_cbdata_t *cb = data;
2851 vdev_stat_t *vs;
2852 uint_t vsc;
2853 (void) hdl_data;
2854
2855 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2856 (uint64_t **)&vs, &vsc) != 0)
2857 return (1);
2858
2859 if (vs->vs_checksum_errors || vs->vs_read_errors ||
2860 vs->vs_write_errors || vs->vs_state != VDEV_STATE_HEALTHY)
2861 return (1);
2862
2863 if (cb->cb_print_slow_ios && vs->vs_slow_ios)
2864 return (1);
2865
2866 return (0);
2867 }
2868
2869 /*
2870 * Print out configuration state as requested by status_callback.
2871 */
2872 static void
print_status_config(zpool_handle_t * zhp,status_cbdata_t * cb,const char * name,nvlist_t * nv,int depth,boolean_t isspare,vdev_rebuild_stat_t * vrs)2873 print_status_config(zpool_handle_t *zhp, status_cbdata_t *cb, const char *name,
2874 nvlist_t *nv, int depth, boolean_t isspare, vdev_rebuild_stat_t *vrs)
2875 {
2876 nvlist_t **child, *root;
2877 uint_t c, i, vsc, children;
2878 pool_scan_stat_t *ps = NULL;
2879 vdev_stat_t *vs;
2880 char rbuf[6], wbuf[6], cbuf[6], dbuf[6];
2881 char *vname;
2882 uint64_t notpresent;
2883 spare_cbdata_t spare_cb;
2884 const char *state;
2885 const char *type;
2886 const char *path = NULL;
2887 const char *rcolor = NULL, *wcolor = NULL, *ccolor = NULL,
2888 *scolor = NULL;
2889
2890 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2891 &child, &children) != 0)
2892 children = 0;
2893
2894 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
2895 (uint64_t **)&vs, &vsc) == 0);
2896
2897 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
2898
2899 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
2900 return;
2901
2902 state = zpool_state_to_name(vs->vs_state, vs->vs_aux);
2903
2904 if (isspare) {
2905 /*
2906 * For hot spares, we use the terms 'INUSE' and 'AVAILABLE' for
2907 * online drives.
2908 */
2909 if (vs->vs_aux == VDEV_AUX_SPARED)
2910 state = gettext("INUSE");
2911 else if (vs->vs_state == VDEV_STATE_HEALTHY)
2912 state = gettext("AVAIL");
2913 }
2914
2915 /*
2916 * If '-e' is specified then top-level vdevs and their children
2917 * can be pruned if all of their leaves are healthy.
2918 */
2919 if (cb->cb_print_unhealthy && depth > 0 &&
2920 for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) {
2921 return;
2922 }
2923
2924 (void) printf_color(health_str_to_color(state),
2925 "\t%*s%-*s %-8s", depth, "", cb->cb_namewidth - depth,
2926 name, state);
2927
2928 if (!isspare) {
2929 if (vs->vs_read_errors)
2930 rcolor = ANSI_RED;
2931
2932 if (vs->vs_write_errors)
2933 wcolor = ANSI_RED;
2934
2935 if (vs->vs_checksum_errors)
2936 ccolor = ANSI_RED;
2937
2938 if (vs->vs_slow_ios)
2939 scolor = ANSI_BLUE;
2940
2941 if (cb->cb_literal) {
2942 (void) fputc(' ', stdout);
2943 (void) printf_color(rcolor, "%5llu",
2944 (u_longlong_t)vs->vs_read_errors);
2945 (void) fputc(' ', stdout);
2946 (void) printf_color(wcolor, "%5llu",
2947 (u_longlong_t)vs->vs_write_errors);
2948 (void) fputc(' ', stdout);
2949 (void) printf_color(ccolor, "%5llu",
2950 (u_longlong_t)vs->vs_checksum_errors);
2951 } else {
2952 zfs_nicenum(vs->vs_read_errors, rbuf, sizeof (rbuf));
2953 zfs_nicenum(vs->vs_write_errors, wbuf, sizeof (wbuf));
2954 zfs_nicenum(vs->vs_checksum_errors, cbuf,
2955 sizeof (cbuf));
2956 (void) fputc(' ', stdout);
2957 (void) printf_color(rcolor, "%5s", rbuf);
2958 (void) fputc(' ', stdout);
2959 (void) printf_color(wcolor, "%5s", wbuf);
2960 (void) fputc(' ', stdout);
2961 (void) printf_color(ccolor, "%5s", cbuf);
2962 }
2963 if (cb->cb_print_slow_ios) {
2964 if (children == 0) {
2965 /* Only leafs vdevs have slow IOs */
2966 zfs_nicenum(vs->vs_slow_ios, rbuf,
2967 sizeof (rbuf));
2968 } else {
2969 (void) snprintf(rbuf, sizeof (rbuf), "-");
2970 }
2971
2972 if (cb->cb_literal)
2973 (void) printf_color(scolor, " %5llu",
2974 (u_longlong_t)vs->vs_slow_ios);
2975 else
2976 (void) printf_color(scolor, " %5s", rbuf);
2977 }
2978 if (cb->cb_print_power) {
2979 if (children == 0) {
2980 /* Only leaf vdevs have physical slots */
2981 switch (zpool_power_current_state(zhp, (char *)
2982 fnvlist_lookup_string(nv,
2983 ZPOOL_CONFIG_PATH))) {
2984 case 0:
2985 (void) printf_color(ANSI_RED, " %5s",
2986 gettext("off"));
2987 break;
2988 case 1:
2989 printf(" %5s", gettext("on"));
2990 break;
2991 default:
2992 printf(" %5s", "-");
2993 }
2994 } else {
2995 printf(" %5s", "-");
2996 }
2997 }
2998 if (VDEV_STAT_VALID(vs_dio_verify_errors, vsc) &&
2999 cb->cb_print_dio_verify) {
3000 zfs_nicenum(vs->vs_dio_verify_errors, dbuf,
3001 sizeof (dbuf));
3002
3003 if (cb->cb_literal)
3004 printf(" %5llu",
3005 (u_longlong_t)vs->vs_dio_verify_errors);
3006 else
3007 printf(" %5s", dbuf);
3008 }
3009 }
3010
3011 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3012 ¬present) == 0) {
3013 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
3014 (void) printf(" %s %s", gettext("was"), path);
3015 } else if (vs->vs_aux != 0) {
3016 (void) printf(" ");
3017 color_start(ANSI_RED);
3018 switch (vs->vs_aux) {
3019 case VDEV_AUX_OPEN_FAILED:
3020 (void) printf(gettext("cannot open"));
3021 break;
3022
3023 case VDEV_AUX_BAD_GUID_SUM:
3024 (void) printf(gettext("missing device"));
3025 break;
3026
3027 case VDEV_AUX_NO_REPLICAS:
3028 (void) printf(gettext("insufficient replicas"));
3029 break;
3030
3031 case VDEV_AUX_VERSION_NEWER:
3032 (void) printf(gettext("newer version"));
3033 break;
3034
3035 case VDEV_AUX_UNSUP_FEAT:
3036 (void) printf(gettext("unsupported feature(s)"));
3037 break;
3038
3039 case VDEV_AUX_ASHIFT_TOO_BIG:
3040 (void) printf(gettext("unsupported minimum blocksize"));
3041 break;
3042
3043 case VDEV_AUX_SPARED:
3044 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3045 &spare_cb.cb_guid) == 0);
3046 if (zpool_iter(g_zfs, find_spare, &spare_cb) == 1) {
3047 if (strcmp(zpool_get_name(spare_cb.cb_zhp),
3048 zpool_get_name(zhp)) == 0)
3049 (void) printf(gettext("currently in "
3050 "use"));
3051 else
3052 (void) printf(gettext("in use by "
3053 "pool '%s'"),
3054 zpool_get_name(spare_cb.cb_zhp));
3055 zpool_close(spare_cb.cb_zhp);
3056 } else {
3057 (void) printf(gettext("currently in use"));
3058 }
3059 break;
3060
3061 case VDEV_AUX_ERR_EXCEEDED:
3062 if (vs->vs_read_errors + vs->vs_write_errors +
3063 vs->vs_checksum_errors == 0 && children == 0 &&
3064 vs->vs_slow_ios > 0) {
3065 (void) printf(gettext("too many slow I/Os"));
3066 } else {
3067 (void) printf(gettext("too many errors"));
3068 }
3069 break;
3070
3071 case VDEV_AUX_IO_FAILURE:
3072 (void) printf(gettext("experienced I/O failures"));
3073 break;
3074
3075 case VDEV_AUX_BAD_LOG:
3076 (void) printf(gettext("bad intent log"));
3077 break;
3078
3079 case VDEV_AUX_EXTERNAL:
3080 (void) printf(gettext("external device fault"));
3081 break;
3082
3083 case VDEV_AUX_SPLIT_POOL:
3084 (void) printf(gettext("split into new pool"));
3085 break;
3086
3087 case VDEV_AUX_ACTIVE:
3088 (void) printf(gettext("currently in use"));
3089 break;
3090
3091 case VDEV_AUX_CHILDREN_OFFLINE:
3092 (void) printf(gettext("all children offline"));
3093 break;
3094
3095 case VDEV_AUX_BAD_LABEL:
3096 (void) printf(gettext("invalid label"));
3097 break;
3098
3099 default:
3100 (void) printf(gettext("corrupted data"));
3101 break;
3102 }
3103 color_end();
3104 } else if (children == 0 && !isspare &&
3105 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL &&
3106 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
3107 vs->vs_configured_ashift < vs->vs_physical_ashift) {
3108 (void) printf(
3109 gettext(" block size: %dB configured, %dB native"),
3110 1 << vs->vs_configured_ashift, 1 << vs->vs_physical_ashift);
3111 }
3112
3113 if (vs->vs_scan_removing != 0) {
3114 (void) printf(gettext(" (removing)"));
3115 } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) {
3116 (void) printf(gettext(" (non-allocating)"));
3117 }
3118
3119 /* The root vdev has the scrub/resilver stats */
3120 root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3121 ZPOOL_CONFIG_VDEV_TREE);
3122 (void) nvlist_lookup_uint64_array(root, ZPOOL_CONFIG_SCAN_STATS,
3123 (uint64_t **)&ps, &c);
3124
3125 /*
3126 * If you force fault a drive that's resilvering, its scan stats can
3127 * get frozen in time, giving the false impression that it's
3128 * being resilvered. That's why we check the state to see if the vdev
3129 * is healthy before reporting "resilvering" or "repairing".
3130 */
3131 if (ps != NULL && ps->pss_state == DSS_SCANNING && children == 0 &&
3132 vs->vs_state == VDEV_STATE_HEALTHY) {
3133 if (vs->vs_scan_processed != 0) {
3134 (void) printf(gettext(" (%s)"),
3135 (ps->pss_func == POOL_SCAN_RESILVER) ?
3136 "resilvering" : "repairing");
3137 } else if (vs->vs_resilver_deferred) {
3138 (void) printf(gettext(" (awaiting resilver)"));
3139 }
3140 }
3141
3142 /* The top-level vdevs have the rebuild stats */
3143 if (vrs != NULL && vrs->vrs_state == VDEV_REBUILD_ACTIVE &&
3144 children == 0 && vs->vs_state == VDEV_STATE_HEALTHY) {
3145 if (vs->vs_rebuild_processed != 0) {
3146 (void) printf(gettext(" (resilvering)"));
3147 }
3148 }
3149
3150 if (cb->vcdl != NULL) {
3151 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3152 printf(" ");
3153 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
3154 }
3155 }
3156
3157 /* Display vdev initialization and trim status for leaves. */
3158 if (children == 0) {
3159 print_status_initialize(vs, cb->cb_print_vdev_init);
3160 print_status_trim(vs, cb->cb_print_vdev_trim);
3161 }
3162
3163 (void) printf("\n");
3164
3165 for (c = 0; c < children; c++) {
3166 uint64_t islog = B_FALSE, ishole = B_FALSE;
3167
3168 /* Don't print logs or holes here */
3169 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3170 &islog);
3171 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3172 &ishole);
3173 if (islog || ishole)
3174 continue;
3175 /* Only print normal classes here */
3176 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
3177 continue;
3178
3179 /* Provide vdev_rebuild_stats to children if available */
3180 if (vrs == NULL) {
3181 (void) nvlist_lookup_uint64_array(nv,
3182 ZPOOL_CONFIG_REBUILD_STATS,
3183 (uint64_t **)&vrs, &i);
3184 }
3185
3186 vname = zpool_vdev_name(g_zfs, zhp, child[c],
3187 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
3188 print_status_config(zhp, cb, vname, child[c], depth + 2,
3189 isspare, vrs);
3190 free(vname);
3191 }
3192 }
3193
3194 /*
3195 * Print the configuration of an exported pool. Iterate over all vdevs in the
3196 * pool, printing out the name and status for each one.
3197 */
3198 static void
print_import_config(status_cbdata_t * cb,const char * name,nvlist_t * nv,int depth)3199 print_import_config(status_cbdata_t *cb, const char *name, nvlist_t *nv,
3200 int depth)
3201 {
3202 nvlist_t **child;
3203 uint_t c, children;
3204 vdev_stat_t *vs;
3205 const char *type;
3206 char *vname;
3207
3208 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3209 if (strcmp(type, VDEV_TYPE_MISSING) == 0 ||
3210 strcmp(type, VDEV_TYPE_HOLE) == 0)
3211 return;
3212
3213 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3214 (uint64_t **)&vs, &c) == 0);
3215
3216 (void) printf("\t%*s%-*s", depth, "", cb->cb_namewidth - depth, name);
3217 (void) printf(" %s", zpool_state_to_name(vs->vs_state, vs->vs_aux));
3218
3219 if (vs->vs_aux != 0) {
3220 (void) printf(" ");
3221
3222 switch (vs->vs_aux) {
3223 case VDEV_AUX_OPEN_FAILED:
3224 (void) printf(gettext("cannot open"));
3225 break;
3226
3227 case VDEV_AUX_BAD_GUID_SUM:
3228 (void) printf(gettext("missing device"));
3229 break;
3230
3231 case VDEV_AUX_NO_REPLICAS:
3232 (void) printf(gettext("insufficient replicas"));
3233 break;
3234
3235 case VDEV_AUX_VERSION_NEWER:
3236 (void) printf(gettext("newer version"));
3237 break;
3238
3239 case VDEV_AUX_UNSUP_FEAT:
3240 (void) printf(gettext("unsupported feature(s)"));
3241 break;
3242
3243 case VDEV_AUX_ERR_EXCEEDED:
3244 (void) printf(gettext("too many errors"));
3245 break;
3246
3247 case VDEV_AUX_ACTIVE:
3248 (void) printf(gettext("currently in use"));
3249 break;
3250
3251 case VDEV_AUX_CHILDREN_OFFLINE:
3252 (void) printf(gettext("all children offline"));
3253 break;
3254
3255 case VDEV_AUX_BAD_LABEL:
3256 (void) printf(gettext("invalid label"));
3257 break;
3258
3259 default:
3260 (void) printf(gettext("corrupted data"));
3261 break;
3262 }
3263 }
3264 (void) printf("\n");
3265
3266 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3267 &child, &children) != 0)
3268 return;
3269
3270 for (c = 0; c < children; c++) {
3271 uint64_t is_log = B_FALSE;
3272
3273 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3274 &is_log);
3275 if (is_log)
3276 continue;
3277 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
3278 continue;
3279
3280 vname = zpool_vdev_name(g_zfs, NULL, child[c],
3281 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
3282 print_import_config(cb, vname, child[c], depth + 2);
3283 free(vname);
3284 }
3285
3286 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3287 &child, &children) == 0) {
3288 (void) printf(gettext("\tcache\n"));
3289 for (c = 0; c < children; c++) {
3290 vname = zpool_vdev_name(g_zfs, NULL, child[c],
3291 cb->cb_name_flags);
3292 (void) printf("\t %s\n", vname);
3293 free(vname);
3294 }
3295 }
3296
3297 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
3298 &child, &children) == 0) {
3299 (void) printf(gettext("\tspares\n"));
3300 for (c = 0; c < children; c++) {
3301 vname = zpool_vdev_name(g_zfs, NULL, child[c],
3302 cb->cb_name_flags);
3303 (void) printf("\t %s\n", vname);
3304 free(vname);
3305 }
3306 }
3307 }
3308
3309 /*
3310 * Print specialized class vdevs.
3311 *
3312 * These are recorded as top level vdevs in the main pool child array
3313 * but with "is_log" set to 1 or an "alloc_bias" string. We use either
3314 * print_status_config() or print_import_config() to print the top level
3315 * class vdevs then any of their children (eg mirrored slogs) are printed
3316 * recursively - which works because only the top level vdev is marked.
3317 */
3318 static void
print_class_vdevs(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nv,const char * class)3319 print_class_vdevs(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
3320 const char *class)
3321 {
3322 uint_t c, children;
3323 nvlist_t **child;
3324 boolean_t printed = B_FALSE;
3325
3326 assert(zhp != NULL || !cb->cb_verbose);
3327
3328 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
3329 &children) != 0)
3330 return;
3331
3332 for (c = 0; c < children; c++) {
3333 uint64_t is_log = B_FALSE;
3334 const char *bias = NULL;
3335 const char *type = NULL;
3336
3337 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3338 &is_log);
3339
3340 if (is_log) {
3341 bias = (char *)VDEV_ALLOC_CLASS_LOGS;
3342 } else {
3343 (void) nvlist_lookup_string(child[c],
3344 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
3345 (void) nvlist_lookup_string(child[c],
3346 ZPOOL_CONFIG_TYPE, &type);
3347 }
3348
3349 if (bias == NULL || strcmp(bias, class) != 0)
3350 continue;
3351 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
3352 continue;
3353
3354 if (!printed) {
3355 (void) printf("\t%s\t\n", gettext(class));
3356 printed = B_TRUE;
3357 }
3358
3359 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
3360 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
3361 if (cb->cb_print_status)
3362 print_status_config(zhp, cb, name, child[c], 2,
3363 B_FALSE, NULL);
3364 else
3365 print_import_config(cb, name, child[c], 2);
3366 free(name);
3367 }
3368 }
3369
3370 /*
3371 * Display the status for the given pool.
3372 */
3373 static int
show_import(nvlist_t * config,boolean_t report_error)3374 show_import(nvlist_t *config, boolean_t report_error)
3375 {
3376 uint64_t pool_state;
3377 vdev_stat_t *vs;
3378 const char *name;
3379 uint64_t guid;
3380 uint64_t hostid = 0;
3381 const char *msgid;
3382 const char *hostname = "unknown";
3383 nvlist_t *nvroot, *nvinfo;
3384 zpool_status_t reason;
3385 zpool_errata_t errata;
3386 const char *health;
3387 uint_t vsc;
3388 const char *comment;
3389 const char *indent;
3390 char buf[2048];
3391 status_cbdata_t cb = { 0 };
3392
3393 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3394 &name) == 0);
3395 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3396 &guid) == 0);
3397 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3398 &pool_state) == 0);
3399 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3400 &nvroot) == 0);
3401
3402 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
3403 (uint64_t **)&vs, &vsc) == 0);
3404 health = zpool_state_to_name(vs->vs_state, vs->vs_aux);
3405
3406 reason = zpool_import_status(config, &msgid, &errata);
3407
3408 /*
3409 * If we're importing using a cachefile, then we won't report any
3410 * errors unless we are in the scan phase of the import.
3411 */
3412 if (reason != ZPOOL_STATUS_OK && !report_error)
3413 return (reason);
3414
3415 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) {
3416 indent = " ";
3417 } else {
3418 comment = NULL;
3419 indent = "";
3420 }
3421
3422 (void) printf(gettext("%s pool: %s\n"), indent, name);
3423 (void) printf(gettext("%s id: %llu\n"), indent, (u_longlong_t)guid);
3424 (void) printf(gettext("%s state: %s"), indent, health);
3425 if (pool_state == POOL_STATE_DESTROYED)
3426 (void) printf(gettext(" (DESTROYED)"));
3427 (void) printf("\n");
3428
3429 if (reason != ZPOOL_STATUS_OK) {
3430 (void) printf("%s", indent);
3431 (void) printf_color(ANSI_BOLD, gettext("status: "));
3432 }
3433 switch (reason) {
3434 case ZPOOL_STATUS_MISSING_DEV_R:
3435 case ZPOOL_STATUS_MISSING_DEV_NR:
3436 case ZPOOL_STATUS_BAD_GUID_SUM:
3437 (void) printf_color(ANSI_YELLOW, gettext("One or more devices "
3438 "are missing from the system.\n"));
3439 break;
3440
3441 case ZPOOL_STATUS_CORRUPT_LABEL_R:
3442 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
3443 (void) printf_color(ANSI_YELLOW, gettext("One or more devices "
3444 "contains corrupted data.\n"));
3445 break;
3446
3447 case ZPOOL_STATUS_CORRUPT_DATA:
3448 (void) printf_color(ANSI_YELLOW, gettext("The pool data is "
3449 "corrupted.\n"));
3450 break;
3451
3452 case ZPOOL_STATUS_OFFLINE_DEV:
3453 (void) printf_color(ANSI_YELLOW, gettext("One or more devices "
3454 "are offlined.\n"));
3455 break;
3456
3457 case ZPOOL_STATUS_CORRUPT_POOL:
3458 (void) printf_color(ANSI_YELLOW, gettext("The pool metadata is "
3459 "incomplete or corrupted.\n"));
3460 break;
3461
3462 case ZPOOL_STATUS_VERSION_OLDER:
3463 (void) printf_color(ANSI_YELLOW, gettext("The pool is "
3464 "formatted using a legacy on-disk version.\n"));
3465 break;
3466
3467 case ZPOOL_STATUS_VERSION_NEWER:
3468 (void) printf_color(ANSI_YELLOW, gettext("The pool is "
3469 "formatted using an incompatible version.\n"));
3470 break;
3471
3472 case ZPOOL_STATUS_FEAT_DISABLED:
3473 (void) printf_color(ANSI_YELLOW, gettext("Some supported "
3474 "features are not enabled on the pool.\n"
3475 "\t%s(Note that they may be intentionally disabled if the\n"
3476 "\t%s'compatibility' property is set.)\n"), indent, indent);
3477 break;
3478
3479 case ZPOOL_STATUS_COMPATIBILITY_ERR:
3480 (void) printf_color(ANSI_YELLOW, gettext("Error reading or "
3481 "parsing the file(s) indicated by the 'compatibility'\n"
3482 "\t%sproperty.\n"), indent);
3483 break;
3484
3485 case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
3486 (void) printf_color(ANSI_YELLOW, gettext("One or more features "
3487 "are enabled on the pool despite not being\n"
3488 "\t%srequested by the 'compatibility' property.\n"),
3489 indent);
3490 break;
3491
3492 case ZPOOL_STATUS_UNSUP_FEAT_READ:
3493 (void) printf_color(ANSI_YELLOW, gettext("The pool uses the "
3494 "following feature(s) not supported on this system:\n"));
3495 color_start(ANSI_YELLOW);
3496 zpool_collect_unsup_feat(config, buf, 2048);
3497 (void) printf("%s", buf);
3498 color_end();
3499 break;
3500
3501 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
3502 (void) printf_color(ANSI_YELLOW, gettext("The pool can only be "
3503 "accessed in read-only mode on this system. It\n"
3504 "\t%scannot be accessed in read-write mode because it uses "
3505 "the following\n"
3506 "\t%sfeature(s) not supported on this system:\n"),
3507 indent, indent);
3508 color_start(ANSI_YELLOW);
3509 zpool_collect_unsup_feat(config, buf, 2048);
3510 (void) printf("%s", buf);
3511 color_end();
3512 break;
3513
3514 case ZPOOL_STATUS_HOSTID_ACTIVE:
3515 (void) printf_color(ANSI_YELLOW, gettext("The pool is "
3516 "currently imported by another system.\n"));
3517 break;
3518
3519 case ZPOOL_STATUS_HOSTID_REQUIRED:
3520 (void) printf_color(ANSI_YELLOW, gettext("The pool has the "
3521 "multihost property on. It cannot\n"
3522 "\t%sbe safely imported when the system hostid is not "
3523 "set.\n"), indent);
3524 break;
3525
3526 case ZPOOL_STATUS_HOSTID_MISMATCH:
3527 (void) printf_color(ANSI_YELLOW, gettext("The pool was last "
3528 "accessed by another system.\n"));
3529 break;
3530
3531 case ZPOOL_STATUS_FAULTED_DEV_R:
3532 case ZPOOL_STATUS_FAULTED_DEV_NR:
3533 (void) printf_color(ANSI_YELLOW, gettext("One or more devices "
3534 "are faulted.\n"));
3535 break;
3536
3537 case ZPOOL_STATUS_BAD_LOG:
3538 (void) printf_color(ANSI_YELLOW, gettext("An intent log record "
3539 "cannot be read.\n"));
3540 break;
3541
3542 case ZPOOL_STATUS_RESILVERING:
3543 case ZPOOL_STATUS_REBUILDING:
3544 (void) printf_color(ANSI_YELLOW, gettext("One or more devices "
3545 "were being resilvered.\n"));
3546 break;
3547
3548 case ZPOOL_STATUS_ERRATA:
3549 (void) printf_color(ANSI_YELLOW,
3550 gettext("Errata #%d detected.\n"),
3551 errata);
3552 break;
3553
3554 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
3555 (void) printf_color(ANSI_YELLOW, gettext("One or more devices "
3556 "are configured to use a non-native block size.\n"
3557 "\t%sExpect reduced performance.\n"), indent);
3558 break;
3559
3560 default:
3561 /*
3562 * No other status can be seen when importing pools.
3563 */
3564 assert(reason == ZPOOL_STATUS_OK);
3565 }
3566
3567 /*
3568 * Print out an action according to the overall state of the pool.
3569 */
3570 if (vs->vs_state != VDEV_STATE_HEALTHY ||
3571 reason != ZPOOL_STATUS_ERRATA || errata != ZPOOL_ERRATA_NONE) {
3572 (void) printf("%s", indent);
3573 (void) printf(gettext("action: "));
3574 }
3575 if (vs->vs_state == VDEV_STATE_HEALTHY) {
3576 if (reason == ZPOOL_STATUS_VERSION_OLDER ||
3577 reason == ZPOOL_STATUS_FEAT_DISABLED) {
3578 (void) printf(gettext("The pool can be imported using "
3579 "its name or numeric identifier, though\n"
3580 "\t%ssome features will not be available without "
3581 "an explicit 'zpool upgrade'.\n"), indent);
3582 } else if (reason == ZPOOL_STATUS_COMPATIBILITY_ERR) {
3583 (void) printf(gettext("The pool can be imported using "
3584 "its name or numeric\n"
3585 "\t%sidentifier, though the file(s) indicated by "
3586 "its 'compatibility'\n"
3587 "\t%sproperty cannot be parsed at this time.\n"),
3588 indent, indent);
3589 } else if (reason == ZPOOL_STATUS_HOSTID_MISMATCH) {
3590 (void) printf(gettext("The pool can be imported using "
3591 "its name or numeric identifier and\n"
3592 "\t%sthe '-f' flag.\n"), indent);
3593 } else if (reason == ZPOOL_STATUS_ERRATA) {
3594 switch (errata) {
3595 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
3596 (void) printf(gettext("The pool can be "
3597 "imported using its name or numeric "
3598 "identifier,\n"
3599 "\t%showever there is a compatibility "
3600 "issue which should be corrected\n"
3601 "\t%sby running 'zpool scrub'\n"),
3602 indent, indent);
3603 break;
3604
3605 case ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY:
3606 (void) printf(gettext("The pool cannot be "
3607 "imported with this version of ZFS due to\n"
3608 "\t%san active asynchronous destroy. "
3609 "Revert to an earlier version\n"
3610 "\t%sand allow the destroy to complete "
3611 "before updating.\n"), indent, indent);
3612 break;
3613
3614 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
3615 (void) printf(gettext("Existing encrypted "
3616 "datasets contain an on-disk "
3617 "incompatibility, which\n"
3618 "\t%sneeds to be corrected. Backup these "
3619 "datasets to new encrypted datasets\n"
3620 "\t%sand destroy the old ones.\n"),
3621 indent, indent);
3622 break;
3623
3624 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
3625 (void) printf(gettext("Existing encrypted "
3626 "snapshots and bookmarks contain an "
3627 "on-disk\n"
3628 "\t%sincompatibility. This may cause "
3629 "on-disk corruption if they are used\n"
3630 "\t%swith 'zfs recv'. To correct the "
3631 "issue, enable the bookmark_v2 feature.\n"
3632 "\t%sNo additional action is needed if "
3633 "there are no encrypted snapshots or\n"
3634 "\t%sbookmarks. If preserving the "
3635 "encrypted snapshots and bookmarks is\n"
3636 "\t%srequired, use a non-raw send to "
3637 "backup and restore them. Alternately,\n"
3638 "\t%sthey may be removed to resolve the "
3639 "incompatibility.\n"), indent, indent,
3640 indent, indent, indent, indent);
3641 break;
3642 default:
3643 /*
3644 * All errata must contain an action message.
3645 */
3646 assert(errata == ZPOOL_ERRATA_NONE);
3647 }
3648 } else {
3649 (void) printf(gettext("The pool can be imported using "
3650 "its name or numeric identifier.\n"));
3651 }
3652 } else if (vs->vs_state == VDEV_STATE_DEGRADED) {
3653 (void) printf(gettext("The pool can be imported despite "
3654 "missing or damaged devices. The\n"
3655 "\t%sfault tolerance of the pool may be compromised if "
3656 "imported.\n"), indent);
3657 } else {
3658 switch (reason) {
3659 case ZPOOL_STATUS_VERSION_NEWER:
3660 (void) printf(gettext("The pool cannot be imported. "
3661 "Access the pool on a system running newer\n"
3662 "\t%ssoftware, or recreate the pool from "
3663 "backup.\n"), indent);
3664 break;
3665 case ZPOOL_STATUS_UNSUP_FEAT_READ:
3666 (void) printf(gettext("The pool cannot be imported. "
3667 "Access the pool on a system that supports\n"
3668 "\t%sthe required feature(s), or recreate the pool "
3669 "from backup.\n"), indent);
3670 break;
3671 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
3672 (void) printf(gettext("The pool cannot be imported in "
3673 "read-write mode. Import the pool with\n"
3674 "\t%s'-o readonly=on', access the pool on a system "
3675 "that supports the\n"
3676 "\t%srequired feature(s), or recreate the pool "
3677 "from backup.\n"), indent, indent);
3678 break;
3679 case ZPOOL_STATUS_MISSING_DEV_R:
3680 case ZPOOL_STATUS_MISSING_DEV_NR:
3681 case ZPOOL_STATUS_BAD_GUID_SUM:
3682 (void) printf(gettext("The pool cannot be imported. "
3683 "Attach the missing\n"
3684 "\t%sdevices and try again.\n"), indent);
3685 break;
3686 case ZPOOL_STATUS_HOSTID_ACTIVE:
3687 VERIFY0(nvlist_lookup_nvlist(config,
3688 ZPOOL_CONFIG_LOAD_INFO, &nvinfo));
3689
3690 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3691 hostname = fnvlist_lookup_string(nvinfo,
3692 ZPOOL_CONFIG_MMP_HOSTNAME);
3693
3694 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3695 hostid = fnvlist_lookup_uint64(nvinfo,
3696 ZPOOL_CONFIG_MMP_HOSTID);
3697
3698 (void) printf(gettext("The pool must be exported from "
3699 "%s (hostid=%"PRIx64")\n"
3700 "\t%sbefore it can be safely imported.\n"),
3701 hostname, hostid, indent);
3702 break;
3703 case ZPOOL_STATUS_HOSTID_REQUIRED:
3704 (void) printf(gettext("Set a unique system hostid with "
3705 "the zgenhostid(8) command.\n"));
3706 break;
3707 case ZPOOL_STATUS_CORRUPT_POOL:
3708 (void) printf(gettext("The pool cannot be imported due "
3709 "to missing or damaged devices. Ensure\n"
3710 "\t%sall devices are present and not in use by "
3711 "another subsystem.\n"), indent);
3712 break;
3713 default:
3714 (void) printf(gettext("The pool cannot be imported due "
3715 "to damaged devices or data.\n"));
3716 }
3717 }
3718
3719 /* Print the comment attached to the pool. */
3720 if (comment != NULL)
3721 (void) printf(gettext("comment: %s\n"), comment);
3722
3723 /*
3724 * If the state is "closed" or "can't open", and the aux state
3725 * is "corrupt data":
3726 */
3727 if ((vs->vs_state == VDEV_STATE_CLOSED ||
3728 vs->vs_state == VDEV_STATE_CANT_OPEN) &&
3729 vs->vs_aux == VDEV_AUX_CORRUPT_DATA) {
3730 if (pool_state == POOL_STATE_DESTROYED)
3731 (void) printf(gettext("\t%sThe pool was destroyed, "
3732 "but can be imported using the '-Df' flags.\n"),
3733 indent);
3734 else if (pool_state != POOL_STATE_EXPORTED)
3735 (void) printf(gettext("\t%sThe pool may be active on "
3736 "another system, but can be imported using\n"
3737 "\t%sthe '-f' flag.\n"), indent, indent);
3738 }
3739
3740 if (msgid != NULL) {
3741 (void) printf(gettext("%s see: "
3742 "https://openzfs.github.io/openzfs-docs/msg/%s\n"),
3743 indent, msgid);
3744 }
3745
3746 (void) printf(gettext("%sconfig:\n\n"), indent);
3747
3748 cb.cb_namewidth = max_width(NULL, nvroot, 0, strlen(name),
3749 VDEV_NAME_TYPE_ID);
3750 if (cb.cb_namewidth < 10)
3751 cb.cb_namewidth = 10;
3752
3753 print_import_config(&cb, name, nvroot, 0);
3754
3755 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_DEDUP);
3756 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
3757 print_class_vdevs(NULL, &cb, nvroot, VDEV_ALLOC_CLASS_LOGS);
3758
3759 if (reason == ZPOOL_STATUS_BAD_GUID_SUM) {
3760 (void) printf(gettext("\n\t%sAdditional devices are known to "
3761 "be part of this pool, though their\n"
3762 "\t%sexact configuration cannot be determined.\n"),
3763 indent, indent);
3764 }
3765 return (0);
3766 }
3767
3768 static boolean_t
zfs_force_import_required(nvlist_t * config)3769 zfs_force_import_required(nvlist_t *config)
3770 {
3771 uint64_t state;
3772 uint64_t hostid = 0;
3773 nvlist_t *nvinfo;
3774
3775 state = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE);
3776 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3777
3778 /*
3779 * The hostid on LOAD_INFO comes from the MOS label via
3780 * spa_tryimport(). If its not there then we're likely talking to an
3781 * older kernel, so use the top one, which will be from the label
3782 * discovered in zpool_find_import(), or if a cachefile is in use, the
3783 * local hostid.
3784 */
3785 if (nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_HOSTID, &hostid) != 0)
3786 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID,
3787 &hostid);
3788
3789 if (state != POOL_STATE_EXPORTED && hostid != get_system_hostid())
3790 return (B_TRUE);
3791
3792 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE)) {
3793 mmp_state_t mmp_state = fnvlist_lookup_uint64(nvinfo,
3794 ZPOOL_CONFIG_MMP_STATE);
3795
3796 if (mmp_state != MMP_STATE_INACTIVE)
3797 return (B_TRUE);
3798 }
3799
3800 return (B_FALSE);
3801 }
3802
3803 /*
3804 * Perform the import for the given configuration. This passes the heavy
3805 * lifting off to zpool_import_props(), and then mounts the datasets contained
3806 * within the pool.
3807 */
3808 static int
do_import(nvlist_t * config,const char * newname,const char * mntopts,nvlist_t * props,int flags,uint_t mntthreads)3809 do_import(nvlist_t *config, const char *newname, const char *mntopts,
3810 nvlist_t *props, int flags, uint_t mntthreads)
3811 {
3812 int ret = 0;
3813 int ms_status = 0;
3814 zpool_handle_t *zhp;
3815 const char *name;
3816 uint64_t version;
3817
3818 name = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
3819 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
3820
3821 if (!SPA_VERSION_IS_SUPPORTED(version)) {
3822 (void) fprintf(stderr, gettext("cannot import '%s': pool "
3823 "is formatted using an unsupported ZFS version\n"), name);
3824 return (1);
3825 } else if (zfs_force_import_required(config) &&
3826 !(flags & ZFS_IMPORT_ANY_HOST)) {
3827 mmp_state_t mmp_state = MMP_STATE_INACTIVE;
3828 nvlist_t *nvinfo;
3829
3830 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3831 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_STATE))
3832 mmp_state = fnvlist_lookup_uint64(nvinfo,
3833 ZPOOL_CONFIG_MMP_STATE);
3834
3835 if (mmp_state == MMP_STATE_ACTIVE) {
3836 const char *hostname = "<unknown>";
3837 uint64_t hostid = 0;
3838
3839 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTNAME))
3840 hostname = fnvlist_lookup_string(nvinfo,
3841 ZPOOL_CONFIG_MMP_HOSTNAME);
3842
3843 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_HOSTID))
3844 hostid = fnvlist_lookup_uint64(nvinfo,
3845 ZPOOL_CONFIG_MMP_HOSTID);
3846
3847 (void) fprintf(stderr, gettext("cannot import '%s': "
3848 "pool is imported on %s (hostid: "
3849 "0x%"PRIx64")\nExport the pool on the other "
3850 "system, then run 'zpool import'.\n"),
3851 name, hostname, hostid);
3852 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
3853 (void) fprintf(stderr, gettext("Cannot import '%s': "
3854 "pool has the multihost property on and the\n"
3855 "system's hostid is not set. Set a unique hostid "
3856 "with the zgenhostid(8) command.\n"), name);
3857 } else {
3858 const char *hostname = "<unknown>";
3859 time_t timestamp = 0;
3860 uint64_t hostid = 0;
3861
3862 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTNAME))
3863 hostname = fnvlist_lookup_string(nvinfo,
3864 ZPOOL_CONFIG_HOSTNAME);
3865 else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTNAME))
3866 hostname = fnvlist_lookup_string(config,
3867 ZPOOL_CONFIG_HOSTNAME);
3868
3869 if (nvlist_exists(config, ZPOOL_CONFIG_TIMESTAMP))
3870 timestamp = fnvlist_lookup_uint64(config,
3871 ZPOOL_CONFIG_TIMESTAMP);
3872
3873 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_HOSTID))
3874 hostid = fnvlist_lookup_uint64(nvinfo,
3875 ZPOOL_CONFIG_HOSTID);
3876 else if (nvlist_exists(config, ZPOOL_CONFIG_HOSTID))
3877 hostid = fnvlist_lookup_uint64(config,
3878 ZPOOL_CONFIG_HOSTID);
3879
3880 (void) fprintf(stderr, gettext("cannot import '%s': "
3881 "pool was previously in use from another system.\n"
3882 "Last accessed by %s (hostid=%"PRIx64") at %s"
3883 "The pool can be imported, use 'zpool import -f' "
3884 "to import the pool.\n"), name, hostname,
3885 hostid, ctime(×tamp));
3886 }
3887
3888 if (getenv("ZFS_LOAD_INFO_DEBUG"))
3889 dump_nvlist(nvinfo, 4);
3890
3891 return (1);
3892 }
3893
3894 if (zpool_import_props(g_zfs, config, newname, props, flags) != 0)
3895 return (1);
3896
3897 if (newname != NULL)
3898 name = newname;
3899
3900 if ((zhp = zpool_open_canfail(g_zfs, name)) == NULL)
3901 return (1);
3902
3903 /*
3904 * Loading keys is best effort. We don't want to return immediately
3905 * if it fails but we do want to give the error to the caller.
3906 */
3907 if (flags & ZFS_IMPORT_LOAD_KEYS &&
3908 zfs_crypto_attempt_load_keys(g_zfs, name) != 0)
3909 ret = 1;
3910
3911 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL &&
3912 !(flags & ZFS_IMPORT_ONLY)) {
3913 ms_status = zpool_enable_datasets(zhp, mntopts, 0, mntthreads);
3914 if (ms_status == EZFS_SHAREFAILED) {
3915 (void) fprintf(stderr, gettext("Import was "
3916 "successful, but unable to share some datasets\n"));
3917 } else if (ms_status == EZFS_MOUNTFAILED) {
3918 (void) fprintf(stderr, gettext("Import was "
3919 "successful, but unable to mount some datasets\n"));
3920 }
3921 }
3922
3923 zpool_close(zhp);
3924 return (ret);
3925 }
3926
3927 typedef struct import_parameters {
3928 nvlist_t *ip_config;
3929 const char *ip_mntopts;
3930 nvlist_t *ip_props;
3931 int ip_flags;
3932 uint_t ip_mntthreads;
3933 int *ip_err;
3934 } import_parameters_t;
3935
3936 static void
do_import_task(void * arg)3937 do_import_task(void *arg)
3938 {
3939 import_parameters_t *ip = arg;
3940 *ip->ip_err |= do_import(ip->ip_config, NULL, ip->ip_mntopts,
3941 ip->ip_props, ip->ip_flags, ip->ip_mntthreads);
3942 free(ip);
3943 }
3944
3945
3946 static int
import_pools(nvlist_t * pools,nvlist_t * props,char * mntopts,int flags,char * orig_name,char * new_name,importargs_t * import)3947 import_pools(nvlist_t *pools, nvlist_t *props, char *mntopts, int flags,
3948 char *orig_name, char *new_name, importargs_t *import)
3949 {
3950 nvlist_t *config = NULL;
3951 nvlist_t *found_config = NULL;
3952 uint64_t pool_state;
3953 boolean_t pool_specified = (import->poolname != NULL ||
3954 import->guid != 0);
3955 uint_t npools = 0;
3956
3957
3958 taskq_t *tq = NULL;
3959 if (import->do_all) {
3960 tq = taskq_create("zpool_import_all",
3961 5 * sysconf(_SC_NPROCESSORS_ONLN), minclsyspri, 1, INT_MAX,
3962 TASKQ_DYNAMIC);
3963 }
3964
3965 /*
3966 * At this point we have a list of import candidate configs. Even if
3967 * we were searching by pool name or guid, we still need to
3968 * post-process the list to deal with pool state and possible
3969 * duplicate names.
3970 */
3971 int err = 0;
3972 nvpair_t *elem = NULL;
3973 boolean_t first = B_TRUE;
3974 if (!pool_specified && import->do_all) {
3975 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL)
3976 npools++;
3977 }
3978 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3979
3980 verify(nvpair_value_nvlist(elem, &config) == 0);
3981
3982 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3983 &pool_state) == 0);
3984 if (!import->do_destroyed &&
3985 pool_state == POOL_STATE_DESTROYED)
3986 continue;
3987 if (import->do_destroyed &&
3988 pool_state != POOL_STATE_DESTROYED)
3989 continue;
3990
3991 verify(nvlist_add_nvlist(config, ZPOOL_LOAD_POLICY,
3992 import->policy) == 0);
3993
3994 if (!pool_specified) {
3995 if (first)
3996 first = B_FALSE;
3997 else if (!import->do_all)
3998 (void) fputc('\n', stdout);
3999
4000 if (import->do_all) {
4001 import_parameters_t *ip = safe_malloc(
4002 sizeof (import_parameters_t));
4003
4004 ip->ip_config = config;
4005 ip->ip_mntopts = mntopts;
4006 ip->ip_props = props;
4007 ip->ip_flags = flags;
4008 ip->ip_mntthreads = mount_tp_nthr / npools;
4009 ip->ip_err = &err;
4010
4011 (void) taskq_dispatch(tq, do_import_task,
4012 (void *)ip, TQ_SLEEP);
4013 } else {
4014 /*
4015 * If we're importing from cachefile, then
4016 * we don't want to report errors until we
4017 * are in the scan phase of the import. If
4018 * we get an error, then we return that error
4019 * to invoke the scan phase.
4020 */
4021 if (import->cachefile && !import->scan)
4022 err = show_import(config, B_FALSE);
4023 else
4024 (void) show_import(config, B_TRUE);
4025 }
4026 } else if (import->poolname != NULL) {
4027 const char *name;
4028
4029 /*
4030 * We are searching for a pool based on name.
4031 */
4032 verify(nvlist_lookup_string(config,
4033 ZPOOL_CONFIG_POOL_NAME, &name) == 0);
4034
4035 if (strcmp(name, import->poolname) == 0) {
4036 if (found_config != NULL) {
4037 (void) fprintf(stderr, gettext(
4038 "cannot import '%s': more than "
4039 "one matching pool\n"),
4040 import->poolname);
4041 (void) fprintf(stderr, gettext(
4042 "import by numeric ID instead\n"));
4043 err = B_TRUE;
4044 }
4045 found_config = config;
4046 }
4047 } else {
4048 uint64_t guid;
4049
4050 /*
4051 * Search for a pool by guid.
4052 */
4053 verify(nvlist_lookup_uint64(config,
4054 ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
4055
4056 if (guid == import->guid)
4057 found_config = config;
4058 }
4059 }
4060 if (import->do_all) {
4061 taskq_wait(tq);
4062 taskq_destroy(tq);
4063 }
4064
4065 /*
4066 * If we were searching for a specific pool, verify that we found a
4067 * pool, and then do the import.
4068 */
4069 if (pool_specified && err == 0) {
4070 if (found_config == NULL) {
4071 (void) fprintf(stderr, gettext("cannot import '%s': "
4072 "no such pool available\n"), orig_name);
4073 err = B_TRUE;
4074 } else {
4075 err |= do_import(found_config, new_name,
4076 mntopts, props, flags, mount_tp_nthr);
4077 }
4078 }
4079
4080 /*
4081 * If we were just looking for pools, report an error if none were
4082 * found.
4083 */
4084 if (!pool_specified && first)
4085 (void) fprintf(stderr,
4086 gettext("no pools available to import\n"));
4087 return (err);
4088 }
4089
4090 typedef struct target_exists_args {
4091 const char *poolname;
4092 uint64_t poolguid;
4093 } target_exists_args_t;
4094
4095 static int
name_or_guid_exists(zpool_handle_t * zhp,void * data)4096 name_or_guid_exists(zpool_handle_t *zhp, void *data)
4097 {
4098 target_exists_args_t *args = data;
4099 nvlist_t *config = zpool_get_config(zhp, NULL);
4100 int found = 0;
4101
4102 if (config == NULL)
4103 return (0);
4104
4105 if (args->poolname != NULL) {
4106 const char *pool_name;
4107
4108 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4109 &pool_name) == 0);
4110 if (strcmp(pool_name, args->poolname) == 0)
4111 found = 1;
4112 } else {
4113 uint64_t pool_guid;
4114
4115 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4116 &pool_guid) == 0);
4117 if (pool_guid == args->poolguid)
4118 found = 1;
4119 }
4120 zpool_close(zhp);
4121
4122 return (found);
4123 }
4124 /*
4125 * zpool checkpoint <pool>
4126 * checkpoint --discard <pool>
4127 *
4128 * -d Discard the checkpoint from a checkpointed
4129 * --discard pool.
4130 *
4131 * -w Wait for discarding a checkpoint to complete.
4132 * --wait
4133 *
4134 * Checkpoints the specified pool, by taking a "snapshot" of its
4135 * current state. A pool can only have one checkpoint at a time.
4136 */
4137 int
zpool_do_checkpoint(int argc,char ** argv)4138 zpool_do_checkpoint(int argc, char **argv)
4139 {
4140 boolean_t discard, wait;
4141 char *pool;
4142 zpool_handle_t *zhp;
4143 int c, err;
4144
4145 struct option long_options[] = {
4146 {"discard", no_argument, NULL, 'd'},
4147 {"wait", no_argument, NULL, 'w'},
4148 {0, 0, 0, 0}
4149 };
4150
4151 discard = B_FALSE;
4152 wait = B_FALSE;
4153 while ((c = getopt_long(argc, argv, ":dw", long_options, NULL)) != -1) {
4154 switch (c) {
4155 case 'd':
4156 discard = B_TRUE;
4157 break;
4158 case 'w':
4159 wait = B_TRUE;
4160 break;
4161 case '?':
4162 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4163 optopt);
4164 usage(B_FALSE);
4165 }
4166 }
4167
4168 if (wait && !discard) {
4169 (void) fprintf(stderr, gettext("--wait only valid when "
4170 "--discard also specified\n"));
4171 usage(B_FALSE);
4172 }
4173
4174 argc -= optind;
4175 argv += optind;
4176
4177 if (argc < 1) {
4178 (void) fprintf(stderr, gettext("missing pool argument\n"));
4179 usage(B_FALSE);
4180 }
4181
4182 if (argc > 1) {
4183 (void) fprintf(stderr, gettext("too many arguments\n"));
4184 usage(B_FALSE);
4185 }
4186
4187 pool = argv[0];
4188
4189 if ((zhp = zpool_open(g_zfs, pool)) == NULL) {
4190 /* As a special case, check for use of '/' in the name */
4191 if (strchr(pool, '/') != NULL)
4192 (void) fprintf(stderr, gettext("'zpool checkpoint' "
4193 "doesn't work on datasets. To save the state "
4194 "of a dataset from a specific point in time "
4195 "please use 'zfs snapshot'\n"));
4196 return (1);
4197 }
4198
4199 if (discard) {
4200 err = (zpool_discard_checkpoint(zhp) != 0);
4201 if (err == 0 && wait)
4202 err = zpool_wait(zhp, ZPOOL_WAIT_CKPT_DISCARD);
4203 } else {
4204 err = (zpool_checkpoint(zhp) != 0);
4205 }
4206
4207 zpool_close(zhp);
4208
4209 return (err);
4210 }
4211
4212 #define CHECKPOINT_OPT 1024
4213
4214 /*
4215 * zpool prefetch [-t <type>] <pool>
4216 *
4217 * Prefetchs a particular type of data in the specified pool.
4218 */
4219 int
zpool_do_prefetch(int argc,char ** argv)4220 zpool_do_prefetch(int argc, char **argv)
4221 {
4222 int c;
4223 char *poolname;
4224 char *typestr = NULL;
4225 zpool_prefetch_type_t type;
4226 zpool_handle_t *zhp;
4227 int err = 0;
4228
4229 while ((c = getopt(argc, argv, "t:")) != -1) {
4230 switch (c) {
4231 case 't':
4232 typestr = optarg;
4233 break;
4234 case ':':
4235 (void) fprintf(stderr, gettext("missing argument for "
4236 "'%c' option\n"), optopt);
4237 usage(B_FALSE);
4238 break;
4239 case '?':
4240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4241 optopt);
4242 usage(B_FALSE);
4243 }
4244 }
4245 argc -= optind;
4246 argv += optind;
4247
4248 if (argc < 1) {
4249 (void) fprintf(stderr, gettext("missing pool name argument\n"));
4250 usage(B_FALSE);
4251 }
4252
4253 if (argc > 1) {
4254 (void) fprintf(stderr, gettext("too many arguments\n"));
4255 usage(B_FALSE);
4256 }
4257
4258 poolname = argv[0];
4259
4260 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
4261 return (1);
4262
4263 if (typestr == NULL) {
4264 /* Prefetch all types */
4265 err = zpool_prefetch(zhp, ZPOOL_PREFETCH_DDT);
4266 if (err == 0)
4267 err = zpool_prefetch(zhp, ZPOOL_PREFETCH_BRT);
4268 } else {
4269 if (strcmp(typestr, "ddt") == 0) {
4270 type = ZPOOL_PREFETCH_DDT;
4271 } else if (strcmp(typestr, "brt") == 0) {
4272 type = ZPOOL_PREFETCH_BRT;
4273 } else {
4274 (void) fprintf(stderr,
4275 gettext("unsupported prefetch type\n"));
4276 zpool_close(zhp);
4277 usage(B_FALSE);
4278 }
4279 err = zpool_prefetch(zhp, type);
4280 }
4281
4282 zpool_close(zhp);
4283
4284 return (err);
4285 }
4286
4287 /*
4288 * zpool import [-d dir] [-D]
4289 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
4290 * [-d dir | -c cachefile | -s] [-f] -a
4291 * import [-o mntopts] [-o prop=value] ... [-R root] [-D] [-l]
4292 * [-d dir | -c cachefile | -s] [-f] [-n] [-F] <pool | id>
4293 * [newpool]
4294 *
4295 * -c Read pool information from a cachefile instead of searching
4296 * devices. If importing from a cachefile config fails, then
4297 * fallback to searching for devices only in the directories that
4298 * exist in the cachefile.
4299 *
4300 * -d Scan in a specific directory, other than /dev/. More than
4301 * one directory can be specified using multiple '-d' options.
4302 *
4303 * -D Scan for previously destroyed pools or import all or only
4304 * specified destroyed pools.
4305 *
4306 * -R Temporarily import the pool, with all mountpoints relative to
4307 * the given root. The pool will remain exported when the machine
4308 * is rebooted.
4309 *
4310 * -V Import even in the presence of faulted vdevs. This is an
4311 * intentionally undocumented option for testing purposes, and
4312 * treats the pool configuration as complete, leaving any bad
4313 * vdevs in the FAULTED state. In other words, it does verbatim
4314 * import.
4315 *
4316 * -f Force import, even if it appears that the pool is active.
4317 *
4318 * -F Attempt rewind if necessary.
4319 *
4320 * -n See if rewind would work, but don't actually rewind.
4321 *
4322 * -N Import the pool but don't mount datasets.
4323 *
4324 * -T Specify a starting txg to use for import. This option is
4325 * intentionally undocumented option for testing purposes.
4326 *
4327 * -a Import all pools found.
4328 *
4329 * -l Load encryption keys while importing.
4330 *
4331 * -o Set property=value and/or temporary mount options (without '=').
4332 *
4333 * -s Scan using the default search path, the libblkid cache will
4334 * not be consulted.
4335 *
4336 * --rewind-to-checkpoint
4337 * Import the pool and revert back to the checkpoint.
4338 *
4339 * The import command scans for pools to import, and import pools based on pool
4340 * name and GUID. The pool can also be renamed as part of the import process.
4341 */
4342 int
zpool_do_import(int argc,char ** argv)4343 zpool_do_import(int argc, char **argv)
4344 {
4345 char **searchdirs = NULL;
4346 char *env, *envdup = NULL;
4347 int nsearch = 0;
4348 int c;
4349 int err = 0;
4350 nvlist_t *pools = NULL;
4351 boolean_t do_all = B_FALSE;
4352 boolean_t do_destroyed = B_FALSE;
4353 char *mntopts = NULL;
4354 uint64_t searchguid = 0;
4355 char *searchname = NULL;
4356 char *propval;
4357 nvlist_t *policy = NULL;
4358 nvlist_t *props = NULL;
4359 int flags = ZFS_IMPORT_NORMAL;
4360 uint32_t rewind_policy = ZPOOL_NO_REWIND;
4361 boolean_t dryrun = B_FALSE;
4362 boolean_t do_rewind = B_FALSE;
4363 boolean_t xtreme_rewind = B_FALSE;
4364 boolean_t do_scan = B_FALSE;
4365 boolean_t pool_exists = B_FALSE;
4366 uint64_t txg = -1ULL;
4367 char *cachefile = NULL;
4368 importargs_t idata = { 0 };
4369 char *endptr;
4370
4371 struct option long_options[] = {
4372 {"rewind-to-checkpoint", no_argument, NULL, CHECKPOINT_OPT},
4373 {0, 0, 0, 0}
4374 };
4375
4376 /* check options */
4377 while ((c = getopt_long(argc, argv, ":aCc:d:DEfFlmnNo:R:stT:VX",
4378 long_options, NULL)) != -1) {
4379 switch (c) {
4380 case 'a':
4381 do_all = B_TRUE;
4382 break;
4383 case 'c':
4384 cachefile = optarg;
4385 break;
4386 case 'd':
4387 searchdirs = safe_realloc(searchdirs,
4388 (nsearch + 1) * sizeof (char *));
4389 searchdirs[nsearch++] = optarg;
4390 break;
4391 case 'D':
4392 do_destroyed = B_TRUE;
4393 break;
4394 case 'f':
4395 flags |= ZFS_IMPORT_ANY_HOST;
4396 break;
4397 case 'F':
4398 do_rewind = B_TRUE;
4399 break;
4400 case 'l':
4401 flags |= ZFS_IMPORT_LOAD_KEYS;
4402 break;
4403 case 'm':
4404 flags |= ZFS_IMPORT_MISSING_LOG;
4405 break;
4406 case 'n':
4407 dryrun = B_TRUE;
4408 break;
4409 case 'N':
4410 flags |= ZFS_IMPORT_ONLY;
4411 break;
4412 case 'o':
4413 if ((propval = strchr(optarg, '=')) != NULL) {
4414 *propval = '\0';
4415 propval++;
4416 if (add_prop_list(optarg, propval,
4417 &props, B_TRUE))
4418 goto error;
4419 } else {
4420 mntopts = optarg;
4421 }
4422 break;
4423 case 'R':
4424 if (add_prop_list(zpool_prop_to_name(
4425 ZPOOL_PROP_ALTROOT), optarg, &props, B_TRUE))
4426 goto error;
4427 if (add_prop_list_default(zpool_prop_to_name(
4428 ZPOOL_PROP_CACHEFILE), "none", &props))
4429 goto error;
4430 break;
4431 case 's':
4432 do_scan = B_TRUE;
4433 break;
4434 case 't':
4435 flags |= ZFS_IMPORT_TEMP_NAME;
4436 if (add_prop_list_default(zpool_prop_to_name(
4437 ZPOOL_PROP_CACHEFILE), "none", &props))
4438 goto error;
4439 break;
4440
4441 case 'T':
4442 errno = 0;
4443 txg = strtoull(optarg, &endptr, 0);
4444 if (errno != 0 || *endptr != '\0') {
4445 (void) fprintf(stderr,
4446 gettext("invalid txg value\n"));
4447 usage(B_FALSE);
4448 }
4449 rewind_policy = ZPOOL_DO_REWIND | ZPOOL_EXTREME_REWIND;
4450 break;
4451 case 'V':
4452 flags |= ZFS_IMPORT_VERBATIM;
4453 break;
4454 case 'X':
4455 xtreme_rewind = B_TRUE;
4456 break;
4457 case CHECKPOINT_OPT:
4458 flags |= ZFS_IMPORT_CHECKPOINT;
4459 break;
4460 case ':':
4461 (void) fprintf(stderr, gettext("missing argument for "
4462 "'%c' option\n"), optopt);
4463 usage(B_FALSE);
4464 break;
4465 case '?':
4466 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4467 optopt);
4468 usage(B_FALSE);
4469 }
4470 }
4471
4472 argc -= optind;
4473 argv += optind;
4474
4475 if (cachefile && nsearch != 0) {
4476 (void) fprintf(stderr, gettext("-c is incompatible with -d\n"));
4477 usage(B_FALSE);
4478 }
4479
4480 if (cachefile && do_scan) {
4481 (void) fprintf(stderr, gettext("-c is incompatible with -s\n"));
4482 usage(B_FALSE);
4483 }
4484
4485 if ((flags & ZFS_IMPORT_LOAD_KEYS) && (flags & ZFS_IMPORT_ONLY)) {
4486 (void) fprintf(stderr, gettext("-l is incompatible with -N\n"));
4487 usage(B_FALSE);
4488 }
4489
4490 if ((flags & ZFS_IMPORT_LOAD_KEYS) && !do_all && argc == 0) {
4491 (void) fprintf(stderr, gettext("-l is only meaningful during "
4492 "an import\n"));
4493 usage(B_FALSE);
4494 }
4495
4496 if ((dryrun || xtreme_rewind) && !do_rewind) {
4497 (void) fprintf(stderr,
4498 gettext("-n or -X only meaningful with -F\n"));
4499 usage(B_FALSE);
4500 }
4501 if (dryrun)
4502 rewind_policy = ZPOOL_TRY_REWIND;
4503 else if (do_rewind)
4504 rewind_policy = ZPOOL_DO_REWIND;
4505 if (xtreme_rewind)
4506 rewind_policy |= ZPOOL_EXTREME_REWIND;
4507
4508 /* In the future, we can capture further policy and include it here */
4509 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
4510 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, txg) != 0 ||
4511 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
4512 rewind_policy) != 0)
4513 goto error;
4514
4515 /* check argument count */
4516 if (do_all) {
4517 if (argc != 0) {
4518 (void) fprintf(stderr, gettext("too many arguments\n"));
4519 usage(B_FALSE);
4520 }
4521 } else {
4522 if (argc > 2) {
4523 (void) fprintf(stderr, gettext("too many arguments\n"));
4524 usage(B_FALSE);
4525 }
4526 }
4527
4528 /*
4529 * Check for the effective uid. We do this explicitly here because
4530 * otherwise any attempt to discover pools will silently fail.
4531 */
4532 if (argc == 0 && geteuid() != 0) {
4533 (void) fprintf(stderr, gettext("cannot "
4534 "discover pools: permission denied\n"));
4535
4536 free(searchdirs);
4537 nvlist_free(props);
4538 nvlist_free(policy);
4539 return (1);
4540 }
4541
4542 /*
4543 * Depending on the arguments given, we do one of the following:
4544 *
4545 * <none> Iterate through all pools and display information about
4546 * each one.
4547 *
4548 * -a Iterate through all pools and try to import each one.
4549 *
4550 * <id> Find the pool that corresponds to the given GUID/pool
4551 * name and import that one.
4552 *
4553 * -D Above options applies only to destroyed pools.
4554 */
4555 if (argc != 0) {
4556 char *endptr;
4557
4558 errno = 0;
4559 searchguid = strtoull(argv[0], &endptr, 10);
4560 if (errno != 0 || *endptr != '\0') {
4561 searchname = argv[0];
4562 searchguid = 0;
4563 }
4564
4565 /*
4566 * User specified a name or guid. Ensure it's unique.
4567 */
4568 target_exists_args_t search = {searchname, searchguid};
4569 pool_exists = zpool_iter(g_zfs, name_or_guid_exists, &search);
4570 }
4571
4572 /*
4573 * Check the environment for the preferred search path.
4574 */
4575 if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
4576 char *dir, *tmp = NULL;
4577
4578 envdup = strdup(env);
4579
4580 for (dir = strtok_r(envdup, ":", &tmp);
4581 dir != NULL;
4582 dir = strtok_r(NULL, ":", &tmp)) {
4583 searchdirs = safe_realloc(searchdirs,
4584 (nsearch + 1) * sizeof (char *));
4585 searchdirs[nsearch++] = dir;
4586 }
4587 }
4588
4589 idata.path = searchdirs;
4590 idata.paths = nsearch;
4591 idata.poolname = searchname;
4592 idata.guid = searchguid;
4593 idata.cachefile = cachefile;
4594 idata.scan = do_scan;
4595 idata.policy = policy;
4596 idata.do_destroyed = do_destroyed;
4597 idata.do_all = do_all;
4598
4599 libpc_handle_t lpch = {
4600 .lpc_lib_handle = g_zfs,
4601 .lpc_ops = &libzfs_config_ops,
4602 .lpc_printerr = B_TRUE
4603 };
4604 pools = zpool_search_import(&lpch, &idata);
4605
4606 if (pools != NULL && pool_exists &&
4607 (argc == 1 || strcmp(argv[0], argv[1]) == 0)) {
4608 (void) fprintf(stderr, gettext("cannot import '%s': "
4609 "a pool with that name already exists\n"),
4610 argv[0]);
4611 (void) fprintf(stderr, gettext("use the form '%s "
4612 "<pool | id> <newpool>' to give it a new name\n"),
4613 "zpool import");
4614 err = 1;
4615 } else if (pools == NULL && pool_exists) {
4616 (void) fprintf(stderr, gettext("cannot import '%s': "
4617 "a pool with that name is already created/imported,\n"),
4618 argv[0]);
4619 (void) fprintf(stderr, gettext("and no additional pools "
4620 "with that name were found\n"));
4621 err = 1;
4622 } else if (pools == NULL) {
4623 if (argc != 0) {
4624 (void) fprintf(stderr, gettext("cannot import '%s': "
4625 "no such pool available\n"), argv[0]);
4626 }
4627 err = 1;
4628 }
4629
4630 if (err == 1) {
4631 free(searchdirs);
4632 free(envdup);
4633 nvlist_free(policy);
4634 nvlist_free(pools);
4635 nvlist_free(props);
4636 return (1);
4637 }
4638
4639 err = import_pools(pools, props, mntopts, flags,
4640 argc >= 1 ? argv[0] : NULL, argc >= 2 ? argv[1] : NULL, &idata);
4641
4642 /*
4643 * If we're using the cachefile and we failed to import, then
4644 * fallback to scanning the directory for pools that match
4645 * those in the cachefile.
4646 */
4647 if (err != 0 && cachefile != NULL) {
4648 (void) printf(gettext("cachefile import failed, retrying\n"));
4649
4650 /*
4651 * We use the scan flag to gather the directories that exist
4652 * in the cachefile. If we need to fallback to searching for
4653 * the pool config, we will only search devices in these
4654 * directories.
4655 */
4656 idata.scan = B_TRUE;
4657 nvlist_free(pools);
4658 pools = zpool_search_import(&lpch, &idata);
4659
4660 err = import_pools(pools, props, mntopts, flags,
4661 argc >= 1 ? argv[0] : NULL, argc >= 2 ? argv[1] : NULL,
4662 &idata);
4663 }
4664
4665 error:
4666 nvlist_free(props);
4667 nvlist_free(pools);
4668 nvlist_free(policy);
4669 free(searchdirs);
4670 free(envdup);
4671
4672 return (err ? 1 : 0);
4673 }
4674
4675 /*
4676 * zpool sync [-f] [pool] ...
4677 *
4678 * -f (undocumented) force uberblock (and config including zpool cache file)
4679 * update.
4680 *
4681 * Sync the specified pool(s).
4682 * Without arguments "zpool sync" will sync all pools.
4683 * This command initiates TXG sync(s) and will return after the TXG(s) commit.
4684 *
4685 */
4686 static int
zpool_do_sync(int argc,char ** argv)4687 zpool_do_sync(int argc, char **argv)
4688 {
4689 int ret;
4690 boolean_t force = B_FALSE;
4691
4692 /* check options */
4693 while ((ret = getopt(argc, argv, "f")) != -1) {
4694 switch (ret) {
4695 case 'f':
4696 force = B_TRUE;
4697 break;
4698 case '?':
4699 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4700 optopt);
4701 usage(B_FALSE);
4702 }
4703 }
4704
4705 argc -= optind;
4706 argv += optind;
4707
4708 /* if argc == 0 we will execute zpool_sync_one on all pools */
4709 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
4710 B_FALSE, zpool_sync_one, &force);
4711
4712 return (ret);
4713 }
4714
4715 typedef struct iostat_cbdata {
4716 uint64_t cb_flags;
4717 int cb_namewidth;
4718 int cb_iteration;
4719 boolean_t cb_verbose;
4720 boolean_t cb_literal;
4721 boolean_t cb_scripted;
4722 zpool_list_t *cb_list;
4723 vdev_cmd_data_list_t *vcdl;
4724 vdev_cbdata_t cb_vdevs;
4725 } iostat_cbdata_t;
4726
4727 /* iostat labels */
4728 typedef struct name_and_columns {
4729 const char *name; /* Column name */
4730 unsigned int columns; /* Center name to this number of columns */
4731 } name_and_columns_t;
4732
4733 #define IOSTAT_MAX_LABELS 15 /* Max number of labels on one line */
4734
4735 static const name_and_columns_t iostat_top_labels[][IOSTAT_MAX_LABELS] =
4736 {
4737 [IOS_DEFAULT] = {{"capacity", 2}, {"operations", 2}, {"bandwidth", 2},
4738 {NULL}},
4739 [IOS_LATENCY] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
4740 {"asyncq_wait", 2}, {"scrub", 1}, {"trim", 1}, {"rebuild", 1},
4741 {NULL}},
4742 [IOS_QUEUES] = {{"syncq_read", 2}, {"syncq_write", 2},
4743 {"asyncq_read", 2}, {"asyncq_write", 2}, {"scrubq_read", 2},
4744 {"trimq_write", 2}, {"rebuildq_write", 2}, {NULL}},
4745 [IOS_L_HISTO] = {{"total_wait", 2}, {"disk_wait", 2}, {"syncq_wait", 2},
4746 {"asyncq_wait", 2}, {NULL}},
4747 [IOS_RQ_HISTO] = {{"sync_read", 2}, {"sync_write", 2},
4748 {"async_read", 2}, {"async_write", 2}, {"scrub", 2},
4749 {"trim", 2}, {"rebuild", 2}, {NULL}},
4750 };
4751
4752 /* Shorthand - if "columns" field not set, default to 1 column */
4753 static const name_and_columns_t iostat_bottom_labels[][IOSTAT_MAX_LABELS] =
4754 {
4755 [IOS_DEFAULT] = {{"alloc"}, {"free"}, {"read"}, {"write"}, {"read"},
4756 {"write"}, {NULL}},
4757 [IOS_LATENCY] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
4758 {"write"}, {"read"}, {"write"}, {"wait"}, {"wait"}, {"wait"},
4759 {NULL}},
4760 [IOS_QUEUES] = {{"pend"}, {"activ"}, {"pend"}, {"activ"}, {"pend"},
4761 {"activ"}, {"pend"}, {"activ"}, {"pend"}, {"activ"},
4762 {"pend"}, {"activ"}, {"pend"}, {"activ"}, {NULL}},
4763 [IOS_L_HISTO] = {{"read"}, {"write"}, {"read"}, {"write"}, {"read"},
4764 {"write"}, {"read"}, {"write"}, {"scrub"}, {"trim"}, {"rebuild"},
4765 {NULL}},
4766 [IOS_RQ_HISTO] = {{"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
4767 {"ind"}, {"agg"}, {"ind"}, {"agg"}, {"ind"}, {"agg"},
4768 {"ind"}, {"agg"}, {NULL}},
4769 };
4770
4771 static const char *histo_to_title[] = {
4772 [IOS_L_HISTO] = "latency",
4773 [IOS_RQ_HISTO] = "req_size",
4774 };
4775
4776 /*
4777 * Return the number of labels in a null-terminated name_and_columns_t
4778 * array.
4779 *
4780 */
4781 static unsigned int
label_array_len(const name_and_columns_t * labels)4782 label_array_len(const name_and_columns_t *labels)
4783 {
4784 int i = 0;
4785
4786 while (labels[i].name)
4787 i++;
4788
4789 return (i);
4790 }
4791
4792 /*
4793 * Return the number of strings in a null-terminated string array.
4794 * For example:
4795 *
4796 * const char foo[] = {"bar", "baz", NULL}
4797 *
4798 * returns 2
4799 */
4800 static uint64_t
str_array_len(const char * array[])4801 str_array_len(const char *array[])
4802 {
4803 uint64_t i = 0;
4804 while (array[i])
4805 i++;
4806
4807 return (i);
4808 }
4809
4810
4811 /*
4812 * Return a default column width for default/latency/queue columns. This does
4813 * not include histograms, which have their columns autosized.
4814 */
4815 static unsigned int
default_column_width(iostat_cbdata_t * cb,enum iostat_type type)4816 default_column_width(iostat_cbdata_t *cb, enum iostat_type type)
4817 {
4818 unsigned long column_width = 5; /* Normal niceprint */
4819 static unsigned long widths[] = {
4820 /*
4821 * Choose some sane default column sizes for printing the
4822 * raw numbers.
4823 */
4824 [IOS_DEFAULT] = 15, /* 1PB capacity */
4825 [IOS_LATENCY] = 10, /* 1B ns = 10sec */
4826 [IOS_QUEUES] = 6, /* 1M queue entries */
4827 [IOS_L_HISTO] = 10, /* 1B ns = 10sec */
4828 [IOS_RQ_HISTO] = 6, /* 1M queue entries */
4829 };
4830
4831 if (cb->cb_literal)
4832 column_width = widths[type];
4833
4834 return (column_width);
4835 }
4836
4837 /*
4838 * Print the column labels, i.e:
4839 *
4840 * capacity operations bandwidth
4841 * alloc free read write read write ...
4842 *
4843 * If force_column_width is set, use it for the column width. If not set, use
4844 * the default column width.
4845 */
4846 static void
print_iostat_labels(iostat_cbdata_t * cb,unsigned int force_column_width,const name_and_columns_t labels[][IOSTAT_MAX_LABELS])4847 print_iostat_labels(iostat_cbdata_t *cb, unsigned int force_column_width,
4848 const name_and_columns_t labels[][IOSTAT_MAX_LABELS])
4849 {
4850 int i, idx, s;
4851 int text_start, rw_column_width, spaces_to_end;
4852 uint64_t flags = cb->cb_flags;
4853 uint64_t f;
4854 unsigned int column_width = force_column_width;
4855
4856 /* For each bit set in flags */
4857 for (f = flags; f; f &= ~(1ULL << idx)) {
4858 idx = lowbit64(f) - 1;
4859 if (!force_column_width)
4860 column_width = default_column_width(cb, idx);
4861 /* Print our top labels centered over "read write" label. */
4862 for (i = 0; i < label_array_len(labels[idx]); i++) {
4863 const char *name = labels[idx][i].name;
4864 /*
4865 * We treat labels[][].columns == 0 as shorthand
4866 * for one column. It makes writing out the label
4867 * tables more concise.
4868 */
4869 unsigned int columns = MAX(1, labels[idx][i].columns);
4870 unsigned int slen = strlen(name);
4871
4872 rw_column_width = (column_width * columns) +
4873 (2 * (columns - 1));
4874
4875 text_start = (int)((rw_column_width) / columns -
4876 slen / columns);
4877 if (text_start < 0)
4878 text_start = 0;
4879
4880 printf(" "); /* Two spaces between columns */
4881
4882 /* Space from beginning of column to label */
4883 for (s = 0; s < text_start; s++)
4884 printf(" ");
4885
4886 printf("%s", name);
4887
4888 /* Print space after label to end of column */
4889 spaces_to_end = rw_column_width - text_start - slen;
4890 if (spaces_to_end < 0)
4891 spaces_to_end = 0;
4892
4893 for (s = 0; s < spaces_to_end; s++)
4894 printf(" ");
4895 }
4896 }
4897 }
4898
4899
4900 /*
4901 * print_cmd_columns - Print custom column titles from -c
4902 *
4903 * If the user specified the "zpool status|iostat -c" then print their custom
4904 * column titles in the header. For example, print_cmd_columns() would print
4905 * the " col1 col2" part of this:
4906 *
4907 * $ zpool iostat -vc 'echo col1=val1; echo col2=val2'
4908 * ...
4909 * capacity operations bandwidth
4910 * pool alloc free read write read write col1 col2
4911 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4912 * mypool 269K 1008M 0 0 107 946
4913 * mirror 269K 1008M 0 0 107 946
4914 * sdb - - 0 0 102 473 val1 val2
4915 * sdc - - 0 0 5 473 val1 val2
4916 * ---------- ----- ----- ----- ----- ----- ----- ---- ----
4917 */
4918 static void
print_cmd_columns(vdev_cmd_data_list_t * vcdl,int use_dashes)4919 print_cmd_columns(vdev_cmd_data_list_t *vcdl, int use_dashes)
4920 {
4921 int i, j;
4922 vdev_cmd_data_t *data = &vcdl->data[0];
4923
4924 if (vcdl->count == 0 || data == NULL)
4925 return;
4926
4927 /*
4928 * Each vdev cmd should have the same column names unless the user did
4929 * something weird with their cmd. Just take the column names from the
4930 * first vdev and assume it works for all of them.
4931 */
4932 for (i = 0; i < vcdl->uniq_cols_cnt; i++) {
4933 printf(" ");
4934 if (use_dashes) {
4935 for (j = 0; j < vcdl->uniq_cols_width[i]; j++)
4936 printf("-");
4937 } else {
4938 (void) printf_color(ANSI_BOLD, "%*s",
4939 vcdl->uniq_cols_width[i],
4940 vcdl->uniq_cols[i]);
4941 }
4942 }
4943 }
4944
4945
4946 /*
4947 * Utility function to print out a line of dashes like:
4948 *
4949 * -------------------------------- ----- ----- ----- ----- -----
4950 *
4951 * ...or a dashed named-row line like:
4952 *
4953 * logs - - - - -
4954 *
4955 * @cb: iostat data
4956 *
4957 * @force_column_width If non-zero, use the value as the column width.
4958 * Otherwise use the default column widths.
4959 *
4960 * @name: Print a dashed named-row line starting
4961 * with @name. Otherwise, print a regular
4962 * dashed line.
4963 */
4964 static void
print_iostat_dashes(iostat_cbdata_t * cb,unsigned int force_column_width,const char * name)4965 print_iostat_dashes(iostat_cbdata_t *cb, unsigned int force_column_width,
4966 const char *name)
4967 {
4968 int i;
4969 unsigned int namewidth;
4970 uint64_t flags = cb->cb_flags;
4971 uint64_t f;
4972 int idx;
4973 const name_and_columns_t *labels;
4974 const char *title;
4975
4976
4977 if (cb->cb_flags & IOS_ANYHISTO_M) {
4978 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
4979 } else if (cb->cb_vdevs.cb_names_count) {
4980 title = "vdev";
4981 } else {
4982 title = "pool";
4983 }
4984
4985 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
4986 name ? strlen(name) : 0);
4987
4988
4989 if (name) {
4990 printf("%-*s", namewidth, name);
4991 } else {
4992 for (i = 0; i < namewidth; i++)
4993 (void) printf("-");
4994 }
4995
4996 /* For each bit in flags */
4997 for (f = flags; f; f &= ~(1ULL << idx)) {
4998 unsigned int column_width;
4999 idx = lowbit64(f) - 1;
5000 if (force_column_width)
5001 column_width = force_column_width;
5002 else
5003 column_width = default_column_width(cb, idx);
5004
5005 labels = iostat_bottom_labels[idx];
5006 for (i = 0; i < label_array_len(labels); i++) {
5007 if (name)
5008 printf(" %*s-", column_width - 1, " ");
5009 else
5010 printf(" %.*s", column_width,
5011 "--------------------");
5012 }
5013 }
5014 }
5015
5016
5017 static void
print_iostat_separator_impl(iostat_cbdata_t * cb,unsigned int force_column_width)5018 print_iostat_separator_impl(iostat_cbdata_t *cb,
5019 unsigned int force_column_width)
5020 {
5021 print_iostat_dashes(cb, force_column_width, NULL);
5022 }
5023
5024 static void
print_iostat_separator(iostat_cbdata_t * cb)5025 print_iostat_separator(iostat_cbdata_t *cb)
5026 {
5027 print_iostat_separator_impl(cb, 0);
5028 }
5029
5030 static void
print_iostat_header_impl(iostat_cbdata_t * cb,unsigned int force_column_width,const char * histo_vdev_name)5031 print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
5032 const char *histo_vdev_name)
5033 {
5034 unsigned int namewidth;
5035 const char *title;
5036
5037 color_start(ANSI_BOLD);
5038
5039 if (cb->cb_flags & IOS_ANYHISTO_M) {
5040 title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
5041 } else if (cb->cb_vdevs.cb_names_count) {
5042 title = "vdev";
5043 } else {
5044 title = "pool";
5045 }
5046
5047 namewidth = MAX(MAX(strlen(title), cb->cb_namewidth),
5048 histo_vdev_name ? strlen(histo_vdev_name) : 0);
5049
5050 if (histo_vdev_name)
5051 printf("%-*s", namewidth, histo_vdev_name);
5052 else
5053 printf("%*s", namewidth, "");
5054
5055
5056 print_iostat_labels(cb, force_column_width, iostat_top_labels);
5057 printf("\n");
5058
5059 printf("%-*s", namewidth, title);
5060
5061 print_iostat_labels(cb, force_column_width, iostat_bottom_labels);
5062 if (cb->vcdl != NULL)
5063 print_cmd_columns(cb->vcdl, 0);
5064
5065 printf("\n");
5066
5067 print_iostat_separator_impl(cb, force_column_width);
5068
5069 if (cb->vcdl != NULL)
5070 print_cmd_columns(cb->vcdl, 1);
5071
5072 color_end();
5073
5074 printf("\n");
5075 }
5076
5077 static void
print_iostat_header(iostat_cbdata_t * cb)5078 print_iostat_header(iostat_cbdata_t *cb)
5079 {
5080 print_iostat_header_impl(cb, 0, NULL);
5081 }
5082
5083 /*
5084 * Prints a size string (i.e. 120M) with the suffix ("M") colored
5085 * by order of magnitude. Uses column_size to add padding.
5086 */
5087 static void
print_stat_color(const char * statbuf,unsigned int column_size)5088 print_stat_color(const char *statbuf, unsigned int column_size)
5089 {
5090 (void) fputs(" ", stdout);
5091 size_t len = strlen(statbuf);
5092 while (len < column_size) {
5093 (void) fputc(' ', stdout);
5094 column_size--;
5095 }
5096 if (*statbuf == '0') {
5097 color_start(ANSI_GRAY);
5098 (void) fputc('0', stdout);
5099 } else {
5100 for (; *statbuf; statbuf++) {
5101 if (*statbuf == 'K') color_start(ANSI_GREEN);
5102 else if (*statbuf == 'M') color_start(ANSI_YELLOW);
5103 else if (*statbuf == 'G') color_start(ANSI_RED);
5104 else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE);
5105 else if (*statbuf == 'P') color_start(ANSI_MAGENTA);
5106 else if (*statbuf == 'E') color_start(ANSI_CYAN);
5107 (void) fputc(*statbuf, stdout);
5108 if (--column_size <= 0)
5109 break;
5110 }
5111 }
5112 color_end();
5113 }
5114
5115 /*
5116 * Display a single statistic.
5117 */
5118 static void
print_one_stat(uint64_t value,enum zfs_nicenum_format format,unsigned int column_size,boolean_t scripted)5119 print_one_stat(uint64_t value, enum zfs_nicenum_format format,
5120 unsigned int column_size, boolean_t scripted)
5121 {
5122 char buf[64];
5123
5124 zfs_nicenum_format(value, buf, sizeof (buf), format);
5125
5126 if (scripted)
5127 printf("\t%s", buf);
5128 else
5129 print_stat_color(buf, column_size);
5130 }
5131
5132 /*
5133 * Calculate the default vdev stats
5134 *
5135 * Subtract oldvs from newvs, apply a scaling factor, and save the resulting
5136 * stats into calcvs.
5137 */
5138 static void
calc_default_iostats(vdev_stat_t * oldvs,vdev_stat_t * newvs,vdev_stat_t * calcvs)5139 calc_default_iostats(vdev_stat_t *oldvs, vdev_stat_t *newvs,
5140 vdev_stat_t *calcvs)
5141 {
5142 int i;
5143
5144 memcpy(calcvs, newvs, sizeof (*calcvs));
5145 for (i = 0; i < ARRAY_SIZE(calcvs->vs_ops); i++)
5146 calcvs->vs_ops[i] = (newvs->vs_ops[i] - oldvs->vs_ops[i]);
5147
5148 for (i = 0; i < ARRAY_SIZE(calcvs->vs_bytes); i++)
5149 calcvs->vs_bytes[i] = (newvs->vs_bytes[i] - oldvs->vs_bytes[i]);
5150 }
5151
5152 /*
5153 * Internal representation of the extended iostats data.
5154 *
5155 * The extended iostat stats are exported in nvlists as either uint64_t arrays
5156 * or single uint64_t's. We make both look like arrays to make them easier
5157 * to process. In order to make single uint64_t's look like arrays, we set
5158 * __data to the stat data, and then set *data = &__data with count = 1. Then,
5159 * we can just use *data and count.
5160 */
5161 struct stat_array {
5162 uint64_t *data;
5163 uint_t count; /* Number of entries in data[] */
5164 uint64_t __data; /* Only used when data is a single uint64_t */
5165 };
5166
5167 static uint64_t
stat_histo_max(struct stat_array * nva,unsigned int len)5168 stat_histo_max(struct stat_array *nva, unsigned int len)
5169 {
5170 uint64_t max = 0;
5171 int i;
5172 for (i = 0; i < len; i++)
5173 max = MAX(max, array64_max(nva[i].data, nva[i].count));
5174
5175 return (max);
5176 }
5177
5178 /*
5179 * Helper function to lookup a uint64_t array or uint64_t value and store its
5180 * data as a stat_array. If the nvpair is a single uint64_t value, then we make
5181 * it look like a one element array to make it easier to process.
5182 */
5183 static int
nvpair64_to_stat_array(nvlist_t * nvl,const char * name,struct stat_array * nva)5184 nvpair64_to_stat_array(nvlist_t *nvl, const char *name,
5185 struct stat_array *nva)
5186 {
5187 nvpair_t *tmp;
5188 int ret;
5189
5190 verify(nvlist_lookup_nvpair(nvl, name, &tmp) == 0);
5191 switch (nvpair_type(tmp)) {
5192 case DATA_TYPE_UINT64_ARRAY:
5193 ret = nvpair_value_uint64_array(tmp, &nva->data, &nva->count);
5194 break;
5195 case DATA_TYPE_UINT64:
5196 ret = nvpair_value_uint64(tmp, &nva->__data);
5197 nva->data = &nva->__data;
5198 nva->count = 1;
5199 break;
5200 default:
5201 /* Not a uint64_t */
5202 ret = EINVAL;
5203 break;
5204 }
5205
5206 return (ret);
5207 }
5208
5209 /*
5210 * Given a list of nvlist names, look up the extended stats in newnv and oldnv,
5211 * subtract them, and return the results in a newly allocated stat_array.
5212 * You must free the returned array after you are done with it with
5213 * free_calc_stats().
5214 *
5215 * Additionally, you can set "oldnv" to NULL if you simply want the newnv
5216 * values.
5217 */
5218 static struct stat_array *
calc_and_alloc_stats_ex(const char ** names,unsigned int len,nvlist_t * oldnv,nvlist_t * newnv)5219 calc_and_alloc_stats_ex(const char **names, unsigned int len, nvlist_t *oldnv,
5220 nvlist_t *newnv)
5221 {
5222 nvlist_t *oldnvx = NULL, *newnvx;
5223 struct stat_array *oldnva, *newnva, *calcnva;
5224 int i, j;
5225 unsigned int alloc_size = (sizeof (struct stat_array)) * len;
5226
5227 /* Extract our extended stats nvlist from the main list */
5228 verify(nvlist_lookup_nvlist(newnv, ZPOOL_CONFIG_VDEV_STATS_EX,
5229 &newnvx) == 0);
5230 if (oldnv) {
5231 verify(nvlist_lookup_nvlist(oldnv, ZPOOL_CONFIG_VDEV_STATS_EX,
5232 &oldnvx) == 0);
5233 }
5234
5235 newnva = safe_malloc(alloc_size);
5236 oldnva = safe_malloc(alloc_size);
5237 calcnva = safe_malloc(alloc_size);
5238
5239 for (j = 0; j < len; j++) {
5240 verify(nvpair64_to_stat_array(newnvx, names[j],
5241 &newnva[j]) == 0);
5242 calcnva[j].count = newnva[j].count;
5243 alloc_size = calcnva[j].count * sizeof (calcnva[j].data[0]);
5244 calcnva[j].data = safe_malloc(alloc_size);
5245 memcpy(calcnva[j].data, newnva[j].data, alloc_size);
5246
5247 if (oldnvx) {
5248 verify(nvpair64_to_stat_array(oldnvx, names[j],
5249 &oldnva[j]) == 0);
5250 for (i = 0; i < oldnva[j].count; i++)
5251 calcnva[j].data[i] -= oldnva[j].data[i];
5252 }
5253 }
5254 free(newnva);
5255 free(oldnva);
5256 return (calcnva);
5257 }
5258
5259 static void
free_calc_stats(struct stat_array * nva,unsigned int len)5260 free_calc_stats(struct stat_array *nva, unsigned int len)
5261 {
5262 int i;
5263 for (i = 0; i < len; i++)
5264 free(nva[i].data);
5265
5266 free(nva);
5267 }
5268
5269 static void
print_iostat_histo(struct stat_array * nva,unsigned int len,iostat_cbdata_t * cb,unsigned int column_width,unsigned int namewidth,double scale)5270 print_iostat_histo(struct stat_array *nva, unsigned int len,
5271 iostat_cbdata_t *cb, unsigned int column_width, unsigned int namewidth,
5272 double scale)
5273 {
5274 int i, j;
5275 char buf[6];
5276 uint64_t val;
5277 enum zfs_nicenum_format format;
5278 unsigned int buckets;
5279 unsigned int start_bucket;
5280
5281 if (cb->cb_literal)
5282 format = ZFS_NICENUM_RAW;
5283 else
5284 format = ZFS_NICENUM_1024;
5285
5286 /* All these histos are the same size, so just use nva[0].count */
5287 buckets = nva[0].count;
5288
5289 if (cb->cb_flags & IOS_RQ_HISTO_M) {
5290 /* Start at 512 - req size should never be lower than this */
5291 start_bucket = 9;
5292 } else {
5293 start_bucket = 0;
5294 }
5295
5296 for (j = start_bucket; j < buckets; j++) {
5297 /* Print histogram bucket label */
5298 if (cb->cb_flags & IOS_L_HISTO_M) {
5299 /* Ending range of this bucket */
5300 val = (1UL << (j + 1)) - 1;
5301 zfs_nicetime(val, buf, sizeof (buf));
5302 } else {
5303 /* Request size (starting range of bucket) */
5304 val = (1UL << j);
5305 zfs_nicenum(val, buf, sizeof (buf));
5306 }
5307
5308 if (cb->cb_scripted)
5309 printf("%llu", (u_longlong_t)val);
5310 else
5311 printf("%-*s", namewidth, buf);
5312
5313 /* Print the values on the line */
5314 for (i = 0; i < len; i++) {
5315 print_one_stat(nva[i].data[j] * scale, format,
5316 column_width, cb->cb_scripted);
5317 }
5318 printf("\n");
5319 }
5320 }
5321
5322 static void
print_solid_separator(unsigned int length)5323 print_solid_separator(unsigned int length)
5324 {
5325 while (length--)
5326 printf("-");
5327 printf("\n");
5328 }
5329
5330 static void
print_iostat_histos(iostat_cbdata_t * cb,nvlist_t * oldnv,nvlist_t * newnv,double scale,const char * name)5331 print_iostat_histos(iostat_cbdata_t *cb, nvlist_t *oldnv,
5332 nvlist_t *newnv, double scale, const char *name)
5333 {
5334 unsigned int column_width;
5335 unsigned int namewidth;
5336 unsigned int entire_width;
5337 enum iostat_type type;
5338 struct stat_array *nva;
5339 const char **names;
5340 unsigned int names_len;
5341
5342 /* What type of histo are we? */
5343 type = IOS_HISTO_IDX(cb->cb_flags);
5344
5345 /* Get NULL-terminated array of nvlist names for our histo */
5346 names = vsx_type_to_nvlist[type];
5347 names_len = str_array_len(names); /* num of names */
5348
5349 nva = calc_and_alloc_stats_ex(names, names_len, oldnv, newnv);
5350
5351 if (cb->cb_literal) {
5352 column_width = MAX(5,
5353 (unsigned int) log10(stat_histo_max(nva, names_len)) + 1);
5354 } else {
5355 column_width = 5;
5356 }
5357
5358 namewidth = MAX(cb->cb_namewidth,
5359 strlen(histo_to_title[IOS_HISTO_IDX(cb->cb_flags)]));
5360
5361 /*
5362 * Calculate the entire line width of what we're printing. The
5363 * +2 is for the two spaces between columns:
5364 */
5365 /* read write */
5366 /* ----- ----- */
5367 /* |___| <---------- column_width */
5368 /* */
5369 /* |__________| <--- entire_width */
5370 /* */
5371 entire_width = namewidth + (column_width + 2) *
5372 label_array_len(iostat_bottom_labels[type]);
5373
5374 if (cb->cb_scripted)
5375 printf("%s\n", name);
5376 else
5377 print_iostat_header_impl(cb, column_width, name);
5378
5379 print_iostat_histo(nva, names_len, cb, column_width,
5380 namewidth, scale);
5381
5382 free_calc_stats(nva, names_len);
5383 if (!cb->cb_scripted)
5384 print_solid_separator(entire_width);
5385 }
5386
5387 /*
5388 * Calculate the average latency of a power-of-two latency histogram
5389 */
5390 static uint64_t
single_histo_average(uint64_t * histo,unsigned int buckets)5391 single_histo_average(uint64_t *histo, unsigned int buckets)
5392 {
5393 int i;
5394 uint64_t count = 0, total = 0;
5395
5396 for (i = 0; i < buckets; i++) {
5397 /*
5398 * Our buckets are power-of-two latency ranges. Use the
5399 * midpoint latency of each bucket to calculate the average.
5400 * For example:
5401 *
5402 * Bucket Midpoint
5403 * 8ns-15ns: 12ns
5404 * 16ns-31ns: 24ns
5405 * ...
5406 */
5407 if (histo[i] != 0) {
5408 total += histo[i] * (((1UL << i) + ((1UL << i)/2)));
5409 count += histo[i];
5410 }
5411 }
5412
5413 /* Prevent divide by zero */
5414 return (count == 0 ? 0 : total / count);
5415 }
5416
5417 static void
print_iostat_queues(iostat_cbdata_t * cb,nvlist_t * newnv)5418 print_iostat_queues(iostat_cbdata_t *cb, nvlist_t *newnv)
5419 {
5420 const char *names[] = {
5421 ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
5422 ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
5423 ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
5424 ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
5425 ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
5426 ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
5427 ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
5428 ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
5429 ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
5430 ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
5431 ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
5432 ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
5433 ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
5434 ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
5435 };
5436
5437 struct stat_array *nva;
5438
5439 unsigned int column_width = default_column_width(cb, IOS_QUEUES);
5440 enum zfs_nicenum_format format;
5441
5442 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), NULL, newnv);
5443
5444 if (cb->cb_literal)
5445 format = ZFS_NICENUM_RAW;
5446 else
5447 format = ZFS_NICENUM_1024;
5448
5449 for (int i = 0; i < ARRAY_SIZE(names); i++) {
5450 uint64_t val = nva[i].data[0];
5451 print_one_stat(val, format, column_width, cb->cb_scripted);
5452 }
5453
5454 free_calc_stats(nva, ARRAY_SIZE(names));
5455 }
5456
5457 static void
print_iostat_latency(iostat_cbdata_t * cb,nvlist_t * oldnv,nvlist_t * newnv)5458 print_iostat_latency(iostat_cbdata_t *cb, nvlist_t *oldnv,
5459 nvlist_t *newnv)
5460 {
5461 int i;
5462 uint64_t val;
5463 const char *names[] = {
5464 ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
5465 ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
5466 ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
5467 ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
5468 ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
5469 ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
5470 ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
5471 ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
5472 ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
5473 ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
5474 ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
5475 };
5476 struct stat_array *nva;
5477
5478 unsigned int column_width = default_column_width(cb, IOS_LATENCY);
5479 enum zfs_nicenum_format format;
5480
5481 nva = calc_and_alloc_stats_ex(names, ARRAY_SIZE(names), oldnv, newnv);
5482
5483 if (cb->cb_literal)
5484 format = ZFS_NICENUM_RAWTIME;
5485 else
5486 format = ZFS_NICENUM_TIME;
5487
5488 /* Print our avg latencies on the line */
5489 for (i = 0; i < ARRAY_SIZE(names); i++) {
5490 /* Compute average latency for a latency histo */
5491 val = single_histo_average(nva[i].data, nva[i].count);
5492 print_one_stat(val, format, column_width, cb->cb_scripted);
5493 }
5494 free_calc_stats(nva, ARRAY_SIZE(names));
5495 }
5496
5497 /*
5498 * Print default statistics (capacity/operations/bandwidth)
5499 */
5500 static void
print_iostat_default(vdev_stat_t * vs,iostat_cbdata_t * cb,double scale)5501 print_iostat_default(vdev_stat_t *vs, iostat_cbdata_t *cb, double scale)
5502 {
5503 unsigned int column_width = default_column_width(cb, IOS_DEFAULT);
5504 enum zfs_nicenum_format format;
5505 char na; /* char to print for "not applicable" values */
5506
5507 if (cb->cb_literal) {
5508 format = ZFS_NICENUM_RAW;
5509 na = '0';
5510 } else {
5511 format = ZFS_NICENUM_1024;
5512 na = '-';
5513 }
5514
5515 /* only toplevel vdevs have capacity stats */
5516 if (vs->vs_space == 0) {
5517 if (cb->cb_scripted)
5518 printf("\t%c\t%c", na, na);
5519 else
5520 printf(" %*c %*c", column_width, na, column_width,
5521 na);
5522 } else {
5523 print_one_stat(vs->vs_alloc, format, column_width,
5524 cb->cb_scripted);
5525 print_one_stat(vs->vs_space - vs->vs_alloc, format,
5526 column_width, cb->cb_scripted);
5527 }
5528
5529 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_READ] * scale),
5530 format, column_width, cb->cb_scripted);
5531 print_one_stat((uint64_t)(vs->vs_ops[ZIO_TYPE_WRITE] * scale),
5532 format, column_width, cb->cb_scripted);
5533 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_READ] * scale),
5534 format, column_width, cb->cb_scripted);
5535 print_one_stat((uint64_t)(vs->vs_bytes[ZIO_TYPE_WRITE] * scale),
5536 format, column_width, cb->cb_scripted);
5537 }
5538
5539 static const char *const class_name[] = {
5540 VDEV_ALLOC_BIAS_DEDUP,
5541 VDEV_ALLOC_BIAS_SPECIAL,
5542 VDEV_ALLOC_CLASS_LOGS
5543 };
5544
5545 /*
5546 * Print out all the statistics for the given vdev. This can either be the
5547 * toplevel configuration, or called recursively. If 'name' is NULL, then this
5548 * is a verbose output, and we don't want to display the toplevel pool stats.
5549 *
5550 * Returns the number of stat lines printed.
5551 */
5552 static unsigned int
print_vdev_stats(zpool_handle_t * zhp,const char * name,nvlist_t * oldnv,nvlist_t * newnv,iostat_cbdata_t * cb,int depth)5553 print_vdev_stats(zpool_handle_t *zhp, const char *name, nvlist_t *oldnv,
5554 nvlist_t *newnv, iostat_cbdata_t *cb, int depth)
5555 {
5556 nvlist_t **oldchild, **newchild;
5557 uint_t c, children, oldchildren;
5558 vdev_stat_t *oldvs, *newvs, *calcvs;
5559 vdev_stat_t zerovs = { 0 };
5560 char *vname;
5561 int i;
5562 int ret = 0;
5563 uint64_t tdelta;
5564 double scale;
5565
5566 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
5567 return (ret);
5568
5569 calcvs = safe_malloc(sizeof (*calcvs));
5570
5571 if (oldnv != NULL) {
5572 verify(nvlist_lookup_uint64_array(oldnv,
5573 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&oldvs, &c) == 0);
5574 } else {
5575 oldvs = &zerovs;
5576 }
5577
5578 /* Do we only want to see a specific vdev? */
5579 for (i = 0; i < cb->cb_vdevs.cb_names_count; i++) {
5580 /* Yes we do. Is this the vdev? */
5581 if (strcmp(name, cb->cb_vdevs.cb_names[i]) == 0) {
5582 /*
5583 * This is our vdev. Since it is the only vdev we
5584 * will be displaying, make depth = 0 so that it
5585 * doesn't get indented.
5586 */
5587 depth = 0;
5588 break;
5589 }
5590 }
5591
5592 if (cb->cb_vdevs.cb_names_count && (i == cb->cb_vdevs.cb_names_count)) {
5593 /* Couldn't match the name */
5594 goto children;
5595 }
5596
5597
5598 verify(nvlist_lookup_uint64_array(newnv, ZPOOL_CONFIG_VDEV_STATS,
5599 (uint64_t **)&newvs, &c) == 0);
5600
5601 /*
5602 * Print the vdev name unless it's is a histogram. Histograms
5603 * display the vdev name in the header itself.
5604 */
5605 if (!(cb->cb_flags & IOS_ANYHISTO_M)) {
5606 if (cb->cb_scripted) {
5607 printf("%s", name);
5608 } else {
5609 if (strlen(name) + depth > cb->cb_namewidth)
5610 (void) printf("%*s%s", depth, "", name);
5611 else
5612 (void) printf("%*s%s%*s", depth, "", name,
5613 (int)(cb->cb_namewidth - strlen(name) -
5614 depth), "");
5615 }
5616 }
5617
5618 /* Calculate our scaling factor */
5619 tdelta = newvs->vs_timestamp - oldvs->vs_timestamp;
5620 if ((oldvs->vs_timestamp == 0) && (cb->cb_flags & IOS_ANYHISTO_M)) {
5621 /*
5622 * If we specify printing histograms with no time interval, then
5623 * print the histogram numbers over the entire lifetime of the
5624 * vdev.
5625 */
5626 scale = 1;
5627 } else {
5628 if (tdelta == 0)
5629 scale = 1.0;
5630 else
5631 scale = (double)NANOSEC / tdelta;
5632 }
5633
5634 if (cb->cb_flags & IOS_DEFAULT_M) {
5635 calc_default_iostats(oldvs, newvs, calcvs);
5636 print_iostat_default(calcvs, cb, scale);
5637 }
5638 if (cb->cb_flags & IOS_LATENCY_M)
5639 print_iostat_latency(cb, oldnv, newnv);
5640 if (cb->cb_flags & IOS_QUEUES_M)
5641 print_iostat_queues(cb, newnv);
5642 if (cb->cb_flags & IOS_ANYHISTO_M) {
5643 printf("\n");
5644 print_iostat_histos(cb, oldnv, newnv, scale, name);
5645 }
5646
5647 if (cb->vcdl != NULL) {
5648 const char *path;
5649 if (nvlist_lookup_string(newnv, ZPOOL_CONFIG_PATH,
5650 &path) == 0) {
5651 printf(" ");
5652 zpool_print_cmd(cb->vcdl, zpool_get_name(zhp), path);
5653 }
5654 }
5655
5656 if (!(cb->cb_flags & IOS_ANYHISTO_M))
5657 printf("\n");
5658
5659 ret++;
5660
5661 children:
5662
5663 free(calcvs);
5664
5665 if (!cb->cb_verbose)
5666 return (ret);
5667
5668 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_CHILDREN,
5669 &newchild, &children) != 0)
5670 return (ret);
5671
5672 if (oldnv) {
5673 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_CHILDREN,
5674 &oldchild, &oldchildren) != 0)
5675 return (ret);
5676
5677 children = MIN(oldchildren, children);
5678 }
5679
5680 /*
5681 * print normal top-level devices
5682 */
5683 for (c = 0; c < children; c++) {
5684 uint64_t ishole = B_FALSE, islog = B_FALSE;
5685
5686 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_HOLE,
5687 &ishole);
5688
5689 (void) nvlist_lookup_uint64(newchild[c], ZPOOL_CONFIG_IS_LOG,
5690 &islog);
5691
5692 if (ishole || islog)
5693 continue;
5694
5695 if (nvlist_exists(newchild[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
5696 continue;
5697
5698 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
5699 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
5700 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c] : NULL,
5701 newchild[c], cb, depth + 2);
5702 free(vname);
5703 }
5704
5705 /*
5706 * print all other top-level devices
5707 */
5708 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
5709 boolean_t printed = B_FALSE;
5710
5711 for (c = 0; c < children; c++) {
5712 uint64_t islog = B_FALSE;
5713 const char *bias = NULL;
5714 const char *type = NULL;
5715
5716 (void) nvlist_lookup_uint64(newchild[c],
5717 ZPOOL_CONFIG_IS_LOG, &islog);
5718 if (islog) {
5719 bias = VDEV_ALLOC_CLASS_LOGS;
5720 } else {
5721 (void) nvlist_lookup_string(newchild[c],
5722 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
5723 (void) nvlist_lookup_string(newchild[c],
5724 ZPOOL_CONFIG_TYPE, &type);
5725 }
5726 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
5727 continue;
5728 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
5729 continue;
5730
5731 if (!printed) {
5732 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) &&
5733 !cb->cb_scripted &&
5734 !cb->cb_vdevs.cb_names) {
5735 print_iostat_dashes(cb, 0,
5736 class_name[n]);
5737 }
5738 printf("\n");
5739 printed = B_TRUE;
5740 }
5741
5742 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
5743 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID);
5744 ret += print_vdev_stats(zhp, vname, oldnv ?
5745 oldchild[c] : NULL, newchild[c], cb, depth + 2);
5746 free(vname);
5747 }
5748 }
5749
5750 /*
5751 * Include level 2 ARC devices in iostat output
5752 */
5753 if (nvlist_lookup_nvlist_array(newnv, ZPOOL_CONFIG_L2CACHE,
5754 &newchild, &children) != 0)
5755 return (ret);
5756
5757 if (oldnv) {
5758 if (nvlist_lookup_nvlist_array(oldnv, ZPOOL_CONFIG_L2CACHE,
5759 &oldchild, &oldchildren) != 0)
5760 return (ret);
5761
5762 children = MIN(oldchildren, children);
5763 }
5764
5765 if (children > 0) {
5766 if ((!(cb->cb_flags & IOS_ANYHISTO_M)) && !cb->cb_scripted &&
5767 !cb->cb_vdevs.cb_names) {
5768 print_iostat_dashes(cb, 0, "cache");
5769 }
5770 printf("\n");
5771
5772 for (c = 0; c < children; c++) {
5773 vname = zpool_vdev_name(g_zfs, zhp, newchild[c],
5774 cb->cb_vdevs.cb_name_flags);
5775 ret += print_vdev_stats(zhp, vname, oldnv ? oldchild[c]
5776 : NULL, newchild[c], cb, depth + 2);
5777 free(vname);
5778 }
5779 }
5780
5781 return (ret);
5782 }
5783
5784 /*
5785 * Callback to print out the iostats for the given pool.
5786 */
5787 static int
print_iostat(zpool_handle_t * zhp,void * data)5788 print_iostat(zpool_handle_t *zhp, void *data)
5789 {
5790 iostat_cbdata_t *cb = data;
5791 nvlist_t *oldconfig, *newconfig;
5792 nvlist_t *oldnvroot, *newnvroot;
5793 int ret;
5794
5795 newconfig = zpool_get_config(zhp, &oldconfig);
5796
5797 if (cb->cb_iteration == 1)
5798 oldconfig = NULL;
5799
5800 verify(nvlist_lookup_nvlist(newconfig, ZPOOL_CONFIG_VDEV_TREE,
5801 &newnvroot) == 0);
5802
5803 if (oldconfig == NULL)
5804 oldnvroot = NULL;
5805 else
5806 verify(nvlist_lookup_nvlist(oldconfig, ZPOOL_CONFIG_VDEV_TREE,
5807 &oldnvroot) == 0);
5808
5809 ret = print_vdev_stats(zhp, zpool_get_name(zhp), oldnvroot, newnvroot,
5810 cb, 0);
5811 if ((ret != 0) && !(cb->cb_flags & IOS_ANYHISTO_M) &&
5812 !cb->cb_scripted && cb->cb_verbose &&
5813 !cb->cb_vdevs.cb_names_count) {
5814 print_iostat_separator(cb);
5815 if (cb->vcdl != NULL) {
5816 print_cmd_columns(cb->vcdl, 1);
5817 }
5818 printf("\n");
5819 }
5820
5821 return (ret);
5822 }
5823
5824 static int
get_columns(void)5825 get_columns(void)
5826 {
5827 struct winsize ws;
5828 int columns = 80;
5829 int error;
5830
5831 if (isatty(STDOUT_FILENO)) {
5832 error = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
5833 if (error == 0)
5834 columns = ws.ws_col;
5835 } else {
5836 columns = 999;
5837 }
5838
5839 return (columns);
5840 }
5841
5842 /*
5843 * Return the required length of the pool/vdev name column. The minimum
5844 * allowed width and output formatting flags must be provided.
5845 */
5846 static int
get_namewidth(zpool_handle_t * zhp,int min_width,int flags,boolean_t verbose)5847 get_namewidth(zpool_handle_t *zhp, int min_width, int flags, boolean_t verbose)
5848 {
5849 nvlist_t *config, *nvroot;
5850 int width = min_width;
5851
5852 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
5853 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5854 &nvroot) == 0);
5855 size_t poolname_len = strlen(zpool_get_name(zhp));
5856 if (verbose == B_FALSE) {
5857 width = MAX(poolname_len, min_width);
5858 } else {
5859 width = MAX(poolname_len,
5860 max_width(zhp, nvroot, 0, min_width, flags));
5861 }
5862 }
5863
5864 return (width);
5865 }
5866
5867 /*
5868 * Parse the input string, get the 'interval' and 'count' value if there is one.
5869 */
5870 static void
get_interval_count(int * argcp,char ** argv,float * iv,unsigned long * cnt)5871 get_interval_count(int *argcp, char **argv, float *iv,
5872 unsigned long *cnt)
5873 {
5874 float interval = 0;
5875 unsigned long count = 0;
5876 int argc = *argcp;
5877
5878 /*
5879 * Determine if the last argument is an integer or a pool name
5880 */
5881 if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
5882 char *end;
5883
5884 errno = 0;
5885 interval = strtof(argv[argc - 1], &end);
5886
5887 if (*end == '\0' && errno == 0) {
5888 if (interval == 0) {
5889 (void) fprintf(stderr, gettext(
5890 "interval cannot be zero\n"));
5891 usage(B_FALSE);
5892 }
5893 /*
5894 * Ignore the last parameter
5895 */
5896 argc--;
5897 } else {
5898 /*
5899 * If this is not a valid number, just plow on. The
5900 * user will get a more informative error message later
5901 * on.
5902 */
5903 interval = 0;
5904 }
5905 }
5906
5907 /*
5908 * If the last argument is also an integer, then we have both a count
5909 * and an interval.
5910 */
5911 if (argc > 0 && zfs_isnumber(argv[argc - 1])) {
5912 char *end;
5913
5914 errno = 0;
5915 count = interval;
5916 interval = strtof(argv[argc - 1], &end);
5917
5918 if (*end == '\0' && errno == 0) {
5919 if (interval == 0) {
5920 (void) fprintf(stderr, gettext(
5921 "interval cannot be zero\n"));
5922 usage(B_FALSE);
5923 }
5924
5925 /*
5926 * Ignore the last parameter
5927 */
5928 argc--;
5929 } else {
5930 interval = 0;
5931 }
5932 }
5933
5934 *iv = interval;
5935 *cnt = count;
5936 *argcp = argc;
5937 }
5938
5939 static void
get_timestamp_arg(char c)5940 get_timestamp_arg(char c)
5941 {
5942 if (c == 'u')
5943 timestamp_fmt = UDATE;
5944 else if (c == 'd')
5945 timestamp_fmt = DDATE;
5946 else
5947 usage(B_FALSE);
5948 }
5949
5950 /*
5951 * Return stat flags that are supported by all pools by both the module and
5952 * zpool iostat. "*data" should be initialized to all 0xFFs before running.
5953 * It will get ANDed down until only the flags that are supported on all pools
5954 * remain.
5955 */
5956 static int
get_stat_flags_cb(zpool_handle_t * zhp,void * data)5957 get_stat_flags_cb(zpool_handle_t *zhp, void *data)
5958 {
5959 uint64_t *mask = data;
5960 nvlist_t *config, *nvroot, *nvx;
5961 uint64_t flags = 0;
5962 int i, j;
5963
5964 config = zpool_get_config(zhp, NULL);
5965 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
5966 &nvroot) == 0);
5967
5968 /* Default stats are always supported, but for completeness.. */
5969 if (nvlist_exists(nvroot, ZPOOL_CONFIG_VDEV_STATS))
5970 flags |= IOS_DEFAULT_M;
5971
5972 /* Get our extended stats nvlist from the main list */
5973 if (nvlist_lookup_nvlist(nvroot, ZPOOL_CONFIG_VDEV_STATS_EX,
5974 &nvx) != 0) {
5975 /*
5976 * No extended stats; they're probably running an older
5977 * module. No big deal, we support that too.
5978 */
5979 goto end;
5980 }
5981
5982 /* For each extended stat, make sure all its nvpairs are supported */
5983 for (j = 0; j < ARRAY_SIZE(vsx_type_to_nvlist); j++) {
5984 if (!vsx_type_to_nvlist[j][0])
5985 continue;
5986
5987 /* Start off by assuming the flag is supported, then check */
5988 flags |= (1ULL << j);
5989 for (i = 0; vsx_type_to_nvlist[j][i]; i++) {
5990 if (!nvlist_exists(nvx, vsx_type_to_nvlist[j][i])) {
5991 /* flag isn't supported */
5992 flags = flags & ~(1ULL << j);
5993 break;
5994 }
5995 }
5996 }
5997 end:
5998 *mask = *mask & flags;
5999 return (0);
6000 }
6001
6002 /*
6003 * Return a bitmask of stats that are supported on all pools by both the module
6004 * and zpool iostat.
6005 */
6006 static uint64_t
get_stat_flags(zpool_list_t * list)6007 get_stat_flags(zpool_list_t *list)
6008 {
6009 uint64_t mask = -1;
6010
6011 /*
6012 * get_stat_flags_cb() will lop off bits from "mask" until only the
6013 * flags that are supported on all pools remain.
6014 */
6015 (void) pool_list_iter(list, B_FALSE, get_stat_flags_cb, &mask);
6016 return (mask);
6017 }
6018
6019 /*
6020 * Return 1 if cb_data->cb_names[0] is this vdev's name, 0 otherwise.
6021 */
6022 static int
is_vdev_cb(void * zhp_data,nvlist_t * nv,void * cb_data)6023 is_vdev_cb(void *zhp_data, nvlist_t *nv, void *cb_data)
6024 {
6025 uint64_t guid;
6026 vdev_cbdata_t *cb = cb_data;
6027 zpool_handle_t *zhp = zhp_data;
6028
6029 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
6030 return (0);
6031
6032 return (guid == zpool_vdev_path_to_guid(zhp, cb->cb_names[0]));
6033 }
6034
6035 /*
6036 * Returns 1 if cb_data->cb_names[0] is a vdev name, 0 otherwise.
6037 */
6038 static int
is_vdev(zpool_handle_t * zhp,void * cb_data)6039 is_vdev(zpool_handle_t *zhp, void *cb_data)
6040 {
6041 return (for_each_vdev(zhp, is_vdev_cb, cb_data));
6042 }
6043
6044 /*
6045 * Check if vdevs are in a pool
6046 *
6047 * Return 1 if all argv[] strings are vdev names in pool "pool_name". Otherwise
6048 * return 0. If pool_name is NULL, then search all pools.
6049 */
6050 static int
are_vdevs_in_pool(int argc,char ** argv,char * pool_name,vdev_cbdata_t * cb)6051 are_vdevs_in_pool(int argc, char **argv, char *pool_name,
6052 vdev_cbdata_t *cb)
6053 {
6054 char **tmp_name;
6055 int ret = 0;
6056 int i;
6057 int pool_count = 0;
6058
6059 if ((argc == 0) || !*argv)
6060 return (0);
6061
6062 if (pool_name)
6063 pool_count = 1;
6064
6065 /* Temporarily hijack cb_names for a second... */
6066 tmp_name = cb->cb_names;
6067
6068 /* Go though our list of prospective vdev names */
6069 for (i = 0; i < argc; i++) {
6070 cb->cb_names = argv + i;
6071
6072 /* Is this name a vdev in our pools? */
6073 ret = for_each_pool(pool_count, &pool_name, B_TRUE, NULL,
6074 ZFS_TYPE_POOL, B_FALSE, is_vdev, cb);
6075 if (!ret) {
6076 /* No match */
6077 break;
6078 }
6079 }
6080
6081 cb->cb_names = tmp_name;
6082
6083 return (ret);
6084 }
6085
6086 static int
is_pool_cb(zpool_handle_t * zhp,void * data)6087 is_pool_cb(zpool_handle_t *zhp, void *data)
6088 {
6089 char *name = data;
6090 if (strcmp(name, zpool_get_name(zhp)) == 0)
6091 return (1);
6092
6093 return (0);
6094 }
6095
6096 /*
6097 * Do we have a pool named *name? If so, return 1, otherwise 0.
6098 */
6099 static int
is_pool(char * name)6100 is_pool(char *name)
6101 {
6102 return (for_each_pool(0, NULL, B_TRUE, NULL, ZFS_TYPE_POOL, B_FALSE,
6103 is_pool_cb, name));
6104 }
6105
6106 /* Are all our argv[] strings pool names? If so return 1, 0 otherwise. */
6107 static int
are_all_pools(int argc,char ** argv)6108 are_all_pools(int argc, char **argv)
6109 {
6110 if ((argc == 0) || !*argv)
6111 return (0);
6112
6113 while (--argc >= 0)
6114 if (!is_pool(argv[argc]))
6115 return (0);
6116
6117 return (1);
6118 }
6119
6120 /*
6121 * Helper function to print out vdev/pool names we can't resolve. Used for an
6122 * error message.
6123 */
6124 static void
error_list_unresolved_vdevs(int argc,char ** argv,char * pool_name,vdev_cbdata_t * cb)6125 error_list_unresolved_vdevs(int argc, char **argv, char *pool_name,
6126 vdev_cbdata_t *cb)
6127 {
6128 int i;
6129 char *name;
6130 char *str;
6131 for (i = 0; i < argc; i++) {
6132 name = argv[i];
6133
6134 if (is_pool(name))
6135 str = gettext("pool");
6136 else if (are_vdevs_in_pool(1, &name, pool_name, cb))
6137 str = gettext("vdev in this pool");
6138 else if (are_vdevs_in_pool(1, &name, NULL, cb))
6139 str = gettext("vdev in another pool");
6140 else
6141 str = gettext("unknown");
6142
6143 fprintf(stderr, "\t%s (%s)\n", name, str);
6144 }
6145 }
6146
6147 /*
6148 * Same as get_interval_count(), but with additional checks to not misinterpret
6149 * guids as interval/count values. Assumes VDEV_NAME_GUID is set in
6150 * cb.cb_vdevs.cb_name_flags.
6151 */
6152 static void
get_interval_count_filter_guids(int * argc,char ** argv,float * interval,unsigned long * count,iostat_cbdata_t * cb)6153 get_interval_count_filter_guids(int *argc, char **argv, float *interval,
6154 unsigned long *count, iostat_cbdata_t *cb)
6155 {
6156 int argc_for_interval = 0;
6157
6158 /* Is the last arg an interval value? Or a guid? */
6159 if (*argc >= 1 && !are_vdevs_in_pool(1, &argv[*argc - 1], NULL,
6160 &cb->cb_vdevs)) {
6161 /*
6162 * The last arg is not a guid, so it's probably an
6163 * interval value.
6164 */
6165 argc_for_interval++;
6166
6167 if (*argc >= 2 &&
6168 !are_vdevs_in_pool(1, &argv[*argc - 2], NULL,
6169 &cb->cb_vdevs)) {
6170 /*
6171 * The 2nd to last arg is not a guid, so it's probably
6172 * an interval value.
6173 */
6174 argc_for_interval++;
6175 }
6176 }
6177
6178 /* Point to our list of possible intervals */
6179 char **tmpargv = &argv[*argc - argc_for_interval];
6180
6181 *argc = *argc - argc_for_interval;
6182 get_interval_count(&argc_for_interval, tmpargv,
6183 interval, count);
6184 }
6185
6186 /*
6187 * Terminal height, in rows. Returns -1 if stdout is not connected to a TTY or
6188 * if we were unable to determine its size.
6189 */
6190 static int
terminal_height(void)6191 terminal_height(void)
6192 {
6193 struct winsize win;
6194
6195 if (isatty(STDOUT_FILENO) == 0)
6196 return (-1);
6197
6198 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_row > 0)
6199 return (win.ws_row);
6200
6201 return (-1);
6202 }
6203
6204 /*
6205 * Run one of the zpool status/iostat -c scripts with the help (-h) option and
6206 * print the result.
6207 *
6208 * name: Short name of the script ('iostat').
6209 * path: Full path to the script ('/usr/local/etc/zfs/zpool.d/iostat');
6210 */
6211 static void
print_zpool_script_help(char * name,char * path)6212 print_zpool_script_help(char *name, char *path)
6213 {
6214 char *argv[] = {path, (char *)"-h", NULL};
6215 char **lines = NULL;
6216 int lines_cnt = 0;
6217 int rc;
6218
6219 rc = libzfs_run_process_get_stdout_nopath(path, argv, NULL, &lines,
6220 &lines_cnt);
6221 if (rc != 0 || lines == NULL || lines_cnt <= 0) {
6222 if (lines != NULL)
6223 libzfs_free_str_array(lines, lines_cnt);
6224 return;
6225 }
6226
6227 for (int i = 0; i < lines_cnt; i++)
6228 if (!is_blank_str(lines[i]))
6229 printf(" %-14s %s\n", name, lines[i]);
6230
6231 libzfs_free_str_array(lines, lines_cnt);
6232 }
6233
6234 /*
6235 * Go though the zpool status/iostat -c scripts in the user's path, run their
6236 * help option (-h), and print out the results.
6237 */
6238 static void
print_zpool_dir_scripts(char * dirpath)6239 print_zpool_dir_scripts(char *dirpath)
6240 {
6241 DIR *dir;
6242 struct dirent *ent;
6243 char fullpath[MAXPATHLEN];
6244 struct stat dir_stat;
6245
6246 if ((dir = opendir(dirpath)) != NULL) {
6247 /* print all the files and directories within directory */
6248 while ((ent = readdir(dir)) != NULL) {
6249 if (snprintf(fullpath, sizeof (fullpath), "%s/%s",
6250 dirpath, ent->d_name) >= sizeof (fullpath)) {
6251 (void) fprintf(stderr,
6252 gettext("internal error: "
6253 "ZPOOL_SCRIPTS_PATH too large.\n"));
6254 exit(1);
6255 }
6256
6257 /* Print the scripts */
6258 if (stat(fullpath, &dir_stat) == 0)
6259 if (dir_stat.st_mode & S_IXUSR &&
6260 S_ISREG(dir_stat.st_mode))
6261 print_zpool_script_help(ent->d_name,
6262 fullpath);
6263 }
6264 (void) closedir(dir);
6265 }
6266 }
6267
6268 /*
6269 * Print out help text for all zpool status/iostat -c scripts.
6270 */
6271 static void
print_zpool_script_list(const char * subcommand)6272 print_zpool_script_list(const char *subcommand)
6273 {
6274 char *dir, *sp, *tmp;
6275
6276 printf(gettext("Available 'zpool %s -c' commands:\n"), subcommand);
6277
6278 sp = zpool_get_cmd_search_path();
6279 if (sp == NULL)
6280 return;
6281
6282 for (dir = strtok_r(sp, ":", &tmp);
6283 dir != NULL;
6284 dir = strtok_r(NULL, ":", &tmp))
6285 print_zpool_dir_scripts(dir);
6286
6287 free(sp);
6288 }
6289
6290 /*
6291 * Set the minimum pool/vdev name column width. The width must be at least 10,
6292 * but may be as large as the column width - 42 so it still fits on one line.
6293 * NOTE: 42 is the width of the default capacity/operations/bandwidth output
6294 */
6295 static int
get_namewidth_iostat(zpool_handle_t * zhp,void * data)6296 get_namewidth_iostat(zpool_handle_t *zhp, void *data)
6297 {
6298 iostat_cbdata_t *cb = data;
6299 int width, available_width;
6300
6301 /*
6302 * get_namewidth() returns the maximum width of any name in that column
6303 * for any pool/vdev/device line that will be output.
6304 */
6305 width = get_namewidth(zhp, cb->cb_namewidth,
6306 cb->cb_vdevs.cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
6307
6308 /*
6309 * The width we are calculating is the width of the header and also the
6310 * padding width for names that are less than maximum width. The stats
6311 * take up 42 characters, so the width available for names is:
6312 */
6313 available_width = get_columns() - 42;
6314
6315 /*
6316 * If the maximum width fits on a screen, then great! Make everything
6317 * line up by justifying all lines to the same width. If that max
6318 * width is larger than what's available, the name plus stats won't fit
6319 * on one line, and justifying to that width would cause every line to
6320 * wrap on the screen. We only want lines with long names to wrap.
6321 * Limit the padding to what won't wrap.
6322 */
6323 if (width > available_width)
6324 width = available_width;
6325
6326 /*
6327 * And regardless of whatever the screen width is (get_columns can
6328 * return 0 if the width is not known or less than 42 for a narrow
6329 * terminal) have the width be a minimum of 10.
6330 */
6331 if (width < 10)
6332 width = 10;
6333
6334 /* Save the calculated width */
6335 cb->cb_namewidth = width;
6336
6337 return (0);
6338 }
6339
6340 /*
6341 * zpool iostat [[-c [script1,script2,...]] [-lq]|[-rw]] [-ghHLpPvy] [-n name]
6342 * [-T d|u] [[ pool ...]|[pool vdev ...]|[vdev ...]]
6343 * [interval [count]]
6344 *
6345 * -c CMD For each vdev, run command CMD
6346 * -g Display guid for individual vdev name.
6347 * -L Follow links when resolving vdev path name.
6348 * -P Display full path for vdev name.
6349 * -v Display statistics for individual vdevs
6350 * -h Display help
6351 * -p Display values in parsable (exact) format.
6352 * -H Scripted mode. Don't display headers, and separate properties
6353 * by a single tab.
6354 * -l Display average latency
6355 * -q Display queue depths
6356 * -w Display latency histograms
6357 * -r Display request size histogram
6358 * -T Display a timestamp in date(1) or Unix format
6359 * -n Only print headers once
6360 *
6361 * This command can be tricky because we want to be able to deal with pool
6362 * creation/destruction as well as vdev configuration changes. The bulk of this
6363 * processing is handled by the pool_list_* routines in zpool_iter.c. We rely
6364 * on pool_list_refresh() to detect the addition and removal of pools.
6365 * Configuration changes are all handled within libzfs.
6366 */
6367 int
zpool_do_iostat(int argc,char ** argv)6368 zpool_do_iostat(int argc, char **argv)
6369 {
6370 int c;
6371 int ret;
6372 float interval = 0;
6373 unsigned long count = 0;
6374 zpool_list_t *list;
6375 boolean_t verbose = B_FALSE;
6376 boolean_t latency = B_FALSE, l_histo = B_FALSE, rq_histo = B_FALSE;
6377 boolean_t queues = B_FALSE, parsable = B_FALSE, scripted = B_FALSE;
6378 boolean_t omit_since_boot = B_FALSE;
6379 boolean_t guid = B_FALSE;
6380 boolean_t follow_links = B_FALSE;
6381 boolean_t full_name = B_FALSE;
6382 boolean_t headers_once = B_FALSE;
6383 iostat_cbdata_t cb = { 0 };
6384 char *cmd = NULL;
6385
6386 /* Used for printing error message */
6387 const char flag_to_arg[] = {[IOS_LATENCY] = 'l', [IOS_QUEUES] = 'q',
6388 [IOS_L_HISTO] = 'w', [IOS_RQ_HISTO] = 'r'};
6389
6390 uint64_t unsupported_flags;
6391
6392 /* check options */
6393 while ((c = getopt(argc, argv, "c:gLPT:vyhplqrwnH")) != -1) {
6394 switch (c) {
6395 case 'c':
6396 if (cmd != NULL) {
6397 fprintf(stderr,
6398 gettext("Can't set -c flag twice\n"));
6399 exit(1);
6400 }
6401
6402 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
6403 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
6404 fprintf(stderr, gettext(
6405 "Can't run -c, disabled by "
6406 "ZPOOL_SCRIPTS_ENABLED.\n"));
6407 exit(1);
6408 }
6409
6410 if ((getuid() <= 0 || geteuid() <= 0) &&
6411 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
6412 fprintf(stderr, gettext(
6413 "Can't run -c with root privileges "
6414 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
6415 exit(1);
6416 }
6417 cmd = optarg;
6418 verbose = B_TRUE;
6419 break;
6420 case 'g':
6421 guid = B_TRUE;
6422 break;
6423 case 'L':
6424 follow_links = B_TRUE;
6425 break;
6426 case 'P':
6427 full_name = B_TRUE;
6428 break;
6429 case 'T':
6430 get_timestamp_arg(*optarg);
6431 break;
6432 case 'v':
6433 verbose = B_TRUE;
6434 break;
6435 case 'p':
6436 parsable = B_TRUE;
6437 break;
6438 case 'l':
6439 latency = B_TRUE;
6440 break;
6441 case 'q':
6442 queues = B_TRUE;
6443 break;
6444 case 'H':
6445 scripted = B_TRUE;
6446 break;
6447 case 'w':
6448 l_histo = B_TRUE;
6449 break;
6450 case 'r':
6451 rq_histo = B_TRUE;
6452 break;
6453 case 'y':
6454 omit_since_boot = B_TRUE;
6455 break;
6456 case 'n':
6457 headers_once = B_TRUE;
6458 break;
6459 case 'h':
6460 usage(B_FALSE);
6461 break;
6462 case '?':
6463 if (optopt == 'c') {
6464 print_zpool_script_list("iostat");
6465 exit(0);
6466 } else {
6467 fprintf(stderr,
6468 gettext("invalid option '%c'\n"), optopt);
6469 }
6470 usage(B_FALSE);
6471 }
6472 }
6473
6474 argc -= optind;
6475 argv += optind;
6476
6477 cb.cb_literal = parsable;
6478 cb.cb_scripted = scripted;
6479
6480 if (guid)
6481 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_GUID;
6482 if (follow_links)
6483 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
6484 if (full_name)
6485 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_PATH;
6486 cb.cb_iteration = 0;
6487 cb.cb_namewidth = 0;
6488 cb.cb_verbose = verbose;
6489
6490 /* Get our interval and count values (if any) */
6491 if (guid) {
6492 get_interval_count_filter_guids(&argc, argv, &interval,
6493 &count, &cb);
6494 } else {
6495 get_interval_count(&argc, argv, &interval, &count);
6496 }
6497
6498 if (argc == 0) {
6499 /* No args, so just print the defaults. */
6500 } else if (are_all_pools(argc, argv)) {
6501 /* All the args are pool names */
6502 } else if (are_vdevs_in_pool(argc, argv, NULL, &cb.cb_vdevs)) {
6503 /* All the args are vdevs */
6504 cb.cb_vdevs.cb_names = argv;
6505 cb.cb_vdevs.cb_names_count = argc;
6506 argc = 0; /* No pools to process */
6507 } else if (are_all_pools(1, argv)) {
6508 /* The first arg is a pool name */
6509 if (are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
6510 &cb.cb_vdevs)) {
6511 /* ...and the rest are vdev names */
6512 cb.cb_vdevs.cb_names = argv + 1;
6513 cb.cb_vdevs.cb_names_count = argc - 1;
6514 argc = 1; /* One pool to process */
6515 } else {
6516 fprintf(stderr, gettext("Expected either a list of "));
6517 fprintf(stderr, gettext("pools, or list of vdevs in"));
6518 fprintf(stderr, " \"%s\", ", argv[0]);
6519 fprintf(stderr, gettext("but got:\n"));
6520 error_list_unresolved_vdevs(argc - 1, argv + 1,
6521 argv[0], &cb.cb_vdevs);
6522 fprintf(stderr, "\n");
6523 usage(B_FALSE);
6524 }
6525 } else {
6526 /*
6527 * The args don't make sense. The first arg isn't a pool name,
6528 * nor are all the args vdevs.
6529 */
6530 fprintf(stderr, gettext("Unable to parse pools/vdevs list.\n"));
6531 fprintf(stderr, "\n");
6532 return (1);
6533 }
6534
6535 if (cb.cb_vdevs.cb_names_count != 0) {
6536 /*
6537 * If user specified vdevs, it implies verbose.
6538 */
6539 cb.cb_verbose = B_TRUE;
6540 }
6541
6542 /*
6543 * Construct the list of all interesting pools.
6544 */
6545 ret = 0;
6546 if ((list = pool_list_get(argc, argv, NULL, ZFS_TYPE_POOL, parsable,
6547 &ret)) == NULL)
6548 return (1);
6549
6550 if (pool_list_count(list) == 0 && argc != 0) {
6551 pool_list_free(list);
6552 return (1);
6553 }
6554
6555 if (pool_list_count(list) == 0 && interval == 0) {
6556 pool_list_free(list);
6557 (void) fprintf(stderr, gettext("no pools available\n"));
6558 return (1);
6559 }
6560
6561 if ((l_histo || rq_histo) && (cmd != NULL || latency || queues)) {
6562 pool_list_free(list);
6563 (void) fprintf(stderr,
6564 gettext("[-r|-w] isn't allowed with [-c|-l|-q]\n"));
6565 usage(B_FALSE);
6566 }
6567
6568 if (l_histo && rq_histo) {
6569 pool_list_free(list);
6570 (void) fprintf(stderr,
6571 gettext("Only one of [-r|-w] can be passed at a time\n"));
6572 usage(B_FALSE);
6573 }
6574
6575 /*
6576 * Enter the main iostat loop.
6577 */
6578 cb.cb_list = list;
6579
6580 if (l_histo) {
6581 /*
6582 * Histograms tables look out of place when you try to display
6583 * them with the other stats, so make a rule that you can only
6584 * print histograms by themselves.
6585 */
6586 cb.cb_flags = IOS_L_HISTO_M;
6587 } else if (rq_histo) {
6588 cb.cb_flags = IOS_RQ_HISTO_M;
6589 } else {
6590 cb.cb_flags = IOS_DEFAULT_M;
6591 if (latency)
6592 cb.cb_flags |= IOS_LATENCY_M;
6593 if (queues)
6594 cb.cb_flags |= IOS_QUEUES_M;
6595 }
6596
6597 /*
6598 * See if the module supports all the stats we want to display.
6599 */
6600 unsupported_flags = cb.cb_flags & ~get_stat_flags(list);
6601 if (unsupported_flags) {
6602 uint64_t f;
6603 int idx;
6604 fprintf(stderr,
6605 gettext("The loaded zfs module doesn't support:"));
6606
6607 /* for each bit set in unsupported_flags */
6608 for (f = unsupported_flags; f; f &= ~(1ULL << idx)) {
6609 idx = lowbit64(f) - 1;
6610 fprintf(stderr, " -%c", flag_to_arg[idx]);
6611 }
6612
6613 fprintf(stderr, ". Try running a newer module.\n");
6614 pool_list_free(list);
6615
6616 return (1);
6617 }
6618
6619 int last_npools = 0;
6620 for (;;) {
6621 /*
6622 * Refresh all pools in list, adding or removing pools as
6623 * necessary.
6624 */
6625 int npools = pool_list_refresh(list);
6626 if (npools == 0) {
6627 (void) fprintf(stderr, gettext("no pools available\n"));
6628 } else {
6629 /*
6630 * If the list of pools has changed since last time
6631 * around, reset the iteration count to force the
6632 * header to be redisplayed.
6633 */
6634 if (last_npools != npools)
6635 cb.cb_iteration = 0;
6636
6637 /*
6638 * If this is the first iteration and -y was supplied
6639 * we skip any printing.
6640 */
6641 boolean_t skip = (omit_since_boot &&
6642 cb.cb_iteration == 0);
6643
6644 /*
6645 * Iterate over all pools to determine the maximum width
6646 * for the pool / device name column across all pools.
6647 */
6648 cb.cb_namewidth = 0;
6649 (void) pool_list_iter(list, B_FALSE,
6650 get_namewidth_iostat, &cb);
6651
6652 if (timestamp_fmt != NODATE)
6653 print_timestamp(timestamp_fmt);
6654
6655 if (cmd != NULL && cb.cb_verbose &&
6656 !(cb.cb_flags & IOS_ANYHISTO_M)) {
6657 cb.vcdl = all_pools_for_each_vdev_run(argc,
6658 argv, cmd, g_zfs, cb.cb_vdevs.cb_names,
6659 cb.cb_vdevs.cb_names_count,
6660 cb.cb_vdevs.cb_name_flags);
6661 } else {
6662 cb.vcdl = NULL;
6663 }
6664
6665
6666 /*
6667 * Check terminal size so we can print headers
6668 * even when terminal window has its height
6669 * changed.
6670 */
6671 int winheight = terminal_height();
6672 /*
6673 * Are we connected to TTY? If not, headers_once
6674 * should be true, to avoid breaking scripts.
6675 */
6676 if (winheight < 0)
6677 headers_once = B_TRUE;
6678
6679 /*
6680 * If it's the first time and we're not skipping it,
6681 * or either skip or verbose mode, print the header.
6682 *
6683 * The histogram code explicitly prints its header on
6684 * every vdev, so skip this for histograms.
6685 */
6686 if (((++cb.cb_iteration == 1 && !skip) ||
6687 (skip != verbose) ||
6688 (!headers_once &&
6689 (cb.cb_iteration % winheight) == 0)) &&
6690 (!(cb.cb_flags & IOS_ANYHISTO_M)) &&
6691 !cb.cb_scripted)
6692 print_iostat_header(&cb);
6693
6694 if (skip) {
6695 (void) fflush(stdout);
6696 (void) fsleep(interval);
6697 last_npools = npools;
6698 continue;
6699 }
6700
6701 (void) pool_list_iter(list, B_FALSE, print_iostat, &cb);
6702
6703 /*
6704 * If there's more than one pool, and we're not in
6705 * verbose mode (which prints a separator for us),
6706 * then print a separator.
6707 *
6708 * In addition, if we're printing specific vdevs then
6709 * we also want an ending separator.
6710 */
6711 if (((npools > 1 && !verbose &&
6712 !(cb.cb_flags & IOS_ANYHISTO_M)) ||
6713 (!(cb.cb_flags & IOS_ANYHISTO_M) &&
6714 cb.cb_vdevs.cb_names_count)) &&
6715 !cb.cb_scripted) {
6716 print_iostat_separator(&cb);
6717 if (cb.vcdl != NULL)
6718 print_cmd_columns(cb.vcdl, 1);
6719 printf("\n");
6720 }
6721
6722 if (cb.vcdl != NULL)
6723 free_vdev_cmd_data_list(cb.vcdl);
6724
6725 }
6726
6727 if (interval == 0)
6728 break;
6729
6730 if (count != 0 && --count == 0)
6731 break;
6732
6733 (void) fflush(stdout);
6734 (void) fsleep(interval);
6735
6736 last_npools = npools;
6737 }
6738
6739 pool_list_free(list);
6740
6741 return (ret);
6742 }
6743
6744 typedef struct list_cbdata {
6745 boolean_t cb_verbose;
6746 int cb_name_flags;
6747 int cb_namewidth;
6748 boolean_t cb_json;
6749 boolean_t cb_scripted;
6750 zprop_list_t *cb_proplist;
6751 boolean_t cb_literal;
6752 nvlist_t *cb_jsobj;
6753 boolean_t cb_json_as_int;
6754 boolean_t cb_json_pool_key_guid;
6755 } list_cbdata_t;
6756
6757
6758 /*
6759 * Given a list of columns to display, print an appropriate line. If
6760 * `vdev_name` is not NULL, we print `vdev_name` followed by a line of dashes.
6761 * If `vdev_name` is NULL, we print a line of the headers.
6762 */
6763 static void
print_line(list_cbdata_t * cb,const char * vdev_name)6764 print_line(list_cbdata_t *cb, const char *vdev_name)
6765 {
6766 zprop_list_t *pl = cb->cb_proplist;
6767 char headerbuf[ZPOOL_MAXPROPLEN];
6768 const char *header;
6769 boolean_t first = B_TRUE;
6770 boolean_t right_justify;
6771 size_t width = 0;
6772
6773 boolean_t print_header = (vdev_name == NULL);
6774
6775 for (; pl != NULL; pl = pl->pl_next) {
6776 width = pl->pl_width;
6777 if (first && cb->cb_verbose) {
6778 /*
6779 * Reset the width to accommodate the verbose listing
6780 * of devices.
6781 */
6782 width = cb->cb_namewidth;
6783 }
6784
6785 if (!first)
6786 (void) fputs(" ", stdout);
6787
6788 if (print_header) {
6789 right_justify = B_FALSE;
6790 if (pl->pl_prop != ZPROP_USERPROP) {
6791 header = zpool_prop_column_name(pl->pl_prop);
6792 right_justify = zpool_prop_align_right(
6793 pl->pl_prop);
6794 } else {
6795 int i;
6796
6797 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
6798 headerbuf[i] = toupper(
6799 pl->pl_user_prop[i]);
6800 headerbuf[i] = '\0';
6801 header = headerbuf;
6802 }
6803
6804 }
6805 /*
6806 * If `print_header` is false, we want to print a line of
6807 * dashes.
6808 */
6809 else {
6810 if (first) {
6811 header = vdev_name;
6812 right_justify = B_FALSE;
6813 } else {
6814 header = "-";
6815 right_justify = B_TRUE;
6816 }
6817 }
6818
6819 if (pl->pl_next == NULL && !right_justify)
6820 (void) fputs(header, stdout);
6821 else if (right_justify)
6822 (void) printf("%*s", (int)width, header);
6823 else
6824 (void) printf("%-*s", (int)width, header);
6825
6826 if (first)
6827 first = B_FALSE;
6828 }
6829
6830 (void) fputc('\n', stdout);
6831 }
6832
6833 /*
6834 * Given a pool and a list of properties, print out all the properties according
6835 * to the described layout. Used by zpool_do_list().
6836 */
6837 static void
collect_pool(zpool_handle_t * zhp,list_cbdata_t * cb)6838 collect_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
6839 {
6840 zprop_list_t *pl = cb->cb_proplist;
6841 boolean_t first = B_TRUE;
6842 char property[ZPOOL_MAXPROPLEN];
6843 const char *propstr;
6844 boolean_t right_justify;
6845 size_t width;
6846 zprop_source_t sourcetype = ZPROP_SRC_NONE;
6847 nvlist_t *item, *d, *props;
6848 item = d = props = NULL;
6849
6850 if (cb->cb_json) {
6851 item = fnvlist_alloc();
6852 props = fnvlist_alloc();
6853 d = fnvlist_lookup_nvlist(cb->cb_jsobj, "pools");
6854 if (d == NULL) {
6855 fprintf(stderr, "pools obj not found.\n");
6856 exit(1);
6857 }
6858 fill_pool_info(item, zhp, B_TRUE, cb->cb_json_as_int);
6859 }
6860
6861 for (; pl != NULL; pl = pl->pl_next) {
6862
6863 width = pl->pl_width;
6864 if (first && cb->cb_verbose) {
6865 /*
6866 * Reset the width to accommodate the verbose listing
6867 * of devices.
6868 */
6869 width = cb->cb_namewidth;
6870 }
6871
6872 if (!cb->cb_json && !first) {
6873 if (cb->cb_scripted)
6874 (void) fputc('\t', stdout);
6875 else
6876 (void) fputs(" ", stdout);
6877 } else {
6878 first = B_FALSE;
6879 }
6880
6881 right_justify = B_FALSE;
6882 if (pl->pl_prop != ZPROP_USERPROP) {
6883 if (zpool_get_prop(zhp, pl->pl_prop, property,
6884 sizeof (property), &sourcetype,
6885 cb->cb_literal) != 0)
6886 propstr = "-";
6887 else
6888 propstr = property;
6889
6890 right_justify = zpool_prop_align_right(pl->pl_prop);
6891 } else if ((zpool_prop_feature(pl->pl_user_prop) ||
6892 zpool_prop_unsupported(pl->pl_user_prop)) &&
6893 zpool_prop_get_feature(zhp, pl->pl_user_prop, property,
6894 sizeof (property)) == 0) {
6895 propstr = property;
6896 sourcetype = ZPROP_SRC_LOCAL;
6897 } else if (zfs_prop_user(pl->pl_user_prop) &&
6898 zpool_get_userprop(zhp, pl->pl_user_prop, property,
6899 sizeof (property), &sourcetype) == 0) {
6900 propstr = property;
6901 } else {
6902 propstr = "-";
6903 }
6904
6905 if (cb->cb_json) {
6906 if (pl->pl_prop == ZPOOL_PROP_NAME)
6907 continue;
6908 const char *prop_name;
6909 if (pl->pl_prop != ZPROP_USERPROP)
6910 prop_name = zpool_prop_to_name(pl->pl_prop);
6911 else
6912 prop_name = pl->pl_user_prop;
6913 (void) zprop_nvlist_one_property(
6914 prop_name, propstr,
6915 sourcetype, NULL, NULL, props, cb->cb_json_as_int);
6916 } else {
6917 /*
6918 * If this is being called in scripted mode, or if this
6919 * is the last column and it is left-justified, don't
6920 * include a width format specifier.
6921 */
6922 if (cb->cb_scripted || (pl->pl_next == NULL &&
6923 !right_justify))
6924 (void) fputs(propstr, stdout);
6925 else if (right_justify)
6926 (void) printf("%*s", (int)width, propstr);
6927 else
6928 (void) printf("%-*s", (int)width, propstr);
6929 }
6930 }
6931
6932 if (cb->cb_json) {
6933 fnvlist_add_nvlist(item, "properties", props);
6934 if (cb->cb_json_pool_key_guid) {
6935 char pool_guid[256];
6936 uint64_t guid = fnvlist_lookup_uint64(
6937 zpool_get_config(zhp, NULL),
6938 ZPOOL_CONFIG_POOL_GUID);
6939 (void) snprintf(pool_guid, 256, "%llu",
6940 (u_longlong_t)guid);
6941 fnvlist_add_nvlist(d, pool_guid, item);
6942 } else {
6943 fnvlist_add_nvlist(d, zpool_get_name(zhp),
6944 item);
6945 }
6946 fnvlist_free(props);
6947 fnvlist_free(item);
6948 } else
6949 (void) fputc('\n', stdout);
6950 }
6951
6952 static void
collect_vdev_prop(zpool_prop_t prop,uint64_t value,const char * str,boolean_t scripted,boolean_t valid,enum zfs_nicenum_format format,boolean_t json,nvlist_t * nvl,boolean_t as_int)6953 collect_vdev_prop(zpool_prop_t prop, uint64_t value, const char *str,
6954 boolean_t scripted, boolean_t valid, enum zfs_nicenum_format format,
6955 boolean_t json, nvlist_t *nvl, boolean_t as_int)
6956 {
6957 char propval[64];
6958 boolean_t fixed;
6959 size_t width = zprop_width(prop, &fixed, ZFS_TYPE_POOL);
6960
6961 switch (prop) {
6962 case ZPOOL_PROP_SIZE:
6963 case ZPOOL_PROP_EXPANDSZ:
6964 case ZPOOL_PROP_CHECKPOINT:
6965 case ZPOOL_PROP_DEDUPRATIO:
6966 case ZPOOL_PROP_DEDUPCACHED:
6967 if (value == 0)
6968 (void) strlcpy(propval, "-", sizeof (propval));
6969 else
6970 zfs_nicenum_format(value, propval, sizeof (propval),
6971 format);
6972 break;
6973 case ZPOOL_PROP_FRAGMENTATION:
6974 if (value == ZFS_FRAG_INVALID) {
6975 (void) strlcpy(propval, "-", sizeof (propval));
6976 } else if (format == ZFS_NICENUM_RAW) {
6977 (void) snprintf(propval, sizeof (propval), "%llu",
6978 (unsigned long long)value);
6979 } else {
6980 (void) snprintf(propval, sizeof (propval), "%llu%%",
6981 (unsigned long long)value);
6982 }
6983 break;
6984 case ZPOOL_PROP_CAPACITY:
6985 /* capacity value is in parts-per-10,000 (aka permyriad) */
6986 if (format == ZFS_NICENUM_RAW)
6987 (void) snprintf(propval, sizeof (propval), "%llu",
6988 (unsigned long long)value / 100);
6989 else
6990 (void) snprintf(propval, sizeof (propval),
6991 value < 1000 ? "%1.2f%%" : value < 10000 ?
6992 "%2.1f%%" : "%3.0f%%", value / 100.0);
6993 break;
6994 case ZPOOL_PROP_HEALTH:
6995 width = 8;
6996 (void) strlcpy(propval, str, sizeof (propval));
6997 break;
6998 default:
6999 zfs_nicenum_format(value, propval, sizeof (propval), format);
7000 }
7001
7002 if (!valid)
7003 (void) strlcpy(propval, "-", sizeof (propval));
7004
7005 if (json) {
7006 (void) zprop_nvlist_one_property(zpool_prop_to_name(prop),
7007 propval, ZPROP_SRC_NONE, NULL, NULL, nvl, as_int);
7008 } else {
7009 if (scripted)
7010 (void) printf("\t%s", propval);
7011 else
7012 (void) printf(" %*s", (int)width, propval);
7013 }
7014 }
7015
7016 /*
7017 * print static default line per vdev
7018 */
7019 static void
collect_list_stats(zpool_handle_t * zhp,const char * name,nvlist_t * nv,list_cbdata_t * cb,int depth,boolean_t isspare,nvlist_t * item)7020 collect_list_stats(zpool_handle_t *zhp, const char *name, nvlist_t *nv,
7021 list_cbdata_t *cb, int depth, boolean_t isspare, nvlist_t *item)
7022 {
7023 nvlist_t **child;
7024 vdev_stat_t *vs;
7025 uint_t c, children = 0;
7026 char *vname;
7027 boolean_t scripted = cb->cb_scripted;
7028 uint64_t islog = B_FALSE;
7029 nvlist_t *props, *ent, *ch, *obj, *l2c, *sp;
7030 props = ent = ch = obj = sp = l2c = NULL;
7031
7032 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
7033 (uint64_t **)&vs, &c) == 0);
7034
7035 if (name != NULL) {
7036 boolean_t toplevel = (vs->vs_space != 0);
7037 uint64_t cap;
7038 enum zfs_nicenum_format format;
7039 const char *state;
7040
7041 if (cb->cb_literal)
7042 format = ZFS_NICENUM_RAW;
7043 else
7044 format = ZFS_NICENUM_1024;
7045
7046 if (strcmp(name, VDEV_TYPE_INDIRECT) == 0)
7047 return;
7048
7049 if (cb->cb_json) {
7050 props = fnvlist_alloc();
7051 ent = fnvlist_alloc();
7052 fill_vdev_info(ent, zhp, (char *)name, B_FALSE,
7053 cb->cb_json_as_int);
7054 } else {
7055 if (scripted)
7056 (void) printf("\t%s", name);
7057 else if (strlen(name) + depth > cb->cb_namewidth)
7058 (void) printf("%*s%s", depth, "", name);
7059 else
7060 (void) printf("%*s%s%*s", depth, "", name,
7061 (int)(cb->cb_namewidth - strlen(name) -
7062 depth), "");
7063 }
7064
7065 /*
7066 * Print the properties for the individual vdevs. Some
7067 * properties are only applicable to toplevel vdevs. The
7068 * 'toplevel' boolean value is passed to the print_one_column()
7069 * to indicate that the value is valid.
7070 */
7071 for (zprop_list_t *pl = cb->cb_proplist; pl != NULL;
7072 pl = pl->pl_next) {
7073 switch (pl->pl_prop) {
7074 case ZPOOL_PROP_SIZE:
7075 if (VDEV_STAT_VALID(vs_pspace, c) &&
7076 vs->vs_pspace) {
7077 collect_vdev_prop(
7078 ZPOOL_PROP_SIZE, vs->vs_pspace,
7079 NULL, scripted, B_TRUE, format,
7080 cb->cb_json, props,
7081 cb->cb_json_as_int);
7082 } else {
7083 collect_vdev_prop(
7084 ZPOOL_PROP_SIZE, vs->vs_space, NULL,
7085 scripted, toplevel, format,
7086 cb->cb_json, props,
7087 cb->cb_json_as_int);
7088 }
7089 break;
7090 case ZPOOL_PROP_ALLOCATED:
7091 collect_vdev_prop(ZPOOL_PROP_ALLOCATED,
7092 vs->vs_alloc, NULL, scripted, toplevel,
7093 format, cb->cb_json, props,
7094 cb->cb_json_as_int);
7095 break;
7096
7097 case ZPOOL_PROP_FREE:
7098 collect_vdev_prop(ZPOOL_PROP_FREE,
7099 vs->vs_space - vs->vs_alloc, NULL, scripted,
7100 toplevel, format, cb->cb_json, props,
7101 cb->cb_json_as_int);
7102 break;
7103
7104 case ZPOOL_PROP_CHECKPOINT:
7105 collect_vdev_prop(ZPOOL_PROP_CHECKPOINT,
7106 vs->vs_checkpoint_space, NULL, scripted,
7107 toplevel, format, cb->cb_json, props,
7108 cb->cb_json_as_int);
7109 break;
7110
7111 case ZPOOL_PROP_EXPANDSZ:
7112 collect_vdev_prop(ZPOOL_PROP_EXPANDSZ,
7113 vs->vs_esize, NULL, scripted, B_TRUE,
7114 format, cb->cb_json, props,
7115 cb->cb_json_as_int);
7116 break;
7117
7118 case ZPOOL_PROP_FRAGMENTATION:
7119 collect_vdev_prop(
7120 ZPOOL_PROP_FRAGMENTATION,
7121 vs->vs_fragmentation, NULL, scripted,
7122 (vs->vs_fragmentation != ZFS_FRAG_INVALID &&
7123 toplevel),
7124 format, cb->cb_json, props,
7125 cb->cb_json_as_int);
7126 break;
7127
7128 case ZPOOL_PROP_CAPACITY:
7129 cap = (vs->vs_space == 0) ?
7130 0 : (vs->vs_alloc * 10000 / vs->vs_space);
7131 collect_vdev_prop(ZPOOL_PROP_CAPACITY, cap,
7132 NULL, scripted, toplevel, format,
7133 cb->cb_json, props, cb->cb_json_as_int);
7134 break;
7135
7136 case ZPOOL_PROP_HEALTH:
7137 state = zpool_state_to_name(vs->vs_state,
7138 vs->vs_aux);
7139 if (isspare) {
7140 if (vs->vs_aux == VDEV_AUX_SPARED)
7141 state = "INUSE";
7142 else if (vs->vs_state ==
7143 VDEV_STATE_HEALTHY)
7144 state = "AVAIL";
7145 }
7146 collect_vdev_prop(ZPOOL_PROP_HEALTH, 0, state,
7147 scripted, B_TRUE, format, cb->cb_json,
7148 props, cb->cb_json_as_int);
7149 break;
7150
7151 case ZPOOL_PROP_NAME:
7152 break;
7153
7154 default:
7155 collect_vdev_prop(pl->pl_prop, 0,
7156 NULL, scripted, B_FALSE, format,
7157 cb->cb_json, props, cb->cb_json_as_int);
7158
7159 }
7160
7161
7162 }
7163
7164 if (cb->cb_json) {
7165 fnvlist_add_nvlist(ent, "properties", props);
7166 fnvlist_free(props);
7167 } else
7168 (void) fputc('\n', stdout);
7169 }
7170
7171 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
7172 &child, &children) != 0) {
7173 if (cb->cb_json) {
7174 fnvlist_add_nvlist(item, name, ent);
7175 fnvlist_free(ent);
7176 }
7177 return;
7178 }
7179
7180 if (cb->cb_json) {
7181 ch = fnvlist_alloc();
7182 }
7183
7184 /* list the normal vdevs first */
7185 for (c = 0; c < children; c++) {
7186 uint64_t ishole = B_FALSE;
7187
7188 if (nvlist_lookup_uint64(child[c],
7189 ZPOOL_CONFIG_IS_HOLE, &ishole) == 0 && ishole)
7190 continue;
7191
7192 if (nvlist_lookup_uint64(child[c],
7193 ZPOOL_CONFIG_IS_LOG, &islog) == 0 && islog)
7194 continue;
7195
7196 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
7197 continue;
7198
7199 vname = zpool_vdev_name(g_zfs, zhp, child[c],
7200 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
7201
7202 if (name == NULL || cb->cb_json != B_TRUE)
7203 collect_list_stats(zhp, vname, child[c], cb, depth + 2,
7204 B_FALSE, item);
7205 else if (cb->cb_json) {
7206 collect_list_stats(zhp, vname, child[c], cb, depth + 2,
7207 B_FALSE, ch);
7208 }
7209 free(vname);
7210 }
7211
7212 if (cb->cb_json) {
7213 if (!nvlist_empty(ch))
7214 fnvlist_add_nvlist(ent, "vdevs", ch);
7215 fnvlist_free(ch);
7216 }
7217
7218 /* list the classes: 'logs', 'dedup', and 'special' */
7219 for (uint_t n = 0; n < ARRAY_SIZE(class_name); n++) {
7220 boolean_t printed = B_FALSE;
7221 if (cb->cb_json)
7222 obj = fnvlist_alloc();
7223 for (c = 0; c < children; c++) {
7224 const char *bias = NULL;
7225 const char *type = NULL;
7226
7227 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
7228 &islog) == 0 && islog) {
7229 bias = VDEV_ALLOC_CLASS_LOGS;
7230 } else {
7231 (void) nvlist_lookup_string(child[c],
7232 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
7233 (void) nvlist_lookup_string(child[c],
7234 ZPOOL_CONFIG_TYPE, &type);
7235 }
7236 if (bias == NULL || strcmp(bias, class_name[n]) != 0)
7237 continue;
7238 if (!islog && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
7239 continue;
7240
7241 if (!printed && !cb->cb_json) {
7242 print_line(cb, class_name[n]);
7243 printed = B_TRUE;
7244 }
7245 vname = zpool_vdev_name(g_zfs, zhp, child[c],
7246 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
7247 collect_list_stats(zhp, vname, child[c], cb, depth + 2,
7248 B_FALSE, obj);
7249 free(vname);
7250 }
7251 if (cb->cb_json) {
7252 if (!nvlist_empty(obj))
7253 fnvlist_add_nvlist(item, class_name[n], obj);
7254 fnvlist_free(obj);
7255 }
7256 }
7257
7258 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
7259 &child, &children) == 0 && children > 0) {
7260 if (cb->cb_json) {
7261 l2c = fnvlist_alloc();
7262 } else {
7263 print_line(cb, "cache");
7264 }
7265 for (c = 0; c < children; c++) {
7266 vname = zpool_vdev_name(g_zfs, zhp, child[c],
7267 cb->cb_name_flags);
7268 collect_list_stats(zhp, vname, child[c], cb, depth + 2,
7269 B_FALSE, l2c);
7270 free(vname);
7271 }
7272 if (cb->cb_json) {
7273 if (!nvlist_empty(l2c))
7274 fnvlist_add_nvlist(item, "l2cache", l2c);
7275 fnvlist_free(l2c);
7276 }
7277 }
7278
7279 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, &child,
7280 &children) == 0 && children > 0) {
7281 if (cb->cb_json) {
7282 sp = fnvlist_alloc();
7283 } else {
7284 print_line(cb, "spare");
7285 }
7286 for (c = 0; c < children; c++) {
7287 vname = zpool_vdev_name(g_zfs, zhp, child[c],
7288 cb->cb_name_flags);
7289 collect_list_stats(zhp, vname, child[c], cb, depth + 2,
7290 B_TRUE, sp);
7291 free(vname);
7292 }
7293 if (cb->cb_json) {
7294 if (!nvlist_empty(sp))
7295 fnvlist_add_nvlist(item, "spares", sp);
7296 fnvlist_free(sp);
7297 }
7298 }
7299
7300 if (name != NULL && cb->cb_json) {
7301 fnvlist_add_nvlist(item, name, ent);
7302 fnvlist_free(ent);
7303 }
7304 }
7305
7306 /*
7307 * Generic callback function to list a pool.
7308 */
7309 static int
list_callback(zpool_handle_t * zhp,void * data)7310 list_callback(zpool_handle_t *zhp, void *data)
7311 {
7312 nvlist_t *p, *d, *nvdevs;
7313 uint64_t guid;
7314 char pool_guid[256];
7315 const char *pool_name = zpool_get_name(zhp);
7316 list_cbdata_t *cbp = data;
7317 p = d = nvdevs = NULL;
7318
7319 collect_pool(zhp, cbp);
7320
7321 if (cbp->cb_verbose) {
7322 nvlist_t *config, *nvroot;
7323 config = zpool_get_config(zhp, NULL);
7324 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
7325 &nvroot) == 0);
7326 if (cbp->cb_json) {
7327 d = fnvlist_lookup_nvlist(cbp->cb_jsobj,
7328 "pools");
7329 if (cbp->cb_json_pool_key_guid) {
7330 guid = fnvlist_lookup_uint64(config,
7331 ZPOOL_CONFIG_POOL_GUID);
7332 (void) snprintf(pool_guid, 256, "%llu",
7333 (u_longlong_t)guid);
7334 p = fnvlist_lookup_nvlist(d, pool_guid);
7335 } else {
7336 p = fnvlist_lookup_nvlist(d, pool_name);
7337 }
7338 nvdevs = fnvlist_alloc();
7339 }
7340 collect_list_stats(zhp, NULL, nvroot, cbp, 0, B_FALSE, nvdevs);
7341 if (cbp->cb_json) {
7342 fnvlist_add_nvlist(p, "vdevs", nvdevs);
7343 if (cbp->cb_json_pool_key_guid)
7344 fnvlist_add_nvlist(d, pool_guid, p);
7345 else
7346 fnvlist_add_nvlist(d, pool_name, p);
7347 fnvlist_add_nvlist(cbp->cb_jsobj, "pools", d);
7348 fnvlist_free(nvdevs);
7349 }
7350 }
7351
7352 return (0);
7353 }
7354
7355 /*
7356 * Set the minimum pool/vdev name column width. The width must be at least 9,
7357 * but may be as large as needed.
7358 */
7359 static int
get_namewidth_list(zpool_handle_t * zhp,void * data)7360 get_namewidth_list(zpool_handle_t *zhp, void *data)
7361 {
7362 list_cbdata_t *cb = data;
7363 int width;
7364
7365 width = get_namewidth(zhp, cb->cb_namewidth,
7366 cb->cb_name_flags | VDEV_NAME_TYPE_ID, cb->cb_verbose);
7367
7368 if (width < 9)
7369 width = 9;
7370
7371 cb->cb_namewidth = width;
7372
7373 return (0);
7374 }
7375
7376 /*
7377 * zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
7378 *
7379 * -g Display guid for individual vdev name.
7380 * -H Scripted mode. Don't display headers, and separate properties
7381 * by a single tab.
7382 * -L Follow links when resolving vdev path name.
7383 * -o List of properties to display. Defaults to
7384 * "name,size,allocated,free,expandsize,fragmentation,capacity,"
7385 * "dedupratio,health,altroot"
7386 * -p Display values in parsable (exact) format.
7387 * -P Display full path for vdev name.
7388 * -T Display a timestamp in date(1) or Unix format
7389 * -j Display the output in JSON format
7390 * --json-int Display the numbers as integer instead of strings.
7391 * --json-pool-key-guid Set pool GUID as key for pool objects.
7392 *
7393 * List all pools in the system, whether or not they're healthy. Output space
7394 * statistics for each one, as well as health status summary.
7395 */
7396 int
zpool_do_list(int argc,char ** argv)7397 zpool_do_list(int argc, char **argv)
7398 {
7399 int c;
7400 int ret = 0;
7401 list_cbdata_t cb = { 0 };
7402 static char default_props[] =
7403 "name,size,allocated,free,checkpoint,expandsize,fragmentation,"
7404 "capacity,dedupratio,health,altroot";
7405 char *props = default_props;
7406 float interval = 0;
7407 unsigned long count = 0;
7408 zpool_list_t *list;
7409 boolean_t first = B_TRUE;
7410 nvlist_t *data = NULL;
7411 current_prop_type = ZFS_TYPE_POOL;
7412
7413 struct option long_options[] = {
7414 {"json", no_argument, NULL, 'j'},
7415 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT},
7416 {"json-pool-key-guid", no_argument, NULL,
7417 ZPOOL_OPTION_POOL_KEY_GUID},
7418 {0, 0, 0, 0}
7419 };
7420
7421 /* check options */
7422 while ((c = getopt_long(argc, argv, ":gjHLo:pPT:v", long_options,
7423 NULL)) != -1) {
7424 switch (c) {
7425 case 'g':
7426 cb.cb_name_flags |= VDEV_NAME_GUID;
7427 break;
7428 case 'H':
7429 cb.cb_scripted = B_TRUE;
7430 break;
7431 case 'L':
7432 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
7433 break;
7434 case 'o':
7435 props = optarg;
7436 break;
7437 case 'P':
7438 cb.cb_name_flags |= VDEV_NAME_PATH;
7439 break;
7440 case 'p':
7441 cb.cb_literal = B_TRUE;
7442 break;
7443 case 'j':
7444 cb.cb_json = B_TRUE;
7445 break;
7446 case ZPOOL_OPTION_JSON_NUMS_AS_INT:
7447 cb.cb_json_as_int = B_TRUE;
7448 cb.cb_literal = B_TRUE;
7449 break;
7450 case ZPOOL_OPTION_POOL_KEY_GUID:
7451 cb.cb_json_pool_key_guid = B_TRUE;
7452 break;
7453 case 'T':
7454 get_timestamp_arg(*optarg);
7455 break;
7456 case 'v':
7457 cb.cb_verbose = B_TRUE;
7458 cb.cb_namewidth = 8; /* 8 until precalc is avail */
7459 break;
7460 case ':':
7461 (void) fprintf(stderr, gettext("missing argument for "
7462 "'%c' option\n"), optopt);
7463 usage(B_FALSE);
7464 break;
7465 case '?':
7466 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7467 optopt);
7468 usage(B_FALSE);
7469 }
7470 }
7471
7472 argc -= optind;
7473 argv += optind;
7474
7475 if (!cb.cb_json && cb.cb_json_as_int) {
7476 (void) fprintf(stderr, gettext("'--json-int' only works with"
7477 " '-j' option\n"));
7478 usage(B_FALSE);
7479 }
7480
7481 if (!cb.cb_json && cb.cb_json_pool_key_guid) {
7482 (void) fprintf(stderr, gettext("'json-pool-key-guid' only"
7483 " works with '-j' option\n"));
7484 usage(B_FALSE);
7485 }
7486
7487 get_interval_count(&argc, argv, &interval, &count);
7488
7489 if (zprop_get_list(g_zfs, props, &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
7490 usage(B_FALSE);
7491
7492 for (;;) {
7493 if ((list = pool_list_get(argc, argv, &cb.cb_proplist,
7494 ZFS_TYPE_POOL, cb.cb_literal, &ret)) == NULL)
7495 return (1);
7496
7497 if (pool_list_count(list) == 0)
7498 break;
7499
7500 if (cb.cb_json) {
7501 cb.cb_jsobj = zpool_json_schema(0, 1);
7502 data = fnvlist_alloc();
7503 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data);
7504 fnvlist_free(data);
7505 }
7506
7507 cb.cb_namewidth = 0;
7508 (void) pool_list_iter(list, B_FALSE, get_namewidth_list, &cb);
7509
7510 if (timestamp_fmt != NODATE) {
7511 if (cb.cb_json) {
7512 if (cb.cb_json_as_int) {
7513 fnvlist_add_uint64(cb.cb_jsobj, "time",
7514 time(NULL));
7515 } else {
7516 char ts[128];
7517 get_timestamp(timestamp_fmt, ts, 128);
7518 fnvlist_add_string(cb.cb_jsobj, "time",
7519 ts);
7520 }
7521 } else
7522 print_timestamp(timestamp_fmt);
7523 }
7524
7525 if (!cb.cb_scripted && (first || cb.cb_verbose) &&
7526 !cb.cb_json) {
7527 print_line(&cb, NULL);
7528 first = B_FALSE;
7529 }
7530 ret = pool_list_iter(list, B_TRUE, list_callback, &cb);
7531
7532 if (ret == 0 && cb.cb_json)
7533 zcmd_print_json(cb.cb_jsobj);
7534 else if (ret != 0 && cb.cb_json)
7535 nvlist_free(cb.cb_jsobj);
7536
7537 if (interval == 0)
7538 break;
7539
7540 if (count != 0 && --count == 0)
7541 break;
7542
7543 pool_list_free(list);
7544
7545 (void) fflush(stdout);
7546 (void) fsleep(interval);
7547 }
7548
7549 if (argc == 0 && !cb.cb_scripted && !cb.cb_json &&
7550 pool_list_count(list) == 0) {
7551 (void) printf(gettext("no pools available\n"));
7552 ret = 0;
7553 }
7554
7555 pool_list_free(list);
7556 zprop_free_list(cb.cb_proplist);
7557 return (ret);
7558 }
7559
7560 static int
zpool_do_attach_or_replace(int argc,char ** argv,int replacing)7561 zpool_do_attach_or_replace(int argc, char **argv, int replacing)
7562 {
7563 boolean_t force = B_FALSE;
7564 boolean_t rebuild = B_FALSE;
7565 boolean_t wait = B_FALSE;
7566 int c;
7567 nvlist_t *nvroot;
7568 char *poolname, *old_disk, *new_disk;
7569 zpool_handle_t *zhp;
7570 nvlist_t *props = NULL;
7571 char *propval;
7572 int ret;
7573
7574 /* check options */
7575 while ((c = getopt(argc, argv, "fo:sw")) != -1) {
7576 switch (c) {
7577 case 'f':
7578 force = B_TRUE;
7579 break;
7580 case 'o':
7581 if ((propval = strchr(optarg, '=')) == NULL) {
7582 (void) fprintf(stderr, gettext("missing "
7583 "'=' for -o option\n"));
7584 usage(B_FALSE);
7585 }
7586 *propval = '\0';
7587 propval++;
7588
7589 if ((strcmp(optarg, ZPOOL_CONFIG_ASHIFT) != 0) ||
7590 (add_prop_list(optarg, propval, &props, B_TRUE)))
7591 usage(B_FALSE);
7592 break;
7593 case 's':
7594 rebuild = B_TRUE;
7595 break;
7596 case 'w':
7597 wait = B_TRUE;
7598 break;
7599 case '?':
7600 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7601 optopt);
7602 usage(B_FALSE);
7603 }
7604 }
7605
7606 argc -= optind;
7607 argv += optind;
7608
7609 /* get pool name and check number of arguments */
7610 if (argc < 1) {
7611 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7612 usage(B_FALSE);
7613 }
7614
7615 poolname = argv[0];
7616
7617 if (argc < 2) {
7618 (void) fprintf(stderr,
7619 gettext("missing <device> specification\n"));
7620 usage(B_FALSE);
7621 }
7622
7623 old_disk = argv[1];
7624
7625 if (argc < 3) {
7626 if (!replacing) {
7627 (void) fprintf(stderr,
7628 gettext("missing <new_device> specification\n"));
7629 usage(B_FALSE);
7630 }
7631 new_disk = old_disk;
7632 argc -= 1;
7633 argv += 1;
7634 } else {
7635 new_disk = argv[2];
7636 argc -= 2;
7637 argv += 2;
7638 }
7639
7640 if (argc > 1) {
7641 (void) fprintf(stderr, gettext("too many arguments\n"));
7642 usage(B_FALSE);
7643 }
7644
7645 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7646 nvlist_free(props);
7647 return (1);
7648 }
7649
7650 if (zpool_get_config(zhp, NULL) == NULL) {
7651 (void) fprintf(stderr, gettext("pool '%s' is unavailable\n"),
7652 poolname);
7653 zpool_close(zhp);
7654 nvlist_free(props);
7655 return (1);
7656 }
7657
7658 /* unless manually specified use "ashift" pool property (if set) */
7659 if (!nvlist_exists(props, ZPOOL_CONFIG_ASHIFT)) {
7660 int intval;
7661 zprop_source_t src;
7662 char strval[ZPOOL_MAXPROPLEN];
7663
7664 intval = zpool_get_prop_int(zhp, ZPOOL_PROP_ASHIFT, &src);
7665 if (src != ZPROP_SRC_DEFAULT) {
7666 (void) sprintf(strval, "%" PRId32, intval);
7667 verify(add_prop_list(ZPOOL_CONFIG_ASHIFT, strval,
7668 &props, B_TRUE) == 0);
7669 }
7670 }
7671
7672 nvroot = make_root_vdev(zhp, props, force, B_FALSE, replacing, B_FALSE,
7673 argc, argv);
7674 if (nvroot == NULL) {
7675 zpool_close(zhp);
7676 nvlist_free(props);
7677 return (1);
7678 }
7679
7680 ret = zpool_vdev_attach(zhp, old_disk, new_disk, nvroot, replacing,
7681 rebuild);
7682
7683 if (ret == 0 && wait) {
7684 zpool_wait_activity_t activity = ZPOOL_WAIT_RESILVER;
7685 char raidz_prefix[] = "raidz";
7686 if (replacing) {
7687 activity = ZPOOL_WAIT_REPLACE;
7688 } else if (strncmp(old_disk,
7689 raidz_prefix, strlen(raidz_prefix)) == 0) {
7690 activity = ZPOOL_WAIT_RAIDZ_EXPAND;
7691 }
7692 ret = zpool_wait(zhp, activity);
7693 }
7694
7695 nvlist_free(props);
7696 nvlist_free(nvroot);
7697 zpool_close(zhp);
7698
7699 return (ret);
7700 }
7701
7702 /*
7703 * zpool replace [-fsw] [-o property=value] <pool> <device> <new_device>
7704 *
7705 * -f Force attach, even if <new_device> appears to be in use.
7706 * -s Use sequential instead of healing reconstruction for resilver.
7707 * -o Set property=value.
7708 * -w Wait for replacing to complete before returning
7709 *
7710 * Replace <device> with <new_device>.
7711 */
7712 int
zpool_do_replace(int argc,char ** argv)7713 zpool_do_replace(int argc, char **argv)
7714 {
7715 return (zpool_do_attach_or_replace(argc, argv, B_TRUE));
7716 }
7717
7718 /*
7719 * zpool attach [-fsw] [-o property=value] <pool> <vdev> <new_device>
7720 *
7721 * -f Force attach, even if <new_device> appears to be in use.
7722 * -s Use sequential instead of healing reconstruction for resilver.
7723 * -o Set property=value.
7724 * -w Wait for resilvering (mirror) or expansion (raidz) to complete
7725 * before returning.
7726 *
7727 * Attach <new_device> to a <vdev>, where the vdev can be of type
7728 * device, mirror or raidz. If <vdev> is not part of a mirror, then <vdev> will
7729 * be transformed into a mirror of <vdev> and <new_device>. When a mirror
7730 * is involved, <new_device> will begin life with a DTL of [0, now], and will
7731 * immediately begin to resilver itself. For the raidz case, a expansion will
7732 * commence and reflow the raidz data across all the disks including the
7733 * <new_device>.
7734 */
7735 int
zpool_do_attach(int argc,char ** argv)7736 zpool_do_attach(int argc, char **argv)
7737 {
7738 return (zpool_do_attach_or_replace(argc, argv, B_FALSE));
7739 }
7740
7741 /*
7742 * zpool detach [-f] <pool> <device>
7743 *
7744 * -f Force detach of <device>, even if DTLs argue against it
7745 * (not supported yet)
7746 *
7747 * Detach a device from a mirror. The operation will be refused if <device>
7748 * is the last device in the mirror, or if the DTLs indicate that this device
7749 * has the only valid copy of some data.
7750 */
7751 int
zpool_do_detach(int argc,char ** argv)7752 zpool_do_detach(int argc, char **argv)
7753 {
7754 int c;
7755 char *poolname, *path;
7756 zpool_handle_t *zhp;
7757 int ret;
7758
7759 /* check options */
7760 while ((c = getopt(argc, argv, "")) != -1) {
7761 switch (c) {
7762 case '?':
7763 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7764 optopt);
7765 usage(B_FALSE);
7766 }
7767 }
7768
7769 argc -= optind;
7770 argv += optind;
7771
7772 /* get pool name and check number of arguments */
7773 if (argc < 1) {
7774 (void) fprintf(stderr, gettext("missing pool name argument\n"));
7775 usage(B_FALSE);
7776 }
7777
7778 if (argc < 2) {
7779 (void) fprintf(stderr,
7780 gettext("missing <device> specification\n"));
7781 usage(B_FALSE);
7782 }
7783
7784 poolname = argv[0];
7785 path = argv[1];
7786
7787 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
7788 return (1);
7789
7790 ret = zpool_vdev_detach(zhp, path);
7791
7792 zpool_close(zhp);
7793
7794 return (ret);
7795 }
7796
7797 /*
7798 * zpool split [-gLnP] [-o prop=val] ...
7799 * [-o mntopt] ...
7800 * [-R altroot] <pool> <newpool> [<device> ...]
7801 *
7802 * -g Display guid for individual vdev name.
7803 * -L Follow links when resolving vdev path name.
7804 * -n Do not split the pool, but display the resulting layout if
7805 * it were to be split.
7806 * -o Set property=value, or set mount options.
7807 * -P Display full path for vdev name.
7808 * -R Mount the split-off pool under an alternate root.
7809 * -l Load encryption keys while importing.
7810 *
7811 * Splits the named pool and gives it the new pool name. Devices to be split
7812 * off may be listed, provided that no more than one device is specified
7813 * per top-level vdev mirror. The newly split pool is left in an exported
7814 * state unless -R is specified.
7815 *
7816 * Restrictions: the top-level of the pool pool must only be made up of
7817 * mirrors; all devices in the pool must be healthy; no device may be
7818 * undergoing a resilvering operation.
7819 */
7820 int
zpool_do_split(int argc,char ** argv)7821 zpool_do_split(int argc, char **argv)
7822 {
7823 char *srcpool, *newpool, *propval;
7824 char *mntopts = NULL;
7825 splitflags_t flags;
7826 int c, ret = 0;
7827 int ms_status = 0;
7828 boolean_t loadkeys = B_FALSE;
7829 zpool_handle_t *zhp;
7830 nvlist_t *config, *props = NULL;
7831
7832 flags.dryrun = B_FALSE;
7833 flags.import = B_FALSE;
7834 flags.name_flags = 0;
7835
7836 /* check options */
7837 while ((c = getopt(argc, argv, ":gLR:lno:P")) != -1) {
7838 switch (c) {
7839 case 'g':
7840 flags.name_flags |= VDEV_NAME_GUID;
7841 break;
7842 case 'L':
7843 flags.name_flags |= VDEV_NAME_FOLLOW_LINKS;
7844 break;
7845 case 'R':
7846 flags.import = B_TRUE;
7847 if (add_prop_list(
7848 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), optarg,
7849 &props, B_TRUE) != 0) {
7850 nvlist_free(props);
7851 usage(B_FALSE);
7852 }
7853 break;
7854 case 'l':
7855 loadkeys = B_TRUE;
7856 break;
7857 case 'n':
7858 flags.dryrun = B_TRUE;
7859 break;
7860 case 'o':
7861 if ((propval = strchr(optarg, '=')) != NULL) {
7862 *propval = '\0';
7863 propval++;
7864 if (add_prop_list(optarg, propval,
7865 &props, B_TRUE) != 0) {
7866 nvlist_free(props);
7867 usage(B_FALSE);
7868 }
7869 } else {
7870 mntopts = optarg;
7871 }
7872 break;
7873 case 'P':
7874 flags.name_flags |= VDEV_NAME_PATH;
7875 break;
7876 case ':':
7877 (void) fprintf(stderr, gettext("missing argument for "
7878 "'%c' option\n"), optopt);
7879 usage(B_FALSE);
7880 break;
7881 case '?':
7882 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7883 optopt);
7884 usage(B_FALSE);
7885 break;
7886 }
7887 }
7888
7889 if (!flags.import && mntopts != NULL) {
7890 (void) fprintf(stderr, gettext("setting mntopts is only "
7891 "valid when importing the pool\n"));
7892 usage(B_FALSE);
7893 }
7894
7895 if (!flags.import && loadkeys) {
7896 (void) fprintf(stderr, gettext("loading keys is only "
7897 "valid when importing the pool\n"));
7898 usage(B_FALSE);
7899 }
7900
7901 argc -= optind;
7902 argv += optind;
7903
7904 if (argc < 1) {
7905 (void) fprintf(stderr, gettext("Missing pool name\n"));
7906 usage(B_FALSE);
7907 }
7908 if (argc < 2) {
7909 (void) fprintf(stderr, gettext("Missing new pool name\n"));
7910 usage(B_FALSE);
7911 }
7912
7913 srcpool = argv[0];
7914 newpool = argv[1];
7915
7916 argc -= 2;
7917 argv += 2;
7918
7919 if ((zhp = zpool_open(g_zfs, srcpool)) == NULL) {
7920 nvlist_free(props);
7921 return (1);
7922 }
7923
7924 config = split_mirror_vdev(zhp, newpool, props, flags, argc, argv);
7925 if (config == NULL) {
7926 ret = 1;
7927 } else {
7928 if (flags.dryrun) {
7929 (void) printf(gettext("would create '%s' with the "
7930 "following layout:\n\n"), newpool);
7931 print_vdev_tree(NULL, newpool, config, 0, "",
7932 flags.name_flags);
7933 print_vdev_tree(NULL, "dedup", config, 0,
7934 VDEV_ALLOC_BIAS_DEDUP, 0);
7935 print_vdev_tree(NULL, "special", config, 0,
7936 VDEV_ALLOC_BIAS_SPECIAL, 0);
7937 }
7938 }
7939
7940 zpool_close(zhp);
7941
7942 if (ret != 0 || flags.dryrun || !flags.import) {
7943 nvlist_free(config);
7944 nvlist_free(props);
7945 return (ret);
7946 }
7947
7948 /*
7949 * The split was successful. Now we need to open the new
7950 * pool and import it.
7951 */
7952 if ((zhp = zpool_open_canfail(g_zfs, newpool)) == NULL) {
7953 nvlist_free(config);
7954 nvlist_free(props);
7955 return (1);
7956 }
7957
7958 if (loadkeys) {
7959 ret = zfs_crypto_attempt_load_keys(g_zfs, newpool);
7960 if (ret != 0)
7961 ret = 1;
7962 }
7963
7964 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) {
7965 ms_status = zpool_enable_datasets(zhp, mntopts, 0,
7966 mount_tp_nthr);
7967 if (ms_status == EZFS_SHAREFAILED) {
7968 (void) fprintf(stderr, gettext("Split was successful, "
7969 "datasets are mounted but sharing of some datasets "
7970 "has failed\n"));
7971 } else if (ms_status == EZFS_MOUNTFAILED) {
7972 (void) fprintf(stderr, gettext("Split was successful"
7973 ", but some datasets could not be mounted\n"));
7974 (void) fprintf(stderr, gettext("Try doing '%s' with a "
7975 "different altroot\n"), "zpool import");
7976 }
7977 }
7978 zpool_close(zhp);
7979 nvlist_free(config);
7980 nvlist_free(props);
7981
7982 return (ret);
7983 }
7984
7985
7986 /*
7987 * zpool online [--power] <pool> <device> ...
7988 *
7989 * --power: Power on the enclosure slot to the drive (if possible)
7990 */
7991 int
zpool_do_online(int argc,char ** argv)7992 zpool_do_online(int argc, char **argv)
7993 {
7994 int c, i;
7995 char *poolname;
7996 zpool_handle_t *zhp;
7997 int ret = 0;
7998 vdev_state_t newstate;
7999 int flags = 0;
8000 boolean_t is_power_on = B_FALSE;
8001 struct option long_options[] = {
8002 {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
8003 {0, 0, 0, 0}
8004 };
8005
8006 /* check options */
8007 while ((c = getopt_long(argc, argv, "e", long_options, NULL)) != -1) {
8008 switch (c) {
8009 case 'e':
8010 flags |= ZFS_ONLINE_EXPAND;
8011 break;
8012 case ZPOOL_OPTION_POWER:
8013 is_power_on = B_TRUE;
8014 break;
8015 case '?':
8016 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8017 optopt);
8018 usage(B_FALSE);
8019 }
8020 }
8021
8022 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
8023 is_power_on = B_TRUE;
8024
8025 argc -= optind;
8026 argv += optind;
8027
8028 /* get pool name and check number of arguments */
8029 if (argc < 1) {
8030 (void) fprintf(stderr, gettext("missing pool name\n"));
8031 usage(B_FALSE);
8032 }
8033 if (argc < 2) {
8034 (void) fprintf(stderr, gettext("missing device name\n"));
8035 usage(B_FALSE);
8036 }
8037
8038 poolname = argv[0];
8039
8040 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
8041 (void) fprintf(stderr, gettext("failed to open pool "
8042 "\"%s\""), poolname);
8043 return (1);
8044 }
8045
8046 for (i = 1; i < argc; i++) {
8047 vdev_state_t oldstate;
8048 boolean_t avail_spare, l2cache;
8049 int rc;
8050
8051 if (is_power_on) {
8052 rc = zpool_power_on_and_disk_wait(zhp, argv[i]);
8053 if (rc == ENOTSUP) {
8054 (void) fprintf(stderr,
8055 gettext("Power control not supported\n"));
8056 }
8057 if (rc != 0)
8058 return (rc);
8059 }
8060
8061 nvlist_t *tgt = zpool_find_vdev(zhp, argv[i], &avail_spare,
8062 &l2cache, NULL);
8063 if (tgt == NULL) {
8064 ret = 1;
8065 (void) fprintf(stderr, gettext("couldn't find device "
8066 "\"%s\" in pool \"%s\"\n"), argv[i], poolname);
8067 continue;
8068 }
8069 uint_t vsc;
8070 oldstate = ((vdev_stat_t *)fnvlist_lookup_uint64_array(tgt,
8071 ZPOOL_CONFIG_VDEV_STATS, &vsc))->vs_state;
8072 if ((rc = zpool_vdev_online(zhp, argv[i], flags,
8073 &newstate)) == 0) {
8074 if (newstate != VDEV_STATE_HEALTHY) {
8075 (void) printf(gettext("warning: device '%s' "
8076 "onlined, but remains in faulted state\n"),
8077 argv[i]);
8078 if (newstate == VDEV_STATE_FAULTED)
8079 (void) printf(gettext("use 'zpool "
8080 "clear' to restore a faulted "
8081 "device\n"));
8082 else
8083 (void) printf(gettext("use 'zpool "
8084 "replace' to replace devices "
8085 "that are no longer present\n"));
8086 if ((flags & ZFS_ONLINE_EXPAND)) {
8087 (void) printf(gettext("%s: failed "
8088 "to expand usable space on "
8089 "unhealthy device '%s'\n"),
8090 (oldstate >= VDEV_STATE_DEGRADED ?
8091 "error" : "warning"), argv[i]);
8092 if (oldstate >= VDEV_STATE_DEGRADED) {
8093 ret = 1;
8094 break;
8095 }
8096 }
8097 }
8098 } else {
8099 (void) fprintf(stderr, gettext("Failed to online "
8100 "\"%s\" in pool \"%s\": %d\n"),
8101 argv[i], poolname, rc);
8102 ret = 1;
8103 }
8104 }
8105
8106 zpool_close(zhp);
8107
8108 return (ret);
8109 }
8110
8111 /*
8112 * zpool offline [-ft]|[--power] <pool> <device> ...
8113 *
8114 *
8115 * -f Force the device into a faulted state.
8116 *
8117 * -t Only take the device off-line temporarily. The offline/faulted
8118 * state will not be persistent across reboots.
8119 *
8120 * --power Power off the enclosure slot to the drive (if possible)
8121 */
8122 int
zpool_do_offline(int argc,char ** argv)8123 zpool_do_offline(int argc, char **argv)
8124 {
8125 int c, i;
8126 char *poolname;
8127 zpool_handle_t *zhp;
8128 int ret = 0;
8129 boolean_t istmp = B_FALSE;
8130 boolean_t fault = B_FALSE;
8131 boolean_t is_power_off = B_FALSE;
8132
8133 struct option long_options[] = {
8134 {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
8135 {0, 0, 0, 0}
8136 };
8137
8138 /* check options */
8139 while ((c = getopt_long(argc, argv, "ft", long_options, NULL)) != -1) {
8140 switch (c) {
8141 case 'f':
8142 fault = B_TRUE;
8143 break;
8144 case 't':
8145 istmp = B_TRUE;
8146 break;
8147 case ZPOOL_OPTION_POWER:
8148 is_power_off = B_TRUE;
8149 break;
8150 case '?':
8151 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8152 optopt);
8153 usage(B_FALSE);
8154 }
8155 }
8156
8157 if (is_power_off && fault) {
8158 (void) fprintf(stderr,
8159 gettext("-0 and -f cannot be used together\n"));
8160 usage(B_FALSE);
8161 }
8162
8163 if (is_power_off && istmp) {
8164 (void) fprintf(stderr,
8165 gettext("-0 and -t cannot be used together\n"));
8166 usage(B_FALSE);
8167 }
8168
8169 argc -= optind;
8170 argv += optind;
8171
8172 /* get pool name and check number of arguments */
8173 if (argc < 1) {
8174 (void) fprintf(stderr, gettext("missing pool name\n"));
8175 usage(B_FALSE);
8176 }
8177 if (argc < 2) {
8178 (void) fprintf(stderr, gettext("missing device name\n"));
8179 usage(B_FALSE);
8180 }
8181
8182 poolname = argv[0];
8183
8184 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
8185 (void) fprintf(stderr, gettext("failed to open pool "
8186 "\"%s\""), poolname);
8187 return (1);
8188 }
8189
8190 for (i = 1; i < argc; i++) {
8191 uint64_t guid = zpool_vdev_path_to_guid(zhp, argv[i]);
8192 if (is_power_off) {
8193 /*
8194 * Note: we have to power off first, then set REMOVED,
8195 * or else zpool_vdev_set_removed_state() returns
8196 * EAGAIN.
8197 */
8198 ret = zpool_power_off(zhp, argv[i]);
8199 if (ret != 0) {
8200 (void) fprintf(stderr, "%s %s %d\n",
8201 gettext("unable to power off slot for"),
8202 argv[i], ret);
8203 }
8204 (void) zpool_vdev_set_removed_state(zhp, guid,
8205 VDEV_AUX_NONE);
8206
8207 } else if (fault) {
8208 vdev_aux_t aux;
8209 if (istmp == B_FALSE) {
8210 /* Force the fault to persist across imports */
8211 aux = VDEV_AUX_EXTERNAL_PERSIST;
8212 } else {
8213 aux = VDEV_AUX_EXTERNAL;
8214 }
8215
8216 if (guid == 0 || zpool_vdev_fault(zhp, guid, aux) != 0)
8217 ret = 1;
8218 } else {
8219 if (zpool_vdev_offline(zhp, argv[i], istmp) != 0)
8220 ret = 1;
8221 }
8222 }
8223
8224 zpool_close(zhp);
8225
8226 return (ret);
8227 }
8228
8229 /*
8230 * zpool clear [-nF]|[--power] <pool> [device]
8231 *
8232 * Clear all errors associated with a pool or a particular device.
8233 */
8234 int
zpool_do_clear(int argc,char ** argv)8235 zpool_do_clear(int argc, char **argv)
8236 {
8237 int c;
8238 int ret = 0;
8239 boolean_t dryrun = B_FALSE;
8240 boolean_t do_rewind = B_FALSE;
8241 boolean_t xtreme_rewind = B_FALSE;
8242 boolean_t is_power_on = B_FALSE;
8243 uint32_t rewind_policy = ZPOOL_NO_REWIND;
8244 nvlist_t *policy = NULL;
8245 zpool_handle_t *zhp;
8246 char *pool, *device;
8247
8248 struct option long_options[] = {
8249 {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
8250 {0, 0, 0, 0}
8251 };
8252
8253 /* check options */
8254 while ((c = getopt_long(argc, argv, "FnX", long_options,
8255 NULL)) != -1) {
8256 switch (c) {
8257 case 'F':
8258 do_rewind = B_TRUE;
8259 break;
8260 case 'n':
8261 dryrun = B_TRUE;
8262 break;
8263 case 'X':
8264 xtreme_rewind = B_TRUE;
8265 break;
8266 case ZPOOL_OPTION_POWER:
8267 is_power_on = B_TRUE;
8268 break;
8269 case '?':
8270 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8271 optopt);
8272 usage(B_FALSE);
8273 }
8274 }
8275
8276 if (libzfs_envvar_is_set("ZPOOL_AUTO_POWER_ON_SLOT"))
8277 is_power_on = B_TRUE;
8278
8279 argc -= optind;
8280 argv += optind;
8281
8282 if (argc < 1) {
8283 (void) fprintf(stderr, gettext("missing pool name\n"));
8284 usage(B_FALSE);
8285 }
8286
8287 if (argc > 2) {
8288 (void) fprintf(stderr, gettext("too many arguments\n"));
8289 usage(B_FALSE);
8290 }
8291
8292 if ((dryrun || xtreme_rewind) && !do_rewind) {
8293 (void) fprintf(stderr,
8294 gettext("-n or -X only meaningful with -F\n"));
8295 usage(B_FALSE);
8296 }
8297 if (dryrun)
8298 rewind_policy = ZPOOL_TRY_REWIND;
8299 else if (do_rewind)
8300 rewind_policy = ZPOOL_DO_REWIND;
8301 if (xtreme_rewind)
8302 rewind_policy |= ZPOOL_EXTREME_REWIND;
8303
8304 /* In future, further rewind policy choices can be passed along here */
8305 if (nvlist_alloc(&policy, NV_UNIQUE_NAME, 0) != 0 ||
8306 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY,
8307 rewind_policy) != 0) {
8308 return (1);
8309 }
8310
8311 pool = argv[0];
8312 device = argc == 2 ? argv[1] : NULL;
8313
8314 if ((zhp = zpool_open_canfail(g_zfs, pool)) == NULL) {
8315 nvlist_free(policy);
8316 return (1);
8317 }
8318
8319 if (is_power_on) {
8320 if (device == NULL) {
8321 (void) zpool_power_on_pool_and_wait_for_devices(zhp);
8322 } else {
8323 (void) zpool_power_on_and_disk_wait(zhp, device);
8324 }
8325 }
8326
8327 if (zpool_clear(zhp, device, policy) != 0)
8328 ret = 1;
8329
8330 zpool_close(zhp);
8331
8332 nvlist_free(policy);
8333
8334 return (ret);
8335 }
8336
8337 /*
8338 * zpool reguid [-g <guid>] <pool>
8339 */
8340 int
zpool_do_reguid(int argc,char ** argv)8341 zpool_do_reguid(int argc, char **argv)
8342 {
8343 uint64_t guid;
8344 uint64_t *guidp = NULL;
8345 int c;
8346 char *endptr;
8347 char *poolname;
8348 zpool_handle_t *zhp;
8349 int ret = 0;
8350
8351 /* check options */
8352 while ((c = getopt(argc, argv, "g:")) != -1) {
8353 switch (c) {
8354 case 'g':
8355 errno = 0;
8356 guid = strtoull(optarg, &endptr, 10);
8357 if (errno != 0 || *endptr != '\0') {
8358 (void) fprintf(stderr,
8359 gettext("invalid GUID: %s\n"), optarg);
8360 usage(B_FALSE);
8361 }
8362 guidp = &guid;
8363 break;
8364 case '?':
8365 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8366 optopt);
8367 usage(B_FALSE);
8368 }
8369 }
8370
8371 argc -= optind;
8372 argv += optind;
8373
8374 /* get pool name and check number of arguments */
8375 if (argc < 1) {
8376 (void) fprintf(stderr, gettext("missing pool name\n"));
8377 usage(B_FALSE);
8378 }
8379
8380 if (argc > 1) {
8381 (void) fprintf(stderr, gettext("too many arguments\n"));
8382 usage(B_FALSE);
8383 }
8384
8385 poolname = argv[0];
8386 if ((zhp = zpool_open(g_zfs, poolname)) == NULL)
8387 return (1);
8388
8389 ret = zpool_set_guid(zhp, guidp);
8390
8391 zpool_close(zhp);
8392 return (ret);
8393 }
8394
8395
8396 /*
8397 * zpool reopen <pool>
8398 *
8399 * Reopen the pool so that the kernel can update the sizes of all vdevs.
8400 */
8401 int
zpool_do_reopen(int argc,char ** argv)8402 zpool_do_reopen(int argc, char **argv)
8403 {
8404 int c;
8405 int ret = 0;
8406 boolean_t scrub_restart = B_TRUE;
8407
8408 /* check options */
8409 while ((c = getopt(argc, argv, "n")) != -1) {
8410 switch (c) {
8411 case 'n':
8412 scrub_restart = B_FALSE;
8413 break;
8414 case '?':
8415 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8416 optopt);
8417 usage(B_FALSE);
8418 }
8419 }
8420
8421 argc -= optind;
8422 argv += optind;
8423
8424 /* if argc == 0 we will execute zpool_reopen_one on all pools */
8425 ret = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8426 B_FALSE, zpool_reopen_one, &scrub_restart);
8427
8428 return (ret);
8429 }
8430
8431 typedef struct scrub_cbdata {
8432 int cb_type;
8433 pool_scrub_cmd_t cb_scrub_cmd;
8434 time_t cb_date_start;
8435 time_t cb_date_end;
8436 } scrub_cbdata_t;
8437
8438 static boolean_t
zpool_has_checkpoint(zpool_handle_t * zhp)8439 zpool_has_checkpoint(zpool_handle_t *zhp)
8440 {
8441 nvlist_t *config, *nvroot;
8442
8443 config = zpool_get_config(zhp, NULL);
8444
8445 if (config != NULL) {
8446 pool_checkpoint_stat_t *pcs = NULL;
8447 uint_t c;
8448
8449 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
8450 (void) nvlist_lookup_uint64_array(nvroot,
8451 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
8452
8453 if (pcs == NULL || pcs->pcs_state == CS_NONE)
8454 return (B_FALSE);
8455
8456 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS ||
8457 pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
8458 return (B_TRUE);
8459 }
8460
8461 return (B_FALSE);
8462 }
8463
8464 static int
scrub_callback(zpool_handle_t * zhp,void * data)8465 scrub_callback(zpool_handle_t *zhp, void *data)
8466 {
8467 scrub_cbdata_t *cb = data;
8468 int err;
8469
8470 /*
8471 * Ignore faulted pools.
8472 */
8473 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
8474 (void) fprintf(stderr, gettext("cannot scan '%s': pool is "
8475 "currently unavailable\n"), zpool_get_name(zhp));
8476 return (1);
8477 }
8478
8479 err = zpool_scan_range(zhp, cb->cb_type, cb->cb_scrub_cmd,
8480 cb->cb_date_start, cb->cb_date_end);
8481 if (err == 0 && zpool_has_checkpoint(zhp) &&
8482 cb->cb_type == POOL_SCAN_SCRUB) {
8483 (void) printf(gettext("warning: will not scrub state that "
8484 "belongs to the checkpoint of pool '%s'\n"),
8485 zpool_get_name(zhp));
8486 }
8487
8488 return (err != 0);
8489 }
8490
8491 static int
wait_callback(zpool_handle_t * zhp,void * data)8492 wait_callback(zpool_handle_t *zhp, void *data)
8493 {
8494 zpool_wait_activity_t *act = data;
8495 return (zpool_wait(zhp, *act));
8496 }
8497
8498 static time_t
date_string_to_sec(const char * timestr,boolean_t rounding)8499 date_string_to_sec(const char *timestr, boolean_t rounding)
8500 {
8501 struct tm tm = {0};
8502 int adjustment = rounding ? 1 : 0;
8503
8504 /* Allow mktime to determine timezone. */
8505 tm.tm_isdst = -1;
8506
8507 if (strptime(timestr, "%Y-%m-%d %H:%M", &tm) == NULL) {
8508 if (strptime(timestr, "%Y-%m-%d", &tm) == NULL) {
8509 fprintf(stderr, gettext("Failed to parse the date.\n"));
8510 usage(B_FALSE);
8511 }
8512 adjustment *= 24 * 60 * 60;
8513 } else {
8514 adjustment *= 60;
8515 }
8516
8517 return (mktime(&tm) + adjustment);
8518 }
8519
8520 /*
8521 * zpool scrub [-e | -s | -p | -C | -E | -S] [-w] [-a | <pool> ...]
8522 *
8523 * -a Scrub all pools.
8524 * -e Only scrub blocks in the error log.
8525 * -E End date of scrub.
8526 * -S Start date of scrub.
8527 * -s Stop. Stops any in-progress scrub.
8528 * -p Pause. Pause in-progress scrub.
8529 * -w Wait. Blocks until scrub has completed.
8530 * -C Scrub from last saved txg.
8531 */
8532 int
zpool_do_scrub(int argc,char ** argv)8533 zpool_do_scrub(int argc, char **argv)
8534 {
8535 int c;
8536 scrub_cbdata_t cb;
8537 boolean_t wait = B_FALSE;
8538 int error;
8539
8540 cb.cb_type = POOL_SCAN_SCRUB;
8541 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
8542 cb.cb_date_start = cb.cb_date_end = 0;
8543
8544 boolean_t is_error_scrub = B_FALSE;
8545 boolean_t is_pause = B_FALSE;
8546 boolean_t is_stop = B_FALSE;
8547 boolean_t is_txg_continue = B_FALSE;
8548 boolean_t scrub_all = B_FALSE;
8549
8550 /* check options */
8551 while ((c = getopt(argc, argv, "aspweCE:S:")) != -1) {
8552 switch (c) {
8553 case 'a':
8554 scrub_all = B_TRUE;
8555 break;
8556 case 'e':
8557 is_error_scrub = B_TRUE;
8558 break;
8559 case 'E':
8560 /*
8561 * Round the date. It's better to scrub more data than
8562 * less. This also makes the date inclusive.
8563 */
8564 cb.cb_date_end = date_string_to_sec(optarg, B_TRUE);
8565 break;
8566 case 's':
8567 is_stop = B_TRUE;
8568 break;
8569 case 'S':
8570 cb.cb_date_start = date_string_to_sec(optarg, B_FALSE);
8571 break;
8572 case 'p':
8573 is_pause = B_TRUE;
8574 break;
8575 case 'w':
8576 wait = B_TRUE;
8577 break;
8578 case 'C':
8579 is_txg_continue = B_TRUE;
8580 break;
8581 case '?':
8582 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8583 optopt);
8584 usage(B_FALSE);
8585 }
8586 }
8587
8588 if (is_pause && is_stop) {
8589 (void) fprintf(stderr, gettext("invalid option "
8590 "combination: -s and -p are mutually exclusive\n"));
8591 usage(B_FALSE);
8592 } else if (is_pause && is_txg_continue) {
8593 (void) fprintf(stderr, gettext("invalid option "
8594 "combination: -p and -C are mutually exclusive\n"));
8595 usage(B_FALSE);
8596 } else if (is_stop && is_txg_continue) {
8597 (void) fprintf(stderr, gettext("invalid option "
8598 "combination: -s and -C are mutually exclusive\n"));
8599 usage(B_FALSE);
8600 } else if (is_error_scrub && is_txg_continue) {
8601 (void) fprintf(stderr, gettext("invalid option "
8602 "combination: -e and -C are mutually exclusive\n"));
8603 usage(B_FALSE);
8604 } else {
8605 if (is_error_scrub)
8606 cb.cb_type = POOL_SCAN_ERRORSCRUB;
8607
8608 if (is_pause) {
8609 cb.cb_scrub_cmd = POOL_SCRUB_PAUSE;
8610 } else if (is_stop) {
8611 cb.cb_type = POOL_SCAN_NONE;
8612 } else if (is_txg_continue) {
8613 cb.cb_scrub_cmd = POOL_SCRUB_FROM_LAST_TXG;
8614 } else {
8615 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
8616 }
8617 }
8618
8619 if ((cb.cb_date_start != 0 || cb.cb_date_end != 0) &&
8620 cb.cb_scrub_cmd != POOL_SCRUB_NORMAL) {
8621 (void) fprintf(stderr, gettext("invalid option combination: "
8622 "start/end date is available only with normal scrub\n"));
8623 usage(B_FALSE);
8624 }
8625 if (cb.cb_date_start != 0 && cb.cb_date_end != 0 &&
8626 cb.cb_date_start > cb.cb_date_end) {
8627 (void) fprintf(stderr, gettext("invalid arguments: "
8628 "end date has to be later than start date\n"));
8629 usage(B_FALSE);
8630 }
8631
8632 if (wait && (cb.cb_type == POOL_SCAN_NONE ||
8633 cb.cb_scrub_cmd == POOL_SCRUB_PAUSE)) {
8634 (void) fprintf(stderr, gettext("invalid option combination: "
8635 "-w cannot be used with -p or -s\n"));
8636 usage(B_FALSE);
8637 }
8638
8639 argc -= optind;
8640 argv += optind;
8641
8642 if (argc < 1 && !scrub_all) {
8643 (void) fprintf(stderr, gettext("missing pool name argument\n"));
8644 usage(B_FALSE);
8645 }
8646
8647 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8648 B_FALSE, scrub_callback, &cb);
8649
8650 if (wait && !error) {
8651 zpool_wait_activity_t act = ZPOOL_WAIT_SCRUB;
8652 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8653 B_FALSE, wait_callback, &act);
8654 }
8655
8656 return (error);
8657 }
8658
8659 /*
8660 * zpool resilver <pool> ...
8661 *
8662 * Restarts any in-progress resilver
8663 */
8664 int
zpool_do_resilver(int argc,char ** argv)8665 zpool_do_resilver(int argc, char **argv)
8666 {
8667 int c;
8668 scrub_cbdata_t cb;
8669
8670 cb.cb_type = POOL_SCAN_RESILVER;
8671 cb.cb_scrub_cmd = POOL_SCRUB_NORMAL;
8672 cb.cb_date_start = cb.cb_date_end = 0;
8673
8674 /* check options */
8675 while ((c = getopt(argc, argv, "")) != -1) {
8676 switch (c) {
8677 case '?':
8678 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
8679 optopt);
8680 usage(B_FALSE);
8681 }
8682 }
8683
8684 argc -= optind;
8685 argv += optind;
8686
8687 if (argc < 1) {
8688 (void) fprintf(stderr, gettext("missing pool name argument\n"));
8689 usage(B_FALSE);
8690 }
8691
8692 return (for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8693 B_FALSE, scrub_callback, &cb));
8694 }
8695
8696 /*
8697 * zpool trim [-d] [-r <rate>] [-c | -s] <-a | pool> [<device> ...]
8698 *
8699 * -a Trim all pools.
8700 * -c Cancel. Ends any in-progress trim.
8701 * -d Secure trim. Requires kernel and device support.
8702 * -r <rate> Sets the TRIM rate in bytes (per second). Supports
8703 * adding a multiplier suffix such as 'k' or 'm'.
8704 * -s Suspend. TRIM can then be restarted with no flags.
8705 * -w Wait. Blocks until trimming has completed.
8706 */
8707 int
zpool_do_trim(int argc,char ** argv)8708 zpool_do_trim(int argc, char **argv)
8709 {
8710 struct option long_options[] = {
8711 {"cancel", no_argument, NULL, 'c'},
8712 {"secure", no_argument, NULL, 'd'},
8713 {"rate", required_argument, NULL, 'r'},
8714 {"suspend", no_argument, NULL, 's'},
8715 {"wait", no_argument, NULL, 'w'},
8716 {"all", no_argument, NULL, 'a'},
8717 {0, 0, 0, 0}
8718 };
8719
8720 pool_trim_func_t cmd_type = POOL_TRIM_START;
8721 uint64_t rate = 0;
8722 boolean_t secure = B_FALSE;
8723 boolean_t wait = B_FALSE;
8724 boolean_t trimall = B_FALSE;
8725 int error;
8726
8727 int c;
8728 while ((c = getopt_long(argc, argv, "acdr:sw", long_options, NULL))
8729 != -1) {
8730 switch (c) {
8731 case 'a':
8732 trimall = B_TRUE;
8733 break;
8734 case 'c':
8735 if (cmd_type != POOL_TRIM_START &&
8736 cmd_type != POOL_TRIM_CANCEL) {
8737 (void) fprintf(stderr, gettext("-c cannot be "
8738 "combined with other options\n"));
8739 usage(B_FALSE);
8740 }
8741 cmd_type = POOL_TRIM_CANCEL;
8742 break;
8743 case 'd':
8744 if (cmd_type != POOL_TRIM_START) {
8745 (void) fprintf(stderr, gettext("-d cannot be "
8746 "combined with the -c or -s options\n"));
8747 usage(B_FALSE);
8748 }
8749 secure = B_TRUE;
8750 break;
8751 case 'r':
8752 if (cmd_type != POOL_TRIM_START) {
8753 (void) fprintf(stderr, gettext("-r cannot be "
8754 "combined with the -c or -s options\n"));
8755 usage(B_FALSE);
8756 }
8757 if (zfs_nicestrtonum(g_zfs, optarg, &rate) == -1) {
8758 (void) fprintf(stderr, "%s: %s\n",
8759 gettext("invalid value for rate"),
8760 libzfs_error_description(g_zfs));
8761 usage(B_FALSE);
8762 }
8763 break;
8764 case 's':
8765 if (cmd_type != POOL_TRIM_START &&
8766 cmd_type != POOL_TRIM_SUSPEND) {
8767 (void) fprintf(stderr, gettext("-s cannot be "
8768 "combined with other options\n"));
8769 usage(B_FALSE);
8770 }
8771 cmd_type = POOL_TRIM_SUSPEND;
8772 break;
8773 case 'w':
8774 wait = B_TRUE;
8775 break;
8776 case '?':
8777 if (optopt != 0) {
8778 (void) fprintf(stderr,
8779 gettext("invalid option '%c'\n"), optopt);
8780 } else {
8781 (void) fprintf(stderr,
8782 gettext("invalid option '%s'\n"),
8783 argv[optind - 1]);
8784 }
8785 usage(B_FALSE);
8786 }
8787 }
8788
8789 argc -= optind;
8790 argv += optind;
8791
8792 trimflags_t trim_flags = {
8793 .secure = secure,
8794 .rate = rate,
8795 .wait = wait,
8796 };
8797
8798 trim_cbdata_t cbdata = {
8799 .trim_flags = trim_flags,
8800 .cmd_type = cmd_type
8801 };
8802
8803 if (argc < 1 && !trimall) {
8804 (void) fprintf(stderr, gettext("missing pool name argument\n"));
8805 usage(B_FALSE);
8806 }
8807
8808 if (wait && (cmd_type != POOL_TRIM_START)) {
8809 (void) fprintf(stderr, gettext("-w cannot be used with -c or "
8810 "-s options\n"));
8811 usage(B_FALSE);
8812 }
8813
8814 if (trimall && argc > 0) {
8815 (void) fprintf(stderr, gettext("-a cannot be combined with "
8816 "individual zpools or vdevs\n"));
8817 usage(B_FALSE);
8818 }
8819
8820 if (argc == 0 && trimall) {
8821 cbdata.trim_flags.fullpool = B_TRUE;
8822 /* Trim each pool recursively */
8823 error = for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
8824 B_FALSE, zpool_trim_one, &cbdata);
8825 } else if (argc == 1) {
8826 char *poolname = argv[0];
8827 zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
8828 if (zhp == NULL)
8829 return (-1);
8830 /* no individual leaf vdevs specified, so add them all */
8831 error = zpool_trim_one(zhp, &cbdata);
8832 zpool_close(zhp);
8833 } else {
8834 char *poolname = argv[0];
8835 zpool_handle_t *zhp = zpool_open(g_zfs, poolname);
8836 if (zhp == NULL)
8837 return (-1);
8838 /* leaf vdevs specified, trim only those */
8839 cbdata.trim_flags.fullpool = B_FALSE;
8840 nvlist_t *vdevs = fnvlist_alloc();
8841 for (int i = 1; i < argc; i++) {
8842 fnvlist_add_boolean(vdevs, argv[i]);
8843 }
8844 error = zpool_trim(zhp, cbdata.cmd_type, vdevs,
8845 &cbdata.trim_flags);
8846 fnvlist_free(vdevs);
8847 zpool_close(zhp);
8848 }
8849
8850 return (error);
8851 }
8852
8853 /*
8854 * Converts a total number of seconds to a human readable string broken
8855 * down in to days/hours/minutes/seconds.
8856 */
8857 static void
secs_to_dhms(uint64_t total,char * buf)8858 secs_to_dhms(uint64_t total, char *buf)
8859 {
8860 uint64_t days = total / 60 / 60 / 24;
8861 uint64_t hours = (total / 60 / 60) % 24;
8862 uint64_t mins = (total / 60) % 60;
8863 uint64_t secs = (total % 60);
8864
8865 if (days > 0) {
8866 (void) sprintf(buf, "%llu days %02llu:%02llu:%02llu",
8867 (u_longlong_t)days, (u_longlong_t)hours,
8868 (u_longlong_t)mins, (u_longlong_t)secs);
8869 } else {
8870 (void) sprintf(buf, "%02llu:%02llu:%02llu",
8871 (u_longlong_t)hours, (u_longlong_t)mins,
8872 (u_longlong_t)secs);
8873 }
8874 }
8875
8876 /*
8877 * Print out detailed error scrub status.
8878 */
8879 static void
print_err_scrub_status(pool_scan_stat_t * ps)8880 print_err_scrub_status(pool_scan_stat_t *ps)
8881 {
8882 time_t start, end, pause;
8883 uint64_t total_secs_left;
8884 uint64_t secs_left, mins_left, hours_left, days_left;
8885 uint64_t examined, to_be_examined;
8886
8887 if (ps == NULL || ps->pss_error_scrub_func != POOL_SCAN_ERRORSCRUB) {
8888 return;
8889 }
8890
8891 (void) printf(gettext(" scrub: "));
8892
8893 start = ps->pss_error_scrub_start;
8894 end = ps->pss_error_scrub_end;
8895 pause = ps->pss_pass_error_scrub_pause;
8896 examined = ps->pss_error_scrub_examined;
8897 to_be_examined = ps->pss_error_scrub_to_be_examined;
8898
8899 assert(ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB);
8900
8901 if (ps->pss_error_scrub_state == DSS_FINISHED) {
8902 total_secs_left = end - start;
8903 days_left = total_secs_left / 60 / 60 / 24;
8904 hours_left = (total_secs_left / 60 / 60) % 24;
8905 mins_left = (total_secs_left / 60) % 60;
8906 secs_left = (total_secs_left % 60);
8907
8908 (void) printf(gettext("scrubbed %llu error blocks in %llu days "
8909 "%02llu:%02llu:%02llu on %s"), (u_longlong_t)examined,
8910 (u_longlong_t)days_left, (u_longlong_t)hours_left,
8911 (u_longlong_t)mins_left, (u_longlong_t)secs_left,
8912 ctime(&end));
8913
8914 return;
8915 } else if (ps->pss_error_scrub_state == DSS_CANCELED) {
8916 (void) printf(gettext("error scrub canceled on %s"),
8917 ctime(&end));
8918 return;
8919 }
8920 assert(ps->pss_error_scrub_state == DSS_ERRORSCRUBBING);
8921
8922 /* Error scrub is in progress. */
8923 if (pause == 0) {
8924 (void) printf(gettext("error scrub in progress since %s"),
8925 ctime(&start));
8926 } else {
8927 (void) printf(gettext("error scrub paused since %s"),
8928 ctime(&pause));
8929 (void) printf(gettext("\terror scrub started on %s"),
8930 ctime(&start));
8931 }
8932
8933 double fraction_done = (double)examined / (to_be_examined + examined);
8934 (void) printf(gettext("\t%.2f%% done, issued I/O for %llu error"
8935 " blocks"), 100 * fraction_done, (u_longlong_t)examined);
8936
8937 (void) printf("\n");
8938 }
8939
8940 /*
8941 * Print out detailed scrub status.
8942 */
8943 static void
print_scan_scrub_resilver_status(pool_scan_stat_t * ps)8944 print_scan_scrub_resilver_status(pool_scan_stat_t *ps)
8945 {
8946 time_t start, end, pause;
8947 uint64_t pass_scanned, scanned, pass_issued, issued, total_s, total_i;
8948 uint64_t elapsed, scan_rate, issue_rate;
8949 double fraction_done;
8950 char processed_buf[7], scanned_buf[7], issued_buf[7], total_s_buf[7];
8951 char total_i_buf[7], srate_buf[7], irate_buf[7], time_buf[32];
8952
8953 printf(" ");
8954 (void) printf_color(ANSI_BOLD, gettext("scan:"));
8955 printf(" ");
8956
8957 /* If there's never been a scan, there's not much to say. */
8958 if (ps == NULL || ps->pss_func == POOL_SCAN_NONE ||
8959 ps->pss_func >= POOL_SCAN_FUNCS) {
8960 (void) printf(gettext("none requested\n"));
8961 return;
8962 }
8963
8964 start = ps->pss_start_time;
8965 end = ps->pss_end_time;
8966 pause = ps->pss_pass_scrub_pause;
8967
8968 zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
8969
8970 int is_resilver = ps->pss_func == POOL_SCAN_RESILVER;
8971 int is_scrub = ps->pss_func == POOL_SCAN_SCRUB;
8972 assert(is_resilver || is_scrub);
8973
8974 /* Scan is finished or canceled. */
8975 if (ps->pss_state == DSS_FINISHED) {
8976 secs_to_dhms(end - start, time_buf);
8977
8978 if (is_scrub) {
8979 (void) printf(gettext("scrub repaired %s "
8980 "in %s with %llu errors on %s"), processed_buf,
8981 time_buf, (u_longlong_t)ps->pss_errors,
8982 ctime(&end));
8983 } else if (is_resilver) {
8984 (void) printf(gettext("resilvered %s "
8985 "in %s with %llu errors on %s"), processed_buf,
8986 time_buf, (u_longlong_t)ps->pss_errors,
8987 ctime(&end));
8988 }
8989 return;
8990 } else if (ps->pss_state == DSS_CANCELED) {
8991 if (is_scrub) {
8992 (void) printf(gettext("scrub canceled on %s"),
8993 ctime(&end));
8994 } else if (is_resilver) {
8995 (void) printf(gettext("resilver canceled on %s"),
8996 ctime(&end));
8997 }
8998 return;
8999 }
9000
9001 assert(ps->pss_state == DSS_SCANNING);
9002
9003 /* Scan is in progress. Resilvers can't be paused. */
9004 if (is_scrub) {
9005 if (pause == 0) {
9006 (void) printf(gettext("scrub in progress since %s"),
9007 ctime(&start));
9008 } else {
9009 (void) printf(gettext("scrub paused since %s"),
9010 ctime(&pause));
9011 (void) printf(gettext("\tscrub started on %s"),
9012 ctime(&start));
9013 }
9014 } else if (is_resilver) {
9015 (void) printf(gettext("resilver in progress since %s"),
9016 ctime(&start));
9017 }
9018
9019 scanned = ps->pss_examined;
9020 pass_scanned = ps->pss_pass_exam;
9021 issued = ps->pss_issued;
9022 pass_issued = ps->pss_pass_issued;
9023 total_s = ps->pss_to_examine;
9024 total_i = ps->pss_to_examine - ps->pss_skipped;
9025
9026 /* we are only done with a block once we have issued the IO for it */
9027 fraction_done = (double)issued / total_i;
9028
9029 /* elapsed time for this pass, rounding up to 1 if it's 0 */
9030 elapsed = time(NULL) - ps->pss_pass_start;
9031 elapsed -= ps->pss_pass_scrub_spent_paused;
9032 elapsed = (elapsed != 0) ? elapsed : 1;
9033
9034 scan_rate = pass_scanned / elapsed;
9035 issue_rate = pass_issued / elapsed;
9036
9037 /* format all of the numbers we will be reporting */
9038 zfs_nicebytes(scanned, scanned_buf, sizeof (scanned_buf));
9039 zfs_nicebytes(issued, issued_buf, sizeof (issued_buf));
9040 zfs_nicebytes(total_s, total_s_buf, sizeof (total_s_buf));
9041 zfs_nicebytes(total_i, total_i_buf, sizeof (total_i_buf));
9042
9043 /* do not print estimated time if we have a paused scrub */
9044 (void) printf(gettext("\t%s / %s scanned"), scanned_buf, total_s_buf);
9045 if (pause == 0 && scan_rate > 0) {
9046 zfs_nicebytes(scan_rate, srate_buf, sizeof (srate_buf));
9047 (void) printf(gettext(" at %s/s"), srate_buf);
9048 }
9049 (void) printf(gettext(", %s / %s issued"), issued_buf, total_i_buf);
9050 if (pause == 0 && issue_rate > 0) {
9051 zfs_nicebytes(issue_rate, irate_buf, sizeof (irate_buf));
9052 (void) printf(gettext(" at %s/s"), irate_buf);
9053 }
9054 (void) printf(gettext("\n"));
9055
9056 if (is_resilver) {
9057 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
9058 processed_buf, 100 * fraction_done);
9059 } else if (is_scrub) {
9060 (void) printf(gettext("\t%s repaired, %.2f%% done"),
9061 processed_buf, 100 * fraction_done);
9062 }
9063
9064 if (pause == 0) {
9065 /*
9066 * Only provide an estimate iff:
9067 * 1) we haven't yet issued all we expected, and
9068 * 2) the issue rate exceeds 10 MB/s, and
9069 * 3) it's either:
9070 * a) a resilver which has started repairs, or
9071 * b) a scrub which has entered the issue phase.
9072 */
9073 if (total_i >= issued && issue_rate >= 10 * 1024 * 1024 &&
9074 ((is_resilver && ps->pss_processed > 0) ||
9075 (is_scrub && issued > 0))) {
9076 secs_to_dhms((total_i - issued) / issue_rate, time_buf);
9077 (void) printf(gettext(", %s to go\n"), time_buf);
9078 } else {
9079 (void) printf(gettext(", no estimated "
9080 "completion time\n"));
9081 }
9082 } else {
9083 (void) printf(gettext("\n"));
9084 }
9085 }
9086
9087 static void
print_rebuild_status_impl(vdev_rebuild_stat_t * vrs,uint_t c,char * vdev_name)9088 print_rebuild_status_impl(vdev_rebuild_stat_t *vrs, uint_t c, char *vdev_name)
9089 {
9090 if (vrs == NULL || vrs->vrs_state == VDEV_REBUILD_NONE)
9091 return;
9092
9093 printf(" ");
9094 (void) printf_color(ANSI_BOLD, gettext("scan:"));
9095 printf(" ");
9096
9097 uint64_t bytes_scanned = vrs->vrs_bytes_scanned;
9098 uint64_t bytes_issued = vrs->vrs_bytes_issued;
9099 uint64_t bytes_rebuilt = vrs->vrs_bytes_rebuilt;
9100 uint64_t bytes_est_s = vrs->vrs_bytes_est;
9101 uint64_t bytes_est_i = vrs->vrs_bytes_est;
9102 if (c > offsetof(vdev_rebuild_stat_t, vrs_pass_bytes_skipped) / 8)
9103 bytes_est_i -= vrs->vrs_pass_bytes_skipped;
9104 uint64_t scan_rate = (vrs->vrs_pass_bytes_scanned /
9105 (vrs->vrs_pass_time_ms + 1)) * 1000;
9106 uint64_t issue_rate = (vrs->vrs_pass_bytes_issued /
9107 (vrs->vrs_pass_time_ms + 1)) * 1000;
9108 double scan_pct = MIN((double)bytes_scanned * 100 /
9109 (bytes_est_s + 1), 100);
9110
9111 /* Format all of the numbers we will be reporting */
9112 char bytes_scanned_buf[7], bytes_issued_buf[7];
9113 char bytes_rebuilt_buf[7], bytes_est_s_buf[7], bytes_est_i_buf[7];
9114 char scan_rate_buf[7], issue_rate_buf[7], time_buf[32];
9115 zfs_nicebytes(bytes_scanned, bytes_scanned_buf,
9116 sizeof (bytes_scanned_buf));
9117 zfs_nicebytes(bytes_issued, bytes_issued_buf,
9118 sizeof (bytes_issued_buf));
9119 zfs_nicebytes(bytes_rebuilt, bytes_rebuilt_buf,
9120 sizeof (bytes_rebuilt_buf));
9121 zfs_nicebytes(bytes_est_s, bytes_est_s_buf, sizeof (bytes_est_s_buf));
9122 zfs_nicebytes(bytes_est_i, bytes_est_i_buf, sizeof (bytes_est_i_buf));
9123
9124 time_t start = vrs->vrs_start_time;
9125 time_t end = vrs->vrs_end_time;
9126
9127 /* Rebuild is finished or canceled. */
9128 if (vrs->vrs_state == VDEV_REBUILD_COMPLETE) {
9129 secs_to_dhms(vrs->vrs_scan_time_ms / 1000, time_buf);
9130 (void) printf(gettext("resilvered (%s) %s in %s "
9131 "with %llu errors on %s"), vdev_name, bytes_rebuilt_buf,
9132 time_buf, (u_longlong_t)vrs->vrs_errors, ctime(&end));
9133 return;
9134 } else if (vrs->vrs_state == VDEV_REBUILD_CANCELED) {
9135 (void) printf(gettext("resilver (%s) canceled on %s"),
9136 vdev_name, ctime(&end));
9137 return;
9138 } else if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
9139 (void) printf(gettext("resilver (%s) in progress since %s"),
9140 vdev_name, ctime(&start));
9141 }
9142
9143 assert(vrs->vrs_state == VDEV_REBUILD_ACTIVE);
9144
9145 (void) printf(gettext("\t%s / %s scanned"), bytes_scanned_buf,
9146 bytes_est_s_buf);
9147 if (scan_rate > 0) {
9148 zfs_nicebytes(scan_rate, scan_rate_buf, sizeof (scan_rate_buf));
9149 (void) printf(gettext(" at %s/s"), scan_rate_buf);
9150 }
9151 (void) printf(gettext(", %s / %s issued"), bytes_issued_buf,
9152 bytes_est_i_buf);
9153 if (issue_rate > 0) {
9154 zfs_nicebytes(issue_rate, issue_rate_buf,
9155 sizeof (issue_rate_buf));
9156 (void) printf(gettext(" at %s/s"), issue_rate_buf);
9157 }
9158 (void) printf(gettext("\n"));
9159
9160 (void) printf(gettext("\t%s resilvered, %.2f%% done"),
9161 bytes_rebuilt_buf, scan_pct);
9162
9163 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
9164 if (bytes_est_s >= bytes_scanned &&
9165 scan_rate >= 10 * 1024 * 1024) {
9166 secs_to_dhms((bytes_est_s - bytes_scanned) / scan_rate,
9167 time_buf);
9168 (void) printf(gettext(", %s to go\n"), time_buf);
9169 } else {
9170 (void) printf(gettext(", no estimated "
9171 "completion time\n"));
9172 }
9173 } else {
9174 (void) printf(gettext("\n"));
9175 }
9176 }
9177
9178 /*
9179 * Print rebuild status for top-level vdevs.
9180 */
9181 static void
print_rebuild_status(zpool_handle_t * zhp,nvlist_t * nvroot)9182 print_rebuild_status(zpool_handle_t *zhp, nvlist_t *nvroot)
9183 {
9184 nvlist_t **child;
9185 uint_t children;
9186
9187 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
9188 &child, &children) != 0)
9189 children = 0;
9190
9191 for (uint_t c = 0; c < children; c++) {
9192 vdev_rebuild_stat_t *vrs;
9193 uint_t i;
9194
9195 if (nvlist_lookup_uint64_array(child[c],
9196 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
9197 char *name = zpool_vdev_name(g_zfs, zhp,
9198 child[c], VDEV_NAME_TYPE_ID);
9199 print_rebuild_status_impl(vrs, i, name);
9200 free(name);
9201 }
9202 }
9203 }
9204
9205 /*
9206 * As we don't scrub checkpointed blocks, we want to warn the user that we
9207 * skipped scanning some blocks if a checkpoint exists or existed at any
9208 * time during the scan. If a sequential instead of healing reconstruction
9209 * was performed then the blocks were reconstructed. However, their checksums
9210 * have not been verified so we still print the warning.
9211 */
9212 static void
print_checkpoint_scan_warning(pool_scan_stat_t * ps,pool_checkpoint_stat_t * pcs)9213 print_checkpoint_scan_warning(pool_scan_stat_t *ps, pool_checkpoint_stat_t *pcs)
9214 {
9215 if (ps == NULL || pcs == NULL)
9216 return;
9217
9218 if (pcs->pcs_state == CS_NONE ||
9219 pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
9220 return;
9221
9222 assert(pcs->pcs_state == CS_CHECKPOINT_EXISTS);
9223
9224 if (ps->pss_state == DSS_NONE)
9225 return;
9226
9227 if ((ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) &&
9228 ps->pss_end_time < pcs->pcs_start_time)
9229 return;
9230
9231 if (ps->pss_state == DSS_FINISHED || ps->pss_state == DSS_CANCELED) {
9232 (void) printf(gettext(" scan warning: skipped blocks "
9233 "that are only referenced by the checkpoint.\n"));
9234 } else {
9235 assert(ps->pss_state == DSS_SCANNING);
9236 (void) printf(gettext(" scan warning: skipping blocks "
9237 "that are only referenced by the checkpoint.\n"));
9238 }
9239 }
9240
9241 /*
9242 * Returns B_TRUE if there is an active rebuild in progress. Otherwise,
9243 * B_FALSE is returned and 'rebuild_end_time' is set to the end time for
9244 * the last completed (or cancelled) rebuild.
9245 */
9246 static boolean_t
check_rebuilding(nvlist_t * nvroot,uint64_t * rebuild_end_time)9247 check_rebuilding(nvlist_t *nvroot, uint64_t *rebuild_end_time)
9248 {
9249 nvlist_t **child;
9250 uint_t children;
9251 boolean_t rebuilding = B_FALSE;
9252 uint64_t end_time = 0;
9253
9254 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
9255 &child, &children) != 0)
9256 children = 0;
9257
9258 for (uint_t c = 0; c < children; c++) {
9259 vdev_rebuild_stat_t *vrs;
9260 uint_t i;
9261
9262 if (nvlist_lookup_uint64_array(child[c],
9263 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i) == 0) {
9264
9265 if (vrs->vrs_end_time > end_time)
9266 end_time = vrs->vrs_end_time;
9267
9268 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
9269 rebuilding = B_TRUE;
9270 end_time = 0;
9271 break;
9272 }
9273 }
9274 }
9275
9276 if (rebuild_end_time != NULL)
9277 *rebuild_end_time = end_time;
9278
9279 return (rebuilding);
9280 }
9281
9282 static void
vdev_stats_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nv,int depth,boolean_t isspare,char * parent,nvlist_t * item)9283 vdev_stats_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
9284 int depth, boolean_t isspare, char *parent, nvlist_t *item)
9285 {
9286 nvlist_t *vds, **child, *ch = NULL;
9287 uint_t vsc, children;
9288 vdev_stat_t *vs;
9289 char *vname;
9290 uint64_t notpresent;
9291 const char *type, *path;
9292
9293 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
9294 &child, &children) != 0)
9295 children = 0;
9296 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
9297 (uint64_t **)&vs, &vsc) == 0);
9298 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
9299 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0)
9300 return;
9301
9302 if (cb->cb_print_unhealthy && depth > 0 &&
9303 for_each_vdev_in_nvlist(nv, vdev_health_check_cb, cb) == 0) {
9304 return;
9305 }
9306 vname = zpool_vdev_name(g_zfs, zhp, nv,
9307 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
9308 vds = fnvlist_alloc();
9309 fill_vdev_info(vds, zhp, vname, B_FALSE, cb->cb_json_as_int);
9310 if (cb->cb_flat_vdevs && parent != NULL) {
9311 fnvlist_add_string(vds, "parent", parent);
9312 }
9313
9314 if (isspare) {
9315 if (vs->vs_aux == VDEV_AUX_SPARED) {
9316 fnvlist_add_string(vds, "state", "INUSE");
9317 used_by_other(zhp, nv, vds);
9318 } else if (vs->vs_state == VDEV_STATE_HEALTHY)
9319 fnvlist_add_string(vds, "state", "AVAIL");
9320 } else {
9321 if (vs->vs_alloc) {
9322 nice_num_str_nvlist(vds, "alloc_space", vs->vs_alloc,
9323 cb->cb_literal, cb->cb_json_as_int,
9324 ZFS_NICENUM_BYTES);
9325 }
9326 if (vs->vs_space) {
9327 nice_num_str_nvlist(vds, "total_space", vs->vs_space,
9328 cb->cb_literal, cb->cb_json_as_int,
9329 ZFS_NICENUM_BYTES);
9330 }
9331 if (vs->vs_dspace) {
9332 nice_num_str_nvlist(vds, "def_space", vs->vs_dspace,
9333 cb->cb_literal, cb->cb_json_as_int,
9334 ZFS_NICENUM_BYTES);
9335 }
9336 if (vs->vs_rsize) {
9337 nice_num_str_nvlist(vds, "rep_dev_size", vs->vs_rsize,
9338 cb->cb_literal, cb->cb_json_as_int,
9339 ZFS_NICENUM_BYTES);
9340 }
9341 if (vs->vs_esize) {
9342 nice_num_str_nvlist(vds, "ex_dev_size", vs->vs_esize,
9343 cb->cb_literal, cb->cb_json_as_int,
9344 ZFS_NICENUM_BYTES);
9345 }
9346 if (vs->vs_self_healed) {
9347 nice_num_str_nvlist(vds, "self_healed",
9348 vs->vs_self_healed, cb->cb_literal,
9349 cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9350 }
9351 if (vs->vs_pspace) {
9352 nice_num_str_nvlist(vds, "phys_space", vs->vs_pspace,
9353 cb->cb_literal, cb->cb_json_as_int,
9354 ZFS_NICENUM_BYTES);
9355 }
9356 nice_num_str_nvlist(vds, "read_errors", vs->vs_read_errors,
9357 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9358 nice_num_str_nvlist(vds, "write_errors", vs->vs_write_errors,
9359 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9360 nice_num_str_nvlist(vds, "checksum_errors",
9361 vs->vs_checksum_errors, cb->cb_literal,
9362 cb->cb_json_as_int, ZFS_NICENUM_1024);
9363 if (vs->vs_scan_processed) {
9364 nice_num_str_nvlist(vds, "scan_processed",
9365 vs->vs_scan_processed, cb->cb_literal,
9366 cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9367 }
9368 if (vs->vs_checkpoint_space) {
9369 nice_num_str_nvlist(vds, "checkpoint_space",
9370 vs->vs_checkpoint_space, cb->cb_literal,
9371 cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9372 }
9373 if (vs->vs_resilver_deferred) {
9374 nice_num_str_nvlist(vds, "resilver_deferred",
9375 vs->vs_resilver_deferred, B_TRUE,
9376 cb->cb_json_as_int, ZFS_NICENUM_1024);
9377 }
9378 if (children == 0) {
9379 nice_num_str_nvlist(vds, "slow_ios", vs->vs_slow_ios,
9380 cb->cb_literal, cb->cb_json_as_int,
9381 ZFS_NICENUM_1024);
9382 }
9383 if (cb->cb_print_power) {
9384 if (children == 0) {
9385 /* Only leaf vdevs have physical slots */
9386 switch (zpool_power_current_state(zhp, (char *)
9387 fnvlist_lookup_string(nv,
9388 ZPOOL_CONFIG_PATH))) {
9389 case 0:
9390 fnvlist_add_string(vds, "power_state",
9391 "off");
9392 break;
9393 case 1:
9394 fnvlist_add_string(vds, "power_state",
9395 "on");
9396 break;
9397 default:
9398 fnvlist_add_string(vds, "power_state",
9399 "-");
9400 }
9401 } else {
9402 fnvlist_add_string(vds, "power_state", "-");
9403 }
9404 }
9405 }
9406
9407 if (cb->cb_print_dio_verify) {
9408 nice_num_str_nvlist(vds, "dio_verify_errors",
9409 vs->vs_dio_verify_errors, cb->cb_literal,
9410 cb->cb_json_as_int, ZFS_NICENUM_1024);
9411 }
9412
9413 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
9414 ¬present) == 0) {
9415 nice_num_str_nvlist(vds, ZPOOL_CONFIG_NOT_PRESENT,
9416 1, B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9417 fnvlist_add_string(vds, "was",
9418 fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH));
9419 } else if (vs->vs_aux != VDEV_AUX_NONE) {
9420 fnvlist_add_string(vds, "aux", vdev_aux_str[vs->vs_aux]);
9421 } else if (children == 0 && !isspare &&
9422 getenv("ZPOOL_STATUS_NON_NATIVE_ASHIFT_IGNORE") == NULL &&
9423 VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
9424 vs->vs_configured_ashift < vs->vs_physical_ashift) {
9425 nice_num_str_nvlist(vds, "configured_ashift",
9426 vs->vs_configured_ashift, B_TRUE, cb->cb_json_as_int,
9427 ZFS_NICENUM_1024);
9428 nice_num_str_nvlist(vds, "physical_ashift",
9429 vs->vs_physical_ashift, B_TRUE, cb->cb_json_as_int,
9430 ZFS_NICENUM_1024);
9431 }
9432 if (vs->vs_scan_removing != 0) {
9433 nice_num_str_nvlist(vds, "removing", vs->vs_scan_removing,
9434 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024);
9435 } else if (VDEV_STAT_VALID(vs_noalloc, vsc) && vs->vs_noalloc != 0) {
9436 nice_num_str_nvlist(vds, "noalloc", vs->vs_noalloc,
9437 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024);
9438 }
9439
9440 if (cb->vcdl != NULL) {
9441 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
9442 zpool_nvlist_cmd(cb->vcdl, zpool_get_name(zhp),
9443 path, vds);
9444 }
9445 }
9446
9447 if (children == 0) {
9448 if (cb->cb_print_vdev_init) {
9449 if (vs->vs_initialize_state != 0) {
9450 uint64_t st = vs->vs_initialize_state;
9451 fnvlist_add_string(vds, "init_state",
9452 vdev_init_state_str[st]);
9453 nice_num_str_nvlist(vds, "initialized",
9454 vs->vs_initialize_bytes_done,
9455 cb->cb_literal, cb->cb_json_as_int,
9456 ZFS_NICENUM_BYTES);
9457 nice_num_str_nvlist(vds, "to_initialize",
9458 vs->vs_initialize_bytes_est,
9459 cb->cb_literal, cb->cb_json_as_int,
9460 ZFS_NICENUM_BYTES);
9461 nice_num_str_nvlist(vds, "init_time",
9462 vs->vs_initialize_action_time,
9463 cb->cb_literal, cb->cb_json_as_int,
9464 ZFS_NICE_TIMESTAMP);
9465 nice_num_str_nvlist(vds, "init_errors",
9466 vs->vs_initialize_errors,
9467 cb->cb_literal, cb->cb_json_as_int,
9468 ZFS_NICENUM_1024);
9469 } else {
9470 fnvlist_add_string(vds, "init_state",
9471 "UNINITIALIZED");
9472 }
9473 }
9474 if (cb->cb_print_vdev_trim) {
9475 if (vs->vs_trim_notsup == 0) {
9476 if (vs->vs_trim_state != 0) {
9477 uint64_t st = vs->vs_trim_state;
9478 fnvlist_add_string(vds, "trim_state",
9479 vdev_trim_state_str[st]);
9480 nice_num_str_nvlist(vds, "trimmed",
9481 vs->vs_trim_bytes_done,
9482 cb->cb_literal, cb->cb_json_as_int,
9483 ZFS_NICENUM_BYTES);
9484 nice_num_str_nvlist(vds, "to_trim",
9485 vs->vs_trim_bytes_est,
9486 cb->cb_literal, cb->cb_json_as_int,
9487 ZFS_NICENUM_BYTES);
9488 nice_num_str_nvlist(vds, "trim_time",
9489 vs->vs_trim_action_time,
9490 cb->cb_literal, cb->cb_json_as_int,
9491 ZFS_NICE_TIMESTAMP);
9492 nice_num_str_nvlist(vds, "trim_errors",
9493 vs->vs_trim_errors,
9494 cb->cb_literal, cb->cb_json_as_int,
9495 ZFS_NICENUM_1024);
9496 } else
9497 fnvlist_add_string(vds, "trim_state",
9498 "UNTRIMMED");
9499 }
9500 nice_num_str_nvlist(vds, "trim_notsup",
9501 vs->vs_trim_notsup, B_TRUE,
9502 cb->cb_json_as_int, ZFS_NICENUM_1024);
9503 }
9504 } else {
9505 ch = fnvlist_alloc();
9506 }
9507
9508 if (cb->cb_flat_vdevs && children == 0) {
9509 fnvlist_add_nvlist(item, vname, vds);
9510 }
9511
9512 for (int c = 0; c < children; c++) {
9513 uint64_t islog = B_FALSE, ishole = B_FALSE;
9514 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
9515 &islog);
9516 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
9517 &ishole);
9518 if (islog || ishole)
9519 continue;
9520 if (nvlist_exists(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS))
9521 continue;
9522 if (cb->cb_flat_vdevs) {
9523 vdev_stats_nvlist(zhp, cb, child[c], depth + 2, isspare,
9524 vname, item);
9525 }
9526 vdev_stats_nvlist(zhp, cb, child[c], depth + 2, isspare,
9527 vname, ch);
9528 }
9529
9530 if (ch != NULL) {
9531 if (!nvlist_empty(ch))
9532 fnvlist_add_nvlist(vds, "vdevs", ch);
9533 fnvlist_free(ch);
9534 }
9535 fnvlist_add_nvlist(item, vname, vds);
9536 fnvlist_free(vds);
9537 free(vname);
9538 }
9539
9540 static void
class_vdevs_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nv,const char * class,nvlist_t * item)9541 class_vdevs_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
9542 const char *class, nvlist_t *item)
9543 {
9544 uint_t c, children;
9545 nvlist_t **child;
9546 nvlist_t *class_obj = NULL;
9547
9548 if (!cb->cb_flat_vdevs)
9549 class_obj = fnvlist_alloc();
9550
9551 assert(zhp != NULL || !cb->cb_verbose);
9552
9553 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child,
9554 &children) != 0)
9555 return;
9556
9557 for (c = 0; c < children; c++) {
9558 uint64_t is_log = B_FALSE;
9559 const char *bias = NULL;
9560 const char *type = NULL;
9561 char *name = zpool_vdev_name(g_zfs, zhp, child[c],
9562 cb->cb_name_flags | VDEV_NAME_TYPE_ID);
9563
9564 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
9565 &is_log);
9566
9567 if (is_log) {
9568 bias = (char *)VDEV_ALLOC_CLASS_LOGS;
9569 } else {
9570 (void) nvlist_lookup_string(child[c],
9571 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias);
9572 (void) nvlist_lookup_string(child[c],
9573 ZPOOL_CONFIG_TYPE, &type);
9574 }
9575
9576 if (bias == NULL || strcmp(bias, class) != 0)
9577 continue;
9578 if (!is_log && strcmp(type, VDEV_TYPE_INDIRECT) == 0)
9579 continue;
9580
9581 if (cb->cb_flat_vdevs) {
9582 vdev_stats_nvlist(zhp, cb, child[c], 2, B_FALSE,
9583 NULL, item);
9584 } else {
9585 vdev_stats_nvlist(zhp, cb, child[c], 2, B_FALSE,
9586 NULL, class_obj);
9587 }
9588 free(name);
9589 }
9590 if (!cb->cb_flat_vdevs) {
9591 if (!nvlist_empty(class_obj))
9592 fnvlist_add_nvlist(item, class, class_obj);
9593 fnvlist_free(class_obj);
9594 }
9595 }
9596
9597 static void
l2cache_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nv,nvlist_t * item)9598 l2cache_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
9599 nvlist_t *item)
9600 {
9601 nvlist_t *l2c = NULL, **l2cache;
9602 uint_t nl2cache;
9603 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
9604 &l2cache, &nl2cache) == 0) {
9605 if (nl2cache == 0)
9606 return;
9607 if (!cb->cb_flat_vdevs)
9608 l2c = fnvlist_alloc();
9609 for (int i = 0; i < nl2cache; i++) {
9610 if (cb->cb_flat_vdevs) {
9611 vdev_stats_nvlist(zhp, cb, l2cache[i], 2,
9612 B_FALSE, NULL, item);
9613 } else {
9614 vdev_stats_nvlist(zhp, cb, l2cache[i], 2,
9615 B_FALSE, NULL, l2c);
9616 }
9617 }
9618 }
9619 if (!cb->cb_flat_vdevs) {
9620 if (!nvlist_empty(l2c))
9621 fnvlist_add_nvlist(item, "l2cache", l2c);
9622 fnvlist_free(l2c);
9623 }
9624 }
9625
9626 static void
spares_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nv,nvlist_t * item)9627 spares_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *nv,
9628 nvlist_t *item)
9629 {
9630 nvlist_t *sp = NULL, **spares;
9631 uint_t nspares;
9632 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
9633 &spares, &nspares) == 0) {
9634 if (nspares == 0)
9635 return;
9636 if (!cb->cb_flat_vdevs)
9637 sp = fnvlist_alloc();
9638 for (int i = 0; i < nspares; i++) {
9639 if (cb->cb_flat_vdevs) {
9640 vdev_stats_nvlist(zhp, cb, spares[i], 2, B_TRUE,
9641 NULL, item);
9642 } else {
9643 vdev_stats_nvlist(zhp, cb, spares[i], 2, B_TRUE,
9644 NULL, sp);
9645 }
9646 }
9647 }
9648 if (!cb->cb_flat_vdevs) {
9649 if (!nvlist_empty(sp))
9650 fnvlist_add_nvlist(item, "spares", sp);
9651 fnvlist_free(sp);
9652 }
9653 }
9654
9655 static void
errors_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * item)9656 errors_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *item)
9657 {
9658 uint64_t nerr;
9659 nvlist_t *config = zpool_get_config(zhp, NULL);
9660 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
9661 &nerr) == 0) {
9662 nice_num_str_nvlist(item, ZPOOL_CONFIG_ERRCOUNT, nerr,
9663 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9664 if (nerr != 0 && cb->cb_verbose) {
9665 nvlist_t *nverrlist = NULL;
9666 if (zpool_get_errlog(zhp, &nverrlist) == 0) {
9667 int i = 0;
9668 int count = 0;
9669 size_t len = MAXPATHLEN * 2;
9670 nvpair_t *elem = NULL;
9671
9672 for (nvpair_t *pair =
9673 nvlist_next_nvpair(nverrlist, NULL);
9674 pair != NULL;
9675 pair = nvlist_next_nvpair(nverrlist, pair))
9676 count++;
9677 char **errl = (char **)malloc(
9678 count * sizeof (char *));
9679
9680 while ((elem = nvlist_next_nvpair(nverrlist,
9681 elem)) != NULL) {
9682 nvlist_t *nv;
9683 uint64_t dsobj, obj;
9684
9685 verify(nvpair_value_nvlist(elem,
9686 &nv) == 0);
9687 verify(nvlist_lookup_uint64(nv,
9688 ZPOOL_ERR_DATASET, &dsobj) == 0);
9689 verify(nvlist_lookup_uint64(nv,
9690 ZPOOL_ERR_OBJECT, &obj) == 0);
9691 errl[i] = safe_malloc(len);
9692 zpool_obj_to_path(zhp, dsobj, obj,
9693 errl[i++], len);
9694 }
9695 nvlist_free(nverrlist);
9696 fnvlist_add_string_array(item, "errlist",
9697 (const char **)errl, count);
9698 for (int i = 0; i < count; ++i)
9699 free(errl[i]);
9700 free(errl);
9701 } else
9702 fnvlist_add_string(item, "errlist",
9703 strerror(errno));
9704 }
9705 }
9706 }
9707
9708 static void
ddt_stats_nvlist(ddt_stat_t * dds,status_cbdata_t * cb,nvlist_t * item)9709 ddt_stats_nvlist(ddt_stat_t *dds, status_cbdata_t *cb, nvlist_t *item)
9710 {
9711 nice_num_str_nvlist(item, "blocks", dds->dds_blocks,
9712 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9713 nice_num_str_nvlist(item, "logical_size", dds->dds_lsize,
9714 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9715 nice_num_str_nvlist(item, "physical_size", dds->dds_psize,
9716 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9717 nice_num_str_nvlist(item, "deflated_size", dds->dds_dsize,
9718 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9719 nice_num_str_nvlist(item, "ref_blocks", dds->dds_ref_blocks,
9720 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9721 nice_num_str_nvlist(item, "ref_lsize", dds->dds_ref_lsize,
9722 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9723 nice_num_str_nvlist(item, "ref_psize", dds->dds_ref_psize,
9724 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9725 nice_num_str_nvlist(item, "ref_dsize", dds->dds_ref_dsize,
9726 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9727 }
9728
9729 static void
dedup_stats_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * item)9730 dedup_stats_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t *item)
9731 {
9732 nvlist_t *config;
9733 if (cb->cb_dedup_stats) {
9734 ddt_histogram_t *ddh;
9735 ddt_stat_t *dds;
9736 ddt_object_t *ddo;
9737 nvlist_t *ddt_stat, *ddt_obj, *dedup;
9738 uint_t c;
9739 uint64_t cspace_prop;
9740
9741 config = zpool_get_config(zhp, NULL);
9742 if (nvlist_lookup_uint64_array(config,
9743 ZPOOL_CONFIG_DDT_OBJ_STATS, (uint64_t **)&ddo, &c) != 0)
9744 return;
9745
9746 dedup = fnvlist_alloc();
9747 ddt_obj = fnvlist_alloc();
9748 nice_num_str_nvlist(dedup, "obj_count", ddo->ddo_count,
9749 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9750 if (ddo->ddo_count == 0) {
9751 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_OBJ_STATS,
9752 ddt_obj);
9753 fnvlist_add_nvlist(item, "dedup_stats", dedup);
9754 fnvlist_free(ddt_obj);
9755 fnvlist_free(dedup);
9756 return;
9757 } else {
9758 nice_num_str_nvlist(dedup, "dspace", ddo->ddo_dspace,
9759 cb->cb_literal, cb->cb_json_as_int,
9760 ZFS_NICENUM_1024);
9761 nice_num_str_nvlist(dedup, "mspace", ddo->ddo_mspace,
9762 cb->cb_literal, cb->cb_json_as_int,
9763 ZFS_NICENUM_1024);
9764 /*
9765 * Squash cached size into in-core size to handle race.
9766 * Only include cached size if it is available.
9767 */
9768 cspace_prop = zpool_get_prop_int(zhp,
9769 ZPOOL_PROP_DEDUPCACHED, NULL);
9770 cspace_prop = MIN(cspace_prop, ddo->ddo_mspace);
9771 nice_num_str_nvlist(dedup, "cspace", cspace_prop,
9772 cb->cb_literal, cb->cb_json_as_int,
9773 ZFS_NICENUM_1024);
9774 }
9775
9776 ddt_stat = fnvlist_alloc();
9777 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
9778 (uint64_t **)&dds, &c) == 0) {
9779 nvlist_t *total = fnvlist_alloc();
9780 if (dds->dds_blocks == 0)
9781 fnvlist_add_string(total, "blocks", "0");
9782 else
9783 ddt_stats_nvlist(dds, cb, total);
9784 fnvlist_add_nvlist(ddt_stat, "total", total);
9785 fnvlist_free(total);
9786 }
9787 if (nvlist_lookup_uint64_array(config,
9788 ZPOOL_CONFIG_DDT_HISTOGRAM, (uint64_t **)&ddh, &c) == 0) {
9789 nvlist_t *hist = fnvlist_alloc();
9790 nvlist_t *entry = NULL;
9791 char buf[16];
9792 for (int h = 0; h < 64; h++) {
9793 if (ddh->ddh_stat[h].dds_blocks != 0) {
9794 entry = fnvlist_alloc();
9795 ddt_stats_nvlist(&ddh->ddh_stat[h], cb,
9796 entry);
9797 (void) snprintf(buf, 16, "%d", h);
9798 fnvlist_add_nvlist(hist, buf, entry);
9799 fnvlist_free(entry);
9800 }
9801 }
9802 if (!nvlist_empty(hist))
9803 fnvlist_add_nvlist(ddt_stat, "histogram", hist);
9804 fnvlist_free(hist);
9805 }
9806
9807 if (!nvlist_empty(ddt_obj)) {
9808 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_OBJ_STATS,
9809 ddt_obj);
9810 }
9811 fnvlist_free(ddt_obj);
9812 if (!nvlist_empty(ddt_stat)) {
9813 fnvlist_add_nvlist(dedup, ZPOOL_CONFIG_DDT_STATS,
9814 ddt_stat);
9815 }
9816 fnvlist_free(ddt_stat);
9817 if (!nvlist_empty(dedup))
9818 fnvlist_add_nvlist(item, "dedup_stats", dedup);
9819 fnvlist_free(dedup);
9820 }
9821 }
9822
9823 static void
raidz_expand_status_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nvroot,nvlist_t * item)9824 raidz_expand_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb,
9825 nvlist_t *nvroot, nvlist_t *item)
9826 {
9827 uint_t c;
9828 pool_raidz_expand_stat_t *pres = NULL;
9829 if (nvlist_lookup_uint64_array(nvroot,
9830 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c) == 0) {
9831 nvlist_t **child;
9832 uint_t children;
9833 nvlist_t *nv = fnvlist_alloc();
9834 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
9835 &child, &children) == 0);
9836 assert(pres->pres_expanding_vdev < children);
9837 char *name =
9838 zpool_vdev_name(g_zfs, zhp,
9839 child[pres->pres_expanding_vdev], 0);
9840 fill_vdev_info(nv, zhp, name, B_FALSE, cb->cb_json_as_int);
9841 fnvlist_add_string(nv, "state",
9842 pool_scan_state_str[pres->pres_state]);
9843 nice_num_str_nvlist(nv, "expanding_vdev",
9844 pres->pres_expanding_vdev, B_TRUE, cb->cb_json_as_int,
9845 ZFS_NICENUM_1024);
9846 nice_num_str_nvlist(nv, "start_time", pres->pres_start_time,
9847 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP);
9848 nice_num_str_nvlist(nv, "end_time", pres->pres_end_time,
9849 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP);
9850 nice_num_str_nvlist(nv, "to_reflow", pres->pres_to_reflow,
9851 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9852 nice_num_str_nvlist(nv, "reflowed", pres->pres_reflowed,
9853 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9854 nice_num_str_nvlist(nv, "waiting_for_resilver",
9855 pres->pres_waiting_for_resilver, B_TRUE,
9856 cb->cb_json_as_int, ZFS_NICENUM_1024);
9857 fnvlist_add_nvlist(item, ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, nv);
9858 fnvlist_free(nv);
9859 free(name);
9860 }
9861 }
9862
9863 static void
checkpoint_status_nvlist(nvlist_t * nvroot,status_cbdata_t * cb,nvlist_t * item)9864 checkpoint_status_nvlist(nvlist_t *nvroot, status_cbdata_t *cb,
9865 nvlist_t *item)
9866 {
9867 uint_t c;
9868 pool_checkpoint_stat_t *pcs = NULL;
9869 if (nvlist_lookup_uint64_array(nvroot,
9870 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c) == 0) {
9871 nvlist_t *nv = fnvlist_alloc();
9872 fnvlist_add_string(nv, "state",
9873 checkpoint_state_str[pcs->pcs_state]);
9874 nice_num_str_nvlist(nv, "start_time",
9875 pcs->pcs_start_time, cb->cb_literal, cb->cb_json_as_int,
9876 ZFS_NICE_TIMESTAMP);
9877 nice_num_str_nvlist(nv, "space",
9878 pcs->pcs_space, cb->cb_literal, cb->cb_json_as_int,
9879 ZFS_NICENUM_BYTES);
9880 fnvlist_add_nvlist(item, ZPOOL_CONFIG_CHECKPOINT_STATS, nv);
9881 fnvlist_free(nv);
9882 }
9883 }
9884
9885 static void
removal_status_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nvroot,nvlist_t * item)9886 removal_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb,
9887 nvlist_t *nvroot, nvlist_t *item)
9888 {
9889 uint_t c;
9890 pool_removal_stat_t *prs = NULL;
9891 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_REMOVAL_STATS,
9892 (uint64_t **)&prs, &c) == 0) {
9893 if (prs->prs_state != DSS_NONE) {
9894 nvlist_t **child;
9895 uint_t children;
9896 verify(nvlist_lookup_nvlist_array(nvroot,
9897 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
9898 assert(prs->prs_removing_vdev < children);
9899 char *vdev_name = zpool_vdev_name(g_zfs, zhp,
9900 child[prs->prs_removing_vdev], B_TRUE);
9901 nvlist_t *nv = fnvlist_alloc();
9902 fill_vdev_info(nv, zhp, vdev_name, B_FALSE,
9903 cb->cb_json_as_int);
9904 fnvlist_add_string(nv, "state",
9905 pool_scan_state_str[prs->prs_state]);
9906 nice_num_str_nvlist(nv, "removing_vdev",
9907 prs->prs_removing_vdev, B_TRUE, cb->cb_json_as_int,
9908 ZFS_NICENUM_1024);
9909 nice_num_str_nvlist(nv, "start_time",
9910 prs->prs_start_time, cb->cb_literal,
9911 cb->cb_json_as_int, ZFS_NICE_TIMESTAMP);
9912 nice_num_str_nvlist(nv, "end_time", prs->prs_end_time,
9913 cb->cb_literal, cb->cb_json_as_int,
9914 ZFS_NICE_TIMESTAMP);
9915 nice_num_str_nvlist(nv, "to_copy", prs->prs_to_copy,
9916 cb->cb_literal, cb->cb_json_as_int,
9917 ZFS_NICENUM_BYTES);
9918 nice_num_str_nvlist(nv, "copied", prs->prs_copied,
9919 cb->cb_literal, cb->cb_json_as_int,
9920 ZFS_NICENUM_BYTES);
9921 nice_num_str_nvlist(nv, "mapping_memory",
9922 prs->prs_mapping_memory, cb->cb_literal,
9923 cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9924 fnvlist_add_nvlist(item,
9925 ZPOOL_CONFIG_REMOVAL_STATS, nv);
9926 fnvlist_free(nv);
9927 free(vdev_name);
9928 }
9929 }
9930 }
9931
9932 static void
scan_status_nvlist(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t * nvroot,nvlist_t * item)9933 scan_status_nvlist(zpool_handle_t *zhp, status_cbdata_t *cb,
9934 nvlist_t *nvroot, nvlist_t *item)
9935 {
9936 pool_scan_stat_t *ps = NULL;
9937 uint_t c;
9938 nvlist_t *scan = fnvlist_alloc();
9939 nvlist_t **child;
9940 uint_t children;
9941
9942 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
9943 (uint64_t **)&ps, &c) == 0) {
9944 fnvlist_add_string(scan, "function",
9945 pool_scan_func_str[ps->pss_func]);
9946 fnvlist_add_string(scan, "state",
9947 pool_scan_state_str[ps->pss_state]);
9948 nice_num_str_nvlist(scan, "start_time", ps->pss_start_time,
9949 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP);
9950 nice_num_str_nvlist(scan, "end_time", ps->pss_end_time,
9951 cb->cb_literal, cb->cb_json_as_int, ZFS_NICE_TIMESTAMP);
9952 nice_num_str_nvlist(scan, "to_examine", ps->pss_to_examine,
9953 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9954 nice_num_str_nvlist(scan, "examined", ps->pss_examined,
9955 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9956 nice_num_str_nvlist(scan, "skipped", ps->pss_skipped,
9957 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9958 nice_num_str_nvlist(scan, "processed", ps->pss_processed,
9959 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9960 nice_num_str_nvlist(scan, "errors", ps->pss_errors,
9961 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_1024);
9962 nice_num_str_nvlist(scan, "bytes_per_scan", ps->pss_pass_exam,
9963 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9964 nice_num_str_nvlist(scan, "pass_start", ps->pss_pass_start,
9965 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024);
9966 nice_num_str_nvlist(scan, "scrub_pause",
9967 ps->pss_pass_scrub_pause, cb->cb_literal,
9968 cb->cb_json_as_int, ZFS_NICE_TIMESTAMP);
9969 nice_num_str_nvlist(scan, "scrub_spent_paused",
9970 ps->pss_pass_scrub_spent_paused,
9971 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024);
9972 nice_num_str_nvlist(scan, "issued_bytes_per_scan",
9973 ps->pss_pass_issued, cb->cb_literal,
9974 cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9975 nice_num_str_nvlist(scan, "issued", ps->pss_issued,
9976 cb->cb_literal, cb->cb_json_as_int, ZFS_NICENUM_BYTES);
9977 if (ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB &&
9978 ps->pss_error_scrub_start > ps->pss_start_time) {
9979 fnvlist_add_string(scan, "err_scrub_func",
9980 pool_scan_func_str[ps->pss_error_scrub_func]);
9981 fnvlist_add_string(scan, "err_scrub_state",
9982 pool_scan_state_str[ps->pss_error_scrub_state]);
9983 nice_num_str_nvlist(scan, "err_scrub_start_time",
9984 ps->pss_error_scrub_start,
9985 cb->cb_literal, cb->cb_json_as_int,
9986 ZFS_NICE_TIMESTAMP);
9987 nice_num_str_nvlist(scan, "err_scrub_end_time",
9988 ps->pss_error_scrub_end,
9989 cb->cb_literal, cb->cb_json_as_int,
9990 ZFS_NICE_TIMESTAMP);
9991 nice_num_str_nvlist(scan, "err_scrub_examined",
9992 ps->pss_error_scrub_examined,
9993 cb->cb_literal, cb->cb_json_as_int,
9994 ZFS_NICENUM_1024);
9995 nice_num_str_nvlist(scan, "err_scrub_to_examine",
9996 ps->pss_error_scrub_to_be_examined,
9997 cb->cb_literal, cb->cb_json_as_int,
9998 ZFS_NICENUM_1024);
9999 nice_num_str_nvlist(scan, "err_scrub_pause",
10000 ps->pss_pass_error_scrub_pause,
10001 B_TRUE, cb->cb_json_as_int, ZFS_NICENUM_1024);
10002 }
10003 }
10004
10005 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
10006 &child, &children) == 0) {
10007 vdev_rebuild_stat_t *vrs;
10008 uint_t i;
10009 char *name;
10010 nvlist_t *nv;
10011 nvlist_t *rebuild = fnvlist_alloc();
10012 uint64_t st;
10013 for (uint_t c = 0; c < children; c++) {
10014 if (nvlist_lookup_uint64_array(child[c],
10015 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs,
10016 &i) == 0) {
10017 if (vrs->vrs_state != VDEV_REBUILD_NONE) {
10018 nv = fnvlist_alloc();
10019 name = zpool_vdev_name(g_zfs, zhp,
10020 child[c], VDEV_NAME_TYPE_ID);
10021 fill_vdev_info(nv, zhp, name, B_FALSE,
10022 cb->cb_json_as_int);
10023 st = vrs->vrs_state;
10024 fnvlist_add_string(nv, "state",
10025 vdev_rebuild_state_str[st]);
10026 nice_num_str_nvlist(nv, "start_time",
10027 vrs->vrs_start_time, cb->cb_literal,
10028 cb->cb_json_as_int,
10029 ZFS_NICE_TIMESTAMP);
10030 nice_num_str_nvlist(nv, "end_time",
10031 vrs->vrs_end_time, cb->cb_literal,
10032 cb->cb_json_as_int,
10033 ZFS_NICE_TIMESTAMP);
10034 nice_num_str_nvlist(nv, "scan_time",
10035 vrs->vrs_scan_time_ms * 1000000,
10036 cb->cb_literal, cb->cb_json_as_int,
10037 ZFS_NICENUM_TIME);
10038 nice_num_str_nvlist(nv, "scanned",
10039 vrs->vrs_bytes_scanned,
10040 cb->cb_literal, cb->cb_json_as_int,
10041 ZFS_NICENUM_BYTES);
10042 nice_num_str_nvlist(nv, "issued",
10043 vrs->vrs_bytes_issued,
10044 cb->cb_literal, cb->cb_json_as_int,
10045 ZFS_NICENUM_BYTES);
10046 nice_num_str_nvlist(nv, "rebuilt",
10047 vrs->vrs_bytes_rebuilt,
10048 cb->cb_literal, cb->cb_json_as_int,
10049 ZFS_NICENUM_BYTES);
10050 nice_num_str_nvlist(nv, "to_scan",
10051 vrs->vrs_bytes_est, cb->cb_literal,
10052 cb->cb_json_as_int,
10053 ZFS_NICENUM_BYTES);
10054 nice_num_str_nvlist(nv, "errors",
10055 vrs->vrs_errors, cb->cb_literal,
10056 cb->cb_json_as_int,
10057 ZFS_NICENUM_1024);
10058 nice_num_str_nvlist(nv, "pass_time",
10059 vrs->vrs_pass_time_ms * 1000000,
10060 cb->cb_literal, cb->cb_json_as_int,
10061 ZFS_NICENUM_TIME);
10062 nice_num_str_nvlist(nv, "pass_scanned",
10063 vrs->vrs_pass_bytes_scanned,
10064 cb->cb_literal, cb->cb_json_as_int,
10065 ZFS_NICENUM_BYTES);
10066 nice_num_str_nvlist(nv, "pass_issued",
10067 vrs->vrs_pass_bytes_issued,
10068 cb->cb_literal, cb->cb_json_as_int,
10069 ZFS_NICENUM_BYTES);
10070 nice_num_str_nvlist(nv, "pass_skipped",
10071 vrs->vrs_pass_bytes_skipped,
10072 cb->cb_literal, cb->cb_json_as_int,
10073 ZFS_NICENUM_BYTES);
10074 fnvlist_add_nvlist(rebuild, name, nv);
10075 free(name);
10076 }
10077 }
10078 }
10079 if (!nvlist_empty(rebuild))
10080 fnvlist_add_nvlist(scan, "rebuild_stats", rebuild);
10081 fnvlist_free(rebuild);
10082 }
10083
10084 if (!nvlist_empty(scan))
10085 fnvlist_add_nvlist(item, ZPOOL_CONFIG_SCAN_STATS, scan);
10086 fnvlist_free(scan);
10087 }
10088
10089 /*
10090 * Print the scan status.
10091 */
10092 static void
print_scan_status(zpool_handle_t * zhp,nvlist_t * nvroot)10093 print_scan_status(zpool_handle_t *zhp, nvlist_t *nvroot)
10094 {
10095 uint64_t rebuild_end_time = 0, resilver_end_time = 0;
10096 boolean_t have_resilver = B_FALSE, have_scrub = B_FALSE;
10097 boolean_t have_errorscrub = B_FALSE;
10098 boolean_t active_resilver = B_FALSE;
10099 pool_checkpoint_stat_t *pcs = NULL;
10100 pool_scan_stat_t *ps = NULL;
10101 uint_t c;
10102 time_t scrub_start = 0, errorscrub_start = 0;
10103
10104 if (nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
10105 (uint64_t **)&ps, &c) == 0) {
10106 if (ps->pss_func == POOL_SCAN_RESILVER) {
10107 resilver_end_time = ps->pss_end_time;
10108 active_resilver = (ps->pss_state == DSS_SCANNING);
10109 }
10110
10111 have_resilver = (ps->pss_func == POOL_SCAN_RESILVER);
10112 have_scrub = (ps->pss_func == POOL_SCAN_SCRUB);
10113 scrub_start = ps->pss_start_time;
10114 if (c > offsetof(pool_scan_stat_t,
10115 pss_pass_error_scrub_pause) / 8) {
10116 have_errorscrub = (ps->pss_error_scrub_func ==
10117 POOL_SCAN_ERRORSCRUB);
10118 errorscrub_start = ps->pss_error_scrub_start;
10119 }
10120 }
10121
10122 boolean_t active_rebuild = check_rebuilding(nvroot, &rebuild_end_time);
10123 boolean_t have_rebuild = (active_rebuild || (rebuild_end_time > 0));
10124
10125 /* Always print the scrub status when available. */
10126 if (have_scrub && scrub_start > errorscrub_start)
10127 print_scan_scrub_resilver_status(ps);
10128 else if (have_errorscrub && errorscrub_start >= scrub_start)
10129 print_err_scrub_status(ps);
10130
10131 /*
10132 * When there is an active resilver or rebuild print its status.
10133 * Otherwise print the status of the last resilver or rebuild.
10134 */
10135 if (active_resilver || (!active_rebuild && have_resilver &&
10136 resilver_end_time && resilver_end_time > rebuild_end_time)) {
10137 print_scan_scrub_resilver_status(ps);
10138 } else if (active_rebuild || (!active_resilver && have_rebuild &&
10139 rebuild_end_time && rebuild_end_time > resilver_end_time)) {
10140 print_rebuild_status(zhp, nvroot);
10141 }
10142
10143 (void) nvlist_lookup_uint64_array(nvroot,
10144 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
10145 print_checkpoint_scan_warning(ps, pcs);
10146 }
10147
10148 /*
10149 * Print out detailed removal status.
10150 */
10151 static void
print_removal_status(zpool_handle_t * zhp,pool_removal_stat_t * prs)10152 print_removal_status(zpool_handle_t *zhp, pool_removal_stat_t *prs)
10153 {
10154 char copied_buf[7], examined_buf[7], total_buf[7], rate_buf[7];
10155 time_t start, end;
10156 nvlist_t *config, *nvroot;
10157 nvlist_t **child;
10158 uint_t children;
10159 char *vdev_name;
10160
10161 if (prs == NULL || prs->prs_state == DSS_NONE)
10162 return;
10163
10164 /*
10165 * Determine name of vdev.
10166 */
10167 config = zpool_get_config(zhp, NULL);
10168 nvroot = fnvlist_lookup_nvlist(config,
10169 ZPOOL_CONFIG_VDEV_TREE);
10170 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
10171 &child, &children) == 0);
10172 assert(prs->prs_removing_vdev < children);
10173 vdev_name = zpool_vdev_name(g_zfs, zhp,
10174 child[prs->prs_removing_vdev], B_TRUE);
10175
10176 (void) printf_color(ANSI_BOLD, gettext("remove: "));
10177
10178 start = prs->prs_start_time;
10179 end = prs->prs_end_time;
10180 zfs_nicenum(prs->prs_copied, copied_buf, sizeof (copied_buf));
10181
10182 /*
10183 * Removal is finished or canceled.
10184 */
10185 if (prs->prs_state == DSS_FINISHED) {
10186 uint64_t minutes_taken = (end - start) / 60;
10187
10188 (void) printf(gettext("Removal of vdev %llu copied %s "
10189 "in %lluh%um, completed on %s"),
10190 (longlong_t)prs->prs_removing_vdev,
10191 copied_buf,
10192 (u_longlong_t)(minutes_taken / 60),
10193 (uint_t)(minutes_taken % 60),
10194 ctime((time_t *)&end));
10195 } else if (prs->prs_state == DSS_CANCELED) {
10196 (void) printf(gettext("Removal of %s canceled on %s"),
10197 vdev_name, ctime(&end));
10198 } else {
10199 uint64_t copied, total, elapsed, rate, mins_left, hours_left;
10200 double fraction_done;
10201
10202 assert(prs->prs_state == DSS_SCANNING);
10203
10204 /*
10205 * Removal is in progress.
10206 */
10207 (void) printf(gettext(
10208 "Evacuation of %s in progress since %s"),
10209 vdev_name, ctime(&start));
10210
10211 copied = prs->prs_copied > 0 ? prs->prs_copied : 1;
10212 total = prs->prs_to_copy;
10213 fraction_done = (double)copied / total;
10214
10215 /* elapsed time for this pass */
10216 elapsed = time(NULL) - prs->prs_start_time;
10217 elapsed = elapsed > 0 ? elapsed : 1;
10218 rate = copied / elapsed;
10219 rate = rate > 0 ? rate : 1;
10220 mins_left = ((total - copied) / rate) / 60;
10221 hours_left = mins_left / 60;
10222
10223 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
10224 zfs_nicenum(total, total_buf, sizeof (total_buf));
10225 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
10226
10227 /*
10228 * do not print estimated time if hours_left is more than
10229 * 30 days
10230 */
10231 (void) printf(gettext(
10232 "\t%s copied out of %s at %s/s, %.2f%% done"),
10233 examined_buf, total_buf, rate_buf, 100 * fraction_done);
10234 if (hours_left < (30 * 24)) {
10235 (void) printf(gettext(", %lluh%um to go\n"),
10236 (u_longlong_t)hours_left, (uint_t)(mins_left % 60));
10237 } else {
10238 (void) printf(gettext(
10239 ", (copy is slow, no estimated time)\n"));
10240 }
10241 }
10242 free(vdev_name);
10243
10244 if (prs->prs_mapping_memory > 0) {
10245 char mem_buf[7];
10246 zfs_nicenum(prs->prs_mapping_memory, mem_buf, sizeof (mem_buf));
10247 (void) printf(gettext(
10248 "\t%s memory used for removed device mappings\n"),
10249 mem_buf);
10250 }
10251 }
10252
10253 /*
10254 * Print out detailed raidz expansion status.
10255 */
10256 static void
print_raidz_expand_status(zpool_handle_t * zhp,pool_raidz_expand_stat_t * pres)10257 print_raidz_expand_status(zpool_handle_t *zhp, pool_raidz_expand_stat_t *pres)
10258 {
10259 char copied_buf[7];
10260
10261 if (pres == NULL || pres->pres_state == DSS_NONE)
10262 return;
10263
10264 /*
10265 * Determine name of vdev.
10266 */
10267 nvlist_t *config = zpool_get_config(zhp, NULL);
10268 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
10269 ZPOOL_CONFIG_VDEV_TREE);
10270 nvlist_t **child;
10271 uint_t children;
10272 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
10273 &child, &children) == 0);
10274 assert(pres->pres_expanding_vdev < children);
10275
10276 (void) printf_color(ANSI_BOLD, gettext("expand: "));
10277
10278 time_t start = pres->pres_start_time;
10279 time_t end = pres->pres_end_time;
10280 char *vname =
10281 zpool_vdev_name(g_zfs, zhp, child[pres->pres_expanding_vdev], 0);
10282 zfs_nicenum(pres->pres_reflowed, copied_buf, sizeof (copied_buf));
10283
10284 /*
10285 * Expansion is finished or canceled.
10286 */
10287 if (pres->pres_state == DSS_FINISHED) {
10288 char time_buf[32];
10289 secs_to_dhms(end - start, time_buf);
10290
10291 (void) printf(gettext("expanded %s-%u copied %s in %s, "
10292 "on %s"), vname, (int)pres->pres_expanding_vdev,
10293 copied_buf, time_buf, ctime((time_t *)&end));
10294 } else {
10295 char examined_buf[7], total_buf[7], rate_buf[7];
10296 uint64_t copied, total, elapsed, rate, secs_left;
10297 double fraction_done;
10298
10299 assert(pres->pres_state == DSS_SCANNING);
10300
10301 /*
10302 * Expansion is in progress.
10303 */
10304 (void) printf(gettext(
10305 "expansion of %s-%u in progress since %s"),
10306 vname, (int)pres->pres_expanding_vdev, ctime(&start));
10307
10308 copied = pres->pres_reflowed > 0 ? pres->pres_reflowed : 1;
10309 total = pres->pres_to_reflow;
10310 fraction_done = (double)copied / total;
10311
10312 /* elapsed time for this pass */
10313 elapsed = time(NULL) - pres->pres_start_time;
10314 elapsed = elapsed > 0 ? elapsed : 1;
10315 rate = copied / elapsed;
10316 rate = rate > 0 ? rate : 1;
10317 secs_left = (total - copied) / rate;
10318
10319 zfs_nicenum(copied, examined_buf, sizeof (examined_buf));
10320 zfs_nicenum(total, total_buf, sizeof (total_buf));
10321 zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
10322
10323 /*
10324 * do not print estimated time if hours_left is more than
10325 * 30 days
10326 */
10327 (void) printf(gettext("\t%s / %s copied at %s/s, %.2f%% done"),
10328 examined_buf, total_buf, rate_buf, 100 * fraction_done);
10329 if (pres->pres_waiting_for_resilver) {
10330 (void) printf(gettext(", paused for resilver or "
10331 "clear\n"));
10332 } else if (secs_left < (30 * 24 * 3600)) {
10333 char time_buf[32];
10334 secs_to_dhms(secs_left, time_buf);
10335 (void) printf(gettext(", %s to go\n"), time_buf);
10336 } else {
10337 (void) printf(gettext(
10338 ", (copy is slow, no estimated time)\n"));
10339 }
10340 }
10341 free(vname);
10342 }
10343 static void
print_checkpoint_status(pool_checkpoint_stat_t * pcs)10344 print_checkpoint_status(pool_checkpoint_stat_t *pcs)
10345 {
10346 time_t start;
10347 char space_buf[7];
10348
10349 if (pcs == NULL || pcs->pcs_state == CS_NONE)
10350 return;
10351
10352 (void) printf(gettext("checkpoint: "));
10353
10354 start = pcs->pcs_start_time;
10355 zfs_nicenum(pcs->pcs_space, space_buf, sizeof (space_buf));
10356
10357 if (pcs->pcs_state == CS_CHECKPOINT_EXISTS) {
10358 char *date = ctime(&start);
10359
10360 /*
10361 * ctime() adds a newline at the end of the generated
10362 * string, thus the weird format specifier and the
10363 * strlen() call used to chop it off from the output.
10364 */
10365 (void) printf(gettext("created %.*s, consumes %s\n"),
10366 (int)(strlen(date) - 1), date, space_buf);
10367 return;
10368 }
10369
10370 assert(pcs->pcs_state == CS_CHECKPOINT_DISCARDING);
10371
10372 (void) printf(gettext("discarding, %s remaining.\n"),
10373 space_buf);
10374 }
10375
10376 static void
print_error_log(zpool_handle_t * zhp)10377 print_error_log(zpool_handle_t *zhp)
10378 {
10379 nvlist_t *nverrlist = NULL;
10380 nvpair_t *elem;
10381 char *pathname;
10382 size_t len = MAXPATHLEN * 2;
10383
10384 if (zpool_get_errlog(zhp, &nverrlist) != 0)
10385 return;
10386
10387 (void) printf("errors: Permanent errors have been "
10388 "detected in the following files:\n\n");
10389
10390 pathname = safe_malloc(len);
10391 elem = NULL;
10392 while ((elem = nvlist_next_nvpair(nverrlist, elem)) != NULL) {
10393 nvlist_t *nv;
10394 uint64_t dsobj, obj;
10395
10396 verify(nvpair_value_nvlist(elem, &nv) == 0);
10397 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_DATASET,
10398 &dsobj) == 0);
10399 verify(nvlist_lookup_uint64(nv, ZPOOL_ERR_OBJECT,
10400 &obj) == 0);
10401 zpool_obj_to_path(zhp, dsobj, obj, pathname, len);
10402 (void) printf("%7s %s\n", "", pathname);
10403 }
10404 free(pathname);
10405 nvlist_free(nverrlist);
10406 }
10407
10408 static void
print_spares(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t ** spares,uint_t nspares)10409 print_spares(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **spares,
10410 uint_t nspares)
10411 {
10412 uint_t i;
10413 char *name;
10414
10415 if (nspares == 0)
10416 return;
10417
10418 (void) printf(gettext("\tspares\n"));
10419
10420 for (i = 0; i < nspares; i++) {
10421 name = zpool_vdev_name(g_zfs, zhp, spares[i],
10422 cb->cb_name_flags);
10423 print_status_config(zhp, cb, name, spares[i], 2, B_TRUE, NULL);
10424 free(name);
10425 }
10426 }
10427
10428 static void
print_l2cache(zpool_handle_t * zhp,status_cbdata_t * cb,nvlist_t ** l2cache,uint_t nl2cache)10429 print_l2cache(zpool_handle_t *zhp, status_cbdata_t *cb, nvlist_t **l2cache,
10430 uint_t nl2cache)
10431 {
10432 uint_t i;
10433 char *name;
10434
10435 if (nl2cache == 0)
10436 return;
10437
10438 (void) printf(gettext("\tcache\n"));
10439
10440 for (i = 0; i < nl2cache; i++) {
10441 name = zpool_vdev_name(g_zfs, zhp, l2cache[i],
10442 cb->cb_name_flags);
10443 print_status_config(zhp, cb, name, l2cache[i], 2,
10444 B_FALSE, NULL);
10445 free(name);
10446 }
10447 }
10448
10449 static void
print_dedup_stats(zpool_handle_t * zhp,nvlist_t * config,boolean_t literal)10450 print_dedup_stats(zpool_handle_t *zhp, nvlist_t *config, boolean_t literal)
10451 {
10452 ddt_histogram_t *ddh;
10453 ddt_stat_t *dds;
10454 ddt_object_t *ddo;
10455 uint_t c;
10456 /* Extra space provided for literal display */
10457 char dspace[32], mspace[32], cspace[32];
10458 uint64_t cspace_prop;
10459 enum zfs_nicenum_format format;
10460 zprop_source_t src;
10461
10462 /*
10463 * If the pool was faulted then we may not have been able to
10464 * obtain the config. Otherwise, if we have anything in the dedup
10465 * table continue processing the stats.
10466 */
10467 if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_OBJ_STATS,
10468 (uint64_t **)&ddo, &c) != 0)
10469 return;
10470
10471 (void) printf("\n");
10472 (void) printf(gettext(" dedup: "));
10473 if (ddo->ddo_count == 0) {
10474 (void) printf(gettext("no DDT entries\n"));
10475 return;
10476 }
10477
10478 /*
10479 * Squash cached size into in-core size to handle race.
10480 * Only include cached size if it is available.
10481 */
10482 cspace_prop = zpool_get_prop_int(zhp, ZPOOL_PROP_DEDUPCACHED, &src);
10483 cspace_prop = MIN(cspace_prop, ddo->ddo_mspace);
10484 format = literal ? ZFS_NICENUM_RAW : ZFS_NICENUM_1024;
10485 zfs_nicenum_format(cspace_prop, cspace, sizeof (cspace), format);
10486 zfs_nicenum_format(ddo->ddo_dspace, dspace, sizeof (dspace), format);
10487 zfs_nicenum_format(ddo->ddo_mspace, mspace, sizeof (mspace), format);
10488 (void) printf("DDT entries %llu, size %s on disk, %s in core",
10489 (u_longlong_t)ddo->ddo_count,
10490 dspace,
10491 mspace);
10492 if (src != ZPROP_SRC_DEFAULT) {
10493 (void) printf(", %s cached (%.02f%%)",
10494 cspace,
10495 (double)cspace_prop / (double)ddo->ddo_mspace * 100.0);
10496 }
10497 (void) printf("\n");
10498
10499 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
10500 (uint64_t **)&dds, &c) == 0);
10501 verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_HISTOGRAM,
10502 (uint64_t **)&ddh, &c) == 0);
10503 zpool_dump_ddt(dds, ddh);
10504 }
10505
10506 #define ST_SIZE 4096
10507 #define AC_SIZE 2048
10508
10509 static void
print_status_reason(zpool_handle_t * zhp,status_cbdata_t * cbp,zpool_status_t reason,zpool_errata_t errata,nvlist_t * item)10510 print_status_reason(zpool_handle_t *zhp, status_cbdata_t *cbp,
10511 zpool_status_t reason, zpool_errata_t errata, nvlist_t *item)
10512 {
10513 char status[ST_SIZE];
10514 char action[AC_SIZE];
10515 memset(status, 0, ST_SIZE);
10516 memset(action, 0, AC_SIZE);
10517
10518 switch (reason) {
10519 case ZPOOL_STATUS_MISSING_DEV_R:
10520 (void) snprintf(status, ST_SIZE,
10521 gettext("One or more devices could "
10522 "not be opened. Sufficient replicas exist for\n\tthe pool "
10523 "to continue functioning in a degraded state.\n"));
10524 (void) snprintf(action, AC_SIZE,
10525 gettext("Attach the missing device "
10526 "and online it using 'zpool online'.\n"));
10527 break;
10528
10529 case ZPOOL_STATUS_MISSING_DEV_NR:
10530 (void) snprintf(status, ST_SIZE,
10531 gettext("One or more devices could "
10532 "not be opened. There are insufficient\n\treplicas for the"
10533 " pool to continue functioning.\n"));
10534 (void) snprintf(action, AC_SIZE,
10535 gettext("Attach the missing device "
10536 "and online it using 'zpool online'.\n"));
10537 break;
10538
10539 case ZPOOL_STATUS_CORRUPT_LABEL_R:
10540 (void) snprintf(status, ST_SIZE,
10541 gettext("One or more devices could "
10542 "not be used because the label is missing or\n\tinvalid. "
10543 "Sufficient replicas exist for the pool to continue\n\t"
10544 "functioning in a degraded state.\n"));
10545 (void) snprintf(action, AC_SIZE,
10546 gettext("Replace the device using 'zpool replace'.\n"));
10547 break;
10548
10549 case ZPOOL_STATUS_CORRUPT_LABEL_NR:
10550 (void) snprintf(status, ST_SIZE,
10551 gettext("One or more devices could "
10552 "not be used because the label is missing \n\tor invalid. "
10553 "There are insufficient replicas for the pool to "
10554 "continue\n\tfunctioning.\n"));
10555 zpool_explain_recover(zpool_get_handle(zhp),
10556 zpool_get_name(zhp), reason, zpool_get_config(zhp, NULL),
10557 action, AC_SIZE);
10558 break;
10559
10560 case ZPOOL_STATUS_FAILING_DEV:
10561 (void) snprintf(status, ST_SIZE,
10562 gettext("One or more devices has "
10563 "experienced an unrecoverable error. An\n\tattempt was "
10564 "made to correct the error. Applications are "
10565 "unaffected.\n"));
10566 (void) snprintf(action, AC_SIZE, gettext("Determine if the "
10567 "device needs to be replaced, and clear the errors\n\tusing"
10568 " 'zpool clear' or replace the device with 'zpool "
10569 "replace'.\n"));
10570 break;
10571
10572 case ZPOOL_STATUS_OFFLINE_DEV:
10573 (void) snprintf(status, ST_SIZE,
10574 gettext("One or more devices has "
10575 "been taken offline by the administrator.\n\tSufficient "
10576 "replicas exist for the pool to continue functioning in "
10577 "a\n\tdegraded state.\n"));
10578 (void) snprintf(action, AC_SIZE, gettext("Online the device "
10579 "using 'zpool online' or replace the device with\n\t'zpool "
10580 "replace'.\n"));
10581 break;
10582
10583 case ZPOOL_STATUS_REMOVED_DEV:
10584 (void) snprintf(status, ST_SIZE,
10585 gettext("One or more devices have "
10586 "been removed.\n\tSufficient replicas exist for the pool "
10587 "to continue functioning in a\n\tdegraded state.\n"));
10588 (void) snprintf(action, AC_SIZE, gettext("Online the device "
10589 "using zpool online' or replace the device with\n\t'zpool "
10590 "replace'.\n"));
10591 break;
10592
10593 case ZPOOL_STATUS_RESILVERING:
10594 case ZPOOL_STATUS_REBUILDING:
10595 (void) snprintf(status, ST_SIZE,
10596 gettext("One or more devices is "
10597 "currently being resilvered. The pool will\n\tcontinue "
10598 "to function, possibly in a degraded state.\n"));
10599 (void) snprintf(action, AC_SIZE,
10600 gettext("Wait for the resilver to complete.\n"));
10601 break;
10602
10603 case ZPOOL_STATUS_REBUILD_SCRUB:
10604 (void) snprintf(status, ST_SIZE,
10605 gettext("One or more devices have "
10606 "been sequentially resilvered, scrubbing\n\tthe pool "
10607 "is recommended.\n"));
10608 (void) snprintf(action, AC_SIZE, gettext("Use 'zpool scrub' to "
10609 "verify all data checksums.\n"));
10610 break;
10611
10612 case ZPOOL_STATUS_CORRUPT_DATA:
10613 (void) snprintf(status, ST_SIZE,
10614 gettext("One or more devices has "
10615 "experienced an error resulting in data\n\tcorruption. "
10616 "Applications may be affected.\n"));
10617 (void) snprintf(action, AC_SIZE,
10618 gettext("Restore the file in question"
10619 " if possible. Otherwise restore the\n\tentire pool from "
10620 "backup.\n"));
10621 break;
10622
10623 case ZPOOL_STATUS_CORRUPT_POOL:
10624 (void) snprintf(status, ST_SIZE, gettext("The pool metadata is "
10625 "incomplete or corrupted and the pool cannot be "
10626 "opened.\n"));
10627 zpool_explain_recover(zpool_get_handle(zhp),
10628 zpool_get_name(zhp), reason, zpool_get_config(zhp, NULL),
10629 action, AC_SIZE);
10630 break;
10631
10632 case ZPOOL_STATUS_VERSION_OLDER:
10633 (void) snprintf(status, ST_SIZE,
10634 gettext("The pool is formatted using "
10635 "a legacy on-disk format. The pool can\n\tstill be used, "
10636 "but some features are unavailable.\n"));
10637 (void) snprintf(action, AC_SIZE,
10638 gettext("Upgrade the pool using "
10639 "'zpool upgrade'. Once this is done, the\n\tpool will no "
10640 "longer be accessible on software that does not support\n\t"
10641 "feature flags.\n"));
10642 break;
10643
10644 case ZPOOL_STATUS_VERSION_NEWER:
10645 (void) snprintf(status, ST_SIZE,
10646 gettext("The pool has been upgraded "
10647 "to a newer, incompatible on-disk version.\n\tThe pool "
10648 "cannot be accessed on this system.\n"));
10649 (void) snprintf(action, AC_SIZE,
10650 gettext("Access the pool from a "
10651 "system running more recent software, or\n\trestore the "
10652 "pool from backup.\n"));
10653 break;
10654
10655 case ZPOOL_STATUS_FEAT_DISABLED:
10656 (void) snprintf(status, ST_SIZE, gettext("Some supported and "
10657 "requested features are not enabled on the pool.\n\t"
10658 "The pool can still be used, but some features are "
10659 "unavailable.\n"));
10660 (void) snprintf(action, AC_SIZE,
10661 gettext("Enable all features using "
10662 "'zpool upgrade'. Once this is done,\n\tthe pool may no "
10663 "longer be accessible by software that does not support\n\t"
10664 "the features. See zpool-features(7) for details.\n"));
10665 break;
10666
10667 case ZPOOL_STATUS_COMPATIBILITY_ERR:
10668 (void) snprintf(status, ST_SIZE, gettext("This pool has a "
10669 "compatibility list specified, but it could not be\n\t"
10670 "read/parsed at this time. The pool can still be used, "
10671 "but this\n\tshould be investigated.\n"));
10672 (void) snprintf(action, AC_SIZE,
10673 gettext("Check the value of the "
10674 "'compatibility' property against the\n\t"
10675 "appropriate file in " ZPOOL_SYSCONF_COMPAT_D " or "
10676 ZPOOL_DATA_COMPAT_D ".\n"));
10677 break;
10678
10679 case ZPOOL_STATUS_INCOMPATIBLE_FEAT:
10680 (void) snprintf(status, ST_SIZE, gettext("One or more features "
10681 "are enabled on the pool despite not being\n\t"
10682 "requested by the 'compatibility' property.\n"));
10683 (void) snprintf(action, AC_SIZE, gettext("Consider setting "
10684 "'compatibility' to an appropriate value, or\n\t"
10685 "adding needed features to the relevant file in\n\t"
10686 ZPOOL_SYSCONF_COMPAT_D " or " ZPOOL_DATA_COMPAT_D ".\n"));
10687 break;
10688
10689 case ZPOOL_STATUS_UNSUP_FEAT_READ:
10690 (void) snprintf(status, ST_SIZE,
10691 gettext("The pool cannot be accessed "
10692 "on this system because it uses the\n\tfollowing feature(s)"
10693 " not supported on this system:\n"));
10694 zpool_collect_unsup_feat(zpool_get_config(zhp, NULL), status,
10695 1024);
10696 (void) snprintf(action, AC_SIZE,
10697 gettext("Access the pool from a "
10698 "system that supports the required feature(s),\n\tor "
10699 "restore the pool from backup.\n"));
10700 break;
10701
10702 case ZPOOL_STATUS_UNSUP_FEAT_WRITE:
10703 (void) snprintf(status, ST_SIZE, gettext("The pool can only be "
10704 "accessed in read-only mode on this system. It\n\tcannot be"
10705 " accessed in read-write mode because it uses the "
10706 "following\n\tfeature(s) not supported on this system:\n"));
10707 zpool_collect_unsup_feat(zpool_get_config(zhp, NULL), status,
10708 1024);
10709 (void) snprintf(action, AC_SIZE,
10710 gettext("The pool cannot be accessed "
10711 "in read-write mode. Import the pool with\n"
10712 "\t\"-o readonly=on\", access the pool from a system that "
10713 "supports the\n\trequired feature(s), or restore the "
10714 "pool from backup.\n"));
10715 break;
10716
10717 case ZPOOL_STATUS_FAULTED_DEV_R:
10718 (void) snprintf(status, ST_SIZE,
10719 gettext("One or more devices are "
10720 "faulted in response to persistent errors.\n\tSufficient "
10721 "replicas exist for the pool to continue functioning "
10722 "in a\n\tdegraded state.\n"));
10723 (void) snprintf(action, AC_SIZE,
10724 gettext("Replace the faulted device, "
10725 "or use 'zpool clear' to mark the device\n\trepaired.\n"));
10726 break;
10727
10728 case ZPOOL_STATUS_FAULTED_DEV_NR:
10729 (void) snprintf(status, ST_SIZE,
10730 gettext("One or more devices are "
10731 "faulted in response to persistent errors. There are "
10732 "insufficient replicas for the pool to\n\tcontinue "
10733 "functioning.\n"));
10734 (void) snprintf(action, AC_SIZE,
10735 gettext("Destroy and re-create the "
10736 "pool from a backup source. Manually marking the device\n"
10737 "\trepaired using 'zpool clear' may allow some data "
10738 "to be recovered.\n"));
10739 break;
10740
10741 case ZPOOL_STATUS_IO_FAILURE_MMP:
10742 (void) snprintf(status, ST_SIZE,
10743 gettext("The pool is suspended "
10744 "because multihost writes failed or were delayed;\n\t"
10745 "another system could import the pool undetected.\n"));
10746 (void) snprintf(action, AC_SIZE,
10747 gettext("Make sure the pool's devices"
10748 " are connected, then reboot your system and\n\timport the "
10749 "pool or run 'zpool clear' to resume the pool.\n"));
10750 break;
10751
10752 case ZPOOL_STATUS_IO_FAILURE_WAIT:
10753 case ZPOOL_STATUS_IO_FAILURE_CONTINUE:
10754 (void) snprintf(status, ST_SIZE,
10755 gettext("One or more devices are "
10756 "faulted in response to IO failures.\n"));
10757 (void) snprintf(action, AC_SIZE,
10758 gettext("Make sure the affected "
10759 "devices are connected, then run 'zpool clear'.\n"));
10760 break;
10761
10762 case ZPOOL_STATUS_BAD_LOG:
10763 (void) snprintf(status, ST_SIZE, gettext("An intent log record "
10764 "could not be read.\n"
10765 "\tWaiting for administrator intervention to fix the "
10766 "faulted pool.\n"));
10767 (void) snprintf(action, AC_SIZE,
10768 gettext("Either restore the affected "
10769 "device(s) and run 'zpool online',\n"
10770 "\tor ignore the intent log records by running "
10771 "'zpool clear'.\n"));
10772 break;
10773
10774 case ZPOOL_STATUS_NON_NATIVE_ASHIFT:
10775 (void) snprintf(status, ST_SIZE,
10776 gettext("One or more devices are "
10777 "configured to use a non-native block size.\n"
10778 "\tExpect reduced performance.\n"));
10779 (void) snprintf(action, AC_SIZE,
10780 gettext("Replace affected devices "
10781 "with devices that support the\n\tconfigured block size, "
10782 "or migrate data to a properly configured\n\tpool.\n"));
10783 break;
10784
10785 case ZPOOL_STATUS_HOSTID_MISMATCH:
10786 (void) snprintf(status, ST_SIZE,
10787 gettext("Mismatch between pool hostid"
10788 " and system hostid on imported pool.\n\tThis pool was "
10789 "previously imported into a system with a different "
10790 "hostid,\n\tand then was verbatim imported into this "
10791 "system.\n"));
10792 (void) snprintf(action, AC_SIZE,
10793 gettext("Export this pool on all "
10794 "systems on which it is imported.\n"
10795 "\tThen import it to correct the mismatch.\n"));
10796 break;
10797
10798 case ZPOOL_STATUS_ERRATA:
10799 (void) snprintf(status, ST_SIZE,
10800 gettext("Errata #%d detected.\n"), errata);
10801 switch (errata) {
10802 case ZPOOL_ERRATA_NONE:
10803 break;
10804
10805 case ZPOOL_ERRATA_ZOL_2094_SCRUB:
10806 (void) snprintf(action, AC_SIZE,
10807 gettext("To correct the issue run "
10808 "'zpool scrub'.\n"));
10809 break;
10810
10811 case ZPOOL_ERRATA_ZOL_6845_ENCRYPTION:
10812 (void) strlcat(status, gettext("\tExisting encrypted "
10813 "datasets contain an on-disk incompatibility\n\t "
10814 "which needs to be corrected.\n"), ST_SIZE);
10815 (void) snprintf(action, AC_SIZE,
10816 gettext("To correct the issue"
10817 " backup existing encrypted datasets to new\n\t"
10818 "encrypted datasets and destroy the old ones. "
10819 "'zfs mount -o ro' can\n\tbe used to temporarily "
10820 "mount existing encrypted datasets readonly.\n"));
10821 break;
10822
10823 case ZPOOL_ERRATA_ZOL_8308_ENCRYPTION:
10824 (void) strlcat(status, gettext("\tExisting encrypted "
10825 "snapshots and bookmarks contain an on-disk\n\t"
10826 "incompatibility. This may cause on-disk "
10827 "corruption if they are used\n\twith "
10828 "'zfs recv'.\n"), ST_SIZE);
10829 (void) snprintf(action, AC_SIZE,
10830 gettext("To correct the"
10831 "issue, enable the bookmark_v2 feature. No "
10832 "additional\n\taction is needed if there are no "
10833 "encrypted snapshots or bookmarks.\n\tIf preserving"
10834 "the encrypted snapshots and bookmarks is required,"
10835 " use\n\ta non-raw send to backup and restore them."
10836 " Alternately, they may be\n\tremoved to resolve "
10837 "the incompatibility.\n"));
10838 break;
10839
10840 default:
10841 /*
10842 * All errata which allow the pool to be imported
10843 * must contain an action message.
10844 */
10845 assert(0);
10846 }
10847 break;
10848
10849 default:
10850 /*
10851 * The remaining errors can't actually be generated, yet.
10852 */
10853 assert(reason == ZPOOL_STATUS_OK);
10854 }
10855
10856 if (status[0] != 0) {
10857 if (cbp->cb_json)
10858 fnvlist_add_string(item, "status", status);
10859 else {
10860 (void) printf_color(ANSI_BOLD, gettext("status: "));
10861 (void) printf_color(ANSI_YELLOW, status);
10862 }
10863 }
10864
10865 if (action[0] != 0) {
10866 if (cbp->cb_json)
10867 fnvlist_add_string(item, "action", action);
10868 else {
10869 (void) printf_color(ANSI_BOLD, gettext("action: "));
10870 (void) printf_color(ANSI_YELLOW, action);
10871 }
10872 }
10873 }
10874
10875 static int
status_callback_json(zpool_handle_t * zhp,void * data)10876 status_callback_json(zpool_handle_t *zhp, void *data)
10877 {
10878 status_cbdata_t *cbp = data;
10879 nvlist_t *config, *nvroot;
10880 const char *msgid;
10881 char pool_guid[256];
10882 char msgbuf[256];
10883 uint64_t guid;
10884 zpool_status_t reason;
10885 zpool_errata_t errata;
10886 uint_t c;
10887 vdev_stat_t *vs;
10888 nvlist_t *item, *d, *load_info, *vds;
10889
10890 /* If dedup stats were requested, also fetch dedupcached. */
10891 if (cbp->cb_dedup_stats > 1)
10892 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME);
10893 reason = zpool_get_status(zhp, &msgid, &errata);
10894 /*
10895 * If we were given 'zpool status -x', only report those pools with
10896 * problems.
10897 */
10898 if (cbp->cb_explain &&
10899 (reason == ZPOOL_STATUS_OK ||
10900 reason == ZPOOL_STATUS_VERSION_OLDER ||
10901 reason == ZPOOL_STATUS_FEAT_DISABLED ||
10902 reason == ZPOOL_STATUS_COMPATIBILITY_ERR ||
10903 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) {
10904 return (0);
10905 }
10906
10907 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "pools");
10908 item = fnvlist_alloc();
10909 vds = fnvlist_alloc();
10910 fill_pool_info(item, zhp, B_FALSE, cbp->cb_json_as_int);
10911 config = zpool_get_config(zhp, NULL);
10912
10913 if (config != NULL) {
10914 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
10915 verify(nvlist_lookup_uint64_array(nvroot,
10916 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &c) == 0);
10917 if (cbp->cb_json_pool_key_guid) {
10918 guid = fnvlist_lookup_uint64(config,
10919 ZPOOL_CONFIG_POOL_GUID);
10920 (void) snprintf(pool_guid, 256, "%llu",
10921 (u_longlong_t)guid);
10922 }
10923 cbp->cb_count++;
10924
10925 print_status_reason(zhp, cbp, reason, errata, item);
10926 if (msgid != NULL) {
10927 (void) snprintf(msgbuf, 256,
10928 "https://openzfs.github.io/openzfs-docs/msg/%s",
10929 msgid);
10930 fnvlist_add_string(item, "msgid", msgid);
10931 fnvlist_add_string(item, "moreinfo", msgbuf);
10932 }
10933
10934 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
10935 &load_info) == 0) {
10936 fnvlist_add_nvlist(item, ZPOOL_CONFIG_LOAD_INFO,
10937 load_info);
10938 }
10939
10940 scan_status_nvlist(zhp, cbp, nvroot, item);
10941 removal_status_nvlist(zhp, cbp, nvroot, item);
10942 checkpoint_status_nvlist(nvroot, cbp, item);
10943 raidz_expand_status_nvlist(zhp, cbp, nvroot, item);
10944 vdev_stats_nvlist(zhp, cbp, nvroot, 0, B_FALSE, NULL, vds);
10945 if (cbp->cb_flat_vdevs) {
10946 class_vdevs_nvlist(zhp, cbp, nvroot,
10947 VDEV_ALLOC_BIAS_DEDUP, vds);
10948 class_vdevs_nvlist(zhp, cbp, nvroot,
10949 VDEV_ALLOC_BIAS_SPECIAL, vds);
10950 class_vdevs_nvlist(zhp, cbp, nvroot,
10951 VDEV_ALLOC_CLASS_LOGS, vds);
10952 l2cache_nvlist(zhp, cbp, nvroot, vds);
10953 spares_nvlist(zhp, cbp, nvroot, vds);
10954
10955 fnvlist_add_nvlist(item, "vdevs", vds);
10956 fnvlist_free(vds);
10957 } else {
10958 fnvlist_add_nvlist(item, "vdevs", vds);
10959 fnvlist_free(vds);
10960
10961 class_vdevs_nvlist(zhp, cbp, nvroot,
10962 VDEV_ALLOC_BIAS_DEDUP, item);
10963 class_vdevs_nvlist(zhp, cbp, nvroot,
10964 VDEV_ALLOC_BIAS_SPECIAL, item);
10965 class_vdevs_nvlist(zhp, cbp, nvroot,
10966 VDEV_ALLOC_CLASS_LOGS, item);
10967 l2cache_nvlist(zhp, cbp, nvroot, item);
10968 spares_nvlist(zhp, cbp, nvroot, item);
10969 }
10970 dedup_stats_nvlist(zhp, cbp, item);
10971 errors_nvlist(zhp, cbp, item);
10972 }
10973 if (cbp->cb_json_pool_key_guid) {
10974 fnvlist_add_nvlist(d, pool_guid, item);
10975 } else {
10976 fnvlist_add_nvlist(d, zpool_get_name(zhp),
10977 item);
10978 }
10979 fnvlist_free(item);
10980 return (0);
10981 }
10982
10983 /*
10984 * Display a summary of pool status. Displays a summary such as:
10985 *
10986 * pool: tank
10987 * status: DEGRADED
10988 * reason: One or more devices ...
10989 * see: https://openzfs.github.io/openzfs-docs/msg/ZFS-xxxx-01
10990 * config:
10991 * mirror DEGRADED
10992 * c1t0d0 OK
10993 * c2t0d0 UNAVAIL
10994 *
10995 * When given the '-v' option, we print out the complete config. If the '-e'
10996 * option is specified, then we print out error rate information as well.
10997 */
10998 static int
status_callback(zpool_handle_t * zhp,void * data)10999 status_callback(zpool_handle_t *zhp, void *data)
11000 {
11001 status_cbdata_t *cbp = data;
11002 nvlist_t *config, *nvroot;
11003 const char *msgid;
11004 zpool_status_t reason;
11005 zpool_errata_t errata;
11006 const char *health;
11007 uint_t c;
11008 vdev_stat_t *vs;
11009
11010 /* If dedup stats were requested, also fetch dedupcached. */
11011 if (cbp->cb_dedup_stats > 1)
11012 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME);
11013
11014 config = zpool_get_config(zhp, NULL);
11015 reason = zpool_get_status(zhp, &msgid, &errata);
11016
11017 cbp->cb_count++;
11018
11019 /*
11020 * If we were given 'zpool status -x', only report those pools with
11021 * problems.
11022 */
11023 if (cbp->cb_explain &&
11024 (reason == ZPOOL_STATUS_OK ||
11025 reason == ZPOOL_STATUS_VERSION_OLDER ||
11026 reason == ZPOOL_STATUS_FEAT_DISABLED ||
11027 reason == ZPOOL_STATUS_COMPATIBILITY_ERR ||
11028 reason == ZPOOL_STATUS_INCOMPATIBLE_FEAT)) {
11029 if (!cbp->cb_allpools) {
11030 (void) printf(gettext("pool '%s' is healthy\n"),
11031 zpool_get_name(zhp));
11032 if (cbp->cb_first)
11033 cbp->cb_first = B_FALSE;
11034 }
11035 return (0);
11036 }
11037
11038 if (cbp->cb_first)
11039 cbp->cb_first = B_FALSE;
11040 else
11041 (void) printf("\n");
11042
11043 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
11044 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
11045 (uint64_t **)&vs, &c) == 0);
11046
11047 health = zpool_get_state_str(zhp);
11048
11049 printf(" ");
11050 (void) printf_color(ANSI_BOLD, gettext("pool:"));
11051 printf(" %s\n", zpool_get_name(zhp));
11052 (void) fputc(' ', stdout);
11053 (void) printf_color(ANSI_BOLD, gettext("state: "));
11054
11055 (void) printf_color(health_str_to_color(health), "%s", health);
11056
11057 (void) fputc('\n', stdout);
11058 print_status_reason(zhp, cbp, reason, errata, NULL);
11059
11060 if (msgid != NULL) {
11061 printf(" ");
11062 (void) printf_color(ANSI_BOLD, gettext("see:"));
11063 printf(gettext(
11064 " https://openzfs.github.io/openzfs-docs/msg/%s\n"),
11065 msgid);
11066 }
11067
11068 if (config != NULL) {
11069 uint64_t nerr;
11070 nvlist_t **spares, **l2cache;
11071 uint_t nspares, nl2cache;
11072
11073 print_scan_status(zhp, nvroot);
11074
11075 pool_removal_stat_t *prs = NULL;
11076 (void) nvlist_lookup_uint64_array(nvroot,
11077 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
11078 print_removal_status(zhp, prs);
11079
11080 pool_checkpoint_stat_t *pcs = NULL;
11081 (void) nvlist_lookup_uint64_array(nvroot,
11082 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
11083 print_checkpoint_status(pcs);
11084
11085 pool_raidz_expand_stat_t *pres = NULL;
11086 (void) nvlist_lookup_uint64_array(nvroot,
11087 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c);
11088 print_raidz_expand_status(zhp, pres);
11089
11090 cbp->cb_namewidth = max_width(zhp, nvroot, 0, 0,
11091 cbp->cb_name_flags | VDEV_NAME_TYPE_ID);
11092 if (cbp->cb_namewidth < 10)
11093 cbp->cb_namewidth = 10;
11094
11095 color_start(ANSI_BOLD);
11096 (void) printf(gettext("config:\n\n"));
11097 (void) printf(gettext("\t%-*s %-8s %5s %5s %5s"),
11098 cbp->cb_namewidth, "NAME", "STATE", "READ", "WRITE",
11099 "CKSUM");
11100 color_end();
11101
11102 if (cbp->cb_print_slow_ios) {
11103 (void) printf_color(ANSI_BOLD, " %5s", gettext("SLOW"));
11104 }
11105
11106 if (cbp->cb_print_power) {
11107 (void) printf_color(ANSI_BOLD, " %5s",
11108 gettext("POWER"));
11109 }
11110
11111 if (cbp->cb_print_dio_verify) {
11112 (void) printf_color(ANSI_BOLD, " %5s", gettext("DIO"));
11113 }
11114
11115 if (cbp->vcdl != NULL)
11116 print_cmd_columns(cbp->vcdl, 0);
11117
11118 printf("\n");
11119
11120 print_status_config(zhp, cbp, zpool_get_name(zhp), nvroot, 0,
11121 B_FALSE, NULL);
11122
11123 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_DEDUP);
11124 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_BIAS_SPECIAL);
11125 print_class_vdevs(zhp, cbp, nvroot, VDEV_ALLOC_CLASS_LOGS);
11126
11127 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
11128 &l2cache, &nl2cache) == 0)
11129 print_l2cache(zhp, cbp, l2cache, nl2cache);
11130
11131 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
11132 &spares, &nspares) == 0)
11133 print_spares(zhp, cbp, spares, nspares);
11134
11135 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
11136 &nerr) == 0) {
11137 (void) printf("\n");
11138 if (nerr == 0) {
11139 (void) printf(gettext(
11140 "errors: No known data errors\n"));
11141 } else if (!cbp->cb_verbose) {
11142 color_start(ANSI_RED);
11143 (void) printf(gettext("errors: %llu data "
11144 "errors, use '-v' for a list\n"),
11145 (u_longlong_t)nerr);
11146 color_end();
11147 } else {
11148 print_error_log(zhp);
11149 }
11150 }
11151
11152 if (cbp->cb_dedup_stats)
11153 print_dedup_stats(zhp, config, cbp->cb_literal);
11154 } else {
11155 (void) printf(gettext("config: The configuration cannot be "
11156 "determined.\n"));
11157 }
11158
11159 return (0);
11160 }
11161
11162 /*
11163 * zpool status [-dDegiLpPstvx] [-c [script1,script2,...]] ...
11164 * [-j|--json [--json-flat-vdevs] [--json-int] ...
11165 * [--json-pool-key-guid]] [--power] [-T d|u] ...
11166 * [pool] [interval [count]]
11167 *
11168 * -c CMD For each vdev, run command CMD
11169 * -D Display dedup status (undocumented)
11170 * -d Display Direct I/O write verify errors
11171 * -e Display only unhealthy vdevs
11172 * -g Display guid for individual vdev name.
11173 * -i Display vdev initialization status.
11174 * -j [...] Display output in JSON format
11175 * --json-flat-vdevs Display vdevs in flat hierarchy
11176 * --json-int Display numbers in integer format instead of string
11177 * --json-pool-key-guid Use pool GUID as key for pool objects
11178 * -L Follow links when resolving vdev path name.
11179 * -P Display full path for vdev name.
11180 * -p Display values in parsable (exact) format.
11181 * --power Display vdev enclosure slot power status
11182 * -s Display slow IOs column.
11183 * -T Display a timestamp in date(1) or Unix format
11184 * -t Display vdev TRIM status.
11185 * -v Display complete error logs
11186 * -x Display only pools with potential problems
11187 *
11188 * Describes the health status of all pools or some subset.
11189 */
11190 int
zpool_do_status(int argc,char ** argv)11191 zpool_do_status(int argc, char **argv)
11192 {
11193 int c;
11194 int ret;
11195 float interval = 0;
11196 unsigned long count = 0;
11197 status_cbdata_t cb = { 0 };
11198 nvlist_t *data;
11199 char *cmd = NULL;
11200
11201 struct option long_options[] = {
11202 {"power", no_argument, NULL, ZPOOL_OPTION_POWER},
11203 {"json", no_argument, NULL, 'j'},
11204 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT},
11205 {"json-flat-vdevs", no_argument, NULL,
11206 ZPOOL_OPTION_JSON_FLAT_VDEVS},
11207 {"json-pool-key-guid", no_argument, NULL,
11208 ZPOOL_OPTION_POOL_KEY_GUID},
11209 {0, 0, 0, 0}
11210 };
11211
11212 /* check options */
11213 while ((c = getopt_long(argc, argv, "c:jdDegiLpPstT:vx", long_options,
11214 NULL)) != -1) {
11215 switch (c) {
11216 case 'c':
11217 if (cmd != NULL) {
11218 fprintf(stderr,
11219 gettext("Can't set -c flag twice\n"));
11220 exit(1);
11221 }
11222
11223 if (getenv("ZPOOL_SCRIPTS_ENABLED") != NULL &&
11224 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_ENABLED")) {
11225 fprintf(stderr, gettext(
11226 "Can't run -c, disabled by "
11227 "ZPOOL_SCRIPTS_ENABLED.\n"));
11228 exit(1);
11229 }
11230
11231 if ((getuid() <= 0 || geteuid() <= 0) &&
11232 !libzfs_envvar_is_set("ZPOOL_SCRIPTS_AS_ROOT")) {
11233 fprintf(stderr, gettext(
11234 "Can't run -c with root privileges "
11235 "unless ZPOOL_SCRIPTS_AS_ROOT is set.\n"));
11236 exit(1);
11237 }
11238 cmd = optarg;
11239 break;
11240 case 'd':
11241 cb.cb_print_dio_verify = B_TRUE;
11242 break;
11243 case 'D':
11244 if (++cb.cb_dedup_stats > 2)
11245 cb.cb_dedup_stats = 2;
11246 break;
11247 case 'e':
11248 cb.cb_print_unhealthy = B_TRUE;
11249 break;
11250 case 'g':
11251 cb.cb_name_flags |= VDEV_NAME_GUID;
11252 break;
11253 case 'i':
11254 cb.cb_print_vdev_init = B_TRUE;
11255 break;
11256 case 'L':
11257 cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
11258 break;
11259 case 'p':
11260 cb.cb_literal = B_TRUE;
11261 break;
11262 case 'P':
11263 cb.cb_name_flags |= VDEV_NAME_PATH;
11264 break;
11265 case 's':
11266 cb.cb_print_slow_ios = B_TRUE;
11267 break;
11268 case 't':
11269 cb.cb_print_vdev_trim = B_TRUE;
11270 break;
11271 case 'T':
11272 get_timestamp_arg(*optarg);
11273 break;
11274 case 'v':
11275 cb.cb_verbose = B_TRUE;
11276 break;
11277 case 'j':
11278 cb.cb_json = B_TRUE;
11279 break;
11280 case 'x':
11281 cb.cb_explain = B_TRUE;
11282 break;
11283 case ZPOOL_OPTION_POWER:
11284 cb.cb_print_power = B_TRUE;
11285 break;
11286 case ZPOOL_OPTION_JSON_FLAT_VDEVS:
11287 cb.cb_flat_vdevs = B_TRUE;
11288 break;
11289 case ZPOOL_OPTION_JSON_NUMS_AS_INT:
11290 cb.cb_json_as_int = B_TRUE;
11291 cb.cb_literal = B_TRUE;
11292 break;
11293 case ZPOOL_OPTION_POOL_KEY_GUID:
11294 cb.cb_json_pool_key_guid = B_TRUE;
11295 break;
11296 case '?':
11297 if (optopt == 'c') {
11298 print_zpool_script_list("status");
11299 exit(0);
11300 } else {
11301 fprintf(stderr,
11302 gettext("invalid option '%c'\n"), optopt);
11303 }
11304 usage(B_FALSE);
11305 }
11306 }
11307
11308 argc -= optind;
11309 argv += optind;
11310
11311 get_interval_count(&argc, argv, &interval, &count);
11312
11313 if (argc == 0)
11314 cb.cb_allpools = B_TRUE;
11315
11316 cb.cb_first = B_TRUE;
11317 cb.cb_print_status = B_TRUE;
11318
11319 if (cb.cb_flat_vdevs && !cb.cb_json) {
11320 fprintf(stderr, gettext("'--json-flat-vdevs' only works with"
11321 " '-j' option\n"));
11322 usage(B_FALSE);
11323 }
11324
11325 if (cb.cb_json_as_int && !cb.cb_json) {
11326 (void) fprintf(stderr, gettext("'--json-int' only works with"
11327 " '-j' option\n"));
11328 usage(B_FALSE);
11329 }
11330
11331 if (!cb.cb_json && cb.cb_json_pool_key_guid) {
11332 (void) fprintf(stderr, gettext("'json-pool-key-guid' only"
11333 " works with '-j' option\n"));
11334 usage(B_FALSE);
11335 }
11336
11337 for (;;) {
11338 if (cb.cb_json) {
11339 cb.cb_jsobj = zpool_json_schema(0, 1);
11340 data = fnvlist_alloc();
11341 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data);
11342 fnvlist_free(data);
11343 }
11344
11345 if (timestamp_fmt != NODATE) {
11346 if (cb.cb_json) {
11347 if (cb.cb_json_as_int) {
11348 fnvlist_add_uint64(cb.cb_jsobj, "time",
11349 time(NULL));
11350 } else {
11351 char ts[128];
11352 get_timestamp(timestamp_fmt, ts, 128);
11353 fnvlist_add_string(cb.cb_jsobj, "time",
11354 ts);
11355 }
11356 } else
11357 print_timestamp(timestamp_fmt);
11358 }
11359
11360 if (cmd != NULL)
11361 cb.vcdl = all_pools_for_each_vdev_run(argc, argv, cmd,
11362 NULL, NULL, 0, 0);
11363
11364 if (cb.cb_json) {
11365 ret = for_each_pool(argc, argv, B_TRUE, NULL,
11366 ZFS_TYPE_POOL, cb.cb_literal,
11367 status_callback_json, &cb);
11368 } else {
11369 ret = for_each_pool(argc, argv, B_TRUE, NULL,
11370 ZFS_TYPE_POOL, cb.cb_literal,
11371 status_callback, &cb);
11372 }
11373
11374 if (cb.vcdl != NULL)
11375 free_vdev_cmd_data_list(cb.vcdl);
11376
11377 if (cb.cb_json) {
11378 if (ret == 0)
11379 zcmd_print_json(cb.cb_jsobj);
11380 else
11381 nvlist_free(cb.cb_jsobj);
11382 } else {
11383 if (argc == 0 && cb.cb_count == 0) {
11384 (void) fprintf(stderr, "%s",
11385 gettext("no pools available\n"));
11386 } else if (cb.cb_explain && cb.cb_first &&
11387 cb.cb_allpools) {
11388 (void) printf("%s",
11389 gettext("all pools are healthy\n"));
11390 }
11391 }
11392
11393 if (ret != 0)
11394 return (ret);
11395
11396 if (interval == 0)
11397 break;
11398
11399 if (count != 0 && --count == 0)
11400 break;
11401
11402 (void) fflush(stdout);
11403 (void) fsleep(interval);
11404 }
11405
11406 return (0);
11407 }
11408
11409 typedef struct upgrade_cbdata {
11410 int cb_first;
11411 int cb_argc;
11412 uint64_t cb_version;
11413 char **cb_argv;
11414 } upgrade_cbdata_t;
11415
11416 static int
check_unsupp_fs(zfs_handle_t * zhp,void * unsupp_fs)11417 check_unsupp_fs(zfs_handle_t *zhp, void *unsupp_fs)
11418 {
11419 int zfs_version = (int)zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
11420 int *count = (int *)unsupp_fs;
11421
11422 if (zfs_version > ZPL_VERSION) {
11423 (void) printf(gettext("%s (v%d) is not supported by this "
11424 "implementation of ZFS.\n"),
11425 zfs_get_name(zhp), zfs_version);
11426 (*count)++;
11427 }
11428
11429 (void) zfs_iter_filesystems_v2(zhp, 0, check_unsupp_fs, unsupp_fs);
11430
11431 zfs_close(zhp);
11432
11433 return (0);
11434 }
11435
11436 static int
upgrade_version(zpool_handle_t * zhp,uint64_t version)11437 upgrade_version(zpool_handle_t *zhp, uint64_t version)
11438 {
11439 int ret;
11440 nvlist_t *config;
11441 uint64_t oldversion;
11442 int unsupp_fs = 0;
11443
11444 config = zpool_get_config(zhp, NULL);
11445 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
11446 &oldversion) == 0);
11447
11448 char compat[ZFS_MAXPROPLEN];
11449 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
11450 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
11451 compat[0] = '\0';
11452
11453 assert(SPA_VERSION_IS_SUPPORTED(oldversion));
11454 assert(oldversion < version);
11455
11456 ret = zfs_iter_root(zpool_get_handle(zhp), check_unsupp_fs, &unsupp_fs);
11457 if (ret != 0)
11458 return (ret);
11459
11460 if (unsupp_fs) {
11461 (void) fprintf(stderr, gettext("Upgrade not performed due "
11462 "to %d unsupported filesystems (max v%d).\n"),
11463 unsupp_fs, (int)ZPL_VERSION);
11464 return (1);
11465 }
11466
11467 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
11468 (void) fprintf(stderr, gettext("Upgrade not performed because "
11469 "'compatibility' property set to '"
11470 ZPOOL_COMPAT_LEGACY "'.\n"));
11471 return (1);
11472 }
11473
11474 ret = zpool_upgrade(zhp, version);
11475 if (ret != 0)
11476 return (ret);
11477
11478 if (version >= SPA_VERSION_FEATURES) {
11479 (void) printf(gettext("Successfully upgraded "
11480 "'%s' from version %llu to feature flags.\n"),
11481 zpool_get_name(zhp), (u_longlong_t)oldversion);
11482 } else {
11483 (void) printf(gettext("Successfully upgraded "
11484 "'%s' from version %llu to version %llu.\n"),
11485 zpool_get_name(zhp), (u_longlong_t)oldversion,
11486 (u_longlong_t)version);
11487 }
11488
11489 return (0);
11490 }
11491
11492 static int
upgrade_enable_all(zpool_handle_t * zhp,int * countp)11493 upgrade_enable_all(zpool_handle_t *zhp, int *countp)
11494 {
11495 int i, ret, count;
11496 boolean_t firstff = B_TRUE;
11497 nvlist_t *enabled = zpool_get_features(zhp);
11498
11499 char compat[ZFS_MAXPROPLEN];
11500 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY, compat,
11501 ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
11502 compat[0] = '\0';
11503
11504 boolean_t requested_features[SPA_FEATURES];
11505 if (zpool_do_load_compat(compat, requested_features) !=
11506 ZPOOL_COMPATIBILITY_OK)
11507 return (-1);
11508
11509 count = 0;
11510 for (i = 0; i < SPA_FEATURES; i++) {
11511 const char *fname = spa_feature_table[i].fi_uname;
11512 const char *fguid = spa_feature_table[i].fi_guid;
11513
11514 if (!spa_feature_table[i].fi_zfs_mod_supported ||
11515 (spa_feature_table[i].fi_flags & ZFEATURE_FLAG_NO_UPGRADE))
11516 continue;
11517
11518 if (!nvlist_exists(enabled, fguid) && requested_features[i]) {
11519 char *propname;
11520 verify(-1 != asprintf(&propname, "feature@%s", fname));
11521 ret = zpool_set_prop(zhp, propname,
11522 ZFS_FEATURE_ENABLED);
11523 if (ret != 0) {
11524 free(propname);
11525 return (ret);
11526 }
11527 count++;
11528
11529 if (firstff) {
11530 (void) printf(gettext("Enabled the "
11531 "following features on '%s':\n"),
11532 zpool_get_name(zhp));
11533 firstff = B_FALSE;
11534 }
11535 (void) printf(gettext(" %s\n"), fname);
11536 free(propname);
11537 }
11538 }
11539
11540 if (countp != NULL)
11541 *countp = count;
11542 return (0);
11543 }
11544
11545 static int
upgrade_cb(zpool_handle_t * zhp,void * arg)11546 upgrade_cb(zpool_handle_t *zhp, void *arg)
11547 {
11548 upgrade_cbdata_t *cbp = arg;
11549 nvlist_t *config;
11550 uint64_t version;
11551 boolean_t modified_pool = B_FALSE;
11552 int ret;
11553
11554 config = zpool_get_config(zhp, NULL);
11555 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
11556 &version) == 0);
11557
11558 assert(SPA_VERSION_IS_SUPPORTED(version));
11559
11560 if (version < cbp->cb_version) {
11561 cbp->cb_first = B_FALSE;
11562 ret = upgrade_version(zhp, cbp->cb_version);
11563 if (ret != 0)
11564 return (ret);
11565 modified_pool = B_TRUE;
11566
11567 /*
11568 * If they did "zpool upgrade -a", then we could
11569 * be doing ioctls to different pools. We need
11570 * to log this history once to each pool, and bypass
11571 * the normal history logging that happens in main().
11572 */
11573 (void) zpool_log_history(g_zfs, history_str);
11574 log_history = B_FALSE;
11575 }
11576
11577 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
11578 int count;
11579 ret = upgrade_enable_all(zhp, &count);
11580 if (ret != 0)
11581 return (ret);
11582
11583 if (count > 0) {
11584 cbp->cb_first = B_FALSE;
11585 modified_pool = B_TRUE;
11586 }
11587 }
11588
11589 if (modified_pool) {
11590 (void) printf("\n");
11591 (void) after_zpool_upgrade(zhp);
11592 }
11593
11594 return (0);
11595 }
11596
11597 static int
upgrade_list_older_cb(zpool_handle_t * zhp,void * arg)11598 upgrade_list_older_cb(zpool_handle_t *zhp, void *arg)
11599 {
11600 upgrade_cbdata_t *cbp = arg;
11601 nvlist_t *config;
11602 uint64_t version;
11603
11604 config = zpool_get_config(zhp, NULL);
11605 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
11606 &version) == 0);
11607
11608 assert(SPA_VERSION_IS_SUPPORTED(version));
11609
11610 if (version < SPA_VERSION_FEATURES) {
11611 if (cbp->cb_first) {
11612 (void) printf(gettext("The following pools are "
11613 "formatted with legacy version numbers and can\n"
11614 "be upgraded to use feature flags. After "
11615 "being upgraded, these pools\nwill no "
11616 "longer be accessible by software that does not "
11617 "support feature\nflags.\n\n"
11618 "Note that setting a pool's 'compatibility' "
11619 "feature to '" ZPOOL_COMPAT_LEGACY "' will\n"
11620 "inhibit upgrades.\n\n"));
11621 (void) printf(gettext("VER POOL\n"));
11622 (void) printf(gettext("--- ------------\n"));
11623 cbp->cb_first = B_FALSE;
11624 }
11625
11626 (void) printf("%2llu %s\n", (u_longlong_t)version,
11627 zpool_get_name(zhp));
11628 }
11629
11630 return (0);
11631 }
11632
11633 static int
upgrade_list_disabled_cb(zpool_handle_t * zhp,void * arg)11634 upgrade_list_disabled_cb(zpool_handle_t *zhp, void *arg)
11635 {
11636 upgrade_cbdata_t *cbp = arg;
11637 nvlist_t *config;
11638 uint64_t version;
11639
11640 config = zpool_get_config(zhp, NULL);
11641 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
11642 &version) == 0);
11643
11644 if (version >= SPA_VERSION_FEATURES) {
11645 int i;
11646 boolean_t poolfirst = B_TRUE;
11647 nvlist_t *enabled = zpool_get_features(zhp);
11648
11649 for (i = 0; i < SPA_FEATURES; i++) {
11650 const char *fguid = spa_feature_table[i].fi_guid;
11651 const char *fname = spa_feature_table[i].fi_uname;
11652
11653 if (!spa_feature_table[i].fi_zfs_mod_supported)
11654 continue;
11655
11656 if (!nvlist_exists(enabled, fguid)) {
11657 if (cbp->cb_first) {
11658 (void) printf(gettext("\nSome "
11659 "supported features are not "
11660 "enabled on the following pools. "
11661 "Once a\nfeature is enabled the "
11662 "pool may become incompatible with "
11663 "software\nthat does not support "
11664 "the feature. See "
11665 "zpool-features(7) for "
11666 "details.\n\n"
11667 "Note that the pool "
11668 "'compatibility' feature can be "
11669 "used to inhibit\nfeature "
11670 "upgrades.\n\n"
11671 "Features marked with (*) are not "
11672 "applied automatically on upgrade, "
11673 "and\nmust be applied explicitly "
11674 "with zpool-set(7).\n\n"));
11675 (void) printf(gettext("POOL "
11676 "FEATURE\n"));
11677 (void) printf(gettext("------"
11678 "---------\n"));
11679 cbp->cb_first = B_FALSE;
11680 }
11681
11682 if (poolfirst) {
11683 (void) printf(gettext("%s\n"),
11684 zpool_get_name(zhp));
11685 poolfirst = B_FALSE;
11686 }
11687
11688 (void) printf(gettext(" %s%s\n"), fname,
11689 spa_feature_table[i].fi_flags &
11690 ZFEATURE_FLAG_NO_UPGRADE ? "(*)" : "");
11691 }
11692 /*
11693 * If they did "zpool upgrade -a", then we could
11694 * be doing ioctls to different pools. We need
11695 * to log this history once to each pool, and bypass
11696 * the normal history logging that happens in main().
11697 */
11698 (void) zpool_log_history(g_zfs, history_str);
11699 log_history = B_FALSE;
11700 }
11701 }
11702
11703 return (0);
11704 }
11705
11706 static int
upgrade_one(zpool_handle_t * zhp,void * data)11707 upgrade_one(zpool_handle_t *zhp, void *data)
11708 {
11709 boolean_t modified_pool = B_FALSE;
11710 upgrade_cbdata_t *cbp = data;
11711 uint64_t cur_version;
11712 int ret;
11713
11714 if (strcmp("log", zpool_get_name(zhp)) == 0) {
11715 (void) fprintf(stderr, gettext("'log' is now a reserved word\n"
11716 "Pool 'log' must be renamed using export and import"
11717 " to upgrade.\n"));
11718 return (1);
11719 }
11720
11721 cur_version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
11722 if (cur_version > cbp->cb_version) {
11723 (void) printf(gettext("Pool '%s' is already formatted "
11724 "using more current version '%llu'.\n\n"),
11725 zpool_get_name(zhp), (u_longlong_t)cur_version);
11726 return (0);
11727 }
11728
11729 if (cbp->cb_version != SPA_VERSION && cur_version == cbp->cb_version) {
11730 (void) printf(gettext("Pool '%s' is already formatted "
11731 "using version %llu.\n\n"), zpool_get_name(zhp),
11732 (u_longlong_t)cbp->cb_version);
11733 return (0);
11734 }
11735
11736 if (cur_version != cbp->cb_version) {
11737 modified_pool = B_TRUE;
11738 ret = upgrade_version(zhp, cbp->cb_version);
11739 if (ret != 0)
11740 return (ret);
11741 }
11742
11743 if (cbp->cb_version >= SPA_VERSION_FEATURES) {
11744 int count = 0;
11745 ret = upgrade_enable_all(zhp, &count);
11746 if (ret != 0)
11747 return (ret);
11748
11749 if (count != 0) {
11750 modified_pool = B_TRUE;
11751 } else if (cur_version == SPA_VERSION) {
11752 (void) printf(gettext("Pool '%s' already has all "
11753 "supported and requested features enabled.\n"),
11754 zpool_get_name(zhp));
11755 }
11756 }
11757
11758 if (modified_pool) {
11759 (void) printf("\n");
11760 (void) after_zpool_upgrade(zhp);
11761 }
11762
11763 return (0);
11764 }
11765
11766 /*
11767 * zpool upgrade
11768 * zpool upgrade -v
11769 * zpool upgrade [-V version] <-a | pool ...>
11770 *
11771 * With no arguments, display downrev'd ZFS pool available for upgrade.
11772 * Individual pools can be upgraded by specifying the pool, and '-a' will
11773 * upgrade all pools.
11774 */
11775 int
zpool_do_upgrade(int argc,char ** argv)11776 zpool_do_upgrade(int argc, char **argv)
11777 {
11778 int c;
11779 upgrade_cbdata_t cb = { 0 };
11780 int ret = 0;
11781 boolean_t showversions = B_FALSE;
11782 boolean_t upgradeall = B_FALSE;
11783 char *end;
11784
11785
11786 /* check options */
11787 while ((c = getopt(argc, argv, ":avV:")) != -1) {
11788 switch (c) {
11789 case 'a':
11790 upgradeall = B_TRUE;
11791 break;
11792 case 'v':
11793 showversions = B_TRUE;
11794 break;
11795 case 'V':
11796 cb.cb_version = strtoll(optarg, &end, 10);
11797 if (*end != '\0' ||
11798 !SPA_VERSION_IS_SUPPORTED(cb.cb_version)) {
11799 (void) fprintf(stderr,
11800 gettext("invalid version '%s'\n"), optarg);
11801 usage(B_FALSE);
11802 }
11803 break;
11804 case ':':
11805 (void) fprintf(stderr, gettext("missing argument for "
11806 "'%c' option\n"), optopt);
11807 usage(B_FALSE);
11808 break;
11809 case '?':
11810 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
11811 optopt);
11812 usage(B_FALSE);
11813 }
11814 }
11815
11816 cb.cb_argc = argc;
11817 cb.cb_argv = argv;
11818 argc -= optind;
11819 argv += optind;
11820
11821 if (cb.cb_version == 0) {
11822 cb.cb_version = SPA_VERSION;
11823 } else if (!upgradeall && argc == 0) {
11824 (void) fprintf(stderr, gettext("-V option is "
11825 "incompatible with other arguments\n"));
11826 usage(B_FALSE);
11827 }
11828
11829 if (showversions) {
11830 if (upgradeall || argc != 0) {
11831 (void) fprintf(stderr, gettext("-v option is "
11832 "incompatible with other arguments\n"));
11833 usage(B_FALSE);
11834 }
11835 } else if (upgradeall) {
11836 if (argc != 0) {
11837 (void) fprintf(stderr, gettext("-a option should not "
11838 "be used along with a pool name\n"));
11839 usage(B_FALSE);
11840 }
11841 }
11842
11843 (void) printf("%s", gettext("This system supports ZFS pool feature "
11844 "flags.\n\n"));
11845 if (showversions) {
11846 int i;
11847
11848 (void) printf(gettext("The following features are "
11849 "supported:\n\n"));
11850 (void) printf(gettext("FEAT DESCRIPTION\n"));
11851 (void) printf("----------------------------------------------"
11852 "---------------\n");
11853 for (i = 0; i < SPA_FEATURES; i++) {
11854 zfeature_info_t *fi = &spa_feature_table[i];
11855 if (!fi->fi_zfs_mod_supported)
11856 continue;
11857 const char *ro =
11858 (fi->fi_flags & ZFEATURE_FLAG_READONLY_COMPAT) ?
11859 " (read-only compatible)" : "";
11860
11861 (void) printf("%-37s%s\n", fi->fi_uname, ro);
11862 (void) printf(" %s\n", fi->fi_desc);
11863 }
11864 (void) printf("\n");
11865
11866 (void) printf(gettext("The following legacy versions are also "
11867 "supported:\n\n"));
11868 (void) printf(gettext("VER DESCRIPTION\n"));
11869 (void) printf("--- -----------------------------------------"
11870 "---------------\n");
11871 (void) printf(gettext(" 1 Initial ZFS version\n"));
11872 (void) printf(gettext(" 2 Ditto blocks "
11873 "(replicated metadata)\n"));
11874 (void) printf(gettext(" 3 Hot spares and double parity "
11875 "RAID-Z\n"));
11876 (void) printf(gettext(" 4 zpool history\n"));
11877 (void) printf(gettext(" 5 Compression using the gzip "
11878 "algorithm\n"));
11879 (void) printf(gettext(" 6 bootfs pool property\n"));
11880 (void) printf(gettext(" 7 Separate intent log devices\n"));
11881 (void) printf(gettext(" 8 Delegated administration\n"));
11882 (void) printf(gettext(" 9 refquota and refreservation "
11883 "properties\n"));
11884 (void) printf(gettext(" 10 Cache devices\n"));
11885 (void) printf(gettext(" 11 Improved scrub performance\n"));
11886 (void) printf(gettext(" 12 Snapshot properties\n"));
11887 (void) printf(gettext(" 13 snapused property\n"));
11888 (void) printf(gettext(" 14 passthrough-x aclinherit\n"));
11889 (void) printf(gettext(" 15 user/group space accounting\n"));
11890 (void) printf(gettext(" 16 stmf property support\n"));
11891 (void) printf(gettext(" 17 Triple-parity RAID-Z\n"));
11892 (void) printf(gettext(" 18 Snapshot user holds\n"));
11893 (void) printf(gettext(" 19 Log device removal\n"));
11894 (void) printf(gettext(" 20 Compression using zle "
11895 "(zero-length encoding)\n"));
11896 (void) printf(gettext(" 21 Deduplication\n"));
11897 (void) printf(gettext(" 22 Received properties\n"));
11898 (void) printf(gettext(" 23 Slim ZIL\n"));
11899 (void) printf(gettext(" 24 System attributes\n"));
11900 (void) printf(gettext(" 25 Improved scrub stats\n"));
11901 (void) printf(gettext(" 26 Improved snapshot deletion "
11902 "performance\n"));
11903 (void) printf(gettext(" 27 Improved snapshot creation "
11904 "performance\n"));
11905 (void) printf(gettext(" 28 Multiple vdev replacements\n"));
11906 (void) printf(gettext("\nFor more information on a particular "
11907 "version, including supported releases,\n"));
11908 (void) printf(gettext("see the ZFS Administration Guide.\n\n"));
11909 } else if (argc == 0 && upgradeall) {
11910 cb.cb_first = B_TRUE;
11911 ret = zpool_iter(g_zfs, upgrade_cb, &cb);
11912 if (ret == 0 && cb.cb_first) {
11913 if (cb.cb_version == SPA_VERSION) {
11914 (void) printf(gettext("All pools are already "
11915 "formatted using feature flags.\n\n"));
11916 (void) printf(gettext("Every feature flags "
11917 "pool already has all supported and "
11918 "requested features enabled.\n"));
11919 } else {
11920 (void) printf(gettext("All pools are already "
11921 "formatted with version %llu or higher.\n"),
11922 (u_longlong_t)cb.cb_version);
11923 }
11924 }
11925 } else if (argc == 0) {
11926 cb.cb_first = B_TRUE;
11927 ret = zpool_iter(g_zfs, upgrade_list_older_cb, &cb);
11928 assert(ret == 0);
11929
11930 if (cb.cb_first) {
11931 (void) printf(gettext("All pools are formatted "
11932 "using feature flags.\n\n"));
11933 } else {
11934 (void) printf(gettext("\nUse 'zpool upgrade -v' "
11935 "for a list of available legacy versions.\n"));
11936 }
11937
11938 cb.cb_first = B_TRUE;
11939 ret = zpool_iter(g_zfs, upgrade_list_disabled_cb, &cb);
11940 assert(ret == 0);
11941
11942 if (cb.cb_first) {
11943 (void) printf(gettext("Every feature flags pool has "
11944 "all supported and requested features enabled.\n"));
11945 } else {
11946 (void) printf(gettext("\n"));
11947 }
11948 } else {
11949 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
11950 B_FALSE, upgrade_one, &cb);
11951 }
11952
11953 return (ret);
11954 }
11955
11956 typedef struct hist_cbdata {
11957 boolean_t first;
11958 boolean_t longfmt;
11959 boolean_t internal;
11960 } hist_cbdata_t;
11961
11962 static void
print_history_records(nvlist_t * nvhis,hist_cbdata_t * cb)11963 print_history_records(nvlist_t *nvhis, hist_cbdata_t *cb)
11964 {
11965 nvlist_t **records;
11966 uint_t numrecords;
11967 int i;
11968
11969 verify(nvlist_lookup_nvlist_array(nvhis, ZPOOL_HIST_RECORD,
11970 &records, &numrecords) == 0);
11971 for (i = 0; i < numrecords; i++) {
11972 nvlist_t *rec = records[i];
11973 char tbuf[64] = "";
11974
11975 if (nvlist_exists(rec, ZPOOL_HIST_TIME)) {
11976 time_t tsec;
11977 struct tm t;
11978
11979 tsec = fnvlist_lookup_uint64(records[i],
11980 ZPOOL_HIST_TIME);
11981 (void) localtime_r(&tsec, &t);
11982 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11983 }
11984
11985 if (nvlist_exists(rec, ZPOOL_HIST_ELAPSED_NS)) {
11986 uint64_t elapsed_ns = fnvlist_lookup_int64(records[i],
11987 ZPOOL_HIST_ELAPSED_NS);
11988 (void) snprintf(tbuf + strlen(tbuf),
11989 sizeof (tbuf) - strlen(tbuf),
11990 " (%lldms)", (long long)elapsed_ns / 1000 / 1000);
11991 }
11992
11993 if (nvlist_exists(rec, ZPOOL_HIST_CMD)) {
11994 (void) printf("%s %s", tbuf,
11995 fnvlist_lookup_string(rec, ZPOOL_HIST_CMD));
11996 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_EVENT)) {
11997 int ievent =
11998 fnvlist_lookup_uint64(rec, ZPOOL_HIST_INT_EVENT);
11999 if (!cb->internal)
12000 continue;
12001 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) {
12002 (void) printf("%s unrecognized record:\n",
12003 tbuf);
12004 dump_nvlist(rec, 4);
12005 continue;
12006 }
12007 (void) printf("%s [internal %s txg:%lld] %s", tbuf,
12008 zfs_history_event_names[ievent],
12009 (longlong_t)fnvlist_lookup_uint64(
12010 rec, ZPOOL_HIST_TXG),
12011 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_STR));
12012 } else if (nvlist_exists(rec, ZPOOL_HIST_INT_NAME)) {
12013 if (!cb->internal)
12014 continue;
12015 (void) printf("%s [txg:%lld] %s", tbuf,
12016 (longlong_t)fnvlist_lookup_uint64(
12017 rec, ZPOOL_HIST_TXG),
12018 fnvlist_lookup_string(rec, ZPOOL_HIST_INT_NAME));
12019 if (nvlist_exists(rec, ZPOOL_HIST_DSNAME)) {
12020 (void) printf(" %s (%llu)",
12021 fnvlist_lookup_string(rec,
12022 ZPOOL_HIST_DSNAME),
12023 (u_longlong_t)fnvlist_lookup_uint64(rec,
12024 ZPOOL_HIST_DSID));
12025 }
12026 (void) printf(" %s", fnvlist_lookup_string(rec,
12027 ZPOOL_HIST_INT_STR));
12028 } else if (nvlist_exists(rec, ZPOOL_HIST_IOCTL)) {
12029 if (!cb->internal)
12030 continue;
12031 (void) printf("%s ioctl %s\n", tbuf,
12032 fnvlist_lookup_string(rec, ZPOOL_HIST_IOCTL));
12033 if (nvlist_exists(rec, ZPOOL_HIST_INPUT_NVL)) {
12034 (void) printf(" input:\n");
12035 dump_nvlist(fnvlist_lookup_nvlist(rec,
12036 ZPOOL_HIST_INPUT_NVL), 8);
12037 }
12038 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_NVL)) {
12039 (void) printf(" output:\n");
12040 dump_nvlist(fnvlist_lookup_nvlist(rec,
12041 ZPOOL_HIST_OUTPUT_NVL), 8);
12042 }
12043 if (nvlist_exists(rec, ZPOOL_HIST_OUTPUT_SIZE)) {
12044 (void) printf(" output nvlist omitted; "
12045 "original size: %lldKB\n",
12046 (longlong_t)fnvlist_lookup_int64(rec,
12047 ZPOOL_HIST_OUTPUT_SIZE) / 1024);
12048 }
12049 if (nvlist_exists(rec, ZPOOL_HIST_ERRNO)) {
12050 (void) printf(" errno: %lld\n",
12051 (longlong_t)fnvlist_lookup_int64(rec,
12052 ZPOOL_HIST_ERRNO));
12053 }
12054 } else {
12055 if (!cb->internal)
12056 continue;
12057 (void) printf("%s unrecognized record:\n", tbuf);
12058 dump_nvlist(rec, 4);
12059 }
12060
12061 if (!cb->longfmt) {
12062 (void) printf("\n");
12063 continue;
12064 }
12065 (void) printf(" [");
12066 if (nvlist_exists(rec, ZPOOL_HIST_WHO)) {
12067 uid_t who = fnvlist_lookup_uint64(rec, ZPOOL_HIST_WHO);
12068 struct passwd *pwd = getpwuid(who);
12069 (void) printf("user %d ", (int)who);
12070 if (pwd != NULL)
12071 (void) printf("(%s) ", pwd->pw_name);
12072 }
12073 if (nvlist_exists(rec, ZPOOL_HIST_HOST)) {
12074 (void) printf("on %s",
12075 fnvlist_lookup_string(rec, ZPOOL_HIST_HOST));
12076 }
12077 if (nvlist_exists(rec, ZPOOL_HIST_ZONE)) {
12078 (void) printf(":%s",
12079 fnvlist_lookup_string(rec, ZPOOL_HIST_ZONE));
12080 }
12081
12082 (void) printf("]");
12083 (void) printf("\n");
12084 }
12085 }
12086
12087 /*
12088 * Print out the command history for a specific pool.
12089 */
12090 static int
get_history_one(zpool_handle_t * zhp,void * data)12091 get_history_one(zpool_handle_t *zhp, void *data)
12092 {
12093 nvlist_t *nvhis;
12094 int ret;
12095 hist_cbdata_t *cb = (hist_cbdata_t *)data;
12096 uint64_t off = 0;
12097 boolean_t eof = B_FALSE;
12098
12099 cb->first = B_FALSE;
12100
12101 (void) printf(gettext("History for '%s':\n"), zpool_get_name(zhp));
12102
12103 while (!eof) {
12104 if ((ret = zpool_get_history(zhp, &nvhis, &off, &eof)) != 0)
12105 return (ret);
12106
12107 print_history_records(nvhis, cb);
12108 nvlist_free(nvhis);
12109 }
12110 (void) printf("\n");
12111
12112 return (ret);
12113 }
12114
12115 /*
12116 * zpool history <pool>
12117 *
12118 * Displays the history of commands that modified pools.
12119 */
12120 int
zpool_do_history(int argc,char ** argv)12121 zpool_do_history(int argc, char **argv)
12122 {
12123 hist_cbdata_t cbdata = { 0 };
12124 int ret;
12125 int c;
12126
12127 cbdata.first = B_TRUE;
12128 /* check options */
12129 while ((c = getopt(argc, argv, "li")) != -1) {
12130 switch (c) {
12131 case 'l':
12132 cbdata.longfmt = B_TRUE;
12133 break;
12134 case 'i':
12135 cbdata.internal = B_TRUE;
12136 break;
12137 case '?':
12138 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
12139 optopt);
12140 usage(B_FALSE);
12141 }
12142 }
12143 argc -= optind;
12144 argv += optind;
12145
12146 ret = for_each_pool(argc, argv, B_FALSE, NULL, ZFS_TYPE_POOL,
12147 B_FALSE, get_history_one, &cbdata);
12148
12149 if (argc == 0 && cbdata.first == B_TRUE) {
12150 (void) fprintf(stderr, gettext("no pools available\n"));
12151 return (0);
12152 }
12153
12154 return (ret);
12155 }
12156
12157 typedef struct ev_opts {
12158 int verbose;
12159 int scripted;
12160 int follow;
12161 int clear;
12162 char poolname[ZFS_MAX_DATASET_NAME_LEN];
12163 } ev_opts_t;
12164
12165 static void
zpool_do_events_short(nvlist_t * nvl,ev_opts_t * opts)12166 zpool_do_events_short(nvlist_t *nvl, ev_opts_t *opts)
12167 {
12168 char ctime_str[26], str[32];
12169 const char *ptr;
12170 int64_t *tv;
12171 uint_t n;
12172
12173 verify(nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tv, &n) == 0);
12174 memset(str, ' ', 32);
12175 (void) ctime_r((const time_t *)&tv[0], ctime_str);
12176 (void) memcpy(str, ctime_str+4, 6); /* 'Jun 30' */
12177 (void) memcpy(str+7, ctime_str+20, 4); /* '1993' */
12178 (void) memcpy(str+12, ctime_str+11, 8); /* '21:49:08' */
12179 (void) sprintf(str+20, ".%09lld", (longlong_t)tv[1]); /* '.123456789' */
12180 if (opts->scripted)
12181 (void) printf(gettext("%s\t"), str);
12182 else
12183 (void) printf(gettext("%s "), str);
12184
12185 verify(nvlist_lookup_string(nvl, FM_CLASS, &ptr) == 0);
12186 (void) printf(gettext("%s\n"), ptr);
12187 }
12188
12189 static void
zpool_do_events_nvprint(nvlist_t * nvl,int depth)12190 zpool_do_events_nvprint(nvlist_t *nvl, int depth)
12191 {
12192 nvpair_t *nvp;
12193 static char flagstr[256];
12194
12195 for (nvp = nvlist_next_nvpair(nvl, NULL);
12196 nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
12197
12198 data_type_t type = nvpair_type(nvp);
12199 const char *name = nvpair_name(nvp);
12200
12201 boolean_t b;
12202 uint8_t i8;
12203 uint16_t i16;
12204 uint32_t i32;
12205 uint64_t i64;
12206 const char *str;
12207 nvlist_t *cnv;
12208
12209 printf(gettext("%*s%s = "), depth, "", name);
12210
12211 switch (type) {
12212 case DATA_TYPE_BOOLEAN:
12213 printf(gettext("%s"), "1");
12214 break;
12215
12216 case DATA_TYPE_BOOLEAN_VALUE:
12217 (void) nvpair_value_boolean_value(nvp, &b);
12218 printf(gettext("%s"), b ? "1" : "0");
12219 break;
12220
12221 case DATA_TYPE_BYTE:
12222 (void) nvpair_value_byte(nvp, &i8);
12223 printf(gettext("0x%x"), i8);
12224 break;
12225
12226 case DATA_TYPE_INT8:
12227 (void) nvpair_value_int8(nvp, (void *)&i8);
12228 printf(gettext("0x%x"), i8);
12229 break;
12230
12231 case DATA_TYPE_UINT8:
12232 (void) nvpair_value_uint8(nvp, &i8);
12233 printf(gettext("0x%x"), i8);
12234 break;
12235
12236 case DATA_TYPE_INT16:
12237 (void) nvpair_value_int16(nvp, (void *)&i16);
12238 printf(gettext("0x%x"), i16);
12239 break;
12240
12241 case DATA_TYPE_UINT16:
12242 (void) nvpair_value_uint16(nvp, &i16);
12243 printf(gettext("0x%x"), i16);
12244 break;
12245
12246 case DATA_TYPE_INT32:
12247 (void) nvpair_value_int32(nvp, (void *)&i32);
12248 printf(gettext("0x%x"), i32);
12249 break;
12250
12251 case DATA_TYPE_UINT32:
12252 (void) nvpair_value_uint32(nvp, &i32);
12253 if (strcmp(name,
12254 FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE) == 0 ||
12255 strcmp(name,
12256 FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE) == 0) {
12257 (void) zfs_valstr_zio_stage(i32, flagstr,
12258 sizeof (flagstr));
12259 printf(gettext("0x%x [%s]"), i32, flagstr);
12260 } else if (strcmp(name,
12261 FM_EREPORT_PAYLOAD_ZFS_ZIO_TYPE) == 0) {
12262 (void) zfs_valstr_zio_type(i32, flagstr,
12263 sizeof (flagstr));
12264 printf(gettext("0x%x [%s]"), i32, flagstr);
12265 } else if (strcmp(name,
12266 FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY) == 0) {
12267 (void) zfs_valstr_zio_priority(i32, flagstr,
12268 sizeof (flagstr));
12269 printf(gettext("0x%x [%s]"), i32, flagstr);
12270 } else {
12271 printf(gettext("0x%x"), i32);
12272 }
12273 break;
12274
12275 case DATA_TYPE_INT64:
12276 (void) nvpair_value_int64(nvp, (void *)&i64);
12277 printf(gettext("0x%llx"), (u_longlong_t)i64);
12278 break;
12279
12280 case DATA_TYPE_UINT64:
12281 (void) nvpair_value_uint64(nvp, &i64);
12282 /*
12283 * translate vdev state values to readable
12284 * strings to aide zpool events consumers
12285 */
12286 if (strcmp(name,
12287 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE) == 0 ||
12288 strcmp(name,
12289 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE) == 0) {
12290 printf(gettext("\"%s\" (0x%llx)"),
12291 zpool_state_to_name(i64, VDEV_AUX_NONE),
12292 (u_longlong_t)i64);
12293 } else if (strcmp(name,
12294 FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS) == 0) {
12295 (void) zfs_valstr_zio_flag(i64, flagstr,
12296 sizeof (flagstr));
12297 printf(gettext("0x%llx [%s]"),
12298 (u_longlong_t)i64, flagstr);
12299 } else {
12300 printf(gettext("0x%llx"), (u_longlong_t)i64);
12301 }
12302 break;
12303
12304 case DATA_TYPE_HRTIME:
12305 (void) nvpair_value_hrtime(nvp, (void *)&i64);
12306 printf(gettext("0x%llx"), (u_longlong_t)i64);
12307 break;
12308
12309 case DATA_TYPE_STRING:
12310 (void) nvpair_value_string(nvp, &str);
12311 printf(gettext("\"%s\""), str ? str : "<NULL>");
12312 break;
12313
12314 case DATA_TYPE_NVLIST:
12315 printf(gettext("(embedded nvlist)\n"));
12316 (void) nvpair_value_nvlist(nvp, &cnv);
12317 zpool_do_events_nvprint(cnv, depth + 8);
12318 printf(gettext("%*s(end %s)"), depth, "", name);
12319 break;
12320
12321 case DATA_TYPE_NVLIST_ARRAY: {
12322 nvlist_t **val;
12323 uint_t i, nelem;
12324
12325 (void) nvpair_value_nvlist_array(nvp, &val, &nelem);
12326 printf(gettext("(%d embedded nvlists)\n"), nelem);
12327 for (i = 0; i < nelem; i++) {
12328 printf(gettext("%*s%s[%d] = %s\n"),
12329 depth, "", name, i, "(embedded nvlist)");
12330 zpool_do_events_nvprint(val[i], depth + 8);
12331 printf(gettext("%*s(end %s[%i])\n"),
12332 depth, "", name, i);
12333 }
12334 printf(gettext("%*s(end %s)\n"), depth, "", name);
12335 }
12336 break;
12337
12338 case DATA_TYPE_INT8_ARRAY: {
12339 int8_t *val;
12340 uint_t i, nelem;
12341
12342 (void) nvpair_value_int8_array(nvp, &val, &nelem);
12343 for (i = 0; i < nelem; i++)
12344 printf(gettext("0x%x "), val[i]);
12345
12346 break;
12347 }
12348
12349 case DATA_TYPE_UINT8_ARRAY: {
12350 uint8_t *val;
12351 uint_t i, nelem;
12352
12353 (void) nvpair_value_uint8_array(nvp, &val, &nelem);
12354 for (i = 0; i < nelem; i++)
12355 printf(gettext("0x%x "), val[i]);
12356
12357 break;
12358 }
12359
12360 case DATA_TYPE_INT16_ARRAY: {
12361 int16_t *val;
12362 uint_t i, nelem;
12363
12364 (void) nvpair_value_int16_array(nvp, &val, &nelem);
12365 for (i = 0; i < nelem; i++)
12366 printf(gettext("0x%x "), val[i]);
12367
12368 break;
12369 }
12370
12371 case DATA_TYPE_UINT16_ARRAY: {
12372 uint16_t *val;
12373 uint_t i, nelem;
12374
12375 (void) nvpair_value_uint16_array(nvp, &val, &nelem);
12376 for (i = 0; i < nelem; i++)
12377 printf(gettext("0x%x "), val[i]);
12378
12379 break;
12380 }
12381
12382 case DATA_TYPE_INT32_ARRAY: {
12383 int32_t *val;
12384 uint_t i, nelem;
12385
12386 (void) nvpair_value_int32_array(nvp, &val, &nelem);
12387 for (i = 0; i < nelem; i++)
12388 printf(gettext("0x%x "), val[i]);
12389
12390 break;
12391 }
12392
12393 case DATA_TYPE_UINT32_ARRAY: {
12394 uint32_t *val;
12395 uint_t i, nelem;
12396
12397 (void) nvpair_value_uint32_array(nvp, &val, &nelem);
12398 for (i = 0; i < nelem; i++)
12399 printf(gettext("0x%x "), val[i]);
12400
12401 break;
12402 }
12403
12404 case DATA_TYPE_INT64_ARRAY: {
12405 int64_t *val;
12406 uint_t i, nelem;
12407
12408 (void) nvpair_value_int64_array(nvp, &val, &nelem);
12409 for (i = 0; i < nelem; i++)
12410 printf(gettext("0x%llx "),
12411 (u_longlong_t)val[i]);
12412
12413 break;
12414 }
12415
12416 case DATA_TYPE_UINT64_ARRAY: {
12417 uint64_t *val;
12418 uint_t i, nelem;
12419
12420 (void) nvpair_value_uint64_array(nvp, &val, &nelem);
12421 for (i = 0; i < nelem; i++)
12422 printf(gettext("0x%llx "),
12423 (u_longlong_t)val[i]);
12424
12425 break;
12426 }
12427
12428 case DATA_TYPE_STRING_ARRAY: {
12429 const char **str;
12430 uint_t i, nelem;
12431
12432 (void) nvpair_value_string_array(nvp, &str, &nelem);
12433 for (i = 0; i < nelem; i++)
12434 printf(gettext("\"%s\" "),
12435 str[i] ? str[i] : "<NULL>");
12436
12437 break;
12438 }
12439
12440 case DATA_TYPE_BOOLEAN_ARRAY:
12441 case DATA_TYPE_BYTE_ARRAY:
12442 case DATA_TYPE_DOUBLE:
12443 case DATA_TYPE_DONTCARE:
12444 case DATA_TYPE_UNKNOWN:
12445 printf(gettext("<unknown>"));
12446 break;
12447 }
12448
12449 printf(gettext("\n"));
12450 }
12451 }
12452
12453 static int
zpool_do_events_next(ev_opts_t * opts)12454 zpool_do_events_next(ev_opts_t *opts)
12455 {
12456 nvlist_t *nvl;
12457 int zevent_fd, ret, dropped;
12458 const char *pool;
12459
12460 zevent_fd = open(ZFS_DEV, O_RDWR);
12461 VERIFY(zevent_fd >= 0);
12462
12463 if (!opts->scripted)
12464 (void) printf(gettext("%-30s %s\n"), "TIME", "CLASS");
12465
12466 while (1) {
12467 ret = zpool_events_next(g_zfs, &nvl, &dropped,
12468 (opts->follow ? ZEVENT_NONE : ZEVENT_NONBLOCK), zevent_fd);
12469 if (ret || nvl == NULL)
12470 break;
12471
12472 if (dropped > 0)
12473 (void) printf(gettext("dropped %d events\n"), dropped);
12474
12475 if (strlen(opts->poolname) > 0 &&
12476 nvlist_lookup_string(nvl, FM_FMRI_ZFS_POOL, &pool) == 0 &&
12477 strcmp(opts->poolname, pool) != 0)
12478 continue;
12479
12480 zpool_do_events_short(nvl, opts);
12481
12482 if (opts->verbose) {
12483 zpool_do_events_nvprint(nvl, 8);
12484 printf(gettext("\n"));
12485 }
12486 (void) fflush(stdout);
12487
12488 nvlist_free(nvl);
12489 }
12490
12491 VERIFY0(close(zevent_fd));
12492
12493 return (ret);
12494 }
12495
12496 static int
zpool_do_events_clear(void)12497 zpool_do_events_clear(void)
12498 {
12499 int count, ret;
12500
12501 ret = zpool_events_clear(g_zfs, &count);
12502 if (!ret)
12503 (void) printf(gettext("cleared %d events\n"), count);
12504
12505 return (ret);
12506 }
12507
12508 /*
12509 * zpool events [-vHf [pool] | -c]
12510 *
12511 * Displays events logs by ZFS.
12512 */
12513 int
zpool_do_events(int argc,char ** argv)12514 zpool_do_events(int argc, char **argv)
12515 {
12516 ev_opts_t opts = { 0 };
12517 int ret;
12518 int c;
12519
12520 /* check options */
12521 while ((c = getopt(argc, argv, "vHfc")) != -1) {
12522 switch (c) {
12523 case 'v':
12524 opts.verbose = 1;
12525 break;
12526 case 'H':
12527 opts.scripted = 1;
12528 break;
12529 case 'f':
12530 opts.follow = 1;
12531 break;
12532 case 'c':
12533 opts.clear = 1;
12534 break;
12535 case '?':
12536 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
12537 optopt);
12538 usage(B_FALSE);
12539 }
12540 }
12541 argc -= optind;
12542 argv += optind;
12543
12544 if (argc > 1) {
12545 (void) fprintf(stderr, gettext("too many arguments\n"));
12546 usage(B_FALSE);
12547 } else if (argc == 1) {
12548 (void) strlcpy(opts.poolname, argv[0], sizeof (opts.poolname));
12549 if (!zfs_name_valid(opts.poolname, ZFS_TYPE_POOL)) {
12550 (void) fprintf(stderr,
12551 gettext("invalid pool name '%s'\n"), opts.poolname);
12552 usage(B_FALSE);
12553 }
12554 }
12555
12556 if ((argc == 1 || opts.verbose || opts.scripted || opts.follow) &&
12557 opts.clear) {
12558 (void) fprintf(stderr,
12559 gettext("invalid options combined with -c\n"));
12560 usage(B_FALSE);
12561 }
12562
12563 if (opts.clear)
12564 ret = zpool_do_events_clear();
12565 else
12566 ret = zpool_do_events_next(&opts);
12567
12568 return (ret);
12569 }
12570
12571 static int
get_callback_vdev(zpool_handle_t * zhp,char * vdevname,void * data)12572 get_callback_vdev(zpool_handle_t *zhp, char *vdevname, void *data)
12573 {
12574 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
12575 char value[ZFS_MAXPROPLEN];
12576 zprop_source_t srctype;
12577 nvlist_t *props, *item, *d;
12578 props = item = d = NULL;
12579
12580 if (cbp->cb_json) {
12581 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "vdevs");
12582 if (d == NULL) {
12583 fprintf(stderr, "vdevs obj not found.\n");
12584 exit(1);
12585 }
12586 props = fnvlist_alloc();
12587 }
12588
12589 for (zprop_list_t *pl = cbp->cb_proplist; pl != NULL;
12590 pl = pl->pl_next) {
12591 char *prop_name;
12592 /*
12593 * If the first property is pool name, it is a special
12594 * placeholder that we can skip. This will also skip
12595 * over the name property when 'all' is specified.
12596 */
12597 if (pl->pl_prop == ZPOOL_PROP_NAME &&
12598 pl == cbp->cb_proplist)
12599 continue;
12600
12601 if (pl->pl_prop == ZPROP_INVAL) {
12602 prop_name = pl->pl_user_prop;
12603 } else {
12604 prop_name = (char *)vdev_prop_to_name(pl->pl_prop);
12605 }
12606 if (zpool_get_vdev_prop(zhp, vdevname, pl->pl_prop,
12607 prop_name, value, sizeof (value), &srctype,
12608 cbp->cb_literal) == 0) {
12609 (void) zprop_collect_property(vdevname, cbp, prop_name,
12610 value, srctype, NULL, NULL, props);
12611 }
12612 }
12613
12614 if (cbp->cb_json) {
12615 if (!nvlist_empty(props)) {
12616 item = fnvlist_alloc();
12617 fill_vdev_info(item, zhp, vdevname, B_TRUE,
12618 cbp->cb_json_as_int);
12619 fnvlist_add_nvlist(item, "properties", props);
12620 fnvlist_add_nvlist(d, vdevname, item);
12621 fnvlist_add_nvlist(cbp->cb_jsobj, "vdevs", d);
12622 fnvlist_free(item);
12623 }
12624 fnvlist_free(props);
12625 }
12626
12627 return (0);
12628 }
12629
12630 static int
get_callback_vdev_cb(void * zhp_data,nvlist_t * nv,void * data)12631 get_callback_vdev_cb(void *zhp_data, nvlist_t *nv, void *data)
12632 {
12633 zpool_handle_t *zhp = zhp_data;
12634 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
12635 char *vdevname;
12636 const char *type;
12637 int ret;
12638
12639 /*
12640 * zpool_vdev_name() transforms the root vdev name (i.e., root-0) to the
12641 * pool name for display purposes, which is not desired. Fallback to
12642 * zpool_vdev_name() when not dealing with the root vdev.
12643 */
12644 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE);
12645 if (zhp != NULL && strcmp(type, "root") == 0)
12646 vdevname = strdup("root-0");
12647 else
12648 vdevname = zpool_vdev_name(g_zfs, zhp, nv,
12649 cbp->cb_vdevs.cb_name_flags);
12650
12651 (void) vdev_expand_proplist(zhp, vdevname, &cbp->cb_proplist);
12652
12653 ret = get_callback_vdev(zhp, vdevname, data);
12654
12655 free(vdevname);
12656
12657 return (ret);
12658 }
12659
12660 static int
get_callback(zpool_handle_t * zhp,void * data)12661 get_callback(zpool_handle_t *zhp, void *data)
12662 {
12663 zprop_get_cbdata_t *cbp = (zprop_get_cbdata_t *)data;
12664 char value[ZFS_MAXPROPLEN];
12665 zprop_source_t srctype;
12666 zprop_list_t *pl;
12667 int vid;
12668 int err = 0;
12669 nvlist_t *props, *item, *d;
12670 props = item = d = NULL;
12671
12672 if (cbp->cb_type == ZFS_TYPE_VDEV) {
12673 if (cbp->cb_json) {
12674 nvlist_t *pool = fnvlist_alloc();
12675 fill_pool_info(pool, zhp, B_FALSE, cbp->cb_json_as_int);
12676 fnvlist_add_nvlist(cbp->cb_jsobj, "pool", pool);
12677 fnvlist_free(pool);
12678 }
12679
12680 if (strcmp(cbp->cb_vdevs.cb_names[0], "all-vdevs") == 0) {
12681 (void) for_each_vdev(zhp, get_callback_vdev_cb, data);
12682 } else {
12683 /* Adjust column widths for vdev properties */
12684 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
12685 vid++) {
12686 (void) vdev_expand_proplist(zhp,
12687 cbp->cb_vdevs.cb_names[vid],
12688 &cbp->cb_proplist);
12689 }
12690 /* Display the properties */
12691 for (vid = 0; vid < cbp->cb_vdevs.cb_names_count;
12692 vid++) {
12693 (void) get_callback_vdev(zhp,
12694 cbp->cb_vdevs.cb_names[vid], data);
12695 }
12696 }
12697 } else {
12698 assert(cbp->cb_type == ZFS_TYPE_POOL);
12699 if (cbp->cb_json) {
12700 d = fnvlist_lookup_nvlist(cbp->cb_jsobj, "pools");
12701 if (d == NULL) {
12702 fprintf(stderr, "pools obj not found.\n");
12703 exit(1);
12704 }
12705 props = fnvlist_alloc();
12706 }
12707 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
12708 /*
12709 * Skip the special fake placeholder. This will also
12710 * skip over the name property when 'all' is specified.
12711 */
12712 if (pl->pl_prop == ZPOOL_PROP_NAME &&
12713 pl == cbp->cb_proplist)
12714 continue;
12715
12716 if (pl->pl_prop == ZPROP_INVAL &&
12717 zfs_prop_user(pl->pl_user_prop)) {
12718 srctype = ZPROP_SRC_LOCAL;
12719
12720 if (zpool_get_userprop(zhp, pl->pl_user_prop,
12721 value, sizeof (value), &srctype) != 0)
12722 continue;
12723
12724 err = zprop_collect_property(
12725 zpool_get_name(zhp), cbp, pl->pl_user_prop,
12726 value, srctype, NULL, NULL, props);
12727 } else if (pl->pl_prop == ZPROP_INVAL &&
12728 (zpool_prop_feature(pl->pl_user_prop) ||
12729 zpool_prop_unsupported(pl->pl_user_prop))) {
12730 srctype = ZPROP_SRC_LOCAL;
12731
12732 if (zpool_prop_get_feature(zhp,
12733 pl->pl_user_prop, value,
12734 sizeof (value)) == 0) {
12735 err = zprop_collect_property(
12736 zpool_get_name(zhp), cbp,
12737 pl->pl_user_prop, value, srctype,
12738 NULL, NULL, props);
12739 }
12740 } else {
12741 if (zpool_get_prop(zhp, pl->pl_prop, value,
12742 sizeof (value), &srctype,
12743 cbp->cb_literal) != 0)
12744 continue;
12745
12746 err = zprop_collect_property(
12747 zpool_get_name(zhp), cbp,
12748 zpool_prop_to_name(pl->pl_prop),
12749 value, srctype, NULL, NULL, props);
12750 }
12751 if (err != 0)
12752 return (err);
12753 }
12754
12755 if (cbp->cb_json) {
12756 if (!nvlist_empty(props)) {
12757 item = fnvlist_alloc();
12758 fill_pool_info(item, zhp, B_TRUE,
12759 cbp->cb_json_as_int);
12760 fnvlist_add_nvlist(item, "properties", props);
12761 if (cbp->cb_json_pool_key_guid) {
12762 char buf[256];
12763 uint64_t guid = fnvlist_lookup_uint64(
12764 zpool_get_config(zhp, NULL),
12765 ZPOOL_CONFIG_POOL_GUID);
12766 (void) snprintf(buf, 256, "%llu",
12767 (u_longlong_t)guid);
12768 fnvlist_add_nvlist(d, buf, item);
12769 } else {
12770 const char *name = zpool_get_name(zhp);
12771 fnvlist_add_nvlist(d, name, item);
12772 }
12773 fnvlist_add_nvlist(cbp->cb_jsobj, "pools", d);
12774 fnvlist_free(item);
12775 }
12776 fnvlist_free(props);
12777 }
12778 }
12779
12780 return (0);
12781 }
12782
12783 /*
12784 * zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
12785 *
12786 * -H Scripted mode. Don't display headers, and separate properties
12787 * by a single tab.
12788 * -o List of columns to display. Defaults to
12789 * "name,property,value,source".
12790 * -p Display values in parsable (exact) format.
12791 * -j Display output in JSON format.
12792 * --json-int Display numbers as integers instead of strings.
12793 * --json-pool-key-guid Set pool GUID as key for pool objects.
12794 *
12795 * Get properties of pools in the system. Output space statistics
12796 * for each one as well as other attributes.
12797 */
12798 int
zpool_do_get(int argc,char ** argv)12799 zpool_do_get(int argc, char **argv)
12800 {
12801 zprop_get_cbdata_t cb = { 0 };
12802 zprop_list_t fake_name = { 0 };
12803 int ret;
12804 int c, i;
12805 char *propstr = NULL;
12806 char *vdev = NULL;
12807 nvlist_t *data = NULL;
12808
12809 cb.cb_first = B_TRUE;
12810
12811 /*
12812 * Set up default columns and sources.
12813 */
12814 cb.cb_sources = ZPROP_SRC_ALL;
12815 cb.cb_columns[0] = GET_COL_NAME;
12816 cb.cb_columns[1] = GET_COL_PROPERTY;
12817 cb.cb_columns[2] = GET_COL_VALUE;
12818 cb.cb_columns[3] = GET_COL_SOURCE;
12819 cb.cb_type = ZFS_TYPE_POOL;
12820 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
12821 current_prop_type = cb.cb_type;
12822
12823 struct option long_options[] = {
12824 {"json", no_argument, NULL, 'j'},
12825 {"json-int", no_argument, NULL, ZPOOL_OPTION_JSON_NUMS_AS_INT},
12826 {"json-pool-key-guid", no_argument, NULL,
12827 ZPOOL_OPTION_POOL_KEY_GUID},
12828 {0, 0, 0, 0}
12829 };
12830
12831 /* check options */
12832 while ((c = getopt_long(argc, argv, ":jHpo:", long_options,
12833 NULL)) != -1) {
12834 switch (c) {
12835 case 'p':
12836 cb.cb_literal = B_TRUE;
12837 break;
12838 case 'H':
12839 cb.cb_scripted = B_TRUE;
12840 break;
12841 case 'j':
12842 cb.cb_json = B_TRUE;
12843 cb.cb_jsobj = zpool_json_schema(0, 1);
12844 data = fnvlist_alloc();
12845 break;
12846 case ZPOOL_OPTION_POOL_KEY_GUID:
12847 cb.cb_json_pool_key_guid = B_TRUE;
12848 break;
12849 case ZPOOL_OPTION_JSON_NUMS_AS_INT:
12850 cb.cb_json_as_int = B_TRUE;
12851 cb.cb_literal = B_TRUE;
12852 break;
12853 case 'o':
12854 memset(&cb.cb_columns, 0, sizeof (cb.cb_columns));
12855 i = 0;
12856
12857 for (char *tok; (tok = strsep(&optarg, ",")); ) {
12858 static const char *const col_opts[] =
12859 { "name", "property", "value", "source",
12860 "all" };
12861 static const zfs_get_column_t col_cols[] =
12862 { GET_COL_NAME, GET_COL_PROPERTY, GET_COL_VALUE,
12863 GET_COL_SOURCE };
12864
12865 if (i == ZFS_GET_NCOLS - 1) {
12866 (void) fprintf(stderr, gettext("too "
12867 "many fields given to -o "
12868 "option\n"));
12869 usage(B_FALSE);
12870 }
12871
12872 for (c = 0; c < ARRAY_SIZE(col_opts); ++c)
12873 if (strcmp(tok, col_opts[c]) == 0)
12874 goto found;
12875
12876 (void) fprintf(stderr,
12877 gettext("invalid column name '%s'\n"), tok);
12878 usage(B_FALSE);
12879
12880 found:
12881 if (c >= 4) {
12882 if (i > 0) {
12883 (void) fprintf(stderr,
12884 gettext("\"all\" conflicts "
12885 "with specific fields "
12886 "given to -o option\n"));
12887 usage(B_FALSE);
12888 }
12889
12890 memcpy(cb.cb_columns, col_cols,
12891 sizeof (col_cols));
12892 i = ZFS_GET_NCOLS - 1;
12893 } else
12894 cb.cb_columns[i++] = col_cols[c];
12895 }
12896 break;
12897 case '?':
12898 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
12899 optopt);
12900 usage(B_FALSE);
12901 }
12902 }
12903
12904 argc -= optind;
12905 argv += optind;
12906
12907 if (!cb.cb_json && cb.cb_json_as_int) {
12908 (void) fprintf(stderr, gettext("'--json-int' only works with"
12909 " '-j' option\n"));
12910 usage(B_FALSE);
12911 }
12912
12913 if (!cb.cb_json && cb.cb_json_pool_key_guid) {
12914 (void) fprintf(stderr, gettext("'json-pool-key-guid' only"
12915 " works with '-j' option\n"));
12916 usage(B_FALSE);
12917 }
12918
12919 if (argc < 1) {
12920 (void) fprintf(stderr, gettext("missing property "
12921 "argument\n"));
12922 usage(B_FALSE);
12923 }
12924
12925 /* Properties list is needed later by zprop_get_list() */
12926 propstr = argv[0];
12927
12928 argc--;
12929 argv++;
12930
12931 if (argc == 0) {
12932 /* No args, so just print the defaults. */
12933 } else if (are_all_pools(argc, argv)) {
12934 /* All the args are pool names */
12935 } else if (are_all_pools(1, argv)) {
12936 /* The first arg is a pool name */
12937 if ((argc == 2 && strcmp(argv[1], "all-vdevs") == 0) ||
12938 (argc == 2 && strcmp(argv[1], "root") == 0) ||
12939 are_vdevs_in_pool(argc - 1, argv + 1, argv[0],
12940 &cb.cb_vdevs)) {
12941
12942 if (strcmp(argv[1], "root") == 0)
12943 vdev = strdup("root-0");
12944
12945 /* ... and the rest are vdev names */
12946 if (vdev == NULL)
12947 cb.cb_vdevs.cb_names = argv + 1;
12948 else
12949 cb.cb_vdevs.cb_names = &vdev;
12950
12951 cb.cb_vdevs.cb_names_count = argc - 1;
12952 cb.cb_type = ZFS_TYPE_VDEV;
12953 argc = 1; /* One pool to process */
12954 } else {
12955 if (cb.cb_json) {
12956 nvlist_free(cb.cb_jsobj);
12957 nvlist_free(data);
12958 }
12959 fprintf(stderr, gettext("Expected a list of vdevs in"
12960 " \"%s\", but got:\n"), argv[0]);
12961 error_list_unresolved_vdevs(argc - 1, argv + 1,
12962 argv[0], &cb.cb_vdevs);
12963 fprintf(stderr, "\n");
12964 usage(B_FALSE);
12965 }
12966 } else {
12967 if (cb.cb_json) {
12968 nvlist_free(cb.cb_jsobj);
12969 nvlist_free(data);
12970 }
12971 /*
12972 * The first arg isn't the name of a valid pool.
12973 */
12974 fprintf(stderr, gettext("Cannot get properties of %s: "
12975 "no such pool available.\n"), argv[0]);
12976 return (1);
12977 }
12978
12979 if (zprop_get_list(g_zfs, propstr, &cb.cb_proplist,
12980 cb.cb_type) != 0) {
12981 /* Use correct list of valid properties (pool or vdev) */
12982 current_prop_type = cb.cb_type;
12983 usage(B_FALSE);
12984 }
12985
12986 if (cb.cb_proplist != NULL) {
12987 fake_name.pl_prop = ZPOOL_PROP_NAME;
12988 fake_name.pl_width = strlen(gettext("NAME"));
12989 fake_name.pl_next = cb.cb_proplist;
12990 cb.cb_proplist = &fake_name;
12991 }
12992
12993 if (cb.cb_json) {
12994 if (cb.cb_type == ZFS_TYPE_VDEV)
12995 fnvlist_add_nvlist(cb.cb_jsobj, "vdevs", data);
12996 else
12997 fnvlist_add_nvlist(cb.cb_jsobj, "pools", data);
12998 fnvlist_free(data);
12999 }
13000
13001 ret = for_each_pool(argc, argv, B_TRUE, &cb.cb_proplist, cb.cb_type,
13002 cb.cb_literal, get_callback, &cb);
13003
13004 if (ret == 0 && cb.cb_json)
13005 zcmd_print_json(cb.cb_jsobj);
13006 else if (ret != 0 && cb.cb_json)
13007 nvlist_free(cb.cb_jsobj);
13008
13009 if (cb.cb_proplist == &fake_name)
13010 zprop_free_list(fake_name.pl_next);
13011 else
13012 zprop_free_list(cb.cb_proplist);
13013
13014 if (vdev != NULL)
13015 free(vdev);
13016
13017 return (ret);
13018 }
13019
13020 typedef struct set_cbdata {
13021 char *cb_propname;
13022 char *cb_value;
13023 zfs_type_t cb_type;
13024 vdev_cbdata_t cb_vdevs;
13025 boolean_t cb_any_successful;
13026 } set_cbdata_t;
13027
13028 static int
set_pool_callback(zpool_handle_t * zhp,set_cbdata_t * cb)13029 set_pool_callback(zpool_handle_t *zhp, set_cbdata_t *cb)
13030 {
13031 int error;
13032
13033 /* Check if we have out-of-bounds features */
13034 if (strcmp(cb->cb_propname, ZPOOL_CONFIG_COMPATIBILITY) == 0) {
13035 boolean_t features[SPA_FEATURES];
13036 if (zpool_do_load_compat(cb->cb_value, features) !=
13037 ZPOOL_COMPATIBILITY_OK)
13038 return (-1);
13039
13040 nvlist_t *enabled = zpool_get_features(zhp);
13041 spa_feature_t i;
13042 for (i = 0; i < SPA_FEATURES; i++) {
13043 const char *fguid = spa_feature_table[i].fi_guid;
13044 if (nvlist_exists(enabled, fguid) && !features[i])
13045 break;
13046 }
13047 if (i < SPA_FEATURES)
13048 (void) fprintf(stderr, gettext("Warning: one or "
13049 "more features already enabled on pool '%s'\n"
13050 "are not present in this compatibility set.\n"),
13051 zpool_get_name(zhp));
13052 }
13053
13054 /* if we're setting a feature, check it's in compatibility set */
13055 if (zpool_prop_feature(cb->cb_propname) &&
13056 strcmp(cb->cb_value, ZFS_FEATURE_ENABLED) == 0) {
13057 char *fname = strchr(cb->cb_propname, '@') + 1;
13058 spa_feature_t f;
13059
13060 if (zfeature_lookup_name(fname, &f) == 0) {
13061 char compat[ZFS_MAXPROPLEN];
13062 if (zpool_get_prop(zhp, ZPOOL_PROP_COMPATIBILITY,
13063 compat, ZFS_MAXPROPLEN, NULL, B_FALSE) != 0)
13064 compat[0] = '\0';
13065
13066 boolean_t features[SPA_FEATURES];
13067 if (zpool_do_load_compat(compat, features) !=
13068 ZPOOL_COMPATIBILITY_OK) {
13069 (void) fprintf(stderr, gettext("Error: "
13070 "cannot enable feature '%s' on pool '%s'\n"
13071 "because the pool's 'compatibility' "
13072 "property cannot be parsed.\n"),
13073 fname, zpool_get_name(zhp));
13074 return (-1);
13075 }
13076
13077 if (!features[f]) {
13078 (void) fprintf(stderr, gettext("Error: "
13079 "cannot enable feature '%s' on pool '%s'\n"
13080 "as it is not specified in this pool's "
13081 "current compatibility set.\n"
13082 "Consider setting 'compatibility' to a "
13083 "less restrictive set, or to 'off'.\n"),
13084 fname, zpool_get_name(zhp));
13085 return (-1);
13086 }
13087 }
13088 }
13089
13090 error = zpool_set_prop(zhp, cb->cb_propname, cb->cb_value);
13091
13092 return (error);
13093 }
13094
13095 static int
set_callback(zpool_handle_t * zhp,void * data)13096 set_callback(zpool_handle_t *zhp, void *data)
13097 {
13098 int error;
13099 set_cbdata_t *cb = (set_cbdata_t *)data;
13100
13101 if (cb->cb_type == ZFS_TYPE_VDEV) {
13102 error = zpool_set_vdev_prop(zhp, *cb->cb_vdevs.cb_names,
13103 cb->cb_propname, cb->cb_value);
13104 } else {
13105 assert(cb->cb_type == ZFS_TYPE_POOL);
13106 error = set_pool_callback(zhp, cb);
13107 }
13108
13109 cb->cb_any_successful = !error;
13110 return (error);
13111 }
13112
13113 int
zpool_do_set(int argc,char ** argv)13114 zpool_do_set(int argc, char **argv)
13115 {
13116 set_cbdata_t cb = { 0 };
13117 int error;
13118 char *vdev = NULL;
13119
13120 current_prop_type = ZFS_TYPE_POOL;
13121 if (argc > 1 && argv[1][0] == '-') {
13122 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
13123 argv[1][1]);
13124 usage(B_FALSE);
13125 }
13126
13127 if (argc < 2) {
13128 (void) fprintf(stderr, gettext("missing property=value "
13129 "argument\n"));
13130 usage(B_FALSE);
13131 }
13132
13133 if (argc < 3) {
13134 (void) fprintf(stderr, gettext("missing pool name\n"));
13135 usage(B_FALSE);
13136 }
13137
13138 if (argc > 4) {
13139 (void) fprintf(stderr, gettext("too many pool names\n"));
13140 usage(B_FALSE);
13141 }
13142
13143 cb.cb_propname = argv[1];
13144 cb.cb_type = ZFS_TYPE_POOL;
13145 cb.cb_vdevs.cb_name_flags |= VDEV_NAME_TYPE_ID;
13146 cb.cb_value = strchr(cb.cb_propname, '=');
13147 if (cb.cb_value == NULL) {
13148 (void) fprintf(stderr, gettext("missing value in "
13149 "property=value argument\n"));
13150 usage(B_FALSE);
13151 }
13152
13153 *(cb.cb_value) = '\0';
13154 cb.cb_value++;
13155 argc -= 2;
13156 argv += 2;
13157
13158 /* argv[0] is pool name */
13159 if (!is_pool(argv[0])) {
13160 (void) fprintf(stderr,
13161 gettext("cannot open '%s': is not a pool\n"), argv[0]);
13162 return (EINVAL);
13163 }
13164
13165 /* argv[1], when supplied, is vdev name */
13166 if (argc == 2) {
13167
13168 if (strcmp(argv[1], "root") == 0)
13169 vdev = strdup("root-0");
13170 else
13171 vdev = strdup(argv[1]);
13172
13173 if (!are_vdevs_in_pool(1, &vdev, argv[0], &cb.cb_vdevs)) {
13174 (void) fprintf(stderr, gettext(
13175 "cannot find '%s' in '%s': device not in pool\n"),
13176 vdev, argv[0]);
13177 free(vdev);
13178 return (EINVAL);
13179 }
13180 cb.cb_vdevs.cb_names = &vdev;
13181 cb.cb_vdevs.cb_names_count = 1;
13182 cb.cb_type = ZFS_TYPE_VDEV;
13183 }
13184
13185 error = for_each_pool(1, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
13186 B_FALSE, set_callback, &cb);
13187
13188 if (vdev != NULL)
13189 free(vdev);
13190
13191 return (error);
13192 }
13193
13194 /* Add up the total number of bytes left to initialize/trim across all vdevs */
13195 static uint64_t
vdev_activity_remaining(nvlist_t * nv,zpool_wait_activity_t activity)13196 vdev_activity_remaining(nvlist_t *nv, zpool_wait_activity_t activity)
13197 {
13198 uint64_t bytes_remaining;
13199 nvlist_t **child;
13200 uint_t c, children;
13201 vdev_stat_t *vs;
13202
13203 assert(activity == ZPOOL_WAIT_INITIALIZE ||
13204 activity == ZPOOL_WAIT_TRIM);
13205
13206 verify(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
13207 (uint64_t **)&vs, &c) == 0);
13208
13209 if (activity == ZPOOL_WAIT_INITIALIZE &&
13210 vs->vs_initialize_state == VDEV_INITIALIZE_ACTIVE)
13211 bytes_remaining = vs->vs_initialize_bytes_est -
13212 vs->vs_initialize_bytes_done;
13213 else if (activity == ZPOOL_WAIT_TRIM &&
13214 vs->vs_trim_state == VDEV_TRIM_ACTIVE)
13215 bytes_remaining = vs->vs_trim_bytes_est -
13216 vs->vs_trim_bytes_done;
13217 else
13218 bytes_remaining = 0;
13219
13220 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13221 &child, &children) != 0)
13222 children = 0;
13223
13224 for (c = 0; c < children; c++)
13225 bytes_remaining += vdev_activity_remaining(child[c], activity);
13226
13227 return (bytes_remaining);
13228 }
13229
13230 /* Add up the total number of bytes left to rebuild across top-level vdevs */
13231 static uint64_t
vdev_activity_top_remaining(nvlist_t * nv)13232 vdev_activity_top_remaining(nvlist_t *nv)
13233 {
13234 uint64_t bytes_remaining = 0;
13235 nvlist_t **child;
13236 uint_t children;
13237 int error;
13238
13239 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13240 &child, &children) != 0)
13241 children = 0;
13242
13243 for (uint_t c = 0; c < children; c++) {
13244 vdev_rebuild_stat_t *vrs;
13245 uint_t i;
13246
13247 error = nvlist_lookup_uint64_array(child[c],
13248 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t **)&vrs, &i);
13249 if (error == 0) {
13250 if (vrs->vrs_state == VDEV_REBUILD_ACTIVE) {
13251 bytes_remaining += (vrs->vrs_bytes_est -
13252 vrs->vrs_bytes_rebuilt);
13253 }
13254 }
13255 }
13256
13257 return (bytes_remaining);
13258 }
13259
13260 /* Whether any vdevs are 'spare' or 'replacing' vdevs */
13261 static boolean_t
vdev_any_spare_replacing(nvlist_t * nv)13262 vdev_any_spare_replacing(nvlist_t *nv)
13263 {
13264 nvlist_t **child;
13265 uint_t c, children;
13266 const char *vdev_type;
13267
13268 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &vdev_type);
13269
13270 if (strcmp(vdev_type, VDEV_TYPE_REPLACING) == 0 ||
13271 strcmp(vdev_type, VDEV_TYPE_SPARE) == 0 ||
13272 strcmp(vdev_type, VDEV_TYPE_DRAID_SPARE) == 0) {
13273 return (B_TRUE);
13274 }
13275
13276 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
13277 &child, &children) != 0)
13278 children = 0;
13279
13280 for (c = 0; c < children; c++) {
13281 if (vdev_any_spare_replacing(child[c]))
13282 return (B_TRUE);
13283 }
13284
13285 return (B_FALSE);
13286 }
13287
13288 typedef struct wait_data {
13289 char *wd_poolname;
13290 boolean_t wd_scripted;
13291 boolean_t wd_exact;
13292 boolean_t wd_headers_once;
13293 boolean_t wd_should_exit;
13294 /* Which activities to wait for */
13295 boolean_t wd_enabled[ZPOOL_WAIT_NUM_ACTIVITIES];
13296 float wd_interval;
13297 pthread_cond_t wd_cv;
13298 pthread_mutex_t wd_mutex;
13299 } wait_data_t;
13300
13301 /*
13302 * Print to stdout a single line, containing one column for each activity that
13303 * we are waiting for specifying how many bytes of work are left for that
13304 * activity.
13305 */
13306 static void
print_wait_status_row(wait_data_t * wd,zpool_handle_t * zhp,int row)13307 print_wait_status_row(wait_data_t *wd, zpool_handle_t *zhp, int row)
13308 {
13309 nvlist_t *config, *nvroot;
13310 uint_t c;
13311 int i;
13312 pool_checkpoint_stat_t *pcs = NULL;
13313 pool_scan_stat_t *pss = NULL;
13314 pool_removal_stat_t *prs = NULL;
13315 pool_raidz_expand_stat_t *pres = NULL;
13316 const char *const headers[] = {"DISCARD", "FREE", "INITIALIZE",
13317 "REPLACE", "REMOVE", "RESILVER", "SCRUB", "TRIM", "RAIDZ_EXPAND"};
13318 int col_widths[ZPOOL_WAIT_NUM_ACTIVITIES];
13319
13320 /* Calculate the width of each column */
13321 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
13322 /*
13323 * Make sure we have enough space in the col for pretty-printed
13324 * numbers and for the column header, and then leave a couple
13325 * spaces between cols for readability.
13326 */
13327 col_widths[i] = MAX(strlen(headers[i]), 6) + 2;
13328 }
13329
13330 if (timestamp_fmt != NODATE)
13331 print_timestamp(timestamp_fmt);
13332
13333 /* Print header if appropriate */
13334 int term_height = terminal_height();
13335 boolean_t reprint_header = (!wd->wd_headers_once && term_height > 0 &&
13336 row % (term_height-1) == 0);
13337 if (!wd->wd_scripted && (row == 0 || reprint_header)) {
13338 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
13339 if (wd->wd_enabled[i])
13340 (void) printf("%*s", col_widths[i], headers[i]);
13341 }
13342 (void) fputc('\n', stdout);
13343 }
13344
13345 /* Bytes of work remaining in each activity */
13346 int64_t bytes_rem[ZPOOL_WAIT_NUM_ACTIVITIES] = {0};
13347
13348 bytes_rem[ZPOOL_WAIT_FREE] =
13349 zpool_get_prop_int(zhp, ZPOOL_PROP_FREEING, NULL);
13350
13351 config = zpool_get_config(zhp, NULL);
13352 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
13353
13354 (void) nvlist_lookup_uint64_array(nvroot,
13355 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t **)&pcs, &c);
13356 if (pcs != NULL && pcs->pcs_state == CS_CHECKPOINT_DISCARDING)
13357 bytes_rem[ZPOOL_WAIT_CKPT_DISCARD] = pcs->pcs_space;
13358
13359 (void) nvlist_lookup_uint64_array(nvroot,
13360 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t **)&prs, &c);
13361 if (prs != NULL && prs->prs_state == DSS_SCANNING)
13362 bytes_rem[ZPOOL_WAIT_REMOVE] = prs->prs_to_copy -
13363 prs->prs_copied;
13364
13365 (void) nvlist_lookup_uint64_array(nvroot,
13366 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&pss, &c);
13367 if (pss != NULL && pss->pss_state == DSS_SCANNING &&
13368 pss->pss_pass_scrub_pause == 0) {
13369 int64_t rem = pss->pss_to_examine - pss->pss_issued;
13370 if (pss->pss_func == POOL_SCAN_SCRUB)
13371 bytes_rem[ZPOOL_WAIT_SCRUB] = rem;
13372 else
13373 bytes_rem[ZPOOL_WAIT_RESILVER] = rem;
13374 } else if (check_rebuilding(nvroot, NULL)) {
13375 bytes_rem[ZPOOL_WAIT_RESILVER] =
13376 vdev_activity_top_remaining(nvroot);
13377 }
13378
13379 (void) nvlist_lookup_uint64_array(nvroot,
13380 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t **)&pres, &c);
13381 if (pres != NULL && pres->pres_state == DSS_SCANNING) {
13382 int64_t rem = pres->pres_to_reflow - pres->pres_reflowed;
13383 bytes_rem[ZPOOL_WAIT_RAIDZ_EXPAND] = rem;
13384 }
13385
13386 bytes_rem[ZPOOL_WAIT_INITIALIZE] =
13387 vdev_activity_remaining(nvroot, ZPOOL_WAIT_INITIALIZE);
13388 bytes_rem[ZPOOL_WAIT_TRIM] =
13389 vdev_activity_remaining(nvroot, ZPOOL_WAIT_TRIM);
13390
13391 /*
13392 * A replace finishes after resilvering finishes, so the amount of work
13393 * left for a replace is the same as for resilvering.
13394 *
13395 * It isn't quite correct to say that if we have any 'spare' or
13396 * 'replacing' vdevs and a resilver is happening, then a replace is in
13397 * progress, like we do here. When a hot spare is used, the faulted vdev
13398 * is not removed after the hot spare is resilvered, so parent 'spare'
13399 * vdev is not removed either. So we could have a 'spare' vdev, but be
13400 * resilvering for a different reason. However, we use it as a heuristic
13401 * because we don't have access to the DTLs, which could tell us whether
13402 * or not we have really finished resilvering a hot spare.
13403 */
13404 if (vdev_any_spare_replacing(nvroot))
13405 bytes_rem[ZPOOL_WAIT_REPLACE] = bytes_rem[ZPOOL_WAIT_RESILVER];
13406
13407 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
13408 char buf[64];
13409 if (!wd->wd_enabled[i])
13410 continue;
13411
13412 if (wd->wd_exact) {
13413 (void) snprintf(buf, sizeof (buf), "%" PRIi64,
13414 bytes_rem[i]);
13415 } else {
13416 zfs_nicenum(bytes_rem[i], buf, sizeof (buf));
13417 }
13418
13419 if (wd->wd_scripted)
13420 (void) printf(i == 0 ? "%s" : "\t%s", buf);
13421 else
13422 (void) printf(" %*s", col_widths[i] - 1, buf);
13423 }
13424 (void) printf("\n");
13425 (void) fflush(stdout);
13426 }
13427
13428 static void *
wait_status_thread(void * arg)13429 wait_status_thread(void *arg)
13430 {
13431 wait_data_t *wd = (wait_data_t *)arg;
13432 zpool_handle_t *zhp;
13433
13434 if ((zhp = zpool_open(g_zfs, wd->wd_poolname)) == NULL)
13435 return (void *)(1);
13436
13437 for (int row = 0; ; row++) {
13438 boolean_t missing;
13439 struct timespec timeout;
13440 int ret = 0;
13441 (void) clock_gettime(CLOCK_REALTIME, &timeout);
13442
13443 if (zpool_refresh_stats(zhp, &missing) != 0 || missing ||
13444 zpool_props_refresh(zhp) != 0) {
13445 zpool_close(zhp);
13446 return (void *)(uintptr_t)(missing ? 0 : 1);
13447 }
13448
13449 print_wait_status_row(wd, zhp, row);
13450
13451 timeout.tv_sec += floor(wd->wd_interval);
13452 long nanos = timeout.tv_nsec +
13453 (wd->wd_interval - floor(wd->wd_interval)) * NANOSEC;
13454 if (nanos >= NANOSEC) {
13455 timeout.tv_sec++;
13456 timeout.tv_nsec = nanos - NANOSEC;
13457 } else {
13458 timeout.tv_nsec = nanos;
13459 }
13460 (void) pthread_mutex_lock(&wd->wd_mutex);
13461 if (!wd->wd_should_exit)
13462 ret = pthread_cond_timedwait(&wd->wd_cv, &wd->wd_mutex,
13463 &timeout);
13464 (void) pthread_mutex_unlock(&wd->wd_mutex);
13465 if (ret == 0) {
13466 break; /* signaled by main thread */
13467 } else if (ret != ETIMEDOUT) {
13468 (void) fprintf(stderr, gettext("pthread_cond_timedwait "
13469 "failed: %s\n"), strerror(ret));
13470 zpool_close(zhp);
13471 return (void *)(uintptr_t)(1);
13472 }
13473 }
13474
13475 zpool_close(zhp);
13476 return (void *)(0);
13477 }
13478
13479 int
zpool_do_wait(int argc,char ** argv)13480 zpool_do_wait(int argc, char **argv)
13481 {
13482 boolean_t verbose = B_FALSE;
13483 int c, i;
13484 unsigned long count;
13485 pthread_t status_thr;
13486 int error = 0;
13487 zpool_handle_t *zhp;
13488
13489 wait_data_t wd;
13490 wd.wd_scripted = B_FALSE;
13491 wd.wd_exact = B_FALSE;
13492 wd.wd_headers_once = B_FALSE;
13493 wd.wd_should_exit = B_FALSE;
13494
13495 (void) pthread_mutex_init(&wd.wd_mutex, NULL);
13496 (void) pthread_cond_init(&wd.wd_cv, NULL);
13497
13498 /* By default, wait for all types of activity. */
13499 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++)
13500 wd.wd_enabled[i] = B_TRUE;
13501
13502 while ((c = getopt(argc, argv, "HpT:t:")) != -1) {
13503 switch (c) {
13504 case 'H':
13505 wd.wd_scripted = B_TRUE;
13506 break;
13507 case 'n':
13508 wd.wd_headers_once = B_TRUE;
13509 break;
13510 case 'p':
13511 wd.wd_exact = B_TRUE;
13512 break;
13513 case 'T':
13514 get_timestamp_arg(*optarg);
13515 break;
13516 case 't':
13517 /* Reset activities array */
13518 memset(&wd.wd_enabled, 0, sizeof (wd.wd_enabled));
13519
13520 for (char *tok; (tok = strsep(&optarg, ",")); ) {
13521 static const char *const col_opts[] = {
13522 "discard", "free", "initialize", "replace",
13523 "remove", "resilver", "scrub", "trim",
13524 "raidz_expand" };
13525
13526 for (i = 0; i < ARRAY_SIZE(col_opts); ++i)
13527 if (strcmp(tok, col_opts[i]) == 0) {
13528 wd.wd_enabled[i] = B_TRUE;
13529 goto found;
13530 }
13531
13532 (void) fprintf(stderr,
13533 gettext("invalid activity '%s'\n"), tok);
13534 usage(B_FALSE);
13535 found:;
13536 }
13537 break;
13538 case '?':
13539 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
13540 optopt);
13541 usage(B_FALSE);
13542 }
13543 }
13544
13545 argc -= optind;
13546 argv += optind;
13547
13548 get_interval_count(&argc, argv, &wd.wd_interval, &count);
13549 if (count != 0) {
13550 /* This subcmd only accepts an interval, not a count */
13551 (void) fprintf(stderr, gettext("too many arguments\n"));
13552 usage(B_FALSE);
13553 }
13554
13555 if (wd.wd_interval != 0)
13556 verbose = B_TRUE;
13557
13558 if (argc < 1) {
13559 (void) fprintf(stderr, gettext("missing 'pool' argument\n"));
13560 usage(B_FALSE);
13561 }
13562 if (argc > 1) {
13563 (void) fprintf(stderr, gettext("too many arguments\n"));
13564 usage(B_FALSE);
13565 }
13566
13567 wd.wd_poolname = argv[0];
13568
13569 if ((zhp = zpool_open(g_zfs, wd.wd_poolname)) == NULL)
13570 return (1);
13571
13572 if (verbose) {
13573 /*
13574 * We use a separate thread for printing status updates because
13575 * the main thread will call lzc_wait(), which blocks as long
13576 * as an activity is in progress, which can be a long time.
13577 */
13578 if (pthread_create(&status_thr, NULL, wait_status_thread, &wd)
13579 != 0) {
13580 (void) fprintf(stderr, gettext("failed to create status"
13581 "thread: %s\n"), strerror(errno));
13582 zpool_close(zhp);
13583 return (1);
13584 }
13585 }
13586
13587 /*
13588 * Loop over all activities that we are supposed to wait for until none
13589 * of them are in progress. Note that this means we can end up waiting
13590 * for more activities to complete than just those that were in progress
13591 * when we began waiting; if an activity we are interested in begins
13592 * while we are waiting for another activity, we will wait for both to
13593 * complete before exiting.
13594 */
13595 for (;;) {
13596 boolean_t missing = B_FALSE;
13597 boolean_t any_waited = B_FALSE;
13598
13599 for (i = 0; i < ZPOOL_WAIT_NUM_ACTIVITIES; i++) {
13600 boolean_t waited;
13601
13602 if (!wd.wd_enabled[i])
13603 continue;
13604
13605 error = zpool_wait_status(zhp, i, &missing, &waited);
13606 if (error != 0 || missing)
13607 break;
13608
13609 any_waited = (any_waited || waited);
13610 }
13611
13612 if (error != 0 || missing || !any_waited)
13613 break;
13614 }
13615
13616 zpool_close(zhp);
13617
13618 if (verbose) {
13619 uintptr_t status;
13620 (void) pthread_mutex_lock(&wd.wd_mutex);
13621 wd.wd_should_exit = B_TRUE;
13622 (void) pthread_cond_signal(&wd.wd_cv);
13623 (void) pthread_mutex_unlock(&wd.wd_mutex);
13624 (void) pthread_join(status_thr, (void *)&status);
13625 if (status != 0)
13626 error = status;
13627 }
13628
13629 (void) pthread_mutex_destroy(&wd.wd_mutex);
13630 (void) pthread_cond_destroy(&wd.wd_cv);
13631 return (error);
13632 }
13633
13634 /*
13635 * zpool ddtprune -d|-p <amount> <pool>
13636 *
13637 * -d <days> Prune entries <days> old and older
13638 * -p <percent> Prune <percent> amount of entries
13639 *
13640 * Prune single reference entries from DDT to satisfy the amount specified.
13641 */
13642 int
zpool_do_ddt_prune(int argc,char ** argv)13643 zpool_do_ddt_prune(int argc, char **argv)
13644 {
13645 zpool_ddt_prune_unit_t unit = ZPOOL_DDT_PRUNE_NONE;
13646 uint64_t amount = 0;
13647 zpool_handle_t *zhp;
13648 char *endptr;
13649 int c;
13650
13651 while ((c = getopt(argc, argv, "d:p:")) != -1) {
13652 switch (c) {
13653 case 'd':
13654 if (unit == ZPOOL_DDT_PRUNE_PERCENTAGE) {
13655 (void) fprintf(stderr, gettext("-d cannot be "
13656 "combined with -p option\n"));
13657 usage(B_FALSE);
13658 }
13659 errno = 0;
13660 amount = strtoull(optarg, &endptr, 0);
13661 if (errno != 0 || *endptr != '\0' || amount == 0) {
13662 (void) fprintf(stderr,
13663 gettext("invalid days value\n"));
13664 usage(B_FALSE);
13665 }
13666 amount *= 86400; /* convert days to seconds */
13667 unit = ZPOOL_DDT_PRUNE_AGE;
13668 break;
13669 case 'p':
13670 if (unit == ZPOOL_DDT_PRUNE_AGE) {
13671 (void) fprintf(stderr, gettext("-p cannot be "
13672 "combined with -d option\n"));
13673 usage(B_FALSE);
13674 }
13675 errno = 0;
13676 amount = strtoull(optarg, &endptr, 0);
13677 if (errno != 0 || *endptr != '\0' ||
13678 amount == 0 || amount > 100) {
13679 (void) fprintf(stderr,
13680 gettext("invalid percentage value\n"));
13681 usage(B_FALSE);
13682 }
13683 unit = ZPOOL_DDT_PRUNE_PERCENTAGE;
13684 break;
13685 case '?':
13686 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
13687 optopt);
13688 usage(B_FALSE);
13689 }
13690 }
13691 argc -= optind;
13692 argv += optind;
13693
13694 if (unit == ZPOOL_DDT_PRUNE_NONE) {
13695 (void) fprintf(stderr,
13696 gettext("missing amount option (-d|-p <value>)\n"));
13697 usage(B_FALSE);
13698 } else if (argc < 1) {
13699 (void) fprintf(stderr, gettext("missing pool argument\n"));
13700 usage(B_FALSE);
13701 } else if (argc > 1) {
13702 (void) fprintf(stderr, gettext("too many arguments\n"));
13703 usage(B_FALSE);
13704 }
13705 zhp = zpool_open(g_zfs, argv[0]);
13706 if (zhp == NULL)
13707 return (-1);
13708
13709 int error = zpool_ddt_prune(zhp, unit, amount);
13710
13711 zpool_close(zhp);
13712
13713 return (error);
13714 }
13715
13716 static int
find_command_idx(const char * command,int * idx)13717 find_command_idx(const char *command, int *idx)
13718 {
13719 for (int i = 0; i < NCOMMAND; ++i) {
13720 if (command_table[i].name == NULL)
13721 continue;
13722
13723 if (strcmp(command, command_table[i].name) == 0) {
13724 *idx = i;
13725 return (0);
13726 }
13727 }
13728 return (1);
13729 }
13730
13731 /*
13732 * Display version message
13733 */
13734 static int
zpool_do_version(int argc,char ** argv)13735 zpool_do_version(int argc, char **argv)
13736 {
13737 int c;
13738 nvlist_t *jsobj = NULL, *zfs_ver = NULL;
13739 boolean_t json = B_FALSE;
13740
13741 struct option long_options[] = {
13742 {"json", no_argument, NULL, 'j'},
13743 };
13744
13745 while ((c = getopt_long(argc, argv, "j", long_options, NULL)) != -1) {
13746 switch (c) {
13747 case 'j':
13748 json = B_TRUE;
13749 jsobj = zpool_json_schema(0, 1);
13750 break;
13751 case '?':
13752 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
13753 optopt);
13754 usage(B_FALSE);
13755 }
13756 }
13757
13758 argc -= optind;
13759 if (argc != 0) {
13760 (void) fprintf(stderr, "too many arguments\n");
13761 usage(B_FALSE);
13762 }
13763
13764 if (json) {
13765 zfs_ver = zfs_version_nvlist();
13766 if (zfs_ver) {
13767 fnvlist_add_nvlist(jsobj, "zfs_version", zfs_ver);
13768 zcmd_print_json(jsobj);
13769 fnvlist_free(zfs_ver);
13770 return (0);
13771 } else
13772 return (-1);
13773 } else
13774 return (zfs_version_print() != 0);
13775 }
13776
13777 /* Display documentation */
13778 static int
zpool_do_help(int argc,char ** argv)13779 zpool_do_help(int argc, char **argv)
13780 {
13781 char page[MAXNAMELEN];
13782 if (argc < 3 || strcmp(argv[2], "zpool") == 0)
13783 (void) strcpy(page, "zpool");
13784 else if (strcmp(argv[2], "concepts") == 0 ||
13785 strcmp(argv[2], "props") == 0)
13786 (void) snprintf(page, sizeof (page), "zpool%s", argv[2]);
13787 else
13788 (void) snprintf(page, sizeof (page), "zpool-%s", argv[2]);
13789
13790 (void) execlp("man", "man", page, NULL);
13791
13792 fprintf(stderr, "couldn't run man program: %s\n", strerror(errno));
13793 return (-1);
13794 }
13795
13796 /*
13797 * Do zpool_load_compat() and print error message on failure
13798 */
13799 static zpool_compat_status_t
zpool_do_load_compat(const char * compat,boolean_t * list)13800 zpool_do_load_compat(const char *compat, boolean_t *list)
13801 {
13802 char report[1024];
13803
13804 zpool_compat_status_t ret;
13805
13806 ret = zpool_load_compat(compat, list, report, 1024);
13807 switch (ret) {
13808
13809 case ZPOOL_COMPATIBILITY_OK:
13810 break;
13811
13812 case ZPOOL_COMPATIBILITY_NOFILES:
13813 case ZPOOL_COMPATIBILITY_BADFILE:
13814 case ZPOOL_COMPATIBILITY_BADTOKEN:
13815 (void) fprintf(stderr, "Error: %s\n", report);
13816 break;
13817
13818 case ZPOOL_COMPATIBILITY_WARNTOKEN:
13819 (void) fprintf(stderr, "Warning: %s\n", report);
13820 ret = ZPOOL_COMPATIBILITY_OK;
13821 break;
13822 }
13823 return (ret);
13824 }
13825
13826 int
main(int argc,char ** argv)13827 main(int argc, char **argv)
13828 {
13829 int ret = 0;
13830 int i = 0;
13831 char *cmdname;
13832 char **newargv;
13833
13834 (void) setlocale(LC_ALL, "");
13835 (void) setlocale(LC_NUMERIC, "C");
13836 (void) textdomain(TEXT_DOMAIN);
13837 srand(time(NULL));
13838
13839 opterr = 0;
13840
13841 /*
13842 * Make sure the user has specified some command.
13843 */
13844 if (argc < 2) {
13845 (void) fprintf(stderr, gettext("missing command\n"));
13846 usage(B_FALSE);
13847 }
13848
13849 cmdname = argv[1];
13850
13851 /*
13852 * Special case '-?'
13853 */
13854 if ((strcmp(cmdname, "-?") == 0) || strcmp(cmdname, "--help") == 0)
13855 usage(B_TRUE);
13856
13857 /*
13858 * Special case '-V|--version'
13859 */
13860 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
13861 return (zfs_version_print() != 0);
13862
13863 /*
13864 * Special case 'help'
13865 */
13866 if (strcmp(cmdname, "help") == 0)
13867 return (zpool_do_help(argc, argv));
13868
13869 if ((g_zfs = libzfs_init()) == NULL) {
13870 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
13871 return (1);
13872 }
13873
13874 libzfs_print_on_error(g_zfs, B_TRUE);
13875
13876 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
13877
13878 /*
13879 * Many commands modify input strings for string parsing reasons.
13880 * We create a copy to protect the original argv.
13881 */
13882 newargv = safe_malloc((argc + 1) * sizeof (newargv[0]));
13883 for (i = 0; i < argc; i++)
13884 newargv[i] = strdup(argv[i]);
13885 newargv[argc] = NULL;
13886
13887 /*
13888 * Run the appropriate command.
13889 */
13890 if (find_command_idx(cmdname, &i) == 0) {
13891 current_command = &command_table[i];
13892 ret = command_table[i].func(argc - 1, newargv + 1);
13893 } else if (strchr(cmdname, '=')) {
13894 verify(find_command_idx("set", &i) == 0);
13895 current_command = &command_table[i];
13896 ret = command_table[i].func(argc, newargv);
13897 } else if (strcmp(cmdname, "freeze") == 0 && argc == 3) {
13898 /*
13899 * 'freeze' is a vile debugging abomination, so we treat
13900 * it as such.
13901 */
13902 zfs_cmd_t zc = {"\0"};
13903
13904 (void) strlcpy(zc.zc_name, argv[2], sizeof (zc.zc_name));
13905 ret = zfs_ioctl(g_zfs, ZFS_IOC_POOL_FREEZE, &zc);
13906 if (ret != 0) {
13907 (void) fprintf(stderr,
13908 gettext("failed to freeze pool: %d\n"), errno);
13909 ret = 1;
13910 }
13911
13912 log_history = 0;
13913 } else {
13914 (void) fprintf(stderr, gettext("unrecognized "
13915 "command '%s'\n"), cmdname);
13916 usage(B_FALSE);
13917 }
13918
13919 for (i = 0; i < argc; i++)
13920 free(newargv[i]);
13921 free(newargv);
13922
13923 if (ret == 0 && log_history)
13924 (void) zpool_log_history(g_zfs, history_str);
13925
13926 libzfs_fini(g_zfs);
13927
13928 /*
13929 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
13930 * for the purposes of running ::findleaks.
13931 */
13932 if (getenv("ZFS_ABORT") != NULL) {
13933 (void) printf("dumping core by request\n");
13934 abort();
13935 }
13936
13937 return (ret);
13938 }
13939