xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision ad135b5d644628e791c3188a6ecbd9c257961ef8)
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.
254263d13fSGeorge Wilson  * Copyright (c) 2012 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"
46*ad135b5dSChristopher Siden #include "zfeature_common.h"
47fa9e4066Sahrens 
4815e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
49990b4856Slling 
50573ca77eSGeorge Wilson #define	DISK_ROOT	"/dev/dsk"
51573ca77eSGeorge Wilson #define	RDISK_ROOT	"/dev/rdsk"
52573ca77eSGeorge Wilson #define	BACKUP_SLICE	"s2"
53573ca77eSGeorge Wilson 
54f9af39baSGeorge Wilson typedef struct prop_flags {
55f9af39baSGeorge Wilson 	int create:1;	/* Validate property on creation */
56f9af39baSGeorge Wilson 	int import:1;	/* Validate property on import */
57f9af39baSGeorge Wilson } prop_flags_t;
58f9af39baSGeorge Wilson 
59990b4856Slling /*
60990b4856Slling  * ====================================================================
61990b4856Slling  *   zpool property functions
62990b4856Slling  * ====================================================================
63990b4856Slling  */
64990b4856Slling 
65990b4856Slling static int
66990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
67990b4856Slling {
68990b4856Slling 	zfs_cmd_t zc = { 0 };
69990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
70990b4856Slling 
71990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
72990b4856Slling 
73990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
74990b4856Slling 		return (-1);
75990b4856Slling 
76990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
77990b4856Slling 		if (errno == ENOMEM) {
78990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
79990b4856Slling 				zcmd_free_nvlists(&zc);
80990b4856Slling 				return (-1);
81990b4856Slling 			}
82990b4856Slling 		} else {
83990b4856Slling 			zcmd_free_nvlists(&zc);
84990b4856Slling 			return (-1);
85990b4856Slling 		}
86990b4856Slling 	}
87990b4856Slling 
88990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
89990b4856Slling 		zcmd_free_nvlists(&zc);
90990b4856Slling 		return (-1);
91990b4856Slling 	}
92990b4856Slling 
93990b4856Slling 	zcmd_free_nvlists(&zc);
94990b4856Slling 
95990b4856Slling 	return (0);
96990b4856Slling }
97990b4856Slling 
98990b4856Slling static int
99990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
100990b4856Slling {
101990b4856Slling 	nvlist_t *old_props;
102990b4856Slling 
103990b4856Slling 	old_props = zhp->zpool_props;
104990b4856Slling 
105990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
106990b4856Slling 		return (-1);
107990b4856Slling 
108990b4856Slling 	nvlist_free(old_props);
109990b4856Slling 	return (0);
110990b4856Slling }
111990b4856Slling 
112990b4856Slling static char *
113990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
114990b4856Slling     zprop_source_t *src)
115990b4856Slling {
116990b4856Slling 	nvlist_t *nv, *nvl;
117990b4856Slling 	uint64_t ival;
118990b4856Slling 	char *value;
119990b4856Slling 	zprop_source_t source;
120990b4856Slling 
121990b4856Slling 	nvl = zhp->zpool_props;
122990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
123990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
124990b4856Slling 		source = ival;
125990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
126990b4856Slling 	} else {
127990b4856Slling 		source = ZPROP_SRC_DEFAULT;
128990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
129990b4856Slling 			value = "-";
130990b4856Slling 	}
131990b4856Slling 
132990b4856Slling 	if (src)
133990b4856Slling 		*src = source;
134990b4856Slling 
135990b4856Slling 	return (value);
136990b4856Slling }
137990b4856Slling 
138990b4856Slling uint64_t
139990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
140990b4856Slling {
141990b4856Slling 	nvlist_t *nv, *nvl;
142990b4856Slling 	uint64_t value;
143990b4856Slling 	zprop_source_t source;
144990b4856Slling 
145b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
146b87f3af3Sperrin 		/*
147b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
148b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
149b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
150b87f3af3Sperrin 		 */
151b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
152b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
153b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
154b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
155b87f3af3Sperrin 		    == 0)) {
156b87f3af3Sperrin 			return (value);
157b87f3af3Sperrin 		}
158990b4856Slling 		return (zpool_prop_default_numeric(prop));
159b87f3af3Sperrin 	}
160990b4856Slling 
161990b4856Slling 	nvl = zhp->zpool_props;
162990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
163990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
164990b4856Slling 		source = value;
165990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
166990b4856Slling 	} else {
167990b4856Slling 		source = ZPROP_SRC_DEFAULT;
168990b4856Slling 		value = zpool_prop_default_numeric(prop);
169990b4856Slling 	}
170990b4856Slling 
171990b4856Slling 	if (src)
172990b4856Slling 		*src = source;
173990b4856Slling 
174990b4856Slling 	return (value);
175990b4856Slling }
176990b4856Slling 
177990b4856Slling /*
178990b4856Slling  * Map VDEV STATE to printed strings.
179990b4856Slling  */
180990b4856Slling char *
181990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
182990b4856Slling {
183990b4856Slling 	switch (state) {
184990b4856Slling 	case VDEV_STATE_CLOSED:
185990b4856Slling 	case VDEV_STATE_OFFLINE:
186990b4856Slling 		return (gettext("OFFLINE"));
187990b4856Slling 	case VDEV_STATE_REMOVED:
188990b4856Slling 		return (gettext("REMOVED"));
189990b4856Slling 	case VDEV_STATE_CANT_OPEN:
190b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
191990b4856Slling 			return (gettext("FAULTED"));
1921195e687SMark J Musante 		else if (aux == VDEV_AUX_SPLIT_POOL)
1931195e687SMark J Musante 			return (gettext("SPLIT"));
194990b4856Slling 		else
195990b4856Slling 			return (gettext("UNAVAIL"));
196990b4856Slling 	case VDEV_STATE_FAULTED:
197990b4856Slling 		return (gettext("FAULTED"));
198990b4856Slling 	case VDEV_STATE_DEGRADED:
199990b4856Slling 		return (gettext("DEGRADED"));
200990b4856Slling 	case VDEV_STATE_HEALTHY:
201990b4856Slling 		return (gettext("ONLINE"));
202990b4856Slling 	}
203990b4856Slling 
204990b4856Slling 	return (gettext("UNKNOWN"));
205990b4856Slling }
206990b4856Slling 
207990b4856Slling /*
208990b4856Slling  * Get a zpool property value for 'prop' and return the value in
209990b4856Slling  * a pre-allocated buffer.
210990b4856Slling  */
211990b4856Slling int
212990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
213990b4856Slling     zprop_source_t *srctype)
214990b4856Slling {
215990b4856Slling 	uint64_t intval;
216990b4856Slling 	const char *strval;
217990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
218990b4856Slling 	nvlist_t *nvroot;
219990b4856Slling 	vdev_stat_t *vs;
220990b4856Slling 	uint_t vsc;
221990b4856Slling 
222990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
223379c004dSEric Schrock 		switch (prop) {
224379c004dSEric Schrock 		case ZPOOL_PROP_NAME:
225990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
226379c004dSEric Schrock 			break;
227379c004dSEric Schrock 
228379c004dSEric Schrock 		case ZPOOL_PROP_HEALTH:
229990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
230379c004dSEric Schrock 			break;
231379c004dSEric Schrock 
232379c004dSEric Schrock 		case ZPOOL_PROP_GUID:
233379c004dSEric Schrock 			intval = zpool_get_prop_int(zhp, prop, &src);
234379c004dSEric Schrock 			(void) snprintf(buf, len, "%llu", intval);
235379c004dSEric Schrock 			break;
236379c004dSEric Schrock 
237379c004dSEric Schrock 		case ZPOOL_PROP_ALTROOT:
238379c004dSEric Schrock 		case ZPOOL_PROP_CACHEFILE:
2398704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
240379c004dSEric Schrock 			if (zhp->zpool_props != NULL ||
241379c004dSEric Schrock 			    zpool_get_all_props(zhp) == 0) {
242379c004dSEric Schrock 				(void) strlcpy(buf,
243379c004dSEric Schrock 				    zpool_get_prop_string(zhp, prop, &src),
244379c004dSEric Schrock 				    len);
245379c004dSEric Schrock 				if (srctype != NULL)
246379c004dSEric Schrock 					*srctype = src;
247379c004dSEric Schrock 				return (0);
248379c004dSEric Schrock 			}
249379c004dSEric Schrock 			/* FALLTHROUGH */
250379c004dSEric Schrock 		default:
251990b4856Slling 			(void) strlcpy(buf, "-", len);
252379c004dSEric Schrock 			break;
253379c004dSEric Schrock 		}
254379c004dSEric Schrock 
255379c004dSEric Schrock 		if (srctype != NULL)
256379c004dSEric Schrock 			*srctype = src;
257990b4856Slling 		return (0);
258990b4856Slling 	}
259990b4856Slling 
260990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
261990b4856Slling 	    prop != ZPOOL_PROP_NAME)
262990b4856Slling 		return (-1);
263990b4856Slling 
264990b4856Slling 	switch (zpool_prop_get_type(prop)) {
265990b4856Slling 	case PROP_TYPE_STRING:
266990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
267990b4856Slling 		    len);
268990b4856Slling 		break;
269990b4856Slling 
270990b4856Slling 	case PROP_TYPE_NUMBER:
271990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
272990b4856Slling 
273990b4856Slling 		switch (prop) {
274990b4856Slling 		case ZPOOL_PROP_SIZE:
275485bbbf5SGeorge Wilson 		case ZPOOL_PROP_ALLOCATED:
276485bbbf5SGeorge Wilson 		case ZPOOL_PROP_FREE:
277*ad135b5dSChristopher Siden 		case ZPOOL_PROP_FREEING:
2784263d13fSGeorge Wilson 		case ZPOOL_PROP_EXPANDSZ:
279990b4856Slling 			(void) zfs_nicenum(intval, buf, len);
280990b4856Slling 			break;
281990b4856Slling 
282990b4856Slling 		case ZPOOL_PROP_CAPACITY:
283990b4856Slling 			(void) snprintf(buf, len, "%llu%%",
284990b4856Slling 			    (u_longlong_t)intval);
285990b4856Slling 			break;
286990b4856Slling 
287b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
288b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
289b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
290b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
291b24ab676SJeff Bonwick 			break;
292b24ab676SJeff Bonwick 
293990b4856Slling 		case ZPOOL_PROP_HEALTH:
294990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
295990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
296990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2973f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2983f9d6ad7SLin Ling 			    == 0);
299990b4856Slling 
300990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
301990b4856Slling 			    vs->vs_aux), len);
302990b4856Slling 			break;
303*ad135b5dSChristopher Siden 		case ZPOOL_PROP_VERSION:
304*ad135b5dSChristopher Siden 			if (intval >= SPA_VERSION_FEATURES) {
305*ad135b5dSChristopher Siden 				(void) snprintf(buf, len, "-");
306*ad135b5dSChristopher Siden 				break;
307*ad135b5dSChristopher Siden 			}
308*ad135b5dSChristopher Siden 			/* FALLTHROUGH */
309990b4856Slling 		default:
310990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
311990b4856Slling 		}
312990b4856Slling 		break;
313990b4856Slling 
314990b4856Slling 	case PROP_TYPE_INDEX:
315990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
316990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
317990b4856Slling 		    != 0)
318990b4856Slling 			return (-1);
319990b4856Slling 		(void) strlcpy(buf, strval, len);
320990b4856Slling 		break;
321990b4856Slling 
322990b4856Slling 	default:
323990b4856Slling 		abort();
324990b4856Slling 	}
325990b4856Slling 
326990b4856Slling 	if (srctype)
327990b4856Slling 		*srctype = src;
328990b4856Slling 
329990b4856Slling 	return (0);
330990b4856Slling }
331990b4856Slling 
332990b4856Slling /*
333990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
334990b4856Slling  * Assuming bootfs is a valid dataset name.
335990b4856Slling  */
336990b4856Slling static boolean_t
337990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
338990b4856Slling {
339990b4856Slling 	int len = strlen(pool);
340990b4856Slling 
341fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
342990b4856Slling 		return (B_FALSE);
343990b4856Slling 
344990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
345990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
346990b4856Slling 		return (B_TRUE);
347990b4856Slling 
348990b4856Slling 	return (B_FALSE);
349990b4856Slling }
350990b4856Slling 
35115e6edf1Sgw /*
35215e6edf1Sgw  * Inspect the configuration to determine if any of the devices contain
35315e6edf1Sgw  * an EFI label.
35415e6edf1Sgw  */
35515e6edf1Sgw static boolean_t
35615e6edf1Sgw pool_uses_efi(nvlist_t *config)
35715e6edf1Sgw {
35815e6edf1Sgw 	nvlist_t **child;
35915e6edf1Sgw 	uint_t c, children;
36015e6edf1Sgw 
36115e6edf1Sgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
36215e6edf1Sgw 	    &child, &children) != 0)
36315e6edf1Sgw 		return (read_efi_label(config, NULL) >= 0);
36415e6edf1Sgw 
36515e6edf1Sgw 	for (c = 0; c < children; c++) {
36615e6edf1Sgw 		if (pool_uses_efi(child[c]))
36715e6edf1Sgw 			return (B_TRUE);
36815e6edf1Sgw 	}
36915e6edf1Sgw 	return (B_FALSE);
37015e6edf1Sgw }
37115e6edf1Sgw 
3724263d13fSGeorge Wilson boolean_t
3734263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
374b5b76fecSGeorge Wilson {
375b5b76fecSGeorge Wilson 	char bootfs[ZPOOL_MAXNAMELEN];
376b5b76fecSGeorge Wilson 
377b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
378b5b76fecSGeorge Wilson 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
379b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
380b5b76fecSGeorge Wilson }
381b5b76fecSGeorge Wilson 
382b5b76fecSGeorge Wilson 
383990b4856Slling /*
384990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
385990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
386990b4856Slling  * specified as strings.
387990b4856Slling  */
388990b4856Slling static nvlist_t *
3890a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
390f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
391990b4856Slling {
392990b4856Slling 	nvpair_t *elem;
393990b4856Slling 	nvlist_t *retprops;
394990b4856Slling 	zpool_prop_t prop;
395990b4856Slling 	char *strval;
396990b4856Slling 	uint64_t intval;
3978704186eSDan McDonald 	char *slash, *check;
3982f8aaab3Seschrock 	struct stat64 statbuf;
39915e6edf1Sgw 	zpool_handle_t *zhp;
40015e6edf1Sgw 	nvlist_t *nvroot;
401990b4856Slling 
402990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
403990b4856Slling 		(void) no_memory(hdl);
404990b4856Slling 		return (NULL);
405990b4856Slling 	}
406990b4856Slling 
407990b4856Slling 	elem = NULL;
408990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
409990b4856Slling 		const char *propname = nvpair_name(elem);
410990b4856Slling 
411*ad135b5dSChristopher Siden 		prop = zpool_name_to_prop(propname);
412*ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
413*ad135b5dSChristopher Siden 			int err;
414*ad135b5dSChristopher Siden 			zfeature_info_t *feature;
415*ad135b5dSChristopher Siden 			char *fname = strchr(propname, '@') + 1;
416*ad135b5dSChristopher Siden 
417*ad135b5dSChristopher Siden 			err = zfeature_lookup_name(fname, &feature);
418*ad135b5dSChristopher Siden 			if (err != 0) {
419*ad135b5dSChristopher Siden 				ASSERT3U(err, ==, ENOENT);
420*ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
421*ad135b5dSChristopher Siden 				    "invalid feature '%s'"), fname);
422*ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
423*ad135b5dSChristopher Siden 				goto error;
424*ad135b5dSChristopher Siden 			}
425*ad135b5dSChristopher Siden 
426*ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
427*ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
428*ad135b5dSChristopher Siden 				    "'%s' must be a string"), propname);
429*ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
430*ad135b5dSChristopher Siden 				goto error;
431*ad135b5dSChristopher Siden 			}
432*ad135b5dSChristopher Siden 
433*ad135b5dSChristopher Siden 			(void) nvpair_value_string(elem, &strval);
434*ad135b5dSChristopher Siden 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
435*ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
436*ad135b5dSChristopher Siden 				    "property '%s' can only be set to "
437*ad135b5dSChristopher Siden 				    "'enabled'"), propname);
438*ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
439*ad135b5dSChristopher Siden 				goto error;
440*ad135b5dSChristopher Siden 			}
441*ad135b5dSChristopher Siden 
442*ad135b5dSChristopher Siden 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
443*ad135b5dSChristopher Siden 				(void) no_memory(hdl);
444*ad135b5dSChristopher Siden 				goto error;
445*ad135b5dSChristopher Siden 			}
446*ad135b5dSChristopher Siden 			continue;
447*ad135b5dSChristopher Siden 		}
448*ad135b5dSChristopher Siden 
449990b4856Slling 		/*
450990b4856Slling 		 * Make sure this property is valid and applies to this type.
451990b4856Slling 		 */
452*ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL) {
453990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
454990b4856Slling 			    "invalid property '%s'"), propname);
455990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
456990b4856Slling 			goto error;
457990b4856Slling 		}
458990b4856Slling 
459990b4856Slling 		if (zpool_prop_readonly(prop)) {
460990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
461990b4856Slling 			    "is readonly"), propname);
462990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
463990b4856Slling 			goto error;
464990b4856Slling 		}
465990b4856Slling 
466990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
467990b4856Slling 		    &strval, &intval, errbuf) != 0)
468990b4856Slling 			goto error;
469990b4856Slling 
470990b4856Slling 		/*
471990b4856Slling 		 * Perform additional checking for specific properties.
472990b4856Slling 		 */
473990b4856Slling 		switch (prop) {
474990b4856Slling 		case ZPOOL_PROP_VERSION:
475*ad135b5dSChristopher Siden 			if (intval < version ||
476*ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
477990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
478990b4856Slling 				    "property '%s' number %d is invalid."),
479990b4856Slling 				    propname, intval);
480990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
481990b4856Slling 				goto error;
482990b4856Slling 			}
483990b4856Slling 			break;
484990b4856Slling 
485990b4856Slling 		case ZPOOL_PROP_BOOTFS:
486f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
487990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
488990b4856Slling 				    "property '%s' cannot be set at creation "
489990b4856Slling 				    "or import time"), propname);
490990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
491990b4856Slling 				goto error;
492990b4856Slling 			}
493990b4856Slling 
494990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
495990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
496990b4856Slling 				    "pool must be upgraded to support "
497990b4856Slling 				    "'%s' property"), propname);
498990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
499990b4856Slling 				goto error;
500990b4856Slling 			}
501990b4856Slling 
502990b4856Slling 			/*
503990b4856Slling 			 * bootfs property value has to be a dataset name and
504990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
505990b4856Slling 			 */
506990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
507990b4856Slling 			    strval)) {
508990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
509990b4856Slling 				    "is an invalid name"), strval);
510990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
511990b4856Slling 				goto error;
512990b4856Slling 			}
51315e6edf1Sgw 
51415e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
51515e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51615e6edf1Sgw 				    "could not open pool '%s'"), poolname);
51715e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
51815e6edf1Sgw 				goto error;
51915e6edf1Sgw 			}
52015e6edf1Sgw 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
52115e6edf1Sgw 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
52215e6edf1Sgw 
52315e6edf1Sgw 			/*
52415e6edf1Sgw 			 * bootfs property cannot be set on a disk which has
52515e6edf1Sgw 			 * been EFI labeled.
52615e6edf1Sgw 			 */
52715e6edf1Sgw 			if (pool_uses_efi(nvroot)) {
52815e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52915e6edf1Sgw 				    "property '%s' not supported on "
53015e6edf1Sgw 				    "EFI labeled devices"), propname);
53115e6edf1Sgw 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
53215e6edf1Sgw 				zpool_close(zhp);
53315e6edf1Sgw 				goto error;
53415e6edf1Sgw 			}
53515e6edf1Sgw 			zpool_close(zhp);
536990b4856Slling 			break;
537990b4856Slling 
5382f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
539f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
540990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541990b4856Slling 				    "property '%s' can only be set during pool "
542990b4856Slling 				    "creation or import"), propname);
543990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
544990b4856Slling 				goto error;
545990b4856Slling 			}
546990b4856Slling 
5472f8aaab3Seschrock 			if (strval[0] != '/') {
548990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5492f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
5502f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
551990b4856Slling 				goto error;
552990b4856Slling 			}
5532f8aaab3Seschrock 			break;
5542f8aaab3Seschrock 
5552f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5562f8aaab3Seschrock 			if (strval[0] == '\0')
5572f8aaab3Seschrock 				break;
5582f8aaab3Seschrock 
5592f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5602f8aaab3Seschrock 				break;
561990b4856Slling 
562990b4856Slling 			if (strval[0] != '/') {
563990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5642f8aaab3Seschrock 				    "property '%s' must be empty, an "
5652f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
566990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
567990b4856Slling 				goto error;
568990b4856Slling 			}
569990b4856Slling 
5702f8aaab3Seschrock 			slash = strrchr(strval, '/');
571990b4856Slling 
5722f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5732f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
5742f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5752f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
5762f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5772f8aaab3Seschrock 				goto error;
5782f8aaab3Seschrock 			}
579990b4856Slling 
5802f8aaab3Seschrock 			*slash = '\0';
5812f8aaab3Seschrock 
5822c32020fSeschrock 			if (strval[0] != '\0' &&
5832c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
5842c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
5852f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5862f8aaab3Seschrock 				    "'%s' is not a valid directory"),
5872f8aaab3Seschrock 				    strval);
5882f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5892f8aaab3Seschrock 				goto error;
5902f8aaab3Seschrock 			}
5912f8aaab3Seschrock 
5922f8aaab3Seschrock 			*slash = '/';
5932f8aaab3Seschrock 			break;
594f9af39baSGeorge Wilson 
5958704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
5968704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
5978704186eSDan McDonald 				if (!isprint(*check)) {
5988704186eSDan McDonald 					zfs_error_aux(hdl,
5998704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
6008704186eSDan McDonald 					    "comment may only have printable "
6018704186eSDan McDonald 					    "characters"));
6028704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
6038704186eSDan McDonald 					    errbuf);
6048704186eSDan McDonald 					goto error;
6058704186eSDan McDonald 				}
6068704186eSDan McDonald 			}
6078704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
6088704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6098704186eSDan McDonald 				    "comment must not exceed %d characters"),
6108704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
6118704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6128704186eSDan McDonald 				goto error;
6138704186eSDan McDonald 			}
6148704186eSDan McDonald 			break;
615f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
616f9af39baSGeorge Wilson 			if (!flags.import) {
617f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
618f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
619f9af39baSGeorge Wilson 				    "import time"), propname);
620f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
621f9af39baSGeorge Wilson 				goto error;
622f9af39baSGeorge Wilson 			}
623f9af39baSGeorge Wilson 			break;
624990b4856Slling 		}
625990b4856Slling 	}
626990b4856Slling 
627990b4856Slling 	return (retprops);
628990b4856Slling error:
629990b4856Slling 	nvlist_free(retprops);
630990b4856Slling 	return (NULL);
631990b4856Slling }
632990b4856Slling 
633990b4856Slling /*
634990b4856Slling  * Set zpool property : propname=propval.
635990b4856Slling  */
636990b4856Slling int
637990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
638990b4856Slling {
639990b4856Slling 	zfs_cmd_t zc = { 0 };
640990b4856Slling 	int ret = -1;
641990b4856Slling 	char errbuf[1024];
642990b4856Slling 	nvlist_t *nvl = NULL;
643990b4856Slling 	nvlist_t *realprops;
644990b4856Slling 	uint64_t version;
645f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
646990b4856Slling 
647990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
648990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
649990b4856Slling 	    zhp->zpool_name);
650990b4856Slling 
651990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
652990b4856Slling 		return (no_memory(zhp->zpool_hdl));
653990b4856Slling 
654990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
655990b4856Slling 		nvlist_free(nvl);
656990b4856Slling 		return (no_memory(zhp->zpool_hdl));
657990b4856Slling 	}
658990b4856Slling 
659990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6600a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
661f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
662990b4856Slling 		nvlist_free(nvl);
663990b4856Slling 		return (-1);
664990b4856Slling 	}
665990b4856Slling 
666990b4856Slling 	nvlist_free(nvl);
667990b4856Slling 	nvl = realprops;
668990b4856Slling 
669990b4856Slling 	/*
670990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
671990b4856Slling 	 */
672990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
673990b4856Slling 
674990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
675990b4856Slling 		nvlist_free(nvl);
676990b4856Slling 		return (-1);
677990b4856Slling 	}
678990b4856Slling 
679990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
680990b4856Slling 
681990b4856Slling 	zcmd_free_nvlists(&zc);
682990b4856Slling 	nvlist_free(nvl);
683990b4856Slling 
684990b4856Slling 	if (ret)
685990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
686990b4856Slling 	else
687990b4856Slling 		(void) zpool_props_refresh(zhp);
688990b4856Slling 
689990b4856Slling 	return (ret);
690990b4856Slling }
691990b4856Slling 
692990b4856Slling int
693990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
694990b4856Slling {
695990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
696990b4856Slling 	zprop_list_t *entry;
697990b4856Slling 	char buf[ZFS_MAXPROPLEN];
698*ad135b5dSChristopher Siden 	nvlist_t *features = NULL;
699*ad135b5dSChristopher Siden 	zprop_list_t **last;
700*ad135b5dSChristopher Siden 	boolean_t firstexpand = (NULL == *plp);
701990b4856Slling 
702990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
703990b4856Slling 		return (-1);
704990b4856Slling 
705*ad135b5dSChristopher Siden 	last = plp;
706*ad135b5dSChristopher Siden 	while (*last != NULL)
707*ad135b5dSChristopher Siden 		last = &(*last)->pl_next;
708*ad135b5dSChristopher Siden 
709*ad135b5dSChristopher Siden 	if ((*plp)->pl_all)
710*ad135b5dSChristopher Siden 		features = zpool_get_features(zhp);
711*ad135b5dSChristopher Siden 
712*ad135b5dSChristopher Siden 	if ((*plp)->pl_all && firstexpand) {
713*ad135b5dSChristopher Siden 		for (int i = 0; i < SPA_FEATURES; i++) {
714*ad135b5dSChristopher Siden 			zprop_list_t *entry = zfs_alloc(hdl,
715*ad135b5dSChristopher Siden 			    sizeof (zprop_list_t));
716*ad135b5dSChristopher Siden 			entry->pl_prop = ZPROP_INVAL;
717*ad135b5dSChristopher Siden 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
718*ad135b5dSChristopher Siden 			    spa_feature_table[i].fi_uname);
719*ad135b5dSChristopher Siden 			entry->pl_width = strlen(entry->pl_user_prop);
720*ad135b5dSChristopher Siden 			entry->pl_all = B_TRUE;
721*ad135b5dSChristopher Siden 
722*ad135b5dSChristopher Siden 			*last = entry;
723*ad135b5dSChristopher Siden 			last = &entry->pl_next;
724*ad135b5dSChristopher Siden 		}
725*ad135b5dSChristopher Siden 	}
726*ad135b5dSChristopher Siden 
727*ad135b5dSChristopher Siden 	/* add any unsupported features */
728*ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
729*ad135b5dSChristopher Siden 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
730*ad135b5dSChristopher Siden 		char *propname;
731*ad135b5dSChristopher Siden 		boolean_t found;
732*ad135b5dSChristopher Siden 		zprop_list_t *entry;
733*ad135b5dSChristopher Siden 
734*ad135b5dSChristopher Siden 		if (zfeature_is_supported(nvpair_name(nvp)))
735*ad135b5dSChristopher Siden 			continue;
736*ad135b5dSChristopher Siden 
737*ad135b5dSChristopher Siden 		propname = zfs_asprintf(hdl, "unsupported@%s",
738*ad135b5dSChristopher Siden 		    nvpair_name(nvp));
739*ad135b5dSChristopher Siden 
740*ad135b5dSChristopher Siden 		/*
741*ad135b5dSChristopher Siden 		 * Before adding the property to the list make sure that no
742*ad135b5dSChristopher Siden 		 * other pool already added the same property.
743*ad135b5dSChristopher Siden 		 */
744*ad135b5dSChristopher Siden 		found = B_FALSE;
745*ad135b5dSChristopher Siden 		entry = *plp;
746*ad135b5dSChristopher Siden 		while (entry != NULL) {
747*ad135b5dSChristopher Siden 			if (entry->pl_user_prop != NULL &&
748*ad135b5dSChristopher Siden 			    strcmp(propname, entry->pl_user_prop) == 0) {
749*ad135b5dSChristopher Siden 				found = B_TRUE;
750*ad135b5dSChristopher Siden 				break;
751*ad135b5dSChristopher Siden 			}
752*ad135b5dSChristopher Siden 			entry = entry->pl_next;
753*ad135b5dSChristopher Siden 		}
754*ad135b5dSChristopher Siden 		if (found) {
755*ad135b5dSChristopher Siden 			free(propname);
756*ad135b5dSChristopher Siden 			continue;
757*ad135b5dSChristopher Siden 		}
758*ad135b5dSChristopher Siden 
759*ad135b5dSChristopher Siden 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
760*ad135b5dSChristopher Siden 		entry->pl_prop = ZPROP_INVAL;
761*ad135b5dSChristopher Siden 		entry->pl_user_prop = propname;
762*ad135b5dSChristopher Siden 		entry->pl_width = strlen(entry->pl_user_prop);
763*ad135b5dSChristopher Siden 		entry->pl_all = B_TRUE;
764*ad135b5dSChristopher Siden 
765*ad135b5dSChristopher Siden 		*last = entry;
766*ad135b5dSChristopher Siden 		last = &entry->pl_next;
767*ad135b5dSChristopher Siden 	}
768*ad135b5dSChristopher Siden 
769990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
770990b4856Slling 
771990b4856Slling 		if (entry->pl_fixed)
772990b4856Slling 			continue;
773990b4856Slling 
774990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
775990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
776990b4856Slling 		    NULL) == 0) {
777990b4856Slling 			if (strlen(buf) > entry->pl_width)
778990b4856Slling 				entry->pl_width = strlen(buf);
779990b4856Slling 		}
780990b4856Slling 	}
781990b4856Slling 
782990b4856Slling 	return (0);
783990b4856Slling }
784990b4856Slling 
785*ad135b5dSChristopher Siden /*
786*ad135b5dSChristopher Siden  * Get the state for the given feature on the given ZFS pool.
787*ad135b5dSChristopher Siden  */
788*ad135b5dSChristopher Siden int
789*ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
790*ad135b5dSChristopher Siden     size_t len)
791*ad135b5dSChristopher Siden {
792*ad135b5dSChristopher Siden 	uint64_t refcount;
793*ad135b5dSChristopher Siden 	boolean_t found = B_FALSE;
794*ad135b5dSChristopher Siden 	nvlist_t *features = zpool_get_features(zhp);
795*ad135b5dSChristopher Siden 	boolean_t supported;
796*ad135b5dSChristopher Siden 	const char *feature = strchr(propname, '@') + 1;
797*ad135b5dSChristopher Siden 
798*ad135b5dSChristopher Siden 	supported = zpool_prop_feature(propname);
799*ad135b5dSChristopher Siden 	ASSERT(supported || zfs_prop_unsupported(propname));
800*ad135b5dSChristopher Siden 
801*ad135b5dSChristopher Siden 	/*
802*ad135b5dSChristopher Siden 	 * Convert from feature name to feature guid. This conversion is
803*ad135b5dSChristopher Siden 	 * unecessary for unsupported@... properties because they already
804*ad135b5dSChristopher Siden 	 * use guids.
805*ad135b5dSChristopher Siden 	 */
806*ad135b5dSChristopher Siden 	if (supported) {
807*ad135b5dSChristopher Siden 		int ret;
808*ad135b5dSChristopher Siden 		zfeature_info_t *fi;
809*ad135b5dSChristopher Siden 
810*ad135b5dSChristopher Siden 		ret = zfeature_lookup_name(feature, &fi);
811*ad135b5dSChristopher Siden 		if (ret != 0) {
812*ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
813*ad135b5dSChristopher Siden 			return (ENOTSUP);
814*ad135b5dSChristopher Siden 		}
815*ad135b5dSChristopher Siden 		feature = fi->fi_guid;
816*ad135b5dSChristopher Siden 	}
817*ad135b5dSChristopher Siden 
818*ad135b5dSChristopher Siden 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
819*ad135b5dSChristopher Siden 		found = B_TRUE;
820*ad135b5dSChristopher Siden 
821*ad135b5dSChristopher Siden 	if (supported) {
822*ad135b5dSChristopher Siden 		if (!found) {
823*ad135b5dSChristopher Siden 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
824*ad135b5dSChristopher Siden 		} else  {
825*ad135b5dSChristopher Siden 			if (refcount == 0)
826*ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
827*ad135b5dSChristopher Siden 			else
828*ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
829*ad135b5dSChristopher Siden 		}
830*ad135b5dSChristopher Siden 	} else {
831*ad135b5dSChristopher Siden 		if (found) {
832*ad135b5dSChristopher Siden 			if (refcount == 0) {
833*ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
834*ad135b5dSChristopher Siden 			} else {
835*ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
836*ad135b5dSChristopher Siden 			}
837*ad135b5dSChristopher Siden 		} else {
838*ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
839*ad135b5dSChristopher Siden 			return (ENOTSUP);
840*ad135b5dSChristopher Siden 		}
841*ad135b5dSChristopher Siden 	}
842*ad135b5dSChristopher Siden 
843*ad135b5dSChristopher Siden 	return (0);
844*ad135b5dSChristopher Siden }
845990b4856Slling 
846573ca77eSGeorge Wilson /*
847573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
848573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
849573ca77eSGeorge Wilson  */
850573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
851573ca77eSGeorge Wilson 
852fa9e4066Sahrens /*
853fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
854fa9e4066Sahrens  * 'buf'.
855fa9e4066Sahrens  */
856e7cbe64fSgw boolean_t
85799653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
858fa9e4066Sahrens {
859fa9e4066Sahrens 	namecheck_err_t why;
860fa9e4066Sahrens 	char what;
861b468a217Seschrock 	int ret;
862b468a217Seschrock 
863b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
864b468a217Seschrock 
865b468a217Seschrock 	/*
866b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
867b468a217Seschrock 	 * But we need to support users with existing pools that may now be
868b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
869b468a217Seschrock 	 * create (or import), and only in userland.
870b468a217Seschrock 	 */
871b468a217Seschrock 	if (ret == 0 && !isopen &&
872b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
873b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
8748654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
8758654d025Sperrin 	    strcmp(pool, "log") == 0)) {
876e7cbe64fSgw 		if (hdl != NULL)
877e7cbe64fSgw 			zfs_error_aux(hdl,
878e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
87999653d4eSeschrock 		return (B_FALSE);
880b468a217Seschrock 	}
881b468a217Seschrock 
882fa9e4066Sahrens 
883b468a217Seschrock 	if (ret != 0) {
88499653d4eSeschrock 		if (hdl != NULL) {
885fa9e4066Sahrens 			switch (why) {
886b81d61a6Slling 			case NAME_ERR_TOOLONG:
88799653d4eSeschrock 				zfs_error_aux(hdl,
888b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
889b81d61a6Slling 				break;
890b81d61a6Slling 
891fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
89299653d4eSeschrock 				zfs_error_aux(hdl,
893fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
894fa9e4066Sahrens 				    "'%c' in pool name"), what);
895fa9e4066Sahrens 				break;
896fa9e4066Sahrens 
897fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
89899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
89999653d4eSeschrock 				    "name must begin with a letter"));
900fa9e4066Sahrens 				break;
901fa9e4066Sahrens 
902fa9e4066Sahrens 			case NAME_ERR_RESERVED:
90399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90499653d4eSeschrock 				    "name is reserved"));
905fa9e4066Sahrens 				break;
906fa9e4066Sahrens 
907fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
90899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90999653d4eSeschrock 				    "pool name is reserved"));
910fa9e4066Sahrens 				break;
9115ad82045Snd 
9125ad82045Snd 			case NAME_ERR_LEADING_SLASH:
9135ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9145ad82045Snd 				    "leading slash in name"));
9155ad82045Snd 				break;
9165ad82045Snd 
9175ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
9185ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9195ad82045Snd 				    "empty component in name"));
9205ad82045Snd 				break;
9215ad82045Snd 
9225ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
9235ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9245ad82045Snd 				    "trailing slash in name"));
9255ad82045Snd 				break;
9265ad82045Snd 
9275ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
9285ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9295ad82045Snd 				    "multiple '@' delimiters in name"));
9305ad82045Snd 				break;
9315ad82045Snd 
932fa9e4066Sahrens 			}
933fa9e4066Sahrens 		}
93499653d4eSeschrock 		return (B_FALSE);
935fa9e4066Sahrens 	}
936fa9e4066Sahrens 
93799653d4eSeschrock 	return (B_TRUE);
938fa9e4066Sahrens }
939fa9e4066Sahrens 
940fa9e4066Sahrens /*
941fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
942fa9e4066Sahrens  * state.
943fa9e4066Sahrens  */
944fa9e4066Sahrens zpool_handle_t *
94599653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
946fa9e4066Sahrens {
947fa9e4066Sahrens 	zpool_handle_t *zhp;
94894de1d4cSeschrock 	boolean_t missing;
949fa9e4066Sahrens 
950fa9e4066Sahrens 	/*
951fa9e4066Sahrens 	 * Make sure the pool name is valid.
952fa9e4066Sahrens 	 */
95399653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
954ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
95599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
95699653d4eSeschrock 		    pool);
957fa9e4066Sahrens 		return (NULL);
958fa9e4066Sahrens 	}
959fa9e4066Sahrens 
96099653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
96199653d4eSeschrock 		return (NULL);
962fa9e4066Sahrens 
96399653d4eSeschrock 	zhp->zpool_hdl = hdl;
964fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
965fa9e4066Sahrens 
96694de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
96794de1d4cSeschrock 		zpool_close(zhp);
96894de1d4cSeschrock 		return (NULL);
96994de1d4cSeschrock 	}
97094de1d4cSeschrock 
97194de1d4cSeschrock 	if (missing) {
972990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
973ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
974990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
97594de1d4cSeschrock 		zpool_close(zhp);
97694de1d4cSeschrock 		return (NULL);
977fa9e4066Sahrens 	}
978fa9e4066Sahrens 
979fa9e4066Sahrens 	return (zhp);
980fa9e4066Sahrens }
981fa9e4066Sahrens 
982fa9e4066Sahrens /*
983fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
984fa9e4066Sahrens  * the configuration cache may be out of date).
985fa9e4066Sahrens  */
98694de1d4cSeschrock int
98794de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
988fa9e4066Sahrens {
989fa9e4066Sahrens 	zpool_handle_t *zhp;
99094de1d4cSeschrock 	boolean_t missing;
991fa9e4066Sahrens 
99294de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
99394de1d4cSeschrock 		return (-1);
994fa9e4066Sahrens 
99599653d4eSeschrock 	zhp->zpool_hdl = hdl;
996fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
997fa9e4066Sahrens 
99894de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
99994de1d4cSeschrock 		zpool_close(zhp);
100094de1d4cSeschrock 		return (-1);
1001fa9e4066Sahrens 	}
1002fa9e4066Sahrens 
100394de1d4cSeschrock 	if (missing) {
100494de1d4cSeschrock 		zpool_close(zhp);
100594de1d4cSeschrock 		*ret = NULL;
100694de1d4cSeschrock 		return (0);
100794de1d4cSeschrock 	}
100894de1d4cSeschrock 
100994de1d4cSeschrock 	*ret = zhp;
101094de1d4cSeschrock 	return (0);
1011fa9e4066Sahrens }
1012fa9e4066Sahrens 
1013fa9e4066Sahrens /*
1014fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1015fa9e4066Sahrens  * state.
1016fa9e4066Sahrens  */
1017fa9e4066Sahrens zpool_handle_t *
101899653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1019fa9e4066Sahrens {
1020fa9e4066Sahrens 	zpool_handle_t *zhp;
1021fa9e4066Sahrens 
102299653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1023fa9e4066Sahrens 		return (NULL);
1024fa9e4066Sahrens 
1025fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1026ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
102799653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1028fa9e4066Sahrens 		zpool_close(zhp);
1029fa9e4066Sahrens 		return (NULL);
1030fa9e4066Sahrens 	}
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 	return (zhp);
1033fa9e4066Sahrens }
1034fa9e4066Sahrens 
1035fa9e4066Sahrens /*
1036fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
1037fa9e4066Sahrens  */
1038fa9e4066Sahrens void
1039fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1040fa9e4066Sahrens {
1041fa9e4066Sahrens 	if (zhp->zpool_config)
1042fa9e4066Sahrens 		nvlist_free(zhp->zpool_config);
1043088e9d47Seschrock 	if (zhp->zpool_old_config)
1044088e9d47Seschrock 		nvlist_free(zhp->zpool_old_config);
1045b1b8ab34Slling 	if (zhp->zpool_props)
1046b1b8ab34Slling 		nvlist_free(zhp->zpool_props);
1047fa9e4066Sahrens 	free(zhp);
1048fa9e4066Sahrens }
1049fa9e4066Sahrens 
1050fa9e4066Sahrens /*
1051fa9e4066Sahrens  * Return the name of the pool.
1052fa9e4066Sahrens  */
1053fa9e4066Sahrens const char *
1054fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1055fa9e4066Sahrens {
1056fa9e4066Sahrens 	return (zhp->zpool_name);
1057fa9e4066Sahrens }
1058fa9e4066Sahrens 
1059fa9e4066Sahrens 
1060fa9e4066Sahrens /*
1061fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1062fa9e4066Sahrens  */
1063fa9e4066Sahrens int
1064fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1065fa9e4066Sahrens {
1066fa9e4066Sahrens 	return (zhp->zpool_state);
1067fa9e4066Sahrens }
1068fa9e4066Sahrens 
1069fa9e4066Sahrens /*
1070fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
1071fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
1072fa9e4066Sahrens  * don't have to worry about error semantics.
1073fa9e4066Sahrens  */
1074fa9e4066Sahrens int
107599653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
10760a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
1077fa9e4066Sahrens {
1078fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
10790a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
10800a48a24eStimh 	nvlist_t *zc_props = NULL;
108199653d4eSeschrock 	char msg[1024];
1082990b4856Slling 	char *altroot;
10830a48a24eStimh 	int ret = -1;
1084fa9e4066Sahrens 
108599653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
108699653d4eSeschrock 	    "cannot create '%s'"), pool);
1087fa9e4066Sahrens 
108899653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
108999653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1090fa9e4066Sahrens 
1091351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1092990b4856Slling 		return (-1);
1093fa9e4066Sahrens 
10940a48a24eStimh 	if (props) {
1095f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1096f9af39baSGeorge Wilson 
10970a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1098f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
10990a48a24eStimh 			goto create_failed;
11000a48a24eStimh 		}
11010a48a24eStimh 	}
110299653d4eSeschrock 
11030a48a24eStimh 	if (fsprops) {
11040a48a24eStimh 		uint64_t zoned;
11050a48a24eStimh 		char *zonestr;
11060a48a24eStimh 
11070a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
11080a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
11090a48a24eStimh 		    strcmp(zonestr, "on") == 0);
11100a48a24eStimh 
11110a48a24eStimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
11120a48a24eStimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
11130a48a24eStimh 			goto create_failed;
11140a48a24eStimh 		}
11150a48a24eStimh 		if (!zc_props &&
11160a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
11170a48a24eStimh 			goto create_failed;
11180a48a24eStimh 		}
11190a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
11200a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
11210a48a24eStimh 			goto create_failed;
11220a48a24eStimh 		}
1123351420b3Slling 	}
1124fa9e4066Sahrens 
11250a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
11260a48a24eStimh 		goto create_failed;
11270a48a24eStimh 
1128990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1129fa9e4066Sahrens 
11300a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1131351420b3Slling 
1132e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
11330a48a24eStimh 		nvlist_free(zc_props);
11340a48a24eStimh 		nvlist_free(zc_fsprops);
1135fa9e4066Sahrens 
113699653d4eSeschrock 		switch (errno) {
1137fa9e4066Sahrens 		case EBUSY:
1138fa9e4066Sahrens 			/*
1139fa9e4066Sahrens 			 * This can happen if the user has specified the same
1140fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1141fa9e4066Sahrens 			 * until we try to add it and see we already have a
1142fa9e4066Sahrens 			 * label.
1143fa9e4066Sahrens 			 */
114499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114599653d4eSeschrock 			    "one or more vdevs refer to the same device"));
114699653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1147fa9e4066Sahrens 
1148fa9e4066Sahrens 		case EOVERFLOW:
1149fa9e4066Sahrens 			/*
115099653d4eSeschrock 			 * This occurs when one of the devices is below
1151fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1152fa9e4066Sahrens 			 * device was the problem device since there's no
1153fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1154fa9e4066Sahrens 			 */
1155fa9e4066Sahrens 			{
1156fa9e4066Sahrens 				char buf[64];
1157fa9e4066Sahrens 
1158fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1159fa9e4066Sahrens 
116099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116199653d4eSeschrock 				    "one or more devices is less than the "
116299653d4eSeschrock 				    "minimum size (%s)"), buf);
1163fa9e4066Sahrens 			}
116499653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1165fa9e4066Sahrens 
1166fa9e4066Sahrens 		case ENOSPC:
116799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116899653d4eSeschrock 			    "one or more devices is out of space"));
116999653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1170fa9e4066Sahrens 
1171fa94a07fSbrendan 		case ENOTBLK:
1172fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1173fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1174fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1175fa94a07fSbrendan 
1176fa9e4066Sahrens 		default:
117799653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1178fa9e4066Sahrens 		}
1179fa9e4066Sahrens 	}
1180fa9e4066Sahrens 
1181fa9e4066Sahrens 	/*
1182fa9e4066Sahrens 	 * If this is an alternate root pool, then we automatically set the
1183e9dbad6fSeschrock 	 * mountpoint of the root dataset to be '/'.
1184fa9e4066Sahrens 	 */
1185990b4856Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1186990b4856Slling 	    &altroot) == 0) {
1187fa9e4066Sahrens 		zfs_handle_t *zhp;
1188fa9e4066Sahrens 
1189990b4856Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1190e9dbad6fSeschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1191e9dbad6fSeschrock 		    "/") == 0);
1192fa9e4066Sahrens 
1193fa9e4066Sahrens 		zfs_close(zhp);
1194fa9e4066Sahrens 	}
1195fa9e4066Sahrens 
11960a48a24eStimh create_failed:
1197351420b3Slling 	zcmd_free_nvlists(&zc);
11980a48a24eStimh 	nvlist_free(zc_props);
11990a48a24eStimh 	nvlist_free(zc_fsprops);
12000a48a24eStimh 	return (ret);
1201fa9e4066Sahrens }
1202fa9e4066Sahrens 
1203fa9e4066Sahrens /*
1204fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1205fa9e4066Sahrens  * datasets left in the pool.
1206fa9e4066Sahrens  */
1207fa9e4066Sahrens int
1208fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp)
1209fa9e4066Sahrens {
1210fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1211fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
121299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
121399653d4eSeschrock 	char msg[1024];
1214fa9e4066Sahrens 
1215fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1216cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1217fa9e4066Sahrens 		return (-1);
1218fa9e4066Sahrens 
1219fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1220fa9e4066Sahrens 
1221cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
122299653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
122399653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1224fa9e4066Sahrens 
122599653d4eSeschrock 		if (errno == EROFS) {
122699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
122799653d4eSeschrock 			    "one or more devices is read only"));
122899653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
122999653d4eSeschrock 		} else {
123099653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1231fa9e4066Sahrens 		}
1232fa9e4066Sahrens 
1233fa9e4066Sahrens 		if (zfp)
1234fa9e4066Sahrens 			zfs_close(zfp);
1235fa9e4066Sahrens 		return (-1);
1236fa9e4066Sahrens 	}
1237fa9e4066Sahrens 
1238fa9e4066Sahrens 	if (zfp) {
1239fa9e4066Sahrens 		remove_mountpoint(zfp);
1240fa9e4066Sahrens 		zfs_close(zfp);
1241fa9e4066Sahrens 	}
1242fa9e4066Sahrens 
1243fa9e4066Sahrens 	return (0);
1244fa9e4066Sahrens }
1245fa9e4066Sahrens 
1246fa9e4066Sahrens /*
1247fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1248fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1249fa9e4066Sahrens  */
1250fa9e4066Sahrens int
1251fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1252fa9e4066Sahrens {
1253e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
125499653d4eSeschrock 	int ret;
125599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
125699653d4eSeschrock 	char msg[1024];
1257fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1258fa94a07fSbrendan 	uint_t nspares, nl2cache;
125999653d4eSeschrock 
126099653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
126199653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
126299653d4eSeschrock 
1263fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1264fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
126599653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
126699653d4eSeschrock 	    &spares, &nspares) == 0) {
126799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
126899653d4eSeschrock 		    "upgraded to add hot spares"));
126999653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
127099653d4eSeschrock 	}
1271fa9e4066Sahrens 
12724263d13fSGeorge Wilson 	if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1273b5b76fecSGeorge Wilson 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1274b5b76fecSGeorge Wilson 		uint64_t s;
1275b5b76fecSGeorge Wilson 
1276b5b76fecSGeorge Wilson 		for (s = 0; s < nspares; s++) {
1277b5b76fecSGeorge Wilson 			char *path;
1278b5b76fecSGeorge Wilson 
1279b5b76fecSGeorge Wilson 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1280b5b76fecSGeorge Wilson 			    &path) == 0 && pool_uses_efi(spares[s])) {
1281b5b76fecSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1282b5b76fecSGeorge Wilson 				    "device '%s' contains an EFI label and "
1283b5b76fecSGeorge Wilson 				    "cannot be used on root pools."),
128488ecc943SGeorge Wilson 				    zpool_vdev_name(hdl, NULL, spares[s],
128588ecc943SGeorge Wilson 				    B_FALSE));
1286b5b76fecSGeorge Wilson 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1287b5b76fecSGeorge Wilson 			}
1288b5b76fecSGeorge Wilson 		}
1289b5b76fecSGeorge Wilson 	}
1290b5b76fecSGeorge Wilson 
1291fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1292fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1293fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1294fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1295fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1296fa94a07fSbrendan 		    "upgraded to add cache devices"));
1297fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1298fa94a07fSbrendan 	}
1299fa94a07fSbrendan 
1300990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
130199653d4eSeschrock 		return (-1);
1302fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1303fa9e4066Sahrens 
1304cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1305fa9e4066Sahrens 		switch (errno) {
1306fa9e4066Sahrens 		case EBUSY:
1307fa9e4066Sahrens 			/*
1308fa9e4066Sahrens 			 * This can happen if the user has specified the same
1309fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1310fa9e4066Sahrens 			 * until we try to add it and see we already have a
1311fa9e4066Sahrens 			 * label.
1312fa9e4066Sahrens 			 */
131399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
131499653d4eSeschrock 			    "one or more vdevs refer to the same device"));
131599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1316fa9e4066Sahrens 			break;
1317fa9e4066Sahrens 
1318fa9e4066Sahrens 		case EOVERFLOW:
1319fa9e4066Sahrens 			/*
1320fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1321fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1322fa9e4066Sahrens 			 * device was the problem device since there's no
1323fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1324fa9e4066Sahrens 			 */
1325fa9e4066Sahrens 			{
1326fa9e4066Sahrens 				char buf[64];
1327fa9e4066Sahrens 
1328fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1329fa9e4066Sahrens 
133099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133199653d4eSeschrock 				    "device is less than the minimum "
133299653d4eSeschrock 				    "size (%s)"), buf);
1333fa9e4066Sahrens 			}
133499653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
133599653d4eSeschrock 			break;
133699653d4eSeschrock 
133799653d4eSeschrock 		case ENOTSUP:
133899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13398654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
134099653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1341fa9e4066Sahrens 			break;
1342fa9e4066Sahrens 
1343b1b8ab34Slling 		case EDOM:
1344b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13458654d025Sperrin 			    "root pool can not have multiple vdevs"
13468654d025Sperrin 			    " or separate logs"));
1347b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1348b1b8ab34Slling 			break;
1349b1b8ab34Slling 
1350fa94a07fSbrendan 		case ENOTBLK:
1351fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1353fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1354fa94a07fSbrendan 			break;
1355fa94a07fSbrendan 
1356fa9e4066Sahrens 		default:
135799653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1358fa9e4066Sahrens 		}
1359fa9e4066Sahrens 
136099653d4eSeschrock 		ret = -1;
136199653d4eSeschrock 	} else {
136299653d4eSeschrock 		ret = 0;
1363fa9e4066Sahrens 	}
1364fa9e4066Sahrens 
1365e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1366fa9e4066Sahrens 
136799653d4eSeschrock 	return (ret);
1368fa9e4066Sahrens }
1369fa9e4066Sahrens 
1370fa9e4066Sahrens /*
1371fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1372fa9e4066Sahrens  * mounted datasets in the pool.
1373fa9e4066Sahrens  */
1374fa9e4066Sahrens int
1375394ab0cbSGeorge Wilson zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1376fa9e4066Sahrens {
1377fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
137889a89ebfSlling 	char msg[1024];
1379fa9e4066Sahrens 
138089a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
138189a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
138289a89ebfSlling 
1383fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
138489a89ebfSlling 	zc.zc_cookie = force;
1385394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
138689a89ebfSlling 
138789a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
138889a89ebfSlling 		switch (errno) {
138989a89ebfSlling 		case EXDEV:
139089a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
139189a89ebfSlling 			    "use '-f' to override the following errors:\n"
139289a89ebfSlling 			    "'%s' has an active shared spare which could be"
139389a89ebfSlling 			    " used by other pools once '%s' is exported."),
139489a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
139589a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
139689a89ebfSlling 			    msg));
139789a89ebfSlling 		default:
139889a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
139989a89ebfSlling 			    msg));
140089a89ebfSlling 		}
140189a89ebfSlling 	}
1402fa9e4066Sahrens 
1403fa9e4066Sahrens 	return (0);
1404fa9e4066Sahrens }
1405fa9e4066Sahrens 
1406394ab0cbSGeorge Wilson int
1407394ab0cbSGeorge Wilson zpool_export(zpool_handle_t *zhp, boolean_t force)
1408394ab0cbSGeorge Wilson {
1409394ab0cbSGeorge Wilson 	return (zpool_export_common(zhp, force, B_FALSE));
1410394ab0cbSGeorge Wilson }
1411394ab0cbSGeorge Wilson 
1412394ab0cbSGeorge Wilson int
1413394ab0cbSGeorge Wilson zpool_export_force(zpool_handle_t *zhp)
1414394ab0cbSGeorge Wilson {
1415394ab0cbSGeorge Wilson 	return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1416394ab0cbSGeorge Wilson }
1417394ab0cbSGeorge Wilson 
1418468c413aSTim Haley static void
1419468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
14204b964adaSGeorge Wilson     nvlist_t *config)
1421468c413aSTim Haley {
14224b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1423468c413aSTim Haley 	uint64_t rewindto;
1424468c413aSTim Haley 	int64_t loss = -1;
1425468c413aSTim Haley 	struct tm t;
1426468c413aSTim Haley 	char timestr[128];
1427468c413aSTim Haley 
14284b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
14294b964adaSGeorge Wilson 		return;
14304b964adaSGeorge Wilson 
1431*ad135b5dSChristopher Siden 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1432*ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1433468c413aSTim Haley 		return;
1434*ad135b5dSChristopher Siden 	}
1435468c413aSTim Haley 
14364b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1437468c413aSTim Haley 		return;
14384b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1439468c413aSTim Haley 
1440468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1441468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1442468c413aSTim Haley 		if (dryrun) {
1443468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1444468c413aSTim Haley 			    "Would be able to return %s "
1445468c413aSTim Haley 			    "to its state as of %s.\n"),
1446468c413aSTim Haley 			    name, timestr);
1447468c413aSTim Haley 		} else {
1448468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1449468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1450468c413aSTim Haley 			    name, timestr);
1451468c413aSTim Haley 		}
1452468c413aSTim Haley 		if (loss > 120) {
1453468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1454468c413aSTim Haley 			    "%s approximately %lld "),
1455468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1456468c413aSTim Haley 			    (loss + 30) / 60);
1457468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1458468c413aSTim Haley 			    "minutes of transactions.\n"));
1459468c413aSTim Haley 		} else if (loss > 0) {
1460468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1461468c413aSTim Haley 			    "%s approximately %lld "),
1462468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1463468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1464468c413aSTim Haley 			    "seconds of transactions.\n"));
1465468c413aSTim Haley 		}
1466468c413aSTim Haley 	}
1467468c413aSTim Haley }
1468468c413aSTim Haley 
1469468c413aSTim Haley void
1470468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1471468c413aSTim Haley     nvlist_t *config)
1472468c413aSTim Haley {
14734b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1474468c413aSTim Haley 	int64_t loss = -1;
1475468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1476468c413aSTim Haley 	uint64_t rewindto;
1477468c413aSTim Haley 	struct tm t;
1478468c413aSTim Haley 	char timestr[128];
1479468c413aSTim Haley 
1480468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1481468c413aSTim Haley 		return;
1482468c413aSTim Haley 
1483468c413aSTim Haley 	if (reason >= 0)
1484468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1485468c413aSTim Haley 	else
1486468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1487468c413aSTim Haley 
1488468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
14894b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1490*ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
14914b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1492468c413aSTim Haley 		goto no_info;
1493468c413aSTim Haley 
14944b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
14954b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1496468c413aSTim Haley 	    &edata);
1497468c413aSTim Haley 
1498468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1499468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1500468c413aSTim Haley 
1501468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1502468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1503468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1504468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1505468c413aSTim Haley 		    "\tshould correct the problem.  "),
1506468c413aSTim Haley 		    timestr);
1507468c413aSTim Haley 	} else {
1508468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1509468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1510468c413aSTim Haley 		    "should correct the problem.\n\t"));
1511468c413aSTim Haley 	}
1512468c413aSTim Haley 
1513468c413aSTim Haley 	if (loss > 120) {
1514468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1515468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1516468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1517468c413aSTim Haley 	} else if (loss > 0) {
1518468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1519468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1520468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1521468c413aSTim Haley 	}
1522468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1523468c413aSTim Haley 		if (edata == 1) {
1524468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1525468c413aSTim Haley 			    "After rewind, at least\n"
1526468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1527468c413aSTim Haley 		} else {
1528468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1529468c413aSTim Haley 			    "After rewind, several\n"
1530468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1531468c413aSTim Haley 		}
1532468c413aSTim Haley 	}
1533468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1534a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1535a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1536468c413aSTim Haley 
1537468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1538468c413aSTim Haley 	    "A scrub of the pool\n"
1539468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1540468c413aSTim Haley 	return;
1541468c413aSTim Haley 
1542468c413aSTim Haley no_info:
1543468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1544468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1545468c413aSTim Haley }
1546468c413aSTim Haley 
1547fa9e4066Sahrens /*
1548990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1549990b4856Slling  * if possible.
1550990b4856Slling  *
1551990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1552990b4856Slling  * new properties value to be set.
1553fa9e4066Sahrens  */
1554fa9e4066Sahrens int
155599653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1556990b4856Slling     char *altroot)
1557990b4856Slling {
1558990b4856Slling 	nvlist_t *props = NULL;
1559990b4856Slling 	int ret;
1560990b4856Slling 
1561990b4856Slling 	if (altroot != NULL) {
1562990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1563990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1564990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1565990b4856Slling 			    newname));
1566990b4856Slling 		}
1567990b4856Slling 
1568990b4856Slling 		if (nvlist_add_string(props,
1569352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1570352d8027SGeorge Wilson 		    nvlist_add_string(props,
1571352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1572990b4856Slling 			nvlist_free(props);
1573990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1574990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1575990b4856Slling 			    newname));
1576990b4856Slling 		}
1577990b4856Slling 	}
1578990b4856Slling 
15794b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
15804b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1581990b4856Slling 	if (props)
1582990b4856Slling 		nvlist_free(props);
1583990b4856Slling 	return (ret);
1584990b4856Slling }
1585990b4856Slling 
15864b964adaSGeorge Wilson static void
15874b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
15884b964adaSGeorge Wilson     int indent)
15894b964adaSGeorge Wilson {
15904b964adaSGeorge Wilson 	nvlist_t **child;
15914b964adaSGeorge Wilson 	uint_t c, children;
15924b964adaSGeorge Wilson 	char *vname;
15934b964adaSGeorge Wilson 	uint64_t is_log = 0;
15944b964adaSGeorge Wilson 
15954b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
15964b964adaSGeorge Wilson 	    &is_log);
15974b964adaSGeorge Wilson 
15984b964adaSGeorge Wilson 	if (name != NULL)
15994b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
16004b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
16014b964adaSGeorge Wilson 
16024b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
16034b964adaSGeorge Wilson 	    &child, &children) != 0)
16044b964adaSGeorge Wilson 		return;
16054b964adaSGeorge Wilson 
16064b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
16074b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
16084b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
16094b964adaSGeorge Wilson 		free(vname);
16104b964adaSGeorge Wilson 	}
16114b964adaSGeorge Wilson }
16124b964adaSGeorge Wilson 
1613*ad135b5dSChristopher Siden void
1614*ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1615*ad135b5dSChristopher Siden {
1616*ad135b5dSChristopher Siden 	nvlist_t *nvinfo, *unsup_feat;
1617*ad135b5dSChristopher Siden 
1618*ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1619*ad135b5dSChristopher Siden 	    0);
1620*ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1621*ad135b5dSChristopher Siden 	    &unsup_feat) == 0);
1622*ad135b5dSChristopher Siden 
1623*ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1624*ad135b5dSChristopher Siden 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1625*ad135b5dSChristopher Siden 		char *desc;
1626*ad135b5dSChristopher Siden 
1627*ad135b5dSChristopher Siden 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1628*ad135b5dSChristopher Siden 		verify(nvpair_value_string(nvp, &desc) == 0);
1629*ad135b5dSChristopher Siden 
1630*ad135b5dSChristopher Siden 		if (strlen(desc) > 0)
1631*ad135b5dSChristopher Siden 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1632*ad135b5dSChristopher Siden 		else
1633*ad135b5dSChristopher Siden 			(void) printf("\t%s\n", nvpair_name(nvp));
1634*ad135b5dSChristopher Siden 	}
1635*ad135b5dSChristopher Siden }
1636*ad135b5dSChristopher Siden 
1637990b4856Slling /*
1638990b4856Slling  * Import the given pool using the known configuration and a list of
1639990b4856Slling  * properties to be set. The configuration should have come from
1640990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1641990b4856Slling  * is imported with a different name.
1642990b4856Slling  */
1643990b4856Slling int
1644990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
16454b964adaSGeorge Wilson     nvlist_t *props, int flags)
1646fa9e4066Sahrens {
1647e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1648468c413aSTim Haley 	zpool_rewind_policy_t policy;
16494b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
16504b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
16514b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1652fa9e4066Sahrens 	char *thename;
1653fa9e4066Sahrens 	char *origname;
1654fa9e4066Sahrens 	int ret;
16554b964adaSGeorge Wilson 	int error = 0;
1656990b4856Slling 	char errbuf[1024];
1657fa9e4066Sahrens 
1658fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1659fa9e4066Sahrens 	    &origname) == 0);
1660fa9e4066Sahrens 
1661990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1662990b4856Slling 	    "cannot import pool '%s'"), origname);
1663990b4856Slling 
1664fa9e4066Sahrens 	if (newname != NULL) {
166599653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1666ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
166799653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
166899653d4eSeschrock 			    newname));
1669fa9e4066Sahrens 		thename = (char *)newname;
1670fa9e4066Sahrens 	} else {
1671fa9e4066Sahrens 		thename = origname;
1672fa9e4066Sahrens 	}
1673fa9e4066Sahrens 
1674990b4856Slling 	if (props) {
1675990b4856Slling 		uint64_t version;
1676f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1677fa9e4066Sahrens 
1678990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1679990b4856Slling 		    &version) == 0);
1680fa9e4066Sahrens 
16810a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1682f9af39baSGeorge Wilson 		    props, version, flags, errbuf)) == NULL) {
1683990b4856Slling 			return (-1);
1684351420b3Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1685351420b3Slling 			nvlist_free(props);
1686990b4856Slling 			return (-1);
1687351420b3Slling 		}
1688990b4856Slling 	}
1689990b4856Slling 
1690990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1691fa9e4066Sahrens 
1692fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1693ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1694fa9e4066Sahrens 
1695351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1696351420b3Slling 		nvlist_free(props);
169799653d4eSeschrock 		return (-1);
1698351420b3Slling 	}
169957f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1700468c413aSTim Haley 		nvlist_free(props);
1701468c413aSTim Haley 		return (-1);
1702468c413aSTim Haley 	}
1703fa9e4066Sahrens 
17044b964adaSGeorge Wilson 	zc.zc_cookie = flags;
17054b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
17064b964adaSGeorge Wilson 	    errno == ENOMEM) {
17074b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
17084b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
17094b964adaSGeorge Wilson 			return (-1);
17104b964adaSGeorge Wilson 		}
17114b964adaSGeorge Wilson 	}
17124b964adaSGeorge Wilson 	if (ret != 0)
17134b964adaSGeorge Wilson 		error = errno;
17144b964adaSGeorge Wilson 
17154b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
17164b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
17174b964adaSGeorge Wilson 
17184b964adaSGeorge Wilson 	if (error) {
1719fa9e4066Sahrens 		char desc[1024];
1720468c413aSTim Haley 
1721468c413aSTim Haley 		/*
1722468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1723468c413aSTim Haley 		 * looks like if we found a best txg
1724468c413aSTim Haley 		 */
17254b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1726468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
17274b964adaSGeorge Wilson 			    B_TRUE, nv);
17284b964adaSGeorge Wilson 			nvlist_free(nv);
1729468c413aSTim Haley 			return (-1);
1730468c413aSTim Haley 		}
1731468c413aSTim Haley 
1732fa9e4066Sahrens 		if (newname == NULL)
1733fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1734fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1735fa9e4066Sahrens 			    thename);
1736fa9e4066Sahrens 		else
1737fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1738fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1739fa9e4066Sahrens 			    origname, thename);
1740fa9e4066Sahrens 
17414b964adaSGeorge Wilson 		switch (error) {
1742ea8dc4b6Seschrock 		case ENOTSUP:
1743*ad135b5dSChristopher Siden 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1744*ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1745*ad135b5dSChristopher Siden 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1746*ad135b5dSChristopher Siden 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1747*ad135b5dSChristopher Siden 				    "pool uses the following feature(s) not "
1748*ad135b5dSChristopher Siden 				    "supported by this system:\n"));
1749*ad135b5dSChristopher Siden 				zpool_print_unsup_feat(nv);
1750*ad135b5dSChristopher Siden 				if (nvlist_exists(nvinfo,
1751*ad135b5dSChristopher Siden 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1752*ad135b5dSChristopher Siden 					(void) printf(dgettext(TEXT_DOMAIN,
1753*ad135b5dSChristopher Siden 					    "All unsupported features are only "
1754*ad135b5dSChristopher Siden 					    "required for writing to the pool."
1755*ad135b5dSChristopher Siden 					    "\nThe pool can be imported using "
1756*ad135b5dSChristopher Siden 					    "'-o readonly=on'.\n"));
1757*ad135b5dSChristopher Siden 				}
1758*ad135b5dSChristopher Siden 			}
1759ea8dc4b6Seschrock 			/*
1760ea8dc4b6Seschrock 			 * Unsupported version.
1761ea8dc4b6Seschrock 			 */
176299653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1763ea8dc4b6Seschrock 			break;
1764ea8dc4b6Seschrock 
1765b5989ec7Seschrock 		case EINVAL:
1766b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1767b5989ec7Seschrock 			break;
1768b5989ec7Seschrock 
176954a91118SChris Kirby 		case EROFS:
177054a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177154a91118SChris Kirby 			    "one or more devices is read only"));
177254a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
177354a91118SChris Kirby 			break;
177454a91118SChris Kirby 
17754b964adaSGeorge Wilson 		case ENXIO:
17764b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
17774b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
17784b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
17794b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
17804b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
17814b964adaSGeorge Wilson 				    "The devices below are missing, use "
17824b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
17834b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
17844b964adaSGeorge Wilson 				(void) printf("\n");
17854b964adaSGeorge Wilson 			}
17864b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17874b964adaSGeorge Wilson 			break;
17884b964adaSGeorge Wilson 
17894b964adaSGeorge Wilson 		case EEXIST:
17904b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17914b964adaSGeorge Wilson 			break;
17924b964adaSGeorge Wilson 
1793fa9e4066Sahrens 		default:
17944b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1795468c413aSTim Haley 			zpool_explain_recover(hdl,
17964b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1797468c413aSTim Haley 			break;
1798fa9e4066Sahrens 		}
1799fa9e4066Sahrens 
18004b964adaSGeorge Wilson 		nvlist_free(nv);
1801fa9e4066Sahrens 		ret = -1;
1802fa9e4066Sahrens 	} else {
1803fa9e4066Sahrens 		zpool_handle_t *zhp;
1804ecd6cf80Smarks 
1805fa9e4066Sahrens 		/*
1806fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1807fa9e4066Sahrens 		 */
1808681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
180994de1d4cSeschrock 			ret = -1;
1810681d9761SEric Taylor 		else if (zhp != NULL)
1811fa9e4066Sahrens 			zpool_close(zhp);
1812468c413aSTim Haley 		if (policy.zrp_request &
1813468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1814468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
18154b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1816468c413aSTim Haley 		}
18174b964adaSGeorge Wilson 		nvlist_free(nv);
1818468c413aSTim Haley 		return (0);
1819fa9e4066Sahrens 	}
1820fa9e4066Sahrens 
1821e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1822351420b3Slling 	nvlist_free(props);
1823351420b3Slling 
1824fa9e4066Sahrens 	return (ret);
1825fa9e4066Sahrens }
1826fa9e4066Sahrens 
1827fa9e4066Sahrens /*
18283f9d6ad7SLin Ling  * Scan the pool.
1829fa9e4066Sahrens  */
1830fa9e4066Sahrens int
18313f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1832fa9e4066Sahrens {
1833fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1834fa9e4066Sahrens 	char msg[1024];
183599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1836fa9e4066Sahrens 
1837fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18383f9d6ad7SLin Ling 	zc.zc_cookie = func;
1839fa9e4066Sahrens 
1840cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
18413f9d6ad7SLin Ling 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1842fa9e4066Sahrens 		return (0);
1843fa9e4066Sahrens 
18443f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
18453f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18463f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
18473f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
18483f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18493f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
18503f9d6ad7SLin Ling 		    zc.zc_name);
18513f9d6ad7SLin Ling 	} else {
18523f9d6ad7SLin Ling 		assert(!"unexpected result");
18533f9d6ad7SLin Ling 	}
1854fa9e4066Sahrens 
18553f9d6ad7SLin Ling 	if (errno == EBUSY) {
18563f9d6ad7SLin Ling 		nvlist_t *nvroot;
18573f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
18583f9d6ad7SLin Ling 		uint_t psc;
18593f9d6ad7SLin Ling 
18603f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
18613f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
18623f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
18633f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
18643f9d6ad7SLin Ling 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
18653f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
18663f9d6ad7SLin Ling 		else
18673f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
18683f9d6ad7SLin Ling 	} else if (errno == ENOENT) {
18693f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
18703f9d6ad7SLin Ling 	} else {
187199653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
18723f9d6ad7SLin Ling 	}
1873fa9e4066Sahrens }
1874fa9e4066Sahrens 
18753fdda499SJohn Harres /*
18763fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
18773fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
18783fdda499SJohn Harres  * verification of the s# part.
18793fdda499SJohn Harres  */
18803fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
18813fdda499SJohn Harres 
18823fdda499SJohn Harres /*
18833fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
18843fdda499SJohn Harres  * and the like.
18853fdda499SJohn Harres  */
18863fdda499SJohn Harres static int
18873fdda499SJohn Harres ctd_check_path(char *str) {
18883fdda499SJohn Harres 	/*
18893fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
18903fdda499SJohn Harres 	 */
18913fdda499SJohn Harres 	if (str && str[0] == '/') {
18923fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
18933fdda499SJohn Harres 
18943fdda499SJohn Harres 		/*
18953fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
18963fdda499SJohn Harres 		 * component of the string instead.
18973fdda499SJohn Harres 		 */
18983fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
18993fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
19003fdda499SJohn Harres 				;
19013fdda499SJohn Harres 		}
19023fdda499SJohn Harres 		str = tmp + 1;
19033fdda499SJohn Harres 	}
19043fdda499SJohn Harres 	return (CTD_CHECK(str));
19053fdda499SJohn Harres }
19063fdda499SJohn Harres 
1907a43d325bSek /*
1908573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1909573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1910a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1911a43d325bSek  * spare; but FALSE if its an INUSE spare.
1912a43d325bSek  */
191399653d4eSeschrock static nvlist_t *
1914573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1915573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1916ea8dc4b6Seschrock {
1917ea8dc4b6Seschrock 	uint_t c, children;
1918ea8dc4b6Seschrock 	nvlist_t **child;
191999653d4eSeschrock 	nvlist_t *ret;
1920ee0eb9f2SEric Schrock 	uint64_t is_log;
1921573ca77eSGeorge Wilson 	char *srchkey;
1922573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1923573ca77eSGeorge Wilson 
1924573ca77eSGeorge Wilson 	/* Nothing to look for */
1925573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1926573ca77eSGeorge Wilson 		return (NULL);
1927ea8dc4b6Seschrock 
1928573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1929573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1930573ca77eSGeorge Wilson 
1931573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1932cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1933573ca77eSGeorge Wilson 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1934cb04b873SMark J Musante 			uint64_t srchval, theguid;
1935cb04b873SMark J Musante 
1936cb04b873SMark J Musante 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1937cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1938cb04b873SMark J Musante 			    &theguid) == 0);
1939cb04b873SMark J Musante 			if (theguid == srchval)
1940cb04b873SMark J Musante 				return (nv);
1941573ca77eSGeorge Wilson 		}
1942573ca77eSGeorge Wilson 		break;
1943573ca77eSGeorge Wilson 
1944573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1945573ca77eSGeorge Wilson 		char *srchval, *val;
1946573ca77eSGeorge Wilson 
1947573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
1948573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1949573ca77eSGeorge Wilson 			break;
1950ea8dc4b6Seschrock 
1951ea8dc4b6Seschrock 		/*
19523fdda499SJohn Harres 		 * Search for the requested value. Special cases:
19533fdda499SJohn Harres 		 *
19543fdda499SJohn Harres 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
19553fdda499SJohn Harres 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
19563fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
19573fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
19583fdda499SJohn Harres 		 *
195988ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
1960ea8dc4b6Seschrock 		 */
19613fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
19623fdda499SJohn Harres 		    ctd_check_path(val)) {
1963573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
1964573ca77eSGeorge Wilson 
1965573ca77eSGeorge Wilson 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1966573ca77eSGeorge Wilson 			    &wholedisk);
1967573ca77eSGeorge Wilson 			if (wholedisk) {
19683fdda499SJohn Harres 				int slen = strlen(srchval);
19693fdda499SJohn Harres 				int vlen = strlen(val);
19703fdda499SJohn Harres 
19713fdda499SJohn Harres 				if (slen != vlen - 2)
19723fdda499SJohn Harres 					break;
19733fdda499SJohn Harres 
19743fdda499SJohn Harres 				/*
19753fdda499SJohn Harres 				 * make_leaf_vdev() should only set
19763fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
19773fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
19783fdda499SJohn Harres 				 * room for the indices used next.
19793fdda499SJohn Harres 				 */
19803fdda499SJohn Harres 				ASSERT(vlen >= 6);
19813fdda499SJohn Harres 
19823fdda499SJohn Harres 				/*
19833fdda499SJohn Harres 				 * strings identical except trailing "s0"
19843fdda499SJohn Harres 				 */
19853fdda499SJohn Harres 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
19863fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
19873fdda499SJohn Harres 					return (nv);
19883fdda499SJohn Harres 
1989573ca77eSGeorge Wilson 				/*
19903fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
1991573ca77eSGeorge Wilson 				 */
19923fdda499SJohn Harres 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
19933fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
19943fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
1995573ca77eSGeorge Wilson 					return (nv);
19963fdda499SJohn Harres 
1997573ca77eSGeorge Wilson 				break;
1998573ca77eSGeorge Wilson 			}
199988ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
200088ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
200188ecc943SGeorge Wilson 			uint64_t id, vdev_id;
200288ecc943SGeorge Wilson 
200388ecc943SGeorge Wilson 			/*
200488ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
200588ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
200688ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
200788ecc943SGeorge Wilson 			 */
200888ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
200988ecc943SGeorge Wilson 				return (NULL);
201088ecc943SGeorge Wilson 
201188ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
201288ecc943SGeorge Wilson 				free(type);
201388ecc943SGeorge Wilson 				break;
201488ecc943SGeorge Wilson 			}
201588ecc943SGeorge Wilson 			idx = p + 1;
201688ecc943SGeorge Wilson 			*p = '\0';
201788ecc943SGeorge Wilson 
201888ecc943SGeorge Wilson 			/*
201988ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
202088ecc943SGeorge Wilson 			 */
202188ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
202288ecc943SGeorge Wilson 				free(type);
202388ecc943SGeorge Wilson 				break;
202488ecc943SGeorge Wilson 			}
202588ecc943SGeorge Wilson 
202688ecc943SGeorge Wilson 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
202788ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
202888ecc943SGeorge Wilson 			    strncmp(type, VDEV_TYPE_MIRROR,
202988ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_MIRROR)) == 0);
203088ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
203188ecc943SGeorge Wilson 			    &id) == 0);
203288ecc943SGeorge Wilson 
203388ecc943SGeorge Wilson 			errno = 0;
203488ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
203588ecc943SGeorge Wilson 
203688ecc943SGeorge Wilson 			free(type);
203788ecc943SGeorge Wilson 			if (errno != 0)
203888ecc943SGeorge Wilson 				return (NULL);
203988ecc943SGeorge Wilson 
204088ecc943SGeorge Wilson 			/*
204188ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
204288ecc943SGeorge Wilson 			 */
204388ecc943SGeorge Wilson 			if (vdev_id == id)
204488ecc943SGeorge Wilson 				return (nv);
2045ea8dc4b6Seschrock 		}
2046573ca77eSGeorge Wilson 
2047573ca77eSGeorge Wilson 		/*
2048573ca77eSGeorge Wilson 		 * Common case
2049573ca77eSGeorge Wilson 		 */
2050573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
2051573ca77eSGeorge Wilson 			return (nv);
2052573ca77eSGeorge Wilson 		break;
2053573ca77eSGeorge Wilson 	}
2054573ca77eSGeorge Wilson 
2055573ca77eSGeorge Wilson 	default:
2056573ca77eSGeorge Wilson 		break;
2057ea8dc4b6Seschrock 	}
2058ea8dc4b6Seschrock 
2059ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2060ea8dc4b6Seschrock 	    &child, &children) != 0)
206199653d4eSeschrock 		return (NULL);
2062ea8dc4b6Seschrock 
2063ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
2064573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2065ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
2066ee0eb9f2SEric Schrock 			/*
2067ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
2068ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
2069ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
2070ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
2071ee0eb9f2SEric Schrock 			 */
2072ee0eb9f2SEric Schrock 			if (log != NULL &&
2073ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
2074ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2075ee0eb9f2SEric Schrock 			    is_log) {
2076ee0eb9f2SEric Schrock 				*log = B_TRUE;
2077ee0eb9f2SEric Schrock 			}
2078ea8dc4b6Seschrock 			return (ret);
2079ee0eb9f2SEric Schrock 		}
2080ee0eb9f2SEric Schrock 	}
2081ea8dc4b6Seschrock 
208299653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
208399653d4eSeschrock 	    &child, &children) == 0) {
208499653d4eSeschrock 		for (c = 0; c < children; c++) {
2085573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2086ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2087a43d325bSek 				*avail_spare = B_TRUE;
208899653d4eSeschrock 				return (ret);
208999653d4eSeschrock 			}
209099653d4eSeschrock 		}
209199653d4eSeschrock 	}
209299653d4eSeschrock 
2093fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2094fa94a07fSbrendan 	    &child, &children) == 0) {
2095fa94a07fSbrendan 		for (c = 0; c < children; c++) {
2096573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2097ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2098fa94a07fSbrendan 				*l2cache = B_TRUE;
2099fa94a07fSbrendan 				return (ret);
2100fa94a07fSbrendan 			}
2101fa94a07fSbrendan 		}
2102fa94a07fSbrendan 	}
2103fa94a07fSbrendan 
210499653d4eSeschrock 	return (NULL);
2105ea8dc4b6Seschrock }
2106ea8dc4b6Seschrock 
2107573ca77eSGeorge Wilson /*
2108573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
2109573ca77eSGeorge Wilson  * associated vdev.
2110573ca77eSGeorge Wilson  */
2111573ca77eSGeorge Wilson nvlist_t *
2112573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2113573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2114573ca77eSGeorge Wilson {
2115573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
2116573ca77eSGeorge Wilson 
2117573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2118573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2119573ca77eSGeorge Wilson 
2120573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2121573ca77eSGeorge Wilson 	    &nvroot) == 0);
2122573ca77eSGeorge Wilson 
2123573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
2124cb04b873SMark J Musante 	*l2cache = B_FALSE;
2125daeb70e5SMark J Musante 	if (log != NULL)
2126daeb70e5SMark J Musante 		*log = B_FALSE;
2127573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2128573ca77eSGeorge Wilson 	nvlist_free(search);
2129573ca77eSGeorge Wilson 
2130573ca77eSGeorge Wilson 	return (ret);
2131573ca77eSGeorge Wilson }
2132573ca77eSGeorge Wilson 
213388ecc943SGeorge Wilson /*
213488ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
213588ecc943SGeorge Wilson  */
213688ecc943SGeorge Wilson boolean_t
213788ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
213888ecc943SGeorge Wilson {
213988ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
214088ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
214188ecc943SGeorge Wilson 		return (B_TRUE);
214288ecc943SGeorge Wilson 	return (B_FALSE);
214388ecc943SGeorge Wilson }
214488ecc943SGeorge Wilson 
214599653d4eSeschrock nvlist_t *
2146fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2147ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
2148ea8dc4b6Seschrock {
2149ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
2150ea8dc4b6Seschrock 	char *end;
2151573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
2152ea8dc4b6Seschrock 	uint64_t guid;
2153ea8dc4b6Seschrock 
2154573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2155573ca77eSGeorge Wilson 
21560917b783Seschrock 	guid = strtoull(path, &end, 10);
2157ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
2158573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
215988ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
216088ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2161ea8dc4b6Seschrock 	} else if (path[0] != '/') {
2162ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
2163573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2164ea8dc4b6Seschrock 	} else {
2165573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2166ea8dc4b6Seschrock 	}
2167ea8dc4b6Seschrock 
2168ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2169ea8dc4b6Seschrock 	    &nvroot) == 0);
2170ea8dc4b6Seschrock 
2171a43d325bSek 	*avail_spare = B_FALSE;
2172fa94a07fSbrendan 	*l2cache = B_FALSE;
2173ee0eb9f2SEric Schrock 	if (log != NULL)
2174ee0eb9f2SEric Schrock 		*log = B_FALSE;
2175573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2176573ca77eSGeorge Wilson 	nvlist_free(search);
2177573ca77eSGeorge Wilson 
2178573ca77eSGeorge Wilson 	return (ret);
2179a43d325bSek }
2180a43d325bSek 
218119397407SSherry Moore static int
218219397407SSherry Moore vdev_online(nvlist_t *nv)
218319397407SSherry Moore {
218419397407SSherry Moore 	uint64_t ival;
218519397407SSherry Moore 
218619397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
218719397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
218819397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
218919397407SSherry Moore 		return (0);
219019397407SSherry Moore 
219119397407SSherry Moore 	return (1);
219219397407SSherry Moore }
219319397407SSherry Moore 
219419397407SSherry Moore /*
219521ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
219619397407SSherry Moore  */
2197753a6d45SSherry Moore static int
219821ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2199753a6d45SSherry Moore     size_t *bytes_written)
220019397407SSherry Moore {
2201753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
2202753a6d45SSherry Moore 	char *tmppath;
2203753a6d45SSherry Moore 	const char *format;
2204753a6d45SSherry Moore 
2205753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2206753a6d45SSherry Moore 	    &tmppath) != 0)
2207753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2208753a6d45SSherry Moore 
2209753a6d45SSherry Moore 	pos = *bytes_written;
2210753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
2211753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
2212753a6d45SSherry Moore 
2213753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2214753a6d45SSherry Moore 	*bytes_written += rsz;
2215753a6d45SSherry Moore 
2216753a6d45SSherry Moore 	if (rsz >= bytes_left) {
2217753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2218753a6d45SSherry Moore 		if (bytes_left != 0) {
2219753a6d45SSherry Moore 			physpath[pos] = 0;
2220753a6d45SSherry Moore 		}
2221753a6d45SSherry Moore 		return (EZFS_NOSPC);
2222753a6d45SSherry Moore 	}
2223753a6d45SSherry Moore 	return (0);
2224753a6d45SSherry Moore }
2225753a6d45SSherry Moore 
222621ecdf64SLin Ling static int
222721ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
222821ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
222921ecdf64SLin Ling {
223021ecdf64SLin Ling 	char *type;
223121ecdf64SLin Ling 	int ret;
223221ecdf64SLin Ling 
223321ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
223421ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
223521ecdf64SLin Ling 
223621ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
223721ecdf64SLin Ling 		/*
223821ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
223921ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
224021ecdf64SLin Ling 		 * spare device.
224121ecdf64SLin Ling 		 */
224221ecdf64SLin Ling 		if (is_spare) {
224321ecdf64SLin Ling 			uint64_t spare = 0;
224421ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
224521ecdf64SLin Ling 			    &spare);
224621ecdf64SLin Ling 			if (!spare)
224721ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
224821ecdf64SLin Ling 		}
224921ecdf64SLin Ling 
225021ecdf64SLin Ling 		if (vdev_online(nv)) {
225121ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
225221ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
225321ecdf64SLin Ling 				return (ret);
225421ecdf64SLin Ling 		}
225521ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
225621ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
225721ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
225821ecdf64SLin Ling 		nvlist_t **child;
225921ecdf64SLin Ling 		uint_t count;
226021ecdf64SLin Ling 		int i, ret;
226121ecdf64SLin Ling 
226221ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
226321ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
226421ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
226521ecdf64SLin Ling 
226621ecdf64SLin Ling 		for (i = 0; i < count; i++) {
226721ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
226821ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
226921ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
227021ecdf64SLin Ling 				return (ret);
227121ecdf64SLin Ling 		}
227221ecdf64SLin Ling 	}
227321ecdf64SLin Ling 
227421ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
227521ecdf64SLin Ling }
227621ecdf64SLin Ling 
2277753a6d45SSherry Moore /*
2278753a6d45SSherry Moore  * Get phys_path for a root pool config.
2279753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2280753a6d45SSherry Moore  */
2281753a6d45SSherry Moore static int
2282753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2283753a6d45SSherry Moore {
2284753a6d45SSherry Moore 	size_t rsz;
228519397407SSherry Moore 	nvlist_t *vdev_root;
228619397407SSherry Moore 	nvlist_t **child;
228719397407SSherry Moore 	uint_t count;
2288753a6d45SSherry Moore 	char *type;
2289753a6d45SSherry Moore 
2290753a6d45SSherry Moore 	rsz = 0;
2291753a6d45SSherry Moore 
2292753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2293753a6d45SSherry Moore 	    &vdev_root) != 0)
2294753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2295753a6d45SSherry Moore 
2296753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2297753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2298753a6d45SSherry Moore 	    &child, &count) != 0)
2299753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
230019397407SSherry Moore 
230119397407SSherry Moore 	/*
2302753a6d45SSherry Moore 	 * root pool can not have EFI labeled disks and can only have
2303753a6d45SSherry Moore 	 * a single top-level vdev.
230419397407SSherry Moore 	 */
2305753a6d45SSherry Moore 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2306753a6d45SSherry Moore 	    pool_uses_efi(vdev_root))
2307753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
230819397407SSherry Moore 
230921ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
231021ecdf64SLin Ling 	    B_FALSE);
231119397407SSherry Moore 
2312753a6d45SSherry Moore 	/* No online devices */
2313753a6d45SSherry Moore 	if (rsz == 0)
2314753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2315753a6d45SSherry Moore 
231619397407SSherry Moore 	return (0);
231719397407SSherry Moore }
231819397407SSherry Moore 
2319753a6d45SSherry Moore /*
2320753a6d45SSherry Moore  * Get phys_path for a root pool
2321753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2322753a6d45SSherry Moore  */
2323753a6d45SSherry Moore int
2324753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2325753a6d45SSherry Moore {
2326753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2327753a6d45SSherry Moore 	    phypath_size));
2328753a6d45SSherry Moore }
2329753a6d45SSherry Moore 
2330573ca77eSGeorge Wilson /*
2331573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2332573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2333573ca77eSGeorge Wilson  */
2334573ca77eSGeorge Wilson static int
2335573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2336573ca77eSGeorge Wilson {
2337573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2338573ca77eSGeorge Wilson 	char errbuf[1024];
2339573ca77eSGeorge Wilson 	int fd, error;
2340573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2341573ca77eSGeorge Wilson 
2342573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2343573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2344573ca77eSGeorge Wilson 		return (-1);
2345573ca77eSGeorge Wilson 
2346573ca77eSGeorge Wilson 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2347573ca77eSGeorge Wilson 
2348573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2349573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2350573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2351573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2352573ca77eSGeorge Wilson 	}
2353573ca77eSGeorge Wilson 
2354573ca77eSGeorge Wilson 	/*
2355573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2356573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2357573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2358573ca77eSGeorge Wilson 	 */
2359573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2360573ca77eSGeorge Wilson 	(void) close(fd);
2361573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2362573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2363573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2364573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2365573ca77eSGeorge Wilson 	}
2366573ca77eSGeorge Wilson 	return (0);
2367573ca77eSGeorge Wilson }
2368573ca77eSGeorge Wilson 
2369fa9e4066Sahrens /*
23703d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
23713d7072f8Seschrock  * ZFS_ONLINE_* flags.
2372fa9e4066Sahrens  */
2373fa9e4066Sahrens int
23743d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
23753d7072f8Seschrock     vdev_state_t *newstate)
2376fa9e4066Sahrens {
2377fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2378fa9e4066Sahrens 	char msg[1024];
237999653d4eSeschrock 	nvlist_t *tgt;
2380573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
238199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2382fa9e4066Sahrens 
2383573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2384573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2385573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2386573ca77eSGeorge Wilson 	} else {
2387573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2388573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2389573ca77eSGeorge Wilson 	}
2390ea8dc4b6Seschrock 
2391fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2392ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2393573ca77eSGeorge Wilson 	    &islog)) == NULL)
239499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2395fa9e4066Sahrens 
239699653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2397fa9e4066Sahrens 
2398069f55e2SEric Schrock 	if (avail_spare)
2399a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2400a43d325bSek 
2401573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND ||
2402573ca77eSGeorge Wilson 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2403573ca77eSGeorge Wilson 		char *pathname = NULL;
2404573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2405573ca77eSGeorge Wilson 
2406573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2407573ca77eSGeorge Wilson 		    &wholedisk);
2408573ca77eSGeorge Wilson 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2409573ca77eSGeorge Wilson 		    &pathname) == 0);
2410573ca77eSGeorge Wilson 
2411573ca77eSGeorge Wilson 		/*
2412573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2413573ca77eSGeorge Wilson 		 */
2414573ca77eSGeorge Wilson 		if (l2cache) {
2415573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2416573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2417573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2418573ca77eSGeorge Wilson 		}
2419573ca77eSGeorge Wilson 
2420573ca77eSGeorge Wilson 		if (wholedisk) {
2421573ca77eSGeorge Wilson 			pathname += strlen(DISK_ROOT) + 1;
2422cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2423573ca77eSGeorge Wilson 		}
2424573ca77eSGeorge Wilson 	}
2425573ca77eSGeorge Wilson 
24263d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
24273d7072f8Seschrock 	zc.zc_obj = flags;
2428fa9e4066Sahrens 
2429cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
24301195e687SMark J Musante 		if (errno == EINVAL) {
24311195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
24321195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
24331195e687SMark J Musante 			    "instead"), "zpool detach");
24341195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
24351195e687SMark J Musante 		}
24363d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24371195e687SMark J Musante 	}
24383d7072f8Seschrock 
24393d7072f8Seschrock 	*newstate = zc.zc_cookie;
24403d7072f8Seschrock 	return (0);
2441fa9e4066Sahrens }
2442fa9e4066Sahrens 
2443fa9e4066Sahrens /*
2444fa9e4066Sahrens  * Take the specified vdev offline
2445fa9e4066Sahrens  */
2446fa9e4066Sahrens int
24473d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2448fa9e4066Sahrens {
2449fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2450fa9e4066Sahrens 	char msg[1024];
245199653d4eSeschrock 	nvlist_t *tgt;
2452fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
245399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2454fa9e4066Sahrens 
2455ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2456ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2457ea8dc4b6Seschrock 
2458fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2459ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2460ee0eb9f2SEric Schrock 	    NULL)) == NULL)
246199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
246299653d4eSeschrock 
246399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2464fa9e4066Sahrens 
2465069f55e2SEric Schrock 	if (avail_spare)
2466a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2467a43d325bSek 
24683d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
24693d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
24703d7072f8Seschrock 
2471cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24723d7072f8Seschrock 		return (0);
24733d7072f8Seschrock 
24743d7072f8Seschrock 	switch (errno) {
24753d7072f8Seschrock 	case EBUSY:
24763d7072f8Seschrock 
24773d7072f8Seschrock 		/*
24783d7072f8Seschrock 		 * There are no other replicas of this device.
24793d7072f8Seschrock 		 */
24803d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24813d7072f8Seschrock 
2482e6ca193dSGeorge Wilson 	case EEXIST:
2483e6ca193dSGeorge Wilson 		/*
2484e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2485e6ca193dSGeorge Wilson 		 */
2486e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2487e6ca193dSGeorge Wilson 
24883d7072f8Seschrock 	default:
24893d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24903d7072f8Seschrock 	}
24913d7072f8Seschrock }
24923d7072f8Seschrock 
24933d7072f8Seschrock /*
24943d7072f8Seschrock  * Mark the given vdev faulted.
24953d7072f8Seschrock  */
24963d7072f8Seschrock int
2497069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
24983d7072f8Seschrock {
24993d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25003d7072f8Seschrock 	char msg[1024];
25013d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25023d7072f8Seschrock 
25033d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25043d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2505441d80aaSlling 
25063d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25073d7072f8Seschrock 	zc.zc_guid = guid;
25083d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2509069f55e2SEric Schrock 	zc.zc_obj = aux;
25103d7072f8Seschrock 
2511cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2512fa9e4066Sahrens 		return (0);
2513fa9e4066Sahrens 
2514fa9e4066Sahrens 	switch (errno) {
251599653d4eSeschrock 	case EBUSY:
2516fa9e4066Sahrens 
2517fa9e4066Sahrens 		/*
2518fa9e4066Sahrens 		 * There are no other replicas of this device.
2519fa9e4066Sahrens 		 */
252099653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2521fa9e4066Sahrens 
252299653d4eSeschrock 	default:
252399653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2524fa9e4066Sahrens 	}
25253d7072f8Seschrock 
25263d7072f8Seschrock }
25273d7072f8Seschrock 
25283d7072f8Seschrock /*
25293d7072f8Seschrock  * Mark the given vdev degraded.
25303d7072f8Seschrock  */
25313d7072f8Seschrock int
2532069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25333d7072f8Seschrock {
25343d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25353d7072f8Seschrock 	char msg[1024];
25363d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25373d7072f8Seschrock 
25383d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25393d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
25403d7072f8Seschrock 
25413d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25423d7072f8Seschrock 	zc.zc_guid = guid;
25433d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2544069f55e2SEric Schrock 	zc.zc_obj = aux;
25453d7072f8Seschrock 
2546cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
25473d7072f8Seschrock 		return (0);
25483d7072f8Seschrock 
25493d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
255099653d4eSeschrock }
255199653d4eSeschrock 
255299653d4eSeschrock /*
255399653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
255499653d4eSeschrock  * a hot spare.
255599653d4eSeschrock  */
255699653d4eSeschrock static boolean_t
255799653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
255899653d4eSeschrock {
255999653d4eSeschrock 	nvlist_t **child;
256099653d4eSeschrock 	uint_t c, children;
256199653d4eSeschrock 	char *type;
256299653d4eSeschrock 
256399653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
256499653d4eSeschrock 	    &children) == 0) {
256599653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
256699653d4eSeschrock 		    &type) == 0);
256799653d4eSeschrock 
256899653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
256999653d4eSeschrock 		    children == 2 && child[which] == tgt)
257099653d4eSeschrock 			return (B_TRUE);
257199653d4eSeschrock 
257299653d4eSeschrock 		for (c = 0; c < children; c++)
257399653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
257499653d4eSeschrock 				return (B_TRUE);
257599653d4eSeschrock 	}
257699653d4eSeschrock 
257799653d4eSeschrock 	return (B_FALSE);
2578fa9e4066Sahrens }
2579fa9e4066Sahrens 
2580fa9e4066Sahrens /*
2581fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
25828654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2583fa9e4066Sahrens  */
2584fa9e4066Sahrens int
2585fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2586fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2587fa9e4066Sahrens {
2588fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2589fa9e4066Sahrens 	char msg[1024];
2590fa9e4066Sahrens 	int ret;
259199653d4eSeschrock 	nvlist_t *tgt;
2592ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2593ee0eb9f2SEric Schrock 	uint64_t val;
2594cb04b873SMark J Musante 	char *newname;
259599653d4eSeschrock 	nvlist_t **child;
259699653d4eSeschrock 	uint_t children;
259799653d4eSeschrock 	nvlist_t *config_root;
259899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25994263d13fSGeorge Wilson 	boolean_t rootpool = zpool_is_bootable(zhp);
2600fa9e4066Sahrens 
2601ea8dc4b6Seschrock 	if (replacing)
2602ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2603ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2604ea8dc4b6Seschrock 	else
2605ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2606ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2607ea8dc4b6Seschrock 
2608b5b76fecSGeorge Wilson 	/*
2609b5b76fecSGeorge Wilson 	 * If this is a root pool, make sure that we're not attaching an
2610b5b76fecSGeorge Wilson 	 * EFI labeled device.
2611b5b76fecSGeorge Wilson 	 */
2612b5b76fecSGeorge Wilson 	if (rootpool && pool_uses_efi(nvroot)) {
2613b5b76fecSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2614b5b76fecSGeorge Wilson 		    "EFI labeled devices are not supported on root pools."));
2615b5b76fecSGeorge Wilson 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2616b5b76fecSGeorge Wilson 	}
2617b5b76fecSGeorge Wilson 
2618fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2619ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2620ee0eb9f2SEric Schrock 	    &islog)) == 0)
262199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
262299653d4eSeschrock 
2623a43d325bSek 	if (avail_spare)
262499653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
262599653d4eSeschrock 
2626fa94a07fSbrendan 	if (l2cache)
2627fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2628fa94a07fSbrendan 
262999653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2630fa9e4066Sahrens 	zc.zc_cookie = replacing;
2631fa9e4066Sahrens 
263299653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
263399653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
263499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263599653d4eSeschrock 		    "new device must be a single disk"));
263699653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
263799653d4eSeschrock 	}
263899653d4eSeschrock 
263999653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
264099653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
264199653d4eSeschrock 
264288ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
26430430f8daSeschrock 		return (-1);
26440430f8daSeschrock 
264599653d4eSeschrock 	/*
264699653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
264799653d4eSeschrock 	 * replace it with another hot spare.
264899653d4eSeschrock 	 */
264999653d4eSeschrock 	if (replacing &&
265099653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2651ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2652ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2653ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
265499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
265599653d4eSeschrock 		    "can only be replaced by another hot spare"));
26560430f8daSeschrock 		free(newname);
265799653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
265899653d4eSeschrock 	}
265999653d4eSeschrock 
26600430f8daSeschrock 	free(newname);
26610430f8daSeschrock 
2662990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
266399653d4eSeschrock 		return (-1);
2664fa9e4066Sahrens 
2665cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2666fa9e4066Sahrens 
2667e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2668fa9e4066Sahrens 
2669b5b76fecSGeorge Wilson 	if (ret == 0) {
2670b5b76fecSGeorge Wilson 		if (rootpool) {
267121ecdf64SLin Ling 			/*
267221ecdf64SLin Ling 			 * XXX need a better way to prevent user from
267321ecdf64SLin Ling 			 * booting up a half-baked vdev.
267421ecdf64SLin Ling 			 */
267521ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
267621ecdf64SLin Ling 			    "sure to wait until resilver is done "
267721ecdf64SLin Ling 			    "before rebooting.\n"));
2678b5b76fecSGeorge Wilson 		}
2679fa9e4066Sahrens 		return (0);
2680b5b76fecSGeorge Wilson 	}
2681fa9e4066Sahrens 
2682fa9e4066Sahrens 	switch (errno) {
2683ea8dc4b6Seschrock 	case ENOTSUP:
2684fa9e4066Sahrens 		/*
2685fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2686fa9e4066Sahrens 		 */
26878654d025Sperrin 		if (replacing) {
2688cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2689cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2690cb04b873SMark J Musante 
2691ee0eb9f2SEric Schrock 			if (islog)
26928654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26938654d025Sperrin 				    "cannot replace a log with a spare"));
2694cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2695cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2696cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2697cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
26988654d025Sperrin 			else
26998654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27008654d025Sperrin 				    "cannot replace a replacing device"));
27018654d025Sperrin 		} else {
270299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270399653d4eSeschrock 			    "can only attach to mirrors and top-level "
270499653d4eSeschrock 			    "disks"));
27058654d025Sperrin 		}
270699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2707fa9e4066Sahrens 		break;
2708fa9e4066Sahrens 
2709ea8dc4b6Seschrock 	case EINVAL:
2710fa9e4066Sahrens 		/*
2711fa9e4066Sahrens 		 * The new device must be a single disk.
2712fa9e4066Sahrens 		 */
271399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
271499653d4eSeschrock 		    "new device must be a single disk"));
271599653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2716fa9e4066Sahrens 		break;
2717fa9e4066Sahrens 
2718ea8dc4b6Seschrock 	case EBUSY:
271999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
272099653d4eSeschrock 		    new_disk);
272199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2722fa9e4066Sahrens 		break;
2723fa9e4066Sahrens 
2724ea8dc4b6Seschrock 	case EOVERFLOW:
2725fa9e4066Sahrens 		/*
2726fa9e4066Sahrens 		 * The new device is too small.
2727fa9e4066Sahrens 		 */
272899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
272999653d4eSeschrock 		    "device is too small"));
273099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2731fa9e4066Sahrens 		break;
2732fa9e4066Sahrens 
2733ea8dc4b6Seschrock 	case EDOM:
2734fa9e4066Sahrens 		/*
2735fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2736fa9e4066Sahrens 		 */
273799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
273899653d4eSeschrock 		    "devices have different sector alignment"));
273999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2740fa9e4066Sahrens 		break;
2741fa9e4066Sahrens 
2742ea8dc4b6Seschrock 	case ENAMETOOLONG:
2743fa9e4066Sahrens 		/*
2744fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2745fa9e4066Sahrens 		 */
274699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2747fa9e4066Sahrens 		break;
2748fa9e4066Sahrens 
2749ea8dc4b6Seschrock 	default:
275099653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2751fa9e4066Sahrens 	}
2752fa9e4066Sahrens 
275399653d4eSeschrock 	return (-1);
2754fa9e4066Sahrens }
2755fa9e4066Sahrens 
2756fa9e4066Sahrens /*
2757fa9e4066Sahrens  * Detach the specified device.
2758fa9e4066Sahrens  */
2759fa9e4066Sahrens int
2760fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2761fa9e4066Sahrens {
2762fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2763fa9e4066Sahrens 	char msg[1024];
276499653d4eSeschrock 	nvlist_t *tgt;
2765fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
276699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2767fa9e4066Sahrens 
2768ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2769ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2770ea8dc4b6Seschrock 
2771fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2772ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2773ee0eb9f2SEric Schrock 	    NULL)) == 0)
277499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2775fa9e4066Sahrens 
2776a43d325bSek 	if (avail_spare)
277799653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
277899653d4eSeschrock 
2779fa94a07fSbrendan 	if (l2cache)
2780fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2781fa94a07fSbrendan 
278299653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
278399653d4eSeschrock 
2784ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2785fa9e4066Sahrens 		return (0);
2786fa9e4066Sahrens 
2787fa9e4066Sahrens 	switch (errno) {
2788fa9e4066Sahrens 
2789ea8dc4b6Seschrock 	case ENOTSUP:
2790fa9e4066Sahrens 		/*
2791fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2792fa9e4066Sahrens 		 */
279399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
279499653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2795cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2796fa9e4066Sahrens 		break;
2797fa9e4066Sahrens 
2798ea8dc4b6Seschrock 	case EBUSY:
2799fa9e4066Sahrens 		/*
2800fa9e4066Sahrens 		 * There are no other replicas of this device.
2801fa9e4066Sahrens 		 */
280299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2803fa9e4066Sahrens 		break;
2804fa9e4066Sahrens 
2805ea8dc4b6Seschrock 	default:
280699653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2807ea8dc4b6Seschrock 	}
2808ea8dc4b6Seschrock 
280999653d4eSeschrock 	return (-1);
281099653d4eSeschrock }
281199653d4eSeschrock 
28121195e687SMark J Musante /*
28131195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
28141195e687SMark J Musante  *
28151195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
28161195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
28171195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
28181195e687SMark J Musante  * see if any entry in the schild array matches.
28191195e687SMark J Musante  *
28201195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
28211195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
28221195e687SMark J Musante  */
28231195e687SMark J Musante static int
28241195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
28251195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
28261195e687SMark J Musante {
28271195e687SMark J Musante 	uint_t mc;
28281195e687SMark J Musante 
28291195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
28301195e687SMark J Musante 		uint_t sc;
28311195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28321195e687SMark J Musante 		    mchild[mc], B_FALSE);
28331195e687SMark J Musante 
28341195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
28351195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28361195e687SMark J Musante 			    schild[sc], B_FALSE);
28371195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
28381195e687SMark J Musante 
28391195e687SMark J Musante 			free(spath);
28401195e687SMark J Musante 			if (result) {
28411195e687SMark J Musante 				free(mpath);
28421195e687SMark J Musante 				return (mc);
28431195e687SMark J Musante 			}
28441195e687SMark J Musante 		}
28451195e687SMark J Musante 
28461195e687SMark J Musante 		free(mpath);
28471195e687SMark J Musante 	}
28481195e687SMark J Musante 
28491195e687SMark J Musante 	return (-1);
28501195e687SMark J Musante }
28511195e687SMark J Musante 
28521195e687SMark J Musante /*
28531195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
28541195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
28551195e687SMark J Musante  */
28561195e687SMark J Musante int
28571195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
28581195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
28591195e687SMark J Musante {
28601195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
28611195e687SMark J Musante 	char msg[1024];
28621195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
28631195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
28641195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
28651195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
28661195e687SMark J Musante 	uint64_t vers;
28671195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
28681195e687SMark J Musante 	int retval = 0;
28691195e687SMark J Musante 
28701195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
28711195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
28721195e687SMark J Musante 
28731195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
28741195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
28751195e687SMark J Musante 
28761195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
28771195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
28781195e687SMark J Musante 		    "retrieve pool configuration\n"));
28791195e687SMark J Musante 		return (-1);
28801195e687SMark J Musante 	}
28811195e687SMark J Musante 
28821195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
28831195e687SMark J Musante 	    == 0);
28841195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
28851195e687SMark J Musante 
28861195e687SMark J Musante 	if (props) {
2887f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
28881195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2889f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
28901195e687SMark J Musante 			return (-1);
28911195e687SMark J Musante 	}
28921195e687SMark J Musante 
28931195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
28941195e687SMark J Musante 	    &children) != 0) {
28951195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28961195e687SMark J Musante 		    "Source pool is missing vdev tree"));
28971195e687SMark J Musante 		if (zc_props)
28981195e687SMark J Musante 			nvlist_free(zc_props);
28991195e687SMark J Musante 		return (-1);
29001195e687SMark J Musante 	}
29011195e687SMark J Musante 
29021195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
29031195e687SMark J Musante 	vcount = 0;
29041195e687SMark J Musante 
29051195e687SMark J Musante 	if (*newroot == NULL ||
29061195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
29071195e687SMark J Musante 	    &newchild, &newchildren) != 0)
29081195e687SMark J Musante 		newchildren = 0;
29091195e687SMark J Musante 
29101195e687SMark J Musante 	for (c = 0; c < children; c++) {
29111195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
29121195e687SMark J Musante 		char *type;
29131195e687SMark J Musante 		nvlist_t **mchild, *vdev;
29141195e687SMark J Musante 		uint_t mchildren;
29151195e687SMark J Musante 		int entry;
29161195e687SMark J Musante 
29171195e687SMark J Musante 		/*
29181195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
29191195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
29201195e687SMark J Musante 		 */
29211195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
29221195e687SMark J Musante 		    &is_log);
29231195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
29241195e687SMark J Musante 		    &is_hole);
29251195e687SMark J Musante 		if (is_log || is_hole) {
29261195e687SMark J Musante 			/*
29271195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
29281195e687SMark J Musante 			 */
29291195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
29301195e687SMark J Musante 				goto out;
29311195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
29321195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
29331195e687SMark J Musante 				goto out;
29341195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
29351195e687SMark J Musante 			    1) != 0)
29361195e687SMark J Musante 				goto out;
29371195e687SMark J Musante 			if (lastlog == 0)
29381195e687SMark J Musante 				lastlog = vcount;
29391195e687SMark J Musante 			varray[vcount++] = vdev;
29401195e687SMark J Musante 			continue;
29411195e687SMark J Musante 		}
29421195e687SMark J Musante 		lastlog = 0;
29431195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
29441195e687SMark J Musante 		    == 0);
29451195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
29461195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29471195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
29481195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29491195e687SMark J Musante 			goto out;
29501195e687SMark J Musante 		}
29511195e687SMark J Musante 
29521195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
29531195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
29541195e687SMark J Musante 
29551195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
29561195e687SMark J Musante 		if (newchildren > 0 &&
29571195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
29581195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
29591195e687SMark J Musante 			/* We found a disk that the user specified. */
29601195e687SMark J Musante 			vdev = mchild[entry];
29611195e687SMark J Musante 			++found;
29621195e687SMark J Musante 		} else {
29631195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
29641195e687SMark J Musante 			vdev = mchild[mchildren - 1];
29651195e687SMark J Musante 		}
29661195e687SMark J Musante 
29671195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
29681195e687SMark J Musante 			goto out;
29691195e687SMark J Musante 	}
29701195e687SMark J Musante 
29711195e687SMark J Musante 	/* did we find every disk the user specified? */
29721195e687SMark J Musante 	if (found != newchildren) {
29731195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
29741195e687SMark J Musante 		    "include at most one disk from each mirror"));
29751195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29761195e687SMark J Musante 		goto out;
29771195e687SMark J Musante 	}
29781195e687SMark J Musante 
29791195e687SMark J Musante 	/* Prepare the nvlist for populating. */
29801195e687SMark J Musante 	if (*newroot == NULL) {
29811195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
29821195e687SMark J Musante 			goto out;
29831195e687SMark J Musante 		freelist = B_TRUE;
29841195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
29851195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
29861195e687SMark J Musante 			goto out;
29871195e687SMark J Musante 	} else {
29881195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
29891195e687SMark J Musante 	}
29901195e687SMark J Musante 
29911195e687SMark J Musante 	/* Add all the children we found */
29921195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
29931195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
29941195e687SMark J Musante 		goto out;
29951195e687SMark J Musante 
29961195e687SMark J Musante 	/*
29971195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
29981195e687SMark J Musante 	 */
29991195e687SMark J Musante 	if (flags.dryrun) {
30001195e687SMark J Musante 		memory_err = B_FALSE;
30011195e687SMark J Musante 		freelist = B_FALSE;
30021195e687SMark J Musante 		goto out;
30031195e687SMark J Musante 	}
30041195e687SMark J Musante 
30051195e687SMark J Musante 	/* now build up the config list & call the ioctl */
30061195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
30071195e687SMark J Musante 		goto out;
30081195e687SMark J Musante 
30091195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
30101195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
30111195e687SMark J Musante 	    nvlist_add_string(newconfig,
30121195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
30131195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
30141195e687SMark J Musante 		goto out;
30151195e687SMark J Musante 
30161195e687SMark J Musante 	/*
30171195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
30181195e687SMark J Musante 	 * explicitly export it.
30191195e687SMark J Musante 	 */
30201195e687SMark J Musante 	if (!flags.import)
30211195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
30221195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30231195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
30241195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
30251195e687SMark J Musante 		goto out;
30261195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
30271195e687SMark J Musante 		goto out;
30281195e687SMark J Musante 
30291195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
30301195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
30311195e687SMark J Musante 		goto out;
30321195e687SMark J Musante 	}
30331195e687SMark J Musante 
30341195e687SMark J Musante 	freelist = B_FALSE;
30351195e687SMark J Musante 	memory_err = B_FALSE;
30361195e687SMark J Musante 
30371195e687SMark J Musante out:
30381195e687SMark J Musante 	if (varray != NULL) {
30391195e687SMark J Musante 		int v;
30401195e687SMark J Musante 
30411195e687SMark J Musante 		for (v = 0; v < vcount; v++)
30421195e687SMark J Musante 			nvlist_free(varray[v]);
30431195e687SMark J Musante 		free(varray);
30441195e687SMark J Musante 	}
30451195e687SMark J Musante 	zcmd_free_nvlists(&zc);
30461195e687SMark J Musante 	if (zc_props)
30471195e687SMark J Musante 		nvlist_free(zc_props);
30481195e687SMark J Musante 	if (newconfig)
30491195e687SMark J Musante 		nvlist_free(newconfig);
30501195e687SMark J Musante 	if (freelist) {
30511195e687SMark J Musante 		nvlist_free(*newroot);
30521195e687SMark J Musante 		*newroot = NULL;
30531195e687SMark J Musante 	}
30541195e687SMark J Musante 
30551195e687SMark J Musante 	if (retval != 0)
30561195e687SMark J Musante 		return (retval);
30571195e687SMark J Musante 
30581195e687SMark J Musante 	if (memory_err)
30591195e687SMark J Musante 		return (no_memory(hdl));
30601195e687SMark J Musante 
30611195e687SMark J Musante 	return (0);
30621195e687SMark J Musante }
30631195e687SMark J Musante 
306499653d4eSeschrock /*
3065fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
3066fa94a07fSbrendan  * and level 2 cache devices.
306799653d4eSeschrock  */
306899653d4eSeschrock int
306999653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
307099653d4eSeschrock {
307199653d4eSeschrock 	zfs_cmd_t zc = { 0 };
307299653d4eSeschrock 	char msg[1024];
307399653d4eSeschrock 	nvlist_t *tgt;
307488ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
307599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
307688ecc943SGeorge Wilson 	uint64_t version;
307799653d4eSeschrock 
307899653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
307999653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
308099653d4eSeschrock 
308199653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3082ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
308388ecc943SGeorge Wilson 	    &islog)) == 0)
308499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
308588ecc943SGeorge Wilson 	/*
308688ecc943SGeorge Wilson 	 * XXX - this should just go away.
308788ecc943SGeorge Wilson 	 */
308888ecc943SGeorge Wilson 	if (!avail_spare && !l2cache && !islog) {
308999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309088ecc943SGeorge Wilson 		    "only inactive hot spares, cache, top-level, "
309188ecc943SGeorge Wilson 		    "or log devices can be removed"));
309299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
309399653d4eSeschrock 	}
309499653d4eSeschrock 
309588ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
309688ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
309788ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309888ecc943SGeorge Wilson 		    "pool must be upgrade to support log removal"));
309988ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
310088ecc943SGeorge Wilson 	}
310188ecc943SGeorge Wilson 
310299653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
310399653d4eSeschrock 
3104ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
310599653d4eSeschrock 		return (0);
310699653d4eSeschrock 
310799653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3108ea8dc4b6Seschrock }
3109ea8dc4b6Seschrock 
3110ea8dc4b6Seschrock /*
3111ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
3112ea8dc4b6Seschrock  */
3113ea8dc4b6Seschrock int
3114468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3115ea8dc4b6Seschrock {
3116ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3117ea8dc4b6Seschrock 	char msg[1024];
311899653d4eSeschrock 	nvlist_t *tgt;
3119468c413aSTim Haley 	zpool_rewind_policy_t policy;
3120fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
312199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3122468c413aSTim Haley 	nvlist_t *nvi = NULL;
31234b964adaSGeorge Wilson 	int error;
3124ea8dc4b6Seschrock 
3125ea8dc4b6Seschrock 	if (path)
3126ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3127ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3128e9dbad6fSeschrock 		    path);
3129ea8dc4b6Seschrock 	else
3130ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3131ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3132ea8dc4b6Seschrock 		    zhp->zpool_name);
3133ea8dc4b6Seschrock 
3134ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
313599653d4eSeschrock 	if (path) {
3136fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3137ee0eb9f2SEric Schrock 		    &l2cache, NULL)) == 0)
313899653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3139ea8dc4b6Seschrock 
3140fa94a07fSbrendan 		/*
3141fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
3142fa94a07fSbrendan 		 * error clearing for l2cache devices.
3143fa94a07fSbrendan 		 */
3144a43d325bSek 		if (avail_spare)
314599653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3146ea8dc4b6Seschrock 
314799653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
314899653d4eSeschrock 		    &zc.zc_guid) == 0);
3149fa9e4066Sahrens 	}
3150fa9e4066Sahrens 
3151468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
3152468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
3153468c413aSTim Haley 
315457f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3155468c413aSTim Haley 		return (-1);
3156468c413aSTim Haley 
3157cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3158468c413aSTim Haley 		return (-1);
3159468c413aSTim Haley 
31604b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
31614b964adaSGeorge Wilson 	    errno == ENOMEM) {
31624b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
31634b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
31644b964adaSGeorge Wilson 			return (-1);
31654b964adaSGeorge Wilson 		}
31664b964adaSGeorge Wilson 	}
31674b964adaSGeorge Wilson 
31684b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3169468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
3170468c413aSTim Haley 		if (policy.zrp_request &
3171468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3172468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3173468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
3174468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3175468c413aSTim Haley 			    nvi);
3176468c413aSTim Haley 			nvlist_free(nvi);
3177468c413aSTim Haley 		}
3178468c413aSTim Haley 		zcmd_free_nvlists(&zc);
317999653d4eSeschrock 		return (0);
3180468c413aSTim Haley 	}
318199653d4eSeschrock 
3182468c413aSTim Haley 	zcmd_free_nvlists(&zc);
318399653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3184fa9e4066Sahrens }
3185fa9e4066Sahrens 
31863d7072f8Seschrock /*
31873d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
31883d7072f8Seschrock  */
31893d7072f8Seschrock int
31903d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
31913d7072f8Seschrock {
31923d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
31933d7072f8Seschrock 	char msg[1024];
31943d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31953d7072f8Seschrock 
31963d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
31973d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
31983d7072f8Seschrock 	    guid);
31993d7072f8Seschrock 
32003d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
32013d7072f8Seschrock 	zc.zc_guid = guid;
320214f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
32033d7072f8Seschrock 
32043d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
32053d7072f8Seschrock 		return (0);
32063d7072f8Seschrock 
32073d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
32083d7072f8Seschrock }
32093d7072f8Seschrock 
3210e9103aaeSGarrett D'Amore /*
3211e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
3212e9103aaeSGarrett D'Amore  */
3213e9103aaeSGarrett D'Amore int
3214e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3215e9103aaeSGarrett D'Amore {
3216e9103aaeSGarrett D'Amore 	char msg[1024];
3217e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3218e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3219e9103aaeSGarrett D'Amore 
3220e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3221e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3222e9103aaeSGarrett D'Amore 
3223e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3224e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3225e9103aaeSGarrett D'Amore 		return (0);
3226e9103aaeSGarrett D'Amore 
3227e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3228e9103aaeSGarrett D'Amore }
3229e9103aaeSGarrett D'Amore 
32304263d13fSGeorge Wilson /*
32314263d13fSGeorge Wilson  * Reopen the pool.
32324263d13fSGeorge Wilson  */
32334263d13fSGeorge Wilson int
32344263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
32354263d13fSGeorge Wilson {
32364263d13fSGeorge Wilson 	zfs_cmd_t zc = { 0 };
32374263d13fSGeorge Wilson 	char msg[1024];
32384263d13fSGeorge Wilson 	libzfs_handle_t *hdl = zhp->zpool_hdl;
32394263d13fSGeorge Wilson 
32404263d13fSGeorge Wilson 	(void) snprintf(msg, sizeof (msg),
32414263d13fSGeorge Wilson 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
32424263d13fSGeorge Wilson 	    zhp->zpool_name);
32434263d13fSGeorge Wilson 
32444263d13fSGeorge Wilson 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
32454263d13fSGeorge Wilson 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
32464263d13fSGeorge Wilson 		return (0);
32474263d13fSGeorge Wilson 	return (zpool_standard_error(hdl, errno, msg));
32484263d13fSGeorge Wilson }
32494263d13fSGeorge Wilson 
3250c67d9675Seschrock /*
3251c67d9675Seschrock  * Convert from a devid string to a path.
3252c67d9675Seschrock  */
3253c67d9675Seschrock static char *
3254c67d9675Seschrock devid_to_path(char *devid_str)
3255c67d9675Seschrock {
3256c67d9675Seschrock 	ddi_devid_t devid;
3257c67d9675Seschrock 	char *minor;
3258c67d9675Seschrock 	char *path;
3259c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3260c67d9675Seschrock 	int ret;
3261c67d9675Seschrock 
3262c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3263c67d9675Seschrock 		return (NULL);
3264c67d9675Seschrock 
3265c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3266c67d9675Seschrock 
3267c67d9675Seschrock 	devid_str_free(minor);
3268c67d9675Seschrock 	devid_free(devid);
3269c67d9675Seschrock 
3270c67d9675Seschrock 	if (ret != 0)
3271c67d9675Seschrock 		return (NULL);
3272c67d9675Seschrock 
327399653d4eSeschrock 	if ((path = strdup(list[0].devname)) == NULL)
327499653d4eSeschrock 		return (NULL);
327599653d4eSeschrock 
3276c67d9675Seschrock 	devid_free_nmlist(list);
3277c67d9675Seschrock 
3278c67d9675Seschrock 	return (path);
3279c67d9675Seschrock }
3280c67d9675Seschrock 
3281c67d9675Seschrock /*
3282c67d9675Seschrock  * Convert from a path to a devid string.
3283c67d9675Seschrock  */
3284c67d9675Seschrock static char *
3285c67d9675Seschrock path_to_devid(const char *path)
3286c67d9675Seschrock {
3287c67d9675Seschrock 	int fd;
3288c67d9675Seschrock 	ddi_devid_t devid;
3289c67d9675Seschrock 	char *minor, *ret;
3290c67d9675Seschrock 
3291c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3292c67d9675Seschrock 		return (NULL);
3293c67d9675Seschrock 
3294c67d9675Seschrock 	minor = NULL;
3295c67d9675Seschrock 	ret = NULL;
3296c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3297c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3298c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3299c67d9675Seschrock 		if (minor != NULL)
3300c67d9675Seschrock 			devid_str_free(minor);
3301c67d9675Seschrock 		devid_free(devid);
3302c67d9675Seschrock 	}
3303c67d9675Seschrock 	(void) close(fd);
3304c67d9675Seschrock 
3305c67d9675Seschrock 	return (ret);
3306c67d9675Seschrock }
3307c67d9675Seschrock 
3308c67d9675Seschrock /*
3309c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3310c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3311c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3312c67d9675Seschrock  */
3313c67d9675Seschrock static void
3314c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3315c67d9675Seschrock {
3316c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3317c67d9675Seschrock 
3318c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3319e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3320c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3321ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3322c67d9675Seschrock 
332399653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3324c67d9675Seschrock }
3325c67d9675Seschrock 
3326c67d9675Seschrock /*
3327c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3328c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3329c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3330c67d9675Seschrock  * trailing 's0' slice name.
3331c67d9675Seschrock  *
3332c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3333c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3334c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3335c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3336c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3337c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3338c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3339c67d9675Seschrock  * of these checks.
3340c67d9675Seschrock  */
3341c67d9675Seschrock char *
334288ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
334388ecc943SGeorge Wilson     boolean_t verbose)
3344c67d9675Seschrock {
3345c67d9675Seschrock 	char *path, *devid;
3346ea8dc4b6Seschrock 	uint64_t value;
3347ea8dc4b6Seschrock 	char buf[64];
33483d7072f8Seschrock 	vdev_stat_t *vs;
33493d7072f8Seschrock 	uint_t vsc;
3350c67d9675Seschrock 
3351ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3352ea8dc4b6Seschrock 	    &value) == 0) {
3353ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3354ea8dc4b6Seschrock 		    &value) == 0);
33555ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
33565ad82045Snd 		    (u_longlong_t)value);
3357ea8dc4b6Seschrock 		path = buf;
3358ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3359c67d9675Seschrock 
33603d7072f8Seschrock 		/*
33613d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
33623d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
33633d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
33643d7072f8Seschrock 		 * effects.
33653d7072f8Seschrock 		 */
33663f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
33673d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
33683d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
33693d7072f8Seschrock 		    zhp != NULL &&
3370c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3371c67d9675Seschrock 			/*
3372c67d9675Seschrock 			 * Determine if the current path is correct.
3373c67d9675Seschrock 			 */
3374c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3375c67d9675Seschrock 
3376c67d9675Seschrock 			if (newdevid == NULL ||
3377c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3378c67d9675Seschrock 				char *newpath;
3379c67d9675Seschrock 
3380c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3381c67d9675Seschrock 					/*
3382c67d9675Seschrock 					 * Update the path appropriately.
3383c67d9675Seschrock 					 */
3384c67d9675Seschrock 					set_path(zhp, nv, newpath);
338599653d4eSeschrock 					if (nvlist_add_string(nv,
338699653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
338799653d4eSeschrock 						verify(nvlist_lookup_string(nv,
338899653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
338999653d4eSeschrock 						    &path) == 0);
3390c67d9675Seschrock 					free(newpath);
3391c67d9675Seschrock 				}
3392c67d9675Seschrock 			}
3393c67d9675Seschrock 
339499653d4eSeschrock 			if (newdevid)
339599653d4eSeschrock 				devid_str_free(newdevid);
3396c67d9675Seschrock 		}
3397c67d9675Seschrock 
3398c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3399c67d9675Seschrock 			path += 9;
3400c67d9675Seschrock 
3401c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3402ea8dc4b6Seschrock 		    &value) == 0 && value) {
34033fdda499SJohn Harres 			int pathlen = strlen(path);
340499653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
34053fdda499SJohn Harres 
34063fdda499SJohn Harres 			/*
34073fdda499SJohn Harres 			 * If it starts with c#, and ends with "s0", chop
34083fdda499SJohn Harres 			 * the "s0" off, or if it ends with "s0/old", remove
34093fdda499SJohn Harres 			 * the "s0" from the middle.
34103fdda499SJohn Harres 			 */
34113fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
34123fdda499SJohn Harres 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
34133fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
34143fdda499SJohn Harres 				} else if (pathlen > 6 &&
34153fdda499SJohn Harres 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
34163fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
34173fdda499SJohn Harres 					    "/old");
34183fdda499SJohn Harres 				}
34193fdda499SJohn Harres 			}
3420c67d9675Seschrock 			return (tmp);
3421c67d9675Seschrock 		}
3422c67d9675Seschrock 	} else {
3423c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
342499653d4eSeschrock 
342599653d4eSeschrock 		/*
342699653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
342799653d4eSeschrock 		 */
342899653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
342999653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
343099653d4eSeschrock 			    &value) == 0);
343199653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
34325ad82045Snd 			    (u_longlong_t)value);
343399653d4eSeschrock 			path = buf;
343499653d4eSeschrock 		}
343588ecc943SGeorge Wilson 
343688ecc943SGeorge Wilson 		/*
343788ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
343888ecc943SGeorge Wilson 		 * naming convention.
343988ecc943SGeorge Wilson 		 */
344088ecc943SGeorge Wilson 		if (verbose) {
344188ecc943SGeorge Wilson 			uint64_t id;
344288ecc943SGeorge Wilson 
344388ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
344488ecc943SGeorge Wilson 			    &id) == 0);
344588ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
344688ecc943SGeorge Wilson 			    (u_longlong_t)id);
344788ecc943SGeorge Wilson 			path = buf;
344888ecc943SGeorge Wilson 		}
3449c67d9675Seschrock 	}
3450c67d9675Seschrock 
345199653d4eSeschrock 	return (zfs_strdup(hdl, path));
3452c67d9675Seschrock }
3453ea8dc4b6Seschrock 
3454ea8dc4b6Seschrock static int
3455ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b)
3456ea8dc4b6Seschrock {
3457ea8dc4b6Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
3458ea8dc4b6Seschrock }
3459ea8dc4b6Seschrock 
3460ea8dc4b6Seschrock /*
3461ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3462ea8dc4b6Seschrock  * caller.
3463ea8dc4b6Seschrock  */
3464ea8dc4b6Seschrock int
346555434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3466ea8dc4b6Seschrock {
3467ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3468ea8dc4b6Seschrock 	uint64_t count;
3469e9dbad6fSeschrock 	zbookmark_t *zb = NULL;
347055434c77Sek 	int i;
3471ea8dc4b6Seschrock 
3472ea8dc4b6Seschrock 	/*
3473ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3474ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3475ea8dc4b6Seschrock 	 * entire list.
3476ea8dc4b6Seschrock 	 */
3477ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3478ea8dc4b6Seschrock 	    &count) == 0);
347975519f38Sek 	if (count == 0)
348075519f38Sek 		return (0);
3481e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
34825ad82045Snd 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
348399653d4eSeschrock 		return (-1);
3484e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3485ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3486ea8dc4b6Seschrock 	for (;;) {
348799653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
348899653d4eSeschrock 		    &zc) != 0) {
3489e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3490ea8dc4b6Seschrock 			if (errno == ENOMEM) {
3491bf561db0Svb 				count = zc.zc_nvlist_dst_size;
3492e9dbad6fSeschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
3493bf561db0Svb 				    zfs_alloc(zhp->zpool_hdl, count *
3494bf561db0Svb 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
349599653d4eSeschrock 					return (-1);
3496ea8dc4b6Seschrock 			} else {
3497ea8dc4b6Seschrock 				return (-1);
3498ea8dc4b6Seschrock 			}
3499ea8dc4b6Seschrock 		} else {
3500ea8dc4b6Seschrock 			break;
3501ea8dc4b6Seschrock 		}
3502ea8dc4b6Seschrock 	}
3503ea8dc4b6Seschrock 
3504ea8dc4b6Seschrock 	/*
3505ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3506ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3507e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3508ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3509ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3510ea8dc4b6Seschrock 	 */
3511e9dbad6fSeschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3512e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3513e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3514ea8dc4b6Seschrock 
3515ea8dc4b6Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3516ea8dc4b6Seschrock 
351755434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3518ea8dc4b6Seschrock 
3519ea8dc4b6Seschrock 	/*
352055434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3521ea8dc4b6Seschrock 	 */
3522ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3523ea8dc4b6Seschrock 		nvlist_t *nv;
3524ea8dc4b6Seschrock 
3525c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
3526c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3527c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
3528ea8dc4b6Seschrock 			continue;
3529ea8dc4b6Seschrock 
353055434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
353155434c77Sek 			goto nomem;
353255434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
353355434c77Sek 		    zb[i].zb_objset) != 0) {
353455434c77Sek 			nvlist_free(nv);
353599653d4eSeschrock 			goto nomem;
3536ea8dc4b6Seschrock 		}
353755434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
353855434c77Sek 		    zb[i].zb_object) != 0) {
353955434c77Sek 			nvlist_free(nv);
354055434c77Sek 			goto nomem;
354155434c77Sek 		}
354255434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
354355434c77Sek 			nvlist_free(nv);
354455434c77Sek 			goto nomem;
354555434c77Sek 		}
354655434c77Sek 		nvlist_free(nv);
3547ea8dc4b6Seschrock 	}
3548ea8dc4b6Seschrock 
35493ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3550ea8dc4b6Seschrock 	return (0);
355199653d4eSeschrock 
355299653d4eSeschrock nomem:
3553e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
355499653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3555ea8dc4b6Seschrock }
3556eaca9bbdSeschrock 
3557eaca9bbdSeschrock /*
3558eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3559eaca9bbdSeschrock  */
3560eaca9bbdSeschrock int
3561990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3562eaca9bbdSeschrock {
3563eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
356499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3565eaca9bbdSeschrock 
3566eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3567990b4856Slling 	zc.zc_cookie = new_version;
3568990b4856Slling 
3569ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3570ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
357199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
357299653d4eSeschrock 		    zhp->zpool_name));
3573eaca9bbdSeschrock 	return (0);
3574eaca9bbdSeschrock }
357506eeb2adSek 
357606eeb2adSek void
35772a6b87f0Sek zpool_set_history_str(const char *subcommand, int argc, char **argv,
35782a6b87f0Sek     char *history_str)
357906eeb2adSek {
358006eeb2adSek 	int i;
358106eeb2adSek 
35822a6b87f0Sek 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
35832a6b87f0Sek 	for (i = 1; i < argc; i++) {
35842a6b87f0Sek 		if (strlen(history_str) + 1 + strlen(argv[i]) >
35852a6b87f0Sek 		    HIS_MAX_RECORD_LEN)
35862a6b87f0Sek 			break;
35872a6b87f0Sek 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
35882a6b87f0Sek 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
35892a6b87f0Sek 	}
35902a6b87f0Sek }
35912a6b87f0Sek 
35922a6b87f0Sek /*
35932a6b87f0Sek  * Stage command history for logging.
35942a6b87f0Sek  */
35952a6b87f0Sek int
35962a6b87f0Sek zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
35972a6b87f0Sek {
35982a6b87f0Sek 	if (history_str == NULL)
35992a6b87f0Sek 		return (EINVAL);
36002a6b87f0Sek 
36012a6b87f0Sek 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
36022a6b87f0Sek 		return (EINVAL);
36032a6b87f0Sek 
3604228975ccSek 	if (hdl->libzfs_log_str != NULL)
3605ecd6cf80Smarks 		free(hdl->libzfs_log_str);
360606eeb2adSek 
36072a6b87f0Sek 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
36082a6b87f0Sek 		return (no_memory(hdl));
360906eeb2adSek 
36102a6b87f0Sek 	return (0);
361106eeb2adSek }
361206eeb2adSek 
361306eeb2adSek /*
361406eeb2adSek  * Perform ioctl to get some command history of a pool.
361506eeb2adSek  *
361606eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
361706eeb2adSek  * logical offset of the history buffer to start reading from.
361806eeb2adSek  *
361906eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
362006eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
362106eeb2adSek  */
362206eeb2adSek static int
362306eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
362406eeb2adSek {
362506eeb2adSek 	zfs_cmd_t zc = { 0 };
362606eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
362706eeb2adSek 
362806eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
362906eeb2adSek 
363006eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
363106eeb2adSek 	zc.zc_history_len = *len;
363206eeb2adSek 	zc.zc_history_offset = *off;
363306eeb2adSek 
363406eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
363506eeb2adSek 		switch (errno) {
363606eeb2adSek 		case EPERM:
3637ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3638ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
363906eeb2adSek 			    "cannot show history for pool '%s'"),
364006eeb2adSek 			    zhp->zpool_name));
364106eeb2adSek 		case ENOENT:
3642ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
364306eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
364406eeb2adSek 			    "'%s'"), zhp->zpool_name));
3645d7306b64Sek 		case ENOTSUP:
3646d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3647d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3648d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
364906eeb2adSek 		default:
3650ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
365106eeb2adSek 			    dgettext(TEXT_DOMAIN,
365206eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
365306eeb2adSek 		}
365406eeb2adSek 	}
365506eeb2adSek 
365606eeb2adSek 	*len = zc.zc_history_len;
365706eeb2adSek 	*off = zc.zc_history_offset;
365806eeb2adSek 
365906eeb2adSek 	return (0);
366006eeb2adSek }
366106eeb2adSek 
366206eeb2adSek /*
366306eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
366406eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
366506eeb2adSek  * processed as there wasn't a complete record.
366606eeb2adSek  */
36678f18d1faSGeorge Wilson int
366806eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
366906eeb2adSek     nvlist_t ***records, uint_t *numrecords)
367006eeb2adSek {
367106eeb2adSek 	uint64_t reclen;
367206eeb2adSek 	nvlist_t *nv;
367306eeb2adSek 	int i;
367406eeb2adSek 
367506eeb2adSek 	while (bytes_read > sizeof (reclen)) {
367606eeb2adSek 
367706eeb2adSek 		/* get length of packed record (stored as little endian) */
367806eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
367906eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
368006eeb2adSek 
368106eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
368206eeb2adSek 			break;
368306eeb2adSek 
368406eeb2adSek 		/* unpack record */
368506eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
368606eeb2adSek 			return (ENOMEM);
368706eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
368806eeb2adSek 		buf += sizeof (reclen) + reclen;
368906eeb2adSek 
369006eeb2adSek 		/* add record to nvlist array */
369106eeb2adSek 		(*numrecords)++;
369206eeb2adSek 		if (ISP2(*numrecords + 1)) {
369306eeb2adSek 			*records = realloc(*records,
369406eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
369506eeb2adSek 		}
369606eeb2adSek 		(*records)[*numrecords - 1] = nv;
369706eeb2adSek 	}
369806eeb2adSek 
369906eeb2adSek 	*leftover = bytes_read;
370006eeb2adSek 	return (0);
370106eeb2adSek }
370206eeb2adSek 
370306eeb2adSek #define	HIS_BUF_LEN	(128*1024)
370406eeb2adSek 
370506eeb2adSek /*
370606eeb2adSek  * Retrieve the command history of a pool.
370706eeb2adSek  */
370806eeb2adSek int
370906eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
371006eeb2adSek {
371106eeb2adSek 	char buf[HIS_BUF_LEN];
371206eeb2adSek 	uint64_t off = 0;
371306eeb2adSek 	nvlist_t **records = NULL;
371406eeb2adSek 	uint_t numrecords = 0;
371506eeb2adSek 	int err, i;
371606eeb2adSek 
371706eeb2adSek 	do {
371806eeb2adSek 		uint64_t bytes_read = sizeof (buf);
371906eeb2adSek 		uint64_t leftover;
372006eeb2adSek 
372106eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
372206eeb2adSek 			break;
372306eeb2adSek 
372406eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
372506eeb2adSek 		if (!bytes_read)
372606eeb2adSek 			break;
372706eeb2adSek 
372806eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
372906eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
373006eeb2adSek 			break;
373106eeb2adSek 		off -= leftover;
373206eeb2adSek 
373306eeb2adSek 		/* CONSTCOND */
373406eeb2adSek 	} while (1);
373506eeb2adSek 
373606eeb2adSek 	if (!err) {
373706eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
373806eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
373906eeb2adSek 		    records, numrecords) == 0);
374006eeb2adSek 	}
374106eeb2adSek 	for (i = 0; i < numrecords; i++)
374206eeb2adSek 		nvlist_free(records[i]);
374306eeb2adSek 	free(records);
374406eeb2adSek 
374506eeb2adSek 	return (err);
374606eeb2adSek }
374755434c77Sek 
374855434c77Sek void
374955434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
375055434c77Sek     char *pathname, size_t len)
375155434c77Sek {
375255434c77Sek 	zfs_cmd_t zc = { 0 };
375355434c77Sek 	boolean_t mounted = B_FALSE;
375455434c77Sek 	char *mntpnt = NULL;
375555434c77Sek 	char dsname[MAXNAMELEN];
375655434c77Sek 
375755434c77Sek 	if (dsobj == 0) {
375855434c77Sek 		/* special case for the MOS */
375955434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
376055434c77Sek 		return;
376155434c77Sek 	}
376255434c77Sek 
376355434c77Sek 	/* get the dataset's name */
376455434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
376555434c77Sek 	zc.zc_obj = dsobj;
376655434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
376755434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
376855434c77Sek 		/* just write out a path of two object numbers */
376955434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
377055434c77Sek 		    dsobj, obj);
377155434c77Sek 		return;
377255434c77Sek 	}
377355434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
377455434c77Sek 
377555434c77Sek 	/* find out if the dataset is mounted */
377655434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
377755434c77Sek 
377855434c77Sek 	/* get the corrupted object's path */
377955434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
378055434c77Sek 	zc.zc_obj = obj;
378155434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
378255434c77Sek 	    &zc) == 0) {
378355434c77Sek 		if (mounted) {
378455434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
378555434c77Sek 			    zc.zc_value);
378655434c77Sek 		} else {
378755434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
378855434c77Sek 			    dsname, zc.zc_value);
378955434c77Sek 		}
379055434c77Sek 	} else {
379155434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
379255434c77Sek 	}
379355434c77Sek 	free(mntpnt);
379455434c77Sek }
3795b1b8ab34Slling 
379615e6edf1Sgw /*
379715e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
379815e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
379915e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
380015e6edf1Sgw  * partition.
380115e6edf1Sgw  */
380215e6edf1Sgw static int
380315e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb)
380415e6edf1Sgw {
380515e6edf1Sgw 	char *path;
380615e6edf1Sgw 	int fd;
380715e6edf1Sgw 	char diskname[MAXPATHLEN];
380815e6edf1Sgw 	int err = -1;
380915e6edf1Sgw 
381015e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
381115e6edf1Sgw 		return (err);
381215e6edf1Sgw 
381315e6edf1Sgw 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
381415e6edf1Sgw 	    strrchr(path, '/'));
381515e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
381615e6edf1Sgw 		struct dk_gpt *vtoc;
381715e6edf1Sgw 
381815e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
381915e6edf1Sgw 			if (sb != NULL)
382015e6edf1Sgw 				*sb = vtoc->efi_parts[0].p_start;
382115e6edf1Sgw 			efi_free(vtoc);
382215e6edf1Sgw 		}
382315e6edf1Sgw 		(void) close(fd);
382415e6edf1Sgw 	}
382515e6edf1Sgw 	return (err);
382615e6edf1Sgw }
382715e6edf1Sgw 
38288488aeb5Staylor /*
38298488aeb5Staylor  * determine where a partition starts on a disk in the current
38308488aeb5Staylor  * configuration
38318488aeb5Staylor  */
38328488aeb5Staylor static diskaddr_t
38338488aeb5Staylor find_start_block(nvlist_t *config)
38348488aeb5Staylor {
38358488aeb5Staylor 	nvlist_t **child;
38368488aeb5Staylor 	uint_t c, children;
38378488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
38388488aeb5Staylor 	uint64_t wholedisk;
38398488aeb5Staylor 
38408488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
38418488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
38428488aeb5Staylor 		if (nvlist_lookup_uint64(config,
38438488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
38448488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
38458488aeb5Staylor 			return (MAXOFFSET_T);
38468488aeb5Staylor 		}
384715e6edf1Sgw 		if (read_efi_label(config, &sb) < 0)
384815e6edf1Sgw 			sb = MAXOFFSET_T;
38498488aeb5Staylor 		return (sb);
38508488aeb5Staylor 	}
38518488aeb5Staylor 
38528488aeb5Staylor 	for (c = 0; c < children; c++) {
38538488aeb5Staylor 		sb = find_start_block(child[c]);
38548488aeb5Staylor 		if (sb != MAXOFFSET_T) {
38558488aeb5Staylor 			return (sb);
38568488aeb5Staylor 		}
38578488aeb5Staylor 	}
38588488aeb5Staylor 	return (MAXOFFSET_T);
38598488aeb5Staylor }
38608488aeb5Staylor 
38618488aeb5Staylor /*
38628488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
38638488aeb5Staylor  * stripped of any leading /dev path.
38648488aeb5Staylor  */
38658488aeb5Staylor int
38668488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
38678488aeb5Staylor {
38688488aeb5Staylor 	char path[MAXPATHLEN];
38698488aeb5Staylor 	struct dk_gpt *vtoc;
38708488aeb5Staylor 	int fd;
38718488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
38728488aeb5Staylor 	uint64_t slice_size;
38738488aeb5Staylor 	diskaddr_t start_block;
38748488aeb5Staylor 	char errbuf[1024];
38758488aeb5Staylor 
3876c6ef114fSmmusante 	/* prepare an error message just in case */
3877c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
3878c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3879c6ef114fSmmusante 
38808488aeb5Staylor 	if (zhp) {
38818488aeb5Staylor 		nvlist_t *nvroot;
38828488aeb5Staylor 
38834263d13fSGeorge Wilson 		if (zpool_is_bootable(zhp)) {
3884b5b76fecSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3885b5b76fecSGeorge Wilson 			    "EFI labeled devices are not supported on root "
3886b5b76fecSGeorge Wilson 			    "pools."));
3887b5b76fecSGeorge Wilson 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3888b5b76fecSGeorge Wilson 		}
3889b5b76fecSGeorge Wilson 
38908488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
38918488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
38928488aeb5Staylor 
38938488aeb5Staylor 		if (zhp->zpool_start_block == 0)
38948488aeb5Staylor 			start_block = find_start_block(nvroot);
38958488aeb5Staylor 		else
38968488aeb5Staylor 			start_block = zhp->zpool_start_block;
38978488aeb5Staylor 		zhp->zpool_start_block = start_block;
38988488aeb5Staylor 	} else {
38998488aeb5Staylor 		/* new pool */
39008488aeb5Staylor 		start_block = NEW_START_BLOCK;
39018488aeb5Staylor 	}
39028488aeb5Staylor 
39038488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
39048488aeb5Staylor 	    BACKUP_SLICE);
39058488aeb5Staylor 
39068488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
39078488aeb5Staylor 		/*
39088488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
39098488aeb5Staylor 		 * is a valid device.
39108488aeb5Staylor 		 */
3911c6ef114fSmmusante 		zfs_error_aux(hdl,
3912c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
39138488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
39148488aeb5Staylor 	}
39158488aeb5Staylor 
39168488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
39178488aeb5Staylor 		/*
39188488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
39198488aeb5Staylor 		 * were unable to read the disk's capacity
39208488aeb5Staylor 		 */
39218488aeb5Staylor 		if (errno == ENOMEM)
39228488aeb5Staylor 			(void) no_memory(hdl);
39238488aeb5Staylor 
39248488aeb5Staylor 		(void) close(fd);
3925c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3926c6ef114fSmmusante 		    "unable to read disk capacity"), name);
39278488aeb5Staylor 
39288488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
39298488aeb5Staylor 	}
39308488aeb5Staylor 
39318488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
39328488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
39338488aeb5Staylor 	if (start_block == MAXOFFSET_T)
39348488aeb5Staylor 		start_block = NEW_START_BLOCK;
39358488aeb5Staylor 	slice_size -= start_block;
39368488aeb5Staylor 
39378488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
39388488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
39398488aeb5Staylor 
39408488aeb5Staylor 	/*
39418488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
39428488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
39438488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
39448488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
39458488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
39468488aeb5Staylor 	 * can get, in the absence of V_OTHER.
39478488aeb5Staylor 	 */
39488488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
39498488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
39508488aeb5Staylor 
39518488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
39528488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
39538488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
39548488aeb5Staylor 
39558488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
39568488aeb5Staylor 		/*
39578488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
39588488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
39598488aeb5Staylor 		 * ecting the user to manually label the disk and give
39608488aeb5Staylor 		 * a specific slice.
39618488aeb5Staylor 		 */
39628488aeb5Staylor 		(void) close(fd);
39638488aeb5Staylor 		efi_free(vtoc);
39648488aeb5Staylor 
39658488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3966c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
39678488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
39688488aeb5Staylor 	}
39698488aeb5Staylor 
39708488aeb5Staylor 	(void) close(fd);
39718488aeb5Staylor 	efi_free(vtoc);
39728488aeb5Staylor 	return (0);
39738488aeb5Staylor }
3974e7cbe64fSgw 
3975e7cbe64fSgw static boolean_t
3976e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3977e7cbe64fSgw {
3978e7cbe64fSgw 	char *type;
3979e7cbe64fSgw 	nvlist_t **child;
3980e7cbe64fSgw 	uint_t children, c;
3981e7cbe64fSgw 
3982e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3983e7cbe64fSgw 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3984e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
3985e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
398688ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3987e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3988e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3989e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
3990e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3991e7cbe64fSgw 		return (B_FALSE);
3992e7cbe64fSgw 	}
3993e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3994e7cbe64fSgw 	    &child, &children) == 0) {
3995e7cbe64fSgw 		for (c = 0; c < children; c++) {
3996e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3997e7cbe64fSgw 				return (B_FALSE);
3998e7cbe64fSgw 		}
3999e7cbe64fSgw 	}
4000e7cbe64fSgw 	return (B_TRUE);
4001e7cbe64fSgw }
4002e7cbe64fSgw 
4003e7cbe64fSgw /*
4004e7cbe64fSgw  * check if this zvol is allowable for use as a dump device; zero if
4005e7cbe64fSgw  * it is, > 0 if it isn't, < 0 if it isn't a zvol
4006e7cbe64fSgw  */
4007e7cbe64fSgw int
4008e7cbe64fSgw zvol_check_dump_config(char *arg)
4009e7cbe64fSgw {
4010e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
4011e7cbe64fSgw 	nvlist_t *config, *nvroot;
4012e7cbe64fSgw 	char *p, *volname;
4013e7cbe64fSgw 	nvlist_t **top;
4014e7cbe64fSgw 	uint_t toplevels;
4015e7cbe64fSgw 	libzfs_handle_t *hdl;
4016e7cbe64fSgw 	char errbuf[1024];
4017e7cbe64fSgw 	char poolname[ZPOOL_MAXNAMELEN];
4018e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4019e7cbe64fSgw 	int ret = 1;
4020e7cbe64fSgw 
4021e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4022e7cbe64fSgw 		return (-1);
4023e7cbe64fSgw 	}
4024e7cbe64fSgw 
4025e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4026e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
4027e7cbe64fSgw 
4028e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
4029e7cbe64fSgw 		return (1);
4030e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
4031e7cbe64fSgw 
4032e7cbe64fSgw 	volname = arg + pathlen;
4033e7cbe64fSgw 
4034e7cbe64fSgw 	/* check the configuration of the pool */
4035e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
4036e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4037e7cbe64fSgw 		    "malformed dataset name"));
4038e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4039e7cbe64fSgw 		return (1);
4040e7cbe64fSgw 	} else if (p - volname >= ZFS_MAXNAMELEN) {
4041e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4042e7cbe64fSgw 		    "dataset name is too long"));
4043e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4044e7cbe64fSgw 		return (1);
4045e7cbe64fSgw 	} else {
4046e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
4047e7cbe64fSgw 		poolname[p - volname] = '\0';
4048e7cbe64fSgw 	}
4049e7cbe64fSgw 
4050e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4051e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4052e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
4053e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4054e7cbe64fSgw 		goto out;
4055e7cbe64fSgw 	}
4056e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
4057e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4058e7cbe64fSgw 	    &nvroot) != 0) {
4059e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4060e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
4061e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4062e7cbe64fSgw 		goto out;
4063e7cbe64fSgw 	}
4064e7cbe64fSgw 
4065e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4066e7cbe64fSgw 	    &top, &toplevels) == 0);
4067e7cbe64fSgw 	if (toplevels != 1) {
4068e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4069e7cbe64fSgw 		    "'%s' has multiple top level vdevs"), poolname);
4070e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
4071e7cbe64fSgw 		goto out;
4072e7cbe64fSgw 	}
4073e7cbe64fSgw 
4074e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4075e7cbe64fSgw 		goto out;
4076e7cbe64fSgw 	}
4077e7cbe64fSgw 	ret = 0;
4078e7cbe64fSgw 
4079e7cbe64fSgw out:
4080e7cbe64fSgw 	if (zhp)
4081e7cbe64fSgw 		zpool_close(zhp);
4082e7cbe64fSgw 	libzfs_fini(hdl);
4083e7cbe64fSgw 	return (ret);
4084e7cbe64fSgw }
4085