xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_pool.c (revision 5c65a0a9163cc00389d8527ee12c4e69df07ea42)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25783d3ff6SMartin Matuska  * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
26eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27eda14cbcSMatt Macy  * Copyright (c) 2018 Datto Inc.
28eda14cbcSMatt Macy  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29eda14cbcSMatt Macy  * Copyright (c) 2017, Intel Corporation.
30eda14cbcSMatt Macy  * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
31ee36e25aSMartin Matuska  * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
32cbfe9975SMartin Matuska  * Copyright (c) 2021, 2023, Klara Inc.
33eda14cbcSMatt Macy  */
34eda14cbcSMatt Macy 
35eda14cbcSMatt Macy #include <errno.h>
36eda14cbcSMatt Macy #include <libintl.h>
37eda14cbcSMatt Macy #include <stdio.h>
38eda14cbcSMatt Macy #include <stdlib.h>
39eda14cbcSMatt Macy #include <strings.h>
40eda14cbcSMatt Macy #include <unistd.h>
41eda14cbcSMatt Macy #include <libgen.h>
42eda14cbcSMatt Macy #include <zone.h>
43eda14cbcSMatt Macy #include <sys/stat.h>
44eda14cbcSMatt Macy #include <sys/efi_partition.h>
45eda14cbcSMatt Macy #include <sys/systeminfo.h>
46eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
477877fdebSMatt Macy #include <sys/zfs_sysfs.h>
48eda14cbcSMatt Macy #include <sys/vdev_disk.h>
49ee36e25aSMartin Matuska #include <sys/types.h>
50eda14cbcSMatt Macy #include <dlfcn.h>
51eda14cbcSMatt Macy #include <libzutil.h>
52ee36e25aSMartin Matuska #include <fcntl.h>
53ee36e25aSMartin Matuska 
54eda14cbcSMatt Macy #include "zfs_namecheck.h"
55eda14cbcSMatt Macy #include "zfs_prop.h"
56eda14cbcSMatt Macy #include "libzfs_impl.h"
57eda14cbcSMatt Macy #include "zfs_comutil.h"
58eda14cbcSMatt Macy #include "zfeature_common.h"
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy static boolean_t zpool_vdev_is_interior(const char *name);
61eda14cbcSMatt Macy 
62eda14cbcSMatt Macy typedef struct prop_flags {
63dbd5678dSMartin Matuska 	unsigned int create:1;	/* Validate property on creation */
64dbd5678dSMartin Matuska 	unsigned int import:1;	/* Validate property on import */
65dbd5678dSMartin Matuska 	unsigned int vdevprop:1; /* Validate property as a VDEV property */
66eda14cbcSMatt Macy } prop_flags_t;
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy /*
69eda14cbcSMatt Macy  * ====================================================================
70eda14cbcSMatt Macy  *   zpool property functions
71eda14cbcSMatt Macy  * ====================================================================
72eda14cbcSMatt Macy  */
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy static int
zpool_get_all_props(zpool_handle_t * zhp)75eda14cbcSMatt Macy zpool_get_all_props(zpool_handle_t *zhp)
76eda14cbcSMatt Macy {
77eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
78eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
81eda14cbcSMatt Macy 
82ce4dcb97SMartin Matuska 	if (zhp->zpool_n_propnames > 0) {
83ce4dcb97SMartin Matuska 		nvlist_t *innvl = fnvlist_alloc();
84ce4dcb97SMartin Matuska 		fnvlist_add_string_array(innvl, ZPOOL_GET_PROPS_NAMES,
85ce4dcb97SMartin Matuska 		    zhp->zpool_propnames, zhp->zpool_n_propnames);
86ce4dcb97SMartin Matuska 		zcmd_write_src_nvlist(hdl, &zc, innvl);
87ce4dcb97SMartin Matuska 	}
88ce4dcb97SMartin Matuska 
89716fd348SMartin Matuska 	zcmd_alloc_dst_nvlist(hdl, &zc, 0);
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy 	while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
92716fd348SMartin Matuska 		if (errno == ENOMEM)
93716fd348SMartin Matuska 			zcmd_expand_dst_nvlist(hdl, &zc);
94716fd348SMartin Matuska 		else {
95eda14cbcSMatt Macy 			zcmd_free_nvlists(&zc);
96eda14cbcSMatt Macy 			return (-1);
97eda14cbcSMatt Macy 		}
98eda14cbcSMatt Macy 	}
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
101eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
102eda14cbcSMatt Macy 		return (-1);
103eda14cbcSMatt Macy 	}
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy 	return (0);
108eda14cbcSMatt Macy }
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy int
zpool_props_refresh(zpool_handle_t * zhp)111eda14cbcSMatt Macy zpool_props_refresh(zpool_handle_t *zhp)
112eda14cbcSMatt Macy {
113eda14cbcSMatt Macy 	nvlist_t *old_props;
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	old_props = zhp->zpool_props;
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy 	if (zpool_get_all_props(zhp) != 0)
118eda14cbcSMatt Macy 		return (-1);
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy 	nvlist_free(old_props);
121eda14cbcSMatt Macy 	return (0);
122eda14cbcSMatt Macy }
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy static const char *
zpool_get_prop_string(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)125eda14cbcSMatt Macy zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
126eda14cbcSMatt Macy     zprop_source_t *src)
127eda14cbcSMatt Macy {
128eda14cbcSMatt Macy 	nvlist_t *nv, *nvl;
129a0b956f5SMartin Matuska 	const char *value;
130eda14cbcSMatt Macy 	zprop_source_t source;
131eda14cbcSMatt Macy 
132eda14cbcSMatt Macy 	nvl = zhp->zpool_props;
133eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
134da5137abSMartin Matuska 		source = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
135da5137abSMartin Matuska 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
136eda14cbcSMatt Macy 	} else {
137eda14cbcSMatt Macy 		source = ZPROP_SRC_DEFAULT;
138a0b956f5SMartin Matuska 		if ((value = zpool_prop_default_string(prop)) == NULL)
139eda14cbcSMatt Macy 			value = "-";
140eda14cbcSMatt Macy 	}
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy 	if (src)
143eda14cbcSMatt Macy 		*src = source;
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 	return (value);
146eda14cbcSMatt Macy }
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy uint64_t
zpool_get_prop_int(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)149eda14cbcSMatt Macy zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
150eda14cbcSMatt Macy {
151eda14cbcSMatt Macy 	nvlist_t *nv, *nvl;
152eda14cbcSMatt Macy 	uint64_t value;
153eda14cbcSMatt Macy 	zprop_source_t source;
154eda14cbcSMatt Macy 
155eda14cbcSMatt Macy 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
156eda14cbcSMatt Macy 		/*
157eda14cbcSMatt Macy 		 * zpool_get_all_props() has most likely failed because
158eda14cbcSMatt Macy 		 * the pool is faulted, but if all we need is the top level
159eda14cbcSMatt Macy 		 * vdev's guid then get it from the zhp config nvlist.
160eda14cbcSMatt Macy 		 */
161eda14cbcSMatt Macy 		if ((prop == ZPOOL_PROP_GUID) &&
162eda14cbcSMatt Macy 		    (nvlist_lookup_nvlist(zhp->zpool_config,
163eda14cbcSMatt Macy 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
164eda14cbcSMatt Macy 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
165eda14cbcSMatt Macy 		    == 0)) {
166eda14cbcSMatt Macy 			return (value);
167eda14cbcSMatt Macy 		}
168eda14cbcSMatt Macy 		return (zpool_prop_default_numeric(prop));
169eda14cbcSMatt Macy 	}
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy 	nvl = zhp->zpool_props;
172eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
173da5137abSMartin Matuska 		source = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
174da5137abSMartin Matuska 		value = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
175eda14cbcSMatt Macy 	} else {
176eda14cbcSMatt Macy 		source = ZPROP_SRC_DEFAULT;
177eda14cbcSMatt Macy 		value = zpool_prop_default_numeric(prop);
178eda14cbcSMatt Macy 	}
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy 	if (src)
181eda14cbcSMatt Macy 		*src = source;
182eda14cbcSMatt Macy 
183eda14cbcSMatt Macy 	return (value);
184eda14cbcSMatt Macy }
185eda14cbcSMatt Macy 
186eda14cbcSMatt Macy /*
187eda14cbcSMatt Macy  * Map VDEV STATE to printed strings.
188eda14cbcSMatt Macy  */
189eda14cbcSMatt Macy const char *
zpool_state_to_name(vdev_state_t state,vdev_aux_t aux)190eda14cbcSMatt Macy zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
191eda14cbcSMatt Macy {
192eda14cbcSMatt Macy 	switch (state) {
193eda14cbcSMatt Macy 	case VDEV_STATE_CLOSED:
194eda14cbcSMatt Macy 	case VDEV_STATE_OFFLINE:
195eda14cbcSMatt Macy 		return (gettext("OFFLINE"));
196eda14cbcSMatt Macy 	case VDEV_STATE_REMOVED:
197eda14cbcSMatt Macy 		return (gettext("REMOVED"));
198eda14cbcSMatt Macy 	case VDEV_STATE_CANT_OPEN:
199eda14cbcSMatt Macy 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
200eda14cbcSMatt Macy 			return (gettext("FAULTED"));
201eda14cbcSMatt Macy 		else if (aux == VDEV_AUX_SPLIT_POOL)
202eda14cbcSMatt Macy 			return (gettext("SPLIT"));
203eda14cbcSMatt Macy 		else
204eda14cbcSMatt Macy 			return (gettext("UNAVAIL"));
205eda14cbcSMatt Macy 	case VDEV_STATE_FAULTED:
206eda14cbcSMatt Macy 		return (gettext("FAULTED"));
207eda14cbcSMatt Macy 	case VDEV_STATE_DEGRADED:
208eda14cbcSMatt Macy 		return (gettext("DEGRADED"));
209eda14cbcSMatt Macy 	case VDEV_STATE_HEALTHY:
210eda14cbcSMatt Macy 		return (gettext("ONLINE"));
211eda14cbcSMatt Macy 
212eda14cbcSMatt Macy 	default:
213eda14cbcSMatt Macy 		break;
214eda14cbcSMatt Macy 	}
215eda14cbcSMatt Macy 
216eda14cbcSMatt Macy 	return (gettext("UNKNOWN"));
217eda14cbcSMatt Macy }
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy /*
220eda14cbcSMatt Macy  * Map POOL STATE to printed strings.
221eda14cbcSMatt Macy  */
222eda14cbcSMatt Macy const char *
zpool_pool_state_to_name(pool_state_t state)223eda14cbcSMatt Macy zpool_pool_state_to_name(pool_state_t state)
224eda14cbcSMatt Macy {
225eda14cbcSMatt Macy 	switch (state) {
226eda14cbcSMatt Macy 	default:
227eda14cbcSMatt Macy 		break;
228eda14cbcSMatt Macy 	case POOL_STATE_ACTIVE:
229eda14cbcSMatt Macy 		return (gettext("ACTIVE"));
230eda14cbcSMatt Macy 	case POOL_STATE_EXPORTED:
231eda14cbcSMatt Macy 		return (gettext("EXPORTED"));
232eda14cbcSMatt Macy 	case POOL_STATE_DESTROYED:
233eda14cbcSMatt Macy 		return (gettext("DESTROYED"));
234eda14cbcSMatt Macy 	case POOL_STATE_SPARE:
235eda14cbcSMatt Macy 		return (gettext("SPARE"));
236eda14cbcSMatt Macy 	case POOL_STATE_L2CACHE:
237eda14cbcSMatt Macy 		return (gettext("L2CACHE"));
238eda14cbcSMatt Macy 	case POOL_STATE_UNINITIALIZED:
239eda14cbcSMatt Macy 		return (gettext("UNINITIALIZED"));
240eda14cbcSMatt Macy 	case POOL_STATE_UNAVAIL:
241eda14cbcSMatt Macy 		return (gettext("UNAVAIL"));
242eda14cbcSMatt Macy 	case POOL_STATE_POTENTIALLY_ACTIVE:
243eda14cbcSMatt Macy 		return (gettext("POTENTIALLY_ACTIVE"));
244eda14cbcSMatt Macy 	}
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy 	return (gettext("UNKNOWN"));
247eda14cbcSMatt Macy }
248eda14cbcSMatt Macy 
249eda14cbcSMatt Macy /*
250eda14cbcSMatt Macy  * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
251eda14cbcSMatt Macy  * "SUSPENDED", etc).
252eda14cbcSMatt Macy  */
253eda14cbcSMatt Macy const char *
zpool_get_state_str(zpool_handle_t * zhp)254eda14cbcSMatt Macy zpool_get_state_str(zpool_handle_t *zhp)
255eda14cbcSMatt Macy {
256eda14cbcSMatt Macy 	zpool_errata_t errata;
257eda14cbcSMatt Macy 	zpool_status_t status;
258eda14cbcSMatt Macy 	const char *str;
259eda14cbcSMatt Macy 
260eda14cbcSMatt Macy 	status = zpool_get_status(zhp, NULL, &errata);
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
263eda14cbcSMatt Macy 		str = gettext("FAULTED");
264eda14cbcSMatt Macy 	} else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
265cbfe9975SMartin Matuska 	    status == ZPOOL_STATUS_IO_FAILURE_CONTINUE ||
266eda14cbcSMatt Macy 	    status == ZPOOL_STATUS_IO_FAILURE_MMP) {
267eda14cbcSMatt Macy 		str = gettext("SUSPENDED");
268eda14cbcSMatt Macy 	} else {
269da5137abSMartin Matuska 		nvlist_t *nvroot = fnvlist_lookup_nvlist(
270da5137abSMartin Matuska 		    zpool_get_config(zhp, NULL), ZPOOL_CONFIG_VDEV_TREE);
271da5137abSMartin Matuska 		uint_t vsc;
272da5137abSMartin Matuska 		vdev_stat_t *vs = (vdev_stat_t *)fnvlist_lookup_uint64_array(
273da5137abSMartin Matuska 		    nvroot, ZPOOL_CONFIG_VDEV_STATS, &vsc);
274eda14cbcSMatt Macy 		str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
275eda14cbcSMatt Macy 	}
276eda14cbcSMatt Macy 	return (str);
277eda14cbcSMatt Macy }
278eda14cbcSMatt Macy 
279eda14cbcSMatt Macy /*
280eda14cbcSMatt Macy  * Get a zpool property value for 'prop' and return the value in
281eda14cbcSMatt Macy  * a pre-allocated buffer.
282eda14cbcSMatt Macy  */
283eda14cbcSMatt Macy int
zpool_get_prop(zpool_handle_t * zhp,zpool_prop_t prop,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)284eda14cbcSMatt Macy zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
285eda14cbcSMatt Macy     size_t len, zprop_source_t *srctype, boolean_t literal)
286eda14cbcSMatt Macy {
287eda14cbcSMatt Macy 	uint64_t intval;
288eda14cbcSMatt Macy 	const char *strval;
289eda14cbcSMatt Macy 	zprop_source_t src = ZPROP_SRC_NONE;
290eda14cbcSMatt Macy 
291eda14cbcSMatt Macy 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
292eda14cbcSMatt Macy 		switch (prop) {
293eda14cbcSMatt Macy 		case ZPOOL_PROP_NAME:
294eda14cbcSMatt Macy 			(void) strlcpy(buf, zpool_get_name(zhp), len);
295eda14cbcSMatt Macy 			break;
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy 		case ZPOOL_PROP_HEALTH:
298eda14cbcSMatt Macy 			(void) strlcpy(buf, zpool_get_state_str(zhp), len);
299eda14cbcSMatt Macy 			break;
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 		case ZPOOL_PROP_GUID:
302eda14cbcSMatt Macy 			intval = zpool_get_prop_int(zhp, prop, &src);
303eda14cbcSMatt Macy 			(void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
304eda14cbcSMatt Macy 			break;
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 		case ZPOOL_PROP_ALTROOT:
307eda14cbcSMatt Macy 		case ZPOOL_PROP_CACHEFILE:
308eda14cbcSMatt Macy 		case ZPOOL_PROP_COMMENT:
309ee36e25aSMartin Matuska 		case ZPOOL_PROP_COMPATIBILITY:
310eda14cbcSMatt Macy 			if (zhp->zpool_props != NULL ||
311eda14cbcSMatt Macy 			    zpool_get_all_props(zhp) == 0) {
312eda14cbcSMatt Macy 				(void) strlcpy(buf,
313eda14cbcSMatt Macy 				    zpool_get_prop_string(zhp, prop, &src),
314eda14cbcSMatt Macy 				    len);
315eda14cbcSMatt Macy 				break;
316eda14cbcSMatt Macy 			}
317c03c5b1cSMartin Matuska 			zfs_fallthrough;
318eda14cbcSMatt Macy 		default:
319eda14cbcSMatt Macy 			(void) strlcpy(buf, "-", len);
320eda14cbcSMatt Macy 			break;
321eda14cbcSMatt Macy 		}
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy 		if (srctype != NULL)
324eda14cbcSMatt Macy 			*srctype = src;
325eda14cbcSMatt Macy 		return (0);
326eda14cbcSMatt Macy 	}
327eda14cbcSMatt Macy 
328ce4dcb97SMartin Matuska 	/*
329ce4dcb97SMartin Matuska 	 * ZPOOL_PROP_DEDUPCACHED can be fetched by name only using
330ce4dcb97SMartin Matuska 	 * the ZPOOL_GET_PROPS_NAMES mechanism
331ce4dcb97SMartin Matuska 	 */
332ce4dcb97SMartin Matuska 	if (prop == ZPOOL_PROP_DEDUPCACHED) {
333ce4dcb97SMartin Matuska 		zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME);
334ce4dcb97SMartin Matuska 		(void) zpool_get_all_props(zhp);
335ce4dcb97SMartin Matuska 	}
336ce4dcb97SMartin Matuska 
337eda14cbcSMatt Macy 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
338eda14cbcSMatt Macy 	    prop != ZPOOL_PROP_NAME)
339eda14cbcSMatt Macy 		return (-1);
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy 	switch (zpool_prop_get_type(prop)) {
342eda14cbcSMatt Macy 	case PROP_TYPE_STRING:
343eda14cbcSMatt Macy 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
344eda14cbcSMatt Macy 		    len);
345eda14cbcSMatt Macy 		break;
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	case PROP_TYPE_NUMBER:
348eda14cbcSMatt Macy 		intval = zpool_get_prop_int(zhp, prop, &src);
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy 		switch (prop) {
351ce4dcb97SMartin Matuska 		case ZPOOL_PROP_DEDUP_TABLE_QUOTA:
352ce4dcb97SMartin Matuska 			/*
353ce4dcb97SMartin Matuska 			 * If dedup quota is 0, we translate this into 'none'
354ce4dcb97SMartin Matuska 			 * (unless literal is set). And if it is UINT64_MAX
355ce4dcb97SMartin Matuska 			 * we translate that as 'automatic' (limit to size of
356ce4dcb97SMartin Matuska 			 * the dedicated dedup VDEV.  Otherwise, fall throught
357ce4dcb97SMartin Matuska 			 * into the regular number formating.
358ce4dcb97SMartin Matuska 			 */
359ce4dcb97SMartin Matuska 			if (intval == 0) {
360ce4dcb97SMartin Matuska 				(void) strlcpy(buf, literal ? "0" : "none",
361ce4dcb97SMartin Matuska 				    len);
362ce4dcb97SMartin Matuska 				break;
363ce4dcb97SMartin Matuska 			} else if (intval == UINT64_MAX) {
364ce4dcb97SMartin Matuska 				(void) strlcpy(buf, "auto", len);
365ce4dcb97SMartin Matuska 				break;
366ce4dcb97SMartin Matuska 			}
367ce4dcb97SMartin Matuska 			zfs_fallthrough;
368ce4dcb97SMartin Matuska 
369eda14cbcSMatt Macy 		case ZPOOL_PROP_SIZE:
370eda14cbcSMatt Macy 		case ZPOOL_PROP_ALLOCATED:
371eda14cbcSMatt Macy 		case ZPOOL_PROP_FREE:
372eda14cbcSMatt Macy 		case ZPOOL_PROP_FREEING:
373eda14cbcSMatt Macy 		case ZPOOL_PROP_LEAKED:
374eda14cbcSMatt Macy 		case ZPOOL_PROP_ASHIFT:
375c03c5b1cSMartin Matuska 		case ZPOOL_PROP_MAXBLOCKSIZE:
376c03c5b1cSMartin Matuska 		case ZPOOL_PROP_MAXDNODESIZE:
3772a58b312SMartin Matuska 		case ZPOOL_PROP_BCLONESAVED:
3782a58b312SMartin Matuska 		case ZPOOL_PROP_BCLONEUSED:
379ce4dcb97SMartin Matuska 		case ZPOOL_PROP_DEDUP_TABLE_SIZE:
380ce4dcb97SMartin Matuska 		case ZPOOL_PROP_DEDUPCACHED:
381eda14cbcSMatt Macy 			if (literal)
382eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu",
383eda14cbcSMatt Macy 				    (u_longlong_t)intval);
384eda14cbcSMatt Macy 			else
385eda14cbcSMatt Macy 				(void) zfs_nicenum(intval, buf, len);
386eda14cbcSMatt Macy 			break;
387eda14cbcSMatt Macy 
388eda14cbcSMatt Macy 		case ZPOOL_PROP_EXPANDSZ:
389eda14cbcSMatt Macy 		case ZPOOL_PROP_CHECKPOINT:
390eda14cbcSMatt Macy 			if (intval == 0) {
391eda14cbcSMatt Macy 				(void) strlcpy(buf, "-", len);
392eda14cbcSMatt Macy 			} else if (literal) {
393eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu",
394eda14cbcSMatt Macy 				    (u_longlong_t)intval);
395eda14cbcSMatt Macy 			} else {
396eda14cbcSMatt Macy 				(void) zfs_nicebytes(intval, buf, len);
397eda14cbcSMatt Macy 			}
398eda14cbcSMatt Macy 			break;
399eda14cbcSMatt Macy 
400eda14cbcSMatt Macy 		case ZPOOL_PROP_CAPACITY:
401eda14cbcSMatt Macy 			if (literal) {
402eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu",
403eda14cbcSMatt Macy 				    (u_longlong_t)intval);
404eda14cbcSMatt Macy 			} else {
405eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu%%",
406eda14cbcSMatt Macy 				    (u_longlong_t)intval);
407eda14cbcSMatt Macy 			}
408eda14cbcSMatt Macy 			break;
409eda14cbcSMatt Macy 
410eda14cbcSMatt Macy 		case ZPOOL_PROP_FRAGMENTATION:
411eda14cbcSMatt Macy 			if (intval == UINT64_MAX) {
412eda14cbcSMatt Macy 				(void) strlcpy(buf, "-", len);
413eda14cbcSMatt Macy 			} else if (literal) {
414eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu",
415eda14cbcSMatt Macy 				    (u_longlong_t)intval);
416eda14cbcSMatt Macy 			} else {
417eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu%%",
418eda14cbcSMatt Macy 				    (u_longlong_t)intval);
419eda14cbcSMatt Macy 			}
420eda14cbcSMatt Macy 			break;
421eda14cbcSMatt Macy 
4222a58b312SMartin Matuska 		case ZPOOL_PROP_BCLONERATIO:
423eda14cbcSMatt Macy 		case ZPOOL_PROP_DEDUPRATIO:
424eda14cbcSMatt Macy 			if (literal)
425eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu.%02llu",
426eda14cbcSMatt Macy 				    (u_longlong_t)(intval / 100),
427eda14cbcSMatt Macy 				    (u_longlong_t)(intval % 100));
428eda14cbcSMatt Macy 			else
429eda14cbcSMatt Macy 				(void) snprintf(buf, len, "%llu.%02llux",
430eda14cbcSMatt Macy 				    (u_longlong_t)(intval / 100),
431eda14cbcSMatt Macy 				    (u_longlong_t)(intval % 100));
432eda14cbcSMatt Macy 			break;
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy 		case ZPOOL_PROP_HEALTH:
435eda14cbcSMatt Macy 			(void) strlcpy(buf, zpool_get_state_str(zhp), len);
436eda14cbcSMatt Macy 			break;
437eda14cbcSMatt Macy 		case ZPOOL_PROP_VERSION:
438eda14cbcSMatt Macy 			if (intval >= SPA_VERSION_FEATURES) {
439eda14cbcSMatt Macy 				(void) snprintf(buf, len, "-");
440eda14cbcSMatt Macy 				break;
441eda14cbcSMatt Macy 			}
442c03c5b1cSMartin Matuska 			zfs_fallthrough;
443eda14cbcSMatt Macy 		default:
444eda14cbcSMatt Macy 			(void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
445eda14cbcSMatt Macy 		}
446eda14cbcSMatt Macy 		break;
447eda14cbcSMatt Macy 
448eda14cbcSMatt Macy 	case PROP_TYPE_INDEX:
449eda14cbcSMatt Macy 		intval = zpool_get_prop_int(zhp, prop, &src);
450eda14cbcSMatt Macy 		if (zpool_prop_index_to_string(prop, intval, &strval)
451eda14cbcSMatt Macy 		    != 0)
452eda14cbcSMatt Macy 			return (-1);
453eda14cbcSMatt Macy 		(void) strlcpy(buf, strval, len);
454eda14cbcSMatt Macy 		break;
455eda14cbcSMatt Macy 
456eda14cbcSMatt Macy 	default:
457eda14cbcSMatt Macy 		abort();
458eda14cbcSMatt Macy 	}
459eda14cbcSMatt Macy 
460eda14cbcSMatt Macy 	if (srctype)
461eda14cbcSMatt Macy 		*srctype = src;
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	return (0);
464eda14cbcSMatt Macy }
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy /*
467c98ecfceSAllan Jude  * Get a zpool property value for 'propname' and return the value in
468c98ecfceSAllan Jude  * a pre-allocated buffer.
469c98ecfceSAllan Jude  */
470c98ecfceSAllan Jude int
zpool_get_userprop(zpool_handle_t * zhp,const char * propname,char * buf,size_t len,zprop_source_t * srctype)471c98ecfceSAllan Jude zpool_get_userprop(zpool_handle_t *zhp, const char *propname, char *buf,
472c98ecfceSAllan Jude     size_t len, zprop_source_t *srctype)
473c98ecfceSAllan Jude {
474*5c65a0a9SMartin Matuska 	nvlist_t *nv;
475c98ecfceSAllan Jude 	uint64_t ival;
476c98ecfceSAllan Jude 	const char *value;
477c98ecfceSAllan Jude 	zprop_source_t source = ZPROP_SRC_LOCAL;
478c98ecfceSAllan Jude 
479*5c65a0a9SMartin Matuska 	if (zhp->zpool_props == NULL)
480*5c65a0a9SMartin Matuska 		zpool_get_all_props(zhp);
481*5c65a0a9SMartin Matuska 
482*5c65a0a9SMartin Matuska 	if (nvlist_lookup_nvlist(zhp->zpool_props, propname, &nv) == 0) {
483c98ecfceSAllan Jude 		if (nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0)
484c98ecfceSAllan Jude 			source = ival;
485c98ecfceSAllan Jude 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
486c98ecfceSAllan Jude 	} else {
487c98ecfceSAllan Jude 		source = ZPROP_SRC_DEFAULT;
488c98ecfceSAllan Jude 		value = "-";
489c98ecfceSAllan Jude 	}
490c98ecfceSAllan Jude 
491c98ecfceSAllan Jude 	if (srctype)
492c98ecfceSAllan Jude 		*srctype = source;
493c98ecfceSAllan Jude 
494c98ecfceSAllan Jude 	(void) strlcpy(buf, value, len);
495c98ecfceSAllan Jude 
496c98ecfceSAllan Jude 	return (0);
497c98ecfceSAllan Jude }
498c98ecfceSAllan Jude 
499c98ecfceSAllan Jude /*
500eda14cbcSMatt Macy  * Check if the bootfs name has the same pool name as it is set to.
501eda14cbcSMatt Macy  * Assuming bootfs is a valid dataset name.
502eda14cbcSMatt Macy  */
503eda14cbcSMatt Macy static boolean_t
bootfs_name_valid(const char * pool,const char * bootfs)504eda14cbcSMatt Macy bootfs_name_valid(const char *pool, const char *bootfs)
505eda14cbcSMatt Macy {
506eda14cbcSMatt Macy 	int len = strlen(pool);
507eda14cbcSMatt Macy 	if (bootfs[0] == '\0')
508eda14cbcSMatt Macy 		return (B_TRUE);
509eda14cbcSMatt Macy 
510eda14cbcSMatt Macy 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
511eda14cbcSMatt Macy 		return (B_FALSE);
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	if (strncmp(pool, bootfs, len) == 0 &&
514eda14cbcSMatt Macy 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
515eda14cbcSMatt Macy 		return (B_TRUE);
516eda14cbcSMatt Macy 
517eda14cbcSMatt Macy 	return (B_FALSE);
518eda14cbcSMatt Macy }
519eda14cbcSMatt Macy 
520eda14cbcSMatt Macy /*
521eda14cbcSMatt Macy  * Given an nvlist of zpool properties to be set, validate that they are
522eda14cbcSMatt Macy  * correct, and parse any numeric properties (index, boolean, etc) if they are
523eda14cbcSMatt Macy  * specified as strings.
524eda14cbcSMatt Macy  */
525eda14cbcSMatt Macy static nvlist_t *
zpool_valid_proplist(libzfs_handle_t * hdl,const char * poolname,nvlist_t * props,uint64_t version,prop_flags_t flags,char * errbuf)526eda14cbcSMatt Macy zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
527eda14cbcSMatt Macy     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
528eda14cbcSMatt Macy {
529eda14cbcSMatt Macy 	nvpair_t *elem;
530eda14cbcSMatt Macy 	nvlist_t *retprops;
531eda14cbcSMatt Macy 	zpool_prop_t prop;
5322a58b312SMartin Matuska 	const char *strval;
533eda14cbcSMatt Macy 	uint64_t intval;
534ce4dcb97SMartin Matuska 	const char *check;
535eda14cbcSMatt Macy 	struct stat64 statbuf;
536eda14cbcSMatt Macy 	zpool_handle_t *zhp;
537ce4dcb97SMartin Matuska 	char *parent, *slash;
53816038816SMartin Matuska 	char report[1024];
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
541eda14cbcSMatt Macy 		(void) no_memory(hdl);
542eda14cbcSMatt Macy 		return (NULL);
543eda14cbcSMatt Macy 	}
544eda14cbcSMatt Macy 
545eda14cbcSMatt Macy 	elem = NULL;
546eda14cbcSMatt Macy 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
547eda14cbcSMatt Macy 		const char *propname = nvpair_name(elem);
548eda14cbcSMatt Macy 
549681ce946SMartin Matuska 		if (flags.vdevprop && zpool_prop_vdev(propname)) {
550681ce946SMartin Matuska 			vdev_prop_t vprop = vdev_name_to_prop(propname);
551681ce946SMartin Matuska 
552681ce946SMartin Matuska 			if (vdev_prop_readonly(vprop)) {
553681ce946SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
554681ce946SMartin Matuska 				    "is readonly"), propname);
555681ce946SMartin Matuska 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
556681ce946SMartin Matuska 				    errbuf);
557681ce946SMartin Matuska 				goto error;
558681ce946SMartin Matuska 			}
559681ce946SMartin Matuska 
560681ce946SMartin Matuska 			if (zprop_parse_value(hdl, elem, vprop, ZFS_TYPE_VDEV,
561681ce946SMartin Matuska 			    retprops, &strval, &intval, errbuf) != 0)
562681ce946SMartin Matuska 				goto error;
563681ce946SMartin Matuska 
564681ce946SMartin Matuska 			continue;
565681ce946SMartin Matuska 		} else if (flags.vdevprop && vdev_prop_user(propname)) {
566681ce946SMartin Matuska 			if (nvlist_add_nvpair(retprops, elem) != 0) {
567681ce946SMartin Matuska 				(void) no_memory(hdl);
568681ce946SMartin Matuska 				goto error;
569681ce946SMartin Matuska 			}
570681ce946SMartin Matuska 			continue;
571681ce946SMartin Matuska 		} else if (flags.vdevprop) {
572681ce946SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
573681ce946SMartin Matuska 			    "invalid property: '%s'"), propname);
574681ce946SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
575681ce946SMartin Matuska 			goto error;
576681ce946SMartin Matuska 		}
577681ce946SMartin Matuska 
578eda14cbcSMatt Macy 		prop = zpool_name_to_prop(propname);
579eda14cbcSMatt Macy 		if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
580eda14cbcSMatt Macy 			int err;
581eda14cbcSMatt Macy 			char *fname = strchr(propname, '@') + 1;
582eda14cbcSMatt Macy 
583eda14cbcSMatt Macy 			err = zfeature_lookup_name(fname, NULL);
584eda14cbcSMatt Macy 			if (err != 0) {
585eda14cbcSMatt Macy 				ASSERT3U(err, ==, ENOENT);
586eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5877877fdebSMatt Macy 				    "feature '%s' unsupported by kernel"),
5887877fdebSMatt Macy 				    fname);
589eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
590eda14cbcSMatt Macy 				goto error;
591eda14cbcSMatt Macy 			}
592eda14cbcSMatt Macy 
593eda14cbcSMatt Macy 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
594eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
595eda14cbcSMatt Macy 				    "'%s' must be a string"), propname);
596eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
597eda14cbcSMatt Macy 				goto error;
598eda14cbcSMatt Macy 			}
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy 			(void) nvpair_value_string(elem, &strval);
601eda14cbcSMatt Macy 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
602eda14cbcSMatt Macy 			    strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
603eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
604eda14cbcSMatt Macy 				    "property '%s' can only be set to "
605eda14cbcSMatt Macy 				    "'enabled' or 'disabled'"), propname);
606eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
607eda14cbcSMatt Macy 				goto error;
608eda14cbcSMatt Macy 			}
609eda14cbcSMatt Macy 
610eda14cbcSMatt Macy 			if (!flags.create &&
611eda14cbcSMatt Macy 			    strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
612eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
613eda14cbcSMatt Macy 				    "property '%s' can only be set to "
614eda14cbcSMatt Macy 				    "'disabled' at creation time"), propname);
615eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
616eda14cbcSMatt Macy 				goto error;
617eda14cbcSMatt Macy 			}
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
620eda14cbcSMatt Macy 				(void) no_memory(hdl);
621eda14cbcSMatt Macy 				goto error;
622eda14cbcSMatt Macy 			}
623eda14cbcSMatt Macy 			continue;
624c98ecfceSAllan Jude 		} else if (prop == ZPOOL_PROP_INVAL &&
625c98ecfceSAllan Jude 		    zfs_prop_user(propname)) {
626c98ecfceSAllan Jude 			/*
627c98ecfceSAllan Jude 			 * This is a user property: make sure it's a
628c98ecfceSAllan Jude 			 * string, and that it's less than ZAP_MAXNAMELEN.
629c98ecfceSAllan Jude 			 */
630c98ecfceSAllan Jude 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
631c98ecfceSAllan Jude 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
632c98ecfceSAllan Jude 				    "'%s' must be a string"), propname);
633c98ecfceSAllan Jude 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
634c98ecfceSAllan Jude 				goto error;
635c98ecfceSAllan Jude 			}
636c98ecfceSAllan Jude 
637c98ecfceSAllan Jude 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
638c98ecfceSAllan Jude 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
639c98ecfceSAllan Jude 				    "property name '%s' is too long"),
640c98ecfceSAllan Jude 				    propname);
641c98ecfceSAllan Jude 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
642c98ecfceSAllan Jude 				goto error;
643c98ecfceSAllan Jude 			}
644c98ecfceSAllan Jude 
645c98ecfceSAllan Jude 			(void) nvpair_value_string(elem, &strval);
646c98ecfceSAllan Jude 
647c98ecfceSAllan Jude 			if (strlen(strval) >= ZFS_MAXPROPLEN) {
648c98ecfceSAllan Jude 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
649c98ecfceSAllan Jude 				    "property value '%s' is too long"),
650c98ecfceSAllan Jude 				    strval);
651c98ecfceSAllan Jude 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
652c98ecfceSAllan Jude 				goto error;
653c98ecfceSAllan Jude 			}
654c98ecfceSAllan Jude 
655c98ecfceSAllan Jude 			if (nvlist_add_string(retprops, propname,
656c98ecfceSAllan Jude 			    strval) != 0) {
657c98ecfceSAllan Jude 				(void) no_memory(hdl);
658c98ecfceSAllan Jude 				goto error;
659c98ecfceSAllan Jude 			}
660c98ecfceSAllan Jude 
661c98ecfceSAllan Jude 			continue;
662eda14cbcSMatt Macy 		}
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 		/*
665eda14cbcSMatt Macy 		 * Make sure this property is valid and applies to this type.
666eda14cbcSMatt Macy 		 */
667eda14cbcSMatt Macy 		if (prop == ZPOOL_PROP_INVAL) {
668eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
669eda14cbcSMatt Macy 			    "invalid property '%s'"), propname);
670eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
671eda14cbcSMatt Macy 			goto error;
672eda14cbcSMatt Macy 		}
673eda14cbcSMatt Macy 
674eda14cbcSMatt Macy 		if (zpool_prop_readonly(prop)) {
675eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
676eda14cbcSMatt Macy 			    "is readonly"), propname);
677eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
678eda14cbcSMatt Macy 			goto error;
679eda14cbcSMatt Macy 		}
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy 		if (!flags.create && zpool_prop_setonce(prop)) {
682eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
683eda14cbcSMatt Macy 			    "property '%s' can only be set at "
684eda14cbcSMatt Macy 			    "creation time"), propname);
685eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
686eda14cbcSMatt Macy 			goto error;
687eda14cbcSMatt Macy 		}
688eda14cbcSMatt Macy 
689eda14cbcSMatt Macy 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
690eda14cbcSMatt Macy 		    &strval, &intval, errbuf) != 0)
691eda14cbcSMatt Macy 			goto error;
692eda14cbcSMatt Macy 
693eda14cbcSMatt Macy 		/*
694eda14cbcSMatt Macy 		 * Perform additional checking for specific properties.
695eda14cbcSMatt Macy 		 */
696eda14cbcSMatt Macy 		switch (prop) {
697eda14cbcSMatt Macy 		case ZPOOL_PROP_VERSION:
698eda14cbcSMatt Macy 			if (intval < version ||
699eda14cbcSMatt Macy 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
700eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
70116038816SMartin Matuska 				    "property '%s' number %llu is invalid."),
70216038816SMartin Matuska 				    propname, (unsigned long long)intval);
703eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
704eda14cbcSMatt Macy 				goto error;
705eda14cbcSMatt Macy 			}
706eda14cbcSMatt Macy 			break;
707eda14cbcSMatt Macy 
708eda14cbcSMatt Macy 		case ZPOOL_PROP_ASHIFT:
709eda14cbcSMatt Macy 			if (intval != 0 &&
710eda14cbcSMatt Macy 			    (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
711eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71216038816SMartin Matuska 				    "property '%s' number %llu is invalid, "
71316038816SMartin Matuska 				    "only values between %" PRId32 " and %"
71416038816SMartin Matuska 				    PRId32 " are allowed."),
71516038816SMartin Matuska 				    propname, (unsigned long long)intval,
71616038816SMartin Matuska 				    ASHIFT_MIN, ASHIFT_MAX);
717eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
718eda14cbcSMatt Macy 				goto error;
719eda14cbcSMatt Macy 			}
720eda14cbcSMatt Macy 			break;
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy 		case ZPOOL_PROP_BOOTFS:
723eda14cbcSMatt Macy 			if (flags.create || flags.import) {
724eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
725eda14cbcSMatt Macy 				    "property '%s' cannot be set at creation "
726eda14cbcSMatt Macy 				    "or import time"), propname);
727eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
728eda14cbcSMatt Macy 				goto error;
729eda14cbcSMatt Macy 			}
730eda14cbcSMatt Macy 
731eda14cbcSMatt Macy 			if (version < SPA_VERSION_BOOTFS) {
732eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
733eda14cbcSMatt Macy 				    "pool must be upgraded to support "
734eda14cbcSMatt Macy 				    "'%s' property"), propname);
735eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
736eda14cbcSMatt Macy 				goto error;
737eda14cbcSMatt Macy 			}
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy 			/*
740eda14cbcSMatt Macy 			 * bootfs property value has to be a dataset name and
741eda14cbcSMatt Macy 			 * the dataset has to be in the same pool as it sets to.
742eda14cbcSMatt Macy 			 */
743eda14cbcSMatt Macy 			if (!bootfs_name_valid(poolname, strval)) {
744eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
745eda14cbcSMatt Macy 				    "is an invalid name"), strval);
746eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
747eda14cbcSMatt Macy 				goto error;
748eda14cbcSMatt Macy 			}
749eda14cbcSMatt Macy 
750eda14cbcSMatt Macy 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
751eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
752eda14cbcSMatt Macy 				    "could not open pool '%s'"), poolname);
753eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
754eda14cbcSMatt Macy 				goto error;
755eda14cbcSMatt Macy 			}
756eda14cbcSMatt Macy 			zpool_close(zhp);
757eda14cbcSMatt Macy 			break;
758eda14cbcSMatt Macy 
759eda14cbcSMatt Macy 		case ZPOOL_PROP_ALTROOT:
760eda14cbcSMatt Macy 			if (!flags.create && !flags.import) {
761eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
762eda14cbcSMatt Macy 				    "property '%s' can only be set during pool "
763eda14cbcSMatt Macy 				    "creation or import"), propname);
764eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
765eda14cbcSMatt Macy 				goto error;
766eda14cbcSMatt Macy 			}
767eda14cbcSMatt Macy 
768eda14cbcSMatt Macy 			if (strval[0] != '/') {
769eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
770eda14cbcSMatt Macy 				    "bad alternate root '%s'"), strval);
771eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
772eda14cbcSMatt Macy 				goto error;
773eda14cbcSMatt Macy 			}
774eda14cbcSMatt Macy 			break;
775eda14cbcSMatt Macy 
776eda14cbcSMatt Macy 		case ZPOOL_PROP_CACHEFILE:
777eda14cbcSMatt Macy 			if (strval[0] == '\0')
778eda14cbcSMatt Macy 				break;
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 			if (strcmp(strval, "none") == 0)
781eda14cbcSMatt Macy 				break;
782eda14cbcSMatt Macy 
783eda14cbcSMatt Macy 			if (strval[0] != '/') {
784eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
785eda14cbcSMatt Macy 				    "property '%s' must be empty, an "
786eda14cbcSMatt Macy 				    "absolute path, or 'none'"), propname);
787eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
788eda14cbcSMatt Macy 				goto error;
789eda14cbcSMatt Macy 			}
790eda14cbcSMatt Macy 
791ce4dcb97SMartin Matuska 			parent = strdup(strval);
792ce4dcb97SMartin Matuska 			if (parent == NULL) {
793ce4dcb97SMartin Matuska 				(void) zfs_error(hdl, EZFS_NOMEM, errbuf);
794ce4dcb97SMartin Matuska 				goto error;
795ce4dcb97SMartin Matuska 			}
796ce4dcb97SMartin Matuska 			slash = strrchr(parent, '/');
797eda14cbcSMatt Macy 
798eda14cbcSMatt Macy 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
799eda14cbcSMatt Macy 			    strcmp(slash, "/..") == 0) {
800eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
801ce4dcb97SMartin Matuska 				    "'%s' is not a valid file"), parent);
802eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
803ce4dcb97SMartin Matuska 				free(parent);
804eda14cbcSMatt Macy 				goto error;
805eda14cbcSMatt Macy 			}
806eda14cbcSMatt Macy 
807ce4dcb97SMartin Matuska 			*slash = '\0';
808eda14cbcSMatt Macy 
809ce4dcb97SMartin Matuska 			if (parent[0] != '\0' &&
810ce4dcb97SMartin Matuska 			    (stat64(parent, &statbuf) != 0 ||
811eda14cbcSMatt Macy 			    !S_ISDIR(statbuf.st_mode))) {
812eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
813eda14cbcSMatt Macy 				    "'%s' is not a valid directory"),
814ce4dcb97SMartin Matuska 				    parent);
815eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
816ce4dcb97SMartin Matuska 				free(parent);
817eda14cbcSMatt Macy 				goto error;
818eda14cbcSMatt Macy 			}
819ce4dcb97SMartin Matuska 			free(parent);
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 			break;
822eda14cbcSMatt Macy 
823ee36e25aSMartin Matuska 		case ZPOOL_PROP_COMPATIBILITY:
82416038816SMartin Matuska 			switch (zpool_load_compat(strval, NULL, report, 1024)) {
825ee36e25aSMartin Matuska 			case ZPOOL_COMPATIBILITY_OK:
82616038816SMartin Matuska 			case ZPOOL_COMPATIBILITY_WARNTOKEN:
827ee36e25aSMartin Matuska 				break;
828ee36e25aSMartin Matuska 			case ZPOOL_COMPATIBILITY_BADFILE:
82916038816SMartin Matuska 			case ZPOOL_COMPATIBILITY_BADTOKEN:
830ee36e25aSMartin Matuska 			case ZPOOL_COMPATIBILITY_NOFILES:
83116038816SMartin Matuska 				zfs_error_aux(hdl, "%s", report);
832ee36e25aSMartin Matuska 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
833ee36e25aSMartin Matuska 				goto error;
834ee36e25aSMartin Matuska 			}
835ee36e25aSMartin Matuska 			break;
836ee36e25aSMartin Matuska 
837eda14cbcSMatt Macy 		case ZPOOL_PROP_COMMENT:
838eda14cbcSMatt Macy 			for (check = strval; *check != '\0'; check++) {
839eda14cbcSMatt Macy 				if (!isprint(*check)) {
840eda14cbcSMatt Macy 					zfs_error_aux(hdl,
841eda14cbcSMatt Macy 					    dgettext(TEXT_DOMAIN,
842eda14cbcSMatt Macy 					    "comment may only have printable "
843eda14cbcSMatt Macy 					    "characters"));
844eda14cbcSMatt Macy 					(void) zfs_error(hdl, EZFS_BADPROP,
845eda14cbcSMatt Macy 					    errbuf);
846eda14cbcSMatt Macy 					goto error;
847eda14cbcSMatt Macy 				}
848eda14cbcSMatt Macy 			}
849eda14cbcSMatt Macy 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
850eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
851eda14cbcSMatt Macy 				    "comment must not exceed %d characters"),
852eda14cbcSMatt Macy 				    ZPROP_MAX_COMMENT);
853eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
854eda14cbcSMatt Macy 				goto error;
855eda14cbcSMatt Macy 			}
856eda14cbcSMatt Macy 			break;
857eda14cbcSMatt Macy 		case ZPOOL_PROP_READONLY:
858eda14cbcSMatt Macy 			if (!flags.import) {
859eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
860eda14cbcSMatt Macy 				    "property '%s' can only be set at "
861eda14cbcSMatt Macy 				    "import time"), propname);
862eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
863eda14cbcSMatt Macy 				goto error;
864eda14cbcSMatt Macy 			}
865eda14cbcSMatt Macy 			break;
866eda14cbcSMatt Macy 		case ZPOOL_PROP_MULTIHOST:
867eda14cbcSMatt Macy 			if (get_system_hostid() == 0) {
868eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
869eda14cbcSMatt Macy 				    "requires a non-zero system hostid"));
870eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
871eda14cbcSMatt Macy 				goto error;
872eda14cbcSMatt Macy 			}
873eda14cbcSMatt Macy 			break;
874eda14cbcSMatt Macy 		case ZPOOL_PROP_DEDUPDITTO:
875eda14cbcSMatt Macy 			printf("Note: property '%s' no longer has "
876eda14cbcSMatt Macy 			    "any effect\n", propname);
877eda14cbcSMatt Macy 			break;
878eda14cbcSMatt Macy 
879eda14cbcSMatt Macy 		default:
880eda14cbcSMatt Macy 			break;
881eda14cbcSMatt Macy 		}
882eda14cbcSMatt Macy 	}
883eda14cbcSMatt Macy 
884eda14cbcSMatt Macy 	return (retprops);
885eda14cbcSMatt Macy error:
886eda14cbcSMatt Macy 	nvlist_free(retprops);
887eda14cbcSMatt Macy 	return (NULL);
888eda14cbcSMatt Macy }
889eda14cbcSMatt Macy 
890eda14cbcSMatt Macy /*
891eda14cbcSMatt Macy  * Set zpool property : propname=propval.
892eda14cbcSMatt Macy  */
893eda14cbcSMatt Macy int
zpool_set_prop(zpool_handle_t * zhp,const char * propname,const char * propval)894eda14cbcSMatt Macy zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
895eda14cbcSMatt Macy {
896eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
897eda14cbcSMatt Macy 	int ret = -1;
8981f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
899eda14cbcSMatt Macy 	nvlist_t *nvl = NULL;
900eda14cbcSMatt Macy 	nvlist_t *realprops;
901eda14cbcSMatt Macy 	uint64_t version;
902eda14cbcSMatt Macy 	prop_flags_t flags = { 0 };
903eda14cbcSMatt Macy 
904eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf),
905eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
906eda14cbcSMatt Macy 	    zhp->zpool_name);
907eda14cbcSMatt Macy 
908eda14cbcSMatt Macy 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
909eda14cbcSMatt Macy 		return (no_memory(zhp->zpool_hdl));
910eda14cbcSMatt Macy 
911eda14cbcSMatt Macy 	if (nvlist_add_string(nvl, propname, propval) != 0) {
912eda14cbcSMatt Macy 		nvlist_free(nvl);
913eda14cbcSMatt Macy 		return (no_memory(zhp->zpool_hdl));
914eda14cbcSMatt Macy 	}
915eda14cbcSMatt Macy 
916eda14cbcSMatt Macy 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
917eda14cbcSMatt Macy 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
918eda14cbcSMatt Macy 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
919eda14cbcSMatt Macy 		nvlist_free(nvl);
920eda14cbcSMatt Macy 		return (-1);
921eda14cbcSMatt Macy 	}
922eda14cbcSMatt Macy 
923eda14cbcSMatt Macy 	nvlist_free(nvl);
924eda14cbcSMatt Macy 	nvl = realprops;
925eda14cbcSMatt Macy 
926eda14cbcSMatt Macy 	/*
927eda14cbcSMatt Macy 	 * Execute the corresponding ioctl() to set this property.
928eda14cbcSMatt Macy 	 */
929eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
930eda14cbcSMatt Macy 
931716fd348SMartin Matuska 	zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl);
932eda14cbcSMatt Macy 
933eda14cbcSMatt Macy 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
934eda14cbcSMatt Macy 
935eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
936eda14cbcSMatt Macy 	nvlist_free(nvl);
937eda14cbcSMatt Macy 
938eda14cbcSMatt Macy 	if (ret)
939eda14cbcSMatt Macy 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
940eda14cbcSMatt Macy 	else
941eda14cbcSMatt Macy 		(void) zpool_props_refresh(zhp);
942eda14cbcSMatt Macy 
943eda14cbcSMatt Macy 	return (ret);
944eda14cbcSMatt Macy }
945eda14cbcSMatt Macy 
946eda14cbcSMatt Macy int
zpool_expand_proplist(zpool_handle_t * zhp,zprop_list_t ** plp,zfs_type_t type,boolean_t literal)9477877fdebSMatt Macy zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp,
948681ce946SMartin Matuska     zfs_type_t type, boolean_t literal)
949eda14cbcSMatt Macy {
950eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
951eda14cbcSMatt Macy 	zprop_list_t *entry;
952eda14cbcSMatt Macy 	char buf[ZFS_MAXPROPLEN];
953eda14cbcSMatt Macy 	nvlist_t *features = NULL;
954eda14cbcSMatt Macy 	nvpair_t *nvp;
955eda14cbcSMatt Macy 	zprop_list_t **last;
956eda14cbcSMatt Macy 	boolean_t firstexpand = (NULL == *plp);
957eda14cbcSMatt Macy 	int i;
958eda14cbcSMatt Macy 
959681ce946SMartin Matuska 	if (zprop_expand_list(hdl, plp, type) != 0)
960eda14cbcSMatt Macy 		return (-1);
961eda14cbcSMatt Macy 
962681ce946SMartin Matuska 	if (type == ZFS_TYPE_VDEV)
963681ce946SMartin Matuska 		return (0);
964681ce946SMartin Matuska 
965eda14cbcSMatt Macy 	last = plp;
966eda14cbcSMatt Macy 	while (*last != NULL)
967eda14cbcSMatt Macy 		last = &(*last)->pl_next;
968eda14cbcSMatt Macy 
969eda14cbcSMatt Macy 	if ((*plp)->pl_all)
970eda14cbcSMatt Macy 		features = zpool_get_features(zhp);
971eda14cbcSMatt Macy 
972eda14cbcSMatt Macy 	if ((*plp)->pl_all && firstexpand) {
973c98ecfceSAllan Jude 		/* Handle userprops in the all properties case */
974c98ecfceSAllan Jude 		if (zhp->zpool_props == NULL && zpool_props_refresh(zhp))
975c98ecfceSAllan Jude 			return (-1);
976c98ecfceSAllan Jude 
977c98ecfceSAllan Jude 		nvp = NULL;
978c98ecfceSAllan Jude 		while ((nvp = nvlist_next_nvpair(zhp->zpool_props, nvp)) !=
979c98ecfceSAllan Jude 		    NULL) {
980c98ecfceSAllan Jude 			const char *propname = nvpair_name(nvp);
981c98ecfceSAllan Jude 
982c98ecfceSAllan Jude 			if (!zfs_prop_user(propname))
983c98ecfceSAllan Jude 				continue;
984c98ecfceSAllan Jude 
985c98ecfceSAllan Jude 			entry = zfs_alloc(hdl, sizeof (zprop_list_t));
986c98ecfceSAllan Jude 			entry->pl_prop = ZPROP_USERPROP;
987c98ecfceSAllan Jude 			entry->pl_user_prop = zfs_strdup(hdl, propname);
988c98ecfceSAllan Jude 			entry->pl_width = strlen(entry->pl_user_prop);
989c98ecfceSAllan Jude 			entry->pl_all = B_TRUE;
990c98ecfceSAllan Jude 
991c98ecfceSAllan Jude 			*last = entry;
992c98ecfceSAllan Jude 			last = &entry->pl_next;
993c98ecfceSAllan Jude 		}
994c98ecfceSAllan Jude 
995eda14cbcSMatt Macy 		for (i = 0; i < SPA_FEATURES; i++) {
996c98ecfceSAllan Jude 			entry = zfs_alloc(hdl, sizeof (zprop_list_t));
9971f1e2261SMartin Matuska 			entry->pl_prop = ZPROP_USERPROP;
998eda14cbcSMatt Macy 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
999eda14cbcSMatt Macy 			    spa_feature_table[i].fi_uname);
1000eda14cbcSMatt Macy 			entry->pl_width = strlen(entry->pl_user_prop);
1001eda14cbcSMatt Macy 			entry->pl_all = B_TRUE;
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy 			*last = entry;
1004eda14cbcSMatt Macy 			last = &entry->pl_next;
1005eda14cbcSMatt Macy 		}
1006eda14cbcSMatt Macy 	}
1007eda14cbcSMatt Macy 
1008eda14cbcSMatt Macy 	/* add any unsupported features */
1009eda14cbcSMatt Macy 	for (nvp = nvlist_next_nvpair(features, NULL);
1010eda14cbcSMatt Macy 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
1011eda14cbcSMatt Macy 		char *propname;
1012eda14cbcSMatt Macy 		boolean_t found;
1013eda14cbcSMatt Macy 
1014eda14cbcSMatt Macy 		if (zfeature_is_supported(nvpair_name(nvp)))
1015eda14cbcSMatt Macy 			continue;
1016eda14cbcSMatt Macy 
1017eda14cbcSMatt Macy 		propname = zfs_asprintf(hdl, "unsupported@%s",
1018eda14cbcSMatt Macy 		    nvpair_name(nvp));
1019eda14cbcSMatt Macy 
1020eda14cbcSMatt Macy 		/*
1021eda14cbcSMatt Macy 		 * Before adding the property to the list make sure that no
1022eda14cbcSMatt Macy 		 * other pool already added the same property.
1023eda14cbcSMatt Macy 		 */
1024eda14cbcSMatt Macy 		found = B_FALSE;
1025eda14cbcSMatt Macy 		entry = *plp;
1026eda14cbcSMatt Macy 		while (entry != NULL) {
1027eda14cbcSMatt Macy 			if (entry->pl_user_prop != NULL &&
1028eda14cbcSMatt Macy 			    strcmp(propname, entry->pl_user_prop) == 0) {
1029eda14cbcSMatt Macy 				found = B_TRUE;
1030eda14cbcSMatt Macy 				break;
1031eda14cbcSMatt Macy 			}
1032eda14cbcSMatt Macy 			entry = entry->pl_next;
1033eda14cbcSMatt Macy 		}
1034eda14cbcSMatt Macy 		if (found) {
1035eda14cbcSMatt Macy 			free(propname);
1036eda14cbcSMatt Macy 			continue;
1037eda14cbcSMatt Macy 		}
1038eda14cbcSMatt Macy 
1039eda14cbcSMatt Macy 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
10401f1e2261SMartin Matuska 		entry->pl_prop = ZPROP_USERPROP;
1041eda14cbcSMatt Macy 		entry->pl_user_prop = propname;
1042eda14cbcSMatt Macy 		entry->pl_width = strlen(entry->pl_user_prop);
1043eda14cbcSMatt Macy 		entry->pl_all = B_TRUE;
1044eda14cbcSMatt Macy 
1045eda14cbcSMatt Macy 		*last = entry;
1046eda14cbcSMatt Macy 		last = &entry->pl_next;
1047eda14cbcSMatt Macy 	}
1048eda14cbcSMatt Macy 
1049eda14cbcSMatt Macy 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
10507877fdebSMatt Macy 		if (entry->pl_fixed && !literal)
1051eda14cbcSMatt Macy 			continue;
1052eda14cbcSMatt Macy 
10531f1e2261SMartin Matuska 		if (entry->pl_prop != ZPROP_USERPROP &&
1054eda14cbcSMatt Macy 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
10557877fdebSMatt Macy 		    NULL, literal) == 0) {
1056eda14cbcSMatt Macy 			if (strlen(buf) > entry->pl_width)
1057eda14cbcSMatt Macy 				entry->pl_width = strlen(buf);
1058c98ecfceSAllan Jude 		} else if (entry->pl_prop == ZPROP_INVAL &&
1059c98ecfceSAllan Jude 		    zfs_prop_user(entry->pl_user_prop) &&
1060c98ecfceSAllan Jude 		    zpool_get_userprop(zhp, entry->pl_user_prop, buf,
1061c98ecfceSAllan Jude 		    sizeof (buf), NULL) == 0) {
1062c98ecfceSAllan Jude 			if (strlen(buf) > entry->pl_width)
1063c98ecfceSAllan Jude 				entry->pl_width = strlen(buf);
1064eda14cbcSMatt Macy 		}
1065eda14cbcSMatt Macy 	}
1066eda14cbcSMatt Macy 
1067eda14cbcSMatt Macy 	return (0);
1068eda14cbcSMatt Macy }
1069eda14cbcSMatt Macy 
1070681ce946SMartin Matuska int
vdev_expand_proplist(zpool_handle_t * zhp,const char * vdevname,zprop_list_t ** plp)1071681ce946SMartin Matuska vdev_expand_proplist(zpool_handle_t *zhp, const char *vdevname,
1072681ce946SMartin Matuska     zprop_list_t **plp)
1073681ce946SMartin Matuska {
1074681ce946SMartin Matuska 	zprop_list_t *entry;
1075681ce946SMartin Matuska 	char buf[ZFS_MAXPROPLEN];
10762a58b312SMartin Matuska 	const char *strval = NULL;
1077681ce946SMartin Matuska 	int err = 0;
1078681ce946SMartin Matuska 	nvpair_t *elem = NULL;
1079681ce946SMartin Matuska 	nvlist_t *vprops = NULL;
1080681ce946SMartin Matuska 	nvlist_t *propval = NULL;
1081681ce946SMartin Matuska 	const char *propname;
1082681ce946SMartin Matuska 	vdev_prop_t prop;
1083681ce946SMartin Matuska 	zprop_list_t **last;
1084681ce946SMartin Matuska 
1085681ce946SMartin Matuska 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
1086681ce946SMartin Matuska 		if (entry->pl_fixed)
1087681ce946SMartin Matuska 			continue;
1088681ce946SMartin Matuska 
1089681ce946SMartin Matuska 		if (zpool_get_vdev_prop(zhp, vdevname, entry->pl_prop,
1090681ce946SMartin Matuska 		    entry->pl_user_prop, buf, sizeof (buf), NULL,
1091681ce946SMartin Matuska 		    B_FALSE) == 0) {
1092681ce946SMartin Matuska 			if (strlen(buf) > entry->pl_width)
1093681ce946SMartin Matuska 				entry->pl_width = strlen(buf);
1094681ce946SMartin Matuska 		}
1095681ce946SMartin Matuska 		if (entry->pl_prop == VDEV_PROP_NAME &&
1096681ce946SMartin Matuska 		    strlen(vdevname) > entry->pl_width)
1097681ce946SMartin Matuska 			entry->pl_width = strlen(vdevname);
1098681ce946SMartin Matuska 	}
1099681ce946SMartin Matuska 
1100681ce946SMartin Matuska 	/* Handle the all properties case */
1101681ce946SMartin Matuska 	last = plp;
1102681ce946SMartin Matuska 	if (*last != NULL && (*last)->pl_all == B_TRUE) {
1103681ce946SMartin Matuska 		while (*last != NULL)
1104681ce946SMartin Matuska 			last = &(*last)->pl_next;
1105681ce946SMartin Matuska 
1106681ce946SMartin Matuska 		err = zpool_get_all_vdev_props(zhp, vdevname, &vprops);
1107681ce946SMartin Matuska 		if (err != 0)
1108681ce946SMartin Matuska 			return (err);
1109681ce946SMartin Matuska 
1110681ce946SMartin Matuska 		while ((elem = nvlist_next_nvpair(vprops, elem)) != NULL) {
1111681ce946SMartin Matuska 			propname = nvpair_name(elem);
1112681ce946SMartin Matuska 
1113681ce946SMartin Matuska 			/* Skip properties that are not user defined */
1114681ce946SMartin Matuska 			if ((prop = vdev_name_to_prop(propname)) !=
11151f1e2261SMartin Matuska 			    VDEV_PROP_USERPROP)
1116681ce946SMartin Matuska 				continue;
1117681ce946SMartin Matuska 
1118681ce946SMartin Matuska 			if (nvpair_value_nvlist(elem, &propval) != 0)
1119681ce946SMartin Matuska 				continue;
1120681ce946SMartin Matuska 
1121da5137abSMartin Matuska 			strval = fnvlist_lookup_string(propval, ZPROP_VALUE);
1122681ce946SMartin Matuska 
1123716fd348SMartin Matuska 			entry = zfs_alloc(zhp->zpool_hdl,
1124716fd348SMartin Matuska 			    sizeof (zprop_list_t));
1125681ce946SMartin Matuska 			entry->pl_prop = prop;
1126681ce946SMartin Matuska 			entry->pl_user_prop = zfs_strdup(zhp->zpool_hdl,
1127681ce946SMartin Matuska 			    propname);
1128681ce946SMartin Matuska 			entry->pl_width = strlen(strval);
1129681ce946SMartin Matuska 			entry->pl_all = B_TRUE;
1130681ce946SMartin Matuska 			*last = entry;
1131681ce946SMartin Matuska 			last = &entry->pl_next;
1132681ce946SMartin Matuska 		}
1133681ce946SMartin Matuska 	}
1134681ce946SMartin Matuska 
1135681ce946SMartin Matuska 	return (0);
1136681ce946SMartin Matuska }
1137681ce946SMartin Matuska 
1138eda14cbcSMatt Macy /*
1139eda14cbcSMatt Macy  * Get the state for the given feature on the given ZFS pool.
1140eda14cbcSMatt Macy  */
1141eda14cbcSMatt Macy int
zpool_prop_get_feature(zpool_handle_t * zhp,const char * propname,char * buf,size_t len)1142eda14cbcSMatt Macy zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
1143eda14cbcSMatt Macy     size_t len)
1144eda14cbcSMatt Macy {
1145eda14cbcSMatt Macy 	uint64_t refcount;
1146eda14cbcSMatt Macy 	boolean_t found = B_FALSE;
1147eda14cbcSMatt Macy 	nvlist_t *features = zpool_get_features(zhp);
1148eda14cbcSMatt Macy 	boolean_t supported;
1149eda14cbcSMatt Macy 	const char *feature = strchr(propname, '@') + 1;
1150eda14cbcSMatt Macy 
1151eda14cbcSMatt Macy 	supported = zpool_prop_feature(propname);
1152eda14cbcSMatt Macy 	ASSERT(supported || zpool_prop_unsupported(propname));
1153eda14cbcSMatt Macy 
1154eda14cbcSMatt Macy 	/*
1155eda14cbcSMatt Macy 	 * Convert from feature name to feature guid. This conversion is
1156eda14cbcSMatt Macy 	 * unnecessary for unsupported@... properties because they already
1157eda14cbcSMatt Macy 	 * use guids.
1158eda14cbcSMatt Macy 	 */
1159eda14cbcSMatt Macy 	if (supported) {
1160eda14cbcSMatt Macy 		int ret;
1161eda14cbcSMatt Macy 		spa_feature_t fid;
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy 		ret = zfeature_lookup_name(feature, &fid);
1164eda14cbcSMatt Macy 		if (ret != 0) {
1165eda14cbcSMatt Macy 			(void) strlcpy(buf, "-", len);
1166eda14cbcSMatt Macy 			return (ENOTSUP);
1167eda14cbcSMatt Macy 		}
1168eda14cbcSMatt Macy 		feature = spa_feature_table[fid].fi_guid;
1169eda14cbcSMatt Macy 	}
1170eda14cbcSMatt Macy 
1171eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
1172eda14cbcSMatt Macy 		found = B_TRUE;
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy 	if (supported) {
1175eda14cbcSMatt Macy 		if (!found) {
1176eda14cbcSMatt Macy 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
1177eda14cbcSMatt Macy 		} else  {
1178eda14cbcSMatt Macy 			if (refcount == 0)
1179eda14cbcSMatt Macy 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
1180eda14cbcSMatt Macy 			else
1181eda14cbcSMatt Macy 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
1182eda14cbcSMatt Macy 		}
1183eda14cbcSMatt Macy 	} else {
1184eda14cbcSMatt Macy 		if (found) {
1185eda14cbcSMatt Macy 			if (refcount == 0) {
1186eda14cbcSMatt Macy 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
1187eda14cbcSMatt Macy 			} else {
1188eda14cbcSMatt Macy 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
1189eda14cbcSMatt Macy 			}
1190eda14cbcSMatt Macy 		} else {
1191eda14cbcSMatt Macy 			(void) strlcpy(buf, "-", len);
1192eda14cbcSMatt Macy 			return (ENOTSUP);
1193eda14cbcSMatt Macy 		}
1194eda14cbcSMatt Macy 	}
1195eda14cbcSMatt Macy 
1196eda14cbcSMatt Macy 	return (0);
1197eda14cbcSMatt Macy }
1198eda14cbcSMatt Macy 
1199eda14cbcSMatt Macy /*
1200eda14cbcSMatt Macy  * Validate the given pool name, optionally putting an extended error message in
1201eda14cbcSMatt Macy  * 'buf'.
1202eda14cbcSMatt Macy  */
1203eda14cbcSMatt Macy boolean_t
zpool_name_valid(libzfs_handle_t * hdl,boolean_t isopen,const char * pool)1204eda14cbcSMatt Macy zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
1205eda14cbcSMatt Macy {
1206eda14cbcSMatt Macy 	namecheck_err_t why;
1207eda14cbcSMatt Macy 	char what;
1208eda14cbcSMatt Macy 	int ret;
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy 	ret = pool_namecheck(pool, &why, &what);
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy 	/*
1213eda14cbcSMatt Macy 	 * The rules for reserved pool names were extended at a later point.
1214eda14cbcSMatt Macy 	 * But we need to support users with existing pools that may now be
1215eda14cbcSMatt Macy 	 * invalid.  So we only check for this expanded set of names during a
1216eda14cbcSMatt Macy 	 * create (or import), and only in userland.
1217eda14cbcSMatt Macy 	 */
1218eda14cbcSMatt Macy 	if (ret == 0 && !isopen &&
1219eda14cbcSMatt Macy 	    (strncmp(pool, "mirror", 6) == 0 ||
1220eda14cbcSMatt Macy 	    strncmp(pool, "raidz", 5) == 0 ||
12217877fdebSMatt Macy 	    strncmp(pool, "draid", 5) == 0 ||
1222eda14cbcSMatt Macy 	    strncmp(pool, "spare", 5) == 0 ||
1223eda14cbcSMatt Macy 	    strcmp(pool, "log") == 0)) {
1224eda14cbcSMatt Macy 		if (hdl != NULL)
1225eda14cbcSMatt Macy 			zfs_error_aux(hdl,
1226eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "name is reserved"));
1227eda14cbcSMatt Macy 		return (B_FALSE);
1228eda14cbcSMatt Macy 	}
1229eda14cbcSMatt Macy 
1230eda14cbcSMatt Macy 
1231eda14cbcSMatt Macy 	if (ret != 0) {
1232eda14cbcSMatt Macy 		if (hdl != NULL) {
1233eda14cbcSMatt Macy 			switch (why) {
1234eda14cbcSMatt Macy 			case NAME_ERR_TOOLONG:
1235eda14cbcSMatt Macy 				zfs_error_aux(hdl,
1236eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN, "name is too long"));
1237eda14cbcSMatt Macy 				break;
1238eda14cbcSMatt Macy 
1239eda14cbcSMatt Macy 			case NAME_ERR_INVALCHAR:
1240eda14cbcSMatt Macy 				zfs_error_aux(hdl,
1241eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN, "invalid character "
1242eda14cbcSMatt Macy 				    "'%c' in pool name"), what);
1243eda14cbcSMatt Macy 				break;
1244eda14cbcSMatt Macy 
1245eda14cbcSMatt Macy 			case NAME_ERR_NOLETTER:
1246eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1247eda14cbcSMatt Macy 				    "name must begin with a letter"));
1248eda14cbcSMatt Macy 				break;
1249eda14cbcSMatt Macy 
1250eda14cbcSMatt Macy 			case NAME_ERR_RESERVED:
1251eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1252eda14cbcSMatt Macy 				    "name is reserved"));
1253eda14cbcSMatt Macy 				break;
1254eda14cbcSMatt Macy 
1255eda14cbcSMatt Macy 			case NAME_ERR_DISKLIKE:
1256eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1257eda14cbcSMatt Macy 				    "pool name is reserved"));
1258eda14cbcSMatt Macy 				break;
1259eda14cbcSMatt Macy 
1260eda14cbcSMatt Macy 			case NAME_ERR_LEADING_SLASH:
1261eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262eda14cbcSMatt Macy 				    "leading slash in name"));
1263eda14cbcSMatt Macy 				break;
1264eda14cbcSMatt Macy 
1265eda14cbcSMatt Macy 			case NAME_ERR_EMPTY_COMPONENT:
1266eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1267eda14cbcSMatt Macy 				    "empty component in name"));
1268eda14cbcSMatt Macy 				break;
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy 			case NAME_ERR_TRAILING_SLASH:
1271eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1272eda14cbcSMatt Macy 				    "trailing slash in name"));
1273eda14cbcSMatt Macy 				break;
1274eda14cbcSMatt Macy 
1275eda14cbcSMatt Macy 			case NAME_ERR_MULTIPLE_DELIMITERS:
1276eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277eda14cbcSMatt Macy 				    "multiple '@' and/or '#' delimiters in "
1278eda14cbcSMatt Macy 				    "name"));
1279eda14cbcSMatt Macy 				break;
1280eda14cbcSMatt Macy 
1281eda14cbcSMatt Macy 			case NAME_ERR_NO_AT:
1282eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1283eda14cbcSMatt Macy 				    "permission set is missing '@'"));
1284eda14cbcSMatt Macy 				break;
1285eda14cbcSMatt Macy 
1286eda14cbcSMatt Macy 			default:
1287eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1288eda14cbcSMatt Macy 				    "(%d) not defined"), why);
1289eda14cbcSMatt Macy 				break;
1290eda14cbcSMatt Macy 			}
1291eda14cbcSMatt Macy 		}
1292eda14cbcSMatt Macy 		return (B_FALSE);
1293eda14cbcSMatt Macy 	}
1294eda14cbcSMatt Macy 
1295eda14cbcSMatt Macy 	return (B_TRUE);
1296eda14cbcSMatt Macy }
1297eda14cbcSMatt Macy 
1298eda14cbcSMatt Macy /*
1299eda14cbcSMatt Macy  * Open a handle to the given pool, even if the pool is currently in the FAULTED
1300eda14cbcSMatt Macy  * state.
1301eda14cbcSMatt Macy  */
1302eda14cbcSMatt Macy zpool_handle_t *
zpool_open_canfail(libzfs_handle_t * hdl,const char * pool)1303eda14cbcSMatt Macy zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1304eda14cbcSMatt Macy {
1305eda14cbcSMatt Macy 	zpool_handle_t *zhp;
1306eda14cbcSMatt Macy 	boolean_t missing;
1307eda14cbcSMatt Macy 
1308eda14cbcSMatt Macy 	/*
1309eda14cbcSMatt Macy 	 * Make sure the pool name is valid.
1310eda14cbcSMatt Macy 	 */
1311eda14cbcSMatt Macy 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1312eda14cbcSMatt Macy 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1313eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1314eda14cbcSMatt Macy 		    pool);
1315eda14cbcSMatt Macy 		return (NULL);
1316eda14cbcSMatt Macy 	}
1317eda14cbcSMatt Macy 
1318716fd348SMartin Matuska 	zhp = zfs_alloc(hdl, sizeof (zpool_handle_t));
1319eda14cbcSMatt Macy 
1320eda14cbcSMatt Macy 	zhp->zpool_hdl = hdl;
1321eda14cbcSMatt Macy 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1322eda14cbcSMatt Macy 
1323eda14cbcSMatt Macy 	if (zpool_refresh_stats(zhp, &missing) != 0) {
1324eda14cbcSMatt Macy 		zpool_close(zhp);
1325eda14cbcSMatt Macy 		return (NULL);
1326eda14cbcSMatt Macy 	}
1327eda14cbcSMatt Macy 
1328eda14cbcSMatt Macy 	if (missing) {
1329eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1330eda14cbcSMatt Macy 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
1331eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1332eda14cbcSMatt Macy 		zpool_close(zhp);
1333eda14cbcSMatt Macy 		return (NULL);
1334eda14cbcSMatt Macy 	}
1335eda14cbcSMatt Macy 
1336eda14cbcSMatt Macy 	return (zhp);
1337eda14cbcSMatt Macy }
1338eda14cbcSMatt Macy 
1339eda14cbcSMatt Macy /*
1340eda14cbcSMatt Macy  * Like the above, but silent on error.  Used when iterating over pools (because
1341eda14cbcSMatt Macy  * the configuration cache may be out of date).
1342eda14cbcSMatt Macy  */
1343eda14cbcSMatt Macy int
zpool_open_silent(libzfs_handle_t * hdl,const char * pool,zpool_handle_t ** ret)1344eda14cbcSMatt Macy zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1345eda14cbcSMatt Macy {
1346eda14cbcSMatt Macy 	zpool_handle_t *zhp;
1347eda14cbcSMatt Macy 	boolean_t missing;
1348eda14cbcSMatt Macy 
1349716fd348SMartin Matuska 	zhp = zfs_alloc(hdl, sizeof (zpool_handle_t));
1350eda14cbcSMatt Macy 
1351eda14cbcSMatt Macy 	zhp->zpool_hdl = hdl;
1352eda14cbcSMatt Macy 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1353eda14cbcSMatt Macy 
1354eda14cbcSMatt Macy 	if (zpool_refresh_stats(zhp, &missing) != 0) {
1355eda14cbcSMatt Macy 		zpool_close(zhp);
1356eda14cbcSMatt Macy 		return (-1);
1357eda14cbcSMatt Macy 	}
1358eda14cbcSMatt Macy 
1359eda14cbcSMatt Macy 	if (missing) {
1360eda14cbcSMatt Macy 		zpool_close(zhp);
1361eda14cbcSMatt Macy 		*ret = NULL;
1362eda14cbcSMatt Macy 		return (0);
1363eda14cbcSMatt Macy 	}
1364eda14cbcSMatt Macy 
1365eda14cbcSMatt Macy 	*ret = zhp;
1366eda14cbcSMatt Macy 	return (0);
1367eda14cbcSMatt Macy }
1368eda14cbcSMatt Macy 
1369eda14cbcSMatt Macy /*
1370eda14cbcSMatt Macy  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1371eda14cbcSMatt Macy  * state.
1372eda14cbcSMatt Macy  */
1373eda14cbcSMatt Macy zpool_handle_t *
zpool_open(libzfs_handle_t * hdl,const char * pool)1374eda14cbcSMatt Macy zpool_open(libzfs_handle_t *hdl, const char *pool)
1375eda14cbcSMatt Macy {
1376eda14cbcSMatt Macy 	zpool_handle_t *zhp;
1377eda14cbcSMatt Macy 
1378eda14cbcSMatt Macy 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1379eda14cbcSMatt Macy 		return (NULL);
1380eda14cbcSMatt Macy 
1381eda14cbcSMatt Macy 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1382eda14cbcSMatt Macy 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1383eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1384eda14cbcSMatt Macy 		zpool_close(zhp);
1385eda14cbcSMatt Macy 		return (NULL);
1386eda14cbcSMatt Macy 	}
1387eda14cbcSMatt Macy 
1388eda14cbcSMatt Macy 	return (zhp);
1389eda14cbcSMatt Macy }
1390eda14cbcSMatt Macy 
1391eda14cbcSMatt Macy /*
1392eda14cbcSMatt Macy  * Close the handle.  Simply frees the memory associated with the handle.
1393eda14cbcSMatt Macy  */
1394eda14cbcSMatt Macy void
zpool_close(zpool_handle_t * zhp)1395eda14cbcSMatt Macy zpool_close(zpool_handle_t *zhp)
1396eda14cbcSMatt Macy {
1397eda14cbcSMatt Macy 	nvlist_free(zhp->zpool_config);
1398eda14cbcSMatt Macy 	nvlist_free(zhp->zpool_old_config);
1399eda14cbcSMatt Macy 	nvlist_free(zhp->zpool_props);
1400eda14cbcSMatt Macy 	free(zhp);
1401eda14cbcSMatt Macy }
1402eda14cbcSMatt Macy 
1403eda14cbcSMatt Macy /*
1404eda14cbcSMatt Macy  * Return the name of the pool.
1405eda14cbcSMatt Macy  */
1406eda14cbcSMatt Macy const char *
zpool_get_name(zpool_handle_t * zhp)1407eda14cbcSMatt Macy zpool_get_name(zpool_handle_t *zhp)
1408eda14cbcSMatt Macy {
1409eda14cbcSMatt Macy 	return (zhp->zpool_name);
1410eda14cbcSMatt Macy }
1411eda14cbcSMatt Macy 
1412eda14cbcSMatt Macy 
1413eda14cbcSMatt Macy /*
1414eda14cbcSMatt Macy  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1415eda14cbcSMatt Macy  */
1416eda14cbcSMatt Macy int
zpool_get_state(zpool_handle_t * zhp)1417eda14cbcSMatt Macy zpool_get_state(zpool_handle_t *zhp)
1418eda14cbcSMatt Macy {
1419eda14cbcSMatt Macy 	return (zhp->zpool_state);
1420eda14cbcSMatt Macy }
1421eda14cbcSMatt Macy 
1422eda14cbcSMatt Macy /*
1423eda14cbcSMatt Macy  * Check if vdev list contains a special vdev
1424eda14cbcSMatt Macy  */
1425eda14cbcSMatt Macy static boolean_t
zpool_has_special_vdev(nvlist_t * nvroot)1426eda14cbcSMatt Macy zpool_has_special_vdev(nvlist_t *nvroot)
1427eda14cbcSMatt Macy {
1428eda14cbcSMatt Macy 	nvlist_t **child;
1429eda14cbcSMatt Macy 	uint_t children;
1430eda14cbcSMatt Macy 
1431eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1432eda14cbcSMatt Macy 	    &children) == 0) {
1433eda14cbcSMatt Macy 		for (uint_t c = 0; c < children; c++) {
14342a58b312SMartin Matuska 			const char *bias;
1435eda14cbcSMatt Macy 
1436eda14cbcSMatt Macy 			if (nvlist_lookup_string(child[c],
1437eda14cbcSMatt Macy 			    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1438eda14cbcSMatt Macy 			    strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1439eda14cbcSMatt Macy 				return (B_TRUE);
1440eda14cbcSMatt Macy 			}
1441eda14cbcSMatt Macy 		}
1442eda14cbcSMatt Macy 	}
1443eda14cbcSMatt Macy 	return (B_FALSE);
1444eda14cbcSMatt Macy }
1445eda14cbcSMatt Macy 
1446eda14cbcSMatt Macy /*
1447184c1b94SMartin Matuska  * Check if vdev list contains a dRAID vdev
1448184c1b94SMartin Matuska  */
1449184c1b94SMartin Matuska static boolean_t
zpool_has_draid_vdev(nvlist_t * nvroot)1450184c1b94SMartin Matuska zpool_has_draid_vdev(nvlist_t *nvroot)
1451184c1b94SMartin Matuska {
1452184c1b94SMartin Matuska 	nvlist_t **child;
1453184c1b94SMartin Matuska 	uint_t children;
1454184c1b94SMartin Matuska 
1455184c1b94SMartin Matuska 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1456184c1b94SMartin Matuska 	    &child, &children) == 0) {
1457184c1b94SMartin Matuska 		for (uint_t c = 0; c < children; c++) {
14582a58b312SMartin Matuska 			const char *type;
1459184c1b94SMartin Matuska 
1460184c1b94SMartin Matuska 			if (nvlist_lookup_string(child[c],
1461184c1b94SMartin Matuska 			    ZPOOL_CONFIG_TYPE, &type) == 0 &&
1462184c1b94SMartin Matuska 			    strcmp(type, VDEV_TYPE_DRAID) == 0) {
1463184c1b94SMartin Matuska 				return (B_TRUE);
1464184c1b94SMartin Matuska 			}
1465184c1b94SMartin Matuska 		}
1466184c1b94SMartin Matuska 	}
1467184c1b94SMartin Matuska 	return (B_FALSE);
1468184c1b94SMartin Matuska }
1469184c1b94SMartin Matuska 
1470184c1b94SMartin Matuska /*
14717877fdebSMatt Macy  * Output a dRAID top-level vdev name in to the provided buffer.
14727877fdebSMatt Macy  */
14737877fdebSMatt Macy static char *
zpool_draid_name(char * name,int len,uint64_t data,uint64_t parity,uint64_t spares,uint64_t children)14747877fdebSMatt Macy zpool_draid_name(char *name, int len, uint64_t data, uint64_t parity,
14757877fdebSMatt Macy     uint64_t spares, uint64_t children)
14767877fdebSMatt Macy {
14777877fdebSMatt Macy 	snprintf(name, len, "%s%llu:%llud:%lluc:%llus",
14787877fdebSMatt Macy 	    VDEV_TYPE_DRAID, (u_longlong_t)parity, (u_longlong_t)data,
14797877fdebSMatt Macy 	    (u_longlong_t)children, (u_longlong_t)spares);
14807877fdebSMatt Macy 
14817877fdebSMatt Macy 	return (name);
14827877fdebSMatt Macy }
14837877fdebSMatt Macy 
14847877fdebSMatt Macy /*
14857877fdebSMatt Macy  * Return B_TRUE if the provided name is a dRAID spare name.
14867877fdebSMatt Macy  */
14877877fdebSMatt Macy boolean_t
zpool_is_draid_spare(const char * name)14887877fdebSMatt Macy zpool_is_draid_spare(const char *name)
14897877fdebSMatt Macy {
14907877fdebSMatt Macy 	uint64_t spare_id, parity, vdev_id;
14917877fdebSMatt Macy 
14927877fdebSMatt Macy 	if (sscanf(name, VDEV_TYPE_DRAID "%llu-%llu-%llu",
14937877fdebSMatt Macy 	    (u_longlong_t *)&parity, (u_longlong_t *)&vdev_id,
14947877fdebSMatt Macy 	    (u_longlong_t *)&spare_id) == 3) {
14957877fdebSMatt Macy 		return (B_TRUE);
14967877fdebSMatt Macy 	}
14977877fdebSMatt Macy 
14987877fdebSMatt Macy 	return (B_FALSE);
14997877fdebSMatt Macy }
15007877fdebSMatt Macy 
15017877fdebSMatt Macy /*
1502eda14cbcSMatt Macy  * Create the named pool, using the provided vdev list.  It is assumed
1503eda14cbcSMatt Macy  * that the consumer has already validated the contents of the nvlist, so we
1504eda14cbcSMatt Macy  * don't have to worry about error semantics.
1505eda14cbcSMatt Macy  */
1506eda14cbcSMatt Macy int
zpool_create(libzfs_handle_t * hdl,const char * pool,nvlist_t * nvroot,nvlist_t * props,nvlist_t * fsprops)1507eda14cbcSMatt Macy zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1508eda14cbcSMatt Macy     nvlist_t *props, nvlist_t *fsprops)
1509eda14cbcSMatt Macy {
1510eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
1511eda14cbcSMatt Macy 	nvlist_t *zc_fsprops = NULL;
1512eda14cbcSMatt Macy 	nvlist_t *zc_props = NULL;
1513eda14cbcSMatt Macy 	nvlist_t *hidden_args = NULL;
1514eda14cbcSMatt Macy 	uint8_t *wkeydata = NULL;
1515eda14cbcSMatt Macy 	uint_t wkeylen = 0;
15161f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
1517eda14cbcSMatt Macy 	int ret = -1;
1518eda14cbcSMatt Macy 
15191f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1520eda14cbcSMatt Macy 	    "cannot create '%s'"), pool);
1521eda14cbcSMatt Macy 
1522eda14cbcSMatt Macy 	if (!zpool_name_valid(hdl, B_FALSE, pool))
15231f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1524eda14cbcSMatt Macy 
1525716fd348SMartin Matuska 	zcmd_write_conf_nvlist(hdl, &zc, nvroot);
1526eda14cbcSMatt Macy 
1527eda14cbcSMatt Macy 	if (props) {
1528eda14cbcSMatt Macy 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1529eda14cbcSMatt Macy 
1530eda14cbcSMatt Macy 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
15311f1e2261SMartin Matuska 		    SPA_VERSION_1, flags, errbuf)) == NULL) {
1532eda14cbcSMatt Macy 			goto create_failed;
1533eda14cbcSMatt Macy 		}
1534eda14cbcSMatt Macy 	}
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy 	if (fsprops) {
1537eda14cbcSMatt Macy 		uint64_t zoned;
15382a58b312SMartin Matuska 		const char *zonestr;
1539eda14cbcSMatt Macy 
1540eda14cbcSMatt Macy 		zoned = ((nvlist_lookup_string(fsprops,
1541eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1542eda14cbcSMatt Macy 		    strcmp(zonestr, "on") == 0);
1543eda14cbcSMatt Macy 
1544eda14cbcSMatt Macy 		if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
15451f1e2261SMartin Matuska 		    fsprops, zoned, NULL, NULL, B_TRUE, errbuf)) == NULL) {
1546eda14cbcSMatt Macy 			goto create_failed;
1547eda14cbcSMatt Macy 		}
1548eda14cbcSMatt Macy 
1549eda14cbcSMatt Macy 		if (nvlist_exists(zc_fsprops,
1550eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1551eda14cbcSMatt Macy 		    !zpool_has_special_vdev(nvroot)) {
1552eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1553eda14cbcSMatt Macy 			    "%s property requires a special vdev"),
1554eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
15551f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1556eda14cbcSMatt Macy 			goto create_failed;
1557eda14cbcSMatt Macy 		}
1558eda14cbcSMatt Macy 
1559eda14cbcSMatt Macy 		if (!zc_props &&
1560eda14cbcSMatt Macy 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1561eda14cbcSMatt Macy 			goto create_failed;
1562eda14cbcSMatt Macy 		}
1563eda14cbcSMatt Macy 		if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1564eda14cbcSMatt Macy 		    &wkeydata, &wkeylen) != 0) {
15651f1e2261SMartin Matuska 			zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
1566eda14cbcSMatt Macy 			goto create_failed;
1567eda14cbcSMatt Macy 		}
1568eda14cbcSMatt Macy 		if (nvlist_add_nvlist(zc_props,
1569eda14cbcSMatt Macy 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1570eda14cbcSMatt Macy 			goto create_failed;
1571eda14cbcSMatt Macy 		}
1572eda14cbcSMatt Macy 		if (wkeydata != NULL) {
1573eda14cbcSMatt Macy 			if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1574eda14cbcSMatt Macy 				goto create_failed;
1575eda14cbcSMatt Macy 
1576eda14cbcSMatt Macy 			if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1577eda14cbcSMatt Macy 			    wkeydata, wkeylen) != 0)
1578eda14cbcSMatt Macy 				goto create_failed;
1579eda14cbcSMatt Macy 
1580eda14cbcSMatt Macy 			if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1581eda14cbcSMatt Macy 			    hidden_args) != 0)
1582eda14cbcSMatt Macy 				goto create_failed;
1583eda14cbcSMatt Macy 		}
1584eda14cbcSMatt Macy 	}
1585eda14cbcSMatt Macy 
1586716fd348SMartin Matuska 	if (zc_props)
1587716fd348SMartin Matuska 		zcmd_write_src_nvlist(hdl, &zc, zc_props);
1588eda14cbcSMatt Macy 
1589eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1590eda14cbcSMatt Macy 
1591eda14cbcSMatt Macy 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1592eda14cbcSMatt Macy 
1593eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
1594eda14cbcSMatt Macy 		nvlist_free(zc_props);
1595eda14cbcSMatt Macy 		nvlist_free(zc_fsprops);
1596eda14cbcSMatt Macy 		nvlist_free(hidden_args);
1597eda14cbcSMatt Macy 		if (wkeydata != NULL)
1598eda14cbcSMatt Macy 			free(wkeydata);
1599eda14cbcSMatt Macy 
1600eda14cbcSMatt Macy 		switch (errno) {
1601eda14cbcSMatt Macy 		case EBUSY:
1602eda14cbcSMatt Macy 			/*
1603eda14cbcSMatt Macy 			 * This can happen if the user has specified the same
1604eda14cbcSMatt Macy 			 * device multiple times.  We can't reliably detect this
1605eda14cbcSMatt Macy 			 * until we try to add it and see we already have a
1606eda14cbcSMatt Macy 			 * label.  This can also happen under if the device is
1607eda14cbcSMatt Macy 			 * part of an active md or lvm device.
1608eda14cbcSMatt Macy 			 */
1609eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1610eda14cbcSMatt Macy 			    "one or more vdevs refer to the same device, or "
1611eda14cbcSMatt Macy 			    "one of\nthe devices is part of an active md or "
1612eda14cbcSMatt Macy 			    "lvm device"));
16131f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1614eda14cbcSMatt Macy 
1615eda14cbcSMatt Macy 		case ERANGE:
1616eda14cbcSMatt Macy 			/*
1617eda14cbcSMatt Macy 			 * This happens if the record size is smaller or larger
1618eda14cbcSMatt Macy 			 * than the allowed size range, or not a power of 2.
1619eda14cbcSMatt Macy 			 *
1620eda14cbcSMatt Macy 			 * NOTE: although zfs_valid_proplist is called earlier,
1621eda14cbcSMatt Macy 			 * this case may have slipped through since the
1622eda14cbcSMatt Macy 			 * pool does not exist yet and it is therefore
1623eda14cbcSMatt Macy 			 * impossible to read properties e.g. max blocksize
1624eda14cbcSMatt Macy 			 * from the pool.
1625eda14cbcSMatt Macy 			 */
1626eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1627eda14cbcSMatt Macy 			    "record size invalid"));
16281f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1629eda14cbcSMatt Macy 
1630eda14cbcSMatt Macy 		case EOVERFLOW:
1631eda14cbcSMatt Macy 			/*
1632eda14cbcSMatt Macy 			 * This occurs when one of the devices is below
1633eda14cbcSMatt Macy 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1634eda14cbcSMatt Macy 			 * device was the problem device since there's no
1635eda14cbcSMatt Macy 			 * reliable way to determine device size from userland.
1636eda14cbcSMatt Macy 			 */
1637eda14cbcSMatt Macy 			{
1638eda14cbcSMatt Macy 				char buf[64];
1639eda14cbcSMatt Macy 
1640eda14cbcSMatt Macy 				zfs_nicebytes(SPA_MINDEVSIZE, buf,
1641eda14cbcSMatt Macy 				    sizeof (buf));
1642eda14cbcSMatt Macy 
1643eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1644eda14cbcSMatt Macy 				    "one or more devices is less than the "
1645eda14cbcSMatt Macy 				    "minimum size (%s)"), buf);
1646eda14cbcSMatt Macy 			}
16471f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1648eda14cbcSMatt Macy 
1649eda14cbcSMatt Macy 		case ENOSPC:
1650eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1651eda14cbcSMatt Macy 			    "one or more devices is out of space"));
16521f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1653eda14cbcSMatt Macy 
1654184c1b94SMartin Matuska 		case EINVAL:
1655184c1b94SMartin Matuska 			if (zpool_has_draid_vdev(nvroot) &&
1656184c1b94SMartin Matuska 			    zfeature_lookup_name("draid", NULL) != 0) {
1657184c1b94SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1658184c1b94SMartin Matuska 				    "dRAID vdevs are unsupported by the "
1659184c1b94SMartin Matuska 				    "kernel"));
16601f1e2261SMartin Matuska 				return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1661184c1b94SMartin Matuska 			} else {
16621f1e2261SMartin Matuska 				return (zpool_standard_error(hdl, errno,
16631f1e2261SMartin Matuska 				    errbuf));
1664184c1b94SMartin Matuska 			}
1665184c1b94SMartin Matuska 
1666eda14cbcSMatt Macy 		default:
16671f1e2261SMartin Matuska 			return (zpool_standard_error(hdl, errno, errbuf));
1668eda14cbcSMatt Macy 		}
1669eda14cbcSMatt Macy 	}
1670eda14cbcSMatt Macy 
1671eda14cbcSMatt Macy create_failed:
1672eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
1673eda14cbcSMatt Macy 	nvlist_free(zc_props);
1674eda14cbcSMatt Macy 	nvlist_free(zc_fsprops);
1675eda14cbcSMatt Macy 	nvlist_free(hidden_args);
1676eda14cbcSMatt Macy 	if (wkeydata != NULL)
1677eda14cbcSMatt Macy 		free(wkeydata);
1678eda14cbcSMatt Macy 	return (ret);
1679eda14cbcSMatt Macy }
1680eda14cbcSMatt Macy 
1681eda14cbcSMatt Macy /*
1682eda14cbcSMatt Macy  * Destroy the given pool.  It is up to the caller to ensure that there are no
1683eda14cbcSMatt Macy  * datasets left in the pool.
1684eda14cbcSMatt Macy  */
1685eda14cbcSMatt Macy int
zpool_destroy(zpool_handle_t * zhp,const char * log_str)1686eda14cbcSMatt Macy zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1687eda14cbcSMatt Macy {
1688eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
1689eda14cbcSMatt Macy 	zfs_handle_t *zfp = NULL;
1690eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
16911f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
1692eda14cbcSMatt Macy 
1693eda14cbcSMatt Macy 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1694eda14cbcSMatt Macy 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1695eda14cbcSMatt Macy 		return (-1);
1696eda14cbcSMatt Macy 
1697eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1698eda14cbcSMatt Macy 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1699eda14cbcSMatt Macy 
1700eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
17011f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1702eda14cbcSMatt Macy 		    "cannot destroy '%s'"), zhp->zpool_name);
1703eda14cbcSMatt Macy 
1704eda14cbcSMatt Macy 		if (errno == EROFS) {
1705eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1706eda14cbcSMatt Macy 			    "one or more devices is read only"));
17071f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1708eda14cbcSMatt Macy 		} else {
17091f1e2261SMartin Matuska 			(void) zpool_standard_error(hdl, errno, errbuf);
1710eda14cbcSMatt Macy 		}
1711eda14cbcSMatt Macy 
1712eda14cbcSMatt Macy 		if (zfp)
1713eda14cbcSMatt Macy 			zfs_close(zfp);
1714eda14cbcSMatt Macy 		return (-1);
1715eda14cbcSMatt Macy 	}
1716eda14cbcSMatt Macy 
1717eda14cbcSMatt Macy 	if (zfp) {
1718eda14cbcSMatt Macy 		remove_mountpoint(zfp);
1719eda14cbcSMatt Macy 		zfs_close(zfp);
1720eda14cbcSMatt Macy 	}
1721eda14cbcSMatt Macy 
1722eda14cbcSMatt Macy 	return (0);
1723eda14cbcSMatt Macy }
1724eda14cbcSMatt Macy 
1725eda14cbcSMatt Macy /*
1726eda14cbcSMatt Macy  * Create a checkpoint in the given pool.
1727eda14cbcSMatt Macy  */
1728eda14cbcSMatt Macy int
zpool_checkpoint(zpool_handle_t * zhp)1729eda14cbcSMatt Macy zpool_checkpoint(zpool_handle_t *zhp)
1730eda14cbcSMatt Macy {
1731eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17321f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
1733eda14cbcSMatt Macy 	int error;
1734eda14cbcSMatt Macy 
1735eda14cbcSMatt Macy 	error = lzc_pool_checkpoint(zhp->zpool_name);
1736eda14cbcSMatt Macy 	if (error != 0) {
17371f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1738eda14cbcSMatt Macy 		    "cannot checkpoint '%s'"), zhp->zpool_name);
17391f1e2261SMartin Matuska 		(void) zpool_standard_error(hdl, error, errbuf);
1740eda14cbcSMatt Macy 		return (-1);
1741eda14cbcSMatt Macy 	}
1742eda14cbcSMatt Macy 
1743eda14cbcSMatt Macy 	return (0);
1744eda14cbcSMatt Macy }
1745eda14cbcSMatt Macy 
1746eda14cbcSMatt Macy /*
1747eda14cbcSMatt Macy  * Discard the checkpoint from the given pool.
1748eda14cbcSMatt Macy  */
1749eda14cbcSMatt Macy int
zpool_discard_checkpoint(zpool_handle_t * zhp)1750eda14cbcSMatt Macy zpool_discard_checkpoint(zpool_handle_t *zhp)
1751eda14cbcSMatt Macy {
1752eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17531f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
1754eda14cbcSMatt Macy 	int error;
1755eda14cbcSMatt Macy 
1756eda14cbcSMatt Macy 	error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1757eda14cbcSMatt Macy 	if (error != 0) {
17581f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1759eda14cbcSMatt Macy 		    "cannot discard checkpoint in '%s'"), zhp->zpool_name);
17601f1e2261SMartin Matuska 		(void) zpool_standard_error(hdl, error, errbuf);
1761eda14cbcSMatt Macy 		return (-1);
1762eda14cbcSMatt Macy 	}
1763eda14cbcSMatt Macy 
1764eda14cbcSMatt Macy 	return (0);
1765eda14cbcSMatt Macy }
1766eda14cbcSMatt Macy 
1767eda14cbcSMatt Macy /*
1768ce4dcb97SMartin Matuska  * Load data type for the given pool.
1769ce4dcb97SMartin Matuska  */
1770ce4dcb97SMartin Matuska int
zpool_prefetch(zpool_handle_t * zhp,zpool_prefetch_type_t type)1771ce4dcb97SMartin Matuska zpool_prefetch(zpool_handle_t *zhp, zpool_prefetch_type_t type)
1772ce4dcb97SMartin Matuska {
1773ce4dcb97SMartin Matuska 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1774ce4dcb97SMartin Matuska 	char msg[1024];
1775ce4dcb97SMartin Matuska 	int error;
1776ce4dcb97SMartin Matuska 
1777ce4dcb97SMartin Matuska 	error = lzc_pool_prefetch(zhp->zpool_name, type);
1778ce4dcb97SMartin Matuska 	if (error != 0) {
1779ce4dcb97SMartin Matuska 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1780ce4dcb97SMartin Matuska 		    "cannot prefetch %s in '%s'"),
1781ce4dcb97SMartin Matuska 		    type == ZPOOL_PREFETCH_DDT ? "ddt" : "", zhp->zpool_name);
1782ce4dcb97SMartin Matuska 		(void) zpool_standard_error(hdl, error, msg);
1783ce4dcb97SMartin Matuska 		return (-1);
1784ce4dcb97SMartin Matuska 	}
1785ce4dcb97SMartin Matuska 
1786ce4dcb97SMartin Matuska 	return (0);
1787ce4dcb97SMartin Matuska }
1788ce4dcb97SMartin Matuska 
1789ce4dcb97SMartin Matuska /*
1790eda14cbcSMatt Macy  * Add the given vdevs to the pool.  The caller must have already performed the
1791eda14cbcSMatt Macy  * necessary verification to ensure that the vdev specification is well-formed.
1792eda14cbcSMatt Macy  */
1793eda14cbcSMatt Macy int
zpool_add(zpool_handle_t * zhp,nvlist_t * nvroot,boolean_t check_ashift)1794783d3ff6SMartin Matuska zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot, boolean_t check_ashift)
1795eda14cbcSMatt Macy {
1796eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
1797eda14cbcSMatt Macy 	int ret;
1798eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
17991f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
1800eda14cbcSMatt Macy 	nvlist_t **spares, **l2cache;
1801eda14cbcSMatt Macy 	uint_t nspares, nl2cache;
1802eda14cbcSMatt Macy 
18031f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1804eda14cbcSMatt Macy 	    "cannot add to '%s'"), zhp->zpool_name);
1805eda14cbcSMatt Macy 
1806eda14cbcSMatt Macy 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1807eda14cbcSMatt Macy 	    SPA_VERSION_SPARES &&
1808eda14cbcSMatt Macy 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1809eda14cbcSMatt Macy 	    &spares, &nspares) == 0) {
1810eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1811eda14cbcSMatt Macy 		    "upgraded to add hot spares"));
18121f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
1813eda14cbcSMatt Macy 	}
1814eda14cbcSMatt Macy 
1815eda14cbcSMatt Macy 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1816eda14cbcSMatt Macy 	    SPA_VERSION_L2CACHE &&
1817eda14cbcSMatt Macy 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1818eda14cbcSMatt Macy 	    &l2cache, &nl2cache) == 0) {
1819eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1820eda14cbcSMatt Macy 		    "upgraded to add cache devices"));
18211f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
1822eda14cbcSMatt Macy 	}
1823eda14cbcSMatt Macy 
1824716fd348SMartin Matuska 	zcmd_write_conf_nvlist(hdl, &zc, nvroot);
1825eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1826783d3ff6SMartin Matuska 	zc.zc_flags = check_ashift;
1827eda14cbcSMatt Macy 
1828eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1829eda14cbcSMatt Macy 		switch (errno) {
1830eda14cbcSMatt Macy 		case EBUSY:
1831eda14cbcSMatt Macy 			/*
1832eda14cbcSMatt Macy 			 * This can happen if the user has specified the same
1833eda14cbcSMatt Macy 			 * device multiple times.  We can't reliably detect this
1834eda14cbcSMatt Macy 			 * until we try to add it and see we already have a
1835eda14cbcSMatt Macy 			 * label.
1836eda14cbcSMatt Macy 			 */
1837eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1838eda14cbcSMatt Macy 			    "one or more vdevs refer to the same device"));
18391f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1840eda14cbcSMatt Macy 			break;
1841eda14cbcSMatt Macy 
1842eda14cbcSMatt Macy 		case EINVAL:
1843184c1b94SMartin Matuska 
1844184c1b94SMartin Matuska 			if (zpool_has_draid_vdev(nvroot) &&
1845184c1b94SMartin Matuska 			    zfeature_lookup_name("draid", NULL) != 0) {
1846eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1847184c1b94SMartin Matuska 				    "dRAID vdevs are unsupported by the "
1848184c1b94SMartin Matuska 				    "kernel"));
1849184c1b94SMartin Matuska 			} else {
1850184c1b94SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1851184c1b94SMartin Matuska 				    "invalid config; a pool with removing/"
1852184c1b94SMartin Matuska 				    "removed vdevs does not support adding "
1853184c1b94SMartin Matuska 				    "raidz or dRAID vdevs"));
1854184c1b94SMartin Matuska 			}
1855184c1b94SMartin Matuska 
18561f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1857eda14cbcSMatt Macy 			break;
1858eda14cbcSMatt Macy 
1859eda14cbcSMatt Macy 		case EOVERFLOW:
1860eda14cbcSMatt Macy 			/*
1861eda14cbcSMatt Macy 			 * This occurs when one of the devices is below
1862eda14cbcSMatt Macy 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1863eda14cbcSMatt Macy 			 * device was the problem device since there's no
1864eda14cbcSMatt Macy 			 * reliable way to determine device size from userland.
1865eda14cbcSMatt Macy 			 */
1866eda14cbcSMatt Macy 			{
1867eda14cbcSMatt Macy 				char buf[64];
1868eda14cbcSMatt Macy 
1869eda14cbcSMatt Macy 				zfs_nicebytes(SPA_MINDEVSIZE, buf,
1870eda14cbcSMatt Macy 				    sizeof (buf));
1871eda14cbcSMatt Macy 
1872eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1873eda14cbcSMatt Macy 				    "device is less than the minimum "
1874eda14cbcSMatt Macy 				    "size (%s)"), buf);
1875eda14cbcSMatt Macy 			}
18761f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1877eda14cbcSMatt Macy 			break;
1878eda14cbcSMatt Macy 
1879eda14cbcSMatt Macy 		case ENOTSUP:
1880eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1881eda14cbcSMatt Macy 			    "pool must be upgraded to add these vdevs"));
18821f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1883eda14cbcSMatt Macy 			break;
1884eda14cbcSMatt Macy 
1885eda14cbcSMatt Macy 		default:
18861f1e2261SMartin Matuska 			(void) zpool_standard_error(hdl, errno, errbuf);
1887eda14cbcSMatt Macy 		}
1888eda14cbcSMatt Macy 
1889eda14cbcSMatt Macy 		ret = -1;
1890eda14cbcSMatt Macy 	} else {
1891eda14cbcSMatt Macy 		ret = 0;
1892eda14cbcSMatt Macy 	}
1893eda14cbcSMatt Macy 
1894eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
1895eda14cbcSMatt Macy 
1896eda14cbcSMatt Macy 	return (ret);
1897eda14cbcSMatt Macy }
1898eda14cbcSMatt Macy 
1899eda14cbcSMatt Macy /*
1900eda14cbcSMatt Macy  * Exports the pool from the system.  The caller must ensure that there are no
1901eda14cbcSMatt Macy  * mounted datasets in the pool.
1902eda14cbcSMatt Macy  */
1903eda14cbcSMatt Macy static int
zpool_export_common(zpool_handle_t * zhp,boolean_t force,boolean_t hardforce,const char * log_str)1904eda14cbcSMatt Macy zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1905eda14cbcSMatt Macy     const char *log_str)
1906eda14cbcSMatt Macy {
1907eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
1908eda14cbcSMatt Macy 
1909eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1910eda14cbcSMatt Macy 	zc.zc_cookie = force;
1911eda14cbcSMatt Macy 	zc.zc_guid = hardforce;
1912eda14cbcSMatt Macy 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1913eda14cbcSMatt Macy 
1914eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1915eda14cbcSMatt Macy 		switch (errno) {
1916eda14cbcSMatt Macy 		case EXDEV:
1917eda14cbcSMatt Macy 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1918eda14cbcSMatt Macy 			    "use '-f' to override the following errors:\n"
1919eda14cbcSMatt Macy 			    "'%s' has an active shared spare which could be"
1920eda14cbcSMatt Macy 			    " used by other pools once '%s' is exported."),
1921eda14cbcSMatt Macy 			    zhp->zpool_name, zhp->zpool_name);
192216038816SMartin Matuska 			return (zfs_error_fmt(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
192316038816SMartin Matuska 			    dgettext(TEXT_DOMAIN, "cannot export '%s'"),
192416038816SMartin Matuska 			    zhp->zpool_name));
1925eda14cbcSMatt Macy 		default:
1926eda14cbcSMatt Macy 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
192716038816SMartin Matuska 			    dgettext(TEXT_DOMAIN, "cannot export '%s'"),
192816038816SMartin Matuska 			    zhp->zpool_name));
1929eda14cbcSMatt Macy 		}
1930eda14cbcSMatt Macy 	}
1931eda14cbcSMatt Macy 
1932eda14cbcSMatt Macy 	return (0);
1933eda14cbcSMatt Macy }
1934eda14cbcSMatt Macy 
1935eda14cbcSMatt Macy int
zpool_export(zpool_handle_t * zhp,boolean_t force,const char * log_str)1936eda14cbcSMatt Macy zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1937eda14cbcSMatt Macy {
1938eda14cbcSMatt Macy 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1939eda14cbcSMatt Macy }
1940eda14cbcSMatt Macy 
1941eda14cbcSMatt Macy int
zpool_export_force(zpool_handle_t * zhp,const char * log_str)1942eda14cbcSMatt Macy zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1943eda14cbcSMatt Macy {
1944eda14cbcSMatt Macy 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1945eda14cbcSMatt Macy }
1946eda14cbcSMatt Macy 
1947eda14cbcSMatt Macy static void
zpool_rewind_exclaim(libzfs_handle_t * hdl,const char * name,boolean_t dryrun,nvlist_t * config)1948eda14cbcSMatt Macy zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1949eda14cbcSMatt Macy     nvlist_t *config)
1950eda14cbcSMatt Macy {
1951eda14cbcSMatt Macy 	nvlist_t *nv = NULL;
1952eda14cbcSMatt Macy 	uint64_t rewindto;
1953eda14cbcSMatt Macy 	int64_t loss = -1;
1954eda14cbcSMatt Macy 	struct tm t;
1955eda14cbcSMatt Macy 	char timestr[128];
1956eda14cbcSMatt Macy 
1957eda14cbcSMatt Macy 	if (!hdl->libzfs_printerr || config == NULL)
1958eda14cbcSMatt Macy 		return;
1959eda14cbcSMatt Macy 
1960eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1961eda14cbcSMatt Macy 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1962eda14cbcSMatt Macy 		return;
1963eda14cbcSMatt Macy 	}
1964eda14cbcSMatt Macy 
1965eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1966eda14cbcSMatt Macy 		return;
1967eda14cbcSMatt Macy 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1968eda14cbcSMatt Macy 
1969eda14cbcSMatt Macy 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
19701719886fSMartin Matuska 	    ctime_r((time_t *)&rewindto, timestr) != NULL) {
19711719886fSMartin Matuska 		timestr[24] = 0;
1972eda14cbcSMatt Macy 		if (dryrun) {
1973eda14cbcSMatt Macy 			(void) printf(dgettext(TEXT_DOMAIN,
1974eda14cbcSMatt Macy 			    "Would be able to return %s "
1975eda14cbcSMatt Macy 			    "to its state as of %s.\n"),
1976eda14cbcSMatt Macy 			    name, timestr);
1977eda14cbcSMatt Macy 		} else {
1978eda14cbcSMatt Macy 			(void) printf(dgettext(TEXT_DOMAIN,
1979eda14cbcSMatt Macy 			    "Pool %s returned to its state as of %s.\n"),
1980eda14cbcSMatt Macy 			    name, timestr);
1981eda14cbcSMatt Macy 		}
1982eda14cbcSMatt Macy 		if (loss > 120) {
1983eda14cbcSMatt Macy 			(void) printf(dgettext(TEXT_DOMAIN,
1984eda14cbcSMatt Macy 			    "%s approximately %lld "),
1985eda14cbcSMatt Macy 			    dryrun ? "Would discard" : "Discarded",
1986eda14cbcSMatt Macy 			    ((longlong_t)loss + 30) / 60);
1987eda14cbcSMatt Macy 			(void) printf(dgettext(TEXT_DOMAIN,
1988eda14cbcSMatt Macy 			    "minutes of transactions.\n"));
1989eda14cbcSMatt Macy 		} else if (loss > 0) {
1990eda14cbcSMatt Macy 			(void) printf(dgettext(TEXT_DOMAIN,
1991eda14cbcSMatt Macy 			    "%s approximately %lld "),
1992eda14cbcSMatt Macy 			    dryrun ? "Would discard" : "Discarded",
1993eda14cbcSMatt Macy 			    (longlong_t)loss);
1994eda14cbcSMatt Macy 			(void) printf(dgettext(TEXT_DOMAIN,
1995eda14cbcSMatt Macy 			    "seconds of transactions.\n"));
1996eda14cbcSMatt Macy 		}
1997eda14cbcSMatt Macy 	}
1998eda14cbcSMatt Macy }
1999eda14cbcSMatt Macy 
2000eda14cbcSMatt Macy void
zpool_explain_recover(libzfs_handle_t * hdl,const char * name,int reason,nvlist_t * config,char * buf,size_t size)2001eda14cbcSMatt Macy zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
2002ce4dcb97SMartin Matuska     nvlist_t *config, char *buf, size_t size)
2003eda14cbcSMatt Macy {
2004eda14cbcSMatt Macy 	nvlist_t *nv = NULL;
2005eda14cbcSMatt Macy 	int64_t loss = -1;
2006eda14cbcSMatt Macy 	uint64_t edata = UINT64_MAX;
2007eda14cbcSMatt Macy 	uint64_t rewindto;
2008eda14cbcSMatt Macy 	struct tm t;
2009ce4dcb97SMartin Matuska 	char timestr[128], temp[1024];
2010eda14cbcSMatt Macy 
2011eda14cbcSMatt Macy 	if (!hdl->libzfs_printerr)
2012eda14cbcSMatt Macy 		return;
2013eda14cbcSMatt Macy 
2014eda14cbcSMatt Macy 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
2015eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
2016eda14cbcSMatt Macy 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
2017eda14cbcSMatt Macy 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
2018eda14cbcSMatt Macy 		goto no_info;
2019eda14cbcSMatt Macy 
2020eda14cbcSMatt Macy 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
2021eda14cbcSMatt Macy 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
2022eda14cbcSMatt Macy 	    &edata);
2023eda14cbcSMatt Macy 
2024ce4dcb97SMartin Matuska 	(void) snprintf(buf, size, dgettext(TEXT_DOMAIN,
2025eda14cbcSMatt Macy 	    "Recovery is possible, but will result in some data loss.\n"));
2026eda14cbcSMatt Macy 
2027eda14cbcSMatt Macy 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
20281719886fSMartin Matuska 	    ctime_r((time_t *)&rewindto, timestr) != NULL) {
20291719886fSMartin Matuska 		timestr[24] = 0;
2030ce4dcb97SMartin Matuska 		(void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2031eda14cbcSMatt Macy 		    "\tReturning the pool to its state as of %s\n"
2032ce4dcb97SMartin Matuska 		    "\tshould correct the problem.  "), timestr);
2033ce4dcb97SMartin Matuska 		(void) strlcat(buf, temp, size);
2034eda14cbcSMatt Macy 	} else {
2035ce4dcb97SMartin Matuska 		(void) strlcat(buf, dgettext(TEXT_DOMAIN,
2036eda14cbcSMatt Macy 		    "\tReverting the pool to an earlier state "
2037ce4dcb97SMartin Matuska 		    "should correct the problem.\n\t"), size);
2038eda14cbcSMatt Macy 	}
2039eda14cbcSMatt Macy 
2040eda14cbcSMatt Macy 	if (loss > 120) {
2041ce4dcb97SMartin Matuska 		(void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2042eda14cbcSMatt Macy 		    "Approximately %lld minutes of data\n"
2043eda14cbcSMatt Macy 		    "\tmust be discarded, irreversibly.  "),
2044eda14cbcSMatt Macy 		    ((longlong_t)loss + 30) / 60);
2045ce4dcb97SMartin Matuska 		(void) strlcat(buf, temp, size);
2046eda14cbcSMatt Macy 	} else if (loss > 0) {
2047ce4dcb97SMartin Matuska 		(void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2048eda14cbcSMatt Macy 		    "Approximately %lld seconds of data\n"
2049eda14cbcSMatt Macy 		    "\tmust be discarded, irreversibly.  "),
2050eda14cbcSMatt Macy 		    (longlong_t)loss);
2051ce4dcb97SMartin Matuska 		(void) strlcat(buf, temp, size);
2052eda14cbcSMatt Macy 	}
2053eda14cbcSMatt Macy 	if (edata != 0 && edata != UINT64_MAX) {
2054eda14cbcSMatt Macy 		if (edata == 1) {
2055ce4dcb97SMartin Matuska 			(void) strlcat(buf, dgettext(TEXT_DOMAIN,
2056eda14cbcSMatt Macy 			    "After rewind, at least\n"
2057ce4dcb97SMartin Matuska 			    "\tone persistent user-data error will remain.  "),
2058ce4dcb97SMartin Matuska 			    size);
2059eda14cbcSMatt Macy 		} else {
2060ce4dcb97SMartin Matuska 			(void) strlcat(buf, dgettext(TEXT_DOMAIN,
2061eda14cbcSMatt Macy 			    "After rewind, several\n"
2062ce4dcb97SMartin Matuska 			    "\tpersistent user-data errors will remain.  "),
2063ce4dcb97SMartin Matuska 			    size);
2064eda14cbcSMatt Macy 		}
2065eda14cbcSMatt Macy 	}
2066ce4dcb97SMartin Matuska 	(void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2067eda14cbcSMatt Macy 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
2068eda14cbcSMatt Macy 	    reason >= 0 ? "clear" : "import", name);
2069ce4dcb97SMartin Matuska 	(void) strlcat(buf, temp, size);
2070eda14cbcSMatt Macy 
2071ce4dcb97SMartin Matuska 	(void) strlcat(buf, dgettext(TEXT_DOMAIN,
2072eda14cbcSMatt Macy 	    "A scrub of the pool\n"
2073ce4dcb97SMartin Matuska 	    "\tis strongly recommended after recovery.\n"), size);
2074eda14cbcSMatt Macy 	return;
2075eda14cbcSMatt Macy 
2076eda14cbcSMatt Macy no_info:
2077ce4dcb97SMartin Matuska 	(void) strlcat(buf, dgettext(TEXT_DOMAIN,
2078ce4dcb97SMartin Matuska 	    "Destroy and re-create the pool from\n\ta backup source.\n"), size);
2079eda14cbcSMatt Macy }
2080eda14cbcSMatt Macy 
2081eda14cbcSMatt Macy /*
2082eda14cbcSMatt Macy  * zpool_import() is a contracted interface. Should be kept the same
2083eda14cbcSMatt Macy  * if possible.
2084eda14cbcSMatt Macy  *
2085eda14cbcSMatt Macy  * Applications should use zpool_import_props() to import a pool with
2086eda14cbcSMatt Macy  * new properties value to be set.
2087eda14cbcSMatt Macy  */
2088eda14cbcSMatt Macy int
zpool_import(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,char * altroot)2089eda14cbcSMatt Macy zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
2090eda14cbcSMatt Macy     char *altroot)
2091eda14cbcSMatt Macy {
2092eda14cbcSMatt Macy 	nvlist_t *props = NULL;
2093eda14cbcSMatt Macy 	int ret;
2094eda14cbcSMatt Macy 
2095eda14cbcSMatt Macy 	if (altroot != NULL) {
2096eda14cbcSMatt Macy 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2097eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
2098eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2099eda14cbcSMatt Macy 			    newname));
2100eda14cbcSMatt Macy 		}
2101eda14cbcSMatt Macy 
2102eda14cbcSMatt Macy 		if (nvlist_add_string(props,
2103eda14cbcSMatt Macy 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
2104eda14cbcSMatt Macy 		    nvlist_add_string(props,
2105eda14cbcSMatt Macy 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
2106eda14cbcSMatt Macy 			nvlist_free(props);
2107eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
2108eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2109eda14cbcSMatt Macy 			    newname));
2110eda14cbcSMatt Macy 		}
2111eda14cbcSMatt Macy 	}
2112eda14cbcSMatt Macy 
2113eda14cbcSMatt Macy 	ret = zpool_import_props(hdl, config, newname, props,
2114eda14cbcSMatt Macy 	    ZFS_IMPORT_NORMAL);
2115eda14cbcSMatt Macy 	nvlist_free(props);
2116eda14cbcSMatt Macy 	return (ret);
2117eda14cbcSMatt Macy }
2118eda14cbcSMatt Macy 
2119eda14cbcSMatt Macy static void
print_vdev_tree(libzfs_handle_t * hdl,const char * name,nvlist_t * nv,int indent)2120eda14cbcSMatt Macy print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
2121eda14cbcSMatt Macy     int indent)
2122eda14cbcSMatt Macy {
2123eda14cbcSMatt Macy 	nvlist_t **child;
2124eda14cbcSMatt Macy 	uint_t c, children;
2125eda14cbcSMatt Macy 	char *vname;
2126eda14cbcSMatt Macy 	uint64_t is_log = 0;
2127eda14cbcSMatt Macy 
2128eda14cbcSMatt Macy 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2129eda14cbcSMatt Macy 	    &is_log);
2130eda14cbcSMatt Macy 
2131eda14cbcSMatt Macy 	if (name != NULL)
2132eda14cbcSMatt Macy 		(void) printf("\t%*s%s%s\n", indent, "", name,
2133eda14cbcSMatt Macy 		    is_log ? " [log]" : "");
2134eda14cbcSMatt Macy 
2135eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2136eda14cbcSMatt Macy 	    &child, &children) != 0)
2137eda14cbcSMatt Macy 		return;
2138eda14cbcSMatt Macy 
2139eda14cbcSMatt Macy 	for (c = 0; c < children; c++) {
2140eda14cbcSMatt Macy 		vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
2141eda14cbcSMatt Macy 		print_vdev_tree(hdl, vname, child[c], indent + 2);
2142eda14cbcSMatt Macy 		free(vname);
2143eda14cbcSMatt Macy 	}
2144eda14cbcSMatt Macy }
2145eda14cbcSMatt Macy 
2146eda14cbcSMatt Macy void
zpool_collect_unsup_feat(nvlist_t * config,char * buf,size_t size)2147ce4dcb97SMartin Matuska zpool_collect_unsup_feat(nvlist_t *config, char *buf, size_t size)
2148eda14cbcSMatt Macy {
2149eda14cbcSMatt Macy 	nvlist_t *nvinfo, *unsup_feat;
2150ce4dcb97SMartin Matuska 	char temp[512];
2151eda14cbcSMatt Macy 
2152da5137abSMartin Matuska 	nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2153da5137abSMartin Matuska 	unsup_feat = fnvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT);
2154eda14cbcSMatt Macy 
2155da5137abSMartin Matuska 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL);
2156da5137abSMartin Matuska 	    nvp != NULL; nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
21572a58b312SMartin Matuska 		const char *desc = fnvpair_value_string(nvp);
2158ce4dcb97SMartin Matuska 		if (strlen(desc) > 0) {
2159ce4dcb97SMartin Matuska 			(void) snprintf(temp, 512, "\t%s (%s)\n",
2160ce4dcb97SMartin Matuska 			    nvpair_name(nvp), desc);
2161ce4dcb97SMartin Matuska 			(void) strlcat(buf, temp, size);
2162ce4dcb97SMartin Matuska 		} else {
2163ce4dcb97SMartin Matuska 			(void) snprintf(temp, 512, "\t%s\n", nvpair_name(nvp));
2164ce4dcb97SMartin Matuska 			(void) strlcat(buf, temp, size);
2165ce4dcb97SMartin Matuska 		}
2166eda14cbcSMatt Macy 	}
2167eda14cbcSMatt Macy }
2168eda14cbcSMatt Macy 
2169eda14cbcSMatt Macy /*
2170eda14cbcSMatt Macy  * Import the given pool using the known configuration and a list of
2171eda14cbcSMatt Macy  * properties to be set. The configuration should have come from
2172eda14cbcSMatt Macy  * zpool_find_import(). The 'newname' parameters control whether the pool
2173eda14cbcSMatt Macy  * is imported with a different name.
2174eda14cbcSMatt Macy  */
2175eda14cbcSMatt Macy int
zpool_import_props(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,nvlist_t * props,int flags)2176eda14cbcSMatt Macy zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
2177eda14cbcSMatt Macy     nvlist_t *props, int flags)
2178eda14cbcSMatt Macy {
2179eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
2180eda14cbcSMatt Macy 	zpool_load_policy_t policy;
2181eda14cbcSMatt Macy 	nvlist_t *nv = NULL;
2182eda14cbcSMatt Macy 	nvlist_t *nvinfo = NULL;
2183eda14cbcSMatt Macy 	nvlist_t *missing = NULL;
2184c03c5b1cSMartin Matuska 	const char *thename;
21852a58b312SMartin Matuska 	const char *origname;
2186eda14cbcSMatt Macy 	int ret;
2187eda14cbcSMatt Macy 	int error = 0;
2188ce4dcb97SMartin Matuska 	char buf[2048];
21891f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2190eda14cbcSMatt Macy 
2191da5137abSMartin Matuska 	origname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2192eda14cbcSMatt Macy 
2193eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2194eda14cbcSMatt Macy 	    "cannot import pool '%s'"), origname);
2195eda14cbcSMatt Macy 
2196eda14cbcSMatt Macy 	if (newname != NULL) {
2197eda14cbcSMatt Macy 		if (!zpool_name_valid(hdl, B_FALSE, newname))
2198eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2199eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2200eda14cbcSMatt Macy 			    newname));
2201c03c5b1cSMartin Matuska 		thename = newname;
2202eda14cbcSMatt Macy 	} else {
2203eda14cbcSMatt Macy 		thename = origname;
2204eda14cbcSMatt Macy 	}
2205eda14cbcSMatt Macy 
2206eda14cbcSMatt Macy 	if (props != NULL) {
2207eda14cbcSMatt Macy 		uint64_t version;
2208eda14cbcSMatt Macy 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2209eda14cbcSMatt Macy 
2210da5137abSMartin Matuska 		version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2211eda14cbcSMatt Macy 
2212eda14cbcSMatt Macy 		if ((props = zpool_valid_proplist(hdl, origname,
2213eda14cbcSMatt Macy 		    props, version, flags, errbuf)) == NULL)
2214eda14cbcSMatt Macy 			return (-1);
2215716fd348SMartin Matuska 		zcmd_write_src_nvlist(hdl, &zc, props);
2216eda14cbcSMatt Macy 		nvlist_free(props);
2217eda14cbcSMatt Macy 	}
2218eda14cbcSMatt Macy 
2219eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
2220eda14cbcSMatt Macy 
2221da5137abSMartin Matuska 	zc.zc_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
2222eda14cbcSMatt Macy 
2223716fd348SMartin Matuska 	zcmd_write_conf_nvlist(hdl, &zc, config);
2224716fd348SMartin Matuska 	zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2);
2225eda14cbcSMatt Macy 
2226eda14cbcSMatt Macy 	zc.zc_cookie = flags;
2227eda14cbcSMatt Macy 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
2228716fd348SMartin Matuska 	    errno == ENOMEM)
2229716fd348SMartin Matuska 		zcmd_expand_dst_nvlist(hdl, &zc);
2230eda14cbcSMatt Macy 	if (ret != 0)
2231eda14cbcSMatt Macy 		error = errno;
2232eda14cbcSMatt Macy 
2233eda14cbcSMatt Macy 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
2234eda14cbcSMatt Macy 
2235eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
2236eda14cbcSMatt Macy 
2237eda14cbcSMatt Macy 	zpool_get_load_policy(config, &policy);
2238eda14cbcSMatt Macy 
2239eda14cbcSMatt Macy 	if (error) {
2240eda14cbcSMatt Macy 		char desc[1024];
2241eda14cbcSMatt Macy 		char aux[256];
2242eda14cbcSMatt Macy 
2243eda14cbcSMatt Macy 		/*
2244eda14cbcSMatt Macy 		 * Dry-run failed, but we print out what success
2245eda14cbcSMatt Macy 		 * looks like if we found a best txg
2246eda14cbcSMatt Macy 		 */
2247eda14cbcSMatt Macy 		if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
2248eda14cbcSMatt Macy 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
2249eda14cbcSMatt Macy 			    B_TRUE, nv);
2250eda14cbcSMatt Macy 			nvlist_free(nv);
2251eda14cbcSMatt Macy 			return (-1);
2252eda14cbcSMatt Macy 		}
2253eda14cbcSMatt Macy 
2254eda14cbcSMatt Macy 		if (newname == NULL)
2255eda14cbcSMatt Macy 			(void) snprintf(desc, sizeof (desc),
2256eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2257eda14cbcSMatt Macy 			    thename);
2258eda14cbcSMatt Macy 		else
2259eda14cbcSMatt Macy 			(void) snprintf(desc, sizeof (desc),
2260eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
2261eda14cbcSMatt Macy 			    origname, thename);
2262eda14cbcSMatt Macy 
2263eda14cbcSMatt Macy 		switch (error) {
2264eda14cbcSMatt Macy 		case ENOTSUP:
2265eda14cbcSMatt Macy 			if (nv != NULL && nvlist_lookup_nvlist(nv,
2266eda14cbcSMatt Macy 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2267eda14cbcSMatt Macy 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
2268eda14cbcSMatt Macy 				(void) printf(dgettext(TEXT_DOMAIN, "This "
2269eda14cbcSMatt Macy 				    "pool uses the following feature(s) not "
2270eda14cbcSMatt Macy 				    "supported by this system:\n"));
2271ce4dcb97SMartin Matuska 				memset(buf, 0, 2048);
2272ce4dcb97SMartin Matuska 				zpool_collect_unsup_feat(nv, buf, 2048);
2273ce4dcb97SMartin Matuska 				(void) printf("%s", buf);
2274eda14cbcSMatt Macy 				if (nvlist_exists(nvinfo,
2275eda14cbcSMatt Macy 				    ZPOOL_CONFIG_CAN_RDONLY)) {
2276eda14cbcSMatt Macy 					(void) printf(dgettext(TEXT_DOMAIN,
2277eda14cbcSMatt Macy 					    "All unsupported features are only "
2278eda14cbcSMatt Macy 					    "required for writing to the pool."
2279eda14cbcSMatt Macy 					    "\nThe pool can be imported using "
2280eda14cbcSMatt Macy 					    "'-o readonly=on'.\n"));
2281eda14cbcSMatt Macy 				}
2282eda14cbcSMatt Macy 			}
2283eda14cbcSMatt Macy 			/*
2284eda14cbcSMatt Macy 			 * Unsupported version.
2285eda14cbcSMatt Macy 			 */
2286eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
2287eda14cbcSMatt Macy 			break;
2288eda14cbcSMatt Macy 
2289eda14cbcSMatt Macy 		case EREMOTEIO:
2290eda14cbcSMatt Macy 			if (nv != NULL && nvlist_lookup_nvlist(nv,
2291eda14cbcSMatt Macy 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
2292a0b956f5SMartin Matuska 				const char *hostname = "<unknown>";
2293eda14cbcSMatt Macy 				uint64_t hostid = 0;
2294eda14cbcSMatt Macy 				mmp_state_t mmp_state;
2295eda14cbcSMatt Macy 
2296eda14cbcSMatt Macy 				mmp_state = fnvlist_lookup_uint64(nvinfo,
2297eda14cbcSMatt Macy 				    ZPOOL_CONFIG_MMP_STATE);
2298eda14cbcSMatt Macy 
2299eda14cbcSMatt Macy 				if (nvlist_exists(nvinfo,
2300eda14cbcSMatt Macy 				    ZPOOL_CONFIG_MMP_HOSTNAME))
2301eda14cbcSMatt Macy 					hostname = fnvlist_lookup_string(nvinfo,
2302eda14cbcSMatt Macy 					    ZPOOL_CONFIG_MMP_HOSTNAME);
2303eda14cbcSMatt Macy 
2304eda14cbcSMatt Macy 				if (nvlist_exists(nvinfo,
2305eda14cbcSMatt Macy 				    ZPOOL_CONFIG_MMP_HOSTID))
2306eda14cbcSMatt Macy 					hostid = fnvlist_lookup_uint64(nvinfo,
2307eda14cbcSMatt Macy 					    ZPOOL_CONFIG_MMP_HOSTID);
2308eda14cbcSMatt Macy 
2309eda14cbcSMatt Macy 				if (mmp_state == MMP_STATE_ACTIVE) {
2310eda14cbcSMatt Macy 					(void) snprintf(aux, sizeof (aux),
2311eda14cbcSMatt Macy 					    dgettext(TEXT_DOMAIN, "pool is imp"
2312eda14cbcSMatt Macy 					    "orted on host '%s' (hostid=%lx).\n"
2313eda14cbcSMatt Macy 					    "Export the pool on the other "
2314eda14cbcSMatt Macy 					    "system, then run 'zpool import'."),
2315eda14cbcSMatt Macy 					    hostname, (unsigned long) hostid);
2316eda14cbcSMatt Macy 				} else if (mmp_state == MMP_STATE_NO_HOSTID) {
2317eda14cbcSMatt Macy 					(void) snprintf(aux, sizeof (aux),
2318eda14cbcSMatt Macy 					    dgettext(TEXT_DOMAIN, "pool has "
2319eda14cbcSMatt Macy 					    "the multihost property on and "
2320eda14cbcSMatt Macy 					    "the\nsystem's hostid is not set. "
2321eda14cbcSMatt Macy 					    "Set a unique system hostid with "
2322eda14cbcSMatt Macy 					    "the zgenhostid(8) command.\n"));
2323eda14cbcSMatt Macy 				}
2324eda14cbcSMatt Macy 
232516038816SMartin Matuska 				(void) zfs_error_aux(hdl, "%s", aux);
2326eda14cbcSMatt Macy 			}
2327eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2328eda14cbcSMatt Macy 			break;
2329eda14cbcSMatt Macy 
2330eda14cbcSMatt Macy 		case EINVAL:
2331eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2332eda14cbcSMatt Macy 			break;
2333eda14cbcSMatt Macy 
2334eda14cbcSMatt Macy 		case EROFS:
2335eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2336eda14cbcSMatt Macy 			    "one or more devices is read only"));
2337eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
2338eda14cbcSMatt Macy 			break;
2339eda14cbcSMatt Macy 
2340eda14cbcSMatt Macy 		case ENXIO:
2341eda14cbcSMatt Macy 			if (nv && nvlist_lookup_nvlist(nv,
2342eda14cbcSMatt Macy 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2343eda14cbcSMatt Macy 			    nvlist_lookup_nvlist(nvinfo,
2344eda14cbcSMatt Macy 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2345eda14cbcSMatt Macy 				(void) printf(dgettext(TEXT_DOMAIN,
2346eda14cbcSMatt Macy 				    "The devices below are missing or "
2347eda14cbcSMatt Macy 				    "corrupted, use '-m' to import the pool "
2348eda14cbcSMatt Macy 				    "anyway:\n"));
2349eda14cbcSMatt Macy 				print_vdev_tree(hdl, NULL, missing, 2);
2350eda14cbcSMatt Macy 				(void) printf("\n");
2351eda14cbcSMatt Macy 			}
2352eda14cbcSMatt Macy 			(void) zpool_standard_error(hdl, error, desc);
2353eda14cbcSMatt Macy 			break;
2354eda14cbcSMatt Macy 
2355eda14cbcSMatt Macy 		case EEXIST:
2356eda14cbcSMatt Macy 			(void) zpool_standard_error(hdl, error, desc);
2357eda14cbcSMatt Macy 			break;
2358eda14cbcSMatt Macy 
2359eda14cbcSMatt Macy 		case EBUSY:
2360eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2361eda14cbcSMatt Macy 			    "one or more devices are already in use\n"));
2362eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
2363eda14cbcSMatt Macy 			break;
2364eda14cbcSMatt Macy 		case ENAMETOOLONG:
2365eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2366eda14cbcSMatt Macy 			    "new name of at least one dataset is longer than "
2367eda14cbcSMatt Macy 			    "the maximum allowable length"));
2368eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2369eda14cbcSMatt Macy 			break;
2370eda14cbcSMatt Macy 		default:
2371eda14cbcSMatt Macy 			(void) zpool_standard_error(hdl, error, desc);
2372ce4dcb97SMartin Matuska 			memset(buf, 0, 2048);
2373eda14cbcSMatt Macy 			zpool_explain_recover(hdl,
2374ce4dcb97SMartin Matuska 			    newname ? origname : thename, -error, nv,
2375ce4dcb97SMartin Matuska 			    buf, 2048);
2376ce4dcb97SMartin Matuska 			(void) printf("\t%s", buf);
2377eda14cbcSMatt Macy 			break;
2378eda14cbcSMatt Macy 		}
2379eda14cbcSMatt Macy 
2380eda14cbcSMatt Macy 		nvlist_free(nv);
2381eda14cbcSMatt Macy 		ret = -1;
2382eda14cbcSMatt Macy 	} else {
2383eda14cbcSMatt Macy 		zpool_handle_t *zhp;
2384eda14cbcSMatt Macy 
2385eda14cbcSMatt Macy 		/*
2386eda14cbcSMatt Macy 		 * This should never fail, but play it safe anyway.
2387eda14cbcSMatt Macy 		 */
2388eda14cbcSMatt Macy 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
2389eda14cbcSMatt Macy 			ret = -1;
2390eda14cbcSMatt Macy 		else if (zhp != NULL)
2391eda14cbcSMatt Macy 			zpool_close(zhp);
2392eda14cbcSMatt Macy 		if (policy.zlp_rewind &
2393eda14cbcSMatt Macy 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2394eda14cbcSMatt Macy 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
2395eda14cbcSMatt Macy 			    ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2396eda14cbcSMatt Macy 		}
2397eda14cbcSMatt Macy 		nvlist_free(nv);
2398eda14cbcSMatt Macy 	}
2399eda14cbcSMatt Macy 
2400eda14cbcSMatt Macy 	return (ret);
2401eda14cbcSMatt Macy }
2402eda14cbcSMatt Macy 
2403eda14cbcSMatt Macy /*
2404eda14cbcSMatt Macy  * Translate vdev names to guids.  If a vdev_path is determined to be
2405eda14cbcSMatt Macy  * unsuitable then a vd_errlist is allocated and the vdev path and errno
2406eda14cbcSMatt Macy  * are added to it.
2407eda14cbcSMatt Macy  */
2408eda14cbcSMatt Macy static int
zpool_translate_vdev_guids(zpool_handle_t * zhp,nvlist_t * vds,nvlist_t * vdev_guids,nvlist_t * guids_to_paths,nvlist_t ** vd_errlist)2409eda14cbcSMatt Macy zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2410eda14cbcSMatt Macy     nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2411eda14cbcSMatt Macy {
2412eda14cbcSMatt Macy 	nvlist_t *errlist = NULL;
2413eda14cbcSMatt Macy 	int error = 0;
2414eda14cbcSMatt Macy 
2415eda14cbcSMatt Macy 	for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2416eda14cbcSMatt Macy 	    elem = nvlist_next_nvpair(vds, elem)) {
2417eda14cbcSMatt Macy 		boolean_t spare, cache;
2418eda14cbcSMatt Macy 
24192a58b312SMartin Matuska 		const char *vd_path = nvpair_name(elem);
2420eda14cbcSMatt Macy 		nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2421eda14cbcSMatt Macy 		    NULL);
2422eda14cbcSMatt Macy 
2423eda14cbcSMatt Macy 		if ((tgt == NULL) || cache || spare) {
2424eda14cbcSMatt Macy 			if (errlist == NULL) {
2425eda14cbcSMatt Macy 				errlist = fnvlist_alloc();
2426eda14cbcSMatt Macy 				error = EINVAL;
2427eda14cbcSMatt Macy 			}
2428eda14cbcSMatt Macy 
2429eda14cbcSMatt Macy 			uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2430eda14cbcSMatt Macy 			    (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2431eda14cbcSMatt Macy 			fnvlist_add_int64(errlist, vd_path, err);
2432eda14cbcSMatt Macy 			continue;
2433eda14cbcSMatt Macy 		}
2434eda14cbcSMatt Macy 
2435eda14cbcSMatt Macy 		uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2436eda14cbcSMatt Macy 		fnvlist_add_uint64(vdev_guids, vd_path, guid);
2437eda14cbcSMatt Macy 
2438eda14cbcSMatt Macy 		char msg[MAXNAMELEN];
2439eda14cbcSMatt Macy 		(void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2440eda14cbcSMatt Macy 		fnvlist_add_string(guids_to_paths, msg, vd_path);
2441eda14cbcSMatt Macy 	}
2442eda14cbcSMatt Macy 
2443eda14cbcSMatt Macy 	if (error != 0) {
2444eda14cbcSMatt Macy 		verify(errlist != NULL);
2445eda14cbcSMatt Macy 		if (vd_errlist != NULL)
2446eda14cbcSMatt Macy 			*vd_errlist = errlist;
2447eda14cbcSMatt Macy 		else
2448eda14cbcSMatt Macy 			fnvlist_free(errlist);
2449eda14cbcSMatt Macy 	}
2450eda14cbcSMatt Macy 
2451eda14cbcSMatt Macy 	return (error);
2452eda14cbcSMatt Macy }
2453eda14cbcSMatt Macy 
2454eda14cbcSMatt Macy static int
xlate_init_err(int err)2455eda14cbcSMatt Macy xlate_init_err(int err)
2456eda14cbcSMatt Macy {
2457eda14cbcSMatt Macy 	switch (err) {
2458eda14cbcSMatt Macy 	case ENODEV:
2459eda14cbcSMatt Macy 		return (EZFS_NODEVICE);
2460eda14cbcSMatt Macy 	case EINVAL:
2461eda14cbcSMatt Macy 	case EROFS:
2462eda14cbcSMatt Macy 		return (EZFS_BADDEV);
2463eda14cbcSMatt Macy 	case EBUSY:
2464eda14cbcSMatt Macy 		return (EZFS_INITIALIZING);
2465eda14cbcSMatt Macy 	case ESRCH:
2466eda14cbcSMatt Macy 		return (EZFS_NO_INITIALIZE);
2467eda14cbcSMatt Macy 	}
2468eda14cbcSMatt Macy 	return (err);
2469eda14cbcSMatt Macy }
2470eda14cbcSMatt Macy 
2471eda14cbcSMatt Macy /*
2472c0a83fe0SMartin Matuska  * Begin, suspend, cancel, or uninit (clear) the initialization (initializing
2473c0a83fe0SMartin Matuska  * of all free blocks) for the given vdevs in the given pool.
2474eda14cbcSMatt Macy  */
2475eda14cbcSMatt Macy static int
zpool_initialize_impl(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds,boolean_t wait)2476eda14cbcSMatt Macy zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2477eda14cbcSMatt Macy     nvlist_t *vds, boolean_t wait)
2478eda14cbcSMatt Macy {
2479eda14cbcSMatt Macy 	int err;
2480eda14cbcSMatt Macy 
2481eda14cbcSMatt Macy 	nvlist_t *vdev_guids = fnvlist_alloc();
2482eda14cbcSMatt Macy 	nvlist_t *guids_to_paths = fnvlist_alloc();
2483eda14cbcSMatt Macy 	nvlist_t *vd_errlist = NULL;
2484eda14cbcSMatt Macy 	nvlist_t *errlist;
2485eda14cbcSMatt Macy 	nvpair_t *elem;
2486eda14cbcSMatt Macy 
2487eda14cbcSMatt Macy 	err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2488eda14cbcSMatt Macy 	    guids_to_paths, &vd_errlist);
2489eda14cbcSMatt Macy 
2490eda14cbcSMatt Macy 	if (err != 0) {
2491eda14cbcSMatt Macy 		verify(vd_errlist != NULL);
2492eda14cbcSMatt Macy 		goto list_errors;
2493eda14cbcSMatt Macy 	}
2494eda14cbcSMatt Macy 
2495eda14cbcSMatt Macy 	err = lzc_initialize(zhp->zpool_name, cmd_type,
2496eda14cbcSMatt Macy 	    vdev_guids, &errlist);
2497eda14cbcSMatt Macy 
2498eda14cbcSMatt Macy 	if (err != 0) {
2499c0a83fe0SMartin Matuska 		if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2500c0a83fe0SMartin Matuska 		    ZPOOL_INITIALIZE_VDEVS, &vd_errlist) == 0) {
2501eda14cbcSMatt Macy 			goto list_errors;
2502eda14cbcSMatt Macy 		}
2503c0a83fe0SMartin Matuska 
2504c0a83fe0SMartin Matuska 		if (err == EINVAL && cmd_type == POOL_INITIALIZE_UNINIT) {
2505c0a83fe0SMartin Matuska 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2506c0a83fe0SMartin Matuska 			    "uninitialize is not supported by kernel"));
2507c0a83fe0SMartin Matuska 		}
2508c0a83fe0SMartin Matuska 
2509eda14cbcSMatt Macy 		(void) zpool_standard_error(zhp->zpool_hdl, err,
2510eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "operation failed"));
2511eda14cbcSMatt Macy 		goto out;
2512eda14cbcSMatt Macy 	}
2513eda14cbcSMatt Macy 
2514eda14cbcSMatt Macy 	if (wait) {
2515eda14cbcSMatt Macy 		for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2516eda14cbcSMatt Macy 		    elem = nvlist_next_nvpair(vdev_guids, elem)) {
2517eda14cbcSMatt Macy 
2518eda14cbcSMatt Macy 			uint64_t guid = fnvpair_value_uint64(elem);
2519eda14cbcSMatt Macy 
2520eda14cbcSMatt Macy 			err = lzc_wait_tag(zhp->zpool_name,
2521eda14cbcSMatt Macy 			    ZPOOL_WAIT_INITIALIZE, guid, NULL);
2522eda14cbcSMatt Macy 			if (err != 0) {
2523eda14cbcSMatt Macy 				(void) zpool_standard_error_fmt(zhp->zpool_hdl,
2524eda14cbcSMatt Macy 				    err, dgettext(TEXT_DOMAIN, "error "
2525eda14cbcSMatt Macy 				    "waiting for '%s' to initialize"),
2526eda14cbcSMatt Macy 				    nvpair_name(elem));
2527eda14cbcSMatt Macy 
2528eda14cbcSMatt Macy 				goto out;
2529eda14cbcSMatt Macy 			}
2530eda14cbcSMatt Macy 		}
2531eda14cbcSMatt Macy 	}
2532eda14cbcSMatt Macy 	goto out;
2533eda14cbcSMatt Macy 
2534eda14cbcSMatt Macy list_errors:
2535eda14cbcSMatt Macy 	for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2536eda14cbcSMatt Macy 	    elem = nvlist_next_nvpair(vd_errlist, elem)) {
2537eda14cbcSMatt Macy 		int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
25382a58b312SMartin Matuska 		const char *path;
2539eda14cbcSMatt Macy 
2540eda14cbcSMatt Macy 		if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2541eda14cbcSMatt Macy 		    &path) != 0)
2542eda14cbcSMatt Macy 			path = nvpair_name(elem);
2543eda14cbcSMatt Macy 
2544eda14cbcSMatt Macy 		(void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2545eda14cbcSMatt Macy 		    "cannot initialize '%s'", path);
2546eda14cbcSMatt Macy 	}
2547eda14cbcSMatt Macy 
2548eda14cbcSMatt Macy out:
2549eda14cbcSMatt Macy 	fnvlist_free(vdev_guids);
2550eda14cbcSMatt Macy 	fnvlist_free(guids_to_paths);
2551eda14cbcSMatt Macy 
2552eda14cbcSMatt Macy 	if (vd_errlist != NULL)
2553eda14cbcSMatt Macy 		fnvlist_free(vd_errlist);
2554eda14cbcSMatt Macy 
2555eda14cbcSMatt Macy 	return (err == 0 ? 0 : -1);
2556eda14cbcSMatt Macy }
2557eda14cbcSMatt Macy 
2558eda14cbcSMatt Macy int
zpool_initialize(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds)2559eda14cbcSMatt Macy zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2560eda14cbcSMatt Macy     nvlist_t *vds)
2561eda14cbcSMatt Macy {
2562eda14cbcSMatt Macy 	return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2563eda14cbcSMatt Macy }
2564eda14cbcSMatt Macy 
2565eda14cbcSMatt Macy int
zpool_initialize_wait(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds)2566eda14cbcSMatt Macy zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2567eda14cbcSMatt Macy     nvlist_t *vds)
2568eda14cbcSMatt Macy {
2569eda14cbcSMatt Macy 	return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2570eda14cbcSMatt Macy }
2571eda14cbcSMatt Macy 
2572eda14cbcSMatt Macy static int
xlate_trim_err(int err)2573eda14cbcSMatt Macy xlate_trim_err(int err)
2574eda14cbcSMatt Macy {
2575eda14cbcSMatt Macy 	switch (err) {
2576eda14cbcSMatt Macy 	case ENODEV:
2577eda14cbcSMatt Macy 		return (EZFS_NODEVICE);
2578eda14cbcSMatt Macy 	case EINVAL:
2579eda14cbcSMatt Macy 	case EROFS:
2580eda14cbcSMatt Macy 		return (EZFS_BADDEV);
2581eda14cbcSMatt Macy 	case EBUSY:
2582eda14cbcSMatt Macy 		return (EZFS_TRIMMING);
2583eda14cbcSMatt Macy 	case ESRCH:
2584eda14cbcSMatt Macy 		return (EZFS_NO_TRIM);
2585eda14cbcSMatt Macy 	case EOPNOTSUPP:
2586eda14cbcSMatt Macy 		return (EZFS_TRIM_NOTSUP);
2587eda14cbcSMatt Macy 	}
2588eda14cbcSMatt Macy 	return (err);
2589eda14cbcSMatt Macy }
2590eda14cbcSMatt Macy 
2591eda14cbcSMatt Macy static int
zpool_trim_wait(zpool_handle_t * zhp,nvlist_t * vdev_guids)2592eda14cbcSMatt Macy zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids)
2593eda14cbcSMatt Macy {
2594eda14cbcSMatt Macy 	int err;
2595eda14cbcSMatt Macy 	nvpair_t *elem;
2596eda14cbcSMatt Macy 
2597eda14cbcSMatt Macy 	for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2598eda14cbcSMatt Macy 	    elem = nvlist_next_nvpair(vdev_guids, elem)) {
2599eda14cbcSMatt Macy 
2600eda14cbcSMatt Macy 		uint64_t guid = fnvpair_value_uint64(elem);
2601eda14cbcSMatt Macy 
2602eda14cbcSMatt Macy 		err = lzc_wait_tag(zhp->zpool_name,
2603eda14cbcSMatt Macy 		    ZPOOL_WAIT_TRIM, guid, NULL);
2604eda14cbcSMatt Macy 		if (err != 0) {
2605eda14cbcSMatt Macy 			(void) zpool_standard_error_fmt(zhp->zpool_hdl,
2606eda14cbcSMatt Macy 			    err, dgettext(TEXT_DOMAIN, "error "
2607eda14cbcSMatt Macy 			    "waiting to trim '%s'"), nvpair_name(elem));
2608eda14cbcSMatt Macy 
2609eda14cbcSMatt Macy 			return (err);
2610eda14cbcSMatt Macy 		}
2611eda14cbcSMatt Macy 	}
2612eda14cbcSMatt Macy 	return (0);
2613eda14cbcSMatt Macy }
2614eda14cbcSMatt Macy 
2615eda14cbcSMatt Macy /*
2616eda14cbcSMatt Macy  * Check errlist and report any errors, omitting ones which should be
2617eda14cbcSMatt Macy  * suppressed. Returns B_TRUE if any errors were reported.
2618eda14cbcSMatt Macy  */
2619eda14cbcSMatt Macy static boolean_t
check_trim_errs(zpool_handle_t * zhp,trimflags_t * trim_flags,nvlist_t * guids_to_paths,nvlist_t * vds,nvlist_t * errlist)2620eda14cbcSMatt Macy check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags,
2621eda14cbcSMatt Macy     nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist)
2622eda14cbcSMatt Macy {
2623eda14cbcSMatt Macy 	nvpair_t *elem;
2624eda14cbcSMatt Macy 	boolean_t reported_errs = B_FALSE;
2625eda14cbcSMatt Macy 	int num_vds = 0;
2626eda14cbcSMatt Macy 	int num_suppressed_errs = 0;
2627eda14cbcSMatt Macy 
2628eda14cbcSMatt Macy 	for (elem = nvlist_next_nvpair(vds, NULL);
2629eda14cbcSMatt Macy 	    elem != NULL; elem = nvlist_next_nvpair(vds, elem)) {
2630eda14cbcSMatt Macy 		num_vds++;
2631eda14cbcSMatt Macy 	}
2632eda14cbcSMatt Macy 
2633eda14cbcSMatt Macy 	for (elem = nvlist_next_nvpair(errlist, NULL);
2634eda14cbcSMatt Macy 	    elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) {
2635eda14cbcSMatt Macy 		int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
26362a58b312SMartin Matuska 		const char *path;
2637eda14cbcSMatt Macy 
2638eda14cbcSMatt Macy 		/*
2639eda14cbcSMatt Macy 		 * If only the pool was specified, and it was not a secure
2640eda14cbcSMatt Macy 		 * trim then suppress warnings for individual vdevs which
2641eda14cbcSMatt Macy 		 * do not support trimming.
2642eda14cbcSMatt Macy 		 */
2643eda14cbcSMatt Macy 		if (vd_error == EZFS_TRIM_NOTSUP &&
2644eda14cbcSMatt Macy 		    trim_flags->fullpool &&
2645eda14cbcSMatt Macy 		    !trim_flags->secure) {
2646eda14cbcSMatt Macy 			num_suppressed_errs++;
2647eda14cbcSMatt Macy 			continue;
2648eda14cbcSMatt Macy 		}
2649eda14cbcSMatt Macy 
2650eda14cbcSMatt Macy 		reported_errs = B_TRUE;
2651eda14cbcSMatt Macy 		if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2652eda14cbcSMatt Macy 		    &path) != 0)
2653eda14cbcSMatt Macy 			path = nvpair_name(elem);
2654eda14cbcSMatt Macy 
2655eda14cbcSMatt Macy 		(void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2656eda14cbcSMatt Macy 		    "cannot trim '%s'", path);
2657eda14cbcSMatt Macy 	}
2658eda14cbcSMatt Macy 
2659eda14cbcSMatt Macy 	if (num_suppressed_errs == num_vds) {
2660eda14cbcSMatt Macy 		(void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2661eda14cbcSMatt Macy 		    "no devices in pool support trim operations"));
2662eda14cbcSMatt Macy 		(void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP,
2663eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot trim")));
2664eda14cbcSMatt Macy 		reported_errs = B_TRUE;
2665eda14cbcSMatt Macy 	}
2666eda14cbcSMatt Macy 
2667eda14cbcSMatt Macy 	return (reported_errs);
2668eda14cbcSMatt Macy }
2669eda14cbcSMatt Macy 
2670eda14cbcSMatt Macy /*
2671eda14cbcSMatt Macy  * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2672eda14cbcSMatt Macy  * the given vdevs in the given pool.
2673eda14cbcSMatt Macy  */
2674eda14cbcSMatt Macy int
zpool_trim(zpool_handle_t * zhp,pool_trim_func_t cmd_type,nvlist_t * vds,trimflags_t * trim_flags)2675eda14cbcSMatt Macy zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2676eda14cbcSMatt Macy     trimflags_t *trim_flags)
2677eda14cbcSMatt Macy {
2678eda14cbcSMatt Macy 	int err;
2679eda14cbcSMatt Macy 	int retval = 0;
2680eda14cbcSMatt Macy 
2681eda14cbcSMatt Macy 	nvlist_t *vdev_guids = fnvlist_alloc();
2682eda14cbcSMatt Macy 	nvlist_t *guids_to_paths = fnvlist_alloc();
2683eda14cbcSMatt Macy 	nvlist_t *errlist = NULL;
2684eda14cbcSMatt Macy 
2685eda14cbcSMatt Macy 	err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2686eda14cbcSMatt Macy 	    guids_to_paths, &errlist);
2687eda14cbcSMatt Macy 	if (err != 0) {
2688eda14cbcSMatt Macy 		check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist);
2689eda14cbcSMatt Macy 		retval = -1;
2690eda14cbcSMatt Macy 		goto out;
2691eda14cbcSMatt Macy 	}
2692eda14cbcSMatt Macy 
2693eda14cbcSMatt Macy 	err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2694eda14cbcSMatt Macy 	    trim_flags->secure, vdev_guids, &errlist);
2695eda14cbcSMatt Macy 	if (err != 0) {
2696eda14cbcSMatt Macy 		nvlist_t *vd_errlist;
2697eda14cbcSMatt Macy 		if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2698eda14cbcSMatt Macy 		    ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) {
2699eda14cbcSMatt Macy 			if (check_trim_errs(zhp, trim_flags, guids_to_paths,
2700eda14cbcSMatt Macy 			    vds, vd_errlist)) {
2701eda14cbcSMatt Macy 				retval = -1;
2702eda14cbcSMatt Macy 				goto out;
2703eda14cbcSMatt Macy 			}
2704eda14cbcSMatt Macy 		} else {
27051f1e2261SMartin Matuska 			char errbuf[ERRBUFLEN];
2706eda14cbcSMatt Macy 
27071f1e2261SMartin Matuska 			(void) snprintf(errbuf, sizeof (errbuf),
2708eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "operation failed"));
27091f1e2261SMartin Matuska 			zpool_standard_error(zhp->zpool_hdl, err, errbuf);
2710eda14cbcSMatt Macy 			retval = -1;
2711eda14cbcSMatt Macy 			goto out;
2712eda14cbcSMatt Macy 		}
2713eda14cbcSMatt Macy 	}
2714eda14cbcSMatt Macy 
2715eda14cbcSMatt Macy 
2716eda14cbcSMatt Macy 	if (trim_flags->wait)
2717eda14cbcSMatt Macy 		retval = zpool_trim_wait(zhp, vdev_guids);
2718eda14cbcSMatt Macy 
2719eda14cbcSMatt Macy out:
2720eda14cbcSMatt Macy 	if (errlist != NULL)
2721eda14cbcSMatt Macy 		fnvlist_free(errlist);
2722eda14cbcSMatt Macy 	fnvlist_free(vdev_guids);
2723eda14cbcSMatt Macy 	fnvlist_free(guids_to_paths);
2724eda14cbcSMatt Macy 	return (retval);
2725eda14cbcSMatt Macy }
2726eda14cbcSMatt Macy 
2727eda14cbcSMatt Macy /*
2728eda14cbcSMatt Macy  * Scan the pool.
2729eda14cbcSMatt Macy  */
2730eda14cbcSMatt Macy int
zpool_scan(zpool_handle_t * zhp,pool_scan_func_t func,pool_scrub_cmd_t cmd)2731eda14cbcSMatt Macy zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2732eda14cbcSMatt Macy {
27331f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2734eda14cbcSMatt Macy 	int err;
2735eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2736eda14cbcSMatt Macy 
2737c0a83fe0SMartin Matuska 	nvlist_t *args = fnvlist_alloc();
2738c0a83fe0SMartin Matuska 	fnvlist_add_uint64(args, "scan_type", (uint64_t)func);
2739c0a83fe0SMartin Matuska 	fnvlist_add_uint64(args, "scan_command", (uint64_t)cmd);
2740c0a83fe0SMartin Matuska 
2741c0a83fe0SMartin Matuska 	err = lzc_scrub(ZFS_IOC_POOL_SCRUB, zhp->zpool_name, args, NULL);
2742c0a83fe0SMartin Matuska 	fnvlist_free(args);
2743c0a83fe0SMartin Matuska 
2744c0a83fe0SMartin Matuska 	if (err == 0) {
2745c0a83fe0SMartin Matuska 		return (0);
2746c0a83fe0SMartin Matuska 	} else if (err == ZFS_ERR_IOC_CMD_UNAVAIL) {
2747c0a83fe0SMartin Matuska 		zfs_cmd_t zc = {"\0"};
2748c0a83fe0SMartin Matuska 		(void) strlcpy(zc.zc_name, zhp->zpool_name,
2749c0a83fe0SMartin Matuska 		    sizeof (zc.zc_name));
2750eda14cbcSMatt Macy 		zc.zc_cookie = func;
2751eda14cbcSMatt Macy 		zc.zc_flags = cmd;
2752eda14cbcSMatt Macy 
2753eda14cbcSMatt Macy 		if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2754eda14cbcSMatt Macy 			return (0);
2755c0a83fe0SMartin Matuska 	}
2756eda14cbcSMatt Macy 
2757c0a83fe0SMartin Matuska 	/*
2758c0a83fe0SMartin Matuska 	 * An ECANCELED on a scrub means one of the following:
2759c0a83fe0SMartin Matuska 	 * 1. we resumed a paused scrub.
2760c0a83fe0SMartin Matuska 	 * 2. we resumed a paused error scrub.
2761c0a83fe0SMartin Matuska 	 * 3. Error scrub is not run because of no error log.
2762c0a83fe0SMartin Matuska 	 */
2763c0a83fe0SMartin Matuska 	if (err == ECANCELED && (func == POOL_SCAN_SCRUB ||
2764c0a83fe0SMartin Matuska 	    func == POOL_SCAN_ERRORSCRUB) && cmd == POOL_SCRUB_NORMAL)
2765eda14cbcSMatt Macy 		return (0);
2766c0a83fe0SMartin Matuska 	/*
2767c0a83fe0SMartin Matuska 	 * The following cases have been handled here:
2768c0a83fe0SMartin Matuska 	 * 1. Paused a scrub/error scrub if there is none in progress.
2769c0a83fe0SMartin Matuska 	 */
2770c0a83fe0SMartin Matuska 	if (err == ENOENT && func != POOL_SCAN_NONE && cmd ==
2771c0a83fe0SMartin Matuska 	    POOL_SCRUB_PAUSE) {
2772eda14cbcSMatt Macy 		return (0);
2773c0a83fe0SMartin Matuska 	}
2774eda14cbcSMatt Macy 
2775c0a83fe0SMartin Matuska 	ASSERT3U(func, >=, POOL_SCAN_NONE);
2776c0a83fe0SMartin Matuska 	ASSERT3U(func, <, POOL_SCAN_FUNCS);
2777c0a83fe0SMartin Matuska 
2778c0a83fe0SMartin Matuska 	if (func == POOL_SCAN_SCRUB || func == POOL_SCAN_ERRORSCRUB) {
2779eda14cbcSMatt Macy 		if (cmd == POOL_SCRUB_PAUSE) {
27801f1e2261SMartin Matuska 			(void) snprintf(errbuf, sizeof (errbuf),
27811f1e2261SMartin Matuska 			    dgettext(TEXT_DOMAIN, "cannot pause scrubbing %s"),
2782c0a83fe0SMartin Matuska 			    zhp->zpool_name);
2783eda14cbcSMatt Macy 		} else {
2784eda14cbcSMatt Macy 			assert(cmd == POOL_SCRUB_NORMAL);
27851f1e2261SMartin Matuska 			(void) snprintf(errbuf, sizeof (errbuf),
27861f1e2261SMartin Matuska 			    dgettext(TEXT_DOMAIN, "cannot scrub %s"),
2787c0a83fe0SMartin Matuska 			    zhp->zpool_name);
2788eda14cbcSMatt Macy 		}
2789eda14cbcSMatt Macy 	} else if (func == POOL_SCAN_RESILVER) {
2790eda14cbcSMatt Macy 		assert(cmd == POOL_SCRUB_NORMAL);
27911f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2792c0a83fe0SMartin Matuska 		    "cannot restart resilver on %s"), zhp->zpool_name);
2793eda14cbcSMatt Macy 	} else if (func == POOL_SCAN_NONE) {
27941f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2795c0a83fe0SMartin Matuska 		    "cannot cancel scrubbing %s"), zhp->zpool_name);
2796eda14cbcSMatt Macy 	} else {
2797eda14cbcSMatt Macy 		assert(!"unexpected result");
2798eda14cbcSMatt Macy 	}
2799eda14cbcSMatt Macy 
2800c0a83fe0SMartin Matuska 	/*
280187bf66d4SMartin Matuska 	 * With EBUSY, six cases are possible:
2802c0a83fe0SMartin Matuska 	 *
2803c0a83fe0SMartin Matuska 	 * Current state		Requested
2804c0a83fe0SMartin Matuska 	 * 1. Normal Scrub Running	Normal Scrub or Error Scrub
2805c0a83fe0SMartin Matuska 	 * 2. Normal Scrub Paused	Error Scrub
2806c0a83fe0SMartin Matuska 	 * 3. Normal Scrub Paused 	Pause Normal Scrub
2807c0a83fe0SMartin Matuska 	 * 4. Error Scrub Running	Normal Scrub or Error Scrub
2808c0a83fe0SMartin Matuska 	 * 5. Error Scrub Paused	Pause Error Scrub
2809c0a83fe0SMartin Matuska 	 * 6. Resilvering		Anything else
2810c0a83fe0SMartin Matuska 	 */
2811eda14cbcSMatt Macy 	if (err == EBUSY) {
2812eda14cbcSMatt Macy 		nvlist_t *nvroot;
2813eda14cbcSMatt Macy 		pool_scan_stat_t *ps = NULL;
2814eda14cbcSMatt Macy 		uint_t psc;
2815eda14cbcSMatt Macy 
2816da5137abSMartin Matuska 		nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2817da5137abSMartin Matuska 		    ZPOOL_CONFIG_VDEV_TREE);
2818eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64_array(nvroot,
2819eda14cbcSMatt Macy 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2820eda14cbcSMatt Macy 		if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2821eda14cbcSMatt Macy 		    ps->pss_state == DSS_SCANNING) {
2822c0a83fe0SMartin Matuska 			if (ps->pss_pass_scrub_pause == 0) {
2823c0a83fe0SMartin Matuska 				/* handles case 1 */
2824c0a83fe0SMartin Matuska 				assert(cmd == POOL_SCRUB_NORMAL);
2825c0a83fe0SMartin Matuska 				return (zfs_error(hdl, EZFS_SCRUBBING,
28261f1e2261SMartin Matuska 				    errbuf));
2827eda14cbcSMatt Macy 			} else {
2828c0a83fe0SMartin Matuska 				if (func == POOL_SCAN_ERRORSCRUB) {
2829c0a83fe0SMartin Matuska 					/* handles case 2 */
2830c0a83fe0SMartin Matuska 					ASSERT3U(cmd, ==, POOL_SCRUB_NORMAL);
2831c0a83fe0SMartin Matuska 					return (zfs_error(hdl,
2832c0a83fe0SMartin Matuska 					    EZFS_SCRUB_PAUSED_TO_CANCEL,
2833c0a83fe0SMartin Matuska 					    errbuf));
2834c0a83fe0SMartin Matuska 				} else {
2835c0a83fe0SMartin Matuska 					/* handles case 3 */
2836c0a83fe0SMartin Matuska 					ASSERT3U(func, ==, POOL_SCAN_SCRUB);
2837c0a83fe0SMartin Matuska 					ASSERT3U(cmd, ==, POOL_SCRUB_PAUSE);
2838c0a83fe0SMartin Matuska 					return (zfs_error(hdl,
2839c0a83fe0SMartin Matuska 					    EZFS_SCRUB_PAUSED, errbuf));
2840c0a83fe0SMartin Matuska 				}
2841c0a83fe0SMartin Matuska 			}
2842c0a83fe0SMartin Matuska 		} else if (ps &&
2843c0a83fe0SMartin Matuska 		    ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB &&
2844c0a83fe0SMartin Matuska 		    ps->pss_error_scrub_state == DSS_ERRORSCRUBBING) {
2845c0a83fe0SMartin Matuska 			if (ps->pss_pass_error_scrub_pause == 0) {
2846c0a83fe0SMartin Matuska 				/* handles case 4 */
2847c0a83fe0SMartin Matuska 				ASSERT3U(cmd, ==, POOL_SCRUB_NORMAL);
2848c0a83fe0SMartin Matuska 				return (zfs_error(hdl, EZFS_ERRORSCRUBBING,
2849c0a83fe0SMartin Matuska 				    errbuf));
2850c0a83fe0SMartin Matuska 			} else {
2851c0a83fe0SMartin Matuska 				/* handles case 5 */
2852c0a83fe0SMartin Matuska 				ASSERT3U(func, ==, POOL_SCAN_ERRORSCRUB);
2853c0a83fe0SMartin Matuska 				ASSERT3U(cmd, ==, POOL_SCRUB_PAUSE);
2854c0a83fe0SMartin Matuska 				return (zfs_error(hdl, EZFS_ERRORSCRUB_PAUSED,
2855c0a83fe0SMartin Matuska 				    errbuf));
2856c0a83fe0SMartin Matuska 			}
2857c0a83fe0SMartin Matuska 		} else {
2858c0a83fe0SMartin Matuska 			/* handles case 6 */
28591f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_RESILVERING, errbuf));
2860eda14cbcSMatt Macy 		}
2861eda14cbcSMatt Macy 	} else if (err == ENOENT) {
28621f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NO_SCRUB, errbuf));
2863eda14cbcSMatt Macy 	} else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
28641f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, errbuf));
2865eda14cbcSMatt Macy 	} else {
28661f1e2261SMartin Matuska 		return (zpool_standard_error(hdl, err, errbuf));
2867eda14cbcSMatt Macy 	}
2868eda14cbcSMatt Macy }
2869eda14cbcSMatt Macy 
2870eda14cbcSMatt Macy /*
2871eda14cbcSMatt Macy  * Find a vdev that matches the search criteria specified. We use the
2872eda14cbcSMatt Macy  * the nvpair name to determine how we should look for the device.
2873eda14cbcSMatt Macy  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2874eda14cbcSMatt Macy  * spare; but FALSE if its an INUSE spare.
2875ce4dcb97SMartin Matuska  *
2876ce4dcb97SMartin Matuska  * If 'return_parent' is set, then return the *parent* of the vdev you're
2877ce4dcb97SMartin Matuska  * searching for rather than the vdev itself.
2878eda14cbcSMatt Macy  */
2879eda14cbcSMatt Macy static nvlist_t *
vdev_to_nvlist_iter(nvlist_t * nv,nvlist_t * search,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log,boolean_t return_parent)2880eda14cbcSMatt Macy vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2881ce4dcb97SMartin Matuska     boolean_t *l2cache, boolean_t *log, boolean_t return_parent)
2882eda14cbcSMatt Macy {
2883eda14cbcSMatt Macy 	uint_t c, children;
2884eda14cbcSMatt Macy 	nvlist_t **child;
2885eda14cbcSMatt Macy 	nvlist_t *ret;
2886eda14cbcSMatt Macy 	uint64_t is_log;
28872a58b312SMartin Matuska 	const char *srchkey;
2888eda14cbcSMatt Macy 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2889ce4dcb97SMartin Matuska 	const char *tmp = NULL;
2890ce4dcb97SMartin Matuska 	boolean_t is_root;
2891eda14cbcSMatt Macy 
2892eda14cbcSMatt Macy 	/* Nothing to look for */
2893eda14cbcSMatt Macy 	if (search == NULL || pair == NULL)
2894eda14cbcSMatt Macy 		return (NULL);
2895eda14cbcSMatt Macy 
2896eda14cbcSMatt Macy 	/* Obtain the key we will use to search */
2897eda14cbcSMatt Macy 	srchkey = nvpair_name(pair);
2898eda14cbcSMatt Macy 
2899ce4dcb97SMartin Matuska 	nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &tmp);
2900ce4dcb97SMartin Matuska 	if (strcmp(tmp, "root") == 0)
2901ce4dcb97SMartin Matuska 		is_root = B_TRUE;
2902ce4dcb97SMartin Matuska 	else
2903ce4dcb97SMartin Matuska 		is_root = B_FALSE;
2904ce4dcb97SMartin Matuska 
2905eda14cbcSMatt Macy 	switch (nvpair_type(pair)) {
2906eda14cbcSMatt Macy 	case DATA_TYPE_UINT64:
2907eda14cbcSMatt Macy 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2908da5137abSMartin Matuska 			uint64_t srchval = fnvpair_value_uint64(pair);
2909da5137abSMartin Matuska 			uint64_t theguid = fnvlist_lookup_uint64(nv,
2910da5137abSMartin Matuska 			    ZPOOL_CONFIG_GUID);
2911eda14cbcSMatt Macy 			if (theguid == srchval)
2912eda14cbcSMatt Macy 				return (nv);
2913eda14cbcSMatt Macy 		}
2914eda14cbcSMatt Macy 		break;
2915eda14cbcSMatt Macy 
2916eda14cbcSMatt Macy 	case DATA_TYPE_STRING: {
29172a58b312SMartin Matuska 		const char *srchval, *val;
2918eda14cbcSMatt Macy 
2919da5137abSMartin Matuska 		srchval = fnvpair_value_string(pair);
2920eda14cbcSMatt Macy 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2921eda14cbcSMatt Macy 			break;
2922eda14cbcSMatt Macy 
2923eda14cbcSMatt Macy 		/*
2924eda14cbcSMatt Macy 		 * Search for the requested value. Special cases:
2925eda14cbcSMatt Macy 		 *
2926eda14cbcSMatt Macy 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
2927eda14cbcSMatt Macy 		 *   "-part1", or "p1".  The suffix is hidden from the user,
2928eda14cbcSMatt Macy 		 *   but included in the string, so this matches around it.
2929eda14cbcSMatt Macy 		 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2930eda14cbcSMatt Macy 		 *   is used to check all possible expanded paths.
2931eda14cbcSMatt Macy 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2932eda14cbcSMatt Macy 		 *
2933eda14cbcSMatt Macy 		 * Otherwise, all other searches are simple string compares.
2934eda14cbcSMatt Macy 		 */
2935eda14cbcSMatt Macy 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2936eda14cbcSMatt Macy 			uint64_t wholedisk = 0;
2937eda14cbcSMatt Macy 
2938eda14cbcSMatt Macy 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2939eda14cbcSMatt Macy 			    &wholedisk);
2940eda14cbcSMatt Macy 			if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2941eda14cbcSMatt Macy 				return (nv);
2942eda14cbcSMatt Macy 
2943dbd5678dSMartin Matuska 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0) {
2944eda14cbcSMatt Macy 			char *type, *idx, *end, *p;
2945eda14cbcSMatt Macy 			uint64_t id, vdev_id;
2946eda14cbcSMatt Macy 
2947eda14cbcSMatt Macy 			/*
2948eda14cbcSMatt Macy 			 * Determine our vdev type, keeping in mind
2949eda14cbcSMatt Macy 			 * that the srchval is composed of a type and
2950eda14cbcSMatt Macy 			 * vdev id pair (i.e. mirror-4).
2951eda14cbcSMatt Macy 			 */
2952eda14cbcSMatt Macy 			if ((type = strdup(srchval)) == NULL)
2953eda14cbcSMatt Macy 				return (NULL);
2954eda14cbcSMatt Macy 
2955eda14cbcSMatt Macy 			if ((p = strrchr(type, '-')) == NULL) {
2956eda14cbcSMatt Macy 				free(type);
2957eda14cbcSMatt Macy 				break;
2958eda14cbcSMatt Macy 			}
2959eda14cbcSMatt Macy 			idx = p + 1;
2960eda14cbcSMatt Macy 			*p = '\0';
2961eda14cbcSMatt Macy 
2962eda14cbcSMatt Macy 			/*
2963eda14cbcSMatt Macy 			 * If the types don't match then keep looking.
2964eda14cbcSMatt Macy 			 */
2965eda14cbcSMatt Macy 			if (strncmp(val, type, strlen(val)) != 0) {
2966eda14cbcSMatt Macy 				free(type);
2967eda14cbcSMatt Macy 				break;
2968eda14cbcSMatt Macy 			}
2969eda14cbcSMatt Macy 
2970eda14cbcSMatt Macy 			verify(zpool_vdev_is_interior(type));
2971eda14cbcSMatt Macy 
2972da5137abSMartin Matuska 			id = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID);
2973eda14cbcSMatt Macy 			errno = 0;
2974eda14cbcSMatt Macy 			vdev_id = strtoull(idx, &end, 10);
2975eda14cbcSMatt Macy 
29766db169e9SMartin Matuska 			/*
29776db169e9SMartin Matuska 			 * If we are looking for a raidz and a parity is
29786db169e9SMartin Matuska 			 * specified, make sure it matches.
29796db169e9SMartin Matuska 			 */
29806db169e9SMartin Matuska 			int rzlen = strlen(VDEV_TYPE_RAIDZ);
29816db169e9SMartin Matuska 			assert(rzlen == strlen(VDEV_TYPE_DRAID));
29826db169e9SMartin Matuska 			int typlen = strlen(type);
29836db169e9SMartin Matuska 			if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 ||
29846db169e9SMartin Matuska 			    strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) &&
29856db169e9SMartin Matuska 			    typlen != rzlen) {
29866db169e9SMartin Matuska 				uint64_t vdev_parity;
29876db169e9SMartin Matuska 				int parity = *(type + rzlen) - '0';
29886db169e9SMartin Matuska 
29896db169e9SMartin Matuska 				if (parity <= 0 || parity > 3 ||
29906db169e9SMartin Matuska 				    (typlen - rzlen) != 1) {
29916db169e9SMartin Matuska 					/*
29926db169e9SMartin Matuska 					 * Nonsense parity specified, can
29936db169e9SMartin Matuska 					 * never match
29946db169e9SMartin Matuska 					 */
29956db169e9SMartin Matuska 					free(type);
29966db169e9SMartin Matuska 					return (NULL);
29976db169e9SMartin Matuska 				}
2998da5137abSMartin Matuska 				vdev_parity = fnvlist_lookup_uint64(nv,
2999da5137abSMartin Matuska 				    ZPOOL_CONFIG_NPARITY);
30006db169e9SMartin Matuska 				if ((int)vdev_parity != parity) {
30016db169e9SMartin Matuska 					free(type);
30026db169e9SMartin Matuska 					break;
30036db169e9SMartin Matuska 				}
30046db169e9SMartin Matuska 			}
30056db169e9SMartin Matuska 
3006eda14cbcSMatt Macy 			free(type);
3007eda14cbcSMatt Macy 			if (errno != 0)
3008eda14cbcSMatt Macy 				return (NULL);
3009eda14cbcSMatt Macy 
3010eda14cbcSMatt Macy 			/*
3011eda14cbcSMatt Macy 			 * Now verify that we have the correct vdev id.
3012eda14cbcSMatt Macy 			 */
3013eda14cbcSMatt Macy 			if (vdev_id == id)
3014eda14cbcSMatt Macy 				return (nv);
3015eda14cbcSMatt Macy 		}
3016eda14cbcSMatt Macy 
3017eda14cbcSMatt Macy 		/*
3018eda14cbcSMatt Macy 		 * Common case
3019eda14cbcSMatt Macy 		 */
3020eda14cbcSMatt Macy 		if (strcmp(srchval, val) == 0)
3021eda14cbcSMatt Macy 			return (nv);
3022eda14cbcSMatt Macy 		break;
3023eda14cbcSMatt Macy 	}
3024eda14cbcSMatt Macy 
3025eda14cbcSMatt Macy 	default:
3026eda14cbcSMatt Macy 		break;
3027eda14cbcSMatt Macy 	}
3028eda14cbcSMatt Macy 
3029eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3030eda14cbcSMatt Macy 	    &child, &children) != 0)
3031eda14cbcSMatt Macy 		return (NULL);
3032eda14cbcSMatt Macy 
3033eda14cbcSMatt Macy 	for (c = 0; c < children; c++) {
3034eda14cbcSMatt Macy 		if ((ret = vdev_to_nvlist_iter(child[c], search,
3035ce4dcb97SMartin Matuska 		    avail_spare, l2cache, NULL, return_parent)) != NULL) {
3036eda14cbcSMatt Macy 			/*
3037eda14cbcSMatt Macy 			 * The 'is_log' value is only set for the toplevel
3038eda14cbcSMatt Macy 			 * vdev, not the leaf vdevs.  So we always lookup the
3039eda14cbcSMatt Macy 			 * log device from the root of the vdev tree (where
3040eda14cbcSMatt Macy 			 * 'log' is non-NULL).
3041eda14cbcSMatt Macy 			 */
3042eda14cbcSMatt Macy 			if (log != NULL &&
3043eda14cbcSMatt Macy 			    nvlist_lookup_uint64(child[c],
3044eda14cbcSMatt Macy 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
3045eda14cbcSMatt Macy 			    is_log) {
3046eda14cbcSMatt Macy 				*log = B_TRUE;
3047eda14cbcSMatt Macy 			}
3048ce4dcb97SMartin Matuska 			return (ret && return_parent && !is_root ? nv : ret);
3049eda14cbcSMatt Macy 		}
3050eda14cbcSMatt Macy 	}
3051eda14cbcSMatt Macy 
3052eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
3053eda14cbcSMatt Macy 	    &child, &children) == 0) {
3054eda14cbcSMatt Macy 		for (c = 0; c < children; c++) {
3055eda14cbcSMatt Macy 			if ((ret = vdev_to_nvlist_iter(child[c], search,
3056ce4dcb97SMartin Matuska 			    avail_spare, l2cache, NULL, return_parent))
3057ce4dcb97SMartin Matuska 			    != NULL) {
3058eda14cbcSMatt Macy 				*avail_spare = B_TRUE;
3059ce4dcb97SMartin Matuska 				return (ret && return_parent &&
3060ce4dcb97SMartin Matuska 				    !is_root ? nv : ret);
3061eda14cbcSMatt Macy 			}
3062eda14cbcSMatt Macy 		}
3063eda14cbcSMatt Macy 	}
3064eda14cbcSMatt Macy 
3065eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3066eda14cbcSMatt Macy 	    &child, &children) == 0) {
3067eda14cbcSMatt Macy 		for (c = 0; c < children; c++) {
3068eda14cbcSMatt Macy 			if ((ret = vdev_to_nvlist_iter(child[c], search,
3069ce4dcb97SMartin Matuska 			    avail_spare, l2cache, NULL, return_parent))
3070ce4dcb97SMartin Matuska 			    != NULL) {
3071eda14cbcSMatt Macy 				*l2cache = B_TRUE;
3072ce4dcb97SMartin Matuska 				return (ret && return_parent &&
3073ce4dcb97SMartin Matuska 				    !is_root ? nv : ret);
3074eda14cbcSMatt Macy 			}
3075eda14cbcSMatt Macy 		}
3076eda14cbcSMatt Macy 	}
3077eda14cbcSMatt Macy 
3078eda14cbcSMatt Macy 	return (NULL);
3079eda14cbcSMatt Macy }
3080eda14cbcSMatt Macy 
3081eda14cbcSMatt Macy /*
3082eda14cbcSMatt Macy  * Given a physical path or guid, find the associated vdev.
3083eda14cbcSMatt Macy  */
3084eda14cbcSMatt Macy nvlist_t *
zpool_find_vdev_by_physpath(zpool_handle_t * zhp,const char * ppath,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)3085eda14cbcSMatt Macy zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
3086eda14cbcSMatt Macy     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
3087eda14cbcSMatt Macy {
3088eda14cbcSMatt Macy 	nvlist_t *search, *nvroot, *ret;
3089eda14cbcSMatt Macy 	uint64_t guid;
3090eda14cbcSMatt Macy 	char *end;
3091eda14cbcSMatt Macy 
3092da5137abSMartin Matuska 	search = fnvlist_alloc();
3093eda14cbcSMatt Macy 
3094eda14cbcSMatt Macy 	guid = strtoull(ppath, &end, 0);
3095eda14cbcSMatt Macy 	if (guid != 0 && *end == '\0') {
3096da5137abSMartin Matuska 		fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
3097eda14cbcSMatt Macy 	} else {
3098da5137abSMartin Matuska 		fnvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath);
3099eda14cbcSMatt Macy 	}
3100eda14cbcSMatt Macy 
3101da5137abSMartin Matuska 	nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
3102da5137abSMartin Matuska 	    ZPOOL_CONFIG_VDEV_TREE);
3103eda14cbcSMatt Macy 
3104eda14cbcSMatt Macy 	*avail_spare = B_FALSE;
3105eda14cbcSMatt Macy 	*l2cache = B_FALSE;
3106eda14cbcSMatt Macy 	if (log != NULL)
3107eda14cbcSMatt Macy 		*log = B_FALSE;
3108ce4dcb97SMartin Matuska 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log,
3109ce4dcb97SMartin Matuska 	    B_FALSE);
3110da5137abSMartin Matuska 	fnvlist_free(search);
3111eda14cbcSMatt Macy 
3112eda14cbcSMatt Macy 	return (ret);
3113eda14cbcSMatt Macy }
3114eda14cbcSMatt Macy 
3115eda14cbcSMatt Macy /*
3116eda14cbcSMatt Macy  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
3117eda14cbcSMatt Macy  */
3118eda14cbcSMatt Macy static boolean_t
zpool_vdev_is_interior(const char * name)3119eda14cbcSMatt Macy zpool_vdev_is_interior(const char *name)
3120eda14cbcSMatt Macy {
3121eda14cbcSMatt Macy 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
3122eda14cbcSMatt Macy 	    strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
3123eda14cbcSMatt Macy 	    strncmp(name,
3124eda14cbcSMatt Macy 	    VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
3125d411c1d6SMartin Matuska 	    strncmp(name, VDEV_TYPE_ROOT, strlen(VDEV_TYPE_ROOT)) == 0 ||
3126eda14cbcSMatt Macy 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
3127eda14cbcSMatt Macy 		return (B_TRUE);
31287877fdebSMatt Macy 
31297877fdebSMatt Macy 	if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 &&
31307877fdebSMatt Macy 	    !zpool_is_draid_spare(name))
31317877fdebSMatt Macy 		return (B_TRUE);
31327877fdebSMatt Macy 
3133eda14cbcSMatt Macy 	return (B_FALSE);
3134eda14cbcSMatt Macy }
3135eda14cbcSMatt Macy 
3136b356da80SMartin Matuska /*
3137ce4dcb97SMartin Matuska  * Lookup the nvlist for a given vdev or vdev's parent (depending on
3138ce4dcb97SMartin Matuska  * if 'return_parent' is set).
3139b356da80SMartin Matuska  */
3140ce4dcb97SMartin Matuska static nvlist_t *
__zpool_find_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log,boolean_t return_parent)3141ce4dcb97SMartin Matuska __zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
3142ce4dcb97SMartin Matuska     boolean_t *l2cache, boolean_t *log, boolean_t return_parent)
3143eda14cbcSMatt Macy {
3144eda14cbcSMatt Macy 	char *end;
3145eda14cbcSMatt Macy 	nvlist_t *nvroot, *search, *ret;
3146eda14cbcSMatt Macy 	uint64_t guid;
3147b356da80SMartin Matuska 	boolean_t __avail_spare, __l2cache, __log;
3148eda14cbcSMatt Macy 
3149da5137abSMartin Matuska 	search = fnvlist_alloc();
3150eda14cbcSMatt Macy 
3151eda14cbcSMatt Macy 	guid = strtoull(path, &end, 0);
3152eda14cbcSMatt Macy 	if (guid != 0 && *end == '\0') {
3153da5137abSMartin Matuska 		fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
3154eda14cbcSMatt Macy 	} else if (zpool_vdev_is_interior(path)) {
3155da5137abSMartin Matuska 		fnvlist_add_string(search, ZPOOL_CONFIG_TYPE, path);
3156eda14cbcSMatt Macy 	} else {
3157da5137abSMartin Matuska 		fnvlist_add_string(search, ZPOOL_CONFIG_PATH, path);
3158eda14cbcSMatt Macy 	}
3159eda14cbcSMatt Macy 
3160da5137abSMartin Matuska 	nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
3161da5137abSMartin Matuska 	    ZPOOL_CONFIG_VDEV_TREE);
3162eda14cbcSMatt Macy 
3163b356da80SMartin Matuska 	/*
3164b356da80SMartin Matuska 	 * User can pass NULL for avail_spare, l2cache, and log, but
3165b356da80SMartin Matuska 	 * we still need to provide variables to vdev_to_nvlist_iter(), so
3166b356da80SMartin Matuska 	 * just point them to junk variables here.
3167b356da80SMartin Matuska 	 */
3168b356da80SMartin Matuska 	if (!avail_spare)
3169b356da80SMartin Matuska 		avail_spare = &__avail_spare;
3170b356da80SMartin Matuska 	if (!l2cache)
3171b356da80SMartin Matuska 		l2cache = &__l2cache;
3172b356da80SMartin Matuska 	if (!log)
3173b356da80SMartin Matuska 		log = &__log;
3174b356da80SMartin Matuska 
3175eda14cbcSMatt Macy 	*avail_spare = B_FALSE;
3176eda14cbcSMatt Macy 	*l2cache = B_FALSE;
3177eda14cbcSMatt Macy 	if (log != NULL)
3178eda14cbcSMatt Macy 		*log = B_FALSE;
3179ce4dcb97SMartin Matuska 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log,
3180ce4dcb97SMartin Matuska 	    return_parent);
3181da5137abSMartin Matuska 	fnvlist_free(search);
3182eda14cbcSMatt Macy 
3183eda14cbcSMatt Macy 	return (ret);
3184eda14cbcSMatt Macy }
3185eda14cbcSMatt Macy 
3186ce4dcb97SMartin Matuska nvlist_t *
zpool_find_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)3187ce4dcb97SMartin Matuska zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
3188ce4dcb97SMartin Matuska     boolean_t *l2cache, boolean_t *log)
3189ce4dcb97SMartin Matuska {
3190ce4dcb97SMartin Matuska 	return (__zpool_find_vdev(zhp, path, avail_spare, l2cache, log,
3191ce4dcb97SMartin Matuska 	    B_FALSE));
3192ce4dcb97SMartin Matuska }
3193ce4dcb97SMartin Matuska 
3194ce4dcb97SMartin Matuska /* Given a vdev path, return its parent's nvlist */
3195ce4dcb97SMartin Matuska nvlist_t *
zpool_find_parent_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)3196ce4dcb97SMartin Matuska zpool_find_parent_vdev(zpool_handle_t *zhp, const char *path,
3197ce4dcb97SMartin Matuska     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
3198ce4dcb97SMartin Matuska {
3199ce4dcb97SMartin Matuska 	return (__zpool_find_vdev(zhp, path, avail_spare, l2cache, log,
3200ce4dcb97SMartin Matuska 	    B_TRUE));
3201ce4dcb97SMartin Matuska }
3202ce4dcb97SMartin Matuska 
3203eda14cbcSMatt Macy /*
3204eda14cbcSMatt Macy  * Convert a vdev path to a GUID.  Returns GUID or 0 on error.
3205eda14cbcSMatt Macy  *
3206eda14cbcSMatt Macy  * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3207eda14cbcSMatt Macy  * if the VDEV is a spare, l2cache, or log device.  If they're NULL then
3208eda14cbcSMatt Macy  * ignore them.
3209eda14cbcSMatt Macy  */
3210eda14cbcSMatt Macy static uint64_t
zpool_vdev_path_to_guid_impl(zpool_handle_t * zhp,const char * path,boolean_t * is_spare,boolean_t * is_l2cache,boolean_t * is_log)3211eda14cbcSMatt Macy zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
3212eda14cbcSMatt Macy     boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
3213eda14cbcSMatt Macy {
3214eda14cbcSMatt Macy 	boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
3215eda14cbcSMatt Macy 	nvlist_t *tgt;
3216eda14cbcSMatt Macy 
3217eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
3218eda14cbcSMatt Macy 	    &log)) == NULL)
3219eda14cbcSMatt Macy 		return (0);
3220eda14cbcSMatt Macy 
3221eda14cbcSMatt Macy 	if (is_spare != NULL)
3222eda14cbcSMatt Macy 		*is_spare = spare;
3223eda14cbcSMatt Macy 	if (is_l2cache != NULL)
3224eda14cbcSMatt Macy 		*is_l2cache = l2cache;
3225eda14cbcSMatt Macy 	if (is_log != NULL)
3226eda14cbcSMatt Macy 		*is_log = log;
3227eda14cbcSMatt Macy 
3228da5137abSMartin Matuska 	return (fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID));
3229eda14cbcSMatt Macy }
3230eda14cbcSMatt Macy 
3231eda14cbcSMatt Macy /* Convert a vdev path to a GUID.  Returns GUID or 0 on error. */
3232eda14cbcSMatt Macy uint64_t
zpool_vdev_path_to_guid(zpool_handle_t * zhp,const char * path)3233eda14cbcSMatt Macy zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
3234eda14cbcSMatt Macy {
3235eda14cbcSMatt Macy 	return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
3236eda14cbcSMatt Macy }
3237eda14cbcSMatt Macy 
3238eda14cbcSMatt Macy /*
3239eda14cbcSMatt Macy  * Bring the specified vdev online.   The 'flags' parameter is a set of the
3240eda14cbcSMatt Macy  * ZFS_ONLINE_* flags.
3241eda14cbcSMatt Macy  */
3242eda14cbcSMatt Macy int
zpool_vdev_online(zpool_handle_t * zhp,const char * path,int flags,vdev_state_t * newstate)3243eda14cbcSMatt Macy zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
3244eda14cbcSMatt Macy     vdev_state_t *newstate)
3245eda14cbcSMatt Macy {
3246eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
32471f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3248eda14cbcSMatt Macy 	nvlist_t *tgt;
3249eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache, islog;
3250eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3251eda14cbcSMatt Macy 
3252eda14cbcSMatt Macy 	if (flags & ZFS_ONLINE_EXPAND) {
32531f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
3254eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
3255eda14cbcSMatt Macy 	} else {
32561f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
3257eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
3258eda14cbcSMatt Macy 	}
3259eda14cbcSMatt Macy 
3260eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3261eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3262eda14cbcSMatt Macy 	    &islog)) == NULL)
32631f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3264eda14cbcSMatt Macy 
3265da5137abSMartin Matuska 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3266eda14cbcSMatt Macy 
326715f0b8c3SMartin Matuska 	if (!(flags & ZFS_ONLINE_SPARE) && avail_spare)
32681f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3269eda14cbcSMatt Macy 
3270da5137abSMartin Matuska #ifndef __FreeBSD__
32712a58b312SMartin Matuska 	const char *pathname;
3272eda14cbcSMatt Macy 	if ((flags & ZFS_ONLINE_EXPAND ||
3273eda14cbcSMatt Macy 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
3274eda14cbcSMatt Macy 	    nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
3275eda14cbcSMatt Macy 		uint64_t wholedisk = 0;
3276eda14cbcSMatt Macy 
3277eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
3278eda14cbcSMatt Macy 		    &wholedisk);
3279eda14cbcSMatt Macy 
3280eda14cbcSMatt Macy 		/*
3281eda14cbcSMatt Macy 		 * XXX - L2ARC 1.0 devices can't support expansion.
3282eda14cbcSMatt Macy 		 */
3283eda14cbcSMatt Macy 		if (l2cache) {
3284eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3285eda14cbcSMatt Macy 			    "cannot expand cache devices"));
32861f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf));
3287eda14cbcSMatt Macy 		}
3288eda14cbcSMatt Macy 
3289eda14cbcSMatt Macy 		if (wholedisk) {
3290eda14cbcSMatt Macy 			const char *fullpath = path;
3291eda14cbcSMatt Macy 			char buf[MAXPATHLEN];
3292da5137abSMartin Matuska 			int error;
3293eda14cbcSMatt Macy 
3294eda14cbcSMatt Macy 			if (path[0] != '/') {
3295eda14cbcSMatt Macy 				error = zfs_resolve_shortname(path, buf,
3296eda14cbcSMatt Macy 				    sizeof (buf));
3297eda14cbcSMatt Macy 				if (error != 0)
3298eda14cbcSMatt Macy 					return (zfs_error(hdl, EZFS_NODEVICE,
32991f1e2261SMartin Matuska 					    errbuf));
3300eda14cbcSMatt Macy 
3301eda14cbcSMatt Macy 				fullpath = buf;
3302eda14cbcSMatt Macy 			}
3303eda14cbcSMatt Macy 
33041f1e2261SMartin Matuska 			error = zpool_relabel_disk(hdl, fullpath, errbuf);
3305eda14cbcSMatt Macy 			if (error != 0)
3306eda14cbcSMatt Macy 				return (error);
3307eda14cbcSMatt Macy 		}
3308eda14cbcSMatt Macy 	}
3309da5137abSMartin Matuska #endif
3310eda14cbcSMatt Macy 
3311eda14cbcSMatt Macy 	zc.zc_cookie = VDEV_STATE_ONLINE;
3312eda14cbcSMatt Macy 	zc.zc_obj = flags;
3313eda14cbcSMatt Macy 
3314eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
3315eda14cbcSMatt Macy 		if (errno == EINVAL) {
3316eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
3317eda14cbcSMatt Macy 			    "from this pool into a new one.  Use '%s' "
3318eda14cbcSMatt Macy 			    "instead"), "zpool detach");
33191f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, errbuf));
3320eda14cbcSMatt Macy 		}
33211f1e2261SMartin Matuska 		return (zpool_standard_error(hdl, errno, errbuf));
3322eda14cbcSMatt Macy 	}
3323eda14cbcSMatt Macy 
3324eda14cbcSMatt Macy 	*newstate = zc.zc_cookie;
3325eda14cbcSMatt Macy 	return (0);
3326eda14cbcSMatt Macy }
3327eda14cbcSMatt Macy 
3328eda14cbcSMatt Macy /*
3329eda14cbcSMatt Macy  * Take the specified vdev offline
3330eda14cbcSMatt Macy  */
3331eda14cbcSMatt Macy int
zpool_vdev_offline(zpool_handle_t * zhp,const char * path,boolean_t istmp)3332eda14cbcSMatt Macy zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
3333eda14cbcSMatt Macy {
3334eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
33351f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3336eda14cbcSMatt Macy 	nvlist_t *tgt;
3337eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache;
3338eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3339eda14cbcSMatt Macy 
33401f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
3341eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
3342eda14cbcSMatt Macy 
3343eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3344eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3345eda14cbcSMatt Macy 	    NULL)) == NULL)
33461f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3347eda14cbcSMatt Macy 
3348da5137abSMartin Matuska 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3349eda14cbcSMatt Macy 
3350eda14cbcSMatt Macy 	if (avail_spare)
33511f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3352eda14cbcSMatt Macy 
3353eda14cbcSMatt Macy 	zc.zc_cookie = VDEV_STATE_OFFLINE;
3354eda14cbcSMatt Macy 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3355eda14cbcSMatt Macy 
3356eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3357eda14cbcSMatt Macy 		return (0);
3358eda14cbcSMatt Macy 
3359eda14cbcSMatt Macy 	switch (errno) {
3360eda14cbcSMatt Macy 	case EBUSY:
3361eda14cbcSMatt Macy 
3362eda14cbcSMatt Macy 		/*
3363eda14cbcSMatt Macy 		 * There are no other replicas of this device.
3364eda14cbcSMatt Macy 		 */
33651f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf));
3366eda14cbcSMatt Macy 
3367eda14cbcSMatt Macy 	case EEXIST:
3368eda14cbcSMatt Macy 		/*
3369eda14cbcSMatt Macy 		 * The log device has unplayed logs
3370eda14cbcSMatt Macy 		 */
33711f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, errbuf));
3372eda14cbcSMatt Macy 
3373eda14cbcSMatt Macy 	default:
33741f1e2261SMartin Matuska 		return (zpool_standard_error(hdl, errno, errbuf));
3375eda14cbcSMatt Macy 	}
3376eda14cbcSMatt Macy }
3377eda14cbcSMatt Macy 
3378eda14cbcSMatt Macy /*
3379be181ee2SMartin Matuska  * Remove the specified vdev asynchronously from the configuration, so
3380be181ee2SMartin Matuska  * that it may come ONLINE if reinserted. This is called from zed on
3381be181ee2SMartin Matuska  * Udev remove event.
3382be181ee2SMartin Matuska  * Note: We also have a similar function zpool_vdev_remove() that
3383be181ee2SMartin Matuska  * removes the vdev from the pool.
3384be181ee2SMartin Matuska  */
3385be181ee2SMartin Matuska int
zpool_vdev_remove_wanted(zpool_handle_t * zhp,const char * path)3386be181ee2SMartin Matuska zpool_vdev_remove_wanted(zpool_handle_t *zhp, const char *path)
3387be181ee2SMartin Matuska {
3388be181ee2SMartin Matuska 	zfs_cmd_t zc = {"\0"};
3389be181ee2SMartin Matuska 	char errbuf[ERRBUFLEN];
3390be181ee2SMartin Matuska 	nvlist_t *tgt;
3391be181ee2SMartin Matuska 	boolean_t avail_spare, l2cache;
3392be181ee2SMartin Matuska 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3393be181ee2SMartin Matuska 
3394be181ee2SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
3395be181ee2SMartin Matuska 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3396be181ee2SMartin Matuska 
3397be181ee2SMartin Matuska 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3398be181ee2SMartin Matuska 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3399be181ee2SMartin Matuska 	    NULL)) == NULL)
3400be181ee2SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3401be181ee2SMartin Matuska 
3402be181ee2SMartin Matuska 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3403be181ee2SMartin Matuska 
3404be181ee2SMartin Matuska 	zc.zc_cookie = VDEV_STATE_REMOVED;
3405be181ee2SMartin Matuska 
3406be181ee2SMartin Matuska 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3407be181ee2SMartin Matuska 		return (0);
3408be181ee2SMartin Matuska 
3409be181ee2SMartin Matuska 	return (zpool_standard_error(hdl, errno, errbuf));
3410be181ee2SMartin Matuska }
3411be181ee2SMartin Matuska 
3412be181ee2SMartin Matuska /*
3413eda14cbcSMatt Macy  * Mark the given vdev faulted.
3414eda14cbcSMatt Macy  */
3415eda14cbcSMatt Macy int
zpool_vdev_fault(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3416eda14cbcSMatt Macy zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3417eda14cbcSMatt Macy {
3418eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
34191f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3420eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3421eda14cbcSMatt Macy 
34221f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
3423eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3424eda14cbcSMatt Macy 
3425eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3426eda14cbcSMatt Macy 	zc.zc_guid = guid;
3427eda14cbcSMatt Macy 	zc.zc_cookie = VDEV_STATE_FAULTED;
3428eda14cbcSMatt Macy 	zc.zc_obj = aux;
3429eda14cbcSMatt Macy 
3430eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3431eda14cbcSMatt Macy 		return (0);
3432eda14cbcSMatt Macy 
3433eda14cbcSMatt Macy 	switch (errno) {
3434eda14cbcSMatt Macy 	case EBUSY:
3435eda14cbcSMatt Macy 
3436eda14cbcSMatt Macy 		/*
3437eda14cbcSMatt Macy 		 * There are no other replicas of this device.
3438eda14cbcSMatt Macy 		 */
34391f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf));
3440eda14cbcSMatt Macy 
3441eda14cbcSMatt Macy 	default:
34421f1e2261SMartin Matuska 		return (zpool_standard_error(hdl, errno, errbuf));
3443eda14cbcSMatt Macy 	}
3444eda14cbcSMatt Macy 
3445eda14cbcSMatt Macy }
3446eda14cbcSMatt Macy 
3447eda14cbcSMatt Macy /*
3448b356da80SMartin Matuska  * Generic set vdev state function
3449eda14cbcSMatt Macy  */
3450b356da80SMartin Matuska static int
zpool_vdev_set_state(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux,vdev_state_t state)3451b356da80SMartin Matuska zpool_vdev_set_state(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux,
3452b356da80SMartin Matuska     vdev_state_t state)
3453eda14cbcSMatt Macy {
3454eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
34551f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3456eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3457eda14cbcSMatt Macy 
34581f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
3459b356da80SMartin Matuska 	    dgettext(TEXT_DOMAIN, "cannot set %s %llu"),
3460b356da80SMartin Matuska 	    zpool_state_to_name(state, aux), (u_longlong_t)guid);
3461eda14cbcSMatt Macy 
3462eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3463eda14cbcSMatt Macy 	zc.zc_guid = guid;
3464b356da80SMartin Matuska 	zc.zc_cookie = state;
3465eda14cbcSMatt Macy 	zc.zc_obj = aux;
3466eda14cbcSMatt Macy 
3467eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3468eda14cbcSMatt Macy 		return (0);
3469eda14cbcSMatt Macy 
34701f1e2261SMartin Matuska 	return (zpool_standard_error(hdl, errno, errbuf));
3471eda14cbcSMatt Macy }
3472eda14cbcSMatt Macy 
3473eda14cbcSMatt Macy /*
3474b356da80SMartin Matuska  * Mark the given vdev degraded.
3475b356da80SMartin Matuska  */
3476b356da80SMartin Matuska int
zpool_vdev_degrade(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3477b356da80SMartin Matuska zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3478b356da80SMartin Matuska {
3479b356da80SMartin Matuska 	return (zpool_vdev_set_state(zhp, guid, aux, VDEV_STATE_DEGRADED));
3480b356da80SMartin Matuska }
3481b356da80SMartin Matuska 
3482b356da80SMartin Matuska /*
3483b356da80SMartin Matuska  * Mark the given vdev as in a removed state (as if the device does not exist).
3484b356da80SMartin Matuska  *
3485b356da80SMartin Matuska  * This is different than zpool_vdev_remove() which does a removal of a device
3486b356da80SMartin Matuska  * from the pool (but the device does exist).
3487b356da80SMartin Matuska  */
3488b356da80SMartin Matuska int
zpool_vdev_set_removed_state(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3489b356da80SMartin Matuska zpool_vdev_set_removed_state(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3490b356da80SMartin Matuska {
3491b356da80SMartin Matuska 	return (zpool_vdev_set_state(zhp, guid, aux, VDEV_STATE_REMOVED));
3492b356da80SMartin Matuska }
3493b356da80SMartin Matuska 
3494b356da80SMartin Matuska /*
3495eda14cbcSMatt Macy  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3496eda14cbcSMatt Macy  * a hot spare.
3497eda14cbcSMatt Macy  */
3498eda14cbcSMatt Macy static boolean_t
is_replacing_spare(nvlist_t * search,nvlist_t * tgt,int which)3499eda14cbcSMatt Macy is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3500eda14cbcSMatt Macy {
3501eda14cbcSMatt Macy 	nvlist_t **child;
3502eda14cbcSMatt Macy 	uint_t c, children;
3503eda14cbcSMatt Macy 
3504eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3505eda14cbcSMatt Macy 	    &children) == 0) {
35062a58b312SMartin Matuska 		const char *type = fnvlist_lookup_string(search,
35072a58b312SMartin Matuska 		    ZPOOL_CONFIG_TYPE);
35087877fdebSMatt Macy 		if ((strcmp(type, VDEV_TYPE_SPARE) == 0 ||
35097877fdebSMatt Macy 		    strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) &&
3510eda14cbcSMatt Macy 		    children == 2 && child[which] == tgt)
3511eda14cbcSMatt Macy 			return (B_TRUE);
3512eda14cbcSMatt Macy 
3513eda14cbcSMatt Macy 		for (c = 0; c < children; c++)
3514eda14cbcSMatt Macy 			if (is_replacing_spare(child[c], tgt, which))
3515eda14cbcSMatt Macy 				return (B_TRUE);
3516eda14cbcSMatt Macy 	}
3517eda14cbcSMatt Macy 
3518eda14cbcSMatt Macy 	return (B_FALSE);
3519eda14cbcSMatt Macy }
3520eda14cbcSMatt Macy 
3521eda14cbcSMatt Macy /*
3522eda14cbcSMatt Macy  * Attach new_disk (fully described by nvroot) to old_disk.
3523eda14cbcSMatt Macy  * If 'replacing' is specified, the new disk will replace the old one.
3524eda14cbcSMatt Macy  */
3525eda14cbcSMatt Macy int
zpool_vdev_attach(zpool_handle_t * zhp,const char * old_disk,const char * new_disk,nvlist_t * nvroot,int replacing,boolean_t rebuild)3526eda14cbcSMatt Macy zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3527eda14cbcSMatt Macy     const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
3528eda14cbcSMatt Macy {
3529eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
35301f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3531eda14cbcSMatt Macy 	int ret;
3532eda14cbcSMatt Macy 	nvlist_t *tgt;
3533eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache, islog;
3534eda14cbcSMatt Macy 	uint64_t val;
3535eda14cbcSMatt Macy 	char *newname;
3536e716630dSMartin Matuska 	const char *type;
3537eda14cbcSMatt Macy 	nvlist_t **child;
3538eda14cbcSMatt Macy 	uint_t children;
3539eda14cbcSMatt Macy 	nvlist_t *config_root;
3540eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3541eda14cbcSMatt Macy 
3542eda14cbcSMatt Macy 	if (replacing)
35431f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3544eda14cbcSMatt Macy 		    "cannot replace %s with %s"), old_disk, new_disk);
3545eda14cbcSMatt Macy 	else
35461f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3547eda14cbcSMatt Macy 		    "cannot attach %s to %s"), new_disk, old_disk);
3548eda14cbcSMatt Macy 
3549eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3550eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3551eda14cbcSMatt Macy 	    &islog)) == NULL)
35521f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3553eda14cbcSMatt Macy 
3554eda14cbcSMatt Macy 	if (avail_spare)
35551f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3556eda14cbcSMatt Macy 
3557eda14cbcSMatt Macy 	if (l2cache)
35581f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf));
3559eda14cbcSMatt Macy 
3560da5137abSMartin Matuska 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3561eda14cbcSMatt Macy 	zc.zc_cookie = replacing;
3562eda14cbcSMatt Macy 	zc.zc_simple = rebuild;
3563eda14cbcSMatt Macy 
3564eda14cbcSMatt Macy 	if (rebuild &&
3565eda14cbcSMatt Macy 	    zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3566eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3567eda14cbcSMatt Macy 		    "the loaded zfs module doesn't support device rebuilds"));
35681f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3569eda14cbcSMatt Macy 	}
3570eda14cbcSMatt Macy 
3571e716630dSMartin Matuska 	type = fnvlist_lookup_string(tgt, ZPOOL_CONFIG_TYPE);
3572e716630dSMartin Matuska 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 &&
3573e716630dSMartin Matuska 	    zfeature_lookup_guid("org.openzfs:raidz_expansion", NULL) != 0) {
3574e716630dSMartin Matuska 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3575e716630dSMartin Matuska 		    "the loaded zfs module doesn't support raidz expansion"));
3576e716630dSMartin Matuska 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3577e716630dSMartin Matuska 	}
3578e716630dSMartin Matuska 
3579eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3580eda14cbcSMatt Macy 	    &child, &children) != 0 || children != 1) {
3581eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3582eda14cbcSMatt Macy 		    "new device must be a single disk"));
35831f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_INVALCONFIG, errbuf));
3584eda14cbcSMatt Macy 	}
3585eda14cbcSMatt Macy 
3586da5137abSMartin Matuska 	config_root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3587da5137abSMartin Matuska 	    ZPOOL_CONFIG_VDEV_TREE);
3588eda14cbcSMatt Macy 
3589eda14cbcSMatt Macy 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3590eda14cbcSMatt Macy 		return (-1);
3591eda14cbcSMatt Macy 
3592eda14cbcSMatt Macy 	/*
3593eda14cbcSMatt Macy 	 * If the target is a hot spare that has been swapped in, we can only
3594eda14cbcSMatt Macy 	 * replace it with another hot spare.
3595eda14cbcSMatt Macy 	 */
3596eda14cbcSMatt Macy 	if (replacing &&
3597eda14cbcSMatt Macy 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3598eda14cbcSMatt Macy 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3599eda14cbcSMatt Macy 	    NULL) == NULL || !avail_spare) &&
3600eda14cbcSMatt Macy 	    is_replacing_spare(config_root, tgt, 1)) {
3601eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3602eda14cbcSMatt Macy 		    "can only be replaced by another hot spare"));
3603eda14cbcSMatt Macy 		free(newname);
36041f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_BADTARGET, errbuf));
3605eda14cbcSMatt Macy 	}
3606eda14cbcSMatt Macy 
3607eda14cbcSMatt Macy 	free(newname);
3608eda14cbcSMatt Macy 
3609716fd348SMartin Matuska 	zcmd_write_conf_nvlist(hdl, &zc, nvroot);
3610eda14cbcSMatt Macy 
3611eda14cbcSMatt Macy 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3612eda14cbcSMatt Macy 
3613eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
3614eda14cbcSMatt Macy 
3615eda14cbcSMatt Macy 	if (ret == 0)
3616eda14cbcSMatt Macy 		return (0);
3617eda14cbcSMatt Macy 
3618eda14cbcSMatt Macy 	switch (errno) {
3619eda14cbcSMatt Macy 	case ENOTSUP:
3620eda14cbcSMatt Macy 		/*
3621eda14cbcSMatt Macy 		 * Can't attach to or replace this type of vdev.
3622eda14cbcSMatt Macy 		 */
3623eda14cbcSMatt Macy 		if (replacing) {
3624eda14cbcSMatt Macy 			uint64_t version = zpool_get_prop_int(zhp,
3625eda14cbcSMatt Macy 			    ZPOOL_PROP_VERSION, NULL);
3626eda14cbcSMatt Macy 
3627eda14cbcSMatt Macy 			if (islog) {
3628eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3629eda14cbcSMatt Macy 				    "cannot replace a log with a spare"));
3630eda14cbcSMatt Macy 			} else if (rebuild) {
3631eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36327877fdebSMatt Macy 				    "only mirror and dRAID vdevs support "
36337877fdebSMatt Macy 				    "sequential reconstruction"));
36347877fdebSMatt Macy 			} else if (zpool_is_draid_spare(new_disk)) {
36357877fdebSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36367877fdebSMatt Macy 				    "dRAID spares can only replace child "
36377877fdebSMatt Macy 				    "devices in their parent's dRAID vdev"));
3638eda14cbcSMatt Macy 			} else if (version >= SPA_VERSION_MULTI_REPLACE) {
3639eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3640eda14cbcSMatt Macy 				    "already in replacing/spare config; wait "
3641eda14cbcSMatt Macy 				    "for completion or use 'zpool detach'"));
3642eda14cbcSMatt Macy 			} else {
3643eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3644eda14cbcSMatt Macy 				    "cannot replace a replacing device"));
3645eda14cbcSMatt Macy 			}
3646e716630dSMartin Matuska 		} else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
3647e716630dSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3648e716630dSMartin Matuska 			    "raidz_expansion feature must be enabled "
3649e716630dSMartin Matuska 			    "in order to attach a device to raidz"));
3650eda14cbcSMatt Macy 		} else {
365181b22a98SMartin Matuska 			char status[64] = {0};
365281b22a98SMartin Matuska 			zpool_prop_get_feature(zhp,
365381b22a98SMartin Matuska 			    "feature@device_rebuild", status, 63);
365481b22a98SMartin Matuska 			if (rebuild &&
365581b22a98SMartin Matuska 			    strncmp(status, ZFS_FEATURE_DISABLED, 64) == 0) {
365681b22a98SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
365781b22a98SMartin Matuska 				    "device_rebuild feature must be enabled "
365881b22a98SMartin Matuska 				    "in order to use sequential "
365981b22a98SMartin Matuska 				    "reconstruction"));
366081b22a98SMartin Matuska 			} else {
3661eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3662eda14cbcSMatt Macy 				    "can only attach to mirrors and top-level "
3663eda14cbcSMatt Macy 				    "disks"));
3664eda14cbcSMatt Macy 			}
366581b22a98SMartin Matuska 		}
36661f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
3667eda14cbcSMatt Macy 		break;
3668eda14cbcSMatt Macy 
3669eda14cbcSMatt Macy 	case EINVAL:
3670eda14cbcSMatt Macy 		/*
3671eda14cbcSMatt Macy 		 * The new device must be a single disk.
3672eda14cbcSMatt Macy 		 */
3673eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3674eda14cbcSMatt Macy 		    "new device must be a single disk"));
36751f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3676eda14cbcSMatt Macy 		break;
3677eda14cbcSMatt Macy 
3678eda14cbcSMatt Macy 	case EBUSY:
3679e716630dSMartin Matuska 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
3680eda14cbcSMatt Macy 		    new_disk);
36811f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3682eda14cbcSMatt Macy 		break;
3683eda14cbcSMatt Macy 
3684eda14cbcSMatt Macy 	case EOVERFLOW:
3685eda14cbcSMatt Macy 		/*
3686eda14cbcSMatt Macy 		 * The new device is too small.
3687eda14cbcSMatt Macy 		 */
3688eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3689eda14cbcSMatt Macy 		    "device is too small"));
36901f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3691eda14cbcSMatt Macy 		break;
3692eda14cbcSMatt Macy 
3693eda14cbcSMatt Macy 	case EDOM:
3694eda14cbcSMatt Macy 		/*
3695eda14cbcSMatt Macy 		 * The new device has a different optimal sector size.
3696eda14cbcSMatt Macy 		 */
3697eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3698eda14cbcSMatt Macy 		    "new device has a different optimal sector size; use the "
3699eda14cbcSMatt Macy 		    "option '-o ashift=N' to override the optimal size"));
37001f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3701eda14cbcSMatt Macy 		break;
3702eda14cbcSMatt Macy 
3703eda14cbcSMatt Macy 	case ENAMETOOLONG:
3704eda14cbcSMatt Macy 		/*
3705eda14cbcSMatt Macy 		 * The resulting top-level vdev spec won't fit in the label.
3706eda14cbcSMatt Macy 		 */
37071f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3708eda14cbcSMatt Macy 		break;
3709eda14cbcSMatt Macy 
3710e716630dSMartin Matuska 	case ENXIO:
3711e716630dSMartin Matuska 		/*
3712e716630dSMartin Matuska 		 * The existing raidz vdev has offline children
3713e716630dSMartin Matuska 		 */
3714e716630dSMartin Matuska 		if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
3715e716630dSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3716e716630dSMartin Matuska 			    "raidz vdev has devices that are are offline or "
3717e716630dSMartin Matuska 			    "being replaced"));
3718e716630dSMartin Matuska 			(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3719e716630dSMartin Matuska 			break;
3720e716630dSMartin Matuska 		} else {
3721e716630dSMartin Matuska 			(void) zpool_standard_error(hdl, errno, errbuf);
3722e716630dSMartin Matuska 		}
3723e716630dSMartin Matuska 		break;
3724e716630dSMartin Matuska 
3725e716630dSMartin Matuska 	case EADDRINUSE:
3726e716630dSMartin Matuska 		/*
3727e716630dSMartin Matuska 		 * The boot reserved area is already being used (FreeBSD)
3728e716630dSMartin Matuska 		 */
3729e716630dSMartin Matuska 		if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
3730e716630dSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3731e716630dSMartin Matuska 			    "the reserved boot area needed for the expansion "
3732e716630dSMartin Matuska 			    "is already being used by a boot loader"));
3733e716630dSMartin Matuska 			(void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3734e716630dSMartin Matuska 		} else {
3735e716630dSMartin Matuska 			(void) zpool_standard_error(hdl, errno, errbuf);
3736e716630dSMartin Matuska 		}
3737e716630dSMartin Matuska 		break;
3738e2df9bb4SMartin Matuska 
3739e2df9bb4SMartin Matuska 	case ZFS_ERR_ASHIFT_MISMATCH:
3740e2df9bb4SMartin Matuska 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3741e2df9bb4SMartin Matuska 		    "The new device cannot have a higher alignment requirement "
3742e2df9bb4SMartin Matuska 		    "than the top-level vdev."));
3743e2df9bb4SMartin Matuska 		(void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
3744e2df9bb4SMartin Matuska 		break;
3745eda14cbcSMatt Macy 	default:
37461f1e2261SMartin Matuska 		(void) zpool_standard_error(hdl, errno, errbuf);
3747eda14cbcSMatt Macy 	}
3748eda14cbcSMatt Macy 
3749eda14cbcSMatt Macy 	return (-1);
3750eda14cbcSMatt Macy }
3751eda14cbcSMatt Macy 
3752eda14cbcSMatt Macy /*
3753eda14cbcSMatt Macy  * Detach the specified device.
3754eda14cbcSMatt Macy  */
3755eda14cbcSMatt Macy int
zpool_vdev_detach(zpool_handle_t * zhp,const char * path)3756eda14cbcSMatt Macy zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3757eda14cbcSMatt Macy {
3758eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
37591f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3760eda14cbcSMatt Macy 	nvlist_t *tgt;
3761eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache;
3762eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3763eda14cbcSMatt Macy 
37641f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
3765eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3766eda14cbcSMatt Macy 
3767eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3768eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3769eda14cbcSMatt Macy 	    NULL)) == NULL)
37701f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3771eda14cbcSMatt Macy 
3772eda14cbcSMatt Macy 	if (avail_spare)
37731f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3774eda14cbcSMatt Macy 
3775eda14cbcSMatt Macy 	if (l2cache)
37761f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf));
3777eda14cbcSMatt Macy 
3778da5137abSMartin Matuska 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3779eda14cbcSMatt Macy 
3780eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3781eda14cbcSMatt Macy 		return (0);
3782eda14cbcSMatt Macy 
3783eda14cbcSMatt Macy 	switch (errno) {
3784eda14cbcSMatt Macy 
3785eda14cbcSMatt Macy 	case ENOTSUP:
3786eda14cbcSMatt Macy 		/*
3787eda14cbcSMatt Macy 		 * Can't detach from this type of vdev.
3788eda14cbcSMatt Macy 		 */
3789eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3790eda14cbcSMatt Macy 		    "applicable to mirror and replacing vdevs"));
37911f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
3792eda14cbcSMatt Macy 		break;
3793eda14cbcSMatt Macy 
3794eda14cbcSMatt Macy 	case EBUSY:
3795eda14cbcSMatt Macy 		/*
3796eda14cbcSMatt Macy 		 * There are no other replicas of this device.
3797eda14cbcSMatt Macy 		 */
37981f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_NOREPLICAS, errbuf);
3799eda14cbcSMatt Macy 		break;
3800eda14cbcSMatt Macy 
3801eda14cbcSMatt Macy 	default:
38021f1e2261SMartin Matuska 		(void) zpool_standard_error(hdl, errno, errbuf);
3803eda14cbcSMatt Macy 	}
3804eda14cbcSMatt Macy 
3805eda14cbcSMatt Macy 	return (-1);
3806eda14cbcSMatt Macy }
3807eda14cbcSMatt Macy 
3808eda14cbcSMatt Macy /*
3809eda14cbcSMatt Macy  * Find a mirror vdev in the source nvlist.
3810eda14cbcSMatt Macy  *
3811eda14cbcSMatt Macy  * The mchild array contains a list of disks in one of the top-level mirrors
3812eda14cbcSMatt Macy  * of the source pool.  The schild array contains a list of disks that the
3813eda14cbcSMatt Macy  * user specified on the command line.  We loop over the mchild array to
3814eda14cbcSMatt Macy  * see if any entry in the schild array matches.
3815eda14cbcSMatt Macy  *
3816eda14cbcSMatt Macy  * If a disk in the mchild array is found in the schild array, we return
3817eda14cbcSMatt Macy  * the index of that entry.  Otherwise we return -1.
3818eda14cbcSMatt Macy  */
3819eda14cbcSMatt Macy static int
find_vdev_entry(zpool_handle_t * zhp,nvlist_t ** mchild,uint_t mchildren,nvlist_t ** schild,uint_t schildren)3820eda14cbcSMatt Macy find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3821eda14cbcSMatt Macy     nvlist_t **schild, uint_t schildren)
3822eda14cbcSMatt Macy {
3823eda14cbcSMatt Macy 	uint_t mc;
3824eda14cbcSMatt Macy 
3825eda14cbcSMatt Macy 	for (mc = 0; mc < mchildren; mc++) {
3826eda14cbcSMatt Macy 		uint_t sc;
3827eda14cbcSMatt Macy 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3828eda14cbcSMatt Macy 		    mchild[mc], 0);
3829eda14cbcSMatt Macy 
3830eda14cbcSMatt Macy 		for (sc = 0; sc < schildren; sc++) {
3831eda14cbcSMatt Macy 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3832eda14cbcSMatt Macy 			    schild[sc], 0);
3833eda14cbcSMatt Macy 			boolean_t result = (strcmp(mpath, spath) == 0);
3834eda14cbcSMatt Macy 
3835eda14cbcSMatt Macy 			free(spath);
3836eda14cbcSMatt Macy 			if (result) {
3837eda14cbcSMatt Macy 				free(mpath);
3838eda14cbcSMatt Macy 				return (mc);
3839eda14cbcSMatt Macy 			}
3840eda14cbcSMatt Macy 		}
3841eda14cbcSMatt Macy 
3842eda14cbcSMatt Macy 		free(mpath);
3843eda14cbcSMatt Macy 	}
3844eda14cbcSMatt Macy 
3845eda14cbcSMatt Macy 	return (-1);
3846eda14cbcSMatt Macy }
3847eda14cbcSMatt Macy 
3848eda14cbcSMatt Macy /*
3849eda14cbcSMatt Macy  * Split a mirror pool.  If newroot points to null, then a new nvlist
3850eda14cbcSMatt Macy  * is generated and it is the responsibility of the caller to free it.
3851eda14cbcSMatt Macy  */
3852eda14cbcSMatt Macy int
zpool_vdev_split(zpool_handle_t * zhp,char * newname,nvlist_t ** newroot,nvlist_t * props,splitflags_t flags)3853eda14cbcSMatt Macy zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3854eda14cbcSMatt Macy     nvlist_t *props, splitflags_t flags)
3855eda14cbcSMatt Macy {
3856eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
38572a58b312SMartin Matuska 	char errbuf[ERRBUFLEN];
38582a58b312SMartin Matuska 	const char *bias;
3859eda14cbcSMatt Macy 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3860eda14cbcSMatt Macy 	nvlist_t **varray = NULL, *zc_props = NULL;
3861eda14cbcSMatt Macy 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3862eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3863eda14cbcSMatt Macy 	uint64_t vers, readonly = B_FALSE;
3864eda14cbcSMatt Macy 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3865eda14cbcSMatt Macy 	int retval = 0;
3866eda14cbcSMatt Macy 
38671f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
3868eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3869eda14cbcSMatt Macy 
3870eda14cbcSMatt Macy 	if (!zpool_name_valid(hdl, B_FALSE, newname))
38711f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3872eda14cbcSMatt Macy 
3873eda14cbcSMatt Macy 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3874eda14cbcSMatt Macy 		(void) fprintf(stderr, gettext("Internal error: unable to "
3875eda14cbcSMatt Macy 		    "retrieve pool configuration\n"));
3876eda14cbcSMatt Macy 		return (-1);
3877eda14cbcSMatt Macy 	}
3878eda14cbcSMatt Macy 
3879da5137abSMartin Matuska 	tree = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
3880da5137abSMartin Matuska 	vers = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
3881eda14cbcSMatt Macy 
3882eda14cbcSMatt Macy 	if (props) {
3883eda14cbcSMatt Macy 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3884eda14cbcSMatt Macy 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
38851f1e2261SMartin Matuska 		    props, vers, flags, errbuf)) == NULL)
3886eda14cbcSMatt Macy 			return (-1);
3887eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(zc_props,
3888eda14cbcSMatt Macy 		    zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3889eda14cbcSMatt Macy 		if (readonly) {
3890eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3891eda14cbcSMatt Macy 			    "property %s can only be set at import time"),
3892eda14cbcSMatt Macy 			    zpool_prop_to_name(ZPOOL_PROP_READONLY));
3893eda14cbcSMatt Macy 			return (-1);
3894eda14cbcSMatt Macy 		}
3895eda14cbcSMatt Macy 	}
3896eda14cbcSMatt Macy 
3897eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3898eda14cbcSMatt Macy 	    &children) != 0) {
3899eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3900eda14cbcSMatt Macy 		    "Source pool is missing vdev tree"));
3901eda14cbcSMatt Macy 		nvlist_free(zc_props);
3902eda14cbcSMatt Macy 		return (-1);
3903eda14cbcSMatt Macy 	}
3904eda14cbcSMatt Macy 
3905eda14cbcSMatt Macy 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3906eda14cbcSMatt Macy 	vcount = 0;
3907eda14cbcSMatt Macy 
3908eda14cbcSMatt Macy 	if (*newroot == NULL ||
3909eda14cbcSMatt Macy 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3910eda14cbcSMatt Macy 	    &newchild, &newchildren) != 0)
3911eda14cbcSMatt Macy 		newchildren = 0;
3912eda14cbcSMatt Macy 
3913eda14cbcSMatt Macy 	for (c = 0; c < children; c++) {
3914eda14cbcSMatt Macy 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
39157877fdebSMatt Macy 		boolean_t is_special = B_FALSE, is_dedup = B_FALSE;
39162a58b312SMartin Matuska 		const char *type;
3917eda14cbcSMatt Macy 		nvlist_t **mchild, *vdev;
3918eda14cbcSMatt Macy 		uint_t mchildren;
3919eda14cbcSMatt Macy 		int entry;
3920eda14cbcSMatt Macy 
3921eda14cbcSMatt Macy 		/*
3922eda14cbcSMatt Macy 		 * Unlike cache & spares, slogs are stored in the
3923eda14cbcSMatt Macy 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
3924eda14cbcSMatt Macy 		 */
3925eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3926eda14cbcSMatt Macy 		    &is_log);
3927eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3928eda14cbcSMatt Macy 		    &is_hole);
3929eda14cbcSMatt Macy 		if (is_log || is_hole) {
3930eda14cbcSMatt Macy 			/*
3931eda14cbcSMatt Macy 			 * Create a hole vdev and put it in the config.
3932eda14cbcSMatt Macy 			 */
3933eda14cbcSMatt Macy 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3934eda14cbcSMatt Macy 				goto out;
3935eda14cbcSMatt Macy 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3936eda14cbcSMatt Macy 			    VDEV_TYPE_HOLE) != 0)
3937eda14cbcSMatt Macy 				goto out;
3938eda14cbcSMatt Macy 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3939eda14cbcSMatt Macy 			    1) != 0)
3940eda14cbcSMatt Macy 				goto out;
3941eda14cbcSMatt Macy 			if (lastlog == 0)
3942eda14cbcSMatt Macy 				lastlog = vcount;
3943eda14cbcSMatt Macy 			varray[vcount++] = vdev;
3944eda14cbcSMatt Macy 			continue;
3945eda14cbcSMatt Macy 		}
3946eda14cbcSMatt Macy 		lastlog = 0;
3947da5137abSMartin Matuska 		type = fnvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE);
3948eda14cbcSMatt Macy 
3949eda14cbcSMatt Macy 		if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
3950eda14cbcSMatt Macy 			vdev = child[c];
3951eda14cbcSMatt Macy 			if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3952eda14cbcSMatt Macy 				goto out;
3953eda14cbcSMatt Macy 			continue;
3954eda14cbcSMatt Macy 		} else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3955eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3956eda14cbcSMatt Macy 			    "Source pool must be composed only of mirrors\n"));
39571f1e2261SMartin Matuska 			retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3958eda14cbcSMatt Macy 			goto out;
3959eda14cbcSMatt Macy 		}
3960eda14cbcSMatt Macy 
39617877fdebSMatt Macy 		if (nvlist_lookup_string(child[c],
39627877fdebSMatt Macy 		    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) {
39637877fdebSMatt Macy 			if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
39647877fdebSMatt Macy 				is_special = B_TRUE;
39657877fdebSMatt Macy 			else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
39667877fdebSMatt Macy 				is_dedup = B_TRUE;
39677877fdebSMatt Macy 		}
3968eda14cbcSMatt Macy 		verify(nvlist_lookup_nvlist_array(child[c],
3969eda14cbcSMatt Macy 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3970eda14cbcSMatt Macy 
3971eda14cbcSMatt Macy 		/* find or add an entry for this top-level vdev */
3972eda14cbcSMatt Macy 		if (newchildren > 0 &&
3973eda14cbcSMatt Macy 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
3974eda14cbcSMatt Macy 		    newchild, newchildren)) >= 0) {
3975eda14cbcSMatt Macy 			/* We found a disk that the user specified. */
3976eda14cbcSMatt Macy 			vdev = mchild[entry];
3977eda14cbcSMatt Macy 			++found;
3978eda14cbcSMatt Macy 		} else {
3979eda14cbcSMatt Macy 			/* User didn't specify a disk for this vdev. */
3980eda14cbcSMatt Macy 			vdev = mchild[mchildren - 1];
3981eda14cbcSMatt Macy 		}
3982eda14cbcSMatt Macy 
3983eda14cbcSMatt Macy 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3984eda14cbcSMatt Macy 			goto out;
39857877fdebSMatt Macy 
39867877fdebSMatt Macy 		if (flags.dryrun != 0) {
39877877fdebSMatt Macy 			if (is_dedup == B_TRUE) {
39887877fdebSMatt Macy 				if (nvlist_add_string(varray[vcount - 1],
39897877fdebSMatt Macy 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
39907877fdebSMatt Macy 				    VDEV_ALLOC_BIAS_DEDUP) != 0)
39917877fdebSMatt Macy 					goto out;
39927877fdebSMatt Macy 			} else if (is_special == B_TRUE) {
39937877fdebSMatt Macy 				if (nvlist_add_string(varray[vcount - 1],
39947877fdebSMatt Macy 				    ZPOOL_CONFIG_ALLOCATION_BIAS,
39957877fdebSMatt Macy 				    VDEV_ALLOC_BIAS_SPECIAL) != 0)
39967877fdebSMatt Macy 					goto out;
39977877fdebSMatt Macy 			}
39987877fdebSMatt Macy 		}
3999eda14cbcSMatt Macy 	}
4000eda14cbcSMatt Macy 
4001eda14cbcSMatt Macy 	/* did we find every disk the user specified? */
4002eda14cbcSMatt Macy 	if (found != newchildren) {
4003eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
4004eda14cbcSMatt Macy 		    "include at most one disk from each mirror"));
40051f1e2261SMartin Matuska 		retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4006eda14cbcSMatt Macy 		goto out;
4007eda14cbcSMatt Macy 	}
4008eda14cbcSMatt Macy 
4009eda14cbcSMatt Macy 	/* Prepare the nvlist for populating. */
4010eda14cbcSMatt Macy 	if (*newroot == NULL) {
4011eda14cbcSMatt Macy 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
4012eda14cbcSMatt Macy 			goto out;
4013eda14cbcSMatt Macy 		freelist = B_TRUE;
4014eda14cbcSMatt Macy 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
4015eda14cbcSMatt Macy 		    VDEV_TYPE_ROOT) != 0)
4016eda14cbcSMatt Macy 			goto out;
4017eda14cbcSMatt Macy 	} else {
4018eda14cbcSMatt Macy 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
4019eda14cbcSMatt Macy 	}
4020eda14cbcSMatt Macy 
4021eda14cbcSMatt Macy 	/* Add all the children we found */
4022681ce946SMartin Matuska 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
4023681ce946SMartin Matuska 	    (const nvlist_t **)varray, lastlog == 0 ? vcount : lastlog) != 0)
4024eda14cbcSMatt Macy 		goto out;
4025eda14cbcSMatt Macy 
4026eda14cbcSMatt Macy 	/*
4027eda14cbcSMatt Macy 	 * If we're just doing a dry run, exit now with success.
4028eda14cbcSMatt Macy 	 */
4029eda14cbcSMatt Macy 	if (flags.dryrun) {
4030eda14cbcSMatt Macy 		memory_err = B_FALSE;
4031eda14cbcSMatt Macy 		freelist = B_FALSE;
4032eda14cbcSMatt Macy 		goto out;
4033eda14cbcSMatt Macy 	}
4034eda14cbcSMatt Macy 
4035eda14cbcSMatt Macy 	/* now build up the config list & call the ioctl */
4036eda14cbcSMatt Macy 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
4037eda14cbcSMatt Macy 		goto out;
4038eda14cbcSMatt Macy 
4039eda14cbcSMatt Macy 	if (nvlist_add_nvlist(newconfig,
4040eda14cbcSMatt Macy 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
4041eda14cbcSMatt Macy 	    nvlist_add_string(newconfig,
4042eda14cbcSMatt Macy 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
4043eda14cbcSMatt Macy 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
4044eda14cbcSMatt Macy 		goto out;
4045eda14cbcSMatt Macy 
4046eda14cbcSMatt Macy 	/*
4047eda14cbcSMatt Macy 	 * The new pool is automatically part of the namespace unless we
4048eda14cbcSMatt Macy 	 * explicitly export it.
4049eda14cbcSMatt Macy 	 */
4050eda14cbcSMatt Macy 	if (!flags.import)
4051eda14cbcSMatt Macy 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
4052eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4053eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
4054716fd348SMartin Matuska 	zcmd_write_conf_nvlist(hdl, &zc, newconfig);
4055716fd348SMartin Matuska 	if (zc_props != NULL)
4056716fd348SMartin Matuska 		zcmd_write_src_nvlist(hdl, &zc, zc_props);
4057eda14cbcSMatt Macy 
4058eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
40591f1e2261SMartin Matuska 		retval = zpool_standard_error(hdl, errno, errbuf);
4060eda14cbcSMatt Macy 		goto out;
4061eda14cbcSMatt Macy 	}
4062eda14cbcSMatt Macy 
4063eda14cbcSMatt Macy 	freelist = B_FALSE;
4064eda14cbcSMatt Macy 	memory_err = B_FALSE;
4065eda14cbcSMatt Macy 
4066eda14cbcSMatt Macy out:
4067eda14cbcSMatt Macy 	if (varray != NULL) {
4068eda14cbcSMatt Macy 		int v;
4069eda14cbcSMatt Macy 
4070eda14cbcSMatt Macy 		for (v = 0; v < vcount; v++)
4071eda14cbcSMatt Macy 			nvlist_free(varray[v]);
4072eda14cbcSMatt Macy 		free(varray);
4073eda14cbcSMatt Macy 	}
4074eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
4075eda14cbcSMatt Macy 	nvlist_free(zc_props);
4076eda14cbcSMatt Macy 	nvlist_free(newconfig);
4077eda14cbcSMatt Macy 	if (freelist) {
4078eda14cbcSMatt Macy 		nvlist_free(*newroot);
4079eda14cbcSMatt Macy 		*newroot = NULL;
4080eda14cbcSMatt Macy 	}
4081eda14cbcSMatt Macy 
4082eda14cbcSMatt Macy 	if (retval != 0)
4083eda14cbcSMatt Macy 		return (retval);
4084eda14cbcSMatt Macy 
4085eda14cbcSMatt Macy 	if (memory_err)
4086eda14cbcSMatt Macy 		return (no_memory(hdl));
4087eda14cbcSMatt Macy 
4088eda14cbcSMatt Macy 	return (0);
4089eda14cbcSMatt Macy }
4090eda14cbcSMatt Macy 
4091eda14cbcSMatt Macy /*
4092eda14cbcSMatt Macy  * Remove the given device.
4093eda14cbcSMatt Macy  */
4094eda14cbcSMatt Macy int
zpool_vdev_remove(zpool_handle_t * zhp,const char * path)4095eda14cbcSMatt Macy zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
4096eda14cbcSMatt Macy {
4097eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
40981f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4099eda14cbcSMatt Macy 	nvlist_t *tgt;
4100eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache, islog;
4101eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4102eda14cbcSMatt Macy 	uint64_t version;
4103eda14cbcSMatt Macy 
41041f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
4105eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
4106eda14cbcSMatt Macy 
41077877fdebSMatt Macy 	if (zpool_is_draid_spare(path)) {
41087877fdebSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41097877fdebSMatt Macy 		    "dRAID spares cannot be removed"));
41101f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
41117877fdebSMatt Macy 	}
41127877fdebSMatt Macy 
4113eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4114eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
4115eda14cbcSMatt Macy 	    &islog)) == NULL)
41161f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4117eda14cbcSMatt Macy 
4118eda14cbcSMatt Macy 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4119eda14cbcSMatt Macy 	if (islog && version < SPA_VERSION_HOLES) {
4120eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4121eda14cbcSMatt Macy 		    "pool must be upgraded to support log removal"));
41221f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4123eda14cbcSMatt Macy 	}
4124eda14cbcSMatt Macy 
4125eda14cbcSMatt Macy 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
4126eda14cbcSMatt Macy 
4127eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
4128eda14cbcSMatt Macy 		return (0);
4129eda14cbcSMatt Macy 
4130eda14cbcSMatt Macy 	switch (errno) {
4131eda14cbcSMatt Macy 
4132315ee00fSMartin Matuska 	case EALREADY:
4133315ee00fSMartin Matuska 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4134315ee00fSMartin Matuska 		    "removal for this vdev is already in progress."));
4135315ee00fSMartin Matuska 		(void) zfs_error(hdl, EZFS_BUSY, errbuf);
4136315ee00fSMartin Matuska 		break;
4137315ee00fSMartin Matuska 
4138eda14cbcSMatt Macy 	case EINVAL:
4139eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4140eda14cbcSMatt Macy 		    "invalid config; all top-level vdevs must "
4141eda14cbcSMatt Macy 		    "have the same sector size and not be raidz."));
41421f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4143eda14cbcSMatt Macy 		break;
4144eda14cbcSMatt Macy 
4145eda14cbcSMatt Macy 	case EBUSY:
4146eda14cbcSMatt Macy 		if (islog) {
4147eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4148eda14cbcSMatt Macy 			    "Mount encrypted datasets to replay logs."));
4149eda14cbcSMatt Macy 		} else {
4150eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4151eda14cbcSMatt Macy 			    "Pool busy; removal may already be in progress"));
4152eda14cbcSMatt Macy 		}
41531f1e2261SMartin Matuska 		(void) zfs_error(hdl, EZFS_BUSY, errbuf);
4154eda14cbcSMatt Macy 		break;
4155eda14cbcSMatt Macy 
4156eda14cbcSMatt Macy 	case EACCES:
4157eda14cbcSMatt Macy 		if (islog) {
4158eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4159eda14cbcSMatt Macy 			    "Mount encrypted datasets to replay logs."));
41601f1e2261SMartin Matuska 			(void) zfs_error(hdl, EZFS_BUSY, errbuf);
4161eda14cbcSMatt Macy 		} else {
41621f1e2261SMartin Matuska 			(void) zpool_standard_error(hdl, errno, errbuf);
4163eda14cbcSMatt Macy 		}
4164eda14cbcSMatt Macy 		break;
4165eda14cbcSMatt Macy 
4166eda14cbcSMatt Macy 	default:
41671f1e2261SMartin Matuska 		(void) zpool_standard_error(hdl, errno, errbuf);
4168eda14cbcSMatt Macy 	}
4169eda14cbcSMatt Macy 	return (-1);
4170eda14cbcSMatt Macy }
4171eda14cbcSMatt Macy 
4172eda14cbcSMatt Macy int
zpool_vdev_remove_cancel(zpool_handle_t * zhp)4173eda14cbcSMatt Macy zpool_vdev_remove_cancel(zpool_handle_t *zhp)
4174eda14cbcSMatt Macy {
4175da5137abSMartin Matuska 	zfs_cmd_t zc = {{0}};
41761f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4177eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4178eda14cbcSMatt Macy 
41791f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
4180eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot cancel removal"));
4181eda14cbcSMatt Macy 
4182eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4183eda14cbcSMatt Macy 	zc.zc_cookie = 1;
4184eda14cbcSMatt Macy 
4185eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
4186eda14cbcSMatt Macy 		return (0);
4187eda14cbcSMatt Macy 
41881f1e2261SMartin Matuska 	return (zpool_standard_error(hdl, errno, errbuf));
4189eda14cbcSMatt Macy }
4190eda14cbcSMatt Macy 
4191eda14cbcSMatt Macy int
zpool_vdev_indirect_size(zpool_handle_t * zhp,const char * path,uint64_t * sizep)4192eda14cbcSMatt Macy zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
4193eda14cbcSMatt Macy     uint64_t *sizep)
4194eda14cbcSMatt Macy {
41951f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4196eda14cbcSMatt Macy 	nvlist_t *tgt;
4197eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache, islog;
4198eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4199eda14cbcSMatt Macy 
42001f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
4201eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
4202eda14cbcSMatt Macy 	    path);
4203eda14cbcSMatt Macy 
4204eda14cbcSMatt Macy 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
4205eda14cbcSMatt Macy 	    &islog)) == NULL)
42061f1e2261SMartin Matuska 		return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4207eda14cbcSMatt Macy 
4208eda14cbcSMatt Macy 	if (avail_spare || l2cache || islog) {
4209eda14cbcSMatt Macy 		*sizep = 0;
4210eda14cbcSMatt Macy 		return (0);
4211eda14cbcSMatt Macy 	}
4212eda14cbcSMatt Macy 
4213eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
4214eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4215eda14cbcSMatt Macy 		    "indirect size not available"));
42161f1e2261SMartin Matuska 		return (zfs_error(hdl, EINVAL, errbuf));
4217eda14cbcSMatt Macy 	}
4218eda14cbcSMatt Macy 	return (0);
4219eda14cbcSMatt Macy }
4220eda14cbcSMatt Macy 
4221eda14cbcSMatt Macy /*
4222eda14cbcSMatt Macy  * Clear the errors for the pool, or the particular device if specified.
4223eda14cbcSMatt Macy  */
4224eda14cbcSMatt Macy int
zpool_clear(zpool_handle_t * zhp,const char * path,nvlist_t * rewindnvl)4225eda14cbcSMatt Macy zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
4226eda14cbcSMatt Macy {
4227eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
42281f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4229eda14cbcSMatt Macy 	nvlist_t *tgt;
4230eda14cbcSMatt Macy 	zpool_load_policy_t policy;
4231eda14cbcSMatt Macy 	boolean_t avail_spare, l2cache;
4232eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4233eda14cbcSMatt Macy 	nvlist_t *nvi = NULL;
4234eda14cbcSMatt Macy 	int error;
4235eda14cbcSMatt Macy 
4236eda14cbcSMatt Macy 	if (path)
42371f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4238eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
4239eda14cbcSMatt Macy 		    path);
4240eda14cbcSMatt Macy 	else
42411f1e2261SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
4242eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
4243eda14cbcSMatt Macy 		    zhp->zpool_name);
4244eda14cbcSMatt Macy 
4245eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4246eda14cbcSMatt Macy 	if (path) {
4247eda14cbcSMatt Macy 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
4248eda14cbcSMatt Macy 		    &l2cache, NULL)) == NULL)
42491f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4250eda14cbcSMatt Macy 
4251eda14cbcSMatt Macy 		/*
4252eda14cbcSMatt Macy 		 * Don't allow error clearing for hot spares.  Do allow
4253eda14cbcSMatt Macy 		 * error clearing for l2cache devices.
4254eda14cbcSMatt Macy 		 */
4255eda14cbcSMatt Macy 		if (avail_spare)
42561f1e2261SMartin Matuska 			return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
4257eda14cbcSMatt Macy 
4258da5137abSMartin Matuska 		zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
4259eda14cbcSMatt Macy 	}
4260eda14cbcSMatt Macy 
4261eda14cbcSMatt Macy 	zpool_get_load_policy(rewindnvl, &policy);
4262eda14cbcSMatt Macy 	zc.zc_cookie = policy.zlp_rewind;
4263eda14cbcSMatt Macy 
4264716fd348SMartin Matuska 	zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2);
4265716fd348SMartin Matuska 	zcmd_write_src_nvlist(hdl, &zc, rewindnvl);
4266eda14cbcSMatt Macy 
4267eda14cbcSMatt Macy 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
4268716fd348SMartin Matuska 	    errno == ENOMEM)
4269716fd348SMartin Matuska 		zcmd_expand_dst_nvlist(hdl, &zc);
4270eda14cbcSMatt Macy 
4271eda14cbcSMatt Macy 	if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
4272eda14cbcSMatt Macy 	    errno != EPERM && errno != EACCES)) {
4273eda14cbcSMatt Macy 		if (policy.zlp_rewind &
4274eda14cbcSMatt Macy 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
4275eda14cbcSMatt Macy 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
4276eda14cbcSMatt Macy 			zpool_rewind_exclaim(hdl, zc.zc_name,
4277eda14cbcSMatt Macy 			    ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
4278eda14cbcSMatt Macy 			    nvi);
4279eda14cbcSMatt Macy 			nvlist_free(nvi);
4280eda14cbcSMatt Macy 		}
4281eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
4282eda14cbcSMatt Macy 		return (0);
4283eda14cbcSMatt Macy 	}
4284eda14cbcSMatt Macy 
4285eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
42861f1e2261SMartin Matuska 	return (zpool_standard_error(hdl, errno, errbuf));
4287eda14cbcSMatt Macy }
4288eda14cbcSMatt Macy 
4289eda14cbcSMatt Macy /*
4290eda14cbcSMatt Macy  * Similar to zpool_clear(), but takes a GUID (used by fmd).
4291eda14cbcSMatt Macy  */
4292eda14cbcSMatt Macy int
zpool_vdev_clear(zpool_handle_t * zhp,uint64_t guid)4293eda14cbcSMatt Macy zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
4294eda14cbcSMatt Macy {
4295eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
42961f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4297eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4298eda14cbcSMatt Macy 
42991f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
4300eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
4301eda14cbcSMatt Macy 	    (u_longlong_t)guid);
4302eda14cbcSMatt Macy 
4303eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4304eda14cbcSMatt Macy 	zc.zc_guid = guid;
4305eda14cbcSMatt Macy 	zc.zc_cookie = ZPOOL_NO_REWIND;
4306eda14cbcSMatt Macy 
4307eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
4308eda14cbcSMatt Macy 		return (0);
4309eda14cbcSMatt Macy 
43101f1e2261SMartin Matuska 	return (zpool_standard_error(hdl, errno, errbuf));
4311eda14cbcSMatt Macy }
4312eda14cbcSMatt Macy 
4313eda14cbcSMatt Macy /*
4314eda14cbcSMatt Macy  * Change the GUID for a pool.
4315e2df9bb4SMartin Matuska  *
4316e2df9bb4SMartin Matuska  * Similar to zpool_reguid(), but may take a GUID.
4317e2df9bb4SMartin Matuska  *
4318e2df9bb4SMartin Matuska  * If the guid argument is NULL, then no GUID is passed in the nvlist to the
4319e2df9bb4SMartin Matuska  * ioctl().
4320eda14cbcSMatt Macy  */
4321eda14cbcSMatt Macy int
zpool_set_guid(zpool_handle_t * zhp,const uint64_t * guid)4322e2df9bb4SMartin Matuska zpool_set_guid(zpool_handle_t *zhp, const uint64_t *guid)
4323eda14cbcSMatt Macy {
43241f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4325eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4326e2df9bb4SMartin Matuska 	nvlist_t *nvl = NULL;
4327eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4328e2df9bb4SMartin Matuska 	int error = -1;
4329e2df9bb4SMartin Matuska 
4330e2df9bb4SMartin Matuska 	if (guid != NULL) {
4331e2df9bb4SMartin Matuska 		if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
4332e2df9bb4SMartin Matuska 			return (no_memory(hdl));
4333e2df9bb4SMartin Matuska 
4334e2df9bb4SMartin Matuska 		if (nvlist_add_uint64(nvl, ZPOOL_REGUID_GUID, *guid) != 0) {
4335e2df9bb4SMartin Matuska 			nvlist_free(nvl);
4336e2df9bb4SMartin Matuska 			return (no_memory(hdl));
4337e2df9bb4SMartin Matuska 		}
4338e2df9bb4SMartin Matuska 
4339e2df9bb4SMartin Matuska 		zcmd_write_src_nvlist(hdl, &zc, nvl);
4340e2df9bb4SMartin Matuska 	}
4341eda14cbcSMatt Macy 
43421f1e2261SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
4343eda14cbcSMatt Macy 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
4344eda14cbcSMatt Macy 
4345eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4346e2df9bb4SMartin Matuska 	error = zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc);
4347e2df9bb4SMartin Matuska 	if (error) {
43481f1e2261SMartin Matuska 		return (zpool_standard_error(hdl, errno, errbuf));
4349eda14cbcSMatt Macy 	}
4350e2df9bb4SMartin Matuska 	if (guid != NULL) {
4351e2df9bb4SMartin Matuska 		zcmd_free_nvlists(&zc);
4352e2df9bb4SMartin Matuska 		nvlist_free(nvl);
4353e2df9bb4SMartin Matuska 	}
4354e2df9bb4SMartin Matuska 	return (0);
4355e2df9bb4SMartin Matuska }
4356e2df9bb4SMartin Matuska 
4357e2df9bb4SMartin Matuska /*
4358e2df9bb4SMartin Matuska  * Change the GUID for a pool.
4359e2df9bb4SMartin Matuska  */
4360e2df9bb4SMartin Matuska int
zpool_reguid(zpool_handle_t * zhp)4361e2df9bb4SMartin Matuska zpool_reguid(zpool_handle_t *zhp)
4362e2df9bb4SMartin Matuska {
4363e2df9bb4SMartin Matuska 	return (zpool_set_guid(zhp, NULL));
4364e2df9bb4SMartin Matuska }
4365eda14cbcSMatt Macy 
4366eda14cbcSMatt Macy /*
4367eda14cbcSMatt Macy  * Reopen the pool.
4368eda14cbcSMatt Macy  */
4369eda14cbcSMatt Macy int
zpool_reopen_one(zpool_handle_t * zhp,void * data)4370eda14cbcSMatt Macy zpool_reopen_one(zpool_handle_t *zhp, void *data)
4371eda14cbcSMatt Macy {
4372eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zpool_get_handle(zhp);
4373eda14cbcSMatt Macy 	const char *pool_name = zpool_get_name(zhp);
4374eda14cbcSMatt Macy 	boolean_t *scrub_restart = data;
4375eda14cbcSMatt Macy 	int error;
4376eda14cbcSMatt Macy 
4377eda14cbcSMatt Macy 	error = lzc_reopen(pool_name, *scrub_restart);
4378eda14cbcSMatt Macy 	if (error) {
4379eda14cbcSMatt Macy 		return (zpool_standard_error_fmt(hdl, error,
4380eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
4381eda14cbcSMatt Macy 	}
4382eda14cbcSMatt Macy 
4383eda14cbcSMatt Macy 	return (0);
4384eda14cbcSMatt Macy }
4385eda14cbcSMatt Macy 
4386eda14cbcSMatt Macy /* call into libzfs_core to execute the sync IOCTL per pool */
4387eda14cbcSMatt Macy int
zpool_sync_one(zpool_handle_t * zhp,void * data)4388eda14cbcSMatt Macy zpool_sync_one(zpool_handle_t *zhp, void *data)
4389eda14cbcSMatt Macy {
4390eda14cbcSMatt Macy 	int ret;
4391eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zpool_get_handle(zhp);
4392eda14cbcSMatt Macy 	const char *pool_name = zpool_get_name(zhp);
4393eda14cbcSMatt Macy 	boolean_t *force = data;
4394eda14cbcSMatt Macy 	nvlist_t *innvl = fnvlist_alloc();
4395eda14cbcSMatt Macy 
4396eda14cbcSMatt Macy 	fnvlist_add_boolean_value(innvl, "force", *force);
4397eda14cbcSMatt Macy 	if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
4398eda14cbcSMatt Macy 		nvlist_free(innvl);
4399eda14cbcSMatt Macy 		return (zpool_standard_error_fmt(hdl, ret,
4400eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
4401eda14cbcSMatt Macy 	}
4402eda14cbcSMatt Macy 	nvlist_free(innvl);
4403eda14cbcSMatt Macy 
4404eda14cbcSMatt Macy 	return (0);
4405eda14cbcSMatt Macy }
4406eda14cbcSMatt Macy 
4407eda14cbcSMatt Macy #define	PATH_BUF_LEN	64
4408eda14cbcSMatt Macy 
4409eda14cbcSMatt Macy /*
4410eda14cbcSMatt Macy  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
4411eda14cbcSMatt Macy  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4412eda14cbcSMatt Macy  * We also check if this is a whole disk, in which case we strip off the
4413eda14cbcSMatt Macy  * trailing 's0' slice name.
4414eda14cbcSMatt Macy  *
4415eda14cbcSMatt Macy  * This routine is also responsible for identifying when disks have been
4416eda14cbcSMatt Macy  * reconfigured in a new location.  The kernel will have opened the device by
4417eda14cbcSMatt Macy  * devid, but the path will still refer to the old location.  To catch this, we
4418eda14cbcSMatt Macy  * first do a path -> devid translation (which is fast for the common case).  If
4419eda14cbcSMatt Macy  * the devid matches, we're done.  If not, we do a reverse devid -> path
4420eda14cbcSMatt Macy  * translation and issue the appropriate ioctl() to update the path of the vdev.
4421eda14cbcSMatt Macy  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4422eda14cbcSMatt Macy  * of these checks.
4423eda14cbcSMatt Macy  */
4424eda14cbcSMatt Macy char *
zpool_vdev_name(libzfs_handle_t * hdl,zpool_handle_t * zhp,nvlist_t * nv,int name_flags)4425eda14cbcSMatt Macy zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
4426eda14cbcSMatt Macy     int name_flags)
4427eda14cbcSMatt Macy {
44282a58b312SMartin Matuska 	const char *type, *tpath;
4429716fd348SMartin Matuska 	const char *path;
4430eda14cbcSMatt Macy 	uint64_t value;
4431eda14cbcSMatt Macy 	char buf[PATH_BUF_LEN];
4432716fd348SMartin Matuska 	char tmpbuf[PATH_BUF_LEN * 2];
4433eda14cbcSMatt Macy 
4434eda14cbcSMatt Macy 	/*
4435eda14cbcSMatt Macy 	 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4436eda14cbcSMatt Macy 	 * zpool name that will be displayed to the user.
4437eda14cbcSMatt Macy 	 */
4438da5137abSMartin Matuska 	type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE);
4439eda14cbcSMatt Macy 	if (zhp != NULL && strcmp(type, "root") == 0)
4440eda14cbcSMatt Macy 		return (zfs_strdup(hdl, zpool_get_name(zhp)));
4441eda14cbcSMatt Macy 
4442716fd348SMartin Matuska 	if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4443eda14cbcSMatt Macy 		name_flags |= VDEV_NAME_PATH;
4444716fd348SMartin Matuska 	if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4445eda14cbcSMatt Macy 		name_flags |= VDEV_NAME_GUID;
4446716fd348SMartin Matuska 	if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4447eda14cbcSMatt Macy 		name_flags |= VDEV_NAME_FOLLOW_LINKS;
4448eda14cbcSMatt Macy 
4449eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4450eda14cbcSMatt Macy 	    name_flags & VDEV_NAME_GUID) {
4451eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
4452eda14cbcSMatt Macy 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
4453eda14cbcSMatt Macy 		path = buf;
4454716fd348SMartin Matuska 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tpath) == 0) {
4455716fd348SMartin Matuska 		path = tpath;
4456716fd348SMartin Matuska 
4457eda14cbcSMatt Macy 		if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4458eda14cbcSMatt Macy 			char *rp = realpath(path, NULL);
4459eda14cbcSMatt Macy 			if (rp) {
4460eda14cbcSMatt Macy 				strlcpy(buf, rp, sizeof (buf));
4461eda14cbcSMatt Macy 				path = buf;
4462eda14cbcSMatt Macy 				free(rp);
4463eda14cbcSMatt Macy 			}
4464eda14cbcSMatt Macy 		}
4465eda14cbcSMatt Macy 
4466eda14cbcSMatt Macy 		/*
4467eda14cbcSMatt Macy 		 * For a block device only use the name.
4468eda14cbcSMatt Macy 		 */
4469eda14cbcSMatt Macy 		if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
4470eda14cbcSMatt Macy 		    !(name_flags & VDEV_NAME_PATH)) {
4471eda14cbcSMatt Macy 			path = zfs_strip_path(path);
4472eda14cbcSMatt Macy 		}
4473eda14cbcSMatt Macy 
4474eda14cbcSMatt Macy 		/*
44757877fdebSMatt Macy 		 * Remove the partition from the path if this is a whole disk.
4476eda14cbcSMatt Macy 		 */
44777877fdebSMatt Macy 		if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 &&
44787877fdebSMatt Macy 		    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4479eda14cbcSMatt Macy 		    == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
4480eda14cbcSMatt Macy 			return (zfs_strip_partition(path));
4481eda14cbcSMatt Macy 		}
4482eda14cbcSMatt Macy 	} else {
4483eda14cbcSMatt Macy 		path = type;
4484eda14cbcSMatt Macy 
4485eda14cbcSMatt Macy 		/*
4486eda14cbcSMatt Macy 		 * If it's a raidz device, we need to stick in the parity level.
4487eda14cbcSMatt Macy 		 */
4488eda14cbcSMatt Macy 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
4489da5137abSMartin Matuska 			value = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY);
4490eda14cbcSMatt Macy 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
4491eda14cbcSMatt Macy 			    (u_longlong_t)value);
4492eda14cbcSMatt Macy 			path = buf;
4493eda14cbcSMatt Macy 		}
4494eda14cbcSMatt Macy 
4495eda14cbcSMatt Macy 		/*
44967877fdebSMatt Macy 		 * If it's a dRAID device, we add parity, groups, and spares.
44977877fdebSMatt Macy 		 */
44987877fdebSMatt Macy 		if (strcmp(path, VDEV_TYPE_DRAID) == 0) {
44997877fdebSMatt Macy 			uint64_t ndata, nparity, nspares;
45007877fdebSMatt Macy 			nvlist_t **child;
45017877fdebSMatt Macy 			uint_t children;
45027877fdebSMatt Macy 
45037877fdebSMatt Macy 			verify(nvlist_lookup_nvlist_array(nv,
45047877fdebSMatt Macy 			    ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
4505da5137abSMartin Matuska 			nparity = fnvlist_lookup_uint64(nv,
4506da5137abSMartin Matuska 			    ZPOOL_CONFIG_NPARITY);
4507da5137abSMartin Matuska 			ndata = fnvlist_lookup_uint64(nv,
4508da5137abSMartin Matuska 			    ZPOOL_CONFIG_DRAID_NDATA);
4509da5137abSMartin Matuska 			nspares = fnvlist_lookup_uint64(nv,
4510da5137abSMartin Matuska 			    ZPOOL_CONFIG_DRAID_NSPARES);
45117877fdebSMatt Macy 
45127877fdebSMatt Macy 			path = zpool_draid_name(buf, sizeof (buf), ndata,
45137877fdebSMatt Macy 			    nparity, nspares, children);
45147877fdebSMatt Macy 		}
45157877fdebSMatt Macy 
45167877fdebSMatt Macy 		/*
4517eda14cbcSMatt Macy 		 * We identify each top-level vdev by using a <type-id>
4518eda14cbcSMatt Macy 		 * naming convention.
4519eda14cbcSMatt Macy 		 */
4520eda14cbcSMatt Macy 		if (name_flags & VDEV_NAME_TYPE_ID) {
4521da5137abSMartin Matuska 			uint64_t id = fnvlist_lookup_uint64(nv,
4522da5137abSMartin Matuska 			    ZPOOL_CONFIG_ID);
4523eda14cbcSMatt Macy 			(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
4524eda14cbcSMatt Macy 			    path, (u_longlong_t)id);
4525eda14cbcSMatt Macy 			path = tmpbuf;
4526eda14cbcSMatt Macy 		}
4527eda14cbcSMatt Macy 	}
4528eda14cbcSMatt Macy 
4529eda14cbcSMatt Macy 	return (zfs_strdup(hdl, path));
4530eda14cbcSMatt Macy }
4531eda14cbcSMatt Macy 
4532eda14cbcSMatt Macy static int
zbookmark_mem_compare(const void * a,const void * b)4533eda14cbcSMatt Macy zbookmark_mem_compare(const void *a, const void *b)
4534eda14cbcSMatt Macy {
4535eda14cbcSMatt Macy 	return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4536eda14cbcSMatt Macy }
4537eda14cbcSMatt Macy 
4538ce4dcb97SMartin Matuska void
zpool_add_propname(zpool_handle_t * zhp,const char * propname)4539ce4dcb97SMartin Matuska zpool_add_propname(zpool_handle_t *zhp, const char *propname)
4540ce4dcb97SMartin Matuska {
4541ce4dcb97SMartin Matuska 	assert(zhp->zpool_n_propnames < ZHP_MAX_PROPNAMES);
4542ce4dcb97SMartin Matuska 	zhp->zpool_propnames[zhp->zpool_n_propnames] = propname;
4543ce4dcb97SMartin Matuska 	zhp->zpool_n_propnames++;
4544ce4dcb97SMartin Matuska }
4545ce4dcb97SMartin Matuska 
4546eda14cbcSMatt Macy /*
4547eda14cbcSMatt Macy  * Retrieve the persistent error log, uniquify the members, and return to the
4548eda14cbcSMatt Macy  * caller.
4549eda14cbcSMatt Macy  */
4550eda14cbcSMatt Macy int
zpool_get_errlog(zpool_handle_t * zhp,nvlist_t ** nverrlistp)4551eda14cbcSMatt Macy zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4552eda14cbcSMatt Macy {
4553eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4554eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
455515f0b8c3SMartin Matuska 	zbookmark_phys_t *buf;
455615f0b8c3SMartin Matuska 	uint64_t buflen = 10000; /* approx. 1MB of RAM */
455715f0b8c3SMartin Matuska 
455815f0b8c3SMartin Matuska 	if (fnvlist_lookup_uint64(zhp->zpool_config,
455915f0b8c3SMartin Matuska 	    ZPOOL_CONFIG_ERRCOUNT) == 0)
456015f0b8c3SMartin Matuska 		return (0);
4561eda14cbcSMatt Macy 
4562eda14cbcSMatt Macy 	/*
456315f0b8c3SMartin Matuska 	 * Retrieve the raw error list from the kernel.  If it doesn't fit,
456415f0b8c3SMartin Matuska 	 * allocate a larger buffer and retry.
4565eda14cbcSMatt Macy 	 */
4566eda14cbcSMatt Macy 	(void) strcpy(zc.zc_name, zhp->zpool_name);
4567eda14cbcSMatt Macy 	for (;;) {
456815f0b8c3SMartin Matuska 		buf = zfs_alloc(zhp->zpool_hdl,
456915f0b8c3SMartin Matuska 		    buflen * sizeof (zbookmark_phys_t));
457015f0b8c3SMartin Matuska 		zc.zc_nvlist_dst = (uintptr_t)buf;
457115f0b8c3SMartin Matuska 		zc.zc_nvlist_dst_size = buflen;
4572eda14cbcSMatt Macy 		if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
4573eda14cbcSMatt Macy 		    &zc) != 0) {
457415f0b8c3SMartin Matuska 			free(buf);
4575eda14cbcSMatt Macy 			if (errno == ENOMEM) {
457615f0b8c3SMartin Matuska 				buflen *= 2;
4577eda14cbcSMatt Macy 			} else {
4578eda14cbcSMatt Macy 				return (zpool_standard_error_fmt(hdl, errno,
4579eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN, "errors: List of "
4580eda14cbcSMatt Macy 				    "errors unavailable")));
4581eda14cbcSMatt Macy 			}
4582eda14cbcSMatt Macy 		} else {
4583eda14cbcSMatt Macy 			break;
4584eda14cbcSMatt Macy 		}
4585eda14cbcSMatt Macy 	}
4586eda14cbcSMatt Macy 
4587eda14cbcSMatt Macy 	/*
4588eda14cbcSMatt Macy 	 * Sort the resulting bookmarks.  This is a little confusing due to the
4589eda14cbcSMatt Macy 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
4590eda14cbcSMatt Macy 	 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4591eda14cbcSMatt Macy 	 * _not_ copied as part of the process.  So we point the start of our
4592eda14cbcSMatt Macy 	 * array appropriate and decrement the total number of elements.
4593eda14cbcSMatt Macy 	 */
459415f0b8c3SMartin Matuska 	zbookmark_phys_t *zb = buf + zc.zc_nvlist_dst_size;
459515f0b8c3SMartin Matuska 	uint64_t zblen = buflen - zc.zc_nvlist_dst_size;
4596eda14cbcSMatt Macy 
459715f0b8c3SMartin Matuska 	qsort(zb, zblen, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4598eda14cbcSMatt Macy 
4599eda14cbcSMatt Macy 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4600eda14cbcSMatt Macy 
4601eda14cbcSMatt Macy 	/*
4602eda14cbcSMatt Macy 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4603eda14cbcSMatt Macy 	 */
460415f0b8c3SMartin Matuska 	for (uint64_t i = 0; i < zblen; i++) {
4605eda14cbcSMatt Macy 		nvlist_t *nv;
4606eda14cbcSMatt Macy 
4607eda14cbcSMatt Macy 		/* ignoring zb_blkid and zb_level for now */
4608eda14cbcSMatt Macy 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4609eda14cbcSMatt Macy 		    zb[i-1].zb_object == zb[i].zb_object)
4610eda14cbcSMatt Macy 			continue;
4611eda14cbcSMatt Macy 
4612eda14cbcSMatt Macy 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4613eda14cbcSMatt Macy 			goto nomem;
4614eda14cbcSMatt Macy 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4615eda14cbcSMatt Macy 		    zb[i].zb_objset) != 0) {
4616eda14cbcSMatt Macy 			nvlist_free(nv);
4617eda14cbcSMatt Macy 			goto nomem;
4618eda14cbcSMatt Macy 		}
4619eda14cbcSMatt Macy 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4620eda14cbcSMatt Macy 		    zb[i].zb_object) != 0) {
4621eda14cbcSMatt Macy 			nvlist_free(nv);
4622eda14cbcSMatt Macy 			goto nomem;
4623eda14cbcSMatt Macy 		}
4624eda14cbcSMatt Macy 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4625eda14cbcSMatt Macy 			nvlist_free(nv);
4626eda14cbcSMatt Macy 			goto nomem;
4627eda14cbcSMatt Macy 		}
4628eda14cbcSMatt Macy 		nvlist_free(nv);
4629eda14cbcSMatt Macy 	}
4630eda14cbcSMatt Macy 
463115f0b8c3SMartin Matuska 	free(buf);
4632eda14cbcSMatt Macy 	return (0);
4633eda14cbcSMatt Macy 
4634eda14cbcSMatt Macy nomem:
463515f0b8c3SMartin Matuska 	free(buf);
4636eda14cbcSMatt Macy 	return (no_memory(zhp->zpool_hdl));
4637eda14cbcSMatt Macy }
4638eda14cbcSMatt Macy 
4639eda14cbcSMatt Macy /*
4640eda14cbcSMatt Macy  * Upgrade a ZFS pool to the latest on-disk version.
4641eda14cbcSMatt Macy  */
4642eda14cbcSMatt Macy int
zpool_upgrade(zpool_handle_t * zhp,uint64_t new_version)4643eda14cbcSMatt Macy zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4644eda14cbcSMatt Macy {
4645eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4646eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4647eda14cbcSMatt Macy 
4648eda14cbcSMatt Macy 	(void) strcpy(zc.zc_name, zhp->zpool_name);
4649eda14cbcSMatt Macy 	zc.zc_cookie = new_version;
4650eda14cbcSMatt Macy 
4651eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4652eda14cbcSMatt Macy 		return (zpool_standard_error_fmt(hdl, errno,
4653eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4654eda14cbcSMatt Macy 		    zhp->zpool_name));
4655eda14cbcSMatt Macy 	return (0);
4656eda14cbcSMatt Macy }
4657eda14cbcSMatt Macy 
4658eda14cbcSMatt Macy void
zfs_save_arguments(int argc,char ** argv,char * string,int len)4659eda14cbcSMatt Macy zfs_save_arguments(int argc, char **argv, char *string, int len)
4660eda14cbcSMatt Macy {
4661eda14cbcSMatt Macy 	int i;
4662eda14cbcSMatt Macy 
46633ff01b23SMartin Matuska 	(void) strlcpy(string, zfs_basename(argv[0]), len);
4664eda14cbcSMatt Macy 	for (i = 1; i < argc; i++) {
4665eda14cbcSMatt Macy 		(void) strlcat(string, " ", len);
4666eda14cbcSMatt Macy 		(void) strlcat(string, argv[i], len);
4667eda14cbcSMatt Macy 	}
4668eda14cbcSMatt Macy }
4669eda14cbcSMatt Macy 
4670eda14cbcSMatt Macy int
zpool_log_history(libzfs_handle_t * hdl,const char * message)4671eda14cbcSMatt Macy zpool_log_history(libzfs_handle_t *hdl, const char *message)
4672eda14cbcSMatt Macy {
4673eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4674eda14cbcSMatt Macy 	nvlist_t *args;
4675eda14cbcSMatt Macy 
4676eda14cbcSMatt Macy 	args = fnvlist_alloc();
4677eda14cbcSMatt Macy 	fnvlist_add_string(args, "message", message);
4678716fd348SMartin Matuska 	zcmd_write_src_nvlist(hdl, &zc, args);
4679716fd348SMartin Matuska 	int err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
4680eda14cbcSMatt Macy 	nvlist_free(args);
4681eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
4682eda14cbcSMatt Macy 	return (err);
4683eda14cbcSMatt Macy }
4684eda14cbcSMatt Macy 
4685eda14cbcSMatt Macy /*
4686eda14cbcSMatt Macy  * Perform ioctl to get some command history of a pool.
4687eda14cbcSMatt Macy  *
4688eda14cbcSMatt Macy  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
4689eda14cbcSMatt Macy  * logical offset of the history buffer to start reading from.
4690eda14cbcSMatt Macy  *
4691eda14cbcSMatt Macy  * Upon return, 'off' is the next logical offset to read from and
4692eda14cbcSMatt Macy  * 'len' is the actual amount of bytes read into 'buf'.
4693eda14cbcSMatt Macy  */
4694eda14cbcSMatt Macy static int
get_history(zpool_handle_t * zhp,char * buf,uint64_t * off,uint64_t * len)4695eda14cbcSMatt Macy get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4696eda14cbcSMatt Macy {
4697eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4698eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4699eda14cbcSMatt Macy 
4700eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4701eda14cbcSMatt Macy 
4702eda14cbcSMatt Macy 	zc.zc_history = (uint64_t)(uintptr_t)buf;
4703eda14cbcSMatt Macy 	zc.zc_history_len = *len;
4704eda14cbcSMatt Macy 	zc.zc_history_offset = *off;
4705eda14cbcSMatt Macy 
4706eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4707eda14cbcSMatt Macy 		switch (errno) {
4708eda14cbcSMatt Macy 		case EPERM:
4709eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_PERM,
4710eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN,
4711eda14cbcSMatt Macy 			    "cannot show history for pool '%s'"),
4712eda14cbcSMatt Macy 			    zhp->zpool_name));
4713eda14cbcSMatt Macy 		case ENOENT:
4714eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4715eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
4716eda14cbcSMatt Macy 			    "'%s'"), zhp->zpool_name));
4717eda14cbcSMatt Macy 		case ENOTSUP:
4718eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4719eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
4720eda14cbcSMatt Macy 			    "'%s', pool must be upgraded"), zhp->zpool_name));
4721eda14cbcSMatt Macy 		default:
4722eda14cbcSMatt Macy 			return (zpool_standard_error_fmt(hdl, errno,
4723eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN,
4724eda14cbcSMatt Macy 			    "cannot get history for '%s'"), zhp->zpool_name));
4725eda14cbcSMatt Macy 		}
4726eda14cbcSMatt Macy 	}
4727eda14cbcSMatt Macy 
4728eda14cbcSMatt Macy 	*len = zc.zc_history_len;
4729eda14cbcSMatt Macy 	*off = zc.zc_history_offset;
4730eda14cbcSMatt Macy 
4731eda14cbcSMatt Macy 	return (0);
4732eda14cbcSMatt Macy }
4733eda14cbcSMatt Macy 
4734eda14cbcSMatt Macy /*
4735eda14cbcSMatt Macy  * Retrieve the command history of a pool.
4736eda14cbcSMatt Macy  */
4737eda14cbcSMatt Macy int
zpool_get_history(zpool_handle_t * zhp,nvlist_t ** nvhisp,uint64_t * off,boolean_t * eof)4738eda14cbcSMatt Macy zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4739eda14cbcSMatt Macy     boolean_t *eof)
4740eda14cbcSMatt Macy {
4741716fd348SMartin Matuska 	libzfs_handle_t *hdl = zhp->zpool_hdl;
4742eda14cbcSMatt Macy 	char *buf;
4743eda14cbcSMatt Macy 	int buflen = 128 * 1024;
4744eda14cbcSMatt Macy 	nvlist_t **records = NULL;
4745eda14cbcSMatt Macy 	uint_t numrecords = 0;
4746716fd348SMartin Matuska 	int err = 0, i;
4747eda14cbcSMatt Macy 	uint64_t start = *off;
4748eda14cbcSMatt Macy 
4749716fd348SMartin Matuska 	buf = zfs_alloc(hdl, buflen);
4750716fd348SMartin Matuska 
4751716fd348SMartin Matuska 	/* process about 1MiB a time */
4752eda14cbcSMatt Macy 	while (*off - start < 1024 * 1024) {
4753eda14cbcSMatt Macy 		uint64_t bytes_read = buflen;
4754eda14cbcSMatt Macy 		uint64_t leftover;
4755eda14cbcSMatt Macy 
4756eda14cbcSMatt Macy 		if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
4757eda14cbcSMatt Macy 			break;
4758eda14cbcSMatt Macy 
4759eda14cbcSMatt Macy 		/* if nothing else was read in, we're at EOF, just return */
4760eda14cbcSMatt Macy 		if (!bytes_read) {
4761eda14cbcSMatt Macy 			*eof = B_TRUE;
4762eda14cbcSMatt Macy 			break;
4763eda14cbcSMatt Macy 		}
4764eda14cbcSMatt Macy 
4765eda14cbcSMatt Macy 		if ((err = zpool_history_unpack(buf, bytes_read,
4766716fd348SMartin Matuska 		    &leftover, &records, &numrecords)) != 0) {
4767716fd348SMartin Matuska 			zpool_standard_error_fmt(hdl, err,
4768716fd348SMartin Matuska 			    dgettext(TEXT_DOMAIN,
4769716fd348SMartin Matuska 			    "cannot get history for '%s'"), zhp->zpool_name);
4770eda14cbcSMatt Macy 			break;
4771716fd348SMartin Matuska 		}
4772eda14cbcSMatt Macy 		*off -= leftover;
4773eda14cbcSMatt Macy 		if (leftover == bytes_read) {
4774eda14cbcSMatt Macy 			/*
4775eda14cbcSMatt Macy 			 * no progress made, because buffer is not big enough
4776eda14cbcSMatt Macy 			 * to hold this record; resize and retry.
4777eda14cbcSMatt Macy 			 */
4778eda14cbcSMatt Macy 			buflen *= 2;
4779eda14cbcSMatt Macy 			free(buf);
4780716fd348SMartin Matuska 			buf = zfs_alloc(hdl, buflen);
4781eda14cbcSMatt Macy 		}
4782eda14cbcSMatt Macy 	}
4783eda14cbcSMatt Macy 
4784eda14cbcSMatt Macy 	free(buf);
4785eda14cbcSMatt Macy 
4786eda14cbcSMatt Macy 	if (!err) {
4787da5137abSMartin Matuska 		*nvhisp = fnvlist_alloc();
4788da5137abSMartin Matuska 		fnvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4789da5137abSMartin Matuska 		    (const nvlist_t **)records, numrecords);
4790eda14cbcSMatt Macy 	}
4791eda14cbcSMatt Macy 	for (i = 0; i < numrecords; i++)
4792eda14cbcSMatt Macy 		nvlist_free(records[i]);
4793eda14cbcSMatt Macy 	free(records);
4794eda14cbcSMatt Macy 
4795eda14cbcSMatt Macy 	return (err);
4796eda14cbcSMatt Macy }
4797eda14cbcSMatt Macy 
4798eda14cbcSMatt Macy /*
4799eda14cbcSMatt Macy  * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4800eda14cbcSMatt Macy  * If there is a new event available 'nvp' will contain a newly allocated
4801eda14cbcSMatt Macy  * nvlist and 'dropped' will be set to the number of missed events since
4802eda14cbcSMatt Macy  * the last call to this function.  When 'nvp' is set to NULL it indicates
4803eda14cbcSMatt Macy  * no new events are available.  In either case the function returns 0 and
4804eda14cbcSMatt Macy  * it is up to the caller to free 'nvp'.  In the case of a fatal error the
4805eda14cbcSMatt Macy  * function will return a non-zero value.  When the function is called in
4806eda14cbcSMatt Macy  * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4807eda14cbcSMatt Macy  * it will not return until a new event is available.
4808eda14cbcSMatt Macy  */
4809eda14cbcSMatt Macy int
zpool_events_next(libzfs_handle_t * hdl,nvlist_t ** nvp,int * dropped,unsigned flags,int zevent_fd)4810eda14cbcSMatt Macy zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4811eda14cbcSMatt Macy     int *dropped, unsigned flags, int zevent_fd)
4812eda14cbcSMatt Macy {
4813eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4814eda14cbcSMatt Macy 	int error = 0;
4815eda14cbcSMatt Macy 
4816eda14cbcSMatt Macy 	*nvp = NULL;
4817eda14cbcSMatt Macy 	*dropped = 0;
4818eda14cbcSMatt Macy 	zc.zc_cleanup_fd = zevent_fd;
4819eda14cbcSMatt Macy 
4820eda14cbcSMatt Macy 	if (flags & ZEVENT_NONBLOCK)
4821eda14cbcSMatt Macy 		zc.zc_guid = ZEVENT_NONBLOCK;
4822eda14cbcSMatt Macy 
4823716fd348SMartin Matuska 	zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE);
4824eda14cbcSMatt Macy 
4825eda14cbcSMatt Macy retry:
4826eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4827eda14cbcSMatt Macy 		switch (errno) {
4828eda14cbcSMatt Macy 		case ESHUTDOWN:
4829eda14cbcSMatt Macy 			error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4830eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "zfs shutdown"));
4831eda14cbcSMatt Macy 			goto out;
4832eda14cbcSMatt Macy 		case ENOENT:
4833eda14cbcSMatt Macy 			/* Blocking error case should not occur */
4834eda14cbcSMatt Macy 			if (!(flags & ZEVENT_NONBLOCK))
4835eda14cbcSMatt Macy 				error = zpool_standard_error_fmt(hdl, errno,
4836eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN, "cannot get event"));
4837eda14cbcSMatt Macy 
4838eda14cbcSMatt Macy 			goto out;
4839eda14cbcSMatt Macy 		case ENOMEM:
4840716fd348SMartin Matuska 			zcmd_expand_dst_nvlist(hdl, &zc);
4841eda14cbcSMatt Macy 			goto retry;
4842eda14cbcSMatt Macy 		default:
4843eda14cbcSMatt Macy 			error = zpool_standard_error_fmt(hdl, errno,
4844eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot get event"));
4845eda14cbcSMatt Macy 			goto out;
4846eda14cbcSMatt Macy 		}
4847eda14cbcSMatt Macy 	}
4848eda14cbcSMatt Macy 
4849eda14cbcSMatt Macy 	error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4850eda14cbcSMatt Macy 	if (error != 0)
4851eda14cbcSMatt Macy 		goto out;
4852eda14cbcSMatt Macy 
4853eda14cbcSMatt Macy 	*dropped = (int)zc.zc_cookie;
4854eda14cbcSMatt Macy out:
4855eda14cbcSMatt Macy 	zcmd_free_nvlists(&zc);
4856eda14cbcSMatt Macy 
4857eda14cbcSMatt Macy 	return (error);
4858eda14cbcSMatt Macy }
4859eda14cbcSMatt Macy 
4860eda14cbcSMatt Macy /*
4861eda14cbcSMatt Macy  * Clear all events.
4862eda14cbcSMatt Macy  */
4863eda14cbcSMatt Macy int
zpool_events_clear(libzfs_handle_t * hdl,int * count)4864eda14cbcSMatt Macy zpool_events_clear(libzfs_handle_t *hdl, int *count)
4865eda14cbcSMatt Macy {
4866eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4867eda14cbcSMatt Macy 
4868eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
486916038816SMartin Matuska 		return (zpool_standard_error(hdl, errno,
487016038816SMartin Matuska 		    dgettext(TEXT_DOMAIN, "cannot clear events")));
4871eda14cbcSMatt Macy 
4872eda14cbcSMatt Macy 	if (count != NULL)
4873eda14cbcSMatt Macy 		*count = (int)zc.zc_cookie; /* # of events cleared */
4874eda14cbcSMatt Macy 
4875eda14cbcSMatt Macy 	return (0);
4876eda14cbcSMatt Macy }
4877eda14cbcSMatt Macy 
4878eda14cbcSMatt Macy /*
4879eda14cbcSMatt Macy  * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4880eda14cbcSMatt Macy  * the passed zevent_fd file handle.  On success zero is returned,
4881eda14cbcSMatt Macy  * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4882eda14cbcSMatt Macy  */
4883eda14cbcSMatt Macy int
zpool_events_seek(libzfs_handle_t * hdl,uint64_t eid,int zevent_fd)4884eda14cbcSMatt Macy zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4885eda14cbcSMatt Macy {
4886eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4887eda14cbcSMatt Macy 	int error = 0;
4888eda14cbcSMatt Macy 
4889eda14cbcSMatt Macy 	zc.zc_guid = eid;
4890eda14cbcSMatt Macy 	zc.zc_cleanup_fd = zevent_fd;
4891eda14cbcSMatt Macy 
4892eda14cbcSMatt Macy 	if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4893eda14cbcSMatt Macy 		switch (errno) {
4894eda14cbcSMatt Macy 		case ENOENT:
4895eda14cbcSMatt Macy 			error = zfs_error_fmt(hdl, EZFS_NOENT,
4896eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot get event"));
4897eda14cbcSMatt Macy 			break;
4898eda14cbcSMatt Macy 
4899eda14cbcSMatt Macy 		case ENOMEM:
4900eda14cbcSMatt Macy 			error = zfs_error_fmt(hdl, EZFS_NOMEM,
4901eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot get event"));
4902eda14cbcSMatt Macy 			break;
4903eda14cbcSMatt Macy 
4904eda14cbcSMatt Macy 		default:
4905eda14cbcSMatt Macy 			error = zpool_standard_error_fmt(hdl, errno,
4906eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot get event"));
4907eda14cbcSMatt Macy 			break;
4908eda14cbcSMatt Macy 		}
4909eda14cbcSMatt Macy 	}
4910eda14cbcSMatt Macy 
4911eda14cbcSMatt Macy 	return (error);
4912eda14cbcSMatt Macy }
4913eda14cbcSMatt Macy 
4914eda14cbcSMatt Macy static void
zpool_obj_to_path_impl(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len,boolean_t always_unmounted)4915eda14cbcSMatt Macy zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4916eda14cbcSMatt Macy     char *pathname, size_t len, boolean_t always_unmounted)
4917eda14cbcSMatt Macy {
4918eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
4919eda14cbcSMatt Macy 	boolean_t mounted = B_FALSE;
4920eda14cbcSMatt Macy 	char *mntpnt = NULL;
4921eda14cbcSMatt Macy 	char dsname[ZFS_MAX_DATASET_NAME_LEN];
4922eda14cbcSMatt Macy 
4923eda14cbcSMatt Macy 	if (dsobj == 0) {
4924eda14cbcSMatt Macy 		/* special case for the MOS */
4925eda14cbcSMatt Macy 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4926eda14cbcSMatt Macy 		    (longlong_t)obj);
4927eda14cbcSMatt Macy 		return;
4928eda14cbcSMatt Macy 	}
4929eda14cbcSMatt Macy 
4930eda14cbcSMatt Macy 	/* get the dataset's name */
4931eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4932eda14cbcSMatt Macy 	zc.zc_obj = dsobj;
4933eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zpool_hdl,
4934eda14cbcSMatt Macy 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4935eda14cbcSMatt Macy 		/* just write out a path of two object numbers */
4936eda14cbcSMatt Macy 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4937eda14cbcSMatt Macy 		    (longlong_t)dsobj, (longlong_t)obj);
4938eda14cbcSMatt Macy 		return;
4939eda14cbcSMatt Macy 	}
4940eda14cbcSMatt Macy 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4941eda14cbcSMatt Macy 
4942eda14cbcSMatt Macy 	/* find out if the dataset is mounted */
4943eda14cbcSMatt Macy 	mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
4944eda14cbcSMatt Macy 	    &mntpnt);
4945eda14cbcSMatt Macy 
4946eda14cbcSMatt Macy 	/* get the corrupted object's path */
4947eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4948eda14cbcSMatt Macy 	zc.zc_obj = obj;
4949eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
4950eda14cbcSMatt Macy 	    &zc) == 0) {
4951eda14cbcSMatt Macy 		if (mounted) {
4952eda14cbcSMatt Macy 			(void) snprintf(pathname, len, "%s%s", mntpnt,
4953eda14cbcSMatt Macy 			    zc.zc_value);
4954eda14cbcSMatt Macy 		} else {
4955eda14cbcSMatt Macy 			(void) snprintf(pathname, len, "%s:%s",
4956eda14cbcSMatt Macy 			    dsname, zc.zc_value);
4957eda14cbcSMatt Macy 		}
4958eda14cbcSMatt Macy 	} else {
4959eda14cbcSMatt Macy 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4960eda14cbcSMatt Macy 		    (longlong_t)obj);
4961eda14cbcSMatt Macy 	}
4962eda14cbcSMatt Macy 	free(mntpnt);
4963eda14cbcSMatt Macy }
4964eda14cbcSMatt Macy 
4965eda14cbcSMatt Macy void
zpool_obj_to_path(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len)4966eda14cbcSMatt Macy zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4967eda14cbcSMatt Macy     char *pathname, size_t len)
4968eda14cbcSMatt Macy {
4969eda14cbcSMatt Macy 	zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
4970eda14cbcSMatt Macy }
4971eda14cbcSMatt Macy 
4972eda14cbcSMatt Macy void
zpool_obj_to_path_ds(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len)4973eda14cbcSMatt Macy zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4974eda14cbcSMatt Macy     char *pathname, size_t len)
4975eda14cbcSMatt Macy {
4976eda14cbcSMatt Macy 	zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
4977eda14cbcSMatt Macy }
4978eda14cbcSMatt Macy /*
4979eda14cbcSMatt Macy  * Wait while the specified activity is in progress in the pool.
4980eda14cbcSMatt Macy  */
4981eda14cbcSMatt Macy int
zpool_wait(zpool_handle_t * zhp,zpool_wait_activity_t activity)4982eda14cbcSMatt Macy zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4983eda14cbcSMatt Macy {
4984eda14cbcSMatt Macy 	boolean_t missing;
4985eda14cbcSMatt Macy 
4986eda14cbcSMatt Macy 	int error = zpool_wait_status(zhp, activity, &missing, NULL);
4987eda14cbcSMatt Macy 
4988eda14cbcSMatt Macy 	if (missing) {
4989eda14cbcSMatt Macy 		(void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4990eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4991eda14cbcSMatt Macy 		    zhp->zpool_name);
4992eda14cbcSMatt Macy 		return (ENOENT);
4993eda14cbcSMatt Macy 	} else {
4994eda14cbcSMatt Macy 		return (error);
4995eda14cbcSMatt Macy 	}
4996eda14cbcSMatt Macy }
4997eda14cbcSMatt Macy 
4998eda14cbcSMatt Macy /*
4999eda14cbcSMatt Macy  * Wait for the given activity and return the status of the wait (whether or not
5000eda14cbcSMatt Macy  * any waiting was done) in the 'waited' parameter. Non-existent pools are
5001eda14cbcSMatt Macy  * reported via the 'missing' parameter, rather than by printing an error
5002eda14cbcSMatt Macy  * message. This is convenient when this function is called in a loop over a
5003eda14cbcSMatt Macy  * long period of time (as it is, for example, by zpool's wait cmd). In that
5004eda14cbcSMatt Macy  * scenario, a pool being exported or destroyed should be considered a normal
5005eda14cbcSMatt Macy  * event, so we don't want to print an error when we find that the pool doesn't
5006eda14cbcSMatt Macy  * exist.
5007eda14cbcSMatt Macy  */
5008eda14cbcSMatt Macy int
zpool_wait_status(zpool_handle_t * zhp,zpool_wait_activity_t activity,boolean_t * missing,boolean_t * waited)5009eda14cbcSMatt Macy zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
5010eda14cbcSMatt Macy     boolean_t *missing, boolean_t *waited)
5011eda14cbcSMatt Macy {
5012eda14cbcSMatt Macy 	int error = lzc_wait(zhp->zpool_name, activity, waited);
5013eda14cbcSMatt Macy 	*missing = (error == ENOENT);
5014eda14cbcSMatt Macy 	if (*missing)
5015eda14cbcSMatt Macy 		return (0);
5016eda14cbcSMatt Macy 
5017eda14cbcSMatt Macy 	if (error != 0) {
5018eda14cbcSMatt Macy 		(void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
5019eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
5020eda14cbcSMatt Macy 		    zhp->zpool_name);
5021eda14cbcSMatt Macy 	}
5022eda14cbcSMatt Macy 
5023eda14cbcSMatt Macy 	return (error);
5024eda14cbcSMatt Macy }
5025eda14cbcSMatt Macy 
5026eda14cbcSMatt Macy int
zpool_set_bootenv(zpool_handle_t * zhp,const nvlist_t * envmap)50272c48331dSMatt Macy zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
5028eda14cbcSMatt Macy {
5029eda14cbcSMatt Macy 	int error = lzc_set_bootenv(zhp->zpool_name, envmap);
5030eda14cbcSMatt Macy 	if (error != 0) {
5031eda14cbcSMatt Macy 		(void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
5032eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN,
5033eda14cbcSMatt Macy 		    "error setting bootenv in pool '%s'"), zhp->zpool_name);
5034eda14cbcSMatt Macy 	}
5035eda14cbcSMatt Macy 
5036eda14cbcSMatt Macy 	return (error);
5037eda14cbcSMatt Macy }
5038eda14cbcSMatt Macy 
5039eda14cbcSMatt Macy int
zpool_get_bootenv(zpool_handle_t * zhp,nvlist_t ** nvlp)50402c48331dSMatt Macy zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
5041eda14cbcSMatt Macy {
50422c48331dSMatt Macy 	nvlist_t *nvl;
50432c48331dSMatt Macy 	int error;
50442c48331dSMatt Macy 
50452c48331dSMatt Macy 	nvl = NULL;
50462c48331dSMatt Macy 	error = lzc_get_bootenv(zhp->zpool_name, &nvl);
5047eda14cbcSMatt Macy 	if (error != 0) {
5048eda14cbcSMatt Macy 		(void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
5049eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN,
5050eda14cbcSMatt Macy 		    "error getting bootenv in pool '%s'"), zhp->zpool_name);
50512c48331dSMatt Macy 	} else {
50522c48331dSMatt Macy 		*nvlp = nvl;
5053eda14cbcSMatt Macy 	}
5054eda14cbcSMatt Macy 
50552c48331dSMatt Macy 	return (error);
5056eda14cbcSMatt Macy }
5057ee36e25aSMartin Matuska 
5058ee36e25aSMartin Matuska /*
5059ee36e25aSMartin Matuska  * Attempt to read and parse feature file(s) (from "compatibility" property).
5060ee36e25aSMartin Matuska  * Files contain zpool feature names, comma or whitespace-separated.
5061ee36e25aSMartin Matuska  * Comments (# character to next newline) are discarded.
5062ee36e25aSMartin Matuska  *
5063ee36e25aSMartin Matuska  * Arguments:
5064ee36e25aSMartin Matuska  *  compatibility : string containing feature filenames
5065ee36e25aSMartin Matuska  *  features : either NULL or pointer to array of boolean
506616038816SMartin Matuska  *  report : either NULL or pointer to string buffer
506716038816SMartin Matuska  *  rlen : length of "report" buffer
5068ee36e25aSMartin Matuska  *
5069ee36e25aSMartin Matuska  * compatibility is NULL (unset), "", "off", "legacy", or list of
5070ee36e25aSMartin Matuska  * comma-separated filenames. filenames should either be absolute,
5071ee36e25aSMartin Matuska  * or relative to:
5072ee36e25aSMartin Matuska  *   1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
5073ee36e25aSMartin Matuska  *   2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
5074ee36e25aSMartin Matuska  * (Unset), "" or "off" => enable all features
5075ee36e25aSMartin Matuska  * "legacy" => disable all features
507616038816SMartin Matuska  *
5077ee36e25aSMartin Matuska  * Any feature names read from files which match unames in spa_feature_table
5078ee36e25aSMartin Matuska  * will have the corresponding boolean set in the features array (if non-NULL).
5079ee36e25aSMartin Matuska  * If more than one feature set specified, only features present in *all* of
5080ee36e25aSMartin Matuska  * them will be set.
5081ee36e25aSMartin Matuska  *
508216038816SMartin Matuska  * "report" if not NULL will be populated with a suitable status message.
5083ee36e25aSMartin Matuska  *
5084ee36e25aSMartin Matuska  * Return values:
5085ee36e25aSMartin Matuska  *   ZPOOL_COMPATIBILITY_OK : files read and parsed ok
5086ee36e25aSMartin Matuska  *   ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
508716038816SMartin Matuska  *   ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
508816038816SMartin Matuska  *   ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
508916038816SMartin Matuska  *   ZPOOL_COMPATIBILITY_NOFILES : no feature files found
5090ee36e25aSMartin Matuska  */
5091ee36e25aSMartin Matuska zpool_compat_status_t
zpool_load_compat(const char * compat,boolean_t * features,char * report,size_t rlen)509216038816SMartin Matuska zpool_load_compat(const char *compat, boolean_t *features, char *report,
509316038816SMartin Matuska     size_t rlen)
5094ee36e25aSMartin Matuska {
5095ee36e25aSMartin Matuska 	int sdirfd, ddirfd, featfd;
5096ee36e25aSMartin Matuska 	struct stat fs;
509716038816SMartin Matuska 	char *fc;
509816038816SMartin Matuska 	char *ps, *ls, *ws;
5099ee36e25aSMartin Matuska 	char *file, *line, *word;
510016038816SMartin Matuska 
510116038816SMartin Matuska 	char l_compat[ZFS_MAXPROPLEN];
510216038816SMartin Matuska 
510316038816SMartin Matuska 	boolean_t ret_nofiles = B_TRUE;
510416038816SMartin Matuska 	boolean_t ret_badfile = B_FALSE;
510516038816SMartin Matuska 	boolean_t ret_badtoken = B_FALSE;
510616038816SMartin Matuska 	boolean_t ret_warntoken = B_FALSE;
5107ee36e25aSMartin Matuska 
5108ee36e25aSMartin Matuska 	/* special cases (unset), "" and "off" => enable all features */
510916038816SMartin Matuska 	if (compat == NULL || compat[0] == '\0' ||
511016038816SMartin Matuska 	    strcmp(compat, ZPOOL_COMPAT_OFF) == 0) {
5111ee36e25aSMartin Matuska 		if (features != NULL)
511216038816SMartin Matuska 			for (uint_t i = 0; i < SPA_FEATURES; i++)
5113ee36e25aSMartin Matuska 				features[i] = B_TRUE;
511416038816SMartin Matuska 		if (report != NULL)
511516038816SMartin Matuska 			strlcpy(report, gettext("all features enabled"), rlen);
5116ee36e25aSMartin Matuska 		return (ZPOOL_COMPATIBILITY_OK);
5117ee36e25aSMartin Matuska 	}
5118ee36e25aSMartin Matuska 
5119ee36e25aSMartin Matuska 	/* Final special case "legacy" => disable all features */
512016038816SMartin Matuska 	if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
5121ee36e25aSMartin Matuska 		if (features != NULL)
512216038816SMartin Matuska 			for (uint_t i = 0; i < SPA_FEATURES; i++)
5123ee36e25aSMartin Matuska 				features[i] = B_FALSE;
512416038816SMartin Matuska 		if (report != NULL)
512516038816SMartin Matuska 			strlcpy(report, gettext("all features disabled"), rlen);
5126ee36e25aSMartin Matuska 		return (ZPOOL_COMPATIBILITY_OK);
5127ee36e25aSMartin Matuska 	}
5128ee36e25aSMartin Matuska 
5129ee36e25aSMartin Matuska 	/*
5130ee36e25aSMartin Matuska 	 * Start with all true; will be ANDed with results from each file
5131ee36e25aSMartin Matuska 	 */
5132ee36e25aSMartin Matuska 	if (features != NULL)
513316038816SMartin Matuska 		for (uint_t i = 0; i < SPA_FEATURES; i++)
5134ee36e25aSMartin Matuska 			features[i] = B_TRUE;
5135ee36e25aSMartin Matuska 
5136c7046f76SMartin Matuska 	char err_badfile[ZFS_MAXPROPLEN] = "";
5137c7046f76SMartin Matuska 	char err_badtoken[ZFS_MAXPROPLEN] = "";
513816038816SMartin Matuska 
5139ee36e25aSMartin Matuska 	/*
5140ee36e25aSMartin Matuska 	 * We ignore errors from the directory open()
5141ee36e25aSMartin Matuska 	 * as they're only needed if the filename is relative
5142ee36e25aSMartin Matuska 	 * which will be checked during the openat().
5143ee36e25aSMartin Matuska 	 */
514416038816SMartin Matuska 
514516038816SMartin Matuska /* O_PATH safer than O_RDONLY if system allows it */
514616038816SMartin Matuska #if defined(O_PATH)
514716038816SMartin Matuska #define	ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
5148ee36e25aSMartin Matuska #else
514916038816SMartin Matuska #define	ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
5150ee36e25aSMartin Matuska #endif
5151ee36e25aSMartin Matuska 
515216038816SMartin Matuska 	sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
515316038816SMartin Matuska 	ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
515416038816SMartin Matuska 
515516038816SMartin Matuska 	(void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
515616038816SMartin Matuska 
515716038816SMartin Matuska 	for (file = strtok_r(l_compat, ",", &ps);
515816038816SMartin Matuska 	    file != NULL;
515916038816SMartin Matuska 	    file = strtok_r(NULL, ",", &ps)) {
516016038816SMartin Matuska 
516116038816SMartin Matuska 		boolean_t l_features[SPA_FEATURES];
516216038816SMartin Matuska 
516316038816SMartin Matuska 		enum { Z_SYSCONF, Z_DATA } source;
5164ee36e25aSMartin Matuska 
5165ee36e25aSMartin Matuska 		/* try sysconfdir first, then datadir */
516616038816SMartin Matuska 		source = Z_SYSCONF;
516716038816SMartin Matuska 		if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) {
516816038816SMartin Matuska 			featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC);
516916038816SMartin Matuska 			source = Z_DATA;
5170ee36e25aSMartin Matuska 		}
5171ee36e25aSMartin Matuska 
517216038816SMartin Matuska 		/* File readable and correct size? */
517316038816SMartin Matuska 		if (featfd < 0 ||
517416038816SMartin Matuska 		    fstat(featfd, &fs) < 0 ||
517516038816SMartin Matuska 		    fs.st_size < 1 ||
517616038816SMartin Matuska 		    fs.st_size > ZPOOL_COMPAT_MAXSIZE) {
5177ee36e25aSMartin Matuska 			(void) close(featfd);
517816038816SMartin Matuska 			strlcat(err_badfile, file, ZFS_MAXPROPLEN);
517916038816SMartin Matuska 			strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
518016038816SMartin Matuska 			ret_badfile = B_TRUE;
518116038816SMartin Matuska 			continue;
5182ee36e25aSMartin Matuska 		}
5183ee36e25aSMartin Matuska 
518416038816SMartin Matuska /* Prefault the file if system allows */
518516038816SMartin Matuska #if defined(MAP_POPULATE)
518616038816SMartin Matuska #define	ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
518716038816SMartin Matuska #elif defined(MAP_PREFAULT_READ)
518816038816SMartin Matuska #define	ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
518916038816SMartin Matuska #else
519016038816SMartin Matuska #define	ZC_MMAP_FLAGS (MAP_PRIVATE)
519116038816SMartin Matuska #endif
519216038816SMartin Matuska 
5193ee36e25aSMartin Matuska 		/* private mmap() so we can strtok safely */
519416038816SMartin Matuska 		fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
519516038816SMartin Matuska 		    ZC_MMAP_FLAGS, featfd, 0);
5196ee36e25aSMartin Matuska 		(void) close(featfd);
5197ee36e25aSMartin Matuska 
519816038816SMartin Matuska 		/* map ok, and last character == newline? */
519916038816SMartin Matuska 		if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') {
5200ee36e25aSMartin Matuska 			(void) munmap((void *) fc, fs.st_size);
520116038816SMartin Matuska 			strlcat(err_badfile, file, ZFS_MAXPROPLEN);
520216038816SMartin Matuska 			strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
520316038816SMartin Matuska 			ret_badfile = B_TRUE;
520416038816SMartin Matuska 			continue;
5205ee36e25aSMartin Matuska 		}
5206ee36e25aSMartin Matuska 
520716038816SMartin Matuska 		ret_nofiles = B_FALSE;
520816038816SMartin Matuska 
520916038816SMartin Matuska 		for (uint_t i = 0; i < SPA_FEATURES; i++)
521016038816SMartin Matuska 			l_features[i] = B_FALSE;
521116038816SMartin Matuska 
521216038816SMartin Matuska 		/* replace final newline with NULL to ensure string ends */
5213ee36e25aSMartin Matuska 		fc[fs.st_size - 1] = '\0';
5214ee36e25aSMartin Matuska 
521516038816SMartin Matuska 		for (line = strtok_r(fc, "\n", &ls);
521616038816SMartin Matuska 		    line != NULL;
521716038816SMartin Matuska 		    line = strtok_r(NULL, "\n", &ls)) {
5218ee36e25aSMartin Matuska 			/* discard comments */
52195eb61f6cSMartin Matuska 			char *r = strchr(line, '#');
52205eb61f6cSMartin Matuska 			if (r != NULL)
52215eb61f6cSMartin Matuska 				*r = '\0';
5222ee36e25aSMartin Matuska 
522316038816SMartin Matuska 			for (word = strtok_r(line, ", \t", &ws);
522416038816SMartin Matuska 			    word != NULL;
522516038816SMartin Matuska 			    word = strtok_r(NULL, ", \t", &ws)) {
5226ee36e25aSMartin Matuska 				/* Find matching feature name */
522716038816SMartin Matuska 				uint_t f;
522816038816SMartin Matuska 				for (f = 0; f < SPA_FEATURES; f++) {
5229ee36e25aSMartin Matuska 					zfeature_info_t *fi =
523016038816SMartin Matuska 					    &spa_feature_table[f];
5231ee36e25aSMartin Matuska 					if (strcmp(word, fi->fi_uname) == 0) {
523216038816SMartin Matuska 						l_features[f] = B_TRUE;
5233ee36e25aSMartin Matuska 						break;
5234ee36e25aSMartin Matuska 					}
5235ee36e25aSMartin Matuska 				}
523616038816SMartin Matuska 				if (f < SPA_FEATURES)
523716038816SMartin Matuska 					continue;
523816038816SMartin Matuska 
523916038816SMartin Matuska 				/* found an unrecognized word */
524016038816SMartin Matuska 				/* lightly sanitize it */
524116038816SMartin Matuska 				if (strlen(word) > 32)
524216038816SMartin Matuska 					word[32] = '\0';
524316038816SMartin Matuska 				for (char *c = word; *c != '\0'; c++)
524416038816SMartin Matuska 					if (!isprint(*c))
524516038816SMartin Matuska 						*c = '?';
524616038816SMartin Matuska 
524716038816SMartin Matuska 				strlcat(err_badtoken, word, ZFS_MAXPROPLEN);
524816038816SMartin Matuska 				strlcat(err_badtoken, " ", ZFS_MAXPROPLEN);
524916038816SMartin Matuska 				if (source == Z_SYSCONF)
525016038816SMartin Matuska 					ret_badtoken = B_TRUE;
525116038816SMartin Matuska 				else
525216038816SMartin Matuska 					ret_warntoken = B_TRUE;
5253ee36e25aSMartin Matuska 			}
5254ee36e25aSMartin Matuska 		}
5255ee36e25aSMartin Matuska 		(void) munmap((void *) fc, fs.st_size);
525616038816SMartin Matuska 
525716038816SMartin Matuska 		if (features != NULL)
525816038816SMartin Matuska 			for (uint_t i = 0; i < SPA_FEATURES; i++)
525916038816SMartin Matuska 				features[i] &= l_features[i];
5260ee36e25aSMartin Matuska 	}
5261ee36e25aSMartin Matuska 	(void) close(sdirfd);
5262ee36e25aSMartin Matuska 	(void) close(ddirfd);
526316038816SMartin Matuska 
526416038816SMartin Matuska 	/* Return the most serious error */
526516038816SMartin Matuska 	if (ret_badfile) {
526616038816SMartin Matuska 		if (report != NULL)
526716038816SMartin Matuska 			snprintf(report, rlen, gettext("could not read/"
526816038816SMartin Matuska 			    "parse feature file(s): %s"), err_badfile);
526916038816SMartin Matuska 		return (ZPOOL_COMPATIBILITY_BADFILE);
527016038816SMartin Matuska 	}
527116038816SMartin Matuska 	if (ret_nofiles) {
527216038816SMartin Matuska 		if (report != NULL)
527316038816SMartin Matuska 			strlcpy(report,
527416038816SMartin Matuska 			    gettext("no valid compatibility files specified"),
527516038816SMartin Matuska 			    rlen);
5276ee36e25aSMartin Matuska 		return (ZPOOL_COMPATIBILITY_NOFILES);
527716038816SMartin Matuska 	}
527816038816SMartin Matuska 	if (ret_badtoken) {
527916038816SMartin Matuska 		if (report != NULL)
528016038816SMartin Matuska 			snprintf(report, rlen, gettext("invalid feature "
528116038816SMartin Matuska 			    "name(s) in local compatibility files: %s"),
528216038816SMartin Matuska 			    err_badtoken);
528316038816SMartin Matuska 		return (ZPOOL_COMPATIBILITY_BADTOKEN);
528416038816SMartin Matuska 	}
528516038816SMartin Matuska 	if (ret_warntoken) {
528616038816SMartin Matuska 		if (report != NULL)
528716038816SMartin Matuska 			snprintf(report, rlen, gettext("unrecognized feature "
528816038816SMartin Matuska 			    "name(s) in distribution compatibility files: %s"),
528916038816SMartin Matuska 			    err_badtoken);
529016038816SMartin Matuska 		return (ZPOOL_COMPATIBILITY_WARNTOKEN);
529116038816SMartin Matuska 	}
529216038816SMartin Matuska 	if (report != NULL)
529316038816SMartin Matuska 		strlcpy(report, gettext("compatibility set ok"), rlen);
5294ee36e25aSMartin Matuska 	return (ZPOOL_COMPATIBILITY_OK);
5295ee36e25aSMartin Matuska }
5296681ce946SMartin Matuska 
5297681ce946SMartin Matuska static int
zpool_vdev_guid(zpool_handle_t * zhp,const char * vdevname,uint64_t * vdev_guid)5298681ce946SMartin Matuska zpool_vdev_guid(zpool_handle_t *zhp, const char *vdevname, uint64_t *vdev_guid)
5299681ce946SMartin Matuska {
5300681ce946SMartin Matuska 	nvlist_t *tgt;
5301681ce946SMartin Matuska 	boolean_t avail_spare, l2cache;
5302681ce946SMartin Matuska 
5303681ce946SMartin Matuska 	verify(zhp != NULL);
5304681ce946SMartin Matuska 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
53051f1e2261SMartin Matuska 		char errbuf[ERRBUFLEN];
5306681ce946SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
5307681ce946SMartin Matuska 		    dgettext(TEXT_DOMAIN, "pool is in an unavailable state"));
5308681ce946SMartin Matuska 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLUNAVAIL, errbuf));
5309681ce946SMartin Matuska 	}
5310681ce946SMartin Matuska 
5311681ce946SMartin Matuska 	if ((tgt = zpool_find_vdev(zhp, vdevname, &avail_spare, &l2cache,
5312681ce946SMartin Matuska 	    NULL)) == NULL) {
53131f1e2261SMartin Matuska 		char errbuf[ERRBUFLEN];
5314681ce946SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
5315681ce946SMartin Matuska 		    dgettext(TEXT_DOMAIN, "can not find %s in %s"),
5316681ce946SMartin Matuska 		    vdevname, zhp->zpool_name);
5317681ce946SMartin Matuska 		return (zfs_error(zhp->zpool_hdl, EZFS_NODEVICE, errbuf));
5318681ce946SMartin Matuska 	}
5319681ce946SMartin Matuska 
5320da5137abSMartin Matuska 	*vdev_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
5321681ce946SMartin Matuska 	return (0);
5322681ce946SMartin Matuska }
5323681ce946SMartin Matuska 
5324681ce946SMartin Matuska /*
5325681ce946SMartin Matuska  * Get a vdev property value for 'prop' and return the value in
5326681ce946SMartin Matuska  * a pre-allocated buffer.
5327681ce946SMartin Matuska  */
5328681ce946SMartin Matuska int
zpool_get_vdev_prop_value(nvlist_t * nvprop,vdev_prop_t prop,char * prop_name,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)5329681ce946SMartin Matuska zpool_get_vdev_prop_value(nvlist_t *nvprop, vdev_prop_t prop, char *prop_name,
5330681ce946SMartin Matuska     char *buf, size_t len, zprop_source_t *srctype, boolean_t literal)
5331681ce946SMartin Matuska {
5332681ce946SMartin Matuska 	nvlist_t *nv;
5333a0b956f5SMartin Matuska 	const char *strval;
5334da5137abSMartin Matuska 	uint64_t intval;
5335681ce946SMartin Matuska 	zprop_source_t src = ZPROP_SRC_NONE;
5336681ce946SMartin Matuska 
53371f1e2261SMartin Matuska 	if (prop == VDEV_PROP_USERPROP) {
5338681ce946SMartin Matuska 		/* user property, prop_name must contain the property name */
5339681ce946SMartin Matuska 		assert(prop_name != NULL);
5340681ce946SMartin Matuska 		if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5341da5137abSMartin Matuska 			src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5342da5137abSMartin Matuska 			strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
5343681ce946SMartin Matuska 		} else {
5344681ce946SMartin Matuska 			/* user prop not found */
5345681ce946SMartin Matuska 			return (-1);
5346681ce946SMartin Matuska 		}
5347681ce946SMartin Matuska 		(void) strlcpy(buf, strval, len);
5348681ce946SMartin Matuska 		if (srctype)
5349681ce946SMartin Matuska 			*srctype = src;
5350681ce946SMartin Matuska 		return (0);
5351681ce946SMartin Matuska 	}
5352681ce946SMartin Matuska 
5353681ce946SMartin Matuska 	if (prop_name == NULL)
5354681ce946SMartin Matuska 		prop_name = (char *)vdev_prop_to_name(prop);
5355681ce946SMartin Matuska 
5356681ce946SMartin Matuska 	switch (vdev_prop_get_type(prop)) {
5357681ce946SMartin Matuska 	case PROP_TYPE_STRING:
5358681ce946SMartin Matuska 		if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5359da5137abSMartin Matuska 			src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5360da5137abSMartin Matuska 			strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
5361681ce946SMartin Matuska 		} else {
5362681ce946SMartin Matuska 			src = ZPROP_SRC_DEFAULT;
5363a0b956f5SMartin Matuska 			if ((strval = vdev_prop_default_string(prop)) == NULL)
5364681ce946SMartin Matuska 				strval = "-";
5365681ce946SMartin Matuska 		}
5366681ce946SMartin Matuska 		(void) strlcpy(buf, strval, len);
5367681ce946SMartin Matuska 		break;
5368681ce946SMartin Matuska 
5369681ce946SMartin Matuska 	case PROP_TYPE_NUMBER:
5370681ce946SMartin Matuska 		if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5371da5137abSMartin Matuska 			src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5372da5137abSMartin Matuska 			intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
5373681ce946SMartin Matuska 		} else {
5374681ce946SMartin Matuska 			src = ZPROP_SRC_DEFAULT;
5375681ce946SMartin Matuska 			intval = vdev_prop_default_numeric(prop);
5376681ce946SMartin Matuska 		}
5377681ce946SMartin Matuska 
5378681ce946SMartin Matuska 		switch (prop) {
5379681ce946SMartin Matuska 		case VDEV_PROP_ASIZE:
5380681ce946SMartin Matuska 		case VDEV_PROP_PSIZE:
5381681ce946SMartin Matuska 		case VDEV_PROP_SIZE:
5382c03c5b1cSMartin Matuska 		case VDEV_PROP_BOOTSIZE:
5383681ce946SMartin Matuska 		case VDEV_PROP_ALLOCATED:
5384681ce946SMartin Matuska 		case VDEV_PROP_FREE:
5385681ce946SMartin Matuska 		case VDEV_PROP_READ_ERRORS:
5386681ce946SMartin Matuska 		case VDEV_PROP_WRITE_ERRORS:
5387681ce946SMartin Matuska 		case VDEV_PROP_CHECKSUM_ERRORS:
5388681ce946SMartin Matuska 		case VDEV_PROP_INITIALIZE_ERRORS:
5389ce4dcb97SMartin Matuska 		case VDEV_PROP_TRIM_ERRORS:
5390ce4dcb97SMartin Matuska 		case VDEV_PROP_SLOW_IOS:
5391681ce946SMartin Matuska 		case VDEV_PROP_OPS_NULL:
5392681ce946SMartin Matuska 		case VDEV_PROP_OPS_READ:
5393681ce946SMartin Matuska 		case VDEV_PROP_OPS_WRITE:
5394681ce946SMartin Matuska 		case VDEV_PROP_OPS_FREE:
5395681ce946SMartin Matuska 		case VDEV_PROP_OPS_CLAIM:
5396681ce946SMartin Matuska 		case VDEV_PROP_OPS_TRIM:
5397681ce946SMartin Matuska 		case VDEV_PROP_BYTES_NULL:
5398681ce946SMartin Matuska 		case VDEV_PROP_BYTES_READ:
5399681ce946SMartin Matuska 		case VDEV_PROP_BYTES_WRITE:
5400681ce946SMartin Matuska 		case VDEV_PROP_BYTES_FREE:
5401681ce946SMartin Matuska 		case VDEV_PROP_BYTES_CLAIM:
5402681ce946SMartin Matuska 		case VDEV_PROP_BYTES_TRIM:
5403681ce946SMartin Matuska 			if (literal) {
5404681ce946SMartin Matuska 				(void) snprintf(buf, len, "%llu",
5405681ce946SMartin Matuska 				    (u_longlong_t)intval);
5406681ce946SMartin Matuska 			} else {
5407681ce946SMartin Matuska 				(void) zfs_nicenum(intval, buf, len);
5408681ce946SMartin Matuska 			}
5409681ce946SMartin Matuska 			break;
5410681ce946SMartin Matuska 		case VDEV_PROP_EXPANDSZ:
5411681ce946SMartin Matuska 			if (intval == 0) {
5412681ce946SMartin Matuska 				(void) strlcpy(buf, "-", len);
5413681ce946SMartin Matuska 			} else if (literal) {
5414681ce946SMartin Matuska 				(void) snprintf(buf, len, "%llu",
5415681ce946SMartin Matuska 				    (u_longlong_t)intval);
5416681ce946SMartin Matuska 			} else {
5417681ce946SMartin Matuska 				(void) zfs_nicenum(intval, buf, len);
5418681ce946SMartin Matuska 			}
5419681ce946SMartin Matuska 			break;
5420681ce946SMartin Matuska 		case VDEV_PROP_CAPACITY:
5421681ce946SMartin Matuska 			if (literal) {
5422681ce946SMartin Matuska 				(void) snprintf(buf, len, "%llu",
5423681ce946SMartin Matuska 				    (u_longlong_t)intval);
5424681ce946SMartin Matuska 			} else {
5425681ce946SMartin Matuska 				(void) snprintf(buf, len, "%llu%%",
5426681ce946SMartin Matuska 				    (u_longlong_t)intval);
5427681ce946SMartin Matuska 			}
5428681ce946SMartin Matuska 			break;
542915f0b8c3SMartin Matuska 		case VDEV_PROP_CHECKSUM_N:
543015f0b8c3SMartin Matuska 		case VDEV_PROP_CHECKSUM_T:
543115f0b8c3SMartin Matuska 		case VDEV_PROP_IO_N:
543215f0b8c3SMartin Matuska 		case VDEV_PROP_IO_T:
5433e2257b31SMartin Matuska 		case VDEV_PROP_SLOW_IO_N:
5434e2257b31SMartin Matuska 		case VDEV_PROP_SLOW_IO_T:
543515f0b8c3SMartin Matuska 			if (intval == UINT64_MAX) {
543615f0b8c3SMartin Matuska 				(void) strlcpy(buf, "-", len);
543715f0b8c3SMartin Matuska 			} else {
543815f0b8c3SMartin Matuska 				(void) snprintf(buf, len, "%llu",
543915f0b8c3SMartin Matuska 				    (u_longlong_t)intval);
544015f0b8c3SMartin Matuska 			}
544115f0b8c3SMartin Matuska 			break;
5442681ce946SMartin Matuska 		case VDEV_PROP_FRAGMENTATION:
5443681ce946SMartin Matuska 			if (intval == UINT64_MAX) {
5444681ce946SMartin Matuska 				(void) strlcpy(buf, "-", len);
5445681ce946SMartin Matuska 			} else {
5446681ce946SMartin Matuska 				(void) snprintf(buf, len, "%llu%%",
5447681ce946SMartin Matuska 				    (u_longlong_t)intval);
5448681ce946SMartin Matuska 			}
5449681ce946SMartin Matuska 			break;
5450681ce946SMartin Matuska 		case VDEV_PROP_STATE:
5451681ce946SMartin Matuska 			if (literal) {
5452681ce946SMartin Matuska 				(void) snprintf(buf, len, "%llu",
5453681ce946SMartin Matuska 				    (u_longlong_t)intval);
5454681ce946SMartin Matuska 			} else {
5455681ce946SMartin Matuska 				(void) strlcpy(buf, zpool_state_to_name(intval,
5456681ce946SMartin Matuska 				    VDEV_AUX_NONE), len);
5457681ce946SMartin Matuska 			}
5458681ce946SMartin Matuska 			break;
5459681ce946SMartin Matuska 		default:
5460681ce946SMartin Matuska 			(void) snprintf(buf, len, "%llu",
5461681ce946SMartin Matuska 			    (u_longlong_t)intval);
5462681ce946SMartin Matuska 		}
5463681ce946SMartin Matuska 		break;
5464681ce946SMartin Matuska 
5465681ce946SMartin Matuska 	case PROP_TYPE_INDEX:
5466681ce946SMartin Matuska 		if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5467da5137abSMartin Matuska 			src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5468da5137abSMartin Matuska 			intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
5469681ce946SMartin Matuska 		} else {
5470ce4dcb97SMartin Matuska 			/* 'trim_support' only valid for leaf vdevs */
5471ce4dcb97SMartin Matuska 			if (prop == VDEV_PROP_TRIM_SUPPORT) {
5472ce4dcb97SMartin Matuska 				(void) strlcpy(buf, "-", len);
5473ce4dcb97SMartin Matuska 				break;
5474ce4dcb97SMartin Matuska 			}
5475681ce946SMartin Matuska 			src = ZPROP_SRC_DEFAULT;
5476681ce946SMartin Matuska 			intval = vdev_prop_default_numeric(prop);
5477e716630dSMartin Matuska 			/* Only use if provided by the RAIDZ VDEV above */
5478e716630dSMartin Matuska 			if (prop == VDEV_PROP_RAIDZ_EXPANDING)
5479e716630dSMartin Matuska 				return (ENOENT);
5480681ce946SMartin Matuska 		}
5481681ce946SMartin Matuska 		if (vdev_prop_index_to_string(prop, intval,
5482681ce946SMartin Matuska 		    (const char **)&strval) != 0)
5483681ce946SMartin Matuska 			return (-1);
5484681ce946SMartin Matuska 		(void) strlcpy(buf, strval, len);
5485681ce946SMartin Matuska 		break;
5486681ce946SMartin Matuska 
5487681ce946SMartin Matuska 	default:
5488681ce946SMartin Matuska 		abort();
5489681ce946SMartin Matuska 	}
5490681ce946SMartin Matuska 
5491681ce946SMartin Matuska 	if (srctype)
5492681ce946SMartin Matuska 		*srctype = src;
5493681ce946SMartin Matuska 
5494681ce946SMartin Matuska 	return (0);
5495681ce946SMartin Matuska }
5496681ce946SMartin Matuska 
5497681ce946SMartin Matuska /*
5498681ce946SMartin Matuska  * Get a vdev property value for 'prop_name' and return the value in
5499681ce946SMartin Matuska  * a pre-allocated buffer.
5500681ce946SMartin Matuska  */
5501681ce946SMartin Matuska int
zpool_get_vdev_prop(zpool_handle_t * zhp,const char * vdevname,vdev_prop_t prop,char * prop_name,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)5502681ce946SMartin Matuska zpool_get_vdev_prop(zpool_handle_t *zhp, const char *vdevname, vdev_prop_t prop,
5503681ce946SMartin Matuska     char *prop_name, char *buf, size_t len, zprop_source_t *srctype,
5504681ce946SMartin Matuska     boolean_t literal)
5505681ce946SMartin Matuska {
5506681ce946SMartin Matuska 	nvlist_t *reqnvl, *reqprops;
5507681ce946SMartin Matuska 	nvlist_t *retprops = NULL;
5508716fd348SMartin Matuska 	uint64_t vdev_guid = 0;
5509681ce946SMartin Matuska 	int ret;
5510681ce946SMartin Matuska 
5511681ce946SMartin Matuska 	if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5512681ce946SMartin Matuska 		return (ret);
5513681ce946SMartin Matuska 
5514681ce946SMartin Matuska 	if (nvlist_alloc(&reqnvl, NV_UNIQUE_NAME, 0) != 0)
5515681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5516681ce946SMartin Matuska 	if (nvlist_alloc(&reqprops, NV_UNIQUE_NAME, 0) != 0)
5517681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5518681ce946SMartin Matuska 
5519681ce946SMartin Matuska 	fnvlist_add_uint64(reqnvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5520681ce946SMartin Matuska 
55211f1e2261SMartin Matuska 	if (prop != VDEV_PROP_USERPROP) {
5522681ce946SMartin Matuska 		/* prop_name overrides prop value */
5523681ce946SMartin Matuska 		if (prop_name != NULL)
5524681ce946SMartin Matuska 			prop = vdev_name_to_prop(prop_name);
5525681ce946SMartin Matuska 		else
5526681ce946SMartin Matuska 			prop_name = (char *)vdev_prop_to_name(prop);
5527681ce946SMartin Matuska 		assert(prop < VDEV_NUM_PROPS);
5528681ce946SMartin Matuska 	}
5529681ce946SMartin Matuska 
5530681ce946SMartin Matuska 	assert(prop_name != NULL);
5531681ce946SMartin Matuska 	if (nvlist_add_uint64(reqprops, prop_name, prop) != 0) {
5532681ce946SMartin Matuska 		nvlist_free(reqnvl);
5533681ce946SMartin Matuska 		nvlist_free(reqprops);
5534681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5535681ce946SMartin Matuska 	}
5536681ce946SMartin Matuska 
5537681ce946SMartin Matuska 	fnvlist_add_nvlist(reqnvl, ZPOOL_VDEV_PROPS_GET_PROPS, reqprops);
5538681ce946SMartin Matuska 
5539681ce946SMartin Matuska 	ret = lzc_get_vdev_prop(zhp->zpool_name, reqnvl, &retprops);
5540681ce946SMartin Matuska 
5541681ce946SMartin Matuska 	if (ret == 0) {
5542681ce946SMartin Matuska 		ret = zpool_get_vdev_prop_value(retprops, prop, prop_name, buf,
5543681ce946SMartin Matuska 		    len, srctype, literal);
5544681ce946SMartin Matuska 	} else {
55451f1e2261SMartin Matuska 		char errbuf[ERRBUFLEN];
5546681ce946SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
5547681ce946SMartin Matuska 		    dgettext(TEXT_DOMAIN, "cannot get vdev property %s from"
5548681ce946SMartin Matuska 		    " %s in %s"), prop_name, vdevname, zhp->zpool_name);
5549681ce946SMartin Matuska 		(void) zpool_standard_error(zhp->zpool_hdl, ret, errbuf);
5550681ce946SMartin Matuska 	}
5551681ce946SMartin Matuska 
5552681ce946SMartin Matuska 	nvlist_free(reqnvl);
5553681ce946SMartin Matuska 	nvlist_free(reqprops);
5554681ce946SMartin Matuska 	nvlist_free(retprops);
5555681ce946SMartin Matuska 
5556681ce946SMartin Matuska 	return (ret);
5557681ce946SMartin Matuska }
5558681ce946SMartin Matuska 
5559681ce946SMartin Matuska /*
5560681ce946SMartin Matuska  * Get all vdev properties
5561681ce946SMartin Matuska  */
5562681ce946SMartin Matuska int
zpool_get_all_vdev_props(zpool_handle_t * zhp,const char * vdevname,nvlist_t ** outnvl)5563681ce946SMartin Matuska zpool_get_all_vdev_props(zpool_handle_t *zhp, const char *vdevname,
5564681ce946SMartin Matuska     nvlist_t **outnvl)
5565681ce946SMartin Matuska {
5566681ce946SMartin Matuska 	nvlist_t *nvl = NULL;
5567716fd348SMartin Matuska 	uint64_t vdev_guid = 0;
5568681ce946SMartin Matuska 	int ret;
5569681ce946SMartin Matuska 
5570681ce946SMartin Matuska 	if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5571681ce946SMartin Matuska 		return (ret);
5572681ce946SMartin Matuska 
5573681ce946SMartin Matuska 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5574681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5575681ce946SMartin Matuska 
5576681ce946SMartin Matuska 	fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5577681ce946SMartin Matuska 
5578681ce946SMartin Matuska 	ret = lzc_get_vdev_prop(zhp->zpool_name, nvl, outnvl);
5579681ce946SMartin Matuska 
5580681ce946SMartin Matuska 	nvlist_free(nvl);
5581681ce946SMartin Matuska 
5582681ce946SMartin Matuska 	if (ret) {
55831f1e2261SMartin Matuska 		char errbuf[ERRBUFLEN];
5584681ce946SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf),
5585681ce946SMartin Matuska 		    dgettext(TEXT_DOMAIN, "cannot get vdev properties for"
5586681ce946SMartin Matuska 		    " %s in %s"), vdevname, zhp->zpool_name);
5587681ce946SMartin Matuska 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5588681ce946SMartin Matuska 	}
5589681ce946SMartin Matuska 
5590681ce946SMartin Matuska 	return (ret);
5591681ce946SMartin Matuska }
5592681ce946SMartin Matuska 
5593681ce946SMartin Matuska /*
5594681ce946SMartin Matuska  * Set vdev property
5595681ce946SMartin Matuska  */
5596681ce946SMartin Matuska int
zpool_set_vdev_prop(zpool_handle_t * zhp,const char * vdevname,const char * propname,const char * propval)5597681ce946SMartin Matuska zpool_set_vdev_prop(zpool_handle_t *zhp, const char *vdevname,
5598681ce946SMartin Matuska     const char *propname, const char *propval)
5599681ce946SMartin Matuska {
5600681ce946SMartin Matuska 	int ret;
5601681ce946SMartin Matuska 	nvlist_t *nvl = NULL;
5602681ce946SMartin Matuska 	nvlist_t *outnvl = NULL;
5603681ce946SMartin Matuska 	nvlist_t *props;
5604681ce946SMartin Matuska 	nvlist_t *realprops;
5605681ce946SMartin Matuska 	prop_flags_t flags = { 0 };
5606681ce946SMartin Matuska 	uint64_t version;
5607681ce946SMartin Matuska 	uint64_t vdev_guid;
5608681ce946SMartin Matuska 
5609681ce946SMartin Matuska 	if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5610681ce946SMartin Matuska 		return (ret);
5611681ce946SMartin Matuska 
5612681ce946SMartin Matuska 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5613681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5614681ce946SMartin Matuska 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
5615681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5616681ce946SMartin Matuska 
5617681ce946SMartin Matuska 	fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_SET_VDEV, vdev_guid);
5618681ce946SMartin Matuska 
5619681ce946SMartin Matuska 	if (nvlist_add_string(props, propname, propval) != 0) {
5620681ce946SMartin Matuska 		nvlist_free(props);
5621681ce946SMartin Matuska 		return (no_memory(zhp->zpool_hdl));
5622681ce946SMartin Matuska 	}
5623681ce946SMartin Matuska 
56241f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
5625681ce946SMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf),
5626681ce946SMartin Matuska 	    dgettext(TEXT_DOMAIN, "cannot set property %s for %s on %s"),
5627681ce946SMartin Matuska 	    propname, vdevname, zhp->zpool_name);
5628681ce946SMartin Matuska 
5629681ce946SMartin Matuska 	flags.vdevprop = 1;
5630681ce946SMartin Matuska 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5631681ce946SMartin Matuska 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5632681ce946SMartin Matuska 	    zhp->zpool_name, props, version, flags, errbuf)) == NULL) {
5633681ce946SMartin Matuska 		nvlist_free(props);
5634681ce946SMartin Matuska 		nvlist_free(nvl);
5635681ce946SMartin Matuska 		return (-1);
5636681ce946SMartin Matuska 	}
5637681ce946SMartin Matuska 
5638681ce946SMartin Matuska 	nvlist_free(props);
5639681ce946SMartin Matuska 	props = realprops;
5640681ce946SMartin Matuska 
5641681ce946SMartin Matuska 	fnvlist_add_nvlist(nvl, ZPOOL_VDEV_PROPS_SET_PROPS, props);
5642681ce946SMartin Matuska 
5643681ce946SMartin Matuska 	ret = lzc_set_vdev_prop(zhp->zpool_name, nvl, &outnvl);
5644681ce946SMartin Matuska 
5645681ce946SMartin Matuska 	nvlist_free(props);
5646681ce946SMartin Matuska 	nvlist_free(nvl);
5647681ce946SMartin Matuska 	nvlist_free(outnvl);
5648681ce946SMartin Matuska 
5649681ce946SMartin Matuska 	if (ret)
5650681ce946SMartin Matuska 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5651681ce946SMartin Matuska 
5652681ce946SMartin Matuska 	return (ret);
5653681ce946SMartin Matuska }
5654e2df9bb4SMartin Matuska 
5655e2df9bb4SMartin Matuska /*
5656e2df9bb4SMartin Matuska  * Prune older entries from the DDT to reclaim space under the quota
5657e2df9bb4SMartin Matuska  */
5658e2df9bb4SMartin Matuska int
zpool_ddt_prune(zpool_handle_t * zhp,zpool_ddt_prune_unit_t unit,uint64_t amount)5659e2df9bb4SMartin Matuska zpool_ddt_prune(zpool_handle_t *zhp, zpool_ddt_prune_unit_t unit,
5660e2df9bb4SMartin Matuska     uint64_t amount)
5661e2df9bb4SMartin Matuska {
5662e2df9bb4SMartin Matuska 	int error = lzc_ddt_prune(zhp->zpool_name, unit, amount);
5663e2df9bb4SMartin Matuska 	if (error != 0) {
5664e2df9bb4SMartin Matuska 		libzfs_handle_t *hdl = zhp->zpool_hdl;
5665e2df9bb4SMartin Matuska 		char errbuf[ERRBUFLEN];
5666e2df9bb4SMartin Matuska 
5667e2df9bb4SMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5668e2df9bb4SMartin Matuska 		    "cannot prune dedup table on '%s'"), zhp->zpool_name);
5669e2df9bb4SMartin Matuska 
5670e2df9bb4SMartin Matuska 		if (error == EALREADY) {
5671e2df9bb4SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5672e2df9bb4SMartin Matuska 			    "a prune operation is already in progress"));
5673e2df9bb4SMartin Matuska 			(void) zfs_error(hdl, EZFS_BUSY, errbuf);
5674e2df9bb4SMartin Matuska 		} else {
5675e2df9bb4SMartin Matuska 			(void) zpool_standard_error(hdl, errno, errbuf);
5676e2df9bb4SMartin Matuska 		}
5677e2df9bb4SMartin Matuska 		return (-1);
5678e2df9bb4SMartin Matuska 	}
5679e2df9bb4SMartin Matuska 
5680e2df9bb4SMartin Matuska 	return (0);
5681e2df9bb4SMartin Matuska }
5682