xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision 8704186e373c9ed74daa395ff3f7fd745396df9e)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25e9103aaeSGarrett D'Amore  * Copyright (c) 2011 by Delphix. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <ctype.h>
29fa9e4066Sahrens #include <errno.h>
30fa9e4066Sahrens #include <devid.h>
31fa9e4066Sahrens #include <fcntl.h>
32fa9e4066Sahrens #include <libintl.h>
33fa9e4066Sahrens #include <stdio.h>
34fa9e4066Sahrens #include <stdlib.h>
35f3861e1aSahl #include <strings.h>
36fa9e4066Sahrens #include <unistd.h>
378488aeb5Staylor #include <sys/efi_partition.h>
388488aeb5Staylor #include <sys/vtoc.h>
39fa9e4066Sahrens #include <sys/zfs_ioctl.h>
40573ca77eSGeorge Wilson #include <dlfcn.h>
41fa9e4066Sahrens 
42fa9e4066Sahrens #include "zfs_namecheck.h"
43b1b8ab34Slling #include "zfs_prop.h"
44fa9e4066Sahrens #include "libzfs_impl.h"
45468c413aSTim Haley #include "zfs_comutil.h"
46fa9e4066Sahrens 
4715e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
48990b4856Slling 
49573ca77eSGeorge Wilson #define	DISK_ROOT	"/dev/dsk"
50573ca77eSGeorge Wilson #define	RDISK_ROOT	"/dev/rdsk"
51573ca77eSGeorge Wilson #define	BACKUP_SLICE	"s2"
52573ca77eSGeorge Wilson 
53f9af39baSGeorge Wilson typedef struct prop_flags {
54f9af39baSGeorge Wilson 	int create:1;	/* Validate property on creation */
55f9af39baSGeorge Wilson 	int import:1;	/* Validate property on import */
56f9af39baSGeorge Wilson } prop_flags_t;
57f9af39baSGeorge Wilson 
58990b4856Slling /*
59990b4856Slling  * ====================================================================
60990b4856Slling  *   zpool property functions
61990b4856Slling  * ====================================================================
62990b4856Slling  */
63990b4856Slling 
64990b4856Slling static int
65990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
66990b4856Slling {
67990b4856Slling 	zfs_cmd_t zc = { 0 };
68990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
69990b4856Slling 
70990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71990b4856Slling 
72990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
73990b4856Slling 		return (-1);
74990b4856Slling 
75990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
76990b4856Slling 		if (errno == ENOMEM) {
77990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
78990b4856Slling 				zcmd_free_nvlists(&zc);
79990b4856Slling 				return (-1);
80990b4856Slling 			}
81990b4856Slling 		} else {
82990b4856Slling 			zcmd_free_nvlists(&zc);
83990b4856Slling 			return (-1);
84990b4856Slling 		}
85990b4856Slling 	}
86990b4856Slling 
87990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
88990b4856Slling 		zcmd_free_nvlists(&zc);
89990b4856Slling 		return (-1);
90990b4856Slling 	}
91990b4856Slling 
92990b4856Slling 	zcmd_free_nvlists(&zc);
93990b4856Slling 
94990b4856Slling 	return (0);
95990b4856Slling }
96990b4856Slling 
97990b4856Slling static int
98990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
99990b4856Slling {
100990b4856Slling 	nvlist_t *old_props;
101990b4856Slling 
102990b4856Slling 	old_props = zhp->zpool_props;
103990b4856Slling 
104990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
105990b4856Slling 		return (-1);
106990b4856Slling 
107990b4856Slling 	nvlist_free(old_props);
108990b4856Slling 	return (0);
109990b4856Slling }
110990b4856Slling 
111990b4856Slling static char *
112990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
113990b4856Slling     zprop_source_t *src)
114990b4856Slling {
115990b4856Slling 	nvlist_t *nv, *nvl;
116990b4856Slling 	uint64_t ival;
117990b4856Slling 	char *value;
118990b4856Slling 	zprop_source_t source;
119990b4856Slling 
120990b4856Slling 	nvl = zhp->zpool_props;
121990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
122990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
123990b4856Slling 		source = ival;
124990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
125990b4856Slling 	} else {
126990b4856Slling 		source = ZPROP_SRC_DEFAULT;
127990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
128990b4856Slling 			value = "-";
129990b4856Slling 	}
130990b4856Slling 
131990b4856Slling 	if (src)
132990b4856Slling 		*src = source;
133990b4856Slling 
134990b4856Slling 	return (value);
135990b4856Slling }
136990b4856Slling 
137990b4856Slling uint64_t
138990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
139990b4856Slling {
140990b4856Slling 	nvlist_t *nv, *nvl;
141990b4856Slling 	uint64_t value;
142990b4856Slling 	zprop_source_t source;
143990b4856Slling 
144b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
145b87f3af3Sperrin 		/*
146b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
147b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
148b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
149b87f3af3Sperrin 		 */
150b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
151b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
152b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
153b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
154b87f3af3Sperrin 		    == 0)) {
155b87f3af3Sperrin 			return (value);
156b87f3af3Sperrin 		}
157990b4856Slling 		return (zpool_prop_default_numeric(prop));
158b87f3af3Sperrin 	}
159990b4856Slling 
160990b4856Slling 	nvl = zhp->zpool_props;
161990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
162990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
163990b4856Slling 		source = value;
164990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
165990b4856Slling 	} else {
166990b4856Slling 		source = ZPROP_SRC_DEFAULT;
167990b4856Slling 		value = zpool_prop_default_numeric(prop);
168990b4856Slling 	}
169990b4856Slling 
170990b4856Slling 	if (src)
171990b4856Slling 		*src = source;
172990b4856Slling 
173990b4856Slling 	return (value);
174990b4856Slling }
175990b4856Slling 
176990b4856Slling /*
177990b4856Slling  * Map VDEV STATE to printed strings.
178990b4856Slling  */
179990b4856Slling char *
180990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
181990b4856Slling {
182990b4856Slling 	switch (state) {
183990b4856Slling 	case VDEV_STATE_CLOSED:
184990b4856Slling 	case VDEV_STATE_OFFLINE:
185990b4856Slling 		return (gettext("OFFLINE"));
186990b4856Slling 	case VDEV_STATE_REMOVED:
187990b4856Slling 		return (gettext("REMOVED"));
188990b4856Slling 	case VDEV_STATE_CANT_OPEN:
189b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190990b4856Slling 			return (gettext("FAULTED"));
1911195e687SMark J Musante 		else if (aux == VDEV_AUX_SPLIT_POOL)
1921195e687SMark J Musante 			return (gettext("SPLIT"));
193990b4856Slling 		else
194990b4856Slling 			return (gettext("UNAVAIL"));
195990b4856Slling 	case VDEV_STATE_FAULTED:
196990b4856Slling 		return (gettext("FAULTED"));
197990b4856Slling 	case VDEV_STATE_DEGRADED:
198990b4856Slling 		return (gettext("DEGRADED"));
199990b4856Slling 	case VDEV_STATE_HEALTHY:
200990b4856Slling 		return (gettext("ONLINE"));
201990b4856Slling 	}
202990b4856Slling 
203990b4856Slling 	return (gettext("UNKNOWN"));
204990b4856Slling }
205990b4856Slling 
206990b4856Slling /*
207990b4856Slling  * Get a zpool property value for 'prop' and return the value in
208990b4856Slling  * a pre-allocated buffer.
209990b4856Slling  */
210990b4856Slling int
211990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
212990b4856Slling     zprop_source_t *srctype)
213990b4856Slling {
214990b4856Slling 	uint64_t intval;
215990b4856Slling 	const char *strval;
216990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
217990b4856Slling 	nvlist_t *nvroot;
218990b4856Slling 	vdev_stat_t *vs;
219990b4856Slling 	uint_t vsc;
220990b4856Slling 
221990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
222379c004dSEric Schrock 		switch (prop) {
223379c004dSEric Schrock 		case ZPOOL_PROP_NAME:
224990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
225379c004dSEric Schrock 			break;
226379c004dSEric Schrock 
227379c004dSEric Schrock 		case ZPOOL_PROP_HEALTH:
228990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
229379c004dSEric Schrock 			break;
230379c004dSEric Schrock 
231379c004dSEric Schrock 		case ZPOOL_PROP_GUID:
232379c004dSEric Schrock 			intval = zpool_get_prop_int(zhp, prop, &src);
233379c004dSEric Schrock 			(void) snprintf(buf, len, "%llu", intval);
234379c004dSEric Schrock 			break;
235379c004dSEric Schrock 
236379c004dSEric Schrock 		case ZPOOL_PROP_ALTROOT:
237379c004dSEric Schrock 		case ZPOOL_PROP_CACHEFILE:
238*8704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
239379c004dSEric Schrock 			if (zhp->zpool_props != NULL ||
240379c004dSEric Schrock 			    zpool_get_all_props(zhp) == 0) {
241379c004dSEric Schrock 				(void) strlcpy(buf,
242379c004dSEric Schrock 				    zpool_get_prop_string(zhp, prop, &src),
243379c004dSEric Schrock 				    len);
244379c004dSEric Schrock 				if (srctype != NULL)
245379c004dSEric Schrock 					*srctype = src;
246379c004dSEric Schrock 				return (0);
247379c004dSEric Schrock 			}
248379c004dSEric Schrock 			/* FALLTHROUGH */
249379c004dSEric Schrock 		default:
250990b4856Slling 			(void) strlcpy(buf, "-", len);
251379c004dSEric Schrock 			break;
252379c004dSEric Schrock 		}
253379c004dSEric Schrock 
254379c004dSEric Schrock 		if (srctype != NULL)
255379c004dSEric Schrock 			*srctype = src;
256990b4856Slling 		return (0);
257990b4856Slling 	}
258990b4856Slling 
259990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
260990b4856Slling 	    prop != ZPOOL_PROP_NAME)
261990b4856Slling 		return (-1);
262990b4856Slling 
263990b4856Slling 	switch (zpool_prop_get_type(prop)) {
264990b4856Slling 	case PROP_TYPE_STRING:
265990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
266990b4856Slling 		    len);
267990b4856Slling 		break;
268990b4856Slling 
269990b4856Slling 	case PROP_TYPE_NUMBER:
270990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
271990b4856Slling 
272990b4856Slling 		switch (prop) {
273990b4856Slling 		case ZPOOL_PROP_SIZE:
274485bbbf5SGeorge Wilson 		case ZPOOL_PROP_ALLOCATED:
275485bbbf5SGeorge Wilson 		case ZPOOL_PROP_FREE:
276990b4856Slling 			(void) zfs_nicenum(intval, buf, len);
277990b4856Slling 			break;
278990b4856Slling 
279990b4856Slling 		case ZPOOL_PROP_CAPACITY:
280990b4856Slling 			(void) snprintf(buf, len, "%llu%%",
281990b4856Slling 			    (u_longlong_t)intval);
282990b4856Slling 			break;
283990b4856Slling 
284b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
285b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
286b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
287b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
288b24ab676SJeff Bonwick 			break;
289b24ab676SJeff Bonwick 
290990b4856Slling 		case ZPOOL_PROP_HEALTH:
291990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
292990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
293990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2943f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2953f9d6ad7SLin Ling 			    == 0);
296990b4856Slling 
297990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
298990b4856Slling 			    vs->vs_aux), len);
299990b4856Slling 			break;
300990b4856Slling 		default:
301990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
302990b4856Slling 		}
303990b4856Slling 		break;
304990b4856Slling 
305990b4856Slling 	case PROP_TYPE_INDEX:
306990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
307990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
308990b4856Slling 		    != 0)
309990b4856Slling 			return (-1);
310990b4856Slling 		(void) strlcpy(buf, strval, len);
311990b4856Slling 		break;
312990b4856Slling 
313990b4856Slling 	default:
314990b4856Slling 		abort();
315990b4856Slling 	}
316990b4856Slling 
317990b4856Slling 	if (srctype)
318990b4856Slling 		*srctype = src;
319990b4856Slling 
320990b4856Slling 	return (0);
321990b4856Slling }
322990b4856Slling 
323990b4856Slling /*
324990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
325990b4856Slling  * Assuming bootfs is a valid dataset name.
326990b4856Slling  */
327990b4856Slling static boolean_t
328990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
329990b4856Slling {
330990b4856Slling 	int len = strlen(pool);
331990b4856Slling 
332fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
333990b4856Slling 		return (B_FALSE);
334990b4856Slling 
335990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
336990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
337990b4856Slling 		return (B_TRUE);
338990b4856Slling 
339990b4856Slling 	return (B_FALSE);
340990b4856Slling }
341990b4856Slling 
34215e6edf1Sgw /*
34315e6edf1Sgw  * Inspect the configuration to determine if any of the devices contain
34415e6edf1Sgw  * an EFI label.
34515e6edf1Sgw  */
34615e6edf1Sgw static boolean_t
34715e6edf1Sgw pool_uses_efi(nvlist_t *config)
34815e6edf1Sgw {
34915e6edf1Sgw 	nvlist_t **child;
35015e6edf1Sgw 	uint_t c, children;
35115e6edf1Sgw 
35215e6edf1Sgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
35315e6edf1Sgw 	    &child, &children) != 0)
35415e6edf1Sgw 		return (read_efi_label(config, NULL) >= 0);
35515e6edf1Sgw 
35615e6edf1Sgw 	for (c = 0; c < children; c++) {
35715e6edf1Sgw 		if (pool_uses_efi(child[c]))
35815e6edf1Sgw 			return (B_TRUE);
35915e6edf1Sgw 	}
36015e6edf1Sgw 	return (B_FALSE);
36115e6edf1Sgw }
36215e6edf1Sgw 
363b5b76fecSGeorge Wilson static boolean_t
364b5b76fecSGeorge Wilson pool_is_bootable(zpool_handle_t *zhp)
365b5b76fecSGeorge Wilson {
366b5b76fecSGeorge Wilson 	char bootfs[ZPOOL_MAXNAMELEN];
367b5b76fecSGeorge Wilson 
368b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
369b5b76fecSGeorge Wilson 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
370b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
371b5b76fecSGeorge Wilson }
372b5b76fecSGeorge Wilson 
373b5b76fecSGeorge Wilson 
374990b4856Slling /*
375990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
376990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
377990b4856Slling  * specified as strings.
378990b4856Slling  */
379990b4856Slling static nvlist_t *
3800a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
381f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
382990b4856Slling {
383990b4856Slling 	nvpair_t *elem;
384990b4856Slling 	nvlist_t *retprops;
385990b4856Slling 	zpool_prop_t prop;
386990b4856Slling 	char *strval;
387990b4856Slling 	uint64_t intval;
388*8704186eSDan McDonald 	char *slash, *check;
3892f8aaab3Seschrock 	struct stat64 statbuf;
39015e6edf1Sgw 	zpool_handle_t *zhp;
39115e6edf1Sgw 	nvlist_t *nvroot;
392990b4856Slling 
393990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
394990b4856Slling 		(void) no_memory(hdl);
395990b4856Slling 		return (NULL);
396990b4856Slling 	}
397990b4856Slling 
398990b4856Slling 	elem = NULL;
399990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
400990b4856Slling 		const char *propname = nvpair_name(elem);
401990b4856Slling 
402990b4856Slling 		/*
403990b4856Slling 		 * Make sure this property is valid and applies to this type.
404990b4856Slling 		 */
405990b4856Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
406990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
407990b4856Slling 			    "invalid property '%s'"), propname);
408990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
409990b4856Slling 			goto error;
410990b4856Slling 		}
411990b4856Slling 
412990b4856Slling 		if (zpool_prop_readonly(prop)) {
413990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
414990b4856Slling 			    "is readonly"), propname);
415990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
416990b4856Slling 			goto error;
417990b4856Slling 		}
418990b4856Slling 
419990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
420990b4856Slling 		    &strval, &intval, errbuf) != 0)
421990b4856Slling 			goto error;
422990b4856Slling 
423990b4856Slling 		/*
424990b4856Slling 		 * Perform additional checking for specific properties.
425990b4856Slling 		 */
426990b4856Slling 		switch (prop) {
427990b4856Slling 		case ZPOOL_PROP_VERSION:
428990b4856Slling 			if (intval < version || intval > SPA_VERSION) {
429990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
430990b4856Slling 				    "property '%s' number %d is invalid."),
431990b4856Slling 				    propname, intval);
432990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
433990b4856Slling 				goto error;
434990b4856Slling 			}
435990b4856Slling 			break;
436990b4856Slling 
437990b4856Slling 		case ZPOOL_PROP_BOOTFS:
438f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
439990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440990b4856Slling 				    "property '%s' cannot be set at creation "
441990b4856Slling 				    "or import time"), propname);
442990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
443990b4856Slling 				goto error;
444990b4856Slling 			}
445990b4856Slling 
446990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
447990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
448990b4856Slling 				    "pool must be upgraded to support "
449990b4856Slling 				    "'%s' property"), propname);
450990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
451990b4856Slling 				goto error;
452990b4856Slling 			}
453990b4856Slling 
454990b4856Slling 			/*
455990b4856Slling 			 * bootfs property value has to be a dataset name and
456990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
457990b4856Slling 			 */
458990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
459990b4856Slling 			    strval)) {
460990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
461990b4856Slling 				    "is an invalid name"), strval);
462990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
463990b4856Slling 				goto error;
464990b4856Slling 			}
46515e6edf1Sgw 
46615e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
46715e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46815e6edf1Sgw 				    "could not open pool '%s'"), poolname);
46915e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
47015e6edf1Sgw 				goto error;
47115e6edf1Sgw 			}
47215e6edf1Sgw 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
47315e6edf1Sgw 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
47415e6edf1Sgw 
47515e6edf1Sgw 			/*
47615e6edf1Sgw 			 * bootfs property cannot be set on a disk which has
47715e6edf1Sgw 			 * been EFI labeled.
47815e6edf1Sgw 			 */
47915e6edf1Sgw 			if (pool_uses_efi(nvroot)) {
48015e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
48115e6edf1Sgw 				    "property '%s' not supported on "
48215e6edf1Sgw 				    "EFI labeled devices"), propname);
48315e6edf1Sgw 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
48415e6edf1Sgw 				zpool_close(zhp);
48515e6edf1Sgw 				goto error;
48615e6edf1Sgw 			}
48715e6edf1Sgw 			zpool_close(zhp);
488990b4856Slling 			break;
489990b4856Slling 
4902f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
491f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
492990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
493990b4856Slling 				    "property '%s' can only be set during pool "
494990b4856Slling 				    "creation or import"), propname);
495990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
496990b4856Slling 				goto error;
497990b4856Slling 			}
498990b4856Slling 
4992f8aaab3Seschrock 			if (strval[0] != '/') {
500990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5012f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
5022f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
503990b4856Slling 				goto error;
504990b4856Slling 			}
5052f8aaab3Seschrock 			break;
5062f8aaab3Seschrock 
5072f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5082f8aaab3Seschrock 			if (strval[0] == '\0')
5092f8aaab3Seschrock 				break;
5102f8aaab3Seschrock 
5112f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5122f8aaab3Seschrock 				break;
513990b4856Slling 
514990b4856Slling 			if (strval[0] != '/') {
515990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5162f8aaab3Seschrock 				    "property '%s' must be empty, an "
5172f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
518990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
519990b4856Slling 				goto error;
520990b4856Slling 			}
521990b4856Slling 
5222f8aaab3Seschrock 			slash = strrchr(strval, '/');
523990b4856Slling 
5242f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5252f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
5262f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5272f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
5282f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5292f8aaab3Seschrock 				goto error;
5302f8aaab3Seschrock 			}
531990b4856Slling 
5322f8aaab3Seschrock 			*slash = '\0';
5332f8aaab3Seschrock 
5342c32020fSeschrock 			if (strval[0] != '\0' &&
5352c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
5362c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
5372f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5382f8aaab3Seschrock 				    "'%s' is not a valid directory"),
5392f8aaab3Seschrock 				    strval);
5402f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5412f8aaab3Seschrock 				goto error;
5422f8aaab3Seschrock 			}
5432f8aaab3Seschrock 
5442f8aaab3Seschrock 			*slash = '/';
5452f8aaab3Seschrock 			break;
546f9af39baSGeorge Wilson 
547*8704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
548*8704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
549*8704186eSDan McDonald 				if (!isprint(*check)) {
550*8704186eSDan McDonald 					zfs_error_aux(hdl,
551*8704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
552*8704186eSDan McDonald 					    "comment may only have printable "
553*8704186eSDan McDonald 					    "characters"));
554*8704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
555*8704186eSDan McDonald 					    errbuf);
556*8704186eSDan McDonald 					goto error;
557*8704186eSDan McDonald 				}
558*8704186eSDan McDonald 			}
559*8704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
560*8704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
561*8704186eSDan McDonald 				    "comment must not exceed %d characters"),
562*8704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
563*8704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
564*8704186eSDan McDonald 				goto error;
565*8704186eSDan McDonald 			}
566*8704186eSDan McDonald 			break;
567f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
568f9af39baSGeorge Wilson 			if (!flags.import) {
569f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
571f9af39baSGeorge Wilson 				    "import time"), propname);
572f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
573f9af39baSGeorge Wilson 				goto error;
574f9af39baSGeorge Wilson 			}
575f9af39baSGeorge Wilson 			break;
576990b4856Slling 		}
577990b4856Slling 	}
578990b4856Slling 
579990b4856Slling 	return (retprops);
580990b4856Slling error:
581990b4856Slling 	nvlist_free(retprops);
582990b4856Slling 	return (NULL);
583990b4856Slling }
584990b4856Slling 
585990b4856Slling /*
586990b4856Slling  * Set zpool property : propname=propval.
587990b4856Slling  */
588990b4856Slling int
589990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
590990b4856Slling {
591990b4856Slling 	zfs_cmd_t zc = { 0 };
592990b4856Slling 	int ret = -1;
593990b4856Slling 	char errbuf[1024];
594990b4856Slling 	nvlist_t *nvl = NULL;
595990b4856Slling 	nvlist_t *realprops;
596990b4856Slling 	uint64_t version;
597f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
598990b4856Slling 
599990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
600990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
601990b4856Slling 	    zhp->zpool_name);
602990b4856Slling 
603990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
604990b4856Slling 		return (no_memory(zhp->zpool_hdl));
605990b4856Slling 
606990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
607990b4856Slling 		nvlist_free(nvl);
608990b4856Slling 		return (no_memory(zhp->zpool_hdl));
609990b4856Slling 	}
610990b4856Slling 
611990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6120a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
613f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
614990b4856Slling 		nvlist_free(nvl);
615990b4856Slling 		return (-1);
616990b4856Slling 	}
617990b4856Slling 
618990b4856Slling 	nvlist_free(nvl);
619990b4856Slling 	nvl = realprops;
620990b4856Slling 
621990b4856Slling 	/*
622990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
623990b4856Slling 	 */
624990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
625990b4856Slling 
626990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
627990b4856Slling 		nvlist_free(nvl);
628990b4856Slling 		return (-1);
629990b4856Slling 	}
630990b4856Slling 
631990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
632990b4856Slling 
633990b4856Slling 	zcmd_free_nvlists(&zc);
634990b4856Slling 	nvlist_free(nvl);
635990b4856Slling 
636990b4856Slling 	if (ret)
637990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
638990b4856Slling 	else
639990b4856Slling 		(void) zpool_props_refresh(zhp);
640990b4856Slling 
641990b4856Slling 	return (ret);
642990b4856Slling }
643990b4856Slling 
644990b4856Slling int
645990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
646990b4856Slling {
647990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
648990b4856Slling 	zprop_list_t *entry;
649990b4856Slling 	char buf[ZFS_MAXPROPLEN];
650990b4856Slling 
651990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
652990b4856Slling 		return (-1);
653990b4856Slling 
654990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
655990b4856Slling 
656990b4856Slling 		if (entry->pl_fixed)
657990b4856Slling 			continue;
658990b4856Slling 
659990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
660990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
661990b4856Slling 		    NULL) == 0) {
662990b4856Slling 			if (strlen(buf) > entry->pl_width)
663990b4856Slling 				entry->pl_width = strlen(buf);
664990b4856Slling 		}
665990b4856Slling 	}
666990b4856Slling 
667990b4856Slling 	return (0);
668990b4856Slling }
669990b4856Slling 
670990b4856Slling 
671573ca77eSGeorge Wilson /*
672573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
673573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
674573ca77eSGeorge Wilson  */
675573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
676573ca77eSGeorge Wilson 
677fa9e4066Sahrens /*
678fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
679fa9e4066Sahrens  * 'buf'.
680fa9e4066Sahrens  */
681e7cbe64fSgw boolean_t
68299653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
683fa9e4066Sahrens {
684fa9e4066Sahrens 	namecheck_err_t why;
685fa9e4066Sahrens 	char what;
686b468a217Seschrock 	int ret;
687b468a217Seschrock 
688b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
689b468a217Seschrock 
690b468a217Seschrock 	/*
691b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
692b468a217Seschrock 	 * But we need to support users with existing pools that may now be
693b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
694b468a217Seschrock 	 * create (or import), and only in userland.
695b468a217Seschrock 	 */
696b468a217Seschrock 	if (ret == 0 && !isopen &&
697b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
698b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6998654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
7008654d025Sperrin 	    strcmp(pool, "log") == 0)) {
701e7cbe64fSgw 		if (hdl != NULL)
702e7cbe64fSgw 			zfs_error_aux(hdl,
703e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
70499653d4eSeschrock 		return (B_FALSE);
705b468a217Seschrock 	}
706b468a217Seschrock 
707fa9e4066Sahrens 
708b468a217Seschrock 	if (ret != 0) {
70999653d4eSeschrock 		if (hdl != NULL) {
710fa9e4066Sahrens 			switch (why) {
711b81d61a6Slling 			case NAME_ERR_TOOLONG:
71299653d4eSeschrock 				zfs_error_aux(hdl,
713b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
714b81d61a6Slling 				break;
715b81d61a6Slling 
716fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
71799653d4eSeschrock 				zfs_error_aux(hdl,
718fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
719fa9e4066Sahrens 				    "'%c' in pool name"), what);
720fa9e4066Sahrens 				break;
721fa9e4066Sahrens 
722fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
72399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
72499653d4eSeschrock 				    "name must begin with a letter"));
725fa9e4066Sahrens 				break;
726fa9e4066Sahrens 
727fa9e4066Sahrens 			case NAME_ERR_RESERVED:
72899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
72999653d4eSeschrock 				    "name is reserved"));
730fa9e4066Sahrens 				break;
731fa9e4066Sahrens 
732fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
73399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
73499653d4eSeschrock 				    "pool name is reserved"));
735fa9e4066Sahrens 				break;
7365ad82045Snd 
7375ad82045Snd 			case NAME_ERR_LEADING_SLASH:
7385ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7395ad82045Snd 				    "leading slash in name"));
7405ad82045Snd 				break;
7415ad82045Snd 
7425ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
7435ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7445ad82045Snd 				    "empty component in name"));
7455ad82045Snd 				break;
7465ad82045Snd 
7475ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
7485ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7495ad82045Snd 				    "trailing slash in name"));
7505ad82045Snd 				break;
7515ad82045Snd 
7525ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
7535ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7545ad82045Snd 				    "multiple '@' delimiters in name"));
7555ad82045Snd 				break;
7565ad82045Snd 
757fa9e4066Sahrens 			}
758fa9e4066Sahrens 		}
75999653d4eSeschrock 		return (B_FALSE);
760fa9e4066Sahrens 	}
761fa9e4066Sahrens 
76299653d4eSeschrock 	return (B_TRUE);
763fa9e4066Sahrens }
764fa9e4066Sahrens 
765fa9e4066Sahrens /*
766fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
767fa9e4066Sahrens  * state.
768fa9e4066Sahrens  */
769fa9e4066Sahrens zpool_handle_t *
77099653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
771fa9e4066Sahrens {
772fa9e4066Sahrens 	zpool_handle_t *zhp;
77394de1d4cSeschrock 	boolean_t missing;
774fa9e4066Sahrens 
775fa9e4066Sahrens 	/*
776fa9e4066Sahrens 	 * Make sure the pool name is valid.
777fa9e4066Sahrens 	 */
77899653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
779ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
78099653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
78199653d4eSeschrock 		    pool);
782fa9e4066Sahrens 		return (NULL);
783fa9e4066Sahrens 	}
784fa9e4066Sahrens 
78599653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
78699653d4eSeschrock 		return (NULL);
787fa9e4066Sahrens 
78899653d4eSeschrock 	zhp->zpool_hdl = hdl;
789fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
790fa9e4066Sahrens 
79194de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
79294de1d4cSeschrock 		zpool_close(zhp);
79394de1d4cSeschrock 		return (NULL);
79494de1d4cSeschrock 	}
79594de1d4cSeschrock 
79694de1d4cSeschrock 	if (missing) {
797990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
798ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
799990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
80094de1d4cSeschrock 		zpool_close(zhp);
80194de1d4cSeschrock 		return (NULL);
802fa9e4066Sahrens 	}
803fa9e4066Sahrens 
804fa9e4066Sahrens 	return (zhp);
805fa9e4066Sahrens }
806fa9e4066Sahrens 
807fa9e4066Sahrens /*
808fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
809fa9e4066Sahrens  * the configuration cache may be out of date).
810fa9e4066Sahrens  */
81194de1d4cSeschrock int
81294de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
813fa9e4066Sahrens {
814fa9e4066Sahrens 	zpool_handle_t *zhp;
81594de1d4cSeschrock 	boolean_t missing;
816fa9e4066Sahrens 
81794de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
81894de1d4cSeschrock 		return (-1);
819fa9e4066Sahrens 
82099653d4eSeschrock 	zhp->zpool_hdl = hdl;
821fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
822fa9e4066Sahrens 
82394de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
82494de1d4cSeschrock 		zpool_close(zhp);
82594de1d4cSeschrock 		return (-1);
826fa9e4066Sahrens 	}
827fa9e4066Sahrens 
82894de1d4cSeschrock 	if (missing) {
82994de1d4cSeschrock 		zpool_close(zhp);
83094de1d4cSeschrock 		*ret = NULL;
83194de1d4cSeschrock 		return (0);
83294de1d4cSeschrock 	}
83394de1d4cSeschrock 
83494de1d4cSeschrock 	*ret = zhp;
83594de1d4cSeschrock 	return (0);
836fa9e4066Sahrens }
837fa9e4066Sahrens 
838fa9e4066Sahrens /*
839fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
840fa9e4066Sahrens  * state.
841fa9e4066Sahrens  */
842fa9e4066Sahrens zpool_handle_t *
84399653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
844fa9e4066Sahrens {
845fa9e4066Sahrens 	zpool_handle_t *zhp;
846fa9e4066Sahrens 
84799653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
848fa9e4066Sahrens 		return (NULL);
849fa9e4066Sahrens 
850fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
851ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
85299653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
853fa9e4066Sahrens 		zpool_close(zhp);
854fa9e4066Sahrens 		return (NULL);
855fa9e4066Sahrens 	}
856fa9e4066Sahrens 
857fa9e4066Sahrens 	return (zhp);
858fa9e4066Sahrens }
859fa9e4066Sahrens 
860fa9e4066Sahrens /*
861fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
862fa9e4066Sahrens  */
863fa9e4066Sahrens void
864fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
865fa9e4066Sahrens {
866fa9e4066Sahrens 	if (zhp->zpool_config)
867fa9e4066Sahrens 		nvlist_free(zhp->zpool_config);
868088e9d47Seschrock 	if (zhp->zpool_old_config)
869088e9d47Seschrock 		nvlist_free(zhp->zpool_old_config);
870b1b8ab34Slling 	if (zhp->zpool_props)
871b1b8ab34Slling 		nvlist_free(zhp->zpool_props);
872fa9e4066Sahrens 	free(zhp);
873fa9e4066Sahrens }
874fa9e4066Sahrens 
875fa9e4066Sahrens /*
876fa9e4066Sahrens  * Return the name of the pool.
877fa9e4066Sahrens  */
878fa9e4066Sahrens const char *
879fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
880fa9e4066Sahrens {
881fa9e4066Sahrens 	return (zhp->zpool_name);
882fa9e4066Sahrens }
883fa9e4066Sahrens 
884fa9e4066Sahrens 
885fa9e4066Sahrens /*
886fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
887fa9e4066Sahrens  */
888fa9e4066Sahrens int
889fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
890fa9e4066Sahrens {
891fa9e4066Sahrens 	return (zhp->zpool_state);
892fa9e4066Sahrens }
893fa9e4066Sahrens 
894fa9e4066Sahrens /*
895fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
896fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
897fa9e4066Sahrens  * don't have to worry about error semantics.
898fa9e4066Sahrens  */
899fa9e4066Sahrens int
90099653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
9010a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
902fa9e4066Sahrens {
903fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
9040a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
9050a48a24eStimh 	nvlist_t *zc_props = NULL;
90699653d4eSeschrock 	char msg[1024];
907990b4856Slling 	char *altroot;
9080a48a24eStimh 	int ret = -1;
909fa9e4066Sahrens 
91099653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
91199653d4eSeschrock 	    "cannot create '%s'"), pool);
912fa9e4066Sahrens 
91399653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
91499653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
915fa9e4066Sahrens 
916351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
917990b4856Slling 		return (-1);
918fa9e4066Sahrens 
9190a48a24eStimh 	if (props) {
920f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
921f9af39baSGeorge Wilson 
9220a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
923f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
9240a48a24eStimh 			goto create_failed;
9250a48a24eStimh 		}
9260a48a24eStimh 	}
92799653d4eSeschrock 
9280a48a24eStimh 	if (fsprops) {
9290a48a24eStimh 		uint64_t zoned;
9300a48a24eStimh 		char *zonestr;
9310a48a24eStimh 
9320a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
9330a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
9340a48a24eStimh 		    strcmp(zonestr, "on") == 0);
9350a48a24eStimh 
9360a48a24eStimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
9370a48a24eStimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
9380a48a24eStimh 			goto create_failed;
9390a48a24eStimh 		}
9400a48a24eStimh 		if (!zc_props &&
9410a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
9420a48a24eStimh 			goto create_failed;
9430a48a24eStimh 		}
9440a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
9450a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
9460a48a24eStimh 			goto create_failed;
9470a48a24eStimh 		}
948351420b3Slling 	}
949fa9e4066Sahrens 
9500a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
9510a48a24eStimh 		goto create_failed;
9520a48a24eStimh 
953990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
954fa9e4066Sahrens 
9550a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
956351420b3Slling 
957e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
9580a48a24eStimh 		nvlist_free(zc_props);
9590a48a24eStimh 		nvlist_free(zc_fsprops);
960fa9e4066Sahrens 
96199653d4eSeschrock 		switch (errno) {
962fa9e4066Sahrens 		case EBUSY:
963fa9e4066Sahrens 			/*
964fa9e4066Sahrens 			 * This can happen if the user has specified the same
965fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
966fa9e4066Sahrens 			 * until we try to add it and see we already have a
967fa9e4066Sahrens 			 * label.
968fa9e4066Sahrens 			 */
96999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
97099653d4eSeschrock 			    "one or more vdevs refer to the same device"));
97199653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
972fa9e4066Sahrens 
973fa9e4066Sahrens 		case EOVERFLOW:
974fa9e4066Sahrens 			/*
97599653d4eSeschrock 			 * This occurs when one of the devices is below
976fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
977fa9e4066Sahrens 			 * device was the problem device since there's no
978fa9e4066Sahrens 			 * reliable way to determine device size from userland.
979fa9e4066Sahrens 			 */
980fa9e4066Sahrens 			{
981fa9e4066Sahrens 				char buf[64];
982fa9e4066Sahrens 
983fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
984fa9e4066Sahrens 
98599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
98699653d4eSeschrock 				    "one or more devices is less than the "
98799653d4eSeschrock 				    "minimum size (%s)"), buf);
988fa9e4066Sahrens 			}
98999653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
990fa9e4066Sahrens 
991fa9e4066Sahrens 		case ENOSPC:
99299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
99399653d4eSeschrock 			    "one or more devices is out of space"));
99499653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
995fa9e4066Sahrens 
996fa94a07fSbrendan 		case ENOTBLK:
997fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
999fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1000fa94a07fSbrendan 
1001fa9e4066Sahrens 		default:
100299653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1003fa9e4066Sahrens 		}
1004fa9e4066Sahrens 	}
1005fa9e4066Sahrens 
1006fa9e4066Sahrens 	/*
1007fa9e4066Sahrens 	 * If this is an alternate root pool, then we automatically set the
1008e9dbad6fSeschrock 	 * mountpoint of the root dataset to be '/'.
1009fa9e4066Sahrens 	 */
1010990b4856Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1011990b4856Slling 	    &altroot) == 0) {
1012fa9e4066Sahrens 		zfs_handle_t *zhp;
1013fa9e4066Sahrens 
1014990b4856Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1015e9dbad6fSeschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1016e9dbad6fSeschrock 		    "/") == 0);
1017fa9e4066Sahrens 
1018fa9e4066Sahrens 		zfs_close(zhp);
1019fa9e4066Sahrens 	}
1020fa9e4066Sahrens 
10210a48a24eStimh create_failed:
1022351420b3Slling 	zcmd_free_nvlists(&zc);
10230a48a24eStimh 	nvlist_free(zc_props);
10240a48a24eStimh 	nvlist_free(zc_fsprops);
10250a48a24eStimh 	return (ret);
1026fa9e4066Sahrens }
1027fa9e4066Sahrens 
1028fa9e4066Sahrens /*
1029fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1030fa9e4066Sahrens  * datasets left in the pool.
1031fa9e4066Sahrens  */
1032fa9e4066Sahrens int
1033fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp)
1034fa9e4066Sahrens {
1035fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1036fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
103799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
103899653d4eSeschrock 	char msg[1024];
1039fa9e4066Sahrens 
1040fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1041cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1042fa9e4066Sahrens 		return (-1);
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1045fa9e4066Sahrens 
1046cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
104799653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
104899653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1049fa9e4066Sahrens 
105099653d4eSeschrock 		if (errno == EROFS) {
105199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105299653d4eSeschrock 			    "one or more devices is read only"));
105399653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
105499653d4eSeschrock 		} else {
105599653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1056fa9e4066Sahrens 		}
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 		if (zfp)
1059fa9e4066Sahrens 			zfs_close(zfp);
1060fa9e4066Sahrens 		return (-1);
1061fa9e4066Sahrens 	}
1062fa9e4066Sahrens 
1063fa9e4066Sahrens 	if (zfp) {
1064fa9e4066Sahrens 		remove_mountpoint(zfp);
1065fa9e4066Sahrens 		zfs_close(zfp);
1066fa9e4066Sahrens 	}
1067fa9e4066Sahrens 
1068fa9e4066Sahrens 	return (0);
1069fa9e4066Sahrens }
1070fa9e4066Sahrens 
1071fa9e4066Sahrens /*
1072fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1073fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1074fa9e4066Sahrens  */
1075fa9e4066Sahrens int
1076fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1077fa9e4066Sahrens {
1078e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
107999653d4eSeschrock 	int ret;
108099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
108199653d4eSeschrock 	char msg[1024];
1082fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1083fa94a07fSbrendan 	uint_t nspares, nl2cache;
108499653d4eSeschrock 
108599653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
108699653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
108799653d4eSeschrock 
1088fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1089fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
109099653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
109199653d4eSeschrock 	    &spares, &nspares) == 0) {
109299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
109399653d4eSeschrock 		    "upgraded to add hot spares"));
109499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
109599653d4eSeschrock 	}
1096fa9e4066Sahrens 
1097b5b76fecSGeorge Wilson 	if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1098b5b76fecSGeorge Wilson 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1099b5b76fecSGeorge Wilson 		uint64_t s;
1100b5b76fecSGeorge Wilson 
1101b5b76fecSGeorge Wilson 		for (s = 0; s < nspares; s++) {
1102b5b76fecSGeorge Wilson 			char *path;
1103b5b76fecSGeorge Wilson 
1104b5b76fecSGeorge Wilson 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1105b5b76fecSGeorge Wilson 			    &path) == 0 && pool_uses_efi(spares[s])) {
1106b5b76fecSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1107b5b76fecSGeorge Wilson 				    "device '%s' contains an EFI label and "
1108b5b76fecSGeorge Wilson 				    "cannot be used on root pools."),
110988ecc943SGeorge Wilson 				    zpool_vdev_name(hdl, NULL, spares[s],
111088ecc943SGeorge Wilson 				    B_FALSE));
1111b5b76fecSGeorge Wilson 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1112b5b76fecSGeorge Wilson 			}
1113b5b76fecSGeorge Wilson 		}
1114b5b76fecSGeorge Wilson 	}
1115b5b76fecSGeorge Wilson 
1116fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1117fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1118fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1119fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1120fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1121fa94a07fSbrendan 		    "upgraded to add cache devices"));
1122fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1123fa94a07fSbrendan 	}
1124fa94a07fSbrendan 
1125990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
112699653d4eSeschrock 		return (-1);
1127fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1128fa9e4066Sahrens 
1129cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1130fa9e4066Sahrens 		switch (errno) {
1131fa9e4066Sahrens 		case EBUSY:
1132fa9e4066Sahrens 			/*
1133fa9e4066Sahrens 			 * This can happen if the user has specified the same
1134fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1135fa9e4066Sahrens 			 * until we try to add it and see we already have a
1136fa9e4066Sahrens 			 * label.
1137fa9e4066Sahrens 			 */
113899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113999653d4eSeschrock 			    "one or more vdevs refer to the same device"));
114099653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1141fa9e4066Sahrens 			break;
1142fa9e4066Sahrens 
1143fa9e4066Sahrens 		case EOVERFLOW:
1144fa9e4066Sahrens 			/*
1145fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1146fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1147fa9e4066Sahrens 			 * device was the problem device since there's no
1148fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1149fa9e4066Sahrens 			 */
1150fa9e4066Sahrens 			{
1151fa9e4066Sahrens 				char buf[64];
1152fa9e4066Sahrens 
1153fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1154fa9e4066Sahrens 
115599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115699653d4eSeschrock 				    "device is less than the minimum "
115799653d4eSeschrock 				    "size (%s)"), buf);
1158fa9e4066Sahrens 			}
115999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
116099653d4eSeschrock 			break;
116199653d4eSeschrock 
116299653d4eSeschrock 		case ENOTSUP:
116399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11648654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
116599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1166fa9e4066Sahrens 			break;
1167fa9e4066Sahrens 
1168b1b8ab34Slling 		case EDOM:
1169b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11708654d025Sperrin 			    "root pool can not have multiple vdevs"
11718654d025Sperrin 			    " or separate logs"));
1172b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1173b1b8ab34Slling 			break;
1174b1b8ab34Slling 
1175fa94a07fSbrendan 		case ENOTBLK:
1176fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1177fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1178fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1179fa94a07fSbrendan 			break;
1180fa94a07fSbrendan 
1181fa9e4066Sahrens 		default:
118299653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1183fa9e4066Sahrens 		}
1184fa9e4066Sahrens 
118599653d4eSeschrock 		ret = -1;
118699653d4eSeschrock 	} else {
118799653d4eSeschrock 		ret = 0;
1188fa9e4066Sahrens 	}
1189fa9e4066Sahrens 
1190e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1191fa9e4066Sahrens 
119299653d4eSeschrock 	return (ret);
1193fa9e4066Sahrens }
1194fa9e4066Sahrens 
1195fa9e4066Sahrens /*
1196fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1197fa9e4066Sahrens  * mounted datasets in the pool.
1198fa9e4066Sahrens  */
1199fa9e4066Sahrens int
1200394ab0cbSGeorge Wilson zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1201fa9e4066Sahrens {
1202fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
120389a89ebfSlling 	char msg[1024];
1204fa9e4066Sahrens 
120589a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
120689a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
120789a89ebfSlling 
1208fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
120989a89ebfSlling 	zc.zc_cookie = force;
1210394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
121189a89ebfSlling 
121289a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
121389a89ebfSlling 		switch (errno) {
121489a89ebfSlling 		case EXDEV:
121589a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
121689a89ebfSlling 			    "use '-f' to override the following errors:\n"
121789a89ebfSlling 			    "'%s' has an active shared spare which could be"
121889a89ebfSlling 			    " used by other pools once '%s' is exported."),
121989a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
122089a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
122189a89ebfSlling 			    msg));
122289a89ebfSlling 		default:
122389a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
122489a89ebfSlling 			    msg));
122589a89ebfSlling 		}
122689a89ebfSlling 	}
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 	return (0);
1229fa9e4066Sahrens }
1230fa9e4066Sahrens 
1231394ab0cbSGeorge Wilson int
1232394ab0cbSGeorge Wilson zpool_export(zpool_handle_t *zhp, boolean_t force)
1233394ab0cbSGeorge Wilson {
1234394ab0cbSGeorge Wilson 	return (zpool_export_common(zhp, force, B_FALSE));
1235394ab0cbSGeorge Wilson }
1236394ab0cbSGeorge Wilson 
1237394ab0cbSGeorge Wilson int
1238394ab0cbSGeorge Wilson zpool_export_force(zpool_handle_t *zhp)
1239394ab0cbSGeorge Wilson {
1240394ab0cbSGeorge Wilson 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1241394ab0cbSGeorge Wilson }
1242394ab0cbSGeorge Wilson 
1243468c413aSTim Haley static void
1244468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
12454b964adaSGeorge Wilson     nvlist_t *config)
1246468c413aSTim Haley {
12474b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1248468c413aSTim Haley 	uint64_t rewindto;
1249468c413aSTim Haley 	int64_t loss = -1;
1250468c413aSTim Haley 	struct tm t;
1251468c413aSTim Haley 	char timestr[128];
1252468c413aSTim Haley 
12534b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
12544b964adaSGeorge Wilson 		return;
12554b964adaSGeorge Wilson 
12564b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1257468c413aSTim Haley 		return;
1258468c413aSTim Haley 
12594b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1260468c413aSTim Haley 		return;
12614b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1262468c413aSTim Haley 
1263468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1264468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1265468c413aSTim Haley 		if (dryrun) {
1266468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1267468c413aSTim Haley 			    "Would be able to return %s "
1268468c413aSTim Haley 			    "to its state as of %s.\n"),
1269468c413aSTim Haley 			    name, timestr);
1270468c413aSTim Haley 		} else {
1271468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1272468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1273468c413aSTim Haley 			    name, timestr);
1274468c413aSTim Haley 		}
1275468c413aSTim Haley 		if (loss > 120) {
1276468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1277468c413aSTim Haley 			    "%s approximately %lld "),
1278468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1279468c413aSTim Haley 			    (loss + 30) / 60);
1280468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1281468c413aSTim Haley 			    "minutes of transactions.\n"));
1282468c413aSTim Haley 		} else if (loss > 0) {
1283468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1284468c413aSTim Haley 			    "%s approximately %lld "),
1285468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1286468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1287468c413aSTim Haley 			    "seconds of transactions.\n"));
1288468c413aSTim Haley 		}
1289468c413aSTim Haley 	}
1290468c413aSTim Haley }
1291468c413aSTim Haley 
1292468c413aSTim Haley void
1293468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1294468c413aSTim Haley     nvlist_t *config)
1295468c413aSTim Haley {
12964b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1297468c413aSTim Haley 	int64_t loss = -1;
1298468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1299468c413aSTim Haley 	uint64_t rewindto;
1300468c413aSTim Haley 	struct tm t;
1301468c413aSTim Haley 	char timestr[128];
1302468c413aSTim Haley 
1303468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1304468c413aSTim Haley 		return;
1305468c413aSTim Haley 
1306468c413aSTim Haley 	if (reason >= 0)
1307468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1308468c413aSTim Haley 	else
1309468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1310468c413aSTim Haley 
1311468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
13124b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
13134b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1314468c413aSTim Haley 		goto no_info;
1315468c413aSTim Haley 
13164b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
13174b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1318468c413aSTim Haley 	    &edata);
1319468c413aSTim Haley 
1320468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1321468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1322468c413aSTim Haley 
1323468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1324468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1325468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1326468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1327468c413aSTim Haley 		    "\tshould correct the problem.  "),
1328468c413aSTim Haley 		    timestr);
1329468c413aSTim Haley 	} else {
1330468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1331468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1332468c413aSTim Haley 		    "should correct the problem.\n\t"));
1333468c413aSTim Haley 	}
1334468c413aSTim Haley 
1335468c413aSTim Haley 	if (loss > 120) {
1336468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1337468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1338468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1339468c413aSTim Haley 	} else if (loss > 0) {
1340468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1341468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1342468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1343468c413aSTim Haley 	}
1344468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1345468c413aSTim Haley 		if (edata == 1) {
1346468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1347468c413aSTim Haley 			    "After rewind, at least\n"
1348468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1349468c413aSTim Haley 		} else {
1350468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1351468c413aSTim Haley 			    "After rewind, several\n"
1352468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1353468c413aSTim Haley 		}
1354468c413aSTim Haley 	}
1355468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1356a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1357a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1358468c413aSTim Haley 
1359468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1360468c413aSTim Haley 	    "A scrub of the pool\n"
1361468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1362468c413aSTim Haley 	return;
1363468c413aSTim Haley 
1364468c413aSTim Haley no_info:
1365468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1366468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1367468c413aSTim Haley }
1368468c413aSTim Haley 
1369fa9e4066Sahrens /*
1370990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1371990b4856Slling  * if possible.
1372990b4856Slling  *
1373990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1374990b4856Slling  * new properties value to be set.
1375fa9e4066Sahrens  */
1376fa9e4066Sahrens int
137799653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1378990b4856Slling     char *altroot)
1379990b4856Slling {
1380990b4856Slling 	nvlist_t *props = NULL;
1381990b4856Slling 	int ret;
1382990b4856Slling 
1383990b4856Slling 	if (altroot != NULL) {
1384990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1385990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1386990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1387990b4856Slling 			    newname));
1388990b4856Slling 		}
1389990b4856Slling 
1390990b4856Slling 		if (nvlist_add_string(props,
1391352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1392352d8027SGeorge Wilson 		    nvlist_add_string(props,
1393352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1394990b4856Slling 			nvlist_free(props);
1395990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1396990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1397990b4856Slling 			    newname));
1398990b4856Slling 		}
1399990b4856Slling 	}
1400990b4856Slling 
14014b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
14024b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1403990b4856Slling 	if (props)
1404990b4856Slling 		nvlist_free(props);
1405990b4856Slling 	return (ret);
1406990b4856Slling }
1407990b4856Slling 
14084b964adaSGeorge Wilson static void
14094b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
14104b964adaSGeorge Wilson     int indent)
14114b964adaSGeorge Wilson {
14124b964adaSGeorge Wilson 	nvlist_t **child;
14134b964adaSGeorge Wilson 	uint_t c, children;
14144b964adaSGeorge Wilson 	char *vname;
14154b964adaSGeorge Wilson 	uint64_t is_log = 0;
14164b964adaSGeorge Wilson 
14174b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
14184b964adaSGeorge Wilson 	    &is_log);
14194b964adaSGeorge Wilson 
14204b964adaSGeorge Wilson 	if (name != NULL)
14214b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
14224b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
14234b964adaSGeorge Wilson 
14244b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
14254b964adaSGeorge Wilson 	    &child, &children) != 0)
14264b964adaSGeorge Wilson 		return;
14274b964adaSGeorge Wilson 
14284b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
14294b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
14304b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
14314b964adaSGeorge Wilson 		free(vname);
14324b964adaSGeorge Wilson 	}
14334b964adaSGeorge Wilson }
14344b964adaSGeorge Wilson 
1435990b4856Slling /*
1436990b4856Slling  * Import the given pool using the known configuration and a list of
1437990b4856Slling  * properties to be set. The configuration should have come from
1438990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1439990b4856Slling  * is imported with a different name.
1440990b4856Slling  */
1441990b4856Slling int
1442990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
14434b964adaSGeorge Wilson     nvlist_t *props, int flags)
1444fa9e4066Sahrens {
1445e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1446468c413aSTim Haley 	zpool_rewind_policy_t policy;
14474b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
14484b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
14494b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1450fa9e4066Sahrens 	char *thename;
1451fa9e4066Sahrens 	char *origname;
1452fa9e4066Sahrens 	int ret;
14534b964adaSGeorge Wilson 	int error = 0;
1454990b4856Slling 	char errbuf[1024];
1455fa9e4066Sahrens 
1456fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1457fa9e4066Sahrens 	    &origname) == 0);
1458fa9e4066Sahrens 
1459990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1460990b4856Slling 	    "cannot import pool '%s'"), origname);
1461990b4856Slling 
1462fa9e4066Sahrens 	if (newname != NULL) {
146399653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1464ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
146599653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
146699653d4eSeschrock 			    newname));
1467fa9e4066Sahrens 		thename = (char *)newname;
1468fa9e4066Sahrens 	} else {
1469fa9e4066Sahrens 		thename = origname;
1470fa9e4066Sahrens 	}
1471fa9e4066Sahrens 
1472990b4856Slling 	if (props) {
1473990b4856Slling 		uint64_t version;
1474f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1475fa9e4066Sahrens 
1476990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1477990b4856Slling 		    &version) == 0);
1478fa9e4066Sahrens 
14790a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1480f9af39baSGeorge Wilson 		    props, version, flags, errbuf)) == NULL) {
1481990b4856Slling 			return (-1);
1482351420b3Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1483351420b3Slling 			nvlist_free(props);
1484990b4856Slling 			return (-1);
1485351420b3Slling 		}
1486990b4856Slling 	}
1487990b4856Slling 
1488990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1489fa9e4066Sahrens 
1490fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1491ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1492fa9e4066Sahrens 
1493351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1494351420b3Slling 		nvlist_free(props);
149599653d4eSeschrock 		return (-1);
1496351420b3Slling 	}
149757f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1498468c413aSTim Haley 		nvlist_free(props);
1499468c413aSTim Haley 		return (-1);
1500468c413aSTim Haley 	}
1501fa9e4066Sahrens 
15024b964adaSGeorge Wilson 	zc.zc_cookie = flags;
15034b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
15044b964adaSGeorge Wilson 	    errno == ENOMEM) {
15054b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
15064b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
15074b964adaSGeorge Wilson 			return (-1);
15084b964adaSGeorge Wilson 		}
15094b964adaSGeorge Wilson 	}
15104b964adaSGeorge Wilson 	if (ret != 0)
15114b964adaSGeorge Wilson 		error = errno;
15124b964adaSGeorge Wilson 
15134b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
15144b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
15154b964adaSGeorge Wilson 
15164b964adaSGeorge Wilson 	if (error) {
1517fa9e4066Sahrens 		char desc[1024];
1518468c413aSTim Haley 
1519468c413aSTim Haley 		/*
1520468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1521468c413aSTim Haley 		 * looks like if we found a best txg
1522468c413aSTim Haley 		 */
15234b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1524468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
15254b964adaSGeorge Wilson 			    B_TRUE, nv);
15264b964adaSGeorge Wilson 			nvlist_free(nv);
1527468c413aSTim Haley 			return (-1);
1528468c413aSTim Haley 		}
1529468c413aSTim Haley 
1530fa9e4066Sahrens 		if (newname == NULL)
1531fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1532fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1533fa9e4066Sahrens 			    thename);
1534fa9e4066Sahrens 		else
1535fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1536fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1537fa9e4066Sahrens 			    origname, thename);
1538fa9e4066Sahrens 
15394b964adaSGeorge Wilson 		switch (error) {
1540ea8dc4b6Seschrock 		case ENOTSUP:
1541ea8dc4b6Seschrock 			/*
1542ea8dc4b6Seschrock 			 * Unsupported version.
1543ea8dc4b6Seschrock 			 */
154499653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1545ea8dc4b6Seschrock 			break;
1546ea8dc4b6Seschrock 
1547b5989ec7Seschrock 		case EINVAL:
1548b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1549b5989ec7Seschrock 			break;
1550b5989ec7Seschrock 
155154a91118SChris Kirby 		case EROFS:
155254a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155354a91118SChris Kirby 			    "one or more devices is read only"));
155454a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
155554a91118SChris Kirby 			break;
155654a91118SChris Kirby 
15574b964adaSGeorge Wilson 		case ENXIO:
15584b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
15594b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
15604b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
15614b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
15624b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
15634b964adaSGeorge Wilson 				    "The devices below are missing, use "
15644b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
15654b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
15664b964adaSGeorge Wilson 				(void) printf("\n");
15674b964adaSGeorge Wilson 			}
15684b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
15694b964adaSGeorge Wilson 			break;
15704b964adaSGeorge Wilson 
15714b964adaSGeorge Wilson 		case EEXIST:
15724b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
15734b964adaSGeorge Wilson 			break;
15744b964adaSGeorge Wilson 
1575fa9e4066Sahrens 		default:
15764b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1577468c413aSTim Haley 			zpool_explain_recover(hdl,
15784b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1579468c413aSTim Haley 			break;
1580fa9e4066Sahrens 		}
1581fa9e4066Sahrens 
15824b964adaSGeorge Wilson 		nvlist_free(nv);
1583fa9e4066Sahrens 		ret = -1;
1584fa9e4066Sahrens 	} else {
1585fa9e4066Sahrens 		zpool_handle_t *zhp;
1586ecd6cf80Smarks 
1587fa9e4066Sahrens 		/*
1588fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1589fa9e4066Sahrens 		 */
1590681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
159194de1d4cSeschrock 			ret = -1;
1592681d9761SEric Taylor 		else if (zhp != NULL)
1593fa9e4066Sahrens 			zpool_close(zhp);
1594468c413aSTim Haley 		if (policy.zrp_request &
1595468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1596468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
15974b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1598468c413aSTim Haley 		}
15994b964adaSGeorge Wilson 		nvlist_free(nv);
1600468c413aSTim Haley 		return (0);
1601fa9e4066Sahrens 	}
1602fa9e4066Sahrens 
1603e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1604351420b3Slling 	nvlist_free(props);
1605351420b3Slling 
1606fa9e4066Sahrens 	return (ret);
1607fa9e4066Sahrens }
1608fa9e4066Sahrens 
1609fa9e4066Sahrens /*
16103f9d6ad7SLin Ling  * Scan the pool.
1611fa9e4066Sahrens  */
1612fa9e4066Sahrens int
16133f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1614fa9e4066Sahrens {
1615fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1616fa9e4066Sahrens 	char msg[1024];
161799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1618fa9e4066Sahrens 
1619fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
16203f9d6ad7SLin Ling 	zc.zc_cookie = func;
1621fa9e4066Sahrens 
1622cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
16233f9d6ad7SLin Ling 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1624fa9e4066Sahrens 		return (0);
1625fa9e4066Sahrens 
16263f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
16273f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
16283f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
16293f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
16303f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
16313f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
16323f9d6ad7SLin Ling 		    zc.zc_name);
16333f9d6ad7SLin Ling 	} else {
16343f9d6ad7SLin Ling 		assert(!"unexpected result");
16353f9d6ad7SLin Ling 	}
1636fa9e4066Sahrens 
16373f9d6ad7SLin Ling 	if (errno == EBUSY) {
16383f9d6ad7SLin Ling 		nvlist_t *nvroot;
16393f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
16403f9d6ad7SLin Ling 		uint_t psc;
16413f9d6ad7SLin Ling 
16423f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
16433f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16443f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
16453f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
16463f9d6ad7SLin Ling 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
16473f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
16483f9d6ad7SLin Ling 		else
16493f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
16503f9d6ad7SLin Ling 	} else if (errno == ENOENT) {
16513f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
16523f9d6ad7SLin Ling 	} else {
165399653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
16543f9d6ad7SLin Ling 	}
1655fa9e4066Sahrens }
1656fa9e4066Sahrens 
16573fdda499SJohn Harres /*
16583fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
16593fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
16603fdda499SJohn Harres  * verification of the s# part.
16613fdda499SJohn Harres  */
16623fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
16633fdda499SJohn Harres 
16643fdda499SJohn Harres /*
16653fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
16663fdda499SJohn Harres  * and the like.
16673fdda499SJohn Harres  */
16683fdda499SJohn Harres static int
16693fdda499SJohn Harres ctd_check_path(char *str) {
16703fdda499SJohn Harres 	/*
16713fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
16723fdda499SJohn Harres 	 */
16733fdda499SJohn Harres 	if (str && str[0] == '/') {
16743fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
16753fdda499SJohn Harres 
16763fdda499SJohn Harres 		/*
16773fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
16783fdda499SJohn Harres 		 * component of the string instead.
16793fdda499SJohn Harres 		 */
16803fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
16813fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
16823fdda499SJohn Harres 				;
16833fdda499SJohn Harres 		}
16843fdda499SJohn Harres 		str = tmp + 1;
16853fdda499SJohn Harres 	}
16863fdda499SJohn Harres 	return (CTD_CHECK(str));
16873fdda499SJohn Harres }
16883fdda499SJohn Harres 
1689a43d325bSek /*
1690573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1691573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1692a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1693a43d325bSek  * spare; but FALSE if its an INUSE spare.
1694a43d325bSek  */
169599653d4eSeschrock static nvlist_t *
1696573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1697573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1698ea8dc4b6Seschrock {
1699ea8dc4b6Seschrock 	uint_t c, children;
1700ea8dc4b6Seschrock 	nvlist_t **child;
170199653d4eSeschrock 	nvlist_t *ret;
1702ee0eb9f2SEric Schrock 	uint64_t is_log;
1703573ca77eSGeorge Wilson 	char *srchkey;
1704573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1705573ca77eSGeorge Wilson 
1706573ca77eSGeorge Wilson 	/* Nothing to look for */
1707573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1708573ca77eSGeorge Wilson 		return (NULL);
1709ea8dc4b6Seschrock 
1710573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1711573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1712573ca77eSGeorge Wilson 
1713573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1714cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1715573ca77eSGeorge Wilson 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1716cb04b873SMark J Musante 			uint64_t srchval, theguid;
1717cb04b873SMark J Musante 
1718cb04b873SMark J Musante 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1719cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1720cb04b873SMark J Musante 			    &theguid) == 0);
1721cb04b873SMark J Musante 			if (theguid == srchval)
1722cb04b873SMark J Musante 				return (nv);
1723573ca77eSGeorge Wilson 		}
1724573ca77eSGeorge Wilson 		break;
1725573ca77eSGeorge Wilson 
1726573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1727573ca77eSGeorge Wilson 		char *srchval, *val;
1728573ca77eSGeorge Wilson 
1729573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
1730573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1731573ca77eSGeorge Wilson 			break;
1732ea8dc4b6Seschrock 
1733ea8dc4b6Seschrock 		/*
17343fdda499SJohn Harres 		 * Search for the requested value. Special cases:
17353fdda499SJohn Harres 		 *
17363fdda499SJohn Harres 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
17373fdda499SJohn Harres 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
17383fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
17393fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
17403fdda499SJohn Harres 		 *
174188ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
1742ea8dc4b6Seschrock 		 */
17433fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
17443fdda499SJohn Harres 		    ctd_check_path(val)) {
1745573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
1746573ca77eSGeorge Wilson 
1747573ca77eSGeorge Wilson 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1748573ca77eSGeorge Wilson 			    &wholedisk);
1749573ca77eSGeorge Wilson 			if (wholedisk) {
17503fdda499SJohn Harres 				int slen = strlen(srchval);
17513fdda499SJohn Harres 				int vlen = strlen(val);
17523fdda499SJohn Harres 
17533fdda499SJohn Harres 				if (slen != vlen - 2)
17543fdda499SJohn Harres 					break;
17553fdda499SJohn Harres 
17563fdda499SJohn Harres 				/*
17573fdda499SJohn Harres 				 * make_leaf_vdev() should only set
17583fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
17593fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
17603fdda499SJohn Harres 				 * room for the indices used next.
17613fdda499SJohn Harres 				 */
17623fdda499SJohn Harres 				ASSERT(vlen >= 6);
17633fdda499SJohn Harres 
17643fdda499SJohn Harres 				/*
17653fdda499SJohn Harres 				 * strings identical except trailing "s0"
17663fdda499SJohn Harres 				 */
17673fdda499SJohn Harres 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
17683fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
17693fdda499SJohn Harres 					return (nv);
17703fdda499SJohn Harres 
1771573ca77eSGeorge Wilson 				/*
17723fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
1773573ca77eSGeorge Wilson 				 */
17743fdda499SJohn Harres 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
17753fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
17763fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
1777573ca77eSGeorge Wilson 					return (nv);
17783fdda499SJohn Harres 
1779573ca77eSGeorge Wilson 				break;
1780573ca77eSGeorge Wilson 			}
178188ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
178288ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
178388ecc943SGeorge Wilson 			uint64_t id, vdev_id;
178488ecc943SGeorge Wilson 
178588ecc943SGeorge Wilson 			/*
178688ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
178788ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
178888ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
178988ecc943SGeorge Wilson 			 */
179088ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
179188ecc943SGeorge Wilson 				return (NULL);
179288ecc943SGeorge Wilson 
179388ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
179488ecc943SGeorge Wilson 				free(type);
179588ecc943SGeorge Wilson 				break;
179688ecc943SGeorge Wilson 			}
179788ecc943SGeorge Wilson 			idx = p + 1;
179888ecc943SGeorge Wilson 			*p = '\0';
179988ecc943SGeorge Wilson 
180088ecc943SGeorge Wilson 			/*
180188ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
180288ecc943SGeorge Wilson 			 */
180388ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
180488ecc943SGeorge Wilson 				free(type);
180588ecc943SGeorge Wilson 				break;
180688ecc943SGeorge Wilson 			}
180788ecc943SGeorge Wilson 
180888ecc943SGeorge Wilson 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
180988ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
181088ecc943SGeorge Wilson 			    strncmp(type, VDEV_TYPE_MIRROR,
181188ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_MIRROR)) == 0);
181288ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
181388ecc943SGeorge Wilson 			    &id) == 0);
181488ecc943SGeorge Wilson 
181588ecc943SGeorge Wilson 			errno = 0;
181688ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
181788ecc943SGeorge Wilson 
181888ecc943SGeorge Wilson 			free(type);
181988ecc943SGeorge Wilson 			if (errno != 0)
182088ecc943SGeorge Wilson 				return (NULL);
182188ecc943SGeorge Wilson 
182288ecc943SGeorge Wilson 			/*
182388ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
182488ecc943SGeorge Wilson 			 */
182588ecc943SGeorge Wilson 			if (vdev_id == id)
182688ecc943SGeorge Wilson 				return (nv);
1827ea8dc4b6Seschrock 		}
1828573ca77eSGeorge Wilson 
1829573ca77eSGeorge Wilson 		/*
1830573ca77eSGeorge Wilson 		 * Common case
1831573ca77eSGeorge Wilson 		 */
1832573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
1833573ca77eSGeorge Wilson 			return (nv);
1834573ca77eSGeorge Wilson 		break;
1835573ca77eSGeorge Wilson 	}
1836573ca77eSGeorge Wilson 
1837573ca77eSGeorge Wilson 	default:
1838573ca77eSGeorge Wilson 		break;
1839ea8dc4b6Seschrock 	}
1840ea8dc4b6Seschrock 
1841ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1842ea8dc4b6Seschrock 	    &child, &children) != 0)
184399653d4eSeschrock 		return (NULL);
1844ea8dc4b6Seschrock 
1845ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
1846573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
1847ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
1848ee0eb9f2SEric Schrock 			/*
1849ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
1850ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
1851ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
1852ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
1853ee0eb9f2SEric Schrock 			 */
1854ee0eb9f2SEric Schrock 			if (log != NULL &&
1855ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
1856ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1857ee0eb9f2SEric Schrock 			    is_log) {
1858ee0eb9f2SEric Schrock 				*log = B_TRUE;
1859ee0eb9f2SEric Schrock 			}
1860ea8dc4b6Seschrock 			return (ret);
1861ee0eb9f2SEric Schrock 		}
1862ee0eb9f2SEric Schrock 	}
1863ea8dc4b6Seschrock 
186499653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
186599653d4eSeschrock 	    &child, &children) == 0) {
186699653d4eSeschrock 		for (c = 0; c < children; c++) {
1867573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
1868ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
1869a43d325bSek 				*avail_spare = B_TRUE;
187099653d4eSeschrock 				return (ret);
187199653d4eSeschrock 			}
187299653d4eSeschrock 		}
187399653d4eSeschrock 	}
187499653d4eSeschrock 
1875fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1876fa94a07fSbrendan 	    &child, &children) == 0) {
1877fa94a07fSbrendan 		for (c = 0; c < children; c++) {
1878573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
1879ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
1880fa94a07fSbrendan 				*l2cache = B_TRUE;
1881fa94a07fSbrendan 				return (ret);
1882fa94a07fSbrendan 			}
1883fa94a07fSbrendan 		}
1884fa94a07fSbrendan 	}
1885fa94a07fSbrendan 
188699653d4eSeschrock 	return (NULL);
1887ea8dc4b6Seschrock }
1888ea8dc4b6Seschrock 
1889573ca77eSGeorge Wilson /*
1890573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
1891573ca77eSGeorge Wilson  * associated vdev.
1892573ca77eSGeorge Wilson  */
1893573ca77eSGeorge Wilson nvlist_t *
1894573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1895573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1896573ca77eSGeorge Wilson {
1897573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
1898573ca77eSGeorge Wilson 
1899573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1900573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1901573ca77eSGeorge Wilson 
1902573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1903573ca77eSGeorge Wilson 	    &nvroot) == 0);
1904573ca77eSGeorge Wilson 
1905573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
1906cb04b873SMark J Musante 	*l2cache = B_FALSE;
1907daeb70e5SMark J Musante 	if (log != NULL)
1908daeb70e5SMark J Musante 		*log = B_FALSE;
1909573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1910573ca77eSGeorge Wilson 	nvlist_free(search);
1911573ca77eSGeorge Wilson 
1912573ca77eSGeorge Wilson 	return (ret);
1913573ca77eSGeorge Wilson }
1914573ca77eSGeorge Wilson 
191588ecc943SGeorge Wilson /*
191688ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
191788ecc943SGeorge Wilson  */
191888ecc943SGeorge Wilson boolean_t
191988ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
192088ecc943SGeorge Wilson {
192188ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
192288ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
192388ecc943SGeorge Wilson 		return (B_TRUE);
192488ecc943SGeorge Wilson 	return (B_FALSE);
192588ecc943SGeorge Wilson }
192688ecc943SGeorge Wilson 
192799653d4eSeschrock nvlist_t *
1928fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1929ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
1930ea8dc4b6Seschrock {
1931ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
1932ea8dc4b6Seschrock 	char *end;
1933573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
1934ea8dc4b6Seschrock 	uint64_t guid;
1935ea8dc4b6Seschrock 
1936573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1937573ca77eSGeorge Wilson 
19380917b783Seschrock 	guid = strtoull(path, &end, 10);
1939ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
1940573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
194188ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
194288ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1943ea8dc4b6Seschrock 	} else if (path[0] != '/') {
1944ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
1945573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1946ea8dc4b6Seschrock 	} else {
1947573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1948ea8dc4b6Seschrock 	}
1949ea8dc4b6Seschrock 
1950ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1951ea8dc4b6Seschrock 	    &nvroot) == 0);
1952ea8dc4b6Seschrock 
1953a43d325bSek 	*avail_spare = B_FALSE;
1954fa94a07fSbrendan 	*l2cache = B_FALSE;
1955ee0eb9f2SEric Schrock 	if (log != NULL)
1956ee0eb9f2SEric Schrock 		*log = B_FALSE;
1957573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1958573ca77eSGeorge Wilson 	nvlist_free(search);
1959573ca77eSGeorge Wilson 
1960573ca77eSGeorge Wilson 	return (ret);
1961a43d325bSek }
1962a43d325bSek 
196319397407SSherry Moore static int
196419397407SSherry Moore vdev_online(nvlist_t *nv)
196519397407SSherry Moore {
196619397407SSherry Moore 	uint64_t ival;
196719397407SSherry Moore 
196819397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
196919397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
197019397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
197119397407SSherry Moore 		return (0);
197219397407SSherry Moore 
197319397407SSherry Moore 	return (1);
197419397407SSherry Moore }
197519397407SSherry Moore 
197619397407SSherry Moore /*
197721ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
197819397407SSherry Moore  */
1979753a6d45SSherry Moore static int
198021ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1981753a6d45SSherry Moore     size_t *bytes_written)
198219397407SSherry Moore {
1983753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
1984753a6d45SSherry Moore 	char *tmppath;
1985753a6d45SSherry Moore 	const char *format;
1986753a6d45SSherry Moore 
1987753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1988753a6d45SSherry Moore 	    &tmppath) != 0)
1989753a6d45SSherry Moore 		return (EZFS_NODEVICE);
1990753a6d45SSherry Moore 
1991753a6d45SSherry Moore 	pos = *bytes_written;
1992753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
1993753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
1994753a6d45SSherry Moore 
1995753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1996753a6d45SSherry Moore 	*bytes_written += rsz;
1997753a6d45SSherry Moore 
1998753a6d45SSherry Moore 	if (rsz >= bytes_left) {
1999753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2000753a6d45SSherry Moore 		if (bytes_left != 0) {
2001753a6d45SSherry Moore 			physpath[pos] = 0;
2002753a6d45SSherry Moore 		}
2003753a6d45SSherry Moore 		return (EZFS_NOSPC);
2004753a6d45SSherry Moore 	}
2005753a6d45SSherry Moore 	return (0);
2006753a6d45SSherry Moore }
2007753a6d45SSherry Moore 
200821ecdf64SLin Ling static int
200921ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
201021ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
201121ecdf64SLin Ling {
201221ecdf64SLin Ling 	char *type;
201321ecdf64SLin Ling 	int ret;
201421ecdf64SLin Ling 
201521ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
201621ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
201721ecdf64SLin Ling 
201821ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
201921ecdf64SLin Ling 		/*
202021ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
202121ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
202221ecdf64SLin Ling 		 * spare device.
202321ecdf64SLin Ling 		 */
202421ecdf64SLin Ling 		if (is_spare) {
202521ecdf64SLin Ling 			uint64_t spare = 0;
202621ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
202721ecdf64SLin Ling 			    &spare);
202821ecdf64SLin Ling 			if (!spare)
202921ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
203021ecdf64SLin Ling 		}
203121ecdf64SLin Ling 
203221ecdf64SLin Ling 		if (vdev_online(nv)) {
203321ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
203421ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
203521ecdf64SLin Ling 				return (ret);
203621ecdf64SLin Ling 		}
203721ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
203821ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
203921ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
204021ecdf64SLin Ling 		nvlist_t **child;
204121ecdf64SLin Ling 		uint_t count;
204221ecdf64SLin Ling 		int i, ret;
204321ecdf64SLin Ling 
204421ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
204521ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
204621ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
204721ecdf64SLin Ling 
204821ecdf64SLin Ling 		for (i = 0; i < count; i++) {
204921ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
205021ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
205121ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
205221ecdf64SLin Ling 				return (ret);
205321ecdf64SLin Ling 		}
205421ecdf64SLin Ling 	}
205521ecdf64SLin Ling 
205621ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
205721ecdf64SLin Ling }
205821ecdf64SLin Ling 
2059753a6d45SSherry Moore /*
2060753a6d45SSherry Moore  * Get phys_path for a root pool config.
2061753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2062753a6d45SSherry Moore  */
2063753a6d45SSherry Moore static int
2064753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2065753a6d45SSherry Moore {
2066753a6d45SSherry Moore 	size_t rsz;
206719397407SSherry Moore 	nvlist_t *vdev_root;
206819397407SSherry Moore 	nvlist_t **child;
206919397407SSherry Moore 	uint_t count;
2070753a6d45SSherry Moore 	char *type;
2071753a6d45SSherry Moore 
2072753a6d45SSherry Moore 	rsz = 0;
2073753a6d45SSherry Moore 
2074753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2075753a6d45SSherry Moore 	    &vdev_root) != 0)
2076753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2077753a6d45SSherry Moore 
2078753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2079753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2080753a6d45SSherry Moore 	    &child, &count) != 0)
2081753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
208219397407SSherry Moore 
208319397407SSherry Moore 	/*
2084753a6d45SSherry Moore 	 * root pool can not have EFI labeled disks and can only have
2085753a6d45SSherry Moore 	 * a single top-level vdev.
208619397407SSherry Moore 	 */
2087753a6d45SSherry Moore 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2088753a6d45SSherry Moore 	    pool_uses_efi(vdev_root))
2089753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
209019397407SSherry Moore 
209121ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
209221ecdf64SLin Ling 	    B_FALSE);
209319397407SSherry Moore 
2094753a6d45SSherry Moore 	/* No online devices */
2095753a6d45SSherry Moore 	if (rsz == 0)
2096753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2097753a6d45SSherry Moore 
209819397407SSherry Moore 	return (0);
209919397407SSherry Moore }
210019397407SSherry Moore 
2101753a6d45SSherry Moore /*
2102753a6d45SSherry Moore  * Get phys_path for a root pool
2103753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2104753a6d45SSherry Moore  */
2105753a6d45SSherry Moore int
2106753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2107753a6d45SSherry Moore {
2108753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2109753a6d45SSherry Moore 	    phypath_size));
2110753a6d45SSherry Moore }
2111753a6d45SSherry Moore 
2112573ca77eSGeorge Wilson /*
2113573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2114573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2115573ca77eSGeorge Wilson  */
2116573ca77eSGeorge Wilson static int
2117573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2118573ca77eSGeorge Wilson {
2119573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2120573ca77eSGeorge Wilson 	char errbuf[1024];
2121573ca77eSGeorge Wilson 	int fd, error;
2122573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2123573ca77eSGeorge Wilson 
2124573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2125573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2126573ca77eSGeorge Wilson 		return (-1);
2127573ca77eSGeorge Wilson 
2128573ca77eSGeorge Wilson 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2129573ca77eSGeorge Wilson 
2130573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2131573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2132573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2133573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2134573ca77eSGeorge Wilson 	}
2135573ca77eSGeorge Wilson 
2136573ca77eSGeorge Wilson 	/*
2137573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2138573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2139573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2140573ca77eSGeorge Wilson 	 */
2141573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2142573ca77eSGeorge Wilson 	(void) close(fd);
2143573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2144573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2145573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2146573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2147573ca77eSGeorge Wilson 	}
2148573ca77eSGeorge Wilson 	return (0);
2149573ca77eSGeorge Wilson }
2150573ca77eSGeorge Wilson 
2151fa9e4066Sahrens /*
21523d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
21533d7072f8Seschrock  * ZFS_ONLINE_* flags.
2154fa9e4066Sahrens  */
2155fa9e4066Sahrens int
21563d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
21573d7072f8Seschrock     vdev_state_t *newstate)
2158fa9e4066Sahrens {
2159fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2160fa9e4066Sahrens 	char msg[1024];
216199653d4eSeschrock 	nvlist_t *tgt;
2162573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
216399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2164fa9e4066Sahrens 
2165573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2166573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2167573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2168573ca77eSGeorge Wilson 	} else {
2169573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2170573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2171573ca77eSGeorge Wilson 	}
2172ea8dc4b6Seschrock 
2173fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2174ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2175573ca77eSGeorge Wilson 	    &islog)) == NULL)
217699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2177fa9e4066Sahrens 
217899653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2179fa9e4066Sahrens 
2180069f55e2SEric Schrock 	if (avail_spare)
2181a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2182a43d325bSek 
2183573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND ||
2184573ca77eSGeorge Wilson 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2185573ca77eSGeorge Wilson 		char *pathname = NULL;
2186573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2187573ca77eSGeorge Wilson 
2188573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2189573ca77eSGeorge Wilson 		    &wholedisk);
2190573ca77eSGeorge Wilson 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2191573ca77eSGeorge Wilson 		    &pathname) == 0);
2192573ca77eSGeorge Wilson 
2193573ca77eSGeorge Wilson 		/*
2194573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2195573ca77eSGeorge Wilson 		 */
2196573ca77eSGeorge Wilson 		if (l2cache) {
2197573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2198573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2199573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2200573ca77eSGeorge Wilson 		}
2201573ca77eSGeorge Wilson 
2202573ca77eSGeorge Wilson 		if (wholedisk) {
2203573ca77eSGeorge Wilson 			pathname += strlen(DISK_ROOT) + 1;
2204cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2205573ca77eSGeorge Wilson 		}
2206573ca77eSGeorge Wilson 	}
2207573ca77eSGeorge Wilson 
22083d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
22093d7072f8Seschrock 	zc.zc_obj = flags;
2210fa9e4066Sahrens 
2211cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
22121195e687SMark J Musante 		if (errno == EINVAL) {
22131195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
22141195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
22151195e687SMark J Musante 			    "instead"), "zpool detach");
22161195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
22171195e687SMark J Musante 		}
22183d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
22191195e687SMark J Musante 	}
22203d7072f8Seschrock 
22213d7072f8Seschrock 	*newstate = zc.zc_cookie;
22223d7072f8Seschrock 	return (0);
2223fa9e4066Sahrens }
2224fa9e4066Sahrens 
2225fa9e4066Sahrens /*
2226fa9e4066Sahrens  * Take the specified vdev offline
2227fa9e4066Sahrens  */
2228fa9e4066Sahrens int
22293d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2230fa9e4066Sahrens {
2231fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2232fa9e4066Sahrens 	char msg[1024];
223399653d4eSeschrock 	nvlist_t *tgt;
2234fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
223599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2236fa9e4066Sahrens 
2237ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2238ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2239ea8dc4b6Seschrock 
2240fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2241ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2242ee0eb9f2SEric Schrock 	    NULL)) == NULL)
224399653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
224499653d4eSeschrock 
224599653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2246fa9e4066Sahrens 
2247069f55e2SEric Schrock 	if (avail_spare)
2248a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2249a43d325bSek 
22503d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
22513d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
22523d7072f8Seschrock 
2253cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
22543d7072f8Seschrock 		return (0);
22553d7072f8Seschrock 
22563d7072f8Seschrock 	switch (errno) {
22573d7072f8Seschrock 	case EBUSY:
22583d7072f8Seschrock 
22593d7072f8Seschrock 		/*
22603d7072f8Seschrock 		 * There are no other replicas of this device.
22613d7072f8Seschrock 		 */
22623d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
22633d7072f8Seschrock 
2264e6ca193dSGeorge Wilson 	case EEXIST:
2265e6ca193dSGeorge Wilson 		/*
2266e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2267e6ca193dSGeorge Wilson 		 */
2268e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2269e6ca193dSGeorge Wilson 
22703d7072f8Seschrock 	default:
22713d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
22723d7072f8Seschrock 	}
22733d7072f8Seschrock }
22743d7072f8Seschrock 
22753d7072f8Seschrock /*
22763d7072f8Seschrock  * Mark the given vdev faulted.
22773d7072f8Seschrock  */
22783d7072f8Seschrock int
2279069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
22803d7072f8Seschrock {
22813d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
22823d7072f8Seschrock 	char msg[1024];
22833d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
22843d7072f8Seschrock 
22853d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
22863d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2287441d80aaSlling 
22883d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
22893d7072f8Seschrock 	zc.zc_guid = guid;
22903d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2291069f55e2SEric Schrock 	zc.zc_obj = aux;
22923d7072f8Seschrock 
2293cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2294fa9e4066Sahrens 		return (0);
2295fa9e4066Sahrens 
2296fa9e4066Sahrens 	switch (errno) {
229799653d4eSeschrock 	case EBUSY:
2298fa9e4066Sahrens 
2299fa9e4066Sahrens 		/*
2300fa9e4066Sahrens 		 * There are no other replicas of this device.
2301fa9e4066Sahrens 		 */
230299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2303fa9e4066Sahrens 
230499653d4eSeschrock 	default:
230599653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2306fa9e4066Sahrens 	}
23073d7072f8Seschrock 
23083d7072f8Seschrock }
23093d7072f8Seschrock 
23103d7072f8Seschrock /*
23113d7072f8Seschrock  * Mark the given vdev degraded.
23123d7072f8Seschrock  */
23133d7072f8Seschrock int
2314069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
23153d7072f8Seschrock {
23163d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
23173d7072f8Seschrock 	char msg[1024];
23183d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
23193d7072f8Seschrock 
23203d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
23213d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
23223d7072f8Seschrock 
23233d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
23243d7072f8Seschrock 	zc.zc_guid = guid;
23253d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2326069f55e2SEric Schrock 	zc.zc_obj = aux;
23273d7072f8Seschrock 
2328cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
23293d7072f8Seschrock 		return (0);
23303d7072f8Seschrock 
23313d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
233299653d4eSeschrock }
233399653d4eSeschrock 
233499653d4eSeschrock /*
233599653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
233699653d4eSeschrock  * a hot spare.
233799653d4eSeschrock  */
233899653d4eSeschrock static boolean_t
233999653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
234099653d4eSeschrock {
234199653d4eSeschrock 	nvlist_t **child;
234299653d4eSeschrock 	uint_t c, children;
234399653d4eSeschrock 	char *type;
234499653d4eSeschrock 
234599653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
234699653d4eSeschrock 	    &children) == 0) {
234799653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
234899653d4eSeschrock 		    &type) == 0);
234999653d4eSeschrock 
235099653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
235199653d4eSeschrock 		    children == 2 && child[which] == tgt)
235299653d4eSeschrock 			return (B_TRUE);
235399653d4eSeschrock 
235499653d4eSeschrock 		for (c = 0; c < children; c++)
235599653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
235699653d4eSeschrock 				return (B_TRUE);
235799653d4eSeschrock 	}
235899653d4eSeschrock 
235999653d4eSeschrock 	return (B_FALSE);
2360fa9e4066Sahrens }
2361fa9e4066Sahrens 
2362fa9e4066Sahrens /*
2363fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
23648654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2365fa9e4066Sahrens  */
2366fa9e4066Sahrens int
2367fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2368fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2369fa9e4066Sahrens {
2370fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2371fa9e4066Sahrens 	char msg[1024];
2372fa9e4066Sahrens 	int ret;
237399653d4eSeschrock 	nvlist_t *tgt;
2374ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2375ee0eb9f2SEric Schrock 	uint64_t val;
2376cb04b873SMark J Musante 	char *newname;
237799653d4eSeschrock 	nvlist_t **child;
237899653d4eSeschrock 	uint_t children;
237999653d4eSeschrock 	nvlist_t *config_root;
238099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2381b5b76fecSGeorge Wilson 	boolean_t rootpool = pool_is_bootable(zhp);
2382fa9e4066Sahrens 
2383ea8dc4b6Seschrock 	if (replacing)
2384ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2385ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2386ea8dc4b6Seschrock 	else
2387ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2388ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2389ea8dc4b6Seschrock 
2390b5b76fecSGeorge Wilson 	/*
2391b5b76fecSGeorge Wilson 	 * If this is a root pool, make sure that we're not attaching an
2392b5b76fecSGeorge Wilson 	 * EFI labeled device.
2393b5b76fecSGeorge Wilson 	 */
2394b5b76fecSGeorge Wilson 	if (rootpool && pool_uses_efi(nvroot)) {
2395b5b76fecSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2396b5b76fecSGeorge Wilson 		    "EFI labeled devices are not supported on root pools."));
2397b5b76fecSGeorge Wilson 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2398b5b76fecSGeorge Wilson 	}
2399b5b76fecSGeorge Wilson 
2400fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2401ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2402ee0eb9f2SEric Schrock 	    &islog)) == 0)
240399653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
240499653d4eSeschrock 
2405a43d325bSek 	if (avail_spare)
240699653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
240799653d4eSeschrock 
2408fa94a07fSbrendan 	if (l2cache)
2409fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2410fa94a07fSbrendan 
241199653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2412fa9e4066Sahrens 	zc.zc_cookie = replacing;
2413fa9e4066Sahrens 
241499653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
241599653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
241699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
241799653d4eSeschrock 		    "new device must be a single disk"));
241899653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
241999653d4eSeschrock 	}
242099653d4eSeschrock 
242199653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
242299653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
242399653d4eSeschrock 
242488ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
24250430f8daSeschrock 		return (-1);
24260430f8daSeschrock 
242799653d4eSeschrock 	/*
242899653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
242999653d4eSeschrock 	 * replace it with another hot spare.
243099653d4eSeschrock 	 */
243199653d4eSeschrock 	if (replacing &&
243299653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2433ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2434ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2435ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
243699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
243799653d4eSeschrock 		    "can only be replaced by another hot spare"));
24380430f8daSeschrock 		free(newname);
243999653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
244099653d4eSeschrock 	}
244199653d4eSeschrock 
24420430f8daSeschrock 	free(newname);
24430430f8daSeschrock 
2444990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
244599653d4eSeschrock 		return (-1);
2446fa9e4066Sahrens 
2447cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2448fa9e4066Sahrens 
2449e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2450fa9e4066Sahrens 
2451b5b76fecSGeorge Wilson 	if (ret == 0) {
2452b5b76fecSGeorge Wilson 		if (rootpool) {
245321ecdf64SLin Ling 			/*
245421ecdf64SLin Ling 			 * XXX need a better way to prevent user from
245521ecdf64SLin Ling 			 * booting up a half-baked vdev.
245621ecdf64SLin Ling 			 */
245721ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
245821ecdf64SLin Ling 			    "sure to wait until resilver is done "
245921ecdf64SLin Ling 			    "before rebooting.\n"));
2460b5b76fecSGeorge Wilson 		}
2461fa9e4066Sahrens 		return (0);
2462b5b76fecSGeorge Wilson 	}
2463fa9e4066Sahrens 
2464fa9e4066Sahrens 	switch (errno) {
2465ea8dc4b6Seschrock 	case ENOTSUP:
2466fa9e4066Sahrens 		/*
2467fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2468fa9e4066Sahrens 		 */
24698654d025Sperrin 		if (replacing) {
2470cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2471cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2472cb04b873SMark J Musante 
2473ee0eb9f2SEric Schrock 			if (islog)
24748654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24758654d025Sperrin 				    "cannot replace a log with a spare"));
2476cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2477cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2478cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2479cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
24808654d025Sperrin 			else
24818654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24828654d025Sperrin 				    "cannot replace a replacing device"));
24838654d025Sperrin 		} else {
248499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
248599653d4eSeschrock 			    "can only attach to mirrors and top-level "
248699653d4eSeschrock 			    "disks"));
24878654d025Sperrin 		}
248899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2489fa9e4066Sahrens 		break;
2490fa9e4066Sahrens 
2491ea8dc4b6Seschrock 	case EINVAL:
2492fa9e4066Sahrens 		/*
2493fa9e4066Sahrens 		 * The new device must be a single disk.
2494fa9e4066Sahrens 		 */
249599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
249699653d4eSeschrock 		    "new device must be a single disk"));
249799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2498fa9e4066Sahrens 		break;
2499fa9e4066Sahrens 
2500ea8dc4b6Seschrock 	case EBUSY:
250199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
250299653d4eSeschrock 		    new_disk);
250399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2504fa9e4066Sahrens 		break;
2505fa9e4066Sahrens 
2506ea8dc4b6Seschrock 	case EOVERFLOW:
2507fa9e4066Sahrens 		/*
2508fa9e4066Sahrens 		 * The new device is too small.
2509fa9e4066Sahrens 		 */
251099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
251199653d4eSeschrock 		    "device is too small"));
251299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2513fa9e4066Sahrens 		break;
2514fa9e4066Sahrens 
2515ea8dc4b6Seschrock 	case EDOM:
2516fa9e4066Sahrens 		/*
2517fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2518fa9e4066Sahrens 		 */
251999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
252099653d4eSeschrock 		    "devices have different sector alignment"));
252199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2522fa9e4066Sahrens 		break;
2523fa9e4066Sahrens 
2524ea8dc4b6Seschrock 	case ENAMETOOLONG:
2525fa9e4066Sahrens 		/*
2526fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2527fa9e4066Sahrens 		 */
252899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2529fa9e4066Sahrens 		break;
2530fa9e4066Sahrens 
2531ea8dc4b6Seschrock 	default:
253299653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2533fa9e4066Sahrens 	}
2534fa9e4066Sahrens 
253599653d4eSeschrock 	return (-1);
2536fa9e4066Sahrens }
2537fa9e4066Sahrens 
2538fa9e4066Sahrens /*
2539fa9e4066Sahrens  * Detach the specified device.
2540fa9e4066Sahrens  */
2541fa9e4066Sahrens int
2542fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2543fa9e4066Sahrens {
2544fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2545fa9e4066Sahrens 	char msg[1024];
254699653d4eSeschrock 	nvlist_t *tgt;
2547fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
254899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2549fa9e4066Sahrens 
2550ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2551ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2552ea8dc4b6Seschrock 
2553fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2554ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2555ee0eb9f2SEric Schrock 	    NULL)) == 0)
255699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2557fa9e4066Sahrens 
2558a43d325bSek 	if (avail_spare)
255999653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
256099653d4eSeschrock 
2561fa94a07fSbrendan 	if (l2cache)
2562fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2563fa94a07fSbrendan 
256499653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
256599653d4eSeschrock 
2566ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2567fa9e4066Sahrens 		return (0);
2568fa9e4066Sahrens 
2569fa9e4066Sahrens 	switch (errno) {
2570fa9e4066Sahrens 
2571ea8dc4b6Seschrock 	case ENOTSUP:
2572fa9e4066Sahrens 		/*
2573fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2574fa9e4066Sahrens 		 */
257599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
257699653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2577cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2578fa9e4066Sahrens 		break;
2579fa9e4066Sahrens 
2580ea8dc4b6Seschrock 	case EBUSY:
2581fa9e4066Sahrens 		/*
2582fa9e4066Sahrens 		 * There are no other replicas of this device.
2583fa9e4066Sahrens 		 */
258499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2585fa9e4066Sahrens 		break;
2586fa9e4066Sahrens 
2587ea8dc4b6Seschrock 	default:
258899653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2589ea8dc4b6Seschrock 	}
2590ea8dc4b6Seschrock 
259199653d4eSeschrock 	return (-1);
259299653d4eSeschrock }
259399653d4eSeschrock 
25941195e687SMark J Musante /*
25951195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
25961195e687SMark J Musante  *
25971195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
25981195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
25991195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
26001195e687SMark J Musante  * see if any entry in the schild array matches.
26011195e687SMark J Musante  *
26021195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
26031195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
26041195e687SMark J Musante  */
26051195e687SMark J Musante static int
26061195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
26071195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
26081195e687SMark J Musante {
26091195e687SMark J Musante 	uint_t mc;
26101195e687SMark J Musante 
26111195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
26121195e687SMark J Musante 		uint_t sc;
26131195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
26141195e687SMark J Musante 		    mchild[mc], B_FALSE);
26151195e687SMark J Musante 
26161195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
26171195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
26181195e687SMark J Musante 			    schild[sc], B_FALSE);
26191195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
26201195e687SMark J Musante 
26211195e687SMark J Musante 			free(spath);
26221195e687SMark J Musante 			if (result) {
26231195e687SMark J Musante 				free(mpath);
26241195e687SMark J Musante 				return (mc);
26251195e687SMark J Musante 			}
26261195e687SMark J Musante 		}
26271195e687SMark J Musante 
26281195e687SMark J Musante 		free(mpath);
26291195e687SMark J Musante 	}
26301195e687SMark J Musante 
26311195e687SMark J Musante 	return (-1);
26321195e687SMark J Musante }
26331195e687SMark J Musante 
26341195e687SMark J Musante /*
26351195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
26361195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
26371195e687SMark J Musante  */
26381195e687SMark J Musante int
26391195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
26401195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
26411195e687SMark J Musante {
26421195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
26431195e687SMark J Musante 	char msg[1024];
26441195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
26451195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
26461195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
26471195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26481195e687SMark J Musante 	uint64_t vers;
26491195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
26501195e687SMark J Musante 	int retval = 0;
26511195e687SMark J Musante 
26521195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
26531195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
26541195e687SMark J Musante 
26551195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
26561195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
26571195e687SMark J Musante 
26581195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
26591195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
26601195e687SMark J Musante 		    "retrieve pool configuration\n"));
26611195e687SMark J Musante 		return (-1);
26621195e687SMark J Musante 	}
26631195e687SMark J Musante 
26641195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
26651195e687SMark J Musante 	    == 0);
26661195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
26671195e687SMark J Musante 
26681195e687SMark J Musante 	if (props) {
2669f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
26701195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2671f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
26721195e687SMark J Musante 			return (-1);
26731195e687SMark J Musante 	}
26741195e687SMark J Musante 
26751195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
26761195e687SMark J Musante 	    &children) != 0) {
26771195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26781195e687SMark J Musante 		    "Source pool is missing vdev tree"));
26791195e687SMark J Musante 		if (zc_props)
26801195e687SMark J Musante 			nvlist_free(zc_props);
26811195e687SMark J Musante 		return (-1);
26821195e687SMark J Musante 	}
26831195e687SMark J Musante 
26841195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
26851195e687SMark J Musante 	vcount = 0;
26861195e687SMark J Musante 
26871195e687SMark J Musante 	if (*newroot == NULL ||
26881195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
26891195e687SMark J Musante 	    &newchild, &newchildren) != 0)
26901195e687SMark J Musante 		newchildren = 0;
26911195e687SMark J Musante 
26921195e687SMark J Musante 	for (c = 0; c < children; c++) {
26931195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
26941195e687SMark J Musante 		char *type;
26951195e687SMark J Musante 		nvlist_t **mchild, *vdev;
26961195e687SMark J Musante 		uint_t mchildren;
26971195e687SMark J Musante 		int entry;
26981195e687SMark J Musante 
26991195e687SMark J Musante 		/*
27001195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
27011195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
27021195e687SMark J Musante 		 */
27031195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
27041195e687SMark J Musante 		    &is_log);
27051195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
27061195e687SMark J Musante 		    &is_hole);
27071195e687SMark J Musante 		if (is_log || is_hole) {
27081195e687SMark J Musante 			/*
27091195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
27101195e687SMark J Musante 			 */
27111195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
27121195e687SMark J Musante 				goto out;
27131195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
27141195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
27151195e687SMark J Musante 				goto out;
27161195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
27171195e687SMark J Musante 			    1) != 0)
27181195e687SMark J Musante 				goto out;
27191195e687SMark J Musante 			if (lastlog == 0)
27201195e687SMark J Musante 				lastlog = vcount;
27211195e687SMark J Musante 			varray[vcount++] = vdev;
27221195e687SMark J Musante 			continue;
27231195e687SMark J Musante 		}
27241195e687SMark J Musante 		lastlog = 0;
27251195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
27261195e687SMark J Musante 		    == 0);
27271195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
27281195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27291195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
27301195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
27311195e687SMark J Musante 			goto out;
27321195e687SMark J Musante 		}
27331195e687SMark J Musante 
27341195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
27351195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
27361195e687SMark J Musante 
27371195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
27381195e687SMark J Musante 		if (newchildren > 0 &&
27391195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
27401195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
27411195e687SMark J Musante 			/* We found a disk that the user specified. */
27421195e687SMark J Musante 			vdev = mchild[entry];
27431195e687SMark J Musante 			++found;
27441195e687SMark J Musante 		} else {
27451195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
27461195e687SMark J Musante 			vdev = mchild[mchildren - 1];
27471195e687SMark J Musante 		}
27481195e687SMark J Musante 
27491195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
27501195e687SMark J Musante 			goto out;
27511195e687SMark J Musante 	}
27521195e687SMark J Musante 
27531195e687SMark J Musante 	/* did we find every disk the user specified? */
27541195e687SMark J Musante 	if (found != newchildren) {
27551195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
27561195e687SMark J Musante 		    "include at most one disk from each mirror"));
27571195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
27581195e687SMark J Musante 		goto out;
27591195e687SMark J Musante 	}
27601195e687SMark J Musante 
27611195e687SMark J Musante 	/* Prepare the nvlist for populating. */
27621195e687SMark J Musante 	if (*newroot == NULL) {
27631195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
27641195e687SMark J Musante 			goto out;
27651195e687SMark J Musante 		freelist = B_TRUE;
27661195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
27671195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
27681195e687SMark J Musante 			goto out;
27691195e687SMark J Musante 	} else {
27701195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
27711195e687SMark J Musante 	}
27721195e687SMark J Musante 
27731195e687SMark J Musante 	/* Add all the children we found */
27741195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
27751195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
27761195e687SMark J Musante 		goto out;
27771195e687SMark J Musante 
27781195e687SMark J Musante 	/*
27791195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
27801195e687SMark J Musante 	 */
27811195e687SMark J Musante 	if (flags.dryrun) {
27821195e687SMark J Musante 		memory_err = B_FALSE;
27831195e687SMark J Musante 		freelist = B_FALSE;
27841195e687SMark J Musante 		goto out;
27851195e687SMark J Musante 	}
27861195e687SMark J Musante 
27871195e687SMark J Musante 	/* now build up the config list & call the ioctl */
27881195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
27891195e687SMark J Musante 		goto out;
27901195e687SMark J Musante 
27911195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
27921195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
27931195e687SMark J Musante 	    nvlist_add_string(newconfig,
27941195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
27951195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
27961195e687SMark J Musante 		goto out;
27971195e687SMark J Musante 
27981195e687SMark J Musante 	/*
27991195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
28001195e687SMark J Musante 	 * explicitly export it.
28011195e687SMark J Musante 	 */
28021195e687SMark J Musante 	if (!flags.import)
28031195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
28041195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
28051195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
28061195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
28071195e687SMark J Musante 		goto out;
28081195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
28091195e687SMark J Musante 		goto out;
28101195e687SMark J Musante 
28111195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
28121195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
28131195e687SMark J Musante 		goto out;
28141195e687SMark J Musante 	}
28151195e687SMark J Musante 
28161195e687SMark J Musante 	freelist = B_FALSE;
28171195e687SMark J Musante 	memory_err = B_FALSE;
28181195e687SMark J Musante 
28191195e687SMark J Musante out:
28201195e687SMark J Musante 	if (varray != NULL) {
28211195e687SMark J Musante 		int v;
28221195e687SMark J Musante 
28231195e687SMark J Musante 		for (v = 0; v < vcount; v++)
28241195e687SMark J Musante 			nvlist_free(varray[v]);
28251195e687SMark J Musante 		free(varray);
28261195e687SMark J Musante 	}
28271195e687SMark J Musante 	zcmd_free_nvlists(&zc);
28281195e687SMark J Musante 	if (zc_props)
28291195e687SMark J Musante 		nvlist_free(zc_props);
28301195e687SMark J Musante 	if (newconfig)
28311195e687SMark J Musante 		nvlist_free(newconfig);
28321195e687SMark J Musante 	if (freelist) {
28331195e687SMark J Musante 		nvlist_free(*newroot);
28341195e687SMark J Musante 		*newroot = NULL;
28351195e687SMark J Musante 	}
28361195e687SMark J Musante 
28371195e687SMark J Musante 	if (retval != 0)
28381195e687SMark J Musante 		return (retval);
28391195e687SMark J Musante 
28401195e687SMark J Musante 	if (memory_err)
28411195e687SMark J Musante 		return (no_memory(hdl));
28421195e687SMark J Musante 
28431195e687SMark J Musante 	return (0);
28441195e687SMark J Musante }
28451195e687SMark J Musante 
284699653d4eSeschrock /*
2847fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
2848fa94a07fSbrendan  * and level 2 cache devices.
284999653d4eSeschrock  */
285099653d4eSeschrock int
285199653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
285299653d4eSeschrock {
285399653d4eSeschrock 	zfs_cmd_t zc = { 0 };
285499653d4eSeschrock 	char msg[1024];
285599653d4eSeschrock 	nvlist_t *tgt;
285688ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
285799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
285888ecc943SGeorge Wilson 	uint64_t version;
285999653d4eSeschrock 
286099653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
286199653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
286299653d4eSeschrock 
286399653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2864ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
286588ecc943SGeorge Wilson 	    &islog)) == 0)
286699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
286788ecc943SGeorge Wilson 	/*
286888ecc943SGeorge Wilson 	 * XXX - this should just go away.
286988ecc943SGeorge Wilson 	 */
287088ecc943SGeorge Wilson 	if (!avail_spare && !l2cache && !islog) {
287199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
287288ecc943SGeorge Wilson 		    "only inactive hot spares, cache, top-level, "
287388ecc943SGeorge Wilson 		    "or log devices can be removed"));
287499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
287599653d4eSeschrock 	}
287699653d4eSeschrock 
287788ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
287888ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
287988ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
288088ecc943SGeorge Wilson 		    "pool must be upgrade to support log removal"));
288188ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
288288ecc943SGeorge Wilson 	}
288388ecc943SGeorge Wilson 
288499653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
288599653d4eSeschrock 
2886ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
288799653d4eSeschrock 		return (0);
288899653d4eSeschrock 
288999653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
2890ea8dc4b6Seschrock }
2891ea8dc4b6Seschrock 
2892ea8dc4b6Seschrock /*
2893ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
2894ea8dc4b6Seschrock  */
2895ea8dc4b6Seschrock int
2896468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2897ea8dc4b6Seschrock {
2898ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
2899ea8dc4b6Seschrock 	char msg[1024];
290099653d4eSeschrock 	nvlist_t *tgt;
2901468c413aSTim Haley 	zpool_rewind_policy_t policy;
2902fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
290399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2904468c413aSTim Haley 	nvlist_t *nvi = NULL;
29054b964adaSGeorge Wilson 	int error;
2906ea8dc4b6Seschrock 
2907ea8dc4b6Seschrock 	if (path)
2908ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
2909ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2910e9dbad6fSeschrock 		    path);
2911ea8dc4b6Seschrock 	else
2912ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
2913ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2914ea8dc4b6Seschrock 		    zhp->zpool_name);
2915ea8dc4b6Seschrock 
2916ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
291799653d4eSeschrock 	if (path) {
2918fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2919ee0eb9f2SEric Schrock 		    &l2cache, NULL)) == 0)
292099653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
2921ea8dc4b6Seschrock 
2922fa94a07fSbrendan 		/*
2923fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
2924fa94a07fSbrendan 		 * error clearing for l2cache devices.
2925fa94a07fSbrendan 		 */
2926a43d325bSek 		if (avail_spare)
292799653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
2928ea8dc4b6Seschrock 
292999653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
293099653d4eSeschrock 		    &zc.zc_guid) == 0);
2931fa9e4066Sahrens 	}
2932fa9e4066Sahrens 
2933468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
2934468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
2935468c413aSTim Haley 
293657f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2937468c413aSTim Haley 		return (-1);
2938468c413aSTim Haley 
2939cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2940468c413aSTim Haley 		return (-1);
2941468c413aSTim Haley 
29424b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
29434b964adaSGeorge Wilson 	    errno == ENOMEM) {
29444b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
29454b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
29464b964adaSGeorge Wilson 			return (-1);
29474b964adaSGeorge Wilson 		}
29484b964adaSGeorge Wilson 	}
29494b964adaSGeorge Wilson 
29504b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2951468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
2952468c413aSTim Haley 		if (policy.zrp_request &
2953468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2954468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2955468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
2956468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2957468c413aSTim Haley 			    nvi);
2958468c413aSTim Haley 			nvlist_free(nvi);
2959468c413aSTim Haley 		}
2960468c413aSTim Haley 		zcmd_free_nvlists(&zc);
296199653d4eSeschrock 		return (0);
2962468c413aSTim Haley 	}
296399653d4eSeschrock 
2964468c413aSTim Haley 	zcmd_free_nvlists(&zc);
296599653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
2966fa9e4066Sahrens }
2967fa9e4066Sahrens 
29683d7072f8Seschrock /*
29693d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
29703d7072f8Seschrock  */
29713d7072f8Seschrock int
29723d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
29733d7072f8Seschrock {
29743d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
29753d7072f8Seschrock 	char msg[1024];
29763d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
29773d7072f8Seschrock 
29783d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
29793d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
29803d7072f8Seschrock 	    guid);
29813d7072f8Seschrock 
29823d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
29833d7072f8Seschrock 	zc.zc_guid = guid;
298414f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
29853d7072f8Seschrock 
29863d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
29873d7072f8Seschrock 		return (0);
29883d7072f8Seschrock 
29893d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
29903d7072f8Seschrock }
29913d7072f8Seschrock 
2992e9103aaeSGarrett D'Amore /*
2993e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
2994e9103aaeSGarrett D'Amore  */
2995e9103aaeSGarrett D'Amore int
2996e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
2997e9103aaeSGarrett D'Amore {
2998e9103aaeSGarrett D'Amore 	char msg[1024];
2999e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3000e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3001e9103aaeSGarrett D'Amore 
3002e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3003e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3004e9103aaeSGarrett D'Amore 
3005e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3006e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3007e9103aaeSGarrett D'Amore 		return (0);
3008e9103aaeSGarrett D'Amore 
3009e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3010e9103aaeSGarrett D'Amore }
3011e9103aaeSGarrett D'Amore 
3012c67d9675Seschrock /*
3013c67d9675Seschrock  * Convert from a devid string to a path.
3014c67d9675Seschrock  */
3015c67d9675Seschrock static char *
3016c67d9675Seschrock devid_to_path(char *devid_str)
3017c67d9675Seschrock {
3018c67d9675Seschrock 	ddi_devid_t devid;
3019c67d9675Seschrock 	char *minor;
3020c67d9675Seschrock 	char *path;
3021c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3022c67d9675Seschrock 	int ret;
3023c67d9675Seschrock 
3024c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3025c67d9675Seschrock 		return (NULL);
3026c67d9675Seschrock 
3027c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3028c67d9675Seschrock 
3029c67d9675Seschrock 	devid_str_free(minor);
3030c67d9675Seschrock 	devid_free(devid);
3031c67d9675Seschrock 
3032c67d9675Seschrock 	if (ret != 0)
3033c67d9675Seschrock 		return (NULL);
3034c67d9675Seschrock 
303599653d4eSeschrock 	if ((path = strdup(list[0].devname)) == NULL)
303699653d4eSeschrock 		return (NULL);
303799653d4eSeschrock 
3038c67d9675Seschrock 	devid_free_nmlist(list);
3039c67d9675Seschrock 
3040c67d9675Seschrock 	return (path);
3041c67d9675Seschrock }
3042c67d9675Seschrock 
3043c67d9675Seschrock /*
3044c67d9675Seschrock  * Convert from a path to a devid string.
3045c67d9675Seschrock  */
3046c67d9675Seschrock static char *
3047c67d9675Seschrock path_to_devid(const char *path)
3048c67d9675Seschrock {
3049c67d9675Seschrock 	int fd;
3050c67d9675Seschrock 	ddi_devid_t devid;
3051c67d9675Seschrock 	char *minor, *ret;
3052c67d9675Seschrock 
3053c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3054c67d9675Seschrock 		return (NULL);
3055c67d9675Seschrock 
3056c67d9675Seschrock 	minor = NULL;
3057c67d9675Seschrock 	ret = NULL;
3058c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3059c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3060c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3061c67d9675Seschrock 		if (minor != NULL)
3062c67d9675Seschrock 			devid_str_free(minor);
3063c67d9675Seschrock 		devid_free(devid);
3064c67d9675Seschrock 	}
3065c67d9675Seschrock 	(void) close(fd);
3066c67d9675Seschrock 
3067c67d9675Seschrock 	return (ret);
3068c67d9675Seschrock }
3069c67d9675Seschrock 
3070c67d9675Seschrock /*
3071c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3072c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3073c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3074c67d9675Seschrock  */
3075c67d9675Seschrock static void
3076c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3077c67d9675Seschrock {
3078c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3079c67d9675Seschrock 
3080c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3081e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3082c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3083ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3084c67d9675Seschrock 
308599653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3086c67d9675Seschrock }
3087c67d9675Seschrock 
3088c67d9675Seschrock /*
3089c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3090c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3091c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3092c67d9675Seschrock  * trailing 's0' slice name.
3093c67d9675Seschrock  *
3094c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3095c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3096c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3097c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3098c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3099c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3100c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3101c67d9675Seschrock  * of these checks.
3102c67d9675Seschrock  */
3103c67d9675Seschrock char *
310488ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
310588ecc943SGeorge Wilson     boolean_t verbose)
3106c67d9675Seschrock {
3107c67d9675Seschrock 	char *path, *devid;
3108ea8dc4b6Seschrock 	uint64_t value;
3109ea8dc4b6Seschrock 	char buf[64];
31103d7072f8Seschrock 	vdev_stat_t *vs;
31113d7072f8Seschrock 	uint_t vsc;
3112c67d9675Seschrock 
3113ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3114ea8dc4b6Seschrock 	    &value) == 0) {
3115ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3116ea8dc4b6Seschrock 		    &value) == 0);
31175ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
31185ad82045Snd 		    (u_longlong_t)value);
3119ea8dc4b6Seschrock 		path = buf;
3120ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3121c67d9675Seschrock 
31223d7072f8Seschrock 		/*
31233d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
31243d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
31253d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
31263d7072f8Seschrock 		 * effects.
31273d7072f8Seschrock 		 */
31283f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
31293d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
31303d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
31313d7072f8Seschrock 		    zhp != NULL &&
3132c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3133c67d9675Seschrock 			/*
3134c67d9675Seschrock 			 * Determine if the current path is correct.
3135c67d9675Seschrock 			 */
3136c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3137c67d9675Seschrock 
3138c67d9675Seschrock 			if (newdevid == NULL ||
3139c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3140c67d9675Seschrock 				char *newpath;
3141c67d9675Seschrock 
3142c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3143c67d9675Seschrock 					/*
3144c67d9675Seschrock 					 * Update the path appropriately.
3145c67d9675Seschrock 					 */
3146c67d9675Seschrock 					set_path(zhp, nv, newpath);
314799653d4eSeschrock 					if (nvlist_add_string(nv,
314899653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
314999653d4eSeschrock 						verify(nvlist_lookup_string(nv,
315099653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
315199653d4eSeschrock 						    &path) == 0);
3152c67d9675Seschrock 					free(newpath);
3153c67d9675Seschrock 				}
3154c67d9675Seschrock 			}
3155c67d9675Seschrock 
315699653d4eSeschrock 			if (newdevid)
315799653d4eSeschrock 				devid_str_free(newdevid);
3158c67d9675Seschrock 		}
3159c67d9675Seschrock 
3160c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3161c67d9675Seschrock 			path += 9;
3162c67d9675Seschrock 
3163c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3164ea8dc4b6Seschrock 		    &value) == 0 && value) {
31653fdda499SJohn Harres 			int pathlen = strlen(path);
316699653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
31673fdda499SJohn Harres 
31683fdda499SJohn Harres 			/*
31693fdda499SJohn Harres 			 * If it starts with c#, and ends with "s0", chop
31703fdda499SJohn Harres 			 * the "s0" off, or if it ends with "s0/old", remove
31713fdda499SJohn Harres 			 * the "s0" from the middle.
31723fdda499SJohn Harres 			 */
31733fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
31743fdda499SJohn Harres 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
31753fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
31763fdda499SJohn Harres 				} else if (pathlen > 6 &&
31773fdda499SJohn Harres 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
31783fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
31793fdda499SJohn Harres 					    "/old");
31803fdda499SJohn Harres 				}
31813fdda499SJohn Harres 			}
3182c67d9675Seschrock 			return (tmp);
3183c67d9675Seschrock 		}
3184c67d9675Seschrock 	} else {
3185c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
318699653d4eSeschrock 
318799653d4eSeschrock 		/*
318899653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
318999653d4eSeschrock 		 */
319099653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
319199653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
319299653d4eSeschrock 			    &value) == 0);
319399653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
31945ad82045Snd 			    (u_longlong_t)value);
319599653d4eSeschrock 			path = buf;
319699653d4eSeschrock 		}
319788ecc943SGeorge Wilson 
319888ecc943SGeorge Wilson 		/*
319988ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
320088ecc943SGeorge Wilson 		 * naming convention.
320188ecc943SGeorge Wilson 		 */
320288ecc943SGeorge Wilson 		if (verbose) {
320388ecc943SGeorge Wilson 			uint64_t id;
320488ecc943SGeorge Wilson 
320588ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
320688ecc943SGeorge Wilson 			    &id) == 0);
320788ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
320888ecc943SGeorge Wilson 			    (u_longlong_t)id);
320988ecc943SGeorge Wilson 			path = buf;
321088ecc943SGeorge Wilson 		}
3211c67d9675Seschrock 	}
3212c67d9675Seschrock 
321399653d4eSeschrock 	return (zfs_strdup(hdl, path));
3214c67d9675Seschrock }
3215ea8dc4b6Seschrock 
3216ea8dc4b6Seschrock static int
3217ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b)
3218ea8dc4b6Seschrock {
3219ea8dc4b6Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
3220ea8dc4b6Seschrock }
3221ea8dc4b6Seschrock 
3222ea8dc4b6Seschrock /*
3223ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3224ea8dc4b6Seschrock  * caller.
3225ea8dc4b6Seschrock  */
3226ea8dc4b6Seschrock int
322755434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3228ea8dc4b6Seschrock {
3229ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3230ea8dc4b6Seschrock 	uint64_t count;
3231e9dbad6fSeschrock 	zbookmark_t *zb = NULL;
323255434c77Sek 	int i;
3233ea8dc4b6Seschrock 
3234ea8dc4b6Seschrock 	/*
3235ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3236ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3237ea8dc4b6Seschrock 	 * entire list.
3238ea8dc4b6Seschrock 	 */
3239ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3240ea8dc4b6Seschrock 	    &count) == 0);
324175519f38Sek 	if (count == 0)
324275519f38Sek 		return (0);
3243e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
32445ad82045Snd 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
324599653d4eSeschrock 		return (-1);
3246e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3247ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3248ea8dc4b6Seschrock 	for (;;) {
324999653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
325099653d4eSeschrock 		    &zc) != 0) {
3251e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3252ea8dc4b6Seschrock 			if (errno == ENOMEM) {
3253bf561db0Svb 				count = zc.zc_nvlist_dst_size;
3254e9dbad6fSeschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
3255bf561db0Svb 				    zfs_alloc(zhp->zpool_hdl, count *
3256bf561db0Svb 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
325799653d4eSeschrock 					return (-1);
3258ea8dc4b6Seschrock 			} else {
3259ea8dc4b6Seschrock 				return (-1);
3260ea8dc4b6Seschrock 			}
3261ea8dc4b6Seschrock 		} else {
3262ea8dc4b6Seschrock 			break;
3263ea8dc4b6Seschrock 		}
3264ea8dc4b6Seschrock 	}
3265ea8dc4b6Seschrock 
3266ea8dc4b6Seschrock 	/*
3267ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3268ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3269e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3270ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3271ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3272ea8dc4b6Seschrock 	 */
3273e9dbad6fSeschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3274e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3275e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3276ea8dc4b6Seschrock 
3277ea8dc4b6Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3278ea8dc4b6Seschrock 
327955434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3280ea8dc4b6Seschrock 
3281ea8dc4b6Seschrock 	/*
328255434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3283ea8dc4b6Seschrock 	 */
3284ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3285ea8dc4b6Seschrock 		nvlist_t *nv;
3286ea8dc4b6Seschrock 
3287c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
3288c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3289c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
3290ea8dc4b6Seschrock 			continue;
3291ea8dc4b6Seschrock 
329255434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
329355434c77Sek 			goto nomem;
329455434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
329555434c77Sek 		    zb[i].zb_objset) != 0) {
329655434c77Sek 			nvlist_free(nv);
329799653d4eSeschrock 			goto nomem;
3298ea8dc4b6Seschrock 		}
329955434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
330055434c77Sek 		    zb[i].zb_object) != 0) {
330155434c77Sek 			nvlist_free(nv);
330255434c77Sek 			goto nomem;
330355434c77Sek 		}
330455434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
330555434c77Sek 			nvlist_free(nv);
330655434c77Sek 			goto nomem;
330755434c77Sek 		}
330855434c77Sek 		nvlist_free(nv);
3309ea8dc4b6Seschrock 	}
3310ea8dc4b6Seschrock 
33113ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3312ea8dc4b6Seschrock 	return (0);
331399653d4eSeschrock 
331499653d4eSeschrock nomem:
3315e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
331699653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3317ea8dc4b6Seschrock }
3318eaca9bbdSeschrock 
3319eaca9bbdSeschrock /*
3320eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3321eaca9bbdSeschrock  */
3322eaca9bbdSeschrock int
3323990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3324eaca9bbdSeschrock {
3325eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
332699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3327eaca9bbdSeschrock 
3328eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3329990b4856Slling 	zc.zc_cookie = new_version;
3330990b4856Slling 
3331ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3332ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
333399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
333499653d4eSeschrock 		    zhp->zpool_name));
3335eaca9bbdSeschrock 	return (0);
3336eaca9bbdSeschrock }
333706eeb2adSek 
333806eeb2adSek void
33392a6b87f0Sek zpool_set_history_str(const char *subcommand, int argc, char **argv,
33402a6b87f0Sek     char *history_str)
334106eeb2adSek {
334206eeb2adSek 	int i;
334306eeb2adSek 
33442a6b87f0Sek 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
33452a6b87f0Sek 	for (i = 1; i < argc; i++) {
33462a6b87f0Sek 		if (strlen(history_str) + 1 + strlen(argv[i]) >
33472a6b87f0Sek 		    HIS_MAX_RECORD_LEN)
33482a6b87f0Sek 			break;
33492a6b87f0Sek 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
33502a6b87f0Sek 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
33512a6b87f0Sek 	}
33522a6b87f0Sek }
33532a6b87f0Sek 
33542a6b87f0Sek /*
33552a6b87f0Sek  * Stage command history for logging.
33562a6b87f0Sek  */
33572a6b87f0Sek int
33582a6b87f0Sek zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
33592a6b87f0Sek {
33602a6b87f0Sek 	if (history_str == NULL)
33612a6b87f0Sek 		return (EINVAL);
33622a6b87f0Sek 
33632a6b87f0Sek 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
33642a6b87f0Sek 		return (EINVAL);
33652a6b87f0Sek 
3366228975ccSek 	if (hdl->libzfs_log_str != NULL)
3367ecd6cf80Smarks 		free(hdl->libzfs_log_str);
336806eeb2adSek 
33692a6b87f0Sek 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
33702a6b87f0Sek 		return (no_memory(hdl));
337106eeb2adSek 
33722a6b87f0Sek 	return (0);
337306eeb2adSek }
337406eeb2adSek 
337506eeb2adSek /*
337606eeb2adSek  * Perform ioctl to get some command history of a pool.
337706eeb2adSek  *
337806eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
337906eeb2adSek  * logical offset of the history buffer to start reading from.
338006eeb2adSek  *
338106eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
338206eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
338306eeb2adSek  */
338406eeb2adSek static int
338506eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
338606eeb2adSek {
338706eeb2adSek 	zfs_cmd_t zc = { 0 };
338806eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
338906eeb2adSek 
339006eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
339106eeb2adSek 
339206eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
339306eeb2adSek 	zc.zc_history_len = *len;
339406eeb2adSek 	zc.zc_history_offset = *off;
339506eeb2adSek 
339606eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
339706eeb2adSek 		switch (errno) {
339806eeb2adSek 		case EPERM:
3399ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3400ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
340106eeb2adSek 			    "cannot show history for pool '%s'"),
340206eeb2adSek 			    zhp->zpool_name));
340306eeb2adSek 		case ENOENT:
3404ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
340506eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
340606eeb2adSek 			    "'%s'"), zhp->zpool_name));
3407d7306b64Sek 		case ENOTSUP:
3408d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3409d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3410d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
341106eeb2adSek 		default:
3412ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
341306eeb2adSek 			    dgettext(TEXT_DOMAIN,
341406eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
341506eeb2adSek 		}
341606eeb2adSek 	}
341706eeb2adSek 
341806eeb2adSek 	*len = zc.zc_history_len;
341906eeb2adSek 	*off = zc.zc_history_offset;
342006eeb2adSek 
342106eeb2adSek 	return (0);
342206eeb2adSek }
342306eeb2adSek 
342406eeb2adSek /*
342506eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
342606eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
342706eeb2adSek  * processed as there wasn't a complete record.
342806eeb2adSek  */
34298f18d1faSGeorge Wilson int
343006eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
343106eeb2adSek     nvlist_t ***records, uint_t *numrecords)
343206eeb2adSek {
343306eeb2adSek 	uint64_t reclen;
343406eeb2adSek 	nvlist_t *nv;
343506eeb2adSek 	int i;
343606eeb2adSek 
343706eeb2adSek 	while (bytes_read > sizeof (reclen)) {
343806eeb2adSek 
343906eeb2adSek 		/* get length of packed record (stored as little endian) */
344006eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
344106eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
344206eeb2adSek 
344306eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
344406eeb2adSek 			break;
344506eeb2adSek 
344606eeb2adSek 		/* unpack record */
344706eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
344806eeb2adSek 			return (ENOMEM);
344906eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
345006eeb2adSek 		buf += sizeof (reclen) + reclen;
345106eeb2adSek 
345206eeb2adSek 		/* add record to nvlist array */
345306eeb2adSek 		(*numrecords)++;
345406eeb2adSek 		if (ISP2(*numrecords + 1)) {
345506eeb2adSek 			*records = realloc(*records,
345606eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
345706eeb2adSek 		}
345806eeb2adSek 		(*records)[*numrecords - 1] = nv;
345906eeb2adSek 	}
346006eeb2adSek 
346106eeb2adSek 	*leftover = bytes_read;
346206eeb2adSek 	return (0);
346306eeb2adSek }
346406eeb2adSek 
346506eeb2adSek #define	HIS_BUF_LEN	(128*1024)
346606eeb2adSek 
346706eeb2adSek /*
346806eeb2adSek  * Retrieve the command history of a pool.
346906eeb2adSek  */
347006eeb2adSek int
347106eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
347206eeb2adSek {
347306eeb2adSek 	char buf[HIS_BUF_LEN];
347406eeb2adSek 	uint64_t off = 0;
347506eeb2adSek 	nvlist_t **records = NULL;
347606eeb2adSek 	uint_t numrecords = 0;
347706eeb2adSek 	int err, i;
347806eeb2adSek 
347906eeb2adSek 	do {
348006eeb2adSek 		uint64_t bytes_read = sizeof (buf);
348106eeb2adSek 		uint64_t leftover;
348206eeb2adSek 
348306eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
348406eeb2adSek 			break;
348506eeb2adSek 
348606eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
348706eeb2adSek 		if (!bytes_read)
348806eeb2adSek 			break;
348906eeb2adSek 
349006eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
349106eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
349206eeb2adSek 			break;
349306eeb2adSek 		off -= leftover;
349406eeb2adSek 
349506eeb2adSek 		/* CONSTCOND */
349606eeb2adSek 	} while (1);
349706eeb2adSek 
349806eeb2adSek 	if (!err) {
349906eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
350006eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
350106eeb2adSek 		    records, numrecords) == 0);
350206eeb2adSek 	}
350306eeb2adSek 	for (i = 0; i < numrecords; i++)
350406eeb2adSek 		nvlist_free(records[i]);
350506eeb2adSek 	free(records);
350606eeb2adSek 
350706eeb2adSek 	return (err);
350806eeb2adSek }
350955434c77Sek 
351055434c77Sek void
351155434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
351255434c77Sek     char *pathname, size_t len)
351355434c77Sek {
351455434c77Sek 	zfs_cmd_t zc = { 0 };
351555434c77Sek 	boolean_t mounted = B_FALSE;
351655434c77Sek 	char *mntpnt = NULL;
351755434c77Sek 	char dsname[MAXNAMELEN];
351855434c77Sek 
351955434c77Sek 	if (dsobj == 0) {
352055434c77Sek 		/* special case for the MOS */
352155434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
352255434c77Sek 		return;
352355434c77Sek 	}
352455434c77Sek 
352555434c77Sek 	/* get the dataset's name */
352655434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
352755434c77Sek 	zc.zc_obj = dsobj;
352855434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
352955434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
353055434c77Sek 		/* just write out a path of two object numbers */
353155434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
353255434c77Sek 		    dsobj, obj);
353355434c77Sek 		return;
353455434c77Sek 	}
353555434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
353655434c77Sek 
353755434c77Sek 	/* find out if the dataset is mounted */
353855434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
353955434c77Sek 
354055434c77Sek 	/* get the corrupted object's path */
354155434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
354255434c77Sek 	zc.zc_obj = obj;
354355434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
354455434c77Sek 	    &zc) == 0) {
354555434c77Sek 		if (mounted) {
354655434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
354755434c77Sek 			    zc.zc_value);
354855434c77Sek 		} else {
354955434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
355055434c77Sek 			    dsname, zc.zc_value);
355155434c77Sek 		}
355255434c77Sek 	} else {
355355434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
355455434c77Sek 	}
355555434c77Sek 	free(mntpnt);
355655434c77Sek }
3557b1b8ab34Slling 
355815e6edf1Sgw /*
355915e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
356015e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
356115e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
356215e6edf1Sgw  * partition.
356315e6edf1Sgw  */
356415e6edf1Sgw static int
356515e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb)
356615e6edf1Sgw {
356715e6edf1Sgw 	char *path;
356815e6edf1Sgw 	int fd;
356915e6edf1Sgw 	char diskname[MAXPATHLEN];
357015e6edf1Sgw 	int err = -1;
357115e6edf1Sgw 
357215e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
357315e6edf1Sgw 		return (err);
357415e6edf1Sgw 
357515e6edf1Sgw 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
357615e6edf1Sgw 	    strrchr(path, '/'));
357715e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
357815e6edf1Sgw 		struct dk_gpt *vtoc;
357915e6edf1Sgw 
358015e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
358115e6edf1Sgw 			if (sb != NULL)
358215e6edf1Sgw 				*sb = vtoc->efi_parts[0].p_start;
358315e6edf1Sgw 			efi_free(vtoc);
358415e6edf1Sgw 		}
358515e6edf1Sgw 		(void) close(fd);
358615e6edf1Sgw 	}
358715e6edf1Sgw 	return (err);
358815e6edf1Sgw }
358915e6edf1Sgw 
35908488aeb5Staylor /*
35918488aeb5Staylor  * determine where a partition starts on a disk in the current
35928488aeb5Staylor  * configuration
35938488aeb5Staylor  */
35948488aeb5Staylor static diskaddr_t
35958488aeb5Staylor find_start_block(nvlist_t *config)
35968488aeb5Staylor {
35978488aeb5Staylor 	nvlist_t **child;
35988488aeb5Staylor 	uint_t c, children;
35998488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
36008488aeb5Staylor 	uint64_t wholedisk;
36018488aeb5Staylor 
36028488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
36038488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
36048488aeb5Staylor 		if (nvlist_lookup_uint64(config,
36058488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
36068488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
36078488aeb5Staylor 			return (MAXOFFSET_T);
36088488aeb5Staylor 		}
360915e6edf1Sgw 		if (read_efi_label(config, &sb) < 0)
361015e6edf1Sgw 			sb = MAXOFFSET_T;
36118488aeb5Staylor 		return (sb);
36128488aeb5Staylor 	}
36138488aeb5Staylor 
36148488aeb5Staylor 	for (c = 0; c < children; c++) {
36158488aeb5Staylor 		sb = find_start_block(child[c]);
36168488aeb5Staylor 		if (sb != MAXOFFSET_T) {
36178488aeb5Staylor 			return (sb);
36188488aeb5Staylor 		}
36198488aeb5Staylor 	}
36208488aeb5Staylor 	return (MAXOFFSET_T);
36218488aeb5Staylor }
36228488aeb5Staylor 
36238488aeb5Staylor /*
36248488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
36258488aeb5Staylor  * stripped of any leading /dev path.
36268488aeb5Staylor  */
36278488aeb5Staylor int
36288488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
36298488aeb5Staylor {
36308488aeb5Staylor 	char path[MAXPATHLEN];
36318488aeb5Staylor 	struct dk_gpt *vtoc;
36328488aeb5Staylor 	int fd;
36338488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
36348488aeb5Staylor 	uint64_t slice_size;
36358488aeb5Staylor 	diskaddr_t start_block;
36368488aeb5Staylor 	char errbuf[1024];
36378488aeb5Staylor 
3638c6ef114fSmmusante 	/* prepare an error message just in case */
3639c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
3640c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3641c6ef114fSmmusante 
36428488aeb5Staylor 	if (zhp) {
36438488aeb5Staylor 		nvlist_t *nvroot;
36448488aeb5Staylor 
3645b5b76fecSGeorge Wilson 		if (pool_is_bootable(zhp)) {
3646b5b76fecSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3647b5b76fecSGeorge Wilson 			    "EFI labeled devices are not supported on root "
3648b5b76fecSGeorge Wilson 			    "pools."));
3649b5b76fecSGeorge Wilson 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3650b5b76fecSGeorge Wilson 		}
3651b5b76fecSGeorge Wilson 
36528488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
36538488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
36548488aeb5Staylor 
36558488aeb5Staylor 		if (zhp->zpool_start_block == 0)
36568488aeb5Staylor 			start_block = find_start_block(nvroot);
36578488aeb5Staylor 		else
36588488aeb5Staylor 			start_block = zhp->zpool_start_block;
36598488aeb5Staylor 		zhp->zpool_start_block = start_block;
36608488aeb5Staylor 	} else {
36618488aeb5Staylor 		/* new pool */
36628488aeb5Staylor 		start_block = NEW_START_BLOCK;
36638488aeb5Staylor 	}
36648488aeb5Staylor 
36658488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
36668488aeb5Staylor 	    BACKUP_SLICE);
36678488aeb5Staylor 
36688488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
36698488aeb5Staylor 		/*
36708488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
36718488aeb5Staylor 		 * is a valid device.
36728488aeb5Staylor 		 */
3673c6ef114fSmmusante 		zfs_error_aux(hdl,
3674c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
36758488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
36768488aeb5Staylor 	}
36778488aeb5Staylor 
36788488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
36798488aeb5Staylor 		/*
36808488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
36818488aeb5Staylor 		 * were unable to read the disk's capacity
36828488aeb5Staylor 		 */
36838488aeb5Staylor 		if (errno == ENOMEM)
36848488aeb5Staylor 			(void) no_memory(hdl);
36858488aeb5Staylor 
36868488aeb5Staylor 		(void) close(fd);
3687c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3688c6ef114fSmmusante 		    "unable to read disk capacity"), name);
36898488aeb5Staylor 
36908488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
36918488aeb5Staylor 	}
36928488aeb5Staylor 
36938488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
36948488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
36958488aeb5Staylor 	if (start_block == MAXOFFSET_T)
36968488aeb5Staylor 		start_block = NEW_START_BLOCK;
36978488aeb5Staylor 	slice_size -= start_block;
36988488aeb5Staylor 
36998488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
37008488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
37018488aeb5Staylor 
37028488aeb5Staylor 	/*
37038488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
37048488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
37058488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
37068488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
37078488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
37088488aeb5Staylor 	 * can get, in the absence of V_OTHER.
37098488aeb5Staylor 	 */
37108488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
37118488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
37128488aeb5Staylor 
37138488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
37148488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
37158488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
37168488aeb5Staylor 
37178488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
37188488aeb5Staylor 		/*
37198488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
37208488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
37218488aeb5Staylor 		 * ecting the user to manually label the disk and give
37228488aeb5Staylor 		 * a specific slice.
37238488aeb5Staylor 		 */
37248488aeb5Staylor 		(void) close(fd);
37258488aeb5Staylor 		efi_free(vtoc);
37268488aeb5Staylor 
37278488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3728c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
37298488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
37308488aeb5Staylor 	}
37318488aeb5Staylor 
37328488aeb5Staylor 	(void) close(fd);
37338488aeb5Staylor 	efi_free(vtoc);
37348488aeb5Staylor 	return (0);
37358488aeb5Staylor }
3736e7cbe64fSgw 
3737e7cbe64fSgw static boolean_t
3738e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3739e7cbe64fSgw {
3740e7cbe64fSgw 	char *type;
3741e7cbe64fSgw 	nvlist_t **child;
3742e7cbe64fSgw 	uint_t children, c;
3743e7cbe64fSgw 
3744e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3745e7cbe64fSgw 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3746e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
3747e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
374888ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3749e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3750e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3751e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
3752e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3753e7cbe64fSgw 		return (B_FALSE);
3754e7cbe64fSgw 	}
3755e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3756e7cbe64fSgw 	    &child, &children) == 0) {
3757e7cbe64fSgw 		for (c = 0; c < children; c++) {
3758e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3759e7cbe64fSgw 				return (B_FALSE);
3760e7cbe64fSgw 		}
3761e7cbe64fSgw 	}
3762e7cbe64fSgw 	return (B_TRUE);
3763e7cbe64fSgw }
3764e7cbe64fSgw 
3765e7cbe64fSgw /*
3766e7cbe64fSgw  * check if this zvol is allowable for use as a dump device; zero if
3767e7cbe64fSgw  * it is, > 0 if it isn't, < 0 if it isn't a zvol
3768e7cbe64fSgw  */
3769e7cbe64fSgw int
3770e7cbe64fSgw zvol_check_dump_config(char *arg)
3771e7cbe64fSgw {
3772e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
3773e7cbe64fSgw 	nvlist_t *config, *nvroot;
3774e7cbe64fSgw 	char *p, *volname;
3775e7cbe64fSgw 	nvlist_t **top;
3776e7cbe64fSgw 	uint_t toplevels;
3777e7cbe64fSgw 	libzfs_handle_t *hdl;
3778e7cbe64fSgw 	char errbuf[1024];
3779e7cbe64fSgw 	char poolname[ZPOOL_MAXNAMELEN];
3780e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3781e7cbe64fSgw 	int ret = 1;
3782e7cbe64fSgw 
3783e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3784e7cbe64fSgw 		return (-1);
3785e7cbe64fSgw 	}
3786e7cbe64fSgw 
3787e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3788e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
3789e7cbe64fSgw 
3790e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
3791e7cbe64fSgw 		return (1);
3792e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
3793e7cbe64fSgw 
3794e7cbe64fSgw 	volname = arg + pathlen;
3795e7cbe64fSgw 
3796e7cbe64fSgw 	/* check the configuration of the pool */
3797e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
3798e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3799e7cbe64fSgw 		    "malformed dataset name"));
3800e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3801e7cbe64fSgw 		return (1);
3802e7cbe64fSgw 	} else if (p - volname >= ZFS_MAXNAMELEN) {
3803e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3804e7cbe64fSgw 		    "dataset name is too long"));
3805e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3806e7cbe64fSgw 		return (1);
3807e7cbe64fSgw 	} else {
3808e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
3809e7cbe64fSgw 		poolname[p - volname] = '\0';
3810e7cbe64fSgw 	}
3811e7cbe64fSgw 
3812e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3813e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3814e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
3815e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3816e7cbe64fSgw 		goto out;
3817e7cbe64fSgw 	}
3818e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
3819e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3820e7cbe64fSgw 	    &nvroot) != 0) {
3821e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3822e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
3823e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3824e7cbe64fSgw 		goto out;
3825e7cbe64fSgw 	}
3826e7cbe64fSgw 
3827e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3828e7cbe64fSgw 	    &top, &toplevels) == 0);
3829e7cbe64fSgw 	if (toplevels != 1) {
3830e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3831e7cbe64fSgw 		    "'%s' has multiple top level vdevs"), poolname);
3832e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3833e7cbe64fSgw 		goto out;
3834e7cbe64fSgw 	}
3835e7cbe64fSgw 
3836e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
3837e7cbe64fSgw 		goto out;
3838e7cbe64fSgw 	}
3839e7cbe64fSgw 	ret = 0;
3840e7cbe64fSgw 
3841e7cbe64fSgw out:
3842e7cbe64fSgw 	if (zhp)
3843e7cbe64fSgw 		zpool_close(zhp);
3844e7cbe64fSgw 	libzfs_fini(hdl);
3845e7cbe64fSgw 	return (ret);
3846e7cbe64fSgw }
3847