xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_dataset.c (revision afbc45417fe46ac092547adfe04ef4966809b862)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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 <stddef.h>
40 #include <zone.h>
41 #include <fcntl.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
44 #include <sys/mount.h>
45 #include <sys/avl.h>
46 #include <priv.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <stddef.h>
50 #include <ucred.h>
51 
52 #include <sys/spa.h>
53 #include <sys/zap.h>
54 #include <libzfs.h>
55 
56 #include "zfs_namecheck.h"
57 #include "zfs_prop.h"
58 #include "libzfs_impl.h"
59 #include "zfs_deleg.h"
60 
61 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
62 
63 /*
64  * Given a single type (not a mask of types), return the type in a human
65  * readable form.
66  */
67 const char *
68 zfs_type_to_name(zfs_type_t type)
69 {
70 	switch (type) {
71 	case ZFS_TYPE_FILESYSTEM:
72 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73 	case ZFS_TYPE_SNAPSHOT:
74 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75 	case ZFS_TYPE_VOLUME:
76 		return (dgettext(TEXT_DOMAIN, "volume"));
77 	}
78 
79 	return (NULL);
80 }
81 
82 /*
83  * Given a path and mask of ZFS types, return a string describing this dataset.
84  * This is used when we fail to open a dataset and we cannot get an exact type.
85  * We guess what the type would have been based on the path and the mask of
86  * acceptable types.
87  */
88 static const char *
89 path_to_str(const char *path, int types)
90 {
91 	/*
92 	 * When given a single type, always report the exact type.
93 	 */
94 	if (types == ZFS_TYPE_SNAPSHOT)
95 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96 	if (types == ZFS_TYPE_FILESYSTEM)
97 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98 	if (types == ZFS_TYPE_VOLUME)
99 		return (dgettext(TEXT_DOMAIN, "volume"));
100 
101 	/*
102 	 * The user is requesting more than one type of dataset.  If this is the
103 	 * case, consult the path itself.  If we're looking for a snapshot, and
104 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105 	 * snapshot attribute and try again.
106 	 */
107 	if (types & ZFS_TYPE_SNAPSHOT) {
108 		if (strchr(path, '@') != NULL)
109 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111 	}
112 
113 
114 	/*
115 	 * The user has requested either filesystems or volumes.
116 	 * We have no way of knowing a priori what type this would be, so always
117 	 * report it as "filesystem" or "volume", our two primitive types.
118 	 */
119 	if (types & ZFS_TYPE_FILESYSTEM)
120 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121 
122 	assert(types & ZFS_TYPE_VOLUME);
123 	return (dgettext(TEXT_DOMAIN, "volume"));
124 }
125 
126 /*
127  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128  * provide a more meaningful error message.  We place a more useful message in
129  * 'buf' detailing exactly why the name was not valid.
130  */
131 static int
132 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
133     boolean_t modifying)
134 {
135 	namecheck_err_t why;
136 	char what;
137 
138 	if (dataset_namecheck(path, &why, &what) != 0) {
139 		if (hdl != NULL) {
140 			switch (why) {
141 			case NAME_ERR_TOOLONG:
142 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 				    "name is too long"));
144 				break;
145 
146 			case NAME_ERR_LEADING_SLASH:
147 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 				    "leading slash in name"));
149 				break;
150 
151 			case NAME_ERR_EMPTY_COMPONENT:
152 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153 				    "empty component in name"));
154 				break;
155 
156 			case NAME_ERR_TRAILING_SLASH:
157 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158 				    "trailing slash in name"));
159 				break;
160 
161 			case NAME_ERR_INVALCHAR:
162 				zfs_error_aux(hdl,
163 				    dgettext(TEXT_DOMAIN, "invalid character "
164 				    "'%c' in name"), what);
165 				break;
166 
167 			case NAME_ERR_MULTIPLE_AT:
168 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169 				    "multiple '@' delimiters in name"));
170 				break;
171 
172 			case NAME_ERR_NOLETTER:
173 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174 				    "pool doesn't begin with a letter"));
175 				break;
176 
177 			case NAME_ERR_RESERVED:
178 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179 				    "name is reserved"));
180 				break;
181 
182 			case NAME_ERR_DISKLIKE:
183 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184 				    "reserved disk name"));
185 				break;
186 			}
187 		}
188 
189 		return (0);
190 	}
191 
192 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
193 		if (hdl != NULL)
194 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
195 			    "snapshot delimiter '@' in filesystem name"));
196 		return (0);
197 	}
198 
199 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
200 		if (hdl != NULL)
201 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
202 			    "missing '@' delimiter in snapshot name"));
203 		return (0);
204 	}
205 
206 	if (modifying && strchr(path, '%') != NULL) {
207 		if (hdl != NULL)
208 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
209 			    "invalid character %c in name"), '%');
210 		return (0);
211 	}
212 
213 	return (-1);
214 }
215 
216 int
217 zfs_name_valid(const char *name, zfs_type_t type)
218 {
219 	if (type == ZFS_TYPE_POOL)
220 		return (zpool_name_valid(NULL, B_FALSE, name));
221 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222 }
223 
224 /*
225  * This function takes the raw DSL properties, and filters out the user-defined
226  * properties into a separate nvlist.
227  */
228 static nvlist_t *
229 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
230 {
231 	libzfs_handle_t *hdl = zhp->zfs_hdl;
232 	nvpair_t *elem;
233 	nvlist_t *propval;
234 	nvlist_t *nvl;
235 
236 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
237 		(void) no_memory(hdl);
238 		return (NULL);
239 	}
240 
241 	elem = NULL;
242 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
243 		if (!zfs_prop_user(nvpair_name(elem)))
244 			continue;
245 
246 		verify(nvpair_value_nvlist(elem, &propval) == 0);
247 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
248 			nvlist_free(nvl);
249 			(void) no_memory(hdl);
250 			return (NULL);
251 		}
252 	}
253 
254 	return (nvl);
255 }
256 
257 /*
258  * Utility function to gather stats (objset and zpl) for the given object.
259  */
260 static int
261 get_stats(zfs_handle_t *zhp)
262 {
263 	zfs_cmd_t zc = { 0 };
264 	libzfs_handle_t *hdl = zhp->zfs_hdl;
265 	nvlist_t *allprops, *userprops;
266 
267 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
268 
269 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
270 		return (-1);
271 
272 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
273 		if (errno == ENOMEM) {
274 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
275 				zcmd_free_nvlists(&zc);
276 				return (-1);
277 			}
278 		} else {
279 			zcmd_free_nvlists(&zc);
280 			return (-1);
281 		}
282 	}
283 
284 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
285 
286 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
287 
288 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
289 		zcmd_free_nvlists(&zc);
290 		return (-1);
291 	}
292 
293 	zcmd_free_nvlists(&zc);
294 
295 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
296 		nvlist_free(allprops);
297 		return (-1);
298 	}
299 
300 	nvlist_free(zhp->zfs_props);
301 	nvlist_free(zhp->zfs_user_props);
302 
303 	zhp->zfs_props = allprops;
304 	zhp->zfs_user_props = userprops;
305 
306 	return (0);
307 }
308 
309 /*
310  * Refresh the properties currently stored in the handle.
311  */
312 void
313 zfs_refresh_properties(zfs_handle_t *zhp)
314 {
315 	(void) get_stats(zhp);
316 }
317 
318 /*
319  * Makes a handle from the given dataset name.  Used by zfs_open() and
320  * zfs_iter_* to create child handles on the fly.
321  */
322 zfs_handle_t *
323 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
324 {
325 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
326 	char *logstr;
327 
328 	if (zhp == NULL)
329 		return (NULL);
330 
331 	zhp->zfs_hdl = hdl;
332 
333 	/*
334 	 * Preserve history log string.
335 	 * any changes performed here will be
336 	 * logged as an internal event.
337 	 */
338 	logstr = zhp->zfs_hdl->libzfs_log_str;
339 	zhp->zfs_hdl->libzfs_log_str = NULL;
340 top:
341 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
342 
343 	if (get_stats(zhp) != 0) {
344 		zhp->zfs_hdl->libzfs_log_str = logstr;
345 		free(zhp);
346 		return (NULL);
347 	}
348 
349 	if (zhp->zfs_dmustats.dds_inconsistent) {
350 		zfs_cmd_t zc = { 0 };
351 
352 		/*
353 		 * If it is dds_inconsistent, then we've caught it in
354 		 * the middle of a 'zfs receive' or 'zfs destroy', and
355 		 * it is inconsistent from the ZPL's point of view, so
356 		 * can't be mounted.  However, it could also be that we
357 		 * have crashed in the middle of one of those
358 		 * operations, in which case we need to get rid of the
359 		 * inconsistent state.  We do that by either rolling
360 		 * back to the previous snapshot (which will fail if
361 		 * there is none), or destroying the filesystem.  Note
362 		 * that if we are still in the middle of an active
363 		 * 'receive' or 'destroy', then the rollback and destroy
364 		 * will fail with EBUSY and we will drive on as usual.
365 		 */
366 
367 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
368 
369 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
370 			(void) zvol_remove_link(hdl, zhp->zfs_name);
371 			zc.zc_objset_type = DMU_OST_ZVOL;
372 		} else {
373 			zc.zc_objset_type = DMU_OST_ZFS;
374 		}
375 
376 		/*
377 		 * If we can successfully destroy it, pretend that it
378 		 * never existed.
379 		 */
380 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
381 			zhp->zfs_hdl->libzfs_log_str = logstr;
382 			free(zhp);
383 			errno = ENOENT;
384 			return (NULL);
385 		}
386 		/* If we can successfully roll it back, reget the stats */
387 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
388 			goto top;
389 	}
390 
391 	/*
392 	 * We've managed to open the dataset and gather statistics.  Determine
393 	 * the high-level type.
394 	 */
395 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
396 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
397 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
398 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
399 	else
400 		abort();
401 
402 	if (zhp->zfs_dmustats.dds_is_snapshot)
403 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
404 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
405 		zhp->zfs_type = ZFS_TYPE_VOLUME;
406 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
407 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
408 	else
409 		abort();	/* we should never see any other types */
410 
411 	zhp->zfs_hdl->libzfs_log_str = logstr;
412 	return (zhp);
413 }
414 
415 /*
416  * Opens the given snapshot, filesystem, or volume.   The 'types'
417  * argument is a mask of acceptable types.  The function will print an
418  * appropriate error message and return NULL if it can't be opened.
419  */
420 zfs_handle_t *
421 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
422 {
423 	zfs_handle_t *zhp;
424 	char errbuf[1024];
425 
426 	(void) snprintf(errbuf, sizeof (errbuf),
427 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
428 
429 	/*
430 	 * Validate the name before we even try to open it.
431 	 */
432 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
433 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
434 		    "invalid dataset name"));
435 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
436 		return (NULL);
437 	}
438 
439 	/*
440 	 * Try to get stats for the dataset, which will tell us if it exists.
441 	 */
442 	errno = 0;
443 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
444 		(void) zfs_standard_error(hdl, errno, errbuf);
445 		return (NULL);
446 	}
447 
448 	if (!(types & zhp->zfs_type)) {
449 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
450 		zfs_close(zhp);
451 		return (NULL);
452 	}
453 
454 	return (zhp);
455 }
456 
457 /*
458  * Release a ZFS handle.  Nothing to do but free the associated memory.
459  */
460 void
461 zfs_close(zfs_handle_t *zhp)
462 {
463 	if (zhp->zfs_mntopts)
464 		free(zhp->zfs_mntopts);
465 	nvlist_free(zhp->zfs_props);
466 	nvlist_free(zhp->zfs_user_props);
467 	free(zhp);
468 }
469 
470 int
471 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
472 {
473 	char *pool_name;
474 	zpool_handle_t *zpool_handle;
475 	char *p;
476 
477 	pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN);
478 	if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name,
479 	    MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) {
480 		free(pool_name);
481 		return (-1);
482 	}
483 
484 	if (p = strchr(pool_name, '/'))
485 		*p = '\0';
486 	zpool_handle = zpool_open(zhp->zfs_hdl, pool_name);
487 	free(pool_name);
488 	if (zpool_handle == NULL)
489 		return (-1);
490 
491 	*spa_version = zpool_get_prop_int(zpool_handle,
492 	    ZPOOL_PROP_VERSION, NULL);
493 	zpool_close(zpool_handle);
494 	return (0);
495 }
496 
497 /*
498  * The choice of reservation property depends on the SPA version.
499  */
500 static int
501 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
502 {
503 	int spa_version;
504 
505 	if (zfs_spa_version(zhp, &spa_version) < 0)
506 		return (-1);
507 
508 	if (spa_version >= SPA_VERSION_REFRESERVATION)
509 		*resv_prop = ZFS_PROP_REFRESERVATION;
510 	else
511 		*resv_prop = ZFS_PROP_RESERVATION;
512 
513 	return (0);
514 }
515 
516 /*
517  * Given an nvlist of properties to set, validates that they are correct, and
518  * parses any numeric properties (index, boolean, etc) if they are specified as
519  * strings.
520  */
521 static nvlist_t *
522 zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
523     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
524 {
525 	nvpair_t *elem;
526 	uint64_t intval;
527 	char *strval;
528 	zfs_prop_t prop;
529 	nvlist_t *ret;
530 	int chosen_normal = -1;
531 	int chosen_utf = -1;
532 
533 	if (type == ZFS_TYPE_SNAPSHOT) {
534 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
535 		    "snapshot properties cannot be modified"));
536 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
537 		return (NULL);
538 	}
539 
540 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
541 		(void) no_memory(hdl);
542 		return (NULL);
543 	}
544 
545 	elem = NULL;
546 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
547 		const char *propname = nvpair_name(elem);
548 
549 		/*
550 		 * Make sure this property is valid and applies to this type.
551 		 */
552 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
553 			if (!zfs_prop_user(propname)) {
554 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 				    "invalid property '%s'"), propname);
556 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
557 				goto error;
558 			}
559 
560 			/*
561 			 * If this is a user property, make sure it's a
562 			 * string, and that it's less than ZAP_MAXNAMELEN.
563 			 */
564 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
565 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
566 				    "'%s' must be a string"), propname);
567 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
568 				goto error;
569 			}
570 
571 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
572 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
573 				    "property name '%s' is too long"),
574 				    propname);
575 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
576 				goto error;
577 			}
578 
579 			(void) nvpair_value_string(elem, &strval);
580 			if (nvlist_add_string(ret, propname, strval) != 0) {
581 				(void) no_memory(hdl);
582 				goto error;
583 			}
584 			continue;
585 		}
586 
587 		if (!zfs_prop_valid_for_type(prop, type)) {
588 			zfs_error_aux(hdl,
589 			    dgettext(TEXT_DOMAIN, "'%s' does not "
590 			    "apply to datasets of this type"), propname);
591 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
592 			goto error;
593 		}
594 
595 		if (zfs_prop_readonly(prop) &&
596 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
597 			zfs_error_aux(hdl,
598 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
599 			    propname);
600 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
601 			goto error;
602 		}
603 
604 		if (zprop_parse_value(hdl, elem, prop, type, ret,
605 		    &strval, &intval, errbuf) != 0)
606 			goto error;
607 
608 		/*
609 		 * Perform some additional checks for specific properties.
610 		 */
611 		switch (prop) {
612 		case ZFS_PROP_VERSION:
613 		{
614 			int version;
615 
616 			if (zhp == NULL)
617 				break;
618 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
619 			if (intval < version) {
620 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
621 				    "Can not downgrade; already at version %u"),
622 				    version);
623 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
624 				goto error;
625 			}
626 			break;
627 		}
628 
629 		case ZFS_PROP_RECORDSIZE:
630 		case ZFS_PROP_VOLBLOCKSIZE:
631 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
632 			if (intval < SPA_MINBLOCKSIZE ||
633 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
634 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
635 				    "'%s' must be power of 2 from %u "
636 				    "to %uk"), propname,
637 				    (uint_t)SPA_MINBLOCKSIZE,
638 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
639 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
640 				goto error;
641 			}
642 			break;
643 
644 		case ZFS_PROP_SHAREISCSI:
645 			if (strcmp(strval, "off") != 0 &&
646 			    strcmp(strval, "on") != 0 &&
647 			    strcmp(strval, "type=disk") != 0) {
648 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
649 				    "'%s' must be 'on', 'off', or 'type=disk'"),
650 				    propname);
651 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
652 				goto error;
653 			}
654 
655 			break;
656 
657 		case ZFS_PROP_MOUNTPOINT:
658 		{
659 			namecheck_err_t why;
660 
661 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
662 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
663 				break;
664 
665 			if (mountpoint_namecheck(strval, &why)) {
666 				switch (why) {
667 				case NAME_ERR_LEADING_SLASH:
668 					zfs_error_aux(hdl,
669 					    dgettext(TEXT_DOMAIN,
670 					    "'%s' must be an absolute path, "
671 					    "'none', or 'legacy'"), propname);
672 					break;
673 				case NAME_ERR_TOOLONG:
674 					zfs_error_aux(hdl,
675 					    dgettext(TEXT_DOMAIN,
676 					    "component of '%s' is too long"),
677 					    propname);
678 					break;
679 				}
680 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
681 				goto error;
682 			}
683 		}
684 
685 			/*FALLTHRU*/
686 
687 		case ZFS_PROP_SHARESMB:
688 		case ZFS_PROP_SHARENFS:
689 			/*
690 			 * For the mountpoint and sharenfs or sharesmb
691 			 * properties, check if it can be set in a
692 			 * global/non-global zone based on
693 			 * the zoned property value:
694 			 *
695 			 *		global zone	    non-global zone
696 			 * --------------------------------------------------
697 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
698 			 *		sharenfs (no)	    sharenfs (no)
699 			 *		sharesmb (no)	    sharesmb (no)
700 			 *
701 			 * zoned=off	mountpoint (yes)	N/A
702 			 *		sharenfs (yes)
703 			 *		sharesmb (yes)
704 			 */
705 			if (zoned) {
706 				if (getzoneid() == GLOBAL_ZONEID) {
707 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
708 					    "'%s' cannot be set on "
709 					    "dataset in a non-global zone"),
710 					    propname);
711 					(void) zfs_error(hdl, EZFS_ZONED,
712 					    errbuf);
713 					goto error;
714 				} else if (prop == ZFS_PROP_SHARENFS ||
715 				    prop == ZFS_PROP_SHARESMB) {
716 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
717 					    "'%s' cannot be set in "
718 					    "a non-global zone"), propname);
719 					(void) zfs_error(hdl, EZFS_ZONED,
720 					    errbuf);
721 					goto error;
722 				}
723 			} else if (getzoneid() != GLOBAL_ZONEID) {
724 				/*
725 				 * If zoned property is 'off', this must be in
726 				 * a globle zone. If not, something is wrong.
727 				 */
728 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
729 				    "'%s' cannot be set while dataset "
730 				    "'zoned' property is set"), propname);
731 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
732 				goto error;
733 			}
734 
735 			/*
736 			 * At this point, it is legitimate to set the
737 			 * property. Now we want to make sure that the
738 			 * property value is valid if it is sharenfs.
739 			 */
740 			if ((prop == ZFS_PROP_SHARENFS ||
741 			    prop == ZFS_PROP_SHARESMB) &&
742 			    strcmp(strval, "on") != 0 &&
743 			    strcmp(strval, "off") != 0) {
744 				zfs_share_proto_t proto;
745 
746 				if (prop == ZFS_PROP_SHARESMB)
747 					proto = PROTO_SMB;
748 				else
749 					proto = PROTO_NFS;
750 
751 				/*
752 				 * Must be an valid sharing protocol
753 				 * option string so init the libshare
754 				 * in order to enable the parser and
755 				 * then parse the options. We use the
756 				 * control API since we don't care about
757 				 * the current configuration and don't
758 				 * want the overhead of loading it
759 				 * until we actually do something.
760 				 */
761 
762 				if (zfs_init_libshare(hdl,
763 				    SA_INIT_CONTROL_API) != SA_OK) {
764 					/*
765 					 * An error occurred so we can't do
766 					 * anything
767 					 */
768 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
769 					    "'%s' cannot be set: problem "
770 					    "in share initialization"),
771 					    propname);
772 					(void) zfs_error(hdl, EZFS_BADPROP,
773 					    errbuf);
774 					goto error;
775 				}
776 
777 				if (zfs_parse_options(strval, proto) != SA_OK) {
778 					/*
779 					 * There was an error in parsing so
780 					 * deal with it by issuing an error
781 					 * message and leaving after
782 					 * uninitializing the the libshare
783 					 * interface.
784 					 */
785 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
786 					    "'%s' cannot be set to invalid "
787 					    "options"), propname);
788 					(void) zfs_error(hdl, EZFS_BADPROP,
789 					    errbuf);
790 					zfs_uninit_libshare(hdl);
791 					goto error;
792 				}
793 				zfs_uninit_libshare(hdl);
794 			}
795 
796 			break;
797 		case ZFS_PROP_UTF8ONLY:
798 			chosen_utf = (int)intval;
799 			break;
800 		case ZFS_PROP_NORMALIZE:
801 			chosen_normal = (int)intval;
802 			break;
803 		}
804 
805 		/*
806 		 * For changes to existing volumes, we have some additional
807 		 * checks to enforce.
808 		 */
809 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
810 			uint64_t volsize = zfs_prop_get_int(zhp,
811 			    ZFS_PROP_VOLSIZE);
812 			uint64_t blocksize = zfs_prop_get_int(zhp,
813 			    ZFS_PROP_VOLBLOCKSIZE);
814 			char buf[64];
815 
816 			switch (prop) {
817 			case ZFS_PROP_RESERVATION:
818 			case ZFS_PROP_REFRESERVATION:
819 				if (intval > volsize) {
820 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
821 					    "'%s' is greater than current "
822 					    "volume size"), propname);
823 					(void) zfs_error(hdl, EZFS_BADPROP,
824 					    errbuf);
825 					goto error;
826 				}
827 				break;
828 
829 			case ZFS_PROP_VOLSIZE:
830 				if (intval % blocksize != 0) {
831 					zfs_nicenum(blocksize, buf,
832 					    sizeof (buf));
833 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
834 					    "'%s' must be a multiple of "
835 					    "volume block size (%s)"),
836 					    propname, buf);
837 					(void) zfs_error(hdl, EZFS_BADPROP,
838 					    errbuf);
839 					goto error;
840 				}
841 
842 				if (intval == 0) {
843 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
844 					    "'%s' cannot be zero"),
845 					    propname);
846 					(void) zfs_error(hdl, EZFS_BADPROP,
847 					    errbuf);
848 					goto error;
849 				}
850 				break;
851 			}
852 		}
853 	}
854 
855 	/*
856 	 * If normalization was chosen, but no UTF8 choice was made,
857 	 * enforce rejection of non-UTF8 names.
858 	 *
859 	 * If normalization was chosen, but rejecting non-UTF8 names
860 	 * was explicitly not chosen, it is an error.
861 	 */
862 	if (chosen_normal > 0 && chosen_utf < 0) {
863 		if (nvlist_add_uint64(ret,
864 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
865 			(void) no_memory(hdl);
866 			goto error;
867 		}
868 	} else if (chosen_normal > 0 && chosen_utf == 0) {
869 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
870 		    "'%s' must be set 'on' if normalization chosen"),
871 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
872 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
873 		goto error;
874 	}
875 
876 	/*
877 	 * If this is an existing volume, and someone is setting the volsize,
878 	 * make sure that it matches the reservation, or add it if necessary.
879 	 */
880 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
881 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
882 	    &intval) == 0) {
883 		uint64_t old_volsize = zfs_prop_get_int(zhp,
884 		    ZFS_PROP_VOLSIZE);
885 		uint64_t old_reservation;
886 		uint64_t new_reservation;
887 		zfs_prop_t resv_prop;
888 
889 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
890 			goto error;
891 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
892 
893 		if (old_volsize == old_reservation &&
894 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
895 		    &new_reservation) != 0) {
896 			if (nvlist_add_uint64(ret,
897 			    zfs_prop_to_name(resv_prop), intval) != 0) {
898 				(void) no_memory(hdl);
899 				goto error;
900 			}
901 		}
902 	}
903 	return (ret);
904 
905 error:
906 	nvlist_free(ret);
907 	return (NULL);
908 }
909 
910 static int
911 zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
912     uint64_t *ret_who)
913 {
914 	struct passwd *pwd;
915 	struct group *grp;
916 	uid_t id;
917 
918 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
919 	    *who_type == ZFS_DELEG_NAMED_SET) {
920 		*ret_who = -1;
921 		return (0);
922 	}
923 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
924 		return (EZFS_BADWHO);
925 
926 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
927 	    strcmp(who, "everyone") == 0) {
928 		*ret_who = -1;
929 		*who_type = ZFS_DELEG_EVERYONE;
930 		return (0);
931 	}
932 
933 	pwd = getpwnam(who);
934 	grp = getgrnam(who);
935 
936 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
937 		*ret_who = pwd->pw_uid;
938 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
939 		*ret_who = grp->gr_gid;
940 	} else if (pwd) {
941 		*ret_who = pwd->pw_uid;
942 		*who_type = ZFS_DELEG_USER;
943 	} else if (grp) {
944 		*ret_who = grp->gr_gid;
945 		*who_type = ZFS_DELEG_GROUP;
946 	} else {
947 		char *end;
948 
949 		id = strtol(who, &end, 10);
950 		if (errno != 0 || *end != '\0') {
951 			return (EZFS_BADWHO);
952 		} else {
953 			*ret_who = id;
954 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
955 				*who_type = ZFS_DELEG_USER;
956 		}
957 	}
958 
959 	return (0);
960 }
961 
962 static void
963 zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
964 {
965 	if (perms_nvp != NULL) {
966 		verify(nvlist_add_nvlist(who_nvp,
967 		    name, perms_nvp) == 0);
968 	} else {
969 		verify(nvlist_add_boolean(who_nvp, name) == 0);
970 	}
971 }
972 
973 static void
974 helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
975     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
976     nvlist_t *sets_nvp)
977 {
978 	boolean_t do_perms, do_sets;
979 	char name[ZFS_MAX_DELEG_NAME];
980 
981 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
982 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
983 
984 	if (!do_perms && !do_sets)
985 		do_perms = do_sets = B_TRUE;
986 
987 	if (do_perms) {
988 		zfs_deleg_whokey(name, who_type, inherit,
989 		    (who_type == ZFS_DELEG_NAMED_SET) ?
990 		    whostr : (void *)&whoid);
991 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
992 	}
993 	if (do_sets) {
994 		zfs_deleg_whokey(name, toupper(who_type), inherit,
995 		    (who_type == ZFS_DELEG_NAMED_SET) ?
996 		    whostr : (void *)&whoid);
997 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
998 	}
999 }
1000 
1001 static void
1002 zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
1003     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
1004     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
1005 {
1006 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
1007 		helper(who_type, whoid, whostr, 0,
1008 		    who_nvp, perms_nvp, sets_nvp);
1009 	} else {
1010 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
1011 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
1012 			    who_nvp, perms_nvp, sets_nvp);
1013 		}
1014 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
1015 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
1016 			    who_nvp, perms_nvp, sets_nvp);
1017 		}
1018 	}
1019 }
1020 
1021 /*
1022  * Construct nvlist to pass down to kernel for setting/removing permissions.
1023  *
1024  * The nvlist is constructed as a series of nvpairs with an optional embedded
1025  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
1026  * base attribute named stored in the dsl.
1027  * Arguments:
1028  *
1029  * whostr:   is a comma separated list of users, groups, or a single set name.
1030  *           whostr may be null for everyone or create perms.
1031  * who_type: is the type of entry in whostr.  Typically this will be
1032  *           ZFS_DELEG_WHO_UNKNOWN.
1033  * perms:    common separated list of permissions.  May be null if user
1034  *           is requested to remove permissions by who.
1035  * inherit:  Specifies the inheritance of the permissions.  Will be either
1036  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
1037  * nvp       The constructed nvlist to pass to zfs_perm_set().
1038  *           The output nvp will look something like this.
1039  *              ul$1234 -> {create ; destroy }
1040  *              Ul$1234 -> { @myset }
1041  *              s-$@myset - { snapshot; checksum; compression }
1042  */
1043 int
1044 zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
1045     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
1046 {
1047 	nvlist_t *who_nvp;
1048 	nvlist_t *perms_nvp = NULL;
1049 	nvlist_t *sets_nvp = NULL;
1050 	char errbuf[1024];
1051 	char *who_tok, *perm;
1052 	int error;
1053 
1054 	*nvp = NULL;
1055 
1056 	if (perms) {
1057 		if ((error = nvlist_alloc(&perms_nvp,
1058 		    NV_UNIQUE_NAME, 0)) != 0) {
1059 			return (1);
1060 		}
1061 		if ((error = nvlist_alloc(&sets_nvp,
1062 		    NV_UNIQUE_NAME, 0)) != 0) {
1063 			nvlist_free(perms_nvp);
1064 			return (1);
1065 		}
1066 	}
1067 
1068 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
1069 		if (perms_nvp)
1070 			nvlist_free(perms_nvp);
1071 		if (sets_nvp)
1072 			nvlist_free(sets_nvp);
1073 		return (1);
1074 	}
1075 
1076 	if (who_type == ZFS_DELEG_NAMED_SET) {
1077 		namecheck_err_t why;
1078 		char what;
1079 
1080 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
1081 			nvlist_free(who_nvp);
1082 			if (perms_nvp)
1083 				nvlist_free(perms_nvp);
1084 			if (sets_nvp)
1085 				nvlist_free(sets_nvp);
1086 
1087 			switch (why) {
1088 			case NAME_ERR_NO_AT:
1089 				zfs_error_aux(zhp->zfs_hdl,
1090 				    dgettext(TEXT_DOMAIN,
1091 				    "set definition must begin with an '@' "
1092 				    "character"));
1093 			}
1094 			return (zfs_error(zhp->zfs_hdl,
1095 			    EZFS_BADPERMSET, whostr));
1096 		}
1097 	}
1098 
1099 	/*
1100 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
1101 	 * The first nvlist perms_nvp will have normal permissions and the
1102 	 * other sets_nvp will have only permssion set names in it.
1103 	 */
1104 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
1105 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
1106 
1107 		if (perm_canonical) {
1108 			verify(nvlist_add_boolean(perms_nvp,
1109 			    perm_canonical) == 0);
1110 		} else if (perm[0] == '@') {
1111 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
1112 		} else {
1113 			nvlist_free(who_nvp);
1114 			nvlist_free(perms_nvp);
1115 			nvlist_free(sets_nvp);
1116 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
1117 		}
1118 	}
1119 
1120 	if (whostr && who_type != ZFS_DELEG_CREATE) {
1121 		who_tok = strtok(whostr, ",");
1122 		if (who_tok == NULL) {
1123 			nvlist_free(who_nvp);
1124 			if (perms_nvp)
1125 				nvlist_free(perms_nvp);
1126 			if (sets_nvp)
1127 				nvlist_free(sets_nvp);
1128 			(void) snprintf(errbuf, sizeof (errbuf),
1129 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
1130 			    whostr);
1131 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
1132 		}
1133 	}
1134 
1135 	/*
1136 	 * Now create the nvlist(s)
1137 	 */
1138 	do {
1139 		uint64_t who_id;
1140 
1141 		error = zfs_get_perm_who(who_tok, &who_type,
1142 		    &who_id);
1143 		if (error) {
1144 			nvlist_free(who_nvp);
1145 			if (perms_nvp)
1146 				nvlist_free(perms_nvp);
1147 			if (sets_nvp)
1148 				nvlist_free(sets_nvp);
1149 			(void) snprintf(errbuf, sizeof (errbuf),
1150 			    dgettext(TEXT_DOMAIN,
1151 			    "Unable to determine uid/gid for "
1152 			    "%s "), who_tok);
1153 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
1154 		}
1155 
1156 		/*
1157 		 * add entries for both local and descendent when required
1158 		 */
1159 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
1160 		    perms_nvp, sets_nvp, who_type, inherit);
1161 
1162 	} while (who_tok = strtok(NULL, ","));
1163 	*nvp = who_nvp;
1164 	return (0);
1165 }
1166 
1167 static int
1168 zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
1169 {
1170 	zfs_cmd_t zc = { 0 };
1171 	int error;
1172 	char errbuf[1024];
1173 
1174 	(void) snprintf(errbuf, sizeof (errbuf),
1175 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
1176 	    zhp->zfs_name);
1177 
1178 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
1179 		return (-1);
1180 
1181 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1182 	zc.zc_perm_action = unset;
1183 
1184 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
1185 	if (error && errno == ENOTSUP) {
1186 		(void) snprintf(errbuf, sizeof (errbuf),
1187 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
1188 		zcmd_free_nvlists(&zc);
1189 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
1190 	} else if (error) {
1191 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
1192 	}
1193 	zcmd_free_nvlists(&zc);
1194 
1195 	return (error);
1196 }
1197 
1198 int
1199 zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
1200 {
1201 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
1202 }
1203 
1204 int
1205 zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
1206 {
1207 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
1208 }
1209 
1210 static int
1211 perm_compare(const void *arg1, const void *arg2)
1212 {
1213 	const zfs_perm_node_t *node1 = arg1;
1214 	const zfs_perm_node_t *node2 = arg2;
1215 	int ret;
1216 
1217 	ret = strcmp(node1->z_pname, node2->z_pname);
1218 
1219 	if (ret > 0)
1220 		return (1);
1221 	if (ret < 0)
1222 		return (-1);
1223 	else
1224 		return (0);
1225 }
1226 
1227 static void
1228 zfs_destroy_perm_tree(avl_tree_t *tree)
1229 {
1230 	zfs_perm_node_t *permnode;
1231 	void *cookie = NULL;
1232 
1233 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
1234 		free(permnode);
1235 	avl_destroy(tree);
1236 }
1237 
1238 static void
1239 zfs_destroy_tree(avl_tree_t *tree)
1240 {
1241 	zfs_allow_node_t *allownode;
1242 	void *cookie = NULL;
1243 
1244 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
1245 		zfs_destroy_perm_tree(&allownode->z_localdescend);
1246 		zfs_destroy_perm_tree(&allownode->z_local);
1247 		zfs_destroy_perm_tree(&allownode->z_descend);
1248 		free(allownode);
1249 	}
1250 	avl_destroy(tree);
1251 }
1252 
1253 void
1254 zfs_free_allows(zfs_allow_t *allow)
1255 {
1256 	zfs_allow_t *allownext;
1257 	zfs_allow_t *freeallow;
1258 
1259 	allownext = allow;
1260 	while (allownext) {
1261 		zfs_destroy_tree(&allownext->z_sets);
1262 		zfs_destroy_tree(&allownext->z_crperms);
1263 		zfs_destroy_tree(&allownext->z_user);
1264 		zfs_destroy_tree(&allownext->z_group);
1265 		zfs_destroy_tree(&allownext->z_everyone);
1266 		freeallow = allownext;
1267 		allownext = allownext->z_next;
1268 		free(freeallow);
1269 	}
1270 }
1271 
1272 static zfs_allow_t *
1273 zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
1274 {
1275 	zfs_allow_t *ptree;
1276 
1277 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
1278 	    sizeof (zfs_allow_t))) == NULL) {
1279 		return (NULL);
1280 	}
1281 
1282 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
1283 	avl_create(&ptree->z_sets,
1284 	    perm_compare, sizeof (zfs_allow_node_t),
1285 	    offsetof(zfs_allow_node_t, z_node));
1286 	avl_create(&ptree->z_crperms,
1287 	    perm_compare, sizeof (zfs_allow_node_t),
1288 	    offsetof(zfs_allow_node_t, z_node));
1289 	avl_create(&ptree->z_user,
1290 	    perm_compare, sizeof (zfs_allow_node_t),
1291 	    offsetof(zfs_allow_node_t, z_node));
1292 	avl_create(&ptree->z_group,
1293 	    perm_compare, sizeof (zfs_allow_node_t),
1294 	    offsetof(zfs_allow_node_t, z_node));
1295 	avl_create(&ptree->z_everyone,
1296 	    perm_compare, sizeof (zfs_allow_node_t),
1297 	    offsetof(zfs_allow_node_t, z_node));
1298 
1299 	if (prev)
1300 		prev->z_next = ptree;
1301 	ptree->z_next = NULL;
1302 	return (ptree);
1303 }
1304 
1305 /*
1306  * Add permissions to the appropriate AVL permission tree.
1307  * The appropriate tree may not be the requested tree.
1308  * For example if ld indicates a local permission, but
1309  * same permission also exists as a descendent permission
1310  * then the permission will be removed from the descendent
1311  * tree and add the the local+descendent tree.
1312  */
1313 static int
1314 zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
1315     char *perm, char ld)
1316 {
1317 	zfs_perm_node_t pnode, *permnode, *permnode2;
1318 	zfs_perm_node_t *newnode;
1319 	avl_index_t where, where2;
1320 	avl_tree_t *tree, *altree;
1321 
1322 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
1323 
1324 	if (ld == ZFS_DELEG_NA) {
1325 		tree =  &allownode->z_localdescend;
1326 		altree = &allownode->z_descend;
1327 	} else if (ld == ZFS_DELEG_LOCAL) {
1328 		tree = &allownode->z_local;
1329 		altree = &allownode->z_descend;
1330 	} else {
1331 		tree = &allownode->z_descend;
1332 		altree = &allownode->z_local;
1333 	}
1334 	permnode = avl_find(tree, &pnode, &where);
1335 	permnode2 = avl_find(altree, &pnode, &where2);
1336 
1337 	if (permnode2) {
1338 		avl_remove(altree, permnode2);
1339 		free(permnode2);
1340 		if (permnode == NULL) {
1341 			tree =  &allownode->z_localdescend;
1342 		}
1343 	}
1344 
1345 	/*
1346 	 * Now insert new permission in either requested location
1347 	 * local/descendent or into ld when perm will exist in both.
1348 	 */
1349 	if (permnode == NULL) {
1350 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
1351 		    sizeof (zfs_perm_node_t))) == NULL) {
1352 			return (-1);
1353 		}
1354 		*newnode = pnode;
1355 		avl_add(tree, newnode);
1356 	}
1357 	return (0);
1358 }
1359 
1360 /*
1361  * Uggh, this is going to be a bit complicated.
1362  * we have an nvlist coming out of the kernel that
1363  * will indicate where the permission is set and then
1364  * it will contain allow of the various "who's", and what
1365  * their permissions are.  To further complicate this
1366  * we will then have to coalesce the local,descendent
1367  * and local+descendent permissions where appropriate.
1368  * The kernel only knows about a permission as being local
1369  * or descendent, but not both.
1370  *
1371  * In order to make this easier for zfs_main to deal with
1372  * a series of AVL trees will be used to maintain
1373  * all of this, primarily for sorting purposes as well
1374  * as the ability to quickly locate a specific entry.
1375  *
1376  * What we end up with are tree's for sets, create perms,
1377  * user, groups and everyone.  With each of those trees
1378  * we have subtrees for local, descendent and local+descendent
1379  * permissions.
1380  */
1381 int
1382 zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
1383 {
1384 	zfs_cmd_t zc = { 0 };
1385 	int error;
1386 	nvlist_t *nvlist;
1387 	nvlist_t *permnv, *sourcenv;
1388 	nvpair_t *who_pair, *source_pair;
1389 	nvpair_t *perm_pair;
1390 	char errbuf[1024];
1391 	zfs_allow_t *zallowp, *newallowp;
1392 	char  ld;
1393 	char *nvpname;
1394 	uid_t	uid;
1395 	gid_t	gid;
1396 	avl_tree_t *tree;
1397 	avl_index_t where;
1398 
1399 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1400 
1401 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1402 		return (-1);
1403 
1404 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
1405 		if (errno == ENOMEM) {
1406 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
1407 				zcmd_free_nvlists(&zc);
1408 				return (-1);
1409 			}
1410 		} else if (errno == ENOTSUP) {
1411 			zcmd_free_nvlists(&zc);
1412 			(void) snprintf(errbuf, sizeof (errbuf),
1413 			    gettext("Pool must be upgraded to use 'allow'"));
1414 			return (zfs_error(zhp->zfs_hdl,
1415 			    EZFS_BADVERSION, errbuf));
1416 		} else {
1417 			zcmd_free_nvlists(&zc);
1418 			return (-1);
1419 		}
1420 	}
1421 
1422 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
1423 		zcmd_free_nvlists(&zc);
1424 		return (-1);
1425 	}
1426 
1427 	zcmd_free_nvlists(&zc);
1428 
1429 	source_pair = nvlist_next_nvpair(nvlist, NULL);
1430 
1431 	if (source_pair == NULL) {
1432 		*zfs_perms = NULL;
1433 		return (0);
1434 	}
1435 
1436 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
1437 	if (*zfs_perms == NULL) {
1438 		return (0);
1439 	}
1440 
1441 	zallowp = *zfs_perms;
1442 
1443 	for (;;) {
1444 		struct passwd *pwd;
1445 		struct group *grp;
1446 		zfs_allow_node_t *allownode;
1447 		zfs_allow_node_t  findallownode;
1448 		zfs_allow_node_t *newallownode;
1449 
1450 		(void) strlcpy(zallowp->z_setpoint,
1451 		    nvpair_name(source_pair),
1452 		    sizeof (zallowp->z_setpoint));
1453 
1454 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
1455 			goto abort;
1456 
1457 		/*
1458 		 * Make sure nvlist is composed correctly
1459 		 */
1460 		if (zfs_deleg_verify_nvlist(sourcenv)) {
1461 			goto abort;
1462 		}
1463 
1464 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
1465 		if (who_pair == NULL) {
1466 			goto abort;
1467 		}
1468 
1469 		do {
1470 			error = nvpair_value_nvlist(who_pair, &permnv);
1471 			if (error) {
1472 				goto abort;
1473 			}
1474 
1475 			/*
1476 			 * First build up the key to use
1477 			 * for looking up in the various
1478 			 * who trees.
1479 			 */
1480 			ld = nvpair_name(who_pair)[1];
1481 			nvpname = nvpair_name(who_pair);
1482 			switch (nvpair_name(who_pair)[0]) {
1483 			case ZFS_DELEG_USER:
1484 			case ZFS_DELEG_USER_SETS:
1485 				tree = &zallowp->z_user;
1486 				uid = atol(&nvpname[3]);
1487 				pwd = getpwuid(uid);
1488 				(void) snprintf(findallownode.z_key,
1489 				    sizeof (findallownode.z_key), "user %s",
1490 				    (pwd) ? pwd->pw_name :
1491 				    &nvpair_name(who_pair)[3]);
1492 				break;
1493 			case ZFS_DELEG_GROUP:
1494 			case ZFS_DELEG_GROUP_SETS:
1495 				tree = &zallowp->z_group;
1496 				gid = atol(&nvpname[3]);
1497 				grp = getgrgid(gid);
1498 				(void) snprintf(findallownode.z_key,
1499 				    sizeof (findallownode.z_key), "group %s",
1500 				    (grp) ? grp->gr_name :
1501 				    &nvpair_name(who_pair)[3]);
1502 				break;
1503 			case ZFS_DELEG_CREATE:
1504 			case ZFS_DELEG_CREATE_SETS:
1505 				tree = &zallowp->z_crperms;
1506 				(void) strlcpy(findallownode.z_key, "",
1507 				    sizeof (findallownode.z_key));
1508 				break;
1509 			case ZFS_DELEG_EVERYONE:
1510 			case ZFS_DELEG_EVERYONE_SETS:
1511 				(void) snprintf(findallownode.z_key,
1512 				    sizeof (findallownode.z_key), "everyone");
1513 				tree = &zallowp->z_everyone;
1514 				break;
1515 			case ZFS_DELEG_NAMED_SET:
1516 			case ZFS_DELEG_NAMED_SET_SETS:
1517 				(void) snprintf(findallownode.z_key,
1518 				    sizeof (findallownode.z_key), "%s",
1519 				    &nvpair_name(who_pair)[3]);
1520 				tree = &zallowp->z_sets;
1521 				break;
1522 			}
1523 
1524 			/*
1525 			 * Place who in tree
1526 			 */
1527 			allownode = avl_find(tree, &findallownode, &where);
1528 			if (allownode == NULL) {
1529 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
1530 				    sizeof (zfs_allow_node_t))) == NULL) {
1531 					goto abort;
1532 				}
1533 				avl_create(&newallownode->z_localdescend,
1534 				    perm_compare,
1535 				    sizeof (zfs_perm_node_t),
1536 				    offsetof(zfs_perm_node_t, z_node));
1537 				avl_create(&newallownode->z_local,
1538 				    perm_compare,
1539 				    sizeof (zfs_perm_node_t),
1540 				    offsetof(zfs_perm_node_t, z_node));
1541 				avl_create(&newallownode->z_descend,
1542 				    perm_compare,
1543 				    sizeof (zfs_perm_node_t),
1544 				    offsetof(zfs_perm_node_t, z_node));
1545 				(void) strlcpy(newallownode->z_key,
1546 				    findallownode.z_key,
1547 				    sizeof (findallownode.z_key));
1548 				avl_insert(tree, newallownode, where);
1549 				allownode = newallownode;
1550 			}
1551 
1552 			/*
1553 			 * Now iterate over the permissions and
1554 			 * place them in the appropriate local,
1555 			 * descendent or local+descendent tree.
1556 			 *
1557 			 * The permissions are added to the tree
1558 			 * via zfs_coalesce_perm().
1559 			 */
1560 			perm_pair = nvlist_next_nvpair(permnv, NULL);
1561 			if (perm_pair == NULL)
1562 				goto abort;
1563 			do {
1564 				if (zfs_coalesce_perm(zhp, allownode,
1565 				    nvpair_name(perm_pair), ld) != 0)
1566 					goto abort;
1567 			} while (perm_pair = nvlist_next_nvpair(permnv,
1568 			    perm_pair));
1569 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
1570 
1571 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
1572 		if (source_pair == NULL)
1573 			break;
1574 
1575 		/*
1576 		 * allocate another node from the link list of
1577 		 * zfs_allow_t structures
1578 		 */
1579 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
1580 		    nvpair_name(source_pair));
1581 		if (newallowp == NULL) {
1582 			goto abort;
1583 		}
1584 		zallowp = newallowp;
1585 	}
1586 	nvlist_free(nvlist);
1587 	return (0);
1588 abort:
1589 	zfs_free_allows(*zfs_perms);
1590 	nvlist_free(nvlist);
1591 	return (-1);
1592 }
1593 
1594 static char *
1595 zfs_deleg_perm_note(zfs_deleg_note_t note)
1596 {
1597 	/*
1598 	 * Don't put newlines on end of lines
1599 	 */
1600 	switch (note) {
1601 	case ZFS_DELEG_NOTE_CREATE:
1602 		return (dgettext(TEXT_DOMAIN,
1603 		    "Must also have the 'mount' ability"));
1604 	case ZFS_DELEG_NOTE_DESTROY:
1605 		return (dgettext(TEXT_DOMAIN,
1606 		    "Must also have the 'mount' ability"));
1607 	case ZFS_DELEG_NOTE_SNAPSHOT:
1608 		return (dgettext(TEXT_DOMAIN,
1609 		    "Must also have the 'mount' ability"));
1610 	case ZFS_DELEG_NOTE_ROLLBACK:
1611 		return (dgettext(TEXT_DOMAIN,
1612 		    "Must also have the 'mount' ability"));
1613 	case ZFS_DELEG_NOTE_CLONE:
1614 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
1615 		    "ability and 'mount'\n"
1616 		    "\t\t\t\tability in the origin file system"));
1617 	case ZFS_DELEG_NOTE_PROMOTE:
1618 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
1619 		    "\t\t\t\tand 'promote' ability in the origin file system"));
1620 	case ZFS_DELEG_NOTE_RENAME:
1621 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
1622 		    "and 'create' \n\t\t\t\tability in the new parent"));
1623 	case ZFS_DELEG_NOTE_RECEIVE:
1624 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
1625 		    " and 'create' ability"));
1626 	case ZFS_DELEG_NOTE_USERPROP:
1627 		return (dgettext(TEXT_DOMAIN,
1628 		    "Allows changing any user property"));
1629 	case ZFS_DELEG_NOTE_ALLOW:
1630 		return (dgettext(TEXT_DOMAIN,
1631 		    "Must also have the permission that is being\n"
1632 		    "\t\t\t\tallowed"));
1633 	case ZFS_DELEG_NOTE_MOUNT:
1634 		return (dgettext(TEXT_DOMAIN,
1635 		    "Allows mount/umount of ZFS datasets"));
1636 	case ZFS_DELEG_NOTE_SHARE:
1637 		return (dgettext(TEXT_DOMAIN,
1638 		    "Allows sharing file systems over NFS or SMB\n"
1639 		    "\t\t\t\tprotocols"));
1640 	case ZFS_DELEG_NOTE_NONE:
1641 	default:
1642 		return (dgettext(TEXT_DOMAIN, ""));
1643 	}
1644 }
1645 
1646 typedef enum {
1647 	ZFS_DELEG_SUBCOMMAND,
1648 	ZFS_DELEG_PROP,
1649 	ZFS_DELEG_OTHER
1650 } zfs_deleg_perm_type_t;
1651 
1652 /*
1653  * is the permission a subcommand or other?
1654  */
1655 zfs_deleg_perm_type_t
1656 zfs_deleg_perm_type(const char *perm)
1657 {
1658 	if (strcmp(perm, "userprop") == 0)
1659 		return (ZFS_DELEG_OTHER);
1660 	else
1661 		return (ZFS_DELEG_SUBCOMMAND);
1662 }
1663 
1664 static char *
1665 zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
1666 {
1667 	switch (type) {
1668 	case ZFS_DELEG_SUBCOMMAND:
1669 		return (dgettext(TEXT_DOMAIN, "subcommand"));
1670 	case ZFS_DELEG_PROP:
1671 		return (dgettext(TEXT_DOMAIN, "property"));
1672 	case ZFS_DELEG_OTHER:
1673 		return (dgettext(TEXT_DOMAIN, "other"));
1674 	}
1675 	return ("");
1676 }
1677 
1678 /*ARGSUSED*/
1679 static int
1680 zfs_deleg_prop_cb(int prop, void *cb)
1681 {
1682 	if (zfs_prop_delegatable(prop))
1683 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
1684 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
1685 
1686 	return (ZPROP_CONT);
1687 }
1688 
1689 void
1690 zfs_deleg_permissions(void)
1691 {
1692 	int i;
1693 
1694 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
1695 	    "TYPE", "NOTES");
1696 
1697 	/*
1698 	 * First print out the subcommands
1699 	 */
1700 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
1701 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
1702 		    zfs_deleg_perm_tab[i].z_perm,
1703 		    zfs_deleg_perm_type_str(
1704 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
1705 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
1706 	}
1707 
1708 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
1709 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
1710 }
1711 
1712 /*
1713  * Given a property name and value, set the property for the given dataset.
1714  */
1715 int
1716 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1717 {
1718 	zfs_cmd_t zc = { 0 };
1719 	int ret = -1;
1720 	prop_changelist_t *cl = NULL;
1721 	char errbuf[1024];
1722 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1723 	nvlist_t *nvl = NULL, *realprops;
1724 	zfs_prop_t prop;
1725 	int do_prefix = 1;
1726 
1727 	(void) snprintf(errbuf, sizeof (errbuf),
1728 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1729 	    zhp->zfs_name);
1730 
1731 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1732 	    nvlist_add_string(nvl, propname, propval) != 0) {
1733 		(void) no_memory(hdl);
1734 		goto error;
1735 	}
1736 
1737 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
1738 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1739 		goto error;
1740 
1741 	nvlist_free(nvl);
1742 	nvl = realprops;
1743 
1744 	prop = zfs_name_to_prop(propname);
1745 
1746 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1747 		goto error;
1748 
1749 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1750 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1751 		    "child dataset with inherited mountpoint is used "
1752 		    "in a non-global zone"));
1753 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1754 		goto error;
1755 	}
1756 
1757 
1758 	/* do not unmount dataset if canmount is being set to noauto */
1759 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
1760 		do_prefix = 0;
1761 
1762 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1763 			goto error;
1764 
1765 	/*
1766 	 * Execute the corresponding ioctl() to set this property.
1767 	 */
1768 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1769 
1770 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1771 		goto error;
1772 
1773 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1774 	if (ret != 0) {
1775 		switch (errno) {
1776 
1777 		case ENOSPC:
1778 			/*
1779 			 * For quotas and reservations, ENOSPC indicates
1780 			 * something different; setting a quota or reservation
1781 			 * doesn't use any disk space.
1782 			 */
1783 			switch (prop) {
1784 			case ZFS_PROP_QUOTA:
1785 			case ZFS_PROP_REFQUOTA:
1786 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1787 				    "size is less than current used or "
1788 				    "reserved space"));
1789 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1790 				break;
1791 
1792 			case ZFS_PROP_RESERVATION:
1793 			case ZFS_PROP_REFRESERVATION:
1794 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1795 				    "size is greater than available space"));
1796 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1797 				break;
1798 
1799 			default:
1800 				(void) zfs_standard_error(hdl, errno, errbuf);
1801 				break;
1802 			}
1803 			break;
1804 
1805 		case EBUSY:
1806 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1807 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
1808 			else
1809 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1810 			break;
1811 
1812 		case EROFS:
1813 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1814 			break;
1815 
1816 		case ENOTSUP:
1817 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1818 			    "pool and or dataset must be upgraded to set this "
1819 			    "property or value"));
1820 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1821 			break;
1822 
1823 		case EOVERFLOW:
1824 			/*
1825 			 * This platform can't address a volume this big.
1826 			 */
1827 #ifdef _ILP32
1828 			if (prop == ZFS_PROP_VOLSIZE) {
1829 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1830 				break;
1831 			}
1832 #endif
1833 			/* FALLTHROUGH */
1834 		default:
1835 			(void) zfs_standard_error(hdl, errno, errbuf);
1836 		}
1837 	} else {
1838 		if (do_prefix)
1839 			ret = changelist_postfix(cl);
1840 
1841 		/*
1842 		 * Refresh the statistics so the new property value
1843 		 * is reflected.
1844 		 */
1845 		if (ret == 0)
1846 			(void) get_stats(zhp);
1847 	}
1848 
1849 error:
1850 	nvlist_free(nvl);
1851 	zcmd_free_nvlists(&zc);
1852 	if (cl)
1853 		changelist_free(cl);
1854 	return (ret);
1855 }
1856 
1857 /*
1858  * Given a property, inherit the value from the parent dataset.
1859  */
1860 int
1861 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1862 {
1863 	zfs_cmd_t zc = { 0 };
1864 	int ret;
1865 	prop_changelist_t *cl;
1866 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1867 	char errbuf[1024];
1868 	zfs_prop_t prop;
1869 
1870 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1871 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1872 
1873 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1874 		/*
1875 		 * For user properties, the amount of work we have to do is very
1876 		 * small, so just do it here.
1877 		 */
1878 		if (!zfs_prop_user(propname)) {
1879 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1880 			    "invalid property"));
1881 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1882 		}
1883 
1884 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1885 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1886 
1887 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1888 			return (zfs_standard_error(hdl, errno, errbuf));
1889 
1890 		return (0);
1891 	}
1892 
1893 	/*
1894 	 * Verify that this property is inheritable.
1895 	 */
1896 	if (zfs_prop_readonly(prop))
1897 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1898 
1899 	if (!zfs_prop_inheritable(prop))
1900 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1901 
1902 	/*
1903 	 * Check to see if the value applies to this type
1904 	 */
1905 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1906 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1907 
1908 	/*
1909 	 * Normalize the name, to get rid of shorthand abbrevations.
1910 	 */
1911 	propname = zfs_prop_to_name(prop);
1912 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1913 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1914 
1915 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1916 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1917 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1918 		    "dataset is used in a non-global zone"));
1919 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1920 	}
1921 
1922 	/*
1923 	 * Determine datasets which will be affected by this change, if any.
1924 	 */
1925 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1926 		return (-1);
1927 
1928 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1929 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1930 		    "child dataset with inherited mountpoint is used "
1931 		    "in a non-global zone"));
1932 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1933 		goto error;
1934 	}
1935 
1936 	if ((ret = changelist_prefix(cl)) != 0)
1937 		goto error;
1938 
1939 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1940 		return (zfs_standard_error(hdl, errno, errbuf));
1941 	} else {
1942 
1943 		if ((ret = changelist_postfix(cl)) != 0)
1944 			goto error;
1945 
1946 		/*
1947 		 * Refresh the statistics so the new property is reflected.
1948 		 */
1949 		(void) get_stats(zhp);
1950 	}
1951 
1952 error:
1953 	changelist_free(cl);
1954 	return (ret);
1955 }
1956 
1957 /*
1958  * True DSL properties are stored in an nvlist.  The following two functions
1959  * extract them appropriately.
1960  */
1961 static uint64_t
1962 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1963 {
1964 	nvlist_t *nv;
1965 	uint64_t value;
1966 
1967 	*source = NULL;
1968 	if (nvlist_lookup_nvlist(zhp->zfs_props,
1969 	    zfs_prop_to_name(prop), &nv) == 0) {
1970 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1971 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1972 	} else {
1973 		value = zfs_prop_default_numeric(prop);
1974 		*source = "";
1975 	}
1976 
1977 	return (value);
1978 }
1979 
1980 static char *
1981 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1982 {
1983 	nvlist_t *nv;
1984 	char *value;
1985 
1986 	*source = NULL;
1987 	if (nvlist_lookup_nvlist(zhp->zfs_props,
1988 	    zfs_prop_to_name(prop), &nv) == 0) {
1989 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1990 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1991 	} else {
1992 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1993 			value = "";
1994 		*source = "";
1995 	}
1996 
1997 	return (value);
1998 }
1999 
2000 /*
2001  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2002  * zfs_prop_get_int() are built using this interface.
2003  *
2004  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2005  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2006  * If they differ from the on-disk values, report the current values and mark
2007  * the source "temporary".
2008  */
2009 static int
2010 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2011     char **source, uint64_t *val)
2012 {
2013 	zfs_cmd_t zc = { 0 };
2014 	nvlist_t *zplprops = NULL;
2015 	struct mnttab mnt;
2016 	char *mntopt_on = NULL;
2017 	char *mntopt_off = NULL;
2018 
2019 	*source = NULL;
2020 
2021 	switch (prop) {
2022 	case ZFS_PROP_ATIME:
2023 		mntopt_on = MNTOPT_ATIME;
2024 		mntopt_off = MNTOPT_NOATIME;
2025 		break;
2026 
2027 	case ZFS_PROP_DEVICES:
2028 		mntopt_on = MNTOPT_DEVICES;
2029 		mntopt_off = MNTOPT_NODEVICES;
2030 		break;
2031 
2032 	case ZFS_PROP_EXEC:
2033 		mntopt_on = MNTOPT_EXEC;
2034 		mntopt_off = MNTOPT_NOEXEC;
2035 		break;
2036 
2037 	case ZFS_PROP_READONLY:
2038 		mntopt_on = MNTOPT_RO;
2039 		mntopt_off = MNTOPT_RW;
2040 		break;
2041 
2042 	case ZFS_PROP_SETUID:
2043 		mntopt_on = MNTOPT_SETUID;
2044 		mntopt_off = MNTOPT_NOSETUID;
2045 		break;
2046 
2047 	case ZFS_PROP_XATTR:
2048 		mntopt_on = MNTOPT_XATTR;
2049 		mntopt_off = MNTOPT_NOXATTR;
2050 		break;
2051 
2052 	case ZFS_PROP_NBMAND:
2053 		mntopt_on = MNTOPT_NBMAND;
2054 		mntopt_off = MNTOPT_NONBMAND;
2055 		break;
2056 	}
2057 
2058 	/*
2059 	 * Because looking up the mount options is potentially expensive
2060 	 * (iterating over all of /etc/mnttab), we defer its calculation until
2061 	 * we're looking up a property which requires its presence.
2062 	 */
2063 	if (!zhp->zfs_mntcheck &&
2064 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2065 		struct mnttab entry, search = { 0 };
2066 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
2067 
2068 		search.mnt_special = (char *)zhp->zfs_name;
2069 		search.mnt_fstype = MNTTYPE_ZFS;
2070 		rewind(mnttab);
2071 
2072 		if (getmntany(mnttab, &entry, &search) == 0) {
2073 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
2074 			    entry.mnt_mntopts);
2075 			if (zhp->zfs_mntopts == NULL)
2076 				return (-1);
2077 		}
2078 
2079 		zhp->zfs_mntcheck = B_TRUE;
2080 	}
2081 
2082 	if (zhp->zfs_mntopts == NULL)
2083 		mnt.mnt_mntopts = "";
2084 	else
2085 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2086 
2087 	switch (prop) {
2088 	case ZFS_PROP_ATIME:
2089 	case ZFS_PROP_DEVICES:
2090 	case ZFS_PROP_EXEC:
2091 	case ZFS_PROP_READONLY:
2092 	case ZFS_PROP_SETUID:
2093 	case ZFS_PROP_XATTR:
2094 	case ZFS_PROP_NBMAND:
2095 		*val = getprop_uint64(zhp, prop, source);
2096 
2097 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
2098 			*val = B_TRUE;
2099 			if (src)
2100 				*src = ZPROP_SRC_TEMPORARY;
2101 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
2102 			*val = B_FALSE;
2103 			if (src)
2104 				*src = ZPROP_SRC_TEMPORARY;
2105 		}
2106 		break;
2107 
2108 	case ZFS_PROP_CANMOUNT:
2109 		*val = getprop_uint64(zhp, prop, source);
2110 		if (*val != ZFS_CANMOUNT_ON)
2111 			*source = zhp->zfs_name;
2112 		else
2113 			*source = "";	/* default */
2114 		break;
2115 
2116 	case ZFS_PROP_QUOTA:
2117 	case ZFS_PROP_REFQUOTA:
2118 	case ZFS_PROP_RESERVATION:
2119 	case ZFS_PROP_REFRESERVATION:
2120 		*val = getprop_uint64(zhp, prop, source);
2121 		if (*val == 0)
2122 			*source = "";	/* default */
2123 		else
2124 			*source = zhp->zfs_name;
2125 		break;
2126 
2127 	case ZFS_PROP_MOUNTED:
2128 		*val = (zhp->zfs_mntopts != NULL);
2129 		break;
2130 
2131 	case ZFS_PROP_NUMCLONES:
2132 		*val = zhp->zfs_dmustats.dds_num_clones;
2133 		break;
2134 
2135 	case ZFS_PROP_VERSION:
2136 	case ZFS_PROP_NORMALIZE:
2137 	case ZFS_PROP_UTF8ONLY:
2138 	case ZFS_PROP_CASE:
2139 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2140 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2141 			return (-1);
2142 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2143 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2144 			zcmd_free_nvlists(&zc);
2145 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2146 			    "unable to get %s property"),
2147 			    zfs_prop_to_name(prop));
2148 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
2149 			    dgettext(TEXT_DOMAIN, "internal error")));
2150 		}
2151 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2152 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2153 		    val) != 0) {
2154 			zcmd_free_nvlists(&zc);
2155 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2156 			    "unable to get %s property"),
2157 			    zfs_prop_to_name(prop));
2158 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
2159 			    dgettext(TEXT_DOMAIN, "internal error")));
2160 		}
2161 		if (zplprops)
2162 			nvlist_free(zplprops);
2163 		zcmd_free_nvlists(&zc);
2164 		break;
2165 
2166 	default:
2167 		switch (zfs_prop_get_type(prop)) {
2168 		case PROP_TYPE_NUMBER:
2169 		case PROP_TYPE_INDEX:
2170 			*val = getprop_uint64(zhp, prop, source);
2171 			break;
2172 
2173 		case PROP_TYPE_STRING:
2174 		default:
2175 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2176 			    "cannot get non-numeric property"));
2177 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2178 			    dgettext(TEXT_DOMAIN, "internal error")));
2179 		}
2180 	}
2181 
2182 	return (0);
2183 }
2184 
2185 /*
2186  * Calculate the source type, given the raw source string.
2187  */
2188 static void
2189 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2190     char *statbuf, size_t statlen)
2191 {
2192 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2193 		return;
2194 
2195 	if (source == NULL) {
2196 		*srctype = ZPROP_SRC_NONE;
2197 	} else if (source[0] == '\0') {
2198 		*srctype = ZPROP_SRC_DEFAULT;
2199 	} else {
2200 		if (strcmp(source, zhp->zfs_name) == 0) {
2201 			*srctype = ZPROP_SRC_LOCAL;
2202 		} else {
2203 			(void) strlcpy(statbuf, source, statlen);
2204 			*srctype = ZPROP_SRC_INHERITED;
2205 		}
2206 	}
2207 
2208 }
2209 
2210 /*
2211  * Retrieve a property from the given object.  If 'literal' is specified, then
2212  * numbers are left as exact values.  Otherwise, numbers are converted to a
2213  * human-readable form.
2214  *
2215  * Returns 0 on success, or -1 on error.
2216  */
2217 int
2218 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2219     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2220 {
2221 	char *source = NULL;
2222 	uint64_t val;
2223 	char *str;
2224 	const char *root;
2225 	const char *strval;
2226 
2227 	/*
2228 	 * Check to see if this property applies to our object
2229 	 */
2230 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2231 		return (-1);
2232 
2233 	if (src)
2234 		*src = ZPROP_SRC_NONE;
2235 
2236 	switch (prop) {
2237 	case ZFS_PROP_CREATION:
2238 		/*
2239 		 * 'creation' is a time_t stored in the statistics.  We convert
2240 		 * this into a string unless 'literal' is specified.
2241 		 */
2242 		{
2243 			val = getprop_uint64(zhp, prop, &source);
2244 			time_t time = (time_t)val;
2245 			struct tm t;
2246 
2247 			if (literal ||
2248 			    localtime_r(&time, &t) == NULL ||
2249 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2250 			    &t) == 0)
2251 				(void) snprintf(propbuf, proplen, "%llu", val);
2252 		}
2253 		break;
2254 
2255 	case ZFS_PROP_MOUNTPOINT:
2256 		/*
2257 		 * Getting the precise mountpoint can be tricky.
2258 		 *
2259 		 *  - for 'none' or 'legacy', return those values.
2260 		 *  - for inherited mountpoints, we want to take everything
2261 		 *    after our ancestor and append it to the inherited value.
2262 		 *
2263 		 * If the pool has an alternate root, we want to prepend that
2264 		 * root to any values we return.
2265 		 */
2266 		root = zhp->zfs_root;
2267 		str = getprop_string(zhp, prop, &source);
2268 
2269 		if (str[0] == '/') {
2270 			const char *relpath = zhp->zfs_name + strlen(source);
2271 
2272 
2273 			if (relpath[0] == '/')
2274 				relpath++;
2275 
2276 			/*
2277 			 * Special case an alternate root of '/'. This will
2278 			 * avoid having multiple leading slashes in the
2279 			 * mountpoint path.
2280 			 */
2281 			if (strcmp(root, "/") == 0)
2282 				root++;
2283 
2284 			/*
2285 			 * If the mountpoint is '/' then skip over this
2286 			 * if we are obtaining either an alternate root or
2287 			 * an inherited mountpoint.
2288 			 */
2289 			if (str[1] == '\0' && (root[0] != '\0' ||
2290 			    relpath[0] != '\0'))
2291 				str++;
2292 
2293 			if (relpath[0] == '\0')
2294 				(void) snprintf(propbuf, proplen, "%s%s",
2295 				    root, str);
2296 			else
2297 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
2298 				    root, str, relpath[0] == '@' ? "" : "/",
2299 				    relpath);
2300 		} else {
2301 			/* 'legacy' or 'none' */
2302 			(void) strlcpy(propbuf, str, proplen);
2303 		}
2304 
2305 		break;
2306 
2307 	case ZFS_PROP_ORIGIN:
2308 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2309 		    proplen);
2310 		/*
2311 		 * If there is no parent at all, return failure to indicate that
2312 		 * it doesn't apply to this dataset.
2313 		 */
2314 		if (propbuf[0] == '\0')
2315 			return (-1);
2316 		break;
2317 
2318 	case ZFS_PROP_QUOTA:
2319 	case ZFS_PROP_REFQUOTA:
2320 	case ZFS_PROP_RESERVATION:
2321 	case ZFS_PROP_REFRESERVATION:
2322 
2323 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2324 			return (-1);
2325 
2326 		/*
2327 		 * If quota or reservation is 0, we translate this into 'none'
2328 		 * (unless literal is set), and indicate that it's the default
2329 		 * value.  Otherwise, we print the number nicely and indicate
2330 		 * that its set locally.
2331 		 */
2332 		if (val == 0) {
2333 			if (literal)
2334 				(void) strlcpy(propbuf, "0", proplen);
2335 			else
2336 				(void) strlcpy(propbuf, "none", proplen);
2337 		} else {
2338 			if (literal)
2339 				(void) snprintf(propbuf, proplen, "%llu",
2340 				    (u_longlong_t)val);
2341 			else
2342 				zfs_nicenum(val, propbuf, proplen);
2343 		}
2344 		break;
2345 
2346 	case ZFS_PROP_COMPRESSRATIO:
2347 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2348 			return (-1);
2349 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
2350 		    val / 100, (longlong_t)val % 100);
2351 		break;
2352 
2353 	case ZFS_PROP_TYPE:
2354 		switch (zhp->zfs_type) {
2355 		case ZFS_TYPE_FILESYSTEM:
2356 			str = "filesystem";
2357 			break;
2358 		case ZFS_TYPE_VOLUME:
2359 			str = "volume";
2360 			break;
2361 		case ZFS_TYPE_SNAPSHOT:
2362 			str = "snapshot";
2363 			break;
2364 		default:
2365 			abort();
2366 		}
2367 		(void) snprintf(propbuf, proplen, "%s", str);
2368 		break;
2369 
2370 	case ZFS_PROP_MOUNTED:
2371 		/*
2372 		 * The 'mounted' property is a pseudo-property that described
2373 		 * whether the filesystem is currently mounted.  Even though
2374 		 * it's a boolean value, the typical values of "on" and "off"
2375 		 * don't make sense, so we translate to "yes" and "no".
2376 		 */
2377 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2378 		    src, &source, &val) != 0)
2379 			return (-1);
2380 		if (val)
2381 			(void) strlcpy(propbuf, "yes", proplen);
2382 		else
2383 			(void) strlcpy(propbuf, "no", proplen);
2384 		break;
2385 
2386 	case ZFS_PROP_NAME:
2387 		/*
2388 		 * The 'name' property is a pseudo-property derived from the
2389 		 * dataset name.  It is presented as a real property to simplify
2390 		 * consumers.
2391 		 */
2392 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2393 		break;
2394 
2395 	default:
2396 		switch (zfs_prop_get_type(prop)) {
2397 		case PROP_TYPE_NUMBER:
2398 			if (get_numeric_property(zhp, prop, src,
2399 			    &source, &val) != 0)
2400 				return (-1);
2401 			if (literal)
2402 				(void) snprintf(propbuf, proplen, "%llu",
2403 				    (u_longlong_t)val);
2404 			else
2405 				zfs_nicenum(val, propbuf, proplen);
2406 			break;
2407 
2408 		case PROP_TYPE_STRING:
2409 			(void) strlcpy(propbuf,
2410 			    getprop_string(zhp, prop, &source), proplen);
2411 			break;
2412 
2413 		case PROP_TYPE_INDEX:
2414 			if (get_numeric_property(zhp, prop, src,
2415 			    &source, &val) != 0)
2416 				return (-1);
2417 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2418 				return (-1);
2419 			(void) strlcpy(propbuf, strval, proplen);
2420 			break;
2421 
2422 		default:
2423 			abort();
2424 		}
2425 	}
2426 
2427 	get_source(zhp, src, source, statbuf, statlen);
2428 
2429 	return (0);
2430 }
2431 
2432 /*
2433  * Utility function to get the given numeric property.  Does no validation that
2434  * the given property is the appropriate type; should only be used with
2435  * hard-coded property types.
2436  */
2437 uint64_t
2438 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2439 {
2440 	char *source;
2441 	uint64_t val;
2442 
2443 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2444 
2445 	return (val);
2446 }
2447 
2448 int
2449 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2450 {
2451 	char buf[64];
2452 
2453 	zfs_nicenum(val, buf, sizeof (buf));
2454 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2455 }
2456 
2457 /*
2458  * Similar to zfs_prop_get(), but returns the value as an integer.
2459  */
2460 int
2461 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2462     zprop_source_t *src, char *statbuf, size_t statlen)
2463 {
2464 	char *source;
2465 
2466 	/*
2467 	 * Check to see if this property applies to our object
2468 	 */
2469 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2470 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2471 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2472 		    zfs_prop_to_name(prop)));
2473 	}
2474 
2475 	if (src)
2476 		*src = ZPROP_SRC_NONE;
2477 
2478 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2479 		return (-1);
2480 
2481 	get_source(zhp, src, source, statbuf, statlen);
2482 
2483 	return (0);
2484 }
2485 
2486 /*
2487  * Returns the name of the given zfs handle.
2488  */
2489 const char *
2490 zfs_get_name(const zfs_handle_t *zhp)
2491 {
2492 	return (zhp->zfs_name);
2493 }
2494 
2495 /*
2496  * Returns the type of the given zfs handle.
2497  */
2498 zfs_type_t
2499 zfs_get_type(const zfs_handle_t *zhp)
2500 {
2501 	return (zhp->zfs_type);
2502 }
2503 
2504 /*
2505  * Iterate over all child filesystems
2506  */
2507 int
2508 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2509 {
2510 	zfs_cmd_t zc = { 0 };
2511 	zfs_handle_t *nzhp;
2512 	int ret;
2513 
2514 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2515 		return (0);
2516 
2517 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2518 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2519 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2520 		/*
2521 		 * Ignore private dataset names.
2522 		 */
2523 		if (dataset_name_hidden(zc.zc_name))
2524 			continue;
2525 
2526 		/*
2527 		 * Silently ignore errors, as the only plausible explanation is
2528 		 * that the pool has since been removed.
2529 		 */
2530 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
2531 		    zc.zc_name)) == NULL)
2532 			continue;
2533 
2534 		if ((ret = func(nzhp, data)) != 0)
2535 			return (ret);
2536 	}
2537 
2538 	/*
2539 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2540 	 * returned, then the underlying dataset has been removed since we
2541 	 * obtained the handle.
2542 	 */
2543 	if (errno != ESRCH && errno != ENOENT)
2544 		return (zfs_standard_error(zhp->zfs_hdl, errno,
2545 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2546 
2547 	return (0);
2548 }
2549 
2550 /*
2551  * Iterate over all snapshots
2552  */
2553 int
2554 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2555 {
2556 	zfs_cmd_t zc = { 0 };
2557 	zfs_handle_t *nzhp;
2558 	int ret;
2559 
2560 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2561 		return (0);
2562 
2563 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2564 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2565 	    &zc) == 0;
2566 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2567 
2568 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
2569 		    zc.zc_name)) == NULL)
2570 			continue;
2571 
2572 		if ((ret = func(nzhp, data)) != 0)
2573 			return (ret);
2574 	}
2575 
2576 	/*
2577 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2578 	 * returned, then the underlying dataset has been removed since we
2579 	 * obtained the handle.  Silently ignore this case, and return success.
2580 	 */
2581 	if (errno != ESRCH && errno != ENOENT)
2582 		return (zfs_standard_error(zhp->zfs_hdl, errno,
2583 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2584 
2585 	return (0);
2586 }
2587 
2588 /*
2589  * Iterate over all children, snapshots and filesystems
2590  */
2591 int
2592 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2593 {
2594 	int ret;
2595 
2596 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2597 		return (ret);
2598 
2599 	return (zfs_iter_snapshots(zhp, func, data));
2600 }
2601 
2602 /*
2603  * Given a complete name, return just the portion that refers to the parent.
2604  * Can return NULL if this is a pool.
2605  */
2606 static int
2607 parent_name(const char *path, char *buf, size_t buflen)
2608 {
2609 	char *loc;
2610 
2611 	if ((loc = strrchr(path, '/')) == NULL)
2612 		return (-1);
2613 
2614 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2615 	buf[loc - path] = '\0';
2616 
2617 	return (0);
2618 }
2619 
2620 /*
2621  * If accept_ancestor is false, then check to make sure that the given path has
2622  * a parent, and that it exists.  If accept_ancestor is true, then find the
2623  * closest existing ancestor for the given path.  In prefixlen return the
2624  * length of already existing prefix of the given path.  We also fetch the
2625  * 'zoned' property, which is used to validate property settings when creating
2626  * new datasets.
2627  */
2628 static int
2629 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2630     boolean_t accept_ancestor, int *prefixlen)
2631 {
2632 	zfs_cmd_t zc = { 0 };
2633 	char parent[ZFS_MAXNAMELEN];
2634 	char *slash;
2635 	zfs_handle_t *zhp;
2636 	char errbuf[1024];
2637 
2638 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
2639 	    path);
2640 
2641 	/* get parent, and check to see if this is just a pool */
2642 	if (parent_name(path, parent, sizeof (parent)) != 0) {
2643 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2644 		    "missing dataset name"));
2645 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2646 	}
2647 
2648 	/* check to see if the pool exists */
2649 	if ((slash = strchr(parent, '/')) == NULL)
2650 		slash = parent + strlen(parent);
2651 	(void) strncpy(zc.zc_name, parent, slash - parent);
2652 	zc.zc_name[slash - parent] = '\0';
2653 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2654 	    errno == ENOENT) {
2655 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2656 		    "no such pool '%s'"), zc.zc_name);
2657 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2658 	}
2659 
2660 	/* check to see if the parent dataset exists */
2661 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2662 		if (errno == ENOENT && accept_ancestor) {
2663 			/*
2664 			 * Go deeper to find an ancestor, give up on top level.
2665 			 */
2666 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
2667 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2668 				    "no such pool '%s'"), zc.zc_name);
2669 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
2670 			}
2671 		} else if (errno == ENOENT) {
2672 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2673 			    "parent does not exist"));
2674 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2675 		} else
2676 			return (zfs_standard_error(hdl, errno, errbuf));
2677 	}
2678 
2679 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2680 	/* we are in a non-global zone, but parent is in the global zone */
2681 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
2682 		(void) zfs_standard_error(hdl, EPERM, errbuf);
2683 		zfs_close(zhp);
2684 		return (-1);
2685 	}
2686 
2687 	/* make sure parent is a filesystem */
2688 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2689 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2690 		    "parent is not a filesystem"));
2691 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2692 		zfs_close(zhp);
2693 		return (-1);
2694 	}
2695 
2696 	zfs_close(zhp);
2697 	if (prefixlen != NULL)
2698 		*prefixlen = strlen(parent);
2699 	return (0);
2700 }
2701 
2702 /*
2703  * Finds whether the dataset of the given type(s) exists.
2704  */
2705 boolean_t
2706 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2707 {
2708 	zfs_handle_t *zhp;
2709 
2710 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
2711 		return (B_FALSE);
2712 
2713 	/*
2714 	 * Try to get stats for the dataset, which will tell us if it exists.
2715 	 */
2716 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2717 		int ds_type = zhp->zfs_type;
2718 
2719 		zfs_close(zhp);
2720 		if (types & ds_type)
2721 			return (B_TRUE);
2722 	}
2723 	return (B_FALSE);
2724 }
2725 
2726 /*
2727  * Given a path to 'target', create all the ancestors between
2728  * the prefixlen portion of the path, and the target itself.
2729  * Fail if the initial prefixlen-ancestor does not already exist.
2730  */
2731 int
2732 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2733 {
2734 	zfs_handle_t *h;
2735 	char *cp;
2736 	const char *opname;
2737 
2738 	/* make sure prefix exists */
2739 	cp = target + prefixlen;
2740 	if (*cp != '/') {
2741 		assert(strchr(cp, '/') == NULL);
2742 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2743 	} else {
2744 		*cp = '\0';
2745 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2746 		*cp = '/';
2747 	}
2748 	if (h == NULL)
2749 		return (-1);
2750 	zfs_close(h);
2751 
2752 	/*
2753 	 * Attempt to create, mount, and share any ancestor filesystems,
2754 	 * up to the prefixlen-long one.
2755 	 */
2756 	for (cp = target + prefixlen + 1;
2757 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
2758 		char *logstr;
2759 
2760 		*cp = '\0';
2761 
2762 		h = make_dataset_handle(hdl, target);
2763 		if (h) {
2764 			/* it already exists, nothing to do here */
2765 			zfs_close(h);
2766 			continue;
2767 		}
2768 
2769 		logstr = hdl->libzfs_log_str;
2770 		hdl->libzfs_log_str = NULL;
2771 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2772 		    NULL) != 0) {
2773 			hdl->libzfs_log_str = logstr;
2774 			opname = dgettext(TEXT_DOMAIN, "create");
2775 			goto ancestorerr;
2776 		}
2777 
2778 		hdl->libzfs_log_str = logstr;
2779 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2780 		if (h == NULL) {
2781 			opname = dgettext(TEXT_DOMAIN, "open");
2782 			goto ancestorerr;
2783 		}
2784 
2785 		if (zfs_mount(h, NULL, 0) != 0) {
2786 			opname = dgettext(TEXT_DOMAIN, "mount");
2787 			goto ancestorerr;
2788 		}
2789 
2790 		if (zfs_share(h) != 0) {
2791 			opname = dgettext(TEXT_DOMAIN, "share");
2792 			goto ancestorerr;
2793 		}
2794 
2795 		zfs_close(h);
2796 	}
2797 
2798 	return (0);
2799 
2800 ancestorerr:
2801 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2802 	    "failed to %s ancestor '%s'"), opname, target);
2803 	return (-1);
2804 }
2805 
2806 /*
2807  * Creates non-existing ancestors of the given path.
2808  */
2809 int
2810 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2811 {
2812 	int prefix;
2813 	uint64_t zoned;
2814 	char *path_copy;
2815 	int rc;
2816 
2817 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
2818 		return (-1);
2819 
2820 	if ((path_copy = strdup(path)) != NULL) {
2821 		rc = create_parents(hdl, path_copy, prefix);
2822 		free(path_copy);
2823 	}
2824 	if (path_copy == NULL || rc != 0)
2825 		return (-1);
2826 
2827 	return (0);
2828 }
2829 
2830 /*
2831  * Create a new filesystem or volume.
2832  */
2833 int
2834 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2835     nvlist_t *props)
2836 {
2837 	zfs_cmd_t zc = { 0 };
2838 	int ret;
2839 	uint64_t size = 0;
2840 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2841 	char errbuf[1024];
2842 	uint64_t zoned;
2843 
2844 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2845 	    "cannot create '%s'"), path);
2846 
2847 	/* validate the path, taking care to note the extended error message */
2848 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
2849 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2850 
2851 	/* validate parents exist */
2852 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2853 		return (-1);
2854 
2855 	/*
2856 	 * The failure modes when creating a dataset of a different type over
2857 	 * one that already exists is a little strange.  In particular, if you
2858 	 * try to create a dataset on top of an existing dataset, the ioctl()
2859 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2860 	 * first try to see if the dataset exists.
2861 	 */
2862 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2863 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2864 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2865 		    "dataset already exists"));
2866 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2867 	}
2868 
2869 	if (type == ZFS_TYPE_VOLUME)
2870 		zc.zc_objset_type = DMU_OST_ZVOL;
2871 	else
2872 		zc.zc_objset_type = DMU_OST_ZFS;
2873 
2874 	if (props && (props = zfs_validate_properties(hdl, type, props,
2875 	    zoned, NULL, errbuf)) == 0)
2876 		return (-1);
2877 
2878 	if (type == ZFS_TYPE_VOLUME) {
2879 		/*
2880 		 * If we are creating a volume, the size and block size must
2881 		 * satisfy a few restraints.  First, the blocksize must be a
2882 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
2883 		 * volsize must be a multiple of the block size, and cannot be
2884 		 * zero.
2885 		 */
2886 		if (props == NULL || nvlist_lookup_uint64(props,
2887 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2888 			nvlist_free(props);
2889 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2890 			    "missing volume size"));
2891 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2892 		}
2893 
2894 		if ((ret = nvlist_lookup_uint64(props,
2895 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2896 		    &blocksize)) != 0) {
2897 			if (ret == ENOENT) {
2898 				blocksize = zfs_prop_default_numeric(
2899 				    ZFS_PROP_VOLBLOCKSIZE);
2900 			} else {
2901 				nvlist_free(props);
2902 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2903 				    "missing volume block size"));
2904 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2905 			}
2906 		}
2907 
2908 		if (size == 0) {
2909 			nvlist_free(props);
2910 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2911 			    "volume size cannot be zero"));
2912 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2913 		}
2914 
2915 		if (size % blocksize != 0) {
2916 			nvlist_free(props);
2917 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2918 			    "volume size must be a multiple of volume block "
2919 			    "size"));
2920 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2921 		}
2922 	}
2923 
2924 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2925 		return (-1);
2926 	nvlist_free(props);
2927 
2928 	/* create the dataset */
2929 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2930 
2931 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2932 		ret = zvol_create_link(hdl, path);
2933 		if (ret) {
2934 			(void) zfs_standard_error(hdl, errno,
2935 			    dgettext(TEXT_DOMAIN,
2936 			    "Volume successfully created, but device links "
2937 			    "were not created"));
2938 			zcmd_free_nvlists(&zc);
2939 			return (-1);
2940 		}
2941 	}
2942 
2943 	zcmd_free_nvlists(&zc);
2944 
2945 	/* check for failure */
2946 	if (ret != 0) {
2947 		char parent[ZFS_MAXNAMELEN];
2948 		(void) parent_name(path, parent, sizeof (parent));
2949 
2950 		switch (errno) {
2951 		case ENOENT:
2952 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2953 			    "no such parent '%s'"), parent);
2954 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2955 
2956 		case EINVAL:
2957 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2958 			    "parent '%s' is not a filesystem"), parent);
2959 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2960 
2961 		case EDOM:
2962 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2963 			    "volume block size must be power of 2 from "
2964 			    "%u to %uk"),
2965 			    (uint_t)SPA_MINBLOCKSIZE,
2966 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
2967 
2968 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2969 
2970 		case ENOTSUP:
2971 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2972 			    "pool must be upgraded to set this "
2973 			    "property or value"));
2974 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2975 #ifdef _ILP32
2976 		case EOVERFLOW:
2977 			/*
2978 			 * This platform can't address a volume this big.
2979 			 */
2980 			if (type == ZFS_TYPE_VOLUME)
2981 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
2982 				    errbuf));
2983 #endif
2984 			/* FALLTHROUGH */
2985 		default:
2986 			return (zfs_standard_error(hdl, errno, errbuf));
2987 		}
2988 	}
2989 
2990 	return (0);
2991 }
2992 
2993 /*
2994  * Destroys the given dataset.  The caller must make sure that the filesystem
2995  * isn't mounted, and that there are no active dependents.
2996  */
2997 int
2998 zfs_destroy(zfs_handle_t *zhp)
2999 {
3000 	zfs_cmd_t zc = { 0 };
3001 
3002 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3003 
3004 	if (ZFS_IS_VOLUME(zhp)) {
3005 		/*
3006 		 * If user doesn't have permissions to unshare volume, then
3007 		 * abort the request.  This would only happen for a
3008 		 * non-privileged user.
3009 		 */
3010 		if (zfs_unshare_iscsi(zhp) != 0) {
3011 			return (-1);
3012 		}
3013 
3014 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3015 			return (-1);
3016 
3017 		zc.zc_objset_type = DMU_OST_ZVOL;
3018 	} else {
3019 		zc.zc_objset_type = DMU_OST_ZFS;
3020 	}
3021 
3022 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3023 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3024 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3025 		    zhp->zfs_name));
3026 	}
3027 
3028 	remove_mountpoint(zhp);
3029 
3030 	return (0);
3031 }
3032 
3033 struct destroydata {
3034 	char *snapname;
3035 	boolean_t gotone;
3036 	boolean_t closezhp;
3037 };
3038 
3039 static int
3040 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
3041 {
3042 	struct destroydata *dd = arg;
3043 	zfs_handle_t *szhp;
3044 	char name[ZFS_MAXNAMELEN];
3045 	boolean_t closezhp = dd->closezhp;
3046 	int rv;
3047 
3048 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
3049 	(void) strlcat(name, "@", sizeof (name));
3050 	(void) strlcat(name, dd->snapname, sizeof (name));
3051 
3052 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
3053 	if (szhp) {
3054 		dd->gotone = B_TRUE;
3055 		zfs_close(szhp);
3056 	}
3057 
3058 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3059 		(void) zvol_remove_link(zhp->zfs_hdl, name);
3060 		/*
3061 		 * NB: this is simply a best-effort.  We don't want to
3062 		 * return an error, because then we wouldn't visit all
3063 		 * the volumes.
3064 		 */
3065 	}
3066 
3067 	dd->closezhp = B_TRUE;
3068 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
3069 	if (closezhp)
3070 		zfs_close(zhp);
3071 	return (rv);
3072 }
3073 
3074 /*
3075  * Destroys all snapshots with the given name in zhp & descendants.
3076  */
3077 int
3078 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
3079 {
3080 	zfs_cmd_t zc = { 0 };
3081 	int ret;
3082 	struct destroydata dd = { 0 };
3083 
3084 	dd.snapname = snapname;
3085 	(void) zfs_remove_link_cb(zhp, &dd);
3086 
3087 	if (!dd.gotone) {
3088 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3089 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3090 		    zhp->zfs_name, snapname));
3091 	}
3092 
3093 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3094 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3095 
3096 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3097 	if (ret != 0) {
3098 		char errbuf[1024];
3099 
3100 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3101 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3102 
3103 		switch (errno) {
3104 		case EEXIST:
3105 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3106 			    "snapshot is cloned"));
3107 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3108 
3109 		default:
3110 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3111 			    errbuf));
3112 		}
3113 	}
3114 
3115 	return (0);
3116 }
3117 
3118 /*
3119  * Clones the given dataset.  The target must be of the same type as the source.
3120  */
3121 int
3122 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3123 {
3124 	zfs_cmd_t zc = { 0 };
3125 	char parent[ZFS_MAXNAMELEN];
3126 	int ret;
3127 	char errbuf[1024];
3128 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3129 	zfs_type_t type;
3130 	uint64_t zoned;
3131 
3132 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3133 
3134 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3135 	    "cannot create '%s'"), target);
3136 
3137 	/* validate the target name */
3138 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3139 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3140 
3141 	/* validate parents exist */
3142 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3143 		return (-1);
3144 
3145 	(void) parent_name(target, parent, sizeof (parent));
3146 
3147 	/* do the clone */
3148 	if (ZFS_IS_VOLUME(zhp)) {
3149 		zc.zc_objset_type = DMU_OST_ZVOL;
3150 		type = ZFS_TYPE_VOLUME;
3151 	} else {
3152 		zc.zc_objset_type = DMU_OST_ZFS;
3153 		type = ZFS_TYPE_FILESYSTEM;
3154 	}
3155 
3156 	if (props) {
3157 		if ((props = zfs_validate_properties(hdl, type, props,
3158 		    zoned, zhp, errbuf)) == NULL)
3159 			return (-1);
3160 
3161 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3162 			nvlist_free(props);
3163 			return (-1);
3164 		}
3165 
3166 		nvlist_free(props);
3167 	}
3168 
3169 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3170 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3171 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3172 
3173 	zcmd_free_nvlists(&zc);
3174 
3175 	if (ret != 0) {
3176 		switch (errno) {
3177 
3178 		case ENOENT:
3179 			/*
3180 			 * The parent doesn't exist.  We should have caught this
3181 			 * above, but there may a race condition that has since
3182 			 * destroyed the parent.
3183 			 *
3184 			 * At this point, we don't know whether it's the source
3185 			 * that doesn't exist anymore, or whether the target
3186 			 * dataset doesn't exist.
3187 			 */
3188 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3189 			    "no such parent '%s'"), parent);
3190 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3191 
3192 		case EXDEV:
3193 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3194 			    "source and target pools differ"));
3195 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3196 			    errbuf));
3197 
3198 		default:
3199 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3200 			    errbuf));
3201 		}
3202 	} else if (ZFS_IS_VOLUME(zhp)) {
3203 		ret = zvol_create_link(zhp->zfs_hdl, target);
3204 	}
3205 
3206 	return (ret);
3207 }
3208 
3209 typedef struct promote_data {
3210 	char cb_mountpoint[MAXPATHLEN];
3211 	const char *cb_target;
3212 	const char *cb_errbuf;
3213 	uint64_t cb_pivot_txg;
3214 } promote_data_t;
3215 
3216 static int
3217 promote_snap_cb(zfs_handle_t *zhp, void *data)
3218 {
3219 	promote_data_t *pd = data;
3220 	zfs_handle_t *szhp;
3221 	char snapname[MAXPATHLEN];
3222 	int rv = 0;
3223 
3224 	/* We don't care about snapshots after the pivot point */
3225 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3226 		zfs_close(zhp);
3227 		return (0);
3228 	}
3229 
3230 	/* Remove the device link if it's a zvol. */
3231 	if (ZFS_IS_VOLUME(zhp))
3232 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3233 
3234 	/* Check for conflicting names */
3235 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3236 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3237 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3238 	if (szhp != NULL) {
3239 		zfs_close(szhp);
3240 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3241 		    "snapshot name '%s' from origin \n"
3242 		    "conflicts with '%s' from target"),
3243 		    zhp->zfs_name, snapname);
3244 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3245 	}
3246 	zfs_close(zhp);
3247 	return (rv);
3248 }
3249 
3250 static int
3251 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3252 {
3253 	promote_data_t *pd = data;
3254 
3255 	/* We don't care about snapshots after the pivot point */
3256 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3257 		/* Create the device link if it's a zvol. */
3258 		if (ZFS_IS_VOLUME(zhp))
3259 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3260 	}
3261 
3262 	zfs_close(zhp);
3263 	return (0);
3264 }
3265 
3266 /*
3267  * Promotes the given clone fs to be the clone parent.
3268  */
3269 int
3270 zfs_promote(zfs_handle_t *zhp)
3271 {
3272 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3273 	zfs_cmd_t zc = { 0 };
3274 	char parent[MAXPATHLEN];
3275 	char *cp;
3276 	int ret;
3277 	zfs_handle_t *pzhp;
3278 	promote_data_t pd;
3279 	char errbuf[1024];
3280 
3281 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3282 	    "cannot promote '%s'"), zhp->zfs_name);
3283 
3284 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3285 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3286 		    "snapshots can not be promoted"));
3287 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3288 	}
3289 
3290 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3291 	if (parent[0] == '\0') {
3292 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3293 		    "not a cloned filesystem"));
3294 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3295 	}
3296 	cp = strchr(parent, '@');
3297 	*cp = '\0';
3298 
3299 	/* Walk the snapshots we will be moving */
3300 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3301 	if (pzhp == NULL)
3302 		return (-1);
3303 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3304 	zfs_close(pzhp);
3305 	pd.cb_target = zhp->zfs_name;
3306 	pd.cb_errbuf = errbuf;
3307 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3308 	if (pzhp == NULL)
3309 		return (-1);
3310 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3311 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3312 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3313 	if (ret != 0) {
3314 		zfs_close(pzhp);
3315 		return (-1);
3316 	}
3317 
3318 	/* issue the ioctl */
3319 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3320 	    sizeof (zc.zc_value));
3321 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3322 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3323 
3324 	if (ret != 0) {
3325 		int save_errno = errno;
3326 
3327 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3328 		zfs_close(pzhp);
3329 
3330 		switch (save_errno) {
3331 		case EEXIST:
3332 			/*
3333 			 * There is a conflicting snapshot name.  We
3334 			 * should have caught this above, but they could
3335 			 * have renamed something in the mean time.
3336 			 */
3337 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3338 			    "conflicting snapshot name from parent '%s'"),
3339 			    parent);
3340 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3341 
3342 		default:
3343 			return (zfs_standard_error(hdl, save_errno, errbuf));
3344 		}
3345 	} else {
3346 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3347 	}
3348 
3349 	zfs_close(pzhp);
3350 	return (ret);
3351 }
3352 
3353 struct createdata {
3354 	const char *cd_snapname;
3355 	int cd_ifexists;
3356 };
3357 
3358 static int
3359 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3360 {
3361 	struct createdata *cd = arg;
3362 	int ret;
3363 
3364 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3365 		char name[MAXPATHLEN];
3366 
3367 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
3368 		(void) strlcat(name, "@", sizeof (name));
3369 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
3370 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
3371 		    cd->cd_ifexists);
3372 		/*
3373 		 * NB: this is simply a best-effort.  We don't want to
3374 		 * return an error, because then we wouldn't visit all
3375 		 * the volumes.
3376 		 */
3377 	}
3378 
3379 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3380 
3381 	zfs_close(zhp);
3382 
3383 	return (ret);
3384 }
3385 
3386 /*
3387  * Takes a snapshot of the given dataset.
3388  */
3389 int
3390 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3391 {
3392 	const char *delim;
3393 	char *parent;
3394 	zfs_handle_t *zhp;
3395 	zfs_cmd_t zc = { 0 };
3396 	int ret;
3397 	char errbuf[1024];
3398 
3399 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3400 	    "cannot snapshot '%s'"), path);
3401 
3402 	/* validate the target name */
3403 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3404 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3405 
3406 	/* make sure the parent exists and is of the appropriate type */
3407 	delim = strchr(path, '@');
3408 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
3409 		return (-1);
3410 	(void) strncpy(parent, path, delim - path);
3411 	parent[delim - path] = '\0';
3412 
3413 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3414 	    ZFS_TYPE_VOLUME)) == NULL) {
3415 		free(parent);
3416 		return (-1);
3417 	}
3418 
3419 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3420 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3421 	if (ZFS_IS_VOLUME(zhp))
3422 		zc.zc_objset_type = DMU_OST_ZVOL;
3423 	else
3424 		zc.zc_objset_type = DMU_OST_ZFS;
3425 	zc.zc_cookie = recursive;
3426 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3427 
3428 	/*
3429 	 * if it was recursive, the one that actually failed will be in
3430 	 * zc.zc_name.
3431 	 */
3432 	if (ret != 0)
3433 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3434 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3435 
3436 	if (ret == 0 && recursive) {
3437 		struct createdata cd;
3438 
3439 		cd.cd_snapname = delim + 1;
3440 		cd.cd_ifexists = B_FALSE;
3441 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3442 	}
3443 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3444 		ret = zvol_create_link(zhp->zfs_hdl, path);
3445 		if (ret != 0) {
3446 			(void) zfs_standard_error(hdl, errno,
3447 			    dgettext(TEXT_DOMAIN,
3448 			    "Volume successfully snapshotted, but device links "
3449 			    "were not created"));
3450 			free(parent);
3451 			zfs_close(zhp);
3452 			return (-1);
3453 		}
3454 	}
3455 
3456 	if (ret != 0)
3457 		(void) zfs_standard_error(hdl, errno, errbuf);
3458 
3459 	free(parent);
3460 	zfs_close(zhp);
3461 
3462 	return (ret);
3463 }
3464 
3465 /*
3466  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3467  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3468  * is a dependent and we should just destroy it without checking the transaction
3469  * group.
3470  */
3471 typedef struct rollback_data {
3472 	const char	*cb_target;		/* the snapshot */
3473 	uint64_t	cb_create;		/* creation time reference */
3474 	boolean_t	cb_error;
3475 	boolean_t	cb_dependent;
3476 	boolean_t	cb_force;
3477 } rollback_data_t;
3478 
3479 static int
3480 rollback_destroy(zfs_handle_t *zhp, void *data)
3481 {
3482 	rollback_data_t *cbp = data;
3483 
3484 	if (!cbp->cb_dependent) {
3485 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3486 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3487 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3488 		    cbp->cb_create) {
3489 			char *logstr;
3490 
3491 			cbp->cb_dependent = B_TRUE;
3492 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3493 			    rollback_destroy, cbp);
3494 			cbp->cb_dependent = B_FALSE;
3495 
3496 			logstr = zhp->zfs_hdl->libzfs_log_str;
3497 			zhp->zfs_hdl->libzfs_log_str = NULL;
3498 			cbp->cb_error |= zfs_destroy(zhp);
3499 			zhp->zfs_hdl->libzfs_log_str = logstr;
3500 		}
3501 	} else {
3502 		/* We must destroy this clone; first unmount it */
3503 		prop_changelist_t *clp;
3504 
3505 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
3506 		    cbp->cb_force ? MS_FORCE: 0);
3507 		if (clp == NULL || changelist_prefix(clp) != 0) {
3508 			cbp->cb_error = B_TRUE;
3509 			zfs_close(zhp);
3510 			return (0);
3511 		}
3512 		if (zfs_destroy(zhp) != 0)
3513 			cbp->cb_error = B_TRUE;
3514 		else
3515 			changelist_remove(clp, zhp->zfs_name);
3516 		(void) changelist_postfix(clp);
3517 		changelist_free(clp);
3518 	}
3519 
3520 	zfs_close(zhp);
3521 	return (0);
3522 }
3523 
3524 /*
3525  * Given a dataset, rollback to a specific snapshot, discarding any
3526  * data changes since then and making it the active dataset.
3527  *
3528  * Any snapshots more recent than the target are destroyed, along with
3529  * their dependents.
3530  */
3531 int
3532 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3533 {
3534 	rollback_data_t cb = { 0 };
3535 	int err;
3536 	zfs_cmd_t zc = { 0 };
3537 	boolean_t restore_resv = 0;
3538 	uint64_t old_volsize, new_volsize;
3539 	zfs_prop_t resv_prop;
3540 
3541 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3542 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3543 
3544 	/*
3545 	 * Destroy all recent snapshots and its dependends.
3546 	 */
3547 	cb.cb_force = force;
3548 	cb.cb_target = snap->zfs_name;
3549 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3550 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
3551 
3552 	if (cb.cb_error)
3553 		return (-1);
3554 
3555 	/*
3556 	 * Now that we have verified that the snapshot is the latest,
3557 	 * rollback to the given snapshot.
3558 	 */
3559 
3560 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3561 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3562 			return (-1);
3563 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3564 			return (-1);
3565 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3566 		restore_resv =
3567 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3568 	}
3569 
3570 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3571 
3572 	if (ZFS_IS_VOLUME(zhp))
3573 		zc.zc_objset_type = DMU_OST_ZVOL;
3574 	else
3575 		zc.zc_objset_type = DMU_OST_ZFS;
3576 
3577 	/*
3578 	 * We rely on zfs_iter_children() to verify that there are no
3579 	 * newer snapshots for the given dataset.  Therefore, we can
3580 	 * simply pass the name on to the ioctl() call.  There is still
3581 	 * an unlikely race condition where the user has taken a
3582 	 * snapshot since we verified that this was the most recent.
3583 	 *
3584 	 */
3585 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3586 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3587 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3588 		    zhp->zfs_name);
3589 		return (err);
3590 	}
3591 
3592 	/*
3593 	 * For volumes, if the pre-rollback volsize matched the pre-
3594 	 * rollback reservation and the volsize has changed then set
3595 	 * the reservation property to the post-rollback volsize.
3596 	 * Make a new handle since the rollback closed the dataset.
3597 	 */
3598 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3599 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3600 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
3601 			zfs_close(zhp);
3602 			return (err);
3603 		}
3604 		if (restore_resv) {
3605 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3606 			if (old_volsize != new_volsize)
3607 				err = zfs_prop_set_int(zhp, resv_prop,
3608 				    new_volsize);
3609 		}
3610 		zfs_close(zhp);
3611 	}
3612 	return (err);
3613 }
3614 
3615 /*
3616  * Iterate over all dependents for a given dataset.  This includes both
3617  * hierarchical dependents (children) and data dependents (snapshots and
3618  * clones).  The bulk of the processing occurs in get_dependents() in
3619  * libzfs_graph.c.
3620  */
3621 int
3622 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3623     zfs_iter_f func, void *data)
3624 {
3625 	char **dependents;
3626 	size_t count;
3627 	int i;
3628 	zfs_handle_t *child;
3629 	int ret = 0;
3630 
3631 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3632 	    &dependents, &count) != 0)
3633 		return (-1);
3634 
3635 	for (i = 0; i < count; i++) {
3636 		if ((child = make_dataset_handle(zhp->zfs_hdl,
3637 		    dependents[i])) == NULL)
3638 			continue;
3639 
3640 		if ((ret = func(child, data)) != 0)
3641 			break;
3642 	}
3643 
3644 	for (i = 0; i < count; i++)
3645 		free(dependents[i]);
3646 	free(dependents);
3647 
3648 	return (ret);
3649 }
3650 
3651 /*
3652  * Renames the given dataset.
3653  */
3654 int
3655 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3656 {
3657 	int ret;
3658 	zfs_cmd_t zc = { 0 };
3659 	char *delim;
3660 	prop_changelist_t *cl = NULL;
3661 	zfs_handle_t *zhrp = NULL;
3662 	char *parentname = NULL;
3663 	char parent[ZFS_MAXNAMELEN];
3664 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3665 	char errbuf[1024];
3666 
3667 	/* if we have the same exact name, just return success */
3668 	if (strcmp(zhp->zfs_name, target) == 0)
3669 		return (0);
3670 
3671 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3672 	    "cannot rename to '%s'"), target);
3673 
3674 	/*
3675 	 * Make sure the target name is valid
3676 	 */
3677 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3678 		if ((strchr(target, '@') == NULL) ||
3679 		    *target == '@') {
3680 			/*
3681 			 * Snapshot target name is abbreviated,
3682 			 * reconstruct full dataset name
3683 			 */
3684 			(void) strlcpy(parent, zhp->zfs_name,
3685 			    sizeof (parent));
3686 			delim = strchr(parent, '@');
3687 			if (strchr(target, '@') == NULL)
3688 				*(++delim) = '\0';
3689 			else
3690 				*delim = '\0';
3691 			(void) strlcat(parent, target, sizeof (parent));
3692 			target = parent;
3693 		} else {
3694 			/*
3695 			 * Make sure we're renaming within the same dataset.
3696 			 */
3697 			delim = strchr(target, '@');
3698 			if (strncmp(zhp->zfs_name, target, delim - target)
3699 			    != 0 || zhp->zfs_name[delim - target] != '@') {
3700 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3701 				    "snapshots must be part of same "
3702 				    "dataset"));
3703 				return (zfs_error(hdl, EZFS_CROSSTARGET,
3704 				    errbuf));
3705 			}
3706 		}
3707 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3708 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3709 	} else {
3710 		if (recursive) {
3711 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3712 			    "recursive rename must be a snapshot"));
3713 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3714 		}
3715 
3716 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3717 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3718 		uint64_t unused;
3719 
3720 		/* validate parents */
3721 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3722 			return (-1);
3723 
3724 		(void) parent_name(target, parent, sizeof (parent));
3725 
3726 		/* make sure we're in the same pool */
3727 		verify((delim = strchr(target, '/')) != NULL);
3728 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3729 		    zhp->zfs_name[delim - target] != '/') {
3730 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3731 			    "datasets must be within same pool"));
3732 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3733 		}
3734 
3735 		/* new name cannot be a child of the current dataset name */
3736 		if (strncmp(parent, zhp->zfs_name,
3737 		    strlen(zhp->zfs_name)) == 0) {
3738 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3739 			    "New dataset name cannot be a descendent of "
3740 			    "current dataset name"));
3741 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3742 		}
3743 	}
3744 
3745 	(void) snprintf(errbuf, sizeof (errbuf),
3746 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3747 
3748 	if (getzoneid() == GLOBAL_ZONEID &&
3749 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3750 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3751 		    "dataset is used in a non-global zone"));
3752 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3753 	}
3754 
3755 	if (recursive) {
3756 		struct destroydata dd;
3757 
3758 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3759 		if (parentname == NULL) {
3760 			ret = -1;
3761 			goto error;
3762 		}
3763 		delim = strchr(parentname, '@');
3764 		*delim = '\0';
3765 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3766 		if (zhrp == NULL) {
3767 			ret = -1;
3768 			goto error;
3769 		}
3770 
3771 		dd.snapname = delim + 1;
3772 		dd.gotone = B_FALSE;
3773 		dd.closezhp = B_TRUE;
3774 
3775 		/* We remove any zvol links prior to renaming them */
3776 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
3777 		if (ret) {
3778 			goto error;
3779 		}
3780 	} else {
3781 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
3782 			return (-1);
3783 
3784 		if (changelist_haszonedchild(cl)) {
3785 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3786 			    "child dataset with inherited mountpoint is used "
3787 			    "in a non-global zone"));
3788 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3789 			goto error;
3790 		}
3791 
3792 		if ((ret = changelist_prefix(cl)) != 0)
3793 			goto error;
3794 	}
3795 
3796 	if (ZFS_IS_VOLUME(zhp))
3797 		zc.zc_objset_type = DMU_OST_ZVOL;
3798 	else
3799 		zc.zc_objset_type = DMU_OST_ZFS;
3800 
3801 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3802 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3803 
3804 	zc.zc_cookie = recursive;
3805 
3806 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3807 		/*
3808 		 * if it was recursive, the one that actually failed will
3809 		 * be in zc.zc_name
3810 		 */
3811 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3812 		    "cannot rename '%s'"), zc.zc_name);
3813 
3814 		if (recursive && errno == EEXIST) {
3815 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3816 			    "a child dataset already has a snapshot "
3817 			    "with the new name"));
3818 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3819 		} else {
3820 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3821 		}
3822 
3823 		/*
3824 		 * On failure, we still want to remount any filesystems that
3825 		 * were previously mounted, so we don't alter the system state.
3826 		 */
3827 		if (recursive) {
3828 			struct createdata cd;
3829 
3830 			/* only create links for datasets that had existed */
3831 			cd.cd_snapname = delim + 1;
3832 			cd.cd_ifexists = B_TRUE;
3833 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3834 			    &cd);
3835 		} else {
3836 			(void) changelist_postfix(cl);
3837 		}
3838 	} else {
3839 		if (recursive) {
3840 			struct createdata cd;
3841 
3842 			/* only create links for datasets that had existed */
3843 			cd.cd_snapname = strchr(target, '@') + 1;
3844 			cd.cd_ifexists = B_TRUE;
3845 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3846 			    &cd);
3847 		} else {
3848 			changelist_rename(cl, zfs_get_name(zhp), target);
3849 			ret = changelist_postfix(cl);
3850 		}
3851 	}
3852 
3853 error:
3854 	if (parentname) {
3855 		free(parentname);
3856 	}
3857 	if (zhrp) {
3858 		zfs_close(zhrp);
3859 	}
3860 	if (cl) {
3861 		changelist_free(cl);
3862 	}
3863 	return (ret);
3864 }
3865 
3866 /*
3867  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3868  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3869  */
3870 int
3871 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3872 {
3873 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
3874 }
3875 
3876 static int
3877 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3878 {
3879 	zfs_cmd_t zc = { 0 };
3880 	di_devlink_handle_t dhdl;
3881 	priv_set_t *priv_effective;
3882 	int privileged;
3883 
3884 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3885 
3886 	/*
3887 	 * Issue the appropriate ioctl.
3888 	 */
3889 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3890 		switch (errno) {
3891 		case EEXIST:
3892 			/*
3893 			 * Silently ignore the case where the link already
3894 			 * exists.  This allows 'zfs volinit' to be run multiple
3895 			 * times without errors.
3896 			 */
3897 			return (0);
3898 
3899 		case ENOENT:
3900 			/*
3901 			 * Dataset does not exist in the kernel.  If we
3902 			 * don't care (see zfs_rename), then ignore the
3903 			 * error quietly.
3904 			 */
3905 			if (ifexists) {
3906 				return (0);
3907 			}
3908 
3909 			/* FALLTHROUGH */
3910 
3911 		default:
3912 			return (zfs_standard_error_fmt(hdl, errno,
3913 			    dgettext(TEXT_DOMAIN, "cannot create device links "
3914 			    "for '%s'"), dataset));
3915 		}
3916 	}
3917 
3918 	/*
3919 	 * If privileged call devfsadm and wait for the links to
3920 	 * magically appear.
3921 	 * Otherwise, print out an informational message.
3922 	 */
3923 
3924 	priv_effective = priv_allocset();
3925 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
3926 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
3927 	priv_freeset(priv_effective);
3928 
3929 	if (privileged) {
3930 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
3931 		    DI_MAKE_LINK)) == NULL) {
3932 			zfs_error_aux(hdl, strerror(errno));
3933 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
3934 			    dgettext(TEXT_DOMAIN, "cannot create device links "
3935 			    "for '%s'"), dataset);
3936 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3937 			return (-1);
3938 		} else {
3939 			(void) di_devlink_fini(&dhdl);
3940 		}
3941 	} else {
3942 		char pathname[MAXPATHLEN];
3943 		struct stat64 statbuf;
3944 		int i;
3945 
3946 #define	MAX_WAIT	10
3947 
3948 		/*
3949 		 * This is the poor mans way of waiting for the link
3950 		 * to show up.  If after 10 seconds we still don't
3951 		 * have it, then print out a message.
3952 		 */
3953 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
3954 		    dataset);
3955 
3956 		for (i = 0; i != MAX_WAIT; i++) {
3957 			if (stat64(pathname, &statbuf) == 0)
3958 				break;
3959 			(void) sleep(1);
3960 		}
3961 		if (i == MAX_WAIT)
3962 			(void) printf(gettext("%s may not be immediately "
3963 			    "available\n"), pathname);
3964 	}
3965 
3966 	return (0);
3967 }
3968 
3969 /*
3970  * Remove a minor node for the given zvol and the associated /dev links.
3971  */
3972 int
3973 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3974 {
3975 	zfs_cmd_t zc = { 0 };
3976 
3977 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3978 
3979 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3980 		switch (errno) {
3981 		case ENXIO:
3982 			/*
3983 			 * Silently ignore the case where the link no longer
3984 			 * exists, so that 'zfs volfini' can be run multiple
3985 			 * times without errors.
3986 			 */
3987 			return (0);
3988 
3989 		default:
3990 			return (zfs_standard_error_fmt(hdl, errno,
3991 			    dgettext(TEXT_DOMAIN, "cannot remove device "
3992 			    "links for '%s'"), dataset));
3993 		}
3994 	}
3995 
3996 	return (0);
3997 }
3998 
3999 nvlist_t *
4000 zfs_get_user_props(zfs_handle_t *zhp)
4001 {
4002 	return (zhp->zfs_user_props);
4003 }
4004 
4005 /*
4006  * This function is used by 'zfs list' to determine the exact set of columns to
4007  * display, and their maximum widths.  This does two main things:
4008  *
4009  *      - If this is a list of all properties, then expand the list to include
4010  *        all native properties, and set a flag so that for each dataset we look
4011  *        for new unique user properties and add them to the list.
4012  *
4013  *      - For non fixed-width properties, keep track of the maximum width seen
4014  *        so that we can size the column appropriately.
4015  */
4016 int
4017 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
4018 {
4019 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4020 	zprop_list_t *entry;
4021 	zprop_list_t **last, **start;
4022 	nvlist_t *userprops, *propval;
4023 	nvpair_t *elem;
4024 	char *strval;
4025 	char buf[ZFS_MAXPROPLEN];
4026 
4027 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4028 		return (-1);
4029 
4030 	userprops = zfs_get_user_props(zhp);
4031 
4032 	entry = *plp;
4033 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4034 		/*
4035 		 * Go through and add any user properties as necessary.  We
4036 		 * start by incrementing our list pointer to the first
4037 		 * non-native property.
4038 		 */
4039 		start = plp;
4040 		while (*start != NULL) {
4041 			if ((*start)->pl_prop == ZPROP_INVAL)
4042 				break;
4043 			start = &(*start)->pl_next;
4044 		}
4045 
4046 		elem = NULL;
4047 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4048 			/*
4049 			 * See if we've already found this property in our list.
4050 			 */
4051 			for (last = start; *last != NULL;
4052 			    last = &(*last)->pl_next) {
4053 				if (strcmp((*last)->pl_user_prop,
4054 				    nvpair_name(elem)) == 0)
4055 					break;
4056 			}
4057 
4058 			if (*last == NULL) {
4059 				if ((entry = zfs_alloc(hdl,
4060 				    sizeof (zprop_list_t))) == NULL ||
4061 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4062 				    nvpair_name(elem)))) == NULL) {
4063 					free(entry);
4064 					return (-1);
4065 				}
4066 
4067 				entry->pl_prop = ZPROP_INVAL;
4068 				entry->pl_width = strlen(nvpair_name(elem));
4069 				entry->pl_all = B_TRUE;
4070 				*last = entry;
4071 			}
4072 		}
4073 	}
4074 
4075 	/*
4076 	 * Now go through and check the width of any non-fixed columns
4077 	 */
4078 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4079 		if (entry->pl_fixed)
4080 			continue;
4081 
4082 		if (entry->pl_prop != ZPROP_INVAL) {
4083 			if (zfs_prop_get(zhp, entry->pl_prop,
4084 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4085 				if (strlen(buf) > entry->pl_width)
4086 					entry->pl_width = strlen(buf);
4087 			}
4088 		} else if (nvlist_lookup_nvlist(userprops,
4089 		    entry->pl_user_prop, &propval)  == 0) {
4090 			verify(nvlist_lookup_string(propval,
4091 			    ZPROP_VALUE, &strval) == 0);
4092 			if (strlen(strval) > entry->pl_width)
4093 				entry->pl_width = strlen(strval);
4094 		}
4095 	}
4096 
4097 	return (0);
4098 }
4099 
4100 int
4101 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
4102 {
4103 	zfs_cmd_t zc = { 0 };
4104 	nvlist_t *nvp;
4105 	gid_t gid;
4106 	uid_t uid;
4107 	const gid_t *groups;
4108 	int group_cnt;
4109 	int error;
4110 
4111 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
4112 		return (no_memory(hdl));
4113 
4114 	uid = ucred_geteuid(cred);
4115 	gid = ucred_getegid(cred);
4116 	group_cnt = ucred_getgroups(cred, &groups);
4117 
4118 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
4119 		return (1);
4120 
4121 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
4122 		nvlist_free(nvp);
4123 		return (1);
4124 	}
4125 
4126 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
4127 		nvlist_free(nvp);
4128 		return (1);
4129 	}
4130 
4131 	if (nvlist_add_uint32_array(nvp,
4132 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
4133 		nvlist_free(nvp);
4134 		return (1);
4135 	}
4136 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4137 
4138 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
4139 		return (-1);
4140 
4141 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
4142 	nvlist_free(nvp);
4143 	return (error);
4144 }
4145 
4146 int
4147 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4148     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
4149 {
4150 	zfs_cmd_t zc = { 0 };
4151 	int error;
4152 
4153 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4154 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4155 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4156 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4157 	zc.zc_share.z_sharetype = operation;
4158 	zc.zc_share.z_sharemax = sharemax;
4159 
4160 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4161 	return (error);
4162 }
4163