xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_pool.c (revision 9adfa60d484ce2435f5af77cc99dcd4e692b6660)
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.
249a686fbcSPaul Dagnelie  * Copyright (c) 2011, 2015 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>
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <ctype.h>
31fa9e4066Sahrens #include <errno.h>
32fa9e4066Sahrens #include <devid.h>
33fa9e4066Sahrens #include <fcntl.h>
34fa9e4066Sahrens #include <libintl.h>
35fa9e4066Sahrens #include <stdio.h>
36fa9e4066Sahrens #include <stdlib.h>
37f3861e1aSahl #include <strings.h>
38fa9e4066Sahrens #include <unistd.h>
394445fffbSMatthew Ahrens #include <libgen.h>
408488aeb5Staylor #include <sys/efi_partition.h>
418488aeb5Staylor #include <sys/vtoc.h>
42fa9e4066Sahrens #include <sys/zfs_ioctl.h>
43573ca77eSGeorge Wilson #include <dlfcn.h>
44fa9e4066Sahrens 
45fa9e4066Sahrens #include "zfs_namecheck.h"
46b1b8ab34Slling #include "zfs_prop.h"
47fa9e4066Sahrens #include "libzfs_impl.h"
48468c413aSTim Haley #include "zfs_comutil.h"
49ad135b5dSChristopher Siden #include "zfeature_common.h"
50fa9e4066Sahrens 
5115e6edf1Sgw static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52990b4856Slling 
53573ca77eSGeorge Wilson #define	BACKUP_SLICE	"s2"
54573ca77eSGeorge Wilson 
55f9af39baSGeorge Wilson typedef struct prop_flags {
56f9af39baSGeorge Wilson 	int create:1;	/* Validate property on creation */
57f9af39baSGeorge Wilson 	int import:1;	/* Validate property on import */
58f9af39baSGeorge Wilson } prop_flags_t;
59f9af39baSGeorge Wilson 
60990b4856Slling /*
61990b4856Slling  * ====================================================================
62990b4856Slling  *   zpool property functions
63990b4856Slling  * ====================================================================
64990b4856Slling  */
65990b4856Slling 
66990b4856Slling static int
67990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
68990b4856Slling {
69990b4856Slling 	zfs_cmd_t zc = { 0 };
70990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
71990b4856Slling 
72990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
73990b4856Slling 
74990b4856Slling 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
75990b4856Slling 		return (-1);
76990b4856Slling 
77990b4856Slling 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
78990b4856Slling 		if (errno == ENOMEM) {
79990b4856Slling 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
80990b4856Slling 				zcmd_free_nvlists(&zc);
81990b4856Slling 				return (-1);
82990b4856Slling 			}
83990b4856Slling 		} else {
84990b4856Slling 			zcmd_free_nvlists(&zc);
85990b4856Slling 			return (-1);
86990b4856Slling 		}
87990b4856Slling 	}
88990b4856Slling 
89990b4856Slling 	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
90990b4856Slling 		zcmd_free_nvlists(&zc);
91990b4856Slling 		return (-1);
92990b4856Slling 	}
93990b4856Slling 
94990b4856Slling 	zcmd_free_nvlists(&zc);
95990b4856Slling 
96990b4856Slling 	return (0);
97990b4856Slling }
98990b4856Slling 
99990b4856Slling static int
100990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
101990b4856Slling {
102990b4856Slling 	nvlist_t *old_props;
103990b4856Slling 
104990b4856Slling 	old_props = zhp->zpool_props;
105990b4856Slling 
106990b4856Slling 	if (zpool_get_all_props(zhp) != 0)
107990b4856Slling 		return (-1);
108990b4856Slling 
109990b4856Slling 	nvlist_free(old_props);
110990b4856Slling 	return (0);
111990b4856Slling }
112990b4856Slling 
113990b4856Slling static char *
114990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
115990b4856Slling     zprop_source_t *src)
116990b4856Slling {
117990b4856Slling 	nvlist_t *nv, *nvl;
118990b4856Slling 	uint64_t ival;
119990b4856Slling 	char *value;
120990b4856Slling 	zprop_source_t source;
121990b4856Slling 
122990b4856Slling 	nvl = zhp->zpool_props;
123990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
124990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
125990b4856Slling 		source = ival;
126990b4856Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
127990b4856Slling 	} else {
128990b4856Slling 		source = ZPROP_SRC_DEFAULT;
129990b4856Slling 		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
130990b4856Slling 			value = "-";
131990b4856Slling 	}
132990b4856Slling 
133990b4856Slling 	if (src)
134990b4856Slling 		*src = source;
135990b4856Slling 
136990b4856Slling 	return (value);
137990b4856Slling }
138990b4856Slling 
139990b4856Slling uint64_t
140990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
141990b4856Slling {
142990b4856Slling 	nvlist_t *nv, *nvl;
143990b4856Slling 	uint64_t value;
144990b4856Slling 	zprop_source_t source;
145990b4856Slling 
146b87f3af3Sperrin 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
147b87f3af3Sperrin 		/*
148b87f3af3Sperrin 		 * zpool_get_all_props() has most likely failed because
149b87f3af3Sperrin 		 * the pool is faulted, but if all we need is the top level
150b87f3af3Sperrin 		 * vdev's guid then get it from the zhp config nvlist.
151b87f3af3Sperrin 		 */
152b87f3af3Sperrin 		if ((prop == ZPOOL_PROP_GUID) &&
153b87f3af3Sperrin 		    (nvlist_lookup_nvlist(zhp->zpool_config,
154b87f3af3Sperrin 		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
155b87f3af3Sperrin 		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
156b87f3af3Sperrin 		    == 0)) {
157b87f3af3Sperrin 			return (value);
158b87f3af3Sperrin 		}
159990b4856Slling 		return (zpool_prop_default_numeric(prop));
160b87f3af3Sperrin 	}
161990b4856Slling 
162990b4856Slling 	nvl = zhp->zpool_props;
163990b4856Slling 	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
164990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
165990b4856Slling 		source = value;
166990b4856Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
167990b4856Slling 	} else {
168990b4856Slling 		source = ZPROP_SRC_DEFAULT;
169990b4856Slling 		value = zpool_prop_default_numeric(prop);
170990b4856Slling 	}
171990b4856Slling 
172990b4856Slling 	if (src)
173990b4856Slling 		*src = source;
174990b4856Slling 
175990b4856Slling 	return (value);
176990b4856Slling }
177990b4856Slling 
178990b4856Slling /*
179990b4856Slling  * Map VDEV STATE to printed strings.
180990b4856Slling  */
1816401734dSWill Andrews const char *
182990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183990b4856Slling {
184990b4856Slling 	switch (state) {
185990b4856Slling 	case VDEV_STATE_CLOSED:
186990b4856Slling 	case VDEV_STATE_OFFLINE:
187990b4856Slling 		return (gettext("OFFLINE"));
188990b4856Slling 	case VDEV_STATE_REMOVED:
189990b4856Slling 		return (gettext("REMOVED"));
190990b4856Slling 	case VDEV_STATE_CANT_OPEN:
191b87f3af3Sperrin 		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
192990b4856Slling 			return (gettext("FAULTED"));
1931195e687SMark J Musante 		else if (aux == VDEV_AUX_SPLIT_POOL)
1941195e687SMark J Musante 			return (gettext("SPLIT"));
195990b4856Slling 		else
196990b4856Slling 			return (gettext("UNAVAIL"));
197990b4856Slling 	case VDEV_STATE_FAULTED:
198990b4856Slling 		return (gettext("FAULTED"));
199990b4856Slling 	case VDEV_STATE_DEGRADED:
200990b4856Slling 		return (gettext("DEGRADED"));
201990b4856Slling 	case VDEV_STATE_HEALTHY:
202990b4856Slling 		return (gettext("ONLINE"));
20388f61deeSIgor Kozhukhov 
20488f61deeSIgor Kozhukhov 	default:
20588f61deeSIgor Kozhukhov 		break;
206990b4856Slling 	}
207990b4856Slling 
208990b4856Slling 	return (gettext("UNKNOWN"));
209990b4856Slling }
210990b4856Slling 
2116401734dSWill Andrews /*
2126401734dSWill Andrews  * Map POOL STATE to printed strings.
2136401734dSWill Andrews  */
2146401734dSWill Andrews const char *
2156401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state)
2166401734dSWill Andrews {
2176401734dSWill Andrews 	switch (state) {
2186401734dSWill Andrews 	case POOL_STATE_ACTIVE:
2196401734dSWill Andrews 		return (gettext("ACTIVE"));
2206401734dSWill Andrews 	case POOL_STATE_EXPORTED:
2216401734dSWill Andrews 		return (gettext("EXPORTED"));
2226401734dSWill Andrews 	case POOL_STATE_DESTROYED:
2236401734dSWill Andrews 		return (gettext("DESTROYED"));
2246401734dSWill Andrews 	case POOL_STATE_SPARE:
2256401734dSWill Andrews 		return (gettext("SPARE"));
2266401734dSWill Andrews 	case POOL_STATE_L2CACHE:
2276401734dSWill Andrews 		return (gettext("L2CACHE"));
2286401734dSWill Andrews 	case POOL_STATE_UNINITIALIZED:
2296401734dSWill Andrews 		return (gettext("UNINITIALIZED"));
2306401734dSWill Andrews 	case POOL_STATE_UNAVAIL:
2316401734dSWill Andrews 		return (gettext("UNAVAIL"));
2326401734dSWill Andrews 	case POOL_STATE_POTENTIALLY_ACTIVE:
2336401734dSWill Andrews 		return (gettext("POTENTIALLY_ACTIVE"));
2346401734dSWill Andrews 	}
2356401734dSWill Andrews 
2366401734dSWill Andrews 	return (gettext("UNKNOWN"));
2376401734dSWill Andrews }
2386401734dSWill Andrews 
239990b4856Slling /*
240990b4856Slling  * Get a zpool property value for 'prop' and return the value in
241990b4856Slling  * a pre-allocated buffer.
242990b4856Slling  */
243990b4856Slling int
244990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
245c58b3526SAdam Stevko     zprop_source_t *srctype, boolean_t literal)
246990b4856Slling {
247990b4856Slling 	uint64_t intval;
248990b4856Slling 	const char *strval;
249990b4856Slling 	zprop_source_t src = ZPROP_SRC_NONE;
250990b4856Slling 	nvlist_t *nvroot;
251990b4856Slling 	vdev_stat_t *vs;
252990b4856Slling 	uint_t vsc;
253990b4856Slling 
254990b4856Slling 	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
255379c004dSEric Schrock 		switch (prop) {
256379c004dSEric Schrock 		case ZPOOL_PROP_NAME:
257990b4856Slling 			(void) strlcpy(buf, zpool_get_name(zhp), len);
258379c004dSEric Schrock 			break;
259379c004dSEric Schrock 
260379c004dSEric Schrock 		case ZPOOL_PROP_HEALTH:
261990b4856Slling 			(void) strlcpy(buf, "FAULTED", len);
262379c004dSEric Schrock 			break;
263379c004dSEric Schrock 
264379c004dSEric Schrock 		case ZPOOL_PROP_GUID:
265379c004dSEric Schrock 			intval = zpool_get_prop_int(zhp, prop, &src);
266379c004dSEric Schrock 			(void) snprintf(buf, len, "%llu", intval);
267379c004dSEric Schrock 			break;
268379c004dSEric Schrock 
269379c004dSEric Schrock 		case ZPOOL_PROP_ALTROOT:
270379c004dSEric Schrock 		case ZPOOL_PROP_CACHEFILE:
2718704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
272379c004dSEric Schrock 			if (zhp->zpool_props != NULL ||
273379c004dSEric Schrock 			    zpool_get_all_props(zhp) == 0) {
274379c004dSEric Schrock 				(void) strlcpy(buf,
275379c004dSEric Schrock 				    zpool_get_prop_string(zhp, prop, &src),
276379c004dSEric Schrock 				    len);
277c58b3526SAdam Stevko 				break;
278379c004dSEric Schrock 			}
279379c004dSEric Schrock 			/* FALLTHROUGH */
280379c004dSEric Schrock 		default:
281990b4856Slling 			(void) strlcpy(buf, "-", len);
282379c004dSEric Schrock 			break;
283379c004dSEric Schrock 		}
284379c004dSEric Schrock 
285379c004dSEric Schrock 		if (srctype != NULL)
286379c004dSEric Schrock 			*srctype = src;
287990b4856Slling 		return (0);
288990b4856Slling 	}
289990b4856Slling 
290990b4856Slling 	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
291990b4856Slling 	    prop != ZPOOL_PROP_NAME)
292990b4856Slling 		return (-1);
293990b4856Slling 
294990b4856Slling 	switch (zpool_prop_get_type(prop)) {
295990b4856Slling 	case PROP_TYPE_STRING:
296990b4856Slling 		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
297990b4856Slling 		    len);
298990b4856Slling 		break;
299990b4856Slling 
300990b4856Slling 	case PROP_TYPE_NUMBER:
301990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
302990b4856Slling 
303990b4856Slling 		switch (prop) {
304990b4856Slling 		case ZPOOL_PROP_SIZE:
305485bbbf5SGeorge Wilson 		case ZPOOL_PROP_ALLOCATED:
306485bbbf5SGeorge Wilson 		case ZPOOL_PROP_FREE:
307ad135b5dSChristopher Siden 		case ZPOOL_PROP_FREEING:
3087fd05ac4SMatthew Ahrens 		case ZPOOL_PROP_LEAKED:
309c58b3526SAdam Stevko 			if (literal) {
310c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
311c58b3526SAdam Stevko 				    (u_longlong_t)intval);
312c58b3526SAdam Stevko 			} else {
313c58b3526SAdam Stevko 				(void) zfs_nicenum(intval, buf, len);
314c58b3526SAdam Stevko 			}
315990b4856Slling 			break;
3167a09f97bSGeorge Wilson 		case ZPOOL_PROP_EXPANDSZ:
3177a09f97bSGeorge Wilson 			if (intval == 0) {
3187a09f97bSGeorge Wilson 				(void) strlcpy(buf, "-", len);
3197a09f97bSGeorge Wilson 			} else if (literal) {
3207a09f97bSGeorge Wilson 				(void) snprintf(buf, len, "%llu",
3217a09f97bSGeorge Wilson 				    (u_longlong_t)intval);
3227a09f97bSGeorge Wilson 			} else {
3237a09f97bSGeorge Wilson 				(void) zfs_nicenum(intval, buf, len);
3247a09f97bSGeorge Wilson 			}
3257a09f97bSGeorge Wilson 			break;
326990b4856Slling 		case ZPOOL_PROP_CAPACITY:
327c58b3526SAdam Stevko 			if (literal) {
328c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu",
329c58b3526SAdam Stevko 				    (u_longlong_t)intval);
330c58b3526SAdam Stevko 			} else {
331c58b3526SAdam Stevko 				(void) snprintf(buf, len, "%llu%%",
332c58b3526SAdam Stevko 				    (u_longlong_t)intval);
333c58b3526SAdam Stevko 			}
334990b4856Slling 			break;
3352e4c9986SGeorge Wilson 		case ZPOOL_PROP_FRAGMENTATION:
3362e4c9986SGeorge Wilson 			if (intval == UINT64_MAX) {
3372e4c9986SGeorge Wilson 				(void) strlcpy(buf, "-", len);
3382e4c9986SGeorge Wilson 			} else {
3392e4c9986SGeorge Wilson 				(void) snprintf(buf, len, "%llu%%",
3402e4c9986SGeorge Wilson 				    (u_longlong_t)intval);
3412e4c9986SGeorge Wilson 			}
3422e4c9986SGeorge Wilson 			break;
343b24ab676SJeff Bonwick 		case ZPOOL_PROP_DEDUPRATIO:
344b24ab676SJeff Bonwick 			(void) snprintf(buf, len, "%llu.%02llux",
345b24ab676SJeff Bonwick 			    (u_longlong_t)(intval / 100),
346b24ab676SJeff Bonwick 			    (u_longlong_t)(intval % 100));
347b24ab676SJeff Bonwick 			break;
348990b4856Slling 		case ZPOOL_PROP_HEALTH:
349990b4856Slling 			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
350990b4856Slling 			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
351990b4856Slling 			verify(nvlist_lookup_uint64_array(nvroot,
3523f9d6ad7SLin Ling 			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3533f9d6ad7SLin Ling 			    == 0);
354990b4856Slling 
355990b4856Slling 			(void) strlcpy(buf, zpool_state_to_name(intval,
356990b4856Slling 			    vs->vs_aux), len);
357990b4856Slling 			break;
358ad135b5dSChristopher Siden 		case ZPOOL_PROP_VERSION:
359ad135b5dSChristopher Siden 			if (intval >= SPA_VERSION_FEATURES) {
360ad135b5dSChristopher Siden 				(void) snprintf(buf, len, "-");
361ad135b5dSChristopher Siden 				break;
362ad135b5dSChristopher Siden 			}
363ad135b5dSChristopher Siden 			/* FALLTHROUGH */
364990b4856Slling 		default:
365990b4856Slling 			(void) snprintf(buf, len, "%llu", intval);
366990b4856Slling 		}
367990b4856Slling 		break;
368990b4856Slling 
369990b4856Slling 	case PROP_TYPE_INDEX:
370990b4856Slling 		intval = zpool_get_prop_int(zhp, prop, &src);
371990b4856Slling 		if (zpool_prop_index_to_string(prop, intval, &strval)
372990b4856Slling 		    != 0)
373990b4856Slling 			return (-1);
374990b4856Slling 		(void) strlcpy(buf, strval, len);
375990b4856Slling 		break;
376990b4856Slling 
377990b4856Slling 	default:
378990b4856Slling 		abort();
379990b4856Slling 	}
380990b4856Slling 
381990b4856Slling 	if (srctype)
382990b4856Slling 		*srctype = src;
383990b4856Slling 
384990b4856Slling 	return (0);
385990b4856Slling }
386990b4856Slling 
387990b4856Slling /*
388990b4856Slling  * Check if the bootfs name has the same pool name as it is set to.
389990b4856Slling  * Assuming bootfs is a valid dataset name.
390990b4856Slling  */
391990b4856Slling static boolean_t
392990b4856Slling bootfs_name_valid(const char *pool, char *bootfs)
393990b4856Slling {
394990b4856Slling 	int len = strlen(pool);
395990b4856Slling 
396fe3e2633SEric Taylor 	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
397990b4856Slling 		return (B_FALSE);
398990b4856Slling 
399990b4856Slling 	if (strncmp(pool, bootfs, len) == 0 &&
400990b4856Slling 	    (bootfs[len] == '/' || bootfs[len] == '\0'))
401990b4856Slling 		return (B_TRUE);
402990b4856Slling 
403990b4856Slling 	return (B_FALSE);
404990b4856Slling }
405990b4856Slling 
4064263d13fSGeorge Wilson boolean_t
4074263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
408b5b76fecSGeorge Wilson {
409*9adfa60dSMatthew Ahrens 	char bootfs[ZFS_MAX_DATASET_NAME_LEN];
410b5b76fecSGeorge Wilson 
411b5b76fecSGeorge Wilson 	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
412c58b3526SAdam Stevko 	    sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
413b5b76fecSGeorge Wilson 	    sizeof (bootfs)) != 0);
414b5b76fecSGeorge Wilson }
415b5b76fecSGeorge Wilson 
416b5b76fecSGeorge Wilson 
417990b4856Slling /*
418990b4856Slling  * Given an nvlist of zpool properties to be set, validate that they are
419990b4856Slling  * correct, and parse any numeric properties (index, boolean, etc) if they are
420990b4856Slling  * specified as strings.
421990b4856Slling  */
422990b4856Slling static nvlist_t *
4230a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
424f9af39baSGeorge Wilson     nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
425990b4856Slling {
426990b4856Slling 	nvpair_t *elem;
427990b4856Slling 	nvlist_t *retprops;
428990b4856Slling 	zpool_prop_t prop;
429990b4856Slling 	char *strval;
430990b4856Slling 	uint64_t intval;
4318704186eSDan McDonald 	char *slash, *check;
4322f8aaab3Seschrock 	struct stat64 statbuf;
43315e6edf1Sgw 	zpool_handle_t *zhp;
434990b4856Slling 
435990b4856Slling 	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
436990b4856Slling 		(void) no_memory(hdl);
437990b4856Slling 		return (NULL);
438990b4856Slling 	}
439990b4856Slling 
440990b4856Slling 	elem = NULL;
441990b4856Slling 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
442990b4856Slling 		const char *propname = nvpair_name(elem);
443990b4856Slling 
444ad135b5dSChristopher Siden 		prop = zpool_name_to_prop(propname);
445ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
446ad135b5dSChristopher Siden 			int err;
447ad135b5dSChristopher Siden 			char *fname = strchr(propname, '@') + 1;
448ad135b5dSChristopher Siden 
4492acef22dSMatthew Ahrens 			err = zfeature_lookup_name(fname, NULL);
450ad135b5dSChristopher Siden 			if (err != 0) {
451ad135b5dSChristopher Siden 				ASSERT3U(err, ==, ENOENT);
452ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
453ad135b5dSChristopher Siden 				    "invalid feature '%s'"), fname);
454ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
455ad135b5dSChristopher Siden 				goto error;
456ad135b5dSChristopher Siden 			}
457ad135b5dSChristopher Siden 
458ad135b5dSChristopher Siden 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
459ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460ad135b5dSChristopher Siden 				    "'%s' must be a string"), propname);
461ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
462ad135b5dSChristopher Siden 				goto error;
463ad135b5dSChristopher Siden 			}
464ad135b5dSChristopher Siden 
465ad135b5dSChristopher Siden 			(void) nvpair_value_string(elem, &strval);
466ad135b5dSChristopher Siden 			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
467ad135b5dSChristopher Siden 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
468ad135b5dSChristopher Siden 				    "property '%s' can only be set to "
469ad135b5dSChristopher Siden 				    "'enabled'"), propname);
470ad135b5dSChristopher Siden 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
471ad135b5dSChristopher Siden 				goto error;
472ad135b5dSChristopher Siden 			}
473ad135b5dSChristopher Siden 
474ad135b5dSChristopher Siden 			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
475ad135b5dSChristopher Siden 				(void) no_memory(hdl);
476ad135b5dSChristopher Siden 				goto error;
477ad135b5dSChristopher Siden 			}
478ad135b5dSChristopher Siden 			continue;
479ad135b5dSChristopher Siden 		}
480ad135b5dSChristopher Siden 
481990b4856Slling 		/*
482990b4856Slling 		 * Make sure this property is valid and applies to this type.
483990b4856Slling 		 */
484ad135b5dSChristopher Siden 		if (prop == ZPROP_INVAL) {
485990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
486990b4856Slling 			    "invalid property '%s'"), propname);
487990b4856Slling 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
488990b4856Slling 			goto error;
489990b4856Slling 		}
490990b4856Slling 
491990b4856Slling 		if (zpool_prop_readonly(prop)) {
492990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
493990b4856Slling 			    "is readonly"), propname);
494990b4856Slling 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
495990b4856Slling 			goto error;
496990b4856Slling 		}
497990b4856Slling 
498990b4856Slling 		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
499990b4856Slling 		    &strval, &intval, errbuf) != 0)
500990b4856Slling 			goto error;
501990b4856Slling 
502990b4856Slling 		/*
503990b4856Slling 		 * Perform additional checking for specific properties.
504990b4856Slling 		 */
505990b4856Slling 		switch (prop) {
506990b4856Slling 		case ZPOOL_PROP_VERSION:
507ad135b5dSChristopher Siden 			if (intval < version ||
508ad135b5dSChristopher Siden 			    !SPA_VERSION_IS_SUPPORTED(intval)) {
509990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
510990b4856Slling 				    "property '%s' number %d is invalid."),
511990b4856Slling 				    propname, intval);
512990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
513990b4856Slling 				goto error;
514990b4856Slling 			}
515990b4856Slling 			break;
516990b4856Slling 
517990b4856Slling 		case ZPOOL_PROP_BOOTFS:
518f9af39baSGeorge Wilson 			if (flags.create || flags.import) {
519990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520990b4856Slling 				    "property '%s' cannot be set at creation "
521990b4856Slling 				    "or import time"), propname);
522990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
523990b4856Slling 				goto error;
524990b4856Slling 			}
525990b4856Slling 
526990b4856Slling 			if (version < SPA_VERSION_BOOTFS) {
527990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
528990b4856Slling 				    "pool must be upgraded to support "
529990b4856Slling 				    "'%s' property"), propname);
530990b4856Slling 				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
531990b4856Slling 				goto error;
532990b4856Slling 			}
533990b4856Slling 
534990b4856Slling 			/*
535990b4856Slling 			 * bootfs property value has to be a dataset name and
536990b4856Slling 			 * the dataset has to be in the same pool as it sets to.
537990b4856Slling 			 */
538990b4856Slling 			if (strval[0] != '\0' && !bootfs_name_valid(poolname,
539990b4856Slling 			    strval)) {
540990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
541990b4856Slling 				    "is an invalid name"), strval);
542990b4856Slling 				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
543990b4856Slling 				goto error;
544990b4856Slling 			}
54515e6edf1Sgw 
54615e6edf1Sgw 			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
54715e6edf1Sgw 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
54815e6edf1Sgw 				    "could not open pool '%s'"), poolname);
54915e6edf1Sgw 				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
55015e6edf1Sgw 				goto error;
55115e6edf1Sgw 			}
55215e6edf1Sgw 			zpool_close(zhp);
553990b4856Slling 			break;
554990b4856Slling 
5552f8aaab3Seschrock 		case ZPOOL_PROP_ALTROOT:
556f9af39baSGeorge Wilson 			if (!flags.create && !flags.import) {
557990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
558990b4856Slling 				    "property '%s' can only be set during pool "
559990b4856Slling 				    "creation or import"), propname);
560990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
561990b4856Slling 				goto error;
562990b4856Slling 			}
563990b4856Slling 
5642f8aaab3Seschrock 			if (strval[0] != '/') {
565990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5662f8aaab3Seschrock 				    "bad alternate root '%s'"), strval);
5672f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
568990b4856Slling 				goto error;
569990b4856Slling 			}
5702f8aaab3Seschrock 			break;
5712f8aaab3Seschrock 
5722f8aaab3Seschrock 		case ZPOOL_PROP_CACHEFILE:
5732f8aaab3Seschrock 			if (strval[0] == '\0')
5742f8aaab3Seschrock 				break;
5752f8aaab3Seschrock 
5762f8aaab3Seschrock 			if (strcmp(strval, "none") == 0)
5772f8aaab3Seschrock 				break;
578990b4856Slling 
579990b4856Slling 			if (strval[0] != '/') {
580990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5812f8aaab3Seschrock 				    "property '%s' must be empty, an "
5822f8aaab3Seschrock 				    "absolute path, or 'none'"), propname);
583990b4856Slling 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
584990b4856Slling 				goto error;
585990b4856Slling 			}
586990b4856Slling 
5872f8aaab3Seschrock 			slash = strrchr(strval, '/');
588990b4856Slling 
5892f8aaab3Seschrock 			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5902f8aaab3Seschrock 			    strcmp(slash, "/..") == 0) {
5912f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5922f8aaab3Seschrock 				    "'%s' is not a valid file"), strval);
5932f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
5942f8aaab3Seschrock 				goto error;
5952f8aaab3Seschrock 			}
596990b4856Slling 
5972f8aaab3Seschrock 			*slash = '\0';
5982f8aaab3Seschrock 
5992c32020fSeschrock 			if (strval[0] != '\0' &&
6002c32020fSeschrock 			    (stat64(strval, &statbuf) != 0 ||
6012c32020fSeschrock 			    !S_ISDIR(statbuf.st_mode))) {
6022f8aaab3Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6032f8aaab3Seschrock 				    "'%s' is not a valid directory"),
6042f8aaab3Seschrock 				    strval);
6052f8aaab3Seschrock 				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
6062f8aaab3Seschrock 				goto error;
6072f8aaab3Seschrock 			}
6082f8aaab3Seschrock 
6092f8aaab3Seschrock 			*slash = '/';
6102f8aaab3Seschrock 			break;
611f9af39baSGeorge Wilson 
6128704186eSDan McDonald 		case ZPOOL_PROP_COMMENT:
6138704186eSDan McDonald 			for (check = strval; *check != '\0'; check++) {
6148704186eSDan McDonald 				if (!isprint(*check)) {
6158704186eSDan McDonald 					zfs_error_aux(hdl,
6168704186eSDan McDonald 					    dgettext(TEXT_DOMAIN,
6178704186eSDan McDonald 					    "comment may only have printable "
6188704186eSDan McDonald 					    "characters"));
6198704186eSDan McDonald 					(void) zfs_error(hdl, EZFS_BADPROP,
6208704186eSDan McDonald 					    errbuf);
6218704186eSDan McDonald 					goto error;
6228704186eSDan McDonald 				}
6238704186eSDan McDonald 			}
6248704186eSDan McDonald 			if (strlen(strval) > ZPROP_MAX_COMMENT) {
6258704186eSDan McDonald 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6268704186eSDan McDonald 				    "comment must not exceed %d characters"),
6278704186eSDan McDonald 				    ZPROP_MAX_COMMENT);
6288704186eSDan McDonald 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6298704186eSDan McDonald 				goto error;
6308704186eSDan McDonald 			}
6318704186eSDan McDonald 			break;
632f9af39baSGeorge Wilson 		case ZPOOL_PROP_READONLY:
633f9af39baSGeorge Wilson 			if (!flags.import) {
634f9af39baSGeorge Wilson 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
635f9af39baSGeorge Wilson 				    "property '%s' can only be set at "
636f9af39baSGeorge Wilson 				    "import time"), propname);
637f9af39baSGeorge Wilson 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
638f9af39baSGeorge Wilson 				goto error;
639f9af39baSGeorge Wilson 			}
640f9af39baSGeorge Wilson 			break;
64188f61deeSIgor Kozhukhov 
64288f61deeSIgor Kozhukhov 		default:
64388f61deeSIgor Kozhukhov 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
64488f61deeSIgor Kozhukhov 			    "property '%s'(%d) not defined"), propname, prop);
64588f61deeSIgor Kozhukhov 			break;
646990b4856Slling 		}
647990b4856Slling 	}
648990b4856Slling 
649990b4856Slling 	return (retprops);
650990b4856Slling error:
651990b4856Slling 	nvlist_free(retprops);
652990b4856Slling 	return (NULL);
653990b4856Slling }
654990b4856Slling 
655990b4856Slling /*
656990b4856Slling  * Set zpool property : propname=propval.
657990b4856Slling  */
658990b4856Slling int
659990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
660990b4856Slling {
661990b4856Slling 	zfs_cmd_t zc = { 0 };
662990b4856Slling 	int ret = -1;
663990b4856Slling 	char errbuf[1024];
664990b4856Slling 	nvlist_t *nvl = NULL;
665990b4856Slling 	nvlist_t *realprops;
666990b4856Slling 	uint64_t version;
667f9af39baSGeorge Wilson 	prop_flags_t flags = { 0 };
668990b4856Slling 
669990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf),
670990b4856Slling 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
671990b4856Slling 	    zhp->zpool_name);
672990b4856Slling 
673990b4856Slling 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
674990b4856Slling 		return (no_memory(zhp->zpool_hdl));
675990b4856Slling 
676990b4856Slling 	if (nvlist_add_string(nvl, propname, propval) != 0) {
677990b4856Slling 		nvlist_free(nvl);
678990b4856Slling 		return (no_memory(zhp->zpool_hdl));
679990b4856Slling 	}
680990b4856Slling 
681990b4856Slling 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
6820a48a24eStimh 	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
683f9af39baSGeorge Wilson 	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
684990b4856Slling 		nvlist_free(nvl);
685990b4856Slling 		return (-1);
686990b4856Slling 	}
687990b4856Slling 
688990b4856Slling 	nvlist_free(nvl);
689990b4856Slling 	nvl = realprops;
690990b4856Slling 
691990b4856Slling 	/*
692990b4856Slling 	 * Execute the corresponding ioctl() to set this property.
693990b4856Slling 	 */
694990b4856Slling 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
695990b4856Slling 
696990b4856Slling 	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
697990b4856Slling 		nvlist_free(nvl);
698990b4856Slling 		return (-1);
699990b4856Slling 	}
700990b4856Slling 
701990b4856Slling 	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
702990b4856Slling 
703990b4856Slling 	zcmd_free_nvlists(&zc);
704990b4856Slling 	nvlist_free(nvl);
705990b4856Slling 
706990b4856Slling 	if (ret)
707990b4856Slling 		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
708990b4856Slling 	else
709990b4856Slling 		(void) zpool_props_refresh(zhp);
710990b4856Slling 
711990b4856Slling 	return (ret);
712990b4856Slling }
713990b4856Slling 
714990b4856Slling int
715990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
716990b4856Slling {
717990b4856Slling 	libzfs_handle_t *hdl = zhp->zpool_hdl;
718990b4856Slling 	zprop_list_t *entry;
719990b4856Slling 	char buf[ZFS_MAXPROPLEN];
720ad135b5dSChristopher Siden 	nvlist_t *features = NULL;
721ad135b5dSChristopher Siden 	zprop_list_t **last;
722ad135b5dSChristopher Siden 	boolean_t firstexpand = (NULL == *plp);
723990b4856Slling 
724990b4856Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
725990b4856Slling 		return (-1);
726990b4856Slling 
727ad135b5dSChristopher Siden 	last = plp;
728ad135b5dSChristopher Siden 	while (*last != NULL)
729ad135b5dSChristopher Siden 		last = &(*last)->pl_next;
730ad135b5dSChristopher Siden 
731ad135b5dSChristopher Siden 	if ((*plp)->pl_all)
732ad135b5dSChristopher Siden 		features = zpool_get_features(zhp);
733ad135b5dSChristopher Siden 
734ad135b5dSChristopher Siden 	if ((*plp)->pl_all && firstexpand) {
735ad135b5dSChristopher Siden 		for (int i = 0; i < SPA_FEATURES; i++) {
736ad135b5dSChristopher Siden 			zprop_list_t *entry = zfs_alloc(hdl,
737ad135b5dSChristopher Siden 			    sizeof (zprop_list_t));
738ad135b5dSChristopher Siden 			entry->pl_prop = ZPROP_INVAL;
739ad135b5dSChristopher Siden 			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
740ad135b5dSChristopher Siden 			    spa_feature_table[i].fi_uname);
741ad135b5dSChristopher Siden 			entry->pl_width = strlen(entry->pl_user_prop);
742ad135b5dSChristopher Siden 			entry->pl_all = B_TRUE;
743ad135b5dSChristopher Siden 
744ad135b5dSChristopher Siden 			*last = entry;
745ad135b5dSChristopher Siden 			last = &entry->pl_next;
746ad135b5dSChristopher Siden 		}
747ad135b5dSChristopher Siden 	}
748ad135b5dSChristopher Siden 
749ad135b5dSChristopher Siden 	/* add any unsupported features */
750ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
751ad135b5dSChristopher Siden 	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
752ad135b5dSChristopher Siden 		char *propname;
753ad135b5dSChristopher Siden 		boolean_t found;
754ad135b5dSChristopher Siden 		zprop_list_t *entry;
755ad135b5dSChristopher Siden 
756ad135b5dSChristopher Siden 		if (zfeature_is_supported(nvpair_name(nvp)))
757ad135b5dSChristopher Siden 			continue;
758ad135b5dSChristopher Siden 
759ad135b5dSChristopher Siden 		propname = zfs_asprintf(hdl, "unsupported@%s",
760ad135b5dSChristopher Siden 		    nvpair_name(nvp));
761ad135b5dSChristopher Siden 
762ad135b5dSChristopher Siden 		/*
763ad135b5dSChristopher Siden 		 * Before adding the property to the list make sure that no
764ad135b5dSChristopher Siden 		 * other pool already added the same property.
765ad135b5dSChristopher Siden 		 */
766ad135b5dSChristopher Siden 		found = B_FALSE;
767ad135b5dSChristopher Siden 		entry = *plp;
768ad135b5dSChristopher Siden 		while (entry != NULL) {
769ad135b5dSChristopher Siden 			if (entry->pl_user_prop != NULL &&
770ad135b5dSChristopher Siden 			    strcmp(propname, entry->pl_user_prop) == 0) {
771ad135b5dSChristopher Siden 				found = B_TRUE;
772ad135b5dSChristopher Siden 				break;
773ad135b5dSChristopher Siden 			}
774ad135b5dSChristopher Siden 			entry = entry->pl_next;
775ad135b5dSChristopher Siden 		}
776ad135b5dSChristopher Siden 		if (found) {
777ad135b5dSChristopher Siden 			free(propname);
778ad135b5dSChristopher Siden 			continue;
779ad135b5dSChristopher Siden 		}
780ad135b5dSChristopher Siden 
781ad135b5dSChristopher Siden 		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
782ad135b5dSChristopher Siden 		entry->pl_prop = ZPROP_INVAL;
783ad135b5dSChristopher Siden 		entry->pl_user_prop = propname;
784ad135b5dSChristopher Siden 		entry->pl_width = strlen(entry->pl_user_prop);
785ad135b5dSChristopher Siden 		entry->pl_all = B_TRUE;
786ad135b5dSChristopher Siden 
787ad135b5dSChristopher Siden 		*last = entry;
788ad135b5dSChristopher Siden 		last = &entry->pl_next;
789ad135b5dSChristopher Siden 	}
790ad135b5dSChristopher Siden 
791990b4856Slling 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
792990b4856Slling 
793990b4856Slling 		if (entry->pl_fixed)
794990b4856Slling 			continue;
795990b4856Slling 
796990b4856Slling 		if (entry->pl_prop != ZPROP_INVAL &&
797990b4856Slling 		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
798c58b3526SAdam Stevko 		    NULL, B_FALSE) == 0) {
799990b4856Slling 			if (strlen(buf) > entry->pl_width)
800990b4856Slling 				entry->pl_width = strlen(buf);
801990b4856Slling 		}
802990b4856Slling 	}
803990b4856Slling 
804990b4856Slling 	return (0);
805990b4856Slling }
806990b4856Slling 
807ad135b5dSChristopher Siden /*
808ad135b5dSChristopher Siden  * Get the state for the given feature on the given ZFS pool.
809ad135b5dSChristopher Siden  */
810ad135b5dSChristopher Siden int
811ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
812ad135b5dSChristopher Siden     size_t len)
813ad135b5dSChristopher Siden {
814ad135b5dSChristopher Siden 	uint64_t refcount;
815ad135b5dSChristopher Siden 	boolean_t found = B_FALSE;
816ad135b5dSChristopher Siden 	nvlist_t *features = zpool_get_features(zhp);
817ad135b5dSChristopher Siden 	boolean_t supported;
818ad135b5dSChristopher Siden 	const char *feature = strchr(propname, '@') + 1;
819ad135b5dSChristopher Siden 
820ad135b5dSChristopher Siden 	supported = zpool_prop_feature(propname);
821ad135b5dSChristopher Siden 	ASSERT(supported || zfs_prop_unsupported(propname));
822ad135b5dSChristopher Siden 
823ad135b5dSChristopher Siden 	/*
824ad135b5dSChristopher Siden 	 * Convert from feature name to feature guid. This conversion is
825ad135b5dSChristopher Siden 	 * unecessary for unsupported@... properties because they already
826ad135b5dSChristopher Siden 	 * use guids.
827ad135b5dSChristopher Siden 	 */
828ad135b5dSChristopher Siden 	if (supported) {
829ad135b5dSChristopher Siden 		int ret;
8302acef22dSMatthew Ahrens 		spa_feature_t fid;
831ad135b5dSChristopher Siden 
8322acef22dSMatthew Ahrens 		ret = zfeature_lookup_name(feature, &fid);
833ad135b5dSChristopher Siden 		if (ret != 0) {
834ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
835ad135b5dSChristopher Siden 			return (ENOTSUP);
836ad135b5dSChristopher Siden 		}
8372acef22dSMatthew Ahrens 		feature = spa_feature_table[fid].fi_guid;
838ad135b5dSChristopher Siden 	}
839ad135b5dSChristopher Siden 
840ad135b5dSChristopher Siden 	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
841ad135b5dSChristopher Siden 		found = B_TRUE;
842ad135b5dSChristopher Siden 
843ad135b5dSChristopher Siden 	if (supported) {
844ad135b5dSChristopher Siden 		if (!found) {
845ad135b5dSChristopher Siden 			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
846ad135b5dSChristopher Siden 		} else  {
847ad135b5dSChristopher Siden 			if (refcount == 0)
848ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
849ad135b5dSChristopher Siden 			else
850ad135b5dSChristopher Siden 				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
851ad135b5dSChristopher Siden 		}
852ad135b5dSChristopher Siden 	} else {
853ad135b5dSChristopher Siden 		if (found) {
854ad135b5dSChristopher Siden 			if (refcount == 0) {
855ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
856ad135b5dSChristopher Siden 			} else {
857ad135b5dSChristopher Siden 				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
858ad135b5dSChristopher Siden 			}
859ad135b5dSChristopher Siden 		} else {
860ad135b5dSChristopher Siden 			(void) strlcpy(buf, "-", len);
861ad135b5dSChristopher Siden 			return (ENOTSUP);
862ad135b5dSChristopher Siden 		}
863ad135b5dSChristopher Siden 	}
864ad135b5dSChristopher Siden 
865ad135b5dSChristopher Siden 	return (0);
866ad135b5dSChristopher Siden }
867990b4856Slling 
868573ca77eSGeorge Wilson /*
869573ca77eSGeorge Wilson  * Don't start the slice at the default block of 34; many storage
870573ca77eSGeorge Wilson  * devices will use a stripe width of 128k, so start there instead.
871573ca77eSGeorge Wilson  */
872573ca77eSGeorge Wilson #define	NEW_START_BLOCK	256
873573ca77eSGeorge Wilson 
874fa9e4066Sahrens /*
875fa9e4066Sahrens  * Validate the given pool name, optionally putting an extended error message in
876fa9e4066Sahrens  * 'buf'.
877fa9e4066Sahrens  */
878e7cbe64fSgw boolean_t
87999653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
880fa9e4066Sahrens {
881fa9e4066Sahrens 	namecheck_err_t why;
882fa9e4066Sahrens 	char what;
883b468a217Seschrock 	int ret;
884b468a217Seschrock 
885b468a217Seschrock 	ret = pool_namecheck(pool, &why, &what);
886b468a217Seschrock 
887b468a217Seschrock 	/*
888b468a217Seschrock 	 * The rules for reserved pool names were extended at a later point.
889b468a217Seschrock 	 * But we need to support users with existing pools that may now be
890b468a217Seschrock 	 * invalid.  So we only check for this expanded set of names during a
891b468a217Seschrock 	 * create (or import), and only in userland.
892b468a217Seschrock 	 */
893b468a217Seschrock 	if (ret == 0 && !isopen &&
894b468a217Seschrock 	    (strncmp(pool, "mirror", 6) == 0 ||
895b468a217Seschrock 	    strncmp(pool, "raidz", 5) == 0 ||
8968654d025Sperrin 	    strncmp(pool, "spare", 5) == 0 ||
8978654d025Sperrin 	    strcmp(pool, "log") == 0)) {
898e7cbe64fSgw 		if (hdl != NULL)
899e7cbe64fSgw 			zfs_error_aux(hdl,
900e7cbe64fSgw 			    dgettext(TEXT_DOMAIN, "name is reserved"));
90199653d4eSeschrock 		return (B_FALSE);
902b468a217Seschrock 	}
903b468a217Seschrock 
904fa9e4066Sahrens 
905b468a217Seschrock 	if (ret != 0) {
90699653d4eSeschrock 		if (hdl != NULL) {
907fa9e4066Sahrens 			switch (why) {
908b81d61a6Slling 			case NAME_ERR_TOOLONG:
90999653d4eSeschrock 				zfs_error_aux(hdl,
910b81d61a6Slling 				    dgettext(TEXT_DOMAIN, "name is too long"));
911b81d61a6Slling 				break;
912b81d61a6Slling 
913fa9e4066Sahrens 			case NAME_ERR_INVALCHAR:
91499653d4eSeschrock 				zfs_error_aux(hdl,
915fa9e4066Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
916fa9e4066Sahrens 				    "'%c' in pool name"), what);
917fa9e4066Sahrens 				break;
918fa9e4066Sahrens 
919fa9e4066Sahrens 			case NAME_ERR_NOLETTER:
92099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
92199653d4eSeschrock 				    "name must begin with a letter"));
922fa9e4066Sahrens 				break;
923fa9e4066Sahrens 
924fa9e4066Sahrens 			case NAME_ERR_RESERVED:
92599653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
92699653d4eSeschrock 				    "name is reserved"));
927fa9e4066Sahrens 				break;
928fa9e4066Sahrens 
929fa9e4066Sahrens 			case NAME_ERR_DISKLIKE:
93099653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
93199653d4eSeschrock 				    "pool name is reserved"));
932fa9e4066Sahrens 				break;
9335ad82045Snd 
9345ad82045Snd 			case NAME_ERR_LEADING_SLASH:
9355ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9365ad82045Snd 				    "leading slash in name"));
9375ad82045Snd 				break;
9385ad82045Snd 
9395ad82045Snd 			case NAME_ERR_EMPTY_COMPONENT:
9405ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9415ad82045Snd 				    "empty component in name"));
9425ad82045Snd 				break;
9435ad82045Snd 
9445ad82045Snd 			case NAME_ERR_TRAILING_SLASH:
9455ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9465ad82045Snd 				    "trailing slash in name"));
9475ad82045Snd 				break;
9485ad82045Snd 
9495ad82045Snd 			case NAME_ERR_MULTIPLE_AT:
9505ad82045Snd 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9515ad82045Snd 				    "multiple '@' delimiters in name"));
9525ad82045Snd 				break;
9535ad82045Snd 
95488f61deeSIgor Kozhukhov 			default:
95588f61deeSIgor Kozhukhov 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
95688f61deeSIgor Kozhukhov 				    "(%d) not defined"), why);
95788f61deeSIgor Kozhukhov 				break;
958fa9e4066Sahrens 			}
959fa9e4066Sahrens 		}
96099653d4eSeschrock 		return (B_FALSE);
961fa9e4066Sahrens 	}
962fa9e4066Sahrens 
96399653d4eSeschrock 	return (B_TRUE);
964fa9e4066Sahrens }
965fa9e4066Sahrens 
966fa9e4066Sahrens /*
967fa9e4066Sahrens  * Open a handle to the given pool, even if the pool is currently in the FAULTED
968fa9e4066Sahrens  * state.
969fa9e4066Sahrens  */
970fa9e4066Sahrens zpool_handle_t *
97199653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
972fa9e4066Sahrens {
973fa9e4066Sahrens 	zpool_handle_t *zhp;
97494de1d4cSeschrock 	boolean_t missing;
975fa9e4066Sahrens 
976fa9e4066Sahrens 	/*
977fa9e4066Sahrens 	 * Make sure the pool name is valid.
978fa9e4066Sahrens 	 */
97999653d4eSeschrock 	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
980ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
98199653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
98299653d4eSeschrock 		    pool);
983fa9e4066Sahrens 		return (NULL);
984fa9e4066Sahrens 	}
985fa9e4066Sahrens 
98699653d4eSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
98799653d4eSeschrock 		return (NULL);
988fa9e4066Sahrens 
98999653d4eSeschrock 	zhp->zpool_hdl = hdl;
990fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
991fa9e4066Sahrens 
99294de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
99394de1d4cSeschrock 		zpool_close(zhp);
99494de1d4cSeschrock 		return (NULL);
99594de1d4cSeschrock 	}
99694de1d4cSeschrock 
99794de1d4cSeschrock 	if (missing) {
998990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
999ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_NOENT,
1000990b4856Slling 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
100194de1d4cSeschrock 		zpool_close(zhp);
100294de1d4cSeschrock 		return (NULL);
1003fa9e4066Sahrens 	}
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	return (zhp);
1006fa9e4066Sahrens }
1007fa9e4066Sahrens 
1008fa9e4066Sahrens /*
1009fa9e4066Sahrens  * Like the above, but silent on error.  Used when iterating over pools (because
1010fa9e4066Sahrens  * the configuration cache may be out of date).
1011fa9e4066Sahrens  */
101294de1d4cSeschrock int
101394de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1014fa9e4066Sahrens {
1015fa9e4066Sahrens 	zpool_handle_t *zhp;
101694de1d4cSeschrock 	boolean_t missing;
1017fa9e4066Sahrens 
101894de1d4cSeschrock 	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
101994de1d4cSeschrock 		return (-1);
1020fa9e4066Sahrens 
102199653d4eSeschrock 	zhp->zpool_hdl = hdl;
1022fa9e4066Sahrens 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1023fa9e4066Sahrens 
102494de1d4cSeschrock 	if (zpool_refresh_stats(zhp, &missing) != 0) {
102594de1d4cSeschrock 		zpool_close(zhp);
102694de1d4cSeschrock 		return (-1);
1027fa9e4066Sahrens 	}
1028fa9e4066Sahrens 
102994de1d4cSeschrock 	if (missing) {
103094de1d4cSeschrock 		zpool_close(zhp);
103194de1d4cSeschrock 		*ret = NULL;
103294de1d4cSeschrock 		return (0);
103394de1d4cSeschrock 	}
103494de1d4cSeschrock 
103594de1d4cSeschrock 	*ret = zhp;
103694de1d4cSeschrock 	return (0);
1037fa9e4066Sahrens }
1038fa9e4066Sahrens 
1039fa9e4066Sahrens /*
1040fa9e4066Sahrens  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1041fa9e4066Sahrens  * state.
1042fa9e4066Sahrens  */
1043fa9e4066Sahrens zpool_handle_t *
104499653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1045fa9e4066Sahrens {
1046fa9e4066Sahrens 	zpool_handle_t *zhp;
1047fa9e4066Sahrens 
104899653d4eSeschrock 	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1049fa9e4066Sahrens 		return (NULL);
1050fa9e4066Sahrens 
1051fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1052ece3d9b3Slling 		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
105399653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1054fa9e4066Sahrens 		zpool_close(zhp);
1055fa9e4066Sahrens 		return (NULL);
1056fa9e4066Sahrens 	}
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	return (zhp);
1059fa9e4066Sahrens }
1060fa9e4066Sahrens 
1061fa9e4066Sahrens /*
1062fa9e4066Sahrens  * Close the handle.  Simply frees the memory associated with the handle.
1063fa9e4066Sahrens  */
1064fa9e4066Sahrens void
1065fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1066fa9e4066Sahrens {
1067aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zhp->zpool_config);
1068aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zhp->zpool_old_config);
1069aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zhp->zpool_props);
1070fa9e4066Sahrens 	free(zhp);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens 
1073fa9e4066Sahrens /*
1074fa9e4066Sahrens  * Return the name of the pool.
1075fa9e4066Sahrens  */
1076fa9e4066Sahrens const char *
1077fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1078fa9e4066Sahrens {
1079fa9e4066Sahrens 	return (zhp->zpool_name);
1080fa9e4066Sahrens }
1081fa9e4066Sahrens 
1082fa9e4066Sahrens 
1083fa9e4066Sahrens /*
1084fa9e4066Sahrens  * Return the state of the pool (ACTIVE or UNAVAILABLE)
1085fa9e4066Sahrens  */
1086fa9e4066Sahrens int
1087fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1088fa9e4066Sahrens {
1089fa9e4066Sahrens 	return (zhp->zpool_state);
1090fa9e4066Sahrens }
1091fa9e4066Sahrens 
1092fa9e4066Sahrens /*
1093fa9e4066Sahrens  * Create the named pool, using the provided vdev list.  It is assumed
1094fa9e4066Sahrens  * that the consumer has already validated the contents of the nvlist, so we
1095fa9e4066Sahrens  * don't have to worry about error semantics.
1096fa9e4066Sahrens  */
1097fa9e4066Sahrens int
109899653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
10990a48a24eStimh     nvlist_t *props, nvlist_t *fsprops)
1100fa9e4066Sahrens {
1101fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
11020a48a24eStimh 	nvlist_t *zc_fsprops = NULL;
11030a48a24eStimh 	nvlist_t *zc_props = NULL;
110499653d4eSeschrock 	char msg[1024];
11050a48a24eStimh 	int ret = -1;
1106fa9e4066Sahrens 
110799653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
110899653d4eSeschrock 	    "cannot create '%s'"), pool);
1109fa9e4066Sahrens 
111099653d4eSeschrock 	if (!zpool_name_valid(hdl, B_FALSE, pool))
111199653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1112fa9e4066Sahrens 
1113351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1114990b4856Slling 		return (-1);
1115fa9e4066Sahrens 
11160a48a24eStimh 	if (props) {
1117f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1118f9af39baSGeorge Wilson 
11190a48a24eStimh 		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1120f9af39baSGeorge Wilson 		    SPA_VERSION_1, flags, msg)) == NULL) {
11210a48a24eStimh 			goto create_failed;
11220a48a24eStimh 		}
11230a48a24eStimh 	}
112499653d4eSeschrock 
11250a48a24eStimh 	if (fsprops) {
11260a48a24eStimh 		uint64_t zoned;
11270a48a24eStimh 		char *zonestr;
11280a48a24eStimh 
11290a48a24eStimh 		zoned = ((nvlist_lookup_string(fsprops,
11300a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
11310a48a24eStimh 		    strcmp(zonestr, "on") == 0);
11320a48a24eStimh 
1133e9316f76SJoe Stein 		if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1134e9316f76SJoe Stein 		    fsprops, zoned, NULL, NULL, msg)) == NULL) {
11350a48a24eStimh 			goto create_failed;
11360a48a24eStimh 		}
11370a48a24eStimh 		if (!zc_props &&
11380a48a24eStimh 		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
11390a48a24eStimh 			goto create_failed;
11400a48a24eStimh 		}
11410a48a24eStimh 		if (nvlist_add_nvlist(zc_props,
11420a48a24eStimh 		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
11430a48a24eStimh 			goto create_failed;
11440a48a24eStimh 		}
1145351420b3Slling 	}
1146fa9e4066Sahrens 
11470a48a24eStimh 	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
11480a48a24eStimh 		goto create_failed;
11490a48a24eStimh 
1150990b4856Slling 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1151fa9e4066Sahrens 
11520a48a24eStimh 	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1153351420b3Slling 
1154e9dbad6fSeschrock 		zcmd_free_nvlists(&zc);
11550a48a24eStimh 		nvlist_free(zc_props);
11560a48a24eStimh 		nvlist_free(zc_fsprops);
1157fa9e4066Sahrens 
115899653d4eSeschrock 		switch (errno) {
1159fa9e4066Sahrens 		case EBUSY:
1160fa9e4066Sahrens 			/*
1161fa9e4066Sahrens 			 * This can happen if the user has specified the same
1162fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1163fa9e4066Sahrens 			 * until we try to add it and see we already have a
1164fa9e4066Sahrens 			 * label.
1165fa9e4066Sahrens 			 */
116699653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116799653d4eSeschrock 			    "one or more vdevs refer to the same device"));
116899653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1169fa9e4066Sahrens 
1170e9316f76SJoe Stein 		case ERANGE:
1171e9316f76SJoe Stein 			/*
1172e9316f76SJoe Stein 			 * This happens if the record size is smaller or larger
1173e9316f76SJoe Stein 			 * than the allowed size range, or not a power of 2.
1174e9316f76SJoe Stein 			 *
1175e9316f76SJoe Stein 			 * NOTE: although zfs_valid_proplist is called earlier,
1176e9316f76SJoe Stein 			 * this case may have slipped through since the
1177e9316f76SJoe Stein 			 * pool does not exist yet and it is therefore
1178e9316f76SJoe Stein 			 * impossible to read properties e.g. max blocksize
1179e9316f76SJoe Stein 			 * from the pool.
1180e9316f76SJoe Stein 			 */
1181e9316f76SJoe Stein 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1182e9316f76SJoe Stein 			    "record size invalid"));
1183e9316f76SJoe Stein 			return (zfs_error(hdl, EZFS_BADPROP, msg));
1184e9316f76SJoe Stein 
1185fa9e4066Sahrens 		case EOVERFLOW:
1186fa9e4066Sahrens 			/*
118799653d4eSeschrock 			 * This occurs when one of the devices is below
1188fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1189fa9e4066Sahrens 			 * device was the problem device since there's no
1190fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1191fa9e4066Sahrens 			 */
1192fa9e4066Sahrens 			{
1193fa9e4066Sahrens 				char buf[64];
1194fa9e4066Sahrens 
1195fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1196fa9e4066Sahrens 
119799653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
119899653d4eSeschrock 				    "one or more devices is less than the "
119999653d4eSeschrock 				    "minimum size (%s)"), buf);
1200fa9e4066Sahrens 			}
120199653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1202fa9e4066Sahrens 
1203fa9e4066Sahrens 		case ENOSPC:
120499653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
120599653d4eSeschrock 			    "one or more devices is out of space"));
120699653d4eSeschrock 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1207fa9e4066Sahrens 
1208fa94a07fSbrendan 		case ENOTBLK:
1209fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1210fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1211fa94a07fSbrendan 			return (zfs_error(hdl, EZFS_BADDEV, msg));
1212fa94a07fSbrendan 
1213fa9e4066Sahrens 		default:
121499653d4eSeschrock 			return (zpool_standard_error(hdl, errno, msg));
1215fa9e4066Sahrens 		}
1216fa9e4066Sahrens 	}
1217fa9e4066Sahrens 
12180a48a24eStimh create_failed:
1219351420b3Slling 	zcmd_free_nvlists(&zc);
12200a48a24eStimh 	nvlist_free(zc_props);
12210a48a24eStimh 	nvlist_free(zc_fsprops);
12220a48a24eStimh 	return (ret);
1223fa9e4066Sahrens }
1224fa9e4066Sahrens 
1225fa9e4066Sahrens /*
1226fa9e4066Sahrens  * Destroy the given pool.  It is up to the caller to ensure that there are no
1227fa9e4066Sahrens  * datasets left in the pool.
1228fa9e4066Sahrens  */
1229fa9e4066Sahrens int
12304445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1231fa9e4066Sahrens {
1232fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1233fa9e4066Sahrens 	zfs_handle_t *zfp = NULL;
123499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
123599653d4eSeschrock 	char msg[1024];
1236fa9e4066Sahrens 
1237fa9e4066Sahrens 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1238cb04b873SMark J Musante 	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1239fa9e4066Sahrens 		return (-1);
1240fa9e4066Sahrens 
1241fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
12424445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1243fa9e4066Sahrens 
1244cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
124599653d4eSeschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
124699653d4eSeschrock 		    "cannot destroy '%s'"), zhp->zpool_name);
1247fa9e4066Sahrens 
124899653d4eSeschrock 		if (errno == EROFS) {
124999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
125099653d4eSeschrock 			    "one or more devices is read only"));
125199653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
125299653d4eSeschrock 		} else {
125399653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1254fa9e4066Sahrens 		}
1255fa9e4066Sahrens 
1256fa9e4066Sahrens 		if (zfp)
1257fa9e4066Sahrens 			zfs_close(zfp);
1258fa9e4066Sahrens 		return (-1);
1259fa9e4066Sahrens 	}
1260fa9e4066Sahrens 
1261fa9e4066Sahrens 	if (zfp) {
1262fa9e4066Sahrens 		remove_mountpoint(zfp);
1263fa9e4066Sahrens 		zfs_close(zfp);
1264fa9e4066Sahrens 	}
1265fa9e4066Sahrens 
1266fa9e4066Sahrens 	return (0);
1267fa9e4066Sahrens }
1268fa9e4066Sahrens 
1269fa9e4066Sahrens /*
1270fa9e4066Sahrens  * Add the given vdevs to the pool.  The caller must have already performed the
1271fa9e4066Sahrens  * necessary verification to ensure that the vdev specification is well-formed.
1272fa9e4066Sahrens  */
1273fa9e4066Sahrens int
1274fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1275fa9e4066Sahrens {
1276e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
127799653d4eSeschrock 	int ret;
127899653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
127999653d4eSeschrock 	char msg[1024];
1280fa94a07fSbrendan 	nvlist_t **spares, **l2cache;
1281fa94a07fSbrendan 	uint_t nspares, nl2cache;
128299653d4eSeschrock 
128399653d4eSeschrock 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
128499653d4eSeschrock 	    "cannot add to '%s'"), zhp->zpool_name);
128599653d4eSeschrock 
1286fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1287fa94a07fSbrendan 	    SPA_VERSION_SPARES &&
128899653d4eSeschrock 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
128999653d4eSeschrock 	    &spares, &nspares) == 0) {
129099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
129199653d4eSeschrock 		    "upgraded to add hot spares"));
129299653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
129399653d4eSeschrock 	}
1294fa9e4066Sahrens 
1295fa94a07fSbrendan 	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1296fa94a07fSbrendan 	    SPA_VERSION_L2CACHE &&
1297fa94a07fSbrendan 	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1298fa94a07fSbrendan 	    &l2cache, &nl2cache) == 0) {
1299fa94a07fSbrendan 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1300fa94a07fSbrendan 		    "upgraded to add cache devices"));
1301fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1302fa94a07fSbrendan 	}
1303fa94a07fSbrendan 
1304990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
130599653d4eSeschrock 		return (-1);
1306fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1307fa9e4066Sahrens 
1308cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1309fa9e4066Sahrens 		switch (errno) {
1310fa9e4066Sahrens 		case EBUSY:
1311fa9e4066Sahrens 			/*
1312fa9e4066Sahrens 			 * This can happen if the user has specified the same
1313fa9e4066Sahrens 			 * device multiple times.  We can't reliably detect this
1314fa9e4066Sahrens 			 * until we try to add it and see we already have a
1315fa9e4066Sahrens 			 * label.
1316fa9e4066Sahrens 			 */
131799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
131899653d4eSeschrock 			    "one or more vdevs refer to the same device"));
131999653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1320fa9e4066Sahrens 			break;
1321fa9e4066Sahrens 
1322fa9e4066Sahrens 		case EOVERFLOW:
1323fa9e4066Sahrens 			/*
1324fa9e4066Sahrens 			 * This occurrs when one of the devices is below
1325fa9e4066Sahrens 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1326fa9e4066Sahrens 			 * device was the problem device since there's no
1327fa9e4066Sahrens 			 * reliable way to determine device size from userland.
1328fa9e4066Sahrens 			 */
1329fa9e4066Sahrens 			{
1330fa9e4066Sahrens 				char buf[64];
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1333fa9e4066Sahrens 
133499653d4eSeschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133599653d4eSeschrock 				    "device is less than the minimum "
133699653d4eSeschrock 				    "size (%s)"), buf);
1337fa9e4066Sahrens 			}
133899653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
133999653d4eSeschrock 			break;
134099653d4eSeschrock 
134199653d4eSeschrock 		case ENOTSUP:
134299653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13438654d025Sperrin 			    "pool must be upgraded to add these vdevs"));
134499653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1345fa9e4066Sahrens 			break;
1346fa9e4066Sahrens 
1347b1b8ab34Slling 		case EDOM:
1348b1b8ab34Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13498654d025Sperrin 			    "root pool can not have multiple vdevs"
13508654d025Sperrin 			    " or separate logs"));
1351b1b8ab34Slling 			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1352b1b8ab34Slling 			break;
1353b1b8ab34Slling 
1354fa94a07fSbrendan 		case ENOTBLK:
1355fa94a07fSbrendan 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1356fa94a07fSbrendan 			    "cache device must be a disk or disk slice"));
1357fa94a07fSbrendan 			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1358fa94a07fSbrendan 			break;
1359fa94a07fSbrendan 
1360fa9e4066Sahrens 		default:
136199653d4eSeschrock 			(void) zpool_standard_error(hdl, errno, msg);
1362fa9e4066Sahrens 		}
1363fa9e4066Sahrens 
136499653d4eSeschrock 		ret = -1;
136599653d4eSeschrock 	} else {
136699653d4eSeschrock 		ret = 0;
1367fa9e4066Sahrens 	}
1368fa9e4066Sahrens 
1369e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
1370fa9e4066Sahrens 
137199653d4eSeschrock 	return (ret);
1372fa9e4066Sahrens }
1373fa9e4066Sahrens 
1374fa9e4066Sahrens /*
1375fa9e4066Sahrens  * Exports the pool from the system.  The caller must ensure that there are no
1376fa9e4066Sahrens  * mounted datasets in the pool.
1377fa9e4066Sahrens  */
13784445fffbSMatthew Ahrens static int
13794445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
13804445fffbSMatthew Ahrens     const char *log_str)
1381fa9e4066Sahrens {
1382fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
138389a89ebfSlling 	char msg[1024];
1384fa9e4066Sahrens 
138589a89ebfSlling 	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
138689a89ebfSlling 	    "cannot export '%s'"), zhp->zpool_name);
138789a89ebfSlling 
1388fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
138989a89ebfSlling 	zc.zc_cookie = force;
1390394ab0cbSGeorge Wilson 	zc.zc_guid = hardforce;
13914445fffbSMatthew Ahrens 	zc.zc_history = (uint64_t)(uintptr_t)log_str;
139289a89ebfSlling 
139389a89ebfSlling 	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
139489a89ebfSlling 		switch (errno) {
139589a89ebfSlling 		case EXDEV:
139689a89ebfSlling 			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
139789a89ebfSlling 			    "use '-f' to override the following errors:\n"
139889a89ebfSlling 			    "'%s' has an active shared spare which could be"
139989a89ebfSlling 			    " used by other pools once '%s' is exported."),
140089a89ebfSlling 			    zhp->zpool_name, zhp->zpool_name);
140189a89ebfSlling 			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
140289a89ebfSlling 			    msg));
140389a89ebfSlling 		default:
140489a89ebfSlling 			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
140589a89ebfSlling 			    msg));
140689a89ebfSlling 		}
140789a89ebfSlling 	}
1408fa9e4066Sahrens 
1409fa9e4066Sahrens 	return (0);
1410fa9e4066Sahrens }
1411fa9e4066Sahrens 
1412394ab0cbSGeorge Wilson int
14134445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1414394ab0cbSGeorge Wilson {
14154445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1416394ab0cbSGeorge Wilson }
1417394ab0cbSGeorge Wilson 
1418394ab0cbSGeorge Wilson int
14194445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1420394ab0cbSGeorge Wilson {
14214445fffbSMatthew Ahrens 	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1422394ab0cbSGeorge Wilson }
1423394ab0cbSGeorge Wilson 
1424468c413aSTim Haley static void
1425468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
14264b964adaSGeorge Wilson     nvlist_t *config)
1427468c413aSTim Haley {
14284b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1429468c413aSTim Haley 	uint64_t rewindto;
1430468c413aSTim Haley 	int64_t loss = -1;
1431468c413aSTim Haley 	struct tm t;
1432468c413aSTim Haley 	char timestr[128];
1433468c413aSTim Haley 
14344b964adaSGeorge Wilson 	if (!hdl->libzfs_printerr || config == NULL)
14354b964adaSGeorge Wilson 		return;
14364b964adaSGeorge Wilson 
1437ad135b5dSChristopher Siden 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1438ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1439468c413aSTim Haley 		return;
1440ad135b5dSChristopher Siden 	}
1441468c413aSTim Haley 
14424b964adaSGeorge Wilson 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1443468c413aSTim Haley 		return;
14444b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1445468c413aSTim Haley 
1446468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1447468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1448468c413aSTim Haley 		if (dryrun) {
1449468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1450468c413aSTim Haley 			    "Would be able to return %s "
1451468c413aSTim Haley 			    "to its state as of %s.\n"),
1452468c413aSTim Haley 			    name, timestr);
1453468c413aSTim Haley 		} else {
1454468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1455468c413aSTim Haley 			    "Pool %s returned to its state as of %s.\n"),
1456468c413aSTim Haley 			    name, timestr);
1457468c413aSTim Haley 		}
1458468c413aSTim Haley 		if (loss > 120) {
1459468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1460468c413aSTim Haley 			    "%s approximately %lld "),
1461468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded",
1462468c413aSTim Haley 			    (loss + 30) / 60);
1463468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1464468c413aSTim Haley 			    "minutes of transactions.\n"));
1465468c413aSTim Haley 		} else if (loss > 0) {
1466468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1467468c413aSTim Haley 			    "%s approximately %lld "),
1468468c413aSTim Haley 			    dryrun ? "Would discard" : "Discarded", loss);
1469468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1470468c413aSTim Haley 			    "seconds of transactions.\n"));
1471468c413aSTim Haley 		}
1472468c413aSTim Haley 	}
1473468c413aSTim Haley }
1474468c413aSTim Haley 
1475468c413aSTim Haley void
1476468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1477468c413aSTim Haley     nvlist_t *config)
1478468c413aSTim Haley {
14794b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
1480468c413aSTim Haley 	int64_t loss = -1;
1481468c413aSTim Haley 	uint64_t edata = UINT64_MAX;
1482468c413aSTim Haley 	uint64_t rewindto;
1483468c413aSTim Haley 	struct tm t;
1484468c413aSTim Haley 	char timestr[128];
1485468c413aSTim Haley 
1486468c413aSTim Haley 	if (!hdl->libzfs_printerr)
1487468c413aSTim Haley 		return;
1488468c413aSTim Haley 
1489468c413aSTim Haley 	if (reason >= 0)
1490468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1491468c413aSTim Haley 	else
1492468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1493468c413aSTim Haley 
1494468c413aSTim Haley 	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
14954b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1496ad135b5dSChristopher Siden 	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
14974b964adaSGeorge Wilson 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1498468c413aSTim Haley 		goto no_info;
1499468c413aSTim Haley 
15004b964adaSGeorge Wilson 	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
15014b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1502468c413aSTim Haley 	    &edata);
1503468c413aSTim Haley 
1504468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1505468c413aSTim Haley 	    "Recovery is possible, but will result in some data loss.\n"));
1506468c413aSTim Haley 
1507468c413aSTim Haley 	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1508468c413aSTim Haley 	    strftime(timestr, 128, 0, &t) != 0) {
1509468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1510468c413aSTim Haley 		    "\tReturning the pool to its state as of %s\n"
1511468c413aSTim Haley 		    "\tshould correct the problem.  "),
1512468c413aSTim Haley 		    timestr);
1513468c413aSTim Haley 	} else {
1514468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1515468c413aSTim Haley 		    "\tReverting the pool to an earlier state "
1516468c413aSTim Haley 		    "should correct the problem.\n\t"));
1517468c413aSTim Haley 	}
1518468c413aSTim Haley 
1519468c413aSTim Haley 	if (loss > 120) {
1520468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1521468c413aSTim Haley 		    "Approximately %lld minutes of data\n"
1522468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1523468c413aSTim Haley 	} else if (loss > 0) {
1524468c413aSTim Haley 		(void) printf(dgettext(TEXT_DOMAIN,
1525468c413aSTim Haley 		    "Approximately %lld seconds of data\n"
1526468c413aSTim Haley 		    "\tmust be discarded, irreversibly.  "), loss);
1527468c413aSTim Haley 	}
1528468c413aSTim Haley 	if (edata != 0 && edata != UINT64_MAX) {
1529468c413aSTim Haley 		if (edata == 1) {
1530468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1531468c413aSTim Haley 			    "After rewind, at least\n"
1532468c413aSTim Haley 			    "\tone persistent user-data error will remain.  "));
1533468c413aSTim Haley 		} else {
1534468c413aSTim Haley 			(void) printf(dgettext(TEXT_DOMAIN,
1535468c413aSTim Haley 			    "After rewind, several\n"
1536468c413aSTim Haley 			    "\tpersistent user-data errors will remain.  "));
1537468c413aSTim Haley 		}
1538468c413aSTim Haley 	}
1539468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1540a33cae98STim Haley 	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1541a33cae98STim Haley 	    reason >= 0 ? "clear" : "import", name);
1542468c413aSTim Haley 
1543468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1544468c413aSTim Haley 	    "A scrub of the pool\n"
1545468c413aSTim Haley 	    "\tis strongly recommended after recovery.\n"));
1546468c413aSTim Haley 	return;
1547468c413aSTim Haley 
1548468c413aSTim Haley no_info:
1549468c413aSTim Haley 	(void) printf(dgettext(TEXT_DOMAIN,
1550468c413aSTim Haley 	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1551468c413aSTim Haley }
1552468c413aSTim Haley 
1553fa9e4066Sahrens /*
1554990b4856Slling  * zpool_import() is a contracted interface. Should be kept the same
1555990b4856Slling  * if possible.
1556990b4856Slling  *
1557990b4856Slling  * Applications should use zpool_import_props() to import a pool with
1558990b4856Slling  * new properties value to be set.
1559fa9e4066Sahrens  */
1560fa9e4066Sahrens int
156199653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1562990b4856Slling     char *altroot)
1563990b4856Slling {
1564990b4856Slling 	nvlist_t *props = NULL;
1565990b4856Slling 	int ret;
1566990b4856Slling 
1567990b4856Slling 	if (altroot != NULL) {
1568990b4856Slling 		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1569990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1570990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1571990b4856Slling 			    newname));
1572990b4856Slling 		}
1573990b4856Slling 
1574990b4856Slling 		if (nvlist_add_string(props,
1575352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1576352d8027SGeorge Wilson 		    nvlist_add_string(props,
1577352d8027SGeorge Wilson 		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1578990b4856Slling 			nvlist_free(props);
1579990b4856Slling 			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1580990b4856Slling 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1581990b4856Slling 			    newname));
1582990b4856Slling 		}
1583990b4856Slling 	}
1584990b4856Slling 
15854b964adaSGeorge Wilson 	ret = zpool_import_props(hdl, config, newname, props,
15864b964adaSGeorge Wilson 	    ZFS_IMPORT_NORMAL);
1587aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(props);
1588990b4856Slling 	return (ret);
1589990b4856Slling }
1590990b4856Slling 
15914b964adaSGeorge Wilson static void
15924b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
15934b964adaSGeorge Wilson     int indent)
15944b964adaSGeorge Wilson {
15954b964adaSGeorge Wilson 	nvlist_t **child;
15964b964adaSGeorge Wilson 	uint_t c, children;
15974b964adaSGeorge Wilson 	char *vname;
15984b964adaSGeorge Wilson 	uint64_t is_log = 0;
15994b964adaSGeorge Wilson 
16004b964adaSGeorge Wilson 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
16014b964adaSGeorge Wilson 	    &is_log);
16024b964adaSGeorge Wilson 
16034b964adaSGeorge Wilson 	if (name != NULL)
16044b964adaSGeorge Wilson 		(void) printf("\t%*s%s%s\n", indent, "", name,
16054b964adaSGeorge Wilson 		    is_log ? " [log]" : "");
16064b964adaSGeorge Wilson 
16074b964adaSGeorge Wilson 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
16084b964adaSGeorge Wilson 	    &child, &children) != 0)
16094b964adaSGeorge Wilson 		return;
16104b964adaSGeorge Wilson 
16114b964adaSGeorge Wilson 	for (c = 0; c < children; c++) {
16124b964adaSGeorge Wilson 		vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
16134b964adaSGeorge Wilson 		print_vdev_tree(hdl, vname, child[c], indent + 2);
16144b964adaSGeorge Wilson 		free(vname);
16154b964adaSGeorge Wilson 	}
16164b964adaSGeorge Wilson }
16174b964adaSGeorge Wilson 
1618ad135b5dSChristopher Siden void
1619ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1620ad135b5dSChristopher Siden {
1621ad135b5dSChristopher Siden 	nvlist_t *nvinfo, *unsup_feat;
1622ad135b5dSChristopher Siden 
1623ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1624ad135b5dSChristopher Siden 	    0);
1625ad135b5dSChristopher Siden 	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1626ad135b5dSChristopher Siden 	    &unsup_feat) == 0);
1627ad135b5dSChristopher Siden 
1628ad135b5dSChristopher Siden 	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1629ad135b5dSChristopher Siden 	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1630ad135b5dSChristopher Siden 		char *desc;
1631ad135b5dSChristopher Siden 
1632ad135b5dSChristopher Siden 		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1633ad135b5dSChristopher Siden 		verify(nvpair_value_string(nvp, &desc) == 0);
1634ad135b5dSChristopher Siden 
1635ad135b5dSChristopher Siden 		if (strlen(desc) > 0)
1636ad135b5dSChristopher Siden 			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1637ad135b5dSChristopher Siden 		else
1638ad135b5dSChristopher Siden 			(void) printf("\t%s\n", nvpair_name(nvp));
1639ad135b5dSChristopher Siden 	}
1640ad135b5dSChristopher Siden }
1641ad135b5dSChristopher Siden 
1642990b4856Slling /*
1643990b4856Slling  * Import the given pool using the known configuration and a list of
1644990b4856Slling  * properties to be set. The configuration should have come from
1645990b4856Slling  * zpool_find_import(). The 'newname' parameters control whether the pool
1646990b4856Slling  * is imported with a different name.
1647990b4856Slling  */
1648990b4856Slling int
1649990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
16504b964adaSGeorge Wilson     nvlist_t *props, int flags)
1651fa9e4066Sahrens {
1652e9dbad6fSeschrock 	zfs_cmd_t zc = { 0 };
1653468c413aSTim Haley 	zpool_rewind_policy_t policy;
16544b964adaSGeorge Wilson 	nvlist_t *nv = NULL;
16554b964adaSGeorge Wilson 	nvlist_t *nvinfo = NULL;
16564b964adaSGeorge Wilson 	nvlist_t *missing = NULL;
1657fa9e4066Sahrens 	char *thename;
1658fa9e4066Sahrens 	char *origname;
1659fa9e4066Sahrens 	int ret;
16604b964adaSGeorge Wilson 	int error = 0;
1661990b4856Slling 	char errbuf[1024];
1662fa9e4066Sahrens 
1663fa9e4066Sahrens 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1664fa9e4066Sahrens 	    &origname) == 0);
1665fa9e4066Sahrens 
1666990b4856Slling 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1667990b4856Slling 	    "cannot import pool '%s'"), origname);
1668990b4856Slling 
1669fa9e4066Sahrens 	if (newname != NULL) {
167099653d4eSeschrock 		if (!zpool_name_valid(hdl, B_FALSE, newname))
1671ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
167299653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
167399653d4eSeschrock 			    newname));
1674fa9e4066Sahrens 		thename = (char *)newname;
1675fa9e4066Sahrens 	} else {
1676fa9e4066Sahrens 		thename = origname;
1677fa9e4066Sahrens 	}
1678fa9e4066Sahrens 
1679078266a5SMarcel Telka 	if (props != NULL) {
1680990b4856Slling 		uint64_t version;
1681f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1682fa9e4066Sahrens 
1683990b4856Slling 		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1684990b4856Slling 		    &version) == 0);
1685fa9e4066Sahrens 
16860a48a24eStimh 		if ((props = zpool_valid_proplist(hdl, origname,
1687078266a5SMarcel Telka 		    props, version, flags, errbuf)) == NULL)
1688990b4856Slling 			return (-1);
1689078266a5SMarcel Telka 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1690351420b3Slling 			nvlist_free(props);
1691990b4856Slling 			return (-1);
1692351420b3Slling 		}
1693078266a5SMarcel Telka 		nvlist_free(props);
1694990b4856Slling 	}
1695990b4856Slling 
1696990b4856Slling 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1697fa9e4066Sahrens 
1698fa9e4066Sahrens 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1699ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
1700fa9e4066Sahrens 
1701351420b3Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1702078266a5SMarcel Telka 		zcmd_free_nvlists(&zc);
170399653d4eSeschrock 		return (-1);
1704351420b3Slling 	}
170557f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1706078266a5SMarcel Telka 		zcmd_free_nvlists(&zc);
1707468c413aSTim Haley 		return (-1);
1708468c413aSTim Haley 	}
1709fa9e4066Sahrens 
17104b964adaSGeorge Wilson 	zc.zc_cookie = flags;
17114b964adaSGeorge Wilson 	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
17124b964adaSGeorge Wilson 	    errno == ENOMEM) {
17134b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
17144b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
17154b964adaSGeorge Wilson 			return (-1);
17164b964adaSGeorge Wilson 		}
17174b964adaSGeorge Wilson 	}
17184b964adaSGeorge Wilson 	if (ret != 0)
17194b964adaSGeorge Wilson 		error = errno;
17204b964adaSGeorge Wilson 
17214b964adaSGeorge Wilson 	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1722078266a5SMarcel Telka 
1723078266a5SMarcel Telka 	zcmd_free_nvlists(&zc);
1724078266a5SMarcel Telka 
17254b964adaSGeorge Wilson 	zpool_get_rewind_policy(config, &policy);
17264b964adaSGeorge Wilson 
17274b964adaSGeorge Wilson 	if (error) {
1728fa9e4066Sahrens 		char desc[1024];
1729468c413aSTim Haley 
1730468c413aSTim Haley 		/*
1731468c413aSTim Haley 		 * Dry-run failed, but we print out what success
1732468c413aSTim Haley 		 * looks like if we found a best txg
1733468c413aSTim Haley 		 */
17344b964adaSGeorge Wilson 		if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1735468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
17364b964adaSGeorge Wilson 			    B_TRUE, nv);
17374b964adaSGeorge Wilson 			nvlist_free(nv);
1738468c413aSTim Haley 			return (-1);
1739468c413aSTim Haley 		}
1740468c413aSTim Haley 
1741fa9e4066Sahrens 		if (newname == NULL)
1742fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1743fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1744fa9e4066Sahrens 			    thename);
1745fa9e4066Sahrens 		else
1746fa9e4066Sahrens 			(void) snprintf(desc, sizeof (desc),
1747fa9e4066Sahrens 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1748fa9e4066Sahrens 			    origname, thename);
1749fa9e4066Sahrens 
17504b964adaSGeorge Wilson 		switch (error) {
1751ea8dc4b6Seschrock 		case ENOTSUP:
1752ad135b5dSChristopher Siden 			if (nv != NULL && nvlist_lookup_nvlist(nv,
1753ad135b5dSChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1754ad135b5dSChristopher Siden 			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1755ad135b5dSChristopher Siden 				(void) printf(dgettext(TEXT_DOMAIN, "This "
1756ad135b5dSChristopher Siden 				    "pool uses the following feature(s) not "
1757ad135b5dSChristopher Siden 				    "supported by this system:\n"));
1758ad135b5dSChristopher Siden 				zpool_print_unsup_feat(nv);
1759ad135b5dSChristopher Siden 				if (nvlist_exists(nvinfo,
1760ad135b5dSChristopher Siden 				    ZPOOL_CONFIG_CAN_RDONLY)) {
1761ad135b5dSChristopher Siden 					(void) printf(dgettext(TEXT_DOMAIN,
1762ad135b5dSChristopher Siden 					    "All unsupported features are only "
1763ad135b5dSChristopher Siden 					    "required for writing to the pool."
1764ad135b5dSChristopher Siden 					    "\nThe pool can be imported using "
1765ad135b5dSChristopher Siden 					    "'-o readonly=on'.\n"));
1766ad135b5dSChristopher Siden 				}
1767ad135b5dSChristopher Siden 			}
1768ea8dc4b6Seschrock 			/*
1769ea8dc4b6Seschrock 			 * Unsupported version.
1770ea8dc4b6Seschrock 			 */
177199653d4eSeschrock 			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1772ea8dc4b6Seschrock 			break;
1773ea8dc4b6Seschrock 
1774b5989ec7Seschrock 		case EINVAL:
1775b5989ec7Seschrock 			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1776b5989ec7Seschrock 			break;
1777b5989ec7Seschrock 
177854a91118SChris Kirby 		case EROFS:
177954a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178054a91118SChris Kirby 			    "one or more devices is read only"));
178154a91118SChris Kirby 			(void) zfs_error(hdl, EZFS_BADDEV, desc);
178254a91118SChris Kirby 			break;
178354a91118SChris Kirby 
17844b964adaSGeorge Wilson 		case ENXIO:
17854b964adaSGeorge Wilson 			if (nv && nvlist_lookup_nvlist(nv,
17864b964adaSGeorge Wilson 			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
17874b964adaSGeorge Wilson 			    nvlist_lookup_nvlist(nvinfo,
17884b964adaSGeorge Wilson 			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
17894b964adaSGeorge Wilson 				(void) printf(dgettext(TEXT_DOMAIN,
17904b964adaSGeorge Wilson 				    "The devices below are missing, use "
17914b964adaSGeorge Wilson 				    "'-m' to import the pool anyway:\n"));
17924b964adaSGeorge Wilson 				print_vdev_tree(hdl, NULL, missing, 2);
17934b964adaSGeorge Wilson 				(void) printf("\n");
17944b964adaSGeorge Wilson 			}
17954b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
17964b964adaSGeorge Wilson 			break;
17974b964adaSGeorge Wilson 
17984b964adaSGeorge Wilson 		case EEXIST:
17994b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
18004b964adaSGeorge Wilson 			break;
1801c971037bSPaul Dagnelie 		case ENAMETOOLONG:
1802c971037bSPaul Dagnelie 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1803c971037bSPaul Dagnelie 			    "new name of at least one dataset is longer than "
1804c971037bSPaul Dagnelie 			    "the maximum allowable length"));
1805c971037bSPaul Dagnelie 			(void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1806c971037bSPaul Dagnelie 			break;
1807fa9e4066Sahrens 		default:
18084b964adaSGeorge Wilson 			(void) zpool_standard_error(hdl, error, desc);
1809468c413aSTim Haley 			zpool_explain_recover(hdl,
18104b964adaSGeorge Wilson 			    newname ? origname : thename, -error, nv);
1811468c413aSTim Haley 			break;
1812fa9e4066Sahrens 		}
1813fa9e4066Sahrens 
18144b964adaSGeorge Wilson 		nvlist_free(nv);
1815fa9e4066Sahrens 		ret = -1;
1816fa9e4066Sahrens 	} else {
1817fa9e4066Sahrens 		zpool_handle_t *zhp;
1818ecd6cf80Smarks 
1819fa9e4066Sahrens 		/*
1820fa9e4066Sahrens 		 * This should never fail, but play it safe anyway.
1821fa9e4066Sahrens 		 */
1822681d9761SEric Taylor 		if (zpool_open_silent(hdl, thename, &zhp) != 0)
182394de1d4cSeschrock 			ret = -1;
1824681d9761SEric Taylor 		else if (zhp != NULL)
1825fa9e4066Sahrens 			zpool_close(zhp);
1826468c413aSTim Haley 		if (policy.zrp_request &
1827468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1828468c413aSTim Haley 			zpool_rewind_exclaim(hdl, newname ? origname : thename,
18294b964adaSGeorge Wilson 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1830468c413aSTim Haley 		}
18314b964adaSGeorge Wilson 		nvlist_free(nv);
1832468c413aSTim Haley 		return (0);
1833fa9e4066Sahrens 	}
1834fa9e4066Sahrens 
1835fa9e4066Sahrens 	return (ret);
1836fa9e4066Sahrens }
1837fa9e4066Sahrens 
1838fa9e4066Sahrens /*
18393f9d6ad7SLin Ling  * Scan the pool.
1840fa9e4066Sahrens  */
1841fa9e4066Sahrens int
18423f9d6ad7SLin Ling zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1843fa9e4066Sahrens {
1844fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
1845fa9e4066Sahrens 	char msg[1024];
184699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1847fa9e4066Sahrens 
1848fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
18493f9d6ad7SLin Ling 	zc.zc_cookie = func;
1850fa9e4066Sahrens 
1851cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
18523f9d6ad7SLin Ling 	    (errno == ENOENT && func != POOL_SCAN_NONE))
1853fa9e4066Sahrens 		return (0);
1854fa9e4066Sahrens 
18553f9d6ad7SLin Ling 	if (func == POOL_SCAN_SCRUB) {
18563f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18573f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
18583f9d6ad7SLin Ling 	} else if (func == POOL_SCAN_NONE) {
18593f9d6ad7SLin Ling 		(void) snprintf(msg, sizeof (msg),
18603f9d6ad7SLin Ling 		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
18613f9d6ad7SLin Ling 		    zc.zc_name);
18623f9d6ad7SLin Ling 	} else {
18633f9d6ad7SLin Ling 		assert(!"unexpected result");
18643f9d6ad7SLin Ling 	}
1865fa9e4066Sahrens 
18663f9d6ad7SLin Ling 	if (errno == EBUSY) {
18673f9d6ad7SLin Ling 		nvlist_t *nvroot;
18683f9d6ad7SLin Ling 		pool_scan_stat_t *ps = NULL;
18693f9d6ad7SLin Ling 		uint_t psc;
18703f9d6ad7SLin Ling 
18713f9d6ad7SLin Ling 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
18723f9d6ad7SLin Ling 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
18733f9d6ad7SLin Ling 		(void) nvlist_lookup_uint64_array(nvroot,
18743f9d6ad7SLin Ling 		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
18753f9d6ad7SLin Ling 		if (ps && ps->pss_func == POOL_SCAN_SCRUB)
18763f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_SCRUBBING, msg));
18773f9d6ad7SLin Ling 		else
18783f9d6ad7SLin Ling 			return (zfs_error(hdl, EZFS_RESILVERING, msg));
18793f9d6ad7SLin Ling 	} else if (errno == ENOENT) {
18803f9d6ad7SLin Ling 		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
18813f9d6ad7SLin Ling 	} else {
188299653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
18833f9d6ad7SLin Ling 	}
1884fa9e4066Sahrens }
1885fa9e4066Sahrens 
18863fdda499SJohn Harres /*
18873fdda499SJohn Harres  * This provides a very minimal check whether a given string is likely a
18883fdda499SJohn Harres  * c#t#d# style string.  Users of this are expected to do their own
18893fdda499SJohn Harres  * verification of the s# part.
18903fdda499SJohn Harres  */
18913fdda499SJohn Harres #define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
18923fdda499SJohn Harres 
18933fdda499SJohn Harres /*
18943fdda499SJohn Harres  * More elaborate version for ones which may start with "/dev/dsk/"
18953fdda499SJohn Harres  * and the like.
18963fdda499SJohn Harres  */
18973fdda499SJohn Harres static int
18989a686fbcSPaul Dagnelie ctd_check_path(char *str)
18999a686fbcSPaul Dagnelie {
19003fdda499SJohn Harres 	/*
19013fdda499SJohn Harres 	 * If it starts with a slash, check the last component.
19023fdda499SJohn Harres 	 */
19033fdda499SJohn Harres 	if (str && str[0] == '/') {
19043fdda499SJohn Harres 		char *tmp = strrchr(str, '/');
19053fdda499SJohn Harres 
19063fdda499SJohn Harres 		/*
19073fdda499SJohn Harres 		 * If it ends in "/old", check the second-to-last
19083fdda499SJohn Harres 		 * component of the string instead.
19093fdda499SJohn Harres 		 */
19103fdda499SJohn Harres 		if (tmp != str && strcmp(tmp, "/old") == 0) {
19113fdda499SJohn Harres 			for (tmp--; *tmp != '/'; tmp--)
19123fdda499SJohn Harres 				;
19133fdda499SJohn Harres 		}
19143fdda499SJohn Harres 		str = tmp + 1;
19153fdda499SJohn Harres 	}
19163fdda499SJohn Harres 	return (CTD_CHECK(str));
19173fdda499SJohn Harres }
19183fdda499SJohn Harres 
1919a43d325bSek /*
1920573ca77eSGeorge Wilson  * Find a vdev that matches the search criteria specified. We use the
1921573ca77eSGeorge Wilson  * the nvpair name to determine how we should look for the device.
1922a43d325bSek  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1923a43d325bSek  * spare; but FALSE if its an INUSE spare.
1924a43d325bSek  */
192599653d4eSeschrock static nvlist_t *
1926573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1927573ca77eSGeorge Wilson     boolean_t *l2cache, boolean_t *log)
1928ea8dc4b6Seschrock {
1929ea8dc4b6Seschrock 	uint_t c, children;
1930ea8dc4b6Seschrock 	nvlist_t **child;
193199653d4eSeschrock 	nvlist_t *ret;
1932ee0eb9f2SEric Schrock 	uint64_t is_log;
1933573ca77eSGeorge Wilson 	char *srchkey;
1934573ca77eSGeorge Wilson 	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1935573ca77eSGeorge Wilson 
1936573ca77eSGeorge Wilson 	/* Nothing to look for */
1937573ca77eSGeorge Wilson 	if (search == NULL || pair == NULL)
1938573ca77eSGeorge Wilson 		return (NULL);
1939ea8dc4b6Seschrock 
1940573ca77eSGeorge Wilson 	/* Obtain the key we will use to search */
1941573ca77eSGeorge Wilson 	srchkey = nvpair_name(pair);
1942573ca77eSGeorge Wilson 
1943573ca77eSGeorge Wilson 	switch (nvpair_type(pair)) {
1944cb04b873SMark J Musante 	case DATA_TYPE_UINT64:
1945573ca77eSGeorge Wilson 		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1946cb04b873SMark J Musante 			uint64_t srchval, theguid;
1947cb04b873SMark J Musante 
1948cb04b873SMark J Musante 			verify(nvpair_value_uint64(pair, &srchval) == 0);
1949cb04b873SMark J Musante 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1950cb04b873SMark J Musante 			    &theguid) == 0);
1951cb04b873SMark J Musante 			if (theguid == srchval)
1952cb04b873SMark J Musante 				return (nv);
1953573ca77eSGeorge Wilson 		}
1954573ca77eSGeorge Wilson 		break;
1955573ca77eSGeorge Wilson 
1956573ca77eSGeorge Wilson 	case DATA_TYPE_STRING: {
1957573ca77eSGeorge Wilson 		char *srchval, *val;
1958573ca77eSGeorge Wilson 
1959573ca77eSGeorge Wilson 		verify(nvpair_value_string(pair, &srchval) == 0);
1960573ca77eSGeorge Wilson 		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1961573ca77eSGeorge Wilson 			break;
1962ea8dc4b6Seschrock 
1963ea8dc4b6Seschrock 		/*
19643fdda499SJohn Harres 		 * Search for the requested value. Special cases:
19653fdda499SJohn Harres 		 *
19663fdda499SJohn Harres 		 * - ZPOOL_CONFIG_PATH for whole disk entries.  These end in
19673fdda499SJohn Harres 		 *   "s0" or "s0/old".  The "s0" part is hidden from the user,
19683fdda499SJohn Harres 		 *   but included in the string, so this matches around it.
19693fdda499SJohn Harres 		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
19703fdda499SJohn Harres 		 *
197188ecc943SGeorge Wilson 		 * Otherwise, all other searches are simple string compares.
1972ea8dc4b6Seschrock 		 */
19733fdda499SJohn Harres 		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
19743fdda499SJohn Harres 		    ctd_check_path(val)) {
1975573ca77eSGeorge Wilson 			uint64_t wholedisk = 0;
1976573ca77eSGeorge Wilson 
1977573ca77eSGeorge Wilson 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1978573ca77eSGeorge Wilson 			    &wholedisk);
1979573ca77eSGeorge Wilson 			if (wholedisk) {
19803fdda499SJohn Harres 				int slen = strlen(srchval);
19813fdda499SJohn Harres 				int vlen = strlen(val);
19823fdda499SJohn Harres 
19833fdda499SJohn Harres 				if (slen != vlen - 2)
19843fdda499SJohn Harres 					break;
19853fdda499SJohn Harres 
19863fdda499SJohn Harres 				/*
19873fdda499SJohn Harres 				 * make_leaf_vdev() should only set
19883fdda499SJohn Harres 				 * wholedisk for ZPOOL_CONFIG_PATHs which
19893fdda499SJohn Harres 				 * will include "/dev/dsk/", giving plenty of
19903fdda499SJohn Harres 				 * room for the indices used next.
19913fdda499SJohn Harres 				 */
19923fdda499SJohn Harres 				ASSERT(vlen >= 6);
19933fdda499SJohn Harres 
19943fdda499SJohn Harres 				/*
19953fdda499SJohn Harres 				 * strings identical except trailing "s0"
19963fdda499SJohn Harres 				 */
19973fdda499SJohn Harres 				if (strcmp(&val[vlen - 2], "s0") == 0 &&
19983fdda499SJohn Harres 				    strncmp(srchval, val, slen) == 0)
19993fdda499SJohn Harres 					return (nv);
20003fdda499SJohn Harres 
2001573ca77eSGeorge Wilson 				/*
20023fdda499SJohn Harres 				 * strings identical except trailing "s0/old"
2003573ca77eSGeorge Wilson 				 */
20043fdda499SJohn Harres 				if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
20053fdda499SJohn Harres 				    strcmp(&srchval[slen - 4], "/old") == 0 &&
20063fdda499SJohn Harres 				    strncmp(srchval, val, slen - 4) == 0)
2007573ca77eSGeorge Wilson 					return (nv);
20083fdda499SJohn Harres 
2009573ca77eSGeorge Wilson 				break;
2010573ca77eSGeorge Wilson 			}
201188ecc943SGeorge Wilson 		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
201288ecc943SGeorge Wilson 			char *type, *idx, *end, *p;
201388ecc943SGeorge Wilson 			uint64_t id, vdev_id;
201488ecc943SGeorge Wilson 
201588ecc943SGeorge Wilson 			/*
201688ecc943SGeorge Wilson 			 * Determine our vdev type, keeping in mind
201788ecc943SGeorge Wilson 			 * that the srchval is composed of a type and
201888ecc943SGeorge Wilson 			 * vdev id pair (i.e. mirror-4).
201988ecc943SGeorge Wilson 			 */
202088ecc943SGeorge Wilson 			if ((type = strdup(srchval)) == NULL)
202188ecc943SGeorge Wilson 				return (NULL);
202288ecc943SGeorge Wilson 
202388ecc943SGeorge Wilson 			if ((p = strrchr(type, '-')) == NULL) {
202488ecc943SGeorge Wilson 				free(type);
202588ecc943SGeorge Wilson 				break;
202688ecc943SGeorge Wilson 			}
202788ecc943SGeorge Wilson 			idx = p + 1;
202888ecc943SGeorge Wilson 			*p = '\0';
202988ecc943SGeorge Wilson 
203088ecc943SGeorge Wilson 			/*
203188ecc943SGeorge Wilson 			 * If the types don't match then keep looking.
203288ecc943SGeorge Wilson 			 */
203388ecc943SGeorge Wilson 			if (strncmp(val, type, strlen(val)) != 0) {
203488ecc943SGeorge Wilson 				free(type);
203588ecc943SGeorge Wilson 				break;
203688ecc943SGeorge Wilson 			}
203788ecc943SGeorge Wilson 
203888ecc943SGeorge Wilson 			verify(strncmp(type, VDEV_TYPE_RAIDZ,
203988ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_RAIDZ)) == 0 ||
204088ecc943SGeorge Wilson 			    strncmp(type, VDEV_TYPE_MIRROR,
204188ecc943SGeorge Wilson 			    strlen(VDEV_TYPE_MIRROR)) == 0);
204288ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
204388ecc943SGeorge Wilson 			    &id) == 0);
204488ecc943SGeorge Wilson 
204588ecc943SGeorge Wilson 			errno = 0;
204688ecc943SGeorge Wilson 			vdev_id = strtoull(idx, &end, 10);
204788ecc943SGeorge Wilson 
204888ecc943SGeorge Wilson 			free(type);
204988ecc943SGeorge Wilson 			if (errno != 0)
205088ecc943SGeorge Wilson 				return (NULL);
205188ecc943SGeorge Wilson 
205288ecc943SGeorge Wilson 			/*
205388ecc943SGeorge Wilson 			 * Now verify that we have the correct vdev id.
205488ecc943SGeorge Wilson 			 */
205588ecc943SGeorge Wilson 			if (vdev_id == id)
205688ecc943SGeorge Wilson 				return (nv);
2057ea8dc4b6Seschrock 		}
2058573ca77eSGeorge Wilson 
2059573ca77eSGeorge Wilson 		/*
2060573ca77eSGeorge Wilson 		 * Common case
2061573ca77eSGeorge Wilson 		 */
2062573ca77eSGeorge Wilson 		if (strcmp(srchval, val) == 0)
2063573ca77eSGeorge Wilson 			return (nv);
2064573ca77eSGeorge Wilson 		break;
2065573ca77eSGeorge Wilson 	}
2066573ca77eSGeorge Wilson 
2067573ca77eSGeorge Wilson 	default:
2068573ca77eSGeorge Wilson 		break;
2069ea8dc4b6Seschrock 	}
2070ea8dc4b6Seschrock 
2071ea8dc4b6Seschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2072ea8dc4b6Seschrock 	    &child, &children) != 0)
207399653d4eSeschrock 		return (NULL);
2074ea8dc4b6Seschrock 
2075ee0eb9f2SEric Schrock 	for (c = 0; c < children; c++) {
2076573ca77eSGeorge Wilson 		if ((ret = vdev_to_nvlist_iter(child[c], search,
2077ee0eb9f2SEric Schrock 		    avail_spare, l2cache, NULL)) != NULL) {
2078ee0eb9f2SEric Schrock 			/*
2079ee0eb9f2SEric Schrock 			 * The 'is_log' value is only set for the toplevel
2080ee0eb9f2SEric Schrock 			 * vdev, not the leaf vdevs.  So we always lookup the
2081ee0eb9f2SEric Schrock 			 * log device from the root of the vdev tree (where
2082ee0eb9f2SEric Schrock 			 * 'log' is non-NULL).
2083ee0eb9f2SEric Schrock 			 */
2084ee0eb9f2SEric Schrock 			if (log != NULL &&
2085ee0eb9f2SEric Schrock 			    nvlist_lookup_uint64(child[c],
2086ee0eb9f2SEric Schrock 			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2087ee0eb9f2SEric Schrock 			    is_log) {
2088ee0eb9f2SEric Schrock 				*log = B_TRUE;
2089ee0eb9f2SEric Schrock 			}
2090ea8dc4b6Seschrock 			return (ret);
2091ee0eb9f2SEric Schrock 		}
2092ee0eb9f2SEric Schrock 	}
2093ea8dc4b6Seschrock 
209499653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
209599653d4eSeschrock 	    &child, &children) == 0) {
209699653d4eSeschrock 		for (c = 0; c < children; c++) {
2097573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2098ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2099a43d325bSek 				*avail_spare = B_TRUE;
210099653d4eSeschrock 				return (ret);
210199653d4eSeschrock 			}
210299653d4eSeschrock 		}
210399653d4eSeschrock 	}
210499653d4eSeschrock 
2105fa94a07fSbrendan 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2106fa94a07fSbrendan 	    &child, &children) == 0) {
2107fa94a07fSbrendan 		for (c = 0; c < children; c++) {
2108573ca77eSGeorge Wilson 			if ((ret = vdev_to_nvlist_iter(child[c], search,
2109ee0eb9f2SEric Schrock 			    avail_spare, l2cache, NULL)) != NULL) {
2110fa94a07fSbrendan 				*l2cache = B_TRUE;
2111fa94a07fSbrendan 				return (ret);
2112fa94a07fSbrendan 			}
2113fa94a07fSbrendan 		}
2114fa94a07fSbrendan 	}
2115fa94a07fSbrendan 
211699653d4eSeschrock 	return (NULL);
2117ea8dc4b6Seschrock }
2118ea8dc4b6Seschrock 
2119573ca77eSGeorge Wilson /*
2120573ca77eSGeorge Wilson  * Given a physical path (minus the "/devices" prefix), find the
2121573ca77eSGeorge Wilson  * associated vdev.
2122573ca77eSGeorge Wilson  */
2123573ca77eSGeorge Wilson nvlist_t *
2124573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2125573ca77eSGeorge Wilson     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2126573ca77eSGeorge Wilson {
2127573ca77eSGeorge Wilson 	nvlist_t *search, *nvroot, *ret;
2128573ca77eSGeorge Wilson 
2129573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2130573ca77eSGeorge Wilson 	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2131573ca77eSGeorge Wilson 
2132573ca77eSGeorge Wilson 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2133573ca77eSGeorge Wilson 	    &nvroot) == 0);
2134573ca77eSGeorge Wilson 
2135573ca77eSGeorge Wilson 	*avail_spare = B_FALSE;
2136cb04b873SMark J Musante 	*l2cache = B_FALSE;
2137daeb70e5SMark J Musante 	if (log != NULL)
2138daeb70e5SMark J Musante 		*log = B_FALSE;
2139573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2140573ca77eSGeorge Wilson 	nvlist_free(search);
2141573ca77eSGeorge Wilson 
2142573ca77eSGeorge Wilson 	return (ret);
2143573ca77eSGeorge Wilson }
2144573ca77eSGeorge Wilson 
214588ecc943SGeorge Wilson /*
214688ecc943SGeorge Wilson  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
214788ecc943SGeorge Wilson  */
214888ecc943SGeorge Wilson boolean_t
214988ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
215088ecc943SGeorge Wilson {
215188ecc943SGeorge Wilson 	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
215288ecc943SGeorge Wilson 	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
215388ecc943SGeorge Wilson 		return (B_TRUE);
215488ecc943SGeorge Wilson 	return (B_FALSE);
215588ecc943SGeorge Wilson }
215688ecc943SGeorge Wilson 
215799653d4eSeschrock nvlist_t *
2158fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2159ee0eb9f2SEric Schrock     boolean_t *l2cache, boolean_t *log)
2160ea8dc4b6Seschrock {
2161ea8dc4b6Seschrock 	char buf[MAXPATHLEN];
2162ea8dc4b6Seschrock 	char *end;
2163573ca77eSGeorge Wilson 	nvlist_t *nvroot, *search, *ret;
2164ea8dc4b6Seschrock 	uint64_t guid;
2165ea8dc4b6Seschrock 
2166573ca77eSGeorge Wilson 	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2167573ca77eSGeorge Wilson 
21680917b783Seschrock 	guid = strtoull(path, &end, 10);
2169ea8dc4b6Seschrock 	if (guid != 0 && *end == '\0') {
2170573ca77eSGeorge Wilson 		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
217188ecc943SGeorge Wilson 	} else if (zpool_vdev_is_interior(path)) {
217288ecc943SGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2173ea8dc4b6Seschrock 	} else if (path[0] != '/') {
21746401734dSWill Andrews 		(void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT,
21756401734dSWill Andrews 		    path);
2176573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2177ea8dc4b6Seschrock 	} else {
2178573ca77eSGeorge Wilson 		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2179ea8dc4b6Seschrock 	}
2180ea8dc4b6Seschrock 
2181ea8dc4b6Seschrock 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2182ea8dc4b6Seschrock 	    &nvroot) == 0);
2183ea8dc4b6Seschrock 
2184a43d325bSek 	*avail_spare = B_FALSE;
2185fa94a07fSbrendan 	*l2cache = B_FALSE;
2186ee0eb9f2SEric Schrock 	if (log != NULL)
2187ee0eb9f2SEric Schrock 		*log = B_FALSE;
2188573ca77eSGeorge Wilson 	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2189573ca77eSGeorge Wilson 	nvlist_free(search);
2190573ca77eSGeorge Wilson 
2191573ca77eSGeorge Wilson 	return (ret);
2192a43d325bSek }
2193a43d325bSek 
219419397407SSherry Moore static int
219519397407SSherry Moore vdev_online(nvlist_t *nv)
219619397407SSherry Moore {
219719397407SSherry Moore 	uint64_t ival;
219819397407SSherry Moore 
219919397407SSherry Moore 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
220019397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
220119397407SSherry Moore 	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
220219397407SSherry Moore 		return (0);
220319397407SSherry Moore 
220419397407SSherry Moore 	return (1);
220519397407SSherry Moore }
220619397407SSherry Moore 
220719397407SSherry Moore /*
220821ecdf64SLin Ling  * Helper function for zpool_get_physpaths().
220919397407SSherry Moore  */
2210753a6d45SSherry Moore static int
221121ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2212753a6d45SSherry Moore     size_t *bytes_written)
221319397407SSherry Moore {
2214753a6d45SSherry Moore 	size_t bytes_left, pos, rsz;
2215753a6d45SSherry Moore 	char *tmppath;
2216753a6d45SSherry Moore 	const char *format;
2217753a6d45SSherry Moore 
2218753a6d45SSherry Moore 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2219753a6d45SSherry Moore 	    &tmppath) != 0)
2220753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2221753a6d45SSherry Moore 
2222753a6d45SSherry Moore 	pos = *bytes_written;
2223753a6d45SSherry Moore 	bytes_left = physpath_size - pos;
2224753a6d45SSherry Moore 	format = (pos == 0) ? "%s" : " %s";
2225753a6d45SSherry Moore 
2226753a6d45SSherry Moore 	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2227753a6d45SSherry Moore 	*bytes_written += rsz;
2228753a6d45SSherry Moore 
2229753a6d45SSherry Moore 	if (rsz >= bytes_left) {
2230753a6d45SSherry Moore 		/* if physpath was not copied properly, clear it */
2231753a6d45SSherry Moore 		if (bytes_left != 0) {
2232753a6d45SSherry Moore 			physpath[pos] = 0;
2233753a6d45SSherry Moore 		}
2234753a6d45SSherry Moore 		return (EZFS_NOSPC);
2235753a6d45SSherry Moore 	}
2236753a6d45SSherry Moore 	return (0);
2237753a6d45SSherry Moore }
2238753a6d45SSherry Moore 
223921ecdf64SLin Ling static int
224021ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
224121ecdf64SLin Ling     size_t *rsz, boolean_t is_spare)
224221ecdf64SLin Ling {
224321ecdf64SLin Ling 	char *type;
224421ecdf64SLin Ling 	int ret;
224521ecdf64SLin Ling 
224621ecdf64SLin Ling 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
224721ecdf64SLin Ling 		return (EZFS_INVALCONFIG);
224821ecdf64SLin Ling 
224921ecdf64SLin Ling 	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
225021ecdf64SLin Ling 		/*
225121ecdf64SLin Ling 		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
225221ecdf64SLin Ling 		 * For a spare vdev, we only want to boot from the active
225321ecdf64SLin Ling 		 * spare device.
225421ecdf64SLin Ling 		 */
225521ecdf64SLin Ling 		if (is_spare) {
225621ecdf64SLin Ling 			uint64_t spare = 0;
225721ecdf64SLin Ling 			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
225821ecdf64SLin Ling 			    &spare);
225921ecdf64SLin Ling 			if (!spare)
226021ecdf64SLin Ling 				return (EZFS_INVALCONFIG);
226121ecdf64SLin Ling 		}
226221ecdf64SLin Ling 
226321ecdf64SLin Ling 		if (vdev_online(nv)) {
226421ecdf64SLin Ling 			if ((ret = vdev_get_one_physpath(nv, physpath,
226521ecdf64SLin Ling 			    phypath_size, rsz)) != 0)
226621ecdf64SLin Ling 				return (ret);
226721ecdf64SLin Ling 		}
226821ecdf64SLin Ling 	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
226921ecdf64SLin Ling 	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
227021ecdf64SLin Ling 	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
227121ecdf64SLin Ling 		nvlist_t **child;
227221ecdf64SLin Ling 		uint_t count;
227321ecdf64SLin Ling 		int i, ret;
227421ecdf64SLin Ling 
227521ecdf64SLin Ling 		if (nvlist_lookup_nvlist_array(nv,
227621ecdf64SLin Ling 		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
227721ecdf64SLin Ling 			return (EZFS_INVALCONFIG);
227821ecdf64SLin Ling 
227921ecdf64SLin Ling 		for (i = 0; i < count; i++) {
228021ecdf64SLin Ling 			ret = vdev_get_physpaths(child[i], physpath,
228121ecdf64SLin Ling 			    phypath_size, rsz, is_spare);
228221ecdf64SLin Ling 			if (ret == EZFS_NOSPC)
228321ecdf64SLin Ling 				return (ret);
228421ecdf64SLin Ling 		}
228521ecdf64SLin Ling 	}
228621ecdf64SLin Ling 
228721ecdf64SLin Ling 	return (EZFS_POOL_INVALARG);
228821ecdf64SLin Ling }
228921ecdf64SLin Ling 
2290753a6d45SSherry Moore /*
2291753a6d45SSherry Moore  * Get phys_path for a root pool config.
2292753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2293753a6d45SSherry Moore  */
2294753a6d45SSherry Moore static int
2295753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2296753a6d45SSherry Moore {
2297753a6d45SSherry Moore 	size_t rsz;
229819397407SSherry Moore 	nvlist_t *vdev_root;
229919397407SSherry Moore 	nvlist_t **child;
230019397407SSherry Moore 	uint_t count;
2301753a6d45SSherry Moore 	char *type;
2302753a6d45SSherry Moore 
2303753a6d45SSherry Moore 	rsz = 0;
2304753a6d45SSherry Moore 
2305753a6d45SSherry Moore 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2306753a6d45SSherry Moore 	    &vdev_root) != 0)
2307753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
2308753a6d45SSherry Moore 
2309753a6d45SSherry Moore 	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2310753a6d45SSherry Moore 	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2311753a6d45SSherry Moore 	    &child, &count) != 0)
2312753a6d45SSherry Moore 		return (EZFS_INVALCONFIG);
231319397407SSherry Moore 
231419397407SSherry Moore 	/*
23151a902ef8SHans Rosenfeld 	 * root pool can only have a single top-level vdev.
231619397407SSherry Moore 	 */
23171a902ef8SHans Rosenfeld 	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2318753a6d45SSherry Moore 		return (EZFS_POOL_INVALARG);
231919397407SSherry Moore 
232021ecdf64SLin Ling 	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
232121ecdf64SLin Ling 	    B_FALSE);
232219397407SSherry Moore 
2323753a6d45SSherry Moore 	/* No online devices */
2324753a6d45SSherry Moore 	if (rsz == 0)
2325753a6d45SSherry Moore 		return (EZFS_NODEVICE);
2326753a6d45SSherry Moore 
232719397407SSherry Moore 	return (0);
232819397407SSherry Moore }
232919397407SSherry Moore 
2330753a6d45SSherry Moore /*
2331753a6d45SSherry Moore  * Get phys_path for a root pool
2332753a6d45SSherry Moore  * Return 0 on success; non-zero on failure.
2333753a6d45SSherry Moore  */
2334753a6d45SSherry Moore int
2335753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2336753a6d45SSherry Moore {
2337753a6d45SSherry Moore 	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2338753a6d45SSherry Moore 	    phypath_size));
2339753a6d45SSherry Moore }
2340753a6d45SSherry Moore 
2341573ca77eSGeorge Wilson /*
2342573ca77eSGeorge Wilson  * If the device has being dynamically expanded then we need to relabel
2343573ca77eSGeorge Wilson  * the disk to use the new unallocated space.
2344573ca77eSGeorge Wilson  */
2345573ca77eSGeorge Wilson static int
2346573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2347573ca77eSGeorge Wilson {
2348573ca77eSGeorge Wilson 	char path[MAXPATHLEN];
2349573ca77eSGeorge Wilson 	char errbuf[1024];
2350573ca77eSGeorge Wilson 	int fd, error;
2351573ca77eSGeorge Wilson 	int (*_efi_use_whole_disk)(int);
2352573ca77eSGeorge Wilson 
2353573ca77eSGeorge Wilson 	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2354573ca77eSGeorge Wilson 	    "efi_use_whole_disk")) == NULL)
2355573ca77eSGeorge Wilson 		return (-1);
2356573ca77eSGeorge Wilson 
23576401734dSWill Andrews 	(void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2358573ca77eSGeorge Wilson 
2359573ca77eSGeorge Wilson 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2360573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2361573ca77eSGeorge Wilson 		    "relabel '%s': unable to open device"), name);
2362573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2363573ca77eSGeorge Wilson 	}
2364573ca77eSGeorge Wilson 
2365573ca77eSGeorge Wilson 	/*
2366573ca77eSGeorge Wilson 	 * It's possible that we might encounter an error if the device
2367573ca77eSGeorge Wilson 	 * does not have any unallocated space left. If so, we simply
2368573ca77eSGeorge Wilson 	 * ignore that error and continue on.
2369573ca77eSGeorge Wilson 	 */
2370573ca77eSGeorge Wilson 	error = _efi_use_whole_disk(fd);
2371573ca77eSGeorge Wilson 	(void) close(fd);
2372573ca77eSGeorge Wilson 	if (error && error != VT_ENOSPC) {
2373573ca77eSGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2374573ca77eSGeorge Wilson 		    "relabel '%s': unable to read disk capacity"), name);
2375573ca77eSGeorge Wilson 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2376573ca77eSGeorge Wilson 	}
2377573ca77eSGeorge Wilson 	return (0);
2378573ca77eSGeorge Wilson }
2379573ca77eSGeorge Wilson 
2380fa9e4066Sahrens /*
23813d7072f8Seschrock  * Bring the specified vdev online.   The 'flags' parameter is a set of the
23823d7072f8Seschrock  * ZFS_ONLINE_* flags.
2383fa9e4066Sahrens  */
2384fa9e4066Sahrens int
23853d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
23863d7072f8Seschrock     vdev_state_t *newstate)
2387fa9e4066Sahrens {
2388fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2389fa9e4066Sahrens 	char msg[1024];
239099653d4eSeschrock 	nvlist_t *tgt;
2391573ca77eSGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
239299653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2393fa9e4066Sahrens 
2394573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND) {
2395573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2396573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2397573ca77eSGeorge Wilson 	} else {
2398573ca77eSGeorge Wilson 		(void) snprintf(msg, sizeof (msg),
2399573ca77eSGeorge Wilson 		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2400573ca77eSGeorge Wilson 	}
2401ea8dc4b6Seschrock 
2402fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2403ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2404573ca77eSGeorge Wilson 	    &islog)) == NULL)
240599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2406fa9e4066Sahrens 
240799653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2408fa9e4066Sahrens 
2409069f55e2SEric Schrock 	if (avail_spare)
2410a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2411a43d325bSek 
2412573ca77eSGeorge Wilson 	if (flags & ZFS_ONLINE_EXPAND ||
2413573ca77eSGeorge Wilson 	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2414573ca77eSGeorge Wilson 		char *pathname = NULL;
2415573ca77eSGeorge Wilson 		uint64_t wholedisk = 0;
2416573ca77eSGeorge Wilson 
2417573ca77eSGeorge Wilson 		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2418573ca77eSGeorge Wilson 		    &wholedisk);
2419573ca77eSGeorge Wilson 		verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2420573ca77eSGeorge Wilson 		    &pathname) == 0);
2421573ca77eSGeorge Wilson 
2422573ca77eSGeorge Wilson 		/*
2423573ca77eSGeorge Wilson 		 * XXX - L2ARC 1.0 devices can't support expansion.
2424573ca77eSGeorge Wilson 		 */
2425573ca77eSGeorge Wilson 		if (l2cache) {
2426573ca77eSGeorge Wilson 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2427573ca77eSGeorge Wilson 			    "cannot expand cache devices"));
2428573ca77eSGeorge Wilson 			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2429573ca77eSGeorge Wilson 		}
2430573ca77eSGeorge Wilson 
2431573ca77eSGeorge Wilson 		if (wholedisk) {
24326401734dSWill Andrews 			pathname += strlen(ZFS_DISK_ROOT) + 1;
2433cb04b873SMark J Musante 			(void) zpool_relabel_disk(hdl, pathname);
2434573ca77eSGeorge Wilson 		}
2435573ca77eSGeorge Wilson 	}
2436573ca77eSGeorge Wilson 
24373d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_ONLINE;
24383d7072f8Seschrock 	zc.zc_obj = flags;
2439fa9e4066Sahrens 
2440cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
24411195e687SMark J Musante 		if (errno == EINVAL) {
24421195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
24431195e687SMark J Musante 			    "from this pool into a new one.  Use '%s' "
24441195e687SMark J Musante 			    "instead"), "zpool detach");
24451195e687SMark J Musante 			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
24461195e687SMark J Musante 		}
24473d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
24481195e687SMark J Musante 	}
24493d7072f8Seschrock 
24503d7072f8Seschrock 	*newstate = zc.zc_cookie;
24513d7072f8Seschrock 	return (0);
2452fa9e4066Sahrens }
2453fa9e4066Sahrens 
2454fa9e4066Sahrens /*
2455fa9e4066Sahrens  * Take the specified vdev offline
2456fa9e4066Sahrens  */
2457fa9e4066Sahrens int
24583d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2459fa9e4066Sahrens {
2460fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2461fa9e4066Sahrens 	char msg[1024];
246299653d4eSeschrock 	nvlist_t *tgt;
2463fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
246499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2465fa9e4066Sahrens 
2466ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2467ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2468ea8dc4b6Seschrock 
2469fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2470ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2471ee0eb9f2SEric Schrock 	    NULL)) == NULL)
247299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
247399653d4eSeschrock 
247499653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2475fa9e4066Sahrens 
2476069f55e2SEric Schrock 	if (avail_spare)
2477a43d325bSek 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2478a43d325bSek 
24793d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_OFFLINE;
24803d7072f8Seschrock 	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
24813d7072f8Seschrock 
2482cb04b873SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
24833d7072f8Seschrock 		return (0);
24843d7072f8Seschrock 
24853d7072f8Seschrock 	switch (errno) {
24863d7072f8Seschrock 	case EBUSY:
24873d7072f8Seschrock 
24883d7072f8Seschrock 		/*
24893d7072f8Seschrock 		 * There are no other replicas of this device.
24903d7072f8Seschrock 		 */
24913d7072f8Seschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
24923d7072f8Seschrock 
2493e6ca193dSGeorge Wilson 	case EEXIST:
2494e6ca193dSGeorge Wilson 		/*
2495e6ca193dSGeorge Wilson 		 * The log device has unplayed logs
2496e6ca193dSGeorge Wilson 		 */
2497e6ca193dSGeorge Wilson 		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2498e6ca193dSGeorge Wilson 
24993d7072f8Seschrock 	default:
25003d7072f8Seschrock 		return (zpool_standard_error(hdl, errno, msg));
25013d7072f8Seschrock 	}
25023d7072f8Seschrock }
25033d7072f8Seschrock 
25043d7072f8Seschrock /*
25053d7072f8Seschrock  * Mark the given vdev faulted.
25063d7072f8Seschrock  */
25073d7072f8Seschrock int
2508069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25093d7072f8Seschrock {
25103d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25113d7072f8Seschrock 	char msg[1024];
25123d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25133d7072f8Seschrock 
25143d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25153d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2516441d80aaSlling 
25173d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25183d7072f8Seschrock 	zc.zc_guid = guid;
25193d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_FAULTED;
2520069f55e2SEric Schrock 	zc.zc_obj = aux;
25213d7072f8Seschrock 
2522cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2523fa9e4066Sahrens 		return (0);
2524fa9e4066Sahrens 
2525fa9e4066Sahrens 	switch (errno) {
252699653d4eSeschrock 	case EBUSY:
2527fa9e4066Sahrens 
2528fa9e4066Sahrens 		/*
2529fa9e4066Sahrens 		 * There are no other replicas of this device.
2530fa9e4066Sahrens 		 */
253199653d4eSeschrock 		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2532fa9e4066Sahrens 
253399653d4eSeschrock 	default:
253499653d4eSeschrock 		return (zpool_standard_error(hdl, errno, msg));
2535fa9e4066Sahrens 	}
25363d7072f8Seschrock 
25373d7072f8Seschrock }
25383d7072f8Seschrock 
25393d7072f8Seschrock /*
25403d7072f8Seschrock  * Mark the given vdev degraded.
25413d7072f8Seschrock  */
25423d7072f8Seschrock int
2543069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
25443d7072f8Seschrock {
25453d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
25463d7072f8Seschrock 	char msg[1024];
25473d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
25483d7072f8Seschrock 
25493d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
25503d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
25513d7072f8Seschrock 
25523d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
25533d7072f8Seschrock 	zc.zc_guid = guid;
25543d7072f8Seschrock 	zc.zc_cookie = VDEV_STATE_DEGRADED;
2555069f55e2SEric Schrock 	zc.zc_obj = aux;
25563d7072f8Seschrock 
2557cb04b873SMark J Musante 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
25583d7072f8Seschrock 		return (0);
25593d7072f8Seschrock 
25603d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
256199653d4eSeschrock }
256299653d4eSeschrock 
256399653d4eSeschrock /*
256499653d4eSeschrock  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
256599653d4eSeschrock  * a hot spare.
256699653d4eSeschrock  */
256799653d4eSeschrock static boolean_t
256899653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
256999653d4eSeschrock {
257099653d4eSeschrock 	nvlist_t **child;
257199653d4eSeschrock 	uint_t c, children;
257299653d4eSeschrock 	char *type;
257399653d4eSeschrock 
257499653d4eSeschrock 	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
257599653d4eSeschrock 	    &children) == 0) {
257699653d4eSeschrock 		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
257799653d4eSeschrock 		    &type) == 0);
257899653d4eSeschrock 
257999653d4eSeschrock 		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
258099653d4eSeschrock 		    children == 2 && child[which] == tgt)
258199653d4eSeschrock 			return (B_TRUE);
258299653d4eSeschrock 
258399653d4eSeschrock 		for (c = 0; c < children; c++)
258499653d4eSeschrock 			if (is_replacing_spare(child[c], tgt, which))
258599653d4eSeschrock 				return (B_TRUE);
258699653d4eSeschrock 	}
258799653d4eSeschrock 
258899653d4eSeschrock 	return (B_FALSE);
2589fa9e4066Sahrens }
2590fa9e4066Sahrens 
2591fa9e4066Sahrens /*
2592fa9e4066Sahrens  * Attach new_disk (fully described by nvroot) to old_disk.
25938654d025Sperrin  * If 'replacing' is specified, the new disk will replace the old one.
2594fa9e4066Sahrens  */
2595fa9e4066Sahrens int
2596fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
2597fa9e4066Sahrens     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2598fa9e4066Sahrens {
2599fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2600fa9e4066Sahrens 	char msg[1024];
2601fa9e4066Sahrens 	int ret;
260299653d4eSeschrock 	nvlist_t *tgt;
2603ee0eb9f2SEric Schrock 	boolean_t avail_spare, l2cache, islog;
2604ee0eb9f2SEric Schrock 	uint64_t val;
2605cb04b873SMark J Musante 	char *newname;
260699653d4eSeschrock 	nvlist_t **child;
260799653d4eSeschrock 	uint_t children;
260899653d4eSeschrock 	nvlist_t *config_root;
260999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
26104263d13fSGeorge Wilson 	boolean_t rootpool = zpool_is_bootable(zhp);
2611fa9e4066Sahrens 
2612ea8dc4b6Seschrock 	if (replacing)
2613ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2614ea8dc4b6Seschrock 		    "cannot replace %s with %s"), old_disk, new_disk);
2615ea8dc4b6Seschrock 	else
2616ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2617ea8dc4b6Seschrock 		    "cannot attach %s to %s"), new_disk, old_disk);
2618ea8dc4b6Seschrock 
2619fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2620ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2621ee0eb9f2SEric Schrock 	    &islog)) == 0)
262299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
262399653d4eSeschrock 
2624a43d325bSek 	if (avail_spare)
262599653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
262699653d4eSeschrock 
2627fa94a07fSbrendan 	if (l2cache)
2628fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2629fa94a07fSbrendan 
263099653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2631fa9e4066Sahrens 	zc.zc_cookie = replacing;
2632fa9e4066Sahrens 
263399653d4eSeschrock 	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
263499653d4eSeschrock 	    &child, &children) != 0 || children != 1) {
263599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
263699653d4eSeschrock 		    "new device must be a single disk"));
263799653d4eSeschrock 		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
263899653d4eSeschrock 	}
263999653d4eSeschrock 
264099653d4eSeschrock 	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
264199653d4eSeschrock 	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
264299653d4eSeschrock 
264388ecc943SGeorge Wilson 	if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
26440430f8daSeschrock 		return (-1);
26450430f8daSeschrock 
264699653d4eSeschrock 	/*
264799653d4eSeschrock 	 * If the target is a hot spare that has been swapped in, we can only
264899653d4eSeschrock 	 * replace it with another hot spare.
264999653d4eSeschrock 	 */
265099653d4eSeschrock 	if (replacing &&
265199653d4eSeschrock 	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2652ee0eb9f2SEric Schrock 	    (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2653ee0eb9f2SEric Schrock 	    NULL) == NULL || !avail_spare) &&
2654ee0eb9f2SEric Schrock 	    is_replacing_spare(config_root, tgt, 1)) {
265599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
265699653d4eSeschrock 		    "can only be replaced by another hot spare"));
26570430f8daSeschrock 		free(newname);
265899653d4eSeschrock 		return (zfs_error(hdl, EZFS_BADTARGET, msg));
265999653d4eSeschrock 	}
266099653d4eSeschrock 
26610430f8daSeschrock 	free(newname);
26620430f8daSeschrock 
2663990b4856Slling 	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
266499653d4eSeschrock 		return (-1);
2665fa9e4066Sahrens 
2666cb04b873SMark J Musante 	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2667fa9e4066Sahrens 
2668e9dbad6fSeschrock 	zcmd_free_nvlists(&zc);
2669fa9e4066Sahrens 
2670b5b76fecSGeorge Wilson 	if (ret == 0) {
2671b5b76fecSGeorge Wilson 		if (rootpool) {
267221ecdf64SLin Ling 			/*
267321ecdf64SLin Ling 			 * XXX need a better way to prevent user from
267421ecdf64SLin Ling 			 * booting up a half-baked vdev.
267521ecdf64SLin Ling 			 */
267621ecdf64SLin Ling 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
267721ecdf64SLin Ling 			    "sure to wait until resilver is done "
267821ecdf64SLin Ling 			    "before rebooting.\n"));
2679b5b76fecSGeorge Wilson 		}
2680fa9e4066Sahrens 		return (0);
2681b5b76fecSGeorge Wilson 	}
2682fa9e4066Sahrens 
2683fa9e4066Sahrens 	switch (errno) {
2684ea8dc4b6Seschrock 	case ENOTSUP:
2685fa9e4066Sahrens 		/*
2686fa9e4066Sahrens 		 * Can't attach to or replace this type of vdev.
2687fa9e4066Sahrens 		 */
26888654d025Sperrin 		if (replacing) {
2689cb04b873SMark J Musante 			uint64_t version = zpool_get_prop_int(zhp,
2690cb04b873SMark J Musante 			    ZPOOL_PROP_VERSION, NULL);
2691cb04b873SMark J Musante 
2692ee0eb9f2SEric Schrock 			if (islog)
26938654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26948654d025Sperrin 				    "cannot replace a log with a spare"));
2695cb04b873SMark J Musante 			else if (version >= SPA_VERSION_MULTI_REPLACE)
2696cb04b873SMark J Musante 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2697cb04b873SMark J Musante 				    "already in replacing/spare config; wait "
2698cb04b873SMark J Musante 				    "for completion or use 'zpool detach'"));
26998654d025Sperrin 			else
27008654d025Sperrin 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27018654d025Sperrin 				    "cannot replace a replacing device"));
27028654d025Sperrin 		} else {
270399653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270499653d4eSeschrock 			    "can only attach to mirrors and top-level "
270599653d4eSeschrock 			    "disks"));
27068654d025Sperrin 		}
270799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2708fa9e4066Sahrens 		break;
2709fa9e4066Sahrens 
2710ea8dc4b6Seschrock 	case EINVAL:
2711fa9e4066Sahrens 		/*
2712fa9e4066Sahrens 		 * The new device must be a single disk.
2713fa9e4066Sahrens 		 */
271499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
271599653d4eSeschrock 		    "new device must be a single disk"));
271699653d4eSeschrock 		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2717fa9e4066Sahrens 		break;
2718fa9e4066Sahrens 
2719ea8dc4b6Seschrock 	case EBUSY:
272099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
272199653d4eSeschrock 		    new_disk);
272299653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2723fa9e4066Sahrens 		break;
2724fa9e4066Sahrens 
2725ea8dc4b6Seschrock 	case EOVERFLOW:
2726fa9e4066Sahrens 		/*
2727fa9e4066Sahrens 		 * The new device is too small.
2728fa9e4066Sahrens 		 */
272999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
273099653d4eSeschrock 		    "device is too small"));
273199653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2732fa9e4066Sahrens 		break;
2733fa9e4066Sahrens 
2734ea8dc4b6Seschrock 	case EDOM:
2735fa9e4066Sahrens 		/*
2736fa9e4066Sahrens 		 * The new device has a different alignment requirement.
2737fa9e4066Sahrens 		 */
273899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
273999653d4eSeschrock 		    "devices have different sector alignment"));
274099653d4eSeschrock 		(void) zfs_error(hdl, EZFS_BADDEV, msg);
2741fa9e4066Sahrens 		break;
2742fa9e4066Sahrens 
2743ea8dc4b6Seschrock 	case ENAMETOOLONG:
2744fa9e4066Sahrens 		/*
2745fa9e4066Sahrens 		 * The resulting top-level vdev spec won't fit in the label.
2746fa9e4066Sahrens 		 */
274799653d4eSeschrock 		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2748fa9e4066Sahrens 		break;
2749fa9e4066Sahrens 
2750ea8dc4b6Seschrock 	default:
275199653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2752fa9e4066Sahrens 	}
2753fa9e4066Sahrens 
275499653d4eSeschrock 	return (-1);
2755fa9e4066Sahrens }
2756fa9e4066Sahrens 
2757fa9e4066Sahrens /*
2758fa9e4066Sahrens  * Detach the specified device.
2759fa9e4066Sahrens  */
2760fa9e4066Sahrens int
2761fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2762fa9e4066Sahrens {
2763fa9e4066Sahrens 	zfs_cmd_t zc = { 0 };
2764fa9e4066Sahrens 	char msg[1024];
276599653d4eSeschrock 	nvlist_t *tgt;
2766fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
276799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
2768fa9e4066Sahrens 
2769ea8dc4b6Seschrock 	(void) snprintf(msg, sizeof (msg),
2770ea8dc4b6Seschrock 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2771ea8dc4b6Seschrock 
2772fa9e4066Sahrens 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2773ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2774ee0eb9f2SEric Schrock 	    NULL)) == 0)
277599653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2776fa9e4066Sahrens 
2777a43d325bSek 	if (avail_spare)
277899653d4eSeschrock 		return (zfs_error(hdl, EZFS_ISSPARE, msg));
277999653d4eSeschrock 
2780fa94a07fSbrendan 	if (l2cache)
2781fa94a07fSbrendan 		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2782fa94a07fSbrendan 
278399653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
278499653d4eSeschrock 
2785ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2786fa9e4066Sahrens 		return (0);
2787fa9e4066Sahrens 
2788fa9e4066Sahrens 	switch (errno) {
2789fa9e4066Sahrens 
2790ea8dc4b6Seschrock 	case ENOTSUP:
2791fa9e4066Sahrens 		/*
2792fa9e4066Sahrens 		 * Can't detach from this type of vdev.
2793fa9e4066Sahrens 		 */
279499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
279599653d4eSeschrock 		    "applicable to mirror and replacing vdevs"));
2796cb04b873SMark J Musante 		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
2797fa9e4066Sahrens 		break;
2798fa9e4066Sahrens 
2799ea8dc4b6Seschrock 	case EBUSY:
2800fa9e4066Sahrens 		/*
2801fa9e4066Sahrens 		 * There are no other replicas of this device.
2802fa9e4066Sahrens 		 */
280399653d4eSeschrock 		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2804fa9e4066Sahrens 		break;
2805fa9e4066Sahrens 
2806ea8dc4b6Seschrock 	default:
280799653d4eSeschrock 		(void) zpool_standard_error(hdl, errno, msg);
2808ea8dc4b6Seschrock 	}
2809ea8dc4b6Seschrock 
281099653d4eSeschrock 	return (-1);
281199653d4eSeschrock }
281299653d4eSeschrock 
28131195e687SMark J Musante /*
28141195e687SMark J Musante  * Find a mirror vdev in the source nvlist.
28151195e687SMark J Musante  *
28161195e687SMark J Musante  * The mchild array contains a list of disks in one of the top-level mirrors
28171195e687SMark J Musante  * of the source pool.  The schild array contains a list of disks that the
28181195e687SMark J Musante  * user specified on the command line.  We loop over the mchild array to
28191195e687SMark J Musante  * see if any entry in the schild array matches.
28201195e687SMark J Musante  *
28211195e687SMark J Musante  * If a disk in the mchild array is found in the schild array, we return
28221195e687SMark J Musante  * the index of that entry.  Otherwise we return -1.
28231195e687SMark J Musante  */
28241195e687SMark J Musante static int
28251195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
28261195e687SMark J Musante     nvlist_t **schild, uint_t schildren)
28271195e687SMark J Musante {
28281195e687SMark J Musante 	uint_t mc;
28291195e687SMark J Musante 
28301195e687SMark J Musante 	for (mc = 0; mc < mchildren; mc++) {
28311195e687SMark J Musante 		uint_t sc;
28321195e687SMark J Musante 		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28331195e687SMark J Musante 		    mchild[mc], B_FALSE);
28341195e687SMark J Musante 
28351195e687SMark J Musante 		for (sc = 0; sc < schildren; sc++) {
28361195e687SMark J Musante 			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
28371195e687SMark J Musante 			    schild[sc], B_FALSE);
28381195e687SMark J Musante 			boolean_t result = (strcmp(mpath, spath) == 0);
28391195e687SMark J Musante 
28401195e687SMark J Musante 			free(spath);
28411195e687SMark J Musante 			if (result) {
28421195e687SMark J Musante 				free(mpath);
28431195e687SMark J Musante 				return (mc);
28441195e687SMark J Musante 			}
28451195e687SMark J Musante 		}
28461195e687SMark J Musante 
28471195e687SMark J Musante 		free(mpath);
28481195e687SMark J Musante 	}
28491195e687SMark J Musante 
28501195e687SMark J Musante 	return (-1);
28511195e687SMark J Musante }
28521195e687SMark J Musante 
28531195e687SMark J Musante /*
28541195e687SMark J Musante  * Split a mirror pool.  If newroot points to null, then a new nvlist
28551195e687SMark J Musante  * is generated and it is the responsibility of the caller to free it.
28561195e687SMark J Musante  */
28571195e687SMark J Musante int
28581195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
28591195e687SMark J Musante     nvlist_t *props, splitflags_t flags)
28601195e687SMark J Musante {
28611195e687SMark J Musante 	zfs_cmd_t zc = { 0 };
28621195e687SMark J Musante 	char msg[1024];
28631195e687SMark J Musante 	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
28641195e687SMark J Musante 	nvlist_t **varray = NULL, *zc_props = NULL;
28651195e687SMark J Musante 	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
28661195e687SMark J Musante 	libzfs_handle_t *hdl = zhp->zpool_hdl;
28671195e687SMark J Musante 	uint64_t vers;
28681195e687SMark J Musante 	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
28691195e687SMark J Musante 	int retval = 0;
28701195e687SMark J Musante 
28711195e687SMark J Musante 	(void) snprintf(msg, sizeof (msg),
28721195e687SMark J Musante 	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
28731195e687SMark J Musante 
28741195e687SMark J Musante 	if (!zpool_name_valid(hdl, B_FALSE, newname))
28751195e687SMark J Musante 		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
28761195e687SMark J Musante 
28771195e687SMark J Musante 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
28781195e687SMark J Musante 		(void) fprintf(stderr, gettext("Internal error: unable to "
28791195e687SMark J Musante 		    "retrieve pool configuration\n"));
28801195e687SMark J Musante 		return (-1);
28811195e687SMark J Musante 	}
28821195e687SMark J Musante 
28831195e687SMark J Musante 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
28841195e687SMark J Musante 	    == 0);
28851195e687SMark J Musante 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
28861195e687SMark J Musante 
28871195e687SMark J Musante 	if (props) {
2888f9af39baSGeorge Wilson 		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
28891195e687SMark J Musante 		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2890f9af39baSGeorge Wilson 		    props, vers, flags, msg)) == NULL)
28911195e687SMark J Musante 			return (-1);
28921195e687SMark J Musante 	}
28931195e687SMark J Musante 
28941195e687SMark J Musante 	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
28951195e687SMark J Musante 	    &children) != 0) {
28961195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28971195e687SMark J Musante 		    "Source pool is missing vdev tree"));
2898aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(zc_props);
28991195e687SMark J Musante 		return (-1);
29001195e687SMark J Musante 	}
29011195e687SMark J Musante 
29021195e687SMark J Musante 	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
29031195e687SMark J Musante 	vcount = 0;
29041195e687SMark J Musante 
29051195e687SMark J Musante 	if (*newroot == NULL ||
29061195e687SMark J Musante 	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
29071195e687SMark J Musante 	    &newchild, &newchildren) != 0)
29081195e687SMark J Musante 		newchildren = 0;
29091195e687SMark J Musante 
29101195e687SMark J Musante 	for (c = 0; c < children; c++) {
29111195e687SMark J Musante 		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
29121195e687SMark J Musante 		char *type;
29131195e687SMark J Musante 		nvlist_t **mchild, *vdev;
29141195e687SMark J Musante 		uint_t mchildren;
29151195e687SMark J Musante 		int entry;
29161195e687SMark J Musante 
29171195e687SMark J Musante 		/*
29181195e687SMark J Musante 		 * Unlike cache & spares, slogs are stored in the
29191195e687SMark J Musante 		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
29201195e687SMark J Musante 		 */
29211195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
29221195e687SMark J Musante 		    &is_log);
29231195e687SMark J Musante 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
29241195e687SMark J Musante 		    &is_hole);
29251195e687SMark J Musante 		if (is_log || is_hole) {
29261195e687SMark J Musante 			/*
29271195e687SMark J Musante 			 * Create a hole vdev and put it in the config.
29281195e687SMark J Musante 			 */
29291195e687SMark J Musante 			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
29301195e687SMark J Musante 				goto out;
29311195e687SMark J Musante 			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
29321195e687SMark J Musante 			    VDEV_TYPE_HOLE) != 0)
29331195e687SMark J Musante 				goto out;
29341195e687SMark J Musante 			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
29351195e687SMark J Musante 			    1) != 0)
29361195e687SMark J Musante 				goto out;
29371195e687SMark J Musante 			if (lastlog == 0)
29381195e687SMark J Musante 				lastlog = vcount;
29391195e687SMark J Musante 			varray[vcount++] = vdev;
29401195e687SMark J Musante 			continue;
29411195e687SMark J Musante 		}
29421195e687SMark J Musante 		lastlog = 0;
29431195e687SMark J Musante 		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
29441195e687SMark J Musante 		    == 0);
29451195e687SMark J Musante 		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
29461195e687SMark J Musante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29471195e687SMark J Musante 			    "Source pool must be composed only of mirrors\n"));
29481195e687SMark J Musante 			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29491195e687SMark J Musante 			goto out;
29501195e687SMark J Musante 		}
29511195e687SMark J Musante 
29521195e687SMark J Musante 		verify(nvlist_lookup_nvlist_array(child[c],
29531195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
29541195e687SMark J Musante 
29551195e687SMark J Musante 		/* find or add an entry for this top-level vdev */
29561195e687SMark J Musante 		if (newchildren > 0 &&
29571195e687SMark J Musante 		    (entry = find_vdev_entry(zhp, mchild, mchildren,
29581195e687SMark J Musante 		    newchild, newchildren)) >= 0) {
29591195e687SMark J Musante 			/* We found a disk that the user specified. */
29601195e687SMark J Musante 			vdev = mchild[entry];
29611195e687SMark J Musante 			++found;
29621195e687SMark J Musante 		} else {
29631195e687SMark J Musante 			/* User didn't specify a disk for this vdev. */
29641195e687SMark J Musante 			vdev = mchild[mchildren - 1];
29651195e687SMark J Musante 		}
29661195e687SMark J Musante 
29671195e687SMark J Musante 		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
29681195e687SMark J Musante 			goto out;
29691195e687SMark J Musante 	}
29701195e687SMark J Musante 
29711195e687SMark J Musante 	/* did we find every disk the user specified? */
29721195e687SMark J Musante 	if (found != newchildren) {
29731195e687SMark J Musante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
29741195e687SMark J Musante 		    "include at most one disk from each mirror"));
29751195e687SMark J Musante 		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
29761195e687SMark J Musante 		goto out;
29771195e687SMark J Musante 	}
29781195e687SMark J Musante 
29791195e687SMark J Musante 	/* Prepare the nvlist for populating. */
29801195e687SMark J Musante 	if (*newroot == NULL) {
29811195e687SMark J Musante 		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
29821195e687SMark J Musante 			goto out;
29831195e687SMark J Musante 		freelist = B_TRUE;
29841195e687SMark J Musante 		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
29851195e687SMark J Musante 		    VDEV_TYPE_ROOT) != 0)
29861195e687SMark J Musante 			goto out;
29871195e687SMark J Musante 	} else {
29881195e687SMark J Musante 		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
29891195e687SMark J Musante 	}
29901195e687SMark J Musante 
29911195e687SMark J Musante 	/* Add all the children we found */
29921195e687SMark J Musante 	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
29931195e687SMark J Musante 	    lastlog == 0 ? vcount : lastlog) != 0)
29941195e687SMark J Musante 		goto out;
29951195e687SMark J Musante 
29961195e687SMark J Musante 	/*
29971195e687SMark J Musante 	 * If we're just doing a dry run, exit now with success.
29981195e687SMark J Musante 	 */
29991195e687SMark J Musante 	if (flags.dryrun) {
30001195e687SMark J Musante 		memory_err = B_FALSE;
30011195e687SMark J Musante 		freelist = B_FALSE;
30021195e687SMark J Musante 		goto out;
30031195e687SMark J Musante 	}
30041195e687SMark J Musante 
30051195e687SMark J Musante 	/* now build up the config list & call the ioctl */
30061195e687SMark J Musante 	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
30071195e687SMark J Musante 		goto out;
30081195e687SMark J Musante 
30091195e687SMark J Musante 	if (nvlist_add_nvlist(newconfig,
30101195e687SMark J Musante 	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
30111195e687SMark J Musante 	    nvlist_add_string(newconfig,
30121195e687SMark J Musante 	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
30131195e687SMark J Musante 	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
30141195e687SMark J Musante 		goto out;
30151195e687SMark J Musante 
30161195e687SMark J Musante 	/*
30171195e687SMark J Musante 	 * The new pool is automatically part of the namespace unless we
30181195e687SMark J Musante 	 * explicitly export it.
30191195e687SMark J Musante 	 */
30201195e687SMark J Musante 	if (!flags.import)
30211195e687SMark J Musante 		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
30221195e687SMark J Musante 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30231195e687SMark J Musante 	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
30241195e687SMark J Musante 	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
30251195e687SMark J Musante 		goto out;
30261195e687SMark J Musante 	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
30271195e687SMark J Musante 		goto out;
30281195e687SMark J Musante 
30291195e687SMark J Musante 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
30301195e687SMark J Musante 		retval = zpool_standard_error(hdl, errno, msg);
30311195e687SMark J Musante 		goto out;
30321195e687SMark J Musante 	}
30331195e687SMark J Musante 
30341195e687SMark J Musante 	freelist = B_FALSE;
30351195e687SMark J Musante 	memory_err = B_FALSE;
30361195e687SMark J Musante 
30371195e687SMark J Musante out:
30381195e687SMark J Musante 	if (varray != NULL) {
30391195e687SMark J Musante 		int v;
30401195e687SMark J Musante 
30411195e687SMark J Musante 		for (v = 0; v < vcount; v++)
30421195e687SMark J Musante 			nvlist_free(varray[v]);
30431195e687SMark J Musante 		free(varray);
30441195e687SMark J Musante 	}
30451195e687SMark J Musante 	zcmd_free_nvlists(&zc);
3046aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(zc_props);
3047aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(newconfig);
30481195e687SMark J Musante 	if (freelist) {
30491195e687SMark J Musante 		nvlist_free(*newroot);
30501195e687SMark J Musante 		*newroot = NULL;
30511195e687SMark J Musante 	}
30521195e687SMark J Musante 
30531195e687SMark J Musante 	if (retval != 0)
30541195e687SMark J Musante 		return (retval);
30551195e687SMark J Musante 
30561195e687SMark J Musante 	if (memory_err)
30571195e687SMark J Musante 		return (no_memory(hdl));
30581195e687SMark J Musante 
30591195e687SMark J Musante 	return (0);
30601195e687SMark J Musante }
30611195e687SMark J Musante 
306299653d4eSeschrock /*
3063fa94a07fSbrendan  * Remove the given device.  Currently, this is supported only for hot spares
3064fa94a07fSbrendan  * and level 2 cache devices.
306599653d4eSeschrock  */
306699653d4eSeschrock int
306799653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
306899653d4eSeschrock {
306999653d4eSeschrock 	zfs_cmd_t zc = { 0 };
307099653d4eSeschrock 	char msg[1024];
307199653d4eSeschrock 	nvlist_t *tgt;
307288ecc943SGeorge Wilson 	boolean_t avail_spare, l2cache, islog;
307399653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
307488ecc943SGeorge Wilson 	uint64_t version;
307599653d4eSeschrock 
307699653d4eSeschrock 	(void) snprintf(msg, sizeof (msg),
307799653d4eSeschrock 	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
307899653d4eSeschrock 
307999653d4eSeschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3080ee0eb9f2SEric Schrock 	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
308188ecc943SGeorge Wilson 	    &islog)) == 0)
308299653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
308388ecc943SGeorge Wilson 	/*
308488ecc943SGeorge Wilson 	 * XXX - this should just go away.
308588ecc943SGeorge Wilson 	 */
308688ecc943SGeorge Wilson 	if (!avail_spare && !l2cache && !islog) {
308799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
308888ecc943SGeorge Wilson 		    "only inactive hot spares, cache, top-level, "
308988ecc943SGeorge Wilson 		    "or log devices can be removed"));
309099653d4eSeschrock 		return (zfs_error(hdl, EZFS_NODEVICE, msg));
309199653d4eSeschrock 	}
309299653d4eSeschrock 
309388ecc943SGeorge Wilson 	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
309488ecc943SGeorge Wilson 	if (islog && version < SPA_VERSION_HOLES) {
309588ecc943SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
309688ecc943SGeorge Wilson 		    "pool must be upgrade to support log removal"));
309788ecc943SGeorge Wilson 		return (zfs_error(hdl, EZFS_BADVERSION, msg));
309888ecc943SGeorge Wilson 	}
309988ecc943SGeorge Wilson 
310099653d4eSeschrock 	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
310199653d4eSeschrock 
3102ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
310399653d4eSeschrock 		return (0);
310499653d4eSeschrock 
310599653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3106ea8dc4b6Seschrock }
3107ea8dc4b6Seschrock 
3108ea8dc4b6Seschrock /*
3109ea8dc4b6Seschrock  * Clear the errors for the pool, or the particular device if specified.
3110ea8dc4b6Seschrock  */
3111ea8dc4b6Seschrock int
3112468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3113ea8dc4b6Seschrock {
3114ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3115ea8dc4b6Seschrock 	char msg[1024];
311699653d4eSeschrock 	nvlist_t *tgt;
3117468c413aSTim Haley 	zpool_rewind_policy_t policy;
3118fa94a07fSbrendan 	boolean_t avail_spare, l2cache;
311999653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3120468c413aSTim Haley 	nvlist_t *nvi = NULL;
31214b964adaSGeorge Wilson 	int error;
3122ea8dc4b6Seschrock 
3123ea8dc4b6Seschrock 	if (path)
3124ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3125ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3126e9dbad6fSeschrock 		    path);
3127ea8dc4b6Seschrock 	else
3128ea8dc4b6Seschrock 		(void) snprintf(msg, sizeof (msg),
3129ea8dc4b6Seschrock 		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3130ea8dc4b6Seschrock 		    zhp->zpool_name);
3131ea8dc4b6Seschrock 
3132ea8dc4b6Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
313399653d4eSeschrock 	if (path) {
3134fa94a07fSbrendan 		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3135ee0eb9f2SEric Schrock 		    &l2cache, NULL)) == 0)
313699653d4eSeschrock 			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3137ea8dc4b6Seschrock 
3138fa94a07fSbrendan 		/*
3139fa94a07fSbrendan 		 * Don't allow error clearing for hot spares.  Do allow
3140fa94a07fSbrendan 		 * error clearing for l2cache devices.
3141fa94a07fSbrendan 		 */
3142a43d325bSek 		if (avail_spare)
314399653d4eSeschrock 			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3144ea8dc4b6Seschrock 
314599653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
314699653d4eSeschrock 		    &zc.zc_guid) == 0);
3147fa9e4066Sahrens 	}
3148fa9e4066Sahrens 
3149468c413aSTim Haley 	zpool_get_rewind_policy(rewindnvl, &policy);
3150468c413aSTim Haley 	zc.zc_cookie = policy.zrp_request;
3151468c413aSTim Haley 
315257f304caSGeorge Wilson 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3153468c413aSTim Haley 		return (-1);
3154468c413aSTim Haley 
3155cb04b873SMark J Musante 	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3156468c413aSTim Haley 		return (-1);
3157468c413aSTim Haley 
31584b964adaSGeorge Wilson 	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
31594b964adaSGeorge Wilson 	    errno == ENOMEM) {
31604b964adaSGeorge Wilson 		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
31614b964adaSGeorge Wilson 			zcmd_free_nvlists(&zc);
31624b964adaSGeorge Wilson 			return (-1);
31634b964adaSGeorge Wilson 		}
31644b964adaSGeorge Wilson 	}
31654b964adaSGeorge Wilson 
31664b964adaSGeorge Wilson 	if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3167468c413aSTim Haley 	    errno != EPERM && errno != EACCES)) {
3168468c413aSTim Haley 		if (policy.zrp_request &
3169468c413aSTim Haley 		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3170468c413aSTim Haley 			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3171468c413aSTim Haley 			zpool_rewind_exclaim(hdl, zc.zc_name,
3172468c413aSTim Haley 			    ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3173468c413aSTim Haley 			    nvi);
3174468c413aSTim Haley 			nvlist_free(nvi);
3175468c413aSTim Haley 		}
3176468c413aSTim Haley 		zcmd_free_nvlists(&zc);
317799653d4eSeschrock 		return (0);
3178468c413aSTim Haley 	}
317999653d4eSeschrock 
3180468c413aSTim Haley 	zcmd_free_nvlists(&zc);
318199653d4eSeschrock 	return (zpool_standard_error(hdl, errno, msg));
3182fa9e4066Sahrens }
3183fa9e4066Sahrens 
31843d7072f8Seschrock /*
31853d7072f8Seschrock  * Similar to zpool_clear(), but takes a GUID (used by fmd).
31863d7072f8Seschrock  */
31873d7072f8Seschrock int
31883d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
31893d7072f8Seschrock {
31903d7072f8Seschrock 	zfs_cmd_t zc = { 0 };
31913d7072f8Seschrock 	char msg[1024];
31923d7072f8Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
31933d7072f8Seschrock 
31943d7072f8Seschrock 	(void) snprintf(msg, sizeof (msg),
31953d7072f8Seschrock 	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
31963d7072f8Seschrock 	    guid);
31973d7072f8Seschrock 
31983d7072f8Seschrock 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
31993d7072f8Seschrock 	zc.zc_guid = guid;
320014f8ce41SVictor Latushkin 	zc.zc_cookie = ZPOOL_NO_REWIND;
32013d7072f8Seschrock 
32023d7072f8Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
32033d7072f8Seschrock 		return (0);
32043d7072f8Seschrock 
32053d7072f8Seschrock 	return (zpool_standard_error(hdl, errno, msg));
32063d7072f8Seschrock }
32073d7072f8Seschrock 
3208e9103aaeSGarrett D'Amore /*
3209e9103aaeSGarrett D'Amore  * Change the GUID for a pool.
3210e9103aaeSGarrett D'Amore  */
3211e9103aaeSGarrett D'Amore int
3212e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3213e9103aaeSGarrett D'Amore {
3214e9103aaeSGarrett D'Amore 	char msg[1024];
3215e9103aaeSGarrett D'Amore 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3216e9103aaeSGarrett D'Amore 	zfs_cmd_t zc = { 0 };
3217e9103aaeSGarrett D'Amore 
3218e9103aaeSGarrett D'Amore 	(void) snprintf(msg, sizeof (msg),
3219e9103aaeSGarrett D'Amore 	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3220e9103aaeSGarrett D'Amore 
3221e9103aaeSGarrett D'Amore 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3222e9103aaeSGarrett D'Amore 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3223e9103aaeSGarrett D'Amore 		return (0);
3224e9103aaeSGarrett D'Amore 
3225e9103aaeSGarrett D'Amore 	return (zpool_standard_error(hdl, errno, msg));
3226e9103aaeSGarrett D'Amore }
3227e9103aaeSGarrett D'Amore 
32284263d13fSGeorge Wilson /*
32294263d13fSGeorge Wilson  * Reopen the pool.
32304263d13fSGeorge Wilson  */
32314263d13fSGeorge Wilson int
32324263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
32334263d13fSGeorge Wilson {
32344263d13fSGeorge Wilson 	zfs_cmd_t zc = { 0 };
32354263d13fSGeorge Wilson 	char msg[1024];
32364263d13fSGeorge Wilson 	libzfs_handle_t *hdl = zhp->zpool_hdl;
32374263d13fSGeorge Wilson 
32384263d13fSGeorge Wilson 	(void) snprintf(msg, sizeof (msg),
32394263d13fSGeorge Wilson 	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
32404263d13fSGeorge Wilson 	    zhp->zpool_name);
32414263d13fSGeorge Wilson 
32424263d13fSGeorge Wilson 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
32434263d13fSGeorge Wilson 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
32444263d13fSGeorge Wilson 		return (0);
32454263d13fSGeorge Wilson 	return (zpool_standard_error(hdl, errno, msg));
32464263d13fSGeorge Wilson }
32474263d13fSGeorge Wilson 
3248c67d9675Seschrock /*
3249c67d9675Seschrock  * Convert from a devid string to a path.
3250c67d9675Seschrock  */
3251c67d9675Seschrock static char *
3252c67d9675Seschrock devid_to_path(char *devid_str)
3253c67d9675Seschrock {
3254c67d9675Seschrock 	ddi_devid_t devid;
3255c67d9675Seschrock 	char *minor;
3256c67d9675Seschrock 	char *path;
3257c67d9675Seschrock 	devid_nmlist_t *list = NULL;
3258c67d9675Seschrock 	int ret;
3259c67d9675Seschrock 
3260c67d9675Seschrock 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3261c67d9675Seschrock 		return (NULL);
3262c67d9675Seschrock 
3263c67d9675Seschrock 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3264c67d9675Seschrock 
3265c67d9675Seschrock 	devid_str_free(minor);
3266c67d9675Seschrock 	devid_free(devid);
3267c67d9675Seschrock 
3268c67d9675Seschrock 	if (ret != 0)
3269c67d9675Seschrock 		return (NULL);
3270c67d9675Seschrock 
3271078266a5SMarcel Telka 	/*
3272078266a5SMarcel Telka 	 * In a case the strdup() fails, we will just return NULL below.
3273078266a5SMarcel Telka 	 */
3274078266a5SMarcel Telka 	path = strdup(list[0].devname);
327599653d4eSeschrock 
3276c67d9675Seschrock 	devid_free_nmlist(list);
3277c67d9675Seschrock 
3278c67d9675Seschrock 	return (path);
3279c67d9675Seschrock }
3280c67d9675Seschrock 
3281c67d9675Seschrock /*
3282c67d9675Seschrock  * Convert from a path to a devid string.
3283c67d9675Seschrock  */
3284c67d9675Seschrock static char *
3285c67d9675Seschrock path_to_devid(const char *path)
3286c67d9675Seschrock {
3287c67d9675Seschrock 	int fd;
3288c67d9675Seschrock 	ddi_devid_t devid;
3289c67d9675Seschrock 	char *minor, *ret;
3290c67d9675Seschrock 
3291c67d9675Seschrock 	if ((fd = open(path, O_RDONLY)) < 0)
3292c67d9675Seschrock 		return (NULL);
3293c67d9675Seschrock 
3294c67d9675Seschrock 	minor = NULL;
3295c67d9675Seschrock 	ret = NULL;
3296c67d9675Seschrock 	if (devid_get(fd, &devid) == 0) {
3297c67d9675Seschrock 		if (devid_get_minor_name(fd, &minor) == 0)
3298c67d9675Seschrock 			ret = devid_str_encode(devid, minor);
3299c67d9675Seschrock 		if (minor != NULL)
3300c67d9675Seschrock 			devid_str_free(minor);
3301c67d9675Seschrock 		devid_free(devid);
3302c67d9675Seschrock 	}
3303c67d9675Seschrock 	(void) close(fd);
3304c67d9675Seschrock 
3305c67d9675Seschrock 	return (ret);
3306c67d9675Seschrock }
3307c67d9675Seschrock 
3308c67d9675Seschrock /*
3309c67d9675Seschrock  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3310c67d9675Seschrock  * ignore any failure here, since a common case is for an unprivileged user to
3311c67d9675Seschrock  * type 'zpool status', and we'll display the correct information anyway.
3312c67d9675Seschrock  */
3313c67d9675Seschrock static void
3314c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3315c67d9675Seschrock {
3316c67d9675Seschrock 	zfs_cmd_t zc = { 0 };
3317c67d9675Seschrock 
3318c67d9675Seschrock 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3319e9dbad6fSeschrock 	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3320c67d9675Seschrock 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3321ea8dc4b6Seschrock 	    &zc.zc_guid) == 0);
3322c67d9675Seschrock 
332399653d4eSeschrock 	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3324c67d9675Seschrock }
3325c67d9675Seschrock 
3326c67d9675Seschrock /*
3327c67d9675Seschrock  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3328c67d9675Seschrock  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3329c67d9675Seschrock  * We also check if this is a whole disk, in which case we strip off the
3330c67d9675Seschrock  * trailing 's0' slice name.
3331c67d9675Seschrock  *
3332c67d9675Seschrock  * This routine is also responsible for identifying when disks have been
3333c67d9675Seschrock  * reconfigured in a new location.  The kernel will have opened the device by
3334c67d9675Seschrock  * devid, but the path will still refer to the old location.  To catch this, we
3335c67d9675Seschrock  * first do a path -> devid translation (which is fast for the common case).  If
3336c67d9675Seschrock  * the devid matches, we're done.  If not, we do a reverse devid -> path
3337c67d9675Seschrock  * translation and issue the appropriate ioctl() to update the path of the vdev.
3338c67d9675Seschrock  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3339c67d9675Seschrock  * of these checks.
3340c67d9675Seschrock  */
3341c67d9675Seschrock char *
334288ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
334388ecc943SGeorge Wilson     boolean_t verbose)
3344c67d9675Seschrock {
3345c67d9675Seschrock 	char *path, *devid;
3346ea8dc4b6Seschrock 	uint64_t value;
3347ea8dc4b6Seschrock 	char buf[64];
33483d7072f8Seschrock 	vdev_stat_t *vs;
33493d7072f8Seschrock 	uint_t vsc;
3350c67d9675Seschrock 
3351ea8dc4b6Seschrock 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3352ea8dc4b6Seschrock 	    &value) == 0) {
3353ea8dc4b6Seschrock 		verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3354ea8dc4b6Seschrock 		    &value) == 0);
33555ad82045Snd 		(void) snprintf(buf, sizeof (buf), "%llu",
33565ad82045Snd 		    (u_longlong_t)value);
3357ea8dc4b6Seschrock 		path = buf;
3358ea8dc4b6Seschrock 	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3359c67d9675Seschrock 
33603d7072f8Seschrock 		/*
33613d7072f8Seschrock 		 * If the device is dead (faulted, offline, etc) then don't
33623d7072f8Seschrock 		 * bother opening it.  Otherwise we may be forcing the user to
33633d7072f8Seschrock 		 * open a misbehaving device, which can have undesirable
33643d7072f8Seschrock 		 * effects.
33653d7072f8Seschrock 		 */
33663f9d6ad7SLin Ling 		if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
33673d7072f8Seschrock 		    (uint64_t **)&vs, &vsc) != 0 ||
33683d7072f8Seschrock 		    vs->vs_state >= VDEV_STATE_DEGRADED) &&
33693d7072f8Seschrock 		    zhp != NULL &&
3370c67d9675Seschrock 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3371c67d9675Seschrock 			/*
3372c67d9675Seschrock 			 * Determine if the current path is correct.
3373c67d9675Seschrock 			 */
3374c67d9675Seschrock 			char *newdevid = path_to_devid(path);
3375c67d9675Seschrock 
3376c67d9675Seschrock 			if (newdevid == NULL ||
3377c67d9675Seschrock 			    strcmp(devid, newdevid) != 0) {
3378c67d9675Seschrock 				char *newpath;
3379c67d9675Seschrock 
3380c67d9675Seschrock 				if ((newpath = devid_to_path(devid)) != NULL) {
3381c67d9675Seschrock 					/*
3382c67d9675Seschrock 					 * Update the path appropriately.
3383c67d9675Seschrock 					 */
3384c67d9675Seschrock 					set_path(zhp, nv, newpath);
338599653d4eSeschrock 					if (nvlist_add_string(nv,
338699653d4eSeschrock 					    ZPOOL_CONFIG_PATH, newpath) == 0)
338799653d4eSeschrock 						verify(nvlist_lookup_string(nv,
338899653d4eSeschrock 						    ZPOOL_CONFIG_PATH,
338999653d4eSeschrock 						    &path) == 0);
3390c67d9675Seschrock 					free(newpath);
3391c67d9675Seschrock 				}
3392c67d9675Seschrock 			}
3393c67d9675Seschrock 
339499653d4eSeschrock 			if (newdevid)
339599653d4eSeschrock 				devid_str_free(newdevid);
3396c67d9675Seschrock 		}
3397c67d9675Seschrock 
33986401734dSWill Andrews 		if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
33996401734dSWill Andrews 			path += strlen(ZFS_DISK_ROOTD);
3400c67d9675Seschrock 
3401c67d9675Seschrock 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3402ea8dc4b6Seschrock 		    &value) == 0 && value) {
34033fdda499SJohn Harres 			int pathlen = strlen(path);
340499653d4eSeschrock 			char *tmp = zfs_strdup(hdl, path);
34053fdda499SJohn Harres 
34063fdda499SJohn Harres 			/*
34073fdda499SJohn Harres 			 * If it starts with c#, and ends with "s0", chop
34083fdda499SJohn Harres 			 * the "s0" off, or if it ends with "s0/old", remove
34093fdda499SJohn Harres 			 * the "s0" from the middle.
34103fdda499SJohn Harres 			 */
34113fdda499SJohn Harres 			if (CTD_CHECK(tmp)) {
34123fdda499SJohn Harres 				if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
34133fdda499SJohn Harres 					tmp[pathlen - 2] = '\0';
34143fdda499SJohn Harres 				} else if (pathlen > 6 &&
34153fdda499SJohn Harres 				    strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
34163fdda499SJohn Harres 					(void) strcpy(&tmp[pathlen - 6],
34173fdda499SJohn Harres 					    "/old");
34183fdda499SJohn Harres 				}
34193fdda499SJohn Harres 			}
3420c67d9675Seschrock 			return (tmp);
3421c67d9675Seschrock 		}
3422c67d9675Seschrock 	} else {
3423c67d9675Seschrock 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
342499653d4eSeschrock 
342599653d4eSeschrock 		/*
342699653d4eSeschrock 		 * If it's a raidz device, we need to stick in the parity level.
342799653d4eSeschrock 		 */
342899653d4eSeschrock 		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
342999653d4eSeschrock 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
343099653d4eSeschrock 			    &value) == 0);
343199653d4eSeschrock 			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
34325ad82045Snd 			    (u_longlong_t)value);
343399653d4eSeschrock 			path = buf;
343499653d4eSeschrock 		}
343588ecc943SGeorge Wilson 
343688ecc943SGeorge Wilson 		/*
343788ecc943SGeorge Wilson 		 * We identify each top-level vdev by using a <type-id>
343888ecc943SGeorge Wilson 		 * naming convention.
343988ecc943SGeorge Wilson 		 */
344088ecc943SGeorge Wilson 		if (verbose) {
344188ecc943SGeorge Wilson 			uint64_t id;
344288ecc943SGeorge Wilson 
344388ecc943SGeorge Wilson 			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
344488ecc943SGeorge Wilson 			    &id) == 0);
344588ecc943SGeorge Wilson 			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
344688ecc943SGeorge Wilson 			    (u_longlong_t)id);
344788ecc943SGeorge Wilson 			path = buf;
344888ecc943SGeorge Wilson 		}
3449c67d9675Seschrock 	}
3450c67d9675Seschrock 
345199653d4eSeschrock 	return (zfs_strdup(hdl, path));
3452c67d9675Seschrock }
3453ea8dc4b6Seschrock 
3454ea8dc4b6Seschrock static int
3455a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b)
3456ea8dc4b6Seschrock {
34577802d7bfSMatthew Ahrens 	return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3458ea8dc4b6Seschrock }
3459ea8dc4b6Seschrock 
3460ea8dc4b6Seschrock /*
3461ea8dc4b6Seschrock  * Retrieve the persistent error log, uniquify the members, and return to the
3462ea8dc4b6Seschrock  * caller.
3463ea8dc4b6Seschrock  */
3464ea8dc4b6Seschrock int
346555434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3466ea8dc4b6Seschrock {
3467ea8dc4b6Seschrock 	zfs_cmd_t zc = { 0 };
3468ea8dc4b6Seschrock 	uint64_t count;
34697802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = NULL;
347055434c77Sek 	int i;
3471ea8dc4b6Seschrock 
3472ea8dc4b6Seschrock 	/*
3473ea8dc4b6Seschrock 	 * Retrieve the raw error list from the kernel.  If the number of errors
3474ea8dc4b6Seschrock 	 * has increased, allocate more space and continue until we get the
3475ea8dc4b6Seschrock 	 * entire list.
3476ea8dc4b6Seschrock 	 */
3477ea8dc4b6Seschrock 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3478ea8dc4b6Seschrock 	    &count) == 0);
347975519f38Sek 	if (count == 0)
348075519f38Sek 		return (0);
3481e9dbad6fSeschrock 	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
34827802d7bfSMatthew Ahrens 	    count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
348399653d4eSeschrock 		return (-1);
3484e9dbad6fSeschrock 	zc.zc_nvlist_dst_size = count;
3485ea8dc4b6Seschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3486ea8dc4b6Seschrock 	for (;;) {
348799653d4eSeschrock 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
348899653d4eSeschrock 		    &zc) != 0) {
3489e9dbad6fSeschrock 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
3490ea8dc4b6Seschrock 			if (errno == ENOMEM) {
34917802d7bfSMatthew Ahrens 				void *dst;
34927802d7bfSMatthew Ahrens 
3493bf561db0Svb 				count = zc.zc_nvlist_dst_size;
34947802d7bfSMatthew Ahrens 				dst = zfs_alloc(zhp->zpool_hdl, count *
34957802d7bfSMatthew Ahrens 				    sizeof (zbookmark_phys_t));
34967802d7bfSMatthew Ahrens 				if (dst == NULL)
349799653d4eSeschrock 					return (-1);
34987802d7bfSMatthew Ahrens 				zc.zc_nvlist_dst = (uintptr_t)dst;
3499ea8dc4b6Seschrock 			} else {
3500ea8dc4b6Seschrock 				return (-1);
3501ea8dc4b6Seschrock 			}
3502ea8dc4b6Seschrock 		} else {
3503ea8dc4b6Seschrock 			break;
3504ea8dc4b6Seschrock 		}
3505ea8dc4b6Seschrock 	}
3506ea8dc4b6Seschrock 
3507ea8dc4b6Seschrock 	/*
3508ea8dc4b6Seschrock 	 * Sort the resulting bookmarks.  This is a little confusing due to the
3509ea8dc4b6Seschrock 	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3510e9dbad6fSeschrock 	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3511ea8dc4b6Seschrock 	 * _not_ copied as part of the process.  So we point the start of our
3512ea8dc4b6Seschrock 	 * array appropriate and decrement the total number of elements.
3513ea8dc4b6Seschrock 	 */
35147802d7bfSMatthew Ahrens 	zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3515e9dbad6fSeschrock 	    zc.zc_nvlist_dst_size;
3516e9dbad6fSeschrock 	count -= zc.zc_nvlist_dst_size;
3517ea8dc4b6Seschrock 
3518a2cdcdd2SPaul Dagnelie 	qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3519ea8dc4b6Seschrock 
352055434c77Sek 	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3521ea8dc4b6Seschrock 
3522ea8dc4b6Seschrock 	/*
352355434c77Sek 	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3524ea8dc4b6Seschrock 	 */
3525ea8dc4b6Seschrock 	for (i = 0; i < count; i++) {
3526ea8dc4b6Seschrock 		nvlist_t *nv;
3527ea8dc4b6Seschrock 
3528c0a81264Sek 		/* ignoring zb_blkid and zb_level for now */
3529c0a81264Sek 		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3530c0a81264Sek 		    zb[i-1].zb_object == zb[i].zb_object)
3531ea8dc4b6Seschrock 			continue;
3532ea8dc4b6Seschrock 
353355434c77Sek 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
353455434c77Sek 			goto nomem;
353555434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
353655434c77Sek 		    zb[i].zb_objset) != 0) {
353755434c77Sek 			nvlist_free(nv);
353899653d4eSeschrock 			goto nomem;
3539ea8dc4b6Seschrock 		}
354055434c77Sek 		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
354155434c77Sek 		    zb[i].zb_object) != 0) {
354255434c77Sek 			nvlist_free(nv);
354355434c77Sek 			goto nomem;
354455434c77Sek 		}
354555434c77Sek 		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
354655434c77Sek 			nvlist_free(nv);
354755434c77Sek 			goto nomem;
354855434c77Sek 		}
354955434c77Sek 		nvlist_free(nv);
3550ea8dc4b6Seschrock 	}
3551ea8dc4b6Seschrock 
35523ccfa83cSahrens 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
3553ea8dc4b6Seschrock 	return (0);
355499653d4eSeschrock 
355599653d4eSeschrock nomem:
3556e9dbad6fSeschrock 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
355799653d4eSeschrock 	return (no_memory(zhp->zpool_hdl));
3558ea8dc4b6Seschrock }
3559eaca9bbdSeschrock 
3560eaca9bbdSeschrock /*
3561eaca9bbdSeschrock  * Upgrade a ZFS pool to the latest on-disk version.
3562eaca9bbdSeschrock  */
3563eaca9bbdSeschrock int
3564990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3565eaca9bbdSeschrock {
3566eaca9bbdSeschrock 	zfs_cmd_t zc = { 0 };
356799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
3568eaca9bbdSeschrock 
3569eaca9bbdSeschrock 	(void) strcpy(zc.zc_name, zhp->zpool_name);
3570990b4856Slling 	zc.zc_cookie = new_version;
3571990b4856Slling 
3572ecd6cf80Smarks 	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3573ece3d9b3Slling 		return (zpool_standard_error_fmt(hdl, errno,
357499653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
357599653d4eSeschrock 		    zhp->zpool_name));
3576eaca9bbdSeschrock 	return (0);
3577eaca9bbdSeschrock }
357806eeb2adSek 
357906eeb2adSek void
35804445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len)
358106eeb2adSek {
35824445fffbSMatthew Ahrens 	(void) strlcpy(string, basename(argv[0]), len);
35834445fffbSMatthew Ahrens 	for (int i = 1; i < argc; i++) {
35844445fffbSMatthew Ahrens 		(void) strlcat(string, " ", len);
35854445fffbSMatthew Ahrens 		(void) strlcat(string, argv[i], len);
35862a6b87f0Sek 	}
35872a6b87f0Sek }
35882a6b87f0Sek 
35892a6b87f0Sek int
35904445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message)
35912a6b87f0Sek {
35924445fffbSMatthew Ahrens 	zfs_cmd_t zc = { 0 };
35934445fffbSMatthew Ahrens 	nvlist_t *args;
35944445fffbSMatthew Ahrens 	int err;
35954445fffbSMatthew Ahrens 
35964445fffbSMatthew Ahrens 	args = fnvlist_alloc();
35974445fffbSMatthew Ahrens 	fnvlist_add_string(args, "message", message);
35984445fffbSMatthew Ahrens 	err = zcmd_write_src_nvlist(hdl, &zc, args);
35994445fffbSMatthew Ahrens 	if (err == 0)
36004445fffbSMatthew Ahrens 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
36014445fffbSMatthew Ahrens 	nvlist_free(args);
36024445fffbSMatthew Ahrens 	zcmd_free_nvlists(&zc);
36034445fffbSMatthew Ahrens 	return (err);
360406eeb2adSek }
360506eeb2adSek 
360606eeb2adSek /*
360706eeb2adSek  * Perform ioctl to get some command history of a pool.
360806eeb2adSek  *
360906eeb2adSek  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
361006eeb2adSek  * logical offset of the history buffer to start reading from.
361106eeb2adSek  *
361206eeb2adSek  * Upon return, 'off' is the next logical offset to read from and
361306eeb2adSek  * 'len' is the actual amount of bytes read into 'buf'.
361406eeb2adSek  */
361506eeb2adSek static int
361606eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
361706eeb2adSek {
361806eeb2adSek 	zfs_cmd_t zc = { 0 };
361906eeb2adSek 	libzfs_handle_t *hdl = zhp->zpool_hdl;
362006eeb2adSek 
362106eeb2adSek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
362206eeb2adSek 
362306eeb2adSek 	zc.zc_history = (uint64_t)(uintptr_t)buf;
362406eeb2adSek 	zc.zc_history_len = *len;
362506eeb2adSek 	zc.zc_history_offset = *off;
362606eeb2adSek 
362706eeb2adSek 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
362806eeb2adSek 		switch (errno) {
362906eeb2adSek 		case EPERM:
3630ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_PERM,
3631ece3d9b3Slling 			    dgettext(TEXT_DOMAIN,
363206eeb2adSek 			    "cannot show history for pool '%s'"),
363306eeb2adSek 			    zhp->zpool_name));
363406eeb2adSek 		case ENOENT:
3635ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
363606eeb2adSek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
363706eeb2adSek 			    "'%s'"), zhp->zpool_name));
3638d7306b64Sek 		case ENOTSUP:
3639d7306b64Sek 			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3640d7306b64Sek 			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
3641d7306b64Sek 			    "'%s', pool must be upgraded"), zhp->zpool_name));
364206eeb2adSek 		default:
3643ece3d9b3Slling 			return (zpool_standard_error_fmt(hdl, errno,
364406eeb2adSek 			    dgettext(TEXT_DOMAIN,
364506eeb2adSek 			    "cannot get history for '%s'"), zhp->zpool_name));
364606eeb2adSek 		}
364706eeb2adSek 	}
364806eeb2adSek 
364906eeb2adSek 	*len = zc.zc_history_len;
365006eeb2adSek 	*off = zc.zc_history_offset;
365106eeb2adSek 
365206eeb2adSek 	return (0);
365306eeb2adSek }
365406eeb2adSek 
365506eeb2adSek /*
365606eeb2adSek  * Process the buffer of nvlists, unpacking and storing each nvlist record
365706eeb2adSek  * into 'records'.  'leftover' is set to the number of bytes that weren't
365806eeb2adSek  * processed as there wasn't a complete record.
365906eeb2adSek  */
36608f18d1faSGeorge Wilson int
366106eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
366206eeb2adSek     nvlist_t ***records, uint_t *numrecords)
366306eeb2adSek {
366406eeb2adSek 	uint64_t reclen;
366506eeb2adSek 	nvlist_t *nv;
366606eeb2adSek 	int i;
366706eeb2adSek 
366806eeb2adSek 	while (bytes_read > sizeof (reclen)) {
366906eeb2adSek 
367006eeb2adSek 		/* get length of packed record (stored as little endian) */
367106eeb2adSek 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
367206eeb2adSek 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
367306eeb2adSek 
367406eeb2adSek 		if (bytes_read < sizeof (reclen) + reclen)
367506eeb2adSek 			break;
367606eeb2adSek 
367706eeb2adSek 		/* unpack record */
367806eeb2adSek 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
367906eeb2adSek 			return (ENOMEM);
368006eeb2adSek 		bytes_read -= sizeof (reclen) + reclen;
368106eeb2adSek 		buf += sizeof (reclen) + reclen;
368206eeb2adSek 
368306eeb2adSek 		/* add record to nvlist array */
368406eeb2adSek 		(*numrecords)++;
368506eeb2adSek 		if (ISP2(*numrecords + 1)) {
368606eeb2adSek 			*records = realloc(*records,
368706eeb2adSek 			    *numrecords * 2 * sizeof (nvlist_t *));
368806eeb2adSek 		}
368906eeb2adSek 		(*records)[*numrecords - 1] = nv;
369006eeb2adSek 	}
369106eeb2adSek 
369206eeb2adSek 	*leftover = bytes_read;
369306eeb2adSek 	return (0);
369406eeb2adSek }
369506eeb2adSek 
369606eeb2adSek /*
369706eeb2adSek  * Retrieve the command history of a pool.
369806eeb2adSek  */
369906eeb2adSek int
370006eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
370106eeb2adSek {
37023339867aSMatthew Ahrens 	char *buf;
37033339867aSMatthew Ahrens 	int buflen = 128 * 1024;
370406eeb2adSek 	uint64_t off = 0;
370506eeb2adSek 	nvlist_t **records = NULL;
370606eeb2adSek 	uint_t numrecords = 0;
370706eeb2adSek 	int err, i;
370806eeb2adSek 
37093339867aSMatthew Ahrens 	buf = malloc(buflen);
37103339867aSMatthew Ahrens 	if (buf == NULL)
37113339867aSMatthew Ahrens 		return (ENOMEM);
371206eeb2adSek 	do {
37133339867aSMatthew Ahrens 		uint64_t bytes_read = buflen;
371406eeb2adSek 		uint64_t leftover;
371506eeb2adSek 
371606eeb2adSek 		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
371706eeb2adSek 			break;
371806eeb2adSek 
371906eeb2adSek 		/* if nothing else was read in, we're at EOF, just return */
372006eeb2adSek 		if (!bytes_read)
372106eeb2adSek 			break;
372206eeb2adSek 
372306eeb2adSek 		if ((err = zpool_history_unpack(buf, bytes_read,
372406eeb2adSek 		    &leftover, &records, &numrecords)) != 0)
372506eeb2adSek 			break;
372606eeb2adSek 		off -= leftover;
37273339867aSMatthew Ahrens 		if (leftover == bytes_read) {
37283339867aSMatthew Ahrens 			/*
37293339867aSMatthew Ahrens 			 * no progress made, because buffer is not big enough
37303339867aSMatthew Ahrens 			 * to hold this record; resize and retry.
37313339867aSMatthew Ahrens 			 */
37323339867aSMatthew Ahrens 			buflen *= 2;
37333339867aSMatthew Ahrens 			free(buf);
37343339867aSMatthew Ahrens 			buf = malloc(buflen);
37353339867aSMatthew Ahrens 			if (buf == NULL)
37363339867aSMatthew Ahrens 				return (ENOMEM);
37373339867aSMatthew Ahrens 		}
373806eeb2adSek 
373906eeb2adSek 		/* CONSTCOND */
374006eeb2adSek 	} while (1);
374106eeb2adSek 
37423339867aSMatthew Ahrens 	free(buf);
37433339867aSMatthew Ahrens 
374406eeb2adSek 	if (!err) {
374506eeb2adSek 		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
374606eeb2adSek 		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
374706eeb2adSek 		    records, numrecords) == 0);
374806eeb2adSek 	}
374906eeb2adSek 	for (i = 0; i < numrecords; i++)
375006eeb2adSek 		nvlist_free(records[i]);
375106eeb2adSek 	free(records);
375206eeb2adSek 
375306eeb2adSek 	return (err);
375406eeb2adSek }
375555434c77Sek 
375655434c77Sek void
375755434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
375855434c77Sek     char *pathname, size_t len)
375955434c77Sek {
376055434c77Sek 	zfs_cmd_t zc = { 0 };
376155434c77Sek 	boolean_t mounted = B_FALSE;
376255434c77Sek 	char *mntpnt = NULL;
3763*9adfa60dSMatthew Ahrens 	char dsname[ZFS_MAX_DATASET_NAME_LEN];
376455434c77Sek 
376555434c77Sek 	if (dsobj == 0) {
376655434c77Sek 		/* special case for the MOS */
376755434c77Sek 		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
376855434c77Sek 		return;
376955434c77Sek 	}
377055434c77Sek 
377155434c77Sek 	/* get the dataset's name */
377255434c77Sek 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
377355434c77Sek 	zc.zc_obj = dsobj;
377455434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd,
377555434c77Sek 	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
377655434c77Sek 		/* just write out a path of two object numbers */
377755434c77Sek 		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
377855434c77Sek 		    dsobj, obj);
377955434c77Sek 		return;
378055434c77Sek 	}
378155434c77Sek 	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
378255434c77Sek 
378355434c77Sek 	/* find out if the dataset is mounted */
378455434c77Sek 	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
378555434c77Sek 
378655434c77Sek 	/* get the corrupted object's path */
378755434c77Sek 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
378855434c77Sek 	zc.zc_obj = obj;
378955434c77Sek 	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
379055434c77Sek 	    &zc) == 0) {
379155434c77Sek 		if (mounted) {
379255434c77Sek 			(void) snprintf(pathname, len, "%s%s", mntpnt,
379355434c77Sek 			    zc.zc_value);
379455434c77Sek 		} else {
379555434c77Sek 			(void) snprintf(pathname, len, "%s:%s",
379655434c77Sek 			    dsname, zc.zc_value);
379755434c77Sek 		}
379855434c77Sek 	} else {
379955434c77Sek 		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
380055434c77Sek 	}
380155434c77Sek 	free(mntpnt);
380255434c77Sek }
3803b1b8ab34Slling 
380415e6edf1Sgw /*
380515e6edf1Sgw  * Read the EFI label from the config, if a label does not exist then
380615e6edf1Sgw  * pass back the error to the caller. If the caller has passed a non-NULL
380715e6edf1Sgw  * diskaddr argument then we set it to the starting address of the EFI
380815e6edf1Sgw  * partition.
380915e6edf1Sgw  */
381015e6edf1Sgw static int
381115e6edf1Sgw read_efi_label(nvlist_t *config, diskaddr_t *sb)
381215e6edf1Sgw {
381315e6edf1Sgw 	char *path;
381415e6edf1Sgw 	int fd;
381515e6edf1Sgw 	char diskname[MAXPATHLEN];
381615e6edf1Sgw 	int err = -1;
381715e6edf1Sgw 
381815e6edf1Sgw 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
381915e6edf1Sgw 		return (err);
382015e6edf1Sgw 
38216401734dSWill Andrews 	(void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
382215e6edf1Sgw 	    strrchr(path, '/'));
382315e6edf1Sgw 	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
382415e6edf1Sgw 		struct dk_gpt *vtoc;
382515e6edf1Sgw 
382615e6edf1Sgw 		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
382715e6edf1Sgw 			if (sb != NULL)
382815e6edf1Sgw 				*sb = vtoc->efi_parts[0].p_start;
382915e6edf1Sgw 			efi_free(vtoc);
383015e6edf1Sgw 		}
383115e6edf1Sgw 		(void) close(fd);
383215e6edf1Sgw 	}
383315e6edf1Sgw 	return (err);
383415e6edf1Sgw }
383515e6edf1Sgw 
38368488aeb5Staylor /*
38378488aeb5Staylor  * determine where a partition starts on a disk in the current
38388488aeb5Staylor  * configuration
38398488aeb5Staylor  */
38408488aeb5Staylor static diskaddr_t
38418488aeb5Staylor find_start_block(nvlist_t *config)
38428488aeb5Staylor {
38438488aeb5Staylor 	nvlist_t **child;
38448488aeb5Staylor 	uint_t c, children;
38458488aeb5Staylor 	diskaddr_t sb = MAXOFFSET_T;
38468488aeb5Staylor 	uint64_t wholedisk;
38478488aeb5Staylor 
38488488aeb5Staylor 	if (nvlist_lookup_nvlist_array(config,
38498488aeb5Staylor 	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
38508488aeb5Staylor 		if (nvlist_lookup_uint64(config,
38518488aeb5Staylor 		    ZPOOL_CONFIG_WHOLE_DISK,
38528488aeb5Staylor 		    &wholedisk) != 0 || !wholedisk) {
38538488aeb5Staylor 			return (MAXOFFSET_T);
38548488aeb5Staylor 		}
385515e6edf1Sgw 		if (read_efi_label(config, &sb) < 0)
385615e6edf1Sgw 			sb = MAXOFFSET_T;
38578488aeb5Staylor 		return (sb);
38588488aeb5Staylor 	}
38598488aeb5Staylor 
38608488aeb5Staylor 	for (c = 0; c < children; c++) {
38618488aeb5Staylor 		sb = find_start_block(child[c]);
38628488aeb5Staylor 		if (sb != MAXOFFSET_T) {
38638488aeb5Staylor 			return (sb);
38648488aeb5Staylor 		}
38658488aeb5Staylor 	}
38668488aeb5Staylor 	return (MAXOFFSET_T);
38678488aeb5Staylor }
38688488aeb5Staylor 
38698488aeb5Staylor /*
38708488aeb5Staylor  * Label an individual disk.  The name provided is the short name,
38718488aeb5Staylor  * stripped of any leading /dev path.
38728488aeb5Staylor  */
38738488aeb5Staylor int
38746401734dSWill Andrews zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
38758488aeb5Staylor {
38768488aeb5Staylor 	char path[MAXPATHLEN];
38778488aeb5Staylor 	struct dk_gpt *vtoc;
38788488aeb5Staylor 	int fd;
38798488aeb5Staylor 	size_t resv = EFI_MIN_RESV_SIZE;
38808488aeb5Staylor 	uint64_t slice_size;
38818488aeb5Staylor 	diskaddr_t start_block;
38828488aeb5Staylor 	char errbuf[1024];
38838488aeb5Staylor 
3884c6ef114fSmmusante 	/* prepare an error message just in case */
3885c6ef114fSmmusante 	(void) snprintf(errbuf, sizeof (errbuf),
3886c6ef114fSmmusante 	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3887c6ef114fSmmusante 
38888488aeb5Staylor 	if (zhp) {
38898488aeb5Staylor 		nvlist_t *nvroot;
38908488aeb5Staylor 
38918488aeb5Staylor 		verify(nvlist_lookup_nvlist(zhp->zpool_config,
38928488aeb5Staylor 		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
38938488aeb5Staylor 
38948488aeb5Staylor 		if (zhp->zpool_start_block == 0)
38958488aeb5Staylor 			start_block = find_start_block(nvroot);
38968488aeb5Staylor 		else
38978488aeb5Staylor 			start_block = zhp->zpool_start_block;
38988488aeb5Staylor 		zhp->zpool_start_block = start_block;
38998488aeb5Staylor 	} else {
39008488aeb5Staylor 		/* new pool */
39018488aeb5Staylor 		start_block = NEW_START_BLOCK;
39028488aeb5Staylor 	}
39038488aeb5Staylor 
39046401734dSWill Andrews 	(void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
39058488aeb5Staylor 	    BACKUP_SLICE);
39068488aeb5Staylor 
39078488aeb5Staylor 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
39088488aeb5Staylor 		/*
39098488aeb5Staylor 		 * This shouldn't happen.  We've long since verified that this
39108488aeb5Staylor 		 * is a valid device.
39118488aeb5Staylor 		 */
3912c6ef114fSmmusante 		zfs_error_aux(hdl,
3913c6ef114fSmmusante 		    dgettext(TEXT_DOMAIN, "unable to open device"));
39148488aeb5Staylor 		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
39158488aeb5Staylor 	}
39168488aeb5Staylor 
39178488aeb5Staylor 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
39188488aeb5Staylor 		/*
39198488aeb5Staylor 		 * The only way this can fail is if we run out of memory, or we
39208488aeb5Staylor 		 * were unable to read the disk's capacity
39218488aeb5Staylor 		 */
39228488aeb5Staylor 		if (errno == ENOMEM)
39238488aeb5Staylor 			(void) no_memory(hdl);
39248488aeb5Staylor 
39258488aeb5Staylor 		(void) close(fd);
3926c6ef114fSmmusante 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3927c6ef114fSmmusante 		    "unable to read disk capacity"), name);
39288488aeb5Staylor 
39298488aeb5Staylor 		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
39308488aeb5Staylor 	}
39318488aeb5Staylor 
39328488aeb5Staylor 	slice_size = vtoc->efi_last_u_lba + 1;
39338488aeb5Staylor 	slice_size -= EFI_MIN_RESV_SIZE;
39348488aeb5Staylor 	if (start_block == MAXOFFSET_T)
39358488aeb5Staylor 		start_block = NEW_START_BLOCK;
39368488aeb5Staylor 	slice_size -= start_block;
39378488aeb5Staylor 
39388488aeb5Staylor 	vtoc->efi_parts[0].p_start = start_block;
39398488aeb5Staylor 	vtoc->efi_parts[0].p_size = slice_size;
39408488aeb5Staylor 
39418488aeb5Staylor 	/*
39428488aeb5Staylor 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
39438488aeb5Staylor 	 * disposable by some EFI utilities (since EFI doesn't have a backup
39448488aeb5Staylor 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
39458488aeb5Staylor 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
39468488aeb5Staylor 	 * etc. were all pretty specific.  V_USR is as close to reality as we
39478488aeb5Staylor 	 * can get, in the absence of V_OTHER.
39488488aeb5Staylor 	 */
39498488aeb5Staylor 	vtoc->efi_parts[0].p_tag = V_USR;
39508488aeb5Staylor 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
39518488aeb5Staylor 
39528488aeb5Staylor 	vtoc->efi_parts[8].p_start = slice_size + start_block;
39538488aeb5Staylor 	vtoc->efi_parts[8].p_size = resv;
39548488aeb5Staylor 	vtoc->efi_parts[8].p_tag = V_RESERVED;
39558488aeb5Staylor 
39568488aeb5Staylor 	if (efi_write(fd, vtoc) != 0) {
39578488aeb5Staylor 		/*
39588488aeb5Staylor 		 * Some block drivers (like pcata) may not support EFI
39598488aeb5Staylor 		 * GPT labels.  Print out a helpful error message dir-
39608488aeb5Staylor 		 * ecting the user to manually label the disk and give
39618488aeb5Staylor 		 * a specific slice.
39628488aeb5Staylor 		 */
39638488aeb5Staylor 		(void) close(fd);
39648488aeb5Staylor 		efi_free(vtoc);
39658488aeb5Staylor 
39668488aeb5Staylor 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3967c6ef114fSmmusante 		    "try using fdisk(1M) and then provide a specific slice"));
39688488aeb5Staylor 		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
39698488aeb5Staylor 	}
39708488aeb5Staylor 
39718488aeb5Staylor 	(void) close(fd);
39728488aeb5Staylor 	efi_free(vtoc);
39738488aeb5Staylor 	return (0);
39748488aeb5Staylor }
3975e7cbe64fSgw 
3976e7cbe64fSgw static boolean_t
3977e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3978e7cbe64fSgw {
3979e7cbe64fSgw 	char *type;
3980e7cbe64fSgw 	nvlist_t **child;
3981e7cbe64fSgw 	uint_t children, c;
3982e7cbe64fSgw 
3983e7cbe64fSgw 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3984810e43b2SBill Pijewski 	if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
398588ecc943SGeorge Wilson 	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3986e7cbe64fSgw 	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
3987e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3988e7cbe64fSgw 		    "vdev type '%s' is not supported"), type);
3989e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3990e7cbe64fSgw 		return (B_FALSE);
3991e7cbe64fSgw 	}
3992e7cbe64fSgw 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3993e7cbe64fSgw 	    &child, &children) == 0) {
3994e7cbe64fSgw 		for (c = 0; c < children; c++) {
3995e7cbe64fSgw 			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3996e7cbe64fSgw 				return (B_FALSE);
3997e7cbe64fSgw 		}
3998e7cbe64fSgw 	}
3999e7cbe64fSgw 	return (B_TRUE);
4000e7cbe64fSgw }
4001e7cbe64fSgw 
4002e7cbe64fSgw /*
4003810e43b2SBill Pijewski  * Check if this zvol is allowable for use as a dump device; zero if
4004810e43b2SBill Pijewski  * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4005810e43b2SBill Pijewski  *
4006810e43b2SBill Pijewski  * Allowable storage configurations include mirrors, all raidz variants, and
4007810e43b2SBill Pijewski  * pools with log, cache, and spare devices.  Pools which are backed by files or
4008810e43b2SBill Pijewski  * have missing/hole vdevs are not suitable.
4009e7cbe64fSgw  */
4010e7cbe64fSgw int
4011e7cbe64fSgw zvol_check_dump_config(char *arg)
4012e7cbe64fSgw {
4013e7cbe64fSgw 	zpool_handle_t *zhp = NULL;
4014e7cbe64fSgw 	nvlist_t *config, *nvroot;
4015e7cbe64fSgw 	char *p, *volname;
4016e7cbe64fSgw 	nvlist_t **top;
4017e7cbe64fSgw 	uint_t toplevels;
4018e7cbe64fSgw 	libzfs_handle_t *hdl;
4019e7cbe64fSgw 	char errbuf[1024];
4020*9adfa60dSMatthew Ahrens 	char poolname[ZFS_MAX_DATASET_NAME_LEN];
4021e7cbe64fSgw 	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4022e7cbe64fSgw 	int ret = 1;
4023e7cbe64fSgw 
4024e7cbe64fSgw 	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4025e7cbe64fSgw 		return (-1);
4026e7cbe64fSgw 	}
4027e7cbe64fSgw 
4028e7cbe64fSgw 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4029e7cbe64fSgw 	    "dump is not supported on device '%s'"), arg);
4030e7cbe64fSgw 
4031e7cbe64fSgw 	if ((hdl = libzfs_init()) == NULL)
4032e7cbe64fSgw 		return (1);
4033e7cbe64fSgw 	libzfs_print_on_error(hdl, B_TRUE);
4034e7cbe64fSgw 
4035e7cbe64fSgw 	volname = arg + pathlen;
4036e7cbe64fSgw 
4037e7cbe64fSgw 	/* check the configuration of the pool */
4038e7cbe64fSgw 	if ((p = strchr(volname, '/')) == NULL) {
4039e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4040e7cbe64fSgw 		    "malformed dataset name"));
4041e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4042e7cbe64fSgw 		return (1);
4043*9adfa60dSMatthew Ahrens 	} else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4044e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4045e7cbe64fSgw 		    "dataset name is too long"));
4046e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4047e7cbe64fSgw 		return (1);
4048e7cbe64fSgw 	} else {
4049e7cbe64fSgw 		(void) strncpy(poolname, volname, p - volname);
4050e7cbe64fSgw 		poolname[p - volname] = '\0';
4051e7cbe64fSgw 	}
4052e7cbe64fSgw 
4053e7cbe64fSgw 	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4054e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4055e7cbe64fSgw 		    "could not open pool '%s'"), poolname);
4056e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4057e7cbe64fSgw 		goto out;
4058e7cbe64fSgw 	}
4059e7cbe64fSgw 	config = zpool_get_config(zhp, NULL);
4060e7cbe64fSgw 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4061e7cbe64fSgw 	    &nvroot) != 0) {
4062e7cbe64fSgw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4063e7cbe64fSgw 		    "could not obtain vdev configuration for  '%s'"), poolname);
4064e7cbe64fSgw 		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4065e7cbe64fSgw 		goto out;
4066e7cbe64fSgw 	}
4067e7cbe64fSgw 
4068e7cbe64fSgw 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4069e7cbe64fSgw 	    &top, &toplevels) == 0);
4070e7cbe64fSgw 
4071e7cbe64fSgw 	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4072e7cbe64fSgw 		goto out;
4073e7cbe64fSgw 	}
4074e7cbe64fSgw 	ret = 0;
4075e7cbe64fSgw 
4076e7cbe64fSgw out:
4077e7cbe64fSgw 	if (zhp)
4078e7cbe64fSgw 		zpool_close(zhp);
4079e7cbe64fSgw 	libzfs_fini(hdl);
4080e7cbe64fSgw 	return (ret);
4081e7cbe64fSgw }
4082