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