xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_dataset.c (revision e153cda9f9660e385e8f468253f80e59f5d454d7)
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 	case ZFS_PROP_CREATETXG:
2757 		/*
2758 		 * GUIDs are stored as numbers, but they are identifiers.
2759 		 * We don't want them to be pretty printed, because pretty
2760 		 * printing mangles the ID into a truncated and useless value.
2761 		 */
2762 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2763 			return (-1);
2764 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2765 		zcp_check(zhp, prop, val, NULL);
2766 		break;
2767 
2768 	default:
2769 		switch (zfs_prop_get_type(prop)) {
2770 		case PROP_TYPE_NUMBER:
2771 			if (get_numeric_property(zhp, prop, src,
2772 			    &source, &val) != 0) {
2773 				return (-1);
2774 			}
2775 
2776 			if (literal) {
2777 				(void) snprintf(propbuf, proplen, "%llu",
2778 				    (u_longlong_t)val);
2779 			} else {
2780 				zfs_nicenum(val, propbuf, proplen);
2781 			}
2782 			zcp_check(zhp, prop, val, NULL);
2783 			break;
2784 
2785 		case PROP_TYPE_STRING:
2786 			str = getprop_string(zhp, prop, &source);
2787 			if (str == NULL)
2788 				return (-1);
2789 
2790 			(void) strlcpy(propbuf, str, proplen);
2791 			zcp_check(zhp, prop, NULL, str);
2792 			break;
2793 
2794 		case PROP_TYPE_INDEX:
2795 			if (get_numeric_property(zhp, prop, src,
2796 			    &source, &val) != 0)
2797 				return (-1);
2798 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2799 				return (-1);
2800 
2801 			(void) strlcpy(propbuf, strval, proplen);
2802 			zcp_check(zhp, prop, NULL, strval);
2803 			break;
2804 
2805 		default:
2806 			abort();
2807 		}
2808 	}
2809 
2810 	get_source(zhp, src, source, statbuf, statlen);
2811 
2812 	return (0);
2813 }
2814 
2815 /*
2816  * Utility function to get the given numeric property.  Does no validation that
2817  * the given property is the appropriate type; should only be used with
2818  * hard-coded property types.
2819  */
2820 uint64_t
2821 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2822 {
2823 	char *source;
2824 	uint64_t val;
2825 
2826 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2827 
2828 	return (val);
2829 }
2830 
2831 int
2832 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2833 {
2834 	char buf[64];
2835 
2836 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2837 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2838 }
2839 
2840 /*
2841  * Similar to zfs_prop_get(), but returns the value as an integer.
2842  */
2843 int
2844 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2845     zprop_source_t *src, char *statbuf, size_t statlen)
2846 {
2847 	char *source;
2848 
2849 	/*
2850 	 * Check to see if this property applies to our object
2851 	 */
2852 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2853 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2854 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2855 		    zfs_prop_to_name(prop)));
2856 	}
2857 
2858 	if (src)
2859 		*src = ZPROP_SRC_NONE;
2860 
2861 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2862 		return (-1);
2863 
2864 	get_source(zhp, src, source, statbuf, statlen);
2865 
2866 	return (0);
2867 }
2868 
2869 static int
2870 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2871     char **domainp, idmap_rid_t *ridp)
2872 {
2873 	idmap_get_handle_t *get_hdl = NULL;
2874 	idmap_stat status;
2875 	int err = EINVAL;
2876 
2877 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2878 		goto out;
2879 
2880 	if (isuser) {
2881 		err = idmap_get_sidbyuid(get_hdl, id,
2882 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2883 	} else {
2884 		err = idmap_get_sidbygid(get_hdl, id,
2885 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2886 	}
2887 	if (err == IDMAP_SUCCESS &&
2888 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2889 	    status == IDMAP_SUCCESS)
2890 		err = 0;
2891 	else
2892 		err = EINVAL;
2893 out:
2894 	if (get_hdl)
2895 		idmap_get_destroy(get_hdl);
2896 	return (err);
2897 }
2898 
2899 /*
2900  * convert the propname into parameters needed by kernel
2901  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2902  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2903  */
2904 static int
2905 userquota_propname_decode(const char *propname, boolean_t zoned,
2906     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2907 {
2908 	zfs_userquota_prop_t type;
2909 	char *cp, *end;
2910 	char *numericsid = NULL;
2911 	boolean_t isuser;
2912 
2913 	domain[0] = '\0';
2914 	*ridp = 0;
2915 	/* Figure out the property type ({user|group}{quota|space}) */
2916 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2917 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2918 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
2919 			break;
2920 	}
2921 	if (type == ZFS_NUM_USERQUOTA_PROPS)
2922 		return (EINVAL);
2923 	*typep = type;
2924 
2925 	isuser = (type == ZFS_PROP_USERQUOTA ||
2926 	    type == ZFS_PROP_USERUSED);
2927 
2928 	cp = strchr(propname, '@') + 1;
2929 
2930 	if (strchr(cp, '@')) {
2931 		/*
2932 		 * It's a SID name (eg "user@domain") that needs to be
2933 		 * turned into S-1-domainID-RID.
2934 		 */
2935 		int flag = 0;
2936 		idmap_stat stat, map_stat;
2937 		uid_t pid;
2938 		idmap_rid_t rid;
2939 		idmap_get_handle_t *gh = NULL;
2940 
2941 		stat = idmap_get_create(&gh);
2942 		if (stat != IDMAP_SUCCESS) {
2943 			idmap_get_destroy(gh);
2944 			return (ENOMEM);
2945 		}
2946 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2947 			return (ENOENT);
2948 		if (isuser) {
2949 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2950 			if (stat < 0)
2951 				return (ENOENT);
2952 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2953 			    &rid, &map_stat);
2954 		} else {
2955 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2956 			if (stat < 0)
2957 				return (ENOENT);
2958 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2959 			    &rid, &map_stat);
2960 		}
2961 		if (stat < 0) {
2962 			idmap_get_destroy(gh);
2963 			return (ENOENT);
2964 		}
2965 		stat = idmap_get_mappings(gh);
2966 		idmap_get_destroy(gh);
2967 
2968 		if (stat < 0) {
2969 			return (ENOENT);
2970 		}
2971 		if (numericsid == NULL)
2972 			return (ENOENT);
2973 		cp = numericsid;
2974 		*ridp = rid;
2975 		/* will be further decoded below */
2976 	}
2977 
2978 	if (strncmp(cp, "S-1-", 4) == 0) {
2979 		/* It's a numeric SID (eg "S-1-234-567-89") */
2980 		(void) strlcpy(domain, cp, domainlen);
2981 		errno = 0;
2982 		if (*ridp == 0) {
2983 			cp = strrchr(domain, '-');
2984 			*cp = '\0';
2985 			cp++;
2986 			*ridp = strtoull(cp, &end, 10);
2987 		} else {
2988 			end = "";
2989 		}
2990 		if (numericsid) {
2991 			free(numericsid);
2992 			numericsid = NULL;
2993 		}
2994 		if (errno != 0 || *end != '\0')
2995 			return (EINVAL);
2996 	} else if (!isdigit(*cp)) {
2997 		/*
2998 		 * It's a user/group name (eg "user") that needs to be
2999 		 * turned into a uid/gid
3000 		 */
3001 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3002 			return (ENOENT);
3003 		if (isuser) {
3004 			struct passwd *pw;
3005 			pw = getpwnam(cp);
3006 			if (pw == NULL)
3007 				return (ENOENT);
3008 			*ridp = pw->pw_uid;
3009 		} else {
3010 			struct group *gr;
3011 			gr = getgrnam(cp);
3012 			if (gr == NULL)
3013 				return (ENOENT);
3014 			*ridp = gr->gr_gid;
3015 		}
3016 	} else {
3017 		/* It's a user/group ID (eg "12345"). */
3018 		uid_t id = strtoul(cp, &end, 10);
3019 		idmap_rid_t rid;
3020 		char *mapdomain;
3021 
3022 		if (*end != '\0')
3023 			return (EINVAL);
3024 		if (id > MAXUID) {
3025 			/* It's an ephemeral ID. */
3026 			if (idmap_id_to_numeric_domain_rid(id, isuser,
3027 			    &mapdomain, &rid) != 0)
3028 				return (ENOENT);
3029 			(void) strlcpy(domain, mapdomain, domainlen);
3030 			*ridp = rid;
3031 		} else {
3032 			*ridp = id;
3033 		}
3034 	}
3035 
3036 	ASSERT3P(numericsid, ==, NULL);
3037 	return (0);
3038 }
3039 
3040 static int
3041 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3042     uint64_t *propvalue, zfs_userquota_prop_t *typep)
3043 {
3044 	int err;
3045 	zfs_cmd_t zc = { 0 };
3046 
3047 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3048 
3049 	err = userquota_propname_decode(propname,
3050 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3051 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3052 	zc.zc_objset_type = *typep;
3053 	if (err)
3054 		return (err);
3055 
3056 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3057 	if (err)
3058 		return (err);
3059 
3060 	*propvalue = zc.zc_cookie;
3061 	return (0);
3062 }
3063 
3064 int
3065 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3066     uint64_t *propvalue)
3067 {
3068 	zfs_userquota_prop_t type;
3069 
3070 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3071 	    &type));
3072 }
3073 
3074 int
3075 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3076     char *propbuf, int proplen, boolean_t literal)
3077 {
3078 	int err;
3079 	uint64_t propvalue;
3080 	zfs_userquota_prop_t type;
3081 
3082 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3083 	    &type);
3084 
3085 	if (err)
3086 		return (err);
3087 
3088 	if (literal) {
3089 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3090 	} else if (propvalue == 0 &&
3091 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
3092 		(void) strlcpy(propbuf, "none", proplen);
3093 	} else {
3094 		zfs_nicenum(propvalue, propbuf, proplen);
3095 	}
3096 	return (0);
3097 }
3098 
3099 int
3100 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3101     uint64_t *propvalue)
3102 {
3103 	int err;
3104 	zfs_cmd_t zc = { 0 };
3105 	const char *snapname;
3106 
3107 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3108 
3109 	snapname = strchr(propname, '@') + 1;
3110 	if (strchr(snapname, '@')) {
3111 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3112 	} else {
3113 		/* snapname is the short name, append it to zhp's fsname */
3114 		char *cp;
3115 
3116 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
3117 		    sizeof (zc.zc_value));
3118 		cp = strchr(zc.zc_value, '@');
3119 		if (cp != NULL)
3120 			*cp = '\0';
3121 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3122 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3123 	}
3124 
3125 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3126 	if (err)
3127 		return (err);
3128 
3129 	*propvalue = zc.zc_cookie;
3130 	return (0);
3131 }
3132 
3133 int
3134 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3135     char *propbuf, int proplen, boolean_t literal)
3136 {
3137 	int err;
3138 	uint64_t propvalue;
3139 
3140 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3141 
3142 	if (err)
3143 		return (err);
3144 
3145 	if (literal) {
3146 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3147 	} else {
3148 		zfs_nicenum(propvalue, propbuf, proplen);
3149 	}
3150 	return (0);
3151 }
3152 
3153 /*
3154  * Returns the name of the given zfs handle.
3155  */
3156 const char *
3157 zfs_get_name(const zfs_handle_t *zhp)
3158 {
3159 	return (zhp->zfs_name);
3160 }
3161 
3162 /*
3163  * Returns the name of the parent pool for the given zfs handle.
3164  */
3165 const char *
3166 zfs_get_pool_name(const zfs_handle_t *zhp)
3167 {
3168 	return (zhp->zpool_hdl->zpool_name);
3169 }
3170 
3171 /*
3172  * Returns the type of the given zfs handle.
3173  */
3174 zfs_type_t
3175 zfs_get_type(const zfs_handle_t *zhp)
3176 {
3177 	return (zhp->zfs_type);
3178 }
3179 
3180 /*
3181  * Is one dataset name a child dataset of another?
3182  *
3183  * Needs to handle these cases:
3184  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3185  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3186  * Descendant?	No.		No.		No.		Yes.
3187  */
3188 static boolean_t
3189 is_descendant(const char *ds1, const char *ds2)
3190 {
3191 	size_t d1len = strlen(ds1);
3192 
3193 	/* ds2 can't be a descendant if it's smaller */
3194 	if (strlen(ds2) < d1len)
3195 		return (B_FALSE);
3196 
3197 	/* otherwise, compare strings and verify that there's a '/' char */
3198 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3199 }
3200 
3201 /*
3202  * Given a complete name, return just the portion that refers to the parent.
3203  * Will return -1 if there is no parent (path is just the name of the
3204  * pool).
3205  */
3206 static int
3207 parent_name(const char *path, char *buf, size_t buflen)
3208 {
3209 	char *slashp;
3210 
3211 	(void) strlcpy(buf, path, buflen);
3212 
3213 	if ((slashp = strrchr(buf, '/')) == NULL)
3214 		return (-1);
3215 	*slashp = '\0';
3216 
3217 	return (0);
3218 }
3219 
3220 /*
3221  * If accept_ancestor is false, then check to make sure that the given path has
3222  * a parent, and that it exists.  If accept_ancestor is true, then find the
3223  * closest existing ancestor for the given path.  In prefixlen return the
3224  * length of already existing prefix of the given path.  We also fetch the
3225  * 'zoned' property, which is used to validate property settings when creating
3226  * new datasets.
3227  */
3228 static int
3229 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3230     boolean_t accept_ancestor, int *prefixlen)
3231 {
3232 	zfs_cmd_t zc = { 0 };
3233 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3234 	char *slash;
3235 	zfs_handle_t *zhp;
3236 	char errbuf[1024];
3237 	uint64_t is_zoned;
3238 
3239 	(void) snprintf(errbuf, sizeof (errbuf),
3240 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3241 
3242 	/* get parent, and check to see if this is just a pool */
3243 	if (parent_name(path, parent, sizeof (parent)) != 0) {
3244 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3245 		    "missing dataset name"));
3246 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3247 	}
3248 
3249 	/* check to see if the pool exists */
3250 	if ((slash = strchr(parent, '/')) == NULL)
3251 		slash = parent + strlen(parent);
3252 	(void) strncpy(zc.zc_name, parent, slash - parent);
3253 	zc.zc_name[slash - parent] = '\0';
3254 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3255 	    errno == ENOENT) {
3256 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3257 		    "no such pool '%s'"), zc.zc_name);
3258 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3259 	}
3260 
3261 	/* check to see if the parent dataset exists */
3262 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3263 		if (errno == ENOENT && accept_ancestor) {
3264 			/*
3265 			 * Go deeper to find an ancestor, give up on top level.
3266 			 */
3267 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
3268 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3269 				    "no such pool '%s'"), zc.zc_name);
3270 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
3271 			}
3272 		} else if (errno == ENOENT) {
3273 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3274 			    "parent does not exist"));
3275 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3276 		} else
3277 			return (zfs_standard_error(hdl, errno, errbuf));
3278 	}
3279 
3280 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3281 	if (zoned != NULL)
3282 		*zoned = is_zoned;
3283 
3284 	/* we are in a non-global zone, but parent is in the global zone */
3285 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3286 		(void) zfs_standard_error(hdl, EPERM, errbuf);
3287 		zfs_close(zhp);
3288 		return (-1);
3289 	}
3290 
3291 	/* make sure parent is a filesystem */
3292 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3293 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3294 		    "parent is not a filesystem"));
3295 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3296 		zfs_close(zhp);
3297 		return (-1);
3298 	}
3299 
3300 	zfs_close(zhp);
3301 	if (prefixlen != NULL)
3302 		*prefixlen = strlen(parent);
3303 	return (0);
3304 }
3305 
3306 /*
3307  * Finds whether the dataset of the given type(s) exists.
3308  */
3309 boolean_t
3310 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3311 {
3312 	zfs_handle_t *zhp;
3313 
3314 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
3315 		return (B_FALSE);
3316 
3317 	/*
3318 	 * Try to get stats for the dataset, which will tell us if it exists.
3319 	 */
3320 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3321 		int ds_type = zhp->zfs_type;
3322 
3323 		zfs_close(zhp);
3324 		if (types & ds_type)
3325 			return (B_TRUE);
3326 	}
3327 	return (B_FALSE);
3328 }
3329 
3330 /*
3331  * Given a path to 'target', create all the ancestors between
3332  * the prefixlen portion of the path, and the target itself.
3333  * Fail if the initial prefixlen-ancestor does not already exist.
3334  */
3335 int
3336 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3337 {
3338 	zfs_handle_t *h;
3339 	char *cp;
3340 	const char *opname;
3341 
3342 	/* make sure prefix exists */
3343 	cp = target + prefixlen;
3344 	if (*cp != '/') {
3345 		assert(strchr(cp, '/') == NULL);
3346 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3347 	} else {
3348 		*cp = '\0';
3349 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3350 		*cp = '/';
3351 	}
3352 	if (h == NULL)
3353 		return (-1);
3354 	zfs_close(h);
3355 
3356 	/*
3357 	 * Attempt to create, mount, and share any ancestor filesystems,
3358 	 * up to the prefixlen-long one.
3359 	 */
3360 	for (cp = target + prefixlen + 1;
3361 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3362 
3363 		*cp = '\0';
3364 
3365 		h = make_dataset_handle(hdl, target);
3366 		if (h) {
3367 			/* it already exists, nothing to do here */
3368 			zfs_close(h);
3369 			continue;
3370 		}
3371 
3372 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3373 		    NULL) != 0) {
3374 			opname = dgettext(TEXT_DOMAIN, "create");
3375 			goto ancestorerr;
3376 		}
3377 
3378 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3379 		if (h == NULL) {
3380 			opname = dgettext(TEXT_DOMAIN, "open");
3381 			goto ancestorerr;
3382 		}
3383 
3384 		if (zfs_mount(h, NULL, 0) != 0) {
3385 			opname = dgettext(TEXT_DOMAIN, "mount");
3386 			goto ancestorerr;
3387 		}
3388 
3389 		if (zfs_share(h) != 0) {
3390 			opname = dgettext(TEXT_DOMAIN, "share");
3391 			goto ancestorerr;
3392 		}
3393 
3394 		zfs_close(h);
3395 	}
3396 
3397 	return (0);
3398 
3399 ancestorerr:
3400 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3401 	    "failed to %s ancestor '%s'"), opname, target);
3402 	return (-1);
3403 }
3404 
3405 /*
3406  * Creates non-existing ancestors of the given path.
3407  */
3408 int
3409 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3410 {
3411 	int prefix;
3412 	char *path_copy;
3413 	char errbuf[1024];
3414 	int rc = 0;
3415 
3416 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3417 	    "cannot create '%s'"), path);
3418 
3419 	/*
3420 	 * Check that we are not passing the nesting limit
3421 	 * before we start creating any ancestors.
3422 	 */
3423 	if (dataset_nestcheck(path) != 0) {
3424 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3425 		    "maximum name nesting depth exceeded"));
3426 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3427 	}
3428 
3429 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3430 		return (-1);
3431 
3432 	if ((path_copy = strdup(path)) != NULL) {
3433 		rc = create_parents(hdl, path_copy, prefix);
3434 		free(path_copy);
3435 	}
3436 	if (path_copy == NULL || rc != 0)
3437 		return (-1);
3438 
3439 	return (0);
3440 }
3441 
3442 /*
3443  * Create a new filesystem or volume.
3444  */
3445 int
3446 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3447     nvlist_t *props)
3448 {
3449 	int ret;
3450 	uint64_t size = 0;
3451 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3452 	char errbuf[1024];
3453 	uint64_t zoned;
3454 	enum lzc_dataset_type ost;
3455 	zpool_handle_t *zpool_handle;
3456 
3457 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3458 	    "cannot create '%s'"), path);
3459 
3460 	/* validate the path, taking care to note the extended error message */
3461 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
3462 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3463 
3464 	if (dataset_nestcheck(path) != 0) {
3465 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3466 		    "maximum name nesting depth exceeded"));
3467 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3468 	}
3469 
3470 	/* validate parents exist */
3471 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3472 		return (-1);
3473 
3474 	/*
3475 	 * The failure modes when creating a dataset of a different type over
3476 	 * one that already exists is a little strange.  In particular, if you
3477 	 * try to create a dataset on top of an existing dataset, the ioctl()
3478 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3479 	 * first try to see if the dataset exists.
3480 	 */
3481 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3482 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3483 		    "dataset already exists"));
3484 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3485 	}
3486 
3487 	if (type == ZFS_TYPE_VOLUME)
3488 		ost = LZC_DATSET_TYPE_ZVOL;
3489 	else
3490 		ost = LZC_DATSET_TYPE_ZFS;
3491 
3492 	/* open zpool handle for prop validation */
3493 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3494 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3495 
3496 	/* truncate pool_path at first slash */
3497 	char *p = strchr(pool_path, '/');
3498 	if (p != NULL)
3499 		*p = '\0';
3500 
3501 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3502 		return (-1);
3503 
3504 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3505 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
3506 		zpool_close(zpool_handle);
3507 		return (-1);
3508 	}
3509 	zpool_close(zpool_handle);
3510 
3511 	if (type == ZFS_TYPE_VOLUME) {
3512 		/*
3513 		 * If we are creating a volume, the size and block size must
3514 		 * satisfy a few restraints.  First, the blocksize must be a
3515 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3516 		 * volsize must be a multiple of the block size, and cannot be
3517 		 * zero.
3518 		 */
3519 		if (props == NULL || nvlist_lookup_uint64(props,
3520 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3521 			nvlist_free(props);
3522 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3523 			    "missing volume size"));
3524 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3525 		}
3526 
3527 		if ((ret = nvlist_lookup_uint64(props,
3528 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3529 		    &blocksize)) != 0) {
3530 			if (ret == ENOENT) {
3531 				blocksize = zfs_prop_default_numeric(
3532 				    ZFS_PROP_VOLBLOCKSIZE);
3533 			} else {
3534 				nvlist_free(props);
3535 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3536 				    "missing volume block size"));
3537 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3538 			}
3539 		}
3540 
3541 		if (size == 0) {
3542 			nvlist_free(props);
3543 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3544 			    "volume size cannot be zero"));
3545 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3546 		}
3547 
3548 		if (size % blocksize != 0) {
3549 			nvlist_free(props);
3550 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3551 			    "volume size must be a multiple of volume block "
3552 			    "size"));
3553 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3554 		}
3555 	}
3556 
3557 	/* create the dataset */
3558 	ret = lzc_create(path, ost, props);
3559 	nvlist_free(props);
3560 
3561 	/* check for failure */
3562 	if (ret != 0) {
3563 		char parent[ZFS_MAX_DATASET_NAME_LEN];
3564 		(void) parent_name(path, parent, sizeof (parent));
3565 
3566 		switch (errno) {
3567 		case ENOENT:
3568 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3569 			    "no such parent '%s'"), parent);
3570 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3571 
3572 		case EINVAL:
3573 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3574 			    "parent '%s' is not a filesystem"), parent);
3575 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3576 
3577 		case ENOTSUP:
3578 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3579 			    "pool must be upgraded to set this "
3580 			    "property or value"));
3581 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3582 		case ERANGE:
3583 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3584 			    "invalid property value(s) specified"));
3585 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3586 #ifdef _ILP32
3587 		case EOVERFLOW:
3588 			/*
3589 			 * This platform can't address a volume this big.
3590 			 */
3591 			if (type == ZFS_TYPE_VOLUME)
3592 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
3593 				    errbuf));
3594 #endif
3595 			/* FALLTHROUGH */
3596 		default:
3597 			return (zfs_standard_error(hdl, errno, errbuf));
3598 		}
3599 	}
3600 
3601 	return (0);
3602 }
3603 
3604 /*
3605  * Destroys the given dataset.  The caller must make sure that the filesystem
3606  * isn't mounted, and that there are no active dependents. If the file system
3607  * does not exist this function does nothing.
3608  */
3609 int
3610 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3611 {
3612 	zfs_cmd_t zc = { 0 };
3613 
3614 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3615 		nvlist_t *nv = fnvlist_alloc();
3616 		fnvlist_add_boolean(nv, zhp->zfs_name);
3617 		int error = lzc_destroy_bookmarks(nv, NULL);
3618 		fnvlist_free(nv);
3619 		if (error != 0) {
3620 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3621 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3622 			    zhp->zfs_name));
3623 		}
3624 		return (0);
3625 	}
3626 
3627 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3628 
3629 	if (ZFS_IS_VOLUME(zhp)) {
3630 		zc.zc_objset_type = DMU_OST_ZVOL;
3631 	} else {
3632 		zc.zc_objset_type = DMU_OST_ZFS;
3633 	}
3634 
3635 	zc.zc_defer_destroy = defer;
3636 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3637 	    errno != ENOENT) {
3638 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3639 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3640 		    zhp->zfs_name));
3641 	}
3642 
3643 	remove_mountpoint(zhp);
3644 
3645 	return (0);
3646 }
3647 
3648 struct destroydata {
3649 	nvlist_t *nvl;
3650 	const char *snapname;
3651 };
3652 
3653 static int
3654 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3655 {
3656 	struct destroydata *dd = arg;
3657 	char name[ZFS_MAX_DATASET_NAME_LEN];
3658 	int rv = 0;
3659 
3660 	(void) snprintf(name, sizeof (name),
3661 	    "%s@%s", zhp->zfs_name, dd->snapname);
3662 
3663 	if (lzc_exists(name))
3664 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
3665 
3666 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3667 	zfs_close(zhp);
3668 	return (rv);
3669 }
3670 
3671 /*
3672  * Destroys all snapshots with the given name in zhp & descendants.
3673  */
3674 int
3675 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3676 {
3677 	int ret;
3678 	struct destroydata dd = { 0 };
3679 
3680 	dd.snapname = snapname;
3681 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3682 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3683 
3684 	if (nvlist_empty(dd.nvl)) {
3685 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3686 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3687 		    zhp->zfs_name, snapname);
3688 	} else {
3689 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3690 	}
3691 	nvlist_free(dd.nvl);
3692 	return (ret);
3693 }
3694 
3695 /*
3696  * Destroys all the snapshots named in the nvlist.
3697  */
3698 int
3699 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3700 {
3701 	int ret;
3702 	nvlist_t *errlist = NULL;
3703 
3704 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
3705 
3706 	if (ret == 0) {
3707 		nvlist_free(errlist);
3708 		return (0);
3709 	}
3710 
3711 	if (nvlist_empty(errlist)) {
3712 		char errbuf[1024];
3713 		(void) snprintf(errbuf, sizeof (errbuf),
3714 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3715 
3716 		ret = zfs_standard_error(hdl, ret, errbuf);
3717 	}
3718 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3719 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3720 		char errbuf[1024];
3721 		(void) snprintf(errbuf, sizeof (errbuf),
3722 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3723 		    nvpair_name(pair));
3724 
3725 		switch (fnvpair_value_int32(pair)) {
3726 		case EEXIST:
3727 			zfs_error_aux(hdl,
3728 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3729 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3730 			break;
3731 		default:
3732 			ret = zfs_standard_error(hdl, errno, errbuf);
3733 			break;
3734 		}
3735 	}
3736 
3737 	nvlist_free(errlist);
3738 	return (ret);
3739 }
3740 
3741 /*
3742  * Clones the given dataset.  The target must be of the same type as the source.
3743  */
3744 int
3745 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3746 {
3747 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3748 	int ret;
3749 	char errbuf[1024];
3750 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3751 	uint64_t zoned;
3752 
3753 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3754 
3755 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3756 	    "cannot create '%s'"), target);
3757 
3758 	/* validate the target/clone name */
3759 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3760 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3761 
3762 	/* validate parents exist */
3763 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3764 		return (-1);
3765 
3766 	(void) parent_name(target, parent, sizeof (parent));
3767 
3768 	/* do the clone */
3769 
3770 	if (props) {
3771 		zfs_type_t type;
3772 
3773 		if (ZFS_IS_VOLUME(zhp)) {
3774 			type = ZFS_TYPE_VOLUME;
3775 		} else {
3776 			type = ZFS_TYPE_FILESYSTEM;
3777 		}
3778 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3779 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
3780 			return (-1);
3781 		if (zfs_fix_auto_resv(zhp, props) == -1) {
3782 			nvlist_free(props);
3783 			return (-1);
3784 		}
3785 	}
3786 
3787 	ret = lzc_clone(target, zhp->zfs_name, props);
3788 	nvlist_free(props);
3789 
3790 	if (ret != 0) {
3791 		switch (errno) {
3792 
3793 		case ENOENT:
3794 			/*
3795 			 * The parent doesn't exist.  We should have caught this
3796 			 * above, but there may a race condition that has since
3797 			 * destroyed the parent.
3798 			 *
3799 			 * At this point, we don't know whether it's the source
3800 			 * that doesn't exist anymore, or whether the target
3801 			 * dataset doesn't exist.
3802 			 */
3803 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3804 			    "no such parent '%s'"), parent);
3805 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3806 
3807 		case EXDEV:
3808 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3809 			    "source and target pools differ"));
3810 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3811 			    errbuf));
3812 
3813 		default:
3814 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3815 			    errbuf));
3816 		}
3817 	}
3818 
3819 	return (ret);
3820 }
3821 
3822 /*
3823  * Promotes the given clone fs to be the clone parent.
3824  */
3825 int
3826 zfs_promote(zfs_handle_t *zhp)
3827 {
3828 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3829 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3830 	int ret;
3831 	char errbuf[1024];
3832 
3833 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3834 	    "cannot promote '%s'"), zhp->zfs_name);
3835 
3836 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3837 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3838 		    "snapshots can not be promoted"));
3839 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3840 	}
3841 
3842 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3843 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3844 		    "not a cloned filesystem"));
3845 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3846 	}
3847 
3848 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3849 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3850 
3851 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3852 
3853 	if (ret != 0) {
3854 		switch (ret) {
3855 		case EEXIST:
3856 			/* There is a conflicting snapshot name. */
3857 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3858 			    "conflicting snapshot '%s' from parent '%s'"),
3859 			    snapname, zhp->zfs_dmustats.dds_origin);
3860 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3861 
3862 		default:
3863 			return (zfs_standard_error(hdl, ret, errbuf));
3864 		}
3865 	}
3866 	return (ret);
3867 }
3868 
3869 typedef struct snapdata {
3870 	nvlist_t *sd_nvl;
3871 	const char *sd_snapname;
3872 } snapdata_t;
3873 
3874 static int
3875 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3876 {
3877 	snapdata_t *sd = arg;
3878 	char name[ZFS_MAX_DATASET_NAME_LEN];
3879 	int rv = 0;
3880 
3881 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3882 		(void) snprintf(name, sizeof (name),
3883 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3884 
3885 		fnvlist_add_boolean(sd->sd_nvl, name);
3886 
3887 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3888 	}
3889 	zfs_close(zhp);
3890 
3891 	return (rv);
3892 }
3893 
3894 int
3895 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3896 {
3897 	int err;
3898 	char errbuf[1024];
3899 
3900 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3901 	    "cannot remap dataset '%s'"), fs);
3902 
3903 	err = lzc_remap(fs);
3904 
3905 	if (err != 0) {
3906 		switch (err) {
3907 		case ENOTSUP:
3908 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3909 			    "pool must be upgraded"));
3910 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3911 			break;
3912 		case EINVAL:
3913 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3914 			break;
3915 		default:
3916 			(void) zfs_standard_error(hdl, err, errbuf);
3917 			break;
3918 		}
3919 	}
3920 
3921 	return (err);
3922 }
3923 
3924 /*
3925  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3926  * created.
3927  */
3928 int
3929 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3930 {
3931 	int ret;
3932 	char errbuf[1024];
3933 	nvpair_t *elem;
3934 	nvlist_t *errors;
3935 
3936 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3937 	    "cannot create snapshots "));
3938 
3939 	elem = NULL;
3940 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3941 		const char *snapname = nvpair_name(elem);
3942 
3943 		/* validate the target name */
3944 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3945 		    B_TRUE)) {
3946 			(void) snprintf(errbuf, sizeof (errbuf),
3947 			    dgettext(TEXT_DOMAIN,
3948 			    "cannot create snapshot '%s'"), snapname);
3949 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3950 		}
3951 	}
3952 
3953 	/*
3954 	 * get pool handle for prop validation. assumes all snaps are in the
3955 	 * same pool, as does lzc_snapshot (below).
3956 	 */
3957 	char pool[ZFS_MAX_DATASET_NAME_LEN];
3958 	elem = nvlist_next_nvpair(snaps, NULL);
3959 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3960 	pool[strcspn(pool, "/@")] = '\0';
3961 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3962 
3963 	if (props != NULL &&
3964 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3965 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3966 		zpool_close(zpool_hdl);
3967 		return (-1);
3968 	}
3969 	zpool_close(zpool_hdl);
3970 
3971 	ret = lzc_snapshot(snaps, props, &errors);
3972 
3973 	if (ret != 0) {
3974 		boolean_t printed = B_FALSE;
3975 		for (elem = nvlist_next_nvpair(errors, NULL);
3976 		    elem != NULL;
3977 		    elem = nvlist_next_nvpair(errors, elem)) {
3978 			(void) snprintf(errbuf, sizeof (errbuf),
3979 			    dgettext(TEXT_DOMAIN,
3980 			    "cannot create snapshot '%s'"), nvpair_name(elem));
3981 			(void) zfs_standard_error(hdl,
3982 			    fnvpair_value_int32(elem), errbuf);
3983 			printed = B_TRUE;
3984 		}
3985 		if (!printed) {
3986 			switch (ret) {
3987 			case EXDEV:
3988 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3989 				    "multiple snapshots of same "
3990 				    "fs not allowed"));
3991 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3992 
3993 				break;
3994 			default:
3995 				(void) zfs_standard_error(hdl, ret, errbuf);
3996 			}
3997 		}
3998 	}
3999 
4000 	nvlist_free(props);
4001 	nvlist_free(errors);
4002 	return (ret);
4003 }
4004 
4005 int
4006 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4007     nvlist_t *props)
4008 {
4009 	int ret;
4010 	snapdata_t sd = { 0 };
4011 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
4012 	char *cp;
4013 	zfs_handle_t *zhp;
4014 	char errbuf[1024];
4015 
4016 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4017 	    "cannot snapshot %s"), path);
4018 
4019 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4020 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4021 
4022 	(void) strlcpy(fsname, path, sizeof (fsname));
4023 	cp = strchr(fsname, '@');
4024 	*cp = '\0';
4025 	sd.sd_snapname = cp + 1;
4026 
4027 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4028 	    ZFS_TYPE_VOLUME)) == NULL) {
4029 		return (-1);
4030 	}
4031 
4032 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4033 	if (recursive) {
4034 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4035 	} else {
4036 		fnvlist_add_boolean(sd.sd_nvl, path);
4037 	}
4038 
4039 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4040 	nvlist_free(sd.sd_nvl);
4041 	zfs_close(zhp);
4042 	return (ret);
4043 }
4044 
4045 /*
4046  * Destroy any more recent snapshots.  We invoke this callback on any dependents
4047  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
4048  * is a dependent and we should just destroy it without checking the transaction
4049  * group.
4050  */
4051 typedef struct rollback_data {
4052 	const char	*cb_target;		/* the snapshot */
4053 	uint64_t	cb_create;		/* creation time reference */
4054 	boolean_t	cb_error;
4055 	boolean_t	cb_force;
4056 } rollback_data_t;
4057 
4058 static int
4059 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4060 {
4061 	rollback_data_t *cbp = data;
4062 	prop_changelist_t *clp;
4063 
4064 	/* We must destroy this clone; first unmount it */
4065 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4066 	    cbp->cb_force ? MS_FORCE: 0);
4067 	if (clp == NULL || changelist_prefix(clp) != 0) {
4068 		cbp->cb_error = B_TRUE;
4069 		zfs_close(zhp);
4070 		return (0);
4071 	}
4072 	if (zfs_destroy(zhp, B_FALSE) != 0)
4073 		cbp->cb_error = B_TRUE;
4074 	else
4075 		changelist_remove(clp, zhp->zfs_name);
4076 	(void) changelist_postfix(clp);
4077 	changelist_free(clp);
4078 
4079 	zfs_close(zhp);
4080 	return (0);
4081 }
4082 
4083 static int
4084 rollback_destroy(zfs_handle_t *zhp, void *data)
4085 {
4086 	rollback_data_t *cbp = data;
4087 
4088 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4089 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4090 		    rollback_destroy_dependent, cbp);
4091 
4092 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4093 	}
4094 
4095 	zfs_close(zhp);
4096 	return (0);
4097 }
4098 
4099 /*
4100  * Given a dataset, rollback to a specific snapshot, discarding any
4101  * data changes since then and making it the active dataset.
4102  *
4103  * Any snapshots and bookmarks more recent than the target are
4104  * destroyed, along with their dependents (i.e. clones).
4105  */
4106 int
4107 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4108 {
4109 	rollback_data_t cb = { 0 };
4110 	int err;
4111 	boolean_t restore_resv = 0;
4112 	uint64_t old_volsize = 0, new_volsize;
4113 	zfs_prop_t resv_prop;
4114 
4115 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4116 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
4117 
4118 	/*
4119 	 * Destroy all recent snapshots and their dependents.
4120 	 */
4121 	cb.cb_force = force;
4122 	cb.cb_target = snap->zfs_name;
4123 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4124 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4125 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4126 
4127 	if (cb.cb_error)
4128 		return (-1);
4129 
4130 	/*
4131 	 * Now that we have verified that the snapshot is the latest,
4132 	 * rollback to the given snapshot.
4133 	 */
4134 
4135 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4136 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4137 			return (-1);
4138 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4139 		restore_resv =
4140 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4141 	}
4142 
4143 	/*
4144 	 * Pass both the filesystem and the wanted snapshot names,
4145 	 * we would get an error back if the snapshot is destroyed or
4146 	 * a new snapshot is created before this request is processed.
4147 	 */
4148 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4149 	if (err != 0) {
4150 		char errbuf[1024];
4151 
4152 		(void) snprintf(errbuf, sizeof (errbuf),
4153 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4154 		    zhp->zfs_name);
4155 		switch (err) {
4156 		case EEXIST:
4157 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4158 			    "there is a snapshot or bookmark more recent "
4159 			    "than '%s'"), snap->zfs_name);
4160 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4161 			break;
4162 		case ESRCH:
4163 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4164 			    "'%s' is not found among snapshots of '%s'"),
4165 			    snap->zfs_name, zhp->zfs_name);
4166 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4167 			break;
4168 		case EINVAL:
4169 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4170 			break;
4171 		default:
4172 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4173 		}
4174 		return (err);
4175 	}
4176 
4177 	/*
4178 	 * For volumes, if the pre-rollback volsize matched the pre-
4179 	 * rollback reservation and the volsize has changed then set
4180 	 * the reservation property to the post-rollback volsize.
4181 	 * Make a new handle since the rollback closed the dataset.
4182 	 */
4183 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4184 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4185 		if (restore_resv) {
4186 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4187 			if (old_volsize != new_volsize)
4188 				err = zfs_prop_set_int(zhp, resv_prop,
4189 				    new_volsize);
4190 		}
4191 		zfs_close(zhp);
4192 	}
4193 	return (err);
4194 }
4195 
4196 /*
4197  * Renames the given dataset.
4198  */
4199 int
4200 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4201     boolean_t force_unmount)
4202 {
4203 	int ret = 0;
4204 	zfs_cmd_t zc = { 0 };
4205 	char *delim;
4206 	prop_changelist_t *cl = NULL;
4207 	zfs_handle_t *zhrp = NULL;
4208 	char *parentname = NULL;
4209 	char parent[ZFS_MAX_DATASET_NAME_LEN];
4210 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4211 	char errbuf[1024];
4212 
4213 	/* if we have the same exact name, just return success */
4214 	if (strcmp(zhp->zfs_name, target) == 0)
4215 		return (0);
4216 
4217 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4218 	    "cannot rename to '%s'"), target);
4219 
4220 	/* make sure source name is valid */
4221 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4222 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4223 
4224 	/*
4225 	 * Make sure the target name is valid
4226 	 */
4227 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4228 		if ((strchr(target, '@') == NULL) ||
4229 		    *target == '@') {
4230 			/*
4231 			 * Snapshot target name is abbreviated,
4232 			 * reconstruct full dataset name
4233 			 */
4234 			(void) strlcpy(parent, zhp->zfs_name,
4235 			    sizeof (parent));
4236 			delim = strchr(parent, '@');
4237 			if (strchr(target, '@') == NULL)
4238 				*(++delim) = '\0';
4239 			else
4240 				*delim = '\0';
4241 			(void) strlcat(parent, target, sizeof (parent));
4242 			target = parent;
4243 		} else {
4244 			/*
4245 			 * Make sure we're renaming within the same dataset.
4246 			 */
4247 			delim = strchr(target, '@');
4248 			if (strncmp(zhp->zfs_name, target, delim - target)
4249 			    != 0 || zhp->zfs_name[delim - target] != '@') {
4250 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4251 				    "snapshots must be part of same "
4252 				    "dataset"));
4253 				return (zfs_error(hdl, EZFS_CROSSTARGET,
4254 				    errbuf));
4255 			}
4256 		}
4257 
4258 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4259 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4260 	} else {
4261 		if (recursive) {
4262 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4263 			    "recursive rename must be a snapshot"));
4264 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4265 		}
4266 
4267 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4268 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4269 
4270 		/* validate parents */
4271 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4272 			return (-1);
4273 
4274 		/* make sure we're in the same pool */
4275 		verify((delim = strchr(target, '/')) != NULL);
4276 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4277 		    zhp->zfs_name[delim - target] != '/') {
4278 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4279 			    "datasets must be within same pool"));
4280 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4281 		}
4282 
4283 		/* new name cannot be a child of the current dataset name */
4284 		if (is_descendant(zhp->zfs_name, target)) {
4285 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4286 			    "New dataset name cannot be a descendant of "
4287 			    "current dataset name"));
4288 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4289 		}
4290 	}
4291 
4292 	(void) snprintf(errbuf, sizeof (errbuf),
4293 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4294 
4295 	if (getzoneid() == GLOBAL_ZONEID &&
4296 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4297 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4298 		    "dataset is used in a non-global zone"));
4299 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4300 	}
4301 
4302 	if (recursive) {
4303 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4304 		if (parentname == NULL) {
4305 			ret = -1;
4306 			goto error;
4307 		}
4308 		delim = strchr(parentname, '@');
4309 		*delim = '\0';
4310 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4311 		if (zhrp == NULL) {
4312 			ret = -1;
4313 			goto error;
4314 		}
4315 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4316 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4317 		    force_unmount ? MS_FORCE : 0)) == NULL)
4318 			return (-1);
4319 
4320 		if (changelist_haszonedchild(cl)) {
4321 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4322 			    "child dataset with inherited mountpoint is used "
4323 			    "in a non-global zone"));
4324 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4325 			ret = -1;
4326 			goto error;
4327 		}
4328 
4329 		if ((ret = changelist_prefix(cl)) != 0)
4330 			goto error;
4331 	}
4332 
4333 	if (ZFS_IS_VOLUME(zhp))
4334 		zc.zc_objset_type = DMU_OST_ZVOL;
4335 	else
4336 		zc.zc_objset_type = DMU_OST_ZFS;
4337 
4338 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4339 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4340 
4341 	zc.zc_cookie = recursive;
4342 
4343 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4344 		/*
4345 		 * if it was recursive, the one that actually failed will
4346 		 * be in zc.zc_name
4347 		 */
4348 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4349 		    "cannot rename '%s'"), zc.zc_name);
4350 
4351 		if (recursive && errno == EEXIST) {
4352 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4353 			    "a child dataset already has a snapshot "
4354 			    "with the new name"));
4355 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4356 		} else {
4357 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4358 		}
4359 
4360 		/*
4361 		 * On failure, we still want to remount any filesystems that
4362 		 * were previously mounted, so we don't alter the system state.
4363 		 */
4364 		if (cl != NULL)
4365 			(void) changelist_postfix(cl);
4366 	} else {
4367 		if (cl != NULL) {
4368 			changelist_rename(cl, zfs_get_name(zhp), target);
4369 			ret = changelist_postfix(cl);
4370 		}
4371 	}
4372 
4373 error:
4374 	if (parentname != NULL) {
4375 		free(parentname);
4376 	}
4377 	if (zhrp != NULL) {
4378 		zfs_close(zhrp);
4379 	}
4380 	if (cl != NULL) {
4381 		changelist_free(cl);
4382 	}
4383 	return (ret);
4384 }
4385 
4386 nvlist_t *
4387 zfs_get_user_props(zfs_handle_t *zhp)
4388 {
4389 	return (zhp->zfs_user_props);
4390 }
4391 
4392 nvlist_t *
4393 zfs_get_recvd_props(zfs_handle_t *zhp)
4394 {
4395 	if (zhp->zfs_recvd_props == NULL)
4396 		if (get_recvd_props_ioctl(zhp) != 0)
4397 			return (NULL);
4398 	return (zhp->zfs_recvd_props);
4399 }
4400 
4401 /*
4402  * This function is used by 'zfs list' to determine the exact set of columns to
4403  * display, and their maximum widths.  This does two main things:
4404  *
4405  *      - If this is a list of all properties, then expand the list to include
4406  *        all native properties, and set a flag so that for each dataset we look
4407  *        for new unique user properties and add them to the list.
4408  *
4409  *      - For non fixed-width properties, keep track of the maximum width seen
4410  *        so that we can size the column appropriately. If the user has
4411  *        requested received property values, we also need to compute the width
4412  *        of the RECEIVED column.
4413  */
4414 int
4415 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4416     boolean_t literal)
4417 {
4418 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4419 	zprop_list_t *entry;
4420 	zprop_list_t **last, **start;
4421 	nvlist_t *userprops, *propval;
4422 	nvpair_t *elem;
4423 	char *strval;
4424 	char buf[ZFS_MAXPROPLEN];
4425 
4426 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4427 		return (-1);
4428 
4429 	userprops = zfs_get_user_props(zhp);
4430 
4431 	entry = *plp;
4432 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4433 		/*
4434 		 * Go through and add any user properties as necessary.  We
4435 		 * start by incrementing our list pointer to the first
4436 		 * non-native property.
4437 		 */
4438 		start = plp;
4439 		while (*start != NULL) {
4440 			if ((*start)->pl_prop == ZPROP_INVAL)
4441 				break;
4442 			start = &(*start)->pl_next;
4443 		}
4444 
4445 		elem = NULL;
4446 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4447 			/*
4448 			 * See if we've already found this property in our list.
4449 			 */
4450 			for (last = start; *last != NULL;
4451 			    last = &(*last)->pl_next) {
4452 				if (strcmp((*last)->pl_user_prop,
4453 				    nvpair_name(elem)) == 0)
4454 					break;
4455 			}
4456 
4457 			if (*last == NULL) {
4458 				if ((entry = zfs_alloc(hdl,
4459 				    sizeof (zprop_list_t))) == NULL ||
4460 				    ((entry->pl_user_prop = zfs_strdup(hdl,
4461 				    nvpair_name(elem)))) == NULL) {
4462 					free(entry);
4463 					return (-1);
4464 				}
4465 
4466 				entry->pl_prop = ZPROP_INVAL;
4467 				entry->pl_width = strlen(nvpair_name(elem));
4468 				entry->pl_all = B_TRUE;
4469 				*last = entry;
4470 			}
4471 		}
4472 	}
4473 
4474 	/*
4475 	 * Now go through and check the width of any non-fixed columns
4476 	 */
4477 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4478 		if (entry->pl_fixed && !literal)
4479 			continue;
4480 
4481 		if (entry->pl_prop != ZPROP_INVAL) {
4482 			if (zfs_prop_get(zhp, entry->pl_prop,
4483 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4484 				if (strlen(buf) > entry->pl_width)
4485 					entry->pl_width = strlen(buf);
4486 			}
4487 			if (received && zfs_prop_get_recvd(zhp,
4488 			    zfs_prop_to_name(entry->pl_prop),
4489 			    buf, sizeof (buf), literal) == 0)
4490 				if (strlen(buf) > entry->pl_recvd_width)
4491 					entry->pl_recvd_width = strlen(buf);
4492 		} else {
4493 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4494 			    &propval) == 0) {
4495 				verify(nvlist_lookup_string(propval,
4496 				    ZPROP_VALUE, &strval) == 0);
4497 				if (strlen(strval) > entry->pl_width)
4498 					entry->pl_width = strlen(strval);
4499 			}
4500 			if (received && zfs_prop_get_recvd(zhp,
4501 			    entry->pl_user_prop,
4502 			    buf, sizeof (buf), literal) == 0)
4503 				if (strlen(buf) > entry->pl_recvd_width)
4504 					entry->pl_recvd_width = strlen(buf);
4505 		}
4506 	}
4507 
4508 	return (0);
4509 }
4510 
4511 int
4512 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4513     char *resource, void *export, void *sharetab,
4514     int sharemax, zfs_share_op_t operation)
4515 {
4516 	zfs_cmd_t zc = { 0 };
4517 	int error;
4518 
4519 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4520 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4521 	if (resource)
4522 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4523 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4524 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4525 	zc.zc_share.z_sharetype = operation;
4526 	zc.zc_share.z_sharemax = sharemax;
4527 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4528 	return (error);
4529 }
4530 
4531 void
4532 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4533 {
4534 	nvpair_t *curr;
4535 
4536 	/*
4537 	 * Keep a reference to the props-table against which we prune the
4538 	 * properties.
4539 	 */
4540 	zhp->zfs_props_table = props;
4541 
4542 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4543 
4544 	while (curr) {
4545 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4546 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4547 
4548 		/*
4549 		 * User properties will result in ZPROP_INVAL, and since we
4550 		 * only know how to prune standard ZFS properties, we always
4551 		 * leave these in the list.  This can also happen if we
4552 		 * encounter an unknown DSL property (when running older
4553 		 * software, for example).
4554 		 */
4555 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4556 			(void) nvlist_remove(zhp->zfs_props,
4557 			    nvpair_name(curr), nvpair_type(curr));
4558 		curr = next;
4559 	}
4560 }
4561 
4562 static int
4563 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4564     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4565 {
4566 	zfs_cmd_t zc = { 0 };
4567 	nvlist_t *nvlist = NULL;
4568 	int error;
4569 
4570 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4571 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4572 	zc.zc_cookie = (uint64_t)cmd;
4573 
4574 	if (cmd == ZFS_SMB_ACL_RENAME) {
4575 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4576 			(void) no_memory(hdl);
4577 			return (0);
4578 		}
4579 	}
4580 
4581 	switch (cmd) {
4582 	case ZFS_SMB_ACL_ADD:
4583 	case ZFS_SMB_ACL_REMOVE:
4584 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4585 		break;
4586 	case ZFS_SMB_ACL_RENAME:
4587 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4588 		    resource1) != 0) {
4589 				(void) no_memory(hdl);
4590 				return (-1);
4591 		}
4592 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4593 		    resource2) != 0) {
4594 				(void) no_memory(hdl);
4595 				return (-1);
4596 		}
4597 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4598 			nvlist_free(nvlist);
4599 			return (-1);
4600 		}
4601 		break;
4602 	case ZFS_SMB_ACL_PURGE:
4603 		break;
4604 	default:
4605 		return (-1);
4606 	}
4607 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4608 	nvlist_free(nvlist);
4609 	return (error);
4610 }
4611 
4612 int
4613 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4614     char *path, char *resource)
4615 {
4616 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4617 	    resource, NULL));
4618 }
4619 
4620 int
4621 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4622     char *path, char *resource)
4623 {
4624 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4625 	    resource, NULL));
4626 }
4627 
4628 int
4629 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4630 {
4631 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4632 	    NULL, NULL));
4633 }
4634 
4635 int
4636 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4637     char *oldname, char *newname)
4638 {
4639 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4640 	    oldname, newname));
4641 }
4642 
4643 int
4644 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4645     zfs_userspace_cb_t func, void *arg)
4646 {
4647 	zfs_cmd_t zc = { 0 };
4648 	zfs_useracct_t buf[100];
4649 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4650 	int ret;
4651 
4652 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4653 
4654 	zc.zc_objset_type = type;
4655 	zc.zc_nvlist_dst = (uintptr_t)buf;
4656 
4657 	for (;;) {
4658 		zfs_useracct_t *zua = buf;
4659 
4660 		zc.zc_nvlist_dst_size = sizeof (buf);
4661 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4662 			char errbuf[1024];
4663 
4664 			(void) snprintf(errbuf, sizeof (errbuf),
4665 			    dgettext(TEXT_DOMAIN,
4666 			    "cannot get used/quota for %s"), zc.zc_name);
4667 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4668 		}
4669 		if (zc.zc_nvlist_dst_size == 0)
4670 			break;
4671 
4672 		while (zc.zc_nvlist_dst_size > 0) {
4673 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4674 			    zua->zu_space)) != 0)
4675 				return (ret);
4676 			zua++;
4677 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4678 		}
4679 	}
4680 
4681 	return (0);
4682 }
4683 
4684 struct holdarg {
4685 	nvlist_t *nvl;
4686 	const char *snapname;
4687 	const char *tag;
4688 	boolean_t recursive;
4689 	int error;
4690 };
4691 
4692 static int
4693 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4694 {
4695 	struct holdarg *ha = arg;
4696 	char name[ZFS_MAX_DATASET_NAME_LEN];
4697 	int rv = 0;
4698 
4699 	(void) snprintf(name, sizeof (name),
4700 	    "%s@%s", zhp->zfs_name, ha->snapname);
4701 
4702 	if (lzc_exists(name))
4703 		fnvlist_add_string(ha->nvl, name, ha->tag);
4704 
4705 	if (ha->recursive)
4706 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4707 	zfs_close(zhp);
4708 	return (rv);
4709 }
4710 
4711 int
4712 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4713     boolean_t recursive, int cleanup_fd)
4714 {
4715 	int ret;
4716 	struct holdarg ha;
4717 
4718 	ha.nvl = fnvlist_alloc();
4719 	ha.snapname = snapname;
4720 	ha.tag = tag;
4721 	ha.recursive = recursive;
4722 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4723 
4724 	if (nvlist_empty(ha.nvl)) {
4725 		char errbuf[1024];
4726 
4727 		fnvlist_free(ha.nvl);
4728 		ret = ENOENT;
4729 		(void) snprintf(errbuf, sizeof (errbuf),
4730 		    dgettext(TEXT_DOMAIN,
4731 		    "cannot hold snapshot '%s@%s'"),
4732 		    zhp->zfs_name, snapname);
4733 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4734 		return (ret);
4735 	}
4736 
4737 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4738 	fnvlist_free(ha.nvl);
4739 
4740 	return (ret);
4741 }
4742 
4743 int
4744 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4745 {
4746 	int ret;
4747 	nvlist_t *errors;
4748 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4749 	char errbuf[1024];
4750 	nvpair_t *elem;
4751 
4752 	errors = NULL;
4753 	ret = lzc_hold(holds, cleanup_fd, &errors);
4754 
4755 	if (ret == 0) {
4756 		/* There may be errors even in the success case. */
4757 		fnvlist_free(errors);
4758 		return (0);
4759 	}
4760 
4761 	if (nvlist_empty(errors)) {
4762 		/* no hold-specific errors */
4763 		(void) snprintf(errbuf, sizeof (errbuf),
4764 		    dgettext(TEXT_DOMAIN, "cannot hold"));
4765 		switch (ret) {
4766 		case ENOTSUP:
4767 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4768 			    "pool must be upgraded"));
4769 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4770 			break;
4771 		case EINVAL:
4772 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4773 			break;
4774 		default:
4775 			(void) zfs_standard_error(hdl, ret, errbuf);
4776 		}
4777 	}
4778 
4779 	for (elem = nvlist_next_nvpair(errors, NULL);
4780 	    elem != NULL;
4781 	    elem = nvlist_next_nvpair(errors, elem)) {
4782 		(void) snprintf(errbuf, sizeof (errbuf),
4783 		    dgettext(TEXT_DOMAIN,
4784 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
4785 		switch (fnvpair_value_int32(elem)) {
4786 		case E2BIG:
4787 			/*
4788 			 * Temporary tags wind up having the ds object id
4789 			 * prepended. So even if we passed the length check
4790 			 * above, it's still possible for the tag to wind
4791 			 * up being slightly too long.
4792 			 */
4793 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4794 			break;
4795 		case EINVAL:
4796 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4797 			break;
4798 		case EEXIST:
4799 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4800 			break;
4801 		default:
4802 			(void) zfs_standard_error(hdl,
4803 			    fnvpair_value_int32(elem), errbuf);
4804 		}
4805 	}
4806 
4807 	fnvlist_free(errors);
4808 	return (ret);
4809 }
4810 
4811 static int
4812 zfs_release_one(zfs_handle_t *zhp, void *arg)
4813 {
4814 	struct holdarg *ha = arg;
4815 	char name[ZFS_MAX_DATASET_NAME_LEN];
4816 	int rv = 0;
4817 	nvlist_t *existing_holds;
4818 
4819 	(void) snprintf(name, sizeof (name),
4820 	    "%s@%s", zhp->zfs_name, ha->snapname);
4821 
4822 	if (lzc_get_holds(name, &existing_holds) != 0) {
4823 		ha->error = ENOENT;
4824 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4825 		ha->error = ESRCH;
4826 	} else {
4827 		nvlist_t *torelease = fnvlist_alloc();
4828 		fnvlist_add_boolean(torelease, ha->tag);
4829 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4830 		fnvlist_free(torelease);
4831 	}
4832 
4833 	if (ha->recursive)
4834 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4835 	zfs_close(zhp);
4836 	return (rv);
4837 }
4838 
4839 int
4840 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4841     boolean_t recursive)
4842 {
4843 	int ret;
4844 	struct holdarg ha;
4845 	nvlist_t *errors = NULL;
4846 	nvpair_t *elem;
4847 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4848 	char errbuf[1024];
4849 
4850 	ha.nvl = fnvlist_alloc();
4851 	ha.snapname = snapname;
4852 	ha.tag = tag;
4853 	ha.recursive = recursive;
4854 	ha.error = 0;
4855 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4856 
4857 	if (nvlist_empty(ha.nvl)) {
4858 		fnvlist_free(ha.nvl);
4859 		ret = ha.error;
4860 		(void) snprintf(errbuf, sizeof (errbuf),
4861 		    dgettext(TEXT_DOMAIN,
4862 		    "cannot release hold from snapshot '%s@%s'"),
4863 		    zhp->zfs_name, snapname);
4864 		if (ret == ESRCH) {
4865 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4866 		} else {
4867 			(void) zfs_standard_error(hdl, ret, errbuf);
4868 		}
4869 		return (ret);
4870 	}
4871 
4872 	ret = lzc_release(ha.nvl, &errors);
4873 	fnvlist_free(ha.nvl);
4874 
4875 	if (ret == 0) {
4876 		/* There may be errors even in the success case. */
4877 		fnvlist_free(errors);
4878 		return (0);
4879 	}
4880 
4881 	if (nvlist_empty(errors)) {
4882 		/* no hold-specific errors */
4883 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4884 		    "cannot release"));
4885 		switch (errno) {
4886 		case ENOTSUP:
4887 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4888 			    "pool must be upgraded"));
4889 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4890 			break;
4891 		default:
4892 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
4893 		}
4894 	}
4895 
4896 	for (elem = nvlist_next_nvpair(errors, NULL);
4897 	    elem != NULL;
4898 	    elem = nvlist_next_nvpair(errors, elem)) {
4899 		(void) snprintf(errbuf, sizeof (errbuf),
4900 		    dgettext(TEXT_DOMAIN,
4901 		    "cannot release hold from snapshot '%s'"),
4902 		    nvpair_name(elem));
4903 		switch (fnvpair_value_int32(elem)) {
4904 		case ESRCH:
4905 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4906 			break;
4907 		case EINVAL:
4908 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4909 			break;
4910 		default:
4911 			(void) zfs_standard_error_fmt(hdl,
4912 			    fnvpair_value_int32(elem), errbuf);
4913 		}
4914 	}
4915 
4916 	fnvlist_free(errors);
4917 	return (ret);
4918 }
4919 
4920 int
4921 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4922 {
4923 	zfs_cmd_t zc = { 0 };
4924 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4925 	int nvsz = 2048;
4926 	void *nvbuf;
4927 	int err = 0;
4928 	char errbuf[1024];
4929 
4930 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4931 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4932 
4933 tryagain:
4934 
4935 	nvbuf = malloc(nvsz);
4936 	if (nvbuf == NULL) {
4937 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4938 		goto out;
4939 	}
4940 
4941 	zc.zc_nvlist_dst_size = nvsz;
4942 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4943 
4944 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4945 
4946 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4947 		(void) snprintf(errbuf, sizeof (errbuf),
4948 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4949 		    zc.zc_name);
4950 		switch (errno) {
4951 		case ENOMEM:
4952 			free(nvbuf);
4953 			nvsz = zc.zc_nvlist_dst_size;
4954 			goto tryagain;
4955 
4956 		case ENOTSUP:
4957 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4958 			    "pool must be upgraded"));
4959 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4960 			break;
4961 		case EINVAL:
4962 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4963 			break;
4964 		case ENOENT:
4965 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4966 			break;
4967 		default:
4968 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
4969 			break;
4970 		}
4971 	} else {
4972 		/* success */
4973 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4974 		if (rc) {
4975 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
4976 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
4977 			    zc.zc_name);
4978 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
4979 		}
4980 	}
4981 
4982 	free(nvbuf);
4983 out:
4984 	return (err);
4985 }
4986 
4987 int
4988 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4989 {
4990 	zfs_cmd_t zc = { 0 };
4991 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4992 	char *nvbuf;
4993 	char errbuf[1024];
4994 	size_t nvsz;
4995 	int err;
4996 
4997 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4998 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4999 
5000 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5001 	assert(err == 0);
5002 
5003 	nvbuf = malloc(nvsz);
5004 
5005 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5006 	assert(err == 0);
5007 
5008 	zc.zc_nvlist_src_size = nvsz;
5009 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
5010 	zc.zc_perm_action = un;
5011 
5012 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5013 
5014 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5015 		(void) snprintf(errbuf, sizeof (errbuf),
5016 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5017 		    zc.zc_name);
5018 		switch (errno) {
5019 		case ENOTSUP:
5020 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5021 			    "pool must be upgraded"));
5022 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5023 			break;
5024 		case EINVAL:
5025 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5026 			break;
5027 		case ENOENT:
5028 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5029 			break;
5030 		default:
5031 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
5032 			break;
5033 		}
5034 	}
5035 
5036 	free(nvbuf);
5037 
5038 	return (err);
5039 }
5040 
5041 int
5042 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5043 {
5044 	int err;
5045 	char errbuf[1024];
5046 
5047 	err = lzc_get_holds(zhp->zfs_name, nvl);
5048 
5049 	if (err != 0) {
5050 		libzfs_handle_t *hdl = zhp->zfs_hdl;
5051 
5052 		(void) snprintf(errbuf, sizeof (errbuf),
5053 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5054 		    zhp->zfs_name);
5055 		switch (err) {
5056 		case ENOTSUP:
5057 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5058 			    "pool must be upgraded"));
5059 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5060 			break;
5061 		case EINVAL:
5062 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5063 			break;
5064 		case ENOENT:
5065 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5066 			break;
5067 		default:
5068 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
5069 			break;
5070 		}
5071 	}
5072 
5073 	return (err);
5074 }
5075 
5076 /*
5077  * Convert the zvol's volume size to an appropriate reservation.
5078  * Note: If this routine is updated, it is necessary to update the ZFS test
5079  * suite's shell version in reservation.kshlib.
5080  */
5081 uint64_t
5082 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5083 {
5084 	uint64_t numdb;
5085 	uint64_t nblocks, volblocksize;
5086 	int ncopies;
5087 	char *strval;
5088 
5089 	if (nvlist_lookup_string(props,
5090 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5091 		ncopies = atoi(strval);
5092 	else
5093 		ncopies = 1;
5094 	if (nvlist_lookup_uint64(props,
5095 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5096 	    &volblocksize) != 0)
5097 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5098 	nblocks = volsize/volblocksize;
5099 	/* start with metadnode L0-L6 */
5100 	numdb = 7;
5101 	/* calculate number of indirects */
5102 	while (nblocks > 1) {
5103 		nblocks += DNODES_PER_LEVEL - 1;
5104 		nblocks /= DNODES_PER_LEVEL;
5105 		numdb += nblocks;
5106 	}
5107 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5108 	volsize *= ncopies;
5109 	/*
5110 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5111 	 * compressed, but in practice they compress down to about
5112 	 * 1100 bytes
5113 	 */
5114 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5115 	volsize += numdb;
5116 	return (volsize);
5117 }
5118