xref: /titanic_50/usr/src/lib/libzfs/common/libzfs_dataset.c (revision a5eb7107f06a6e23e8e77e8d3a84c1ff90a73ac6)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
27  * Copyright (c) 2013 Martin Matuska. All rights reserved.
28  * Copyright (c) 2013 Steven Hartland. All rights reserved.
29  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
30  */
31 
32 #include <ctype.h>
33 #include <errno.h>
34 #include <libintl.h>
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <stddef.h>
41 #include <zone.h>
42 #include <fcntl.h>
43 #include <sys/mntent.h>
44 #include <sys/mount.h>
45 #include <priv.h>
46 #include <pwd.h>
47 #include <grp.h>
48 #include <stddef.h>
49 #include <ucred.h>
50 #include <idmap.h>
51 #include <aclutils.h>
52 #include <directory.h>
53 
54 #include <sys/dnode.h>
55 #include <sys/spa.h>
56 #include <sys/zap.h>
57 #include <libzfs.h>
58 
59 #include "zfs_namecheck.h"
60 #include "zfs_prop.h"
61 #include "libzfs_impl.h"
62 #include "zfs_deleg.h"
63 
64 static int userquota_propname_decode(const char *propname, boolean_t zoned,
65     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
66 
67 /*
68  * Given a single type (not a mask of types), return the type in a human
69  * readable form.
70  */
71 const char *
72 zfs_type_to_name(zfs_type_t type)
73 {
74 	switch (type) {
75 	case ZFS_TYPE_FILESYSTEM:
76 		return (dgettext(TEXT_DOMAIN, "filesystem"));
77 	case ZFS_TYPE_SNAPSHOT:
78 		return (dgettext(TEXT_DOMAIN, "snapshot"));
79 	case ZFS_TYPE_VOLUME:
80 		return (dgettext(TEXT_DOMAIN, "volume"));
81 	}
82 
83 	return (NULL);
84 }
85 
86 /*
87  * Given a path and mask of ZFS types, return a string describing this dataset.
88  * This is used when we fail to open a dataset and we cannot get an exact type.
89  * We guess what the type would have been based on the path and the mask of
90  * acceptable types.
91  */
92 static const char *
93 path_to_str(const char *path, int types)
94 {
95 	/*
96 	 * When given a single type, always report the exact type.
97 	 */
98 	if (types == ZFS_TYPE_SNAPSHOT)
99 		return (dgettext(TEXT_DOMAIN, "snapshot"));
100 	if (types == ZFS_TYPE_FILESYSTEM)
101 		return (dgettext(TEXT_DOMAIN, "filesystem"));
102 	if (types == ZFS_TYPE_VOLUME)
103 		return (dgettext(TEXT_DOMAIN, "volume"));
104 
105 	/*
106 	 * The user is requesting more than one type of dataset.  If this is the
107 	 * case, consult the path itself.  If we're looking for a snapshot, and
108 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
109 	 * snapshot attribute and try again.
110 	 */
111 	if (types & ZFS_TYPE_SNAPSHOT) {
112 		if (strchr(path, '@') != NULL)
113 			return (dgettext(TEXT_DOMAIN, "snapshot"));
114 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
115 	}
116 
117 	/*
118 	 * The user has requested either filesystems or volumes.
119 	 * We have no way of knowing a priori what type this would be, so always
120 	 * report it as "filesystem" or "volume", our two primitive types.
121 	 */
122 	if (types & ZFS_TYPE_FILESYSTEM)
123 		return (dgettext(TEXT_DOMAIN, "filesystem"));
124 
125 	assert(types & ZFS_TYPE_VOLUME);
126 	return (dgettext(TEXT_DOMAIN, "volume"));
127 }
128 
129 /*
130  * Validate a ZFS path.  This is used even before trying to open the dataset, to
131  * provide a more meaningful error message.  We call zfs_error_aux() to
132  * explain exactly why the name was not valid.
133  */
134 int
135 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
136     boolean_t modifying)
137 {
138 	namecheck_err_t why;
139 	char what;
140 
141 	(void) zfs_prop_get_table();
142 	if (dataset_namecheck(path, &why, &what) != 0) {
143 		if (hdl != NULL) {
144 			switch (why) {
145 			case NAME_ERR_TOOLONG:
146 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
147 				    "name is too long"));
148 				break;
149 
150 			case NAME_ERR_LEADING_SLASH:
151 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152 				    "leading slash in name"));
153 				break;
154 
155 			case NAME_ERR_EMPTY_COMPONENT:
156 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157 				    "empty component in name"));
158 				break;
159 
160 			case NAME_ERR_TRAILING_SLASH:
161 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
162 				    "trailing slash in name"));
163 				break;
164 
165 			case NAME_ERR_INVALCHAR:
166 				zfs_error_aux(hdl,
167 				    dgettext(TEXT_DOMAIN, "invalid character "
168 				    "'%c' in name"), what);
169 				break;
170 
171 			case NAME_ERR_MULTIPLE_AT:
172 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173 				    "multiple '@' delimiters in name"));
174 				break;
175 
176 			case NAME_ERR_NOLETTER:
177 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178 				    "pool doesn't begin with a letter"));
179 				break;
180 
181 			case NAME_ERR_RESERVED:
182 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183 				    "name is reserved"));
184 				break;
185 
186 			case NAME_ERR_DISKLIKE:
187 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
188 				    "reserved disk name"));
189 				break;
190 			}
191 		}
192 
193 		return (0);
194 	}
195 
196 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
197 		if (hdl != NULL)
198 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199 			    "snapshot delimiter '@' in filesystem name"));
200 		return (0);
201 	}
202 
203 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
204 		if (hdl != NULL)
205 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
206 			    "missing '@' delimiter in snapshot name"));
207 		return (0);
208 	}
209 
210 	if (modifying && strchr(path, '%') != NULL) {
211 		if (hdl != NULL)
212 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
213 			    "invalid character %c in name"), '%');
214 		return (0);
215 	}
216 
217 	return (-1);
218 }
219 
220 int
221 zfs_name_valid(const char *name, zfs_type_t type)
222 {
223 	if (type == ZFS_TYPE_POOL)
224 		return (zpool_name_valid(NULL, B_FALSE, name));
225 	return (zfs_validate_name(NULL, name, type, B_FALSE));
226 }
227 
228 /*
229  * This function takes the raw DSL properties, and filters out the user-defined
230  * properties into a separate nvlist.
231  */
232 static nvlist_t *
233 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
234 {
235 	libzfs_handle_t *hdl = zhp->zfs_hdl;
236 	nvpair_t *elem;
237 	nvlist_t *propval;
238 	nvlist_t *nvl;
239 
240 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
241 		(void) no_memory(hdl);
242 		return (NULL);
243 	}
244 
245 	elem = NULL;
246 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
247 		if (!zfs_prop_user(nvpair_name(elem)))
248 			continue;
249 
250 		verify(nvpair_value_nvlist(elem, &propval) == 0);
251 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
252 			nvlist_free(nvl);
253 			(void) no_memory(hdl);
254 			return (NULL);
255 		}
256 	}
257 
258 	return (nvl);
259 }
260 
261 static zpool_handle_t *
262 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
263 {
264 	libzfs_handle_t *hdl = zhp->zfs_hdl;
265 	zpool_handle_t *zph;
266 
267 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
268 		if (hdl->libzfs_pool_handles != NULL)
269 			zph->zpool_next = hdl->libzfs_pool_handles;
270 		hdl->libzfs_pool_handles = zph;
271 	}
272 	return (zph);
273 }
274 
275 static zpool_handle_t *
276 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
277 {
278 	libzfs_handle_t *hdl = zhp->zfs_hdl;
279 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
280 
281 	while ((zph != NULL) &&
282 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
283 		zph = zph->zpool_next;
284 	return (zph);
285 }
286 
287 /*
288  * Returns a handle to the pool that contains the provided dataset.
289  * If a handle to that pool already exists then that handle is returned.
290  * Otherwise, a new handle is created and added to the list of handles.
291  */
292 static zpool_handle_t *
293 zpool_handle(zfs_handle_t *zhp)
294 {
295 	char *pool_name;
296 	int len;
297 	zpool_handle_t *zph;
298 
299 	len = strcspn(zhp->zfs_name, "/@#") + 1;
300 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
301 	(void) strlcpy(pool_name, zhp->zfs_name, len);
302 
303 	zph = zpool_find_handle(zhp, pool_name, len);
304 	if (zph == NULL)
305 		zph = zpool_add_handle(zhp, pool_name);
306 
307 	free(pool_name);
308 	return (zph);
309 }
310 
311 void
312 zpool_free_handles(libzfs_handle_t *hdl)
313 {
314 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
315 
316 	while (zph != NULL) {
317 		next = zph->zpool_next;
318 		zpool_close(zph);
319 		zph = next;
320 	}
321 	hdl->libzfs_pool_handles = NULL;
322 }
323 
324 /*
325  * Utility function to gather stats (objset and zpl) for the given object.
326  */
327 static int
328 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
329 {
330 	libzfs_handle_t *hdl = zhp->zfs_hdl;
331 
332 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
333 
334 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
335 		if (errno == ENOMEM) {
336 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
337 				return (-1);
338 			}
339 		} else {
340 			return (-1);
341 		}
342 	}
343 	return (0);
344 }
345 
346 /*
347  * Utility function to get the received properties of the given object.
348  */
349 static int
350 get_recvd_props_ioctl(zfs_handle_t *zhp)
351 {
352 	libzfs_handle_t *hdl = zhp->zfs_hdl;
353 	nvlist_t *recvdprops;
354 	zfs_cmd_t zc = { 0 };
355 	int err;
356 
357 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
358 		return (-1);
359 
360 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
361 
362 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
363 		if (errno == ENOMEM) {
364 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
365 				return (-1);
366 			}
367 		} else {
368 			zcmd_free_nvlists(&zc);
369 			return (-1);
370 		}
371 	}
372 
373 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
374 	zcmd_free_nvlists(&zc);
375 	if (err != 0)
376 		return (-1);
377 
378 	nvlist_free(zhp->zfs_recvd_props);
379 	zhp->zfs_recvd_props = recvdprops;
380 
381 	return (0);
382 }
383 
384 static int
385 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
386 {
387 	nvlist_t *allprops, *userprops;
388 
389 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
390 
391 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
392 		return (-1);
393 	}
394 
395 	/*
396 	 * XXX Why do we store the user props separately, in addition to
397 	 * storing them in zfs_props?
398 	 */
399 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
400 		nvlist_free(allprops);
401 		return (-1);
402 	}
403 
404 	nvlist_free(zhp->zfs_props);
405 	nvlist_free(zhp->zfs_user_props);
406 
407 	zhp->zfs_props = allprops;
408 	zhp->zfs_user_props = userprops;
409 
410 	return (0);
411 }
412 
413 static int
414 get_stats(zfs_handle_t *zhp)
415 {
416 	int rc = 0;
417 	zfs_cmd_t zc = { 0 };
418 
419 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
420 		return (-1);
421 	if (get_stats_ioctl(zhp, &zc) != 0)
422 		rc = -1;
423 	else if (put_stats_zhdl(zhp, &zc) != 0)
424 		rc = -1;
425 	zcmd_free_nvlists(&zc);
426 	return (rc);
427 }
428 
429 /*
430  * Refresh the properties currently stored in the handle.
431  */
432 void
433 zfs_refresh_properties(zfs_handle_t *zhp)
434 {
435 	(void) get_stats(zhp);
436 }
437 
438 /*
439  * Makes a handle from the given dataset name.  Used by zfs_open() and
440  * zfs_iter_* to create child handles on the fly.
441  */
442 static int
443 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
444 {
445 	if (put_stats_zhdl(zhp, zc) != 0)
446 		return (-1);
447 
448 	/*
449 	 * We've managed to open the dataset and gather statistics.  Determine
450 	 * the high-level type.
451 	 */
452 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
453 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
454 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
455 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
456 	else
457 		abort();
458 
459 	if (zhp->zfs_dmustats.dds_is_snapshot)
460 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
461 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
462 		zhp->zfs_type = ZFS_TYPE_VOLUME;
463 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
464 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
465 	else
466 		abort();	/* we should never see any other types */
467 
468 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
469 		return (-1);
470 
471 	return (0);
472 }
473 
474 zfs_handle_t *
475 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
476 {
477 	zfs_cmd_t zc = { 0 };
478 
479 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
480 
481 	if (zhp == NULL)
482 		return (NULL);
483 
484 	zhp->zfs_hdl = hdl;
485 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
486 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
487 		free(zhp);
488 		return (NULL);
489 	}
490 	if (get_stats_ioctl(zhp, &zc) == -1) {
491 		zcmd_free_nvlists(&zc);
492 		free(zhp);
493 		return (NULL);
494 	}
495 	if (make_dataset_handle_common(zhp, &zc) == -1) {
496 		free(zhp);
497 		zhp = NULL;
498 	}
499 	zcmd_free_nvlists(&zc);
500 	return (zhp);
501 }
502 
503 zfs_handle_t *
504 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
505 {
506 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
507 
508 	if (zhp == NULL)
509 		return (NULL);
510 
511 	zhp->zfs_hdl = hdl;
512 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
513 	if (make_dataset_handle_common(zhp, zc) == -1) {
514 		free(zhp);
515 		return (NULL);
516 	}
517 	return (zhp);
518 }
519 
520 zfs_handle_t *
521 zfs_handle_dup(zfs_handle_t *zhp_orig)
522 {
523 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
524 
525 	if (zhp == NULL)
526 		return (NULL);
527 
528 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
529 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
530 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
531 	    sizeof (zhp->zfs_name));
532 	zhp->zfs_type = zhp_orig->zfs_type;
533 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
534 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
535 	if (zhp_orig->zfs_props != NULL) {
536 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
537 			(void) no_memory(zhp->zfs_hdl);
538 			zfs_close(zhp);
539 			return (NULL);
540 		}
541 	}
542 	if (zhp_orig->zfs_user_props != NULL) {
543 		if (nvlist_dup(zhp_orig->zfs_user_props,
544 		    &zhp->zfs_user_props, 0) != 0) {
545 			(void) no_memory(zhp->zfs_hdl);
546 			zfs_close(zhp);
547 			return (NULL);
548 		}
549 	}
550 	if (zhp_orig->zfs_recvd_props != NULL) {
551 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
552 		    &zhp->zfs_recvd_props, 0)) {
553 			(void) no_memory(zhp->zfs_hdl);
554 			zfs_close(zhp);
555 			return (NULL);
556 		}
557 	}
558 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
559 	if (zhp_orig->zfs_mntopts != NULL) {
560 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
561 		    zhp_orig->zfs_mntopts);
562 	}
563 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
564 	return (zhp);
565 }
566 
567 boolean_t
568 zfs_bookmark_exists(const char *path)
569 {
570 	nvlist_t *bmarks;
571 	nvlist_t *props;
572 	char fsname[ZFS_MAXNAMELEN];
573 	char *bmark_name;
574 	char *pound;
575 	int err;
576 	boolean_t rv;
577 
578 
579 	(void) strlcpy(fsname, path, sizeof (fsname));
580 	pound = strchr(fsname, '#');
581 	if (pound == NULL)
582 		return (B_FALSE);
583 
584 	*pound = '\0';
585 	bmark_name = pound + 1;
586 	props = fnvlist_alloc();
587 	err = lzc_get_bookmarks(fsname, props, &bmarks);
588 	nvlist_free(props);
589 	if (err != 0) {
590 		nvlist_free(bmarks);
591 		return (B_FALSE);
592 	}
593 
594 	rv = nvlist_exists(bmarks, bmark_name);
595 	nvlist_free(bmarks);
596 	return (rv);
597 }
598 
599 zfs_handle_t *
600 make_bookmark_handle(zfs_handle_t *parent, const char *path,
601     nvlist_t *bmark_props)
602 {
603 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
604 
605 	if (zhp == NULL)
606 		return (NULL);
607 
608 	/* Fill in the name. */
609 	zhp->zfs_hdl = parent->zfs_hdl;
610 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
611 
612 	/* Set the property lists. */
613 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
614 		free(zhp);
615 		return (NULL);
616 	}
617 
618 	/* Set the types. */
619 	zhp->zfs_head_type = parent->zfs_head_type;
620 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
621 
622 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
623 		nvlist_free(zhp->zfs_props);
624 		free(zhp);
625 		return (NULL);
626 	}
627 
628 	return (zhp);
629 }
630 
631 /*
632  * Opens the given snapshot, filesystem, or volume.   The 'types'
633  * argument is a mask of acceptable types.  The function will print an
634  * appropriate error message and return NULL if it can't be opened.
635  */
636 zfs_handle_t *
637 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
638 {
639 	zfs_handle_t *zhp;
640 	char errbuf[1024];
641 
642 	(void) snprintf(errbuf, sizeof (errbuf),
643 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
644 
645 	/*
646 	 * Validate the name before we even try to open it.
647 	 */
648 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
649 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
650 		    "invalid dataset name"));
651 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
652 		return (NULL);
653 	}
654 
655 	/*
656 	 * Try to get stats for the dataset, which will tell us if it exists.
657 	 */
658 	errno = 0;
659 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
660 		(void) zfs_standard_error(hdl, errno, errbuf);
661 		return (NULL);
662 	}
663 
664 	if (!(types & zhp->zfs_type)) {
665 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
666 		zfs_close(zhp);
667 		return (NULL);
668 	}
669 
670 	return (zhp);
671 }
672 
673 /*
674  * Release a ZFS handle.  Nothing to do but free the associated memory.
675  */
676 void
677 zfs_close(zfs_handle_t *zhp)
678 {
679 	if (zhp->zfs_mntopts)
680 		free(zhp->zfs_mntopts);
681 	nvlist_free(zhp->zfs_props);
682 	nvlist_free(zhp->zfs_user_props);
683 	nvlist_free(zhp->zfs_recvd_props);
684 	free(zhp);
685 }
686 
687 typedef struct mnttab_node {
688 	struct mnttab mtn_mt;
689 	avl_node_t mtn_node;
690 } mnttab_node_t;
691 
692 static int
693 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
694 {
695 	const mnttab_node_t *mtn1 = arg1;
696 	const mnttab_node_t *mtn2 = arg2;
697 	int rv;
698 
699 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
700 
701 	if (rv == 0)
702 		return (0);
703 	return (rv > 0 ? 1 : -1);
704 }
705 
706 void
707 libzfs_mnttab_init(libzfs_handle_t *hdl)
708 {
709 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
710 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
711 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
712 }
713 
714 void
715 libzfs_mnttab_update(libzfs_handle_t *hdl)
716 {
717 	struct mnttab entry;
718 
719 	rewind(hdl->libzfs_mnttab);
720 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
721 		mnttab_node_t *mtn;
722 
723 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
724 			continue;
725 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
726 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
727 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
728 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
729 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
730 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
731 	}
732 }
733 
734 void
735 libzfs_mnttab_fini(libzfs_handle_t *hdl)
736 {
737 	void *cookie = NULL;
738 	mnttab_node_t *mtn;
739 
740 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
741 		free(mtn->mtn_mt.mnt_special);
742 		free(mtn->mtn_mt.mnt_mountp);
743 		free(mtn->mtn_mt.mnt_fstype);
744 		free(mtn->mtn_mt.mnt_mntopts);
745 		free(mtn);
746 	}
747 	avl_destroy(&hdl->libzfs_mnttab_cache);
748 }
749 
750 void
751 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
752 {
753 	hdl->libzfs_mnttab_enable = enable;
754 }
755 
756 int
757 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
758     struct mnttab *entry)
759 {
760 	mnttab_node_t find;
761 	mnttab_node_t *mtn;
762 
763 	if (!hdl->libzfs_mnttab_enable) {
764 		struct mnttab srch = { 0 };
765 
766 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
767 			libzfs_mnttab_fini(hdl);
768 		rewind(hdl->libzfs_mnttab);
769 		srch.mnt_special = (char *)fsname;
770 		srch.mnt_fstype = MNTTYPE_ZFS;
771 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
772 			return (0);
773 		else
774 			return (ENOENT);
775 	}
776 
777 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
778 		libzfs_mnttab_update(hdl);
779 
780 	find.mtn_mt.mnt_special = (char *)fsname;
781 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
782 	if (mtn) {
783 		*entry = mtn->mtn_mt;
784 		return (0);
785 	}
786 	return (ENOENT);
787 }
788 
789 void
790 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
791     const char *mountp, const char *mntopts)
792 {
793 	mnttab_node_t *mtn;
794 
795 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
796 		return;
797 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
798 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
799 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
800 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
801 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
802 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
803 }
804 
805 void
806 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
807 {
808 	mnttab_node_t find;
809 	mnttab_node_t *ret;
810 
811 	find.mtn_mt.mnt_special = (char *)fsname;
812 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
813 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
814 		free(ret->mtn_mt.mnt_special);
815 		free(ret->mtn_mt.mnt_mountp);
816 		free(ret->mtn_mt.mnt_fstype);
817 		free(ret->mtn_mt.mnt_mntopts);
818 		free(ret);
819 	}
820 }
821 
822 int
823 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
824 {
825 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
826 
827 	if (zpool_handle == NULL)
828 		return (-1);
829 
830 	*spa_version = zpool_get_prop_int(zpool_handle,
831 	    ZPOOL_PROP_VERSION, NULL);
832 	return (0);
833 }
834 
835 /*
836  * The choice of reservation property depends on the SPA version.
837  */
838 static int
839 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
840 {
841 	int spa_version;
842 
843 	if (zfs_spa_version(zhp, &spa_version) < 0)
844 		return (-1);
845 
846 	if (spa_version >= SPA_VERSION_REFRESERVATION)
847 		*resv_prop = ZFS_PROP_REFRESERVATION;
848 	else
849 		*resv_prop = ZFS_PROP_RESERVATION;
850 
851 	return (0);
852 }
853 
854 /*
855  * Given an nvlist of properties to set, validates that they are correct, and
856  * parses any numeric properties (index, boolean, etc) if they are specified as
857  * strings.
858  */
859 nvlist_t *
860 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
861     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
862     const char *errbuf)
863 {
864 	nvpair_t *elem;
865 	uint64_t intval;
866 	char *strval;
867 	zfs_prop_t prop;
868 	nvlist_t *ret;
869 	int chosen_normal = -1;
870 	int chosen_utf = -1;
871 
872 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
873 		(void) no_memory(hdl);
874 		return (NULL);
875 	}
876 
877 	/*
878 	 * Make sure this property is valid and applies to this type.
879 	 */
880 
881 	elem = NULL;
882 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
883 		const char *propname = nvpair_name(elem);
884 
885 		prop = zfs_name_to_prop(propname);
886 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
887 			/*
888 			 * This is a user property: make sure it's a
889 			 * string, and that it's less than ZAP_MAXNAMELEN.
890 			 */
891 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
892 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
893 				    "'%s' must be a string"), propname);
894 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
895 				goto error;
896 			}
897 
898 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
899 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
900 				    "property name '%s' is too long"),
901 				    propname);
902 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
903 				goto error;
904 			}
905 
906 			(void) nvpair_value_string(elem, &strval);
907 			if (nvlist_add_string(ret, propname, strval) != 0) {
908 				(void) no_memory(hdl);
909 				goto error;
910 			}
911 			continue;
912 		}
913 
914 		/*
915 		 * Currently, only user properties can be modified on
916 		 * snapshots.
917 		 */
918 		if (type == ZFS_TYPE_SNAPSHOT) {
919 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
920 			    "this property can not be modified for snapshots"));
921 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
922 			goto error;
923 		}
924 
925 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
926 			zfs_userquota_prop_t uqtype;
927 			char newpropname[128];
928 			char domain[128];
929 			uint64_t rid;
930 			uint64_t valary[3];
931 
932 			if (userquota_propname_decode(propname, zoned,
933 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
934 				zfs_error_aux(hdl,
935 				    dgettext(TEXT_DOMAIN,
936 				    "'%s' has an invalid user/group name"),
937 				    propname);
938 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
939 				goto error;
940 			}
941 
942 			if (uqtype != ZFS_PROP_USERQUOTA &&
943 			    uqtype != ZFS_PROP_GROUPQUOTA) {
944 				zfs_error_aux(hdl,
945 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
946 				    propname);
947 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
948 				    errbuf);
949 				goto error;
950 			}
951 
952 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
953 				(void) nvpair_value_string(elem, &strval);
954 				if (strcmp(strval, "none") == 0) {
955 					intval = 0;
956 				} else if (zfs_nicestrtonum(hdl,
957 				    strval, &intval) != 0) {
958 					(void) zfs_error(hdl,
959 					    EZFS_BADPROP, errbuf);
960 					goto error;
961 				}
962 			} else if (nvpair_type(elem) ==
963 			    DATA_TYPE_UINT64) {
964 				(void) nvpair_value_uint64(elem, &intval);
965 				if (intval == 0) {
966 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
967 					    "use 'none' to disable "
968 					    "userquota/groupquota"));
969 					goto error;
970 				}
971 			} else {
972 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
973 				    "'%s' must be a number"), propname);
974 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
975 				goto error;
976 			}
977 
978 			/*
979 			 * Encode the prop name as
980 			 * userquota@<hex-rid>-domain, to make it easy
981 			 * for the kernel to decode.
982 			 */
983 			(void) snprintf(newpropname, sizeof (newpropname),
984 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
985 			    (longlong_t)rid, domain);
986 			valary[0] = uqtype;
987 			valary[1] = rid;
988 			valary[2] = intval;
989 			if (nvlist_add_uint64_array(ret, newpropname,
990 			    valary, 3) != 0) {
991 				(void) no_memory(hdl);
992 				goto error;
993 			}
994 			continue;
995 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
996 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997 			    "'%s' is readonly"),
998 			    propname);
999 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1000 			goto error;
1001 		}
1002 
1003 		if (prop == ZPROP_INVAL) {
1004 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1005 			    "invalid property '%s'"), propname);
1006 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1007 			goto error;
1008 		}
1009 
1010 		if (!zfs_prop_valid_for_type(prop, type)) {
1011 			zfs_error_aux(hdl,
1012 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1013 			    "apply to datasets of this type"), propname);
1014 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1015 			goto error;
1016 		}
1017 
1018 		if (zfs_prop_readonly(prop) &&
1019 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
1020 			zfs_error_aux(hdl,
1021 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1022 			    propname);
1023 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1024 			goto error;
1025 		}
1026 
1027 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1028 		    &strval, &intval, errbuf) != 0)
1029 			goto error;
1030 
1031 		/*
1032 		 * Perform some additional checks for specific properties.
1033 		 */
1034 		switch (prop) {
1035 		case ZFS_PROP_VERSION:
1036 		{
1037 			int version;
1038 
1039 			if (zhp == NULL)
1040 				break;
1041 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1042 			if (intval < version) {
1043 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1044 				    "Can not downgrade; already at version %u"),
1045 				    version);
1046 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1047 				goto error;
1048 			}
1049 			break;
1050 		}
1051 
1052 		case ZFS_PROP_VOLBLOCKSIZE:
1053 		case ZFS_PROP_RECORDSIZE:
1054 		{
1055 			int maxbs = SPA_MAXBLOCKSIZE;
1056 			if (zpool_hdl != NULL) {
1057 				maxbs = zpool_get_prop_int(zpool_hdl,
1058 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1059 			}
1060 			/*
1061 			 * Volumes are limited to a volblocksize of 128KB,
1062 			 * because they typically service workloads with
1063 			 * small random writes, which incur a large performance
1064 			 * penalty with large blocks.
1065 			 */
1066 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1067 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1068 			/*
1069 			 * The value must be a power of two between
1070 			 * SPA_MINBLOCKSIZE and maxbs.
1071 			 */
1072 			if (intval < SPA_MINBLOCKSIZE ||
1073 			    intval > maxbs || !ISP2(intval)) {
1074 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1075 				    "'%s' must be power of 2 from 512B "
1076 				    "to %uKB"), propname, maxbs >> 10);
1077 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1078 				goto error;
1079 			}
1080 			break;
1081 		}
1082 		case ZFS_PROP_MLSLABEL:
1083 		{
1084 			/*
1085 			 * Verify the mlslabel string and convert to
1086 			 * internal hex label string.
1087 			 */
1088 
1089 			m_label_t *new_sl;
1090 			char *hex = NULL;	/* internal label string */
1091 
1092 			/* Default value is already OK. */
1093 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1094 				break;
1095 
1096 			/* Verify the label can be converted to binary form */
1097 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1098 			    (str_to_label(strval, &new_sl, MAC_LABEL,
1099 			    L_NO_CORRECTION, NULL) == -1)) {
1100 				goto badlabel;
1101 			}
1102 
1103 			/* Now translate to hex internal label string */
1104 			if (label_to_str(new_sl, &hex, M_INTERNAL,
1105 			    DEF_NAMES) != 0) {
1106 				if (hex)
1107 					free(hex);
1108 				goto badlabel;
1109 			}
1110 			m_label_free(new_sl);
1111 
1112 			/* If string is already in internal form, we're done. */
1113 			if (strcmp(strval, hex) == 0) {
1114 				free(hex);
1115 				break;
1116 			}
1117 
1118 			/* Replace the label string with the internal form. */
1119 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
1120 			    DATA_TYPE_STRING);
1121 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1122 			    hex) == 0);
1123 			free(hex);
1124 
1125 			break;
1126 
1127 badlabel:
1128 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129 			    "invalid mlslabel '%s'"), strval);
1130 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1131 			m_label_free(new_sl);	/* OK if null */
1132 			goto error;
1133 
1134 		}
1135 
1136 		case ZFS_PROP_MOUNTPOINT:
1137 		{
1138 			namecheck_err_t why;
1139 
1140 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1141 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1142 				break;
1143 
1144 			if (mountpoint_namecheck(strval, &why)) {
1145 				switch (why) {
1146 				case NAME_ERR_LEADING_SLASH:
1147 					zfs_error_aux(hdl,
1148 					    dgettext(TEXT_DOMAIN,
1149 					    "'%s' must be an absolute path, "
1150 					    "'none', or 'legacy'"), propname);
1151 					break;
1152 				case NAME_ERR_TOOLONG:
1153 					zfs_error_aux(hdl,
1154 					    dgettext(TEXT_DOMAIN,
1155 					    "component of '%s' is too long"),
1156 					    propname);
1157 					break;
1158 				}
1159 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1160 				goto error;
1161 			}
1162 		}
1163 
1164 			/*FALLTHRU*/
1165 
1166 		case ZFS_PROP_SHARESMB:
1167 		case ZFS_PROP_SHARENFS:
1168 			/*
1169 			 * For the mountpoint and sharenfs or sharesmb
1170 			 * properties, check if it can be set in a
1171 			 * global/non-global zone based on
1172 			 * the zoned property value:
1173 			 *
1174 			 *		global zone	    non-global zone
1175 			 * --------------------------------------------------
1176 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1177 			 *		sharenfs (no)	    sharenfs (no)
1178 			 *		sharesmb (no)	    sharesmb (no)
1179 			 *
1180 			 * zoned=off	mountpoint (yes)	N/A
1181 			 *		sharenfs (yes)
1182 			 *		sharesmb (yes)
1183 			 */
1184 			if (zoned) {
1185 				if (getzoneid() == GLOBAL_ZONEID) {
1186 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1187 					    "'%s' cannot be set on "
1188 					    "dataset in a non-global zone"),
1189 					    propname);
1190 					(void) zfs_error(hdl, EZFS_ZONED,
1191 					    errbuf);
1192 					goto error;
1193 				} else if (prop == ZFS_PROP_SHARENFS ||
1194 				    prop == ZFS_PROP_SHARESMB) {
1195 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1196 					    "'%s' cannot be set in "
1197 					    "a non-global zone"), propname);
1198 					(void) zfs_error(hdl, EZFS_ZONED,
1199 					    errbuf);
1200 					goto error;
1201 				}
1202 			} else if (getzoneid() != GLOBAL_ZONEID) {
1203 				/*
1204 				 * If zoned property is 'off', this must be in
1205 				 * a global zone. If not, something is wrong.
1206 				 */
1207 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1208 				    "'%s' cannot be set while dataset "
1209 				    "'zoned' property is set"), propname);
1210 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1211 				goto error;
1212 			}
1213 
1214 			/*
1215 			 * At this point, it is legitimate to set the
1216 			 * property. Now we want to make sure that the
1217 			 * property value is valid if it is sharenfs.
1218 			 */
1219 			if ((prop == ZFS_PROP_SHARENFS ||
1220 			    prop == ZFS_PROP_SHARESMB) &&
1221 			    strcmp(strval, "on") != 0 &&
1222 			    strcmp(strval, "off") != 0) {
1223 				zfs_share_proto_t proto;
1224 
1225 				if (prop == ZFS_PROP_SHARESMB)
1226 					proto = PROTO_SMB;
1227 				else
1228 					proto = PROTO_NFS;
1229 
1230 				/*
1231 				 * Must be an valid sharing protocol
1232 				 * option string so init the libshare
1233 				 * in order to enable the parser and
1234 				 * then parse the options. We use the
1235 				 * control API since we don't care about
1236 				 * the current configuration and don't
1237 				 * want the overhead of loading it
1238 				 * until we actually do something.
1239 				 */
1240 
1241 				if (zfs_init_libshare(hdl,
1242 				    SA_INIT_CONTROL_API) != SA_OK) {
1243 					/*
1244 					 * An error occurred so we can't do
1245 					 * anything
1246 					 */
1247 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1248 					    "'%s' cannot be set: problem "
1249 					    "in share initialization"),
1250 					    propname);
1251 					(void) zfs_error(hdl, EZFS_BADPROP,
1252 					    errbuf);
1253 					goto error;
1254 				}
1255 
1256 				if (zfs_parse_options(strval, proto) != SA_OK) {
1257 					/*
1258 					 * There was an error in parsing so
1259 					 * deal with it by issuing an error
1260 					 * message and leaving after
1261 					 * uninitializing the the libshare
1262 					 * interface.
1263 					 */
1264 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1265 					    "'%s' cannot be set to invalid "
1266 					    "options"), propname);
1267 					(void) zfs_error(hdl, EZFS_BADPROP,
1268 					    errbuf);
1269 					zfs_uninit_libshare(hdl);
1270 					goto error;
1271 				}
1272 				zfs_uninit_libshare(hdl);
1273 			}
1274 
1275 			break;
1276 		case ZFS_PROP_UTF8ONLY:
1277 			chosen_utf = (int)intval;
1278 			break;
1279 		case ZFS_PROP_NORMALIZE:
1280 			chosen_normal = (int)intval;
1281 			break;
1282 		}
1283 
1284 		/*
1285 		 * For changes to existing volumes, we have some additional
1286 		 * checks to enforce.
1287 		 */
1288 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1289 			uint64_t volsize = zfs_prop_get_int(zhp,
1290 			    ZFS_PROP_VOLSIZE);
1291 			uint64_t blocksize = zfs_prop_get_int(zhp,
1292 			    ZFS_PROP_VOLBLOCKSIZE);
1293 			char buf[64];
1294 
1295 			switch (prop) {
1296 			case ZFS_PROP_RESERVATION:
1297 			case ZFS_PROP_REFRESERVATION:
1298 				if (intval > volsize) {
1299 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1300 					    "'%s' is greater than current "
1301 					    "volume size"), propname);
1302 					(void) zfs_error(hdl, EZFS_BADPROP,
1303 					    errbuf);
1304 					goto error;
1305 				}
1306 				break;
1307 
1308 			case ZFS_PROP_VOLSIZE:
1309 				if (intval % blocksize != 0) {
1310 					zfs_nicenum(blocksize, buf,
1311 					    sizeof (buf));
1312 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1313 					    "'%s' must be a multiple of "
1314 					    "volume block size (%s)"),
1315 					    propname, buf);
1316 					(void) zfs_error(hdl, EZFS_BADPROP,
1317 					    errbuf);
1318 					goto error;
1319 				}
1320 
1321 				if (intval == 0) {
1322 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1323 					    "'%s' cannot be zero"),
1324 					    propname);
1325 					(void) zfs_error(hdl, EZFS_BADPROP,
1326 					    errbuf);
1327 					goto error;
1328 				}
1329 				break;
1330 			}
1331 		}
1332 	}
1333 
1334 	/*
1335 	 * If normalization was chosen, but no UTF8 choice was made,
1336 	 * enforce rejection of non-UTF8 names.
1337 	 *
1338 	 * If normalization was chosen, but rejecting non-UTF8 names
1339 	 * was explicitly not chosen, it is an error.
1340 	 */
1341 	if (chosen_normal > 0 && chosen_utf < 0) {
1342 		if (nvlist_add_uint64(ret,
1343 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1344 			(void) no_memory(hdl);
1345 			goto error;
1346 		}
1347 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1348 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1349 		    "'%s' must be set 'on' if normalization chosen"),
1350 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1351 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1352 		goto error;
1353 	}
1354 	return (ret);
1355 
1356 error:
1357 	nvlist_free(ret);
1358 	return (NULL);
1359 }
1360 
1361 int
1362 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1363 {
1364 	uint64_t old_volsize;
1365 	uint64_t new_volsize;
1366 	uint64_t old_reservation;
1367 	uint64_t new_reservation;
1368 	zfs_prop_t resv_prop;
1369 	nvlist_t *props;
1370 
1371 	/*
1372 	 * If this is an existing volume, and someone is setting the volsize,
1373 	 * make sure that it matches the reservation, or add it if necessary.
1374 	 */
1375 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1376 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1377 		return (-1);
1378 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1379 
1380 	props = fnvlist_alloc();
1381 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1382 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1383 
1384 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
1385 	    old_reservation) || nvlist_exists(nvl,
1386 	    zfs_prop_to_name(resv_prop))) {
1387 		fnvlist_free(props);
1388 		return (0);
1389 	}
1390 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1391 	    &new_volsize) != 0) {
1392 		fnvlist_free(props);
1393 		return (-1);
1394 	}
1395 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1396 	fnvlist_free(props);
1397 
1398 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1399 	    new_reservation) != 0) {
1400 		(void) no_memory(zhp->zfs_hdl);
1401 		return (-1);
1402 	}
1403 	return (1);
1404 }
1405 
1406 void
1407 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1408     char *errbuf)
1409 {
1410 	switch (err) {
1411 
1412 	case ENOSPC:
1413 		/*
1414 		 * For quotas and reservations, ENOSPC indicates
1415 		 * something different; setting a quota or reservation
1416 		 * doesn't use any disk space.
1417 		 */
1418 		switch (prop) {
1419 		case ZFS_PROP_QUOTA:
1420 		case ZFS_PROP_REFQUOTA:
1421 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1422 			    "size is less than current used or "
1423 			    "reserved space"));
1424 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1425 			break;
1426 
1427 		case ZFS_PROP_RESERVATION:
1428 		case ZFS_PROP_REFRESERVATION:
1429 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1430 			    "size is greater than available space"));
1431 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1432 			break;
1433 
1434 		default:
1435 			(void) zfs_standard_error(hdl, err, errbuf);
1436 			break;
1437 		}
1438 		break;
1439 
1440 	case EBUSY:
1441 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
1442 		break;
1443 
1444 	case EROFS:
1445 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1446 		break;
1447 
1448 	case E2BIG:
1449 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1450 		    "property value too long"));
1451 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1452 		break;
1453 
1454 	case ENOTSUP:
1455 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1456 		    "pool and or dataset must be upgraded to set this "
1457 		    "property or value"));
1458 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1459 		break;
1460 
1461 	case ERANGE:
1462 		if (prop == ZFS_PROP_COMPRESSION ||
1463 		    prop == ZFS_PROP_RECORDSIZE) {
1464 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465 			    "property setting is not allowed on "
1466 			    "bootable datasets"));
1467 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1468 		} else if (prop == ZFS_PROP_CHECKSUM ||
1469 		    prop == ZFS_PROP_DEDUP) {
1470 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1471 			    "property setting is not allowed on "
1472 			    "root pools"));
1473 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1474 		} else {
1475 			(void) zfs_standard_error(hdl, err, errbuf);
1476 		}
1477 		break;
1478 
1479 	case EINVAL:
1480 		if (prop == ZPROP_INVAL) {
1481 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1482 		} else {
1483 			(void) zfs_standard_error(hdl, err, errbuf);
1484 		}
1485 		break;
1486 
1487 	case EOVERFLOW:
1488 		/*
1489 		 * This platform can't address a volume this big.
1490 		 */
1491 #ifdef _ILP32
1492 		if (prop == ZFS_PROP_VOLSIZE) {
1493 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1494 			break;
1495 		}
1496 #endif
1497 		/* FALLTHROUGH */
1498 	default:
1499 		(void) zfs_standard_error(hdl, err, errbuf);
1500 	}
1501 }
1502 
1503 /*
1504  * Given a property name and value, set the property for the given dataset.
1505  */
1506 int
1507 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1508 {
1509 	int ret = -1;
1510 	char errbuf[1024];
1511 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1512 	nvlist_t *nvl = NULL;
1513 
1514 	(void) snprintf(errbuf, sizeof (errbuf),
1515 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1516 	    zhp->zfs_name);
1517 
1518 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1519 	    nvlist_add_string(nvl, propname, propval) != 0) {
1520 		(void) no_memory(hdl);
1521 		goto error;
1522 	}
1523 
1524 	ret = zfs_prop_set_list(zhp, nvl);
1525 
1526 error:
1527 	nvlist_free(nvl);
1528 	return (ret);
1529 }
1530 
1531 
1532 
1533 /*
1534  * Given an nvlist of property names and values, set the properties for the
1535  * given dataset.
1536  */
1537 int
1538 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1539 {
1540 	zfs_cmd_t zc = { 0 };
1541 	int ret = -1;
1542 	prop_changelist_t **cls = NULL;
1543 	int cl_idx;
1544 	char errbuf[1024];
1545 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1546 	nvlist_t *nvl;
1547 	int nvl_len;
1548 	int added_resv;
1549 
1550 	(void) snprintf(errbuf, sizeof (errbuf),
1551 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1552 	    zhp->zfs_name);
1553 
1554 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1555 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1556 	    errbuf)) == NULL)
1557 		goto error;
1558 
1559 	/*
1560 	 * We have to check for any extra properties which need to be added
1561 	 * before computing the length of the nvlist.
1562 	 */
1563 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1564 	    elem != NULL;
1565 	    elem = nvlist_next_nvpair(nvl, elem)) {
1566 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1567 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1568 			goto error;
1569 		}
1570 	}
1571 	/*
1572 	 * Check how many properties we're setting and allocate an array to
1573 	 * store changelist pointers for postfix().
1574 	 */
1575 	nvl_len = 0;
1576 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1577 	    elem != NULL;
1578 	    elem = nvlist_next_nvpair(nvl, elem))
1579 		nvl_len++;
1580 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1581 		goto error;
1582 
1583 	cl_idx = 0;
1584 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1585 	    elem != NULL;
1586 	    elem = nvlist_next_nvpair(nvl, elem)) {
1587 
1588 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1589 
1590 		assert(cl_idx < nvl_len);
1591 		/*
1592 		 * We don't want to unmount & remount the dataset when changing
1593 		 * its canmount property to 'on' or 'noauto'.  We only use
1594 		 * the changelist logic to unmount when setting canmount=off.
1595 		 */
1596 		if (!(prop == ZFS_PROP_CANMOUNT &&
1597 		    fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) {
1598 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1599 			if (cls[cl_idx] == NULL)
1600 				goto error;
1601 		}
1602 
1603 		if (prop == ZFS_PROP_MOUNTPOINT &&
1604 		    changelist_haszonedchild(cls[cl_idx])) {
1605 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1606 			    "child dataset with inherited mountpoint is used "
1607 			    "in a non-global zone"));
1608 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1609 			goto error;
1610 		}
1611 
1612 		if (cls[cl_idx] != NULL &&
1613 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1614 			goto error;
1615 
1616 		cl_idx++;
1617 	}
1618 	assert(cl_idx == nvl_len);
1619 
1620 	/*
1621 	 * Execute the corresponding ioctl() to set this list of properties.
1622 	 */
1623 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1624 
1625 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1626 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1627 		goto error;
1628 
1629 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1630 
1631 	if (ret != 0) {
1632 		/* Get the list of unset properties back and report them. */
1633 		nvlist_t *errorprops = NULL;
1634 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1635 			goto error;
1636 		for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1637 		    elem != NULL;
1638 		    elem = nvlist_next_nvpair(nvl, elem)) {
1639 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1640 			zfs_setprop_error(hdl, prop, errno, errbuf);
1641 		}
1642 		nvlist_free(errorprops);
1643 
1644 		if (added_resv && errno == ENOSPC) {
1645 			/* clean up the volsize property we tried to set */
1646 			uint64_t old_volsize = zfs_prop_get_int(zhp,
1647 			    ZFS_PROP_VOLSIZE);
1648 			nvlist_free(nvl);
1649 			nvl = NULL;
1650 			zcmd_free_nvlists(&zc);
1651 
1652 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1653 				goto error;
1654 			if (nvlist_add_uint64(nvl,
1655 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1656 			    old_volsize) != 0)
1657 				goto error;
1658 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1659 				goto error;
1660 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1661 		}
1662 	} else {
1663 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1664 			if (cls[cl_idx] != NULL) {
1665 				int clp_err = changelist_postfix(cls[cl_idx]);
1666 				if (clp_err != 0)
1667 					ret = clp_err;
1668 			}
1669 		}
1670 
1671 		/*
1672 		 * Refresh the statistics so the new property value
1673 		 * is reflected.
1674 		 */
1675 		if (ret == 0)
1676 			(void) get_stats(zhp);
1677 	}
1678 
1679 error:
1680 	nvlist_free(nvl);
1681 	zcmd_free_nvlists(&zc);
1682 	if (cls != NULL) {
1683 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1684 			if (cls[cl_idx] != NULL)
1685 				changelist_free(cls[cl_idx]);
1686 		}
1687 		free(cls);
1688 	}
1689 	return (ret);
1690 }
1691 
1692 /*
1693  * Given a property, inherit the value from the parent dataset, or if received
1694  * is TRUE, revert to the received value, if any.
1695  */
1696 int
1697 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1698 {
1699 	zfs_cmd_t zc = { 0 };
1700 	int ret;
1701 	prop_changelist_t *cl;
1702 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1703 	char errbuf[1024];
1704 	zfs_prop_t prop;
1705 
1706 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1707 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1708 
1709 	zc.zc_cookie = received;
1710 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1711 		/*
1712 		 * For user properties, the amount of work we have to do is very
1713 		 * small, so just do it here.
1714 		 */
1715 		if (!zfs_prop_user(propname)) {
1716 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1717 			    "invalid property"));
1718 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1719 		}
1720 
1721 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1722 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1723 
1724 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1725 			return (zfs_standard_error(hdl, errno, errbuf));
1726 
1727 		return (0);
1728 	}
1729 
1730 	/*
1731 	 * Verify that this property is inheritable.
1732 	 */
1733 	if (zfs_prop_readonly(prop))
1734 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1735 
1736 	if (!zfs_prop_inheritable(prop) && !received)
1737 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1738 
1739 	/*
1740 	 * Check to see if the value applies to this type
1741 	 */
1742 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1743 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1744 
1745 	/*
1746 	 * Normalize the name, to get rid of shorthand abbreviations.
1747 	 */
1748 	propname = zfs_prop_to_name(prop);
1749 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1750 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1751 
1752 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1753 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1754 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1755 		    "dataset is used in a non-global zone"));
1756 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1757 	}
1758 
1759 	/*
1760 	 * Determine datasets which will be affected by this change, if any.
1761 	 */
1762 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1763 		return (-1);
1764 
1765 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1766 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1767 		    "child dataset with inherited mountpoint is used "
1768 		    "in a non-global zone"));
1769 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1770 		goto error;
1771 	}
1772 
1773 	if ((ret = changelist_prefix(cl)) != 0)
1774 		goto error;
1775 
1776 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1777 		return (zfs_standard_error(hdl, errno, errbuf));
1778 	} else {
1779 
1780 		if ((ret = changelist_postfix(cl)) != 0)
1781 			goto error;
1782 
1783 		/*
1784 		 * Refresh the statistics so the new property is reflected.
1785 		 */
1786 		(void) get_stats(zhp);
1787 	}
1788 
1789 error:
1790 	changelist_free(cl);
1791 	return (ret);
1792 }
1793 
1794 /*
1795  * True DSL properties are stored in an nvlist.  The following two functions
1796  * extract them appropriately.
1797  */
1798 static uint64_t
1799 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1800 {
1801 	nvlist_t *nv;
1802 	uint64_t value;
1803 
1804 	*source = NULL;
1805 	if (nvlist_lookup_nvlist(zhp->zfs_props,
1806 	    zfs_prop_to_name(prop), &nv) == 0) {
1807 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1808 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1809 	} else {
1810 		verify(!zhp->zfs_props_table ||
1811 		    zhp->zfs_props_table[prop] == B_TRUE);
1812 		value = zfs_prop_default_numeric(prop);
1813 		*source = "";
1814 	}
1815 
1816 	return (value);
1817 }
1818 
1819 static const char *
1820 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1821 {
1822 	nvlist_t *nv;
1823 	const char *value;
1824 
1825 	*source = NULL;
1826 	if (nvlist_lookup_nvlist(zhp->zfs_props,
1827 	    zfs_prop_to_name(prop), &nv) == 0) {
1828 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1829 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1830 	} else {
1831 		verify(!zhp->zfs_props_table ||
1832 		    zhp->zfs_props_table[prop] == B_TRUE);
1833 		value = zfs_prop_default_string(prop);
1834 		*source = "";
1835 	}
1836 
1837 	return (value);
1838 }
1839 
1840 static boolean_t
1841 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1842 {
1843 	return (zhp->zfs_props == zhp->zfs_recvd_props);
1844 }
1845 
1846 static void
1847 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1848 {
1849 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1850 	zhp->zfs_props = zhp->zfs_recvd_props;
1851 }
1852 
1853 static void
1854 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1855 {
1856 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1857 	*cookie = 0;
1858 }
1859 
1860 /*
1861  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1862  * zfs_prop_get_int() are built using this interface.
1863  *
1864  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1865  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1866  * If they differ from the on-disk values, report the current values and mark
1867  * the source "temporary".
1868  */
1869 static int
1870 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1871     char **source, uint64_t *val)
1872 {
1873 	zfs_cmd_t zc = { 0 };
1874 	nvlist_t *zplprops = NULL;
1875 	struct mnttab mnt;
1876 	char *mntopt_on = NULL;
1877 	char *mntopt_off = NULL;
1878 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1879 
1880 	*source = NULL;
1881 
1882 	switch (prop) {
1883 	case ZFS_PROP_ATIME:
1884 		mntopt_on = MNTOPT_ATIME;
1885 		mntopt_off = MNTOPT_NOATIME;
1886 		break;
1887 
1888 	case ZFS_PROP_DEVICES:
1889 		mntopt_on = MNTOPT_DEVICES;
1890 		mntopt_off = MNTOPT_NODEVICES;
1891 		break;
1892 
1893 	case ZFS_PROP_EXEC:
1894 		mntopt_on = MNTOPT_EXEC;
1895 		mntopt_off = MNTOPT_NOEXEC;
1896 		break;
1897 
1898 	case ZFS_PROP_READONLY:
1899 		mntopt_on = MNTOPT_RO;
1900 		mntopt_off = MNTOPT_RW;
1901 		break;
1902 
1903 	case ZFS_PROP_SETUID:
1904 		mntopt_on = MNTOPT_SETUID;
1905 		mntopt_off = MNTOPT_NOSETUID;
1906 		break;
1907 
1908 	case ZFS_PROP_XATTR:
1909 		mntopt_on = MNTOPT_XATTR;
1910 		mntopt_off = MNTOPT_NOXATTR;
1911 		break;
1912 
1913 	case ZFS_PROP_NBMAND:
1914 		mntopt_on = MNTOPT_NBMAND;
1915 		mntopt_off = MNTOPT_NONBMAND;
1916 		break;
1917 	}
1918 
1919 	/*
1920 	 * Because looking up the mount options is potentially expensive
1921 	 * (iterating over all of /etc/mnttab), we defer its calculation until
1922 	 * we're looking up a property which requires its presence.
1923 	 */
1924 	if (!zhp->zfs_mntcheck &&
1925 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1926 		libzfs_handle_t *hdl = zhp->zfs_hdl;
1927 		struct mnttab entry;
1928 
1929 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1930 			zhp->zfs_mntopts = zfs_strdup(hdl,
1931 			    entry.mnt_mntopts);
1932 			if (zhp->zfs_mntopts == NULL)
1933 				return (-1);
1934 		}
1935 
1936 		zhp->zfs_mntcheck = B_TRUE;
1937 	}
1938 
1939 	if (zhp->zfs_mntopts == NULL)
1940 		mnt.mnt_mntopts = "";
1941 	else
1942 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1943 
1944 	switch (prop) {
1945 	case ZFS_PROP_ATIME:
1946 	case ZFS_PROP_DEVICES:
1947 	case ZFS_PROP_EXEC:
1948 	case ZFS_PROP_READONLY:
1949 	case ZFS_PROP_SETUID:
1950 	case ZFS_PROP_XATTR:
1951 	case ZFS_PROP_NBMAND:
1952 		*val = getprop_uint64(zhp, prop, source);
1953 
1954 		if (received)
1955 			break;
1956 
1957 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
1958 			*val = B_TRUE;
1959 			if (src)
1960 				*src = ZPROP_SRC_TEMPORARY;
1961 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
1962 			*val = B_FALSE;
1963 			if (src)
1964 				*src = ZPROP_SRC_TEMPORARY;
1965 		}
1966 		break;
1967 
1968 	case ZFS_PROP_CANMOUNT:
1969 	case ZFS_PROP_VOLSIZE:
1970 	case ZFS_PROP_QUOTA:
1971 	case ZFS_PROP_REFQUOTA:
1972 	case ZFS_PROP_RESERVATION:
1973 	case ZFS_PROP_REFRESERVATION:
1974 	case ZFS_PROP_FILESYSTEM_LIMIT:
1975 	case ZFS_PROP_SNAPSHOT_LIMIT:
1976 	case ZFS_PROP_FILESYSTEM_COUNT:
1977 	case ZFS_PROP_SNAPSHOT_COUNT:
1978 		*val = getprop_uint64(zhp, prop, source);
1979 
1980 		if (*source == NULL) {
1981 			/* not default, must be local */
1982 			*source = zhp->zfs_name;
1983 		}
1984 		break;
1985 
1986 	case ZFS_PROP_MOUNTED:
1987 		*val = (zhp->zfs_mntopts != NULL);
1988 		break;
1989 
1990 	case ZFS_PROP_NUMCLONES:
1991 		*val = zhp->zfs_dmustats.dds_num_clones;
1992 		break;
1993 
1994 	case ZFS_PROP_VERSION:
1995 	case ZFS_PROP_NORMALIZE:
1996 	case ZFS_PROP_UTF8ONLY:
1997 	case ZFS_PROP_CASE:
1998 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1999 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2000 			return (-1);
2001 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2002 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2003 			zcmd_free_nvlists(&zc);
2004 			return (-1);
2005 		}
2006 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2007 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2008 		    val) != 0) {
2009 			zcmd_free_nvlists(&zc);
2010 			return (-1);
2011 		}
2012 		if (zplprops)
2013 			nvlist_free(zplprops);
2014 		zcmd_free_nvlists(&zc);
2015 		break;
2016 
2017 	case ZFS_PROP_INCONSISTENT:
2018 		*val = zhp->zfs_dmustats.dds_inconsistent;
2019 		break;
2020 
2021 	default:
2022 		switch (zfs_prop_get_type(prop)) {
2023 		case PROP_TYPE_NUMBER:
2024 		case PROP_TYPE_INDEX:
2025 			*val = getprop_uint64(zhp, prop, source);
2026 			/*
2027 			 * If we tried to use a default value for a
2028 			 * readonly property, it means that it was not
2029 			 * present.
2030 			 */
2031 			if (zfs_prop_readonly(prop) &&
2032 			    *source != NULL && (*source)[0] == '\0') {
2033 				*source = NULL;
2034 			}
2035 			break;
2036 
2037 		case PROP_TYPE_STRING:
2038 		default:
2039 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2040 			    "cannot get non-numeric property"));
2041 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2042 			    dgettext(TEXT_DOMAIN, "internal error")));
2043 		}
2044 	}
2045 
2046 	return (0);
2047 }
2048 
2049 /*
2050  * Calculate the source type, given the raw source string.
2051  */
2052 static void
2053 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2054     char *statbuf, size_t statlen)
2055 {
2056 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2057 		return;
2058 
2059 	if (source == NULL) {
2060 		*srctype = ZPROP_SRC_NONE;
2061 	} else if (source[0] == '\0') {
2062 		*srctype = ZPROP_SRC_DEFAULT;
2063 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2064 		*srctype = ZPROP_SRC_RECEIVED;
2065 	} else {
2066 		if (strcmp(source, zhp->zfs_name) == 0) {
2067 			*srctype = ZPROP_SRC_LOCAL;
2068 		} else {
2069 			(void) strlcpy(statbuf, source, statlen);
2070 			*srctype = ZPROP_SRC_INHERITED;
2071 		}
2072 	}
2073 
2074 }
2075 
2076 int
2077 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2078     size_t proplen, boolean_t literal)
2079 {
2080 	zfs_prop_t prop;
2081 	int err = 0;
2082 
2083 	if (zhp->zfs_recvd_props == NULL)
2084 		if (get_recvd_props_ioctl(zhp) != 0)
2085 			return (-1);
2086 
2087 	prop = zfs_name_to_prop(propname);
2088 
2089 	if (prop != ZPROP_INVAL) {
2090 		uint64_t cookie;
2091 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2092 			return (-1);
2093 		zfs_set_recvd_props_mode(zhp, &cookie);
2094 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
2095 		    NULL, NULL, 0, literal);
2096 		zfs_unset_recvd_props_mode(zhp, &cookie);
2097 	} else {
2098 		nvlist_t *propval;
2099 		char *recvdval;
2100 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2101 		    propname, &propval) != 0)
2102 			return (-1);
2103 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2104 		    &recvdval) == 0);
2105 		(void) strlcpy(propbuf, recvdval, proplen);
2106 	}
2107 
2108 	return (err == 0 ? 0 : -1);
2109 }
2110 
2111 static int
2112 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2113 {
2114 	nvlist_t *value;
2115 	nvpair_t *pair;
2116 
2117 	value = zfs_get_clones_nvl(zhp);
2118 	if (value == NULL)
2119 		return (-1);
2120 
2121 	propbuf[0] = '\0';
2122 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2123 	    pair = nvlist_next_nvpair(value, pair)) {
2124 		if (propbuf[0] != '\0')
2125 			(void) strlcat(propbuf, ",", proplen);
2126 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
2127 	}
2128 
2129 	return (0);
2130 }
2131 
2132 struct get_clones_arg {
2133 	uint64_t numclones;
2134 	nvlist_t *value;
2135 	const char *origin;
2136 	char buf[ZFS_MAXNAMELEN];
2137 };
2138 
2139 int
2140 get_clones_cb(zfs_handle_t *zhp, void *arg)
2141 {
2142 	struct get_clones_arg *gca = arg;
2143 
2144 	if (gca->numclones == 0) {
2145 		zfs_close(zhp);
2146 		return (0);
2147 	}
2148 
2149 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2150 	    NULL, NULL, 0, B_TRUE) != 0)
2151 		goto out;
2152 	if (strcmp(gca->buf, gca->origin) == 0) {
2153 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2154 		gca->numclones--;
2155 	}
2156 
2157 out:
2158 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
2159 	zfs_close(zhp);
2160 	return (0);
2161 }
2162 
2163 nvlist_t *
2164 zfs_get_clones_nvl(zfs_handle_t *zhp)
2165 {
2166 	nvlist_t *nv, *value;
2167 
2168 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2169 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2170 		struct get_clones_arg gca;
2171 
2172 		/*
2173 		 * if this is a snapshot, then the kernel wasn't able
2174 		 * to get the clones.  Do it by slowly iterating.
2175 		 */
2176 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2177 			return (NULL);
2178 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2179 			return (NULL);
2180 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2181 			nvlist_free(nv);
2182 			return (NULL);
2183 		}
2184 
2185 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2186 		gca.value = value;
2187 		gca.origin = zhp->zfs_name;
2188 
2189 		if (gca.numclones != 0) {
2190 			zfs_handle_t *root;
2191 			char pool[ZFS_MAXNAMELEN];
2192 			char *cp = pool;
2193 
2194 			/* get the pool name */
2195 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2196 			(void) strsep(&cp, "/@");
2197 			root = zfs_open(zhp->zfs_hdl, pool,
2198 			    ZFS_TYPE_FILESYSTEM);
2199 
2200 			(void) get_clones_cb(root, &gca);
2201 		}
2202 
2203 		if (gca.numclones != 0 ||
2204 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2205 		    nvlist_add_nvlist(zhp->zfs_props,
2206 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2207 			nvlist_free(nv);
2208 			nvlist_free(value);
2209 			return (NULL);
2210 		}
2211 		nvlist_free(nv);
2212 		nvlist_free(value);
2213 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2214 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2215 	}
2216 
2217 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2218 
2219 	return (value);
2220 }
2221 
2222 /*
2223  * Retrieve a property from the given object.  If 'literal' is specified, then
2224  * numbers are left as exact values.  Otherwise, numbers are converted to a
2225  * human-readable form.
2226  *
2227  * Returns 0 on success, or -1 on error.
2228  */
2229 int
2230 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2231     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2232 {
2233 	char *source = NULL;
2234 	uint64_t val;
2235 	const char *str;
2236 	const char *strval;
2237 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2238 
2239 	/*
2240 	 * Check to see if this property applies to our object
2241 	 */
2242 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2243 		return (-1);
2244 
2245 	if (received && zfs_prop_readonly(prop))
2246 		return (-1);
2247 
2248 	if (src)
2249 		*src = ZPROP_SRC_NONE;
2250 
2251 	switch (prop) {
2252 	case ZFS_PROP_CREATION:
2253 		/*
2254 		 * 'creation' is a time_t stored in the statistics.  We convert
2255 		 * this into a string unless 'literal' is specified.
2256 		 */
2257 		{
2258 			val = getprop_uint64(zhp, prop, &source);
2259 			time_t time = (time_t)val;
2260 			struct tm t;
2261 
2262 			if (literal ||
2263 			    localtime_r(&time, &t) == NULL ||
2264 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2265 			    &t) == 0)
2266 				(void) snprintf(propbuf, proplen, "%llu", val);
2267 		}
2268 		break;
2269 
2270 	case ZFS_PROP_MOUNTPOINT:
2271 		/*
2272 		 * Getting the precise mountpoint can be tricky.
2273 		 *
2274 		 *  - for 'none' or 'legacy', return those values.
2275 		 *  - for inherited mountpoints, we want to take everything
2276 		 *    after our ancestor and append it to the inherited value.
2277 		 *
2278 		 * If the pool has an alternate root, we want to prepend that
2279 		 * root to any values we return.
2280 		 */
2281 
2282 		str = getprop_string(zhp, prop, &source);
2283 
2284 		if (str[0] == '/') {
2285 			char buf[MAXPATHLEN];
2286 			char *root = buf;
2287 			const char *relpath;
2288 
2289 			/*
2290 			 * If we inherit the mountpoint, even from a dataset
2291 			 * with a received value, the source will be the path of
2292 			 * the dataset we inherit from. If source is
2293 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2294 			 * inherited.
2295 			 */
2296 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2297 				relpath = "";
2298 			} else {
2299 				relpath = zhp->zfs_name + strlen(source);
2300 				if (relpath[0] == '/')
2301 					relpath++;
2302 			}
2303 
2304 			if ((zpool_get_prop(zhp->zpool_hdl,
2305 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2306 			    B_FALSE)) || (strcmp(root, "-") == 0))
2307 				root[0] = '\0';
2308 			/*
2309 			 * Special case an alternate root of '/'. This will
2310 			 * avoid having multiple leading slashes in the
2311 			 * mountpoint path.
2312 			 */
2313 			if (strcmp(root, "/") == 0)
2314 				root++;
2315 
2316 			/*
2317 			 * If the mountpoint is '/' then skip over this
2318 			 * if we are obtaining either an alternate root or
2319 			 * an inherited mountpoint.
2320 			 */
2321 			if (str[1] == '\0' && (root[0] != '\0' ||
2322 			    relpath[0] != '\0'))
2323 				str++;
2324 
2325 			if (relpath[0] == '\0')
2326 				(void) snprintf(propbuf, proplen, "%s%s",
2327 				    root, str);
2328 			else
2329 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
2330 				    root, str, relpath[0] == '@' ? "" : "/",
2331 				    relpath);
2332 		} else {
2333 			/* 'legacy' or 'none' */
2334 			(void) strlcpy(propbuf, str, proplen);
2335 		}
2336 
2337 		break;
2338 
2339 	case ZFS_PROP_ORIGIN:
2340 		str = getprop_string(zhp, prop, &source);
2341 		if (str == NULL)
2342 			return (-1);
2343 		(void) strlcpy(propbuf, str, proplen);
2344 		break;
2345 
2346 	case ZFS_PROP_CLONES:
2347 		if (get_clones_string(zhp, propbuf, proplen) != 0)
2348 			return (-1);
2349 		break;
2350 
2351 	case ZFS_PROP_QUOTA:
2352 	case ZFS_PROP_REFQUOTA:
2353 	case ZFS_PROP_RESERVATION:
2354 	case ZFS_PROP_REFRESERVATION:
2355 
2356 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2357 			return (-1);
2358 
2359 		/*
2360 		 * If quota or reservation is 0, we translate this into 'none'
2361 		 * (unless literal is set), and indicate that it's the default
2362 		 * value.  Otherwise, we print the number nicely and indicate
2363 		 * that its set locally.
2364 		 */
2365 		if (val == 0) {
2366 			if (literal)
2367 				(void) strlcpy(propbuf, "0", proplen);
2368 			else
2369 				(void) strlcpy(propbuf, "none", proplen);
2370 		} else {
2371 			if (literal)
2372 				(void) snprintf(propbuf, proplen, "%llu",
2373 				    (u_longlong_t)val);
2374 			else
2375 				zfs_nicenum(val, propbuf, proplen);
2376 		}
2377 		break;
2378 
2379 	case ZFS_PROP_FILESYSTEM_LIMIT:
2380 	case ZFS_PROP_SNAPSHOT_LIMIT:
2381 	case ZFS_PROP_FILESYSTEM_COUNT:
2382 	case ZFS_PROP_SNAPSHOT_COUNT:
2383 
2384 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2385 			return (-1);
2386 
2387 		/*
2388 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2389 		 * literal is set), and indicate that it's the default value.
2390 		 * Otherwise, we print the number nicely and indicate that it's
2391 		 * set locally.
2392 		 */
2393 		if (literal) {
2394 			(void) snprintf(propbuf, proplen, "%llu",
2395 			    (u_longlong_t)val);
2396 		} else if (val == UINT64_MAX) {
2397 			(void) strlcpy(propbuf, "none", proplen);
2398 		} else {
2399 			zfs_nicenum(val, propbuf, proplen);
2400 		}
2401 		break;
2402 
2403 	case ZFS_PROP_REFRATIO:
2404 	case ZFS_PROP_COMPRESSRATIO:
2405 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2406 			return (-1);
2407 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2408 		    (u_longlong_t)(val / 100),
2409 		    (u_longlong_t)(val % 100));
2410 		break;
2411 
2412 	case ZFS_PROP_TYPE:
2413 		switch (zhp->zfs_type) {
2414 		case ZFS_TYPE_FILESYSTEM:
2415 			str = "filesystem";
2416 			break;
2417 		case ZFS_TYPE_VOLUME:
2418 			str = "volume";
2419 			break;
2420 		case ZFS_TYPE_SNAPSHOT:
2421 			str = "snapshot";
2422 			break;
2423 		case ZFS_TYPE_BOOKMARK:
2424 			str = "bookmark";
2425 			break;
2426 		default:
2427 			abort();
2428 		}
2429 		(void) snprintf(propbuf, proplen, "%s", str);
2430 		break;
2431 
2432 	case ZFS_PROP_MOUNTED:
2433 		/*
2434 		 * The 'mounted' property is a pseudo-property that described
2435 		 * whether the filesystem is currently mounted.  Even though
2436 		 * it's a boolean value, the typical values of "on" and "off"
2437 		 * don't make sense, so we translate to "yes" and "no".
2438 		 */
2439 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2440 		    src, &source, &val) != 0)
2441 			return (-1);
2442 		if (val)
2443 			(void) strlcpy(propbuf, "yes", proplen);
2444 		else
2445 			(void) strlcpy(propbuf, "no", proplen);
2446 		break;
2447 
2448 	case ZFS_PROP_NAME:
2449 		/*
2450 		 * The 'name' property is a pseudo-property derived from the
2451 		 * dataset name.  It is presented as a real property to simplify
2452 		 * consumers.
2453 		 */
2454 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2455 		break;
2456 
2457 	case ZFS_PROP_MLSLABEL:
2458 		{
2459 			m_label_t *new_sl = NULL;
2460 			char *ascii = NULL;	/* human readable label */
2461 
2462 			(void) strlcpy(propbuf,
2463 			    getprop_string(zhp, prop, &source), proplen);
2464 
2465 			if (literal || (strcasecmp(propbuf,
2466 			    ZFS_MLSLABEL_DEFAULT) == 0))
2467 				break;
2468 
2469 			/*
2470 			 * Try to translate the internal hex string to
2471 			 * human-readable output.  If there are any
2472 			 * problems just use the hex string.
2473 			 */
2474 
2475 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2476 			    L_NO_CORRECTION, NULL) == -1) {
2477 				m_label_free(new_sl);
2478 				break;
2479 			}
2480 
2481 			if (label_to_str(new_sl, &ascii, M_LABEL,
2482 			    DEF_NAMES) != 0) {
2483 				if (ascii)
2484 					free(ascii);
2485 				m_label_free(new_sl);
2486 				break;
2487 			}
2488 			m_label_free(new_sl);
2489 
2490 			(void) strlcpy(propbuf, ascii, proplen);
2491 			free(ascii);
2492 		}
2493 		break;
2494 
2495 	case ZFS_PROP_GUID:
2496 		/*
2497 		 * GUIDs are stored as numbers, but they are identifiers.
2498 		 * We don't want them to be pretty printed, because pretty
2499 		 * printing mangles the ID into a truncated and useless value.
2500 		 */
2501 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2502 			return (-1);
2503 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2504 		break;
2505 
2506 	default:
2507 		switch (zfs_prop_get_type(prop)) {
2508 		case PROP_TYPE_NUMBER:
2509 			if (get_numeric_property(zhp, prop, src,
2510 			    &source, &val) != 0)
2511 				return (-1);
2512 			if (literal)
2513 				(void) snprintf(propbuf, proplen, "%llu",
2514 				    (u_longlong_t)val);
2515 			else
2516 				zfs_nicenum(val, propbuf, proplen);
2517 			break;
2518 
2519 		case PROP_TYPE_STRING:
2520 			str = getprop_string(zhp, prop, &source);
2521 			if (str == NULL)
2522 				return (-1);
2523 			(void) strlcpy(propbuf, str, proplen);
2524 			break;
2525 
2526 		case PROP_TYPE_INDEX:
2527 			if (get_numeric_property(zhp, prop, src,
2528 			    &source, &val) != 0)
2529 				return (-1);
2530 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2531 				return (-1);
2532 			(void) strlcpy(propbuf, strval, proplen);
2533 			break;
2534 
2535 		default:
2536 			abort();
2537 		}
2538 	}
2539 
2540 	get_source(zhp, src, source, statbuf, statlen);
2541 
2542 	return (0);
2543 }
2544 
2545 /*
2546  * Utility function to get the given numeric property.  Does no validation that
2547  * the given property is the appropriate type; should only be used with
2548  * hard-coded property types.
2549  */
2550 uint64_t
2551 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2552 {
2553 	char *source;
2554 	uint64_t val;
2555 
2556 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2557 
2558 	return (val);
2559 }
2560 
2561 int
2562 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2563 {
2564 	char buf[64];
2565 
2566 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2567 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2568 }
2569 
2570 /*
2571  * Similar to zfs_prop_get(), but returns the value as an integer.
2572  */
2573 int
2574 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2575     zprop_source_t *src, char *statbuf, size_t statlen)
2576 {
2577 	char *source;
2578 
2579 	/*
2580 	 * Check to see if this property applies to our object
2581 	 */
2582 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2583 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2584 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2585 		    zfs_prop_to_name(prop)));
2586 	}
2587 
2588 	if (src)
2589 		*src = ZPROP_SRC_NONE;
2590 
2591 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2592 		return (-1);
2593 
2594 	get_source(zhp, src, source, statbuf, statlen);
2595 
2596 	return (0);
2597 }
2598 
2599 static int
2600 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2601     char **domainp, idmap_rid_t *ridp)
2602 {
2603 	idmap_get_handle_t *get_hdl = NULL;
2604 	idmap_stat status;
2605 	int err = EINVAL;
2606 
2607 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2608 		goto out;
2609 
2610 	if (isuser) {
2611 		err = idmap_get_sidbyuid(get_hdl, id,
2612 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2613 	} else {
2614 		err = idmap_get_sidbygid(get_hdl, id,
2615 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2616 	}
2617 	if (err == IDMAP_SUCCESS &&
2618 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2619 	    status == IDMAP_SUCCESS)
2620 		err = 0;
2621 	else
2622 		err = EINVAL;
2623 out:
2624 	if (get_hdl)
2625 		idmap_get_destroy(get_hdl);
2626 	return (err);
2627 }
2628 
2629 /*
2630  * convert the propname into parameters needed by kernel
2631  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2632  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2633  */
2634 static int
2635 userquota_propname_decode(const char *propname, boolean_t zoned,
2636     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2637 {
2638 	zfs_userquota_prop_t type;
2639 	char *cp, *end;
2640 	char *numericsid = NULL;
2641 	boolean_t isuser;
2642 
2643 	domain[0] = '\0';
2644 	*ridp = 0;
2645 	/* Figure out the property type ({user|group}{quota|space}) */
2646 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2647 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2648 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
2649 			break;
2650 	}
2651 	if (type == ZFS_NUM_USERQUOTA_PROPS)
2652 		return (EINVAL);
2653 	*typep = type;
2654 
2655 	isuser = (type == ZFS_PROP_USERQUOTA ||
2656 	    type == ZFS_PROP_USERUSED);
2657 
2658 	cp = strchr(propname, '@') + 1;
2659 
2660 	if (strchr(cp, '@')) {
2661 		/*
2662 		 * It's a SID name (eg "user@domain") that needs to be
2663 		 * turned into S-1-domainID-RID.
2664 		 */
2665 		int flag = 0;
2666 		idmap_stat stat, map_stat;
2667 		uid_t pid;
2668 		idmap_rid_t rid;
2669 		idmap_get_handle_t *gh = NULL;
2670 
2671 		stat = idmap_get_create(&gh);
2672 		if (stat != IDMAP_SUCCESS) {
2673 			idmap_get_destroy(gh);
2674 			return (ENOMEM);
2675 		}
2676 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2677 			return (ENOENT);
2678 		if (isuser) {
2679 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2680 			if (stat < 0)
2681 				return (ENOENT);
2682 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2683 			    &rid, &map_stat);
2684 		} else {
2685 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2686 			if (stat < 0)
2687 				return (ENOENT);
2688 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2689 			    &rid, &map_stat);
2690 		}
2691 		if (stat < 0) {
2692 			idmap_get_destroy(gh);
2693 			return (ENOENT);
2694 		}
2695 		stat = idmap_get_mappings(gh);
2696 		idmap_get_destroy(gh);
2697 
2698 		if (stat < 0) {
2699 			return (ENOENT);
2700 		}
2701 		if (numericsid == NULL)
2702 			return (ENOENT);
2703 		cp = numericsid;
2704 		*ridp = rid;
2705 		/* will be further decoded below */
2706 	}
2707 
2708 	if (strncmp(cp, "S-1-", 4) == 0) {
2709 		/* It's a numeric SID (eg "S-1-234-567-89") */
2710 		(void) strlcpy(domain, cp, domainlen);
2711 		errno = 0;
2712 		if (*ridp == 0) {
2713 			cp = strrchr(domain, '-');
2714 			*cp = '\0';
2715 			cp++;
2716 			*ridp = strtoull(cp, &end, 10);
2717 		} else {
2718 			end = "";
2719 		}
2720 		if (numericsid) {
2721 			free(numericsid);
2722 			numericsid = NULL;
2723 		}
2724 		if (errno != 0 || *end != '\0')
2725 			return (EINVAL);
2726 	} else if (!isdigit(*cp)) {
2727 		/*
2728 		 * It's a user/group name (eg "user") that needs to be
2729 		 * turned into a uid/gid
2730 		 */
2731 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2732 			return (ENOENT);
2733 		if (isuser) {
2734 			struct passwd *pw;
2735 			pw = getpwnam(cp);
2736 			if (pw == NULL)
2737 				return (ENOENT);
2738 			*ridp = pw->pw_uid;
2739 		} else {
2740 			struct group *gr;
2741 			gr = getgrnam(cp);
2742 			if (gr == NULL)
2743 				return (ENOENT);
2744 			*ridp = gr->gr_gid;
2745 		}
2746 	} else {
2747 		/* It's a user/group ID (eg "12345"). */
2748 		uid_t id = strtoul(cp, &end, 10);
2749 		idmap_rid_t rid;
2750 		char *mapdomain;
2751 
2752 		if (*end != '\0')
2753 			return (EINVAL);
2754 		if (id > MAXUID) {
2755 			/* It's an ephemeral ID. */
2756 			if (idmap_id_to_numeric_domain_rid(id, isuser,
2757 			    &mapdomain, &rid) != 0)
2758 				return (ENOENT);
2759 			(void) strlcpy(domain, mapdomain, domainlen);
2760 			*ridp = rid;
2761 		} else {
2762 			*ridp = id;
2763 		}
2764 	}
2765 
2766 	ASSERT3P(numericsid, ==, NULL);
2767 	return (0);
2768 }
2769 
2770 static int
2771 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2772     uint64_t *propvalue, zfs_userquota_prop_t *typep)
2773 {
2774 	int err;
2775 	zfs_cmd_t zc = { 0 };
2776 
2777 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2778 
2779 	err = userquota_propname_decode(propname,
2780 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2781 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2782 	zc.zc_objset_type = *typep;
2783 	if (err)
2784 		return (err);
2785 
2786 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2787 	if (err)
2788 		return (err);
2789 
2790 	*propvalue = zc.zc_cookie;
2791 	return (0);
2792 }
2793 
2794 int
2795 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2796     uint64_t *propvalue)
2797 {
2798 	zfs_userquota_prop_t type;
2799 
2800 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2801 	    &type));
2802 }
2803 
2804 int
2805 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2806     char *propbuf, int proplen, boolean_t literal)
2807 {
2808 	int err;
2809 	uint64_t propvalue;
2810 	zfs_userquota_prop_t type;
2811 
2812 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2813 	    &type);
2814 
2815 	if (err)
2816 		return (err);
2817 
2818 	if (literal) {
2819 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2820 	} else if (propvalue == 0 &&
2821 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2822 		(void) strlcpy(propbuf, "none", proplen);
2823 	} else {
2824 		zfs_nicenum(propvalue, propbuf, proplen);
2825 	}
2826 	return (0);
2827 }
2828 
2829 int
2830 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2831     uint64_t *propvalue)
2832 {
2833 	int err;
2834 	zfs_cmd_t zc = { 0 };
2835 	const char *snapname;
2836 
2837 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2838 
2839 	snapname = strchr(propname, '@') + 1;
2840 	if (strchr(snapname, '@')) {
2841 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2842 	} else {
2843 		/* snapname is the short name, append it to zhp's fsname */
2844 		char *cp;
2845 
2846 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
2847 		    sizeof (zc.zc_value));
2848 		cp = strchr(zc.zc_value, '@');
2849 		if (cp != NULL)
2850 			*cp = '\0';
2851 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2852 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2853 	}
2854 
2855 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2856 	if (err)
2857 		return (err);
2858 
2859 	*propvalue = zc.zc_cookie;
2860 	return (0);
2861 }
2862 
2863 int
2864 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2865     char *propbuf, int proplen, boolean_t literal)
2866 {
2867 	int err;
2868 	uint64_t propvalue;
2869 
2870 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2871 
2872 	if (err)
2873 		return (err);
2874 
2875 	if (literal) {
2876 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2877 	} else {
2878 		zfs_nicenum(propvalue, propbuf, proplen);
2879 	}
2880 	return (0);
2881 }
2882 
2883 /*
2884  * Returns the name of the given zfs handle.
2885  */
2886 const char *
2887 zfs_get_name(const zfs_handle_t *zhp)
2888 {
2889 	return (zhp->zfs_name);
2890 }
2891 
2892 /*
2893  * Returns the type of the given zfs handle.
2894  */
2895 zfs_type_t
2896 zfs_get_type(const zfs_handle_t *zhp)
2897 {
2898 	return (zhp->zfs_type);
2899 }
2900 
2901 /*
2902  * Is one dataset name a child dataset of another?
2903  *
2904  * Needs to handle these cases:
2905  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
2906  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
2907  * Descendant?	No.		No.		No.		Yes.
2908  */
2909 static boolean_t
2910 is_descendant(const char *ds1, const char *ds2)
2911 {
2912 	size_t d1len = strlen(ds1);
2913 
2914 	/* ds2 can't be a descendant if it's smaller */
2915 	if (strlen(ds2) < d1len)
2916 		return (B_FALSE);
2917 
2918 	/* otherwise, compare strings and verify that there's a '/' char */
2919 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2920 }
2921 
2922 /*
2923  * Given a complete name, return just the portion that refers to the parent.
2924  * Will return -1 if there is no parent (path is just the name of the
2925  * pool).
2926  */
2927 static int
2928 parent_name(const char *path, char *buf, size_t buflen)
2929 {
2930 	char *slashp;
2931 
2932 	(void) strlcpy(buf, path, buflen);
2933 
2934 	if ((slashp = strrchr(buf, '/')) == NULL)
2935 		return (-1);
2936 	*slashp = '\0';
2937 
2938 	return (0);
2939 }
2940 
2941 /*
2942  * If accept_ancestor is false, then check to make sure that the given path has
2943  * a parent, and that it exists.  If accept_ancestor is true, then find the
2944  * closest existing ancestor for the given path.  In prefixlen return the
2945  * length of already existing prefix of the given path.  We also fetch the
2946  * 'zoned' property, which is used to validate property settings when creating
2947  * new datasets.
2948  */
2949 static int
2950 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2951     boolean_t accept_ancestor, int *prefixlen)
2952 {
2953 	zfs_cmd_t zc = { 0 };
2954 	char parent[ZFS_MAXNAMELEN];
2955 	char *slash;
2956 	zfs_handle_t *zhp;
2957 	char errbuf[1024];
2958 	uint64_t is_zoned;
2959 
2960 	(void) snprintf(errbuf, sizeof (errbuf),
2961 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2962 
2963 	/* get parent, and check to see if this is just a pool */
2964 	if (parent_name(path, parent, sizeof (parent)) != 0) {
2965 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2966 		    "missing dataset name"));
2967 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2968 	}
2969 
2970 	/* check to see if the pool exists */
2971 	if ((slash = strchr(parent, '/')) == NULL)
2972 		slash = parent + strlen(parent);
2973 	(void) strncpy(zc.zc_name, parent, slash - parent);
2974 	zc.zc_name[slash - parent] = '\0';
2975 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2976 	    errno == ENOENT) {
2977 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2978 		    "no such pool '%s'"), zc.zc_name);
2979 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2980 	}
2981 
2982 	/* check to see if the parent dataset exists */
2983 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2984 		if (errno == ENOENT && accept_ancestor) {
2985 			/*
2986 			 * Go deeper to find an ancestor, give up on top level.
2987 			 */
2988 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
2989 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2990 				    "no such pool '%s'"), zc.zc_name);
2991 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
2992 			}
2993 		} else if (errno == ENOENT) {
2994 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2995 			    "parent does not exist"));
2996 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2997 		} else
2998 			return (zfs_standard_error(hdl, errno, errbuf));
2999 	}
3000 
3001 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3002 	if (zoned != NULL)
3003 		*zoned = is_zoned;
3004 
3005 	/* we are in a non-global zone, but parent is in the global zone */
3006 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3007 		(void) zfs_standard_error(hdl, EPERM, errbuf);
3008 		zfs_close(zhp);
3009 		return (-1);
3010 	}
3011 
3012 	/* make sure parent is a filesystem */
3013 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3014 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3015 		    "parent is not a filesystem"));
3016 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3017 		zfs_close(zhp);
3018 		return (-1);
3019 	}
3020 
3021 	zfs_close(zhp);
3022 	if (prefixlen != NULL)
3023 		*prefixlen = strlen(parent);
3024 	return (0);
3025 }
3026 
3027 /*
3028  * Finds whether the dataset of the given type(s) exists.
3029  */
3030 boolean_t
3031 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3032 {
3033 	zfs_handle_t *zhp;
3034 
3035 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
3036 		return (B_FALSE);
3037 
3038 	/*
3039 	 * Try to get stats for the dataset, which will tell us if it exists.
3040 	 */
3041 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3042 		int ds_type = zhp->zfs_type;
3043 
3044 		zfs_close(zhp);
3045 		if (types & ds_type)
3046 			return (B_TRUE);
3047 	}
3048 	return (B_FALSE);
3049 }
3050 
3051 /*
3052  * Given a path to 'target', create all the ancestors between
3053  * the prefixlen portion of the path, and the target itself.
3054  * Fail if the initial prefixlen-ancestor does not already exist.
3055  */
3056 int
3057 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3058 {
3059 	zfs_handle_t *h;
3060 	char *cp;
3061 	const char *opname;
3062 
3063 	/* make sure prefix exists */
3064 	cp = target + prefixlen;
3065 	if (*cp != '/') {
3066 		assert(strchr(cp, '/') == NULL);
3067 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3068 	} else {
3069 		*cp = '\0';
3070 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3071 		*cp = '/';
3072 	}
3073 	if (h == NULL)
3074 		return (-1);
3075 	zfs_close(h);
3076 
3077 	/*
3078 	 * Attempt to create, mount, and share any ancestor filesystems,
3079 	 * up to the prefixlen-long one.
3080 	 */
3081 	for (cp = target + prefixlen + 1;
3082 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
3083 
3084 		*cp = '\0';
3085 
3086 		h = make_dataset_handle(hdl, target);
3087 		if (h) {
3088 			/* it already exists, nothing to do here */
3089 			zfs_close(h);
3090 			continue;
3091 		}
3092 
3093 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3094 		    NULL) != 0) {
3095 			opname = dgettext(TEXT_DOMAIN, "create");
3096 			goto ancestorerr;
3097 		}
3098 
3099 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3100 		if (h == NULL) {
3101 			opname = dgettext(TEXT_DOMAIN, "open");
3102 			goto ancestorerr;
3103 		}
3104 
3105 		if (zfs_mount(h, NULL, 0) != 0) {
3106 			opname = dgettext(TEXT_DOMAIN, "mount");
3107 			goto ancestorerr;
3108 		}
3109 
3110 		if (zfs_share(h) != 0) {
3111 			opname = dgettext(TEXT_DOMAIN, "share");
3112 			goto ancestorerr;
3113 		}
3114 
3115 		zfs_close(h);
3116 	}
3117 
3118 	return (0);
3119 
3120 ancestorerr:
3121 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3122 	    "failed to %s ancestor '%s'"), opname, target);
3123 	return (-1);
3124 }
3125 
3126 /*
3127  * Creates non-existing ancestors of the given path.
3128  */
3129 int
3130 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3131 {
3132 	int prefix;
3133 	char *path_copy;
3134 	int rc;
3135 
3136 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3137 		return (-1);
3138 
3139 	if ((path_copy = strdup(path)) != NULL) {
3140 		rc = create_parents(hdl, path_copy, prefix);
3141 		free(path_copy);
3142 	}
3143 	if (path_copy == NULL || rc != 0)
3144 		return (-1);
3145 
3146 	return (0);
3147 }
3148 
3149 /*
3150  * Create a new filesystem or volume.
3151  */
3152 int
3153 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3154     nvlist_t *props)
3155 {
3156 	int ret;
3157 	uint64_t size = 0;
3158 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3159 	char errbuf[1024];
3160 	uint64_t zoned;
3161 	dmu_objset_type_t ost;
3162 
3163 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3164 	    "cannot create '%s'"), path);
3165 
3166 	/* validate the path, taking care to note the extended error message */
3167 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
3168 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3169 
3170 	/* validate parents exist */
3171 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3172 		return (-1);
3173 
3174 	/*
3175 	 * The failure modes when creating a dataset of a different type over
3176 	 * one that already exists is a little strange.  In particular, if you
3177 	 * try to create a dataset on top of an existing dataset, the ioctl()
3178 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3179 	 * first try to see if the dataset exists.
3180 	 */
3181 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3182 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3183 		    "dataset already exists"));
3184 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3185 	}
3186 
3187 	if (type == ZFS_TYPE_VOLUME)
3188 		ost = DMU_OST_ZVOL;
3189 	else
3190 		ost = DMU_OST_ZFS;
3191 
3192 	/* open zpool handle for prop validation */
3193 	char pool_path[MAXNAMELEN];
3194 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3195 
3196 	/* truncate pool_path at first slash */
3197 	char *p = strchr(pool_path, '/');
3198 	if (p != NULL)
3199 		*p = '\0';
3200 
3201 	zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3202 
3203 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3204 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3205 		zpool_close(zpool_handle);
3206 		return (-1);
3207 	}
3208 	zpool_close(zpool_handle);
3209 
3210 	if (type == ZFS_TYPE_VOLUME) {
3211 		/*
3212 		 * If we are creating a volume, the size and block size must
3213 		 * satisfy a few restraints.  First, the blocksize must be a
3214 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3215 		 * volsize must be a multiple of the block size, and cannot be
3216 		 * zero.
3217 		 */
3218 		if (props == NULL || nvlist_lookup_uint64(props,
3219 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3220 			nvlist_free(props);
3221 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3222 			    "missing volume size"));
3223 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3224 		}
3225 
3226 		if ((ret = nvlist_lookup_uint64(props,
3227 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3228 		    &blocksize)) != 0) {
3229 			if (ret == ENOENT) {
3230 				blocksize = zfs_prop_default_numeric(
3231 				    ZFS_PROP_VOLBLOCKSIZE);
3232 			} else {
3233 				nvlist_free(props);
3234 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3235 				    "missing volume block size"));
3236 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3237 			}
3238 		}
3239 
3240 		if (size == 0) {
3241 			nvlist_free(props);
3242 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3243 			    "volume size cannot be zero"));
3244 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3245 		}
3246 
3247 		if (size % blocksize != 0) {
3248 			nvlist_free(props);
3249 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3250 			    "volume size must be a multiple of volume block "
3251 			    "size"));
3252 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3253 		}
3254 	}
3255 
3256 	/* create the dataset */
3257 	ret = lzc_create(path, ost, props);
3258 	nvlist_free(props);
3259 
3260 	/* check for failure */
3261 	if (ret != 0) {
3262 		char parent[ZFS_MAXNAMELEN];
3263 		(void) parent_name(path, parent, sizeof (parent));
3264 
3265 		switch (errno) {
3266 		case ENOENT:
3267 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3268 			    "no such parent '%s'"), parent);
3269 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3270 
3271 		case EINVAL:
3272 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3273 			    "parent '%s' is not a filesystem"), parent);
3274 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3275 
3276 		case ENOTSUP:
3277 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3278 			    "pool must be upgraded to set this "
3279 			    "property or value"));
3280 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3281 #ifdef _ILP32
3282 		case EOVERFLOW:
3283 			/*
3284 			 * This platform can't address a volume this big.
3285 			 */
3286 			if (type == ZFS_TYPE_VOLUME)
3287 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
3288 				    errbuf));
3289 #endif
3290 			/* FALLTHROUGH */
3291 		default:
3292 			return (zfs_standard_error(hdl, errno, errbuf));
3293 		}
3294 	}
3295 
3296 	return (0);
3297 }
3298 
3299 /*
3300  * Destroys the given dataset.  The caller must make sure that the filesystem
3301  * isn't mounted, and that there are no active dependents. If the file system
3302  * does not exist this function does nothing.
3303  */
3304 int
3305 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3306 {
3307 	zfs_cmd_t zc = { 0 };
3308 
3309 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3310 		nvlist_t *nv = fnvlist_alloc();
3311 		fnvlist_add_boolean(nv, zhp->zfs_name);
3312 		int error = lzc_destroy_bookmarks(nv, NULL);
3313 		fnvlist_free(nv);
3314 		if (error != 0) {
3315 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3316 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3317 			    zhp->zfs_name));
3318 		}
3319 		return (0);
3320 	}
3321 
3322 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3323 
3324 	if (ZFS_IS_VOLUME(zhp)) {
3325 		zc.zc_objset_type = DMU_OST_ZVOL;
3326 	} else {
3327 		zc.zc_objset_type = DMU_OST_ZFS;
3328 	}
3329 
3330 	zc.zc_defer_destroy = defer;
3331 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3332 	    errno != ENOENT) {
3333 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3334 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3335 		    zhp->zfs_name));
3336 	}
3337 
3338 	remove_mountpoint(zhp);
3339 
3340 	return (0);
3341 }
3342 
3343 struct destroydata {
3344 	nvlist_t *nvl;
3345 	const char *snapname;
3346 };
3347 
3348 static int
3349 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3350 {
3351 	struct destroydata *dd = arg;
3352 	char name[ZFS_MAXNAMELEN];
3353 	int rv = 0;
3354 
3355 	(void) snprintf(name, sizeof (name),
3356 	    "%s@%s", zhp->zfs_name, dd->snapname);
3357 
3358 	if (lzc_exists(name))
3359 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
3360 
3361 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3362 	zfs_close(zhp);
3363 	return (rv);
3364 }
3365 
3366 /*
3367  * Destroys all snapshots with the given name in zhp & descendants.
3368  */
3369 int
3370 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3371 {
3372 	int ret;
3373 	struct destroydata dd = { 0 };
3374 
3375 	dd.snapname = snapname;
3376 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3377 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3378 
3379 	if (nvlist_empty(dd.nvl)) {
3380 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3381 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3382 		    zhp->zfs_name, snapname);
3383 	} else {
3384 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3385 	}
3386 	nvlist_free(dd.nvl);
3387 	return (ret);
3388 }
3389 
3390 /*
3391  * Destroys all the snapshots named in the nvlist.
3392  */
3393 int
3394 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3395 {
3396 	int ret;
3397 	nvlist_t *errlist;
3398 
3399 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
3400 
3401 	if (ret == 0)
3402 		return (0);
3403 
3404 	if (nvlist_empty(errlist)) {
3405 		char errbuf[1024];
3406 		(void) snprintf(errbuf, sizeof (errbuf),
3407 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3408 
3409 		ret = zfs_standard_error(hdl, ret, errbuf);
3410 	}
3411 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3412 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3413 		char errbuf[1024];
3414 		(void) snprintf(errbuf, sizeof (errbuf),
3415 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3416 		    nvpair_name(pair));
3417 
3418 		switch (fnvpair_value_int32(pair)) {
3419 		case EEXIST:
3420 			zfs_error_aux(hdl,
3421 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3422 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3423 			break;
3424 		default:
3425 			ret = zfs_standard_error(hdl, errno, errbuf);
3426 			break;
3427 		}
3428 	}
3429 
3430 	return (ret);
3431 }
3432 
3433 /*
3434  * Clones the given dataset.  The target must be of the same type as the source.
3435  */
3436 int
3437 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3438 {
3439 	char parent[ZFS_MAXNAMELEN];
3440 	int ret;
3441 	char errbuf[1024];
3442 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3443 	uint64_t zoned;
3444 
3445 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3446 
3447 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3448 	    "cannot create '%s'"), target);
3449 
3450 	/* validate the target/clone name */
3451 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3452 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3453 
3454 	/* validate parents exist */
3455 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3456 		return (-1);
3457 
3458 	(void) parent_name(target, parent, sizeof (parent));
3459 
3460 	/* do the clone */
3461 
3462 	if (props) {
3463 		zfs_type_t type;
3464 		if (ZFS_IS_VOLUME(zhp)) {
3465 			type = ZFS_TYPE_VOLUME;
3466 		} else {
3467 			type = ZFS_TYPE_FILESYSTEM;
3468 		}
3469 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3470 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3471 			return (-1);
3472 	}
3473 
3474 	ret = lzc_clone(target, zhp->zfs_name, props);
3475 	nvlist_free(props);
3476 
3477 	if (ret != 0) {
3478 		switch (errno) {
3479 
3480 		case ENOENT:
3481 			/*
3482 			 * The parent doesn't exist.  We should have caught this
3483 			 * above, but there may a race condition that has since
3484 			 * destroyed the parent.
3485 			 *
3486 			 * At this point, we don't know whether it's the source
3487 			 * that doesn't exist anymore, or whether the target
3488 			 * dataset doesn't exist.
3489 			 */
3490 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3491 			    "no such parent '%s'"), parent);
3492 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3493 
3494 		case EXDEV:
3495 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3496 			    "source and target pools differ"));
3497 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3498 			    errbuf));
3499 
3500 		default:
3501 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3502 			    errbuf));
3503 		}
3504 	}
3505 
3506 	return (ret);
3507 }
3508 
3509 /*
3510  * Promotes the given clone fs to be the clone parent.
3511  */
3512 int
3513 zfs_promote(zfs_handle_t *zhp)
3514 {
3515 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3516 	zfs_cmd_t zc = { 0 };
3517 	char parent[MAXPATHLEN];
3518 	int ret;
3519 	char errbuf[1024];
3520 
3521 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3522 	    "cannot promote '%s'"), zhp->zfs_name);
3523 
3524 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3525 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3526 		    "snapshots can not be promoted"));
3527 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3528 	}
3529 
3530 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3531 	if (parent[0] == '\0') {
3532 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3533 		    "not a cloned filesystem"));
3534 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3535 	}
3536 
3537 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3538 	    sizeof (zc.zc_value));
3539 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3540 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3541 
3542 	if (ret != 0) {
3543 		int save_errno = errno;
3544 
3545 		switch (save_errno) {
3546 		case EEXIST:
3547 			/* There is a conflicting snapshot name. */
3548 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3549 			    "conflicting snapshot '%s' from parent '%s'"),
3550 			    zc.zc_string, parent);
3551 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3552 
3553 		default:
3554 			return (zfs_standard_error(hdl, save_errno, errbuf));
3555 		}
3556 	}
3557 	return (ret);
3558 }
3559 
3560 typedef struct snapdata {
3561 	nvlist_t *sd_nvl;
3562 	const char *sd_snapname;
3563 } snapdata_t;
3564 
3565 static int
3566 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3567 {
3568 	snapdata_t *sd = arg;
3569 	char name[ZFS_MAXNAMELEN];
3570 	int rv = 0;
3571 
3572 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3573 		(void) snprintf(name, sizeof (name),
3574 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3575 
3576 		fnvlist_add_boolean(sd->sd_nvl, name);
3577 
3578 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3579 	}
3580 	zfs_close(zhp);
3581 
3582 	return (rv);
3583 }
3584 
3585 /*
3586  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3587  * created.
3588  */
3589 int
3590 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3591 {
3592 	int ret;
3593 	char errbuf[1024];
3594 	nvpair_t *elem;
3595 	nvlist_t *errors;
3596 
3597 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3598 	    "cannot create snapshots "));
3599 
3600 	elem = NULL;
3601 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3602 		const char *snapname = nvpair_name(elem);
3603 
3604 		/* validate the target name */
3605 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3606 		    B_TRUE)) {
3607 			(void) snprintf(errbuf, sizeof (errbuf),
3608 			    dgettext(TEXT_DOMAIN,
3609 			    "cannot create snapshot '%s'"), snapname);
3610 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3611 		}
3612 	}
3613 
3614 	/*
3615 	 * get pool handle for prop validation. assumes all snaps are in the
3616 	 * same pool, as does lzc_snapshot (below).
3617 	 */
3618 	char pool[MAXNAMELEN];
3619 	elem = nvlist_next_nvpair(snaps, NULL);
3620 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3621 	pool[strcspn(pool, "/@")] = '\0';
3622 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3623 
3624 	if (props != NULL &&
3625 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3626 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3627 		zpool_close(zpool_hdl);
3628 		return (-1);
3629 	}
3630 	zpool_close(zpool_hdl);
3631 
3632 	ret = lzc_snapshot(snaps, props, &errors);
3633 
3634 	if (ret != 0) {
3635 		boolean_t printed = B_FALSE;
3636 		for (elem = nvlist_next_nvpair(errors, NULL);
3637 		    elem != NULL;
3638 		    elem = nvlist_next_nvpair(errors, elem)) {
3639 			(void) snprintf(errbuf, sizeof (errbuf),
3640 			    dgettext(TEXT_DOMAIN,
3641 			    "cannot create snapshot '%s'"), nvpair_name(elem));
3642 			(void) zfs_standard_error(hdl,
3643 			    fnvpair_value_int32(elem), errbuf);
3644 			printed = B_TRUE;
3645 		}
3646 		if (!printed) {
3647 			switch (ret) {
3648 			case EXDEV:
3649 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3650 				    "multiple snapshots of same "
3651 				    "fs not allowed"));
3652 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3653 
3654 				break;
3655 			default:
3656 				(void) zfs_standard_error(hdl, ret, errbuf);
3657 			}
3658 		}
3659 	}
3660 
3661 	nvlist_free(props);
3662 	nvlist_free(errors);
3663 	return (ret);
3664 }
3665 
3666 int
3667 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3668     nvlist_t *props)
3669 {
3670 	int ret;
3671 	snapdata_t sd = { 0 };
3672 	char fsname[ZFS_MAXNAMELEN];
3673 	char *cp;
3674 	zfs_handle_t *zhp;
3675 	char errbuf[1024];
3676 
3677 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3678 	    "cannot snapshot %s"), path);
3679 
3680 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3681 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3682 
3683 	(void) strlcpy(fsname, path, sizeof (fsname));
3684 	cp = strchr(fsname, '@');
3685 	*cp = '\0';
3686 	sd.sd_snapname = cp + 1;
3687 
3688 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3689 	    ZFS_TYPE_VOLUME)) == NULL) {
3690 		return (-1);
3691 	}
3692 
3693 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3694 	if (recursive) {
3695 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3696 	} else {
3697 		fnvlist_add_boolean(sd.sd_nvl, path);
3698 	}
3699 
3700 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3701 	nvlist_free(sd.sd_nvl);
3702 	zfs_close(zhp);
3703 	return (ret);
3704 }
3705 
3706 /*
3707  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3708  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3709  * is a dependent and we should just destroy it without checking the transaction
3710  * group.
3711  */
3712 typedef struct rollback_data {
3713 	const char	*cb_target;		/* the snapshot */
3714 	uint64_t	cb_create;		/* creation time reference */
3715 	boolean_t	cb_error;
3716 	boolean_t	cb_force;
3717 } rollback_data_t;
3718 
3719 static int
3720 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3721 {
3722 	rollback_data_t *cbp = data;
3723 	prop_changelist_t *clp;
3724 
3725 	/* We must destroy this clone; first unmount it */
3726 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3727 	    cbp->cb_force ? MS_FORCE: 0);
3728 	if (clp == NULL || changelist_prefix(clp) != 0) {
3729 		cbp->cb_error = B_TRUE;
3730 		zfs_close(zhp);
3731 		return (0);
3732 	}
3733 	if (zfs_destroy(zhp, B_FALSE) != 0)
3734 		cbp->cb_error = B_TRUE;
3735 	else
3736 		changelist_remove(clp, zhp->zfs_name);
3737 	(void) changelist_postfix(clp);
3738 	changelist_free(clp);
3739 
3740 	zfs_close(zhp);
3741 	return (0);
3742 }
3743 
3744 static int
3745 rollback_destroy(zfs_handle_t *zhp, void *data)
3746 {
3747 	rollback_data_t *cbp = data;
3748 
3749 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3750 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3751 		    rollback_destroy_dependent, cbp);
3752 
3753 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3754 	}
3755 
3756 	zfs_close(zhp);
3757 	return (0);
3758 }
3759 
3760 /*
3761  * Given a dataset, rollback to a specific snapshot, discarding any
3762  * data changes since then and making it the active dataset.
3763  *
3764  * Any snapshots and bookmarks more recent than the target are
3765  * destroyed, along with their dependents (i.e. clones).
3766  */
3767 int
3768 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3769 {
3770 	rollback_data_t cb = { 0 };
3771 	int err;
3772 	boolean_t restore_resv = 0;
3773 	uint64_t old_volsize, new_volsize;
3774 	zfs_prop_t resv_prop;
3775 
3776 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3777 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3778 
3779 	/*
3780 	 * Destroy all recent snapshots and their dependents.
3781 	 */
3782 	cb.cb_force = force;
3783 	cb.cb_target = snap->zfs_name;
3784 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3785 	(void) zfs_iter_snapshots(zhp, rollback_destroy, &cb);
3786 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3787 
3788 	if (cb.cb_error)
3789 		return (-1);
3790 
3791 	/*
3792 	 * Now that we have verified that the snapshot is the latest,
3793 	 * rollback to the given snapshot.
3794 	 */
3795 
3796 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3797 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3798 			return (-1);
3799 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3800 		restore_resv =
3801 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3802 	}
3803 
3804 	/*
3805 	 * We rely on zfs_iter_children() to verify that there are no
3806 	 * newer snapshots for the given dataset.  Therefore, we can
3807 	 * simply pass the name on to the ioctl() call.  There is still
3808 	 * an unlikely race condition where the user has taken a
3809 	 * snapshot since we verified that this was the most recent.
3810 	 */
3811 	err = lzc_rollback(zhp->zfs_name, NULL, 0);
3812 	if (err != 0) {
3813 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3814 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3815 		    zhp->zfs_name);
3816 		return (err);
3817 	}
3818 
3819 	/*
3820 	 * For volumes, if the pre-rollback volsize matched the pre-
3821 	 * rollback reservation and the volsize has changed then set
3822 	 * the reservation property to the post-rollback volsize.
3823 	 * Make a new handle since the rollback closed the dataset.
3824 	 */
3825 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3826 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3827 		if (restore_resv) {
3828 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3829 			if (old_volsize != new_volsize)
3830 				err = zfs_prop_set_int(zhp, resv_prop,
3831 				    new_volsize);
3832 		}
3833 		zfs_close(zhp);
3834 	}
3835 	return (err);
3836 }
3837 
3838 /*
3839  * Renames the given dataset.
3840  */
3841 int
3842 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3843     boolean_t force_unmount)
3844 {
3845 	int ret;
3846 	zfs_cmd_t zc = { 0 };
3847 	char *delim;
3848 	prop_changelist_t *cl = NULL;
3849 	zfs_handle_t *zhrp = NULL;
3850 	char *parentname = NULL;
3851 	char parent[ZFS_MAXNAMELEN];
3852 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3853 	char errbuf[1024];
3854 
3855 	/* if we have the same exact name, just return success */
3856 	if (strcmp(zhp->zfs_name, target) == 0)
3857 		return (0);
3858 
3859 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3860 	    "cannot rename to '%s'"), target);
3861 
3862 	/*
3863 	 * Make sure the target name is valid
3864 	 */
3865 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3866 		if ((strchr(target, '@') == NULL) ||
3867 		    *target == '@') {
3868 			/*
3869 			 * Snapshot target name is abbreviated,
3870 			 * reconstruct full dataset name
3871 			 */
3872 			(void) strlcpy(parent, zhp->zfs_name,
3873 			    sizeof (parent));
3874 			delim = strchr(parent, '@');
3875 			if (strchr(target, '@') == NULL)
3876 				*(++delim) = '\0';
3877 			else
3878 				*delim = '\0';
3879 			(void) strlcat(parent, target, sizeof (parent));
3880 			target = parent;
3881 		} else {
3882 			/*
3883 			 * Make sure we're renaming within the same dataset.
3884 			 */
3885 			delim = strchr(target, '@');
3886 			if (strncmp(zhp->zfs_name, target, delim - target)
3887 			    != 0 || zhp->zfs_name[delim - target] != '@') {
3888 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3889 				    "snapshots must be part of same "
3890 				    "dataset"));
3891 				return (zfs_error(hdl, EZFS_CROSSTARGET,
3892 				    errbuf));
3893 			}
3894 		}
3895 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3896 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3897 	} else {
3898 		if (recursive) {
3899 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3900 			    "recursive rename must be a snapshot"));
3901 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3902 		}
3903 
3904 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3905 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3906 
3907 		/* validate parents */
3908 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3909 			return (-1);
3910 
3911 		/* make sure we're in the same pool */
3912 		verify((delim = strchr(target, '/')) != NULL);
3913 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3914 		    zhp->zfs_name[delim - target] != '/') {
3915 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3916 			    "datasets must be within same pool"));
3917 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3918 		}
3919 
3920 		/* new name cannot be a child of the current dataset name */
3921 		if (is_descendant(zhp->zfs_name, target)) {
3922 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3923 			    "New dataset name cannot be a descendant of "
3924 			    "current dataset name"));
3925 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3926 		}
3927 	}
3928 
3929 	(void) snprintf(errbuf, sizeof (errbuf),
3930 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3931 
3932 	if (getzoneid() == GLOBAL_ZONEID &&
3933 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3934 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3935 		    "dataset is used in a non-global zone"));
3936 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3937 	}
3938 
3939 	if (recursive) {
3940 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3941 		if (parentname == NULL) {
3942 			ret = -1;
3943 			goto error;
3944 		}
3945 		delim = strchr(parentname, '@');
3946 		*delim = '\0';
3947 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3948 		if (zhrp == NULL) {
3949 			ret = -1;
3950 			goto error;
3951 		}
3952 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
3953 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3954 		    force_unmount ? MS_FORCE : 0)) == NULL)
3955 			return (-1);
3956 
3957 		if (changelist_haszonedchild(cl)) {
3958 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3959 			    "child dataset with inherited mountpoint is used "
3960 			    "in a non-global zone"));
3961 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
3962 			goto error;
3963 		}
3964 
3965 		if ((ret = changelist_prefix(cl)) != 0)
3966 			goto error;
3967 	}
3968 
3969 	if (ZFS_IS_VOLUME(zhp))
3970 		zc.zc_objset_type = DMU_OST_ZVOL;
3971 	else
3972 		zc.zc_objset_type = DMU_OST_ZFS;
3973 
3974 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3975 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3976 
3977 	zc.zc_cookie = recursive;
3978 
3979 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3980 		/*
3981 		 * if it was recursive, the one that actually failed will
3982 		 * be in zc.zc_name
3983 		 */
3984 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3985 		    "cannot rename '%s'"), zc.zc_name);
3986 
3987 		if (recursive && errno == EEXIST) {
3988 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3989 			    "a child dataset already has a snapshot "
3990 			    "with the new name"));
3991 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3992 		} else {
3993 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3994 		}
3995 
3996 		/*
3997 		 * On failure, we still want to remount any filesystems that
3998 		 * were previously mounted, so we don't alter the system state.
3999 		 */
4000 		if (cl != NULL)
4001 			(void) changelist_postfix(cl);
4002 	} else {
4003 		if (cl != NULL) {
4004 			changelist_rename(cl, zfs_get_name(zhp), target);
4005 			ret = changelist_postfix(cl);
4006 		}
4007 	}
4008 
4009 error:
4010 	if (parentname != NULL) {
4011 		free(parentname);
4012 	}
4013 	if (zhrp != NULL) {
4014 		zfs_close(zhrp);
4015 	}
4016 	if (cl != NULL) {
4017 		changelist_free(cl);
4018 	}
4019 	return (ret);
4020 }
4021 
4022 nvlist_t *
4023 zfs_get_user_props(zfs_handle_t *zhp)
4024 {
4025 	return (zhp->zfs_user_props);
4026 }
4027 
4028 nvlist_t *
4029 zfs_get_recvd_props(zfs_handle_t *zhp)
4030 {
4031 	if (zhp->zfs_recvd_props == NULL)
4032 		if (get_recvd_props_ioctl(zhp) != 0)
4033 			return (NULL);
4034 	return (zhp->zfs_recvd_props);
4035 }
4036 
4037 /*
4038  * This function is used by 'zfs list' to determine the exact set of columns to
4039  * display, and their maximum widths.  This does two main things:
4040  *
4041  *      - If this is a list of all properties, then expand the list to include
4042  *        all native properties, and set a flag so that for each dataset we look
4043  *        for new unique user properties and add them to the list.
4044  *
4045  *      - For non fixed-width properties, keep track of the maximum width seen
4046  *        so that we can size the column appropriately. If the user has
4047  *        requested received property values, we also need to compute the width
4048  *        of the RECEIVED column.
4049  */
4050 int
4051 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4052     boolean_t literal)
4053 {
4054 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4055 	zprop_list_t *entry;
4056 	zprop_list_t **last, **start;
4057 	nvlist_t *userprops, *propval;
4058 	nvpair_t *elem;
4059 	char *strval;
4060 	char buf[ZFS_MAXPROPLEN];
4061 
4062 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4063 		return (-1);
4064 
4065 	userprops = zfs_get_user_props(zhp);
4066 
4067 	entry = *plp;
4068 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4069 		/*
4070 		 * Go through and add any user properties as necessary.  We
4071 		 * start by incrementing our list pointer to the first
4072 		 * non-native property.
4073 		 */
4074 		start = plp;
4075 		while (*start != NULL) {
4076 			if ((*start)->pl_prop == ZPROP_INVAL)
4077 				break;
4078 			start = &(*start)->pl_next;
4079 		}
4080 
4081 		elem = NULL;
4082 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4083 			/*
4084 			 * See if we've already found this property in our list.
4085 			 */
4086 			for (last = start; *last != NULL;
4087 			    last = &(*last)->pl_next) {
4088 				if (strcmp((*last)->pl_user_prop,
4089 				    nvpair_name(elem)) == 0)
4090 					break;
4091 			}
4092 
4093 			if (*last == NULL) {
4094 				if ((entry = zfs_alloc(hdl,
4095 				    sizeof (zprop_list_t))) == NULL ||
4096 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4097 				    nvpair_name(elem)))) == NULL) {
4098 					free(entry);
4099 					return (-1);
4100 				}
4101 
4102 				entry->pl_prop = ZPROP_INVAL;
4103 				entry->pl_width = strlen(nvpair_name(elem));
4104 				entry->pl_all = B_TRUE;
4105 				*last = entry;
4106 			}
4107 		}
4108 	}
4109 
4110 	/*
4111 	 * Now go through and check the width of any non-fixed columns
4112 	 */
4113 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4114 		if (entry->pl_fixed && !literal)
4115 			continue;
4116 
4117 		if (entry->pl_prop != ZPROP_INVAL) {
4118 			if (zfs_prop_get(zhp, entry->pl_prop,
4119 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4120 				if (strlen(buf) > entry->pl_width)
4121 					entry->pl_width = strlen(buf);
4122 			}
4123 			if (received && zfs_prop_get_recvd(zhp,
4124 			    zfs_prop_to_name(entry->pl_prop),
4125 			    buf, sizeof (buf), literal) == 0)
4126 				if (strlen(buf) > entry->pl_recvd_width)
4127 					entry->pl_recvd_width = strlen(buf);
4128 		} else {
4129 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4130 			    &propval) == 0) {
4131 				verify(nvlist_lookup_string(propval,
4132 				    ZPROP_VALUE, &strval) == 0);
4133 				if (strlen(strval) > entry->pl_width)
4134 					entry->pl_width = strlen(strval);
4135 			}
4136 			if (received && zfs_prop_get_recvd(zhp,
4137 			    entry->pl_user_prop,
4138 			    buf, sizeof (buf), literal) == 0)
4139 				if (strlen(buf) > entry->pl_recvd_width)
4140 					entry->pl_recvd_width = strlen(buf);
4141 		}
4142 	}
4143 
4144 	return (0);
4145 }
4146 
4147 int
4148 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4149     char *resource, void *export, void *sharetab,
4150     int sharemax, zfs_share_op_t operation)
4151 {
4152 	zfs_cmd_t zc = { 0 };
4153 	int error;
4154 
4155 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4156 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4157 	if (resource)
4158 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4159 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4160 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4161 	zc.zc_share.z_sharetype = operation;
4162 	zc.zc_share.z_sharemax = sharemax;
4163 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4164 	return (error);
4165 }
4166 
4167 void
4168 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4169 {
4170 	nvpair_t *curr;
4171 
4172 	/*
4173 	 * Keep a reference to the props-table against which we prune the
4174 	 * properties.
4175 	 */
4176 	zhp->zfs_props_table = props;
4177 
4178 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4179 
4180 	while (curr) {
4181 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4182 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4183 
4184 		/*
4185 		 * User properties will result in ZPROP_INVAL, and since we
4186 		 * only know how to prune standard ZFS properties, we always
4187 		 * leave these in the list.  This can also happen if we
4188 		 * encounter an unknown DSL property (when running older
4189 		 * software, for example).
4190 		 */
4191 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4192 			(void) nvlist_remove(zhp->zfs_props,
4193 			    nvpair_name(curr), nvpair_type(curr));
4194 		curr = next;
4195 	}
4196 }
4197 
4198 static int
4199 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4200     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4201 {
4202 	zfs_cmd_t zc = { 0 };
4203 	nvlist_t *nvlist = NULL;
4204 	int error;
4205 
4206 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4207 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4208 	zc.zc_cookie = (uint64_t)cmd;
4209 
4210 	if (cmd == ZFS_SMB_ACL_RENAME) {
4211 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4212 			(void) no_memory(hdl);
4213 			return (0);
4214 		}
4215 	}
4216 
4217 	switch (cmd) {
4218 	case ZFS_SMB_ACL_ADD:
4219 	case ZFS_SMB_ACL_REMOVE:
4220 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4221 		break;
4222 	case ZFS_SMB_ACL_RENAME:
4223 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4224 		    resource1) != 0) {
4225 				(void) no_memory(hdl);
4226 				return (-1);
4227 		}
4228 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4229 		    resource2) != 0) {
4230 				(void) no_memory(hdl);
4231 				return (-1);
4232 		}
4233 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4234 			nvlist_free(nvlist);
4235 			return (-1);
4236 		}
4237 		break;
4238 	case ZFS_SMB_ACL_PURGE:
4239 		break;
4240 	default:
4241 		return (-1);
4242 	}
4243 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4244 	if (nvlist)
4245 		nvlist_free(nvlist);
4246 	return (error);
4247 }
4248 
4249 int
4250 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4251     char *path, char *resource)
4252 {
4253 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4254 	    resource, NULL));
4255 }
4256 
4257 int
4258 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4259     char *path, char *resource)
4260 {
4261 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4262 	    resource, NULL));
4263 }
4264 
4265 int
4266 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4267 {
4268 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4269 	    NULL, NULL));
4270 }
4271 
4272 int
4273 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4274     char *oldname, char *newname)
4275 {
4276 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4277 	    oldname, newname));
4278 }
4279 
4280 int
4281 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4282     zfs_userspace_cb_t func, void *arg)
4283 {
4284 	zfs_cmd_t zc = { 0 };
4285 	zfs_useracct_t buf[100];
4286 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4287 	int ret;
4288 
4289 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4290 
4291 	zc.zc_objset_type = type;
4292 	zc.zc_nvlist_dst = (uintptr_t)buf;
4293 
4294 	for (;;) {
4295 		zfs_useracct_t *zua = buf;
4296 
4297 		zc.zc_nvlist_dst_size = sizeof (buf);
4298 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4299 			char errbuf[1024];
4300 
4301 			(void) snprintf(errbuf, sizeof (errbuf),
4302 			    dgettext(TEXT_DOMAIN,
4303 			    "cannot get used/quota for %s"), zc.zc_name);
4304 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4305 		}
4306 		if (zc.zc_nvlist_dst_size == 0)
4307 			break;
4308 
4309 		while (zc.zc_nvlist_dst_size > 0) {
4310 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4311 			    zua->zu_space)) != 0)
4312 				return (ret);
4313 			zua++;
4314 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4315 		}
4316 	}
4317 
4318 	return (0);
4319 }
4320 
4321 struct holdarg {
4322 	nvlist_t *nvl;
4323 	const char *snapname;
4324 	const char *tag;
4325 	boolean_t recursive;
4326 	int error;
4327 };
4328 
4329 static int
4330 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4331 {
4332 	struct holdarg *ha = arg;
4333 	char name[ZFS_MAXNAMELEN];
4334 	int rv = 0;
4335 
4336 	(void) snprintf(name, sizeof (name),
4337 	    "%s@%s", zhp->zfs_name, ha->snapname);
4338 
4339 	if (lzc_exists(name))
4340 		fnvlist_add_string(ha->nvl, name, ha->tag);
4341 
4342 	if (ha->recursive)
4343 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4344 	zfs_close(zhp);
4345 	return (rv);
4346 }
4347 
4348 int
4349 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4350     boolean_t recursive, int cleanup_fd)
4351 {
4352 	int ret;
4353 	struct holdarg ha;
4354 
4355 	ha.nvl = fnvlist_alloc();
4356 	ha.snapname = snapname;
4357 	ha.tag = tag;
4358 	ha.recursive = recursive;
4359 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4360 
4361 	if (nvlist_empty(ha.nvl)) {
4362 		char errbuf[1024];
4363 
4364 		fnvlist_free(ha.nvl);
4365 		ret = ENOENT;
4366 		(void) snprintf(errbuf, sizeof (errbuf),
4367 		    dgettext(TEXT_DOMAIN,
4368 		    "cannot hold snapshot '%s@%s'"),
4369 		    zhp->zfs_name, snapname);
4370 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4371 		return (ret);
4372 	}
4373 
4374 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4375 	fnvlist_free(ha.nvl);
4376 
4377 	return (ret);
4378 }
4379 
4380 int
4381 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4382 {
4383 	int ret;
4384 	nvlist_t *errors;
4385 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4386 	char errbuf[1024];
4387 	nvpair_t *elem;
4388 
4389 	errors = NULL;
4390 	ret = lzc_hold(holds, cleanup_fd, &errors);
4391 
4392 	if (ret == 0) {
4393 		/* There may be errors even in the success case. */
4394 		fnvlist_free(errors);
4395 		return (0);
4396 	}
4397 
4398 	if (nvlist_empty(errors)) {
4399 		/* no hold-specific errors */
4400 		(void) snprintf(errbuf, sizeof (errbuf),
4401 		    dgettext(TEXT_DOMAIN, "cannot hold"));
4402 		switch (ret) {
4403 		case ENOTSUP:
4404 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4405 			    "pool must be upgraded"));
4406 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4407 			break;
4408 		case EINVAL:
4409 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4410 			break;
4411 		default:
4412 			(void) zfs_standard_error(hdl, ret, errbuf);
4413 		}
4414 	}
4415 
4416 	for (elem = nvlist_next_nvpair(errors, NULL);
4417 	    elem != NULL;
4418 	    elem = nvlist_next_nvpair(errors, elem)) {
4419 		(void) snprintf(errbuf, sizeof (errbuf),
4420 		    dgettext(TEXT_DOMAIN,
4421 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
4422 		switch (fnvpair_value_int32(elem)) {
4423 		case E2BIG:
4424 			/*
4425 			 * Temporary tags wind up having the ds object id
4426 			 * prepended. So even if we passed the length check
4427 			 * above, it's still possible for the tag to wind
4428 			 * up being slightly too long.
4429 			 */
4430 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4431 			break;
4432 		case EINVAL:
4433 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4434 			break;
4435 		case EEXIST:
4436 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4437 			break;
4438 		default:
4439 			(void) zfs_standard_error(hdl,
4440 			    fnvpair_value_int32(elem), errbuf);
4441 		}
4442 	}
4443 
4444 	fnvlist_free(errors);
4445 	return (ret);
4446 }
4447 
4448 static int
4449 zfs_release_one(zfs_handle_t *zhp, void *arg)
4450 {
4451 	struct holdarg *ha = arg;
4452 	char name[ZFS_MAXNAMELEN];
4453 	int rv = 0;
4454 	nvlist_t *existing_holds;
4455 
4456 	(void) snprintf(name, sizeof (name),
4457 	    "%s@%s", zhp->zfs_name, ha->snapname);
4458 
4459 	if (lzc_get_holds(name, &existing_holds) != 0) {
4460 		ha->error = ENOENT;
4461 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4462 		ha->error = ESRCH;
4463 	} else {
4464 		nvlist_t *torelease = fnvlist_alloc();
4465 		fnvlist_add_boolean(torelease, ha->tag);
4466 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4467 		fnvlist_free(torelease);
4468 	}
4469 
4470 	if (ha->recursive)
4471 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4472 	zfs_close(zhp);
4473 	return (rv);
4474 }
4475 
4476 int
4477 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4478     boolean_t recursive)
4479 {
4480 	int ret;
4481 	struct holdarg ha;
4482 	nvlist_t *errors = NULL;
4483 	nvpair_t *elem;
4484 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4485 	char errbuf[1024];
4486 
4487 	ha.nvl = fnvlist_alloc();
4488 	ha.snapname = snapname;
4489 	ha.tag = tag;
4490 	ha.recursive = recursive;
4491 	ha.error = 0;
4492 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4493 
4494 	if (nvlist_empty(ha.nvl)) {
4495 		fnvlist_free(ha.nvl);
4496 		ret = ha.error;
4497 		(void) snprintf(errbuf, sizeof (errbuf),
4498 		    dgettext(TEXT_DOMAIN,
4499 		    "cannot release hold from snapshot '%s@%s'"),
4500 		    zhp->zfs_name, snapname);
4501 		if (ret == ESRCH) {
4502 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4503 		} else {
4504 			(void) zfs_standard_error(hdl, ret, errbuf);
4505 		}
4506 		return (ret);
4507 	}
4508 
4509 	ret = lzc_release(ha.nvl, &errors);
4510 	fnvlist_free(ha.nvl);
4511 
4512 	if (ret == 0) {
4513 		/* There may be errors even in the success case. */
4514 		fnvlist_free(errors);
4515 		return (0);
4516 	}
4517 
4518 	if (nvlist_empty(errors)) {
4519 		/* no hold-specific errors */
4520 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4521 		    "cannot release"));
4522 		switch (errno) {
4523 		case ENOTSUP:
4524 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4525 			    "pool must be upgraded"));
4526 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4527 			break;
4528 		default:
4529 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4530 		}
4531 	}
4532 
4533 	for (elem = nvlist_next_nvpair(errors, NULL);
4534 	    elem != NULL;
4535 	    elem = nvlist_next_nvpair(errors, elem)) {
4536 		(void) snprintf(errbuf, sizeof (errbuf),
4537 		    dgettext(TEXT_DOMAIN,
4538 		    "cannot release hold from snapshot '%s'"),
4539 		    nvpair_name(elem));
4540 		switch (fnvpair_value_int32(elem)) {
4541 		case ESRCH:
4542 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4543 			break;
4544 		case EINVAL:
4545 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4546 			break;
4547 		default:
4548 			(void) zfs_standard_error_fmt(hdl,
4549 			    fnvpair_value_int32(elem), errbuf);
4550 		}
4551 	}
4552 
4553 	fnvlist_free(errors);
4554 	return (ret);
4555 }
4556 
4557 int
4558 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4559 {
4560 	zfs_cmd_t zc = { 0 };
4561 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4562 	int nvsz = 2048;
4563 	void *nvbuf;
4564 	int err = 0;
4565 	char errbuf[1024];
4566 
4567 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4568 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4569 
4570 tryagain:
4571 
4572 	nvbuf = malloc(nvsz);
4573 	if (nvbuf == NULL) {
4574 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4575 		goto out;
4576 	}
4577 
4578 	zc.zc_nvlist_dst_size = nvsz;
4579 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4580 
4581 	(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4582 
4583 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4584 		(void) snprintf(errbuf, sizeof (errbuf),
4585 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4586 		    zc.zc_name);
4587 		switch (errno) {
4588 		case ENOMEM:
4589 			free(nvbuf);
4590 			nvsz = zc.zc_nvlist_dst_size;
4591 			goto tryagain;
4592 
4593 		case ENOTSUP:
4594 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4595 			    "pool must be upgraded"));
4596 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4597 			break;
4598 		case EINVAL:
4599 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4600 			break;
4601 		case ENOENT:
4602 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4603 			break;
4604 		default:
4605 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4606 			break;
4607 		}
4608 	} else {
4609 		/* success */
4610 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4611 		if (rc) {
4612 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
4613 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
4614 			    zc.zc_name);
4615 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
4616 		}
4617 	}
4618 
4619 	free(nvbuf);
4620 out:
4621 	return (err);
4622 }
4623 
4624 int
4625 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4626 {
4627 	zfs_cmd_t zc = { 0 };
4628 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4629 	char *nvbuf;
4630 	char errbuf[1024];
4631 	size_t nvsz;
4632 	int err;
4633 
4634 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4635 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4636 
4637 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4638 	assert(err == 0);
4639 
4640 	nvbuf = malloc(nvsz);
4641 
4642 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4643 	assert(err == 0);
4644 
4645 	zc.zc_nvlist_src_size = nvsz;
4646 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
4647 	zc.zc_perm_action = un;
4648 
4649 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4650 
4651 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4652 		(void) snprintf(errbuf, sizeof (errbuf),
4653 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4654 		    zc.zc_name);
4655 		switch (errno) {
4656 		case ENOTSUP:
4657 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4658 			    "pool must be upgraded"));
4659 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4660 			break;
4661 		case EINVAL:
4662 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4663 			break;
4664 		case ENOENT:
4665 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4666 			break;
4667 		default:
4668 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4669 			break;
4670 		}
4671 	}
4672 
4673 	free(nvbuf);
4674 
4675 	return (err);
4676 }
4677 
4678 int
4679 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4680 {
4681 	int err;
4682 	char errbuf[1024];
4683 
4684 	err = lzc_get_holds(zhp->zfs_name, nvl);
4685 
4686 	if (err != 0) {
4687 		libzfs_handle_t *hdl = zhp->zfs_hdl;
4688 
4689 		(void) snprintf(errbuf, sizeof (errbuf),
4690 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4691 		    zhp->zfs_name);
4692 		switch (err) {
4693 		case ENOTSUP:
4694 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4695 			    "pool must be upgraded"));
4696 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4697 			break;
4698 		case EINVAL:
4699 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4700 			break;
4701 		case ENOENT:
4702 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4703 			break;
4704 		default:
4705 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4706 			break;
4707 		}
4708 	}
4709 
4710 	return (err);
4711 }
4712 
4713 /*
4714  * Convert the zvol's volume size to an appropriate reservation.
4715  * Note: If this routine is updated, it is necessary to update the ZFS test
4716  * suite's shell version in reservation.kshlib.
4717  */
4718 uint64_t
4719 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4720 {
4721 	uint64_t numdb;
4722 	uint64_t nblocks, volblocksize;
4723 	int ncopies;
4724 	char *strval;
4725 
4726 	if (nvlist_lookup_string(props,
4727 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4728 		ncopies = atoi(strval);
4729 	else
4730 		ncopies = 1;
4731 	if (nvlist_lookup_uint64(props,
4732 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4733 	    &volblocksize) != 0)
4734 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4735 	nblocks = volsize/volblocksize;
4736 	/* start with metadnode L0-L6 */
4737 	numdb = 7;
4738 	/* calculate number of indirects */
4739 	while (nblocks > 1) {
4740 		nblocks += DNODES_PER_LEVEL - 1;
4741 		nblocks /= DNODES_PER_LEVEL;
4742 		numdb += nblocks;
4743 	}
4744 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4745 	volsize *= ncopies;
4746 	/*
4747 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4748 	 * compressed, but in practice they compress down to about
4749 	 * 1100 bytes
4750 	 */
4751 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4752 	volsize += numdb;
4753 	return (volsize);
4754 }
4755