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