1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright 2019 Joyent, Inc.
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28 * Copyright (c) 2017 Datto Inc.
29 * Copyright (c) 2017, Intel Corporation.
30 */
31
32#include <ctype.h>
33#include <errno.h>
34#include <devid.h>
35#include <fcntl.h>
36#include <libintl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <strings.h>
40#include <unistd.h>
41#include <libgen.h>
42#include <sys/efi_partition.h>
43#include <sys/vtoc.h>
44#include <sys/zfs_ioctl.h>
45#include <dlfcn.h>
46
47#include "zfs_namecheck.h"
48#include "zfs_prop.h"
49#include "libzfs_impl.h"
50#include "zfs_comutil.h"
51#include "zfeature_common.h"
52
53static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
54static boolean_t zpool_vdev_is_interior(const char *name);
55
56#define	BACKUP_SLICE	"s2"
57
58typedef struct prop_flags {
59	int create:1;	/* Validate property on creation */
60	int import:1;	/* Validate property on import */
61} prop_flags_t;
62
63/*
64 * ====================================================================
65 *   zpool property functions
66 * ====================================================================
67 */
68
69static int
70zpool_get_all_props(zpool_handle_t *zhp)
71{
72	zfs_cmd_t zc = { 0 };
73	libzfs_handle_t *hdl = zhp->zpool_hdl;
74
75	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
76
77	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
78		return (-1);
79
80	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
81		if (errno == ENOMEM) {
82			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
83				zcmd_free_nvlists(&zc);
84				return (-1);
85			}
86		} else {
87			zcmd_free_nvlists(&zc);
88			return (-1);
89		}
90	}
91
92	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
93		zcmd_free_nvlists(&zc);
94		return (-1);
95	}
96
97	zcmd_free_nvlists(&zc);
98
99	return (0);
100}
101
102static int
103zpool_props_refresh(zpool_handle_t *zhp)
104{
105	nvlist_t *old_props;
106
107	old_props = zhp->zpool_props;
108
109	if (zpool_get_all_props(zhp) != 0)
110		return (-1);
111
112	nvlist_free(old_props);
113	return (0);
114}
115
116static char *
117zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
118    zprop_source_t *src)
119{
120	nvlist_t *nv, *nvl;
121	uint64_t ival;
122	char *value;
123	zprop_source_t source;
124
125	nvl = zhp->zpool_props;
126	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
127		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
128		source = ival;
129		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
130	} else {
131		source = ZPROP_SRC_DEFAULT;
132		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
133			value = "-";
134	}
135
136	if (src)
137		*src = source;
138
139	return (value);
140}
141
142uint64_t
143zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
144{
145	nvlist_t *nv, *nvl;
146	uint64_t value;
147	zprop_source_t source;
148
149	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
150		/*
151		 * zpool_get_all_props() has most likely failed because
152		 * the pool is faulted, but if all we need is the top level
153		 * vdev's guid then get it from the zhp config nvlist.
154		 */
155		if ((prop == ZPOOL_PROP_GUID) &&
156		    (nvlist_lookup_nvlist(zhp->zpool_config,
157		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
158		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
159		    == 0)) {
160			return (value);
161		}
162		return (zpool_prop_default_numeric(prop));
163	}
164
165	nvl = zhp->zpool_props;
166	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
167		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
168		source = value;
169		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
170	} else {
171		source = ZPROP_SRC_DEFAULT;
172		value = zpool_prop_default_numeric(prop);
173	}
174
175	if (src)
176		*src = source;
177
178	return (value);
179}
180
181/*
182 * Map VDEV STATE to printed strings.
183 */
184const char *
185zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
186{
187	switch (state) {
188	case VDEV_STATE_CLOSED:
189	case VDEV_STATE_OFFLINE:
190		return (gettext("OFFLINE"));
191	case VDEV_STATE_REMOVED:
192		return (gettext("REMOVED"));
193	case VDEV_STATE_CANT_OPEN:
194		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
195			return (gettext("FAULTED"));
196		else if (aux == VDEV_AUX_SPLIT_POOL)
197			return (gettext("SPLIT"));
198		else
199			return (gettext("UNAVAIL"));
200	case VDEV_STATE_FAULTED:
201		return (gettext("FAULTED"));
202	case VDEV_STATE_DEGRADED:
203		return (gettext("DEGRADED"));
204	case VDEV_STATE_HEALTHY:
205		return (gettext("ONLINE"));
206
207	default:
208		break;
209	}
210
211	return (gettext("UNKNOWN"));
212}
213
214/*
215 * Map POOL STATE to printed strings.
216 */
217const char *
218zpool_pool_state_to_name(pool_state_t state)
219{
220	switch (state) {
221	case POOL_STATE_ACTIVE:
222		return (gettext("ACTIVE"));
223	case POOL_STATE_EXPORTED:
224		return (gettext("EXPORTED"));
225	case POOL_STATE_DESTROYED:
226		return (gettext("DESTROYED"));
227	case POOL_STATE_SPARE:
228		return (gettext("SPARE"));
229	case POOL_STATE_L2CACHE:
230		return (gettext("L2CACHE"));
231	case POOL_STATE_UNINITIALIZED:
232		return (gettext("UNINITIALIZED"));
233	case POOL_STATE_UNAVAIL:
234		return (gettext("UNAVAIL"));
235	case POOL_STATE_POTENTIALLY_ACTIVE:
236		return (gettext("POTENTIALLY_ACTIVE"));
237	}
238
239	return (gettext("UNKNOWN"));
240}
241
242/*
243 * Get a zpool property value for 'prop' and return the value in
244 * a pre-allocated buffer.
245 */
246int
247zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
248    zprop_source_t *srctype, boolean_t literal)
249{
250	uint64_t intval;
251	const char *strval;
252	zprop_source_t src = ZPROP_SRC_NONE;
253	nvlist_t *nvroot;
254	vdev_stat_t *vs;
255	uint_t vsc;
256
257	if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
258		switch (prop) {
259		case ZPOOL_PROP_NAME:
260			(void) strlcpy(buf, zpool_get_name(zhp), len);
261			break;
262
263		case ZPOOL_PROP_HEALTH:
264			(void) strlcpy(buf, "FAULTED", len);
265			break;
266
267		case ZPOOL_PROP_GUID:
268			intval = zpool_get_prop_int(zhp, prop, &src);
269			(void) snprintf(buf, len, "%llu", intval);
270			break;
271
272		case ZPOOL_PROP_ALTROOT:
273		case ZPOOL_PROP_CACHEFILE:
274		case ZPOOL_PROP_COMMENT:
275			if (zhp->zpool_props != NULL ||
276			    zpool_get_all_props(zhp) == 0) {
277				(void) strlcpy(buf,
278				    zpool_get_prop_string(zhp, prop, &src),
279				    len);
280				break;
281			}
282			/* FALLTHROUGH */
283		default:
284			(void) strlcpy(buf, "-", len);
285			break;
286		}
287
288		if (srctype != NULL)
289			*srctype = src;
290		return (0);
291	}
292
293	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
294	    prop != ZPOOL_PROP_NAME)
295		return (-1);
296
297	switch (zpool_prop_get_type(prop)) {
298	case PROP_TYPE_STRING:
299		(void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
300		    len);
301		break;
302
303	case PROP_TYPE_NUMBER:
304		intval = zpool_get_prop_int(zhp, prop, &src);
305
306		switch (prop) {
307		case ZPOOL_PROP_SIZE:
308		case ZPOOL_PROP_ALLOCATED:
309		case ZPOOL_PROP_FREE:
310		case ZPOOL_PROP_FREEING:
311		case ZPOOL_PROP_LEAKED:
312		case ZPOOL_PROP_ASHIFT:
313			if (literal) {
314				(void) snprintf(buf, len, "%llu",
315				    (u_longlong_t)intval);
316			} else {
317				(void) zfs_nicenum(intval, buf, len);
318			}
319			break;
320		case ZPOOL_PROP_BOOTSIZE:
321		case ZPOOL_PROP_EXPANDSZ:
322		case ZPOOL_PROP_CHECKPOINT:
323			if (intval == 0) {
324				(void) strlcpy(buf, "-", len);
325			} else if (literal) {
326				(void) snprintf(buf, len, "%llu",
327				    (u_longlong_t)intval);
328			} else {
329				(void) zfs_nicenum(intval, buf, len);
330			}
331			break;
332		case ZPOOL_PROP_CAPACITY:
333			if (literal) {
334				(void) snprintf(buf, len, "%llu",
335				    (u_longlong_t)intval);
336			} else {
337				(void) snprintf(buf, len, "%llu%%",
338				    (u_longlong_t)intval);
339			}
340			break;
341		case ZPOOL_PROP_FRAGMENTATION:
342			if (intval == UINT64_MAX) {
343				(void) strlcpy(buf, "-", len);
344			} else {
345				(void) snprintf(buf, len, "%llu%%",
346				    (u_longlong_t)intval);
347			}
348			break;
349		case ZPOOL_PROP_DEDUPRATIO:
350			(void) snprintf(buf, len, "%llu.%02llux",
351			    (u_longlong_t)(intval / 100),
352			    (u_longlong_t)(intval % 100));
353			break;
354		case ZPOOL_PROP_HEALTH:
355			verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
356			    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
357			verify(nvlist_lookup_uint64_array(nvroot,
358			    ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
359			    == 0);
360
361			(void) strlcpy(buf, zpool_state_to_name(intval,
362			    vs->vs_aux), len);
363			break;
364		case ZPOOL_PROP_VERSION:
365			if (intval >= SPA_VERSION_FEATURES) {
366				(void) snprintf(buf, len, "-");
367				break;
368			}
369			/* FALLTHROUGH */
370		default:
371			(void) snprintf(buf, len, "%llu", intval);
372		}
373		break;
374
375	case PROP_TYPE_INDEX:
376		intval = zpool_get_prop_int(zhp, prop, &src);
377		if (zpool_prop_index_to_string(prop, intval, &strval)
378		    != 0)
379			return (-1);
380		(void) strlcpy(buf, strval, len);
381		break;
382
383	default:
384		abort();
385	}
386
387	if (srctype)
388		*srctype = src;
389
390	return (0);
391}
392
393/*
394 * Check if the bootfs name has the same pool name as it is set to.
395 * Assuming bootfs is a valid dataset name.
396 */
397static boolean_t
398bootfs_name_valid(const char *pool, char *bootfs)
399{
400	int len = strlen(pool);
401	if (bootfs[0] == '\0')
402		return (B_TRUE);
403
404	if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
405		return (B_FALSE);
406
407	if (strncmp(pool, bootfs, len) == 0 &&
408	    (bootfs[len] == '/' || bootfs[len] == '\0'))
409		return (B_TRUE);
410
411	return (B_FALSE);
412}
413
414boolean_t
415zpool_is_bootable(zpool_handle_t *zhp)
416{
417	char bootfs[ZFS_MAX_DATASET_NAME_LEN];
418
419	return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
420	    sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
421	    sizeof (bootfs)) != 0);
422}
423
424
425/*
426 * Given an nvlist of zpool properties to be set, validate that they are
427 * correct, and parse any numeric properties (index, boolean, etc) if they are
428 * specified as strings.
429 */
430static nvlist_t *
431zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
432    nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
433{
434	nvpair_t *elem;
435	nvlist_t *retprops;
436	zpool_prop_t prop;
437	char *strval;
438	uint64_t intval;
439	char *slash, *check;
440	struct stat64 statbuf;
441	zpool_handle_t *zhp;
442
443	if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
444		(void) no_memory(hdl);
445		return (NULL);
446	}
447
448	elem = NULL;
449	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
450		const char *propname = nvpair_name(elem);
451
452		prop = zpool_name_to_prop(propname);
453		if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
454			int err;
455			char *fname = strchr(propname, '@') + 1;
456
457			err = zfeature_lookup_name(fname, NULL);
458			if (err != 0) {
459				ASSERT3U(err, ==, ENOENT);
460				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
461				    "invalid feature '%s', '%s'"), fname,
462				    propname);
463				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
464				goto error;
465			}
466
467			if (nvpair_type(elem) != DATA_TYPE_STRING) {
468				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469				    "'%s' must be a string"), propname);
470				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
471				goto error;
472			}
473
474			(void) nvpair_value_string(elem, &strval);
475			if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
476				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
477				    "property '%s' can only be set to "
478				    "'enabled'"), propname);
479				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
480				goto error;
481			}
482
483			if (nvlist_add_uint64(retprops, propname, 0) != 0) {
484				(void) no_memory(hdl);
485				goto error;
486			}
487			continue;
488		}
489
490		/*
491		 * Make sure this property is valid and applies to this type.
492		 */
493		if (prop == ZPOOL_PROP_INVAL) {
494			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
495			    "invalid property '%s'"), propname);
496			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
497			goto error;
498		}
499
500		if (zpool_prop_readonly(prop)) {
501			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
502			    "is readonly"), propname);
503			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
504			goto error;
505		}
506
507		if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
508		    &strval, &intval, errbuf) != 0)
509			goto error;
510
511		/*
512		 * Perform additional checking for specific properties.
513		 */
514		switch (prop) {
515		case ZPOOL_PROP_VERSION:
516			if (intval < version ||
517			    !SPA_VERSION_IS_SUPPORTED(intval)) {
518				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
519				    "property '%s' number %d is invalid."),
520				    propname, intval);
521				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
522				goto error;
523			}
524			break;
525
526		case ZPOOL_PROP_BOOTSIZE:
527			if (!flags.create) {
528				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
529				    "property '%s' can only be set during pool "
530				    "creation"), propname);
531				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
532				goto error;
533			}
534			break;
535
536		case ZPOOL_PROP_ASHIFT:
537			if (intval != 0 &&
538			    (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
539				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
540				    "invalid '%s=%d' property: only values "
541				    "between %" PRId32 " and %" PRId32 " "
542				    "are allowed.\n"),
543				    propname, intval, ASHIFT_MIN, ASHIFT_MAX);
544				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
545				goto error;
546			}
547			break;
548
549		case ZPOOL_PROP_BOOTFS:
550			if (flags.create || flags.import) {
551				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
552				    "property '%s' cannot be set at creation "
553				    "or import time"), propname);
554				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
555				goto error;
556			}
557
558			if (version < SPA_VERSION_BOOTFS) {
559				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
560				    "pool must be upgraded to support "
561				    "'%s' property"), propname);
562				(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
563				goto error;
564			}
565
566			/*
567			 * bootfs property value has to be a dataset name and
568			 * the dataset has to be in the same pool as it sets to.
569			 */
570			if (!bootfs_name_valid(poolname, strval)) {
571				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
572				    "is an invalid name"), strval);
573				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
574				goto error;
575			}
576
577			if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
578				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
579				    "could not open pool '%s'"), poolname);
580				(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
581				goto error;
582			}
583			zpool_close(zhp);
584			break;
585
586		case ZPOOL_PROP_ALTROOT:
587			if (!flags.create && !flags.import) {
588				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
589				    "property '%s' can only be set during pool "
590				    "creation or import"), propname);
591				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
592				goto error;
593			}
594
595			if (strval[0] != '/') {
596				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
597				    "bad alternate root '%s'"), strval);
598				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
599				goto error;
600			}
601			break;
602
603		case ZPOOL_PROP_CACHEFILE:
604			if (strval[0] == '\0')
605				break;
606
607			if (strcmp(strval, "none") == 0)
608				break;
609
610			if (strval[0] != '/') {
611				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
612				    "property '%s' must be empty, an "
613				    "absolute path, or 'none'"), propname);
614				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
615				goto error;
616			}
617
618			slash = strrchr(strval, '/');
619
620			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
621			    strcmp(slash, "/..") == 0) {
622				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
623				    "'%s' is not a valid file"), strval);
624				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
625				goto error;
626			}
627
628			*slash = '\0';
629
630			if (strval[0] != '\0' &&
631			    (stat64(strval, &statbuf) != 0 ||
632			    !S_ISDIR(statbuf.st_mode))) {
633				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
634				    "'%s' is not a valid directory"),
635				    strval);
636				(void) zfs_error(hdl, EZFS_BADPATH, errbuf);
637				goto error;
638			}
639
640			*slash = '/';
641			break;
642
643		case ZPOOL_PROP_COMMENT:
644			for (check = strval; *check != '\0'; check++) {
645				if (!isprint(*check)) {
646					zfs_error_aux(hdl,
647					    dgettext(TEXT_DOMAIN,
648					    "comment may only have printable "
649					    "characters"));
650					(void) zfs_error(hdl, EZFS_BADPROP,
651					    errbuf);
652					goto error;
653				}
654			}
655			if (strlen(strval) > ZPROP_MAX_COMMENT) {
656				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
657				    "comment must not exceed %d characters"),
658				    ZPROP_MAX_COMMENT);
659				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
660				goto error;
661			}
662			break;
663
664		case ZPOOL_PROP_READONLY:
665			if (!flags.import) {
666				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
667				    "property '%s' can only be set at "
668				    "import time"), propname);
669				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
670				goto error;
671			}
672			break;
673
674		case ZPOOL_PROP_TNAME:
675			if (!flags.create) {
676				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
677				    "property '%s' can only be set at "
678				    "creation time"), propname);
679				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
680				goto error;
681			}
682			break;
683
684		case ZPOOL_PROP_MULTIHOST:
685			if (get_system_hostid() == 0) {
686				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
687				    "requires a non-zero system hostid"));
688				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
689				goto error;
690			}
691			break;
692
693		default:
694			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
695			    "property '%s'(%d) not defined"), propname, prop);
696			break;
697		}
698	}
699
700	return (retprops);
701error:
702	nvlist_free(retprops);
703	return (NULL);
704}
705
706/*
707 * Set zpool property : propname=propval.
708 */
709int
710zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
711{
712	zfs_cmd_t zc = { 0 };
713	int ret = -1;
714	char errbuf[1024];
715	nvlist_t *nvl = NULL;
716	nvlist_t *realprops;
717	uint64_t version;
718	prop_flags_t flags = { 0 };
719
720	(void) snprintf(errbuf, sizeof (errbuf),
721	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
722	    zhp->zpool_name);
723
724	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
725		return (no_memory(zhp->zpool_hdl));
726
727	if (nvlist_add_string(nvl, propname, propval) != 0) {
728		nvlist_free(nvl);
729		return (no_memory(zhp->zpool_hdl));
730	}
731
732	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
733	if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
734	    zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
735		nvlist_free(nvl);
736		return (-1);
737	}
738
739	nvlist_free(nvl);
740	nvl = realprops;
741
742	/*
743	 * Execute the corresponding ioctl() to set this property.
744	 */
745	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
746
747	if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
748		nvlist_free(nvl);
749		return (-1);
750	}
751
752	ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
753
754	zcmd_free_nvlists(&zc);
755	nvlist_free(nvl);
756
757	if (ret)
758		(void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
759	else
760		(void) zpool_props_refresh(zhp);
761
762	return (ret);
763}
764
765int
766zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
767{
768	libzfs_handle_t *hdl = zhp->zpool_hdl;
769	zprop_list_t *entry;
770	char buf[ZFS_MAXPROPLEN];
771	nvlist_t *features = NULL;
772	zprop_list_t **last;
773	boolean_t firstexpand = (NULL == *plp);
774
775	if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
776		return (-1);
777
778	last = plp;
779	while (*last != NULL)
780		last = &(*last)->pl_next;
781
782	if ((*plp)->pl_all)
783		features = zpool_get_features(zhp);
784
785	if ((*plp)->pl_all && firstexpand) {
786		for (int i = 0; i < SPA_FEATURES; i++) {
787			zprop_list_t *entry = zfs_alloc(hdl,
788			    sizeof (zprop_list_t));
789			entry->pl_prop = ZPROP_INVAL;
790			entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
791			    spa_feature_table[i].fi_uname);
792			entry->pl_width = strlen(entry->pl_user_prop);
793			entry->pl_all = B_TRUE;
794
795			*last = entry;
796			last = &entry->pl_next;
797		}
798	}
799
800	/* add any unsupported features */
801	for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
802	    nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
803		char *propname;
804		boolean_t found;
805		zprop_list_t *entry;
806
807		if (zfeature_is_supported(nvpair_name(nvp)))
808			continue;
809
810		propname = zfs_asprintf(hdl, "unsupported@%s",
811		    nvpair_name(nvp));
812
813		/*
814		 * Before adding the property to the list make sure that no
815		 * other pool already added the same property.
816		 */
817		found = B_FALSE;
818		entry = *plp;
819		while (entry != NULL) {
820			if (entry->pl_user_prop != NULL &&
821			    strcmp(propname, entry->pl_user_prop) == 0) {
822				found = B_TRUE;
823				break;
824			}
825			entry = entry->pl_next;
826		}
827		if (found) {
828			free(propname);
829			continue;
830		}
831
832		entry = zfs_alloc(hdl, sizeof (zprop_list_t));
833		entry->pl_prop = ZPROP_INVAL;
834		entry->pl_user_prop = propname;
835		entry->pl_width = strlen(entry->pl_user_prop);
836		entry->pl_all = B_TRUE;
837
838		*last = entry;
839		last = &entry->pl_next;
840	}
841
842	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
843
844		if (entry->pl_fixed)
845			continue;
846
847		if (entry->pl_prop != ZPROP_INVAL &&
848		    zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
849		    NULL, B_FALSE) == 0) {
850			if (strlen(buf) > entry->pl_width)
851				entry->pl_width = strlen(buf);
852		}
853	}
854
855	return (0);
856}
857
858/*
859 * Get the state for the given feature on the given ZFS pool.
860 */
861int
862zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
863    size_t len)
864{
865	uint64_t refcount;
866	boolean_t found = B_FALSE;
867	nvlist_t *features = zpool_get_features(zhp);
868	boolean_t supported;
869	const char *feature = strchr(propname, '@') + 1;
870
871	supported = zpool_prop_feature(propname);
872	ASSERT(supported || zpool_prop_unsupported(propname));
873
874	/*
875	 * Convert from feature name to feature guid. This conversion is
876	 * unecessary for unsupported@... properties because they already
877	 * use guids.
878	 */
879	if (supported) {
880		int ret;
881		spa_feature_t fid;
882
883		ret = zfeature_lookup_name(feature, &fid);
884		if (ret != 0) {
885			(void) strlcpy(buf, "-", len);
886			return (ENOTSUP);
887		}
888		feature = spa_feature_table[fid].fi_guid;
889	}
890
891	if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
892		found = B_TRUE;
893
894	if (supported) {
895		if (!found) {
896			(void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
897		} else  {
898			if (refcount == 0)
899				(void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
900			else
901				(void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
902		}
903	} else {
904		if (found) {
905			if (refcount == 0) {
906				(void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
907			} else {
908				(void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
909			}
910		} else {
911			(void) strlcpy(buf, "-", len);
912			return (ENOTSUP);
913		}
914	}
915
916	return (0);
917}
918
919/*
920 * Don't start the slice at the default block of 34; many storage
921 * devices will use a stripe width of 128k, so start there instead.
922 */
923#define	NEW_START_BLOCK	256
924
925/*
926 * Validate the given pool name, optionally putting an extended error message in
927 * 'buf'.
928 */
929boolean_t
930zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
931{
932	namecheck_err_t why;
933	char what;
934	int ret;
935
936	ret = pool_namecheck(pool, &why, &what);
937
938	/*
939	 * The rules for reserved pool names were extended at a later point.
940	 * But we need to support users with existing pools that may now be
941	 * invalid.  So we only check for this expanded set of names during a
942	 * create (or import), and only in userland.
943	 */
944	if (ret == 0 && !isopen &&
945	    (strncmp(pool, "mirror", 6) == 0 ||
946	    strncmp(pool, "raidz", 5) == 0 ||
947	    strncmp(pool, "spare", 5) == 0 ||
948	    strcmp(pool, "log") == 0)) {
949		if (hdl != NULL)
950			zfs_error_aux(hdl,
951			    dgettext(TEXT_DOMAIN, "name is reserved"));
952		return (B_FALSE);
953	}
954
955
956	if (ret != 0) {
957		if (hdl != NULL) {
958			switch (why) {
959			case NAME_ERR_TOOLONG:
960				zfs_error_aux(hdl,
961				    dgettext(TEXT_DOMAIN, "name is too long"));
962				break;
963
964			case NAME_ERR_INVALCHAR:
965				zfs_error_aux(hdl,
966				    dgettext(TEXT_DOMAIN, "invalid character "
967				    "'%c' in pool name"), what);
968				break;
969
970			case NAME_ERR_NOLETTER:
971				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
972				    "name must begin with a letter"));
973				break;
974
975			case NAME_ERR_RESERVED:
976				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977				    "name is reserved"));
978				break;
979
980			case NAME_ERR_DISKLIKE:
981				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
982				    "pool name is reserved"));
983				break;
984
985			case NAME_ERR_LEADING_SLASH:
986				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
987				    "leading slash in name"));
988				break;
989
990			case NAME_ERR_EMPTY_COMPONENT:
991				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
992				    "empty component in name"));
993				break;
994
995			case NAME_ERR_TRAILING_SLASH:
996				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997				    "trailing slash in name"));
998				break;
999
1000			case NAME_ERR_MULTIPLE_DELIMITERS:
1001				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1002				    "multiple '@' and/or '#' delimiters in "
1003				    "name"));
1004				break;
1005
1006			default:
1007				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008				    "(%d) not defined"), why);
1009				break;
1010			}
1011		}
1012		return (B_FALSE);
1013	}
1014
1015	return (B_TRUE);
1016}
1017
1018/*
1019 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1020 * state.
1021 */
1022zpool_handle_t *
1023zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1024{
1025	zpool_handle_t *zhp;
1026	boolean_t missing;
1027
1028	/*
1029	 * Make sure the pool name is valid.
1030	 */
1031	if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1032		(void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1033		    dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1034		    pool);
1035		return (NULL);
1036	}
1037
1038	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1039		return (NULL);
1040
1041	zhp->zpool_hdl = hdl;
1042	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1043
1044	if (zpool_refresh_stats(zhp, &missing) != 0) {
1045		zpool_close(zhp);
1046		return (NULL);
1047	}
1048
1049	if (missing) {
1050		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1051		(void) zfs_error_fmt(hdl, EZFS_NOENT,
1052		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1053		zpool_close(zhp);
1054		return (NULL);
1055	}
1056
1057	return (zhp);
1058}
1059
1060/*
1061 * Like the above, but silent on error.  Used when iterating over pools (because
1062 * the configuration cache may be out of date).
1063 */
1064int
1065zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1066{
1067	zpool_handle_t *zhp;
1068	boolean_t missing;
1069
1070	if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1071		return (-1);
1072
1073	zhp->zpool_hdl = hdl;
1074	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1075
1076	if (zpool_refresh_stats(zhp, &missing) != 0) {
1077		zpool_close(zhp);
1078		return (-1);
1079	}
1080
1081	if (missing) {
1082		zpool_close(zhp);
1083		*ret = NULL;
1084		return (0);
1085	}
1086
1087	*ret = zhp;
1088	return (0);
1089}
1090
1091/*
1092 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1093 * state.
1094 */
1095zpool_handle_t *
1096zpool_open(libzfs_handle_t *hdl, const char *pool)
1097{
1098	zpool_handle_t *zhp;
1099
1100	if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1101		return (NULL);
1102
1103	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1104		(void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1105		    dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1106		zpool_close(zhp);
1107		return (NULL);
1108	}
1109
1110	return (zhp);
1111}
1112
1113/*
1114 * Close the handle.  Simply frees the memory associated with the handle.
1115 */
1116void
1117zpool_close(zpool_handle_t *zhp)
1118{
1119	nvlist_free(zhp->zpool_config);
1120	nvlist_free(zhp->zpool_old_config);
1121	nvlist_free(zhp->zpool_props);
1122	free(zhp);
1123}
1124
1125/*
1126 * Return the name of the pool.
1127 */
1128const char *
1129zpool_get_name(zpool_handle_t *zhp)
1130{
1131	return (zhp->zpool_name);
1132}
1133
1134
1135/*
1136 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1137 */
1138int
1139zpool_get_state(zpool_handle_t *zhp)
1140{
1141	return (zhp->zpool_state);
1142}
1143
1144/*
1145 * Check if vdev list contains a special vdev
1146 */
1147static boolean_t
1148zpool_has_special_vdev(nvlist_t *nvroot)
1149{
1150	nvlist_t **child;
1151	uint_t children;
1152
1153	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1154	    &children) == 0) {
1155		for (uint_t c = 0; c < children; c++) {
1156			char *bias;
1157
1158			if (nvlist_lookup_string(child[c],
1159			    ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1160			    strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1161				return (B_TRUE);
1162			}
1163		}
1164	}
1165	return (B_FALSE);
1166}
1167
1168/*
1169 * Create the named pool, using the provided vdev list.  It is assumed
1170 * that the consumer has already validated the contents of the nvlist, so we
1171 * don't have to worry about error semantics.
1172 */
1173int
1174zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1175    nvlist_t *props, nvlist_t *fsprops)
1176{
1177	zfs_cmd_t zc = { 0 };
1178	nvlist_t *zc_fsprops = NULL;
1179	nvlist_t *zc_props = NULL;
1180	nvlist_t *hidden_args = NULL;
1181	uint8_t *wkeydata = NULL;
1182	uint_t wkeylen = 0;
1183	char msg[1024];
1184	int ret = -1;
1185
1186	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1187	    "cannot create '%s'"), pool);
1188
1189	if (!zpool_name_valid(hdl, B_FALSE, pool))
1190		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1191
1192	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1193		return (-1);
1194
1195	if (props) {
1196		prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1197
1198		if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1199		    SPA_VERSION_1, flags, msg)) == NULL) {
1200			goto create_failed;
1201		}
1202	}
1203
1204	if (fsprops) {
1205		uint64_t zoned;
1206		char *zonestr;
1207
1208		zoned = ((nvlist_lookup_string(fsprops,
1209		    zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1210		    strcmp(zonestr, "on") == 0);
1211
1212		if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1213		    fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1214			goto create_failed;
1215		}
1216
1217		if (nvlist_exists(zc_fsprops,
1218		    zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1219		    !zpool_has_special_vdev(nvroot)) {
1220			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1221			    "%s property requires a special vdev"),
1222			    zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1223			(void) zfs_error(hdl, EZFS_BADPROP, msg);
1224			goto create_failed;
1225		}
1226
1227		if (!zc_props &&
1228		    (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1229			goto create_failed;
1230		}
1231		if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1232		    &wkeydata, &wkeylen) != 0) {
1233			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1234			goto create_failed;
1235		}
1236		if (nvlist_add_nvlist(zc_props,
1237		    ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1238			goto create_failed;
1239		}
1240		if (wkeydata != NULL) {
1241			if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1242				goto create_failed;
1243
1244			if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1245			    wkeydata, wkeylen) != 0)
1246				goto create_failed;
1247
1248			if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1249			    hidden_args) != 0)
1250				goto create_failed;
1251		}
1252	}
1253
1254	if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1255		goto create_failed;
1256
1257	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1258
1259	if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1260
1261		zcmd_free_nvlists(&zc);
1262		nvlist_free(zc_props);
1263		nvlist_free(zc_fsprops);
1264		nvlist_free(hidden_args);
1265		if (wkeydata != NULL)
1266			free(wkeydata);
1267
1268		switch (errno) {
1269		case EBUSY:
1270			/*
1271			 * This can happen if the user has specified the same
1272			 * device multiple times.  We can't reliably detect this
1273			 * until we try to add it and see we already have a
1274			 * label.
1275			 */
1276			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277			    "one or more vdevs refer to the same device"));
1278			return (zfs_error(hdl, EZFS_BADDEV, msg));
1279
1280		case ERANGE:
1281			/*
1282			 * This happens if the record size is smaller or larger
1283			 * than the allowed size range, or not a power of 2.
1284			 *
1285			 * NOTE: although zfs_valid_proplist is called earlier,
1286			 * this case may have slipped through since the
1287			 * pool does not exist yet and it is therefore
1288			 * impossible to read properties e.g. max blocksize
1289			 * from the pool.
1290			 */
1291			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1292			    "record size invalid"));
1293			return (zfs_error(hdl, EZFS_BADPROP, msg));
1294
1295		case EOVERFLOW:
1296			/*
1297			 * This occurs when one of the devices is below
1298			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1299			 * device was the problem device since there's no
1300			 * reliable way to determine device size from userland.
1301			 */
1302			{
1303				char buf[64];
1304
1305				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1306
1307				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1308				    "one or more devices is less than the "
1309				    "minimum size (%s)"), buf);
1310			}
1311			return (zfs_error(hdl, EZFS_BADDEV, msg));
1312
1313		case ENOSPC:
1314			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1315			    "one or more devices is out of space"));
1316			return (zfs_error(hdl, EZFS_BADDEV, msg));
1317
1318		default:
1319			return (zpool_standard_error(hdl, errno, msg));
1320		}
1321	}
1322
1323create_failed:
1324	zcmd_free_nvlists(&zc);
1325	nvlist_free(zc_props);
1326	nvlist_free(zc_fsprops);
1327	nvlist_free(hidden_args);
1328	if (wkeydata != NULL)
1329		free(wkeydata);
1330	return (ret);
1331}
1332
1333/*
1334 * Destroy the given pool.  It is up to the caller to ensure that there are no
1335 * datasets left in the pool.
1336 */
1337int
1338zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1339{
1340	zfs_cmd_t zc = { 0 };
1341	zfs_handle_t *zfp = NULL;
1342	libzfs_handle_t *hdl = zhp->zpool_hdl;
1343	char msg[1024];
1344
1345	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1346	    (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1347		return (-1);
1348
1349	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1350	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1351
1352	if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1353		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1354		    "cannot destroy '%s'"), zhp->zpool_name);
1355
1356		if (errno == EROFS) {
1357			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1358			    "one or more devices is read only"));
1359			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1360		} else {
1361			(void) zpool_standard_error(hdl, errno, msg);
1362		}
1363
1364		if (zfp)
1365			zfs_close(zfp);
1366		return (-1);
1367	}
1368
1369	if (zfp) {
1370		remove_mountpoint(zfp);
1371		zfs_close(zfp);
1372	}
1373
1374	return (0);
1375}
1376
1377/*
1378 * Create a checkpoint in the given pool.
1379 */
1380int
1381zpool_checkpoint(zpool_handle_t *zhp)
1382{
1383	libzfs_handle_t *hdl = zhp->zpool_hdl;
1384	char msg[1024];
1385	int error;
1386
1387	error = lzc_pool_checkpoint(zhp->zpool_name);
1388	if (error != 0) {
1389		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1390		    "cannot checkpoint '%s'"), zhp->zpool_name);
1391		(void) zpool_standard_error(hdl, error, msg);
1392		return (-1);
1393	}
1394
1395	return (0);
1396}
1397
1398/*
1399 * Discard the checkpoint from the given pool.
1400 */
1401int
1402zpool_discard_checkpoint(zpool_handle_t *zhp)
1403{
1404	libzfs_handle_t *hdl = zhp->zpool_hdl;
1405	char msg[1024];
1406	int error;
1407
1408	error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1409	if (error != 0) {
1410		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1411		    "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1412		(void) zpool_standard_error(hdl, error, msg);
1413		return (-1);
1414	}
1415
1416	return (0);
1417}
1418
1419/*
1420 * Add the given vdevs to the pool.  The caller must have already performed the
1421 * necessary verification to ensure that the vdev specification is well-formed.
1422 */
1423int
1424zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1425{
1426	zfs_cmd_t zc = { 0 };
1427	int ret;
1428	libzfs_handle_t *hdl = zhp->zpool_hdl;
1429	char msg[1024];
1430	nvlist_t **spares, **l2cache;
1431	uint_t nspares, nl2cache;
1432
1433	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1434	    "cannot add to '%s'"), zhp->zpool_name);
1435
1436	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1437	    SPA_VERSION_SPARES &&
1438	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1439	    &spares, &nspares) == 0) {
1440		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1441		    "upgraded to add hot spares"));
1442		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1443	}
1444
1445	if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1446	    SPA_VERSION_L2CACHE &&
1447	    nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1448	    &l2cache, &nl2cache) == 0) {
1449		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1450		    "upgraded to add cache devices"));
1451		return (zfs_error(hdl, EZFS_BADVERSION, msg));
1452	}
1453
1454	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1455		return (-1);
1456	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1457
1458	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1459		switch (errno) {
1460		case EBUSY:
1461			/*
1462			 * This can happen if the user has specified the same
1463			 * device multiple times.  We can't reliably detect this
1464			 * until we try to add it and see we already have a
1465			 * label.
1466			 */
1467			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1468			    "one or more vdevs refer to the same device"));
1469			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1470			break;
1471
1472		case EINVAL:
1473			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1474			    "invalid config; a pool with removing/removed "
1475			    "vdevs does not support adding raidz vdevs"));
1476			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1477			break;
1478
1479		case EOVERFLOW:
1480			/*
1481			 * This occurrs when one of the devices is below
1482			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1483			 * device was the problem device since there's no
1484			 * reliable way to determine device size from userland.
1485			 */
1486			{
1487				char buf[64];
1488
1489				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1490
1491				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492				    "device is less than the minimum "
1493				    "size (%s)"), buf);
1494			}
1495			(void) zfs_error(hdl, EZFS_BADDEV, msg);
1496			break;
1497
1498		case ENOTSUP:
1499			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1500			    "pool must be upgraded to add these vdevs"));
1501			(void) zfs_error(hdl, EZFS_BADVERSION, msg);
1502			break;
1503
1504		case EDOM:
1505			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506			    "root pool can not have multiple vdevs"
1507			    " or separate logs"));
1508			(void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1509			break;
1510
1511		default:
1512			(void) zpool_standard_error(hdl, errno, msg);
1513		}
1514
1515		ret = -1;
1516	} else {
1517		ret = 0;
1518	}
1519
1520	zcmd_free_nvlists(&zc);
1521
1522	return (ret);
1523}
1524
1525/*
1526 * Exports the pool from the system.  The caller must ensure that there are no
1527 * mounted datasets in the pool.
1528 */
1529static int
1530zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1531    const char *log_str)
1532{
1533	zfs_cmd_t zc = { 0 };
1534	char msg[1024];
1535
1536	(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1537	    "cannot export '%s'"), zhp->zpool_name);
1538
1539	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1540	zc.zc_cookie = force;
1541	zc.zc_guid = hardforce;
1542	zc.zc_history = (uint64_t)(uintptr_t)log_str;
1543
1544	if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1545		switch (errno) {
1546		case EXDEV:
1547			zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1548			    "use '-f' to override the following errors:\n"
1549			    "'%s' has an active shared spare which could be"
1550			    " used by other pools once '%s' is exported."),
1551			    zhp->zpool_name, zhp->zpool_name);
1552			return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1553			    msg));
1554		default:
1555			return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1556			    msg));
1557		}
1558	}
1559
1560	return (0);
1561}
1562
1563int
1564zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1565{
1566	return (zpool_export_common(zhp, force, B_FALSE, log_str));
1567}
1568
1569int
1570zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1571{
1572	return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1573}
1574
1575static void
1576zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1577    nvlist_t *config)
1578{
1579	nvlist_t *nv = NULL;
1580	uint64_t rewindto;
1581	int64_t loss = -1;
1582	struct tm t;
1583	char timestr[128];
1584
1585	if (!hdl->libzfs_printerr || config == NULL)
1586		return;
1587
1588	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1589	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1590		return;
1591	}
1592
1593	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1594		return;
1595	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1596
1597	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1598	    strftime(timestr, 128, 0, &t) != 0) {
1599		if (dryrun) {
1600			(void) printf(dgettext(TEXT_DOMAIN,
1601			    "Would be able to return %s "
1602			    "to its state as of %s.\n"),
1603			    name, timestr);
1604		} else {
1605			(void) printf(dgettext(TEXT_DOMAIN,
1606			    "Pool %s returned to its state as of %s.\n"),
1607			    name, timestr);
1608		}
1609		if (loss > 120) {
1610			(void) printf(dgettext(TEXT_DOMAIN,
1611			    "%s approximately %lld "),
1612			    dryrun ? "Would discard" : "Discarded",
1613			    (loss + 30) / 60);
1614			(void) printf(dgettext(TEXT_DOMAIN,
1615			    "minutes of transactions.\n"));
1616		} else if (loss > 0) {
1617			(void) printf(dgettext(TEXT_DOMAIN,
1618			    "%s approximately %lld "),
1619			    dryrun ? "Would discard" : "Discarded", loss);
1620			(void) printf(dgettext(TEXT_DOMAIN,
1621			    "seconds of transactions.\n"));
1622		}
1623	}
1624}
1625
1626void
1627zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1628    nvlist_t *config)
1629{
1630	nvlist_t *nv = NULL;
1631	int64_t loss = -1;
1632	uint64_t edata = UINT64_MAX;
1633	uint64_t rewindto;
1634	struct tm t;
1635	char timestr[128];
1636
1637	if (!hdl->libzfs_printerr)
1638		return;
1639
1640	if (reason >= 0)
1641		(void) printf(dgettext(TEXT_DOMAIN, "action: "));
1642	else
1643		(void) printf(dgettext(TEXT_DOMAIN, "\t"));
1644
1645	/* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1646	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1647	    nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1648	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1649		goto no_info;
1650
1651	(void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1652	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1653	    &edata);
1654
1655	(void) printf(dgettext(TEXT_DOMAIN,
1656	    "Recovery is possible, but will result in some data loss.\n"));
1657
1658	if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1659	    strftime(timestr, 128, 0, &t) != 0) {
1660		(void) printf(dgettext(TEXT_DOMAIN,
1661		    "\tReturning the pool to its state as of %s\n"
1662		    "\tshould correct the problem.  "),
1663		    timestr);
1664	} else {
1665		(void) printf(dgettext(TEXT_DOMAIN,
1666		    "\tReverting the pool to an earlier state "
1667		    "should correct the problem.\n\t"));
1668	}
1669
1670	if (loss > 120) {
1671		(void) printf(dgettext(TEXT_DOMAIN,
1672		    "Approximately %lld minutes of data\n"
1673		    "\tmust be discarded, irreversibly.  "), (loss + 30) / 60);
1674	} else if (loss > 0) {
1675		(void) printf(dgettext(TEXT_DOMAIN,
1676		    "Approximately %lld seconds of data\n"
1677		    "\tmust be discarded, irreversibly.  "), loss);
1678	}
1679	if (edata != 0 && edata != UINT64_MAX) {
1680		if (edata == 1) {
1681			(void) printf(dgettext(TEXT_DOMAIN,
1682			    "After rewind, at least\n"
1683			    "\tone persistent user-data error will remain.  "));
1684		} else {
1685			(void) printf(dgettext(TEXT_DOMAIN,
1686			    "After rewind, several\n"
1687			    "\tpersistent user-data errors will remain.  "));
1688		}
1689	}
1690	(void) printf(dgettext(TEXT_DOMAIN,
1691	    "Recovery can be attempted\n\tby executing 'zpool %s -F %s'.  "),
1692	    reason >= 0 ? "clear" : "import", name);
1693
1694	(void) printf(dgettext(TEXT_DOMAIN,
1695	    "A scrub of the pool\n"
1696	    "\tis strongly recommended after recovery.\n"));
1697	return;
1698
1699no_info:
1700	(void) printf(dgettext(TEXT_DOMAIN,
1701	    "Destroy and re-create the pool from\n\ta backup source.\n"));
1702}
1703
1704/*
1705 * zpool_import() is a contracted interface. Should be kept the same
1706 * if possible.
1707 *
1708 * Applications should use zpool_import_props() to import a pool with
1709 * new properties value to be set.
1710 */
1711int
1712zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1713    char *altroot)
1714{
1715	nvlist_t *props = NULL;
1716	int ret;
1717
1718	if (altroot != NULL) {
1719		if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1720			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1721			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1722			    newname));
1723		}
1724
1725		if (nvlist_add_string(props,
1726		    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1727		    nvlist_add_string(props,
1728		    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1729			nvlist_free(props);
1730			return (zfs_error_fmt(hdl, EZFS_NOMEM,
1731			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1732			    newname));
1733		}
1734	}
1735
1736	ret = zpool_import_props(hdl, config, newname, props,
1737	    ZFS_IMPORT_NORMAL);
1738	nvlist_free(props);
1739	return (ret);
1740}
1741
1742static void
1743print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1744    int indent)
1745{
1746	nvlist_t **child;
1747	uint_t c, children;
1748	char *vname;
1749	uint64_t is_log = 0;
1750
1751	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1752	    &is_log);
1753
1754	if (name != NULL)
1755		(void) printf("\t%*s%s%s\n", indent, "", name,
1756		    is_log ? " [log]" : "");
1757
1758	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1759	    &child, &children) != 0)
1760		return;
1761
1762	for (c = 0; c < children; c++) {
1763		vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1764		print_vdev_tree(hdl, vname, child[c], indent + 2);
1765		free(vname);
1766	}
1767}
1768
1769void
1770zpool_print_unsup_feat(nvlist_t *config)
1771{
1772	nvlist_t *nvinfo, *unsup_feat;
1773
1774	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1775	    0);
1776	verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1777	    &unsup_feat) == 0);
1778
1779	for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1780	    nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1781		char *desc;
1782
1783		verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1784		verify(nvpair_value_string(nvp, &desc) == 0);
1785
1786		if (strlen(desc) > 0)
1787			(void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1788		else
1789			(void) printf("\t%s\n", nvpair_name(nvp));
1790	}
1791}
1792
1793/*
1794 * Import the given pool using the known configuration and a list of
1795 * properties to be set. The configuration should have come from
1796 * zpool_find_import(). The 'newname' parameters control whether the pool
1797 * is imported with a different name.
1798 */
1799int
1800zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1801    nvlist_t *props, int flags)
1802{
1803	zfs_cmd_t zc = { 0 };
1804	zpool_load_policy_t policy;
1805	nvlist_t *nv = NULL;
1806	nvlist_t *nvinfo = NULL;
1807	nvlist_t *missing = NULL;
1808	char *thename;
1809	char *origname;
1810	int ret;
1811	int error = 0;
1812	char errbuf[1024];
1813
1814	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1815	    &origname) == 0);
1816
1817	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1818	    "cannot import pool '%s'"), origname);
1819
1820	if (newname != NULL) {
1821		if (!zpool_name_valid(hdl, B_FALSE, newname))
1822			return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1823			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1824			    newname));
1825		thename = (char *)newname;
1826	} else {
1827		thename = origname;
1828	}
1829
1830	if (props != NULL) {
1831		uint64_t version;
1832		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1833
1834		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1835		    &version) == 0);
1836
1837		if ((props = zpool_valid_proplist(hdl, origname,
1838		    props, version, flags, errbuf)) == NULL)
1839			return (-1);
1840		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1841			nvlist_free(props);
1842			return (-1);
1843		}
1844		nvlist_free(props);
1845	}
1846
1847	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1848
1849	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1850	    &zc.zc_guid) == 0);
1851
1852	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1853		zcmd_free_nvlists(&zc);
1854		return (-1);
1855	}
1856	if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1857		zcmd_free_nvlists(&zc);
1858		return (-1);
1859	}
1860
1861	zc.zc_cookie = flags;
1862	while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1863	    errno == ENOMEM) {
1864		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1865			zcmd_free_nvlists(&zc);
1866			return (-1);
1867		}
1868	}
1869	if (ret != 0)
1870		error = errno;
1871
1872	(void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1873
1874	zcmd_free_nvlists(&zc);
1875
1876	zpool_get_load_policy(config, &policy);
1877
1878	if (error) {
1879		char desc[1024];
1880		char aux[256];
1881
1882		/*
1883		 * Dry-run failed, but we print out what success
1884		 * looks like if we found a best txg
1885		 */
1886		if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1887			zpool_rewind_exclaim(hdl, newname ? origname : thename,
1888			    B_TRUE, nv);
1889			nvlist_free(nv);
1890			return (-1);
1891		}
1892
1893		if (newname == NULL)
1894			(void) snprintf(desc, sizeof (desc),
1895			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1896			    thename);
1897		else
1898			(void) snprintf(desc, sizeof (desc),
1899			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1900			    origname, thename);
1901
1902		switch (error) {
1903		case ENOTSUP:
1904			if (nv != NULL && nvlist_lookup_nvlist(nv,
1905			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1906			    nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1907				(void) printf(dgettext(TEXT_DOMAIN, "This "
1908				    "pool uses the following feature(s) not "
1909				    "supported by this system:\n"));
1910				zpool_print_unsup_feat(nv);
1911				if (nvlist_exists(nvinfo,
1912				    ZPOOL_CONFIG_CAN_RDONLY)) {
1913					(void) printf(dgettext(TEXT_DOMAIN,
1914					    "All unsupported features are only "
1915					    "required for writing to the pool."
1916					    "\nThe pool can be imported using "
1917					    "'-o readonly=on'.\n"));
1918				}
1919			}
1920			/*
1921			 * Unsupported version.
1922			 */
1923			(void) zfs_error(hdl, EZFS_BADVERSION, desc);
1924			break;
1925
1926		case EREMOTEIO:
1927			if (nv != NULL && nvlist_lookup_nvlist(nv,
1928			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1929				char *hostname = "<unknown>";
1930				uint64_t hostid = 0;
1931				mmp_state_t mmp_state;
1932
1933				mmp_state = fnvlist_lookup_uint64(nvinfo,
1934				    ZPOOL_CONFIG_MMP_STATE);
1935
1936				if (nvlist_exists(nvinfo,
1937				    ZPOOL_CONFIG_MMP_HOSTNAME))
1938					hostname = fnvlist_lookup_string(nvinfo,
1939					    ZPOOL_CONFIG_MMP_HOSTNAME);
1940
1941				if (nvlist_exists(nvinfo,
1942				    ZPOOL_CONFIG_MMP_HOSTID))
1943					hostid = fnvlist_lookup_uint64(nvinfo,
1944					    ZPOOL_CONFIG_MMP_HOSTID);
1945
1946				if (mmp_state == MMP_STATE_ACTIVE) {
1947					(void) snprintf(aux, sizeof (aux),
1948					    dgettext(TEXT_DOMAIN, "pool is imp"
1949					    "orted on host '%s' (hostid=%lx).\n"
1950					    "Export the pool on the other "
1951					    "system, then run 'zpool import'."),
1952					    hostname, (unsigned long) hostid);
1953				} else if (mmp_state == MMP_STATE_NO_HOSTID) {
1954					(void) snprintf(aux, sizeof (aux),
1955					    dgettext(TEXT_DOMAIN, "pool has "
1956					    "the multihost property on and "
1957					    "the\nsystem's hostid is not "
1958					    "set.\n"));
1959				}
1960
1961				(void) zfs_error_aux(hdl, aux);
1962			}
1963			(void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1964			break;
1965
1966		case EINVAL:
1967			(void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1968			break;
1969
1970		case EROFS:
1971			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1972			    "one or more devices is read only"));
1973			(void) zfs_error(hdl, EZFS_BADDEV, desc);
1974			break;
1975
1976		case ENXIO:
1977			if (nv && nvlist_lookup_nvlist(nv,
1978			    ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1979			    nvlist_lookup_nvlist(nvinfo,
1980			    ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1981				(void) printf(dgettext(TEXT_DOMAIN,
1982				    "The devices below are missing or "
1983				    "corrupted, use '-m' to import the pool "
1984				    "anyway:\n"));
1985				print_vdev_tree(hdl, NULL, missing, 2);
1986				(void) printf("\n");
1987			}
1988			(void) zpool_standard_error(hdl, error, desc);
1989			break;
1990
1991		case EEXIST:
1992			(void) zpool_standard_error(hdl, error, desc);
1993			break;
1994		case ENAMETOOLONG:
1995			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1996			    "new name of at least one dataset is longer than "
1997			    "the maximum allowable length"));
1998			(void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1999			break;
2000		default:
2001			(void) zpool_standard_error(hdl, error, desc);
2002			zpool_explain_recover(hdl,
2003			    newname ? origname : thename, -error, nv);
2004			break;
2005		}
2006
2007		nvlist_free(nv);
2008		ret = -1;
2009	} else {
2010		zpool_handle_t *zhp;
2011
2012		/*
2013		 * This should never fail, but play it safe anyway.
2014		 */
2015		if (zpool_open_silent(hdl, thename, &zhp) != 0)
2016			ret = -1;
2017		else if (zhp != NULL)
2018			zpool_close(zhp);
2019		if (policy.zlp_rewind &
2020		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2021			zpool_rewind_exclaim(hdl, newname ? origname : thename,
2022			    ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2023		}
2024		nvlist_free(nv);
2025		return (0);
2026	}
2027
2028	return (ret);
2029}
2030
2031/*
2032 * Translate vdev names to guids.  If a vdev_path is determined to be
2033 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2034 * are added to it.
2035 */
2036static int
2037zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2038    nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2039{
2040	nvlist_t *errlist = NULL;
2041	int error = 0;
2042
2043	for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2044	    elem = nvlist_next_nvpair(vds, elem)) {
2045		boolean_t spare, cache;
2046
2047		char *vd_path = nvpair_name(elem);
2048		nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2049		    NULL);
2050
2051		if ((tgt == NULL) || cache || spare) {
2052			if (errlist == NULL) {
2053				errlist = fnvlist_alloc();
2054				error = EINVAL;
2055			}
2056
2057			uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2058			    (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2059			fnvlist_add_int64(errlist, vd_path, err);
2060			continue;
2061		}
2062
2063		uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2064		fnvlist_add_uint64(vdev_guids, vd_path, guid);
2065
2066		char msg[MAXNAMELEN];
2067		(void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2068		fnvlist_add_string(guids_to_paths, msg, vd_path);
2069	}
2070
2071	if (error != 0) {
2072		verify(errlist != NULL);
2073		if (vd_errlist != NULL)
2074			*vd_errlist = errlist;
2075		else
2076			fnvlist_free(errlist);
2077	}
2078
2079	return (error);
2080}
2081
2082/*
2083 * Scan the pool.
2084 */
2085int
2086zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2087{
2088	zfs_cmd_t zc = { 0 };
2089	char msg[1024];
2090	int err;
2091	libzfs_handle_t *hdl = zhp->zpool_hdl;
2092
2093	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2094	zc.zc_cookie = func;
2095	zc.zc_flags = cmd;
2096
2097	if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2098		return (0);
2099
2100	err = errno;
2101
2102	/* ECANCELED on a scrub means we resumed a paused scrub */
2103	if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2104	    cmd == POOL_SCRUB_NORMAL)
2105		return (0);
2106
2107	if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2108		return (0);
2109
2110	if (func == POOL_SCAN_SCRUB) {
2111		if (cmd == POOL_SCRUB_PAUSE) {
2112			(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2113			    "cannot pause scrubbing %s"), zc.zc_name);
2114		} else {
2115			assert(cmd == POOL_SCRUB_NORMAL);
2116			(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2117			    "cannot scrub %s"), zc.zc_name);
2118		}
2119	} else if (func == POOL_SCAN_RESILVER) {
2120		assert(cmd == POOL_SCRUB_NORMAL);
2121		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2122		    "cannot restart resilver on %s"), zc.zc_name);
2123	} else if (func == POOL_SCAN_NONE) {
2124		(void) snprintf(msg, sizeof (msg),
2125		    dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2126		    zc.zc_name);
2127	} else {
2128		assert(!"unexpected result");
2129	}
2130
2131	if (err == EBUSY) {
2132		nvlist_t *nvroot;
2133		pool_scan_stat_t *ps = NULL;
2134		uint_t psc;
2135
2136		verify(nvlist_lookup_nvlist(zhp->zpool_config,
2137		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2138		(void) nvlist_lookup_uint64_array(nvroot,
2139		    ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2140		if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2141			if (cmd == POOL_SCRUB_PAUSE)
2142				return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2143			else
2144				return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2145		} else {
2146			return (zfs_error(hdl, EZFS_RESILVERING, msg));
2147		}
2148	} else if (err == ENOENT) {
2149		return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2150	} else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2151		return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2152	} else {
2153		return (zpool_standard_error(hdl, err, msg));
2154	}
2155}
2156
2157static int
2158xlate_init_err(int err)
2159{
2160	switch (err) {
2161	case ENODEV:
2162		return (EZFS_NODEVICE);
2163	case EINVAL:
2164	case EROFS:
2165		return (EZFS_BADDEV);
2166	case EBUSY:
2167		return (EZFS_INITIALIZING);
2168	case ESRCH:
2169		return (EZFS_NO_INITIALIZE);
2170	}
2171	return (err);
2172}
2173
2174/*
2175 * Begin, suspend, or cancel the initialization (initializing of all free
2176 * blocks) for the given vdevs in the given pool.
2177 */
2178int
2179zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2180    nvlist_t *vds)
2181{
2182	char msg[1024];
2183	int err;
2184
2185	nvlist_t *vdev_guids = fnvlist_alloc();
2186	nvlist_t *guids_to_paths = fnvlist_alloc();
2187	nvlist_t *vd_errlist = NULL;
2188	nvlist_t *errlist;
2189	nvpair_t *elem;
2190
2191	err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2192	    guids_to_paths, &vd_errlist);
2193
2194	if (err == 0) {
2195		err = lzc_initialize(zhp->zpool_name, cmd_type,
2196		    vdev_guids, &errlist);
2197		if (err == 0) {
2198			fnvlist_free(vdev_guids);
2199			fnvlist_free(guids_to_paths);
2200			return (0);
2201		}
2202
2203		if (errlist != NULL) {
2204			vd_errlist = fnvlist_lookup_nvlist(errlist,
2205			    ZPOOL_INITIALIZE_VDEVS);
2206		}
2207
2208		(void) snprintf(msg, sizeof (msg),
2209		    dgettext(TEXT_DOMAIN, "operation failed"));
2210	} else {
2211		verify(vd_errlist != NULL);
2212	}
2213
2214	for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2215	    elem = nvlist_next_nvpair(vd_errlist, elem)) {
2216		int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2217		char *path;
2218
2219		if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2220		    &path) != 0)
2221			path = nvpair_name(elem);
2222
2223		(void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2224		    "cannot initialize '%s'", path);
2225	}
2226
2227	fnvlist_free(vdev_guids);
2228	fnvlist_free(guids_to_paths);
2229
2230	if (vd_errlist != NULL) {
2231		fnvlist_free(vd_errlist);
2232		return (-1);
2233	}
2234
2235	return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2236}
2237
2238static int
2239xlate_trim_err(int err)
2240{
2241	switch (err) {
2242	case ENODEV:
2243		return (EZFS_NODEVICE);
2244	case EINVAL:
2245	case EROFS:
2246		return (EZFS_BADDEV);
2247	case EBUSY:
2248		return (EZFS_TRIMMING);
2249	case ESRCH:
2250		return (EZFS_NO_TRIM);
2251	case EOPNOTSUPP:
2252		return (EZFS_TRIM_NOTSUP);
2253	}
2254	return (err);
2255}
2256
2257/*
2258 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2259 * the given vdevs in the given pool.
2260 */
2261int
2262zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2263    trimflags_t *trim_flags)
2264{
2265	char msg[1024];
2266	int err;
2267
2268	nvlist_t *vdev_guids = fnvlist_alloc();
2269	nvlist_t *guids_to_paths = fnvlist_alloc();
2270	nvlist_t *vd_errlist = NULL;
2271	nvlist_t *errlist;
2272	nvpair_t *elem;
2273
2274	err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2275	    guids_to_paths, &vd_errlist);
2276	if (err == 0) {
2277		err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2278		    trim_flags->secure, vdev_guids, &errlist);
2279		if (err == 0) {
2280			fnvlist_free(vdev_guids);
2281			fnvlist_free(guids_to_paths);
2282			return (0);
2283		}
2284
2285		if (errlist != NULL) {
2286			vd_errlist = fnvlist_lookup_nvlist(errlist,
2287			    ZPOOL_TRIM_VDEVS);
2288		}
2289
2290		(void) snprintf(msg, sizeof (msg),
2291		    dgettext(TEXT_DOMAIN, "operation failed"));
2292	} else {
2293		verify(vd_errlist != NULL);
2294	}
2295
2296	for (elem = nvlist_next_nvpair(vd_errlist, NULL);
2297	    elem != NULL; elem = nvlist_next_nvpair(vd_errlist, elem)) {
2298		int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2299		char *path;
2300		/*
2301		 * If only the pool was specified, and it was not a secure
2302		 * trim then suppress warnings for individual vdevs which
2303		 * do not support trimming.
2304		 */
2305		if (vd_error == EZFS_TRIM_NOTSUP &&
2306		    trim_flags->fullpool &&
2307		    !trim_flags->secure) {
2308			continue;
2309		}
2310
2311		if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2312		    &path) != 0)
2313			path = nvpair_name(elem);
2314
2315		(void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2316		    "cannot trim '%s'", path);
2317	}
2318
2319	fnvlist_free(vdev_guids);
2320	fnvlist_free(guids_to_paths);
2321
2322	if (vd_errlist != NULL) {
2323		fnvlist_free(vd_errlist);
2324		return (-1);
2325	}
2326
2327	return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2328}
2329
2330/*
2331 * This provides a very minimal check whether a given string is likely a
2332 * c#t#d# style string.  Users of this are expected to do their own
2333 * verification of the s# part.
2334 */
2335#define	CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
2336
2337/*
2338 * More elaborate version for ones which may start with "/dev/dsk/"
2339 * and the like.
2340 */
2341static int
2342ctd_check_path(char *str)
2343{
2344	/*
2345	 * If it starts with a slash, check the last component.
2346	 */
2347	if (str && str[0] == '/') {
2348		char *tmp = strrchr(str, '/');
2349
2350		/*
2351		 * If it ends in "/old", check the second-to-last
2352		 * component of the string instead.
2353		 */
2354		if (tmp != str && strcmp(tmp, "/old") == 0) {
2355			for (tmp--; *tmp != '/'; tmp--)
2356				;
2357		}
2358		str = tmp + 1;
2359	}
2360	return (CTD_CHECK(str));
2361}
2362
2363/*
2364 * Find a vdev that matches the search criteria specified. We use the
2365 * the nvpair name to determine how we should look for the device.
2366 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2367 * spare; but FALSE if its an INUSE spare.
2368 */
2369static nvlist_t *
2370vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2371    boolean_t *l2cache, boolean_t *log)
2372{
2373	uint_t c, children;
2374	nvlist_t **child;
2375	nvlist_t *ret;
2376	uint64_t is_log;
2377	char *srchkey;
2378	nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2379
2380	/* Nothing to look for */
2381	if (search == NULL || pair == NULL)
2382		return (NULL);
2383
2384	/* Obtain the key we will use to search */
2385	srchkey = nvpair_name(pair);
2386
2387	switch (nvpair_type(pair)) {
2388	case DATA_TYPE_UINT64:
2389		if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2390			uint64_t srchval, theguid;
2391
2392			verify(nvpair_value_uint64(pair, &srchval) == 0);
2393			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2394			    &theguid) == 0);
2395			if (theguid == srchval)
2396				return (nv);
2397		}
2398		break;
2399
2400	case DATA_TYPE_STRING: {
2401		char *srchval, *val;
2402
2403		verify(nvpair_value_string(pair, &srchval) == 0);
2404		if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2405			break;
2406
2407		/*
2408		 * Search for the requested value. Special cases:
2409		 *
2410		 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2411		 *   UEFI boot, these end in "s0" or "s0/old" or "s1" or
2412		 *   "s1/old".   The "s0" or "s1" part is hidden from the user,
2413		 *   but included in the string, so this matches around it.
2414		 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2415		 *
2416		 * Otherwise, all other searches are simple string compares.
2417		 */
2418		if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2419		    ctd_check_path(val)) {
2420			uint64_t wholedisk = 0;
2421
2422			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2423			    &wholedisk);
2424			if (wholedisk) {
2425				int slen = strlen(srchval);
2426				int vlen = strlen(val);
2427
2428				if (slen != vlen - 2)
2429					break;
2430
2431				/*
2432				 * make_leaf_vdev() should only set
2433				 * wholedisk for ZPOOL_CONFIG_PATHs which
2434				 * will include "/dev/dsk/", giving plenty of
2435				 * room for the indices used next.
2436				 */
2437				ASSERT(vlen >= 6);
2438
2439				/*
2440				 * strings identical except trailing "s0"
2441				 */
2442				if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2443				    strcmp(&val[vlen - 2], "s1") == 0) &&
2444				    strncmp(srchval, val, slen) == 0)
2445					return (nv);
2446
2447				/*
2448				 * strings identical except trailing "s0/old"
2449				 */
2450				if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2451				    strcmp(&val[vlen - 6], "s1/old") == 0) &&
2452				    strcmp(&srchval[slen - 4], "/old") == 0 &&
2453				    strncmp(srchval, val, slen - 4) == 0)
2454					return (nv);
2455
2456				break;
2457			}
2458		} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2459			char *type, *idx, *end, *p;
2460			uint64_t id, vdev_id;
2461
2462			/*
2463			 * Determine our vdev type, keeping in mind
2464			 * that the srchval is composed of a type and
2465			 * vdev id pair (i.e. mirror-4).
2466			 */
2467			if ((type = strdup(srchval)) == NULL)
2468				return (NULL);
2469
2470			if ((p = strrchr(type, '-')) == NULL) {
2471				free(type);
2472				break;
2473			}
2474			idx = p + 1;
2475			*p = '\0';
2476
2477			/*
2478			 * If the types don't match then keep looking.
2479			 */
2480			if (strncmp(val, type, strlen(val)) != 0) {
2481				free(type);
2482				break;
2483			}
2484
2485			verify(zpool_vdev_is_interior(type));
2486			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2487			    &id) == 0);
2488
2489			errno = 0;
2490			vdev_id = strtoull(idx, &end, 10);
2491
2492			free(type);
2493			if (errno != 0)
2494				return (NULL);
2495
2496			/*
2497			 * Now verify that we have the correct vdev id.
2498			 */
2499			if (vdev_id == id)
2500				return (nv);
2501		}
2502
2503		/*
2504		 * Common case
2505		 */
2506		if (strcmp(srchval, val) == 0)
2507			return (nv);
2508		break;
2509	}
2510
2511	default:
2512		break;
2513	}
2514
2515	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2516	    &child, &children) != 0)
2517		return (NULL);
2518
2519	for (c = 0; c < children; c++) {
2520		if ((ret = vdev_to_nvlist_iter(child[c], search,
2521		    avail_spare, l2cache, NULL)) != NULL) {
2522			/*
2523			 * The 'is_log' value is only set for the toplevel
2524			 * vdev, not the leaf vdevs.  So we always lookup the
2525			 * log device from the root of the vdev tree (where
2526			 * 'log' is non-NULL).
2527			 */
2528			if (log != NULL &&
2529			    nvlist_lookup_uint64(child[c],
2530			    ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2531			    is_log) {
2532				*log = B_TRUE;
2533			}
2534			return (ret);
2535		}
2536	}
2537
2538	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2539	    &child, &children) == 0) {
2540		for (c = 0; c < children; c++) {
2541			if ((ret = vdev_to_nvlist_iter(child[c], search,
2542			    avail_spare, l2cache, NULL)) != NULL) {
2543				*avail_spare = B_TRUE;
2544				return (ret);
2545			}
2546		}
2547	}
2548
2549	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2550	    &child, &children) == 0) {
2551		for (c = 0; c < children; c++) {
2552			if ((ret = vdev_to_nvlist_iter(child[c], search,
2553			    avail_spare, l2cache, NULL)) != NULL) {
2554				*l2cache = B_TRUE;
2555				return (ret);
2556			}
2557		}
2558	}
2559
2560	return (NULL);
2561}
2562
2563/*
2564 * Given a physical path (minus the "/devices" prefix), find the
2565 * associated vdev.
2566 */
2567nvlist_t *
2568zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2569    boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2570{
2571	nvlist_t *search, *nvroot, *ret;
2572
2573	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2574	verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2575
2576	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2577	    &nvroot) == 0);
2578
2579	*avail_spare = B_FALSE;
2580	*l2cache = B_FALSE;
2581	if (log != NULL)
2582		*log = B_FALSE;
2583	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2584	nvlist_free(search);
2585
2586	return (ret);
2587}
2588
2589/*
2590 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2591 */
2592static boolean_t
2593zpool_vdev_is_interior(const char *name)
2594{
2595	if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2596	    strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2597	    strncmp(name,
2598	    VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2599	    strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2600		return (B_TRUE);
2601	return (B_FALSE);
2602}
2603
2604nvlist_t *
2605zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2606    boolean_t *l2cache, boolean_t *log)
2607{
2608	char buf[MAXPATHLEN];
2609	char *end;
2610	nvlist_t *nvroot, *search, *ret;
2611	uint64_t guid;
2612
2613	verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2614
2615	guid = strtoull(path, &end, 10);
2616	if (guid != 0 && *end == '\0') {
2617		verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2618	} else if (zpool_vdev_is_interior(path)) {
2619		verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2620	} else if (path[0] != '/') {
2621		(void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT,
2622		    path);
2623		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2624	} else {
2625		verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2626	}
2627
2628	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2629	    &nvroot) == 0);
2630
2631	*avail_spare = B_FALSE;
2632	*l2cache = B_FALSE;
2633	if (log != NULL)
2634		*log = B_FALSE;
2635	ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2636	nvlist_free(search);
2637
2638	return (ret);
2639}
2640
2641static int
2642vdev_is_online(nvlist_t *nv)
2643{
2644	uint64_t ival;
2645
2646	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2647	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2648	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2649		return (0);
2650
2651	return (1);
2652}
2653
2654/*
2655 * Helper function for zpool_get_physpaths().
2656 */
2657static int
2658vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2659    size_t *bytes_written)
2660{
2661	size_t bytes_left, pos, rsz;
2662	char *tmppath;
2663	const char *format;
2664
2665	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2666	    &tmppath) != 0)
2667		return (EZFS_NODEVICE);
2668
2669	pos = *bytes_written;
2670	bytes_left = physpath_size - pos;
2671	format = (pos == 0) ? "%s" : " %s";
2672
2673	rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2674	*bytes_written += rsz;
2675
2676	if (rsz >= bytes_left) {
2677		/* if physpath was not copied properly, clear it */
2678		if (bytes_left != 0) {
2679			physpath[pos] = 0;
2680		}
2681		return (EZFS_NOSPC);
2682	}
2683	return (0);
2684}
2685
2686static int
2687vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2688    size_t *rsz, boolean_t is_spare)
2689{
2690	char *type;
2691	int ret;
2692
2693	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2694		return (EZFS_INVALCONFIG);
2695
2696	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2697		/*
2698		 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2699		 * For a spare vdev, we only want to boot from the active
2700		 * spare device.
2701		 */
2702		if (is_spare) {
2703			uint64_t spare = 0;
2704			(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2705			    &spare);
2706			if (!spare)
2707				return (EZFS_INVALCONFIG);
2708		}
2709
2710		if (vdev_is_online(nv)) {
2711			if ((ret = vdev_get_one_physpath(nv, physpath,
2712			    phypath_size, rsz)) != 0)
2713				return (ret);
2714		}
2715	} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2716	    strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2717	    strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2718	    (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2719		nvlist_t **child;
2720		uint_t count;
2721		int i, ret;
2722
2723		if (nvlist_lookup_nvlist_array(nv,
2724		    ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2725			return (EZFS_INVALCONFIG);
2726
2727		for (i = 0; i < count; i++) {
2728			ret = vdev_get_physpaths(child[i], physpath,
2729			    phypath_size, rsz, is_spare);
2730			if (ret == EZFS_NOSPC)
2731				return (ret);
2732		}
2733	}
2734
2735	return (EZFS_POOL_INVALARG);
2736}
2737
2738/*
2739 * Get phys_path for a root pool config.
2740 * Return 0 on success; non-zero on failure.
2741 */
2742static int
2743zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2744{
2745	size_t rsz;
2746	nvlist_t *vdev_root;
2747	nvlist_t **child;
2748	uint_t count;
2749	char *type;
2750
2751	rsz = 0;
2752
2753	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2754	    &vdev_root) != 0)
2755		return (EZFS_INVALCONFIG);
2756
2757	if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2758	    nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2759	    &child, &count) != 0)
2760		return (EZFS_INVALCONFIG);
2761
2762	/*
2763	 * root pool can only have a single top-level vdev.
2764	 */
2765	if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2766		return (EZFS_POOL_INVALARG);
2767
2768	(void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2769	    B_FALSE);
2770
2771	/* No online devices */
2772	if (rsz == 0)
2773		return (EZFS_NODEVICE);
2774
2775	return (0);
2776}
2777
2778/*
2779 * Get phys_path for a root pool
2780 * Return 0 on success; non-zero on failure.
2781 */
2782int
2783zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2784{
2785	return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2786	    phypath_size));
2787}
2788
2789/*
2790 * If the device has being dynamically expanded then we need to relabel
2791 * the disk to use the new unallocated space.
2792 */
2793static int
2794zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2795{
2796	char path[MAXPATHLEN];
2797	char errbuf[1024];
2798	int fd, error;
2799	int (*_efi_use_whole_disk)(int);
2800
2801	if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2802	    "efi_use_whole_disk")) == NULL)
2803		return (-1);
2804
2805	(void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2806
2807	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2808		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2809		    "relabel '%s': unable to open device"), name);
2810		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2811	}
2812
2813	/*
2814	 * It's possible that we might encounter an error if the device
2815	 * does not have any unallocated space left. If so, we simply
2816	 * ignore that error and continue on.
2817	 */
2818	error = _efi_use_whole_disk(fd);
2819	(void) close(fd);
2820	if (error && error != VT_ENOSPC) {
2821		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2822		    "relabel '%s': unable to read disk capacity"), name);
2823		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2824	}
2825	return (0);
2826}
2827
2828/*
2829 * Bring the specified vdev online.   The 'flags' parameter is a set of the
2830 * ZFS_ONLINE_* flags.
2831 */
2832int
2833zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2834    vdev_state_t *newstate)
2835{
2836	zfs_cmd_t zc = { 0 };
2837	char msg[1024];
2838	char *pathname;
2839	nvlist_t *tgt;
2840	boolean_t avail_spare, l2cache, islog;
2841	libzfs_handle_t *hdl = zhp->zpool_hdl;
2842
2843	if (flags & ZFS_ONLINE_EXPAND) {
2844		(void) snprintf(msg, sizeof (msg),
2845		    dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2846	} else {
2847		(void) snprintf(msg, sizeof (msg),
2848		    dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2849	}
2850
2851	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2852	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2853	    &islog)) == NULL)
2854		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2855
2856	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2857
2858	if (avail_spare)
2859		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2860
2861	if ((flags & ZFS_ONLINE_EXPAND ||
2862	    zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2863	    nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2864		uint64_t wholedisk = 0;
2865
2866		(void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2867		    &wholedisk);
2868
2869		/*
2870		 * XXX - L2ARC 1.0 devices can't support expansion.
2871		 */
2872		if (l2cache) {
2873			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2874			    "cannot expand cache devices"));
2875			return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2876		}
2877
2878		if (wholedisk) {
2879			pathname += strlen(ZFS_DISK_ROOT) + 1;
2880			(void) zpool_relabel_disk(hdl, pathname);
2881		}
2882	}
2883
2884	zc.zc_cookie = VDEV_STATE_ONLINE;
2885	zc.zc_obj = flags;
2886
2887	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2888		if (errno == EINVAL) {
2889			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2890			    "from this pool into a new one.  Use '%s' "
2891			    "instead"), "zpool detach");
2892			return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2893		}
2894		return (zpool_standard_error(hdl, errno, msg));
2895	}
2896
2897	*newstate = zc.zc_cookie;
2898	return (0);
2899}
2900
2901/*
2902 * Take the specified vdev offline
2903 */
2904int
2905zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2906{
2907	zfs_cmd_t zc = { 0 };
2908	char msg[1024];
2909	nvlist_t *tgt;
2910	boolean_t avail_spare, l2cache;
2911	libzfs_handle_t *hdl = zhp->zpool_hdl;
2912
2913	(void) snprintf(msg, sizeof (msg),
2914	    dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2915
2916	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2917	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2918	    NULL)) == NULL)
2919		return (zfs_error(hdl, EZFS_NODEVICE, msg));
2920
2921	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2922
2923	if (avail_spare)
2924		return (zfs_error(hdl, EZFS_ISSPARE, msg));
2925
2926	zc.zc_cookie = VDEV_STATE_OFFLINE;
2927	zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2928
2929	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2930		return (0);
2931
2932	switch (errno) {
2933	case EBUSY:
2934
2935		/*
2936		 * There are no other replicas of this device.
2937		 */
2938		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2939
2940	case EEXIST:
2941		/*
2942		 * The log device has unplayed logs
2943		 */
2944		return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2945
2946	default:
2947		return (zpool_standard_error(hdl, errno, msg));
2948	}
2949}
2950
2951/*
2952 * Mark the given vdev faulted.
2953 */
2954int
2955zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2956{
2957	zfs_cmd_t zc = { 0 };
2958	char msg[1024];
2959	libzfs_handle_t *hdl = zhp->zpool_hdl;
2960
2961	(void) snprintf(msg, sizeof (msg),
2962	    dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2963
2964	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2965	zc.zc_guid = guid;
2966	zc.zc_cookie = VDEV_STATE_FAULTED;
2967	zc.zc_obj = aux;
2968
2969	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2970		return (0);
2971
2972	switch (errno) {
2973	case EBUSY:
2974
2975		/*
2976		 * There are no other replicas of this device.
2977		 */
2978		return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2979
2980	default:
2981		return (zpool_standard_error(hdl, errno, msg));
2982	}
2983
2984}
2985
2986/*
2987 * Mark the given vdev degraded.
2988 */
2989int
2990zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2991{
2992	zfs_cmd_t zc = { 0 };
2993	char msg[1024];
2994	libzfs_handle_t *hdl = zhp->zpool_hdl;
2995
2996	(void) snprintf(msg, sizeof (msg),
2997	    dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2998
2999	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3000	zc.zc_guid = guid;
3001	zc.zc_cookie = VDEV_STATE_DEGRADED;
3002	zc.zc_obj = aux;
3003
3004	if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3005		return (0);
3006
3007	return (zpool_standard_error(hdl, errno, msg));
3008}
3009
3010/*
3011 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3012 * a hot spare.
3013 */
3014static boolean_t
3015is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3016{
3017	nvlist_t **child;
3018	uint_t c, children;
3019	char *type;
3020
3021	if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3022	    &children) == 0) {
3023		verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3024		    &type) == 0);
3025
3026		if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
3027		    children == 2 && child[which] == tgt)
3028			return (B_TRUE);
3029
3030		for (c = 0; c < children; c++)
3031			if (is_replacing_spare(child[c], tgt, which))
3032				return (B_TRUE);
3033	}
3034
3035	return (B_FALSE);
3036}
3037
3038/*
3039 * Attach new_disk (fully described by nvroot) to old_disk.
3040 * If 'replacing' is specified, the new disk will replace the old one.
3041 */
3042int
3043zpool_vdev_attach(zpool_handle_t *zhp,
3044    const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
3045{
3046	zfs_cmd_t zc = { 0 };
3047	char msg[1024];
3048	int ret;
3049	nvlist_t *tgt, *newvd;
3050	boolean_t avail_spare, l2cache, islog;
3051	uint64_t val;
3052	char *newname;
3053	nvlist_t **child;
3054	uint_t children;
3055	nvlist_t *config_root;
3056	libzfs_handle_t *hdl = zhp->zpool_hdl;
3057	boolean_t rootpool = zpool_is_bootable(zhp);
3058
3059	if (replacing)
3060		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3061		    "cannot replace %s with %s"), old_disk, new_disk);
3062	else
3063		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3064		    "cannot attach %s to %s"), new_disk, old_disk);
3065
3066	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3067	if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3068	    &islog)) == NULL)
3069		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3070
3071	if (avail_spare)
3072		return (zfs_error(hdl, EZFS_ISSPARE, msg));
3073
3074	if (l2cache)
3075		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3076
3077	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3078	zc.zc_cookie = replacing;
3079
3080	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3081	    &child, &children) != 0 || children != 1) {
3082		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3083		    "new device must be a single disk"));
3084		return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3085	}
3086
3087	verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3088	    ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3089
3090	if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3091		return (-1);
3092
3093	newvd = zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, NULL);
3094	/*
3095	 * If the target is a hot spare that has been swapped in, we can only
3096	 * replace it with another hot spare.
3097	 */
3098	if (replacing &&
3099	    nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3100	    (newvd == NULL || !avail_spare) &&
3101	    is_replacing_spare(config_root, tgt, 1)) {
3102		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3103		    "can only be replaced by another hot spare"));
3104		free(newname);
3105		return (zfs_error(hdl, EZFS_BADTARGET, msg));
3106	}
3107
3108	free(newname);
3109
3110	if (replacing && avail_spare && !vdev_is_online(newvd)) {
3111		(void) zpool_standard_error(hdl, ENXIO, msg);
3112		return (-1);
3113	}
3114
3115	if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3116		return (-1);
3117
3118	ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3119
3120	zcmd_free_nvlists(&zc);
3121
3122	if (ret == 0) {
3123		if (rootpool) {
3124			/*
3125			 * XXX need a better way to prevent user from
3126			 * booting up a half-baked vdev.
3127			 */
3128			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
3129			    "sure to wait until resilver is done "
3130			    "before rebooting.\n"));
3131		}
3132		return (0);
3133	}
3134
3135	switch (errno) {
3136	case ENOTSUP:
3137		/*
3138		 * Can't attach to or replace this type of vdev.
3139		 */
3140		if (replacing) {
3141			uint64_t version = zpool_get_prop_int(zhp,
3142			    ZPOOL_PROP_VERSION, NULL);
3143
3144			if (islog)
3145				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3146				    "cannot replace a log with a spare"));
3147			else if (version >= SPA_VERSION_MULTI_REPLACE)
3148				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3149				    "already in replacing/spare config; wait "
3150				    "for completion or use 'zpool detach'"));
3151			else
3152				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3153				    "cannot replace a replacing device"));
3154		} else {
3155			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3156			    "can only attach to mirrors and top-level "
3157			    "disks"));
3158		}
3159		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
3160		break;
3161
3162	case EINVAL:
3163		/*
3164		 * The new device must be a single disk.
3165		 */
3166		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3167		    "new device must be a single disk"));
3168		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3169		break;
3170
3171	case EBUSY:
3172		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3173		    "or device removal is in progress"),
3174		    new_disk);
3175		(void) zfs_error(hdl, EZFS_BADDEV, msg);
3176		break;
3177
3178	case EOVERFLOW:
3179		/*
3180		 * The new device is too small.
3181		 */
3182		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3183		    "device is too small"));
3184		(void) zfs_error(hdl, EZFS_BADDEV, msg);
3185		break;
3186
3187	case EDOM:
3188		/*
3189		 * The new device has a different optimal sector size.
3190		 */
3191		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3192		    "new device has a different optimal sector size; use the "
3193		    "option '-o ashift=N' to override the optimal size"));
3194		(void) zfs_error(hdl, EZFS_BADDEV, msg);
3195		break;
3196
3197	case ENAMETOOLONG:
3198		/*
3199		 * The resulting top-level vdev spec won't fit in the label.
3200		 */
3201		(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3202		break;
3203
3204	default:
3205		(void) zpool_standard_error(hdl, errno, msg);
3206	}
3207
3208	return (-1);
3209}
3210
3211/*
3212 * Detach the specified device.
3213 */
3214int
3215zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3216{
3217	zfs_cmd_t zc = { 0 };
3218	char msg[1024];
3219	nvlist_t *tgt;
3220	boolean_t avail_spare, l2cache;
3221	libzfs_handle_t *hdl = zhp->zpool_hdl;
3222
3223	(void) snprintf(msg, sizeof (msg),
3224	    dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3225
3226	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3227	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3228	    NULL)) == NULL)
3229		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3230
3231	if (avail_spare)
3232		return (zfs_error(hdl, EZFS_ISSPARE, msg));
3233
3234	if (l2cache)
3235		return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3236
3237	verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3238
3239	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3240		return (0);
3241
3242	switch (errno) {
3243
3244	case ENOTSUP:
3245		/*
3246		 * Can't detach from this type of vdev.
3247		 */
3248		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3249		    "applicable to mirror and replacing vdevs"));
3250		(void) zfs_error(hdl, EZFS_BADTARGET, msg);
3251		break;
3252
3253	case EBUSY:
3254		/*
3255		 * There are no other replicas of this device.
3256		 */
3257		(void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3258		break;
3259
3260	default:
3261		(void) zpool_standard_error(hdl, errno, msg);
3262	}
3263
3264	return (-1);
3265}
3266
3267/*
3268 * Find a mirror vdev in the source nvlist.
3269 *
3270 * The mchild array contains a list of disks in one of the top-level mirrors
3271 * of the source pool.  The schild array contains a list of disks that the
3272 * user specified on the command line.  We loop over the mchild array to
3273 * see if any entry in the schild array matches.
3274 *
3275 * If a disk in the mchild array is found in the schild array, we return
3276 * the index of that entry.  Otherwise we return -1.
3277 */
3278static int
3279find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3280    nvlist_t **schild, uint_t schildren)
3281{
3282	uint_t mc;
3283
3284	for (mc = 0; mc < mchildren; mc++) {
3285		uint_t sc;
3286		char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3287		    mchild[mc], 0);
3288
3289		for (sc = 0; sc < schildren; sc++) {
3290			char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3291			    schild[sc], 0);
3292			boolean_t result = (strcmp(mpath, spath) == 0);
3293
3294			free(spath);
3295			if (result) {
3296				free(mpath);
3297				return (mc);
3298			}
3299		}
3300
3301		free(mpath);
3302	}
3303
3304	return (-1);
3305}
3306
3307/*
3308 * Split a mirror pool.  If newroot points to null, then a new nvlist
3309 * is generated and it is the responsibility of the caller to free it.
3310 */
3311int
3312zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3313    nvlist_t *props, splitflags_t flags)
3314{
3315	zfs_cmd_t zc = { 0 };
3316	char msg[1024];
3317	nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3318	nvlist_t **varray = NULL, *zc_props = NULL;
3319	uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3320	libzfs_handle_t *hdl = zhp->zpool_hdl;
3321	uint64_t vers;
3322	boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3323	int retval = 0;
3324
3325	(void) snprintf(msg, sizeof (msg),
3326	    dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3327
3328	if (!zpool_name_valid(hdl, B_FALSE, newname))
3329		return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3330
3331	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3332		(void) fprintf(stderr, gettext("Internal error: unable to "
3333		    "retrieve pool configuration\n"));
3334		return (-1);
3335	}
3336
3337	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3338	    == 0);
3339	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3340
3341	if (props) {
3342		prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3343		if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3344		    props, vers, flags, msg)) == NULL)
3345			return (-1);
3346	}
3347
3348	if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3349	    &children) != 0) {
3350		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3351		    "Source pool is missing vdev tree"));
3352		nvlist_free(zc_props);
3353		return (-1);
3354	}
3355
3356	varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3357	vcount = 0;
3358
3359	if (*newroot == NULL ||
3360	    nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3361	    &newchild, &newchildren) != 0)
3362		newchildren = 0;
3363
3364	for (c = 0; c < children; c++) {
3365		uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3366		char *type;
3367		nvlist_t **mchild, *vdev;
3368		uint_t mchildren;
3369		int entry;
3370
3371		/*
3372		 * Unlike cache & spares, slogs are stored in the
3373		 * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
3374		 */
3375		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3376		    &is_log);
3377		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3378		    &is_hole);
3379		if (is_log || is_hole) {
3380			/*
3381			 * Create a hole vdev and put it in the config.
3382			 */
3383			if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3384				goto out;
3385			if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3386			    VDEV_TYPE_HOLE) != 0)
3387				goto out;
3388			if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3389			    1) != 0)
3390				goto out;
3391			if (lastlog == 0)
3392				lastlog = vcount;
3393			varray[vcount++] = vdev;
3394			continue;
3395		}
3396		lastlog = 0;
3397		verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3398		    == 0);
3399		if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3400			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3401			    "Source pool must be composed only of mirrors\n"));
3402			retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3403			goto out;
3404		}
3405
3406		verify(nvlist_lookup_nvlist_array(child[c],
3407		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3408
3409		/* find or add an entry for this top-level vdev */
3410		if (newchildren > 0 &&
3411		    (entry = find_vdev_entry(zhp, mchild, mchildren,
3412		    newchild, newchildren)) >= 0) {
3413			/* We found a disk that the user specified. */
3414			vdev = mchild[entry];
3415			++found;
3416		} else {
3417			/* User didn't specify a disk for this vdev. */
3418			vdev = mchild[mchildren - 1];
3419		}
3420
3421		if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3422			goto out;
3423	}
3424
3425	/* did we find every disk the user specified? */
3426	if (found != newchildren) {
3427		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3428		    "include at most one disk from each mirror"));
3429		retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3430		goto out;
3431	}
3432
3433	/* Prepare the nvlist for populating. */
3434	if (*newroot == NULL) {
3435		if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3436			goto out;
3437		freelist = B_TRUE;
3438		if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3439		    VDEV_TYPE_ROOT) != 0)
3440			goto out;
3441	} else {
3442		verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3443	}
3444
3445	/* Add all the children we found */
3446	if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3447	    lastlog == 0 ? vcount : lastlog) != 0)
3448		goto out;
3449
3450	/*
3451	 * If we're just doing a dry run, exit now with success.
3452	 */
3453	if (flags.dryrun) {
3454		memory_err = B_FALSE;
3455		freelist = B_FALSE;
3456		goto out;
3457	}
3458
3459	/* now build up the config list & call the ioctl */
3460	if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3461		goto out;
3462
3463	if (nvlist_add_nvlist(newconfig,
3464	    ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3465	    nvlist_add_string(newconfig,
3466	    ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3467	    nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3468		goto out;
3469
3470	/*
3471	 * The new pool is automatically part of the namespace unless we
3472	 * explicitly export it.
3473	 */
3474	if (!flags.import)
3475		zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3476	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3477	(void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3478	if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3479		goto out;
3480	if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3481		goto out;
3482
3483	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3484		retval = zpool_standard_error(hdl, errno, msg);
3485		goto out;
3486	}
3487
3488	freelist = B_FALSE;
3489	memory_err = B_FALSE;
3490
3491out:
3492	if (varray != NULL) {
3493		int v;
3494
3495		for (v = 0; v < vcount; v++)
3496			nvlist_free(varray[v]);
3497		free(varray);
3498	}
3499	zcmd_free_nvlists(&zc);
3500	nvlist_free(zc_props);
3501	nvlist_free(newconfig);
3502	if (freelist) {
3503		nvlist_free(*newroot);
3504		*newroot = NULL;
3505	}
3506
3507	if (retval != 0)
3508		return (retval);
3509
3510	if (memory_err)
3511		return (no_memory(hdl));
3512
3513	return (0);
3514}
3515
3516/*
3517 * Remove the given device.
3518 */
3519int
3520zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3521{
3522	zfs_cmd_t zc = { 0 };
3523	char msg[1024];
3524	nvlist_t *tgt;
3525	boolean_t avail_spare, l2cache, islog;
3526	libzfs_handle_t *hdl = zhp->zpool_hdl;
3527	uint64_t version;
3528
3529	(void) snprintf(msg, sizeof (msg),
3530	    dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3531
3532	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3533	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3534	    &islog)) == NULL)
3535		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3536
3537	version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3538	if (islog && version < SPA_VERSION_HOLES) {
3539		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3540		    "pool must be upgraded to support log removal"));
3541		return (zfs_error(hdl, EZFS_BADVERSION, msg));
3542	}
3543
3544	zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3545
3546	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3547		return (0);
3548
3549	switch (errno) {
3550
3551	case EINVAL:
3552		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3553		    "invalid config; all top-level vdevs must "
3554		    "have the same sector size and not be raidz."));
3555		(void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3556		break;
3557
3558	case EBUSY:
3559		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3560		    "Pool busy; removal may already be in progress"));
3561		(void) zfs_error(hdl, EZFS_BUSY, msg);
3562		break;
3563
3564	default:
3565		(void) zpool_standard_error(hdl, errno, msg);
3566	}
3567	return (-1);
3568}
3569
3570int
3571zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3572{
3573	zfs_cmd_t zc = { 0 };
3574	char msg[1024];
3575	libzfs_handle_t *hdl = zhp->zpool_hdl;
3576
3577	(void) snprintf(msg, sizeof (msg),
3578	    dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3579
3580	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3581	zc.zc_cookie = 1;
3582
3583	if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3584		return (0);
3585
3586	return (zpool_standard_error(hdl, errno, msg));
3587}
3588
3589int
3590zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3591    uint64_t *sizep)
3592{
3593	char msg[1024];
3594	nvlist_t *tgt;
3595	boolean_t avail_spare, l2cache, islog;
3596	libzfs_handle_t *hdl = zhp->zpool_hdl;
3597
3598	(void) snprintf(msg, sizeof (msg),
3599	    dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3600	    path);
3601
3602	if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3603	    &islog)) == NULL)
3604		return (zfs_error(hdl, EZFS_NODEVICE, msg));
3605
3606	if (avail_spare || l2cache || islog) {
3607		*sizep = 0;
3608		return (0);
3609	}
3610
3611	if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3612		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3613		    "indirect size not available"));
3614		return (zfs_error(hdl, EINVAL, msg));
3615	}
3616	return (0);
3617}
3618
3619/*
3620 * Clear the errors for the pool, or the particular device if specified.
3621 */
3622int
3623zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3624{
3625	zfs_cmd_t zc = { 0 };
3626	char msg[1024];
3627	nvlist_t *tgt;
3628	zpool_load_policy_t policy;
3629	boolean_t avail_spare, l2cache;
3630	libzfs_handle_t *hdl = zhp->zpool_hdl;
3631	nvlist_t *nvi = NULL;
3632	int error;
3633
3634	if (path)
3635		(void) snprintf(msg, sizeof (msg),
3636		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3637		    path);
3638	else
3639		(void) snprintf(msg, sizeof (msg),
3640		    dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3641		    zhp->zpool_name);
3642
3643	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3644	if (path) {
3645		if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3646		    &l2cache, NULL)) == NULL)
3647			return (zfs_error(hdl, EZFS_NODEVICE, msg));
3648
3649		/*
3650		 * Don't allow error clearing for hot spares.  Do allow
3651		 * error clearing for l2cache devices.
3652		 */
3653		if (avail_spare)
3654			return (zfs_error(hdl, EZFS_ISSPARE, msg));
3655
3656		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3657		    &zc.zc_guid) == 0);
3658	}
3659
3660	zpool_get_load_policy(rewindnvl, &policy);
3661	zc.zc_cookie = policy.zlp_rewind;
3662
3663	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3664		return (-1);
3665
3666	if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3667		return (-1);
3668
3669	while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3670	    errno == ENOMEM) {
3671		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3672			zcmd_free_nvlists(&zc);
3673			return (-1);
3674		}
3675	}
3676
3677	if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3678	    errno != EPERM && errno != EACCES)) {
3679		if (policy.zlp_rewind &
3680		    (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3681			(void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3682			zpool_rewind_exclaim(hdl, zc.zc_name,
3683			    ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3684			    nvi);
3685			nvlist_free(nvi);
3686		}
3687		zcmd_free_nvlists(&zc);
3688		return (0);
3689	}
3690
3691	zcmd_free_nvlists(&zc);
3692	return (zpool_standard_error(hdl, errno, msg));
3693}
3694
3695/*
3696 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3697 */
3698int
3699zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3700{
3701	zfs_cmd_t zc = { 0 };
3702	char msg[1024];
3703	libzfs_handle_t *hdl = zhp->zpool_hdl;
3704
3705	(void) snprintf(msg, sizeof (msg),
3706	    dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3707	    guid);
3708
3709	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3710	zc.zc_guid = guid;
3711	zc.zc_cookie = ZPOOL_NO_REWIND;
3712
3713	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3714		return (0);
3715
3716	return (zpool_standard_error(hdl, errno, msg));
3717}
3718
3719/*
3720 * Change the GUID for a pool.
3721 */
3722int
3723zpool_reguid(zpool_handle_t *zhp)
3724{
3725	char msg[1024];
3726	libzfs_handle_t *hdl = zhp->zpool_hdl;
3727	zfs_cmd_t zc = { 0 };
3728
3729	(void) snprintf(msg, sizeof (msg),
3730	    dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3731
3732	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3733	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3734		return (0);
3735
3736	return (zpool_standard_error(hdl, errno, msg));
3737}
3738
3739/*
3740 * Reopen the pool.
3741 */
3742int
3743zpool_reopen(zpool_handle_t *zhp)
3744{
3745	zfs_cmd_t zc = { 0 };
3746	char msg[1024];
3747	libzfs_handle_t *hdl = zhp->zpool_hdl;
3748
3749	(void) snprintf(msg, sizeof (msg),
3750	    dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3751	    zhp->zpool_name);
3752
3753	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3754	if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3755		return (0);
3756	return (zpool_standard_error(hdl, errno, msg));
3757}
3758
3759/* call into libzfs_core to execute the sync IOCTL per pool */
3760int
3761zpool_sync_one(zpool_handle_t *zhp, void *data)
3762{
3763	int ret;
3764	libzfs_handle_t *hdl = zpool_get_handle(zhp);
3765	const char *pool_name = zpool_get_name(zhp);
3766	boolean_t *force = data;
3767	nvlist_t *innvl = fnvlist_alloc();
3768
3769	fnvlist_add_boolean_value(innvl, "force", *force);
3770	if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3771		nvlist_free(innvl);
3772		return (zpool_standard_error_fmt(hdl, ret,
3773		    dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3774	}
3775	nvlist_free(innvl);
3776
3777	return (0);
3778}
3779
3780/*
3781 * Convert from a devid string to a path.
3782 */
3783static char *
3784devid_to_path(char *devid_str)
3785{
3786	ddi_devid_t devid;
3787	char *minor;
3788	char *path;
3789	devid_nmlist_t *list = NULL;
3790	int ret;
3791
3792	if (devid_str_decode(devid_str, &devid, &minor) != 0)
3793		return (NULL);
3794
3795	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3796
3797	devid_str_free(minor);
3798	devid_free(devid);
3799
3800	if (ret != 0)
3801		return (NULL);
3802
3803	/*
3804	 * In a case the strdup() fails, we will just return NULL below.
3805	 */
3806	path = strdup(list[0].devname);
3807
3808	devid_free_nmlist(list);
3809
3810	return (path);
3811}
3812
3813/*
3814 * Convert from a path to a devid string.
3815 */
3816static char *
3817path_to_devid(const char *path)
3818{
3819	int fd;
3820	ddi_devid_t devid;
3821	char *minor, *ret;
3822
3823	if ((fd = open(path, O_RDONLY)) < 0)
3824		return (NULL);
3825
3826	minor = NULL;
3827	ret = NULL;
3828	if (devid_get(fd, &devid) == 0) {
3829		if (devid_get_minor_name(fd, &minor) == 0)
3830			ret = devid_str_encode(devid, minor);
3831		if (minor != NULL)
3832			devid_str_free(minor);
3833		devid_free(devid);
3834	}
3835	(void) close(fd);
3836
3837	return (ret);
3838}
3839
3840struct path_from_physpath_walker_args {
3841	char *pfpwa_path;
3842};
3843
3844/*
3845 * Walker for use with di_devlink_walk().  Stores the "/dev" path of the first
3846 * primary devlink (i.e., the first devlink which refers to our "/devices"
3847 * node) and stops walking.
3848 */
3849static int
3850path_from_physpath_walker(di_devlink_t devlink, void *arg)
3851{
3852	struct path_from_physpath_walker_args *pfpwa = arg;
3853
3854	if (di_devlink_type(devlink) != DI_PRIMARY_LINK) {
3855		return (DI_WALK_CONTINUE);
3856	}
3857
3858	verify(pfpwa->pfpwa_path == NULL);
3859	if ((pfpwa->pfpwa_path = strdup(di_devlink_path(devlink))) != NULL) {
3860		return (DI_WALK_TERMINATE);
3861	}
3862
3863	return (DI_WALK_CONTINUE);
3864}
3865
3866/*
3867 * Search for a "/dev" path that refers to our physical path.  Returns the new
3868 * path if one is found and it does not match the existing "path" value.  If
3869 * the value is unchanged, or one could not be found, returns NULL.
3870 */
3871static char *
3872path_from_physpath(libzfs_handle_t *hdl, const char *path,
3873    const char *physpath)
3874{
3875	struct path_from_physpath_walker_args pfpwa;
3876
3877	if (physpath == NULL) {
3878		return (NULL);
3879	}
3880
3881	if (hdl->libzfs_devlink == NULL) {
3882		if ((hdl->libzfs_devlink = di_devlink_init(NULL, 0)) ==
3883		    DI_LINK_NIL) {
3884			/*
3885			 * We may not be able to open a handle if this process
3886			 * is insufficiently privileged, or we are too early in
3887			 * boot for devfsadm to be ready.  Ignore this error
3888			 * and defer the path check to a subsequent run.
3889			 */
3890			return (NULL);
3891		}
3892	}
3893
3894	pfpwa.pfpwa_path = NULL;
3895	(void) di_devlink_walk(hdl->libzfs_devlink, NULL, physpath,
3896	    DI_PRIMARY_LINK, &pfpwa, path_from_physpath_walker);
3897
3898	if (path != NULL && pfpwa.pfpwa_path != NULL &&
3899	    strcmp(path, pfpwa.pfpwa_path) == 0) {
3900		/*
3901		 * If the path is already correct, no change is required.
3902		 */
3903		free(pfpwa.pfpwa_path);
3904		return (NULL);
3905	}
3906
3907	return (pfpwa.pfpwa_path);
3908}
3909
3910/*
3911 * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3912 * ignore any failure here, since a common case is for an unprivileged user to
3913 * type 'zpool status', and we'll display the correct information anyway.
3914 */
3915static void
3916set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3917{
3918	zfs_cmd_t zc = { 0 };
3919
3920	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3921	(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3922	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3923	    &zc.zc_guid) == 0);
3924
3925	(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3926}
3927
3928/*
3929 * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3930 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3931 * We also check if this is a whole disk, in which case we strip off the
3932 * trailing 's0' slice name.
3933 *
3934 * This routine is also responsible for identifying when disks have been
3935 * reconfigured in a new location.  The kernel will have opened the device by
3936 * devid, but the path will still refer to the old location.  To catch this, we
3937 * first do a path -> devid translation (which is fast for the common case).  If
3938 * the devid matches, we're done.  If not, we do a reverse devid -> path
3939 * translation and issue the appropriate ioctl() to update the path of the vdev.
3940 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3941 * of these checks.
3942 */
3943char *
3944zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3945    int name_flags)
3946{
3947	char *path, *env;
3948	uint64_t value;
3949	char buf[64];
3950
3951	env = getenv("ZPOOL_VDEV_NAME_PATH");
3952	if (env && (strtoul(env, NULL, 0) > 0 ||
3953	    !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3954		name_flags |= VDEV_NAME_PATH;
3955
3956	env = getenv("ZPOOL_VDEV_NAME_GUID");
3957	if (env && (strtoul(env, NULL, 0) > 0 ||
3958	    !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3959		name_flags |= VDEV_NAME_GUID;
3960
3961	env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3962	if (env && (strtoul(env, NULL, 0) > 0 ||
3963	    !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3964		name_flags |= VDEV_NAME_FOLLOW_LINKS;
3965
3966	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3967	    name_flags & VDEV_NAME_GUID) {
3968		nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3969		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3970		path = buf;
3971	} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3972		vdev_stat_t *vs;
3973		uint_t vsc;
3974		char *newpath = NULL;
3975		char *physpath = NULL;
3976		char *devid = NULL;
3977
3978		/*
3979		 * If the device is dead (faulted, offline, etc) then don't
3980		 * bother opening it.  Otherwise we may be forcing the user to
3981		 * open a misbehaving device, which can have undesirable
3982		 * effects.
3983		 */
3984		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3985		    (uint64_t **)&vs, &vsc) != 0 ||
3986		    vs->vs_state < VDEV_STATE_DEGRADED ||
3987		    zhp == NULL) {
3988			goto after_open;
3989		}
3990
3991		if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3992			/*
3993			 * This vdev has a devid.  We can use it to check the
3994			 * current path.
3995			 */
3996			char *newdevid = path_to_devid(path);
3997
3998			if (newdevid == NULL || strcmp(devid, newdevid) != 0) {
3999				newpath = devid_to_path(devid);
4000			}
4001
4002			if (newdevid != NULL)
4003				devid_str_free(newdevid);
4004
4005		} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4006		    &physpath) == 0) {
4007			/*
4008			 * This vdev does not have a devid, but it does have a
4009			 * physical path.  Attempt to translate this to a /dev
4010			 * path.
4011			 */
4012			newpath = path_from_physpath(hdl, path, physpath);
4013		}
4014
4015		if (newpath != NULL) {
4016			/*
4017			 * Update the path appropriately.
4018			 */
4019			set_path(zhp, nv, newpath);
4020			if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
4021			    newpath) == 0) {
4022				verify(nvlist_lookup_string(nv,
4023				    ZPOOL_CONFIG_PATH, &path) == 0);
4024			}
4025			free(newpath);
4026		}
4027
4028		if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4029			char *rp = realpath(path, NULL);
4030			if (rp) {
4031				strlcpy(buf, rp, sizeof (buf));
4032				path = buf;
4033				free(rp);
4034			}
4035		}
4036
4037after_open:
4038		if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
4039			path += strlen(ZFS_DISK_ROOTD);
4040
4041		/*
4042		 * Remove the partition from the path it this is a whole disk.
4043		 */
4044		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4045		    == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
4046			int pathlen = strlen(path);
4047			char *tmp = zfs_strdup(hdl, path);
4048
4049			/*
4050			 * If it starts with c#, and ends with "s0" or "s1",
4051			 * chop the slice off, or if it ends with "s0/old" or
4052			 * "s1/old", remove the slice from the middle.
4053			 */
4054			if (CTD_CHECK(tmp)) {
4055				if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
4056				    strcmp(&tmp[pathlen - 2], "s1") == 0) {
4057					tmp[pathlen - 2] = '\0';
4058				} else if (pathlen > 6 &&
4059				    (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
4060				    strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
4061					(void) strcpy(&tmp[pathlen - 6],
4062					    "/old");
4063				}
4064			}
4065			return (tmp);
4066		}
4067	} else {
4068		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
4069
4070		/*
4071		 * If it's a raidz device, we need to stick in the parity level.
4072		 */
4073		if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
4074			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4075			    &value) == 0);
4076			(void) snprintf(buf, sizeof (buf), "%s%llu", path,
4077			    (u_longlong_t)value);
4078			path = buf;
4079		}
4080
4081		/*
4082		 * We identify each top-level vdev by using a <type-id>
4083		 * naming convention.
4084		 */
4085		if (name_flags & VDEV_NAME_TYPE_ID) {
4086			uint64_t id;
4087
4088			verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
4089			    &id) == 0);
4090			(void) snprintf(buf, sizeof (buf), "%s-%llu", path,
4091			    (u_longlong_t)id);
4092			path = buf;
4093		}
4094	}
4095
4096	return (zfs_strdup(hdl, path));
4097}
4098
4099static int
4100zbookmark_mem_compare(const void *a, const void *b)
4101{
4102	return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4103}
4104
4105/*
4106 * Retrieve the persistent error log, uniquify the members, and return to the
4107 * caller.
4108 */
4109int
4110zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4111{
4112	zfs_cmd_t zc = { 0 };
4113	uint64_t count;
4114	zbookmark_phys_t *zb = NULL;
4115	int i;
4116
4117	/*
4118	 * Retrieve the raw error list from the kernel.  If the number of errors
4119	 * has increased, allocate more space and continue until we get the
4120	 * entire list.
4121	 */
4122	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4123	    &count) == 0);
4124	if (count == 0)
4125		return (0);
4126	if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4127	    count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
4128		return (-1);
4129	zc.zc_nvlist_dst_size = count;
4130	(void) strcpy(zc.zc_name, zhp->zpool_name);
4131	for (;;) {
4132		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
4133		    &zc) != 0) {
4134			free((void *)(uintptr_t)zc.zc_nvlist_dst);
4135			if (errno == ENOMEM) {
4136				void *dst;
4137
4138				count = zc.zc_nvlist_dst_size;
4139				dst = zfs_alloc(zhp->zpool_hdl, count *
4140				    sizeof (zbookmark_phys_t));
4141				if (dst == NULL)
4142					return (-1);
4143				zc.zc_nvlist_dst = (uintptr_t)dst;
4144			} else {
4145				return (-1);
4146			}
4147		} else {
4148			break;
4149		}
4150	}
4151
4152	/*
4153	 * Sort the resulting bookmarks.  This is a little confusing due to the
4154	 * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
4155	 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
4156	 * _not_ copied as part of the process.  So we point the start of our
4157	 * array appropriate and decrement the total number of elements.
4158	 */
4159	zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4160	    zc.zc_nvlist_dst_size;
4161	count -= zc.zc_nvlist_dst_size;
4162
4163	qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4164
4165	verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4166
4167	/*
4168	 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4169	 */
4170	for (i = 0; i < count; i++) {
4171		nvlist_t *nv;
4172
4173		/* ignoring zb_blkid and zb_level for now */
4174		if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4175		    zb[i-1].zb_object == zb[i].zb_object)
4176			continue;
4177
4178		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4179			goto nomem;
4180		if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4181		    zb[i].zb_objset) != 0) {
4182			nvlist_free(nv);
4183			goto nomem;
4184		}
4185		if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4186		    zb[i].zb_object) != 0) {
4187			nvlist_free(nv);
4188			goto nomem;
4189		}
4190		if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4191			nvlist_free(nv);
4192			goto nomem;
4193		}
4194		nvlist_free(nv);
4195	}
4196
4197	free((void *)(uintptr_t)zc.zc_nvlist_dst);
4198	return (0);
4199
4200nomem:
4201	free((void *)(uintptr_t)zc.zc_nvlist_dst);
4202	return (no_memory(zhp->zpool_hdl));
4203}
4204
4205/*
4206 * Upgrade a ZFS pool to the latest on-disk version.
4207 */
4208int
4209zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4210{
4211	zfs_cmd_t zc = { 0 };
4212	libzfs_handle_t *hdl = zhp->zpool_hdl;
4213
4214	(void) strcpy(zc.zc_name, zhp->zpool_name);
4215	zc.zc_cookie = new_version;
4216
4217	if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4218		return (zpool_standard_error_fmt(hdl, errno,
4219		    dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4220		    zhp->zpool_name));
4221	return (0);
4222}
4223
4224void
4225zfs_save_arguments(int argc, char **argv, char *string, int len)
4226{
4227	(void) strlcpy(string, basename(argv[0]), len);
4228	for (int i = 1; i < argc; i++) {
4229		(void) strlcat(string, " ", len);
4230		(void) strlcat(string, argv[i], len);
4231	}
4232}
4233
4234int
4235zpool_log_history(libzfs_handle_t *hdl, const char *message)
4236{
4237	zfs_cmd_t zc = { 0 };
4238	nvlist_t *args;
4239	int err;
4240
4241	args = fnvlist_alloc();
4242	fnvlist_add_string(args, "message", message);
4243	err = zcmd_write_src_nvlist(hdl, &zc, args);
4244	if (err == 0)
4245		err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
4246	nvlist_free(args);
4247	zcmd_free_nvlists(&zc);
4248	return (err);
4249}
4250
4251/*
4252 * Perform ioctl to get some command history of a pool.
4253 *
4254 * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
4255 * logical offset of the history buffer to start reading from.
4256 *
4257 * Upon return, 'off' is the next logical offset to read from and
4258 * 'len' is the actual amount of bytes read into 'buf'.
4259 */
4260static int
4261get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4262{
4263	zfs_cmd_t zc = { 0 };
4264	libzfs_handle_t *hdl = zhp->zpool_hdl;
4265
4266	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4267
4268	zc.zc_history = (uint64_t)(uintptr_t)buf;
4269	zc.zc_history_len = *len;
4270	zc.zc_history_offset = *off;
4271
4272	if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4273		switch (errno) {
4274		case EPERM:
4275			return (zfs_error_fmt(hdl, EZFS_PERM,
4276			    dgettext(TEXT_DOMAIN,
4277			    "cannot show history for pool '%s'"),
4278			    zhp->zpool_name));
4279		case ENOENT:
4280			return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4281			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
4282			    "'%s'"), zhp->zpool_name));
4283		case ENOTSUP:
4284			return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4285			    dgettext(TEXT_DOMAIN, "cannot get history for pool "
4286			    "'%s', pool must be upgraded"), zhp->zpool_name));
4287		default:
4288			return (zpool_standard_error_fmt(hdl, errno,
4289			    dgettext(TEXT_DOMAIN,
4290			    "cannot get history for '%s'"), zhp->zpool_name));
4291		}
4292	}
4293
4294	*len = zc.zc_history_len;
4295	*off = zc.zc_history_offset;
4296
4297	return (0);
4298}
4299
4300/*
4301 * Process the buffer of nvlists, unpacking and storing each nvlist record
4302 * into 'records'.  'leftover' is set to the number of bytes that weren't
4303 * processed as there wasn't a complete record.
4304 */
4305int
4306zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
4307    nvlist_t ***records, uint_t *numrecords)
4308{
4309	uint64_t reclen;
4310	nvlist_t *nv;
4311	int i;
4312
4313	while (bytes_read > sizeof (reclen)) {
4314
4315		/* get length of packed record (stored as little endian) */
4316		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
4317			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
4318
4319		if (bytes_read < sizeof (reclen) + reclen)
4320			break;
4321
4322		/* unpack record */
4323		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
4324			return (ENOMEM);
4325		bytes_read -= sizeof (reclen) + reclen;
4326		buf += sizeof (reclen) + reclen;
4327
4328		/* add record to nvlist array */
4329		(*numrecords)++;
4330		if (ISP2(*numrecords + 1)) {
4331			*records = realloc(*records,
4332			    *numrecords * 2 * sizeof (nvlist_t *));
4333		}
4334		(*records)[*numrecords - 1] = nv;
4335	}
4336
4337	*leftover = bytes_read;
4338	return (0);
4339}
4340
4341/*
4342 * Retrieve the command history of a pool.
4343 */
4344int
4345zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4346{
4347	char *buf;
4348	int buflen = 128 * 1024;
4349	uint64_t off = 0;
4350	nvlist_t **records = NULL;
4351	uint_t numrecords = 0;
4352	int err, i;
4353
4354	buf = malloc(buflen);
4355	if (buf == NULL)
4356		return (ENOMEM);
4357	do {
4358		uint64_t bytes_read = buflen;
4359		uint64_t leftover;
4360
4361		if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4362			break;
4363
4364		/* if nothing else was read in, we're at EOF, just return */
4365		if (!bytes_read)
4366			break;
4367
4368		if ((err = zpool_history_unpack(buf, bytes_read,
4369		    &leftover, &records, &numrecords)) != 0)
4370			break;
4371		off -= leftover;
4372		if (leftover == bytes_read) {
4373			/*
4374			 * no progress made, because buffer is not big enough
4375			 * to hold this record; resize and retry.
4376			 */
4377			buflen *= 2;
4378			free(buf);
4379			buf = malloc(buflen);
4380			if (buf == NULL)
4381				return (ENOMEM);
4382		}
4383
4384		/* CONSTCOND */
4385	} while (1);
4386
4387	free(buf);
4388
4389	if (!err) {
4390		verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4391		verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4392		    records, numrecords) == 0);
4393	}
4394	for (i = 0; i < numrecords; i++)
4395		nvlist_free(records[i]);
4396	free(records);
4397
4398	return (err);
4399}
4400
4401void
4402zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4403    char *pathname, size_t len)
4404{
4405	zfs_cmd_t zc = { 0 };
4406	boolean_t mounted = B_FALSE;
4407	char *mntpnt = NULL;
4408	char dsname[ZFS_MAX_DATASET_NAME_LEN];
4409
4410	if (dsobj == 0) {
4411		/* special case for the MOS */
4412		(void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
4413		return;
4414	}
4415
4416	/* get the dataset's name */
4417	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4418	zc.zc_obj = dsobj;
4419	if (ioctl(zhp->zpool_hdl->libzfs_fd,
4420	    ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4421		/* just write out a path of two object numbers */
4422		(void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4423		    dsobj, obj);
4424		return;
4425	}
4426	(void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4427
4428	/* find out if the dataset is mounted */
4429	mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4430
4431	/* get the corrupted object's path */
4432	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4433	zc.zc_obj = obj;
4434	if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4435	    &zc) == 0) {
4436		if (mounted) {
4437			(void) snprintf(pathname, len, "%s%s", mntpnt,
4438			    zc.zc_value);
4439		} else {
4440			(void) snprintf(pathname, len, "%s:%s",
4441			    dsname, zc.zc_value);
4442		}
4443	} else {
4444		(void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
4445	}
4446	free(mntpnt);
4447}
4448
4449/*
4450 * Read the EFI label from the config, if a label does not exist then
4451 * pass back the error to the caller. If the caller has passed a non-NULL
4452 * diskaddr argument then we set it to the starting address of the EFI
4453 * partition. If the caller has passed a non-NULL boolean argument, then
4454 * we set it to indicate if the disk does have efi system partition.
4455 */
4456static int
4457read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
4458{
4459	char *path;
4460	int fd;
4461	char diskname[MAXPATHLEN];
4462	boolean_t boot = B_FALSE;
4463	int err = -1;
4464	int slice;
4465
4466	if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4467		return (err);
4468
4469	(void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
4470	    strrchr(path, '/'));
4471	if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
4472		struct dk_gpt *vtoc;
4473
4474		if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4475			for (slice = 0; slice < vtoc->efi_nparts; slice++) {
4476				if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
4477					boot = B_TRUE;
4478				if (vtoc->efi_parts[slice].p_tag == V_USR)
4479					break;
4480			}
4481			if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
4482				*sb = vtoc->efi_parts[slice].p_start;
4483			if (system != NULL)
4484				*system = boot;
4485			efi_free(vtoc);
4486		}
4487		(void) close(fd);
4488	}
4489	return (err);
4490}
4491
4492/*
4493 * determine where a partition starts on a disk in the current
4494 * configuration
4495 */
4496static diskaddr_t
4497find_start_block(nvlist_t *config)
4498{
4499	nvlist_t **child;
4500	uint_t c, children;
4501	diskaddr_t sb = MAXOFFSET_T;
4502	uint64_t wholedisk;
4503
4504	if (nvlist_lookup_nvlist_array(config,
4505	    ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4506		if (nvlist_lookup_uint64(config,
4507		    ZPOOL_CONFIG_WHOLE_DISK,
4508		    &wholedisk) != 0 || !wholedisk) {
4509			return (MAXOFFSET_T);
4510		}
4511		if (read_efi_label(config, &sb, NULL) < 0)
4512			sb = MAXOFFSET_T;
4513		return (sb);
4514	}
4515
4516	for (c = 0; c < children; c++) {
4517		sb = find_start_block(child[c]);
4518		if (sb != MAXOFFSET_T) {
4519			return (sb);
4520		}
4521	}
4522	return (MAXOFFSET_T);
4523}
4524
4525/*
4526 * Label an individual disk.  The name provided is the short name,
4527 * stripped of any leading /dev path.
4528 */
4529int
4530zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4531    zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4532{
4533	char path[MAXPATHLEN];
4534	struct dk_gpt *vtoc;
4535	int fd;
4536	size_t resv;
4537	uint64_t slice_size;
4538	diskaddr_t start_block;
4539	char errbuf[1024];
4540
4541	/* prepare an error message just in case */
4542	(void) snprintf(errbuf, sizeof (errbuf),
4543	    dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4544
4545	if (zhp) {
4546		nvlist_t *nvroot;
4547
4548		verify(nvlist_lookup_nvlist(zhp->zpool_config,
4549		    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4550
4551		if (zhp->zpool_start_block == 0)
4552			start_block = find_start_block(nvroot);
4553		else
4554			start_block = zhp->zpool_start_block;
4555		zhp->zpool_start_block = start_block;
4556	} else {
4557		/* new pool */
4558		start_block = NEW_START_BLOCK;
4559	}
4560
4561	(void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4562	    BACKUP_SLICE);
4563
4564	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4565		/*
4566		 * This shouldn't happen.  We've long since verified that this
4567		 * is a valid device.
4568		 */
4569		zfs_error_aux(hdl,
4570		    dgettext(TEXT_DOMAIN, "unable to open device"));
4571		return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4572	}
4573
4574	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4575		/*
4576		 * The only way this can fail is if we run out of memory, or we
4577		 * were unable to read the disk's capacity
4578		 */
4579		if (errno == ENOMEM)
4580			(void) no_memory(hdl);
4581
4582		(void) close(fd);
4583		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4584		    "unable to read disk capacity"), name);
4585
4586		return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4587	}
4588	resv = efi_reserved_sectors(vtoc);
4589
4590	/*
4591	 * Why we use V_USR: V_BACKUP confuses users, and is considered
4592	 * disposable by some EFI utilities (since EFI doesn't have a backup
4593	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
4594	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
4595	 * etc. were all pretty specific.  V_USR is as close to reality as we
4596	 * can get, in the absence of V_OTHER.
4597	 */
4598	/* first fix the partition start block */
4599	if (start_block == MAXOFFSET_T)
4600		start_block = NEW_START_BLOCK;
4601
4602	/*
4603	 * EFI System partition is using slice 0.
4604	 * ZFS is on slice 1 and slice 8 is reserved.
4605	 * We assume the GPT partition table without system
4606	 * partition has zfs p_start == NEW_START_BLOCK.
4607	 * If start_block != NEW_START_BLOCK, it means we have
4608	 * system partition. Correct solution would be to query/cache vtoc
4609	 * from existing vdev member.
4610	 */
4611	if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4612		if (boot_size % vtoc->efi_lbasize != 0) {
4613			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4614			    "boot partition size must be a multiple of %d"),
4615			    vtoc->efi_lbasize);
4616			(void) close(fd);
4617			efi_free(vtoc);
4618			return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4619		}
4620		/*
4621		 * System partition size checks.
4622		 * Note the 1MB is quite arbitrary value, since we
4623		 * are creating dedicated pool, it should be enough
4624		 * to hold fat + efi bootloader. May need to be
4625		 * adjusted if the bootloader size will grow.
4626		 */
4627		if (boot_size < 1024 * 1024) {
4628			char buf[64];
4629			zfs_nicenum(boot_size, buf, sizeof (buf));
4630			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4631			    "Specified size %s for EFI System partition is too "
4632			    "small, the minimum size is 1MB."), buf);
4633			(void) close(fd);
4634			efi_free(vtoc);
4635			return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4636		}
4637		/* 33MB is tested with mkfs -F pcfs */
4638		if (hdl->libzfs_printerr &&
4639		    ((vtoc->efi_lbasize == 512 &&
4640		    boot_size < 33 * 1024 * 1024) ||
4641		    (vtoc->efi_lbasize == 4096 &&
4642		    boot_size < 256 * 1024 * 1024)))  {
4643			char buf[64];
4644			zfs_nicenum(boot_size, buf, sizeof (buf));
4645			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4646			    "Warning: EFI System partition size %s is "
4647			    "not allowing to create FAT32 file\nsystem, which "
4648			    "may result in unbootable system.\n"), buf);
4649		}
4650		/* Adjust zfs partition start by size of system partition. */
4651		start_block += boot_size / vtoc->efi_lbasize;
4652	}
4653
4654	if (start_block == NEW_START_BLOCK) {
4655		/*
4656		 * Use default layout.
4657		 * ZFS is on slice 0 and slice 8 is reserved.
4658		 */
4659		slice_size = vtoc->efi_last_u_lba + 1;
4660		slice_size -= resv;
4661		slice_size -= start_block;
4662		if (slice != NULL)
4663			*slice = 0;
4664
4665		vtoc->efi_parts[0].p_start = start_block;
4666		vtoc->efi_parts[0].p_size = slice_size;
4667
4668		vtoc->efi_parts[0].p_tag = V_USR;
4669		(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4670
4671		vtoc->efi_parts[8].p_start = slice_size + start_block;
4672		vtoc->efi_parts[8].p_size = resv;
4673		vtoc->efi_parts[8].p_tag = V_RESERVED;
4674	} else {
4675		slice_size = start_block - NEW_START_BLOCK;
4676		vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4677		vtoc->efi_parts[0].p_size = slice_size;
4678		vtoc->efi_parts[0].p_tag = V_SYSTEM;
4679		(void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4680		if (slice != NULL)
4681			*slice = 1;
4682		/* prepare slice 1 */
4683		slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4684		slice_size -= resv;
4685		slice_size -= NEW_START_BLOCK;
4686		vtoc->efi_parts[1].p_start = start_block;
4687		vtoc->efi_parts[1].p_size = slice_size;
4688		vtoc->efi_parts[1].p_tag = V_USR;
4689		(void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4690
4691		vtoc->efi_parts[8].p_start = slice_size + start_block;
4692		vtoc->efi_parts[8].p_size = resv;
4693		vtoc->efi_parts[8].p_tag = V_RESERVED;
4694	}
4695
4696	if (efi_write(fd, vtoc) != 0) {
4697		/*
4698		 * Some block drivers (like pcata) may not support EFI
4699		 * GPT labels.  Print out a helpful error message dir-
4700		 * ecting the user to manually label the disk and give
4701		 * a specific slice.
4702		 */
4703		(void) close(fd);
4704		efi_free(vtoc);
4705
4706		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4707		    "try using fdisk(1M) and then provide a specific slice"));
4708		return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4709	}
4710
4711	(void) close(fd);
4712	efi_free(vtoc);
4713	return (0);
4714}
4715
4716static boolean_t
4717supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4718{
4719	char *type;
4720	nvlist_t **child;
4721	uint_t children, c;
4722
4723	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4724	if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4725	    strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4726	    strcmp(type, VDEV_TYPE_MISSING) == 0) {
4727		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4728		    "vdev type '%s' is not supported"), type);
4729		(void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4730		return (B_FALSE);
4731	}
4732	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4733	    &child, &children) == 0) {
4734		for (c = 0; c < children; c++) {
4735			if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4736				return (B_FALSE);
4737		}
4738	}
4739	return (B_TRUE);
4740}
4741
4742/*
4743 * Check if this zvol is allowable for use as a dump device; zero if
4744 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4745 *
4746 * Allowable storage configurations include mirrors, all raidz variants, and
4747 * pools with log, cache, and spare devices.  Pools which are backed by files or
4748 * have missing/hole vdevs are not suitable.
4749 */
4750int
4751zvol_check_dump_config(char *arg)
4752{
4753	zpool_handle_t *zhp = NULL;
4754	nvlist_t *config, *nvroot;
4755	char *p, *volname;
4756	nvlist_t **top;
4757	uint_t toplevels;
4758	libzfs_handle_t *hdl;
4759	char errbuf[1024];
4760	char poolname[ZFS_MAX_DATASET_NAME_LEN];
4761	int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4762	int ret = 1;
4763
4764	if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4765		return (-1);
4766	}
4767
4768	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4769	    "dump is not supported on device '%s'"), arg);
4770
4771	if ((hdl = libzfs_init()) == NULL)
4772		return (1);
4773	libzfs_print_on_error(hdl, B_TRUE);
4774
4775	volname = arg + pathlen;
4776
4777	/* check the configuration of the pool */
4778	if ((p = strchr(volname, '/')) == NULL) {
4779		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4780		    "malformed dataset name"));
4781		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4782		return (1);
4783	} else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4784		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4785		    "dataset name is too long"));
4786		(void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4787		return (1);
4788	} else {
4789		(void) strncpy(poolname, volname, p - volname);
4790		poolname[p - volname] = '\0';
4791	}
4792
4793	if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4794		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4795		    "could not open pool '%s'"), poolname);
4796		(void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4797		goto out;
4798	}
4799	config = zpool_get_config(zhp, NULL);
4800	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4801	    &nvroot) != 0) {
4802		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4803		    "could not obtain vdev configuration for  '%s'"), poolname);
4804		(void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4805		goto out;
4806	}
4807
4808	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4809	    &top, &toplevels) == 0);
4810
4811	if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4812		goto out;
4813	}
4814	ret = 0;
4815
4816out:
4817	if (zhp)
4818		zpool_close(zhp);
4819	libzfs_fini(hdl);
4820	return (ret);
4821}
4822