xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_pool.c (revision e2dcee5754c56d91c6e1ff847db294541069ca0d)
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 /*
23078266a5SMarcel Telka  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
243f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25*a1988827SMatthew Ahrens  * Copyright (c) 2011, 2015 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 
5015e6edf1Sgw25295 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
zpool_get_all_props(zpool_handle_t * zhp)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
zpool_props_refresh(zpool_handle_t * zhp)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 *
zpool_get_prop_string(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)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
zpool_get_prop_int(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)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 *
zpool_state_to_name(vdev_state_t state,vdev_aux_t aux)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
zpool_get_prop(zpool_handle_t * zhp,zpool_prop_t prop,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)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:
2787fd05ac4SMatthew Ahrens 		case ZPOOL_PROP_LEAKED:
279c58b3526SAdam Stevko 			if (literal) {
280c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
281c58b3526SAdam Stevko 				    (u_longlong_t)intval);
282c58b3526SAdam Stevko 			} else {
283990b4856Slling 				(void) zfs_nicenum(intval, buf, len);
284c58b3526SAdam Stevko 			}
285990b4856Slling 			break;
2867a09f97bSGeorge Wilson 		case ZPOOL_PROP_EXPANDSZ:
2877a09f97bSGeorge Wilson 			if (intval == 0) {
2887a09f97bSGeorge Wilson 				(void) strlcpy(buf, "-", len);
2897a09f97bSGeorge Wilson 			} else if (literal) {
2907a09f97bSGeorge Wilson 				(void) snprintf(buf, len, "%llu",
2917a09f97bSGeorge Wilson 				    (u_longlong_t)intval);
2927a09f97bSGeorge Wilson 			} else {
2937a09f97bSGeorge Wilson 				(void) zfs_nicenum(intval, buf, len);
2947a09f97bSGeorge Wilson 			}
2957a09f97bSGeorge Wilson 			break;
296990b4856Slling 		case ZPOOL_PROP_CAPACITY:
297c58b3526SAdam Stevko 			if (literal) {
298c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
299c58b3526SAdam Stevko 				    (u_longlong_t)intval);
300c58b3526SAdam Stevko 			} else {
301990b4856Slling 				(void) snprintf(buf, len, "%llu%%",
302990b4856Slling 				    (u_longlong_t)intval);
303c58b3526SAdam Stevko 			}
304990b4856Slling 			break;
3052e4c9986SGeorge Wilson 		case ZPOOL_PROP_FRAGMENTATION:
3062e4c9986SGeorge Wilson 			if (intval == UINT64_MAX) {
3072e4c9986SGeorge Wilson 				(void) strlcpy(buf, "-", len);
3082e4c9986SGeorge Wilson 			} else {
3092e4c9986SGeorge Wilson 				(void) snprintf(buf, len, "%llu%%",
3102e4c9986SGeorge Wilson 				    (u_longlong_t)intval);
3112e4c9986SGeorge Wilson 			}
3122e4c9986SGeorge Wilson 			break;
313b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
314b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
315b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
316b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
317b24ab676SJeff Bonwick 			break;
318990b4856Slling 		case ZPOOL_PROP_HEALTH:
319990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
320990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
321990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
3223f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3233f9d6ad7SLin Ling 			    == 0);
324990b4856Slling 
325990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
326990b4856Slling 			    vs->vs_aux), len);
327990b4856Slling 			break;
328ad135b5dSChristopher Siden 		case ZPOOL_PROP_VERSION:
329ad135b5dSChristopher Siden 			if (intval >= SPA_VERSION_FEATURES) {
330ad135b5dSChristopher Siden 				(void) snprintf(buf, len, "-");
331ad135b5dSChristopher Siden 				break;
332ad135b5dSChristopher Siden 			}
333ad135b5dSChristopher Siden 			/* FALLTHROUGH */
334990b4856Slling 		default:
335990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
336990b4856Slling 		}
337990b4856Slling 		break;
338990b4856Slling 
339990b4856Slling 	case PROP_TYPE_INDEX:
340990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
341990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
342990b4856Slling 		    != 0)
343990b4856Slling 			return (-1);
344990b4856Slling 		(void) strlcpy(buf, strval, len);
345990b4856Slling 		break;
346990b4856Slling 
347990b4856Slling 	default:
348990b4856Slling 		abort();
349990b4856Slling 	}
350990b4856Slling 
351990b4856Slling 	if (srctype)
352990b4856Slling 		*srctype = src;
353990b4856Slling 
354990b4856Slling 	return (0);
355990b4856Slling }
356990b4856Slling 
357990b4856Slling /*
358990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
359990b4856Slling  * Assuming bootfs is a valid dataset name.
360990b4856Slling  */
361990b4856Slling static boolean_t
bootfs_name_valid(const char * pool,char * bootfs)362990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
363990b4856Slling {
364990b4856Slling 	int len = strlen(pool);
365990b4856Slling 
366fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
367990b4856Slling 		return (B_FALSE);
368990b4856Slling 
369990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
370990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
371990b4856Slling 		return (B_TRUE);
372990b4856Slling 
373990b4856Slling 	return (B_FALSE);
374990b4856Slling }
375990b4856Slling 
3764263d13fSGeorge Wilson boolean_t
zpool_is_bootable(zpool_handle_t * zhp)3774263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
378b5b76fecSGeorge Wilson {
379*a1988827SMatthew Ahrens 	char bootfs[ZFS_MAX_DATASET_NAME_LEN];
380b5b76fecSGeorge Wilson 
381b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
382c58b3526SAdam Stevko 	    sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
383b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
384b5b76fecSGeorge Wilson }
385b5b76fecSGeorge Wilson 
386b5b76fecSGeorge Wilson 
38715e6edf1Sgw25295 /*
388990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
389990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
390990b4856Slling  * specified as strings.
391990b4856Slling  */
392990b4856Slling static nvlist_t *
zpool_valid_proplist(libzfs_handle_t * hdl,const char * poolname,nvlist_t * props,uint64_t version,prop_flags_t flags,char * errbuf)3930a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
394f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
395990b4856Slling {
396990b4856Slling 	nvpair_t *elem;
397990b4856Slling 	nvlist_t *retprops;
398990b4856Slling 	zpool_prop_t prop;
399990b4856Slling 	char *strval;
400990b4856Slling 	uint64_t intval;
4018704186eSDan McDonald 	char *slash, *check;
4022f8aaab3Seschrock 	struct stat64 statbuf;
40315e6edf1Sgw25295 	zpool_handle_t *zhp;
404990b4856Slling 
405990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
406990b4856Slling 		(void) no_memory(hdl);
407990b4856Slling 		return (NULL);
408990b4856Slling 	}
409990b4856Slling 
410990b4856Slling 	elem = NULL;
411990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
412990b4856Slling 		const char *propname = nvpair_name(elem);
413990b4856Slling 
414ad135b5dSChristopher Siden 		prop = zpool_name_to_prop(propname);
415ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
416ad135b5dSChristopher Siden 			int err;
417ad135b5dSChristopher Siden 			char *fname = strchr(propname, '@') + 1;
418ad135b5dSChristopher Siden 
4192acef22dSMatthew Ahrens 			err = zfeature_lookup_name(fname, NULL);
420ad135b5dSChristopher Siden 			if (err != 0) {
421ad135b5dSChristopher Siden 				ASSERT3U(err, ==, ENOENT);
422ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
423ad135b5dSChristopher Siden 				    "invalid feature '%s'"), fname);
424ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
425ad135b5dSChristopher Siden 				goto error;
426ad135b5dSChristopher Siden 			}
427ad135b5dSChristopher Siden 
428ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
429ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
430ad135b5dSChristopher Siden 				    "'%s' must be a string"), propname);
431ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
432ad135b5dSChristopher Siden 				goto error;
433ad135b5dSChristopher Siden 			}
434ad135b5dSChristopher Siden 
435ad135b5dSChristopher Siden 			(void) nvpair_value_string(elem, &strval);
436ad135b5dSChristopher Siden 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
437ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
438ad135b5dSChristopher Siden 				    "property '%s' can only be set to "
439ad135b5dSChristopher Siden 				    "'enabled'"), propname);
440ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
441ad135b5dSChristopher Siden 				goto error;
442ad135b5dSChristopher Siden 			}
443ad135b5dSChristopher Siden 
444ad135b5dSChristopher Siden 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
445ad135b5dSChristopher Siden 				(void) no_memory(hdl);
446ad135b5dSChristopher Siden 				goto error;
447ad135b5dSChristopher Siden 			}
448ad135b5dSChristopher Siden 			continue;
449ad135b5dSChristopher Siden 		}
450ad135b5dSChristopher Siden 
451990b4856Slling 		/*
452990b4856Slling 		 * Make sure this property is valid and applies to this type.
453990b4856Slling 		 */
454ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL) {
455990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
456990b4856Slling 			    "invalid property '%s'"), propname);
457990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
458990b4856Slling 			goto error;
459990b4856Slling 		}
460990b4856Slling 
461990b4856Slling 		if (zpool_prop_readonly(prop)) {
462990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
463990b4856Slling 			    "is readonly"), propname);
464990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
465990b4856Slling 			goto error;
466990b4856Slling 		}
467990b4856Slling 
468990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
469990b4856Slling 		    &strval, &intval, errbuf) != 0)
470990b4856Slling 			goto error;
471990b4856Slling 
472990b4856Slling 		/*
473990b4856Slling 		 * Perform additional checking for specific properties.
474990b4856Slling 		 */
475990b4856Slling 		switch (prop) {
476990b4856Slling 		case ZPOOL_PROP_VERSION:
477ad135b5dSChristopher Siden 			if (intval < version ||
478ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
479990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
480990b4856Slling 				    "property '%s' number %d is invalid."),
481990b4856Slling 				    propname, intval);
482990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
483990b4856Slling 				goto error;
484990b4856Slling 			}
485990b4856Slling 			break;
486990b4856Slling 
487990b4856Slling 		case ZPOOL_PROP_BOOTFS:
488f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
489990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
490990b4856Slling 				    "property '%s' cannot be set at creation "
491990b4856Slling 				    "or import time"), propname);
492990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
493990b4856Slling 				goto error;
494990b4856Slling 			}
495990b4856Slling 
496990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
497990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
498990b4856Slling 				    "pool must be upgraded to support "
499990b4856Slling 				    "'%s' property"), propname);
500990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
501990b4856Slling 				goto error;
502990b4856Slling 			}
503990b4856Slling 
504990b4856Slling 			/*
505990b4856Slling 			 * bootfs property value has to be a dataset name and
506990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
507990b4856Slling 			 */
508990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
509990b4856Slling 			    strval)) {
510990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
511990b4856Slling 				    "is an invalid name"), strval);
512990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
513990b4856Slling 				goto error;
514990b4856Slling 			}
51515e6edf1Sgw25295 
51615e6edf1Sgw25295 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
51715e6edf1Sgw25295 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51815e6edf1Sgw25295 				    "could not open pool '%s'"), poolname);
51915e6edf1Sgw25295 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
52015e6edf1Sgw25295 				goto error;
52115e6edf1Sgw25295 			}
52215e6edf1Sgw25295 			zpool_close(zhp);
523990b4856Slling 			break;
524990b4856Slling 
525990b4856Slling 		case ZPOOL_PROP_ALTROOT:
526f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
527990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
528990b4856Slling 				    "property '%s' can only be set during pool "
529990b4856Slling 				    "creation or import"), propname);
530990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
531990b4856Slling 				goto error;
532990b4856Slling 			}
533990b4856Slling 
534990b4856Slling 			if (strval[0] != '/') {
535990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536990b4856Slling 				    "bad alternate root '%s'"), strval);
537990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
538990b4856Slling 				goto error;
539990b4856Slling 			}
540990b4856Slling 			break;
541990b4856Slling 
5422f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5432f8aaab3Seschrock 			if (strval[0] == '\0')
5442f8aaab3Seschrock 				break;
5452f8aaab3Seschrock 
5462f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5472f8aaab3Seschrock 				break;
5482f8aaab3Seschrock 
5492f8aaab3Seschrock 			if (strval[0] != '/') {
550990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5512f8aaab3Seschrock 				    "property '%s' must be empty, an "
5522f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
5532f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
554990b4856Slling 				goto error;
5552f8aaab3Seschrock 			}
556990b4856Slling 
5572f8aaab3Seschrock 			slash = strrchr(strval, '/');
5582f8aaab3Seschrock 
5592f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5602f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
5612f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5622f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
5632f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
564990b4856Slling 				goto error;
565990b4856Slling 			}
5662f8aaab3Seschrock 
5672f8aaab3Seschrock 			*slash = '\0';
5682f8aaab3Seschrock 
5692c32020fSeschrock 			if (strval[0] != '\0' &&
5702c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
5712c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
5722f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5732f8aaab3Seschrock 				    "'%s' is not a valid directory"),
5742f8aaab3Seschrock 				    strval);
5752f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5762f8aaab3Seschrock 				goto error;
5772f8aaab3Seschrock 			}
5782f8aaab3Seschrock 
5792f8aaab3Seschrock 			*slash = '/';
5802f8aaab3Seschrock 			break;
581f9af39baSGeorge Wilson 
5828704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
5838704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
5848704186eSDan McDonald 				if (!isprint(*check)) {
5858704186eSDan McDonald 					zfs_error_aux(hdl,
5868704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
5878704186eSDan McDonald 					    "comment may only have printable "
5888704186eSDan McDonald 					    "characters"));
5898704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
5908704186eSDan McDonald 					    errbuf);
5918704186eSDan McDonald 					goto error;
5928704186eSDan McDonald 				}
5938704186eSDan McDonald 			}
5948704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
5958704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5968704186eSDan McDonald 				    "comment must not exceed %d characters"),
5978704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
5988704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5998704186eSDan McDonald 				goto error;
6008704186eSDan McDonald 			}
6018704186eSDan McDonald 			break;
602f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
603f9af39baSGeorge Wilson 			if (!flags.import) {
604f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
605f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
606f9af39baSGeorge Wilson 				    "import time"), propname);
607f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
608f9af39baSGeorge Wilson 				goto error;
609f9af39baSGeorge Wilson 			}
610f9af39baSGeorge Wilson 			break;
6112f8aaab3Seschrock 		}
612990b4856Slling 	}
613990b4856Slling 
614990b4856Slling 	return (retprops);
615990b4856Slling error:
616990b4856Slling 	nvlist_free(retprops);
617990b4856Slling 	return (NULL);
618990b4856Slling }
619990b4856Slling 
620990b4856Slling /*
621990b4856Slling  * Set zpool property : propname=propval.
622990b4856Slling  */
623990b4856Slling int
zpool_set_prop(zpool_handle_t * zhp,const char * propname,const char * propval)624990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
625990b4856Slling {
626990b4856Slling 	zfs_cmd_t zc = { 0 };
627990b4856Slling 	int ret = -1;
628990b4856Slling 	char errbuf[1024];
629990b4856Slling 	nvlist_t *nvl = NULL;
630990b4856Slling 	nvlist_t *realprops;
631990b4856Slling 	uint64_t version;
632f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
633990b4856Slling 
634990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
635990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
636990b4856Slling 	    zhp->zpool_name);
637990b4856Slling 
638990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
639990b4856Slling 		return (no_memory(zhp->zpool_hdl));
640990b4856Slling 
641990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
642990b4856Slling 		nvlist_free(nvl);
643990b4856Slling 		return (no_memory(zhp->zpool_hdl));
644990b4856Slling 	}
645990b4856Slling 
646990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6470a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
648f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
649990b4856Slling 		nvlist_free(nvl);
650990b4856Slling 		return (-1);
651990b4856Slling 	}
652990b4856Slling 
653990b4856Slling 	nvlist_free(nvl);
654990b4856Slling 	nvl = realprops;
655990b4856Slling 
656990b4856Slling 	/*
657990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
658990b4856Slling 	 */
659990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
660990b4856Slling 
661990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
662990b4856Slling 		nvlist_free(nvl);
663990b4856Slling 		return (-1);
664990b4856Slling 	}
665990b4856Slling 
666990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
667990b4856Slling 
668990b4856Slling 	zcmd_free_nvlists(&zc);
669990b4856Slling 	nvlist_free(nvl);
670990b4856Slling 
671990b4856Slling 	if (ret)
672990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
673990b4856Slling 	else
674990b4856Slling 		(void) zpool_props_refresh(zhp);
675990b4856Slling 
676990b4856Slling 	return (ret);
677990b4856Slling }
678990b4856Slling 
679990b4856Slling int
zpool_expand_proplist(zpool_handle_t * zhp,zprop_list_t ** plp)680990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
681990b4856Slling {
682990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
683990b4856Slling 	zprop_list_t *entry;
684990b4856Slling 	char buf[ZFS_MAXPROPLEN];
685ad135b5dSChristopher Siden 	nvlist_t *features = NULL;
686ad135b5dSChristopher Siden 	zprop_list_t **last;
687ad135b5dSChristopher Siden 	boolean_t firstexpand = (NULL == *plp);
688990b4856Slling 
689990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
690990b4856Slling 		return (-1);
691990b4856Slling 
692ad135b5dSChristopher Siden 	last = plp;
693ad135b5dSChristopher Siden 	while (*last != NULL)
694ad135b5dSChristopher Siden 		last = &(*last)->pl_next;
695ad135b5dSChristopher Siden 
696ad135b5dSChristopher Siden 	if ((*plp)->pl_all)
697ad135b5dSChristopher Siden 		features = zpool_get_features(zhp);
698ad135b5dSChristopher Siden 
699ad135b5dSChristopher Siden 	if ((*plp)->pl_all && firstexpand) {
700ad135b5dSChristopher Siden 		for (int i = 0; i < SPA_FEATURES; i++) {
701ad135b5dSChristopher Siden 			zprop_list_t *entry = zfs_alloc(hdl,
702ad135b5dSChristopher Siden 			    sizeof (zprop_list_t));
703ad135b5dSChristopher Siden 			entry->pl_prop = ZPROP_INVAL;
704ad135b5dSChristopher Siden 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
705ad135b5dSChristopher Siden 			    spa_feature_table[i].fi_uname);
706ad135b5dSChristopher Siden 			entry->pl_width = strlen(entry->pl_user_prop);
707ad135b5dSChristopher Siden 			entry->pl_all = B_TRUE;
708ad135b5dSChristopher Siden 
709ad135b5dSChristopher Siden 			*last = entry;
710ad135b5dSChristopher Siden 			last = &entry->pl_next;
711ad135b5dSChristopher Siden 		}
712ad135b5dSChristopher Siden 	}
713ad135b5dSChristopher Siden 
714ad135b5dSChristopher Siden 	/* add any unsupported features */
715ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
716ad135b5dSChristopher Siden 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
717ad135b5dSChristopher Siden 		char *propname;
718ad135b5dSChristopher Siden 		boolean_t found;
719ad135b5dSChristopher Siden 		zprop_list_t *entry;
720ad135b5dSChristopher Siden 
721ad135b5dSChristopher Siden 		if (zfeature_is_supported(nvpair_name(nvp)))
722ad135b5dSChristopher Siden 			continue;
723ad135b5dSChristopher Siden 
724ad135b5dSChristopher Siden 		propname = zfs_asprintf(hdl, "unsupported@%s",
725ad135b5dSChristopher Siden 		    nvpair_name(nvp));
726ad135b5dSChristopher Siden 
727ad135b5dSChristopher Siden 		/*
728ad135b5dSChristopher Siden 		 * Before adding the property to the list make sure that no
729ad135b5dSChristopher Siden 		 * other pool already added the same property.
730ad135b5dSChristopher Siden 		 */
731ad135b5dSChristopher Siden 		found = B_FALSE;
732ad135b5dSChristopher Siden 		entry = *plp;
733ad135b5dSChristopher Siden 		while (entry != NULL) {
734ad135b5dSChristopher Siden 			if (entry->pl_user_prop != NULL &&
735ad135b5dSChristopher Siden 			    strcmp(propname, entry->pl_user_prop) == 0) {
736ad135b5dSChristopher Siden 				found = B_TRUE;
737ad135b5dSChristopher Siden 				break;
738ad135b5dSChristopher Siden 			}
739ad135b5dSChristopher Siden 			entry = entry->pl_next;
740ad135b5dSChristopher Siden 		}
741ad135b5dSChristopher Siden 		if (found) {
742ad135b5dSChristopher Siden 			free(propname);
743ad135b5dSChristopher Siden 			continue;
744ad135b5dSChristopher Siden 		}
745ad135b5dSChristopher Siden 
746ad135b5dSChristopher Siden 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
747ad135b5dSChristopher Siden 		entry->pl_prop = ZPROP_INVAL;
748ad135b5dSChristopher Siden 		entry->pl_user_prop = propname;
749ad135b5dSChristopher Siden 		entry->pl_width = strlen(entry->pl_user_prop);
750ad135b5dSChristopher Siden 		entry->pl_all = B_TRUE;
751ad135b5dSChristopher Siden 
752ad135b5dSChristopher Siden 		*last = entry;
753ad135b5dSChristopher Siden 		last = &entry->pl_next;
754ad135b5dSChristopher Siden 	}
755ad135b5dSChristopher Siden 
756990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
757990b4856Slling 
758990b4856Slling 		if (entry->pl_fixed)
759990b4856Slling 			continue;
760990b4856Slling 
761990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
762990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
763c58b3526SAdam Stevko 		    NULL, B_FALSE) == 0) {
764990b4856Slling 			if (strlen(buf) > entry->pl_width)
765990b4856Slling 				entry->pl_width = strlen(buf);
766990b4856Slling 		}
767990b4856Slling 	}
768990b4856Slling 
769990b4856Slling 	return (0);
770990b4856Slling }
771990b4856Slling 
772ad135b5dSChristopher Siden /*
773ad135b5dSChristopher Siden  * Get the state for the given feature on the given ZFS pool.
774ad135b5dSChristopher Siden  */
775ad135b5dSChristopher Siden int
zpool_prop_get_feature(zpool_handle_t * zhp,const char * propname,char * buf,size_t len)776ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
777ad135b5dSChristopher Siden     size_t len)
778ad135b5dSChristopher Siden {
779ad135b5dSChristopher Siden 	uint64_t refcount;
780ad135b5dSChristopher Siden 	boolean_t found = B_FALSE;
781ad135b5dSChristopher Siden 	nvlist_t *features = zpool_get_features(zhp);
782ad135b5dSChristopher Siden 	boolean_t supported;
783ad135b5dSChristopher Siden 	const char *feature = strchr(propname, '@') + 1;
784ad135b5dSChristopher Siden 
785ad135b5dSChristopher Siden 	supported = zpool_prop_feature(propname);
786ad135b5dSChristopher Siden 	ASSERT(supported || zfs_prop_unsupported(propname));
787ad135b5dSChristopher Siden 
788ad135b5dSChristopher Siden 	/*
789ad135b5dSChristopher Siden 	 * Convert from feature name to feature guid. This conversion is
790ad135b5dSChristopher Siden 	 * unecessary for unsupported@... properties because they already
791ad135b5dSChristopher Siden 	 * use guids.
792ad135b5dSChristopher Siden 	 */
793ad135b5dSChristopher Siden 	if (supported) {
794ad135b5dSChristopher Siden 		int ret;
7952acef22dSMatthew Ahrens 		spa_feature_t fid;
796ad135b5dSChristopher Siden 
7972acef22dSMatthew Ahrens 		ret = zfeature_lookup_name(feature, &fid);
798ad135b5dSChristopher Siden 		if (ret != 0) {
799ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
800ad135b5dSChristopher Siden 			return (ENOTSUP);
801ad135b5dSChristopher Siden 		}
8022acef22dSMatthew Ahrens 		feature = spa_feature_table[fid].fi_guid;
803ad135b5dSChristopher Siden 	}
804ad135b5dSChristopher Siden 
805ad135b5dSChristopher Siden 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
806ad135b5dSChristopher Siden 		found = B_TRUE;
807ad135b5dSChristopher Siden 
808ad135b5dSChristopher Siden 	if (supported) {
809ad135b5dSChristopher Siden 		if (!found) {
810ad135b5dSChristopher Siden 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
811ad135b5dSChristopher Siden 		} else  {
812ad135b5dSChristopher Siden 			if (refcount == 0)
813ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
814ad135b5dSChristopher Siden 			else
815ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
816ad135b5dSChristopher Siden 		}
817ad135b5dSChristopher Siden 	} else {
818ad135b5dSChristopher Siden 		if (found) {
819ad135b5dSChristopher Siden 			if (refcount == 0) {
820ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
821ad135b5dSChristopher Siden 			} else {
822ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
823ad135b5dSChristopher Siden 			}
824ad135b5dSChristopher Siden 		} else {
825ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
826ad135b5dSChristopher Siden 			return (ENOTSUP);
827ad135b5dSChristopher Siden 		}
828ad135b5dSChristopher Siden 	}
829ad135b5dSChristopher Siden 
830ad135b5dSChristopher Siden 	return (0);
831ad135b5dSChristopher Siden }
832990b4856Slling 
833fa9e4066Sahrens /*
834573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
835573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
836573ca77eSGeorge Wilson  */
837573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
838573ca77eSGeorge Wilson 
839573ca77eSGeorge Wilson /*
840fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
841fa9e4066Sahrens  * 'buf'.
842fa9e4066Sahrens  */
843e7cbe64fSgw25295 boolean_t
zpool_name_valid(libzfs_handle_t * hdl,boolean_t isopen,const char * pool)84499653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
845fa9e4066Sahrens {
846fa9e4066Sahrens 	namecheck_err_t why;
847fa9e4066Sahrens 	char what;
848b468a217Seschrock 	int ret;
849fa9e4066Sahrens 
850b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
851b468a217Seschrock 
852b468a217Seschrock 	/*
853b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
854b468a217Seschrock 	 * But we need to support users with existing pools that may now be
855b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
856b468a217Seschrock 	 * create (or import), and only in userland.
857b468a217Seschrock 	 */
858b468a217Seschrock 	if (ret == 0 && !isopen &&
859b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
860b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
8618654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
8628654d025Sperrin 	    strcmp(pool, "log") == 0)) {
863e7cbe64fSgw25295 		if (hdl != NULL)
86499653d4eSeschrock 			zfs_error_aux(hdl,
86599653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "name is reserved"));
86699653d4eSeschrock 		return (B_FALSE);
867b468a217Seschrock 	}
868b468a217Seschrock 
869b468a217Seschrock 
870b468a217Seschrock 	if (ret != 0) {
87199653d4eSeschrock 		if (hdl != NULL) {
872fa9e4066Sahrens 			switch (why) {
873b81d61a6Slling 			case NAME_ERR_TOOLONG:
87499653d4eSeschrock 				zfs_error_aux(hdl,
875b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
876b81d61a6Slling 				break;
877b81d61a6Slling 
878fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
87999653d4eSeschrock 				zfs_error_aux(hdl,
880fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
881fa9e4066Sahrens 				    "'%c' in pool name"), what);
882fa9e4066Sahrens 				break;
883fa9e4066Sahrens 
884fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
88599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
88699653d4eSeschrock 				    "name must begin with a letter"));
887fa9e4066Sahrens 				break;
888fa9e4066Sahrens 
889fa9e4066Sahrens 			case NAME_ERR_RESERVED:
89099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
89199653d4eSeschrock 				    "name is reserved"));
892fa9e4066Sahrens 				break;
893fa9e4066Sahrens 
894fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
89599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
89699653d4eSeschrock 				    "pool name is reserved"));
897fa9e4066Sahrens 				break;
8985ad82045Snd150628 
8995ad82045Snd150628 			case NAME_ERR_LEADING_SLASH:
9005ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9015ad82045Snd150628 				    "leading slash in name"));
9025ad82045Snd150628 				break;
9035ad82045Snd150628 
9045ad82045Snd150628 			case NAME_ERR_EMPTY_COMPONENT:
9055ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9065ad82045Snd150628 				    "empty component in name"));
9075ad82045Snd150628 				break;
9085ad82045Snd150628 
9095ad82045Snd150628 			case NAME_ERR_TRAILING_SLASH:
9105ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9115ad82045Snd150628 				    "trailing slash in name"));
9125ad82045Snd150628 				break;
9135ad82045Snd150628 
914639f4a28SMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
9155ad82045Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
916639f4a28SMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
917639f4a28SMarcel Telka 				    "name"));
9185ad82045Snd150628 				break;
9195ad82045Snd150628 
920fa9e4066Sahrens 			}
921fa9e4066Sahrens 		}
92299653d4eSeschrock 		return (B_FALSE);
923fa9e4066Sahrens 	}
924fa9e4066Sahrens 
92599653d4eSeschrock 	return (B_TRUE);
926fa9e4066Sahrens }
927fa9e4066Sahrens 
928fa9e4066Sahrens /*
929fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
930fa9e4066Sahrens  * state.
931fa9e4066Sahrens  */
932fa9e4066Sahrens zpool_handle_t *
zpool_open_canfail(libzfs_handle_t * hdl,const char * pool)93399653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
934fa9e4066Sahrens {
935fa9e4066Sahrens 	zpool_handle_t *zhp;
93694de1d4cSeschrock 	boolean_t missing;
937fa9e4066Sahrens 
938fa9e4066Sahrens 	/*
939fa9e4066Sahrens 	 * Make sure the pool name is valid.
940fa9e4066Sahrens 	 */
94199653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
942ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
94399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
94499653d4eSeschrock 		    pool);
945fa9e4066Sahrens 		return (NULL);
946fa9e4066Sahrens 	}
947fa9e4066Sahrens 
94899653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
94999653d4eSeschrock 		return (NULL);
950fa9e4066Sahrens 
95199653d4eSeschrock 	zhp->zpool_hdl = hdl;
952fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
953fa9e4066Sahrens 
95494de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
95594de1d4cSeschrock 		zpool_close(zhp);
95694de1d4cSeschrock 		return (NULL);
95794de1d4cSeschrock 	}
95894de1d4cSeschrock 
95994de1d4cSeschrock 	if (missing) {
960990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
961ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
962990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
96394de1d4cSeschrock 		zpool_close(zhp);
964fa9e4066Sahrens 		return (NULL);
965fa9e4066Sahrens 	}
966fa9e4066Sahrens 
967fa9e4066Sahrens 	return (zhp);
968fa9e4066Sahrens }
969fa9e4066Sahrens 
970fa9e4066Sahrens /*
971fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
972fa9e4066Sahrens  * the configuration cache may be out of date).
973fa9e4066Sahrens  */
97494de1d4cSeschrock int
zpool_open_silent(libzfs_handle_t * hdl,const char * pool,zpool_handle_t ** ret)97594de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
976fa9e4066Sahrens {
977fa9e4066Sahrens 	zpool_handle_t *zhp;
97894de1d4cSeschrock 	boolean_t missing;
979fa9e4066Sahrens 
98094de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
98194de1d4cSeschrock 		return (-1);
982fa9e4066Sahrens 
98399653d4eSeschrock 	zhp->zpool_hdl = hdl;
984fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
985fa9e4066Sahrens 
98694de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
98794de1d4cSeschrock 		zpool_close(zhp);
98894de1d4cSeschrock 		return (-1);
989fa9e4066Sahrens 	}
990fa9e4066Sahrens 
99194de1d4cSeschrock 	if (missing) {
99294de1d4cSeschrock 		zpool_close(zhp);
99394de1d4cSeschrock 		*ret = NULL;
99494de1d4cSeschrock 		return (0);
99594de1d4cSeschrock 	}
99694de1d4cSeschrock 
99794de1d4cSeschrock 	*ret = zhp;
99894de1d4cSeschrock 	return (0);
999fa9e4066Sahrens }
1000fa9e4066Sahrens 
1001fa9e4066Sahrens /*
1002fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1003fa9e4066Sahrens  * state.
1004fa9e4066Sahrens  */
1005fa9e4066Sahrens zpool_handle_t *
zpool_open(libzfs_handle_t * hdl,const char * pool)100699653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1007fa9e4066Sahrens {
1008fa9e4066Sahrens 	zpool_handle_t *zhp;
1009fa9e4066Sahrens 
101099653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1011fa9e4066Sahrens 		return (NULL);
1012fa9e4066Sahrens 
1013fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1014ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
101599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1016fa9e4066Sahrens 		zpool_close(zhp);
1017fa9e4066Sahrens 		return (NULL);
1018fa9e4066Sahrens 	}
1019fa9e4066Sahrens 
1020fa9e4066Sahrens 	return (zhp);
1021fa9e4066Sahrens }
1022fa9e4066Sahrens 
1023fa9e4066Sahrens /*
1024fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
1025fa9e4066Sahrens  */
1026fa9e4066Sahrens void
zpool_close(zpool_handle_t * zhp)1027fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1028fa9e4066Sahrens {
1029fa9e4066Sahrens 	nvlist_free(zhp->zpool_config);
1030088e9d47Seschrock 	nvlist_free(zhp->zpool_old_config);
1031b1b8ab34Slling 	nvlist_free(zhp->zpool_props);
1032fa9e4066Sahrens 	free(zhp);
1033fa9e4066Sahrens }
1034fa9e4066Sahrens 
1035fa9e4066Sahrens /*
1036fa9e4066Sahrens  * Return the name of the pool.
1037fa9e4066Sahrens  */
1038fa9e4066Sahrens const char *
zpool_get_name(zpool_handle_t * zhp)1039fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1040fa9e4066Sahrens {
1041fa9e4066Sahrens 	return (zhp->zpool_name);
1042fa9e4066Sahrens }
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 
1045fa9e4066Sahrens /*
1046fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1047fa9e4066Sahrens  */
1048fa9e4066Sahrens int
zpool_get_state(zpool_handle_t * zhp)1049fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1050fa9e4066Sahrens {
1051fa9e4066Sahrens 	return (zhp->zpool_state);
1052fa9e4066Sahrens }
1053fa9e4066Sahrens 
1054fa9e4066Sahrens /*
1055fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
1056fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
1057fa9e4066Sahrens  * don't have to worry about error semantics.
1058fa9e4066Sahrens  */
1059fa9e4066Sahrens int
zpool_create(libzfs_handle_t * hdl,const char * pool,nvlist_t * nvroot,nvlist_t * props,nvlist_t * fsprops)106099653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
10610a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
1062fa9e4066Sahrens {
1063fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
10640a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
10650a48a24eStimh 	nvlist_t *zc_props = NULL;
106699653d4eSeschrock 	char msg[1024];
10670a48a24eStimh 	int ret = -1;
1068fa9e4066Sahrens 
106999653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
107099653d4eSeschrock 	    "cannot create '%s'"), pool);
107199653d4eSeschrock 
107299653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
107399653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
107499653d4eSeschrock 
1075351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1076351420b3Slling 		return (-1);
1077351420b3Slling 
10780a48a24eStimh 	if (props) {
1079f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1080f9af39baSGeorge Wilson 
10810a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1082f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
10830a48a24eStimh 			goto create_failed;
1084351420b3Slling 		}
10850a48a24eStimh 	}
10860a48a24eStimh 
10870a48a24eStimh 	if (fsprops) {
10880a48a24eStimh 		uint64_t zoned;
10890a48a24eStimh 		char *zonestr;
10900a48a24eStimh 
10910a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
10920a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
10930a48a24eStimh 		    strcmp(zonestr, "on") == 0);
10940a48a24eStimh 
10950a48a24eStimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
10960a48a24eStimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
10970a48a24eStimh 			goto create_failed;
10980a48a24eStimh 		}
10990a48a24eStimh 		if (!zc_props &&
11000a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
11010a48a24eStimh 			goto create_failed;
11020a48a24eStimh 		}
11030a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
11040a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
11050a48a24eStimh 			goto create_failed;
11060a48a24eStimh 		}
11070a48a24eStimh 	}
11080a48a24eStimh 
11090a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
11100a48a24eStimh 		goto create_failed;
111199653d4eSeschrock 
1112fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1113fa9e4066Sahrens 
11140a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1115351420b3Slling 
1116e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
11170a48a24eStimh 		nvlist_free(zc_props);
11180a48a24eStimh 		nvlist_free(zc_fsprops);
111999653d4eSeschrock 
1120fa9e4066Sahrens 		switch (errno) {
1121fa9e4066Sahrens 		case EBUSY:
1122fa9e4066Sahrens 			/*
1123fa9e4066Sahrens 			 * This can happen if the user has specified the same
1124fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1125fa9e4066Sahrens 			 * until we try to add it and see we already have a
1126fa9e4066Sahrens 			 * label.
1127fa9e4066Sahrens 			 */
112899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
112999653d4eSeschrock 			    "one or more vdevs refer to the same device"));
113099653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 		case EOVERFLOW:
1133fa9e4066Sahrens 			/*
113499653d4eSeschrock 			 * This occurs when one of the devices is below
1135fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1136fa9e4066Sahrens 			 * device was the problem device since there's no
1137fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1138fa9e4066Sahrens 			 */
1139fa9e4066Sahrens 			{
1140fa9e4066Sahrens 				char buf[64];
1141fa9e4066Sahrens 
1142fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1143fa9e4066Sahrens 
114499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114599653d4eSeschrock 				    "one or more devices is less than the "
114699653d4eSeschrock 				    "minimum size (%s)"), buf);
1147fa9e4066Sahrens 			}
114899653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 		case ENOSPC:
115199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115299653d4eSeschrock 			    "one or more devices is out of space"));
115399653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1154fa9e4066Sahrens 
1155fa94a07fSbrendan 		case ENOTBLK:
1156fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1157fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1158fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1159fa94a07fSbrendan 
1160fa9e4066Sahrens 		default:
116199653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1162fa9e4066Sahrens 		}
1163fa9e4066Sahrens 	}
1164fa9e4066Sahrens 
11650a48a24eStimh create_failed:
1166351420b3Slling 	zcmd_free_nvlists(&zc);
11670a48a24eStimh 	nvlist_free(zc_props);
11680a48a24eStimh 	nvlist_free(zc_fsprops);
11690a48a24eStimh 	return (ret);
1170fa9e4066Sahrens }
1171fa9e4066Sahrens 
1172fa9e4066Sahrens /*
1173fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1174fa9e4066Sahrens  * datasets left in the pool.
1175fa9e4066Sahrens  */
1176fa9e4066Sahrens int
zpool_destroy(zpool_handle_t * zhp,const char * log_str)11774445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1178fa9e4066Sahrens {
1179fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1180fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
118199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
118299653d4eSeschrock 	char msg[1024];
1183fa9e4066Sahrens 
1184fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1185cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1186fa9e4066Sahrens 		return (-1);
1187fa9e4066Sahrens 
1188fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
11894445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1190fa9e4066Sahrens 
1191cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
119299653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
119399653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1194fa9e4066Sahrens 
119599653d4eSeschrock 		if (errno == EROFS) {
119699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
119799653d4eSeschrock 			    "one or more devices is read only"));
119899653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
119999653d4eSeschrock 		} else {
120099653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1201fa9e4066Sahrens 		}
1202fa9e4066Sahrens 
1203fa9e4066Sahrens 		if (zfp)
1204fa9e4066Sahrens 			zfs_close(zfp);
1205fa9e4066Sahrens 		return (-1);
1206fa9e4066Sahrens 	}
1207fa9e4066Sahrens 
1208fa9e4066Sahrens 	if (zfp) {
1209fa9e4066Sahrens 		remove_mountpoint(zfp);
1210fa9e4066Sahrens 		zfs_close(zfp);
1211fa9e4066Sahrens 	}
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	return (0);
1214fa9e4066Sahrens }
1215fa9e4066Sahrens 
1216fa9e4066Sahrens /*
1217fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1218fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1219fa9e4066Sahrens  */
1220fa9e4066Sahrens int
zpool_add(zpool_handle_t * zhp,nvlist_t * nvroot)1221fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1222fa9e4066Sahrens {
1223e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
122499653d4eSeschrock 	int ret;
122599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
122699653d4eSeschrock 	char msg[1024];
1227fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1228fa94a07fSbrendan 	uint_t nspares, nl2cache;
122999653d4eSeschrock 
123099653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
123199653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
123299653d4eSeschrock 
1233fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1234fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
123599653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
123699653d4eSeschrock 	    &spares, &nspares) == 0) {
123799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
123899653d4eSeschrock 		    "upgraded to add hot spares"));
123999653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
124099653d4eSeschrock 	}
1241fa9e4066Sahrens 
1242fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1243fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1244fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1245fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1246fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1247fa94a07fSbrendan 		    "upgraded to add cache devices"));
1248fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1249fa94a07fSbrendan 	}
1250fa94a07fSbrendan 
1251990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
125299653d4eSeschrock 		return (-1);
1253fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1254fa9e4066Sahrens 
1255cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1256fa9e4066Sahrens 		switch (errno) {
1257fa9e4066Sahrens 		case EBUSY:
1258fa9e4066Sahrens 			/*
1259fa9e4066Sahrens 			 * This can happen if the user has specified the same
1260fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1261fa9e4066Sahrens 			 * until we try to add it and see we already have a
1262fa9e4066Sahrens 			 * label.
1263fa9e4066Sahrens 			 */
126499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
126599653d4eSeschrock 			    "one or more vdevs refer to the same device"));
126699653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1267fa9e4066Sahrens 			break;
1268fa9e4066Sahrens 
1269fa9e4066Sahrens 		case EOVERFLOW:
1270fa9e4066Sahrens 			/*
1271fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1272fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1273fa9e4066Sahrens 			 * device was the problem device since there's no
1274fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1275fa9e4066Sahrens 			 */
1276fa9e4066Sahrens 			{
1277fa9e4066Sahrens 				char buf[64];
1278fa9e4066Sahrens 
1279fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1280fa9e4066Sahrens 
128199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128299653d4eSeschrock 				    "device is less than the minimum "
128399653d4eSeschrock 				    "size (%s)"), buf);
1284fa9e4066Sahrens 			}
128599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
128699653d4eSeschrock 			break;
128799653d4eSeschrock 
128899653d4eSeschrock 		case ENOTSUP:
128999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12908654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
129199653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1292fa9e4066Sahrens 			break;
1293fa9e4066Sahrens 
1294b1b8ab34Slling 		case EDOM:
1295b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12968654d025Sperrin 			    "root pool can not have multiple vdevs"
12978654d025Sperrin 			    " or separate logs"));
1298b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1299b1b8ab34Slling 			break;
1300b1b8ab34Slling 
1301fa94a07fSbrendan 		case ENOTBLK:
1302fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1303fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1304fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1305fa94a07fSbrendan 			break;
1306fa94a07fSbrendan 
1307fa9e4066Sahrens 		default:
130899653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1309fa9e4066Sahrens 		}
1310fa9e4066Sahrens 
131199653d4eSeschrock 		ret = -1;
131299653d4eSeschrock 	} else {
131399653d4eSeschrock 		ret = 0;
1314fa9e4066Sahrens 	}
1315fa9e4066Sahrens 
1316e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1317fa9e4066Sahrens 
131899653d4eSeschrock 	return (ret);
1319fa9e4066Sahrens }
1320fa9e4066Sahrens 
1321fa9e4066Sahrens /*
1322fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1323fa9e4066Sahrens  * mounted datasets in the pool.
1324fa9e4066Sahrens  */
13254445fffbSMatthew Ahrens static int
zpool_export_common(zpool_handle_t * zhp,boolean_t force,boolean_t hardforce,const char * log_str)13264445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
13274445fffbSMatthew Ahrens     const char *log_str)
1328fa9e4066Sahrens {
1329fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
133089a89ebfSlling 	char msg[1024];
1331fa9e4066Sahrens 
133289a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
133389a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
1334fa9e4066Sahrens 
133589a89ebfSlling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
133689a89ebfSlling 	zc.zc_cookie = force;
1337394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
13384445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
133989a89ebfSlling 
134089a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
134189a89ebfSlling 		switch (errno) {
134289a89ebfSlling 		case EXDEV:
134389a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
134489a89ebfSlling 			    "use '-f' to override the following errors:\n"
134589a89ebfSlling 			    "'%s' has an active shared spare which could be"
134689a89ebfSlling 			    " used by other pools once '%s' is exported."),
134789a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
134889a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
134989a89ebfSlling 			    msg));
135089a89ebfSlling 		default:
1351ece3d9b3Slling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
135289a89ebfSlling 			    msg));
135389a89ebfSlling 		}
135489a89ebfSlling 	}
135589a89ebfSlling 
1356fa9e4066Sahrens 	return (0);
1357fa9e4066Sahrens }
1358fa9e4066Sahrens 
1359394ab0cbSGeorge Wilson int
zpool_export(zpool_handle_t * zhp,boolean_t force,const char * log_str)13604445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1361394ab0cbSGeorge Wilson {
13624445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1363394ab0cbSGeorge Wilson }
1364394ab0cbSGeorge Wilson 
1365394ab0cbSGeorge Wilson int
zpool_export_force(zpool_handle_t * zhp,const char * log_str)13664445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1367394ab0cbSGeorge Wilson {
13684445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1369394ab0cbSGeorge Wilson }
1370394ab0cbSGeorge Wilson 
1371468c413aSTim Haley static void
zpool_rewind_exclaim(libzfs_handle_t * hdl,const char * name,boolean_t dryrun,nvlist_t * config)1372468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
13734b964adaSGeorge Wilson     nvlist_t *config)
1374468c413aSTim Haley {
13754b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1376468c413aSTim Haley 	uint64_t rewindto;
1377468c413aSTim Haley 	int64_t loss = -1;
1378468c413aSTim Haley 	struct tm t;
1379468c413aSTim Haley 	char timestr[128];
1380468c413aSTim Haley 
13814b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
1382468c413aSTim Haley 		return;
1383468c413aSTim Haley 
1384ad135b5dSChristopher Siden 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1385ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1386468c413aSTim Haley 		return;
1387ad135b5dSChristopher Siden 	}
13884b964adaSGeorge Wilson 
13894b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
13904b964adaSGeorge Wilson 		return;
13914b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1392468c413aSTim Haley 
1393468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1394468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1395468c413aSTim Haley 		if (dryrun) {
1396468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1397468c413aSTim Haley 			    "Would be able to return %s "
1398468c413aSTim Haley 			    "to its state as of %s.\n"),
1399468c413aSTim Haley 			    name, timestr);
1400468c413aSTim Haley 		} else {
1401468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1402468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1403468c413aSTim Haley 			    name, timestr);
1404468c413aSTim Haley 		}
1405468c413aSTim Haley 		if (loss > 120) {
1406468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1407468c413aSTim Haley 			    "%s approximately %lld "),
1408468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1409468c413aSTim Haley 			    (loss + 30) / 60);
1410468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1411468c413aSTim Haley 			    "minutes of transactions.\n"));
1412468c413aSTim Haley 		} else if (loss > 0) {
1413468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1414468c413aSTim Haley 			    "%s approximately %lld "),
1415468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1416468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1417468c413aSTim Haley 			    "seconds of transactions.\n"));
1418468c413aSTim Haley 		}
1419468c413aSTim Haley 	}
1420468c413aSTim Haley }
1421468c413aSTim Haley 
1422468c413aSTim Haley void
zpool_explain_recover(libzfs_handle_t * hdl,const char * name,int reason,nvlist_t * config)1423468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1424468c413aSTim Haley     nvlist_t *config)
1425468c413aSTim Haley {
14264b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1427468c413aSTim Haley 	int64_t loss = -1;
1428468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1429468c413aSTim Haley 	uint64_t rewindto;
1430468c413aSTim Haley 	struct tm t;
1431468c413aSTim Haley 	char timestr[128];
1432468c413aSTim Haley 
1433468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1434468c413aSTim Haley 		return;
1435468c413aSTim Haley 
1436468c413aSTim Haley 	if (reason >= 0)
1437468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1438468c413aSTim Haley 	else
1439468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1440468c413aSTim Haley 
1441468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
14424b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1443ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
14444b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1445468c413aSTim Haley 		goto no_info;
1446468c413aSTim Haley 
14474b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
14484b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1449468c413aSTim Haley 	    &edata);
1450468c413aSTim Haley 
1451468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1452468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1453468c413aSTim Haley 
1454468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1455468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1456468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1457468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1458468c413aSTim Haley 		    "\tshould correct the problem.  "),
1459468c413aSTim Haley 		    timestr);
1460468c413aSTim Haley 	} else {
1461468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1462468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1463468c413aSTim Haley 		    "should correct the problem.\n\t"));
1464468c413aSTim Haley 	}
1465468c413aSTim Haley 
1466468c413aSTim Haley 	if (loss > 120) {
1467468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1468468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1469468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1470468c413aSTim Haley 	} else if (loss > 0) {
1471468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1472468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1473468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1474468c413aSTim Haley 	}
1475468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1476468c413aSTim Haley 		if (edata == 1) {
1477468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1478468c413aSTim Haley 			    "After rewind, at least\n"
1479468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1480468c413aSTim Haley 		} else {
1481468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1482468c413aSTim Haley 			    "After rewind, several\n"
1483468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1484468c413aSTim Haley 		}
1485468c413aSTim Haley 	}
1486468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1487a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1488a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1489468c413aSTim Haley 
1490468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1491468c413aSTim Haley 	    "A scrub of the pool\n"
1492468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1493468c413aSTim Haley 	return;
1494468c413aSTim Haley 
1495468c413aSTim Haley no_info:
1496468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1497468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1498468c413aSTim Haley }
1499468c413aSTim Haley 
1500fa9e4066Sahrens /*
1501990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1502990b4856Slling  * if possible.
1503990b4856Slling  *
1504990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1505990b4856Slling  * new properties value to be set.
1506fa9e4066Sahrens  */
1507fa9e4066Sahrens int
zpool_import(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,char * altroot)150899653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1509990b4856Slling     char *altroot)
1510990b4856Slling {
1511990b4856Slling 	nvlist_t *props = NULL;
1512990b4856Slling 	int ret;
1513990b4856Slling 
1514990b4856Slling 	if (altroot != NULL) {
1515990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1516990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1517990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1518990b4856Slling 			    newname));
1519990b4856Slling 		}
1520990b4856Slling 
1521990b4856Slling 		if (nvlist_add_string(props,
1522352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1523352d8027SGeorge Wilson 		    nvlist_add_string(props,
1524352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1525990b4856Slling 			nvlist_free(props);
1526990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1527990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1528990b4856Slling 			    newname));
1529990b4856Slling 		}
1530990b4856Slling 	}
1531990b4856Slling 
15324b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
15334b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1534990b4856Slling 	nvlist_free(props);
1535990b4856Slling 	return (ret);
1536990b4856Slling }
1537990b4856Slling 
15384b964adaSGeorge Wilson static void
print_vdev_tree(libzfs_handle_t * hdl,const char * name,nvlist_t * nv,int indent)15394b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
15404b964adaSGeorge Wilson     int indent)
15414b964adaSGeorge Wilson {
15424b964adaSGeorge Wilson 	nvlist_t **child;
15434b964adaSGeorge Wilson 	uint_t c, children;
15444b964adaSGeorge Wilson 	char *vname;
15454b964adaSGeorge Wilson 	uint64_t is_log = 0;
15464b964adaSGeorge Wilson 
15474b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
15484b964adaSGeorge Wilson 	    &is_log);
15494b964adaSGeorge Wilson 
15504b964adaSGeorge Wilson 	if (name != NULL)
15514b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
15524b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
15534b964adaSGeorge Wilson 
15544b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
15554b964adaSGeorge Wilson 	    &child, &children) != 0)
15564b964adaSGeorge Wilson 		return;
15574b964adaSGeorge Wilson 
15584b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
15594b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
15604b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
15614b964adaSGeorge Wilson 		free(vname);
15624b964adaSGeorge Wilson 	}
15634b964adaSGeorge Wilson }
15644b964adaSGeorge Wilson 
1565ad135b5dSChristopher Siden void
zpool_print_unsup_feat(nvlist_t * config)1566ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1567ad135b5dSChristopher Siden {
1568ad135b5dSChristopher Siden 	nvlist_t *nvinfo, *unsup_feat;
1569ad135b5dSChristopher Siden 
1570ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1571ad135b5dSChristopher Siden 	    0);
1572ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1573ad135b5dSChristopher Siden 	    &unsup_feat) == 0);
1574ad135b5dSChristopher Siden 
1575ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1576ad135b5dSChristopher Siden 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1577ad135b5dSChristopher Siden 		char *desc;
1578ad135b5dSChristopher Siden 
1579ad135b5dSChristopher Siden 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1580ad135b5dSChristopher Siden 		verify(nvpair_value_string(nvp, &desc) == 0);
1581ad135b5dSChristopher Siden 
1582ad135b5dSChristopher Siden 		if (strlen(desc) > 0)
1583ad135b5dSChristopher Siden 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1584ad135b5dSChristopher Siden 		else
1585ad135b5dSChristopher Siden 			(void) printf("\t%s\n", nvpair_name(nvp));
1586ad135b5dSChristopher Siden 	}
1587ad135b5dSChristopher Siden }
1588ad135b5dSChristopher Siden 
1589990b4856Slling /*
1590990b4856Slling  * Import the given pool using the known configuration and a list of
1591990b4856Slling  * properties to be set. The configuration should have come from
1592990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1593990b4856Slling  * is imported with a different name.
1594990b4856Slling  */
1595990b4856Slling int
zpool_import_props(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,nvlist_t * props,int flags)1596990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
15974b964adaSGeorge Wilson     nvlist_t *props, int flags)
1598fa9e4066Sahrens {
1599e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1600468c413aSTim Haley 	zpool_rewind_policy_t policy;
16014b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
16024b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
16034b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1604fa9e4066Sahrens 	char *thename;
1605fa9e4066Sahrens 	char *origname;
1606fa9e4066Sahrens 	int ret;
16074b964adaSGeorge Wilson 	int error = 0;
1608990b4856Slling 	char errbuf[1024];
1609fa9e4066Sahrens 
1610fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1611fa9e4066Sahrens 	    &origname) == 0);
1612fa9e4066Sahrens 
1613990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1614990b4856Slling 	    "cannot import pool '%s'"), origname);
1615990b4856Slling 
1616fa9e4066Sahrens 	if (newname != NULL) {
161799653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1618ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
161999653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
162099653d4eSeschrock 			    newname));
1621fa9e4066Sahrens 		thename = (char *)newname;
1622fa9e4066Sahrens 	} else {
1623fa9e4066Sahrens 		thename = origname;
1624fa9e4066Sahrens 	}
1625fa9e4066Sahrens 
1626078266a5SMarcel Telka 	if (props != NULL) {
1627990b4856Slling 		uint64_t version;
1628f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1629990b4856Slling 
1630990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1631990b4856Slling 		    &version) == 0);
1632990b4856Slling 
16330a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1634078266a5SMarcel Telka 		    props, version, flags, errbuf)) == NULL)
1635990b4856Slling 			return (-1);
1636078266a5SMarcel Telka 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1637351420b3Slling 			nvlist_free(props);
1638990b4856Slling 			return (-1);
1639990b4856Slling 		}
1640078266a5SMarcel Telka 		nvlist_free(props);
1641351420b3Slling 	}
1642fa9e4066Sahrens 
1643fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1644fa9e4066Sahrens 
1645fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1646ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1647fa9e4066Sahrens 
1648351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1649078266a5SMarcel Telka 		zcmd_free_nvlists(&zc);
165099653d4eSeschrock 		return (-1);
1651351420b3Slling 	}
165257f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1653078266a5SMarcel Telka 		zcmd_free_nvlists(&zc);
1654468c413aSTim Haley 		return (-1);
1655468c413aSTim Haley 	}
1656fa9e4066Sahrens 
16574b964adaSGeorge Wilson 	zc.zc_cookie = flags;
16584b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
16594b964adaSGeorge Wilson 	    errno == ENOMEM) {
16604b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
16614b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
16624b964adaSGeorge Wilson 			return (-1);
16634b964adaSGeorge Wilson 		}
16644b964adaSGeorge Wilson 	}
16654b964adaSGeorge Wilson 	if (ret != 0)
16664b964adaSGeorge Wilson 		error = errno;
16674b964adaSGeorge Wilson 
16684b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1669078266a5SMarcel Telka 
1670078266a5SMarcel Telka 	zcmd_free_nvlists(&zc);
1671078266a5SMarcel Telka 
16724b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
16734b964adaSGeorge Wilson 
16744b964adaSGeorge Wilson 	if (error) {
1675fa9e4066Sahrens 		char desc[1024];
1676468c413aSTim Haley 
1677468c413aSTim Haley 		/*
1678468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1679468c413aSTim Haley 		 * looks like if we found a best txg
1680468c413aSTim Haley 		 */
16814b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1682468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
16834b964adaSGeorge Wilson 			    B_TRUE, nv);
16844b964adaSGeorge Wilson 			nvlist_free(nv);
1685468c413aSTim Haley 			return (-1);
1686468c413aSTim Haley 		}
1687468c413aSTim Haley 
1688fa9e4066Sahrens 		if (newname == NULL)
1689fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1690fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1691fa9e4066Sahrens 			    thename);
1692fa9e4066Sahrens 		else
1693fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1694fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1695fa9e4066Sahrens 			    origname, thename);
1696fa9e4066Sahrens 
16974b964adaSGeorge Wilson 		switch (error) {
1698ea8dc4b6Seschrock 		case ENOTSUP:
1699ad135b5dSChristopher Siden 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1700ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1701ad135b5dSChristopher Siden 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1702ad135b5dSChristopher Siden 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1703ad135b5dSChristopher Siden 				    "pool uses the following feature(s) not "
1704ad135b5dSChristopher Siden 				    "supported by this system:\n"));
1705ad135b5dSChristopher Siden 				zpool_print_unsup_feat(nv);
1706ad135b5dSChristopher Siden 				if (nvlist_exists(nvinfo,
1707ad135b5dSChristopher Siden 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1708ad135b5dSChristopher Siden 					(void) printf(dgettext(TEXT_DOMAIN,
1709ad135b5dSChristopher Siden 					    "All unsupported features are only "
1710ad135b5dSChristopher Siden 					    "required for writing to the pool."
1711ad135b5dSChristopher Siden 					    "\nThe pool can be imported using "
1712ad135b5dSChristopher Siden 					    "'-o readonly=on'.\n"));
1713ad135b5dSChristopher Siden 				}
1714ad135b5dSChristopher Siden 			}
1715ea8dc4b6Seschrock 			/*
1716ea8dc4b6Seschrock 			 * Unsupported version.
1717ea8dc4b6Seschrock 			 */
171899653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1719ea8dc4b6Seschrock 			break;
1720ea8dc4b6Seschrock 
1721b5989ec7Seschrock 		case EINVAL:
1722b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1723b5989ec7Seschrock 			break;
1724b5989ec7Seschrock 
172554a91118SChris Kirby 		case EROFS:
172654a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172754a91118SChris Kirby 			    "one or more devices is read only"));
172854a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
172954a91118SChris Kirby 			break;
173054a91118SChris Kirby 
17314b964adaSGeorge Wilson 		case ENXIO:
17324b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
17334b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
17344b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
17354b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
17364b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
17374b964adaSGeorge Wilson 				    "The devices below are missing, use "
17384b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
17394b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
17404b964adaSGeorge Wilson 				(void) printf("\n");
17414b964adaSGeorge Wilson 			}
17424b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17434b964adaSGeorge Wilson 			break;
17444b964adaSGeorge Wilson 
17454b964adaSGeorge Wilson 		case EEXIST:
17464b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17474b964adaSGeorge Wilson 			break;
17484b964adaSGeorge Wilson 
1749fa9e4066Sahrens 		default:
17504b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1751468c413aSTim Haley 			zpool_explain_recover(hdl,
17524b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1753468c413aSTim Haley 			break;
1754fa9e4066Sahrens 		}
1755fa9e4066Sahrens 
17564b964adaSGeorge Wilson 		nvlist_free(nv);
1757fa9e4066Sahrens 		ret = -1;
1758fa9e4066Sahrens 	} else {
1759fa9e4066Sahrens 		zpool_handle_t *zhp;
1760ecd6cf80Smarks 
1761fa9e4066Sahrens 		/*
1762fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1763fa9e4066Sahrens 		 */
1764681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
176594de1d4cSeschrock 			ret = -1;
1766681d9761SEric Taylor 		else if (zhp != NULL)
1767fa9e4066Sahrens 			zpool_close(zhp);
1768468c413aSTim Haley 		if (policy.zrp_request &
1769468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1770468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
17714b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1772468c413aSTim Haley 		}
17734b964adaSGeorge Wilson 		nvlist_free(nv);
1774468c413aSTim Haley 		return (0);
1775fa9e4066Sahrens 	}
1776ecd6cf80Smarks 
1777fa9e4066Sahrens 	return (ret);
1778fa9e4066Sahrens }
1779fa9e4066Sahrens 
1780fa9e4066Sahrens /*
17813f9d6ad7SLin Ling  * Scan the pool.
1782fa9e4066Sahrens  */
1783fa9e4066Sahrens int
zpool_scan(zpool_handle_t * zhp,pool_scan_func_t func)17843f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1785fa9e4066Sahrens {
1786fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1787fa9e4066Sahrens 	char msg[1024];
178899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1789fa9e4066Sahrens 
1790fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
17913f9d6ad7SLin Ling 	zc.zc_cookie = func;
1792fa9e4066Sahrens 
1793cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
17943f9d6ad7SLin Ling 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1795fa9e4066Sahrens 		return (0);
1796fa9e4066Sahrens 
17973f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
1798fa9e4066Sahrens 		(void) snprintf(msg, sizeof (msg),
1799fa9e4066Sahrens 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
18003f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
18013f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18023f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
18033f9d6ad7SLin Ling 		    zc.zc_name);
18043f9d6ad7SLin Ling 	} else {
18053f9d6ad7SLin Ling 		assert(!"unexpected result");
18063f9d6ad7SLin Ling 	}
1807fa9e4066Sahrens 
18083f9d6ad7SLin Ling 	if (errno == EBUSY) {
18093f9d6ad7SLin Ling 		nvlist_t *nvroot;
18103f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
18113f9d6ad7SLin Ling 		uint_t psc;
18123f9d6ad7SLin Ling 
18133f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
18143f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
18153f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
18163f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
18173f9d6ad7SLin Ling 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
18183f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
181999653d4eSeschrock 		else
18203f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
18213f9d6ad7SLin Ling 	} else if (errno == ENOENT) {
18223f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
18233f9d6ad7SLin Ling 	} else {
182499653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
1825fa9e4066Sahrens 	}
18263f9d6ad7SLin Ling }
1827fa9e4066Sahrens 
1828a43d325bSek110237 /*
18293fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
18303fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
18313fdda499SJohn Harres  * verification of the s# part.
18323fdda499SJohn Harres  */
18333fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
18343fdda499SJohn Harres 
18353fdda499SJohn Harres /*
18363fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
18373fdda499SJohn Harres  * and the like.
18383fdda499SJohn Harres  */
18393fdda499SJohn Harres static int
ctd_check_path(char * str)18403fdda499SJohn Harres ctd_check_path(char *str) {
18413fdda499SJohn Harres 	/*
18423fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
18433fdda499SJohn Harres 	 */
18443fdda499SJohn Harres 	if (str && str[0] == '/') {
18453fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
18463fdda499SJohn Harres 
18473fdda499SJohn Harres 		/*
18483fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
18493fdda499SJohn Harres 		 * component of the string instead.
18503fdda499SJohn Harres 		 */
18513fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
18523fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
18533fdda499SJohn Harres 				;
18543fdda499SJohn Harres 		}
18553fdda499SJohn Harres 		str = tmp + 1;
18563fdda499SJohn Harres 	}
18573fdda499SJohn Harres 	return (CTD_CHECK(str));
18583fdda499SJohn Harres }
18593fdda499SJohn Harres 
18603fdda499SJohn Harres /*
1861573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1862573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1863a43d325bSek110237  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1864a43d325bSek110237  * spare; but FALSE if its an INUSE spare.
1865a43d325bSek110237  */
186699653d4eSeschrock static nvlist_t *
vdev_to_nvlist_iter(nvlist_t * nv,nvlist_t * search,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)1867573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1868573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1869ea8dc4b6Seschrock {
1870ea8dc4b6Seschrock 	uint_t c, children;
1871ea8dc4b6Seschrock 	nvlist_t **child;
187299653d4eSeschrock 	nvlist_t *ret;
1873ee0eb9f2SEric Schrock 	uint64_t is_log;
1874573ca77eSGeorge Wilson 	char *srchkey;
1875573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1876ea8dc4b6Seschrock 
1877573ca77eSGeorge Wilson 	/* Nothing to look for */
1878573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1879573ca77eSGeorge Wilson 		return (NULL);
1880ea8dc4b6Seschrock 
1881573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1882573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1883573ca77eSGeorge Wilson 
1884573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1885cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1886cb04b873SMark J Musante 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1887cb04b873SMark J Musante 			uint64_t srchval, theguid;
1888573ca77eSGeorge Wilson 
1889573ca77eSGeorge Wilson 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1890cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1891cb04b873SMark J Musante 			    &theguid) == 0);
1892573ca77eSGeorge Wilson 			if (theguid == srchval)
189399653d4eSeschrock 				return (nv);
1894573ca77eSGeorge Wilson 		}
1895573ca77eSGeorge Wilson 		break;
1896573ca77eSGeorge Wilson 
1897573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1898573ca77eSGeorge Wilson 		char *srchval, *val;
1899573ca77eSGeorge Wilson 
1900573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
1901573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1902573ca77eSGeorge Wilson 			break;
1903573ca77eSGeorge Wilson 
1904573ca77eSGeorge Wilson 		/*
19053fdda499SJohn Harres 		 * Search for the requested value. Special cases:
19063fdda499SJohn Harres 		 *
19073fdda499SJohn Harres 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
19083fdda499SJohn Harres 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
19093fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
19103fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
19113fdda499SJohn Harres 		 *
191288ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
1913573ca77eSGeorge Wilson 		 */
19143fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
19153fdda499SJohn Harres 		    ctd_check_path(val)) {
1916573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
1917573ca77eSGeorge Wilson 
1918ea8dc4b6Seschrock 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1919ea8dc4b6Seschrock 			    &wholedisk);
1920ea8dc4b6Seschrock 			if (wholedisk) {
19213fdda499SJohn Harres 				int slen = strlen(srchval);
19223fdda499SJohn Harres 				int vlen = strlen(val);
19233fdda499SJohn Harres 
19243fdda499SJohn Harres 				if (slen != vlen - 2)
19253fdda499SJohn Harres 					break;
19263fdda499SJohn Harres 
1927ea8dc4b6Seschrock 				/*
19283fdda499SJohn Harres 				 * make_leaf_vdev() should only set
19293fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
19303fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
19313fdda499SJohn Harres 				 * room for the indices used next.
1932ea8dc4b6Seschrock 				 */
19333fdda499SJohn Harres 				ASSERT(vlen >= 6);
19343fdda499SJohn Harres 
19353fdda499SJohn Harres 				/*
19363fdda499SJohn Harres 				 * strings identical except trailing "s0"
19373fdda499SJohn Harres 				 */
19383fdda499SJohn Harres 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
19393fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
194099653d4eSeschrock 					return (nv);
19413fdda499SJohn Harres 
19423fdda499SJohn Harres 				/*
19433fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
19443fdda499SJohn Harres 				 */
19453fdda499SJohn Harres 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
19463fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
19473fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
19483fdda499SJohn Harres 					return (nv);
19493fdda499SJohn Harres 
1950573ca77eSGeorge Wilson 				break;
1951ea8dc4b6Seschrock 			}
195288ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
195388ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
195488ecc943SGeorge Wilson 			uint64_t id, vdev_id;
195588ecc943SGeorge Wilson 
195688ecc943SGeorge Wilson 			/*
195788ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
195888ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
195988ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
196088ecc943SGeorge Wilson 			 */
196188ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
196288ecc943SGeorge Wilson 				return (NULL);
196388ecc943SGeorge Wilson 
196488ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
196588ecc943SGeorge Wilson 				free(type);
196688ecc943SGeorge Wilson 				break;
196788ecc943SGeorge Wilson 			}
196888ecc943SGeorge Wilson 			idx = p + 1;
196988ecc943SGeorge Wilson 			*p = '\0';
197088ecc943SGeorge Wilson 
197188ecc943SGeorge Wilson 			/*
197288ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
197388ecc943SGeorge Wilson 			 */
197488ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
197588ecc943SGeorge Wilson 				free(type);
197688ecc943SGeorge Wilson 				break;
197788ecc943SGeorge Wilson 			}
197888ecc943SGeorge Wilson 
197988ecc943SGeorge Wilson 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
198088ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
198188ecc943SGeorge Wilson 			    strncmp(type, VDEV_TYPE_MIRROR,
198288ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_MIRROR)) == 0);
198388ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
198488ecc943SGeorge Wilson 			    &id) == 0);
198588ecc943SGeorge Wilson 
198688ecc943SGeorge Wilson 			errno = 0;
198788ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
198888ecc943SGeorge Wilson 
198988ecc943SGeorge Wilson 			free(type);
199088ecc943SGeorge Wilson 			if (errno != 0)
199188ecc943SGeorge Wilson 				return (NULL);
199288ecc943SGeorge Wilson 
199388ecc943SGeorge Wilson 			/*
199488ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
199588ecc943SGeorge Wilson 			 */
199688ecc943SGeorge Wilson 			if (vdev_id == id)
199788ecc943SGeorge Wilson 				return (nv);
1998ea8dc4b6Seschrock 		}
1999ea8dc4b6Seschrock 
2000573ca77eSGeorge Wilson 		/*
2001573ca77eSGeorge Wilson 		 * Common case
2002573ca77eSGeorge Wilson 		 */
2003573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
2004573ca77eSGeorge Wilson 			return (nv);
2005573ca77eSGeorge Wilson 		break;
2006573ca77eSGeorge Wilson 	}
2007573ca77eSGeorge Wilson 
2008573ca77eSGeorge Wilson 	default:
2009573ca77eSGeorge Wilson 		break;
2010573ca77eSGeorge Wilson 	}
2011573ca77eSGeorge Wilson 
2012ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2013ea8dc4b6Seschrock 	    &child, &children) != 0)
201499653d4eSeschrock 		return (NULL);
2015ea8dc4b6Seschrock 
2016ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
2017573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2018ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
2019ee0eb9f2SEric Schrock 			/*
2020ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
2021ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
2022ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
2023ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
2024ee0eb9f2SEric Schrock 			 */
2025ee0eb9f2SEric Schrock 			if (log != NULL &&
2026ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
2027ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2028ee0eb9f2SEric Schrock 			    is_log) {
2029ee0eb9f2SEric Schrock 				*log = B_TRUE;
2030ee0eb9f2SEric Schrock 			}
2031ea8dc4b6Seschrock 			return (ret);
2032ee0eb9f2SEric Schrock 		}
2033ee0eb9f2SEric Schrock 	}
2034ea8dc4b6Seschrock 
203599653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
203699653d4eSeschrock 	    &child, &children) == 0) {
203799653d4eSeschrock 		for (c = 0; c < children; c++) {
2038573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2039ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2040a43d325bSek110237 				*avail_spare = B_TRUE;
204199653d4eSeschrock 				return (ret);
204299653d4eSeschrock 			}
204399653d4eSeschrock 		}
2044ea8dc4b6Seschrock 	}
2045ea8dc4b6Seschrock 
2046fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2047fa94a07fSbrendan 	    &child, &children) == 0) {
2048fa94a07fSbrendan 		for (c = 0; c < children; c++) {
2049573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2050ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2051fa94a07fSbrendan 				*l2cache = B_TRUE;
2052fa94a07fSbrendan 				return (ret);
2053fa94a07fSbrendan 			}
2054fa94a07fSbrendan 		}
2055fa94a07fSbrendan 	}
2056fa94a07fSbrendan 
205799653d4eSeschrock 	return (NULL);
205899653d4eSeschrock }
205999653d4eSeschrock 
2060573ca77eSGeorge Wilson /*
2061573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
2062573ca77eSGeorge Wilson  * associated vdev.
2063573ca77eSGeorge Wilson  */
2064573ca77eSGeorge Wilson nvlist_t *
zpool_find_vdev_by_physpath(zpool_handle_t * zhp,const char * ppath,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)2065573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2066573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2067573ca77eSGeorge Wilson {
2068573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
2069573ca77eSGeorge Wilson 
2070573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2071573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2072573ca77eSGeorge Wilson 
2073573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2074573ca77eSGeorge Wilson 	    &nvroot) == 0);
2075573ca77eSGeorge Wilson 
2076573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
2077cb04b873SMark J Musante 	*l2cache = B_FALSE;
2078daeb70e5SMark J Musante 	if (log != NULL)
2079cb04b873SMark J Musante 		*log = B_FALSE;
2080573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2081573ca77eSGeorge Wilson 	nvlist_free(search);
2082573ca77eSGeorge Wilson 
2083573ca77eSGeorge Wilson 	return (ret);
2084573ca77eSGeorge Wilson }
2085573ca77eSGeorge Wilson 
208688ecc943SGeorge Wilson /*
208788ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
208888ecc943SGeorge Wilson  */
208988ecc943SGeorge Wilson boolean_t
zpool_vdev_is_interior(const char * name)209088ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
209188ecc943SGeorge Wilson {
209288ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
209388ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
209488ecc943SGeorge Wilson 		return (B_TRUE);
209588ecc943SGeorge Wilson 	return (B_FALSE);
209688ecc943SGeorge Wilson }
209788ecc943SGeorge Wilson 
209899653d4eSeschrock nvlist_t *
zpool_find_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)2099fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2100ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
2101ea8dc4b6Seschrock {
2102ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
2103ea8dc4b6Seschrock 	char *end;
2104573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
2105ea8dc4b6Seschrock 	uint64_t guid;
2106ea8dc4b6Seschrock 
2107573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2108573ca77eSGeorge Wilson 
21090917b783Seschrock 	guid = strtoull(path, &end, 10);
2110ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
2111573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
211288ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
211388ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2114ea8dc4b6Seschrock 	} else if (path[0] != '/') {
2115ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
2116573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2117ea8dc4b6Seschrock 	} else {
2118573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2119ea8dc4b6Seschrock 	}
2120ea8dc4b6Seschrock 
2121ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2122ea8dc4b6Seschrock 	    &nvroot) == 0);
2123ea8dc4b6Seschrock 
2124a43d325bSek110237 	*avail_spare = B_FALSE;
2125fa94a07fSbrendan 	*l2cache = B_FALSE;
2126ee0eb9f2SEric Schrock 	if (log != NULL)
2127ee0eb9f2SEric Schrock 		*log = B_FALSE;
2128573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2129573ca77eSGeorge Wilson 	nvlist_free(search);
2130573ca77eSGeorge Wilson 
2131573ca77eSGeorge Wilson 	return (ret);
2132a43d325bSek110237 }
2133a43d325bSek110237 
213419397407SSherry Moore static int
vdev_online(nvlist_t * nv)213519397407SSherry Moore vdev_online(nvlist_t *nv)
213619397407SSherry Moore {
213719397407SSherry Moore 	uint64_t ival;
213819397407SSherry Moore 
213919397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
214019397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
214119397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
214219397407SSherry Moore 		return (0);
214319397407SSherry Moore 
214419397407SSherry Moore 	return (1);
214519397407SSherry Moore }
214619397407SSherry Moore 
214719397407SSherry Moore /*
214821ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
214919397407SSherry Moore  */
2150753a6d45SSherry Moore static int
vdev_get_one_physpath(nvlist_t * config,char * physpath,size_t physpath_size,size_t * bytes_written)215121ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2152753a6d45SSherry Moore     size_t *bytes_written)
215319397407SSherry Moore {
2154753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
2155753a6d45SSherry Moore 	char *tmppath;
2156753a6d45SSherry Moore 	const char *format;
2157753a6d45SSherry Moore 
2158753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2159753a6d45SSherry Moore 	    &tmppath) != 0)
2160753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2161753a6d45SSherry Moore 
2162753a6d45SSherry Moore 	pos = *bytes_written;
2163753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
2164753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
2165753a6d45SSherry Moore 
2166753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2167753a6d45SSherry Moore 	*bytes_written += rsz;
2168753a6d45SSherry Moore 
2169753a6d45SSherry Moore 	if (rsz >= bytes_left) {
2170753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2171753a6d45SSherry Moore 		if (bytes_left != 0) {
2172753a6d45SSherry Moore 			physpath[pos] = 0;
2173753a6d45SSherry Moore 		}
2174753a6d45SSherry Moore 		return (EZFS_NOSPC);
2175753a6d45SSherry Moore 	}
2176753a6d45SSherry Moore 	return (0);
2177753a6d45SSherry Moore }
217819397407SSherry Moore 
217921ecdf64SLin Ling static int
vdev_get_physpaths(nvlist_t * nv,char * physpath,size_t phypath_size,size_t * rsz,boolean_t is_spare)218021ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
218121ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
218221ecdf64SLin Ling {
218321ecdf64SLin Ling 	char *type;
218421ecdf64SLin Ling 	int ret;
218521ecdf64SLin Ling 
218621ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
218721ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
218821ecdf64SLin Ling 
218921ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
219021ecdf64SLin Ling 		/*
219121ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
219221ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
219321ecdf64SLin Ling 		 * spare device.
219421ecdf64SLin Ling 		 */
219521ecdf64SLin Ling 		if (is_spare) {
219621ecdf64SLin Ling 			uint64_t spare = 0;
219721ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
219821ecdf64SLin Ling 			    &spare);
219921ecdf64SLin Ling 			if (!spare)
220021ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
220121ecdf64SLin Ling 		}
220221ecdf64SLin Ling 
220321ecdf64SLin Ling 		if (vdev_online(nv)) {
220421ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
220521ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
220621ecdf64SLin Ling 				return (ret);
220721ecdf64SLin Ling 		}
220821ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
220921ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
221021ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
221121ecdf64SLin Ling 		nvlist_t **child;
221221ecdf64SLin Ling 		uint_t count;
221321ecdf64SLin Ling 		int i, ret;
221421ecdf64SLin Ling 
221521ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
221621ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
221721ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
221821ecdf64SLin Ling 
221921ecdf64SLin Ling 		for (i = 0; i < count; i++) {
222021ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
222121ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
222221ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
222321ecdf64SLin Ling 				return (ret);
222421ecdf64SLin Ling 		}
222521ecdf64SLin Ling 	}
222621ecdf64SLin Ling 
222721ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
222821ecdf64SLin Ling }
222921ecdf64SLin Ling 
223019397407SSherry Moore /*
2231753a6d45SSherry Moore  * Get phys_path for a root pool config.
2232753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
223319397407SSherry Moore  */
2234753a6d45SSherry Moore static int
zpool_get_config_physpath(nvlist_t * config,char * physpath,size_t phypath_size)2235753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2236753a6d45SSherry Moore {
2237753a6d45SSherry Moore 	size_t rsz;
2238753a6d45SSherry Moore 	nvlist_t *vdev_root;
2239753a6d45SSherry Moore 	nvlist_t **child;
2240753a6d45SSherry Moore 	uint_t count;
224119397407SSherry Moore 	char *type;
224219397407SSherry Moore 
2243753a6d45SSherry Moore 	rsz = 0;
2244753a6d45SSherry Moore 
2245753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2246753a6d45SSherry Moore 	    &vdev_root) != 0)
2247753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2248753a6d45SSherry Moore 
2249753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2250753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2251753a6d45SSherry Moore 	    &child, &count) != 0)
2252753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2253753a6d45SSherry Moore 
2254753a6d45SSherry Moore 	/*
22551a902ef8SHans Rosenfeld 	 * root pool can only have a single top-level vdev.
2256753a6d45SSherry Moore 	 */
22571a902ef8SHans Rosenfeld 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2258753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
2259753a6d45SSherry Moore 
226021ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
226121ecdf64SLin Ling 	    B_FALSE);
2262753a6d45SSherry Moore 
2263753a6d45SSherry Moore 	/* No online devices */
2264753a6d45SSherry Moore 	if (rsz == 0)
2265753a6d45SSherry Moore 		return (EZFS_NODEVICE);
226619397407SSherry Moore 
226719397407SSherry Moore 	return (0);
226819397407SSherry Moore }
226919397407SSherry Moore 
2270a43d325bSek110237 /*
2271753a6d45SSherry Moore  * Get phys_path for a root pool
2272753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2273753a6d45SSherry Moore  */
2274753a6d45SSherry Moore int
zpool_get_physpath(zpool_handle_t * zhp,char * physpath,size_t phypath_size)2275753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2276753a6d45SSherry Moore {
2277753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2278753a6d45SSherry Moore 	    phypath_size));
2279753a6d45SSherry Moore }
2280753a6d45SSherry Moore 
2281753a6d45SSherry Moore /*
2282573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2283573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2284573ca77eSGeorge Wilson  */
2285573ca77eSGeorge Wilson static int
zpool_relabel_disk(libzfs_handle_t * hdl,const char * name)2286573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2287573ca77eSGeorge Wilson {
2288573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2289573ca77eSGeorge Wilson 	char errbuf[1024];
2290573ca77eSGeorge Wilson 	int fd, error;
2291573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2292573ca77eSGeorge Wilson 
2293573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2294573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2295573ca77eSGeorge Wilson 		return (-1);
2296573ca77eSGeorge Wilson 
2297573ca77eSGeorge Wilson 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2298573ca77eSGeorge Wilson 
2299573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2300573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2301573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2302573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2303573ca77eSGeorge Wilson 	}
2304573ca77eSGeorge Wilson 
2305573ca77eSGeorge Wilson 	/*
2306573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2307573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2308573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2309573ca77eSGeorge Wilson 	 */
2310573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2311573ca77eSGeorge Wilson 	(void) close(fd);
2312573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2313573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2314573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2315573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2316573ca77eSGeorge Wilson 	}
2317573ca77eSGeorge Wilson 	return (0);
2318573ca77eSGeorge Wilson }
2319573ca77eSGeorge Wilson 
2320573ca77eSGeorge Wilson /*
23213d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
23223d7072f8Seschrock  * ZFS_ONLINE_* flags.
2323fa9e4066Sahrens  */
2324fa9e4066Sahrens int
zpool_vdev_online(zpool_handle_t * zhp,const char * path,int flags,vdev_state_t * newstate)23253d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
23263d7072f8Seschrock     vdev_state_t *newstate)
2327fa9e4066Sahrens {
2328fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2329fa9e4066Sahrens 	char msg[1024];
233099653d4eSeschrock 	nvlist_t *tgt;
2331573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
233299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2333fa9e4066Sahrens 
2334573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2335573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2336573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2337573ca77eSGeorge Wilson 	} else {
2338fa9e4066Sahrens 		(void) snprintf(msg, sizeof (msg),
2339ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2340573ca77eSGeorge Wilson 	}
2341ea8dc4b6Seschrock 
2342ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2343ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2344573ca77eSGeorge Wilson 	    &islog)) == NULL)
234599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2346ea8dc4b6Seschrock 
234799653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
234899653d4eSeschrock 
2349069f55e2SEric Schrock 	if (avail_spare)
2350a43d325bSek110237 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2351a43d325bSek110237 
2352573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND ||
2353573ca77eSGeorge Wilson 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2354573ca77eSGeorge Wilson 		char *pathname = NULL;
2355573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2356573ca77eSGeorge Wilson 
2357573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2358573ca77eSGeorge Wilson 		    &wholedisk);
2359573ca77eSGeorge Wilson 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2360573ca77eSGeorge Wilson 		    &pathname) == 0);
2361573ca77eSGeorge Wilson 
2362573ca77eSGeorge Wilson 		/*
2363573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2364573ca77eSGeorge Wilson 		 */
2365573ca77eSGeorge Wilson 		if (l2cache) {
2366573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2367573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2368573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2369573ca77eSGeorge Wilson 		}
2370573ca77eSGeorge Wilson 
2371573ca77eSGeorge Wilson 		if (wholedisk) {
2372573ca77eSGeorge Wilson 			pathname += strlen(DISK_ROOT) + 1;
2373cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2374573ca77eSGeorge Wilson 		}
2375573ca77eSGeorge Wilson 	}
2376573ca77eSGeorge Wilson 
23773d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
23783d7072f8Seschrock 	zc.zc_obj = flags;
2379fa9e4066Sahrens 
2380cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
23811195e687SMark J Musante 		if (errno == EINVAL) {
23821195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
23831195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
23841195e687SMark J Musante 			    "instead"), "zpool detach");
23851195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
23861195e687SMark J Musante 		}
238799653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
23881195e687SMark J Musante 	}
23893d7072f8Seschrock 
23903d7072f8Seschrock 	*newstate = zc.zc_cookie;
23913d7072f8Seschrock 	return (0);
2392fa9e4066Sahrens }
2393fa9e4066Sahrens 
2394fa9e4066Sahrens /*
2395fa9e4066Sahrens  * Take the specified vdev offline
2396fa9e4066Sahrens  */
2397fa9e4066Sahrens int
zpool_vdev_offline(zpool_handle_t * zhp,const char * path,boolean_t istmp)23983d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2399fa9e4066Sahrens {
2400fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2401fa9e4066Sahrens 	char msg[1024];
240299653d4eSeschrock 	nvlist_t *tgt;
2403fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
240499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2405fa9e4066Sahrens 
2406ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2407ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2408ea8dc4b6Seschrock 
2409fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2410ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2411ee0eb9f2SEric Schrock 	    NULL)) == NULL)
241299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
241399653d4eSeschrock 
241499653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2415fa9e4066Sahrens 
2416069f55e2SEric Schrock 	if (avail_spare)
2417a43d325bSek110237 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2418a43d325bSek110237 
24193d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
24203d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2421441d80aaSlling 
2422cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2423fa9e4066Sahrens 		return (0);
2424fa9e4066Sahrens 
2425fa9e4066Sahrens 	switch (errno) {
2426fa9e4066Sahrens 	case EBUSY:
242799653d4eSeschrock 
2428fa9e4066Sahrens 		/*
2429fa9e4066Sahrens 		 * There are no other replicas of this device.
2430fa9e4066Sahrens 		 */
243199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2432fa9e4066Sahrens 
2433e6ca193dSGeorge Wilson 	case EEXIST:
2434e6ca193dSGeorge Wilson 		/*
2435e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2436e6ca193dSGeorge Wilson 		 */
2437e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2438e6ca193dSGeorge Wilson 
2439fa9e4066Sahrens 	default:
244099653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2441fa9e4066Sahrens 	}
244299653d4eSeschrock }
244399653d4eSeschrock 
244499653d4eSeschrock /*
24453d7072f8Seschrock  * Mark the given vdev faulted.
24463d7072f8Seschrock  */
24473d7072f8Seschrock int
zpool_vdev_fault(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)2448069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
24493d7072f8Seschrock {
24503d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
24513d7072f8Seschrock 	char msg[1024];
24523d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24533d7072f8Seschrock 
24543d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
24553d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
24563d7072f8Seschrock 
24573d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24583d7072f8Seschrock 	zc.zc_guid = guid;
24593d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2460069f55e2SEric Schrock 	zc.zc_obj = aux;
24613d7072f8Seschrock 
2462cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24633d7072f8Seschrock 		return (0);
24643d7072f8Seschrock 
24653d7072f8Seschrock 	switch (errno) {
24663d7072f8Seschrock 	case EBUSY:
24673d7072f8Seschrock 
24683d7072f8Seschrock 		/*
24693d7072f8Seschrock 		 * There are no other replicas of this device.
24703d7072f8Seschrock 		 */
24713d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24723d7072f8Seschrock 
24733d7072f8Seschrock 	default:
24743d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24753d7072f8Seschrock 	}
24763d7072f8Seschrock 
24773d7072f8Seschrock }
24783d7072f8Seschrock 
24793d7072f8Seschrock /*
24803d7072f8Seschrock  * Mark the given vdev degraded.
24813d7072f8Seschrock  */
24823d7072f8Seschrock int
zpool_vdev_degrade(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)2483069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
24843d7072f8Seschrock {
24853d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
24863d7072f8Seschrock 	char msg[1024];
24873d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
24883d7072f8Seschrock 
24893d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
24903d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
24913d7072f8Seschrock 
24923d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
24933d7072f8Seschrock 	zc.zc_guid = guid;
24943d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2495069f55e2SEric Schrock 	zc.zc_obj = aux;
24963d7072f8Seschrock 
2497cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24983d7072f8Seschrock 		return (0);
24993d7072f8Seschrock 
25003d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
25013d7072f8Seschrock }
25023d7072f8Seschrock 
25033d7072f8Seschrock /*
250499653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
250599653d4eSeschrock  * a hot spare.
250699653d4eSeschrock  */
250799653d4eSeschrock static boolean_t
is_replacing_spare(nvlist_t * search,nvlist_t * tgt,int which)250899653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
250999653d4eSeschrock {
251099653d4eSeschrock 	nvlist_t **child;
251199653d4eSeschrock 	uint_t c, children;
251299653d4eSeschrock 	char *type;
251399653d4eSeschrock 
251499653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
251599653d4eSeschrock 	    &children) == 0) {
251699653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
251799653d4eSeschrock 		    &type) == 0);
251899653d4eSeschrock 
251999653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
252099653d4eSeschrock 		    children == 2 && child[which] == tgt)
252199653d4eSeschrock 			return (B_TRUE);
252299653d4eSeschrock 
252399653d4eSeschrock 		for (c = 0; c < children; c++)
252499653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
252599653d4eSeschrock 				return (B_TRUE);
252699653d4eSeschrock 	}
252799653d4eSeschrock 
252899653d4eSeschrock 	return (B_FALSE);
2529fa9e4066Sahrens }
2530fa9e4066Sahrens 
2531fa9e4066Sahrens /*
2532fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
25338654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2534fa9e4066Sahrens  */
2535fa9e4066Sahrens int
zpool_vdev_attach(zpool_handle_t * zhp,const char * old_disk,const char * new_disk,nvlist_t * nvroot,int replacing)2536fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2537fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2538fa9e4066Sahrens {
2539fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2540fa9e4066Sahrens 	char msg[1024];
2541fa9e4066Sahrens 	int ret;
254299653d4eSeschrock 	nvlist_t *tgt;
2543ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2544ee0eb9f2SEric Schrock 	uint64_t val;
2545cb04b873SMark J Musante 	char *newname;
254699653d4eSeschrock 	nvlist_t **child;
254799653d4eSeschrock 	uint_t children;
254899653d4eSeschrock 	nvlist_t *config_root;
254999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25504263d13fSGeorge Wilson 	boolean_t rootpool = zpool_is_bootable(zhp);
2551fa9e4066Sahrens 
2552ea8dc4b6Seschrock 	if (replacing)
2553ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2554ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2555ea8dc4b6Seschrock 	else
2556ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2557ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2558ea8dc4b6Seschrock 
2559fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2560ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2561ee0eb9f2SEric Schrock 	    &islog)) == 0)
256299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
256399653d4eSeschrock 
2564a43d325bSek110237 	if (avail_spare)
256599653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
256699653d4eSeschrock 
2567fa94a07fSbrendan 	if (l2cache)
2568fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2569fa94a07fSbrendan 
257099653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2571fa9e4066Sahrens 	zc.zc_cookie = replacing;
2572fa9e4066Sahrens 
257399653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
257499653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
257599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
257699653d4eSeschrock 		    "new device must be a single disk"));
257799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
257899653d4eSeschrock 	}
257999653d4eSeschrock 
258099653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
258199653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
258299653d4eSeschrock 
258388ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
25840430f8daSeschrock 		return (-1);
25850430f8daSeschrock 
258699653d4eSeschrock 	/*
258799653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
258899653d4eSeschrock 	 * replace it with another hot spare.
258999653d4eSeschrock 	 */
259099653d4eSeschrock 	if (replacing &&
259199653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2592ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2593ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2594ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
259599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
259699653d4eSeschrock 		    "can only be replaced by another hot spare"));
25970430f8daSeschrock 		free(newname);
259899653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
259999653d4eSeschrock 	}
260099653d4eSeschrock 
26010430f8daSeschrock 	free(newname);
26020430f8daSeschrock 
2603990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
260499653d4eSeschrock 		return (-1);
2605fa9e4066Sahrens 
2606cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2607fa9e4066Sahrens 
2608e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2609fa9e4066Sahrens 
2610b5b76fecSGeorge Wilson 	if (ret == 0) {
2611b5b76fecSGeorge Wilson 		if (rootpool) {
2612b5b76fecSGeorge Wilson 			/*
261321ecdf64SLin Ling 			 * XXX need a better way to prevent user from
261421ecdf64SLin Ling 			 * booting up a half-baked vdev.
261521ecdf64SLin Ling 			 */
261621ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
261721ecdf64SLin Ling 			    "sure to wait until resilver is done "
261821ecdf64SLin Ling 			    "before rebooting.\n"));
2619b5b76fecSGeorge Wilson 		}
2620fa9e4066Sahrens 		return (0);
2621b5b76fecSGeorge Wilson 	}
2622fa9e4066Sahrens 
2623fa9e4066Sahrens 	switch (errno) {
2624fa9e4066Sahrens 	case ENOTSUP:
2625fa9e4066Sahrens 		/*
2626fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2627fa9e4066Sahrens 		 */
26288654d025Sperrin 		if (replacing) {
2629cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2630cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2631cb04b873SMark J Musante 
2632ee0eb9f2SEric Schrock 			if (islog)
26338654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26348654d025Sperrin 				    "cannot replace a log with a spare"));
2635cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2636cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2637cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2638cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
26398654d025Sperrin 			else
264099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
264199653d4eSeschrock 				    "cannot replace a replacing device"));
26428654d025Sperrin 		} else {
264399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
264499653d4eSeschrock 			    "can only attach to mirrors and top-level "
264599653d4eSeschrock 			    "disks"));
26468654d025Sperrin 		}
264799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2648fa9e4066Sahrens 		break;
2649fa9e4066Sahrens 
2650fa9e4066Sahrens 	case EINVAL:
2651fa9e4066Sahrens 		/*
2652fa9e4066Sahrens 		 * The new device must be a single disk.
2653fa9e4066Sahrens 		 */
265499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
265599653d4eSeschrock 		    "new device must be a single disk"));
265699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2657fa9e4066Sahrens 		break;
2658fa9e4066Sahrens 
2659fa9e4066Sahrens 	case EBUSY:
266099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
266199653d4eSeschrock 		    new_disk);
266299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2663fa9e4066Sahrens 		break;
2664fa9e4066Sahrens 
2665fa9e4066Sahrens 	case EOVERFLOW:
2666fa9e4066Sahrens 		/*
2667fa9e4066Sahrens 		 * The new device is too small.
2668fa9e4066Sahrens 		 */
266999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
267099653d4eSeschrock 		    "device is too small"));
267199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2672fa9e4066Sahrens 		break;
2673fa9e4066Sahrens 
2674fa9e4066Sahrens 	case EDOM:
2675fa9e4066Sahrens 		/*
2676fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2677fa9e4066Sahrens 		 */
267899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
267999653d4eSeschrock 		    "devices have different sector alignment"));
268099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2681fa9e4066Sahrens 		break;
2682fa9e4066Sahrens 
2683fa9e4066Sahrens 	case ENAMETOOLONG:
2684fa9e4066Sahrens 		/*
2685fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2686fa9e4066Sahrens 		 */
268799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2688fa9e4066Sahrens 		break;
2689fa9e4066Sahrens 
2690fa9e4066Sahrens 	default:
269199653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2692fa9e4066Sahrens 	}
2693fa9e4066Sahrens 
269499653d4eSeschrock 	return (-1);
2695fa9e4066Sahrens }
2696fa9e4066Sahrens 
2697fa9e4066Sahrens /*
2698fa9e4066Sahrens  * Detach the specified device.
2699fa9e4066Sahrens  */
2700fa9e4066Sahrens int
zpool_vdev_detach(zpool_handle_t * zhp,const char * path)2701fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2702fa9e4066Sahrens {
2703fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2704fa9e4066Sahrens 	char msg[1024];
270599653d4eSeschrock 	nvlist_t *tgt;
2706fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
270799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2708fa9e4066Sahrens 
2709fa9e4066Sahrens 	(void) snprintf(msg, sizeof (msg),
2710ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2711ea8dc4b6Seschrock 
2712ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2713ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2714ee0eb9f2SEric Schrock 	    NULL)) == 0)
271599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2716ea8dc4b6Seschrock 
2717a43d325bSek110237 	if (avail_spare)
271899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
271999653d4eSeschrock 
2720fa94a07fSbrendan 	if (l2cache)
2721fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2722fa94a07fSbrendan 
272399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
272499653d4eSeschrock 
2725ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2726ea8dc4b6Seschrock 		return (0);
2727fa9e4066Sahrens 
2728fa9e4066Sahrens 	switch (errno) {
2729fa9e4066Sahrens 
2730fa9e4066Sahrens 	case ENOTSUP:
2731fa9e4066Sahrens 		/*
2732fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2733fa9e4066Sahrens 		 */
273499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
273599653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2736cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2737fa9e4066Sahrens 		break;
2738fa9e4066Sahrens 
2739fa9e4066Sahrens 	case EBUSY:
2740fa9e4066Sahrens 		/*
2741fa9e4066Sahrens 		 * There are no other replicas of this device.
2742fa9e4066Sahrens 		 */
274399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2744fa9e4066Sahrens 		break;
2745fa9e4066Sahrens 
2746fa9e4066Sahrens 	default:
274799653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2748fa9e4066Sahrens 	}
2749fa9e4066Sahrens 
275099653d4eSeschrock 	return (-1);
275199653d4eSeschrock }
275299653d4eSeschrock 
275399653d4eSeschrock /*
27541195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
27551195e687SMark J Musante  *
27561195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
27571195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
27581195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
27591195e687SMark J Musante  * see if any entry in the schild array matches.
27601195e687SMark J Musante  *
27611195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
27621195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
27631195e687SMark J Musante  */
27641195e687SMark J Musante static int
find_vdev_entry(zpool_handle_t * zhp,nvlist_t ** mchild,uint_t mchildren,nvlist_t ** schild,uint_t schildren)27651195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
27661195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
27671195e687SMark J Musante {
27681195e687SMark J Musante 	uint_t mc;
27691195e687SMark J Musante 
27701195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
27711195e687SMark J Musante 		uint_t sc;
27721195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
27731195e687SMark J Musante 		    mchild[mc], B_FALSE);
27741195e687SMark J Musante 
27751195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
27761195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
27771195e687SMark J Musante 			    schild[sc], B_FALSE);
27781195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
27791195e687SMark J Musante 
27801195e687SMark J Musante 			free(spath);
27811195e687SMark J Musante 			if (result) {
27821195e687SMark J Musante 				free(mpath);
27831195e687SMark J Musante 				return (mc);
27841195e687SMark J Musante 			}
27851195e687SMark J Musante 		}
27861195e687SMark J Musante 
27871195e687SMark J Musante 		free(mpath);
27881195e687SMark J Musante 	}
27891195e687SMark J Musante 
27901195e687SMark J Musante 	return (-1);
27911195e687SMark J Musante }
27921195e687SMark J Musante 
27931195e687SMark J Musante /*
27941195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
27951195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
27961195e687SMark J Musante  */
27971195e687SMark J Musante int
zpool_vdev_split(zpool_handle_t * zhp,char * newname,nvlist_t ** newroot,nvlist_t * props,splitflags_t flags)27981195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
27991195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
28001195e687SMark J Musante {
28011195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
28021195e687SMark J Musante 	char msg[1024];
28031195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
28041195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
28051195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
28061195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
28071195e687SMark J Musante 	uint64_t vers;
28081195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
28091195e687SMark J Musante 	int retval = 0;
28101195e687SMark J Musante 
28111195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
28121195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
28131195e687SMark J Musante 
28141195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
28151195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
28161195e687SMark J Musante 
28171195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
28181195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
28191195e687SMark J Musante 		    "retrieve pool configuration\n"));
28201195e687SMark J Musante 		return (-1);
28211195e687SMark J Musante 	}
28221195e687SMark J Musante 
28231195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
28241195e687SMark J Musante 	    == 0);
28251195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
28261195e687SMark J Musante 
28271195e687SMark J Musante 	if (props) {
2828f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
28291195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2830f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
28311195e687SMark J Musante 			return (-1);
28321195e687SMark J Musante 	}
28331195e687SMark J Musante 
28341195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
28351195e687SMark J Musante 	    &children) != 0) {
28361195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28371195e687SMark J Musante 		    "Source pool is missing vdev tree"));
28381195e687SMark J Musante 		nvlist_free(zc_props);
28391195e687SMark J Musante 		return (-1);
28401195e687SMark J Musante 	}
28411195e687SMark J Musante 
28421195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
28431195e687SMark J Musante 	vcount = 0;
28441195e687SMark J Musante 
28451195e687SMark J Musante 	if (*newroot == NULL ||
28461195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
28471195e687SMark J Musante 	    &newchild, &newchildren) != 0)
28481195e687SMark J Musante 		newchildren = 0;
28491195e687SMark J Musante 
28501195e687SMark J Musante 	for (c = 0; c < children; c++) {
28511195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
28521195e687SMark J Musante 		char *type;
28531195e687SMark J Musante 		nvlist_t **mchild, *vdev;
28541195e687SMark J Musante 		uint_t mchildren;
28551195e687SMark J Musante 		int entry;
28561195e687SMark J Musante 
28571195e687SMark J Musante 		/*
28581195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
28591195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
28601195e687SMark J Musante 		 */
28611195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
28621195e687SMark J Musante 		    &is_log);
28631195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
28641195e687SMark J Musante 		    &is_hole);
28651195e687SMark J Musante 		if (is_log || is_hole) {
28661195e687SMark J Musante 			/*
28671195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
28681195e687SMark J Musante 			 */
28691195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
28701195e687SMark J Musante 				goto out;
28711195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
28721195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
28731195e687SMark J Musante 				goto out;
28741195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
28751195e687SMark J Musante 			    1) != 0)
28761195e687SMark J Musante 				goto out;
28771195e687SMark J Musante 			if (lastlog == 0)
28781195e687SMark J Musante 				lastlog = vcount;
28791195e687SMark J Musante 			varray[vcount++] = vdev;
28801195e687SMark J Musante 			continue;
28811195e687SMark J Musante 		}
28821195e687SMark J Musante 		lastlog = 0;
28831195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
28841195e687SMark J Musante 		    == 0);
28851195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
28861195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28871195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
28881195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
28891195e687SMark J Musante 			goto out;
28901195e687SMark J Musante 		}
28911195e687SMark J Musante 
28921195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
28931195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
28941195e687SMark J Musante 
28951195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
28961195e687SMark J Musante 		if (newchildren > 0 &&
28971195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
28981195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
28991195e687SMark J Musante 			/* We found a disk that the user specified. */
29001195e687SMark J Musante 			vdev = mchild[entry];
29011195e687SMark J Musante 			++found;
29021195e687SMark J Musante 		} else {
29031195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
29041195e687SMark J Musante 			vdev = mchild[mchildren - 1];
29051195e687SMark J Musante 		}
29061195e687SMark J Musante 
29071195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
29081195e687SMark J Musante 			goto out;
29091195e687SMark J Musante 	}
29101195e687SMark J Musante 
29111195e687SMark J Musante 	/* did we find every disk the user specified? */
29121195e687SMark J Musante 	if (found != newchildren) {
29131195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
29141195e687SMark J Musante 		    "include at most one disk from each mirror"));
29151195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29161195e687SMark J Musante 		goto out;
29171195e687SMark J Musante 	}
29181195e687SMark J Musante 
29191195e687SMark J Musante 	/* Prepare the nvlist for populating. */
29201195e687SMark J Musante 	if (*newroot == NULL) {
29211195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
29221195e687SMark J Musante 			goto out;
29231195e687SMark J Musante 		freelist = B_TRUE;
29241195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
29251195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
29261195e687SMark J Musante 			goto out;
29271195e687SMark J Musante 	} else {
29281195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
29291195e687SMark J Musante 	}
29301195e687SMark J Musante 
29311195e687SMark J Musante 	/* Add all the children we found */
29321195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
29331195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
29341195e687SMark J Musante 		goto out;
29351195e687SMark J Musante 
29361195e687SMark J Musante 	/*
29371195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
29381195e687SMark J Musante 	 */
29391195e687SMark J Musante 	if (flags.dryrun) {
29401195e687SMark J Musante 		memory_err = B_FALSE;
29411195e687SMark J Musante 		freelist = B_FALSE;
29421195e687SMark J Musante 		goto out;
29431195e687SMark J Musante 	}
29441195e687SMark J Musante 
29451195e687SMark J Musante 	/* now build up the config list & call the ioctl */
29461195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
29471195e687SMark J Musante 		goto out;
29481195e687SMark J Musante 
29491195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
29501195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
29511195e687SMark J Musante 	    nvlist_add_string(newconfig,
29521195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
29531195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
29541195e687SMark J Musante 		goto out;
29551195e687SMark J Musante 
29561195e687SMark J Musante 	/*
29571195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
29581195e687SMark J Musante 	 * explicitly export it.
29591195e687SMark J Musante 	 */
29601195e687SMark J Musante 	if (!flags.import)
29611195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
29621195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
29631195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
29641195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
29651195e687SMark J Musante 		goto out;
29661195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
29671195e687SMark J Musante 		goto out;
29681195e687SMark J Musante 
29691195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
29701195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
29711195e687SMark J Musante 		goto out;
29721195e687SMark J Musante 	}
29731195e687SMark J Musante 
29741195e687SMark J Musante 	freelist = B_FALSE;
29751195e687SMark J Musante 	memory_err = B_FALSE;
29761195e687SMark J Musante 
29771195e687SMark J Musante out:
29781195e687SMark J Musante 	if (varray != NULL) {
29791195e687SMark J Musante 		int v;
29801195e687SMark J Musante 
29811195e687SMark J Musante 		for (v = 0; v < vcount; v++)
29821195e687SMark J Musante 			nvlist_free(varray[v]);
29831195e687SMark J Musante 		free(varray);
29841195e687SMark J Musante 	}
29851195e687SMark J Musante 	zcmd_free_nvlists(&zc);
29861195e687SMark J Musante 	nvlist_free(zc_props);
29871195e687SMark J Musante 	nvlist_free(newconfig);
29881195e687SMark J Musante 	if (freelist) {
29891195e687SMark J Musante 		nvlist_free(*newroot);
29901195e687SMark J Musante 		*newroot = NULL;
29911195e687SMark J Musante 	}
29921195e687SMark J Musante 
29931195e687SMark J Musante 	if (retval != 0)
29941195e687SMark J Musante 		return (retval);
29951195e687SMark J Musante 
29961195e687SMark J Musante 	if (memory_err)
29971195e687SMark J Musante 		return (no_memory(hdl));
29981195e687SMark J Musante 
29991195e687SMark J Musante 	return (0);
30001195e687SMark J Musante }
30011195e687SMark J Musante 
30021195e687SMark J Musante /*
3003fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
3004fa94a07fSbrendan  * and level 2 cache devices.
300599653d4eSeschrock  */
300699653d4eSeschrock int
zpool_vdev_remove(zpool_handle_t * zhp,const char * path)300799653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
300899653d4eSeschrock {
300999653d4eSeschrock 	zfs_cmd_t zc = { 0 };
301099653d4eSeschrock 	char msg[1024];
301199653d4eSeschrock 	nvlist_t *tgt;
301288ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
301399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
301488ecc943SGeorge Wilson 	uint64_t version;
301599653d4eSeschrock 
301699653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
301799653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
301899653d4eSeschrock 
301999653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3020ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
302188ecc943SGeorge Wilson 	    &islog)) == 0)
302299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
302388ecc943SGeorge Wilson 	/*
302488ecc943SGeorge Wilson 	 * XXX - this should just go away.
302588ecc943SGeorge Wilson 	 */
302688ecc943SGeorge Wilson 	if (!avail_spare && !l2cache && !islog) {
302799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
302888ecc943SGeorge Wilson 		    "only inactive hot spares, cache, top-level, "
302988ecc943SGeorge Wilson 		    "or log devices can be removed"));
303099653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
303199653d4eSeschrock 	}
303299653d4eSeschrock 
303388ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
303488ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
303588ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
303688ecc943SGeorge Wilson 		    "pool must be upgrade to support log removal"));
303788ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
303888ecc943SGeorge Wilson 	}
303988ecc943SGeorge Wilson 
304099653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
304199653d4eSeschrock 
3042ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
304399653d4eSeschrock 		return (0);
304499653d4eSeschrock 
304599653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3046fa9e4066Sahrens }
3047fa9e4066Sahrens 
3048ea8dc4b6Seschrock /*
3049ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
3050ea8dc4b6Seschrock  */
3051ea8dc4b6Seschrock int
zpool_clear(zpool_handle_t * zhp,const char * path,nvlist_t * rewindnvl)3052468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3053ea8dc4b6Seschrock {
3054ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3055ea8dc4b6Seschrock 	char msg[1024];
305699653d4eSeschrock 	nvlist_t *tgt;
3057468c413aSTim Haley 	zpool_rewind_policy_t policy;
3058fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
305999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3060468c413aSTim Haley 	nvlist_t *nvi = NULL;
30614b964adaSGeorge Wilson 	int error;
3062ea8dc4b6Seschrock 
3063ea8dc4b6Seschrock 	if (path)
3064ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3065ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3066e9dbad6fSeschrock 		    path);
3067ea8dc4b6Seschrock 	else
3068ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3069ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3070ea8dc4b6Seschrock 		    zhp->zpool_name);
3071ea8dc4b6Seschrock 
3072ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
307399653d4eSeschrock 	if (path) {
3074fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3075ee0eb9f2SEric Schrock 		    &l2cache, NULL)) == 0)
307699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
307799653d4eSeschrock 
3078fa94a07fSbrendan 		/*
3079fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
3080fa94a07fSbrendan 		 * error clearing for l2cache devices.
3081fa94a07fSbrendan 		 */
3082a43d325bSek110237 		if (avail_spare)
308399653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
308499653d4eSeschrock 
308599653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
308699653d4eSeschrock 		    &zc.zc_guid) == 0);
3087ea8dc4b6Seschrock 	}
3088ea8dc4b6Seschrock 
3089468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
3090468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
3091ea8dc4b6Seschrock 
309257f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3093468c413aSTim Haley 		return (-1);
3094468c413aSTim Haley 
3095cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3096468c413aSTim Haley 		return (-1);
3097468c413aSTim Haley 
30984b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
30994b964adaSGeorge Wilson 	    errno == ENOMEM) {
31004b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
31014b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
31024b964adaSGeorge Wilson 			return (-1);
31034b964adaSGeorge Wilson 		}
31044b964adaSGeorge Wilson 	}
31054b964adaSGeorge Wilson 
31064b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3107468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
3108468c413aSTim Haley 		if (policy.zrp_request &
3109468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3110468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3111468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
3112468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3113468c413aSTim Haley 			    nvi);
3114468c413aSTim Haley 			nvlist_free(nvi);
3115468c413aSTim Haley 		}
3116468c413aSTim Haley 		zcmd_free_nvlists(&zc);
3117468c413aSTim Haley 		return (0);
3118468c413aSTim Haley 	}
3119468c413aSTim Haley 
3120468c413aSTim Haley 	zcmd_free_nvlists(&zc);
312199653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3122ea8dc4b6Seschrock }
3123ea8dc4b6Seschrock 
3124fa9e4066Sahrens /*
31253d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
31263d7072f8Seschrock  */
31273d7072f8Seschrock int
zpool_vdev_clear(zpool_handle_t * zhp,uint64_t guid)31283d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
31293d7072f8Seschrock {
31303d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
31313d7072f8Seschrock 	char msg[1024];
31323d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31333d7072f8Seschrock 
31343d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
31353d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
31363d7072f8Seschrock 	    guid);
31373d7072f8Seschrock 
31383d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
31393d7072f8Seschrock 	zc.zc_guid = guid;
314014f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
31413d7072f8Seschrock 
31423d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
31433d7072f8Seschrock 		return (0);
31443d7072f8Seschrock 
31453d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
31463d7072f8Seschrock }
31473d7072f8Seschrock 
31483d7072f8Seschrock /*
3149e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
3150e9103aaeSGarrett D'Amore  */
3151e9103aaeSGarrett D'Amore int
zpool_reguid(zpool_handle_t * zhp)3152e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3153e9103aaeSGarrett D'Amore {
3154e9103aaeSGarrett D'Amore 	char msg[1024];
3155e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3156e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3157e9103aaeSGarrett D'Amore 
3158e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3159e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3160e9103aaeSGarrett D'Amore 
3161e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3162e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3163e9103aaeSGarrett D'Amore 		return (0);
3164e9103aaeSGarrett D'Amore 
3165e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3166e9103aaeSGarrett D'Amore }
3167e9103aaeSGarrett D'Amore 
3168e9103aaeSGarrett D'Amore /*
31694263d13fSGeorge Wilson  * Reopen the pool.
31704263d13fSGeorge Wilson  */
31714263d13fSGeorge Wilson int
zpool_reopen(zpool_handle_t * zhp)31724263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
31734263d13fSGeorge Wilson {
31744263d13fSGeorge Wilson 	zfs_cmd_t zc = { 0 };
31754263d13fSGeorge Wilson 	char msg[1024];
31764263d13fSGeorge Wilson 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31774263d13fSGeorge Wilson 
31784263d13fSGeorge Wilson 	(void) snprintf(msg, sizeof (msg),
31794263d13fSGeorge Wilson 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
31804263d13fSGeorge Wilson 	    zhp->zpool_name);
31814263d13fSGeorge Wilson 
31824263d13fSGeorge Wilson 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
31834263d13fSGeorge Wilson 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
31844263d13fSGeorge Wilson 		return (0);
31854263d13fSGeorge Wilson 	return (zpool_standard_error(hdl, errno, msg));
31864263d13fSGeorge Wilson }
31874263d13fSGeorge Wilson 
31884263d13fSGeorge Wilson /*
3189c67d9675Seschrock  * Convert from a devid string to a path.
3190c67d9675Seschrock  */
3191c67d9675Seschrock static char *
devid_to_path(char * devid_str)3192c67d9675Seschrock devid_to_path(char *devid_str)
3193c67d9675Seschrock {
3194c67d9675Seschrock 	ddi_devid_t devid;
3195c67d9675Seschrock 	char *minor;
3196c67d9675Seschrock 	char *path;
3197c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3198c67d9675Seschrock 	int ret;
3199c67d9675Seschrock 
3200c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3201c67d9675Seschrock 		return (NULL);
3202c67d9675Seschrock 
3203c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3204c67d9675Seschrock 
3205c67d9675Seschrock 	devid_str_free(minor);
3206c67d9675Seschrock 	devid_free(devid);
3207c67d9675Seschrock 
3208c67d9675Seschrock 	if (ret != 0)
3209c67d9675Seschrock 		return (NULL);
3210c67d9675Seschrock 
3211078266a5SMarcel Telka 	/*
3212078266a5SMarcel Telka 	 * In a case the strdup() fails, we will just return NULL below.
3213078266a5SMarcel Telka 	 */
3214078266a5SMarcel Telka 	path = strdup(list[0].devname);
321599653d4eSeschrock 
3216c67d9675Seschrock 	devid_free_nmlist(list);
3217c67d9675Seschrock 
3218c67d9675Seschrock 	return (path);
3219c67d9675Seschrock }
3220c67d9675Seschrock 
3221c67d9675Seschrock /*
3222c67d9675Seschrock  * Convert from a path to a devid string.
3223c67d9675Seschrock  */
3224c67d9675Seschrock static char *
path_to_devid(const char * path)3225c67d9675Seschrock path_to_devid(const char *path)
3226c67d9675Seschrock {
3227c67d9675Seschrock 	int fd;
3228c67d9675Seschrock 	ddi_devid_t devid;
3229c67d9675Seschrock 	char *minor, *ret;
3230c67d9675Seschrock 
3231c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3232c67d9675Seschrock 		return (NULL);
3233c67d9675Seschrock 
3234c67d9675Seschrock 	minor = NULL;
3235c67d9675Seschrock 	ret = NULL;
3236c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3237c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3238c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3239c67d9675Seschrock 		if (minor != NULL)
3240c67d9675Seschrock 			devid_str_free(minor);
3241c67d9675Seschrock 		devid_free(devid);
3242c67d9675Seschrock 	}
3243c67d9675Seschrock 	(void) close(fd);
3244c67d9675Seschrock 
3245c67d9675Seschrock 	return (ret);
3246c67d9675Seschrock }
3247c67d9675Seschrock 
3248c67d9675Seschrock /*
3249c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3250c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3251c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3252c67d9675Seschrock  */
3253c67d9675Seschrock static void
set_path(zpool_handle_t * zhp,nvlist_t * nv,const char * path)3254c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3255c67d9675Seschrock {
3256c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3257c67d9675Seschrock 
3258c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3259e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3260c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3261ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3262c67d9675Seschrock 
326399653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3264c67d9675Seschrock }
3265c67d9675Seschrock 
3266c67d9675Seschrock /*
3267c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3268c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3269c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3270c67d9675Seschrock  * trailing 's0' slice name.
3271c67d9675Seschrock  *
3272c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3273c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3274c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3275c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3276c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3277c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3278c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3279c67d9675Seschrock  * of these checks.
3280c67d9675Seschrock  */
3281c67d9675Seschrock char *
zpool_vdev_name(libzfs_handle_t * hdl,zpool_handle_t * zhp,nvlist_t * nv,boolean_t verbose)328288ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
328388ecc943SGeorge Wilson     boolean_t verbose)
3284c67d9675Seschrock {
3285c67d9675Seschrock 	char *path, *devid;
3286ea8dc4b6Seschrock 	uint64_t value;
3287ea8dc4b6Seschrock 	char buf[64];
32883d7072f8Seschrock 	vdev_stat_t *vs;
32893d7072f8Seschrock 	uint_t vsc;
3290c67d9675Seschrock 
3291ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3292ea8dc4b6Seschrock 	    &value) == 0) {
3293ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3294ea8dc4b6Seschrock 		    &value) == 0);
32955ad82045Snd150628 		(void) snprintf(buf, sizeof (buf), "%llu",
32965ad82045Snd150628 		    (u_longlong_t)value);
3297ea8dc4b6Seschrock 		path = buf;
3298ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3299c67d9675Seschrock 
33003d7072f8Seschrock 		/*
33013d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
33023d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
33033d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
33043d7072f8Seschrock 		 * effects.
33053d7072f8Seschrock 		 */
33063f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
33073d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
33083d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
33093d7072f8Seschrock 		    zhp != NULL &&
3310c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3311c67d9675Seschrock 			/*
3312c67d9675Seschrock 			 * Determine if the current path is correct.
3313c67d9675Seschrock 			 */
3314c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3315c67d9675Seschrock 
3316c67d9675Seschrock 			if (newdevid == NULL ||
3317c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3318c67d9675Seschrock 				char *newpath;
3319c67d9675Seschrock 
3320c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3321c67d9675Seschrock 					/*
3322c67d9675Seschrock 					 * Update the path appropriately.
3323c67d9675Seschrock 					 */
3324c67d9675Seschrock 					set_path(zhp, nv, newpath);
332599653d4eSeschrock 					if (nvlist_add_string(nv,
332699653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
3327c67d9675Seschrock 						verify(nvlist_lookup_string(nv,
332899653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
332999653d4eSeschrock 						    &path) == 0);
333099653d4eSeschrock 					free(newpath);
333199653d4eSeschrock 				}
3332c67d9675Seschrock 			}
3333c67d9675Seschrock 
3334c67d9675Seschrock 			if (newdevid)
3335c67d9675Seschrock 				devid_str_free(newdevid);
3336c67d9675Seschrock 		}
3337c67d9675Seschrock 
3338c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3339c67d9675Seschrock 			path += 9;
3340c67d9675Seschrock 
3341c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3342ea8dc4b6Seschrock 		    &value) == 0 && value) {
33433fdda499SJohn Harres 			int pathlen = strlen(path);
334499653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
33453fdda499SJohn Harres 
33463fdda499SJohn Harres 			/*
33473fdda499SJohn Harres 			 * If it starts with c#, and ends with "s0", chop
33483fdda499SJohn Harres 			 * the "s0" off, or if it ends with "s0/old", remove
33493fdda499SJohn Harres 			 * the "s0" from the middle.
33503fdda499SJohn Harres 			 */
33513fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
33523fdda499SJohn Harres 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
33533fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
33543fdda499SJohn Harres 				} else if (pathlen > 6 &&
33553fdda499SJohn Harres 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
33563fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
33573fdda499SJohn Harres 					    "/old");
33583fdda499SJohn Harres 				}
33593fdda499SJohn Harres 			}
3360c67d9675Seschrock 			return (tmp);
3361c67d9675Seschrock 		}
3362c67d9675Seschrock 	} else {
3363c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
336499653d4eSeschrock 
336599653d4eSeschrock 		/*
336699653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
336799653d4eSeschrock 		 */
336899653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
336999653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
337099653d4eSeschrock 			    &value) == 0);
337199653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
33725ad82045Snd150628 			    (u_longlong_t)value);
337399653d4eSeschrock 			path = buf;
337499653d4eSeschrock 		}
337588ecc943SGeorge Wilson 
337688ecc943SGeorge Wilson 		/*
337788ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
337888ecc943SGeorge Wilson 		 * naming convention.
337988ecc943SGeorge Wilson 		 */
338088ecc943SGeorge Wilson 		if (verbose) {
338188ecc943SGeorge Wilson 			uint64_t id;
338288ecc943SGeorge Wilson 
338388ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
338488ecc943SGeorge Wilson 			    &id) == 0);
338588ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
338688ecc943SGeorge Wilson 			    (u_longlong_t)id);
338788ecc943SGeorge Wilson 			path = buf;
338888ecc943SGeorge Wilson 		}
3389c67d9675Seschrock 	}
3390c67d9675Seschrock 
339199653d4eSeschrock 	return (zfs_strdup(hdl, path));
3392c67d9675Seschrock }
3393ea8dc4b6Seschrock 
3394ea8dc4b6Seschrock static int
zbookmark_mem_compare(const void * a,const void * b)3395a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b)
3396ea8dc4b6Seschrock {
33977802d7bfSMatthew Ahrens 	return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3398ea8dc4b6Seschrock }
3399ea8dc4b6Seschrock 
3400ea8dc4b6Seschrock /*
3401ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3402ea8dc4b6Seschrock  * caller.
3403ea8dc4b6Seschrock  */
3404ea8dc4b6Seschrock int
zpool_get_errlog(zpool_handle_t * zhp,nvlist_t ** nverrlistp)340555434c77Sek110237 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3406ea8dc4b6Seschrock {
3407ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3408ea8dc4b6Seschrock 	uint64_t count;
34097802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = NULL;
341055434c77Sek110237 	int i;
3411ea8dc4b6Seschrock 
3412ea8dc4b6Seschrock 	/*
3413ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3414ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3415ea8dc4b6Seschrock 	 * entire list.
3416ea8dc4b6Seschrock 	 */
3417ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3418ea8dc4b6Seschrock 	    &count) == 0);
341975519f38Sek110237 	if (count == 0)
342075519f38Sek110237 		return (0);
3421e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
34227802d7bfSMatthew Ahrens 	    count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
342399653d4eSeschrock 		return (-1);
3424e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3425ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3426ea8dc4b6Seschrock 	for (;;) {
342799653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
342899653d4eSeschrock 		    &zc) != 0) {
3429e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
343099653d4eSeschrock 			if (errno == ENOMEM) {
34317802d7bfSMatthew Ahrens 				void *dst;
34327802d7bfSMatthew Ahrens 
3433bf561db0Svb160487 				count = zc.zc_nvlist_dst_size;
34347802d7bfSMatthew Ahrens 				dst = zfs_alloc(zhp->zpool_hdl, count *
34357802d7bfSMatthew Ahrens 				    sizeof (zbookmark_phys_t));
34367802d7bfSMatthew Ahrens 				if (dst == NULL)
343799653d4eSeschrock 					return (-1);
34387802d7bfSMatthew Ahrens 				zc.zc_nvlist_dst = (uintptr_t)dst;
3439ea8dc4b6Seschrock 			} else {
3440ea8dc4b6Seschrock 				return (-1);
3441ea8dc4b6Seschrock 			}
3442ea8dc4b6Seschrock 		} else {
3443ea8dc4b6Seschrock 			break;
3444ea8dc4b6Seschrock 		}
3445ea8dc4b6Seschrock 	}
3446ea8dc4b6Seschrock 
3447ea8dc4b6Seschrock 	/*
3448ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3449ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3450e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3451ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3452ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3453ea8dc4b6Seschrock 	 */
34547802d7bfSMatthew Ahrens 	zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3455e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3456e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3457ea8dc4b6Seschrock 
3458a2cdcdd2SPaul Dagnelie 	qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3459ea8dc4b6Seschrock 
346055434c77Sek110237 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
346155434c77Sek110237 
3462ea8dc4b6Seschrock 	/*
346355434c77Sek110237 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3464ea8dc4b6Seschrock 	 */
3465ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3466ea8dc4b6Seschrock 		nvlist_t *nv;
3467ea8dc4b6Seschrock 
3468c0a81264Sek110237 		/* ignoring zb_blkid and zb_level for now */
3469c0a81264Sek110237 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3470c0a81264Sek110237 		    zb[i-1].zb_object == zb[i].zb_object)
3471ea8dc4b6Seschrock 			continue;
3472ea8dc4b6Seschrock 
347355434c77Sek110237 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3474e9dbad6fSeschrock 			goto nomem;
347555434c77Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
347655434c77Sek110237 		    zb[i].zb_objset) != 0) {
347755434c77Sek110237 			nvlist_free(nv);
3478e9dbad6fSeschrock 			goto nomem;
3479e9dbad6fSeschrock 		}
348055434c77Sek110237 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
348155434c77Sek110237 		    zb[i].zb_object) != 0) {
348255434c77Sek110237 			nvlist_free(nv);
348399653d4eSeschrock 			goto nomem;
3484ea8dc4b6Seschrock 		}
348555434c77Sek110237 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
348655434c77Sek110237 			nvlist_free(nv);
3487e9dbad6fSeschrock 			goto nomem;
3488e9dbad6fSeschrock 		}
348955434c77Sek110237 		nvlist_free(nv);
3490e9dbad6fSeschrock 	}
3491e9dbad6fSeschrock 
34923ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3493ea8dc4b6Seschrock 	return (0);
349499653d4eSeschrock 
349599653d4eSeschrock nomem:
3496e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
349799653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3498ea8dc4b6Seschrock }
3499eaca9bbdSeschrock 
3500eaca9bbdSeschrock /*
3501eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3502eaca9bbdSeschrock  */
3503eaca9bbdSeschrock int
zpool_upgrade(zpool_handle_t * zhp,uint64_t new_version)3504990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3505eaca9bbdSeschrock {
3506eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
350799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3508eaca9bbdSeschrock 
3509eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3510990b4856Slling 	zc.zc_cookie = new_version;
3511990b4856Slling 
3512ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3513ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
351499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
351599653d4eSeschrock 		    zhp->zpool_name));
3516eaca9bbdSeschrock 	return (0);
3517eaca9bbdSeschrock }
351806eeb2adSek110237 
351906eeb2adSek110237 void
zfs_save_arguments(int argc,char ** argv,char * string,int len)35204445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len)
352106eeb2adSek110237 {
35224445fffbSMatthew Ahrens 	(void) strlcpy(string, basename(argv[0]), len);
35234445fffbSMatthew Ahrens 	for (int i = 1; i < argc; i++) {
35244445fffbSMatthew Ahrens 		(void) strlcat(string, " ", len);
35254445fffbSMatthew Ahrens 		(void) strlcat(string, argv[i], len);
35262a6b87f0Sek110237 	}
35272a6b87f0Sek110237 }
35282a6b87f0Sek110237 
35292a6b87f0Sek110237 int
zpool_log_history(libzfs_handle_t * hdl,const char * message)35304445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message)
35312a6b87f0Sek110237 {
35324445fffbSMatthew Ahrens 	zfs_cmd_t zc = { 0 };
35334445fffbSMatthew Ahrens 	nvlist_t *args;
35344445fffbSMatthew Ahrens 	int err;
35352a6b87f0Sek110237 
35364445fffbSMatthew Ahrens 	args = fnvlist_alloc();
35374445fffbSMatthew Ahrens 	fnvlist_add_string(args, "message", message);
35384445fffbSMatthew Ahrens 	err = zcmd_write_src_nvlist(hdl, &zc, args);
35394445fffbSMatthew Ahrens 	if (err == 0)
35404445fffbSMatthew Ahrens 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
35414445fffbSMatthew Ahrens 	nvlist_free(args);
35424445fffbSMatthew Ahrens 	zcmd_free_nvlists(&zc);
35434445fffbSMatthew Ahrens 	return (err);
354406eeb2adSek110237 }
354506eeb2adSek110237 
354606eeb2adSek110237 /*
354706eeb2adSek110237  * Perform ioctl to get some command history of a pool.
354806eeb2adSek110237  *
354906eeb2adSek110237  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
355006eeb2adSek110237  * logical offset of the history buffer to start reading from.
355106eeb2adSek110237  *
355206eeb2adSek110237  * Upon return, 'off' is the next logical offset to read from and
355306eeb2adSek110237  * 'len' is the actual amount of bytes read into 'buf'.
355406eeb2adSek110237  */
355506eeb2adSek110237 static int
get_history(zpool_handle_t * zhp,char * buf,uint64_t * off,uint64_t * len)355606eeb2adSek110237 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
355706eeb2adSek110237 {
355806eeb2adSek110237 	zfs_cmd_t zc = { 0 };
355906eeb2adSek110237 	libzfs_handle_t *hdl = zhp->zpool_hdl;
356006eeb2adSek110237 
356106eeb2adSek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
356206eeb2adSek110237 
356306eeb2adSek110237 	zc.zc_history = (uint64_t)(uintptr_t)buf;
356406eeb2adSek110237 	zc.zc_history_len = *len;
356506eeb2adSek110237 	zc.zc_history_offset = *off;
356606eeb2adSek110237 
356706eeb2adSek110237 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
356806eeb2adSek110237 		switch (errno) {
356906eeb2adSek110237 		case EPERM:
3570ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3571ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
357206eeb2adSek110237 			    "cannot show history for pool '%s'"),
357306eeb2adSek110237 			    zhp->zpool_name));
357406eeb2adSek110237 		case ENOENT:
3575ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
357606eeb2adSek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
357706eeb2adSek110237 			    "'%s'"), zhp->zpool_name));
3578d7306b64Sek110237 		case ENOTSUP:
3579d7306b64Sek110237 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3580d7306b64Sek110237 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3581d7306b64Sek110237 			    "'%s', pool must be upgraded"), zhp->zpool_name));
358206eeb2adSek110237 		default:
3583ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
358406eeb2adSek110237 			    dgettext(TEXT_DOMAIN,
358506eeb2adSek110237 			    "cannot get history for '%s'"), zhp->zpool_name));
358606eeb2adSek110237 		}
358706eeb2adSek110237 	}
358806eeb2adSek110237 
358906eeb2adSek110237 	*len = zc.zc_history_len;
359006eeb2adSek110237 	*off = zc.zc_history_offset;
359106eeb2adSek110237 
359206eeb2adSek110237 	return (0);
359306eeb2adSek110237 }
359406eeb2adSek110237 
359506eeb2adSek110237 /*
359606eeb2adSek110237  * Process the buffer of nvlists, unpacking and storing each nvlist record
359706eeb2adSek110237  * into 'records'.  'leftover' is set to the number of bytes that weren't
359806eeb2adSek110237  * processed as there wasn't a complete record.
359906eeb2adSek110237  */
36008f18d1faSGeorge Wilson int
zpool_history_unpack(char * buf,uint64_t bytes_read,uint64_t * leftover,nvlist_t *** records,uint_t * numrecords)360106eeb2adSek110237 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
360206eeb2adSek110237     nvlist_t ***records, uint_t *numrecords)
360306eeb2adSek110237 {
360406eeb2adSek110237 	uint64_t reclen;
360506eeb2adSek110237 	nvlist_t *nv;
360606eeb2adSek110237 	int i;
360706eeb2adSek110237 
360806eeb2adSek110237 	while (bytes_read > sizeof (reclen)) {
360906eeb2adSek110237 
361006eeb2adSek110237 		/* get length of packed record (stored as little endian) */
361106eeb2adSek110237 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
361206eeb2adSek110237 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
361306eeb2adSek110237 
361406eeb2adSek110237 		if (bytes_read < sizeof (reclen) + reclen)
361506eeb2adSek110237 			break;
361606eeb2adSek110237 
361706eeb2adSek110237 		/* unpack record */
361806eeb2adSek110237 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
361906eeb2adSek110237 			return (ENOMEM);
362006eeb2adSek110237 		bytes_read -= sizeof (reclen) + reclen;
362106eeb2adSek110237 		buf += sizeof (reclen) + reclen;
362206eeb2adSek110237 
362306eeb2adSek110237 		/* add record to nvlist array */
362406eeb2adSek110237 		(*numrecords)++;
362506eeb2adSek110237 		if (ISP2(*numrecords + 1)) {
362606eeb2adSek110237 			*records = realloc(*records,
362706eeb2adSek110237 			    *numrecords * 2 * sizeof (nvlist_t *));
362806eeb2adSek110237 		}
362906eeb2adSek110237 		(*records)[*numrecords - 1] = nv;
363006eeb2adSek110237 	}
363106eeb2adSek110237 
363206eeb2adSek110237 	*leftover = bytes_read;
363306eeb2adSek110237 	return (0);
363406eeb2adSek110237 }
363506eeb2adSek110237 
363606eeb2adSek110237 /*
363706eeb2adSek110237  * Retrieve the command history of a pool.
363806eeb2adSek110237  */
363906eeb2adSek110237 int
zpool_get_history(zpool_handle_t * zhp,nvlist_t ** nvhisp)364006eeb2adSek110237 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
364106eeb2adSek110237 {
36423339867aSMatthew Ahrens 	char *buf;
36433339867aSMatthew Ahrens 	int buflen = 128 * 1024;
364406eeb2adSek110237 	uint64_t off = 0;
364506eeb2adSek110237 	nvlist_t **records = NULL;
364606eeb2adSek110237 	uint_t numrecords = 0;
364706eeb2adSek110237 	int err, i;
364806eeb2adSek110237 
36493339867aSMatthew Ahrens 	buf = malloc(buflen);
36503339867aSMatthew Ahrens 	if (buf == NULL)
36513339867aSMatthew Ahrens 		return (ENOMEM);
365206eeb2adSek110237 	do {
36533339867aSMatthew Ahrens 		uint64_t bytes_read = buflen;
365406eeb2adSek110237 		uint64_t leftover;
365506eeb2adSek110237 
365606eeb2adSek110237 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
365706eeb2adSek110237 			break;
365806eeb2adSek110237 
365906eeb2adSek110237 		/* if nothing else was read in, we're at EOF, just return */
366006eeb2adSek110237 		if (!bytes_read)
366106eeb2adSek110237 			break;
366206eeb2adSek110237 
366306eeb2adSek110237 		if ((err = zpool_history_unpack(buf, bytes_read,
366406eeb2adSek110237 		    &leftover, &records, &numrecords)) != 0)
366506eeb2adSek110237 			break;
366606eeb2adSek110237 		off -= leftover;
36673339867aSMatthew Ahrens 		if (leftover == bytes_read) {
36683339867aSMatthew Ahrens 			/*
36693339867aSMatthew Ahrens 			 * no progress made, because buffer is not big enough
36703339867aSMatthew Ahrens 			 * to hold this record; resize and retry.
36713339867aSMatthew Ahrens 			 */
36723339867aSMatthew Ahrens 			buflen *= 2;
36733339867aSMatthew Ahrens 			free(buf);
36743339867aSMatthew Ahrens 			buf = malloc(buflen);
36753339867aSMatthew Ahrens 			if (buf == NULL)
36763339867aSMatthew Ahrens 				return (ENOMEM);
36773339867aSMatthew Ahrens 		}
367806eeb2adSek110237 
367906eeb2adSek110237 		/* CONSTCOND */
368006eeb2adSek110237 	} while (1);
368106eeb2adSek110237 
36823339867aSMatthew Ahrens 	free(buf);
36833339867aSMatthew Ahrens 
368406eeb2adSek110237 	if (!err) {
368506eeb2adSek110237 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
368606eeb2adSek110237 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
368706eeb2adSek110237 		    records, numrecords) == 0);
368806eeb2adSek110237 	}
368906eeb2adSek110237 	for (i = 0; i < numrecords; i++)
369006eeb2adSek110237 		nvlist_free(records[i]);
369106eeb2adSek110237 	free(records);
369206eeb2adSek110237 
369306eeb2adSek110237 	return (err);
369406eeb2adSek110237 }
369555434c77Sek110237 
369655434c77Sek110237 void
zpool_obj_to_path(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len)369755434c77Sek110237 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
369855434c77Sek110237     char *pathname, size_t len)
369955434c77Sek110237 {
370055434c77Sek110237 	zfs_cmd_t zc = { 0 };
370155434c77Sek110237 	boolean_t mounted = B_FALSE;
370255434c77Sek110237 	char *mntpnt = NULL;
3703*a1988827SMatthew Ahrens 	char dsname[ZFS_MAX_DATASET_NAME_LEN];
370455434c77Sek110237 
370555434c77Sek110237 	if (dsobj == 0) {
370655434c77Sek110237 		/* special case for the MOS */
370755434c77Sek110237 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
370855434c77Sek110237 		return;
370955434c77Sek110237 	}
371055434c77Sek110237 
371155434c77Sek110237 	/* get the dataset's name */
371255434c77Sek110237 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
371355434c77Sek110237 	zc.zc_obj = dsobj;
371455434c77Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
371555434c77Sek110237 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
371655434c77Sek110237 		/* just write out a path of two object numbers */
371755434c77Sek110237 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
371855434c77Sek110237 		    dsobj, obj);
371955434c77Sek110237 		return;
372055434c77Sek110237 	}
372155434c77Sek110237 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
372255434c77Sek110237 
372355434c77Sek110237 	/* find out if the dataset is mounted */
372455434c77Sek110237 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
372555434c77Sek110237 
372655434c77Sek110237 	/* get the corrupted object's path */
372755434c77Sek110237 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
372855434c77Sek110237 	zc.zc_obj = obj;
372955434c77Sek110237 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
373055434c77Sek110237 	    &zc) == 0) {
373155434c77Sek110237 		if (mounted) {
373255434c77Sek110237 			(void) snprintf(pathname, len, "%s%s", mntpnt,
373355434c77Sek110237 			    zc.zc_value);
373455434c77Sek110237 		} else {
373555434c77Sek110237 			(void) snprintf(pathname, len, "%s:%s",
373655434c77Sek110237 			    dsname, zc.zc_value);
373755434c77Sek110237 		}
373855434c77Sek110237 	} else {
373955434c77Sek110237 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
374055434c77Sek110237 	}
374155434c77Sek110237 	free(mntpnt);
374255434c77Sek110237 }
3743b1b8ab34Slling 
37448488aeb5Staylor /*
374515e6edf1Sgw25295  * Read the EFI label from the config, if a label does not exist then
374615e6edf1Sgw25295  * pass back the error to the caller. If the caller has passed a non-NULL
374715e6edf1Sgw25295  * diskaddr argument then we set it to the starting address of the EFI
374815e6edf1Sgw25295  * partition.
374915e6edf1Sgw25295  */
375015e6edf1Sgw25295 static int
read_efi_label(nvlist_t * config,diskaddr_t * sb)375115e6edf1Sgw25295 read_efi_label(nvlist_t *config, diskaddr_t *sb)
375215e6edf1Sgw25295 {
375315e6edf1Sgw25295 	char *path;
375415e6edf1Sgw25295 	int fd;
375515e6edf1Sgw25295 	char diskname[MAXPATHLEN];
375615e6edf1Sgw25295 	int err = -1;
375715e6edf1Sgw25295 
375815e6edf1Sgw25295 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
375915e6edf1Sgw25295 		return (err);
376015e6edf1Sgw25295 
376115e6edf1Sgw25295 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
376215e6edf1Sgw25295 	    strrchr(path, '/'));
376315e6edf1Sgw25295 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
376415e6edf1Sgw25295 		struct dk_gpt *vtoc;
376515e6edf1Sgw25295 
376615e6edf1Sgw25295 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
376715e6edf1Sgw25295 			if (sb != NULL)
376815e6edf1Sgw25295 				*sb = vtoc->efi_parts[0].p_start;
376915e6edf1Sgw25295 			efi_free(vtoc);
377015e6edf1Sgw25295 		}
377115e6edf1Sgw25295 		(void) close(fd);
377215e6edf1Sgw25295 	}
377315e6edf1Sgw25295 	return (err);
377415e6edf1Sgw25295 }
377515e6edf1Sgw25295 
377615e6edf1Sgw25295 /*
37778488aeb5Staylor  * determine where a partition starts on a disk in the current
37788488aeb5Staylor  * configuration
37798488aeb5Staylor  */
37808488aeb5Staylor static diskaddr_t
find_start_block(nvlist_t * config)37818488aeb5Staylor find_start_block(nvlist_t *config)
37828488aeb5Staylor {
37838488aeb5Staylor 	nvlist_t **child;
37848488aeb5Staylor 	uint_t c, children;
37858488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
37868488aeb5Staylor 	uint64_t wholedisk;
37878488aeb5Staylor 
37888488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
37898488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
37908488aeb5Staylor 		if (nvlist_lookup_uint64(config,
37918488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
37928488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
37938488aeb5Staylor 			return (MAXOFFSET_T);
37948488aeb5Staylor 		}
379515e6edf1Sgw25295 		if (read_efi_label(config, &sb) < 0)
379615e6edf1Sgw25295 			sb = MAXOFFSET_T;
37978488aeb5Staylor 		return (sb);
37988488aeb5Staylor 	}
37998488aeb5Staylor 
38008488aeb5Staylor 	for (c = 0; c < children; c++) {
38018488aeb5Staylor 		sb = find_start_block(child[c]);
38028488aeb5Staylor 		if (sb != MAXOFFSET_T) {
38038488aeb5Staylor 			return (sb);
38048488aeb5Staylor 		}
38058488aeb5Staylor 	}
38068488aeb5Staylor 	return (MAXOFFSET_T);
38078488aeb5Staylor }
38088488aeb5Staylor 
38098488aeb5Staylor /*
38108488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
38118488aeb5Staylor  * stripped of any leading /dev path.
38128488aeb5Staylor  */
38138488aeb5Staylor int
zpool_label_disk(libzfs_handle_t * hdl,zpool_handle_t * zhp,char * name)38148488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
38158488aeb5Staylor {
38168488aeb5Staylor 	char path[MAXPATHLEN];
38178488aeb5Staylor 	struct dk_gpt *vtoc;
38188488aeb5Staylor 	int fd;
38198488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
38208488aeb5Staylor 	uint64_t slice_size;
38218488aeb5Staylor 	diskaddr_t start_block;
38228488aeb5Staylor 	char errbuf[1024];
38238488aeb5Staylor 
3824c6ef114fSmmusante 	/* prepare an error message just in case */
3825c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
3826c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3827c6ef114fSmmusante 
38288488aeb5Staylor 	if (zhp) {
38298488aeb5Staylor 		nvlist_t *nvroot;
38308488aeb5Staylor 
38318488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
38328488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
38338488aeb5Staylor 
38348488aeb5Staylor 		if (zhp->zpool_start_block == 0)
38358488aeb5Staylor 			start_block = find_start_block(nvroot);
38368488aeb5Staylor 		else
38378488aeb5Staylor 			start_block = zhp->zpool_start_block;
38388488aeb5Staylor 		zhp->zpool_start_block = start_block;
38398488aeb5Staylor 	} else {
38408488aeb5Staylor 		/* new pool */
38418488aeb5Staylor 		start_block = NEW_START_BLOCK;
38428488aeb5Staylor 	}
38438488aeb5Staylor 
38448488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
38458488aeb5Staylor 	    BACKUP_SLICE);
38468488aeb5Staylor 
38478488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
38488488aeb5Staylor 		/*
38498488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
38508488aeb5Staylor 		 * is a valid device.
38518488aeb5Staylor 		 */
3852c6ef114fSmmusante 		zfs_error_aux(hdl,
3853c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
38548488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
38558488aeb5Staylor 	}
38568488aeb5Staylor 
38578488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
38588488aeb5Staylor 		/*
38598488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
38608488aeb5Staylor 		 * were unable to read the disk's capacity
38618488aeb5Staylor 		 */
38628488aeb5Staylor 		if (errno == ENOMEM)
38638488aeb5Staylor 			(void) no_memory(hdl);
38648488aeb5Staylor 
38658488aeb5Staylor 		(void) close(fd);
3866c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3867c6ef114fSmmusante 		    "unable to read disk capacity"), name);
38688488aeb5Staylor 
38698488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
38708488aeb5Staylor 	}
38718488aeb5Staylor 
38728488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
38738488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
38748488aeb5Staylor 	if (start_block == MAXOFFSET_T)
38758488aeb5Staylor 		start_block = NEW_START_BLOCK;
38768488aeb5Staylor 	slice_size -= start_block;
38778488aeb5Staylor 
38788488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
38798488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
38808488aeb5Staylor 
38818488aeb5Staylor 	/*
38828488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
38838488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
38848488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
38858488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
38868488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
38878488aeb5Staylor 	 * can get, in the absence of V_OTHER.
38888488aeb5Staylor 	 */
38898488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
38908488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
38918488aeb5Staylor 
38928488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
38938488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
38948488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
38958488aeb5Staylor 
38968488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
38978488aeb5Staylor 		/*
38988488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
38998488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
39008488aeb5Staylor 		 * ecting the user to manually label the disk and give
39018488aeb5Staylor 		 * a specific slice.
39028488aeb5Staylor 		 */
39038488aeb5Staylor 		(void) close(fd);
39048488aeb5Staylor 		efi_free(vtoc);
39058488aeb5Staylor 
39068488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3907c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
39088488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
39098488aeb5Staylor 	}
39108488aeb5Staylor 
39118488aeb5Staylor 	(void) close(fd);
39128488aeb5Staylor 	efi_free(vtoc);
39138488aeb5Staylor 	return (0);
39148488aeb5Staylor }
3915e7cbe64fSgw25295 
3916e7cbe64fSgw25295 static boolean_t
supported_dump_vdev_type(libzfs_handle_t * hdl,nvlist_t * config,char * errbuf)3917e7cbe64fSgw25295 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3918e7cbe64fSgw25295 {
3919e7cbe64fSgw25295 	char *type;
3920e7cbe64fSgw25295 	nvlist_t **child;
3921e7cbe64fSgw25295 	uint_t children, c;
3922e7cbe64fSgw25295 
3923e7cbe64fSgw25295 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3924810e43b2SBill Pijewski 	if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
392588ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3926e7cbe64fSgw25295 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3927e7cbe64fSgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3928e7cbe64fSgw25295 		    "vdev type '%s' is not supported"), type);
3929e7cbe64fSgw25295 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3930e7cbe64fSgw25295 		return (B_FALSE);
3931e7cbe64fSgw25295 	}
3932e7cbe64fSgw25295 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3933e7cbe64fSgw25295 	    &child, &children) == 0) {
3934e7cbe64fSgw25295 		for (c = 0; c < children; c++) {
3935e7cbe64fSgw25295 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3936e7cbe64fSgw25295 				return (B_FALSE);
3937e7cbe64fSgw25295 		}
3938e7cbe64fSgw25295 	}
3939e7cbe64fSgw25295 	return (B_TRUE);
3940e7cbe64fSgw25295 }
3941e7cbe64fSgw25295 
3942e7cbe64fSgw25295 /*
3943810e43b2SBill Pijewski  * Check if this zvol is allowable for use as a dump device; zero if
3944810e43b2SBill Pijewski  * it is, > 0 if it isn't, < 0 if it isn't a zvol.
3945810e43b2SBill Pijewski  *
3946810e43b2SBill Pijewski  * Allowable storage configurations include mirrors, all raidz variants, and
3947810e43b2SBill Pijewski  * pools with log, cache, and spare devices.  Pools which are backed by files or
3948810e43b2SBill Pijewski  * have missing/hole vdevs are not suitable.
3949e7cbe64fSgw25295  */
3950e7cbe64fSgw25295 int
zvol_check_dump_config(char * arg)3951e7cbe64fSgw25295 zvol_check_dump_config(char *arg)
3952e7cbe64fSgw25295 {
3953e7cbe64fSgw25295 	zpool_handle_t *zhp = NULL;
3954e7cbe64fSgw25295 	nvlist_t *config, *nvroot;
3955e7cbe64fSgw25295 	char *p, *volname;
3956e7cbe64fSgw25295 	nvlist_t **top;
3957e7cbe64fSgw25295 	uint_t toplevels;
3958e7cbe64fSgw25295 	libzfs_handle_t *hdl;
3959e7cbe64fSgw25295 	char errbuf[1024];
3960*a1988827SMatthew Ahrens 	char poolname[ZFS_MAX_DATASET_NAME_LEN];
3961e7cbe64fSgw25295 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3962e7cbe64fSgw25295 	int ret = 1;
3963e7cbe64fSgw25295 
3964e7cbe64fSgw25295 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3965e7cbe64fSgw25295 		return (-1);
3966e7cbe64fSgw25295 	}
3967e7cbe64fSgw25295 
3968e7cbe64fSgw25295 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3969e7cbe64fSgw25295 	    "dump is not supported on device '%s'"), arg);
3970e7cbe64fSgw25295 
3971e7cbe64fSgw25295 	if ((hdl = libzfs_init()) == NULL)
3972e7cbe64fSgw25295 		return (1);
3973e7cbe64fSgw25295 	libzfs_print_on_error(hdl, B_TRUE);
3974e7cbe64fSgw25295 
3975e7cbe64fSgw25295 	volname = arg + pathlen;
3976e7cbe64fSgw25295 
3977e7cbe64fSgw25295 	/* check the configuration of the pool */
3978e7cbe64fSgw25295 	if ((p = strchr(volname, '/')) == NULL) {
3979e7cbe64fSgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3980e7cbe64fSgw25295 		    "malformed dataset name"));
3981e7cbe64fSgw25295 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3982e7cbe64fSgw25295 		return (1);
3983*a1988827SMatthew Ahrens 	} else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
3984e7cbe64fSgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3985e7cbe64fSgw25295 		    "dataset name is too long"));
3986e7cbe64fSgw25295 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3987e7cbe64fSgw25295 		return (1);
3988e7cbe64fSgw25295 	} else {
3989e7cbe64fSgw25295 		(void) strncpy(poolname, volname, p - volname);
3990e7cbe64fSgw25295 		poolname[p - volname] = '\0';
3991e7cbe64fSgw25295 	}
3992e7cbe64fSgw25295 
3993e7cbe64fSgw25295 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3994e7cbe64fSgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3995e7cbe64fSgw25295 		    "could not open pool '%s'"), poolname);
3996e7cbe64fSgw25295 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3997e7cbe64fSgw25295 		goto out;
3998e7cbe64fSgw25295 	}
3999e7cbe64fSgw25295 	config = zpool_get_config(zhp, NULL);
4000e7cbe64fSgw25295 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4001e7cbe64fSgw25295 	    &nvroot) != 0) {
4002e7cbe64fSgw25295 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4003e7cbe64fSgw25295 		    "could not obtain vdev configuration for  '%s'"), poolname);
4004e7cbe64fSgw25295 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4005e7cbe64fSgw25295 		goto out;
4006e7cbe64fSgw25295 	}
4007e7cbe64fSgw25295 
4008e7cbe64fSgw25295 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4009e7cbe64fSgw25295 	    &top, &toplevels) == 0);
4010e7cbe64fSgw25295 
4011e7cbe64fSgw25295 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4012e7cbe64fSgw25295 		goto out;
4013e7cbe64fSgw25295 	}
4014e7cbe64fSgw25295 	ret = 0;
4015e7cbe64fSgw25295 
4016e7cbe64fSgw25295 out:
4017e7cbe64fSgw25295 	if (zhp)
4018e7cbe64fSgw25295 		zpool_close(zhp);
4019e7cbe64fSgw25295 	libzfs_fini(hdl);
4020e7cbe64fSgw25295 	return (ret);
4021e7cbe64fSgw25295 }
4022