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