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 {
2196423Sgw25295 	if (type == ZFS_TYPE_POOL)
2206423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2215326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222789Sahrens }
223789Sahrens 
224789Sahrens /*
2252676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2262676Seschrock  * properties into a separate nvlist.
2272676Seschrock  */
2284217Seschrock static nvlist_t *
2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2302676Seschrock {
2312676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2322676Seschrock 	nvpair_t *elem;
2332676Seschrock 	nvlist_t *propval;
2344217Seschrock 	nvlist_t *nvl;
2354217Seschrock 
2364217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2374217Seschrock 		(void) no_memory(hdl);
2384217Seschrock 		return (NULL);
2394217Seschrock 	}
2402676Seschrock 
2412676Seschrock 	elem = NULL;
2424217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2432676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2442676Seschrock 			continue;
2452676Seschrock 
2462676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2474217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2484217Seschrock 			nvlist_free(nvl);
2494217Seschrock 			(void) no_memory(hdl);
2504217Seschrock 			return (NULL);
2514217Seschrock 		}
2522676Seschrock 	}
2532676Seschrock 
2544217Seschrock 	return (nvl);
2552676Seschrock }
2562676Seschrock 
2572676Seschrock /*
258789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
259789Sahrens  */
260789Sahrens static int
261789Sahrens get_stats(zfs_handle_t *zhp)
262789Sahrens {
263789Sahrens 	zfs_cmd_t zc = { 0 };
2642676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2654217Seschrock 	nvlist_t *allprops, *userprops;
266789Sahrens 
267789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
268789Sahrens 
2692676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
2702082Seschrock 		return (-1);
2711356Seschrock 
2722082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
2731356Seschrock 		if (errno == ENOMEM) {
2742676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2752676Seschrock 				zcmd_free_nvlists(&zc);
2762082Seschrock 				return (-1);
2772676Seschrock 			}
2781356Seschrock 		} else {
2792676Seschrock 			zcmd_free_nvlists(&zc);
2801356Seschrock 			return (-1);
2811356Seschrock 		}
2821356Seschrock 	}
283789Sahrens 
2842885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
285789Sahrens 
2862676Seschrock 	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
2871544Seschrock 
2884217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
2892676Seschrock 		zcmd_free_nvlists(&zc);
2902082Seschrock 		return (-1);
2912082Seschrock 	}
292789Sahrens 
2932676Seschrock 	zcmd_free_nvlists(&zc);
2942676Seschrock 
2954217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
2964217Seschrock 		nvlist_free(allprops);
2972676Seschrock 		return (-1);
2984217Seschrock 	}
2994217Seschrock 
3004217Seschrock 	nvlist_free(zhp->zfs_props);
3014217Seschrock 	nvlist_free(zhp->zfs_user_props);
3024217Seschrock 
3034217Seschrock 	zhp->zfs_props = allprops;
3044217Seschrock 	zhp->zfs_user_props = userprops;
3052082Seschrock 
306789Sahrens 	return (0);
307789Sahrens }
308789Sahrens 
309789Sahrens /*
310789Sahrens  * Refresh the properties currently stored in the handle.
311789Sahrens  */
312789Sahrens void
313789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
314789Sahrens {
315789Sahrens 	(void) get_stats(zhp);
316789Sahrens }
317789Sahrens 
318789Sahrens /*
319789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
320789Sahrens  * zfs_iter_* to create child handles on the fly.
321789Sahrens  */
322789Sahrens zfs_handle_t *
3232082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
324789Sahrens {
3252082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3264543Smarks 	char *logstr;
3272082Seschrock 
3282082Seschrock 	if (zhp == NULL)
3292082Seschrock 		return (NULL);
3302082Seschrock 
3312082Seschrock 	zhp->zfs_hdl = hdl;
332789Sahrens 
3334543Smarks 	/*
3344543Smarks 	 * Preserve history log string.
3354543Smarks 	 * any changes performed here will be
3364543Smarks 	 * logged as an internal event.
3374543Smarks 	 */
3384543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3394543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3401758Sahrens top:
341789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
342789Sahrens 
343789Sahrens 	if (get_stats(zhp) != 0) {
3444543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
345789Sahrens 		free(zhp);
346789Sahrens 		return (NULL);
347789Sahrens 	}
348789Sahrens 
3491758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
3501758Sahrens 		zfs_cmd_t zc = { 0 };
3511758Sahrens 
3521758Sahrens 		/*
3531758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
3541758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
3551758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
3561758Sahrens 		 * can't be mounted.  However, it could also be that we
3571758Sahrens 		 * have crashed in the middle of one of those
3581758Sahrens 		 * operations, in which case we need to get rid of the
3591758Sahrens 		 * inconsistent state.  We do that by either rolling
3601758Sahrens 		 * back to the previous snapshot (which will fail if
3611758Sahrens 		 * there is none), or destroying the filesystem.  Note
3621758Sahrens 		 * that if we are still in the middle of an active
3631758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
3641758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
3651758Sahrens 		 */
3661758Sahrens 
3671758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3681758Sahrens 
3692885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
3702082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
3711758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
3721758Sahrens 		} else {
3731758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
3741758Sahrens 		}
3751758Sahrens 
3761758Sahrens 		/*
3775331Samw 		 * If we can successfully destroy it, pretend that it
3781758Sahrens 		 * never existed.
3791758Sahrens 		 */
3802082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
3814543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
3821758Sahrens 			free(zhp);
3831758Sahrens 			errno = ENOENT;
3841758Sahrens 			return (NULL);
3851758Sahrens 		}
3865367Sahrens 		/* If we can successfully roll it back, reget the stats */
3875367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
3885367Sahrens 			goto top;
3891758Sahrens 	}
3901758Sahrens 
391789Sahrens 	/*
392789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
393789Sahrens 	 * the high-level type.
394789Sahrens 	 */
3952885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
3962885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
3972885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
3982885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
3992885Sahrens 	else
4002885Sahrens 		abort();
4012885Sahrens 
402789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
403789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
404789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
405789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
406789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
407789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
408789Sahrens 	else
4092082Seschrock 		abort();	/* we should never see any other types */
410789Sahrens 
4114543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
412789Sahrens 	return (zhp);
413789Sahrens }
414789Sahrens 
415789Sahrens /*
416789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
417789Sahrens  * argument is a mask of acceptable types.  The function will print an
418789Sahrens  * appropriate error message and return NULL if it can't be opened.
419789Sahrens  */
420789Sahrens zfs_handle_t *
4212082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
422789Sahrens {
423789Sahrens 	zfs_handle_t *zhp;
4242082Seschrock 	char errbuf[1024];
4252082Seschrock 
4262082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4272082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
428789Sahrens 
429789Sahrens 	/*
4302082Seschrock 	 * Validate the name before we even try to open it.
431789Sahrens 	 */
4325326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4332082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4342082Seschrock 		    "invalid dataset name"));
4352082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
436789Sahrens 		return (NULL);
437789Sahrens 	}
438789Sahrens 
439789Sahrens 	/*
440789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
441789Sahrens 	 */
442789Sahrens 	errno = 0;
4432082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
4443237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
445789Sahrens 		return (NULL);
446789Sahrens 	}
447789Sahrens 
448789Sahrens 	if (!(types & zhp->zfs_type)) {
4492082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4502142Seschrock 		zfs_close(zhp);
451789Sahrens 		return (NULL);
452789Sahrens 	}
453789Sahrens 
454789Sahrens 	return (zhp);
455789Sahrens }
456789Sahrens 
457789Sahrens /*
458789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
459789Sahrens  */
460789Sahrens void
461789Sahrens zfs_close(zfs_handle_t *zhp)
462789Sahrens {
463789Sahrens 	if (zhp->zfs_mntopts)
464789Sahrens 		free(zhp->zfs_mntopts);
4652676Seschrock 	nvlist_free(zhp->zfs_props);
4662676Seschrock 	nvlist_free(zhp->zfs_user_props);
467789Sahrens 	free(zhp);
468789Sahrens }
469789Sahrens 
4705713Srm160521 int
4715713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
4725713Srm160521 {
4735713Srm160521 	char *pool_name;
4745713Srm160521 	zpool_handle_t *zpool_handle;
4755713Srm160521 	char *p;
4765713Srm160521 
4775713Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN);
4785713Srm160521 	if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name,
4795713Srm160521 	    MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) {
4805713Srm160521 		free(pool_name);
4815713Srm160521 		return (-1);
4825713Srm160521 	}
4835713Srm160521 
4845713Srm160521 	if (p = strchr(pool_name, '/'))
4855713Srm160521 		*p = '\0';
4865713Srm160521 	zpool_handle = zpool_open(zhp->zfs_hdl, pool_name);
4875713Srm160521 	free(pool_name);
4885713Srm160521 	if (zpool_handle == NULL)
4895713Srm160521 		return (-1);
4905713Srm160521 
4915713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
4925713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
4935713Srm160521 	zpool_close(zpool_handle);
4945713Srm160521 	return (0);
4955713Srm160521 }
4965713Srm160521 
4975713Srm160521 /*
4985713Srm160521  * The choice of reservation property depends on the SPA version.
4995713Srm160521  */
5005713Srm160521 static int
5015713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5025713Srm160521 {
5035713Srm160521 	int spa_version;
5045713Srm160521 
5055713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5065713Srm160521 		return (-1);
5075713Srm160521 
5085713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5095713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5105713Srm160521 	else
5115713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5125713Srm160521 
5135713Srm160521 	return (0);
5145713Srm160521 }
5155713Srm160521 
5163912Slling /*
5172676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
5182676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
5192676Seschrock  * strings.
520789Sahrens  */
5215094Slling static nvlist_t *
5225094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
5235094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
524789Sahrens {
5252676Seschrock 	nvpair_t *elem;
5262676Seschrock 	uint64_t intval;
5272676Seschrock 	char *strval;
5285094Slling 	zfs_prop_t prop;
5292676Seschrock 	nvlist_t *ret;
5305331Samw 	int chosen_normal = -1;
5315331Samw 	int chosen_utf = -1;
5322676Seschrock 
5332676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
5342676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5353413Smmusante 		    "snapshot properties cannot be modified"));
5362676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5375094Slling 		return (NULL);
5385094Slling 	}
5395094Slling 
5405094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5415094Slling 		(void) no_memory(hdl);
5425094Slling 		return (NULL);
543789Sahrens 	}
544789Sahrens 
5452676Seschrock 	elem = NULL;
5462676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5475094Slling 		const char *propname = nvpair_name(elem);
5482676Seschrock 
5492676Seschrock 		/*
5502676Seschrock 		 * Make sure this property is valid and applies to this type.
5512676Seschrock 		 */
5525094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
5535094Slling 			if (!zfs_prop_user(propname)) {
5542676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5555094Slling 				    "invalid property '%s'"), propname);
5565094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5575094Slling 				goto error;
5585094Slling 			}
5595094Slling 
5605094Slling 			/*
5615094Slling 			 * If this is a user property, make sure it's a
5625094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
5635094Slling 			 */
5645094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
5655094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5665094Slling 				    "'%s' must be a string"), propname);
5675094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5685094Slling 				goto error;
5695094Slling 			}
5705094Slling 
5715094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
5725094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5735094Slling 				    "property name '%s' is too long"),
5742676Seschrock 				    propname);
5752676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5762676Seschrock 				goto error;
5772676Seschrock 			}
5782676Seschrock 
5792676Seschrock 			(void) nvpair_value_string(elem, &strval);
5802676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
5812676Seschrock 				(void) no_memory(hdl);
5822676Seschrock 				goto error;
5832676Seschrock 			}
5842676Seschrock 			continue;
585789Sahrens 		}
5862676Seschrock 
5872676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
5882676Seschrock 			zfs_error_aux(hdl,
5892676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
5902676Seschrock 			    "apply to datasets of this type"), propname);
5912676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5922676Seschrock 			goto error;
5932676Seschrock 		}
5942676Seschrock 
5952676Seschrock 		if (zfs_prop_readonly(prop) &&
5965331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
5972676Seschrock 			zfs_error_aux(hdl,
5982676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
5992676Seschrock 			    propname);
6002676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
6012676Seschrock 			goto error;
6022676Seschrock 		}
6032676Seschrock 
6045094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
6055094Slling 		    &strval, &intval, errbuf) != 0)
6062676Seschrock 			goto error;
6072676Seschrock 
6082676Seschrock 		/*
6092676Seschrock 		 * Perform some additional checks for specific properties.
6102676Seschrock 		 */
6112676Seschrock 		switch (prop) {
6124577Sahrens 		case ZFS_PROP_VERSION:
6134577Sahrens 		{
6144577Sahrens 			int version;
6154577Sahrens 
6164577Sahrens 			if (zhp == NULL)
6174577Sahrens 				break;
6184577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6194577Sahrens 			if (intval < version) {
6204577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6214577Sahrens 				    "Can not downgrade; already at version %u"),
6224577Sahrens 				    version);
6234577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6244577Sahrens 				goto error;
6254577Sahrens 			}
6264577Sahrens 			break;
6274577Sahrens 		}
6284577Sahrens 
6292676Seschrock 		case ZFS_PROP_RECORDSIZE:
6302676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
6312676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
6322676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
6332676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
6342082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6352676Seschrock 				    "'%s' must be power of 2 from %u "
6362676Seschrock 				    "to %uk"), propname,
6372676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
6382676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
6392676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6402676Seschrock 				goto error;
641789Sahrens 			}
642789Sahrens 			break;
643789Sahrens 
6443126Sahl 		case ZFS_PROP_SHAREISCSI:
6453126Sahl 			if (strcmp(strval, "off") != 0 &&
6463126Sahl 			    strcmp(strval, "on") != 0 &&
6473126Sahl 			    strcmp(strval, "type=disk") != 0) {
6483126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6493126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6503126Sahl 				    propname);
6513126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6523126Sahl 				goto error;
6533126Sahl 			}
6543126Sahl 
6553126Sahl 			break;
6563126Sahl 
6572676Seschrock 		case ZFS_PROP_MOUNTPOINT:
6584778Srm160521 		{
6594778Srm160521 			namecheck_err_t why;
6604778Srm160521 
6612676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
6622676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
6632676Seschrock 				break;
6642676Seschrock 
6654778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
6664778Srm160521 				switch (why) {
6674778Srm160521 				case NAME_ERR_LEADING_SLASH:
6684778Srm160521 					zfs_error_aux(hdl,
6694778Srm160521 					    dgettext(TEXT_DOMAIN,
6704778Srm160521 					    "'%s' must be an absolute path, "
6714778Srm160521 					    "'none', or 'legacy'"), propname);
6724778Srm160521 					break;
6734778Srm160521 				case NAME_ERR_TOOLONG:
6744778Srm160521 					zfs_error_aux(hdl,
6754778Srm160521 					    dgettext(TEXT_DOMAIN,
6764778Srm160521 					    "component of '%s' is too long"),
6774778Srm160521 					    propname);
6784778Srm160521 					break;
6794778Srm160521 				}
6802676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6812676Seschrock 				goto error;
682789Sahrens 			}
6834778Srm160521 		}
6844778Srm160521 
6853126Sahl 			/*FALLTHRU*/
6863126Sahl 
6875331Samw 		case ZFS_PROP_SHARESMB:
6883126Sahl 		case ZFS_PROP_SHARENFS:
6893126Sahl 			/*
6905331Samw 			 * For the mountpoint and sharenfs or sharesmb
6915331Samw 			 * properties, check if it can be set in a
6925331Samw 			 * global/non-global zone based on
6933126Sahl 			 * the zoned property value:
6943126Sahl 			 *
6953126Sahl 			 *		global zone	    non-global zone
6963126Sahl 			 * --------------------------------------------------
6973126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
6983126Sahl 			 *		sharenfs (no)	    sharenfs (no)
6995331Samw 			 *		sharesmb (no)	    sharesmb (no)
7003126Sahl 			 *
7013126Sahl 			 * zoned=off	mountpoint (yes)	N/A
7023126Sahl 			 *		sharenfs (yes)
7035331Samw 			 *		sharesmb (yes)
7043126Sahl 			 */
7052676Seschrock 			if (zoned) {
7062676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
7072676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7082676Seschrock 					    "'%s' cannot be set on "
7092676Seschrock 					    "dataset in a non-global zone"),
7102676Seschrock 					    propname);
7112676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7122676Seschrock 					    errbuf);
7132676Seschrock 					goto error;
7145331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
7155331Samw 				    prop == ZFS_PROP_SHARESMB) {
7162676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7172676Seschrock 					    "'%s' cannot be set in "
7182676Seschrock 					    "a non-global zone"), propname);
7192676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7202676Seschrock 					    errbuf);
7212676Seschrock 					goto error;
7222676Seschrock 				}
7232676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
7242676Seschrock 				/*
7252676Seschrock 				 * If zoned property is 'off', this must be in
7262676Seschrock 				 * a globle zone. If not, something is wrong.
7272676Seschrock 				 */
7282676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7292676Seschrock 				    "'%s' cannot be set while dataset "
7302676Seschrock 				    "'zoned' property is set"), propname);
7312676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
7322676Seschrock 				goto error;
7332676Seschrock 			}
7343126Sahl 
7354180Sdougm 			/*
7364180Sdougm 			 * At this point, it is legitimate to set the
7374180Sdougm 			 * property. Now we want to make sure that the
7384180Sdougm 			 * property value is valid if it is sharenfs.
7394180Sdougm 			 */
7405331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
7415331Samw 			    prop == ZFS_PROP_SHARESMB) &&
7424217Seschrock 			    strcmp(strval, "on") != 0 &&
7434217Seschrock 			    strcmp(strval, "off") != 0) {
7445331Samw 				zfs_share_proto_t proto;
7455331Samw 
7465331Samw 				if (prop == ZFS_PROP_SHARESMB)
7475331Samw 					proto = PROTO_SMB;
7485331Samw 				else
7495331Samw 					proto = PROTO_NFS;
7504180Sdougm 
7514180Sdougm 				/*
7525331Samw 				 * Must be an valid sharing protocol
7535331Samw 				 * option string so init the libshare
7545331Samw 				 * in order to enable the parser and
7555331Samw 				 * then parse the options. We use the
7565331Samw 				 * control API since we don't care about
7575331Samw 				 * the current configuration and don't
7584180Sdougm 				 * want the overhead of loading it
7594180Sdougm 				 * until we actually do something.
7604180Sdougm 				 */
7614180Sdougm 
7624217Seschrock 				if (zfs_init_libshare(hdl,
7634217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
7644217Seschrock 					/*
7654217Seschrock 					 * An error occurred so we can't do
7664217Seschrock 					 * anything
7674217Seschrock 					 */
7684217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7694217Seschrock 					    "'%s' cannot be set: problem "
7704217Seschrock 					    "in share initialization"),
7714217Seschrock 					    propname);
7724217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7734217Seschrock 					    errbuf);
7744217Seschrock 					goto error;
7754217Seschrock 				}
7764217Seschrock 
7775331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
7784217Seschrock 					/*
7794217Seschrock 					 * There was an error in parsing so
7804217Seschrock 					 * deal with it by issuing an error
7814217Seschrock 					 * message and leaving after
7824217Seschrock 					 * uninitializing the the libshare
7834217Seschrock 					 * interface.
7844217Seschrock 					 */
7854217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7864217Seschrock 					    "'%s' cannot be set to invalid "
7874217Seschrock 					    "options"), propname);
7884217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
7894217Seschrock 					    errbuf);
7904217Seschrock 					zfs_uninit_libshare(hdl);
7914217Seschrock 					goto error;
7924217Seschrock 				}
7934180Sdougm 				zfs_uninit_libshare(hdl);
7944180Sdougm 			}
7954180Sdougm 
7963126Sahl 			break;
7975331Samw 		case ZFS_PROP_UTF8ONLY:
7985331Samw 			chosen_utf = (int)intval;
7995331Samw 			break;
8005331Samw 		case ZFS_PROP_NORMALIZE:
8015331Samw 			chosen_normal = (int)intval;
8025331Samw 			break;
8032676Seschrock 		}
8042676Seschrock 
8052676Seschrock 		/*
8062676Seschrock 		 * For changes to existing volumes, we have some additional
8072676Seschrock 		 * checks to enforce.
8082676Seschrock 		 */
8092676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
8102676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
8112676Seschrock 			    ZFS_PROP_VOLSIZE);
8122676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
8132676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
8142676Seschrock 			char buf[64];
8152676Seschrock 
8162676Seschrock 			switch (prop) {
8172676Seschrock 			case ZFS_PROP_RESERVATION:
8185378Sck153898 			case ZFS_PROP_REFRESERVATION:
8192676Seschrock 				if (intval > volsize) {
8202676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8212676Seschrock 					    "'%s' is greater than current "
8222676Seschrock 					    "volume size"), propname);
8232676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8242676Seschrock 					    errbuf);
8252676Seschrock 					goto error;
8262676Seschrock 				}
8272676Seschrock 				break;
8282676Seschrock 
8292676Seschrock 			case ZFS_PROP_VOLSIZE:
8302676Seschrock 				if (intval % blocksize != 0) {
8312676Seschrock 					zfs_nicenum(blocksize, buf,
8322676Seschrock 					    sizeof (buf));
8332676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8342676Seschrock 					    "'%s' must be a multiple of "
8352676Seschrock 					    "volume block size (%s)"),
8362676Seschrock 					    propname, buf);
8372676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8382676Seschrock 					    errbuf);
8392676Seschrock 					goto error;
8402676Seschrock 				}
8412676Seschrock 
8422676Seschrock 				if (intval == 0) {
8432676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8442676Seschrock 					    "'%s' cannot be zero"),
8452676Seschrock 					    propname);
8462676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8472676Seschrock 					    errbuf);
8482676Seschrock 					goto error;
849789Sahrens 				}
8503126Sahl 				break;
851789Sahrens 			}
852789Sahrens 		}
853789Sahrens 	}
854789Sahrens 
8552676Seschrock 	/*
8565331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
8575331Samw 	 * enforce rejection of non-UTF8 names.
8585331Samw 	 *
8595331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
8605331Samw 	 * was explicitly not chosen, it is an error.
8615331Samw 	 */
8625498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
8635331Samw 		if (nvlist_add_uint64(ret,
8645331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
8655331Samw 			(void) no_memory(hdl);
8665331Samw 			goto error;
8675331Samw 		}
8685498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
8695331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8705331Samw 		    "'%s' must be set 'on' if normalization chosen"),
8715331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
8725331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8735331Samw 		goto error;
8745331Samw 	}
8755331Samw 
8765331Samw 	/*
8772676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
8782676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
8792676Seschrock 	 */
8802676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
8812676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
8822676Seschrock 	    &intval) == 0) {
8832676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
8842676Seschrock 		    ZFS_PROP_VOLSIZE);
8855481Sck153898 		uint64_t old_reservation;
8862676Seschrock 		uint64_t new_reservation;
8875481Sck153898 		zfs_prop_t resv_prop;
8885713Srm160521 
8895713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
8905481Sck153898 			goto error;
8915481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
8922676Seschrock 
8932676Seschrock 		if (old_volsize == old_reservation &&
8945481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
8952676Seschrock 		    &new_reservation) != 0) {
8962676Seschrock 			if (nvlist_add_uint64(ret,
8975481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
8982676Seschrock 				(void) no_memory(hdl);
8992676Seschrock 				goto error;
9002676Seschrock 			}
9012676Seschrock 		}
9022676Seschrock 	}
9032676Seschrock 	return (ret);
9042676Seschrock 
9052676Seschrock error:
9062676Seschrock 	nvlist_free(ret);
9072676Seschrock 	return (NULL);
908789Sahrens }
909789Sahrens 
9104543Smarks static int
9114543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9124543Smarks     uint64_t *ret_who)
9134543Smarks {
9144543Smarks 	struct passwd *pwd;
9154543Smarks 	struct group *grp;
9164543Smarks 	uid_t id;
9174543Smarks 
9184543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9194543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9204543Smarks 		*ret_who = -1;
9214543Smarks 		return (0);
9224543Smarks 	}
9234543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9244543Smarks 		return (EZFS_BADWHO);
9254543Smarks 
9264543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9274543Smarks 	    strcmp(who, "everyone") == 0) {
9284543Smarks 		*ret_who = -1;
9294543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9304543Smarks 		return (0);
9314543Smarks 	}
9324543Smarks 
9334543Smarks 	pwd = getpwnam(who);
9344543Smarks 	grp = getgrnam(who);
9354543Smarks 
9364543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9374543Smarks 		*ret_who = pwd->pw_uid;
9384543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9394543Smarks 		*ret_who = grp->gr_gid;
9404543Smarks 	} else if (pwd) {
9414543Smarks 		*ret_who = pwd->pw_uid;
9424543Smarks 		*who_type = ZFS_DELEG_USER;
9434543Smarks 	} else if (grp) {
9444543Smarks 		*ret_who = grp->gr_gid;
9454543Smarks 		*who_type = ZFS_DELEG_GROUP;
9464543Smarks 	} else {
9474543Smarks 		char *end;
9484543Smarks 
9494543Smarks 		id = strtol(who, &end, 10);
9504543Smarks 		if (errno != 0 || *end != '\0') {
9514543Smarks 			return (EZFS_BADWHO);
9524543Smarks 		} else {
9534543Smarks 			*ret_who = id;
9544543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
9554543Smarks 				*who_type = ZFS_DELEG_USER;
9564543Smarks 		}
9574543Smarks 	}
9584543Smarks 
9594543Smarks 	return (0);
9604543Smarks }
9614543Smarks 
9624543Smarks static void
9634543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
9644543Smarks {
9654543Smarks 	if (perms_nvp != NULL) {
9664543Smarks 		verify(nvlist_add_nvlist(who_nvp,
9674543Smarks 		    name, perms_nvp) == 0);
9684543Smarks 	} else {
9694543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
9704543Smarks 	}
9714543Smarks }
9724543Smarks 
9734543Smarks static void
9744543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
9754543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
9764543Smarks     nvlist_t *sets_nvp)
9774543Smarks {
9784543Smarks 	boolean_t do_perms, do_sets;
9794543Smarks 	char name[ZFS_MAX_DELEG_NAME];
9804543Smarks 
9814543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
9824543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
9834543Smarks 
9844543Smarks 	if (!do_perms && !do_sets)
9854543Smarks 		do_perms = do_sets = B_TRUE;
9864543Smarks 
9874543Smarks 	if (do_perms) {
9884543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
9894543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9904543Smarks 		    whostr : (void *)&whoid);
9914543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
9924543Smarks 	}
9934543Smarks 	if (do_sets) {
9944543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
9954543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
9964543Smarks 		    whostr : (void *)&whoid);
9974543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
9984543Smarks 	}
9994543Smarks }
10004543Smarks 
10014543Smarks static void
10024543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
10034543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
10044543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10054543Smarks {
10064543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10074543Smarks 		helper(who_type, whoid, whostr, 0,
10084543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10094543Smarks 	} else {
10104543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10114543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10124543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10134543Smarks 		}
10144543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10154543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10164543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10174543Smarks 		}
10184543Smarks 	}
10194543Smarks }
10204543Smarks 
10214543Smarks /*
10224543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10234543Smarks  *
10244543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10254543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10264543Smarks  * base attribute named stored in the dsl.
10274543Smarks  * Arguments:
10284543Smarks  *
10294543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10304543Smarks  *           whostr may be null for everyone or create perms.
10314543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10324543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10335331Samw  * perms:    common separated list of permissions.  May be null if user
10344543Smarks  *           is requested to remove permissions by who.
10354543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10364543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10374543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10384543Smarks  *           The output nvp will look something like this.
10394543Smarks  *              ul$1234 -> {create ; destroy }
10404543Smarks  *              Ul$1234 -> { @myset }
10414543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10424543Smarks  */
10434543Smarks int
10444543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10454543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10464543Smarks {
10474543Smarks 	nvlist_t *who_nvp;
10484543Smarks 	nvlist_t *perms_nvp = NULL;
10494543Smarks 	nvlist_t *sets_nvp = NULL;
10504543Smarks 	char errbuf[1024];
10514787Sahrens 	char *who_tok, *perm;
10524543Smarks 	int error;
10534543Smarks 
10544543Smarks 	*nvp = NULL;
10554543Smarks 
10564543Smarks 	if (perms) {
10574543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
10584543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10594543Smarks 			return (1);
10604543Smarks 		}
10614543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
10624543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
10634543Smarks 			nvlist_free(perms_nvp);
10644543Smarks 			return (1);
10654543Smarks 		}
10664543Smarks 	}
10674543Smarks 
10684543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
10694543Smarks 		if (perms_nvp)
10704543Smarks 			nvlist_free(perms_nvp);
10714543Smarks 		if (sets_nvp)
10724543Smarks 			nvlist_free(sets_nvp);
10734543Smarks 		return (1);
10744543Smarks 	}
10754543Smarks 
10764543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
10774543Smarks 		namecheck_err_t why;
10784543Smarks 		char what;
10794543Smarks 
10804543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
10814787Sahrens 			nvlist_free(who_nvp);
10824787Sahrens 			if (perms_nvp)
10834787Sahrens 				nvlist_free(perms_nvp);
10844787Sahrens 			if (sets_nvp)
10854787Sahrens 				nvlist_free(sets_nvp);
10864787Sahrens 
10874543Smarks 			switch (why) {
10884543Smarks 			case NAME_ERR_NO_AT:
10894543Smarks 				zfs_error_aux(zhp->zfs_hdl,
10904543Smarks 				    dgettext(TEXT_DOMAIN,
10914543Smarks 				    "set definition must begin with an '@' "
10924543Smarks 				    "character"));
10934543Smarks 			}
10944543Smarks 			return (zfs_error(zhp->zfs_hdl,
10954543Smarks 			    EZFS_BADPERMSET, whostr));
10964543Smarks 		}
10974543Smarks 	}
10984543Smarks 
10994543Smarks 	/*
11004543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
11014543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
11024543Smarks 	 * other sets_nvp will have only permssion set names in it.
11034543Smarks 	 */
11044787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11054787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11064787Sahrens 
11074787Sahrens 		if (perm_canonical) {
11084787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11094787Sahrens 			    perm_canonical) == 0);
11104787Sahrens 		} else if (perm[0] == '@') {
11114787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11124787Sahrens 		} else {
11134787Sahrens 			nvlist_free(who_nvp);
11144787Sahrens 			nvlist_free(perms_nvp);
11154787Sahrens 			nvlist_free(sets_nvp);
11164787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11174543Smarks 		}
11184543Smarks 	}
11194543Smarks 
11204543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11214543Smarks 		who_tok = strtok(whostr, ",");
11224543Smarks 		if (who_tok == NULL) {
11234543Smarks 			nvlist_free(who_nvp);
11244787Sahrens 			if (perms_nvp)
11254787Sahrens 				nvlist_free(perms_nvp);
11264543Smarks 			if (sets_nvp)
11274543Smarks 				nvlist_free(sets_nvp);
11284543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11294543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11304543Smarks 			    whostr);
11314543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11324543Smarks 		}
11334543Smarks 	}
11344543Smarks 
11354543Smarks 	/*
11364543Smarks 	 * Now create the nvlist(s)
11374543Smarks 	 */
11384543Smarks 	do {
11394543Smarks 		uint64_t who_id;
11404543Smarks 
11414543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11424543Smarks 		    &who_id);
11434543Smarks 		if (error) {
11444543Smarks 			nvlist_free(who_nvp);
11454787Sahrens 			if (perms_nvp)
11464787Sahrens 				nvlist_free(perms_nvp);
11474543Smarks 			if (sets_nvp)
11484543Smarks 				nvlist_free(sets_nvp);
11494543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11504543Smarks 			    dgettext(TEXT_DOMAIN,
11514543Smarks 			    "Unable to determine uid/gid for "
11524543Smarks 			    "%s "), who_tok);
11534543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11544543Smarks 		}
11554543Smarks 
11564543Smarks 		/*
11574543Smarks 		 * add entries for both local and descendent when required
11584543Smarks 		 */
11594543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
11604543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
11614543Smarks 
11624543Smarks 	} while (who_tok = strtok(NULL, ","));
11634543Smarks 	*nvp = who_nvp;
11644543Smarks 	return (0);
11654543Smarks }
11664543Smarks 
11674543Smarks static int
11684543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
11694543Smarks {
11704543Smarks 	zfs_cmd_t zc = { 0 };
11714543Smarks 	int error;
11724543Smarks 	char errbuf[1024];
11734543Smarks 
11744543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
11754543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
11764543Smarks 	    zhp->zfs_name);
11774543Smarks 
11785094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
11794543Smarks 		return (-1);
11804543Smarks 
11814543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
11824543Smarks 	zc.zc_perm_action = unset;
11834543Smarks 
11844543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
11854543Smarks 	if (error && errno == ENOTSUP) {
11864543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
11874543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
11884543Smarks 		zcmd_free_nvlists(&zc);
11894543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
11904543Smarks 	} else if (error) {
11914543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
11924543Smarks 	}
11934543Smarks 	zcmd_free_nvlists(&zc);
11944543Smarks 
11954543Smarks 	return (error);
11964543Smarks }
11974543Smarks 
11984543Smarks int
11994543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
12004543Smarks {
12014543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12024543Smarks }
12034543Smarks 
12044543Smarks int
12054543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12064543Smarks {
12074543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12084543Smarks }
12094543Smarks 
12104543Smarks static int
12114543Smarks perm_compare(const void *arg1, const void *arg2)
12124543Smarks {
12134543Smarks 	const zfs_perm_node_t *node1 = arg1;
12144543Smarks 	const zfs_perm_node_t *node2 = arg2;
12154543Smarks 	int ret;
12164543Smarks 
12174543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12184543Smarks 
12194543Smarks 	if (ret > 0)
12204543Smarks 		return (1);
12214543Smarks 	if (ret < 0)
12224543Smarks 		return (-1);
12234543Smarks 	else
12244543Smarks 		return (0);
12254543Smarks }
12264543Smarks 
12274543Smarks static void
12284543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12294543Smarks {
12304543Smarks 	zfs_perm_node_t *permnode;
12315367Sahrens 	void *cookie = NULL;
12325367Sahrens 
12335367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12344543Smarks 		free(permnode);
12355367Sahrens 	avl_destroy(tree);
12364543Smarks }
12374543Smarks 
12384543Smarks static void
12394543Smarks zfs_destroy_tree(avl_tree_t *tree)
12404543Smarks {
12414543Smarks 	zfs_allow_node_t *allownode;
12425367Sahrens 	void *cookie = NULL;
12435367Sahrens 
12444543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12454543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12464543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12474543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12484543Smarks 		free(allownode);
12494543Smarks 	}
12505367Sahrens 	avl_destroy(tree);
12514543Smarks }
12524543Smarks 
12534543Smarks void
12544543Smarks zfs_free_allows(zfs_allow_t *allow)
12554543Smarks {
12564543Smarks 	zfs_allow_t *allownext;
12574543Smarks 	zfs_allow_t *freeallow;
12584543Smarks 
12594543Smarks 	allownext = allow;
12604543Smarks 	while (allownext) {
12614543Smarks 		zfs_destroy_tree(&allownext->z_sets);
12624543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
12634543Smarks 		zfs_destroy_tree(&allownext->z_user);
12644543Smarks 		zfs_destroy_tree(&allownext->z_group);
12654543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
12664543Smarks 		freeallow = allownext;
12674543Smarks 		allownext = allownext->z_next;
12684543Smarks 		free(freeallow);
12694543Smarks 	}
12704543Smarks }
12714543Smarks 
12724543Smarks static zfs_allow_t *
12734543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
12744543Smarks {
12754543Smarks 	zfs_allow_t *ptree;
12764543Smarks 
12774543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
12784543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
12794543Smarks 		return (NULL);
12804543Smarks 	}
12814543Smarks 
12824543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
12834543Smarks 	avl_create(&ptree->z_sets,
12844543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12854543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12864543Smarks 	avl_create(&ptree->z_crperms,
12874543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12884543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12894543Smarks 	avl_create(&ptree->z_user,
12904543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12914543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12924543Smarks 	avl_create(&ptree->z_group,
12934543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12944543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12954543Smarks 	avl_create(&ptree->z_everyone,
12964543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
12974543Smarks 	    offsetof(zfs_allow_node_t, z_node));
12984543Smarks 
12994543Smarks 	if (prev)
13004543Smarks 		prev->z_next = ptree;
13014543Smarks 	ptree->z_next = NULL;
13024543Smarks 	return (ptree);
13034543Smarks }
13044543Smarks 
13054543Smarks /*
13064543Smarks  * Add permissions to the appropriate AVL permission tree.
13074543Smarks  * The appropriate tree may not be the requested tree.
13084543Smarks  * For example if ld indicates a local permission, but
13094543Smarks  * same permission also exists as a descendent permission
13104543Smarks  * then the permission will be removed from the descendent
13114543Smarks  * tree and add the the local+descendent tree.
13124543Smarks  */
13134543Smarks static int
13144543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13154543Smarks     char *perm, char ld)
13164543Smarks {
13174543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13184543Smarks 	zfs_perm_node_t *newnode;
13194543Smarks 	avl_index_t where, where2;
13204543Smarks 	avl_tree_t *tree, *altree;
13214543Smarks 
13224543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13234543Smarks 
13244543Smarks 	if (ld == ZFS_DELEG_NA) {
13254543Smarks 		tree =  &allownode->z_localdescend;
13264543Smarks 		altree = &allownode->z_descend;
13274543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13284543Smarks 		tree = &allownode->z_local;
13294543Smarks 		altree = &allownode->z_descend;
13304543Smarks 	} else {
13314543Smarks 		tree = &allownode->z_descend;
13324543Smarks 		altree = &allownode->z_local;
13334543Smarks 	}
13344543Smarks 	permnode = avl_find(tree, &pnode, &where);
13354543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13364543Smarks 
13374543Smarks 	if (permnode2) {
13384543Smarks 		avl_remove(altree, permnode2);
13394543Smarks 		free(permnode2);
13404543Smarks 		if (permnode == NULL) {
13414543Smarks 			tree =  &allownode->z_localdescend;
13424543Smarks 		}
13434543Smarks 	}
13444543Smarks 
13454543Smarks 	/*
13464543Smarks 	 * Now insert new permission in either requested location
13474543Smarks 	 * local/descendent or into ld when perm will exist in both.
13484543Smarks 	 */
13494543Smarks 	if (permnode == NULL) {
13504543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13514543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
13524543Smarks 			return (-1);
13534543Smarks 		}
13544543Smarks 		*newnode = pnode;
13554543Smarks 		avl_add(tree, newnode);
13564543Smarks 	}
13574543Smarks 	return (0);
13584543Smarks }
13594577Sahrens 
13604543Smarks /*
13614543Smarks  * Uggh, this is going to be a bit complicated.
13624543Smarks  * we have an nvlist coming out of the kernel that
13634543Smarks  * will indicate where the permission is set and then
13644543Smarks  * it will contain allow of the various "who's", and what
13654543Smarks  * their permissions are.  To further complicate this
13664543Smarks  * we will then have to coalesce the local,descendent
13674543Smarks  * and local+descendent permissions where appropriate.
13684543Smarks  * The kernel only knows about a permission as being local
13694543Smarks  * or descendent, but not both.
13704543Smarks  *
13714543Smarks  * In order to make this easier for zfs_main to deal with
13724543Smarks  * a series of AVL trees will be used to maintain
13734543Smarks  * all of this, primarily for sorting purposes as well
13744543Smarks  * as the ability to quickly locate a specific entry.
13754543Smarks  *
13764543Smarks  * What we end up with are tree's for sets, create perms,
13774543Smarks  * user, groups and everyone.  With each of those trees
13784543Smarks  * we have subtrees for local, descendent and local+descendent
13794543Smarks  * permissions.
13804543Smarks  */
13814543Smarks int
13824543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
13834543Smarks {
13844543Smarks 	zfs_cmd_t zc = { 0 };
13854543Smarks 	int error;
13864543Smarks 	nvlist_t *nvlist;
13874543Smarks 	nvlist_t *permnv, *sourcenv;
13884543Smarks 	nvpair_t *who_pair, *source_pair;
13894543Smarks 	nvpair_t *perm_pair;
13904543Smarks 	char errbuf[1024];
13914543Smarks 	zfs_allow_t *zallowp, *newallowp;
13924543Smarks 	char  ld;
13934543Smarks 	char *nvpname;
13944543Smarks 	uid_t	uid;
13954543Smarks 	gid_t	gid;
13964543Smarks 	avl_tree_t *tree;
13974543Smarks 	avl_index_t where;
13984543Smarks 
13994543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14004543Smarks 
14014543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
14024543Smarks 		return (-1);
14034543Smarks 
14044543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14054543Smarks 		if (errno == ENOMEM) {
14064543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14074543Smarks 				zcmd_free_nvlists(&zc);
14084543Smarks 				return (-1);
14094543Smarks 			}
14104543Smarks 		} else if (errno == ENOTSUP) {
14114543Smarks 			zcmd_free_nvlists(&zc);
14124543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14134543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14144543Smarks 			return (zfs_error(zhp->zfs_hdl,
14154543Smarks 			    EZFS_BADVERSION, errbuf));
14164543Smarks 		} else {
14174543Smarks 			zcmd_free_nvlists(&zc);
14184543Smarks 			return (-1);
14194543Smarks 		}
14204543Smarks 	}
14214543Smarks 
14224543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14234543Smarks 		zcmd_free_nvlists(&zc);
14244543Smarks 		return (-1);
14254543Smarks 	}
14264543Smarks 
14274543Smarks 	zcmd_free_nvlists(&zc);
14284543Smarks 
14294543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14304543Smarks 
14314543Smarks 	if (source_pair == NULL) {
14324543Smarks 		*zfs_perms = NULL;
14334543Smarks 		return (0);
14344543Smarks 	}
14354543Smarks 
14364543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14374543Smarks 	if (*zfs_perms == NULL) {
14384543Smarks 		return (0);
14394543Smarks 	}
14404543Smarks 
14414543Smarks 	zallowp = *zfs_perms;
14424543Smarks 
14434543Smarks 	for (;;) {
14444543Smarks 		struct passwd *pwd;
14454543Smarks 		struct group *grp;
14464543Smarks 		zfs_allow_node_t *allownode;
14474543Smarks 		zfs_allow_node_t  findallownode;
14484543Smarks 		zfs_allow_node_t *newallownode;
14494543Smarks 
14504543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14514543Smarks 		    nvpair_name(source_pair),
14524543Smarks 		    sizeof (zallowp->z_setpoint));
14534543Smarks 
14544543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
14554543Smarks 			goto abort;
14564543Smarks 
14574543Smarks 		/*
14584543Smarks 		 * Make sure nvlist is composed correctly
14594543Smarks 		 */
14604543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
14614543Smarks 			goto abort;
14624543Smarks 		}
14634543Smarks 
14644543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
14654543Smarks 		if (who_pair == NULL) {
14664543Smarks 			goto abort;
14674543Smarks 		}
14684543Smarks 
14694543Smarks 		do {
14704543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
14714543Smarks 			if (error) {
14724543Smarks 				goto abort;
14734543Smarks 			}
14744543Smarks 
14754543Smarks 			/*
14764543Smarks 			 * First build up the key to use
14774543Smarks 			 * for looking up in the various
14784543Smarks 			 * who trees.
14794543Smarks 			 */
14804543Smarks 			ld = nvpair_name(who_pair)[1];
14814543Smarks 			nvpname = nvpair_name(who_pair);
14824543Smarks 			switch (nvpair_name(who_pair)[0]) {
14834543Smarks 			case ZFS_DELEG_USER:
14844543Smarks 			case ZFS_DELEG_USER_SETS:
14854543Smarks 				tree = &zallowp->z_user;
14864543Smarks 				uid = atol(&nvpname[3]);
14874543Smarks 				pwd = getpwuid(uid);
14884543Smarks 				(void) snprintf(findallownode.z_key,
14894543Smarks 				    sizeof (findallownode.z_key), "user %s",
14904543Smarks 				    (pwd) ? pwd->pw_name :
14914543Smarks 				    &nvpair_name(who_pair)[3]);
14924543Smarks 				break;
14934543Smarks 			case ZFS_DELEG_GROUP:
14944543Smarks 			case ZFS_DELEG_GROUP_SETS:
14954543Smarks 				tree = &zallowp->z_group;
14964543Smarks 				gid = atol(&nvpname[3]);
14974543Smarks 				grp = getgrgid(gid);
14984543Smarks 				(void) snprintf(findallownode.z_key,
14994543Smarks 				    sizeof (findallownode.z_key), "group %s",
15004543Smarks 				    (grp) ? grp->gr_name :
15014543Smarks 				    &nvpair_name(who_pair)[3]);
15024543Smarks 				break;
15034543Smarks 			case ZFS_DELEG_CREATE:
15044543Smarks 			case ZFS_DELEG_CREATE_SETS:
15054543Smarks 				tree = &zallowp->z_crperms;
15064543Smarks 				(void) strlcpy(findallownode.z_key, "",
15074543Smarks 				    sizeof (findallownode.z_key));
15084543Smarks 				break;
15094543Smarks 			case ZFS_DELEG_EVERYONE:
15104543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15114543Smarks 				(void) snprintf(findallownode.z_key,
15124543Smarks 				    sizeof (findallownode.z_key), "everyone");
15134543Smarks 				tree = &zallowp->z_everyone;
15144543Smarks 				break;
15154543Smarks 			case ZFS_DELEG_NAMED_SET:
15164543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15174543Smarks 				(void) snprintf(findallownode.z_key,
15184543Smarks 				    sizeof (findallownode.z_key), "%s",
15194543Smarks 				    &nvpair_name(who_pair)[3]);
15204543Smarks 				tree = &zallowp->z_sets;
15214543Smarks 				break;
15224543Smarks 			}
15234543Smarks 
15244543Smarks 			/*
15254543Smarks 			 * Place who in tree
15264543Smarks 			 */
15274543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15284543Smarks 			if (allownode == NULL) {
15294543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15304543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15314543Smarks 					goto abort;
15324543Smarks 				}
15334543Smarks 				avl_create(&newallownode->z_localdescend,
15344543Smarks 				    perm_compare,
15354543Smarks 				    sizeof (zfs_perm_node_t),
15364543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15374543Smarks 				avl_create(&newallownode->z_local,
15384543Smarks 				    perm_compare,
15394543Smarks 				    sizeof (zfs_perm_node_t),
15404543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15414543Smarks 				avl_create(&newallownode->z_descend,
15424543Smarks 				    perm_compare,
15434543Smarks 				    sizeof (zfs_perm_node_t),
15444543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15454543Smarks 				(void) strlcpy(newallownode->z_key,
15464543Smarks 				    findallownode.z_key,
15474543Smarks 				    sizeof (findallownode.z_key));
15484543Smarks 				avl_insert(tree, newallownode, where);
15494543Smarks 				allownode = newallownode;
15504543Smarks 			}
15514543Smarks 
15524543Smarks 			/*
15534543Smarks 			 * Now iterate over the permissions and
15544543Smarks 			 * place them in the appropriate local,
15554543Smarks 			 * descendent or local+descendent tree.
15564543Smarks 			 *
15574543Smarks 			 * The permissions are added to the tree
15584543Smarks 			 * via zfs_coalesce_perm().
15594543Smarks 			 */
15604543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
15614543Smarks 			if (perm_pair == NULL)
15624543Smarks 				goto abort;
15634543Smarks 			do {
15644543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
15654543Smarks 				    nvpair_name(perm_pair), ld) != 0)
15664543Smarks 					goto abort;
15674543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
15684543Smarks 			    perm_pair));
15694543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
15704543Smarks 
15714543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
15724543Smarks 		if (source_pair == NULL)
15734543Smarks 			break;
15744543Smarks 
15754543Smarks 		/*
15764543Smarks 		 * allocate another node from the link list of
15774543Smarks 		 * zfs_allow_t structures
15784543Smarks 		 */
15794543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
15804543Smarks 		    nvpair_name(source_pair));
15814543Smarks 		if (newallowp == NULL) {
15824543Smarks 			goto abort;
15834543Smarks 		}
15844543Smarks 		zallowp = newallowp;
15854543Smarks 	}
15864543Smarks 	nvlist_free(nvlist);
15874543Smarks 	return (0);
15884543Smarks abort:
15894543Smarks 	zfs_free_allows(*zfs_perms);
15904543Smarks 	nvlist_free(nvlist);
15914543Smarks 	return (-1);
15924543Smarks }
15934543Smarks 
15945993Smarks static char *
15955993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
15965993Smarks {
15975993Smarks 	/*
15985993Smarks 	 * Don't put newlines on end of lines
15995993Smarks 	 */
16005993Smarks 	switch (note) {
16015993Smarks 	case ZFS_DELEG_NOTE_CREATE:
16025993Smarks 		return (dgettext(TEXT_DOMAIN,
16035993Smarks 		    "Must also have the 'mount' ability"));
16045993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16055993Smarks 		return (dgettext(TEXT_DOMAIN,
16065993Smarks 		    "Must also have the 'mount' ability"));
16075993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16085993Smarks 		return (dgettext(TEXT_DOMAIN,
16095993Smarks 		    "Must also have the 'mount' ability"));
16105993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16115993Smarks 		return (dgettext(TEXT_DOMAIN,
16125993Smarks 		    "Must also have the 'mount' ability"));
16135993Smarks 	case ZFS_DELEG_NOTE_CLONE:
16145993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16155993Smarks 		    "ability and 'mount'\n"
16165993Smarks 		    "\t\t\t\tability in the origin file system"));
16175993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16185993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16195993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16205993Smarks 	case ZFS_DELEG_NOTE_RENAME:
16215993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16225993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16235993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16245993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16255993Smarks 		    " and 'create' ability"));
16265993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16275993Smarks 		return (dgettext(TEXT_DOMAIN,
16285993Smarks 		    "Allows changing any user property"));
16295993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16305993Smarks 		return (dgettext(TEXT_DOMAIN,
16315993Smarks 		    "Must also have the permission that is being\n"
16325993Smarks 		    "\t\t\t\tallowed"));
16335993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16345993Smarks 		return (dgettext(TEXT_DOMAIN,
16355993Smarks 		    "Allows mount/umount of ZFS datasets"));
16365993Smarks 	case ZFS_DELEG_NOTE_SHARE:
16375993Smarks 		return (dgettext(TEXT_DOMAIN,
16385993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16395993Smarks 		    "\t\t\t\tprotocols"));
16405993Smarks 	case ZFS_DELEG_NOTE_NONE:
16415993Smarks 	default:
16425993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16435993Smarks 	}
16445993Smarks }
16455993Smarks 
16465993Smarks typedef enum {
16475993Smarks 	ZFS_DELEG_SUBCOMMAND,
16485993Smarks 	ZFS_DELEG_PROP,
16495993Smarks 	ZFS_DELEG_OTHER
16505993Smarks } zfs_deleg_perm_type_t;
16515993Smarks 
16525993Smarks /*
16535993Smarks  * is the permission a subcommand or other?
16545993Smarks  */
16555993Smarks zfs_deleg_perm_type_t
16565993Smarks zfs_deleg_perm_type(const char *perm)
16575993Smarks {
16585993Smarks 	if (strcmp(perm, "userprop") == 0)
16595993Smarks 		return (ZFS_DELEG_OTHER);
16605993Smarks 	else
16615993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
16625993Smarks }
16635993Smarks 
16645993Smarks static char *
16655993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
16665993Smarks {
16675993Smarks 	switch (type) {
16685993Smarks 	case ZFS_DELEG_SUBCOMMAND:
16695993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
16705993Smarks 	case ZFS_DELEG_PROP:
16715993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
16725993Smarks 	case ZFS_DELEG_OTHER:
16735993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
16745993Smarks 	}
16755993Smarks 	return ("");
16765993Smarks }
16775993Smarks 
16785993Smarks /*ARGSUSED*/
16795993Smarks static int
16805993Smarks zfs_deleg_prop_cb(int prop, void *cb)
16815993Smarks {
16825993Smarks 	if (zfs_prop_delegatable(prop))
16835993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
16845993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
16855993Smarks 
16865993Smarks 	return (ZPROP_CONT);
16875993Smarks }
16885993Smarks 
16895993Smarks void
16905993Smarks zfs_deleg_permissions(void)
16915993Smarks {
16925993Smarks 	int i;
16935993Smarks 
16945993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
16955993Smarks 	    "TYPE", "NOTES");
16965993Smarks 
16975993Smarks 	/*
16985993Smarks 	 * First print out the subcommands
16995993Smarks 	 */
17005993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
17015993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17025993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17035993Smarks 		    zfs_deleg_perm_type_str(
17045993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17055993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17065993Smarks 	}
17075993Smarks 
17085993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17095993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17105993Smarks }
17115993Smarks 
1712789Sahrens /*
1713789Sahrens  * Given a property name and value, set the property for the given dataset.
1714789Sahrens  */
1715789Sahrens int
17162676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1717789Sahrens {
1718789Sahrens 	zfs_cmd_t zc = { 0 };
17192676Seschrock 	int ret = -1;
17202676Seschrock 	prop_changelist_t *cl = NULL;
17212082Seschrock 	char errbuf[1024];
17222082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17232676Seschrock 	nvlist_t *nvl = NULL, *realprops;
17242676Seschrock 	zfs_prop_t prop;
17256168Shs24103 	int do_prefix = 1;
17262082Seschrock 
17272082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
17282676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
17292082Seschrock 	    zhp->zfs_name);
17302082Seschrock 
17312676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
17322676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
17332676Seschrock 		(void) no_memory(hdl);
17342676Seschrock 		goto error;
1735789Sahrens 	}
1736789Sahrens 
17375094Slling 	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl,
17382676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
17392676Seschrock 		goto error;
17405094Slling 
17412676Seschrock 	nvlist_free(nvl);
17422676Seschrock 	nvl = realprops;
17432676Seschrock 
17442676Seschrock 	prop = zfs_name_to_prop(propname);
17452676Seschrock 
1746789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
17472676Seschrock 		goto error;
1748789Sahrens 
1749789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17512082Seschrock 		    "child dataset with inherited mountpoint is used "
17522082Seschrock 		    "in a non-global zone"));
17532082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1754789Sahrens 		goto error;
1755789Sahrens 	}
1756789Sahrens 
17576168Shs24103 
17586168Shs24103 	/* do not unmount dataset if canmount is being set to noauto */
17596168Shs24103 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
17606168Shs24103 		do_prefix = 0;
17616168Shs24103 
17626168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
17636168Shs24103 			goto error;
1764789Sahrens 
1765789Sahrens 	/*
1766789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1767789Sahrens 	 */
1768789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1769789Sahrens 
17705094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
17712676Seschrock 		goto error;
17722676Seschrock 
17734543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1774789Sahrens 	if (ret != 0) {
1775789Sahrens 		switch (errno) {
1776789Sahrens 
1777789Sahrens 		case ENOSPC:
1778789Sahrens 			/*
1779789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1780789Sahrens 			 * something different; setting a quota or reservation
1781789Sahrens 			 * doesn't use any disk space.
1782789Sahrens 			 */
1783789Sahrens 			switch (prop) {
1784789Sahrens 			case ZFS_PROP_QUOTA:
17855378Sck153898 			case ZFS_PROP_REFQUOTA:
17862082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17872082Seschrock 				    "size is less than current used or "
17882082Seschrock 				    "reserved space"));
17892082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1790789Sahrens 				break;
1791789Sahrens 
1792789Sahrens 			case ZFS_PROP_RESERVATION:
17935378Sck153898 			case ZFS_PROP_REFRESERVATION:
17942082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17952082Seschrock 				    "size is greater than available space"));
17962082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1797789Sahrens 				break;
1798789Sahrens 
1799789Sahrens 			default:
18002082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1801789Sahrens 				break;
1802789Sahrens 			}
1803789Sahrens 			break;
1804789Sahrens 
1805789Sahrens 		case EBUSY:
18062082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
18072082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
18082082Seschrock 			else
18092676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1810789Sahrens 			break;
1811789Sahrens 
18121175Slling 		case EROFS:
18132082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18141175Slling 			break;
18151175Slling 
18163886Sahl 		case ENOTSUP:
18173886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18185977Smarks 			    "pool and or dataset must be upgraded to set this "
18194603Sahrens 			    "property or value"));
18203886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
18213886Sahl 			break;
18223886Sahl 
1823789Sahrens 		case EOVERFLOW:
1824789Sahrens 			/*
1825789Sahrens 			 * This platform can't address a volume this big.
1826789Sahrens 			 */
1827789Sahrens #ifdef _ILP32
1828789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
18292082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1830789Sahrens 				break;
1831789Sahrens 			}
1832789Sahrens #endif
18332082Seschrock 			/* FALLTHROUGH */
1834789Sahrens 		default:
18352082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1836789Sahrens 		}
1837789Sahrens 	} else {
18386168Shs24103 		if (do_prefix)
18396168Shs24103 			ret = changelist_postfix(cl);
18406168Shs24103 
1841789Sahrens 		/*
1842789Sahrens 		 * Refresh the statistics so the new property value
1843789Sahrens 		 * is reflected.
1844789Sahrens 		 */
18456168Shs24103 		if (ret == 0)
18462676Seschrock 			(void) get_stats(zhp);
1847789Sahrens 	}
1848789Sahrens 
1849789Sahrens error:
18502676Seschrock 	nvlist_free(nvl);
18512676Seschrock 	zcmd_free_nvlists(&zc);
18522676Seschrock 	if (cl)
18532676Seschrock 		changelist_free(cl);
1854789Sahrens 	return (ret);
1855789Sahrens }
1856789Sahrens 
1857789Sahrens /*
1858789Sahrens  * Given a property, inherit the value from the parent dataset.
1859789Sahrens  */
1860789Sahrens int
18612676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1862789Sahrens {
1863789Sahrens 	zfs_cmd_t zc = { 0 };
1864789Sahrens 	int ret;
1865789Sahrens 	prop_changelist_t *cl;
18662082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
18672082Seschrock 	char errbuf[1024];
18682676Seschrock 	zfs_prop_t prop;
18692082Seschrock 
18702082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
18712082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1872789Sahrens 
18735094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
18742676Seschrock 		/*
18752676Seschrock 		 * For user properties, the amount of work we have to do is very
18762676Seschrock 		 * small, so just do it here.
18772676Seschrock 		 */
18782676Seschrock 		if (!zfs_prop_user(propname)) {
18792676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18802676Seschrock 			    "invalid property"));
18812676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
18822676Seschrock 		}
18832676Seschrock 
18842676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
18852676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
18862676Seschrock 
18874849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
18882676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
18892676Seschrock 
18902676Seschrock 		return (0);
18912676Seschrock 	}
18922676Seschrock 
1893789Sahrens 	/*
1894789Sahrens 	 * Verify that this property is inheritable.
1895789Sahrens 	 */
18962082Seschrock 	if (zfs_prop_readonly(prop))
18972082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
18982082Seschrock 
18992082Seschrock 	if (!zfs_prop_inheritable(prop))
19002082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1901789Sahrens 
1902789Sahrens 	/*
1903789Sahrens 	 * Check to see if the value applies to this type
1904789Sahrens 	 */
19052082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
19062082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1907789Sahrens 
19083443Srm160521 	/*
19093443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
19103443Srm160521 	 */
19113443Srm160521 	propname = zfs_prop_to_name(prop);
1912789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19132676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1914789Sahrens 
1915789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1916789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
19172082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19182082Seschrock 		    "dataset is used in a non-global zone"));
19192082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1920789Sahrens 	}
1921789Sahrens 
1922789Sahrens 	/*
1923789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1924789Sahrens 	 */
1925789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1926789Sahrens 		return (-1);
1927789Sahrens 
1928789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19292082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19302082Seschrock 		    "child dataset with inherited mountpoint is used "
19312082Seschrock 		    "in a non-global zone"));
19322082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1933789Sahrens 		goto error;
1934789Sahrens 	}
1935789Sahrens 
1936789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1937789Sahrens 		goto error;
1938789Sahrens 
19394849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
19402082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1941789Sahrens 	} else {
1942789Sahrens 
19432169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1944789Sahrens 			goto error;
1945789Sahrens 
1946789Sahrens 		/*
1947789Sahrens 		 * Refresh the statistics so the new property is reflected.
1948789Sahrens 		 */
1949789Sahrens 		(void) get_stats(zhp);
1950789Sahrens 	}
1951789Sahrens 
1952789Sahrens error:
1953789Sahrens 	changelist_free(cl);
1954789Sahrens 	return (ret);
1955789Sahrens }
1956789Sahrens 
1957789Sahrens /*
19581356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
19591356Seschrock  * extract them appropriately.
19601356Seschrock  */
19611356Seschrock static uint64_t
19621356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19631356Seschrock {
19641356Seschrock 	nvlist_t *nv;
19651356Seschrock 	uint64_t value;
19661356Seschrock 
19672885Sahrens 	*source = NULL;
19681356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19691356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
19705094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
19715094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19721356Seschrock 	} else {
19731356Seschrock 		value = zfs_prop_default_numeric(prop);
19741356Seschrock 		*source = "";
19751356Seschrock 	}
19761356Seschrock 
19771356Seschrock 	return (value);
19781356Seschrock }
19791356Seschrock 
19801356Seschrock static char *
19811356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
19821356Seschrock {
19831356Seschrock 	nvlist_t *nv;
19841356Seschrock 	char *value;
19851356Seschrock 
19862885Sahrens 	*source = NULL;
19871356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
19881356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
19895094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
19905094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
19911356Seschrock 	} else {
19921356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
19931356Seschrock 			value = "";
19941356Seschrock 		*source = "";
19951356Seschrock 	}
19961356Seschrock 
19971356Seschrock 	return (value);
19981356Seschrock }
19991356Seschrock 
20001356Seschrock /*
2001789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2002789Sahrens  * zfs_prop_get_int() are built using this interface.
2003789Sahrens  *
2004789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2005789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2006789Sahrens  * If they differ from the on-disk values, report the current values and mark
2007789Sahrens  * the source "temporary".
2008789Sahrens  */
20092082Seschrock static int
20105094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
20112082Seschrock     char **source, uint64_t *val)
2012789Sahrens {
20135147Srm160521 	zfs_cmd_t zc = { 0 };
20145592Stimh 	nvlist_t *zplprops = NULL;
2015789Sahrens 	struct mnttab mnt;
20163265Sahrens 	char *mntopt_on = NULL;
20173265Sahrens 	char *mntopt_off = NULL;
2018789Sahrens 
2019789Sahrens 	*source = NULL;
2020789Sahrens 
20213265Sahrens 	switch (prop) {
20223265Sahrens 	case ZFS_PROP_ATIME:
20233265Sahrens 		mntopt_on = MNTOPT_ATIME;
20243265Sahrens 		mntopt_off = MNTOPT_NOATIME;
20253265Sahrens 		break;
20263265Sahrens 
20273265Sahrens 	case ZFS_PROP_DEVICES:
20283265Sahrens 		mntopt_on = MNTOPT_DEVICES;
20293265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
20303265Sahrens 		break;
20313265Sahrens 
20323265Sahrens 	case ZFS_PROP_EXEC:
20333265Sahrens 		mntopt_on = MNTOPT_EXEC;
20343265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
20353265Sahrens 		break;
20363265Sahrens 
20373265Sahrens 	case ZFS_PROP_READONLY:
20383265Sahrens 		mntopt_on = MNTOPT_RO;
20393265Sahrens 		mntopt_off = MNTOPT_RW;
20403265Sahrens 		break;
20413265Sahrens 
20423265Sahrens 	case ZFS_PROP_SETUID:
20433265Sahrens 		mntopt_on = MNTOPT_SETUID;
20443265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
20453265Sahrens 		break;
20463265Sahrens 
20473265Sahrens 	case ZFS_PROP_XATTR:
20483265Sahrens 		mntopt_on = MNTOPT_XATTR;
20493265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
20503265Sahrens 		break;
20515331Samw 
20525331Samw 	case ZFS_PROP_NBMAND:
20535331Samw 		mntopt_on = MNTOPT_NBMAND;
20545331Samw 		mntopt_off = MNTOPT_NONBMAND;
20555331Samw 		break;
20563265Sahrens 	}
20573265Sahrens 
20582474Seschrock 	/*
20592474Seschrock 	 * Because looking up the mount options is potentially expensive
20602474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
20612474Seschrock 	 * we're looking up a property which requires its presence.
20622474Seschrock 	 */
20632474Seschrock 	if (!zhp->zfs_mntcheck &&
20643265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
20653265Sahrens 		struct mnttab entry, search = { 0 };
20663265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
20672474Seschrock 
20682474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
20692474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
20703265Sahrens 		rewind(mnttab);
20713265Sahrens 
20723265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
20733265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
20743265Sahrens 			    entry.mnt_mntopts);
20753265Sahrens 			if (zhp->zfs_mntopts == NULL)
20763265Sahrens 				return (-1);
20773265Sahrens 		}
20782474Seschrock 
20792474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
20802474Seschrock 	}
20812474Seschrock 
2082789Sahrens 	if (zhp->zfs_mntopts == NULL)
2083789Sahrens 		mnt.mnt_mntopts = "";
2084789Sahrens 	else
2085789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2086789Sahrens 
2087789Sahrens 	switch (prop) {
2088789Sahrens 	case ZFS_PROP_ATIME:
20893265Sahrens 	case ZFS_PROP_DEVICES:
20903265Sahrens 	case ZFS_PROP_EXEC:
20913265Sahrens 	case ZFS_PROP_READONLY:
20923265Sahrens 	case ZFS_PROP_SETUID:
20933265Sahrens 	case ZFS_PROP_XATTR:
20945331Samw 	case ZFS_PROP_NBMAND:
20952082Seschrock 		*val = getprop_uint64(zhp, prop, source);
20962082Seschrock 
20973265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
20982082Seschrock 			*val = B_TRUE;
2099789Sahrens 			if (src)
21005094Slling 				*src = ZPROP_SRC_TEMPORARY;
21013265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
21022082Seschrock 			*val = B_FALSE;
2103789Sahrens 			if (src)
21045094Slling 				*src = ZPROP_SRC_TEMPORARY;
2105789Sahrens 		}
21062082Seschrock 		break;
2107789Sahrens 
21083265Sahrens 	case ZFS_PROP_CANMOUNT:
21092082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21106168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
21113417Srm160521 			*source = zhp->zfs_name;
21123417Srm160521 		else
21133417Srm160521 			*source = "";	/* default */
21142082Seschrock 		break;
2115789Sahrens 
2116789Sahrens 	case ZFS_PROP_QUOTA:
21175378Sck153898 	case ZFS_PROP_REFQUOTA:
2118789Sahrens 	case ZFS_PROP_RESERVATION:
21195378Sck153898 	case ZFS_PROP_REFRESERVATION:
21202885Sahrens 		*val = getprop_uint64(zhp, prop, source);
21212885Sahrens 		if (*val == 0)
2122789Sahrens 			*source = "";	/* default */
2123789Sahrens 		else
2124789Sahrens 			*source = zhp->zfs_name;
21252082Seschrock 		break;
2126789Sahrens 
2127789Sahrens 	case ZFS_PROP_MOUNTED:
21282082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
21292082Seschrock 		break;
2130789Sahrens 
21313377Seschrock 	case ZFS_PROP_NUMCLONES:
21323377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
21333377Seschrock 		break;
21343377Seschrock 
21355147Srm160521 	case ZFS_PROP_VERSION:
21365498Stimh 	case ZFS_PROP_NORMALIZE:
21375498Stimh 	case ZFS_PROP_UTF8ONLY:
21385498Stimh 	case ZFS_PROP_CASE:
21395498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
21405498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
21415473Srm160521 			return (-1);
21425147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21435498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
21445498Stimh 			zcmd_free_nvlists(&zc);
21455147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21465498Stimh 			    "unable to get %s property"),
21475498Stimh 			    zfs_prop_to_name(prop));
21485147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
21495147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
21505147Srm160521 		}
21515498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
21525498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
21535498Stimh 		    val) != 0) {
21545498Stimh 			zcmd_free_nvlists(&zc);
21555498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21565498Stimh 			    "unable to get %s property"),
21575498Stimh 			    zfs_prop_to_name(prop));
21585498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
21595498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
21605498Stimh 		}
21615592Stimh 		if (zplprops)
21625592Stimh 			nvlist_free(zplprops);
21635498Stimh 		zcmd_free_nvlists(&zc);
21645147Srm160521 		break;
21655147Srm160521 
2166789Sahrens 	default:
21674577Sahrens 		switch (zfs_prop_get_type(prop)) {
21684787Sahrens 		case PROP_TYPE_NUMBER:
21694787Sahrens 		case PROP_TYPE_INDEX:
21704577Sahrens 			*val = getprop_uint64(zhp, prop, source);
21714577Sahrens 			break;
21724577Sahrens 
21734787Sahrens 		case PROP_TYPE_STRING:
21744577Sahrens 		default:
21754577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
21764577Sahrens 			    "cannot get non-numeric property"));
21774577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
21784577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
21794577Sahrens 		}
2180789Sahrens 	}
2181789Sahrens 
2182789Sahrens 	return (0);
2183789Sahrens }
2184789Sahrens 
2185789Sahrens /*
2186789Sahrens  * Calculate the source type, given the raw source string.
2187789Sahrens  */
2188789Sahrens static void
21895094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2190789Sahrens     char *statbuf, size_t statlen)
2191789Sahrens {
21925094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2193789Sahrens 		return;
2194789Sahrens 
2195789Sahrens 	if (source == NULL) {
21965094Slling 		*srctype = ZPROP_SRC_NONE;
2197789Sahrens 	} else if (source[0] == '\0') {
21985094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2199789Sahrens 	} else {
2200789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
22015094Slling 			*srctype = ZPROP_SRC_LOCAL;
2202789Sahrens 		} else {
2203789Sahrens 			(void) strlcpy(statbuf, source, statlen);
22045094Slling 			*srctype = ZPROP_SRC_INHERITED;
2205789Sahrens 		}
2206789Sahrens 	}
2207789Sahrens 
2208789Sahrens }
2209789Sahrens 
2210789Sahrens /*
2211789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2212789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2213789Sahrens  * human-readable form.
2214789Sahrens  *
2215789Sahrens  * Returns 0 on success, or -1 on error.
2216789Sahrens  */
2217789Sahrens int
2218789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
22195094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2220789Sahrens {
2221789Sahrens 	char *source = NULL;
2222789Sahrens 	uint64_t val;
2223789Sahrens 	char *str;
2224789Sahrens 	const char *root;
22252676Seschrock 	const char *strval;
2226789Sahrens 
2227789Sahrens 	/*
2228789Sahrens 	 * Check to see if this property applies to our object
2229789Sahrens 	 */
2230789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2231789Sahrens 		return (-1);
2232789Sahrens 
2233789Sahrens 	if (src)
22345094Slling 		*src = ZPROP_SRC_NONE;
2235789Sahrens 
2236789Sahrens 	switch (prop) {
2237789Sahrens 	case ZFS_PROP_CREATION:
2238789Sahrens 		/*
2239789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2240789Sahrens 		 * this into a string unless 'literal' is specified.
2241789Sahrens 		 */
2242789Sahrens 		{
22432885Sahrens 			val = getprop_uint64(zhp, prop, &source);
22442885Sahrens 			time_t time = (time_t)val;
2245789Sahrens 			struct tm t;
2246789Sahrens 
2247789Sahrens 			if (literal ||
2248789Sahrens 			    localtime_r(&time, &t) == NULL ||
2249789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2250789Sahrens 			    &t) == 0)
22512885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2252789Sahrens 		}
2253789Sahrens 		break;
2254789Sahrens 
2255789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2256789Sahrens 		/*
2257789Sahrens 		 * Getting the precise mountpoint can be tricky.
2258789Sahrens 		 *
2259789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2260789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2261789Sahrens 		 *    after our ancestor and append it to the inherited value.
2262789Sahrens 		 *
2263789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2264789Sahrens 		 * root to any values we return.
2265789Sahrens 		 */
22661544Seschrock 		root = zhp->zfs_root;
22671356Seschrock 		str = getprop_string(zhp, prop, &source);
22681356Seschrock 
2269*6612Sgw25295 		if (str[0] == '/') {
22701356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2271789Sahrens 
2272*6612Sgw25295 
2273789Sahrens 			if (relpath[0] == '/')
2274789Sahrens 				relpath++;
2275*6612Sgw25295 
2276*6612Sgw25295 			/*
2277*6612Sgw25295 			 * Special case an alternate root of '/'. This will
2278*6612Sgw25295 			 * avoid having multiple leading slashes in the
2279*6612Sgw25295 			 * mountpoint path.
2280*6612Sgw25295 			 */
2281*6612Sgw25295 			if (strcmp(root, "/") == 0)
2282*6612Sgw25295 				root++;
2283*6612Sgw25295 
2284*6612Sgw25295 			/*
2285*6612Sgw25295 			 * If the mountpoint is '/' then skip over this
2286*6612Sgw25295 			 * if we are obtaining either an alternate root or
2287*6612Sgw25295 			 * an inherited mountpoint.
2288*6612Sgw25295 			 */
2289*6612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
2290*6612Sgw25295 			    relpath[0] != '\0'))
22911356Seschrock 				str++;
2292789Sahrens 
2293789Sahrens 			if (relpath[0] == '\0')
2294789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
22951356Seschrock 				    root, str);
2296789Sahrens 			else
2297789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
22981356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2299789Sahrens 				    relpath);
2300789Sahrens 		} else {
2301789Sahrens 			/* 'legacy' or 'none' */
23021356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2303789Sahrens 		}
2304789Sahrens 
2305789Sahrens 		break;
2306789Sahrens 
2307789Sahrens 	case ZFS_PROP_ORIGIN:
23082885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2309789Sahrens 		    proplen);
2310789Sahrens 		/*
2311789Sahrens 		 * If there is no parent at all, return failure to indicate that
2312789Sahrens 		 * it doesn't apply to this dataset.
2313789Sahrens 		 */
2314789Sahrens 		if (propbuf[0] == '\0')
2315789Sahrens 			return (-1);
2316789Sahrens 		break;
2317789Sahrens 
2318789Sahrens 	case ZFS_PROP_QUOTA:
23195378Sck153898 	case ZFS_PROP_REFQUOTA:
2320789Sahrens 	case ZFS_PROP_RESERVATION:
23215378Sck153898 	case ZFS_PROP_REFRESERVATION:
23225378Sck153898 
23232082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23242082Seschrock 			return (-1);
2325789Sahrens 
2326789Sahrens 		/*
2327789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2328789Sahrens 		 * (unless literal is set), and indicate that it's the default
2329789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2330789Sahrens 		 * that its set locally.
2331789Sahrens 		 */
2332789Sahrens 		if (val == 0) {
2333789Sahrens 			if (literal)
2334789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2335789Sahrens 			else
2336789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2337789Sahrens 		} else {
2338789Sahrens 			if (literal)
23392856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
23403912Slling 				    (u_longlong_t)val);
2341789Sahrens 			else
2342789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2343789Sahrens 		}
2344789Sahrens 		break;
2345789Sahrens 
2346789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
23472082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23482082Seschrock 			return (-1);
23492856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
23502856Snd150628 		    val / 100, (longlong_t)val % 100);
2351789Sahrens 		break;
2352789Sahrens 
2353789Sahrens 	case ZFS_PROP_TYPE:
2354789Sahrens 		switch (zhp->zfs_type) {
2355789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2356789Sahrens 			str = "filesystem";
2357789Sahrens 			break;
2358789Sahrens 		case ZFS_TYPE_VOLUME:
2359789Sahrens 			str = "volume";
2360789Sahrens 			break;
2361789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2362789Sahrens 			str = "snapshot";
2363789Sahrens 			break;
2364789Sahrens 		default:
23652082Seschrock 			abort();
2366789Sahrens 		}
2367789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2368789Sahrens 		break;
2369789Sahrens 
2370789Sahrens 	case ZFS_PROP_MOUNTED:
2371789Sahrens 		/*
2372789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2373789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2374789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2375789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2376789Sahrens 		 */
23772082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
23782082Seschrock 		    src, &source, &val) != 0)
23792082Seschrock 			return (-1);
23802082Seschrock 		if (val)
2381789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2382789Sahrens 		else
2383789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2384789Sahrens 		break;
2385789Sahrens 
2386789Sahrens 	case ZFS_PROP_NAME:
2387789Sahrens 		/*
2388789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2389789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2390789Sahrens 		 * consumers.
2391789Sahrens 		 */
2392789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2393789Sahrens 		break;
2394789Sahrens 
2395789Sahrens 	default:
23964577Sahrens 		switch (zfs_prop_get_type(prop)) {
23974787Sahrens 		case PROP_TYPE_NUMBER:
23984577Sahrens 			if (get_numeric_property(zhp, prop, src,
23994577Sahrens 			    &source, &val) != 0)
24004577Sahrens 				return (-1);
24014577Sahrens 			if (literal)
24024577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
24034577Sahrens 				    (u_longlong_t)val);
24044577Sahrens 			else
24054577Sahrens 				zfs_nicenum(val, propbuf, proplen);
24064577Sahrens 			break;
24074577Sahrens 
24084787Sahrens 		case PROP_TYPE_STRING:
24094577Sahrens 			(void) strlcpy(propbuf,
24104577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
24114577Sahrens 			break;
24124577Sahrens 
24134787Sahrens 		case PROP_TYPE_INDEX:
24144861Sahrens 			if (get_numeric_property(zhp, prop, src,
24154861Sahrens 			    &source, &val) != 0)
24164861Sahrens 				return (-1);
24174861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
24184577Sahrens 				return (-1);
24194577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
24204577Sahrens 			break;
24214577Sahrens 
24224577Sahrens 		default:
24234577Sahrens 			abort();
24244577Sahrens 		}
2425789Sahrens 	}
2426789Sahrens 
2427789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2428789Sahrens 
2429789Sahrens 	return (0);
2430789Sahrens }
2431789Sahrens 
2432789Sahrens /*
2433789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2434789Sahrens  * the given property is the appropriate type; should only be used with
2435789Sahrens  * hard-coded property types.
2436789Sahrens  */
2437789Sahrens uint64_t
2438789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2439789Sahrens {
2440789Sahrens 	char *source;
24412082Seschrock 	uint64_t val;
24422082Seschrock 
24435367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
24442082Seschrock 
24452082Seschrock 	return (val);
2446789Sahrens }
2447789Sahrens 
24485713Srm160521 int
24495713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
24505713Srm160521 {
24515713Srm160521 	char buf[64];
24525713Srm160521 
24535713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
24545713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
24555713Srm160521 }
24565713Srm160521 
2457789Sahrens /*
2458789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2459789Sahrens  */
2460789Sahrens int
2461789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
24625094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2463789Sahrens {
2464789Sahrens 	char *source;
2465789Sahrens 
2466789Sahrens 	/*
2467789Sahrens 	 * Check to see if this property applies to our object
2468789Sahrens 	 */
24694849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
24703237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
24712082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
24722082Seschrock 		    zfs_prop_to_name(prop)));
24734849Sahrens 	}
2474789Sahrens 
2475789Sahrens 	if (src)
24765094Slling 		*src = ZPROP_SRC_NONE;
2477789Sahrens 
24782082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
24792082Seschrock 		return (-1);
2480789Sahrens 
2481789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2482789Sahrens 
2483789Sahrens 	return (0);
2484789Sahrens }
2485789Sahrens 
2486789Sahrens /*
2487789Sahrens  * Returns the name of the given zfs handle.
2488789Sahrens  */
2489789Sahrens const char *
2490789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2491789Sahrens {
2492789Sahrens 	return (zhp->zfs_name);
2493789Sahrens }
2494789Sahrens 
2495789Sahrens /*
2496789Sahrens  * Returns the type of the given zfs handle.
2497789Sahrens  */
2498789Sahrens zfs_type_t
2499789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2500789Sahrens {
2501789Sahrens 	return (zhp->zfs_type);
2502789Sahrens }
2503789Sahrens 
2504789Sahrens /*
25051356Seschrock  * Iterate over all child filesystems
2506789Sahrens  */
2507789Sahrens int
25081356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2509789Sahrens {
2510789Sahrens 	zfs_cmd_t zc = { 0 };
2511789Sahrens 	zfs_handle_t *nzhp;
2512789Sahrens 	int ret;
2513789Sahrens 
25145367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25155367Sahrens 		return (0);
25165367Sahrens 
2517789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25182082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2519789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2520789Sahrens 		/*
2521789Sahrens 		 * Ignore private dataset names.
2522789Sahrens 		 */
2523789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2524789Sahrens 			continue;
2525789Sahrens 
2526789Sahrens 		/*
2527789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2528789Sahrens 		 * that the pool has since been removed.
2529789Sahrens 		 */
25302082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25312082Seschrock 		    zc.zc_name)) == NULL)
2532789Sahrens 			continue;
2533789Sahrens 
2534789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2535789Sahrens 			return (ret);
2536789Sahrens 	}
2537789Sahrens 
2538789Sahrens 	/*
2539789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2540789Sahrens 	 * returned, then the underlying dataset has been removed since we
2541789Sahrens 	 * obtained the handle.
2542789Sahrens 	 */
2543789Sahrens 	if (errno != ESRCH && errno != ENOENT)
25442082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
25452082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2546789Sahrens 
25471356Seschrock 	return (0);
25481356Seschrock }
25491356Seschrock 
25501356Seschrock /*
25511356Seschrock  * Iterate over all snapshots
25521356Seschrock  */
25531356Seschrock int
25541356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25551356Seschrock {
25561356Seschrock 	zfs_cmd_t zc = { 0 };
25571356Seschrock 	zfs_handle_t *nzhp;
25581356Seschrock 	int ret;
2559789Sahrens 
25605367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
25615367Sahrens 		return (0);
25625367Sahrens 
2563789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25642082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
25652082Seschrock 	    &zc) == 0;
2566789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2567789Sahrens 
25682082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25692082Seschrock 		    zc.zc_name)) == NULL)
2570789Sahrens 			continue;
2571789Sahrens 
2572789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2573789Sahrens 			return (ret);
2574789Sahrens 	}
2575789Sahrens 
2576789Sahrens 	/*
2577789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2578789Sahrens 	 * returned, then the underlying dataset has been removed since we
2579789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2580789Sahrens 	 */
2581789Sahrens 	if (errno != ESRCH && errno != ENOENT)
25822082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
25832082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2584789Sahrens 
2585789Sahrens 	return (0);
2586789Sahrens }
2587789Sahrens 
2588789Sahrens /*
25891356Seschrock  * Iterate over all children, snapshots and filesystems
25901356Seschrock  */
25911356Seschrock int
25921356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
25931356Seschrock {
25941356Seschrock 	int ret;
25951356Seschrock 
25961356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
25971356Seschrock 		return (ret);
25981356Seschrock 
25991356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
26001356Seschrock }
26011356Seschrock 
26021356Seschrock /*
2603789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2604789Sahrens  * Can return NULL if this is a pool.
2605789Sahrens  */
2606789Sahrens static int
2607789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2608789Sahrens {
2609789Sahrens 	char *loc;
2610789Sahrens 
2611789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2612789Sahrens 		return (-1);
2613789Sahrens 
2614789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2615789Sahrens 	buf[loc - path] = '\0';
2616789Sahrens 
2617789Sahrens 	return (0);
2618789Sahrens }
2619789Sahrens 
2620789Sahrens /*
26214490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
26224490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26234490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
26244490Svb160487  * length of already existing prefix of the given path.  We also fetch the
26254490Svb160487  * 'zoned' property, which is used to validate property settings when creating
26264490Svb160487  * new datasets.
2627789Sahrens  */
2628789Sahrens static int
26294490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
26304490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2631789Sahrens {
2632789Sahrens 	zfs_cmd_t zc = { 0 };
2633789Sahrens 	char parent[ZFS_MAXNAMELEN];
2634789Sahrens 	char *slash;
26351356Seschrock 	zfs_handle_t *zhp;
26362082Seschrock 	char errbuf[1024];
26372082Seschrock 
26382082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
26392082Seschrock 	    path);
2640789Sahrens 
2641789Sahrens 	/* get parent, and check to see if this is just a pool */
2642789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
26432082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26442082Seschrock 		    "missing dataset name"));
26452082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2646789Sahrens 	}
2647789Sahrens 
2648789Sahrens 	/* check to see if the pool exists */
2649789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2650789Sahrens 		slash = parent + strlen(parent);
2651789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2652789Sahrens 	zc.zc_name[slash - parent] = '\0';
26532082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2654789Sahrens 	    errno == ENOENT) {
26552082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26562082Seschrock 		    "no such pool '%s'"), zc.zc_name);
26572082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2658789Sahrens 	}
2659789Sahrens 
2660789Sahrens 	/* check to see if the parent dataset exists */
26614490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
26624490Svb160487 		if (errno == ENOENT && accept_ancestor) {
26634490Svb160487 			/*
26644490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
26654490Svb160487 			 */
26664490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
26674490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26684490Svb160487 				    "no such pool '%s'"), zc.zc_name);
26694490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
26704490Svb160487 			}
26714490Svb160487 		} else if (errno == ENOENT) {
26722082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26732082Seschrock 			    "parent does not exist"));
26742082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
26754490Svb160487 		} else
26762082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2677789Sahrens 	}
2678789Sahrens 
26792676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2680789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
26812676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
26822082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
26831356Seschrock 		zfs_close(zhp);
2684789Sahrens 		return (-1);
2685789Sahrens 	}
2686789Sahrens 
2687789Sahrens 	/* make sure parent is a filesystem */
26881356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
26892082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26902082Seschrock 		    "parent is not a filesystem"));
26912082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
26921356Seschrock 		zfs_close(zhp);
2693789Sahrens 		return (-1);
2694789Sahrens 	}
2695789Sahrens 
26961356Seschrock 	zfs_close(zhp);
26974490Svb160487 	if (prefixlen != NULL)
26984490Svb160487 		*prefixlen = strlen(parent);
26994490Svb160487 	return (0);
27004490Svb160487 }
27014490Svb160487 
27024490Svb160487 /*
27034490Svb160487  * Finds whether the dataset of the given type(s) exists.
27044490Svb160487  */
27054490Svb160487 boolean_t
27064490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
27074490Svb160487 {
27084490Svb160487 	zfs_handle_t *zhp;
27094490Svb160487 
27105326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
27114490Svb160487 		return (B_FALSE);
27124490Svb160487 
27134490Svb160487 	/*
27144490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27154490Svb160487 	 */
27164490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27174490Svb160487 		int ds_type = zhp->zfs_type;
27184490Svb160487 
27194490Svb160487 		zfs_close(zhp);
27204490Svb160487 		if (types & ds_type)
27214490Svb160487 			return (B_TRUE);
27224490Svb160487 	}
27234490Svb160487 	return (B_FALSE);
27244490Svb160487 }
27254490Svb160487 
27264490Svb160487 /*
27275367Sahrens  * Given a path to 'target', create all the ancestors between
27285367Sahrens  * the prefixlen portion of the path, and the target itself.
27295367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
27305367Sahrens  */
27315367Sahrens int
27325367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
27335367Sahrens {
27345367Sahrens 	zfs_handle_t *h;
27355367Sahrens 	char *cp;
27365367Sahrens 	const char *opname;
27375367Sahrens 
27385367Sahrens 	/* make sure prefix exists */
27395367Sahrens 	cp = target + prefixlen;
27405367Sahrens 	if (*cp != '/') {
27415367Sahrens 		assert(strchr(cp, '/') == NULL);
27425367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27435367Sahrens 	} else {
27445367Sahrens 		*cp = '\0';
27455367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27465367Sahrens 		*cp = '/';
27475367Sahrens 	}
27485367Sahrens 	if (h == NULL)
27495367Sahrens 		return (-1);
27505367Sahrens 	zfs_close(h);
27515367Sahrens 
27525367Sahrens 	/*
27535367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
27545367Sahrens 	 * up to the prefixlen-long one.
27555367Sahrens 	 */
27565367Sahrens 	for (cp = target + prefixlen + 1;
27575367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
27585367Sahrens 		char *logstr;
27595367Sahrens 
27605367Sahrens 		*cp = '\0';
27615367Sahrens 
27625367Sahrens 		h = make_dataset_handle(hdl, target);
27635367Sahrens 		if (h) {
27645367Sahrens 			/* it already exists, nothing to do here */
27655367Sahrens 			zfs_close(h);
27665367Sahrens 			continue;
27675367Sahrens 		}
27685367Sahrens 
27695367Sahrens 		logstr = hdl->libzfs_log_str;
27705367Sahrens 		hdl->libzfs_log_str = NULL;
27715367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
27725367Sahrens 		    NULL) != 0) {
27735367Sahrens 			hdl->libzfs_log_str = logstr;
27745367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
27755367Sahrens 			goto ancestorerr;
27765367Sahrens 		}
27775367Sahrens 
27785367Sahrens 		hdl->libzfs_log_str = logstr;
27795367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
27805367Sahrens 		if (h == NULL) {
27815367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
27825367Sahrens 			goto ancestorerr;
27835367Sahrens 		}
27845367Sahrens 
27855367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
27865367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
27875367Sahrens 			goto ancestorerr;
27885367Sahrens 		}
27895367Sahrens 
27905367Sahrens 		if (zfs_share(h) != 0) {
27915367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
27925367Sahrens 			goto ancestorerr;
27935367Sahrens 		}
27945367Sahrens 
27955367Sahrens 		zfs_close(h);
27965367Sahrens 	}
27975367Sahrens 
27985367Sahrens 	return (0);
27995367Sahrens 
28005367Sahrens ancestorerr:
28015367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28025367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28035367Sahrens 	return (-1);
28045367Sahrens }
28055367Sahrens 
28065367Sahrens /*
28074490Svb160487  * Creates non-existing ancestors of the given path.
28084490Svb160487  */
28094490Svb160487 int
28104490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28114490Svb160487 {
28124490Svb160487 	int prefix;
28134490Svb160487 	uint64_t zoned;
28144490Svb160487 	char *path_copy;
28154490Svb160487 	int rc;
28164490Svb160487 
28174490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28184490Svb160487 		return (-1);
28194490Svb160487 
28204490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28214490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28224490Svb160487 		free(path_copy);
28234490Svb160487 	}
28244490Svb160487 	if (path_copy == NULL || rc != 0)
28254490Svb160487 		return (-1);
28264490Svb160487 
2827789Sahrens 	return (0);
2828789Sahrens }
2829789Sahrens 
2830789Sahrens /*
28312676Seschrock  * Create a new filesystem or volume.
2832789Sahrens  */
2833789Sahrens int
28342082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28352676Seschrock     nvlist_t *props)
2836789Sahrens {
2837789Sahrens 	zfs_cmd_t zc = { 0 };
2838789Sahrens 	int ret;
2839789Sahrens 	uint64_t size = 0;
2840789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
28412082Seschrock 	char errbuf[1024];
28422676Seschrock 	uint64_t zoned;
28432082Seschrock 
28442082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28452082Seschrock 	    "cannot create '%s'"), path);
2846789Sahrens 
2847789Sahrens 	/* validate the path, taking care to note the extended error message */
28485326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
28492082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2850789Sahrens 
2851789Sahrens 	/* validate parents exist */
28524490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2853789Sahrens 		return (-1);
2854789Sahrens 
2855789Sahrens 	/*
2856789Sahrens 	 * The failure modes when creating a dataset of a different type over
2857789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2858789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2859789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2860789Sahrens 	 * first try to see if the dataset exists.
2861789Sahrens 	 */
2862789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
28635094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
28642082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28652082Seschrock 		    "dataset already exists"));
28662082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2867789Sahrens 	}
2868789Sahrens 
2869789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2870789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2871789Sahrens 	else
2872789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2873789Sahrens 
28745094Slling 	if (props && (props = zfs_validate_properties(hdl, type, props,
28753912Slling 	    zoned, NULL, errbuf)) == 0)
28762676Seschrock 		return (-1);
28772676Seschrock 
2878789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
28791133Seschrock 		/*
28801133Seschrock 		 * If we are creating a volume, the size and block size must
28811133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
28821133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
28831133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
28841133Seschrock 		 * zero.
28851133Seschrock 		 */
28862676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
28872676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
28882676Seschrock 			nvlist_free(props);
28892082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28902676Seschrock 			    "missing volume size"));
28912676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2892789Sahrens 		}
2893789Sahrens 
28942676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
28952676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
28962676Seschrock 		    &blocksize)) != 0) {
28972676Seschrock 			if (ret == ENOENT) {
28982676Seschrock 				blocksize = zfs_prop_default_numeric(
28992676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29002676Seschrock 			} else {
29012676Seschrock 				nvlist_free(props);
29022676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29032676Seschrock 				    "missing volume block size"));
29042676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29052676Seschrock 			}
29062676Seschrock 		}
29072676Seschrock 
29082676Seschrock 		if (size == 0) {
29092676Seschrock 			nvlist_free(props);
29102082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29112676Seschrock 			    "volume size cannot be zero"));
29122676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29131133Seschrock 		}
29141133Seschrock 
29151133Seschrock 		if (size % blocksize != 0) {
29162676Seschrock 			nvlist_free(props);
29172082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29182676Seschrock 			    "volume size must be a multiple of volume block "
29192676Seschrock 			    "size"));
29202676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29211133Seschrock 		}
2922789Sahrens 	}
2923789Sahrens 
29245094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
29252676Seschrock 		return (-1);
29262676Seschrock 	nvlist_free(props);
29272676Seschrock 
2928789Sahrens 	/* create the dataset */
29294543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2930789Sahrens 
29313912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29322082Seschrock 		ret = zvol_create_link(hdl, path);
29333912Slling 		if (ret) {
29343912Slling 			(void) zfs_standard_error(hdl, errno,
29353912Slling 			    dgettext(TEXT_DOMAIN,
29363912Slling 			    "Volume successfully created, but device links "
29373912Slling 			    "were not created"));
29383912Slling 			zcmd_free_nvlists(&zc);
29393912Slling 			return (-1);
29403912Slling 		}
29413912Slling 	}
2942789Sahrens 
29432676Seschrock 	zcmd_free_nvlists(&zc);
29442676Seschrock 
2945789Sahrens 	/* check for failure */
2946789Sahrens 	if (ret != 0) {
2947789Sahrens 		char parent[ZFS_MAXNAMELEN];
2948789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2949789Sahrens 
2950789Sahrens 		switch (errno) {
2951789Sahrens 		case ENOENT:
29522082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29532082Seschrock 			    "no such parent '%s'"), parent);
29542082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2955789Sahrens 
2956789Sahrens 		case EINVAL:
29572082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29583413Smmusante 			    "parent '%s' is not a filesystem"), parent);
29592082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2960789Sahrens 
2961789Sahrens 		case EDOM:
29622082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29632676Seschrock 			    "volume block size must be power of 2 from "
29642676Seschrock 			    "%u to %uk"),
2965789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2966789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
29672082Seschrock 
29682676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29692082Seschrock 
29704603Sahrens 		case ENOTSUP:
29714603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29724603Sahrens 			    "pool must be upgraded to set this "
29734603Sahrens 			    "property or value"));
29744603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2975789Sahrens #ifdef _ILP32
2976789Sahrens 		case EOVERFLOW:
2977789Sahrens 			/*
2978789Sahrens 			 * This platform can't address a volume this big.
2979789Sahrens 			 */
29802082Seschrock 			if (type == ZFS_TYPE_VOLUME)
29812082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
29822082Seschrock 				    errbuf));
2983789Sahrens #endif
29842082Seschrock 			/* FALLTHROUGH */
2985789Sahrens 		default:
29862082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2987789Sahrens 		}
2988789Sahrens 	}
2989789Sahrens 
2990789Sahrens 	return (0);
2991789Sahrens }
2992789Sahrens 
2993789Sahrens /*
2994789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2995789Sahrens  * isn't mounted, and that there are no active dependents.
2996789Sahrens  */
2997789Sahrens int
2998789Sahrens zfs_destroy(zfs_handle_t *zhp)
2999789Sahrens {
3000789Sahrens 	zfs_cmd_t zc = { 0 };
3001789Sahrens 
3002789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3003789Sahrens 
30042676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30053126Sahl 		/*
30064543Smarks 		 * If user doesn't have permissions to unshare volume, then
30074543Smarks 		 * abort the request.  This would only happen for a
30084543Smarks 		 * non-privileged user.
30093126Sahl 		 */
30104543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30114543Smarks 			return (-1);
30124543Smarks 		}
30133126Sahl 
30142082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3015789Sahrens 			return (-1);
3016789Sahrens 
3017789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3018789Sahrens 	} else {
3019789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3020789Sahrens 	}
3021789Sahrens 
30224543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30233237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30242082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30252082Seschrock 		    zhp->zfs_name));
30262199Sahrens 	}
3027789Sahrens 
3028789Sahrens 	remove_mountpoint(zhp);
3029789Sahrens 
3030789Sahrens 	return (0);
3031789Sahrens }
3032789Sahrens 
30332199Sahrens struct destroydata {
30342199Sahrens 	char *snapname;
30352199Sahrens 	boolean_t gotone;
30363265Sahrens 	boolean_t closezhp;
30372199Sahrens };
30382199Sahrens 
30392199Sahrens static int
30402199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
30412199Sahrens {
30422199Sahrens 	struct destroydata *dd = arg;
30432199Sahrens 	zfs_handle_t *szhp;
30442199Sahrens 	char name[ZFS_MAXNAMELEN];
30453265Sahrens 	boolean_t closezhp = dd->closezhp;
30463265Sahrens 	int rv;
30472199Sahrens 
30482676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
30492676Seschrock 	(void) strlcat(name, "@", sizeof (name));
30502676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
30512199Sahrens 
30522199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
30532199Sahrens 	if (szhp) {
30542199Sahrens 		dd->gotone = B_TRUE;
30552199Sahrens 		zfs_close(szhp);
30562199Sahrens 	}
30572199Sahrens 
30582199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
30592199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
30602199Sahrens 		/*
30612199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
30622199Sahrens 		 * return an error, because then we wouldn't visit all
30632199Sahrens 		 * the volumes.
30642199Sahrens 		 */
30652199Sahrens 	}
30662199Sahrens 
30673265Sahrens 	dd->closezhp = B_TRUE;
30683265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
30693265Sahrens 	if (closezhp)
30703265Sahrens 		zfs_close(zhp);
30713265Sahrens 	return (rv);
30722199Sahrens }
30732199Sahrens 
30742199Sahrens /*
30752199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
30762199Sahrens  */
30772199Sahrens int
30782199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
30792199Sahrens {
30802199Sahrens 	zfs_cmd_t zc = { 0 };
30812199Sahrens 	int ret;
30822199Sahrens 	struct destroydata dd = { 0 };
30832199Sahrens 
30842199Sahrens 	dd.snapname = snapname;
30852199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
30862199Sahrens 
30872199Sahrens 	if (!dd.gotone) {
30883237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
30892199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
30902199Sahrens 		    zhp->zfs_name, snapname));
30912199Sahrens 	}
30922199Sahrens 
30932199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
30942676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
30952199Sahrens 
30964543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
30972199Sahrens 	if (ret != 0) {
30982199Sahrens 		char errbuf[1024];
30992199Sahrens 
31002199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31012199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31022199Sahrens 
31032199Sahrens 		switch (errno) {
31042199Sahrens 		case EEXIST:
31052199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31062199Sahrens 			    "snapshot is cloned"));
31072199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31082199Sahrens 
31092199Sahrens 		default:
31102199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31112199Sahrens 			    errbuf));
31122199Sahrens 		}
31132199Sahrens 	}
31142199Sahrens 
31152199Sahrens 	return (0);
31162199Sahrens }
31172199Sahrens 
3118789Sahrens /*
3119789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3120789Sahrens  */
3121789Sahrens int
31222676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3123789Sahrens {
3124789Sahrens 	zfs_cmd_t zc = { 0 };
3125789Sahrens 	char parent[ZFS_MAXNAMELEN];
3126789Sahrens 	int ret;
31272082Seschrock 	char errbuf[1024];
31282082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31292676Seschrock 	zfs_type_t type;
31302676Seschrock 	uint64_t zoned;
3131789Sahrens 
3132789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3133789Sahrens 
31342082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31352082Seschrock 	    "cannot create '%s'"), target);
31362082Seschrock 
3137789Sahrens 	/* validate the target name */
31385326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
31392082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3140789Sahrens 
3141789Sahrens 	/* validate parents exist */
31424490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3143789Sahrens 		return (-1);
3144789Sahrens 
3145789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3146789Sahrens 
3147789Sahrens 	/* do the clone */
31482676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3149789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
31502744Snn35248 		type = ZFS_TYPE_VOLUME;
31512676Seschrock 	} else {
3152789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
31532744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
31542676Seschrock 	}
31552676Seschrock 
31562676Seschrock 	if (props) {
31575094Slling 		if ((props = zfs_validate_properties(hdl, type, props,
31583912Slling 		    zoned, zhp, errbuf)) == NULL)
31592676Seschrock 			return (-1);
31602676Seschrock 
31615094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
31622676Seschrock 			nvlist_free(props);
31632676Seschrock 			return (-1);
31642676Seschrock 		}
31652676Seschrock 
31662676Seschrock 		nvlist_free(props);
31672676Seschrock 	}
3168789Sahrens 
3169789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
31702676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
31714543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3172789Sahrens 
31732676Seschrock 	zcmd_free_nvlists(&zc);
31742676Seschrock 
3175789Sahrens 	if (ret != 0) {
3176789Sahrens 		switch (errno) {
3177789Sahrens 
3178789Sahrens 		case ENOENT:
3179789Sahrens 			/*
3180789Sahrens 			 * The parent doesn't exist.  We should have caught this
3181789Sahrens 			 * above, but there may a race condition that has since
3182789Sahrens 			 * destroyed the parent.
3183789Sahrens 			 *
3184789Sahrens 			 * At this point, we don't know whether it's the source
3185789Sahrens 			 * that doesn't exist anymore, or whether the target
3186789Sahrens 			 * dataset doesn't exist.
3187789Sahrens 			 */
31882082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31892082Seschrock 			    "no such parent '%s'"), parent);
31902082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
31912082Seschrock 
31922082Seschrock 		case EXDEV:
31932082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31942082Seschrock 			    "source and target pools differ"));
31952082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
31962082Seschrock 			    errbuf));
31972082Seschrock 
31982082Seschrock 		default:
31992082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32002082Seschrock 			    errbuf));
32012082Seschrock 		}
32022676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32032082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32042082Seschrock 	}
32052082Seschrock 
32062082Seschrock 	return (ret);
32072082Seschrock }
32082082Seschrock 
32092082Seschrock typedef struct promote_data {
32102082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32112082Seschrock 	const char *cb_target;
32122082Seschrock 	const char *cb_errbuf;
32132082Seschrock 	uint64_t cb_pivot_txg;
32142082Seschrock } promote_data_t;
32152082Seschrock 
32162082Seschrock static int
32172082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32182082Seschrock {
32192082Seschrock 	promote_data_t *pd = data;
32202082Seschrock 	zfs_handle_t *szhp;
32212082Seschrock 	char snapname[MAXPATHLEN];
32223265Sahrens 	int rv = 0;
32232082Seschrock 
32242082Seschrock 	/* We don't care about snapshots after the pivot point */
32253265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32263265Sahrens 		zfs_close(zhp);
32272082Seschrock 		return (0);
32283265Sahrens 	}
32292082Seschrock 
32302417Sahrens 	/* Remove the device link if it's a zvol. */
32312676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32322417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32332082Seschrock 
32342082Seschrock 	/* Check for conflicting names */
32352676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32362676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
32372082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
32382082Seschrock 	if (szhp != NULL) {
32392082Seschrock 		zfs_close(szhp);
32402082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32412082Seschrock 		    "snapshot name '%s' from origin \n"
32422082Seschrock 		    "conflicts with '%s' from target"),
32432082Seschrock 		    zhp->zfs_name, snapname);
32443265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
32452082Seschrock 	}
32463265Sahrens 	zfs_close(zhp);
32473265Sahrens 	return (rv);
32482082Seschrock }
32492082Seschrock 
32502417Sahrens static int
32512417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
32522417Sahrens {
32532417Sahrens 	promote_data_t *pd = data;
32542417Sahrens 
32552417Sahrens 	/* We don't care about snapshots after the pivot point */
32563265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
32573265Sahrens 		/* Create the device link if it's a zvol. */
32583265Sahrens 		if (ZFS_IS_VOLUME(zhp))
32593265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
32603265Sahrens 	}
32613265Sahrens 
32623265Sahrens 	zfs_close(zhp);
32632417Sahrens 	return (0);
32642417Sahrens }
32652417Sahrens 
32662082Seschrock /*
32672082Seschrock  * Promotes the given clone fs to be the clone parent.
32682082Seschrock  */
32692082Seschrock int
32702082Seschrock zfs_promote(zfs_handle_t *zhp)
32712082Seschrock {
32722082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
32732082Seschrock 	zfs_cmd_t zc = { 0 };
32742082Seschrock 	char parent[MAXPATHLEN];
32752082Seschrock 	char *cp;
32762082Seschrock 	int ret;
32772082Seschrock 	zfs_handle_t *pzhp;
32782082Seschrock 	promote_data_t pd;
32792082Seschrock 	char errbuf[1024];
32802082Seschrock 
32812082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32822082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
32832082Seschrock 
32842082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
32852082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32862082Seschrock 		    "snapshots can not be promoted"));
32872082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
32882082Seschrock 	}
32892082Seschrock 
32905367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
32912082Seschrock 	if (parent[0] == '\0') {
32922082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32932082Seschrock 		    "not a cloned filesystem"));
32942082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
32952082Seschrock 	}
32962082Seschrock 	cp = strchr(parent, '@');
32972082Seschrock 	*cp = '\0';
32982082Seschrock 
32992082Seschrock 	/* Walk the snapshots we will be moving */
33005367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
33012082Seschrock 	if (pzhp == NULL)
33022082Seschrock 		return (-1);
33032082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33042082Seschrock 	zfs_close(pzhp);
33052082Seschrock 	pd.cb_target = zhp->zfs_name;
33062082Seschrock 	pd.cb_errbuf = errbuf;
33075094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
33082082Seschrock 	if (pzhp == NULL)
33092082Seschrock 		return (-1);
33102082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33112082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33122082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33132417Sahrens 	if (ret != 0) {
33142417Sahrens 		zfs_close(pzhp);
33152082Seschrock 		return (-1);
33162417Sahrens 	}
33172082Seschrock 
33182082Seschrock 	/* issue the ioctl */
33195367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
33202676Seschrock 	    sizeof (zc.zc_value));
33212082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33224543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33232082Seschrock 
33242082Seschrock 	if (ret != 0) {
33252417Sahrens 		int save_errno = errno;
33262417Sahrens 
33272417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33282417Sahrens 		zfs_close(pzhp);
33292417Sahrens 
33302417Sahrens 		switch (save_errno) {
3331789Sahrens 		case EEXIST:
3332789Sahrens 			/*
33332082Seschrock 			 * There is a conflicting snapshot name.  We
33342082Seschrock 			 * should have caught this above, but they could
33352082Seschrock 			 * have renamed something in the mean time.
3336789Sahrens 			 */
33372082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33382082Seschrock 			    "conflicting snapshot name from parent '%s'"),
33392082Seschrock 			    parent);
33402082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3341789Sahrens 
3342789Sahrens 		default:
33432417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3344789Sahrens 		}
33452417Sahrens 	} else {
33462417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3347789Sahrens 	}
3348789Sahrens 
33492417Sahrens 	zfs_close(pzhp);
3350789Sahrens 	return (ret);
3351789Sahrens }
3352789Sahrens 
33534007Smmusante struct createdata {
33544007Smmusante 	const char *cd_snapname;
33554007Smmusante 	int cd_ifexists;
33564007Smmusante };
33574007Smmusante 
33582199Sahrens static int
33592199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
33602199Sahrens {
33614007Smmusante 	struct createdata *cd = arg;
33622676Seschrock 	int ret;
33632199Sahrens 
33642199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33652199Sahrens 		char name[MAXPATHLEN];
33662199Sahrens 
33672676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
33682676Seschrock 		(void) strlcat(name, "@", sizeof (name));
33694007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
33704007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
33714007Smmusante 		    cd->cd_ifexists);
33722199Sahrens 		/*
33732199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
33742199Sahrens 		 * return an error, because then we wouldn't visit all
33752199Sahrens 		 * the volumes.
33762199Sahrens 		 */
33772199Sahrens 	}
33782676Seschrock 
33794007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
33802676Seschrock 
33812676Seschrock 	zfs_close(zhp);
33822676Seschrock 
33832676Seschrock 	return (ret);
33842199Sahrens }
33852199Sahrens 
3386789Sahrens /*
33873504Sahl  * Takes a snapshot of the given dataset.
3388789Sahrens  */
3389789Sahrens int
33902199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3391789Sahrens {
3392789Sahrens 	const char *delim;
3393789Sahrens 	char *parent;
3394789Sahrens 	zfs_handle_t *zhp;
3395789Sahrens 	zfs_cmd_t zc = { 0 };
3396789Sahrens 	int ret;
33972082Seschrock 	char errbuf[1024];
33982082Seschrock 
33992082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34002082Seschrock 	    "cannot snapshot '%s'"), path);
34012082Seschrock 
34022082Seschrock 	/* validate the target name */
34035326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
34042082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3405789Sahrens 
3406789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34072199Sahrens 	delim = strchr(path, '@');
34082082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
34092082Seschrock 		return (-1);
3410789Sahrens 	(void) strncpy(parent, path, delim - path);
3411789Sahrens 	parent[delim - path] = '\0';
3412789Sahrens 
34132082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3414789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3415789Sahrens 		free(parent);
3416789Sahrens 		return (-1);
3417789Sahrens 	}
3418789Sahrens 
34192199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34202676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34214543Smarks 	if (ZFS_IS_VOLUME(zhp))
34224543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34234543Smarks 	else
34244543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34252199Sahrens 	zc.zc_cookie = recursive;
34264543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
34272199Sahrens 
34282199Sahrens 	/*
34292199Sahrens 	 * if it was recursive, the one that actually failed will be in
34302199Sahrens 	 * zc.zc_name.
34312199Sahrens 	 */
34324543Smarks 	if (ret != 0)
34334543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34344543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
34354543Smarks 
34362199Sahrens 	if (ret == 0 && recursive) {
34374007Smmusante 		struct createdata cd;
34384007Smmusante 
34394007Smmusante 		cd.cd_snapname = delim + 1;
34404007Smmusante 		cd.cd_ifexists = B_FALSE;
34414007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
34422199Sahrens 	}
3443789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
34442082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
34452199Sahrens 		if (ret != 0) {
34464543Smarks 			(void) zfs_standard_error(hdl, errno,
34474543Smarks 			    dgettext(TEXT_DOMAIN,
34484543Smarks 			    "Volume successfully snapshotted, but device links "
34494543Smarks 			    "were not created"));
34504543Smarks 			free(parent);
34514543Smarks 			zfs_close(zhp);
34524543Smarks 			return (-1);
34532199Sahrens 		}
3454789Sahrens 	}
3455789Sahrens 
34562082Seschrock 	if (ret != 0)
34572082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3458789Sahrens 
3459789Sahrens 	free(parent);
3460789Sahrens 	zfs_close(zhp);
3461789Sahrens 
3462789Sahrens 	return (ret);
3463789Sahrens }
3464789Sahrens 
3465789Sahrens /*
34661294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
34671294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
34681294Slling  * is a dependent and we should just destroy it without checking the transaction
34691294Slling  * group.
3470789Sahrens  */
34711294Slling typedef struct rollback_data {
34721294Slling 	const char	*cb_target;		/* the snapshot */
34731294Slling 	uint64_t	cb_create;		/* creation time reference */
34745749Sahrens 	boolean_t	cb_error;
34752082Seschrock 	boolean_t	cb_dependent;
34765749Sahrens 	boolean_t	cb_force;
34771294Slling } rollback_data_t;
34781294Slling 
34791294Slling static int
34801294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
34811294Slling {
34821294Slling 	rollback_data_t *cbp = data;
34831294Slling 
34841294Slling 	if (!cbp->cb_dependent) {
34851294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
34861294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
34871294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
34881294Slling 		    cbp->cb_create) {
34894543Smarks 			char *logstr;
34901294Slling 
34912082Seschrock 			cbp->cb_dependent = B_TRUE;
34925446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
34935446Sahrens 			    rollback_destroy, cbp);
34942082Seschrock 			cbp->cb_dependent = B_FALSE;
34951294Slling 
34964543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
34974543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
34985446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
34994543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
35001294Slling 		}
35011294Slling 	} else {
35025749Sahrens 		/* We must destroy this clone; first unmount it */
35035749Sahrens 		prop_changelist_t *clp;
35045749Sahrens 
35055749Sahrens 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
35065749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
35075749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
35085749Sahrens 			cbp->cb_error = B_TRUE;
35095749Sahrens 			zfs_close(zhp);
35105749Sahrens 			return (0);
35115749Sahrens 		}
35125749Sahrens 		if (zfs_destroy(zhp) != 0)
35135749Sahrens 			cbp->cb_error = B_TRUE;
35145749Sahrens 		else
35155749Sahrens 			changelist_remove(clp, zhp->zfs_name);
35165751Sahrens 		(void) changelist_postfix(clp);
35175749Sahrens 		changelist_free(clp);
35181294Slling 	}
35191294Slling 
35201294Slling 	zfs_close(zhp);
35211294Slling 	return (0);
35221294Slling }
35231294Slling 
35241294Slling /*
35255446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35265446Sahrens  * data changes since then and making it the active dataset.
35275446Sahrens  *
35285446Sahrens  * Any snapshots more recent than the target are destroyed, along with
35295446Sahrens  * their dependents.
35301294Slling  */
35315446Sahrens int
35325749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3533789Sahrens {
35345446Sahrens 	rollback_data_t cb = { 0 };
35355446Sahrens 	int err;
3536789Sahrens 	zfs_cmd_t zc = { 0 };
35375713Srm160521 	boolean_t restore_resv = 0;
35385713Srm160521 	uint64_t old_volsize, new_volsize;
35395713Srm160521 	zfs_prop_t resv_prop;
3540789Sahrens 
3541789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3542789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3543789Sahrens 
35445446Sahrens 	/*
35455446Sahrens 	 * Destroy all recent snapshots and its dependends.
35465446Sahrens 	 */
35475749Sahrens 	cb.cb_force = force;
35485446Sahrens 	cb.cb_target = snap->zfs_name;
35495446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
35505446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
35515446Sahrens 
35525749Sahrens 	if (cb.cb_error)
35535749Sahrens 		return (-1);
35545446Sahrens 
35555446Sahrens 	/*
35565446Sahrens 	 * Now that we have verified that the snapshot is the latest,
35575446Sahrens 	 * rollback to the given snapshot.
35585446Sahrens 	 */
35595446Sahrens 
35605713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
35615713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
35625713Srm160521 			return (-1);
35635713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
35645713Srm160521 			return (-1);
35655713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
35665713Srm160521 		restore_resv =
35675713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
35685713Srm160521 	}
3569789Sahrens 
3570789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3571789Sahrens 
35722676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3573789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3574789Sahrens 	else
3575789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3576789Sahrens 
3577789Sahrens 	/*
35785446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
35795446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
35805446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
35815446Sahrens 	 * an unlikely race condition where the user has taken a
35825446Sahrens 	 * snapshot since we verified that this was the most recent.
35835713Srm160521 	 *
3584789Sahrens 	 */
35855446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
35863237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
35872082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
35882082Seschrock 		    zhp->zfs_name);
35895717Srm160521 		return (err);
35905717Srm160521 	}
35915713Srm160521 
35925713Srm160521 	/*
35935713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
35945713Srm160521 	 * rollback reservation and the volsize has changed then set
35955713Srm160521 	 * the reservation property to the post-rollback volsize.
35965713Srm160521 	 * Make a new handle since the rollback closed the dataset.
35975713Srm160521 	 */
35985717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
35995717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
36005717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
36015717Srm160521 			zfs_close(zhp);
36025713Srm160521 			return (err);
36035717Srm160521 		}
36045713Srm160521 		if (restore_resv) {
36055713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36065713Srm160521 			if (old_volsize != new_volsize)
36075717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
36085717Srm160521 				    new_volsize);
36095713Srm160521 		}
36105713Srm160521 		zfs_close(zhp);
3611789Sahrens 	}
36125446Sahrens 	return (err);
36131294Slling }
36141294Slling 
36151294Slling /*
3616789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3617789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3618789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3619789Sahrens  * libzfs_graph.c.
3620789Sahrens  */
3621789Sahrens int
36222474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36232474Seschrock     zfs_iter_f func, void *data)
3624789Sahrens {
3625789Sahrens 	char **dependents;
3626789Sahrens 	size_t count;
3627789Sahrens 	int i;
3628789Sahrens 	zfs_handle_t *child;
3629789Sahrens 	int ret = 0;
3630789Sahrens 
36312474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
36322474Seschrock 	    &dependents, &count) != 0)
36332474Seschrock 		return (-1);
36342474Seschrock 
3635789Sahrens 	for (i = 0; i < count; i++) {
36362082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
36372082Seschrock 		    dependents[i])) == NULL)
3638789Sahrens 			continue;
3639789Sahrens 
3640789Sahrens 		if ((ret = func(child, data)) != 0)
3641789Sahrens 			break;
3642789Sahrens 	}
3643789Sahrens 
3644789Sahrens 	for (i = 0; i < count; i++)
3645789Sahrens 		free(dependents[i]);
3646789Sahrens 	free(dependents);
3647789Sahrens 
3648789Sahrens 	return (ret);
3649789Sahrens }
3650789Sahrens 
3651789Sahrens /*
3652789Sahrens  * Renames the given dataset.
3653789Sahrens  */
3654789Sahrens int
36554490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3656789Sahrens {
3657789Sahrens 	int ret;
3658789Sahrens 	zfs_cmd_t zc = { 0 };
3659789Sahrens 	char *delim;
36604007Smmusante 	prop_changelist_t *cl = NULL;
36614007Smmusante 	zfs_handle_t *zhrp = NULL;
36624007Smmusante 	char *parentname = NULL;
3663789Sahrens 	char parent[ZFS_MAXNAMELEN];
36642082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
36652082Seschrock 	char errbuf[1024];
3666789Sahrens 
3667789Sahrens 	/* if we have the same exact name, just return success */
3668789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3669789Sahrens 		return (0);
3670789Sahrens 
36712082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36722082Seschrock 	    "cannot rename to '%s'"), target);
36732082Seschrock 
3674789Sahrens 	/*
3675789Sahrens 	 * Make sure the target name is valid
3676789Sahrens 	 */
3677789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
36782665Snd150628 		if ((strchr(target, '@') == NULL) ||
36792665Snd150628 		    *target == '@') {
36802665Snd150628 			/*
36812665Snd150628 			 * Snapshot target name is abbreviated,
36822665Snd150628 			 * reconstruct full dataset name
36832665Snd150628 			 */
36842665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
36852665Snd150628 			    sizeof (parent));
36862665Snd150628 			delim = strchr(parent, '@');
36872665Snd150628 			if (strchr(target, '@') == NULL)
36882665Snd150628 				*(++delim) = '\0';
36892665Snd150628 			else
36902665Snd150628 				*delim = '\0';
36912665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
36922665Snd150628 			target = parent;
36932665Snd150628 		} else {
36942665Snd150628 			/*
36952665Snd150628 			 * Make sure we're renaming within the same dataset.
36962665Snd150628 			 */
36972665Snd150628 			delim = strchr(target, '@');
36982665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
36992665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
37002665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37012665Snd150628 				    "snapshots must be part of same "
37022665Snd150628 				    "dataset"));
37032665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
37043912Slling 				    errbuf));
37052665Snd150628 			}
3706789Sahrens 		}
37075326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37082665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3709789Sahrens 	} else {
37104007Smmusante 		if (recursive) {
37114007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37124007Smmusante 			    "recursive rename must be a snapshot"));
37134007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
37144007Smmusante 		}
37154007Smmusante 
37165326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37172665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37182676Seschrock 		uint64_t unused;
37192676Seschrock 
3720789Sahrens 		/* validate parents */
37214490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3722789Sahrens 			return (-1);
3723789Sahrens 
3724789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3725789Sahrens 
3726789Sahrens 		/* make sure we're in the same pool */
3727789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3728789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3729789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
37302082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37312082Seschrock 			    "datasets must be within same pool"));
37322082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3733789Sahrens 		}
37342440Snd150628 
37352440Snd150628 		/* new name cannot be a child of the current dataset name */
37362440Snd150628 		if (strncmp(parent, zhp->zfs_name,
37373912Slling 		    strlen(zhp->zfs_name)) == 0) {
37382440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37392440Snd150628 			    "New dataset name cannot be a descendent of "
37402440Snd150628 			    "current dataset name"));
37412440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37422440Snd150628 		}
3743789Sahrens 	}
3744789Sahrens 
37452082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
37462082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
37472082Seschrock 
3748789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3749789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
37502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37512082Seschrock 		    "dataset is used in a non-global zone"));
37522082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3753789Sahrens 	}
3754789Sahrens 
37554007Smmusante 	if (recursive) {
37564007Smmusante 		struct destroydata dd;
37574007Smmusante 
37584183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
37594183Smmusante 		if (parentname == NULL) {
37604183Smmusante 			ret = -1;
37614183Smmusante 			goto error;
37624183Smmusante 		}
37634007Smmusante 		delim = strchr(parentname, '@');
37644007Smmusante 		*delim = '\0';
37655094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
37664007Smmusante 		if (zhrp == NULL) {
37674183Smmusante 			ret = -1;
37684183Smmusante 			goto error;
37694007Smmusante 		}
37704007Smmusante 
37714007Smmusante 		dd.snapname = delim + 1;
37724007Smmusante 		dd.gotone = B_FALSE;
37734183Smmusante 		dd.closezhp = B_TRUE;
37744007Smmusante 
37754007Smmusante 		/* We remove any zvol links prior to renaming them */
37764007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
37774007Smmusante 		if (ret) {
37784007Smmusante 			goto error;
37794007Smmusante 		}
37804007Smmusante 	} else {
37814007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
37824007Smmusante 			return (-1);
37834007Smmusante 
37844007Smmusante 		if (changelist_haszonedchild(cl)) {
37854007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37864007Smmusante 			    "child dataset with inherited mountpoint is used "
37874007Smmusante 			    "in a non-global zone"));
37884007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
37894007Smmusante 			goto error;
37904007Smmusante 		}
37914007Smmusante 
37924007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
37934007Smmusante 			goto error;
3794789Sahrens 	}
3795789Sahrens 
37962676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3797789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3798789Sahrens 	else
3799789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3800789Sahrens 
38012665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
38022676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
38032665Snd150628 
38044007Smmusante 	zc.zc_cookie = recursive;
38054007Smmusante 
38064543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
38074007Smmusante 		/*
38084007Smmusante 		 * if it was recursive, the one that actually failed will
38094007Smmusante 		 * be in zc.zc_name
38104007Smmusante 		 */
38114007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38125367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
38134007Smmusante 
38144007Smmusante 		if (recursive && errno == EEXIST) {
38154007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38164007Smmusante 			    "a child dataset already has a snapshot "
38174007Smmusante 			    "with the new name"));
38184801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38194007Smmusante 		} else {
38204007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
38214007Smmusante 		}
3822789Sahrens 
3823789Sahrens 		/*
3824789Sahrens 		 * On failure, we still want to remount any filesystems that
3825789Sahrens 		 * were previously mounted, so we don't alter the system state.
3826789Sahrens 		 */
38274007Smmusante 		if (recursive) {
38284007Smmusante 			struct createdata cd;
38294007Smmusante 
38304007Smmusante 			/* only create links for datasets that had existed */
38314007Smmusante 			cd.cd_snapname = delim + 1;
38324007Smmusante 			cd.cd_ifexists = B_TRUE;
38334007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38344007Smmusante 			    &cd);
38354007Smmusante 		} else {
38364007Smmusante 			(void) changelist_postfix(cl);
38374007Smmusante 		}
3838789Sahrens 	} else {
38394007Smmusante 		if (recursive) {
38404007Smmusante 			struct createdata cd;
38414007Smmusante 
38424007Smmusante 			/* only create links for datasets that had existed */
38434007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
38444007Smmusante 			cd.cd_ifexists = B_TRUE;
38454007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38464007Smmusante 			    &cd);
38474007Smmusante 		} else {
38484007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
38494007Smmusante 			ret = changelist_postfix(cl);
38504007Smmusante 		}
3851789Sahrens 	}
3852789Sahrens 
3853789Sahrens error:
38544007Smmusante 	if (parentname) {
38554007Smmusante 		free(parentname);
38564007Smmusante 	}
38574007Smmusante 	if (zhrp) {
38584007Smmusante 		zfs_close(zhrp);
38594007Smmusante 	}
38604007Smmusante 	if (cl) {
38614007Smmusante 		changelist_free(cl);
38624007Smmusante 	}
3863789Sahrens 	return (ret);
3864789Sahrens }
3865789Sahrens 
3866789Sahrens /*
3867789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3868789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3869789Sahrens  */
3870789Sahrens int
38712082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3872789Sahrens {
38734007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
38744007Smmusante }
38754007Smmusante 
38764007Smmusante static int
38774007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
38784007Smmusante {
3879789Sahrens 	zfs_cmd_t zc = { 0 };
38802082Seschrock 	di_devlink_handle_t dhdl;
38814543Smarks 	priv_set_t *priv_effective;
38824543Smarks 	int privileged;
3883789Sahrens 
3884789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3885789Sahrens 
3886789Sahrens 	/*
3887789Sahrens 	 * Issue the appropriate ioctl.
3888789Sahrens 	 */
38892082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3890789Sahrens 		switch (errno) {
3891789Sahrens 		case EEXIST:
3892789Sahrens 			/*
3893789Sahrens 			 * Silently ignore the case where the link already
3894789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3895789Sahrens 			 * times without errors.
3896789Sahrens 			 */
3897789Sahrens 			return (0);
3898789Sahrens 
38994007Smmusante 		case ENOENT:
39004007Smmusante 			/*
39014007Smmusante 			 * Dataset does not exist in the kernel.  If we
39024007Smmusante 			 * don't care (see zfs_rename), then ignore the
39034007Smmusante 			 * error quietly.
39044007Smmusante 			 */
39054007Smmusante 			if (ifexists) {
39064007Smmusante 				return (0);
39074007Smmusante 			}
39084007Smmusante 
39094007Smmusante 			/* FALLTHROUGH */
39104007Smmusante 
3911789Sahrens 		default:
39123237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39132082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39142082Seschrock 			    "for '%s'"), dataset));
3915789Sahrens 		}
3916789Sahrens 	}
3917789Sahrens 
3918789Sahrens 	/*
39194543Smarks 	 * If privileged call devfsadm and wait for the links to
39204543Smarks 	 * magically appear.
39214543Smarks 	 * Otherwise, print out an informational message.
3922789Sahrens 	 */
39234543Smarks 
39244543Smarks 	priv_effective = priv_allocset();
39254543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
39264543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
39274543Smarks 	priv_freeset(priv_effective);
39284543Smarks 
39294543Smarks 	if (privileged) {
39304543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
39314543Smarks 		    DI_MAKE_LINK)) == NULL) {
39324543Smarks 			zfs_error_aux(hdl, strerror(errno));
39334543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
39344543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39354543Smarks 			    "for '%s'"), dataset);
39364543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
39374543Smarks 			return (-1);
39384543Smarks 		} else {
39394543Smarks 			(void) di_devlink_fini(&dhdl);
39404543Smarks 		}
3941789Sahrens 	} else {
39424543Smarks 		char pathname[MAXPATHLEN];
39434543Smarks 		struct stat64 statbuf;
39444543Smarks 		int i;
39454543Smarks 
39464543Smarks #define	MAX_WAIT	10
39474543Smarks 
39484543Smarks 		/*
39494543Smarks 		 * This is the poor mans way of waiting for the link
39504543Smarks 		 * to show up.  If after 10 seconds we still don't
39514543Smarks 		 * have it, then print out a message.
39524543Smarks 		 */
39534543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
39544543Smarks 		    dataset);
39554543Smarks 
39564543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
39574543Smarks 			if (stat64(pathname, &statbuf) == 0)
39584543Smarks 				break;
39594543Smarks 			(void) sleep(1);
39604543Smarks 		}
39614543Smarks 		if (i == MAX_WAIT)
39624543Smarks 			(void) printf(gettext("%s may not be immediately "
39634543Smarks 			    "available\n"), pathname);
3964789Sahrens 	}
3965789Sahrens 
3966789Sahrens 	return (0);
3967789Sahrens }
3968789Sahrens 
3969789Sahrens /*
3970789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3971789Sahrens  */
3972789Sahrens int
39732082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3974789Sahrens {
3975789Sahrens 	zfs_cmd_t zc = { 0 };
3976789Sahrens 
3977789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3978789Sahrens 
39792082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3980789Sahrens 		switch (errno) {
3981789Sahrens 		case ENXIO:
3982789Sahrens 			/*
3983789Sahrens 			 * Silently ignore the case where the link no longer
3984789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3985789Sahrens 			 * times without errors.
3986789Sahrens 			 */
3987789Sahrens 			return (0);
3988789Sahrens 
3989789Sahrens 		default:
39903237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39912082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
39922082Seschrock 			    "links for '%s'"), dataset));
3993789Sahrens 		}
3994789Sahrens 	}
3995789Sahrens 
3996789Sahrens 	return (0);
3997789Sahrens }
39982676Seschrock 
39992676Seschrock nvlist_t *
40002676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
40012676Seschrock {
40022676Seschrock 	return (zhp->zfs_user_props);
40032676Seschrock }
40042676Seschrock 
40052676Seschrock /*
40063912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
40073912Slling  * display, and their maximum widths.  This does two main things:
40083912Slling  *
40093912Slling  *      - If this is a list of all properties, then expand the list to include
40103912Slling  *        all native properties, and set a flag so that for each dataset we look
40113912Slling  *        for new unique user properties and add them to the list.
40123912Slling  *
40133912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
40143912Slling  *        so that we can size the column appropriately.
40153912Slling  */
40163912Slling int
40175094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
40183912Slling {
40193912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40205094Slling 	zprop_list_t *entry;
40215094Slling 	zprop_list_t **last, **start;
40223912Slling 	nvlist_t *userprops, *propval;
40233912Slling 	nvpair_t *elem;
40243912Slling 	char *strval;
40253912Slling 	char buf[ZFS_MAXPROPLEN];
40263912Slling 
40275094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
40283912Slling 		return (-1);
40292676Seschrock 
40302676Seschrock 	userprops = zfs_get_user_props(zhp);
40312676Seschrock 
40322676Seschrock 	entry = *plp;
40332676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
40342676Seschrock 		/*
40352676Seschrock 		 * Go through and add any user properties as necessary.  We
40362676Seschrock 		 * start by incrementing our list pointer to the first
40372676Seschrock 		 * non-native property.
40382676Seschrock 		 */
40392676Seschrock 		start = plp;
40402676Seschrock 		while (*start != NULL) {
40415094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
40422676Seschrock 				break;
40432676Seschrock 			start = &(*start)->pl_next;
40442676Seschrock 		}
40452676Seschrock 
40462676Seschrock 		elem = NULL;
40472676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
40482676Seschrock 			/*
40492676Seschrock 			 * See if we've already found this property in our list.
40502676Seschrock 			 */
40512676Seschrock 			for (last = start; *last != NULL;
40522676Seschrock 			    last = &(*last)->pl_next) {
40532676Seschrock 				if (strcmp((*last)->pl_user_prop,
40542676Seschrock 				    nvpair_name(elem)) == 0)
40552676Seschrock 					break;
40562676Seschrock 			}
40572676Seschrock 
40582676Seschrock 			if (*last == NULL) {
40592676Seschrock 				if ((entry = zfs_alloc(hdl,
40605094Slling 				    sizeof (zprop_list_t))) == NULL ||
40612676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
40622676Seschrock 				    nvpair_name(elem)))) == NULL) {
40632676Seschrock 					free(entry);
40642676Seschrock 					return (-1);
40652676Seschrock 				}
40662676Seschrock 
40675094Slling 				entry->pl_prop = ZPROP_INVAL;
40682676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
40692676Seschrock 				entry->pl_all = B_TRUE;
40702676Seschrock 				*last = entry;
40712676Seschrock 			}
40722676Seschrock 		}
40732676Seschrock 	}
40742676Seschrock 
40752676Seschrock 	/*
40762676Seschrock 	 * Now go through and check the width of any non-fixed columns
40772676Seschrock 	 */
40782676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
40792676Seschrock 		if (entry->pl_fixed)
40802676Seschrock 			continue;
40812676Seschrock 
40825094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
40832676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
40842676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
40852676Seschrock 				if (strlen(buf) > entry->pl_width)
40862676Seschrock 					entry->pl_width = strlen(buf);
40872676Seschrock 			}
40882676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
40892676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
40902676Seschrock 			verify(nvlist_lookup_string(propval,
40915094Slling 			    ZPROP_VALUE, &strval) == 0);
40922676Seschrock 			if (strlen(strval) > entry->pl_width)
40932676Seschrock 				entry->pl_width = strlen(strval);
40942676Seschrock 		}
40952676Seschrock 	}
40962676Seschrock 
40972676Seschrock 	return (0);
40982676Seschrock }
40994543Smarks 
41004543Smarks int
41014543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
41024543Smarks {
41034543Smarks 	zfs_cmd_t zc = { 0 };
41044543Smarks 	nvlist_t *nvp;
41054543Smarks 	gid_t gid;
41064543Smarks 	uid_t uid;
41074543Smarks 	const gid_t *groups;
41084543Smarks 	int group_cnt;
41094543Smarks 	int error;
41104543Smarks 
41114543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
41124543Smarks 		return (no_memory(hdl));
41134543Smarks 
41144543Smarks 	uid = ucred_geteuid(cred);
41154543Smarks 	gid = ucred_getegid(cred);
41164543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
41174543Smarks 
41184543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
41194543Smarks 		return (1);
41204543Smarks 
41214543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
41224543Smarks 		nvlist_free(nvp);
41234543Smarks 		return (1);
41244543Smarks 	}
41254543Smarks 
41264543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
41274543Smarks 		nvlist_free(nvp);
41284543Smarks 		return (1);
41294543Smarks 	}
41304543Smarks 
41314543Smarks 	if (nvlist_add_uint32_array(nvp,
41324543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
41334543Smarks 		nvlist_free(nvp);
41344543Smarks 		return (1);
41354543Smarks 	}
41364543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
41374543Smarks 
41385094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
41394543Smarks 		return (-1);
41404543Smarks 
41414543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
41424543Smarks 	nvlist_free(nvp);
41434543Smarks 	return (error);
41444543Smarks }
41454543Smarks 
41464543Smarks int
41474543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
41485331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
41494543Smarks {
41504543Smarks 	zfs_cmd_t zc = { 0 };
41514543Smarks 	int error;
41524543Smarks 
41534543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
41544543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
41554543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
41564543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
41575331Samw 	zc.zc_share.z_sharetype = operation;
41584543Smarks 	zc.zc_share.z_sharemax = sharemax;
41594543Smarks 
41604543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
41614543Smarks 	return (error);
41624543Smarks }
4163