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