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