1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25810e43b2SBill Pijewski  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
266401734dSWill Andrews  * Copyright 2016 Nexenta Systems, Inc.
2788f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
281702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <ctype.h>
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <devid.h>
34fa9e4066Sahrens #include <fcntl.h>
35fa9e4066Sahrens #include <libintl.h>
36fa9e4066Sahrens #include <stdio.h>
37fa9e4066Sahrens #include <stdlib.h>
38f3861e1aSahl #include <strings.h>
39fa9e4066Sahrens #include <unistd.h>
404445fffbSMatthew Ahrens #include <libgen.h>
418488aeb5Staylor #include <sys/efi_partition.h>
428488aeb5Staylor #include <sys/vtoc.h>
43fa9e4066Sahrens #include <sys/zfs_ioctl.h>
44573ca77eSGeorge Wilson #include <dlfcn.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens #include "zfs_namecheck.h"
47b1b8ab34Slling #include "zfs_prop.h"
48fa9e4066Sahrens #include "libzfs_impl.h"
49468c413aSTim Haley #include "zfs_comutil.h"
50ad135b5dSChristopher Siden #include "zfeature_common.h"
51fa9e4066Sahrens 
527855d95bSToomas Soome static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
53*2ba5f978SAlan Somers static boolean_t zpool_vdev_is_interior(const char *name);
54990b4856Slling 
55573ca77eSGeorge Wilson #define	BACKUP_SLICE	"s2"
56573ca77eSGeorge Wilson 
57f9af39baSGeorge Wilson typedef struct prop_flags {
58f9af39baSGeorge Wilson 	int create:1;	/* Validate property on creation */
59f9af39baSGeorge Wilson 	int import:1;	/* Validate property on import */
60f9af39baSGeorge Wilson } prop_flags_t;
61f9af39baSGeorge Wilson 
62990b4856Slling /*
63990b4856Slling  * ====================================================================
64990b4856Slling  *   zpool property functions
65990b4856Slling  * ====================================================================
66990b4856Slling  */
67990b4856Slling 
68990b4856Slling static int
69990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
70990b4856Slling {
71990b4856Slling 	zfs_cmd_t zc = { 0 };
72990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
73990b4856Slling 
74990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
75990b4856Slling 
76990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
77990b4856Slling 		return (-1);
78990b4856Slling 
79990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
80990b4856Slling 		if (errno == ENOMEM) {
81990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
82990b4856Slling 				zcmd_free_nvlists(&zc);
83990b4856Slling 				return (-1);
84990b4856Slling 			}
85990b4856Slling 		} else {
86990b4856Slling 			zcmd_free_nvlists(&zc);
87990b4856Slling 			return (-1);
88990b4856Slling 		}
89990b4856Slling 	}
90990b4856Slling 
91990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
92990b4856Slling 		zcmd_free_nvlists(&zc);
93990b4856Slling 		return (-1);
94990b4856Slling 	}
95990b4856Slling 
96990b4856Slling 	zcmd_free_nvlists(&zc);
97990b4856Slling 
98990b4856Slling 	return (0);
99990b4856Slling }
100990b4856Slling 
101990b4856Slling static int
102990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
103990b4856Slling {
104990b4856Slling 	nvlist_t *old_props;
105990b4856Slling 
106990b4856Slling 	old_props = zhp->zpool_props;
107990b4856Slling 
108990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
109990b4856Slling 		return (-1);
110990b4856Slling 
111990b4856Slling 	nvlist_free(old_props);
112990b4856Slling 	return (0);
113990b4856Slling }
114990b4856Slling 
115990b4856Slling static char *
116990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
117990b4856Slling     zprop_source_t *src)
118990b4856Slling {
119990b4856Slling 	nvlist_t *nv, *nvl;
120990b4856Slling 	uint64_t ival;
121990b4856Slling 	char *value;
122990b4856Slling 	zprop_source_t source;
123990b4856Slling 
124990b4856Slling 	nvl = zhp->zpool_props;
125990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
126990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
127990b4856Slling 		source = ival;
128990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
129990b4856Slling 	} else {
130990b4856Slling 		source = ZPROP_SRC_DEFAULT;
131990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
132990b4856Slling 			value = "-";
133990b4856Slling 	}
134990b4856Slling 
135990b4856Slling 	if (src)
136990b4856Slling 		*src = source;
137990b4856Slling 
138990b4856Slling 	return (value);
139990b4856Slling }
140990b4856Slling 
141990b4856Slling uint64_t
142990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
143990b4856Slling {
144990b4856Slling 	nvlist_t *nv, *nvl;
145990b4856Slling 	uint64_t value;
146990b4856Slling 	zprop_source_t source;
147990b4856Slling 
148b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
149b87f3af3Sperrin 		/*
150b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
151b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
152b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
153b87f3af3Sperrin 		 */
154b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
155b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
156b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
157b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
158b87f3af3Sperrin 		    == 0)) {
159b87f3af3Sperrin 			return (value);
160b87f3af3Sperrin 		}
161990b4856Slling 		return (zpool_prop_default_numeric(prop));
162b87f3af3Sperrin 	}
163990b4856Slling 
164990b4856Slling 	nvl = zhp->zpool_props;
165990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
166990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
167990b4856Slling 		source = value;
168990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
169990b4856Slling 	} else {
170990b4856Slling 		source = ZPROP_SRC_DEFAULT;
171990b4856Slling 		value = zpool_prop_default_numeric(prop);
172990b4856Slling 	}
173990b4856Slling 
174990b4856Slling 	if (src)
175990b4856Slling 		*src = source;
176990b4856Slling 
177990b4856Slling 	return (value);
178990b4856Slling }
179990b4856Slling 
180990b4856Slling /*
181990b4856Slling  * Map VDEV STATE to printed strings.
182990b4856Slling  */
1836401734dSWill Andrews const char *
184990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
185990b4856Slling {
186990b4856Slling 	switch (state) {
187990b4856Slling 	case VDEV_STATE_CLOSED:
188990b4856Slling 	case VDEV_STATE_OFFLINE:
189990b4856Slling 		return (gettext("OFFLINE"));
190990b4856Slling 	case VDEV_STATE_REMOVED:
191990b4856Slling 		return (gettext("REMOVED"));
192990b4856Slling 	case VDEV_STATE_CANT_OPEN:
193b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
194990b4856Slling 			return (gettext("FAULTED"));
1951195e687SMark J Musante 		else if (aux == VDEV_AUX_SPLIT_POOL)
1961195e687SMark J Musante 			return (gettext("SPLIT"));
197990b4856Slling 		else
198990b4856Slling 			return (gettext("UNAVAIL"));
199990b4856Slling 	case VDEV_STATE_FAULTED:
200990b4856Slling 		return (gettext("FAULTED"));
201990b4856Slling 	case VDEV_STATE_DEGRADED:
202990b4856Slling 		return (gettext("DEGRADED"));
203990b4856Slling 	case VDEV_STATE_HEALTHY:
204990b4856Slling 		return (gettext("ONLINE"));
20588f61deeSIgor Kozhukhov 
20688f61deeSIgor Kozhukhov 	default:
20788f61deeSIgor Kozhukhov 		break;
208990b4856Slling 	}
209990b4856Slling 
210990b4856Slling 	return (gettext("UNKNOWN"));
211990b4856Slling }
212990b4856Slling 
2136401734dSWill Andrews /*
2146401734dSWill Andrews  * Map POOL STATE to printed strings.
2156401734dSWill Andrews  */
2166401734dSWill Andrews const char *
2176401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state)
2186401734dSWill Andrews {
2196401734dSWill Andrews 	switch (state) {
2206401734dSWill Andrews 	case POOL_STATE_ACTIVE:
2216401734dSWill Andrews 		return (gettext("ACTIVE"));
2226401734dSWill Andrews 	case POOL_STATE_EXPORTED:
2236401734dSWill Andrews 		return (gettext("EXPORTED"));
2246401734dSWill Andrews 	case POOL_STATE_DESTROYED:
2256401734dSWill Andrews 		return (gettext("DESTROYED"));
2266401734dSWill Andrews 	case POOL_STATE_SPARE:
2276401734dSWill Andrews 		return (gettext("SPARE"));
2286401734dSWill Andrews 	case POOL_STATE_L2CACHE:
2296401734dSWill Andrews 		return (gettext("L2CACHE"));
2306401734dSWill Andrews 	case POOL_STATE_UNINITIALIZED:
2316401734dSWill Andrews 		return (gettext("UNINITIALIZED"));
2326401734dSWill Andrews 	case POOL_STATE_UNAVAIL:
2336401734dSWill Andrews 		return (gettext("UNAVAIL"));
2346401734dSWill Andrews 	case POOL_STATE_POTENTIALLY_ACTIVE:
2356401734dSWill Andrews 		return (gettext("POTENTIALLY_ACTIVE"));
2366401734dSWill Andrews 	}
2376401734dSWill Andrews 
2386401734dSWill Andrews 	return (gettext("UNKNOWN"));
2396401734dSWill Andrews }
2406401734dSWill Andrews 
241990b4856Slling /*
242990b4856Slling  * Get a zpool property value for 'prop' and return the value in
243990b4856Slling  * a pre-allocated buffer.
244990b4856Slling  */
245990b4856Slling int
246990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
247c58b3526SAdam Stevko     zprop_source_t *srctype, boolean_t literal)
248990b4856Slling {
249990b4856Slling 	uint64_t intval;
250990b4856Slling 	const char *strval;
251990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
252990b4856Slling 	nvlist_t *nvroot;
253990b4856Slling 	vdev_stat_t *vs;
254990b4856Slling 	uint_t vsc;
255990b4856Slling 
256990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
257379c004dSEric Schrock 		switch (prop) {
258379c004dSEric Schrock 		case ZPOOL_PROP_NAME:
259990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
260379c004dSEric Schrock 			break;
261379c004dSEric Schrock 
262379c004dSEric Schrock 		case ZPOOL_PROP_HEALTH:
263990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
264379c004dSEric Schrock 			break;
265379c004dSEric Schrock 
266379c004dSEric Schrock 		case ZPOOL_PROP_GUID:
267379c004dSEric Schrock 			intval = zpool_get_prop_int(zhp, prop, &src);
268379c004dSEric Schrock 			(void) snprintf(buf, len, "%llu", intval);
269379c004dSEric Schrock 			break;
270379c004dSEric Schrock 
271379c004dSEric Schrock 		case ZPOOL_PROP_ALTROOT:
272379c004dSEric Schrock 		case ZPOOL_PROP_CACHEFILE:
2738704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
274379c004dSEric Schrock 			if (zhp->zpool_props != NULL ||
275379c004dSEric Schrock 			    zpool_get_all_props(zhp) == 0) {
276379c004dSEric Schrock 				(void) strlcpy(buf,
277379c004dSEric Schrock 				    zpool_get_prop_string(zhp, prop, &src),
278379c004dSEric Schrock 				    len);
279c58b3526SAdam Stevko 				break;
280379c004dSEric Schrock 			}
281379c004dSEric Schrock 			/* FALLTHROUGH */
282379c004dSEric Schrock 		default:
283990b4856Slling 			(void) strlcpy(buf, "-", len);
284379c004dSEric Schrock 			break;
285379c004dSEric Schrock 		}
286379c004dSEric Schrock 
287379c004dSEric Schrock 		if (srctype != NULL)
288379c004dSEric Schrock 			*srctype = src;
289990b4856Slling 		return (0);
290990b4856Slling 	}
291990b4856Slling 
292990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
293990b4856Slling 	    prop != ZPOOL_PROP_NAME)
294990b4856Slling 		return (-1);
295990b4856Slling 
296990b4856Slling 	switch (zpool_prop_get_type(prop)) {
297990b4856Slling 	case PROP_TYPE_STRING:
298990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
299990b4856Slling 		    len);
300990b4856Slling 		break;
301990b4856Slling 
302990b4856Slling 	case PROP_TYPE_NUMBER:
303990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
304990b4856Slling 
305990b4856Slling 		switch (prop) {
306990b4856Slling 		case ZPOOL_PROP_SIZE:
307485bbbf5SGeorge Wilson 		case ZPOOL_PROP_ALLOCATED:
308485bbbf5SGeorge Wilson 		case ZPOOL_PROP_FREE:
309ad135b5dSChristopher Siden 		case ZPOOL_PROP_FREEING:
3107fd05ac4SMatthew Ahrens 		case ZPOOL_PROP_LEAKED:
311c58b3526SAdam Stevko 			if (literal) {
312c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
313c58b3526SAdam Stevko 				    (u_longlong_t)intval);
314c58b3526SAdam Stevko 			} else {
315c58b3526SAdam Stevko 				(void) zfs_nicenum(intval, buf, len);
316c58b3526SAdam Stevko 			}
317990b4856Slling 			break;
3187855d95bSToomas Soome 		case ZPOOL_PROP_BOOTSIZE:
3197a09f97bSGeorge Wilson 		case ZPOOL_PROP_EXPANDSZ:
3207a09f97bSGeorge Wilson 			if (intval == 0) {
3217a09f97bSGeorge Wilson 				(void) strlcpy(buf, "-", len);
3227a09f97bSGeorge Wilson 			} else if (literal) {
3237a09f97bSGeorge Wilson 				(void) snprintf(buf, len, "%llu",
3247a09f97bSGeorge Wilson 				    (u_longlong_t)intval);
3257a09f97bSGeorge Wilson 			} else {
3267a09f97bSGeorge Wilson 				(void) zfs_nicenum(intval, buf, len);
3277a09f97bSGeorge Wilson 			}
3287a09f97bSGeorge Wilson 			break;
329990b4856Slling 		case ZPOOL_PROP_CAPACITY:
330c58b3526SAdam Stevko 			if (literal) {
331c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
332c58b3526SAdam Stevko 				    (u_longlong_t)intval);
333c58b3526SAdam Stevko 			} else {
334c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu%%",
335c58b3526SAdam Stevko 				    (u_longlong_t)intval);
336c58b3526SAdam Stevko 			}
337990b4856Slling 			break;
3382e4c9986SGeorge Wilson 		case ZPOOL_PROP_FRAGMENTATION:
3392e4c9986SGeorge Wilson 			if (intval == UINT64_MAX) {
3402e4c9986SGeorge Wilson 				(void) strlcpy(buf, "-", len);
3412e4c9986SGeorge Wilson 			} else {
3422e4c9986SGeorge Wilson 				(void) snprintf(buf, len, "%llu%%",
3432e4c9986SGeorge Wilson 				    (u_longlong_t)intval);
3442e4c9986SGeorge Wilson 			}
3452e4c9986SGeorge Wilson 			break;
346b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
347b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
348b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
349b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
350b24ab676SJeff Bonwick 			break;
351990b4856Slling 		case ZPOOL_PROP_HEALTH:
352990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
353990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
354990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
3553f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3563f9d6ad7SLin Ling 			    == 0);
357990b4856Slling 
358990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
359990b4856Slling 			    vs->vs_aux), len);
360990b4856Slling 			break;
361ad135b5dSChristopher Siden 		case ZPOOL_PROP_VERSION:
362ad135b5dSChristopher Siden 			if (intval >= SPA_VERSION_FEATURES) {
363ad135b5dSChristopher Siden 				(void) snprintf(buf, len, "-");
364ad135b5dSChristopher Siden 				break;
365ad135b5dSChristopher Siden 			}
366ad135b5dSChristopher Siden 			/* FALLTHROUGH */
367990b4856Slling 		default:
368990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
369990b4856Slling 		}
370990b4856Slling 		break;
371990b4856Slling 
372990b4856Slling 	case PROP_TYPE_INDEX:
373990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
374990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
375990b4856Slling 		    != 0)
376990b4856Slling 			return (-1);
377990b4856Slling 		(void) strlcpy(buf, strval, len);
378990b4856Slling 		break;
379990b4856Slling 
380990b4856Slling 	default:
381990b4856Slling 		abort();
382990b4856Slling 	}
383990b4856Slling 
384990b4856Slling 	if (srctype)
385990b4856Slling 		*srctype = src;
386990b4856Slling 
387990b4856Slling 	return (0);
388990b4856Slling }
389990b4856Slling 
390990b4856Slling /*
391990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
392990b4856Slling  * Assuming bootfs is a valid dataset name.
393990b4856Slling  */
394990b4856Slling static boolean_t
395990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
396990b4856Slling {
397990b4856Slling 	int len = strlen(pool);
398990b4856Slling 
399fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
400990b4856Slling 		return (B_FALSE);
401990b4856Slling 
402990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
403990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
404990b4856Slling 		return (B_TRUE);
405990b4856Slling 
406990b4856Slling 	return (B_FALSE);
407990b4856Slling }
408990b4856Slling 
4094263d13fSGeorge Wilson boolean_t
4104263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
411b5b76fecSGeorge Wilson {
4129adfa60dSMatthew Ahrens 	char bootfs[ZFS_MAX_DATASET_NAME_LEN];
413b5b76fecSGeorge Wilson 
414b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
415c58b3526SAdam Stevko 	    sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
416b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
417b5b76fecSGeorge Wilson }
418b5b76fecSGeorge Wilson 
419b5b76fecSGeorge Wilson 
420990b4856Slling /*
421990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
422990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
423990b4856Slling  * specified as strings.
424990b4856Slling  */
425990b4856Slling static nvlist_t *
4260a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
427f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
428990b4856Slling {
429990b4856Slling 	nvpair_t *elem;
430990b4856Slling 	nvlist_t *retprops;
431990b4856Slling 	zpool_prop_t prop;
432990b4856Slling 	char *strval;
433990b4856Slling 	uint64_t intval;
4348704186eSDan McDonald 	char *slash, *check;
4352f8aaab3Seschrock 	struct stat64 statbuf;
43615e6edf1Sgw 	zpool_handle_t *zhp;
437990b4856Slling 
438990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
439990b4856Slling 		(void) no_memory(hdl);
440990b4856Slling 		return (NULL);
441990b4856Slling 	}
442990b4856Slling 
443990b4856Slling 	elem = NULL;
444990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
445990b4856Slling 		const char *propname = nvpair_name(elem);
446990b4856Slling 
447ad135b5dSChristopher Siden 		prop = zpool_name_to_prop(propname);
448ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
449ad135b5dSChristopher Siden 			int err;
450ad135b5dSChristopher Siden 			char *fname = strchr(propname, '@') + 1;
451ad135b5dSChristopher Siden 
4522acef22dSMatthew Ahrens 			err = zfeature_lookup_name(fname, NULL);
453ad135b5dSChristopher Siden 			if (err != 0) {
454ad135b5dSChristopher Siden 				ASSERT3U(err, ==, ENOENT);
455ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
456ad135b5dSChristopher Siden 				    "invalid feature '%s'"), fname);
457ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
458ad135b5dSChristopher Siden 				goto error;
459ad135b5dSChristopher Siden 			}
460ad135b5dSChristopher Siden 
461ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
462ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
463ad135b5dSChristopher Siden 				    "'%s' must be a string"), propname);
464ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
465ad135b5dSChristopher Siden 				goto error;
466ad135b5dSChristopher Siden 			}
467ad135b5dSChristopher Siden 
468ad135b5dSChristopher Siden 			(void) nvpair_value_string(elem, &strval);
469ad135b5dSChristopher Siden 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
470ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
471ad135b5dSChristopher Siden 				    "property '%s' can only be set to "
472ad135b5dSChristopher Siden 				    "'enabled'"), propname);
473ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
474ad135b5dSChristopher Siden 				goto error;
475ad135b5dSChristopher Siden 			}
476ad135b5dSChristopher Siden 
477ad135b5dSChristopher Siden 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
478ad135b5dSChristopher Siden 				(void) no_memory(hdl);
479ad135b5dSChristopher Siden 				goto error;
480ad135b5dSChristopher Siden 			}
481ad135b5dSChristopher Siden 			continue;
482ad135b5dSChristopher Siden 		}
483ad135b5dSChristopher Siden 
484990b4856Slling 		/*
485990b4856Slling 		 * Make sure this property is valid and applies to this type.
486990b4856Slling 		 */
487ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL) {
488990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
489990b4856Slling 			    "invalid property '%s'"), propname);
490990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
491990b4856Slling 			goto error;
492990b4856Slling 		}
493990b4856Slling 
494990b4856Slling 		if (zpool_prop_readonly(prop)) {
495990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
496990b4856Slling 			    "is readonly"), propname);
497990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
498990b4856Slling 			goto error;
499990b4856Slling 		}
500990b4856Slling 
501990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
502990b4856Slling 		    &strval, &intval, errbuf) != 0)
503990b4856Slling 			goto error;
504990b4856Slling 
505990b4856Slling 		/*
506990b4856Slling 		 * Perform additional checking for specific properties.
507990b4856Slling 		 */
508990b4856Slling 		switch (prop) {
509990b4856Slling 		case ZPOOL_PROP_VERSION:
510ad135b5dSChristopher Siden 			if (intval < version ||
511ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
512990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513990b4856Slling 				    "property '%s' number %d is invalid."),
514990b4856Slling 				    propname, intval);
515990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
516990b4856Slling 				goto error;
517990b4856Slling 			}
518990b4856Slling 			break;
519990b4856Slling 
5207855d95bSToomas Soome 		case ZPOOL_PROP_BOOTSIZE:
5217855d95bSToomas Soome 			if (!flags.create) {
5227855d95bSToomas Soome 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5237855d95bSToomas Soome 				    "property '%s' can only be set during pool "
5247855d95bSToomas Soome 				    "creation"), propname);
5257855d95bSToomas Soome 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5267855d95bSToomas Soome 				goto error;
5277855d95bSToomas Soome 			}
5287855d95bSToomas Soome 			break;
5297855d95bSToomas Soome 
530990b4856Slling 		case ZPOOL_PROP_BOOTFS:
531f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
532990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533990b4856Slling 				    "property '%s' cannot be set at creation "
534990b4856Slling 				    "or import time"), propname);
535990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
536990b4856Slling 				goto error;
537990b4856Slling 			}
538990b4856Slling 
539990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
540990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541990b4856Slling 				    "pool must be upgraded to support "
542990b4856Slling 				    "'%s' property"), propname);
543990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
544990b4856Slling 				goto error;
545990b4856Slling 			}
546990b4856Slling 
547990b4856Slling 			/*
548990b4856Slling 			 * bootfs property value has to be a dataset name and
549990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
550990b4856Slling 			 */
551990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
552990b4856Slling 			    strval)) {
553990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
554990b4856Slling 				    "is an invalid name"), strval);
555990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
556990b4856Slling 				goto error;
557990b4856Slling 			}
55815e6edf1Sgw 
55915e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
56015e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
56115e6edf1Sgw 				    "could not open pool '%s'"), poolname);
56215e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
56315e6edf1Sgw 				goto error;
56415e6edf1Sgw 			}
56515e6edf1Sgw 			zpool_close(zhp);
566990b4856Slling 			break;
567990b4856Slling 
5682f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
569f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
570990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
571990b4856Slling 				    "property '%s' can only be set during pool "
572990b4856Slling 				    "creation or import"), propname);
573990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
574990b4856Slling 				goto error;
575990b4856Slling 			}
576990b4856Slling 
5772f8aaab3Seschrock 			if (strval[0] != '/') {
578990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5792f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
5802f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
581990b4856Slling 				goto error;
582990b4856Slling 			}
5832f8aaab3Seschrock 			break;
5842f8aaab3Seschrock 
5852f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5862f8aaab3Seschrock 			if (strval[0] == '\0')
5872f8aaab3Seschrock 				break;
5882f8aaab3Seschrock 
5892f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5902f8aaab3Seschrock 				break;
591990b4856Slling 
592990b4856Slling 			if (strval[0] != '/') {
593990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5942f8aaab3Seschrock 				    "property '%s' must be empty, an "
5952f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
596990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
597990b4856Slling 				goto error;
598990b4856Slling 			}
599990b4856Slling 
6002f8aaab3Seschrock 			slash = strrchr(strval, '/');
601990b4856Slling 
6022f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
6032f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
6042f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6052f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
6062f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
6072f8aaab3Seschrock 				goto error;
6082f8aaab3Seschrock 			}
609990b4856Slling 
6102f8aaab3Seschrock 			*slash = '\0';
6112f8aaab3Seschrock 
6122c32020fSeschrock 			if (strval[0] != '\0' &&
6132c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
6142c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
6152f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6162f8aaab3Seschrock 				    "'%s' is not a valid directory"),
6172f8aaab3Seschrock 				    strval);
6182f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
6192f8aaab3Seschrock 				goto error;
6202f8aaab3Seschrock 			}
6212f8aaab3Seschrock 
6222f8aaab3Seschrock 			*slash = '/';
6232f8aaab3Seschrock 			break;
624f9af39baSGeorge Wilson 
6258704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
6268704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
6278704186eSDan McDonald 				if (!isprint(*check)) {
6288704186eSDan McDonald 					zfs_error_aux(hdl,
6298704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
6308704186eSDan McDonald 					    "comment may only have printable "
6318704186eSDan McDonald 					    "characters"));
6328704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
6338704186eSDan McDonald 					    errbuf);
6348704186eSDan McDonald 					goto error;
6358704186eSDan McDonald 				}
6368704186eSDan McDonald 			}
6378704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
6388704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6398704186eSDan McDonald 				    "comment must not exceed %d characters"),
6408704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
6418704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6428704186eSDan McDonald 				goto error;
6438704186eSDan McDonald 			}
6448704186eSDan McDonald 			break;
645f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
646f9af39baSGeorge Wilson 			if (!flags.import) {
647f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
648f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
649f9af39baSGeorge Wilson 				    "import time"), propname);
650f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
651f9af39baSGeorge Wilson 				goto error;
652f9af39baSGeorge Wilson 			}
653f9af39baSGeorge Wilson 			break;
65488f61deeSIgor Kozhukhov 
65588f61deeSIgor Kozhukhov 		default:
65688f61deeSIgor Kozhukhov 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
65788f61deeSIgor Kozhukhov 			    "property '%s'(%d) not defined"), propname, prop);
65888f61deeSIgor Kozhukhov 			break;
659990b4856Slling 		}
660990b4856Slling 	}
661990b4856Slling 
662990b4856Slling 	return (retprops);
663990b4856Slling error:
664990b4856Slling 	nvlist_free(retprops);
665990b4856Slling 	return (NULL);
666990b4856Slling }
667990b4856Slling 
668990b4856Slling /*
669990b4856Slling  * Set zpool property : propname=propval.
670990b4856Slling  */
671990b4856Slling int
672990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
673990b4856Slling {
674990b4856Slling 	zfs_cmd_t zc = { 0 };
675990b4856Slling 	int ret = -1;
676990b4856Slling 	char errbuf[1024];
677990b4856Slling 	nvlist_t *nvl = NULL;
678990b4856Slling 	nvlist_t *realprops;
679990b4856Slling 	uint64_t version;
680f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
681990b4856Slling 
682990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
683990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
684990b4856Slling 	    zhp->zpool_name);
685990b4856Slling 
686990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
687990b4856Slling 		return (no_memory(zhp->zpool_hdl));
688990b4856Slling 
689990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
690990b4856Slling 		nvlist_free(nvl);
691990b4856Slling 		return (no_memory(zhp->zpool_hdl));
692990b4856Slling 	}
693990b4856Slling 
694990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6950a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
696f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
697990b4856Slling 		nvlist_free(nvl);
698990b4856Slling 		return (-1);
699990b4856Slling 	}
700990b4856Slling 
701990b4856Slling 	nvlist_free(nvl);
702990b4856Slling 	nvl = realprops;
703990b4856Slling 
704990b4856Slling 	/*
705990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
706990b4856Slling 	 */
707990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
708990b4856Slling 
709990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
710990b4856Slling 		nvlist_free(nvl);
711990b4856Slling 		return (-1);
712990b4856Slling 	}
713990b4856Slling 
714990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
715990b4856Slling 
716990b4856Slling 	zcmd_free_nvlists(&zc);
717990b4856Slling 	nvlist_free(nvl);
718990b4856Slling 
719990b4856Slling 	if (ret)
720990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
721990b4856Slling 	else
722990b4856Slling 		(void) zpool_props_refresh(zhp);
723990b4856Slling 
724990b4856Slling 	return (ret);
725990b4856Slling }
726990b4856Slling 
727990b4856Slling int
728990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
729990b4856Slling {
730990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
731990b4856Slling 	zprop_list_t *entry;
732990b4856Slling 	char buf[ZFS_MAXPROPLEN];
733ad135b5dSChristopher Siden 	nvlist_t *features = NULL;
734ad135b5dSChristopher Siden 	zprop_list_t **last;
735ad135b5dSChristopher Siden 	boolean_t firstexpand = (NULL == *plp);
736990b4856Slling 
737990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
738990b4856Slling 		return (-1);
739990b4856Slling 
740ad135b5dSChristopher Siden 	last = plp;
741ad135b5dSChristopher Siden 	while (*last != NULL)
742ad135b5dSChristopher Siden 		last = &(*last)->pl_next;
743ad135b5dSChristopher Siden 
744ad135b5dSChristopher Siden 	if ((*plp)->pl_all)
745ad135b5dSChristopher Siden 		features = zpool_get_features(zhp);
746ad135b5dSChristopher Siden 
747ad135b5dSChristopher Siden 	if ((*plp)->pl_all && firstexpand) {
748ad135b5dSChristopher Siden 		for (int i = 0; i < SPA_FEATURES; i++) {
749ad135b5dSChristopher Siden 			zprop_list_t *entry = zfs_alloc(hdl,
750ad135b5dSChristopher Siden 			    sizeof (zprop_list_t));
751ad135b5dSChristopher Siden 			entry->pl_prop = ZPROP_INVAL;
752ad135b5dSChristopher Siden 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
753ad135b5dSChristopher Siden 			    spa_feature_table[i].fi_uname);
754ad135b5dSChristopher Siden 			entry->pl_width = strlen(entry->pl_user_prop);
755ad135b5dSChristopher Siden 			entry->pl_all = B_TRUE;
756ad135b5dSChristopher Siden 
757ad135b5dSChristopher Siden 			*last = entry;
758ad135b5dSChristopher Siden 			last = &entry->pl_next;
759ad135b5dSChristopher Siden 		}
760ad135b5dSChristopher Siden 	}
761ad135b5dSChristopher Siden 
762ad135b5dSChristopher Siden 	/* add any unsupported features */
763ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
764ad135b5dSChristopher Siden 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
765ad135b5dSChristopher Siden 		char *propname;
766ad135b5dSChristopher Siden 		boolean_t found;
767ad135b5dSChristopher Siden 		zprop_list_t *entry;
768ad135b5dSChristopher Siden 
769ad135b5dSChristopher Siden 		if (zfeature_is_supported(nvpair_name(nvp)))
770ad135b5dSChristopher Siden 			continue;
771ad135b5dSChristopher Siden 
772ad135b5dSChristopher Siden 		propname = zfs_asprintf(hdl, "unsupported@%s",
773ad135b5dSChristopher Siden 		    nvpair_name(nvp));
774ad135b5dSChristopher Siden 
775ad135b5dSChristopher Siden 		/*
776ad135b5dSChristopher Siden 		 * Before adding the property to the list make sure that no
777ad135b5dSChristopher Siden 		 * other pool already added the same property.
778ad135b5dSChristopher Siden 		 */
779ad135b5dSChristopher Siden 		found = B_FALSE;
780ad135b5dSChristopher Siden 		entry = *plp;
781ad135b5dSChristopher Siden 		while (entry != NULL) {
782ad135b5dSChristopher Siden 			if (entry->pl_user_prop != NULL &&
783ad135b5dSChristopher Siden 			    strcmp(propname, entry->pl_user_prop) == 0) {
784ad135b5dSChristopher Siden 				found = B_TRUE;
785ad135b5dSChristopher Siden 				break;
786ad135b5dSChristopher Siden 			}
787ad135b5dSChristopher Siden 			entry = entry->pl_next;
788ad135b5dSChristopher Siden 		}
789ad135b5dSChristopher Siden 		if (found) {
790ad135b5dSChristopher Siden 			free(propname);
791ad135b5dSChristopher Siden 			continue;
792ad135b5dSChristopher Siden 		}
793ad135b5dSChristopher Siden 
794ad135b5dSChristopher Siden 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
795ad135b5dSChristopher Siden 		entry->pl_prop = ZPROP_INVAL;
796ad135b5dSChristopher Siden 		entry->pl_user_prop = propname;
797ad135b5dSChristopher Siden 		entry->pl_width = strlen(entry->pl_user_prop);
798ad135b5dSChristopher Siden 		entry->pl_all = B_TRUE;
799ad135b5dSChristopher Siden 
800ad135b5dSChristopher Siden 		*last = entry;
801ad135b5dSChristopher Siden 		last = &entry->pl_next;
802ad135b5dSChristopher Siden 	}
803ad135b5dSChristopher Siden 
804990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
805990b4856Slling 
806990b4856Slling 		if (entry->pl_fixed)
807990b4856Slling 			continue;
808990b4856Slling 
809990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
810990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
811c58b3526SAdam Stevko 		    NULL, B_FALSE) == 0) {
812990b4856Slling 			if (strlen(buf) > entry->pl_width)
813990b4856Slling 				entry->pl_width = strlen(buf);
814990b4856Slling 		}
815990b4856Slling 	}
816990b4856Slling 
817990b4856Slling 	return (0);
818990b4856Slling }
819990b4856Slling 
820ad135b5dSChristopher Siden /*
821ad135b5dSChristopher Siden  * Get the state for the given feature on the given ZFS pool.
822ad135b5dSChristopher Siden  */
823ad135b5dSChristopher Siden int
824ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
825ad135b5dSChristopher Siden     size_t len)
826ad135b5dSChristopher Siden {
827ad135b5dSChristopher Siden 	uint64_t refcount;
828ad135b5dSChristopher Siden 	boolean_t found = B_FALSE;
829ad135b5dSChristopher Siden 	nvlist_t *features = zpool_get_features(zhp);
830ad135b5dSChristopher Siden 	boolean_t supported;
831ad135b5dSChristopher Siden 	const char *feature = strchr(propname, '@') + 1;
832ad135b5dSChristopher Siden 
833ad135b5dSChristopher Siden 	supported = zpool_prop_feature(propname);
8347c13517fSSerapheim Dimitropoulos 	ASSERT(supported || zpool_prop_unsupported(propname));
835ad135b5dSChristopher Siden 
836ad135b5dSChristopher Siden 	/*
837ad135b5dSChristopher Siden 	 * Convert from feature name to feature guid. This conversion is
838ad135b5dSChristopher Siden 	 * unecessary for unsupported@... properties because they already
839ad135b5dSChristopher Siden 	 * use guids.
840ad135b5dSChristopher Siden 	 */
841ad135b5dSChristopher Siden 	if (supported) {
842ad135b5dSChristopher Siden 		int ret;
8432acef22dSMatthew Ahrens 		spa_feature_t fid;
844ad135b5dSChristopher Siden 
8452acef22dSMatthew Ahrens 		ret = zfeature_lookup_name(feature, &fid);
846ad135b5dSChristopher Siden 		if (ret != 0) {
847ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
848ad135b5dSChristopher Siden 			return (ENOTSUP);
849ad135b5dSChristopher Siden 		}
8502acef22dSMatthew Ahrens 		feature = spa_feature_table[fid].fi_guid;
851ad135b5dSChristopher Siden 	}
852ad135b5dSChristopher Siden 
853ad135b5dSChristopher Siden 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
854ad135b5dSChristopher Siden 		found = B_TRUE;
855ad135b5dSChristopher Siden 
856ad135b5dSChristopher Siden 	if (supported) {
857ad135b5dSChristopher Siden 		if (!found) {
858ad135b5dSChristopher Siden 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
859ad135b5dSChristopher Siden 		} else  {
860ad135b5dSChristopher Siden 			if (refcount == 0)
861ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
862ad135b5dSChristopher Siden 			else
863ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
864ad135b5dSChristopher Siden 		}
865ad135b5dSChristopher Siden 	} else {
866ad135b5dSChristopher Siden 		if (found) {
867ad135b5dSChristopher Siden 			if (refcount == 0) {
868ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
869ad135b5dSChristopher Siden 			} else {
870ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
871ad135b5dSChristopher Siden 			}
872ad135b5dSChristopher Siden 		} else {
873ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
874ad135b5dSChristopher Siden 			return (ENOTSUP);
875ad135b5dSChristopher Siden 		}
876ad135b5dSChristopher Siden 	}
877ad135b5dSChristopher Siden 
878ad135b5dSChristopher Siden 	return (0);
879ad135b5dSChristopher Siden }
880990b4856Slling 
881573ca77eSGeorge Wilson /*
882573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
883573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
884573ca77eSGeorge Wilson  */
885573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
886573ca77eSGeorge Wilson 
887fa9e4066Sahrens /*
888fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
889fa9e4066Sahrens  * 'buf'.
890fa9e4066Sahrens  */
891e7cbe64fSgw boolean_t
89299653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
893fa9e4066Sahrens {
894fa9e4066Sahrens 	namecheck_err_t why;
895fa9e4066Sahrens 	char what;
896b468a217Seschrock 	int ret;
897b468a217Seschrock 
898b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
899b468a217Seschrock 
900b468a217Seschrock 	/*
901b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
902b468a217Seschrock 	 * But we need to support users with existing pools that may now be
903b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
904b468a217Seschrock 	 * create (or import), and only in userland.
905b468a217Seschrock 	 */
906b468a217Seschrock 	if (ret == 0 && !isopen &&
907b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
908b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
9098654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
9108654d025Sperrin 	    strcmp(pool, "log") == 0)) {
911e7cbe64fSgw 		if (hdl != NULL)
912e7cbe64fSgw 			zfs_error_aux(hdl,
913e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
91499653d4eSeschrock 		return (B_FALSE);
915b468a217Seschrock 	}
916b468a217Seschrock 
917fa9e4066Sahrens 
918b468a217Seschrock 	if (ret != 0) {
91999653d4eSeschrock 		if (hdl != NULL) {
920fa9e4066Sahrens 			switch (why) {
921b81d61a6Slling 			case NAME_ERR_TOOLONG:
92299653d4eSeschrock 				zfs_error_aux(hdl,
923b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
924b81d61a6Slling 				break;
925b81d61a6Slling 
926fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
92799653d4eSeschrock 				zfs_error_aux(hdl,
928fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
929fa9e4066Sahrens 				    "'%c' in pool name"), what);
930fa9e4066Sahrens 				break;
931fa9e4066Sahrens 
932fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
93399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93499653d4eSeschrock 				    "name must begin with a letter"));
935fa9e4066Sahrens 				break;
936fa9e4066Sahrens 
937fa9e4066Sahrens 			case NAME_ERR_RESERVED:
93899653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93999653d4eSeschrock 				    "name is reserved"));
940fa9e4066Sahrens 				break;
941fa9e4066Sahrens 
942fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
94399653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
94499653d4eSeschrock 				    "pool name is reserved"));
945fa9e4066Sahrens 				break;
9465ad82045Snd 
9475ad82045Snd 			case NAME_ERR_LEADING_SLASH:
9485ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9495ad82045Snd 				    "leading slash in name"));
9505ad82045Snd 				break;
9515ad82045Snd 
9525ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
9535ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9545ad82045Snd 				    "empty component in name"));
9555ad82045Snd 				break;
9565ad82045Snd 
9575ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
9585ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9595ad82045Snd 				    "trailing slash in name"));
9605ad82045Snd 				break;
9615ad82045Snd 
962edb901aaSMarcel Telka 			case NAME_ERR_MULTIPLE_DELIMITERS:
9635ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964edb901aaSMarcel Telka 				    "multiple '@' and/or '#' delimiters in "
965edb901aaSMarcel Telka 				    "name"));
9665ad82045Snd 				break;
9675ad82045Snd 
96888f61deeSIgor Kozhukhov 			default:
96988f61deeSIgor Kozhukhov 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
97088f61deeSIgor Kozhukhov 				    "(%d) not defined"), why);
97188f61deeSIgor Kozhukhov 				break;
972fa9e4066Sahrens 			}
973fa9e4066Sahrens 		}
97499653d4eSeschrock 		return (B_FALSE);
975fa9e4066Sahrens 	}
976fa9e4066Sahrens 
97799653d4eSeschrock 	return (B_TRUE);
978fa9e4066Sahrens }
979fa9e4066Sahrens 
980fa9e4066Sahrens /*
981fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
982fa9e4066Sahrens  * state.
983fa9e4066Sahrens  */
984fa9e4066Sahrens zpool_handle_t *
98599653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
986fa9e4066Sahrens {
987fa9e4066Sahrens 	zpool_handle_t *zhp;
98894de1d4cSeschrock 	boolean_t missing;
989fa9e4066Sahrens 
990fa9e4066Sahrens 	/*
991fa9e4066Sahrens 	 * Make sure the pool name is valid.
992fa9e4066Sahrens 	 */
99399653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
994ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
99599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
99699653d4eSeschrock 		    pool);
997fa9e4066Sahrens 		return (NULL);
998fa9e4066Sahrens 	}
999fa9e4066Sahrens 
100099653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
100199653d4eSeschrock 		return (NULL);
1002fa9e4066Sahrens 
100399653d4eSeschrock 	zhp->zpool_hdl = hdl;
1004fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1005fa9e4066Sahrens 
100694de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
100794de1d4cSeschrock 		zpool_close(zhp);
100894de1d4cSeschrock 		return (NULL);
100994de1d4cSeschrock 	}
101094de1d4cSeschrock 
101194de1d4cSeschrock 	if (missing) {
1012990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1013ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
1014990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
101594de1d4cSeschrock 		zpool_close(zhp);
101694de1d4cSeschrock 		return (NULL);
1017fa9e4066Sahrens 	}
1018fa9e4066Sahrens 
1019fa9e4066Sahrens 	return (zhp);
1020fa9e4066Sahrens }
1021fa9e4066Sahrens 
1022fa9e4066Sahrens /*
1023fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
1024fa9e4066Sahrens  * the configuration cache may be out of date).
1025fa9e4066Sahrens  */
102694de1d4cSeschrock int
102794de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1028fa9e4066Sahrens {
1029fa9e4066Sahrens 	zpool_handle_t *zhp;
103094de1d4cSeschrock 	boolean_t missing;
1031fa9e4066Sahrens 
103294de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
103394de1d4cSeschrock 		return (-1);
1034fa9e4066Sahrens 
103599653d4eSeschrock 	zhp->zpool_hdl = hdl;
1036fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1037fa9e4066Sahrens 
103894de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
103994de1d4cSeschrock 		zpool_close(zhp);
104094de1d4cSeschrock 		return (-1);
1041fa9e4066Sahrens 	}
1042fa9e4066Sahrens 
104394de1d4cSeschrock 	if (missing) {
104494de1d4cSeschrock 		zpool_close(zhp);
104594de1d4cSeschrock 		*ret = NULL;
104694de1d4cSeschrock 		return (0);
104794de1d4cSeschrock 	}
104894de1d4cSeschrock 
104994de1d4cSeschrock 	*ret = zhp;
105094de1d4cSeschrock 	return (0);
1051fa9e4066Sahrens }
1052fa9e4066Sahrens 
1053fa9e4066Sahrens /*
1054fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1055fa9e4066Sahrens  * state.
1056fa9e4066Sahrens  */
1057fa9e4066Sahrens zpool_handle_t *
105899653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1059fa9e4066Sahrens {
1060fa9e4066Sahrens 	zpool_handle_t *zhp;
1061fa9e4066Sahrens 
106299653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1063fa9e4066Sahrens 		return (NULL);
1064fa9e4066Sahrens 
1065fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1066ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
106799653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1068fa9e4066Sahrens 		zpool_close(zhp);
1069fa9e4066Sahrens 		return (NULL);
1070fa9e4066Sahrens 	}
1071fa9e4066Sahrens 
1072fa9e4066Sahrens 	return (zhp);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens 
1075fa9e4066Sahrens /*
1076fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
1077fa9e4066Sahrens  */
1078fa9e4066Sahrens void
1079fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1080fa9e4066Sahrens {
1081aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zhp->zpool_config);
1082aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zhp->zpool_old_config);
1083aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zhp->zpool_props);
1084fa9e4066Sahrens 	free(zhp);
1085fa9e4066Sahrens }
1086fa9e4066Sahrens 
1087fa9e4066Sahrens /*
1088fa9e4066Sahrens  * Return the name of the pool.
1089fa9e4066Sahrens  */
1090fa9e4066Sahrens const char *
1091fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1092fa9e4066Sahrens {
1093fa9e4066Sahrens 	return (zhp->zpool_name);
1094fa9e4066Sahrens }
1095fa9e4066Sahrens 
1096fa9e4066Sahrens 
1097fa9e4066Sahrens /*
1098fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1099fa9e4066Sahrens  */
1100fa9e4066Sahrens int
1101fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1102fa9e4066Sahrens {
1103fa9e4066Sahrens 	return (zhp->zpool_state);
1104fa9e4066Sahrens }
1105fa9e4066Sahrens 
1106fa9e4066Sahrens /*
1107fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
1108fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
1109fa9e4066Sahrens  * don't have to worry about error semantics.
1110fa9e4066Sahrens  */
1111fa9e4066Sahrens int
111299653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
11130a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
1114fa9e4066Sahrens {
1115fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
11160a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
11170a48a24eStimh 	nvlist_t *zc_props = NULL;
111899653d4eSeschrock 	char msg[1024];
11190a48a24eStimh 	int ret = -1;
1120fa9e4066Sahrens 
112199653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
112299653d4eSeschrock 	    "cannot create '%s'"), pool);
1123fa9e4066Sahrens 
112499653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
112599653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1126fa9e4066Sahrens 
1127351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1128990b4856Slling 		return (-1);
1129fa9e4066Sahrens 
11300a48a24eStimh 	if (props) {
1131f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1132f9af39baSGeorge Wilson 
11330a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1134f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
11350a48a24eStimh 			goto create_failed;
11360a48a24eStimh 		}
11370a48a24eStimh 	}
113899653d4eSeschrock 
11390a48a24eStimh 	if (fsprops) {
11400a48a24eStimh 		uint64_t zoned;
11410a48a24eStimh 		char *zonestr;
11420a48a24eStimh 
11430a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
11440a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
11450a48a24eStimh 		    strcmp(zonestr, "on") == 0);
11460a48a24eStimh 
1147e9316f76SJoe Stein 		if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1148e9316f76SJoe Stein 		    fsprops, zoned, NULL, NULL, msg)) == NULL) {
11490a48a24eStimh 			goto create_failed;
11500a48a24eStimh 		}
11510a48a24eStimh 		if (!zc_props &&
11520a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
11530a48a24eStimh 			goto create_failed;
11540a48a24eStimh 		}
11550a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
11560a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
11570a48a24eStimh 			goto create_failed;
11580a48a24eStimh 		}
1159351420b3Slling 	}
1160fa9e4066Sahrens 
11610a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
11620a48a24eStimh 		goto create_failed;
11630a48a24eStimh 
1164990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1165fa9e4066Sahrens 
11660a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1167351420b3Slling 
1168e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
11690a48a24eStimh 		nvlist_free(zc_props);
11700a48a24eStimh 		nvlist_free(zc_fsprops);
1171fa9e4066Sahrens 
117299653d4eSeschrock 		switch (errno) {
1173fa9e4066Sahrens 		case EBUSY:
1174fa9e4066Sahrens 			/*
1175fa9e4066Sahrens 			 * This can happen if the user has specified the same
1176fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1177fa9e4066Sahrens 			 * until we try to add it and see we already have a
1178fa9e4066Sahrens 			 * label.
1179fa9e4066Sahrens 			 */
118099653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118199653d4eSeschrock 			    "one or more vdevs refer to the same device"));
118299653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1183fa9e4066Sahrens 
1184e9316f76SJoe Stein 		case ERANGE:
1185e9316f76SJoe Stein 			/*
1186e9316f76SJoe Stein 			 * This happens if the record size is smaller or larger
1187e9316f76SJoe Stein 			 * than the allowed size range, or not a power of 2.
1188e9316f76SJoe Stein 			 *
1189e9316f76SJoe Stein 			 * NOTE: although zfs_valid_proplist is called earlier,
1190e9316f76SJoe Stein 			 * this case may have slipped through since the
1191e9316f76SJoe Stein 			 * pool does not exist yet and it is therefore
1192e9316f76SJoe Stein 			 * impossible to read properties e.g. max blocksize
1193e9316f76SJoe Stein 			 * from the pool.
1194e9316f76SJoe Stein 			 */
1195e9316f76SJoe Stein 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1196e9316f76SJoe Stein 			    "record size invalid"));
1197e9316f76SJoe Stein 			return (zfs_error(hdl, EZFS_BADPROP, msg));
1198e9316f76SJoe Stein 
1199fa9e4066Sahrens 		case EOVERFLOW:
1200fa9e4066Sahrens 			/*
120199653d4eSeschrock 			 * This occurs when one of the devices is below
1202fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1203fa9e4066Sahrens 			 * device was the problem device since there's no
1204fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1205fa9e4066Sahrens 			 */
1206fa9e4066Sahrens 			{
1207fa9e4066Sahrens 				char buf[64];
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1210fa9e4066Sahrens 
121199653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121299653d4eSeschrock 				    "one or more devices is less than the "
121399653d4eSeschrock 				    "minimum size (%s)"), buf);
1214fa9e4066Sahrens 			}
121599653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1216fa9e4066Sahrens 
1217fa9e4066Sahrens 		case ENOSPC:
121899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121999653d4eSeschrock 			    "one or more devices is out of space"));
122099653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1221fa9e4066Sahrens 
1222fa94a07fSbrendan 		case ENOTBLK:
1223fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1225fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1226fa94a07fSbrendan 
1227fa9e4066Sahrens 		default:
122899653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1229fa9e4066Sahrens 		}
1230fa9e4066Sahrens 	}
1231fa9e4066Sahrens 
12320a48a24eStimh create_failed:
1233351420b3Slling 	zcmd_free_nvlists(&zc);
12340a48a24eStimh 	nvlist_free(zc_props);
12350a48a24eStimh 	nvlist_free(zc_fsprops);
12360a48a24eStimh 	return (ret);
1237fa9e4066Sahrens }
1238fa9e4066Sahrens 
1239fa9e4066Sahrens /*
1240fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1241fa9e4066Sahrens  * datasets left in the pool.
1242fa9e4066Sahrens  */
1243fa9e4066Sahrens int
12444445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1245fa9e4066Sahrens {
1246fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1247fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
124899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
124999653d4eSeschrock 	char msg[1024];
1250fa9e4066Sahrens 
1251fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1252cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1253fa9e4066Sahrens 		return (-1);
1254fa9e4066Sahrens 
1255fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
12564445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1257fa9e4066Sahrens 
1258cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
125999653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
126099653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1261fa9e4066Sahrens 
126299653d4eSeschrock 		if (errno == EROFS) {
126399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
126499653d4eSeschrock 			    "one or more devices is read only"));
126599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
126699653d4eSeschrock 		} else {
126799653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1268fa9e4066Sahrens 		}
1269fa9e4066Sahrens 
1270fa9e4066Sahrens 		if (zfp)
1271fa9e4066Sahrens 			zfs_close(zfp);
1272fa9e4066Sahrens 		return (-1);
1273fa9e4066Sahrens 	}
1274fa9e4066Sahrens 
1275fa9e4066Sahrens 	if (zfp) {
1276fa9e4066Sahrens 		remove_mountpoint(zfp);
1277fa9e4066Sahrens 		zfs_close(zfp);
1278fa9e4066Sahrens 	}
1279fa9e4066Sahrens 
1280fa9e4066Sahrens 	return (0);
1281fa9e4066Sahrens }
1282fa9e4066Sahrens 
1283fa9e4066Sahrens /*
1284fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1285fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1286fa9e4066Sahrens  */
1287fa9e4066Sahrens int
1288fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1289fa9e4066Sahrens {
1290e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
129199653d4eSeschrock 	int ret;
129299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
129399653d4eSeschrock 	char msg[1024];
1294fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1295fa94a07fSbrendan 	uint_t nspares, nl2cache;
129699653d4eSeschrock 
129799653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
129899653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
129999653d4eSeschrock 
1300fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1301fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
130299653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
130399653d4eSeschrock 	    &spares, &nspares) == 0) {
130499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
130599653d4eSeschrock 		    "upgraded to add hot spares"));
130699653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
130799653d4eSeschrock 	}
1308fa9e4066Sahrens 
1309fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1310fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1311fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1312fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1313fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1314fa94a07fSbrendan 		    "upgraded to add cache devices"));
1315fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1316fa94a07fSbrendan 	}
1317fa94a07fSbrendan 
1318990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
131999653d4eSeschrock 		return (-1);
1320fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1321fa9e4066Sahrens 
1322cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1323fa9e4066Sahrens 		switch (errno) {
1324fa9e4066Sahrens 		case EBUSY:
1325fa9e4066Sahrens 			/*
1326fa9e4066Sahrens 			 * This can happen if the user has specified the same
1327fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1328fa9e4066Sahrens 			 * until we try to add it and see we already have a
1329fa9e4066Sahrens 			 * label.
1330fa9e4066Sahrens 			 */
133199653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133299653d4eSeschrock 			    "one or more vdevs refer to the same device"));
133399653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1334fa9e4066Sahrens 			break;
1335fa9e4066Sahrens 
13365cabbc6bSPrashanth Sreenivasa 		case EINVAL:
13375cabbc6bSPrashanth Sreenivasa 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13385cabbc6bSPrashanth Sreenivasa 			    "invalid config; a pool with removing/removed "
13395cabbc6bSPrashanth Sreenivasa 			    "vdevs does not support adding raidz vdevs"));
13405cabbc6bSPrashanth Sreenivasa 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
13415cabbc6bSPrashanth Sreenivasa 			break;
13425cabbc6bSPrashanth Sreenivasa 
1343fa9e4066Sahrens 		case EOVERFLOW:
1344fa9e4066Sahrens 			/*
1345fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1346fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1347fa9e4066Sahrens 			 * device was the problem device since there's no
1348fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1349fa9e4066Sahrens 			 */
1350fa9e4066Sahrens 			{
1351fa9e4066Sahrens 				char buf[64];
1352fa9e4066Sahrens 
1353fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1354fa9e4066Sahrens 
135599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135699653d4eSeschrock 				    "device is less than the minimum "
135799653d4eSeschrock 				    "size (%s)"), buf);
1358fa9e4066Sahrens 			}
135999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
136099653d4eSeschrock 			break;
136199653d4eSeschrock 
136299653d4eSeschrock 		case ENOTSUP:
136399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13648654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
136599653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1366fa9e4066Sahrens 			break;
1367fa9e4066Sahrens 
1368b1b8ab34Slling 		case EDOM:
1369b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13708654d025Sperrin 			    "root pool can not have multiple vdevs"
13718654d025Sperrin 			    " or separate logs"));
1372b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1373b1b8ab34Slling 			break;
1374b1b8ab34Slling 
1375fa94a07fSbrendan 		case ENOTBLK:
1376fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1377fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1378fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1379fa94a07fSbrendan 			break;
1380fa94a07fSbrendan 
1381fa9e4066Sahrens 		default:
138299653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1383fa9e4066Sahrens 		}
1384fa9e4066Sahrens 
138599653d4eSeschrock 		ret = -1;
138699653d4eSeschrock 	} else {
138799653d4eSeschrock 		ret = 0;
1388fa9e4066Sahrens 	}
1389fa9e4066Sahrens 
1390e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1391fa9e4066Sahrens 
139299653d4eSeschrock 	return (ret);
1393fa9e4066Sahrens }
1394fa9e4066Sahrens 
1395fa9e4066Sahrens /*
1396fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1397fa9e4066Sahrens  * mounted datasets in the pool.
1398fa9e4066Sahrens  */
13994445fffbSMatthew Ahrens static int
14004445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
14014445fffbSMatthew Ahrens     const char *log_str)
1402fa9e4066Sahrens {
1403fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
140489a89ebfSlling 	char msg[1024];
1405fa9e4066Sahrens 
140689a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
140789a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
140889a89ebfSlling 
1409fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
141089a89ebfSlling 	zc.zc_cookie = force;
1411394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
14124445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
141389a89ebfSlling 
141489a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
141589a89ebfSlling 		switch (errno) {
141689a89ebfSlling 		case EXDEV:
141789a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
141889a89ebfSlling 			    "use '-f' to override the following errors:\n"
141989a89ebfSlling 			    "'%s' has an active shared spare which could be"
142089a89ebfSlling 			    " used by other pools once '%s' is exported."),
142189a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
142289a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
142389a89ebfSlling 			    msg));
142489a89ebfSlling 		default:
142589a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
142689a89ebfSlling 			    msg));
142789a89ebfSlling 		}
142889a89ebfSlling 	}
1429fa9e4066Sahrens 
1430fa9e4066Sahrens 	return (0);
1431fa9e4066Sahrens }
1432fa9e4066Sahrens 
1433394ab0cbSGeorge Wilson int
14344445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1435394ab0cbSGeorge Wilson {
14364445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1437394ab0cbSGeorge Wilson }
1438394ab0cbSGeorge Wilson 
1439394ab0cbSGeorge Wilson int
14404445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1441394ab0cbSGeorge Wilson {
14424445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1443394ab0cbSGeorge Wilson }
1444394ab0cbSGeorge Wilson 
1445468c413aSTim Haley static void
1446468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
14474b964adaSGeorge Wilson     nvlist_t *config)
1448468c413aSTim Haley {
14494b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1450468c413aSTim Haley 	uint64_t rewindto;
1451468c413aSTim Haley 	int64_t loss = -1;
1452468c413aSTim Haley 	struct tm t;
1453468c413aSTim Haley 	char timestr[128];
1454468c413aSTim Haley 
14554b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
14564b964adaSGeorge Wilson 		return;
14574b964adaSGeorge Wilson 
1458ad135b5dSChristopher Siden 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1459ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1460468c413aSTim Haley 		return;
1461ad135b5dSChristopher Siden 	}
1462468c413aSTim Haley 
14634b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1464468c413aSTim Haley 		return;
14654b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1466468c413aSTim Haley 
1467468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1468468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1469468c413aSTim Haley 		if (dryrun) {
1470468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1471468c413aSTim Haley 			    "Would be able to return %s "
1472468c413aSTim Haley 			    "to its state as of %s.\n"),
1473468c413aSTim Haley 			    name, timestr);
1474468c413aSTim Haley 		} else {
1475468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1476468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1477468c413aSTim Haley 			    name, timestr);
1478468c413aSTim Haley 		}
1479468c413aSTim Haley 		if (loss > 120) {
1480468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1481468c413aSTim Haley 			    "%s approximately %lld "),
1482468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1483468c413aSTim Haley 			    (loss + 30) / 60);
1484468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1485468c413aSTim Haley 			    "minutes of transactions.\n"));
1486468c413aSTim Haley 		} else if (loss > 0) {
1487468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1488468c413aSTim Haley 			    "%s approximately %lld "),
1489468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1490468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1491468c413aSTim Haley 			    "seconds of transactions.\n"));
1492468c413aSTim Haley 		}
1493468c413aSTim Haley 	}
1494468c413aSTim Haley }
1495468c413aSTim Haley 
1496468c413aSTim Haley void
1497468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1498468c413aSTim Haley     nvlist_t *config)
1499468c413aSTim Haley {
15004b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1501468c413aSTim Haley 	int64_t loss = -1;
1502468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1503468c413aSTim Haley 	uint64_t rewindto;
1504468c413aSTim Haley 	struct tm t;
1505468c413aSTim Haley 	char timestr[128];
1506468c413aSTim Haley 
1507468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1508468c413aSTim Haley 		return;
1509468c413aSTim Haley 
1510468c413aSTim Haley 	if (reason >= 0)
1511468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1512468c413aSTim Haley 	else
1513468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1514468c413aSTim Haley 
1515468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
15164b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1517ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
15184b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1519468c413aSTim Haley 		goto no_info;
1520468c413aSTim Haley 
15214b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
15224b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1523468c413aSTim Haley 	    &edata);
1524468c413aSTim Haley 
1525468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1526468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1527468c413aSTim Haley 
1528468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1529468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1530468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1531468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1532468c413aSTim Haley 		    "\tshould correct the problem.  "),
1533468c413aSTim Haley 		    timestr);
1534468c413aSTim Haley 	} else {
1535468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1536468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1537468c413aSTim Haley 		    "should correct the problem.\n\t"));
1538468c413aSTim Haley 	}
1539468c413aSTim Haley 
1540468c413aSTim Haley 	if (loss > 120) {
1541468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1542468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1543468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1544468c413aSTim Haley 	} else if (loss > 0) {
1545468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1546468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1547468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1548468c413aSTim Haley 	}
1549468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1550468c413aSTim Haley 		if (edata == 1) {
1551468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1552468c413aSTim Haley 			    "After rewind, at least\n"
1553468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1554468c413aSTim Haley 		} else {
1555468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1556468c413aSTim Haley 			    "After rewind, several\n"
1557468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1558468c413aSTim Haley 		}
1559468c413aSTim Haley 	}
1560468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1561a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1562a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1563468c413aSTim Haley 
1564468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1565468c413aSTim Haley 	    "A scrub of the pool\n"
1566468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1567468c413aSTim Haley 	return;
1568468c413aSTim Haley 
1569468c413aSTim Haley no_info:
1570468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1571468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1572468c413aSTim Haley }
1573468c413aSTim Haley 
1574fa9e4066Sahrens /*
1575990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1576990b4856Slling  * if possible.
1577990b4856Slling  *
1578990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1579990b4856Slling  * new properties value to be set.
1580fa9e4066Sahrens  */
1581fa9e4066Sahrens int
158299653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1583990b4856Slling     char *altroot)
1584990b4856Slling {
1585990b4856Slling 	nvlist_t *props = NULL;
1586990b4856Slling 	int ret;
1587990b4856Slling 
1588990b4856Slling 	if (altroot != NULL) {
1589990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1590990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1591990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1592990b4856Slling 			    newname));
1593990b4856Slling 		}
1594990b4856Slling 
1595990b4856Slling 		if (nvlist_add_string(props,
1596352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1597352d8027SGeorge Wilson 		    nvlist_add_string(props,
1598352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1599990b4856Slling 			nvlist_free(props);
1600990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1601990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1602990b4856Slling 			    newname));
1603990b4856Slling 		}
1604990b4856Slling 	}
1605990b4856Slling 
16064b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
16074b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1608aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(props);
1609990b4856Slling 	return (ret);
1610990b4856Slling }
1611990b4856Slling 
16124b964adaSGeorge Wilson static void
16134b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
16144b964adaSGeorge Wilson     int indent)
16154b964adaSGeorge Wilson {
16164b964adaSGeorge Wilson 	nvlist_t **child;
16174b964adaSGeorge Wilson 	uint_t c, children;
16184b964adaSGeorge Wilson 	char *vname;
16194b964adaSGeorge Wilson 	uint64_t is_log = 0;
16204b964adaSGeorge Wilson 
16214b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
16224b964adaSGeorge Wilson 	    &is_log);
16234b964adaSGeorge Wilson 
16244b964adaSGeorge Wilson 	if (name != NULL)
16254b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
16264b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
16274b964adaSGeorge Wilson 
16284b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
16294b964adaSGeorge Wilson 	    &child, &children) != 0)
16304b964adaSGeorge Wilson 		return;
16314b964adaSGeorge Wilson 
16324b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
16334b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
16344b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
16354b964adaSGeorge Wilson 		free(vname);
16364b964adaSGeorge Wilson 	}
16374b964adaSGeorge Wilson }
16384b964adaSGeorge Wilson 
1639ad135b5dSChristopher Siden void
1640ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1641ad135b5dSChristopher Siden {
1642ad135b5dSChristopher Siden 	nvlist_t *nvinfo, *unsup_feat;
1643ad135b5dSChristopher Siden 
1644ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1645ad135b5dSChristopher Siden 	    0);
1646ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1647ad135b5dSChristopher Siden 	    &unsup_feat) == 0);
1648ad135b5dSChristopher Siden 
1649ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1650ad135b5dSChristopher Siden 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1651ad135b5dSChristopher Siden 		char *desc;
1652ad135b5dSChristopher Siden 
1653ad135b5dSChristopher Siden 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1654ad135b5dSChristopher Siden 		verify(nvpair_value_string(nvp, &desc) == 0);
1655ad135b5dSChristopher Siden 
1656ad135b5dSChristopher Siden 		if (strlen(desc) > 0)
1657ad135b5dSChristopher Siden 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1658ad135b5dSChristopher Siden 		else
1659ad135b5dSChristopher Siden 			(void) printf("\t%s\n", nvpair_name(nvp));
1660ad135b5dSChristopher Siden 	}
1661ad135b5dSChristopher Siden }
1662ad135b5dSChristopher Siden 
1663990b4856Slling /*
1664990b4856Slling  * Import the given pool using the known configuration and a list of
1665990b4856Slling  * properties to be set. The configuration should have come from
1666990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1667990b4856Slling  * is imported with a different name.
1668990b4856Slling  */
1669990b4856Slling int
1670990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
16714b964adaSGeorge Wilson     nvlist_t *props, int flags)
1672fa9e4066Sahrens {
1673e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1674468c413aSTim Haley 	zpool_rewind_policy_t policy;
16754b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
16764b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
16774b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1678fa9e4066Sahrens 	char *thename;
1679fa9e4066Sahrens 	char *origname;
1680fa9e4066Sahrens 	int ret;
16814b964adaSGeorge Wilson 	int error = 0;
1682990b4856Slling 	char errbuf[1024];
1683fa9e4066Sahrens 
1684fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1685fa9e4066Sahrens 	    &origname) == 0);
1686fa9e4066Sahrens 
1687990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1688990b4856Slling 	    "cannot import pool '%s'"), origname);
1689990b4856Slling 
1690fa9e4066Sahrens 	if (newname != NULL) {
169199653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1692ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
169399653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
169499653d4eSeschrock 			    newname));
1695fa9e4066Sahrens 		thename = (char *)newname;
1696fa9e4066Sahrens 	} else {
1697fa9e4066Sahrens 		thename = origname;
1698fa9e4066Sahrens 	}
1699fa9e4066Sahrens 
1700078266a5SMarcel Telka 	if (props != NULL) {
1701990b4856Slling 		uint64_t version;
1702f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1703fa9e4066Sahrens 
1704990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1705990b4856Slling 		    &version) == 0);
1706fa9e4066Sahrens 
17070a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1708078266a5SMarcel Telka 		    props, version, flags, errbuf)) == NULL)
1709990b4856Slling 			return (-1);
1710078266a5SMarcel Telka 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1711351420b3Slling 			nvlist_free(props);
1712990b4856Slling 			return (-1);
1713351420b3Slling 		}
1714078266a5SMarcel Telka 		nvlist_free(props);
1715990b4856Slling 	}
1716990b4856Slling 
1717990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1718fa9e4066Sahrens 
1719fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1720ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1721fa9e4066Sahrens 
1722351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1723078266a5SMarcel Telka 		zcmd_free_nvlists(&zc);
172499653d4eSeschrock 		return (-1);
1725351420b3Slling 	}
172657f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1727078266a5SMarcel Telka 		zcmd_free_nvlists(&zc);
1728468c413aSTim Haley 		return (-1);
1729468c413aSTim Haley 	}
1730fa9e4066Sahrens 
17314b964adaSGeorge Wilson 	zc.zc_cookie = flags;
17324b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
17334b964adaSGeorge Wilson 	    errno == ENOMEM) {
17344b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
17354b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
17364b964adaSGeorge Wilson 			return (-1);
17374b964adaSGeorge Wilson 		}
17384b964adaSGeorge Wilson 	}
17394b964adaSGeorge Wilson 	if (ret != 0)
17404b964adaSGeorge Wilson 		error = errno;
17414b964adaSGeorge Wilson 
17424b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1743078266a5SMarcel Telka 
1744078266a5SMarcel Telka 	zcmd_free_nvlists(&zc);
1745078266a5SMarcel Telka 
17464b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
17474b964adaSGeorge Wilson 
17484b964adaSGeorge Wilson 	if (error) {
1749fa9e4066Sahrens 		char desc[1024];
1750468c413aSTim Haley 
1751468c413aSTim Haley 		/*
1752468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1753468c413aSTim Haley 		 * looks like if we found a best txg
1754468c413aSTim Haley 		 */
17554b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1756468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
17574b964adaSGeorge Wilson 			    B_TRUE, nv);
17584b964adaSGeorge Wilson 			nvlist_free(nv);
1759468c413aSTim Haley 			return (-1);
1760468c413aSTim Haley 		}
1761468c413aSTim Haley 
1762fa9e4066Sahrens 		if (newname == NULL)
1763fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1764fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1765fa9e4066Sahrens 			    thename);
1766fa9e4066Sahrens 		else
1767fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1768fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1769fa9e4066Sahrens 			    origname, thename);
1770fa9e4066Sahrens 
17714b964adaSGeorge Wilson 		switch (error) {
1772ea8dc4b6Seschrock 		case ENOTSUP:
1773ad135b5dSChristopher Siden 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1774ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1775ad135b5dSChristopher Siden 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1776ad135b5dSChristopher Siden 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1777ad135b5dSChristopher Siden 				    "pool uses the following feature(s) not "
1778ad135b5dSChristopher Siden 				    "supported by this system:\n"));
1779ad135b5dSChristopher Siden 				zpool_print_unsup_feat(nv);
1780ad135b5dSChristopher Siden 				if (nvlist_exists(nvinfo,
1781ad135b5dSChristopher Siden 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1782ad135b5dSChristopher Siden 					(void) printf(dgettext(TEXT_DOMAIN,
1783ad135b5dSChristopher Siden 					    "All unsupported features are only "
1784ad135b5dSChristopher Siden 					    "required for writing to the pool."
1785ad135b5dSChristopher Siden 					    "\nThe pool can be imported using "
1786ad135b5dSChristopher Siden 					    "'-o readonly=on'.\n"));
1787ad135b5dSChristopher Siden 				}
1788ad135b5dSChristopher Siden 			}
1789ea8dc4b6Seschrock 			/*
1790ea8dc4b6Seschrock 			 * Unsupported version.
1791ea8dc4b6Seschrock 			 */
179299653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1793ea8dc4b6Seschrock 			break;
1794ea8dc4b6Seschrock 
1795b5989ec7Seschrock 		case EINVAL:
1796b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1797b5989ec7Seschrock 			break;
1798b5989ec7Seschrock 
179954a91118SChris Kirby 		case EROFS:
180054a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
180154a91118SChris Kirby 			    "one or more devices is read only"));
180254a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
180354a91118SChris Kirby 			break;
180454a91118SChris Kirby 
18054b964adaSGeorge Wilson 		case ENXIO:
18064b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
18074b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
18084b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
18094b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
18104b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
18114b964adaSGeorge Wilson 				    "The devices below are missing, use "
18124b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
18134b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
18144b964adaSGeorge Wilson 				(void) printf("\n");
18154b964adaSGeorge Wilson 			}
18164b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
18174b964adaSGeorge Wilson 			break;
18184b964adaSGeorge Wilson 
18194b964adaSGeorge Wilson 		case EEXIST:
18204b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
18214b964adaSGeorge Wilson 			break;
1822c971037bSPaul Dagnelie 		case ENAMETOOLONG:
1823c971037bSPaul Dagnelie 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1824c971037bSPaul Dagnelie 			    "new name of at least one dataset is longer than "
1825c971037bSPaul Dagnelie 			    "the maximum allowable length"));
1826c971037bSPaul Dagnelie 			(void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1827c971037bSPaul Dagnelie 			break;
1828fa9e4066Sahrens 		default:
18294b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1830468c413aSTim Haley 			zpool_explain_recover(hdl,
18314b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1832468c413aSTim Haley 			break;
1833fa9e4066Sahrens 		}
1834fa9e4066Sahrens 
18354b964adaSGeorge Wilson 		nvlist_free(nv);
1836fa9e4066Sahrens 		ret = -1;
1837fa9e4066Sahrens 	} else {
1838fa9e4066Sahrens 		zpool_handle_t *zhp;
1839ecd6cf80Smarks 
1840fa9e4066Sahrens 		/*
1841fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1842fa9e4066Sahrens 		 */
1843681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
184494de1d4cSeschrock 			ret = -1;
1845681d9761SEric Taylor 		else if (zhp != NULL)
1846fa9e4066Sahrens 			zpool_close(zhp);
1847468c413aSTim Haley 		if (policy.zrp_request &
1848468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1849468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
18504b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1851468c413aSTim Haley 		}
18524b964adaSGeorge Wilson 		nvlist_free(nv);
1853468c413aSTim Haley 		return (0);
1854fa9e4066Sahrens 	}
1855fa9e4066Sahrens 
1856fa9e4066Sahrens 	return (ret);
1857fa9e4066Sahrens }
1858fa9e4066Sahrens 
1859fa9e4066Sahrens /*
18603f9d6ad7SLin Ling  * Scan the pool.
1861fa9e4066Sahrens  */
1862fa9e4066Sahrens int
18631702cce7SAlek Pinchuk zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1864fa9e4066Sahrens {
1865fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1866fa9e4066Sahrens 	char msg[1024];
18671702cce7SAlek Pinchuk 	int err;
186899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1869fa9e4066Sahrens 
1870fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18713f9d6ad7SLin Ling 	zc.zc_cookie = func;
18721702cce7SAlek Pinchuk 	zc.zc_flags = cmd;
1873fa9e4066Sahrens 
18741702cce7SAlek Pinchuk 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
18751702cce7SAlek Pinchuk 		return (0);
18761702cce7SAlek Pinchuk 
18771702cce7SAlek Pinchuk 	err = errno;
18781702cce7SAlek Pinchuk 
18791702cce7SAlek Pinchuk 	/* ECANCELED on a scrub means we resumed a paused scrub */
18801702cce7SAlek Pinchuk 	if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
18811702cce7SAlek Pinchuk 	    cmd == POOL_SCRUB_NORMAL)
18821702cce7SAlek Pinchuk 		return (0);
18831702cce7SAlek Pinchuk 
18841702cce7SAlek Pinchuk 	if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1885fa9e4066Sahrens 		return (0);
1886fa9e4066Sahrens 
18873f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
18881702cce7SAlek Pinchuk 		if (cmd == POOL_SCRUB_PAUSE) {
18891702cce7SAlek Pinchuk 			(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
18901702cce7SAlek Pinchuk 			    "cannot pause scrubbing %s"), zc.zc_name);
18911702cce7SAlek Pinchuk 		} else {
18921702cce7SAlek Pinchuk 			assert(cmd == POOL_SCRUB_NORMAL);
18931702cce7SAlek Pinchuk 			(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
18941702cce7SAlek Pinchuk 			    "cannot scrub %s"), zc.zc_name);
18951702cce7SAlek Pinchuk 		}
18963f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
18973f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18983f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
18993f9d6ad7SLin Ling 		    zc.zc_name);
19003f9d6ad7SLin Ling 	} else {
19013f9d6ad7SLin Ling 		assert(!"unexpected result");
19023f9d6ad7SLin Ling 	}
1903fa9e4066Sahrens 
19041702cce7SAlek Pinchuk 	if (err == EBUSY) {
19053f9d6ad7SLin Ling 		nvlist_t *nvroot;
19063f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
19073f9d6ad7SLin Ling 		uint_t psc;
19083f9d6ad7SLin Ling 
19093f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
19103f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
19113f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
19123f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
19131702cce7SAlek Pinchuk 		if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
19141702cce7SAlek Pinchuk 			if (cmd == POOL_SCRUB_PAUSE)
19151702cce7SAlek Pinchuk 				return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
19161702cce7SAlek Pinchuk 			else
19171702cce7SAlek Pinchuk 				return (zfs_error(hdl, EZFS_SCRUBBING, msg));
19181702cce7SAlek Pinchuk 		} else {
19193f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
19201702cce7SAlek Pinchuk 		}
19211702cce7SAlek Pinchuk 	} else if (err == ENOENT) {
19223f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
19233f9d6ad7SLin Ling 	} else {
19241702cce7SAlek Pinchuk 		return (zpool_standard_error(hdl, err, msg));
19253f9d6ad7SLin Ling 	}
1926fa9e4066Sahrens }
1927fa9e4066Sahrens 
19283fdda499SJohn Harres /*
19293fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
19303fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
19313fdda499SJohn Harres  * verification of the s# part.
19323fdda499SJohn Harres  */
19333fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
19343fdda499SJohn Harres 
19353fdda499SJohn Harres /*
19363fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
19373fdda499SJohn Harres  * and the like.
19383fdda499SJohn Harres  */
19393fdda499SJohn Harres static int
19409a686fbcSPaul Dagnelie ctd_check_path(char *str)
19419a686fbcSPaul Dagnelie {
19423fdda499SJohn Harres 	/*
19433fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
19443fdda499SJohn Harres 	 */
19453fdda499SJohn Harres 	if (str && str[0] == '/') {
19463fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
19473fdda499SJohn Harres 
19483fdda499SJohn Harres 		/*
19493fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
19503fdda499SJohn Harres 		 * component of the string instead.
19513fdda499SJohn Harres 		 */
19523fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
19533fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
19543fdda499SJohn Harres 				;
19553fdda499SJohn Harres 		}
19563fdda499SJohn Harres 		str = tmp + 1;
19573fdda499SJohn Harres 	}
19583fdda499SJohn Harres 	return (CTD_CHECK(str));
19593fdda499SJohn Harres }
19603fdda499SJohn Harres 
1961a43d325bSek /*
1962573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1963573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1964a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1965a43d325bSek  * spare; but FALSE if its an INUSE spare.
1966a43d325bSek  */
196799653d4eSeschrock static nvlist_t *
1968573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1969573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1970ea8dc4b6Seschrock {
1971ea8dc4b6Seschrock 	uint_t c, children;
1972ea8dc4b6Seschrock 	nvlist_t **child;
197399653d4eSeschrock 	nvlist_t *ret;
1974ee0eb9f2SEric Schrock 	uint64_t is_log;
1975573ca77eSGeorge Wilson 	char *srchkey;
1976573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1977573ca77eSGeorge Wilson 
1978573ca77eSGeorge Wilson 	/* Nothing to look for */
1979573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1980573ca77eSGeorge Wilson 		return (NULL);
1981ea8dc4b6Seschrock 
1982573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1983573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1984573ca77eSGeorge Wilson 
1985573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1986cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1987573ca77eSGeorge Wilson 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1988cb04b873SMark J Musante 			uint64_t srchval, theguid;
1989cb04b873SMark J Musante 
1990cb04b873SMark J Musante 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1991cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1992cb04b873SMark J Musante 			    &theguid) == 0);
1993cb04b873SMark J Musante 			if (theguid == srchval)
1994cb04b873SMark J Musante 				return (nv);
1995573ca77eSGeorge Wilson 		}
1996573ca77eSGeorge Wilson 		break;
1997573ca77eSGeorge Wilson 
1998573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1999573ca77eSGeorge Wilson 		char *srchval, *val;
2000573ca77eSGeorge Wilson 
2001573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
2002573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2003573ca77eSGeorge Wilson 			break;
2004ea8dc4b6Seschrock 
2005ea8dc4b6Seschrock 		/*
20063fdda499SJohn Harres 		 * Search for the requested value. Special cases:
20073fdda499SJohn Harres 		 *
20087855d95bSToomas Soome 		 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
20097855d95bSToomas Soome 		 *   UEFI boot, these end in "s0" or "s0/old" or "s1" or
20107855d95bSToomas Soome 		 *   "s1/old".   The "s0" or "s1" part is hidden from the user,
20113fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
20123fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
20133fdda499SJohn Harres 		 *
201488ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
2015ea8dc4b6Seschrock 		 */
20163fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
20173fdda499SJohn Harres 		    ctd_check_path(val)) {
2018573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
2019573ca77eSGeorge Wilson 
2020573ca77eSGeorge Wilson 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2021573ca77eSGeorge Wilson 			    &wholedisk);
2022573ca77eSGeorge Wilson 			if (wholedisk) {
20233fdda499SJohn Harres 				int slen = strlen(srchval);
20243fdda499SJohn Harres 				int vlen = strlen(val);
20253fdda499SJohn Harres 
20263fdda499SJohn Harres 				if (slen != vlen - 2)
20273fdda499SJohn Harres 					break;
20283fdda499SJohn Harres 
20293fdda499SJohn Harres 				/*
20303fdda499SJohn Harres 				 * make_leaf_vdev() should only set
20313fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
20323fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
20333fdda499SJohn Harres 				 * room for the indices used next.
20343fdda499SJohn Harres 				 */
20353fdda499SJohn Harres 				ASSERT(vlen >= 6);
20363fdda499SJohn Harres 
20373fdda499SJohn Harres 				/*
20383fdda499SJohn Harres 				 * strings identical except trailing "s0"
20393fdda499SJohn Harres 				 */
20407855d95bSToomas Soome 				if ((strcmp(&val[vlen - 2], "s0") == 0 ||
20417855d95bSToomas Soome 				    strcmp(&val[vlen - 2], "s1") == 0) &&
20423fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
20433fdda499SJohn Harres 					return (nv);
20443fdda499SJohn Harres 
2045573ca77eSGeorge Wilson 				/*
20463fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
2047573ca77eSGeorge Wilson 				 */
20487855d95bSToomas Soome 				if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
20497855d95bSToomas Soome 				    strcmp(&val[vlen - 6], "s1/old") == 0) &&
20503fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
20513fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
2052573ca77eSGeorge Wilson 					return (nv);
20533fdda499SJohn Harres 
2054573ca77eSGeorge Wilson 				break;
2055573ca77eSGeorge Wilson 			}
205688ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
205788ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
205888ecc943SGeorge Wilson 			uint64_t id, vdev_id;
205988ecc943SGeorge Wilson 
206088ecc943SGeorge Wilson 			/*
206188ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
206288ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
206388ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
206488ecc943SGeorge Wilson 			 */
206588ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
206688ecc943SGeorge Wilson 				return (NULL);
206788ecc943SGeorge Wilson 
206888ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
206988ecc943SGeorge Wilson 				free(type);
207088ecc943SGeorge Wilson 				break;
207188ecc943SGeorge Wilson 			}
207288ecc943SGeorge Wilson 			idx = p + 1;
207388ecc943SGeorge Wilson 			*p = '\0';
207488ecc943SGeorge Wilson 
207588ecc943SGeorge Wilson 			/*
207688ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
207788ecc943SGeorge Wilson 			 */
207888ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
207988ecc943SGeorge Wilson 				free(type);
208088ecc943SGeorge Wilson 				break;
208188ecc943SGeorge Wilson 			}
208288ecc943SGeorge Wilson 
2083*2ba5f978SAlan Somers 			verify(zpool_vdev_is_interior(type));
208488ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
208588ecc943SGeorge Wilson 			    &id) == 0);
208688ecc943SGeorge Wilson 
208788ecc943SGeorge Wilson 			errno = 0;
208888ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
208988ecc943SGeorge Wilson 
209088ecc943SGeorge Wilson 			free(type);
209188ecc943SGeorge Wilson 			if (errno != 0)
209288ecc943SGeorge Wilson 				return (NULL);
209388ecc943SGeorge Wilson 
209488ecc943SGeorge Wilson 			/*
209588ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
209688ecc943SGeorge Wilson 			 */
209788ecc943SGeorge Wilson 			if (vdev_id == id)
209888ecc943SGeorge Wilson 				return (nv);
2099ea8dc4b6Seschrock 		}
2100573ca77eSGeorge Wilson 
2101573ca77eSGeorge Wilson 		/*
2102573ca77eSGeorge Wilson 		 * Common case
2103573ca77eSGeorge Wilson 		 */
2104573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
2105573ca77eSGeorge Wilson 			return (nv);
2106573ca77eSGeorge Wilson 		break;
2107573ca77eSGeorge Wilson 	}
2108573ca77eSGeorge Wilson 
2109573ca77eSGeorge Wilson 	default:
2110573ca77eSGeorge Wilson 		break;
2111ea8dc4b6Seschrock 	}
2112ea8dc4b6Seschrock 
2113ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2114ea8dc4b6Seschrock 	    &child, &children) != 0)
211599653d4eSeschrock 		return (NULL);
2116ea8dc4b6Seschrock 
2117ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
2118573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2119ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
2120ee0eb9f2SEric Schrock 			/*
2121ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
2122ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
2123ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
2124ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
2125ee0eb9f2SEric Schrock 			 */
2126ee0eb9f2SEric Schrock 			if (log != NULL &&
2127ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
2128ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2129ee0eb9f2SEric Schrock 			    is_log) {
2130ee0eb9f2SEric Schrock 				*log = B_TRUE;
2131ee0eb9f2SEric Schrock 			}
2132ea8dc4b6Seschrock 			return (ret);
2133ee0eb9f2SEric Schrock 		}
2134ee0eb9f2SEric Schrock 	}
2135ea8dc4b6Seschrock 
213699653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
213799653d4eSeschrock 	    &child, &children) == 0) {
213899653d4eSeschrock 		for (c = 0; c < children; c++) {
2139573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2140ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2141a43d325bSek 				*avail_spare = B_TRUE;
214299653d4eSeschrock 				return (ret);
214399653d4eSeschrock 			}
214499653d4eSeschrock 		}
214599653d4eSeschrock 	}
214699653d4eSeschrock 
2147fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2148fa94a07fSbrendan 	    &child, &children) == 0) {
2149fa94a07fSbrendan 		for (c = 0; c < children; c++) {
2150573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2151ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2152fa94a07fSbrendan 				*l2cache = B_TRUE;
2153fa94a07fSbrendan 				return (ret);
2154fa94a07fSbrendan 			}
2155fa94a07fSbrendan 		}
2156fa94a07fSbrendan 	}
2157fa94a07fSbrendan 
215899653d4eSeschrock 	return (NULL);
2159ea8dc4b6Seschrock }
2160ea8dc4b6Seschrock 
2161573ca77eSGeorge Wilson /*
2162573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
2163573ca77eSGeorge Wilson  * associated vdev.
2164573ca77eSGeorge Wilson  */
2165573ca77eSGeorge Wilson nvlist_t *
2166573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2167573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2168573ca77eSGeorge Wilson {
2169573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
2170573ca77eSGeorge Wilson 
2171573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2172573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2173573ca77eSGeorge Wilson 
2174573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2175573ca77eSGeorge Wilson 	    &nvroot) == 0);
2176573ca77eSGeorge Wilson 
2177573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
2178cb04b873SMark J Musante 	*l2cache = B_FALSE;
2179daeb70e5SMark J Musante 	if (log != NULL)
2180daeb70e5SMark J Musante 		*log = B_FALSE;
2181573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2182573ca77eSGeorge Wilson 	nvlist_free(search);
2183573ca77eSGeorge Wilson 
2184573ca77eSGeorge Wilson 	return (ret);
2185573ca77eSGeorge Wilson }
2186573ca77eSGeorge Wilson 
218788ecc943SGeorge Wilson /*
218888ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
218988ecc943SGeorge Wilson  */
2190*2ba5f978SAlan Somers static boolean_t
219188ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
219288ecc943SGeorge Wilson {
219388ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2194*2ba5f978SAlan Somers 	    strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2195*2ba5f978SAlan Somers 	    strncmp(name,
2196*2ba5f978SAlan Somers 	    VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
219788ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
219888ecc943SGeorge Wilson 		return (B_TRUE);
219988ecc943SGeorge Wilson 	return (B_FALSE);
220088ecc943SGeorge Wilson }
220188ecc943SGeorge Wilson 
220299653d4eSeschrock nvlist_t *
2203fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2204ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
2205ea8dc4b6Seschrock {
2206ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
2207ea8dc4b6Seschrock 	char *end;
2208573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
2209ea8dc4b6Seschrock 	uint64_t guid;
2210ea8dc4b6Seschrock 
2211573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2212573ca77eSGeorge Wilson 
22130917b783Seschrock 	guid = strtoull(path, &end, 10);
2214ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
2215573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
221688ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
221788ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2218ea8dc4b6Seschrock 	} else if (path[0] != '/') {
22196401734dSWill Andrews 		(void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT,
22206401734dSWill Andrews 		    path);
2221573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2222ea8dc4b6Seschrock 	} else {
2223573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2224ea8dc4b6Seschrock 	}
2225ea8dc4b6Seschrock 
2226ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2227ea8dc4b6Seschrock 	    &nvroot) == 0);
2228ea8dc4b6Seschrock 
2229a43d325bSek 	*avail_spare = B_FALSE;
2230fa94a07fSbrendan 	*l2cache = B_FALSE;
2231ee0eb9f2SEric Schrock 	if (log != NULL)
2232ee0eb9f2SEric Schrock 		*log = B_FALSE;
2233573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2234573ca77eSGeorge Wilson 	nvlist_free(search);
2235573ca77eSGeorge Wilson 
2236573ca77eSGeorge Wilson 	return (ret);
2237a43d325bSek }
2238a43d325bSek 
223919397407SSherry Moore static int
224019397407SSherry Moore vdev_online(nvlist_t *nv)
224119397407SSherry Moore {
224219397407SSherry Moore 	uint64_t ival;
224319397407SSherry Moore 
224419397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
224519397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
224619397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
224719397407SSherry Moore 		return (0);
224819397407SSherry Moore 
224919397407SSherry Moore 	return (1);
225019397407SSherry Moore }
225119397407SSherry Moore 
225219397407SSherry Moore /*
225321ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
225419397407SSherry Moore  */
2255753a6d45SSherry Moore static int
225621ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2257753a6d45SSherry Moore     size_t *bytes_written)
225819397407SSherry Moore {
2259753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
2260753a6d45SSherry Moore 	char *tmppath;
2261753a6d45SSherry Moore 	const char *format;
2262753a6d45SSherry Moore 
2263753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2264753a6d45SSherry Moore 	    &tmppath) != 0)
2265753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2266753a6d45SSherry Moore 
2267753a6d45SSherry Moore 	pos = *bytes_written;
2268753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
2269753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
2270753a6d45SSherry Moore 
2271753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2272753a6d45SSherry Moore 	*bytes_written += rsz;
2273753a6d45SSherry Moore 
2274753a6d45SSherry Moore 	if (rsz >= bytes_left) {
2275753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2276753a6d45SSherry Moore 		if (bytes_left != 0) {
2277753a6d45SSherry Moore 			physpath[pos] = 0;
2278753a6d45SSherry Moore 		}
2279753a6d45SSherry Moore 		return (EZFS_NOSPC);
2280753a6d45SSherry Moore 	}
2281753a6d45SSherry Moore 	return (0);
2282753a6d45SSherry Moore }
2283753a6d45SSherry Moore 
228421ecdf64SLin Ling static int
228521ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
228621ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
228721ecdf64SLin Ling {
228821ecdf64SLin Ling 	char *type;
228921ecdf64SLin Ling 	int ret;
229021ecdf64SLin Ling 
229121ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
229221ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
229321ecdf64SLin Ling 
229421ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
229521ecdf64SLin Ling 		/*
229621ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
229721ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
229821ecdf64SLin Ling 		 * spare device.
229921ecdf64SLin Ling 		 */
230021ecdf64SLin Ling 		if (is_spare) {
230121ecdf64SLin Ling 			uint64_t spare = 0;
230221ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
230321ecdf64SLin Ling 			    &spare);
230421ecdf64SLin Ling 			if (!spare)
230521ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
230621ecdf64SLin Ling 		}
230721ecdf64SLin Ling 
230821ecdf64SLin Ling 		if (vdev_online(nv)) {
230921ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
231021ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
231121ecdf64SLin Ling 				return (ret);
231221ecdf64SLin Ling 		}
231321ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2314d5f26ad8SToomas Soome 	    strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
231521ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
231621ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
231721ecdf64SLin Ling 		nvlist_t **child;
231821ecdf64SLin Ling 		uint_t count;
231921ecdf64SLin Ling 		int i, ret;
232021ecdf64SLin Ling 
232121ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
232221ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
232321ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
232421ecdf64SLin Ling 
232521ecdf64SLin Ling 		for (i = 0; i < count; i++) {
232621ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
232721ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
232821ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
232921ecdf64SLin Ling 				return (ret);
233021ecdf64SLin Ling 		}
233121ecdf64SLin Ling 	}
233221ecdf64SLin Ling 
233321ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
233421ecdf64SLin Ling }
233521ecdf64SLin Ling 
2336753a6d45SSherry Moore /*
2337753a6d45SSherry Moore  * Get phys_path for a root pool config.
2338753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2339753a6d45SSherry Moore  */
2340753a6d45SSherry Moore static int
2341753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2342753a6d45SSherry Moore {
2343753a6d45SSherry Moore 	size_t rsz;
234419397407SSherry Moore 	nvlist_t *vdev_root;
234519397407SSherry Moore 	nvlist_t **child;
234619397407SSherry Moore 	uint_t count;
2347753a6d45SSherry Moore 	char *type;
2348753a6d45SSherry Moore 
2349753a6d45SSherry Moore 	rsz = 0;
2350753a6d45SSherry Moore 
2351753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2352753a6d45SSherry Moore 	    &vdev_root) != 0)
2353753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2354753a6d45SSherry Moore 
2355753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2356753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2357753a6d45SSherry Moore 	    &child, &count) != 0)
2358753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
235919397407SSherry Moore 
236019397407SSherry Moore 	/*
23611a902ef8SHans Rosenfeld 	 * root pool can only have a single top-level vdev.
236219397407SSherry Moore 	 */
23631a902ef8SHans Rosenfeld 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2364753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
236519397407SSherry Moore 
236621ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
236721ecdf64SLin Ling 	    B_FALSE);
236819397407SSherry Moore 
2369753a6d45SSherry Moore 	/* No online devices */
2370753a6d45SSherry Moore 	if (rsz == 0)
2371753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2372753a6d45SSherry Moore 
237319397407SSherry Moore 	return (0);
237419397407SSherry Moore }
237519397407SSherry Moore 
2376753a6d45SSherry Moore /*
2377753a6d45SSherry Moore  * Get phys_path for a root pool
2378753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2379753a6d45SSherry Moore  */
2380753a6d45SSherry Moore int
2381753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2382753a6d45SSherry Moore {
2383753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2384753a6d45SSherry Moore 	    phypath_size));
2385753a6d45SSherry Moore }
2386753a6d45SSherry Moore 
2387573ca77eSGeorge Wilson /*
2388573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2389573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2390573ca77eSGeorge Wilson  */
2391573ca77eSGeorge Wilson static int
2392573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2393573ca77eSGeorge Wilson {
2394573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2395573ca77eSGeorge Wilson 	char errbuf[1024];
2396573ca77eSGeorge Wilson 	int fd, error;
2397573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2398573ca77eSGeorge Wilson 
2399573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2400573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2401573ca77eSGeorge Wilson 		return (-1);
2402573ca77eSGeorge Wilson 
24036401734dSWill Andrews 	(void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2404573ca77eSGeorge Wilson 
2405573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2406573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2407573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2408573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2409573ca77eSGeorge Wilson 	}
2410573ca77eSGeorge Wilson 
2411573ca77eSGeorge Wilson 	/*
2412573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2413573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2414573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2415573ca77eSGeorge Wilson 	 */
2416573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2417573ca77eSGeorge Wilson 	(void) close(fd);
2418573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2419573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2420573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2421573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2422573ca77eSGeorge Wilson 	}
2423573ca77eSGeorge Wilson 	return (0);
2424573ca77eSGeorge Wilson }
2425573ca77eSGeorge Wilson 
2426fa9e4066Sahrens /*
24273d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
24283d7072f8Seschrock  * ZFS_ONLINE_* flags.
2429fa9e4066Sahrens  */
2430fa9e4066Sahrens int
24313d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
24323d7072f8Seschrock     vdev_state_t *newstate)
2433fa9e4066Sahrens {
2434fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2435fa9e4066Sahrens 	char msg[1024];
24369a551dd6SYuri Pankov 	char *pathname;
243799653d4eSeschrock 	nvlist_t *tgt;
2438573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
243999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2440fa9e4066Sahrens 
2441573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2442573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2443573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2444573ca77eSGeorge Wilson 	} else {
2445573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2446573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2447573ca77eSGeorge Wilson 	}
2448ea8dc4b6Seschrock 
2449fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2450ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2451573ca77eSGeorge Wilson 	    &islog)) == NULL)
245299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2453fa9e4066Sahrens 
245499653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2455fa9e4066Sahrens 
2456069f55e2SEric Schrock 	if (avail_spare)
2457a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2458a43d325bSek 
24599a551dd6SYuri Pankov 	if ((flags & ZFS_ONLINE_EXPAND ||
24609a551dd6SYuri Pankov 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
24619a551dd6SYuri Pankov 	    nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2462573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2463573ca77eSGeorge Wilson 
2464573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2465573ca77eSGeorge Wilson 		    &wholedisk);
2466573ca77eSGeorge Wilson 
2467573ca77eSGeorge Wilson 		/*
2468573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2469573ca77eSGeorge Wilson 		 */
2470573ca77eSGeorge Wilson 		if (l2cache) {
2471573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2472573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2473573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2474573ca77eSGeorge Wilson 		}
2475573ca77eSGeorge Wilson 
2476573ca77eSGeorge Wilson 		if (wholedisk) {
24776401734dSWill Andrews 			pathname += strlen(ZFS_DISK_ROOT) + 1;
2478cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2479573ca77eSGeorge Wilson 		}
2480573ca77eSGeorge Wilson 	}
2481573ca77eSGeorge Wilson 
24823d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
24833d7072f8Seschrock 	zc.zc_obj = flags;
2484fa9e4066Sahrens 
2485cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
24861195e687SMark J Musante 		if (errno == EINVAL) {
24871195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
24881195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
24891195e687SMark J Musante 			    "instead"), "zpool detach");
24901195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
24911195e687SMark J Musante 		}
24923d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24931195e687SMark J Musante 	}
24943d7072f8Seschrock 
24953d7072f8Seschrock 	*newstate = zc.zc_cookie;
24963d7072f8Seschrock 	return (0);
2497fa9e4066Sahrens }
2498fa9e4066Sahrens 
2499fa9e4066Sahrens /*
2500fa9e4066Sahrens  * Take the specified vdev offline
2501fa9e4066Sahrens  */
2502fa9e4066Sahrens int
25033d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2504fa9e4066Sahrens {
2505fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2506fa9e4066Sahrens 	char msg[1024];
250799653d4eSeschrock 	nvlist_t *tgt;
2508fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
250999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2510fa9e4066Sahrens 
2511ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2512ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2513ea8dc4b6Seschrock 
2514fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2515ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2516ee0eb9f2SEric Schrock 	    NULL)) == NULL)
251799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
251899653d4eSeschrock 
251999653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2520fa9e4066Sahrens 
2521069f55e2SEric Schrock 	if (avail_spare)
2522a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2523a43d325bSek 
25243d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
25253d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
25263d7072f8Seschrock 
2527cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
25283d7072f8Seschrock 		return (0);
25293d7072f8Seschrock 
25303d7072f8Seschrock 	switch (errno) {
25313d7072f8Seschrock 	case EBUSY:
25323d7072f8Seschrock 
25333d7072f8Seschrock 		/*
25343d7072f8Seschrock 		 * There are no other replicas of this device.
25353d7072f8Seschrock 		 */
25363d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
25373d7072f8Seschrock 
2538e6ca193dSGeorge Wilson 	case EEXIST:
2539e6ca193dSGeorge Wilson 		/*
2540e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2541e6ca193dSGeorge Wilson 		 */
2542e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2543e6ca193dSGeorge Wilson 
25443d7072f8Seschrock 	default:
25453d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
25463d7072f8Seschrock 	}
25473d7072f8Seschrock }
25483d7072f8Seschrock 
25493d7072f8Seschrock /*
25503d7072f8Seschrock  * Mark the given vdev faulted.
25513d7072f8Seschrock  */
25523d7072f8Seschrock int
2553069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25543d7072f8Seschrock {
25553d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25563d7072f8Seschrock 	char msg[1024];
25573d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25583d7072f8Seschrock 
25593d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25603d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2561441d80aaSlling 
25623d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25633d7072f8Seschrock 	zc.zc_guid = guid;
25643d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2565069f55e2SEric Schrock 	zc.zc_obj = aux;
25663d7072f8Seschrock 
2567cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2568fa9e4066Sahrens 		return (0);
2569fa9e4066Sahrens 
2570fa9e4066Sahrens 	switch (errno) {
257199653d4eSeschrock 	case EBUSY:
2572fa9e4066Sahrens 
2573fa9e4066Sahrens 		/*
2574fa9e4066Sahrens 		 * There are no other replicas of this device.
2575fa9e4066Sahrens 		 */
257699653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2577fa9e4066Sahrens 
257899653d4eSeschrock 	default:
257999653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2580fa9e4066Sahrens 	}
25813d7072f8Seschrock 
25823d7072f8Seschrock }
25833d7072f8Seschrock 
25843d7072f8Seschrock /*
25853d7072f8Seschrock  * Mark the given vdev degraded.
25863d7072f8Seschrock  */
25873d7072f8Seschrock int
2588069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25893d7072f8Seschrock {
25903d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25913d7072f8Seschrock 	char msg[1024];
25923d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25933d7072f8Seschrock 
25943d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25953d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
25963d7072f8Seschrock 
25973d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25983d7072f8Seschrock 	zc.zc_guid = guid;
25993d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2600069f55e2SEric Schrock 	zc.zc_obj = aux;
26013d7072f8Seschrock 
2602cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
26033d7072f8Seschrock 		return (0);
26043d7072f8Seschrock 
26053d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
260699653d4eSeschrock }
260799653d4eSeschrock 
260899653d4eSeschrock /*
260999653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
261099653d4eSeschrock  * a hot spare.
261199653d4eSeschrock  */
261299653d4eSeschrock static boolean_t
261399653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
261499653d4eSeschrock {
261599653d4eSeschrock 	nvlist_t **child;
261699653d4eSeschrock 	uint_t c, children;
261799653d4eSeschrock 	char *type;
261899653d4eSeschrock 
261999653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
262099653d4eSeschrock 	    &children) == 0) {
262199653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
262299653d4eSeschrock 		    &type) == 0);
262399653d4eSeschrock 
262499653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
262599653d4eSeschrock 		    children == 2 && child[which] == tgt)
262699653d4eSeschrock 			return (B_TRUE);
262799653d4eSeschrock 
262899653d4eSeschrock 		for (c = 0; c < children; c++)
262999653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
263099653d4eSeschrock 				return (B_TRUE);
263199653d4eSeschrock 	}
263299653d4eSeschrock 
263399653d4eSeschrock 	return (B_FALSE);
2634fa9e4066Sahrens }
2635fa9e4066Sahrens 
2636fa9e4066Sahrens /*
2637fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
26388654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2639fa9e4066Sahrens  */
2640fa9e4066Sahrens int
2641fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2642fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2643fa9e4066Sahrens {
2644fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2645fa9e4066Sahrens 	char msg[1024];
2646fa9e4066Sahrens 	int ret;
264799653d4eSeschrock 	nvlist_t *tgt;
2648ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2649ee0eb9f2SEric Schrock 	uint64_t val;
2650cb04b873SMark J Musante 	char *newname;
265199653d4eSeschrock 	nvlist_t **child;
265299653d4eSeschrock 	uint_t children;
265399653d4eSeschrock 	nvlist_t *config_root;
265499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26554263d13fSGeorge Wilson 	boolean_t rootpool = zpool_is_bootable(zhp);
2656fa9e4066Sahrens 
2657ea8dc4b6Seschrock 	if (replacing)
2658ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2659ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2660ea8dc4b6Seschrock 	else
2661ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2662ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2663ea8dc4b6Seschrock 
2664fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2665ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
26665cabbc6bSPrashanth Sreenivasa 	    &islog)) == NULL)
266799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
266899653d4eSeschrock 
2669a43d325bSek 	if (avail_spare)
267099653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
267199653d4eSeschrock 
2672fa94a07fSbrendan 	if (l2cache)
2673fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2674fa94a07fSbrendan 
267599653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2676fa9e4066Sahrens 	zc.zc_cookie = replacing;
2677fa9e4066Sahrens 
267899653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
267999653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
268099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
268199653d4eSeschrock 		    "new device must be a single disk"));
268299653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
268399653d4eSeschrock 	}
268499653d4eSeschrock 
268599653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
268699653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
268799653d4eSeschrock 
268888ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
26890430f8daSeschrock 		return (-1);
26900430f8daSeschrock 
269199653d4eSeschrock 	/*
269299653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
269399653d4eSeschrock 	 * replace it with another hot spare.
269499653d4eSeschrock 	 */
269599653d4eSeschrock 	if (replacing &&
269699653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2697ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2698ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2699ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
270099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270199653d4eSeschrock 		    "can only be replaced by another hot spare"));
27020430f8daSeschrock 		free(newname);
270399653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
270499653d4eSeschrock 	}
270599653d4eSeschrock 
27060430f8daSeschrock 	free(newname);
27070430f8daSeschrock 
2708990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
270999653d4eSeschrock 		return (-1);
2710fa9e4066Sahrens 
2711cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2712fa9e4066Sahrens 
2713e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2714fa9e4066Sahrens 
2715b5b76fecSGeorge Wilson 	if (ret == 0) {
2716b5b76fecSGeorge Wilson 		if (rootpool) {
271721ecdf64SLin Ling 			/*
271821ecdf64SLin Ling 			 * XXX need a better way to prevent user from
271921ecdf64SLin Ling 			 * booting up a half-baked vdev.
272021ecdf64SLin Ling 			 */
272121ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
272221ecdf64SLin Ling 			    "sure to wait until resilver is done "
272321ecdf64SLin Ling 			    "before rebooting.\n"));
2724b5b76fecSGeorge Wilson 		}
2725fa9e4066Sahrens 		return (0);
2726b5b76fecSGeorge Wilson 	}
2727fa9e4066Sahrens 
2728fa9e4066Sahrens 	switch (errno) {
2729ea8dc4b6Seschrock 	case ENOTSUP:
2730fa9e4066Sahrens 		/*
2731fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2732fa9e4066Sahrens 		 */
27338654d025Sperrin 		if (replacing) {
2734cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2735cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2736cb04b873SMark J Musante 
2737ee0eb9f2SEric Schrock 			if (islog)
27388654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27398654d025Sperrin 				    "cannot replace a log with a spare"));
2740cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2741cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2742cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2743cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
27448654d025Sperrin 			else
27458654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27468654d025Sperrin 				    "cannot replace a replacing device"));
27478654d025Sperrin 		} else {
274899653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
274999653d4eSeschrock 			    "can only attach to mirrors and top-level "
275099653d4eSeschrock 			    "disks"));
27518654d025Sperrin 		}
275299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2753fa9e4066Sahrens 		break;
2754fa9e4066Sahrens 
2755ea8dc4b6Seschrock 	case EINVAL:
2756fa9e4066Sahrens 		/*
2757fa9e4066Sahrens 		 * The new device must be a single disk.
2758fa9e4066Sahrens 		 */
275999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
276099653d4eSeschrock 		    "new device must be a single disk"));
276199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2762fa9e4066Sahrens 		break;
2763fa9e4066Sahrens 
2764ea8dc4b6Seschrock 	case EBUSY:
27655cabbc6bSPrashanth Sreenivasa 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
27665cabbc6bSPrashanth Sreenivasa 		    "or pool has removing/removed vdevs"),
276799653d4eSeschrock 		    new_disk);
276899653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2769fa9e4066Sahrens 		break;
2770fa9e4066Sahrens 
2771ea8dc4b6Seschrock 	case EOVERFLOW:
2772fa9e4066Sahrens 		/*
2773fa9e4066Sahrens 		 * The new device is too small.
2774fa9e4066Sahrens 		 */
277599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
277699653d4eSeschrock 		    "device is too small"));
277799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2778fa9e4066Sahrens 		break;
2779fa9e4066Sahrens 
2780ea8dc4b6Seschrock 	case EDOM:
2781fa9e4066Sahrens 		/*
2782fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2783fa9e4066Sahrens 		 */
278499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
278599653d4eSeschrock 		    "devices have different sector alignment"));
278699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2787fa9e4066Sahrens 		break;
2788fa9e4066Sahrens 
2789ea8dc4b6Seschrock 	case ENAMETOOLONG:
2790fa9e4066Sahrens 		/*
2791fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2792fa9e4066Sahrens 		 */
279399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2794fa9e4066Sahrens 		break;
2795fa9e4066Sahrens 
2796ea8dc4b6Seschrock 	default:
279799653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2798fa9e4066Sahrens 	}
2799fa9e4066Sahrens 
280099653d4eSeschrock 	return (-1);
2801fa9e4066Sahrens }
2802fa9e4066Sahrens 
2803fa9e4066Sahrens /*
2804fa9e4066Sahrens  * Detach the specified device.
2805fa9e4066Sahrens  */
2806fa9e4066Sahrens int
2807fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2808fa9e4066Sahrens {
2809fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2810fa9e4066Sahrens 	char msg[1024];
281199653d4eSeschrock 	nvlist_t *tgt;
2812fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
281399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2814fa9e4066Sahrens 
2815ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2816ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2817ea8dc4b6Seschrock 
2818fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2819ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
28205cabbc6bSPrashanth Sreenivasa 	    NULL)) == NULL)
282199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2822fa9e4066Sahrens 
2823a43d325bSek 	if (avail_spare)
282499653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
282599653d4eSeschrock 
2826fa94a07fSbrendan 	if (l2cache)
2827fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2828fa94a07fSbrendan 
282999653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
283099653d4eSeschrock 
2831ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2832fa9e4066Sahrens 		return (0);
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 	switch (errno) {
2835fa9e4066Sahrens 
2836ea8dc4b6Seschrock 	case ENOTSUP:
2837fa9e4066Sahrens 		/*
2838fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2839fa9e4066Sahrens 		 */
284099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
284199653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2842cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2843fa9e4066Sahrens 		break;
2844fa9e4066Sahrens 
2845ea8dc4b6Seschrock 	case EBUSY:
2846fa9e4066Sahrens 		/*
2847fa9e4066Sahrens 		 * There are no other replicas of this device.
2848fa9e4066Sahrens 		 */
284999653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2850fa9e4066Sahrens 		break;
2851fa9e4066Sahrens 
2852ea8dc4b6Seschrock 	default:
285399653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2854ea8dc4b6Seschrock 	}
2855ea8dc4b6Seschrock 
285699653d4eSeschrock 	return (-1);
285799653d4eSeschrock }
285899653d4eSeschrock 
28591195e687SMark J Musante /*
28601195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
28611195e687SMark J Musante  *
28621195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
28631195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
28641195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
28651195e687SMark J Musante  * see if any entry in the schild array matches.
28661195e687SMark J Musante  *
28671195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
28681195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
28691195e687SMark J Musante  */
28701195e687SMark J Musante static int
28711195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
28721195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
28731195e687SMark J Musante {
28741195e687SMark J Musante 	uint_t mc;
28751195e687SMark J Musante 
28761195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
28771195e687SMark J Musante 		uint_t sc;
28781195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28791195e687SMark J Musante 		    mchild[mc], B_FALSE);
28801195e687SMark J Musante 
28811195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
28821195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28831195e687SMark J Musante 			    schild[sc], B_FALSE);
28841195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
28851195e687SMark J Musante 
28861195e687SMark J Musante 			free(spath);
28871195e687SMark J Musante 			if (result) {
28881195e687SMark J Musante 				free(mpath);
28891195e687SMark J Musante 				return (mc);
28901195e687SMark J Musante 			}
28911195e687SMark J Musante 		}
28921195e687SMark J Musante 
28931195e687SMark J Musante 		free(mpath);
28941195e687SMark J Musante 	}
28951195e687SMark J Musante 
28961195e687SMark J Musante 	return (-1);
28971195e687SMark J Musante }
28981195e687SMark J Musante 
28991195e687SMark J Musante /*
29001195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
29011195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
29021195e687SMark J Musante  */
29031195e687SMark J Musante int
29041195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
29051195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
29061195e687SMark J Musante {
29071195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
29081195e687SMark J Musante 	char msg[1024];
29091195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
29101195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
29111195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
29121195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
29131195e687SMark J Musante 	uint64_t vers;
29141195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
29151195e687SMark J Musante 	int retval = 0;
29161195e687SMark J Musante 
29171195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
29181195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
29191195e687SMark J Musante 
29201195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
29211195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
29221195e687SMark J Musante 
29231195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
29241195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
29251195e687SMark J Musante 		    "retrieve pool configuration\n"));
29261195e687SMark J Musante 		return (-1);
29271195e687SMark J Musante 	}
29281195e687SMark J Musante 
29291195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
29301195e687SMark J Musante 	    == 0);
29311195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
29321195e687SMark J Musante 
29331195e687SMark J Musante 	if (props) {
2934f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
29351195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2936f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
29371195e687SMark J Musante 			return (-1);
29381195e687SMark J Musante 	}
29391195e687SMark J Musante 
29401195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
29411195e687SMark J Musante 	    &children) != 0) {
29421195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29431195e687SMark J Musante 		    "Source pool is missing vdev tree"));
2944aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(zc_props);
29451195e687SMark J Musante 		return (-1);
29461195e687SMark J Musante 	}
29471195e687SMark J Musante 
29481195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
29491195e687SMark J Musante 	vcount = 0;
29501195e687SMark J Musante 
29511195e687SMark J Musante 	if (*newroot == NULL ||
29521195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
29531195e687SMark J Musante 	    &newchild, &newchildren) != 0)
29541195e687SMark J Musante 		newchildren = 0;
29551195e687SMark J Musante 
29561195e687SMark J Musante 	for (c = 0; c < children; c++) {
29571195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
29581195e687SMark J Musante 		char *type;
29591195e687SMark J Musante 		nvlist_t **mchild, *vdev;
29601195e687SMark J Musante 		uint_t mchildren;
29611195e687SMark J Musante 		int entry;
29621195e687SMark J Musante 
29631195e687SMark J Musante 		/*
29641195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
29651195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
29661195e687SMark J Musante 		 */
29671195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
29681195e687SMark J Musante 		    &is_log);
29691195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
29701195e687SMark J Musante 		    &is_hole);
29711195e687SMark J Musante 		if (is_log || is_hole) {
29721195e687SMark J Musante 			/*
29731195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
29741195e687SMark J Musante 			 */
29751195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
29761195e687SMark J Musante 				goto out;
29771195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
29781195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
29791195e687SMark J Musante 				goto out;
29801195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
29811195e687SMark J Musante 			    1) != 0)
29821195e687SMark J Musante 				goto out;
29831195e687SMark J Musante 			if (lastlog == 0)
29841195e687SMark J Musante 				lastlog = vcount;
29851195e687SMark J Musante 			varray[vcount++] = vdev;
29861195e687SMark J Musante 			continue;
29871195e687SMark J Musante 		}
29881195e687SMark J Musante 		lastlog = 0;
29891195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
29901195e687SMark J Musante 		    == 0);
29911195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
29921195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29931195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
29941195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29951195e687SMark J Musante 			goto out;
29961195e687SMark J Musante 		}
29971195e687SMark J Musante 
29981195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
29991195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
30001195e687SMark J Musante 
30011195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
30021195e687SMark J Musante 		if (newchildren > 0 &&
30031195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
30041195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
30051195e687SMark J Musante 			/* We found a disk that the user specified. */
30061195e687SMark J Musante 			vdev = mchild[entry];
30071195e687SMark J Musante 			++found;
30081195e687SMark J Musante 		} else {
30091195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
30101195e687SMark J Musante 			vdev = mchild[mchildren - 1];
30111195e687SMark J Musante 		}
30121195e687SMark J Musante 
30131195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
30141195e687SMark J Musante 			goto out;
30151195e687SMark J Musante 	}
30161195e687SMark J Musante 
30171195e687SMark J Musante 	/* did we find every disk the user specified? */
30181195e687SMark J Musante 	if (found != newchildren) {
30191195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
30201195e687SMark J Musante 		    "include at most one disk from each mirror"));
30211195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
30221195e687SMark J Musante 		goto out;
30231195e687SMark J Musante 	}
30241195e687SMark J Musante 
30251195e687SMark J Musante 	/* Prepare the nvlist for populating. */
30261195e687SMark J Musante 	if (*newroot == NULL) {
30271195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
30281195e687SMark J Musante 			goto out;
30291195e687SMark J Musante 		freelist = B_TRUE;
30301195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
30311195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
30321195e687SMark J Musante 			goto out;
30331195e687SMark J Musante 	} else {
30341195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
30351195e687SMark J Musante 	}
30361195e687SMark J Musante 
30371195e687SMark J Musante 	/* Add all the children we found */
30381195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
30391195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
30401195e687SMark J Musante 		goto out;
30411195e687SMark J Musante 
30421195e687SMark J Musante 	/*
30431195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
30441195e687SMark J Musante 	 */
30451195e687SMark J Musante 	if (flags.dryrun) {
30461195e687SMark J Musante 		memory_err = B_FALSE;
30471195e687SMark J Musante 		freelist = B_FALSE;
30481195e687SMark J Musante 		goto out;
30491195e687SMark J Musante 	}
30501195e687SMark J Musante 
30511195e687SMark J Musante 	/* now build up the config list & call the ioctl */
30521195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
30531195e687SMark J Musante 		goto out;
30541195e687SMark J Musante 
30551195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
30561195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
30571195e687SMark J Musante 	    nvlist_add_string(newconfig,
30581195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
30591195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
30601195e687SMark J Musante 		goto out;
30611195e687SMark J Musante 
30621195e687SMark J Musante 	/*
30631195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
30641195e687SMark J Musante 	 * explicitly export it.
30651195e687SMark J Musante 	 */
30661195e687SMark J Musante 	if (!flags.import)
30671195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
30681195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30691195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
30701195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
30711195e687SMark J Musante 		goto out;
30721195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
30731195e687SMark J Musante 		goto out;
30741195e687SMark J Musante 
30751195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
30761195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
30771195e687SMark J Musante 		goto out;
30781195e687SMark J Musante 	}
30791195e687SMark J Musante 
30801195e687SMark J Musante 	freelist = B_FALSE;
30811195e687SMark J Musante 	memory_err = B_FALSE;
30821195e687SMark J Musante 
30831195e687SMark J Musante out:
30841195e687SMark J Musante 	if (varray != NULL) {
30851195e687SMark J Musante 		int v;
30861195e687SMark J Musante 
30871195e687SMark J Musante 		for (v = 0; v < vcount; v++)
30881195e687SMark J Musante 			nvlist_free(varray[v]);
30891195e687SMark J Musante 		free(varray);
30901195e687SMark J Musante 	}
30911195e687SMark J Musante 	zcmd_free_nvlists(&zc);
3092aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zc_props);
3093aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(newconfig);
30941195e687SMark J Musante 	if (freelist) {
30951195e687SMark J Musante 		nvlist_free(*newroot);
30961195e687SMark J Musante 		*newroot = NULL;
30971195e687SMark J Musante 	}
30981195e687SMark J Musante 
30991195e687SMark J Musante 	if (retval != 0)
31001195e687SMark J Musante 		return (retval);
31011195e687SMark J Musante 
31021195e687SMark J Musante 	if (memory_err)
31031195e687SMark J Musante 		return (no_memory(hdl));
31041195e687SMark J Musante 
31051195e687SMark J Musante 	return (0);
31061195e687SMark J Musante }
31071195e687SMark J Musante 
310899653d4eSeschrock /*
31095cabbc6bSPrashanth Sreenivasa  * Remove the given device.
311099653d4eSeschrock  */
311199653d4eSeschrock int
311299653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
311399653d4eSeschrock {
311499653d4eSeschrock 	zfs_cmd_t zc = { 0 };
311599653d4eSeschrock 	char msg[1024];
311699653d4eSeschrock 	nvlist_t *tgt;
311788ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
311899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
311988ecc943SGeorge Wilson 	uint64_t version;
312099653d4eSeschrock 
312199653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
312299653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
312399653d4eSeschrock 
312499653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3125ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
31265cabbc6bSPrashanth Sreenivasa 	    &islog)) == NULL)
312799653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
312899653d4eSeschrock 
312988ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
313088ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
313188ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31325cabbc6bSPrashanth Sreenivasa 		    "pool must be upgraded to support log removal"));
313388ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
313488ecc943SGeorge Wilson 	}
313588ecc943SGeorge Wilson 
31365cabbc6bSPrashanth Sreenivasa 	if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
31375cabbc6bSPrashanth Sreenivasa 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31385cabbc6bSPrashanth Sreenivasa 		    "root pool can not have removed devices, "
31395cabbc6bSPrashanth Sreenivasa 		    "because GRUB does not understand them"));
31405cabbc6bSPrashanth Sreenivasa 		return (zfs_error(hdl, EINVAL, msg));
31415cabbc6bSPrashanth Sreenivasa 	}
31425cabbc6bSPrashanth Sreenivasa 
31435cabbc6bSPrashanth Sreenivasa 	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
31445cabbc6bSPrashanth Sreenivasa 
31455cabbc6bSPrashanth Sreenivasa 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
31465cabbc6bSPrashanth Sreenivasa 		return (0);
31475cabbc6bSPrashanth Sreenivasa 
31485cabbc6bSPrashanth Sreenivasa 	switch (errno) {
31495cabbc6bSPrashanth Sreenivasa 
31505cabbc6bSPrashanth Sreenivasa 	case EINVAL:
31515cabbc6bSPrashanth Sreenivasa 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31525cabbc6bSPrashanth Sreenivasa 		    "invalid config; all top-level vdevs must "
31535cabbc6bSPrashanth Sreenivasa 		    "have the same sector size and not be raidz."));
31545cabbc6bSPrashanth Sreenivasa 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
31555cabbc6bSPrashanth Sreenivasa 		break;
31565cabbc6bSPrashanth Sreenivasa 
31575cabbc6bSPrashanth Sreenivasa 	case EBUSY:
31585cabbc6bSPrashanth Sreenivasa 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31595cabbc6bSPrashanth Sreenivasa 		    "Pool busy; removal may already be in progress"));
31605cabbc6bSPrashanth Sreenivasa 		(void) zfs_error(hdl, EZFS_BUSY, msg);
31615cabbc6bSPrashanth Sreenivasa 		break;
31625cabbc6bSPrashanth Sreenivasa 
31635cabbc6bSPrashanth Sreenivasa 	default:
31645cabbc6bSPrashanth Sreenivasa 		(void) zpool_standard_error(hdl, errno, msg);
31655cabbc6bSPrashanth Sreenivasa 	}
31665cabbc6bSPrashanth Sreenivasa 	return (-1);
31675cabbc6bSPrashanth Sreenivasa }
31685cabbc6bSPrashanth Sreenivasa 
31695cabbc6bSPrashanth Sreenivasa int
31705cabbc6bSPrashanth Sreenivasa zpool_vdev_remove_cancel(zpool_handle_t *zhp)
31715cabbc6bSPrashanth Sreenivasa {
31725cabbc6bSPrashanth Sreenivasa 	zfs_cmd_t zc = { 0 };
31735cabbc6bSPrashanth Sreenivasa 	char msg[1024];
31745cabbc6bSPrashanth Sreenivasa 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31755cabbc6bSPrashanth Sreenivasa 
31765cabbc6bSPrashanth Sreenivasa 	(void) snprintf(msg, sizeof (msg),
31775cabbc6bSPrashanth Sreenivasa 	    dgettext(TEXT_DOMAIN, "cannot cancel removal"));
31785cabbc6bSPrashanth Sreenivasa 
31795cabbc6bSPrashanth Sreenivasa 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
31805cabbc6bSPrashanth Sreenivasa 	zc.zc_cookie = 1;
318199653d4eSeschrock 
3182ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
318399653d4eSeschrock 		return (0);
318499653d4eSeschrock 
318599653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3186ea8dc4b6Seschrock }
3187ea8dc4b6Seschrock 
31885cabbc6bSPrashanth Sreenivasa int
31895cabbc6bSPrashanth Sreenivasa zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
31905cabbc6bSPrashanth Sreenivasa     uint64_t *sizep)
31915cabbc6bSPrashanth Sreenivasa {
31925cabbc6bSPrashanth Sreenivasa 	char msg[1024];
31935cabbc6bSPrashanth Sreenivasa 	nvlist_t *tgt;
31945cabbc6bSPrashanth Sreenivasa 	boolean_t avail_spare, l2cache, islog;
31955cabbc6bSPrashanth Sreenivasa 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31965cabbc6bSPrashanth Sreenivasa 
31975cabbc6bSPrashanth Sreenivasa 	(void) snprintf(msg, sizeof (msg),
31985cabbc6bSPrashanth Sreenivasa 	    dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
31995cabbc6bSPrashanth Sreenivasa 	    path);
32005cabbc6bSPrashanth Sreenivasa 
32015cabbc6bSPrashanth Sreenivasa 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
32025cabbc6bSPrashanth Sreenivasa 	    &islog)) == NULL)
32035cabbc6bSPrashanth Sreenivasa 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
32045cabbc6bSPrashanth Sreenivasa 
32055cabbc6bSPrashanth Sreenivasa 	if (avail_spare || l2cache || islog) {
32065cabbc6bSPrashanth Sreenivasa 		*sizep = 0;
32075cabbc6bSPrashanth Sreenivasa 		return (0);
32085cabbc6bSPrashanth Sreenivasa 	}
32095cabbc6bSPrashanth Sreenivasa 
32105cabbc6bSPrashanth Sreenivasa 	if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
32115cabbc6bSPrashanth Sreenivasa 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32125cabbc6bSPrashanth Sreenivasa 		    "indirect size not available"));
32135cabbc6bSPrashanth Sreenivasa 		return (zfs_error(hdl, EINVAL, msg));
32145cabbc6bSPrashanth Sreenivasa 	}
32155cabbc6bSPrashanth Sreenivasa 	return (0);
32165cabbc6bSPrashanth Sreenivasa }
32175cabbc6bSPrashanth Sreenivasa 
3218ea8dc4b6Seschrock /*
3219ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
3220ea8dc4b6Seschrock  */
3221ea8dc4b6Seschrock int
3222468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3223ea8dc4b6Seschrock {
3224ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3225ea8dc4b6Seschrock 	char msg[1024];
322699653d4eSeschrock 	nvlist_t *tgt;
3227468c413aSTim Haley 	zpool_rewind_policy_t policy;
3228fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
322999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3230468c413aSTim Haley 	nvlist_t *nvi = NULL;
32314b964adaSGeorge Wilson 	int error;
3232ea8dc4b6Seschrock 
3233ea8dc4b6Seschrock 	if (path)
3234ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3235ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3236e9dbad6fSeschrock 		    path);
3237ea8dc4b6Seschrock 	else
3238ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3239ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3240ea8dc4b6Seschrock 		    zhp->zpool_name);
3241ea8dc4b6Seschrock 
3242ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
324399653d4eSeschrock 	if (path) {
3244fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
32455cabbc6bSPrashanth Sreenivasa 		    &l2cache, NULL)) == NULL)
324699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3247ea8dc4b6Seschrock 
3248fa94a07fSbrendan 		/*
3249fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
3250fa94a07fSbrendan 		 * error clearing for l2cache devices.
3251fa94a07fSbrendan 		 */
3252a43d325bSek 		if (avail_spare)
325399653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3254ea8dc4b6Seschrock 
325599653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
325699653d4eSeschrock 		    &zc.zc_guid) == 0);
3257fa9e4066Sahrens 	}
3258fa9e4066Sahrens 
3259468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
3260468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
3261468c413aSTim Haley 
326257f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3263468c413aSTim Haley 		return (-1);
3264468c413aSTim Haley 
3265cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3266468c413aSTim Haley 		return (-1);
3267468c413aSTim Haley 
32684b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
32694b964adaSGeorge Wilson 	    errno == ENOMEM) {
32704b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
32714b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
32724b964adaSGeorge Wilson 			return (-1);
32734b964adaSGeorge Wilson 		}
32744b964adaSGeorge Wilson 	}
32754b964adaSGeorge Wilson 
32764b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3277468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
3278468c413aSTim Haley 		if (policy.zrp_request &
3279468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3280468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3281468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
3282468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3283468c413aSTim Haley 			    nvi);
3284468c413aSTim Haley 			nvlist_free(nvi);
3285468c413aSTim Haley 		}
3286468c413aSTim Haley 		zcmd_free_nvlists(&zc);
328799653d4eSeschrock 		return (0);
3288468c413aSTim Haley 	}
328999653d4eSeschrock 
3290468c413aSTim Haley 	zcmd_free_nvlists(&zc);
329199653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3292fa9e4066Sahrens }
3293fa9e4066Sahrens 
32943d7072f8Seschrock /*
32953d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
32963d7072f8Seschrock  */
32973d7072f8Seschrock int
32983d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
32993d7072f8Seschrock {
33003d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
33013d7072f8Seschrock 	char msg[1024];
33023d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
33033d7072f8Seschrock 
33043d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
33053d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
33063d7072f8Seschrock 	    guid);
33073d7072f8Seschrock 
33083d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
33093d7072f8Seschrock 	zc.zc_guid = guid;
331014f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
33113d7072f8Seschrock 
33123d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
33133d7072f8Seschrock 		return (0);
33143d7072f8Seschrock 
33153d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
33163d7072f8Seschrock }
33173d7072f8Seschrock 
3318e9103aaeSGarrett D'Amore /*
3319e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
3320e9103aaeSGarrett D'Amore  */
3321e9103aaeSGarrett D'Amore int
3322e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3323e9103aaeSGarrett D'Amore {
3324e9103aaeSGarrett D'Amore 	char msg[1024];
3325e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3326e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3327e9103aaeSGarrett D'Amore 
3328e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3329e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3330e9103aaeSGarrett D'Amore 
3331e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3332e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3333e9103aaeSGarrett D'Amore 		return (0);
3334e9103aaeSGarrett D'Amore 
3335e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3336e9103aaeSGarrett D'Amore }
3337e9103aaeSGarrett D'Amore 
33384263d13fSGeorge Wilson /*
33394263d13fSGeorge Wilson  * Reopen the pool.
33404263d13fSGeorge Wilson  */
33414263d13fSGeorge Wilson int
33424263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
33434263d13fSGeorge Wilson {
33444263d13fSGeorge Wilson 	zfs_cmd_t zc = { 0 };
33454263d13fSGeorge Wilson 	char msg[1024];
33464263d13fSGeorge Wilson 	libzfs_handle_t *hdl = zhp->zpool_hdl;
33474263d13fSGeorge Wilson 
33484263d13fSGeorge Wilson 	(void) snprintf(msg, sizeof (msg),
33494263d13fSGeorge Wilson 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
33504263d13fSGeorge Wilson 	    zhp->zpool_name);
33514263d13fSGeorge Wilson 
33524263d13fSGeorge Wilson 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
33534263d13fSGeorge Wilson 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
33544263d13fSGeorge Wilson 		return (0);
33554263d13fSGeorge Wilson 	return (zpool_standard_error(hdl, errno, msg));
33564263d13fSGeorge Wilson }
33574263d13fSGeorge Wilson 
3358c67d9675Seschrock /*
3359c67d9675Seschrock  * Convert from a devid string to a path.
3360c67d9675Seschrock  */
3361c67d9675Seschrock static char *
3362c67d9675Seschrock devid_to_path(char *devid_str)
3363c67d9675Seschrock {
3364c67d9675Seschrock 	ddi_devid_t devid;
3365c67d9675Seschrock 	char *minor;
3366c67d9675Seschrock 	char *path;
3367c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3368c67d9675Seschrock 	int ret;
3369c67d9675Seschrock 
3370c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3371c67d9675Seschrock 		return (NULL);
3372c67d9675Seschrock 
3373c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3374c67d9675Seschrock 
3375c67d9675Seschrock 	devid_str_free(minor);
3376c67d9675Seschrock 	devid_free(devid);
3377c67d9675Seschrock 
3378c67d9675Seschrock 	if (ret != 0)
3379c67d9675Seschrock 		return (NULL);
3380c67d9675Seschrock 
3381078266a5SMarcel Telka 	/*
3382078266a5SMarcel Telka 	 * In a case the strdup() fails, we will just return NULL below.
3383078266a5SMarcel Telka 	 */
3384078266a5SMarcel Telka 	path = strdup(list[0].devname);
338599653d4eSeschrock 
3386c67d9675Seschrock 	devid_free_nmlist(list);
3387c67d9675Seschrock 
3388c67d9675Seschrock 	return (path);
3389c67d9675Seschrock }
3390c67d9675Seschrock 
3391c67d9675Seschrock /*
3392c67d9675Seschrock  * Convert from a path to a devid string.
3393c67d9675Seschrock  */
3394c67d9675Seschrock static char *
3395c67d9675Seschrock path_to_devid(const char *path)
3396c67d9675Seschrock {
3397c67d9675Seschrock 	int fd;
3398c67d9675Seschrock 	ddi_devid_t devid;
3399c67d9675Seschrock 	char *minor, *ret;
3400c67d9675Seschrock 
3401c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3402c67d9675Seschrock 		return (NULL);
3403c67d9675Seschrock 
3404c67d9675Seschrock 	minor = NULL;
3405c67d9675Seschrock 	ret = NULL;
3406c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3407c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3408c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3409c67d9675Seschrock 		if (minor != NULL)
3410c67d9675Seschrock 			devid_str_free(minor);
3411c67d9675Seschrock 		devid_free(devid);
3412c67d9675Seschrock 	}
3413c67d9675Seschrock 	(void) close(fd);
3414c67d9675Seschrock 
3415c67d9675Seschrock 	return (ret);
3416c67d9675Seschrock }
3417c67d9675Seschrock 
3418c67d9675Seschrock /*
3419c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3420c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3421c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3422c67d9675Seschrock  */
3423c67d9675Seschrock static void
3424c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3425c67d9675Seschrock {
3426c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3427c67d9675Seschrock 
3428c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3429e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3430c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3431ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3432c67d9675Seschrock 
343399653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3434c67d9675Seschrock }
3435c67d9675Seschrock 
3436c67d9675Seschrock /*
3437c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3438c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3439c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3440c67d9675Seschrock  * trailing 's0' slice name.
3441c67d9675Seschrock  *
3442c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3443c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3444c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3445c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3446c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3447c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3448c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3449c67d9675Seschrock  * of these checks.
3450c67d9675Seschrock  */
3451c67d9675Seschrock char *
345288ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
345388ecc943SGeorge Wilson     boolean_t verbose)
3454c67d9675Seschrock {
3455c67d9675Seschrock 	char *path, *devid;
3456ea8dc4b6Seschrock 	uint64_t value;
3457ea8dc4b6Seschrock 	char buf[64];
34583d7072f8Seschrock 	vdev_stat_t *vs;
34593d7072f8Seschrock 	uint_t vsc;
3460c67d9675Seschrock 
3461ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3462ea8dc4b6Seschrock 	    &value) == 0) {
3463ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3464ea8dc4b6Seschrock 		    &value) == 0);
34655ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
34665ad82045Snd 		    (u_longlong_t)value);
3467ea8dc4b6Seschrock 		path = buf;
3468ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3469c67d9675Seschrock 
34703d7072f8Seschrock 		/*
34713d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
34723d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
34733d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
34743d7072f8Seschrock 		 * effects.
34753d7072f8Seschrock 		 */
34763f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
34773d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
34783d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
34793d7072f8Seschrock 		    zhp != NULL &&
3480c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3481c67d9675Seschrock 			/*
3482c67d9675Seschrock 			 * Determine if the current path is correct.
3483c67d9675Seschrock 			 */
3484c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3485c67d9675Seschrock 
3486c67d9675Seschrock 			if (newdevid == NULL ||
3487c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3488c67d9675Seschrock 				char *newpath;
3489c67d9675Seschrock 
3490c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3491c67d9675Seschrock 					/*
3492c67d9675Seschrock 					 * Update the path appropriately.
3493c67d9675Seschrock 					 */
3494c67d9675Seschrock 					set_path(zhp, nv, newpath);
349599653d4eSeschrock 					if (nvlist_add_string(nv,
349699653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
349799653d4eSeschrock 						verify(nvlist_lookup_string(nv,
349899653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
349999653d4eSeschrock 						    &path) == 0);
3500c67d9675Seschrock 					free(newpath);
3501c67d9675Seschrock 				}
3502c67d9675Seschrock 			}
3503c67d9675Seschrock 
350499653d4eSeschrock 			if (newdevid)
350599653d4eSeschrock 				devid_str_free(newdevid);
3506c67d9675Seschrock 		}
3507c67d9675Seschrock 
35086401734dSWill Andrews 		if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
35096401734dSWill Andrews 			path += strlen(ZFS_DISK_ROOTD);
3510c67d9675Seschrock 
3511c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3512ea8dc4b6Seschrock 		    &value) == 0 && value) {
35133fdda499SJohn Harres 			int pathlen = strlen(path);
351499653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
35153fdda499SJohn Harres 
35163fdda499SJohn Harres 			/*
35177855d95bSToomas Soome 			 * If it starts with c#, and ends with "s0" or "s1",
35187855d95bSToomas Soome 			 * chop the slice off, or if it ends with "s0/old" or
35197855d95bSToomas Soome 			 * "s1/old", remove the slice from the middle.
35203fdda499SJohn Harres 			 */
35213fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
35227855d95bSToomas Soome 				if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
35237855d95bSToomas Soome 				    strcmp(&tmp[pathlen - 2], "s1") == 0) {
35243fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
35253fdda499SJohn Harres 				} else if (pathlen > 6 &&
35267855d95bSToomas Soome 				    (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
35277855d95bSToomas Soome 				    strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
35283fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
35293fdda499SJohn Harres 					    "/old");
35303fdda499SJohn Harres 				}
35313fdda499SJohn Harres 			}
3532c67d9675Seschrock 			return (tmp);
3533c67d9675Seschrock 		}
3534c67d9675Seschrock 	} else {
3535c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
353699653d4eSeschrock 
353799653d4eSeschrock 		/*
353899653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
353999653d4eSeschrock 		 */
354099653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
354199653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
354299653d4eSeschrock 			    &value) == 0);
354399653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
35445ad82045Snd 			    (u_longlong_t)value);
354599653d4eSeschrock 			path = buf;
354699653d4eSeschrock 		}
354788ecc943SGeorge Wilson 
354888ecc943SGeorge Wilson 		/*
354988ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
355088ecc943SGeorge Wilson 		 * naming convention.
355188ecc943SGeorge Wilson 		 */
355288ecc943SGeorge Wilson 		if (verbose) {
355388ecc943SGeorge Wilson 			uint64_t id;
355488ecc943SGeorge Wilson 
355588ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
355688ecc943SGeorge Wilson 			    &id) == 0);
355788ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
355888ecc943SGeorge Wilson 			    (u_longlong_t)id);
355988ecc943SGeorge Wilson 			path = buf;
356088ecc943SGeorge Wilson 		}
3561c67d9675Seschrock 	}
3562c67d9675Seschrock 
356399653d4eSeschrock 	return (zfs_strdup(hdl, path));
3564c67d9675Seschrock }
3565ea8dc4b6Seschrock 
3566ea8dc4b6Seschrock static int
3567a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b)
3568ea8dc4b6Seschrock {
35697802d7bfSMatthew Ahrens 	return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3570ea8dc4b6Seschrock }
3571ea8dc4b6Seschrock 
3572ea8dc4b6Seschrock /*
3573ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3574ea8dc4b6Seschrock  * caller.
3575ea8dc4b6Seschrock  */
3576ea8dc4b6Seschrock int
357755434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3578ea8dc4b6Seschrock {
3579ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3580ea8dc4b6Seschrock 	uint64_t count;
35817802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = NULL;
358255434c77Sek 	int i;
3583ea8dc4b6Seschrock 
3584ea8dc4b6Seschrock 	/*
3585ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3586ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3587ea8dc4b6Seschrock 	 * entire list.
3588ea8dc4b6Seschrock 	 */
3589ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3590ea8dc4b6Seschrock 	    &count) == 0);
359175519f38Sek 	if (count == 0)
359275519f38Sek 		return (0);
3593e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
35947802d7bfSMatthew Ahrens 	    count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
359599653d4eSeschrock 		return (-1);
3596e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3597ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3598ea8dc4b6Seschrock 	for (;;) {
359999653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
360099653d4eSeschrock 		    &zc) != 0) {
3601e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3602ea8dc4b6Seschrock 			if (errno == ENOMEM) {
36037802d7bfSMatthew Ahrens 				void *dst;
36047802d7bfSMatthew Ahrens 
3605bf561db0Svb 				count = zc.zc_nvlist_dst_size;
36067802d7bfSMatthew Ahrens 				dst = zfs_alloc(zhp->zpool_hdl, count *
36077802d7bfSMatthew Ahrens 				    sizeof (zbookmark_phys_t));
36087802d7bfSMatthew Ahrens 				if (dst == NULL)
360999653d4eSeschrock 					return (-1);
36107802d7bfSMatthew Ahrens 				zc.zc_nvlist_dst = (uintptr_t)dst;
3611ea8dc4b6Seschrock 			} else {
3612ea8dc4b6Seschrock 				return (-1);
3613ea8dc4b6Seschrock 			}
3614ea8dc4b6Seschrock 		} else {
3615ea8dc4b6Seschrock 			break;
3616ea8dc4b6Seschrock 		}
3617ea8dc4b6Seschrock 	}
3618ea8dc4b6Seschrock 
3619ea8dc4b6Seschrock 	/*
3620ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3621ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3622e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3623ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3624ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3625ea8dc4b6Seschrock 	 */
36267802d7bfSMatthew Ahrens 	zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3627e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3628e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3629ea8dc4b6Seschrock 
3630a2cdcdd2SPaul Dagnelie 	qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3631ea8dc4b6Seschrock 
363255434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3633ea8dc4b6Seschrock 
3634ea8dc4b6Seschrock 	/*
363555434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3636ea8dc4b6Seschrock 	 */
3637ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3638ea8dc4b6Seschrock 		nvlist_t *nv;
3639ea8dc4b6Seschrock 
3640c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
3641c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3642c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
3643ea8dc4b6Seschrock 			continue;
3644ea8dc4b6Seschrock 
364555434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
364655434c77Sek 			goto nomem;
364755434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
364855434c77Sek 		    zb[i].zb_objset) != 0) {
364955434c77Sek 			nvlist_free(nv);
365099653d4eSeschrock 			goto nomem;
3651ea8dc4b6Seschrock 		}
365255434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
365355434c77Sek 		    zb[i].zb_object) != 0) {
365455434c77Sek 			nvlist_free(nv);
365555434c77Sek 			goto nomem;
365655434c77Sek 		}
365755434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
365855434c77Sek 			nvlist_free(nv);
365955434c77Sek 			goto nomem;
366055434c77Sek 		}
366155434c77Sek 		nvlist_free(nv);
3662ea8dc4b6Seschrock 	}
3663ea8dc4b6Seschrock 
36643ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3665ea8dc4b6Seschrock 	return (0);
366699653d4eSeschrock 
366799653d4eSeschrock nomem:
3668e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
366999653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3670ea8dc4b6Seschrock }
3671eaca9bbdSeschrock 
3672eaca9bbdSeschrock /*
3673eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3674eaca9bbdSeschrock  */
3675eaca9bbdSeschrock int
3676990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3677eaca9bbdSeschrock {
3678eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
367999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3680eaca9bbdSeschrock 
3681eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3682990b4856Slling 	zc.zc_cookie = new_version;
3683990b4856Slling 
3684ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3685ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
368699653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
368799653d4eSeschrock 		    zhp->zpool_name));
3688eaca9bbdSeschrock 	return (0);
3689eaca9bbdSeschrock }
369006eeb2adSek 
369106eeb2adSek void
36924445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len)
369306eeb2adSek {
36944445fffbSMatthew Ahrens 	(void) strlcpy(string, basename(argv[0]), len);
36954445fffbSMatthew Ahrens 	for (int i = 1; i < argc; i++) {
36964445fffbSMatthew Ahrens 		(void) strlcat(string, " ", len);
36974445fffbSMatthew Ahrens 		(void) strlcat(string, argv[i], len);
36982a6b87f0Sek 	}
36992a6b87f0Sek }
37002a6b87f0Sek 
37012a6b87f0Sek int
37024445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message)
37032a6b87f0Sek {
37044445fffbSMatthew Ahrens 	zfs_cmd_t zc = { 0 };
37054445fffbSMatthew Ahrens 	nvlist_t *args;
37064445fffbSMatthew Ahrens 	int err;
37074445fffbSMatthew Ahrens 
37084445fffbSMatthew Ahrens 	args = fnvlist_alloc();
37094445fffbSMatthew Ahrens 	fnvlist_add_string(args, "message", message);
37104445fffbSMatthew Ahrens 	err = zcmd_write_src_nvlist(hdl, &zc, args);
37114445fffbSMatthew Ahrens 	if (err == 0)
37124445fffbSMatthew Ahrens 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
37134445fffbSMatthew Ahrens 	nvlist_free(args);
37144445fffbSMatthew Ahrens 	zcmd_free_nvlists(&zc);
37154445fffbSMatthew Ahrens 	return (err);
371606eeb2adSek }
371706eeb2adSek 
371806eeb2adSek /*
371906eeb2adSek  * Perform ioctl to get some command history of a pool.
372006eeb2adSek  *
372106eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
372206eeb2adSek  * logical offset of the history buffer to start reading from.
372306eeb2adSek  *
372406eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
372506eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
372606eeb2adSek  */
372706eeb2adSek static int
372806eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
372906eeb2adSek {
373006eeb2adSek 	zfs_cmd_t zc = { 0 };
373106eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
373206eeb2adSek 
373306eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
373406eeb2adSek 
373506eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
373606eeb2adSek 	zc.zc_history_len = *len;
373706eeb2adSek 	zc.zc_history_offset = *off;
373806eeb2adSek 
373906eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
374006eeb2adSek 		switch (errno) {
374106eeb2adSek 		case EPERM:
3742ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3743ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
374406eeb2adSek 			    "cannot show history for pool '%s'"),
374506eeb2adSek 			    zhp->zpool_name));
374606eeb2adSek 		case ENOENT:
3747ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
374806eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
374906eeb2adSek 			    "'%s'"), zhp->zpool_name));
3750d7306b64Sek 		case ENOTSUP:
3751d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3752d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3753d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
375406eeb2adSek 		default:
3755ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
375606eeb2adSek 			    dgettext(TEXT_DOMAIN,
375706eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
375806eeb2adSek 		}
375906eeb2adSek 	}
376006eeb2adSek 
376106eeb2adSek 	*len = zc.zc_history_len;
376206eeb2adSek 	*off = zc.zc_history_offset;
376306eeb2adSek 
376406eeb2adSek 	return (0);
376506eeb2adSek }
376606eeb2adSek 
376706eeb2adSek /*
376806eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
376906eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
377006eeb2adSek  * processed as there wasn't a complete record.
377106eeb2adSek  */
37728f18d1faSGeorge Wilson int
377306eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
377406eeb2adSek     nvlist_t ***records, uint_t *numrecords)
377506eeb2adSek {
377606eeb2adSek 	uint64_t reclen;
377706eeb2adSek 	nvlist_t *nv;
377806eeb2adSek 	int i;
377906eeb2adSek 
378006eeb2adSek 	while (bytes_read > sizeof (reclen)) {
378106eeb2adSek 
378206eeb2adSek 		/* get length of packed record (stored as little endian) */
378306eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
378406eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
378506eeb2adSek 
378606eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
378706eeb2adSek 			break;
378806eeb2adSek 
378906eeb2adSek 		/* unpack record */
379006eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
379106eeb2adSek 			return (ENOMEM);
379206eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
379306eeb2adSek 		buf += sizeof (reclen) + reclen;
379406eeb2adSek 
379506eeb2adSek 		/* add record to nvlist array */
379606eeb2adSek 		(*numrecords)++;
379706eeb2adSek 		if (ISP2(*numrecords + 1)) {
379806eeb2adSek 			*records = realloc(*records,
379906eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
380006eeb2adSek 		}
380106eeb2adSek 		(*records)[*numrecords - 1] = nv;
380206eeb2adSek 	}
380306eeb2adSek 
380406eeb2adSek 	*leftover = bytes_read;
380506eeb2adSek 	return (0);
380606eeb2adSek }
380706eeb2adSek 
380806eeb2adSek /*
380906eeb2adSek  * Retrieve the command history of a pool.
381006eeb2adSek  */
381106eeb2adSek int
381206eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
381306eeb2adSek {
38143339867aSMatthew Ahrens 	char *buf;
38153339867aSMatthew Ahrens 	int buflen = 128 * 1024;
381606eeb2adSek 	uint64_t off = 0;
381706eeb2adSek 	nvlist_t **records = NULL;
381806eeb2adSek 	uint_t numrecords = 0;
381906eeb2adSek 	int err, i;
382006eeb2adSek 
38213339867aSMatthew Ahrens 	buf = malloc(buflen);
38223339867aSMatthew Ahrens 	if (buf == NULL)
38233339867aSMatthew Ahrens 		return (ENOMEM);
382406eeb2adSek 	do {
38253339867aSMatthew Ahrens 		uint64_t bytes_read = buflen;
382606eeb2adSek 		uint64_t leftover;
382706eeb2adSek 
382806eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
382906eeb2adSek 			break;
383006eeb2adSek 
383106eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
383206eeb2adSek 		if (!bytes_read)
383306eeb2adSek 			break;
383406eeb2adSek 
383506eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
383606eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
383706eeb2adSek 			break;
383806eeb2adSek 		off -= leftover;
38393339867aSMatthew Ahrens 		if (leftover == bytes_read) {
38403339867aSMatthew Ahrens 			/*
38413339867aSMatthew Ahrens 			 * no progress made, because buffer is not big enough
38423339867aSMatthew Ahrens 			 * to hold this record; resize and retry.
38433339867aSMatthew Ahrens 			 */
38443339867aSMatthew Ahrens 			buflen *= 2;
38453339867aSMatthew Ahrens 			free(buf);
38463339867aSMatthew Ahrens 			buf = malloc(buflen);
38473339867aSMatthew Ahrens 			if (buf == NULL)
38483339867aSMatthew Ahrens 				return (ENOMEM);
38493339867aSMatthew Ahrens 		}
385006eeb2adSek 
385106eeb2adSek 		/* CONSTCOND */
385206eeb2adSek 	} while (1);
385306eeb2adSek 
38543339867aSMatthew Ahrens 	free(buf);
38553339867aSMatthew Ahrens 
385606eeb2adSek 	if (!err) {
385706eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
385806eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
385906eeb2adSek 		    records, numrecords) == 0);
386006eeb2adSek 	}
386106eeb2adSek 	for (i = 0; i < numrecords; i++)
386206eeb2adSek 		nvlist_free(records[i]);
386306eeb2adSek 	free(records);
386406eeb2adSek 
386506eeb2adSek 	return (err);
386606eeb2adSek }
386755434c77Sek 
386855434c77Sek void
386955434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
387055434c77Sek     char *pathname, size_t len)
387155434c77Sek {
387255434c77Sek 	zfs_cmd_t zc = { 0 };
387355434c77Sek 	boolean_t mounted = B_FALSE;
387455434c77Sek 	char *mntpnt = NULL;
38759adfa60dSMatthew Ahrens 	char dsname[ZFS_MAX_DATASET_NAME_LEN];
387655434c77Sek 
387755434c77Sek 	if (dsobj == 0) {
387855434c77Sek 		/* special case for the MOS */
387955434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
388055434c77Sek 		return;
388155434c77Sek 	}
388255434c77Sek 
388355434c77Sek 	/* get the dataset's name */
388455434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
388555434c77Sek 	zc.zc_obj = dsobj;
388655434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
388755434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
388855434c77Sek 		/* just write out a path of two object numbers */
388955434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
389055434c77Sek 		    dsobj, obj);
389155434c77Sek 		return;
389255434c77Sek 	}
389355434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
389455434c77Sek 
389555434c77Sek 	/* find out if the dataset is mounted */
389655434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
389755434c77Sek 
389855434c77Sek 	/* get the corrupted object's path */
389955434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
390055434c77Sek 	zc.zc_obj = obj;
390155434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
390255434c77Sek 	    &zc) == 0) {
390355434c77Sek 		if (mounted) {
390455434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
390555434c77Sek 			    zc.zc_value);
390655434c77Sek 		} else {
390755434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
390855434c77Sek 			    dsname, zc.zc_value);
390955434c77Sek 		}
391055434c77Sek 	} else {
391155434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
391255434c77Sek 	}
391355434c77Sek 	free(mntpnt);
391455434c77Sek }
3915b1b8ab34Slling 
391615e6edf1Sgw /*
391715e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
391815e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
391915e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
39207855d95bSToomas Soome  * partition. If the caller has passed a non-NULL boolean argument, then
39217855d95bSToomas Soome  * we set it to indicate if the disk does have efi system partition.
392215e6edf1Sgw  */
392315e6edf1Sgw static int
39247855d95bSToomas Soome read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
392515e6edf1Sgw {
392615e6edf1Sgw 	char *path;
392715e6edf1Sgw 	int fd;
392815e6edf1Sgw 	char diskname[MAXPATHLEN];
39297855d95bSToomas Soome 	boolean_t boot = B_FALSE;
393015e6edf1Sgw 	int err = -1;
39317855d95bSToomas Soome 	int slice;
393215e6edf1Sgw 
393315e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
393415e6edf1Sgw 		return (err);
393515e6edf1Sgw 
39366401734dSWill Andrews 	(void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
393715e6edf1Sgw 	    strrchr(path, '/'));
393815e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
393915e6edf1Sgw 		struct dk_gpt *vtoc;
394015e6edf1Sgw 
394115e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
39427855d95bSToomas Soome 			for (slice = 0; slice < vtoc->efi_nparts; slice++) {
39437855d95bSToomas Soome 				if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
39447855d95bSToomas Soome 					boot = B_TRUE;
39457855d95bSToomas Soome 				if (vtoc->efi_parts[slice].p_tag == V_USR)
39467855d95bSToomas Soome 					break;
39477855d95bSToomas Soome 			}
39487855d95bSToomas Soome 			if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
39497855d95bSToomas Soome 				*sb = vtoc->efi_parts[slice].p_start;
39507855d95bSToomas Soome 			if (system != NULL)
39517855d95bSToomas Soome 				*system = boot;
395215e6edf1Sgw 			efi_free(vtoc);
395315e6edf1Sgw 		}
395415e6edf1Sgw 		(void) close(fd);
395515e6edf1Sgw 	}
395615e6edf1Sgw 	return (err);
395715e6edf1Sgw }
395815e6edf1Sgw 
39598488aeb5Staylor /*
39608488aeb5Staylor  * determine where a partition starts on a disk in the current
39618488aeb5Staylor  * configuration
39628488aeb5Staylor  */
39638488aeb5Staylor static diskaddr_t
39648488aeb5Staylor find_start_block(nvlist_t *config)
39658488aeb5Staylor {
39668488aeb5Staylor 	nvlist_t **child;
39678488aeb5Staylor 	uint_t c, children;
39688488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
39698488aeb5Staylor 	uint64_t wholedisk;
39708488aeb5Staylor 
39718488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
39728488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
39738488aeb5Staylor 		if (nvlist_lookup_uint64(config,
39748488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
39758488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
39768488aeb5Staylor 			return (MAXOFFSET_T);
39778488aeb5Staylor 		}
39787855d95bSToomas Soome 		if (read_efi_label(config, &sb, NULL) < 0)
397915e6edf1Sgw 			sb = MAXOFFSET_T;
39808488aeb5Staylor 		return (sb);
39818488aeb5Staylor 	}
39828488aeb5Staylor 
39838488aeb5Staylor 	for (c = 0; c < children; c++) {
39848488aeb5Staylor 		sb = find_start_block(child[c]);
39858488aeb5Staylor 		if (sb != MAXOFFSET_T) {
39868488aeb5Staylor 			return (sb);
39878488aeb5Staylor 		}
39888488aeb5Staylor 	}
39898488aeb5Staylor 	return (MAXOFFSET_T);
39908488aeb5Staylor }
39918488aeb5Staylor 
39928488aeb5Staylor /*
39938488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
39948488aeb5Staylor  * stripped of any leading /dev path.
39958488aeb5Staylor  */
39968488aeb5Staylor int
39977855d95bSToomas Soome zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
39987855d95bSToomas Soome     zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
39998488aeb5Staylor {
40008488aeb5Staylor 	char path[MAXPATHLEN];
40018488aeb5Staylor 	struct dk_gpt *vtoc;
40028488aeb5Staylor 	int fd;
40038488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
40048488aeb5Staylor 	uint64_t slice_size;
40058488aeb5Staylor 	diskaddr_t start_block;
40068488aeb5Staylor 	char errbuf[1024];
40078488aeb5Staylor 
4008c6ef114fSmmusante 	/* prepare an error message just in case */
4009c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
4010c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4011c6ef114fSmmusante 
40128488aeb5Staylor 	if (zhp) {
40138488aeb5Staylor 		nvlist_t *nvroot;
40148488aeb5Staylor 
40158488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
40168488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
40178488aeb5Staylor 
40188488aeb5Staylor 		if (zhp->zpool_start_block == 0)
40198488aeb5Staylor 			start_block = find_start_block(nvroot);
40208488aeb5Staylor 		else
40218488aeb5Staylor 			start_block = zhp->zpool_start_block;
40228488aeb5Staylor 		zhp->zpool_start_block = start_block;
40238488aeb5Staylor 	} else {
40248488aeb5Staylor 		/* new pool */
40258488aeb5Staylor 		start_block = NEW_START_BLOCK;
40268488aeb5Staylor 	}
40278488aeb5Staylor 
40286401734dSWill Andrews 	(void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
40298488aeb5Staylor 	    BACKUP_SLICE);
40308488aeb5Staylor 
40318488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
40328488aeb5Staylor 		/*
40338488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
40348488aeb5Staylor 		 * is a valid device.
40358488aeb5Staylor 		 */
4036c6ef114fSmmusante 		zfs_error_aux(hdl,
4037c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
40388488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
40398488aeb5Staylor 	}
40408488aeb5Staylor 
40418488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
40428488aeb5Staylor 		/*
40438488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
40448488aeb5Staylor 		 * were unable to read the disk's capacity
40458488aeb5Staylor 		 */
40468488aeb5Staylor 		if (errno == ENOMEM)
40478488aeb5Staylor 			(void) no_memory(hdl);
40488488aeb5Staylor 
40498488aeb5Staylor 		(void) close(fd);
4050c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4051c6ef114fSmmusante 		    "unable to read disk capacity"), name);
40528488aeb5Staylor 
40538488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
40548488aeb5Staylor 	}
40558488aeb5Staylor 
40568488aeb5Staylor 	/*
40578488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
40588488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
40598488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
40608488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
40618488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
40628488aeb5Staylor 	 * can get, in the absence of V_OTHER.
40638488aeb5Staylor 	 */
40647855d95bSToomas Soome 	/* first fix the partition start block */
40657855d95bSToomas Soome 	if (start_block == MAXOFFSET_T)
40667855d95bSToomas Soome 		start_block = NEW_START_BLOCK;
40678488aeb5Staylor 
40687855d95bSToomas Soome 	/*
40697855d95bSToomas Soome 	 * EFI System partition is using slice 0.
40707855d95bSToomas Soome 	 * ZFS is on slice 1 and slice 8 is reserved.
40717855d95bSToomas Soome 	 * We assume the GPT partition table without system
40727855d95bSToomas Soome 	 * partition has zfs p_start == NEW_START_BLOCK.
40737855d95bSToomas Soome 	 * If start_block != NEW_START_BLOCK, it means we have
40747855d95bSToomas Soome 	 * system partition. Correct solution would be to query/cache vtoc
40757855d95bSToomas Soome 	 * from existing vdev member.
40767855d95bSToomas Soome 	 */
40777855d95bSToomas Soome 	if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
40787855d95bSToomas Soome 		if (boot_size % vtoc->efi_lbasize != 0) {
40797855d95bSToomas Soome 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40807855d95bSToomas Soome 			    "boot partition size must be a multiple of %d"),
40817855d95bSToomas Soome 			    vtoc->efi_lbasize);
40827855d95bSToomas Soome 			(void) close(fd);
40837855d95bSToomas Soome 			efi_free(vtoc);
40847855d95bSToomas Soome 			return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
40857855d95bSToomas Soome 		}
40867855d95bSToomas Soome 		/*
40877855d95bSToomas Soome 		 * System partition size checks.
40887855d95bSToomas Soome 		 * Note the 1MB is quite arbitrary value, since we
40897855d95bSToomas Soome 		 * are creating dedicated pool, it should be enough
40907855d95bSToomas Soome 		 * to hold fat + efi bootloader. May need to be
40917855d95bSToomas Soome 		 * adjusted if the bootloader size will grow.
40927855d95bSToomas Soome 		 */
40937855d95bSToomas Soome 		if (boot_size < 1024 * 1024) {
40947855d95bSToomas Soome 			char buf[64];
40957855d95bSToomas Soome 			zfs_nicenum(boot_size, buf, sizeof (buf));
40967855d95bSToomas Soome 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40977855d95bSToomas Soome 			    "Specified size %s for EFI System partition is too "
40987855d95bSToomas Soome 			    "small, the minimum size is 1MB."), buf);
40997855d95bSToomas Soome 			(void) close(fd);
41007855d95bSToomas Soome 			efi_free(vtoc);
41017855d95bSToomas Soome 			return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
41027855d95bSToomas Soome 		}
41037855d95bSToomas Soome 		/* 33MB is tested with mkfs -F pcfs */
41047855d95bSToomas Soome 		if (hdl->libzfs_printerr &&
41057855d95bSToomas Soome 		    ((vtoc->efi_lbasize == 512 &&
41067855d95bSToomas Soome 		    boot_size < 33 * 1024 * 1024) ||
41077855d95bSToomas Soome 		    (vtoc->efi_lbasize == 4096 &&
41087855d95bSToomas Soome 		    boot_size < 256 * 1024 * 1024)))  {
41097855d95bSToomas Soome 			char buf[64];
41107855d95bSToomas Soome 			zfs_nicenum(boot_size, buf, sizeof (buf));
41117855d95bSToomas Soome 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
41127855d95bSToomas Soome 			    "Warning: EFI System partition size %s is "
41137855d95bSToomas Soome 			    "not allowing to create FAT32 file\nsystem, which "
41147855d95bSToomas Soome 			    "may result in unbootable system.\n"), buf);
41157855d95bSToomas Soome 		}
41167855d95bSToomas Soome 		/* Adjust zfs partition start by size of system partition. */
41177855d95bSToomas Soome 		start_block += boot_size / vtoc->efi_lbasize;
41187855d95bSToomas Soome 	}
41197855d95bSToomas Soome 
41207855d95bSToomas Soome 	if (start_block == NEW_START_BLOCK) {
41217855d95bSToomas Soome 		/*
41227855d95bSToomas Soome 		 * Use default layout.
41237855d95bSToomas Soome 		 * ZFS is on slice 0 and slice 8 is reserved.
41247855d95bSToomas Soome 		 */
41257855d95bSToomas Soome 		slice_size = vtoc->efi_last_u_lba + 1;
41267855d95bSToomas Soome 		slice_size -= EFI_MIN_RESV_SIZE;
41277855d95bSToomas Soome 		slice_size -= start_block;
41287855d95bSToomas Soome 		if (slice != NULL)
41297855d95bSToomas Soome 			*slice = 0;
41307855d95bSToomas Soome 
41317855d95bSToomas Soome 		vtoc->efi_parts[0].p_start = start_block;
41327855d95bSToomas Soome 		vtoc->efi_parts[0].p_size = slice_size;
41337855d95bSToomas Soome 
41347855d95bSToomas Soome 		vtoc->efi_parts[0].p_tag = V_USR;
41357855d95bSToomas Soome 		(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
41367855d95bSToomas Soome 
41377855d95bSToomas Soome 		vtoc->efi_parts[8].p_start = slice_size + start_block;
41387855d95bSToomas Soome 		vtoc->efi_parts[8].p_size = resv;
41397855d95bSToomas Soome 		vtoc->efi_parts[8].p_tag = V_RESERVED;
41407855d95bSToomas Soome 	} else {
41417855d95bSToomas Soome 		slice_size = start_block - NEW_START_BLOCK;
41427855d95bSToomas Soome 		vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
41437855d95bSToomas Soome 		vtoc->efi_parts[0].p_size = slice_size;
41447855d95bSToomas Soome 		vtoc->efi_parts[0].p_tag = V_SYSTEM;
41457855d95bSToomas Soome 		(void) strcpy(vtoc->efi_parts[0].p_name, "loader");
41467855d95bSToomas Soome 		if (slice != NULL)
41477855d95bSToomas Soome 			*slice = 1;
41487855d95bSToomas Soome 		/* prepare slice 1 */
41497855d95bSToomas Soome 		slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
41507855d95bSToomas Soome 		slice_size -= resv;
41517855d95bSToomas Soome 		slice_size -= NEW_START_BLOCK;
41527855d95bSToomas Soome 		vtoc->efi_parts[1].p_start = start_block;
41537855d95bSToomas Soome 		vtoc->efi_parts[1].p_size = slice_size;
41547855d95bSToomas Soome 		vtoc->efi_parts[1].p_tag = V_USR;
41557855d95bSToomas Soome 		(void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
41567855d95bSToomas Soome 
41577855d95bSToomas Soome 		vtoc->efi_parts[8].p_start = slice_size + start_block;
41587855d95bSToomas Soome 		vtoc->efi_parts[8].p_size = resv;
41597855d95bSToomas Soome 		vtoc->efi_parts[8].p_tag = V_RESERVED;
41607855d95bSToomas Soome 	}
41618488aeb5Staylor 
41628488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
41638488aeb5Staylor 		/*
41648488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
41658488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
41668488aeb5Staylor 		 * ecting the user to manually label the disk and give
41678488aeb5Staylor 		 * a specific slice.
41688488aeb5Staylor 		 */
41698488aeb5Staylor 		(void) close(fd);
41708488aeb5Staylor 		efi_free(vtoc);
41718488aeb5Staylor 
41728488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4173c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
41748488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
41758488aeb5Staylor 	}
41768488aeb5Staylor 
41778488aeb5Staylor 	(void) close(fd);
41788488aeb5Staylor 	efi_free(vtoc);
41798488aeb5Staylor 	return (0);
41808488aeb5Staylor }
4181e7cbe64fSgw 
4182e7cbe64fSgw static boolean_t
4183e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4184e7cbe64fSgw {
4185e7cbe64fSgw 	char *type;
4186e7cbe64fSgw 	nvlist_t **child;
4187e7cbe64fSgw 	uint_t children, c;
4188e7cbe64fSgw 
4189e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4190810e43b2SBill Pijewski 	if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
419188ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4192e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
4193e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4194e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
4195e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4196e7cbe64fSgw 		return (B_FALSE);
4197e7cbe64fSgw 	}
4198e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4199e7cbe64fSgw 	    &child, &children) == 0) {
4200e7cbe64fSgw 		for (c = 0; c < children; c++) {
4201e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4202e7cbe64fSgw 				return (B_FALSE);
4203e7cbe64fSgw 		}
4204e7cbe64fSgw 	}
4205e7cbe64fSgw 	return (B_TRUE);
4206e7cbe64fSgw }
4207e7cbe64fSgw 
4208e7cbe64fSgw /*
4209810e43b2SBill Pijewski  * Check if this zvol is allowable for use as a dump device; zero if
4210810e43b2SBill Pijewski  * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4211810e43b2SBill Pijewski  *
4212810e43b2SBill Pijewski  * Allowable storage configurations include mirrors, all raidz variants, and
4213810e43b2SBill Pijewski  * pools with log, cache, and spare devices.  Pools which are backed by files or
4214810e43b2SBill Pijewski  * have missing/hole vdevs are not suitable.
4215e7cbe64fSgw  */
4216e7cbe64fSgw int
4217e7cbe64fSgw zvol_check_dump_config(char *arg)
4218e7cbe64fSgw {
4219e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
4220e7cbe64fSgw 	nvlist_t *config, *nvroot;
4221e7cbe64fSgw 	char *p, *volname;
4222e7cbe64fSgw 	nvlist_t **top;
4223e7cbe64fSgw 	uint_t toplevels;
4224e7cbe64fSgw 	libzfs_handle_t *hdl;
4225e7cbe64fSgw 	char errbuf[1024];
42269adfa60dSMatthew Ahrens 	char poolname[ZFS_MAX_DATASET_NAME_LEN];
4227e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4228e7cbe64fSgw 	int ret = 1;
4229e7cbe64fSgw 
4230e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4231e7cbe64fSgw 		return (-1);
4232e7cbe64fSgw 	}
4233e7cbe64fSgw 
4234e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4235e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
4236e7cbe64fSgw 
4237e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
4238e7cbe64fSgw 		return (1);
4239e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
4240e7cbe64fSgw 
4241e7cbe64fSgw 	volname = arg + pathlen;
4242e7cbe64fSgw 
4243e7cbe64fSgw 	/* check the configuration of the pool */
4244e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
4245e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4246e7cbe64fSgw 		    "malformed dataset name"));
4247e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4248e7cbe64fSgw 		return (1);
42499adfa60dSMatthew Ahrens 	} else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4250e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4251e7cbe64fSgw 		    "dataset name is too long"));
4252e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4253e7cbe64fSgw 		return (1);
4254e7cbe64fSgw 	} else {
4255e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
4256e7cbe64fSgw 		poolname[p - volname] = '\0';
4257e7cbe64fSgw 	}
4258e7cbe64fSgw 
4259e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4260e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4261e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
4262e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4263e7cbe64fSgw 		goto out;
4264e7cbe64fSgw 	}
4265e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
4266e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4267e7cbe64fSgw 	    &nvroot) != 0) {
4268e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4269e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
4270e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4271e7cbe64fSgw 		goto out;
4272e7cbe64fSgw 	}
4273e7cbe64fSgw 
4274e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4275e7cbe64fSgw 	    &top, &toplevels) == 0);
4276e7cbe64fSgw 
4277e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4278e7cbe64fSgw 		goto out;
4279e7cbe64fSgw 	}
4280e7cbe64fSgw 	ret = 0;
4281e7cbe64fSgw 
4282e7cbe64fSgw out:
4283e7cbe64fSgw 	if (zhp)
4284e7cbe64fSgw 		zpool_close(zhp);
4285e7cbe64fSgw 	libzfs_fini(hdl);
4286e7cbe64fSgw 	return (ret);
4287e7cbe64fSgw }
4288