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