xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision 43d18f1c320355e93c47399bea0b2e022fe06364)
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 2005 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 <libdevinfo.h>
33 #include <libintl.h>
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <zone.h>
40 #include <sys/mntent.h>
41 #include <sys/mnttab.h>
42 
43 #include <sys/spa.h>
44 #include <sys/zio.h>
45 #include <libzfs.h>
46 
47 #include "zfs_namecheck.h"
48 #include "zfs_prop.h"
49 #include "libzfs_impl.h"
50 
51 /*
52  * Given a single type (not a mask of types), return the type in a human
53  * readable form.
54  */
55 const char *
56 zfs_type_to_name(zfs_type_t type)
57 {
58 	switch (type) {
59 	case ZFS_TYPE_FILESYSTEM:
60 		return (dgettext(TEXT_DOMAIN, "filesystem"));
61 	case ZFS_TYPE_SNAPSHOT:
62 		return (dgettext(TEXT_DOMAIN, "snapshot"));
63 	case ZFS_TYPE_VOLUME:
64 		return (dgettext(TEXT_DOMAIN, "volume"));
65 	}
66 
67 	zfs_baderror(type);
68 	return (NULL);
69 }
70 
71 /*
72  * Given a path and mask of ZFS types, return a string describing this dataset.
73  * This is used when we fail to open a dataset and we cannot get an exact type.
74  * We guess what the type would have been based on the path and the mask of
75  * acceptable types.
76  */
77 static const char *
78 path_to_str(const char *path, int types)
79 {
80 	/*
81 	 * When given a single type, always report the exact type.
82 	 */
83 	if (types == ZFS_TYPE_SNAPSHOT)
84 		return (dgettext(TEXT_DOMAIN, "snapshot"));
85 	if (types == ZFS_TYPE_FILESYSTEM)
86 		return (dgettext(TEXT_DOMAIN, "filesystem"));
87 	if (types == ZFS_TYPE_VOLUME)
88 		return (dgettext(TEXT_DOMAIN, "volume"));
89 
90 	/*
91 	 * The user is requesting more than one type of dataset.  If this is the
92 	 * case, consult the path itself.  If we're looking for a snapshot, and
93 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
94 	 * snapshot attribute and try again.
95 	 */
96 	if (types & ZFS_TYPE_SNAPSHOT) {
97 		if (strchr(path, '@') != NULL)
98 			return (dgettext(TEXT_DOMAIN, "snapshot"));
99 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
100 	}
101 
102 
103 	/*
104 	 * The user has requested either filesystems or volumes.
105 	 * We have no way of knowing a priori what type this would be, so always
106 	 * report it as "filesystem" or "volume", our two primitive types.
107 	 */
108 	if (types & ZFS_TYPE_FILESYSTEM)
109 		return (dgettext(TEXT_DOMAIN, "filesystem"));
110 
111 	assert(types & ZFS_TYPE_VOLUME);
112 	return (dgettext(TEXT_DOMAIN, "volume"));
113 }
114 
115 /*
116  * Validate a ZFS path.  This is used even before trying to open the dataset, to
117  * provide a more meaningful error message.  We place a more useful message in
118  * 'buf' detailing exactly why the name was not valid.
119  */
120 static int
121 zfs_validate_name(const char *path, int type, char *buf, size_t buflen)
122 {
123 	namecheck_err_t why;
124 	char what;
125 
126 	if (dataset_namecheck(path, &why, &what) != 0) {
127 		if (buf != NULL) {
128 			switch (why) {
129 			case NAME_ERR_TOOLONG:
130 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
131 				    "name is too long"), buflen);
132 				break;
133 
134 			case NAME_ERR_LEADING_SLASH:
135 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
136 				    "leading slash"), buflen);
137 				break;
138 
139 			case NAME_ERR_EMPTY_COMPONENT:
140 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
141 				    "empty component"), buflen);
142 				break;
143 
144 			case NAME_ERR_TRAILING_SLASH:
145 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
146 				    "trailing slash"), buflen);
147 				break;
148 
149 			case NAME_ERR_INVALCHAR:
150 				(void) snprintf(buf, buflen,
151 				    dgettext(TEXT_DOMAIN, "invalid character "
152 				    "'%c'"), what);
153 				break;
154 
155 			case NAME_ERR_MULTIPLE_AT:
156 				(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
157 				    "multiple '@' delimiters"), buflen);
158 				break;
159 			}
160 		}
161 
162 		return (0);
163 	}
164 
165 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
166 		if (buf != NULL)
167 			(void) strlcpy(buf,
168 			    dgettext(TEXT_DOMAIN,
169 			    "snapshot delimiter '@'"), buflen);
170 		return (0);
171 	}
172 
173 	return (1);
174 }
175 
176 int
177 zfs_name_valid(const char *name, zfs_type_t type)
178 {
179 	return (zfs_validate_name(name, type, NULL, NULL));
180 }
181 
182 /*
183  * Utility function to gather stats (objset and zpl) for the given object.
184  */
185 static int
186 get_stats(zfs_handle_t *zhp)
187 {
188 	zfs_cmd_t zc = { 0 };
189 
190 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
191 
192 	/*
193 	 * get the generic DMU stats and per-type (zfs, zvol) stats
194 	 */
195 	if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0)
196 		return (-1);
197 
198 	bcopy(&zc.zc_objset_stats, &zhp->zfs_dmustats,
199 	    sizeof (zc.zc_objset_stats));
200 
201 	bcopy(&zc.zc_zfs_stats, &zhp->zfs_zplstats, sizeof (zc.zc_zfs_stats));
202 
203 	zhp->zfs_volsize = zc.zc_volsize;
204 	zhp->zfs_volblocksize = zc.zc_volblocksize;
205 
206 	return (0);
207 }
208 
209 /*
210  * Refresh the properties currently stored in the handle.
211  */
212 void
213 zfs_refresh_properties(zfs_handle_t *zhp)
214 {
215 	(void) get_stats(zhp);
216 }
217 
218 /*
219  * Makes a handle from the given dataset name.  Used by zfs_open() and
220  * zfs_iter_* to create child handles on the fly.
221  */
222 zfs_handle_t *
223 make_dataset_handle(const char *path)
224 {
225 	zfs_handle_t *zhp = zfs_malloc(sizeof (zfs_handle_t));
226 
227 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
228 
229 	if (get_stats(zhp) != 0) {
230 		free(zhp);
231 		return (NULL);
232 	}
233 
234 	/*
235 	 * We've managed to open the dataset and gather statistics.  Determine
236 	 * the high-level type.
237 	 */
238 	if (zhp->zfs_dmustats.dds_is_snapshot)
239 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
240 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
241 		zhp->zfs_type = ZFS_TYPE_VOLUME;
242 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
243 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
244 	else
245 		/* we should never see any other dataset types */
246 		zfs_baderror(zhp->zfs_dmustats.dds_type);
247 
248 	return (zhp);
249 }
250 
251 /*
252  * Opens the given snapshot, filesystem, or volume.   The 'types'
253  * argument is a mask of acceptable types.  The function will print an
254  * appropriate error message and return NULL if it can't be opened.
255  */
256 zfs_handle_t *
257 zfs_open(const char *path, int types)
258 {
259 	zfs_handle_t *zhp;
260 
261 	/*
262 	 * Validate the name before we even try to open it.  We don't care about
263 	 * the verbose invalid messages here; just report a generic error.
264 	 */
265 	if (!zfs_validate_name(path, types, NULL, 0)) {
266 		zfs_error(dgettext(TEXT_DOMAIN,
267 		    "cannot open '%s': invalid %s name"), path,
268 		    path_to_str(path, types));
269 		return (NULL);
270 	}
271 
272 	/*
273 	 * Try to get stats for the dataset, which will tell us if it exists.
274 	 */
275 	errno = 0;
276 	if ((zhp = make_dataset_handle(path)) == NULL) {
277 		switch (errno) {
278 		case ENOENT:
279 			/*
280 			 * The dataset doesn't exist.
281 			 */
282 			zfs_error(dgettext(TEXT_DOMAIN,
283 			    "cannot open '%s': no such %s"), path,
284 			    path_to_str(path, types));
285 			break;
286 
287 		case EBUSY:
288 			/*
289 			 * We were able to open the dataset but couldn't
290 			 * get the stats.
291 			 */
292 			zfs_error(dgettext(TEXT_DOMAIN,
293 			    "cannot open '%s': %s is busy"), path,
294 			    path_to_str(path, types));
295 			break;
296 
297 		default:
298 			zfs_baderror(errno);
299 
300 		}
301 		return (NULL);
302 	}
303 
304 	if (!(types & zhp->zfs_type)) {
305 		zfs_error(dgettext(TEXT_DOMAIN, "cannot open '%s': operation "
306 		    "not supported for %ss"), path,
307 		    zfs_type_to_name(zhp->zfs_type));
308 		free(zhp);
309 		return (NULL);
310 	}
311 
312 	return (zhp);
313 }
314 
315 /*
316  * Release a ZFS handle.  Nothing to do but free the associated memory.
317  */
318 void
319 zfs_close(zfs_handle_t *zhp)
320 {
321 	if (zhp->zfs_mntopts)
322 		free(zhp->zfs_mntopts);
323 	free(zhp);
324 }
325 
326 struct {
327 	const char *name;
328 	uint64_t value;
329 } checksum_table[] = {
330 	{ "on",		ZIO_CHECKSUM_ON },
331 	{ "off",	ZIO_CHECKSUM_OFF },
332 	{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
333 	{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
334 	{ "sha256",	ZIO_CHECKSUM_SHA256 },
335 	{ NULL }
336 };
337 
338 struct {
339 	const char *name;
340 	uint64_t value;
341 } compress_table[] = {
342 	{ "on",		ZIO_COMPRESS_ON },
343 	{ "off",	ZIO_COMPRESS_OFF },
344 	{ "lzjb",	ZIO_COMPRESS_LZJB },
345 	{ NULL }
346 };
347 
348 struct {
349 	const char *name;
350 	uint64_t value;
351 } snapdir_table[] = {
352 	{ "hidden",	ZFS_SNAPDIR_HIDDEN },
353 	{ "visible",	ZFS_SNAPDIR_VISIBLE },
354 	{ NULL }
355 };
356 
357 struct {
358 	const char *name;
359 	uint64_t value;
360 } acl_mode_table[] = {
361 	{ "discard",	DISCARD },
362 	{ "groupmask",	GROUPMASK },
363 	{ "passthrough", PASSTHROUGH },
364 	{ NULL }
365 };
366 
367 struct {
368 	const char *name;
369 	uint64_t value;
370 } acl_inherit_table[] = {
371 	{ "discard",	DISCARD },
372 	{ "noallow",	NOALLOW },
373 	{ "secure",	SECURE },
374 	{ "passthrough", PASSTHROUGH },
375 	{ NULL }
376 };
377 
378 
379 /*
380  * Given a numeric suffix, convert the value into a number of bits that the
381  * resulting value must be shifted.
382  */
383 static int
384 str2shift(const char *buf, char *reason, size_t len)
385 {
386 	const char *ends = "BKMGTPEZ";
387 	int i;
388 
389 	if (buf[0] == '\0')
390 		return (0);
391 	for (i = 0; i < strlen(ends); i++) {
392 		if (toupper(buf[0]) == ends[i])
393 			break;
394 	}
395 	if (i == strlen(ends)) {
396 		(void) snprintf(reason, len, dgettext(TEXT_DOMAIN, "invalid "
397 		    "numeric suffix '%s'"), buf);
398 		return (-1);
399 	}
400 
401 	/*
402 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
403 	 * allow 'BB' - that's just weird.
404 	 */
405 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
406 	    toupper(buf[0]) != 'B')) {
407 		return (10*i);
408 	}
409 
410 	(void) snprintf(reason, len, dgettext(TEXT_DOMAIN, "invalid numeric "
411 	    "suffix '%s'"), buf);
412 	return (-1);
413 }
414 
415 /*
416  * Convert a string of the form '100G' into a real number.  Used when setting
417  * properties or creating a volume.  'buf' is used to place an extended error
418  * message for the caller to use.
419  */
420 static int
421 nicestrtonum(const char *value, uint64_t *num, char *buf, size_t buflen)
422 {
423 	char *end;
424 	int shift;
425 
426 	*num = 0;
427 
428 	/* Check to see if this looks like a number.  */
429 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
430 		(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
431 		    "must be a numeric value"), buflen);
432 		return (-1);
433 	}
434 
435 	/* Rely on stroll() to process the numeric portion.  */
436 	errno = 0;
437 	*num = strtoll(value, &end, 10);
438 
439 	/*
440 	 * Check for ERANGE, which indicates that the value is too large to fit
441 	 * in a 64-bit value.
442 	 */
443 	if (errno == ERANGE) {
444 		(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
445 		    "value is too large"), buflen);
446 		return (-1);
447 	}
448 
449 	/*
450 	 * If we have a decimal value, then do the computation with floating
451 	 * point arithmetic.  Otherwise, use standard arithmetic.
452 	 */
453 	if (*end == '.') {
454 		double fval = strtod(value, &end);
455 
456 		if ((shift = str2shift(end, buf, buflen)) == -1)
457 			return (-1);
458 
459 		fval *= pow(2, shift);
460 
461 		if (fval > UINT64_MAX) {
462 			(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
463 			    "value is too large"), buflen);
464 			return (-1);
465 		}
466 
467 		*num = (uint64_t)fval;
468 	} else {
469 		if ((shift = str2shift(end, buf, buflen)) == -1)
470 			return (-1);
471 
472 		/* Check for overflow */
473 		if (shift >= 64 || (*num << shift) >> shift != *num) {
474 			(void) strlcpy(buf, dgettext(TEXT_DOMAIN,
475 			    "value is too large"), buflen);
476 			return (-1);
477 		}
478 
479 		*num <<= shift;
480 	}
481 
482 	return (0);
483 }
484 
485 int
486 zfs_nicestrtonum(const char *str, uint64_t *val)
487 {
488 	char buf[1];
489 
490 	return (nicestrtonum(str, val, buf, sizeof (buf)));
491 }
492 
493 /*
494  * Given a property type and value, verify that the value is appropriate.  Used
495  * by zfs_prop_set() and some libzfs consumers.
496  */
497 int
498 zfs_prop_validate(zfs_prop_t prop, const char *value, uint64_t *intval)
499 {
500 	const char *propname = zfs_prop_to_name(prop);
501 	uint64_t number;
502 	char reason[64];
503 	int i;
504 
505 	/*
506 	 * Check to see if this a read-only property.
507 	 */
508 	if (zfs_prop_readonly(prop)) {
509 		zfs_error(dgettext(TEXT_DOMAIN,
510 		    "cannot set %s property: read-only property"), propname);
511 		return (-1);
512 	}
513 
514 	/* See if the property value is too long */
515 	if (strlen(value) >= ZFS_MAXPROPLEN) {
516 		zfs_error(dgettext(TEXT_DOMAIN,
517 		    "bad %s value '%s': value is too long"), propname,
518 		    value);
519 		return (-1);
520 	}
521 
522 	/* Perform basic checking based on property type */
523 	switch (zfs_prop_get_type(prop)) {
524 	case prop_type_boolean:
525 		if (strcmp(value, "on") == 0) {
526 			number = 1;
527 		} else if (strcmp(value, "off") == 0) {
528 			number = 0;
529 		} else {
530 			zfs_error(dgettext(TEXT_DOMAIN,
531 			    "bad %s value '%s': must be 'on' or 'off'"),
532 			    propname, value);
533 			return (-1);
534 		}
535 		break;
536 
537 	case prop_type_number:
538 		/* treat 'none' as 0 */
539 		if (strcmp(value, "none") == 0) {
540 			number = 0;
541 			break;
542 		}
543 
544 		if (nicestrtonum(value, &number, reason,
545 		    sizeof (reason)) != 0) {
546 			zfs_error(dgettext(TEXT_DOMAIN,
547 			    "bad %s value '%s': %s"), propname, value,
548 			    reason);
549 			return (-1);
550 		}
551 
552 		/* don't allow 0 for quota, use 'none' instead */
553 		if (prop == ZFS_PROP_QUOTA && number == 0 &&
554 		    strcmp(value, "none") != 0) {
555 			zfs_error(dgettext(TEXT_DOMAIN,
556 			    "bad %s value '%s': use '%s=none' to disable"),
557 			    propname, value, propname);
558 			return (-1);
559 		}
560 
561 		/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
562 		if (prop == ZFS_PROP_RECORDSIZE ||
563 		    prop == ZFS_PROP_VOLBLOCKSIZE) {
564 			if (number < SPA_MINBLOCKSIZE ||
565 			    number > SPA_MAXBLOCKSIZE || !ISP2(number)) {
566 				zfs_error(dgettext(TEXT_DOMAIN,
567 				    "bad %s value '%s': "
568 				    "must be power of 2 from %u to %uk"),
569 				    propname, value,
570 				    (uint_t)SPA_MINBLOCKSIZE,
571 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
572 				return (-1);
573 			}
574 		}
575 
576 		break;
577 
578 	case prop_type_string:
579 	case prop_type_index:
580 		/*
581 		 * The two writable string values, 'mountpoint' and
582 		 * 'checksum' need special consideration.  The 'index' types are
583 		 * specified as strings by the user, but passed to the kernel as
584 		 * integers.
585 		 */
586 		switch (prop) {
587 		case ZFS_PROP_MOUNTPOINT:
588 			if (strcmp(value, ZFS_MOUNTPOINT_NONE) == 0 ||
589 			    strcmp(value, ZFS_MOUNTPOINT_LEGACY) == 0)
590 				break;
591 
592 			if (value[0] != '/') {
593 				zfs_error(dgettext(TEXT_DOMAIN,
594 				    "bad %s value '%s': must be an absolute "
595 				    "path, 'none', or 'legacy'"),
596 				    propname, value);
597 				return (-1);
598 			}
599 			break;
600 
601 		case ZFS_PROP_CHECKSUM:
602 			for (i = 0; checksum_table[i].name != NULL; i++) {
603 				if (strcmp(value, checksum_table[i].name)
604 				    == 0) {
605 					number = checksum_table[i].value;
606 					break;
607 				}
608 			}
609 
610 			if (checksum_table[i].name == NULL) {
611 				zfs_error(dgettext(TEXT_DOMAIN,
612 				    "bad %s value '%s': must be 'on', 'off', "
613 				    "'fletcher2', 'fletcher4', or 'sha256'"),
614 				    propname, value);
615 				return (-1);
616 			}
617 			break;
618 
619 		case ZFS_PROP_COMPRESSION:
620 			for (i = 0; compress_table[i].name != NULL; i++) {
621 				if (strcmp(value, compress_table[i].name)
622 				    == 0) {
623 					number = compress_table[i].value;
624 					break;
625 				}
626 			}
627 
628 			if (compress_table[i].name == NULL) {
629 				zfs_error(dgettext(TEXT_DOMAIN,
630 				    "bad %s value '%s': must be 'on', 'off', "
631 				    "or 'lzjb'"),
632 				    propname, value);
633 				return (-1);
634 			}
635 			break;
636 
637 		case ZFS_PROP_SNAPDIR:
638 			for (i = 0; snapdir_table[i].name != NULL; i++) {
639 				if (strcmp(value, snapdir_table[i].name) == 0) {
640 					number = snapdir_table[i].value;
641 					break;
642 				}
643 			}
644 
645 			if (snapdir_table[i].name == NULL) {
646 				zfs_error(dgettext(TEXT_DOMAIN,
647 				    "bad %s value '%s': must be 'hidden' "
648 				    "or 'visible'"),
649 				    propname, value);
650 				return (-1);
651 			}
652 			break;
653 
654 		case ZFS_PROP_ACLMODE:
655 			for (i = 0; acl_mode_table[i].name != NULL; i++) {
656 				if (strcmp(value, acl_mode_table[i].name)
657 				    == 0) {
658 					number = acl_mode_table[i].value;
659 					break;
660 				}
661 			}
662 
663 			if (acl_mode_table[i].name == NULL) {
664 				zfs_error(dgettext(TEXT_DOMAIN,
665 				    "bad %s value '%s': must be 'discard', "
666 				    "'groupmask' or 'passthrough'"),
667 				    propname, value);
668 				return (-1);
669 			}
670 			break;
671 
672 		case ZFS_PROP_ACLINHERIT:
673 			for (i = 0; acl_inherit_table[i].name != NULL; i++) {
674 				if (strcmp(value, acl_inherit_table[i].name)
675 				    == 0) {
676 					number = acl_inherit_table[i].value;
677 					break;
678 				}
679 			}
680 
681 			if (acl_inherit_table[i].name == NULL) {
682 				zfs_error(dgettext(TEXT_DOMAIN,
683 				    "bad %s value '%s': must be 'discard', "
684 				    "'noallow', 'secure' or 'passthrough'"),
685 				    propname, value);
686 				return (-1);
687 			}
688 			break;
689 
690 		case ZFS_PROP_SHARENFS:
691 			/*
692 			 * Nothing to do for 'sharenfs', this gets passed on to
693 			 * share(1M) verbatim.
694 			 */
695 			break;
696 		}
697 	}
698 
699 	if (intval != NULL)
700 		*intval = number;
701 
702 	return (0);
703 }
704 
705 /*
706  * Given a property name and value, set the property for the given dataset.
707  */
708 int
709 zfs_prop_set(zfs_handle_t *zhp, zfs_prop_t prop, const char *propval)
710 {
711 	const char *propname = zfs_prop_to_name(prop);
712 	uint64_t number;
713 	zfs_cmd_t zc = { 0 };
714 	int ret;
715 	prop_changelist_t *cl;
716 
717 	if (zfs_prop_validate(prop, propval, &number) != 0)
718 		return (-1);
719 
720 	/*
721 	 * Check to see if the value applies to this type
722 	 */
723 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
724 		zfs_error(dgettext(TEXT_DOMAIN,
725 		    "cannot set %s for '%s': property does not apply to %ss"),
726 		    propname, zhp->zfs_name, zfs_type_to_name(zhp->zfs_type));
727 		return (-1);
728 	}
729 
730 	/*
731 	 * For the mountpoint and sharenfs properties, check if it can be set
732 	 * in a global/non-global zone based on the zoned property value:
733 	 *
734 	 *		global zone	    non-global zone
735 	 * -----------------------------------------------------
736 	 * zoned=on	mountpoint (no)	    mountpoint (yes)
737 	 *		sharenfs (no)	    sharenfs (no)
738 	 *
739 	 * zoned=off	mountpoint (yes)	N/A
740 	 *		sharenfs (yes)
741 	 */
742 	if (prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS) {
743 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
744 			if (getzoneid() == GLOBAL_ZONEID) {
745 				zfs_error(dgettext(TEXT_DOMAIN,
746 				    "cannot set %s for '%s', "
747 				    "dataset is used in a non-global zone"),
748 				    propname, zhp->zfs_name);
749 				return (-1);
750 			} else if (prop == ZFS_PROP_SHARENFS) {
751 				zfs_error(dgettext(TEXT_DOMAIN,
752 				    "cannot set %s for '%s', filesystems "
753 				    "cannot be shared in a non-global zone"),
754 				    propname, zhp->zfs_name);
755 				return (-1);
756 			}
757 		} else if (getzoneid() != GLOBAL_ZONEID) {
758 			/*
759 			 * If zoned property is 'off', this must be in
760 			 * a globle zone. If not, something is wrong.
761 			 */
762 			zfs_error(dgettext(TEXT_DOMAIN,
763 			    "cannot set %s for '%s', dataset is "
764 			    "used in a non-global zone, but 'zoned' "
765 			    "property is not set"),
766 			    propname, zhp->zfs_name);
767 			return (-1);
768 		}
769 	}
770 
771 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
772 		return (-1);
773 
774 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
775 		zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s for '%s', "
776 			"child dataset with inherited mountpoint is used "
777 			"in a non-global zone"),
778 			propname, zhp->zfs_name);
779 		ret = -1;
780 		goto error;
781 	}
782 
783 	if ((ret = changelist_prefix(cl)) != 0)
784 		goto error;
785 
786 	/*
787 	 * Execute the corresponding ioctl() to set this property.
788 	 */
789 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
790 
791 	switch (prop) {
792 	case ZFS_PROP_QUOTA:
793 		zc.zc_cookie = number;
794 		ret = ioctl(zfs_fd, ZFS_IOC_SET_QUOTA, &zc);
795 		break;
796 	case ZFS_PROP_RESERVATION:
797 		zc.zc_cookie = number;
798 		ret = ioctl(zfs_fd, ZFS_IOC_SET_RESERVATION, &zc);
799 		break;
800 	case ZFS_PROP_MOUNTPOINT:
801 	case ZFS_PROP_SHARENFS:
802 		/*
803 		 * These properties are passed down as real strings.
804 		 */
805 		(void) strlcpy(zc.zc_prop_name, propname,
806 		    sizeof (zc.zc_prop_name));
807 		(void) strlcpy(zc.zc_prop_value, propval,
808 		    sizeof (zc.zc_prop_value));
809 		zc.zc_intsz = 1;
810 		zc.zc_numints = strlen(propval) + 1;
811 		ret = ioctl(zfs_fd, ZFS_IOC_SET_PROP, &zc);
812 		break;
813 	case ZFS_PROP_VOLSIZE:
814 		zc.zc_volsize = number;
815 		ret = ioctl(zfs_fd, ZFS_IOC_SET_VOLSIZE, &zc);
816 		break;
817 	case ZFS_PROP_VOLBLOCKSIZE:
818 		zc.zc_volblocksize = number;
819 		ret = ioctl(zfs_fd, ZFS_IOC_SET_VOLBLOCKSIZE, &zc);
820 		break;
821 	default:
822 		(void) strlcpy(zc.zc_prop_name, propname,
823 		    sizeof (zc.zc_prop_name));
824 		/* LINTED - alignment */
825 		*(uint64_t *)zc.zc_prop_value = number;
826 		zc.zc_intsz = 8;
827 		zc.zc_numints = 1;
828 		ret = ioctl(zfs_fd, ZFS_IOC_SET_PROP, &zc);
829 		break;
830 	}
831 
832 	if (ret != 0) {
833 		switch (errno) {
834 
835 		case EPERM:
836 			zfs_error(dgettext(TEXT_DOMAIN,
837 			    "cannot set %s for '%s': permission "
838 			    "denied"), propname, zhp->zfs_name);
839 			break;
840 
841 		case ENOENT:
842 			zfs_error(dgettext(TEXT_DOMAIN,
843 			    "cannot open '%s': no such %s"), zhp->zfs_name,
844 			    zfs_type_to_name(zhp->zfs_type));
845 			break;
846 
847 		case ENOSPC:
848 			/*
849 			 * For quotas and reservations, ENOSPC indicates
850 			 * something different; setting a quota or reservation
851 			 * doesn't use any disk space.
852 			 */
853 			switch (prop) {
854 			case ZFS_PROP_QUOTA:
855 				zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s "
856 				    "for '%s': size is less than current "
857 				    "used or reserved space"), propname,
858 				    zhp->zfs_name);
859 				break;
860 
861 			case ZFS_PROP_RESERVATION:
862 				zfs_error(dgettext(TEXT_DOMAIN, "cannot set %s "
863 				    "for '%s': size is greater than available "
864 				    "space"), propname, zhp->zfs_name);
865 				break;
866 
867 			default:
868 				zfs_error(dgettext(TEXT_DOMAIN,
869 				    "cannot set %s for '%s': out of space"),
870 				    propname, zhp->zfs_name);
871 				break;
872 			}
873 			break;
874 
875 		case EBUSY:
876 			if (prop == ZFS_PROP_VOLBLOCKSIZE) {
877 				zfs_error(dgettext(TEXT_DOMAIN,
878 				    "cannot set %s for '%s': "
879 				    "volume already contains data"),
880 				    propname, zhp->zfs_name);
881 			} else {
882 				zfs_baderror(errno);
883 			}
884 			break;
885 
886 		case EOVERFLOW:
887 			/*
888 			 * This platform can't address a volume this big.
889 			 */
890 #ifdef _ILP32
891 			if (prop == ZFS_PROP_VOLSIZE) {
892 				zfs_error(dgettext(TEXT_DOMAIN,
893 				    "cannot set %s for '%s': "
894 				    "max volume size is 1TB on 32-bit systems"),
895 				    propname, zhp->zfs_name);
896 				break;
897 			}
898 #endif
899 			zfs_baderror(errno);
900 		default:
901 			zfs_baderror(errno);
902 		}
903 	} else {
904 		/*
905 		 * Refresh the statistics so the new property value
906 		 * is reflected.
907 		 */
908 		if ((ret = changelist_postfix(cl)) != 0)
909 			goto error;
910 
911 		(void) get_stats(zhp);
912 	}
913 
914 error:
915 	changelist_free(cl);
916 	return (ret);
917 }
918 
919 /*
920  * Given a property, inherit the value from the parent dataset.
921  */
922 int
923 zfs_prop_inherit(zfs_handle_t *zhp, zfs_prop_t prop)
924 {
925 	const char *propname = zfs_prop_to_name(prop);
926 	zfs_cmd_t zc = { 0 };
927 	int ret;
928 	prop_changelist_t *cl;
929 
930 	/*
931 	 * Verify that this property is inheritable.
932 	 */
933 	if (zfs_prop_readonly(prop)) {
934 		zfs_error(dgettext(TEXT_DOMAIN,
935 		    "cannot inherit %s for '%s': property is read-only"),
936 		    propname, zhp->zfs_name);
937 		return (-1);
938 	}
939 
940 	if (!zfs_prop_inheritable(prop)) {
941 		zfs_error(dgettext(TEXT_DOMAIN,
942 		    "cannot inherit %s for '%s': property is not inheritable"),
943 		    propname, zhp->zfs_name);
944 		return (-1);
945 	}
946 
947 	/*
948 	 * Check to see if the value applies to this type
949 	 */
950 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
951 		zfs_error(dgettext(TEXT_DOMAIN,
952 		    "cannot inherit %s for '%s': property does "
953 		    "not apply to %ss"), propname, zhp->zfs_name,
954 		    zfs_type_to_name(zhp->zfs_type));
955 		return (-1);
956 	}
957 
958 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
959 	(void) strlcpy(zc.zc_prop_name, propname, sizeof (zc.zc_prop_name));
960 
961 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
962 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
963 		zfs_error(dgettext(TEXT_DOMAIN, "cannot inherit %s for '%s', "
964 		    "dataset is used in a non-global zone"), propname,
965 		    zhp->zfs_name);
966 		return (-1);
967 	}
968 
969 	/*
970 	 * Determine datasets which will be affected by this change, if any.
971 	 */
972 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
973 		return (-1);
974 
975 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
976 		zfs_error(dgettext(TEXT_DOMAIN, "cannot inherit %s for '%s', "
977 			"child dataset with inherited mountpoint is "
978 			"used in a non-global zone"),
979 			propname, zhp->zfs_name);
980 		ret = -1;
981 		goto error;
982 	}
983 
984 	if ((ret = changelist_prefix(cl)) != 0)
985 		goto error;
986 
987 	zc.zc_numints = 0;
988 
989 	if ((ret = ioctl(zfs_fd, ZFS_IOC_SET_PROP, &zc)) != 0) {
990 		switch (errno) {
991 		case EPERM:
992 			zfs_error(dgettext(TEXT_DOMAIN,
993 			    "cannot inherit %s for '%s': permission "
994 			    "denied"), propname, zhp->zfs_name);
995 			break;
996 		case ENOENT:
997 			zfs_error(dgettext(TEXT_DOMAIN,
998 			    "cannot open '%s': no such %s"), zhp->zfs_name,
999 			    zfs_type_to_name(zhp->zfs_type));
1000 			break;
1001 		case ENOSPC:
1002 			zfs_error(dgettext(TEXT_DOMAIN,
1003 			    "cannot inherit %s for '%s': "
1004 			    "out of space"), propname, zhp->zfs_name);
1005 			break;
1006 		default:
1007 			zfs_baderror(errno);
1008 		}
1009 
1010 	} else {
1011 
1012 		if ((ret = changelist_postfix(cl)) != 0)
1013 			goto error;
1014 
1015 		/*
1016 		 * Refresh the statistics so the new property is reflected.
1017 		 */
1018 		(void) get_stats(zhp);
1019 	}
1020 
1021 
1022 error:
1023 	changelist_free(cl);
1024 	return (ret);
1025 }
1026 
1027 static void
1028 nicebool(int value, char *buf, size_t buflen)
1029 {
1030 	if (value)
1031 		(void) strlcpy(buf, "on", buflen);
1032 	else
1033 		(void) strlcpy(buf, "off", buflen);
1034 }
1035 
1036 /*
1037  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1038  * zfs_prop_get_int() are built using this interface.
1039  *
1040  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1041  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1042  * If they differ from the on-disk values, report the current values and mark
1043  * the source "temporary".
1044  */
1045 static uint64_t
1046 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src,
1047     char **source)
1048 {
1049 	uint64_t val;
1050 	struct mnttab mnt;
1051 
1052 	*source = NULL;
1053 
1054 	if (zhp->zfs_mntopts == NULL)
1055 		mnt.mnt_mntopts = "";
1056 	else
1057 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1058 
1059 	switch (prop) {
1060 	case ZFS_PROP_ATIME:
1061 		*source = zhp->zfs_zplstats.zs_atime_setpoint;
1062 		val = zhp->zfs_zplstats.zs_devices;
1063 
1064 		if (hasmntopt(&mnt, MNTOPT_ATIME) && !val) {
1065 			val = TRUE;
1066 			if (src)
1067 				*src = ZFS_SRC_TEMPORARY;
1068 		} else if (hasmntopt(&mnt, MNTOPT_NOATIME) && val) {
1069 			val = FALSE;
1070 			if (src)
1071 				*src = ZFS_SRC_TEMPORARY;
1072 		}
1073 		return (zhp->zfs_zplstats.zs_atime);
1074 
1075 	case ZFS_PROP_AVAILABLE:
1076 		return (zhp->zfs_dmustats.dds_available);
1077 
1078 	case ZFS_PROP_DEVICES:
1079 		*source = zhp->zfs_zplstats.zs_devices_setpoint;
1080 		val = zhp->zfs_zplstats.zs_devices;
1081 
1082 		if (hasmntopt(&mnt, MNTOPT_DEVICES) && !val) {
1083 			val = TRUE;
1084 			if (src)
1085 				*src = ZFS_SRC_TEMPORARY;
1086 		} else if (hasmntopt(&mnt, MNTOPT_NODEVICES) && val) {
1087 			val = FALSE;
1088 			if (src)
1089 				*src = ZFS_SRC_TEMPORARY;
1090 		}
1091 		return (val);
1092 
1093 	case ZFS_PROP_EXEC:
1094 		*source = zhp->zfs_zplstats.zs_exec_setpoint;
1095 		val = zhp->zfs_zplstats.zs_exec;
1096 
1097 		if (hasmntopt(&mnt, MNTOPT_EXEC) && !val) {
1098 			val = TRUE;
1099 			if (src)
1100 				*src = ZFS_SRC_TEMPORARY;
1101 		} else if (hasmntopt(&mnt, MNTOPT_NOEXEC) && val) {
1102 			val = FALSE;
1103 			if (src)
1104 				*src = ZFS_SRC_TEMPORARY;
1105 		}
1106 		return (val);
1107 
1108 	case ZFS_PROP_RECORDSIZE:
1109 		*source = zhp->zfs_zplstats.zs_recordsize_setpoint;
1110 		return (zhp->zfs_zplstats.zs_recordsize);
1111 
1112 	case ZFS_PROP_COMPRESSION:
1113 		*source = zhp->zfs_dmustats.dds_compression_setpoint;
1114 		return (zhp->zfs_dmustats.dds_compression);
1115 
1116 	case ZFS_PROP_READONLY:
1117 		*source = zhp->zfs_zplstats.zs_readonly_setpoint;
1118 		val = zhp->zfs_zplstats.zs_readonly;
1119 
1120 		if (hasmntopt(&mnt, MNTOPT_RO) && !val) {
1121 			val = TRUE;
1122 			if (src)
1123 				*src = ZFS_SRC_TEMPORARY;
1124 		} else if (hasmntopt(&mnt, MNTOPT_RW) && val) {
1125 			val = FALSE;
1126 			if (src)
1127 				*src = ZFS_SRC_TEMPORARY;
1128 		}
1129 		return (val);
1130 
1131 	case ZFS_PROP_QUOTA:
1132 		if (zhp->zfs_dmustats.dds_quota == 0)
1133 			*source = "";	/* default */
1134 		else
1135 			*source = zhp->zfs_name;
1136 		return (zhp->zfs_dmustats.dds_quota);
1137 
1138 	case ZFS_PROP_RESERVATION:
1139 		if (zhp->zfs_dmustats.dds_reserved == 0)
1140 			*source = "";	/* default */
1141 		else
1142 			*source = zhp->zfs_name;
1143 		return (zhp->zfs_dmustats.dds_reserved);
1144 
1145 	case ZFS_PROP_COMPRESSRATIO:
1146 		/*
1147 		 * Using physical space and logical space, calculate the
1148 		 * compression ratio.  We return the number as a multiple of
1149 		 * 100, so '2.5x' would be returned as 250.
1150 		 */
1151 		if (zhp->zfs_dmustats.dds_compressed_bytes == 0)
1152 			return (100ULL);
1153 		else
1154 			return (zhp->zfs_dmustats.dds_uncompressed_bytes * 100 /
1155 			    zhp->zfs_dmustats.dds_compressed_bytes);
1156 
1157 	case ZFS_PROP_REFERENCED:
1158 		/*
1159 		 * 'referenced' refers to the amount of physical space
1160 		 * referenced (possibly shared) by this object.
1161 		 */
1162 		return (zhp->zfs_dmustats.dds_space_refd);
1163 
1164 	case ZFS_PROP_SETUID:
1165 		*source = zhp->zfs_zplstats.zs_setuid_setpoint;
1166 		val = zhp->zfs_zplstats.zs_setuid;
1167 
1168 		if (hasmntopt(&mnt, MNTOPT_SETUID) && !val) {
1169 			val = TRUE;
1170 			if (src)
1171 				*src = ZFS_SRC_TEMPORARY;
1172 		} else if (hasmntopt(&mnt, MNTOPT_NOSETUID) && val) {
1173 			val = FALSE;
1174 			if (src)
1175 				*src = ZFS_SRC_TEMPORARY;
1176 		}
1177 		return (val);
1178 
1179 	case ZFS_PROP_VOLSIZE:
1180 		return (zhp->zfs_volsize);
1181 
1182 	case ZFS_PROP_VOLBLOCKSIZE:
1183 		return (zhp->zfs_volblocksize);
1184 
1185 	case ZFS_PROP_ZONED:
1186 		*source = zhp->zfs_dmustats.dds_zoned_setpoint;
1187 		return (zhp->zfs_dmustats.dds_zoned);
1188 
1189 	case ZFS_PROP_USED:
1190 		return (zhp->zfs_dmustats.dds_space_used);
1191 
1192 	case ZFS_PROP_CREATETXG:
1193 		return (zhp->zfs_dmustats.dds_creation_txg);
1194 
1195 	case ZFS_PROP_MOUNTED:
1196 		/*
1197 		 * Unlike other properties, we defer calculation of 'MOUNTED'
1198 		 * until actually requested.  This is because the getmntany()
1199 		 * call can be extremely expensive on systems with a large
1200 		 * number of filesystems, and the property isn't needed in
1201 		 * normal use cases.
1202 		 */
1203 		if (zhp->zfs_mntopts == NULL) {
1204 			struct mnttab search = { 0 }, entry;
1205 
1206 			search.mnt_special = (char *)zhp->zfs_name;
1207 			rewind(mnttab_file);
1208 
1209 			if (getmntany(mnttab_file, &entry, &search) == 0)
1210 				zhp->zfs_mntopts =
1211 				    zfs_strdup(entry.mnt_mntopts);
1212 		}
1213 		return (zhp->zfs_mntopts != NULL);
1214 
1215 	default:
1216 		zfs_baderror(EINVAL);
1217 	}
1218 
1219 	return (0);
1220 }
1221 
1222 /*
1223  * Calculate the source type, given the raw source string.
1224  */
1225 static void
1226 get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source,
1227     char *statbuf, size_t statlen)
1228 {
1229 	if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY)
1230 		return;
1231 
1232 	if (source == NULL) {
1233 		*srctype = ZFS_SRC_NONE;
1234 	} else if (source[0] == '\0') {
1235 		*srctype = ZFS_SRC_DEFAULT;
1236 	} else {
1237 		if (strcmp(source, zhp->zfs_name) == 0) {
1238 			*srctype = ZFS_SRC_LOCAL;
1239 		} else {
1240 			(void) strlcpy(statbuf, source, statlen);
1241 			*srctype = ZFS_SRC_INHERITED;
1242 		}
1243 	}
1244 
1245 }
1246 
1247 /*
1248  * Retrieve a property from the given object.  If 'literal' is specified, then
1249  * numbers are left as exact values.  Otherwise, numbers are converted to a
1250  * human-readable form.
1251  *
1252  * Returns 0 on success, or -1 on error.
1253  */
1254 int
1255 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1256     zfs_source_t *src, char *statbuf, size_t statlen, int literal)
1257 {
1258 	char *source = NULL;
1259 	uint64_t val;
1260 	char *str;
1261 	int i;
1262 	const char *root;
1263 
1264 	/*
1265 	 * Check to see if this property applies to our object
1266 	 */
1267 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1268 		return (-1);
1269 
1270 	if (src)
1271 		*src = ZFS_SRC_NONE;
1272 
1273 	switch (prop) {
1274 	case ZFS_PROP_ATIME:
1275 	case ZFS_PROP_READONLY:
1276 	case ZFS_PROP_SETUID:
1277 	case ZFS_PROP_ZONED:
1278 	case ZFS_PROP_DEVICES:
1279 	case ZFS_PROP_EXEC:
1280 		/*
1281 		 * Basic boolean values are built on top of
1282 		 * get_numeric_property().
1283 		 */
1284 		nicebool(get_numeric_property(zhp, prop, src, &source),
1285 		    propbuf, proplen);
1286 
1287 		break;
1288 
1289 	case ZFS_PROP_AVAILABLE:
1290 	case ZFS_PROP_RECORDSIZE:
1291 	case ZFS_PROP_CREATETXG:
1292 	case ZFS_PROP_REFERENCED:
1293 	case ZFS_PROP_USED:
1294 	case ZFS_PROP_VOLSIZE:
1295 	case ZFS_PROP_VOLBLOCKSIZE:
1296 		/*
1297 		 * Basic numeric values are built on top of
1298 		 * get_numeric_property().
1299 		 */
1300 		val = get_numeric_property(zhp, prop, src, &source);
1301 		if (literal)
1302 			(void) snprintf(propbuf, proplen, "%llu", val);
1303 		else
1304 			zfs_nicenum(val, propbuf, proplen);
1305 		break;
1306 
1307 	case ZFS_PROP_COMPRESSION:
1308 		for (i = 0; compress_table[i].name != NULL; i++) {
1309 			if (compress_table[i].value ==
1310 			    zhp->zfs_dmustats.dds_compression)
1311 				break;
1312 		}
1313 		assert(compress_table[i].name != NULL);
1314 		(void) strlcpy(propbuf, compress_table[i].name, proplen);
1315 		source = zhp->zfs_dmustats.dds_compression_setpoint;
1316 		break;
1317 
1318 	case ZFS_PROP_CHECKSUM:
1319 		for (i = 0; checksum_table[i].name != NULL; i++) {
1320 			if (checksum_table[i].value ==
1321 			    zhp->zfs_dmustats.dds_checksum)
1322 				break;
1323 		}
1324 		assert(checksum_table[i].name != NULL);
1325 		(void) strlcpy(propbuf, checksum_table[i].name, proplen);
1326 		source = zhp->zfs_dmustats.dds_checksum_setpoint;
1327 		break;
1328 
1329 	case ZFS_PROP_SNAPDIR:
1330 		for (i = 0; snapdir_table[i].name != NULL; i++) {
1331 			if (snapdir_table[i].value ==
1332 			    zhp->zfs_zplstats.zs_snapdir)
1333 				break;
1334 		}
1335 		assert(snapdir_table[i].name != NULL);
1336 		(void) strlcpy(propbuf, snapdir_table[i].name, proplen);
1337 		source = zhp->zfs_zplstats.zs_snapdir_setpoint;
1338 		break;
1339 
1340 	case ZFS_PROP_ACLMODE:
1341 		for (i = 0; acl_mode_table[i].name != NULL; i++) {
1342 			if (acl_mode_table[i].value ==
1343 			    zhp->zfs_zplstats.zs_acl_mode)
1344 				break;
1345 		}
1346 		assert(acl_mode_table[i].name != NULL);
1347 		(void) strlcpy(propbuf, acl_mode_table[i].name, proplen);
1348 		source = zhp->zfs_zplstats.zs_acl_mode_setpoint;
1349 		break;
1350 
1351 	case ZFS_PROP_ACLINHERIT:
1352 		for (i = 0; acl_inherit_table[i].name != NULL; i++) {
1353 			if (acl_inherit_table[i].value ==
1354 			    zhp->zfs_zplstats.zs_acl_inherit)
1355 				break;
1356 		}
1357 		assert(acl_inherit_table[i].name != NULL);
1358 		(void) strlcpy(propbuf, acl_inherit_table[i].name, proplen);
1359 		source = zhp->zfs_zplstats.zs_acl_inherit_setpoint;
1360 		break;
1361 
1362 	case ZFS_PROP_CREATION:
1363 		/*
1364 		 * 'creation' is a time_t stored in the statistics.  We convert
1365 		 * this into a string unless 'literal' is specified.
1366 		 */
1367 		{
1368 			time_t time = (time_t)
1369 			    zhp->zfs_dmustats.dds_creation_time;
1370 			struct tm t;
1371 
1372 			if (literal ||
1373 			    localtime_r(&time, &t) == NULL ||
1374 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1375 			    &t) == 0)
1376 				(void) snprintf(propbuf, proplen, "%llu",
1377 				    zhp->zfs_dmustats.dds_creation_time);
1378 		}
1379 		break;
1380 
1381 	case ZFS_PROP_MOUNTPOINT:
1382 		/*
1383 		 * Getting the precise mountpoint can be tricky.
1384 		 *
1385 		 *  - for 'none' or 'legacy', return those values.
1386 		 *  - for default mountpoints, construct it as /zfs/<dataset>
1387 		 *  - for inherited mountpoints, we want to take everything
1388 		 *    after our ancestor and append it to the inherited value.
1389 		 *
1390 		 * If the pool has an alternate root, we want to prepend that
1391 		 * root to any values we return.
1392 		 */
1393 		root = zhp->zfs_dmustats.dds_altroot;
1394 
1395 		if (zhp->zfs_zplstats.zs_mountpoint[0] == '\0') {
1396 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
1397 			    root, zhp->zfs_name);
1398 		} else if (zhp->zfs_zplstats.zs_mountpoint[0] == '/') {
1399 			const char *relpath = zhp->zfs_name +
1400 			    strlen(zhp->zfs_zplstats.zs_mountpoint_setpoint);
1401 			const char *mntpoint = zhp->zfs_zplstats.zs_mountpoint;
1402 
1403 			if (relpath[0] == '/')
1404 				relpath++;
1405 			if (mntpoint[1] == '\0')
1406 				mntpoint++;
1407 
1408 			if (relpath[0] == '\0')
1409 				(void) snprintf(propbuf, proplen, "%s%s",
1410 				    root, mntpoint);
1411 			else
1412 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
1413 				    root, mntpoint,
1414 				    relpath[0] == '@' ? "" : "/",
1415 				    relpath);
1416 		} else {
1417 			/* 'legacy' or 'none' */
1418 			(void) strlcpy(propbuf, zhp->zfs_zplstats.zs_mountpoint,
1419 			    proplen);
1420 		}
1421 
1422 		source = zhp->zfs_zplstats.zs_mountpoint_setpoint;
1423 		break;
1424 
1425 	case ZFS_PROP_SHARENFS:
1426 		(void) strlcpy(propbuf, zhp->zfs_zplstats.zs_sharenfs, proplen);
1427 		source = zhp->zfs_zplstats.zs_sharenfs_setpoint;
1428 		break;
1429 
1430 	case ZFS_PROP_ORIGIN:
1431 		(void) strlcpy(propbuf, zhp->zfs_dmustats.dds_clone_of,
1432 		    proplen);
1433 		/*
1434 		 * If there is no parent at all, return failure to indicate that
1435 		 * it doesn't apply to this dataset.
1436 		 */
1437 		if (propbuf[0] == '\0')
1438 			return (-1);
1439 		break;
1440 
1441 	case ZFS_PROP_QUOTA:
1442 	case ZFS_PROP_RESERVATION:
1443 		val = get_numeric_property(zhp, prop, src, &source);
1444 
1445 		/*
1446 		 * If quota or reservation is 0, we translate this into 'none'
1447 		 * (unless literal is set), and indicate that it's the default
1448 		 * value.  Otherwise, we print the number nicely and indicate
1449 		 * that its set locally.
1450 		 */
1451 		if (val == 0) {
1452 			if (literal)
1453 				(void) strlcpy(propbuf, "0", proplen);
1454 			else
1455 				(void) strlcpy(propbuf, "none", proplen);
1456 		} else {
1457 			if (literal)
1458 				(void) snprintf(propbuf, proplen, "%llu", val);
1459 			else
1460 				zfs_nicenum(val, propbuf, proplen);
1461 		}
1462 		break;
1463 
1464 	case ZFS_PROP_COMPRESSRATIO:
1465 		val = get_numeric_property(zhp, prop, src, &source);
1466 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", val / 100,
1467 		    val % 100);
1468 		break;
1469 
1470 	case ZFS_PROP_TYPE:
1471 		switch (zhp->zfs_type) {
1472 		case ZFS_TYPE_FILESYSTEM:
1473 			str = "filesystem";
1474 			break;
1475 		case ZFS_TYPE_VOLUME:
1476 			str = "volume";
1477 			break;
1478 		case ZFS_TYPE_SNAPSHOT:
1479 			str = "snapshot";
1480 			break;
1481 		default:
1482 			zfs_baderror(zhp->zfs_type);
1483 		}
1484 		(void) snprintf(propbuf, proplen, "%s", str);
1485 		break;
1486 
1487 	case ZFS_PROP_MOUNTED:
1488 		/*
1489 		 * The 'mounted' property is a pseudo-property that described
1490 		 * whether the filesystem is currently mounted.  Even though
1491 		 * it's a boolean value, the typical values of "on" and "off"
1492 		 * don't make sense, so we translate to "yes" and "no".
1493 		 */
1494 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, src, &source))
1495 			(void) strlcpy(propbuf, "yes", proplen);
1496 		else
1497 			(void) strlcpy(propbuf, "no", proplen);
1498 		break;
1499 
1500 	case ZFS_PROP_NAME:
1501 		/*
1502 		 * The 'name' property is a pseudo-property derived from the
1503 		 * dataset name.  It is presented as a real property to simplify
1504 		 * consumers.
1505 		 */
1506 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
1507 		break;
1508 
1509 	default:
1510 		zfs_baderror(EINVAL);
1511 	}
1512 
1513 	get_source(zhp, src, source, statbuf, statlen);
1514 
1515 	return (0);
1516 }
1517 
1518 /*
1519  * Utility function to get the given numeric property.  Does no validation that
1520  * the given property is the appropriate type; should only be used with
1521  * hard-coded property types.
1522  */
1523 uint64_t
1524 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1525 {
1526 	char *source;
1527 	zfs_source_t sourcetype = ZFS_SRC_NONE;
1528 
1529 	return (get_numeric_property(zhp, prop, &sourcetype, &source));
1530 }
1531 
1532 /*
1533  * Similar to zfs_prop_get(), but returns the value as an integer.
1534  */
1535 int
1536 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
1537     zfs_source_t *src, char *statbuf, size_t statlen)
1538 {
1539 	char *source;
1540 
1541 	/*
1542 	 * Check to see if this property applies to our object
1543 	 */
1544 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1545 		return (-1);
1546 
1547 	if (src)
1548 		*src = ZFS_SRC_NONE;
1549 
1550 	*value = get_numeric_property(zhp, prop, src, &source);
1551 
1552 	get_source(zhp, src, source, statbuf, statlen);
1553 
1554 	return (0);
1555 }
1556 
1557 /*
1558  * Returns the name of the given zfs handle.
1559  */
1560 const char *
1561 zfs_get_name(const zfs_handle_t *zhp)
1562 {
1563 	return (zhp->zfs_name);
1564 }
1565 
1566 /*
1567  * Returns the type of the given zfs handle.
1568  */
1569 zfs_type_t
1570 zfs_get_type(const zfs_handle_t *zhp)
1571 {
1572 	return (zhp->zfs_type);
1573 }
1574 
1575 /*
1576  * Iterate over all children, datasets and snapshots.
1577  */
1578 int
1579 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
1580 {
1581 	zfs_cmd_t zc = { 0 };
1582 	zfs_handle_t *nzhp;
1583 	int ret;
1584 
1585 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1586 	    ioctl(zfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
1587 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
1588 		/*
1589 		 * Ignore private dataset names.
1590 		 */
1591 		if (dataset_name_hidden(zc.zc_name))
1592 			continue;
1593 
1594 		/*
1595 		 * Silently ignore errors, as the only plausible explanation is
1596 		 * that the pool has since been removed.
1597 		 */
1598 		if ((nzhp = make_dataset_handle(zc.zc_name)) == NULL)
1599 			continue;
1600 
1601 		if ((ret = func(nzhp, data)) != 0)
1602 			return (ret);
1603 	}
1604 
1605 	/*
1606 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
1607 	 * returned, then the underlying dataset has been removed since we
1608 	 * obtained the handle.
1609 	 */
1610 	if (errno != ESRCH && errno != ENOENT)
1611 		zfs_baderror(errno);
1612 
1613 	bzero(&zc, sizeof (zc));
1614 
1615 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1616 	    ioctl(zfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, &zc) == 0;
1617 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
1618 
1619 		if ((nzhp = make_dataset_handle(zc.zc_name)) == NULL)
1620 			continue;
1621 
1622 		if ((ret = func(nzhp, data)) != 0)
1623 			return (ret);
1624 	}
1625 
1626 	/*
1627 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
1628 	 * returned, then the underlying dataset has been removed since we
1629 	 * obtained the handle.  Silently ignore this case, and return success.
1630 	 */
1631 	if (errno != ESRCH && errno != ENOENT)
1632 		zfs_baderror(errno);
1633 
1634 	return (0);
1635 }
1636 
1637 /*
1638  * Given a complete name, return just the portion that refers to the parent.
1639  * Can return NULL if this is a pool.
1640  */
1641 static int
1642 parent_name(const char *path, char *buf, size_t buflen)
1643 {
1644 	char *loc;
1645 
1646 	if ((loc = strrchr(path, '/')) == NULL)
1647 		return (-1);
1648 
1649 	(void) strncpy(buf, path, MIN(buflen, loc - path));
1650 	buf[loc - path] = '\0';
1651 
1652 	return (0);
1653 }
1654 
1655 /*
1656  * Checks to make sure that the given path has a parent, and that it exists.
1657  */
1658 static int
1659 check_parents(const char *path, zfs_type_t type)
1660 {
1661 	zfs_cmd_t zc = { 0 };
1662 	char parent[ZFS_MAXNAMELEN];
1663 	char *slash;
1664 
1665 	/* get parent, and check to see if this is just a pool */
1666 	if (parent_name(path, parent, sizeof (parent)) != 0) {
1667 		zfs_error(dgettext(TEXT_DOMAIN,
1668 		    "cannot create '%s': missing dataset name"),
1669 		    path, zfs_type_to_name(type));
1670 		zfs_error(dgettext(TEXT_DOMAIN,
1671 		    "use 'zpool create' to create a storage pool"));
1672 		return (-1);
1673 	}
1674 
1675 	/* check to see if the pool exists */
1676 	if ((slash = strchr(parent, '/')) == NULL)
1677 		slash = parent + strlen(parent);
1678 	(void) strncpy(zc.zc_name, parent, slash - parent);
1679 	zc.zc_name[slash - parent] = '\0';
1680 	if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
1681 	    errno == ENOENT) {
1682 		zfs_error(dgettext(TEXT_DOMAIN,
1683 		    "cannot create '%s': no such pool '%s'"), path, zc.zc_name);
1684 		return (-1);
1685 	}
1686 
1687 	/* check to see if the parent dataset exists */
1688 	(void) strlcpy(zc.zc_name, parent, sizeof (zc.zc_name));
1689 	if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1690 		switch (errno) {
1691 		case ENOENT:
1692 			zfs_error(dgettext(TEXT_DOMAIN,
1693 			    "cannot create '%s': parent does not exist"), path);
1694 			return (-1);
1695 
1696 		default:
1697 			zfs_baderror(errno);
1698 		}
1699 	}
1700 
1701 	/* we are in a non-global zone, but parent is in the global zone */
1702 	if (getzoneid() != GLOBAL_ZONEID && !zc.zc_objset_stats.dds_zoned) {
1703 		zfs_error(dgettext(TEXT_DOMAIN,
1704 		    "cannot create '%s': permission denied"), path);
1705 		return (-1);
1706 	}
1707 
1708 	/* make sure parent is a filesystem */
1709 	if (zc.zc_objset_stats.dds_type != DMU_OST_ZFS) {
1710 		zfs_error(dgettext(TEXT_DOMAIN,
1711 		    "cannot create '%s': parent is not a filesystem"),
1712 		    path);
1713 		return (-1);
1714 	}
1715 
1716 	return (0);
1717 }
1718 
1719 /*
1720  * Create a new filesystem or volume.  'sizestr' and 'blocksizestr' are used
1721  * only for volumes, and indicate the size and blocksize of the volume.
1722  */
1723 int
1724 zfs_create(const char *path, zfs_type_t type,
1725 	const char *sizestr, const char *blocksizestr)
1726 {
1727 	char reason[64];
1728 	zfs_cmd_t zc = { 0 };
1729 	int ret;
1730 	uint64_t size = 0;
1731 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
1732 
1733 	/* convert sizestr into integer size */
1734 	if (sizestr != NULL && nicestrtonum(sizestr, &size,
1735 	    reason, sizeof (reason)) != 0) {
1736 		zfs_error(dgettext(TEXT_DOMAIN,
1737 		    "bad volume size '%s': %s"), sizestr, reason);
1738 		return (-1);
1739 	}
1740 
1741 	/* convert blocksizestr into integer blocksize */
1742 	if (blocksizestr != NULL && nicestrtonum(blocksizestr, &blocksize,
1743 	    reason, sizeof (reason)) != 0) {
1744 		zfs_error(dgettext(TEXT_DOMAIN,
1745 		    "bad volume blocksize '%s': %s"), blocksizestr, reason);
1746 		return (-1);
1747 	}
1748 
1749 	/* validate the path, taking care to note the extended error message */
1750 	if (!zfs_validate_name(path, type, reason, sizeof (reason))) {
1751 		zfs_error(dgettext(TEXT_DOMAIN,
1752 		    "cannot create '%s': %s in %s name"), path, reason,
1753 		    zfs_type_to_name(type));
1754 		if (strstr(reason, "snapshot") != NULL)
1755 			zfs_error(dgettext(TEXT_DOMAIN,
1756 			    "use 'zfs snapshot' to create a snapshot"));
1757 		return (-1);
1758 	}
1759 
1760 	/* validate parents exist */
1761 	if (check_parents(path, type) != 0)
1762 		return (-1);
1763 
1764 	/*
1765 	 * The failure modes when creating a dataset of a different type over
1766 	 * one that already exists is a little strange.  In particular, if you
1767 	 * try to create a dataset on top of an existing dataset, the ioctl()
1768 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
1769 	 * first try to see if the dataset exists.
1770 	 */
1771 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
1772 	if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0) {
1773 		zfs_error(dgettext(TEXT_DOMAIN,
1774 		    "cannot create '%s': dataset exists"), path);
1775 		return (-1);
1776 	}
1777 
1778 	if (type == ZFS_TYPE_VOLUME)
1779 		zc.zc_objset_type = DMU_OST_ZVOL;
1780 	else
1781 		zc.zc_objset_type = DMU_OST_ZFS;
1782 
1783 	if (type == ZFS_TYPE_VOLUME) {
1784 		if (size == 0) {
1785 			zfs_error(dgettext(TEXT_DOMAIN,
1786 			    "bad volume size '%s': cannot be zero"), sizestr);
1787 			return (-1);
1788 		}
1789 
1790 		zc.zc_volsize = size;
1791 		zc.zc_volblocksize = blocksize;
1792 	}
1793 
1794 	/* create the dataset */
1795 
1796 	ret = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc);
1797 
1798 	if (ret == 0 && type == ZFS_TYPE_VOLUME)
1799 		ret = zvol_create_link(path);
1800 
1801 	/* check for failure */
1802 	if (ret != 0) {
1803 		char parent[ZFS_MAXNAMELEN];
1804 		(void) parent_name(path, parent, sizeof (parent));
1805 
1806 		switch (errno) {
1807 		case ENOENT:
1808 			/*
1809 			 * The parent dataset has been deleted since our
1810 			 * previous check.
1811 			 */
1812 			zfs_error(dgettext(TEXT_DOMAIN,
1813 			    "cannot create '%s': no such parent '%s'"),
1814 			    path, parent);
1815 			break;
1816 
1817 		case EPERM:
1818 			/*
1819 			 * The user doesn't have permission to create a new
1820 			 * dataset here.
1821 			 */
1822 			zfs_error(dgettext(TEXT_DOMAIN,
1823 			    "cannot create '%s': permission denied"), path);
1824 			break;
1825 
1826 		case EDQUOT:
1827 		case ENOSPC:
1828 			/*
1829 			 * The parent dataset does not have enough free space
1830 			 * to create a new dataset.
1831 			 */
1832 			zfs_error(dgettext(TEXT_DOMAIN,
1833 			    "cannot create '%s': not enough space in '%s'"),
1834 			    path, parent);
1835 			break;
1836 
1837 		case EEXIST:
1838 			/*
1839 			 * The target dataset already exists.  We should have
1840 			 * caught this above, but there may be some unexplained
1841 			 * race condition.
1842 			 */
1843 			zfs_error(dgettext(TEXT_DOMAIN,
1844 			    "cannot create '%s': dataset exists"), path);
1845 			break;
1846 
1847 		case EINVAL:
1848 			/*
1849 			 * The target dataset does not support children.
1850 			 */
1851 			zfs_error(dgettext(TEXT_DOMAIN,
1852 			    "cannot create '%s': children unsupported in '%s'"),
1853 			    path, parent);
1854 			break;
1855 
1856 		case EDOM:
1857 			zfs_error(dgettext(TEXT_DOMAIN, "bad %s value '%s': "
1858 			    "must be power of 2 from %u to %uk"),
1859 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1860 			    blocksizestr ? blocksizestr : "<unknown>",
1861 			    (uint_t)SPA_MINBLOCKSIZE,
1862 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
1863 			break;
1864 #ifdef _ILP32
1865 		case EOVERFLOW:
1866 			/*
1867 			 * This platform can't address a volume this big.
1868 			 */
1869 			if (type == ZFS_TYPE_VOLUME) {
1870 				zfs_error(dgettext(TEXT_DOMAIN,
1871 				    "cannot create '%s': "
1872 				    "max volume size is 1TB on 32-bit systems"),
1873 				    path);
1874 				break;
1875 			}
1876 #endif
1877 
1878 		default:
1879 			zfs_baderror(errno);
1880 		}
1881 
1882 		return (-1);
1883 	}
1884 
1885 	return (0);
1886 }
1887 
1888 /*
1889  * Destroys the given dataset.  The caller must make sure that the filesystem
1890  * isn't mounted, and that there are no active dependents.
1891  */
1892 int
1893 zfs_destroy(zfs_handle_t *zhp)
1894 {
1895 	zfs_cmd_t zc = { 0 };
1896 	int ret;
1897 
1898 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1899 
1900 	/*
1901 	 * We use the check for 'zfs_volblocksize' instead of ZFS_TYPE_VOLUME
1902 	 * so that we do the right thing for snapshots of volumes.
1903 	 */
1904 	if (zhp->zfs_volblocksize != 0) {
1905 		if (zvol_remove_link(zhp->zfs_name) != 0)
1906 			return (-1);
1907 
1908 		zc.zc_objset_type = DMU_OST_ZVOL;
1909 	} else {
1910 		zc.zc_objset_type = DMU_OST_ZFS;
1911 	}
1912 
1913 	ret = ioctl(zfs_fd, ZFS_IOC_DESTROY, &zc);
1914 
1915 	if (ret != 0) {
1916 		switch (errno) {
1917 
1918 		case EPERM:
1919 			/*
1920 			 * We don't have permission to destroy this dataset.
1921 			 */
1922 			zfs_error(dgettext(TEXT_DOMAIN,
1923 			    "cannot destroy '%s': permission denied"),
1924 			    zhp->zfs_name);
1925 			break;
1926 
1927 		case ENOENT:
1928 			/*
1929 			 * We've hit a race condition where the dataset has been
1930 			 * destroyed since we opened it.
1931 			 */
1932 			zfs_error(dgettext(TEXT_DOMAIN,
1933 			    "cannot destroy '%s': no such %s"),
1934 			    zhp->zfs_name, zfs_type_to_name(zhp->zfs_type));
1935 			break;
1936 
1937 		case EBUSY:
1938 			/*
1939 			 * Even if we destroy all children, there is a chance we
1940 			 * can hit this case if:
1941 			 *
1942 			 * 	- A child dataset has since been created
1943 			 * 	- A filesystem is mounted
1944 			 *
1945 			 * This error message is awful, but hopefully we've
1946 			 * already caught the common cases (and aborted more
1947 			 * appropriately) before calling this function.  There's
1948 			 * nothing else we can do at this point.
1949 			 */
1950 			zfs_error(dgettext(TEXT_DOMAIN,
1951 			    "cannot destroy '%s': %s is busy"),
1952 			    zhp->zfs_name, zfs_type_to_name(zhp->zfs_type));
1953 			break;
1954 
1955 		default:
1956 			zfs_baderror(errno);
1957 		}
1958 
1959 		return (-1);
1960 	}
1961 
1962 	remove_mountpoint(zhp);
1963 
1964 	return (0);
1965 }
1966 
1967 /*
1968  * Clones the given dataset.  The target must be of the same type as the source.
1969  */
1970 int
1971 zfs_clone(zfs_handle_t *zhp, const char *target)
1972 {
1973 	char reason[64];
1974 	zfs_cmd_t zc = { 0 };
1975 	char parent[ZFS_MAXNAMELEN];
1976 	int ret;
1977 
1978 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1979 
1980 	/* validate the target name */
1981 	if (!zfs_validate_name(target, ZFS_TYPE_FILESYSTEM, reason,
1982 	    sizeof (reason))) {
1983 		zfs_error(dgettext(TEXT_DOMAIN,
1984 		    "cannot create '%s': %s in filesystem name"), target,
1985 		    reason, zfs_type_to_name(ZFS_TYPE_FILESYSTEM));
1986 		return (-1);
1987 	}
1988 
1989 	/* validate parents exist */
1990 	if (check_parents(target, zhp->zfs_type) != 0)
1991 		return (-1);
1992 
1993 	(void) parent_name(target, parent, sizeof (parent));
1994 
1995 	/* do the clone */
1996 	if (zhp->zfs_volblocksize != 0)
1997 		zc.zc_objset_type = DMU_OST_ZVOL;
1998 	else
1999 		zc.zc_objset_type = DMU_OST_ZFS;
2000 
2001 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
2002 	(void) strlcpy(zc.zc_filename, zhp->zfs_name, sizeof (zc.zc_filename));
2003 	ret = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc);
2004 
2005 	if (ret != 0) {
2006 		switch (errno) {
2007 		case EPERM:
2008 			/*
2009 			 * The user doesn't have permission to create the clone.
2010 			 */
2011 			zfs_error(dgettext(TEXT_DOMAIN,
2012 			    "cannot create '%s': permission denied"),
2013 			    target);
2014 			break;
2015 
2016 		case ENOENT:
2017 			/*
2018 			 * The parent doesn't exist.  We should have caught this
2019 			 * above, but there may a race condition that has since
2020 			 * destroyed the parent.
2021 			 *
2022 			 * At this point, we don't know whether it's the source
2023 			 * that doesn't exist anymore, or whether the target
2024 			 * dataset doesn't exist.
2025 			 */
2026 			zfs_error(dgettext(TEXT_DOMAIN,
2027 			    "cannot create '%s': no such parent '%s'"),
2028 			    target, parent);
2029 			break;
2030 
2031 		case EDQUOT:
2032 		case ENOSPC:
2033 			/*
2034 			 * There is not enough space in the target dataset
2035 			 */
2036 			zfs_error(dgettext(TEXT_DOMAIN,
2037 			    "cannot create '%s': not enough space in '%s'"),
2038 			    target, parent);
2039 			break;
2040 
2041 		case EEXIST:
2042 			/*
2043 			 * The target already exists.
2044 			 */
2045 			zfs_error(dgettext(TEXT_DOMAIN,
2046 			    "cannot create '%s': dataset exists"), target);
2047 			break;
2048 
2049 		case EXDEV:
2050 			/*
2051 			 * The source and target pools differ.
2052 			 */
2053 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
2054 			    "source and target pools differ"), target);
2055 			break;
2056 
2057 		default:
2058 			zfs_baderror(errno);
2059 		}
2060 	} else if (zhp->zfs_volblocksize != 0) {
2061 		ret = zvol_create_link(target);
2062 	}
2063 
2064 	return (ret);
2065 }
2066 
2067 /*
2068  * Takes a snapshot of the given dataset
2069  */
2070 int
2071 zfs_snapshot(const char *path)
2072 {
2073 	char reason[64];
2074 	const char *delim;
2075 	char *parent;
2076 	zfs_handle_t *zhp;
2077 	zfs_cmd_t zc = { 0 };
2078 	int ret;
2079 
2080 	/* validate the snapshot name */
2081 	if (!zfs_validate_name(path, ZFS_TYPE_SNAPSHOT, reason,
2082 	    sizeof (reason))) {
2083 		zfs_error(dgettext(TEXT_DOMAIN,
2084 		    "cannot snapshot '%s': %s in snapshot name"), path,
2085 		    reason);
2086 		return (-1);
2087 	}
2088 
2089 	/* make sure we have a snapshot */
2090 	if ((delim = strchr(path, '@')) == NULL) {
2091 		zfs_error(dgettext(TEXT_DOMAIN,
2092 		    "cannot snapshot '%s': missing '@' delim in snapshot "
2093 		    "name"), path);
2094 		zfs_error(dgettext(TEXT_DOMAIN,
2095 		    "use 'zfs create' to create a filesystem"));
2096 		return (-1);
2097 	}
2098 
2099 	/* make sure the parent exists and is of the appropriate type */
2100 	parent = zfs_malloc(delim - path + 1);
2101 	(void) strncpy(parent, path, delim - path);
2102 	parent[delim - path] = '\0';
2103 
2104 	if ((zhp = zfs_open(parent, ZFS_TYPE_FILESYSTEM |
2105 	    ZFS_TYPE_VOLUME)) == NULL) {
2106 		free(parent);
2107 		return (-1);
2108 	}
2109 
2110 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2111 
2112 	if (zhp->zfs_type == ZFS_TYPE_VOLUME)
2113 		zc.zc_objset_type = DMU_OST_ZVOL;
2114 	else
2115 		zc.zc_objset_type = DMU_OST_ZFS;
2116 
2117 	ret = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc);
2118 
2119 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
2120 		ret = zvol_create_link(path);
2121 		if (ret != 0)
2122 			(void) ioctl(zfs_fd, ZFS_IOC_DESTROY, &zc);
2123 	}
2124 
2125 	if (ret != 0) {
2126 		switch (errno) {
2127 		case EPERM:
2128 			/*
2129 			 * User doesn't have permission to create a snapshot
2130 			 */
2131 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
2132 			    "permission denied"), path);
2133 			break;
2134 
2135 		case EDQUOT:
2136 		case ENOSPC:
2137 			/*
2138 			 * Out of space in parent.
2139 			 */
2140 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
2141 			    "not enough space in '%s'"), path, parent);
2142 			break;
2143 
2144 		case EEXIST:
2145 			/*
2146 			 * Snapshot already exists.
2147 			 */
2148 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create '%s': "
2149 			    "snapshot exists"), path);
2150 			break;
2151 
2152 		case ENOENT:
2153 			/*
2154 			 * Shouldn't happen because we verified the parent
2155 			 * above.  But there may be a race condition where it
2156 			 * has since been removed.
2157 			 */
2158 			zfs_error(dgettext(TEXT_DOMAIN, "cannot open '%s': "
2159 			    "no such %s"), parent,
2160 			    zfs_type_to_name(zhp->zfs_type));
2161 			break;
2162 
2163 		default:
2164 			zfs_baderror(errno);
2165 		}
2166 	}
2167 
2168 	free(parent);
2169 	zfs_close(zhp);
2170 
2171 	return (ret);
2172 }
2173 
2174 /*
2175  * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
2176  */
2177 int
2178 zfs_backup(zfs_handle_t *zhp_to, zfs_handle_t *zhp_from)
2179 {
2180 	zfs_cmd_t zc = { 0 };
2181 	int ret;
2182 
2183 	/* do the ioctl() */
2184 	(void) strlcpy(zc.zc_name, zhp_to->zfs_name, sizeof (zc.zc_name));
2185 	if (zhp_from) {
2186 		(void) strlcpy(zc.zc_prop_value, zhp_from->zfs_name,
2187 		    sizeof (zc.zc_name));
2188 	} else {
2189 		zc.zc_prop_value[0] = '\0';
2190 	}
2191 	zc.zc_cookie = STDOUT_FILENO;
2192 
2193 	ret = ioctl(zfs_fd, ZFS_IOC_SENDBACKUP, &zc);
2194 	if (ret != 0) {
2195 		switch (errno) {
2196 		case EPERM:
2197 			/*
2198 			 * User doesn't have permission to do a backup
2199 			 */
2200 			zfs_error(dgettext(TEXT_DOMAIN, "cannot backup '%s': "
2201 			    "permission denied"), zhp_to->zfs_name);
2202 			break;
2203 
2204 		case EXDEV:
2205 			zfs_error(dgettext(TEXT_DOMAIN,
2206 			    "cannot do incremental backup from %s:\n"
2207 			    "it is not an earlier snapshot from the "
2208 			    "same fs as %s"),
2209 			    zhp_from->zfs_name, zhp_to->zfs_name);
2210 			break;
2211 
2212 		case ENOENT:
2213 			/*
2214 			 * Shouldn't happen because we verified the parent
2215 			 * above.  But there may be a race condition where it
2216 			 * has since been removed.
2217 			 */
2218 			zfs_error(dgettext(TEXT_DOMAIN, "cannot open: "
2219 			    "no such snapshot"));
2220 			break;
2221 
2222 		case EDQUOT:
2223 		case EFBIG:
2224 		case EIO:
2225 		case ENOLINK:
2226 		case ENOSPC:
2227 		case ENOSTR:
2228 		case ENXIO:
2229 		case EPIPE:
2230 		case ERANGE:
2231 		case EFAULT:
2232 		case EROFS:
2233 			zfs_error(dgettext(TEXT_DOMAIN,
2234 			    "cannot write backup stream: %s"),
2235 			    strerror(errno));
2236 			break;
2237 
2238 		case EINTR:
2239 			zfs_error(dgettext(TEXT_DOMAIN,
2240 			    "backup failed: signal recieved"));
2241 			break;
2242 
2243 		default:
2244 			zfs_baderror(errno);
2245 		}
2246 	}
2247 
2248 	return (ret);
2249 }
2250 
2251 /*
2252  * Restores a backup of tosnap from stdin.
2253  */
2254 int
2255 zfs_restore(const char *tosnap, int isprefix, int verbose, int dryrun)
2256 {
2257 	zfs_cmd_t zc = { 0 };
2258 	time_t begin_time;
2259 	int ioctl_err, err, bytes, size;
2260 	char *cp;
2261 	dmu_replay_record_t drr;
2262 	struct drr_begin *drrb = &zc.zc_begin_record;
2263 
2264 	begin_time = time(NULL);
2265 
2266 	/* trim off snapname, if any */
2267 	(void) strcpy(zc.zc_name, tosnap);
2268 	cp = strchr(zc.zc_name, '@');
2269 	if (cp)
2270 		*cp = '\0';
2271 
2272 	/* read in the BEGIN record */
2273 	cp = (char *)&drr;
2274 	bytes = 0;
2275 	do {
2276 		size = read(STDIN_FILENO, cp, sizeof (drr) - bytes);
2277 		cp += size;
2278 		bytes += size;
2279 	} while (size > 0);
2280 
2281 	if (size < 0 || bytes != sizeof (drr)) {
2282 		zfs_error(dgettext(TEXT_DOMAIN,
2283 		    "cannot restore: invalid backup stream "
2284 		    "(couldn't read first record)"));
2285 		return (-1);
2286 	}
2287 
2288 	zc.zc_begin_record = drr.drr_u.drr_begin;
2289 
2290 	if (drrb->drr_magic != DMU_BACKUP_MAGIC &&
2291 	    drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) {
2292 		zfs_error(dgettext(TEXT_DOMAIN,
2293 		    "cannot restore: invalid backup stream "
2294 		    "(invalid magic number)"));
2295 		return (-1);
2296 	}
2297 
2298 	if (drrb->drr_version != DMU_BACKUP_VERSION &&
2299 	    drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) {
2300 		if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
2301 			drrb->drr_version = BSWAP_64(drrb->drr_version);
2302 		zfs_error(dgettext(TEXT_DOMAIN,
2303 		    "cannot restore: only backup version 0x%llx is supported, "
2304 		    "stream is version %llx."),
2305 		    DMU_BACKUP_VERSION, drrb->drr_version);
2306 		return (-1);
2307 	}
2308 
2309 	/*
2310 	 * Determine name of destination snapshot.
2311 	 */
2312 	(void) strcpy(drrb->drr_toname, tosnap);
2313 	if (isprefix) {
2314 		if (strchr(tosnap, '@') != NULL) {
2315 			zfs_error(dgettext(TEXT_DOMAIN,
2316 			    "cannot restore: "
2317 			    "argument to -d must be a filesystem"));
2318 			return (-1);
2319 		}
2320 
2321 		cp = strchr(drr.drr_u.drr_begin.drr_toname, '/');
2322 		if (cp == NULL)
2323 			cp = drr.drr_u.drr_begin.drr_toname;
2324 		else
2325 			cp++;
2326 
2327 		(void) strcat(drrb->drr_toname, "/");
2328 		(void) strcat(drrb->drr_toname, cp);
2329 	} else if (strchr(tosnap, '@') == NULL) {
2330 		/*
2331 		 * they specified just a filesystem; tack on the
2332 		 * snapname from the backup.
2333 		 */
2334 		cp = strchr(drr.drr_u.drr_begin.drr_toname, '@');
2335 		if (cp == NULL || strlen(tosnap) + strlen(cp) >= MAXNAMELEN) {
2336 			zfs_error(dgettext(TEXT_DOMAIN,
2337 			    "cannot restore: invalid backup stream "
2338 			    "(invalid snapshot name)"));
2339 			return (-1);
2340 		}
2341 		(void) strcat(drrb->drr_toname, cp);
2342 	}
2343 
2344 	if (drrb->drr_fromguid) {
2345 		zfs_handle_t *h;
2346 		/* incremental backup stream */
2347 
2348 		/* do the ioctl to the containing fs */
2349 		(void) strcpy(zc.zc_name, drrb->drr_toname);
2350 		cp = strchr(zc.zc_name, '@');
2351 		*cp = '\0';
2352 
2353 		/* make sure destination fs exists */
2354 		h = zfs_open(zc.zc_name, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2355 		if (h == NULL) {
2356 			zfs_error(dgettext(TEXT_DOMAIN,
2357 			    "cannot restore incrememtal backup: destination\n"
2358 			    "filesystem %s does not exist"),
2359 			    zc.zc_name);
2360 			return (-1);
2361 		}
2362 		if (!dryrun) {
2363 			/* unmount destination fs or remove device link. */
2364 			if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
2365 				(void) zfs_unmount(h, NULL, 0);
2366 			} else {
2367 				(void) zvol_remove_link(h->zfs_name);
2368 			}
2369 		}
2370 		zfs_close(h);
2371 	} else {
2372 		/* full backup stream */
2373 
2374 		(void) strcpy(zc.zc_name, drrb->drr_toname);
2375 
2376 		/* make sure they aren't trying to restore into the root */
2377 		if (strchr(zc.zc_name, '/') == NULL) {
2378 			cp = strchr(zc.zc_name, '@');
2379 			if (cp)
2380 				*cp = '\0';
2381 			zfs_error(dgettext(TEXT_DOMAIN,
2382 			    "cannot restore: destination fs %s already exists"),
2383 			    zc.zc_name);
2384 			return (-1);
2385 		}
2386 
2387 		if (isprefix) {
2388 			zfs_handle_t *h;
2389 
2390 			/* make sure prefix exists */
2391 			h = zfs_open(tosnap, ZFS_TYPE_FILESYSTEM |
2392 			    ZFS_TYPE_VOLUME);
2393 			if (h == NULL) {
2394 				zfs_error(dgettext(TEXT_DOMAIN,
2395 				    "cannot restore: "
2396 				    "filesystem %s does not exist"),
2397 				    tosnap);
2398 				return (-1);
2399 			}
2400 
2401 			/* create any necessary ancestors up to prefix */
2402 			zc.zc_objset_type = DMU_OST_ZFS;
2403 			/*
2404 			 * zc.zc_name is now the full name of the snap
2405 			 * we're restoring into
2406 			 */
2407 			cp = zc.zc_name + strlen(tosnap) + 1;
2408 			while (cp = strchr(cp, '/')) {
2409 				*cp = '\0';
2410 				err = ioctl(zfs_fd, ZFS_IOC_CREATE, &zc);
2411 				if (err && errno != ENOENT && errno != EEXIST) {
2412 					zfs_error(dgettext(TEXT_DOMAIN,
2413 					    "cannot restore: "
2414 					    "couldn't create ancestor %s"),
2415 					    zc.zc_name);
2416 					return (-1);
2417 				}
2418 				*cp = '/';
2419 				cp++;
2420 			}
2421 		}
2422 
2423 		/* Make sure destination fs does not exist */
2424 		cp = strchr(zc.zc_name, '@');
2425 		*cp = '\0';
2426 		if (ioctl(zfs_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0) {
2427 			zfs_error(dgettext(TEXT_DOMAIN,
2428 			    "cannot restore full backup: "
2429 			    "destination filesystem %s already exists"),
2430 			    zc.zc_name);
2431 			return (-1);
2432 		}
2433 
2434 		/* Do the recvbackup ioctl to the fs's parent. */
2435 		cp = strrchr(zc.zc_name, '/');
2436 		*cp = '\0';
2437 	}
2438 
2439 	(void) strcpy(zc.zc_prop_value, tosnap);
2440 	zc.zc_cookie = STDIN_FILENO;
2441 	zc.zc_intsz = isprefix;
2442 	if (verbose) {
2443 		(void) printf("%s %s backup of %s into %s\n",
2444 		    dryrun ? "would restore" : "restoring",
2445 		    drrb->drr_fromguid ? "incremental" : "full",
2446 		    drr.drr_u.drr_begin.drr_toname,
2447 		    zc.zc_begin_record.drr_toname);
2448 		(void) fflush(stdout);
2449 	}
2450 	if (dryrun)
2451 		return (0);
2452 	err = ioctl_err = ioctl(zfs_fd, ZFS_IOC_RECVBACKUP, &zc);
2453 	if (ioctl_err != 0) {
2454 		switch (errno) {
2455 		case ENODEV:
2456 			zfs_error(dgettext(TEXT_DOMAIN,
2457 			    "cannot restore: "
2458 			    "most recent snapshot does not "
2459 			    "match incremental backup source"));
2460 			break;
2461 		case ETXTBSY:
2462 			zfs_error(dgettext(TEXT_DOMAIN,
2463 			    "cannot restore: "
2464 			    "destination has been modified since "
2465 			    "most recent snapshot --\n"
2466 			    "use 'zfs rollback' to discard changes"));
2467 			break;
2468 		case EEXIST:
2469 			if (drrb->drr_fromguid == 0) {
2470 				/* it's the containing fs that exists */
2471 				cp = strchr(drrb->drr_toname, '@');
2472 				*cp = '\0';
2473 			}
2474 			zfs_error(dgettext(TEXT_DOMAIN,
2475 			    "cannot restore to %s: destination already exists"),
2476 			    drrb->drr_toname);
2477 			break;
2478 		case ENOENT:
2479 			zfs_error(dgettext(TEXT_DOMAIN,
2480 			    "cannot restore: destination does not exist"));
2481 			break;
2482 		case EBUSY:
2483 			zfs_error(dgettext(TEXT_DOMAIN,
2484 			    "cannot restore: destination is in use"));
2485 			break;
2486 		case ENOSPC:
2487 			zfs_error(dgettext(TEXT_DOMAIN,
2488 			    "cannot restore: out of space"));
2489 			break;
2490 		case EDQUOT:
2491 			zfs_error(dgettext(TEXT_DOMAIN,
2492 			    "cannot restore: quota exceeded"));
2493 			break;
2494 		case EINTR:
2495 			zfs_error(dgettext(TEXT_DOMAIN,
2496 			    "restore failed: signal recieved"));
2497 			break;
2498 		case EINVAL:
2499 			zfs_error(dgettext(TEXT_DOMAIN,
2500 			    "cannot restore: invalid backup stream"));
2501 			break;
2502 		case EPERM:
2503 			zfs_error(dgettext(TEXT_DOMAIN,
2504 			    "cannot restore: permission denied"));
2505 			break;
2506 		default:
2507 			zfs_baderror(errno);
2508 		}
2509 	}
2510 
2511 	/*
2512 	 * Mount or recreate the /dev links for the target filesystem
2513 	 * (if created, or if we tore them down to do an incremental
2514 	 * restore), and the /dev links for the new snapshot (if
2515 	 * created).
2516 	 */
2517 	cp = strchr(drrb->drr_toname, '@');
2518 	if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) {
2519 		zfs_handle_t *h;
2520 
2521 		*cp = '\0';
2522 		h = zfs_open(drrb->drr_toname,
2523 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2524 		*cp = '@';
2525 		if (h) {
2526 			if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
2527 				err = zfs_mount(h, NULL, 0);
2528 			} else {
2529 				err = zvol_create_link(h->zfs_name);
2530 				if (err == 0 && ioctl_err == 0) {
2531 					err =
2532 					    zvol_create_link(drrb->drr_toname);
2533 				}
2534 			}
2535 			zfs_close(h);
2536 		}
2537 	}
2538 
2539 	if (err || ioctl_err)
2540 		return (-1);
2541 
2542 	if (verbose) {
2543 		char buf1[64];
2544 		char buf2[64];
2545 		uint64_t bytes = zc.zc_cookie;
2546 		time_t delta = time(NULL) - begin_time;
2547 		if (delta == 0)
2548 			delta = 1;
2549 		zfs_nicenum(bytes, buf1, sizeof (buf1));
2550 		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
2551 
2552 		(void) printf("restored %sb backup in %lu seconds (%sb/sec)\n",
2553 		    buf1, delta, buf2);
2554 	}
2555 	return (0);
2556 }
2557 
2558 /*
2559  * Rollback the given dataset to the previous snapshot.  It is up to the caller
2560  * to verify that there is a previous snapshot available.
2561  */
2562 int
2563 zfs_rollback(zfs_handle_t *zhp)
2564 {
2565 	int ret;
2566 	zfs_cmd_t zc = { 0 };
2567 
2568 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
2569 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
2570 
2571 	if (zhp->zfs_type == ZFS_TYPE_VOLUME &&
2572 	    zvol_remove_link(zhp->zfs_name) != 0)
2573 		return (-1);
2574 
2575 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2576 
2577 	if (zhp->zfs_volblocksize != 0)
2578 		zc.zc_objset_type = DMU_OST_ZVOL;
2579 	else
2580 		zc.zc_objset_type = DMU_OST_ZFS;
2581 
2582 	/*
2583 	 * We rely on the consumer to verify that there are no newer snapshots
2584 	 * for the given dataset.  Given these constraints, we can simply pass
2585 	 * the name on to the ioctl() call.  There is still an unlikely race
2586 	 * condition where the user has taken a snapshot since we verified that
2587 	 * this was the most recent.
2588 	 */
2589 	if ((ret = ioctl(zfs_fd, ZFS_IOC_ROLLBACK, &zc)) != 0) {
2590 		switch (errno) {
2591 		case EPERM:
2592 			/*
2593 			 * The user doesn't have permission to rollback the
2594 			 * given dataset.
2595 			 */
2596 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': "
2597 			    "permission denied"), zhp->zfs_name);
2598 			break;
2599 
2600 		case EDQUOT:
2601 		case ENOSPC:
2602 			/*
2603 			 * The parent dataset doesn't have enough space to
2604 			 * rollback to the last snapshot.
2605 			 */
2606 			{
2607 				char parent[ZFS_MAXNAMELEN];
2608 				(void) parent_name(zhp->zfs_name, parent,
2609 				    sizeof (parent));
2610 				zfs_error(dgettext(TEXT_DOMAIN, "cannot "
2611 				    "rollback '%s': out of space"), parent);
2612 			}
2613 			break;
2614 
2615 		case ENOENT:
2616 			/*
2617 			 * The dataset doesn't exist.  This shouldn't happen
2618 			 * except in race conditions.
2619 			 */
2620 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': "
2621 			    "no such %s"), zhp->zfs_name,
2622 			    zfs_type_to_name(zhp->zfs_type));
2623 			break;
2624 
2625 		case EBUSY:
2626 			/*
2627 			 * The filesystem is busy.  This should have been caught
2628 			 * by the caller before getting here, but there may be
2629 			 * an unexpected problem.
2630 			 */
2631 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rollback '%s': "
2632 			    "%s is busy"), zhp->zfs_name,
2633 			    zfs_type_to_name(zhp->zfs_type));
2634 			break;
2635 
2636 		default:
2637 			zfs_baderror(errno);
2638 		}
2639 	} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
2640 		ret = zvol_create_link(zhp->zfs_name);
2641 	}
2642 
2643 	return (ret);
2644 }
2645 
2646 /*
2647  * Iterate over all dependents for a given dataset.  This includes both
2648  * hierarchical dependents (children) and data dependents (snapshots and
2649  * clones).  The bulk of the processing occurs in get_dependents() in
2650  * libzfs_graph.c.
2651  */
2652 int
2653 zfs_iter_dependents(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2654 {
2655 	char **dependents;
2656 	size_t count;
2657 	int i;
2658 	zfs_handle_t *child;
2659 	int ret = 0;
2660 
2661 	dependents = get_dependents(zhp->zfs_name, &count);
2662 	for (i = 0; i < count; i++) {
2663 		if ((child = make_dataset_handle(dependents[i])) == NULL)
2664 			continue;
2665 
2666 		if ((ret = func(child, data)) != 0)
2667 			break;
2668 	}
2669 
2670 	for (i = 0; i < count; i++)
2671 		free(dependents[i]);
2672 	free(dependents);
2673 
2674 	return (ret);
2675 }
2676 
2677 /*
2678  * Renames the given dataset.
2679  */
2680 int
2681 zfs_rename(zfs_handle_t *zhp, const char *target)
2682 {
2683 	int ret;
2684 	zfs_cmd_t zc = { 0 };
2685 	char reason[64];
2686 	char *delim;
2687 	prop_changelist_t *cl;
2688 	char parent[ZFS_MAXNAMELEN];
2689 
2690 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2691 	(void) strlcpy(zc.zc_prop_value, target, sizeof (zc.zc_prop_value));
2692 
2693 	/* if we have the same exact name, just return success */
2694 	if (strcmp(zhp->zfs_name, target) == 0)
2695 		return (0);
2696 
2697 	/*
2698 	 * Make sure the target name is valid
2699 	 */
2700 	if (!zfs_validate_name(target, zhp->zfs_type, reason,
2701 	    sizeof (reason))) {
2702 		zfs_error(dgettext(TEXT_DOMAIN,
2703 		    "cannot create '%s': %s in %s name"), target, reason,
2704 		    zfs_type_to_name(zhp->zfs_type));
2705 		return (-1);
2706 	}
2707 
2708 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2709 		if ((delim = strchr(target, '@')) == NULL) {
2710 			zfs_error(dgettext(TEXT_DOMAIN,
2711 			    "cannot rename to '%s': not a snapshot"), target);
2712 			return (-1);
2713 		}
2714 
2715 		/*
2716 		 * Make sure we're renaming within the same dataset.
2717 		 */
2718 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
2719 		    zhp->zfs_name[delim - target] != '@') {
2720 			zfs_error(dgettext(TEXT_DOMAIN,
2721 			    "cannot rename to '%s': snapshots must be part "
2722 			    "of same dataset"), target);
2723 			return (-1);
2724 		}
2725 
2726 		(void) strncpy(parent, target, delim - target);
2727 		parent[delim - target] = '\0';
2728 	} else {
2729 		/* validate parents */
2730 		if (check_parents(target, zhp->zfs_type) != 0)
2731 			return (-1);
2732 
2733 		(void) parent_name(target, parent, sizeof (parent));
2734 
2735 		/* make sure we're in the same pool */
2736 		verify((delim = strchr(target, '/')) != NULL);
2737 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
2738 		    zhp->zfs_name[delim - target] != '/') {
2739 			zfs_error(dgettext(TEXT_DOMAIN,
2740 			    "cannot rename to '%s': "
2741 			    "datasets must be within same pool"), target);
2742 			return (-1);
2743 		}
2744 	}
2745 
2746 	if (getzoneid() == GLOBAL_ZONEID &&
2747 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2748 		zfs_error(dgettext(TEXT_DOMAIN, "cannot rename %s, "
2749 		    "dataset is used in a non-global zone"), zhp->zfs_name);
2750 		return (-1);
2751 	}
2752 
2753 	if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
2754 		return (1);
2755 
2756 	if (changelist_haszonedchild(cl)) {
2757 		zfs_error(dgettext(TEXT_DOMAIN,
2758 		    "cannot rename '%s': child dataset with inherited "
2759 		    "mountpoint is used in a non-global zone"), zhp->zfs_name);
2760 		ret = -1;
2761 		goto error;
2762 	}
2763 
2764 	if ((ret = changelist_prefix(cl)) != 0)
2765 		goto error;
2766 
2767 	if (zhp->zfs_volblocksize != 0)
2768 		zc.zc_objset_type = DMU_OST_ZVOL;
2769 	else
2770 		zc.zc_objset_type = DMU_OST_ZFS;
2771 
2772 	if ((ret = ioctl(zfs_fd, ZFS_IOC_RENAME, &zc)) != 0) {
2773 		switch (errno) {
2774 		case EPERM:
2775 			/*
2776 			 * The user doesn't have permission to rename the
2777 			 * given dataset.
2778 			 */
2779 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s': "
2780 			    "permission denied"), zhp->zfs_name);
2781 			break;
2782 
2783 		case EDQUOT:
2784 		case ENOSPC:
2785 			/*
2786 			 * Not enough space in the parent dataset.
2787 			 */
2788 			zfs_error(dgettext(TEXT_DOMAIN, "cannot "
2789 			    "rename '%s': not enough space in '%s'"),
2790 			    zhp->zfs_name, parent);
2791 			break;
2792 
2793 		case ENOENT:
2794 			/*
2795 			 * The destination doesn't exist.
2796 			 */
2797 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s' "
2798 			    "to '%s': destination doesn't exist"),
2799 			    zhp->zfs_name, target);
2800 			break;
2801 
2802 		case EEXIST:
2803 			/*
2804 			 * The destination already exists.
2805 			 */
2806 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s' "
2807 			    "to '%s': destination already exists"),
2808 			    zhp->zfs_name, target);
2809 			break;
2810 
2811 		case EBUSY:
2812 			/*
2813 			 * The filesystem is busy.  This should have been caught
2814 			 * by the caller before getting here, but there may be
2815 			 * an unexpected problem.
2816 			 */
2817 			zfs_error(dgettext(TEXT_DOMAIN, "cannot rename '%s': "
2818 			    "%s is busy"), zhp->zfs_name,
2819 			    zfs_type_to_name(zhp->zfs_type));
2820 			break;
2821 
2822 		default:
2823 			zfs_baderror(errno);
2824 		}
2825 
2826 		/*
2827 		 * On failure, we still want to remount any filesystems that
2828 		 * were previously mounted, so we don't alter the system state.
2829 		 */
2830 		(void) changelist_postfix(cl);
2831 	} else {
2832 		changelist_rename(cl, zfs_get_name(zhp), target);
2833 
2834 		ret = changelist_postfix(cl);
2835 	}
2836 
2837 error:
2838 	changelist_free(cl);
2839 	return (ret);
2840 }
2841 
2842 /*
2843  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
2844  * poke devfsadm to create the /dev link, and then wait for the link to appear.
2845  */
2846 int
2847 zvol_create_link(const char *dataset)
2848 {
2849 	zfs_cmd_t zc = { 0 };
2850 	di_devlink_handle_t hdl;
2851 
2852 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
2853 
2854 	/*
2855 	 * Issue the appropriate ioctl.
2856 	 */
2857 	if (ioctl(zfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
2858 		switch (errno) {
2859 		case EPERM:
2860 			zfs_error(dgettext(TEXT_DOMAIN, "cannot create "
2861 			    "device links for '%s': permission denied"),
2862 			    dataset);
2863 			break;
2864 
2865 		case EEXIST:
2866 			/*
2867 			 * Silently ignore the case where the link already
2868 			 * exists.  This allows 'zfs volinit' to be run multiple
2869 			 * times without errors.
2870 			 */
2871 			return (0);
2872 
2873 		default:
2874 			zfs_baderror(errno);
2875 		}
2876 
2877 		return (-1);
2878 	}
2879 
2880 	/*
2881 	 * Call devfsadm and wait for the links to magically appear.
2882 	 */
2883 	if ((hdl = di_devlink_init(ZFS_DRIVER, DI_MAKE_LINK)) == NULL) {
2884 		zfs_error(dgettext(TEXT_DOMAIN,
2885 		    "cannot create device links for '%s'"), dataset);
2886 		(void) ioctl(zfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
2887 		return (-1);
2888 	} else {
2889 		(void) di_devlink_fini(&hdl);
2890 	}
2891 
2892 	return (0);
2893 }
2894 
2895 /*
2896  * Remove a minor node for the given zvol and the associated /dev links.
2897  */
2898 int
2899 zvol_remove_link(const char *dataset)
2900 {
2901 	zfs_cmd_t zc = { 0 };
2902 
2903 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
2904 
2905 	if (ioctl(zfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
2906 		switch (errno) {
2907 		case EPERM:
2908 			zfs_error(dgettext(TEXT_DOMAIN, "cannot remove "
2909 			    "device links for '%s': permission denied"),
2910 			    dataset);
2911 			break;
2912 
2913 		case EBUSY:
2914 			zfs_error(dgettext(TEXT_DOMAIN, "cannot remove "
2915 			    "device links for '%s': volume is in use"),
2916 			    dataset);
2917 			break;
2918 
2919 		case ENXIO:
2920 			/*
2921 			 * Silently ignore the case where the link no longer
2922 			 * exists, so that 'zfs volfini' can be run multiple
2923 			 * times without errors.
2924 			 */
2925 			return (0);
2926 
2927 		default:
2928 			zfs_baderror(errno);
2929 		}
2930 
2931 		return (-1);
2932 	}
2933 
2934 	return (0);
2935 }
2936