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