xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision fe3e2633be44d2f5361a7bba26abeb80fcc04fbc)
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 /*
23c6ef114fSmmusante  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27f3861e1aSahl #include <alloca.h>
28fa9e4066Sahrens #include <assert.h>
29fa9e4066Sahrens #include <ctype.h>
30fa9e4066Sahrens #include <errno.h>
31fa9e4066Sahrens #include <devid.h>
32f3861e1aSahl #include <dirent.h>
33fa9e4066Sahrens #include <fcntl.h>
34fa9e4066Sahrens #include <libintl.h>
35fa9e4066Sahrens #include <stdio.h>
36fa9e4066Sahrens #include <stdlib.h>
37f3861e1aSahl #include <strings.h>
38fa9e4066Sahrens #include <unistd.h>
390a48a24eStimh #include <zone.h>
408488aeb5Staylor #include <sys/efi_partition.h>
418488aeb5Staylor #include <sys/vtoc.h>
42fa9e4066Sahrens #include <sys/zfs_ioctl.h>
43ea8dc4b6Seschrock #include <sys/zio.h>
4406eeb2adSek #include <strings.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens #include "zfs_namecheck.h"
47b1b8ab34Slling #include "zfs_prop.h"
48fa9e4066Sahrens #include "libzfs_impl.h"
49fa9e4066Sahrens 
5015e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51990b4856Slling 
52990b4856Slling /*
53990b4856Slling  * ====================================================================
54990b4856Slling  *   zpool property functions
55990b4856Slling  * ====================================================================
56990b4856Slling  */
57990b4856Slling 
58990b4856Slling static int
59990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
60990b4856Slling {
61990b4856Slling 	zfs_cmd_t zc = { 0 };
62990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
63990b4856Slling 
64990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
65990b4856Slling 
66990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
67990b4856Slling 		return (-1);
68990b4856Slling 
69990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
70990b4856Slling 		if (errno == ENOMEM) {
71990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
72990b4856Slling 				zcmd_free_nvlists(&zc);
73990b4856Slling 				return (-1);
74990b4856Slling 			}
75990b4856Slling 		} else {
76990b4856Slling 			zcmd_free_nvlists(&zc);
77990b4856Slling 			return (-1);
78990b4856Slling 		}
79990b4856Slling 	}
80990b4856Slling 
81990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
82990b4856Slling 		zcmd_free_nvlists(&zc);
83990b4856Slling 		return (-1);
84990b4856Slling 	}
85990b4856Slling 
86990b4856Slling 	zcmd_free_nvlists(&zc);
87990b4856Slling 
88990b4856Slling 	return (0);
89990b4856Slling }
90990b4856Slling 
91990b4856Slling static int
92990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
93990b4856Slling {
94990b4856Slling 	nvlist_t *old_props;
95990b4856Slling 
96990b4856Slling 	old_props = zhp->zpool_props;
97990b4856Slling 
98990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
99990b4856Slling 		return (-1);
100990b4856Slling 
101990b4856Slling 	nvlist_free(old_props);
102990b4856Slling 	return (0);
103990b4856Slling }
104990b4856Slling 
105990b4856Slling static char *
106990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
107990b4856Slling     zprop_source_t *src)
108990b4856Slling {
109990b4856Slling 	nvlist_t *nv, *nvl;
110990b4856Slling 	uint64_t ival;
111990b4856Slling 	char *value;
112990b4856Slling 	zprop_source_t source;
113990b4856Slling 
114990b4856Slling 	nvl = zhp->zpool_props;
115990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
116990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
117990b4856Slling 		source = ival;
118990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
119990b4856Slling 	} else {
120990b4856Slling 		source = ZPROP_SRC_DEFAULT;
121990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
122990b4856Slling 			value = "-";
123990b4856Slling 	}
124990b4856Slling 
125990b4856Slling 	if (src)
126990b4856Slling 		*src = source;
127990b4856Slling 
128990b4856Slling 	return (value);
129990b4856Slling }
130990b4856Slling 
131990b4856Slling uint64_t
132990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
133990b4856Slling {
134990b4856Slling 	nvlist_t *nv, *nvl;
135990b4856Slling 	uint64_t value;
136990b4856Slling 	zprop_source_t source;
137990b4856Slling 
138b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
139b87f3af3Sperrin 		/*
140b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
141b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
142b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
143b87f3af3Sperrin 		 */
144b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
145b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
146b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
147b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
148b87f3af3Sperrin 		    == 0)) {
149b87f3af3Sperrin 			return (value);
150b87f3af3Sperrin 		}
151990b4856Slling 		return (zpool_prop_default_numeric(prop));
152b87f3af3Sperrin 	}
153990b4856Slling 
154990b4856Slling 	nvl = zhp->zpool_props;
155990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
156990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
157990b4856Slling 		source = value;
158990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
159990b4856Slling 	} else {
160990b4856Slling 		source = ZPROP_SRC_DEFAULT;
161990b4856Slling 		value = zpool_prop_default_numeric(prop);
162990b4856Slling 	}
163990b4856Slling 
164990b4856Slling 	if (src)
165990b4856Slling 		*src = source;
166990b4856Slling 
167990b4856Slling 	return (value);
168990b4856Slling }
169990b4856Slling 
170990b4856Slling /*
171990b4856Slling  * Map VDEV STATE to printed strings.
172990b4856Slling  */
173990b4856Slling char *
174990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
175990b4856Slling {
176990b4856Slling 	switch (state) {
177990b4856Slling 	case VDEV_STATE_CLOSED:
178990b4856Slling 	case VDEV_STATE_OFFLINE:
179990b4856Slling 		return (gettext("OFFLINE"));
180990b4856Slling 	case VDEV_STATE_REMOVED:
181990b4856Slling 		return (gettext("REMOVED"));
182990b4856Slling 	case VDEV_STATE_CANT_OPEN:
183b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
184990b4856Slling 			return (gettext("FAULTED"));
185990b4856Slling 		else
186990b4856Slling 			return (gettext("UNAVAIL"));
187990b4856Slling 	case VDEV_STATE_FAULTED:
188990b4856Slling 		return (gettext("FAULTED"));
189990b4856Slling 	case VDEV_STATE_DEGRADED:
190990b4856Slling 		return (gettext("DEGRADED"));
191990b4856Slling 	case VDEV_STATE_HEALTHY:
192990b4856Slling 		return (gettext("ONLINE"));
193990b4856Slling 	}
194990b4856Slling 
195990b4856Slling 	return (gettext("UNKNOWN"));
196990b4856Slling }
197990b4856Slling 
198990b4856Slling /*
199990b4856Slling  * Get a zpool property value for 'prop' and return the value in
200990b4856Slling  * a pre-allocated buffer.
201990b4856Slling  */
202990b4856Slling int
203990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
204990b4856Slling     zprop_source_t *srctype)
205990b4856Slling {
206990b4856Slling 	uint64_t intval;
207990b4856Slling 	const char *strval;
208990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
209990b4856Slling 	nvlist_t *nvroot;
210990b4856Slling 	vdev_stat_t *vs;
211990b4856Slling 	uint_t vsc;
212990b4856Slling 
213990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
214990b4856Slling 		if (prop == ZPOOL_PROP_NAME)
215990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
216990b4856Slling 		else if (prop == ZPOOL_PROP_HEALTH)
217990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
218990b4856Slling 		else
219990b4856Slling 			(void) strlcpy(buf, "-", len);
220990b4856Slling 		return (0);
221990b4856Slling 	}
222990b4856Slling 
223990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
224990b4856Slling 	    prop != ZPOOL_PROP_NAME)
225990b4856Slling 		return (-1);
226990b4856Slling 
227990b4856Slling 	switch (zpool_prop_get_type(prop)) {
228990b4856Slling 	case PROP_TYPE_STRING:
229990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
230990b4856Slling 		    len);
231990b4856Slling 		break;
232990b4856Slling 
233990b4856Slling 	case PROP_TYPE_NUMBER:
234990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
235990b4856Slling 
236990b4856Slling 		switch (prop) {
237990b4856Slling 		case ZPOOL_PROP_SIZE:
238990b4856Slling 		case ZPOOL_PROP_USED:
239990b4856Slling 		case ZPOOL_PROP_AVAILABLE:
240990b4856Slling 			(void) zfs_nicenum(intval, buf, len);
241990b4856Slling 			break;
242990b4856Slling 
243990b4856Slling 		case ZPOOL_PROP_CAPACITY:
244990b4856Slling 			(void) snprintf(buf, len, "%llu%%",
245990b4856Slling 			    (u_longlong_t)intval);
246990b4856Slling 			break;
247990b4856Slling 
248990b4856Slling 		case ZPOOL_PROP_HEALTH:
249990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
250990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
251990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
252990b4856Slling 			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
253990b4856Slling 
254990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
255990b4856Slling 			    vs->vs_aux), len);
256990b4856Slling 			break;
257990b4856Slling 		default:
258990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
259990b4856Slling 		}
260990b4856Slling 		break;
261990b4856Slling 
262990b4856Slling 	case PROP_TYPE_INDEX:
263990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
264990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
265990b4856Slling 		    != 0)
266990b4856Slling 			return (-1);
267990b4856Slling 		(void) strlcpy(buf, strval, len);
268990b4856Slling 		break;
269990b4856Slling 
270990b4856Slling 	default:
271990b4856Slling 		abort();
272990b4856Slling 	}
273990b4856Slling 
274990b4856Slling 	if (srctype)
275990b4856Slling 		*srctype = src;
276990b4856Slling 
277990b4856Slling 	return (0);
278990b4856Slling }
279990b4856Slling 
280990b4856Slling /*
281990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
282990b4856Slling  * Assuming bootfs is a valid dataset name.
283990b4856Slling  */
284990b4856Slling static boolean_t
285990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
286990b4856Slling {
287990b4856Slling 	int len = strlen(pool);
288990b4856Slling 
289*fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
290990b4856Slling 		return (B_FALSE);
291990b4856Slling 
292990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
293990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
294990b4856Slling 		return (B_TRUE);
295990b4856Slling 
296990b4856Slling 	return (B_FALSE);
297990b4856Slling }
298990b4856Slling 
29915e6edf1Sgw /*
30015e6edf1Sgw  * Inspect the configuration to determine if any of the devices contain
30115e6edf1Sgw  * an EFI label.
30215e6edf1Sgw  */
30315e6edf1Sgw static boolean_t
30415e6edf1Sgw pool_uses_efi(nvlist_t *config)
30515e6edf1Sgw {
30615e6edf1Sgw 	nvlist_t **child;
30715e6edf1Sgw 	uint_t c, children;
30815e6edf1Sgw 
30915e6edf1Sgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
31015e6edf1Sgw 	    &child, &children) != 0)
31115e6edf1Sgw 		return (read_efi_label(config, NULL) >= 0);
31215e6edf1Sgw 
31315e6edf1Sgw 	for (c = 0; c < children; c++) {
31415e6edf1Sgw 		if (pool_uses_efi(child[c]))
31515e6edf1Sgw 			return (B_TRUE);
31615e6edf1Sgw 	}
31715e6edf1Sgw 	return (B_FALSE);
31815e6edf1Sgw }
31915e6edf1Sgw 
320990b4856Slling /*
321990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
322990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
323990b4856Slling  * specified as strings.
324990b4856Slling  */
325990b4856Slling static nvlist_t *
3260a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
327990b4856Slling     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
328990b4856Slling {
329990b4856Slling 	nvpair_t *elem;
330990b4856Slling 	nvlist_t *retprops;
331990b4856Slling 	zpool_prop_t prop;
332990b4856Slling 	char *strval;
333990b4856Slling 	uint64_t intval;
3342f8aaab3Seschrock 	char *slash;
3352f8aaab3Seschrock 	struct stat64 statbuf;
33615e6edf1Sgw 	zpool_handle_t *zhp;
33715e6edf1Sgw 	nvlist_t *nvroot;
338990b4856Slling 
339990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
340990b4856Slling 		(void) no_memory(hdl);
341990b4856Slling 		return (NULL);
342990b4856Slling 	}
343990b4856Slling 
344990b4856Slling 	elem = NULL;
345990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
346990b4856Slling 		const char *propname = nvpair_name(elem);
347990b4856Slling 
348990b4856Slling 		/*
349990b4856Slling 		 * Make sure this property is valid and applies to this type.
350990b4856Slling 		 */
351990b4856Slling 		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
352990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
353990b4856Slling 			    "invalid property '%s'"), propname);
354990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
355990b4856Slling 			goto error;
356990b4856Slling 		}
357990b4856Slling 
358990b4856Slling 		if (zpool_prop_readonly(prop)) {
359990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
360990b4856Slling 			    "is readonly"), propname);
361990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
362990b4856Slling 			goto error;
363990b4856Slling 		}
364990b4856Slling 
365990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
366990b4856Slling 		    &strval, &intval, errbuf) != 0)
367990b4856Slling 			goto error;
368990b4856Slling 
369990b4856Slling 		/*
370990b4856Slling 		 * Perform additional checking for specific properties.
371990b4856Slling 		 */
372990b4856Slling 		switch (prop) {
373990b4856Slling 		case ZPOOL_PROP_VERSION:
374990b4856Slling 			if (intval < version || intval > SPA_VERSION) {
375990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
376990b4856Slling 				    "property '%s' number %d is invalid."),
377990b4856Slling 				    propname, intval);
378990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
379990b4856Slling 				goto error;
380990b4856Slling 			}
381990b4856Slling 			break;
382990b4856Slling 
383990b4856Slling 		case ZPOOL_PROP_BOOTFS:
384990b4856Slling 			if (create_or_import) {
385990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
386990b4856Slling 				    "property '%s' cannot be set at creation "
387990b4856Slling 				    "or import time"), propname);
388990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
389990b4856Slling 				goto error;
390990b4856Slling 			}
391990b4856Slling 
392990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
393990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
394990b4856Slling 				    "pool must be upgraded to support "
395990b4856Slling 				    "'%s' property"), propname);
396990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
397990b4856Slling 				goto error;
398990b4856Slling 			}
399990b4856Slling 
400990b4856Slling 			/*
401990b4856Slling 			 * bootfs property value has to be a dataset name and
402990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
403990b4856Slling 			 */
404990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
405990b4856Slling 			    strval)) {
406990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
407990b4856Slling 				    "is an invalid name"), strval);
408990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
409990b4856Slling 				goto error;
410990b4856Slling 			}
41115e6edf1Sgw 
41215e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
41315e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41415e6edf1Sgw 				    "could not open pool '%s'"), poolname);
41515e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
41615e6edf1Sgw 				goto error;
41715e6edf1Sgw 			}
41815e6edf1Sgw 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
41915e6edf1Sgw 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
42015e6edf1Sgw 
42115e6edf1Sgw 			/*
42215e6edf1Sgw 			 * bootfs property cannot be set on a disk which has
42315e6edf1Sgw 			 * been EFI labeled.
42415e6edf1Sgw 			 */
42515e6edf1Sgw 			if (pool_uses_efi(nvroot)) {
42615e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42715e6edf1Sgw 				    "property '%s' not supported on "
42815e6edf1Sgw 				    "EFI labeled devices"), propname);
42915e6edf1Sgw 				(void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
43015e6edf1Sgw 				zpool_close(zhp);
43115e6edf1Sgw 				goto error;
43215e6edf1Sgw 			}
43315e6edf1Sgw 			zpool_close(zhp);
434990b4856Slling 			break;
435990b4856Slling 
4362f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
437990b4856Slling 			if (!create_or_import) {
438990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439990b4856Slling 				    "property '%s' can only be set during pool "
440990b4856Slling 				    "creation or import"), propname);
441990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
442990b4856Slling 				goto error;
443990b4856Slling 			}
444990b4856Slling 
4452f8aaab3Seschrock 			if (strval[0] != '/') {
446990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4472f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
4482f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
449990b4856Slling 				goto error;
450990b4856Slling 			}
4512f8aaab3Seschrock 			break;
4522f8aaab3Seschrock 
4532f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
4542f8aaab3Seschrock 			if (strval[0] == '\0')
4552f8aaab3Seschrock 				break;
4562f8aaab3Seschrock 
4572f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
4582f8aaab3Seschrock 				break;
459990b4856Slling 
460990b4856Slling 			if (strval[0] != '/') {
461990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4622f8aaab3Seschrock 				    "property '%s' must be empty, an "
4632f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
464990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
465990b4856Slling 				goto error;
466990b4856Slling 			}
467990b4856Slling 
4682f8aaab3Seschrock 			slash = strrchr(strval, '/');
469990b4856Slling 
4702f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
4712f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
4722f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4732f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
4742f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4752f8aaab3Seschrock 				goto error;
4762f8aaab3Seschrock 			}
477990b4856Slling 
4782f8aaab3Seschrock 			*slash = '\0';
4792f8aaab3Seschrock 
4802c32020fSeschrock 			if (strval[0] != '\0' &&
4812c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
4822c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
4832f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4842f8aaab3Seschrock 				    "'%s' is not a valid directory"),
4852f8aaab3Seschrock 				    strval);
4862f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
4872f8aaab3Seschrock 				goto error;
4882f8aaab3Seschrock 			}
4892f8aaab3Seschrock 
4902f8aaab3Seschrock 			*slash = '/';
4912f8aaab3Seschrock 			break;
492990b4856Slling 		}
493990b4856Slling 	}
494990b4856Slling 
495990b4856Slling 	return (retprops);
496990b4856Slling error:
497990b4856Slling 	nvlist_free(retprops);
498990b4856Slling 	return (NULL);
499990b4856Slling }
500990b4856Slling 
501990b4856Slling /*
502990b4856Slling  * Set zpool property : propname=propval.
503990b4856Slling  */
504990b4856Slling int
505990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
506990b4856Slling {
507990b4856Slling 	zfs_cmd_t zc = { 0 };
508990b4856Slling 	int ret = -1;
509990b4856Slling 	char errbuf[1024];
510990b4856Slling 	nvlist_t *nvl = NULL;
511990b4856Slling 	nvlist_t *realprops;
512990b4856Slling 	uint64_t version;
513990b4856Slling 
514990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
515990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
516990b4856Slling 	    zhp->zpool_name);
517990b4856Slling 
518990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
519990b4856Slling 		return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
520990b4856Slling 
521990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
522990b4856Slling 		return (no_memory(zhp->zpool_hdl));
523990b4856Slling 
524990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
525990b4856Slling 		nvlist_free(nvl);
526990b4856Slling 		return (no_memory(zhp->zpool_hdl));
527990b4856Slling 	}
528990b4856Slling 
529990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5300a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
531990b4856Slling 	    zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
532990b4856Slling 		nvlist_free(nvl);
533990b4856Slling 		return (-1);
534990b4856Slling 	}
535990b4856Slling 
536990b4856Slling 	nvlist_free(nvl);
537990b4856Slling 	nvl = realprops;
538990b4856Slling 
539990b4856Slling 	/*
540990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
541990b4856Slling 	 */
542990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
543990b4856Slling 
544990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
545990b4856Slling 		nvlist_free(nvl);
546990b4856Slling 		return (-1);
547990b4856Slling 	}
548990b4856Slling 
549990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
550990b4856Slling 
551990b4856Slling 	zcmd_free_nvlists(&zc);
552990b4856Slling 	nvlist_free(nvl);
553990b4856Slling 
554990b4856Slling 	if (ret)
555990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
556990b4856Slling 	else
557990b4856Slling 		(void) zpool_props_refresh(zhp);
558990b4856Slling 
559990b4856Slling 	return (ret);
560990b4856Slling }
561990b4856Slling 
562990b4856Slling int
563990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
564990b4856Slling {
565990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
566990b4856Slling 	zprop_list_t *entry;
567990b4856Slling 	char buf[ZFS_MAXPROPLEN];
568990b4856Slling 
569990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
570990b4856Slling 		return (-1);
571990b4856Slling 
572990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
573990b4856Slling 
574990b4856Slling 		if (entry->pl_fixed)
575990b4856Slling 			continue;
576990b4856Slling 
577990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
578990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
579990b4856Slling 		    NULL) == 0) {
580990b4856Slling 			if (strlen(buf) > entry->pl_width)
581990b4856Slling 				entry->pl_width = strlen(buf);
582990b4856Slling 		}
583990b4856Slling 	}
584990b4856Slling 
585990b4856Slling 	return (0);
586990b4856Slling }
587990b4856Slling 
588990b4856Slling 
589fa9e4066Sahrens /*
590fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
591fa9e4066Sahrens  * 'buf'.
592fa9e4066Sahrens  */
593e7cbe64fSgw boolean_t
59499653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
595fa9e4066Sahrens {
596fa9e4066Sahrens 	namecheck_err_t why;
597fa9e4066Sahrens 	char what;
598b468a217Seschrock 	int ret;
599b468a217Seschrock 
600b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
601b468a217Seschrock 
602b468a217Seschrock 	/*
603b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
604b468a217Seschrock 	 * But we need to support users with existing pools that may now be
605b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
606b468a217Seschrock 	 * create (or import), and only in userland.
607b468a217Seschrock 	 */
608b468a217Seschrock 	if (ret == 0 && !isopen &&
609b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
610b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
6118654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
6128654d025Sperrin 	    strcmp(pool, "log") == 0)) {
613e7cbe64fSgw 		if (hdl != NULL)
614e7cbe64fSgw 			zfs_error_aux(hdl,
615e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
61699653d4eSeschrock 		return (B_FALSE);
617b468a217Seschrock 	}
618b468a217Seschrock 
619fa9e4066Sahrens 
620b468a217Seschrock 	if (ret != 0) {
62199653d4eSeschrock 		if (hdl != NULL) {
622fa9e4066Sahrens 			switch (why) {
623b81d61a6Slling 			case NAME_ERR_TOOLONG:
62499653d4eSeschrock 				zfs_error_aux(hdl,
625b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
626b81d61a6Slling 				break;
627b81d61a6Slling 
628fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
62999653d4eSeschrock 				zfs_error_aux(hdl,
630fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
631fa9e4066Sahrens 				    "'%c' in pool name"), what);
632fa9e4066Sahrens 				break;
633fa9e4066Sahrens 
634fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
63599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
63699653d4eSeschrock 				    "name must begin with a letter"));
637fa9e4066Sahrens 				break;
638fa9e4066Sahrens 
639fa9e4066Sahrens 			case NAME_ERR_RESERVED:
64099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
64199653d4eSeschrock 				    "name is reserved"));
642fa9e4066Sahrens 				break;
643fa9e4066Sahrens 
644fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
64599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
64699653d4eSeschrock 				    "pool name is reserved"));
647fa9e4066Sahrens 				break;
6485ad82045Snd 
6495ad82045Snd 			case NAME_ERR_LEADING_SLASH:
6505ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6515ad82045Snd 				    "leading slash in name"));
6525ad82045Snd 				break;
6535ad82045Snd 
6545ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
6555ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6565ad82045Snd 				    "empty component in name"));
6575ad82045Snd 				break;
6585ad82045Snd 
6595ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
6605ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6615ad82045Snd 				    "trailing slash in name"));
6625ad82045Snd 				break;
6635ad82045Snd 
6645ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
6655ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6665ad82045Snd 				    "multiple '@' delimiters in name"));
6675ad82045Snd 				break;
6685ad82045Snd 
669fa9e4066Sahrens 			}
670fa9e4066Sahrens 		}
67199653d4eSeschrock 		return (B_FALSE);
672fa9e4066Sahrens 	}
673fa9e4066Sahrens 
67499653d4eSeschrock 	return (B_TRUE);
675fa9e4066Sahrens }
676fa9e4066Sahrens 
677fa9e4066Sahrens /*
678fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
679fa9e4066Sahrens  * state.
680fa9e4066Sahrens  */
681fa9e4066Sahrens zpool_handle_t *
68299653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
683fa9e4066Sahrens {
684fa9e4066Sahrens 	zpool_handle_t *zhp;
68594de1d4cSeschrock 	boolean_t missing;
686fa9e4066Sahrens 
687fa9e4066Sahrens 	/*
688fa9e4066Sahrens 	 * Make sure the pool name is valid.
689fa9e4066Sahrens 	 */
69099653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
691ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
69299653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
69399653d4eSeschrock 		    pool);
694fa9e4066Sahrens 		return (NULL);
695fa9e4066Sahrens 	}
696fa9e4066Sahrens 
69799653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
69899653d4eSeschrock 		return (NULL);
699fa9e4066Sahrens 
70099653d4eSeschrock 	zhp->zpool_hdl = hdl;
701fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
702fa9e4066Sahrens 
70394de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
70494de1d4cSeschrock 		zpool_close(zhp);
70594de1d4cSeschrock 		return (NULL);
70694de1d4cSeschrock 	}
70794de1d4cSeschrock 
70894de1d4cSeschrock 	if (missing) {
709990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
710ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
711990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
71294de1d4cSeschrock 		zpool_close(zhp);
71394de1d4cSeschrock 		return (NULL);
714fa9e4066Sahrens 	}
715fa9e4066Sahrens 
716fa9e4066Sahrens 	return (zhp);
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719fa9e4066Sahrens /*
720fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
721fa9e4066Sahrens  * the configuration cache may be out of date).
722fa9e4066Sahrens  */
72394de1d4cSeschrock int
72494de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
725fa9e4066Sahrens {
726fa9e4066Sahrens 	zpool_handle_t *zhp;
72794de1d4cSeschrock 	boolean_t missing;
728fa9e4066Sahrens 
72994de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
73094de1d4cSeschrock 		return (-1);
731fa9e4066Sahrens 
73299653d4eSeschrock 	zhp->zpool_hdl = hdl;
733fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
734fa9e4066Sahrens 
73594de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
73694de1d4cSeschrock 		zpool_close(zhp);
73794de1d4cSeschrock 		return (-1);
738fa9e4066Sahrens 	}
739fa9e4066Sahrens 
74094de1d4cSeschrock 	if (missing) {
74194de1d4cSeschrock 		zpool_close(zhp);
74294de1d4cSeschrock 		*ret = NULL;
74394de1d4cSeschrock 		return (0);
74494de1d4cSeschrock 	}
74594de1d4cSeschrock 
74694de1d4cSeschrock 	*ret = zhp;
74794de1d4cSeschrock 	return (0);
748fa9e4066Sahrens }
749fa9e4066Sahrens 
750fa9e4066Sahrens /*
751fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
752fa9e4066Sahrens  * state.
753fa9e4066Sahrens  */
754fa9e4066Sahrens zpool_handle_t *
75599653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
756fa9e4066Sahrens {
757fa9e4066Sahrens 	zpool_handle_t *zhp;
758fa9e4066Sahrens 
75999653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
760fa9e4066Sahrens 		return (NULL);
761fa9e4066Sahrens 
762fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
763ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
76499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
765fa9e4066Sahrens 		zpool_close(zhp);
766fa9e4066Sahrens 		return (NULL);
767fa9e4066Sahrens 	}
768fa9e4066Sahrens 
769fa9e4066Sahrens 	return (zhp);
770fa9e4066Sahrens }
771fa9e4066Sahrens 
772fa9e4066Sahrens /*
773fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
774fa9e4066Sahrens  */
775fa9e4066Sahrens void
776fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
777fa9e4066Sahrens {
778fa9e4066Sahrens 	if (zhp->zpool_config)
779fa9e4066Sahrens 		nvlist_free(zhp->zpool_config);
780088e9d47Seschrock 	if (zhp->zpool_old_config)
781088e9d47Seschrock 		nvlist_free(zhp->zpool_old_config);
782b1b8ab34Slling 	if (zhp->zpool_props)
783b1b8ab34Slling 		nvlist_free(zhp->zpool_props);
784fa9e4066Sahrens 	free(zhp);
785fa9e4066Sahrens }
786fa9e4066Sahrens 
787fa9e4066Sahrens /*
788fa9e4066Sahrens  * Return the name of the pool.
789fa9e4066Sahrens  */
790fa9e4066Sahrens const char *
791fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
792fa9e4066Sahrens {
793fa9e4066Sahrens 	return (zhp->zpool_name);
794fa9e4066Sahrens }
795fa9e4066Sahrens 
796fa9e4066Sahrens 
797fa9e4066Sahrens /*
798fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
799fa9e4066Sahrens  */
800fa9e4066Sahrens int
801fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
802fa9e4066Sahrens {
803fa9e4066Sahrens 	return (zhp->zpool_state);
804fa9e4066Sahrens }
805fa9e4066Sahrens 
806fa9e4066Sahrens /*
807fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
808fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
809fa9e4066Sahrens  * don't have to worry about error semantics.
810fa9e4066Sahrens  */
811fa9e4066Sahrens int
81299653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
8130a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
814fa9e4066Sahrens {
815fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
8160a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
8170a48a24eStimh 	nvlist_t *zc_props = NULL;
81899653d4eSeschrock 	char msg[1024];
819990b4856Slling 	char *altroot;
8200a48a24eStimh 	int ret = -1;
821fa9e4066Sahrens 
82299653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
82399653d4eSeschrock 	    "cannot create '%s'"), pool);
824fa9e4066Sahrens 
82599653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
82699653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
827fa9e4066Sahrens 
828351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
829990b4856Slling 		return (-1);
830fa9e4066Sahrens 
8310a48a24eStimh 	if (props) {
8320a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
8330a48a24eStimh 		    SPA_VERSION_1, B_TRUE, msg)) == NULL) {
8340a48a24eStimh 			goto create_failed;
8350a48a24eStimh 		}
8360a48a24eStimh 	}
83799653d4eSeschrock 
8380a48a24eStimh 	if (fsprops) {
8390a48a24eStimh 		uint64_t zoned;
8400a48a24eStimh 		char *zonestr;
8410a48a24eStimh 
8420a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
8430a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
8440a48a24eStimh 		    strcmp(zonestr, "on") == 0);
8450a48a24eStimh 
8460a48a24eStimh 		if ((zc_fsprops = zfs_valid_proplist(hdl,
8470a48a24eStimh 		    ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
8480a48a24eStimh 			goto create_failed;
8490a48a24eStimh 		}
8500a48a24eStimh 		if (!zc_props &&
8510a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
8520a48a24eStimh 			goto create_failed;
8530a48a24eStimh 		}
8540a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
8550a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
8560a48a24eStimh 			goto create_failed;
8570a48a24eStimh 		}
858351420b3Slling 	}
859fa9e4066Sahrens 
8600a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
8610a48a24eStimh 		goto create_failed;
8620a48a24eStimh 
863990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
864fa9e4066Sahrens 
8650a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
866351420b3Slling 
867e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
8680a48a24eStimh 		nvlist_free(zc_props);
8690a48a24eStimh 		nvlist_free(zc_fsprops);
870fa9e4066Sahrens 
87199653d4eSeschrock 		switch (errno) {
872fa9e4066Sahrens 		case EBUSY:
873fa9e4066Sahrens 			/*
874fa9e4066Sahrens 			 * This can happen if the user has specified the same
875fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
876fa9e4066Sahrens 			 * until we try to add it and see we already have a
877fa9e4066Sahrens 			 * label.
878fa9e4066Sahrens 			 */
87999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
88099653d4eSeschrock 			    "one or more vdevs refer to the same device"));
88199653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
882fa9e4066Sahrens 
883fa9e4066Sahrens 		case EOVERFLOW:
884fa9e4066Sahrens 			/*
88599653d4eSeschrock 			 * This occurs when one of the devices is below
886fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
887fa9e4066Sahrens 			 * device was the problem device since there's no
888fa9e4066Sahrens 			 * reliable way to determine device size from userland.
889fa9e4066Sahrens 			 */
890fa9e4066Sahrens 			{
891fa9e4066Sahrens 				char buf[64];
892fa9e4066Sahrens 
893fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
894fa9e4066Sahrens 
89599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
89699653d4eSeschrock 				    "one or more devices is less than the "
89799653d4eSeschrock 				    "minimum size (%s)"), buf);
898fa9e4066Sahrens 			}
89999653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
900fa9e4066Sahrens 
901fa9e4066Sahrens 		case ENOSPC:
90299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
90399653d4eSeschrock 			    "one or more devices is out of space"));
90499653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
905fa9e4066Sahrens 
906fa94a07fSbrendan 		case ENOTBLK:
907fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
908fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
909fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
910fa94a07fSbrendan 
911fa9e4066Sahrens 		default:
91299653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
913fa9e4066Sahrens 		}
914fa9e4066Sahrens 	}
915fa9e4066Sahrens 
916fa9e4066Sahrens 	/*
917fa9e4066Sahrens 	 * If this is an alternate root pool, then we automatically set the
918e9dbad6fSeschrock 	 * mountpoint of the root dataset to be '/'.
919fa9e4066Sahrens 	 */
920990b4856Slling 	if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
921990b4856Slling 	    &altroot) == 0) {
922fa9e4066Sahrens 		zfs_handle_t *zhp;
923fa9e4066Sahrens 
924990b4856Slling 		verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
925e9dbad6fSeschrock 		verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
926e9dbad6fSeschrock 		    "/") == 0);
927fa9e4066Sahrens 
928fa9e4066Sahrens 		zfs_close(zhp);
929fa9e4066Sahrens 	}
930fa9e4066Sahrens 
9310a48a24eStimh create_failed:
932351420b3Slling 	zcmd_free_nvlists(&zc);
9330a48a24eStimh 	nvlist_free(zc_props);
9340a48a24eStimh 	nvlist_free(zc_fsprops);
9350a48a24eStimh 	return (ret);
936fa9e4066Sahrens }
937fa9e4066Sahrens 
938fa9e4066Sahrens /*
939fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
940fa9e4066Sahrens  * datasets left in the pool.
941fa9e4066Sahrens  */
942fa9e4066Sahrens int
943fa9e4066Sahrens zpool_destroy(zpool_handle_t *zhp)
944fa9e4066Sahrens {
945fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
946fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
94799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
94899653d4eSeschrock 	char msg[1024];
949fa9e4066Sahrens 
950fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
95199653d4eSeschrock 	    (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
95299653d4eSeschrock 	    ZFS_TYPE_FILESYSTEM)) == NULL)
953fa9e4066Sahrens 		return (-1);
954fa9e4066Sahrens 
9555ad82045Snd 	if (zpool_remove_zvol_links(zhp) != 0)
956fa9e4066Sahrens 		return (-1);
957fa9e4066Sahrens 
958fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
959fa9e4066Sahrens 
960ecd6cf80Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
96199653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
96299653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
963fa9e4066Sahrens 
96499653d4eSeschrock 		if (errno == EROFS) {
96599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
96699653d4eSeschrock 			    "one or more devices is read only"));
96799653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
96899653d4eSeschrock 		} else {
96999653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
970fa9e4066Sahrens 		}
971fa9e4066Sahrens 
972fa9e4066Sahrens 		if (zfp)
973fa9e4066Sahrens 			zfs_close(zfp);
974fa9e4066Sahrens 		return (-1);
975fa9e4066Sahrens 	}
976fa9e4066Sahrens 
977fa9e4066Sahrens 	if (zfp) {
978fa9e4066Sahrens 		remove_mountpoint(zfp);
979fa9e4066Sahrens 		zfs_close(zfp);
980fa9e4066Sahrens 	}
981fa9e4066Sahrens 
982fa9e4066Sahrens 	return (0);
983fa9e4066Sahrens }
984fa9e4066Sahrens 
985fa9e4066Sahrens /*
986fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
987fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
988fa9e4066Sahrens  */
989fa9e4066Sahrens int
990fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
991fa9e4066Sahrens {
992e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
99399653d4eSeschrock 	int ret;
99499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
99599653d4eSeschrock 	char msg[1024];
996fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
997fa94a07fSbrendan 	uint_t nspares, nl2cache;
99899653d4eSeschrock 
99999653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
100099653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
100199653d4eSeschrock 
1002fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1003fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
100499653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
100599653d4eSeschrock 	    &spares, &nspares) == 0) {
100699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
100799653d4eSeschrock 		    "upgraded to add hot spares"));
100899653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
100999653d4eSeschrock 	}
1010fa9e4066Sahrens 
1011fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1012fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1013fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1014fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1015fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1016fa94a07fSbrendan 		    "upgraded to add cache devices"));
1017fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1018fa94a07fSbrendan 	}
1019fa94a07fSbrendan 
1020990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
102199653d4eSeschrock 		return (-1);
1022fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1023fa9e4066Sahrens 
1024ecd6cf80Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1025fa9e4066Sahrens 		switch (errno) {
1026fa9e4066Sahrens 		case EBUSY:
1027fa9e4066Sahrens 			/*
1028fa9e4066Sahrens 			 * This can happen if the user has specified the same
1029fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1030fa9e4066Sahrens 			 * until we try to add it and see we already have a
1031fa9e4066Sahrens 			 * label.
1032fa9e4066Sahrens 			 */
103399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
103499653d4eSeschrock 			    "one or more vdevs refer to the same device"));
103599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1036fa9e4066Sahrens 			break;
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 		case EOVERFLOW:
1039fa9e4066Sahrens 			/*
1040fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1041fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1042fa9e4066Sahrens 			 * device was the problem device since there's no
1043fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1044fa9e4066Sahrens 			 */
1045fa9e4066Sahrens 			{
1046fa9e4066Sahrens 				char buf[64];
1047fa9e4066Sahrens 
1048fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1049fa9e4066Sahrens 
105099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105199653d4eSeschrock 				    "device is less than the minimum "
105299653d4eSeschrock 				    "size (%s)"), buf);
1053fa9e4066Sahrens 			}
105499653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
105599653d4eSeschrock 			break;
105699653d4eSeschrock 
105799653d4eSeschrock 		case ENOTSUP:
105899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10598654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
106099653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1061fa9e4066Sahrens 			break;
1062fa9e4066Sahrens 
1063b1b8ab34Slling 		case EDOM:
1064b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10658654d025Sperrin 			    "root pool can not have multiple vdevs"
10668654d025Sperrin 			    " or separate logs"));
1067b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1068b1b8ab34Slling 			break;
1069b1b8ab34Slling 
1070fa94a07fSbrendan 		case ENOTBLK:
1071fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1072fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1073fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1074fa94a07fSbrendan 			break;
1075fa94a07fSbrendan 
1076fa9e4066Sahrens 		default:
107799653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1078fa9e4066Sahrens 		}
1079fa9e4066Sahrens 
108099653d4eSeschrock 		ret = -1;
108199653d4eSeschrock 	} else {
108299653d4eSeschrock 		ret = 0;
1083fa9e4066Sahrens 	}
1084fa9e4066Sahrens 
1085e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1086fa9e4066Sahrens 
108799653d4eSeschrock 	return (ret);
1088fa9e4066Sahrens }
1089fa9e4066Sahrens 
1090fa9e4066Sahrens /*
1091fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1092fa9e4066Sahrens  * mounted datasets in the pool.
1093fa9e4066Sahrens  */
1094fa9e4066Sahrens int
109589a89ebfSlling zpool_export(zpool_handle_t *zhp, boolean_t force)
1096fa9e4066Sahrens {
1097fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
109889a89ebfSlling 	char msg[1024];
1099fa9e4066Sahrens 
1100fa9e4066Sahrens 	if (zpool_remove_zvol_links(zhp) != 0)
1101fa9e4066Sahrens 		return (-1);
1102fa9e4066Sahrens 
110389a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
110489a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
110589a89ebfSlling 
1106fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
110789a89ebfSlling 	zc.zc_cookie = force;
110889a89ebfSlling 
110989a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
111089a89ebfSlling 		switch (errno) {
111189a89ebfSlling 		case EXDEV:
111289a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
111389a89ebfSlling 			    "use '-f' to override the following errors:\n"
111489a89ebfSlling 			    "'%s' has an active shared spare which could be"
111589a89ebfSlling 			    " used by other pools once '%s' is exported."),
111689a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
111789a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
111889a89ebfSlling 			    msg));
111989a89ebfSlling 		default:
112089a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
112189a89ebfSlling 			    msg));
112289a89ebfSlling 		}
112389a89ebfSlling 	}
1124fa9e4066Sahrens 
1125fa9e4066Sahrens 	return (0);
1126fa9e4066Sahrens }
1127fa9e4066Sahrens 
1128fa9e4066Sahrens /*
1129990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1130990b4856Slling  * if possible.
1131990b4856Slling  *
1132990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1133990b4856Slling  * new properties value to be set.
1134fa9e4066Sahrens  */
1135fa9e4066Sahrens int
113699653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1137990b4856Slling     char *altroot)
1138990b4856Slling {
1139990b4856Slling 	nvlist_t *props = NULL;
1140990b4856Slling 	int ret;
1141990b4856Slling 
1142990b4856Slling 	if (altroot != NULL) {
1143990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1144990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1145990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1146990b4856Slling 			    newname));
1147990b4856Slling 		}
1148990b4856Slling 
1149990b4856Slling 		if (nvlist_add_string(props,
1150990b4856Slling 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
1151990b4856Slling 			nvlist_free(props);
1152990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1153990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1154990b4856Slling 			    newname));
1155990b4856Slling 		}
1156990b4856Slling 	}
1157990b4856Slling 
1158c5904d13Seschrock 	ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1159990b4856Slling 	if (props)
1160990b4856Slling 		nvlist_free(props);
1161990b4856Slling 	return (ret);
1162990b4856Slling }
1163990b4856Slling 
1164990b4856Slling /*
1165990b4856Slling  * Import the given pool using the known configuration and a list of
1166990b4856Slling  * properties to be set. The configuration should have come from
1167990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1168990b4856Slling  * is imported with a different name.
1169990b4856Slling  */
1170990b4856Slling int
1171990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1172c5904d13Seschrock     nvlist_t *props, boolean_t importfaulted)
1173fa9e4066Sahrens {
1174e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1175fa9e4066Sahrens 	char *thename;
1176fa9e4066Sahrens 	char *origname;
1177fa9e4066Sahrens 	int ret;
1178990b4856Slling 	char errbuf[1024];
1179fa9e4066Sahrens 
1180fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1181fa9e4066Sahrens 	    &origname) == 0);
1182fa9e4066Sahrens 
1183990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1184990b4856Slling 	    "cannot import pool '%s'"), origname);
1185990b4856Slling 
1186fa9e4066Sahrens 	if (newname != NULL) {
118799653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1188ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
118999653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
119099653d4eSeschrock 			    newname));
1191fa9e4066Sahrens 		thename = (char *)newname;
1192fa9e4066Sahrens 	} else {
1193fa9e4066Sahrens 		thename = origname;
1194fa9e4066Sahrens 	}
1195fa9e4066Sahrens 
1196990b4856Slling 	if (props) {
1197990b4856Slling 		uint64_t version;
1198fa9e4066Sahrens 
1199990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1200990b4856Slling 		    &version) == 0);
1201fa9e4066Sahrens 
12020a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1203351420b3Slling 		    props, version, B_TRUE, errbuf)) == NULL) {
1204990b4856Slling 			return (-1);
1205351420b3Slling 		} else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1206351420b3Slling 			nvlist_free(props);
1207990b4856Slling 			return (-1);
1208351420b3Slling 		}
1209990b4856Slling 	}
1210990b4856Slling 
1211990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1214ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1215fa9e4066Sahrens 
1216351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1217351420b3Slling 		nvlist_free(props);
121899653d4eSeschrock 		return (-1);
1219351420b3Slling 	}
1220fa9e4066Sahrens 
1221c5904d13Seschrock 	zc.zc_cookie = (uint64_t)importfaulted;
1222fa9e4066Sahrens 	ret = 0;
1223ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1224fa9e4066Sahrens 		char desc[1024];
1225fa9e4066Sahrens 		if (newname == NULL)
1226fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1227fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1228fa9e4066Sahrens 			    thename);
1229fa9e4066Sahrens 		else
1230fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1231fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1232fa9e4066Sahrens 			    origname, thename);
1233fa9e4066Sahrens 
1234fa9e4066Sahrens 		switch (errno) {
1235ea8dc4b6Seschrock 		case ENOTSUP:
1236ea8dc4b6Seschrock 			/*
1237ea8dc4b6Seschrock 			 * Unsupported version.
1238ea8dc4b6Seschrock 			 */
123999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1240ea8dc4b6Seschrock 			break;
1241ea8dc4b6Seschrock 
1242b5989ec7Seschrock 		case EINVAL:
1243b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1244b5989ec7Seschrock 			break;
1245b5989ec7Seschrock 
1246fa9e4066Sahrens 		default:
124799653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, desc);
1248fa9e4066Sahrens 		}
1249fa9e4066Sahrens 
1250fa9e4066Sahrens 		ret = -1;
1251fa9e4066Sahrens 	} else {
1252fa9e4066Sahrens 		zpool_handle_t *zhp;
1253ecd6cf80Smarks 
1254fa9e4066Sahrens 		/*
1255fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1256fa9e4066Sahrens 		 */
125794de1d4cSeschrock 		if (zpool_open_silent(hdl, thename, &zhp) != 0) {
125894de1d4cSeschrock 			ret = -1;
125994de1d4cSeschrock 		} else if (zhp != NULL) {
1260fa9e4066Sahrens 			ret = zpool_create_zvol_links(zhp);
1261fa9e4066Sahrens 			zpool_close(zhp);
1262fa9e4066Sahrens 		}
1263ecd6cf80Smarks 
1264fa9e4066Sahrens 	}
1265fa9e4066Sahrens 
1266e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1267351420b3Slling 	nvlist_free(props);
1268351420b3Slling 
1269fa9e4066Sahrens 	return (ret);
1270fa9e4066Sahrens }
1271fa9e4066Sahrens 
1272fa9e4066Sahrens /*
1273fa9e4066Sahrens  * Scrub the pool.
1274fa9e4066Sahrens  */
1275fa9e4066Sahrens int
1276fa9e4066Sahrens zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1277fa9e4066Sahrens {
1278fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1279fa9e4066Sahrens 	char msg[1024];
128099653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1281fa9e4066Sahrens 
1282fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1283fa9e4066Sahrens 	zc.zc_cookie = type;
1284fa9e4066Sahrens 
1285ecd6cf80Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1286fa9e4066Sahrens 		return (0);
1287fa9e4066Sahrens 
1288fa9e4066Sahrens 	(void) snprintf(msg, sizeof (msg),
1289fa9e4066Sahrens 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1290fa9e4066Sahrens 
129199653d4eSeschrock 	if (errno == EBUSY)
129299653d4eSeschrock 		return (zfs_error(hdl, EZFS_RESILVERING, msg));
129399653d4eSeschrock 	else
129499653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
1295fa9e4066Sahrens }
1296fa9e4066Sahrens 
1297a43d325bSek /*
1298a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1299a43d325bSek  * spare; but FALSE if its an INUSE spare.
1300a43d325bSek  */
130199653d4eSeschrock static nvlist_t *
130299653d4eSeschrock vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1303fa94a07fSbrendan     boolean_t *avail_spare, boolean_t *l2cache)
1304ea8dc4b6Seschrock {
1305ea8dc4b6Seschrock 	uint_t c, children;
1306ea8dc4b6Seschrock 	nvlist_t **child;
130799653d4eSeschrock 	uint64_t theguid, present;
1308ea8dc4b6Seschrock 	char *path;
1309ea8dc4b6Seschrock 	uint64_t wholedisk = 0;
131099653d4eSeschrock 	nvlist_t *ret;
1311ea8dc4b6Seschrock 
131299653d4eSeschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1313ea8dc4b6Seschrock 
1314ea8dc4b6Seschrock 	if (search == NULL &&
1315ea8dc4b6Seschrock 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1316ea8dc4b6Seschrock 		/*
1317ea8dc4b6Seschrock 		 * If the device has never been present since import, the only
1318ea8dc4b6Seschrock 		 * reliable way to match the vdev is by GUID.
1319ea8dc4b6Seschrock 		 */
132099653d4eSeschrock 		if (theguid == guid)
132199653d4eSeschrock 			return (nv);
1322ea8dc4b6Seschrock 	} else if (search != NULL &&
1323ea8dc4b6Seschrock 	    nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1324ea8dc4b6Seschrock 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1325ea8dc4b6Seschrock 		    &wholedisk);
1326ea8dc4b6Seschrock 		if (wholedisk) {
1327ea8dc4b6Seschrock 			/*
1328ea8dc4b6Seschrock 			 * For whole disks, the internal path has 's0', but the
1329ea8dc4b6Seschrock 			 * path passed in by the user doesn't.
1330ea8dc4b6Seschrock 			 */
1331ea8dc4b6Seschrock 			if (strlen(search) == strlen(path) - 2 &&
1332ea8dc4b6Seschrock 			    strncmp(search, path, strlen(search)) == 0)
133399653d4eSeschrock 				return (nv);
1334ea8dc4b6Seschrock 		} else if (strcmp(search, path) == 0) {
133599653d4eSeschrock 			return (nv);
1336ea8dc4b6Seschrock 		}
1337ea8dc4b6Seschrock 	}
1338ea8dc4b6Seschrock 
1339ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1340ea8dc4b6Seschrock 	    &child, &children) != 0)
134199653d4eSeschrock 		return (NULL);
1342ea8dc4b6Seschrock 
1343ea8dc4b6Seschrock 	for (c = 0; c < children; c++)
134499653d4eSeschrock 		if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1345fa94a07fSbrendan 		    avail_spare, l2cache)) != NULL)
1346ea8dc4b6Seschrock 			return (ret);
1347ea8dc4b6Seschrock 
134899653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
134999653d4eSeschrock 	    &child, &children) == 0) {
135099653d4eSeschrock 		for (c = 0; c < children; c++) {
135199653d4eSeschrock 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1352fa94a07fSbrendan 			    avail_spare, l2cache)) != NULL) {
1353a43d325bSek 				*avail_spare = B_TRUE;
135499653d4eSeschrock 				return (ret);
135599653d4eSeschrock 			}
135699653d4eSeschrock 		}
135799653d4eSeschrock 	}
135899653d4eSeschrock 
1359fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1360fa94a07fSbrendan 	    &child, &children) == 0) {
1361fa94a07fSbrendan 		for (c = 0; c < children; c++) {
1362fa94a07fSbrendan 			if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1363fa94a07fSbrendan 			    avail_spare, l2cache)) != NULL) {
1364fa94a07fSbrendan 				*l2cache = B_TRUE;
1365fa94a07fSbrendan 				return (ret);
1366fa94a07fSbrendan 			}
1367fa94a07fSbrendan 		}
1368fa94a07fSbrendan 	}
1369fa94a07fSbrendan 
137099653d4eSeschrock 	return (NULL);
1371ea8dc4b6Seschrock }
1372ea8dc4b6Seschrock 
137399653d4eSeschrock nvlist_t *
1374fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1375fa94a07fSbrendan     boolean_t *l2cache)
1376ea8dc4b6Seschrock {
1377ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
1378ea8dc4b6Seschrock 	const char *search;
1379ea8dc4b6Seschrock 	char *end;
1380ea8dc4b6Seschrock 	nvlist_t *nvroot;
1381ea8dc4b6Seschrock 	uint64_t guid;
1382ea8dc4b6Seschrock 
13830917b783Seschrock 	guid = strtoull(path, &end, 10);
1384ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
1385ea8dc4b6Seschrock 		search = NULL;
1386ea8dc4b6Seschrock 	} else if (path[0] != '/') {
1387ea8dc4b6Seschrock 		(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
1388ea8dc4b6Seschrock 		search = buf;
1389ea8dc4b6Seschrock 	} else {
1390ea8dc4b6Seschrock 		search = path;
1391ea8dc4b6Seschrock 	}
1392ea8dc4b6Seschrock 
1393ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1394ea8dc4b6Seschrock 	    &nvroot) == 0);
1395ea8dc4b6Seschrock 
1396a43d325bSek 	*avail_spare = B_FALSE;
1397fa94a07fSbrendan 	*l2cache = B_FALSE;
1398fa94a07fSbrendan 	return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1399fa94a07fSbrendan 	    l2cache));
1400a43d325bSek }
1401a43d325bSek 
1402a43d325bSek /*
1403fa94a07fSbrendan  * Returns TRUE if the given guid corresponds to the given type.
1404fa94a07fSbrendan  * This is used to check for hot spares (INUSE or not), and level 2 cache
1405fa94a07fSbrendan  * devices.
1406a43d325bSek  */
1407a43d325bSek static boolean_t
1408fa94a07fSbrendan is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1409a43d325bSek {
1410fa94a07fSbrendan 	uint64_t target_guid;
1411a43d325bSek 	nvlist_t *nvroot;
1412fa94a07fSbrendan 	nvlist_t **list;
1413fa94a07fSbrendan 	uint_t count;
1414a43d325bSek 	int i;
1415a43d325bSek 
1416a43d325bSek 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1417a43d325bSek 	    &nvroot) == 0);
1418fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1419fa94a07fSbrendan 		for (i = 0; i < count; i++) {
1420fa94a07fSbrendan 			verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1421fa94a07fSbrendan 			    &target_guid) == 0);
1422fa94a07fSbrendan 			if (guid == target_guid)
1423a43d325bSek 				return (B_TRUE);
1424a43d325bSek 		}
1425a43d325bSek 	}
1426a43d325bSek 
1427a43d325bSek 	return (B_FALSE);
1428ea8dc4b6Seschrock }
1429ea8dc4b6Seschrock 
1430fa9e4066Sahrens /*
14313d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
14323d7072f8Seschrock  * ZFS_ONLINE_* flags.
1433fa9e4066Sahrens  */
1434fa9e4066Sahrens int
14353d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
14363d7072f8Seschrock     vdev_state_t *newstate)
1437fa9e4066Sahrens {
1438fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1439fa9e4066Sahrens 	char msg[1024];
144099653d4eSeschrock 	nvlist_t *tgt;
1441fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
144299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1443fa9e4066Sahrens 
1444ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
1445ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1446ea8dc4b6Seschrock 
1447fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1448fa94a07fSbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
144999653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1450fa9e4066Sahrens 
145199653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1452fa9e4066Sahrens 
1453fa94a07fSbrendan 	if (avail_spare ||
1454fa94a07fSbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1455a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
1456a43d325bSek 
14573d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
14583d7072f8Seschrock 	zc.zc_obj = flags;
1459fa9e4066Sahrens 
1460ecd6cf80Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
14613d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
14623d7072f8Seschrock 
14633d7072f8Seschrock 	*newstate = zc.zc_cookie;
14643d7072f8Seschrock 	return (0);
1465fa9e4066Sahrens }
1466fa9e4066Sahrens 
1467fa9e4066Sahrens /*
1468fa9e4066Sahrens  * Take the specified vdev offline
1469fa9e4066Sahrens  */
1470fa9e4066Sahrens int
14713d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1472fa9e4066Sahrens {
1473fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1474fa9e4066Sahrens 	char msg[1024];
147599653d4eSeschrock 	nvlist_t *tgt;
1476fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
147799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1478fa9e4066Sahrens 
1479ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
1480ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1481ea8dc4b6Seschrock 
1482fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1483fa94a07fSbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == NULL)
148499653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
148599653d4eSeschrock 
148699653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1487fa9e4066Sahrens 
1488fa94a07fSbrendan 	if (avail_spare ||
1489fa94a07fSbrendan 	    is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1490a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
1491a43d325bSek 
14923d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
14933d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
14943d7072f8Seschrock 
1495ecd6cf80Smarks 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
14963d7072f8Seschrock 		return (0);
14973d7072f8Seschrock 
14983d7072f8Seschrock 	switch (errno) {
14993d7072f8Seschrock 	case EBUSY:
15003d7072f8Seschrock 
15013d7072f8Seschrock 		/*
15023d7072f8Seschrock 		 * There are no other replicas of this device.
15033d7072f8Seschrock 		 */
15043d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
15053d7072f8Seschrock 
15063d7072f8Seschrock 	default:
15073d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
15083d7072f8Seschrock 	}
15093d7072f8Seschrock }
15103d7072f8Seschrock 
15113d7072f8Seschrock /*
15123d7072f8Seschrock  * Mark the given vdev faulted.
15133d7072f8Seschrock  */
15143d7072f8Seschrock int
15153d7072f8Seschrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
15163d7072f8Seschrock {
15173d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
15183d7072f8Seschrock 	char msg[1024];
15193d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15203d7072f8Seschrock 
15213d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
15223d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1523441d80aaSlling 
15243d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15253d7072f8Seschrock 	zc.zc_guid = guid;
15263d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
15273d7072f8Seschrock 
15283d7072f8Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1529fa9e4066Sahrens 		return (0);
1530fa9e4066Sahrens 
1531fa9e4066Sahrens 	switch (errno) {
153299653d4eSeschrock 	case EBUSY:
1533fa9e4066Sahrens 
1534fa9e4066Sahrens 		/*
1535fa9e4066Sahrens 		 * There are no other replicas of this device.
1536fa9e4066Sahrens 		 */
153799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1538fa9e4066Sahrens 
153999653d4eSeschrock 	default:
154099653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
1541fa9e4066Sahrens 	}
15423d7072f8Seschrock 
15433d7072f8Seschrock }
15443d7072f8Seschrock 
15453d7072f8Seschrock /*
15463d7072f8Seschrock  * Mark the given vdev degraded.
15473d7072f8Seschrock  */
15483d7072f8Seschrock int
15493d7072f8Seschrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
15503d7072f8Seschrock {
15513d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
15523d7072f8Seschrock 	char msg[1024];
15533d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
15543d7072f8Seschrock 
15553d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
15563d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
15573d7072f8Seschrock 
15583d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
15593d7072f8Seschrock 	zc.zc_guid = guid;
15603d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
15613d7072f8Seschrock 
15623d7072f8Seschrock 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
15633d7072f8Seschrock 		return (0);
15643d7072f8Seschrock 
15653d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
156699653d4eSeschrock }
156799653d4eSeschrock 
156899653d4eSeschrock /*
156999653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
157099653d4eSeschrock  * a hot spare.
157199653d4eSeschrock  */
157299653d4eSeschrock static boolean_t
157399653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
157499653d4eSeschrock {
157599653d4eSeschrock 	nvlist_t **child;
157699653d4eSeschrock 	uint_t c, children;
157799653d4eSeschrock 	char *type;
157899653d4eSeschrock 
157999653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
158099653d4eSeschrock 	    &children) == 0) {
158199653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
158299653d4eSeschrock 		    &type) == 0);
158399653d4eSeschrock 
158499653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
158599653d4eSeschrock 		    children == 2 && child[which] == tgt)
158699653d4eSeschrock 			return (B_TRUE);
158799653d4eSeschrock 
158899653d4eSeschrock 		for (c = 0; c < children; c++)
158999653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
159099653d4eSeschrock 				return (B_TRUE);
159199653d4eSeschrock 	}
159299653d4eSeschrock 
159399653d4eSeschrock 	return (B_FALSE);
1594fa9e4066Sahrens }
1595fa9e4066Sahrens 
1596fa9e4066Sahrens /*
1597fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
15988654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
1599fa9e4066Sahrens  */
1600fa9e4066Sahrens int
1601fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
1602fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1603fa9e4066Sahrens {
1604fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1605fa9e4066Sahrens 	char msg[1024];
1606fa9e4066Sahrens 	int ret;
160799653d4eSeschrock 	nvlist_t *tgt;
1608fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
16098654d025Sperrin 	uint64_t val, is_log;
16100430f8daSeschrock 	char *path, *newname;
161199653d4eSeschrock 	nvlist_t **child;
161299653d4eSeschrock 	uint_t children;
161399653d4eSeschrock 	nvlist_t *config_root;
161499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1615fa9e4066Sahrens 
1616ea8dc4b6Seschrock 	if (replacing)
1617ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1618ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
1619ea8dc4b6Seschrock 	else
1620ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1621ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
1622ea8dc4b6Seschrock 
1623fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1624fa94a07fSbrendan 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache)) == 0)
162599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
162699653d4eSeschrock 
1627a43d325bSek 	if (avail_spare)
162899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
162999653d4eSeschrock 
1630fa94a07fSbrendan 	if (l2cache)
1631fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1632fa94a07fSbrendan 
163399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1634fa9e4066Sahrens 	zc.zc_cookie = replacing;
1635fa9e4066Sahrens 
163699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
163799653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
163899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163999653d4eSeschrock 		    "new device must be a single disk"));
164099653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
164199653d4eSeschrock 	}
164299653d4eSeschrock 
164399653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
164499653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
164599653d4eSeschrock 
16460430f8daSeschrock 	if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
16470430f8daSeschrock 		return (-1);
16480430f8daSeschrock 
164999653d4eSeschrock 	/*
165099653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
165199653d4eSeschrock 	 * replace it with another hot spare.
165299653d4eSeschrock 	 */
165399653d4eSeschrock 	if (replacing &&
165499653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
16550430f8daSeschrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) == NULL ||
1656a43d325bSek 	    !avail_spare) && is_replacing_spare(config_root, tgt, 1)) {
165799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
165899653d4eSeschrock 		    "can only be replaced by another hot spare"));
16590430f8daSeschrock 		free(newname);
166099653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
166199653d4eSeschrock 	}
166299653d4eSeschrock 
166399653d4eSeschrock 	/*
166499653d4eSeschrock 	 * If we are attempting to replace a spare, it canot be applied to an
166599653d4eSeschrock 	 * already spared device.
166699653d4eSeschrock 	 */
166799653d4eSeschrock 	if (replacing &&
166899653d4eSeschrock 	    nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
16690430f8daSeschrock 	    zpool_find_vdev(zhp, newname, &avail_spare, &l2cache) != NULL &&
1670fa94a07fSbrendan 	    avail_spare && is_replacing_spare(config_root, tgt, 0)) {
167199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167299653d4eSeschrock 		    "device has already been replaced with a spare"));
16730430f8daSeschrock 		free(newname);
167499653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
167599653d4eSeschrock 	}
167699653d4eSeschrock 
16770430f8daSeschrock 	free(newname);
16780430f8daSeschrock 
1679990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
168099653d4eSeschrock 		return (-1);
1681fa9e4066Sahrens 
1682ecd6cf80Smarks 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1683fa9e4066Sahrens 
1684e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1685fa9e4066Sahrens 
1686fa9e4066Sahrens 	if (ret == 0)
1687fa9e4066Sahrens 		return (0);
1688fa9e4066Sahrens 
1689fa9e4066Sahrens 	switch (errno) {
1690ea8dc4b6Seschrock 	case ENOTSUP:
1691fa9e4066Sahrens 		/*
1692fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
1693fa9e4066Sahrens 		 */
16948654d025Sperrin 		if (replacing) {
16958654d025Sperrin 			is_log = B_FALSE;
16968654d025Sperrin 			(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_LOG,
16978654d025Sperrin 			    &is_log);
16988654d025Sperrin 			if (is_log)
16998654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17008654d025Sperrin 				    "cannot replace a log with a spare"));
17018654d025Sperrin 			else
17028654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17038654d025Sperrin 				    "cannot replace a replacing device"));
17048654d025Sperrin 		} else {
170599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170699653d4eSeschrock 			    "can only attach to mirrors and top-level "
170799653d4eSeschrock 			    "disks"));
17088654d025Sperrin 		}
170999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
1710fa9e4066Sahrens 		break;
1711fa9e4066Sahrens 
1712ea8dc4b6Seschrock 	case EINVAL:
1713fa9e4066Sahrens 		/*
1714fa9e4066Sahrens 		 * The new device must be a single disk.
1715fa9e4066Sahrens 		 */
171699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171799653d4eSeschrock 		    "new device must be a single disk"));
171899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1719fa9e4066Sahrens 		break;
1720fa9e4066Sahrens 
1721ea8dc4b6Seschrock 	case EBUSY:
172299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
172399653d4eSeschrock 		    new_disk);
172499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1725fa9e4066Sahrens 		break;
1726fa9e4066Sahrens 
1727ea8dc4b6Seschrock 	case EOVERFLOW:
1728fa9e4066Sahrens 		/*
1729fa9e4066Sahrens 		 * The new device is too small.
1730fa9e4066Sahrens 		 */
173199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173299653d4eSeschrock 		    "device is too small"));
173399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1734fa9e4066Sahrens 		break;
1735fa9e4066Sahrens 
1736ea8dc4b6Seschrock 	case EDOM:
1737fa9e4066Sahrens 		/*
1738fa9e4066Sahrens 		 * The new device has a different alignment requirement.
1739fa9e4066Sahrens 		 */
174099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174199653d4eSeschrock 		    "devices have different sector alignment"));
174299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
1743fa9e4066Sahrens 		break;
1744fa9e4066Sahrens 
1745ea8dc4b6Seschrock 	case ENAMETOOLONG:
1746fa9e4066Sahrens 		/*
1747fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
1748fa9e4066Sahrens 		 */
174999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1750fa9e4066Sahrens 		break;
1751fa9e4066Sahrens 
1752ea8dc4b6Seschrock 	default:
175399653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
1754fa9e4066Sahrens 	}
1755fa9e4066Sahrens 
175699653d4eSeschrock 	return (-1);
1757fa9e4066Sahrens }
1758fa9e4066Sahrens 
1759fa9e4066Sahrens /*
1760fa9e4066Sahrens  * Detach the specified device.
1761fa9e4066Sahrens  */
1762fa9e4066Sahrens int
1763fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1764fa9e4066Sahrens {
1765fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1766fa9e4066Sahrens 	char msg[1024];
176799653d4eSeschrock 	nvlist_t *tgt;
1768fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
176999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1770fa9e4066Sahrens 
1771ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
1772ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1773ea8dc4b6Seschrock 
1774fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1775fa94a07fSbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
177699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
1777fa9e4066Sahrens 
1778a43d325bSek 	if (avail_spare)
177999653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
178099653d4eSeschrock 
1781fa94a07fSbrendan 	if (l2cache)
1782fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1783fa94a07fSbrendan 
178499653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
178599653d4eSeschrock 
1786ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1787fa9e4066Sahrens 		return (0);
1788fa9e4066Sahrens 
1789fa9e4066Sahrens 	switch (errno) {
1790fa9e4066Sahrens 
1791ea8dc4b6Seschrock 	case ENOTSUP:
1792fa9e4066Sahrens 		/*
1793fa9e4066Sahrens 		 * Can't detach from this type of vdev.
1794fa9e4066Sahrens 		 */
179599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
179699653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
179799653d4eSeschrock 		(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1798fa9e4066Sahrens 		break;
1799fa9e4066Sahrens 
1800ea8dc4b6Seschrock 	case EBUSY:
1801fa9e4066Sahrens 		/*
1802fa9e4066Sahrens 		 * There are no other replicas of this device.
1803fa9e4066Sahrens 		 */
180499653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1805fa9e4066Sahrens 		break;
1806fa9e4066Sahrens 
1807ea8dc4b6Seschrock 	default:
180899653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
1809ea8dc4b6Seschrock 	}
1810ea8dc4b6Seschrock 
181199653d4eSeschrock 	return (-1);
181299653d4eSeschrock }
181399653d4eSeschrock 
181499653d4eSeschrock /*
1815fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
1816fa94a07fSbrendan  * and level 2 cache devices.
181799653d4eSeschrock  */
181899653d4eSeschrock int
181999653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
182099653d4eSeschrock {
182199653d4eSeschrock 	zfs_cmd_t zc = { 0 };
182299653d4eSeschrock 	char msg[1024];
182399653d4eSeschrock 	nvlist_t *tgt;
1824fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
182599653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
182699653d4eSeschrock 
182799653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
182899653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
182999653d4eSeschrock 
183099653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1831fa94a07fSbrendan 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache)) == 0)
183299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
183399653d4eSeschrock 
1834fa94a07fSbrendan 	if (!avail_spare && !l2cache) {
183599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1836fa94a07fSbrendan 		    "only inactive hot spares or cache devices "
1837fa94a07fSbrendan 		    "can be removed"));
183899653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
183999653d4eSeschrock 	}
184099653d4eSeschrock 
184199653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
184299653d4eSeschrock 
1843ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
184499653d4eSeschrock 		return (0);
184599653d4eSeschrock 
184699653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
1847ea8dc4b6Seschrock }
1848ea8dc4b6Seschrock 
1849ea8dc4b6Seschrock /*
1850ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
1851ea8dc4b6Seschrock  */
1852ea8dc4b6Seschrock int
1853ea8dc4b6Seschrock zpool_clear(zpool_handle_t *zhp, const char *path)
1854ea8dc4b6Seschrock {
1855ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
1856ea8dc4b6Seschrock 	char msg[1024];
185799653d4eSeschrock 	nvlist_t *tgt;
1858fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
185999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1860ea8dc4b6Seschrock 
1861ea8dc4b6Seschrock 	if (path)
1862ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
1863ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
1864e9dbad6fSeschrock 		    path);
1865ea8dc4b6Seschrock 	else
1866ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
1867ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
1868ea8dc4b6Seschrock 		    zhp->zpool_name);
1869ea8dc4b6Seschrock 
1870ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
187199653d4eSeschrock 	if (path) {
1872fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
1873fa94a07fSbrendan 		    &l2cache)) == 0)
187499653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
1875ea8dc4b6Seschrock 
1876fa94a07fSbrendan 		/*
1877fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
1878fa94a07fSbrendan 		 * error clearing for l2cache devices.
1879fa94a07fSbrendan 		 */
1880a43d325bSek 		if (avail_spare)
188199653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
1882ea8dc4b6Seschrock 
188399653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
188499653d4eSeschrock 		    &zc.zc_guid) == 0);
1885fa9e4066Sahrens 	}
1886fa9e4066Sahrens 
1887ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
188899653d4eSeschrock 		return (0);
188999653d4eSeschrock 
189099653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
1891fa9e4066Sahrens }
1892fa9e4066Sahrens 
18933d7072f8Seschrock /*
18943d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
18953d7072f8Seschrock  */
18963d7072f8Seschrock int
18973d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
18983d7072f8Seschrock {
18993d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
19003d7072f8Seschrock 	char msg[1024];
19013d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
19023d7072f8Seschrock 
19033d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
19043d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
19053d7072f8Seschrock 	    guid);
19063d7072f8Seschrock 
19073d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
19083d7072f8Seschrock 	zc.zc_guid = guid;
19093d7072f8Seschrock 
19103d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
19113d7072f8Seschrock 		return (0);
19123d7072f8Seschrock 
19133d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
19143d7072f8Seschrock }
19153d7072f8Seschrock 
1916f3861e1aSahl /*
1917f3861e1aSahl  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
1918f3861e1aSahl  * hierarchy.
1919f3861e1aSahl  */
1920f3861e1aSahl int
1921f3861e1aSahl zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
1922f3861e1aSahl     void *data)
1923fa9e4066Sahrens {
1924f3861e1aSahl 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1925f3861e1aSahl 	char (*paths)[MAXPATHLEN];
1926f3861e1aSahl 	size_t size = 4;
1927f3861e1aSahl 	int curr, fd, base, ret = 0;
1928f3861e1aSahl 	DIR *dirp;
1929f3861e1aSahl 	struct dirent *dp;
1930f3861e1aSahl 	struct stat st;
1931f3861e1aSahl 
1932f3861e1aSahl 	if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
1933f3861e1aSahl 		return (errno == ENOENT ? 0 : -1);
1934f3861e1aSahl 
1935f3861e1aSahl 	if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
1936f3861e1aSahl 		int err = errno;
1937f3861e1aSahl 		(void) close(base);
1938f3861e1aSahl 		return (err == ENOENT ? 0 : -1);
1939f3861e1aSahl 	}
1940fa9e4066Sahrens 
1941fa9e4066Sahrens 	/*
1942f3861e1aSahl 	 * Oddly this wasn't a directory -- ignore that failure since we
1943f3861e1aSahl 	 * know there are no links lower in the (non-existant) hierarchy.
1944fa9e4066Sahrens 	 */
1945f3861e1aSahl 	if (!S_ISDIR(st.st_mode)) {
1946f3861e1aSahl 		(void) close(base);
1947f3861e1aSahl 		return (0);
1948fa9e4066Sahrens 	}
1949fa9e4066Sahrens 
1950f3861e1aSahl 	if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
1951f3861e1aSahl 		(void) close(base);
1952f3861e1aSahl 		return (-1);
1953f3861e1aSahl 	}
1954f3861e1aSahl 
1955f3861e1aSahl 	(void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
1956f3861e1aSahl 	curr = 0;
1957f3861e1aSahl 
1958f3861e1aSahl 	while (curr >= 0) {
1959f3861e1aSahl 		if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
1960f3861e1aSahl 			goto err;
1961f3861e1aSahl 
1962f3861e1aSahl 		if (S_ISDIR(st.st_mode)) {
1963f3861e1aSahl 			if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
1964f3861e1aSahl 				goto err;
1965f3861e1aSahl 
1966f3861e1aSahl 			if ((dirp = fdopendir(fd)) == NULL) {
1967f3861e1aSahl 				(void) close(fd);
1968f3861e1aSahl 				goto err;
1969f3861e1aSahl 			}
1970f3861e1aSahl 
1971f3861e1aSahl 			while ((dp = readdir(dirp)) != NULL) {
1972f3861e1aSahl 				if (dp->d_name[0] == '.')
1973f3861e1aSahl 					continue;
1974f3861e1aSahl 
1975f3861e1aSahl 				if (curr + 1 == size) {
1976f3861e1aSahl 					paths = zfs_realloc(hdl, paths,
1977f3861e1aSahl 					    size * sizeof (paths[0]),
1978f3861e1aSahl 					    size * 2 * sizeof (paths[0]));
1979f3861e1aSahl 					if (paths == NULL) {
1980f3861e1aSahl 						(void) closedir(dirp);
1981f3861e1aSahl 						(void) close(fd);
1982f3861e1aSahl 						goto err;
1983f3861e1aSahl 					}
1984f3861e1aSahl 
1985f3861e1aSahl 					size *= 2;
1986f3861e1aSahl 				}
1987f3861e1aSahl 
1988f3861e1aSahl 				(void) strlcpy(paths[curr + 1], paths[curr],
1989f3861e1aSahl 				    sizeof (paths[curr + 1]));
1990f3861e1aSahl 				(void) strlcat(paths[curr], "/",
1991f3861e1aSahl 				    sizeof (paths[curr]));
1992f3861e1aSahl 				(void) strlcat(paths[curr], dp->d_name,
1993f3861e1aSahl 				    sizeof (paths[curr]));
1994f3861e1aSahl 				curr++;
1995f3861e1aSahl 			}
1996f3861e1aSahl 
1997f3861e1aSahl 			(void) closedir(dirp);
1998f3861e1aSahl 
1999f3861e1aSahl 		} else {
2000f3861e1aSahl 			if ((ret = cb(paths[curr], data)) != 0)
2001f3861e1aSahl 				break;
2002f3861e1aSahl 		}
2003f3861e1aSahl 
2004f3861e1aSahl 		curr--;
2005f3861e1aSahl 	}
2006f3861e1aSahl 
2007f3861e1aSahl 	free(paths);
2008f3861e1aSahl 	(void) close(base);
2009f3861e1aSahl 
2010f3861e1aSahl 	return (ret);
2011f3861e1aSahl 
2012f3861e1aSahl err:
2013f3861e1aSahl 	free(paths);
2014f3861e1aSahl 	(void) close(base);
2015f3861e1aSahl 	return (-1);
2016f3861e1aSahl }
2017f3861e1aSahl 
2018f3861e1aSahl typedef struct zvol_cb {
2019f3861e1aSahl 	zpool_handle_t *zcb_pool;
2020f3861e1aSahl 	boolean_t zcb_create;
2021f3861e1aSahl } zvol_cb_t;
2022f3861e1aSahl 
2023f3861e1aSahl /*ARGSUSED*/
2024f3861e1aSahl static int
2025f3861e1aSahl do_zvol_create(zfs_handle_t *zhp, void *data)
2026f3861e1aSahl {
20273aefe2c7Sahrens 	int ret = 0;
2028f3861e1aSahl 
20293aefe2c7Sahrens 	if (ZFS_IS_VOLUME(zhp)) {
2030f3861e1aSahl 		(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
20313aefe2c7Sahrens 		ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
20323aefe2c7Sahrens 	}
2033f3861e1aSahl 
20343aefe2c7Sahrens 	if (ret == 0)
20353aefe2c7Sahrens 		ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2036fa9e4066Sahrens 
2037fa9e4066Sahrens 	zfs_close(zhp);
2038f3861e1aSahl 
2039fa9e4066Sahrens 	return (ret);
2040fa9e4066Sahrens }
2041fa9e4066Sahrens 
2042fa9e4066Sahrens /*
2043fa9e4066Sahrens  * Iterate over all zvols in the pool and make any necessary minor nodes.
2044fa9e4066Sahrens  */
2045fa9e4066Sahrens int
2046fa9e4066Sahrens zpool_create_zvol_links(zpool_handle_t *zhp)
2047fa9e4066Sahrens {
2048fa9e4066Sahrens 	zfs_handle_t *zfp;
2049fa9e4066Sahrens 	int ret;
2050fa9e4066Sahrens 
2051fa9e4066Sahrens 	/*
2052fa9e4066Sahrens 	 * If the pool is unavailable, just return success.
2053fa9e4066Sahrens 	 */
205499653d4eSeschrock 	if ((zfp = make_dataset_handle(zhp->zpool_hdl,
205599653d4eSeschrock 	    zhp->zpool_name)) == NULL)
2056fa9e4066Sahrens 		return (0);
2057fa9e4066Sahrens 
20583aefe2c7Sahrens 	ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2059fa9e4066Sahrens 
2060fa9e4066Sahrens 	zfs_close(zfp);
2061fa9e4066Sahrens 	return (ret);
2062fa9e4066Sahrens }
2063fa9e4066Sahrens 
2064f3861e1aSahl static int
2065f3861e1aSahl do_zvol_remove(const char *dataset, void *data)
2066f3861e1aSahl {
2067f3861e1aSahl 	zpool_handle_t *zhp = data;
2068f3861e1aSahl 
2069f3861e1aSahl 	return (zvol_remove_link(zhp->zpool_hdl, dataset));
2070f3861e1aSahl }
2071f3861e1aSahl 
2072fa9e4066Sahrens /*
2073f3861e1aSahl  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
2074f3861e1aSahl  * by examining the /dev links so that a corrupted pool doesn't impede this
2075f3861e1aSahl  * operation.
2076fa9e4066Sahrens  */
2077fa9e4066Sahrens int
2078fa9e4066Sahrens zpool_remove_zvol_links(zpool_handle_t *zhp)
2079fa9e4066Sahrens {
2080f3861e1aSahl 	return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2081fa9e4066Sahrens }
2082c67d9675Seschrock 
2083c67d9675Seschrock /*
2084c67d9675Seschrock  * Convert from a devid string to a path.
2085c67d9675Seschrock  */
2086c67d9675Seschrock static char *
2087c67d9675Seschrock devid_to_path(char *devid_str)
2088c67d9675Seschrock {
2089c67d9675Seschrock 	ddi_devid_t devid;
2090c67d9675Seschrock 	char *minor;
2091c67d9675Seschrock 	char *path;
2092c67d9675Seschrock 	devid_nmlist_t *list = NULL;
2093c67d9675Seschrock 	int ret;
2094c67d9675Seschrock 
2095c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
2096c67d9675Seschrock 		return (NULL);
2097c67d9675Seschrock 
2098c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2099c67d9675Seschrock 
2100c67d9675Seschrock 	devid_str_free(minor);
2101c67d9675Seschrock 	devid_free(devid);
2102c67d9675Seschrock 
2103c67d9675Seschrock 	if (ret != 0)
2104c67d9675Seschrock 		return (NULL);
2105c67d9675Seschrock 
210699653d4eSeschrock 	if ((path = strdup(list[0].devname)) == NULL)
210799653d4eSeschrock 		return (NULL);
210899653d4eSeschrock 
2109c67d9675Seschrock 	devid_free_nmlist(list);
2110c67d9675Seschrock 
2111c67d9675Seschrock 	return (path);
2112c67d9675Seschrock }
2113c67d9675Seschrock 
2114c67d9675Seschrock /*
2115c67d9675Seschrock  * Convert from a path to a devid string.
2116c67d9675Seschrock  */
2117c67d9675Seschrock static char *
2118c67d9675Seschrock path_to_devid(const char *path)
2119c67d9675Seschrock {
2120c67d9675Seschrock 	int fd;
2121c67d9675Seschrock 	ddi_devid_t devid;
2122c67d9675Seschrock 	char *minor, *ret;
2123c67d9675Seschrock 
2124c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
2125c67d9675Seschrock 		return (NULL);
2126c67d9675Seschrock 
2127c67d9675Seschrock 	minor = NULL;
2128c67d9675Seschrock 	ret = NULL;
2129c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
2130c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
2131c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
2132c67d9675Seschrock 		if (minor != NULL)
2133c67d9675Seschrock 			devid_str_free(minor);
2134c67d9675Seschrock 		devid_free(devid);
2135c67d9675Seschrock 	}
2136c67d9675Seschrock 	(void) close(fd);
2137c67d9675Seschrock 
2138c67d9675Seschrock 	return (ret);
2139c67d9675Seschrock }
2140c67d9675Seschrock 
2141c67d9675Seschrock /*
2142c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
2143c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
2144c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
2145c67d9675Seschrock  */
2146c67d9675Seschrock static void
2147c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2148c67d9675Seschrock {
2149c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
2150c67d9675Seschrock 
2151c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2152e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2153c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2154ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
2155c67d9675Seschrock 
215699653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2157c67d9675Seschrock }
2158c67d9675Seschrock 
2159c67d9675Seschrock /*
2160c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
2161c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2162c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
2163c67d9675Seschrock  * trailing 's0' slice name.
2164c67d9675Seschrock  *
2165c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
2166c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
2167c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
2168c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
2169c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
2170c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
2171c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2172c67d9675Seschrock  * of these checks.
2173c67d9675Seschrock  */
2174c67d9675Seschrock char *
217599653d4eSeschrock zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2176c67d9675Seschrock {
2177c67d9675Seschrock 	char *path, *devid;
2178ea8dc4b6Seschrock 	uint64_t value;
2179ea8dc4b6Seschrock 	char buf[64];
21803d7072f8Seschrock 	vdev_stat_t *vs;
21813d7072f8Seschrock 	uint_t vsc;
2182c67d9675Seschrock 
2183ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2184ea8dc4b6Seschrock 	    &value) == 0) {
2185ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2186ea8dc4b6Seschrock 		    &value) == 0);
21875ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
21885ad82045Snd 		    (u_longlong_t)value);
2189ea8dc4b6Seschrock 		path = buf;
2190ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2191c67d9675Seschrock 
21923d7072f8Seschrock 		/*
21933d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
21943d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
21953d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
21963d7072f8Seschrock 		 * effects.
21973d7072f8Seschrock 		 */
21983d7072f8Seschrock 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
21993d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
22003d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
22013d7072f8Seschrock 		    zhp != NULL &&
2202c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2203c67d9675Seschrock 			/*
2204c67d9675Seschrock 			 * Determine if the current path is correct.
2205c67d9675Seschrock 			 */
2206c67d9675Seschrock 			char *newdevid = path_to_devid(path);
2207c67d9675Seschrock 
2208c67d9675Seschrock 			if (newdevid == NULL ||
2209c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
2210c67d9675Seschrock 				char *newpath;
2211c67d9675Seschrock 
2212c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
2213c67d9675Seschrock 					/*
2214c67d9675Seschrock 					 * Update the path appropriately.
2215c67d9675Seschrock 					 */
2216c67d9675Seschrock 					set_path(zhp, nv, newpath);
221799653d4eSeschrock 					if (nvlist_add_string(nv,
221899653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
221999653d4eSeschrock 						verify(nvlist_lookup_string(nv,
222099653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
222199653d4eSeschrock 						    &path) == 0);
2222c67d9675Seschrock 					free(newpath);
2223c67d9675Seschrock 				}
2224c67d9675Seschrock 			}
2225c67d9675Seschrock 
222699653d4eSeschrock 			if (newdevid)
222799653d4eSeschrock 				devid_str_free(newdevid);
2228c67d9675Seschrock 		}
2229c67d9675Seschrock 
2230c67d9675Seschrock 		if (strncmp(path, "/dev/dsk/", 9) == 0)
2231c67d9675Seschrock 			path += 9;
2232c67d9675Seschrock 
2233c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2234ea8dc4b6Seschrock 		    &value) == 0 && value) {
223599653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
223699653d4eSeschrock 			if (tmp == NULL)
223799653d4eSeschrock 				return (NULL);
2238c67d9675Seschrock 			tmp[strlen(path) - 2] = '\0';
2239c67d9675Seschrock 			return (tmp);
2240c67d9675Seschrock 		}
2241c67d9675Seschrock 	} else {
2242c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
224399653d4eSeschrock 
224499653d4eSeschrock 		/*
224599653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
224699653d4eSeschrock 		 */
224799653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
224899653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
224999653d4eSeschrock 			    &value) == 0);
225099653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
22515ad82045Snd 			    (u_longlong_t)value);
225299653d4eSeschrock 			path = buf;
225399653d4eSeschrock 		}
2254c67d9675Seschrock 	}
2255c67d9675Seschrock 
225699653d4eSeschrock 	return (zfs_strdup(hdl, path));
2257c67d9675Seschrock }
2258ea8dc4b6Seschrock 
2259ea8dc4b6Seschrock static int
2260ea8dc4b6Seschrock zbookmark_compare(const void *a, const void *b)
2261ea8dc4b6Seschrock {
2262ea8dc4b6Seschrock 	return (memcmp(a, b, sizeof (zbookmark_t)));
2263ea8dc4b6Seschrock }
2264ea8dc4b6Seschrock 
2265ea8dc4b6Seschrock /*
2266ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
2267ea8dc4b6Seschrock  * caller.
2268ea8dc4b6Seschrock  */
2269ea8dc4b6Seschrock int
227055434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2271ea8dc4b6Seschrock {
2272ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
2273ea8dc4b6Seschrock 	uint64_t count;
2274e9dbad6fSeschrock 	zbookmark_t *zb = NULL;
227555434c77Sek 	int i;
2276ea8dc4b6Seschrock 
2277ea8dc4b6Seschrock 	/*
2278ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
2279ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
2280ea8dc4b6Seschrock 	 * entire list.
2281ea8dc4b6Seschrock 	 */
2282ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2283ea8dc4b6Seschrock 	    &count) == 0);
228475519f38Sek 	if (count == 0)
228575519f38Sek 		return (0);
2286e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
22875ad82045Snd 	    count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
228899653d4eSeschrock 		return (-1);
2289e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
2290ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
2291ea8dc4b6Seschrock 	for (;;) {
229299653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
229399653d4eSeschrock 		    &zc) != 0) {
2294e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
2295ea8dc4b6Seschrock 			if (errno == ENOMEM) {
2296bf561db0Svb 				count = zc.zc_nvlist_dst_size;
2297e9dbad6fSeschrock 				if ((zc.zc_nvlist_dst = (uintptr_t)
2298bf561db0Svb 				    zfs_alloc(zhp->zpool_hdl, count *
2299bf561db0Svb 				    sizeof (zbookmark_t))) == (uintptr_t)NULL)
230099653d4eSeschrock 					return (-1);
2301ea8dc4b6Seschrock 			} else {
2302ea8dc4b6Seschrock 				return (-1);
2303ea8dc4b6Seschrock 			}
2304ea8dc4b6Seschrock 		} else {
2305ea8dc4b6Seschrock 			break;
2306ea8dc4b6Seschrock 		}
2307ea8dc4b6Seschrock 	}
2308ea8dc4b6Seschrock 
2309ea8dc4b6Seschrock 	/*
2310ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
2311ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
2312e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2313ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
2314ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
2315ea8dc4b6Seschrock 	 */
2316e9dbad6fSeschrock 	zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2317e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
2318e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
2319ea8dc4b6Seschrock 
2320ea8dc4b6Seschrock 	qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2321ea8dc4b6Seschrock 
232255434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2323ea8dc4b6Seschrock 
2324ea8dc4b6Seschrock 	/*
232555434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2326ea8dc4b6Seschrock 	 */
2327ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
2328ea8dc4b6Seschrock 		nvlist_t *nv;
2329ea8dc4b6Seschrock 
2330c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
2331c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2332c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
2333ea8dc4b6Seschrock 			continue;
2334ea8dc4b6Seschrock 
233555434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
233655434c77Sek 			goto nomem;
233755434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
233855434c77Sek 		    zb[i].zb_objset) != 0) {
233955434c77Sek 			nvlist_free(nv);
234099653d4eSeschrock 			goto nomem;
2341ea8dc4b6Seschrock 		}
234255434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
234355434c77Sek 		    zb[i].zb_object) != 0) {
234455434c77Sek 			nvlist_free(nv);
234555434c77Sek 			goto nomem;
234655434c77Sek 		}
234755434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
234855434c77Sek 			nvlist_free(nv);
234955434c77Sek 			goto nomem;
235055434c77Sek 		}
235155434c77Sek 		nvlist_free(nv);
2352ea8dc4b6Seschrock 	}
2353ea8dc4b6Seschrock 
23543ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
2355ea8dc4b6Seschrock 	return (0);
235699653d4eSeschrock 
235799653d4eSeschrock nomem:
2358e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
235999653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
2360ea8dc4b6Seschrock }
2361eaca9bbdSeschrock 
2362eaca9bbdSeschrock /*
2363eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
2364eaca9bbdSeschrock  */
2365eaca9bbdSeschrock int
2366990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2367eaca9bbdSeschrock {
2368eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
236999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2370eaca9bbdSeschrock 
2371eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
2372990b4856Slling 	zc.zc_cookie = new_version;
2373990b4856Slling 
2374ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2375ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
237699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
237799653d4eSeschrock 		    zhp->zpool_name));
2378eaca9bbdSeschrock 	return (0);
2379eaca9bbdSeschrock }
238006eeb2adSek 
238106eeb2adSek void
23822a6b87f0Sek zpool_set_history_str(const char *subcommand, int argc, char **argv,
23832a6b87f0Sek     char *history_str)
238406eeb2adSek {
238506eeb2adSek 	int i;
238606eeb2adSek 
23872a6b87f0Sek 	(void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
23882a6b87f0Sek 	for (i = 1; i < argc; i++) {
23892a6b87f0Sek 		if (strlen(history_str) + 1 + strlen(argv[i]) >
23902a6b87f0Sek 		    HIS_MAX_RECORD_LEN)
23912a6b87f0Sek 			break;
23922a6b87f0Sek 		(void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
23932a6b87f0Sek 		(void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
23942a6b87f0Sek 	}
23952a6b87f0Sek }
23962a6b87f0Sek 
23972a6b87f0Sek /*
23982a6b87f0Sek  * Stage command history for logging.
23992a6b87f0Sek  */
24002a6b87f0Sek int
24012a6b87f0Sek zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
24022a6b87f0Sek {
24032a6b87f0Sek 	if (history_str == NULL)
24042a6b87f0Sek 		return (EINVAL);
24052a6b87f0Sek 
24062a6b87f0Sek 	if (strlen(history_str) > HIS_MAX_RECORD_LEN)
24072a6b87f0Sek 		return (EINVAL);
24082a6b87f0Sek 
2409228975ccSek 	if (hdl->libzfs_log_str != NULL)
2410ecd6cf80Smarks 		free(hdl->libzfs_log_str);
241106eeb2adSek 
24122a6b87f0Sek 	if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
24132a6b87f0Sek 		return (no_memory(hdl));
241406eeb2adSek 
24152a6b87f0Sek 	return (0);
241606eeb2adSek }
241706eeb2adSek 
241806eeb2adSek /*
241906eeb2adSek  * Perform ioctl to get some command history of a pool.
242006eeb2adSek  *
242106eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
242206eeb2adSek  * logical offset of the history buffer to start reading from.
242306eeb2adSek  *
242406eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
242506eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
242606eeb2adSek  */
242706eeb2adSek static int
242806eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
242906eeb2adSek {
243006eeb2adSek 	zfs_cmd_t zc = { 0 };
243106eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
243206eeb2adSek 
243306eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
243406eeb2adSek 
243506eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
243606eeb2adSek 	zc.zc_history_len = *len;
243706eeb2adSek 	zc.zc_history_offset = *off;
243806eeb2adSek 
243906eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
244006eeb2adSek 		switch (errno) {
244106eeb2adSek 		case EPERM:
2442ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
2443ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
244406eeb2adSek 			    "cannot show history for pool '%s'"),
244506eeb2adSek 			    zhp->zpool_name));
244606eeb2adSek 		case ENOENT:
2447ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
244806eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
244906eeb2adSek 			    "'%s'"), zhp->zpool_name));
2450d7306b64Sek 		case ENOTSUP:
2451d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2452d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
2453d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
245406eeb2adSek 		default:
2455ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
245606eeb2adSek 			    dgettext(TEXT_DOMAIN,
245706eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
245806eeb2adSek 		}
245906eeb2adSek 	}
246006eeb2adSek 
246106eeb2adSek 	*len = zc.zc_history_len;
246206eeb2adSek 	*off = zc.zc_history_offset;
246306eeb2adSek 
246406eeb2adSek 	return (0);
246506eeb2adSek }
246606eeb2adSek 
246706eeb2adSek /*
246806eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
246906eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
247006eeb2adSek  * processed as there wasn't a complete record.
247106eeb2adSek  */
247206eeb2adSek static int
247306eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
247406eeb2adSek     nvlist_t ***records, uint_t *numrecords)
247506eeb2adSek {
247606eeb2adSek 	uint64_t reclen;
247706eeb2adSek 	nvlist_t *nv;
247806eeb2adSek 	int i;
247906eeb2adSek 
248006eeb2adSek 	while (bytes_read > sizeof (reclen)) {
248106eeb2adSek 
248206eeb2adSek 		/* get length of packed record (stored as little endian) */
248306eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
248406eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
248506eeb2adSek 
248606eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
248706eeb2adSek 			break;
248806eeb2adSek 
248906eeb2adSek 		/* unpack record */
249006eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
249106eeb2adSek 			return (ENOMEM);
249206eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
249306eeb2adSek 		buf += sizeof (reclen) + reclen;
249406eeb2adSek 
249506eeb2adSek 		/* add record to nvlist array */
249606eeb2adSek 		(*numrecords)++;
249706eeb2adSek 		if (ISP2(*numrecords + 1)) {
249806eeb2adSek 			*records = realloc(*records,
249906eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
250006eeb2adSek 		}
250106eeb2adSek 		(*records)[*numrecords - 1] = nv;
250206eeb2adSek 	}
250306eeb2adSek 
250406eeb2adSek 	*leftover = bytes_read;
250506eeb2adSek 	return (0);
250606eeb2adSek }
250706eeb2adSek 
250806eeb2adSek #define	HIS_BUF_LEN	(128*1024)
250906eeb2adSek 
251006eeb2adSek /*
251106eeb2adSek  * Retrieve the command history of a pool.
251206eeb2adSek  */
251306eeb2adSek int
251406eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
251506eeb2adSek {
251606eeb2adSek 	char buf[HIS_BUF_LEN];
251706eeb2adSek 	uint64_t off = 0;
251806eeb2adSek 	nvlist_t **records = NULL;
251906eeb2adSek 	uint_t numrecords = 0;
252006eeb2adSek 	int err, i;
252106eeb2adSek 
252206eeb2adSek 	do {
252306eeb2adSek 		uint64_t bytes_read = sizeof (buf);
252406eeb2adSek 		uint64_t leftover;
252506eeb2adSek 
252606eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
252706eeb2adSek 			break;
252806eeb2adSek 
252906eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
253006eeb2adSek 		if (!bytes_read)
253106eeb2adSek 			break;
253206eeb2adSek 
253306eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
253406eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
253506eeb2adSek 			break;
253606eeb2adSek 		off -= leftover;
253706eeb2adSek 
253806eeb2adSek 		/* CONSTCOND */
253906eeb2adSek 	} while (1);
254006eeb2adSek 
254106eeb2adSek 	if (!err) {
254206eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
254306eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
254406eeb2adSek 		    records, numrecords) == 0);
254506eeb2adSek 	}
254606eeb2adSek 	for (i = 0; i < numrecords; i++)
254706eeb2adSek 		nvlist_free(records[i]);
254806eeb2adSek 	free(records);
254906eeb2adSek 
255006eeb2adSek 	return (err);
255106eeb2adSek }
255255434c77Sek 
255355434c77Sek void
255455434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
255555434c77Sek     char *pathname, size_t len)
255655434c77Sek {
255755434c77Sek 	zfs_cmd_t zc = { 0 };
255855434c77Sek 	boolean_t mounted = B_FALSE;
255955434c77Sek 	char *mntpnt = NULL;
256055434c77Sek 	char dsname[MAXNAMELEN];
256155434c77Sek 
256255434c77Sek 	if (dsobj == 0) {
256355434c77Sek 		/* special case for the MOS */
256455434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
256555434c77Sek 		return;
256655434c77Sek 	}
256755434c77Sek 
256855434c77Sek 	/* get the dataset's name */
256955434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
257055434c77Sek 	zc.zc_obj = dsobj;
257155434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
257255434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
257355434c77Sek 		/* just write out a path of two object numbers */
257455434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
257555434c77Sek 		    dsobj, obj);
257655434c77Sek 		return;
257755434c77Sek 	}
257855434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
257955434c77Sek 
258055434c77Sek 	/* find out if the dataset is mounted */
258155434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
258255434c77Sek 
258355434c77Sek 	/* get the corrupted object's path */
258455434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
258555434c77Sek 	zc.zc_obj = obj;
258655434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
258755434c77Sek 	    &zc) == 0) {
258855434c77Sek 		if (mounted) {
258955434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
259055434c77Sek 			    zc.zc_value);
259155434c77Sek 		} else {
259255434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
259355434c77Sek 			    dsname, zc.zc_value);
259455434c77Sek 		}
259555434c77Sek 	} else {
259655434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
259755434c77Sek 	}
259855434c77Sek 	free(mntpnt);
259955434c77Sek }
2600b1b8ab34Slling 
26018488aeb5Staylor #define	RDISK_ROOT	"/dev/rdsk"
26028488aeb5Staylor #define	BACKUP_SLICE	"s2"
26038488aeb5Staylor /*
26048488aeb5Staylor  * Don't start the slice at the default block of 34; many storage
26058488aeb5Staylor  * devices will use a stripe width of 128k, so start there instead.
26068488aeb5Staylor  */
26078488aeb5Staylor #define	NEW_START_BLOCK	256
26088488aeb5Staylor 
260915e6edf1Sgw /*
261015e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
261115e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
261215e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
261315e6edf1Sgw  * partition.
261415e6edf1Sgw  */
261515e6edf1Sgw static int
261615e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb)
261715e6edf1Sgw {
261815e6edf1Sgw 	char *path;
261915e6edf1Sgw 	int fd;
262015e6edf1Sgw 	char diskname[MAXPATHLEN];
262115e6edf1Sgw 	int err = -1;
262215e6edf1Sgw 
262315e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
262415e6edf1Sgw 		return (err);
262515e6edf1Sgw 
262615e6edf1Sgw 	(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
262715e6edf1Sgw 	    strrchr(path, '/'));
262815e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
262915e6edf1Sgw 		struct dk_gpt *vtoc;
263015e6edf1Sgw 
263115e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
263215e6edf1Sgw 			if (sb != NULL)
263315e6edf1Sgw 				*sb = vtoc->efi_parts[0].p_start;
263415e6edf1Sgw 			efi_free(vtoc);
263515e6edf1Sgw 		}
263615e6edf1Sgw 		(void) close(fd);
263715e6edf1Sgw 	}
263815e6edf1Sgw 	return (err);
263915e6edf1Sgw }
264015e6edf1Sgw 
26418488aeb5Staylor /*
26428488aeb5Staylor  * determine where a partition starts on a disk in the current
26438488aeb5Staylor  * configuration
26448488aeb5Staylor  */
26458488aeb5Staylor static diskaddr_t
26468488aeb5Staylor find_start_block(nvlist_t *config)
26478488aeb5Staylor {
26488488aeb5Staylor 	nvlist_t **child;
26498488aeb5Staylor 	uint_t c, children;
26508488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
26518488aeb5Staylor 	uint64_t wholedisk;
26528488aeb5Staylor 
26538488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
26548488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
26558488aeb5Staylor 		if (nvlist_lookup_uint64(config,
26568488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
26578488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
26588488aeb5Staylor 			return (MAXOFFSET_T);
26598488aeb5Staylor 		}
266015e6edf1Sgw 		if (read_efi_label(config, &sb) < 0)
266115e6edf1Sgw 			sb = MAXOFFSET_T;
26628488aeb5Staylor 		return (sb);
26638488aeb5Staylor 	}
26648488aeb5Staylor 
26658488aeb5Staylor 	for (c = 0; c < children; c++) {
26668488aeb5Staylor 		sb = find_start_block(child[c]);
26678488aeb5Staylor 		if (sb != MAXOFFSET_T) {
26688488aeb5Staylor 			return (sb);
26698488aeb5Staylor 		}
26708488aeb5Staylor 	}
26718488aeb5Staylor 	return (MAXOFFSET_T);
26728488aeb5Staylor }
26738488aeb5Staylor 
26748488aeb5Staylor /*
26758488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
26768488aeb5Staylor  * stripped of any leading /dev path.
26778488aeb5Staylor  */
26788488aeb5Staylor int
26798488aeb5Staylor zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
26808488aeb5Staylor {
26818488aeb5Staylor 	char path[MAXPATHLEN];
26828488aeb5Staylor 	struct dk_gpt *vtoc;
26838488aeb5Staylor 	int fd;
26848488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
26858488aeb5Staylor 	uint64_t slice_size;
26868488aeb5Staylor 	diskaddr_t start_block;
26878488aeb5Staylor 	char errbuf[1024];
26888488aeb5Staylor 
2689c6ef114fSmmusante 	/* prepare an error message just in case */
2690c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
2691c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2692c6ef114fSmmusante 
26938488aeb5Staylor 	if (zhp) {
26948488aeb5Staylor 		nvlist_t *nvroot;
26958488aeb5Staylor 
26968488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
26978488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
26988488aeb5Staylor 
26998488aeb5Staylor 		if (zhp->zpool_start_block == 0)
27008488aeb5Staylor 			start_block = find_start_block(nvroot);
27018488aeb5Staylor 		else
27028488aeb5Staylor 			start_block = zhp->zpool_start_block;
27038488aeb5Staylor 		zhp->zpool_start_block = start_block;
27048488aeb5Staylor 	} else {
27058488aeb5Staylor 		/* new pool */
27068488aeb5Staylor 		start_block = NEW_START_BLOCK;
27078488aeb5Staylor 	}
27088488aeb5Staylor 
27098488aeb5Staylor 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
27108488aeb5Staylor 	    BACKUP_SLICE);
27118488aeb5Staylor 
27128488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
27138488aeb5Staylor 		/*
27148488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
27158488aeb5Staylor 		 * is a valid device.
27168488aeb5Staylor 		 */
2717c6ef114fSmmusante 		zfs_error_aux(hdl,
2718c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
27198488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
27208488aeb5Staylor 	}
27218488aeb5Staylor 
27228488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
27238488aeb5Staylor 		/*
27248488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
27258488aeb5Staylor 		 * were unable to read the disk's capacity
27268488aeb5Staylor 		 */
27278488aeb5Staylor 		if (errno == ENOMEM)
27288488aeb5Staylor 			(void) no_memory(hdl);
27298488aeb5Staylor 
27308488aeb5Staylor 		(void) close(fd);
2731c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2732c6ef114fSmmusante 		    "unable to read disk capacity"), name);
27338488aeb5Staylor 
27348488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
27358488aeb5Staylor 	}
27368488aeb5Staylor 
27378488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
27388488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
27398488aeb5Staylor 	if (start_block == MAXOFFSET_T)
27408488aeb5Staylor 		start_block = NEW_START_BLOCK;
27418488aeb5Staylor 	slice_size -= start_block;
27428488aeb5Staylor 
27438488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
27448488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
27458488aeb5Staylor 
27468488aeb5Staylor 	/*
27478488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
27488488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
27498488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
27508488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
27518488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
27528488aeb5Staylor 	 * can get, in the absence of V_OTHER.
27538488aeb5Staylor 	 */
27548488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
27558488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
27568488aeb5Staylor 
27578488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
27588488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
27598488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
27608488aeb5Staylor 
27618488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
27628488aeb5Staylor 		/*
27638488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
27648488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
27658488aeb5Staylor 		 * ecting the user to manually label the disk and give
27668488aeb5Staylor 		 * a specific slice.
27678488aeb5Staylor 		 */
27688488aeb5Staylor 		(void) close(fd);
27698488aeb5Staylor 		efi_free(vtoc);
27708488aeb5Staylor 
27718488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2772c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
27738488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
27748488aeb5Staylor 	}
27758488aeb5Staylor 
27768488aeb5Staylor 	(void) close(fd);
27778488aeb5Staylor 	efi_free(vtoc);
27788488aeb5Staylor 	return (0);
27798488aeb5Staylor }
2780e7cbe64fSgw 
2781e7cbe64fSgw static boolean_t
2782e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
2783e7cbe64fSgw {
2784e7cbe64fSgw 	char *type;
2785e7cbe64fSgw 	nvlist_t **child;
2786e7cbe64fSgw 	uint_t children, c;
2787e7cbe64fSgw 
2788e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
2789e7cbe64fSgw 	if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2790e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_FILE) == 0 ||
2791e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_LOG) == 0 ||
2792e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
2793e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2794e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
2795e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
2796e7cbe64fSgw 		return (B_FALSE);
2797e7cbe64fSgw 	}
2798e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
2799e7cbe64fSgw 	    &child, &children) == 0) {
2800e7cbe64fSgw 		for (c = 0; c < children; c++) {
2801e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
2802e7cbe64fSgw 				return (B_FALSE);
2803e7cbe64fSgw 		}
2804e7cbe64fSgw 	}
2805e7cbe64fSgw 	return (B_TRUE);
2806e7cbe64fSgw }
2807e7cbe64fSgw 
2808e7cbe64fSgw /*
2809e7cbe64fSgw  * check if this zvol is allowable for use as a dump device; zero if
2810e7cbe64fSgw  * it is, > 0 if it isn't, < 0 if it isn't a zvol
2811e7cbe64fSgw  */
2812e7cbe64fSgw int
2813e7cbe64fSgw zvol_check_dump_config(char *arg)
2814e7cbe64fSgw {
2815e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
2816e7cbe64fSgw 	nvlist_t *config, *nvroot;
2817e7cbe64fSgw 	char *p, *volname;
2818e7cbe64fSgw 	nvlist_t **top;
2819e7cbe64fSgw 	uint_t toplevels;
2820e7cbe64fSgw 	libzfs_handle_t *hdl;
2821e7cbe64fSgw 	char errbuf[1024];
2822e7cbe64fSgw 	char poolname[ZPOOL_MAXNAMELEN];
2823e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
2824e7cbe64fSgw 	int ret = 1;
2825e7cbe64fSgw 
2826e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
2827e7cbe64fSgw 		return (-1);
2828e7cbe64fSgw 	}
2829e7cbe64fSgw 
2830e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2831e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
2832e7cbe64fSgw 
2833e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
2834e7cbe64fSgw 		return (1);
2835e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
2836e7cbe64fSgw 
2837e7cbe64fSgw 	volname = arg + pathlen;
2838e7cbe64fSgw 
2839e7cbe64fSgw 	/* check the configuration of the pool */
2840e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
2841e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2842e7cbe64fSgw 		    "malformed dataset name"));
2843e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
2844e7cbe64fSgw 		return (1);
2845e7cbe64fSgw 	} else if (p - volname >= ZFS_MAXNAMELEN) {
2846e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2847e7cbe64fSgw 		    "dataset name is too long"));
2848e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
2849e7cbe64fSgw 		return (1);
2850e7cbe64fSgw 	} else {
2851e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
2852e7cbe64fSgw 		poolname[p - volname] = '\0';
2853e7cbe64fSgw 	}
2854e7cbe64fSgw 
2855e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
2856e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2857e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
2858e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
2859e7cbe64fSgw 		goto out;
2860e7cbe64fSgw 	}
2861e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
2862e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2863e7cbe64fSgw 	    &nvroot) != 0) {
2864e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2865e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
2866e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
2867e7cbe64fSgw 		goto out;
2868e7cbe64fSgw 	}
2869e7cbe64fSgw 
2870e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2871e7cbe64fSgw 	    &top, &toplevels) == 0);
2872e7cbe64fSgw 	if (toplevels != 1) {
2873e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2874e7cbe64fSgw 		    "'%s' has multiple top level vdevs"), poolname);
2875e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
2876e7cbe64fSgw 		goto out;
2877e7cbe64fSgw 	}
2878e7cbe64fSgw 
2879e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
2880e7cbe64fSgw 		goto out;
2881e7cbe64fSgw 	}
2882e7cbe64fSgw 	ret = 0;
2883e7cbe64fSgw 
2884e7cbe64fSgw out:
2885e7cbe64fSgw 	if (zhp)
2886e7cbe64fSgw 		zpool_close(zhp);
2887e7cbe64fSgw 	libzfs_fini(hdl);
2888e7cbe64fSgw 	return (ret);
2889e7cbe64fSgw }
2890