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