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