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>
37*4445fffbSMatthew Ahrens #include <libgen.h>
388488aeb5Staylor #include <sys/efi_partition.h>
398488aeb5Staylor #include <sys/vtoc.h>
40fa9e4066Sahrens #include <sys/zfs_ioctl.h>
41573ca77eSGeorge Wilson #include <dlfcn.h>
42fa9e4066Sahrens 
43fa9e4066Sahrens #include "zfs_namecheck.h"
44b1b8ab34Slling #include "zfs_prop.h"
45fa9e4066Sahrens #include "libzfs_impl.h"
46468c413aSTim Haley #include "zfs_comutil.h"
47ad135b5dSChristopher Siden #include "zfeature_common.h"
48fa9e4066Sahrens 
4915e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
50990b4856Slling 
51573ca77eSGeorge Wilson #define	DISK_ROOT	"/dev/dsk"
52573ca77eSGeorge Wilson #define	RDISK_ROOT	"/dev/rdsk"
53573ca77eSGeorge Wilson #define	BACKUP_SLICE	"s2"
54573ca77eSGeorge Wilson 
55f9af39baSGeorge Wilson typedef struct prop_flags {
56f9af39baSGeorge Wilson 	int create:1;	/* Validate property on creation */
57f9af39baSGeorge Wilson 	int import:1;	/* Validate property on import */
58f9af39baSGeorge Wilson } prop_flags_t;
59f9af39baSGeorge Wilson 
60990b4856Slling /*
61990b4856Slling  * ====================================================================
62990b4856Slling  *   zpool property functions
63990b4856Slling  * ====================================================================
64990b4856Slling  */
65990b4856Slling 
66990b4856Slling static int
67990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
68990b4856Slling {
69990b4856Slling 	zfs_cmd_t zc = { 0 };
70990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
71990b4856Slling 
72990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
73990b4856Slling 
74990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
75990b4856Slling 		return (-1);
76990b4856Slling 
77990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
78990b4856Slling 		if (errno == ENOMEM) {
79990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
80990b4856Slling 				zcmd_free_nvlists(&zc);
81990b4856Slling 				return (-1);
82990b4856Slling 			}
83990b4856Slling 		} else {
84990b4856Slling 			zcmd_free_nvlists(&zc);
85990b4856Slling 			return (-1);
86990b4856Slling 		}
87990b4856Slling 	}
88990b4856Slling 
89990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
90990b4856Slling 		zcmd_free_nvlists(&zc);
91990b4856Slling 		return (-1);
92990b4856Slling 	}
93990b4856Slling 
94990b4856Slling 	zcmd_free_nvlists(&zc);
95990b4856Slling 
96990b4856Slling 	return (0);
97990b4856Slling }
98990b4856Slling 
99990b4856Slling static int
100990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
101990b4856Slling {
102990b4856Slling 	nvlist_t *old_props;
103990b4856Slling 
104990b4856Slling 	old_props = zhp->zpool_props;
105990b4856Slling 
106990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
107990b4856Slling 		return (-1);
108990b4856Slling 
109990b4856Slling 	nvlist_free(old_props);
110990b4856Slling 	return (0);
111990b4856Slling }
112990b4856Slling 
113990b4856Slling static char *
114990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
115990b4856Slling     zprop_source_t *src)
116990b4856Slling {
117990b4856Slling 	nvlist_t *nv, *nvl;
118990b4856Slling 	uint64_t ival;
119990b4856Slling 	char *value;
120990b4856Slling 	zprop_source_t source;
121990b4856Slling 
122990b4856Slling 	nvl = zhp->zpool_props;
123990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
124990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
125990b4856Slling 		source = ival;
126990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
127990b4856Slling 	} else {
128990b4856Slling 		source = ZPROP_SRC_DEFAULT;
129990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
130990b4856Slling 			value = "-";
131990b4856Slling 	}
132990b4856Slling 
133990b4856Slling 	if (src)
134990b4856Slling 		*src = source;
135990b4856Slling 
136990b4856Slling 	return (value);
137990b4856Slling }
138990b4856Slling 
139990b4856Slling uint64_t
140990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
141990b4856Slling {
142990b4856Slling 	nvlist_t *nv, *nvl;
143990b4856Slling 	uint64_t value;
144990b4856Slling 	zprop_source_t source;
145990b4856Slling 
146b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
147b87f3af3Sperrin 		/*
148b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
149b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
150b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
151b87f3af3Sperrin 		 */
152b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
153b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
154b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
155b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
156b87f3af3Sperrin 		    == 0)) {
157b87f3af3Sperrin 			return (value);
158b87f3af3Sperrin 		}
159990b4856Slling 		return (zpool_prop_default_numeric(prop));
160b87f3af3Sperrin 	}
161990b4856Slling 
162990b4856Slling 	nvl = zhp->zpool_props;
163990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
164990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
165990b4856Slling 		source = value;
166990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
167990b4856Slling 	} else {
168990b4856Slling 		source = ZPROP_SRC_DEFAULT;
169990b4856Slling 		value = zpool_prop_default_numeric(prop);
170990b4856Slling 	}
171990b4856Slling 
172990b4856Slling 	if (src)
173990b4856Slling 		*src = source;
174990b4856Slling 
175990b4856Slling 	return (value);
176990b4856Slling }
177990b4856Slling 
178990b4856Slling /*
179990b4856Slling  * Map VDEV STATE to printed strings.
180990b4856Slling  */
181990b4856Slling char *
182990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183990b4856Slling {
184990b4856Slling 	switch (state) {
185990b4856Slling 	case VDEV_STATE_CLOSED:
186990b4856Slling 	case VDEV_STATE_OFFLINE:
187990b4856Slling 		return (gettext("OFFLINE"));
188990b4856Slling 	case VDEV_STATE_REMOVED:
189990b4856Slling 		return (gettext("REMOVED"));
190990b4856Slling 	case VDEV_STATE_CANT_OPEN:
191b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
192990b4856Slling 			return (gettext("FAULTED"));
1931195e687SMark J Musante 		else if (aux == VDEV_AUX_SPLIT_POOL)
1941195e687SMark J Musante 			return (gettext("SPLIT"));
195990b4856Slling 		else
196990b4856Slling 			return (gettext("UNAVAIL"));
197990b4856Slling 	case VDEV_STATE_FAULTED:
198990b4856Slling 		return (gettext("FAULTED"));
199990b4856Slling 	case VDEV_STATE_DEGRADED:
200990b4856Slling 		return (gettext("DEGRADED"));
201990b4856Slling 	case VDEV_STATE_HEALTHY:
202990b4856Slling 		return (gettext("ONLINE"));
203990b4856Slling 	}
204990b4856Slling 
205990b4856Slling 	return (gettext("UNKNOWN"));
206990b4856Slling }
207990b4856Slling 
208990b4856Slling /*
209990b4856Slling  * Get a zpool property value for 'prop' and return the value in
210990b4856Slling  * a pre-allocated buffer.
211990b4856Slling  */
212990b4856Slling int
213990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
214990b4856Slling     zprop_source_t *srctype)
215990b4856Slling {
216990b4856Slling 	uint64_t intval;
217990b4856Slling 	const char *strval;
218990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
219990b4856Slling 	nvlist_t *nvroot;
220990b4856Slling 	vdev_stat_t *vs;
221990b4856Slling 	uint_t vsc;
222990b4856Slling 
223990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
224379c004dSEric Schrock 		switch (prop) {
225379c004dSEric Schrock 		case ZPOOL_PROP_NAME:
226990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
227379c004dSEric Schrock 			break;
228379c004dSEric Schrock 
229379c004dSEric Schrock 		case ZPOOL_PROP_HEALTH:
230990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
231379c004dSEric Schrock 			break;
232379c004dSEric Schrock 
233379c004dSEric Schrock 		case ZPOOL_PROP_GUID:
234379c004dSEric Schrock 			intval = zpool_get_prop_int(zhp, prop, &src);
235379c004dSEric Schrock 			(void) snprintf(buf, len, "%llu", intval);
236379c004dSEric Schrock 			break;
237379c004dSEric Schrock 
238379c004dSEric Schrock 		case ZPOOL_PROP_ALTROOT:
239379c004dSEric Schrock 		case ZPOOL_PROP_CACHEFILE:
2408704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
241379c004dSEric Schrock 			if (zhp->zpool_props != NULL ||
242379c004dSEric Schrock 			    zpool_get_all_props(zhp) == 0) {
243379c004dSEric Schrock 				(void) strlcpy(buf,
244379c004dSEric Schrock 				    zpool_get_prop_string(zhp, prop, &src),
245379c004dSEric Schrock 				    len);
246379c004dSEric Schrock 				if (srctype != NULL)
247379c004dSEric Schrock 					*srctype = src;
248379c004dSEric Schrock 				return (0);
249379c004dSEric Schrock 			}
250379c004dSEric Schrock 			/* FALLTHROUGH */
251379c004dSEric Schrock 		default:
252990b4856Slling 			(void) strlcpy(buf, "-", len);
253379c004dSEric Schrock 			break;
254379c004dSEric Schrock 		}
255379c004dSEric Schrock 
256379c004dSEric Schrock 		if (srctype != NULL)
257379c004dSEric Schrock 			*srctype = src;
258990b4856Slling 		return (0);
259990b4856Slling 	}
260990b4856Slling 
261990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
262990b4856Slling 	    prop != ZPOOL_PROP_NAME)
263990b4856Slling 		return (-1);
264990b4856Slling 
265990b4856Slling 	switch (zpool_prop_get_type(prop)) {
266990b4856Slling 	case PROP_TYPE_STRING:
267990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
268990b4856Slling 		    len);
269990b4856Slling 		break;
270990b4856Slling 
271990b4856Slling 	case PROP_TYPE_NUMBER:
272990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
273990b4856Slling 
274990b4856Slling 		switch (prop) {
275990b4856Slling 		case ZPOOL_PROP_SIZE:
276485bbbf5SGeorge Wilson 		case ZPOOL_PROP_ALLOCATED:
277485bbbf5SGeorge Wilson 		case ZPOOL_PROP_FREE:
278ad135b5dSChristopher Siden 		case ZPOOL_PROP_FREEING:
2794263d13fSGeorge Wilson 		case ZPOOL_PROP_EXPANDSZ:
280990b4856Slling 			(void) zfs_nicenum(intval, buf, len);
281990b4856Slling 			break;
282990b4856Slling 
283990b4856Slling 		case ZPOOL_PROP_CAPACITY:
284990b4856Slling 			(void) snprintf(buf, len, "%llu%%",
285990b4856Slling 			    (u_longlong_t)intval);
286990b4856Slling 			break;
287990b4856Slling 
288b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
289b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
290b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
291b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
292b24ab676SJeff Bonwick 			break;
293b24ab676SJeff Bonwick 
294990b4856Slling 		case ZPOOL_PROP_HEALTH:
295990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
296990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
297990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
2983f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2993f9d6ad7SLin Ling 			    == 0);
300990b4856Slling 
301990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
302990b4856Slling 			    vs->vs_aux), len);
303990b4856Slling 			break;
304ad135b5dSChristopher Siden 		case ZPOOL_PROP_VERSION:
305ad135b5dSChristopher Siden 			if (intval >= SPA_VERSION_FEATURES) {
306ad135b5dSChristopher Siden 				(void) snprintf(buf, len, "-");
307ad135b5dSChristopher Siden 				break;
308ad135b5dSChristopher Siden 			}
309ad135b5dSChristopher Siden 			/* FALLTHROUGH */
310990b4856Slling 		default:
311990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
312990b4856Slling 		}
313990b4856Slling 		break;
314990b4856Slling 
315990b4856Slling 	case PROP_TYPE_INDEX:
316990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
317990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
318990b4856Slling 		    != 0)
319990b4856Slling 			return (-1);
320990b4856Slling 		(void) strlcpy(buf, strval, len);
321990b4856Slling 		break;
322990b4856Slling 
323990b4856Slling 	default:
324990b4856Slling 		abort();
325990b4856Slling 	}
326990b4856Slling 
327990b4856Slling 	if (srctype)
328990b4856Slling 		*srctype = src;
329990b4856Slling 
330990b4856Slling 	return (0);
331990b4856Slling }
332990b4856Slling 
333990b4856Slling /*
334990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
335990b4856Slling  * Assuming bootfs is a valid dataset name.
336990b4856Slling  */
337990b4856Slling static boolean_t
338990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
339990b4856Slling {
340990b4856Slling 	int len = strlen(pool);
341990b4856Slling 
342fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
343990b4856Slling 		return (B_FALSE);
344990b4856Slling 
345990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
346990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
347990b4856Slling 		return (B_TRUE);
348990b4856Slling 
349990b4856Slling 	return (B_FALSE);
350990b4856Slling }
351990b4856Slling 
35215e6edf1Sgw /*
35315e6edf1Sgw  * Inspect the configuration to determine if any of the devices contain
35415e6edf1Sgw  * an EFI label.
35515e6edf1Sgw  */
35615e6edf1Sgw static boolean_t
35715e6edf1Sgw pool_uses_efi(nvlist_t *config)
35815e6edf1Sgw {
35915e6edf1Sgw 	nvlist_t **child;
36015e6edf1Sgw 	uint_t c, children;
36115e6edf1Sgw 
36215e6edf1Sgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
36315e6edf1Sgw 	    &child, &children) != 0)
36415e6edf1Sgw 		return (read_efi_label(config, NULL) >= 0);
36515e6edf1Sgw 
36615e6edf1Sgw 	for (c = 0; c < children; c++) {
36715e6edf1Sgw 		if (pool_uses_efi(child[c]))
36815e6edf1Sgw 			return (B_TRUE);
36915e6edf1Sgw 	}
37015e6edf1Sgw 	return (B_FALSE);
37115e6edf1Sgw }
37215e6edf1Sgw 
3734263d13fSGeorge Wilson boolean_t
3744263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
375b5b76fecSGeorge Wilson {
376b5b76fecSGeorge Wilson 	char bootfs[ZPOOL_MAXNAMELEN];
377b5b76fecSGeorge Wilson 
378b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
379b5b76fecSGeorge Wilson 	    sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
380b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
381b5b76fecSGeorge Wilson }
382b5b76fecSGeorge Wilson 
383b5b76fecSGeorge Wilson 
384990b4856Slling /*
385990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
386990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
387990b4856Slling  * specified as strings.
388990b4856Slling  */
389990b4856Slling static nvlist_t *
3900a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
391f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
392990b4856Slling {
393990b4856Slling 	nvpair_t *elem;
394990b4856Slling 	nvlist_t *retprops;
395990b4856Slling 	zpool_prop_t prop;
396990b4856Slling 	char *strval;
397990b4856Slling 	uint64_t intval;
3988704186eSDan McDonald 	char *slash, *check;
3992f8aaab3Seschrock 	struct stat64 statbuf;
40015e6edf1Sgw 	zpool_handle_t *zhp;
40115e6edf1Sgw 	nvlist_t *nvroot;
402990b4856Slling 
403990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
404990b4856Slling 		(void) no_memory(hdl);
405990b4856Slling 		return (NULL);
406990b4856Slling 	}
407990b4856Slling 
408990b4856Slling 	elem = NULL;
409990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
410990b4856Slling 		const char *propname = nvpair_name(elem);
411990b4856Slling 
412ad135b5dSChristopher Siden 		prop = zpool_name_to_prop(propname);
413ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
414ad135b5dSChristopher Siden 			int err;
415ad135b5dSChristopher Siden 			zfeature_info_t *feature;
416ad135b5dSChristopher Siden 			char *fname = strchr(propname, '@') + 1;
417ad135b5dSChristopher Siden 
418ad135b5dSChristopher Siden 			err = zfeature_lookup_name(fname, &feature);
419ad135b5dSChristopher Siden 			if (err != 0) {
420ad135b5dSChristopher Siden 				ASSERT3U(err, ==, ENOENT);
421ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
422ad135b5dSChristopher Siden 				    "invalid feature '%s'"), fname);
423ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
424ad135b5dSChristopher Siden 				goto error;
425ad135b5dSChristopher Siden 			}
426ad135b5dSChristopher Siden 
427ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
428ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
429ad135b5dSChristopher Siden 				    "'%s' must be a string"), propname);
430ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
431ad135b5dSChristopher Siden 				goto error;
432ad135b5dSChristopher Siden 			}
433ad135b5dSChristopher Siden 
434ad135b5dSChristopher Siden 			(void) nvpair_value_string(elem, &strval);
435ad135b5dSChristopher Siden 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
436ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437ad135b5dSChristopher Siden 				    "property '%s' can only be set to "
438ad135b5dSChristopher Siden 				    "'enabled'"), propname);
439ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
440ad135b5dSChristopher Siden 				goto error;
441ad135b5dSChristopher Siden 			}
442ad135b5dSChristopher Siden 
443ad135b5dSChristopher Siden 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
444ad135b5dSChristopher Siden 				(void) no_memory(hdl);
445ad135b5dSChristopher Siden 				goto error;
446ad135b5dSChristopher Siden 			}
447ad135b5dSChristopher Siden 			continue;
448ad135b5dSChristopher Siden 		}
449ad135b5dSChristopher Siden 
450990b4856Slling 		/*
451990b4856Slling 		 * Make sure this property is valid and applies to this type.
452990b4856Slling 		 */
453ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL) {
454990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
455990b4856Slling 			    "invalid property '%s'"), propname);
456990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
457990b4856Slling 			goto error;
458990b4856Slling 		}
459990b4856Slling 
460990b4856Slling 		if (zpool_prop_readonly(prop)) {
461990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
462990b4856Slling 			    "is readonly"), propname);
463990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
464990b4856Slling 			goto error;
465990b4856Slling 		}
466990b4856Slling 
467990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
468990b4856Slling 		    &strval, &intval, errbuf) != 0)
469990b4856Slling 			goto error;
470990b4856Slling 
471990b4856Slling 		/*
472990b4856Slling 		 * Perform additional checking for specific properties.
473990b4856Slling 		 */
474990b4856Slling 		switch (prop) {
475990b4856Slling 		case ZPOOL_PROP_VERSION:
476ad135b5dSChristopher Siden 			if (intval < version ||
477ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
478990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
479990b4856Slling 				    "property '%s' number %d is invalid."),
480990b4856Slling 				    propname, intval);
481990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
482990b4856Slling 				goto error;
483990b4856Slling 			}
484990b4856Slling 			break;
485990b4856Slling 
486990b4856Slling 		case ZPOOL_PROP_BOOTFS:
487f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
488990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
489990b4856Slling 				    "property '%s' cannot be set at creation "
490990b4856Slling 				    "or import time"), propname);
491990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
492990b4856Slling 				goto error;
493990b4856Slling 			}
494990b4856Slling 
495990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
496990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
497990b4856Slling 				    "pool must be upgraded to support "
498990b4856Slling 				    "'%s' property"), propname);
499990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
500990b4856Slling 				goto error;
501990b4856Slling 			}
502990b4856Slling 
503990b4856Slling 			/*
504990b4856Slling 			 * bootfs property value has to be a dataset name and
505990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
506990b4856Slling 			 */
507990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
508990b4856Slling 			    strval)) {
509990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
510990b4856Slling 				    "is an invalid name"), strval);
511990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
512990b4856Slling 				goto error;
513990b4856Slling 			}
51415e6edf1Sgw 
51515e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
51615e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51715e6edf1Sgw 				    "could not open pool '%s'"), poolname);
51815e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
51915e6edf1Sgw 				goto error;
52015e6edf1Sgw 			}
52115e6edf1Sgw 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
52215e6edf1Sgw 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
52315e6edf1Sgw 
52415e6edf1Sgw 			/*
52515e6edf1Sgw 			 * bootfs property cannot be set on a disk which has
52615e6edf1Sgw 			 * been EFI labeled.
52715e6edf1Sgw 			 */
52815e6edf1Sgw 			if (pool_uses_efi(nvroot)) {
52915e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
53015e6edf1Sgw 				    "property '%s' not supported on "
53115e6edf1Sgw 				    "EFI labeled devices"), propname);
53215e6edf1Sgw 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
53315e6edf1Sgw 				zpool_close(zhp);
53415e6edf1Sgw 				goto error;
53515e6edf1Sgw 			}
53615e6edf1Sgw 			zpool_close(zhp);
537990b4856Slling 			break;
538990b4856Slling 
5392f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
540f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
541990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
542990b4856Slling 				    "property '%s' can only be set during pool "
543990b4856Slling 				    "creation or import"), propname);
544990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
545990b4856Slling 				goto error;
546990b4856Slling 			}
547990b4856Slling 
5482f8aaab3Seschrock 			if (strval[0] != '/') {
549990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5502f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
5512f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
552990b4856Slling 				goto error;
553990b4856Slling 			}
5542f8aaab3Seschrock 			break;
5552f8aaab3Seschrock 
5562f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5572f8aaab3Seschrock 			if (strval[0] == '\0')
5582f8aaab3Seschrock 				break;
5592f8aaab3Seschrock 
5602f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5612f8aaab3Seschrock 				break;
562990b4856Slling 
563990b4856Slling 			if (strval[0] != '/') {
564990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5652f8aaab3Seschrock 				    "property '%s' must be empty, an "
5662f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
567990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
568990b4856Slling 				goto error;
569990b4856Slling 			}
570990b4856Slling 
5712f8aaab3Seschrock 			slash = strrchr(strval, '/');
572990b4856Slling 
5732f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5742f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
5752f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5762f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
5772f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5782f8aaab3Seschrock 				goto error;
5792f8aaab3Seschrock 			}
580990b4856Slling 
5812f8aaab3Seschrock 			*slash = '\0';
5822f8aaab3Seschrock 
5832c32020fSeschrock 			if (strval[0] != '\0' &&
5842c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
5852c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
5862f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5872f8aaab3Seschrock 				    "'%s' is not a valid directory"),
5882f8aaab3Seschrock 				    strval);
5892f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5902f8aaab3Seschrock 				goto error;
5912f8aaab3Seschrock 			}
5922f8aaab3Seschrock 
5932f8aaab3Seschrock 			*slash = '/';
5942f8aaab3Seschrock 			break;
595f9af39baSGeorge Wilson 
5968704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
5978704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
5988704186eSDan McDonald 				if (!isprint(*check)) {
5998704186eSDan McDonald 					zfs_error_aux(hdl,
6008704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
6018704186eSDan McDonald 					    "comment may only have printable "
6028704186eSDan McDonald 					    "characters"));
6038704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
6048704186eSDan McDonald 					    errbuf);
6058704186eSDan McDonald 					goto error;
6068704186eSDan McDonald 				}
6078704186eSDan McDonald 			}
6088704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
6098704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6108704186eSDan McDonald 				    "comment must not exceed %d characters"),
6118704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
6128704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6138704186eSDan McDonald 				goto error;
6148704186eSDan McDonald 			}
6158704186eSDan McDonald 			break;
616f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
617f9af39baSGeorge Wilson 			if (!flags.import) {
618f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
619f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
620f9af39baSGeorge Wilson 				    "import time"), propname);
621f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
622f9af39baSGeorge Wilson 				goto error;
623f9af39baSGeorge Wilson 			}
624f9af39baSGeorge Wilson 			break;
625990b4856Slling 		}
626990b4856Slling 	}
627990b4856Slling 
628990b4856Slling 	return (retprops);
629990b4856Slling error:
630990b4856Slling 	nvlist_free(retprops);
631990b4856Slling 	return (NULL);
632990b4856Slling }
633990b4856Slling 
634990b4856Slling /*
635990b4856Slling  * Set zpool property : propname=propval.
636990b4856Slling  */
637990b4856Slling int
638990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
639990b4856Slling {
640990b4856Slling 	zfs_cmd_t zc = { 0 };
641990b4856Slling 	int ret = -1;
642990b4856Slling 	char errbuf[1024];
643990b4856Slling 	nvlist_t *nvl = NULL;
644990b4856Slling 	nvlist_t *realprops;
645990b4856Slling 	uint64_t version;
646f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
647990b4856Slling 
648990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
649990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
650990b4856Slling 	    zhp->zpool_name);
651990b4856Slling 
652990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
653990b4856Slling 		return (no_memory(zhp->zpool_hdl));
654990b4856Slling 
655990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
656990b4856Slling 		nvlist_free(nvl);
657990b4856Slling 		return (no_memory(zhp->zpool_hdl));
658990b4856Slling 	}
659990b4856Slling 
660990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6610a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
662f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
663990b4856Slling 		nvlist_free(nvl);
664990b4856Slling 		return (-1);
665990b4856Slling 	}
666990b4856Slling 
667990b4856Slling 	nvlist_free(nvl);
668990b4856Slling 	nvl = realprops;
669990b4856Slling 
670990b4856Slling 	/*
671990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
672990b4856Slling 	 */
673990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
674990b4856Slling 
675990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
676990b4856Slling 		nvlist_free(nvl);
677990b4856Slling 		return (-1);
678990b4856Slling 	}
679990b4856Slling 
680990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
681990b4856Slling 
682990b4856Slling 	zcmd_free_nvlists(&zc);
683990b4856Slling 	nvlist_free(nvl);
684990b4856Slling 
685990b4856Slling 	if (ret)
686990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
687990b4856Slling 	else
688990b4856Slling 		(void) zpool_props_refresh(zhp);
689990b4856Slling 
690990b4856Slling 	return (ret);
691990b4856Slling }
692990b4856Slling 
693990b4856Slling int
694990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
695990b4856Slling {
696990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
697990b4856Slling 	zprop_list_t *entry;
698990b4856Slling 	char buf[ZFS_MAXPROPLEN];
699ad135b5dSChristopher Siden 	nvlist_t *features = NULL;
700ad135b5dSChristopher Siden 	zprop_list_t **last;
701ad135b5dSChristopher Siden 	boolean_t firstexpand = (NULL == *plp);
702990b4856Slling 
703990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
704990b4856Slling 		return (-1);
705990b4856Slling 
706ad135b5dSChristopher Siden 	last = plp;
707ad135b5dSChristopher Siden 	while (*last != NULL)
708ad135b5dSChristopher Siden 		last = &(*last)->pl_next;
709ad135b5dSChristopher Siden 
710ad135b5dSChristopher Siden 	if ((*plp)->pl_all)
711ad135b5dSChristopher Siden 		features = zpool_get_features(zhp);
712ad135b5dSChristopher Siden 
713ad135b5dSChristopher Siden 	if ((*plp)->pl_all && firstexpand) {
714ad135b5dSChristopher Siden 		for (int i = 0; i < SPA_FEATURES; i++) {
715ad135b5dSChristopher Siden 			zprop_list_t *entry = zfs_alloc(hdl,
716ad135b5dSChristopher Siden 			    sizeof (zprop_list_t));
717ad135b5dSChristopher Siden 			entry->pl_prop = ZPROP_INVAL;
718ad135b5dSChristopher Siden 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
719ad135b5dSChristopher Siden 			    spa_feature_table[i].fi_uname);
720ad135b5dSChristopher Siden 			entry->pl_width = strlen(entry->pl_user_prop);
721ad135b5dSChristopher Siden 			entry->pl_all = B_TRUE;
722ad135b5dSChristopher Siden 
723ad135b5dSChristopher Siden 			*last = entry;
724ad135b5dSChristopher Siden 			last = &entry->pl_next;
725ad135b5dSChristopher Siden 		}
726ad135b5dSChristopher Siden 	}
727ad135b5dSChristopher Siden 
728ad135b5dSChristopher Siden 	/* add any unsupported features */
729ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
730ad135b5dSChristopher Siden 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
731ad135b5dSChristopher Siden 		char *propname;
732ad135b5dSChristopher Siden 		boolean_t found;
733ad135b5dSChristopher Siden 		zprop_list_t *entry;
734ad135b5dSChristopher Siden 
735ad135b5dSChristopher Siden 		if (zfeature_is_supported(nvpair_name(nvp)))
736ad135b5dSChristopher Siden 			continue;
737ad135b5dSChristopher Siden 
738ad135b5dSChristopher Siden 		propname = zfs_asprintf(hdl, "unsupported@%s",
739ad135b5dSChristopher Siden 		    nvpair_name(nvp));
740ad135b5dSChristopher Siden 
741ad135b5dSChristopher Siden 		/*
742ad135b5dSChristopher Siden 		 * Before adding the property to the list make sure that no
743ad135b5dSChristopher Siden 		 * other pool already added the same property.
744ad135b5dSChristopher Siden 		 */
745ad135b5dSChristopher Siden 		found = B_FALSE;
746ad135b5dSChristopher Siden 		entry = *plp;
747ad135b5dSChristopher Siden 		while (entry != NULL) {
748ad135b5dSChristopher Siden 			if (entry->pl_user_prop != NULL &&
749ad135b5dSChristopher Siden 			    strcmp(propname, entry->pl_user_prop) == 0) {
750ad135b5dSChristopher Siden 				found = B_TRUE;
751ad135b5dSChristopher Siden 				break;
752ad135b5dSChristopher Siden 			}
753ad135b5dSChristopher Siden 			entry = entry->pl_next;
754ad135b5dSChristopher Siden 		}
755ad135b5dSChristopher Siden 		if (found) {
756ad135b5dSChristopher Siden 			free(propname);
757ad135b5dSChristopher Siden 			continue;
758ad135b5dSChristopher Siden 		}
759ad135b5dSChristopher Siden 
760ad135b5dSChristopher Siden 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
761ad135b5dSChristopher Siden 		entry->pl_prop = ZPROP_INVAL;
762ad135b5dSChristopher Siden 		entry->pl_user_prop = propname;
763ad135b5dSChristopher Siden 		entry->pl_width = strlen(entry->pl_user_prop);
764ad135b5dSChristopher Siden 		entry->pl_all = B_TRUE;
765ad135b5dSChristopher Siden 
766ad135b5dSChristopher Siden 		*last = entry;
767ad135b5dSChristopher Siden 		last = &entry->pl_next;
768ad135b5dSChristopher Siden 	}
769ad135b5dSChristopher Siden 
770990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
771990b4856Slling 
772990b4856Slling 		if (entry->pl_fixed)
773990b4856Slling 			continue;
774990b4856Slling 
775990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
776990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
777990b4856Slling 		    NULL) == 0) {
778990b4856Slling 			if (strlen(buf) > entry->pl_width)
779990b4856Slling 				entry->pl_width = strlen(buf);
780990b4856Slling 		}
781990b4856Slling 	}
782990b4856Slling 
783990b4856Slling 	return (0);
784990b4856Slling }
785990b4856Slling 
786ad135b5dSChristopher Siden /*
787ad135b5dSChristopher Siden  * Get the state for the given feature on the given ZFS pool.
788ad135b5dSChristopher Siden  */
789ad135b5dSChristopher Siden int
790ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
791ad135b5dSChristopher Siden     size_t len)
792ad135b5dSChristopher Siden {
793ad135b5dSChristopher Siden 	uint64_t refcount;
794ad135b5dSChristopher Siden 	boolean_t found = B_FALSE;
795ad135b5dSChristopher Siden 	nvlist_t *features = zpool_get_features(zhp);
796ad135b5dSChristopher Siden 	boolean_t supported;
797ad135b5dSChristopher Siden 	const char *feature = strchr(propname, '@') + 1;
798ad135b5dSChristopher Siden 
799ad135b5dSChristopher Siden 	supported = zpool_prop_feature(propname);
800ad135b5dSChristopher Siden 	ASSERT(supported || zfs_prop_unsupported(propname));
801ad135b5dSChristopher Siden 
802ad135b5dSChristopher Siden 	/*
803ad135b5dSChristopher Siden 	 * Convert from feature name to feature guid. This conversion is
804ad135b5dSChristopher Siden 	 * unecessary for unsupported@... properties because they already
805ad135b5dSChristopher Siden 	 * use guids.
806ad135b5dSChristopher Siden 	 */
807ad135b5dSChristopher Siden 	if (supported) {
808ad135b5dSChristopher Siden 		int ret;
809ad135b5dSChristopher Siden 		zfeature_info_t *fi;
810ad135b5dSChristopher Siden 
811ad135b5dSChristopher Siden 		ret = zfeature_lookup_name(feature, &fi);
812ad135b5dSChristopher Siden 		if (ret != 0) {
813ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
814ad135b5dSChristopher Siden 			return (ENOTSUP);
815ad135b5dSChristopher Siden 		}
816ad135b5dSChristopher Siden 		feature = fi->fi_guid;
817ad135b5dSChristopher Siden 	}
818ad135b5dSChristopher Siden 
819ad135b5dSChristopher Siden 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
820ad135b5dSChristopher Siden 		found = B_TRUE;
821ad135b5dSChristopher Siden 
822ad135b5dSChristopher Siden 	if (supported) {
823ad135b5dSChristopher Siden 		if (!found) {
824ad135b5dSChristopher Siden 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
825ad135b5dSChristopher Siden 		} else  {
826ad135b5dSChristopher Siden 			if (refcount == 0)
827ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
828ad135b5dSChristopher Siden 			else
829ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
830ad135b5dSChristopher Siden 		}
831ad135b5dSChristopher Siden 	} else {
832ad135b5dSChristopher Siden 		if (found) {
833ad135b5dSChristopher Siden 			if (refcount == 0) {
834ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
835ad135b5dSChristopher Siden 			} else {
836ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
837ad135b5dSChristopher Siden 			}
838ad135b5dSChristopher Siden 		} else {
839ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
840ad135b5dSChristopher Siden 			return (ENOTSUP);
841ad135b5dSChristopher Siden 		}
842ad135b5dSChristopher Siden 	}
843ad135b5dSChristopher Siden 
844ad135b5dSChristopher Siden 	return (0);
845ad135b5dSChristopher Siden }
846990b4856Slling 
847573ca77eSGeorge Wilson /*
848573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
849573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
850573ca77eSGeorge Wilson  */
851573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
852573ca77eSGeorge Wilson 
853fa9e4066Sahrens /*
854fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
855fa9e4066Sahrens  * 'buf'.
856fa9e4066Sahrens  */
857e7cbe64fSgw boolean_t
85899653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
859fa9e4066Sahrens {
860fa9e4066Sahrens 	namecheck_err_t why;
861fa9e4066Sahrens 	char what;
862b468a217Seschrock 	int ret;
863b468a217Seschrock 
864b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
865b468a217Seschrock 
866b468a217Seschrock 	/*
867b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
868b468a217Seschrock 	 * But we need to support users with existing pools that may now be
869b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
870b468a217Seschrock 	 * create (or import), and only in userland.
871b468a217Seschrock 	 */
872b468a217Seschrock 	if (ret == 0 && !isopen &&
873b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
874b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
8758654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
8768654d025Sperrin 	    strcmp(pool, "log") == 0)) {
877e7cbe64fSgw 		if (hdl != NULL)
878e7cbe64fSgw 			zfs_error_aux(hdl,
879e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
88099653d4eSeschrock 		return (B_FALSE);
881b468a217Seschrock 	}
882b468a217Seschrock 
883fa9e4066Sahrens 
884b468a217Seschrock 	if (ret != 0) {
88599653d4eSeschrock 		if (hdl != NULL) {
886fa9e4066Sahrens 			switch (why) {
887b81d61a6Slling 			case NAME_ERR_TOOLONG:
88899653d4eSeschrock 				zfs_error_aux(hdl,
889b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
890b81d61a6Slling 				break;
891b81d61a6Slling 
892fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
89399653d4eSeschrock 				zfs_error_aux(hdl,
894fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
895fa9e4066Sahrens 				    "'%c' in pool name"), what);
896fa9e4066Sahrens 				break;
897fa9e4066Sahrens 
898fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
89999653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90099653d4eSeschrock 				    "name must begin with a letter"));
901fa9e4066Sahrens 				break;
902fa9e4066Sahrens 
903fa9e4066Sahrens 			case NAME_ERR_RESERVED:
90499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90599653d4eSeschrock 				    "name is reserved"));
906fa9e4066Sahrens 				break;
907fa9e4066Sahrens 
908fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
90999653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
91099653d4eSeschrock 				    "pool name is reserved"));
911fa9e4066Sahrens 				break;
9125ad82045Snd 
9135ad82045Snd 			case NAME_ERR_LEADING_SLASH:
9145ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9155ad82045Snd 				    "leading slash in name"));
9165ad82045Snd 				break;
9175ad82045Snd 
9185ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
9195ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9205ad82045Snd 				    "empty component in name"));
9215ad82045Snd 				break;
9225ad82045Snd 
9235ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
9245ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9255ad82045Snd 				    "trailing slash in name"));
9265ad82045Snd 				break;
9275ad82045Snd 
9285ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
9295ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9305ad82045Snd 				    "multiple '@' delimiters in name"));
9315ad82045Snd 				break;
9325ad82045Snd 
933fa9e4066Sahrens 			}
934fa9e4066Sahrens 		}
93599653d4eSeschrock 		return (B_FALSE);
936fa9e4066Sahrens 	}
937fa9e4066Sahrens 
93899653d4eSeschrock 	return (B_TRUE);
939fa9e4066Sahrens }
940fa9e4066Sahrens 
941fa9e4066Sahrens /*
942fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
943fa9e4066Sahrens  * state.
944fa9e4066Sahrens  */
945fa9e4066Sahrens zpool_handle_t *
94699653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
947fa9e4066Sahrens {
948fa9e4066Sahrens 	zpool_handle_t *zhp;
94994de1d4cSeschrock 	boolean_t missing;
950fa9e4066Sahrens 
951fa9e4066Sahrens 	/*
952fa9e4066Sahrens 	 * Make sure the pool name is valid.
953fa9e4066Sahrens 	 */
95499653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
955ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
95699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
95799653d4eSeschrock 		    pool);
958fa9e4066Sahrens 		return (NULL);
959fa9e4066Sahrens 	}
960fa9e4066Sahrens 
96199653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
96299653d4eSeschrock 		return (NULL);
963fa9e4066Sahrens 
96499653d4eSeschrock 	zhp->zpool_hdl = hdl;
965fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
966fa9e4066Sahrens 
96794de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
96894de1d4cSeschrock 		zpool_close(zhp);
96994de1d4cSeschrock 		return (NULL);
97094de1d4cSeschrock 	}
97194de1d4cSeschrock 
97294de1d4cSeschrock 	if (missing) {
973990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
974ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
975990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
97694de1d4cSeschrock 		zpool_close(zhp);
97794de1d4cSeschrock 		return (NULL);
978fa9e4066Sahrens 	}
979fa9e4066Sahrens 
980fa9e4066Sahrens 	return (zhp);
981fa9e4066Sahrens }
982fa9e4066Sahrens 
983fa9e4066Sahrens /*
984fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
985fa9e4066Sahrens  * the configuration cache may be out of date).
986fa9e4066Sahrens  */
98794de1d4cSeschrock int
98894de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
989fa9e4066Sahrens {
990fa9e4066Sahrens 	zpool_handle_t *zhp;
99194de1d4cSeschrock 	boolean_t missing;
992fa9e4066Sahrens 
99394de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
99494de1d4cSeschrock 		return (-1);
995fa9e4066Sahrens 
99699653d4eSeschrock 	zhp->zpool_hdl = hdl;
997fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
998fa9e4066Sahrens 
99994de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
100094de1d4cSeschrock 		zpool_close(zhp);
100194de1d4cSeschrock 		return (-1);
1002fa9e4066Sahrens 	}
1003fa9e4066Sahrens 
100494de1d4cSeschrock 	if (missing) {
100594de1d4cSeschrock 		zpool_close(zhp);
100694de1d4cSeschrock 		*ret = NULL;
100794de1d4cSeschrock 		return (0);
100894de1d4cSeschrock 	}
100994de1d4cSeschrock 
101094de1d4cSeschrock 	*ret = zhp;
101194de1d4cSeschrock 	return (0);
1012fa9e4066Sahrens }
1013fa9e4066Sahrens 
1014fa9e4066Sahrens /*
1015fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1016fa9e4066Sahrens  * state.
1017fa9e4066Sahrens  */
1018fa9e4066Sahrens zpool_handle_t *
101999653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1020fa9e4066Sahrens {
1021fa9e4066Sahrens 	zpool_handle_t *zhp;
1022fa9e4066Sahrens 
102399653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1024fa9e4066Sahrens 		return (NULL);
1025fa9e4066Sahrens 
1026fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1027ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
102899653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1029fa9e4066Sahrens 		zpool_close(zhp);
1030fa9e4066Sahrens 		return (NULL);
1031fa9e4066Sahrens 	}
1032fa9e4066Sahrens 
1033fa9e4066Sahrens 	return (zhp);
1034fa9e4066Sahrens }
1035fa9e4066Sahrens 
1036fa9e4066Sahrens /*
1037fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
1038fa9e4066Sahrens  */
1039fa9e4066Sahrens void
1040fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1041fa9e4066Sahrens {
1042fa9e4066Sahrens 	if (zhp->zpool_config)
1043fa9e4066Sahrens 		nvlist_free(zhp->zpool_config);
1044088e9d47Seschrock 	if (zhp->zpool_old_config)
1045088e9d47Seschrock 		nvlist_free(zhp->zpool_old_config);
1046b1b8ab34Slling 	if (zhp->zpool_props)
1047b1b8ab34Slling 		nvlist_free(zhp->zpool_props);
1048fa9e4066Sahrens 	free(zhp);
1049fa9e4066Sahrens }
1050fa9e4066Sahrens 
1051fa9e4066Sahrens /*
1052fa9e4066Sahrens  * Return the name of the pool.
1053fa9e4066Sahrens  */
1054fa9e4066Sahrens const char *
1055fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1056fa9e4066Sahrens {
1057fa9e4066Sahrens 	return (zhp->zpool_name);
1058fa9e4066Sahrens }
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 
1061fa9e4066Sahrens /*
1062fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1063fa9e4066Sahrens  */
1064fa9e4066Sahrens int
1065fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1066fa9e4066Sahrens {
1067fa9e4066Sahrens 	return (zhp->zpool_state);
1068fa9e4066Sahrens }
1069fa9e4066Sahrens 
1070fa9e4066Sahrens /*
1071fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
1072fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
1073fa9e4066Sahrens  * don't have to worry about error semantics.
1074fa9e4066Sahrens  */
1075fa9e4066Sahrens int
107699653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
10770a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
1078fa9e4066Sahrens {
1079fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
10800a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
10810a48a24eStimh 	nvlist_t *zc_props = NULL;
108299653d4eSeschrock 	char msg[1024];
1083990b4856Slling 	char *altroot;
10840a48a24eStimh 	int ret = -1;
1085fa9e4066Sahrens 
108699653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
108799653d4eSeschrock 	    "cannot create '%s'"), pool);
1088fa9e4066Sahrens 
108999653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
109099653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1091fa9e4066Sahrens 
1092351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1093990b4856Slling 		return (-1);
1094fa9e4066Sahrens 
10950a48a24eStimh 	if (props) {
1096f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1097f9af39baSGeorge Wilson 
10980a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1099f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
11000a48a24eStimh 			goto create_failed;
11010a48a24eStimh 		}
11020a48a24eStimh 	}
110399653d4eSeschrock 
11040a48a24eStimh 	if (fsprops) {
11050a48a24eStimh 		uint64_t zoned;
11060a48a24eStimh 		char *zonestr;
11070a48a24eStimh 
11080a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
11090a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
11100a48a24eStimh 		    strcmp(zonestr, "on") == 0);
11110a48a24eStimh 
11120a48a24eStimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
11130a48a24eStimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
11140a48a24eStimh 			goto create_failed;
11150a48a24eStimh 		}
11160a48a24eStimh 		if (!zc_props &&
11170a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
11180a48a24eStimh 			goto create_failed;
11190a48a24eStimh 		}
11200a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
11210a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
11220a48a24eStimh 			goto create_failed;
11230a48a24eStimh 		}
1124351420b3Slling 	}
1125fa9e4066Sahrens 
11260a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
11270a48a24eStimh 		goto create_failed;
11280a48a24eStimh 
1129990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1130fa9e4066Sahrens 
11310a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1132351420b3Slling 
1133e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
11340a48a24eStimh 		nvlist_free(zc_props);
11350a48a24eStimh 		nvlist_free(zc_fsprops);
1136fa9e4066Sahrens 
113799653d4eSeschrock 		switch (errno) {
1138fa9e4066Sahrens 		case EBUSY:
1139fa9e4066Sahrens 			/*
1140fa9e4066Sahrens 			 * This can happen if the user has specified the same
1141fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1142fa9e4066Sahrens 			 * until we try to add it and see we already have a
1143fa9e4066Sahrens 			 * label.
1144fa9e4066Sahrens 			 */
114599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114699653d4eSeschrock 			    "one or more vdevs refer to the same device"));
114799653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1148fa9e4066Sahrens 
1149fa9e4066Sahrens 		case EOVERFLOW:
1150fa9e4066Sahrens 			/*
115199653d4eSeschrock 			 * This occurs when one of the devices is below
1152fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1153fa9e4066Sahrens 			 * device was the problem device since there's no
1154fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1155fa9e4066Sahrens 			 */
1156fa9e4066Sahrens 			{
1157fa9e4066Sahrens 				char buf[64];
1158fa9e4066Sahrens 
1159fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1160fa9e4066Sahrens 
116199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116299653d4eSeschrock 				    "one or more devices is less than the "
116399653d4eSeschrock 				    "minimum size (%s)"), buf);
1164fa9e4066Sahrens 			}
116599653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1166fa9e4066Sahrens 
1167fa9e4066Sahrens 		case ENOSPC:
116899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116999653d4eSeschrock 			    "one or more devices is out of space"));
117099653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1171fa9e4066Sahrens 
1172fa94a07fSbrendan 		case ENOTBLK:
1173fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1174fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1175fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1176fa94a07fSbrendan 
1177fa9e4066Sahrens 		default:
117899653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1179fa9e4066Sahrens 		}
1180fa9e4066Sahrens 	}
1181fa9e4066Sahrens 
1182fa9e4066Sahrens 	/*
1183fa9e4066Sahrens 	 * If this is an alternate root pool, then we automatically set the
1184e9dbad6fSeschrock 	 * mountpoint of the root dataset to be '/'.
1185fa9e4066Sahrens 	 */
1186990b4856Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1187990b4856Slling 	    &altroot) == 0) {
1188fa9e4066Sahrens 		zfs_handle_t *zhp;
1189fa9e4066Sahrens 
1190990b4856Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1191e9dbad6fSeschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1192e9dbad6fSeschrock 		    "/") == 0);
1193fa9e4066Sahrens 
1194fa9e4066Sahrens 		zfs_close(zhp);
1195fa9e4066Sahrens 	}
1196fa9e4066Sahrens 
11970a48a24eStimh create_failed:
1198351420b3Slling 	zcmd_free_nvlists(&zc);
11990a48a24eStimh 	nvlist_free(zc_props);
12000a48a24eStimh 	nvlist_free(zc_fsprops);
12010a48a24eStimh 	return (ret);
1202fa9e4066Sahrens }
1203fa9e4066Sahrens 
1204fa9e4066Sahrens /*
1205fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1206fa9e4066Sahrens  * datasets left in the pool.
1207fa9e4066Sahrens  */
1208fa9e4066Sahrens int
1209*4445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1210fa9e4066Sahrens {
1211fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1212fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
121399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
121499653d4eSeschrock 	char msg[1024];
1215fa9e4066Sahrens 
1216fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1217cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1218fa9e4066Sahrens 		return (-1);
1219fa9e4066Sahrens 
1220fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1221*4445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1222fa9e4066Sahrens 
1223cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
122499653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
122599653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1226fa9e4066Sahrens 
122799653d4eSeschrock 		if (errno == EROFS) {
122899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
122999653d4eSeschrock 			    "one or more devices is read only"));
123099653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
123199653d4eSeschrock 		} else {
123299653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1233fa9e4066Sahrens 		}
1234fa9e4066Sahrens 
1235fa9e4066Sahrens 		if (zfp)
1236fa9e4066Sahrens 			zfs_close(zfp);
1237fa9e4066Sahrens 		return (-1);
1238fa9e4066Sahrens 	}
1239fa9e4066Sahrens 
1240fa9e4066Sahrens 	if (zfp) {
1241fa9e4066Sahrens 		remove_mountpoint(zfp);
1242fa9e4066Sahrens 		zfs_close(zfp);
1243fa9e4066Sahrens 	}
1244fa9e4066Sahrens 
1245fa9e4066Sahrens 	return (0);
1246fa9e4066Sahrens }
1247fa9e4066Sahrens 
1248fa9e4066Sahrens /*
1249fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1250fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1251fa9e4066Sahrens  */
1252fa9e4066Sahrens int
1253fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1254fa9e4066Sahrens {
1255e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
125699653d4eSeschrock 	int ret;
125799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
125899653d4eSeschrock 	char msg[1024];
1259fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1260fa94a07fSbrendan 	uint_t nspares, nl2cache;
126199653d4eSeschrock 
126299653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
126399653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
126499653d4eSeschrock 
1265fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1266fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
126799653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
126899653d4eSeschrock 	    &spares, &nspares) == 0) {
126999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
127099653d4eSeschrock 		    "upgraded to add hot spares"));
127199653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
127299653d4eSeschrock 	}
1273fa9e4066Sahrens 
12744263d13fSGeorge Wilson 	if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1275b5b76fecSGeorge Wilson 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1276b5b76fecSGeorge Wilson 		uint64_t s;
1277b5b76fecSGeorge Wilson 
1278b5b76fecSGeorge Wilson 		for (s = 0; s < nspares; s++) {
1279b5b76fecSGeorge Wilson 			char *path;
1280b5b76fecSGeorge Wilson 
1281b5b76fecSGeorge Wilson 			if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1282b5b76fecSGeorge Wilson 			    &path) == 0 && pool_uses_efi(spares[s])) {
1283b5b76fecSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1284b5b76fecSGeorge Wilson 				    "device '%s' contains an EFI label and "
1285b5b76fecSGeorge Wilson 				    "cannot be used on root pools."),
128688ecc943SGeorge Wilson 				    zpool_vdev_name(hdl, NULL, spares[s],
128788ecc943SGeorge Wilson 				    B_FALSE));
1288b5b76fecSGeorge Wilson 				return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1289b5b76fecSGeorge Wilson 			}
1290b5b76fecSGeorge Wilson 		}
1291b5b76fecSGeorge Wilson 	}
1292b5b76fecSGeorge Wilson 
1293fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1294fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1295fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1296fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1297fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1298fa94a07fSbrendan 		    "upgraded to add cache devices"));
1299fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1300fa94a07fSbrendan 	}
1301fa94a07fSbrendan 
1302990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
130399653d4eSeschrock 		return (-1);
1304fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1305fa9e4066Sahrens 
1306cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1307fa9e4066Sahrens 		switch (errno) {
1308fa9e4066Sahrens 		case EBUSY:
1309fa9e4066Sahrens 			/*
1310fa9e4066Sahrens 			 * This can happen if the user has specified the same
1311fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1312fa9e4066Sahrens 			 * until we try to add it and see we already have a
1313fa9e4066Sahrens 			 * label.
1314fa9e4066Sahrens 			 */
131599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
131699653d4eSeschrock 			    "one or more vdevs refer to the same device"));
131799653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1318fa9e4066Sahrens 			break;
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 		case EOVERFLOW:
1321fa9e4066Sahrens 			/*
1322fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1323fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1324fa9e4066Sahrens 			 * device was the problem device since there's no
1325fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1326fa9e4066Sahrens 			 */
1327fa9e4066Sahrens 			{
1328fa9e4066Sahrens 				char buf[64];
1329fa9e4066Sahrens 
1330fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1331fa9e4066Sahrens 
133299653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133399653d4eSeschrock 				    "device is less than the minimum "
133499653d4eSeschrock 				    "size (%s)"), buf);
1335fa9e4066Sahrens 			}
133699653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
133799653d4eSeschrock 			break;
133899653d4eSeschrock 
133999653d4eSeschrock 		case ENOTSUP:
134099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13418654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
134299653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1343fa9e4066Sahrens 			break;
1344fa9e4066Sahrens 
1345b1b8ab34Slling 		case EDOM:
1346b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13478654d025Sperrin 			    "root pool can not have multiple vdevs"
13488654d025Sperrin 			    " or separate logs"));
1349b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1350b1b8ab34Slling 			break;
1351b1b8ab34Slling 
1352fa94a07fSbrendan 		case ENOTBLK:
1353fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1354fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1355fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1356fa94a07fSbrendan 			break;
1357fa94a07fSbrendan 
1358fa9e4066Sahrens 		default:
135999653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1360fa9e4066Sahrens 		}
1361fa9e4066Sahrens 
136299653d4eSeschrock 		ret = -1;
136399653d4eSeschrock 	} else {
136499653d4eSeschrock 		ret = 0;
1365fa9e4066Sahrens 	}
1366fa9e4066Sahrens 
1367e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1368fa9e4066Sahrens 
136999653d4eSeschrock 	return (ret);
1370fa9e4066Sahrens }
1371fa9e4066Sahrens 
1372fa9e4066Sahrens /*
1373fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1374fa9e4066Sahrens  * mounted datasets in the pool.
1375fa9e4066Sahrens  */
1376*4445fffbSMatthew Ahrens static int
1377*4445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1378*4445fffbSMatthew Ahrens     const char *log_str)
1379fa9e4066Sahrens {
1380fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
138189a89ebfSlling 	char msg[1024];
1382fa9e4066Sahrens 
138389a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
138489a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
138589a89ebfSlling 
1386fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
138789a89ebfSlling 	zc.zc_cookie = force;
1388394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
1389*4445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
139089a89ebfSlling 
139189a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
139289a89ebfSlling 		switch (errno) {
139389a89ebfSlling 		case EXDEV:
139489a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
139589a89ebfSlling 			    "use '-f' to override the following errors:\n"
139689a89ebfSlling 			    "'%s' has an active shared spare which could be"
139789a89ebfSlling 			    " used by other pools once '%s' is exported."),
139889a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
139989a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
140089a89ebfSlling 			    msg));
140189a89ebfSlling 		default:
140289a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
140389a89ebfSlling 			    msg));
140489a89ebfSlling 		}
140589a89ebfSlling 	}
1406fa9e4066Sahrens 
1407fa9e4066Sahrens 	return (0);
1408fa9e4066Sahrens }
1409fa9e4066Sahrens 
1410394ab0cbSGeorge Wilson int
1411*4445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1412394ab0cbSGeorge Wilson {
1413*4445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1414394ab0cbSGeorge Wilson }
1415394ab0cbSGeorge Wilson 
1416394ab0cbSGeorge Wilson int
1417*4445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1418394ab0cbSGeorge Wilson {
1419*4445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1420394ab0cbSGeorge Wilson }
1421394ab0cbSGeorge Wilson 
1422468c413aSTim Haley static void
1423468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
14244b964adaSGeorge Wilson     nvlist_t *config)
1425468c413aSTim Haley {
14264b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1427468c413aSTim Haley 	uint64_t rewindto;
1428468c413aSTim Haley 	int64_t loss = -1;
1429468c413aSTim Haley 	struct tm t;
1430468c413aSTim Haley 	char timestr[128];
1431468c413aSTim Haley 
14324b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
14334b964adaSGeorge Wilson 		return;
14344b964adaSGeorge Wilson 
1435ad135b5dSChristopher Siden 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1436ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1437468c413aSTim Haley 		return;
1438ad135b5dSChristopher Siden 	}
1439468c413aSTim Haley 
14404b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1441468c413aSTim Haley 		return;
14424b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1443468c413aSTim Haley 
1444468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1445468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1446468c413aSTim Haley 		if (dryrun) {
1447468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1448468c413aSTim Haley 			    "Would be able to return %s "
1449468c413aSTim Haley 			    "to its state as of %s.\n"),
1450468c413aSTim Haley 			    name, timestr);
1451468c413aSTim Haley 		} else {
1452468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1453468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1454468c413aSTim Haley 			    name, timestr);
1455468c413aSTim Haley 		}
1456468c413aSTim Haley 		if (loss > 120) {
1457468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1458468c413aSTim Haley 			    "%s approximately %lld "),
1459468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1460468c413aSTim Haley 			    (loss + 30) / 60);
1461468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1462468c413aSTim Haley 			    "minutes of transactions.\n"));
1463468c413aSTim Haley 		} else if (loss > 0) {
1464468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1465468c413aSTim Haley 			    "%s approximately %lld "),
1466468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1467468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1468468c413aSTim Haley 			    "seconds of transactions.\n"));
1469468c413aSTim Haley 		}
1470468c413aSTim Haley 	}
1471468c413aSTim Haley }
1472468c413aSTim Haley 
1473468c413aSTim Haley void
1474468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1475468c413aSTim Haley     nvlist_t *config)
1476468c413aSTim Haley {
14774b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1478468c413aSTim Haley 	int64_t loss = -1;
1479468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1480468c413aSTim Haley 	uint64_t rewindto;
1481468c413aSTim Haley 	struct tm t;
1482468c413aSTim Haley 	char timestr[128];
1483468c413aSTim Haley 
1484468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1485468c413aSTim Haley 		return;
1486468c413aSTim Haley 
1487468c413aSTim Haley 	if (reason >= 0)
1488468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1489468c413aSTim Haley 	else
1490468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1491468c413aSTim Haley 
1492468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
14934b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1494ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
14954b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1496468c413aSTim Haley 		goto no_info;
1497468c413aSTim Haley 
14984b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
14994b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1500468c413aSTim Haley 	    &edata);
1501468c413aSTim Haley 
1502468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1503468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1504468c413aSTim Haley 
1505468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1506468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1507468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1508468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1509468c413aSTim Haley 		    "\tshould correct the problem.  "),
1510468c413aSTim Haley 		    timestr);
1511468c413aSTim Haley 	} else {
1512468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1513468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1514468c413aSTim Haley 		    "should correct the problem.\n\t"));
1515468c413aSTim Haley 	}
1516468c413aSTim Haley 
1517468c413aSTim Haley 	if (loss > 120) {
1518468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1519468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1520468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1521468c413aSTim Haley 	} else if (loss > 0) {
1522468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1523468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1524468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1525468c413aSTim Haley 	}
1526468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1527468c413aSTim Haley 		if (edata == 1) {
1528468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1529468c413aSTim Haley 			    "After rewind, at least\n"
1530468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1531468c413aSTim Haley 		} else {
1532468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1533468c413aSTim Haley 			    "After rewind, several\n"
1534468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1535468c413aSTim Haley 		}
1536468c413aSTim Haley 	}
1537468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1538a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1539a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1540468c413aSTim Haley 
1541468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1542468c413aSTim Haley 	    "A scrub of the pool\n"
1543468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1544468c413aSTim Haley 	return;
1545468c413aSTim Haley 
1546468c413aSTim Haley no_info:
1547468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1548468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1549468c413aSTim Haley }
1550468c413aSTim Haley 
1551fa9e4066Sahrens /*
1552990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1553990b4856Slling  * if possible.
1554990b4856Slling  *
1555990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1556990b4856Slling  * new properties value to be set.
1557fa9e4066Sahrens  */
1558fa9e4066Sahrens int
155999653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1560990b4856Slling     char *altroot)
1561990b4856Slling {
1562990b4856Slling 	nvlist_t *props = NULL;
1563990b4856Slling 	int ret;
1564990b4856Slling 
1565990b4856Slling 	if (altroot != NULL) {
1566990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1567990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1568990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1569990b4856Slling 			    newname));
1570990b4856Slling 		}
1571990b4856Slling 
1572990b4856Slling 		if (nvlist_add_string(props,
1573352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1574352d8027SGeorge Wilson 		    nvlist_add_string(props,
1575352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1576990b4856Slling 			nvlist_free(props);
1577990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1578990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1579990b4856Slling 			    newname));
1580990b4856Slling 		}
1581990b4856Slling 	}
1582990b4856Slling 
15834b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
15844b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1585990b4856Slling 	if (props)
1586990b4856Slling 		nvlist_free(props);
1587990b4856Slling 	return (ret);
1588990b4856Slling }
1589990b4856Slling 
15904b964adaSGeorge Wilson static void
15914b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
15924b964adaSGeorge Wilson     int indent)
15934b964adaSGeorge Wilson {
15944b964adaSGeorge Wilson 	nvlist_t **child;
15954b964adaSGeorge Wilson 	uint_t c, children;
15964b964adaSGeorge Wilson 	char *vname;
15974b964adaSGeorge Wilson 	uint64_t is_log = 0;
15984b964adaSGeorge Wilson 
15994b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
16004b964adaSGeorge Wilson 	    &is_log);
16014b964adaSGeorge Wilson 
16024b964adaSGeorge Wilson 	if (name != NULL)
16034b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
16044b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
16054b964adaSGeorge Wilson 
16064b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
16074b964adaSGeorge Wilson 	    &child, &children) != 0)
16084b964adaSGeorge Wilson 		return;
16094b964adaSGeorge Wilson 
16104b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
16114b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
16124b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
16134b964adaSGeorge Wilson 		free(vname);
16144b964adaSGeorge Wilson 	}
16154b964adaSGeorge Wilson }
16164b964adaSGeorge Wilson 
1617ad135b5dSChristopher Siden void
1618ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1619ad135b5dSChristopher Siden {
1620ad135b5dSChristopher Siden 	nvlist_t *nvinfo, *unsup_feat;
1621ad135b5dSChristopher Siden 
1622ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1623ad135b5dSChristopher Siden 	    0);
1624ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1625ad135b5dSChristopher Siden 	    &unsup_feat) == 0);
1626ad135b5dSChristopher Siden 
1627ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1628ad135b5dSChristopher Siden 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1629ad135b5dSChristopher Siden 		char *desc;
1630ad135b5dSChristopher Siden 
1631ad135b5dSChristopher Siden 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1632ad135b5dSChristopher Siden 		verify(nvpair_value_string(nvp, &desc) == 0);
1633ad135b5dSChristopher Siden 
1634ad135b5dSChristopher Siden 		if (strlen(desc) > 0)
1635ad135b5dSChristopher Siden 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1636ad135b5dSChristopher Siden 		else
1637ad135b5dSChristopher Siden 			(void) printf("\t%s\n", nvpair_name(nvp));
1638ad135b5dSChristopher Siden 	}
1639ad135b5dSChristopher Siden }
1640ad135b5dSChristopher Siden 
1641990b4856Slling /*
1642990b4856Slling  * Import the given pool using the known configuration and a list of
1643990b4856Slling  * properties to be set. The configuration should have come from
1644990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1645990b4856Slling  * is imported with a different name.
1646990b4856Slling  */
1647990b4856Slling int
1648990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
16494b964adaSGeorge Wilson     nvlist_t *props, int flags)
1650fa9e4066Sahrens {
1651e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1652468c413aSTim Haley 	zpool_rewind_policy_t policy;
16534b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
16544b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
16554b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1656fa9e4066Sahrens 	char *thename;
1657fa9e4066Sahrens 	char *origname;
1658fa9e4066Sahrens 	int ret;
16594b964adaSGeorge Wilson 	int error = 0;
1660990b4856Slling 	char errbuf[1024];
1661fa9e4066Sahrens 
1662fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1663fa9e4066Sahrens 	    &origname) == 0);
1664fa9e4066Sahrens 
1665990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1666990b4856Slling 	    "cannot import pool '%s'"), origname);
1667990b4856Slling 
1668fa9e4066Sahrens 	if (newname != NULL) {
166999653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1670ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
167199653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
167299653d4eSeschrock 			    newname));
1673fa9e4066Sahrens 		thename = (char *)newname;
1674fa9e4066Sahrens 	} else {
1675fa9e4066Sahrens 		thename = origname;
1676fa9e4066Sahrens 	}
1677fa9e4066Sahrens 
1678990b4856Slling 	if (props) {
1679990b4856Slling 		uint64_t version;
1680f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1681fa9e4066Sahrens 
1682990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1683990b4856Slling 		    &version) == 0);
1684fa9e4066Sahrens 
16850a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1686f9af39baSGeorge Wilson 		    props, version, flags, errbuf)) == NULL) {
1687990b4856Slling 			return (-1);
1688351420b3Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1689351420b3Slling 			nvlist_free(props);
1690990b4856Slling 			return (-1);
1691351420b3Slling 		}
1692990b4856Slling 	}
1693990b4856Slling 
1694990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1695fa9e4066Sahrens 
1696fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1697ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1698fa9e4066Sahrens 
1699351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1700351420b3Slling 		nvlist_free(props);
170199653d4eSeschrock 		return (-1);
1702351420b3Slling 	}
170357f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1704468c413aSTim Haley 		nvlist_free(props);
1705468c413aSTim Haley 		return (-1);
1706468c413aSTim Haley 	}
1707fa9e4066Sahrens 
17084b964adaSGeorge Wilson 	zc.zc_cookie = flags;
17094b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
17104b964adaSGeorge Wilson 	    errno == ENOMEM) {
17114b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
17124b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
17134b964adaSGeorge Wilson 			return (-1);
17144b964adaSGeorge Wilson 		}
17154b964adaSGeorge Wilson 	}
17164b964adaSGeorge Wilson 	if (ret != 0)
17174b964adaSGeorge Wilson 		error = errno;
17184b964adaSGeorge Wilson 
17194b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
17204b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
17214b964adaSGeorge Wilson 
17224b964adaSGeorge Wilson 	if (error) {
1723fa9e4066Sahrens 		char desc[1024];
1724468c413aSTim Haley 
1725468c413aSTim Haley 		/*
1726468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1727468c413aSTim Haley 		 * looks like if we found a best txg
1728468c413aSTim Haley 		 */
17294b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1730468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
17314b964adaSGeorge Wilson 			    B_TRUE, nv);
17324b964adaSGeorge Wilson 			nvlist_free(nv);
1733468c413aSTim Haley 			return (-1);
1734468c413aSTim Haley 		}
1735468c413aSTim Haley 
1736fa9e4066Sahrens 		if (newname == NULL)
1737fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1738fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1739fa9e4066Sahrens 			    thename);
1740fa9e4066Sahrens 		else
1741fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1742fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1743fa9e4066Sahrens 			    origname, thename);
1744fa9e4066Sahrens 
17454b964adaSGeorge Wilson 		switch (error) {
1746ea8dc4b6Seschrock 		case ENOTSUP:
1747ad135b5dSChristopher Siden 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1748ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1749ad135b5dSChristopher Siden 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1750ad135b5dSChristopher Siden 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1751ad135b5dSChristopher Siden 				    "pool uses the following feature(s) not "
1752ad135b5dSChristopher Siden 				    "supported by this system:\n"));
1753ad135b5dSChristopher Siden 				zpool_print_unsup_feat(nv);
1754ad135b5dSChristopher Siden 				if (nvlist_exists(nvinfo,
1755ad135b5dSChristopher Siden 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1756ad135b5dSChristopher Siden 					(void) printf(dgettext(TEXT_DOMAIN,
1757ad135b5dSChristopher Siden 					    "All unsupported features are only "
1758ad135b5dSChristopher Siden 					    "required for writing to the pool."
1759ad135b5dSChristopher Siden 					    "\nThe pool can be imported using "
1760ad135b5dSChristopher Siden 					    "'-o readonly=on'.\n"));
1761ad135b5dSChristopher Siden 				}
1762ad135b5dSChristopher Siden 			}
1763ea8dc4b6Seschrock 			/*
1764ea8dc4b6Seschrock 			 * Unsupported version.
1765ea8dc4b6Seschrock 			 */
176699653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1767ea8dc4b6Seschrock 			break;
1768ea8dc4b6Seschrock 
1769b5989ec7Seschrock 		case EINVAL:
1770b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1771b5989ec7Seschrock 			break;
1772b5989ec7Seschrock 
177354a91118SChris Kirby 		case EROFS:
177454a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177554a91118SChris Kirby 			    "one or more devices is read only"));
177654a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
177754a91118SChris Kirby 			break;
177854a91118SChris Kirby 
17794b964adaSGeorge Wilson 		case ENXIO:
17804b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
17814b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
17824b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
17834b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
17844b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
17854b964adaSGeorge Wilson 				    "The devices below are missing, use "
17864b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
17874b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
17884b964adaSGeorge Wilson 				(void) printf("\n");
17894b964adaSGeorge Wilson 			}
17904b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17914b964adaSGeorge Wilson 			break;
17924b964adaSGeorge Wilson 
17934b964adaSGeorge Wilson 		case EEXIST:
17944b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17954b964adaSGeorge Wilson 			break;
17964b964adaSGeorge Wilson 
1797fa9e4066Sahrens 		default:
17984b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1799468c413aSTim Haley 			zpool_explain_recover(hdl,
18004b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1801468c413aSTim Haley 			break;
1802fa9e4066Sahrens 		}
1803fa9e4066Sahrens 
18044b964adaSGeorge Wilson 		nvlist_free(nv);
1805fa9e4066Sahrens 		ret = -1;
1806fa9e4066Sahrens 	} else {
1807fa9e4066Sahrens 		zpool_handle_t *zhp;
1808ecd6cf80Smarks 
1809fa9e4066Sahrens 		/*
1810fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1811fa9e4066Sahrens 		 */
1812681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
181394de1d4cSeschrock 			ret = -1;
1814681d9761SEric Taylor 		else if (zhp != NULL)
1815fa9e4066Sahrens 			zpool_close(zhp);
1816468c413aSTim Haley 		if (policy.zrp_request &
1817468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1818468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
18194b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1820468c413aSTim Haley 		}
18214b964adaSGeorge Wilson 		nvlist_free(nv);
1822468c413aSTim Haley 		return (0);
1823fa9e4066Sahrens 	}
1824fa9e4066Sahrens 
1825e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1826351420b3Slling 	nvlist_free(props);
1827351420b3Slling 
1828fa9e4066Sahrens 	return (ret);
1829fa9e4066Sahrens }
1830fa9e4066Sahrens 
1831fa9e4066Sahrens /*
18323f9d6ad7SLin Ling  * Scan the pool.
1833fa9e4066Sahrens  */
1834fa9e4066Sahrens int
18353f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1836fa9e4066Sahrens {
1837fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1838fa9e4066Sahrens 	char msg[1024];
183999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1840fa9e4066Sahrens 
1841fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18423f9d6ad7SLin Ling 	zc.zc_cookie = func;
1843fa9e4066Sahrens 
1844cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
18453f9d6ad7SLin Ling 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1846fa9e4066Sahrens 		return (0);
1847fa9e4066Sahrens 
18483f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
18493f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18503f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
18513f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
18523f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18533f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
18543f9d6ad7SLin Ling 		    zc.zc_name);
18553f9d6ad7SLin Ling 	} else {
18563f9d6ad7SLin Ling 		assert(!"unexpected result");
18573f9d6ad7SLin Ling 	}
1858fa9e4066Sahrens 
18593f9d6ad7SLin Ling 	if (errno == EBUSY) {
18603f9d6ad7SLin Ling 		nvlist_t *nvroot;
18613f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
18623f9d6ad7SLin Ling 		uint_t psc;
18633f9d6ad7SLin Ling 
18643f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
18653f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
18663f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
18673f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
18683f9d6ad7SLin Ling 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
18693f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
18703f9d6ad7SLin Ling 		else
18713f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
18723f9d6ad7SLin Ling 	} else if (errno == ENOENT) {
18733f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
18743f9d6ad7SLin Ling 	} else {
187599653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
18763f9d6ad7SLin Ling 	}
1877fa9e4066Sahrens }
1878fa9e4066Sahrens 
18793fdda499SJohn Harres /*
18803fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
18813fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
18823fdda499SJohn Harres  * verification of the s# part.
18833fdda499SJohn Harres  */
18843fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
18853fdda499SJohn Harres 
18863fdda499SJohn Harres /*
18873fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
18883fdda499SJohn Harres  * and the like.
18893fdda499SJohn Harres  */
18903fdda499SJohn Harres static int
18913fdda499SJohn Harres ctd_check_path(char *str) {
18923fdda499SJohn Harres 	/*
18933fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
18943fdda499SJohn Harres 	 */
18953fdda499SJohn Harres 	if (str && str[0] == '/') {
18963fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
18973fdda499SJohn Harres 
18983fdda499SJohn Harres 		/*
18993fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
19003fdda499SJohn Harres 		 * component of the string instead.
19013fdda499SJohn Harres 		 */
19023fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
19033fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
19043fdda499SJohn Harres 				;
19053fdda499SJohn Harres 		}
19063fdda499SJohn Harres 		str = tmp + 1;
19073fdda499SJohn Harres 	}
19083fdda499SJohn Harres 	return (CTD_CHECK(str));
19093fdda499SJohn Harres }
19103fdda499SJohn Harres 
1911a43d325bSek /*
1912573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1913573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1914a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1915a43d325bSek  * spare; but FALSE if its an INUSE spare.
1916a43d325bSek  */
191799653d4eSeschrock static nvlist_t *
1918573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1919573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1920ea8dc4b6Seschrock {
1921ea8dc4b6Seschrock 	uint_t c, children;
1922ea8dc4b6Seschrock 	nvlist_t **child;
192399653d4eSeschrock 	nvlist_t *ret;
1924ee0eb9f2SEric Schrock 	uint64_t is_log;
1925573ca77eSGeorge Wilson 	char *srchkey;
1926573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1927573ca77eSGeorge Wilson 
1928573ca77eSGeorge Wilson 	/* Nothing to look for */
1929573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1930573ca77eSGeorge Wilson 		return (NULL);
1931ea8dc4b6Seschrock 
1932573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1933573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1934573ca77eSGeorge Wilson 
1935573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1936cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1937573ca77eSGeorge Wilson 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1938cb04b873SMark J Musante 			uint64_t srchval, theguid;
1939cb04b873SMark J Musante 
1940cb04b873SMark J Musante 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1941cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1942cb04b873SMark J Musante 			    &theguid) == 0);
1943cb04b873SMark J Musante 			if (theguid == srchval)
1944cb04b873SMark J Musante 				return (nv);
1945573ca77eSGeorge Wilson 		}
1946573ca77eSGeorge Wilson 		break;
1947573ca77eSGeorge Wilson 
1948573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1949573ca77eSGeorge Wilson 		char *srchval, *val;
1950573ca77eSGeorge Wilson 
1951573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
1952573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1953573ca77eSGeorge Wilson 			break;
1954ea8dc4b6Seschrock 
1955ea8dc4b6Seschrock 		/*
19563fdda499SJohn Harres 		 * Search for the requested value. Special cases:
19573fdda499SJohn Harres 		 *
19583fdda499SJohn Harres 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
19593fdda499SJohn Harres 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
19603fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
19613fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
19623fdda499SJohn Harres 		 *
196388ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
1964ea8dc4b6Seschrock 		 */
19653fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
19663fdda499SJohn Harres 		    ctd_check_path(val)) {
1967573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
1968573ca77eSGeorge Wilson 
1969573ca77eSGeorge Wilson 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1970573ca77eSGeorge Wilson 			    &wholedisk);
1971573ca77eSGeorge Wilson 			if (wholedisk) {
19723fdda499SJohn Harres 				int slen = strlen(srchval);
19733fdda499SJohn Harres 				int vlen = strlen(val);
19743fdda499SJohn Harres 
19753fdda499SJohn Harres 				if (slen != vlen - 2)
19763fdda499SJohn Harres 					break;
19773fdda499SJohn Harres 
19783fdda499SJohn Harres 				/*
19793fdda499SJohn Harres 				 * make_leaf_vdev() should only set
19803fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
19813fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
19823fdda499SJohn Harres 				 * room for the indices used next.
19833fdda499SJohn Harres 				 */
19843fdda499SJohn Harres 				ASSERT(vlen >= 6);
19853fdda499SJohn Harres 
19863fdda499SJohn Harres 				/*
19873fdda499SJohn Harres 				 * strings identical except trailing "s0"
19883fdda499SJohn Harres 				 */
19893fdda499SJohn Harres 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
19903fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
19913fdda499SJohn Harres 					return (nv);
19923fdda499SJohn Harres 
1993573ca77eSGeorge Wilson 				/*
19943fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
1995573ca77eSGeorge Wilson 				 */
19963fdda499SJohn Harres 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
19973fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
19983fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
1999573ca77eSGeorge Wilson 					return (nv);
20003fdda499SJohn Harres 
2001573ca77eSGeorge Wilson 				break;
2002573ca77eSGeorge Wilson 			}
200388ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
200488ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
200588ecc943SGeorge Wilson 			uint64_t id, vdev_id;
200688ecc943SGeorge Wilson 
200788ecc943SGeorge Wilson 			/*
200888ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
200988ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
201088ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
201188ecc943SGeorge Wilson 			 */
201288ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
201388ecc943SGeorge Wilson 				return (NULL);
201488ecc943SGeorge Wilson 
201588ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
201688ecc943SGeorge Wilson 				free(type);
201788ecc943SGeorge Wilson 				break;
201888ecc943SGeorge Wilson 			}
201988ecc943SGeorge Wilson 			idx = p + 1;
202088ecc943SGeorge Wilson 			*p = '\0';
202188ecc943SGeorge Wilson 
202288ecc943SGeorge Wilson 			/*
202388ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
202488ecc943SGeorge Wilson 			 */
202588ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
202688ecc943SGeorge Wilson 				free(type);
202788ecc943SGeorge Wilson 				break;
202888ecc943SGeorge Wilson 			}
202988ecc943SGeorge Wilson 
203088ecc943SGeorge Wilson 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
203188ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
203288ecc943SGeorge Wilson 			    strncmp(type, VDEV_TYPE_MIRROR,
203388ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_MIRROR)) == 0);
203488ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
203588ecc943SGeorge Wilson 			    &id) == 0);
203688ecc943SGeorge Wilson 
203788ecc943SGeorge Wilson 			errno = 0;
203888ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
203988ecc943SGeorge Wilson 
204088ecc943SGeorge Wilson 			free(type);
204188ecc943SGeorge Wilson 			if (errno != 0)
204288ecc943SGeorge Wilson 				return (NULL);
204388ecc943SGeorge Wilson 
204488ecc943SGeorge Wilson 			/*
204588ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
204688ecc943SGeorge Wilson 			 */
204788ecc943SGeorge Wilson 			if (vdev_id == id)
204888ecc943SGeorge Wilson 				return (nv);
2049ea8dc4b6Seschrock 		}
2050573ca77eSGeorge Wilson 
2051573ca77eSGeorge Wilson 		/*
2052573ca77eSGeorge Wilson 		 * Common case
2053573ca77eSGeorge Wilson 		 */
2054573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
2055573ca77eSGeorge Wilson 			return (nv);
2056573ca77eSGeorge Wilson 		break;
2057573ca77eSGeorge Wilson 	}
2058573ca77eSGeorge Wilson 
2059573ca77eSGeorge Wilson 	default:
2060573ca77eSGeorge Wilson 		break;
2061ea8dc4b6Seschrock 	}
2062ea8dc4b6Seschrock 
2063ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2064ea8dc4b6Seschrock 	    &child, &children) != 0)
206599653d4eSeschrock 		return (NULL);
2066ea8dc4b6Seschrock 
2067ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
2068573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2069ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
2070ee0eb9f2SEric Schrock 			/*
2071ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
2072ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
2073ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
2074ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
2075ee0eb9f2SEric Schrock 			 */
2076ee0eb9f2SEric Schrock 			if (log != NULL &&
2077ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
2078ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2079ee0eb9f2SEric Schrock 			    is_log) {
2080ee0eb9f2SEric Schrock 				*log = B_TRUE;
2081ee0eb9f2SEric Schrock 			}
2082ea8dc4b6Seschrock 			return (ret);
2083ee0eb9f2SEric Schrock 		}
2084ee0eb9f2SEric Schrock 	}
2085ea8dc4b6Seschrock 
208699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
208799653d4eSeschrock 	    &child, &children) == 0) {
208899653d4eSeschrock 		for (c = 0; c < children; c++) {
2089573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2090ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2091a43d325bSek 				*avail_spare = B_TRUE;
209299653d4eSeschrock 				return (ret);
209399653d4eSeschrock 			}
209499653d4eSeschrock 		}
209599653d4eSeschrock 	}
209699653d4eSeschrock 
2097fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2098fa94a07fSbrendan 	    &child, &children) == 0) {
2099fa94a07fSbrendan 		for (c = 0; c < children; c++) {
2100573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2101ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2102fa94a07fSbrendan 				*l2cache = B_TRUE;
2103fa94a07fSbrendan 				return (ret);
2104fa94a07fSbrendan 			}
2105fa94a07fSbrendan 		}
2106fa94a07fSbrendan 	}
2107fa94a07fSbrendan 
210899653d4eSeschrock 	return (NULL);
2109ea8dc4b6Seschrock }
2110ea8dc4b6Seschrock 
2111573ca77eSGeorge Wilson /*
2112573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
2113573ca77eSGeorge Wilson  * associated vdev.
2114573ca77eSGeorge Wilson  */
2115573ca77eSGeorge Wilson nvlist_t *
2116573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2117573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2118573ca77eSGeorge Wilson {
2119573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
2120573ca77eSGeorge Wilson 
2121573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2122573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2123573ca77eSGeorge Wilson 
2124573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2125573ca77eSGeorge Wilson 	    &nvroot) == 0);
2126573ca77eSGeorge Wilson 
2127573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
2128cb04b873SMark J Musante 	*l2cache = B_FALSE;
2129daeb70e5SMark J Musante 	if (log != NULL)
2130daeb70e5SMark J Musante 		*log = B_FALSE;
2131573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2132573ca77eSGeorge Wilson 	nvlist_free(search);
2133573ca77eSGeorge Wilson 
2134573ca77eSGeorge Wilson 	return (ret);
2135573ca77eSGeorge Wilson }
2136573ca77eSGeorge Wilson 
213788ecc943SGeorge Wilson /*
213888ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
213988ecc943SGeorge Wilson  */
214088ecc943SGeorge Wilson boolean_t
214188ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
214288ecc943SGeorge Wilson {
214388ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
214488ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
214588ecc943SGeorge Wilson 		return (B_TRUE);
214688ecc943SGeorge Wilson 	return (B_FALSE);
214788ecc943SGeorge Wilson }
214888ecc943SGeorge Wilson 
214999653d4eSeschrock nvlist_t *
2150fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2151ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
2152ea8dc4b6Seschrock {
2153ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
2154ea8dc4b6Seschrock 	char *end;
2155573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
2156ea8dc4b6Seschrock 	uint64_t guid;
2157ea8dc4b6Seschrock 
2158573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2159573ca77eSGeorge Wilson 
21600917b783Seschrock 	guid = strtoull(path, &end, 10);
2161ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
2162573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
216388ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
216488ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2165ea8dc4b6Seschrock 	} else if (path[0] != '/') {
2166ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
2167573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2168ea8dc4b6Seschrock 	} else {
2169573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2170ea8dc4b6Seschrock 	}
2171ea8dc4b6Seschrock 
2172ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2173ea8dc4b6Seschrock 	    &nvroot) == 0);
2174ea8dc4b6Seschrock 
2175a43d325bSek 	*avail_spare = B_FALSE;
2176fa94a07fSbrendan 	*l2cache = B_FALSE;
2177ee0eb9f2SEric Schrock 	if (log != NULL)
2178ee0eb9f2SEric Schrock 		*log = B_FALSE;
2179573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2180573ca77eSGeorge Wilson 	nvlist_free(search);
2181573ca77eSGeorge Wilson 
2182573ca77eSGeorge Wilson 	return (ret);
2183a43d325bSek }
2184a43d325bSek 
218519397407SSherry Moore static int
218619397407SSherry Moore vdev_online(nvlist_t *nv)
218719397407SSherry Moore {
218819397407SSherry Moore 	uint64_t ival;
218919397407SSherry Moore 
219019397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
219119397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
219219397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
219319397407SSherry Moore 		return (0);
219419397407SSherry Moore 
219519397407SSherry Moore 	return (1);
219619397407SSherry Moore }
219719397407SSherry Moore 
219819397407SSherry Moore /*
219921ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
220019397407SSherry Moore  */
2201753a6d45SSherry Moore static int
220221ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2203753a6d45SSherry Moore     size_t *bytes_written)
220419397407SSherry Moore {
2205753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
2206753a6d45SSherry Moore 	char *tmppath;
2207753a6d45SSherry Moore 	const char *format;
2208753a6d45SSherry Moore 
2209753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2210753a6d45SSherry Moore 	    &tmppath) != 0)
2211753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2212753a6d45SSherry Moore 
2213753a6d45SSherry Moore 	pos = *bytes_written;
2214753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
2215753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
2216753a6d45SSherry Moore 
2217753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2218753a6d45SSherry Moore 	*bytes_written += rsz;
2219753a6d45SSherry Moore 
2220753a6d45SSherry Moore 	if (rsz >= bytes_left) {
2221753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2222753a6d45SSherry Moore 		if (bytes_left != 0) {
2223753a6d45SSherry Moore 			physpath[pos] = 0;
2224753a6d45SSherry Moore 		}
2225753a6d45SSherry Moore 		return (EZFS_NOSPC);
2226753a6d45SSherry Moore 	}
2227753a6d45SSherry Moore 	return (0);
2228753a6d45SSherry Moore }
2229753a6d45SSherry Moore 
223021ecdf64SLin Ling static int
223121ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
223221ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
223321ecdf64SLin Ling {
223421ecdf64SLin Ling 	char *type;
223521ecdf64SLin Ling 	int ret;
223621ecdf64SLin Ling 
223721ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
223821ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
223921ecdf64SLin Ling 
224021ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
224121ecdf64SLin Ling 		/*
224221ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
224321ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
224421ecdf64SLin Ling 		 * spare device.
224521ecdf64SLin Ling 		 */
224621ecdf64SLin Ling 		if (is_spare) {
224721ecdf64SLin Ling 			uint64_t spare = 0;
224821ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
224921ecdf64SLin Ling 			    &spare);
225021ecdf64SLin Ling 			if (!spare)
225121ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
225221ecdf64SLin Ling 		}
225321ecdf64SLin Ling 
225421ecdf64SLin Ling 		if (vdev_online(nv)) {
225521ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
225621ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
225721ecdf64SLin Ling 				return (ret);
225821ecdf64SLin Ling 		}
225921ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
226021ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
226121ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
226221ecdf64SLin Ling 		nvlist_t **child;
226321ecdf64SLin Ling 		uint_t count;
226421ecdf64SLin Ling 		int i, ret;
226521ecdf64SLin Ling 
226621ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
226721ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
226821ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
226921ecdf64SLin Ling 
227021ecdf64SLin Ling 		for (i = 0; i < count; i++) {
227121ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
227221ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
227321ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
227421ecdf64SLin Ling 				return (ret);
227521ecdf64SLin Ling 		}
227621ecdf64SLin Ling 	}
227721ecdf64SLin Ling 
227821ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
227921ecdf64SLin Ling }
228021ecdf64SLin Ling 
2281753a6d45SSherry Moore /*
2282753a6d45SSherry Moore  * Get phys_path for a root pool config.
2283753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2284753a6d45SSherry Moore  */
2285753a6d45SSherry Moore static int
2286753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2287753a6d45SSherry Moore {
2288753a6d45SSherry Moore 	size_t rsz;
228919397407SSherry Moore 	nvlist_t *vdev_root;
229019397407SSherry Moore 	nvlist_t **child;
229119397407SSherry Moore 	uint_t count;
2292753a6d45SSherry Moore 	char *type;
2293753a6d45SSherry Moore 
2294753a6d45SSherry Moore 	rsz = 0;
2295753a6d45SSherry Moore 
2296753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2297753a6d45SSherry Moore 	    &vdev_root) != 0)
2298753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2299753a6d45SSherry Moore 
2300753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2301753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2302753a6d45SSherry Moore 	    &child, &count) != 0)
2303753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
230419397407SSherry Moore 
230519397407SSherry Moore 	/*
2306753a6d45SSherry Moore 	 * root pool can not have EFI labeled disks and can only have
2307753a6d45SSherry Moore 	 * a single top-level vdev.
230819397407SSherry Moore 	 */
2309753a6d45SSherry Moore 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2310753a6d45SSherry Moore 	    pool_uses_efi(vdev_root))
2311753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
231219397407SSherry Moore 
231321ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
231421ecdf64SLin Ling 	    B_FALSE);
231519397407SSherry Moore 
2316753a6d45SSherry Moore 	/* No online devices */
2317753a6d45SSherry Moore 	if (rsz == 0)
2318753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2319753a6d45SSherry Moore 
232019397407SSherry Moore 	return (0);
232119397407SSherry Moore }
232219397407SSherry Moore 
2323753a6d45SSherry Moore /*
2324753a6d45SSherry Moore  * Get phys_path for a root pool
2325753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2326753a6d45SSherry Moore  */
2327753a6d45SSherry Moore int
2328753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2329753a6d45SSherry Moore {
2330753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2331753a6d45SSherry Moore 	    phypath_size));
2332753a6d45SSherry Moore }
2333753a6d45SSherry Moore 
2334573ca77eSGeorge Wilson /*
2335573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2336573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2337573ca77eSGeorge Wilson  */
2338573ca77eSGeorge Wilson static int
2339573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2340573ca77eSGeorge Wilson {
2341573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2342573ca77eSGeorge Wilson 	char errbuf[1024];
2343573ca77eSGeorge Wilson 	int fd, error;
2344573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2345573ca77eSGeorge Wilson 
2346573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2347573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2348573ca77eSGeorge Wilson 		return (-1);
2349573ca77eSGeorge Wilson 
2350573ca77eSGeorge Wilson 	(void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2351573ca77eSGeorge Wilson 
2352573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2353573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2354573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2355573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2356573ca77eSGeorge Wilson 	}
2357573ca77eSGeorge Wilson 
2358573ca77eSGeorge Wilson 	/*
2359573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2360573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2361573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2362573ca77eSGeorge Wilson 	 */
2363573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2364573ca77eSGeorge Wilson 	(void) close(fd);
2365573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2366573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2367573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2368573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2369573ca77eSGeorge Wilson 	}
2370573ca77eSGeorge Wilson 	return (0);
2371573ca77eSGeorge Wilson }
2372573ca77eSGeorge Wilson 
2373fa9e4066Sahrens /*
23743d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
23753d7072f8Seschrock  * ZFS_ONLINE_* flags.
2376fa9e4066Sahrens  */
2377fa9e4066Sahrens int
23783d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
23793d7072f8Seschrock     vdev_state_t *newstate)
2380fa9e4066Sahrens {
2381fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2382fa9e4066Sahrens 	char msg[1024];
238399653d4eSeschrock 	nvlist_t *tgt;
2384573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
238599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2386fa9e4066Sahrens 
2387573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2388573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2389573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2390573ca77eSGeorge Wilson 	} else {
2391573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2392573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2393573ca77eSGeorge Wilson 	}
2394ea8dc4b6Seschrock 
2395fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2396ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2397573ca77eSGeorge Wilson 	    &islog)) == NULL)
239899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2399fa9e4066Sahrens 
240099653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2401fa9e4066Sahrens 
2402069f55e2SEric Schrock 	if (avail_spare)
2403a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2404a43d325bSek 
2405573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND ||
2406573ca77eSGeorge Wilson 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2407573ca77eSGeorge Wilson 		char *pathname = NULL;
2408573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2409573ca77eSGeorge Wilson 
2410573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2411573ca77eSGeorge Wilson 		    &wholedisk);
2412573ca77eSGeorge Wilson 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2413573ca77eSGeorge Wilson 		    &pathname) == 0);
2414573ca77eSGeorge Wilson 
2415573ca77eSGeorge Wilson 		/*
2416573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2417573ca77eSGeorge Wilson 		 */
2418573ca77eSGeorge Wilson 		if (l2cache) {
2419573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2420573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2421573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2422573ca77eSGeorge Wilson 		}
2423573ca77eSGeorge Wilson 
2424573ca77eSGeorge Wilson 		if (wholedisk) {
2425573ca77eSGeorge Wilson 			pathname += strlen(DISK_ROOT) + 1;
2426cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2427573ca77eSGeorge Wilson 		}
2428573ca77eSGeorge Wilson 	}
2429573ca77eSGeorge Wilson 
24303d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
24313d7072f8Seschrock 	zc.zc_obj = flags;
2432fa9e4066Sahrens 
2433cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
24341195e687SMark J Musante 		if (errno == EINVAL) {
24351195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
24361195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
24371195e687SMark J Musante 			    "instead"), "zpool detach");
24381195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
24391195e687SMark J Musante 		}
24403d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24411195e687SMark J Musante 	}
24423d7072f8Seschrock 
24433d7072f8Seschrock 	*newstate = zc.zc_cookie;
24443d7072f8Seschrock 	return (0);
2445fa9e4066Sahrens }
2446fa9e4066Sahrens 
2447fa9e4066Sahrens /*
2448fa9e4066Sahrens  * Take the specified vdev offline
2449fa9e4066Sahrens  */
2450fa9e4066Sahrens int
24513d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2452fa9e4066Sahrens {
2453fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2454fa9e4066Sahrens 	char msg[1024];
245599653d4eSeschrock 	nvlist_t *tgt;
2456fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
245799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2458fa9e4066Sahrens 
2459ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2460ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2461ea8dc4b6Seschrock 
2462fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2463ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2464ee0eb9f2SEric Schrock 	    NULL)) == NULL)
246599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
246699653d4eSeschrock 
246799653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2468fa9e4066Sahrens 
2469069f55e2SEric Schrock 	if (avail_spare)
2470a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2471a43d325bSek 
24723d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
24733d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
24743d7072f8Seschrock 
2475cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24763d7072f8Seschrock 		return (0);
24773d7072f8Seschrock 
24783d7072f8Seschrock 	switch (errno) {
24793d7072f8Seschrock 	case EBUSY:
24803d7072f8Seschrock 
24813d7072f8Seschrock 		/*
24823d7072f8Seschrock 		 * There are no other replicas of this device.
24833d7072f8Seschrock 		 */
24843d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24853d7072f8Seschrock 
2486e6ca193dSGeorge Wilson 	case EEXIST:
2487e6ca193dSGeorge Wilson 		/*
2488e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2489e6ca193dSGeorge Wilson 		 */
2490e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2491e6ca193dSGeorge Wilson 
24923d7072f8Seschrock 	default:
24933d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24943d7072f8Seschrock 	}
24953d7072f8Seschrock }
24963d7072f8Seschrock 
24973d7072f8Seschrock /*
24983d7072f8Seschrock  * Mark the given vdev faulted.
24993d7072f8Seschrock  */
25003d7072f8Seschrock int
2501069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25023d7072f8Seschrock {
25033d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25043d7072f8Seschrock 	char msg[1024];
25053d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25063d7072f8Seschrock 
25073d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25083d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2509441d80aaSlling 
25103d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25113d7072f8Seschrock 	zc.zc_guid = guid;
25123d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2513069f55e2SEric Schrock 	zc.zc_obj = aux;
25143d7072f8Seschrock 
2515cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2516fa9e4066Sahrens 		return (0);
2517fa9e4066Sahrens 
2518fa9e4066Sahrens 	switch (errno) {
251999653d4eSeschrock 	case EBUSY:
2520fa9e4066Sahrens 
2521fa9e4066Sahrens 		/*
2522fa9e4066Sahrens 		 * There are no other replicas of this device.
2523fa9e4066Sahrens 		 */
252499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2525fa9e4066Sahrens 
252699653d4eSeschrock 	default:
252799653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2528fa9e4066Sahrens 	}
25293d7072f8Seschrock 
25303d7072f8Seschrock }
25313d7072f8Seschrock 
25323d7072f8Seschrock /*
25333d7072f8Seschrock  * Mark the given vdev degraded.
25343d7072f8Seschrock  */
25353d7072f8Seschrock int
2536069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25373d7072f8Seschrock {
25383d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25393d7072f8Seschrock 	char msg[1024];
25403d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25413d7072f8Seschrock 
25423d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25433d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
25443d7072f8Seschrock 
25453d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25463d7072f8Seschrock 	zc.zc_guid = guid;
25473d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2548069f55e2SEric Schrock 	zc.zc_obj = aux;
25493d7072f8Seschrock 
2550cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
25513d7072f8Seschrock 		return (0);
25523d7072f8Seschrock 
25533d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
255499653d4eSeschrock }
255599653d4eSeschrock 
255699653d4eSeschrock /*
255799653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
255899653d4eSeschrock  * a hot spare.
255999653d4eSeschrock  */
256099653d4eSeschrock static boolean_t
256199653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
256299653d4eSeschrock {
256399653d4eSeschrock 	nvlist_t **child;
256499653d4eSeschrock 	uint_t c, children;
256599653d4eSeschrock 	char *type;
256699653d4eSeschrock 
256799653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
256899653d4eSeschrock 	    &children) == 0) {
256999653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
257099653d4eSeschrock 		    &type) == 0);
257199653d4eSeschrock 
257299653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
257399653d4eSeschrock 		    children == 2 && child[which] == tgt)
257499653d4eSeschrock 			return (B_TRUE);
257599653d4eSeschrock 
257699653d4eSeschrock 		for (c = 0; c < children; c++)
257799653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
257899653d4eSeschrock 				return (B_TRUE);
257999653d4eSeschrock 	}
258099653d4eSeschrock 
258199653d4eSeschrock 	return (B_FALSE);
2582fa9e4066Sahrens }
2583fa9e4066Sahrens 
2584fa9e4066Sahrens /*
2585fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
25868654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2587fa9e4066Sahrens  */
2588fa9e4066Sahrens int
2589fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2590fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2591fa9e4066Sahrens {
2592fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2593fa9e4066Sahrens 	char msg[1024];
2594fa9e4066Sahrens 	int ret;
259599653d4eSeschrock 	nvlist_t *tgt;
2596ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2597ee0eb9f2SEric Schrock 	uint64_t val;
2598cb04b873SMark J Musante 	char *newname;
259999653d4eSeschrock 	nvlist_t **child;
260099653d4eSeschrock 	uint_t children;
260199653d4eSeschrock 	nvlist_t *config_root;
260299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26034263d13fSGeorge Wilson 	boolean_t rootpool = zpool_is_bootable(zhp);
2604fa9e4066Sahrens 
2605ea8dc4b6Seschrock 	if (replacing)
2606ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2607ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2608ea8dc4b6Seschrock 	else
2609ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2610ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2611ea8dc4b6Seschrock 
2612b5b76fecSGeorge Wilson 	/*
2613b5b76fecSGeorge Wilson 	 * If this is a root pool, make sure that we're not attaching an
2614b5b76fecSGeorge Wilson 	 * EFI labeled device.
2615b5b76fecSGeorge Wilson 	 */
2616b5b76fecSGeorge Wilson 	if (rootpool && pool_uses_efi(nvroot)) {
2617b5b76fecSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2618b5b76fecSGeorge Wilson 		    "EFI labeled devices are not supported on root pools."));
2619b5b76fecSGeorge Wilson 		return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2620b5b76fecSGeorge Wilson 	}
2621b5b76fecSGeorge Wilson 
2622fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2623ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2624ee0eb9f2SEric Schrock 	    &islog)) == 0)
262599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
262699653d4eSeschrock 
2627a43d325bSek 	if (avail_spare)
262899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
262999653d4eSeschrock 
2630fa94a07fSbrendan 	if (l2cache)
2631fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2632fa94a07fSbrendan 
263399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2634fa9e4066Sahrens 	zc.zc_cookie = replacing;
2635fa9e4066Sahrens 
263699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
263799653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
263899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263999653d4eSeschrock 		    "new device must be a single disk"));
264099653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
264199653d4eSeschrock 	}
264299653d4eSeschrock 
264399653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
264499653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
264599653d4eSeschrock 
264688ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
26470430f8daSeschrock 		return (-1);
26480430f8daSeschrock 
264999653d4eSeschrock 	/*
265099653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
265199653d4eSeschrock 	 * replace it with another hot spare.
265299653d4eSeschrock 	 */
265399653d4eSeschrock 	if (replacing &&
265499653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2655ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2656ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2657ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
265899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
265999653d4eSeschrock 		    "can only be replaced by another hot spare"));
26600430f8daSeschrock 		free(newname);
266199653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
266299653d4eSeschrock 	}
266399653d4eSeschrock 
26640430f8daSeschrock 	free(newname);
26650430f8daSeschrock 
2666990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
266799653d4eSeschrock 		return (-1);
2668fa9e4066Sahrens 
2669cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2670fa9e4066Sahrens 
2671e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2672fa9e4066Sahrens 
2673b5b76fecSGeorge Wilson 	if (ret == 0) {
2674b5b76fecSGeorge Wilson 		if (rootpool) {
267521ecdf64SLin Ling 			/*
267621ecdf64SLin Ling 			 * XXX need a better way to prevent user from
267721ecdf64SLin Ling 			 * booting up a half-baked vdev.
267821ecdf64SLin Ling 			 */
267921ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
268021ecdf64SLin Ling 			    "sure to wait until resilver is done "
268121ecdf64SLin Ling 			    "before rebooting.\n"));
2682b5b76fecSGeorge Wilson 		}
2683fa9e4066Sahrens 		return (0);
2684b5b76fecSGeorge Wilson 	}
2685fa9e4066Sahrens 
2686fa9e4066Sahrens 	switch (errno) {
2687ea8dc4b6Seschrock 	case ENOTSUP:
2688fa9e4066Sahrens 		/*
2689fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2690fa9e4066Sahrens 		 */
26918654d025Sperrin 		if (replacing) {
2692cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2693cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2694cb04b873SMark J Musante 
2695ee0eb9f2SEric Schrock 			if (islog)
26968654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26978654d025Sperrin 				    "cannot replace a log with a spare"));
2698cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2699cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2700cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2701cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
27028654d025Sperrin 			else
27038654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27048654d025Sperrin 				    "cannot replace a replacing device"));
27058654d025Sperrin 		} else {
270699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270799653d4eSeschrock 			    "can only attach to mirrors and top-level "
270899653d4eSeschrock 			    "disks"));
27098654d025Sperrin 		}
271099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2711fa9e4066Sahrens 		break;
2712fa9e4066Sahrens 
2713ea8dc4b6Seschrock 	case EINVAL:
2714fa9e4066Sahrens 		/*
2715fa9e4066Sahrens 		 * The new device must be a single disk.
2716fa9e4066Sahrens 		 */
271799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
271899653d4eSeschrock 		    "new device must be a single disk"));
271999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2720fa9e4066Sahrens 		break;
2721fa9e4066Sahrens 
2722ea8dc4b6Seschrock 	case EBUSY:
272399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
272499653d4eSeschrock 		    new_disk);
272599653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2726fa9e4066Sahrens 		break;
2727fa9e4066Sahrens 
2728ea8dc4b6Seschrock 	case EOVERFLOW:
2729fa9e4066Sahrens 		/*
2730fa9e4066Sahrens 		 * The new device is too small.
2731fa9e4066Sahrens 		 */
273299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
273399653d4eSeschrock 		    "device is too small"));
273499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2735fa9e4066Sahrens 		break;
2736fa9e4066Sahrens 
2737ea8dc4b6Seschrock 	case EDOM:
2738fa9e4066Sahrens 		/*
2739fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2740fa9e4066Sahrens 		 */
274199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
274299653d4eSeschrock 		    "devices have different sector alignment"));
274399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2744fa9e4066Sahrens 		break;
2745fa9e4066Sahrens 
2746ea8dc4b6Seschrock 	case ENAMETOOLONG:
2747fa9e4066Sahrens 		/*
2748fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2749fa9e4066Sahrens 		 */
275099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2751fa9e4066Sahrens 		break;
2752fa9e4066Sahrens 
2753ea8dc4b6Seschrock 	default:
275499653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2755fa9e4066Sahrens 	}
2756fa9e4066Sahrens 
275799653d4eSeschrock 	return (-1);
2758fa9e4066Sahrens }
2759fa9e4066Sahrens 
2760fa9e4066Sahrens /*
2761fa9e4066Sahrens  * Detach the specified device.
2762fa9e4066Sahrens  */
2763fa9e4066Sahrens int
2764fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2765fa9e4066Sahrens {
2766fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2767fa9e4066Sahrens 	char msg[1024];
276899653d4eSeschrock 	nvlist_t *tgt;
2769fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
277099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2771fa9e4066Sahrens 
2772ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2773ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2774ea8dc4b6Seschrock 
2775fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2776ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2777ee0eb9f2SEric Schrock 	    NULL)) == 0)
277899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2779fa9e4066Sahrens 
2780a43d325bSek 	if (avail_spare)
278199653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
278299653d4eSeschrock 
2783fa94a07fSbrendan 	if (l2cache)
2784fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2785fa94a07fSbrendan 
278699653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
278799653d4eSeschrock 
2788ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2789fa9e4066Sahrens 		return (0);
2790fa9e4066Sahrens 
2791fa9e4066Sahrens 	switch (errno) {
2792fa9e4066Sahrens 
2793ea8dc4b6Seschrock 	case ENOTSUP:
2794fa9e4066Sahrens 		/*
2795fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2796fa9e4066Sahrens 		 */
279799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
279899653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2799cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2800fa9e4066Sahrens 		break;
2801fa9e4066Sahrens 
2802ea8dc4b6Seschrock 	case EBUSY:
2803fa9e4066Sahrens 		/*
2804fa9e4066Sahrens 		 * There are no other replicas of this device.
2805fa9e4066Sahrens 		 */
280699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2807fa9e4066Sahrens 		break;
2808fa9e4066Sahrens 
2809ea8dc4b6Seschrock 	default:
281099653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2811ea8dc4b6Seschrock 	}
2812ea8dc4b6Seschrock 
281399653d4eSeschrock 	return (-1);
281499653d4eSeschrock }
281599653d4eSeschrock 
28161195e687SMark J Musante /*
28171195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
28181195e687SMark J Musante  *
28191195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
28201195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
28211195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
28221195e687SMark J Musante  * see if any entry in the schild array matches.
28231195e687SMark J Musante  *
28241195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
28251195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
28261195e687SMark J Musante  */
28271195e687SMark J Musante static int
28281195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
28291195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
28301195e687SMark J Musante {
28311195e687SMark J Musante 	uint_t mc;
28321195e687SMark J Musante 
28331195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
28341195e687SMark J Musante 		uint_t sc;
28351195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28361195e687SMark J Musante 		    mchild[mc], B_FALSE);
28371195e687SMark J Musante 
28381195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
28391195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28401195e687SMark J Musante 			    schild[sc], B_FALSE);
28411195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
28421195e687SMark J Musante 
28431195e687SMark J Musante 			free(spath);
28441195e687SMark J Musante 			if (result) {
28451195e687SMark J Musante 				free(mpath);
28461195e687SMark J Musante 				return (mc);
28471195e687SMark J Musante 			}
28481195e687SMark J Musante 		}
28491195e687SMark J Musante 
28501195e687SMark J Musante 		free(mpath);
28511195e687SMark J Musante 	}
28521195e687SMark J Musante 
28531195e687SMark J Musante 	return (-1);
28541195e687SMark J Musante }
28551195e687SMark J Musante 
28561195e687SMark J Musante /*
28571195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
28581195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
28591195e687SMark J Musante  */
28601195e687SMark J Musante int
28611195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
28621195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
28631195e687SMark J Musante {
28641195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
28651195e687SMark J Musante 	char msg[1024];
28661195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
28671195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
28681195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
28691195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
28701195e687SMark J Musante 	uint64_t vers;
28711195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
28721195e687SMark J Musante 	int retval = 0;
28731195e687SMark J Musante 
28741195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
28751195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
28761195e687SMark J Musante 
28771195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
28781195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
28791195e687SMark J Musante 
28801195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
28811195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
28821195e687SMark J Musante 		    "retrieve pool configuration\n"));
28831195e687SMark J Musante 		return (-1);
28841195e687SMark J Musante 	}
28851195e687SMark J Musante 
28861195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
28871195e687SMark J Musante 	    == 0);
28881195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
28891195e687SMark J Musante 
28901195e687SMark J Musante 	if (props) {
2891f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
28921195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2893f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
28941195e687SMark J Musante 			return (-1);
28951195e687SMark J Musante 	}
28961195e687SMark J Musante 
28971195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
28981195e687SMark J Musante 	    &children) != 0) {
28991195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29001195e687SMark J Musante 		    "Source pool is missing vdev tree"));
29011195e687SMark J Musante 		if (zc_props)
29021195e687SMark J Musante 			nvlist_free(zc_props);
29031195e687SMark J Musante 		return (-1);
29041195e687SMark J Musante 	}
29051195e687SMark J Musante 
29061195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
29071195e687SMark J Musante 	vcount = 0;
29081195e687SMark J Musante 
29091195e687SMark J Musante 	if (*newroot == NULL ||
29101195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
29111195e687SMark J Musante 	    &newchild, &newchildren) != 0)
29121195e687SMark J Musante 		newchildren = 0;
29131195e687SMark J Musante 
29141195e687SMark J Musante 	for (c = 0; c < children; c++) {
29151195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
29161195e687SMark J Musante 		char *type;
29171195e687SMark J Musante 		nvlist_t **mchild, *vdev;
29181195e687SMark J Musante 		uint_t mchildren;
29191195e687SMark J Musante 		int entry;
29201195e687SMark J Musante 
29211195e687SMark J Musante 		/*
29221195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
29231195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
29241195e687SMark J Musante 		 */
29251195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
29261195e687SMark J Musante 		    &is_log);
29271195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
29281195e687SMark J Musante 		    &is_hole);
29291195e687SMark J Musante 		if (is_log || is_hole) {
29301195e687SMark J Musante 			/*
29311195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
29321195e687SMark J Musante 			 */
29331195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
29341195e687SMark J Musante 				goto out;
29351195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
29361195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
29371195e687SMark J Musante 				goto out;
29381195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
29391195e687SMark J Musante 			    1) != 0)
29401195e687SMark J Musante 				goto out;
29411195e687SMark J Musante 			if (lastlog == 0)
29421195e687SMark J Musante 				lastlog = vcount;
29431195e687SMark J Musante 			varray[vcount++] = vdev;
29441195e687SMark J Musante 			continue;
29451195e687SMark J Musante 		}
29461195e687SMark J Musante 		lastlog = 0;
29471195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
29481195e687SMark J Musante 		    == 0);
29491195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
29501195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29511195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
29521195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29531195e687SMark J Musante 			goto out;
29541195e687SMark J Musante 		}
29551195e687SMark J Musante 
29561195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
29571195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
29581195e687SMark J Musante 
29591195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
29601195e687SMark J Musante 		if (newchildren > 0 &&
29611195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
29621195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
29631195e687SMark J Musante 			/* We found a disk that the user specified. */
29641195e687SMark J Musante 			vdev = mchild[entry];
29651195e687SMark J Musante 			++found;
29661195e687SMark J Musante 		} else {
29671195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
29681195e687SMark J Musante 			vdev = mchild[mchildren - 1];
29691195e687SMark J Musante 		}
29701195e687SMark J Musante 
29711195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
29721195e687SMark J Musante 			goto out;
29731195e687SMark J Musante 	}
29741195e687SMark J Musante 
29751195e687SMark J Musante 	/* did we find every disk the user specified? */
29761195e687SMark J Musante 	if (found != newchildren) {
29771195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
29781195e687SMark J Musante 		    "include at most one disk from each mirror"));
29791195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29801195e687SMark J Musante 		goto out;
29811195e687SMark J Musante 	}
29821195e687SMark J Musante 
29831195e687SMark J Musante 	/* Prepare the nvlist for populating. */
29841195e687SMark J Musante 	if (*newroot == NULL) {
29851195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
29861195e687SMark J Musante 			goto out;
29871195e687SMark J Musante 		freelist = B_TRUE;
29881195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
29891195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
29901195e687SMark J Musante 			goto out;
29911195e687SMark J Musante 	} else {
29921195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
29931195e687SMark J Musante 	}
29941195e687SMark J Musante 
29951195e687SMark J Musante 	/* Add all the children we found */
29961195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
29971195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
29981195e687SMark J Musante 		goto out;
29991195e687SMark J Musante 
30001195e687SMark J Musante 	/*
30011195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
30021195e687SMark J Musante 	 */
30031195e687SMark J Musante 	if (flags.dryrun) {
30041195e687SMark J Musante 		memory_err = B_FALSE;
30051195e687SMark J Musante 		freelist = B_FALSE;
30061195e687SMark J Musante 		goto out;
30071195e687SMark J Musante 	}
30081195e687SMark J Musante 
30091195e687SMark J Musante 	/* now build up the config list & call the ioctl */
30101195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
30111195e687SMark J Musante 		goto out;
30121195e687SMark J Musante 
30131195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
30141195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
30151195e687SMark J Musante 	    nvlist_add_string(newconfig,
30161195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
30171195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
30181195e687SMark J Musante 		goto out;
30191195e687SMark J Musante 
30201195e687SMark J Musante 	/*
30211195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
30221195e687SMark J Musante 	 * explicitly export it.
30231195e687SMark J Musante 	 */
30241195e687SMark J Musante 	if (!flags.import)
30251195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
30261195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30271195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
30281195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
30291195e687SMark J Musante 		goto out;
30301195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
30311195e687SMark J Musante 		goto out;
30321195e687SMark J Musante 
30331195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
30341195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
30351195e687SMark J Musante 		goto out;
30361195e687SMark J Musante 	}
30371195e687SMark J Musante 
30381195e687SMark J Musante 	freelist = B_FALSE;
30391195e687SMark J Musante 	memory_err = B_FALSE;
30401195e687SMark J Musante 
30411195e687SMark J Musante out:
30421195e687SMark J Musante 	if (varray != NULL) {
30431195e687SMark J Musante 		int v;
30441195e687SMark J Musante 
30451195e687SMark J Musante 		for (v = 0; v < vcount; v++)
30461195e687SMark J Musante 			nvlist_free(varray[v]);
30471195e687SMark J Musante 		free(varray);
30481195e687SMark J Musante 	}
30491195e687SMark J Musante 	zcmd_free_nvlists(&zc);
30501195e687SMark J Musante 	if (zc_props)
30511195e687SMark J Musante 		nvlist_free(zc_props);
30521195e687SMark J Musante 	if (newconfig)
30531195e687SMark J Musante 		nvlist_free(newconfig);
30541195e687SMark J Musante 	if (freelist) {
30551195e687SMark J Musante 		nvlist_free(*newroot);
30561195e687SMark J Musante 		*newroot = NULL;
30571195e687SMark J Musante 	}
30581195e687SMark J Musante 
30591195e687SMark J Musante 	if (retval != 0)
30601195e687SMark J Musante 		return (retval);
30611195e687SMark J Musante 
30621195e687SMark J Musante 	if (memory_err)
30631195e687SMark J Musante 		return (no_memory(hdl));
30641195e687SMark J Musante 
30651195e687SMark J Musante 	return (0);
30661195e687SMark J Musante }
30671195e687SMark J Musante 
306899653d4eSeschrock /*
3069fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
3070fa94a07fSbrendan  * and level 2 cache devices.
307199653d4eSeschrock  */
307299653d4eSeschrock int
307399653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
307499653d4eSeschrock {
307599653d4eSeschrock 	zfs_cmd_t zc = { 0 };
307699653d4eSeschrock 	char msg[1024];
307799653d4eSeschrock 	nvlist_t *tgt;
307888ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
307999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
308088ecc943SGeorge Wilson 	uint64_t version;
308199653d4eSeschrock 
308299653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
308399653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
308499653d4eSeschrock 
308599653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3086ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
308788ecc943SGeorge Wilson 	    &islog)) == 0)
308899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
308988ecc943SGeorge Wilson 	/*
309088ecc943SGeorge Wilson 	 * XXX - this should just go away.
309188ecc943SGeorge Wilson 	 */
309288ecc943SGeorge Wilson 	if (!avail_spare && !l2cache && !islog) {
309399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309488ecc943SGeorge Wilson 		    "only inactive hot spares, cache, top-level, "
309588ecc943SGeorge Wilson 		    "or log devices can be removed"));
309699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
309799653d4eSeschrock 	}
309899653d4eSeschrock 
309988ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
310088ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
310188ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
310288ecc943SGeorge Wilson 		    "pool must be upgrade to support log removal"));
310388ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
310488ecc943SGeorge Wilson 	}
310588ecc943SGeorge Wilson 
310699653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
310799653d4eSeschrock 
3108ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
310999653d4eSeschrock 		return (0);
311099653d4eSeschrock 
311199653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3112ea8dc4b6Seschrock }
3113ea8dc4b6Seschrock 
3114ea8dc4b6Seschrock /*
3115ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
3116ea8dc4b6Seschrock  */
3117ea8dc4b6Seschrock int
3118468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3119ea8dc4b6Seschrock {
3120ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3121ea8dc4b6Seschrock 	char msg[1024];
312299653d4eSeschrock 	nvlist_t *tgt;
3123468c413aSTim Haley 	zpool_rewind_policy_t policy;
3124fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
312599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3126468c413aSTim Haley 	nvlist_t *nvi = NULL;
31274b964adaSGeorge Wilson 	int error;
3128ea8dc4b6Seschrock 
3129ea8dc4b6Seschrock 	if (path)
3130ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3131ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3132e9dbad6fSeschrock 		    path);
3133ea8dc4b6Seschrock 	else
3134ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3135ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3136ea8dc4b6Seschrock 		    zhp->zpool_name);
3137ea8dc4b6Seschrock 
3138ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
313999653d4eSeschrock 	if (path) {
3140fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3141ee0eb9f2SEric Schrock 		    &l2cache, NULL)) == 0)
314299653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3143ea8dc4b6Seschrock 
3144fa94a07fSbrendan 		/*
3145fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
3146fa94a07fSbrendan 		 * error clearing for l2cache devices.
3147fa94a07fSbrendan 		 */
3148a43d325bSek 		if (avail_spare)
314999653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3150ea8dc4b6Seschrock 
315199653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
315299653d4eSeschrock 		    &zc.zc_guid) == 0);
3153fa9e4066Sahrens 	}
3154fa9e4066Sahrens 
3155468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
3156468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
3157468c413aSTim Haley 
315857f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3159468c413aSTim Haley 		return (-1);
3160468c413aSTim Haley 
3161cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3162468c413aSTim Haley 		return (-1);
3163468c413aSTim Haley 
31644b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
31654b964adaSGeorge Wilson 	    errno == ENOMEM) {
31664b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
31674b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
31684b964adaSGeorge Wilson 			return (-1);
31694b964adaSGeorge Wilson 		}
31704b964adaSGeorge Wilson 	}
31714b964adaSGeorge Wilson 
31724b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3173468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
3174468c413aSTim Haley 		if (policy.zrp_request &
3175468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3176468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3177468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
3178468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3179468c413aSTim Haley 			    nvi);
3180468c413aSTim Haley 			nvlist_free(nvi);
3181468c413aSTim Haley 		}
3182468c413aSTim Haley 		zcmd_free_nvlists(&zc);
318399653d4eSeschrock 		return (0);
3184468c413aSTim Haley 	}
318599653d4eSeschrock 
3186468c413aSTim Haley 	zcmd_free_nvlists(&zc);
318799653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3188fa9e4066Sahrens }
3189fa9e4066Sahrens 
31903d7072f8Seschrock /*
31913d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
31923d7072f8Seschrock  */
31933d7072f8Seschrock int
31943d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
31953d7072f8Seschrock {
31963d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
31973d7072f8Seschrock 	char msg[1024];
31983d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31993d7072f8Seschrock 
32003d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
32013d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
32023d7072f8Seschrock 	    guid);
32033d7072f8Seschrock 
32043d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
32053d7072f8Seschrock 	zc.zc_guid = guid;
320614f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
32073d7072f8Seschrock 
32083d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
32093d7072f8Seschrock 		return (0);
32103d7072f8Seschrock 
32113d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
32123d7072f8Seschrock }
32133d7072f8Seschrock 
3214e9103aaeSGarrett D'Amore /*
3215e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
3216e9103aaeSGarrett D'Amore  */
3217e9103aaeSGarrett D'Amore int
3218e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3219e9103aaeSGarrett D'Amore {
3220e9103aaeSGarrett D'Amore 	char msg[1024];
3221e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3222e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3223e9103aaeSGarrett D'Amore 
3224e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3225e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3226e9103aaeSGarrett D'Amore 
3227e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3228e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3229e9103aaeSGarrett D'Amore 		return (0);
3230e9103aaeSGarrett D'Amore 
3231e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3232e9103aaeSGarrett D'Amore }
3233e9103aaeSGarrett D'Amore 
32344263d13fSGeorge Wilson /*
32354263d13fSGeorge Wilson  * Reopen the pool.
32364263d13fSGeorge Wilson  */
32374263d13fSGeorge Wilson int
32384263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
32394263d13fSGeorge Wilson {
32404263d13fSGeorge Wilson 	zfs_cmd_t zc = { 0 };
32414263d13fSGeorge Wilson 	char msg[1024];
32424263d13fSGeorge Wilson 	libzfs_handle_t *hdl = zhp->zpool_hdl;
32434263d13fSGeorge Wilson 
32444263d13fSGeorge Wilson 	(void) snprintf(msg, sizeof (msg),
32454263d13fSGeorge Wilson 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
32464263d13fSGeorge Wilson 	    zhp->zpool_name);
32474263d13fSGeorge Wilson 
32484263d13fSGeorge Wilson 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
32494263d13fSGeorge Wilson 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
32504263d13fSGeorge Wilson 		return (0);
32514263d13fSGeorge Wilson 	return (zpool_standard_error(hdl, errno, msg));
32524263d13fSGeorge Wilson }
32534263d13fSGeorge Wilson 
3254c67d9675Seschrock /*
3255c67d9675Seschrock  * Convert from a devid string to a path.
3256c67d9675Seschrock  */
3257c67d9675Seschrock static char *
3258c67d9675Seschrock devid_to_path(char *devid_str)
3259c67d9675Seschrock {
3260c67d9675Seschrock 	ddi_devid_t devid;
3261c67d9675Seschrock 	char *minor;
3262c67d9675Seschrock 	char *path;
3263c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3264c67d9675Seschrock 	int ret;
3265c67d9675Seschrock 
3266c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3267c67d9675Seschrock 		return (NULL);
3268c67d9675Seschrock 
3269c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3270c67d9675Seschrock 
3271c67d9675Seschrock 	devid_str_free(minor);
3272c67d9675Seschrock 	devid_free(devid);
3273c67d9675Seschrock 
3274c67d9675Seschrock 	if (ret != 0)
3275c67d9675Seschrock 		return (NULL);
3276c67d9675Seschrock 
327799653d4eSeschrock 	if ((path = strdup(list[0].devname)) == NULL)
327899653d4eSeschrock 		return (NULL);
327999653d4eSeschrock 
3280c67d9675Seschrock 	devid_free_nmlist(list);
3281c67d9675Seschrock 
3282c67d9675Seschrock 	return (path);
3283c67d9675Seschrock }
3284c67d9675Seschrock 
3285c67d9675Seschrock /*
3286c67d9675Seschrock  * Convert from a path to a devid string.
3287c67d9675Seschrock  */
3288c67d9675Seschrock static char *
3289c67d9675Seschrock path_to_devid(const char *path)
3290c67d9675Seschrock {
3291c67d9675Seschrock 	int fd;
3292c67d9675Seschrock 	ddi_devid_t devid;
3293c67d9675Seschrock 	char *minor, *ret;
3294c67d9675Seschrock 
3295c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3296c67d9675Seschrock 		return (NULL);
3297c67d9675Seschrock 
3298c67d9675Seschrock 	minor = NULL;
3299c67d9675Seschrock 	ret = NULL;
3300c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3301c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3302c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3303c67d9675Seschrock 		if (minor != NULL)
3304c67d9675Seschrock 			devid_str_free(minor);
3305c67d9675Seschrock 		devid_free(devid);
3306c67d9675Seschrock 	}
3307c67d9675Seschrock 	(void) close(fd);
3308c67d9675Seschrock 
3309c67d9675Seschrock 	return (ret);
3310c67d9675Seschrock }
3311c67d9675Seschrock 
3312c67d9675Seschrock /*
3313c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3314c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3315c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3316c67d9675Seschrock  */
3317c67d9675Seschrock static void
3318c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3319c67d9675Seschrock {
3320c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3321c67d9675Seschrock 
3322c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3323e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3324c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3325ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3326c67d9675Seschrock 
332799653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3328c67d9675Seschrock }
3329c67d9675Seschrock 
3330c67d9675Seschrock /*
3331c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3332c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3333c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3334c67d9675Seschrock  * trailing 's0' slice name.
3335c67d9675Seschrock  *
3336c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3337c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3338c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3339c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3340c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3341c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3342c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3343c67d9675Seschrock  * of these checks.
3344c67d9675Seschrock  */
3345c67d9675Seschrock char *
334688ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
334788ecc943SGeorge Wilson     boolean_t verbose)
3348c67d9675Seschrock {
3349c67d9675Seschrock 	char *path, *devid;
3350ea8dc4b6Seschrock 	uint64_t value;
3351ea8dc4b6Seschrock 	char buf[64];
33523d7072f8Seschrock 	vdev_stat_t *vs;
33533d7072f8Seschrock 	uint_t vsc;
3354c67d9675Seschrock 
3355ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3356ea8dc4b6Seschrock 	    &value) == 0) {
3357ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3358ea8dc4b6Seschrock 		    &value) == 0);
33595ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
33605ad82045Snd 		    (u_longlong_t)value);
3361ea8dc4b6Seschrock 		path = buf;
3362ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3363c67d9675Seschrock 
33643d7072f8Seschrock 		/*
33653d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
33663d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
33673d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
33683d7072f8Seschrock 		 * effects.
33693d7072f8Seschrock 		 */
33703f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
33713d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
33723d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
33733d7072f8Seschrock 		    zhp != NULL &&
3374c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3375c67d9675Seschrock 			/*
3376c67d9675Seschrock 			 * Determine if the current path is correct.
3377c67d9675Seschrock 			 */
3378c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3379c67d9675Seschrock 
3380c67d9675Seschrock 			if (newdevid == NULL ||
3381c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3382c67d9675Seschrock 				char *newpath;
3383c67d9675Seschrock 
3384c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3385c67d9675Seschrock 					/*
3386c67d9675Seschrock 					 * Update the path appropriately.
3387c67d9675Seschrock 					 */
3388c67d9675Seschrock 					set_path(zhp, nv, newpath);
338999653d4eSeschrock 					if (nvlist_add_string(nv,
339099653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
339199653d4eSeschrock 						verify(nvlist_lookup_string(nv,
339299653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
339399653d4eSeschrock 						    &path) == 0);
3394c67d9675Seschrock 					free(newpath);
3395c67d9675Seschrock 				}
3396c67d9675Seschrock 			}
3397c67d9675Seschrock 
339899653d4eSeschrock 			if (newdevid)
339999653d4eSeschrock 				devid_str_free(newdevid);
3400c67d9675Seschrock 		}
3401c67d9675Seschrock 
3402c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
3403c67d9675Seschrock 			path += 9;
3404c67d9675Seschrock 
3405c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3406ea8dc4b6Seschrock 		    &value) == 0 && value) {
34073fdda499SJohn Harres 			int pathlen = strlen(path);
340899653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
34093fdda499SJohn Harres 
34103fdda499SJohn Harres 			/*
34113fdda499SJohn Harres 			 * If it starts with c#, and ends with "s0", chop
34123fdda499SJohn Harres 			 * the "s0" off, or if it ends with "s0/old", remove
34133fdda499SJohn Harres 			 * the "s0" from the middle.
34143fdda499SJohn Harres 			 */
34153fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
34163fdda499SJohn Harres 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
34173fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
34183fdda499SJohn Harres 				} else if (pathlen > 6 &&
34193fdda499SJohn Harres 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
34203fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
34213fdda499SJohn Harres 					    "/old");
34223fdda499SJohn Harres 				}
34233fdda499SJohn Harres 			}
3424c67d9675Seschrock 			return (tmp);
3425c67d9675Seschrock 		}
3426c67d9675Seschrock 	} else {
3427c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
342899653d4eSeschrock 
342999653d4eSeschrock 		/*
343099653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
343199653d4eSeschrock 		 */
343299653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
343399653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
343499653d4eSeschrock 			    &value) == 0);
343599653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
34365ad82045Snd 			    (u_longlong_t)value);
343799653d4eSeschrock 			path = buf;
343899653d4eSeschrock 		}
343988ecc943SGeorge Wilson 
344088ecc943SGeorge Wilson 		/*
344188ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
344288ecc943SGeorge Wilson 		 * naming convention.
344388ecc943SGeorge Wilson 		 */
344488ecc943SGeorge Wilson 		if (verbose) {
344588ecc943SGeorge Wilson 			uint64_t id;
344688ecc943SGeorge Wilson 
344788ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
344888ecc943SGeorge Wilson 			    &id) == 0);
344988ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
345088ecc943SGeorge Wilson 			    (u_longlong_t)id);
345188ecc943SGeorge Wilson 			path = buf;
345288ecc943SGeorge Wilson 		}
3453c67d9675Seschrock 	}
3454c67d9675Seschrock 
345599653d4eSeschrock 	return (zfs_strdup(hdl, path));
3456c67d9675Seschrock }
3457ea8dc4b6Seschrock 
3458ea8dc4b6Seschrock static int
3459ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b)
3460ea8dc4b6Seschrock {
3461ea8dc4b6Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
3462ea8dc4b6Seschrock }
3463ea8dc4b6Seschrock 
3464ea8dc4b6Seschrock /*
3465ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3466ea8dc4b6Seschrock  * caller.
3467ea8dc4b6Seschrock  */
3468ea8dc4b6Seschrock int
346955434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3470ea8dc4b6Seschrock {
3471ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3472ea8dc4b6Seschrock 	uint64_t count;
3473e9dbad6fSeschrock 	zbookmark_t *zb = NULL;
347455434c77Sek 	int i;
3475ea8dc4b6Seschrock 
3476ea8dc4b6Seschrock 	/*
3477ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3478ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3479ea8dc4b6Seschrock 	 * entire list.
3480ea8dc4b6Seschrock 	 */
3481ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3482ea8dc4b6Seschrock 	    &count) == 0);
348375519f38Sek 	if (count == 0)
348475519f38Sek 		return (0);
3485e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
34865ad82045Snd 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
348799653d4eSeschrock 		return (-1);
3488e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3489ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3490ea8dc4b6Seschrock 	for (;;) {
349199653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
349299653d4eSeschrock 		    &zc) != 0) {
3493e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3494ea8dc4b6Seschrock 			if (errno == ENOMEM) {
3495bf561db0Svb 				count = zc.zc_nvlist_dst_size;
3496e9dbad6fSeschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
3497bf561db0Svb 				    zfs_alloc(zhp->zpool_hdl, count *
3498bf561db0Svb 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
349999653d4eSeschrock 					return (-1);
3500ea8dc4b6Seschrock 			} else {
3501ea8dc4b6Seschrock 				return (-1);
3502ea8dc4b6Seschrock 			}
3503ea8dc4b6Seschrock 		} else {
3504ea8dc4b6Seschrock 			break;
3505ea8dc4b6Seschrock 		}
3506ea8dc4b6Seschrock 	}
3507ea8dc4b6Seschrock 
3508ea8dc4b6Seschrock 	/*
3509ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3510ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3511e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3512ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3513ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3514ea8dc4b6Seschrock 	 */
3515e9dbad6fSeschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3516e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3517e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3518ea8dc4b6Seschrock 
3519ea8dc4b6Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3520ea8dc4b6Seschrock 
352155434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3522ea8dc4b6Seschrock 
3523ea8dc4b6Seschrock 	/*
352455434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3525ea8dc4b6Seschrock 	 */
3526ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3527ea8dc4b6Seschrock 		nvlist_t *nv;
3528ea8dc4b6Seschrock 
3529c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
3530c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3531c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
3532ea8dc4b6Seschrock 			continue;
3533ea8dc4b6Seschrock 
353455434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
353555434c77Sek 			goto nomem;
353655434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
353755434c77Sek 		    zb[i].zb_objset) != 0) {
353855434c77Sek 			nvlist_free(nv);
353999653d4eSeschrock 			goto nomem;
3540ea8dc4b6Seschrock 		}
354155434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
354255434c77Sek 		    zb[i].zb_object) != 0) {
354355434c77Sek 			nvlist_free(nv);
354455434c77Sek 			goto nomem;
354555434c77Sek 		}
354655434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
354755434c77Sek 			nvlist_free(nv);
354855434c77Sek 			goto nomem;
354955434c77Sek 		}
355055434c77Sek 		nvlist_free(nv);
3551ea8dc4b6Seschrock 	}
3552ea8dc4b6Seschrock 
35533ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3554ea8dc4b6Seschrock 	return (0);
355599653d4eSeschrock 
355699653d4eSeschrock nomem:
3557e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
355899653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3559ea8dc4b6Seschrock }
3560eaca9bbdSeschrock 
3561eaca9bbdSeschrock /*
3562eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3563eaca9bbdSeschrock  */
3564eaca9bbdSeschrock int
3565990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3566eaca9bbdSeschrock {
3567eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
356899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3569eaca9bbdSeschrock 
3570eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3571990b4856Slling 	zc.zc_cookie = new_version;
3572990b4856Slling 
3573ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3574ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
357599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
357699653d4eSeschrock 		    zhp->zpool_name));
3577eaca9bbdSeschrock 	return (0);
3578eaca9bbdSeschrock }
357906eeb2adSek 
358006eeb2adSek void
3581*4445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len)
358206eeb2adSek {
3583*4445fffbSMatthew Ahrens 	(void) strlcpy(string, basename(argv[0]), len);
3584*4445fffbSMatthew Ahrens 	for (int i = 1; i < argc; i++) {
3585*4445fffbSMatthew Ahrens 		(void) strlcat(string, " ", len);
3586*4445fffbSMatthew Ahrens 		(void) strlcat(string, argv[i], len);
35872a6b87f0Sek 	}
35882a6b87f0Sek }
35892a6b87f0Sek 
35902a6b87f0Sek int
3591*4445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message)
35922a6b87f0Sek {
3593*4445fffbSMatthew Ahrens 	zfs_cmd_t zc = { 0 };
3594*4445fffbSMatthew Ahrens 	nvlist_t *args;
3595*4445fffbSMatthew Ahrens 	int err;
3596*4445fffbSMatthew Ahrens 
3597*4445fffbSMatthew Ahrens 	args = fnvlist_alloc();
3598*4445fffbSMatthew Ahrens 	fnvlist_add_string(args, "message", message);
3599*4445fffbSMatthew Ahrens 	err = zcmd_write_src_nvlist(hdl, &zc, args);
3600*4445fffbSMatthew Ahrens 	if (err == 0)
3601*4445fffbSMatthew Ahrens 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3602*4445fffbSMatthew Ahrens 	nvlist_free(args);
3603*4445fffbSMatthew Ahrens 	zcmd_free_nvlists(&zc);
3604*4445fffbSMatthew Ahrens 	return (err);
360506eeb2adSek }
360606eeb2adSek 
360706eeb2adSek /*
360806eeb2adSek  * Perform ioctl to get some command history of a pool.
360906eeb2adSek  *
361006eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
361106eeb2adSek  * logical offset of the history buffer to start reading from.
361206eeb2adSek  *
361306eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
361406eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
361506eeb2adSek  */
361606eeb2adSek static int
361706eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
361806eeb2adSek {
361906eeb2adSek 	zfs_cmd_t zc = { 0 };
362006eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
362106eeb2adSek 
362206eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
362306eeb2adSek 
362406eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
362506eeb2adSek 	zc.zc_history_len = *len;
362606eeb2adSek 	zc.zc_history_offset = *off;
362706eeb2adSek 
362806eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
362906eeb2adSek 		switch (errno) {
363006eeb2adSek 		case EPERM:
3631ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3632ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
363306eeb2adSek 			    "cannot show history for pool '%s'"),
363406eeb2adSek 			    zhp->zpool_name));
363506eeb2adSek 		case ENOENT:
3636ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
363706eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
363806eeb2adSek 			    "'%s'"), zhp->zpool_name));
3639d7306b64Sek 		case ENOTSUP:
3640d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3641d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3642d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
364306eeb2adSek 		default:
3644ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
364506eeb2adSek 			    dgettext(TEXT_DOMAIN,
364606eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
364706eeb2adSek 		}
364806eeb2adSek 	}
364906eeb2adSek 
365006eeb2adSek 	*len = zc.zc_history_len;
365106eeb2adSek 	*off = zc.zc_history_offset;
365206eeb2adSek 
365306eeb2adSek 	return (0);
365406eeb2adSek }
365506eeb2adSek 
365606eeb2adSek /*
365706eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
365806eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
365906eeb2adSek  * processed as there wasn't a complete record.
366006eeb2adSek  */
36618f18d1faSGeorge Wilson int
366206eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
366306eeb2adSek     nvlist_t ***records, uint_t *numrecords)
366406eeb2adSek {
366506eeb2adSek 	uint64_t reclen;
366606eeb2adSek 	nvlist_t *nv;
366706eeb2adSek 	int i;
366806eeb2adSek 
366906eeb2adSek 	while (bytes_read > sizeof (reclen)) {
367006eeb2adSek 
367106eeb2adSek 		/* get length of packed record (stored as little endian) */
367206eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
367306eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
367406eeb2adSek 
367506eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
367606eeb2adSek 			break;
367706eeb2adSek 
367806eeb2adSek 		/* unpack record */
367906eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
368006eeb2adSek 			return (ENOMEM);
368106eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
368206eeb2adSek 		buf += sizeof (reclen) + reclen;
368306eeb2adSek 
368406eeb2adSek 		/* add record to nvlist array */
368506eeb2adSek 		(*numrecords)++;
368606eeb2adSek 		if (ISP2(*numrecords + 1)) {
368706eeb2adSek 			*records = realloc(*records,
368806eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
368906eeb2adSek 		}
369006eeb2adSek 		(*records)[*numrecords - 1] = nv;
369106eeb2adSek 	}
369206eeb2adSek 
369306eeb2adSek 	*leftover = bytes_read;
369406eeb2adSek 	return (0);
369506eeb2adSek }
369606eeb2adSek 
369706eeb2adSek #define	HIS_BUF_LEN	(128*1024)
369806eeb2adSek 
369906eeb2adSek /*
370006eeb2adSek  * Retrieve the command history of a pool.
370106eeb2adSek  */
370206eeb2adSek int
370306eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
370406eeb2adSek {
370506eeb2adSek 	char buf[HIS_BUF_LEN];
370606eeb2adSek 	uint64_t off = 0;
370706eeb2adSek 	nvlist_t **records = NULL;
370806eeb2adSek 	uint_t numrecords = 0;
370906eeb2adSek 	int err, i;
371006eeb2adSek 
371106eeb2adSek 	do {
371206eeb2adSek 		uint64_t bytes_read = sizeof (buf);
371306eeb2adSek 		uint64_t leftover;
371406eeb2adSek 
371506eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
371606eeb2adSek 			break;
371706eeb2adSek 
371806eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
371906eeb2adSek 		if (!bytes_read)
372006eeb2adSek 			break;
372106eeb2adSek 
372206eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
372306eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
372406eeb2adSek 			break;
372506eeb2adSek 		off -= leftover;
372606eeb2adSek 
372706eeb2adSek 		/* CONSTCOND */
372806eeb2adSek 	} while (1);
372906eeb2adSek 
373006eeb2adSek 	if (!err) {
373106eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
373206eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
373306eeb2adSek 		    records, numrecords) == 0);
373406eeb2adSek 	}
373506eeb2adSek 	for (i = 0; i < numrecords; i++)
373606eeb2adSek 		nvlist_free(records[i]);
373706eeb2adSek 	free(records);
373806eeb2adSek 
373906eeb2adSek 	return (err);
374006eeb2adSek }
374155434c77Sek 
374255434c77Sek void
374355434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
374455434c77Sek     char *pathname, size_t len)
374555434c77Sek {
374655434c77Sek 	zfs_cmd_t zc = { 0 };
374755434c77Sek 	boolean_t mounted = B_FALSE;
374855434c77Sek 	char *mntpnt = NULL;
374955434c77Sek 	char dsname[MAXNAMELEN];
375055434c77Sek 
375155434c77Sek 	if (dsobj == 0) {
375255434c77Sek 		/* special case for the MOS */
375355434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
375455434c77Sek 		return;
375555434c77Sek 	}
375655434c77Sek 
375755434c77Sek 	/* get the dataset's name */
375855434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
375955434c77Sek 	zc.zc_obj = dsobj;
376055434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
376155434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
376255434c77Sek 		/* just write out a path of two object numbers */
376355434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
376455434c77Sek 		    dsobj, obj);
376555434c77Sek 		return;
376655434c77Sek 	}
376755434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
376855434c77Sek 
376955434c77Sek 	/* find out if the dataset is mounted */
377055434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
377155434c77Sek 
377255434c77Sek 	/* get the corrupted object's path */
377355434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
377455434c77Sek 	zc.zc_obj = obj;
377555434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
377655434c77Sek 	    &zc) == 0) {
377755434c77Sek 		if (mounted) {
377855434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
377955434c77Sek 			    zc.zc_value);
378055434c77Sek 		} else {
378155434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
378255434c77Sek 			    dsname, zc.zc_value);
378355434c77Sek 		}
378455434c77Sek 	} else {
378555434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
378655434c77Sek 	}
378755434c77Sek 	free(mntpnt);
378855434c77Sek }
3789b1b8ab34Slling 
379015e6edf1Sgw /*
379115e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
379215e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
379315e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
379415e6edf1Sgw  * partition.
379515e6edf1Sgw  */
379615e6edf1Sgw static int
379715e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb)
379815e6edf1Sgw {
379915e6edf1Sgw 	char *path;
380015e6edf1Sgw 	int fd;
380115e6edf1Sgw 	char diskname[MAXPATHLEN];
380215e6edf1Sgw 	int err = -1;
380315e6edf1Sgw 
380415e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
380515e6edf1Sgw 		return (err);
380615e6edf1Sgw 
380715e6edf1Sgw 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
380815e6edf1Sgw 	    strrchr(path, '/'));
380915e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
381015e6edf1Sgw 		struct dk_gpt *vtoc;
381115e6edf1Sgw 
381215e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
381315e6edf1Sgw 			if (sb != NULL)
381415e6edf1Sgw 				*sb = vtoc->efi_parts[0].p_start;
381515e6edf1Sgw 			efi_free(vtoc);
381615e6edf1Sgw 		}
381715e6edf1Sgw 		(void) close(fd);
381815e6edf1Sgw 	}
381915e6edf1Sgw 	return (err);
382015e6edf1Sgw }
382115e6edf1Sgw 
38228488aeb5Staylor /*
38238488aeb5Staylor  * determine where a partition starts on a disk in the current
38248488aeb5Staylor  * configuration
38258488aeb5Staylor  */
38268488aeb5Staylor static diskaddr_t
38278488aeb5Staylor find_start_block(nvlist_t *config)
38288488aeb5Staylor {
38298488aeb5Staylor 	nvlist_t **child;
38308488aeb5Staylor 	uint_t c, children;
38318488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
38328488aeb5Staylor 	uint64_t wholedisk;
38338488aeb5Staylor 
38348488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
38358488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
38368488aeb5Staylor 		if (nvlist_lookup_uint64(config,
38378488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
38388488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
38398488aeb5Staylor 			return (MAXOFFSET_T);
38408488aeb5Staylor 		}
384115e6edf1Sgw 		if (read_efi_label(config, &sb) < 0)
384215e6edf1Sgw 			sb = MAXOFFSET_T;
38438488aeb5Staylor 		return (sb);
38448488aeb5Staylor 	}
38458488aeb5Staylor 
38468488aeb5Staylor 	for (c = 0; c < children; c++) {
38478488aeb5Staylor 		sb = find_start_block(child[c]);
38488488aeb5Staylor 		if (sb != MAXOFFSET_T) {
38498488aeb5Staylor 			return (sb);
38508488aeb5Staylor 		}
38518488aeb5Staylor 	}
38528488aeb5Staylor 	return (MAXOFFSET_T);
38538488aeb5Staylor }
38548488aeb5Staylor 
38558488aeb5Staylor /*
38568488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
38578488aeb5Staylor  * stripped of any leading /dev path.
38588488aeb5Staylor  */
38598488aeb5Staylor int
38608488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
38618488aeb5Staylor {
38628488aeb5Staylor 	char path[MAXPATHLEN];
38638488aeb5Staylor 	struct dk_gpt *vtoc;
38648488aeb5Staylor 	int fd;
38658488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
38668488aeb5Staylor 	uint64_t slice_size;
38678488aeb5Staylor 	diskaddr_t start_block;
38688488aeb5Staylor 	char errbuf[1024];
38698488aeb5Staylor 
3870c6ef114fSmmusante 	/* prepare an error message just in case */
3871c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
3872c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3873c6ef114fSmmusante 
38748488aeb5Staylor 	if (zhp) {
38758488aeb5Staylor 		nvlist_t *nvroot;
38768488aeb5Staylor 
38774263d13fSGeorge Wilson 		if (zpool_is_bootable(zhp)) {
3878b5b76fecSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3879b5b76fecSGeorge Wilson 			    "EFI labeled devices are not supported on root "
3880b5b76fecSGeorge Wilson 			    "pools."));
3881b5b76fecSGeorge Wilson 			return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3882b5b76fecSGeorge Wilson 		}
3883b5b76fecSGeorge Wilson 
38848488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
38858488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
38868488aeb5Staylor 
38878488aeb5Staylor 		if (zhp->zpool_start_block == 0)
38888488aeb5Staylor 			start_block = find_start_block(nvroot);
38898488aeb5Staylor 		else
38908488aeb5Staylor 			start_block = zhp->zpool_start_block;
38918488aeb5Staylor 		zhp->zpool_start_block = start_block;
38928488aeb5Staylor 	} else {
38938488aeb5Staylor 		/* new pool */
38948488aeb5Staylor 		start_block = NEW_START_BLOCK;
38958488aeb5Staylor 	}
38968488aeb5Staylor 
38978488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
38988488aeb5Staylor 	    BACKUP_SLICE);
38998488aeb5Staylor 
39008488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
39018488aeb5Staylor 		/*
39028488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
39038488aeb5Staylor 		 * is a valid device.
39048488aeb5Staylor 		 */
3905c6ef114fSmmusante 		zfs_error_aux(hdl,
3906c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
39078488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
39088488aeb5Staylor 	}
39098488aeb5Staylor 
39108488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
39118488aeb5Staylor 		/*
39128488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
39138488aeb5Staylor 		 * were unable to read the disk's capacity
39148488aeb5Staylor 		 */
39158488aeb5Staylor 		if (errno == ENOMEM)
39168488aeb5Staylor 			(void) no_memory(hdl);
39178488aeb5Staylor 
39188488aeb5Staylor 		(void) close(fd);
3919c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3920c6ef114fSmmusante 		    "unable to read disk capacity"), name);
39218488aeb5Staylor 
39228488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
39238488aeb5Staylor 	}
39248488aeb5Staylor 
39258488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
39268488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
39278488aeb5Staylor 	if (start_block == MAXOFFSET_T)
39288488aeb5Staylor 		start_block = NEW_START_BLOCK;
39298488aeb5Staylor 	slice_size -= start_block;
39308488aeb5Staylor 
39318488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
39328488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
39338488aeb5Staylor 
39348488aeb5Staylor 	/*
39358488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
39368488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
39378488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
39388488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
39398488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
39408488aeb5Staylor 	 * can get, in the absence of V_OTHER.
39418488aeb5Staylor 	 */
39428488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
39438488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
39448488aeb5Staylor 
39458488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
39468488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
39478488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
39488488aeb5Staylor 
39498488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
39508488aeb5Staylor 		/*
39518488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
39528488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
39538488aeb5Staylor 		 * ecting the user to manually label the disk and give
39548488aeb5Staylor 		 * a specific slice.
39558488aeb5Staylor 		 */
39568488aeb5Staylor 		(void) close(fd);
39578488aeb5Staylor 		efi_free(vtoc);
39588488aeb5Staylor 
39598488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3960c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
39618488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
39628488aeb5Staylor 	}
39638488aeb5Staylor 
39648488aeb5Staylor 	(void) close(fd);
39658488aeb5Staylor 	efi_free(vtoc);
39668488aeb5Staylor 	return (0);
39678488aeb5Staylor }
3968e7cbe64fSgw 
3969e7cbe64fSgw static boolean_t
3970e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3971e7cbe64fSgw {
3972e7cbe64fSgw 	char *type;
3973e7cbe64fSgw 	nvlist_t **child;
3974e7cbe64fSgw 	uint_t children, c;
3975e7cbe64fSgw 
3976e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3977e7cbe64fSgw 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3978e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
3979e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
398088ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3981e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3982e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3983e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
3984e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3985e7cbe64fSgw 		return (B_FALSE);
3986e7cbe64fSgw 	}
3987e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3988e7cbe64fSgw 	    &child, &children) == 0) {
3989e7cbe64fSgw 		for (c = 0; c < children; c++) {
3990e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3991e7cbe64fSgw 				return (B_FALSE);
3992e7cbe64fSgw 		}
3993e7cbe64fSgw 	}
3994e7cbe64fSgw 	return (B_TRUE);
3995e7cbe64fSgw }
3996e7cbe64fSgw 
3997e7cbe64fSgw /*
3998e7cbe64fSgw  * check if this zvol is allowable for use as a dump device; zero if
3999e7cbe64fSgw  * it is, > 0 if it isn't, < 0 if it isn't a zvol
4000e7cbe64fSgw  */
4001e7cbe64fSgw int
4002e7cbe64fSgw zvol_check_dump_config(char *arg)
4003e7cbe64fSgw {
4004e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
4005e7cbe64fSgw 	nvlist_t *config, *nvroot;
4006e7cbe64fSgw 	char *p, *volname;
4007e7cbe64fSgw 	nvlist_t **top;
4008e7cbe64fSgw 	uint_t toplevels;
4009e7cbe64fSgw 	libzfs_handle_t *hdl;
4010e7cbe64fSgw 	char errbuf[1024];
4011e7cbe64fSgw 	char poolname[ZPOOL_MAXNAMELEN];
4012e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4013e7cbe64fSgw 	int ret = 1;
4014e7cbe64fSgw 
4015e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4016e7cbe64fSgw 		return (-1);
4017e7cbe64fSgw 	}
4018e7cbe64fSgw 
4019e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4020e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
4021e7cbe64fSgw 
4022e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
4023e7cbe64fSgw 		return (1);
4024e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
4025e7cbe64fSgw 
4026e7cbe64fSgw 	volname = arg + pathlen;
4027e7cbe64fSgw 
4028e7cbe64fSgw 	/* check the configuration of the pool */
4029e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
4030e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4031e7cbe64fSgw 		    "malformed dataset name"));
4032e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4033e7cbe64fSgw 		return (1);
4034e7cbe64fSgw 	} else if (p - volname >= ZFS_MAXNAMELEN) {
4035e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4036e7cbe64fSgw 		    "dataset name is too long"));
4037e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4038e7cbe64fSgw 		return (1);
4039e7cbe64fSgw 	} else {
4040e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
4041e7cbe64fSgw 		poolname[p - volname] = '\0';
4042e7cbe64fSgw 	}
4043e7cbe64fSgw 
4044e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4045e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4046e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
4047e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4048e7cbe64fSgw 		goto out;
4049e7cbe64fSgw 	}
4050e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
4051e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4052e7cbe64fSgw 	    &nvroot) != 0) {
4053e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4054e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
4055e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4056e7cbe64fSgw 		goto out;
4057e7cbe64fSgw 	}
4058e7cbe64fSgw 
4059e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4060e7cbe64fSgw 	    &top, &toplevels) == 0);
4061e7cbe64fSgw 	if (toplevels != 1) {
4062e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4063e7cbe64fSgw 		    "'%s' has multiple top level vdevs"), poolname);
4064e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
4065e7cbe64fSgw 		goto out;
4066e7cbe64fSgw 	}
4067e7cbe64fSgw 
4068e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4069e7cbe64fSgw 		goto out;
4070e7cbe64fSgw 	}
4071e7cbe64fSgw 	ret = 0;
4072e7cbe64fSgw 
4073e7cbe64fSgw out:
4074e7cbe64fSgw 	if (zhp)
4075e7cbe64fSgw 		zpool_close(zhp);
4076e7cbe64fSgw 	libzfs_fini(hdl);
4077e7cbe64fSgw 	return (ret);
4078e7cbe64fSgw }
4079