xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision 8488aeb5df27784d479c16cde06a9e25cd9a1152)
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 /*
2339c23413Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29f3861e1aSahl #include <alloca.h>
30fa9e4066Sahrens #include <assert.h>
31fa9e4066Sahrens #include <ctype.h>
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <devid.h>
34f3861e1aSahl #include <dirent.h>
35fa9e4066Sahrens #include <fcntl.h>
36fa9e4066Sahrens #include <libintl.h>
37fa9e4066Sahrens #include <stdio.h>
38fa9e4066Sahrens #include <stdlib.h>
39f3861e1aSahl #include <strings.h>
40fa9e4066Sahrens #include <unistd.h>
41*8488aeb5Staylor #include <sys/efi_partition.h>
42*8488aeb5Staylor #include <sys/vtoc.h>
43fa9e4066Sahrens #include <sys/zfs_ioctl.h>
44ea8dc4b6Seschrock #include <sys/zio.h>
4506eeb2adSek #include <strings.h>
46fa9e4066Sahrens 
47fa9e4066Sahrens #include "zfs_namecheck.h"
48b1b8ab34Slling #include "zfs_prop.h"
49fa9e4066Sahrens #include "libzfs_impl.h"
50fa9e4066Sahrens 
51fa9e4066Sahrens /*
52fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
53fa9e4066Sahrens  * 'buf'.
54fa9e4066Sahrens  */
5599653d4eSeschrock static boolean_t
5699653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
57fa9e4066Sahrens {
58fa9e4066Sahrens 	namecheck_err_t why;
59fa9e4066Sahrens 	char what;
60b468a217Seschrock 	int ret;
61b468a217Seschrock 
62b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
63b468a217Seschrock 
64b468a217Seschrock 	/*
65b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
66b468a217Seschrock 	 * But we need to support users with existing pools that may now be
67b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
68b468a217Seschrock 	 * create (or import), and only in userland.
69b468a217Seschrock 	 */
70b468a217Seschrock 	if (ret == 0 && !isopen &&
71b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
72b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
73b468a217Seschrock 	    strncmp(pool, "spare", 5) == 0)) {
7499653d4eSeschrock 		zfs_error_aux(hdl,
7599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "name is reserved"));
7699653d4eSeschrock 		return (B_FALSE);
77b468a217Seschrock 	}
78b468a217Seschrock 
79fa9e4066Sahrens 
80b468a217Seschrock 	if (ret != 0) {
8199653d4eSeschrock 		if (hdl != NULL) {
82fa9e4066Sahrens 			switch (why) {
83b81d61a6Slling 			case NAME_ERR_TOOLONG:
8499653d4eSeschrock 				zfs_error_aux(hdl,
85b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
86b81d61a6Slling 				break;
87b81d61a6Slling 
88fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
8999653d4eSeschrock 				zfs_error_aux(hdl,
90fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
91fa9e4066Sahrens 				    "'%c' in pool name"), what);
92fa9e4066Sahrens 				break;
93fa9e4066Sahrens 
94fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
9599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9699653d4eSeschrock 				    "name must begin with a letter"));
97fa9e4066Sahrens 				break;
98fa9e4066Sahrens 
99fa9e4066Sahrens 			case NAME_ERR_RESERVED:
10099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10199653d4eSeschrock 				    "name is reserved"));
102fa9e4066Sahrens 				break;
103fa9e4066Sahrens 
104fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
10599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10699653d4eSeschrock 				    "pool name is reserved"));
107fa9e4066Sahrens 				break;
1085ad82045Snd 
1095ad82045Snd 			case NAME_ERR_LEADING_SLASH:
1105ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115ad82045Snd 				    "leading slash in name"));
1125ad82045Snd 				break;
1135ad82045Snd 
1145ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
1155ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1165ad82045Snd 				    "empty component in name"));
1175ad82045Snd 				break;
1185ad82045Snd 
1195ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
1205ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1215ad82045Snd 				    "trailing slash in name"));
1225ad82045Snd 				break;
1235ad82045Snd 
1245ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
1255ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1265ad82045Snd 				    "multiple '@' delimiters in name"));
1275ad82045Snd 				break;
1285ad82045Snd 
129fa9e4066Sahrens 			}
130fa9e4066Sahrens 		}
13199653d4eSeschrock 		return (B_FALSE);
132fa9e4066Sahrens 	}
133fa9e4066Sahrens 
13499653d4eSeschrock 	return (B_TRUE);
135fa9e4066Sahrens }
136fa9e4066Sahrens 
137b1b8ab34Slling static int
138b1b8ab34Slling zpool_get_all_props(zpool_handle_t *zhp)
139b1b8ab34Slling {
140b1b8ab34Slling 	zfs_cmd_t zc = { 0 };
141b1b8ab34Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
142b1b8ab34Slling 
143b1b8ab34Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
144b1b8ab34Slling 
145b1b8ab34Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
146b1b8ab34Slling 		return (-1);
147b1b8ab34Slling 
148b1b8ab34Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
149b1b8ab34Slling 		if (errno == ENOMEM) {
150b1b8ab34Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
151b1b8ab34Slling 				zcmd_free_nvlists(&zc);
152b1b8ab34Slling 				return (-1);
153b1b8ab34Slling 			}
154b1b8ab34Slling 		} else {
155b1b8ab34Slling 			zcmd_free_nvlists(&zc);
156b1b8ab34Slling 			return (-1);
157b1b8ab34Slling 		}
158b1b8ab34Slling 	}
159b1b8ab34Slling 
160b1b8ab34Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
161b1b8ab34Slling 		zcmd_free_nvlists(&zc);
162b1b8ab34Slling 		return (-1);
163b1b8ab34Slling 	}
164b1b8ab34Slling 
165b1b8ab34Slling 	zcmd_free_nvlists(&zc);
166b1b8ab34Slling 
167b1b8ab34Slling 	return (0);
168b1b8ab34Slling }
169b1b8ab34Slling 
170fa9e4066Sahrens /*
171fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
172fa9e4066Sahrens  * state.
173fa9e4066Sahrens  */
174fa9e4066Sahrens zpool_handle_t *
17599653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
176fa9e4066Sahrens {
177fa9e4066Sahrens 	zpool_handle_t *zhp;
17894de1d4cSeschrock 	boolean_t missing;
179fa9e4066Sahrens 
180fa9e4066Sahrens 	/*
181fa9e4066Sahrens 	 * Make sure the pool name is valid.
182fa9e4066Sahrens 	 */
18399653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
184ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
18599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
18699653d4eSeschrock 		    pool);
187fa9e4066Sahrens 		return (NULL);
188fa9e4066Sahrens 	}
189fa9e4066Sahrens 
19099653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
19199653d4eSeschrock 		return (NULL);
192fa9e4066Sahrens 
19399653d4eSeschrock 	zhp->zpool_hdl = hdl;
194fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
195fa9e4066Sahrens 
19694de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
19794de1d4cSeschrock 		zpool_close(zhp);
19894de1d4cSeschrock 		return (NULL);
19994de1d4cSeschrock 	}
20094de1d4cSeschrock 
20194de1d4cSeschrock 	if (missing) {
20294de1d4cSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20394de1d4cSeschrock 		    "no such pool"));
204ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
20594de1d4cSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
20694de1d4cSeschrock 		    pool);
20794de1d4cSeschrock 		zpool_close(zhp);
20894de1d4cSeschrock 		return (NULL);
209fa9e4066Sahrens 	}
210fa9e4066Sahrens 
211fa9e4066Sahrens 	return (zhp);
212fa9e4066Sahrens }
213fa9e4066Sahrens 
214fa9e4066Sahrens /*
215fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
216fa9e4066Sahrens  * the configuration cache may be out of date).
217fa9e4066Sahrens  */
21894de1d4cSeschrock int
21994de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
220fa9e4066Sahrens {
221fa9e4066Sahrens 	zpool_handle_t *zhp;
22294de1d4cSeschrock 	boolean_t missing;
223fa9e4066Sahrens 
22494de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
22594de1d4cSeschrock 		return (-1);
226fa9e4066Sahrens 
22799653d4eSeschrock 	zhp->zpool_hdl = hdl;
228fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
229fa9e4066Sahrens 
23094de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
23194de1d4cSeschrock 		zpool_close(zhp);
23294de1d4cSeschrock 		return (-1);
233fa9e4066Sahrens 	}
234fa9e4066Sahrens 
23594de1d4cSeschrock 	if (missing) {
23694de1d4cSeschrock 		zpool_close(zhp);
23794de1d4cSeschrock 		*ret = NULL;
23894de1d4cSeschrock 		return (0);
23994de1d4cSeschrock 	}
24094de1d4cSeschrock 
24194de1d4cSeschrock 	*ret = zhp;
24294de1d4cSeschrock 	return (0);
243fa9e4066Sahrens }
244fa9e4066Sahrens 
245fa9e4066Sahrens /*
246fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
247fa9e4066Sahrens  * state.
248fa9e4066Sahrens  */
249fa9e4066Sahrens zpool_handle_t *
25099653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
251fa9e4066Sahrens {
252fa9e4066Sahrens 	zpool_handle_t *zhp;
253fa9e4066Sahrens 
25499653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
255fa9e4066Sahrens 		return (NULL);
256fa9e4066Sahrens 
257fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
258ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
25999653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
260fa9e4066Sahrens 		zpool_close(zhp);
261fa9e4066Sahrens 		return (NULL);
262fa9e4066Sahrens 	}
263fa9e4066Sahrens 
264fa9e4066Sahrens 	return (zhp);
265fa9e4066Sahrens }
266fa9e4066Sahrens 
267fa9e4066Sahrens /*
268fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
269fa9e4066Sahrens  */
270fa9e4066Sahrens void
271fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
272fa9e4066Sahrens {
273fa9e4066Sahrens 	if (zhp->zpool_config)
274fa9e4066Sahrens 		nvlist_free(zhp->zpool_config);
275088e9d47Seschrock 	if (zhp->zpool_old_config)
276088e9d47Seschrock 		nvlist_free(zhp->zpool_old_config);
277b1b8ab34Slling 	if (zhp->zpool_props)
278b1b8ab34Slling 		nvlist_free(zhp->zpool_props);
279fa9e4066Sahrens 	free(zhp);
280fa9e4066Sahrens }
281fa9e4066Sahrens 
282fa9e4066Sahrens /*
283fa9e4066Sahrens  * Return the name of the pool.
284fa9e4066Sahrens  */
285fa9e4066Sahrens const char *
286fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
287fa9e4066Sahrens {
288fa9e4066Sahrens 	return (zhp->zpool_name);
289fa9e4066Sahrens }
290fa9e4066Sahrens 
291fa9e4066Sahrens /*
292fa9e4066Sahrens  * Return the GUID of the pool.
293fa9e4066Sahrens  */
294fa9e4066Sahrens uint64_t
295fa9e4066Sahrens zpool_get_guid(zpool_handle_t *zhp)
296fa9e4066Sahrens {
297fa9e4066Sahrens 	uint64_t guid;
298fa9e4066Sahrens 
299fa9e4066Sahrens 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
300fa9e4066Sahrens 	    &guid) == 0);
301fa9e4066Sahrens 	return (guid);
302fa9e4066Sahrens }
303fa9e4066Sahrens 
30499653d4eSeschrock /*
30599653d4eSeschrock  * Return the version of the pool.
30699653d4eSeschrock  */
30799653d4eSeschrock uint64_t
30899653d4eSeschrock zpool_get_version(zpool_handle_t *zhp)
30999653d4eSeschrock {
31099653d4eSeschrock 	uint64_t version;
31199653d4eSeschrock 
31299653d4eSeschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_VERSION,
31399653d4eSeschrock 	    &version) == 0);
31499653d4eSeschrock 
31599653d4eSeschrock 	return (version);
31699653d4eSeschrock }
31799653d4eSeschrock 
318fa9e4066Sahrens /*
319fa9e4066Sahrens  * Return the amount of space currently consumed by the pool.
320fa9e4066Sahrens  */
321fa9e4066Sahrens uint64_t
322fa9e4066Sahrens zpool_get_space_used(zpool_handle_t *zhp)
323fa9e4066Sahrens {
324fa9e4066Sahrens 	nvlist_t *nvroot;
325fa9e4066Sahrens 	vdev_stat_t *vs;
326fa9e4066Sahrens 	uint_t vsc;
327fa9e4066Sahrens 
328fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
329fa9e4066Sahrens 	    &nvroot) == 0);
330fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
331fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	return (vs->vs_alloc);
334fa9e4066Sahrens }
335fa9e4066Sahrens 
336fa9e4066Sahrens /*
337fa9e4066Sahrens  * Return the total space in the pool.
338fa9e4066Sahrens  */
339fa9e4066Sahrens uint64_t
340fa9e4066Sahrens zpool_get_space_total(zpool_handle_t *zhp)
341fa9e4066Sahrens {
342fa9e4066Sahrens 	nvlist_t *nvroot;
343fa9e4066Sahrens 	vdev_stat_t *vs;
344fa9e4066Sahrens 	uint_t vsc;
345fa9e4066Sahrens 
346fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
347fa9e4066Sahrens 	    &nvroot) == 0);
348fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
349fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
350fa9e4066Sahrens 
351fa9e4066Sahrens 	return (vs->vs_space);
352fa9e4066Sahrens }
353fa9e4066Sahrens 
354fa9e4066Sahrens /*
355fa9e4066Sahrens  * Return the alternate root for this pool, if any.
356fa9e4066Sahrens  */
357fa9e4066Sahrens int
358fa9e4066Sahrens zpool_get_root(zpool_handle_t *zhp, char *buf, size_t buflen)
359fa9e4066Sahrens {
360fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
361fa9e4066Sahrens 
362fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
36399653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 ||
364e9dbad6fSeschrock 	    zc.zc_value[0] == '\0')
365fa9e4066Sahrens 		return (-1);
366fa9e4066Sahrens 
367e9dbad6fSeschrock 	(void) strlcpy(buf, zc.zc_value, buflen);
368fa9e4066Sahrens 
369fa9e4066Sahrens 	return (0);
370fa9e4066Sahrens }
371fa9e4066Sahrens 
372fa9e4066Sahrens /*
373fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
374fa9e4066Sahrens  */
375fa9e4066Sahrens int
376fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
377fa9e4066Sahrens {
378fa9e4066Sahrens 	return (zhp->zpool_state);
379fa9e4066Sahrens }
380fa9e4066Sahrens 
381fa9e4066Sahrens /*
382fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
383fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
384fa9e4066Sahrens  * don't have to worry about error semantics.
385fa9e4066Sahrens  */
386fa9e4066Sahrens int
38799653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
38899653d4eSeschrock     const char *altroot)
389fa9e4066Sahrens {
390fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
39199653d4eSeschrock 	char msg[1024];
392fa9e4066Sahrens 
39399653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
39499653d4eSeschrock 	    "cannot create '%s'"), pool);
395fa9e4066Sahrens 
39699653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
39799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
398fa9e4066Sahrens 
39999653d4eSeschrock 	if (altroot != NULL && altroot[0] != '/')
400ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_BADPATH,
40199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "bad alternate root '%s'"), altroot));
402fa9e4066Sahrens 
403e9dbad6fSeschrock 	if (zcmd_write_src_nvlist(hdl, &zc, nvroot, NULL) != 0)
40499653d4eSeschrock 		return (-1);
40599653d4eSeschrock 
406fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
407fa9e4066Sahrens 
408fa9e4066Sahrens 	if (altroot != NULL)
409e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, altroot, sizeof (zc.zc_value));
410fa9e4066Sahrens 
41199653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_CREATE, &zc) != 0) {
412e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
413fa9e4066Sahrens 
41499653d4eSeschrock 		switch (errno) {
415fa9e4066Sahrens 		case EBUSY:
416fa9e4066Sahrens 			/*
417fa9e4066Sahrens 			 * This can happen if the user has specified the same
418fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
419fa9e4066Sahrens 			 * until we try to add it and see we already have a
420fa9e4066Sahrens 			 * label.
421fa9e4066Sahrens 			 */
42299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42399653d4eSeschrock 			    "one or more vdevs refer to the same device"));
42499653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
425fa9e4066Sahrens 
426fa9e4066Sahrens 		case EOVERFLOW:
427fa9e4066Sahrens 			/*
42899653d4eSeschrock 			 * This occurs when one of the devices is below
429fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
430fa9e4066Sahrens 			 * device was the problem device since there's no
431fa9e4066Sahrens 			 * reliable way to determine device size from userland.
432fa9e4066Sahrens 			 */
433fa9e4066Sahrens 			{
434fa9e4066Sahrens 				char buf[64];
435fa9e4066Sahrens 
436fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
437fa9e4066Sahrens 
43899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43999653d4eSeschrock 				    "one or more devices is less than the "
44099653d4eSeschrock 				    "minimum size (%s)"), buf);
441fa9e4066Sahrens 			}
44299653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
443fa9e4066Sahrens 
444fa9e4066Sahrens 		case ENOSPC:
44599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44699653d4eSeschrock 			    "one or more devices is out of space"));
44799653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
448fa9e4066Sahrens 
449fa9e4066Sahrens 		default:
45099653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
451fa9e4066Sahrens 		}
452fa9e4066Sahrens 	}
453fa9e4066Sahrens 
454e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
455fa9e4066Sahrens 
456fa9e4066Sahrens 	/*
457fa9e4066Sahrens 	 * If this is an alternate root pool, then we automatically set the
458e9dbad6fSeschrock 	 * mountpoint of the root dataset to be '/'.
459fa9e4066Sahrens 	 */
460fa9e4066Sahrens 	if (altroot != NULL) {
461fa9e4066Sahrens 		zfs_handle_t *zhp;
462fa9e4066Sahrens 
46399653d4eSeschrock 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_ANY)) != NULL);
464e9dbad6fSeschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
465e9dbad6fSeschrock 		    "/") == 0);
466fa9e4066Sahrens 
467fa9e4066Sahrens 		zfs_close(zhp);
468fa9e4066Sahrens 	}
469fa9e4066Sahrens 
470fa9e4066Sahrens 	return (0);
471fa9e4066Sahrens }
472fa9e4066Sahrens 
473fa9e4066Sahrens /*
474fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
475fa9e4066Sahrens  * datasets left in the pool.
476fa9e4066Sahrens  */
477fa9e4066Sahrens int
478fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp)
479fa9e4066Sahrens {
480fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
481fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
48299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
48399653d4eSeschrock 	char msg[1024];
484fa9e4066Sahrens 
485fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
48699653d4eSeschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
48799653d4eSeschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
488fa9e4066Sahrens 		return (-1);
489fa9e4066Sahrens 
4905ad82045Snd 	if (zpool_remove_zvol_links(zhp) != 0)
491fa9e4066Sahrens 		return (-1);
492fa9e4066Sahrens 
493fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
494fa9e4066Sahrens 
49599653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
49699653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
49799653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
498fa9e4066Sahrens 
49999653d4eSeschrock 		if (errno == EROFS) {
50099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50199653d4eSeschrock 			    "one or more devices is read only"));
50299653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
50399653d4eSeschrock 		} else {
50499653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
505fa9e4066Sahrens 		}
506fa9e4066Sahrens 
507fa9e4066Sahrens 		if (zfp)
508fa9e4066Sahrens 			zfs_close(zfp);
509fa9e4066Sahrens 		return (-1);
510fa9e4066Sahrens 	}
511fa9e4066Sahrens 
512fa9e4066Sahrens 	if (zfp) {
513fa9e4066Sahrens 		remove_mountpoint(zfp);
514fa9e4066Sahrens 		zfs_close(zfp);
515fa9e4066Sahrens 	}
516fa9e4066Sahrens 
517fa9e4066Sahrens 	return (0);
518fa9e4066Sahrens }
519fa9e4066Sahrens 
520fa9e4066Sahrens /*
521fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
522fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
523fa9e4066Sahrens  */
524fa9e4066Sahrens int
525fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
526fa9e4066Sahrens {
527e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
52899653d4eSeschrock 	int ret;
52999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
53099653d4eSeschrock 	char msg[1024];
53199653d4eSeschrock 	nvlist_t **spares;
53299653d4eSeschrock 	uint_t nspares;
53399653d4eSeschrock 
53499653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
53599653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
53699653d4eSeschrock 
53799653d4eSeschrock 	if (zpool_get_version(zhp) < ZFS_VERSION_SPARES &&
53899653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
53999653d4eSeschrock 	    &spares, &nspares) == 0) {
54099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
54199653d4eSeschrock 		    "upgraded to add hot spares"));
54299653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
54399653d4eSeschrock 	}
544fa9e4066Sahrens 
545e9dbad6fSeschrock 	if (zcmd_write_src_nvlist(hdl, &zc, nvroot, NULL) != 0)
54699653d4eSeschrock 		return (-1);
547fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
548fa9e4066Sahrens 
54999653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_ADD, &zc) != 0) {
550fa9e4066Sahrens 		switch (errno) {
551fa9e4066Sahrens 		case EBUSY:
552fa9e4066Sahrens 			/*
553fa9e4066Sahrens 			 * This can happen if the user has specified the same
554fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
555fa9e4066Sahrens 			 * until we try to add it and see we already have a
556fa9e4066Sahrens 			 * label.
557fa9e4066Sahrens 			 */
55899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
55999653d4eSeschrock 			    "one or more vdevs refer to the same device"));
56099653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
561fa9e4066Sahrens 			break;
562fa9e4066Sahrens 
563fa9e4066Sahrens 		case EOVERFLOW:
564fa9e4066Sahrens 			/*
565fa9e4066Sahrens 			 * This occurrs when one of the devices is below
566fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
567fa9e4066Sahrens 			 * device was the problem device since there's no
568fa9e4066Sahrens 			 * reliable way to determine device size from userland.
569fa9e4066Sahrens 			 */
570fa9e4066Sahrens 			{
571fa9e4066Sahrens 				char buf[64];
572fa9e4066Sahrens 
573fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
574fa9e4066Sahrens 
57599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
57699653d4eSeschrock 				    "device is less than the minimum "
57799653d4eSeschrock 				    "size (%s)"), buf);
578fa9e4066Sahrens 			}
57999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
58099653d4eSeschrock 			break;
58199653d4eSeschrock 
58299653d4eSeschrock 		case ENOTSUP:
58399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
58499653d4eSeschrock 			    "pool must be upgraded to add raidz2 vdevs"));
58599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
586fa9e4066Sahrens 			break;
587fa9e4066Sahrens 
588b1b8ab34Slling 		case EDOM:
589b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
590b1b8ab34Slling 			    "root pool can not have concatenated devices"));
591b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
592b1b8ab34Slling 			break;
593b1b8ab34Slling 
594fa9e4066Sahrens 		default:
59599653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
596fa9e4066Sahrens 		}
597fa9e4066Sahrens 
59899653d4eSeschrock 		ret = -1;
59999653d4eSeschrock 	} else {
60099653d4eSeschrock 		ret = 0;
601fa9e4066Sahrens 	}
602fa9e4066Sahrens 
603e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
604fa9e4066Sahrens 
60599653d4eSeschrock 	return (ret);
606fa9e4066Sahrens }
607fa9e4066Sahrens 
608fa9e4066Sahrens /*
609fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
610fa9e4066Sahrens  * mounted datasets in the pool.
611fa9e4066Sahrens  */
612fa9e4066Sahrens int
613fa9e4066Sahrens zpool_export(zpool_handle_t *zhp)
614fa9e4066Sahrens {
615fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
616fa9e4066Sahrens 
617fa9e4066Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
618fa9e4066Sahrens 		return (-1);
619fa9e4066Sahrens 
620fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
621fa9e4066Sahrens 
62299653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_EXPORT, &zc) != 0)
623ece3d9b3Slling 		return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
62499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot export '%s'"),
62599653d4eSeschrock 		    zhp->zpool_name));
626fa9e4066Sahrens 	return (0);
627fa9e4066Sahrens }
628fa9e4066Sahrens 
629fa9e4066Sahrens /*
630fa9e4066Sahrens  * Import the given pool using the known configuration.  The configuration
631fa9e4066Sahrens  * should have come from zpool_find_import().  The 'newname' and 'altroot'
632fa9e4066Sahrens  * parameters control whether the pool is imported with a different name or with
633fa9e4066Sahrens  * an alternate root, respectively.
634fa9e4066Sahrens  */
635fa9e4066Sahrens int
63699653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
63799653d4eSeschrock     const char *altroot)
638fa9e4066Sahrens {
639e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
640fa9e4066Sahrens 	char *thename;
641fa9e4066Sahrens 	char *origname;
642fa9e4066Sahrens 	int ret;
643fa9e4066Sahrens 
644fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
645fa9e4066Sahrens 	    &origname) == 0);
646fa9e4066Sahrens 
647fa9e4066Sahrens 	if (newname != NULL) {
64899653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
649ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
65099653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
65199653d4eSeschrock 			    newname));
652fa9e4066Sahrens 		thename = (char *)newname;
653fa9e4066Sahrens 	} else {
654fa9e4066Sahrens 		thename = origname;
655fa9e4066Sahrens 	}
656fa9e4066Sahrens 
65799653d4eSeschrock 	if (altroot != NULL && altroot[0] != '/')
658ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_BADPATH,
65999653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "bad alternate root '%s'"),
66099653d4eSeschrock 		    altroot));
661fa9e4066Sahrens 
662fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
663fa9e4066Sahrens 
664fa9e4066Sahrens 	if (altroot != NULL)
665e9dbad6fSeschrock 		(void) strlcpy(zc.zc_value, altroot, sizeof (zc.zc_value));
666fa9e4066Sahrens 	else
667e9dbad6fSeschrock 		zc.zc_value[0] = '\0';
668fa9e4066Sahrens 
669fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
670ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
671fa9e4066Sahrens 
672e9dbad6fSeschrock 	if (zcmd_write_src_nvlist(hdl, &zc, config, NULL) != 0)
67399653d4eSeschrock 		return (-1);
674fa9e4066Sahrens 
675fa9e4066Sahrens 	ret = 0;
67699653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
677fa9e4066Sahrens 		char desc[1024];
678fa9e4066Sahrens 		if (newname == NULL)
679fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
680fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
681fa9e4066Sahrens 			    thename);
682fa9e4066Sahrens 		else
683fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
684fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
685fa9e4066Sahrens 			    origname, thename);
686fa9e4066Sahrens 
687fa9e4066Sahrens 		switch (errno) {
688ea8dc4b6Seschrock 		case ENOTSUP:
689ea8dc4b6Seschrock 			/*
690ea8dc4b6Seschrock 			 * Unsupported version.
691ea8dc4b6Seschrock 			 */
69299653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
693ea8dc4b6Seschrock 			break;
694ea8dc4b6Seschrock 
695b5989ec7Seschrock 		case EINVAL:
696b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
697b5989ec7Seschrock 			break;
698b5989ec7Seschrock 
699fa9e4066Sahrens 		default:
70099653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, desc);
701fa9e4066Sahrens 		}
702fa9e4066Sahrens 
703fa9e4066Sahrens 		ret = -1;
704fa9e4066Sahrens 	} else {
705fa9e4066Sahrens 		zpool_handle_t *zhp;
706fa9e4066Sahrens 		/*
707fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
708fa9e4066Sahrens 		 */
70994de1d4cSeschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
71094de1d4cSeschrock 			ret = -1;
71194de1d4cSeschrock 		} else if (zhp != NULL) {
712fa9e4066Sahrens 			ret = zpool_create_zvol_links(zhp);
713fa9e4066Sahrens 			zpool_close(zhp);
714fa9e4066Sahrens 		}
715fa9e4066Sahrens 	}
716fa9e4066Sahrens 
717e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
718fa9e4066Sahrens 	return (ret);
719fa9e4066Sahrens }
720fa9e4066Sahrens 
721fa9e4066Sahrens /*
722fa9e4066Sahrens  * Scrub the pool.
723fa9e4066Sahrens  */
724fa9e4066Sahrens int
725fa9e4066Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
726fa9e4066Sahrens {
727fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
728fa9e4066Sahrens 	char msg[1024];
72999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
730fa9e4066Sahrens 
731fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
732fa9e4066Sahrens 	zc.zc_cookie = type;
733fa9e4066Sahrens 
73499653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_SCRUB, &zc) == 0)
735fa9e4066Sahrens 		return (0);
736fa9e4066Sahrens 
737fa9e4066Sahrens 	(void) snprintf(msg, sizeof (msg),
738fa9e4066Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
739fa9e4066Sahrens 
74099653d4eSeschrock 	if (errno == EBUSY)
74199653d4eSeschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
74299653d4eSeschrock 	else
74399653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
744fa9e4066Sahrens }
745fa9e4066Sahrens 
746a43d325bSek /*
747a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
748a43d325bSek  * spare; but FALSE if its an INUSE spare.
749a43d325bSek  */
75099653d4eSeschrock static nvlist_t *
75199653d4eSeschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
752a43d325bSek     boolean_t *avail_spare)
753ea8dc4b6Seschrock {
754ea8dc4b6Seschrock 	uint_t c, children;
755ea8dc4b6Seschrock 	nvlist_t **child;
75699653d4eSeschrock 	uint64_t theguid, present;
757ea8dc4b6Seschrock 	char *path;
758ea8dc4b6Seschrock 	uint64_t wholedisk = 0;
75999653d4eSeschrock 	nvlist_t *ret;
760ea8dc4b6Seschrock 
76199653d4eSeschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
762ea8dc4b6Seschrock 
763ea8dc4b6Seschrock 	if (search == NULL &&
764ea8dc4b6Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
765ea8dc4b6Seschrock 		/*
766ea8dc4b6Seschrock 		 * If the device has never been present since import, the only
767ea8dc4b6Seschrock 		 * reliable way to match the vdev is by GUID.
768ea8dc4b6Seschrock 		 */
76999653d4eSeschrock 		if (theguid == guid)
77099653d4eSeschrock 			return (nv);
771ea8dc4b6Seschrock 	} else if (search != NULL &&
772ea8dc4b6Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
773ea8dc4b6Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
774ea8dc4b6Seschrock 		    &wholedisk);
775ea8dc4b6Seschrock 		if (wholedisk) {
776ea8dc4b6Seschrock 			/*
777ea8dc4b6Seschrock 			 * For whole disks, the internal path has 's0', but the
778ea8dc4b6Seschrock 			 * path passed in by the user doesn't.
779ea8dc4b6Seschrock 			 */
780ea8dc4b6Seschrock 			if (strlen(search) == strlen(path) - 2 &&
781ea8dc4b6Seschrock 			    strncmp(search, path, strlen(search)) == 0)
78299653d4eSeschrock 				return (nv);
783ea8dc4b6Seschrock 		} else if (strcmp(search, path) == 0) {
78499653d4eSeschrock 			return (nv);
785ea8dc4b6Seschrock 		}
786ea8dc4b6Seschrock 	}
787ea8dc4b6Seschrock 
788ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
789ea8dc4b6Seschrock 	    &child, &children) != 0)
79099653d4eSeschrock 		return (NULL);
791ea8dc4b6Seschrock 
792ea8dc4b6Seschrock 	for (c = 0; c < children; c++)
79399653d4eSeschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
794a43d325bSek 		    avail_spare)) != NULL)
795ea8dc4b6Seschrock 			return (ret);
796ea8dc4b6Seschrock 
79799653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
79899653d4eSeschrock 	    &child, &children) == 0) {
79999653d4eSeschrock 		for (c = 0; c < children; c++) {
80099653d4eSeschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
801a43d325bSek 			    avail_spare)) != NULL) {
802a43d325bSek 				*avail_spare = B_TRUE;
80399653d4eSeschrock 				return (ret);
80499653d4eSeschrock 			}
80599653d4eSeschrock 		}
80699653d4eSeschrock 	}
80799653d4eSeschrock 
80899653d4eSeschrock 	return (NULL);
809ea8dc4b6Seschrock }
810ea8dc4b6Seschrock 
81199653d4eSeschrock nvlist_t *
812a43d325bSek zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare)
813ea8dc4b6Seschrock {
814ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
815ea8dc4b6Seschrock 	const char *search;
816ea8dc4b6Seschrock 	char *end;
817ea8dc4b6Seschrock 	nvlist_t *nvroot;
818ea8dc4b6Seschrock 	uint64_t guid;
819ea8dc4b6Seschrock 
8200917b783Seschrock 	guid = strtoull(path, &end, 10);
821ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
822ea8dc4b6Seschrock 		search = NULL;
823ea8dc4b6Seschrock 	} else if (path[0] != '/') {
824ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
825ea8dc4b6Seschrock 		search = buf;
826ea8dc4b6Seschrock 	} else {
827ea8dc4b6Seschrock 		search = path;
828ea8dc4b6Seschrock 	}
829ea8dc4b6Seschrock 
830ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
831ea8dc4b6Seschrock 	    &nvroot) == 0);
832ea8dc4b6Seschrock 
833a43d325bSek 	*avail_spare = B_FALSE;
834a43d325bSek 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare));
835a43d325bSek }
836a43d325bSek 
837a43d325bSek /*
838a43d325bSek  * Returns TRUE if the given guid corresponds to a spare (INUSE or not).
839a43d325bSek  */
840a43d325bSek static boolean_t
841a43d325bSek is_spare(zpool_handle_t *zhp, uint64_t guid)
842a43d325bSek {
843a43d325bSek 	uint64_t spare_guid;
844a43d325bSek 	nvlist_t *nvroot;
845a43d325bSek 	nvlist_t **spares;
846a43d325bSek 	uint_t nspares;
847a43d325bSek 	int i;
848a43d325bSek 
849a43d325bSek 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
850a43d325bSek 	    &nvroot) == 0);
851a43d325bSek 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
852a43d325bSek 	    &spares, &nspares) == 0) {
853a43d325bSek 		for (i = 0; i < nspares; i++) {
854a43d325bSek 			verify(nvlist_lookup_uint64(spares[i],
855a43d325bSek 			    ZPOOL_CONFIG_GUID, &spare_guid) == 0);
856a43d325bSek 			if (guid == spare_guid)
857a43d325bSek 				return (B_TRUE);
858a43d325bSek 		}
859a43d325bSek 	}
860a43d325bSek 
861a43d325bSek 	return (B_FALSE);
862ea8dc4b6Seschrock }
863ea8dc4b6Seschrock 
864fa9e4066Sahrens /*
865fa9e4066Sahrens  * Bring the specified vdev online
866fa9e4066Sahrens  */
867fa9e4066Sahrens int
868fa9e4066Sahrens zpool_vdev_online(zpool_handle_t *zhp, const char *path)
869fa9e4066Sahrens {
870fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
871fa9e4066Sahrens 	char msg[1024];
87299653d4eSeschrock 	nvlist_t *tgt;
873a43d325bSek 	boolean_t avail_spare;
87499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
875fa9e4066Sahrens 
876ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
877ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
878ea8dc4b6Seschrock 
879fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
880a43d325bSek 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == NULL)
88199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
882fa9e4066Sahrens 
88399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
884fa9e4066Sahrens 
885a43d325bSek 	if (avail_spare || is_spare(zhp, zc.zc_guid) == B_TRUE)
886a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
887a43d325bSek 
88899653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_ONLINE, &zc) == 0)
88999653d4eSeschrock 		return (0);
890fa9e4066Sahrens 
89199653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
892fa9e4066Sahrens }
893fa9e4066Sahrens 
894fa9e4066Sahrens /*
895fa9e4066Sahrens  * Take the specified vdev offline
896fa9e4066Sahrens  */
897fa9e4066Sahrens int
898441d80aaSlling zpool_vdev_offline(zpool_handle_t *zhp, const char *path, int istmp)
899fa9e4066Sahrens {
900fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
901fa9e4066Sahrens 	char msg[1024];
90299653d4eSeschrock 	nvlist_t *tgt;
903a43d325bSek 	boolean_t avail_spare;
90499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
905fa9e4066Sahrens 
906ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
907ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
908ea8dc4b6Seschrock 
909fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
910a43d325bSek 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == NULL)
91199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
91299653d4eSeschrock 
91399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
914fa9e4066Sahrens 
915a43d325bSek 	if (avail_spare || is_spare(zhp, zc.zc_guid) == B_TRUE)
916a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
917a43d325bSek 
918441d80aaSlling 	zc.zc_cookie = istmp;
919441d80aaSlling 
92099653d4eSeschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_OFFLINE, &zc) == 0)
921fa9e4066Sahrens 		return (0);
922fa9e4066Sahrens 
923fa9e4066Sahrens 	switch (errno) {
92499653d4eSeschrock 	case EBUSY:
925fa9e4066Sahrens 
926fa9e4066Sahrens 		/*
927fa9e4066Sahrens 		 * There are no other replicas of this device.
928fa9e4066Sahrens 		 */
92999653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
930fa9e4066Sahrens 
93199653d4eSeschrock 	default:
93299653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
933fa9e4066Sahrens 	}
93499653d4eSeschrock }
93599653d4eSeschrock 
93699653d4eSeschrock /*
93799653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
93899653d4eSeschrock  * a hot spare.
93999653d4eSeschrock  */
94099653d4eSeschrock static boolean_t
94199653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
94299653d4eSeschrock {
94399653d4eSeschrock 	nvlist_t **child;
94499653d4eSeschrock 	uint_t c, children;
94599653d4eSeschrock 	char *type;
94699653d4eSeschrock 
94799653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
94899653d4eSeschrock 	    &children) == 0) {
94999653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
95099653d4eSeschrock 		    &type) == 0);
95199653d4eSeschrock 
95299653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
95399653d4eSeschrock 		    children == 2 && child[which] == tgt)
95499653d4eSeschrock 			return (B_TRUE);
95599653d4eSeschrock 
95699653d4eSeschrock 		for (c = 0; c < children; c++)
95799653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
95899653d4eSeschrock 				return (B_TRUE);
95999653d4eSeschrock 	}
96099653d4eSeschrock 
96199653d4eSeschrock 	return (B_FALSE);
962fa9e4066Sahrens }
963fa9e4066Sahrens 
964fa9e4066Sahrens /*
965fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
966fa9e4066Sahrens  * If 'replacing' is specified, tne new disk will replace the old one.
967fa9e4066Sahrens  */
968fa9e4066Sahrens int
969fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
970fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
971fa9e4066Sahrens {
972fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
973fa9e4066Sahrens 	char msg[1024];
974fa9e4066Sahrens 	int ret;
97599653d4eSeschrock 	nvlist_t *tgt;
976a43d325bSek 	boolean_t avail_spare;
97799653d4eSeschrock 	uint64_t val;
97899653d4eSeschrock 	char *path;
97999653d4eSeschrock 	nvlist_t **child;
98099653d4eSeschrock 	uint_t children;
98199653d4eSeschrock 	nvlist_t *config_root;
98299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
983fa9e4066Sahrens 
984ea8dc4b6Seschrock 	if (replacing)
985ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
986ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
987ea8dc4b6Seschrock 	else
988ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
989ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
990ea8dc4b6Seschrock 
991fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
992a43d325bSek 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare)) == 0)
99399653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
99499653d4eSeschrock 
995a43d325bSek 	if (avail_spare)
99699653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
99799653d4eSeschrock 
99899653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
999fa9e4066Sahrens 	zc.zc_cookie = replacing;
1000fa9e4066Sahrens 
100199653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
100299653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
100399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
100499653d4eSeschrock 		    "new device must be a single disk"));
100599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
100699653d4eSeschrock 	}
100799653d4eSeschrock 
100899653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
100999653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
101099653d4eSeschrock 
101199653d4eSeschrock 	/*
101299653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
101399653d4eSeschrock 	 * replace it with another hot spare.
101499653d4eSeschrock 	 */
101599653d4eSeschrock 	if (replacing &&
101699653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
101799653d4eSeschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1018a43d325bSek 	    (zpool_find_vdev(zhp, path, &avail_spare) == NULL ||
1019a43d325bSek 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
102099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
102199653d4eSeschrock 		    "can only be replaced by another hot spare"));
102299653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
102399653d4eSeschrock 	}
102499653d4eSeschrock 
102599653d4eSeschrock 	/*
102699653d4eSeschrock 	 * If we are attempting to replace a spare, it canot be applied to an
102799653d4eSeschrock 	 * already spared device.
102899653d4eSeschrock 	 */
102999653d4eSeschrock 	if (replacing &&
103099653d4eSeschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1031a43d325bSek 	    zpool_find_vdev(zhp, path, &avail_spare) != NULL && avail_spare &&
103299653d4eSeschrock 	    is_replacing_spare(config_root, tgt, 0)) {
103399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
103499653d4eSeschrock 		    "device has already been replaced with a spare"));
103599653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
103699653d4eSeschrock 	}
103799653d4eSeschrock 
1038e9dbad6fSeschrock 	if (zcmd_write_src_nvlist(hdl, &zc, nvroot, NULL) != 0)
103999653d4eSeschrock 		return (-1);
1040fa9e4066Sahrens 
104199653d4eSeschrock 	ret = ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_ATTACH, &zc);
1042fa9e4066Sahrens 
1043e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1044fa9e4066Sahrens 
1045fa9e4066Sahrens 	if (ret == 0)
1046fa9e4066Sahrens 		return (0);
1047fa9e4066Sahrens 
1048fa9e4066Sahrens 	switch (errno) {
1049ea8dc4b6Seschrock 	case ENOTSUP:
1050fa9e4066Sahrens 		/*
1051fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
1052fa9e4066Sahrens 		 */
1053fa9e4066Sahrens 		if (replacing)
105499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105599653d4eSeschrock 			    "cannot replace a replacing device"));
1056fa9e4066Sahrens 		else
105799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105899653d4eSeschrock 			    "can only attach to mirrors and top-level "
105999653d4eSeschrock 			    "disks"));
106099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1061fa9e4066Sahrens 		break;
1062fa9e4066Sahrens 
1063ea8dc4b6Seschrock 	case EINVAL:
1064fa9e4066Sahrens 		/*
1065fa9e4066Sahrens 		 * The new device must be a single disk.
1066fa9e4066Sahrens 		 */
106799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106899653d4eSeschrock 		    "new device must be a single disk"));
106999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1070fa9e4066Sahrens 		break;
1071fa9e4066Sahrens 
1072ea8dc4b6Seschrock 	case EBUSY:
107399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
107499653d4eSeschrock 		    new_disk);
107599653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1076fa9e4066Sahrens 		break;
1077fa9e4066Sahrens 
1078ea8dc4b6Seschrock 	case EOVERFLOW:
1079fa9e4066Sahrens 		/*
1080fa9e4066Sahrens 		 * The new device is too small.
1081fa9e4066Sahrens 		 */
108299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
108399653d4eSeschrock 		    "device is too small"));
108499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1085fa9e4066Sahrens 		break;
1086fa9e4066Sahrens 
1087ea8dc4b6Seschrock 	case EDOM:
1088fa9e4066Sahrens 		/*
1089fa9e4066Sahrens 		 * The new device has a different alignment requirement.
1090fa9e4066Sahrens 		 */
109199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109299653d4eSeschrock 		    "devices have different sector alignment"));
109399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1094fa9e4066Sahrens 		break;
1095fa9e4066Sahrens 
1096ea8dc4b6Seschrock 	case ENAMETOOLONG:
1097fa9e4066Sahrens 		/*
1098fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1099fa9e4066Sahrens 		 */
110099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1101fa9e4066Sahrens 		break;
1102fa9e4066Sahrens 
1103ea8dc4b6Seschrock 	default:
110499653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
1105fa9e4066Sahrens 	}
1106fa9e4066Sahrens 
110799653d4eSeschrock 	return (-1);
1108fa9e4066Sahrens }
1109fa9e4066Sahrens 
1110fa9e4066Sahrens /*
1111fa9e4066Sahrens  * Detach the specified device.
1112fa9e4066Sahrens  */
1113fa9e4066Sahrens int
1114fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1115fa9e4066Sahrens {
1116fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1117fa9e4066Sahrens 	char msg[1024];
111899653d4eSeschrock 	nvlist_t *tgt;
1119a43d325bSek 	boolean_t avail_spare;
112099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1121fa9e4066Sahrens 
1122ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
1123ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1124ea8dc4b6Seschrock 
1125fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1126a43d325bSek 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == 0)
112799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1128fa9e4066Sahrens 
1129a43d325bSek 	if (avail_spare)
113099653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
113199653d4eSeschrock 
113299653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
113399653d4eSeschrock 
113499653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1135fa9e4066Sahrens 		return (0);
1136fa9e4066Sahrens 
1137fa9e4066Sahrens 	switch (errno) {
1138fa9e4066Sahrens 
1139ea8dc4b6Seschrock 	case ENOTSUP:
1140fa9e4066Sahrens 		/*
1141fa9e4066Sahrens 		 * Can't detach from this type of vdev.
1142fa9e4066Sahrens 		 */
114399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
114499653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
114599653d4eSeschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1146fa9e4066Sahrens 		break;
1147fa9e4066Sahrens 
1148ea8dc4b6Seschrock 	case EBUSY:
1149fa9e4066Sahrens 		/*
1150fa9e4066Sahrens 		 * There are no other replicas of this device.
1151fa9e4066Sahrens 		 */
115299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1153fa9e4066Sahrens 		break;
1154fa9e4066Sahrens 
1155ea8dc4b6Seschrock 	default:
115699653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
1157ea8dc4b6Seschrock 	}
1158ea8dc4b6Seschrock 
115999653d4eSeschrock 	return (-1);
116099653d4eSeschrock }
116199653d4eSeschrock 
116299653d4eSeschrock /*
116399653d4eSeschrock  * Remove the given device.  Currently, this is supported only for hot spares.
116499653d4eSeschrock  */
116599653d4eSeschrock int
116699653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
116799653d4eSeschrock {
116899653d4eSeschrock 	zfs_cmd_t zc = { 0 };
116999653d4eSeschrock 	char msg[1024];
117099653d4eSeschrock 	nvlist_t *tgt;
1171a43d325bSek 	boolean_t avail_spare;
117299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
117399653d4eSeschrock 
117499653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
117599653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
117699653d4eSeschrock 
117799653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1178a43d325bSek 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == 0)
117999653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
118099653d4eSeschrock 
1181a43d325bSek 	if (!avail_spare) {
118299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118339c23413Seschrock 		    "only inactive hot spares can be removed"));
118499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
118599653d4eSeschrock 	}
118699653d4eSeschrock 
118799653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
118899653d4eSeschrock 
118999653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
119099653d4eSeschrock 		return (0);
119199653d4eSeschrock 
119299653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
1193ea8dc4b6Seschrock }
1194ea8dc4b6Seschrock 
1195ea8dc4b6Seschrock /*
1196ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
1197ea8dc4b6Seschrock  */
1198ea8dc4b6Seschrock int
1199ea8dc4b6Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
1200ea8dc4b6Seschrock {
1201ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
1202ea8dc4b6Seschrock 	char msg[1024];
120399653d4eSeschrock 	nvlist_t *tgt;
1204a43d325bSek 	boolean_t avail_spare;
120599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1206ea8dc4b6Seschrock 
1207ea8dc4b6Seschrock 	if (path)
1208ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
1209ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
1210e9dbad6fSeschrock 		    path);
1211ea8dc4b6Seschrock 	else
1212ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
1213ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
1214ea8dc4b6Seschrock 		    zhp->zpool_name);
1215ea8dc4b6Seschrock 
1216ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
121799653d4eSeschrock 	if (path) {
1218a43d325bSek 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == 0)
121999653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
1220ea8dc4b6Seschrock 
1221a43d325bSek 		if (avail_spare)
122299653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
1223ea8dc4b6Seschrock 
122499653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
122599653d4eSeschrock 		    &zc.zc_guid) == 0);
1226fa9e4066Sahrens 	}
1227fa9e4066Sahrens 
122899653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
122999653d4eSeschrock 		return (0);
123099653d4eSeschrock 
123199653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
1232fa9e4066Sahrens }
1233fa9e4066Sahrens 
1234f3861e1aSahl /*
1235f3861e1aSahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
1236f3861e1aSahl  * hierarchy.
1237f3861e1aSahl  */
1238f3861e1aSahl int
1239f3861e1aSahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
1240f3861e1aSahl     void *data)
1241fa9e4066Sahrens {
1242f3861e1aSahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1243f3861e1aSahl 	char (*paths)[MAXPATHLEN];
1244f3861e1aSahl 	size_t size = 4;
1245f3861e1aSahl 	int curr, fd, base, ret = 0;
1246f3861e1aSahl 	DIR *dirp;
1247f3861e1aSahl 	struct dirent *dp;
1248f3861e1aSahl 	struct stat st;
1249f3861e1aSahl 
1250f3861e1aSahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
1251f3861e1aSahl 		return (errno == ENOENT ? 0 : -1);
1252f3861e1aSahl 
1253f3861e1aSahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
1254f3861e1aSahl 		int err = errno;
1255f3861e1aSahl 		(void) close(base);
1256f3861e1aSahl 		return (err == ENOENT ? 0 : -1);
1257f3861e1aSahl 	}
1258fa9e4066Sahrens 
1259fa9e4066Sahrens 	/*
1260f3861e1aSahl 	 * Oddly this wasn't a directory -- ignore that failure since we
1261f3861e1aSahl 	 * know there are no links lower in the (non-existant) hierarchy.
1262fa9e4066Sahrens 	 */
1263f3861e1aSahl 	if (!S_ISDIR(st.st_mode)) {
1264f3861e1aSahl 		(void) close(base);
1265f3861e1aSahl 		return (0);
1266fa9e4066Sahrens 	}
1267fa9e4066Sahrens 
1268f3861e1aSahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
1269f3861e1aSahl 		(void) close(base);
1270f3861e1aSahl 		return (-1);
1271f3861e1aSahl 	}
1272f3861e1aSahl 
1273f3861e1aSahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
1274f3861e1aSahl 	curr = 0;
1275f3861e1aSahl 
1276f3861e1aSahl 	while (curr >= 0) {
1277f3861e1aSahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
1278f3861e1aSahl 			goto err;
1279f3861e1aSahl 
1280f3861e1aSahl 		if (S_ISDIR(st.st_mode)) {
1281f3861e1aSahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
1282f3861e1aSahl 				goto err;
1283f3861e1aSahl 
1284f3861e1aSahl 			if ((dirp = fdopendir(fd)) == NULL) {
1285f3861e1aSahl 				(void) close(fd);
1286f3861e1aSahl 				goto err;
1287f3861e1aSahl 			}
1288f3861e1aSahl 
1289f3861e1aSahl 			while ((dp = readdir(dirp)) != NULL) {
1290f3861e1aSahl 				if (dp->d_name[0] == '.')
1291f3861e1aSahl 					continue;
1292f3861e1aSahl 
1293f3861e1aSahl 				if (curr + 1 == size) {
1294f3861e1aSahl 					paths = zfs_realloc(hdl, paths,
1295f3861e1aSahl 					    size * sizeof (paths[0]),
1296f3861e1aSahl 					    size * 2 * sizeof (paths[0]));
1297f3861e1aSahl 					if (paths == NULL) {
1298f3861e1aSahl 						(void) closedir(dirp);
1299f3861e1aSahl 						(void) close(fd);
1300f3861e1aSahl 						goto err;
1301f3861e1aSahl 					}
1302f3861e1aSahl 
1303f3861e1aSahl 					size *= 2;
1304f3861e1aSahl 				}
1305f3861e1aSahl 
1306f3861e1aSahl 				(void) strlcpy(paths[curr + 1], paths[curr],
1307f3861e1aSahl 				    sizeof (paths[curr + 1]));
1308f3861e1aSahl 				(void) strlcat(paths[curr], "/",
1309f3861e1aSahl 				    sizeof (paths[curr]));
1310f3861e1aSahl 				(void) strlcat(paths[curr], dp->d_name,
1311f3861e1aSahl 				    sizeof (paths[curr]));
1312f3861e1aSahl 				curr++;
1313f3861e1aSahl 			}
1314f3861e1aSahl 
1315f3861e1aSahl 			(void) closedir(dirp);
1316f3861e1aSahl 
1317f3861e1aSahl 		} else {
1318f3861e1aSahl 			if ((ret = cb(paths[curr], data)) != 0)
1319f3861e1aSahl 				break;
1320f3861e1aSahl 		}
1321f3861e1aSahl 
1322f3861e1aSahl 		curr--;
1323f3861e1aSahl 	}
1324f3861e1aSahl 
1325f3861e1aSahl 	free(paths);
1326f3861e1aSahl 	(void) close(base);
1327f3861e1aSahl 
1328f3861e1aSahl 	return (ret);
1329f3861e1aSahl 
1330f3861e1aSahl err:
1331f3861e1aSahl 	free(paths);
1332f3861e1aSahl 	(void) close(base);
1333f3861e1aSahl 	return (-1);
1334f3861e1aSahl }
1335f3861e1aSahl 
1336f3861e1aSahl typedef struct zvol_cb {
1337f3861e1aSahl 	zpool_handle_t *zcb_pool;
1338f3861e1aSahl 	boolean_t zcb_create;
1339f3861e1aSahl } zvol_cb_t;
1340f3861e1aSahl 
1341f3861e1aSahl /*ARGSUSED*/
1342f3861e1aSahl static int
1343f3861e1aSahl do_zvol_create(zfs_handle_t *zhp, void *data)
1344f3861e1aSahl {
1345f3861e1aSahl 	int ret;
1346f3861e1aSahl 
1347f3861e1aSahl 	if (ZFS_IS_VOLUME(zhp))
1348f3861e1aSahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
1349f3861e1aSahl 
1350f3861e1aSahl 	ret = zfs_iter_children(zhp, do_zvol_create, NULL);
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	zfs_close(zhp);
1353f3861e1aSahl 
1354fa9e4066Sahrens 	return (ret);
1355fa9e4066Sahrens }
1356fa9e4066Sahrens 
1357fa9e4066Sahrens /*
1358fa9e4066Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
1359fa9e4066Sahrens  */
1360fa9e4066Sahrens int
1361fa9e4066Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
1362fa9e4066Sahrens {
1363fa9e4066Sahrens 	zfs_handle_t *zfp;
1364fa9e4066Sahrens 	int ret;
1365fa9e4066Sahrens 
1366fa9e4066Sahrens 	/*
1367fa9e4066Sahrens 	 * If the pool is unavailable, just return success.
1368fa9e4066Sahrens 	 */
136999653d4eSeschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
137099653d4eSeschrock 	    zhp->zpool_name)) == NULL)
1371fa9e4066Sahrens 		return (0);
1372fa9e4066Sahrens 
1373f3861e1aSahl 	ret = zfs_iter_children(zfp, do_zvol_create, NULL);
1374fa9e4066Sahrens 
1375fa9e4066Sahrens 	zfs_close(zfp);
1376fa9e4066Sahrens 	return (ret);
1377fa9e4066Sahrens }
1378fa9e4066Sahrens 
1379f3861e1aSahl static int
1380f3861e1aSahl do_zvol_remove(const char *dataset, void *data)
1381f3861e1aSahl {
1382f3861e1aSahl 	zpool_handle_t *zhp = data;
1383f3861e1aSahl 
1384f3861e1aSahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
1385f3861e1aSahl }
1386f3861e1aSahl 
1387fa9e4066Sahrens /*
1388f3861e1aSahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
1389f3861e1aSahl  * by examining the /dev links so that a corrupted pool doesn't impede this
1390f3861e1aSahl  * operation.
1391fa9e4066Sahrens  */
1392fa9e4066Sahrens int
1393fa9e4066Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
1394fa9e4066Sahrens {
1395f3861e1aSahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
1396fa9e4066Sahrens }
1397c67d9675Seschrock 
1398c67d9675Seschrock /*
1399c67d9675Seschrock  * Convert from a devid string to a path.
1400c67d9675Seschrock  */
1401c67d9675Seschrock static char *
1402c67d9675Seschrock devid_to_path(char *devid_str)
1403c67d9675Seschrock {
1404c67d9675Seschrock 	ddi_devid_t devid;
1405c67d9675Seschrock 	char *minor;
1406c67d9675Seschrock 	char *path;
1407c67d9675Seschrock 	devid_nmlist_t *list = NULL;
1408c67d9675Seschrock 	int ret;
1409c67d9675Seschrock 
1410c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
1411c67d9675Seschrock 		return (NULL);
1412c67d9675Seschrock 
1413c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
1414c67d9675Seschrock 
1415c67d9675Seschrock 	devid_str_free(minor);
1416c67d9675Seschrock 	devid_free(devid);
1417c67d9675Seschrock 
1418c67d9675Seschrock 	if (ret != 0)
1419c67d9675Seschrock 		return (NULL);
1420c67d9675Seschrock 
142199653d4eSeschrock 	if ((path = strdup(list[0].devname)) == NULL)
142299653d4eSeschrock 		return (NULL);
142399653d4eSeschrock 
1424c67d9675Seschrock 	devid_free_nmlist(list);
1425c67d9675Seschrock 
1426c67d9675Seschrock 	return (path);
1427c67d9675Seschrock }
1428c67d9675Seschrock 
1429c67d9675Seschrock /*
1430c67d9675Seschrock  * Convert from a path to a devid string.
1431c67d9675Seschrock  */
1432c67d9675Seschrock static char *
1433c67d9675Seschrock path_to_devid(const char *path)
1434c67d9675Seschrock {
1435c67d9675Seschrock 	int fd;
1436c67d9675Seschrock 	ddi_devid_t devid;
1437c67d9675Seschrock 	char *minor, *ret;
1438c67d9675Seschrock 
1439c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
1440c67d9675Seschrock 		return (NULL);
1441c67d9675Seschrock 
1442c67d9675Seschrock 	minor = NULL;
1443c67d9675Seschrock 	ret = NULL;
1444c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
1445c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
1446c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
1447c67d9675Seschrock 		if (minor != NULL)
1448c67d9675Seschrock 			devid_str_free(minor);
1449c67d9675Seschrock 		devid_free(devid);
1450c67d9675Seschrock 	}
1451c67d9675Seschrock 	(void) close(fd);
1452c67d9675Seschrock 
1453c67d9675Seschrock 	return (ret);
1454c67d9675Seschrock }
1455c67d9675Seschrock 
1456c67d9675Seschrock /*
1457c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
1458c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
1459c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
1460c67d9675Seschrock  */
1461c67d9675Seschrock static void
1462c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
1463c67d9675Seschrock {
1464c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
1465c67d9675Seschrock 
1466c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1467e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
1468c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1469ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1470c67d9675Seschrock 
147199653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
1472c67d9675Seschrock }
1473c67d9675Seschrock 
1474c67d9675Seschrock /*
1475c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
1476c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
1477c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
1478c67d9675Seschrock  * trailing 's0' slice name.
1479c67d9675Seschrock  *
1480c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
1481c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
1482c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
1483c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
1484c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
1485c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
1486c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
1487c67d9675Seschrock  * of these checks.
1488c67d9675Seschrock  */
1489c67d9675Seschrock char *
149099653d4eSeschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
1491c67d9675Seschrock {
1492c67d9675Seschrock 	char *path, *devid;
1493ea8dc4b6Seschrock 	uint64_t value;
1494ea8dc4b6Seschrock 	char buf[64];
1495c67d9675Seschrock 
1496ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
1497ea8dc4b6Seschrock 	    &value) == 0) {
1498ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1499ea8dc4b6Seschrock 		    &value) == 0);
15005ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
15015ad82045Snd 		    (u_longlong_t)value);
1502ea8dc4b6Seschrock 		path = buf;
1503ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1504c67d9675Seschrock 
1505c67d9675Seschrock 		if (zhp != NULL &&
1506c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
1507c67d9675Seschrock 			/*
1508c67d9675Seschrock 			 * Determine if the current path is correct.
1509c67d9675Seschrock 			 */
1510c67d9675Seschrock 			char *newdevid = path_to_devid(path);
1511c67d9675Seschrock 
1512c67d9675Seschrock 			if (newdevid == NULL ||
1513c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
1514c67d9675Seschrock 				char *newpath;
1515c67d9675Seschrock 
1516c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
1517c67d9675Seschrock 					/*
1518c67d9675Seschrock 					 * Update the path appropriately.
1519c67d9675Seschrock 					 */
1520c67d9675Seschrock 					set_path(zhp, nv, newpath);
152199653d4eSeschrock 					if (nvlist_add_string(nv,
152299653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
152399653d4eSeschrock 						verify(nvlist_lookup_string(nv,
152499653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
152599653d4eSeschrock 						    &path) == 0);
1526c67d9675Seschrock 					free(newpath);
1527c67d9675Seschrock 				}
1528c67d9675Seschrock 			}
1529c67d9675Seschrock 
153099653d4eSeschrock 			if (newdevid)
153199653d4eSeschrock 				devid_str_free(newdevid);
1532c67d9675Seschrock 		}
1533c67d9675Seschrock 
1534c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
1535c67d9675Seschrock 			path += 9;
1536c67d9675Seschrock 
1537c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1538ea8dc4b6Seschrock 		    &value) == 0 && value) {
153999653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
154099653d4eSeschrock 			if (tmp == NULL)
154199653d4eSeschrock 				return (NULL);
1542c67d9675Seschrock 			tmp[strlen(path) - 2] = '\0';
1543c67d9675Seschrock 			return (tmp);
1544c67d9675Seschrock 		}
1545c67d9675Seschrock 	} else {
1546c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
154799653d4eSeschrock 
154899653d4eSeschrock 		/*
154999653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
155099653d4eSeschrock 		 */
155199653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
155299653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
155399653d4eSeschrock 			    &value) == 0);
155499653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
15555ad82045Snd 			    (u_longlong_t)value);
155699653d4eSeschrock 			path = buf;
155799653d4eSeschrock 		}
1558c67d9675Seschrock 	}
1559c67d9675Seschrock 
156099653d4eSeschrock 	return (zfs_strdup(hdl, path));
1561c67d9675Seschrock }
1562ea8dc4b6Seschrock 
1563ea8dc4b6Seschrock static int
1564ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b)
1565ea8dc4b6Seschrock {
1566ea8dc4b6Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
1567ea8dc4b6Seschrock }
1568ea8dc4b6Seschrock 
1569ea8dc4b6Seschrock /*
1570ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
1571ea8dc4b6Seschrock  * caller.
1572ea8dc4b6Seschrock  */
1573ea8dc4b6Seschrock int
157455434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
1575ea8dc4b6Seschrock {
1576ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
1577ea8dc4b6Seschrock 	uint64_t count;
1578e9dbad6fSeschrock 	zbookmark_t *zb = NULL;
157955434c77Sek 	int i;
1580ea8dc4b6Seschrock 
1581ea8dc4b6Seschrock 	/*
1582ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
1583ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
1584ea8dc4b6Seschrock 	 * entire list.
1585ea8dc4b6Seschrock 	 */
1586ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
1587ea8dc4b6Seschrock 	    &count) == 0);
1588e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
15895ad82045Snd 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
159099653d4eSeschrock 		return (-1);
1591e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
1592ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
1593ea8dc4b6Seschrock 	for (;;) {
159499653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
159599653d4eSeschrock 		    &zc) != 0) {
1596e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
1597ea8dc4b6Seschrock 			if (errno == ENOMEM) {
1598bf561db0Svb 				count = zc.zc_nvlist_dst_size;
1599e9dbad6fSeschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
1600bf561db0Svb 				    zfs_alloc(zhp->zpool_hdl, count *
1601bf561db0Svb 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
160299653d4eSeschrock 					return (-1);
1603ea8dc4b6Seschrock 			} else {
1604ea8dc4b6Seschrock 				return (-1);
1605ea8dc4b6Seschrock 			}
1606ea8dc4b6Seschrock 		} else {
1607ea8dc4b6Seschrock 			break;
1608ea8dc4b6Seschrock 		}
1609ea8dc4b6Seschrock 	}
1610ea8dc4b6Seschrock 
1611ea8dc4b6Seschrock 	/*
1612ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
1613ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
1614e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
1615ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
1616ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
1617ea8dc4b6Seschrock 	 */
1618e9dbad6fSeschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
1619e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
1620e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
1621ea8dc4b6Seschrock 
1622ea8dc4b6Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
1623ea8dc4b6Seschrock 
162455434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
1625ea8dc4b6Seschrock 
1626ea8dc4b6Seschrock 	/*
162755434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
1628ea8dc4b6Seschrock 	 */
1629ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
1630ea8dc4b6Seschrock 		nvlist_t *nv;
1631ea8dc4b6Seschrock 
1632c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
1633c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
1634c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
1635ea8dc4b6Seschrock 			continue;
1636ea8dc4b6Seschrock 
163755434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
163855434c77Sek 			goto nomem;
163955434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
164055434c77Sek 		    zb[i].zb_objset) != 0) {
164155434c77Sek 			nvlist_free(nv);
164299653d4eSeschrock 			goto nomem;
1643ea8dc4b6Seschrock 		}
164455434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
164555434c77Sek 		    zb[i].zb_object) != 0) {
164655434c77Sek 			nvlist_free(nv);
164755434c77Sek 			goto nomem;
164855434c77Sek 		}
164955434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
165055434c77Sek 			nvlist_free(nv);
165155434c77Sek 			goto nomem;
165255434c77Sek 		}
165355434c77Sek 		nvlist_free(nv);
1654ea8dc4b6Seschrock 	}
1655ea8dc4b6Seschrock 
16563ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
1657ea8dc4b6Seschrock 	return (0);
165899653d4eSeschrock 
165999653d4eSeschrock nomem:
1660e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
166199653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
1662ea8dc4b6Seschrock }
1663eaca9bbdSeschrock 
1664eaca9bbdSeschrock /*
1665eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
1666eaca9bbdSeschrock  */
1667eaca9bbdSeschrock int
1668eaca9bbdSeschrock zpool_upgrade(zpool_handle_t *zhp)
1669eaca9bbdSeschrock {
1670eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
167199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1672eaca9bbdSeschrock 
1673eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
167499653d4eSeschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
1675ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
167699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
167799653d4eSeschrock 		    zhp->zpool_name));
1678eaca9bbdSeschrock 
1679eaca9bbdSeschrock 	return (0);
1680eaca9bbdSeschrock }
168106eeb2adSek 
168206eeb2adSek /*
168306eeb2adSek  * Log command history.
168406eeb2adSek  *
168506eeb2adSek  * 'pool' is B_TRUE if we are logging a command for 'zpool'; B_FALSE
168606eeb2adSek  * otherwise ('zfs').  'pool_create' is B_TRUE if we are logging the creation
168706eeb2adSek  * of the pool; B_FALSE otherwise.  'path' is the pathanme containing the
168806eeb2adSek  * poolname.  'argc' and 'argv' are used to construct the command string.
168906eeb2adSek  */
169006eeb2adSek void
169106eeb2adSek zpool_log_history(libzfs_handle_t *hdl, int argc, char **argv, const char *path,
1692b1b8ab34Slling 	boolean_t pool, boolean_t pool_create)
169306eeb2adSek {
169406eeb2adSek 	char cmd_buf[HIS_MAX_RECORD_LEN];
169506eeb2adSek 	char *dspath;
169606eeb2adSek 	zfs_cmd_t zc = { 0 };
169706eeb2adSek 	int i;
169806eeb2adSek 
169906eeb2adSek 	/* construct the command string */
170006eeb2adSek 	(void) strcpy(cmd_buf, pool ? "zpool" : "zfs");
170106eeb2adSek 	for (i = 0; i < argc; i++) {
170206eeb2adSek 		if (strlen(cmd_buf) + 1 + strlen(argv[i]) > HIS_MAX_RECORD_LEN)
170306eeb2adSek 			break;
170406eeb2adSek 		(void) strcat(cmd_buf, " ");
170506eeb2adSek 		(void) strcat(cmd_buf, argv[i]);
170606eeb2adSek 	}
170706eeb2adSek 
170806eeb2adSek 	/* figure out the poolname */
170906eeb2adSek 	dspath = strpbrk(path, "/@");
171006eeb2adSek 	if (dspath == NULL) {
171106eeb2adSek 		(void) strcpy(zc.zc_name, path);
171206eeb2adSek 	} else {
171306eeb2adSek 		(void) strncpy(zc.zc_name, path, dspath - path);
171406eeb2adSek 		zc.zc_name[dspath-path] = '\0';
171506eeb2adSek 	}
171606eeb2adSek 
171706eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)cmd_buf;
171806eeb2adSek 	zc.zc_history_len = strlen(cmd_buf);
171906eeb2adSek 
172006eeb2adSek 	/* overloading zc_history_offset */
172106eeb2adSek 	zc.zc_history_offset = pool_create;
172206eeb2adSek 
172306eeb2adSek 	(void) ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_LOG_HISTORY, &zc);
172406eeb2adSek }
172506eeb2adSek 
172606eeb2adSek /*
172706eeb2adSek  * Perform ioctl to get some command history of a pool.
172806eeb2adSek  *
172906eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
173006eeb2adSek  * logical offset of the history buffer to start reading from.
173106eeb2adSek  *
173206eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
173306eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
173406eeb2adSek  */
173506eeb2adSek static int
173606eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
173706eeb2adSek {
173806eeb2adSek 	zfs_cmd_t zc = { 0 };
173906eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
174006eeb2adSek 
174106eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
174206eeb2adSek 
174306eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
174406eeb2adSek 	zc.zc_history_len = *len;
174506eeb2adSek 	zc.zc_history_offset = *off;
174606eeb2adSek 
174706eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
174806eeb2adSek 		switch (errno) {
174906eeb2adSek 		case EPERM:
1750ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
1751ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
175206eeb2adSek 			    "cannot show history for pool '%s'"),
175306eeb2adSek 			    zhp->zpool_name));
175406eeb2adSek 		case ENOENT:
1755ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
175606eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
175706eeb2adSek 			    "'%s'"), zhp->zpool_name));
1758d7306b64Sek 		case ENOTSUP:
1759d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
1760d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
1761d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
176206eeb2adSek 		default:
1763ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
176406eeb2adSek 			    dgettext(TEXT_DOMAIN,
176506eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
176606eeb2adSek 		}
176706eeb2adSek 	}
176806eeb2adSek 
176906eeb2adSek 	*len = zc.zc_history_len;
177006eeb2adSek 	*off = zc.zc_history_offset;
177106eeb2adSek 
177206eeb2adSek 	return (0);
177306eeb2adSek }
177406eeb2adSek 
177506eeb2adSek /*
177606eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
177706eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
177806eeb2adSek  * processed as there wasn't a complete record.
177906eeb2adSek  */
178006eeb2adSek static int
178106eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
178206eeb2adSek     nvlist_t ***records, uint_t *numrecords)
178306eeb2adSek {
178406eeb2adSek 	uint64_t reclen;
178506eeb2adSek 	nvlist_t *nv;
178606eeb2adSek 	int i;
178706eeb2adSek 
178806eeb2adSek 	while (bytes_read > sizeof (reclen)) {
178906eeb2adSek 
179006eeb2adSek 		/* get length of packed record (stored as little endian) */
179106eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
179206eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
179306eeb2adSek 
179406eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
179506eeb2adSek 			break;
179606eeb2adSek 
179706eeb2adSek 		/* unpack record */
179806eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
179906eeb2adSek 			return (ENOMEM);
180006eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
180106eeb2adSek 		buf += sizeof (reclen) + reclen;
180206eeb2adSek 
180306eeb2adSek 		/* add record to nvlist array */
180406eeb2adSek 		(*numrecords)++;
180506eeb2adSek 		if (ISP2(*numrecords + 1)) {
180606eeb2adSek 			*records = realloc(*records,
180706eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
180806eeb2adSek 		}
180906eeb2adSek 		(*records)[*numrecords - 1] = nv;
181006eeb2adSek 	}
181106eeb2adSek 
181206eeb2adSek 	*leftover = bytes_read;
181306eeb2adSek 	return (0);
181406eeb2adSek }
181506eeb2adSek 
181606eeb2adSek #define	HIS_BUF_LEN	(128*1024)
181706eeb2adSek 
181806eeb2adSek /*
181906eeb2adSek  * Retrieve the command history of a pool.
182006eeb2adSek  */
182106eeb2adSek int
182206eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
182306eeb2adSek {
182406eeb2adSek 	char buf[HIS_BUF_LEN];
182506eeb2adSek 	uint64_t off = 0;
182606eeb2adSek 	nvlist_t **records = NULL;
182706eeb2adSek 	uint_t numrecords = 0;
182806eeb2adSek 	int err, i;
182906eeb2adSek 
183006eeb2adSek 	do {
183106eeb2adSek 		uint64_t bytes_read = sizeof (buf);
183206eeb2adSek 		uint64_t leftover;
183306eeb2adSek 
183406eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
183506eeb2adSek 			break;
183606eeb2adSek 
183706eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
183806eeb2adSek 		if (!bytes_read)
183906eeb2adSek 			break;
184006eeb2adSek 
184106eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
184206eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
184306eeb2adSek 			break;
184406eeb2adSek 		off -= leftover;
184506eeb2adSek 
184606eeb2adSek 		/* CONSTCOND */
184706eeb2adSek 	} while (1);
184806eeb2adSek 
184906eeb2adSek 	if (!err) {
185006eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
185106eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
185206eeb2adSek 		    records, numrecords) == 0);
185306eeb2adSek 	}
185406eeb2adSek 	for (i = 0; i < numrecords; i++)
185506eeb2adSek 		nvlist_free(records[i]);
185606eeb2adSek 	free(records);
185706eeb2adSek 
185806eeb2adSek 	return (err);
185906eeb2adSek }
186055434c77Sek 
186155434c77Sek void
186255434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
186355434c77Sek     char *pathname, size_t len)
186455434c77Sek {
186555434c77Sek 	zfs_cmd_t zc = { 0 };
186655434c77Sek 	boolean_t mounted = B_FALSE;
186755434c77Sek 	char *mntpnt = NULL;
186855434c77Sek 	char dsname[MAXNAMELEN];
186955434c77Sek 
187055434c77Sek 	if (dsobj == 0) {
187155434c77Sek 		/* special case for the MOS */
187255434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
187355434c77Sek 		return;
187455434c77Sek 	}
187555434c77Sek 
187655434c77Sek 	/* get the dataset's name */
187755434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
187855434c77Sek 	zc.zc_obj = dsobj;
187955434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
188055434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
188155434c77Sek 		/* just write out a path of two object numbers */
188255434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
188355434c77Sek 		    dsobj, obj);
188455434c77Sek 		return;
188555434c77Sek 	}
188655434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
188755434c77Sek 
188855434c77Sek 	/* find out if the dataset is mounted */
188955434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
189055434c77Sek 
189155434c77Sek 	/* get the corrupted object's path */
189255434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
189355434c77Sek 	zc.zc_obj = obj;
189455434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
189555434c77Sek 	    &zc) == 0) {
189655434c77Sek 		if (mounted) {
189755434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
189855434c77Sek 			    zc.zc_value);
189955434c77Sek 		} else {
190055434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
190155434c77Sek 			    dsname, zc.zc_value);
190255434c77Sek 		}
190355434c77Sek 	} else {
190455434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
190555434c77Sek 	}
190655434c77Sek 	free(mntpnt);
190755434c77Sek }
1908b1b8ab34Slling 
1909*8488aeb5Staylor #define	RDISK_ROOT	"/dev/rdsk"
1910*8488aeb5Staylor #define	BACKUP_SLICE	"s2"
1911*8488aeb5Staylor /*
1912*8488aeb5Staylor  * Don't start the slice at the default block of 34; many storage
1913*8488aeb5Staylor  * devices will use a stripe width of 128k, so start there instead.
1914*8488aeb5Staylor  */
1915*8488aeb5Staylor #define	NEW_START_BLOCK	256
1916*8488aeb5Staylor 
1917*8488aeb5Staylor /*
1918*8488aeb5Staylor  * determine where a partition starts on a disk in the current
1919*8488aeb5Staylor  * configuration
1920*8488aeb5Staylor  */
1921*8488aeb5Staylor static diskaddr_t
1922*8488aeb5Staylor find_start_block(nvlist_t *config)
1923*8488aeb5Staylor {
1924*8488aeb5Staylor 	nvlist_t **child;
1925*8488aeb5Staylor 	uint_t c, children;
1926*8488aeb5Staylor 	char *path;
1927*8488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
1928*8488aeb5Staylor 	int fd;
1929*8488aeb5Staylor 	char diskname[MAXPATHLEN];
1930*8488aeb5Staylor 	uint64_t wholedisk;
1931*8488aeb5Staylor 
1932*8488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
1933*8488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
1934*8488aeb5Staylor 		if (nvlist_lookup_uint64(config,
1935*8488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
1936*8488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
1937*8488aeb5Staylor 			return (MAXOFFSET_T);
1938*8488aeb5Staylor 		}
1939*8488aeb5Staylor 		if (nvlist_lookup_string(config,
1940*8488aeb5Staylor 		    ZPOOL_CONFIG_PATH, &path) != 0) {
1941*8488aeb5Staylor 			return (MAXOFFSET_T);
1942*8488aeb5Staylor 		}
1943*8488aeb5Staylor 
1944*8488aeb5Staylor 		(void) snprintf(diskname, sizeof (diskname), "%s%s",
1945*8488aeb5Staylor 		    RDISK_ROOT, strrchr(path, '/'));
1946*8488aeb5Staylor 		if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
1947*8488aeb5Staylor 			struct dk_gpt *vtoc;
1948*8488aeb5Staylor 			if (efi_alloc_and_read(fd, &vtoc) >= 0) {
1949*8488aeb5Staylor 				sb = vtoc->efi_parts[0].p_start;
1950*8488aeb5Staylor 				efi_free(vtoc);
1951*8488aeb5Staylor 			}
1952*8488aeb5Staylor 			(void) close(fd);
1953*8488aeb5Staylor 		}
1954*8488aeb5Staylor 		return (sb);
1955*8488aeb5Staylor 	}
1956*8488aeb5Staylor 
1957*8488aeb5Staylor 	for (c = 0; c < children; c++) {
1958*8488aeb5Staylor 		sb = find_start_block(child[c]);
1959*8488aeb5Staylor 		if (sb != MAXOFFSET_T) {
1960*8488aeb5Staylor 			return (sb);
1961*8488aeb5Staylor 		}
1962*8488aeb5Staylor 	}
1963*8488aeb5Staylor 	return (MAXOFFSET_T);
1964*8488aeb5Staylor }
1965*8488aeb5Staylor 
1966*8488aeb5Staylor /*
1967*8488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
1968*8488aeb5Staylor  * stripped of any leading /dev path.
1969*8488aeb5Staylor  */
1970*8488aeb5Staylor int
1971*8488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
1972*8488aeb5Staylor {
1973*8488aeb5Staylor 	char path[MAXPATHLEN];
1974*8488aeb5Staylor 	struct dk_gpt *vtoc;
1975*8488aeb5Staylor 	int fd;
1976*8488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
1977*8488aeb5Staylor 	uint64_t slice_size;
1978*8488aeb5Staylor 	diskaddr_t start_block;
1979*8488aeb5Staylor 	char errbuf[1024];
1980*8488aeb5Staylor 
1981*8488aeb5Staylor 	if (zhp) {
1982*8488aeb5Staylor 		nvlist_t *nvroot;
1983*8488aeb5Staylor 
1984*8488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
1985*8488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1986*8488aeb5Staylor 
1987*8488aeb5Staylor 		if (zhp->zpool_start_block == 0)
1988*8488aeb5Staylor 			start_block = find_start_block(nvroot);
1989*8488aeb5Staylor 		else
1990*8488aeb5Staylor 			start_block = zhp->zpool_start_block;
1991*8488aeb5Staylor 		zhp->zpool_start_block = start_block;
1992*8488aeb5Staylor 	} else {
1993*8488aeb5Staylor 		/* new pool */
1994*8488aeb5Staylor 		start_block = NEW_START_BLOCK;
1995*8488aeb5Staylor 	}
1996*8488aeb5Staylor 
1997*8488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
1998*8488aeb5Staylor 	    BACKUP_SLICE);
1999*8488aeb5Staylor 
2000*8488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2001*8488aeb5Staylor 		/*
2002*8488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
2003*8488aeb5Staylor 		 * is a valid device.
2004*8488aeb5Staylor 		 */
2005*8488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2006*8488aeb5Staylor 		    "label '%s': unable to open device"), name);
2007*8488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2008*8488aeb5Staylor 	}
2009*8488aeb5Staylor 
2010*8488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2011*8488aeb5Staylor 		/*
2012*8488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
2013*8488aeb5Staylor 		 * were unable to read the disk's capacity
2014*8488aeb5Staylor 		 */
2015*8488aeb5Staylor 		if (errno == ENOMEM)
2016*8488aeb5Staylor 			(void) no_memory(hdl);
2017*8488aeb5Staylor 
2018*8488aeb5Staylor 		(void) close(fd);
2019*8488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2020*8488aeb5Staylor 		    "label '%s': unable to read disk capacity"), name);
2021*8488aeb5Staylor 
2022*8488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2023*8488aeb5Staylor 	}
2024*8488aeb5Staylor 
2025*8488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
2026*8488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
2027*8488aeb5Staylor 	if (start_block == MAXOFFSET_T)
2028*8488aeb5Staylor 		start_block = NEW_START_BLOCK;
2029*8488aeb5Staylor 	slice_size -= start_block;
2030*8488aeb5Staylor 
2031*8488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
2032*8488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
2033*8488aeb5Staylor 
2034*8488aeb5Staylor 	/*
2035*8488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
2036*8488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
2037*8488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
2038*8488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
2039*8488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
2040*8488aeb5Staylor 	 * can get, in the absence of V_OTHER.
2041*8488aeb5Staylor 	 */
2042*8488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
2043*8488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2044*8488aeb5Staylor 
2045*8488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
2046*8488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
2047*8488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
2048*8488aeb5Staylor 
2049*8488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
2050*8488aeb5Staylor 		/*
2051*8488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
2052*8488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
2053*8488aeb5Staylor 		 * ecting the user to manually label the disk and give
2054*8488aeb5Staylor 		 * a specific slice.
2055*8488aeb5Staylor 		 */
2056*8488aeb5Staylor 		(void) close(fd);
2057*8488aeb5Staylor 		efi_free(vtoc);
2058*8488aeb5Staylor 
2059*8488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2060*8488aeb5Staylor 		    "cannot label '%s': try using fdisk(1M) and then "
2061*8488aeb5Staylor 		    "provide a specific slice"), name);
2062*8488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
2063*8488aeb5Staylor 	}
2064*8488aeb5Staylor 
2065*8488aeb5Staylor 	(void) close(fd);
2066*8488aeb5Staylor 	efi_free(vtoc);
2067*8488aeb5Staylor 	return (0);
2068*8488aeb5Staylor }
2069*8488aeb5Staylor 
2070b1b8ab34Slling int
2071b1b8ab34Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
2072b1b8ab34Slling {
2073b1b8ab34Slling 	zfs_cmd_t zc = { 0 };
2074b1b8ab34Slling 	int ret = -1;
2075b1b8ab34Slling 	char errbuf[1024];
2076b1b8ab34Slling 	nvlist_t *nvl = NULL;
2077b1b8ab34Slling 	nvlist_t *realprops;
2078b1b8ab34Slling 
2079b1b8ab34Slling 	(void) snprintf(errbuf, sizeof (errbuf),
2080b1b8ab34Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
2081b1b8ab34Slling 	    zhp->zpool_name);
2082b1b8ab34Slling 
2083b1b8ab34Slling 	if (zpool_get_version(zhp) < ZFS_VERSION_BOOTFS) {
2084b1b8ab34Slling 		zfs_error_aux(zhp->zpool_hdl,
2085b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "pool must be "
2086b1b8ab34Slling 		    "upgraded to support pool properties"));
2087b1b8ab34Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_BADVERSION, errbuf));
2088b1b8ab34Slling 	}
2089b1b8ab34Slling 
2090b1b8ab34Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
2091b1b8ab34Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
2092b1b8ab34Slling 
2093b1b8ab34Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
2094b1b8ab34Slling 	    nvlist_add_string(nvl, propname, propval) != 0) {
2095b1b8ab34Slling 		return (no_memory(zhp->zpool_hdl));
2096b1b8ab34Slling 	}
2097b1b8ab34Slling 
2098b1b8ab34Slling 	if ((realprops = zfs_validate_properties(zhp->zpool_hdl, ZFS_TYPE_POOL,
2099b1b8ab34Slling 	    zhp->zpool_name, nvl, 0, NULL, errbuf)) == NULL) {
2100b1b8ab34Slling 		nvlist_free(nvl);
2101b1b8ab34Slling 		return (-1);
2102b1b8ab34Slling 	}
2103b1b8ab34Slling 
2104b1b8ab34Slling 	nvlist_free(nvl);
2105b1b8ab34Slling 	nvl = realprops;
2106b1b8ab34Slling 
2107b1b8ab34Slling 	/*
2108b1b8ab34Slling 	 * Execute the corresponding ioctl() to set this property.
2109b1b8ab34Slling 	 */
2110b1b8ab34Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2111b1b8ab34Slling 
2112b1b8ab34Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl, NULL) != 0)
2113b1b8ab34Slling 		return (-1);
2114b1b8ab34Slling 
2115b1b8ab34Slling 	ret = ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_SET_PROPS, &zc);
2116b1b8ab34Slling 	zcmd_free_nvlists(&zc);
2117b1b8ab34Slling 
2118b1b8ab34Slling 	if (ret)
2119b1b8ab34Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
2120b1b8ab34Slling 
2121b1b8ab34Slling 	return (ret);
2122b1b8ab34Slling }
2123b1b8ab34Slling 
2124b1b8ab34Slling int
2125b1b8ab34Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *propbuf,
2126b1b8ab34Slling     size_t proplen, zfs_source_t *srctype)
2127b1b8ab34Slling {
2128b1b8ab34Slling 	uint64_t value;
2129b1b8ab34Slling 	char msg[1024], *strvalue;
2130b1b8ab34Slling 	nvlist_t *nvp;
2131b1b8ab34Slling 	zfs_source_t src = ZFS_SRC_NONE;
2132b1b8ab34Slling 
2133b1b8ab34Slling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2134b1b8ab34Slling 	    "cannot get property '%s'"), zpool_prop_to_name(prop));
2135b1b8ab34Slling 
2136b1b8ab34Slling 	if (zpool_get_version(zhp) < ZFS_VERSION_BOOTFS) {
2137b1b8ab34Slling 		zfs_error_aux(zhp->zpool_hdl,
2138b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "pool must be "
2139b1b8ab34Slling 		    "upgraded to support pool properties"));
2140b1b8ab34Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_BADVERSION, msg));
2141b1b8ab34Slling 	}
2142b1b8ab34Slling 
2143b1b8ab34Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
2144b1b8ab34Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, msg));
2145b1b8ab34Slling 
2146b1b8ab34Slling 	/*
2147b1b8ab34Slling 	 * the "name" property is special cased
2148b1b8ab34Slling 	 */
2149b1b8ab34Slling 	if (!zfs_prop_valid_for_type(prop, ZFS_TYPE_POOL) &&
2150b1b8ab34Slling 	    prop != ZFS_PROP_NAME)
2151b1b8ab34Slling 		return (-1);
2152b1b8ab34Slling 
2153b1b8ab34Slling 	switch (prop) {
2154b1b8ab34Slling 	case ZFS_PROP_NAME:
2155b1b8ab34Slling 		(void) strlcpy(propbuf, zhp->zpool_name, proplen);
2156b1b8ab34Slling 		break;
2157b1b8ab34Slling 
2158b1b8ab34Slling 	case ZFS_PROP_BOOTFS:
2159b1b8ab34Slling 		if (nvlist_lookup_nvlist(zhp->zpool_props,
2160b1b8ab34Slling 		    zpool_prop_to_name(prop), &nvp) != 0) {
2161b1b8ab34Slling 			strvalue = (char *)zfs_prop_default_string(prop);
2162b1b8ab34Slling 			if (strvalue == NULL)
2163b1b8ab34Slling 				strvalue = "-";
2164b1b8ab34Slling 			src = ZFS_SRC_DEFAULT;
2165b1b8ab34Slling 		} else {
2166b1b8ab34Slling 			VERIFY(nvlist_lookup_uint64(nvp,
2167b1b8ab34Slling 			    ZFS_PROP_SOURCE, &value) == 0);
2168b1b8ab34Slling 			src = value;
2169b1b8ab34Slling 			VERIFY(nvlist_lookup_string(nvp, ZFS_PROP_VALUE,
2170b1b8ab34Slling 			    &strvalue) == 0);
2171b1b8ab34Slling 			if (strlen(strvalue) >= proplen)
2172b1b8ab34Slling 				return (-1);
2173b1b8ab34Slling 		}
2174b1b8ab34Slling 		(void) strcpy(propbuf, strvalue);
2175b1b8ab34Slling 		break;
2176b1b8ab34Slling 
2177b1b8ab34Slling 	default:
2178b1b8ab34Slling 		return (-1);
2179b1b8ab34Slling 	}
2180b1b8ab34Slling 	if (srctype)
2181b1b8ab34Slling 		*srctype = src;
2182b1b8ab34Slling 	return (0);
2183b1b8ab34Slling }
2184b1b8ab34Slling 
2185b1b8ab34Slling int
2186b1b8ab34Slling zpool_get_proplist(libzfs_handle_t *hdl, char *fields, zpool_proplist_t **listp)
2187b1b8ab34Slling {
2188b1b8ab34Slling 	return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_POOL));
2189b1b8ab34Slling }
2190b1b8ab34Slling 
2191b1b8ab34Slling 
2192b1b8ab34Slling int
2193b1b8ab34Slling zpool_expand_proplist(zpool_handle_t *zhp, zpool_proplist_t **plp)
2194b1b8ab34Slling {
2195b1b8ab34Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2196b1b8ab34Slling 	zpool_proplist_t *entry;
2197b1b8ab34Slling 	char buf[ZFS_MAXPROPLEN];
2198b1b8ab34Slling 
2199b1b8ab34Slling 	if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_POOL) != 0)
2200b1b8ab34Slling 		return (-1);
2201b1b8ab34Slling 
2202b1b8ab34Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
2203b1b8ab34Slling 
2204b1b8ab34Slling 		if (entry->pl_fixed)
2205b1b8ab34Slling 			continue;
2206b1b8ab34Slling 
2207b1b8ab34Slling 		if (entry->pl_prop != ZFS_PROP_INVAL &&
2208b1b8ab34Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
2209b1b8ab34Slling 		    NULL) == 0) {
2210b1b8ab34Slling 			if (strlen(buf) > entry->pl_width)
2211b1b8ab34Slling 				entry->pl_width = strlen(buf);
2212b1b8ab34Slling 		}
2213b1b8ab34Slling 	}
2214b1b8ab34Slling 
2215b1b8ab34Slling 	return (0);
2216b1b8ab34Slling }
2217