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