1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
213126Sahl 
22789Sahrens /*
235977Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens #include <assert.h>
30789Sahrens #include <ctype.h>
31789Sahrens #include <errno.h>
32789Sahrens #include <libdevinfo.h>
33789Sahrens #include <libintl.h>
34789Sahrens #include <math.h>
35789Sahrens #include <stdio.h>
36789Sahrens #include <stdlib.h>
37789Sahrens #include <strings.h>
38789Sahrens #include <unistd.h>
395367Sahrens #include <stddef.h>
40789Sahrens #include <zone.h>
412082Seschrock #include <fcntl.h>
42789Sahrens #include <sys/mntent.h>
43789Sahrens #include <sys/mnttab.h>
441294Slling #include <sys/mount.h>
454543Smarks #include <sys/avl.h>
464543Smarks #include <priv.h>
474543Smarks #include <pwd.h>
484543Smarks #include <grp.h>
494543Smarks #include <stddef.h>
504543Smarks #include <ucred.h>
51789Sahrens 
52789Sahrens #include <sys/spa.h>
532676Seschrock #include <sys/zap.h>
54789Sahrens #include <libzfs.h>
55789Sahrens 
56789Sahrens #include "zfs_namecheck.h"
57789Sahrens #include "zfs_prop.h"
58789Sahrens #include "libzfs_impl.h"
594543Smarks #include "zfs_deleg.h"
60789Sahrens 
614007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
624007Smmusante 
63789Sahrens /*
64789Sahrens  * Given a single type (not a mask of types), return the type in a human
65789Sahrens  * readable form.
66789Sahrens  */
67789Sahrens const char *
68789Sahrens zfs_type_to_name(zfs_type_t type)
69789Sahrens {
70789Sahrens 	switch (type) {
71789Sahrens 	case ZFS_TYPE_FILESYSTEM:
72789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73789Sahrens 	case ZFS_TYPE_SNAPSHOT:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75789Sahrens 	case ZFS_TYPE_VOLUME:
76789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
77789Sahrens 	}
78789Sahrens 
79789Sahrens 	return (NULL);
80789Sahrens }
81789Sahrens 
82789Sahrens /*
83789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
84789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
85789Sahrens  * We guess what the type would have been based on the path and the mask of
86789Sahrens  * acceptable types.
87789Sahrens  */
88789Sahrens static const char *
89789Sahrens path_to_str(const char *path, int types)
90789Sahrens {
91789Sahrens 	/*
92789Sahrens 	 * When given a single type, always report the exact type.
93789Sahrens 	 */
94789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
95789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98789Sahrens 	if (types == ZFS_TYPE_VOLUME)
99789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
100789Sahrens 
101789Sahrens 	/*
102789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
103789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
104789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105789Sahrens 	 * snapshot attribute and try again.
106789Sahrens 	 */
107789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
108789Sahrens 		if (strchr(path, '@') != NULL)
109789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111789Sahrens 	}
112789Sahrens 
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * The user has requested either filesystems or volumes.
116789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118789Sahrens 	 */
119789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121789Sahrens 
122789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124789Sahrens }
125789Sahrens 
126789Sahrens /*
127789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128789Sahrens  * provide a more meaningful error message.  We place a more useful message in
129789Sahrens  * 'buf' detailing exactly why the name was not valid.
130789Sahrens  */
131789Sahrens static int
1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1335326Sek110237     boolean_t modifying)
134789Sahrens {
135789Sahrens 	namecheck_err_t why;
136789Sahrens 	char what;
137789Sahrens 
138789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1392082Seschrock 		if (hdl != NULL) {
140789Sahrens 			switch (why) {
1411003Slling 			case NAME_ERR_TOOLONG:
1422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432082Seschrock 				    "name is too long"));
1441003Slling 				break;
1451003Slling 
146789Sahrens 			case NAME_ERR_LEADING_SLASH:
1472082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482082Seschrock 				    "leading slash in name"));
149789Sahrens 				break;
150789Sahrens 
151789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532082Seschrock 				    "empty component in name"));
154789Sahrens 				break;
155789Sahrens 
156789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1572082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582082Seschrock 				    "trailing slash in name"));
159789Sahrens 				break;
160789Sahrens 
161789Sahrens 			case NAME_ERR_INVALCHAR:
1622082Seschrock 				zfs_error_aux(hdl,
163789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1642082Seschrock 				    "'%c' in name"), what);
165789Sahrens 				break;
166789Sahrens 
167789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1682082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692082Seschrock 				    "multiple '@' delimiters in name"));
170789Sahrens 				break;
1712856Snd150628 
1722856Snd150628 			case NAME_ERR_NOLETTER:
1732856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1742856Snd150628 				    "pool doesn't begin with a letter"));
1752856Snd150628 				break;
1762856Snd150628 
1772856Snd150628 			case NAME_ERR_RESERVED:
1782856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1792856Snd150628 				    "name is reserved"));
1802856Snd150628 				break;
1812856Snd150628 
1822856Snd150628 			case NAME_ERR_DISKLIKE:
1832856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1842856Snd150628 				    "reserved disk name"));
1852856Snd150628 				break;
186789Sahrens 			}
187789Sahrens 		}
188789Sahrens 
189789Sahrens 		return (0);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1932082Seschrock 		if (hdl != NULL)
1942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1952082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
196789Sahrens 		return (0);
197789Sahrens 	}
198789Sahrens 
1992199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2002199Sahrens 		if (hdl != NULL)
2012199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2023413Smmusante 			    "missing '@' delimiter in snapshot name"));
2032199Sahrens 		return (0);
2042199Sahrens 	}
2052199Sahrens 
2065326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2075326Sek110237 		if (hdl != NULL)
2085326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2095326Sek110237 			    "invalid character %c in name"), '%');
2105326Sek110237 		return (0);
2115326Sek110237 	}
2125326Sek110237 
2132082Seschrock 	return (-1);
214789Sahrens }
215789Sahrens 
216789Sahrens int
217789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218789Sahrens {
2195326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
220789Sahrens }
221789Sahrens 
222789Sahrens /*
2232676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2242676Seschrock  * properties into a separate nvlist.
2252676Seschrock  */
2264217Seschrock static nvlist_t *
2274217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2282676Seschrock {
2292676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2302676Seschrock 	nvpair_t *elem;
2312676Seschrock 	nvlist_t *propval;
2324217Seschrock 	nvlist_t *nvl;
2334217Seschrock 
2344217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2354217Seschrock 		(void) no_memory(hdl);
2364217Seschrock 		return (NULL);
2374217Seschrock 	}
2382676Seschrock 
2392676Seschrock 	elem = NULL;
2404217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2412676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2422676Seschrock 			continue;
2432676Seschrock 
2442676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2454217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2464217Seschrock 			nvlist_free(nvl);
2474217Seschrock 			(void) no_memory(hdl);
2484217Seschrock 			return (NULL);
2494217Seschrock 		}
2502676Seschrock 	}
2512676Seschrock 
2524217Seschrock 	return (nvl);
2532676Seschrock }
2542676Seschrock 
2552676Seschrock /*
256789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
257789Sahrens  */
258789Sahrens static int
259789Sahrens get_stats(zfs_handle_t *zhp)
260789Sahrens {
261789Sahrens 	zfs_cmd_t zc = { 0 };
2622676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2634217Seschrock 	nvlist_t *allprops, *userprops;
264789Sahrens 
265789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
266789Sahrens 
2672676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
2682082Seschrock 		return (-1);
2691356Seschrock 
2702082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
2711356Seschrock 		if (errno == ENOMEM) {
2722676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2732676Seschrock 				zcmd_free_nvlists(&zc);
2742082Seschrock 				return (-1);
2752676Seschrock 			}
2761356Seschrock 		} else {
2772676Seschrock 			zcmd_free_nvlists(&zc);
2781356Seschrock 			return (-1);
2791356Seschrock 		}
2801356Seschrock 	}
281789Sahrens 
2822885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
283789Sahrens 
2842676Seschrock 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
2851544Seschrock 
2864217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
2872676Seschrock 		zcmd_free_nvlists(&zc);
2882082Seschrock 		return (-1);
2892082Seschrock 	}
290789Sahrens 
2912676Seschrock 	zcmd_free_nvlists(&zc);
2922676Seschrock 
2934217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
2944217Seschrock 		nvlist_free(allprops);
2952676Seschrock 		return (-1);
2964217Seschrock 	}
2974217Seschrock 
2984217Seschrock 	nvlist_free(zhp->zfs_props);
2994217Seschrock 	nvlist_free(zhp->zfs_user_props);
3004217Seschrock 
3014217Seschrock 	zhp->zfs_props = allprops;
3024217Seschrock 	zhp->zfs_user_props = userprops;
3032082Seschrock 
304789Sahrens 	return (0);
305789Sahrens }
306789Sahrens 
307789Sahrens /*
308789Sahrens  * Refresh the properties currently stored in the handle.
309789Sahrens  */
310789Sahrens void
311789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
312789Sahrens {
313789Sahrens 	(void) get_stats(zhp);
314789Sahrens }
315789Sahrens 
316789Sahrens /*
317789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
318789Sahrens  * zfs_iter_* to create child handles on the fly.
319789Sahrens  */
320789Sahrens zfs_handle_t *
3212082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
322789Sahrens {
3232082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3244543Smarks 	char *logstr;
3252082Seschrock 
3262082Seschrock 	if (zhp == NULL)
3272082Seschrock 		return (NULL);
3282082Seschrock 
3292082Seschrock 	zhp->zfs_hdl = hdl;
330789Sahrens 
3314543Smarks 	/*
3324543Smarks 	 * Preserve history log string.
3334543Smarks 	 * any changes performed here will be
3344543Smarks 	 * logged as an internal event.
3354543Smarks 	 */
3364543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3374543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3381758Sahrens top:
339789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
340789Sahrens 
341789Sahrens 	if (get_stats(zhp) != 0) {
3424543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
343789Sahrens 		free(zhp);
344789Sahrens 		return (NULL);
345789Sahrens 	}
346789Sahrens 
3471758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
3481758Sahrens 		zfs_cmd_t zc = { 0 };
3491758Sahrens 
3501758Sahrens 		/*
3511758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
3521758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
3531758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
3541758Sahrens 		 * can't be mounted.  However, it could also be that we
3551758Sahrens 		 * have crashed in the middle of one of those
3561758Sahrens 		 * operations, in which case we need to get rid of the
3571758Sahrens 		 * inconsistent state.  We do that by either rolling
3581758Sahrens 		 * back to the previous snapshot (which will fail if
3591758Sahrens 		 * there is none), or destroying the filesystem.  Note
3601758Sahrens 		 * that if we are still in the middle of an active
3611758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
3621758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
3631758Sahrens 		 */
3641758Sahrens 
3651758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3661758Sahrens 
3672885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
3682082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
3691758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
3701758Sahrens 		} else {
3711758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
3721758Sahrens 		}
3731758Sahrens 
3741758Sahrens 		/*
3755331Samw 		 * If we can successfully destroy it, pretend that it
3761758Sahrens 		 * never existed.
3771758Sahrens 		 */
3782082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
3794543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3801758Sahrens 			free(zhp);
3811758Sahrens 			errno = ENOENT;
3821758Sahrens 			return (NULL);
3831758Sahrens 		}
3845367Sahrens 		/* If we can successfully roll it back, reget the stats */
3855367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
3865367Sahrens 			goto top;
3871758Sahrens 	}
3881758Sahrens 
389789Sahrens 	/*
390789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
391789Sahrens 	 * the high-level type.
392789Sahrens 	 */
3932885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
3942885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
3952885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
3962885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
3972885Sahrens 	else
3982885Sahrens 		abort();
3992885Sahrens 
400789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
401789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
402789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
403789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
404789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
405789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
406789Sahrens 	else
4072082Seschrock 		abort();	/* we should never see any other types */
408789Sahrens 
4094543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
410789Sahrens 	return (zhp);
411789Sahrens }
412789Sahrens 
413789Sahrens /*
414789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
415789Sahrens  * argument is a mask of acceptable types.  The function will print an
416789Sahrens  * appropriate error message and return NULL if it can't be opened.
417789Sahrens  */
418789Sahrens zfs_handle_t *
4192082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
420789Sahrens {
421789Sahrens 	zfs_handle_t *zhp;
4222082Seschrock 	char errbuf[1024];
4232082Seschrock 
4242082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4252082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
426789Sahrens 
427789Sahrens 	/*
4282082Seschrock 	 * Validate the name before we even try to open it.
429789Sahrens 	 */
4305326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4312082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4322082Seschrock 		    "invalid dataset name"));
4332082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
434789Sahrens 		return (NULL);
435789Sahrens 	}
436789Sahrens 
437789Sahrens 	/*
438789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
439789Sahrens 	 */
440789Sahrens 	errno = 0;
4412082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
4423237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
443789Sahrens 		return (NULL);
444789Sahrens 	}
445789Sahrens 
446789Sahrens 	if (!(types & zhp->zfs_type)) {
4472082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4482142Seschrock 		zfs_close(zhp);
449789Sahrens 		return (NULL);
450789Sahrens 	}
451789Sahrens 
452789Sahrens 	return (zhp);
453789Sahrens }
454789Sahrens 
455789Sahrens /*
456789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
457789Sahrens  */
458789Sahrens void
459789Sahrens zfs_close(zfs_handle_t *zhp)
460789Sahrens {
461789Sahrens 	if (zhp->zfs_mntopts)
462789Sahrens 		free(zhp->zfs_mntopts);
4632676Seschrock 	nvlist_free(zhp->zfs_props);
4642676Seschrock 	nvlist_free(zhp->zfs_user_props);
465789Sahrens 	free(zhp);
466789Sahrens }
467789Sahrens 
4685713Srm160521 int
4695713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
4705713Srm160521 {
4715713Srm160521 	char *pool_name;
4725713Srm160521 	zpool_handle_t *zpool_handle;
4735713Srm160521 	char *p;
4745713Srm160521 
4755713Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN);
4765713Srm160521 	if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name,
4775713Srm160521 	    MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) {
4785713Srm160521 		free(pool_name);
4795713Srm160521 		return (-1);
4805713Srm160521 	}
4815713Srm160521 
4825713Srm160521 	if (p = strchr(pool_name, '/'))
4835713Srm160521 		*p = '\0';
4845713Srm160521 	zpool_handle = zpool_open(zhp->zfs_hdl, pool_name);
4855713Srm160521 	free(pool_name);
4865713Srm160521 	if (zpool_handle == NULL)
4875713Srm160521 		return (-1);
4885713Srm160521 
4895713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
4905713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
4915713Srm160521 	zpool_close(zpool_handle);
4925713Srm160521 	return (0);
4935713Srm160521 }
4945713Srm160521 
4955713Srm160521 /*
4965713Srm160521  * The choice of reservation property depends on the SPA version.
4975713Srm160521  */
4985713Srm160521 static int
4995713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5005713Srm160521 {
5015713Srm160521 	int spa_version;
5025713Srm160521 
5035713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5045713Srm160521 		return (-1);
5055713Srm160521 
5065713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5075713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5085713Srm160521 	else
5095713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5105713Srm160521 
5115713Srm160521 	return (0);
5125713Srm160521 }
5135713Srm160521 
5143912Slling /*
5152676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
5162676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
5172676Seschrock  * strings.
518789Sahrens  */
5195094Slling static nvlist_t *
5205094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
5215094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
522789Sahrens {
5232676Seschrock 	nvpair_t *elem;
5242676Seschrock 	uint64_t intval;
5252676Seschrock 	char *strval;
5265094Slling 	zfs_prop_t prop;
5272676Seschrock 	nvlist_t *ret;
5285331Samw 	int chosen_normal = -1;
5295331Samw 	int chosen_utf = -1;
5302676Seschrock 
5312676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
5322676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5333413Smmusante 		    "snapshot properties cannot be modified"));
5342676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5355094Slling 		return (NULL);
5365094Slling 	}
5375094Slling 
5385094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5395094Slling 		(void) no_memory(hdl);
5405094Slling 		return (NULL);
541789Sahrens 	}
542789Sahrens 
5432676Seschrock 	elem = NULL;
5442676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5455094Slling 		const char *propname = nvpair_name(elem);
5462676Seschrock 
5472676Seschrock 		/*
5482676Seschrock 		 * Make sure this property is valid and applies to this type.
5492676Seschrock 		 */
5505094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
5515094Slling 			if (!zfs_prop_user(propname)) {
5522676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5535094Slling 				    "invalid property '%s'"), propname);
5545094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5555094Slling 				goto error;
5565094Slling 			}
5575094Slling 
5585094Slling 			/*
5595094Slling 			 * If this is a user property, make sure it's a
5605094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
5615094Slling 			 */
5625094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
5635094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5645094Slling 				    "'%s' must be a string"), propname);
5655094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5665094Slling 				goto error;
5675094Slling 			}
5685094Slling 
5695094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
5705094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5715094Slling 				    "property name '%s' is too long"),
5722676Seschrock 				    propname);
5732676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5742676Seschrock 				goto error;
5752676Seschrock 			}
5762676Seschrock 
5772676Seschrock 			(void) nvpair_value_string(elem, &strval);
5782676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
5792676Seschrock 				(void) no_memory(hdl);
5802676Seschrock 				goto error;
5812676Seschrock 			}
5822676Seschrock 			continue;
583789Sahrens 		}
5842676Seschrock 
5852676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
5862676Seschrock 			zfs_error_aux(hdl,
5872676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
5882676Seschrock 			    "apply to datasets of this type"), propname);
5892676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5902676Seschrock 			goto error;
5912676Seschrock 		}
5922676Seschrock 
5932676Seschrock 		if (zfs_prop_readonly(prop) &&
5945331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
5952676Seschrock 			zfs_error_aux(hdl,
5962676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
5972676Seschrock 			    propname);
5982676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
5992676Seschrock 			goto error;
6002676Seschrock 		}
6012676Seschrock 
6025094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
6035094Slling 		    &strval, &intval, errbuf) != 0)
6042676Seschrock 			goto error;
6052676Seschrock 
6062676Seschrock 		/*
6072676Seschrock 		 * Perform some additional checks for specific properties.
6082676Seschrock 		 */
6092676Seschrock 		switch (prop) {
6104577Sahrens 		case ZFS_PROP_VERSION:
6114577Sahrens 		{
6124577Sahrens 			int version;
6134577Sahrens 
6144577Sahrens 			if (zhp == NULL)
6154577Sahrens 				break;
6164577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6174577Sahrens 			if (intval < version) {
6184577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6194577Sahrens 				    "Can not downgrade; already at version %u"),
6204577Sahrens 				    version);
6214577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6224577Sahrens 				goto error;
6234577Sahrens 			}
6244577Sahrens 			break;
6254577Sahrens 		}
6264577Sahrens 
6272676Seschrock 		case ZFS_PROP_RECORDSIZE:
6282676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
6292676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
6302676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
6312676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
6322082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6332676Seschrock 				    "'%s' must be power of 2 from %u "
6342676Seschrock 				    "to %uk"), propname,
6352676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
6362676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
6372676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6382676Seschrock 				goto error;
639789Sahrens 			}
640789Sahrens 			break;
641789Sahrens 
6423126Sahl 		case ZFS_PROP_SHAREISCSI:
6433126Sahl 			if (strcmp(strval, "off") != 0 &&
6443126Sahl 			    strcmp(strval, "on") != 0 &&
6453126Sahl 			    strcmp(strval, "type=disk") != 0) {
6463126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6473126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6483126Sahl 				    propname);
6493126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6503126Sahl 				goto error;
6513126Sahl 			}
6523126Sahl 
6533126Sahl 			break;
6543126Sahl 
6552676Seschrock 		case ZFS_PROP_MOUNTPOINT:
6564778Srm160521 		{
6574778Srm160521 			namecheck_err_t why;
6584778Srm160521 
6592676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
6602676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
6612676Seschrock 				break;
6622676Seschrock 
6634778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
6644778Srm160521 				switch (why) {
6654778Srm160521 				case NAME_ERR_LEADING_SLASH:
6664778Srm160521 					zfs_error_aux(hdl,
6674778Srm160521 					    dgettext(TEXT_DOMAIN,
6684778Srm160521 					    "'%s' must be an absolute path, "
6694778Srm160521 					    "'none', or 'legacy'"), propname);
6704778Srm160521 					break;
6714778Srm160521 				case NAME_ERR_TOOLONG:
6724778Srm160521 					zfs_error_aux(hdl,
6734778Srm160521 					    dgettext(TEXT_DOMAIN,
6744778Srm160521 					    "component of '%s' is too long"),
6754778Srm160521 					    propname);
6764778Srm160521 					break;
6774778Srm160521 				}
6782676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6792676Seschrock 				goto error;
680789Sahrens 			}
6814778Srm160521 		}
6824778Srm160521 
6833126Sahl 			/*FALLTHRU*/
6843126Sahl 
6855331Samw 		case ZFS_PROP_SHARESMB:
6863126Sahl 		case ZFS_PROP_SHARENFS:
6873126Sahl 			/*
6885331Samw 			 * For the mountpoint and sharenfs or sharesmb
6895331Samw 			 * properties, check if it can be set in a
6905331Samw 			 * global/non-global zone based on
6913126Sahl 			 * the zoned property value:
6923126Sahl 			 *
6933126Sahl 			 *		global zone	    non-global zone
6943126Sahl 			 * --------------------------------------------------
6953126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
6963126Sahl 			 *		sharenfs (no)	    sharenfs (no)
6975331Samw 			 *		sharesmb (no)	    sharesmb (no)
6983126Sahl 			 *
6993126Sahl 			 * zoned=off	mountpoint (yes)	N/A
7003126Sahl 			 *		sharenfs (yes)
7015331Samw 			 *		sharesmb (yes)
7023126Sahl 			 */
7032676Seschrock 			if (zoned) {
7042676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
7052676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7062676Seschrock 					    "'%s' cannot be set on "
7072676Seschrock 					    "dataset in a non-global zone"),
7082676Seschrock 					    propname);
7092676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7102676Seschrock 					    errbuf);
7112676Seschrock 					goto error;
7125331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
7135331Samw 				    prop == ZFS_PROP_SHARESMB) {
7142676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7152676Seschrock 					    "'%s' cannot be set in "
7162676Seschrock 					    "a non-global zone"), propname);
7172676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7182676Seschrock 					    errbuf);
7192676Seschrock 					goto error;
7202676Seschrock 				}
7212676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
7222676Seschrock 				/*
7232676Seschrock 				 * If zoned property is 'off', this must be in
7242676Seschrock 				 * a globle zone. If not, something is wrong.
7252676Seschrock 				 */
7262676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7272676Seschrock 				    "'%s' cannot be set while dataset "
7282676Seschrock 				    "'zoned' property is set"), propname);
7292676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
7302676Seschrock 				goto error;
7312676Seschrock 			}
7323126Sahl 
7334180Sdougm 			/*
7344180Sdougm 			 * At this point, it is legitimate to set the
7354180Sdougm 			 * property. Now we want to make sure that the
7364180Sdougm 			 * property value is valid if it is sharenfs.
7374180Sdougm 			 */
7385331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
7395331Samw 			    prop == ZFS_PROP_SHARESMB) &&
7404217Seschrock 			    strcmp(strval, "on") != 0 &&
7414217Seschrock 			    strcmp(strval, "off") != 0) {
7425331Samw 				zfs_share_proto_t proto;
7435331Samw 
7445331Samw 				if (prop == ZFS_PROP_SHARESMB)
7455331Samw 					proto = PROTO_SMB;
7465331Samw 				else
7475331Samw 					proto = PROTO_NFS;
7484180Sdougm 
7494180Sdougm 				/*
7505331Samw 				 * Must be an valid sharing protocol
7515331Samw 				 * option string so init the libshare
7525331Samw 				 * in order to enable the parser and
7535331Samw 				 * then parse the options. We use the
7545331Samw 				 * control API since we don't care about
7555331Samw 				 * the current configuration and don't
7564180Sdougm 				 * want the overhead of loading it
7574180Sdougm 				 * until we actually do something.
7584180Sdougm 				 */
7594180Sdougm 
7604217Seschrock 				if (zfs_init_libshare(hdl,
7614217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
7624217Seschrock 					/*
7634217Seschrock 					 * An error occurred so we can't do
7644217Seschrock 					 * anything
7654217Seschrock 					 */
7664217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7674217Seschrock 					    "'%s' cannot be set: problem "
7684217Seschrock 					    "in share initialization"),
7694217Seschrock 					    propname);
7704217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7714217Seschrock 					    errbuf);
7724217Seschrock 					goto error;
7734217Seschrock 				}
7744217Seschrock 
7755331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
7764217Seschrock 					/*
7774217Seschrock 					 * There was an error in parsing so
7784217Seschrock 					 * deal with it by issuing an error
7794217Seschrock 					 * message and leaving after
7804217Seschrock 					 * uninitializing the the libshare
7814217Seschrock 					 * interface.
7824217Seschrock 					 */
7834217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7844217Seschrock 					    "'%s' cannot be set to invalid "
7854217Seschrock 					    "options"), propname);
7864217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7874217Seschrock 					    errbuf);
7884217Seschrock 					zfs_uninit_libshare(hdl);
7894217Seschrock 					goto error;
7904217Seschrock 				}
7914180Sdougm 				zfs_uninit_libshare(hdl);
7924180Sdougm 			}
7934180Sdougm 
7943126Sahl 			break;
7955331Samw 		case ZFS_PROP_UTF8ONLY:
7965331Samw 			chosen_utf = (int)intval;
7975331Samw 			break;
7985331Samw 		case ZFS_PROP_NORMALIZE:
7995331Samw 			chosen_normal = (int)intval;
8005331Samw 			break;
8012676Seschrock 		}
8022676Seschrock 
8032676Seschrock 		/*
8042676Seschrock 		 * For changes to existing volumes, we have some additional
8052676Seschrock 		 * checks to enforce.
8062676Seschrock 		 */
8072676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
8082676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
8092676Seschrock 			    ZFS_PROP_VOLSIZE);
8102676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
8112676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
8122676Seschrock 			char buf[64];
8132676Seschrock 
8142676Seschrock 			switch (prop) {
8152676Seschrock 			case ZFS_PROP_RESERVATION:
8165378Sck153898 			case ZFS_PROP_REFRESERVATION:
8172676Seschrock 				if (intval > volsize) {
8182676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8192676Seschrock 					    "'%s' is greater than current "
8202676Seschrock 					    "volume size"), propname);
8212676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8222676Seschrock 					    errbuf);
8232676Seschrock 					goto error;
8242676Seschrock 				}
8252676Seschrock 				break;
8262676Seschrock 
8272676Seschrock 			case ZFS_PROP_VOLSIZE:
8282676Seschrock 				if (intval % blocksize != 0) {
8292676Seschrock 					zfs_nicenum(blocksize, buf,
8302676Seschrock 					    sizeof (buf));
8312676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8322676Seschrock 					    "'%s' must be a multiple of "
8332676Seschrock 					    "volume block size (%s)"),
8342676Seschrock 					    propname, buf);
8352676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8362676Seschrock 					    errbuf);
8372676Seschrock 					goto error;
8382676Seschrock 				}
8392676Seschrock 
8402676Seschrock 				if (intval == 0) {
8412676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8422676Seschrock 					    "'%s' cannot be zero"),
8432676Seschrock 					    propname);
8442676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8452676Seschrock 					    errbuf);
8462676Seschrock 					goto error;
847789Sahrens 				}
8483126Sahl 				break;
849789Sahrens 			}
850789Sahrens 		}
851789Sahrens 	}
852789Sahrens 
8532676Seschrock 	/*
8545331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
8555331Samw 	 * enforce rejection of non-UTF8 names.
8565331Samw 	 *
8575331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
8585331Samw 	 * was explicitly not chosen, it is an error.
8595331Samw 	 */
8605498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
8615331Samw 		if (nvlist_add_uint64(ret,
8625331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
8635331Samw 			(void) no_memory(hdl);
8645331Samw 			goto error;
8655331Samw 		}
8665498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
8675331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8685331Samw 		    "'%s' must be set 'on' if normalization chosen"),
8695331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
8705331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8715331Samw 		goto error;
8725331Samw 	}
8735331Samw 
8745331Samw 	/*
8752676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
8762676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
8772676Seschrock 	 */
8782676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
8792676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
8802676Seschrock 	    &intval) == 0) {
8812676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
8822676Seschrock 		    ZFS_PROP_VOLSIZE);
8835481Sck153898 		uint64_t old_reservation;
8842676Seschrock 		uint64_t new_reservation;
8855481Sck153898 		zfs_prop_t resv_prop;
8865713Srm160521 
8875713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
8885481Sck153898 			goto error;
8895481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
8902676Seschrock 
8912676Seschrock 		if (old_volsize == old_reservation &&
8925481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
8932676Seschrock 		    &new_reservation) != 0) {
8942676Seschrock 			if (nvlist_add_uint64(ret,
8955481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
8962676Seschrock 				(void) no_memory(hdl);
8972676Seschrock 				goto error;
8982676Seschrock 			}
8992676Seschrock 		}
9002676Seschrock 	}
9012676Seschrock 	return (ret);
9022676Seschrock 
9032676Seschrock error:
9042676Seschrock 	nvlist_free(ret);
9052676Seschrock 	return (NULL);
906789Sahrens }
907789Sahrens 
9084543Smarks static int
9094543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9104543Smarks     uint64_t *ret_who)
9114543Smarks {
9124543Smarks 	struct passwd *pwd;
9134543Smarks 	struct group *grp;
9144543Smarks 	uid_t id;
9154543Smarks 
9164543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9174543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9184543Smarks 		*ret_who = -1;
9194543Smarks 		return (0);
9204543Smarks 	}
9214543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9224543Smarks 		return (EZFS_BADWHO);
9234543Smarks 
9244543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9254543Smarks 	    strcmp(who, "everyone") == 0) {
9264543Smarks 		*ret_who = -1;
9274543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9284543Smarks 		return (0);
9294543Smarks 	}
9304543Smarks 
9314543Smarks 	pwd = getpwnam(who);
9324543Smarks 	grp = getgrnam(who);
9334543Smarks 
9344543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9354543Smarks 		*ret_who = pwd->pw_uid;
9364543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9374543Smarks 		*ret_who = grp->gr_gid;
9384543Smarks 	} else if (pwd) {
9394543Smarks 		*ret_who = pwd->pw_uid;
9404543Smarks 		*who_type = ZFS_DELEG_USER;
9414543Smarks 	} else if (grp) {
9424543Smarks 		*ret_who = grp->gr_gid;
9434543Smarks 		*who_type = ZFS_DELEG_GROUP;
9444543Smarks 	} else {
9454543Smarks 		char *end;
9464543Smarks 
9474543Smarks 		id = strtol(who, &end, 10);
9484543Smarks 		if (errno != 0 || *end != '\0') {
9494543Smarks 			return (EZFS_BADWHO);
9504543Smarks 		} else {
9514543Smarks 			*ret_who = id;
9524543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
9534543Smarks 				*who_type = ZFS_DELEG_USER;
9544543Smarks 		}
9554543Smarks 	}
9564543Smarks 
9574543Smarks 	return (0);
9584543Smarks }
9594543Smarks 
9604543Smarks static void
9614543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
9624543Smarks {
9634543Smarks 	if (perms_nvp != NULL) {
9644543Smarks 		verify(nvlist_add_nvlist(who_nvp,
9654543Smarks 		    name, perms_nvp) == 0);
9664543Smarks 	} else {
9674543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
9684543Smarks 	}
9694543Smarks }
9704543Smarks 
9714543Smarks static void
9724543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
9734543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
9744543Smarks     nvlist_t *sets_nvp)
9754543Smarks {
9764543Smarks 	boolean_t do_perms, do_sets;
9774543Smarks 	char name[ZFS_MAX_DELEG_NAME];
9784543Smarks 
9794543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
9804543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
9814543Smarks 
9824543Smarks 	if (!do_perms && !do_sets)
9834543Smarks 		do_perms = do_sets = B_TRUE;
9844543Smarks 
9854543Smarks 	if (do_perms) {
9864543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
9874543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9884543Smarks 		    whostr : (void *)&whoid);
9894543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
9904543Smarks 	}
9914543Smarks 	if (do_sets) {
9924543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
9934543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9944543Smarks 		    whostr : (void *)&whoid);
9954543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
9964543Smarks 	}
9974543Smarks }
9984543Smarks 
9994543Smarks static void
10004543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
10014543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
10024543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10034543Smarks {
10044543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10054543Smarks 		helper(who_type, whoid, whostr, 0,
10064543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10074543Smarks 	} else {
10084543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10094543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10104543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10114543Smarks 		}
10124543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10134543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10144543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10154543Smarks 		}
10164543Smarks 	}
10174543Smarks }
10184543Smarks 
10194543Smarks /*
10204543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10214543Smarks  *
10224543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10234543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10244543Smarks  * base attribute named stored in the dsl.
10254543Smarks  * Arguments:
10264543Smarks  *
10274543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10284543Smarks  *           whostr may be null for everyone or create perms.
10294543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10304543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10315331Samw  * perms:    common separated list of permissions.  May be null if user
10324543Smarks  *           is requested to remove permissions by who.
10334543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10344543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10354543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10364543Smarks  *           The output nvp will look something like this.
10374543Smarks  *              ul$1234 -> {create ; destroy }
10384543Smarks  *              Ul$1234 -> { @myset }
10394543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10404543Smarks  */
10414543Smarks int
10424543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10434543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10444543Smarks {
10454543Smarks 	nvlist_t *who_nvp;
10464543Smarks 	nvlist_t *perms_nvp = NULL;
10474543Smarks 	nvlist_t *sets_nvp = NULL;
10484543Smarks 	char errbuf[1024];
10494787Sahrens 	char *who_tok, *perm;
10504543Smarks 	int error;
10514543Smarks 
10524543Smarks 	*nvp = NULL;
10534543Smarks 
10544543Smarks 	if (perms) {
10554543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
10564543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10574543Smarks 			return (1);
10584543Smarks 		}
10594543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
10604543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10614543Smarks 			nvlist_free(perms_nvp);
10624543Smarks 			return (1);
10634543Smarks 		}
10644543Smarks 	}
10654543Smarks 
10664543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
10674543Smarks 		if (perms_nvp)
10684543Smarks 			nvlist_free(perms_nvp);
10694543Smarks 		if (sets_nvp)
10704543Smarks 			nvlist_free(sets_nvp);
10714543Smarks 		return (1);
10724543Smarks 	}
10734543Smarks 
10744543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
10754543Smarks 		namecheck_err_t why;
10764543Smarks 		char what;
10774543Smarks 
10784543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
10794787Sahrens 			nvlist_free(who_nvp);
10804787Sahrens 			if (perms_nvp)
10814787Sahrens 				nvlist_free(perms_nvp);
10824787Sahrens 			if (sets_nvp)
10834787Sahrens 				nvlist_free(sets_nvp);
10844787Sahrens 
10854543Smarks 			switch (why) {
10864543Smarks 			case NAME_ERR_NO_AT:
10874543Smarks 				zfs_error_aux(zhp->zfs_hdl,
10884543Smarks 				    dgettext(TEXT_DOMAIN,
10894543Smarks 				    "set definition must begin with an '@' "
10904543Smarks 				    "character"));
10914543Smarks 			}
10924543Smarks 			return (zfs_error(zhp->zfs_hdl,
10934543Smarks 			    EZFS_BADPERMSET, whostr));
10944543Smarks 		}
10954543Smarks 	}
10964543Smarks 
10974543Smarks 	/*
10984543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
10994543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
11004543Smarks 	 * other sets_nvp will have only permssion set names in it.
11014543Smarks 	 */
11024787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11034787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11044787Sahrens 
11054787Sahrens 		if (perm_canonical) {
11064787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11074787Sahrens 			    perm_canonical) == 0);
11084787Sahrens 		} else if (perm[0] == '@') {
11094787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11104787Sahrens 		} else {
11114787Sahrens 			nvlist_free(who_nvp);
11124787Sahrens 			nvlist_free(perms_nvp);
11134787Sahrens 			nvlist_free(sets_nvp);
11144787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11154543Smarks 		}
11164543Smarks 	}
11174543Smarks 
11184543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11194543Smarks 		who_tok = strtok(whostr, ",");
11204543Smarks 		if (who_tok == NULL) {
11214543Smarks 			nvlist_free(who_nvp);
11224787Sahrens 			if (perms_nvp)
11234787Sahrens 				nvlist_free(perms_nvp);
11244543Smarks 			if (sets_nvp)
11254543Smarks 				nvlist_free(sets_nvp);
11264543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11274543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11284543Smarks 			    whostr);
11294543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11304543Smarks 		}
11314543Smarks 	}
11324543Smarks 
11334543Smarks 	/*
11344543Smarks 	 * Now create the nvlist(s)
11354543Smarks 	 */
11364543Smarks 	do {
11374543Smarks 		uint64_t who_id;
11384543Smarks 
11394543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11404543Smarks 		    &who_id);
11414543Smarks 		if (error) {
11424543Smarks 			nvlist_free(who_nvp);
11434787Sahrens 			if (perms_nvp)
11444787Sahrens 				nvlist_free(perms_nvp);
11454543Smarks 			if (sets_nvp)
11464543Smarks 				nvlist_free(sets_nvp);
11474543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11484543Smarks 			    dgettext(TEXT_DOMAIN,
11494543Smarks 			    "Unable to determine uid/gid for "
11504543Smarks 			    "%s "), who_tok);
11514543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11524543Smarks 		}
11534543Smarks 
11544543Smarks 		/*
11554543Smarks 		 * add entries for both local and descendent when required
11564543Smarks 		 */
11574543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
11584543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
11594543Smarks 
11604543Smarks 	} while (who_tok = strtok(NULL, ","));
11614543Smarks 	*nvp = who_nvp;
11624543Smarks 	return (0);
11634543Smarks }
11644543Smarks 
11654543Smarks static int
11664543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
11674543Smarks {
11684543Smarks 	zfs_cmd_t zc = { 0 };
11694543Smarks 	int error;
11704543Smarks 	char errbuf[1024];
11714543Smarks 
11724543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
11734543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
11744543Smarks 	    zhp->zfs_name);
11754543Smarks 
11765094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
11774543Smarks 		return (-1);
11784543Smarks 
11794543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
11804543Smarks 	zc.zc_perm_action = unset;
11814543Smarks 
11824543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
11834543Smarks 	if (error && errno == ENOTSUP) {
11844543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
11854543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
11864543Smarks 		zcmd_free_nvlists(&zc);
11874543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
11884543Smarks 	} else if (error) {
11894543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
11904543Smarks 	}
11914543Smarks 	zcmd_free_nvlists(&zc);
11924543Smarks 
11934543Smarks 	return (error);
11944543Smarks }
11954543Smarks 
11964543Smarks int
11974543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
11984543Smarks {
11994543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12004543Smarks }
12014543Smarks 
12024543Smarks int
12034543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12044543Smarks {
12054543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12064543Smarks }
12074543Smarks 
12084543Smarks static int
12094543Smarks perm_compare(const void *arg1, const void *arg2)
12104543Smarks {
12114543Smarks 	const zfs_perm_node_t *node1 = arg1;
12124543Smarks 	const zfs_perm_node_t *node2 = arg2;
12134543Smarks 	int ret;
12144543Smarks 
12154543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12164543Smarks 
12174543Smarks 	if (ret > 0)
12184543Smarks 		return (1);
12194543Smarks 	if (ret < 0)
12204543Smarks 		return (-1);
12214543Smarks 	else
12224543Smarks 		return (0);
12234543Smarks }
12244543Smarks 
12254543Smarks static void
12264543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12274543Smarks {
12284543Smarks 	zfs_perm_node_t *permnode;
12295367Sahrens 	void *cookie = NULL;
12305367Sahrens 
12315367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12324543Smarks 		free(permnode);
12335367Sahrens 	avl_destroy(tree);
12344543Smarks }
12354543Smarks 
12364543Smarks static void
12374543Smarks zfs_destroy_tree(avl_tree_t *tree)
12384543Smarks {
12394543Smarks 	zfs_allow_node_t *allownode;
12405367Sahrens 	void *cookie = NULL;
12415367Sahrens 
12424543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12434543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12444543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12454543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12464543Smarks 		free(allownode);
12474543Smarks 	}
12485367Sahrens 	avl_destroy(tree);
12494543Smarks }
12504543Smarks 
12514543Smarks void
12524543Smarks zfs_free_allows(zfs_allow_t *allow)
12534543Smarks {
12544543Smarks 	zfs_allow_t *allownext;
12554543Smarks 	zfs_allow_t *freeallow;
12564543Smarks 
12574543Smarks 	allownext = allow;
12584543Smarks 	while (allownext) {
12594543Smarks 		zfs_destroy_tree(&allownext->z_sets);
12604543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
12614543Smarks 		zfs_destroy_tree(&allownext->z_user);
12624543Smarks 		zfs_destroy_tree(&allownext->z_group);
12634543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
12644543Smarks 		freeallow = allownext;
12654543Smarks 		allownext = allownext->z_next;
12664543Smarks 		free(freeallow);
12674543Smarks 	}
12684543Smarks }
12694543Smarks 
12704543Smarks static zfs_allow_t *
12714543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
12724543Smarks {
12734543Smarks 	zfs_allow_t *ptree;
12744543Smarks 
12754543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
12764543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
12774543Smarks 		return (NULL);
12784543Smarks 	}
12794543Smarks 
12804543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
12814543Smarks 	avl_create(&ptree->z_sets,
12824543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12834543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12844543Smarks 	avl_create(&ptree->z_crperms,
12854543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12864543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12874543Smarks 	avl_create(&ptree->z_user,
12884543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12894543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12904543Smarks 	avl_create(&ptree->z_group,
12914543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12924543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12934543Smarks 	avl_create(&ptree->z_everyone,
12944543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12954543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12964543Smarks 
12974543Smarks 	if (prev)
12984543Smarks 		prev->z_next = ptree;
12994543Smarks 	ptree->z_next = NULL;
13004543Smarks 	return (ptree);
13014543Smarks }
13024543Smarks 
13034543Smarks /*
13044543Smarks  * Add permissions to the appropriate AVL permission tree.
13054543Smarks  * The appropriate tree may not be the requested tree.
13064543Smarks  * For example if ld indicates a local permission, but
13074543Smarks  * same permission also exists as a descendent permission
13084543Smarks  * then the permission will be removed from the descendent
13094543Smarks  * tree and add the the local+descendent tree.
13104543Smarks  */
13114543Smarks static int
13124543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13134543Smarks     char *perm, char ld)
13144543Smarks {
13154543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13164543Smarks 	zfs_perm_node_t *newnode;
13174543Smarks 	avl_index_t where, where2;
13184543Smarks 	avl_tree_t *tree, *altree;
13194543Smarks 
13204543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13214543Smarks 
13224543Smarks 	if (ld == ZFS_DELEG_NA) {
13234543Smarks 		tree =  &allownode->z_localdescend;
13244543Smarks 		altree = &allownode->z_descend;
13254543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13264543Smarks 		tree = &allownode->z_local;
13274543Smarks 		altree = &allownode->z_descend;
13284543Smarks 	} else {
13294543Smarks 		tree = &allownode->z_descend;
13304543Smarks 		altree = &allownode->z_local;
13314543Smarks 	}
13324543Smarks 	permnode = avl_find(tree, &pnode, &where);
13334543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13344543Smarks 
13354543Smarks 	if (permnode2) {
13364543Smarks 		avl_remove(altree, permnode2);
13374543Smarks 		free(permnode2);
13384543Smarks 		if (permnode == NULL) {
13394543Smarks 			tree =  &allownode->z_localdescend;
13404543Smarks 		}
13414543Smarks 	}
13424543Smarks 
13434543Smarks 	/*
13444543Smarks 	 * Now insert new permission in either requested location
13454543Smarks 	 * local/descendent or into ld when perm will exist in both.
13464543Smarks 	 */
13474543Smarks 	if (permnode == NULL) {
13484543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13494543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
13504543Smarks 			return (-1);
13514543Smarks 		}
13524543Smarks 		*newnode = pnode;
13534543Smarks 		avl_add(tree, newnode);
13544543Smarks 	}
13554543Smarks 	return (0);
13564543Smarks }
13574577Sahrens 
13584543Smarks /*
13594543Smarks  * Uggh, this is going to be a bit complicated.
13604543Smarks  * we have an nvlist coming out of the kernel that
13614543Smarks  * will indicate where the permission is set and then
13624543Smarks  * it will contain allow of the various "who's", and what
13634543Smarks  * their permissions are.  To further complicate this
13644543Smarks  * we will then have to coalesce the local,descendent
13654543Smarks  * and local+descendent permissions where appropriate.
13664543Smarks  * The kernel only knows about a permission as being local
13674543Smarks  * or descendent, but not both.
13684543Smarks  *
13694543Smarks  * In order to make this easier for zfs_main to deal with
13704543Smarks  * a series of AVL trees will be used to maintain
13714543Smarks  * all of this, primarily for sorting purposes as well
13724543Smarks  * as the ability to quickly locate a specific entry.
13734543Smarks  *
13744543Smarks  * What we end up with are tree's for sets, create perms,
13754543Smarks  * user, groups and everyone.  With each of those trees
13764543Smarks  * we have subtrees for local, descendent and local+descendent
13774543Smarks  * permissions.
13784543Smarks  */
13794543Smarks int
13804543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
13814543Smarks {
13824543Smarks 	zfs_cmd_t zc = { 0 };
13834543Smarks 	int error;
13844543Smarks 	nvlist_t *nvlist;
13854543Smarks 	nvlist_t *permnv, *sourcenv;
13864543Smarks 	nvpair_t *who_pair, *source_pair;
13874543Smarks 	nvpair_t *perm_pair;
13884543Smarks 	char errbuf[1024];
13894543Smarks 	zfs_allow_t *zallowp, *newallowp;
13904543Smarks 	char  ld;
13914543Smarks 	char *nvpname;
13924543Smarks 	uid_t	uid;
13934543Smarks 	gid_t	gid;
13944543Smarks 	avl_tree_t *tree;
13954543Smarks 	avl_index_t where;
13964543Smarks 
13974543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13984543Smarks 
13994543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
14004543Smarks 		return (-1);
14014543Smarks 
14024543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14034543Smarks 		if (errno == ENOMEM) {
14044543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14054543Smarks 				zcmd_free_nvlists(&zc);
14064543Smarks 				return (-1);
14074543Smarks 			}
14084543Smarks 		} else if (errno == ENOTSUP) {
14094543Smarks 			zcmd_free_nvlists(&zc);
14104543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14114543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14124543Smarks 			return (zfs_error(zhp->zfs_hdl,
14134543Smarks 			    EZFS_BADVERSION, errbuf));
14144543Smarks 		} else {
14154543Smarks 			zcmd_free_nvlists(&zc);
14164543Smarks 			return (-1);
14174543Smarks 		}
14184543Smarks 	}
14194543Smarks 
14204543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14214543Smarks 		zcmd_free_nvlists(&zc);
14224543Smarks 		return (-1);
14234543Smarks 	}
14244543Smarks 
14254543Smarks 	zcmd_free_nvlists(&zc);
14264543Smarks 
14274543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14284543Smarks 
14294543Smarks 	if (source_pair == NULL) {
14304543Smarks 		*zfs_perms = NULL;
14314543Smarks 		return (0);
14324543Smarks 	}
14334543Smarks 
14344543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14354543Smarks 	if (*zfs_perms == NULL) {
14364543Smarks 		return (0);
14374543Smarks 	}
14384543Smarks 
14394543Smarks 	zallowp = *zfs_perms;
14404543Smarks 
14414543Smarks 	for (;;) {
14424543Smarks 		struct passwd *pwd;
14434543Smarks 		struct group *grp;
14444543Smarks 		zfs_allow_node_t *allownode;
14454543Smarks 		zfs_allow_node_t  findallownode;
14464543Smarks 		zfs_allow_node_t *newallownode;
14474543Smarks 
14484543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14494543Smarks 		    nvpair_name(source_pair),
14504543Smarks 		    sizeof (zallowp->z_setpoint));
14514543Smarks 
14524543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
14534543Smarks 			goto abort;
14544543Smarks 
14554543Smarks 		/*
14564543Smarks 		 * Make sure nvlist is composed correctly
14574543Smarks 		 */
14584543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
14594543Smarks 			goto abort;
14604543Smarks 		}
14614543Smarks 
14624543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
14634543Smarks 		if (who_pair == NULL) {
14644543Smarks 			goto abort;
14654543Smarks 		}
14664543Smarks 
14674543Smarks 		do {
14684543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
14694543Smarks 			if (error) {
14704543Smarks 				goto abort;
14714543Smarks 			}
14724543Smarks 
14734543Smarks 			/*
14744543Smarks 			 * First build up the key to use
14754543Smarks 			 * for looking up in the various
14764543Smarks 			 * who trees.
14774543Smarks 			 */
14784543Smarks 			ld = nvpair_name(who_pair)[1];
14794543Smarks 			nvpname = nvpair_name(who_pair);
14804543Smarks 			switch (nvpair_name(who_pair)[0]) {
14814543Smarks 			case ZFS_DELEG_USER:
14824543Smarks 			case ZFS_DELEG_USER_SETS:
14834543Smarks 				tree = &zallowp->z_user;
14844543Smarks 				uid = atol(&nvpname[3]);
14854543Smarks 				pwd = getpwuid(uid);
14864543Smarks 				(void) snprintf(findallownode.z_key,
14874543Smarks 				    sizeof (findallownode.z_key), "user %s",
14884543Smarks 				    (pwd) ? pwd->pw_name :
14894543Smarks 				    &nvpair_name(who_pair)[3]);
14904543Smarks 				break;
14914543Smarks 			case ZFS_DELEG_GROUP:
14924543Smarks 			case ZFS_DELEG_GROUP_SETS:
14934543Smarks 				tree = &zallowp->z_group;
14944543Smarks 				gid = atol(&nvpname[3]);
14954543Smarks 				grp = getgrgid(gid);
14964543Smarks 				(void) snprintf(findallownode.z_key,
14974543Smarks 				    sizeof (findallownode.z_key), "group %s",
14984543Smarks 				    (grp) ? grp->gr_name :
14994543Smarks 				    &nvpair_name(who_pair)[3]);
15004543Smarks 				break;
15014543Smarks 			case ZFS_DELEG_CREATE:
15024543Smarks 			case ZFS_DELEG_CREATE_SETS:
15034543Smarks 				tree = &zallowp->z_crperms;
15044543Smarks 				(void) strlcpy(findallownode.z_key, "",
15054543Smarks 				    sizeof (findallownode.z_key));
15064543Smarks 				break;
15074543Smarks 			case ZFS_DELEG_EVERYONE:
15084543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15094543Smarks 				(void) snprintf(findallownode.z_key,
15104543Smarks 				    sizeof (findallownode.z_key), "everyone");
15114543Smarks 				tree = &zallowp->z_everyone;
15124543Smarks 				break;
15134543Smarks 			case ZFS_DELEG_NAMED_SET:
15144543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15154543Smarks 				(void) snprintf(findallownode.z_key,
15164543Smarks 				    sizeof (findallownode.z_key), "%s",
15174543Smarks 				    &nvpair_name(who_pair)[3]);
15184543Smarks 				tree = &zallowp->z_sets;
15194543Smarks 				break;
15204543Smarks 			}
15214543Smarks 
15224543Smarks 			/*
15234543Smarks 			 * Place who in tree
15244543Smarks 			 */
15254543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15264543Smarks 			if (allownode == NULL) {
15274543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15284543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15294543Smarks 					goto abort;
15304543Smarks 				}
15314543Smarks 				avl_create(&newallownode->z_localdescend,
15324543Smarks 				    perm_compare,
15334543Smarks 				    sizeof (zfs_perm_node_t),
15344543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15354543Smarks 				avl_create(&newallownode->z_local,
15364543Smarks 				    perm_compare,
15374543Smarks 				    sizeof (zfs_perm_node_t),
15384543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15394543Smarks 				avl_create(&newallownode->z_descend,
15404543Smarks 				    perm_compare,
15414543Smarks 				    sizeof (zfs_perm_node_t),
15424543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15434543Smarks 				(void) strlcpy(newallownode->z_key,
15444543Smarks 				    findallownode.z_key,
15454543Smarks 				    sizeof (findallownode.z_key));
15464543Smarks 				avl_insert(tree, newallownode, where);
15474543Smarks 				allownode = newallownode;
15484543Smarks 			}
15494543Smarks 
15504543Smarks 			/*
15514543Smarks 			 * Now iterate over the permissions and
15524543Smarks 			 * place them in the appropriate local,
15534543Smarks 			 * descendent or local+descendent tree.
15544543Smarks 			 *
15554543Smarks 			 * The permissions are added to the tree
15564543Smarks 			 * via zfs_coalesce_perm().
15574543Smarks 			 */
15584543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
15594543Smarks 			if (perm_pair == NULL)
15604543Smarks 				goto abort;
15614543Smarks 			do {
15624543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
15634543Smarks 				    nvpair_name(perm_pair), ld) != 0)
15644543Smarks 					goto abort;
15654543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
15664543Smarks 			    perm_pair));
15674543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
15684543Smarks 
15694543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
15704543Smarks 		if (source_pair == NULL)
15714543Smarks 			break;
15724543Smarks 
15734543Smarks 		/*
15744543Smarks 		 * allocate another node from the link list of
15754543Smarks 		 * zfs_allow_t structures
15764543Smarks 		 */
15774543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
15784543Smarks 		    nvpair_name(source_pair));
15794543Smarks 		if (newallowp == NULL) {
15804543Smarks 			goto abort;
15814543Smarks 		}
15824543Smarks 		zallowp = newallowp;
15834543Smarks 	}
15844543Smarks 	nvlist_free(nvlist);
15854543Smarks 	return (0);
15864543Smarks abort:
15874543Smarks 	zfs_free_allows(*zfs_perms);
15884543Smarks 	nvlist_free(nvlist);
15894543Smarks 	return (-1);
15904543Smarks }
15914543Smarks 
15925993Smarks static char *
15935993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
15945993Smarks {
15955993Smarks 	/*
15965993Smarks 	 * Don't put newlines on end of lines
15975993Smarks 	 */
15985993Smarks 	switch (note) {
15995993Smarks 	case ZFS_DELEG_NOTE_CREATE:
16005993Smarks 		return (dgettext(TEXT_DOMAIN,
16015993Smarks 		    "Must also have the 'mount' ability"));
16025993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16035993Smarks 		return (dgettext(TEXT_DOMAIN,
16045993Smarks 		    "Must also have the 'mount' ability"));
16055993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16065993Smarks 		return (dgettext(TEXT_DOMAIN,
16075993Smarks 		    "Must also have the 'mount' ability"));
16085993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16095993Smarks 		return (dgettext(TEXT_DOMAIN,
16105993Smarks 		    "Must also have the 'mount' ability"));
16115993Smarks 	case ZFS_DELEG_NOTE_CLONE:
16125993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16135993Smarks 		    "ability and 'mount'\n"
16145993Smarks 		    "\t\t\t\tability in the origin file system"));
16155993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16165993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16175993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16185993Smarks 	case ZFS_DELEG_NOTE_RENAME:
16195993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16205993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16215993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16225993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16235993Smarks 		    " and 'create' ability"));
16245993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16255993Smarks 		return (dgettext(TEXT_DOMAIN,
16265993Smarks 		    "Allows changing any user property"));
16275993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16285993Smarks 		return (dgettext(TEXT_DOMAIN,
16295993Smarks 		    "Must also have the permission that is being\n"
16305993Smarks 		    "\t\t\t\tallowed"));
16315993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16325993Smarks 		return (dgettext(TEXT_DOMAIN,
16335993Smarks 		    "Allows mount/umount of ZFS datasets"));
16345993Smarks 	case ZFS_DELEG_NOTE_SHARE:
16355993Smarks 		return (dgettext(TEXT_DOMAIN,
16365993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16375993Smarks 		    "\t\t\t\tprotocols"));
16385993Smarks 	case ZFS_DELEG_NOTE_NONE:
16395993Smarks 	default:
16405993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16415993Smarks 	}
16425993Smarks }
16435993Smarks 
16445993Smarks typedef enum {
16455993Smarks 	ZFS_DELEG_SUBCOMMAND,
16465993Smarks 	ZFS_DELEG_PROP,
16475993Smarks 	ZFS_DELEG_OTHER
16485993Smarks } zfs_deleg_perm_type_t;
16495993Smarks 
16505993Smarks /*
16515993Smarks  * is the permission a subcommand or other?
16525993Smarks  */
16535993Smarks zfs_deleg_perm_type_t
16545993Smarks zfs_deleg_perm_type(const char *perm)
16555993Smarks {
16565993Smarks 	if (strcmp(perm, "userprop") == 0)
16575993Smarks 		return (ZFS_DELEG_OTHER);
16585993Smarks 	else
16595993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
16605993Smarks }
16615993Smarks 
16625993Smarks static char *
16635993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
16645993Smarks {
16655993Smarks 	switch (type) {
16665993Smarks 	case ZFS_DELEG_SUBCOMMAND:
16675993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
16685993Smarks 	case ZFS_DELEG_PROP:
16695993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
16705993Smarks 	case ZFS_DELEG_OTHER:
16715993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
16725993Smarks 	}
16735993Smarks 	return ("");
16745993Smarks }
16755993Smarks 
16765993Smarks /*ARGSUSED*/
16775993Smarks static int
16785993Smarks zfs_deleg_prop_cb(int prop, void *cb)
16795993Smarks {
16805993Smarks 	if (zfs_prop_delegatable(prop))
16815993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
16825993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
16835993Smarks 
16845993Smarks 	return (ZPROP_CONT);
16855993Smarks }
16865993Smarks 
16875993Smarks void
16885993Smarks zfs_deleg_permissions(void)
16895993Smarks {
16905993Smarks 	int i;
16915993Smarks 
16925993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
16935993Smarks 	    "TYPE", "NOTES");
16945993Smarks 
16955993Smarks 	/*
16965993Smarks 	 * First print out the subcommands
16975993Smarks 	 */
16985993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
16995993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17005993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17015993Smarks 		    zfs_deleg_perm_type_str(
17025993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17035993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17045993Smarks 	}
17055993Smarks 
17065993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17075993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17085993Smarks }
17095993Smarks 
1710789Sahrens /*
1711789Sahrens  * Given a property name and value, set the property for the given dataset.
1712789Sahrens  */
1713789Sahrens int
17142676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1715789Sahrens {
1716789Sahrens 	zfs_cmd_t zc = { 0 };
17172676Seschrock 	int ret = -1;
17182676Seschrock 	prop_changelist_t *cl = NULL;
17192082Seschrock 	char errbuf[1024];
17202082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17212676Seschrock 	nvlist_t *nvl = NULL, *realprops;
17222676Seschrock 	zfs_prop_t prop;
1723*6168Shs24103 	int do_prefix = 1;
17242082Seschrock 
17252082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
17262676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
17272082Seschrock 	    zhp->zfs_name);
17282082Seschrock 
17292676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
17302676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
17312676Seschrock 		(void) no_memory(hdl);
17322676Seschrock 		goto error;
1733789Sahrens 	}
1734789Sahrens 
17355094Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
17362676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
17372676Seschrock 		goto error;
17385094Slling 
17392676Seschrock 	nvlist_free(nvl);
17402676Seschrock 	nvl = realprops;
17412676Seschrock 
17422676Seschrock 	prop = zfs_name_to_prop(propname);
17432676Seschrock 
1744789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
17452676Seschrock 		goto error;
1746789Sahrens 
1747789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17482082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17492082Seschrock 		    "child dataset with inherited mountpoint is used "
17502082Seschrock 		    "in a non-global zone"));
17512082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1752789Sahrens 		goto error;
1753789Sahrens 	}
1754789Sahrens 
1755*6168Shs24103 
1756*6168Shs24103 	/* do not unmount dataset if canmount is being set to noauto */
1757*6168Shs24103 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
1758*6168Shs24103 		do_prefix = 0;
1759*6168Shs24103 
1760*6168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1761*6168Shs24103 			goto error;
1762789Sahrens 
1763789Sahrens 	/*
1764789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1765789Sahrens 	 */
1766789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1767789Sahrens 
17685094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
17692676Seschrock 		goto error;
17702676Seschrock 
17714543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1772789Sahrens 	if (ret != 0) {
1773789Sahrens 		switch (errno) {
1774789Sahrens 
1775789Sahrens 		case ENOSPC:
1776789Sahrens 			/*
1777789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1778789Sahrens 			 * something different; setting a quota or reservation
1779789Sahrens 			 * doesn't use any disk space.
1780789Sahrens 			 */
1781789Sahrens 			switch (prop) {
1782789Sahrens 			case ZFS_PROP_QUOTA:
17835378Sck153898 			case ZFS_PROP_REFQUOTA:
17842082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17852082Seschrock 				    "size is less than current used or "
17862082Seschrock 				    "reserved space"));
17872082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1788789Sahrens 				break;
1789789Sahrens 
1790789Sahrens 			case ZFS_PROP_RESERVATION:
17915378Sck153898 			case ZFS_PROP_REFRESERVATION:
17922082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17932082Seschrock 				    "size is greater than available space"));
17942082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1795789Sahrens 				break;
1796789Sahrens 
1797789Sahrens 			default:
17982082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1799789Sahrens 				break;
1800789Sahrens 			}
1801789Sahrens 			break;
1802789Sahrens 
1803789Sahrens 		case EBUSY:
18042082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
18052082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
18062082Seschrock 			else
18072676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1808789Sahrens 			break;
1809789Sahrens 
18101175Slling 		case EROFS:
18112082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18121175Slling 			break;
18131175Slling 
18143886Sahl 		case ENOTSUP:
18153886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18165977Smarks 			    "pool and or dataset must be upgraded to set this "
18174603Sahrens 			    "property or value"));
18183886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
18193886Sahl 			break;
18203886Sahl 
1821789Sahrens 		case EOVERFLOW:
1822789Sahrens 			/*
1823789Sahrens 			 * This platform can't address a volume this big.
1824789Sahrens 			 */
1825789Sahrens #ifdef _ILP32
1826789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
18272082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1828789Sahrens 				break;
1829789Sahrens 			}
1830789Sahrens #endif
18312082Seschrock 			/* FALLTHROUGH */
1832789Sahrens 		default:
18332082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1834789Sahrens 		}
1835789Sahrens 	} else {
1836*6168Shs24103 		if (do_prefix)
1837*6168Shs24103 			ret = changelist_postfix(cl);
1838*6168Shs24103 
1839789Sahrens 		/*
1840789Sahrens 		 * Refresh the statistics so the new property value
1841789Sahrens 		 * is reflected.
1842789Sahrens 		 */
1843*6168Shs24103 		if (ret == 0)
18442676Seschrock 			(void) get_stats(zhp);
1845789Sahrens 	}
1846789Sahrens 
1847789Sahrens error:
18482676Seschrock 	nvlist_free(nvl);
18492676Seschrock 	zcmd_free_nvlists(&zc);
18502676Seschrock 	if (cl)
18512676Seschrock 		changelist_free(cl);
1852789Sahrens 	return (ret);
1853789Sahrens }
1854789Sahrens 
1855789Sahrens /*
1856789Sahrens  * Given a property, inherit the value from the parent dataset.
1857789Sahrens  */
1858789Sahrens int
18592676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1860789Sahrens {
1861789Sahrens 	zfs_cmd_t zc = { 0 };
1862789Sahrens 	int ret;
1863789Sahrens 	prop_changelist_t *cl;
18642082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
18652082Seschrock 	char errbuf[1024];
18662676Seschrock 	zfs_prop_t prop;
18672082Seschrock 
18682082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
18692082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1870789Sahrens 
18715094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
18722676Seschrock 		/*
18732676Seschrock 		 * For user properties, the amount of work we have to do is very
18742676Seschrock 		 * small, so just do it here.
18752676Seschrock 		 */
18762676Seschrock 		if (!zfs_prop_user(propname)) {
18772676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18782676Seschrock 			    "invalid property"));
18792676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
18802676Seschrock 		}
18812676Seschrock 
18822676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
18832676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
18842676Seschrock 
18854849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
18862676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
18872676Seschrock 
18882676Seschrock 		return (0);
18892676Seschrock 	}
18902676Seschrock 
1891789Sahrens 	/*
1892789Sahrens 	 * Verify that this property is inheritable.
1893789Sahrens 	 */
18942082Seschrock 	if (zfs_prop_readonly(prop))
18952082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
18962082Seschrock 
18972082Seschrock 	if (!zfs_prop_inheritable(prop))
18982082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1899789Sahrens 
1900789Sahrens 	/*
1901789Sahrens 	 * Check to see if the value applies to this type
1902789Sahrens 	 */
19032082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
19042082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1905789Sahrens 
19063443Srm160521 	/*
19073443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
19083443Srm160521 	 */
19093443Srm160521 	propname = zfs_prop_to_name(prop);
1910789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19112676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1912789Sahrens 
1913789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1914789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
19152082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19162082Seschrock 		    "dataset is used in a non-global zone"));
19172082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1918789Sahrens 	}
1919789Sahrens 
1920789Sahrens 	/*
1921789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1922789Sahrens 	 */
1923789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1924789Sahrens 		return (-1);
1925789Sahrens 
1926789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19282082Seschrock 		    "child dataset with inherited mountpoint is used "
19292082Seschrock 		    "in a non-global zone"));
19302082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1931789Sahrens 		goto error;
1932789Sahrens 	}
1933789Sahrens 
1934789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1935789Sahrens 		goto error;
1936789Sahrens 
19374849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
19382082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1939789Sahrens 	} else {
1940789Sahrens 
19412169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1942789Sahrens 			goto error;
1943789Sahrens 
1944789Sahrens 		/*
1945789Sahrens 		 * Refresh the statistics so the new property is reflected.
1946789Sahrens 		 */
1947789Sahrens 		(void) get_stats(zhp);
1948789Sahrens 	}
1949789Sahrens 
1950789Sahrens error:
1951789Sahrens 	changelist_free(cl);
1952789Sahrens 	return (ret);
1953789Sahrens }
1954789Sahrens 
1955789Sahrens /*
19561356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
19571356Seschrock  * extract them appropriately.
19581356Seschrock  */
19591356Seschrock static uint64_t
19601356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19611356Seschrock {
19621356Seschrock 	nvlist_t *nv;
19631356Seschrock 	uint64_t value;
19641356Seschrock 
19652885Sahrens 	*source = NULL;
19661356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19671356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
19685094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
19695094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19701356Seschrock 	} else {
19711356Seschrock 		value = zfs_prop_default_numeric(prop);
19721356Seschrock 		*source = "";
19731356Seschrock 	}
19741356Seschrock 
19751356Seschrock 	return (value);
19761356Seschrock }
19771356Seschrock 
19781356Seschrock static char *
19791356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19801356Seschrock {
19811356Seschrock 	nvlist_t *nv;
19821356Seschrock 	char *value;
19831356Seschrock 
19842885Sahrens 	*source = NULL;
19851356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19861356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
19875094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
19885094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19891356Seschrock 	} else {
19901356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
19911356Seschrock 			value = "";
19921356Seschrock 		*source = "";
19931356Seschrock 	}
19941356Seschrock 
19951356Seschrock 	return (value);
19961356Seschrock }
19971356Seschrock 
19981356Seschrock /*
1999789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2000789Sahrens  * zfs_prop_get_int() are built using this interface.
2001789Sahrens  *
2002789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2003789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2004789Sahrens  * If they differ from the on-disk values, report the current values and mark
2005789Sahrens  * the source "temporary".
2006789Sahrens  */
20072082Seschrock static int
20085094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
20092082Seschrock     char **source, uint64_t *val)
2010789Sahrens {
20115147Srm160521 	zfs_cmd_t zc = { 0 };
20125592Stimh 	nvlist_t *zplprops = NULL;
2013789Sahrens 	struct mnttab mnt;
20143265Sahrens 	char *mntopt_on = NULL;
20153265Sahrens 	char *mntopt_off = NULL;
2016789Sahrens 
2017789Sahrens 	*source = NULL;
2018789Sahrens 
20193265Sahrens 	switch (prop) {
20203265Sahrens 	case ZFS_PROP_ATIME:
20213265Sahrens 		mntopt_on = MNTOPT_ATIME;
20223265Sahrens 		mntopt_off = MNTOPT_NOATIME;
20233265Sahrens 		break;
20243265Sahrens 
20253265Sahrens 	case ZFS_PROP_DEVICES:
20263265Sahrens 		mntopt_on = MNTOPT_DEVICES;
20273265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
20283265Sahrens 		break;
20293265Sahrens 
20303265Sahrens 	case ZFS_PROP_EXEC:
20313265Sahrens 		mntopt_on = MNTOPT_EXEC;
20323265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
20333265Sahrens 		break;
20343265Sahrens 
20353265Sahrens 	case ZFS_PROP_READONLY:
20363265Sahrens 		mntopt_on = MNTOPT_RO;
20373265Sahrens 		mntopt_off = MNTOPT_RW;
20383265Sahrens 		break;
20393265Sahrens 
20403265Sahrens 	case ZFS_PROP_SETUID:
20413265Sahrens 		mntopt_on = MNTOPT_SETUID;
20423265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
20433265Sahrens 		break;
20443265Sahrens 
20453265Sahrens 	case ZFS_PROP_XATTR:
20463265Sahrens 		mntopt_on = MNTOPT_XATTR;
20473265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
20483265Sahrens 		break;
20495331Samw 
20505331Samw 	case ZFS_PROP_NBMAND:
20515331Samw 		mntopt_on = MNTOPT_NBMAND;
20525331Samw 		mntopt_off = MNTOPT_NONBMAND;
20535331Samw 		break;
20543265Sahrens 	}
20553265Sahrens 
20562474Seschrock 	/*
20572474Seschrock 	 * Because looking up the mount options is potentially expensive
20582474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
20592474Seschrock 	 * we're looking up a property which requires its presence.
20602474Seschrock 	 */
20612474Seschrock 	if (!zhp->zfs_mntcheck &&
20623265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
20633265Sahrens 		struct mnttab entry, search = { 0 };
20643265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
20652474Seschrock 
20662474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
20672474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
20683265Sahrens 		rewind(mnttab);
20693265Sahrens 
20703265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
20713265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
20723265Sahrens 			    entry.mnt_mntopts);
20733265Sahrens 			if (zhp->zfs_mntopts == NULL)
20743265Sahrens 				return (-1);
20753265Sahrens 		}
20762474Seschrock 
20772474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
20782474Seschrock 	}
20792474Seschrock 
2080789Sahrens 	if (zhp->zfs_mntopts == NULL)
2081789Sahrens 		mnt.mnt_mntopts = "";
2082789Sahrens 	else
2083789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2084789Sahrens 
2085789Sahrens 	switch (prop) {
2086789Sahrens 	case ZFS_PROP_ATIME:
20873265Sahrens 	case ZFS_PROP_DEVICES:
20883265Sahrens 	case ZFS_PROP_EXEC:
20893265Sahrens 	case ZFS_PROP_READONLY:
20903265Sahrens 	case ZFS_PROP_SETUID:
20913265Sahrens 	case ZFS_PROP_XATTR:
20925331Samw 	case ZFS_PROP_NBMAND:
20932082Seschrock 		*val = getprop_uint64(zhp, prop, source);
20942082Seschrock 
20953265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
20962082Seschrock 			*val = B_TRUE;
2097789Sahrens 			if (src)
20985094Slling 				*src = ZPROP_SRC_TEMPORARY;
20993265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
21002082Seschrock 			*val = B_FALSE;
2101789Sahrens 			if (src)
21025094Slling 				*src = ZPROP_SRC_TEMPORARY;
2103789Sahrens 		}
21042082Seschrock 		break;
2105789Sahrens 
21063265Sahrens 	case ZFS_PROP_CANMOUNT:
21072082Seschrock 		*val = getprop_uint64(zhp, prop, source);
2108*6168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
21093417Srm160521 			*source = zhp->zfs_name;
21103417Srm160521 		else
21113417Srm160521 			*source = "";	/* default */
21122082Seschrock 		break;
2113789Sahrens 
2114789Sahrens 	case ZFS_PROP_QUOTA:
21155378Sck153898 	case ZFS_PROP_REFQUOTA:
2116789Sahrens 	case ZFS_PROP_RESERVATION:
21175378Sck153898 	case ZFS_PROP_REFRESERVATION:
21182885Sahrens 		*val = getprop_uint64(zhp, prop, source);
21192885Sahrens 		if (*val == 0)
2120789Sahrens 			*source = "";	/* default */
2121789Sahrens 		else
2122789Sahrens 			*source = zhp->zfs_name;
21232082Seschrock 		break;
2124789Sahrens 
2125789Sahrens 	case ZFS_PROP_MOUNTED:
21262082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
21272082Seschrock 		break;
2128789Sahrens 
21293377Seschrock 	case ZFS_PROP_NUMCLONES:
21303377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
21313377Seschrock 		break;
21323377Seschrock 
21335147Srm160521 	case ZFS_PROP_VERSION:
21345498Stimh 	case ZFS_PROP_NORMALIZE:
21355498Stimh 	case ZFS_PROP_UTF8ONLY:
21365498Stimh 	case ZFS_PROP_CASE:
21375498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
21385498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
21395473Srm160521 			return (-1);
21405147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21415498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
21425498Stimh 			zcmd_free_nvlists(&zc);
21435147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21445498Stimh 			    "unable to get %s property"),
21455498Stimh 			    zfs_prop_to_name(prop));
21465147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
21475147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
21485147Srm160521 		}
21495498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
21505498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
21515498Stimh 		    val) != 0) {
21525498Stimh 			zcmd_free_nvlists(&zc);
21535498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21545498Stimh 			    "unable to get %s property"),
21555498Stimh 			    zfs_prop_to_name(prop));
21565498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
21575498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
21585498Stimh 		}
21595592Stimh 		if (zplprops)
21605592Stimh 			nvlist_free(zplprops);
21615498Stimh 		zcmd_free_nvlists(&zc);
21625147Srm160521 		break;
21635147Srm160521 
2164789Sahrens 	default:
21654577Sahrens 		switch (zfs_prop_get_type(prop)) {
21664787Sahrens 		case PROP_TYPE_NUMBER:
21674787Sahrens 		case PROP_TYPE_INDEX:
21684577Sahrens 			*val = getprop_uint64(zhp, prop, source);
21694577Sahrens 			break;
21704577Sahrens 
21714787Sahrens 		case PROP_TYPE_STRING:
21724577Sahrens 		default:
21734577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21744577Sahrens 			    "cannot get non-numeric property"));
21754577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
21764577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
21774577Sahrens 		}
2178789Sahrens 	}
2179789Sahrens 
2180789Sahrens 	return (0);
2181789Sahrens }
2182789Sahrens 
2183789Sahrens /*
2184789Sahrens  * Calculate the source type, given the raw source string.
2185789Sahrens  */
2186789Sahrens static void
21875094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2188789Sahrens     char *statbuf, size_t statlen)
2189789Sahrens {
21905094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2191789Sahrens 		return;
2192789Sahrens 
2193789Sahrens 	if (source == NULL) {
21945094Slling 		*srctype = ZPROP_SRC_NONE;
2195789Sahrens 	} else if (source[0] == '\0') {
21965094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2197789Sahrens 	} else {
2198789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
21995094Slling 			*srctype = ZPROP_SRC_LOCAL;
2200789Sahrens 		} else {
2201789Sahrens 			(void) strlcpy(statbuf, source, statlen);
22025094Slling 			*srctype = ZPROP_SRC_INHERITED;
2203789Sahrens 		}
2204789Sahrens 	}
2205789Sahrens 
2206789Sahrens }
2207789Sahrens 
2208789Sahrens /*
2209789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2210789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2211789Sahrens  * human-readable form.
2212789Sahrens  *
2213789Sahrens  * Returns 0 on success, or -1 on error.
2214789Sahrens  */
2215789Sahrens int
2216789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
22175094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2218789Sahrens {
2219789Sahrens 	char *source = NULL;
2220789Sahrens 	uint64_t val;
2221789Sahrens 	char *str;
2222789Sahrens 	const char *root;
22232676Seschrock 	const char *strval;
2224789Sahrens 
2225789Sahrens 	/*
2226789Sahrens 	 * Check to see if this property applies to our object
2227789Sahrens 	 */
2228789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2229789Sahrens 		return (-1);
2230789Sahrens 
2231789Sahrens 	if (src)
22325094Slling 		*src = ZPROP_SRC_NONE;
2233789Sahrens 
2234789Sahrens 	switch (prop) {
2235789Sahrens 	case ZFS_PROP_CREATION:
2236789Sahrens 		/*
2237789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2238789Sahrens 		 * this into a string unless 'literal' is specified.
2239789Sahrens 		 */
2240789Sahrens 		{
22412885Sahrens 			val = getprop_uint64(zhp, prop, &source);
22422885Sahrens 			time_t time = (time_t)val;
2243789Sahrens 			struct tm t;
2244789Sahrens 
2245789Sahrens 			if (literal ||
2246789Sahrens 			    localtime_r(&time, &t) == NULL ||
2247789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2248789Sahrens 			    &t) == 0)
22492885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2250789Sahrens 		}
2251789Sahrens 		break;
2252789Sahrens 
2253789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2254789Sahrens 		/*
2255789Sahrens 		 * Getting the precise mountpoint can be tricky.
2256789Sahrens 		 *
2257789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2258789Sahrens 		 *  - for default mountpoints, construct it as /zfs/<dataset>
2259789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2260789Sahrens 		 *    after our ancestor and append it to the inherited value.
2261789Sahrens 		 *
2262789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2263789Sahrens 		 * root to any values we return.
2264789Sahrens 		 */
22651544Seschrock 		root = zhp->zfs_root;
22661356Seschrock 		str = getprop_string(zhp, prop, &source);
22671356Seschrock 
22681356Seschrock 		if (str[0] == '\0') {
2269789Sahrens 			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
2270789Sahrens 			    root, zhp->zfs_name);
22711356Seschrock 		} else if (str[0] == '/') {
22721356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2273789Sahrens 
2274789Sahrens 			if (relpath[0] == '/')
2275789Sahrens 				relpath++;
22761356Seschrock 			if (str[1] == '\0')
22771356Seschrock 				str++;
2278789Sahrens 
2279789Sahrens 			if (relpath[0] == '\0')
2280789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
22811356Seschrock 				    root, str);
2282789Sahrens 			else
2283789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
22841356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2285789Sahrens 				    relpath);
2286789Sahrens 		} else {
2287789Sahrens 			/* 'legacy' or 'none' */
22881356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2289789Sahrens 		}
2290789Sahrens 
2291789Sahrens 		break;
2292789Sahrens 
2293789Sahrens 	case ZFS_PROP_ORIGIN:
22942885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2295789Sahrens 		    proplen);
2296789Sahrens 		/*
2297789Sahrens 		 * If there is no parent at all, return failure to indicate that
2298789Sahrens 		 * it doesn't apply to this dataset.
2299789Sahrens 		 */
2300789Sahrens 		if (propbuf[0] == '\0')
2301789Sahrens 			return (-1);
2302789Sahrens 		break;
2303789Sahrens 
2304789Sahrens 	case ZFS_PROP_QUOTA:
23055378Sck153898 	case ZFS_PROP_REFQUOTA:
2306789Sahrens 	case ZFS_PROP_RESERVATION:
23075378Sck153898 	case ZFS_PROP_REFRESERVATION:
23085378Sck153898 
23092082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23102082Seschrock 			return (-1);
2311789Sahrens 
2312789Sahrens 		/*
2313789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2314789Sahrens 		 * (unless literal is set), and indicate that it's the default
2315789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2316789Sahrens 		 * that its set locally.
2317789Sahrens 		 */
2318789Sahrens 		if (val == 0) {
2319789Sahrens 			if (literal)
2320789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2321789Sahrens 			else
2322789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2323789Sahrens 		} else {
2324789Sahrens 			if (literal)
23252856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
23263912Slling 				    (u_longlong_t)val);
2327789Sahrens 			else
2328789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2329789Sahrens 		}
2330789Sahrens 		break;
2331789Sahrens 
2332789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
23332082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23342082Seschrock 			return (-1);
23352856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
23362856Snd150628 		    val / 100, (longlong_t)val % 100);
2337789Sahrens 		break;
2338789Sahrens 
2339789Sahrens 	case ZFS_PROP_TYPE:
2340789Sahrens 		switch (zhp->zfs_type) {
2341789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2342789Sahrens 			str = "filesystem";
2343789Sahrens 			break;
2344789Sahrens 		case ZFS_TYPE_VOLUME:
2345789Sahrens 			str = "volume";
2346789Sahrens 			break;
2347789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2348789Sahrens 			str = "snapshot";
2349789Sahrens 			break;
2350789Sahrens 		default:
23512082Seschrock 			abort();
2352789Sahrens 		}
2353789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2354789Sahrens 		break;
2355789Sahrens 
2356789Sahrens 	case ZFS_PROP_MOUNTED:
2357789Sahrens 		/*
2358789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2359789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2360789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2361789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2362789Sahrens 		 */
23632082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
23642082Seschrock 		    src, &source, &val) != 0)
23652082Seschrock 			return (-1);
23662082Seschrock 		if (val)
2367789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2368789Sahrens 		else
2369789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2370789Sahrens 		break;
2371789Sahrens 
2372789Sahrens 	case ZFS_PROP_NAME:
2373789Sahrens 		/*
2374789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2375789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2376789Sahrens 		 * consumers.
2377789Sahrens 		 */
2378789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2379789Sahrens 		break;
2380789Sahrens 
2381789Sahrens 	default:
23824577Sahrens 		switch (zfs_prop_get_type(prop)) {
23834787Sahrens 		case PROP_TYPE_NUMBER:
23844577Sahrens 			if (get_numeric_property(zhp, prop, src,
23854577Sahrens 			    &source, &val) != 0)
23864577Sahrens 				return (-1);
23874577Sahrens 			if (literal)
23884577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
23894577Sahrens 				    (u_longlong_t)val);
23904577Sahrens 			else
23914577Sahrens 				zfs_nicenum(val, propbuf, proplen);
23924577Sahrens 			break;
23934577Sahrens 
23944787Sahrens 		case PROP_TYPE_STRING:
23954577Sahrens 			(void) strlcpy(propbuf,
23964577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
23974577Sahrens 			break;
23984577Sahrens 
23994787Sahrens 		case PROP_TYPE_INDEX:
24004861Sahrens 			if (get_numeric_property(zhp, prop, src,
24014861Sahrens 			    &source, &val) != 0)
24024861Sahrens 				return (-1);
24034861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
24044577Sahrens 				return (-1);
24054577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
24064577Sahrens 			break;
24074577Sahrens 
24084577Sahrens 		default:
24094577Sahrens 			abort();
24104577Sahrens 		}
2411789Sahrens 	}
2412789Sahrens 
2413789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2414789Sahrens 
2415789Sahrens 	return (0);
2416789Sahrens }
2417789Sahrens 
2418789Sahrens /*
2419789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2420789Sahrens  * the given property is the appropriate type; should only be used with
2421789Sahrens  * hard-coded property types.
2422789Sahrens  */
2423789Sahrens uint64_t
2424789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2425789Sahrens {
2426789Sahrens 	char *source;
24272082Seschrock 	uint64_t val;
24282082Seschrock 
24295367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
24302082Seschrock 
24312082Seschrock 	return (val);
2432789Sahrens }
2433789Sahrens 
24345713Srm160521 int
24355713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
24365713Srm160521 {
24375713Srm160521 	char buf[64];
24385713Srm160521 
24395713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
24405713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
24415713Srm160521 }
24425713Srm160521 
2443789Sahrens /*
2444789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2445789Sahrens  */
2446789Sahrens int
2447789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
24485094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2449789Sahrens {
2450789Sahrens 	char *source;
2451789Sahrens 
2452789Sahrens 	/*
2453789Sahrens 	 * Check to see if this property applies to our object
2454789Sahrens 	 */
24554849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
24563237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
24572082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
24582082Seschrock 		    zfs_prop_to_name(prop)));
24594849Sahrens 	}
2460789Sahrens 
2461789Sahrens 	if (src)
24625094Slling 		*src = ZPROP_SRC_NONE;
2463789Sahrens 
24642082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
24652082Seschrock 		return (-1);
2466789Sahrens 
2467789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2468789Sahrens 
2469789Sahrens 	return (0);
2470789Sahrens }
2471789Sahrens 
2472789Sahrens /*
2473789Sahrens  * Returns the name of the given zfs handle.
2474789Sahrens  */
2475789Sahrens const char *
2476789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2477789Sahrens {
2478789Sahrens 	return (zhp->zfs_name);
2479789Sahrens }
2480789Sahrens 
2481789Sahrens /*
2482789Sahrens  * Returns the type of the given zfs handle.
2483789Sahrens  */
2484789Sahrens zfs_type_t
2485789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2486789Sahrens {
2487789Sahrens 	return (zhp->zfs_type);
2488789Sahrens }
2489789Sahrens 
2490789Sahrens /*
24911356Seschrock  * Iterate over all child filesystems
2492789Sahrens  */
2493789Sahrens int
24941356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2495789Sahrens {
2496789Sahrens 	zfs_cmd_t zc = { 0 };
2497789Sahrens 	zfs_handle_t *nzhp;
2498789Sahrens 	int ret;
2499789Sahrens 
25005367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25015367Sahrens 		return (0);
25025367Sahrens 
2503789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25042082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2505789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2506789Sahrens 		/*
2507789Sahrens 		 * Ignore private dataset names.
2508789Sahrens 		 */
2509789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2510789Sahrens 			continue;
2511789Sahrens 
2512789Sahrens 		/*
2513789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2514789Sahrens 		 * that the pool has since been removed.
2515789Sahrens 		 */
25162082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25172082Seschrock 		    zc.zc_name)) == NULL)
2518789Sahrens 			continue;
2519789Sahrens 
2520789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2521789Sahrens 			return (ret);
2522789Sahrens 	}
2523789Sahrens 
2524789Sahrens 	/*
2525789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2526789Sahrens 	 * returned, then the underlying dataset has been removed since we
2527789Sahrens 	 * obtained the handle.
2528789Sahrens 	 */
2529789Sahrens 	if (errno != ESRCH && errno != ENOENT)
25302082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
25312082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2532789Sahrens 
25331356Seschrock 	return (0);
25341356Seschrock }
25351356Seschrock 
25361356Seschrock /*
25371356Seschrock  * Iterate over all snapshots
25381356Seschrock  */
25391356Seschrock int
25401356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25411356Seschrock {
25421356Seschrock 	zfs_cmd_t zc = { 0 };
25431356Seschrock 	zfs_handle_t *nzhp;
25441356Seschrock 	int ret;
2545789Sahrens 
25465367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
25475367Sahrens 		return (0);
25485367Sahrens 
2549789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25502082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
25512082Seschrock 	    &zc) == 0;
2552789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2553789Sahrens 
25542082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25552082Seschrock 		    zc.zc_name)) == NULL)
2556789Sahrens 			continue;
2557789Sahrens 
2558789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2559789Sahrens 			return (ret);
2560789Sahrens 	}
2561789Sahrens 
2562789Sahrens 	/*
2563789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2564789Sahrens 	 * returned, then the underlying dataset has been removed since we
2565789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2566789Sahrens 	 */
2567789Sahrens 	if (errno != ESRCH && errno != ENOENT)
25682082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
25692082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2570789Sahrens 
2571789Sahrens 	return (0);
2572789Sahrens }
2573789Sahrens 
2574789Sahrens /*
25751356Seschrock  * Iterate over all children, snapshots and filesystems
25761356Seschrock  */
25771356Seschrock int
25781356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25791356Seschrock {
25801356Seschrock 	int ret;
25811356Seschrock 
25821356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
25831356Seschrock 		return (ret);
25841356Seschrock 
25851356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
25861356Seschrock }
25871356Seschrock 
25881356Seschrock /*
2589789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2590789Sahrens  * Can return NULL if this is a pool.
2591789Sahrens  */
2592789Sahrens static int
2593789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2594789Sahrens {
2595789Sahrens 	char *loc;
2596789Sahrens 
2597789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2598789Sahrens 		return (-1);
2599789Sahrens 
2600789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2601789Sahrens 	buf[loc - path] = '\0';
2602789Sahrens 
2603789Sahrens 	return (0);
2604789Sahrens }
2605789Sahrens 
2606789Sahrens /*
26074490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
26084490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26094490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
26104490Svb160487  * length of already existing prefix of the given path.  We also fetch the
26114490Svb160487  * 'zoned' property, which is used to validate property settings when creating
26124490Svb160487  * new datasets.
2613789Sahrens  */
2614789Sahrens static int
26154490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
26164490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2617789Sahrens {
2618789Sahrens 	zfs_cmd_t zc = { 0 };
2619789Sahrens 	char parent[ZFS_MAXNAMELEN];
2620789Sahrens 	char *slash;
26211356Seschrock 	zfs_handle_t *zhp;
26222082Seschrock 	char errbuf[1024];
26232082Seschrock 
26242082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
26252082Seschrock 	    path);
2626789Sahrens 
2627789Sahrens 	/* get parent, and check to see if this is just a pool */
2628789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
26292082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26302082Seschrock 		    "missing dataset name"));
26312082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2632789Sahrens 	}
2633789Sahrens 
2634789Sahrens 	/* check to see if the pool exists */
2635789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2636789Sahrens 		slash = parent + strlen(parent);
2637789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2638789Sahrens 	zc.zc_name[slash - parent] = '\0';
26392082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2640789Sahrens 	    errno == ENOENT) {
26412082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26422082Seschrock 		    "no such pool '%s'"), zc.zc_name);
26432082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2644789Sahrens 	}
2645789Sahrens 
2646789Sahrens 	/* check to see if the parent dataset exists */
26474490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
26484490Svb160487 		if (errno == ENOENT && accept_ancestor) {
26494490Svb160487 			/*
26504490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
26514490Svb160487 			 */
26524490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
26534490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26544490Svb160487 				    "no such pool '%s'"), zc.zc_name);
26554490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
26564490Svb160487 			}
26574490Svb160487 		} else if (errno == ENOENT) {
26582082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26592082Seschrock 			    "parent does not exist"));
26602082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
26614490Svb160487 		} else
26622082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2663789Sahrens 	}
2664789Sahrens 
26652676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2666789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
26672676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
26682082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
26691356Seschrock 		zfs_close(zhp);
2670789Sahrens 		return (-1);
2671789Sahrens 	}
2672789Sahrens 
2673789Sahrens 	/* make sure parent is a filesystem */
26741356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
26752082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26762082Seschrock 		    "parent is not a filesystem"));
26772082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
26781356Seschrock 		zfs_close(zhp);
2679789Sahrens 		return (-1);
2680789Sahrens 	}
2681789Sahrens 
26821356Seschrock 	zfs_close(zhp);
26834490Svb160487 	if (prefixlen != NULL)
26844490Svb160487 		*prefixlen = strlen(parent);
26854490Svb160487 	return (0);
26864490Svb160487 }
26874490Svb160487 
26884490Svb160487 /*
26894490Svb160487  * Finds whether the dataset of the given type(s) exists.
26904490Svb160487  */
26914490Svb160487 boolean_t
26924490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
26934490Svb160487 {
26944490Svb160487 	zfs_handle_t *zhp;
26954490Svb160487 
26965326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
26974490Svb160487 		return (B_FALSE);
26984490Svb160487 
26994490Svb160487 	/*
27004490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27014490Svb160487 	 */
27024490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27034490Svb160487 		int ds_type = zhp->zfs_type;
27044490Svb160487 
27054490Svb160487 		zfs_close(zhp);
27064490Svb160487 		if (types & ds_type)
27074490Svb160487 			return (B_TRUE);
27084490Svb160487 	}
27094490Svb160487 	return (B_FALSE);
27104490Svb160487 }
27114490Svb160487 
27124490Svb160487 /*
27135367Sahrens  * Given a path to 'target', create all the ancestors between
27145367Sahrens  * the prefixlen portion of the path, and the target itself.
27155367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
27165367Sahrens  */
27175367Sahrens int
27185367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
27195367Sahrens {
27205367Sahrens 	zfs_handle_t *h;
27215367Sahrens 	char *cp;
27225367Sahrens 	const char *opname;
27235367Sahrens 
27245367Sahrens 	/* make sure prefix exists */
27255367Sahrens 	cp = target + prefixlen;
27265367Sahrens 	if (*cp != '/') {
27275367Sahrens 		assert(strchr(cp, '/') == NULL);
27285367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27295367Sahrens 	} else {
27305367Sahrens 		*cp = '\0';
27315367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27325367Sahrens 		*cp = '/';
27335367Sahrens 	}
27345367Sahrens 	if (h == NULL)
27355367Sahrens 		return (-1);
27365367Sahrens 	zfs_close(h);
27375367Sahrens 
27385367Sahrens 	/*
27395367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
27405367Sahrens 	 * up to the prefixlen-long one.
27415367Sahrens 	 */
27425367Sahrens 	for (cp = target + prefixlen + 1;
27435367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
27445367Sahrens 		char *logstr;
27455367Sahrens 
27465367Sahrens 		*cp = '\0';
27475367Sahrens 
27485367Sahrens 		h = make_dataset_handle(hdl, target);
27495367Sahrens 		if (h) {
27505367Sahrens 			/* it already exists, nothing to do here */
27515367Sahrens 			zfs_close(h);
27525367Sahrens 			continue;
27535367Sahrens 		}
27545367Sahrens 
27555367Sahrens 		logstr = hdl->libzfs_log_str;
27565367Sahrens 		hdl->libzfs_log_str = NULL;
27575367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
27585367Sahrens 		    NULL) != 0) {
27595367Sahrens 			hdl->libzfs_log_str = logstr;
27605367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
27615367Sahrens 			goto ancestorerr;
27625367Sahrens 		}
27635367Sahrens 
27645367Sahrens 		hdl->libzfs_log_str = logstr;
27655367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27665367Sahrens 		if (h == NULL) {
27675367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
27685367Sahrens 			goto ancestorerr;
27695367Sahrens 		}
27705367Sahrens 
27715367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
27725367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
27735367Sahrens 			goto ancestorerr;
27745367Sahrens 		}
27755367Sahrens 
27765367Sahrens 		if (zfs_share(h) != 0) {
27775367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
27785367Sahrens 			goto ancestorerr;
27795367Sahrens 		}
27805367Sahrens 
27815367Sahrens 		zfs_close(h);
27825367Sahrens 	}
27835367Sahrens 
27845367Sahrens 	return (0);
27855367Sahrens 
27865367Sahrens ancestorerr:
27875367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27885367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
27895367Sahrens 	return (-1);
27905367Sahrens }
27915367Sahrens 
27925367Sahrens /*
27934490Svb160487  * Creates non-existing ancestors of the given path.
27944490Svb160487  */
27954490Svb160487 int
27964490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
27974490Svb160487 {
27984490Svb160487 	int prefix;
27994490Svb160487 	uint64_t zoned;
28004490Svb160487 	char *path_copy;
28014490Svb160487 	int rc;
28024490Svb160487 
28034490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28044490Svb160487 		return (-1);
28054490Svb160487 
28064490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28074490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28084490Svb160487 		free(path_copy);
28094490Svb160487 	}
28104490Svb160487 	if (path_copy == NULL || rc != 0)
28114490Svb160487 		return (-1);
28124490Svb160487 
2813789Sahrens 	return (0);
2814789Sahrens }
2815789Sahrens 
2816789Sahrens /*
28172676Seschrock  * Create a new filesystem or volume.
2818789Sahrens  */
2819789Sahrens int
28202082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28212676Seschrock     nvlist_t *props)
2822789Sahrens {
2823789Sahrens 	zfs_cmd_t zc = { 0 };
2824789Sahrens 	int ret;
2825789Sahrens 	uint64_t size = 0;
2826789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
28272082Seschrock 	char errbuf[1024];
28282676Seschrock 	uint64_t zoned;
28292082Seschrock 
28302082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28312082Seschrock 	    "cannot create '%s'"), path);
2832789Sahrens 
2833789Sahrens 	/* validate the path, taking care to note the extended error message */
28345326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
28352082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2836789Sahrens 
2837789Sahrens 	/* validate parents exist */
28384490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2839789Sahrens 		return (-1);
2840789Sahrens 
2841789Sahrens 	/*
2842789Sahrens 	 * The failure modes when creating a dataset of a different type over
2843789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2844789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2845789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2846789Sahrens 	 * first try to see if the dataset exists.
2847789Sahrens 	 */
2848789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
28495094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
28502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28512082Seschrock 		    "dataset already exists"));
28522082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2853789Sahrens 	}
2854789Sahrens 
2855789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2856789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2857789Sahrens 	else
2858789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2859789Sahrens 
28605094Slling 	if (props && (props = zfs_validate_properties(hdl, type, props,
28613912Slling 	    zoned, NULL, errbuf)) == 0)
28622676Seschrock 		return (-1);
28632676Seschrock 
2864789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
28651133Seschrock 		/*
28661133Seschrock 		 * If we are creating a volume, the size and block size must
28671133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
28681133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
28691133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
28701133Seschrock 		 * zero.
28711133Seschrock 		 */
28722676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
28732676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
28742676Seschrock 			nvlist_free(props);
28752082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28762676Seschrock 			    "missing volume size"));
28772676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2878789Sahrens 		}
2879789Sahrens 
28802676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
28812676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
28822676Seschrock 		    &blocksize)) != 0) {
28832676Seschrock 			if (ret == ENOENT) {
28842676Seschrock 				blocksize = zfs_prop_default_numeric(
28852676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
28862676Seschrock 			} else {
28872676Seschrock 				nvlist_free(props);
28882676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28892676Seschrock 				    "missing volume block size"));
28902676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
28912676Seschrock 			}
28922676Seschrock 		}
28932676Seschrock 
28942676Seschrock 		if (size == 0) {
28952676Seschrock 			nvlist_free(props);
28962082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28972676Seschrock 			    "volume size cannot be zero"));
28982676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
28991133Seschrock 		}
29001133Seschrock 
29011133Seschrock 		if (size % blocksize != 0) {
29022676Seschrock 			nvlist_free(props);
29032082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29042676Seschrock 			    "volume size must be a multiple of volume block "
29052676Seschrock 			    "size"));
29062676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29071133Seschrock 		}
2908789Sahrens 	}
2909789Sahrens 
29105094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
29112676Seschrock 		return (-1);
29122676Seschrock 	nvlist_free(props);
29132676Seschrock 
2914789Sahrens 	/* create the dataset */
29154543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2916789Sahrens 
29173912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29182082Seschrock 		ret = zvol_create_link(hdl, path);
29193912Slling 		if (ret) {
29203912Slling 			(void) zfs_standard_error(hdl, errno,
29213912Slling 			    dgettext(TEXT_DOMAIN,
29223912Slling 			    "Volume successfully created, but device links "
29233912Slling 			    "were not created"));
29243912Slling 			zcmd_free_nvlists(&zc);
29253912Slling 			return (-1);
29263912Slling 		}
29273912Slling 	}
2928789Sahrens 
29292676Seschrock 	zcmd_free_nvlists(&zc);
29302676Seschrock 
2931789Sahrens 	/* check for failure */
2932789Sahrens 	if (ret != 0) {
2933789Sahrens 		char parent[ZFS_MAXNAMELEN];
2934789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2935789Sahrens 
2936789Sahrens 		switch (errno) {
2937789Sahrens 		case ENOENT:
29382082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29392082Seschrock 			    "no such parent '%s'"), parent);
29402082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2941789Sahrens 
2942789Sahrens 		case EINVAL:
29432082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29443413Smmusante 			    "parent '%s' is not a filesystem"), parent);
29452082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2946789Sahrens 
2947789Sahrens 		case EDOM:
29482082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29492676Seschrock 			    "volume block size must be power of 2 from "
29502676Seschrock 			    "%u to %uk"),
2951789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2952789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
29532082Seschrock 
29542676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29552082Seschrock 
29564603Sahrens 		case ENOTSUP:
29574603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29584603Sahrens 			    "pool must be upgraded to set this "
29594603Sahrens 			    "property or value"));
29604603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
29614603Sahrens 
2962789Sahrens #ifdef _ILP32
2963789Sahrens 		case EOVERFLOW:
2964789Sahrens 			/*
2965789Sahrens 			 * This platform can't address a volume this big.
2966789Sahrens 			 */
29672082Seschrock 			if (type == ZFS_TYPE_VOLUME)
29682082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
29692082Seschrock 				    errbuf));
2970789Sahrens #endif
29712082Seschrock 			/* FALLTHROUGH */
2972789Sahrens 		default:
29732082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2974789Sahrens 		}
2975789Sahrens 	}
2976789Sahrens 
2977789Sahrens 	return (0);
2978789Sahrens }
2979789Sahrens 
2980789Sahrens /*
2981789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2982789Sahrens  * isn't mounted, and that there are no active dependents.
2983789Sahrens  */
2984789Sahrens int
2985789Sahrens zfs_destroy(zfs_handle_t *zhp)
2986789Sahrens {
2987789Sahrens 	zfs_cmd_t zc = { 0 };
2988789Sahrens 
2989789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2990789Sahrens 
29912676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
29923126Sahl 		/*
29934543Smarks 		 * If user doesn't have permissions to unshare volume, then
29944543Smarks 		 * abort the request.  This would only happen for a
29954543Smarks 		 * non-privileged user.
29963126Sahl 		 */
29974543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
29984543Smarks 			return (-1);
29994543Smarks 		}
30003126Sahl 
30012082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3002789Sahrens 			return (-1);
3003789Sahrens 
3004789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3005789Sahrens 	} else {
3006789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3007789Sahrens 	}
3008789Sahrens 
30094543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30103237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30112082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30122082Seschrock 		    zhp->zfs_name));
30132199Sahrens 	}
3014789Sahrens 
3015789Sahrens 	remove_mountpoint(zhp);
3016789Sahrens 
3017789Sahrens 	return (0);
3018789Sahrens }
3019789Sahrens 
30202199Sahrens struct destroydata {
30212199Sahrens 	char *snapname;
30222199Sahrens 	boolean_t gotone;
30233265Sahrens 	boolean_t closezhp;
30242199Sahrens };
30252199Sahrens 
30262199Sahrens static int
30272199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
30282199Sahrens {
30292199Sahrens 	struct destroydata *dd = arg;
30302199Sahrens 	zfs_handle_t *szhp;
30312199Sahrens 	char name[ZFS_MAXNAMELEN];
30323265Sahrens 	boolean_t closezhp = dd->closezhp;
30333265Sahrens 	int rv;
30342199Sahrens 
30352676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
30362676Seschrock 	(void) strlcat(name, "@", sizeof (name));
30372676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
30382199Sahrens 
30392199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
30402199Sahrens 	if (szhp) {
30412199Sahrens 		dd->gotone = B_TRUE;
30422199Sahrens 		zfs_close(szhp);
30432199Sahrens 	}
30442199Sahrens 
30452199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
30462199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
30472199Sahrens 		/*
30482199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
30492199Sahrens 		 * return an error, because then we wouldn't visit all
30502199Sahrens 		 * the volumes.
30512199Sahrens 		 */
30522199Sahrens 	}
30532199Sahrens 
30543265Sahrens 	dd->closezhp = B_TRUE;
30553265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
30563265Sahrens 	if (closezhp)
30573265Sahrens 		zfs_close(zhp);
30583265Sahrens 	return (rv);
30592199Sahrens }
30602199Sahrens 
30612199Sahrens /*
30622199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
30632199Sahrens  */
30642199Sahrens int
30652199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
30662199Sahrens {
30672199Sahrens 	zfs_cmd_t zc = { 0 };
30682199Sahrens 	int ret;
30692199Sahrens 	struct destroydata dd = { 0 };
30702199Sahrens 
30712199Sahrens 	dd.snapname = snapname;
30722199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
30732199Sahrens 
30742199Sahrens 	if (!dd.gotone) {
30753237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
30762199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
30772199Sahrens 		    zhp->zfs_name, snapname));
30782199Sahrens 	}
30792199Sahrens 
30802199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
30812676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
30822199Sahrens 
30834543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
30842199Sahrens 	if (ret != 0) {
30852199Sahrens 		char errbuf[1024];
30862199Sahrens 
30872199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30882199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
30892199Sahrens 
30902199Sahrens 		switch (errno) {
30912199Sahrens 		case EEXIST:
30922199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
30932199Sahrens 			    "snapshot is cloned"));
30942199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
30952199Sahrens 
30962199Sahrens 		default:
30972199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
30982199Sahrens 			    errbuf));
30992199Sahrens 		}
31002199Sahrens 	}
31012199Sahrens 
31022199Sahrens 	return (0);
31032199Sahrens }
31042199Sahrens 
3105789Sahrens /*
3106789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3107789Sahrens  */
3108789Sahrens int
31092676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3110789Sahrens {
3111789Sahrens 	zfs_cmd_t zc = { 0 };
3112789Sahrens 	char parent[ZFS_MAXNAMELEN];
3113789Sahrens 	int ret;
31142082Seschrock 	char errbuf[1024];
31152082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31162676Seschrock 	zfs_type_t type;
31172676Seschrock 	uint64_t zoned;
3118789Sahrens 
3119789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3120789Sahrens 
31212082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31222082Seschrock 	    "cannot create '%s'"), target);
31232082Seschrock 
3124789Sahrens 	/* validate the target name */
31255326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
31262082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3127789Sahrens 
3128789Sahrens 	/* validate parents exist */
31294490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3130789Sahrens 		return (-1);
3131789Sahrens 
3132789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3133789Sahrens 
3134789Sahrens 	/* do the clone */
31352676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3136789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
31372744Snn35248 		type = ZFS_TYPE_VOLUME;
31382676Seschrock 	} else {
3139789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
31402744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
31412676Seschrock 	}
31422676Seschrock 
31432676Seschrock 	if (props) {
31445094Slling 		if ((props = zfs_validate_properties(hdl, type, props,
31453912Slling 		    zoned, zhp, errbuf)) == NULL)
31462676Seschrock 			return (-1);
31472676Seschrock 
31485094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
31492676Seschrock 			nvlist_free(props);
31502676Seschrock 			return (-1);
31512676Seschrock 		}
31522676Seschrock 
31532676Seschrock 		nvlist_free(props);
31542676Seschrock 	}
3155789Sahrens 
3156789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
31572676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
31584543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3159789Sahrens 
31602676Seschrock 	zcmd_free_nvlists(&zc);
31612676Seschrock 
3162789Sahrens 	if (ret != 0) {
3163789Sahrens 		switch (errno) {
3164789Sahrens 
3165789Sahrens 		case ENOENT:
3166789Sahrens 			/*
3167789Sahrens 			 * The parent doesn't exist.  We should have caught this
3168789Sahrens 			 * above, but there may a race condition that has since
3169789Sahrens 			 * destroyed the parent.
3170789Sahrens 			 *
3171789Sahrens 			 * At this point, we don't know whether it's the source
3172789Sahrens 			 * that doesn't exist anymore, or whether the target
3173789Sahrens 			 * dataset doesn't exist.
3174789Sahrens 			 */
31752082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31762082Seschrock 			    "no such parent '%s'"), parent);
31772082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
31782082Seschrock 
31792082Seschrock 		case EXDEV:
31802082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31812082Seschrock 			    "source and target pools differ"));
31822082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
31832082Seschrock 			    errbuf));
31842082Seschrock 
31852082Seschrock 		default:
31862082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31872082Seschrock 			    errbuf));
31882082Seschrock 		}
31892676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
31902082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
31912082Seschrock 	}
31922082Seschrock 
31932082Seschrock 	return (ret);
31942082Seschrock }
31952082Seschrock 
31962082Seschrock typedef struct promote_data {
31972082Seschrock 	char cb_mountpoint[MAXPATHLEN];
31982082Seschrock 	const char *cb_target;
31992082Seschrock 	const char *cb_errbuf;
32002082Seschrock 	uint64_t cb_pivot_txg;
32012082Seschrock } promote_data_t;
32022082Seschrock 
32032082Seschrock static int
32042082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32052082Seschrock {
32062082Seschrock 	promote_data_t *pd = data;
32072082Seschrock 	zfs_handle_t *szhp;
32082082Seschrock 	char snapname[MAXPATHLEN];
32093265Sahrens 	int rv = 0;
32102082Seschrock 
32112082Seschrock 	/* We don't care about snapshots after the pivot point */
32123265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32133265Sahrens 		zfs_close(zhp);
32142082Seschrock 		return (0);
32153265Sahrens 	}
32162082Seschrock 
32172417Sahrens 	/* Remove the device link if it's a zvol. */
32182676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32192417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32202082Seschrock 
32212082Seschrock 	/* Check for conflicting names */
32222676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32232676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
32242082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
32252082Seschrock 	if (szhp != NULL) {
32262082Seschrock 		zfs_close(szhp);
32272082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32282082Seschrock 		    "snapshot name '%s' from origin \n"
32292082Seschrock 		    "conflicts with '%s' from target"),
32302082Seschrock 		    zhp->zfs_name, snapname);
32313265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
32322082Seschrock 	}
32333265Sahrens 	zfs_close(zhp);
32343265Sahrens 	return (rv);
32352082Seschrock }
32362082Seschrock 
32372417Sahrens static int
32382417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
32392417Sahrens {
32402417Sahrens 	promote_data_t *pd = data;
32412417Sahrens 
32422417Sahrens 	/* We don't care about snapshots after the pivot point */
32433265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
32443265Sahrens 		/* Create the device link if it's a zvol. */
32453265Sahrens 		if (ZFS_IS_VOLUME(zhp))
32463265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
32473265Sahrens 	}
32483265Sahrens 
32493265Sahrens 	zfs_close(zhp);
32502417Sahrens 	return (0);
32512417Sahrens }
32522417Sahrens 
32532082Seschrock /*
32542082Seschrock  * Promotes the given clone fs to be the clone parent.
32552082Seschrock  */
32562082Seschrock int
32572082Seschrock zfs_promote(zfs_handle_t *zhp)
32582082Seschrock {
32592082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
32602082Seschrock 	zfs_cmd_t zc = { 0 };
32612082Seschrock 	char parent[MAXPATHLEN];
32622082Seschrock 	char *cp;
32632082Seschrock 	int ret;
32642082Seschrock 	zfs_handle_t *pzhp;
32652082Seschrock 	promote_data_t pd;
32662082Seschrock 	char errbuf[1024];
32672082Seschrock 
32682082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32692082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
32702082Seschrock 
32712082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
32722082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32732082Seschrock 		    "snapshots can not be promoted"));
32742082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
32752082Seschrock 	}
32762082Seschrock 
32775367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
32782082Seschrock 	if (parent[0] == '\0') {
32792082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32802082Seschrock 		    "not a cloned filesystem"));
32812082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
32822082Seschrock 	}
32832082Seschrock 	cp = strchr(parent, '@');
32842082Seschrock 	*cp = '\0';
32852082Seschrock 
32862082Seschrock 	/* Walk the snapshots we will be moving */
32875367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
32882082Seschrock 	if (pzhp == NULL)
32892082Seschrock 		return (-1);
32902082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
32912082Seschrock 	zfs_close(pzhp);
32922082Seschrock 	pd.cb_target = zhp->zfs_name;
32932082Seschrock 	pd.cb_errbuf = errbuf;
32945094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
32952082Seschrock 	if (pzhp == NULL)
32962082Seschrock 		return (-1);
32972082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
32982082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
32992082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33002417Sahrens 	if (ret != 0) {
33012417Sahrens 		zfs_close(pzhp);
33022082Seschrock 		return (-1);
33032417Sahrens 	}
33042082Seschrock 
33052082Seschrock 	/* issue the ioctl */
33065367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
33072676Seschrock 	    sizeof (zc.zc_value));
33082082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33094543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33102082Seschrock 
33112082Seschrock 	if (ret != 0) {
33122417Sahrens 		int save_errno = errno;
33132417Sahrens 
33142417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33152417Sahrens 		zfs_close(pzhp);
33162417Sahrens 
33172417Sahrens 		switch (save_errno) {
3318789Sahrens 		case EEXIST:
3319789Sahrens 			/*
33202082Seschrock 			 * There is a conflicting snapshot name.  We
33212082Seschrock 			 * should have caught this above, but they could
33222082Seschrock 			 * have renamed something in the mean time.
3323789Sahrens 			 */
33242082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33252082Seschrock 			    "conflicting snapshot name from parent '%s'"),
33262082Seschrock 			    parent);
33272082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3328789Sahrens 
3329789Sahrens 		default:
33302417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3331789Sahrens 		}
33322417Sahrens 	} else {
33332417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3334789Sahrens 	}
3335789Sahrens 
33362417Sahrens 	zfs_close(pzhp);
3337789Sahrens 	return (ret);
3338789Sahrens }
3339789Sahrens 
33404007Smmusante struct createdata {
33414007Smmusante 	const char *cd_snapname;
33424007Smmusante 	int cd_ifexists;
33434007Smmusante };
33444007Smmusante 
33452199Sahrens static int
33462199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
33472199Sahrens {
33484007Smmusante 	struct createdata *cd = arg;
33492676Seschrock 	int ret;
33502199Sahrens 
33512199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33522199Sahrens 		char name[MAXPATHLEN];
33532199Sahrens 
33542676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
33552676Seschrock 		(void) strlcat(name, "@", sizeof (name));
33564007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
33574007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
33584007Smmusante 		    cd->cd_ifexists);
33592199Sahrens 		/*
33602199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
33612199Sahrens 		 * return an error, because then we wouldn't visit all
33622199Sahrens 		 * the volumes.
33632199Sahrens 		 */
33642199Sahrens 	}
33652676Seschrock 
33664007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
33672676Seschrock 
33682676Seschrock 	zfs_close(zhp);
33692676Seschrock 
33702676Seschrock 	return (ret);
33712199Sahrens }
33722199Sahrens 
3373789Sahrens /*
33743504Sahl  * Takes a snapshot of the given dataset.
3375789Sahrens  */
3376789Sahrens int
33772199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3378789Sahrens {
3379789Sahrens 	const char *delim;
3380789Sahrens 	char *parent;
3381789Sahrens 	zfs_handle_t *zhp;
3382789Sahrens 	zfs_cmd_t zc = { 0 };
3383789Sahrens 	int ret;
33842082Seschrock 	char errbuf[1024];
33852082Seschrock 
33862082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33872082Seschrock 	    "cannot snapshot '%s'"), path);
33882082Seschrock 
33892082Seschrock 	/* validate the target name */
33905326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
33912082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3392789Sahrens 
3393789Sahrens 	/* make sure the parent exists and is of the appropriate type */
33942199Sahrens 	delim = strchr(path, '@');
33952082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
33962082Seschrock 		return (-1);
3397789Sahrens 	(void) strncpy(parent, path, delim - path);
3398789Sahrens 	parent[delim - path] = '\0';
3399789Sahrens 
34002082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3401789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3402789Sahrens 		free(parent);
3403789Sahrens 		return (-1);
3404789Sahrens 	}
3405789Sahrens 
34062199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34072676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34084543Smarks 	if (ZFS_IS_VOLUME(zhp))
34094543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34104543Smarks 	else
34114543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34122199Sahrens 	zc.zc_cookie = recursive;
34134543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
34142199Sahrens 
34152199Sahrens 	/*
34162199Sahrens 	 * if it was recursive, the one that actually failed will be in
34172199Sahrens 	 * zc.zc_name.
34182199Sahrens 	 */
34194543Smarks 	if (ret != 0)
34204543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34214543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
34224543Smarks 
34232199Sahrens 	if (ret == 0 && recursive) {
34244007Smmusante 		struct createdata cd;
34254007Smmusante 
34264007Smmusante 		cd.cd_snapname = delim + 1;
34274007Smmusante 		cd.cd_ifexists = B_FALSE;
34284007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
34292199Sahrens 	}
3430789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
34312082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
34322199Sahrens 		if (ret != 0) {
34334543Smarks 			(void) zfs_standard_error(hdl, errno,
34344543Smarks 			    dgettext(TEXT_DOMAIN,
34354543Smarks 			    "Volume successfully snapshotted, but device links "
34364543Smarks 			    "were not created"));
34374543Smarks 			free(parent);
34384543Smarks 			zfs_close(zhp);
34394543Smarks 			return (-1);
34402199Sahrens 		}
3441789Sahrens 	}
3442789Sahrens 
34432082Seschrock 	if (ret != 0)
34442082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3445789Sahrens 
3446789Sahrens 	free(parent);
3447789Sahrens 	zfs_close(zhp);
3448789Sahrens 
3449789Sahrens 	return (ret);
3450789Sahrens }
3451789Sahrens 
3452789Sahrens /*
34531294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
34541294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
34551294Slling  * is a dependent and we should just destroy it without checking the transaction
34561294Slling  * group.
3457789Sahrens  */
34581294Slling typedef struct rollback_data {
34591294Slling 	const char	*cb_target;		/* the snapshot */
34601294Slling 	uint64_t	cb_create;		/* creation time reference */
34615749Sahrens 	boolean_t	cb_error;
34622082Seschrock 	boolean_t	cb_dependent;
34635749Sahrens 	boolean_t	cb_force;
34641294Slling } rollback_data_t;
34651294Slling 
34661294Slling static int
34671294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
34681294Slling {
34691294Slling 	rollback_data_t *cbp = data;
34701294Slling 
34711294Slling 	if (!cbp->cb_dependent) {
34721294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
34731294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
34741294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
34751294Slling 		    cbp->cb_create) {
34764543Smarks 			char *logstr;
34771294Slling 
34782082Seschrock 			cbp->cb_dependent = B_TRUE;
34795446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
34805446Sahrens 			    rollback_destroy, cbp);
34812082Seschrock 			cbp->cb_dependent = B_FALSE;
34821294Slling 
34834543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
34844543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
34855446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
34864543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
34871294Slling 		}
34881294Slling 	} else {
34895749Sahrens 		/* We must destroy this clone; first unmount it */
34905749Sahrens 		prop_changelist_t *clp;
34915749Sahrens 
34925749Sahrens 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
34935749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
34945749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
34955749Sahrens 			cbp->cb_error = B_TRUE;
34965749Sahrens 			zfs_close(zhp);
34975749Sahrens 			return (0);
34985749Sahrens 		}
34995749Sahrens 		if (zfs_destroy(zhp) != 0)
35005749Sahrens 			cbp->cb_error = B_TRUE;
35015749Sahrens 		else
35025749Sahrens 			changelist_remove(clp, zhp->zfs_name);
35035751Sahrens 		(void) changelist_postfix(clp);
35045749Sahrens 		changelist_free(clp);
35051294Slling 	}
35061294Slling 
35071294Slling 	zfs_close(zhp);
35081294Slling 	return (0);
35091294Slling }
35101294Slling 
35111294Slling /*
35125446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35135446Sahrens  * data changes since then and making it the active dataset.
35145446Sahrens  *
35155446Sahrens  * Any snapshots more recent than the target are destroyed, along with
35165446Sahrens  * their dependents.
35171294Slling  */
35185446Sahrens int
35195749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3520789Sahrens {
35215446Sahrens 	rollback_data_t cb = { 0 };
35225446Sahrens 	int err;
3523789Sahrens 	zfs_cmd_t zc = { 0 };
35245713Srm160521 	boolean_t restore_resv = 0;
35255713Srm160521 	uint64_t old_volsize, new_volsize;
35265713Srm160521 	zfs_prop_t resv_prop;
3527789Sahrens 
3528789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3529789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3530789Sahrens 
35315446Sahrens 	/*
35325446Sahrens 	 * Destroy all recent snapshots and its dependends.
35335446Sahrens 	 */
35345749Sahrens 	cb.cb_force = force;
35355446Sahrens 	cb.cb_target = snap->zfs_name;
35365446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
35375446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
35385446Sahrens 
35395749Sahrens 	if (cb.cb_error)
35405749Sahrens 		return (-1);
35415446Sahrens 
35425446Sahrens 	/*
35435446Sahrens 	 * Now that we have verified that the snapshot is the latest,
35445446Sahrens 	 * rollback to the given snapshot.
35455446Sahrens 	 */
35465446Sahrens 
35475713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
35485713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
35495713Srm160521 			return (-1);
35505713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
35515713Srm160521 			return (-1);
35525713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35535713Srm160521 		restore_resv =
35545713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
35555713Srm160521 	}
3556789Sahrens 
3557789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3558789Sahrens 
35592676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3560789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3561789Sahrens 	else
3562789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3563789Sahrens 
3564789Sahrens 	/*
35655446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
35665446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
35675446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
35685446Sahrens 	 * an unlikely race condition where the user has taken a
35695446Sahrens 	 * snapshot since we verified that this was the most recent.
35705713Srm160521 	 *
3571789Sahrens 	 */
35725446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
35733237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
35742082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
35752082Seschrock 		    zhp->zfs_name);
35765717Srm160521 		return (err);
35775717Srm160521 	}
35785713Srm160521 
35795713Srm160521 	/*
35805713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
35815713Srm160521 	 * rollback reservation and the volsize has changed then set
35825713Srm160521 	 * the reservation property to the post-rollback volsize.
35835713Srm160521 	 * Make a new handle since the rollback closed the dataset.
35845713Srm160521 	 */
35855717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
35865717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
35875717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
35885717Srm160521 			zfs_close(zhp);
35895713Srm160521 			return (err);
35905717Srm160521 		}
35915713Srm160521 		if (restore_resv) {
35925713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35935713Srm160521 			if (old_volsize != new_volsize)
35945717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
35955717Srm160521 				    new_volsize);
35965713Srm160521 		}
35975713Srm160521 		zfs_close(zhp);
3598789Sahrens 	}
35995446Sahrens 	return (err);
36001294Slling }
36011294Slling 
36021294Slling /*
3603789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3604789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3605789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3606789Sahrens  * libzfs_graph.c.
3607789Sahrens  */
3608789Sahrens int
36092474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36102474Seschrock     zfs_iter_f func, void *data)
3611789Sahrens {
3612789Sahrens 	char **dependents;
3613789Sahrens 	size_t count;
3614789Sahrens 	int i;
3615789Sahrens 	zfs_handle_t *child;
3616789Sahrens 	int ret = 0;
3617789Sahrens 
36182474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
36192474Seschrock 	    &dependents, &count) != 0)
36202474Seschrock 		return (-1);
36212474Seschrock 
3622789Sahrens 	for (i = 0; i < count; i++) {
36232082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
36242082Seschrock 		    dependents[i])) == NULL)
3625789Sahrens 			continue;
3626789Sahrens 
3627789Sahrens 		if ((ret = func(child, data)) != 0)
3628789Sahrens 			break;
3629789Sahrens 	}
3630789Sahrens 
3631789Sahrens 	for (i = 0; i < count; i++)
3632789Sahrens 		free(dependents[i]);
3633789Sahrens 	free(dependents);
3634789Sahrens 
3635789Sahrens 	return (ret);
3636789Sahrens }
3637789Sahrens 
3638789Sahrens /*
3639789Sahrens  * Renames the given dataset.
3640789Sahrens  */
3641789Sahrens int
36424490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3643789Sahrens {
3644789Sahrens 	int ret;
3645789Sahrens 	zfs_cmd_t zc = { 0 };
3646789Sahrens 	char *delim;
36474007Smmusante 	prop_changelist_t *cl = NULL;
36484007Smmusante 	zfs_handle_t *zhrp = NULL;
36494007Smmusante 	char *parentname = NULL;
3650789Sahrens 	char parent[ZFS_MAXNAMELEN];
36512082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
36522082Seschrock 	char errbuf[1024];
3653789Sahrens 
3654789Sahrens 	/* if we have the same exact name, just return success */
3655789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3656789Sahrens 		return (0);
3657789Sahrens 
36582082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36592082Seschrock 	    "cannot rename to '%s'"), target);
36602082Seschrock 
3661789Sahrens 	/*
3662789Sahrens 	 * Make sure the target name is valid
3663789Sahrens 	 */
3664789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
36652665Snd150628 		if ((strchr(target, '@') == NULL) ||
36662665Snd150628 		    *target == '@') {
36672665Snd150628 			/*
36682665Snd150628 			 * Snapshot target name is abbreviated,
36692665Snd150628 			 * reconstruct full dataset name
36702665Snd150628 			 */
36712665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
36722665Snd150628 			    sizeof (parent));
36732665Snd150628 			delim = strchr(parent, '@');
36742665Snd150628 			if (strchr(target, '@') == NULL)
36752665Snd150628 				*(++delim) = '\0';
36762665Snd150628 			else
36772665Snd150628 				*delim = '\0';
36782665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
36792665Snd150628 			target = parent;
36802665Snd150628 		} else {
36812665Snd150628 			/*
36822665Snd150628 			 * Make sure we're renaming within the same dataset.
36832665Snd150628 			 */
36842665Snd150628 			delim = strchr(target, '@');
36852665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
36862665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
36872665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36882665Snd150628 				    "snapshots must be part of same "
36892665Snd150628 				    "dataset"));
36902665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
36913912Slling 				    errbuf));
36922665Snd150628 			}
3693789Sahrens 		}
36945326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
36952665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3696789Sahrens 	} else {
36974007Smmusante 		if (recursive) {
36984007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36994007Smmusante 			    "recursive rename must be a snapshot"));
37004007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
37014007Smmusante 		}
37024007Smmusante 
37035326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37042665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37052676Seschrock 		uint64_t unused;
37062676Seschrock 
3707789Sahrens 		/* validate parents */
37084490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3709789Sahrens 			return (-1);
3710789Sahrens 
3711789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3712789Sahrens 
3713789Sahrens 		/* make sure we're in the same pool */
3714789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3715789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3716789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
37172082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37182082Seschrock 			    "datasets must be within same pool"));
37192082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3720789Sahrens 		}
37212440Snd150628 
37222440Snd150628 		/* new name cannot be a child of the current dataset name */
37232440Snd150628 		if (strncmp(parent, zhp->zfs_name,
37243912Slling 		    strlen(zhp->zfs_name)) == 0) {
37252440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37262440Snd150628 			    "New dataset name cannot be a descendent of "
37272440Snd150628 			    "current dataset name"));
37282440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37292440Snd150628 		}
3730789Sahrens 	}
3731789Sahrens 
37322082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
37332082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
37342082Seschrock 
3735789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3736789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
37372082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37382082Seschrock 		    "dataset is used in a non-global zone"));
37392082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3740789Sahrens 	}
3741789Sahrens 
37424007Smmusante 	if (recursive) {
37434007Smmusante 		struct destroydata dd;
37444007Smmusante 
37454183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
37464183Smmusante 		if (parentname == NULL) {
37474183Smmusante 			ret = -1;
37484183Smmusante 			goto error;
37494183Smmusante 		}
37504007Smmusante 		delim = strchr(parentname, '@');
37514007Smmusante 		*delim = '\0';
37525094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
37534007Smmusante 		if (zhrp == NULL) {
37544183Smmusante 			ret = -1;
37554183Smmusante 			goto error;
37564007Smmusante 		}
37574007Smmusante 
37584007Smmusante 		dd.snapname = delim + 1;
37594007Smmusante 		dd.gotone = B_FALSE;
37604183Smmusante 		dd.closezhp = B_TRUE;
37614007Smmusante 
37624007Smmusante 		/* We remove any zvol links prior to renaming them */
37634007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
37644007Smmusante 		if (ret) {
37654007Smmusante 			goto error;
37664007Smmusante 		}
37674007Smmusante 	} else {
37684007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
37694007Smmusante 			return (-1);
37704007Smmusante 
37714007Smmusante 		if (changelist_haszonedchild(cl)) {
37724007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37734007Smmusante 			    "child dataset with inherited mountpoint is used "
37744007Smmusante 			    "in a non-global zone"));
37754007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
37764007Smmusante 			goto error;
37774007Smmusante 		}
37784007Smmusante 
37794007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
37804007Smmusante 			goto error;
3781789Sahrens 	}
3782789Sahrens 
37832676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3784789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3785789Sahrens 	else
3786789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3787789Sahrens 
37882665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
37892676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
37902665Snd150628 
37914007Smmusante 	zc.zc_cookie = recursive;
37924007Smmusante 
37934543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
37944007Smmusante 		/*
37954007Smmusante 		 * if it was recursive, the one that actually failed will
37964007Smmusante 		 * be in zc.zc_name
37974007Smmusante 		 */
37984007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37995367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
38004007Smmusante 
38014007Smmusante 		if (recursive && errno == EEXIST) {
38024007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38034007Smmusante 			    "a child dataset already has a snapshot "
38044007Smmusante 			    "with the new name"));
38054801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38064007Smmusante 		} else {
38074007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
38084007Smmusante 		}
3809789Sahrens 
3810789Sahrens 		/*
3811789Sahrens 		 * On failure, we still want to remount any filesystems that
3812789Sahrens 		 * were previously mounted, so we don't alter the system state.
3813789Sahrens 		 */
38144007Smmusante 		if (recursive) {
38154007Smmusante 			struct createdata cd;
38164007Smmusante 
38174007Smmusante 			/* only create links for datasets that had existed */
38184007Smmusante 			cd.cd_snapname = delim + 1;
38194007Smmusante 			cd.cd_ifexists = B_TRUE;
38204007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38214007Smmusante 			    &cd);
38224007Smmusante 		} else {
38234007Smmusante 			(void) changelist_postfix(cl);
38244007Smmusante 		}
3825789Sahrens 	} else {
38264007Smmusante 		if (recursive) {
38274007Smmusante 			struct createdata cd;
38284007Smmusante 
38294007Smmusante 			/* only create links for datasets that had existed */
38304007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
38314007Smmusante 			cd.cd_ifexists = B_TRUE;
38324007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38334007Smmusante 			    &cd);
38344007Smmusante 		} else {
38354007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
38364007Smmusante 			ret = changelist_postfix(cl);
38374007Smmusante 		}
3838789Sahrens 	}
3839789Sahrens 
3840789Sahrens error:
38414007Smmusante 	if (parentname) {
38424007Smmusante 		free(parentname);
38434007Smmusante 	}
38444007Smmusante 	if (zhrp) {
38454007Smmusante 		zfs_close(zhrp);
38464007Smmusante 	}
38474007Smmusante 	if (cl) {
38484007Smmusante 		changelist_free(cl);
38494007Smmusante 	}
3850789Sahrens 	return (ret);
3851789Sahrens }
3852789Sahrens 
3853789Sahrens /*
3854789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3855789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3856789Sahrens  */
3857789Sahrens int
38582082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3859789Sahrens {
38604007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
38614007Smmusante }
38624007Smmusante 
38634007Smmusante static int
38644007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
38654007Smmusante {
3866789Sahrens 	zfs_cmd_t zc = { 0 };
38672082Seschrock 	di_devlink_handle_t dhdl;
38684543Smarks 	priv_set_t *priv_effective;
38694543Smarks 	int privileged;
3870789Sahrens 
3871789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3872789Sahrens 
3873789Sahrens 	/*
3874789Sahrens 	 * Issue the appropriate ioctl.
3875789Sahrens 	 */
38762082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3877789Sahrens 		switch (errno) {
3878789Sahrens 		case EEXIST:
3879789Sahrens 			/*
3880789Sahrens 			 * Silently ignore the case where the link already
3881789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3882789Sahrens 			 * times without errors.
3883789Sahrens 			 */
3884789Sahrens 			return (0);
3885789Sahrens 
38864007Smmusante 		case ENOENT:
38874007Smmusante 			/*
38884007Smmusante 			 * Dataset does not exist in the kernel.  If we
38894007Smmusante 			 * don't care (see zfs_rename), then ignore the
38904007Smmusante 			 * error quietly.
38914007Smmusante 			 */
38924007Smmusante 			if (ifexists) {
38934007Smmusante 				return (0);
38944007Smmusante 			}
38954007Smmusante 
38964007Smmusante 			/* FALLTHROUGH */
38974007Smmusante 
3898789Sahrens 		default:
38993237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39002082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39012082Seschrock 			    "for '%s'"), dataset));
3902789Sahrens 		}
3903789Sahrens 	}
3904789Sahrens 
3905789Sahrens 	/*
39064543Smarks 	 * If privileged call devfsadm and wait for the links to
39074543Smarks 	 * magically appear.
39084543Smarks 	 * Otherwise, print out an informational message.
3909789Sahrens 	 */
39104543Smarks 
39114543Smarks 	priv_effective = priv_allocset();
39124543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
39134543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
39144543Smarks 	priv_freeset(priv_effective);
39154543Smarks 
39164543Smarks 	if (privileged) {
39174543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
39184543Smarks 		    DI_MAKE_LINK)) == NULL) {
39194543Smarks 			zfs_error_aux(hdl, strerror(errno));
39204543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
39214543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39224543Smarks 			    "for '%s'"), dataset);
39234543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
39244543Smarks 			return (-1);
39254543Smarks 		} else {
39264543Smarks 			(void) di_devlink_fini(&dhdl);
39274543Smarks 		}
3928789Sahrens 	} else {
39294543Smarks 		char pathname[MAXPATHLEN];
39304543Smarks 		struct stat64 statbuf;
39314543Smarks 		int i;
39324543Smarks 
39334543Smarks #define	MAX_WAIT	10
39344543Smarks 
39354543Smarks 		/*
39364543Smarks 		 * This is the poor mans way of waiting for the link
39374543Smarks 		 * to show up.  If after 10 seconds we still don't
39384543Smarks 		 * have it, then print out a message.
39394543Smarks 		 */
39404543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
39414543Smarks 		    dataset);
39424543Smarks 
39434543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
39444543Smarks 			if (stat64(pathname, &statbuf) == 0)
39454543Smarks 				break;
39464543Smarks 			(void) sleep(1);
39474543Smarks 		}
39484543Smarks 		if (i == MAX_WAIT)
39494543Smarks 			(void) printf(gettext("%s may not be immediately "
39504543Smarks 			    "available\n"), pathname);
3951789Sahrens 	}
3952789Sahrens 
3953789Sahrens 	return (0);
3954789Sahrens }
3955789Sahrens 
3956789Sahrens /*
3957789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3958789Sahrens  */
3959789Sahrens int
39602082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3961789Sahrens {
3962789Sahrens 	zfs_cmd_t zc = { 0 };
3963789Sahrens 
3964789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3965789Sahrens 
39662082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3967789Sahrens 		switch (errno) {
3968789Sahrens 		case ENXIO:
3969789Sahrens 			/*
3970789Sahrens 			 * Silently ignore the case where the link no longer
3971789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3972789Sahrens 			 * times without errors.
3973789Sahrens 			 */
3974789Sahrens 			return (0);
3975789Sahrens 
3976789Sahrens 		default:
39773237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39782082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
39792082Seschrock 			    "links for '%s'"), dataset));
3980789Sahrens 		}
3981789Sahrens 	}
3982789Sahrens 
3983789Sahrens 	return (0);
3984789Sahrens }
39852676Seschrock 
39862676Seschrock nvlist_t *
39872676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
39882676Seschrock {
39892676Seschrock 	return (zhp->zfs_user_props);
39902676Seschrock }
39912676Seschrock 
39922676Seschrock /*
39933912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
39943912Slling  * display, and their maximum widths.  This does two main things:
39953912Slling  *
39963912Slling  *      - If this is a list of all properties, then expand the list to include
39973912Slling  *        all native properties, and set a flag so that for each dataset we look
39983912Slling  *        for new unique user properties and add them to the list.
39993912Slling  *
40003912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
40013912Slling  *        so that we can size the column appropriately.
40023912Slling  */
40033912Slling int
40045094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
40053912Slling {
40063912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40075094Slling 	zprop_list_t *entry;
40085094Slling 	zprop_list_t **last, **start;
40093912Slling 	nvlist_t *userprops, *propval;
40103912Slling 	nvpair_t *elem;
40113912Slling 	char *strval;
40123912Slling 	char buf[ZFS_MAXPROPLEN];
40133912Slling 
40145094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
40153912Slling 		return (-1);
40162676Seschrock 
40172676Seschrock 	userprops = zfs_get_user_props(zhp);
40182676Seschrock 
40192676Seschrock 	entry = *plp;
40202676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
40212676Seschrock 		/*
40222676Seschrock 		 * Go through and add any user properties as necessary.  We
40232676Seschrock 		 * start by incrementing our list pointer to the first
40242676Seschrock 		 * non-native property.
40252676Seschrock 		 */
40262676Seschrock 		start = plp;
40272676Seschrock 		while (*start != NULL) {
40285094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
40292676Seschrock 				break;
40302676Seschrock 			start = &(*start)->pl_next;
40312676Seschrock 		}
40322676Seschrock 
40332676Seschrock 		elem = NULL;
40342676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
40352676Seschrock 			/*
40362676Seschrock 			 * See if we've already found this property in our list.
40372676Seschrock 			 */
40382676Seschrock 			for (last = start; *last != NULL;
40392676Seschrock 			    last = &(*last)->pl_next) {
40402676Seschrock 				if (strcmp((*last)->pl_user_prop,
40412676Seschrock 				    nvpair_name(elem)) == 0)
40422676Seschrock 					break;
40432676Seschrock 			}
40442676Seschrock 
40452676Seschrock 			if (*last == NULL) {
40462676Seschrock 				if ((entry = zfs_alloc(hdl,
40475094Slling 				    sizeof (zprop_list_t))) == NULL ||
40482676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
40492676Seschrock 				    nvpair_name(elem)))) == NULL) {
40502676Seschrock 					free(entry);
40512676Seschrock 					return (-1);
40522676Seschrock 				}
40532676Seschrock 
40545094Slling 				entry->pl_prop = ZPROP_INVAL;
40552676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
40562676Seschrock 				entry->pl_all = B_TRUE;
40572676Seschrock 				*last = entry;
40582676Seschrock 			}
40592676Seschrock 		}
40602676Seschrock 	}
40612676Seschrock 
40622676Seschrock 	/*
40632676Seschrock 	 * Now go through and check the width of any non-fixed columns
40642676Seschrock 	 */
40652676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
40662676Seschrock 		if (entry->pl_fixed)
40672676Seschrock 			continue;
40682676Seschrock 
40695094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
40702676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
40712676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
40722676Seschrock 				if (strlen(buf) > entry->pl_width)
40732676Seschrock 					entry->pl_width = strlen(buf);
40742676Seschrock 			}
40752676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
40762676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
40772676Seschrock 			verify(nvlist_lookup_string(propval,
40785094Slling 			    ZPROP_VALUE, &strval) == 0);
40792676Seschrock 			if (strlen(strval) > entry->pl_width)
40802676Seschrock 				entry->pl_width = strlen(strval);
40812676Seschrock 		}
40822676Seschrock 	}
40832676Seschrock 
40842676Seschrock 	return (0);
40852676Seschrock }
40864543Smarks 
40874543Smarks int
40884543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
40894543Smarks {
40904543Smarks 	zfs_cmd_t zc = { 0 };
40914543Smarks 	nvlist_t *nvp;
40924543Smarks 	gid_t gid;
40934543Smarks 	uid_t uid;
40944543Smarks 	const gid_t *groups;
40954543Smarks 	int group_cnt;
40964543Smarks 	int error;
40974543Smarks 
40984543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
40994543Smarks 		return (no_memory(hdl));
41004543Smarks 
41014543Smarks 	uid = ucred_geteuid(cred);
41024543Smarks 	gid = ucred_getegid(cred);
41034543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
41044543Smarks 
41054543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
41064543Smarks 		return (1);
41074543Smarks 
41084543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
41094543Smarks 		nvlist_free(nvp);
41104543Smarks 		return (1);
41114543Smarks 	}
41124543Smarks 
41134543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
41144543Smarks 		nvlist_free(nvp);
41154543Smarks 		return (1);
41164543Smarks 	}
41174543Smarks 
41184543Smarks 	if (nvlist_add_uint32_array(nvp,
41194543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
41204543Smarks 		nvlist_free(nvp);
41214543Smarks 		return (1);
41224543Smarks 	}
41234543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
41244543Smarks 
41255094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
41264543Smarks 		return (-1);
41274543Smarks 
41284543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
41294543Smarks 	nvlist_free(nvp);
41304543Smarks 	return (error);
41314543Smarks }
41324543Smarks 
41334543Smarks int
41344543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
41355331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
41364543Smarks {
41374543Smarks 	zfs_cmd_t zc = { 0 };
41384543Smarks 	int error;
41394543Smarks 
41404543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
41414543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
41424543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
41434543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
41445331Samw 	zc.zc_share.z_sharetype = operation;
41454543Smarks 	zc.zc_share.z_sharemax = sharemax;
41464543Smarks 
41474543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
41484543Smarks 	return (error);
41494543Smarks }
4150