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