]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzfs/libzfs_pool.c
deadlock between spa_errlog_lock and dp_config_rwlock
[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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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 62typedef struct prop_flags {
abb42dc5
BD
63 unsigned int create:1; /* Validate property on creation */
64 unsigned int import:1; /* Validate property on import */
65 unsigned 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;
a926aab9 122 const char *value;
34dc7c2f
BB
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;
a926aab9 131 if ((value = zpool_prop_default_string(prop)) == NULL)
34dc7c2f
BB
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 778 int ret = -1;
d68a2bfa 779 char errbuf[ERRBUFLEN];
34dc7c2f
BB
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));
4ff7a8fa 857 entry->pl_prop = ZPROP_USERPROP;
9ae529ec
CS
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));
4ff7a8fa 901 entry->pl_prop = ZPROP_USERPROP;
9ae529ec
CS
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
4ff7a8fa 914 if (entry->pl_prop != ZPROP_USERPROP &&
34dc7c2f 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)) !=
4ff7a8fa 970 VDEV_PROP_USERPROP)
2a673e76
AJ
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;
cdeb98a1 1371 char errbuf[ERRBUFLEN];
b128c09f 1372 int ret = -1;
34dc7c2f 1373
cdeb98a1 1374 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34dc7c2f
BB
1375 "cannot create '%s'"), pool);
1376
1377 if (!zpool_name_valid(hdl, B_FALSE, pool))
cdeb98a1 1378 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
34dc7c2f 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,
cdeb98a1 1386 SPA_VERSION_1, flags, errbuf)) == 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,
cdeb98a1 1400 fsprops, zoned, NULL, NULL, B_TRUE, errbuf)) == 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));
cdeb98a1 1410 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
cc99f275
DB
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 1419 &wkeydata, &wkeylen) != 0) {
cdeb98a1 1420 zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
b5256303
TC
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"));
cdeb98a1 1468 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
34dc7c2f 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"));
cdeb98a1 1483 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
82f6f6e6 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 }
cdeb98a1 1502 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
34dc7c2f
BB
1503
1504 case ENOSPC:
1505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506 "one or more devices is out of space"));
cdeb98a1 1507 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
34dc7c2f 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"));
cdeb98a1 1515 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
76e1f78d 1516 } else {
cdeb98a1
RM
1517 return (zpool_standard_error(hdl, errno,
1518 errbuf));
76e1f78d
BB
1519 }
1520
34dc7c2f 1521 default:
cdeb98a1 1522 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
1523 }
1524 }
1525
b128c09f 1526create_failed:
34dc7c2f 1527 zcmd_free_nvlists(&zc);
b128c09f
BB
1528 nvlist_free(zc_props);
1529 nvlist_free(zc_fsprops);
b5256303
TC
1530 nvlist_free(hidden_args);
1531 if (wkeydata != NULL)
1532 free(wkeydata);
b128c09f 1533 return (ret);
34dc7c2f
BB
1534}
1535
1536/*
1537 * Destroy the given pool. It is up to the caller to ensure that there are no
1538 * datasets left in the pool.
1539 */
1540int
6f1ffb06 1541zpool_destroy(zpool_handle_t *zhp, const char *log_str)
34dc7c2f 1542{
13fe0198 1543 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
1544 zfs_handle_t *zfp = NULL;
1545 libzfs_handle_t *hdl = zhp->zpool_hdl;
cdeb98a1 1546 char errbuf[ERRBUFLEN];
34dc7c2f
BB
1547
1548 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
572e2857 1549 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
34dc7c2f
BB
1550 return (-1);
1551
34dc7c2f 1552 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
6f1ffb06 1553 zc.zc_history = (uint64_t)(uintptr_t)log_str;
34dc7c2f 1554
572e2857 1555 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
cdeb98a1 1556 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34dc7c2f
BB
1557 "cannot destroy '%s'"), zhp->zpool_name);
1558
1559 if (errno == EROFS) {
1560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1561 "one or more devices is read only"));
cdeb98a1 1562 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
34dc7c2f 1563 } else {
cdeb98a1 1564 (void) zpool_standard_error(hdl, errno, errbuf);
34dc7c2f
BB
1565 }
1566
1567 if (zfp)
1568 zfs_close(zfp);
1569 return (-1);
1570 }
1571
1572 if (zfp) {
1573 remove_mountpoint(zfp);
1574 zfs_close(zfp);
1575 }
1576
1577 return (0);
1578}
1579
d2734cce
SD
1580/*
1581 * Create a checkpoint in the given pool.
1582 */
1583int
1584zpool_checkpoint(zpool_handle_t *zhp)
1585{
1586 libzfs_handle_t *hdl = zhp->zpool_hdl;
cdeb98a1 1587 char errbuf[ERRBUFLEN];
d2734cce
SD
1588 int error;
1589
1590 error = lzc_pool_checkpoint(zhp->zpool_name);
1591 if (error != 0) {
cdeb98a1 1592 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
d2734cce 1593 "cannot checkpoint '%s'"), zhp->zpool_name);
cdeb98a1 1594 (void) zpool_standard_error(hdl, error, errbuf);
d2734cce
SD
1595 return (-1);
1596 }
1597
1598 return (0);
1599}
1600
1601/*
1602 * Discard the checkpoint from the given pool.
1603 */
1604int
1605zpool_discard_checkpoint(zpool_handle_t *zhp)
1606{
1607 libzfs_handle_t *hdl = zhp->zpool_hdl;
cdeb98a1 1608 char errbuf[ERRBUFLEN];
d2734cce
SD
1609 int error;
1610
1611 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1612 if (error != 0) {
cdeb98a1 1613 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
d2734cce 1614 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
cdeb98a1 1615 (void) zpool_standard_error(hdl, error, errbuf);
d2734cce
SD
1616 return (-1);
1617 }
1618
1619 return (0);
1620}
1621
34dc7c2f
BB
1622/*
1623 * Add the given vdevs to the pool. The caller must have already performed the
1624 * necessary verification to ensure that the vdev specification is well-formed.
1625 */
1626int
1627zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1628{
13fe0198 1629 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
1630 int ret;
1631 libzfs_handle_t *hdl = zhp->zpool_hdl;
cdeb98a1 1632 char errbuf[ERRBUFLEN];
34dc7c2f
BB
1633 nvlist_t **spares, **l2cache;
1634 uint_t nspares, nl2cache;
1635
cdeb98a1 1636 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34dc7c2f
BB
1637 "cannot add to '%s'"), zhp->zpool_name);
1638
1639 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1640 SPA_VERSION_SPARES &&
1641 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1642 &spares, &nspares) == 0) {
1643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1644 "upgraded to add hot spares"));
cdeb98a1 1645 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
34dc7c2f
BB
1646 }
1647
1648 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1649 SPA_VERSION_L2CACHE &&
1650 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1651 &l2cache, &nl2cache) == 0) {
1652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1653 "upgraded to add cache devices"));
cdeb98a1 1654 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
34dc7c2f
BB
1655 }
1656
18dbf5c8 1657 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
34dc7c2f
BB
1658 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1659
572e2857 1660 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
34dc7c2f
BB
1661 switch (errno) {
1662 case EBUSY:
1663 /*
1664 * This can happen if the user has specified the same
1665 * device multiple times. We can't reliably detect this
1666 * until we try to add it and see we already have a
1667 * label.
1668 */
1669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1670 "one or more vdevs refer to the same device"));
cdeb98a1 1671 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
34dc7c2f
BB
1672 break;
1673
a1d477c2 1674 case EINVAL:
76e1f78d
BB
1675
1676 if (zpool_has_draid_vdev(nvroot) &&
1677 zfeature_lookup_name("draid", NULL) != 0) {
1678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1679 "dRAID vdevs are unsupported by the "
1680 "kernel"));
1681 } else {
1682 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1683 "invalid config; a pool with removing/"
1684 "removed vdevs does not support adding "
1685 "raidz or dRAID vdevs"));
1686 }
1687
cdeb98a1 1688 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
a1d477c2
MA
1689 break;
1690
34dc7c2f
BB
1691 case EOVERFLOW:
1692 /*
78595377 1693 * This occurs when one of the devices is below
34dc7c2f
BB
1694 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1695 * device was the problem device since there's no
1696 * reliable way to determine device size from userland.
1697 */
1698 {
1699 char buf[64];
1700
e7fbeb60 1701 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1702 sizeof (buf));
34dc7c2f
BB
1703
1704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1705 "device is less than the minimum "
1706 "size (%s)"), buf);
1707 }
cdeb98a1 1708 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
34dc7c2f
BB
1709 break;
1710
1711 case ENOTSUP:
1712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1713 "pool must be upgraded to add these vdevs"));
cdeb98a1 1714 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
34dc7c2f
BB
1715 break;
1716
34dc7c2f 1717 default:
cdeb98a1 1718 (void) zpool_standard_error(hdl, errno, errbuf);
34dc7c2f
BB
1719 }
1720
1721 ret = -1;
1722 } else {
1723 ret = 0;
1724 }
1725
1726 zcmd_free_nvlists(&zc);
1727
1728 return (ret);
1729}
1730
1731/*
1732 * Exports the pool from the system. The caller must ensure that there are no
1733 * mounted datasets in the pool.
1734 */
6f1ffb06
MA
1735static int
1736zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1737 const char *log_str)
34dc7c2f 1738{
13fe0198 1739 zfs_cmd_t zc = {"\0"};
b128c09f 1740
34dc7c2f 1741 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 1742 zc.zc_cookie = force;
fb5f0bc8 1743 zc.zc_guid = hardforce;
6f1ffb06 1744 zc.zc_history = (uint64_t)(uintptr_t)log_str;
b128c09f
BB
1745
1746 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1747 switch (errno) {
1748 case EXDEV:
1749 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1750 "use '-f' to override the following errors:\n"
1751 "'%s' has an active shared spare which could be"
1752 " used by other pools once '%s' is exported."),
1753 zhp->zpool_name, zhp->zpool_name);
f00f4690
AZ
1754 return (zfs_error_fmt(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1755 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1756 zhp->zpool_name));
b128c09f
BB
1757 default:
1758 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
f00f4690
AZ
1759 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1760 zhp->zpool_name));
b128c09f
BB
1761 }
1762 }
34dc7c2f 1763
34dc7c2f
BB
1764 return (0);
1765}
1766
fb5f0bc8 1767int
6f1ffb06 1768zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
fb5f0bc8 1769{
6f1ffb06 1770 return (zpool_export_common(zhp, force, B_FALSE, log_str));
fb5f0bc8
BB
1771}
1772
1773int
6f1ffb06 1774zpool_export_force(zpool_handle_t *zhp, const char *log_str)
fb5f0bc8 1775{
6f1ffb06 1776 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
fb5f0bc8
BB
1777}
1778
428870ff
BB
1779static void
1780zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
572e2857 1781 nvlist_t *config)
428870ff 1782{
572e2857 1783 nvlist_t *nv = NULL;
428870ff
BB
1784 uint64_t rewindto;
1785 int64_t loss = -1;
1786 struct tm t;
1787 char timestr[128];
1788
572e2857
BB
1789 if (!hdl->libzfs_printerr || config == NULL)
1790 return;
1791
9ae529ec
CS
1792 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1793 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
428870ff 1794 return;
9ae529ec 1795 }
428870ff 1796
572e2857 1797 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
428870ff 1798 return;
572e2857 1799 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
428870ff
BB
1800
1801 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
b8864a23 1802 strftime(timestr, 128, "%c", &t) != 0) {
428870ff
BB
1803 if (dryrun) {
1804 (void) printf(dgettext(TEXT_DOMAIN,
1805 "Would be able to return %s "
1806 "to its state as of %s.\n"),
1807 name, timestr);
1808 } else {
1809 (void) printf(dgettext(TEXT_DOMAIN,
1810 "Pool %s returned to its state as of %s.\n"),
1811 name, timestr);
1812 }
1813 if (loss > 120) {
1814 (void) printf(dgettext(TEXT_DOMAIN,
1815 "%s approximately %lld "),
1816 dryrun ? "Would discard" : "Discarded",
b8864a23 1817 ((longlong_t)loss + 30) / 60);
428870ff
BB
1818 (void) printf(dgettext(TEXT_DOMAIN,
1819 "minutes of transactions.\n"));
1820 } else if (loss > 0) {
1821 (void) printf(dgettext(TEXT_DOMAIN,
1822 "%s approximately %lld "),
b8864a23
BB
1823 dryrun ? "Would discard" : "Discarded",
1824 (longlong_t)loss);
428870ff
BB
1825 (void) printf(dgettext(TEXT_DOMAIN,
1826 "seconds of transactions.\n"));
1827 }
1828 }
1829}
1830
1831void
1832zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1833 nvlist_t *config)
1834{
572e2857 1835 nvlist_t *nv = NULL;
428870ff
BB
1836 int64_t loss = -1;
1837 uint64_t edata = UINT64_MAX;
1838 uint64_t rewindto;
1839 struct tm t;
1840 char timestr[128];
1841
1842 if (!hdl->libzfs_printerr)
1843 return;
1844
1845 if (reason >= 0)
1846 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1847 else
1848 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1849
1850 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
572e2857 1851 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
9ae529ec 1852 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
572e2857 1853 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
428870ff
BB
1854 goto no_info;
1855
572e2857
BB
1856 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1857 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
428870ff
BB
1858 &edata);
1859
1860 (void) printf(dgettext(TEXT_DOMAIN,
1861 "Recovery is possible, but will result in some data loss.\n"));
1862
1863 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
b8864a23 1864 strftime(timestr, 128, "%c", &t) != 0) {
428870ff
BB
1865 (void) printf(dgettext(TEXT_DOMAIN,
1866 "\tReturning the pool to its state as of %s\n"
1867 "\tshould correct the problem. "),
1868 timestr);
1869 } else {
1870 (void) printf(dgettext(TEXT_DOMAIN,
1871 "\tReverting the pool to an earlier state "
1872 "should correct the problem.\n\t"));
1873 }
1874
1875 if (loss > 120) {
1876 (void) printf(dgettext(TEXT_DOMAIN,
1877 "Approximately %lld minutes of data\n"
b8864a23
BB
1878 "\tmust be discarded, irreversibly. "),
1879 ((longlong_t)loss + 30) / 60);
428870ff
BB
1880 } else if (loss > 0) {
1881 (void) printf(dgettext(TEXT_DOMAIN,
1882 "Approximately %lld seconds of data\n"
b8864a23
BB
1883 "\tmust be discarded, irreversibly. "),
1884 (longlong_t)loss);
428870ff
BB
1885 }
1886 if (edata != 0 && edata != UINT64_MAX) {
1887 if (edata == 1) {
1888 (void) printf(dgettext(TEXT_DOMAIN,
1889 "After rewind, at least\n"
1890 "\tone persistent user-data error will remain. "));
1891 } else {
1892 (void) printf(dgettext(TEXT_DOMAIN,
1893 "After rewind, several\n"
1894 "\tpersistent user-data errors will remain. "));
1895 }
1896 }
1897 (void) printf(dgettext(TEXT_DOMAIN,
1898 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1899 reason >= 0 ? "clear" : "import", name);
1900
1901 (void) printf(dgettext(TEXT_DOMAIN,
1902 "A scrub of the pool\n"
1903 "\tis strongly recommended after recovery.\n"));
1904 return;
1905
1906no_info:
1907 (void) printf(dgettext(TEXT_DOMAIN,
1908 "Destroy and re-create the pool from\n\ta backup source.\n"));
1909}
1910
34dc7c2f
BB
1911/*
1912 * zpool_import() is a contracted interface. Should be kept the same
1913 * if possible.
1914 *
1915 * Applications should use zpool_import_props() to import a pool with
1916 * new properties value to be set.
1917 */
1918int
1919zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1920 char *altroot)
1921{
1922 nvlist_t *props = NULL;
1923 int ret;
1924
1925 if (altroot != NULL) {
1926 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1927 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1928 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1929 newname));
1930 }
1931
1932 if (nvlist_add_string(props,
fb5f0bc8
BB
1933 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1934 nvlist_add_string(props,
1935 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
34dc7c2f
BB
1936 nvlist_free(props);
1937 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1938 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1939 newname));
1940 }
1941 }
1942
572e2857
BB
1943 ret = zpool_import_props(hdl, config, newname, props,
1944 ZFS_IMPORT_NORMAL);
8a5fc748 1945 nvlist_free(props);
34dc7c2f
BB
1946 return (ret);
1947}
1948
572e2857
BB
1949static void
1950print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1951 int indent)
1952{
1953 nvlist_t **child;
1954 uint_t c, children;
1955 char *vname;
1956 uint64_t is_log = 0;
1957
1958 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1959 &is_log);
1960
1961 if (name != NULL)
1962 (void) printf("\t%*s%s%s\n", indent, "", name,
1963 is_log ? " [log]" : "");
1964
1965 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1966 &child, &children) != 0)
1967 return;
1968
1969 for (c = 0; c < children; c++) {
d2f3e292 1970 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
572e2857
BB
1971 print_vdev_tree(hdl, vname, child[c], indent + 2);
1972 free(vname);
1973 }
1974}
1975
9ae529ec
CS
1976void
1977zpool_print_unsup_feat(nvlist_t *config)
1978{
1979 nvlist_t *nvinfo, *unsup_feat;
9ae529ec 1980
8bb9ecf4
RM
1981 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
1982 unsup_feat = fnvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT);
9ae529ec 1983
8bb9ecf4
RM
1984 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL);
1985 nvp != NULL; nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1986 char *desc = fnvpair_value_string(nvp);
9ae529ec
CS
1987 if (strlen(desc) > 0)
1988 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1989 else
1990 (void) printf("\t%s\n", nvpair_name(nvp));
1991 }
1992}
1993
34dc7c2f
BB
1994/*
1995 * Import the given pool using the known configuration and a list of
1996 * properties to be set. The configuration should have come from
1997 * zpool_find_import(). The 'newname' parameters control whether the pool
1998 * is imported with a different name.
1999 */
2000int
2001zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
572e2857 2002 nvlist_t *props, int flags)
34dc7c2f 2003{
13fe0198 2004 zfs_cmd_t zc = {"\0"};
8a393be3 2005 zpool_load_policy_t policy;
572e2857
BB
2006 nvlist_t *nv = NULL;
2007 nvlist_t *nvinfo = NULL;
2008 nvlist_t *missing = NULL;
0f7a0cc7 2009 const char *thename;
34dc7c2f
BB
2010 char *origname;
2011 int ret;
572e2857 2012 int error = 0;
d68a2bfa 2013 char errbuf[ERRBUFLEN];
34dc7c2f 2014
8bb9ecf4 2015 origname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
34dc7c2f
BB
2016
2017 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2018 "cannot import pool '%s'"), origname);
2019
2020 if (newname != NULL) {
2021 if (!zpool_name_valid(hdl, B_FALSE, newname))
2022 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2023 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2024 newname));
0f7a0cc7 2025 thename = newname;
34dc7c2f
BB
2026 } else {
2027 thename = origname;
2028 }
2029
0fdd8d64 2030 if (props != NULL) {
34dc7c2f 2031 uint64_t version;
572e2857 2032 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
34dc7c2f 2033
8bb9ecf4 2034 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
34dc7c2f 2035
b128c09f 2036 if ((props = zpool_valid_proplist(hdl, origname,
0fdd8d64 2037 props, version, flags, errbuf)) == NULL)
34dc7c2f 2038 return (-1);
18dbf5c8 2039 zcmd_write_src_nvlist(hdl, &zc, props);
0fdd8d64 2040 nvlist_free(props);
34dc7c2f
BB
2041 }
2042
2043 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
2044
8bb9ecf4 2045 zc.zc_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
34dc7c2f 2046
18dbf5c8
AZ
2047 zcmd_write_conf_nvlist(hdl, &zc, config);
2048 zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2);
34dc7c2f 2049
572e2857
BB
2050 zc.zc_cookie = flags;
2051 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
18dbf5c8
AZ
2052 errno == ENOMEM)
2053 zcmd_expand_dst_nvlist(hdl, &zc);
572e2857
BB
2054 if (ret != 0)
2055 error = errno;
2056
2057 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
0fdd8d64
MT
2058
2059 zcmd_free_nvlists(&zc);
2060
8a393be3 2061 zpool_get_load_policy(config, &policy);
572e2857
BB
2062
2063 if (error) {
34dc7c2f 2064 char desc[1024];
379ca9cf 2065 char aux[256];
428870ff 2066
428870ff
BB
2067 /*
2068 * Dry-run failed, but we print out what success
2069 * looks like if we found a best txg
2070 */
8a393be3 2071 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
428870ff 2072 zpool_rewind_exclaim(hdl, newname ? origname : thename,
572e2857
BB
2073 B_TRUE, nv);
2074 nvlist_free(nv);
428870ff
BB
2075 return (-1);
2076 }
2077
34dc7c2f
BB
2078 if (newname == NULL)
2079 (void) snprintf(desc, sizeof (desc),
2080 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2081 thename);
2082 else
2083 (void) snprintf(desc, sizeof (desc),
2084 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
2085 origname, thename);
2086
572e2857 2087 switch (error) {
34dc7c2f 2088 case ENOTSUP:
9ae529ec
CS
2089 if (nv != NULL && nvlist_lookup_nvlist(nv,
2090 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2091 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
2092 (void) printf(dgettext(TEXT_DOMAIN, "This "
2093 "pool uses the following feature(s) not "
2094 "supported by this system:\n"));
2095 zpool_print_unsup_feat(nv);
2096 if (nvlist_exists(nvinfo,
2097 ZPOOL_CONFIG_CAN_RDONLY)) {
2098 (void) printf(dgettext(TEXT_DOMAIN,
2099 "All unsupported features are only "
2100 "required for writing to the pool."
2101 "\nThe pool can be imported using "
2102 "'-o readonly=on'.\n"));
2103 }
2104 }
34dc7c2f
BB
2105 /*
2106 * Unsupported version.
2107 */
2108 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
2109 break;
2110
379ca9cf
OF
2111 case EREMOTEIO:
2112 if (nv != NULL && nvlist_lookup_nvlist(nv,
2113 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
a926aab9 2114 const char *hostname = "<unknown>";
379ca9cf
OF
2115 uint64_t hostid = 0;
2116 mmp_state_t mmp_state;
2117
2118 mmp_state = fnvlist_lookup_uint64(nvinfo,
2119 ZPOOL_CONFIG_MMP_STATE);
2120
2121 if (nvlist_exists(nvinfo,
2122 ZPOOL_CONFIG_MMP_HOSTNAME))
2123 hostname = fnvlist_lookup_string(nvinfo,
2124 ZPOOL_CONFIG_MMP_HOSTNAME);
2125
2126 if (nvlist_exists(nvinfo,
2127 ZPOOL_CONFIG_MMP_HOSTID))
2128 hostid = fnvlist_lookup_uint64(nvinfo,
2129 ZPOOL_CONFIG_MMP_HOSTID);
2130
2131 if (mmp_state == MMP_STATE_ACTIVE) {
2132 (void) snprintf(aux, sizeof (aux),
2133 dgettext(TEXT_DOMAIN, "pool is imp"
2134 "orted on host '%s' (hostid=%lx).\n"
2135 "Export the pool on the other "
2136 "system, then run 'zpool import'."),
2137 hostname, (unsigned long) hostid);
2138 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2139 (void) snprintf(aux, sizeof (aux),
2140 dgettext(TEXT_DOMAIN, "pool has "
2141 "the multihost property on and "
2142 "the\nsystem's hostid is not set. "
2143 "Set a unique system hostid with "
b9373170 2144 "the zgenhostid(8) command.\n"));
379ca9cf
OF
2145 }
2146
f00f4690 2147 (void) zfs_error_aux(hdl, "%s", aux);
379ca9cf
OF
2148 }
2149 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2150 break;
2151
34dc7c2f
BB
2152 case EINVAL:
2153 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2154 break;
2155
428870ff
BB
2156 case EROFS:
2157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2158 "one or more devices is read only"));
2159 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2160 break;
2161
572e2857
BB
2162 case ENXIO:
2163 if (nv && nvlist_lookup_nvlist(nv,
2164 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2165 nvlist_lookup_nvlist(nvinfo,
2166 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2167 (void) printf(dgettext(TEXT_DOMAIN,
6cb8e530
PZ
2168 "The devices below are missing or "
2169 "corrupted, use '-m' to import the pool "
2170 "anyway:\n"));
572e2857
BB
2171 print_vdev_tree(hdl, NULL, missing, 2);
2172 (void) printf("\n");
2173 }
2174 (void) zpool_standard_error(hdl, error, desc);
2175 break;
2176
2177 case EEXIST:
2178 (void) zpool_standard_error(hdl, error, desc);
2179 break;
2180
abe5b8fb
BB
2181 case EBUSY:
2182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2183 "one or more devices are already in use\n"));
2184 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2185 break;
d1d19c78
PD
2186 case ENAMETOOLONG:
2187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2188 "new name of at least one dataset is longer than "
2189 "the maximum allowable length"));
2190 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2191 break;
34dc7c2f 2192 default:
572e2857 2193 (void) zpool_standard_error(hdl, error, desc);
428870ff 2194 zpool_explain_recover(hdl,
572e2857 2195 newname ? origname : thename, -error, nv);
428870ff 2196 break;
34dc7c2f
BB
2197 }
2198
572e2857 2199 nvlist_free(nv);
34dc7c2f
BB
2200 ret = -1;
2201 } else {
2202 zpool_handle_t *zhp;
2203
2204 /*
2205 * This should never fail, but play it safe anyway.
2206 */
428870ff 2207 if (zpool_open_silent(hdl, thename, &zhp) != 0)
34dc7c2f 2208 ret = -1;
428870ff 2209 else if (zhp != NULL)
34dc7c2f 2210 zpool_close(zhp);
8a393be3 2211 if (policy.zlp_rewind &
428870ff
BB
2212 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2213 zpool_rewind_exclaim(hdl, newname ? origname : thename,
8a393be3 2214 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
34dc7c2f 2215 }
572e2857 2216 nvlist_free(nv);
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 {
cdeb98a1 2519 char errbuf[ERRBUFLEN];
50ff6327 2520
cdeb98a1 2521 (void) snprintf(errbuf, sizeof (errbuf),
50ff6327 2522 dgettext(TEXT_DOMAIN, "operation failed"));
cdeb98a1 2523 zpool_standard_error(zhp->zpool_hdl, err, errbuf);
50ff6327
JG
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"};
cdeb98a1 2548 char errbuf[ERRBUFLEN];
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 2570 if (cmd == POOL_SCRUB_PAUSE) {
cdeb98a1
RM
2571 (void) snprintf(errbuf, sizeof (errbuf),
2572 dgettext(TEXT_DOMAIN, "cannot pause scrubbing %s"),
2573 zc.zc_name);
0ea05c64
AP
2574 } else {
2575 assert(cmd == POOL_SCRUB_NORMAL);
cdeb98a1
RM
2576 (void) snprintf(errbuf, sizeof (errbuf),
2577 dgettext(TEXT_DOMAIN, "cannot scrub %s"),
2578 zc.zc_name);
0ea05c64 2579 }
fa241660
TC
2580 } else if (func == POOL_SCAN_RESILVER) {
2581 assert(cmd == POOL_SCRUB_NORMAL);
cdeb98a1 2582 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
fa241660 2583 "cannot restart resilver on %s"), zc.zc_name);
428870ff 2584 } else if (func == POOL_SCAN_NONE) {
cdeb98a1
RM
2585 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2586 "cannot cancel scrubbing %s"), zc.zc_name);
428870ff
BB
2587 } else {
2588 assert(!"unexpected result");
2589 }
34dc7c2f 2590
0ea05c64 2591 if (err == EBUSY) {
428870ff
BB
2592 nvlist_t *nvroot;
2593 pool_scan_stat_t *ps = NULL;
2594 uint_t psc;
2595
8bb9ecf4
RM
2596 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2597 ZPOOL_CONFIG_VDEV_TREE);
428870ff
BB
2598 (void) nvlist_lookup_uint64_array(nvroot,
2599 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
9a49d3f3
BB
2600 if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2601 ps->pss_state == DSS_SCANNING) {
0ea05c64 2602 if (cmd == POOL_SCRUB_PAUSE)
cdeb98a1
RM
2603 return (zfs_error(hdl, EZFS_SCRUB_PAUSED,
2604 errbuf));
0ea05c64 2605 else
cdeb98a1 2606 return (zfs_error(hdl, EZFS_SCRUBBING, errbuf));
0ea05c64 2607 } else {
cdeb98a1 2608 return (zfs_error(hdl, EZFS_RESILVERING, errbuf));
0ea05c64
AP
2609 }
2610 } else if (err == ENOENT) {
cdeb98a1 2611 return (zfs_error(hdl, EZFS_NO_SCRUB, errbuf));
fa241660 2612 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
cdeb98a1 2613 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, errbuf));
428870ff 2614 } else {
cdeb98a1 2615 return (zpool_standard_error(hdl, err, errbuf));
428870ff
BB
2616 }
2617}
2618
34dc7c2f 2619/*
9babb374
BB
2620 * Find a vdev that matches the search criteria specified. We use the
2621 * the nvpair name to determine how we should look for the device.
34dc7c2f
BB
2622 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2623 * spare; but FALSE if its an INUSE spare.
2624 */
2625static nvlist_t *
9babb374
BB
2626vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2627 boolean_t *l2cache, boolean_t *log)
34dc7c2f
BB
2628{
2629 uint_t c, children;
2630 nvlist_t **child;
34dc7c2f 2631 nvlist_t *ret;
b128c09f 2632 uint64_t is_log;
9babb374
BB
2633 char *srchkey;
2634 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2635
2636 /* Nothing to look for */
2637 if (search == NULL || pair == NULL)
2638 return (NULL);
2639
2640 /* Obtain the key we will use to search */
2641 srchkey = nvpair_name(pair);
2642
2643 switch (nvpair_type(pair)) {
572e2857 2644 case DATA_TYPE_UINT64:
9babb374 2645 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
8bb9ecf4
RM
2646 uint64_t srchval = fnvpair_value_uint64(pair);
2647 uint64_t theguid = fnvlist_lookup_uint64(nv,
2648 ZPOOL_CONFIG_GUID);
572e2857
BB
2649 if (theguid == srchval)
2650 return (nv);
9babb374
BB
2651 }
2652 break;
9babb374
BB
2653
2654 case DATA_TYPE_STRING: {
2655 char *srchval, *val;
2656
8bb9ecf4 2657 srchval = fnvpair_value_string(pair);
9babb374
BB
2658 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2659 break;
34dc7c2f 2660
9babb374 2661 /*
428870ff
BB
2662 * Search for the requested value. Special cases:
2663 *
eac47204
BB
2664 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2665 * "-part1", or "p1". The suffix is hidden from the user,
2666 * but included in the string, so this matches around it.
2667 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2668 * is used to check all possible expanded paths.
428870ff
BB
2669 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2670 *
2671 * Otherwise, all other searches are simple string compares.
9babb374 2672 */
a2c6816c 2673 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
9babb374
BB
2674 uint64_t wholedisk = 0;
2675
2676 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2677 &wholedisk);
eac47204
BB
2678 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2679 return (nv);
428870ff 2680
d953bcbf 2681 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0) {
428870ff
BB
2682 char *type, *idx, *end, *p;
2683 uint64_t id, vdev_id;
2684
2685 /*
2686 * Determine our vdev type, keeping in mind
2687 * that the srchval is composed of a type and
2688 * vdev id pair (i.e. mirror-4).
2689 */
2690 if ((type = strdup(srchval)) == NULL)
2691 return (NULL);
2692
2693 if ((p = strrchr(type, '-')) == NULL) {
2694 free(type);
2695 break;
2696 }
2697 idx = p + 1;
2698 *p = '\0';
2699
2700 /*
2701 * If the types don't match then keep looking.
2702 */
2703 if (strncmp(val, type, strlen(val)) != 0) {
2704 free(type);
2705 break;
2706 }
2707
1574c73b 2708 verify(zpool_vdev_is_interior(type));
428870ff 2709
8bb9ecf4 2710 id = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID);
428870ff
BB
2711 errno = 0;
2712 vdev_id = strtoull(idx, &end, 10);
2713
b85f47ef
MA
2714 /*
2715 * If we are looking for a raidz and a parity is
2716 * specified, make sure it matches.
2717 */
2718 int rzlen = strlen(VDEV_TYPE_RAIDZ);
2719 assert(rzlen == strlen(VDEV_TYPE_DRAID));
2720 int typlen = strlen(type);
2721 if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 ||
2722 strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) &&
2723 typlen != rzlen) {
2724 uint64_t vdev_parity;
2725 int parity = *(type + rzlen) - '0';
2726
2727 if (parity <= 0 || parity > 3 ||
2728 (typlen - rzlen) != 1) {
2729 /*
2730 * Nonsense parity specified, can
2731 * never match
2732 */
2733 free(type);
2734 return (NULL);
2735 }
8bb9ecf4
RM
2736 vdev_parity = fnvlist_lookup_uint64(nv,
2737 ZPOOL_CONFIG_NPARITY);
b85f47ef
MA
2738 if ((int)vdev_parity != parity) {
2739 free(type);
2740 break;
2741 }
2742 }
2743
428870ff
BB
2744 free(type);
2745 if (errno != 0)
2746 return (NULL);
2747
2748 /*
2749 * Now verify that we have the correct vdev id.
2750 */
2751 if (vdev_id == id)
2752 return (nv);
9babb374 2753 }
34dc7c2f 2754
34dc7c2f 2755 /*
9babb374 2756 * Common case
34dc7c2f 2757 */
9babb374 2758 if (strcmp(srchval, val) == 0)
34dc7c2f 2759 return (nv);
9babb374
BB
2760 break;
2761 }
2762
2763 default:
2764 break;
34dc7c2f
BB
2765 }
2766
2767 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2768 &child, &children) != 0)
2769 return (NULL);
2770
b128c09f 2771 for (c = 0; c < children; c++) {
9babb374 2772 if ((ret = vdev_to_nvlist_iter(child[c], search,
b128c09f
BB
2773 avail_spare, l2cache, NULL)) != NULL) {
2774 /*
2775 * The 'is_log' value is only set for the toplevel
2776 * vdev, not the leaf vdevs. So we always lookup the
2777 * log device from the root of the vdev tree (where
2778 * 'log' is non-NULL).
2779 */
2780 if (log != NULL &&
2781 nvlist_lookup_uint64(child[c],
2782 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2783 is_log) {
2784 *log = B_TRUE;
2785 }
34dc7c2f 2786 return (ret);
b128c09f
BB
2787 }
2788 }
34dc7c2f
BB
2789
2790 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2791 &child, &children) == 0) {
2792 for (c = 0; c < children; c++) {
9babb374 2793 if ((ret = vdev_to_nvlist_iter(child[c], search,
b128c09f 2794 avail_spare, l2cache, NULL)) != NULL) {
34dc7c2f
BB
2795 *avail_spare = B_TRUE;
2796 return (ret);
2797 }
2798 }
2799 }
2800
2801 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2802 &child, &children) == 0) {
2803 for (c = 0; c < children; c++) {
9babb374 2804 if ((ret = vdev_to_nvlist_iter(child[c], search,
b128c09f 2805 avail_spare, l2cache, NULL)) != NULL) {
34dc7c2f
BB
2806 *l2cache = B_TRUE;
2807 return (ret);
2808 }
2809 }
2810 }
2811
2812 return (NULL);
2813}
2814
9babb374 2815/*
d441e85d 2816 * Given a physical path or guid, find the associated vdev.
9babb374
BB
2817 */
2818nvlist_t *
2819zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2820 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2821{
2822 nvlist_t *search, *nvroot, *ret;
d441e85d
BB
2823 uint64_t guid;
2824 char *end;
9babb374 2825
8bb9ecf4 2826 search = fnvlist_alloc();
d441e85d
BB
2827
2828 guid = strtoull(ppath, &end, 0);
2829 if (guid != 0 && *end == '\0') {
8bb9ecf4 2830 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
d441e85d 2831 } else {
8bb9ecf4 2832 fnvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath);
d441e85d 2833 }
9babb374 2834
8bb9ecf4
RM
2835 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2836 ZPOOL_CONFIG_VDEV_TREE);
9babb374
BB
2837
2838 *avail_spare = B_FALSE;
572e2857
BB
2839 *l2cache = B_FALSE;
2840 if (log != NULL)
2841 *log = B_FALSE;
9babb374 2842 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
8bb9ecf4 2843 fnvlist_free(search);
9babb374
BB
2844
2845 return (ret);
2846}
2847
428870ff
BB
2848/*
2849 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2850 */
1574c73b 2851static boolean_t
428870ff
BB
2852zpool_vdev_is_interior(const char *name)
2853{
2854 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1574c73b
BB
2855 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2856 strncmp(name,
2857 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
428870ff
BB
2858 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2859 return (B_TRUE);
b2255edc
BB
2860
2861 if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 &&
2862 !zpool_is_draid_spare(name))
2863 return (B_TRUE);
2864
428870ff
BB
2865 return (B_FALSE);
2866}
2867
34dc7c2f
BB
2868nvlist_t *
2869zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
b128c09f 2870 boolean_t *l2cache, boolean_t *log)
34dc7c2f 2871{
34dc7c2f 2872 char *end;
9babb374 2873 nvlist_t *nvroot, *search, *ret;
34dc7c2f
BB
2874 uint64_t guid;
2875
8bb9ecf4 2876 search = fnvlist_alloc();
9babb374 2877
1a5c611a 2878 guid = strtoull(path, &end, 0);
34dc7c2f 2879 if (guid != 0 && *end == '\0') {
8bb9ecf4 2880 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
428870ff 2881 } else if (zpool_vdev_is_interior(path)) {
8bb9ecf4 2882 fnvlist_add_string(search, ZPOOL_CONFIG_TYPE, path);
34dc7c2f 2883 } else {
8bb9ecf4 2884 fnvlist_add_string(search, ZPOOL_CONFIG_PATH, path);
34dc7c2f
BB
2885 }
2886
8bb9ecf4
RM
2887 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2888 ZPOOL_CONFIG_VDEV_TREE);
34dc7c2f
BB
2889
2890 *avail_spare = B_FALSE;
2891 *l2cache = B_FALSE;
b128c09f
BB
2892 if (log != NULL)
2893 *log = B_FALSE;
9babb374 2894 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
8bb9ecf4 2895 fnvlist_free(search);
9babb374
BB
2896
2897 return (ret);
b128c09f
BB
2898}
2899
4a283c7f
TH
2900/*
2901 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2902 *
2903 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2904 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2905 * ignore them.
2906 */
2907static uint64_t
2908zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2909 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2910{
4a283c7f
TH
2911 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2912 nvlist_t *tgt;
2913
2914 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2915 &log)) == NULL)
2916 return (0);
2917
4a283c7f
TH
2918 if (is_spare != NULL)
2919 *is_spare = spare;
2920 if (is_l2cache != NULL)
2921 *is_l2cache = l2cache;
2922 if (is_log != NULL)
2923 *is_log = log;
2924
8bb9ecf4 2925 return (fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID));
4a283c7f
TH
2926}
2927
2928/* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2929uint64_t
2930zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2931{
2932 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2933}
2934
34dc7c2f
BB
2935/*
2936 * Bring the specified vdev online. The 'flags' parameter is a set of the
2937 * ZFS_ONLINE_* flags.
2938 */
2939int
2940zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2941 vdev_state_t *newstate)
2942{
13fe0198 2943 zfs_cmd_t zc = {"\0"};
cdeb98a1 2944 char errbuf[ERRBUFLEN];
34dc7c2f 2945 nvlist_t *tgt;
9babb374 2946 boolean_t avail_spare, l2cache, islog;
34dc7c2f
BB
2947 libzfs_handle_t *hdl = zhp->zpool_hdl;
2948
9babb374 2949 if (flags & ZFS_ONLINE_EXPAND) {
cdeb98a1 2950 (void) snprintf(errbuf, sizeof (errbuf),
9babb374
BB
2951 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2952 } else {
cdeb98a1 2953 (void) snprintf(errbuf, sizeof (errbuf),
9babb374
BB
2954 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2955 }
34dc7c2f
BB
2956
2957 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 2958 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
9babb374 2959 &islog)) == NULL)
cdeb98a1 2960 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
34dc7c2f 2961
8bb9ecf4 2962 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f 2963
428870ff 2964 if (avail_spare)
cdeb98a1 2965 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
34dc7c2f 2966
76bcffb7
RM
2967#ifndef __FreeBSD__
2968 char *pathname;
8198c57b
YP
2969 if ((flags & ZFS_ONLINE_EXPAND ||
2970 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2971 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
9babb374
BB
2972 uint64_t wholedisk = 0;
2973
2974 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2975 &wholedisk);
9babb374
BB
2976
2977 /*
2978 * XXX - L2ARC 1.0 devices can't support expansion.
2979 */
2980 if (l2cache) {
2981 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2982 "cannot expand cache devices"));
cdeb98a1 2983 return (zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf));
9babb374
BB
2984 }
2985
2986 if (wholedisk) {
7608bd0d
ED
2987 const char *fullpath = path;
2988 char buf[MAXPATHLEN];
76bcffb7 2989 int error;
7608bd0d
ED
2990
2991 if (path[0] != '/') {
2992 error = zfs_resolve_shortname(path, buf,
d1d7e268 2993 sizeof (buf));
7608bd0d
ED
2994 if (error != 0)
2995 return (zfs_error(hdl, EZFS_NODEVICE,
cdeb98a1 2996 errbuf));
7608bd0d
ED
2997
2998 fullpath = buf;
2999 }
3000
cdeb98a1 3001 error = zpool_relabel_disk(hdl, fullpath, errbuf);
8adf4864
ED
3002 if (error != 0)
3003 return (error);
9babb374
BB
3004 }
3005 }
76bcffb7 3006#endif
9babb374 3007
34dc7c2f
BB
3008 zc.zc_cookie = VDEV_STATE_ONLINE;
3009 zc.zc_obj = flags;
3010
572e2857 3011 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
428870ff
BB
3012 if (errno == EINVAL) {
3013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
3014 "from this pool into a new one. Use '%s' "
3015 "instead"), "zpool detach");
cdeb98a1 3016 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, errbuf));
428870ff 3017 }
cdeb98a1 3018 return (zpool_standard_error(hdl, errno, errbuf));
428870ff 3019 }
34dc7c2f
BB
3020
3021 *newstate = zc.zc_cookie;
3022 return (0);
3023}
3024
3025/*
3026 * Take the specified vdev offline
3027 */
3028int
3029zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
3030{
13fe0198 3031 zfs_cmd_t zc = {"\0"};
cdeb98a1 3032 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3033 nvlist_t *tgt;
3034 boolean_t avail_spare, l2cache;
3035 libzfs_handle_t *hdl = zhp->zpool_hdl;
3036
cdeb98a1 3037 (void) snprintf(errbuf, sizeof (errbuf),
34dc7c2f
BB
3038 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
3039
3040 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f
BB
3041 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3042 NULL)) == NULL)
cdeb98a1 3043 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
34dc7c2f 3044
8bb9ecf4 3045 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f 3046
428870ff 3047 if (avail_spare)
cdeb98a1 3048 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
34dc7c2f 3049
34dc7c2f
BB
3050 zc.zc_cookie = VDEV_STATE_OFFLINE;
3051 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3052
572e2857 3053 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
34dc7c2f
BB
3054 return (0);
3055
3056 switch (errno) {
3057 case EBUSY:
3058
3059 /*
3060 * There are no other replicas of this device.
3061 */
cdeb98a1 3062 return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf));
34dc7c2f 3063
9babb374
BB
3064 case EEXIST:
3065 /*
3066 * The log device has unplayed logs
3067 */
cdeb98a1 3068 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, errbuf));
9babb374 3069
34dc7c2f 3070 default:
cdeb98a1 3071 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
3072 }
3073}
3074
55c12724
AH
3075/*
3076 * Remove the specified vdev asynchronously from the configuration, so
3077 * that it may come ONLINE if reinserted. This is called from zed on
3078 * Udev remove event.
3079 * Note: We also have a similar function zpool_vdev_remove() that
3080 * removes the vdev from the pool.
3081 */
3082int
3083zpool_vdev_remove_wanted(zpool_handle_t *zhp, const char *path)
3084{
3085 zfs_cmd_t zc = {"\0"};
3086 char errbuf[ERRBUFLEN];
3087 nvlist_t *tgt;
3088 boolean_t avail_spare, l2cache;
3089 libzfs_handle_t *hdl = zhp->zpool_hdl;
3090
3091 (void) snprintf(errbuf, sizeof (errbuf),
3092 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3093
3094 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3095 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3096 NULL)) == NULL)
3097 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3098
3099 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3100
3101 if (avail_spare)
3102 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3103
3104 zc.zc_cookie = VDEV_STATE_REMOVED;
3105
3106 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3107 return (0);
3108
3109 return (zpool_standard_error(hdl, errno, errbuf));
3110}
3111
34dc7c2f
BB
3112/*
3113 * Mark the given vdev faulted.
3114 */
3115int
428870ff 3116zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
34dc7c2f 3117{
13fe0198 3118 zfs_cmd_t zc = {"\0"};
cdeb98a1 3119 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3120 libzfs_handle_t *hdl = zhp->zpool_hdl;
3121
cdeb98a1 3122 (void) snprintf(errbuf, sizeof (errbuf),
d1d7e268 3123 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
34dc7c2f
BB
3124
3125 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3126 zc.zc_guid = guid;
3127 zc.zc_cookie = VDEV_STATE_FAULTED;
428870ff 3128 zc.zc_obj = aux;
34dc7c2f 3129
b834b58a 3130 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
34dc7c2f
BB
3131 return (0);
3132
3133 switch (errno) {
3134 case EBUSY:
3135
3136 /*
3137 * There are no other replicas of this device.
3138 */
cdeb98a1 3139 return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf));
34dc7c2f
BB
3140
3141 default:
cdeb98a1 3142 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
3143 }
3144
3145}
3146
3147/*
3148 * Mark the given vdev degraded.
3149 */
3150int
428870ff 3151zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
34dc7c2f 3152{
13fe0198 3153 zfs_cmd_t zc = {"\0"};
cdeb98a1 3154 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3155 libzfs_handle_t *hdl = zhp->zpool_hdl;
3156
cdeb98a1 3157 (void) snprintf(errbuf, sizeof (errbuf),
d1d7e268 3158 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
34dc7c2f
BB
3159
3160 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3161 zc.zc_guid = guid;
3162 zc.zc_cookie = VDEV_STATE_DEGRADED;
428870ff 3163 zc.zc_obj = aux;
34dc7c2f 3164
b834b58a 3165 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
34dc7c2f
BB
3166 return (0);
3167
cdeb98a1 3168 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
3169}
3170
3171/*
3172 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3173 * a hot spare.
3174 */
3175static boolean_t
3176is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3177{
3178 nvlist_t **child;
3179 uint_t c, children;
34dc7c2f
BB
3180
3181 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3182 &children) == 0) {
8bb9ecf4 3183 char *type = fnvlist_lookup_string(search, ZPOOL_CONFIG_TYPE);
b2255edc
BB
3184 if ((strcmp(type, VDEV_TYPE_SPARE) == 0 ||
3185 strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) &&
34dc7c2f
BB
3186 children == 2 && child[which] == tgt)
3187 return (B_TRUE);
3188
3189 for (c = 0; c < children; c++)
3190 if (is_replacing_spare(child[c], tgt, which))
3191 return (B_TRUE);
3192 }
3193
3194 return (B_FALSE);
3195}
3196
3197/*
3198 * Attach new_disk (fully described by nvroot) to old_disk.
3199 * If 'replacing' is specified, the new disk will replace the old one.
3200 */
3201int
9a49d3f3
BB
3202zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3203 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
34dc7c2f 3204{
13fe0198 3205 zfs_cmd_t zc = {"\0"};
cdeb98a1 3206 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3207 int ret;
3208 nvlist_t *tgt;
b128c09f
BB
3209 boolean_t avail_spare, l2cache, islog;
3210 uint64_t val;
572e2857 3211 char *newname;
34dc7c2f
BB
3212 nvlist_t **child;
3213 uint_t children;
3214 nvlist_t *config_root;
3215 libzfs_handle_t *hdl = zhp->zpool_hdl;
3216
3217 if (replacing)
cdeb98a1 3218 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34dc7c2f
BB
3219 "cannot replace %s with %s"), old_disk, new_disk);
3220 else
cdeb98a1 3221 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34dc7c2f
BB
3222 "cannot attach %s to %s"), new_disk, old_disk);
3223
3224 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3225 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
a1d477c2 3226 &islog)) == NULL)
cdeb98a1 3227 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
34dc7c2f
BB
3228
3229 if (avail_spare)
cdeb98a1 3230 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
34dc7c2f
BB
3231
3232 if (l2cache)
cdeb98a1 3233 return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf));
34dc7c2f 3234
8bb9ecf4 3235 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f 3236 zc.zc_cookie = replacing;
9a49d3f3
BB
3237 zc.zc_simple = rebuild;
3238
3239 if (rebuild &&
3240 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3242 "the loaded zfs module doesn't support device rebuilds"));
cdeb98a1 3243 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
9a49d3f3 3244 }
34dc7c2f
BB
3245
3246 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3247 &child, &children) != 0 || children != 1) {
3248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3249 "new device must be a single disk"));
cdeb98a1 3250 return (zfs_error(hdl, EZFS_INVALCONFIG, errbuf));
34dc7c2f
BB
3251 }
3252
8bb9ecf4
RM
3253 config_root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3254 ZPOOL_CONFIG_VDEV_TREE);
34dc7c2f 3255
d2f3e292 3256 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
b128c09f
BB
3257 return (-1);
3258
34dc7c2f
BB
3259 /*
3260 * If the target is a hot spare that has been swapped in, we can only
3261 * replace it with another hot spare.
3262 */
3263 if (replacing &&
3264 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
b128c09f
BB
3265 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3266 NULL) == NULL || !avail_spare) &&
3267 is_replacing_spare(config_root, tgt, 1)) {
34dc7c2f
BB
3268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3269 "can only be replaced by another hot spare"));
b128c09f 3270 free(newname);
cdeb98a1 3271 return (zfs_error(hdl, EZFS_BADTARGET, errbuf));
34dc7c2f
BB
3272 }
3273
b128c09f
BB
3274 free(newname);
3275
18dbf5c8 3276 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
34dc7c2f 3277
572e2857 3278 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
34dc7c2f
BB
3279
3280 zcmd_free_nvlists(&zc);
3281
eaa25f1a 3282 if (ret == 0)
34dc7c2f
BB
3283 return (0);
3284
3285 switch (errno) {
3286 case ENOTSUP:
3287 /*
3288 * Can't attach to or replace this type of vdev.
3289 */
3290 if (replacing) {
572e2857
BB
3291 uint64_t version = zpool_get_prop_int(zhp,
3292 ZPOOL_PROP_VERSION, NULL);
3293
9a49d3f3 3294 if (islog) {
34dc7c2f
BB
3295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3296 "cannot replace a log with a spare"));
9a49d3f3
BB
3297 } else if (rebuild) {
3298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b2255edc
BB
3299 "only mirror and dRAID vdevs support "
3300 "sequential reconstruction"));
3301 } else if (zpool_is_draid_spare(new_disk)) {
3302 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3303 "dRAID spares can only replace child "
3304 "devices in their parent's dRAID vdev"));
9a49d3f3 3305 } else if (version >= SPA_VERSION_MULTI_REPLACE) {
572e2857
BB
3306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3307 "already in replacing/spare config; wait "
3308 "for completion or use 'zpool detach'"));
9a49d3f3 3309 } else {
34dc7c2f
BB
3310 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3311 "cannot replace a replacing device"));
9a49d3f3 3312 }
34dc7c2f 3313 } else {
1139e170
RE
3314 char status[64] = {0};
3315 zpool_prop_get_feature(zhp,
3316 "feature@device_rebuild", status, 63);
3317 if (rebuild &&
3318 strncmp(status, ZFS_FEATURE_DISABLED, 64) == 0) {
3319 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3320 "device_rebuild feature must be enabled "
3321 "in order to use sequential "
3322 "reconstruction"));
3323 } else {
3324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3325 "can only attach to mirrors and top-level "
3326 "disks"));
3327 }
34dc7c2f 3328 }
cdeb98a1 3329 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
34dc7c2f
BB
3330 break;
3331
3332 case EINVAL:
3333 /*
3334 * The new device must be a single disk.
3335 */
3336 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3337 "new device must be a single disk"));
cdeb98a1 3338 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
34dc7c2f
BB
3339 break;
3340
3341 case EBUSY:
a1d477c2 3342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
9e052db4 3343 "or device removal is in progress"),
34dc7c2f 3344 new_disk);
cdeb98a1 3345 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
34dc7c2f
BB
3346 break;
3347
3348 case EOVERFLOW:
3349 /*
3350 * The new device is too small.
3351 */
3352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3353 "device is too small"));
cdeb98a1 3354 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
34dc7c2f
BB
3355 break;
3356
3357 case EDOM:
3358 /*
d4aae2a0 3359 * The new device has a different optimal sector size.
34dc7c2f
BB
3360 */
3361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
d4aae2a0
BB
3362 "new device has a different optimal sector size; use the "
3363 "option '-o ashift=N' to override the optimal size"));
cdeb98a1 3364 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
34dc7c2f
BB
3365 break;
3366
3367 case ENAMETOOLONG:
3368 /*
3369 * The resulting top-level vdev spec won't fit in the label.
3370 */
cdeb98a1 3371 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
34dc7c2f
BB
3372 break;
3373
3374 default:
cdeb98a1 3375 (void) zpool_standard_error(hdl, errno, errbuf);
34dc7c2f
BB
3376 }
3377
3378 return (-1);
3379}
3380
3381/*
3382 * Detach the specified device.
3383 */
3384int
3385zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3386{
13fe0198 3387 zfs_cmd_t zc = {"\0"};
cdeb98a1 3388 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3389 nvlist_t *tgt;
3390 boolean_t avail_spare, l2cache;
3391 libzfs_handle_t *hdl = zhp->zpool_hdl;
3392
cdeb98a1 3393 (void) snprintf(errbuf, sizeof (errbuf),
34dc7c2f
BB
3394 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3395
3396 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3397 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
a1d477c2 3398 NULL)) == NULL)
cdeb98a1 3399 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
34dc7c2f
BB
3400
3401 if (avail_spare)
cdeb98a1 3402 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
34dc7c2f
BB
3403
3404 if (l2cache)
cdeb98a1 3405 return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf));
34dc7c2f 3406
8bb9ecf4 3407 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f
BB
3408
3409 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3410 return (0);
3411
3412 switch (errno) {
3413
3414 case ENOTSUP:
3415 /*
3416 * Can't detach from this type of vdev.
3417 */
3418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3419 "applicable to mirror and replacing vdevs"));
cdeb98a1 3420 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
34dc7c2f
BB
3421 break;
3422
3423 case EBUSY:
3424 /*
3425 * There are no other replicas of this device.
3426 */
cdeb98a1 3427 (void) zfs_error(hdl, EZFS_NOREPLICAS, errbuf);
34dc7c2f
BB
3428 break;
3429
3430 default:
cdeb98a1 3431 (void) zpool_standard_error(hdl, errno, errbuf);
34dc7c2f
BB
3432 }
3433
3434 return (-1);
3435}
3436
428870ff
BB
3437/*
3438 * Find a mirror vdev in the source nvlist.
3439 *
3440 * The mchild array contains a list of disks in one of the top-level mirrors
3441 * of the source pool. The schild array contains a list of disks that the
3442 * user specified on the command line. We loop over the mchild array to
3443 * see if any entry in the schild array matches.
3444 *
3445 * If a disk in the mchild array is found in the schild array, we return
3446 * the index of that entry. Otherwise we return -1.
3447 */
3448static int
3449find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3450 nvlist_t **schild, uint_t schildren)
3451{
3452 uint_t mc;
3453
3454 for (mc = 0; mc < mchildren; mc++) {
3455 uint_t sc;
3456 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
d2f3e292 3457 mchild[mc], 0);
428870ff
BB
3458
3459 for (sc = 0; sc < schildren; sc++) {
3460 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
d2f3e292 3461 schild[sc], 0);
428870ff
BB
3462 boolean_t result = (strcmp(mpath, spath) == 0);
3463
3464 free(spath);
3465 if (result) {
3466 free(mpath);
3467 return (mc);
3468 }
3469 }
3470
3471 free(mpath);
3472 }
3473
3474 return (-1);
3475}
3476
3477/*
3478 * Split a mirror pool. If newroot points to null, then a new nvlist
3479 * is generated and it is the responsibility of the caller to free it.
3480 */
3481int
3482zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3483 nvlist_t *props, splitflags_t flags)
3484{
13fe0198 3485 zfs_cmd_t zc = {"\0"};
cdeb98a1 3486 char errbuf[ERRBUFLEN], *bias;
428870ff
BB
3487 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3488 nvlist_t **varray = NULL, *zc_props = NULL;
3489 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3490 libzfs_handle_t *hdl = zhp->zpool_hdl;
7fab6361 3491 uint64_t vers, readonly = B_FALSE;
428870ff
BB
3492 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3493 int retval = 0;
3494
cdeb98a1 3495 (void) snprintf(errbuf, sizeof (errbuf),
428870ff
BB
3496 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3497
3498 if (!zpool_name_valid(hdl, B_FALSE, newname))
cdeb98a1 3499 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
428870ff
BB
3500
3501 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3502 (void) fprintf(stderr, gettext("Internal error: unable to "
3503 "retrieve pool configuration\n"));
3504 return (-1);
3505 }
3506
8bb9ecf4
RM
3507 tree = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
3508 vers = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
428870ff
BB
3509
3510 if (props) {
572e2857 3511 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
428870ff 3512 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
cdeb98a1 3513 props, vers, flags, errbuf)) == NULL)
428870ff 3514 return (-1);
7fab6361 3515 (void) nvlist_lookup_uint64(zc_props,
3516 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3517 if (readonly) {
3518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3519 "property %s can only be set at import time"),
3520 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3521 return (-1);
3522 }
428870ff
BB
3523 }
3524
3525 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3526 &children) != 0) {
3527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3528 "Source pool is missing vdev tree"));
8a5fc748 3529 nvlist_free(zc_props);
428870ff
BB
3530 return (-1);
3531 }
3532
3533 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3534 vcount = 0;
3535
3536 if (*newroot == NULL ||
3537 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3538 &newchild, &newchildren) != 0)
3539 newchildren = 0;
3540
3541 for (c = 0; c < children; c++) {
3542 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
0cb40fa3 3543 boolean_t is_special = B_FALSE, is_dedup = B_FALSE;
428870ff
BB
3544 char *type;
3545 nvlist_t **mchild, *vdev;
3546 uint_t mchildren;
3547 int entry;
3548
3549 /*
3550 * Unlike cache & spares, slogs are stored in the
3551 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3552 */
3553 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3554 &is_log);
3555 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3556 &is_hole);
3557 if (is_log || is_hole) {
3558 /*
3559 * Create a hole vdev and put it in the config.
3560 */
3561 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3562 goto out;
3563 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3564 VDEV_TYPE_HOLE) != 0)
3565 goto out;
3566 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3567 1) != 0)
3568 goto out;
3569 if (lastlog == 0)
3570 lastlog = vcount;
3571 varray[vcount++] = vdev;
3572 continue;
3573 }
3574 lastlog = 0;
8bb9ecf4 3575 type = fnvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE);
1b664952
GA
3576
3577 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
3578 vdev = child[c];
3579 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3580 goto out;
3581 continue;
3582 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
428870ff
BB
3583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3584 "Source pool must be composed only of mirrors\n"));
cdeb98a1 3585 retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
428870ff
BB
3586 goto out;
3587 }
3588
0cb40fa3
AF
3589 if (nvlist_lookup_string(child[c],
3590 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) {
3591 if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
3592 is_special = B_TRUE;
3593 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
3594 is_dedup = B_TRUE;
3595 }
428870ff
BB
3596 verify(nvlist_lookup_nvlist_array(child[c],
3597 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3598
3599 /* find or add an entry for this top-level vdev */
3600 if (newchildren > 0 &&
3601 (entry = find_vdev_entry(zhp, mchild, mchildren,
3602 newchild, newchildren)) >= 0) {
3603 /* We found a disk that the user specified. */
3604 vdev = mchild[entry];
3605 ++found;
3606 } else {
3607 /* User didn't specify a disk for this vdev. */
3608 vdev = mchild[mchildren - 1];
3609 }
3610
3611 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3612 goto out;
0cb40fa3
AF
3613
3614 if (flags.dryrun != 0) {
3615 if (is_dedup == B_TRUE) {
3616 if (nvlist_add_string(varray[vcount - 1],
3617 ZPOOL_CONFIG_ALLOCATION_BIAS,
3618 VDEV_ALLOC_BIAS_DEDUP) != 0)
3619 goto out;
3620 } else if (is_special == B_TRUE) {
3621 if (nvlist_add_string(varray[vcount - 1],
3622 ZPOOL_CONFIG_ALLOCATION_BIAS,
3623 VDEV_ALLOC_BIAS_SPECIAL) != 0)
3624 goto out;
3625 }
3626 }
428870ff
BB
3627 }
3628
3629 /* did we find every disk the user specified? */
3630 if (found != newchildren) {
3631 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3632 "include at most one disk from each mirror"));
cdeb98a1 3633 retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
428870ff
BB
3634 goto out;
3635 }
3636
3637 /* Prepare the nvlist for populating. */
3638 if (*newroot == NULL) {
3639 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3640 goto out;
3641 freelist = B_TRUE;
3642 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3643 VDEV_TYPE_ROOT) != 0)
3644 goto out;
3645 } else {
3646 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3647 }
3648
3649 /* Add all the children we found */
795075e6
PD
3650 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3651 (const nvlist_t **)varray, lastlog == 0 ? vcount : lastlog) != 0)
428870ff
BB
3652 goto out;
3653
3654 /*
3655 * If we're just doing a dry run, exit now with success.
3656 */
3657 if (flags.dryrun) {
3658 memory_err = B_FALSE;
3659 freelist = B_FALSE;
3660 goto out;
3661 }
3662
3663 /* now build up the config list & call the ioctl */
3664 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3665 goto out;
3666
3667 if (nvlist_add_nvlist(newconfig,
3668 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3669 nvlist_add_string(newconfig,
3670 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3671 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3672 goto out;
3673
3674 /*
3675 * The new pool is automatically part of the namespace unless we
3676 * explicitly export it.
3677 */
3678 if (!flags.import)
3679 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3680 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3681 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
18dbf5c8
AZ
3682 zcmd_write_conf_nvlist(hdl, &zc, newconfig);
3683 if (zc_props != NULL)
3684 zcmd_write_src_nvlist(hdl, &zc, zc_props);
428870ff
BB
3685
3686 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
cdeb98a1 3687 retval = zpool_standard_error(hdl, errno, errbuf);
428870ff
BB
3688 goto out;
3689 }
3690
3691 freelist = B_FALSE;
3692 memory_err = B_FALSE;
3693
3694out:
3695 if (varray != NULL) {
3696 int v;
3697
3698 for (v = 0; v < vcount; v++)
3699 nvlist_free(varray[v]);
3700 free(varray);
3701 }
3702 zcmd_free_nvlists(&zc);
8a5fc748
JJS
3703 nvlist_free(zc_props);
3704 nvlist_free(newconfig);
428870ff
BB
3705 if (freelist) {
3706 nvlist_free(*newroot);
3707 *newroot = NULL;
3708 }
3709
3710 if (retval != 0)
3711 return (retval);
3712
3713 if (memory_err)
3714 return (no_memory(hdl));
3715
3716 return (0);
3717}
3718
34dc7c2f 3719/*
a1d477c2 3720 * Remove the given device.
34dc7c2f
BB
3721 */
3722int
3723zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3724{
13fe0198 3725 zfs_cmd_t zc = {"\0"};
cdeb98a1 3726 char errbuf[ERRBUFLEN];
34dc7c2f 3727 nvlist_t *tgt;
428870ff 3728 boolean_t avail_spare, l2cache, islog;
34dc7c2f 3729 libzfs_handle_t *hdl = zhp->zpool_hdl;
428870ff 3730 uint64_t version;
34dc7c2f 3731
cdeb98a1 3732 (void) snprintf(errbuf, sizeof (errbuf),
34dc7c2f
BB
3733 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3734
b2255edc
BB
3735 if (zpool_is_draid_spare(path)) {
3736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3737 "dRAID spares cannot be removed"));
cdeb98a1 3738 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
b2255edc
BB
3739 }
3740
34dc7c2f 3741 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
b128c09f 3742 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
a1d477c2 3743 &islog)) == NULL)
cdeb98a1 3744 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
34dc7c2f 3745
428870ff
BB
3746 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3747 if (islog && version < SPA_VERSION_HOLES) {
3748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
a1d477c2 3749 "pool must be upgraded to support log removal"));
cdeb98a1 3750 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
428870ff
BB
3751 }
3752
a1d477c2
MA
3753 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3754
3755 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3756 return (0);
3757
3758 switch (errno) {
3759
3760 case EINVAL:
3761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3762 "invalid config; all top-level vdevs must "
3763 "have the same sector size and not be raidz."));
cdeb98a1 3764 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
a1d477c2
MA
3765 break;
3766
3767 case EBUSY:
2ffd89fc
PZ
3768 if (islog) {
3769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3770 "Mount encrypted datasets to replay logs."));
3771 } else {
3772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3773 "Pool busy; removal may already be in progress"));
3774 }
cdeb98a1 3775 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
a1d477c2
MA
3776 break;
3777
2ffd89fc
PZ
3778 case EACCES:
3779 if (islog) {
3780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3781 "Mount encrypted datasets to replay logs."));
cdeb98a1 3782 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
2ffd89fc 3783 } else {
cdeb98a1 3784 (void) zpool_standard_error(hdl, errno, errbuf);
2ffd89fc
PZ
3785 }
3786 break;
3787
a1d477c2 3788 default:
cdeb98a1 3789 (void) zpool_standard_error(hdl, errno, errbuf);
a1d477c2
MA
3790 }
3791 return (-1);
3792}
3793
3794int
3795zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3796{
861166b0 3797 zfs_cmd_t zc = {{0}};
cdeb98a1 3798 char errbuf[ERRBUFLEN];
a1d477c2
MA
3799 libzfs_handle_t *hdl = zhp->zpool_hdl;
3800
cdeb98a1 3801 (void) snprintf(errbuf, sizeof (errbuf),
a1d477c2
MA
3802 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3803
a1d477c2
MA
3804 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3805 zc.zc_cookie = 1;
34dc7c2f
BB
3806
3807 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3808 return (0);
3809
cdeb98a1 3810 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
3811}
3812
a1d477c2
MA
3813int
3814zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3815 uint64_t *sizep)
3816{
cdeb98a1 3817 char errbuf[ERRBUFLEN];
a1d477c2
MA
3818 nvlist_t *tgt;
3819 boolean_t avail_spare, l2cache, islog;
3820 libzfs_handle_t *hdl = zhp->zpool_hdl;
3821
cdeb98a1 3822 (void) snprintf(errbuf, sizeof (errbuf),
a1d477c2
MA
3823 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3824 path);
3825
3826 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3827 &islog)) == NULL)
cdeb98a1 3828 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
a1d477c2
MA
3829
3830 if (avail_spare || l2cache || islog) {
3831 *sizep = 0;
3832 return (0);
3833 }
3834
3835 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3836 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3837 "indirect size not available"));
cdeb98a1 3838 return (zfs_error(hdl, EINVAL, errbuf));
a1d477c2
MA
3839 }
3840 return (0);
3841}
3842
34dc7c2f
BB
3843/*
3844 * Clear the errors for the pool, or the particular device if specified.
3845 */
3846int
428870ff 3847zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
34dc7c2f 3848{
13fe0198 3849 zfs_cmd_t zc = {"\0"};
cdeb98a1 3850 char errbuf[ERRBUFLEN];
34dc7c2f 3851 nvlist_t *tgt;
8a393be3 3852 zpool_load_policy_t policy;
34dc7c2f
BB
3853 boolean_t avail_spare, l2cache;
3854 libzfs_handle_t *hdl = zhp->zpool_hdl;
428870ff 3855 nvlist_t *nvi = NULL;
572e2857 3856 int error;
34dc7c2f
BB
3857
3858 if (path)
cdeb98a1 3859 (void) snprintf(errbuf, sizeof (errbuf),
34dc7c2f
BB
3860 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3861 path);
3862 else
cdeb98a1 3863 (void) snprintf(errbuf, sizeof (errbuf),
34dc7c2f
BB
3864 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3865 zhp->zpool_name);
3866
3867 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3868 if (path) {
3869 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
a1d477c2 3870 &l2cache, NULL)) == NULL)
cdeb98a1 3871 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
34dc7c2f
BB
3872
3873 /*
3874 * Don't allow error clearing for hot spares. Do allow
3875 * error clearing for l2cache devices.
3876 */
3877 if (avail_spare)
cdeb98a1 3878 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
34dc7c2f 3879
8bb9ecf4 3880 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
34dc7c2f
BB
3881 }
3882
8a393be3
PZ
3883 zpool_get_load_policy(rewindnvl, &policy);
3884 zc.zc_cookie = policy.zlp_rewind;
428870ff 3885
18dbf5c8
AZ
3886 zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2);
3887 zcmd_write_src_nvlist(hdl, &zc, rewindnvl);
428870ff 3888
572e2857 3889 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
18dbf5c8
AZ
3890 errno == ENOMEM)
3891 zcmd_expand_dst_nvlist(hdl, &zc);
572e2857 3892
8a393be3 3893 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
428870ff 3894 errno != EPERM && errno != EACCES)) {
8a393be3 3895 if (policy.zlp_rewind &
428870ff
BB
3896 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3897 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3898 zpool_rewind_exclaim(hdl, zc.zc_name,
8a393be3 3899 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
428870ff
BB
3900 nvi);
3901 nvlist_free(nvi);
3902 }
3903 zcmd_free_nvlists(&zc);
34dc7c2f 3904 return (0);
428870ff 3905 }
34dc7c2f 3906
428870ff 3907 zcmd_free_nvlists(&zc);
cdeb98a1 3908 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
3909}
3910
3911/*
3912 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3913 */
3914int
3915zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3916{
13fe0198 3917 zfs_cmd_t zc = {"\0"};
cdeb98a1 3918 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3919 libzfs_handle_t *hdl = zhp->zpool_hdl;
3920
cdeb98a1 3921 (void) snprintf(errbuf, sizeof (errbuf),
34dc7c2f 3922 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
d1d7e268 3923 (u_longlong_t)guid);
34dc7c2f
BB
3924
3925 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3926 zc.zc_guid = guid;
428870ff 3927 zc.zc_cookie = ZPOOL_NO_REWIND;
34dc7c2f 3928
b834b58a 3929 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
34dc7c2f
BB
3930 return (0);
3931
cdeb98a1 3932 return (zpool_standard_error(hdl, errno, errbuf));
34dc7c2f
BB
3933}
3934
3541dc6d
GA
3935/*
3936 * Change the GUID for a pool.
3937 */
3938int
3939zpool_reguid(zpool_handle_t *zhp)
3940{
cdeb98a1 3941 char errbuf[ERRBUFLEN];
3541dc6d 3942 libzfs_handle_t *hdl = zhp->zpool_hdl;
13fe0198 3943 zfs_cmd_t zc = {"\0"};
3541dc6d 3944
cdeb98a1 3945 (void) snprintf(errbuf, sizeof (errbuf),
3541dc6d
GA
3946 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3947
3948 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3949 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3950 return (0);
3951
cdeb98a1 3952 return (zpool_standard_error(hdl, errno, errbuf));
3541dc6d
GA
3953}
3954
1bd201e7
CS
3955/*
3956 * Reopen the pool.
3957 */
3958int
d3f2cd7e 3959zpool_reopen_one(zpool_handle_t *zhp, void *data)
1bd201e7 3960{
d3f2cd7e
AB
3961 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3962 const char *pool_name = zpool_get_name(zhp);
3963 boolean_t *scrub_restart = data;
3964 int error;
1bd201e7 3965
d3f2cd7e
AB
3966 error = lzc_reopen(pool_name, *scrub_restart);
3967 if (error) {
3968 return (zpool_standard_error_fmt(hdl, error,
3969 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3970 }
1bd201e7 3971
d3f2cd7e 3972 return (0);
1bd201e7
CS
3973}
3974
bec1067d
AP
3975/* call into libzfs_core to execute the sync IOCTL per pool */
3976int
3977zpool_sync_one(zpool_handle_t *zhp, void *data)
3978{
3979 int ret;
3980 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3981 const char *pool_name = zpool_get_name(zhp);
3982 boolean_t *force = data;
3983 nvlist_t *innvl = fnvlist_alloc();
3984
3985 fnvlist_add_boolean_value(innvl, "force", *force);
3986 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3987 nvlist_free(innvl);
3988 return (zpool_standard_error_fmt(hdl, ret,
3989 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3990 }
3991 nvlist_free(innvl);
3992
3993 return (0);
3994}
3995
858219cc
NB
3996#define PATH_BUF_LEN 64
3997
34dc7c2f
BB
3998/*
3999 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4000 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4001 * We also check if this is a whole disk, in which case we strip off the
4002 * trailing 's0' slice name.
4003 *
4004 * This routine is also responsible for identifying when disks have been
4005 * reconfigured in a new location. The kernel will have opened the device by
4006 * devid, but the path will still refer to the old location. To catch this, we
4007 * first do a path -> devid translation (which is fast for the common case). If
4008 * the devid matches, we're done. If not, we do a reverse devid -> path
4009 * translation and issue the appropriate ioctl() to update the path of the vdev.
4010 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4011 * of these checks.
4012 */
4013char *
428870ff 4014zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
d2f3e292 4015 int name_flags)
34dc7c2f 4016{
5ac80603
AZ
4017 char *type, *tpath;
4018 const char *path;
34dc7c2f 4019 uint64_t value;
858219cc 4020 char buf[PATH_BUF_LEN];
ca3b105c 4021 char tmpbuf[PATH_BUF_LEN * 2];
34dc7c2f 4022
2df9ad1c
GG
4023 /*
4024 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4025 * zpool name that will be displayed to the user.
4026 */
8bb9ecf4 4027 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE);
2df9ad1c
GG
4028 if (zhp != NULL && strcmp(type, "root") == 0)
4029 return (zfs_strdup(hdl, zpool_get_name(zhp)));
4030
ca3b105c 4031 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
d2f3e292 4032 name_flags |= VDEV_NAME_PATH;
ca3b105c 4033 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
d2f3e292 4034 name_flags |= VDEV_NAME_GUID;
ca3b105c 4035 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
d2f3e292
RY
4036 name_flags |= VDEV_NAME_FOLLOW_LINKS;
4037
4038 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4039 name_flags & VDEV_NAME_GUID) {
aecdc706 4040 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
d2f3e292 4041 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
34dc7c2f 4042 path = buf;
5ac80603
AZ
4043 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tpath) == 0) {
4044 path = tpath;
4045
d2f3e292
RY
4046 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4047 char *rp = realpath(path, NULL);
4048 if (rp) {
4049 strlcpy(buf, rp, sizeof (buf));
4050 path = buf;
4051 free(rp);
4052 }
4053 }
4054
d603ed6c
BB
4055 /*
4056 * For a block device only use the name.
4057 */
d2f3e292
RY
4058 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
4059 !(name_flags & VDEV_NAME_PATH)) {
035ebb36 4060 path = zfs_strip_path(path);
d603ed6c 4061 }
34dc7c2f 4062
d603ed6c 4063 /*
b2255edc 4064 * Remove the partition from the path if this is a whole disk.
d603ed6c 4065 */
b2255edc
BB
4066 if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 &&
4067 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
d2f3e292 4068 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
6078881a 4069 return (zfs_strip_partition(path));
34dc7c2f
BB
4070 }
4071 } else {
2df9ad1c 4072 path = type;
34dc7c2f
BB
4073
4074 /*
4075 * If it's a raidz device, we need to stick in the parity level.
4076 */
4077 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
8bb9ecf4 4078 value = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY);
fc24f7c8 4079 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
34dc7c2f 4080 (u_longlong_t)value);
fc24f7c8 4081 path = buf;
34dc7c2f 4082 }
428870ff 4083
b2255edc
BB
4084 /*
4085 * If it's a dRAID device, we add parity, groups, and spares.
4086 */
4087 if (strcmp(path, VDEV_TYPE_DRAID) == 0) {
4088 uint64_t ndata, nparity, nspares;
4089 nvlist_t **child;
4090 uint_t children;
4091
4092 verify(nvlist_lookup_nvlist_array(nv,
4093 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
8bb9ecf4
RM
4094 nparity = fnvlist_lookup_uint64(nv,
4095 ZPOOL_CONFIG_NPARITY);
4096 ndata = fnvlist_lookup_uint64(nv,
4097 ZPOOL_CONFIG_DRAID_NDATA);
4098 nspares = fnvlist_lookup_uint64(nv,
4099 ZPOOL_CONFIG_DRAID_NSPARES);
b2255edc
BB
4100
4101 path = zpool_draid_name(buf, sizeof (buf), ndata,
4102 nparity, nspares, children);
4103 }
4104
428870ff
BB
4105 /*
4106 * We identify each top-level vdev by using a <type-id>
4107 * naming convention.
4108 */
d2f3e292 4109 if (name_flags & VDEV_NAME_TYPE_ID) {
8bb9ecf4
RM
4110 uint64_t id = fnvlist_lookup_uint64(nv,
4111 ZPOOL_CONFIG_ID);
fc24f7c8
MM
4112 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
4113 path, (u_longlong_t)id);
4114 path = tmpbuf;
428870ff 4115 }
34dc7c2f
BB
4116 }
4117
4118 return (zfs_strdup(hdl, path));
4119}
4120
4121static int
fcff0f35 4122zbookmark_mem_compare(const void *a, const void *b)
34dc7c2f 4123{
5dbd68a3 4124 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
34dc7c2f
BB
4125}
4126
4127/*
4128 * Retrieve the persistent error log, uniquify the members, and return to the
4129 * caller.
4130 */
4131int
4132zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4133{
13fe0198 4134 zfs_cmd_t zc = {"\0"};
56a6054d 4135 libzfs_handle_t *hdl = zhp->zpool_hdl;
018f2604
MA
4136 zbookmark_phys_t *buf;
4137 uint64_t buflen = 10000; /* approx. 1MB of RAM */
4138
4139 if (fnvlist_lookup_uint64(zhp->zpool_config,
4140 ZPOOL_CONFIG_ERRCOUNT) == 0)
4141 return (0);
34dc7c2f
BB
4142
4143 /*
018f2604
MA
4144 * Retrieve the raw error list from the kernel. If it doesn't fit,
4145 * allocate a larger buffer and retry.
34dc7c2f 4146 */
34dc7c2f
BB
4147 (void) strcpy(zc.zc_name, zhp->zpool_name);
4148 for (;;) {
018f2604
MA
4149 buf = zfs_alloc(zhp->zpool_hdl,
4150 buflen * sizeof (zbookmark_phys_t));
4151 zc.zc_nvlist_dst = (uintptr_t)buf;
4152 zc.zc_nvlist_dst_size = buflen;
b834b58a 4153 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
34dc7c2f 4154 &zc) != 0) {
018f2604 4155 free(buf);
34dc7c2f 4156 if (errno == ENOMEM) {
018f2604 4157 buflen *= 2;
34dc7c2f 4158 } else {
56a6054d
BB
4159 return (zpool_standard_error_fmt(hdl, errno,
4160 dgettext(TEXT_DOMAIN, "errors: List of "
4161 "errors unavailable")));
34dc7c2f
BB
4162 }
4163 } else {
4164 break;
4165 }
4166 }
4167
4168 /*
4169 * Sort the resulting bookmarks. This is a little confusing due to the
4170 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
78595377 4171 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
34dc7c2f
BB
4172 * _not_ copied as part of the process. So we point the start of our
4173 * array appropriate and decrement the total number of elements.
4174 */
018f2604
MA
4175 zbookmark_phys_t *zb = buf + zc.zc_nvlist_dst_size;
4176 uint64_t zblen = buflen - zc.zc_nvlist_dst_size;
34dc7c2f 4177
018f2604 4178 qsort(zb, zblen, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
34dc7c2f
BB
4179
4180 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4181
4182 /*
4183 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4184 */
018f2604 4185 for (uint64_t i = 0; i < zblen; i++) {
34dc7c2f
BB
4186 nvlist_t *nv;
4187
4188 /* ignoring zb_blkid and zb_level for now */
4189 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4190 zb[i-1].zb_object == zb[i].zb_object)
4191 continue;
4192
4193 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4194 goto nomem;
4195 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4196 zb[i].zb_objset) != 0) {
4197 nvlist_free(nv);
4198 goto nomem;
4199 }
4200 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4201 zb[i].zb_object) != 0) {
4202 nvlist_free(nv);
4203 goto nomem;
4204 }
4205 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4206 nvlist_free(nv);
4207 goto nomem;
4208 }
4209 nvlist_free(nv);
4210 }
4211
018f2604 4212 free(buf);
34dc7c2f
BB
4213 return (0);
4214
4215nomem:
018f2604 4216 free(buf);
34dc7c2f
BB
4217 return (no_memory(zhp->zpool_hdl));
4218}
4219
4220/*
4221 * Upgrade a ZFS pool to the latest on-disk version.
4222 */
4223int
4224zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4225{
13fe0198 4226 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4227 libzfs_handle_t *hdl = zhp->zpool_hdl;
4228
4229 (void) strcpy(zc.zc_name, zhp->zpool_name);
4230 zc.zc_cookie = new_version;
4231
4232 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4233 return (zpool_standard_error_fmt(hdl, errno,
4234 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4235 zhp->zpool_name));
4236 return (0);
4237}
4238
4239void
6f1ffb06 4240zfs_save_arguments(int argc, char **argv, char *string, int len)
34dc7c2f
BB
4241{
4242 int i;
4243
feb04e66 4244 (void) strlcpy(string, zfs_basename(argv[0]), len);
34dc7c2f 4245 for (i = 1; i < argc; i++) {
6f1ffb06
MA
4246 (void) strlcat(string, " ", len);
4247 (void) strlcat(string, argv[i], len);
34dc7c2f
BB
4248 }
4249}
4250
34dc7c2f 4251int
6f1ffb06
MA
4252zpool_log_history(libzfs_handle_t *hdl, const char *message)
4253{
13fe0198 4254 zfs_cmd_t zc = {"\0"};
6f1ffb06 4255 nvlist_t *args;
6f1ffb06
MA
4256
4257 args = fnvlist_alloc();
4258 fnvlist_add_string(args, "message", message);
18dbf5c8
AZ
4259 zcmd_write_src_nvlist(hdl, &zc, args);
4260 int err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
6f1ffb06
MA
4261 nvlist_free(args);
4262 zcmd_free_nvlists(&zc);
4263 return (err);
34dc7c2f
BB
4264}
4265
4266/*
4267 * Perform ioctl to get some command history of a pool.
4268 *
4269 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4270 * logical offset of the history buffer to start reading from.
4271 *
4272 * Upon return, 'off' is the next logical offset to read from and
4273 * 'len' is the actual amount of bytes read into 'buf'.
4274 */
4275static int
4276get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4277{
13fe0198 4278 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4279 libzfs_handle_t *hdl = zhp->zpool_hdl;
4280
4281 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4282
4283 zc.zc_history = (uint64_t)(uintptr_t)buf;
4284 zc.zc_history_len = *len;
4285 zc.zc_history_offset = *off;
4286
b834b58a 4287 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
34dc7c2f
BB
4288 switch (errno) {
4289 case EPERM:
4290 return (zfs_error_fmt(hdl, EZFS_PERM,
4291 dgettext(TEXT_DOMAIN,
4292 "cannot show history for pool '%s'"),
4293 zhp->zpool_name));
4294 case ENOENT:
4295 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4296 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4297 "'%s'"), zhp->zpool_name));
4298 case ENOTSUP:
4299 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4300 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4301 "'%s', pool must be upgraded"), zhp->zpool_name));
4302 default:
4303 return (zpool_standard_error_fmt(hdl, errno,
4304 dgettext(TEXT_DOMAIN,
4305 "cannot get history for '%s'"), zhp->zpool_name));
4306 }
4307 }
4308
4309 *len = zc.zc_history_len;
4310 *off = zc.zc_history_offset;
4311
4312 return (0);
4313}
4314
34dc7c2f
BB
4315/*
4316 * Retrieve the command history of a pool.
4317 */
4318int
7125a109
CC
4319zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4320 boolean_t *eof)
34dc7c2f 4321{
4dced31b 4322 libzfs_handle_t *hdl = zhp->zpool_hdl;
1f6f97f3
MA
4323 char *buf;
4324 int buflen = 128 * 1024;
34dc7c2f
BB
4325 nvlist_t **records = NULL;
4326 uint_t numrecords = 0;
4dced31b 4327 int err = 0, i;
7125a109 4328 uint64_t start = *off;
34dc7c2f 4329
4dced31b
W
4330 buf = zfs_alloc(hdl, buflen);
4331
4332 /* process about 1MiB a time */
7125a109 4333 while (*off - start < 1024 * 1024) {
1f6f97f3 4334 uint64_t bytes_read = buflen;
34dc7c2f
BB
4335 uint64_t leftover;
4336
7125a109 4337 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
34dc7c2f
BB
4338 break;
4339
4340 /* if nothing else was read in, we're at EOF, just return */
7125a109
CC
4341 if (!bytes_read) {
4342 *eof = B_TRUE;
34dc7c2f 4343 break;
7125a109 4344 }
34dc7c2f
BB
4345
4346 if ((err = zpool_history_unpack(buf, bytes_read,
4dced31b
W
4347 &leftover, &records, &numrecords)) != 0) {
4348 zpool_standard_error_fmt(hdl, err,
4349 dgettext(TEXT_DOMAIN,
4350 "cannot get history for '%s'"), zhp->zpool_name);
34dc7c2f 4351 break;
4dced31b 4352 }
7125a109 4353 *off -= leftover;
1f6f97f3
MA
4354 if (leftover == bytes_read) {
4355 /*
4356 * no progress made, because buffer is not big enough
4357 * to hold this record; resize and retry.
4358 */
4359 buflen *= 2;
4360 free(buf);
4dced31b 4361 buf = zfs_alloc(hdl, buflen);
1f6f97f3 4362 }
7125a109 4363 }
34dc7c2f 4364
1f6f97f3
MA
4365 free(buf);
4366
34dc7c2f 4367 if (!err) {
8bb9ecf4
RM
4368 *nvhisp = fnvlist_alloc();
4369 fnvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4370 (const nvlist_t **)records, numrecords);
34dc7c2f
BB
4371 }
4372 for (i = 0; i < numrecords; i++)
4373 nvlist_free(records[i]);
4374 free(records);
4375
4376 return (err);
4377}
4378
26685276 4379/*
9b101a73
BB
4380 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4381 * If there is a new event available 'nvp' will contain a newly allocated
4382 * nvlist and 'dropped' will be set to the number of missed events since
4383 * the last call to this function. When 'nvp' is set to NULL it indicates
4384 * no new events are available. In either case the function returns 0 and
4385 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4386 * function will return a non-zero value. When the function is called in
8c7aa0cf
CD
4387 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4388 * it will not return until a new event is available.
26685276
BB
4389 */
4390int
4391zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
8c7aa0cf 4392 int *dropped, unsigned flags, int zevent_fd)
26685276 4393{
13fe0198 4394 zfs_cmd_t zc = {"\0"};
26685276
BB
4395 int error = 0;
4396
4397 *nvp = NULL;
4398 *dropped = 0;
9b101a73 4399 zc.zc_cleanup_fd = zevent_fd;
26685276 4400
8c7aa0cf 4401 if (flags & ZEVENT_NONBLOCK)
26685276
BB
4402 zc.zc_guid = ZEVENT_NONBLOCK;
4403
18dbf5c8 4404 zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE);
26685276
BB
4405
4406retry:
4407 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4408 switch (errno) {
4409 case ESHUTDOWN:
4410 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4411 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4412 goto out;
4413 case ENOENT:
4414 /* Blocking error case should not occur */
8c7aa0cf 4415 if (!(flags & ZEVENT_NONBLOCK))
26685276
BB
4416 error = zpool_standard_error_fmt(hdl, errno,
4417 dgettext(TEXT_DOMAIN, "cannot get event"));
4418
4419 goto out;
4420 case ENOMEM:
18dbf5c8
AZ
4421 zcmd_expand_dst_nvlist(hdl, &zc);
4422 goto retry;
26685276
BB
4423 default:
4424 error = zpool_standard_error_fmt(hdl, errno,
4425 dgettext(TEXT_DOMAIN, "cannot get event"));
4426 goto out;
4427 }
4428 }
4429
4430 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4431 if (error != 0)
4432 goto out;
4433
4434 *dropped = (int)zc.zc_cookie;
4435out:
4436 zcmd_free_nvlists(&zc);
4437
4438 return (error);
4439}
4440
4441/*
4442 * Clear all events.
4443 */
4444int
4445zpool_events_clear(libzfs_handle_t *hdl, int *count)
4446{
13fe0198 4447 zfs_cmd_t zc = {"\0"};
26685276
BB
4448
4449 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
f00f4690
AZ
4450 return (zpool_standard_error(hdl, errno,
4451 dgettext(TEXT_DOMAIN, "cannot clear events")));
26685276
BB
4452
4453 if (count != NULL)
4454 *count = (int)zc.zc_cookie; /* # of events cleared */
4455
4456 return (0);
4457}
4458
75e3ff58
BB
4459/*
4460 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4461 * the passed zevent_fd file handle. On success zero is returned,
4462 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4463 */
4464int
4465zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4466{
4467 zfs_cmd_t zc = {"\0"};
4468 int error = 0;
4469
4470 zc.zc_guid = eid;
4471 zc.zc_cleanup_fd = zevent_fd;
4472
4473 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4474 switch (errno) {
4475 case ENOENT:
4476 error = zfs_error_fmt(hdl, EZFS_NOENT,
4477 dgettext(TEXT_DOMAIN, "cannot get event"));
4478 break;
4479
4480 case ENOMEM:
4481 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4482 dgettext(TEXT_DOMAIN, "cannot get event"));
4483 break;
4484
4485 default:
4486 error = zpool_standard_error_fmt(hdl, errno,
4487 dgettext(TEXT_DOMAIN, "cannot get event"));
4488 break;
4489 }
4490 }
4491
4492 return (error);
4493}
4494
de4f06c2
PD
4495static void
4496zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4497 char *pathname, size_t len, boolean_t always_unmounted)
34dc7c2f 4498{
13fe0198 4499 zfs_cmd_t zc = {"\0"};
34dc7c2f
BB
4500 boolean_t mounted = B_FALSE;
4501 char *mntpnt = NULL;
eca7b760 4502 char dsname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
4503
4504 if (dsobj == 0) {
4505 /* special case for the MOS */
d1d7e268
MK
4506 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4507 (longlong_t)obj);
34dc7c2f
BB
4508 return;
4509 }
4510
4511 /* get the dataset's name */
4512 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4513 zc.zc_obj = dsobj;
b834b58a 4514 if (zfs_ioctl(zhp->zpool_hdl,
34dc7c2f
BB
4515 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4516 /* just write out a path of two object numbers */
4517 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
b8864a23 4518 (longlong_t)dsobj, (longlong_t)obj);
34dc7c2f
BB
4519 return;
4520 }
4521 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4522
4523 /* find out if the dataset is mounted */
de4f06c2
PD
4524 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
4525 &mntpnt);
34dc7c2f
BB
4526
4527 /* get the corrupted object's path */
4528 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4529 zc.zc_obj = obj;
b834b58a 4530 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
34dc7c2f
BB
4531 &zc) == 0) {
4532 if (mounted) {
4533 (void) snprintf(pathname, len, "%s%s", mntpnt,
4534 zc.zc_value);
4535 } else {
4536 (void) snprintf(pathname, len, "%s:%s",
4537 dsname, zc.zc_value);
4538 }
4539 } else {
d1d7e268
MK
4540 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4541 (longlong_t)obj);
34dc7c2f
BB
4542 }
4543 free(mntpnt);
4544}
4545
de4f06c2
PD
4546void
4547zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4548 char *pathname, size_t len)
4549{
4550 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
4551}
4552
4553void
4554zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4555 char *pathname, size_t len)
4556{
4557 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
4558}
e60e158e
JG
4559/*
4560 * Wait while the specified activity is in progress in the pool.
4561 */
4562int
4563zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4564{
4565 boolean_t missing;
4566
4567 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4568
4569 if (missing) {
4570 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4571 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4572 zhp->zpool_name);
4573 return (ENOENT);
4574 } else {
4575 return (error);
4576 }
4577}
4578
4579/*
4580 * Wait for the given activity and return the status of the wait (whether or not
4581 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4582 * reported via the 'missing' parameter, rather than by printing an error
4583 * message. This is convenient when this function is called in a loop over a
4584 * long period of time (as it is, for example, by zpool's wait cmd). In that
4585 * scenario, a pool being exported or destroyed should be considered a normal
4586 * event, so we don't want to print an error when we find that the pool doesn't
4587 * exist.
4588 */
4589int
4590zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4591 boolean_t *missing, boolean_t *waited)
4592{
4593 int error = lzc_wait(zhp->zpool_name, activity, waited);
4594 *missing = (error == ENOENT);
4595 if (*missing)
4596 return (0);
4597
4598 if (error != 0) {
4599 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4600 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4601 zhp->zpool_name);
4602 }
4603
4604 return (error);
4605}
108a454a
PD
4606
4607int
1db9e6e4 4608zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
108a454a
PD
4609{
4610 int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4611 if (error != 0) {
4612 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4613 dgettext(TEXT_DOMAIN,
4614 "error setting bootenv in pool '%s'"), zhp->zpool_name);
4615 }
4616
4617 return (error);
4618}
4619
4620int
1db9e6e4 4621zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
108a454a 4622{
1db9e6e4
TS
4623 nvlist_t *nvl;
4624 int error;
4625
4626 nvl = NULL;
4627 error = lzc_get_bootenv(zhp->zpool_name, &nvl);
108a454a
PD
4628 if (error != 0) {
4629 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4630 dgettext(TEXT_DOMAIN,
4631 "error getting bootenv in pool '%s'"), zhp->zpool_name);
1db9e6e4
TS
4632 } else {
4633 *nvlp = nvl;
108a454a
PD
4634 }
4635
1db9e6e4 4636 return (error);
108a454a 4637}
658fb802
CB
4638
4639/*
4640 * Attempt to read and parse feature file(s) (from "compatibility" property).
4641 * Files contain zpool feature names, comma or whitespace-separated.
4642 * Comments (# character to next newline) are discarded.
4643 *
4644 * Arguments:
4645 * compatibility : string containing feature filenames
4646 * features : either NULL or pointer to array of boolean
e086db16
CB
4647 * report : either NULL or pointer to string buffer
4648 * rlen : length of "report" buffer
658fb802
CB
4649 *
4650 * compatibility is NULL (unset), "", "off", "legacy", or list of
4651 * comma-separated filenames. filenames should either be absolute,
4652 * or relative to:
4653 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4654 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4655 * (Unset), "" or "off" => enable all features
4656 * "legacy" => disable all features
e086db16 4657 *
658fb802
CB
4658 * Any feature names read from files which match unames in spa_feature_table
4659 * will have the corresponding boolean set in the features array (if non-NULL).
4660 * If more than one feature set specified, only features present in *all* of
4661 * them will be set.
4662 *
e086db16 4663 * "report" if not NULL will be populated with a suitable status message.
658fb802
CB
4664 *
4665 * Return values:
4666 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
658fb802 4667 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
e086db16
CB
4668 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4669 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4670 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
658fb802
CB
4671 */
4672zpool_compat_status_t
e086db16
CB
4673zpool_load_compat(const char *compat, boolean_t *features, char *report,
4674 size_t rlen)
658fb802
CB
4675{
4676 int sdirfd, ddirfd, featfd;
658fb802 4677 struct stat fs;
e086db16
CB
4678 char *fc;
4679 char *ps, *ls, *ws;
658fb802 4680 char *file, *line, *word;
e086db16
CB
4681
4682 char l_compat[ZFS_MAXPROPLEN];
4683
4684 boolean_t ret_nofiles = B_TRUE;
4685 boolean_t ret_badfile = B_FALSE;
4686 boolean_t ret_badtoken = B_FALSE;
4687 boolean_t ret_warntoken = B_FALSE;
658fb802
CB
4688
4689 /* special cases (unset), "" and "off" => enable all features */
e086db16
CB
4690 if (compat == NULL || compat[0] == '\0' ||
4691 strcmp(compat, ZPOOL_COMPAT_OFF) == 0) {
658fb802 4692 if (features != NULL)
e086db16 4693 for (uint_t i = 0; i < SPA_FEATURES; i++)
658fb802 4694 features[i] = B_TRUE;
e086db16
CB
4695 if (report != NULL)
4696 strlcpy(report, gettext("all features enabled"), rlen);
658fb802
CB
4697 return (ZPOOL_COMPATIBILITY_OK);
4698 }
4699
4700 /* Final special case "legacy" => disable all features */
e086db16 4701 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
658fb802 4702 if (features != NULL)
e086db16 4703 for (uint_t i = 0; i < SPA_FEATURES; i++)
658fb802 4704 features[i] = B_FALSE;
e086db16
CB
4705 if (report != NULL)
4706 strlcpy(report, gettext("all features disabled"), rlen);
658fb802
CB
4707 return (ZPOOL_COMPATIBILITY_OK);
4708 }
4709
4710 /*
4711 * Start with all true; will be ANDed with results from each file
4712 */
4713 if (features != NULL)
e086db16 4714 for (uint_t i = 0; i < SPA_FEATURES; i++)
658fb802
CB
4715 features[i] = B_TRUE;
4716
710fd1de
RY
4717 char err_badfile[ZFS_MAXPROPLEN] = "";
4718 char err_badtoken[ZFS_MAXPROPLEN] = "";
e086db16 4719
658fb802
CB
4720 /*
4721 * We ignore errors from the directory open()
4722 * as they're only needed if the filename is relative
4723 * which will be checked during the openat().
4724 */
f97142c7
CB
4725
4726/* O_PATH safer than O_RDONLY if system allows it */
4727#if defined(O_PATH)
4728#define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4729#else
4730#define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
658fb802 4731#endif
f97142c7
CB
4732
4733 sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
4734 ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
658fb802 4735
e086db16
CB
4736 (void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
4737
4738 for (file = strtok_r(l_compat, ",", &ps);
4739 file != NULL;
4740 file = strtok_r(NULL, ",", &ps)) {
4741
f97142c7 4742 boolean_t l_features[SPA_FEATURES];
e086db16
CB
4743
4744 enum { Z_SYSCONF, Z_DATA } source;
658fb802
CB
4745
4746 /* try sysconfdir first, then datadir */
e086db16 4747 source = Z_SYSCONF;
8bc357ba
AZ
4748 if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) {
4749 featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC);
e086db16 4750 source = Z_DATA;
658fb802
CB
4751 }
4752
e086db16
CB
4753 /* File readable and correct size? */
4754 if (featfd < 0 ||
4755 fstat(featfd, &fs) < 0 ||
4756 fs.st_size < 1 ||
4757 fs.st_size > ZPOOL_COMPAT_MAXSIZE) {
658fb802 4758 (void) close(featfd);
e086db16
CB
4759 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4760 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4761 ret_badfile = B_TRUE;
4762 continue;
658fb802
CB
4763 }
4764
f97142c7
CB
4765/* Prefault the file if system allows */
4766#if defined(MAP_POPULATE)
4767#define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4768#elif defined(MAP_PREFAULT_READ)
4769#define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4770#else
4771#define ZC_MMAP_FLAGS (MAP_PRIVATE)
8bc357ba 4772#endif
f97142c7 4773
658fb802 4774 /* private mmap() so we can strtok safely */
f97142c7
CB
4775 fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
4776 ZC_MMAP_FLAGS, featfd, 0);
658fb802
CB
4777 (void) close(featfd);
4778
e086db16 4779 /* map ok, and last character == newline? */
8bc357ba 4780 if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') {
658fb802 4781 (void) munmap((void *) fc, fs.st_size);
e086db16
CB
4782 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4783 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4784 ret_badfile = B_TRUE;
4785 continue;
658fb802
CB
4786 }
4787
e086db16 4788 ret_nofiles = B_FALSE;
658fb802 4789
f97142c7
CB
4790 for (uint_t i = 0; i < SPA_FEATURES; i++)
4791 l_features[i] = B_FALSE;
4792
4793 /* replace final newline with NULL to ensure string ends */
e086db16
CB
4794 fc[fs.st_size - 1] = '\0';
4795
4796 for (line = strtok_r(fc, "\n", &ls);
4797 line != NULL;
4798 line = strtok_r(NULL, "\n", &ls)) {
658fb802 4799 /* discard comments */
d2119d0e
JL
4800 char *r = strchr(line, '#');
4801 if (r != NULL)
4802 *r = '\0';
658fb802 4803
e086db16
CB
4804 for (word = strtok_r(line, ", \t", &ws);
4805 word != NULL;
4806 word = strtok_r(NULL, ", \t", &ws)) {
658fb802 4807 /* Find matching feature name */
e086db16
CB
4808 uint_t f;
4809 for (f = 0; f < SPA_FEATURES; f++) {
658fb802 4810 zfeature_info_t *fi =
e086db16 4811 &spa_feature_table[f];
658fb802 4812 if (strcmp(word, fi->fi_uname) == 0) {
e086db16 4813 l_features[f] = B_TRUE;
658fb802
CB
4814 break;
4815 }
4816 }
e086db16
CB
4817 if (f < SPA_FEATURES)
4818 continue;
4819
4820 /* found an unrecognized word */
4821 /* lightly sanitize it */
4822 if (strlen(word) > 32)
4823 word[32] = '\0';
4824 for (char *c = word; *c != '\0'; c++)
4825 if (!isprint(*c))
4826 *c = '?';
4827
4828 strlcat(err_badtoken, word, ZFS_MAXPROPLEN);
4829 strlcat(err_badtoken, " ", ZFS_MAXPROPLEN);
4830 if (source == Z_SYSCONF)
4831 ret_badtoken = B_TRUE;
4832 else
4833 ret_warntoken = B_TRUE;
658fb802 4834 }
658fb802
CB
4835 }
4836 (void) munmap((void *) fc, fs.st_size);
e086db16
CB
4837
4838 if (features != NULL)
4839 for (uint_t i = 0; i < SPA_FEATURES; i++)
4840 features[i] &= l_features[i];
658fb802
CB
4841 }
4842 (void) close(sdirfd);
4843 (void) close(ddirfd);
e086db16
CB
4844
4845 /* Return the most serious error */
4846 if (ret_badfile) {
4847 if (report != NULL)
4848 snprintf(report, rlen, gettext("could not read/"
4849 "parse feature file(s): %s"), err_badfile);
4850 return (ZPOOL_COMPATIBILITY_BADFILE);
4851 }
4852 if (ret_nofiles) {
4853 if (report != NULL)
4854 strlcpy(report,
4855 gettext("no valid compatibility files specified"),
4856 rlen);
658fb802 4857 return (ZPOOL_COMPATIBILITY_NOFILES);
e086db16
CB
4858 }
4859 if (ret_badtoken) {
4860 if (report != NULL)
4861 snprintf(report, rlen, gettext("invalid feature "
4862 "name(s) in local compatibility files: %s"),
4863 err_badtoken);
4864 return (ZPOOL_COMPATIBILITY_BADTOKEN);
4865 }
4866 if (ret_warntoken) {
4867 if (report != NULL)
4868 snprintf(report, rlen, gettext("unrecognized feature "
4869 "name(s) in distribution compatibility files: %s"),
4870 err_badtoken);
4871 return (ZPOOL_COMPATIBILITY_WARNTOKEN);
4872 }
4873 if (report != NULL)
4874 strlcpy(report, gettext("compatibility set ok"), rlen);
658fb802
CB
4875 return (ZPOOL_COMPATIBILITY_OK);
4876}
2a673e76
AJ
4877
4878static int
4879zpool_vdev_guid(zpool_handle_t *zhp, const char *vdevname, uint64_t *vdev_guid)
4880{
4881 nvlist_t *tgt;
4882 boolean_t avail_spare, l2cache;
4883
4884 verify(zhp != NULL);
4885 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
d68a2bfa 4886 char errbuf[ERRBUFLEN];
2a673e76
AJ
4887 (void) snprintf(errbuf, sizeof (errbuf),
4888 dgettext(TEXT_DOMAIN, "pool is in an unavailable state"));
4889 return (zfs_error(zhp->zpool_hdl, EZFS_POOLUNAVAIL, errbuf));
4890 }
4891
4892 if ((tgt = zpool_find_vdev(zhp, vdevname, &avail_spare, &l2cache,
4893 NULL)) == NULL) {
d68a2bfa 4894 char errbuf[ERRBUFLEN];
2a673e76
AJ
4895 (void) snprintf(errbuf, sizeof (errbuf),
4896 dgettext(TEXT_DOMAIN, "can not find %s in %s"),
4897 vdevname, zhp->zpool_name);
4898 return (zfs_error(zhp->zpool_hdl, EZFS_NODEVICE, errbuf));
4899 }
4900
8bb9ecf4 4901 *vdev_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2a673e76
AJ
4902 return (0);
4903}
4904
4905/*
4906 * Get a vdev property value for 'prop' and return the value in
4907 * a pre-allocated buffer.
4908 */
4909int
4910zpool_get_vdev_prop_value(nvlist_t *nvprop, vdev_prop_t prop, char *prop_name,
4911 char *buf, size_t len, zprop_source_t *srctype, boolean_t literal)
4912{
4913 nvlist_t *nv;
a926aab9 4914 const char *strval;
8bb9ecf4 4915 uint64_t intval;
2a673e76
AJ
4916 zprop_source_t src = ZPROP_SRC_NONE;
4917
4ff7a8fa 4918 if (prop == VDEV_PROP_USERPROP) {
2a673e76
AJ
4919 /* user property, prop_name must contain the property name */
4920 assert(prop_name != NULL);
4921 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
4922 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
4923 strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
2a673e76
AJ
4924 } else {
4925 /* user prop not found */
4926 return (-1);
4927 }
4928 (void) strlcpy(buf, strval, len);
4929 if (srctype)
4930 *srctype = src;
4931 return (0);
4932 }
4933
4934 if (prop_name == NULL)
4935 prop_name = (char *)vdev_prop_to_name(prop);
4936
4937 switch (vdev_prop_get_type(prop)) {
4938 case PROP_TYPE_STRING:
4939 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
4940 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
4941 strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
2a673e76
AJ
4942 } else {
4943 src = ZPROP_SRC_DEFAULT;
a926aab9 4944 if ((strval = vdev_prop_default_string(prop)) == NULL)
2a673e76
AJ
4945 strval = "-";
4946 }
4947 (void) strlcpy(buf, strval, len);
4948 break;
4949
4950 case PROP_TYPE_NUMBER:
4951 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
4952 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
4953 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
2a673e76
AJ
4954 } else {
4955 src = ZPROP_SRC_DEFAULT;
4956 intval = vdev_prop_default_numeric(prop);
4957 }
4958
4959 switch (prop) {
4960 case VDEV_PROP_ASIZE:
4961 case VDEV_PROP_PSIZE:
4962 case VDEV_PROP_SIZE:
be8e1d81 4963 case VDEV_PROP_BOOTSIZE:
2a673e76
AJ
4964 case VDEV_PROP_ALLOCATED:
4965 case VDEV_PROP_FREE:
4966 case VDEV_PROP_READ_ERRORS:
4967 case VDEV_PROP_WRITE_ERRORS:
4968 case VDEV_PROP_CHECKSUM_ERRORS:
4969 case VDEV_PROP_INITIALIZE_ERRORS:
4970 case VDEV_PROP_OPS_NULL:
4971 case VDEV_PROP_OPS_READ:
4972 case VDEV_PROP_OPS_WRITE:
4973 case VDEV_PROP_OPS_FREE:
4974 case VDEV_PROP_OPS_CLAIM:
4975 case VDEV_PROP_OPS_TRIM:
4976 case VDEV_PROP_BYTES_NULL:
4977 case VDEV_PROP_BYTES_READ:
4978 case VDEV_PROP_BYTES_WRITE:
4979 case VDEV_PROP_BYTES_FREE:
4980 case VDEV_PROP_BYTES_CLAIM:
4981 case VDEV_PROP_BYTES_TRIM:
4982 if (literal) {
4983 (void) snprintf(buf, len, "%llu",
4984 (u_longlong_t)intval);
4985 } else {
4986 (void) zfs_nicenum(intval, buf, len);
4987 }
4988 break;
4989 case VDEV_PROP_EXPANDSZ:
4990 if (intval == 0) {
4991 (void) strlcpy(buf, "-", len);
4992 } else if (literal) {
4993 (void) snprintf(buf, len, "%llu",
4994 (u_longlong_t)intval);
4995 } else {
4996 (void) zfs_nicenum(intval, buf, len);
4997 }
4998 break;
4999 case VDEV_PROP_CAPACITY:
5000 if (literal) {
5001 (void) snprintf(buf, len, "%llu",
5002 (u_longlong_t)intval);
5003 } else {
5004 (void) snprintf(buf, len, "%llu%%",
5005 (u_longlong_t)intval);
5006 }
5007 break;
5008 case VDEV_PROP_FRAGMENTATION:
5009 if (intval == UINT64_MAX) {
5010 (void) strlcpy(buf, "-", len);
5011 } else {
5012 (void) snprintf(buf, len, "%llu%%",
5013 (u_longlong_t)intval);
5014 }
5015 break;
5016 case VDEV_PROP_STATE:
5017 if (literal) {
5018 (void) snprintf(buf, len, "%llu",
5019 (u_longlong_t)intval);
5020 } else {
5021 (void) strlcpy(buf, zpool_state_to_name(intval,
5022 VDEV_AUX_NONE), len);
5023 }
5024 break;
5025 default:
5026 (void) snprintf(buf, len, "%llu",
5027 (u_longlong_t)intval);
5028 }
5029 break;
5030
5031 case PROP_TYPE_INDEX:
5032 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
8bb9ecf4
RM
5033 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5034 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
2a673e76
AJ
5035 } else {
5036 src = ZPROP_SRC_DEFAULT;
5037 intval = vdev_prop_default_numeric(prop);
5038 }
5039 if (vdev_prop_index_to_string(prop, intval,
5040 (const char **)&strval) != 0)
5041 return (-1);
5042 (void) strlcpy(buf, strval, len);
5043 break;
5044
5045 default:
5046 abort();
5047 }
5048
5049 if (srctype)
5050 *srctype = src;
5051
5052 return (0);
5053}
5054
5055/*
5056 * Get a vdev property value for 'prop_name' and return the value in
5057 * a pre-allocated buffer.
5058 */
5059int
5060zpool_get_vdev_prop(zpool_handle_t *zhp, const char *vdevname, vdev_prop_t prop,
5061 char *prop_name, char *buf, size_t len, zprop_source_t *srctype,
5062 boolean_t literal)
5063{
5064 nvlist_t *reqnvl, *reqprops;
5065 nvlist_t *retprops = NULL;
6fc34371 5066 uint64_t vdev_guid = 0;
2a673e76
AJ
5067 int ret;
5068
5069 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5070 return (ret);
5071
5072 if (nvlist_alloc(&reqnvl, NV_UNIQUE_NAME, 0) != 0)
5073 return (no_memory(zhp->zpool_hdl));
5074 if (nvlist_alloc(&reqprops, NV_UNIQUE_NAME, 0) != 0)
5075 return (no_memory(zhp->zpool_hdl));
5076
5077 fnvlist_add_uint64(reqnvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5078
4ff7a8fa 5079 if (prop != VDEV_PROP_USERPROP) {
2a673e76
AJ
5080 /* prop_name overrides prop value */
5081 if (prop_name != NULL)
5082 prop = vdev_name_to_prop(prop_name);
5083 else
5084 prop_name = (char *)vdev_prop_to_name(prop);
5085 assert(prop < VDEV_NUM_PROPS);
5086 }
5087
5088 assert(prop_name != NULL);
5089 if (nvlist_add_uint64(reqprops, prop_name, prop) != 0) {
5090 nvlist_free(reqnvl);
5091 nvlist_free(reqprops);
5092 return (no_memory(zhp->zpool_hdl));
5093 }
5094
5095 fnvlist_add_nvlist(reqnvl, ZPOOL_VDEV_PROPS_GET_PROPS, reqprops);
5096
5097 ret = lzc_get_vdev_prop(zhp->zpool_name, reqnvl, &retprops);
5098
5099 if (ret == 0) {
5100 ret = zpool_get_vdev_prop_value(retprops, prop, prop_name, buf,
5101 len, srctype, literal);
5102 } else {
d68a2bfa 5103 char errbuf[ERRBUFLEN];
2a673e76
AJ
5104 (void) snprintf(errbuf, sizeof (errbuf),
5105 dgettext(TEXT_DOMAIN, "cannot get vdev property %s from"
5106 " %s in %s"), prop_name, vdevname, zhp->zpool_name);
5107 (void) zpool_standard_error(zhp->zpool_hdl, ret, errbuf);
5108 }
5109
5110 nvlist_free(reqnvl);
5111 nvlist_free(reqprops);
5112 nvlist_free(retprops);
5113
5114 return (ret);
5115}
5116
5117/*
5118 * Get all vdev properties
5119 */
5120int
5121zpool_get_all_vdev_props(zpool_handle_t *zhp, const char *vdevname,
5122 nvlist_t **outnvl)
5123{
5124 nvlist_t *nvl = NULL;
6fc34371 5125 uint64_t vdev_guid = 0;
2a673e76
AJ
5126 int ret;
5127
5128 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5129 return (ret);
5130
5131 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5132 return (no_memory(zhp->zpool_hdl));
5133
5134 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5135
5136 ret = lzc_get_vdev_prop(zhp->zpool_name, nvl, outnvl);
5137
5138 nvlist_free(nvl);
5139
5140 if (ret) {
d68a2bfa 5141 char errbuf[ERRBUFLEN];
2a673e76
AJ
5142 (void) snprintf(errbuf, sizeof (errbuf),
5143 dgettext(TEXT_DOMAIN, "cannot get vdev properties for"
5144 " %s in %s"), vdevname, zhp->zpool_name);
5145 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5146 }
5147
5148 return (ret);
5149}
5150
5151/*
5152 * Set vdev property
5153 */
5154int
5155zpool_set_vdev_prop(zpool_handle_t *zhp, const char *vdevname,
5156 const char *propname, const char *propval)
5157{
5158 int ret;
2a673e76
AJ
5159 nvlist_t *nvl = NULL;
5160 nvlist_t *outnvl = NULL;
5161 nvlist_t *props;
5162 nvlist_t *realprops;
5163 prop_flags_t flags = { 0 };
5164 uint64_t version;
5165 uint64_t vdev_guid;
5166
5167 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5168 return (ret);
5169
2a673e76
AJ
5170 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5171 return (no_memory(zhp->zpool_hdl));
5172 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
5173 return (no_memory(zhp->zpool_hdl));
5174
5175 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_SET_VDEV, vdev_guid);
5176
5177 if (nvlist_add_string(props, propname, propval) != 0) {
5178 nvlist_free(props);
5179 return (no_memory(zhp->zpool_hdl));
5180 }
5181
d68a2bfa 5182 char errbuf[ERRBUFLEN];
2a673e76
AJ
5183 (void) snprintf(errbuf, sizeof (errbuf),
5184 dgettext(TEXT_DOMAIN, "cannot set property %s for %s on %s"),
5185 propname, vdevname, zhp->zpool_name);
5186
5187 flags.vdevprop = 1;
5188 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5189 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5190 zhp->zpool_name, props, version, flags, errbuf)) == NULL) {
5191 nvlist_free(props);
5192 nvlist_free(nvl);
5193 return (-1);
5194 }
5195
5196 nvlist_free(props);
5197 props = realprops;
5198
5199 fnvlist_add_nvlist(nvl, ZPOOL_VDEV_PROPS_SET_PROPS, props);
5200
5201 ret = lzc_set_vdev_prop(zhp->zpool_name, nvl, &outnvl);
5202
5203 nvlist_free(props);
5204 nvlist_free(nvl);
5205 nvlist_free(outnvl);
5206
5207 if (ret)
5208 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5209
5210 return (ret);
5211}