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