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