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 	char errbuf[1024];
3413 	int rc = 0;
3414 
3415 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3416 	    "cannot create '%s'"), path);
3417 
3418 	/*
3419 	 * Check that we are not passing the nesting limit
3420 	 * before we start creating any ancestors.
3421 	 */
3422 	if (dataset_nestcheck(path) != 0) {
3423 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3424 		    "maximum name nesting depth exceeded"));
3425 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3426 	}
3427 
3428 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3429 		return (-1);
3430 
3431 	if ((path_copy = strdup(path)) != NULL) {
3432 		rc = create_parents(hdl, path_copy, prefix);
3433 		free(path_copy);
3434 	}
3435 	if (path_copy == NULL || rc != 0)
3436 		return (-1);
3437 
3438 	return (0);
3439 }
3440 
3441 /*
3442  * Create a new filesystem or volume.
3443  */
3444 int
3445 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3446     nvlist_t *props)
3447 {
3448 	int ret;
3449 	uint64_t size = 0;
3450 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3451 	char errbuf[1024];
3452 	uint64_t zoned;
3453 	enum lzc_dataset_type ost;
3454 	zpool_handle_t *zpool_handle;
3455 
3456 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3457 	    "cannot create '%s'"), path);
3458 
3459 	/* validate the path, taking care to note the extended error message */
3460 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
3461 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3462 
3463 	if (dataset_nestcheck(path) != 0) {
3464 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3465 		    "maximum name nesting depth exceeded"));
3466 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3467 	}
3468 
3469 	/* validate parents exist */
3470 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3471 		return (-1);
3472 
3473 	/*
3474 	 * The failure modes when creating a dataset of a different type over
3475 	 * one that already exists is a little strange.  In particular, if you
3476 	 * try to create a dataset on top of an existing dataset, the ioctl()
3477 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3478 	 * first try to see if the dataset exists.
3479 	 */
3480 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3481 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3482 		    "dataset already exists"));
3483 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3484 	}
3485 
3486 	if (type == ZFS_TYPE_VOLUME)
3487 		ost = LZC_DATSET_TYPE_ZVOL;
3488 	else
3489 		ost = LZC_DATSET_TYPE_ZFS;
3490 
3491 	/* open zpool handle for prop validation */
3492 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3493 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3494 
3495 	/* truncate pool_path at first slash */
3496 	char *p = strchr(pool_path, '/');
3497 	if (p != NULL)
3498 		*p = '\0';
3499 
3500 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3501 		return (-1);
3502 
3503 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3504 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3505 		zpool_close(zpool_handle);
3506 		return (-1);
3507 	}
3508 	zpool_close(zpool_handle);
3509 
3510 	if (type == ZFS_TYPE_VOLUME) {
3511 		/*
3512 		 * If we are creating a volume, the size and block size must
3513 		 * satisfy a few restraints.  First, the blocksize must be a
3514 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3515 		 * volsize must be a multiple of the block size, and cannot be
3516 		 * zero.
3517 		 */
3518 		if (props == NULL || nvlist_lookup_uint64(props,
3519 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3520 			nvlist_free(props);
3521 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3522 			    "missing volume size"));
3523 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3524 		}
3525 
3526 		if ((ret = nvlist_lookup_uint64(props,
3527 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3528 		    &blocksize)) != 0) {
3529 			if (ret == ENOENT) {
3530 				blocksize = zfs_prop_default_numeric(
3531 				    ZFS_PROP_VOLBLOCKSIZE);
3532 			} else {
3533 				nvlist_free(props);
3534 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3535 				    "missing volume block size"));
3536 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3537 			}
3538 		}
3539 
3540 		if (size == 0) {
3541 			nvlist_free(props);
3542 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3543 			    "volume size cannot be zero"));
3544 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3545 		}
3546 
3547 		if (size % blocksize != 0) {
3548 			nvlist_free(props);
3549 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3550 			    "volume size must be a multiple of volume block "
3551 			    "size"));
3552 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3553 		}
3554 	}
3555 
3556 	/* create the dataset */
3557 	ret = lzc_create(path, ost, props);
3558 	nvlist_free(props);
3559 
3560 	/* check for failure */
3561 	if (ret != 0) {
3562 		char parent[ZFS_MAX_DATASET_NAME_LEN];
3563 		(void) parent_name(path, parent, sizeof (parent));
3564 
3565 		switch (errno) {
3566 		case ENOENT:
3567 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3568 			    "no such parent '%s'"), parent);
3569 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3570 
3571 		case EINVAL:
3572 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3573 			    "parent '%s' is not a filesystem"), parent);
3574 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3575 
3576 		case ENOTSUP:
3577 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3578 			    "pool must be upgraded to set this "
3579 			    "property or value"));
3580 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3581 		case ERANGE:
3582 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3583 			    "invalid property value(s) specified"));
3584 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3585 #ifdef _ILP32
3586 		case EOVERFLOW:
3587 			/*
3588 			 * This platform can't address a volume this big.
3589 			 */
3590 			if (type == ZFS_TYPE_VOLUME)
3591 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
3592 				    errbuf));
3593 #endif
3594 			/* FALLTHROUGH */
3595 		default:
3596 			return (zfs_standard_error(hdl, errno, errbuf));
3597 		}
3598 	}
3599 
3600 	return (0);
3601 }
3602 
3603 /*
3604  * Destroys the given dataset.  The caller must make sure that the filesystem
3605  * isn't mounted, and that there are no active dependents. If the file system
3606  * does not exist this function does nothing.
3607  */
3608 int
3609 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3610 {
3611 	zfs_cmd_t zc = { 0 };
3612 
3613 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3614 		nvlist_t *nv = fnvlist_alloc();
3615 		fnvlist_add_boolean(nv, zhp->zfs_name);
3616 		int error = lzc_destroy_bookmarks(nv, NULL);
3617 		fnvlist_free(nv);
3618 		if (error != 0) {
3619 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3620 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3621 			    zhp->zfs_name));
3622 		}
3623 		return (0);
3624 	}
3625 
3626 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3627 
3628 	if (ZFS_IS_VOLUME(zhp)) {
3629 		zc.zc_objset_type = DMU_OST_ZVOL;
3630 	} else {
3631 		zc.zc_objset_type = DMU_OST_ZFS;
3632 	}
3633 
3634 	zc.zc_defer_destroy = defer;
3635 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3636 	    errno != ENOENT) {
3637 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3638 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3639 		    zhp->zfs_name));
3640 	}
3641 
3642 	remove_mountpoint(zhp);
3643 
3644 	return (0);
3645 }
3646 
3647 struct destroydata {
3648 	nvlist_t *nvl;
3649 	const char *snapname;
3650 };
3651 
3652 static int
3653 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3654 {
3655 	struct destroydata *dd = arg;
3656 	char name[ZFS_MAX_DATASET_NAME_LEN];
3657 	int rv = 0;
3658 
3659 	(void) snprintf(name, sizeof (name),
3660 	    "%s@%s", zhp->zfs_name, dd->snapname);
3661 
3662 	if (lzc_exists(name))
3663 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
3664 
3665 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3666 	zfs_close(zhp);
3667 	return (rv);
3668 }
3669 
3670 /*
3671  * Destroys all snapshots with the given name in zhp & descendants.
3672  */
3673 int
3674 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3675 {
3676 	int ret;
3677 	struct destroydata dd = { 0 };
3678 
3679 	dd.snapname = snapname;
3680 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3681 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3682 
3683 	if (nvlist_empty(dd.nvl)) {
3684 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3685 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3686 		    zhp->zfs_name, snapname);
3687 	} else {
3688 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3689 	}
3690 	nvlist_free(dd.nvl);
3691 	return (ret);
3692 }
3693 
3694 /*
3695  * Destroys all the snapshots named in the nvlist.
3696  */
3697 int
3698 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3699 {
3700 	int ret;
3701 	nvlist_t *errlist = NULL;
3702 
3703 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
3704 
3705 	if (ret == 0) {
3706 		nvlist_free(errlist);
3707 		return (0);
3708 	}
3709 
3710 	if (nvlist_empty(errlist)) {
3711 		char errbuf[1024];
3712 		(void) snprintf(errbuf, sizeof (errbuf),
3713 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3714 
3715 		ret = zfs_standard_error(hdl, ret, errbuf);
3716 	}
3717 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3718 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3719 		char errbuf[1024];
3720 		(void) snprintf(errbuf, sizeof (errbuf),
3721 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3722 		    nvpair_name(pair));
3723 
3724 		switch (fnvpair_value_int32(pair)) {
3725 		case EEXIST:
3726 			zfs_error_aux(hdl,
3727 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3728 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3729 			break;
3730 		default:
3731 			ret = zfs_standard_error(hdl, errno, errbuf);
3732 			break;
3733 		}
3734 	}
3735 
3736 	nvlist_free(errlist);
3737 	return (ret);
3738 }
3739 
3740 /*
3741  * Clones the given dataset.  The target must be of the same type as the source.
3742  */
3743 int
3744 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3745 {
3746 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3747 	int ret;
3748 	char errbuf[1024];
3749 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3750 	uint64_t zoned;
3751 
3752 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3753 
3754 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3755 	    "cannot create '%s'"), target);
3756 
3757 	/* validate the target/clone name */
3758 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3759 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3760 
3761 	/* validate parents exist */
3762 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3763 		return (-1);
3764 
3765 	(void) parent_name(target, parent, sizeof (parent));
3766 
3767 	/* do the clone */
3768 
3769 	if (props) {
3770 		zfs_type_t type;
3771 
3772 		if (ZFS_IS_VOLUME(zhp)) {
3773 			type = ZFS_TYPE_VOLUME;
3774 		} else {
3775 			type = ZFS_TYPE_FILESYSTEM;
3776 		}
3777 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3778 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3779 			return (-1);
3780 		if (zfs_fix_auto_resv(zhp, props) == -1) {
3781 			nvlist_free(props);
3782 			return (-1);
3783 		}
3784 	}
3785 
3786 	ret = lzc_clone(target, zhp->zfs_name, props);
3787 	nvlist_free(props);
3788 
3789 	if (ret != 0) {
3790 		switch (errno) {
3791 
3792 		case ENOENT:
3793 			/*
3794 			 * The parent doesn't exist.  We should have caught this
3795 			 * above, but there may a race condition that has since
3796 			 * destroyed the parent.
3797 			 *
3798 			 * At this point, we don't know whether it's the source
3799 			 * that doesn't exist anymore, or whether the target
3800 			 * dataset doesn't exist.
3801 			 */
3802 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3803 			    "no such parent '%s'"), parent);
3804 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3805 
3806 		case EXDEV:
3807 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3808 			    "source and target pools differ"));
3809 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3810 			    errbuf));
3811 
3812 		default:
3813 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3814 			    errbuf));
3815 		}
3816 	}
3817 
3818 	return (ret);
3819 }
3820 
3821 /*
3822  * Promotes the given clone fs to be the clone parent.
3823  */
3824 int
3825 zfs_promote(zfs_handle_t *zhp)
3826 {
3827 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3828 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3829 	int ret;
3830 	char errbuf[1024];
3831 
3832 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3833 	    "cannot promote '%s'"), zhp->zfs_name);
3834 
3835 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3836 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3837 		    "snapshots can not be promoted"));
3838 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3839 	}
3840 
3841 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3842 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3843 		    "not a cloned filesystem"));
3844 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3845 	}
3846 
3847 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3848 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3849 
3850 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3851 
3852 	if (ret != 0) {
3853 		switch (ret) {
3854 		case EEXIST:
3855 			/* There is a conflicting snapshot name. */
3856 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3857 			    "conflicting snapshot '%s' from parent '%s'"),
3858 			    snapname, zhp->zfs_dmustats.dds_origin);
3859 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3860 
3861 		default:
3862 			return (zfs_standard_error(hdl, ret, errbuf));
3863 		}
3864 	}
3865 	return (ret);
3866 }
3867 
3868 typedef struct snapdata {
3869 	nvlist_t *sd_nvl;
3870 	const char *sd_snapname;
3871 } snapdata_t;
3872 
3873 static int
3874 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3875 {
3876 	snapdata_t *sd = arg;
3877 	char name[ZFS_MAX_DATASET_NAME_LEN];
3878 	int rv = 0;
3879 
3880 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3881 		(void) snprintf(name, sizeof (name),
3882 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3883 
3884 		fnvlist_add_boolean(sd->sd_nvl, name);
3885 
3886 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3887 	}
3888 	zfs_close(zhp);
3889 
3890 	return (rv);
3891 }
3892 
3893 int
3894 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3895 {
3896 	int err;
3897 	char errbuf[1024];
3898 
3899 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3900 	    "cannot remap dataset '%s'"), fs);
3901 
3902 	err = lzc_remap(fs);
3903 
3904 	if (err != 0) {
3905 		switch (err) {
3906 		case ENOTSUP:
3907 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3908 			    "pool must be upgraded"));
3909 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3910 			break;
3911 		case EINVAL:
3912 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3913 			break;
3914 		default:
3915 			(void) zfs_standard_error(hdl, err, errbuf);
3916 			break;
3917 		}
3918 	}
3919 
3920 	return (err);
3921 }
3922 
3923 /*
3924  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3925  * created.
3926  */
3927 int
3928 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3929 {
3930 	int ret;
3931 	char errbuf[1024];
3932 	nvpair_t *elem;
3933 	nvlist_t *errors;
3934 
3935 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3936 	    "cannot create snapshots "));
3937 
3938 	elem = NULL;
3939 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3940 		const char *snapname = nvpair_name(elem);
3941 
3942 		/* validate the target name */
3943 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3944 		    B_TRUE)) {
3945 			(void) snprintf(errbuf, sizeof (errbuf),
3946 			    dgettext(TEXT_DOMAIN,
3947 			    "cannot create snapshot '%s'"), snapname);
3948 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3949 		}
3950 	}
3951 
3952 	/*
3953 	 * get pool handle for prop validation. assumes all snaps are in the
3954 	 * same pool, as does lzc_snapshot (below).
3955 	 */
3956 	char pool[ZFS_MAX_DATASET_NAME_LEN];
3957 	elem = nvlist_next_nvpair(snaps, NULL);
3958 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3959 	pool[strcspn(pool, "/@")] = '\0';
3960 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3961 
3962 	if (props != NULL &&
3963 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3964 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3965 		zpool_close(zpool_hdl);
3966 		return (-1);
3967 	}
3968 	zpool_close(zpool_hdl);
3969 
3970 	ret = lzc_snapshot(snaps, props, &errors);
3971 
3972 	if (ret != 0) {
3973 		boolean_t printed = B_FALSE;
3974 		for (elem = nvlist_next_nvpair(errors, NULL);
3975 		    elem != NULL;
3976 		    elem = nvlist_next_nvpair(errors, elem)) {
3977 			(void) snprintf(errbuf, sizeof (errbuf),
3978 			    dgettext(TEXT_DOMAIN,
3979 			    "cannot create snapshot '%s'"), nvpair_name(elem));
3980 			(void) zfs_standard_error(hdl,
3981 			    fnvpair_value_int32(elem), errbuf);
3982 			printed = B_TRUE;
3983 		}
3984 		if (!printed) {
3985 			switch (ret) {
3986 			case EXDEV:
3987 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3988 				    "multiple snapshots of same "
3989 				    "fs not allowed"));
3990 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3991 
3992 				break;
3993 			default:
3994 				(void) zfs_standard_error(hdl, ret, errbuf);
3995 			}
3996 		}
3997 	}
3998 
3999 	nvlist_free(props);
4000 	nvlist_free(errors);
4001 	return (ret);
4002 }
4003 
4004 int
4005 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4006     nvlist_t *props)
4007 {
4008 	int ret;
4009 	snapdata_t sd = { 0 };
4010 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
4011 	char *cp;
4012 	zfs_handle_t *zhp;
4013 	char errbuf[1024];
4014 
4015 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4016 	    "cannot snapshot %s"), path);
4017 
4018 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4019 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4020 
4021 	(void) strlcpy(fsname, path, sizeof (fsname));
4022 	cp = strchr(fsname, '@');
4023 	*cp = '\0';
4024 	sd.sd_snapname = cp + 1;
4025 
4026 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4027 	    ZFS_TYPE_VOLUME)) == NULL) {
4028 		return (-1);
4029 	}
4030 
4031 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4032 	if (recursive) {
4033 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4034 	} else {
4035 		fnvlist_add_boolean(sd.sd_nvl, path);
4036 	}
4037 
4038 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4039 	nvlist_free(sd.sd_nvl);
4040 	zfs_close(zhp);
4041 	return (ret);
4042 }
4043 
4044 /*
4045  * Destroy any more recent snapshots.  We invoke this callback on any dependents
4046  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
4047  * is a dependent and we should just destroy it without checking the transaction
4048  * group.
4049  */
4050 typedef struct rollback_data {
4051 	const char	*cb_target;		/* the snapshot */
4052 	uint64_t	cb_create;		/* creation time reference */
4053 	boolean_t	cb_error;
4054 	boolean_t	cb_force;
4055 } rollback_data_t;
4056 
4057 static int
4058 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4059 {
4060 	rollback_data_t *cbp = data;
4061 	prop_changelist_t *clp;
4062 
4063 	/* We must destroy this clone; first unmount it */
4064 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4065 	    cbp->cb_force ? MS_FORCE: 0);
4066 	if (clp == NULL || changelist_prefix(clp) != 0) {
4067 		cbp->cb_error = B_TRUE;
4068 		zfs_close(zhp);
4069 		return (0);
4070 	}
4071 	if (zfs_destroy(zhp, B_FALSE) != 0)
4072 		cbp->cb_error = B_TRUE;
4073 	else
4074 		changelist_remove(clp, zhp->zfs_name);
4075 	(void) changelist_postfix(clp);
4076 	changelist_free(clp);
4077 
4078 	zfs_close(zhp);
4079 	return (0);
4080 }
4081 
4082 static int
4083 rollback_destroy(zfs_handle_t *zhp, void *data)
4084 {
4085 	rollback_data_t *cbp = data;
4086 
4087 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4088 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4089 		    rollback_destroy_dependent, cbp);
4090 
4091 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4092 	}
4093 
4094 	zfs_close(zhp);
4095 	return (0);
4096 }
4097 
4098 /*
4099  * Given a dataset, rollback to a specific snapshot, discarding any
4100  * data changes since then and making it the active dataset.
4101  *
4102  * Any snapshots and bookmarks more recent than the target are
4103  * destroyed, along with their dependents (i.e. clones).
4104  */
4105 int
4106 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4107 {
4108 	rollback_data_t cb = { 0 };
4109 	int err;
4110 	boolean_t restore_resv = 0;
4111 	uint64_t old_volsize = 0, new_volsize;
4112 	zfs_prop_t resv_prop;
4113 
4114 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4115 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
4116 
4117 	/*
4118 	 * Destroy all recent snapshots and their dependents.
4119 	 */
4120 	cb.cb_force = force;
4121 	cb.cb_target = snap->zfs_name;
4122 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4123 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4124 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4125 
4126 	if (cb.cb_error)
4127 		return (-1);
4128 
4129 	/*
4130 	 * Now that we have verified that the snapshot is the latest,
4131 	 * rollback to the given snapshot.
4132 	 */
4133 
4134 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4135 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4136 			return (-1);
4137 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4138 		restore_resv =
4139 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4140 	}
4141 
4142 	/*
4143 	 * Pass both the filesystem and the wanted snapshot names,
4144 	 * we would get an error back if the snapshot is destroyed or
4145 	 * a new snapshot is created before this request is processed.
4146 	 */
4147 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4148 	if (err != 0) {
4149 		char errbuf[1024];
4150 
4151 		(void) snprintf(errbuf, sizeof (errbuf),
4152 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4153 		    zhp->zfs_name);
4154 		switch (err) {
4155 		case EEXIST:
4156 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4157 			    "there is a snapshot or bookmark more recent "
4158 			    "than '%s'"), snap->zfs_name);
4159 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4160 			break;
4161 		case ESRCH:
4162 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4163 			    "'%s' is not found among snapshots of '%s'"),
4164 			    snap->zfs_name, zhp->zfs_name);
4165 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4166 			break;
4167 		case EINVAL:
4168 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4169 			break;
4170 		default:
4171 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4172 		}
4173 		return (err);
4174 	}
4175 
4176 	/*
4177 	 * For volumes, if the pre-rollback volsize matched the pre-
4178 	 * rollback reservation and the volsize has changed then set
4179 	 * the reservation property to the post-rollback volsize.
4180 	 * Make a new handle since the rollback closed the dataset.
4181 	 */
4182 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4183 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4184 		if (restore_resv) {
4185 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4186 			if (old_volsize != new_volsize)
4187 				err = zfs_prop_set_int(zhp, resv_prop,
4188 				    new_volsize);
4189 		}
4190 		zfs_close(zhp);
4191 	}
4192 	return (err);
4193 }
4194 
4195 /*
4196  * Renames the given dataset.
4197  */
4198 int
4199 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4200     boolean_t force_unmount)
4201 {
4202 	int ret = 0;
4203 	zfs_cmd_t zc = { 0 };
4204 	char *delim;
4205 	prop_changelist_t *cl = NULL;
4206 	zfs_handle_t *zhrp = NULL;
4207 	char *parentname = NULL;
4208 	char parent[ZFS_MAX_DATASET_NAME_LEN];
4209 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4210 	char errbuf[1024];
4211 
4212 	/* if we have the same exact name, just return success */
4213 	if (strcmp(zhp->zfs_name, target) == 0)
4214 		return (0);
4215 
4216 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4217 	    "cannot rename to '%s'"), target);
4218 
4219 	/* make sure source name is valid */
4220 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4221 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4222 
4223 	/*
4224 	 * Make sure the target name is valid
4225 	 */
4226 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4227 		if ((strchr(target, '@') == NULL) ||
4228 		    *target == '@') {
4229 			/*
4230 			 * Snapshot target name is abbreviated,
4231 			 * reconstruct full dataset name
4232 			 */
4233 			(void) strlcpy(parent, zhp->zfs_name,
4234 			    sizeof (parent));
4235 			delim = strchr(parent, '@');
4236 			if (strchr(target, '@') == NULL)
4237 				*(++delim) = '\0';
4238 			else
4239 				*delim = '\0';
4240 			(void) strlcat(parent, target, sizeof (parent));
4241 			target = parent;
4242 		} else {
4243 			/*
4244 			 * Make sure we're renaming within the same dataset.
4245 			 */
4246 			delim = strchr(target, '@');
4247 			if (strncmp(zhp->zfs_name, target, delim - target)
4248 			    != 0 || zhp->zfs_name[delim - target] != '@') {
4249 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4250 				    "snapshots must be part of same "
4251 				    "dataset"));
4252 				return (zfs_error(hdl, EZFS_CROSSTARGET,
4253 				    errbuf));
4254 			}
4255 		}
4256 
4257 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4258 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4259 	} else {
4260 		if (recursive) {
4261 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4262 			    "recursive rename must be a snapshot"));
4263 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4264 		}
4265 
4266 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4267 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4268 
4269 		/* validate parents */
4270 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4271 			return (-1);
4272 
4273 		/* make sure we're in the same pool */
4274 		verify((delim = strchr(target, '/')) != NULL);
4275 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4276 		    zhp->zfs_name[delim - target] != '/') {
4277 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4278 			    "datasets must be within same pool"));
4279 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4280 		}
4281 
4282 		/* new name cannot be a child of the current dataset name */
4283 		if (is_descendant(zhp->zfs_name, target)) {
4284 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4285 			    "New dataset name cannot be a descendant of "
4286 			    "current dataset name"));
4287 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4288 		}
4289 	}
4290 
4291 	(void) snprintf(errbuf, sizeof (errbuf),
4292 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4293 
4294 	if (getzoneid() == GLOBAL_ZONEID &&
4295 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4296 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4297 		    "dataset is used in a non-global zone"));
4298 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4299 	}
4300 
4301 	if (recursive) {
4302 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4303 		if (parentname == NULL) {
4304 			ret = -1;
4305 			goto error;
4306 		}
4307 		delim = strchr(parentname, '@');
4308 		*delim = '\0';
4309 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4310 		if (zhrp == NULL) {
4311 			ret = -1;
4312 			goto error;
4313 		}
4314 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4315 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4316 		    force_unmount ? MS_FORCE : 0)) == NULL)
4317 			return (-1);
4318 
4319 		if (changelist_haszonedchild(cl)) {
4320 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4321 			    "child dataset with inherited mountpoint is used "
4322 			    "in a non-global zone"));
4323 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4324 			ret = -1;
4325 			goto error;
4326 		}
4327 
4328 		if ((ret = changelist_prefix(cl)) != 0)
4329 			goto error;
4330 	}
4331 
4332 	if (ZFS_IS_VOLUME(zhp))
4333 		zc.zc_objset_type = DMU_OST_ZVOL;
4334 	else
4335 		zc.zc_objset_type = DMU_OST_ZFS;
4336 
4337 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4338 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4339 
4340 	zc.zc_cookie = recursive;
4341 
4342 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4343 		/*
4344 		 * if it was recursive, the one that actually failed will
4345 		 * be in zc.zc_name
4346 		 */
4347 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4348 		    "cannot rename '%s'"), zc.zc_name);
4349 
4350 		if (recursive && errno == EEXIST) {
4351 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4352 			    "a child dataset already has a snapshot "
4353 			    "with the new name"));
4354 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4355 		} else {
4356 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4357 		}
4358 
4359 		/*
4360 		 * On failure, we still want to remount any filesystems that
4361 		 * were previously mounted, so we don't alter the system state.
4362 		 */
4363 		if (cl != NULL)
4364 			(void) changelist_postfix(cl);
4365 	} else {
4366 		if (cl != NULL) {
4367 			changelist_rename(cl, zfs_get_name(zhp), target);
4368 			ret = changelist_postfix(cl);
4369 		}
4370 	}
4371 
4372 error:
4373 	if (parentname != NULL) {
4374 		free(parentname);
4375 	}
4376 	if (zhrp != NULL) {
4377 		zfs_close(zhrp);
4378 	}
4379 	if (cl != NULL) {
4380 		changelist_free(cl);
4381 	}
4382 	return (ret);
4383 }
4384 
4385 nvlist_t *
4386 zfs_get_user_props(zfs_handle_t *zhp)
4387 {
4388 	return (zhp->zfs_user_props);
4389 }
4390 
4391 nvlist_t *
4392 zfs_get_recvd_props(zfs_handle_t *zhp)
4393 {
4394 	if (zhp->zfs_recvd_props == NULL)
4395 		if (get_recvd_props_ioctl(zhp) != 0)
4396 			return (NULL);
4397 	return (zhp->zfs_recvd_props);
4398 }
4399 
4400 /*
4401  * This function is used by 'zfs list' to determine the exact set of columns to
4402  * display, and their maximum widths.  This does two main things:
4403  *
4404  *      - If this is a list of all properties, then expand the list to include
4405  *        all native properties, and set a flag so that for each dataset we look
4406  *        for new unique user properties and add them to the list.
4407  *
4408  *      - For non fixed-width properties, keep track of the maximum width seen
4409  *        so that we can size the column appropriately. If the user has
4410  *        requested received property values, we also need to compute the width
4411  *        of the RECEIVED column.
4412  */
4413 int
4414 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4415     boolean_t literal)
4416 {
4417 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4418 	zprop_list_t *entry;
4419 	zprop_list_t **last, **start;
4420 	nvlist_t *userprops, *propval;
4421 	nvpair_t *elem;
4422 	char *strval;
4423 	char buf[ZFS_MAXPROPLEN];
4424 
4425 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4426 		return (-1);
4427 
4428 	userprops = zfs_get_user_props(zhp);
4429 
4430 	entry = *plp;
4431 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4432 		/*
4433 		 * Go through and add any user properties as necessary.  We
4434 		 * start by incrementing our list pointer to the first
4435 		 * non-native property.
4436 		 */
4437 		start = plp;
4438 		while (*start != NULL) {
4439 			if ((*start)->pl_prop == ZPROP_INVAL)
4440 				break;
4441 			start = &(*start)->pl_next;
4442 		}
4443 
4444 		elem = NULL;
4445 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4446 			/*
4447 			 * See if we've already found this property in our list.
4448 			 */
4449 			for (last = start; *last != NULL;
4450 			    last = &(*last)->pl_next) {
4451 				if (strcmp((*last)->pl_user_prop,
4452 				    nvpair_name(elem)) == 0)
4453 					break;
4454 			}
4455 
4456 			if (*last == NULL) {
4457 				if ((entry = zfs_alloc(hdl,
4458 				    sizeof (zprop_list_t))) == NULL ||
4459 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4460 				    nvpair_name(elem)))) == NULL) {
4461 					free(entry);
4462 					return (-1);
4463 				}
4464 
4465 				entry->pl_prop = ZPROP_INVAL;
4466 				entry->pl_width = strlen(nvpair_name(elem));
4467 				entry->pl_all = B_TRUE;
4468 				*last = entry;
4469 			}
4470 		}
4471 	}
4472 
4473 	/*
4474 	 * Now go through and check the width of any non-fixed columns
4475 	 */
4476 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4477 		if (entry->pl_fixed && !literal)
4478 			continue;
4479 
4480 		if (entry->pl_prop != ZPROP_INVAL) {
4481 			if (zfs_prop_get(zhp, entry->pl_prop,
4482 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4483 				if (strlen(buf) > entry->pl_width)
4484 					entry->pl_width = strlen(buf);
4485 			}
4486 			if (received && zfs_prop_get_recvd(zhp,
4487 			    zfs_prop_to_name(entry->pl_prop),
4488 			    buf, sizeof (buf), literal) == 0)
4489 				if (strlen(buf) > entry->pl_recvd_width)
4490 					entry->pl_recvd_width = strlen(buf);
4491 		} else {
4492 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4493 			    &propval) == 0) {
4494 				verify(nvlist_lookup_string(propval,
4495 				    ZPROP_VALUE, &strval) == 0);
4496 				if (strlen(strval) > entry->pl_width)
4497 					entry->pl_width = strlen(strval);
4498 			}
4499 			if (received && zfs_prop_get_recvd(zhp,
4500 			    entry->pl_user_prop,
4501 			    buf, sizeof (buf), literal) == 0)
4502 				if (strlen(buf) > entry->pl_recvd_width)
4503 					entry->pl_recvd_width = strlen(buf);
4504 		}
4505 	}
4506 
4507 	return (0);
4508 }
4509 
4510 int
4511 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4512     char *resource, void *export, void *sharetab,
4513     int sharemax, zfs_share_op_t operation)
4514 {
4515 	zfs_cmd_t zc = { 0 };
4516 	int error;
4517 
4518 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4519 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4520 	if (resource)
4521 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4522 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4523 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4524 	zc.zc_share.z_sharetype = operation;
4525 	zc.zc_share.z_sharemax = sharemax;
4526 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4527 	return (error);
4528 }
4529 
4530 void
4531 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4532 {
4533 	nvpair_t *curr;
4534 
4535 	/*
4536 	 * Keep a reference to the props-table against which we prune the
4537 	 * properties.
4538 	 */
4539 	zhp->zfs_props_table = props;
4540 
4541 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4542 
4543 	while (curr) {
4544 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4545 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4546 
4547 		/*
4548 		 * User properties will result in ZPROP_INVAL, and since we
4549 		 * only know how to prune standard ZFS properties, we always
4550 		 * leave these in the list.  This can also happen if we
4551 		 * encounter an unknown DSL property (when running older
4552 		 * software, for example).
4553 		 */
4554 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4555 			(void) nvlist_remove(zhp->zfs_props,
4556 			    nvpair_name(curr), nvpair_type(curr));
4557 		curr = next;
4558 	}
4559 }
4560 
4561 static int
4562 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4563     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4564 {
4565 	zfs_cmd_t zc = { 0 };
4566 	nvlist_t *nvlist = NULL;
4567 	int error;
4568 
4569 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4570 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4571 	zc.zc_cookie = (uint64_t)cmd;
4572 
4573 	if (cmd == ZFS_SMB_ACL_RENAME) {
4574 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4575 			(void) no_memory(hdl);
4576 			return (0);
4577 		}
4578 	}
4579 
4580 	switch (cmd) {
4581 	case ZFS_SMB_ACL_ADD:
4582 	case ZFS_SMB_ACL_REMOVE:
4583 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4584 		break;
4585 	case ZFS_SMB_ACL_RENAME:
4586 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4587 		    resource1) != 0) {
4588 				(void) no_memory(hdl);
4589 				return (-1);
4590 		}
4591 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4592 		    resource2) != 0) {
4593 				(void) no_memory(hdl);
4594 				return (-1);
4595 		}
4596 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4597 			nvlist_free(nvlist);
4598 			return (-1);
4599 		}
4600 		break;
4601 	case ZFS_SMB_ACL_PURGE:
4602 		break;
4603 	default:
4604 		return (-1);
4605 	}
4606 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4607 	nvlist_free(nvlist);
4608 	return (error);
4609 }
4610 
4611 int
4612 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4613     char *path, char *resource)
4614 {
4615 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4616 	    resource, NULL));
4617 }
4618 
4619 int
4620 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4621     char *path, char *resource)
4622 {
4623 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4624 	    resource, NULL));
4625 }
4626 
4627 int
4628 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4629 {
4630 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4631 	    NULL, NULL));
4632 }
4633 
4634 int
4635 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4636     char *oldname, char *newname)
4637 {
4638 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4639 	    oldname, newname));
4640 }
4641 
4642 int
4643 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4644     zfs_userspace_cb_t func, void *arg)
4645 {
4646 	zfs_cmd_t zc = { 0 };
4647 	zfs_useracct_t buf[100];
4648 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4649 	int ret;
4650 
4651 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4652 
4653 	zc.zc_objset_type = type;
4654 	zc.zc_nvlist_dst = (uintptr_t)buf;
4655 
4656 	for (;;) {
4657 		zfs_useracct_t *zua = buf;
4658 
4659 		zc.zc_nvlist_dst_size = sizeof (buf);
4660 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4661 			char errbuf[1024];
4662 
4663 			(void) snprintf(errbuf, sizeof (errbuf),
4664 			    dgettext(TEXT_DOMAIN,
4665 			    "cannot get used/quota for %s"), zc.zc_name);
4666 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4667 		}
4668 		if (zc.zc_nvlist_dst_size == 0)
4669 			break;
4670 
4671 		while (zc.zc_nvlist_dst_size > 0) {
4672 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4673 			    zua->zu_space)) != 0)
4674 				return (ret);
4675 			zua++;
4676 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4677 		}
4678 	}
4679 
4680 	return (0);
4681 }
4682 
4683 struct holdarg {
4684 	nvlist_t *nvl;
4685 	const char *snapname;
4686 	const char *tag;
4687 	boolean_t recursive;
4688 	int error;
4689 };
4690 
4691 static int
4692 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4693 {
4694 	struct holdarg *ha = arg;
4695 	char name[ZFS_MAX_DATASET_NAME_LEN];
4696 	int rv = 0;
4697 
4698 	(void) snprintf(name, sizeof (name),
4699 	    "%s@%s", zhp->zfs_name, ha->snapname);
4700 
4701 	if (lzc_exists(name))
4702 		fnvlist_add_string(ha->nvl, name, ha->tag);
4703 
4704 	if (ha->recursive)
4705 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4706 	zfs_close(zhp);
4707 	return (rv);
4708 }
4709 
4710 int
4711 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4712     boolean_t recursive, int cleanup_fd)
4713 {
4714 	int ret;
4715 	struct holdarg ha;
4716 
4717 	ha.nvl = fnvlist_alloc();
4718 	ha.snapname = snapname;
4719 	ha.tag = tag;
4720 	ha.recursive = recursive;
4721 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4722 
4723 	if (nvlist_empty(ha.nvl)) {
4724 		char errbuf[1024];
4725 
4726 		fnvlist_free(ha.nvl);
4727 		ret = ENOENT;
4728 		(void) snprintf(errbuf, sizeof (errbuf),
4729 		    dgettext(TEXT_DOMAIN,
4730 		    "cannot hold snapshot '%s@%s'"),
4731 		    zhp->zfs_name, snapname);
4732 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4733 		return (ret);
4734 	}
4735 
4736 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4737 	fnvlist_free(ha.nvl);
4738 
4739 	return (ret);
4740 }
4741 
4742 int
4743 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4744 {
4745 	int ret;
4746 	nvlist_t *errors;
4747 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4748 	char errbuf[1024];
4749 	nvpair_t *elem;
4750 
4751 	errors = NULL;
4752 	ret = lzc_hold(holds, cleanup_fd, &errors);
4753 
4754 	if (ret == 0) {
4755 		/* There may be errors even in the success case. */
4756 		fnvlist_free(errors);
4757 		return (0);
4758 	}
4759 
4760 	if (nvlist_empty(errors)) {
4761 		/* no hold-specific errors */
4762 		(void) snprintf(errbuf, sizeof (errbuf),
4763 		    dgettext(TEXT_DOMAIN, "cannot hold"));
4764 		switch (ret) {
4765 		case ENOTSUP:
4766 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4767 			    "pool must be upgraded"));
4768 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4769 			break;
4770 		case EINVAL:
4771 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4772 			break;
4773 		default:
4774 			(void) zfs_standard_error(hdl, ret, errbuf);
4775 		}
4776 	}
4777 
4778 	for (elem = nvlist_next_nvpair(errors, NULL);
4779 	    elem != NULL;
4780 	    elem = nvlist_next_nvpair(errors, elem)) {
4781 		(void) snprintf(errbuf, sizeof (errbuf),
4782 		    dgettext(TEXT_DOMAIN,
4783 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
4784 		switch (fnvpair_value_int32(elem)) {
4785 		case E2BIG:
4786 			/*
4787 			 * Temporary tags wind up having the ds object id
4788 			 * prepended. So even if we passed the length check
4789 			 * above, it's still possible for the tag to wind
4790 			 * up being slightly too long.
4791 			 */
4792 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4793 			break;
4794 		case EINVAL:
4795 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4796 			break;
4797 		case EEXIST:
4798 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4799 			break;
4800 		default:
4801 			(void) zfs_standard_error(hdl,
4802 			    fnvpair_value_int32(elem), errbuf);
4803 		}
4804 	}
4805 
4806 	fnvlist_free(errors);
4807 	return (ret);
4808 }
4809 
4810 static int
4811 zfs_release_one(zfs_handle_t *zhp, void *arg)
4812 {
4813 	struct holdarg *ha = arg;
4814 	char name[ZFS_MAX_DATASET_NAME_LEN];
4815 	int rv = 0;
4816 	nvlist_t *existing_holds;
4817 
4818 	(void) snprintf(name, sizeof (name),
4819 	    "%s@%s", zhp->zfs_name, ha->snapname);
4820 
4821 	if (lzc_get_holds(name, &existing_holds) != 0) {
4822 		ha->error = ENOENT;
4823 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4824 		ha->error = ESRCH;
4825 	} else {
4826 		nvlist_t *torelease = fnvlist_alloc();
4827 		fnvlist_add_boolean(torelease, ha->tag);
4828 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4829 		fnvlist_free(torelease);
4830 	}
4831 
4832 	if (ha->recursive)
4833 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4834 	zfs_close(zhp);
4835 	return (rv);
4836 }
4837 
4838 int
4839 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4840     boolean_t recursive)
4841 {
4842 	int ret;
4843 	struct holdarg ha;
4844 	nvlist_t *errors = NULL;
4845 	nvpair_t *elem;
4846 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4847 	char errbuf[1024];
4848 
4849 	ha.nvl = fnvlist_alloc();
4850 	ha.snapname = snapname;
4851 	ha.tag = tag;
4852 	ha.recursive = recursive;
4853 	ha.error = 0;
4854 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4855 
4856 	if (nvlist_empty(ha.nvl)) {
4857 		fnvlist_free(ha.nvl);
4858 		ret = ha.error;
4859 		(void) snprintf(errbuf, sizeof (errbuf),
4860 		    dgettext(TEXT_DOMAIN,
4861 		    "cannot release hold from snapshot '%s@%s'"),
4862 		    zhp->zfs_name, snapname);
4863 		if (ret == ESRCH) {
4864 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4865 		} else {
4866 			(void) zfs_standard_error(hdl, ret, errbuf);
4867 		}
4868 		return (ret);
4869 	}
4870 
4871 	ret = lzc_release(ha.nvl, &errors);
4872 	fnvlist_free(ha.nvl);
4873 
4874 	if (ret == 0) {
4875 		/* There may be errors even in the success case. */
4876 		fnvlist_free(errors);
4877 		return (0);
4878 	}
4879 
4880 	if (nvlist_empty(errors)) {
4881 		/* no hold-specific errors */
4882 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4883 		    "cannot release"));
4884 		switch (errno) {
4885 		case ENOTSUP:
4886 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4887 			    "pool must be upgraded"));
4888 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4889 			break;
4890 		default:
4891 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4892 		}
4893 	}
4894 
4895 	for (elem = nvlist_next_nvpair(errors, NULL);
4896 	    elem != NULL;
4897 	    elem = nvlist_next_nvpair(errors, elem)) {
4898 		(void) snprintf(errbuf, sizeof (errbuf),
4899 		    dgettext(TEXT_DOMAIN,
4900 		    "cannot release hold from snapshot '%s'"),
4901 		    nvpair_name(elem));
4902 		switch (fnvpair_value_int32(elem)) {
4903 		case ESRCH:
4904 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4905 			break;
4906 		case EINVAL:
4907 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4908 			break;
4909 		default:
4910 			(void) zfs_standard_error_fmt(hdl,
4911 			    fnvpair_value_int32(elem), errbuf);
4912 		}
4913 	}
4914 
4915 	fnvlist_free(errors);
4916 	return (ret);
4917 }
4918 
4919 int
4920 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4921 {
4922 	zfs_cmd_t zc = { 0 };
4923 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4924 	int nvsz = 2048;
4925 	void *nvbuf;
4926 	int err = 0;
4927 	char errbuf[1024];
4928 
4929 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4930 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4931 
4932 tryagain:
4933 
4934 	nvbuf = malloc(nvsz);
4935 	if (nvbuf == NULL) {
4936 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4937 		goto out;
4938 	}
4939 
4940 	zc.zc_nvlist_dst_size = nvsz;
4941 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4942 
4943 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4944 
4945 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4946 		(void) snprintf(errbuf, sizeof (errbuf),
4947 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4948 		    zc.zc_name);
4949 		switch (errno) {
4950 		case ENOMEM:
4951 			free(nvbuf);
4952 			nvsz = zc.zc_nvlist_dst_size;
4953 			goto tryagain;
4954 
4955 		case ENOTSUP:
4956 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4957 			    "pool must be upgraded"));
4958 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4959 			break;
4960 		case EINVAL:
4961 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4962 			break;
4963 		case ENOENT:
4964 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4965 			break;
4966 		default:
4967 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4968 			break;
4969 		}
4970 	} else {
4971 		/* success */
4972 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4973 		if (rc) {
4974 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
4975 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
4976 			    zc.zc_name);
4977 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
4978 		}
4979 	}
4980 
4981 	free(nvbuf);
4982 out:
4983 	return (err);
4984 }
4985 
4986 int
4987 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4988 {
4989 	zfs_cmd_t zc = { 0 };
4990 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4991 	char *nvbuf;
4992 	char errbuf[1024];
4993 	size_t nvsz;
4994 	int err;
4995 
4996 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4997 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4998 
4999 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5000 	assert(err == 0);
5001 
5002 	nvbuf = malloc(nvsz);
5003 
5004 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5005 	assert(err == 0);
5006 
5007 	zc.zc_nvlist_src_size = nvsz;
5008 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
5009 	zc.zc_perm_action = un;
5010 
5011 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5012 
5013 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5014 		(void) snprintf(errbuf, sizeof (errbuf),
5015 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5016 		    zc.zc_name);
5017 		switch (errno) {
5018 		case ENOTSUP:
5019 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5020 			    "pool must be upgraded"));
5021 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5022 			break;
5023 		case EINVAL:
5024 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5025 			break;
5026 		case ENOENT:
5027 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5028 			break;
5029 		default:
5030 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
5031 			break;
5032 		}
5033 	}
5034 
5035 	free(nvbuf);
5036 
5037 	return (err);
5038 }
5039 
5040 int
5041 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5042 {
5043 	int err;
5044 	char errbuf[1024];
5045 
5046 	err = lzc_get_holds(zhp->zfs_name, nvl);
5047 
5048 	if (err != 0) {
5049 		libzfs_handle_t *hdl = zhp->zfs_hdl;
5050 
5051 		(void) snprintf(errbuf, sizeof (errbuf),
5052 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5053 		    zhp->zfs_name);
5054 		switch (err) {
5055 		case ENOTSUP:
5056 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5057 			    "pool must be upgraded"));
5058 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5059 			break;
5060 		case EINVAL:
5061 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5062 			break;
5063 		case ENOENT:
5064 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5065 			break;
5066 		default:
5067 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
5068 			break;
5069 		}
5070 	}
5071 
5072 	return (err);
5073 }
5074 
5075 /*
5076  * Convert the zvol's volume size to an appropriate reservation.
5077  * Note: If this routine is updated, it is necessary to update the ZFS test
5078  * suite's shell version in reservation.kshlib.
5079  */
5080 uint64_t
5081 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5082 {
5083 	uint64_t numdb;
5084 	uint64_t nblocks, volblocksize;
5085 	int ncopies;
5086 	char *strval;
5087 
5088 	if (nvlist_lookup_string(props,
5089 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5090 		ncopies = atoi(strval);
5091 	else
5092 		ncopies = 1;
5093 	if (nvlist_lookup_uint64(props,
5094 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5095 	    &volblocksize) != 0)
5096 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5097 	nblocks = volsize/volblocksize;
5098 	/* start with metadnode L0-L6 */
5099 	numdb = 7;
5100 	/* calculate number of indirects */
5101 	while (nblocks > 1) {
5102 		nblocks += DNODES_PER_LEVEL - 1;
5103 		nblocks /= DNODES_PER_LEVEL;
5104 		numdb += nblocks;
5105 	}
5106 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5107 	volsize *= ncopies;
5108 	/*
5109 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5110 	 * compressed, but in practice they compress down to about
5111 	 * 1100 bytes
5112 	 */
5113 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5114 	volsize += numdb;
5115 	return (volsize);
5116 }
5117