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