xref: /titanic_50/usr/src/lib/libzfs/common/libzfs_pool.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <assert.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <devid.h>
33 #include <fcntl.h>
34 #include <libintl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/zfs_ioctl.h>
40 
41 #include "zfs_namecheck.h"
42 #include "libzfs_impl.h"
43 
44 /*
45  * Validate the given pool name, optionally putting an extended error message in
46  * 'buf'.
47  */
48 static int
49 zpool_name_valid(const char *pool, char *buf, size_t buflen)
50 {
51 	namecheck_err_t why;
52 	char what;
53 
54 	if (pool_namecheck(pool, &why, &what) != 0) {
55 		if (buf != NULL) {
56 			switch (why) {
57 			case NAME_ERR_TOOLONG:
58 				(void) snprintf(buf, buflen,
59 				    dgettext(TEXT_DOMAIN, "name is too long"));
60 				break;
61 
62 			case NAME_ERR_INVALCHAR:
63 				(void) snprintf(buf, buflen,
64 				    dgettext(TEXT_DOMAIN, "invalid character "
65 				    "'%c' in pool name"), what);
66 				break;
67 
68 			case NAME_ERR_NOLETTER:
69 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
70 				    "name must begin with a letter"), buflen);
71 				break;
72 
73 			case NAME_ERR_RESERVED:
74 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
75 				    "name is reserved\n"
76 				    "pool name may have been omitted"), buflen);
77 				break;
78 
79 			case NAME_ERR_DISKLIKE:
80 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
81 				    "pool name is reserved\n"
82 				    "pool name may have been omitted"), buflen);
83 				break;
84 			}
85 		}
86 		return (FALSE);
87 	}
88 
89 	return (TRUE);
90 }
91 
92 /*
93  * Set the pool-wide health based on the vdev state of the root vdev.
94  */
95 void
96 set_pool_health(nvlist_t *config)
97 {
98 	nvlist_t *nvroot;
99 	vdev_stat_t *vs;
100 	uint_t vsc;
101 	char *health;
102 
103 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
104 	    &nvroot) == 0);
105 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
106 	    (uint64_t **)&vs, &vsc) == 0);
107 
108 	switch (vs->vs_state) {
109 
110 	case VDEV_STATE_CLOSED:
111 	case VDEV_STATE_CANT_OPEN:
112 	case VDEV_STATE_OFFLINE:
113 		health = dgettext(TEXT_DOMAIN, "FAULTED");
114 		break;
115 
116 	case VDEV_STATE_DEGRADED:
117 		health = dgettext(TEXT_DOMAIN, "DEGRADED");
118 		break;
119 
120 	case VDEV_STATE_HEALTHY:
121 		health = dgettext(TEXT_DOMAIN, "ONLINE");
122 		break;
123 
124 	default:
125 		zfs_baderror(vs->vs_state);
126 	}
127 
128 	verify(nvlist_add_string(config, ZPOOL_CONFIG_POOL_HEALTH,
129 	    health) == 0);
130 }
131 
132 /*
133  * Open a handle to the given pool, even if the pool is currently in the FAULTED
134  * state.
135  */
136 zpool_handle_t *
137 zpool_open_canfail(const char *pool)
138 {
139 	zpool_handle_t *zhp;
140 	int error;
141 
142 	/*
143 	 * Make sure the pool name is valid.
144 	 */
145 	if (!zpool_name_valid(pool, NULL, 0)) {
146 		zfs_error(dgettext(TEXT_DOMAIN, "cannot open '%s': invalid "
147 		    "pool name"), pool);
148 		return (NULL);
149 	}
150 
151 	zhp = zfs_malloc(sizeof (zpool_handle_t));
152 
153 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
154 
155 	if ((error = zpool_refresh_stats(zhp)) != 0) {
156 		if (error == ENOENT || error == EINVAL) {
157 			zfs_error(dgettext(TEXT_DOMAIN, "cannot open '%s': no "
158 			    "such pool"), pool);
159 			free(zhp);
160 			return (NULL);
161 		} else {
162 			zhp->zpool_state = POOL_STATE_UNAVAIL;
163 		}
164 	} else {
165 		zhp->zpool_state = POOL_STATE_ACTIVE;
166 	}
167 
168 	return (zhp);
169 }
170 
171 /*
172  * Like the above, but silent on error.  Used when iterating over pools (because
173  * the configuration cache may be out of date).
174  */
175 zpool_handle_t *
176 zpool_open_silent(const char *pool)
177 {
178 	zpool_handle_t *zhp;
179 	int error;
180 
181 	zhp = zfs_malloc(sizeof (zpool_handle_t));
182 
183 	(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
184 
185 	if ((error = zpool_refresh_stats(zhp)) != 0) {
186 		if (error == ENOENT || error == EINVAL) {
187 			free(zhp);
188 			return (NULL);
189 		} else {
190 			zhp->zpool_state = POOL_STATE_UNAVAIL;
191 		}
192 	} else {
193 		zhp->zpool_state = POOL_STATE_ACTIVE;
194 	}
195 
196 	return (zhp);
197 }
198 
199 /*
200  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
201  * state.
202  */
203 zpool_handle_t *
204 zpool_open(const char *pool)
205 {
206 	zpool_handle_t *zhp;
207 
208 	if ((zhp = zpool_open_canfail(pool)) == NULL)
209 		return (NULL);
210 
211 	if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
212 		zfs_error(dgettext(TEXT_DOMAIN, "cannot open ' %s': pool is "
213 		    "currently unavailable\n"), zhp->zpool_name);
214 		zfs_error(dgettext(TEXT_DOMAIN, "run 'zpool status -v %s' for "
215 		    "detailed information\n"), zhp->zpool_name);
216 		zpool_close(zhp);
217 		return (NULL);
218 	}
219 
220 	return (zhp);
221 }
222 
223 /*
224  * Close the handle.  Simply frees the memory associated with the handle.
225  */
226 void
227 zpool_close(zpool_handle_t *zhp)
228 {
229 	if (zhp->zpool_config)
230 		nvlist_free(zhp->zpool_config);
231 	if (zhp->zpool_old_config)
232 		nvlist_free(zhp->zpool_old_config);
233 	free(zhp);
234 }
235 
236 /*
237  * Return the name of the pool.
238  */
239 const char *
240 zpool_get_name(zpool_handle_t *zhp)
241 {
242 	return (zhp->zpool_name);
243 }
244 
245 /*
246  * Return the GUID of the pool.
247  */
248 uint64_t
249 zpool_get_guid(zpool_handle_t *zhp)
250 {
251 	uint64_t guid;
252 
253 	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
254 	    &guid) == 0);
255 	return (guid);
256 }
257 
258 /*
259  * Return the amount of space currently consumed by the pool.
260  */
261 uint64_t
262 zpool_get_space_used(zpool_handle_t *zhp)
263 {
264 	nvlist_t *nvroot;
265 	vdev_stat_t *vs;
266 	uint_t vsc;
267 
268 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
269 	    &nvroot) == 0);
270 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
271 	    (uint64_t **)&vs, &vsc) == 0);
272 
273 	return (vs->vs_alloc);
274 }
275 
276 /*
277  * Return the total space in the pool.
278  */
279 uint64_t
280 zpool_get_space_total(zpool_handle_t *zhp)
281 {
282 	nvlist_t *nvroot;
283 	vdev_stat_t *vs;
284 	uint_t vsc;
285 
286 	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
287 	    &nvroot) == 0);
288 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
289 	    (uint64_t **)&vs, &vsc) == 0);
290 
291 	return (vs->vs_space);
292 }
293 
294 /*
295  * Return the alternate root for this pool, if any.
296  */
297 int
298 zpool_get_root(zpool_handle_t *zhp, char *buf, size_t buflen)
299 {
300 	zfs_cmd_t zc = { 0 };
301 
302 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
303 	if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 ||
304 	    zc.zc_objset_stats.dds_altroot[0] == '\0')
305 		return (-1);
306 
307 	(void) strlcpy(buf, zc.zc_objset_stats.dds_altroot, buflen);
308 
309 	return (0);
310 }
311 
312 /*
313  * Return the state of the pool (ACTIVE or UNAVAILABLE)
314  */
315 int
316 zpool_get_state(zpool_handle_t *zhp)
317 {
318 	return (zhp->zpool_state);
319 }
320 
321 /*
322  * Create the named pool, using the provided vdev list.  It is assumed
323  * that the consumer has already validated the contents of the nvlist, so we
324  * don't have to worry about error semantics.
325  */
326 int
327 zpool_create(const char *pool, nvlist_t *nvroot, const char *altroot)
328 {
329 	zfs_cmd_t zc = { 0 };
330 	char *packed;
331 	size_t len;
332 	int err;
333 	char reason[64];
334 
335 	if (!zpool_name_valid(pool, reason, sizeof (reason))) {
336 		zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': %s"),
337 		    pool, reason);
338 		return (-1);
339 	}
340 
341 	if (altroot != NULL && altroot[0] != '/') {
342 		zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': alternate "
343 		    "root '%s' must be a complete path"), pool, altroot);
344 		return (-1);
345 	}
346 
347 	if ((err = nvlist_size(nvroot, &len, NV_ENCODE_NATIVE)) != 0)
348 		zfs_baderror(err);
349 
350 	packed = zfs_malloc(len);
351 
352 	if ((err = nvlist_pack(nvroot, &packed, &len,
353 	    NV_ENCODE_NATIVE, 0)) != 0)
354 		zfs_baderror(err);
355 
356 	(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
357 	zc.zc_config_src = (uint64_t)(uintptr_t)packed;
358 	zc.zc_config_src_size = len;
359 
360 	if (altroot != NULL)
361 		(void) strlcpy(zc.zc_root, altroot, sizeof (zc.zc_root));
362 
363 	if (ioctl(zfs_fd, ZFS_IOC_POOL_CREATE, &zc) != 0) {
364 		switch (errno) {
365 		case EEXIST:
366 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
367 			    "pool exists"), pool);
368 			break;
369 
370 		case EPERM:
371 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
372 			    "permission denied"), pool);
373 			break;
374 
375 		case EBUSY:
376 			/*
377 			 * This can happen if the user has specified the same
378 			 * device multiple times.  We can't reliably detect this
379 			 * until we try to add it and see we already have a
380 			 * label.
381 			 */
382 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
383 			    "one or more vdevs refer to the same device"),
384 			    pool);
385 			break;
386 
387 		case EOVERFLOW:
388 			/*
389 			 * This occurrs when one of the devices is below
390 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
391 			 * device was the problem device since there's no
392 			 * reliable way to determine device size from userland.
393 			 */
394 			{
395 				char buf[64];
396 
397 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
398 
399 				zfs_error(dgettext(TEXT_DOMAIN, "cannot "
400 				    "create '%s': one or more devices is less "
401 				    "than the minimum size (%s)"), pool,
402 				    buf);
403 			}
404 			break;
405 
406 		case ENAMETOOLONG:
407 			/*
408 			 * One of the vdevs has exceeded VDEV_SPEC_MAX length in
409 			 * its plaintext representation.
410 			 */
411 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
412 			    "too many devices in a single vdev"), pool);
413 			break;
414 
415 		case EIO:
416 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
417 			    "I/O error on one or more devices"), pool);
418 			break;
419 
420 		case ENXIO:
421 			/*
422 			 * This is unlikely to happen since we've verified that
423 			 * all the devices can be opened from userland, but it's
424 			 * still possible in some circumstances.
425 			 */
426 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
427 			    "one or more devices is unavailable"), pool);
428 			break;
429 
430 		case ENOSPC:
431 			/*
432 			 * This can occur if we were incapable of writing to a
433 			 * file vdev because the underlying filesystem is out of
434 			 * space.  This is very similar to EOVERFLOW, but we'll
435 			 * produce a slightly different message.
436 			 */
437 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
438 			    "one or more devices is out of space"), pool);
439 			break;
440 
441 		default:
442 			zfs_baderror(errno);
443 		}
444 
445 		return (-1);
446 	}
447 
448 	free(packed);
449 
450 	/*
451 	 * If this is an alternate root pool, then we automatically set the
452 	 * moutnpoint of the root dataset to be '/'.
453 	 */
454 	if (altroot != NULL) {
455 		zfs_handle_t *zhp;
456 
457 		verify((zhp = zfs_open(pool, ZFS_TYPE_ANY)) != NULL);
458 		verify(zfs_prop_set(zhp, ZFS_PROP_MOUNTPOINT, "/") == 0);
459 
460 		zfs_close(zhp);
461 	}
462 
463 	return (0);
464 }
465 
466 /*
467  * Destroy the given pool.  It is up to the caller to ensure that there are no
468  * datasets left in the pool.
469  */
470 int
471 zpool_destroy(zpool_handle_t *zhp)
472 {
473 	zfs_cmd_t zc = { 0 };
474 	zfs_handle_t *zfp = NULL;
475 
476 	if (zhp->zpool_state == POOL_STATE_ACTIVE &&
477 	    (zfp = zfs_open(zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
478 		return (-1);
479 
480 	if (zpool_remove_zvol_links(zhp) != NULL)
481 		return (-1);
482 
483 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
484 
485 	if (ioctl(zfs_fd, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
486 		switch (errno) {
487 		case EPERM:
488 			zfs_error(dgettext(TEXT_DOMAIN,
489 			    "cannot destroy '%s': permission denied"),
490 			    zhp->zpool_name);
491 			break;
492 
493 		case EBUSY:
494 			zfs_error(dgettext(TEXT_DOMAIN,
495 			    "cannot destroy '%s': pool busy"),
496 			    zhp->zpool_name);
497 			break;
498 
499 		case ENOENT:
500 			zfs_error(dgettext(TEXT_DOMAIN,
501 			    "cannot destroy '%s': no such pool"),
502 			    zhp->zpool_name);
503 			break;
504 
505 		case EROFS:
506 			zfs_error(dgettext(TEXT_DOMAIN,
507 			    "cannot destroy '%s': one or more devices is "
508 			    "read only, or '/' is mounted read only"),
509 			    zhp->zpool_name);
510 			break;
511 
512 		default:
513 			zfs_baderror(errno);
514 		}
515 
516 		if (zfp)
517 			zfs_close(zfp);
518 		return (-1);
519 	}
520 
521 	if (zfp) {
522 		remove_mountpoint(zfp);
523 		zfs_close(zfp);
524 	}
525 
526 	return (0);
527 }
528 
529 /*
530  * Add the given vdevs to the pool.  The caller must have already performed the
531  * necessary verification to ensure that the vdev specification is well-formed.
532  */
533 int
534 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
535 {
536 	char *packed;
537 	size_t len;
538 	zfs_cmd_t zc;
539 
540 	verify(nvlist_size(nvroot, &len, NV_ENCODE_NATIVE) == 0);
541 
542 	packed = zfs_malloc(len);
543 
544 	verify(nvlist_pack(nvroot, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
545 
546 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
547 	zc.zc_config_src = (uint64_t)(uintptr_t)packed;
548 	zc.zc_config_src_size = len;
549 
550 	if (ioctl(zfs_fd, ZFS_IOC_VDEV_ADD, &zc) != 0) {
551 		switch (errno) {
552 		case EPERM:
553 			zfs_error(dgettext(TEXT_DOMAIN, "cannot add to '%s': "
554 			    "permission denied"), zhp->zpool_name);
555 			break;
556 
557 		case EBUSY:
558 			/*
559 			 * This can happen if the user has specified the same
560 			 * device multiple times.  We can't reliably detect this
561 			 * until we try to add it and see we already have a
562 			 * label.
563 			 */
564 			zfs_error(dgettext(TEXT_DOMAIN, "cannot add to '%s': "
565 			    "one or more vdevs refer to the same device"),
566 			    zhp->zpool_name);
567 			break;
568 
569 		case ENAMETOOLONG:
570 			/*
571 			 * One of the vdevs has exceeded VDEV_SPEC_MAX length in
572 			 * its plaintext representation.
573 			 */
574 			zfs_error(dgettext(TEXT_DOMAIN, "cannot add to '%s': "
575 			    "too many devices in a single vdev"),
576 			    zhp->zpool_name);
577 			break;
578 
579 		case ENXIO:
580 			/*
581 			 * This is unlikely to happen since we've verified that
582 			 * all the devices can be opened from userland, but it's
583 			 * still possible in some circumstances.
584 			 */
585 			zfs_error(dgettext(TEXT_DOMAIN, "cannot add to '%s': "
586 			    "one or more devices is unavailable"),
587 			    zhp->zpool_name);
588 			break;
589 
590 		case EOVERFLOW:
591 			/*
592 			 * This occurrs when one of the devices is below
593 			 * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
594 			 * device was the problem device since there's no
595 			 * reliable way to determine device size from userland.
596 			 */
597 			{
598 				char buf[64];
599 
600 				zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
601 
602 				zfs_error(dgettext(TEXT_DOMAIN, "cannot "
603 				    "add to '%s': one or more devices is less "
604 				    "than the minimum size (%s)"),
605 				    zhp->zpool_name, buf);
606 			}
607 			break;
608 
609 		default:
610 			zfs_baderror(errno);
611 		}
612 
613 		return (-1);
614 	}
615 
616 	free(packed);
617 
618 	return (0);
619 }
620 
621 /*
622  * Exports the pool from the system.  The caller must ensure that there are no
623  * mounted datasets in the pool.
624  */
625 int
626 zpool_export(zpool_handle_t *zhp)
627 {
628 	zfs_cmd_t zc = { 0 };
629 
630 	if (zpool_remove_zvol_links(zhp) != 0)
631 		return (-1);
632 
633 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
634 
635 	if (ioctl(zfs_fd, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
636 		switch (errno) {
637 		case EPERM:
638 			zfs_error(dgettext(TEXT_DOMAIN,
639 			    "cannot export '%s': permission denied"),
640 			    zhp->zpool_name);
641 			break;
642 
643 		case EBUSY:
644 			zfs_error(dgettext(TEXT_DOMAIN,
645 			    "cannot export '%s': pool is in use"),
646 			    zhp->zpool_name);
647 			break;
648 
649 		case ENOENT:
650 			zfs_error(dgettext(TEXT_DOMAIN,
651 			    "cannot export '%s': no such pool"),
652 			    zhp->zpool_name);
653 			break;
654 
655 		default:
656 			zfs_baderror(errno);
657 		}
658 
659 		return (-1);
660 	}
661 
662 	return (0);
663 }
664 
665 /*
666  * Import the given pool using the known configuration.  The configuration
667  * should have come from zpool_find_import().  The 'newname' and 'altroot'
668  * parameters control whether the pool is imported with a different name or with
669  * an alternate root, respectively.
670  */
671 int
672 zpool_import(nvlist_t *config, const char *newname, const char *altroot)
673 {
674 	zfs_cmd_t zc;
675 	char *packed;
676 	size_t len;
677 	char *thename;
678 	char *origname;
679 	int ret;
680 
681 	verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
682 	    &origname) == 0);
683 
684 	if (newname != NULL) {
685 		if (!zpool_name_valid(newname, NULL, 0)) {
686 			zfs_error(dgettext(TEXT_DOMAIN, "cannot import '%s': "
687 			    "invalid pool name"), newname);
688 			return (-1);
689 		}
690 		thename = (char *)newname;
691 	} else {
692 		thename = origname;
693 	}
694 
695 	if (altroot != NULL && altroot[0] != '/') {
696 		zfs_error(dgettext(TEXT_DOMAIN, "cannot import '%s': alternate "
697 		    "root '%s' must be a complete path"), thename,
698 		    altroot);
699 		return (-1);
700 	}
701 
702 	(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
703 
704 	if (altroot != NULL)
705 		(void) strlcpy(zc.zc_root, altroot, sizeof (zc.zc_root));
706 	else
707 		zc.zc_root[0] = '\0';
708 
709 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
710 	    &zc.zc_pool_guid) == 0);
711 
712 	verify(nvlist_size(config, &len, NV_ENCODE_NATIVE) == 0);
713 
714 	packed = zfs_malloc(len);
715 
716 	verify(nvlist_pack(config, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
717 
718 	zc.zc_config_src = (uint64_t)(uintptr_t)packed;
719 	zc.zc_config_src_size = len;
720 
721 	ret = 0;
722 	if (ioctl(zfs_fd, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
723 		char desc[1024];
724 		if (newname == NULL)
725 			(void) snprintf(desc, sizeof (desc),
726 			    dgettext(TEXT_DOMAIN, "cannot import '%s'"),
727 			    thename);
728 		else
729 			(void) snprintf(desc, sizeof (desc),
730 			    dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
731 			    origname, thename);
732 
733 		switch (errno) {
734 		case EEXIST:
735 			/*
736 			 * A pool with that name already exists.
737 			 */
738 			zfs_error(dgettext(TEXT_DOMAIN, "%s: pool exists"),
739 			    desc);
740 			break;
741 
742 		case EPERM:
743 			/*
744 			 * The user doesn't have permission to create pools.
745 			 */
746 			zfs_error(dgettext(TEXT_DOMAIN, "%s: permission "
747 			    "denied"), desc);
748 			break;
749 
750 		case ENXIO:
751 		case EDOM:
752 			/*
753 			 * Device is unavailable, or vdev sum didn't match.
754 			 */
755 			zfs_error(dgettext(TEXT_DOMAIN, "%s: one or more "
756 			    "devices is unavailable"),
757 			    desc);
758 			break;
759 
760 		default:
761 			zfs_baderror(errno);
762 		}
763 
764 		ret = -1;
765 	} else {
766 		zpool_handle_t *zhp;
767 		/*
768 		 * This should never fail, but play it safe anyway.
769 		 */
770 		if ((zhp = zpool_open_silent(thename)) != NULL) {
771 			ret = zpool_create_zvol_links(zhp);
772 			zpool_close(zhp);
773 		}
774 	}
775 
776 	free(packed);
777 	return (ret);
778 }
779 
780 /*
781  * Scrub the pool.
782  */
783 int
784 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
785 {
786 	zfs_cmd_t zc = { 0 };
787 	char msg[1024];
788 
789 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
790 	zc.zc_cookie = type;
791 
792 	if (ioctl(zfs_fd, ZFS_IOC_POOL_SCRUB, &zc) == 0)
793 		return (0);
794 
795 	(void) snprintf(msg, sizeof (msg),
796 	    dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
797 
798 	switch (errno) {
799 	    case EPERM:
800 		/*
801 		 * No permission to scrub this pool.
802 		 */
803 		zfs_error(dgettext(TEXT_DOMAIN, "%s: permission denied"), msg);
804 		break;
805 
806 	    case EBUSY:
807 		/*
808 		 * Resilver in progress.
809 		 */
810 		zfs_error(dgettext(TEXT_DOMAIN, "%s: currently resilvering"),
811 		    msg);
812 		break;
813 
814 	    default:
815 		zfs_baderror(errno);
816 	}
817 	return (-1);
818 }
819 
820 /*
821  * Bring the specified vdev online
822  */
823 int
824 zpool_vdev_online(zpool_handle_t *zhp, const char *path)
825 {
826 	zfs_cmd_t zc = { 0 };
827 	char msg[1024];
828 
829 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
830 	(void) snprintf(zc.zc_prop_value, sizeof (zc.zc_prop_value),
831 	    "%s%s", path[0] == '/' ? "" : "/dev/dsk/", path);
832 
833 	if (ioctl(zfs_fd, ZFS_IOC_VDEV_ONLINE, &zc) == 0)
834 		return (0);
835 
836 	(void) snprintf(msg, sizeof (msg),
837 	    dgettext(TEXT_DOMAIN, "cannot online %s"), zc.zc_prop_value);
838 
839 	switch (errno) {
840 	    case ENODEV:
841 		/*
842 		 * Device doesn't exist
843 		 */
844 		zfs_error(dgettext(TEXT_DOMAIN, "%s: device not in pool"), msg);
845 		break;
846 
847 	    case EPERM:
848 		/*
849 		 * No permission to bring this vdev online.
850 		 */
851 		zfs_error(dgettext(TEXT_DOMAIN, "%s: permission denied"), msg);
852 		break;
853 
854 	    default:
855 		zfs_baderror(errno);
856 	}
857 	return (-1);
858 }
859 
860 /*
861  * Take the specified vdev offline
862  */
863 int
864 zpool_vdev_offline(zpool_handle_t *zhp, const char *path)
865 {
866 	zfs_cmd_t zc = { 0 };
867 	char msg[1024];
868 
869 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
870 	(void) snprintf(zc.zc_prop_value, sizeof (zc.zc_prop_value),
871 	    "%s%s", path[0] == '/' ? "" : "/dev/dsk/", path);
872 
873 	if (ioctl(zfs_fd, ZFS_IOC_VDEV_OFFLINE, &zc) == 0)
874 		return (0);
875 
876 	(void) snprintf(msg, sizeof (msg),
877 	    dgettext(TEXT_DOMAIN, "cannot offline %s"), zc.zc_prop_value);
878 
879 	switch (errno) {
880 	    case ENODEV:
881 		/*
882 		 * Device doesn't exist
883 		 */
884 		zfs_error(dgettext(TEXT_DOMAIN, "%s: device not in pool"), msg);
885 		break;
886 
887 	    case EPERM:
888 		/*
889 		 * No permission to take this vdev offline.
890 		 */
891 		zfs_error(dgettext(TEXT_DOMAIN, "%s: permission denied"), msg);
892 		break;
893 
894 	    case EBUSY:
895 		/*
896 		 * There are no other replicas of this device.
897 		 */
898 		zfs_error(dgettext(TEXT_DOMAIN, "%s: no valid replicas"), msg);
899 		break;
900 
901 	    default:
902 		zfs_baderror(errno);
903 	}
904 	return (-1);
905 }
906 
907 /*
908  * Attach new_disk (fully described by nvroot) to old_disk.
909  * If 'replacing' is specified, tne new disk will replace the old one.
910  */
911 int
912 zpool_vdev_attach(zpool_handle_t *zhp,
913     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
914 {
915 	zfs_cmd_t zc = { 0 };
916 	char msg[1024];
917 	char *packed;
918 	int ret;
919 	size_t len;
920 
921 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
922 	(void) snprintf(zc.zc_prop_value, sizeof (zc.zc_prop_value),
923 	    "%s%s", old_disk[0] == '/' ? "" : "/dev/dsk/", old_disk);
924 	zc.zc_cookie = replacing;
925 
926 	verify(nvlist_size(nvroot, &len, NV_ENCODE_NATIVE) == 0);
927 
928 	packed = zfs_malloc(len);
929 
930 	verify(nvlist_pack(nvroot, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
931 
932 	zc.zc_config_src = (uint64_t)(uintptr_t)packed;
933 	zc.zc_config_src_size = len;
934 
935 	ret = ioctl(zfs_fd, ZFS_IOC_VDEV_ATTACH, &zc);
936 
937 	free(packed);
938 
939 	if (ret == 0)
940 		return (0);
941 
942 	if (replacing)
943 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
944 		    "cannot replace %s with %s"), old_disk, new_disk);
945 	else
946 		(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
947 		    "cannot attach %s to %s"), new_disk, old_disk);
948 
949 	switch (errno) {
950 	    case EPERM:
951 		/*
952 		 * No permission to mess with the config.
953 		 */
954 		zfs_error(dgettext(TEXT_DOMAIN, "%s: permission denied"), msg);
955 		break;
956 
957 	    case ENODEV:
958 		/*
959 		 * Device doesn't exist.
960 		 */
961 		zfs_error(dgettext(TEXT_DOMAIN, "%s: %s not in pool"),
962 		    msg, old_disk);
963 		break;
964 
965 	    case ENOTSUP:
966 		/*
967 		 * Can't attach to or replace this type of vdev.
968 		 */
969 		if (replacing)
970 			zfs_error(dgettext(TEXT_DOMAIN,
971 			    "%s: cannot replace a replacing device"), msg);
972 		else
973 			zfs_error(dgettext(TEXT_DOMAIN,
974 			    "%s: attach is only applicable to mirrors"), msg);
975 		break;
976 
977 	    case EINVAL:
978 		/*
979 		 * The new device must be a single disk.
980 		 */
981 		zfs_error(dgettext(TEXT_DOMAIN,
982 		    "%s: <new_device> must be a single disk"), msg);
983 		break;
984 
985 	    case ENXIO:
986 		/*
987 		 * This is unlikely to happen since we've verified that
988 		 * all the devices can be opened from userland, but it's
989 		 * still possible in some circumstances.
990 		 */
991 		zfs_error(dgettext(TEXT_DOMAIN, "%s: %s is unavailable"),
992 		    msg, new_disk);
993 		break;
994 
995 	    case EBUSY:
996 		/*
997 		 * The new device is is use.
998 		 */
999 		zfs_error(dgettext(TEXT_DOMAIN, "%s: %s busy"), msg, new_disk);
1000 		break;
1001 
1002 	    case EOVERFLOW:
1003 		/*
1004 		 * The new device is too small.
1005 		 */
1006 		zfs_error(dgettext(TEXT_DOMAIN, "%s: %s is too small"),
1007 		    msg, new_disk);
1008 		break;
1009 
1010 	    case EDOM:
1011 		/*
1012 		 * The new device has a different alignment requirement.
1013 		 */
1014 		zfs_error(dgettext(TEXT_DOMAIN,
1015 		    "%s: devices have different sector alignment"), msg);
1016 		break;
1017 
1018 	    case ENAMETOOLONG:
1019 		/*
1020 		 * The resulting top-level vdev spec won't fit in the label.
1021 		 */
1022 		zfs_error(dgettext(TEXT_DOMAIN,
1023 		    "%s: too many devices in a single vdev"), msg);
1024 		break;
1025 
1026 	    default:
1027 		zfs_baderror(errno);
1028 	}
1029 
1030 	return (1);
1031 }
1032 
1033 /*
1034  * Detach the specified device.
1035  */
1036 int
1037 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1038 {
1039 	zfs_cmd_t zc = { 0 };
1040 	char msg[1024];
1041 
1042 	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1043 	(void) snprintf(zc.zc_prop_value, sizeof (zc.zc_prop_value),
1044 	    "%s%s", path[0] == '/' ? "" : "/dev/dsk/", path);
1045 
1046 	if (ioctl(zfs_fd, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1047 		return (0);
1048 
1049 	(void) snprintf(msg, sizeof (msg),
1050 	    dgettext(TEXT_DOMAIN, "cannot detach %s"), zc.zc_prop_value);
1051 
1052 	switch (errno) {
1053 	    case EPERM:
1054 		/*
1055 		 * No permission to mess with the config.
1056 		 */
1057 		zfs_error(dgettext(TEXT_DOMAIN, "%s: permission denied"), msg);
1058 		break;
1059 
1060 	    case ENODEV:
1061 		/*
1062 		 * Device doesn't exist.
1063 		 */
1064 		zfs_error(dgettext(TEXT_DOMAIN, "%s: device not in pool"), msg);
1065 		break;
1066 
1067 	    case ENOTSUP:
1068 		/*
1069 		 * Can't detach from this type of vdev.
1070 		 */
1071 		zfs_error(dgettext(TEXT_DOMAIN,
1072 		    "%s: only applicable to mirror and replacing vdevs"), msg);
1073 		break;
1074 
1075 	    case EBUSY:
1076 		/*
1077 		 * There are no other replicas of this device.
1078 		 */
1079 		zfs_error(dgettext(TEXT_DOMAIN, "%s: no valid replicas"), msg);
1080 		break;
1081 
1082 	    default:
1083 		zfs_baderror(errno);
1084 	}
1085 
1086 	return (1);
1087 }
1088 
1089 static int
1090 do_zvol(zfs_handle_t *zhp, void *data)
1091 {
1092 	int linktype = (int)(uintptr_t)data;
1093 	int ret;
1094 
1095 	/*
1096 	 * We check for volblocksize intead of ZFS_TYPE_VOLUME so that we
1097 	 * correctly handle snapshots of volumes.
1098 	 */
1099 	if (zhp->zfs_volblocksize != 0) {
1100 		if (linktype)
1101 			ret = zvol_create_link(zhp->zfs_name);
1102 		else
1103 			ret = zvol_remove_link(zhp->zfs_name);
1104 	}
1105 
1106 	ret = zfs_iter_children(zhp, do_zvol, data);
1107 
1108 	zfs_close(zhp);
1109 	return (ret);
1110 }
1111 
1112 /*
1113  * Iterate over all zvols in the pool and make any necessary minor nodes.
1114  */
1115 int
1116 zpool_create_zvol_links(zpool_handle_t *zhp)
1117 {
1118 	zfs_handle_t *zfp;
1119 	int ret;
1120 
1121 	/*
1122 	 * If the pool is unavailable, just return success.
1123 	 */
1124 	if ((zfp = make_dataset_handle(zhp->zpool_name)) == NULL)
1125 		return (0);
1126 
1127 	ret = zfs_iter_children(zfp, do_zvol, (void *)TRUE);
1128 
1129 	zfs_close(zfp);
1130 	return (ret);
1131 }
1132 
1133 /*
1134  * Iterate over all zvols in the poool and remove any minor nodes.
1135  */
1136 int
1137 zpool_remove_zvol_links(zpool_handle_t *zhp)
1138 {
1139 	zfs_handle_t *zfp;
1140 	int ret;
1141 
1142 	/*
1143 	 * If the pool is unavailable, just return success.
1144 	 */
1145 	if ((zfp = make_dataset_handle(zhp->zpool_name)) == NULL)
1146 		return (0);
1147 
1148 	ret = zfs_iter_children(zfp, do_zvol, (void *)FALSE);
1149 
1150 	zfs_close(zfp);
1151 	return (ret);
1152 }
1153 
1154 /*
1155  * Convert from a devid string to a path.
1156  */
1157 static char *
1158 devid_to_path(char *devid_str)
1159 {
1160 	ddi_devid_t devid;
1161 	char *minor;
1162 	char *path;
1163 	devid_nmlist_t *list = NULL;
1164 	int ret;
1165 
1166 	if (devid_str_decode(devid_str, &devid, &minor) != 0)
1167 		return (NULL);
1168 
1169 	ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
1170 
1171 	devid_str_free(minor);
1172 	devid_free(devid);
1173 
1174 	if (ret != 0)
1175 		return (NULL);
1176 
1177 	path = zfs_strdup(list[0].devname);
1178 	devid_free_nmlist(list);
1179 
1180 	return (path);
1181 }
1182 
1183 /*
1184  * Convert from a path to a devid string.
1185  */
1186 static char *
1187 path_to_devid(const char *path)
1188 {
1189 	int fd;
1190 	ddi_devid_t devid;
1191 	char *minor, *ret;
1192 
1193 	if ((fd = open(path, O_RDONLY)) < 0)
1194 		return (NULL);
1195 
1196 	minor = NULL;
1197 	ret = NULL;
1198 	if (devid_get(fd, &devid) == 0) {
1199 		if (devid_get_minor_name(fd, &minor) == 0)
1200 			ret = devid_str_encode(devid, minor);
1201 		if (minor != NULL)
1202 			devid_str_free(minor);
1203 		devid_free(devid);
1204 	}
1205 	(void) close(fd);
1206 
1207 	return (ret);
1208 }
1209 
1210 /*
1211  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
1212  * ignore any failure here, since a common case is for an unprivileged user to
1213  * type 'zpool status', and we'll display the correct information anyway.
1214  */
1215 static void
1216 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
1217 {
1218 	zfs_cmd_t zc = { 0 };
1219 
1220 	(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1221 	(void) strncpy(zc.zc_prop_value, path, sizeof (zc.zc_prop_value));
1222 	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1223 	    &zc.zc_pool_guid) == 0);
1224 
1225 	(void) ioctl(zfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
1226 }
1227 
1228 /*
1229  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
1230  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
1231  * We also check if this is a whole disk, in which case we strip off the
1232  * trailing 's0' slice name.
1233  *
1234  * This routine is also responsible for identifying when disks have been
1235  * reconfigured in a new location.  The kernel will have opened the device by
1236  * devid, but the path will still refer to the old location.  To catch this, we
1237  * first do a path -> devid translation (which is fast for the common case).  If
1238  * the devid matches, we're done.  If not, we do a reverse devid -> path
1239  * translation and issue the appropriate ioctl() to update the path of the vdev.
1240  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
1241  * of these checks.
1242  */
1243 char *
1244 zpool_vdev_name(zpool_handle_t *zhp, nvlist_t *nv)
1245 {
1246 	char *path, *devid;
1247 	uint64_t wholedisk;
1248 
1249 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1250 
1251 		if (zhp != NULL &&
1252 		    nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
1253 			/*
1254 			 * Determine if the current path is correct.
1255 			 */
1256 			char *newdevid = path_to_devid(path);
1257 
1258 			if (newdevid == NULL ||
1259 			    strcmp(devid, newdevid) != 0) {
1260 				char *newpath;
1261 
1262 				if ((newpath = devid_to_path(devid)) != NULL) {
1263 					/*
1264 					 * Update the path appropriately.
1265 					 */
1266 					set_path(zhp, nv, newpath);
1267 					verify(nvlist_add_string(nv,
1268 					    ZPOOL_CONFIG_PATH, newpath) == 0);
1269 					free(newpath);
1270 					verify(nvlist_lookup_string(nv,
1271 					    ZPOOL_CONFIG_PATH, &path) == 0);
1272 				}
1273 
1274 				if (newdevid)
1275 					devid_str_free(newdevid);
1276 			}
1277 
1278 		}
1279 
1280 		if (strncmp(path, "/dev/dsk/", 9) == 0)
1281 			path += 9;
1282 
1283 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1284 		    &wholedisk) == 0 && wholedisk) {
1285 			char *tmp = zfs_strdup(path);
1286 			tmp[strlen(path) - 2] = '\0';
1287 			return (tmp);
1288 		}
1289 	} else {
1290 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
1291 	}
1292 
1293 	return (zfs_strdup(path));
1294 }
1295