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