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