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