]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzfs/libzfs_pool.c
zpool: max_width: monomorphise subtype iteration
[mirror_zfs.git] / lib / libzfs / libzfs_pool.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/*
0fdd8d64 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
428870ff 24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
108a454a 25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
23d70cde 26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
2ffd89fc 27 * Copyright (c) 2018 Datto Inc.
d3f2cd7e 28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
cc99f275 29 * Copyright (c) 2017, Intel Corporation.
f0f97865 30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
658fb802 31 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
2a673e76 32 * Copyright (c) 2021, Klara Inc.
34dc7c2f
BB
33 */
34
34dc7c2f 35#include <errno.h>
34dc7c2f
BB
36#include <libintl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <strings.h>
40#include <unistd.h>
6f1ffb06 41#include <libgen.h>
d603ed6c
BB
42#include <zone.h>
43#include <sys/stat.h>
34dc7c2f 44#include <sys/efi_partition.h>
46364cb2 45#include <sys/systeminfo.h>
34dc7c2f 46#include <sys/zfs_ioctl.h>
b2255edc 47#include <sys/zfs_sysfs.h>
74d42600 48#include <sys/vdev_disk.h>
658fb802 49#include <sys/types.h>
9babb374 50#include <dlfcn.h>
e89f1295 51#include <libzutil.h>
658fb802 52#include <fcntl.h>
658fb802 53
34dc7c2f
BB
54#include "zfs_namecheck.h"
55#include "zfs_prop.h"
56#include "libzfs_impl.h"
428870ff 57#include "zfs_comutil.h"
9ae529ec 58#include "zfeature_common.h"
34dc7c2f 59
1574c73b 60static boolean_t zpool_vdev_is_interior(const char *name);
b128c09f 61
572e2857
BB
62typedef struct prop_flags {
63 int create:1; /* Validate property on creation */
64 int import:1; /* Validate property on import */
2a673e76 65 int vdevprop:1; /* Validate property as a VDEV property */
572e2857
BB
66} prop_flags_t;
67
34dc7c2f
BB
68/*
69 * ====================================================================
70 * zpool property functions
71 * ====================================================================
72 */
73
74static int
75zpool_get_all_props(zpool_handle_t *zhp)
76{
13fe0198 77 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
78 libzfs_handle_t *hdl = zhp->zpool_hdl;
79
80 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
81
18dbf5c8 82 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
34dc7c2f 83
b834b58a 84 while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
18dbf5c8
AZ
85 if (errno == ENOMEM)
86 zcmd_expand_dst_nvlist(hdl, &zc);
87 else {
34dc7c2f
BB
88 zcmd_free_nvlists(&zc);
89 return (-1);
90 }
91 }
92
93 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
94 zcmd_free_nvlists(&zc);
95 return (-1);
96 }
97
98 zcmd_free_nvlists(&zc);
99
100 return (0);
101}
102
e60e158e 103int
34dc7c2f
BB
104zpool_props_refresh(zpool_handle_t *zhp)
105{
106 nvlist_t *old_props;
107
108 old_props = zhp->zpool_props;
109
110 if (zpool_get_all_props(zhp) != 0)
111 return (-1);
112
113 nvlist_free(old_props);
114 return (0);
115}
116
6b8655ad 117static const char *
34dc7c2f
BB
118zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
119 zprop_source_t *src)
120{
121 nvlist_t *nv, *nvl;
34dc7c2f
BB
122 char *value;
123 zprop_source_t source;
124
125 nvl = zhp->zpool_props;
126 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
8bb9ecf4
RM
127 source = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
128 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
34dc7c2f
BB
129 } else {
130 source = ZPROP_SRC_DEFAULT;
131 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
132 value = "-";
133 }
134
135 if (src)
136 *src = source;
137
138 return (value);
139}
140
141uint64_t
142zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
143{
144 nvlist_t *nv, *nvl;
145 uint64_t value;
146 zprop_source_t source;
147
b128c09f
BB
148 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
149 /*
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
153 */
154 if ((prop == ZPOOL_PROP_GUID) &&
155 (nvlist_lookup_nvlist(zhp->zpool_config,
156 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
157 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
158 == 0)) {
159 return (value);
160 }
34dc7c2f 161 return (zpool_prop_default_numeric(prop));
b128c09f 162 }
34dc7c2f
BB
163
164 nvl = zhp->zpool_props;
165 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
8bb9ecf4
RM
166 source = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
167 value = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
34dc7c2f
BB
168 } else {
169 source = ZPROP_SRC_DEFAULT;
170 value = zpool_prop_default_numeric(prop);
171 }
172
173 if (src)
174 *src = source;
175
176 return (value);
177}
178
179/*
180 * Map VDEV STATE to printed strings.
181 */
6b8655ad 182const char *
34dc7c2f
BB
183zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
184{
185 switch (state) {
186 case VDEV_STATE_CLOSED:
187 case VDEV_STATE_OFFLINE:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN:
b128c09f 192 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
34dc7c2f 193 return (gettext("FAULTED"));
428870ff
BB
194 else if (aux == VDEV_AUX_SPLIT_POOL)
195 return (gettext("SPLIT"));
34dc7c2f
BB
196 else
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY:
203 return (gettext("ONLINE"));
23d70cde
GM
204
205 default:
206 break;
34dc7c2f
BB
207 }
208
209 return (gettext("UNKNOWN"));
210}
211
131cc95c
DK
212/*
213 * Map POOL STATE to printed strings.
214 */
215const char *
216zpool_pool_state_to_name(pool_state_t state)
217{
218 switch (state) {
219 default:
220 break;
221 case POOL_STATE_ACTIVE:
222 return (gettext("ACTIVE"));
223 case POOL_STATE_EXPORTED:
224 return (gettext("EXPORTED"));
225 case POOL_STATE_DESTROYED:
226 return (gettext("DESTROYED"));
227 case POOL_STATE_SPARE:
228 return (gettext("SPARE"));
229 case POOL_STATE_L2CACHE:
230 return (gettext("L2CACHE"));
231 case POOL_STATE_UNINITIALIZED:
232 return (gettext("UNINITIALIZED"));
233 case POOL_STATE_UNAVAIL:
234 return (gettext("UNAVAIL"));
235 case POOL_STATE_POTENTIALLY_ACTIVE:
236 return (gettext("POTENTIALLY_ACTIVE"));
237 }
238
239 return (gettext("UNKNOWN"));
240}
241
f0ed6c74
TH
242/*
243 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
244 * "SUSPENDED", etc).
245 */
246const char *
247zpool_get_state_str(zpool_handle_t *zhp)
248{
249 zpool_errata_t errata;
250 zpool_status_t status;
f0ed6c74
TH
251 const char *str;
252
253 status = zpool_get_status(zhp, NULL, &errata);
254
255 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
256 str = gettext("FAULTED");
257 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
258 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
259 str = gettext("SUSPENDED");
260 } else {
8bb9ecf4
RM
261 nvlist_t *nvroot = fnvlist_lookup_nvlist(
262 zpool_get_config(zhp, NULL), ZPOOL_CONFIG_VDEV_TREE);
263 uint_t vsc;
264 vdev_stat_t *vs = (vdev_stat_t *)fnvlist_lookup_uint64_array(
265 nvroot, ZPOOL_CONFIG_VDEV_STATS, &vsc);
f0ed6c74
TH
266 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
267 }
268 return (str);
269}
270
8b921f66
RE
271/*
272 * Get a zpool property value for 'prop' and return the value in
273 * a pre-allocated buffer.
274 */
275int
2a8b84b7 276zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
d1d7e268 277 size_t len, zprop_source_t *srctype, boolean_t literal)
34dc7c2f
BB
278{
279 uint64_t intval;
280 const char *strval;
281 zprop_source_t src = ZPROP_SRC_NONE;
34dc7c2f
BB
282
283 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
d164b209
BB
284 switch (prop) {
285 case ZPOOL_PROP_NAME:
34dc7c2f 286 (void) strlcpy(buf, zpool_get_name(zhp), len);
d164b209
BB
287 break;
288
289 case ZPOOL_PROP_HEALTH:
f0ed6c74 290 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
d164b209
BB
291 break;
292
293 case ZPOOL_PROP_GUID:
294 intval = zpool_get_prop_int(zhp, prop, &src);
b8864a23 295 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
d164b209
BB
296 break;
297
298 case ZPOOL_PROP_ALTROOT:
299 case ZPOOL_PROP_CACHEFILE:
d96eb2b1 300 case ZPOOL_PROP_COMMENT:
658fb802 301 case ZPOOL_PROP_COMPATIBILITY:
d164b209
BB
302 if (zhp->zpool_props != NULL ||
303 zpool_get_all_props(zhp) == 0) {
304 (void) strlcpy(buf,
305 zpool_get_prop_string(zhp, prop, &src),
306 len);
2a8b84b7 307 break;
d164b209 308 }
9a70e97f 309 zfs_fallthrough;
d164b209 310 default:
34dc7c2f 311 (void) strlcpy(buf, "-", len);
d164b209
BB
312 break;
313 }
314
315 if (srctype != NULL)
316 *srctype = src;
34dc7c2f
BB
317 return (0);
318 }
319
320 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
321 prop != ZPOOL_PROP_NAME)
322 return (-1);
323
324 switch (zpool_prop_get_type(prop)) {
325 case PROP_TYPE_STRING:
326 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
327 len);
328 break;
329
330 case PROP_TYPE_NUMBER:
331 intval = zpool_get_prop_int(zhp, prop, &src);
332
333 switch (prop) {
334 case ZPOOL_PROP_SIZE:
428870ff
BB
335 case ZPOOL_PROP_ALLOCATED:
336 case ZPOOL_PROP_FREE:
9ae529ec 337 case ZPOOL_PROP_FREEING:
fbeddd60 338 case ZPOOL_PROP_LEAKED:
df30f566 339 case ZPOOL_PROP_ASHIFT:
be8e1d81
AZ
340 case ZPOOL_PROP_MAXBLOCKSIZE:
341 case ZPOOL_PROP_MAXDNODESIZE:
8b921f66
RE
342 if (literal)
343 (void) snprintf(buf, len, "%llu",
02730c33 344 (u_longlong_t)intval);
8b921f66
RE
345 else
346 (void) zfs_nicenum(intval, buf, len);
34dc7c2f
BB
347 break;
348
a05dfd00 349 case ZPOOL_PROP_EXPANDSZ:
d2734cce 350 case ZPOOL_PROP_CHECKPOINT:
a05dfd00
GW
351 if (intval == 0) {
352 (void) strlcpy(buf, "-", len);
353 } else if (literal) {
354 (void) snprintf(buf, len, "%llu",
355 (u_longlong_t)intval);
356 } else {
e7fbeb60 357 (void) zfs_nicebytes(intval, buf, len);
a05dfd00
GW
358 }
359 break;
360
34dc7c2f 361 case ZPOOL_PROP_CAPACITY:
2a8b84b7
AS
362 if (literal) {
363 (void) snprintf(buf, len, "%llu",
364 (u_longlong_t)intval);
365 } else {
366 (void) snprintf(buf, len, "%llu%%",
367 (u_longlong_t)intval);
368 }
34dc7c2f
BB
369 break;
370
1ca56e60 371 case ZPOOL_PROP_FRAGMENTATION:
372 if (intval == UINT64_MAX) {
373 (void) strlcpy(buf, "-", len);
bc2d8093
CE
374 } else if (literal) {
375 (void) snprintf(buf, len, "%llu",
376 (u_longlong_t)intval);
1ca56e60 377 } else {
378 (void) snprintf(buf, len, "%llu%%",
379 (u_longlong_t)intval);
380 }
381 break;
382
428870ff 383 case ZPOOL_PROP_DEDUPRATIO:
bc2d8093
CE
384 if (literal)
385 (void) snprintf(buf, len, "%llu.%02llu",
386 (u_longlong_t)(intval / 100),
387 (u_longlong_t)(intval % 100));
388 else
389 (void) snprintf(buf, len, "%llu.%02llux",
390 (u_longlong_t)(intval / 100),
391 (u_longlong_t)(intval % 100));
428870ff
BB
392 break;
393
34dc7c2f 394 case ZPOOL_PROP_HEALTH:
f0ed6c74 395 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
34dc7c2f 396 break;
9ae529ec
CS
397 case ZPOOL_PROP_VERSION:
398 if (intval >= SPA_VERSION_FEATURES) {
399 (void) snprintf(buf, len, "-");
400 break;
401 }
9a70e97f 402 zfs_fallthrough;
34dc7c2f 403 default:
b8864a23 404 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
34dc7c2f
BB
405 }
406 break;
407
408 case PROP_TYPE_INDEX:
409 intval = zpool_get_prop_int(zhp, prop, &src);
410 if (zpool_prop_index_to_string(prop, intval, &strval)
411 != 0)
412 return (-1);
413 (void) strlcpy(buf, strval, len);
414 break;
415
416 default:
417 abort();
418 }
419
420 if (srctype)
421 *srctype = src;
422
423 return (0);
424}
425
426/*
427 * Check if the bootfs name has the same pool name as it is set to.
428 * Assuming bootfs is a valid dataset name.
429 */
430static boolean_t
108a454a 431bootfs_name_valid(const char *pool, const char *bootfs)
34dc7c2f
BB
432{
433 int len = strlen(pool);
387b6856
PD
434 if (bootfs[0] == '\0')
435 return (B_TRUE);
34dc7c2f 436
b128c09f 437 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
34dc7c2f
BB
438 return (B_FALSE);
439
440 if (strncmp(pool, bootfs, len) == 0 &&
441 (bootfs[len] == '/' || bootfs[len] == '\0'))
442 return (B_TRUE);
443
444 return (B_FALSE);
445}
446
447/*
448 * Given an nvlist of zpool properties to be set, validate that they are
449 * correct, and parse any numeric properties (index, boolean, etc) if they are
450 * specified as strings.
451 */
452static nvlist_t *
b128c09f 453zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
572e2857 454 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
34dc7c2f
BB
455{
456 nvpair_t *elem;
457 nvlist_t *retprops;
458 zpool_prop_t prop;
459 char *strval;
460 uint64_t intval;
d96eb2b1 461 char *slash, *check;
34dc7c2f 462 struct stat64 statbuf;
b128c09f 463 zpool_handle_t *zhp;
e086db16 464 char report[1024];
34dc7c2f
BB
465
466 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
467 (void) no_memory(hdl);
468 return (NULL);
469 }
470
471 elem = NULL;
472 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
473 const char *propname = nvpair_name(elem);
474
2a673e76
AJ
475 if (flags.vdevprop && zpool_prop_vdev(propname)) {
476 vdev_prop_t vprop = vdev_name_to_prop(propname);
477
478 if (vdev_prop_readonly(vprop)) {
479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
480 "is readonly"), propname);
481 (void) zfs_error(hdl, EZFS_PROPREADONLY,
482 errbuf);
483 goto error;
484 }
485
486 if (zprop_parse_value(hdl, elem, vprop, ZFS_TYPE_VDEV,
487 retprops, &strval, &intval, errbuf) != 0)
488 goto error;
489
490 continue;
491 } else if (flags.vdevprop && vdev_prop_user(propname)) {
492 if (nvlist_add_nvpair(retprops, elem) != 0) {
493 (void) no_memory(hdl);
494 goto error;
495 }
496 continue;
497 } else if (flags.vdevprop) {
498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
499 "invalid property: '%s'"), propname);
500 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
501 goto error;
502 }
503
9ae529ec 504 prop = zpool_name_to_prop(propname);
31864e3d 505 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
9ae529ec 506 int err;
9ae529ec
CS
507 char *fname = strchr(propname, '@') + 1;
508
fa86b5db 509 err = zfeature_lookup_name(fname, NULL);
9ae529ec
CS
510 if (err != 0) {
511 ASSERT3U(err, ==, ENOENT);
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b2255edc
BB
513 "feature '%s' unsupported by kernel"),
514 fname);
9ae529ec
CS
515 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
516 goto error;
517 }
518
519 if (nvpair_type(elem) != DATA_TYPE_STRING) {
520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521 "'%s' must be a string"), propname);
522 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
523 goto error;
524 }
525
526 (void) nvpair_value_string(elem, &strval);
e4010f27 527 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
528 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
9ae529ec
CS
529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
530 "property '%s' can only be set to "
e4010f27 531 "'enabled' or 'disabled'"), propname);
9966754a 532 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
533 goto error;
534 }
535
536 if (!flags.create &&
537 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
539 "property '%s' can only be set to "
540 "'disabled' at creation time"), propname);
9ae529ec
CS
541 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
542 goto error;
543 }
544
545 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
546 (void) no_memory(hdl);
547 goto error;
548 }
549 continue;
550 }
551
34dc7c2f
BB
552 /*
553 * Make sure this property is valid and applies to this type.
554 */
31864e3d 555 if (prop == ZPOOL_PROP_INVAL) {
34dc7c2f
BB
556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
557 "invalid property '%s'"), propname);
558 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
559 goto error;
560 }
561
562 if (zpool_prop_readonly(prop)) {
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
564 "is readonly"), propname);
565 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
566 goto error;
567 }
568
edf60b86
CC
569 if (!flags.create && zpool_prop_setonce(prop)) {
570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
571 "property '%s' can only be set at "
572 "creation time"), propname);
573 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
574 goto error;
575 }
576
34dc7c2f
BB
577 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
578 &strval, &intval, errbuf) != 0)
579 goto error;
580
581 /*
582 * Perform additional checking for specific properties.
583 */
584 switch (prop) {
585 case ZPOOL_PROP_VERSION:
9ae529ec
CS
586 if (intval < version ||
587 !SPA_VERSION_IS_SUPPORTED(intval)) {
34dc7c2f 588 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
f00f4690
AZ
589 "property '%s' number %llu is invalid."),
590 propname, (unsigned long long)intval);
34dc7c2f
BB
591 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
592 goto error;
593 }
594 break;
595
df30f566 596 case ZPOOL_PROP_ASHIFT:
ff61d1a4 597 if (intval != 0 &&
598 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
df30f566 599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
f00f4690
AZ
600 "property '%s' number %llu is invalid, "
601 "only values between %" PRId32 " and %"
602 PRId32 " are allowed."),
603 propname, (unsigned long long)intval,
604 ASHIFT_MIN, ASHIFT_MAX);
df30f566
CK
605 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
606 goto error;
607 }
608 break;
609
34dc7c2f 610 case ZPOOL_PROP_BOOTFS:
572e2857 611 if (flags.create || flags.import) {
34dc7c2f
BB
612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
613 "property '%s' cannot be set at creation "
614 "or import time"), propname);
615 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
616 goto error;
617 }
618
619 if (version < SPA_VERSION_BOOTFS) {
620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
621 "pool must be upgraded to support "
622 "'%s' property"), propname);
623 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
624 goto error;
625 }
626
627 /*
628 * bootfs property value has to be a dataset name and
629 * the dataset has to be in the same pool as it sets to.
630 */
387b6856 631 if (!bootfs_name_valid(poolname, strval)) {
34dc7c2f
BB
632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
633 "is an invalid name"), strval);
634 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
635 goto error;
636 }
b128c09f
BB
637
638 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
640 "could not open pool '%s'"), poolname);
641 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
642 goto error;
643 }
b128c09f 644 zpool_close(zhp);
34dc7c2f
BB
645 break;
646
647 case ZPOOL_PROP_ALTROOT:
572e2857 648 if (!flags.create && !flags.import) {
34dc7c2f
BB
649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
650 "property '%s' can only be set during pool "
651 "creation or import"), propname);
652 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
653 goto error;
654 }
655
656 if (strval[0] != '/') {
657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
658 "bad alternate root '%s'"), strval);
659 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
660 goto error;
661 }
662 break;
663
664 case ZPOOL_PROP_CACHEFILE:
665 if (strval[0] == '\0')
666 break;
667
668 if (strcmp(strval, "none") == 0)
669 break;
670
671 if (strval[0] != '/') {
672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
673 "property '%s' must be empty, an "
674 "absolute path, or 'none'"), propname);
675 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
676 goto error;
677 }
678
679 slash = strrchr(strval, '/');
680
681 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
682 strcmp(slash, "/..") == 0) {
683 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
684 "'%s' is not a valid file"), strval);
685 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
686 goto error;
687 }
688
689 *slash = '\0';
690
691 if (strval[0] != '\0' &&
692 (stat64(strval, &statbuf) != 0 ||
693 !S_ISDIR(statbuf.st_mode))) {
694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
695 "'%s' is not a valid directory"),
696 strval);
697 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
698 goto error;
699 }
700
701 *slash = '/';
702 break;
572e2857 703
658fb802 704 case ZPOOL_PROP_COMPATIBILITY:
e086db16 705 switch (zpool_load_compat(strval, NULL, report, 1024)) {
658fb802 706 case ZPOOL_COMPATIBILITY_OK:
e086db16 707 case ZPOOL_COMPATIBILITY_WARNTOKEN:
658fb802 708 break;
658fb802 709 case ZPOOL_COMPATIBILITY_BADFILE:
e086db16 710 case ZPOOL_COMPATIBILITY_BADTOKEN:
658fb802 711 case ZPOOL_COMPATIBILITY_NOFILES:
f00f4690 712 zfs_error_aux(hdl, "%s", report);
658fb802
CB
713 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
714 goto error;
715 }
716 break;
717
d96eb2b1
DM
718 case ZPOOL_PROP_COMMENT:
719 for (check = strval; *check != '\0'; check++) {
720 if (!isprint(*check)) {
721 zfs_error_aux(hdl,
722 dgettext(TEXT_DOMAIN,
723 "comment may only have printable "
724 "characters"));
725 (void) zfs_error(hdl, EZFS_BADPROP,
726 errbuf);
727 goto error;
728 }
729 }
730 if (strlen(strval) > ZPROP_MAX_COMMENT) {
731 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
732 "comment must not exceed %d characters"),
733 ZPROP_MAX_COMMENT);
734 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
735 goto error;
736 }
737 break;
572e2857
BB
738 case ZPOOL_PROP_READONLY:
739 if (!flags.import) {
740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
741 "property '%s' can only be set at "
742 "import time"), propname);
743 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
744 goto error;
745 }
746 break;
379ca9cf
OF
747 case ZPOOL_PROP_MULTIHOST:
748 if (get_system_hostid() == 0) {
749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
750 "requires a non-zero system hostid"));
751 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
752 goto error;
753 }
754 break;
f0f97865 755 case ZPOOL_PROP_DEDUPDITTO:
050d720c
MA
756 printf("Note: property '%s' no longer has "
757 "any effect\n", propname);
f0f97865 758 break;
cc99f275 759
23d70cde 760 default:
83e9986f 761 break;
34dc7c2f
BB
762 }
763 }
764
765 return (retprops);
766error:
767 nvlist_free(retprops);
768 return (NULL);
769}
770
771/*
772 * Set zpool property : propname=propval.
773 */
774int
775zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
776{
13fe0198 777 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
778 int ret = -1;
779 char errbuf[1024];
780 nvlist_t *nvl = NULL;
781 nvlist_t *realprops;
782 uint64_t version;
572e2857 783 prop_flags_t flags = { 0 };
34dc7c2f
BB
784
785 (void) snprintf(errbuf, sizeof (errbuf),
786 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
787 zhp->zpool_name);
788
34dc7c2f
BB
789 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
790 return (no_memory(zhp->zpool_hdl));
791
792 if (nvlist_add_string(nvl, propname, propval) != 0) {
793 nvlist_free(nvl);
794 return (no_memory(zhp->zpool_hdl));
795 }
796
797 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
b128c09f 798 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
572e2857 799 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
34dc7c2f
BB
800 nvlist_free(nvl);
801 return (-1);
802 }
803
804 nvlist_free(nvl);
805 nvl = realprops;
806
807 /*
808 * Execute the corresponding ioctl() to set this property.
809 */
810 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
811
18dbf5c8 812 zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl);
34dc7c2f
BB
813
814 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
815
816 zcmd_free_nvlists(&zc);
817 nvlist_free(nvl);
818
819 if (ret)
820 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
821 else
822 (void) zpool_props_refresh(zhp);
823
824 return (ret);
825}
826
827int
e6c59cd1 828zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp,
2a673e76 829 zfs_type_t type, boolean_t literal)
34dc7c2f
BB
830{
831 libzfs_handle_t *hdl = zhp->zpool_hdl;
832 zprop_list_t *entry;
833 char buf[ZFS_MAXPROPLEN];
9ae529ec
CS
834 nvlist_t *features = NULL;
835 nvpair_t *nvp;
836 zprop_list_t **last;
837 boolean_t firstexpand = (NULL == *plp);
838 int i;
34dc7c2f 839
2a673e76 840 if (zprop_expand_list(hdl, plp, type) != 0)
34dc7c2f
BB
841 return (-1);
842
2a673e76
AJ
843 if (type == ZFS_TYPE_VDEV)
844 return (0);
845
9ae529ec
CS
846 last = plp;
847 while (*last != NULL)
848 last = &(*last)->pl_next;
849
850 if ((*plp)->pl_all)
851 features = zpool_get_features(zhp);
852
853 if ((*plp)->pl_all && firstexpand) {
854 for (i = 0; i < SPA_FEATURES; i++) {
855 zprop_list_t *entry = zfs_alloc(hdl,
856 sizeof (zprop_list_t));
857 entry->pl_prop = ZPROP_INVAL;
858 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
859 spa_feature_table[i].fi_uname);
860 entry->pl_width = strlen(entry->pl_user_prop);
861 entry->pl_all = B_TRUE;
862
863 *last = entry;
864 last = &entry->pl_next;
865 }
866 }
867
868 /* add any unsupported features */
869 for (nvp = nvlist_next_nvpair(features, NULL);
870 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
871 char *propname;
872 boolean_t found;
873 zprop_list_t *entry;
874
875 if (zfeature_is_supported(nvpair_name(nvp)))
876 continue;
877
878 propname = zfs_asprintf(hdl, "unsupported@%s",
879 nvpair_name(nvp));
880
881 /*
882 * Before adding the property to the list make sure that no
883 * other pool already added the same property.
884 */
885 found = B_FALSE;
886 entry = *plp;
887 while (entry != NULL) {
888 if (entry->pl_user_prop != NULL &&
889 strcmp(propname, entry->pl_user_prop) == 0) {
890 found = B_TRUE;
891 break;
892 }
893 entry = entry->pl_next;
894 }
895 if (found) {
896 free(propname);
897 continue;
898 }
899
900 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
901 entry->pl_prop = ZPROP_INVAL;
902 entry->pl_user_prop = propname;
903 entry->pl_width = strlen(entry->pl_user_prop);
904 entry->pl_all = B_TRUE;
905
906 *last = entry;
907 last = &entry->pl_next;
908 }
909
34dc7c2f 910 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
e6c59cd1 911 if (entry->pl_fixed && !literal)
34dc7c2f
BB
912 continue;
913
914 if (entry->pl_prop != ZPROP_INVAL &&
915 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
e6c59cd1 916 NULL, literal) == 0) {
34dc7c2f
BB
917 if (strlen(buf) > entry->pl_width)
918 entry->pl_width = strlen(buf);
919 }
920 }
921
922 return (0);
923}
924
2a673e76
AJ
925int
926vdev_expand_proplist(zpool_handle_t *zhp, const char *vdevname,
927 zprop_list_t **plp)
928{
929 zprop_list_t *entry;
930 char buf[ZFS_MAXPROPLEN];
931 char *strval = NULL;
932 int err = 0;
933 nvpair_t *elem = NULL;
934 nvlist_t *vprops = NULL;
935 nvlist_t *propval = NULL;
936 const char *propname;
937 vdev_prop_t prop;
938 zprop_list_t **last;
939
940 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
941 if (entry->pl_fixed)
942 continue;
943
944 if (zpool_get_vdev_prop(zhp, vdevname, entry->pl_prop,
945 entry->pl_user_prop, buf, sizeof (buf), NULL,
946 B_FALSE) == 0) {
947 if (strlen(buf) > entry->pl_width)
948 entry->pl_width = strlen(buf);
949 }
950 if (entry->pl_prop == VDEV_PROP_NAME &&
951 strlen(vdevname) > entry->pl_width)
952 entry->pl_width = strlen(vdevname);
953 }
954
955 /* Handle the all properties case */
956 last = plp;
957 if (*last != NULL && (*last)->pl_all == B_TRUE) {
958 while (*last != NULL)
959 last = &(*last)->pl_next;
960
961 err = zpool_get_all_vdev_props(zhp, vdevname, &vprops);
962 if (err != 0)
963 return (err);
964
965 while ((elem = nvlist_next_nvpair(vprops, elem)) != NULL) {
966 propname = nvpair_name(elem);
967
968 /* Skip properties that are not user defined */
969 if ((prop = vdev_name_to_prop(propname)) !=
970 VDEV_PROP_USER)
971 continue;
972
973 if (nvpair_value_nvlist(elem, &propval) != 0)
974 continue;
975
8bb9ecf4 976 strval = fnvlist_lookup_string(propval, ZPROP_VALUE);
2a673e76 977
18dbf5c8
AZ
978 entry = zfs_alloc(zhp->zpool_hdl,
979 sizeof (zprop_list_t));
2a673e76
AJ
980 entry->pl_prop = prop;
981 entry->pl_user_prop = zfs_strdup(zhp->zpool_hdl,
982 propname);
983 entry->pl_width = strlen(strval);
984 entry->pl_all = B_TRUE;
985 *last = entry;
986 last = &entry->pl_next;
987 }
988 }
989
990 return (0);
991}
992
9ae529ec
CS
993/*
994 * Get the state for the given feature on the given ZFS pool.
995 */
996int
997zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
998 size_t len)
999{
1000 uint64_t refcount;
1001 boolean_t found = B_FALSE;
1002 nvlist_t *features = zpool_get_features(zhp);
1003 boolean_t supported;
1004 const char *feature = strchr(propname, '@') + 1;
1005
1006 supported = zpool_prop_feature(propname);
1007 ASSERT(supported || zpool_prop_unsupported(propname));
1008
1009 /*
1010 * Convert from feature name to feature guid. This conversion is
4e33ba4c 1011 * unnecessary for unsupported@... properties because they already
9ae529ec
CS
1012 * use guids.
1013 */
1014 if (supported) {
1015 int ret;
fa86b5db 1016 spa_feature_t fid;
9ae529ec 1017
fa86b5db 1018 ret = zfeature_lookup_name(feature, &fid);
9ae529ec
CS
1019 if (ret != 0) {
1020 (void) strlcpy(buf, "-", len);
1021 return (ENOTSUP);
1022 }
fa86b5db 1023 feature = spa_feature_table[fid].fi_guid;
9ae529ec
CS
1024 }
1025
1026 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
1027 found = B_TRUE;
1028
1029 if (supported) {
1030 if (!found) {
1031 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
1032 } else {
1033 if (refcount == 0)
1034 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
1035 else
1036 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
1037 }
1038 } else {
1039 if (found) {
1040 if (refcount == 0) {
1041 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
1042 } else {
1043 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
1044 }
1045 } else {
1046 (void) strlcpy(buf, "-", len);
1047 return (ENOTSUP);
1048 }
1049 }
1050
1051 return (0);
1052}
34dc7c2f
BB
1053
1054/*
1055 * Validate the given pool name, optionally putting an extended error message in
1056 * 'buf'.
1057 */
1058boolean_t
1059zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
1060{
1061 namecheck_err_t why;
1062 char what;
1063 int ret;
1064
1065 ret = pool_namecheck(pool, &why, &what);
1066
1067 /*
1068 * The rules for reserved pool names were extended at a later point.
1069 * But we need to support users with existing pools that may now be
1070 * invalid. So we only check for this expanded set of names during a
1071 * create (or import), and only in userland.
1072 */
1073 if (ret == 0 && !isopen &&
1074 (strncmp(pool, "mirror", 6) == 0 ||
1075 strncmp(pool, "raidz", 5) == 0 ||
b2255edc 1076 strncmp(pool, "draid", 5) == 0 ||
34dc7c2f
BB
1077 strncmp(pool, "spare", 5) == 0 ||
1078 strcmp(pool, "log") == 0)) {
1079 if (hdl != NULL)
1080 zfs_error_aux(hdl,
1081 dgettext(TEXT_DOMAIN, "name is reserved"));
1082 return (B_FALSE);
1083 }
1084
1085
1086 if (ret != 0) {
1087 if (hdl != NULL) {
1088 switch (why) {
1089 case NAME_ERR_TOOLONG:
1090 zfs_error_aux(hdl,
1091 dgettext(TEXT_DOMAIN, "name is too long"));
1092 break;
1093
1094 case NAME_ERR_INVALCHAR:
1095 zfs_error_aux(hdl,
1096 dgettext(TEXT_DOMAIN, "invalid character "
1097 "'%c' in pool name"), what);
1098 break;
1099
1100 case NAME_ERR_NOLETTER:
1101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1102 "name must begin with a letter"));
1103 break;
1104
1105 case NAME_ERR_RESERVED:
1106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1107 "name is reserved"));
1108 break;
1109
1110 case NAME_ERR_DISKLIKE:
1111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1112 "pool name is reserved"));
1113 break;
1114
1115 case NAME_ERR_LEADING_SLASH:
1116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1117 "leading slash in name"));
1118 break;
1119
1120 case NAME_ERR_EMPTY_COMPONENT:
1121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1122 "empty component in name"));
1123 break;
1124
1125 case NAME_ERR_TRAILING_SLASH:
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127 "trailing slash in name"));
1128 break;
1129
aeacdefe 1130 case NAME_ERR_MULTIPLE_DELIMITERS:
34dc7c2f 1131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
aeacdefe
GM
1132 "multiple '@' and/or '#' delimiters in "
1133 "name"));
34dc7c2f 1134 break;
97dde921 1135
e75c13c3
BB
1136 case NAME_ERR_NO_AT:
1137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1138 "permission set is missing '@'"));
97dde921 1139 break;
23d70cde
GM
1140
1141 default:
1142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1143 "(%d) not defined"), why);
e75c13c3 1144 break;
34dc7c2f
BB
1145 }
1146 }
1147 return (B_FALSE);
1148 }
1149
1150 return (B_TRUE);
1151}
1152
1153/*
1154 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1155 * state.
1156 */
1157zpool_handle_t *
1158zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1159{
1160 zpool_handle_t *zhp;
1161 boolean_t missing;
1162
1163 /*
1164 * Make sure the pool name is valid.
1165 */
1166 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1167 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1168 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1169 pool);
1170 return (NULL);
1171 }
1172
18dbf5c8 1173 zhp = zfs_alloc(hdl, sizeof (zpool_handle_t));
34dc7c2f
BB
1174
1175 zhp->zpool_hdl = hdl;
1176 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1177
1178 if (zpool_refresh_stats(zhp, &missing) != 0) {
1179 zpool_close(zhp);
1180 return (NULL);
1181 }
1182
1183 if (missing) {
1184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1185 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1186 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1187 zpool_close(zhp);
1188 return (NULL);
1189 }
1190
1191 return (zhp);
1192}
1193
1194/*
1195 * Like the above, but silent on error. Used when iterating over pools (because
1196 * the configuration cache may be out of date).
1197 */
1198int
1199zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1200{
1201 zpool_handle_t *zhp;
1202 boolean_t missing;
1203
18dbf5c8 1204 zhp = zfs_alloc(hdl, sizeof (zpool_handle_t));
34dc7c2f
BB
1205
1206 zhp->zpool_hdl = hdl;
1207 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1208
1209 if (zpool_refresh_stats(zhp, &missing) != 0) {
1210 zpool_close(zhp);
1211 return (-1);
1212 }
1213
1214 if (missing) {
1215 zpool_close(zhp);
1216 *ret = NULL;
1217 return (0);
1218 }
1219
1220 *ret = zhp;
1221 return (0);
1222}
1223
1224/*
1225 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1226 * state.
1227 */
1228zpool_handle_t *
1229zpool_open(libzfs_handle_t *hdl, const char *pool)
1230{
1231 zpool_handle_t *zhp;
1232
1233 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1234 return (NULL);
1235
1236 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1237 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1238 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1239 zpool_close(zhp);
1240 return (NULL);
1241 }
1242
1243 return (zhp);
1244}
1245
1246/*
1247 * Close the handle. Simply frees the memory associated with the handle.
1248 */
1249void
1250zpool_close(zpool_handle_t *zhp)
1251{
8a5fc748
JJS
1252 nvlist_free(zhp->zpool_config);
1253 nvlist_free(zhp->zpool_old_config);
1254 nvlist_free(zhp->zpool_props);
34dc7c2f
BB
1255 free(zhp);
1256}
1257
1258/*
1259 * Return the name of the pool.
1260 */
1261const char *
1262zpool_get_name(zpool_handle_t *zhp)
1263{
1264 return (zhp->zpool_name);
1265}
1266
1267
1268/*
1269 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1270 */
1271int
1272zpool_get_state(zpool_handle_t *zhp)
1273{
1274 return (zhp->zpool_state);
1275}
1276
cc99f275
DB
1277/*
1278 * Check if vdev list contains a special vdev
1279 */
1280static boolean_t
1281zpool_has_special_vdev(nvlist_t *nvroot)
1282{
1283 nvlist_t **child;
1284 uint_t children;
1285
1286 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1287 &children) == 0) {
1288 for (uint_t c = 0; c < children; c++) {
1289 char *bias;
1290
1291 if (nvlist_lookup_string(child[c],
1292 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1293 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1294 return (B_TRUE);
1295 }
1296 }
1297 }
1298 return (B_FALSE);
1299}
1300
76e1f78d
BB
1301/*
1302 * Check if vdev list contains a dRAID vdev
1303 */
1304static boolean_t
1305zpool_has_draid_vdev(nvlist_t *nvroot)
1306{
1307 nvlist_t **child;
1308 uint_t children;
1309
1310 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1311 &child, &children) == 0) {
1312 for (uint_t c = 0; c < children; c++) {
1313 char *type;
1314
1315 if (nvlist_lookup_string(child[c],
1316 ZPOOL_CONFIG_TYPE, &type) == 0 &&
1317 strcmp(type, VDEV_TYPE_DRAID) == 0) {
1318 return (B_TRUE);
1319 }
1320 }
1321 }
1322 return (B_FALSE);
1323}
1324
b2255edc
BB
1325/*
1326 * Output a dRAID top-level vdev name in to the provided buffer.
1327 */
1328static char *
1329zpool_draid_name(char *name, int len, uint64_t data, uint64_t parity,
1330 uint64_t spares, uint64_t children)
1331{
1332 snprintf(name, len, "%s%llu:%llud:%lluc:%llus",
1333 VDEV_TYPE_DRAID, (u_longlong_t)parity, (u_longlong_t)data,
1334 (u_longlong_t)children, (u_longlong_t)spares);
1335
1336 return (name);
1337}
1338
1339/*
1340 * Return B_TRUE if the provided name is a dRAID spare name.
1341 */
1342boolean_t
1343zpool_is_draid_spare(const char *name)
1344{
1345 uint64_t spare_id, parity, vdev_id;
1346
1347 if (sscanf(name, VDEV_TYPE_DRAID "%llu-%llu-%llu",
1348 (u_longlong_t *)&parity, (u_longlong_t *)&vdev_id,
1349 (u_longlong_t *)&spare_id) == 3) {
1350 return (B_TRUE);
1351 }
1352
1353 return (B_FALSE);
1354}
1355
34dc7c2f
BB
1356/*
1357 * Create the named pool, using the provided vdev list. It is assumed
1358 * that the consumer has already validated the contents of the nvlist, so we
1359 * don't have to worry about error semantics.
1360 */
1361int
1362zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
b128c09f 1363 nvlist_t *props, nvlist_t *fsprops)
34dc7c2f 1364{
13fe0198 1365 zfs_cmd_t zc = {"\0"};
b128c09f
BB
1366 nvlist_t *zc_fsprops = NULL;
1367 nvlist_t *zc_props = NULL;
b5256303
TC
1368 nvlist_t *hidden_args = NULL;
1369 uint8_t *wkeydata = NULL;
1370 uint_t wkeylen = 0;
34dc7c2f 1371 char msg[1024];
b128c09f 1372 int ret = -1;
34dc7c2f
BB
1373
1374 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1375 "cannot create '%s'"), pool);
1376
1377 if (!zpool_name_valid(hdl, B_FALSE, pool))
1378 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1379
18dbf5c8 1380 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
34dc7c2f 1381
b128c09f 1382 if (props) {
572e2857
BB
1383 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1384
b128c09f 1385 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
572e2857 1386 SPA_VERSION_1, flags, msg)) == NULL) {
b128c09f
BB
1387 goto create_failed;
1388 }
1389 }
34dc7c2f 1390
b128c09f
BB
1391 if (fsprops) {
1392 uint64_t zoned;
1393 char *zonestr;
1394
1395 zoned = ((nvlist_lookup_string(fsprops,
1396 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1397 strcmp(zonestr, "on") == 0);
1398
82f6f6e6 1399 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
b5256303 1400 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
b128c09f
BB
1401 goto create_failed;
1402 }
cc99f275
DB
1403
1404 if (nvlist_exists(zc_fsprops,
1405 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1406 !zpool_has_special_vdev(nvroot)) {
1407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1408 "%s property requires a special vdev"),
1409 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1410 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1411 goto create_failed;
1412 }
1413
b128c09f
BB
1414 if (!zc_props &&
1415 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1416 goto create_failed;
1417 }
d9c460a0 1418 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
b5256303
TC
1419 &wkeydata, &wkeylen) != 0) {
1420 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1421 goto create_failed;
1422 }
b128c09f
BB
1423 if (nvlist_add_nvlist(zc_props,
1424 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1425 goto create_failed;
1426 }
b5256303
TC
1427 if (wkeydata != NULL) {
1428 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1429 goto create_failed;
1430
1431 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1432 wkeydata, wkeylen) != 0)
1433 goto create_failed;
1434
1435 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1436 hidden_args) != 0)
1437 goto create_failed;
1438 }
34dc7c2f
BB
1439 }
1440
18dbf5c8
AZ
1441 if (zc_props)
1442 zcmd_write_src_nvlist(hdl, &zc, zc_props);
b128c09f 1443
34dc7c2f
BB
1444 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1445
b128c09f 1446 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
34dc7c2f
BB
1447
1448 zcmd_free_nvlists(&zc);
b128c09f
BB
1449 nvlist_free(zc_props);
1450 nvlist_free(zc_fsprops);
b5256303
TC
1451 nvlist_free(hidden_args);
1452 if (wkeydata != NULL)
1453 free(wkeydata);
34dc7c2f
BB
1454
1455 switch (errno) {
1456 case EBUSY:
1457 /*
1458 * This can happen if the user has specified the same
1459 * device multiple times. We can't reliably detect this
1460 * until we try to add it and see we already have a
d603ed6c
BB
1461 * label. This can also happen under if the device is
1462 * part of an active md or lvm device.
34dc7c2f
BB
1463 */
1464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
d1d7e268
MK
1465 "one or more vdevs refer to the same device, or "
1466 "one of\nthe devices is part of an active md or "
1467 "lvm device"));
34dc7c2f
BB
1468 return (zfs_error(hdl, EZFS_BADDEV, msg));
1469
82f6f6e6
JS
1470 case ERANGE:
1471 /*
1472 * This happens if the record size is smaller or larger
1473 * than the allowed size range, or not a power of 2.
1474 *
1475 * NOTE: although zfs_valid_proplist is called earlier,
1476 * this case may have slipped through since the
1477 * pool does not exist yet and it is therefore
1478 * impossible to read properties e.g. max blocksize
1479 * from the pool.
1480 */
1481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482 "record size invalid"));
1483 return (zfs_error(hdl, EZFS_BADPROP, msg));
1484
34dc7c2f
BB
1485 case EOVERFLOW:
1486 /*
1487 * This occurs when one of the devices is below
1488 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1489 * device was the problem device since there's no
1490 * reliable way to determine device size from userland.
1491 */
1492 {
1493 char buf[64];
1494
e7fbeb60 1495 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1496 sizeof (buf));
34dc7c2f
BB
1497
1498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1499 "one or more devices is less than the "
1500 "minimum size (%s)"), buf);
1501 }
1502 return (zfs_error(hdl, EZFS_BADDEV, msg));
1503
1504 case ENOSPC:
1505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506 "one or more devices is out of space"));
1507 return (zfs_error(hdl, EZFS_BADDEV, msg));
1508
76e1f78d
BB
1509 case EINVAL:
1510 if (zpool_has_draid_vdev(nvroot) &&
1511 zfeature_lookup_name("draid", NULL) != 0) {
1512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1513 "dRAID vdevs are unsupported by the "
1514 "kernel"));
1515 return (zfs_error(hdl, EZFS_BADDEV, msg));
1516 } else {
1517 return (zpool_standard_error(hdl, errno, msg));
1518 }
1519
34dc7c2f
BB
1520 default:
1521 return (zpool_standard_error(hdl, errno, msg));
1522 }
1523 }
1524
b128c09f 1525create_failed:
34dc7c2f 1526 zcmd_free_nvlists(&zc);
b128c09f
BB
1527 nvlist_free(zc_props);
1528 nvlist_free(zc_fsprops);
b5256303
TC
1529 nvlist_free(hidden_args);
1530 if (wkeydata != NULL)
1531 free(wkeydata);
b128c09f 1532 return (ret);
34dc7c2f
BB
1533}
1534
1535/*
1536 * Destroy the given pool. It is up to the caller to ensure that there are no
1537 * datasets left in the pool.
1538 */
1539int
6f1ffb06 1540zpool_destroy(zpool_handle_t *zhp, const char *log_str)
34dc7c2f 1541{
13fe0198 1542 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
1543 zfs_handle_t *zfp = NULL;
1544 libzfs_handle_t *hdl = zhp->zpool_hdl;
1545 char msg[1024];
1546
1547 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
572e2857 1548 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
34dc7c2f
BB
1549 return (-1);
1550
34dc7c2f 1551 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
6f1ffb06 1552 zc.zc_history = (uint64_t)(uintptr_t)log_str;
34dc7c2f 1553
572e2857 1554 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
34dc7c2f
BB
1555 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1556 "cannot destroy '%s'"), zhp->zpool_name);
1557
1558 if (errno == EROFS) {
1559 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1560 "one or more devices is read only"));
1561 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1562 } else {
1563 (void) zpool_standard_error(hdl, errno, msg);
1564 }
1565
1566 if (zfp)
1567 zfs_close(zfp);
1568 return (-1);
1569 }
1570
1571 if (zfp) {
1572 remove_mountpoint(zfp);
1573 zfs_close(zfp);
1574 }
1575
1576 return (0);
1577}
1578
d2734cce
SD
1579/*
1580 * Create a checkpoint in the given pool.
1581 */
1582int
1583zpool_checkpoint(zpool_handle_t *zhp)
1584{
1585 libzfs_handle_t *hdl = zhp->zpool_hdl;
1586 char msg[1024];
1587 int error;
1588
1589 error = lzc_pool_checkpoint(zhp->zpool_name);
1590 if (error != 0) {
1591 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1592 "cannot checkpoint '%s'"), zhp->zpool_name);
1593 (void) zpool_standard_error(hdl, error, msg);
1594 return (-1);
1595 }
1596
1597 return (0);
1598}
1599
1600/*
1601 * Discard the checkpoint from the given pool.
1602 */
1603int
1604zpool_discard_checkpoint(zpool_handle_t *zhp)
1605{
1606 libzfs_handle_t *hdl = zhp->zpool_hdl;
1607 char msg[1024];
1608 int error;
1609
1610 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1611 if (error != 0) {
1612 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1613 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1614 (void) zpool_standard_error(hdl, error, msg);
1615 return (-1);
1616 }
1617
1618 return (0);
1619}
1620
34dc7c2f
BB
1621/*
1622 * Add the given vdevs to the pool. The caller must have already performed the
1623 * necessary verification to ensure that the vdev specification is well-formed.
1624 */
1625int
1626zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1627{
13fe0198 1628 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
1629 int ret;
1630 libzfs_handle_t *hdl = zhp->zpool_hdl;
1631 char msg[1024];
1632 nvlist_t **spares, **l2cache;
1633 uint_t nspares, nl2cache;
1634
1635 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1636 "cannot add to '%s'"), zhp->zpool_name);
1637
1638 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1639 SPA_VERSION_SPARES &&
1640 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1641 &spares, &nspares) == 0) {
1642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1643 "upgraded to add hot spares"));
1644 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1645 }
1646
1647 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1648 SPA_VERSION_L2CACHE &&
1649 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1650 &l2cache, &nl2cache) == 0) {
1651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1652 "upgraded to add cache devices"));
1653 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1654 }
1655
18dbf5c8 1656 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
34dc7c2f
BB
1657 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1658
572e2857 1659 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
34dc7c2f
BB
1660 switch (errno) {
1661 case EBUSY:
1662 /*
1663 * This can happen if the user has specified the same
1664 * device multiple times. We can't reliably detect this
1665 * until we try to add it and see we already have a
1666 * label.
1667 */
1668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1669 "one or more vdevs refer to the same device"));
1670 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1671 break;
1672
a1d477c2 1673 case EINVAL:
76e1f78d
BB
1674
1675 if (zpool_has_draid_vdev(nvroot) &&
1676 zfeature_lookup_name("draid", NULL) != 0) {
1677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1678 "dRAID vdevs are unsupported by the "
1679 "kernel"));
1680 } else {
1681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1682 "invalid config; a pool with removing/"
1683 "removed vdevs does not support adding "
1684 "raidz or dRAID vdevs"));
1685 }
1686
a1d477c2
MA
1687 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1688 break;
1689
34dc7c2f
BB
1690 case EOVERFLOW:
1691 /*
78595377 1692 * This occurs when one of the devices is below
34dc7c2f
BB
1693 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1694 * device was the problem device since there's no
1695 * reliable way to determine device size from userland.
1696 */
1697 {
1698 char buf[64];
1699
e7fbeb60 1700 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1701 sizeof (buf));
34dc7c2f
BB
1702
1703 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1704 "device is less than the minimum "
1705 "size (%s)"), buf);
1706 }
1707 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1708 break;
1709
1710 case ENOTSUP:
1711 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1712 "pool must be upgraded to add these vdevs"));
1713 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1714 break;
1715
34dc7c2f
BB
1716 default:
1717 (void) zpool_standard_error(hdl, errno, msg);
1718 }
1719
1720 ret = -1;
1721 } else {
1722 ret = 0;
1723 }
1724
1725 zcmd_free_nvlists(&zc);
1726
1727 return (ret);
1728}
1729
1730/*
1731 * Exports the pool from the system. The caller must ensure that there are no
1732 * mounted datasets in the pool.
1733 */
6f1ffb06
MA
1734static int
1735zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1736 const char *log_str)
34dc7c2f 1737{
13fe0198 1738 zfs_cmd_t zc = {"\0"};
b128c09f 1739
34dc7c2f 1740 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 1741 zc.zc_cookie = force;
fb5f0bc8 1742 zc.zc_guid = hardforce;
6f1ffb06 1743 zc.zc_history = (uint64_t)(uintptr_t)log_str;
b128c09f
BB
1744
1745 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1746 switch (errno) {
1747 case EXDEV:
1748 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1749 "use '-f' to override the following errors:\n"
1750 "'%s' has an active shared spare which could be"
1751 " used by other pools once '%s' is exported."),
1752 zhp->zpool_name, zhp->zpool_name);
f00f4690
AZ
1753 return (zfs_error_fmt(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1754 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1755 zhp->zpool_name));
b128c09f
BB
1756 default:
1757 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
f00f4690
AZ
1758 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1759 zhp->zpool_name));
b128c09f
BB
1760 }
1761 }
34dc7c2f 1762
34dc7c2f
BB
1763 return (0);
1764}
1765
fb5f0bc8 1766int
6f1ffb06 1767zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
fb5f0bc8 1768{
6f1ffb06 1769 return (zpool_export_common(zhp, force, B_FALSE, log_str));
fb5f0bc8
BB
1770}
1771
1772int
6f1ffb06 1773zpool_export_force(zpool_handle_t *zhp, const char *log_str)
fb5f0bc8 1774{
6f1ffb06 1775 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
fb5f0bc8
BB
1776}
1777
428870ff
BB
1778static void
1779zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
572e2857 1780 nvlist_t *config)
428870ff 1781{
572e2857 1782 nvlist_t *nv = NULL;
428870ff
BB
1783 uint64_t rewindto;
1784 int64_t loss = -1;
1785 struct tm t;
1786 char timestr[128];
1787
572e2857
BB
1788 if (!hdl->libzfs_printerr || config == NULL)
1789 return;
1790
9ae529ec
CS
1791 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1792 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
428870ff 1793 return;
9ae529ec 1794 }
428870ff 1795
572e2857 1796 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
428870ff 1797 return;
572e2857 1798 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
428870ff
BB
1799
1800 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
b8864a23 1801 strftime(timestr, 128, "%c", &t) != 0) {
428870ff
BB
1802 if (dryrun) {
1803 (void) printf(dgettext(TEXT_DOMAIN,
1804 "Would be able to return %s "
1805 "to its state as of %s.\n"),
1806 name, timestr);
1807 } else {
1808 (void) printf(dgettext(TEXT_DOMAIN,
1809 "Pool %s returned to its state as of %s.\n"),
1810 name, timestr);
1811 }
1812 if (loss > 120) {
1813 (void) printf(dgettext(TEXT_DOMAIN,
1814 "%s approximately %lld "),
1815 dryrun ? "Would discard" : "Discarded",
b8864a23 1816 ((longlong_t)loss + 30) / 60);
428870ff
BB
1817 (void) printf(dgettext(TEXT_DOMAIN,
1818 "minutes of transactions.\n"));
1819 } else if (loss > 0) {
1820 (void) printf(dgettext(TEXT_DOMAIN,
1821 "%s approximately %lld "),
b8864a23
BB
1822 dryrun ? "Would discard" : "Discarded",
1823 (longlong_t)loss);
428870ff
BB
1824 (void) printf(dgettext(TEXT_DOMAIN,
1825 "seconds of transactions.\n"));
1826 }
1827 }
1828}
1829
1830void
1831zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1832 nvlist_t *config)
1833{
572e2857 1834 nvlist_t *nv = NULL;
428870ff
BB
1835 int64_t loss = -1;
1836 uint64_t edata = UINT64_MAX;
1837 uint64_t rewindto;
1838 struct tm t;
1839 char timestr[128];
1840
1841 if (!hdl->libzfs_printerr)
1842 return;
1843
1844 if (reason >= 0)
1845 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1846 else
1847 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1848
1849 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
572e2857 1850 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
9ae529ec 1851 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
572e2857 1852 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
428870ff
BB
1853 goto no_info;
1854
572e2857
BB
1855 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1856 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
428870ff
BB
1857 &edata);
1858
1859 (void) printf(dgettext(TEXT_DOMAIN,
1860 "Recovery is possible, but will result in some data loss.\n"));
1861
1862 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
b8864a23 1863 strftime(timestr, 128, "%c", &t) != 0) {
428870ff
BB
1864 (void) printf(dgettext(TEXT_DOMAIN,
1865 "\tReturning the pool to its state as of %s\n"
1866 "\tshould correct the problem. "),
1867 timestr);
1868 } else {
1869 (void) printf(dgettext(TEXT_DOMAIN,
1870 "\tReverting the pool to an earlier state "
1871 "should correct the problem.\n\t"));
1872 }
1873
1874 if (loss > 120) {
1875 (void) printf(dgettext(TEXT_DOMAIN,
1876 "Approximately %lld minutes of data\n"
b8864a23
BB
1877 "\tmust be discarded, irreversibly. "),
1878 ((longlong_t)loss + 30) / 60);
428870ff
BB
1879 } else if (loss > 0) {
1880 (void) printf(dgettext(TEXT_DOMAIN,
1881 "Approximately %lld seconds of data\n"
b8864a23
BB
1882 "\tmust be discarded, irreversibly. "),
1883 (longlong_t)loss);
428870ff
BB
1884 }
1885 if (edata != 0 && edata != UINT64_MAX) {
1886 if (edata == 1) {
1887 (void) printf(dgettext(TEXT_DOMAIN,
1888 "After rewind, at least\n"
1889 "\tone persistent user-data error will remain. "));
1890 } else {
1891 (void) printf(dgettext(TEXT_DOMAIN,
1892 "After rewind, several\n"
1893 "\tpersistent user-data errors will remain. "));
1894 }
1895 }
1896 (void) printf(dgettext(TEXT_DOMAIN,
1897 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1898 reason >= 0 ? "clear" : "import", name);
1899
1900 (void) printf(dgettext(TEXT_DOMAIN,
1901 "A scrub of the pool\n"
1902 "\tis strongly recommended after recovery.\n"));
1903 return;
1904
1905no_info:
1906 (void) printf(dgettext(TEXT_DOMAIN,
1907 "Destroy and re-create the pool from\n\ta backup source.\n"));
1908}
1909
34dc7c2f
BB
1910/*
1911 * zpool_import() is a contracted interface. Should be kept the same
1912 * if possible.
1913 *
1914 * Applications should use zpool_import_props() to import a pool with
1915 * new properties value to be set.
1916 */
1917int
1918zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1919 char *altroot)
1920{
1921 nvlist_t *props = NULL;
1922 int ret;
1923
1924 if (altroot != NULL) {
1925 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1926 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1927 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1928 newname));
1929 }
1930
1931 if (nvlist_add_string(props,
fb5f0bc8
BB
1932 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1933 nvlist_add_string(props,
1934 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
34dc7c2f
BB
1935 nvlist_free(props);
1936 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1937 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1938 newname));
1939 }
1940 }
1941
572e2857
BB
1942 ret = zpool_import_props(hdl, config, newname, props,
1943 ZFS_IMPORT_NORMAL);
8a5fc748 1944 nvlist_free(props);
34dc7c2f
BB
1945 return (ret);
1946}
1947
572e2857
BB
1948static void
1949print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1950 int indent)
1951{
1952 nvlist_t **child;
1953 uint_t c, children;
1954 char *vname;
1955 uint64_t is_log = 0;
1956
1957 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1958 &is_log);
1959
1960 if (name != NULL)
1961 (void) printf("\t%*s%s%s\n", indent, "", name,
1962 is_log ? " [log]" : "");
1963
1964 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1965 &child, &children) != 0)
1966 return;
1967
1968 for (c = 0; c < children; c++) {
d2f3e292 1969 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
572e2857
BB
1970 print_vdev_tree(hdl, vname, child[c], indent + 2);
1971 free(vname);
1972 }
1973}
1974
9ae529ec
CS
1975void
1976zpool_print_unsup_feat(nvlist_t *config)
1977{
1978 nvlist_t *nvinfo, *unsup_feat;
9ae529ec 1979
8bb9ecf4
RM
1980 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
1981 unsup_feat = fnvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT);
9ae529ec 1982
8bb9ecf4
RM
1983 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL);
1984 nvp != NULL; nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1985 char *desc = fnvpair_value_string(nvp);
9ae529ec
CS
1986 if (strlen(desc) > 0)
1987 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1988 else
1989 (void) printf("\t%s\n", nvpair_name(nvp));
1990 }
1991}
1992
34dc7c2f
BB
1993/*
1994 * Import the given pool using the known configuration and a list of
1995 * properties to be set. The configuration should have come from
1996 * zpool_find_import(). The 'newname' parameters control whether the pool
1997 * is imported with a different name.
1998 */
1999int
2000zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
572e2857 2001 nvlist_t *props, int flags)
34dc7c2f 2002{
13fe0198 2003 zfs_cmd_t zc = {"\0"};
8a393be3 2004 zpool_load_policy_t policy;
572e2857
BB
2005 nvlist_t *nv = NULL;
2006 nvlist_t *nvinfo = NULL;
2007 nvlist_t *missing = NULL;
0f7a0cc7 2008 const char *thename;
34dc7c2f
BB
2009 char *origname;
2010 int ret;
572e2857 2011 int error = 0;
34dc7c2f
BB
2012 char errbuf[1024];
2013
8bb9ecf4 2014 origname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
34dc7c2f
BB
2015
2016 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2017 "cannot import pool '%s'"), origname);
2018
2019 if (newname != NULL) {
2020 if (!zpool_name_valid(hdl, B_FALSE, newname))
2021 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2022 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2023 newname));
0f7a0cc7 2024 thename = newname;
34dc7c2f
BB
2025 } else {
2026 thename = origname;
2027 }
2028
0fdd8d64 2029 if (props != NULL) {
34dc7c2f 2030 uint64_t version;
572e2857 2031 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
34dc7c2f 2032
8bb9ecf4 2033 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
34dc7c2f 2034
b128c09f 2035 if ((props = zpool_valid_proplist(hdl, origname,
0fdd8d64 2036 props, version, flags, errbuf)) == NULL)
34dc7c2f 2037 return (-1);
18dbf5c8 2038 zcmd_write_src_nvlist(hdl, &zc, props);
0fdd8d64 2039 nvlist_free(props);
34dc7c2f
BB
2040 }
2041
2042 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
2043
8bb9ecf4 2044 zc.zc_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
34dc7c2f 2045
18dbf5c8
AZ
2046 zcmd_write_conf_nvlist(hdl, &zc, config);
2047 zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2);
34dc7c2f 2048
572e2857
BB
2049 zc.zc_cookie = flags;
2050 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
18dbf5c8
AZ
2051 errno == ENOMEM)
2052 zcmd_expand_dst_nvlist(hdl, &zc);
572e2857
BB
2053 if (ret != 0)
2054 error = errno;
2055
2056 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
0fdd8d64
MT
2057
2058 zcmd_free_nvlists(&zc);
2059
8a393be3 2060 zpool_get_load_policy(config, &policy);
572e2857
BB
2061
2062 if (error) {
34dc7c2f 2063 char desc[1024];
379ca9cf 2064 char aux[256];
428870ff 2065
428870ff
BB
2066 /*
2067 * Dry-run failed, but we print out what success
2068 * looks like if we found a best txg
2069 */
8a393be3 2070 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
428870ff 2071 zpool_rewind_exclaim(hdl, newname ? origname : thename,
572e2857
BB
2072 B_TRUE, nv);
2073 nvlist_free(nv);
428870ff
BB
2074 return (-1);
2075 }
2076
34dc7c2f
BB
2077 if (newname == NULL)
2078 (void) snprintf(desc, sizeof (desc),
2079 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2080 thename);
2081 else
2082 (void) snprintf(desc, sizeof (desc),
2083 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
2084 origname, thename);
2085
572e2857 2086 switch (error) {
34dc7c2f 2087 case ENOTSUP:
9ae529ec
CS
2088 if (nv != NULL && nvlist_lookup_nvlist(nv,
2089 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2090 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
2091 (void) printf(dgettext(TEXT_DOMAIN, "This "
2092 "pool uses the following feature(s) not "
2093 "supported by this system:\n"));
2094 zpool_print_unsup_feat(nv);
2095 if (nvlist_exists(nvinfo,
2096 ZPOOL_CONFIG_CAN_RDONLY)) {
2097 (void) printf(dgettext(TEXT_DOMAIN,
2098 "All unsupported features are only "
2099 "required for writing to the pool."
2100 "\nThe pool can be imported using "
2101 "'-o readonly=on'.\n"));
2102 }
2103 }
34dc7c2f
BB
2104 /*
2105 * Unsupported version.
2106 */
2107 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
2108 break;
2109
379ca9cf
OF
2110 case EREMOTEIO:
2111 if (nv != NULL && nvlist_lookup_nvlist(nv,
2112 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
2113 char *hostname = "<unknown>";
2114 uint64_t hostid = 0;
2115 mmp_state_t mmp_state;
2116
2117 mmp_state = fnvlist_lookup_uint64(nvinfo,
2118 ZPOOL_CONFIG_MMP_STATE);
2119
2120 if (nvlist_exists(nvinfo,
2121 ZPOOL_CONFIG_MMP_HOSTNAME))
2122 hostname = fnvlist_lookup_string(nvinfo,
2123 ZPOOL_CONFIG_MMP_HOSTNAME);
2124
2125 if (nvlist_exists(nvinfo,
2126 ZPOOL_CONFIG_MMP_HOSTID))
2127 hostid = fnvlist_lookup_uint64(nvinfo,
2128 ZPOOL_CONFIG_MMP_HOSTID);
2129
2130 if (mmp_state == MMP_STATE_ACTIVE) {
2131 (void) snprintf(aux, sizeof (aux),
2132 dgettext(TEXT_DOMAIN, "pool is imp"
2133 "orted on host '%s' (hostid=%lx).\n"
2134 "Export the pool on the other "
2135 "system, then run 'zpool import'."),
2136 hostname, (unsigned long) hostid);
2137 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2138 (void) snprintf(aux, sizeof (aux),
2139 dgettext(TEXT_DOMAIN, "pool has "
2140 "the multihost property on and "
2141 "the\nsystem's hostid is not set. "
2142 "Set a unique system hostid with "
b9373170 2143 "the zgenhostid(8) command.\n"));
379ca9cf
OF
2144 }
2145
f00f4690 2146 (void) zfs_error_aux(hdl, "%s", aux);
379ca9cf
OF
2147 }
2148 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2149 break;
2150
34dc7c2f
BB
2151 case EINVAL:
2152 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2153 break;
2154
428870ff
BB
2155 case EROFS:
2156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2157 "one or more devices is read only"));
2158 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2159 break;
2160
572e2857
BB
2161 case ENXIO:
2162 if (nv && nvlist_lookup_nvlist(nv,
2163 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2164 nvlist_lookup_nvlist(nvinfo,
2165 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2166 (void) printf(dgettext(TEXT_DOMAIN,
6cb8e530
PZ
2167 "The devices below are missing or "
2168 "corrupted, use '-m' to import the pool "
2169 "anyway:\n"));
572e2857
BB
2170 print_vdev_tree(hdl, NULL, missing, 2);
2171 (void) printf("\n");
2172 }
2173 (void) zpool_standard_error(hdl, error, desc);
2174 break;
2175
2176 case EEXIST:
2177 (void) zpool_standard_error(hdl, error, desc);
2178 break;
2179
abe5b8fb
BB
2180 case EBUSY:
2181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2182 "one or more devices are already in use\n"));
2183 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2184 break;
d1d19c78
PD
2185 case ENAMETOOLONG:
2186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2187 "new name of at least one dataset is longer than "
2188 "the maximum allowable length"));
2189 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2190 break;
34dc7c2f 2191 default:
572e2857 2192 (void) zpool_standard_error(hdl, error, desc);
428870ff 2193 zpool_explain_recover(hdl,
572e2857 2194 newname ? origname : thename, -error, nv);
428870ff 2195 break;
34dc7c2f
BB
2196 }
2197
572e2857 2198 nvlist_free(nv);
34dc7c2f
BB
2199 ret = -1;
2200 } else {
2201 zpool_handle_t *zhp;
2202
2203 /*
2204 * This should never fail, but play it safe anyway.
2205 */
428870ff 2206 if (zpool_open_silent(hdl, thename, &zhp) != 0)
34dc7c2f 2207 ret = -1;
428870ff 2208 else if (zhp != NULL)
34dc7c2f 2209 zpool_close(zhp);
8a393be3 2210 if (policy.zlp_rewind &
428870ff
BB
2211 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2212 zpool_rewind_exclaim(hdl, newname ? origname : thename,
8a393be3 2213 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
34dc7c2f 2214 }
572e2857 2215 nvlist_free(nv);
428870ff 2216 return (0);
34dc7c2f
BB
2217 }
2218
34dc7c2f
BB
2219 return (ret);
2220}
2221
1b939560
BB
2222/*
2223 * Translate vdev names to guids. If a vdev_path is determined to be
2224 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2225 * are added to it.
2226 */
2227static int
2228zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2229 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2230{
2231 nvlist_t *errlist = NULL;
2232 int error = 0;
2233
2234 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2235 elem = nvlist_next_nvpair(vds, elem)) {
2236 boolean_t spare, cache;
2237
2238 char *vd_path = nvpair_name(elem);
2239 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2240 NULL);
2241
2242 if ((tgt == NULL) || cache || spare) {
2243 if (errlist == NULL) {
2244 errlist = fnvlist_alloc();
2245 error = EINVAL;
2246 }
2247
2248 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2249 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2250 fnvlist_add_int64(errlist, vd_path, err);
2251 continue;
2252 }
2253
2254 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2255 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2256
2257 char msg[MAXNAMELEN];
2258 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2259 fnvlist_add_string(guids_to_paths, msg, vd_path);
2260 }
2261
2262 if (error != 0) {
2263 verify(errlist != NULL);
2264 if (vd_errlist != NULL)
2265 *vd_errlist = errlist;
2266 else
2267 fnvlist_free(errlist);
2268 }
2269
2270 return (error);
2271}
2272
619f0976
GW
2273static int
2274xlate_init_err(int err)
2275{
2276 switch (err) {
2277 case ENODEV:
2278 return (EZFS_NODEVICE);
2279 case EINVAL:
2280 case EROFS:
2281 return (EZFS_BADDEV);
2282 case EBUSY:
2283 return (EZFS_INITIALIZING);
2284 case ESRCH:
2285 return (EZFS_NO_INITIALIZE);
2286 }
2287 return (err);
2288}
2289
2290/*
2291 * Begin, suspend, or cancel the initialization (initializing of all free
2292 * blocks) for the given vdevs in the given pool.
2293 */
65c7cc49 2294static int
e60e158e
JG
2295zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2296 nvlist_t *vds, boolean_t wait)
619f0976 2297{
1b939560 2298 int err;
619f0976 2299
619f0976
GW
2300 nvlist_t *vdev_guids = fnvlist_alloc();
2301 nvlist_t *guids_to_paths = fnvlist_alloc();
1b939560
BB
2302 nvlist_t *vd_errlist = NULL;
2303 nvlist_t *errlist;
619f0976
GW
2304 nvpair_t *elem;
2305
1b939560
BB
2306 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2307 guids_to_paths, &vd_errlist);
619f0976 2308
e60e158e
JG
2309 if (err != 0) {
2310 verify(vd_errlist != NULL);
2311 goto list_errors;
2312 }
2313
2314 err = lzc_initialize(zhp->zpool_name, cmd_type,
2315 vdev_guids, &errlist);
619f0976 2316
e60e158e 2317 if (err != 0) {
1b939560
BB
2318 if (errlist != NULL) {
2319 vd_errlist = fnvlist_lookup_nvlist(errlist,
2320 ZPOOL_INITIALIZE_VDEVS);
e60e158e 2321 goto list_errors;
1b939560 2322 }
e60e158e 2323 (void) zpool_standard_error(zhp->zpool_hdl, err,
1b939560 2324 dgettext(TEXT_DOMAIN, "operation failed"));
e60e158e 2325 goto out;
1b939560
BB
2326 }
2327
e60e158e
JG
2328 if (wait) {
2329 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2330 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2331
2332 uint64_t guid = fnvpair_value_uint64(elem);
2333
2334 err = lzc_wait_tag(zhp->zpool_name,
2335 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2336 if (err != 0) {
2337 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2338 err, dgettext(TEXT_DOMAIN, "error "
2339 "waiting for '%s' to initialize"),
2340 nvpair_name(elem));
2341
2342 goto out;
2343 }
2344 }
2345 }
2346 goto out;
2347
2348list_errors:
1b939560
BB
2349 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2350 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2351 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2352 char *path;
2353
2354 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2355 &path) != 0)
2356 path = nvpair_name(elem);
2357
2358 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2359 "cannot initialize '%s'", path);
619f0976
GW
2360 }
2361
e60e158e 2362out:
619f0976 2363 fnvlist_free(vdev_guids);
1b939560 2364 fnvlist_free(guids_to_paths);
619f0976 2365
e60e158e 2366 if (vd_errlist != NULL)
1b939560 2367 fnvlist_free(vd_errlist);
619f0976 2368
e60e158e
JG
2369 return (err == 0 ? 0 : -1);
2370}
2371
2372int
2373zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2374 nvlist_t *vds)
2375{
2376 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2377}
2378
2379int
2380zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2381 nvlist_t *vds)
2382{
2383 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
1b939560
BB
2384}
2385
2386static int
2387xlate_trim_err(int err)
2388{
2389 switch (err) {
2390 case ENODEV:
2391 return (EZFS_NODEVICE);
2392 case EINVAL:
2393 case EROFS:
2394 return (EZFS_BADDEV);
2395 case EBUSY:
2396 return (EZFS_TRIMMING);
2397 case ESRCH:
2398 return (EZFS_NO_TRIM);
2399 case EOPNOTSUPP:
2400 return (EZFS_TRIM_NOTSUP);
2401 }
2402 return (err);
2403}
2404
2288d419
BB
2405static int
2406zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids)
2407{
2408 int err;
2409 nvpair_t *elem;
2410
2411 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2412 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2413
2414 uint64_t guid = fnvpair_value_uint64(elem);
2415
2416 err = lzc_wait_tag(zhp->zpool_name,
2417 ZPOOL_WAIT_TRIM, guid, NULL);
2418 if (err != 0) {
2419 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2420 err, dgettext(TEXT_DOMAIN, "error "
2421 "waiting to trim '%s'"), nvpair_name(elem));
2422
2423 return (err);
2424 }
2425 }
2426 return (0);
2427}
2428
1b939560 2429/*
50ff6327
JG
2430 * Check errlist and report any errors, omitting ones which should be
2431 * suppressed. Returns B_TRUE if any errors were reported.
1b939560 2432 */
50ff6327
JG
2433static boolean_t
2434check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags,
2435 nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist)
1b939560 2436{
1b939560 2437 nvpair_t *elem;
50ff6327
JG
2438 boolean_t reported_errs = B_FALSE;
2439 int num_vds = 0;
2440 int num_suppressed_errs = 0;
1b939560 2441
50ff6327
JG
2442 for (elem = nvlist_next_nvpair(vds, NULL);
2443 elem != NULL; elem = nvlist_next_nvpair(vds, elem)) {
2444 num_vds++;
619f0976
GW
2445 }
2446
50ff6327
JG
2447 for (elem = nvlist_next_nvpair(errlist, NULL);
2448 elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) {
1b939560
BB
2449 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2450 char *path;
619f0976 2451
1b939560
BB
2452 /*
2453 * If only the pool was specified, and it was not a secure
2454 * trim then suppress warnings for individual vdevs which
2455 * do not support trimming.
2456 */
2457 if (vd_error == EZFS_TRIM_NOTSUP &&
2458 trim_flags->fullpool &&
2459 !trim_flags->secure) {
50ff6327 2460 num_suppressed_errs++;
1b939560
BB
2461 continue;
2462 }
2463
50ff6327 2464 reported_errs = B_TRUE;
1b939560
BB
2465 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2466 &path) != 0)
2467 path = nvpair_name(elem);
2468
2469 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2470 "cannot trim '%s'", path);
619f0976
GW
2471 }
2472
50ff6327
JG
2473 if (num_suppressed_errs == num_vds) {
2474 (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2475 "no devices in pool support trim operations"));
2476 (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP,
2477 dgettext(TEXT_DOMAIN, "cannot trim")));
2478 reported_errs = B_TRUE;
2479 }
1b939560 2480
50ff6327
JG
2481 return (reported_errs);
2482}
2483
2484/*
2485 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2486 * the given vdevs in the given pool.
2487 */
2488int
2489zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2490 trimflags_t *trim_flags)
2491{
2492 int err;
2493 int retval = 0;
2494
2495 nvlist_t *vdev_guids = fnvlist_alloc();
2496 nvlist_t *guids_to_paths = fnvlist_alloc();
2497 nvlist_t *errlist = NULL;
2498
2499 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2500 guids_to_paths, &errlist);
2501 if (err != 0) {
2502 check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist);
2503 retval = -1;
2504 goto out;
1b939560 2505 }
619f0976 2506
50ff6327
JG
2507 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2508 trim_flags->secure, vdev_guids, &errlist);
2509 if (err != 0) {
2510 nvlist_t *vd_errlist;
2511 if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2512 ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) {
2513 if (check_trim_errs(zhp, trim_flags, guids_to_paths,
2514 vds, vd_errlist)) {
2515 retval = -1;
2516 goto out;
2517 }
2518 } else {
2519 char msg[1024];
2520
2521 (void) snprintf(msg, sizeof (msg),
2522 dgettext(TEXT_DOMAIN, "operation failed"));
2523 zpool_standard_error(zhp->zpool_hdl, err, msg);
2524 retval = -1;
2525 goto out;
2526 }
2527 }
2528
2529
2530 if (trim_flags->wait)
2531 retval = zpool_trim_wait(zhp, vdev_guids);
2532
2533out:
2534 if (errlist != NULL)
2535 fnvlist_free(errlist);
2536 fnvlist_free(vdev_guids);
2537 fnvlist_free(guids_to_paths);
2538 return (retval);
619f0976
GW
2539}
2540
34dc7c2f 2541/*
428870ff 2542 * Scan the pool.
34dc7c2f
BB
2543 */
2544int
0ea05c64 2545zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
34dc7c2f 2546{
13fe0198 2547 zfs_cmd_t zc = {"\0"};
34dc7c2f 2548 char msg[1024];
0ea05c64 2549 int err;
34dc7c2f
BB
2550 libzfs_handle_t *hdl = zhp->zpool_hdl;
2551
2552 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
428870ff 2553 zc.zc_cookie = func;
0ea05c64 2554 zc.zc_flags = cmd;
34dc7c2f 2555
0ea05c64
AP
2556 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2557 return (0);
2558
2559 err = errno;
2560
2561 /* ECANCELED on a scrub means we resumed a paused scrub */
2562 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2563 cmd == POOL_SCRUB_NORMAL)
2564 return (0);
2565
2566 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
34dc7c2f
BB
2567 return (0);
2568
428870ff 2569 if (func == POOL_SCAN_SCRUB) {
0ea05c64
AP
2570 if (cmd == POOL_SCRUB_PAUSE) {
2571 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2572 "cannot pause scrubbing %s"), zc.zc_name);
2573 } else {
2574 assert(cmd == POOL_SCRUB_NORMAL);
2575 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2576 "cannot scrub %s"), zc.zc_name);
2577 }
fa241660
TC
2578 } else if (func == POOL_SCAN_RESILVER) {
2579 assert(cmd == POOL_SCRUB_NORMAL);
2580 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2581 "cannot restart resilver on %s"), zc.zc_name);
428870ff
BB
2582 } else if (func == POOL_SCAN_NONE) {
2583 (void) snprintf(msg, sizeof (msg),
2584 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2585 zc.zc_name);
2586 } else {
2587 assert(!"unexpected result");
2588 }
34dc7c2f 2589
0ea05c64 2590 if (err == EBUSY) {
428870ff
BB
2591 nvlist_t *nvroot;
2592 pool_scan_stat_t *ps = NULL;
2593 uint_t psc;
2594
8bb9ecf4
RM
2595 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2596 ZPOOL_CONFIG_VDEV_TREE);
428870ff
BB
2597 (void) nvlist_lookup_uint64_array(nvroot,
2598 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
9a49d3f3
BB
2599 if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2600 ps->pss_state == DSS_SCANNING) {
0ea05c64
AP
2601 if (cmd == POOL_SCRUB_PAUSE)
2602 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2603 else
2604 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2605 } else {
428870ff 2606 return (zfs_error(hdl, EZFS_RESILVERING, msg));
0ea05c64
AP
2607 }
2608 } else if (err == ENOENT) {
428870ff 2609 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
fa241660
TC
2610 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2611 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
428870ff 2612 } else {
0ea05c64 2613 return (zpool_standard_error(hdl, err, msg));
428870ff
BB
2614 }
2615}
2616
34dc7c2f 2617/*
9babb374
BB
2618 * Find a vdev that matches the search criteria specified. We use the
2619 * the nvpair name to determine how we should look for the device.
34dc7c2f
BB
2620 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2621 * spare; but FALSE if its an INUSE spare.
2622 */
2623static nvlist_t *
9babb374
BB
2624vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2625 boolean_t *l2cache, boolean_t *log)
34dc7c2f
BB
2626{
2627 uint_t c, children;
2628 nvlist_t **child;
34dc7c2f 2629 nvlist_t *ret;
b128c09f 2630 uint64_t is_log;
9babb374
BB
2631 char *srchkey;
2632 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2633
2634 /* Nothing to look for */
2635 if (search == NULL || pair == NULL)
2636 return (NULL);
2637
2638 /* Obtain the key we will use to search */
2639 srchkey = nvpair_name(pair);
2640
2641 switch (nvpair_type(pair)) {
572e2857 2642 case DATA_TYPE_UINT64:
9babb374 2643 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
8bb9ecf4
RM
2644 uint64_t srchval = fnvpair_value_uint64(pair);
2645 uint64_t theguid = fnvlist_lookup_uint64(nv,
2646 ZPOOL_CONFIG_GUID);
572e2857
BB
2647 if (theguid == srchval)
2648 return (nv);
9babb374
BB
2649 }
2650 break;
9babb374
BB
2651
2652 case DATA_TYPE_STRING: {
2653 char *srchval, *val;
2654
8bb9ecf4 2655 srchval = fnvpair_value_string(pair);
9babb374
BB
2656 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2657 break;
34dc7c2f 2658
9babb374 2659 /*
428870ff
BB
2660 * Search for the requested value. Special cases:
2661 *
eac47204
BB
2662 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2663 * "-part1", or "p1". The suffix is hidden from the user,
2664 * but included in the string, so this matches around it.
2665 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2666 * is used to check all possible expanded paths.
428870ff
BB
2667 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2668 *
2669 * Otherwise, all other searches are simple string compares.
9babb374 2670 */
a2c6816c 2671 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
9babb374
BB
2672 uint64_t wholedisk = 0;
2673
2674 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2675 &wholedisk);
eac47204
BB
2676 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2677 return (nv);
428870ff 2678
428870ff
BB
2679 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2680 char *type, *idx, *end, *p;
2681 uint64_t id, vdev_id;
2682
2683 /*
2684 * Determine our vdev type, keeping in mind
2685 * that the srchval is composed of a type and
2686 * vdev id pair (i.e. mirror-4).
2687 */
2688 if ((type = strdup(srchval)) == NULL)
2689 return (NULL);
2690
2691 if ((p = strrchr(type, '-')) == NULL) {
2692 free(type);
2693 break;
2694 }
2695 idx = p + 1;
2696 *p = '\0';
2697
2698 /*
2699 * If the types don't match then keep looking.
2700 */
2701 if (strncmp(val, type, strlen(val)) != 0) {
2702 free(type);
2703 break;
2704 }
2705
1574c73b 2706 verify(zpool_vdev_is_interior(type));
428870ff 2707
8bb9ecf4 2708 id = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID);
428870ff
BB
2709 errno = 0;
2710 vdev_id = strtoull(idx, &end, 10);
2711
b85f47ef
MA
2712 /*
2713 * If we are looking for a raidz and a parity is
2714 * specified, make sure it matches.
2715 */
2716 int rzlen = strlen(VDEV_TYPE_RAIDZ);
2717 assert(rzlen == strlen(VDEV_TYPE_DRAID));
2718 int typlen = strlen(type);
2719 if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 ||
2720 strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) &&
2721 typlen != rzlen) {
2722 uint64_t vdev_parity;
2723 int parity = *(type + rzlen) - '0';
2724
2725 if (parity <= 0 || parity > 3 ||
2726 (typlen - rzlen) != 1) {
2727 /*
2728 * Nonsense parity specified, can
2729 * never match
2730 */
2731 free(type);
2732 return (NULL);
2733 }
8bb9ecf4
RM
2734 vdev_parity = fnvlist_lookup_uint64(nv,
2735 ZPOOL_CONFIG_NPARITY);
b85f47ef
MA
2736 if ((int)vdev_parity != parity) {
2737 free(type);
2738 break;
2739 }
2740 }
2741
428870ff
BB
2742 free(type);
2743 if (errno != 0)
2744 return (NULL);
2745
2746 /*
2747 * Now verify that we have the correct vdev id.
2748 */
2749 if (vdev_id == id)
2750 return (nv);
9babb374 2751 }
34dc7c2f 2752
34dc7c2f 2753 /*
9babb374 2754 * Common case
34dc7c2f 2755 */
9babb374 2756 if (strcmp(srchval, val) == 0)
34dc7c2f 2757 return (nv);
9babb374
BB
2758 break;
2759 }
2760
2761 default:
2762 break;
34dc7c2f
BB
2763 }
2764
2765 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2766 &child, &children) != 0)
2767 return (NULL);
2768
b128c09f 2769 for (c = 0; c < children; c++) {
9babb374 2770 if ((ret = vdev_to_nvlist_iter(child[c], search,
b128c09f
BB
2771 avail_spare, l2cache, NULL)) != NULL) {
2772 /*
2773 * The 'is_log' value is only set for the toplevel
2774 * vdev, not the leaf vdevs. So we always lookup the
2775 * log device from the root of the vdev tree (where
2776 * 'log' is non-NULL).
2777 */
2778 if (log != NULL &&
2779 nvlist_lookup_uint64(child[c],
2780 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2781 is_log) {
2782 *log = B_TRUE;
2783 }
34dc7c2f 2784 return (ret);
b128c09f
BB
2785 }
2786 }
34dc7c2f
BB
2787
2788 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2789 &child, &children) == 0) {
2790 for (c = 0; c < children; c++) {
9babb374 2791 if ((ret = vdev_to_nvlist_iter(child[c], search,
b128c09f 2792 avail_spare, l2cache, NULL)) != NULL) {
34dc7c2f
BB
2793 *avail_spare = B_TRUE;
2794 return (ret);
2795 }
2796 }
2797 }
2798
2799 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2800 &child, &children) == 0) {
2801 for (c = 0; c < children; c++) {
9babb374 2802 if ((ret = vdev_to_nvlist_iter(child[c], search,
b128c09f 2803 avail_spare, l2cache, NULL)) != NULL) {
34dc7c2f
BB
2804 *l2cache = B_TRUE;
2805 return (ret);
2806 }
2807 }
2808 }
2809
2810 return (NULL);
2811}
2812
9babb374 2813/*
d441e85d 2814 * Given a physical path or guid, find the associated vdev.
9babb374
BB
2815 */
2816nvlist_t *
2817zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2818 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2819{
2820 nvlist_t *search, *nvroot, *ret;
d441e85d
BB
2821 uint64_t guid;
2822 char *end;
9babb374 2823
8bb9ecf4 2824 search = fnvlist_alloc();
d441e85d
BB
2825
2826 guid = strtoull(ppath, &end, 0);
2827 if (guid != 0 && *end == '\0') {
8bb9ecf4 2828 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
d441e85d 2829 } else {
8bb9ecf4 2830 fnvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath);
d441e85d 2831 }
9babb374 2832
8bb9ecf4
RM
2833 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2834 ZPOOL_CONFIG_VDEV_TREE);
9babb374
BB
2835
2836 *avail_spare = B_FALSE;
572e2857
BB
2837 *l2cache = B_FALSE;
2838 if (log != NULL)
2839 *log = B_FALSE;
9babb374 2840 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
8bb9ecf4 2841 fnvlist_free(search);
9babb374
BB
2842
2843 return (ret);
2844}
2845
428870ff
BB
2846/*
2847 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2848 */
1574c73b 2849static boolean_t
428870ff
BB
2850zpool_vdev_is_interior(const char *name)
2851{
2852 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1574c73b
BB
2853 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2854 strncmp(name,
2855 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
428870ff
BB
2856 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2857 return (B_TRUE);
b2255edc
BB
2858
2859 if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 &&
2860 !zpool_is_draid_spare(name))
2861 return (B_TRUE);
2862
428870ff
BB
2863 return (B_FALSE);
2864}
2865
34dc7c2f
BB
2866nvlist_t *
2867zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
b128c09f 2868 boolean_t *l2cache, boolean_t *log)
34dc7c2f 2869{
34dc7c2f 2870 char *end;
9babb374 2871 nvlist_t *nvroot, *search, *ret;
34dc7c2f
BB
2872 uint64_t guid;
2873
8bb9ecf4 2874 search = fnvlist_alloc();
9babb374 2875
1a5c611a 2876 guid = strtoull(path, &end, 0);
34dc7c2f 2877 if (guid != 0 && *end == '\0') {
8bb9ecf4 2878 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
428870ff 2879 } else if (zpool_vdev_is_interior(path)) {
8bb9ecf4 2880 fnvlist_add_string(search, ZPOOL_CONFIG_TYPE, path);
34dc7c2f 2881 } else {
8bb9ecf4 2882 fnvlist_add_string(search, ZPOOL_CONFIG_PATH, path);
34dc7c2f
BB
2883 }
2884
8bb9ecf4
RM
2885 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2886 ZPOOL_CONFIG_VDEV_TREE);
34dc7c2f
BB
2887
2888 *avail_spare = B_FALSE;
2889 *l2cache = B_FALSE;
b128c09f
BB
2890 if (log != NULL)
2891 *log = B_FALSE;
9babb374 2892 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
8bb9ecf4 2893 fnvlist_free(search);
9babb374
BB
2894
2895 return (ret);
b128c09f
BB
2896}
2897
2898static int
379ca9cf 2899vdev_is_online(nvlist_t *nv)
b128c09f
BB
2900{
2901 uint64_t ival;
2902
2903 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2904 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2905 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2906 return (0);
2907
2908 return (1);
2909}
2910
2911/*
9babb374 2912 * Helper function for zpool_get_physpaths().
b128c09f 2913 */
9babb374
BB
2914static int
2915vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2916 size_t *bytes_written)
2917{
2918 size_t bytes_left, pos, rsz;
2919 char *tmppath;
2920 const char *format;
2921
2922 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2923 &tmppath) != 0)
2924 return (EZFS_NODEVICE);
2925
2926 pos = *bytes_written;
2927 bytes_left = physpath_size - pos;
2928 format = (pos == 0) ? "%s" : " %s";
2929
2930 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2931 *bytes_written += rsz;
2932
2933 if (rsz >= bytes_left) {
2934 /* if physpath was not copied properly, clear it */
2935 if (bytes_left != 0) {
2936 physpath[pos] = 0;
2937 }
2938 return (EZFS_NOSPC);
2939 }
2940 return (0);
2941}
2942
2943static int
2944vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2945 size_t *rsz, boolean_t is_spare)
2946{
2947 char *type;
2948 int ret;
2949
2950 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2951 return (EZFS_INVALCONFIG);
2952
2953 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2954 /*
2955 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2956 * For a spare vdev, we only want to boot from the active
2957 * spare device.
2958 */
2959 if (is_spare) {
2960 uint64_t spare = 0;
2961 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2962 &spare);
2963 if (!spare)
2964 return (EZFS_INVALCONFIG);
2965 }
2966
379ca9cf 2967 if (vdev_is_online(nv)) {
9babb374
BB
2968 if ((ret = vdev_get_one_physpath(nv, physpath,
2969 phypath_size, rsz)) != 0)
2970 return (ret);
2971 }
2972 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
0a3d2673 2973 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
9babb374
BB
2974 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2975 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2976 nvlist_t **child;
2977 uint_t count;
2978 int i, ret;
2979
2980 if (nvlist_lookup_nvlist_array(nv,
2981 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2982 return (EZFS_INVALCONFIG);
2983
2984 for (i = 0; i < count; i++) {
2985 ret = vdev_get_physpaths(child[i], physpath,
2986 phypath_size, rsz, is_spare);
2987 if (ret == EZFS_NOSPC)
2988 return (ret);
2989 }
2990 }
2991
2992 return (EZFS_POOL_INVALARG);
2993}
2994
2995/*
2996 * Get phys_path for a root pool config.
2997 * Return 0 on success; non-zero on failure.
2998 */
2999static int
3000zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
b128c09f 3001{
9babb374 3002 size_t rsz;
b128c09f
BB
3003 nvlist_t *vdev_root;
3004 nvlist_t **child;
3005 uint_t count;
9babb374 3006 char *type;
b128c09f 3007
9babb374 3008 rsz = 0;
b128c09f 3009
9babb374
BB
3010 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3011 &vdev_root) != 0)
3012 return (EZFS_INVALCONFIG);
b128c09f 3013
9babb374
BB
3014 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
3015 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
b128c09f 3016 &child, &count) != 0)
9babb374 3017 return (EZFS_INVALCONFIG);
b128c09f 3018
9babb374 3019 /*
986dd8aa 3020 * root pool can only have a single top-level vdev.
9babb374 3021 */
986dd8aa 3022 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
9babb374 3023 return (EZFS_POOL_INVALARG);
b128c09f 3024
9babb374
BB
3025 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
3026 B_FALSE);
3027
3028 /* No online devices */
3029 if (rsz == 0)
3030 return (EZFS_NODEVICE);
b128c09f
BB
3031
3032 return (0);
34dc7c2f
BB
3033}
3034
9babb374
BB
3035/*
3036 * Get phys_path for a root pool
3037 * Return 0 on success; non-zero on failure.
3038 */
3039int
3040zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
3041{
3042 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
3043 phypath_size));
3044}
3045
4a283c7f
TH
3046/*
3047 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3048 *
3049 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3050 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3051 * ignore them.
3052 */
3053static uint64_t
3054zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
3055 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
3056{
4a283c7f
TH
3057 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
3058 nvlist_t *tgt;
3059
3060 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
3061 &log)) == NULL)
3062 return (0);
3063
4a283c7f
TH
3064 if (is_spare != NULL)
3065 *is_spare = spare;
3066 if (is_l2cache != NULL)
3067 *is_l2cache = l2cache;
3068 if (is_log != NULL)
3069 *is_log = log;
3070
8bb9ecf4 3071 return (fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID));
4a283c7f
TH
3072}
3073
3074/* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3075uint64_t
3076zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
3077{
3078 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
3079}
3080
34dc7c2f
BB
3081/*
3082 * Bring the specified vdev online. The 'flags' parameter is a set of the
3083 * ZFS_ONLINE_* flags.
3084 */
3085int
3086zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
3087 vdev_state_t *newstate)
3088{
13fe0198 3089 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3090 char msg[1024];
3091 nvlist_t *tgt;
9babb374 3092 boolean_t avail_spare, l2cache, islog;
34dc7c2f
BB
3093 libzfs_handle_t *hdl = zhp->zpool_hdl;
3094
9babb374
BB
3095 if (flags & ZFS_ONLINE_EXPAND) {
3096 (void) snprintf(msg, sizeof (msg),
3097 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
3098 } else {
3099 (void) snprintf(msg, sizeof (msg),
3100 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
3101 }
34dc7c2f
BB
3102
3103 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3104 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
9babb374 3105 &islog)) == NULL)
34dc7c2f
BB
3106 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3107
8bb9ecf4 3108 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f 3109
428870ff 3110 if (avail_spare)
34dc7c2f
BB
3111 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3112
76bcffb7
RM
3113#ifndef __FreeBSD__
3114 char *pathname;
8198c57b
YP
3115 if ((flags & ZFS_ONLINE_EXPAND ||
3116 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
3117 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
9babb374
BB
3118 uint64_t wholedisk = 0;
3119
3120 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
3121 &wholedisk);
9babb374
BB
3122
3123 /*
3124 * XXX - L2ARC 1.0 devices can't support expansion.
3125 */
3126 if (l2cache) {
3127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3128 "cannot expand cache devices"));
3129 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
3130 }
3131
3132 if (wholedisk) {
7608bd0d
ED
3133 const char *fullpath = path;
3134 char buf[MAXPATHLEN];
76bcffb7 3135 int error;
7608bd0d
ED
3136
3137 if (path[0] != '/') {
3138 error = zfs_resolve_shortname(path, buf,
d1d7e268 3139 sizeof (buf));
7608bd0d
ED
3140 if (error != 0)
3141 return (zfs_error(hdl, EZFS_NODEVICE,
3142 msg));
3143
3144 fullpath = buf;
3145 }
3146
3147 error = zpool_relabel_disk(hdl, fullpath, msg);
8adf4864
ED
3148 if (error != 0)
3149 return (error);
9babb374
BB
3150 }
3151 }
76bcffb7 3152#endif
9babb374 3153
34dc7c2f
BB
3154 zc.zc_cookie = VDEV_STATE_ONLINE;
3155 zc.zc_obj = flags;
3156
572e2857 3157 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
428870ff
BB
3158 if (errno == EINVAL) {
3159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
3160 "from this pool into a new one. Use '%s' "
3161 "instead"), "zpool detach");
3162 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
3163 }
34dc7c2f 3164 return (zpool_standard_error(hdl, errno, msg));
428870ff 3165 }
34dc7c2f
BB
3166
3167 *newstate = zc.zc_cookie;
3168 return (0);
3169}
3170
3171/*
3172 * Take the specified vdev offline
3173 */
3174int
3175zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
3176{
13fe0198 3177 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3178 char msg[1024];
3179 nvlist_t *tgt;
3180 boolean_t avail_spare, l2cache;
3181 libzfs_handle_t *hdl = zhp->zpool_hdl;
3182
3183 (void) snprintf(msg, sizeof (msg),
3184 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
3185
3186 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f
BB
3187 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3188 NULL)) == NULL)
34dc7c2f
BB
3189 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3190
8bb9ecf4 3191 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f 3192
428870ff 3193 if (avail_spare)
34dc7c2f
BB
3194 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3195
34dc7c2f
BB
3196 zc.zc_cookie = VDEV_STATE_OFFLINE;
3197 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3198
572e2857 3199 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
34dc7c2f
BB
3200 return (0);
3201
3202 switch (errno) {
3203 case EBUSY:
3204
3205 /*
3206 * There are no other replicas of this device.
3207 */
3208 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3209
9babb374
BB
3210 case EEXIST:
3211 /*
3212 * The log device has unplayed logs
3213 */
3214 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
3215
34dc7c2f
BB
3216 default:
3217 return (zpool_standard_error(hdl, errno, msg));
3218 }
3219}
3220
3221/*
3222 * Mark the given vdev faulted.
3223 */
3224int
428870ff 3225zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
34dc7c2f 3226{
13fe0198 3227 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3228 char msg[1024];
3229 libzfs_handle_t *hdl = zhp->zpool_hdl;
3230
3231 (void) snprintf(msg, sizeof (msg),
d1d7e268 3232 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
34dc7c2f
BB
3233
3234 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3235 zc.zc_guid = guid;
3236 zc.zc_cookie = VDEV_STATE_FAULTED;
428870ff 3237 zc.zc_obj = aux;
34dc7c2f 3238
b834b58a 3239 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
34dc7c2f
BB
3240 return (0);
3241
3242 switch (errno) {
3243 case EBUSY:
3244
3245 /*
3246 * There are no other replicas of this device.
3247 */
3248 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3249
3250 default:
3251 return (zpool_standard_error(hdl, errno, msg));
3252 }
3253
3254}
3255
3256/*
3257 * Mark the given vdev degraded.
3258 */
3259int
428870ff 3260zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
34dc7c2f 3261{
13fe0198 3262 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3263 char msg[1024];
3264 libzfs_handle_t *hdl = zhp->zpool_hdl;
3265
3266 (void) snprintf(msg, sizeof (msg),
d1d7e268 3267 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
34dc7c2f
BB
3268
3269 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3270 zc.zc_guid = guid;
3271 zc.zc_cookie = VDEV_STATE_DEGRADED;
428870ff 3272 zc.zc_obj = aux;
34dc7c2f 3273
b834b58a 3274 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
34dc7c2f
BB
3275 return (0);
3276
3277 return (zpool_standard_error(hdl, errno, msg));
3278}
3279
3280/*
3281 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3282 * a hot spare.
3283 */
3284static boolean_t
3285is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3286{
3287 nvlist_t **child;
3288 uint_t c, children;
34dc7c2f
BB
3289
3290 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3291 &children) == 0) {
8bb9ecf4 3292 char *type = fnvlist_lookup_string(search, ZPOOL_CONFIG_TYPE);
b2255edc
BB
3293 if ((strcmp(type, VDEV_TYPE_SPARE) == 0 ||
3294 strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) &&
34dc7c2f
BB
3295 children == 2 && child[which] == tgt)
3296 return (B_TRUE);
3297
3298 for (c = 0; c < children; c++)
3299 if (is_replacing_spare(child[c], tgt, which))
3300 return (B_TRUE);
3301 }
3302
3303 return (B_FALSE);
3304}
3305
3306/*
3307 * Attach new_disk (fully described by nvroot) to old_disk.
3308 * If 'replacing' is specified, the new disk will replace the old one.
3309 */
3310int
9a49d3f3
BB
3311zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3312 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
34dc7c2f 3313{
13fe0198 3314 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3315 char msg[1024];
3316 int ret;
3317 nvlist_t *tgt;
b128c09f
BB
3318 boolean_t avail_spare, l2cache, islog;
3319 uint64_t val;
572e2857 3320 char *newname;
34dc7c2f
BB
3321 nvlist_t **child;
3322 uint_t children;
3323 nvlist_t *config_root;
3324 libzfs_handle_t *hdl = zhp->zpool_hdl;
3325
3326 if (replacing)
3327 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3328 "cannot replace %s with %s"), old_disk, new_disk);
3329 else
3330 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3331 "cannot attach %s to %s"), new_disk, old_disk);
3332
3333 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3334 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
a1d477c2 3335 &islog)) == NULL)
34dc7c2f
BB
3336 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3337
3338 if (avail_spare)
3339 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3340
3341 if (l2cache)
3342 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3343
8bb9ecf4 3344 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f 3345 zc.zc_cookie = replacing;
9a49d3f3
BB
3346 zc.zc_simple = rebuild;
3347
3348 if (rebuild &&
3349 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3350 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3351 "the loaded zfs module doesn't support device rebuilds"));
3352 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
3353 }
34dc7c2f
BB
3354
3355 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3356 &child, &children) != 0 || children != 1) {
3357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3358 "new device must be a single disk"));
3359 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3360 }
3361
8bb9ecf4
RM
3362 config_root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3363 ZPOOL_CONFIG_VDEV_TREE);
34dc7c2f 3364
d2f3e292 3365 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
b128c09f
BB
3366 return (-1);
3367
34dc7c2f
BB
3368 /*
3369 * If the target is a hot spare that has been swapped in, we can only
3370 * replace it with another hot spare.
3371 */
3372 if (replacing &&
3373 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
b128c09f
BB
3374 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3375 NULL) == NULL || !avail_spare) &&
3376 is_replacing_spare(config_root, tgt, 1)) {
34dc7c2f
BB
3377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3378 "can only be replaced by another hot spare"));
b128c09f 3379 free(newname);
34dc7c2f
BB
3380 return (zfs_error(hdl, EZFS_BADTARGET, msg));
3381 }
3382
b128c09f
BB
3383 free(newname);
3384
18dbf5c8 3385 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
34dc7c2f 3386
572e2857 3387 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
34dc7c2f
BB
3388
3389 zcmd_free_nvlists(&zc);
3390
eaa25f1a 3391 if (ret == 0)
34dc7c2f
BB
3392 return (0);
3393
3394 switch (errno) {
3395 case ENOTSUP:
3396 /*
3397 * Can't attach to or replace this type of vdev.
3398 */
3399 if (replacing) {
572e2857
BB
3400 uint64_t version = zpool_get_prop_int(zhp,
3401 ZPOOL_PROP_VERSION, NULL);
3402
9a49d3f3 3403 if (islog) {
34dc7c2f
BB
3404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3405 "cannot replace a log with a spare"));
9a49d3f3
BB
3406 } else if (rebuild) {
3407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b2255edc
BB
3408 "only mirror and dRAID vdevs support "
3409 "sequential reconstruction"));
3410 } else if (zpool_is_draid_spare(new_disk)) {
3411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3412 "dRAID spares can only replace child "
3413 "devices in their parent's dRAID vdev"));
9a49d3f3 3414 } else if (version >= SPA_VERSION_MULTI_REPLACE) {
572e2857
BB
3415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3416 "already in replacing/spare config; wait "
3417 "for completion or use 'zpool detach'"));
9a49d3f3 3418 } else {
34dc7c2f
BB
3419 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3420 "cannot replace a replacing device"));
9a49d3f3 3421 }
34dc7c2f 3422 } else {
1139e170
RE
3423 char status[64] = {0};
3424 zpool_prop_get_feature(zhp,
3425 "feature@device_rebuild", status, 63);
3426 if (rebuild &&
3427 strncmp(status, ZFS_FEATURE_DISABLED, 64) == 0) {
3428 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3429 "device_rebuild feature must be enabled "
3430 "in order to use sequential "
3431 "reconstruction"));
3432 } else {
3433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3434 "can only attach to mirrors and top-level "
3435 "disks"));
3436 }
34dc7c2f
BB
3437 }
3438 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3439 break;
3440
3441 case EINVAL:
3442 /*
3443 * The new device must be a single disk.
3444 */
3445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3446 "new device must be a single disk"));
3447 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3448 break;
3449
3450 case EBUSY:
a1d477c2 3451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
9e052db4 3452 "or device removal is in progress"),
34dc7c2f
BB
3453 new_disk);
3454 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3455 break;
3456
3457 case EOVERFLOW:
3458 /*
3459 * The new device is too small.
3460 */
3461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3462 "device is too small"));
3463 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3464 break;
3465
3466 case EDOM:
3467 /*
d4aae2a0 3468 * The new device has a different optimal sector size.
34dc7c2f
BB
3469 */
3470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
d4aae2a0
BB
3471 "new device has a different optimal sector size; use the "
3472 "option '-o ashift=N' to override the optimal size"));
34dc7c2f
BB
3473 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3474 break;
3475
3476 case ENAMETOOLONG:
3477 /*
3478 * The resulting top-level vdev spec won't fit in the label.
3479 */
3480 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3481 break;
3482
3483 default:
3484 (void) zpool_standard_error(hdl, errno, msg);
3485 }
3486
3487 return (-1);
3488}
3489
3490/*
3491 * Detach the specified device.
3492 */
3493int
3494zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3495{
13fe0198 3496 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3497 char msg[1024];
3498 nvlist_t *tgt;
3499 boolean_t avail_spare, l2cache;
3500 libzfs_handle_t *hdl = zhp->zpool_hdl;
3501
3502 (void) snprintf(msg, sizeof (msg),
3503 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3504
3505 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3506 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
a1d477c2 3507 NULL)) == NULL)
34dc7c2f
BB
3508 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3509
3510 if (avail_spare)
3511 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3512
3513 if (l2cache)
3514 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3515
8bb9ecf4 3516 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f
BB
3517
3518 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3519 return (0);
3520
3521 switch (errno) {
3522
3523 case ENOTSUP:
3524 /*
3525 * Can't detach from this type of vdev.
3526 */
3527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3528 "applicable to mirror and replacing vdevs"));
572e2857 3529 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
34dc7c2f
BB
3530 break;
3531
3532 case EBUSY:
3533 /*
3534 * There are no other replicas of this device.
3535 */
3536 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3537 break;
3538
3539 default:
3540 (void) zpool_standard_error(hdl, errno, msg);
3541 }
3542
3543 return (-1);
3544}
3545
428870ff
BB
3546/*
3547 * Find a mirror vdev in the source nvlist.
3548 *
3549 * The mchild array contains a list of disks in one of the top-level mirrors
3550 * of the source pool. The schild array contains a list of disks that the
3551 * user specified on the command line. We loop over the mchild array to
3552 * see if any entry in the schild array matches.
3553 *
3554 * If a disk in the mchild array is found in the schild array, we return
3555 * the index of that entry. Otherwise we return -1.
3556 */
3557static int
3558find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3559 nvlist_t **schild, uint_t schildren)
3560{
3561 uint_t mc;
3562
3563 for (mc = 0; mc < mchildren; mc++) {
3564 uint_t sc;
3565 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
d2f3e292 3566 mchild[mc], 0);
428870ff
BB
3567
3568 for (sc = 0; sc < schildren; sc++) {
3569 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
d2f3e292 3570 schild[sc], 0);
428870ff
BB
3571 boolean_t result = (strcmp(mpath, spath) == 0);
3572
3573 free(spath);
3574 if (result) {
3575 free(mpath);
3576 return (mc);
3577 }
3578 }
3579
3580 free(mpath);
3581 }
3582
3583 return (-1);
3584}
3585
3586/*
3587 * Split a mirror pool. If newroot points to null, then a new nvlist
3588 * is generated and it is the responsibility of the caller to free it.
3589 */
3590int
3591zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3592 nvlist_t *props, splitflags_t flags)
3593{
13fe0198 3594 zfs_cmd_t zc = {"\0"};
0cb40fa3 3595 char msg[1024], *bias;
428870ff
BB
3596 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3597 nvlist_t **varray = NULL, *zc_props = NULL;
3598 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3599 libzfs_handle_t *hdl = zhp->zpool_hdl;
7fab6361 3600 uint64_t vers, readonly = B_FALSE;
428870ff
BB
3601 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3602 int retval = 0;
3603
3604 (void) snprintf(msg, sizeof (msg),
3605 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3606
3607 if (!zpool_name_valid(hdl, B_FALSE, newname))
3608 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3609
3610 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3611 (void) fprintf(stderr, gettext("Internal error: unable to "
3612 "retrieve pool configuration\n"));
3613 return (-1);
3614 }
3615
8bb9ecf4
RM
3616 tree = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
3617 vers = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
428870ff
BB
3618
3619 if (props) {
572e2857 3620 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
428870ff 3621 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
572e2857 3622 props, vers, flags, msg)) == NULL)
428870ff 3623 return (-1);
7fab6361 3624 (void) nvlist_lookup_uint64(zc_props,
3625 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3626 if (readonly) {
3627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3628 "property %s can only be set at import time"),
3629 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3630 return (-1);
3631 }
428870ff
BB
3632 }
3633
3634 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3635 &children) != 0) {
3636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3637 "Source pool is missing vdev tree"));
8a5fc748 3638 nvlist_free(zc_props);
428870ff
BB
3639 return (-1);
3640 }
3641
3642 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3643 vcount = 0;
3644
3645 if (*newroot == NULL ||
3646 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3647 &newchild, &newchildren) != 0)
3648 newchildren = 0;
3649
3650 for (c = 0; c < children; c++) {
3651 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
0cb40fa3 3652 boolean_t is_special = B_FALSE, is_dedup = B_FALSE;
428870ff
BB
3653 char *type;
3654 nvlist_t **mchild, *vdev;
3655 uint_t mchildren;
3656 int entry;
3657
3658 /*
3659 * Unlike cache & spares, slogs are stored in the
3660 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3661 */
3662 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3663 &is_log);
3664 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3665 &is_hole);
3666 if (is_log || is_hole) {
3667 /*
3668 * Create a hole vdev and put it in the config.
3669 */
3670 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3671 goto out;
3672 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3673 VDEV_TYPE_HOLE) != 0)
3674 goto out;
3675 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3676 1) != 0)
3677 goto out;
3678 if (lastlog == 0)
3679 lastlog = vcount;
3680 varray[vcount++] = vdev;
3681 continue;
3682 }
3683 lastlog = 0;
8bb9ecf4 3684 type = fnvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE);
1b664952
GA
3685
3686 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
3687 vdev = child[c];
3688 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3689 goto out;
3690 continue;
3691 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
428870ff
BB
3692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3693 "Source pool must be composed only of mirrors\n"));
3694 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3695 goto out;
3696 }
3697
0cb40fa3
AF
3698 if (nvlist_lookup_string(child[c],
3699 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) {
3700 if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
3701 is_special = B_TRUE;
3702 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
3703 is_dedup = B_TRUE;
3704 }
428870ff
BB
3705 verify(nvlist_lookup_nvlist_array(child[c],
3706 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3707
3708 /* find or add an entry for this top-level vdev */
3709 if (newchildren > 0 &&
3710 (entry = find_vdev_entry(zhp, mchild, mchildren,
3711 newchild, newchildren)) >= 0) {
3712 /* We found a disk that the user specified. */
3713 vdev = mchild[entry];
3714 ++found;
3715 } else {
3716 /* User didn't specify a disk for this vdev. */
3717 vdev = mchild[mchildren - 1];
3718 }
3719
3720 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3721 goto out;
0cb40fa3
AF
3722
3723 if (flags.dryrun != 0) {
3724 if (is_dedup == B_TRUE) {
3725 if (nvlist_add_string(varray[vcount - 1],
3726 ZPOOL_CONFIG_ALLOCATION_BIAS,
3727 VDEV_ALLOC_BIAS_DEDUP) != 0)
3728 goto out;
3729 } else if (is_special == B_TRUE) {
3730 if (nvlist_add_string(varray[vcount - 1],
3731 ZPOOL_CONFIG_ALLOCATION_BIAS,
3732 VDEV_ALLOC_BIAS_SPECIAL) != 0)
3733 goto out;
3734 }
3735 }
428870ff
BB
3736 }
3737
3738 /* did we find every disk the user specified? */
3739 if (found != newchildren) {
3740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3741 "include at most one disk from each mirror"));
3742 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3743 goto out;
3744 }
3745
3746 /* Prepare the nvlist for populating. */
3747 if (*newroot == NULL) {
3748 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3749 goto out;
3750 freelist = B_TRUE;
3751 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3752 VDEV_TYPE_ROOT) != 0)
3753 goto out;
3754 } else {
3755 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3756 }
3757
3758 /* Add all the children we found */
795075e6
PD
3759 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3760 (const nvlist_t **)varray, lastlog == 0 ? vcount : lastlog) != 0)
428870ff
BB
3761 goto out;
3762
3763 /*
3764 * If we're just doing a dry run, exit now with success.
3765 */
3766 if (flags.dryrun) {
3767 memory_err = B_FALSE;
3768 freelist = B_FALSE;
3769 goto out;
3770 }
3771
3772 /* now build up the config list & call the ioctl */
3773 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3774 goto out;
3775
3776 if (nvlist_add_nvlist(newconfig,
3777 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3778 nvlist_add_string(newconfig,
3779 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3780 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3781 goto out;
3782
3783 /*
3784 * The new pool is automatically part of the namespace unless we
3785 * explicitly export it.
3786 */
3787 if (!flags.import)
3788 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3789 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3790 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
18dbf5c8
AZ
3791 zcmd_write_conf_nvlist(hdl, &zc, newconfig);
3792 if (zc_props != NULL)
3793 zcmd_write_src_nvlist(hdl, &zc, zc_props);
428870ff
BB
3794
3795 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3796 retval = zpool_standard_error(hdl, errno, msg);
3797 goto out;
3798 }
3799
3800 freelist = B_FALSE;
3801 memory_err = B_FALSE;
3802
3803out:
3804 if (varray != NULL) {
3805 int v;
3806
3807 for (v = 0; v < vcount; v++)
3808 nvlist_free(varray[v]);
3809 free(varray);
3810 }
3811 zcmd_free_nvlists(&zc);
8a5fc748
JJS
3812 nvlist_free(zc_props);
3813 nvlist_free(newconfig);
428870ff
BB
3814 if (freelist) {
3815 nvlist_free(*newroot);
3816 *newroot = NULL;
3817 }
3818
3819 if (retval != 0)
3820 return (retval);
3821
3822 if (memory_err)
3823 return (no_memory(hdl));
3824
3825 return (0);
3826}
3827
34dc7c2f 3828/*
a1d477c2 3829 * Remove the given device.
34dc7c2f
BB
3830 */
3831int
3832zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3833{
13fe0198 3834 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3835 char msg[1024];
3836 nvlist_t *tgt;
428870ff 3837 boolean_t avail_spare, l2cache, islog;
34dc7c2f 3838 libzfs_handle_t *hdl = zhp->zpool_hdl;
428870ff 3839 uint64_t version;
34dc7c2f
BB
3840
3841 (void) snprintf(msg, sizeof (msg),
3842 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3843
b2255edc
BB
3844 if (zpool_is_draid_spare(path)) {
3845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3846 "dRAID spares cannot be removed"));
3847 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3848 }
3849
34dc7c2f 3850 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3851 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
a1d477c2 3852 &islog)) == NULL)
34dc7c2f 3853 return (zfs_error(hdl, EZFS_NODEVICE, msg));
34dc7c2f 3854
428870ff
BB
3855 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3856 if (islog && version < SPA_VERSION_HOLES) {
3857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
a1d477c2 3858 "pool must be upgraded to support log removal"));
428870ff
BB
3859 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3860 }
3861
a1d477c2
MA
3862 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3863
3864 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3865 return (0);
3866
3867 switch (errno) {
3868
3869 case EINVAL:
3870 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3871 "invalid config; all top-level vdevs must "
3872 "have the same sector size and not be raidz."));
3873 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3874 break;
3875
3876 case EBUSY:
2ffd89fc
PZ
3877 if (islog) {
3878 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3879 "Mount encrypted datasets to replay logs."));
3880 } else {
3881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3882 "Pool busy; removal may already be in progress"));
3883 }
a1d477c2
MA
3884 (void) zfs_error(hdl, EZFS_BUSY, msg);
3885 break;
3886
2ffd89fc
PZ
3887 case EACCES:
3888 if (islog) {
3889 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3890 "Mount encrypted datasets to replay logs."));
3891 (void) zfs_error(hdl, EZFS_BUSY, msg);
3892 } else {
3893 (void) zpool_standard_error(hdl, errno, msg);
3894 }
3895 break;
3896
a1d477c2
MA
3897 default:
3898 (void) zpool_standard_error(hdl, errno, msg);
3899 }
3900 return (-1);
3901}
3902
3903int
3904zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3905{
861166b0 3906 zfs_cmd_t zc = {{0}};
a1d477c2
MA
3907 char msg[1024];
3908 libzfs_handle_t *hdl = zhp->zpool_hdl;
3909
3910 (void) snprintf(msg, sizeof (msg),
3911 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3912
a1d477c2
MA
3913 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3914 zc.zc_cookie = 1;
34dc7c2f
BB
3915
3916 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3917 return (0);
3918
3919 return (zpool_standard_error(hdl, errno, msg));
3920}
3921
a1d477c2
MA
3922int
3923zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3924 uint64_t *sizep)
3925{
3926 char msg[1024];
3927 nvlist_t *tgt;
3928 boolean_t avail_spare, l2cache, islog;
3929 libzfs_handle_t *hdl = zhp->zpool_hdl;
3930
3931 (void) snprintf(msg, sizeof (msg),
3932 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3933 path);
3934
3935 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3936 &islog)) == NULL)
3937 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3938
3939 if (avail_spare || l2cache || islog) {
3940 *sizep = 0;
3941 return (0);
3942 }
3943
3944 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3946 "indirect size not available"));
3947 return (zfs_error(hdl, EINVAL, msg));
3948 }
3949 return (0);
3950}
3951
34dc7c2f
BB
3952/*
3953 * Clear the errors for the pool, or the particular device if specified.
3954 */
3955int
428870ff 3956zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
34dc7c2f 3957{
13fe0198 3958 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
3959 char msg[1024];
3960 nvlist_t *tgt;
8a393be3 3961 zpool_load_policy_t policy;
34dc7c2f
BB
3962 boolean_t avail_spare, l2cache;
3963 libzfs_handle_t *hdl = zhp->zpool_hdl;
428870ff 3964 nvlist_t *nvi = NULL;
572e2857 3965 int error;
34dc7c2f
BB
3966
3967 if (path)
3968 (void) snprintf(msg, sizeof (msg),
3969 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3970 path);
3971 else
3972 (void) snprintf(msg, sizeof (msg),
3973 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3974 zhp->zpool_name);
3975
3976 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3977 if (path) {
3978 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
a1d477c2 3979 &l2cache, NULL)) == NULL)
34dc7c2f
BB
3980 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3981
3982 /*
3983 * Don't allow error clearing for hot spares. Do allow
3984 * error clearing for l2cache devices.
3985 */
3986 if (avail_spare)
3987 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3988
8bb9ecf4 3989 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f
BB
3990 }
3991
8a393be3
PZ
3992 zpool_get_load_policy(rewindnvl, &policy);
3993 zc.zc_cookie = policy.zlp_rewind;
428870ff 3994
18dbf5c8
AZ
3995 zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2);
3996 zcmd_write_src_nvlist(hdl, &zc, rewindnvl);
428870ff 3997
572e2857 3998 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
18dbf5c8
AZ
3999 errno == ENOMEM)
4000 zcmd_expand_dst_nvlist(hdl, &zc);
572e2857 4001
8a393be3 4002 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
428870ff 4003 errno != EPERM && errno != EACCES)) {
8a393be3 4004 if (policy.zlp_rewind &
428870ff
BB
4005 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
4006 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
4007 zpool_rewind_exclaim(hdl, zc.zc_name,
8a393be3 4008 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
428870ff
BB
4009 nvi);
4010 nvlist_free(nvi);
4011 }
4012 zcmd_free_nvlists(&zc);
34dc7c2f 4013 return (0);
428870ff 4014 }
34dc7c2f 4015
428870ff 4016 zcmd_free_nvlists(&zc);
34dc7c2f
BB
4017 return (zpool_standard_error(hdl, errno, msg));
4018}
4019
4020/*
4021 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4022 */
4023int
4024zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
4025{
13fe0198 4026 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4027 char msg[1024];
4028 libzfs_handle_t *hdl = zhp->zpool_hdl;
4029
4030 (void) snprintf(msg, sizeof (msg),
4031 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
d1d7e268 4032 (u_longlong_t)guid);
34dc7c2f
BB
4033
4034 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4035 zc.zc_guid = guid;
428870ff 4036 zc.zc_cookie = ZPOOL_NO_REWIND;
34dc7c2f 4037
b834b58a 4038 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
34dc7c2f
BB
4039 return (0);
4040
4041 return (zpool_standard_error(hdl, errno, msg));
4042}
4043
3541dc6d
GA
4044/*
4045 * Change the GUID for a pool.
4046 */
4047int
4048zpool_reguid(zpool_handle_t *zhp)
4049{
4050 char msg[1024];
4051 libzfs_handle_t *hdl = zhp->zpool_hdl;
13fe0198 4052 zfs_cmd_t zc = {"\0"};
3541dc6d
GA
4053
4054 (void) snprintf(msg, sizeof (msg),
4055 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
4056
4057 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4058 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
4059 return (0);
4060
4061 return (zpool_standard_error(hdl, errno, msg));
4062}
4063
1bd201e7
CS
4064/*
4065 * Reopen the pool.
4066 */
4067int
d3f2cd7e 4068zpool_reopen_one(zpool_handle_t *zhp, void *data)
1bd201e7 4069{
d3f2cd7e
AB
4070 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4071 const char *pool_name = zpool_get_name(zhp);
4072 boolean_t *scrub_restart = data;
4073 int error;
1bd201e7 4074
d3f2cd7e
AB
4075 error = lzc_reopen(pool_name, *scrub_restart);
4076 if (error) {
4077 return (zpool_standard_error_fmt(hdl, error,
4078 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
4079 }
1bd201e7 4080
d3f2cd7e 4081 return (0);
1bd201e7
CS
4082}
4083
bec1067d
AP
4084/* call into libzfs_core to execute the sync IOCTL per pool */
4085int
4086zpool_sync_one(zpool_handle_t *zhp, void *data)
4087{
4088 int ret;
4089 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4090 const char *pool_name = zpool_get_name(zhp);
4091 boolean_t *force = data;
4092 nvlist_t *innvl = fnvlist_alloc();
4093
4094 fnvlist_add_boolean_value(innvl, "force", *force);
4095 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
4096 nvlist_free(innvl);
4097 return (zpool_standard_error_fmt(hdl, ret,
4098 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
4099 }
4100 nvlist_free(innvl);
4101
4102 return (0);
4103}
4104
858219cc
NB
4105#define PATH_BUF_LEN 64
4106
34dc7c2f
BB
4107/*
4108 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4109 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4110 * We also check if this is a whole disk, in which case we strip off the
4111 * trailing 's0' slice name.
4112 *
4113 * This routine is also responsible for identifying when disks have been
4114 * reconfigured in a new location. The kernel will have opened the device by
4115 * devid, but the path will still refer to the old location. To catch this, we
4116 * first do a path -> devid translation (which is fast for the common case). If
4117 * the devid matches, we're done. If not, we do a reverse devid -> path
4118 * translation and issue the appropriate ioctl() to update the path of the vdev.
4119 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4120 * of these checks.
4121 */
4122char *
428870ff 4123zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
d2f3e292 4124 int name_flags)
34dc7c2f 4125{
39fc0cb5 4126 char *path, *type, *env;
34dc7c2f 4127 uint64_t value;
858219cc 4128 char buf[PATH_BUF_LEN];
fc24f7c8 4129 char tmpbuf[PATH_BUF_LEN];
34dc7c2f 4130
2df9ad1c
GG
4131 /*
4132 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4133 * zpool name that will be displayed to the user.
4134 */
8bb9ecf4 4135 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE);
2df9ad1c
GG
4136 if (zhp != NULL && strcmp(type, "root") == 0)
4137 return (zfs_strdup(hdl, zpool_get_name(zhp)));
4138
d2f3e292
RY
4139 env = getenv("ZPOOL_VDEV_NAME_PATH");
4140 if (env && (strtoul(env, NULL, 0) > 0 ||
4141 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4142 name_flags |= VDEV_NAME_PATH;
4143
4144 env = getenv("ZPOOL_VDEV_NAME_GUID");
4145 if (env && (strtoul(env, NULL, 0) > 0 ||
4146 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4147 name_flags |= VDEV_NAME_GUID;
4148
4149 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
4150 if (env && (strtoul(env, NULL, 0) > 0 ||
4151 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4152 name_flags |= VDEV_NAME_FOLLOW_LINKS;
4153
4154 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4155 name_flags & VDEV_NAME_GUID) {
aecdc706 4156 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
d2f3e292 4157 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
34dc7c2f
BB
4158 path = buf;
4159 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
d2f3e292
RY
4160 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4161 char *rp = realpath(path, NULL);
4162 if (rp) {
4163 strlcpy(buf, rp, sizeof (buf));
4164 path = buf;
4165 free(rp);
4166 }
4167 }
4168
d603ed6c
BB
4169 /*
4170 * For a block device only use the name.
4171 */
d2f3e292
RY
4172 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
4173 !(name_flags & VDEV_NAME_PATH)) {
035ebb36 4174 path = zfs_strip_path(path);
d603ed6c 4175 }
34dc7c2f 4176
d603ed6c 4177 /*
b2255edc 4178 * Remove the partition from the path if this is a whole disk.
d603ed6c 4179 */
b2255edc
BB
4180 if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 &&
4181 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
d2f3e292 4182 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
6078881a 4183 return (zfs_strip_partition(path));
34dc7c2f
BB
4184 }
4185 } else {
2df9ad1c 4186 path = type;
34dc7c2f
BB
4187
4188 /*
4189 * If it's a raidz device, we need to stick in the parity level.
4190 */
4191 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
8bb9ecf4 4192 value = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY);
fc24f7c8 4193 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
34dc7c2f 4194 (u_longlong_t)value);
fc24f7c8 4195 path = buf;
34dc7c2f 4196 }
428870ff 4197
b2255edc
BB
4198 /*
4199 * If it's a dRAID device, we add parity, groups, and spares.
4200 */
4201 if (strcmp(path, VDEV_TYPE_DRAID) == 0) {
4202 uint64_t ndata, nparity, nspares;
4203 nvlist_t **child;
4204 uint_t children;
4205
4206 verify(nvlist_lookup_nvlist_array(nv,
4207 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
8bb9ecf4
RM
4208 nparity = fnvlist_lookup_uint64(nv,
4209 ZPOOL_CONFIG_NPARITY);
4210 ndata = fnvlist_lookup_uint64(nv,
4211 ZPOOL_CONFIG_DRAID_NDATA);
4212 nspares = fnvlist_lookup_uint64(nv,
4213 ZPOOL_CONFIG_DRAID_NSPARES);
b2255edc
BB
4214
4215 path = zpool_draid_name(buf, sizeof (buf), ndata,
4216 nparity, nspares, children);
4217 }
4218
428870ff
BB
4219 /*
4220 * We identify each top-level vdev by using a <type-id>
4221 * naming convention.
4222 */
d2f3e292 4223 if (name_flags & VDEV_NAME_TYPE_ID) {
8bb9ecf4
RM
4224 uint64_t id = fnvlist_lookup_uint64(nv,
4225 ZPOOL_CONFIG_ID);
fc24f7c8
MM
4226 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
4227 path, (u_longlong_t)id);
4228 path = tmpbuf;
428870ff 4229 }
34dc7c2f
BB
4230 }
4231
4232 return (zfs_strdup(hdl, path));
4233}
4234
4235static int
fcff0f35 4236zbookmark_mem_compare(const void *a, const void *b)
34dc7c2f 4237{
5dbd68a3 4238 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
34dc7c2f
BB
4239}
4240
4241/*
4242 * Retrieve the persistent error log, uniquify the members, and return to the
4243 * caller.
4244 */
4245int
4246zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4247{
13fe0198 4248 zfs_cmd_t zc = {"\0"};
56a6054d 4249 libzfs_handle_t *hdl = zhp->zpool_hdl;
34dc7c2f 4250 uint64_t count;
5dbd68a3 4251 zbookmark_phys_t *zb = NULL;
34dc7c2f
BB
4252 int i;
4253
4254 /*
4255 * Retrieve the raw error list from the kernel. If the number of errors
4256 * has increased, allocate more space and continue until we get the
4257 * entire list.
4258 */
8bb9ecf4 4259 count = fnvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT);
34dc7c2f
BB
4260 if (count == 0)
4261 return (0);
56a6054d
BB
4262 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4263 count * sizeof (zbookmark_phys_t));
34dc7c2f
BB
4264 zc.zc_nvlist_dst_size = count;
4265 (void) strcpy(zc.zc_name, zhp->zpool_name);
4266 for (;;) {
b834b58a 4267 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
34dc7c2f
BB
4268 &zc) != 0) {
4269 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4270 if (errno == ENOMEM) {
5dbd68a3
MA
4271 void *dst;
4272
34dc7c2f 4273 count = zc.zc_nvlist_dst_size;
5dbd68a3
MA
4274 dst = zfs_alloc(zhp->zpool_hdl, count *
4275 sizeof (zbookmark_phys_t));
5dbd68a3 4276 zc.zc_nvlist_dst = (uintptr_t)dst;
34dc7c2f 4277 } else {
56a6054d
BB
4278 return (zpool_standard_error_fmt(hdl, errno,
4279 dgettext(TEXT_DOMAIN, "errors: List of "
4280 "errors unavailable")));
34dc7c2f
BB
4281 }
4282 } else {
4283 break;
4284 }
4285 }
4286
4287 /*
4288 * Sort the resulting bookmarks. This is a little confusing due to the
4289 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
78595377 4290 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
34dc7c2f
BB
4291 * _not_ copied as part of the process. So we point the start of our
4292 * array appropriate and decrement the total number of elements.
4293 */
5dbd68a3 4294 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
34dc7c2f
BB
4295 zc.zc_nvlist_dst_size;
4296 count -= zc.zc_nvlist_dst_size;
4297
fcff0f35 4298 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
34dc7c2f
BB
4299
4300 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4301
4302 /*
4303 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4304 */
4305 for (i = 0; i < count; i++) {
4306 nvlist_t *nv;
4307
4308 /* ignoring zb_blkid and zb_level for now */
4309 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4310 zb[i-1].zb_object == zb[i].zb_object)
4311 continue;
4312
4313 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4314 goto nomem;
4315 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4316 zb[i].zb_objset) != 0) {
4317 nvlist_free(nv);
4318 goto nomem;
4319 }
4320 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4321 zb[i].zb_object) != 0) {
4322 nvlist_free(nv);
4323 goto nomem;
4324 }
4325 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4326 nvlist_free(nv);
4327 goto nomem;
4328 }
4329 nvlist_free(nv);
4330 }
4331
4332 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4333 return (0);
4334
4335nomem:
4336 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4337 return (no_memory(zhp->zpool_hdl));
4338}
4339
4340/*
4341 * Upgrade a ZFS pool to the latest on-disk version.
4342 */
4343int
4344zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4345{
13fe0198 4346 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4347 libzfs_handle_t *hdl = zhp->zpool_hdl;
4348
4349 (void) strcpy(zc.zc_name, zhp->zpool_name);
4350 zc.zc_cookie = new_version;
4351
4352 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4353 return (zpool_standard_error_fmt(hdl, errno,
4354 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4355 zhp->zpool_name));
4356 return (0);
4357}
4358
4359void
6f1ffb06 4360zfs_save_arguments(int argc, char **argv, char *string, int len)
34dc7c2f
BB
4361{
4362 int i;
4363
feb04e66 4364 (void) strlcpy(string, zfs_basename(argv[0]), len);
34dc7c2f 4365 for (i = 1; i < argc; i++) {
6f1ffb06
MA
4366 (void) strlcat(string, " ", len);
4367 (void) strlcat(string, argv[i], len);
34dc7c2f
BB
4368 }
4369}
4370
34dc7c2f 4371int
6f1ffb06
MA
4372zpool_log_history(libzfs_handle_t *hdl, const char *message)
4373{
13fe0198 4374 zfs_cmd_t zc = {"\0"};
6f1ffb06 4375 nvlist_t *args;
6f1ffb06
MA
4376
4377 args = fnvlist_alloc();
4378 fnvlist_add_string(args, "message", message);
18dbf5c8
AZ
4379 zcmd_write_src_nvlist(hdl, &zc, args);
4380 int err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
6f1ffb06
MA
4381 nvlist_free(args);
4382 zcmd_free_nvlists(&zc);
4383 return (err);
34dc7c2f
BB
4384}
4385
4386/*
4387 * Perform ioctl to get some command history of a pool.
4388 *
4389 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4390 * logical offset of the history buffer to start reading from.
4391 *
4392 * Upon return, 'off' is the next logical offset to read from and
4393 * 'len' is the actual amount of bytes read into 'buf'.
4394 */
4395static int
4396get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4397{
13fe0198 4398 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4399 libzfs_handle_t *hdl = zhp->zpool_hdl;
4400
4401 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4402
4403 zc.zc_history = (uint64_t)(uintptr_t)buf;
4404 zc.zc_history_len = *len;
4405 zc.zc_history_offset = *off;
4406
b834b58a 4407 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
34dc7c2f
BB
4408 switch (errno) {
4409 case EPERM:
4410 return (zfs_error_fmt(hdl, EZFS_PERM,
4411 dgettext(TEXT_DOMAIN,
4412 "cannot show history for pool '%s'"),
4413 zhp->zpool_name));
4414 case ENOENT:
4415 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4416 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4417 "'%s'"), zhp->zpool_name));
4418 case ENOTSUP:
4419 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4420 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4421 "'%s', pool must be upgraded"), zhp->zpool_name));
4422 default:
4423 return (zpool_standard_error_fmt(hdl, errno,
4424 dgettext(TEXT_DOMAIN,
4425 "cannot get history for '%s'"), zhp->zpool_name));
4426 }
4427 }
4428
4429 *len = zc.zc_history_len;
4430 *off = zc.zc_history_offset;
4431
4432 return (0);
4433}
4434
34dc7c2f
BB
4435/*
4436 * Retrieve the command history of a pool.
4437 */
4438int
7125a109
CC
4439zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4440 boolean_t *eof)
34dc7c2f 4441{
4dced31b 4442 libzfs_handle_t *hdl = zhp->zpool_hdl;
1f6f97f3
MA
4443 char *buf;
4444 int buflen = 128 * 1024;
34dc7c2f
BB
4445 nvlist_t **records = NULL;
4446 uint_t numrecords = 0;
4dced31b 4447 int err = 0, i;
7125a109 4448 uint64_t start = *off;
34dc7c2f 4449
4dced31b
W
4450 buf = zfs_alloc(hdl, buflen);
4451
4452 /* process about 1MiB a time */
7125a109 4453 while (*off - start < 1024 * 1024) {
1f6f97f3 4454 uint64_t bytes_read = buflen;
34dc7c2f
BB
4455 uint64_t leftover;
4456
7125a109 4457 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
34dc7c2f
BB
4458 break;
4459
4460 /* if nothing else was read in, we're at EOF, just return */
7125a109
CC
4461 if (!bytes_read) {
4462 *eof = B_TRUE;
34dc7c2f 4463 break;
7125a109 4464 }
34dc7c2f
BB
4465
4466 if ((err = zpool_history_unpack(buf, bytes_read,
4dced31b
W
4467 &leftover, &records, &numrecords)) != 0) {
4468 zpool_standard_error_fmt(hdl, err,
4469 dgettext(TEXT_DOMAIN,
4470 "cannot get history for '%s'"), zhp->zpool_name);
34dc7c2f 4471 break;
4dced31b 4472 }
7125a109 4473 *off -= leftover;
1f6f97f3
MA
4474 if (leftover == bytes_read) {
4475 /*
4476 * no progress made, because buffer is not big enough
4477 * to hold this record; resize and retry.
4478 */
4479 buflen *= 2;
4480 free(buf);
4dced31b 4481 buf = zfs_alloc(hdl, buflen);
1f6f97f3 4482 }
7125a109 4483 }
34dc7c2f 4484
1f6f97f3
MA
4485 free(buf);
4486
34dc7c2f 4487 if (!err) {
8bb9ecf4
RM
4488 *nvhisp = fnvlist_alloc();
4489 fnvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4490 (const nvlist_t **)records, numrecords);
34dc7c2f
BB
4491 }
4492 for (i = 0; i < numrecords; i++)
4493 nvlist_free(records[i]);
4494 free(records);
4495
4496 return (err);
4497}
4498
26685276 4499/*
9b101a73
BB
4500 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4501 * If there is a new event available 'nvp' will contain a newly allocated
4502 * nvlist and 'dropped' will be set to the number of missed events since
4503 * the last call to this function. When 'nvp' is set to NULL it indicates
4504 * no new events are available. In either case the function returns 0 and
4505 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4506 * function will return a non-zero value. When the function is called in
8c7aa0cf
CD
4507 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4508 * it will not return until a new event is available.
26685276
BB
4509 */
4510int
4511zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
8c7aa0cf 4512 int *dropped, unsigned flags, int zevent_fd)
26685276 4513{
13fe0198 4514 zfs_cmd_t zc = {"\0"};
26685276
BB
4515 int error = 0;
4516
4517 *nvp = NULL;
4518 *dropped = 0;
9b101a73 4519 zc.zc_cleanup_fd = zevent_fd;
26685276 4520
8c7aa0cf 4521 if (flags & ZEVENT_NONBLOCK)
26685276
BB
4522 zc.zc_guid = ZEVENT_NONBLOCK;
4523
18dbf5c8 4524 zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE);
26685276
BB
4525
4526retry:
4527 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4528 switch (errno) {
4529 case ESHUTDOWN:
4530 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4531 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4532 goto out;
4533 case ENOENT:
4534 /* Blocking error case should not occur */
8c7aa0cf 4535 if (!(flags & ZEVENT_NONBLOCK))
26685276
BB
4536 error = zpool_standard_error_fmt(hdl, errno,
4537 dgettext(TEXT_DOMAIN, "cannot get event"));
4538
4539 goto out;
4540 case ENOMEM:
18dbf5c8
AZ
4541 zcmd_expand_dst_nvlist(hdl, &zc);
4542 goto retry;
26685276
BB
4543 default:
4544 error = zpool_standard_error_fmt(hdl, errno,
4545 dgettext(TEXT_DOMAIN, "cannot get event"));
4546 goto out;
4547 }
4548 }
4549
4550 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4551 if (error != 0)
4552 goto out;
4553
4554 *dropped = (int)zc.zc_cookie;
4555out:
4556 zcmd_free_nvlists(&zc);
4557
4558 return (error);
4559}
4560
4561/*
4562 * Clear all events.
4563 */
4564int
4565zpool_events_clear(libzfs_handle_t *hdl, int *count)
4566{
13fe0198 4567 zfs_cmd_t zc = {"\0"};
26685276
BB
4568
4569 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
f00f4690
AZ
4570 return (zpool_standard_error(hdl, errno,
4571 dgettext(TEXT_DOMAIN, "cannot clear events")));
26685276
BB
4572
4573 if (count != NULL)
4574 *count = (int)zc.zc_cookie; /* # of events cleared */
4575
4576 return (0);
4577}
4578
75e3ff58
BB
4579/*
4580 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4581 * the passed zevent_fd file handle. On success zero is returned,
4582 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4583 */
4584int
4585zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4586{
4587 zfs_cmd_t zc = {"\0"};
4588 int error = 0;
4589
4590 zc.zc_guid = eid;
4591 zc.zc_cleanup_fd = zevent_fd;
4592
4593 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4594 switch (errno) {
4595 case ENOENT:
4596 error = zfs_error_fmt(hdl, EZFS_NOENT,
4597 dgettext(TEXT_DOMAIN, "cannot get event"));
4598 break;
4599
4600 case ENOMEM:
4601 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4602 dgettext(TEXT_DOMAIN, "cannot get event"));
4603 break;
4604
4605 default:
4606 error = zpool_standard_error_fmt(hdl, errno,
4607 dgettext(TEXT_DOMAIN, "cannot get event"));
4608 break;
4609 }
4610 }
4611
4612 return (error);
4613}
4614
de4f06c2
PD
4615static void
4616zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4617 char *pathname, size_t len, boolean_t always_unmounted)
34dc7c2f 4618{
13fe0198 4619 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4620 boolean_t mounted = B_FALSE;
4621 char *mntpnt = NULL;
eca7b760 4622 char dsname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
4623
4624 if (dsobj == 0) {
4625 /* special case for the MOS */
d1d7e268
MK
4626 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4627 (longlong_t)obj);
34dc7c2f
BB
4628 return;
4629 }
4630
4631 /* get the dataset's name */
4632 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4633 zc.zc_obj = dsobj;
b834b58a 4634 if (zfs_ioctl(zhp->zpool_hdl,
34dc7c2f
BB
4635 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4636 /* just write out a path of two object numbers */
4637 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
b8864a23 4638 (longlong_t)dsobj, (longlong_t)obj);
34dc7c2f
BB
4639 return;
4640 }
4641 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4642
4643 /* find out if the dataset is mounted */
de4f06c2
PD
4644 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
4645 &mntpnt);
34dc7c2f
BB
4646
4647 /* get the corrupted object's path */
4648 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4649 zc.zc_obj = obj;
b834b58a 4650 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
34dc7c2f
BB
4651 &zc) == 0) {
4652 if (mounted) {
4653 (void) snprintf(pathname, len, "%s%s", mntpnt,
4654 zc.zc_value);
4655 } else {
4656 (void) snprintf(pathname, len, "%s:%s",
4657 dsname, zc.zc_value);
4658 }
4659 } else {
d1d7e268
MK
4660 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4661 (longlong_t)obj);
34dc7c2f
BB
4662 }
4663 free(mntpnt);
4664}
4665
de4f06c2
PD
4666void
4667zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4668 char *pathname, size_t len)
4669{
4670 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
4671}
4672
4673void
4674zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4675 char *pathname, size_t len)
4676{
4677 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
4678}
e60e158e
JG
4679/*
4680 * Wait while the specified activity is in progress in the pool.
4681 */
4682int
4683zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4684{
4685 boolean_t missing;
4686
4687 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4688
4689 if (missing) {
4690 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4691 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4692 zhp->zpool_name);
4693 return (ENOENT);
4694 } else {
4695 return (error);
4696 }
4697}
4698
4699/*
4700 * Wait for the given activity and return the status of the wait (whether or not
4701 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4702 * reported via the 'missing' parameter, rather than by printing an error
4703 * message. This is convenient when this function is called in a loop over a
4704 * long period of time (as it is, for example, by zpool's wait cmd). In that
4705 * scenario, a pool being exported or destroyed should be considered a normal
4706 * event, so we don't want to print an error when we find that the pool doesn't
4707 * exist.
4708 */
4709int
4710zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4711 boolean_t *missing, boolean_t *waited)
4712{
4713 int error = lzc_wait(zhp->zpool_name, activity, waited);
4714 *missing = (error == ENOENT);
4715 if (*missing)
4716 return (0);
4717
4718 if (error != 0) {
4719 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4720 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4721 zhp->zpool_name);
4722 }
4723
4724 return (error);
4725}
108a454a
PD
4726
4727int
1db9e6e4 4728zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
108a454a
PD
4729{
4730 int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4731 if (error != 0) {
4732 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4733 dgettext(TEXT_DOMAIN,
4734 "error setting bootenv in pool '%s'"), zhp->zpool_name);
4735 }
4736
4737 return (error);
4738}
4739
4740int
1db9e6e4 4741zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
108a454a 4742{
1db9e6e4
TS
4743 nvlist_t *nvl;
4744 int error;
4745
4746 nvl = NULL;
4747 error = lzc_get_bootenv(zhp->zpool_name, &nvl);
108a454a
PD
4748 if (error != 0) {
4749 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4750 dgettext(TEXT_DOMAIN,
4751 "error getting bootenv in pool '%s'"), zhp->zpool_name);
1db9e6e4
TS
4752 } else {
4753 *nvlp = nvl;
108a454a
PD
4754 }
4755
1db9e6e4 4756 return (error);
108a454a 4757}
658fb802
CB
4758
4759/*
4760 * Attempt to read and parse feature file(s) (from "compatibility" property).
4761 * Files contain zpool feature names, comma or whitespace-separated.
4762 * Comments (# character to next newline) are discarded.
4763 *
4764 * Arguments:
4765 * compatibility : string containing feature filenames
4766 * features : either NULL or pointer to array of boolean
e086db16
CB
4767 * report : either NULL or pointer to string buffer
4768 * rlen : length of "report" buffer
658fb802
CB
4769 *
4770 * compatibility is NULL (unset), "", "off", "legacy", or list of
4771 * comma-separated filenames. filenames should either be absolute,
4772 * or relative to:
4773 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4774 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4775 * (Unset), "" or "off" => enable all features
4776 * "legacy" => disable all features
e086db16 4777 *
658fb802
CB
4778 * Any feature names read from files which match unames in spa_feature_table
4779 * will have the corresponding boolean set in the features array (if non-NULL).
4780 * If more than one feature set specified, only features present in *all* of
4781 * them will be set.
4782 *
e086db16 4783 * "report" if not NULL will be populated with a suitable status message.
658fb802
CB
4784 *
4785 * Return values:
4786 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
658fb802 4787 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
e086db16
CB
4788 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4789 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4790 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
658fb802
CB
4791 */
4792zpool_compat_status_t
e086db16
CB
4793zpool_load_compat(const char *compat, boolean_t *features, char *report,
4794 size_t rlen)
658fb802
CB
4795{
4796 int sdirfd, ddirfd, featfd;
658fb802 4797 struct stat fs;
e086db16
CB
4798 char *fc;
4799 char *ps, *ls, *ws;
658fb802 4800 char *file, *line, *word;
e086db16
CB
4801
4802 char l_compat[ZFS_MAXPROPLEN];
4803
4804 boolean_t ret_nofiles = B_TRUE;
4805 boolean_t ret_badfile = B_FALSE;
4806 boolean_t ret_badtoken = B_FALSE;
4807 boolean_t ret_warntoken = B_FALSE;
658fb802
CB
4808
4809 /* special cases (unset), "" and "off" => enable all features */
e086db16
CB
4810 if (compat == NULL || compat[0] == '\0' ||
4811 strcmp(compat, ZPOOL_COMPAT_OFF) == 0) {
658fb802 4812 if (features != NULL)
e086db16 4813 for (uint_t i = 0; i < SPA_FEATURES; i++)
658fb802 4814 features[i] = B_TRUE;
e086db16
CB
4815 if (report != NULL)
4816 strlcpy(report, gettext("all features enabled"), rlen);
658fb802
CB
4817 return (ZPOOL_COMPATIBILITY_OK);
4818 }
4819
4820 /* Final special case "legacy" => disable all features */
e086db16 4821 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
658fb802 4822 if (features != NULL)
e086db16 4823 for (uint_t i = 0; i < SPA_FEATURES; i++)
658fb802 4824 features[i] = B_FALSE;
e086db16
CB
4825 if (report != NULL)
4826 strlcpy(report, gettext("all features disabled"), rlen);
658fb802
CB
4827 return (ZPOOL_COMPATIBILITY_OK);
4828 }
4829
4830 /*
4831 * Start with all true; will be ANDed with results from each file
4832 */
4833 if (features != NULL)
e086db16 4834 for (uint_t i = 0; i < SPA_FEATURES; i++)
658fb802
CB
4835 features[i] = B_TRUE;
4836
e086db16
CB
4837 char err_badfile[1024] = "";
4838 char err_badtoken[1024] = "";
4839
658fb802
CB
4840 /*
4841 * We ignore errors from the directory open()
4842 * as they're only needed if the filename is relative
4843 * which will be checked during the openat().
4844 */
f97142c7
CB
4845
4846/* O_PATH safer than O_RDONLY if system allows it */
4847#if defined(O_PATH)
4848#define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4849#else
4850#define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
658fb802 4851#endif
f97142c7
CB
4852
4853 sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
4854 ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
658fb802 4855
e086db16
CB
4856 (void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
4857
4858 for (file = strtok_r(l_compat, ",", &ps);
4859 file != NULL;
4860 file = strtok_r(NULL, ",", &ps)) {
4861
f97142c7 4862 boolean_t l_features[SPA_FEATURES];
e086db16
CB
4863
4864 enum { Z_SYSCONF, Z_DATA } source;
658fb802
CB
4865
4866 /* try sysconfdir first, then datadir */
e086db16 4867 source = Z_SYSCONF;
8bc357ba
AZ
4868 if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) {
4869 featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC);
e086db16 4870 source = Z_DATA;
658fb802
CB
4871 }
4872
e086db16
CB
4873 /* File readable and correct size? */
4874 if (featfd < 0 ||
4875 fstat(featfd, &fs) < 0 ||
4876 fs.st_size < 1 ||
4877 fs.st_size > ZPOOL_COMPAT_MAXSIZE) {
658fb802 4878 (void) close(featfd);
e086db16
CB
4879 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4880 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4881 ret_badfile = B_TRUE;
4882 continue;
658fb802
CB
4883 }
4884
f97142c7
CB
4885/* Prefault the file if system allows */
4886#if defined(MAP_POPULATE)
4887#define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4888#elif defined(MAP_PREFAULT_READ)
4889#define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4890#else
4891#define ZC_MMAP_FLAGS (MAP_PRIVATE)
8bc357ba 4892#endif
f97142c7 4893
658fb802 4894 /* private mmap() so we can strtok safely */
f97142c7
CB
4895 fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
4896 ZC_MMAP_FLAGS, featfd, 0);
658fb802
CB
4897 (void) close(featfd);
4898
e086db16 4899 /* map ok, and last character == newline? */
8bc357ba 4900 if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') {
658fb802 4901 (void) munmap((void *) fc, fs.st_size);
e086db16
CB
4902 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4903 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4904 ret_badfile = B_TRUE;
4905 continue;
658fb802
CB
4906 }
4907
e086db16 4908 ret_nofiles = B_FALSE;
658fb802 4909
f97142c7
CB
4910 for (uint_t i = 0; i < SPA_FEATURES; i++)
4911 l_features[i] = B_FALSE;
4912
4913 /* replace final newline with NULL to ensure string ends */
e086db16
CB
4914 fc[fs.st_size - 1] = '\0';
4915
4916 for (line = strtok_r(fc, "\n", &ls);
4917 line != NULL;
4918 line = strtok_r(NULL, "\n", &ls)) {
658fb802 4919 /* discard comments */
d2119d0e
JL
4920 char *r = strchr(line, '#');
4921 if (r != NULL)
4922 *r = '\0';
658fb802 4923
e086db16
CB
4924 for (word = strtok_r(line, ", \t", &ws);
4925 word != NULL;
4926 word = strtok_r(NULL, ", \t", &ws)) {
658fb802 4927 /* Find matching feature name */
e086db16
CB
4928 uint_t f;
4929 for (f = 0; f < SPA_FEATURES; f++) {
658fb802 4930 zfeature_info_t *fi =
e086db16 4931 &spa_feature_table[f];
658fb802 4932 if (strcmp(word, fi->fi_uname) == 0) {
e086db16 4933 l_features[f] = B_TRUE;
658fb802
CB
4934 break;
4935 }
4936 }
e086db16
CB
4937 if (f < SPA_FEATURES)
4938 continue;
4939
4940 /* found an unrecognized word */
4941 /* lightly sanitize it */
4942 if (strlen(word) > 32)
4943 word[32] = '\0';
4944 for (char *c = word; *c != '\0'; c++)
4945 if (!isprint(*c))
4946 *c = '?';
4947
4948 strlcat(err_badtoken, word, ZFS_MAXPROPLEN);
4949 strlcat(err_badtoken, " ", ZFS_MAXPROPLEN);
4950 if (source == Z_SYSCONF)
4951 ret_badtoken = B_TRUE;
4952 else
4953 ret_warntoken = B_TRUE;
658fb802 4954 }
658fb802
CB
4955 }
4956 (void) munmap((void *) fc, fs.st_size);
e086db16
CB
4957
4958 if (features != NULL)
4959 for (uint_t i = 0; i < SPA_FEATURES; i++)
4960 features[i] &= l_features[i];
658fb802
CB
4961 }
4962 (void) close(sdirfd);
4963 (void) close(ddirfd);
e086db16
CB
4964
4965 /* Return the most serious error */
4966 if (ret_badfile) {
4967 if (report != NULL)
4968 snprintf(report, rlen, gettext("could not read/"
4969 "parse feature file(s): %s"), err_badfile);
4970 return (ZPOOL_COMPATIBILITY_BADFILE);
4971 }
4972 if (ret_nofiles) {
4973 if (report != NULL)
4974 strlcpy(report,
4975 gettext("no valid compatibility files specified"),
4976 rlen);
658fb802 4977 return (ZPOOL_COMPATIBILITY_NOFILES);
e086db16
CB
4978 }
4979 if (ret_badtoken) {
4980 if (report != NULL)
4981 snprintf(report, rlen, gettext("invalid feature "
4982 "name(s) in local compatibility files: %s"),
4983 err_badtoken);
4984 return (ZPOOL_COMPATIBILITY_BADTOKEN);
4985 }
4986 if (ret_warntoken) {
4987 if (report != NULL)
4988 snprintf(report, rlen, gettext("unrecognized feature "
4989 "name(s) in distribution compatibility files: %s"),
4990 err_badtoken);
4991 return (ZPOOL_COMPATIBILITY_WARNTOKEN);
4992 }
4993 if (report != NULL)
4994 strlcpy(report, gettext("compatibility set ok"), rlen);
658fb802
CB
4995 return (ZPOOL_COMPATIBILITY_OK);
4996}
2a673e76
AJ
4997
4998static int
4999zpool_vdev_guid(zpool_handle_t *zhp, const char *vdevname, uint64_t *vdev_guid)
5000{
5001 nvlist_t *tgt;
5002 boolean_t avail_spare, l2cache;
5003
5004 verify(zhp != NULL);
5005 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5006 char errbuf[1024];
5007 (void) snprintf(errbuf, sizeof (errbuf),
5008 dgettext(TEXT_DOMAIN, "pool is in an unavailable state"));
5009 return (zfs_error(zhp->zpool_hdl, EZFS_POOLUNAVAIL, errbuf));
5010 }
5011
5012 if ((tgt = zpool_find_vdev(zhp, vdevname, &avail_spare, &l2cache,
5013 NULL)) == NULL) {
5014 char errbuf[1024];
5015 (void) snprintf(errbuf, sizeof (errbuf),
5016 dgettext(TEXT_DOMAIN, "can not find %s in %s"),
5017 vdevname, zhp->zpool_name);
5018 return (zfs_error(zhp->zpool_hdl, EZFS_NODEVICE, errbuf));
5019 }
5020
8bb9ecf4 5021 *vdev_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2a673e76
AJ
5022 return (0);
5023}
5024
5025/*
5026 * Get a vdev property value for 'prop' and return the value in
5027 * a pre-allocated buffer.
5028 */
5029int
5030zpool_get_vdev_prop_value(nvlist_t *nvprop, vdev_prop_t prop, char *prop_name,
5031 char *buf, size_t len, zprop_source_t *srctype, boolean_t literal)
5032{
5033 nvlist_t *nv;
2a673e76 5034 char *strval;
8bb9ecf4 5035 uint64_t intval;
2a673e76
AJ
5036 zprop_source_t src = ZPROP_SRC_NONE;
5037
5038 if (prop == VDEV_PROP_USER) {
5039 /* user property, prop_name must contain the property name */
5040 assert(prop_name != NULL);
5041 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
5042 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5043 strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
2a673e76
AJ
5044 } else {
5045 /* user prop not found */
5046 return (-1);
5047 }
5048 (void) strlcpy(buf, strval, len);
5049 if (srctype)
5050 *srctype = src;
5051 return (0);
5052 }
5053
5054 if (prop_name == NULL)
5055 prop_name = (char *)vdev_prop_to_name(prop);
5056
5057 switch (vdev_prop_get_type(prop)) {
5058 case PROP_TYPE_STRING:
5059 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
5060 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5061 strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
2a673e76
AJ
5062 } else {
5063 src = ZPROP_SRC_DEFAULT;
5064 if ((strval = (char *)vdev_prop_default_string(prop))
5065 == NULL)
5066 strval = "-";
5067 }
5068 (void) strlcpy(buf, strval, len);
5069 break;
5070
5071 case PROP_TYPE_NUMBER:
5072 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
5073 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5074 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
2a673e76
AJ
5075 } else {
5076 src = ZPROP_SRC_DEFAULT;
5077 intval = vdev_prop_default_numeric(prop);
5078 }
5079
5080 switch (prop) {
5081 case VDEV_PROP_ASIZE:
5082 case VDEV_PROP_PSIZE:
5083 case VDEV_PROP_SIZE:
be8e1d81 5084 case VDEV_PROP_BOOTSIZE:
2a673e76
AJ
5085 case VDEV_PROP_ALLOCATED:
5086 case VDEV_PROP_FREE:
5087 case VDEV_PROP_READ_ERRORS:
5088 case VDEV_PROP_WRITE_ERRORS:
5089 case VDEV_PROP_CHECKSUM_ERRORS:
5090 case VDEV_PROP_INITIALIZE_ERRORS:
5091 case VDEV_PROP_OPS_NULL:
5092 case VDEV_PROP_OPS_READ:
5093 case VDEV_PROP_OPS_WRITE:
5094 case VDEV_PROP_OPS_FREE:
5095 case VDEV_PROP_OPS_CLAIM:
5096 case VDEV_PROP_OPS_TRIM:
5097 case VDEV_PROP_BYTES_NULL:
5098 case VDEV_PROP_BYTES_READ:
5099 case VDEV_PROP_BYTES_WRITE:
5100 case VDEV_PROP_BYTES_FREE:
5101 case VDEV_PROP_BYTES_CLAIM:
5102 case VDEV_PROP_BYTES_TRIM:
5103 if (literal) {
5104 (void) snprintf(buf, len, "%llu",
5105 (u_longlong_t)intval);
5106 } else {
5107 (void) zfs_nicenum(intval, buf, len);
5108 }
5109 break;
5110 case VDEV_PROP_EXPANDSZ:
5111 if (intval == 0) {
5112 (void) strlcpy(buf, "-", len);
5113 } else if (literal) {
5114 (void) snprintf(buf, len, "%llu",
5115 (u_longlong_t)intval);
5116 } else {
5117 (void) zfs_nicenum(intval, buf, len);
5118 }
5119 break;
5120 case VDEV_PROP_CAPACITY:
5121 if (literal) {
5122 (void) snprintf(buf, len, "%llu",
5123 (u_longlong_t)intval);
5124 } else {
5125 (void) snprintf(buf, len, "%llu%%",
5126 (u_longlong_t)intval);
5127 }
5128 break;
5129 case VDEV_PROP_FRAGMENTATION:
5130 if (intval == UINT64_MAX) {
5131 (void) strlcpy(buf, "-", len);
5132 } else {
5133 (void) snprintf(buf, len, "%llu%%",
5134 (u_longlong_t)intval);
5135 }
5136 break;
5137 case VDEV_PROP_STATE:
5138 if (literal) {
5139 (void) snprintf(buf, len, "%llu",
5140 (u_longlong_t)intval);
5141 } else {
5142 (void) strlcpy(buf, zpool_state_to_name(intval,
5143 VDEV_AUX_NONE), len);
5144 }
5145 break;
5146 default:
5147 (void) snprintf(buf, len, "%llu",
5148 (u_longlong_t)intval);
5149 }
5150 break;
5151
5152 case PROP_TYPE_INDEX:
5153 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
5154 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5155 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
2a673e76
AJ
5156 } else {
5157 src = ZPROP_SRC_DEFAULT;
5158 intval = vdev_prop_default_numeric(prop);
5159 }
5160 if (vdev_prop_index_to_string(prop, intval,
5161 (const char **)&strval) != 0)
5162 return (-1);
5163 (void) strlcpy(buf, strval, len);
5164 break;
5165
5166 default:
5167 abort();
5168 }
5169
5170 if (srctype)
5171 *srctype = src;
5172
5173 return (0);
5174}
5175
5176/*
5177 * Get a vdev property value for 'prop_name' and return the value in
5178 * a pre-allocated buffer.
5179 */
5180int
5181zpool_get_vdev_prop(zpool_handle_t *zhp, const char *vdevname, vdev_prop_t prop,
5182 char *prop_name, char *buf, size_t len, zprop_source_t *srctype,
5183 boolean_t literal)
5184{
5185 nvlist_t *reqnvl, *reqprops;
5186 nvlist_t *retprops = NULL;
6fc34371 5187 uint64_t vdev_guid = 0;
2a673e76
AJ
5188 int ret;
5189
5190 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5191 return (ret);
5192
5193 if (nvlist_alloc(&reqnvl, NV_UNIQUE_NAME, 0) != 0)
5194 return (no_memory(zhp->zpool_hdl));
5195 if (nvlist_alloc(&reqprops, NV_UNIQUE_NAME, 0) != 0)
5196 return (no_memory(zhp->zpool_hdl));
5197
5198 fnvlist_add_uint64(reqnvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5199
5200 if (prop != VDEV_PROP_USER) {
5201 /* prop_name overrides prop value */
5202 if (prop_name != NULL)
5203 prop = vdev_name_to_prop(prop_name);
5204 else
5205 prop_name = (char *)vdev_prop_to_name(prop);
5206 assert(prop < VDEV_NUM_PROPS);
5207 }
5208
5209 assert(prop_name != NULL);
5210 if (nvlist_add_uint64(reqprops, prop_name, prop) != 0) {
5211 nvlist_free(reqnvl);
5212 nvlist_free(reqprops);
5213 return (no_memory(zhp->zpool_hdl));
5214 }
5215
5216 fnvlist_add_nvlist(reqnvl, ZPOOL_VDEV_PROPS_GET_PROPS, reqprops);
5217
5218 ret = lzc_get_vdev_prop(zhp->zpool_name, reqnvl, &retprops);
5219
5220 if (ret == 0) {
5221 ret = zpool_get_vdev_prop_value(retprops, prop, prop_name, buf,
5222 len, srctype, literal);
5223 } else {
5224 char errbuf[1024];
5225 (void) snprintf(errbuf, sizeof (errbuf),
5226 dgettext(TEXT_DOMAIN, "cannot get vdev property %s from"
5227 " %s in %s"), prop_name, vdevname, zhp->zpool_name);
5228 (void) zpool_standard_error(zhp->zpool_hdl, ret, errbuf);
5229 }
5230
5231 nvlist_free(reqnvl);
5232 nvlist_free(reqprops);
5233 nvlist_free(retprops);
5234
5235 return (ret);
5236}
5237
5238/*
5239 * Get all vdev properties
5240 */
5241int
5242zpool_get_all_vdev_props(zpool_handle_t *zhp, const char *vdevname,
5243 nvlist_t **outnvl)
5244{
5245 nvlist_t *nvl = NULL;
6fc34371 5246 uint64_t vdev_guid = 0;
2a673e76
AJ
5247 int ret;
5248
5249 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5250 return (ret);
5251
5252 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5253 return (no_memory(zhp->zpool_hdl));
5254
5255 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5256
5257 ret = lzc_get_vdev_prop(zhp->zpool_name, nvl, outnvl);
5258
5259 nvlist_free(nvl);
5260
5261 if (ret) {
5262 char errbuf[1024];
5263 (void) snprintf(errbuf, sizeof (errbuf),
5264 dgettext(TEXT_DOMAIN, "cannot get vdev properties for"
5265 " %s in %s"), vdevname, zhp->zpool_name);
5266 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5267 }
5268
5269 return (ret);
5270}
5271
5272/*
5273 * Set vdev property
5274 */
5275int
5276zpool_set_vdev_prop(zpool_handle_t *zhp, const char *vdevname,
5277 const char *propname, const char *propval)
5278{
5279 int ret;
2a673e76
AJ
5280 nvlist_t *nvl = NULL;
5281 nvlist_t *outnvl = NULL;
5282 nvlist_t *props;
5283 nvlist_t *realprops;
5284 prop_flags_t flags = { 0 };
5285 uint64_t version;
5286 uint64_t vdev_guid;
5287
5288 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5289 return (ret);
5290
2a673e76
AJ
5291 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5292 return (no_memory(zhp->zpool_hdl));
5293 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
5294 return (no_memory(zhp->zpool_hdl));
5295
5296 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_SET_VDEV, vdev_guid);
5297
5298 if (nvlist_add_string(props, propname, propval) != 0) {
5299 nvlist_free(props);
5300 return (no_memory(zhp->zpool_hdl));
5301 }
5302
5303 char errbuf[1024];
5304 (void) snprintf(errbuf, sizeof (errbuf),
5305 dgettext(TEXT_DOMAIN, "cannot set property %s for %s on %s"),
5306 propname, vdevname, zhp->zpool_name);
5307
5308 flags.vdevprop = 1;
5309 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5310 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5311 zhp->zpool_name, props, version, flags, errbuf)) == NULL) {
5312 nvlist_free(props);
5313 nvlist_free(nvl);
5314 return (-1);
5315 }
5316
5317 nvlist_free(props);
5318 props = realprops;
5319
5320 fnvlist_add_nvlist(nvl, ZPOOL_VDEV_PROPS_SET_PROPS, props);
5321
5322 ret = lzc_set_vdev_prop(zhp->zpool_name, nvl, &outnvl);
5323
5324 nvlist_free(props);
5325 nvlist_free(nvl);
5326 nvlist_free(outnvl);
5327
5328 if (ret)
5329 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5330
5331 return (ret);
5332}