xref: /titanic_52/usr/src/lib/libzfs/common/libzfs_dataset.c (revision d1a17b3d9a7284d357ac39056f95a18d95106206)
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_FOLLOW:
1992 		mntopt_on = MNTOPT_FOLLOW;
1993 		mntopt_off = MNTOPT_NOFOLLOW;
1994 		break;
1995 
1996 	case ZFS_PROP_READONLY:
1997 		mntopt_on = MNTOPT_RO;
1998 		mntopt_off = MNTOPT_RW;
1999 		break;
2000 
2001 	case ZFS_PROP_SETUID:
2002 		mntopt_on = MNTOPT_SETUID;
2003 		mntopt_off = MNTOPT_NOSETUID;
2004 		break;
2005 
2006 	case ZFS_PROP_XATTR:
2007 		mntopt_on = MNTOPT_XATTR;
2008 		mntopt_off = MNTOPT_NOXATTR;
2009 		break;
2010 
2011 	case ZFS_PROP_NBMAND:
2012 		mntopt_on = MNTOPT_NBMAND;
2013 		mntopt_off = MNTOPT_NONBMAND;
2014 		break;
2015 	}
2016 
2017 	/*
2018 	 * Because looking up the mount options is potentially expensive
2019 	 * (iterating over all of /etc/mnttab), we defer its calculation until
2020 	 * we're looking up a property which requires its presence.
2021 	 */
2022 	if (!zhp->zfs_mntcheck &&
2023 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2024 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2025 		struct mnttab entry;
2026 
2027 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2028 			zhp->zfs_mntopts = zfs_strdup(hdl,
2029 			    entry.mnt_mntopts);
2030 			if (zhp->zfs_mntopts == NULL)
2031 				return (-1);
2032 		}
2033 
2034 		zhp->zfs_mntcheck = B_TRUE;
2035 	}
2036 
2037 	if (zhp->zfs_mntopts == NULL)
2038 		mnt.mnt_mntopts = "";
2039 	else
2040 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2041 
2042 	switch (prop) {
2043 	case ZFS_PROP_ATIME:
2044 	case ZFS_PROP_DEVICES:
2045 	case ZFS_PROP_EXEC:
2046 	case ZFS_PROP_FOLLOW:
2047 	case ZFS_PROP_READONLY:
2048 	case ZFS_PROP_SETUID:
2049 	case ZFS_PROP_XATTR:
2050 	case ZFS_PROP_NBMAND:
2051 		*val = getprop_uint64(zhp, prop, source);
2052 
2053 		if (received)
2054 			break;
2055 
2056 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
2057 			*val = B_TRUE;
2058 			if (src)
2059 				*src = ZPROP_SRC_TEMPORARY;
2060 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
2061 			*val = B_FALSE;
2062 			if (src)
2063 				*src = ZPROP_SRC_TEMPORARY;
2064 		}
2065 		break;
2066 
2067 	case ZFS_PROP_CANMOUNT:
2068 	case ZFS_PROP_VOLSIZE:
2069 	case ZFS_PROP_QUOTA:
2070 	case ZFS_PROP_REFQUOTA:
2071 	case ZFS_PROP_RESERVATION:
2072 	case ZFS_PROP_REFRESERVATION:
2073 	case ZFS_PROP_FILESYSTEM_LIMIT:
2074 	case ZFS_PROP_SNAPSHOT_LIMIT:
2075 	case ZFS_PROP_FILESYSTEM_COUNT:
2076 	case ZFS_PROP_SNAPSHOT_COUNT:
2077 		*val = getprop_uint64(zhp, prop, source);
2078 
2079 		if (*source == NULL) {
2080 			/* not default, must be local */
2081 			*source = zhp->zfs_name;
2082 		}
2083 		break;
2084 
2085 	case ZFS_PROP_MOUNTED:
2086 		*val = (zhp->zfs_mntopts != NULL);
2087 		break;
2088 
2089 	case ZFS_PROP_NUMCLONES:
2090 		*val = zhp->zfs_dmustats.dds_num_clones;
2091 		break;
2092 
2093 	case ZFS_PROP_VERSION:
2094 	case ZFS_PROP_NORMALIZE:
2095 	case ZFS_PROP_UTF8ONLY:
2096 	case ZFS_PROP_CASE:
2097 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2098 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2099 			return (-1);
2100 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2101 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2102 			zcmd_free_nvlists(&zc);
2103 			return (-1);
2104 		}
2105 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2106 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2107 		    val) != 0) {
2108 			zcmd_free_nvlists(&zc);
2109 			return (-1);
2110 		}
2111 		nvlist_free(zplprops);
2112 		zcmd_free_nvlists(&zc);
2113 		break;
2114 
2115 	case ZFS_PROP_INCONSISTENT:
2116 		*val = zhp->zfs_dmustats.dds_inconsistent;
2117 		break;
2118 
2119 	default:
2120 		switch (zfs_prop_get_type(prop)) {
2121 		case PROP_TYPE_NUMBER:
2122 		case PROP_TYPE_INDEX:
2123 			*val = getprop_uint64(zhp, prop, source);
2124 			/*
2125 			 * If we tried to use a default value for a
2126 			 * readonly property, it means that it was not
2127 			 * present.
2128 			 */
2129 			if (zfs_prop_readonly(prop) &&
2130 			    *source != NULL && (*source)[0] == '\0') {
2131 				*source = NULL;
2132 			}
2133 			break;
2134 
2135 		case PROP_TYPE_STRING:
2136 		default:
2137 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2138 			    "cannot get non-numeric property"));
2139 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2140 			    dgettext(TEXT_DOMAIN, "internal error")));
2141 		}
2142 	}
2143 
2144 	return (0);
2145 }
2146 
2147 /*
2148  * Calculate the source type, given the raw source string.
2149  */
2150 static void
2151 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2152     char *statbuf, size_t statlen)
2153 {
2154 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2155 		return;
2156 
2157 	if (source == NULL) {
2158 		*srctype = ZPROP_SRC_NONE;
2159 	} else if (source[0] == '\0') {
2160 		*srctype = ZPROP_SRC_DEFAULT;
2161 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2162 		*srctype = ZPROP_SRC_RECEIVED;
2163 	} else {
2164 		if (strcmp(source, zhp->zfs_name) == 0) {
2165 			*srctype = ZPROP_SRC_LOCAL;
2166 		} else {
2167 			(void) strlcpy(statbuf, source, statlen);
2168 			*srctype = ZPROP_SRC_INHERITED;
2169 		}
2170 	}
2171 
2172 }
2173 
2174 int
2175 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2176     size_t proplen, boolean_t literal)
2177 {
2178 	zfs_prop_t prop;
2179 	int err = 0;
2180 
2181 	if (zhp->zfs_recvd_props == NULL)
2182 		if (get_recvd_props_ioctl(zhp) != 0)
2183 			return (-1);
2184 
2185 	prop = zfs_name_to_prop(propname);
2186 
2187 	if (prop != ZPROP_INVAL) {
2188 		uint64_t cookie;
2189 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2190 			return (-1);
2191 		zfs_set_recvd_props_mode(zhp, &cookie);
2192 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
2193 		    NULL, NULL, 0, literal);
2194 		zfs_unset_recvd_props_mode(zhp, &cookie);
2195 	} else {
2196 		nvlist_t *propval;
2197 		char *recvdval;
2198 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2199 		    propname, &propval) != 0)
2200 			return (-1);
2201 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2202 		    &recvdval) == 0);
2203 		(void) strlcpy(propbuf, recvdval, proplen);
2204 	}
2205 
2206 	return (err == 0 ? 0 : -1);
2207 }
2208 
2209 static int
2210 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2211 {
2212 	nvlist_t *value;
2213 	nvpair_t *pair;
2214 
2215 	value = zfs_get_clones_nvl(zhp);
2216 	if (value == NULL)
2217 		return (-1);
2218 
2219 	propbuf[0] = '\0';
2220 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2221 	    pair = nvlist_next_nvpair(value, pair)) {
2222 		if (propbuf[0] != '\0')
2223 			(void) strlcat(propbuf, ",", proplen);
2224 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
2225 	}
2226 
2227 	return (0);
2228 }
2229 
2230 struct get_clones_arg {
2231 	uint64_t numclones;
2232 	nvlist_t *value;
2233 	const char *origin;
2234 	char buf[ZFS_MAX_DATASET_NAME_LEN];
2235 };
2236 
2237 int
2238 get_clones_cb(zfs_handle_t *zhp, void *arg)
2239 {
2240 	struct get_clones_arg *gca = arg;
2241 
2242 	if (gca->numclones == 0) {
2243 		zfs_close(zhp);
2244 		return (0);
2245 	}
2246 
2247 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2248 	    NULL, NULL, 0, B_TRUE) != 0)
2249 		goto out;
2250 	if (strcmp(gca->buf, gca->origin) == 0) {
2251 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2252 		gca->numclones--;
2253 	}
2254 
2255 out:
2256 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
2257 	zfs_close(zhp);
2258 	return (0);
2259 }
2260 
2261 nvlist_t *
2262 zfs_get_clones_nvl(zfs_handle_t *zhp)
2263 {
2264 	nvlist_t *nv, *value;
2265 
2266 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2267 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2268 		struct get_clones_arg gca;
2269 
2270 		/*
2271 		 * if this is a snapshot, then the kernel wasn't able
2272 		 * to get the clones.  Do it by slowly iterating.
2273 		 */
2274 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2275 			return (NULL);
2276 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2277 			return (NULL);
2278 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2279 			nvlist_free(nv);
2280 			return (NULL);
2281 		}
2282 
2283 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2284 		gca.value = value;
2285 		gca.origin = zhp->zfs_name;
2286 
2287 		if (gca.numclones != 0) {
2288 			zfs_handle_t *root;
2289 			char pool[ZFS_MAX_DATASET_NAME_LEN];
2290 			char *cp = pool;
2291 
2292 			/* get the pool name */
2293 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2294 			(void) strsep(&cp, "/@");
2295 			root = zfs_open(zhp->zfs_hdl, pool,
2296 			    ZFS_TYPE_FILESYSTEM);
2297 
2298 			(void) get_clones_cb(root, &gca);
2299 		}
2300 
2301 		if (gca.numclones != 0 ||
2302 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2303 		    nvlist_add_nvlist(zhp->zfs_props,
2304 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2305 			nvlist_free(nv);
2306 			nvlist_free(value);
2307 			return (NULL);
2308 		}
2309 		nvlist_free(nv);
2310 		nvlist_free(value);
2311 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2312 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2313 	}
2314 
2315 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2316 
2317 	return (value);
2318 }
2319 
2320 /*
2321  * Retrieve a property from the given object.  If 'literal' is specified, then
2322  * numbers are left as exact values.  Otherwise, numbers are converted to a
2323  * human-readable form.
2324  *
2325  * Returns 0 on success, or -1 on error.
2326  */
2327 int
2328 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2329     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2330 {
2331 	char *source = NULL;
2332 	uint64_t val;
2333 	const char *str;
2334 	const char *strval;
2335 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2336 
2337 	/*
2338 	 * Check to see if this property applies to our object
2339 	 */
2340 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2341 		return (-1);
2342 
2343 	if (received && zfs_prop_readonly(prop))
2344 		return (-1);
2345 
2346 	if (src)
2347 		*src = ZPROP_SRC_NONE;
2348 
2349 	switch (prop) {
2350 	case ZFS_PROP_CREATION:
2351 		/*
2352 		 * 'creation' is a time_t stored in the statistics.  We convert
2353 		 * this into a string unless 'literal' is specified.
2354 		 */
2355 		{
2356 			val = getprop_uint64(zhp, prop, &source);
2357 			time_t time = (time_t)val;
2358 			struct tm t;
2359 
2360 			if (literal ||
2361 			    localtime_r(&time, &t) == NULL ||
2362 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2363 			    &t) == 0)
2364 				(void) snprintf(propbuf, proplen, "%llu", val);
2365 		}
2366 		break;
2367 
2368 	case ZFS_PROP_MOUNTPOINT:
2369 		/*
2370 		 * Getting the precise mountpoint can be tricky.
2371 		 *
2372 		 *  - for 'none' or 'legacy', return those values.
2373 		 *  - for inherited mountpoints, we want to take everything
2374 		 *    after our ancestor and append it to the inherited value.
2375 		 *
2376 		 * If the pool has an alternate root, we want to prepend that
2377 		 * root to any values we return.
2378 		 */
2379 
2380 		str = getprop_string(zhp, prop, &source);
2381 
2382 		if (str[0] == '/') {
2383 			char buf[MAXPATHLEN];
2384 			char *root = buf;
2385 			const char *relpath;
2386 
2387 			/*
2388 			 * If we inherit the mountpoint, even from a dataset
2389 			 * with a received value, the source will be the path of
2390 			 * the dataset we inherit from. If source is
2391 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2392 			 * inherited.
2393 			 */
2394 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2395 				relpath = "";
2396 			} else {
2397 				relpath = zhp->zfs_name + strlen(source);
2398 				if (relpath[0] == '/')
2399 					relpath++;
2400 			}
2401 
2402 			if ((zpool_get_prop(zhp->zpool_hdl,
2403 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2404 			    B_FALSE)) || (strcmp(root, "-") == 0))
2405 				root[0] = '\0';
2406 			/*
2407 			 * Special case an alternate root of '/'. This will
2408 			 * avoid having multiple leading slashes in the
2409 			 * mountpoint path.
2410 			 */
2411 			if (strcmp(root, "/") == 0)
2412 				root++;
2413 
2414 			/*
2415 			 * If the mountpoint is '/' then skip over this
2416 			 * if we are obtaining either an alternate root or
2417 			 * an inherited mountpoint.
2418 			 */
2419 			if (str[1] == '\0' && (root[0] != '\0' ||
2420 			    relpath[0] != '\0'))
2421 				str++;
2422 
2423 			if (relpath[0] == '\0')
2424 				(void) snprintf(propbuf, proplen, "%s%s",
2425 				    root, str);
2426 			else
2427 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
2428 				    root, str, relpath[0] == '@' ? "" : "/",
2429 				    relpath);
2430 		} else {
2431 			/* 'legacy' or 'none' */
2432 			(void) strlcpy(propbuf, str, proplen);
2433 		}
2434 
2435 		break;
2436 
2437 	case ZFS_PROP_ORIGIN:
2438 		str = getprop_string(zhp, prop, &source);
2439 		if (str == NULL)
2440 			return (-1);
2441 		(void) strlcpy(propbuf, str, proplen);
2442 		break;
2443 
2444 	case ZFS_PROP_CLONES:
2445 		if (get_clones_string(zhp, propbuf, proplen) != 0)
2446 			return (-1);
2447 		break;
2448 
2449 	case ZFS_PROP_QUOTA:
2450 	case ZFS_PROP_REFQUOTA:
2451 	case ZFS_PROP_RESERVATION:
2452 	case ZFS_PROP_REFRESERVATION:
2453 
2454 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2455 			return (-1);
2456 
2457 		/*
2458 		 * If quota or reservation is 0, we translate this into 'none'
2459 		 * (unless literal is set), and indicate that it's the default
2460 		 * value.  Otherwise, we print the number nicely and indicate
2461 		 * that its set locally.
2462 		 */
2463 		if (val == 0) {
2464 			if (literal)
2465 				(void) strlcpy(propbuf, "0", proplen);
2466 			else
2467 				(void) strlcpy(propbuf, "none", proplen);
2468 		} else {
2469 			if (literal)
2470 				(void) snprintf(propbuf, proplen, "%llu",
2471 				    (u_longlong_t)val);
2472 			else
2473 				zfs_nicenum(val, propbuf, proplen);
2474 		}
2475 		break;
2476 
2477 	case ZFS_PROP_FILESYSTEM_LIMIT:
2478 	case ZFS_PROP_SNAPSHOT_LIMIT:
2479 	case ZFS_PROP_FILESYSTEM_COUNT:
2480 	case ZFS_PROP_SNAPSHOT_COUNT:
2481 
2482 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2483 			return (-1);
2484 
2485 		/*
2486 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2487 		 * literal is set), and indicate that it's the default value.
2488 		 * Otherwise, we print the number nicely and indicate that it's
2489 		 * set locally.
2490 		 */
2491 		if (literal) {
2492 			(void) snprintf(propbuf, proplen, "%llu",
2493 			    (u_longlong_t)val);
2494 		} else if (val == UINT64_MAX) {
2495 			(void) strlcpy(propbuf, "none", proplen);
2496 		} else {
2497 			zfs_nicenum(val, propbuf, proplen);
2498 		}
2499 		break;
2500 
2501 	case ZFS_PROP_REFRATIO:
2502 	case ZFS_PROP_COMPRESSRATIO:
2503 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2504 			return (-1);
2505 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2506 		    (u_longlong_t)(val / 100),
2507 		    (u_longlong_t)(val % 100));
2508 		break;
2509 
2510 	case ZFS_PROP_TYPE:
2511 		switch (zhp->zfs_type) {
2512 		case ZFS_TYPE_FILESYSTEM:
2513 			str = "filesystem";
2514 			break;
2515 		case ZFS_TYPE_VOLUME:
2516 			str = "volume";
2517 			break;
2518 		case ZFS_TYPE_SNAPSHOT:
2519 			str = "snapshot";
2520 			break;
2521 		case ZFS_TYPE_BOOKMARK:
2522 			str = "bookmark";
2523 			break;
2524 		default:
2525 			abort();
2526 		}
2527 		(void) snprintf(propbuf, proplen, "%s", str);
2528 		break;
2529 
2530 	case ZFS_PROP_MOUNTED:
2531 		/*
2532 		 * The 'mounted' property is a pseudo-property that described
2533 		 * whether the filesystem is currently mounted.  Even though
2534 		 * it's a boolean value, the typical values of "on" and "off"
2535 		 * don't make sense, so we translate to "yes" and "no".
2536 		 */
2537 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2538 		    src, &source, &val) != 0)
2539 			return (-1);
2540 		if (val)
2541 			(void) strlcpy(propbuf, "yes", proplen);
2542 		else
2543 			(void) strlcpy(propbuf, "no", proplen);
2544 		break;
2545 
2546 	case ZFS_PROP_NAME:
2547 		/*
2548 		 * The 'name' property is a pseudo-property derived from the
2549 		 * dataset name.  It is presented as a real property to simplify
2550 		 * consumers.
2551 		 */
2552 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2553 		break;
2554 
2555 	case ZFS_PROP_MLSLABEL:
2556 		{
2557 			m_label_t *new_sl = NULL;
2558 			char *ascii = NULL;	/* human readable label */
2559 
2560 			(void) strlcpy(propbuf,
2561 			    getprop_string(zhp, prop, &source), proplen);
2562 
2563 			if (literal || (strcasecmp(propbuf,
2564 			    ZFS_MLSLABEL_DEFAULT) == 0))
2565 				break;
2566 
2567 			/*
2568 			 * Try to translate the internal hex string to
2569 			 * human-readable output.  If there are any
2570 			 * problems just use the hex string.
2571 			 */
2572 
2573 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2574 			    L_NO_CORRECTION, NULL) == -1) {
2575 				m_label_free(new_sl);
2576 				break;
2577 			}
2578 
2579 			if (label_to_str(new_sl, &ascii, M_LABEL,
2580 			    DEF_NAMES) != 0) {
2581 				if (ascii)
2582 					free(ascii);
2583 				m_label_free(new_sl);
2584 				break;
2585 			}
2586 			m_label_free(new_sl);
2587 
2588 			(void) strlcpy(propbuf, ascii, proplen);
2589 			free(ascii);
2590 		}
2591 		break;
2592 
2593 	case ZFS_PROP_GUID:
2594 		/*
2595 		 * GUIDs are stored as numbers, but they are identifiers.
2596 		 * We don't want them to be pretty printed, because pretty
2597 		 * printing mangles the ID into a truncated and useless value.
2598 		 */
2599 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2600 			return (-1);
2601 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2602 		break;
2603 
2604 	default:
2605 		switch (zfs_prop_get_type(prop)) {
2606 		case PROP_TYPE_NUMBER:
2607 			if (get_numeric_property(zhp, prop, src,
2608 			    &source, &val) != 0)
2609 				return (-1);
2610 			if (literal)
2611 				(void) snprintf(propbuf, proplen, "%llu",
2612 				    (u_longlong_t)val);
2613 			else
2614 				zfs_nicenum(val, propbuf, proplen);
2615 			break;
2616 
2617 		case PROP_TYPE_STRING:
2618 			str = getprop_string(zhp, prop, &source);
2619 			if (str == NULL)
2620 				return (-1);
2621 			(void) strlcpy(propbuf, str, proplen);
2622 			break;
2623 
2624 		case PROP_TYPE_INDEX:
2625 			if (get_numeric_property(zhp, prop, src,
2626 			    &source, &val) != 0)
2627 				return (-1);
2628 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2629 				return (-1);
2630 			(void) strlcpy(propbuf, strval, proplen);
2631 			break;
2632 
2633 		default:
2634 			abort();
2635 		}
2636 	}
2637 
2638 	get_source(zhp, src, source, statbuf, statlen);
2639 
2640 	return (0);
2641 }
2642 
2643 /*
2644  * Utility function to get the given numeric property.  Does no validation that
2645  * the given property is the appropriate type; should only be used with
2646  * hard-coded property types.
2647  */
2648 uint64_t
2649 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2650 {
2651 	char *source;
2652 	uint64_t val;
2653 
2654 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2655 
2656 	return (val);
2657 }
2658 
2659 int
2660 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2661 {
2662 	char buf[64];
2663 
2664 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2665 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2666 }
2667 
2668 /*
2669  * Similar to zfs_prop_get(), but returns the value as an integer.
2670  */
2671 int
2672 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2673     zprop_source_t *src, char *statbuf, size_t statlen)
2674 {
2675 	char *source;
2676 
2677 	/*
2678 	 * Check to see if this property applies to our object
2679 	 */
2680 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2681 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2682 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2683 		    zfs_prop_to_name(prop)));
2684 	}
2685 
2686 	if (src)
2687 		*src = ZPROP_SRC_NONE;
2688 
2689 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2690 		return (-1);
2691 
2692 	get_source(zhp, src, source, statbuf, statlen);
2693 
2694 	return (0);
2695 }
2696 
2697 static int
2698 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2699     char **domainp, idmap_rid_t *ridp)
2700 {
2701 	idmap_get_handle_t *get_hdl = NULL;
2702 	idmap_stat status;
2703 	int err = EINVAL;
2704 
2705 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2706 		goto out;
2707 
2708 	if (isuser) {
2709 		err = idmap_get_sidbyuid(get_hdl, id,
2710 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2711 	} else {
2712 		err = idmap_get_sidbygid(get_hdl, id,
2713 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2714 	}
2715 	if (err == IDMAP_SUCCESS &&
2716 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2717 	    status == IDMAP_SUCCESS)
2718 		err = 0;
2719 	else
2720 		err = EINVAL;
2721 out:
2722 	if (get_hdl)
2723 		idmap_get_destroy(get_hdl);
2724 	return (err);
2725 }
2726 
2727 /*
2728  * convert the propname into parameters needed by kernel
2729  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2730  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2731  */
2732 static int
2733 userquota_propname_decode(const char *propname, boolean_t zoned,
2734     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2735 {
2736 	zfs_userquota_prop_t type;
2737 	char *cp, *end;
2738 	char *numericsid = NULL;
2739 	boolean_t isuser;
2740 
2741 	domain[0] = '\0';
2742 	*ridp = 0;
2743 	/* Figure out the property type ({user|group}{quota|space}) */
2744 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2745 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2746 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
2747 			break;
2748 	}
2749 	if (type == ZFS_NUM_USERQUOTA_PROPS)
2750 		return (EINVAL);
2751 	*typep = type;
2752 
2753 	isuser = (type == ZFS_PROP_USERQUOTA ||
2754 	    type == ZFS_PROP_USERUSED);
2755 
2756 	cp = strchr(propname, '@') + 1;
2757 
2758 	if (strchr(cp, '@')) {
2759 		/*
2760 		 * It's a SID name (eg "user@domain") that needs to be
2761 		 * turned into S-1-domainID-RID.
2762 		 */
2763 		int flag = 0;
2764 		idmap_stat stat, map_stat;
2765 		uid_t pid;
2766 		idmap_rid_t rid;
2767 		idmap_get_handle_t *gh = NULL;
2768 
2769 		stat = idmap_get_create(&gh);
2770 		if (stat != IDMAP_SUCCESS) {
2771 			idmap_get_destroy(gh);
2772 			return (ENOMEM);
2773 		}
2774 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2775 			return (ENOENT);
2776 		if (isuser) {
2777 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2778 			if (stat < 0)
2779 				return (ENOENT);
2780 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2781 			    &rid, &map_stat);
2782 		} else {
2783 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2784 			if (stat < 0)
2785 				return (ENOENT);
2786 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2787 			    &rid, &map_stat);
2788 		}
2789 		if (stat < 0) {
2790 			idmap_get_destroy(gh);
2791 			return (ENOENT);
2792 		}
2793 		stat = idmap_get_mappings(gh);
2794 		idmap_get_destroy(gh);
2795 
2796 		if (stat < 0) {
2797 			return (ENOENT);
2798 		}
2799 		if (numericsid == NULL)
2800 			return (ENOENT);
2801 		cp = numericsid;
2802 		*ridp = rid;
2803 		/* will be further decoded below */
2804 	}
2805 
2806 	if (strncmp(cp, "S-1-", 4) == 0) {
2807 		/* It's a numeric SID (eg "S-1-234-567-89") */
2808 		(void) strlcpy(domain, cp, domainlen);
2809 		errno = 0;
2810 		if (*ridp == 0) {
2811 			cp = strrchr(domain, '-');
2812 			*cp = '\0';
2813 			cp++;
2814 			*ridp = strtoull(cp, &end, 10);
2815 		} else {
2816 			end = "";
2817 		}
2818 		if (numericsid) {
2819 			free(numericsid);
2820 			numericsid = NULL;
2821 		}
2822 		if (errno != 0 || *end != '\0')
2823 			return (EINVAL);
2824 	} else if (!isdigit(*cp)) {
2825 		/*
2826 		 * It's a user/group name (eg "user") that needs to be
2827 		 * turned into a uid/gid
2828 		 */
2829 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2830 			return (ENOENT);
2831 		if (isuser) {
2832 			struct passwd *pw;
2833 			pw = getpwnam(cp);
2834 			if (pw == NULL)
2835 				return (ENOENT);
2836 			*ridp = pw->pw_uid;
2837 		} else {
2838 			struct group *gr;
2839 			gr = getgrnam(cp);
2840 			if (gr == NULL)
2841 				return (ENOENT);
2842 			*ridp = gr->gr_gid;
2843 		}
2844 	} else {
2845 		/* It's a user/group ID (eg "12345"). */
2846 		uid_t id = strtoul(cp, &end, 10);
2847 		idmap_rid_t rid;
2848 		char *mapdomain;
2849 
2850 		if (*end != '\0')
2851 			return (EINVAL);
2852 		if (id > MAXUID) {
2853 			/* It's an ephemeral ID. */
2854 			if (idmap_id_to_numeric_domain_rid(id, isuser,
2855 			    &mapdomain, &rid) != 0)
2856 				return (ENOENT);
2857 			(void) strlcpy(domain, mapdomain, domainlen);
2858 			*ridp = rid;
2859 		} else {
2860 			*ridp = id;
2861 		}
2862 	}
2863 
2864 	ASSERT3P(numericsid, ==, NULL);
2865 	return (0);
2866 }
2867 
2868 static int
2869 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2870     uint64_t *propvalue, zfs_userquota_prop_t *typep)
2871 {
2872 	int err;
2873 	zfs_cmd_t zc = { 0 };
2874 
2875 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2876 
2877 	err = userquota_propname_decode(propname,
2878 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2879 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2880 	zc.zc_objset_type = *typep;
2881 	if (err)
2882 		return (err);
2883 
2884 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2885 	if (err)
2886 		return (err);
2887 
2888 	*propvalue = zc.zc_cookie;
2889 	return (0);
2890 }
2891 
2892 int
2893 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2894     uint64_t *propvalue)
2895 {
2896 	zfs_userquota_prop_t type;
2897 
2898 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2899 	    &type));
2900 }
2901 
2902 int
2903 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2904     char *propbuf, int proplen, boolean_t literal)
2905 {
2906 	int err;
2907 	uint64_t propvalue;
2908 	zfs_userquota_prop_t type;
2909 
2910 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2911 	    &type);
2912 
2913 	if (err)
2914 		return (err);
2915 
2916 	if (literal) {
2917 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2918 	} else if (propvalue == 0 &&
2919 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2920 		(void) strlcpy(propbuf, "none", proplen);
2921 	} else {
2922 		zfs_nicenum(propvalue, propbuf, proplen);
2923 	}
2924 	return (0);
2925 }
2926 
2927 int
2928 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2929     uint64_t *propvalue)
2930 {
2931 	int err;
2932 	zfs_cmd_t zc = { 0 };
2933 	const char *snapname;
2934 
2935 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2936 
2937 	snapname = strchr(propname, '@') + 1;
2938 	if (strchr(snapname, '@')) {
2939 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2940 	} else {
2941 		/* snapname is the short name, append it to zhp's fsname */
2942 		char *cp;
2943 
2944 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
2945 		    sizeof (zc.zc_value));
2946 		cp = strchr(zc.zc_value, '@');
2947 		if (cp != NULL)
2948 			*cp = '\0';
2949 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2950 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2951 	}
2952 
2953 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2954 	if (err)
2955 		return (err);
2956 
2957 	*propvalue = zc.zc_cookie;
2958 	return (0);
2959 }
2960 
2961 int
2962 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2963     char *propbuf, int proplen, boolean_t literal)
2964 {
2965 	int err;
2966 	uint64_t propvalue;
2967 
2968 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2969 
2970 	if (err)
2971 		return (err);
2972 
2973 	if (literal) {
2974 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
2975 	} else {
2976 		zfs_nicenum(propvalue, propbuf, proplen);
2977 	}
2978 	return (0);
2979 }
2980 
2981 /*
2982  * Returns the name of the given zfs handle.
2983  */
2984 const char *
2985 zfs_get_name(const zfs_handle_t *zhp)
2986 {
2987 	return (zhp->zfs_name);
2988 }
2989 
2990 /*
2991  * Returns the name of the parent pool for the given zfs handle.
2992  */
2993 const char *
2994 zfs_get_pool_name(const zfs_handle_t *zhp)
2995 {
2996 	return (zhp->zpool_hdl->zpool_name);
2997 }
2998 
2999 /*
3000  * Returns the type of the given zfs handle.
3001  */
3002 zfs_type_t
3003 zfs_get_type(const zfs_handle_t *zhp)
3004 {
3005 	return (zhp->zfs_type);
3006 }
3007 
3008 /*
3009  * Is one dataset name a child dataset of another?
3010  *
3011  * Needs to handle these cases:
3012  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3013  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3014  * Descendant?	No.		No.		No.		Yes.
3015  */
3016 static boolean_t
3017 is_descendant(const char *ds1, const char *ds2)
3018 {
3019 	size_t d1len = strlen(ds1);
3020 
3021 	/* ds2 can't be a descendant if it's smaller */
3022 	if (strlen(ds2) < d1len)
3023 		return (B_FALSE);
3024 
3025 	/* otherwise, compare strings and verify that there's a '/' char */
3026 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3027 }
3028 
3029 /*
3030  * Given a complete name, return just the portion that refers to the parent.
3031  * Will return -1 if there is no parent (path is just the name of the
3032  * pool).
3033  */
3034 static int
3035 parent_name(const char *path, char *buf, size_t buflen)
3036 {
3037 	char *slashp;
3038 
3039 	(void) strlcpy(buf, path, buflen);
3040 
3041 	if ((slashp = strrchr(buf, '/')) == NULL)
3042 		return (-1);
3043 	*slashp = '\0';
3044 
3045 	return (0);
3046 }
3047 
3048 /*
3049  * If accept_ancestor is false, then check to make sure that the given path has
3050  * a parent, and that it exists.  If accept_ancestor is true, then find the
3051  * closest existing ancestor for the given path.  In prefixlen return the
3052  * length of already existing prefix of the given path.  We also fetch the
3053  * 'zoned' property, which is used to validate property settings when creating
3054  * new datasets.
3055  */
3056 static int
3057 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3058     boolean_t accept_ancestor, int *prefixlen)
3059 {
3060 	zfs_cmd_t zc = { 0 };
3061 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3062 	char *slash;
3063 	zfs_handle_t *zhp;
3064 	char errbuf[1024];
3065 	uint64_t is_zoned;
3066 
3067 	(void) snprintf(errbuf, sizeof (errbuf),
3068 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3069 
3070 	/* get parent, and check to see if this is just a pool */
3071 	if (parent_name(path, parent, sizeof (parent)) != 0) {
3072 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3073 		    "missing dataset name"));
3074 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3075 	}
3076 
3077 	/* check to see if the pool exists */
3078 	if ((slash = strchr(parent, '/')) == NULL)
3079 		slash = parent + strlen(parent);
3080 	(void) strncpy(zc.zc_name, parent, slash - parent);
3081 	zc.zc_name[slash - parent] = '\0';
3082 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3083 	    errno == ENOENT) {
3084 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085 		    "no such pool '%s'"), zc.zc_name);
3086 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3087 	}
3088 
3089 	/* check to see if the parent dataset exists */
3090 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3091 		if (errno == ENOENT && accept_ancestor) {
3092 			/*
3093 			 * Go deeper to find an ancestor, give up on top level.
3094 			 */
3095 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
3096 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3097 				    "no such pool '%s'"), zc.zc_name);
3098 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
3099 			}
3100 		} else if (errno == ENOENT) {
3101 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3102 			    "parent does not exist"));
3103 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3104 		} else
3105 			return (zfs_standard_error(hdl, errno, errbuf));
3106 	}
3107 
3108 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3109 	if (zoned != NULL)
3110 		*zoned = is_zoned;
3111 
3112 	/* we are in a non-global zone, but parent is in the global zone */
3113 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3114 		(void) zfs_standard_error(hdl, EPERM, errbuf);
3115 		zfs_close(zhp);
3116 		return (-1);
3117 	}
3118 
3119 	/* make sure parent is a filesystem */
3120 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3121 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3122 		    "parent is not a filesystem"));
3123 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3124 		zfs_close(zhp);
3125 		return (-1);
3126 	}
3127 
3128 	zfs_close(zhp);
3129 	if (prefixlen != NULL)
3130 		*prefixlen = strlen(parent);
3131 	return (0);
3132 }
3133 
3134 /*
3135  * Finds whether the dataset of the given type(s) exists.
3136  */
3137 boolean_t
3138 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3139 {
3140 	zfs_handle_t *zhp;
3141 
3142 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
3143 		return (B_FALSE);
3144 
3145 	/*
3146 	 * Try to get stats for the dataset, which will tell us if it exists.
3147 	 */
3148 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3149 		int ds_type = zhp->zfs_type;
3150 
3151 		zfs_close(zhp);
3152 		if (types & ds_type)
3153 			return (B_TRUE);
3154 	}
3155 	return (B_FALSE);
3156 }
3157 
3158 /*
3159  * Given a path to 'target', create all the ancestors between
3160  * the prefixlen portion of the path, and the target itself.
3161  * Fail if the initial prefixlen-ancestor does not already exist.
3162  */
3163 int
3164 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3165 {
3166 	zfs_handle_t *h;
3167 	char *cp;
3168 	const char *opname;
3169 
3170 	/* make sure prefix exists */
3171 	cp = target + prefixlen;
3172 	if (*cp != '/') {
3173 		assert(strchr(cp, '/') == NULL);
3174 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3175 	} else {
3176 		*cp = '\0';
3177 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3178 		*cp = '/';
3179 	}
3180 	if (h == NULL)
3181 		return (-1);
3182 	zfs_close(h);
3183 
3184 	/*
3185 	 * Attempt to create, mount, and share any ancestor filesystems,
3186 	 * up to the prefixlen-long one.
3187 	 */
3188 	for (cp = target + prefixlen + 1;
3189 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
3190 
3191 		*cp = '\0';
3192 
3193 		h = make_dataset_handle(hdl, target);
3194 		if (h) {
3195 			/* it already exists, nothing to do here */
3196 			zfs_close(h);
3197 			continue;
3198 		}
3199 
3200 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3201 		    NULL) != 0) {
3202 			opname = dgettext(TEXT_DOMAIN, "create");
3203 			goto ancestorerr;
3204 		}
3205 
3206 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3207 		if (h == NULL) {
3208 			opname = dgettext(TEXT_DOMAIN, "open");
3209 			goto ancestorerr;
3210 		}
3211 
3212 		if (zfs_mount(h, NULL, 0) != 0) {
3213 			opname = dgettext(TEXT_DOMAIN, "mount");
3214 			goto ancestorerr;
3215 		}
3216 
3217 		if (zfs_share(h) != 0) {
3218 			opname = dgettext(TEXT_DOMAIN, "share");
3219 			goto ancestorerr;
3220 		}
3221 
3222 		zfs_close(h);
3223 	}
3224 
3225 	return (0);
3226 
3227 ancestorerr:
3228 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3229 	    "failed to %s ancestor '%s'"), opname, target);
3230 	return (-1);
3231 }
3232 
3233 /*
3234  * Creates non-existing ancestors of the given path.
3235  */
3236 int
3237 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3238 {
3239 	int prefix;
3240 	char *path_copy;
3241 	int rc;
3242 
3243 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3244 		return (-1);
3245 
3246 	if ((path_copy = strdup(path)) != NULL) {
3247 		rc = create_parents(hdl, path_copy, prefix);
3248 		free(path_copy);
3249 	}
3250 	if (path_copy == NULL || rc != 0)
3251 		return (-1);
3252 
3253 	return (0);
3254 }
3255 
3256 /*
3257  * Create a new filesystem or volume.
3258  */
3259 int
3260 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3261     nvlist_t *props)
3262 {
3263 	int ret;
3264 	uint64_t size = 0;
3265 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3266 	char errbuf[1024];
3267 	uint64_t zoned;
3268 	dmu_objset_type_t ost;
3269 
3270 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3271 	    "cannot create '%s'"), path);
3272 
3273 	/* validate the path, taking care to note the extended error message */
3274 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
3275 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3276 
3277 	/* validate parents exist */
3278 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3279 		return (-1);
3280 
3281 	/*
3282 	 * The failure modes when creating a dataset of a different type over
3283 	 * one that already exists is a little strange.  In particular, if you
3284 	 * try to create a dataset on top of an existing dataset, the ioctl()
3285 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3286 	 * first try to see if the dataset exists.
3287 	 */
3288 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3289 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3290 		    "dataset already exists"));
3291 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3292 	}
3293 
3294 	if (type == ZFS_TYPE_VOLUME)
3295 		ost = DMU_OST_ZVOL;
3296 	else
3297 		ost = DMU_OST_ZFS;
3298 
3299 	/* open zpool handle for prop validation */
3300 	char pool_path[MAXNAMELEN];
3301 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3302 
3303 	/* truncate pool_path at first slash */
3304 	char *p = strchr(pool_path, '/');
3305 	if (p != NULL)
3306 		*p = '\0';
3307 
3308 	zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3309 
3310 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3311 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3312 		zpool_close(zpool_handle);
3313 		return (-1);
3314 	}
3315 	zpool_close(zpool_handle);
3316 
3317 	if (type == ZFS_TYPE_VOLUME) {
3318 		/*
3319 		 * If we are creating a volume, the size and block size must
3320 		 * satisfy a few restraints.  First, the blocksize must be a
3321 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3322 		 * volsize must be a multiple of the block size, and cannot be
3323 		 * zero.
3324 		 */
3325 		if (props == NULL || nvlist_lookup_uint64(props,
3326 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3327 			nvlist_free(props);
3328 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3329 			    "missing volume size"));
3330 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3331 		}
3332 
3333 		if ((ret = nvlist_lookup_uint64(props,
3334 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3335 		    &blocksize)) != 0) {
3336 			if (ret == ENOENT) {
3337 				blocksize = zfs_prop_default_numeric(
3338 				    ZFS_PROP_VOLBLOCKSIZE);
3339 			} else {
3340 				nvlist_free(props);
3341 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3342 				    "missing volume block size"));
3343 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3344 			}
3345 		}
3346 
3347 		if (size == 0) {
3348 			nvlist_free(props);
3349 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3350 			    "volume size cannot be zero"));
3351 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3352 		}
3353 
3354 		if (size % blocksize != 0) {
3355 			nvlist_free(props);
3356 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3357 			    "volume size must be a multiple of volume block "
3358 			    "size"));
3359 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3360 		}
3361 	}
3362 
3363 	/* create the dataset */
3364 	ret = lzc_create(path, ost, props);
3365 	nvlist_free(props);
3366 
3367 	/* check for failure */
3368 	if (ret != 0) {
3369 		char parent[ZFS_MAX_DATASET_NAME_LEN];
3370 		(void) parent_name(path, parent, sizeof (parent));
3371 
3372 		switch (errno) {
3373 		case ENOENT:
3374 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3375 			    "no such parent '%s'"), parent);
3376 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3377 
3378 		case EINVAL:
3379 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3380 			    "parent '%s' is not a filesystem"), parent);
3381 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3382 
3383 		case ENOTSUP:
3384 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3385 			    "pool must be upgraded to set this "
3386 			    "property or value"));
3387 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3388 #ifdef _ILP32
3389 		case EOVERFLOW:
3390 			/*
3391 			 * This platform can't address a volume this big.
3392 			 */
3393 			if (type == ZFS_TYPE_VOLUME)
3394 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
3395 				    errbuf));
3396 #endif
3397 			/* FALLTHROUGH */
3398 		default:
3399 			return (zfs_standard_error(hdl, errno, errbuf));
3400 		}
3401 	}
3402 
3403 	return (0);
3404 }
3405 
3406 /*
3407  * Destroys the given dataset.  The caller must make sure that the filesystem
3408  * isn't mounted, and that there are no active dependents. If the file system
3409  * does not exist this function does nothing.
3410  */
3411 int
3412 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3413 {
3414 	zfs_cmd_t zc = { 0 };
3415 
3416 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3417 		nvlist_t *nv = fnvlist_alloc();
3418 		fnvlist_add_boolean(nv, zhp->zfs_name);
3419 		int error = lzc_destroy_bookmarks(nv, NULL);
3420 		fnvlist_free(nv);
3421 		if (error != 0) {
3422 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3423 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3424 			    zhp->zfs_name));
3425 		}
3426 		return (0);
3427 	}
3428 
3429 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3430 
3431 	if (ZFS_IS_VOLUME(zhp)) {
3432 		zc.zc_objset_type = DMU_OST_ZVOL;
3433 	} else {
3434 		zc.zc_objset_type = DMU_OST_ZFS;
3435 	}
3436 
3437 	zc.zc_defer_destroy = defer;
3438 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3439 	    errno != ENOENT) {
3440 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3441 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3442 		    zhp->zfs_name));
3443 	}
3444 
3445 	remove_mountpoint(zhp);
3446 
3447 	return (0);
3448 }
3449 
3450 struct destroydata {
3451 	nvlist_t *nvl;
3452 	const char *snapname;
3453 };
3454 
3455 static int
3456 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3457 {
3458 	struct destroydata *dd = arg;
3459 	char name[ZFS_MAX_DATASET_NAME_LEN];
3460 	int rv = 0;
3461 
3462 	(void) snprintf(name, sizeof (name),
3463 	    "%s@%s", zhp->zfs_name, dd->snapname);
3464 
3465 	if (lzc_exists(name))
3466 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
3467 
3468 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3469 	zfs_close(zhp);
3470 	return (rv);
3471 }
3472 
3473 /*
3474  * Destroys all snapshots with the given name in zhp & descendants.
3475  */
3476 int
3477 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3478 {
3479 	int ret;
3480 	struct destroydata dd = { 0 };
3481 
3482 	dd.snapname = snapname;
3483 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3484 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3485 
3486 	if (nvlist_empty(dd.nvl)) {
3487 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3488 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3489 		    zhp->zfs_name, snapname);
3490 	} else {
3491 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3492 	}
3493 	nvlist_free(dd.nvl);
3494 	return (ret);
3495 }
3496 
3497 /*
3498  * Destroys all the snapshots named in the nvlist.
3499  */
3500 int
3501 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3502 {
3503 	int ret;
3504 	nvlist_t *errlist;
3505 
3506 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
3507 
3508 	if (ret == 0)
3509 		return (0);
3510 
3511 	if (nvlist_empty(errlist)) {
3512 		char errbuf[1024];
3513 		(void) snprintf(errbuf, sizeof (errbuf),
3514 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3515 
3516 		ret = zfs_standard_error(hdl, ret, errbuf);
3517 	}
3518 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3519 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3520 		char errbuf[1024];
3521 		(void) snprintf(errbuf, sizeof (errbuf),
3522 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3523 		    nvpair_name(pair));
3524 
3525 		switch (fnvpair_value_int32(pair)) {
3526 		case EEXIST:
3527 			zfs_error_aux(hdl,
3528 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3529 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3530 			break;
3531 		default:
3532 			ret = zfs_standard_error(hdl, errno, errbuf);
3533 			break;
3534 		}
3535 	}
3536 
3537 	nvlist_free(errlist);
3538 	return (ret);
3539 }
3540 
3541 /*
3542  * Clones the given dataset.  The target must be of the same type as the source.
3543  */
3544 int
3545 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3546 {
3547 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3548 	int ret;
3549 	char errbuf[1024];
3550 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3551 	uint64_t zoned;
3552 
3553 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3554 
3555 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3556 	    "cannot create '%s'"), target);
3557 
3558 	/* validate the target/clone name */
3559 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3560 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3561 
3562 	/* validate parents exist */
3563 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3564 		return (-1);
3565 
3566 	(void) parent_name(target, parent, sizeof (parent));
3567 
3568 	/* do the clone */
3569 
3570 	if (props) {
3571 		zfs_type_t type;
3572 		if (ZFS_IS_VOLUME(zhp)) {
3573 			type = ZFS_TYPE_VOLUME;
3574 		} else {
3575 			type = ZFS_TYPE_FILESYSTEM;
3576 		}
3577 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3578 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3579 			return (-1);
3580 	}
3581 
3582 	ret = lzc_clone(target, zhp->zfs_name, props);
3583 	nvlist_free(props);
3584 
3585 	if (ret != 0) {
3586 		switch (errno) {
3587 
3588 		case ENOENT:
3589 			/*
3590 			 * The parent doesn't exist.  We should have caught this
3591 			 * above, but there may a race condition that has since
3592 			 * destroyed the parent.
3593 			 *
3594 			 * At this point, we don't know whether it's the source
3595 			 * that doesn't exist anymore, or whether the target
3596 			 * dataset doesn't exist.
3597 			 */
3598 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3599 			    "no such parent '%s'"), parent);
3600 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3601 
3602 		case EXDEV:
3603 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3604 			    "source and target pools differ"));
3605 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3606 			    errbuf));
3607 
3608 		default:
3609 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3610 			    errbuf));
3611 		}
3612 	}
3613 
3614 	return (ret);
3615 }
3616 
3617 /*
3618  * Promotes the given clone fs to be the clone parent.
3619  */
3620 int
3621 zfs_promote(zfs_handle_t *zhp)
3622 {
3623 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3624 	zfs_cmd_t zc = { 0 };
3625 	char parent[MAXPATHLEN];
3626 	int ret;
3627 	char errbuf[1024];
3628 
3629 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3630 	    "cannot promote '%s'"), zhp->zfs_name);
3631 
3632 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3633 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3634 		    "snapshots can not be promoted"));
3635 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3636 	}
3637 
3638 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3639 	if (parent[0] == '\0') {
3640 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3641 		    "not a cloned filesystem"));
3642 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3643 	}
3644 
3645 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3646 	    sizeof (zc.zc_value));
3647 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3648 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3649 
3650 	if (ret != 0) {
3651 		int save_errno = errno;
3652 
3653 		switch (save_errno) {
3654 		case EEXIST:
3655 			/* There is a conflicting snapshot name. */
3656 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3657 			    "conflicting snapshot '%s' from parent '%s'"),
3658 			    zc.zc_string, parent);
3659 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3660 
3661 		default:
3662 			return (zfs_standard_error(hdl, save_errno, errbuf));
3663 		}
3664 	}
3665 	return (ret);
3666 }
3667 
3668 typedef struct snapdata {
3669 	nvlist_t *sd_nvl;
3670 	const char *sd_snapname;
3671 } snapdata_t;
3672 
3673 static int
3674 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3675 {
3676 	snapdata_t *sd = arg;
3677 	char name[ZFS_MAX_DATASET_NAME_LEN];
3678 	int rv = 0;
3679 
3680 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3681 		(void) snprintf(name, sizeof (name),
3682 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3683 
3684 		fnvlist_add_boolean(sd->sd_nvl, name);
3685 
3686 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3687 	}
3688 	zfs_close(zhp);
3689 
3690 	return (rv);
3691 }
3692 
3693 /*
3694  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3695  * created.
3696  */
3697 int
3698 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3699 {
3700 	int ret;
3701 	char errbuf[1024];
3702 	nvpair_t *elem;
3703 	nvlist_t *errors;
3704 
3705 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3706 	    "cannot create snapshots "));
3707 
3708 	elem = NULL;
3709 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3710 		const char *snapname = nvpair_name(elem);
3711 
3712 		/* validate the target name */
3713 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3714 		    B_TRUE)) {
3715 			(void) snprintf(errbuf, sizeof (errbuf),
3716 			    dgettext(TEXT_DOMAIN,
3717 			    "cannot create snapshot '%s'"), snapname);
3718 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3719 		}
3720 	}
3721 
3722 	/*
3723 	 * get pool handle for prop validation. assumes all snaps are in the
3724 	 * same pool, as does lzc_snapshot (below).
3725 	 */
3726 	char pool[MAXNAMELEN];
3727 	elem = nvlist_next_nvpair(snaps, NULL);
3728 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3729 	pool[strcspn(pool, "/@")] = '\0';
3730 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3731 
3732 	if (props != NULL &&
3733 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3734 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3735 		zpool_close(zpool_hdl);
3736 		return (-1);
3737 	}
3738 	zpool_close(zpool_hdl);
3739 
3740 	ret = lzc_snapshot(snaps, props, &errors);
3741 
3742 	if (ret != 0) {
3743 		boolean_t printed = B_FALSE;
3744 		for (elem = nvlist_next_nvpair(errors, NULL);
3745 		    elem != NULL;
3746 		    elem = nvlist_next_nvpair(errors, elem)) {
3747 			(void) snprintf(errbuf, sizeof (errbuf),
3748 			    dgettext(TEXT_DOMAIN,
3749 			    "cannot create snapshot '%s'"), nvpair_name(elem));
3750 			(void) zfs_standard_error(hdl,
3751 			    fnvpair_value_int32(elem), errbuf);
3752 			printed = B_TRUE;
3753 		}
3754 		if (!printed) {
3755 			switch (ret) {
3756 			case EXDEV:
3757 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3758 				    "multiple snapshots of same "
3759 				    "fs not allowed"));
3760 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3761 
3762 				break;
3763 			default:
3764 				(void) zfs_standard_error(hdl, ret, errbuf);
3765 			}
3766 		}
3767 	}
3768 
3769 	nvlist_free(props);
3770 	nvlist_free(errors);
3771 	return (ret);
3772 }
3773 
3774 int
3775 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3776     nvlist_t *props)
3777 {
3778 	int ret;
3779 	snapdata_t sd = { 0 };
3780 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
3781 	char *cp;
3782 	zfs_handle_t *zhp;
3783 	char errbuf[1024];
3784 
3785 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3786 	    "cannot snapshot %s"), path);
3787 
3788 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3789 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3790 
3791 	(void) strlcpy(fsname, path, sizeof (fsname));
3792 	cp = strchr(fsname, '@');
3793 	*cp = '\0';
3794 	sd.sd_snapname = cp + 1;
3795 
3796 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3797 	    ZFS_TYPE_VOLUME)) == NULL) {
3798 		return (-1);
3799 	}
3800 
3801 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3802 	if (recursive) {
3803 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3804 	} else {
3805 		fnvlist_add_boolean(sd.sd_nvl, path);
3806 	}
3807 
3808 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3809 	nvlist_free(sd.sd_nvl);
3810 	zfs_close(zhp);
3811 	return (ret);
3812 }
3813 
3814 /*
3815  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3816  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3817  * is a dependent and we should just destroy it without checking the transaction
3818  * group.
3819  */
3820 typedef struct rollback_data {
3821 	const char	*cb_target;		/* the snapshot */
3822 	uint64_t	cb_create;		/* creation time reference */
3823 	boolean_t	cb_error;
3824 	boolean_t	cb_force;
3825 } rollback_data_t;
3826 
3827 static int
3828 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3829 {
3830 	rollback_data_t *cbp = data;
3831 	prop_changelist_t *clp;
3832 
3833 	/* We must destroy this clone; first unmount it */
3834 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3835 	    cbp->cb_force ? MS_FORCE: 0);
3836 	if (clp == NULL || changelist_prefix(clp) != 0) {
3837 		cbp->cb_error = B_TRUE;
3838 		zfs_close(zhp);
3839 		return (0);
3840 	}
3841 	if (zfs_destroy(zhp, B_FALSE) != 0)
3842 		cbp->cb_error = B_TRUE;
3843 	else
3844 		changelist_remove(clp, zhp->zfs_name);
3845 	(void) changelist_postfix(clp);
3846 	changelist_free(clp);
3847 
3848 	zfs_close(zhp);
3849 	return (0);
3850 }
3851 
3852 static int
3853 rollback_destroy(zfs_handle_t *zhp, void *data)
3854 {
3855 	rollback_data_t *cbp = data;
3856 
3857 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3858 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3859 		    rollback_destroy_dependent, cbp);
3860 
3861 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3862 	}
3863 
3864 	zfs_close(zhp);
3865 	return (0);
3866 }
3867 
3868 /*
3869  * Given a dataset, rollback to a specific snapshot, discarding any
3870  * data changes since then and making it the active dataset.
3871  *
3872  * Any snapshots and bookmarks more recent than the target are
3873  * destroyed, along with their dependents (i.e. clones).
3874  */
3875 int
3876 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3877 {
3878 	rollback_data_t cb = { 0 };
3879 	int err;
3880 	boolean_t restore_resv = 0;
3881 	uint64_t old_volsize, new_volsize;
3882 	zfs_prop_t resv_prop;
3883 
3884 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3885 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3886 
3887 	/*
3888 	 * Destroy all recent snapshots and their dependents.
3889 	 */
3890 	cb.cb_force = force;
3891 	cb.cb_target = snap->zfs_name;
3892 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3893 	(void) zfs_iter_snapshots(zhp, rollback_destroy, &cb);
3894 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3895 
3896 	if (cb.cb_error)
3897 		return (-1);
3898 
3899 	/*
3900 	 * Now that we have verified that the snapshot is the latest,
3901 	 * rollback to the given snapshot.
3902 	 */
3903 
3904 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3905 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3906 			return (-1);
3907 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3908 		restore_resv =
3909 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3910 	}
3911 
3912 	/*
3913 	 * We rely on zfs_iter_children() to verify that there are no
3914 	 * newer snapshots for the given dataset.  Therefore, we can
3915 	 * simply pass the name on to the ioctl() call.  There is still
3916 	 * an unlikely race condition where the user has taken a
3917 	 * snapshot since we verified that this was the most recent.
3918 	 */
3919 	err = lzc_rollback(zhp->zfs_name, NULL, 0);
3920 	if (err != 0) {
3921 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3922 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3923 		    zhp->zfs_name);
3924 		return (err);
3925 	}
3926 
3927 	/*
3928 	 * For volumes, if the pre-rollback volsize matched the pre-
3929 	 * rollback reservation and the volsize has changed then set
3930 	 * the reservation property to the post-rollback volsize.
3931 	 * Make a new handle since the rollback closed the dataset.
3932 	 */
3933 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3934 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3935 		if (restore_resv) {
3936 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3937 			if (old_volsize != new_volsize)
3938 				err = zfs_prop_set_int(zhp, resv_prop,
3939 				    new_volsize);
3940 		}
3941 		zfs_close(zhp);
3942 	}
3943 	return (err);
3944 }
3945 
3946 /*
3947  * Renames the given dataset.
3948  */
3949 int
3950 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3951     boolean_t force_unmount)
3952 {
3953 	int ret;
3954 	zfs_cmd_t zc = { 0 };
3955 	char *delim;
3956 	prop_changelist_t *cl = NULL;
3957 	zfs_handle_t *zhrp = NULL;
3958 	char *parentname = NULL;
3959 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3960 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3961 	char errbuf[1024];
3962 
3963 	/* if we have the same exact name, just return success */
3964 	if (strcmp(zhp->zfs_name, target) == 0)
3965 		return (0);
3966 
3967 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3968 	    "cannot rename to '%s'"), target);
3969 
3970 	/*
3971 	 * Make sure the target name is valid
3972 	 */
3973 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3974 		if ((strchr(target, '@') == NULL) ||
3975 		    *target == '@') {
3976 			/*
3977 			 * Snapshot target name is abbreviated,
3978 			 * reconstruct full dataset name
3979 			 */
3980 			(void) strlcpy(parent, zhp->zfs_name,
3981 			    sizeof (parent));
3982 			delim = strchr(parent, '@');
3983 			if (strchr(target, '@') == NULL)
3984 				*(++delim) = '\0';
3985 			else
3986 				*delim = '\0';
3987 			(void) strlcat(parent, target, sizeof (parent));
3988 			target = parent;
3989 		} else {
3990 			/*
3991 			 * Make sure we're renaming within the same dataset.
3992 			 */
3993 			delim = strchr(target, '@');
3994 			if (strncmp(zhp->zfs_name, target, delim - target)
3995 			    != 0 || zhp->zfs_name[delim - target] != '@') {
3996 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3997 				    "snapshots must be part of same "
3998 				    "dataset"));
3999 				return (zfs_error(hdl, EZFS_CROSSTARGET,
4000 				    errbuf));
4001 			}
4002 		}
4003 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4004 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4005 	} else {
4006 		if (recursive) {
4007 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4008 			    "recursive rename must be a snapshot"));
4009 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4010 		}
4011 
4012 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4013 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4014 
4015 		/* validate parents */
4016 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4017 			return (-1);
4018 
4019 		/* make sure we're in the same pool */
4020 		verify((delim = strchr(target, '/')) != NULL);
4021 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4022 		    zhp->zfs_name[delim - target] != '/') {
4023 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4024 			    "datasets must be within same pool"));
4025 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4026 		}
4027 
4028 		/* new name cannot be a child of the current dataset name */
4029 		if (is_descendant(zhp->zfs_name, target)) {
4030 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4031 			    "New dataset name cannot be a descendant of "
4032 			    "current dataset name"));
4033 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4034 		}
4035 	}
4036 
4037 	(void) snprintf(errbuf, sizeof (errbuf),
4038 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4039 
4040 	if (getzoneid() == GLOBAL_ZONEID &&
4041 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4042 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4043 		    "dataset is used in a non-global zone"));
4044 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4045 	}
4046 
4047 	if (recursive) {
4048 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4049 		if (parentname == NULL) {
4050 			ret = -1;
4051 			goto error;
4052 		}
4053 		delim = strchr(parentname, '@');
4054 		*delim = '\0';
4055 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4056 		if (zhrp == NULL) {
4057 			ret = -1;
4058 			goto error;
4059 		}
4060 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4061 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4062 		    force_unmount ? MS_FORCE : 0)) == NULL)
4063 			return (-1);
4064 
4065 		if (changelist_haszonedchild(cl)) {
4066 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4067 			    "child dataset with inherited mountpoint is used "
4068 			    "in a non-global zone"));
4069 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4070 			goto error;
4071 		}
4072 
4073 		if ((ret = changelist_prefix(cl)) != 0)
4074 			goto error;
4075 	}
4076 
4077 	if (ZFS_IS_VOLUME(zhp))
4078 		zc.zc_objset_type = DMU_OST_ZVOL;
4079 	else
4080 		zc.zc_objset_type = DMU_OST_ZFS;
4081 
4082 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4083 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4084 
4085 	zc.zc_cookie = recursive;
4086 
4087 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4088 		/*
4089 		 * if it was recursive, the one that actually failed will
4090 		 * be in zc.zc_name
4091 		 */
4092 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4093 		    "cannot rename '%s'"), zc.zc_name);
4094 
4095 		if (recursive && errno == EEXIST) {
4096 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4097 			    "a child dataset already has a snapshot "
4098 			    "with the new name"));
4099 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4100 		} else {
4101 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4102 		}
4103 
4104 		/*
4105 		 * On failure, we still want to remount any filesystems that
4106 		 * were previously mounted, so we don't alter the system state.
4107 		 */
4108 		if (cl != NULL)
4109 			(void) changelist_postfix(cl);
4110 	} else {
4111 		if (cl != NULL) {
4112 			changelist_rename(cl, zfs_get_name(zhp), target);
4113 			ret = changelist_postfix(cl);
4114 		}
4115 	}
4116 
4117 error:
4118 	if (parentname != NULL) {
4119 		free(parentname);
4120 	}
4121 	if (zhrp != NULL) {
4122 		zfs_close(zhrp);
4123 	}
4124 	if (cl != NULL) {
4125 		changelist_free(cl);
4126 	}
4127 	return (ret);
4128 }
4129 
4130 nvlist_t *
4131 zfs_get_user_props(zfs_handle_t *zhp)
4132 {
4133 	return (zhp->zfs_user_props);
4134 }
4135 
4136 nvlist_t *
4137 zfs_get_recvd_props(zfs_handle_t *zhp)
4138 {
4139 	if (zhp->zfs_recvd_props == NULL)
4140 		if (get_recvd_props_ioctl(zhp) != 0)
4141 			return (NULL);
4142 	return (zhp->zfs_recvd_props);
4143 }
4144 
4145 /*
4146  * This function is used by 'zfs list' to determine the exact set of columns to
4147  * display, and their maximum widths.  This does two main things:
4148  *
4149  *      - If this is a list of all properties, then expand the list to include
4150  *        all native properties, and set a flag so that for each dataset we look
4151  *        for new unique user properties and add them to the list.
4152  *
4153  *      - For non fixed-width properties, keep track of the maximum width seen
4154  *        so that we can size the column appropriately. If the user has
4155  *        requested received property values, we also need to compute the width
4156  *        of the RECEIVED column.
4157  */
4158 int
4159 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4160     boolean_t literal)
4161 {
4162 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4163 	zprop_list_t *entry;
4164 	zprop_list_t **last, **start;
4165 	nvlist_t *userprops, *propval;
4166 	nvpair_t *elem;
4167 	char *strval;
4168 	char buf[ZFS_MAXPROPLEN];
4169 
4170 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4171 		return (-1);
4172 
4173 	userprops = zfs_get_user_props(zhp);
4174 
4175 	entry = *plp;
4176 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4177 		/*
4178 		 * Go through and add any user properties as necessary.  We
4179 		 * start by incrementing our list pointer to the first
4180 		 * non-native property.
4181 		 */
4182 		start = plp;
4183 		while (*start != NULL) {
4184 			if ((*start)->pl_prop == ZPROP_INVAL)
4185 				break;
4186 			start = &(*start)->pl_next;
4187 		}
4188 
4189 		elem = NULL;
4190 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4191 			/*
4192 			 * See if we've already found this property in our list.
4193 			 */
4194 			for (last = start; *last != NULL;
4195 			    last = &(*last)->pl_next) {
4196 				if (strcmp((*last)->pl_user_prop,
4197 				    nvpair_name(elem)) == 0)
4198 					break;
4199 			}
4200 
4201 			if (*last == NULL) {
4202 				if ((entry = zfs_alloc(hdl,
4203 				    sizeof (zprop_list_t))) == NULL ||
4204 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4205 				    nvpair_name(elem)))) == NULL) {
4206 					free(entry);
4207 					return (-1);
4208 				}
4209 
4210 				entry->pl_prop = ZPROP_INVAL;
4211 				entry->pl_width = strlen(nvpair_name(elem));
4212 				entry->pl_all = B_TRUE;
4213 				*last = entry;
4214 			}
4215 		}
4216 	}
4217 
4218 	/*
4219 	 * Now go through and check the width of any non-fixed columns
4220 	 */
4221 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4222 		if (entry->pl_fixed && !literal)
4223 			continue;
4224 
4225 		if (entry->pl_prop != ZPROP_INVAL) {
4226 			if (zfs_prop_get(zhp, entry->pl_prop,
4227 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4228 				if (strlen(buf) > entry->pl_width)
4229 					entry->pl_width = strlen(buf);
4230 			}
4231 			if (received && zfs_prop_get_recvd(zhp,
4232 			    zfs_prop_to_name(entry->pl_prop),
4233 			    buf, sizeof (buf), literal) == 0)
4234 				if (strlen(buf) > entry->pl_recvd_width)
4235 					entry->pl_recvd_width = strlen(buf);
4236 		} else {
4237 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4238 			    &propval) == 0) {
4239 				verify(nvlist_lookup_string(propval,
4240 				    ZPROP_VALUE, &strval) == 0);
4241 				if (strlen(strval) > entry->pl_width)
4242 					entry->pl_width = strlen(strval);
4243 			}
4244 			if (received && zfs_prop_get_recvd(zhp,
4245 			    entry->pl_user_prop,
4246 			    buf, sizeof (buf), literal) == 0)
4247 				if (strlen(buf) > entry->pl_recvd_width)
4248 					entry->pl_recvd_width = strlen(buf);
4249 		}
4250 	}
4251 
4252 	return (0);
4253 }
4254 
4255 int
4256 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4257     char *resource, void *export, void *sharetab,
4258     int sharemax, zfs_share_op_t operation)
4259 {
4260 	zfs_cmd_t zc = { 0 };
4261 	int error;
4262 
4263 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4264 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4265 	if (resource)
4266 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4267 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4268 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4269 	zc.zc_share.z_sharetype = operation;
4270 	zc.zc_share.z_sharemax = sharemax;
4271 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4272 	return (error);
4273 }
4274 
4275 void
4276 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4277 {
4278 	nvpair_t *curr;
4279 
4280 	/*
4281 	 * Keep a reference to the props-table against which we prune the
4282 	 * properties.
4283 	 */
4284 	zhp->zfs_props_table = props;
4285 
4286 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4287 
4288 	while (curr) {
4289 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4290 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4291 
4292 		/*
4293 		 * User properties will result in ZPROP_INVAL, and since we
4294 		 * only know how to prune standard ZFS properties, we always
4295 		 * leave these in the list.  This can also happen if we
4296 		 * encounter an unknown DSL property (when running older
4297 		 * software, for example).
4298 		 */
4299 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4300 			(void) nvlist_remove(zhp->zfs_props,
4301 			    nvpair_name(curr), nvpair_type(curr));
4302 		curr = next;
4303 	}
4304 }
4305 
4306 static int
4307 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4308     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4309 {
4310 	zfs_cmd_t zc = { 0 };
4311 	nvlist_t *nvlist = NULL;
4312 	int error;
4313 
4314 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4315 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4316 	zc.zc_cookie = (uint64_t)cmd;
4317 
4318 	if (cmd == ZFS_SMB_ACL_RENAME) {
4319 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4320 			(void) no_memory(hdl);
4321 			return (0);
4322 		}
4323 	}
4324 
4325 	switch (cmd) {
4326 	case ZFS_SMB_ACL_ADD:
4327 	case ZFS_SMB_ACL_REMOVE:
4328 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4329 		break;
4330 	case ZFS_SMB_ACL_RENAME:
4331 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4332 		    resource1) != 0) {
4333 				(void) no_memory(hdl);
4334 				return (-1);
4335 		}
4336 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4337 		    resource2) != 0) {
4338 				(void) no_memory(hdl);
4339 				return (-1);
4340 		}
4341 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4342 			nvlist_free(nvlist);
4343 			return (-1);
4344 		}
4345 		break;
4346 	case ZFS_SMB_ACL_PURGE:
4347 		break;
4348 	default:
4349 		return (-1);
4350 	}
4351 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4352 	nvlist_free(nvlist);
4353 	return (error);
4354 }
4355 
4356 int
4357 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4358     char *path, char *resource)
4359 {
4360 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4361 	    resource, NULL));
4362 }
4363 
4364 int
4365 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4366     char *path, char *resource)
4367 {
4368 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4369 	    resource, NULL));
4370 }
4371 
4372 int
4373 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4374 {
4375 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4376 	    NULL, NULL));
4377 }
4378 
4379 int
4380 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4381     char *oldname, char *newname)
4382 {
4383 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4384 	    oldname, newname));
4385 }
4386 
4387 int
4388 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4389     zfs_userspace_cb_t func, void *arg)
4390 {
4391 	zfs_cmd_t zc = { 0 };
4392 	zfs_useracct_t buf[100];
4393 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4394 	int ret;
4395 
4396 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4397 
4398 	zc.zc_objset_type = type;
4399 	zc.zc_nvlist_dst = (uintptr_t)buf;
4400 
4401 	for (;;) {
4402 		zfs_useracct_t *zua = buf;
4403 
4404 		zc.zc_nvlist_dst_size = sizeof (buf);
4405 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4406 			char errbuf[1024];
4407 
4408 			(void) snprintf(errbuf, sizeof (errbuf),
4409 			    dgettext(TEXT_DOMAIN,
4410 			    "cannot get used/quota for %s"), zc.zc_name);
4411 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4412 		}
4413 		if (zc.zc_nvlist_dst_size == 0)
4414 			break;
4415 
4416 		while (zc.zc_nvlist_dst_size > 0) {
4417 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4418 			    zua->zu_space)) != 0)
4419 				return (ret);
4420 			zua++;
4421 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4422 		}
4423 	}
4424 
4425 	return (0);
4426 }
4427 
4428 struct holdarg {
4429 	nvlist_t *nvl;
4430 	const char *snapname;
4431 	const char *tag;
4432 	boolean_t recursive;
4433 	int error;
4434 };
4435 
4436 static int
4437 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4438 {
4439 	struct holdarg *ha = arg;
4440 	char name[ZFS_MAX_DATASET_NAME_LEN];
4441 	int rv = 0;
4442 
4443 	(void) snprintf(name, sizeof (name),
4444 	    "%s@%s", zhp->zfs_name, ha->snapname);
4445 
4446 	if (lzc_exists(name))
4447 		fnvlist_add_string(ha->nvl, name, ha->tag);
4448 
4449 	if (ha->recursive)
4450 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4451 	zfs_close(zhp);
4452 	return (rv);
4453 }
4454 
4455 int
4456 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4457     boolean_t recursive, int cleanup_fd)
4458 {
4459 	int ret;
4460 	struct holdarg ha;
4461 
4462 	ha.nvl = fnvlist_alloc();
4463 	ha.snapname = snapname;
4464 	ha.tag = tag;
4465 	ha.recursive = recursive;
4466 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4467 
4468 	if (nvlist_empty(ha.nvl)) {
4469 		char errbuf[1024];
4470 
4471 		fnvlist_free(ha.nvl);
4472 		ret = ENOENT;
4473 		(void) snprintf(errbuf, sizeof (errbuf),
4474 		    dgettext(TEXT_DOMAIN,
4475 		    "cannot hold snapshot '%s@%s'"),
4476 		    zhp->zfs_name, snapname);
4477 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4478 		return (ret);
4479 	}
4480 
4481 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4482 	fnvlist_free(ha.nvl);
4483 
4484 	return (ret);
4485 }
4486 
4487 int
4488 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4489 {
4490 	int ret;
4491 	nvlist_t *errors;
4492 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4493 	char errbuf[1024];
4494 	nvpair_t *elem;
4495 
4496 	errors = NULL;
4497 	ret = lzc_hold(holds, cleanup_fd, &errors);
4498 
4499 	if (ret == 0) {
4500 		/* There may be errors even in the success case. */
4501 		fnvlist_free(errors);
4502 		return (0);
4503 	}
4504 
4505 	if (nvlist_empty(errors)) {
4506 		/* no hold-specific errors */
4507 		(void) snprintf(errbuf, sizeof (errbuf),
4508 		    dgettext(TEXT_DOMAIN, "cannot hold"));
4509 		switch (ret) {
4510 		case ENOTSUP:
4511 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4512 			    "pool must be upgraded"));
4513 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4514 			break;
4515 		case EINVAL:
4516 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4517 			break;
4518 		default:
4519 			(void) zfs_standard_error(hdl, ret, errbuf);
4520 		}
4521 	}
4522 
4523 	for (elem = nvlist_next_nvpair(errors, NULL);
4524 	    elem != NULL;
4525 	    elem = nvlist_next_nvpair(errors, elem)) {
4526 		(void) snprintf(errbuf, sizeof (errbuf),
4527 		    dgettext(TEXT_DOMAIN,
4528 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
4529 		switch (fnvpair_value_int32(elem)) {
4530 		case E2BIG:
4531 			/*
4532 			 * Temporary tags wind up having the ds object id
4533 			 * prepended. So even if we passed the length check
4534 			 * above, it's still possible for the tag to wind
4535 			 * up being slightly too long.
4536 			 */
4537 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4538 			break;
4539 		case EINVAL:
4540 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4541 			break;
4542 		case EEXIST:
4543 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4544 			break;
4545 		default:
4546 			(void) zfs_standard_error(hdl,
4547 			    fnvpair_value_int32(elem), errbuf);
4548 		}
4549 	}
4550 
4551 	fnvlist_free(errors);
4552 	return (ret);
4553 }
4554 
4555 static int
4556 zfs_release_one(zfs_handle_t *zhp, void *arg)
4557 {
4558 	struct holdarg *ha = arg;
4559 	char name[ZFS_MAX_DATASET_NAME_LEN];
4560 	int rv = 0;
4561 	nvlist_t *existing_holds;
4562 
4563 	(void) snprintf(name, sizeof (name),
4564 	    "%s@%s", zhp->zfs_name, ha->snapname);
4565 
4566 	if (lzc_get_holds(name, &existing_holds) != 0) {
4567 		ha->error = ENOENT;
4568 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4569 		ha->error = ESRCH;
4570 	} else {
4571 		nvlist_t *torelease = fnvlist_alloc();
4572 		fnvlist_add_boolean(torelease, ha->tag);
4573 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4574 		fnvlist_free(torelease);
4575 	}
4576 
4577 	if (ha->recursive)
4578 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4579 	zfs_close(zhp);
4580 	return (rv);
4581 }
4582 
4583 int
4584 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4585     boolean_t recursive)
4586 {
4587 	int ret;
4588 	struct holdarg ha;
4589 	nvlist_t *errors = NULL;
4590 	nvpair_t *elem;
4591 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4592 	char errbuf[1024];
4593 
4594 	ha.nvl = fnvlist_alloc();
4595 	ha.snapname = snapname;
4596 	ha.tag = tag;
4597 	ha.recursive = recursive;
4598 	ha.error = 0;
4599 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4600 
4601 	if (nvlist_empty(ha.nvl)) {
4602 		fnvlist_free(ha.nvl);
4603 		ret = ha.error;
4604 		(void) snprintf(errbuf, sizeof (errbuf),
4605 		    dgettext(TEXT_DOMAIN,
4606 		    "cannot release hold from snapshot '%s@%s'"),
4607 		    zhp->zfs_name, snapname);
4608 		if (ret == ESRCH) {
4609 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4610 		} else {
4611 			(void) zfs_standard_error(hdl, ret, errbuf);
4612 		}
4613 		return (ret);
4614 	}
4615 
4616 	ret = lzc_release(ha.nvl, &errors);
4617 	fnvlist_free(ha.nvl);
4618 
4619 	if (ret == 0) {
4620 		/* There may be errors even in the success case. */
4621 		fnvlist_free(errors);
4622 		return (0);
4623 	}
4624 
4625 	if (nvlist_empty(errors)) {
4626 		/* no hold-specific errors */
4627 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4628 		    "cannot release"));
4629 		switch (errno) {
4630 		case ENOTSUP:
4631 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4632 			    "pool must be upgraded"));
4633 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4634 			break;
4635 		default:
4636 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4637 		}
4638 	}
4639 
4640 	for (elem = nvlist_next_nvpair(errors, NULL);
4641 	    elem != NULL;
4642 	    elem = nvlist_next_nvpair(errors, elem)) {
4643 		(void) snprintf(errbuf, sizeof (errbuf),
4644 		    dgettext(TEXT_DOMAIN,
4645 		    "cannot release hold from snapshot '%s'"),
4646 		    nvpair_name(elem));
4647 		switch (fnvpair_value_int32(elem)) {
4648 		case ESRCH:
4649 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4650 			break;
4651 		case EINVAL:
4652 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4653 			break;
4654 		default:
4655 			(void) zfs_standard_error_fmt(hdl,
4656 			    fnvpair_value_int32(elem), errbuf);
4657 		}
4658 	}
4659 
4660 	fnvlist_free(errors);
4661 	return (ret);
4662 }
4663 
4664 int
4665 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4666 {
4667 	zfs_cmd_t zc = { 0 };
4668 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4669 	int nvsz = 2048;
4670 	void *nvbuf;
4671 	int err = 0;
4672 	char errbuf[1024];
4673 
4674 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4675 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4676 
4677 tryagain:
4678 
4679 	nvbuf = malloc(nvsz);
4680 	if (nvbuf == NULL) {
4681 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4682 		goto out;
4683 	}
4684 
4685 	zc.zc_nvlist_dst_size = nvsz;
4686 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4687 
4688 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4689 
4690 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4691 		(void) snprintf(errbuf, sizeof (errbuf),
4692 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4693 		    zc.zc_name);
4694 		switch (errno) {
4695 		case ENOMEM:
4696 			free(nvbuf);
4697 			nvsz = zc.zc_nvlist_dst_size;
4698 			goto tryagain;
4699 
4700 		case ENOTSUP:
4701 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4702 			    "pool must be upgraded"));
4703 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4704 			break;
4705 		case EINVAL:
4706 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4707 			break;
4708 		case ENOENT:
4709 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4710 			break;
4711 		default:
4712 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4713 			break;
4714 		}
4715 	} else {
4716 		/* success */
4717 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4718 		if (rc) {
4719 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
4720 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
4721 			    zc.zc_name);
4722 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
4723 		}
4724 	}
4725 
4726 	free(nvbuf);
4727 out:
4728 	return (err);
4729 }
4730 
4731 int
4732 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4733 {
4734 	zfs_cmd_t zc = { 0 };
4735 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4736 	char *nvbuf;
4737 	char errbuf[1024];
4738 	size_t nvsz;
4739 	int err;
4740 
4741 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4742 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4743 
4744 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4745 	assert(err == 0);
4746 
4747 	nvbuf = malloc(nvsz);
4748 
4749 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4750 	assert(err == 0);
4751 
4752 	zc.zc_nvlist_src_size = nvsz;
4753 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
4754 	zc.zc_perm_action = un;
4755 
4756 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4757 
4758 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4759 		(void) snprintf(errbuf, sizeof (errbuf),
4760 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4761 		    zc.zc_name);
4762 		switch (errno) {
4763 		case ENOTSUP:
4764 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4765 			    "pool must be upgraded"));
4766 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4767 			break;
4768 		case EINVAL:
4769 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4770 			break;
4771 		case ENOENT:
4772 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4773 			break;
4774 		default:
4775 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4776 			break;
4777 		}
4778 	}
4779 
4780 	free(nvbuf);
4781 
4782 	return (err);
4783 }
4784 
4785 int
4786 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4787 {
4788 	int err;
4789 	char errbuf[1024];
4790 
4791 	err = lzc_get_holds(zhp->zfs_name, nvl);
4792 
4793 	if (err != 0) {
4794 		libzfs_handle_t *hdl = zhp->zfs_hdl;
4795 
4796 		(void) snprintf(errbuf, sizeof (errbuf),
4797 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4798 		    zhp->zfs_name);
4799 		switch (err) {
4800 		case ENOTSUP:
4801 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4802 			    "pool must be upgraded"));
4803 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4804 			break;
4805 		case EINVAL:
4806 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4807 			break;
4808 		case ENOENT:
4809 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4810 			break;
4811 		default:
4812 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4813 			break;
4814 		}
4815 	}
4816 
4817 	return (err);
4818 }
4819 
4820 /*
4821  * Convert the zvol's volume size to an appropriate reservation.
4822  * Note: If this routine is updated, it is necessary to update the ZFS test
4823  * suite's shell version in reservation.kshlib.
4824  */
4825 uint64_t
4826 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4827 {
4828 	uint64_t numdb;
4829 	uint64_t nblocks, volblocksize;
4830 	int ncopies;
4831 	char *strval;
4832 
4833 	if (nvlist_lookup_string(props,
4834 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4835 		ncopies = atoi(strval);
4836 	else
4837 		ncopies = 1;
4838 	if (nvlist_lookup_uint64(props,
4839 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4840 	    &volblocksize) != 0)
4841 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4842 	nblocks = volsize/volblocksize;
4843 	/* start with metadnode L0-L6 */
4844 	numdb = 7;
4845 	/* calculate number of indirects */
4846 	while (nblocks > 1) {
4847 		nblocks += DNODES_PER_LEVEL - 1;
4848 		nblocks /= DNODES_PER_LEVEL;
4849 		numdb += nblocks;
4850 	}
4851 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4852 	volsize *= ncopies;
4853 	/*
4854 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4855 	 * compressed, but in practice they compress down to about
4856 	 * 1100 bytes
4857 	 */
4858 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4859 	volsize += numdb;
4860 	return (volsize);
4861 }
4862