xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision 7fd05ac4dec0c343d2f68f310d3718b715ecfbaf)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
252acef22dSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
26810e43b2SBill Pijewski  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <ctype.h>
30fa9e4066Sahrens #include <errno.h>
31fa9e4066Sahrens #include <devid.h>
32fa9e4066Sahrens #include <fcntl.h>
33fa9e4066Sahrens #include <libintl.h>
34fa9e4066Sahrens #include <stdio.h>
35fa9e4066Sahrens #include <stdlib.h>
36f3861e1aSahl #include <strings.h>
37fa9e4066Sahrens #include <unistd.h>
384445fffbSMatthew Ahrens #include <libgen.h>
398488aeb5Staylor #include <sys/efi_partition.h>
408488aeb5Staylor #include <sys/vtoc.h>
41fa9e4066Sahrens #include <sys/zfs_ioctl.h>
42573ca77eSGeorge Wilson #include <dlfcn.h>
43fa9e4066Sahrens 
44fa9e4066Sahrens #include "zfs_namecheck.h"
45b1b8ab34Slling #include "zfs_prop.h"
46fa9e4066Sahrens #include "libzfs_impl.h"
47468c413aSTim Haley #include "zfs_comutil.h"
48ad135b5dSChristopher Siden #include "zfeature_common.h"
49fa9e4066Sahrens 
5015e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51990b4856Slling 
52573ca77eSGeorge Wilson #define	DISK_ROOT	"/dev/dsk"
53573ca77eSGeorge Wilson #define	RDISK_ROOT	"/dev/rdsk"
54573ca77eSGeorge Wilson #define	BACKUP_SLICE	"s2"
55573ca77eSGeorge Wilson 
56f9af39baSGeorge Wilson typedef struct prop_flags {
57f9af39baSGeorge Wilson 	int create:1;	/* Validate property on creation */
58f9af39baSGeorge Wilson 	int import:1;	/* Validate property on import */
59f9af39baSGeorge Wilson } prop_flags_t;
60f9af39baSGeorge Wilson 
61990b4856Slling /*
62990b4856Slling  * ====================================================================
63990b4856Slling  *   zpool property functions
64990b4856Slling  * ====================================================================
65990b4856Slling  */
66990b4856Slling 
67990b4856Slling static int
68990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
69990b4856Slling {
70990b4856Slling 	zfs_cmd_t zc = { 0 };
71990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
72990b4856Slling 
73990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
74990b4856Slling 
75990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
76990b4856Slling 		return (-1);
77990b4856Slling 
78990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
79990b4856Slling 		if (errno == ENOMEM) {
80990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
81990b4856Slling 				zcmd_free_nvlists(&zc);
82990b4856Slling 				return (-1);
83990b4856Slling 			}
84990b4856Slling 		} else {
85990b4856Slling 			zcmd_free_nvlists(&zc);
86990b4856Slling 			return (-1);
87990b4856Slling 		}
88990b4856Slling 	}
89990b4856Slling 
90990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
91990b4856Slling 		zcmd_free_nvlists(&zc);
92990b4856Slling 		return (-1);
93990b4856Slling 	}
94990b4856Slling 
95990b4856Slling 	zcmd_free_nvlists(&zc);
96990b4856Slling 
97990b4856Slling 	return (0);
98990b4856Slling }
99990b4856Slling 
100990b4856Slling static int
101990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
102990b4856Slling {
103990b4856Slling 	nvlist_t *old_props;
104990b4856Slling 
105990b4856Slling 	old_props = zhp->zpool_props;
106990b4856Slling 
107990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
108990b4856Slling 		return (-1);
109990b4856Slling 
110990b4856Slling 	nvlist_free(old_props);
111990b4856Slling 	return (0);
112990b4856Slling }
113990b4856Slling 
114990b4856Slling static char *
115990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
116990b4856Slling     zprop_source_t *src)
117990b4856Slling {
118990b4856Slling 	nvlist_t *nv, *nvl;
119990b4856Slling 	uint64_t ival;
120990b4856Slling 	char *value;
121990b4856Slling 	zprop_source_t source;
122990b4856Slling 
123990b4856Slling 	nvl = zhp->zpool_props;
124990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
125990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
126990b4856Slling 		source = ival;
127990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
128990b4856Slling 	} else {
129990b4856Slling 		source = ZPROP_SRC_DEFAULT;
130990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
131990b4856Slling 			value = "-";
132990b4856Slling 	}
133990b4856Slling 
134990b4856Slling 	if (src)
135990b4856Slling 		*src = source;
136990b4856Slling 
137990b4856Slling 	return (value);
138990b4856Slling }
139990b4856Slling 
140990b4856Slling uint64_t
141990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
142990b4856Slling {
143990b4856Slling 	nvlist_t *nv, *nvl;
144990b4856Slling 	uint64_t value;
145990b4856Slling 	zprop_source_t source;
146990b4856Slling 
147b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
148b87f3af3Sperrin 		/*
149b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
150b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
151b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
152b87f3af3Sperrin 		 */
153b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
154b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
155b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
156b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
157b87f3af3Sperrin 		    == 0)) {
158b87f3af3Sperrin 			return (value);
159b87f3af3Sperrin 		}
160990b4856Slling 		return (zpool_prop_default_numeric(prop));
161b87f3af3Sperrin 	}
162990b4856Slling 
163990b4856Slling 	nvl = zhp->zpool_props;
164990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
165990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
166990b4856Slling 		source = value;
167990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
168990b4856Slling 	} else {
169990b4856Slling 		source = ZPROP_SRC_DEFAULT;
170990b4856Slling 		value = zpool_prop_default_numeric(prop);
171990b4856Slling 	}
172990b4856Slling 
173990b4856Slling 	if (src)
174990b4856Slling 		*src = source;
175990b4856Slling 
176990b4856Slling 	return (value);
177990b4856Slling }
178990b4856Slling 
179990b4856Slling /*
180990b4856Slling  * Map VDEV STATE to printed strings.
181990b4856Slling  */
182990b4856Slling char *
183990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
184990b4856Slling {
185990b4856Slling 	switch (state) {
186990b4856Slling 	case VDEV_STATE_CLOSED:
187990b4856Slling 	case VDEV_STATE_OFFLINE:
188990b4856Slling 		return (gettext("OFFLINE"));
189990b4856Slling 	case VDEV_STATE_REMOVED:
190990b4856Slling 		return (gettext("REMOVED"));
191990b4856Slling 	case VDEV_STATE_CANT_OPEN:
192b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
193990b4856Slling 			return (gettext("FAULTED"));
1941195e687SMark J Musante 		else if (aux == VDEV_AUX_SPLIT_POOL)
1951195e687SMark J Musante 			return (gettext("SPLIT"));
196990b4856Slling 		else
197990b4856Slling 			return (gettext("UNAVAIL"));
198990b4856Slling 	case VDEV_STATE_FAULTED:
199990b4856Slling 		return (gettext("FAULTED"));
200990b4856Slling 	case VDEV_STATE_DEGRADED:
201990b4856Slling 		return (gettext("DEGRADED"));
202990b4856Slling 	case VDEV_STATE_HEALTHY:
203990b4856Slling 		return (gettext("ONLINE"));
204990b4856Slling 	}
205990b4856Slling 
206990b4856Slling 	return (gettext("UNKNOWN"));
207990b4856Slling }
208990b4856Slling 
209990b4856Slling /*
210990b4856Slling  * Get a zpool property value for 'prop' and return the value in
211990b4856Slling  * a pre-allocated buffer.
212990b4856Slling  */
213990b4856Slling int
214990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
215c58b3526SAdam Stevko     zprop_source_t *srctype, boolean_t literal)
216990b4856Slling {
217990b4856Slling 	uint64_t intval;
218990b4856Slling 	const char *strval;
219990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
220990b4856Slling 	nvlist_t *nvroot;
221990b4856Slling 	vdev_stat_t *vs;
222990b4856Slling 	uint_t vsc;
223990b4856Slling 
224990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
225379c004dSEric Schrock 		switch (prop) {
226379c004dSEric Schrock 		case ZPOOL_PROP_NAME:
227990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
228379c004dSEric Schrock 			break;
229379c004dSEric Schrock 
230379c004dSEric Schrock 		case ZPOOL_PROP_HEALTH:
231990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
232379c004dSEric Schrock 			break;
233379c004dSEric Schrock 
234379c004dSEric Schrock 		case ZPOOL_PROP_GUID:
235379c004dSEric Schrock 			intval = zpool_get_prop_int(zhp, prop, &src);
236379c004dSEric Schrock 			(void) snprintf(buf, len, "%llu", intval);
237379c004dSEric Schrock 			break;
238379c004dSEric Schrock 
239379c004dSEric Schrock 		case ZPOOL_PROP_ALTROOT:
240379c004dSEric Schrock 		case ZPOOL_PROP_CACHEFILE:
2418704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
242379c004dSEric Schrock 			if (zhp->zpool_props != NULL ||
243379c004dSEric Schrock 			    zpool_get_all_props(zhp) == 0) {
244379c004dSEric Schrock 				(void) strlcpy(buf,
245379c004dSEric Schrock 				    zpool_get_prop_string(zhp, prop, &src),
246379c004dSEric Schrock 				    len);
247c58b3526SAdam Stevko 				break;
248379c004dSEric Schrock 			}
249379c004dSEric Schrock 			/* FALLTHROUGH */
250379c004dSEric Schrock 		default:
251990b4856Slling 			(void) strlcpy(buf, "-", len);
252379c004dSEric Schrock 			break;
253379c004dSEric Schrock 		}
254379c004dSEric Schrock 
255379c004dSEric Schrock 		if (srctype != NULL)
256379c004dSEric Schrock 			*srctype = src;
257990b4856Slling 		return (0);
258990b4856Slling 	}
259990b4856Slling 
260990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
261990b4856Slling 	    prop != ZPOOL_PROP_NAME)
262990b4856Slling 		return (-1);
263990b4856Slling 
264990b4856Slling 	switch (zpool_prop_get_type(prop)) {
265990b4856Slling 	case PROP_TYPE_STRING:
266990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
267990b4856Slling 		    len);
268990b4856Slling 		break;
269990b4856Slling 
270990b4856Slling 	case PROP_TYPE_NUMBER:
271990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
272990b4856Slling 
273990b4856Slling 		switch (prop) {
274990b4856Slling 		case ZPOOL_PROP_SIZE:
275485bbbf5SGeorge Wilson 		case ZPOOL_PROP_ALLOCATED:
276485bbbf5SGeorge Wilson 		case ZPOOL_PROP_FREE:
277ad135b5dSChristopher Siden 		case ZPOOL_PROP_FREEING:
278*7fd05ac4SMatthew Ahrens 		case ZPOOL_PROP_LEAKED:
2794263d13fSGeorge Wilson 		case ZPOOL_PROP_EXPANDSZ:
280c58b3526SAdam Stevko 			if (literal) {
281c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
282c58b3526SAdam Stevko 				    (u_longlong_t)intval);
283c58b3526SAdam Stevko 			} else {
284c58b3526SAdam Stevko 				(void) zfs_nicenum(intval, buf, len);
285c58b3526SAdam Stevko 			}
286990b4856Slling 			break;
287990b4856Slling 
288990b4856Slling 		case ZPOOL_PROP_CAPACITY:
289c58b3526SAdam Stevko 			if (literal) {
290c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
291c58b3526SAdam Stevko 				    (u_longlong_t)intval);
292c58b3526SAdam Stevko 			} else {
293c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu%%",
294c58b3526SAdam Stevko 				    (u_longlong_t)intval);
295c58b3526SAdam Stevko 			}
296990b4856Slling 			break;
297990b4856Slling 
298b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
299b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
300b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
301b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
302b24ab676SJeff Bonwick 			break;
303b24ab676SJeff Bonwick 
304990b4856Slling 		case ZPOOL_PROP_HEALTH:
305990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
306990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
307990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
3083f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3093f9d6ad7SLin Ling 			    == 0);
310990b4856Slling 
311990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
312990b4856Slling 			    vs->vs_aux), len);
313990b4856Slling 			break;
314ad135b5dSChristopher Siden 		case ZPOOL_PROP_VERSION:
315ad135b5dSChristopher Siden 			if (intval >= SPA_VERSION_FEATURES) {
316ad135b5dSChristopher Siden 				(void) snprintf(buf, len, "-");
317ad135b5dSChristopher Siden 				break;
318ad135b5dSChristopher Siden 			}
319ad135b5dSChristopher Siden 			/* FALLTHROUGH */
320990b4856Slling 		default:
321990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
322990b4856Slling 		}
323990b4856Slling 		break;
324990b4856Slling 
325990b4856Slling 	case PROP_TYPE_INDEX:
326990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
327990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
328990b4856Slling 		    != 0)
329990b4856Slling 			return (-1);
330990b4856Slling 		(void) strlcpy(buf, strval, len);
331990b4856Slling 		break;
332990b4856Slling 
333990b4856Slling 	default:
334990b4856Slling 		abort();
335990b4856Slling 	}
336990b4856Slling 
337990b4856Slling 	if (srctype)
338990b4856Slling 		*srctype = src;
339990b4856Slling 
340990b4856Slling 	return (0);
341990b4856Slling }
342990b4856Slling 
343990b4856Slling /*
344990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
345990b4856Slling  * Assuming bootfs is a valid dataset name.
346990b4856Slling  */
347990b4856Slling static boolean_t
348990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
349990b4856Slling {
350990b4856Slling 	int len = strlen(pool);
351990b4856Slling 
352fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
353990b4856Slling 		return (B_FALSE);
354990b4856Slling 
355990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
356990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
357990b4856Slling 		return (B_TRUE);
358990b4856Slling 
359990b4856Slling 	return (B_FALSE);
360990b4856Slling }
361990b4856Slling 
36215e6edf1Sgw /*
36315e6edf1Sgw  * Inspect the configuration to determine if any of the devices contain
36415e6edf1Sgw  * an EFI label.
36515e6edf1Sgw  */
36615e6edf1Sgw static boolean_t
36715e6edf1Sgw pool_uses_efi(nvlist_t *config)
36815e6edf1Sgw {
36915e6edf1Sgw 	nvlist_t **child;
37015e6edf1Sgw 	uint_t c, children;
37115e6edf1Sgw 
37215e6edf1Sgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
37315e6edf1Sgw 	    &child, &children) != 0)
37415e6edf1Sgw 		return (read_efi_label(config, NULL) >= 0);
37515e6edf1Sgw 
37615e6edf1Sgw 	for (c = 0; c < children; c++) {
37715e6edf1Sgw 		if (pool_uses_efi(child[c]))
37815e6edf1Sgw 			return (B_TRUE);
37915e6edf1Sgw 	}
38015e6edf1Sgw 	return (B_FALSE);
38115e6edf1Sgw }
38215e6edf1Sgw 
3834263d13fSGeorge Wilson boolean_t
3844263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
385b5b76fecSGeorge Wilson {
386b5b76fecSGeorge Wilson 	char bootfs[ZPOOL_MAXNAMELEN];
387b5b76fecSGeorge Wilson 
388b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
389c58b3526SAdam Stevko 	    sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
390b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
391b5b76fecSGeorge Wilson }
392b5b76fecSGeorge Wilson 
393b5b76fecSGeorge Wilson 
394990b4856Slling /*
395990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
396990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
397990b4856Slling  * specified as strings.
398990b4856Slling  */
399990b4856Slling static nvlist_t *
4000a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
401f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
402990b4856Slling {
403990b4856Slling 	nvpair_t *elem;
404990b4856Slling 	nvlist_t *retprops;
405990b4856Slling 	zpool_prop_t prop;
406990b4856Slling 	char *strval;
407990b4856Slling 	uint64_t intval;
4088704186eSDan McDonald 	char *slash, *check;
4092f8aaab3Seschrock 	struct stat64 statbuf;
41015e6edf1Sgw 	zpool_handle_t *zhp;
41115e6edf1Sgw 	nvlist_t *nvroot;
412990b4856Slling 
413990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
414990b4856Slling 		(void) no_memory(hdl);
415990b4856Slling 		return (NULL);
416990b4856Slling 	}
417990b4856Slling 
418990b4856Slling 	elem = NULL;
419990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
420990b4856Slling 		const char *propname = nvpair_name(elem);
421990b4856Slling 
422ad135b5dSChristopher Siden 		prop = zpool_name_to_prop(propname);
423ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
424ad135b5dSChristopher Siden 			int err;
425ad135b5dSChristopher Siden 			char *fname = strchr(propname, '@') + 1;
426ad135b5dSChristopher Siden 
4272acef22dSMatthew Ahrens 			err = zfeature_lookup_name(fname, NULL);
428ad135b5dSChristopher Siden 			if (err != 0) {
429ad135b5dSChristopher Siden 				ASSERT3U(err, ==, ENOENT);
430ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
431ad135b5dSChristopher Siden 				    "invalid feature '%s'"), fname);
432ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
433ad135b5dSChristopher Siden 				goto error;
434ad135b5dSChristopher Siden 			}
435ad135b5dSChristopher Siden 
436ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
437ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
438ad135b5dSChristopher Siden 				    "'%s' must be a string"), propname);
439ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
440ad135b5dSChristopher Siden 				goto error;
441ad135b5dSChristopher Siden 			}
442ad135b5dSChristopher Siden 
443ad135b5dSChristopher Siden 			(void) nvpair_value_string(elem, &strval);
444ad135b5dSChristopher Siden 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
445ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
446ad135b5dSChristopher Siden 				    "property '%s' can only be set to "
447ad135b5dSChristopher Siden 				    "'enabled'"), propname);
448ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
449ad135b5dSChristopher Siden 				goto error;
450ad135b5dSChristopher Siden 			}
451ad135b5dSChristopher Siden 
452ad135b5dSChristopher Siden 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
453ad135b5dSChristopher Siden 				(void) no_memory(hdl);
454ad135b5dSChristopher Siden 				goto error;
455ad135b5dSChristopher Siden 			}
456ad135b5dSChristopher Siden 			continue;
457ad135b5dSChristopher Siden 		}
458ad135b5dSChristopher Siden 
459990b4856Slling 		/*
460990b4856Slling 		 * Make sure this property is valid and applies to this type.
461990b4856Slling 		 */
462ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL) {
463990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
464990b4856Slling 			    "invalid property '%s'"), propname);
465990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
466990b4856Slling 			goto error;
467990b4856Slling 		}
468990b4856Slling 
469990b4856Slling 		if (zpool_prop_readonly(prop)) {
470990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
471990b4856Slling 			    "is readonly"), propname);
472990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
473990b4856Slling 			goto error;
474990b4856Slling 		}
475990b4856Slling 
476990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
477990b4856Slling 		    &strval, &intval, errbuf) != 0)
478990b4856Slling 			goto error;
479990b4856Slling 
480990b4856Slling 		/*
481990b4856Slling 		 * Perform additional checking for specific properties.
482990b4856Slling 		 */
483990b4856Slling 		switch (prop) {
484990b4856Slling 		case ZPOOL_PROP_VERSION:
485ad135b5dSChristopher Siden 			if (intval < version ||
486ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
487990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
488990b4856Slling 				    "property '%s' number %d is invalid."),
489990b4856Slling 				    propname, intval);
490990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
491990b4856Slling 				goto error;
492990b4856Slling 			}
493990b4856Slling 			break;
494990b4856Slling 
495990b4856Slling 		case ZPOOL_PROP_BOOTFS:
496f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
497990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
498990b4856Slling 				    "property '%s' cannot be set at creation "
499990b4856Slling 				    "or import time"), propname);
500990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
501990b4856Slling 				goto error;
502990b4856Slling 			}
503990b4856Slling 
504990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
505990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
506990b4856Slling 				    "pool must be upgraded to support "
507990b4856Slling 				    "'%s' property"), propname);
508990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
509990b4856Slling 				goto error;
510990b4856Slling 			}
511990b4856Slling 
512990b4856Slling 			/*
513990b4856Slling 			 * bootfs property value has to be a dataset name and
514990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
515990b4856Slling 			 */
516990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
517990b4856Slling 			    strval)) {
518990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
519990b4856Slling 				    "is an invalid name"), strval);
520990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
521990b4856Slling 				goto error;
522990b4856Slling 			}
52315e6edf1Sgw 
52415e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
52515e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52615e6edf1Sgw 				    "could not open pool '%s'"), poolname);
52715e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
52815e6edf1Sgw 				goto error;
52915e6edf1Sgw 			}
53015e6edf1Sgw 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
53115e6edf1Sgw 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
53215e6edf1Sgw 
53315e6edf1Sgw 			/*
53415e6edf1Sgw 			 * bootfs property cannot be set on a disk which has
53515e6edf1Sgw 			 * been EFI labeled.
53615e6edf1Sgw 			 */
53715e6edf1Sgw 			if (pool_uses_efi(nvroot)) {
53815e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
53915e6edf1Sgw 				    "property '%s' not supported on "
54015e6edf1Sgw 				    "EFI labeled devices"), propname);
54115e6edf1Sgw 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
54215e6edf1Sgw 				zpool_close(zhp);
54315e6edf1Sgw 				goto error;
54415e6edf1Sgw 			}
54515e6edf1Sgw 			zpool_close(zhp);
546990b4856Slling 			break;
547990b4856Slling 
5482f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
549f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
550990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551990b4856Slling 				    "property '%s' can only be set during pool "
552990b4856Slling 				    "creation or import"), propname);
553990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
554990b4856Slling 				goto error;
555990b4856Slling 			}
556990b4856Slling 
5572f8aaab3Seschrock 			if (strval[0] != '/') {
558990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5592f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
5602f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
561990b4856Slling 				goto error;
562990b4856Slling 			}
5632f8aaab3Seschrock 			break;
5642f8aaab3Seschrock 
5652f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5662f8aaab3Seschrock 			if (strval[0] == '\0')
5672f8aaab3Seschrock 				break;
5682f8aaab3Seschrock 
5692f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5702f8aaab3Seschrock 				break;
571990b4856Slling 
572990b4856Slling 			if (strval[0] != '/') {
573990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5742f8aaab3Seschrock 				    "property '%s' must be empty, an "
5752f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
576990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
577990b4856Slling 				goto error;
578990b4856Slling 			}
579990b4856Slling 
5802f8aaab3Seschrock 			slash = strrchr(strval, '/');
581990b4856Slling 
5822f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5832f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
5842f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5852f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
5862f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5872f8aaab3Seschrock 				goto error;
5882f8aaab3Seschrock 			}
589990b4856Slling 
5902f8aaab3Seschrock 			*slash = '\0';
5912f8aaab3Seschrock 
5922c32020fSeschrock 			if (strval[0] != '\0' &&
5932c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
5942c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
5952f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5962f8aaab3Seschrock 				    "'%s' is not a valid directory"),
5972f8aaab3Seschrock 				    strval);
5982f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5992f8aaab3Seschrock 				goto error;
6002f8aaab3Seschrock 			}
6012f8aaab3Seschrock 
6022f8aaab3Seschrock 			*slash = '/';
6032f8aaab3Seschrock 			break;
604f9af39baSGeorge Wilson 
6058704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
6068704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
6078704186eSDan McDonald 				if (!isprint(*check)) {
6088704186eSDan McDonald 					zfs_error_aux(hdl,
6098704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
6108704186eSDan McDonald 					    "comment may only have printable "
6118704186eSDan McDonald 					    "characters"));
6128704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
6138704186eSDan McDonald 					    errbuf);
6148704186eSDan McDonald 					goto error;
6158704186eSDan McDonald 				}
6168704186eSDan McDonald 			}
6178704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
6188704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6198704186eSDan McDonald 				    "comment must not exceed %d characters"),
6208704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
6218704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6228704186eSDan McDonald 				goto error;
6238704186eSDan McDonald 			}
6248704186eSDan McDonald 			break;
625f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
626f9af39baSGeorge Wilson 			if (!flags.import) {
627f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
628f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
629f9af39baSGeorge Wilson 				    "import time"), propname);
630f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
631f9af39baSGeorge Wilson 				goto error;
632f9af39baSGeorge Wilson 			}
633f9af39baSGeorge Wilson 			break;
634990b4856Slling 		}
635990b4856Slling 	}
636990b4856Slling 
637990b4856Slling 	return (retprops);
638990b4856Slling error:
639990b4856Slling 	nvlist_free(retprops);
640990b4856Slling 	return (NULL);
641990b4856Slling }
642990b4856Slling 
643990b4856Slling /*
644990b4856Slling  * Set zpool property : propname=propval.
645990b4856Slling  */
646990b4856Slling int
647990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
648990b4856Slling {
649990b4856Slling 	zfs_cmd_t zc = { 0 };
650990b4856Slling 	int ret = -1;
651990b4856Slling 	char errbuf[1024];
652990b4856Slling 	nvlist_t *nvl = NULL;
653990b4856Slling 	nvlist_t *realprops;
654990b4856Slling 	uint64_t version;
655f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
656990b4856Slling 
657990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
658990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
659990b4856Slling 	    zhp->zpool_name);
660990b4856Slling 
661990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
662990b4856Slling 		return (no_memory(zhp->zpool_hdl));
663990b4856Slling 
664990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
665990b4856Slling 		nvlist_free(nvl);
666990b4856Slling 		return (no_memory(zhp->zpool_hdl));
667990b4856Slling 	}
668990b4856Slling 
669990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6700a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
671f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
672990b4856Slling 		nvlist_free(nvl);
673990b4856Slling 		return (-1);
674990b4856Slling 	}
675990b4856Slling 
676990b4856Slling 	nvlist_free(nvl);
677990b4856Slling 	nvl = realprops;
678990b4856Slling 
679990b4856Slling 	/*
680990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
681990b4856Slling 	 */
682990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
683990b4856Slling 
684990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
685990b4856Slling 		nvlist_free(nvl);
686990b4856Slling 		return (-1);
687990b4856Slling 	}
688990b4856Slling 
689990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
690990b4856Slling 
691990b4856Slling 	zcmd_free_nvlists(&zc);
692990b4856Slling 	nvlist_free(nvl);
693990b4856Slling 
694990b4856Slling 	if (ret)
695990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
696990b4856Slling 	else
697990b4856Slling 		(void) zpool_props_refresh(zhp);
698990b4856Slling 
699990b4856Slling 	return (ret);
700990b4856Slling }
701990b4856Slling 
702990b4856Slling int
703990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
704990b4856Slling {
705990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
706990b4856Slling 	zprop_list_t *entry;
707990b4856Slling 	char buf[ZFS_MAXPROPLEN];
708ad135b5dSChristopher Siden 	nvlist_t *features = NULL;
709ad135b5dSChristopher Siden 	zprop_list_t **last;
710ad135b5dSChristopher Siden 	boolean_t firstexpand = (NULL == *plp);
711990b4856Slling 
712990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
713990b4856Slling 		return (-1);
714990b4856Slling 
715ad135b5dSChristopher Siden 	last = plp;
716ad135b5dSChristopher Siden 	while (*last != NULL)
717ad135b5dSChristopher Siden 		last = &(*last)->pl_next;
718ad135b5dSChristopher Siden 
719ad135b5dSChristopher Siden 	if ((*plp)->pl_all)
720ad135b5dSChristopher Siden 		features = zpool_get_features(zhp);
721ad135b5dSChristopher Siden 
722ad135b5dSChristopher Siden 	if ((*plp)->pl_all && firstexpand) {
723ad135b5dSChristopher Siden 		for (int i = 0; i < SPA_FEATURES; i++) {
724ad135b5dSChristopher Siden 			zprop_list_t *entry = zfs_alloc(hdl,
725ad135b5dSChristopher Siden 			    sizeof (zprop_list_t));
726ad135b5dSChristopher Siden 			entry->pl_prop = ZPROP_INVAL;
727ad135b5dSChristopher Siden 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
728ad135b5dSChristopher Siden 			    spa_feature_table[i].fi_uname);
729ad135b5dSChristopher Siden 			entry->pl_width = strlen(entry->pl_user_prop);
730ad135b5dSChristopher Siden 			entry->pl_all = B_TRUE;
731ad135b5dSChristopher Siden 
732ad135b5dSChristopher Siden 			*last = entry;
733ad135b5dSChristopher Siden 			last = &entry->pl_next;
734ad135b5dSChristopher Siden 		}
735ad135b5dSChristopher Siden 	}
736ad135b5dSChristopher Siden 
737ad135b5dSChristopher Siden 	/* add any unsupported features */
738ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
739ad135b5dSChristopher Siden 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
740ad135b5dSChristopher Siden 		char *propname;
741ad135b5dSChristopher Siden 		boolean_t found;
742ad135b5dSChristopher Siden 		zprop_list_t *entry;
743ad135b5dSChristopher Siden 
744ad135b5dSChristopher Siden 		if (zfeature_is_supported(nvpair_name(nvp)))
745ad135b5dSChristopher Siden 			continue;
746ad135b5dSChristopher Siden 
747ad135b5dSChristopher Siden 		propname = zfs_asprintf(hdl, "unsupported@%s",
748ad135b5dSChristopher Siden 		    nvpair_name(nvp));
749ad135b5dSChristopher Siden 
750ad135b5dSChristopher Siden 		/*
751ad135b5dSChristopher Siden 		 * Before adding the property to the list make sure that no
752ad135b5dSChristopher Siden 		 * other pool already added the same property.
753ad135b5dSChristopher Siden 		 */
754ad135b5dSChristopher Siden 		found = B_FALSE;
755ad135b5dSChristopher Siden 		entry = *plp;
756ad135b5dSChristopher Siden 		while (entry != NULL) {
757ad135b5dSChristopher Siden 			if (entry->pl_user_prop != NULL &&
758ad135b5dSChristopher Siden 			    strcmp(propname, entry->pl_user_prop) == 0) {
759ad135b5dSChristopher Siden 				found = B_TRUE;
760ad135b5dSChristopher Siden 				break;
761ad135b5dSChristopher Siden 			}
762ad135b5dSChristopher Siden 			entry = entry->pl_next;
763ad135b5dSChristopher Siden 		}
764ad135b5dSChristopher Siden 		if (found) {
765ad135b5dSChristopher Siden 			free(propname);
766ad135b5dSChristopher Siden 			continue;
767ad135b5dSChristopher Siden 		}
768ad135b5dSChristopher Siden 
769ad135b5dSChristopher Siden 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
770ad135b5dSChristopher Siden 		entry->pl_prop = ZPROP_INVAL;
771ad135b5dSChristopher Siden 		entry->pl_user_prop = propname;
772ad135b5dSChristopher Siden 		entry->pl_width = strlen(entry->pl_user_prop);
773ad135b5dSChristopher Siden 		entry->pl_all = B_TRUE;
774ad135b5dSChristopher Siden 
775ad135b5dSChristopher Siden 		*last = entry;
776ad135b5dSChristopher Siden 		last = &entry->pl_next;
777ad135b5dSChristopher Siden 	}
778ad135b5dSChristopher Siden 
779990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
780990b4856Slling 
781990b4856Slling 		if (entry->pl_fixed)
782990b4856Slling 			continue;
783990b4856Slling 
784990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
785990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
786c58b3526SAdam Stevko 		    NULL, B_FALSE) == 0) {
787990b4856Slling 			if (strlen(buf) > entry->pl_width)
788990b4856Slling 				entry->pl_width = strlen(buf);
789990b4856Slling 		}
790990b4856Slling 	}
791990b4856Slling 
792990b4856Slling 	return (0);
793990b4856Slling }
794990b4856Slling 
795ad135b5dSChristopher Siden /*
796ad135b5dSChristopher Siden  * Get the state for the given feature on the given ZFS pool.
797ad135b5dSChristopher Siden  */
798ad135b5dSChristopher Siden int
799ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
800ad135b5dSChristopher Siden     size_t len)
801ad135b5dSChristopher Siden {
802ad135b5dSChristopher Siden 	uint64_t refcount;
803ad135b5dSChristopher Siden 	boolean_t found = B_FALSE;
804ad135b5dSChristopher Siden 	nvlist_t *features = zpool_get_features(zhp);
805ad135b5dSChristopher Siden 	boolean_t supported;
806ad135b5dSChristopher Siden 	const char *feature = strchr(propname, '@') + 1;
807ad135b5dSChristopher Siden 
808ad135b5dSChristopher Siden 	supported = zpool_prop_feature(propname);
809ad135b5dSChristopher Siden 	ASSERT(supported || zfs_prop_unsupported(propname));
810ad135b5dSChristopher Siden 
811ad135b5dSChristopher Siden 	/*
812ad135b5dSChristopher Siden 	 * Convert from feature name to feature guid. This conversion is
813ad135b5dSChristopher Siden 	 * unecessary for unsupported@... properties because they already
814ad135b5dSChristopher Siden 	 * use guids.
815ad135b5dSChristopher Siden 	 */
816ad135b5dSChristopher Siden 	if (supported) {
817ad135b5dSChristopher Siden 		int ret;
8182acef22dSMatthew Ahrens 		spa_feature_t fid;
819ad135b5dSChristopher Siden 
8202acef22dSMatthew Ahrens 		ret = zfeature_lookup_name(feature, &fid);
821ad135b5dSChristopher Siden 		if (ret != 0) {
822ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
823ad135b5dSChristopher Siden 			return (ENOTSUP);
824ad135b5dSChristopher Siden 		}
8252acef22dSMatthew Ahrens 		feature = spa_feature_table[fid].fi_guid;
826ad135b5dSChristopher Siden 	}
827ad135b5dSChristopher Siden 
828ad135b5dSChristopher Siden 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
829ad135b5dSChristopher Siden 		found = B_TRUE;
830ad135b5dSChristopher Siden 
831ad135b5dSChristopher Siden 	if (supported) {
832ad135b5dSChristopher Siden 		if (!found) {
833ad135b5dSChristopher Siden 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
834ad135b5dSChristopher Siden 		} else  {
835ad135b5dSChristopher Siden 			if (refcount == 0)
836ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
837ad135b5dSChristopher Siden 			else
838ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
839ad135b5dSChristopher Siden 		}
840ad135b5dSChristopher Siden 	} else {
841ad135b5dSChristopher Siden 		if (found) {
842ad135b5dSChristopher Siden 			if (refcount == 0) {
843ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
844ad135b5dSChristopher Siden 			} else {
845ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
846ad135b5dSChristopher Siden 			}
847ad135b5dSChristopher Siden 		} else {
848ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
849ad135b5dSChristopher Siden 			return (ENOTSUP);
850ad135b5dSChristopher Siden 		}
851ad135b5dSChristopher Siden 	}
852ad135b5dSChristopher Siden 
853ad135b5dSChristopher Siden 	return (0);
854ad135b5dSChristopher Siden }
855990b4856Slling 
856573ca77eSGeorge Wilson /*
857573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
858573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
859573ca77eSGeorge Wilson  */
860573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
861573ca77eSGeorge Wilson 
862fa9e4066Sahrens /*
863fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
864fa9e4066Sahrens  * 'buf'.
865fa9e4066Sahrens  */
866e7cbe64fSgw boolean_t
86799653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
868fa9e4066Sahrens {
869fa9e4066Sahrens 	namecheck_err_t why;
870fa9e4066Sahrens 	char what;
871b468a217Seschrock 	int ret;
872b468a217Seschrock 
873b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
874b468a217Seschrock 
875b468a217Seschrock 	/*
876b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
877b468a217Seschrock 	 * But we need to support users with existing pools that may now be
878b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
879b468a217Seschrock 	 * create (or import), and only in userland.
880b468a217Seschrock 	 */
881b468a217Seschrock 	if (ret == 0 && !isopen &&
882b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
883b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
8848654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
8858654d025Sperrin 	    strcmp(pool, "log") == 0)) {
886e7cbe64fSgw 		if (hdl != NULL)
887e7cbe64fSgw 			zfs_error_aux(hdl,
888e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
88999653d4eSeschrock 		return (B_FALSE);
890b468a217Seschrock 	}
891b468a217Seschrock 
892fa9e4066Sahrens 
893b468a217Seschrock 	if (ret != 0) {
89499653d4eSeschrock 		if (hdl != NULL) {
895fa9e4066Sahrens 			switch (why) {
896b81d61a6Slling 			case NAME_ERR_TOOLONG:
89799653d4eSeschrock 				zfs_error_aux(hdl,
898b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
899b81d61a6Slling 				break;
900b81d61a6Slling 
901fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
90299653d4eSeschrock 				zfs_error_aux(hdl,
903fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
904fa9e4066Sahrens 				    "'%c' in pool name"), what);
905fa9e4066Sahrens 				break;
906fa9e4066Sahrens 
907fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
90899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90999653d4eSeschrock 				    "name must begin with a letter"));
910fa9e4066Sahrens 				break;
911fa9e4066Sahrens 
912fa9e4066Sahrens 			case NAME_ERR_RESERVED:
91399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
91499653d4eSeschrock 				    "name is reserved"));
915fa9e4066Sahrens 				break;
916fa9e4066Sahrens 
917fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
91899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
91999653d4eSeschrock 				    "pool name is reserved"));
920fa9e4066Sahrens 				break;
9215ad82045Snd 
9225ad82045Snd 			case NAME_ERR_LEADING_SLASH:
9235ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9245ad82045Snd 				    "leading slash in name"));
9255ad82045Snd 				break;
9265ad82045Snd 
9275ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
9285ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9295ad82045Snd 				    "empty component in name"));
9305ad82045Snd 				break;
9315ad82045Snd 
9325ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
9335ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9345ad82045Snd 				    "trailing slash in name"));
9355ad82045Snd 				break;
9365ad82045Snd 
9375ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
9385ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9395ad82045Snd 				    "multiple '@' delimiters in name"));
9405ad82045Snd 				break;
9415ad82045Snd 
942fa9e4066Sahrens 			}
943fa9e4066Sahrens 		}
94499653d4eSeschrock 		return (B_FALSE);
945fa9e4066Sahrens 	}
946fa9e4066Sahrens 
94799653d4eSeschrock 	return (B_TRUE);
948fa9e4066Sahrens }
949fa9e4066Sahrens 
950fa9e4066Sahrens /*
951fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
952fa9e4066Sahrens  * state.
953fa9e4066Sahrens  */
954fa9e4066Sahrens zpool_handle_t *
95599653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
956fa9e4066Sahrens {
957fa9e4066Sahrens 	zpool_handle_t *zhp;
95894de1d4cSeschrock 	boolean_t missing;
959fa9e4066Sahrens 
960fa9e4066Sahrens 	/*
961fa9e4066Sahrens 	 * Make sure the pool name is valid.
962fa9e4066Sahrens 	 */
96399653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
964ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
96599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
96699653d4eSeschrock 		    pool);
967fa9e4066Sahrens 		return (NULL);
968fa9e4066Sahrens 	}
969fa9e4066Sahrens 
97099653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
97199653d4eSeschrock 		return (NULL);
972fa9e4066Sahrens 
97399653d4eSeschrock 	zhp->zpool_hdl = hdl;
974fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
975fa9e4066Sahrens 
97694de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
97794de1d4cSeschrock 		zpool_close(zhp);
97894de1d4cSeschrock 		return (NULL);
97994de1d4cSeschrock 	}
98094de1d4cSeschrock 
98194de1d4cSeschrock 	if (missing) {
982990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
983ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
984990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
98594de1d4cSeschrock 		zpool_close(zhp);
98694de1d4cSeschrock 		return (NULL);
987fa9e4066Sahrens 	}
988fa9e4066Sahrens 
989fa9e4066Sahrens 	return (zhp);
990fa9e4066Sahrens }
991fa9e4066Sahrens 
992fa9e4066Sahrens /*
993fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
994fa9e4066Sahrens  * the configuration cache may be out of date).
995fa9e4066Sahrens  */
99694de1d4cSeschrock int
99794de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
998fa9e4066Sahrens {
999fa9e4066Sahrens 	zpool_handle_t *zhp;
100094de1d4cSeschrock 	boolean_t missing;
1001fa9e4066Sahrens 
100294de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
100394de1d4cSeschrock 		return (-1);
1004fa9e4066Sahrens 
100599653d4eSeschrock 	zhp->zpool_hdl = hdl;
1006fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1007fa9e4066Sahrens 
100894de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
100994de1d4cSeschrock 		zpool_close(zhp);
101094de1d4cSeschrock 		return (-1);
1011fa9e4066Sahrens 	}
1012fa9e4066Sahrens 
101394de1d4cSeschrock 	if (missing) {
101494de1d4cSeschrock 		zpool_close(zhp);
101594de1d4cSeschrock 		*ret = NULL;
101694de1d4cSeschrock 		return (0);
101794de1d4cSeschrock 	}
101894de1d4cSeschrock 
101994de1d4cSeschrock 	*ret = zhp;
102094de1d4cSeschrock 	return (0);
1021fa9e4066Sahrens }
1022fa9e4066Sahrens 
1023fa9e4066Sahrens /*
1024fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1025fa9e4066Sahrens  * state.
1026fa9e4066Sahrens  */
1027fa9e4066Sahrens zpool_handle_t *
102899653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1029fa9e4066Sahrens {
1030fa9e4066Sahrens 	zpool_handle_t *zhp;
1031fa9e4066Sahrens 
103299653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1033fa9e4066Sahrens 		return (NULL);
1034fa9e4066Sahrens 
1035fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1036ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
103799653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1038fa9e4066Sahrens 		zpool_close(zhp);
1039fa9e4066Sahrens 		return (NULL);
1040fa9e4066Sahrens 	}
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 	return (zhp);
1043fa9e4066Sahrens }
1044fa9e4066Sahrens 
1045fa9e4066Sahrens /*
1046fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
1047fa9e4066Sahrens  */
1048fa9e4066Sahrens void
1049fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1050fa9e4066Sahrens {
1051fa9e4066Sahrens 	if (zhp->zpool_config)
1052fa9e4066Sahrens 		nvlist_free(zhp->zpool_config);
1053088e9d47Seschrock 	if (zhp->zpool_old_config)
1054088e9d47Seschrock 		nvlist_free(zhp->zpool_old_config);
1055b1b8ab34Slling 	if (zhp->zpool_props)
1056b1b8ab34Slling 		nvlist_free(zhp->zpool_props);
1057fa9e4066Sahrens 	free(zhp);
1058fa9e4066Sahrens }
1059fa9e4066Sahrens 
1060fa9e4066Sahrens /*
1061fa9e4066Sahrens  * Return the name of the pool.
1062fa9e4066Sahrens  */
1063fa9e4066Sahrens const char *
1064fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1065fa9e4066Sahrens {
1066fa9e4066Sahrens 	return (zhp->zpool_name);
1067fa9e4066Sahrens }
1068fa9e4066Sahrens 
1069fa9e4066Sahrens 
1070fa9e4066Sahrens /*
1071fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1072fa9e4066Sahrens  */
1073fa9e4066Sahrens int
1074fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1075fa9e4066Sahrens {
1076fa9e4066Sahrens 	return (zhp->zpool_state);
1077fa9e4066Sahrens }
1078fa9e4066Sahrens 
1079fa9e4066Sahrens /*
1080fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
1081fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
1082fa9e4066Sahrens  * don't have to worry about error semantics.
1083fa9e4066Sahrens  */
1084fa9e4066Sahrens int
108599653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
10860a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
1087fa9e4066Sahrens {
1088fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
10890a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
10900a48a24eStimh 	nvlist_t *zc_props = NULL;
109199653d4eSeschrock 	char msg[1024];
10920a48a24eStimh 	int ret = -1;
1093fa9e4066Sahrens 
109499653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
109599653d4eSeschrock 	    "cannot create '%s'"), pool);
1096fa9e4066Sahrens 
109799653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
109899653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1099fa9e4066Sahrens 
1100351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1101990b4856Slling 		return (-1);
1102fa9e4066Sahrens 
11030a48a24eStimh 	if (props) {
1104f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1105f9af39baSGeorge Wilson 
11060a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1107f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
11080a48a24eStimh 			goto create_failed;
11090a48a24eStimh 		}
11100a48a24eStimh 	}
111199653d4eSeschrock 
11120a48a24eStimh 	if (fsprops) {
11130a48a24eStimh 		uint64_t zoned;
11140a48a24eStimh 		char *zonestr;
11150a48a24eStimh 
11160a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
11170a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
11180a48a24eStimh 		    strcmp(zonestr, "on") == 0);
11190a48a24eStimh 
11200a48a24eStimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
11210a48a24eStimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
11220a48a24eStimh 			goto create_failed;
11230a48a24eStimh 		}
11240a48a24eStimh 		if (!zc_props &&
11250a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
11260a48a24eStimh 			goto create_failed;
11270a48a24eStimh 		}
11280a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
11290a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
11300a48a24eStimh 			goto create_failed;
11310a48a24eStimh 		}
1132351420b3Slling 	}
1133fa9e4066Sahrens 
11340a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
11350a48a24eStimh 		goto create_failed;
11360a48a24eStimh 
1137990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1138fa9e4066Sahrens 
11390a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1140351420b3Slling 
1141e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
11420a48a24eStimh 		nvlist_free(zc_props);
11430a48a24eStimh 		nvlist_free(zc_fsprops);
1144fa9e4066Sahrens 
114599653d4eSeschrock 		switch (errno) {
1146fa9e4066Sahrens 		case EBUSY:
1147fa9e4066Sahrens 			/*
1148fa9e4066Sahrens 			 * This can happen if the user has specified the same
1149fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1150fa9e4066Sahrens 			 * until we try to add it and see we already have a
1151fa9e4066Sahrens 			 * label.
1152fa9e4066Sahrens 			 */
115399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115499653d4eSeschrock 			    "one or more vdevs refer to the same device"));
115599653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1156fa9e4066Sahrens 
1157fa9e4066Sahrens 		case EOVERFLOW:
1158fa9e4066Sahrens 			/*
115999653d4eSeschrock 			 * This occurs when one of the devices is below
1160fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1161fa9e4066Sahrens 			 * device was the problem device since there's no
1162fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1163fa9e4066Sahrens 			 */
1164fa9e4066Sahrens 			{
1165fa9e4066Sahrens 				char buf[64];
1166fa9e4066Sahrens 
1167fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1168fa9e4066Sahrens 
116999653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
117099653d4eSeschrock 				    "one or more devices is less than the "
117199653d4eSeschrock 				    "minimum size (%s)"), buf);
1172fa9e4066Sahrens 			}
117399653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1174fa9e4066Sahrens 
1175fa9e4066Sahrens 		case ENOSPC:
117699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
117799653d4eSeschrock 			    "one or more devices is out of space"));
117899653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1179fa9e4066Sahrens 
1180fa94a07fSbrendan 		case ENOTBLK:
1181fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1182fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1183fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1184fa94a07fSbrendan 
1185fa9e4066Sahrens 		default:
118699653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1187fa9e4066Sahrens 		}
1188fa9e4066Sahrens 	}
1189fa9e4066Sahrens 
11900a48a24eStimh create_failed:
1191351420b3Slling 	zcmd_free_nvlists(&zc);
11920a48a24eStimh 	nvlist_free(zc_props);
11930a48a24eStimh 	nvlist_free(zc_fsprops);
11940a48a24eStimh 	return (ret);
1195fa9e4066Sahrens }
1196fa9e4066Sahrens 
1197fa9e4066Sahrens /*
1198fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1199fa9e4066Sahrens  * datasets left in the pool.
1200fa9e4066Sahrens  */
1201fa9e4066Sahrens int
12024445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1203fa9e4066Sahrens {
1204fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1205fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
120699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
120799653d4eSeschrock 	char msg[1024];
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1210cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1211fa9e4066Sahrens 		return (-1);
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
12144445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1215fa9e4066Sahrens 
1216cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
121799653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
121899653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1219fa9e4066Sahrens 
122099653d4eSeschrock 		if (errno == EROFS) {
122199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
122299653d4eSeschrock 			    "one or more devices is read only"));
122399653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
122499653d4eSeschrock 		} else {
122599653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1226fa9e4066Sahrens 		}
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 		if (zfp)
1229fa9e4066Sahrens 			zfs_close(zfp);
1230fa9e4066Sahrens 		return (-1);
1231fa9e4066Sahrens 	}
1232fa9e4066Sahrens 
1233fa9e4066Sahrens 	if (zfp) {
1234fa9e4066Sahrens 		remove_mountpoint(zfp);
1235fa9e4066Sahrens 		zfs_close(zfp);
1236fa9e4066Sahrens 	}
1237fa9e4066Sahrens 
1238fa9e4066Sahrens 	return (0);
1239fa9e4066Sahrens }
1240fa9e4066Sahrens 
1241fa9e4066Sahrens /*
1242fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1243fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1244fa9e4066Sahrens  */
1245fa9e4066Sahrens int
1246fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1247fa9e4066Sahrens {
1248e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
124999653d4eSeschrock 	int ret;
125099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
125199653d4eSeschrock 	char msg[1024];
1252fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1253fa94a07fSbrendan 	uint_t nspares, nl2cache;
125499653d4eSeschrock 
125599653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
125699653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
125799653d4eSeschrock 
1258fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1259fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
126099653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
126199653d4eSeschrock 	    &spares, &nspares) == 0) {
126299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
126399653d4eSeschrock 		    "upgraded to add hot spares"));
126499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
126599653d4eSeschrock 	}
1266fa9e4066Sahrens 
12674263d13fSGeorge Wilson 	if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1268b5b76fecSGeorge Wilson 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1269b5b76fecSGeorge Wilson 		uint64_t s;
1270b5b76fecSGeorge Wilson 
1271b5b76fecSGeorge Wilson 		for (s = 0; s < nspares; s++) {
1272b5b76fecSGeorge Wilson 			char *path;
1273b5b76fecSGeorge Wilson 
1274b5b76fecSGeorge Wilson 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1275b5b76fecSGeorge Wilson 			    &path) == 0 && pool_uses_efi(spares[s])) {
1276b5b76fecSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277b5b76fecSGeorge Wilson 				    "device '%s' contains an EFI label and "
1278b5b76fecSGeorge Wilson 				    "cannot be used on root pools."),
127988ecc943SGeorge Wilson 				    zpool_vdev_name(hdl, NULL, spares[s],
128088ecc943SGeorge Wilson 				    B_FALSE));
1281b5b76fecSGeorge Wilson 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1282b5b76fecSGeorge Wilson 			}
1283b5b76fecSGeorge Wilson 		}
1284b5b76fecSGeorge Wilson 	}
1285b5b76fecSGeorge Wilson 
1286fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1287fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1288fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1289fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1290fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1291fa94a07fSbrendan 		    "upgraded to add cache devices"));
1292fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1293fa94a07fSbrendan 	}
1294fa94a07fSbrendan 
1295990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
129699653d4eSeschrock 		return (-1);
1297fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1298fa9e4066Sahrens 
1299cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1300fa9e4066Sahrens 		switch (errno) {
1301fa9e4066Sahrens 		case EBUSY:
1302fa9e4066Sahrens 			/*
1303fa9e4066Sahrens 			 * This can happen if the user has specified the same
1304fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1305fa9e4066Sahrens 			 * until we try to add it and see we already have a
1306fa9e4066Sahrens 			 * label.
1307fa9e4066Sahrens 			 */
130899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
130999653d4eSeschrock 			    "one or more vdevs refer to the same device"));
131099653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1311fa9e4066Sahrens 			break;
1312fa9e4066Sahrens 
1313fa9e4066Sahrens 		case EOVERFLOW:
1314fa9e4066Sahrens 			/*
1315fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1316fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1317fa9e4066Sahrens 			 * device was the problem device since there's no
1318fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1319fa9e4066Sahrens 			 */
1320fa9e4066Sahrens 			{
1321fa9e4066Sahrens 				char buf[64];
1322fa9e4066Sahrens 
1323fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1324fa9e4066Sahrens 
132599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132699653d4eSeschrock 				    "device is less than the minimum "
132799653d4eSeschrock 				    "size (%s)"), buf);
1328fa9e4066Sahrens 			}
132999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
133099653d4eSeschrock 			break;
133199653d4eSeschrock 
133299653d4eSeschrock 		case ENOTSUP:
133399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13348654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
133599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1336fa9e4066Sahrens 			break;
1337fa9e4066Sahrens 
1338b1b8ab34Slling 		case EDOM:
1339b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13408654d025Sperrin 			    "root pool can not have multiple vdevs"
13418654d025Sperrin 			    " or separate logs"));
1342b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1343b1b8ab34Slling 			break;
1344b1b8ab34Slling 
1345fa94a07fSbrendan 		case ENOTBLK:
1346fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1347fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1348fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1349fa94a07fSbrendan 			break;
1350fa94a07fSbrendan 
1351fa9e4066Sahrens 		default:
135299653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1353fa9e4066Sahrens 		}
1354fa9e4066Sahrens 
135599653d4eSeschrock 		ret = -1;
135699653d4eSeschrock 	} else {
135799653d4eSeschrock 		ret = 0;
1358fa9e4066Sahrens 	}
1359fa9e4066Sahrens 
1360e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1361fa9e4066Sahrens 
136299653d4eSeschrock 	return (ret);
1363fa9e4066Sahrens }
1364fa9e4066Sahrens 
1365fa9e4066Sahrens /*
1366fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1367fa9e4066Sahrens  * mounted datasets in the pool.
1368fa9e4066Sahrens  */
13694445fffbSMatthew Ahrens static int
13704445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
13714445fffbSMatthew Ahrens     const char *log_str)
1372fa9e4066Sahrens {
1373fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
137489a89ebfSlling 	char msg[1024];
1375fa9e4066Sahrens 
137689a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
137789a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
137889a89ebfSlling 
1379fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
138089a89ebfSlling 	zc.zc_cookie = force;
1381394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
13824445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
138389a89ebfSlling 
138489a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
138589a89ebfSlling 		switch (errno) {
138689a89ebfSlling 		case EXDEV:
138789a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
138889a89ebfSlling 			    "use '-f' to override the following errors:\n"
138989a89ebfSlling 			    "'%s' has an active shared spare which could be"
139089a89ebfSlling 			    " used by other pools once '%s' is exported."),
139189a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
139289a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
139389a89ebfSlling 			    msg));
139489a89ebfSlling 		default:
139589a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
139689a89ebfSlling 			    msg));
139789a89ebfSlling 		}
139889a89ebfSlling 	}
1399fa9e4066Sahrens 
1400fa9e4066Sahrens 	return (0);
1401fa9e4066Sahrens }
1402fa9e4066Sahrens 
1403394ab0cbSGeorge Wilson int
14044445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1405394ab0cbSGeorge Wilson {
14064445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1407394ab0cbSGeorge Wilson }
1408394ab0cbSGeorge Wilson 
1409394ab0cbSGeorge Wilson int
14104445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1411394ab0cbSGeorge Wilson {
14124445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1413394ab0cbSGeorge Wilson }
1414394ab0cbSGeorge Wilson 
1415468c413aSTim Haley static void
1416468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
14174b964adaSGeorge Wilson     nvlist_t *config)
1418468c413aSTim Haley {
14194b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1420468c413aSTim Haley 	uint64_t rewindto;
1421468c413aSTim Haley 	int64_t loss = -1;
1422468c413aSTim Haley 	struct tm t;
1423468c413aSTim Haley 	char timestr[128];
1424468c413aSTim Haley 
14254b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
14264b964adaSGeorge Wilson 		return;
14274b964adaSGeorge Wilson 
1428ad135b5dSChristopher Siden 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1429ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1430468c413aSTim Haley 		return;
1431ad135b5dSChristopher Siden 	}
1432468c413aSTim Haley 
14334b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1434468c413aSTim Haley 		return;
14354b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1436468c413aSTim Haley 
1437468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1438468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1439468c413aSTim Haley 		if (dryrun) {
1440468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1441468c413aSTim Haley 			    "Would be able to return %s "
1442468c413aSTim Haley 			    "to its state as of %s.\n"),
1443468c413aSTim Haley 			    name, timestr);
1444468c413aSTim Haley 		} else {
1445468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1446468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1447468c413aSTim Haley 			    name, timestr);
1448468c413aSTim Haley 		}
1449468c413aSTim Haley 		if (loss > 120) {
1450468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1451468c413aSTim Haley 			    "%s approximately %lld "),
1452468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1453468c413aSTim Haley 			    (loss + 30) / 60);
1454468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1455468c413aSTim Haley 			    "minutes of transactions.\n"));
1456468c413aSTim Haley 		} else if (loss > 0) {
1457468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1458468c413aSTim Haley 			    "%s approximately %lld "),
1459468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1460468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1461468c413aSTim Haley 			    "seconds of transactions.\n"));
1462468c413aSTim Haley 		}
1463468c413aSTim Haley 	}
1464468c413aSTim Haley }
1465468c413aSTim Haley 
1466468c413aSTim Haley void
1467468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1468468c413aSTim Haley     nvlist_t *config)
1469468c413aSTim Haley {
14704b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1471468c413aSTim Haley 	int64_t loss = -1;
1472468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1473468c413aSTim Haley 	uint64_t rewindto;
1474468c413aSTim Haley 	struct tm t;
1475468c413aSTim Haley 	char timestr[128];
1476468c413aSTim Haley 
1477468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1478468c413aSTim Haley 		return;
1479468c413aSTim Haley 
1480468c413aSTim Haley 	if (reason >= 0)
1481468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1482468c413aSTim Haley 	else
1483468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1484468c413aSTim Haley 
1485468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
14864b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1487ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
14884b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1489468c413aSTim Haley 		goto no_info;
1490468c413aSTim Haley 
14914b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
14924b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1493468c413aSTim Haley 	    &edata);
1494468c413aSTim Haley 
1495468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1496468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1497468c413aSTim Haley 
1498468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1499468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1500468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1501468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1502468c413aSTim Haley 		    "\tshould correct the problem.  "),
1503468c413aSTim Haley 		    timestr);
1504468c413aSTim Haley 	} else {
1505468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1506468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1507468c413aSTim Haley 		    "should correct the problem.\n\t"));
1508468c413aSTim Haley 	}
1509468c413aSTim Haley 
1510468c413aSTim Haley 	if (loss > 120) {
1511468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1512468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1513468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1514468c413aSTim Haley 	} else if (loss > 0) {
1515468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1516468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1517468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1518468c413aSTim Haley 	}
1519468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1520468c413aSTim Haley 		if (edata == 1) {
1521468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1522468c413aSTim Haley 			    "After rewind, at least\n"
1523468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1524468c413aSTim Haley 		} else {
1525468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1526468c413aSTim Haley 			    "After rewind, several\n"
1527468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1528468c413aSTim Haley 		}
1529468c413aSTim Haley 	}
1530468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1531a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1532a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1533468c413aSTim Haley 
1534468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1535468c413aSTim Haley 	    "A scrub of the pool\n"
1536468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1537468c413aSTim Haley 	return;
1538468c413aSTim Haley 
1539468c413aSTim Haley no_info:
1540468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1541468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1542468c413aSTim Haley }
1543468c413aSTim Haley 
1544fa9e4066Sahrens /*
1545990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1546990b4856Slling  * if possible.
1547990b4856Slling  *
1548990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1549990b4856Slling  * new properties value to be set.
1550fa9e4066Sahrens  */
1551fa9e4066Sahrens int
155299653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1553990b4856Slling     char *altroot)
1554990b4856Slling {
1555990b4856Slling 	nvlist_t *props = NULL;
1556990b4856Slling 	int ret;
1557990b4856Slling 
1558990b4856Slling 	if (altroot != NULL) {
1559990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1560990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1561990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1562990b4856Slling 			    newname));
1563990b4856Slling 		}
1564990b4856Slling 
1565990b4856Slling 		if (nvlist_add_string(props,
1566352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1567352d8027SGeorge Wilson 		    nvlist_add_string(props,
1568352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1569990b4856Slling 			nvlist_free(props);
1570990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1571990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1572990b4856Slling 			    newname));
1573990b4856Slling 		}
1574990b4856Slling 	}
1575990b4856Slling 
15764b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
15774b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1578990b4856Slling 	if (props)
1579990b4856Slling 		nvlist_free(props);
1580990b4856Slling 	return (ret);
1581990b4856Slling }
1582990b4856Slling 
15834b964adaSGeorge Wilson static void
15844b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
15854b964adaSGeorge Wilson     int indent)
15864b964adaSGeorge Wilson {
15874b964adaSGeorge Wilson 	nvlist_t **child;
15884b964adaSGeorge Wilson 	uint_t c, children;
15894b964adaSGeorge Wilson 	char *vname;
15904b964adaSGeorge Wilson 	uint64_t is_log = 0;
15914b964adaSGeorge Wilson 
15924b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
15934b964adaSGeorge Wilson 	    &is_log);
15944b964adaSGeorge Wilson 
15954b964adaSGeorge Wilson 	if (name != NULL)
15964b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
15974b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
15984b964adaSGeorge Wilson 
15994b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
16004b964adaSGeorge Wilson 	    &child, &children) != 0)
16014b964adaSGeorge Wilson 		return;
16024b964adaSGeorge Wilson 
16034b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
16044b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
16054b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
16064b964adaSGeorge Wilson 		free(vname);
16074b964adaSGeorge Wilson 	}
16084b964adaSGeorge Wilson }
16094b964adaSGeorge Wilson 
1610ad135b5dSChristopher Siden void
1611ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1612ad135b5dSChristopher Siden {
1613ad135b5dSChristopher Siden 	nvlist_t *nvinfo, *unsup_feat;
1614ad135b5dSChristopher Siden 
1615ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1616ad135b5dSChristopher Siden 	    0);
1617ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1618ad135b5dSChristopher Siden 	    &unsup_feat) == 0);
1619ad135b5dSChristopher Siden 
1620ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1621ad135b5dSChristopher Siden 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1622ad135b5dSChristopher Siden 		char *desc;
1623ad135b5dSChristopher Siden 
1624ad135b5dSChristopher Siden 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1625ad135b5dSChristopher Siden 		verify(nvpair_value_string(nvp, &desc) == 0);
1626ad135b5dSChristopher Siden 
1627ad135b5dSChristopher Siden 		if (strlen(desc) > 0)
1628ad135b5dSChristopher Siden 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1629ad135b5dSChristopher Siden 		else
1630ad135b5dSChristopher Siden 			(void) printf("\t%s\n", nvpair_name(nvp));
1631ad135b5dSChristopher Siden 	}
1632ad135b5dSChristopher Siden }
1633ad135b5dSChristopher Siden 
1634990b4856Slling /*
1635990b4856Slling  * Import the given pool using the known configuration and a list of
1636990b4856Slling  * properties to be set. The configuration should have come from
1637990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1638990b4856Slling  * is imported with a different name.
1639990b4856Slling  */
1640990b4856Slling int
1641990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
16424b964adaSGeorge Wilson     nvlist_t *props, int flags)
1643fa9e4066Sahrens {
1644e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1645468c413aSTim Haley 	zpool_rewind_policy_t policy;
16464b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
16474b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
16484b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1649fa9e4066Sahrens 	char *thename;
1650fa9e4066Sahrens 	char *origname;
1651fa9e4066Sahrens 	int ret;
16524b964adaSGeorge Wilson 	int error = 0;
1653990b4856Slling 	char errbuf[1024];
1654fa9e4066Sahrens 
1655fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1656fa9e4066Sahrens 	    &origname) == 0);
1657fa9e4066Sahrens 
1658990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1659990b4856Slling 	    "cannot import pool '%s'"), origname);
1660990b4856Slling 
1661fa9e4066Sahrens 	if (newname != NULL) {
166299653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1663ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
166499653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
166599653d4eSeschrock 			    newname));
1666fa9e4066Sahrens 		thename = (char *)newname;
1667fa9e4066Sahrens 	} else {
1668fa9e4066Sahrens 		thename = origname;
1669fa9e4066Sahrens 	}
1670fa9e4066Sahrens 
1671990b4856Slling 	if (props) {
1672990b4856Slling 		uint64_t version;
1673f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1674fa9e4066Sahrens 
1675990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1676990b4856Slling 		    &version) == 0);
1677fa9e4066Sahrens 
16780a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1679f9af39baSGeorge Wilson 		    props, version, flags, errbuf)) == NULL) {
1680990b4856Slling 			return (-1);
1681351420b3Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1682351420b3Slling 			nvlist_free(props);
1683990b4856Slling 			return (-1);
1684351420b3Slling 		}
1685990b4856Slling 	}
1686990b4856Slling 
1687990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1688fa9e4066Sahrens 
1689fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1690ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1691fa9e4066Sahrens 
1692351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1693351420b3Slling 		nvlist_free(props);
169499653d4eSeschrock 		return (-1);
1695351420b3Slling 	}
169657f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1697468c413aSTim Haley 		nvlist_free(props);
1698468c413aSTim Haley 		return (-1);
1699468c413aSTim Haley 	}
1700fa9e4066Sahrens 
17014b964adaSGeorge Wilson 	zc.zc_cookie = flags;
17024b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
17034b964adaSGeorge Wilson 	    errno == ENOMEM) {
17044b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
17054b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
17064b964adaSGeorge Wilson 			return (-1);
17074b964adaSGeorge Wilson 		}
17084b964adaSGeorge Wilson 	}
17094b964adaSGeorge Wilson 	if (ret != 0)
17104b964adaSGeorge Wilson 		error = errno;
17114b964adaSGeorge Wilson 
17124b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
17134b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
17144b964adaSGeorge Wilson 
17154b964adaSGeorge Wilson 	if (error) {
1716fa9e4066Sahrens 		char desc[1024];
1717468c413aSTim Haley 
1718468c413aSTim Haley 		/*
1719468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1720468c413aSTim Haley 		 * looks like if we found a best txg
1721468c413aSTim Haley 		 */
17224b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1723468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
17244b964adaSGeorge Wilson 			    B_TRUE, nv);
17254b964adaSGeorge Wilson 			nvlist_free(nv);
1726468c413aSTim Haley 			return (-1);
1727468c413aSTim Haley 		}
1728468c413aSTim Haley 
1729fa9e4066Sahrens 		if (newname == NULL)
1730fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1731fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1732fa9e4066Sahrens 			    thename);
1733fa9e4066Sahrens 		else
1734fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1735fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1736fa9e4066Sahrens 			    origname, thename);
1737fa9e4066Sahrens 
17384b964adaSGeorge Wilson 		switch (error) {
1739ea8dc4b6Seschrock 		case ENOTSUP:
1740ad135b5dSChristopher Siden 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1741ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1742ad135b5dSChristopher Siden 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1743ad135b5dSChristopher Siden 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1744ad135b5dSChristopher Siden 				    "pool uses the following feature(s) not "
1745ad135b5dSChristopher Siden 				    "supported by this system:\n"));
1746ad135b5dSChristopher Siden 				zpool_print_unsup_feat(nv);
1747ad135b5dSChristopher Siden 				if (nvlist_exists(nvinfo,
1748ad135b5dSChristopher Siden 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1749ad135b5dSChristopher Siden 					(void) printf(dgettext(TEXT_DOMAIN,
1750ad135b5dSChristopher Siden 					    "All unsupported features are only "
1751ad135b5dSChristopher Siden 					    "required for writing to the pool."
1752ad135b5dSChristopher Siden 					    "\nThe pool can be imported using "
1753ad135b5dSChristopher Siden 					    "'-o readonly=on'.\n"));
1754ad135b5dSChristopher Siden 				}
1755ad135b5dSChristopher Siden 			}
1756ea8dc4b6Seschrock 			/*
1757ea8dc4b6Seschrock 			 * Unsupported version.
1758ea8dc4b6Seschrock 			 */
175999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1760ea8dc4b6Seschrock 			break;
1761ea8dc4b6Seschrock 
1762b5989ec7Seschrock 		case EINVAL:
1763b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1764b5989ec7Seschrock 			break;
1765b5989ec7Seschrock 
176654a91118SChris Kirby 		case EROFS:
176754a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176854a91118SChris Kirby 			    "one or more devices is read only"));
176954a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
177054a91118SChris Kirby 			break;
177154a91118SChris Kirby 
17724b964adaSGeorge Wilson 		case ENXIO:
17734b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
17744b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
17754b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
17764b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
17774b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
17784b964adaSGeorge Wilson 				    "The devices below are missing, use "
17794b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
17804b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
17814b964adaSGeorge Wilson 				(void) printf("\n");
17824b964adaSGeorge Wilson 			}
17834b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17844b964adaSGeorge Wilson 			break;
17854b964adaSGeorge Wilson 
17864b964adaSGeorge Wilson 		case EEXIST:
17874b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17884b964adaSGeorge Wilson 			break;
17894b964adaSGeorge Wilson 
1790fa9e4066Sahrens 		default:
17914b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1792468c413aSTim Haley 			zpool_explain_recover(hdl,
17934b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1794468c413aSTim Haley 			break;
1795fa9e4066Sahrens 		}
1796fa9e4066Sahrens 
17974b964adaSGeorge Wilson 		nvlist_free(nv);
1798fa9e4066Sahrens 		ret = -1;
1799fa9e4066Sahrens 	} else {
1800fa9e4066Sahrens 		zpool_handle_t *zhp;
1801ecd6cf80Smarks 
1802fa9e4066Sahrens 		/*
1803fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1804fa9e4066Sahrens 		 */
1805681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
180694de1d4cSeschrock 			ret = -1;
1807681d9761SEric Taylor 		else if (zhp != NULL)
1808fa9e4066Sahrens 			zpool_close(zhp);
1809468c413aSTim Haley 		if (policy.zrp_request &
1810468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1811468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
18124b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1813468c413aSTim Haley 		}
18144b964adaSGeorge Wilson 		nvlist_free(nv);
1815468c413aSTim Haley 		return (0);
1816fa9e4066Sahrens 	}
1817fa9e4066Sahrens 
1818e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1819351420b3Slling 	nvlist_free(props);
1820351420b3Slling 
1821fa9e4066Sahrens 	return (ret);
1822fa9e4066Sahrens }
1823fa9e4066Sahrens 
1824fa9e4066Sahrens /*
18253f9d6ad7SLin Ling  * Scan the pool.
1826fa9e4066Sahrens  */
1827fa9e4066Sahrens int
18283f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1829fa9e4066Sahrens {
1830fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1831fa9e4066Sahrens 	char msg[1024];
183299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1833fa9e4066Sahrens 
1834fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18353f9d6ad7SLin Ling 	zc.zc_cookie = func;
1836fa9e4066Sahrens 
1837cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
18383f9d6ad7SLin Ling 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1839fa9e4066Sahrens 		return (0);
1840fa9e4066Sahrens 
18413f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
18423f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18433f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
18443f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
18453f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18463f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
18473f9d6ad7SLin Ling 		    zc.zc_name);
18483f9d6ad7SLin Ling 	} else {
18493f9d6ad7SLin Ling 		assert(!"unexpected result");
18503f9d6ad7SLin Ling 	}
1851fa9e4066Sahrens 
18523f9d6ad7SLin Ling 	if (errno == EBUSY) {
18533f9d6ad7SLin Ling 		nvlist_t *nvroot;
18543f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
18553f9d6ad7SLin Ling 		uint_t psc;
18563f9d6ad7SLin Ling 
18573f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
18583f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
18593f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
18603f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
18613f9d6ad7SLin Ling 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
18623f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
18633f9d6ad7SLin Ling 		else
18643f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
18653f9d6ad7SLin Ling 	} else if (errno == ENOENT) {
18663f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
18673f9d6ad7SLin Ling 	} else {
186899653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
18693f9d6ad7SLin Ling 	}
1870fa9e4066Sahrens }
1871fa9e4066Sahrens 
18723fdda499SJohn Harres /*
18733fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
18743fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
18753fdda499SJohn Harres  * verification of the s# part.
18763fdda499SJohn Harres  */
18773fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
18783fdda499SJohn Harres 
18793fdda499SJohn Harres /*
18803fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
18813fdda499SJohn Harres  * and the like.
18823fdda499SJohn Harres  */
18833fdda499SJohn Harres static int
18843fdda499SJohn Harres ctd_check_path(char *str) {
18853fdda499SJohn Harres 	/*
18863fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
18873fdda499SJohn Harres 	 */
18883fdda499SJohn Harres 	if (str && str[0] == '/') {
18893fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
18903fdda499SJohn Harres 
18913fdda499SJohn Harres 		/*
18923fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
18933fdda499SJohn Harres 		 * component of the string instead.
18943fdda499SJohn Harres 		 */
18953fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
18963fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
18973fdda499SJohn Harres 				;
18983fdda499SJohn Harres 		}
18993fdda499SJohn Harres 		str = tmp + 1;
19003fdda499SJohn Harres 	}
19013fdda499SJohn Harres 	return (CTD_CHECK(str));
19023fdda499SJohn Harres }
19033fdda499SJohn Harres 
1904a43d325bSek /*
1905573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1906573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1907a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1908a43d325bSek  * spare; but FALSE if its an INUSE spare.
1909a43d325bSek  */
191099653d4eSeschrock static nvlist_t *
1911573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1912573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1913ea8dc4b6Seschrock {
1914ea8dc4b6Seschrock 	uint_t c, children;
1915ea8dc4b6Seschrock 	nvlist_t **child;
191699653d4eSeschrock 	nvlist_t *ret;
1917ee0eb9f2SEric Schrock 	uint64_t is_log;
1918573ca77eSGeorge Wilson 	char *srchkey;
1919573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1920573ca77eSGeorge Wilson 
1921573ca77eSGeorge Wilson 	/* Nothing to look for */
1922573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1923573ca77eSGeorge Wilson 		return (NULL);
1924ea8dc4b6Seschrock 
1925573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1926573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1927573ca77eSGeorge Wilson 
1928573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1929cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1930573ca77eSGeorge Wilson 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1931cb04b873SMark J Musante 			uint64_t srchval, theguid;
1932cb04b873SMark J Musante 
1933cb04b873SMark J Musante 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1934cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1935cb04b873SMark J Musante 			    &theguid) == 0);
1936cb04b873SMark J Musante 			if (theguid == srchval)
1937cb04b873SMark J Musante 				return (nv);
1938573ca77eSGeorge Wilson 		}
1939573ca77eSGeorge Wilson 		break;
1940573ca77eSGeorge Wilson 
1941573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1942573ca77eSGeorge Wilson 		char *srchval, *val;
1943573ca77eSGeorge Wilson 
1944573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
1945573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1946573ca77eSGeorge Wilson 			break;
1947ea8dc4b6Seschrock 
1948ea8dc4b6Seschrock 		/*
19493fdda499SJohn Harres 		 * Search for the requested value. Special cases:
19503fdda499SJohn Harres 		 *
19513fdda499SJohn Harres 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
19523fdda499SJohn Harres 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
19533fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
19543fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
19553fdda499SJohn Harres 		 *
195688ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
1957ea8dc4b6Seschrock 		 */
19583fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
19593fdda499SJohn Harres 		    ctd_check_path(val)) {
1960573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
1961573ca77eSGeorge Wilson 
1962573ca77eSGeorge Wilson 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1963573ca77eSGeorge Wilson 			    &wholedisk);
1964573ca77eSGeorge Wilson 			if (wholedisk) {
19653fdda499SJohn Harres 				int slen = strlen(srchval);
19663fdda499SJohn Harres 				int vlen = strlen(val);
19673fdda499SJohn Harres 
19683fdda499SJohn Harres 				if (slen != vlen - 2)
19693fdda499SJohn Harres 					break;
19703fdda499SJohn Harres 
19713fdda499SJohn Harres 				/*
19723fdda499SJohn Harres 				 * make_leaf_vdev() should only set
19733fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
19743fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
19753fdda499SJohn Harres 				 * room for the indices used next.
19763fdda499SJohn Harres 				 */
19773fdda499SJohn Harres 				ASSERT(vlen >= 6);
19783fdda499SJohn Harres 
19793fdda499SJohn Harres 				/*
19803fdda499SJohn Harres 				 * strings identical except trailing "s0"
19813fdda499SJohn Harres 				 */
19823fdda499SJohn Harres 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
19833fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
19843fdda499SJohn Harres 					return (nv);
19853fdda499SJohn Harres 
1986573ca77eSGeorge Wilson 				/*
19873fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
1988573ca77eSGeorge Wilson 				 */
19893fdda499SJohn Harres 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
19903fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
19913fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
1992573ca77eSGeorge Wilson 					return (nv);
19933fdda499SJohn Harres 
1994573ca77eSGeorge Wilson 				break;
1995573ca77eSGeorge Wilson 			}
199688ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
199788ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
199888ecc943SGeorge Wilson 			uint64_t id, vdev_id;
199988ecc943SGeorge Wilson 
200088ecc943SGeorge Wilson 			/*
200188ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
200288ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
200388ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
200488ecc943SGeorge Wilson 			 */
200588ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
200688ecc943SGeorge Wilson 				return (NULL);
200788ecc943SGeorge Wilson 
200888ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
200988ecc943SGeorge Wilson 				free(type);
201088ecc943SGeorge Wilson 				break;
201188ecc943SGeorge Wilson 			}
201288ecc943SGeorge Wilson 			idx = p + 1;
201388ecc943SGeorge Wilson 			*p = '\0';
201488ecc943SGeorge Wilson 
201588ecc943SGeorge Wilson 			/*
201688ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
201788ecc943SGeorge Wilson 			 */
201888ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
201988ecc943SGeorge Wilson 				free(type);
202088ecc943SGeorge Wilson 				break;
202188ecc943SGeorge Wilson 			}
202288ecc943SGeorge Wilson 
202388ecc943SGeorge Wilson 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
202488ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
202588ecc943SGeorge Wilson 			    strncmp(type, VDEV_TYPE_MIRROR,
202688ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_MIRROR)) == 0);
202788ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
202888ecc943SGeorge Wilson 			    &id) == 0);
202988ecc943SGeorge Wilson 
203088ecc943SGeorge Wilson 			errno = 0;
203188ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
203288ecc943SGeorge Wilson 
203388ecc943SGeorge Wilson 			free(type);
203488ecc943SGeorge Wilson 			if (errno != 0)
203588ecc943SGeorge Wilson 				return (NULL);
203688ecc943SGeorge Wilson 
203788ecc943SGeorge Wilson 			/*
203888ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
203988ecc943SGeorge Wilson 			 */
204088ecc943SGeorge Wilson 			if (vdev_id == id)
204188ecc943SGeorge Wilson 				return (nv);
2042ea8dc4b6Seschrock 		}
2043573ca77eSGeorge Wilson 
2044573ca77eSGeorge Wilson 		/*
2045573ca77eSGeorge Wilson 		 * Common case
2046573ca77eSGeorge Wilson 		 */
2047573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
2048573ca77eSGeorge Wilson 			return (nv);
2049573ca77eSGeorge Wilson 		break;
2050573ca77eSGeorge Wilson 	}
2051573ca77eSGeorge Wilson 
2052573ca77eSGeorge Wilson 	default:
2053573ca77eSGeorge Wilson 		break;
2054ea8dc4b6Seschrock 	}
2055ea8dc4b6Seschrock 
2056ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2057ea8dc4b6Seschrock 	    &child, &children) != 0)
205899653d4eSeschrock 		return (NULL);
2059ea8dc4b6Seschrock 
2060ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
2061573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2062ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
2063ee0eb9f2SEric Schrock 			/*
2064ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
2065ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
2066ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
2067ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
2068ee0eb9f2SEric Schrock 			 */
2069ee0eb9f2SEric Schrock 			if (log != NULL &&
2070ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
2071ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2072ee0eb9f2SEric Schrock 			    is_log) {
2073ee0eb9f2SEric Schrock 				*log = B_TRUE;
2074ee0eb9f2SEric Schrock 			}
2075ea8dc4b6Seschrock 			return (ret);
2076ee0eb9f2SEric Schrock 		}
2077ee0eb9f2SEric Schrock 	}
2078ea8dc4b6Seschrock 
207999653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
208099653d4eSeschrock 	    &child, &children) == 0) {
208199653d4eSeschrock 		for (c = 0; c < children; c++) {
2082573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2083ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2084a43d325bSek 				*avail_spare = B_TRUE;
208599653d4eSeschrock 				return (ret);
208699653d4eSeschrock 			}
208799653d4eSeschrock 		}
208899653d4eSeschrock 	}
208999653d4eSeschrock 
2090fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2091fa94a07fSbrendan 	    &child, &children) == 0) {
2092fa94a07fSbrendan 		for (c = 0; c < children; c++) {
2093573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2094ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2095fa94a07fSbrendan 				*l2cache = B_TRUE;
2096fa94a07fSbrendan 				return (ret);
2097fa94a07fSbrendan 			}
2098fa94a07fSbrendan 		}
2099fa94a07fSbrendan 	}
2100fa94a07fSbrendan 
210199653d4eSeschrock 	return (NULL);
2102ea8dc4b6Seschrock }
2103ea8dc4b6Seschrock 
2104573ca77eSGeorge Wilson /*
2105573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
2106573ca77eSGeorge Wilson  * associated vdev.
2107573ca77eSGeorge Wilson  */
2108573ca77eSGeorge Wilson nvlist_t *
2109573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2110573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2111573ca77eSGeorge Wilson {
2112573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
2113573ca77eSGeorge Wilson 
2114573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2115573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2116573ca77eSGeorge Wilson 
2117573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2118573ca77eSGeorge Wilson 	    &nvroot) == 0);
2119573ca77eSGeorge Wilson 
2120573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
2121cb04b873SMark J Musante 	*l2cache = B_FALSE;
2122daeb70e5SMark J Musante 	if (log != NULL)
2123daeb70e5SMark J Musante 		*log = B_FALSE;
2124573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2125573ca77eSGeorge Wilson 	nvlist_free(search);
2126573ca77eSGeorge Wilson 
2127573ca77eSGeorge Wilson 	return (ret);
2128573ca77eSGeorge Wilson }
2129573ca77eSGeorge Wilson 
213088ecc943SGeorge Wilson /*
213188ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
213288ecc943SGeorge Wilson  */
213388ecc943SGeorge Wilson boolean_t
213488ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
213588ecc943SGeorge Wilson {
213688ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
213788ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
213888ecc943SGeorge Wilson 		return (B_TRUE);
213988ecc943SGeorge Wilson 	return (B_FALSE);
214088ecc943SGeorge Wilson }
214188ecc943SGeorge Wilson 
214299653d4eSeschrock nvlist_t *
2143fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2144ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
2145ea8dc4b6Seschrock {
2146ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
2147ea8dc4b6Seschrock 	char *end;
2148573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
2149ea8dc4b6Seschrock 	uint64_t guid;
2150ea8dc4b6Seschrock 
2151573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2152573ca77eSGeorge Wilson 
21530917b783Seschrock 	guid = strtoull(path, &end, 10);
2154ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
2155573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
215688ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
215788ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2158ea8dc4b6Seschrock 	} else if (path[0] != '/') {
2159ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
2160573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2161ea8dc4b6Seschrock 	} else {
2162573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2163ea8dc4b6Seschrock 	}
2164ea8dc4b6Seschrock 
2165ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2166ea8dc4b6Seschrock 	    &nvroot) == 0);
2167ea8dc4b6Seschrock 
2168a43d325bSek 	*avail_spare = B_FALSE;
2169fa94a07fSbrendan 	*l2cache = B_FALSE;
2170ee0eb9f2SEric Schrock 	if (log != NULL)
2171ee0eb9f2SEric Schrock 		*log = B_FALSE;
2172573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2173573ca77eSGeorge Wilson 	nvlist_free(search);
2174573ca77eSGeorge Wilson 
2175573ca77eSGeorge Wilson 	return (ret);
2176a43d325bSek }
2177a43d325bSek 
217819397407SSherry Moore static int
217919397407SSherry Moore vdev_online(nvlist_t *nv)
218019397407SSherry Moore {
218119397407SSherry Moore 	uint64_t ival;
218219397407SSherry Moore 
218319397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
218419397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
218519397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
218619397407SSherry Moore 		return (0);
218719397407SSherry Moore 
218819397407SSherry Moore 	return (1);
218919397407SSherry Moore }
219019397407SSherry Moore 
219119397407SSherry Moore /*
219221ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
219319397407SSherry Moore  */
2194753a6d45SSherry Moore static int
219521ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2196753a6d45SSherry Moore     size_t *bytes_written)
219719397407SSherry Moore {
2198753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
2199753a6d45SSherry Moore 	char *tmppath;
2200753a6d45SSherry Moore 	const char *format;
2201753a6d45SSherry Moore 
2202753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2203753a6d45SSherry Moore 	    &tmppath) != 0)
2204753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2205753a6d45SSherry Moore 
2206753a6d45SSherry Moore 	pos = *bytes_written;
2207753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
2208753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
2209753a6d45SSherry Moore 
2210753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2211753a6d45SSherry Moore 	*bytes_written += rsz;
2212753a6d45SSherry Moore 
2213753a6d45SSherry Moore 	if (rsz >= bytes_left) {
2214753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2215753a6d45SSherry Moore 		if (bytes_left != 0) {
2216753a6d45SSherry Moore 			physpath[pos] = 0;
2217753a6d45SSherry Moore 		}
2218753a6d45SSherry Moore 		return (EZFS_NOSPC);
2219753a6d45SSherry Moore 	}
2220753a6d45SSherry Moore 	return (0);
2221753a6d45SSherry Moore }
2222753a6d45SSherry Moore 
222321ecdf64SLin Ling static int
222421ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
222521ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
222621ecdf64SLin Ling {
222721ecdf64SLin Ling 	char *type;
222821ecdf64SLin Ling 	int ret;
222921ecdf64SLin Ling 
223021ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
223121ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
223221ecdf64SLin Ling 
223321ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
223421ecdf64SLin Ling 		/*
223521ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
223621ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
223721ecdf64SLin Ling 		 * spare device.
223821ecdf64SLin Ling 		 */
223921ecdf64SLin Ling 		if (is_spare) {
224021ecdf64SLin Ling 			uint64_t spare = 0;
224121ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
224221ecdf64SLin Ling 			    &spare);
224321ecdf64SLin Ling 			if (!spare)
224421ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
224521ecdf64SLin Ling 		}
224621ecdf64SLin Ling 
224721ecdf64SLin Ling 		if (vdev_online(nv)) {
224821ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
224921ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
225021ecdf64SLin Ling 				return (ret);
225121ecdf64SLin Ling 		}
225221ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
225321ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
225421ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
225521ecdf64SLin Ling 		nvlist_t **child;
225621ecdf64SLin Ling 		uint_t count;
225721ecdf64SLin Ling 		int i, ret;
225821ecdf64SLin Ling 
225921ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
226021ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
226121ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
226221ecdf64SLin Ling 
226321ecdf64SLin Ling 		for (i = 0; i < count; i++) {
226421ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
226521ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
226621ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
226721ecdf64SLin Ling 				return (ret);
226821ecdf64SLin Ling 		}
226921ecdf64SLin Ling 	}
227021ecdf64SLin Ling 
227121ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
227221ecdf64SLin Ling }
227321ecdf64SLin Ling 
2274753a6d45SSherry Moore /*
2275753a6d45SSherry Moore  * Get phys_path for a root pool config.
2276753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2277753a6d45SSherry Moore  */
2278753a6d45SSherry Moore static int
2279753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2280753a6d45SSherry Moore {
2281753a6d45SSherry Moore 	size_t rsz;
228219397407SSherry Moore 	nvlist_t *vdev_root;
228319397407SSherry Moore 	nvlist_t **child;
228419397407SSherry Moore 	uint_t count;
2285753a6d45SSherry Moore 	char *type;
2286753a6d45SSherry Moore 
2287753a6d45SSherry Moore 	rsz = 0;
2288753a6d45SSherry Moore 
2289753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2290753a6d45SSherry Moore 	    &vdev_root) != 0)
2291753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2292753a6d45SSherry Moore 
2293753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2294753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2295753a6d45SSherry Moore 	    &child, &count) != 0)
2296753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
229719397407SSherry Moore 
229819397407SSherry Moore 	/*
2299753a6d45SSherry Moore 	 * root pool can not have EFI labeled disks and can only have
2300753a6d45SSherry Moore 	 * a single top-level vdev.
230119397407SSherry Moore 	 */
2302753a6d45SSherry Moore 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2303753a6d45SSherry Moore 	    pool_uses_efi(vdev_root))
2304753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
230519397407SSherry Moore 
230621ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
230721ecdf64SLin Ling 	    B_FALSE);
230819397407SSherry Moore 
2309753a6d45SSherry Moore 	/* No online devices */
2310753a6d45SSherry Moore 	if (rsz == 0)
2311753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2312753a6d45SSherry Moore 
231319397407SSherry Moore 	return (0);
231419397407SSherry Moore }
231519397407SSherry Moore 
2316753a6d45SSherry Moore /*
2317753a6d45SSherry Moore  * Get phys_path for a root pool
2318753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2319753a6d45SSherry Moore  */
2320753a6d45SSherry Moore int
2321753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2322753a6d45SSherry Moore {
2323753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2324753a6d45SSherry Moore 	    phypath_size));
2325753a6d45SSherry Moore }
2326753a6d45SSherry Moore 
2327573ca77eSGeorge Wilson /*
2328573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2329573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2330573ca77eSGeorge Wilson  */
2331573ca77eSGeorge Wilson static int
2332573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2333573ca77eSGeorge Wilson {
2334573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2335573ca77eSGeorge Wilson 	char errbuf[1024];
2336573ca77eSGeorge Wilson 	int fd, error;
2337573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2338573ca77eSGeorge Wilson 
2339573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2340573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2341573ca77eSGeorge Wilson 		return (-1);
2342573ca77eSGeorge Wilson 
2343573ca77eSGeorge Wilson 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2344573ca77eSGeorge Wilson 
2345573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2346573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2347573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2348573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2349573ca77eSGeorge Wilson 	}
2350573ca77eSGeorge Wilson 
2351573ca77eSGeorge Wilson 	/*
2352573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2353573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2354573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2355573ca77eSGeorge Wilson 	 */
2356573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2357573ca77eSGeorge Wilson 	(void) close(fd);
2358573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2359573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2360573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2361573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2362573ca77eSGeorge Wilson 	}
2363573ca77eSGeorge Wilson 	return (0);
2364573ca77eSGeorge Wilson }
2365573ca77eSGeorge Wilson 
2366fa9e4066Sahrens /*
23673d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
23683d7072f8Seschrock  * ZFS_ONLINE_* flags.
2369fa9e4066Sahrens  */
2370fa9e4066Sahrens int
23713d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
23723d7072f8Seschrock     vdev_state_t *newstate)
2373fa9e4066Sahrens {
2374fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2375fa9e4066Sahrens 	char msg[1024];
237699653d4eSeschrock 	nvlist_t *tgt;
2377573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
237899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2379fa9e4066Sahrens 
2380573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2381573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2382573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2383573ca77eSGeorge Wilson 	} else {
2384573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2385573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2386573ca77eSGeorge Wilson 	}
2387ea8dc4b6Seschrock 
2388fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2389ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2390573ca77eSGeorge Wilson 	    &islog)) == NULL)
239199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2392fa9e4066Sahrens 
239399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2394fa9e4066Sahrens 
2395069f55e2SEric Schrock 	if (avail_spare)
2396a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2397a43d325bSek 
2398573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND ||
2399573ca77eSGeorge Wilson 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2400573ca77eSGeorge Wilson 		char *pathname = NULL;
2401573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2402573ca77eSGeorge Wilson 
2403573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2404573ca77eSGeorge Wilson 		    &wholedisk);
2405573ca77eSGeorge Wilson 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2406573ca77eSGeorge Wilson 		    &pathname) == 0);
2407573ca77eSGeorge Wilson 
2408573ca77eSGeorge Wilson 		/*
2409573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2410573ca77eSGeorge Wilson 		 */
2411573ca77eSGeorge Wilson 		if (l2cache) {
2412573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2413573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2414573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2415573ca77eSGeorge Wilson 		}
2416573ca77eSGeorge Wilson 
2417573ca77eSGeorge Wilson 		if (wholedisk) {
2418573ca77eSGeorge Wilson 			pathname += strlen(DISK_ROOT) + 1;
2419cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2420573ca77eSGeorge Wilson 		}
2421573ca77eSGeorge Wilson 	}
2422573ca77eSGeorge Wilson 
24233d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
24243d7072f8Seschrock 	zc.zc_obj = flags;
2425fa9e4066Sahrens 
2426cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
24271195e687SMark J Musante 		if (errno == EINVAL) {
24281195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
24291195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
24301195e687SMark J Musante 			    "instead"), "zpool detach");
24311195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
24321195e687SMark J Musante 		}
24333d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24341195e687SMark J Musante 	}
24353d7072f8Seschrock 
24363d7072f8Seschrock 	*newstate = zc.zc_cookie;
24373d7072f8Seschrock 	return (0);
2438fa9e4066Sahrens }
2439fa9e4066Sahrens 
2440fa9e4066Sahrens /*
2441fa9e4066Sahrens  * Take the specified vdev offline
2442fa9e4066Sahrens  */
2443fa9e4066Sahrens int
24443d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2445fa9e4066Sahrens {
2446fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2447fa9e4066Sahrens 	char msg[1024];
244899653d4eSeschrock 	nvlist_t *tgt;
2449fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
245099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2451fa9e4066Sahrens 
2452ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2453ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2454ea8dc4b6Seschrock 
2455fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2456ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2457ee0eb9f2SEric Schrock 	    NULL)) == NULL)
245899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
245999653d4eSeschrock 
246099653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2461fa9e4066Sahrens 
2462069f55e2SEric Schrock 	if (avail_spare)
2463a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2464a43d325bSek 
24653d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
24663d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
24673d7072f8Seschrock 
2468cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24693d7072f8Seschrock 		return (0);
24703d7072f8Seschrock 
24713d7072f8Seschrock 	switch (errno) {
24723d7072f8Seschrock 	case EBUSY:
24733d7072f8Seschrock 
24743d7072f8Seschrock 		/*
24753d7072f8Seschrock 		 * There are no other replicas of this device.
24763d7072f8Seschrock 		 */
24773d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24783d7072f8Seschrock 
2479e6ca193dSGeorge Wilson 	case EEXIST:
2480e6ca193dSGeorge Wilson 		/*
2481e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2482e6ca193dSGeorge Wilson 		 */
2483e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2484e6ca193dSGeorge Wilson 
24853d7072f8Seschrock 	default:
24863d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24873d7072f8Seschrock 	}
24883d7072f8Seschrock }
24893d7072f8Seschrock 
24903d7072f8Seschrock /*
24913d7072f8Seschrock  * Mark the given vdev faulted.
24923d7072f8Seschrock  */
24933d7072f8Seschrock int
2494069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
24953d7072f8Seschrock {
24963d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
24973d7072f8Seschrock 	char msg[1024];
24983d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24993d7072f8Seschrock 
25003d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25013d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2502441d80aaSlling 
25033d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25043d7072f8Seschrock 	zc.zc_guid = guid;
25053d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2506069f55e2SEric Schrock 	zc.zc_obj = aux;
25073d7072f8Seschrock 
2508cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2509fa9e4066Sahrens 		return (0);
2510fa9e4066Sahrens 
2511fa9e4066Sahrens 	switch (errno) {
251299653d4eSeschrock 	case EBUSY:
2513fa9e4066Sahrens 
2514fa9e4066Sahrens 		/*
2515fa9e4066Sahrens 		 * There are no other replicas of this device.
2516fa9e4066Sahrens 		 */
251799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2518fa9e4066Sahrens 
251999653d4eSeschrock 	default:
252099653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2521fa9e4066Sahrens 	}
25223d7072f8Seschrock 
25233d7072f8Seschrock }
25243d7072f8Seschrock 
25253d7072f8Seschrock /*
25263d7072f8Seschrock  * Mark the given vdev degraded.
25273d7072f8Seschrock  */
25283d7072f8Seschrock int
2529069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25303d7072f8Seschrock {
25313d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25323d7072f8Seschrock 	char msg[1024];
25333d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25343d7072f8Seschrock 
25353d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25363d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
25373d7072f8Seschrock 
25383d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25393d7072f8Seschrock 	zc.zc_guid = guid;
25403d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2541069f55e2SEric Schrock 	zc.zc_obj = aux;
25423d7072f8Seschrock 
2543cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
25443d7072f8Seschrock 		return (0);
25453d7072f8Seschrock 
25463d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
254799653d4eSeschrock }
254899653d4eSeschrock 
254999653d4eSeschrock /*
255099653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
255199653d4eSeschrock  * a hot spare.
255299653d4eSeschrock  */
255399653d4eSeschrock static boolean_t
255499653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
255599653d4eSeschrock {
255699653d4eSeschrock 	nvlist_t **child;
255799653d4eSeschrock 	uint_t c, children;
255899653d4eSeschrock 	char *type;
255999653d4eSeschrock 
256099653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
256199653d4eSeschrock 	    &children) == 0) {
256299653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
256399653d4eSeschrock 		    &type) == 0);
256499653d4eSeschrock 
256599653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
256699653d4eSeschrock 		    children == 2 && child[which] == tgt)
256799653d4eSeschrock 			return (B_TRUE);
256899653d4eSeschrock 
256999653d4eSeschrock 		for (c = 0; c < children; c++)
257099653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
257199653d4eSeschrock 				return (B_TRUE);
257299653d4eSeschrock 	}
257399653d4eSeschrock 
257499653d4eSeschrock 	return (B_FALSE);
2575fa9e4066Sahrens }
2576fa9e4066Sahrens 
2577fa9e4066Sahrens /*
2578fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
25798654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2580fa9e4066Sahrens  */
2581fa9e4066Sahrens int
2582fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2583fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2584fa9e4066Sahrens {
2585fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2586fa9e4066Sahrens 	char msg[1024];
2587fa9e4066Sahrens 	int ret;
258899653d4eSeschrock 	nvlist_t *tgt;
2589ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2590ee0eb9f2SEric Schrock 	uint64_t val;
2591cb04b873SMark J Musante 	char *newname;
259299653d4eSeschrock 	nvlist_t **child;
259399653d4eSeschrock 	uint_t children;
259499653d4eSeschrock 	nvlist_t *config_root;
259599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25964263d13fSGeorge Wilson 	boolean_t rootpool = zpool_is_bootable(zhp);
2597fa9e4066Sahrens 
2598ea8dc4b6Seschrock 	if (replacing)
2599ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2600ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2601ea8dc4b6Seschrock 	else
2602ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2603ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2604ea8dc4b6Seschrock 
2605b5b76fecSGeorge Wilson 	/*
2606b5b76fecSGeorge Wilson 	 * If this is a root pool, make sure that we're not attaching an
2607b5b76fecSGeorge Wilson 	 * EFI labeled device.
2608b5b76fecSGeorge Wilson 	 */
2609b5b76fecSGeorge Wilson 	if (rootpool && pool_uses_efi(nvroot)) {
2610b5b76fecSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2611b5b76fecSGeorge Wilson 		    "EFI labeled devices are not supported on root pools."));
2612b5b76fecSGeorge Wilson 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2613b5b76fecSGeorge Wilson 	}
2614b5b76fecSGeorge Wilson 
2615fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2616ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2617ee0eb9f2SEric Schrock 	    &islog)) == 0)
261899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
261999653d4eSeschrock 
2620a43d325bSek 	if (avail_spare)
262199653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
262299653d4eSeschrock 
2623fa94a07fSbrendan 	if (l2cache)
2624fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2625fa94a07fSbrendan 
262699653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2627fa9e4066Sahrens 	zc.zc_cookie = replacing;
2628fa9e4066Sahrens 
262999653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
263099653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
263199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263299653d4eSeschrock 		    "new device must be a single disk"));
263399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
263499653d4eSeschrock 	}
263599653d4eSeschrock 
263699653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
263799653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
263899653d4eSeschrock 
263988ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
26400430f8daSeschrock 		return (-1);
26410430f8daSeschrock 
264299653d4eSeschrock 	/*
264399653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
264499653d4eSeschrock 	 * replace it with another hot spare.
264599653d4eSeschrock 	 */
264699653d4eSeschrock 	if (replacing &&
264799653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2648ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2649ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2650ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
265199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
265299653d4eSeschrock 		    "can only be replaced by another hot spare"));
26530430f8daSeschrock 		free(newname);
265499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
265599653d4eSeschrock 	}
265699653d4eSeschrock 
26570430f8daSeschrock 	free(newname);
26580430f8daSeschrock 
2659990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
266099653d4eSeschrock 		return (-1);
2661fa9e4066Sahrens 
2662cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2663fa9e4066Sahrens 
2664e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2665fa9e4066Sahrens 
2666b5b76fecSGeorge Wilson 	if (ret == 0) {
2667b5b76fecSGeorge Wilson 		if (rootpool) {
266821ecdf64SLin Ling 			/*
266921ecdf64SLin Ling 			 * XXX need a better way to prevent user from
267021ecdf64SLin Ling 			 * booting up a half-baked vdev.
267121ecdf64SLin Ling 			 */
267221ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
267321ecdf64SLin Ling 			    "sure to wait until resilver is done "
267421ecdf64SLin Ling 			    "before rebooting.\n"));
2675b5b76fecSGeorge Wilson 		}
2676fa9e4066Sahrens 		return (0);
2677b5b76fecSGeorge Wilson 	}
2678fa9e4066Sahrens 
2679fa9e4066Sahrens 	switch (errno) {
2680ea8dc4b6Seschrock 	case ENOTSUP:
2681fa9e4066Sahrens 		/*
2682fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2683fa9e4066Sahrens 		 */
26848654d025Sperrin 		if (replacing) {
2685cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2686cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2687cb04b873SMark J Musante 
2688ee0eb9f2SEric Schrock 			if (islog)
26898654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26908654d025Sperrin 				    "cannot replace a log with a spare"));
2691cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2692cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2693cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2694cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
26958654d025Sperrin 			else
26968654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26978654d025Sperrin 				    "cannot replace a replacing device"));
26988654d025Sperrin 		} else {
269999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270099653d4eSeschrock 			    "can only attach to mirrors and top-level "
270199653d4eSeschrock 			    "disks"));
27028654d025Sperrin 		}
270399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2704fa9e4066Sahrens 		break;
2705fa9e4066Sahrens 
2706ea8dc4b6Seschrock 	case EINVAL:
2707fa9e4066Sahrens 		/*
2708fa9e4066Sahrens 		 * The new device must be a single disk.
2709fa9e4066Sahrens 		 */
271099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
271199653d4eSeschrock 		    "new device must be a single disk"));
271299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2713fa9e4066Sahrens 		break;
2714fa9e4066Sahrens 
2715ea8dc4b6Seschrock 	case EBUSY:
271699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
271799653d4eSeschrock 		    new_disk);
271899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2719fa9e4066Sahrens 		break;
2720fa9e4066Sahrens 
2721ea8dc4b6Seschrock 	case EOVERFLOW:
2722fa9e4066Sahrens 		/*
2723fa9e4066Sahrens 		 * The new device is too small.
2724fa9e4066Sahrens 		 */
272599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
272699653d4eSeschrock 		    "device is too small"));
272799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2728fa9e4066Sahrens 		break;
2729fa9e4066Sahrens 
2730ea8dc4b6Seschrock 	case EDOM:
2731fa9e4066Sahrens 		/*
2732fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2733fa9e4066Sahrens 		 */
273499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
273599653d4eSeschrock 		    "devices have different sector alignment"));
273699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2737fa9e4066Sahrens 		break;
2738fa9e4066Sahrens 
2739ea8dc4b6Seschrock 	case ENAMETOOLONG:
2740fa9e4066Sahrens 		/*
2741fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2742fa9e4066Sahrens 		 */
274399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2744fa9e4066Sahrens 		break;
2745fa9e4066Sahrens 
2746ea8dc4b6Seschrock 	default:
274799653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2748fa9e4066Sahrens 	}
2749fa9e4066Sahrens 
275099653d4eSeschrock 	return (-1);
2751fa9e4066Sahrens }
2752fa9e4066Sahrens 
2753fa9e4066Sahrens /*
2754fa9e4066Sahrens  * Detach the specified device.
2755fa9e4066Sahrens  */
2756fa9e4066Sahrens int
2757fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2758fa9e4066Sahrens {
2759fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2760fa9e4066Sahrens 	char msg[1024];
276199653d4eSeschrock 	nvlist_t *tgt;
2762fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
276399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2764fa9e4066Sahrens 
2765ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2766ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2767ea8dc4b6Seschrock 
2768fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2769ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2770ee0eb9f2SEric Schrock 	    NULL)) == 0)
277199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2772fa9e4066Sahrens 
2773a43d325bSek 	if (avail_spare)
277499653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
277599653d4eSeschrock 
2776fa94a07fSbrendan 	if (l2cache)
2777fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2778fa94a07fSbrendan 
277999653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
278099653d4eSeschrock 
2781ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2782fa9e4066Sahrens 		return (0);
2783fa9e4066Sahrens 
2784fa9e4066Sahrens 	switch (errno) {
2785fa9e4066Sahrens 
2786ea8dc4b6Seschrock 	case ENOTSUP:
2787fa9e4066Sahrens 		/*
2788fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2789fa9e4066Sahrens 		 */
279099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
279199653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2792cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2793fa9e4066Sahrens 		break;
2794fa9e4066Sahrens 
2795ea8dc4b6Seschrock 	case EBUSY:
2796fa9e4066Sahrens 		/*
2797fa9e4066Sahrens 		 * There are no other replicas of this device.
2798fa9e4066Sahrens 		 */
279999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2800fa9e4066Sahrens 		break;
2801fa9e4066Sahrens 
2802ea8dc4b6Seschrock 	default:
280399653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2804ea8dc4b6Seschrock 	}
2805ea8dc4b6Seschrock 
280699653d4eSeschrock 	return (-1);
280799653d4eSeschrock }
280899653d4eSeschrock 
28091195e687SMark J Musante /*
28101195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
28111195e687SMark J Musante  *
28121195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
28131195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
28141195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
28151195e687SMark J Musante  * see if any entry in the schild array matches.
28161195e687SMark J Musante  *
28171195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
28181195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
28191195e687SMark J Musante  */
28201195e687SMark J Musante static int
28211195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
28221195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
28231195e687SMark J Musante {
28241195e687SMark J Musante 	uint_t mc;
28251195e687SMark J Musante 
28261195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
28271195e687SMark J Musante 		uint_t sc;
28281195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28291195e687SMark J Musante 		    mchild[mc], B_FALSE);
28301195e687SMark J Musante 
28311195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
28321195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28331195e687SMark J Musante 			    schild[sc], B_FALSE);
28341195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
28351195e687SMark J Musante 
28361195e687SMark J Musante 			free(spath);
28371195e687SMark J Musante 			if (result) {
28381195e687SMark J Musante 				free(mpath);
28391195e687SMark J Musante 				return (mc);
28401195e687SMark J Musante 			}
28411195e687SMark J Musante 		}
28421195e687SMark J Musante 
28431195e687SMark J Musante 		free(mpath);
28441195e687SMark J Musante 	}
28451195e687SMark J Musante 
28461195e687SMark J Musante 	return (-1);
28471195e687SMark J Musante }
28481195e687SMark J Musante 
28491195e687SMark J Musante /*
28501195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
28511195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
28521195e687SMark J Musante  */
28531195e687SMark J Musante int
28541195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
28551195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
28561195e687SMark J Musante {
28571195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
28581195e687SMark J Musante 	char msg[1024];
28591195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
28601195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
28611195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
28621195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
28631195e687SMark J Musante 	uint64_t vers;
28641195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
28651195e687SMark J Musante 	int retval = 0;
28661195e687SMark J Musante 
28671195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
28681195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
28691195e687SMark J Musante 
28701195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
28711195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
28721195e687SMark J Musante 
28731195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
28741195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
28751195e687SMark J Musante 		    "retrieve pool configuration\n"));
28761195e687SMark J Musante 		return (-1);
28771195e687SMark J Musante 	}
28781195e687SMark J Musante 
28791195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
28801195e687SMark J Musante 	    == 0);
28811195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
28821195e687SMark J Musante 
28831195e687SMark J Musante 	if (props) {
2884f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
28851195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2886f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
28871195e687SMark J Musante 			return (-1);
28881195e687SMark J Musante 	}
28891195e687SMark J Musante 
28901195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
28911195e687SMark J Musante 	    &children) != 0) {
28921195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28931195e687SMark J Musante 		    "Source pool is missing vdev tree"));
28941195e687SMark J Musante 		if (zc_props)
28951195e687SMark J Musante 			nvlist_free(zc_props);
28961195e687SMark J Musante 		return (-1);
28971195e687SMark J Musante 	}
28981195e687SMark J Musante 
28991195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
29001195e687SMark J Musante 	vcount = 0;
29011195e687SMark J Musante 
29021195e687SMark J Musante 	if (*newroot == NULL ||
29031195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
29041195e687SMark J Musante 	    &newchild, &newchildren) != 0)
29051195e687SMark J Musante 		newchildren = 0;
29061195e687SMark J Musante 
29071195e687SMark J Musante 	for (c = 0; c < children; c++) {
29081195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
29091195e687SMark J Musante 		char *type;
29101195e687SMark J Musante 		nvlist_t **mchild, *vdev;
29111195e687SMark J Musante 		uint_t mchildren;
29121195e687SMark J Musante 		int entry;
29131195e687SMark J Musante 
29141195e687SMark J Musante 		/*
29151195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
29161195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
29171195e687SMark J Musante 		 */
29181195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
29191195e687SMark J Musante 		    &is_log);
29201195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
29211195e687SMark J Musante 		    &is_hole);
29221195e687SMark J Musante 		if (is_log || is_hole) {
29231195e687SMark J Musante 			/*
29241195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
29251195e687SMark J Musante 			 */
29261195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
29271195e687SMark J Musante 				goto out;
29281195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
29291195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
29301195e687SMark J Musante 				goto out;
29311195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
29321195e687SMark J Musante 			    1) != 0)
29331195e687SMark J Musante 				goto out;
29341195e687SMark J Musante 			if (lastlog == 0)
29351195e687SMark J Musante 				lastlog = vcount;
29361195e687SMark J Musante 			varray[vcount++] = vdev;
29371195e687SMark J Musante 			continue;
29381195e687SMark J Musante 		}
29391195e687SMark J Musante 		lastlog = 0;
29401195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
29411195e687SMark J Musante 		    == 0);
29421195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
29431195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29441195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
29451195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29461195e687SMark J Musante 			goto out;
29471195e687SMark J Musante 		}
29481195e687SMark J Musante 
29491195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
29501195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
29511195e687SMark J Musante 
29521195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
29531195e687SMark J Musante 		if (newchildren > 0 &&
29541195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
29551195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
29561195e687SMark J Musante 			/* We found a disk that the user specified. */
29571195e687SMark J Musante 			vdev = mchild[entry];
29581195e687SMark J Musante 			++found;
29591195e687SMark J Musante 		} else {
29601195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
29611195e687SMark J Musante 			vdev = mchild[mchildren - 1];
29621195e687SMark J Musante 		}
29631195e687SMark J Musante 
29641195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
29651195e687SMark J Musante 			goto out;
29661195e687SMark J Musante 	}
29671195e687SMark J Musante 
29681195e687SMark J Musante 	/* did we find every disk the user specified? */
29691195e687SMark J Musante 	if (found != newchildren) {
29701195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
29711195e687SMark J Musante 		    "include at most one disk from each mirror"));
29721195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29731195e687SMark J Musante 		goto out;
29741195e687SMark J Musante 	}
29751195e687SMark J Musante 
29761195e687SMark J Musante 	/* Prepare the nvlist for populating. */
29771195e687SMark J Musante 	if (*newroot == NULL) {
29781195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
29791195e687SMark J Musante 			goto out;
29801195e687SMark J Musante 		freelist = B_TRUE;
29811195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
29821195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
29831195e687SMark J Musante 			goto out;
29841195e687SMark J Musante 	} else {
29851195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
29861195e687SMark J Musante 	}
29871195e687SMark J Musante 
29881195e687SMark J Musante 	/* Add all the children we found */
29891195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
29901195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
29911195e687SMark J Musante 		goto out;
29921195e687SMark J Musante 
29931195e687SMark J Musante 	/*
29941195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
29951195e687SMark J Musante 	 */
29961195e687SMark J Musante 	if (flags.dryrun) {
29971195e687SMark J Musante 		memory_err = B_FALSE;
29981195e687SMark J Musante 		freelist = B_FALSE;
29991195e687SMark J Musante 		goto out;
30001195e687SMark J Musante 	}
30011195e687SMark J Musante 
30021195e687SMark J Musante 	/* now build up the config list & call the ioctl */
30031195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
30041195e687SMark J Musante 		goto out;
30051195e687SMark J Musante 
30061195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
30071195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
30081195e687SMark J Musante 	    nvlist_add_string(newconfig,
30091195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
30101195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
30111195e687SMark J Musante 		goto out;
30121195e687SMark J Musante 
30131195e687SMark J Musante 	/*
30141195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
30151195e687SMark J Musante 	 * explicitly export it.
30161195e687SMark J Musante 	 */
30171195e687SMark J Musante 	if (!flags.import)
30181195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
30191195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30201195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
30211195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
30221195e687SMark J Musante 		goto out;
30231195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
30241195e687SMark J Musante 		goto out;
30251195e687SMark J Musante 
30261195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
30271195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
30281195e687SMark J Musante 		goto out;
30291195e687SMark J Musante 	}
30301195e687SMark J Musante 
30311195e687SMark J Musante 	freelist = B_FALSE;
30321195e687SMark J Musante 	memory_err = B_FALSE;
30331195e687SMark J Musante 
30341195e687SMark J Musante out:
30351195e687SMark J Musante 	if (varray != NULL) {
30361195e687SMark J Musante 		int v;
30371195e687SMark J Musante 
30381195e687SMark J Musante 		for (v = 0; v < vcount; v++)
30391195e687SMark J Musante 			nvlist_free(varray[v]);
30401195e687SMark J Musante 		free(varray);
30411195e687SMark J Musante 	}
30421195e687SMark J Musante 	zcmd_free_nvlists(&zc);
30431195e687SMark J Musante 	if (zc_props)
30441195e687SMark J Musante 		nvlist_free(zc_props);
30451195e687SMark J Musante 	if (newconfig)
30461195e687SMark J Musante 		nvlist_free(newconfig);
30471195e687SMark J Musante 	if (freelist) {
30481195e687SMark J Musante 		nvlist_free(*newroot);
30491195e687SMark J Musante 		*newroot = NULL;
30501195e687SMark J Musante 	}
30511195e687SMark J Musante 
30521195e687SMark J Musante 	if (retval != 0)
30531195e687SMark J Musante 		return (retval);
30541195e687SMark J Musante 
30551195e687SMark J Musante 	if (memory_err)
30561195e687SMark J Musante 		return (no_memory(hdl));
30571195e687SMark J Musante 
30581195e687SMark J Musante 	return (0);
30591195e687SMark J Musante }
30601195e687SMark J Musante 
306199653d4eSeschrock /*
3062fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
3063fa94a07fSbrendan  * and level 2 cache devices.
306499653d4eSeschrock  */
306599653d4eSeschrock int
306699653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
306799653d4eSeschrock {
306899653d4eSeschrock 	zfs_cmd_t zc = { 0 };
306999653d4eSeschrock 	char msg[1024];
307099653d4eSeschrock 	nvlist_t *tgt;
307188ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
307299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
307388ecc943SGeorge Wilson 	uint64_t version;
307499653d4eSeschrock 
307599653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
307699653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
307799653d4eSeschrock 
307899653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3079ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
308088ecc943SGeorge Wilson 	    &islog)) == 0)
308199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
308288ecc943SGeorge Wilson 	/*
308388ecc943SGeorge Wilson 	 * XXX - this should just go away.
308488ecc943SGeorge Wilson 	 */
308588ecc943SGeorge Wilson 	if (!avail_spare && !l2cache && !islog) {
308699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
308788ecc943SGeorge Wilson 		    "only inactive hot spares, cache, top-level, "
308888ecc943SGeorge Wilson 		    "or log devices can be removed"));
308999653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
309099653d4eSeschrock 	}
309199653d4eSeschrock 
309288ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
309388ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
309488ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309588ecc943SGeorge Wilson 		    "pool must be upgrade to support log removal"));
309688ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
309788ecc943SGeorge Wilson 	}
309888ecc943SGeorge Wilson 
309999653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
310099653d4eSeschrock 
3101ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
310299653d4eSeschrock 		return (0);
310399653d4eSeschrock 
310499653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3105ea8dc4b6Seschrock }
3106ea8dc4b6Seschrock 
3107ea8dc4b6Seschrock /*
3108ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
3109ea8dc4b6Seschrock  */
3110ea8dc4b6Seschrock int
3111468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3112ea8dc4b6Seschrock {
3113ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3114ea8dc4b6Seschrock 	char msg[1024];
311599653d4eSeschrock 	nvlist_t *tgt;
3116468c413aSTim Haley 	zpool_rewind_policy_t policy;
3117fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
311899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3119468c413aSTim Haley 	nvlist_t *nvi = NULL;
31204b964adaSGeorge Wilson 	int error;
3121ea8dc4b6Seschrock 
3122ea8dc4b6Seschrock 	if (path)
3123ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3124ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3125e9dbad6fSeschrock 		    path);
3126ea8dc4b6Seschrock 	else
3127ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3128ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3129ea8dc4b6Seschrock 		    zhp->zpool_name);
3130ea8dc4b6Seschrock 
3131ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
313299653d4eSeschrock 	if (path) {
3133fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3134ee0eb9f2SEric Schrock 		    &l2cache, NULL)) == 0)
313599653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3136ea8dc4b6Seschrock 
3137fa94a07fSbrendan 		/*
3138fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
3139fa94a07fSbrendan 		 * error clearing for l2cache devices.
3140fa94a07fSbrendan 		 */
3141a43d325bSek 		if (avail_spare)
314299653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3143ea8dc4b6Seschrock 
314499653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
314599653d4eSeschrock 		    &zc.zc_guid) == 0);
3146fa9e4066Sahrens 	}
3147fa9e4066Sahrens 
3148468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
3149468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
3150468c413aSTim Haley 
315157f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3152468c413aSTim Haley 		return (-1);
3153468c413aSTim Haley 
3154cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3155468c413aSTim Haley 		return (-1);
3156468c413aSTim Haley 
31574b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
31584b964adaSGeorge Wilson 	    errno == ENOMEM) {
31594b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
31604b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
31614b964adaSGeorge Wilson 			return (-1);
31624b964adaSGeorge Wilson 		}
31634b964adaSGeorge Wilson 	}
31644b964adaSGeorge Wilson 
31654b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3166468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
3167468c413aSTim Haley 		if (policy.zrp_request &
3168468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3169468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3170468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
3171468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3172468c413aSTim Haley 			    nvi);
3173468c413aSTim Haley 			nvlist_free(nvi);
3174468c413aSTim Haley 		}
3175468c413aSTim Haley 		zcmd_free_nvlists(&zc);
317699653d4eSeschrock 		return (0);
3177468c413aSTim Haley 	}
317899653d4eSeschrock 
3179468c413aSTim Haley 	zcmd_free_nvlists(&zc);
318099653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3181fa9e4066Sahrens }
3182fa9e4066Sahrens 
31833d7072f8Seschrock /*
31843d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
31853d7072f8Seschrock  */
31863d7072f8Seschrock int
31873d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
31883d7072f8Seschrock {
31893d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
31903d7072f8Seschrock 	char msg[1024];
31913d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31923d7072f8Seschrock 
31933d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
31943d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
31953d7072f8Seschrock 	    guid);
31963d7072f8Seschrock 
31973d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
31983d7072f8Seschrock 	zc.zc_guid = guid;
319914f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
32003d7072f8Seschrock 
32013d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
32023d7072f8Seschrock 		return (0);
32033d7072f8Seschrock 
32043d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
32053d7072f8Seschrock }
32063d7072f8Seschrock 
3207e9103aaeSGarrett D'Amore /*
3208e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
3209e9103aaeSGarrett D'Amore  */
3210e9103aaeSGarrett D'Amore int
3211e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3212e9103aaeSGarrett D'Amore {
3213e9103aaeSGarrett D'Amore 	char msg[1024];
3214e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3215e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3216e9103aaeSGarrett D'Amore 
3217e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3218e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3219e9103aaeSGarrett D'Amore 
3220e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3221e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3222e9103aaeSGarrett D'Amore 		return (0);
3223e9103aaeSGarrett D'Amore 
3224e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3225e9103aaeSGarrett D'Amore }
3226e9103aaeSGarrett D'Amore 
32274263d13fSGeorge Wilson /*
32284263d13fSGeorge Wilson  * Reopen the pool.
32294263d13fSGeorge Wilson  */
32304263d13fSGeorge Wilson int
32314263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
32324263d13fSGeorge Wilson {
32334263d13fSGeorge Wilson 	zfs_cmd_t zc = { 0 };
32344263d13fSGeorge Wilson 	char msg[1024];
32354263d13fSGeorge Wilson 	libzfs_handle_t *hdl = zhp->zpool_hdl;
32364263d13fSGeorge Wilson 
32374263d13fSGeorge Wilson 	(void) snprintf(msg, sizeof (msg),
32384263d13fSGeorge Wilson 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
32394263d13fSGeorge Wilson 	    zhp->zpool_name);
32404263d13fSGeorge Wilson 
32414263d13fSGeorge Wilson 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
32424263d13fSGeorge Wilson 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
32434263d13fSGeorge Wilson 		return (0);
32444263d13fSGeorge Wilson 	return (zpool_standard_error(hdl, errno, msg));
32454263d13fSGeorge Wilson }
32464263d13fSGeorge Wilson 
3247c67d9675Seschrock /*
3248c67d9675Seschrock  * Convert from a devid string to a path.
3249c67d9675Seschrock  */
3250c67d9675Seschrock static char *
3251c67d9675Seschrock devid_to_path(char *devid_str)
3252c67d9675Seschrock {
3253c67d9675Seschrock 	ddi_devid_t devid;
3254c67d9675Seschrock 	char *minor;
3255c67d9675Seschrock 	char *path;
3256c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3257c67d9675Seschrock 	int ret;
3258c67d9675Seschrock 
3259c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3260c67d9675Seschrock 		return (NULL);
3261c67d9675Seschrock 
3262c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3263c67d9675Seschrock 
3264c67d9675Seschrock 	devid_str_free(minor);
3265c67d9675Seschrock 	devid_free(devid);
3266c67d9675Seschrock 
3267c67d9675Seschrock 	if (ret != 0)
3268c67d9675Seschrock 		return (NULL);
3269c67d9675Seschrock 
327099653d4eSeschrock 	if ((path = strdup(list[0].devname)) == NULL)
327199653d4eSeschrock 		return (NULL);
327299653d4eSeschrock 
3273c67d9675Seschrock 	devid_free_nmlist(list);
3274c67d9675Seschrock 
3275c67d9675Seschrock 	return (path);
3276c67d9675Seschrock }
3277c67d9675Seschrock 
3278c67d9675Seschrock /*
3279c67d9675Seschrock  * Convert from a path to a devid string.
3280c67d9675Seschrock  */
3281c67d9675Seschrock static char *
3282c67d9675Seschrock path_to_devid(const char *path)
3283c67d9675Seschrock {
3284c67d9675Seschrock 	int fd;
3285c67d9675Seschrock 	ddi_devid_t devid;
3286c67d9675Seschrock 	char *minor, *ret;
3287c67d9675Seschrock 
3288c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3289c67d9675Seschrock 		return (NULL);
3290c67d9675Seschrock 
3291c67d9675Seschrock 	minor = NULL;
3292c67d9675Seschrock 	ret = NULL;
3293c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3294c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3295c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3296c67d9675Seschrock 		if (minor != NULL)
3297c67d9675Seschrock 			devid_str_free(minor);
3298c67d9675Seschrock 		devid_free(devid);
3299c67d9675Seschrock 	}
3300c67d9675Seschrock 	(void) close(fd);
3301c67d9675Seschrock 
3302c67d9675Seschrock 	return (ret);
3303c67d9675Seschrock }
3304c67d9675Seschrock 
3305c67d9675Seschrock /*
3306c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3307c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3308c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3309c67d9675Seschrock  */
3310c67d9675Seschrock static void
3311c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3312c67d9675Seschrock {
3313c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3314c67d9675Seschrock 
3315c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3316e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3317c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3318ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3319c67d9675Seschrock 
332099653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3321c67d9675Seschrock }
3322c67d9675Seschrock 
3323c67d9675Seschrock /*
3324c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3325c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3326c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3327c67d9675Seschrock  * trailing 's0' slice name.
3328c67d9675Seschrock  *
3329c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3330c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3331c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3332c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3333c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3334c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3335c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3336c67d9675Seschrock  * of these checks.
3337c67d9675Seschrock  */
3338c67d9675Seschrock char *
333988ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
334088ecc943SGeorge Wilson     boolean_t verbose)
3341c67d9675Seschrock {
3342c67d9675Seschrock 	char *path, *devid;
3343ea8dc4b6Seschrock 	uint64_t value;
3344ea8dc4b6Seschrock 	char buf[64];
33453d7072f8Seschrock 	vdev_stat_t *vs;
33463d7072f8Seschrock 	uint_t vsc;
3347c67d9675Seschrock 
3348ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3349ea8dc4b6Seschrock 	    &value) == 0) {
3350ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3351ea8dc4b6Seschrock 		    &value) == 0);
33525ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
33535ad82045Snd 		    (u_longlong_t)value);
3354ea8dc4b6Seschrock 		path = buf;
3355ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3356c67d9675Seschrock 
33573d7072f8Seschrock 		/*
33583d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
33593d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
33603d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
33613d7072f8Seschrock 		 * effects.
33623d7072f8Seschrock 		 */
33633f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
33643d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
33653d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
33663d7072f8Seschrock 		    zhp != NULL &&
3367c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3368c67d9675Seschrock 			/*
3369c67d9675Seschrock 			 * Determine if the current path is correct.
3370c67d9675Seschrock 			 */
3371c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3372c67d9675Seschrock 
3373c67d9675Seschrock 			if (newdevid == NULL ||
3374c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3375c67d9675Seschrock 				char *newpath;
3376c67d9675Seschrock 
3377c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3378c67d9675Seschrock 					/*
3379c67d9675Seschrock 					 * Update the path appropriately.
3380c67d9675Seschrock 					 */
3381c67d9675Seschrock 					set_path(zhp, nv, newpath);
338299653d4eSeschrock 					if (nvlist_add_string(nv,
338399653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
338499653d4eSeschrock 						verify(nvlist_lookup_string(nv,
338599653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
338699653d4eSeschrock 						    &path) == 0);
3387c67d9675Seschrock 					free(newpath);
3388c67d9675Seschrock 				}
3389c67d9675Seschrock 			}
3390c67d9675Seschrock 
339199653d4eSeschrock 			if (newdevid)
339299653d4eSeschrock 				devid_str_free(newdevid);
3393c67d9675Seschrock 		}
3394c67d9675Seschrock 
3395c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3396c67d9675Seschrock 			path += 9;
3397c67d9675Seschrock 
3398c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3399ea8dc4b6Seschrock 		    &value) == 0 && value) {
34003fdda499SJohn Harres 			int pathlen = strlen(path);
340199653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
34023fdda499SJohn Harres 
34033fdda499SJohn Harres 			/*
34043fdda499SJohn Harres 			 * If it starts with c#, and ends with "s0", chop
34053fdda499SJohn Harres 			 * the "s0" off, or if it ends with "s0/old", remove
34063fdda499SJohn Harres 			 * the "s0" from the middle.
34073fdda499SJohn Harres 			 */
34083fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
34093fdda499SJohn Harres 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
34103fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
34113fdda499SJohn Harres 				} else if (pathlen > 6 &&
34123fdda499SJohn Harres 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
34133fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
34143fdda499SJohn Harres 					    "/old");
34153fdda499SJohn Harres 				}
34163fdda499SJohn Harres 			}
3417c67d9675Seschrock 			return (tmp);
3418c67d9675Seschrock 		}
3419c67d9675Seschrock 	} else {
3420c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
342199653d4eSeschrock 
342299653d4eSeschrock 		/*
342399653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
342499653d4eSeschrock 		 */
342599653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
342699653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
342799653d4eSeschrock 			    &value) == 0);
342899653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
34295ad82045Snd 			    (u_longlong_t)value);
343099653d4eSeschrock 			path = buf;
343199653d4eSeschrock 		}
343288ecc943SGeorge Wilson 
343388ecc943SGeorge Wilson 		/*
343488ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
343588ecc943SGeorge Wilson 		 * naming convention.
343688ecc943SGeorge Wilson 		 */
343788ecc943SGeorge Wilson 		if (verbose) {
343888ecc943SGeorge Wilson 			uint64_t id;
343988ecc943SGeorge Wilson 
344088ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
344188ecc943SGeorge Wilson 			    &id) == 0);
344288ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
344388ecc943SGeorge Wilson 			    (u_longlong_t)id);
344488ecc943SGeorge Wilson 			path = buf;
344588ecc943SGeorge Wilson 		}
3446c67d9675Seschrock 	}
3447c67d9675Seschrock 
344899653d4eSeschrock 	return (zfs_strdup(hdl, path));
3449c67d9675Seschrock }
3450ea8dc4b6Seschrock 
3451ea8dc4b6Seschrock static int
3452ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b)
3453ea8dc4b6Seschrock {
3454ea8dc4b6Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
3455ea8dc4b6Seschrock }
3456ea8dc4b6Seschrock 
3457ea8dc4b6Seschrock /*
3458ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3459ea8dc4b6Seschrock  * caller.
3460ea8dc4b6Seschrock  */
3461ea8dc4b6Seschrock int
346255434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3463ea8dc4b6Seschrock {
3464ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3465ea8dc4b6Seschrock 	uint64_t count;
3466e9dbad6fSeschrock 	zbookmark_t *zb = NULL;
346755434c77Sek 	int i;
3468ea8dc4b6Seschrock 
3469ea8dc4b6Seschrock 	/*
3470ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3471ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3472ea8dc4b6Seschrock 	 * entire list.
3473ea8dc4b6Seschrock 	 */
3474ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3475ea8dc4b6Seschrock 	    &count) == 0);
347675519f38Sek 	if (count == 0)
347775519f38Sek 		return (0);
3478e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
34795ad82045Snd 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
348099653d4eSeschrock 		return (-1);
3481e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3482ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3483ea8dc4b6Seschrock 	for (;;) {
348499653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
348599653d4eSeschrock 		    &zc) != 0) {
3486e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3487ea8dc4b6Seschrock 			if (errno == ENOMEM) {
3488bf561db0Svb 				count = zc.zc_nvlist_dst_size;
3489e9dbad6fSeschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
3490bf561db0Svb 				    zfs_alloc(zhp->zpool_hdl, count *
3491bf561db0Svb 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
349299653d4eSeschrock 					return (-1);
3493ea8dc4b6Seschrock 			} else {
3494ea8dc4b6Seschrock 				return (-1);
3495ea8dc4b6Seschrock 			}
3496ea8dc4b6Seschrock 		} else {
3497ea8dc4b6Seschrock 			break;
3498ea8dc4b6Seschrock 		}
3499ea8dc4b6Seschrock 	}
3500ea8dc4b6Seschrock 
3501ea8dc4b6Seschrock 	/*
3502ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3503ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3504e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3505ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3506ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3507ea8dc4b6Seschrock 	 */
3508e9dbad6fSeschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3509e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3510e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3511ea8dc4b6Seschrock 
3512ea8dc4b6Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3513ea8dc4b6Seschrock 
351455434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3515ea8dc4b6Seschrock 
3516ea8dc4b6Seschrock 	/*
351755434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3518ea8dc4b6Seschrock 	 */
3519ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3520ea8dc4b6Seschrock 		nvlist_t *nv;
3521ea8dc4b6Seschrock 
3522c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
3523c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3524c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
3525ea8dc4b6Seschrock 			continue;
3526ea8dc4b6Seschrock 
352755434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
352855434c77Sek 			goto nomem;
352955434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
353055434c77Sek 		    zb[i].zb_objset) != 0) {
353155434c77Sek 			nvlist_free(nv);
353299653d4eSeschrock 			goto nomem;
3533ea8dc4b6Seschrock 		}
353455434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
353555434c77Sek 		    zb[i].zb_object) != 0) {
353655434c77Sek 			nvlist_free(nv);
353755434c77Sek 			goto nomem;
353855434c77Sek 		}
353955434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
354055434c77Sek 			nvlist_free(nv);
354155434c77Sek 			goto nomem;
354255434c77Sek 		}
354355434c77Sek 		nvlist_free(nv);
3544ea8dc4b6Seschrock 	}
3545ea8dc4b6Seschrock 
35463ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3547ea8dc4b6Seschrock 	return (0);
354899653d4eSeschrock 
354999653d4eSeschrock nomem:
3550e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
355199653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3552ea8dc4b6Seschrock }
3553eaca9bbdSeschrock 
3554eaca9bbdSeschrock /*
3555eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3556eaca9bbdSeschrock  */
3557eaca9bbdSeschrock int
3558990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3559eaca9bbdSeschrock {
3560eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
356199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3562eaca9bbdSeschrock 
3563eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3564990b4856Slling 	zc.zc_cookie = new_version;
3565990b4856Slling 
3566ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3567ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
356899653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
356999653d4eSeschrock 		    zhp->zpool_name));
3570eaca9bbdSeschrock 	return (0);
3571eaca9bbdSeschrock }
357206eeb2adSek 
357306eeb2adSek void
35744445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len)
357506eeb2adSek {
35764445fffbSMatthew Ahrens 	(void) strlcpy(string, basename(argv[0]), len);
35774445fffbSMatthew Ahrens 	for (int i = 1; i < argc; i++) {
35784445fffbSMatthew Ahrens 		(void) strlcat(string, " ", len);
35794445fffbSMatthew Ahrens 		(void) strlcat(string, argv[i], len);
35802a6b87f0Sek 	}
35812a6b87f0Sek }
35822a6b87f0Sek 
35832a6b87f0Sek int
35844445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message)
35852a6b87f0Sek {
35864445fffbSMatthew Ahrens 	zfs_cmd_t zc = { 0 };
35874445fffbSMatthew Ahrens 	nvlist_t *args;
35884445fffbSMatthew Ahrens 	int err;
35894445fffbSMatthew Ahrens 
35904445fffbSMatthew Ahrens 	args = fnvlist_alloc();
35914445fffbSMatthew Ahrens 	fnvlist_add_string(args, "message", message);
35924445fffbSMatthew Ahrens 	err = zcmd_write_src_nvlist(hdl, &zc, args);
35934445fffbSMatthew Ahrens 	if (err == 0)
35944445fffbSMatthew Ahrens 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
35954445fffbSMatthew Ahrens 	nvlist_free(args);
35964445fffbSMatthew Ahrens 	zcmd_free_nvlists(&zc);
35974445fffbSMatthew Ahrens 	return (err);
359806eeb2adSek }
359906eeb2adSek 
360006eeb2adSek /*
360106eeb2adSek  * Perform ioctl to get some command history of a pool.
360206eeb2adSek  *
360306eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
360406eeb2adSek  * logical offset of the history buffer to start reading from.
360506eeb2adSek  *
360606eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
360706eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
360806eeb2adSek  */
360906eeb2adSek static int
361006eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
361106eeb2adSek {
361206eeb2adSek 	zfs_cmd_t zc = { 0 };
361306eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
361406eeb2adSek 
361506eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
361606eeb2adSek 
361706eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
361806eeb2adSek 	zc.zc_history_len = *len;
361906eeb2adSek 	zc.zc_history_offset = *off;
362006eeb2adSek 
362106eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
362206eeb2adSek 		switch (errno) {
362306eeb2adSek 		case EPERM:
3624ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3625ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
362606eeb2adSek 			    "cannot show history for pool '%s'"),
362706eeb2adSek 			    zhp->zpool_name));
362806eeb2adSek 		case ENOENT:
3629ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
363006eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
363106eeb2adSek 			    "'%s'"), zhp->zpool_name));
3632d7306b64Sek 		case ENOTSUP:
3633d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3634d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3635d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
363606eeb2adSek 		default:
3637ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
363806eeb2adSek 			    dgettext(TEXT_DOMAIN,
363906eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
364006eeb2adSek 		}
364106eeb2adSek 	}
364206eeb2adSek 
364306eeb2adSek 	*len = zc.zc_history_len;
364406eeb2adSek 	*off = zc.zc_history_offset;
364506eeb2adSek 
364606eeb2adSek 	return (0);
364706eeb2adSek }
364806eeb2adSek 
364906eeb2adSek /*
365006eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
365106eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
365206eeb2adSek  * processed as there wasn't a complete record.
365306eeb2adSek  */
36548f18d1faSGeorge Wilson int
365506eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
365606eeb2adSek     nvlist_t ***records, uint_t *numrecords)
365706eeb2adSek {
365806eeb2adSek 	uint64_t reclen;
365906eeb2adSek 	nvlist_t *nv;
366006eeb2adSek 	int i;
366106eeb2adSek 
366206eeb2adSek 	while (bytes_read > sizeof (reclen)) {
366306eeb2adSek 
366406eeb2adSek 		/* get length of packed record (stored as little endian) */
366506eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
366606eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
366706eeb2adSek 
366806eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
366906eeb2adSek 			break;
367006eeb2adSek 
367106eeb2adSek 		/* unpack record */
367206eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
367306eeb2adSek 			return (ENOMEM);
367406eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
367506eeb2adSek 		buf += sizeof (reclen) + reclen;
367606eeb2adSek 
367706eeb2adSek 		/* add record to nvlist array */
367806eeb2adSek 		(*numrecords)++;
367906eeb2adSek 		if (ISP2(*numrecords + 1)) {
368006eeb2adSek 			*records = realloc(*records,
368106eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
368206eeb2adSek 		}
368306eeb2adSek 		(*records)[*numrecords - 1] = nv;
368406eeb2adSek 	}
368506eeb2adSek 
368606eeb2adSek 	*leftover = bytes_read;
368706eeb2adSek 	return (0);
368806eeb2adSek }
368906eeb2adSek 
369006eeb2adSek #define	HIS_BUF_LEN	(128*1024)
369106eeb2adSek 
369206eeb2adSek /*
369306eeb2adSek  * Retrieve the command history of a pool.
369406eeb2adSek  */
369506eeb2adSek int
369606eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
369706eeb2adSek {
369806eeb2adSek 	char buf[HIS_BUF_LEN];
369906eeb2adSek 	uint64_t off = 0;
370006eeb2adSek 	nvlist_t **records = NULL;
370106eeb2adSek 	uint_t numrecords = 0;
370206eeb2adSek 	int err, i;
370306eeb2adSek 
370406eeb2adSek 	do {
370506eeb2adSek 		uint64_t bytes_read = sizeof (buf);
370606eeb2adSek 		uint64_t leftover;
370706eeb2adSek 
370806eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
370906eeb2adSek 			break;
371006eeb2adSek 
371106eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
371206eeb2adSek 		if (!bytes_read)
371306eeb2adSek 			break;
371406eeb2adSek 
371506eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
371606eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
371706eeb2adSek 			break;
371806eeb2adSek 		off -= leftover;
371906eeb2adSek 
372006eeb2adSek 		/* CONSTCOND */
372106eeb2adSek 	} while (1);
372206eeb2adSek 
372306eeb2adSek 	if (!err) {
372406eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
372506eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
372606eeb2adSek 		    records, numrecords) == 0);
372706eeb2adSek 	}
372806eeb2adSek 	for (i = 0; i < numrecords; i++)
372906eeb2adSek 		nvlist_free(records[i]);
373006eeb2adSek 	free(records);
373106eeb2adSek 
373206eeb2adSek 	return (err);
373306eeb2adSek }
373455434c77Sek 
373555434c77Sek void
373655434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
373755434c77Sek     char *pathname, size_t len)
373855434c77Sek {
373955434c77Sek 	zfs_cmd_t zc = { 0 };
374055434c77Sek 	boolean_t mounted = B_FALSE;
374155434c77Sek 	char *mntpnt = NULL;
374255434c77Sek 	char dsname[MAXNAMELEN];
374355434c77Sek 
374455434c77Sek 	if (dsobj == 0) {
374555434c77Sek 		/* special case for the MOS */
374655434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
374755434c77Sek 		return;
374855434c77Sek 	}
374955434c77Sek 
375055434c77Sek 	/* get the dataset's name */
375155434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
375255434c77Sek 	zc.zc_obj = dsobj;
375355434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
375455434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
375555434c77Sek 		/* just write out a path of two object numbers */
375655434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
375755434c77Sek 		    dsobj, obj);
375855434c77Sek 		return;
375955434c77Sek 	}
376055434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
376155434c77Sek 
376255434c77Sek 	/* find out if the dataset is mounted */
376355434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
376455434c77Sek 
376555434c77Sek 	/* get the corrupted object's path */
376655434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
376755434c77Sek 	zc.zc_obj = obj;
376855434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
376955434c77Sek 	    &zc) == 0) {
377055434c77Sek 		if (mounted) {
377155434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
377255434c77Sek 			    zc.zc_value);
377355434c77Sek 		} else {
377455434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
377555434c77Sek 			    dsname, zc.zc_value);
377655434c77Sek 		}
377755434c77Sek 	} else {
377855434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
377955434c77Sek 	}
378055434c77Sek 	free(mntpnt);
378155434c77Sek }
3782b1b8ab34Slling 
378315e6edf1Sgw /*
378415e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
378515e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
378615e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
378715e6edf1Sgw  * partition.
378815e6edf1Sgw  */
378915e6edf1Sgw static int
379015e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb)
379115e6edf1Sgw {
379215e6edf1Sgw 	char *path;
379315e6edf1Sgw 	int fd;
379415e6edf1Sgw 	char diskname[MAXPATHLEN];
379515e6edf1Sgw 	int err = -1;
379615e6edf1Sgw 
379715e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
379815e6edf1Sgw 		return (err);
379915e6edf1Sgw 
380015e6edf1Sgw 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
380115e6edf1Sgw 	    strrchr(path, '/'));
380215e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
380315e6edf1Sgw 		struct dk_gpt *vtoc;
380415e6edf1Sgw 
380515e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
380615e6edf1Sgw 			if (sb != NULL)
380715e6edf1Sgw 				*sb = vtoc->efi_parts[0].p_start;
380815e6edf1Sgw 			efi_free(vtoc);
380915e6edf1Sgw 		}
381015e6edf1Sgw 		(void) close(fd);
381115e6edf1Sgw 	}
381215e6edf1Sgw 	return (err);
381315e6edf1Sgw }
381415e6edf1Sgw 
38158488aeb5Staylor /*
38168488aeb5Staylor  * determine where a partition starts on a disk in the current
38178488aeb5Staylor  * configuration
38188488aeb5Staylor  */
38198488aeb5Staylor static diskaddr_t
38208488aeb5Staylor find_start_block(nvlist_t *config)
38218488aeb5Staylor {
38228488aeb5Staylor 	nvlist_t **child;
38238488aeb5Staylor 	uint_t c, children;
38248488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
38258488aeb5Staylor 	uint64_t wholedisk;
38268488aeb5Staylor 
38278488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
38288488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
38298488aeb5Staylor 		if (nvlist_lookup_uint64(config,
38308488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
38318488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
38328488aeb5Staylor 			return (MAXOFFSET_T);
38338488aeb5Staylor 		}
383415e6edf1Sgw 		if (read_efi_label(config, &sb) < 0)
383515e6edf1Sgw 			sb = MAXOFFSET_T;
38368488aeb5Staylor 		return (sb);
38378488aeb5Staylor 	}
38388488aeb5Staylor 
38398488aeb5Staylor 	for (c = 0; c < children; c++) {
38408488aeb5Staylor 		sb = find_start_block(child[c]);
38418488aeb5Staylor 		if (sb != MAXOFFSET_T) {
38428488aeb5Staylor 			return (sb);
38438488aeb5Staylor 		}
38448488aeb5Staylor 	}
38458488aeb5Staylor 	return (MAXOFFSET_T);
38468488aeb5Staylor }
38478488aeb5Staylor 
38488488aeb5Staylor /*
38498488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
38508488aeb5Staylor  * stripped of any leading /dev path.
38518488aeb5Staylor  */
38528488aeb5Staylor int
38538488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
38548488aeb5Staylor {
38558488aeb5Staylor 	char path[MAXPATHLEN];
38568488aeb5Staylor 	struct dk_gpt *vtoc;
38578488aeb5Staylor 	int fd;
38588488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
38598488aeb5Staylor 	uint64_t slice_size;
38608488aeb5Staylor 	diskaddr_t start_block;
38618488aeb5Staylor 	char errbuf[1024];
38628488aeb5Staylor 
3863c6ef114fSmmusante 	/* prepare an error message just in case */
3864c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
3865c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3866c6ef114fSmmusante 
38678488aeb5Staylor 	if (zhp) {
38688488aeb5Staylor 		nvlist_t *nvroot;
38698488aeb5Staylor 
38704263d13fSGeorge Wilson 		if (zpool_is_bootable(zhp)) {
3871b5b76fecSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3872b5b76fecSGeorge Wilson 			    "EFI labeled devices are not supported on root "
3873b5b76fecSGeorge Wilson 			    "pools."));
3874b5b76fecSGeorge Wilson 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3875b5b76fecSGeorge Wilson 		}
3876b5b76fecSGeorge Wilson 
38778488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
38788488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
38798488aeb5Staylor 
38808488aeb5Staylor 		if (zhp->zpool_start_block == 0)
38818488aeb5Staylor 			start_block = find_start_block(nvroot);
38828488aeb5Staylor 		else
38838488aeb5Staylor 			start_block = zhp->zpool_start_block;
38848488aeb5Staylor 		zhp->zpool_start_block = start_block;
38858488aeb5Staylor 	} else {
38868488aeb5Staylor 		/* new pool */
38878488aeb5Staylor 		start_block = NEW_START_BLOCK;
38888488aeb5Staylor 	}
38898488aeb5Staylor 
38908488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
38918488aeb5Staylor 	    BACKUP_SLICE);
38928488aeb5Staylor 
38938488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
38948488aeb5Staylor 		/*
38958488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
38968488aeb5Staylor 		 * is a valid device.
38978488aeb5Staylor 		 */
3898c6ef114fSmmusante 		zfs_error_aux(hdl,
3899c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
39008488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
39018488aeb5Staylor 	}
39028488aeb5Staylor 
39038488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
39048488aeb5Staylor 		/*
39058488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
39068488aeb5Staylor 		 * were unable to read the disk's capacity
39078488aeb5Staylor 		 */
39088488aeb5Staylor 		if (errno == ENOMEM)
39098488aeb5Staylor 			(void) no_memory(hdl);
39108488aeb5Staylor 
39118488aeb5Staylor 		(void) close(fd);
3912c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3913c6ef114fSmmusante 		    "unable to read disk capacity"), name);
39148488aeb5Staylor 
39158488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
39168488aeb5Staylor 	}
39178488aeb5Staylor 
39188488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
39198488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
39208488aeb5Staylor 	if (start_block == MAXOFFSET_T)
39218488aeb5Staylor 		start_block = NEW_START_BLOCK;
39228488aeb5Staylor 	slice_size -= start_block;
39238488aeb5Staylor 
39248488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
39258488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
39268488aeb5Staylor 
39278488aeb5Staylor 	/*
39288488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
39298488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
39308488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
39318488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
39328488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
39338488aeb5Staylor 	 * can get, in the absence of V_OTHER.
39348488aeb5Staylor 	 */
39358488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
39368488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
39378488aeb5Staylor 
39388488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
39398488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
39408488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
39418488aeb5Staylor 
39428488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
39438488aeb5Staylor 		/*
39448488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
39458488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
39468488aeb5Staylor 		 * ecting the user to manually label the disk and give
39478488aeb5Staylor 		 * a specific slice.
39488488aeb5Staylor 		 */
39498488aeb5Staylor 		(void) close(fd);
39508488aeb5Staylor 		efi_free(vtoc);
39518488aeb5Staylor 
39528488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3953c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
39548488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
39558488aeb5Staylor 	}
39568488aeb5Staylor 
39578488aeb5Staylor 	(void) close(fd);
39588488aeb5Staylor 	efi_free(vtoc);
39598488aeb5Staylor 	return (0);
39608488aeb5Staylor }
3961e7cbe64fSgw 
3962e7cbe64fSgw static boolean_t
3963e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3964e7cbe64fSgw {
3965e7cbe64fSgw 	char *type;
3966e7cbe64fSgw 	nvlist_t **child;
3967e7cbe64fSgw 	uint_t children, c;
3968e7cbe64fSgw 
3969e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3970810e43b2SBill Pijewski 	if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
397188ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3972e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3973e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3974e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
3975e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3976e7cbe64fSgw 		return (B_FALSE);
3977e7cbe64fSgw 	}
3978e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3979e7cbe64fSgw 	    &child, &children) == 0) {
3980e7cbe64fSgw 		for (c = 0; c < children; c++) {
3981e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3982e7cbe64fSgw 				return (B_FALSE);
3983e7cbe64fSgw 		}
3984e7cbe64fSgw 	}
3985e7cbe64fSgw 	return (B_TRUE);
3986e7cbe64fSgw }
3987e7cbe64fSgw 
3988e7cbe64fSgw /*
3989810e43b2SBill Pijewski  * Check if this zvol is allowable for use as a dump device; zero if
3990810e43b2SBill Pijewski  * it is, > 0 if it isn't, < 0 if it isn't a zvol.
3991810e43b2SBill Pijewski  *
3992810e43b2SBill Pijewski  * Allowable storage configurations include mirrors, all raidz variants, and
3993810e43b2SBill Pijewski  * pools with log, cache, and spare devices.  Pools which are backed by files or
3994810e43b2SBill Pijewski  * have missing/hole vdevs are not suitable.
3995e7cbe64fSgw  */
3996e7cbe64fSgw int
3997e7cbe64fSgw zvol_check_dump_config(char *arg)
3998e7cbe64fSgw {
3999e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
4000e7cbe64fSgw 	nvlist_t *config, *nvroot;
4001e7cbe64fSgw 	char *p, *volname;
4002e7cbe64fSgw 	nvlist_t **top;
4003e7cbe64fSgw 	uint_t toplevels;
4004e7cbe64fSgw 	libzfs_handle_t *hdl;
4005e7cbe64fSgw 	char errbuf[1024];
4006e7cbe64fSgw 	char poolname[ZPOOL_MAXNAMELEN];
4007e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4008e7cbe64fSgw 	int ret = 1;
4009e7cbe64fSgw 
4010e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4011e7cbe64fSgw 		return (-1);
4012e7cbe64fSgw 	}
4013e7cbe64fSgw 
4014e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4015e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
4016e7cbe64fSgw 
4017e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
4018e7cbe64fSgw 		return (1);
4019e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
4020e7cbe64fSgw 
4021e7cbe64fSgw 	volname = arg + pathlen;
4022e7cbe64fSgw 
4023e7cbe64fSgw 	/* check the configuration of the pool */
4024e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
4025e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4026e7cbe64fSgw 		    "malformed dataset name"));
4027e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4028e7cbe64fSgw 		return (1);
4029e7cbe64fSgw 	} else if (p - volname >= ZFS_MAXNAMELEN) {
4030e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4031e7cbe64fSgw 		    "dataset name is too long"));
4032e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4033e7cbe64fSgw 		return (1);
4034e7cbe64fSgw 	} else {
4035e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
4036e7cbe64fSgw 		poolname[p - volname] = '\0';
4037e7cbe64fSgw 	}
4038e7cbe64fSgw 
4039e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4040e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4041e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
4042e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4043e7cbe64fSgw 		goto out;
4044e7cbe64fSgw 	}
4045e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
4046e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4047e7cbe64fSgw 	    &nvroot) != 0) {
4048e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4049e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
4050e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4051e7cbe64fSgw 		goto out;
4052e7cbe64fSgw 	}
4053e7cbe64fSgw 
4054e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4055e7cbe64fSgw 	    &top, &toplevels) == 0);
4056e7cbe64fSgw 
4057e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4058e7cbe64fSgw 		goto out;
4059e7cbe64fSgw 	}
4060e7cbe64fSgw 	ret = 0;
4061e7cbe64fSgw 
4062e7cbe64fSgw out:
4063e7cbe64fSgw 	if (zhp)
4064e7cbe64fSgw 		zpool_close(zhp);
4065e7cbe64fSgw 	libzfs_fini(hdl);
4066e7cbe64fSgw 	return (ret);
4067e7cbe64fSgw }
4068