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