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