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