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 /*
238802SSanjeev.Bagewadi@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <assert.h>
28789Sahrens #include <ctype.h>
29789Sahrens #include <errno.h>
30789Sahrens #include <libdevinfo.h>
31789Sahrens #include <libintl.h>
32789Sahrens #include <math.h>
33789Sahrens #include <stdio.h>
34789Sahrens #include <stdlib.h>
35789Sahrens #include <strings.h>
36789Sahrens #include <unistd.h>
375367Sahrens #include <stddef.h>
38789Sahrens #include <zone.h>
392082Seschrock #include <fcntl.h>
40789Sahrens #include <sys/mntent.h>
411294Slling #include <sys/mount.h>
424543Smarks #include <sys/avl.h>
434543Smarks #include <priv.h>
444543Smarks #include <pwd.h>
454543Smarks #include <grp.h>
464543Smarks #include <stddef.h>
474543Smarks #include <ucred.h>
48789Sahrens 
49789Sahrens #include <sys/spa.h>
502676Seschrock #include <sys/zap.h>
51789Sahrens #include <libzfs.h>
52789Sahrens 
53789Sahrens #include "zfs_namecheck.h"
54789Sahrens #include "zfs_prop.h"
55789Sahrens #include "libzfs_impl.h"
564543Smarks #include "zfs_deleg.h"
57789Sahrens 
584007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
594007Smmusante 
60789Sahrens /*
61789Sahrens  * Given a single type (not a mask of types), return the type in a human
62789Sahrens  * readable form.
63789Sahrens  */
64789Sahrens const char *
65789Sahrens zfs_type_to_name(zfs_type_t type)
66789Sahrens {
67789Sahrens 	switch (type) {
68789Sahrens 	case ZFS_TYPE_FILESYSTEM:
69789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
70789Sahrens 	case ZFS_TYPE_SNAPSHOT:
71789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
72789Sahrens 	case ZFS_TYPE_VOLUME:
73789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
74789Sahrens 	}
75789Sahrens 
76789Sahrens 	return (NULL);
77789Sahrens }
78789Sahrens 
79789Sahrens /*
80789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
81789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
82789Sahrens  * We guess what the type would have been based on the path and the mask of
83789Sahrens  * acceptable types.
84789Sahrens  */
85789Sahrens static const char *
86789Sahrens path_to_str(const char *path, int types)
87789Sahrens {
88789Sahrens 	/*
89789Sahrens 	 * When given a single type, always report the exact type.
90789Sahrens 	 */
91789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
92789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
93789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
94789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
95789Sahrens 	if (types == ZFS_TYPE_VOLUME)
96789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
97789Sahrens 
98789Sahrens 	/*
99789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
100789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
101789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
102789Sahrens 	 * snapshot attribute and try again.
103789Sahrens 	 */
104789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
105789Sahrens 		if (strchr(path, '@') != NULL)
106789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
107789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
108789Sahrens 	}
109789Sahrens 
110789Sahrens 	/*
111789Sahrens 	 * The user has requested either filesystems or volumes.
112789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
113789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
114789Sahrens 	 */
115789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
116789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
117789Sahrens 
118789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
119789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
120789Sahrens }
121789Sahrens 
122789Sahrens /*
123789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
124789Sahrens  * provide a more meaningful error message.  We place a more useful message in
125789Sahrens  * 'buf' detailing exactly why the name was not valid.
126789Sahrens  */
127789Sahrens static int
1285326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1295326Sek110237     boolean_t modifying)
130789Sahrens {
131789Sahrens 	namecheck_err_t why;
132789Sahrens 	char what;
133789Sahrens 
134789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1352082Seschrock 		if (hdl != NULL) {
136789Sahrens 			switch (why) {
1371003Slling 			case NAME_ERR_TOOLONG:
1382082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1392082Seschrock 				    "name is too long"));
1401003Slling 				break;
1411003Slling 
142789Sahrens 			case NAME_ERR_LEADING_SLASH:
1432082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442082Seschrock 				    "leading slash in name"));
145789Sahrens 				break;
146789Sahrens 
147789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1482082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492082Seschrock 				    "empty component in name"));
150789Sahrens 				break;
151789Sahrens 
152789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1532082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542082Seschrock 				    "trailing slash in name"));
155789Sahrens 				break;
156789Sahrens 
157789Sahrens 			case NAME_ERR_INVALCHAR:
1582082Seschrock 				zfs_error_aux(hdl,
159789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1602082Seschrock 				    "'%c' in name"), what);
161789Sahrens 				break;
162789Sahrens 
163789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1642082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1652082Seschrock 				    "multiple '@' delimiters in name"));
166789Sahrens 				break;
1672856Snd150628 
1682856Snd150628 			case NAME_ERR_NOLETTER:
1692856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1702856Snd150628 				    "pool doesn't begin with a letter"));
1712856Snd150628 				break;
1722856Snd150628 
1732856Snd150628 			case NAME_ERR_RESERVED:
1742856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1752856Snd150628 				    "name is reserved"));
1762856Snd150628 				break;
1772856Snd150628 
1782856Snd150628 			case NAME_ERR_DISKLIKE:
1792856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1802856Snd150628 				    "reserved disk name"));
1812856Snd150628 				break;
182789Sahrens 			}
183789Sahrens 		}
184789Sahrens 
185789Sahrens 		return (0);
186789Sahrens 	}
187789Sahrens 
188789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1892082Seschrock 		if (hdl != NULL)
1902082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1912082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
192789Sahrens 		return (0);
193789Sahrens 	}
194789Sahrens 
1952199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1962199Sahrens 		if (hdl != NULL)
1972199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1983413Smmusante 			    "missing '@' delimiter in snapshot name"));
1992199Sahrens 		return (0);
2002199Sahrens 	}
2012199Sahrens 
2025326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2035326Sek110237 		if (hdl != NULL)
2045326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2055326Sek110237 			    "invalid character %c in name"), '%');
2065326Sek110237 		return (0);
2075326Sek110237 	}
2085326Sek110237 
2092082Seschrock 	return (-1);
210789Sahrens }
211789Sahrens 
212789Sahrens int
213789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
214789Sahrens {
2156423Sgw25295 	if (type == ZFS_TYPE_POOL)
2166423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2175326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
218789Sahrens }
219789Sahrens 
220789Sahrens /*
2212676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2222676Seschrock  * properties into a separate nvlist.
2232676Seschrock  */
2244217Seschrock static nvlist_t *
2254217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2262676Seschrock {
2272676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2282676Seschrock 	nvpair_t *elem;
2292676Seschrock 	nvlist_t *propval;
2304217Seschrock 	nvlist_t *nvl;
2314217Seschrock 
2324217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2334217Seschrock 		(void) no_memory(hdl);
2344217Seschrock 		return (NULL);
2354217Seschrock 	}
2362676Seschrock 
2372676Seschrock 	elem = NULL;
2384217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2392676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2402676Seschrock 			continue;
2412676Seschrock 
2422676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2434217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2444217Seschrock 			nvlist_free(nvl);
2454217Seschrock 			(void) no_memory(hdl);
2464217Seschrock 			return (NULL);
2474217Seschrock 		}
2482676Seschrock 	}
2492676Seschrock 
2504217Seschrock 	return (nvl);
2512676Seschrock }
2522676Seschrock 
2536865Srm160521 static zpool_handle_t *
2546865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2556865Srm160521 {
2566865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2576865Srm160521 	zpool_handle_t *zph;
2586865Srm160521 
2596865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2606865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2616865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2626865Srm160521 		hdl->libzfs_pool_handles = zph;
2636865Srm160521 	}
2646865Srm160521 	return (zph);
2656865Srm160521 }
2666865Srm160521 
2676865Srm160521 static zpool_handle_t *
2686865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2696865Srm160521 {
2706865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2716865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2726865Srm160521 
2736865Srm160521 	while ((zph != NULL) &&
2746865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2756865Srm160521 		zph = zph->zpool_next;
2766865Srm160521 	return (zph);
2776865Srm160521 }
2786865Srm160521 
2796865Srm160521 /*
2806865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2816865Srm160521  * If a handle to that pool already exists then that handle is returned.
2826865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2836865Srm160521  */
2846865Srm160521 static zpool_handle_t *
2856865Srm160521 zpool_handle(zfs_handle_t *zhp)
2866865Srm160521 {
2876865Srm160521 	char *pool_name;
2886865Srm160521 	int len;
2896865Srm160521 	zpool_handle_t *zph;
2906865Srm160521 
2916865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2926865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2936865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2946865Srm160521 
2956865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
2966865Srm160521 	if (zph == NULL)
2976865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
2986865Srm160521 
2996865Srm160521 	free(pool_name);
3006865Srm160521 	return (zph);
3016865Srm160521 }
3026865Srm160521 
3036865Srm160521 void
3046865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3056865Srm160521 {
3066865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3076865Srm160521 
3086865Srm160521 	while (zph != NULL) {
3096865Srm160521 		next = zph->zpool_next;
3106865Srm160521 		zpool_close(zph);
3116865Srm160521 		zph = next;
3126865Srm160521 	}
3136865Srm160521 	hdl->libzfs_pool_handles = NULL;
3146865Srm160521 }
3156865Srm160521 
3162676Seschrock /*
317789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
318789Sahrens  */
319789Sahrens static int
3208228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
321789Sahrens {
3222676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3238228SEric.Taylor@Sun.COM 
3248228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
3258228SEric.Taylor@Sun.COM 
3268228SEric.Taylor@Sun.COM 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3271356Seschrock 		if (errno == ENOMEM) {
3288228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
3292082Seschrock 				return (-1);
3302676Seschrock 			}
3311356Seschrock 		} else {
3321356Seschrock 			return (-1);
3331356Seschrock 		}
3341356Seschrock 	}
3358228SEric.Taylor@Sun.COM 	return (0);
3368228SEric.Taylor@Sun.COM }
3378228SEric.Taylor@Sun.COM 
3388228SEric.Taylor@Sun.COM static int
3398228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
3408228SEric.Taylor@Sun.COM {
3418228SEric.Taylor@Sun.COM 	nvlist_t *allprops, *userprops;
3428228SEric.Taylor@Sun.COM 
3438228SEric.Taylor@Sun.COM 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
3448228SEric.Taylor@Sun.COM 
3458228SEric.Taylor@Sun.COM 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
3462082Seschrock 		return (-1);
3472082Seschrock 	}
348789Sahrens 
3494217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3504217Seschrock 		nvlist_free(allprops);
3512676Seschrock 		return (-1);
3524217Seschrock 	}
3534217Seschrock 
3544217Seschrock 	nvlist_free(zhp->zfs_props);
3554217Seschrock 	nvlist_free(zhp->zfs_user_props);
3564217Seschrock 
3574217Seschrock 	zhp->zfs_props = allprops;
3584217Seschrock 	zhp->zfs_user_props = userprops;
3592082Seschrock 
360789Sahrens 	return (0);
361789Sahrens }
362789Sahrens 
3638228SEric.Taylor@Sun.COM static int
3648228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp)
3658228SEric.Taylor@Sun.COM {
3668228SEric.Taylor@Sun.COM 	int rc = 0;
3678228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
3688228SEric.Taylor@Sun.COM 
3698228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
3708228SEric.Taylor@Sun.COM 		return (-1);
3718228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) != 0)
3728228SEric.Taylor@Sun.COM 		rc = -1;
3738228SEric.Taylor@Sun.COM 	else if (put_stats_zhdl(zhp, &zc) != 0)
3748228SEric.Taylor@Sun.COM 		rc = -1;
3758228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
3768228SEric.Taylor@Sun.COM 	return (rc);
3778228SEric.Taylor@Sun.COM }
3788228SEric.Taylor@Sun.COM 
379789Sahrens /*
380789Sahrens  * Refresh the properties currently stored in the handle.
381789Sahrens  */
382789Sahrens void
383789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
384789Sahrens {
385789Sahrens 	(void) get_stats(zhp);
386789Sahrens }
387789Sahrens 
388789Sahrens /*
389789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
390789Sahrens  * zfs_iter_* to create child handles on the fly.
391789Sahrens  */
3928228SEric.Taylor@Sun.COM static int
3938228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
394789Sahrens {
3954543Smarks 	char *logstr;
3968228SEric.Taylor@Sun.COM 	libzfs_handle_t *hdl = zhp->zfs_hdl;
397789Sahrens 
3984543Smarks 	/*
3994543Smarks 	 * Preserve history log string.
4004543Smarks 	 * any changes performed here will be
4014543Smarks 	 * logged as an internal event.
4024543Smarks 	 */
4034543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
4044543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
4058228SEric.Taylor@Sun.COM 
4061758Sahrens top:
4078228SEric.Taylor@Sun.COM 	if (put_stats_zhdl(zhp, zc) != 0) {
4084543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
4098228SEric.Taylor@Sun.COM 		return (-1);
410789Sahrens 	}
411789Sahrens 
4128228SEric.Taylor@Sun.COM 
4131758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4148228SEric.Taylor@Sun.COM 		zfs_cmd_t zc2 = { 0 };
4151758Sahrens 
4161758Sahrens 		/*
4171758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4181758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4191758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4201758Sahrens 		 * can't be mounted.  However, it could also be that we
4211758Sahrens 		 * have crashed in the middle of one of those
4221758Sahrens 		 * operations, in which case we need to get rid of the
4231758Sahrens 		 * inconsistent state.  We do that by either rolling
4241758Sahrens 		 * back to the previous snapshot (which will fail if
4251758Sahrens 		 * there is none), or destroying the filesystem.  Note
4261758Sahrens 		 * that if we are still in the middle of an active
4271758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4281758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4291758Sahrens 		 */
4301758Sahrens 
4318228SEric.Taylor@Sun.COM 		(void) strlcpy(zc2.zc_name, zhp->zfs_name,
4328228SEric.Taylor@Sun.COM 		    sizeof (zc2.zc_name));
4331758Sahrens 
4342885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4352082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4368228SEric.Taylor@Sun.COM 			zc2.zc_objset_type = DMU_OST_ZVOL;
4371758Sahrens 		} else {
4388228SEric.Taylor@Sun.COM 			zc2.zc_objset_type = DMU_OST_ZFS;
4391758Sahrens 		}
4401758Sahrens 
4411758Sahrens 		/*
4425331Samw 		 * If we can successfully destroy it, pretend that it
4431758Sahrens 		 * never existed.
4441758Sahrens 		 */
4458228SEric.Taylor@Sun.COM 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
4464543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4471758Sahrens 			errno = ENOENT;
4488228SEric.Taylor@Sun.COM 			return (-1);
4491758Sahrens 		}
4508228SEric.Taylor@Sun.COM 		/* If we can successfully roll it back, reset the stats */
4518228SEric.Taylor@Sun.COM 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
4528228SEric.Taylor@Sun.COM 			if (get_stats_ioctl(zhp, zc) != 0) {
4538228SEric.Taylor@Sun.COM 				zhp->zfs_hdl->libzfs_log_str = logstr;
4548228SEric.Taylor@Sun.COM 				return (-1);
4558228SEric.Taylor@Sun.COM 			}
4565367Sahrens 			goto top;
4578228SEric.Taylor@Sun.COM 		}
4581758Sahrens 	}
4591758Sahrens 
460789Sahrens 	/*
461789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
462789Sahrens 	 * the high-level type.
463789Sahrens 	 */
4642885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4652885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4662885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4672885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4682885Sahrens 	else
4692885Sahrens 		abort();
4702885Sahrens 
471789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
472789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
473789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
474789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
475789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
476789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
477789Sahrens 	else
4782082Seschrock 		abort();	/* we should never see any other types */
479789Sahrens 
4804543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
4816865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
4828228SEric.Taylor@Sun.COM 	return (0);
4838228SEric.Taylor@Sun.COM }
4848228SEric.Taylor@Sun.COM 
4858228SEric.Taylor@Sun.COM zfs_handle_t *
4868228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path)
4878228SEric.Taylor@Sun.COM {
4888228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
4898228SEric.Taylor@Sun.COM 
4908228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4918228SEric.Taylor@Sun.COM 
4928228SEric.Taylor@Sun.COM 	if (zhp == NULL)
4938228SEric.Taylor@Sun.COM 		return (NULL);
4948228SEric.Taylor@Sun.COM 
4958228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
4968228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
4978228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
4988228SEric.Taylor@Sun.COM 		free(zhp);
4998228SEric.Taylor@Sun.COM 		return (NULL);
5008228SEric.Taylor@Sun.COM 	}
5018228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) == -1) {
5028228SEric.Taylor@Sun.COM 		zcmd_free_nvlists(&zc);
5038228SEric.Taylor@Sun.COM 		free(zhp);
5048228SEric.Taylor@Sun.COM 		return (NULL);
5058228SEric.Taylor@Sun.COM 	}
5068228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, &zc) == -1) {
5078228SEric.Taylor@Sun.COM 		free(zhp);
5088228SEric.Taylor@Sun.COM 		zhp = NULL;
5098228SEric.Taylor@Sun.COM 	}
5108228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
5118228SEric.Taylor@Sun.COM 	return (zhp);
5128228SEric.Taylor@Sun.COM }
5138228SEric.Taylor@Sun.COM 
5148228SEric.Taylor@Sun.COM static zfs_handle_t *
5158228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
5168228SEric.Taylor@Sun.COM {
5178228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5188228SEric.Taylor@Sun.COM 
5198228SEric.Taylor@Sun.COM 	if (zhp == NULL)
5208228SEric.Taylor@Sun.COM 		return (NULL);
5218228SEric.Taylor@Sun.COM 
5228228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
5238228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5248228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, zc) == -1) {
5258228SEric.Taylor@Sun.COM 		free(zhp);
5268228SEric.Taylor@Sun.COM 		return (NULL);
5278228SEric.Taylor@Sun.COM 	}
528789Sahrens 	return (zhp);
529789Sahrens }
530789Sahrens 
531789Sahrens /*
532789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
533789Sahrens  * argument is a mask of acceptable types.  The function will print an
534789Sahrens  * appropriate error message and return NULL if it can't be opened.
535789Sahrens  */
536789Sahrens zfs_handle_t *
5372082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
538789Sahrens {
539789Sahrens 	zfs_handle_t *zhp;
5402082Seschrock 	char errbuf[1024];
5412082Seschrock 
5422082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
5432082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
544789Sahrens 
545789Sahrens 	/*
5462082Seschrock 	 * Validate the name before we even try to open it.
547789Sahrens 	 */
5485326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
5492082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5502082Seschrock 		    "invalid dataset name"));
5512082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
552789Sahrens 		return (NULL);
553789Sahrens 	}
554789Sahrens 
555789Sahrens 	/*
556789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
557789Sahrens 	 */
558789Sahrens 	errno = 0;
5592082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5603237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
561789Sahrens 		return (NULL);
562789Sahrens 	}
563789Sahrens 
564789Sahrens 	if (!(types & zhp->zfs_type)) {
5652082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5662142Seschrock 		zfs_close(zhp);
567789Sahrens 		return (NULL);
568789Sahrens 	}
569789Sahrens 
570789Sahrens 	return (zhp);
571789Sahrens }
572789Sahrens 
573789Sahrens /*
574789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
575789Sahrens  */
576789Sahrens void
577789Sahrens zfs_close(zfs_handle_t *zhp)
578789Sahrens {
579789Sahrens 	if (zhp->zfs_mntopts)
580789Sahrens 		free(zhp->zfs_mntopts);
5812676Seschrock 	nvlist_free(zhp->zfs_props);
5822676Seschrock 	nvlist_free(zhp->zfs_user_props);
583789Sahrens 	free(zhp);
584789Sahrens }
585789Sahrens 
5868228SEric.Taylor@Sun.COM typedef struct mnttab_node {
5878228SEric.Taylor@Sun.COM 	struct mnttab mtn_mt;
5888228SEric.Taylor@Sun.COM 	avl_node_t mtn_node;
5898228SEric.Taylor@Sun.COM } mnttab_node_t;
5908228SEric.Taylor@Sun.COM 
5918228SEric.Taylor@Sun.COM static int
5928228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
5938228SEric.Taylor@Sun.COM {
5948228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn1 = arg1;
5958228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn2 = arg2;
5968228SEric.Taylor@Sun.COM 	int rv;
5978228SEric.Taylor@Sun.COM 
5988228SEric.Taylor@Sun.COM 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
5998228SEric.Taylor@Sun.COM 
6008228SEric.Taylor@Sun.COM 	if (rv == 0)
6018228SEric.Taylor@Sun.COM 		return (0);
6028228SEric.Taylor@Sun.COM 	return (rv > 0 ? 1 : -1);
6038228SEric.Taylor@Sun.COM }
6048228SEric.Taylor@Sun.COM 
6058228SEric.Taylor@Sun.COM void
6068228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl)
6078228SEric.Taylor@Sun.COM {
6088228SEric.Taylor@Sun.COM 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
6098228SEric.Taylor@Sun.COM 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
6108228SEric.Taylor@Sun.COM 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
611*8811SEric.Taylor@Sun.COM }
612*8811SEric.Taylor@Sun.COM 
613*8811SEric.Taylor@Sun.COM void
614*8811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl)
615*8811SEric.Taylor@Sun.COM {
616*8811SEric.Taylor@Sun.COM 	struct mnttab entry;
6178228SEric.Taylor@Sun.COM 
6188228SEric.Taylor@Sun.COM 	rewind(hdl->libzfs_mnttab);
6198228SEric.Taylor@Sun.COM 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
6208228SEric.Taylor@Sun.COM 		mnttab_node_t *mtn;
6218228SEric.Taylor@Sun.COM 
6228228SEric.Taylor@Sun.COM 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6238228SEric.Taylor@Sun.COM 			continue;
6248228SEric.Taylor@Sun.COM 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
6258228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
6268228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
6278228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
6288228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
6298228SEric.Taylor@Sun.COM 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
6308228SEric.Taylor@Sun.COM 	}
6318228SEric.Taylor@Sun.COM }
6328228SEric.Taylor@Sun.COM 
6338228SEric.Taylor@Sun.COM void
6348228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl)
6358228SEric.Taylor@Sun.COM {
6368228SEric.Taylor@Sun.COM 	void *cookie = NULL;
6378228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6388228SEric.Taylor@Sun.COM 
6398228SEric.Taylor@Sun.COM 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
6408228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_special);
6418228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mountp);
6428228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_fstype);
6438228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mntopts);
6448228SEric.Taylor@Sun.COM 		free(mtn);
6458228SEric.Taylor@Sun.COM 	}
6468228SEric.Taylor@Sun.COM 	avl_destroy(&hdl->libzfs_mnttab_cache);
6478228SEric.Taylor@Sun.COM }
6488228SEric.Taylor@Sun.COM 
649*8811SEric.Taylor@Sun.COM void
650*8811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
651*8811SEric.Taylor@Sun.COM {
652*8811SEric.Taylor@Sun.COM 	hdl->libzfs_mnttab_enable = enable;
653*8811SEric.Taylor@Sun.COM }
654*8811SEric.Taylor@Sun.COM 
6558228SEric.Taylor@Sun.COM int
6568228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
6578228SEric.Taylor@Sun.COM     struct mnttab *entry)
6588228SEric.Taylor@Sun.COM {
6598228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6608228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6618228SEric.Taylor@Sun.COM 
662*8811SEric.Taylor@Sun.COM 	if (!hdl->libzfs_mnttab_enable) {
663*8811SEric.Taylor@Sun.COM 		struct mnttab srch = { 0 };
664*8811SEric.Taylor@Sun.COM 
665*8811SEric.Taylor@Sun.COM 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
666*8811SEric.Taylor@Sun.COM 			libzfs_mnttab_fini(hdl);
667*8811SEric.Taylor@Sun.COM 		rewind(hdl->libzfs_mnttab);
668*8811SEric.Taylor@Sun.COM 		srch.mnt_special = (char *)fsname;
669*8811SEric.Taylor@Sun.COM 		srch.mnt_fstype = MNTTYPE_ZFS;
670*8811SEric.Taylor@Sun.COM 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
671*8811SEric.Taylor@Sun.COM 			return (0);
672*8811SEric.Taylor@Sun.COM 		else
673*8811SEric.Taylor@Sun.COM 			return (ENOENT);
674*8811SEric.Taylor@Sun.COM 	}
675*8811SEric.Taylor@Sun.COM 
6768228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
677*8811SEric.Taylor@Sun.COM 		libzfs_mnttab_update(hdl);
6788228SEric.Taylor@Sun.COM 
6798228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6808228SEric.Taylor@Sun.COM 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
6818228SEric.Taylor@Sun.COM 	if (mtn) {
6828228SEric.Taylor@Sun.COM 		*entry = mtn->mtn_mt;
6838228SEric.Taylor@Sun.COM 		return (0);
6848228SEric.Taylor@Sun.COM 	}
6858228SEric.Taylor@Sun.COM 	return (ENOENT);
6868228SEric.Taylor@Sun.COM }
6878228SEric.Taylor@Sun.COM 
6888228SEric.Taylor@Sun.COM void
6898228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
6908228SEric.Taylor@Sun.COM     const char *mountp, const char *mntopts)
6918228SEric.Taylor@Sun.COM {
6928228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6938228SEric.Taylor@Sun.COM 
6948228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6958228SEric.Taylor@Sun.COM 		return;
6968228SEric.Taylor@Sun.COM 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
6978228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
6988228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
6998228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
7008228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
7018228SEric.Taylor@Sun.COM 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
7028228SEric.Taylor@Sun.COM }
7038228SEric.Taylor@Sun.COM 
7048228SEric.Taylor@Sun.COM void
7058228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
7068228SEric.Taylor@Sun.COM {
7078228SEric.Taylor@Sun.COM 	mnttab_node_t find;
7088228SEric.Taylor@Sun.COM 	mnttab_node_t *ret;
7098228SEric.Taylor@Sun.COM 
7108228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
7118228SEric.Taylor@Sun.COM 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
7128228SEric.Taylor@Sun.COM 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
7138228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_special);
7148228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mountp);
7158228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_fstype);
7168228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mntopts);
7178228SEric.Taylor@Sun.COM 		free(ret);
7188228SEric.Taylor@Sun.COM 	}
7198228SEric.Taylor@Sun.COM }
7208228SEric.Taylor@Sun.COM 
7215713Srm160521 int
7225713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
7235713Srm160521 {
7246865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
7256865Srm160521 
7265713Srm160521 	if (zpool_handle == NULL)
7275713Srm160521 		return (-1);
7285713Srm160521 
7295713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
7305713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
7315713Srm160521 	return (0);
7325713Srm160521 }
7335713Srm160521 
7345713Srm160521 /*
7355713Srm160521  * The choice of reservation property depends on the SPA version.
7365713Srm160521  */
7375713Srm160521 static int
7385713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
7395713Srm160521 {
7405713Srm160521 	int spa_version;
7415713Srm160521 
7425713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
7435713Srm160521 		return (-1);
7445713Srm160521 
7455713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
7465713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
7475713Srm160521 	else
7485713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
7495713Srm160521 
7505713Srm160521 	return (0);
7515713Srm160521 }
7525713Srm160521 
7533912Slling /*
7542676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
7552676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
7562676Seschrock  * strings.
757789Sahrens  */
7587184Stimh nvlist_t *
7597184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
7605094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
761789Sahrens {
7622676Seschrock 	nvpair_t *elem;
7632676Seschrock 	uint64_t intval;
7642676Seschrock 	char *strval;
7655094Slling 	zfs_prop_t prop;
7662676Seschrock 	nvlist_t *ret;
7675331Samw 	int chosen_normal = -1;
7685331Samw 	int chosen_utf = -1;
7692676Seschrock 
7705094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
7715094Slling 		(void) no_memory(hdl);
7725094Slling 		return (NULL);
773789Sahrens 	}
774789Sahrens 
7752676Seschrock 	elem = NULL;
7762676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
7775094Slling 		const char *propname = nvpair_name(elem);
7782676Seschrock 
7792676Seschrock 		/*
7802676Seschrock 		 * Make sure this property is valid and applies to this type.
7812676Seschrock 		 */
7825094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
7835094Slling 			if (!zfs_prop_user(propname)) {
7842676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7855094Slling 				    "invalid property '%s'"), propname);
7865094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7875094Slling 				goto error;
7885094Slling 			}
7895094Slling 
7905094Slling 			/*
7915094Slling 			 * If this is a user property, make sure it's a
7925094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
7935094Slling 			 */
7945094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
7955094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7965094Slling 				    "'%s' must be a string"), propname);
7975094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7985094Slling 				goto error;
7995094Slling 			}
8005094Slling 
8015094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
8025094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8035094Slling 				    "property name '%s' is too long"),
8042676Seschrock 				    propname);
8052676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8062676Seschrock 				goto error;
8072676Seschrock 			}
8082676Seschrock 
8092676Seschrock 			(void) nvpair_value_string(elem, &strval);
8102676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
8112676Seschrock 				(void) no_memory(hdl);
8122676Seschrock 				goto error;
8132676Seschrock 			}
8142676Seschrock 			continue;
815789Sahrens 		}
8162676Seschrock 
8177265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
8187265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8197265Sahrens 			    "this property can not be modified for snapshots"));
8207265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8217265Sahrens 			goto error;
8227265Sahrens 		}
8237265Sahrens 
8242676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
8252676Seschrock 			zfs_error_aux(hdl,
8262676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
8272676Seschrock 			    "apply to datasets of this type"), propname);
8282676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8292676Seschrock 			goto error;
8302676Seschrock 		}
8312676Seschrock 
8322676Seschrock 		if (zfs_prop_readonly(prop) &&
8335331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
8342676Seschrock 			zfs_error_aux(hdl,
8352676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
8362676Seschrock 			    propname);
8372676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
8382676Seschrock 			goto error;
8392676Seschrock 		}
8402676Seschrock 
8415094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
8425094Slling 		    &strval, &intval, errbuf) != 0)
8432676Seschrock 			goto error;
8442676Seschrock 
8452676Seschrock 		/*
8462676Seschrock 		 * Perform some additional checks for specific properties.
8472676Seschrock 		 */
8482676Seschrock 		switch (prop) {
8494577Sahrens 		case ZFS_PROP_VERSION:
8504577Sahrens 		{
8514577Sahrens 			int version;
8524577Sahrens 
8534577Sahrens 			if (zhp == NULL)
8544577Sahrens 				break;
8554577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
8564577Sahrens 			if (intval < version) {
8574577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8584577Sahrens 				    "Can not downgrade; already at version %u"),
8594577Sahrens 				    version);
8604577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8614577Sahrens 				goto error;
8624577Sahrens 			}
8634577Sahrens 			break;
8644577Sahrens 		}
8654577Sahrens 
8662676Seschrock 		case ZFS_PROP_RECORDSIZE:
8672676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
8682676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
8692676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
8702676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
8712082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8722676Seschrock 				    "'%s' must be power of 2 from %u "
8732676Seschrock 				    "to %uk"), propname,
8742676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
8752676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
8762676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8772676Seschrock 				goto error;
878789Sahrens 			}
879789Sahrens 			break;
880789Sahrens 
8813126Sahl 		case ZFS_PROP_SHAREISCSI:
8823126Sahl 			if (strcmp(strval, "off") != 0 &&
8833126Sahl 			    strcmp(strval, "on") != 0 &&
8843126Sahl 			    strcmp(strval, "type=disk") != 0) {
8853126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8863126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
8873126Sahl 				    propname);
8883126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8893126Sahl 				goto error;
8903126Sahl 			}
8913126Sahl 
8923126Sahl 			break;
8933126Sahl 
8942676Seschrock 		case ZFS_PROP_MOUNTPOINT:
8954778Srm160521 		{
8964778Srm160521 			namecheck_err_t why;
8974778Srm160521 
8982676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
8992676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9002676Seschrock 				break;
9012676Seschrock 
9024778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
9034778Srm160521 				switch (why) {
9044778Srm160521 				case NAME_ERR_LEADING_SLASH:
9054778Srm160521 					zfs_error_aux(hdl,
9064778Srm160521 					    dgettext(TEXT_DOMAIN,
9074778Srm160521 					    "'%s' must be an absolute path, "
9084778Srm160521 					    "'none', or 'legacy'"), propname);
9094778Srm160521 					break;
9104778Srm160521 				case NAME_ERR_TOOLONG:
9114778Srm160521 					zfs_error_aux(hdl,
9124778Srm160521 					    dgettext(TEXT_DOMAIN,
9134778Srm160521 					    "component of '%s' is too long"),
9144778Srm160521 					    propname);
9154778Srm160521 					break;
9164778Srm160521 				}
9172676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9182676Seschrock 				goto error;
919789Sahrens 			}
9204778Srm160521 		}
9214778Srm160521 
9223126Sahl 			/*FALLTHRU*/
9233126Sahl 
9245331Samw 		case ZFS_PROP_SHARESMB:
9253126Sahl 		case ZFS_PROP_SHARENFS:
9263126Sahl 			/*
9275331Samw 			 * For the mountpoint and sharenfs or sharesmb
9285331Samw 			 * properties, check if it can be set in a
9295331Samw 			 * global/non-global zone based on
9303126Sahl 			 * the zoned property value:
9313126Sahl 			 *
9323126Sahl 			 *		global zone	    non-global zone
9333126Sahl 			 * --------------------------------------------------
9343126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
9353126Sahl 			 *		sharenfs (no)	    sharenfs (no)
9365331Samw 			 *		sharesmb (no)	    sharesmb (no)
9373126Sahl 			 *
9383126Sahl 			 * zoned=off	mountpoint (yes)	N/A
9393126Sahl 			 *		sharenfs (yes)
9405331Samw 			 *		sharesmb (yes)
9413126Sahl 			 */
9422676Seschrock 			if (zoned) {
9432676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
9442676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9452676Seschrock 					    "'%s' cannot be set on "
9462676Seschrock 					    "dataset in a non-global zone"),
9472676Seschrock 					    propname);
9482676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9492676Seschrock 					    errbuf);
9502676Seschrock 					goto error;
9515331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
9525331Samw 				    prop == ZFS_PROP_SHARESMB) {
9532676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9542676Seschrock 					    "'%s' cannot be set in "
9552676Seschrock 					    "a non-global zone"), propname);
9562676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9572676Seschrock 					    errbuf);
9582676Seschrock 					goto error;
9592676Seschrock 				}
9602676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
9612676Seschrock 				/*
9622676Seschrock 				 * If zoned property is 'off', this must be in
9632676Seschrock 				 * a globle zone. If not, something is wrong.
9642676Seschrock 				 */
9652676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9662676Seschrock 				    "'%s' cannot be set while dataset "
9672676Seschrock 				    "'zoned' property is set"), propname);
9682676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
9692676Seschrock 				goto error;
9702676Seschrock 			}
9713126Sahl 
9724180Sdougm 			/*
9734180Sdougm 			 * At this point, it is legitimate to set the
9744180Sdougm 			 * property. Now we want to make sure that the
9754180Sdougm 			 * property value is valid if it is sharenfs.
9764180Sdougm 			 */
9775331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
9785331Samw 			    prop == ZFS_PROP_SHARESMB) &&
9794217Seschrock 			    strcmp(strval, "on") != 0 &&
9804217Seschrock 			    strcmp(strval, "off") != 0) {
9815331Samw 				zfs_share_proto_t proto;
9825331Samw 
9835331Samw 				if (prop == ZFS_PROP_SHARESMB)
9845331Samw 					proto = PROTO_SMB;
9855331Samw 				else
9865331Samw 					proto = PROTO_NFS;
9874180Sdougm 
9884180Sdougm 				/*
9895331Samw 				 * Must be an valid sharing protocol
9905331Samw 				 * option string so init the libshare
9915331Samw 				 * in order to enable the parser and
9925331Samw 				 * then parse the options. We use the
9935331Samw 				 * control API since we don't care about
9945331Samw 				 * the current configuration and don't
9954180Sdougm 				 * want the overhead of loading it
9964180Sdougm 				 * until we actually do something.
9974180Sdougm 				 */
9984180Sdougm 
9994217Seschrock 				if (zfs_init_libshare(hdl,
10004217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
10014217Seschrock 					/*
10024217Seschrock 					 * An error occurred so we can't do
10034217Seschrock 					 * anything
10044217Seschrock 					 */
10054217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10064217Seschrock 					    "'%s' cannot be set: problem "
10074217Seschrock 					    "in share initialization"),
10084217Seschrock 					    propname);
10094217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10104217Seschrock 					    errbuf);
10114217Seschrock 					goto error;
10124217Seschrock 				}
10134217Seschrock 
10145331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
10154217Seschrock 					/*
10164217Seschrock 					 * There was an error in parsing so
10174217Seschrock 					 * deal with it by issuing an error
10184217Seschrock 					 * message and leaving after
10194217Seschrock 					 * uninitializing the the libshare
10204217Seschrock 					 * interface.
10214217Seschrock 					 */
10224217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10234217Seschrock 					    "'%s' cannot be set to invalid "
10244217Seschrock 					    "options"), propname);
10254217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10264217Seschrock 					    errbuf);
10274217Seschrock 					zfs_uninit_libshare(hdl);
10284217Seschrock 					goto error;
10294217Seschrock 				}
10304180Sdougm 				zfs_uninit_libshare(hdl);
10314180Sdougm 			}
10324180Sdougm 
10333126Sahl 			break;
10345331Samw 		case ZFS_PROP_UTF8ONLY:
10355331Samw 			chosen_utf = (int)intval;
10365331Samw 			break;
10375331Samw 		case ZFS_PROP_NORMALIZE:
10385331Samw 			chosen_normal = (int)intval;
10395331Samw 			break;
10402676Seschrock 		}
10412676Seschrock 
10422676Seschrock 		/*
10432676Seschrock 		 * For changes to existing volumes, we have some additional
10442676Seschrock 		 * checks to enforce.
10452676Seschrock 		 */
10462676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
10472676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
10482676Seschrock 			    ZFS_PROP_VOLSIZE);
10492676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
10502676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
10512676Seschrock 			char buf[64];
10522676Seschrock 
10532676Seschrock 			switch (prop) {
10542676Seschrock 			case ZFS_PROP_RESERVATION:
10555378Sck153898 			case ZFS_PROP_REFRESERVATION:
10562676Seschrock 				if (intval > volsize) {
10572676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10582676Seschrock 					    "'%s' is greater than current "
10592676Seschrock 					    "volume size"), propname);
10602676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10612676Seschrock 					    errbuf);
10622676Seschrock 					goto error;
10632676Seschrock 				}
10642676Seschrock 				break;
10652676Seschrock 
10662676Seschrock 			case ZFS_PROP_VOLSIZE:
10672676Seschrock 				if (intval % blocksize != 0) {
10682676Seschrock 					zfs_nicenum(blocksize, buf,
10692676Seschrock 					    sizeof (buf));
10702676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10712676Seschrock 					    "'%s' must be a multiple of "
10722676Seschrock 					    "volume block size (%s)"),
10732676Seschrock 					    propname, buf);
10742676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10752676Seschrock 					    errbuf);
10762676Seschrock 					goto error;
10772676Seschrock 				}
10782676Seschrock 
10792676Seschrock 				if (intval == 0) {
10802676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10812676Seschrock 					    "'%s' cannot be zero"),
10822676Seschrock 					    propname);
10832676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10842676Seschrock 					    errbuf);
10852676Seschrock 					goto error;
1086789Sahrens 				}
10873126Sahl 				break;
1088789Sahrens 			}
1089789Sahrens 		}
1090789Sahrens 	}
1091789Sahrens 
10922676Seschrock 	/*
10935331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
10945331Samw 	 * enforce rejection of non-UTF8 names.
10955331Samw 	 *
10965331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
10975331Samw 	 * was explicitly not chosen, it is an error.
10985331Samw 	 */
10995498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
11005331Samw 		if (nvlist_add_uint64(ret,
11015331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
11025331Samw 			(void) no_memory(hdl);
11035331Samw 			goto error;
11045331Samw 		}
11055498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
11065331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11075331Samw 		    "'%s' must be set 'on' if normalization chosen"),
11085331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
11095331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11105331Samw 		goto error;
11115331Samw 	}
11125331Samw 
11135331Samw 	/*
11142676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
11152676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
11162676Seschrock 	 */
11172676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
11182676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
11192676Seschrock 	    &intval) == 0) {
11202676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
11212676Seschrock 		    ZFS_PROP_VOLSIZE);
11225481Sck153898 		uint64_t old_reservation;
11232676Seschrock 		uint64_t new_reservation;
11245481Sck153898 		zfs_prop_t resv_prop;
11255713Srm160521 
11265713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
11275481Sck153898 			goto error;
11285481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
11292676Seschrock 
11302676Seschrock 		if (old_volsize == old_reservation &&
11315481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
11322676Seschrock 		    &new_reservation) != 0) {
11332676Seschrock 			if (nvlist_add_uint64(ret,
11345481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
11352676Seschrock 				(void) no_memory(hdl);
11362676Seschrock 				goto error;
11372676Seschrock 			}
11382676Seschrock 		}
11392676Seschrock 	}
11402676Seschrock 	return (ret);
11412676Seschrock 
11422676Seschrock error:
11432676Seschrock 	nvlist_free(ret);
11442676Seschrock 	return (NULL);
1145789Sahrens }
1146789Sahrens 
11474543Smarks static int
11484543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
11494543Smarks     uint64_t *ret_who)
11504543Smarks {
11514543Smarks 	struct passwd *pwd;
11524543Smarks 	struct group *grp;
11534543Smarks 	uid_t id;
11544543Smarks 
11554543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
11564543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
11574543Smarks 		*ret_who = -1;
11584543Smarks 		return (0);
11594543Smarks 	}
11604543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
11614543Smarks 		return (EZFS_BADWHO);
11624543Smarks 
11634543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
11644543Smarks 	    strcmp(who, "everyone") == 0) {
11654543Smarks 		*ret_who = -1;
11664543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
11674543Smarks 		return (0);
11684543Smarks 	}
11694543Smarks 
11704543Smarks 	pwd = getpwnam(who);
11714543Smarks 	grp = getgrnam(who);
11724543Smarks 
11734543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
11744543Smarks 		*ret_who = pwd->pw_uid;
11754543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
11764543Smarks 		*ret_who = grp->gr_gid;
11774543Smarks 	} else if (pwd) {
11784543Smarks 		*ret_who = pwd->pw_uid;
11794543Smarks 		*who_type = ZFS_DELEG_USER;
11804543Smarks 	} else if (grp) {
11814543Smarks 		*ret_who = grp->gr_gid;
11824543Smarks 		*who_type = ZFS_DELEG_GROUP;
11834543Smarks 	} else {
11844543Smarks 		char *end;
11854543Smarks 
11864543Smarks 		id = strtol(who, &end, 10);
11874543Smarks 		if (errno != 0 || *end != '\0') {
11884543Smarks 			return (EZFS_BADWHO);
11894543Smarks 		} else {
11904543Smarks 			*ret_who = id;
11914543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
11924543Smarks 				*who_type = ZFS_DELEG_USER;
11934543Smarks 		}
11944543Smarks 	}
11954543Smarks 
11964543Smarks 	return (0);
11974543Smarks }
11984543Smarks 
11994543Smarks static void
12004543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
12014543Smarks {
12024543Smarks 	if (perms_nvp != NULL) {
12034543Smarks 		verify(nvlist_add_nvlist(who_nvp,
12044543Smarks 		    name, perms_nvp) == 0);
12054543Smarks 	} else {
12064543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
12074543Smarks 	}
12084543Smarks }
12094543Smarks 
12104543Smarks static void
12114543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
12124543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
12134543Smarks     nvlist_t *sets_nvp)
12144543Smarks {
12154543Smarks 	boolean_t do_perms, do_sets;
12164543Smarks 	char name[ZFS_MAX_DELEG_NAME];
12174543Smarks 
12184543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
12194543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
12204543Smarks 
12214543Smarks 	if (!do_perms && !do_sets)
12224543Smarks 		do_perms = do_sets = B_TRUE;
12234543Smarks 
12244543Smarks 	if (do_perms) {
12254543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
12264543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
12274543Smarks 		    whostr : (void *)&whoid);
12284543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
12294543Smarks 	}
12304543Smarks 	if (do_sets) {
12314543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
12324543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
12334543Smarks 		    whostr : (void *)&whoid);
12344543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
12354543Smarks 	}
12364543Smarks }
12374543Smarks 
12384543Smarks static void
12394543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
12404543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
12414543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
12424543Smarks {
12434543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
12444543Smarks 		helper(who_type, whoid, whostr, 0,
12454543Smarks 		    who_nvp, perms_nvp, sets_nvp);
12464543Smarks 	} else {
12474543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
12484543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
12494543Smarks 			    who_nvp, perms_nvp, sets_nvp);
12504543Smarks 		}
12514543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
12524543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
12534543Smarks 			    who_nvp, perms_nvp, sets_nvp);
12544543Smarks 		}
12554543Smarks 	}
12564543Smarks }
12574543Smarks 
12584543Smarks /*
12594543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
12604543Smarks  *
12614543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
12624543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
12634543Smarks  * base attribute named stored in the dsl.
12644543Smarks  * Arguments:
12654543Smarks  *
12664543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
12674543Smarks  *           whostr may be null for everyone or create perms.
12684543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
12694543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
12705331Samw  * perms:    common separated list of permissions.  May be null if user
12714543Smarks  *           is requested to remove permissions by who.
12724543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
12734543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
12744543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
12754543Smarks  *           The output nvp will look something like this.
12764543Smarks  *              ul$1234 -> {create ; destroy }
12774543Smarks  *              Ul$1234 -> { @myset }
12784543Smarks  *              s-$@myset - { snapshot; checksum; compression }
12794543Smarks  */
12804543Smarks int
12814543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
12824543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
12834543Smarks {
12844543Smarks 	nvlist_t *who_nvp;
12854543Smarks 	nvlist_t *perms_nvp = NULL;
12864543Smarks 	nvlist_t *sets_nvp = NULL;
12874543Smarks 	char errbuf[1024];
12884787Sahrens 	char *who_tok, *perm;
12894543Smarks 	int error;
12904543Smarks 
12914543Smarks 	*nvp = NULL;
12924543Smarks 
12934543Smarks 	if (perms) {
12944543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
12954543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
12964543Smarks 			return (1);
12974543Smarks 		}
12984543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
12994543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
13004543Smarks 			nvlist_free(perms_nvp);
13014543Smarks 			return (1);
13024543Smarks 		}
13034543Smarks 	}
13044543Smarks 
13054543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
13064543Smarks 		if (perms_nvp)
13074543Smarks 			nvlist_free(perms_nvp);
13084543Smarks 		if (sets_nvp)
13094543Smarks 			nvlist_free(sets_nvp);
13104543Smarks 		return (1);
13114543Smarks 	}
13124543Smarks 
13134543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
13144543Smarks 		namecheck_err_t why;
13154543Smarks 		char what;
13164543Smarks 
13174543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
13184787Sahrens 			nvlist_free(who_nvp);
13194787Sahrens 			if (perms_nvp)
13204787Sahrens 				nvlist_free(perms_nvp);
13214787Sahrens 			if (sets_nvp)
13224787Sahrens 				nvlist_free(sets_nvp);
13234787Sahrens 
13244543Smarks 			switch (why) {
13254543Smarks 			case NAME_ERR_NO_AT:
13264543Smarks 				zfs_error_aux(zhp->zfs_hdl,
13274543Smarks 				    dgettext(TEXT_DOMAIN,
13284543Smarks 				    "set definition must begin with an '@' "
13294543Smarks 				    "character"));
13304543Smarks 			}
13314543Smarks 			return (zfs_error(zhp->zfs_hdl,
13324543Smarks 			    EZFS_BADPERMSET, whostr));
13334543Smarks 		}
13344543Smarks 	}
13354543Smarks 
13364543Smarks 	/*
13374543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
13384543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
13394543Smarks 	 * other sets_nvp will have only permssion set names in it.
13404543Smarks 	 */
13414787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
13424787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
13434787Sahrens 
13444787Sahrens 		if (perm_canonical) {
13454787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
13464787Sahrens 			    perm_canonical) == 0);
13474787Sahrens 		} else if (perm[0] == '@') {
13484787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
13494787Sahrens 		} else {
13504787Sahrens 			nvlist_free(who_nvp);
13514787Sahrens 			nvlist_free(perms_nvp);
13524787Sahrens 			nvlist_free(sets_nvp);
13534787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
13544543Smarks 		}
13554543Smarks 	}
13564543Smarks 
13574543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
13584543Smarks 		who_tok = strtok(whostr, ",");
13594543Smarks 		if (who_tok == NULL) {
13604543Smarks 			nvlist_free(who_nvp);
13614787Sahrens 			if (perms_nvp)
13624787Sahrens 				nvlist_free(perms_nvp);
13634543Smarks 			if (sets_nvp)
13644543Smarks 				nvlist_free(sets_nvp);
13654543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
13664543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
13674543Smarks 			    whostr);
13684543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
13694543Smarks 		}
13704543Smarks 	}
13714543Smarks 
13724543Smarks 	/*
13734543Smarks 	 * Now create the nvlist(s)
13744543Smarks 	 */
13754543Smarks 	do {
13764543Smarks 		uint64_t who_id;
13774543Smarks 
13784543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
13794543Smarks 		    &who_id);
13804543Smarks 		if (error) {
13814543Smarks 			nvlist_free(who_nvp);
13824787Sahrens 			if (perms_nvp)
13834787Sahrens 				nvlist_free(perms_nvp);
13844543Smarks 			if (sets_nvp)
13854543Smarks 				nvlist_free(sets_nvp);
13864543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
13874543Smarks 			    dgettext(TEXT_DOMAIN,
13884543Smarks 			    "Unable to determine uid/gid for "
13894543Smarks 			    "%s "), who_tok);
13904543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
13914543Smarks 		}
13924543Smarks 
13934543Smarks 		/*
13944543Smarks 		 * add entries for both local and descendent when required
13954543Smarks 		 */
13964543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
13974543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
13984543Smarks 
13994543Smarks 	} while (who_tok = strtok(NULL, ","));
14004543Smarks 	*nvp = who_nvp;
14014543Smarks 	return (0);
14024543Smarks }
14034543Smarks 
14044543Smarks static int
14054543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
14064543Smarks {
14074543Smarks 	zfs_cmd_t zc = { 0 };
14084543Smarks 	int error;
14094543Smarks 	char errbuf[1024];
14104543Smarks 
14114543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
14124543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
14134543Smarks 	    zhp->zfs_name);
14144543Smarks 
14155094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
14164543Smarks 		return (-1);
14174543Smarks 
14184543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14194543Smarks 	zc.zc_perm_action = unset;
14204543Smarks 
14214543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
14224543Smarks 	if (error && errno == ENOTSUP) {
14234543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
14244543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
14254543Smarks 		zcmd_free_nvlists(&zc);
14264543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
14274543Smarks 	} else if (error) {
14284543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
14294543Smarks 	}
14304543Smarks 	zcmd_free_nvlists(&zc);
14314543Smarks 
14324543Smarks 	return (error);
14334543Smarks }
14344543Smarks 
14354543Smarks int
14364543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
14374543Smarks {
14384543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
14394543Smarks }
14404543Smarks 
14414543Smarks int
14424543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
14434543Smarks {
14444543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
14454543Smarks }
14464543Smarks 
14474543Smarks static int
14484543Smarks perm_compare(const void *arg1, const void *arg2)
14494543Smarks {
14504543Smarks 	const zfs_perm_node_t *node1 = arg1;
14514543Smarks 	const zfs_perm_node_t *node2 = arg2;
14524543Smarks 	int ret;
14534543Smarks 
14544543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
14554543Smarks 
14564543Smarks 	if (ret > 0)
14574543Smarks 		return (1);
14584543Smarks 	if (ret < 0)
14594543Smarks 		return (-1);
14604543Smarks 	else
14614543Smarks 		return (0);
14624543Smarks }
14634543Smarks 
14644543Smarks static void
14654543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
14664543Smarks {
14674543Smarks 	zfs_perm_node_t *permnode;
14685367Sahrens 	void *cookie = NULL;
14695367Sahrens 
14705367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
14714543Smarks 		free(permnode);
14725367Sahrens 	avl_destroy(tree);
14734543Smarks }
14744543Smarks 
14754543Smarks static void
14764543Smarks zfs_destroy_tree(avl_tree_t *tree)
14774543Smarks {
14784543Smarks 	zfs_allow_node_t *allownode;
14795367Sahrens 	void *cookie = NULL;
14805367Sahrens 
14814543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
14824543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
14834543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
14844543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
14854543Smarks 		free(allownode);
14864543Smarks 	}
14875367Sahrens 	avl_destroy(tree);
14884543Smarks }
14894543Smarks 
14904543Smarks void
14914543Smarks zfs_free_allows(zfs_allow_t *allow)
14924543Smarks {
14934543Smarks 	zfs_allow_t *allownext;
14944543Smarks 	zfs_allow_t *freeallow;
14954543Smarks 
14964543Smarks 	allownext = allow;
14974543Smarks 	while (allownext) {
14984543Smarks 		zfs_destroy_tree(&allownext->z_sets);
14994543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
15004543Smarks 		zfs_destroy_tree(&allownext->z_user);
15014543Smarks 		zfs_destroy_tree(&allownext->z_group);
15024543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
15034543Smarks 		freeallow = allownext;
15044543Smarks 		allownext = allownext->z_next;
15054543Smarks 		free(freeallow);
15064543Smarks 	}
15074543Smarks }
15084543Smarks 
15094543Smarks static zfs_allow_t *
15104543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
15114543Smarks {
15124543Smarks 	zfs_allow_t *ptree;
15134543Smarks 
15144543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
15154543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
15164543Smarks 		return (NULL);
15174543Smarks 	}
15184543Smarks 
15194543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
15204543Smarks 	avl_create(&ptree->z_sets,
15214543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15224543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15234543Smarks 	avl_create(&ptree->z_crperms,
15244543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15254543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15264543Smarks 	avl_create(&ptree->z_user,
15274543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15284543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15294543Smarks 	avl_create(&ptree->z_group,
15304543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15314543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15324543Smarks 	avl_create(&ptree->z_everyone,
15334543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
15344543Smarks 	    offsetof(zfs_allow_node_t, z_node));
15354543Smarks 
15364543Smarks 	if (prev)
15374543Smarks 		prev->z_next = ptree;
15384543Smarks 	ptree->z_next = NULL;
15394543Smarks 	return (ptree);
15404543Smarks }
15414543Smarks 
15424543Smarks /*
15434543Smarks  * Add permissions to the appropriate AVL permission tree.
15444543Smarks  * The appropriate tree may not be the requested tree.
15454543Smarks  * For example if ld indicates a local permission, but
15464543Smarks  * same permission also exists as a descendent permission
15474543Smarks  * then the permission will be removed from the descendent
15484543Smarks  * tree and add the the local+descendent tree.
15494543Smarks  */
15504543Smarks static int
15514543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
15524543Smarks     char *perm, char ld)
15534543Smarks {
15544543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
15554543Smarks 	zfs_perm_node_t *newnode;
15564543Smarks 	avl_index_t where, where2;
15574543Smarks 	avl_tree_t *tree, *altree;
15584543Smarks 
15594543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
15604543Smarks 
15614543Smarks 	if (ld == ZFS_DELEG_NA) {
15624543Smarks 		tree =  &allownode->z_localdescend;
15634543Smarks 		altree = &allownode->z_descend;
15644543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
15654543Smarks 		tree = &allownode->z_local;
15664543Smarks 		altree = &allownode->z_descend;
15674543Smarks 	} else {
15684543Smarks 		tree = &allownode->z_descend;
15694543Smarks 		altree = &allownode->z_local;
15704543Smarks 	}
15714543Smarks 	permnode = avl_find(tree, &pnode, &where);
15724543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
15734543Smarks 
15744543Smarks 	if (permnode2) {
15754543Smarks 		avl_remove(altree, permnode2);
15764543Smarks 		free(permnode2);
15774543Smarks 		if (permnode == NULL) {
15784543Smarks 			tree =  &allownode->z_localdescend;
15794543Smarks 		}
15804543Smarks 	}
15814543Smarks 
15824543Smarks 	/*
15834543Smarks 	 * Now insert new permission in either requested location
15844543Smarks 	 * local/descendent or into ld when perm will exist in both.
15854543Smarks 	 */
15864543Smarks 	if (permnode == NULL) {
15874543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
15884543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
15894543Smarks 			return (-1);
15904543Smarks 		}
15914543Smarks 		*newnode = pnode;
15924543Smarks 		avl_add(tree, newnode);
15934543Smarks 	}
15944543Smarks 	return (0);
15954543Smarks }
15964577Sahrens 
15974543Smarks /*
15984543Smarks  * Uggh, this is going to be a bit complicated.
15994543Smarks  * we have an nvlist coming out of the kernel that
16004543Smarks  * will indicate where the permission is set and then
16014543Smarks  * it will contain allow of the various "who's", and what
16024543Smarks  * their permissions are.  To further complicate this
16034543Smarks  * we will then have to coalesce the local,descendent
16044543Smarks  * and local+descendent permissions where appropriate.
16054543Smarks  * The kernel only knows about a permission as being local
16064543Smarks  * or descendent, but not both.
16074543Smarks  *
16084543Smarks  * In order to make this easier for zfs_main to deal with
16094543Smarks  * a series of AVL trees will be used to maintain
16104543Smarks  * all of this, primarily for sorting purposes as well
16114543Smarks  * as the ability to quickly locate a specific entry.
16124543Smarks  *
16134543Smarks  * What we end up with are tree's for sets, create perms,
16144543Smarks  * user, groups and everyone.  With each of those trees
16154543Smarks  * we have subtrees for local, descendent and local+descendent
16164543Smarks  * permissions.
16174543Smarks  */
16184543Smarks int
16194543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
16204543Smarks {
16214543Smarks 	zfs_cmd_t zc = { 0 };
16224543Smarks 	int error;
16234543Smarks 	nvlist_t *nvlist;
16244543Smarks 	nvlist_t *permnv, *sourcenv;
16254543Smarks 	nvpair_t *who_pair, *source_pair;
16264543Smarks 	nvpair_t *perm_pair;
16274543Smarks 	char errbuf[1024];
16284543Smarks 	zfs_allow_t *zallowp, *newallowp;
16294543Smarks 	char  ld;
16304543Smarks 	char *nvpname;
16314543Smarks 	uid_t	uid;
16324543Smarks 	gid_t	gid;
16334543Smarks 	avl_tree_t *tree;
16344543Smarks 	avl_index_t where;
16354543Smarks 
16364543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16374543Smarks 
16384543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
16394543Smarks 		return (-1);
16404543Smarks 
16414543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
16424543Smarks 		if (errno == ENOMEM) {
16434543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
16444543Smarks 				zcmd_free_nvlists(&zc);
16454543Smarks 				return (-1);
16464543Smarks 			}
16474543Smarks 		} else if (errno == ENOTSUP) {
16484543Smarks 			zcmd_free_nvlists(&zc);
16494543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
16504543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
16514543Smarks 			return (zfs_error(zhp->zfs_hdl,
16524543Smarks 			    EZFS_BADVERSION, errbuf));
16534543Smarks 		} else {
16544543Smarks 			zcmd_free_nvlists(&zc);
16554543Smarks 			return (-1);
16564543Smarks 		}
16574543Smarks 	}
16584543Smarks 
16594543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
16604543Smarks 		zcmd_free_nvlists(&zc);
16614543Smarks 		return (-1);
16624543Smarks 	}
16634543Smarks 
16644543Smarks 	zcmd_free_nvlists(&zc);
16654543Smarks 
16664543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
16674543Smarks 
16684543Smarks 	if (source_pair == NULL) {
16694543Smarks 		*zfs_perms = NULL;
16704543Smarks 		return (0);
16714543Smarks 	}
16724543Smarks 
16734543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
16744543Smarks 	if (*zfs_perms == NULL) {
16754543Smarks 		return (0);
16764543Smarks 	}
16774543Smarks 
16784543Smarks 	zallowp = *zfs_perms;
16794543Smarks 
16804543Smarks 	for (;;) {
16814543Smarks 		struct passwd *pwd;
16824543Smarks 		struct group *grp;
16834543Smarks 		zfs_allow_node_t *allownode;
16844543Smarks 		zfs_allow_node_t  findallownode;
16854543Smarks 		zfs_allow_node_t *newallownode;
16864543Smarks 
16874543Smarks 		(void) strlcpy(zallowp->z_setpoint,
16884543Smarks 		    nvpair_name(source_pair),
16894543Smarks 		    sizeof (zallowp->z_setpoint));
16904543Smarks 
16914543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
16924543Smarks 			goto abort;
16934543Smarks 
16944543Smarks 		/*
16954543Smarks 		 * Make sure nvlist is composed correctly
16964543Smarks 		 */
16974543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
16984543Smarks 			goto abort;
16994543Smarks 		}
17004543Smarks 
17014543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
17024543Smarks 		if (who_pair == NULL) {
17034543Smarks 			goto abort;
17044543Smarks 		}
17054543Smarks 
17064543Smarks 		do {
17074543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
17084543Smarks 			if (error) {
17094543Smarks 				goto abort;
17104543Smarks 			}
17114543Smarks 
17124543Smarks 			/*
17134543Smarks 			 * First build up the key to use
17144543Smarks 			 * for looking up in the various
17154543Smarks 			 * who trees.
17164543Smarks 			 */
17174543Smarks 			ld = nvpair_name(who_pair)[1];
17184543Smarks 			nvpname = nvpair_name(who_pair);
17194543Smarks 			switch (nvpair_name(who_pair)[0]) {
17204543Smarks 			case ZFS_DELEG_USER:
17214543Smarks 			case ZFS_DELEG_USER_SETS:
17224543Smarks 				tree = &zallowp->z_user;
17234543Smarks 				uid = atol(&nvpname[3]);
17244543Smarks 				pwd = getpwuid(uid);
17254543Smarks 				(void) snprintf(findallownode.z_key,
17264543Smarks 				    sizeof (findallownode.z_key), "user %s",
17274543Smarks 				    (pwd) ? pwd->pw_name :
17284543Smarks 				    &nvpair_name(who_pair)[3]);
17294543Smarks 				break;
17304543Smarks 			case ZFS_DELEG_GROUP:
17314543Smarks 			case ZFS_DELEG_GROUP_SETS:
17324543Smarks 				tree = &zallowp->z_group;
17334543Smarks 				gid = atol(&nvpname[3]);
17344543Smarks 				grp = getgrgid(gid);
17354543Smarks 				(void) snprintf(findallownode.z_key,
17364543Smarks 				    sizeof (findallownode.z_key), "group %s",
17374543Smarks 				    (grp) ? grp->gr_name :
17384543Smarks 				    &nvpair_name(who_pair)[3]);
17394543Smarks 				break;
17404543Smarks 			case ZFS_DELEG_CREATE:
17414543Smarks 			case ZFS_DELEG_CREATE_SETS:
17424543Smarks 				tree = &zallowp->z_crperms;
17434543Smarks 				(void) strlcpy(findallownode.z_key, "",
17444543Smarks 				    sizeof (findallownode.z_key));
17454543Smarks 				break;
17464543Smarks 			case ZFS_DELEG_EVERYONE:
17474543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
17484543Smarks 				(void) snprintf(findallownode.z_key,
17494543Smarks 				    sizeof (findallownode.z_key), "everyone");
17504543Smarks 				tree = &zallowp->z_everyone;
17514543Smarks 				break;
17524543Smarks 			case ZFS_DELEG_NAMED_SET:
17534543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
17544543Smarks 				(void) snprintf(findallownode.z_key,
17554543Smarks 				    sizeof (findallownode.z_key), "%s",
17564543Smarks 				    &nvpair_name(who_pair)[3]);
17574543Smarks 				tree = &zallowp->z_sets;
17584543Smarks 				break;
17594543Smarks 			}
17604543Smarks 
17614543Smarks 			/*
17624543Smarks 			 * Place who in tree
17634543Smarks 			 */
17644543Smarks 			allownode = avl_find(tree, &findallownode, &where);
17654543Smarks 			if (allownode == NULL) {
17664543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
17674543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
17684543Smarks 					goto abort;
17694543Smarks 				}
17704543Smarks 				avl_create(&newallownode->z_localdescend,
17714543Smarks 				    perm_compare,
17724543Smarks 				    sizeof (zfs_perm_node_t),
17734543Smarks 				    offsetof(zfs_perm_node_t, z_node));
17744543Smarks 				avl_create(&newallownode->z_local,
17754543Smarks 				    perm_compare,
17764543Smarks 				    sizeof (zfs_perm_node_t),
17774543Smarks 				    offsetof(zfs_perm_node_t, z_node));
17784543Smarks 				avl_create(&newallownode->z_descend,
17794543Smarks 				    perm_compare,
17804543Smarks 				    sizeof (zfs_perm_node_t),
17814543Smarks 				    offsetof(zfs_perm_node_t, z_node));
17824543Smarks 				(void) strlcpy(newallownode->z_key,
17834543Smarks 				    findallownode.z_key,
17844543Smarks 				    sizeof (findallownode.z_key));
17854543Smarks 				avl_insert(tree, newallownode, where);
17864543Smarks 				allownode = newallownode;
17874543Smarks 			}
17884543Smarks 
17894543Smarks 			/*
17904543Smarks 			 * Now iterate over the permissions and
17914543Smarks 			 * place them in the appropriate local,
17924543Smarks 			 * descendent or local+descendent tree.
17934543Smarks 			 *
17944543Smarks 			 * The permissions are added to the tree
17954543Smarks 			 * via zfs_coalesce_perm().
17964543Smarks 			 */
17974543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
17984543Smarks 			if (perm_pair == NULL)
17994543Smarks 				goto abort;
18004543Smarks 			do {
18014543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
18024543Smarks 				    nvpair_name(perm_pair), ld) != 0)
18034543Smarks 					goto abort;
18044543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
18054543Smarks 			    perm_pair));
18064543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
18074543Smarks 
18084543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
18094543Smarks 		if (source_pair == NULL)
18104543Smarks 			break;
18114543Smarks 
18124543Smarks 		/*
18134543Smarks 		 * allocate another node from the link list of
18144543Smarks 		 * zfs_allow_t structures
18154543Smarks 		 */
18164543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
18174543Smarks 		    nvpair_name(source_pair));
18184543Smarks 		if (newallowp == NULL) {
18194543Smarks 			goto abort;
18204543Smarks 		}
18214543Smarks 		zallowp = newallowp;
18224543Smarks 	}
18234543Smarks 	nvlist_free(nvlist);
18244543Smarks 	return (0);
18254543Smarks abort:
18264543Smarks 	zfs_free_allows(*zfs_perms);
18274543Smarks 	nvlist_free(nvlist);
18284543Smarks 	return (-1);
18294543Smarks }
18304543Smarks 
18315993Smarks static char *
18325993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
18335993Smarks {
18345993Smarks 	/*
18355993Smarks 	 * Don't put newlines on end of lines
18365993Smarks 	 */
18375993Smarks 	switch (note) {
18385993Smarks 	case ZFS_DELEG_NOTE_CREATE:
18395993Smarks 		return (dgettext(TEXT_DOMAIN,
18405993Smarks 		    "Must also have the 'mount' ability"));
18415993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
18425993Smarks 		return (dgettext(TEXT_DOMAIN,
18435993Smarks 		    "Must also have the 'mount' ability"));
18445993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
18455993Smarks 		return (dgettext(TEXT_DOMAIN,
18465993Smarks 		    "Must also have the 'mount' ability"));
18475993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
18485993Smarks 		return (dgettext(TEXT_DOMAIN,
18495993Smarks 		    "Must also have the 'mount' ability"));
18505993Smarks 	case ZFS_DELEG_NOTE_CLONE:
18515993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
18525993Smarks 		    "ability and 'mount'\n"
18535993Smarks 		    "\t\t\t\tability in the origin file system"));
18545993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
18555993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
18565993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
18575993Smarks 	case ZFS_DELEG_NOTE_RENAME:
18585993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
18595993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
18605993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
18615993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
18625993Smarks 		    " and 'create' ability"));
18635993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
18645993Smarks 		return (dgettext(TEXT_DOMAIN,
18655993Smarks 		    "Allows changing any user property"));
18665993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
18675993Smarks 		return (dgettext(TEXT_DOMAIN,
18685993Smarks 		    "Must also have the permission that is being\n"
18695993Smarks 		    "\t\t\t\tallowed"));
18705993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
18715993Smarks 		return (dgettext(TEXT_DOMAIN,
18725993Smarks 		    "Allows mount/umount of ZFS datasets"));
18735993Smarks 	case ZFS_DELEG_NOTE_SHARE:
18745993Smarks 		return (dgettext(TEXT_DOMAIN,
18755993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
18765993Smarks 		    "\t\t\t\tprotocols"));
18775993Smarks 	case ZFS_DELEG_NOTE_NONE:
18785993Smarks 	default:
18795993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
18805993Smarks 	}
18815993Smarks }
18825993Smarks 
18835993Smarks typedef enum {
18845993Smarks 	ZFS_DELEG_SUBCOMMAND,
18855993Smarks 	ZFS_DELEG_PROP,
18865993Smarks 	ZFS_DELEG_OTHER
18875993Smarks } zfs_deleg_perm_type_t;
18885993Smarks 
18895993Smarks /*
18905993Smarks  * is the permission a subcommand or other?
18915993Smarks  */
18925993Smarks zfs_deleg_perm_type_t
18935993Smarks zfs_deleg_perm_type(const char *perm)
18945993Smarks {
18955993Smarks 	if (strcmp(perm, "userprop") == 0)
18965993Smarks 		return (ZFS_DELEG_OTHER);
18975993Smarks 	else
18985993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
18995993Smarks }
19005993Smarks 
19015993Smarks static char *
19025993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
19035993Smarks {
19045993Smarks 	switch (type) {
19055993Smarks 	case ZFS_DELEG_SUBCOMMAND:
19065993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
19075993Smarks 	case ZFS_DELEG_PROP:
19085993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
19095993Smarks 	case ZFS_DELEG_OTHER:
19105993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
19115993Smarks 	}
19125993Smarks 	return ("");
19135993Smarks }
19145993Smarks 
19155993Smarks /*ARGSUSED*/
19165993Smarks static int
19175993Smarks zfs_deleg_prop_cb(int prop, void *cb)
19185993Smarks {
19195993Smarks 	if (zfs_prop_delegatable(prop))
19205993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
19215993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
19225993Smarks 
19235993Smarks 	return (ZPROP_CONT);
19245993Smarks }
19255993Smarks 
19265993Smarks void
19275993Smarks zfs_deleg_permissions(void)
19285993Smarks {
19295993Smarks 	int i;
19305993Smarks 
19315993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
19325993Smarks 	    "TYPE", "NOTES");
19335993Smarks 
19345993Smarks 	/*
19355993Smarks 	 * First print out the subcommands
19365993Smarks 	 */
19375993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
19385993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
19395993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
19405993Smarks 		    zfs_deleg_perm_type_str(
19415993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
19425993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
19435993Smarks 	}
19445993Smarks 
19455993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
19465993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
19475993Smarks }
19485993Smarks 
1949789Sahrens /*
1950789Sahrens  * Given a property name and value, set the property for the given dataset.
1951789Sahrens  */
1952789Sahrens int
19532676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1954789Sahrens {
1955789Sahrens 	zfs_cmd_t zc = { 0 };
19562676Seschrock 	int ret = -1;
19572676Seschrock 	prop_changelist_t *cl = NULL;
19582082Seschrock 	char errbuf[1024];
19592082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
19602676Seschrock 	nvlist_t *nvl = NULL, *realprops;
19612676Seschrock 	zfs_prop_t prop;
19627509SMark.Musante@Sun.COM 	boolean_t do_prefix;
19637509SMark.Musante@Sun.COM 	uint64_t idx;
19642082Seschrock 
19652082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
19662676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
19672082Seschrock 	    zhp->zfs_name);
19682082Seschrock 
19692676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
19702676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
19712676Seschrock 		(void) no_memory(hdl);
19722676Seschrock 		goto error;
1973789Sahrens 	}
1974789Sahrens 
19757184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
19762676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
19772676Seschrock 		goto error;
19785094Slling 
19792676Seschrock 	nvlist_free(nvl);
19802676Seschrock 	nvl = realprops;
19812676Seschrock 
19822676Seschrock 	prop = zfs_name_to_prop(propname);
19832676Seschrock 
19847366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
19852676Seschrock 		goto error;
1986789Sahrens 
1987789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19882082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19892082Seschrock 		    "child dataset with inherited mountpoint is used "
19902082Seschrock 		    "in a non-global zone"));
19912082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1992789Sahrens 		goto error;
1993789Sahrens 	}
1994789Sahrens 
19957509SMark.Musante@Sun.COM 	/*
19967509SMark.Musante@Sun.COM 	 * If the dataset's canmount property is being set to noauto,
19977509SMark.Musante@Sun.COM 	 * then we want to prevent unmounting & remounting it.
19987509SMark.Musante@Sun.COM 	 */
19997509SMark.Musante@Sun.COM 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
20007509SMark.Musante@Sun.COM 	    (zprop_string_to_index(prop, propval, &idx,
20017509SMark.Musante@Sun.COM 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
20026168Shs24103 
20036168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
20047509SMark.Musante@Sun.COM 		goto error;
2005789Sahrens 
2006789Sahrens 	/*
2007789Sahrens 	 * Execute the corresponding ioctl() to set this property.
2008789Sahrens 	 */
2009789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2010789Sahrens 
20115094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
20122676Seschrock 		goto error;
20132676Seschrock 
20144543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
2015789Sahrens 	if (ret != 0) {
2016789Sahrens 		switch (errno) {
2017789Sahrens 
2018789Sahrens 		case ENOSPC:
2019789Sahrens 			/*
2020789Sahrens 			 * For quotas and reservations, ENOSPC indicates
2021789Sahrens 			 * something different; setting a quota or reservation
2022789Sahrens 			 * doesn't use any disk space.
2023789Sahrens 			 */
2024789Sahrens 			switch (prop) {
2025789Sahrens 			case ZFS_PROP_QUOTA:
20265378Sck153898 			case ZFS_PROP_REFQUOTA:
20272082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20282082Seschrock 				    "size is less than current used or "
20292082Seschrock 				    "reserved space"));
20302082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
2031789Sahrens 				break;
2032789Sahrens 
2033789Sahrens 			case ZFS_PROP_RESERVATION:
20345378Sck153898 			case ZFS_PROP_REFRESERVATION:
20352082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20362082Seschrock 				    "size is greater than available space"));
20372082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
2038789Sahrens 				break;
2039789Sahrens 
2040789Sahrens 			default:
20412082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
2042789Sahrens 				break;
2043789Sahrens 			}
2044789Sahrens 			break;
2045789Sahrens 
2046789Sahrens 		case EBUSY:
20472082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
20482082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
20492082Seschrock 			else
20502676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
2051789Sahrens 			break;
2052789Sahrens 
20531175Slling 		case EROFS:
20542082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
20551175Slling 			break;
20561175Slling 
20573886Sahl 		case ENOTSUP:
20583886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20595977Smarks 			    "pool and or dataset must be upgraded to set this "
20604603Sahrens 			    "property or value"));
20613886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
20623886Sahl 			break;
20633886Sahl 
20647042Sgw25295 		case ERANGE:
20657042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
20667042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
20677042Sgw25295 				    "property setting is not allowed on "
20687042Sgw25295 				    "bootable datasets"));
20697042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
20707042Sgw25295 			} else {
20717042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
20727042Sgw25295 			}
20737042Sgw25295 			break;
20747042Sgw25295 
2075789Sahrens 		case EOVERFLOW:
2076789Sahrens 			/*
2077789Sahrens 			 * This platform can't address a volume this big.
2078789Sahrens 			 */
2079789Sahrens #ifdef _ILP32
2080789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
20812082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
2082789Sahrens 				break;
2083789Sahrens 			}
2084789Sahrens #endif
20852082Seschrock 			/* FALLTHROUGH */
2086789Sahrens 		default:
20872082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
2088789Sahrens 		}
2089789Sahrens 	} else {
20906168Shs24103 		if (do_prefix)
20916168Shs24103 			ret = changelist_postfix(cl);
20926168Shs24103 
2093789Sahrens 		/*
2094789Sahrens 		 * Refresh the statistics so the new property value
2095789Sahrens 		 * is reflected.
2096789Sahrens 		 */
20976168Shs24103 		if (ret == 0)
20982676Seschrock 			(void) get_stats(zhp);
2099789Sahrens 	}
2100789Sahrens 
2101789Sahrens error:
21022676Seschrock 	nvlist_free(nvl);
21032676Seschrock 	zcmd_free_nvlists(&zc);
21042676Seschrock 	if (cl)
21052676Seschrock 		changelist_free(cl);
2106789Sahrens 	return (ret);
2107789Sahrens }
2108789Sahrens 
2109789Sahrens /*
2110789Sahrens  * Given a property, inherit the value from the parent dataset.
2111789Sahrens  */
2112789Sahrens int
21132676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
2114789Sahrens {
2115789Sahrens 	zfs_cmd_t zc = { 0 };
2116789Sahrens 	int ret;
2117789Sahrens 	prop_changelist_t *cl;
21182082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
21192082Seschrock 	char errbuf[1024];
21202676Seschrock 	zfs_prop_t prop;
21212082Seschrock 
21222082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
21232082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2124789Sahrens 
21255094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
21262676Seschrock 		/*
21272676Seschrock 		 * For user properties, the amount of work we have to do is very
21282676Seschrock 		 * small, so just do it here.
21292676Seschrock 		 */
21302676Seschrock 		if (!zfs_prop_user(propname)) {
21312676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21322676Seschrock 			    "invalid property"));
21332676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
21342676Seschrock 		}
21352676Seschrock 
21362676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21372676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
21382676Seschrock 
21394849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
21402676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
21412676Seschrock 
21422676Seschrock 		return (0);
21432676Seschrock 	}
21442676Seschrock 
2145789Sahrens 	/*
2146789Sahrens 	 * Verify that this property is inheritable.
2147789Sahrens 	 */
21482082Seschrock 	if (zfs_prop_readonly(prop))
21492082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
21502082Seschrock 
21512082Seschrock 	if (!zfs_prop_inheritable(prop))
21522082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2153789Sahrens 
2154789Sahrens 	/*
2155789Sahrens 	 * Check to see if the value applies to this type
2156789Sahrens 	 */
21572082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
21582082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2159789Sahrens 
21603443Srm160521 	/*
21613443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
21623443Srm160521 	 */
21633443Srm160521 	propname = zfs_prop_to_name(prop);
2164789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21652676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2166789Sahrens 
2167789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2168789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
21692082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21702082Seschrock 		    "dataset is used in a non-global zone"));
21712082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2172789Sahrens 	}
2173789Sahrens 
2174789Sahrens 	/*
2175789Sahrens 	 * Determine datasets which will be affected by this change, if any.
2176789Sahrens 	 */
21777366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2178789Sahrens 		return (-1);
2179789Sahrens 
2180789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
21812082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
21822082Seschrock 		    "child dataset with inherited mountpoint is used "
21832082Seschrock 		    "in a non-global zone"));
21842082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2185789Sahrens 		goto error;
2186789Sahrens 	}
2187789Sahrens 
2188789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
2189789Sahrens 		goto error;
2190789Sahrens 
21914849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
21922082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2193789Sahrens 	} else {
2194789Sahrens 
21952169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
2196789Sahrens 			goto error;
2197789Sahrens 
2198789Sahrens 		/*
2199789Sahrens 		 * Refresh the statistics so the new property is reflected.
2200789Sahrens 		 */
2201789Sahrens 		(void) get_stats(zhp);
2202789Sahrens 	}
2203789Sahrens 
2204789Sahrens error:
2205789Sahrens 	changelist_free(cl);
2206789Sahrens 	return (ret);
2207789Sahrens }
2208789Sahrens 
2209789Sahrens /*
22101356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
22111356Seschrock  * extract them appropriately.
22121356Seschrock  */
22131356Seschrock static uint64_t
22141356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
22151356Seschrock {
22161356Seschrock 	nvlist_t *nv;
22171356Seschrock 	uint64_t value;
22181356Seschrock 
22192885Sahrens 	*source = NULL;
22201356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
22211356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
22225094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
22235094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
22241356Seschrock 	} else {
22258802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
22268802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
22271356Seschrock 		value = zfs_prop_default_numeric(prop);
22281356Seschrock 		*source = "";
22291356Seschrock 	}
22301356Seschrock 
22311356Seschrock 	return (value);
22321356Seschrock }
22331356Seschrock 
22341356Seschrock static char *
22351356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
22361356Seschrock {
22371356Seschrock 	nvlist_t *nv;
22381356Seschrock 	char *value;
22391356Seschrock 
22402885Sahrens 	*source = NULL;
22411356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
22421356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
22435094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
22445094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
22451356Seschrock 	} else {
22468802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
22478802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
22481356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
22491356Seschrock 			value = "";
22501356Seschrock 		*source = "";
22511356Seschrock 	}
22521356Seschrock 
22531356Seschrock 	return (value);
22541356Seschrock }
22551356Seschrock 
22561356Seschrock /*
2257789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2258789Sahrens  * zfs_prop_get_int() are built using this interface.
2259789Sahrens  *
2260789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2261789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2262789Sahrens  * If they differ from the on-disk values, report the current values and mark
2263789Sahrens  * the source "temporary".
2264789Sahrens  */
22652082Seschrock static int
22665094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
22672082Seschrock     char **source, uint64_t *val)
2268789Sahrens {
22695147Srm160521 	zfs_cmd_t zc = { 0 };
22705592Stimh 	nvlist_t *zplprops = NULL;
2271789Sahrens 	struct mnttab mnt;
22723265Sahrens 	char *mntopt_on = NULL;
22733265Sahrens 	char *mntopt_off = NULL;
2274789Sahrens 
2275789Sahrens 	*source = NULL;
2276789Sahrens 
22773265Sahrens 	switch (prop) {
22783265Sahrens 	case ZFS_PROP_ATIME:
22793265Sahrens 		mntopt_on = MNTOPT_ATIME;
22803265Sahrens 		mntopt_off = MNTOPT_NOATIME;
22813265Sahrens 		break;
22823265Sahrens 
22833265Sahrens 	case ZFS_PROP_DEVICES:
22843265Sahrens 		mntopt_on = MNTOPT_DEVICES;
22853265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
22863265Sahrens 		break;
22873265Sahrens 
22883265Sahrens 	case ZFS_PROP_EXEC:
22893265Sahrens 		mntopt_on = MNTOPT_EXEC;
22903265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
22913265Sahrens 		break;
22923265Sahrens 
22933265Sahrens 	case ZFS_PROP_READONLY:
22943265Sahrens 		mntopt_on = MNTOPT_RO;
22953265Sahrens 		mntopt_off = MNTOPT_RW;
22963265Sahrens 		break;
22973265Sahrens 
22983265Sahrens 	case ZFS_PROP_SETUID:
22993265Sahrens 		mntopt_on = MNTOPT_SETUID;
23003265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
23013265Sahrens 		break;
23023265Sahrens 
23033265Sahrens 	case ZFS_PROP_XATTR:
23043265Sahrens 		mntopt_on = MNTOPT_XATTR;
23053265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
23063265Sahrens 		break;
23075331Samw 
23085331Samw 	case ZFS_PROP_NBMAND:
23095331Samw 		mntopt_on = MNTOPT_NBMAND;
23105331Samw 		mntopt_off = MNTOPT_NONBMAND;
23115331Samw 		break;
23123265Sahrens 	}
23133265Sahrens 
23142474Seschrock 	/*
23152474Seschrock 	 * Because looking up the mount options is potentially expensive
23162474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
23172474Seschrock 	 * we're looking up a property which requires its presence.
23182474Seschrock 	 */
23192474Seschrock 	if (!zhp->zfs_mntcheck &&
23203265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
23218228SEric.Taylor@Sun.COM 		libzfs_handle_t *hdl = zhp->zfs_hdl;
23228228SEric.Taylor@Sun.COM 		struct mnttab entry;
23238228SEric.Taylor@Sun.COM 
23248228SEric.Taylor@Sun.COM 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
23258228SEric.Taylor@Sun.COM 			zhp->zfs_mntopts = zfs_strdup(hdl,
23263265Sahrens 			    entry.mnt_mntopts);
23273265Sahrens 			if (zhp->zfs_mntopts == NULL)
23283265Sahrens 				return (-1);
23293265Sahrens 		}
23302474Seschrock 
23312474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
23322474Seschrock 	}
23332474Seschrock 
2334789Sahrens 	if (zhp->zfs_mntopts == NULL)
2335789Sahrens 		mnt.mnt_mntopts = "";
2336789Sahrens 	else
2337789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2338789Sahrens 
2339789Sahrens 	switch (prop) {
2340789Sahrens 	case ZFS_PROP_ATIME:
23413265Sahrens 	case ZFS_PROP_DEVICES:
23423265Sahrens 	case ZFS_PROP_EXEC:
23433265Sahrens 	case ZFS_PROP_READONLY:
23443265Sahrens 	case ZFS_PROP_SETUID:
23453265Sahrens 	case ZFS_PROP_XATTR:
23465331Samw 	case ZFS_PROP_NBMAND:
23472082Seschrock 		*val = getprop_uint64(zhp, prop, source);
23482082Seschrock 
23493265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
23502082Seschrock 			*val = B_TRUE;
2351789Sahrens 			if (src)
23525094Slling 				*src = ZPROP_SRC_TEMPORARY;
23533265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
23542082Seschrock 			*val = B_FALSE;
2355789Sahrens 			if (src)
23565094Slling 				*src = ZPROP_SRC_TEMPORARY;
2357789Sahrens 		}
23582082Seschrock 		break;
2359789Sahrens 
23603265Sahrens 	case ZFS_PROP_CANMOUNT:
23612082Seschrock 		*val = getprop_uint64(zhp, prop, source);
23626168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
23633417Srm160521 			*source = zhp->zfs_name;
23643417Srm160521 		else
23653417Srm160521 			*source = "";	/* default */
23662082Seschrock 		break;
2367789Sahrens 
2368789Sahrens 	case ZFS_PROP_QUOTA:
23695378Sck153898 	case ZFS_PROP_REFQUOTA:
2370789Sahrens 	case ZFS_PROP_RESERVATION:
23715378Sck153898 	case ZFS_PROP_REFRESERVATION:
23722885Sahrens 		*val = getprop_uint64(zhp, prop, source);
23732885Sahrens 		if (*val == 0)
2374789Sahrens 			*source = "";	/* default */
2375789Sahrens 		else
2376789Sahrens 			*source = zhp->zfs_name;
23772082Seschrock 		break;
2378789Sahrens 
2379789Sahrens 	case ZFS_PROP_MOUNTED:
23802082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
23812082Seschrock 		break;
2382789Sahrens 
23833377Seschrock 	case ZFS_PROP_NUMCLONES:
23843377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
23853377Seschrock 		break;
23863377Seschrock 
23875147Srm160521 	case ZFS_PROP_VERSION:
23885498Stimh 	case ZFS_PROP_NORMALIZE:
23895498Stimh 	case ZFS_PROP_UTF8ONLY:
23905498Stimh 	case ZFS_PROP_CASE:
23915498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
23925498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
23935473Srm160521 			return (-1);
23945147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
23955498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
23965498Stimh 			zcmd_free_nvlists(&zc);
23975147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
23985498Stimh 			    "unable to get %s property"),
23995498Stimh 			    zfs_prop_to_name(prop));
24005147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
24015147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
24025147Srm160521 		}
24035498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
24045498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
24055498Stimh 		    val) != 0) {
24065498Stimh 			zcmd_free_nvlists(&zc);
24075498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
24085498Stimh 			    "unable to get %s property"),
24095498Stimh 			    zfs_prop_to_name(prop));
24105498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
24115498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
24125498Stimh 		}
24135592Stimh 		if (zplprops)
24145592Stimh 			nvlist_free(zplprops);
24155498Stimh 		zcmd_free_nvlists(&zc);
24165147Srm160521 		break;
24175147Srm160521 
2418789Sahrens 	default:
24194577Sahrens 		switch (zfs_prop_get_type(prop)) {
24204787Sahrens 		case PROP_TYPE_NUMBER:
24214787Sahrens 		case PROP_TYPE_INDEX:
24224577Sahrens 			*val = getprop_uint64(zhp, prop, source);
24237390SMatthew.Ahrens@Sun.COM 			/*
24247390SMatthew.Ahrens@Sun.COM 			 * If we tried to use a defalut value for a
24257390SMatthew.Ahrens@Sun.COM 			 * readonly property, it means that it was not
24267390SMatthew.Ahrens@Sun.COM 			 * present; return an error.
24277390SMatthew.Ahrens@Sun.COM 			 */
24287390SMatthew.Ahrens@Sun.COM 			if (zfs_prop_readonly(prop) &&
24297390SMatthew.Ahrens@Sun.COM 			    *source && (*source)[0] == '\0') {
24307390SMatthew.Ahrens@Sun.COM 				return (-1);
24317390SMatthew.Ahrens@Sun.COM 			}
24324577Sahrens 			break;
24334577Sahrens 
24344787Sahrens 		case PROP_TYPE_STRING:
24354577Sahrens 		default:
24364577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
24374577Sahrens 			    "cannot get non-numeric property"));
24384577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
24394577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
24404577Sahrens 		}
2441789Sahrens 	}
2442789Sahrens 
2443789Sahrens 	return (0);
2444789Sahrens }
2445789Sahrens 
2446789Sahrens /*
2447789Sahrens  * Calculate the source type, given the raw source string.
2448789Sahrens  */
2449789Sahrens static void
24505094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2451789Sahrens     char *statbuf, size_t statlen)
2452789Sahrens {
24535094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2454789Sahrens 		return;
2455789Sahrens 
2456789Sahrens 	if (source == NULL) {
24575094Slling 		*srctype = ZPROP_SRC_NONE;
2458789Sahrens 	} else if (source[0] == '\0') {
24595094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2460789Sahrens 	} else {
2461789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
24625094Slling 			*srctype = ZPROP_SRC_LOCAL;
2463789Sahrens 		} else {
2464789Sahrens 			(void) strlcpy(statbuf, source, statlen);
24655094Slling 			*srctype = ZPROP_SRC_INHERITED;
2466789Sahrens 		}
2467789Sahrens 	}
2468789Sahrens 
2469789Sahrens }
2470789Sahrens 
2471789Sahrens /*
2472789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2473789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2474789Sahrens  * human-readable form.
2475789Sahrens  *
2476789Sahrens  * Returns 0 on success, or -1 on error.
2477789Sahrens  */
2478789Sahrens int
2479789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
24805094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2481789Sahrens {
2482789Sahrens 	char *source = NULL;
2483789Sahrens 	uint64_t val;
2484789Sahrens 	char *str;
24852676Seschrock 	const char *strval;
2486789Sahrens 
2487789Sahrens 	/*
2488789Sahrens 	 * Check to see if this property applies to our object
2489789Sahrens 	 */
2490789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2491789Sahrens 		return (-1);
2492789Sahrens 
2493789Sahrens 	if (src)
24945094Slling 		*src = ZPROP_SRC_NONE;
2495789Sahrens 
2496789Sahrens 	switch (prop) {
2497789Sahrens 	case ZFS_PROP_CREATION:
2498789Sahrens 		/*
2499789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2500789Sahrens 		 * this into a string unless 'literal' is specified.
2501789Sahrens 		 */
2502789Sahrens 		{
25032885Sahrens 			val = getprop_uint64(zhp, prop, &source);
25042885Sahrens 			time_t time = (time_t)val;
2505789Sahrens 			struct tm t;
2506789Sahrens 
2507789Sahrens 			if (literal ||
2508789Sahrens 			    localtime_r(&time, &t) == NULL ||
2509789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2510789Sahrens 			    &t) == 0)
25112885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2512789Sahrens 		}
2513789Sahrens 		break;
2514789Sahrens 
2515789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2516789Sahrens 		/*
2517789Sahrens 		 * Getting the precise mountpoint can be tricky.
2518789Sahrens 		 *
2519789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2520789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2521789Sahrens 		 *    after our ancestor and append it to the inherited value.
2522789Sahrens 		 *
2523789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2524789Sahrens 		 * root to any values we return.
2525789Sahrens 		 */
25266865Srm160521 
25271356Seschrock 		str = getprop_string(zhp, prop, &source);
25281356Seschrock 
25296612Sgw25295 		if (str[0] == '/') {
25306865Srm160521 			char buf[MAXPATHLEN];
25316865Srm160521 			char *root = buf;
25321356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2533789Sahrens 
2534789Sahrens 			if (relpath[0] == '/')
2535789Sahrens 				relpath++;
25366612Sgw25295 
25376865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
25386865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
25396865Srm160521 			    (strcmp(root, "-") == 0))
25406865Srm160521 				root[0] = '\0';
25416612Sgw25295 			/*
25426612Sgw25295 			 * Special case an alternate root of '/'. This will
25436612Sgw25295 			 * avoid having multiple leading slashes in the
25446612Sgw25295 			 * mountpoint path.
25456612Sgw25295 			 */
25466612Sgw25295 			if (strcmp(root, "/") == 0)
25476612Sgw25295 				root++;
25486612Sgw25295 
25496612Sgw25295 			/*
25506612Sgw25295 			 * If the mountpoint is '/' then skip over this
25516612Sgw25295 			 * if we are obtaining either an alternate root or
25526612Sgw25295 			 * an inherited mountpoint.
25536612Sgw25295 			 */
25546612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
25556612Sgw25295 			    relpath[0] != '\0'))
25561356Seschrock 				str++;
2557789Sahrens 
2558789Sahrens 			if (relpath[0] == '\0')
2559789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
25601356Seschrock 				    root, str);
2561789Sahrens 			else
2562789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
25631356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2564789Sahrens 				    relpath);
2565789Sahrens 		} else {
2566789Sahrens 			/* 'legacy' or 'none' */
25671356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2568789Sahrens 		}
2569789Sahrens 
2570789Sahrens 		break;
2571789Sahrens 
2572789Sahrens 	case ZFS_PROP_ORIGIN:
25732885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2574789Sahrens 		    proplen);
2575789Sahrens 		/*
2576789Sahrens 		 * If there is no parent at all, return failure to indicate that
2577789Sahrens 		 * it doesn't apply to this dataset.
2578789Sahrens 		 */
2579789Sahrens 		if (propbuf[0] == '\0')
2580789Sahrens 			return (-1);
2581789Sahrens 		break;
2582789Sahrens 
2583789Sahrens 	case ZFS_PROP_QUOTA:
25845378Sck153898 	case ZFS_PROP_REFQUOTA:
2585789Sahrens 	case ZFS_PROP_RESERVATION:
25865378Sck153898 	case ZFS_PROP_REFRESERVATION:
25875378Sck153898 
25882082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
25892082Seschrock 			return (-1);
2590789Sahrens 
2591789Sahrens 		/*
2592789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2593789Sahrens 		 * (unless literal is set), and indicate that it's the default
2594789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2595789Sahrens 		 * that its set locally.
2596789Sahrens 		 */
2597789Sahrens 		if (val == 0) {
2598789Sahrens 			if (literal)
2599789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2600789Sahrens 			else
2601789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2602789Sahrens 		} else {
2603789Sahrens 			if (literal)
26042856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
26053912Slling 				    (u_longlong_t)val);
2606789Sahrens 			else
2607789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2608789Sahrens 		}
2609789Sahrens 		break;
2610789Sahrens 
2611789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
26122082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
26132082Seschrock 			return (-1);
26142856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
26152856Snd150628 		    val / 100, (longlong_t)val % 100);
2616789Sahrens 		break;
2617789Sahrens 
2618789Sahrens 	case ZFS_PROP_TYPE:
2619789Sahrens 		switch (zhp->zfs_type) {
2620789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2621789Sahrens 			str = "filesystem";
2622789Sahrens 			break;
2623789Sahrens 		case ZFS_TYPE_VOLUME:
2624789Sahrens 			str = "volume";
2625789Sahrens 			break;
2626789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2627789Sahrens 			str = "snapshot";
2628789Sahrens 			break;
2629789Sahrens 		default:
26302082Seschrock 			abort();
2631789Sahrens 		}
2632789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2633789Sahrens 		break;
2634789Sahrens 
2635789Sahrens 	case ZFS_PROP_MOUNTED:
2636789Sahrens 		/*
2637789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2638789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2639789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2640789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2641789Sahrens 		 */
26422082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
26432082Seschrock 		    src, &source, &val) != 0)
26442082Seschrock 			return (-1);
26452082Seschrock 		if (val)
2646789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2647789Sahrens 		else
2648789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2649789Sahrens 		break;
2650789Sahrens 
2651789Sahrens 	case ZFS_PROP_NAME:
2652789Sahrens 		/*
2653789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2654789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2655789Sahrens 		 * consumers.
2656789Sahrens 		 */
2657789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2658789Sahrens 		break;
2659789Sahrens 
2660789Sahrens 	default:
26614577Sahrens 		switch (zfs_prop_get_type(prop)) {
26624787Sahrens 		case PROP_TYPE_NUMBER:
26634577Sahrens 			if (get_numeric_property(zhp, prop, src,
26644577Sahrens 			    &source, &val) != 0)
26654577Sahrens 				return (-1);
26664577Sahrens 			if (literal)
26674577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
26684577Sahrens 				    (u_longlong_t)val);
26694577Sahrens 			else
26704577Sahrens 				zfs_nicenum(val, propbuf, proplen);
26714577Sahrens 			break;
26724577Sahrens 
26734787Sahrens 		case PROP_TYPE_STRING:
26744577Sahrens 			(void) strlcpy(propbuf,
26754577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
26764577Sahrens 			break;
26774577Sahrens 
26784787Sahrens 		case PROP_TYPE_INDEX:
26794861Sahrens 			if (get_numeric_property(zhp, prop, src,
26804861Sahrens 			    &source, &val) != 0)
26814861Sahrens 				return (-1);
26824861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
26834577Sahrens 				return (-1);
26844577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
26854577Sahrens 			break;
26864577Sahrens 
26874577Sahrens 		default:
26884577Sahrens 			abort();
26894577Sahrens 		}
2690789Sahrens 	}
2691789Sahrens 
2692789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2693789Sahrens 
2694789Sahrens 	return (0);
2695789Sahrens }
2696789Sahrens 
2697789Sahrens /*
2698789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2699789Sahrens  * the given property is the appropriate type; should only be used with
2700789Sahrens  * hard-coded property types.
2701789Sahrens  */
2702789Sahrens uint64_t
2703789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2704789Sahrens {
2705789Sahrens 	char *source;
27062082Seschrock 	uint64_t val;
27072082Seschrock 
27085367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
27092082Seschrock 
27102082Seschrock 	return (val);
2711789Sahrens }
2712789Sahrens 
27135713Srm160521 int
27145713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
27155713Srm160521 {
27165713Srm160521 	char buf[64];
27175713Srm160521 
27185713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
27195713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
27205713Srm160521 }
27215713Srm160521 
2722789Sahrens /*
2723789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2724789Sahrens  */
2725789Sahrens int
2726789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
27275094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2728789Sahrens {
2729789Sahrens 	char *source;
2730789Sahrens 
2731789Sahrens 	/*
2732789Sahrens 	 * Check to see if this property applies to our object
2733789Sahrens 	 */
27344849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
27353237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
27362082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
27372082Seschrock 		    zfs_prop_to_name(prop)));
27384849Sahrens 	}
2739789Sahrens 
2740789Sahrens 	if (src)
27415094Slling 		*src = ZPROP_SRC_NONE;
2742789Sahrens 
27432082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
27442082Seschrock 		return (-1);
2745789Sahrens 
2746789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2747789Sahrens 
2748789Sahrens 	return (0);
2749789Sahrens }
2750789Sahrens 
2751789Sahrens /*
2752789Sahrens  * Returns the name of the given zfs handle.
2753789Sahrens  */
2754789Sahrens const char *
2755789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2756789Sahrens {
2757789Sahrens 	return (zhp->zfs_name);
2758789Sahrens }
2759789Sahrens 
2760789Sahrens /*
2761789Sahrens  * Returns the type of the given zfs handle.
2762789Sahrens  */
2763789Sahrens zfs_type_t
2764789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2765789Sahrens {
2766789Sahrens 	return (zhp->zfs_type);
2767789Sahrens }
2768789Sahrens 
27698228SEric.Taylor@Sun.COM static int
27708228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
27718228SEric.Taylor@Sun.COM {
27728228SEric.Taylor@Sun.COM 	int rc;
27738228SEric.Taylor@Sun.COM 	uint64_t	orig_cookie;
27748228SEric.Taylor@Sun.COM 
27758228SEric.Taylor@Sun.COM 	orig_cookie = zc->zc_cookie;
27768228SEric.Taylor@Sun.COM top:
27778228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
27788228SEric.Taylor@Sun.COM 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
27798228SEric.Taylor@Sun.COM 
27808228SEric.Taylor@Sun.COM 	if (rc == -1) {
27818228SEric.Taylor@Sun.COM 		switch (errno) {
27828228SEric.Taylor@Sun.COM 		case ENOMEM:
27838228SEric.Taylor@Sun.COM 			/* expand nvlist memory and try again */
27848228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
27858228SEric.Taylor@Sun.COM 				zcmd_free_nvlists(zc);
27868228SEric.Taylor@Sun.COM 				return (-1);
27878228SEric.Taylor@Sun.COM 			}
27888228SEric.Taylor@Sun.COM 			zc->zc_cookie = orig_cookie;
27898228SEric.Taylor@Sun.COM 			goto top;
27908228SEric.Taylor@Sun.COM 		/*
27918228SEric.Taylor@Sun.COM 		 * An errno value of ESRCH indicates normal completion.
27928228SEric.Taylor@Sun.COM 		 * If ENOENT is returned, then the underlying dataset
27938228SEric.Taylor@Sun.COM 		 * has been removed since we obtained the handle.
27948228SEric.Taylor@Sun.COM 		 */
27958228SEric.Taylor@Sun.COM 		case ESRCH:
27968228SEric.Taylor@Sun.COM 		case ENOENT:
27978228SEric.Taylor@Sun.COM 			rc = 1;
27988228SEric.Taylor@Sun.COM 			break;
27998228SEric.Taylor@Sun.COM 		default:
28008228SEric.Taylor@Sun.COM 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
28018228SEric.Taylor@Sun.COM 			    dgettext(TEXT_DOMAIN,
28028228SEric.Taylor@Sun.COM 			    "cannot iterate filesystems"));
28038228SEric.Taylor@Sun.COM 			break;
28048228SEric.Taylor@Sun.COM 		}
28058228SEric.Taylor@Sun.COM 	}
28068228SEric.Taylor@Sun.COM 	return (rc);
28078228SEric.Taylor@Sun.COM }
28088228SEric.Taylor@Sun.COM 
2809789Sahrens /*
28101356Seschrock  * Iterate over all child filesystems
2811789Sahrens  */
2812789Sahrens int
28131356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2814789Sahrens {
2815789Sahrens 	zfs_cmd_t zc = { 0 };
2816789Sahrens 	zfs_handle_t *nzhp;
2817789Sahrens 	int ret;
2818789Sahrens 
28195367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
28205367Sahrens 		return (0);
28215367Sahrens 
28228228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
28238228SEric.Taylor@Sun.COM 		return (-1);
28248228SEric.Taylor@Sun.COM 
28258228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
28268228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
2827789Sahrens 		/*
2828789Sahrens 		 * Ignore private dataset names.
2829789Sahrens 		 */
2830789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2831789Sahrens 			continue;
2832789Sahrens 
2833789Sahrens 		/*
2834789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2835789Sahrens 		 * that the pool has since been removed.
2836789Sahrens 		 */
28378228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
28388228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2839789Sahrens 			continue;
28408228SEric.Taylor@Sun.COM 		}
28418228SEric.Taylor@Sun.COM 
28428228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
28438228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2844789Sahrens 			return (ret);
28458228SEric.Taylor@Sun.COM 		}
2846789Sahrens 	}
28478228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
28488228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
28491356Seschrock }
28501356Seschrock 
28511356Seschrock /*
28521356Seschrock  * Iterate over all snapshots
28531356Seschrock  */
28541356Seschrock int
28551356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
28561356Seschrock {
28571356Seschrock 	zfs_cmd_t zc = { 0 };
28581356Seschrock 	zfs_handle_t *nzhp;
28591356Seschrock 	int ret;
2860789Sahrens 
28615367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
28625367Sahrens 		return (0);
28635367Sahrens 
28648228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
28658228SEric.Taylor@Sun.COM 		return (-1);
28668228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
28678228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
28688228SEric.Taylor@Sun.COM 
28698228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
28708228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2871789Sahrens 			continue;
28728228SEric.Taylor@Sun.COM 		}
28738228SEric.Taylor@Sun.COM 
28748228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
28758228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2876789Sahrens 			return (ret);
28778228SEric.Taylor@Sun.COM 		}
2878789Sahrens 	}
28798228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
28808228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
2881789Sahrens }
2882789Sahrens 
2883789Sahrens /*
28841356Seschrock  * Iterate over all children, snapshots and filesystems
28851356Seschrock  */
28861356Seschrock int
28871356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
28881356Seschrock {
28891356Seschrock 	int ret;
28901356Seschrock 
28911356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
28921356Seschrock 		return (ret);
28931356Seschrock 
28941356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
28951356Seschrock }
28961356Seschrock 
28971356Seschrock /*
2898789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2899789Sahrens  * Can return NULL if this is a pool.
2900789Sahrens  */
2901789Sahrens static int
2902789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2903789Sahrens {
2904789Sahrens 	char *loc;
2905789Sahrens 
2906789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2907789Sahrens 		return (-1);
2908789Sahrens 
2909789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2910789Sahrens 	buf[loc - path] = '\0';
2911789Sahrens 
2912789Sahrens 	return (0);
2913789Sahrens }
2914789Sahrens 
2915789Sahrens /*
29164490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
29174490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
29184490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
29194490Svb160487  * length of already existing prefix of the given path.  We also fetch the
29204490Svb160487  * 'zoned' property, which is used to validate property settings when creating
29214490Svb160487  * new datasets.
2922789Sahrens  */
2923789Sahrens static int
29244490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
29254490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2926789Sahrens {
2927789Sahrens 	zfs_cmd_t zc = { 0 };
2928789Sahrens 	char parent[ZFS_MAXNAMELEN];
2929789Sahrens 	char *slash;
29301356Seschrock 	zfs_handle_t *zhp;
29312082Seschrock 	char errbuf[1024];
29322082Seschrock 
29338269SMark.Musante@Sun.COM 	(void) snprintf(errbuf, sizeof (errbuf),
29348269SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2935789Sahrens 
2936789Sahrens 	/* get parent, and check to see if this is just a pool */
2937789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
29382082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29392082Seschrock 		    "missing dataset name"));
29402082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2941789Sahrens 	}
2942789Sahrens 
2943789Sahrens 	/* check to see if the pool exists */
2944789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2945789Sahrens 		slash = parent + strlen(parent);
2946789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2947789Sahrens 	zc.zc_name[slash - parent] = '\0';
29482082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2949789Sahrens 	    errno == ENOENT) {
29502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29512082Seschrock 		    "no such pool '%s'"), zc.zc_name);
29522082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2953789Sahrens 	}
2954789Sahrens 
2955789Sahrens 	/* check to see if the parent dataset exists */
29564490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
29574490Svb160487 		if (errno == ENOENT && accept_ancestor) {
29584490Svb160487 			/*
29594490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
29604490Svb160487 			 */
29614490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
29624490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29634490Svb160487 				    "no such pool '%s'"), zc.zc_name);
29644490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
29654490Svb160487 			}
29664490Svb160487 		} else if (errno == ENOENT) {
29672082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29682082Seschrock 			    "parent does not exist"));
29692082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
29704490Svb160487 		} else
29712082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2972789Sahrens 	}
2973789Sahrens 
29742676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2975789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
29762676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
29772082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
29781356Seschrock 		zfs_close(zhp);
2979789Sahrens 		return (-1);
2980789Sahrens 	}
2981789Sahrens 
2982789Sahrens 	/* make sure parent is a filesystem */
29831356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
29842082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29852082Seschrock 		    "parent is not a filesystem"));
29862082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
29871356Seschrock 		zfs_close(zhp);
2988789Sahrens 		return (-1);
2989789Sahrens 	}
2990789Sahrens 
29911356Seschrock 	zfs_close(zhp);
29924490Svb160487 	if (prefixlen != NULL)
29934490Svb160487 		*prefixlen = strlen(parent);
29944490Svb160487 	return (0);
29954490Svb160487 }
29964490Svb160487 
29974490Svb160487 /*
29984490Svb160487  * Finds whether the dataset of the given type(s) exists.
29994490Svb160487  */
30004490Svb160487 boolean_t
30014490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
30024490Svb160487 {
30034490Svb160487 	zfs_handle_t *zhp;
30044490Svb160487 
30055326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
30064490Svb160487 		return (B_FALSE);
30074490Svb160487 
30084490Svb160487 	/*
30094490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
30104490Svb160487 	 */
30114490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
30124490Svb160487 		int ds_type = zhp->zfs_type;
30134490Svb160487 
30144490Svb160487 		zfs_close(zhp);
30154490Svb160487 		if (types & ds_type)
30164490Svb160487 			return (B_TRUE);
30174490Svb160487 	}
30184490Svb160487 	return (B_FALSE);
30194490Svb160487 }
30204490Svb160487 
30214490Svb160487 /*
30225367Sahrens  * Given a path to 'target', create all the ancestors between
30235367Sahrens  * the prefixlen portion of the path, and the target itself.
30245367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
30255367Sahrens  */
30265367Sahrens int
30275367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
30285367Sahrens {
30295367Sahrens 	zfs_handle_t *h;
30305367Sahrens 	char *cp;
30315367Sahrens 	const char *opname;
30325367Sahrens 
30335367Sahrens 	/* make sure prefix exists */
30345367Sahrens 	cp = target + prefixlen;
30355367Sahrens 	if (*cp != '/') {
30365367Sahrens 		assert(strchr(cp, '/') == NULL);
30375367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
30385367Sahrens 	} else {
30395367Sahrens 		*cp = '\0';
30405367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
30415367Sahrens 		*cp = '/';
30425367Sahrens 	}
30435367Sahrens 	if (h == NULL)
30445367Sahrens 		return (-1);
30455367Sahrens 	zfs_close(h);
30465367Sahrens 
30475367Sahrens 	/*
30485367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
30495367Sahrens 	 * up to the prefixlen-long one.
30505367Sahrens 	 */
30515367Sahrens 	for (cp = target + prefixlen + 1;
30525367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
30535367Sahrens 		char *logstr;
30545367Sahrens 
30555367Sahrens 		*cp = '\0';
30565367Sahrens 
30575367Sahrens 		h = make_dataset_handle(hdl, target);
30585367Sahrens 		if (h) {
30595367Sahrens 			/* it already exists, nothing to do here */
30605367Sahrens 			zfs_close(h);
30615367Sahrens 			continue;
30625367Sahrens 		}
30635367Sahrens 
30645367Sahrens 		logstr = hdl->libzfs_log_str;
30655367Sahrens 		hdl->libzfs_log_str = NULL;
30665367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
30675367Sahrens 		    NULL) != 0) {
30685367Sahrens 			hdl->libzfs_log_str = logstr;
30695367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
30705367Sahrens 			goto ancestorerr;
30715367Sahrens 		}
30725367Sahrens 
30735367Sahrens 		hdl->libzfs_log_str = logstr;
30745367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
30755367Sahrens 		if (h == NULL) {
30765367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
30775367Sahrens 			goto ancestorerr;
30785367Sahrens 		}
30795367Sahrens 
30805367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
30815367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
30825367Sahrens 			goto ancestorerr;
30835367Sahrens 		}
30845367Sahrens 
30855367Sahrens 		if (zfs_share(h) != 0) {
30865367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
30875367Sahrens 			goto ancestorerr;
30885367Sahrens 		}
30895367Sahrens 
30905367Sahrens 		zfs_close(h);
30915367Sahrens 	}
30925367Sahrens 
30935367Sahrens 	return (0);
30945367Sahrens 
30955367Sahrens ancestorerr:
30965367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30975367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
30985367Sahrens 	return (-1);
30995367Sahrens }
31005367Sahrens 
31015367Sahrens /*
31024490Svb160487  * Creates non-existing ancestors of the given path.
31034490Svb160487  */
31044490Svb160487 int
31054490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
31064490Svb160487 {
31074490Svb160487 	int prefix;
31084490Svb160487 	uint64_t zoned;
31094490Svb160487 	char *path_copy;
31104490Svb160487 	int rc;
31114490Svb160487 
31124490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
31134490Svb160487 		return (-1);
31144490Svb160487 
31154490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
31164490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
31174490Svb160487 		free(path_copy);
31184490Svb160487 	}
31194490Svb160487 	if (path_copy == NULL || rc != 0)
31204490Svb160487 		return (-1);
31214490Svb160487 
3122789Sahrens 	return (0);
3123789Sahrens }
3124789Sahrens 
3125789Sahrens /*
31262676Seschrock  * Create a new filesystem or volume.
3127789Sahrens  */
3128789Sahrens int
31292082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
31302676Seschrock     nvlist_t *props)
3131789Sahrens {
3132789Sahrens 	zfs_cmd_t zc = { 0 };
3133789Sahrens 	int ret;
3134789Sahrens 	uint64_t size = 0;
3135789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
31362082Seschrock 	char errbuf[1024];
31372676Seschrock 	uint64_t zoned;
31382082Seschrock 
31392082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31402082Seschrock 	    "cannot create '%s'"), path);
3141789Sahrens 
3142789Sahrens 	/* validate the path, taking care to note the extended error message */
31435326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
31442082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3145789Sahrens 
3146789Sahrens 	/* validate parents exist */
31474490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3148789Sahrens 		return (-1);
3149789Sahrens 
3150789Sahrens 	/*
3151789Sahrens 	 * The failure modes when creating a dataset of a different type over
3152789Sahrens 	 * one that already exists is a little strange.  In particular, if you
3153789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
3154789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3155789Sahrens 	 * first try to see if the dataset exists.
3156789Sahrens 	 */
3157789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
31585094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
31592082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31602082Seschrock 		    "dataset already exists"));
31612082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3162789Sahrens 	}
3163789Sahrens 
3164789Sahrens 	if (type == ZFS_TYPE_VOLUME)
3165789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3166789Sahrens 	else
3167789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3168789Sahrens 
31697184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
31703912Slling 	    zoned, NULL, errbuf)) == 0)
31712676Seschrock 		return (-1);
31722676Seschrock 
3173789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
31741133Seschrock 		/*
31751133Seschrock 		 * If we are creating a volume, the size and block size must
31761133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
31771133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
31781133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
31791133Seschrock 		 * zero.
31801133Seschrock 		 */
31812676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
31822676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
31832676Seschrock 			nvlist_free(props);
31842082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31852676Seschrock 			    "missing volume size"));
31862676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3187789Sahrens 		}
3188789Sahrens 
31892676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
31902676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
31912676Seschrock 		    &blocksize)) != 0) {
31922676Seschrock 			if (ret == ENOENT) {
31932676Seschrock 				blocksize = zfs_prop_default_numeric(
31942676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
31952676Seschrock 			} else {
31962676Seschrock 				nvlist_free(props);
31972676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31982676Seschrock 				    "missing volume block size"));
31992676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
32002676Seschrock 			}
32012676Seschrock 		}
32022676Seschrock 
32032676Seschrock 		if (size == 0) {
32042676Seschrock 			nvlist_free(props);
32052082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32062676Seschrock 			    "volume size cannot be zero"));
32072676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
32081133Seschrock 		}
32091133Seschrock 
32101133Seschrock 		if (size % blocksize != 0) {
32112676Seschrock 			nvlist_free(props);
32122082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32132676Seschrock 			    "volume size must be a multiple of volume block "
32142676Seschrock 			    "size"));
32152676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
32161133Seschrock 		}
3217789Sahrens 	}
3218789Sahrens 
32195094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
32202676Seschrock 		return (-1);
32212676Seschrock 	nvlist_free(props);
32222676Seschrock 
3223789Sahrens 	/* create the dataset */
32244543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3225789Sahrens 
32263912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
32272082Seschrock 		ret = zvol_create_link(hdl, path);
32283912Slling 		if (ret) {
32293912Slling 			(void) zfs_standard_error(hdl, errno,
32303912Slling 			    dgettext(TEXT_DOMAIN,
32313912Slling 			    "Volume successfully created, but device links "
32323912Slling 			    "were not created"));
32333912Slling 			zcmd_free_nvlists(&zc);
32343912Slling 			return (-1);
32353912Slling 		}
32363912Slling 	}
3237789Sahrens 
32382676Seschrock 	zcmd_free_nvlists(&zc);
32392676Seschrock 
3240789Sahrens 	/* check for failure */
3241789Sahrens 	if (ret != 0) {
3242789Sahrens 		char parent[ZFS_MAXNAMELEN];
3243789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3244789Sahrens 
3245789Sahrens 		switch (errno) {
3246789Sahrens 		case ENOENT:
32472082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32482082Seschrock 			    "no such parent '%s'"), parent);
32492082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3250789Sahrens 
3251789Sahrens 		case EINVAL:
32522082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32533413Smmusante 			    "parent '%s' is not a filesystem"), parent);
32542082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3255789Sahrens 
3256789Sahrens 		case EDOM:
32572082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32582676Seschrock 			    "volume block size must be power of 2 from "
32592676Seschrock 			    "%u to %uk"),
3260789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3261789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
32622082Seschrock 
32632676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
32642082Seschrock 
32654603Sahrens 		case ENOTSUP:
32664603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32674603Sahrens 			    "pool must be upgraded to set this "
32684603Sahrens 			    "property or value"));
32694603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3270789Sahrens #ifdef _ILP32
3271789Sahrens 		case EOVERFLOW:
3272789Sahrens 			/*
3273789Sahrens 			 * This platform can't address a volume this big.
3274789Sahrens 			 */
32752082Seschrock 			if (type == ZFS_TYPE_VOLUME)
32762082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
32772082Seschrock 				    errbuf));
3278789Sahrens #endif
32792082Seschrock 			/* FALLTHROUGH */
3280789Sahrens 		default:
32812082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3282789Sahrens 		}
3283789Sahrens 	}
3284789Sahrens 
3285789Sahrens 	return (0);
3286789Sahrens }
3287789Sahrens 
3288789Sahrens /*
3289789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3290789Sahrens  * isn't mounted, and that there are no active dependents.
3291789Sahrens  */
3292789Sahrens int
3293789Sahrens zfs_destroy(zfs_handle_t *zhp)
3294789Sahrens {
3295789Sahrens 	zfs_cmd_t zc = { 0 };
3296789Sahrens 
3297789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3298789Sahrens 
32992676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
33003126Sahl 		/*
33014543Smarks 		 * If user doesn't have permissions to unshare volume, then
33024543Smarks 		 * abort the request.  This would only happen for a
33034543Smarks 		 * non-privileged user.
33043126Sahl 		 */
33054543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
33064543Smarks 			return (-1);
33074543Smarks 		}
33083126Sahl 
33092082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3310789Sahrens 			return (-1);
3311789Sahrens 
3312789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3313789Sahrens 	} else {
3314789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3315789Sahrens 	}
3316789Sahrens 
33174543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
33183237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
33192082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
33202082Seschrock 		    zhp->zfs_name));
33212199Sahrens 	}
3322789Sahrens 
3323789Sahrens 	remove_mountpoint(zhp);
3324789Sahrens 
3325789Sahrens 	return (0);
3326789Sahrens }
3327789Sahrens 
33282199Sahrens struct destroydata {
33292199Sahrens 	char *snapname;
33302199Sahrens 	boolean_t gotone;
33313265Sahrens 	boolean_t closezhp;
33322199Sahrens };
33332199Sahrens 
33342199Sahrens static int
33352199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
33362199Sahrens {
33372199Sahrens 	struct destroydata *dd = arg;
33382199Sahrens 	zfs_handle_t *szhp;
33392199Sahrens 	char name[ZFS_MAXNAMELEN];
33403265Sahrens 	boolean_t closezhp = dd->closezhp;
33413265Sahrens 	int rv;
33422199Sahrens 
33432676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
33442676Seschrock 	(void) strlcat(name, "@", sizeof (name));
33452676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
33462199Sahrens 
33472199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
33482199Sahrens 	if (szhp) {
33492199Sahrens 		dd->gotone = B_TRUE;
33502199Sahrens 		zfs_close(szhp);
33512199Sahrens 	}
33522199Sahrens 
33532199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33542199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
33552199Sahrens 		/*
33562199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
33572199Sahrens 		 * return an error, because then we wouldn't visit all
33582199Sahrens 		 * the volumes.
33592199Sahrens 		 */
33602199Sahrens 	}
33612199Sahrens 
33623265Sahrens 	dd->closezhp = B_TRUE;
33633265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
33643265Sahrens 	if (closezhp)
33653265Sahrens 		zfs_close(zhp);
33663265Sahrens 	return (rv);
33672199Sahrens }
33682199Sahrens 
33692199Sahrens /*
33702199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
33712199Sahrens  */
33722199Sahrens int
33732199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
33742199Sahrens {
33752199Sahrens 	zfs_cmd_t zc = { 0 };
33762199Sahrens 	int ret;
33772199Sahrens 	struct destroydata dd = { 0 };
33782199Sahrens 
33792199Sahrens 	dd.snapname = snapname;
33802199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
33812199Sahrens 
33822199Sahrens 	if (!dd.gotone) {
33833237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
33842199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
33852199Sahrens 		    zhp->zfs_name, snapname));
33862199Sahrens 	}
33872199Sahrens 
33882199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33892676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
33902199Sahrens 
33914543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
33922199Sahrens 	if (ret != 0) {
33932199Sahrens 		char errbuf[1024];
33942199Sahrens 
33952199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33962199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
33972199Sahrens 
33982199Sahrens 		switch (errno) {
33992199Sahrens 		case EEXIST:
34002199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
34012199Sahrens 			    "snapshot is cloned"));
34022199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
34032199Sahrens 
34042199Sahrens 		default:
34052199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
34062199Sahrens 			    errbuf));
34072199Sahrens 		}
34082199Sahrens 	}
34092199Sahrens 
34102199Sahrens 	return (0);
34112199Sahrens }
34122199Sahrens 
3413789Sahrens /*
3414789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3415789Sahrens  */
3416789Sahrens int
34172676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3418789Sahrens {
3419789Sahrens 	zfs_cmd_t zc = { 0 };
3420789Sahrens 	char parent[ZFS_MAXNAMELEN];
3421789Sahrens 	int ret;
34222082Seschrock 	char errbuf[1024];
34232082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34242676Seschrock 	zfs_type_t type;
34252676Seschrock 	uint64_t zoned;
3426789Sahrens 
3427789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3428789Sahrens 
34292082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34302082Seschrock 	    "cannot create '%s'"), target);
34312082Seschrock 
3432789Sahrens 	/* validate the target name */
34335326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
34342082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3435789Sahrens 
3436789Sahrens 	/* validate parents exist */
34374490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3438789Sahrens 		return (-1);
3439789Sahrens 
3440789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3441789Sahrens 
3442789Sahrens 	/* do the clone */
34432676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3444789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
34452744Snn35248 		type = ZFS_TYPE_VOLUME;
34462676Seschrock 	} else {
3447789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
34482744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
34492676Seschrock 	}
34502676Seschrock 
34512676Seschrock 	if (props) {
34527184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
34537184Stimh 		    zhp, errbuf)) == NULL)
34542676Seschrock 			return (-1);
34552676Seschrock 
34565094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
34572676Seschrock 			nvlist_free(props);
34582676Seschrock 			return (-1);
34592676Seschrock 		}
34602676Seschrock 
34612676Seschrock 		nvlist_free(props);
34622676Seschrock 	}
3463789Sahrens 
3464789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
34652676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
34664543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3467789Sahrens 
34682676Seschrock 	zcmd_free_nvlists(&zc);
34692676Seschrock 
3470789Sahrens 	if (ret != 0) {
3471789Sahrens 		switch (errno) {
3472789Sahrens 
3473789Sahrens 		case ENOENT:
3474789Sahrens 			/*
3475789Sahrens 			 * The parent doesn't exist.  We should have caught this
3476789Sahrens 			 * above, but there may a race condition that has since
3477789Sahrens 			 * destroyed the parent.
3478789Sahrens 			 *
3479789Sahrens 			 * At this point, we don't know whether it's the source
3480789Sahrens 			 * that doesn't exist anymore, or whether the target
3481789Sahrens 			 * dataset doesn't exist.
3482789Sahrens 			 */
34832082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
34842082Seschrock 			    "no such parent '%s'"), parent);
34852082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
34862082Seschrock 
34872082Seschrock 		case EXDEV:
34882082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
34892082Seschrock 			    "source and target pools differ"));
34902082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
34912082Seschrock 			    errbuf));
34922082Seschrock 
34932082Seschrock 		default:
34942082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
34952082Seschrock 			    errbuf));
34962082Seschrock 		}
34972676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
34982082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
34992082Seschrock 	}
35002082Seschrock 
35012082Seschrock 	return (ret);
35022082Seschrock }
35032082Seschrock 
35042082Seschrock typedef struct promote_data {
35052082Seschrock 	char cb_mountpoint[MAXPATHLEN];
35062082Seschrock 	const char *cb_target;
35072082Seschrock 	const char *cb_errbuf;
35082082Seschrock 	uint64_t cb_pivot_txg;
35092082Seschrock } promote_data_t;
35102082Seschrock 
35112082Seschrock static int
35122082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
35132082Seschrock {
35142082Seschrock 	promote_data_t *pd = data;
35152082Seschrock 	zfs_handle_t *szhp;
35162082Seschrock 	char snapname[MAXPATHLEN];
35173265Sahrens 	int rv = 0;
35182082Seschrock 
35192082Seschrock 	/* We don't care about snapshots after the pivot point */
35203265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
35213265Sahrens 		zfs_close(zhp);
35222082Seschrock 		return (0);
35233265Sahrens 	}
35242082Seschrock 
35252417Sahrens 	/* Remove the device link if it's a zvol. */
35262676Seschrock 	if (ZFS_IS_VOLUME(zhp))
35272417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
35282082Seschrock 
35292082Seschrock 	/* Check for conflicting names */
35302676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
35312676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
35322082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
35332082Seschrock 	if (szhp != NULL) {
35342082Seschrock 		zfs_close(szhp);
35352082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
35362082Seschrock 		    "snapshot name '%s' from origin \n"
35372082Seschrock 		    "conflicts with '%s' from target"),
35382082Seschrock 		    zhp->zfs_name, snapname);
35393265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
35402082Seschrock 	}
35413265Sahrens 	zfs_close(zhp);
35423265Sahrens 	return (rv);
35432082Seschrock }
35442082Seschrock 
35452417Sahrens static int
35462417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
35472417Sahrens {
35482417Sahrens 	promote_data_t *pd = data;
35492417Sahrens 
35502417Sahrens 	/* We don't care about snapshots after the pivot point */
35513265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
35523265Sahrens 		/* Create the device link if it's a zvol. */
35533265Sahrens 		if (ZFS_IS_VOLUME(zhp))
35543265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
35553265Sahrens 	}
35563265Sahrens 
35573265Sahrens 	zfs_close(zhp);
35582417Sahrens 	return (0);
35592417Sahrens }
35602417Sahrens 
35612082Seschrock /*
35622082Seschrock  * Promotes the given clone fs to be the clone parent.
35632082Seschrock  */
35642082Seschrock int
35652082Seschrock zfs_promote(zfs_handle_t *zhp)
35662082Seschrock {
35672082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
35682082Seschrock 	zfs_cmd_t zc = { 0 };
35692082Seschrock 	char parent[MAXPATHLEN];
35702082Seschrock 	char *cp;
35712082Seschrock 	int ret;
35722082Seschrock 	zfs_handle_t *pzhp;
35732082Seschrock 	promote_data_t pd;
35742082Seschrock 	char errbuf[1024];
35752082Seschrock 
35762082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35772082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
35782082Seschrock 
35792082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
35802082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35812082Seschrock 		    "snapshots can not be promoted"));
35822082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
35832082Seschrock 	}
35842082Seschrock 
35855367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
35862082Seschrock 	if (parent[0] == '\0') {
35872082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35882082Seschrock 		    "not a cloned filesystem"));
35892082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
35902082Seschrock 	}
35912082Seschrock 	cp = strchr(parent, '@');
35922082Seschrock 	*cp = '\0';
35932082Seschrock 
35942082Seschrock 	/* Walk the snapshots we will be moving */
35955367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
35962082Seschrock 	if (pzhp == NULL)
35972082Seschrock 		return (-1);
35982082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
35992082Seschrock 	zfs_close(pzhp);
36002082Seschrock 	pd.cb_target = zhp->zfs_name;
36012082Seschrock 	pd.cb_errbuf = errbuf;
36025094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
36032082Seschrock 	if (pzhp == NULL)
36042082Seschrock 		return (-1);
36052082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
36062082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
36072082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
36082417Sahrens 	if (ret != 0) {
36092417Sahrens 		zfs_close(pzhp);
36102082Seschrock 		return (-1);
36112417Sahrens 	}
36122082Seschrock 
36132082Seschrock 	/* issue the ioctl */
36145367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
36152676Seschrock 	    sizeof (zc.zc_value));
36162082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
36174543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
36182082Seschrock 
36192082Seschrock 	if (ret != 0) {
36202417Sahrens 		int save_errno = errno;
36212417Sahrens 
36222417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
36232417Sahrens 		zfs_close(pzhp);
36242417Sahrens 
36252417Sahrens 		switch (save_errno) {
3626789Sahrens 		case EEXIST:
3627789Sahrens 			/*
36282082Seschrock 			 * There is a conflicting snapshot name.  We
36292082Seschrock 			 * should have caught this above, but they could
36302082Seschrock 			 * have renamed something in the mean time.
3631789Sahrens 			 */
36322082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36332082Seschrock 			    "conflicting snapshot name from parent '%s'"),
36342082Seschrock 			    parent);
36352082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3636789Sahrens 
3637789Sahrens 		default:
36382417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3639789Sahrens 		}
36402417Sahrens 	} else {
36412417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3642789Sahrens 	}
3643789Sahrens 
36442417Sahrens 	zfs_close(pzhp);
3645789Sahrens 	return (ret);
3646789Sahrens }
3647789Sahrens 
36484007Smmusante struct createdata {
36494007Smmusante 	const char *cd_snapname;
36504007Smmusante 	int cd_ifexists;
36514007Smmusante };
36524007Smmusante 
36532199Sahrens static int
36542199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
36552199Sahrens {
36564007Smmusante 	struct createdata *cd = arg;
36572676Seschrock 	int ret;
36582199Sahrens 
36592199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
36602199Sahrens 		char name[MAXPATHLEN];
36612199Sahrens 
36622676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
36632676Seschrock 		(void) strlcat(name, "@", sizeof (name));
36644007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
36654007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
36664007Smmusante 		    cd->cd_ifexists);
36672199Sahrens 		/*
36682199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
36692199Sahrens 		 * return an error, because then we wouldn't visit all
36702199Sahrens 		 * the volumes.
36712199Sahrens 		 */
36722199Sahrens 	}
36732676Seschrock 
36744007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
36752676Seschrock 
36762676Seschrock 	zfs_close(zhp);
36772676Seschrock 
36782676Seschrock 	return (ret);
36792199Sahrens }
36802199Sahrens 
3681789Sahrens /*
36823504Sahl  * Takes a snapshot of the given dataset.
3683789Sahrens  */
3684789Sahrens int
36857265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
36867265Sahrens     nvlist_t *props)
3687789Sahrens {
3688789Sahrens 	const char *delim;
36897265Sahrens 	char parent[ZFS_MAXNAMELEN];
3690789Sahrens 	zfs_handle_t *zhp;
3691789Sahrens 	zfs_cmd_t zc = { 0 };
3692789Sahrens 	int ret;
36932082Seschrock 	char errbuf[1024];
36942082Seschrock 
36952082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36962082Seschrock 	    "cannot snapshot '%s'"), path);
36972082Seschrock 
36982082Seschrock 	/* validate the target name */
36995326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
37002082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3701789Sahrens 
37027265Sahrens 	if (props) {
37037265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
37047265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
37057265Sahrens 			return (-1);
37067265Sahrens 
37077265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
37087265Sahrens 			nvlist_free(props);
37097265Sahrens 			return (-1);
37107265Sahrens 		}
37117265Sahrens 
37127265Sahrens 		nvlist_free(props);
37137265Sahrens 	}
37147265Sahrens 
3715789Sahrens 	/* make sure the parent exists and is of the appropriate type */
37162199Sahrens 	delim = strchr(path, '@');
3717789Sahrens 	(void) strncpy(parent, path, delim - path);
3718789Sahrens 	parent[delim - path] = '\0';
3719789Sahrens 
37202082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3721789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
37227265Sahrens 		zcmd_free_nvlists(&zc);
3723789Sahrens 		return (-1);
3724789Sahrens 	}
3725789Sahrens 
37262199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
37272676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
37284543Smarks 	if (ZFS_IS_VOLUME(zhp))
37294543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
37304543Smarks 	else
37314543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
37322199Sahrens 	zc.zc_cookie = recursive;
37334543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
37342199Sahrens 
37357265Sahrens 	zcmd_free_nvlists(&zc);
37367265Sahrens 
37372199Sahrens 	/*
37382199Sahrens 	 * if it was recursive, the one that actually failed will be in
37392199Sahrens 	 * zc.zc_name.
37402199Sahrens 	 */
37414543Smarks 	if (ret != 0)
37424543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37434543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
37444543Smarks 
37452199Sahrens 	if (ret == 0 && recursive) {
37464007Smmusante 		struct createdata cd;
37474007Smmusante 
37484007Smmusante 		cd.cd_snapname = delim + 1;
37494007Smmusante 		cd.cd_ifexists = B_FALSE;
37504007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
37512199Sahrens 	}
3752789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
37532082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
37542199Sahrens 		if (ret != 0) {
37554543Smarks 			(void) zfs_standard_error(hdl, errno,
37564543Smarks 			    dgettext(TEXT_DOMAIN,
37574543Smarks 			    "Volume successfully snapshotted, but device links "
37584543Smarks 			    "were not created"));
37594543Smarks 			zfs_close(zhp);
37604543Smarks 			return (-1);
37612199Sahrens 		}
3762789Sahrens 	}
3763789Sahrens 
37642082Seschrock 	if (ret != 0)
37652082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3766789Sahrens 
3767789Sahrens 	zfs_close(zhp);
3768789Sahrens 
3769789Sahrens 	return (ret);
3770789Sahrens }
3771789Sahrens 
3772789Sahrens /*
37731294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
37741294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
37751294Slling  * is a dependent and we should just destroy it without checking the transaction
37761294Slling  * group.
3777789Sahrens  */
37781294Slling typedef struct rollback_data {
37791294Slling 	const char	*cb_target;		/* the snapshot */
37801294Slling 	uint64_t	cb_create;		/* creation time reference */
37815749Sahrens 	boolean_t	cb_error;
37822082Seschrock 	boolean_t	cb_dependent;
37835749Sahrens 	boolean_t	cb_force;
37841294Slling } rollback_data_t;
37851294Slling 
37861294Slling static int
37871294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
37881294Slling {
37891294Slling 	rollback_data_t *cbp = data;
37901294Slling 
37911294Slling 	if (!cbp->cb_dependent) {
37921294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
37931294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
37941294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
37951294Slling 		    cbp->cb_create) {
37964543Smarks 			char *logstr;
37971294Slling 
37982082Seschrock 			cbp->cb_dependent = B_TRUE;
37995446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
38005446Sahrens 			    rollback_destroy, cbp);
38012082Seschrock 			cbp->cb_dependent = B_FALSE;
38021294Slling 
38034543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
38044543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
38055446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
38064543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
38071294Slling 		}
38081294Slling 	} else {
38095749Sahrens 		/* We must destroy this clone; first unmount it */
38105749Sahrens 		prop_changelist_t *clp;
38115749Sahrens 
38127366STim.Haley@Sun.COM 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
38135749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
38145749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
38155749Sahrens 			cbp->cb_error = B_TRUE;
38165749Sahrens 			zfs_close(zhp);
38175749Sahrens 			return (0);
38185749Sahrens 		}
38195749Sahrens 		if (zfs_destroy(zhp) != 0)
38205749Sahrens 			cbp->cb_error = B_TRUE;
38215749Sahrens 		else
38225749Sahrens 			changelist_remove(clp, zhp->zfs_name);
38235751Sahrens 		(void) changelist_postfix(clp);
38245749Sahrens 		changelist_free(clp);
38251294Slling 	}
38261294Slling 
38271294Slling 	zfs_close(zhp);
38281294Slling 	return (0);
38291294Slling }
38301294Slling 
38311294Slling /*
38325446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
38335446Sahrens  * data changes since then and making it the active dataset.
38345446Sahrens  *
38355446Sahrens  * Any snapshots more recent than the target are destroyed, along with
38365446Sahrens  * their dependents.
38371294Slling  */
38385446Sahrens int
38395749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3840789Sahrens {
38415446Sahrens 	rollback_data_t cb = { 0 };
38425446Sahrens 	int err;
3843789Sahrens 	zfs_cmd_t zc = { 0 };
38445713Srm160521 	boolean_t restore_resv = 0;
38455713Srm160521 	uint64_t old_volsize, new_volsize;
38465713Srm160521 	zfs_prop_t resv_prop;
3847789Sahrens 
3848789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3849789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3850789Sahrens 
38515446Sahrens 	/*
38525446Sahrens 	 * Destroy all recent snapshots and its dependends.
38535446Sahrens 	 */
38545749Sahrens 	cb.cb_force = force;
38555446Sahrens 	cb.cb_target = snap->zfs_name;
38565446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
38575446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
38585446Sahrens 
38595749Sahrens 	if (cb.cb_error)
38605749Sahrens 		return (-1);
38615446Sahrens 
38625446Sahrens 	/*
38635446Sahrens 	 * Now that we have verified that the snapshot is the latest,
38645446Sahrens 	 * rollback to the given snapshot.
38655446Sahrens 	 */
38665446Sahrens 
38675713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
38685713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
38695713Srm160521 			return (-1);
38705713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
38715713Srm160521 			return (-1);
38725713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
38735713Srm160521 		restore_resv =
38745713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
38755713Srm160521 	}
3876789Sahrens 
3877789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3878789Sahrens 
38792676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3880789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3881789Sahrens 	else
3882789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3883789Sahrens 
3884789Sahrens 	/*
38855446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
38865446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
38875446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
38885446Sahrens 	 * an unlikely race condition where the user has taken a
38895446Sahrens 	 * snapshot since we verified that this was the most recent.
38905713Srm160521 	 *
3891789Sahrens 	 */
38925446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
38933237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
38942082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
38952082Seschrock 		    zhp->zfs_name);
38965717Srm160521 		return (err);
38975717Srm160521 	}
38985713Srm160521 
38995713Srm160521 	/*
39005713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
39015713Srm160521 	 * rollback reservation and the volsize has changed then set
39025713Srm160521 	 * the reservation property to the post-rollback volsize.
39035713Srm160521 	 * Make a new handle since the rollback closed the dataset.
39045713Srm160521 	 */
39055717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
39065717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
39075717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
39085717Srm160521 			zfs_close(zhp);
39095713Srm160521 			return (err);
39105717Srm160521 		}
39115713Srm160521 		if (restore_resv) {
39125713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
39135713Srm160521 			if (old_volsize != new_volsize)
39145717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
39155717Srm160521 				    new_volsize);
39165713Srm160521 		}
39175713Srm160521 		zfs_close(zhp);
3918789Sahrens 	}
39195446Sahrens 	return (err);
39201294Slling }
39211294Slling 
39221294Slling /*
3923789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3924789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3925789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3926789Sahrens  * libzfs_graph.c.
3927789Sahrens  */
3928789Sahrens int
39292474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
39302474Seschrock     zfs_iter_f func, void *data)
3931789Sahrens {
3932789Sahrens 	char **dependents;
3933789Sahrens 	size_t count;
3934789Sahrens 	int i;
3935789Sahrens 	zfs_handle_t *child;
3936789Sahrens 	int ret = 0;
3937789Sahrens 
39382474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
39392474Seschrock 	    &dependents, &count) != 0)
39402474Seschrock 		return (-1);
39412474Seschrock 
3942789Sahrens 	for (i = 0; i < count; i++) {
39432082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
39442082Seschrock 		    dependents[i])) == NULL)
3945789Sahrens 			continue;
3946789Sahrens 
3947789Sahrens 		if ((ret = func(child, data)) != 0)
3948789Sahrens 			break;
3949789Sahrens 	}
3950789Sahrens 
3951789Sahrens 	for (i = 0; i < count; i++)
3952789Sahrens 		free(dependents[i]);
3953789Sahrens 	free(dependents);
3954789Sahrens 
3955789Sahrens 	return (ret);
3956789Sahrens }
3957789Sahrens 
3958789Sahrens /*
3959789Sahrens  * Renames the given dataset.
3960789Sahrens  */
3961789Sahrens int
39624490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3963789Sahrens {
3964789Sahrens 	int ret;
3965789Sahrens 	zfs_cmd_t zc = { 0 };
3966789Sahrens 	char *delim;
39674007Smmusante 	prop_changelist_t *cl = NULL;
39684007Smmusante 	zfs_handle_t *zhrp = NULL;
39694007Smmusante 	char *parentname = NULL;
3970789Sahrens 	char parent[ZFS_MAXNAMELEN];
39712082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
39722082Seschrock 	char errbuf[1024];
3973789Sahrens 
3974789Sahrens 	/* if we have the same exact name, just return success */
3975789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3976789Sahrens 		return (0);
3977789Sahrens 
39782082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
39792082Seschrock 	    "cannot rename to '%s'"), target);
39802082Seschrock 
3981789Sahrens 	/*
3982789Sahrens 	 * Make sure the target name is valid
3983789Sahrens 	 */
3984789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
39852665Snd150628 		if ((strchr(target, '@') == NULL) ||
39862665Snd150628 		    *target == '@') {
39872665Snd150628 			/*
39882665Snd150628 			 * Snapshot target name is abbreviated,
39892665Snd150628 			 * reconstruct full dataset name
39902665Snd150628 			 */
39912665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
39922665Snd150628 			    sizeof (parent));
39932665Snd150628 			delim = strchr(parent, '@');
39942665Snd150628 			if (strchr(target, '@') == NULL)
39952665Snd150628 				*(++delim) = '\0';
39962665Snd150628 			else
39972665Snd150628 				*delim = '\0';
39982665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
39992665Snd150628 			target = parent;
40002665Snd150628 		} else {
40012665Snd150628 			/*
40022665Snd150628 			 * Make sure we're renaming within the same dataset.
40032665Snd150628 			 */
40042665Snd150628 			delim = strchr(target, '@');
40052665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
40062665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
40072665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40082665Snd150628 				    "snapshots must be part of same "
40092665Snd150628 				    "dataset"));
40102665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
40113912Slling 				    errbuf));
40122665Snd150628 			}
4013789Sahrens 		}
40145326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
40152665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4016789Sahrens 	} else {
40174007Smmusante 		if (recursive) {
40184007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40194007Smmusante 			    "recursive rename must be a snapshot"));
40204007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
40214007Smmusante 		}
40224007Smmusante 
40235326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
40242665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
40252676Seschrock 		uint64_t unused;
40262676Seschrock 
4027789Sahrens 		/* validate parents */
40284490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
4029789Sahrens 			return (-1);
4030789Sahrens 
4031789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
4032789Sahrens 
4033789Sahrens 		/* make sure we're in the same pool */
4034789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
4035789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4036789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
40372082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40382082Seschrock 			    "datasets must be within same pool"));
40392082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4040789Sahrens 		}
40412440Snd150628 
40422440Snd150628 		/* new name cannot be a child of the current dataset name */
40432440Snd150628 		if (strncmp(parent, zhp->zfs_name,
40443912Slling 		    strlen(zhp->zfs_name)) == 0) {
40452440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40462440Snd150628 			    "New dataset name cannot be a descendent of "
40472440Snd150628 			    "current dataset name"));
40482440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
40492440Snd150628 		}
4050789Sahrens 	}
4051789Sahrens 
40522082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
40532082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
40542082Seschrock 
4055789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
4056789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
40572082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40582082Seschrock 		    "dataset is used in a non-global zone"));
40592082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4060789Sahrens 	}
4061789Sahrens 
40624007Smmusante 	if (recursive) {
40634007Smmusante 		struct destroydata dd;
40644007Smmusante 
40654183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
40664183Smmusante 		if (parentname == NULL) {
40674183Smmusante 			ret = -1;
40684183Smmusante 			goto error;
40694183Smmusante 		}
40704007Smmusante 		delim = strchr(parentname, '@');
40714007Smmusante 		*delim = '\0';
40725094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
40734007Smmusante 		if (zhrp == NULL) {
40744183Smmusante 			ret = -1;
40754183Smmusante 			goto error;
40764007Smmusante 		}
40774007Smmusante 
40784007Smmusante 		dd.snapname = delim + 1;
40794007Smmusante 		dd.gotone = B_FALSE;
40804183Smmusante 		dd.closezhp = B_TRUE;
40814007Smmusante 
40824007Smmusante 		/* We remove any zvol links prior to renaming them */
40834007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
40844007Smmusante 		if (ret) {
40854007Smmusante 			goto error;
40864007Smmusante 		}
40874007Smmusante 	} else {
40887366STim.Haley@Sun.COM 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
40894007Smmusante 			return (-1);
40904007Smmusante 
40914007Smmusante 		if (changelist_haszonedchild(cl)) {
40924007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40934007Smmusante 			    "child dataset with inherited mountpoint is used "
40944007Smmusante 			    "in a non-global zone"));
40954007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
40964007Smmusante 			goto error;
40974007Smmusante 		}
40984007Smmusante 
40994007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
41004007Smmusante 			goto error;
4101789Sahrens 	}
4102789Sahrens 
41032676Seschrock 	if (ZFS_IS_VOLUME(zhp))
4104789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
4105789Sahrens 	else
4106789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
4107789Sahrens 
41082665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
41092676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
41102665Snd150628 
41114007Smmusante 	zc.zc_cookie = recursive;
41124007Smmusante 
41134543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
41144007Smmusante 		/*
41154007Smmusante 		 * if it was recursive, the one that actually failed will
41164007Smmusante 		 * be in zc.zc_name
41174007Smmusante 		 */
41184007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
41195367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
41204007Smmusante 
41214007Smmusante 		if (recursive && errno == EEXIST) {
41224007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41234007Smmusante 			    "a child dataset already has a snapshot "
41244007Smmusante 			    "with the new name"));
41254801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
41264007Smmusante 		} else {
41274007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
41284007Smmusante 		}
4129789Sahrens 
4130789Sahrens 		/*
4131789Sahrens 		 * On failure, we still want to remount any filesystems that
4132789Sahrens 		 * were previously mounted, so we don't alter the system state.
4133789Sahrens 		 */
41344007Smmusante 		if (recursive) {
41354007Smmusante 			struct createdata cd;
41364007Smmusante 
41374007Smmusante 			/* only create links for datasets that had existed */
41384007Smmusante 			cd.cd_snapname = delim + 1;
41394007Smmusante 			cd.cd_ifexists = B_TRUE;
41404007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
41414007Smmusante 			    &cd);
41424007Smmusante 		} else {
41434007Smmusante 			(void) changelist_postfix(cl);
41444007Smmusante 		}
4145789Sahrens 	} else {
41464007Smmusante 		if (recursive) {
41474007Smmusante 			struct createdata cd;
41484007Smmusante 
41494007Smmusante 			/* only create links for datasets that had existed */
41504007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
41514007Smmusante 			cd.cd_ifexists = B_TRUE;
41524007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
41534007Smmusante 			    &cd);
41544007Smmusante 		} else {
41554007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
41564007Smmusante 			ret = changelist_postfix(cl);
41574007Smmusante 		}
4158789Sahrens 	}
4159789Sahrens 
4160789Sahrens error:
41614007Smmusante 	if (parentname) {
41624007Smmusante 		free(parentname);
41634007Smmusante 	}
41644007Smmusante 	if (zhrp) {
41654007Smmusante 		zfs_close(zhrp);
41664007Smmusante 	}
41674007Smmusante 	if (cl) {
41684007Smmusante 		changelist_free(cl);
41694007Smmusante 	}
4170789Sahrens 	return (ret);
4171789Sahrens }
4172789Sahrens 
4173789Sahrens /*
4174789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
4175789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
4176789Sahrens  */
4177789Sahrens int
41782082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
4179789Sahrens {
41804007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
41814007Smmusante }
41824007Smmusante 
41834007Smmusante static int
41844007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
41854007Smmusante {
4186789Sahrens 	zfs_cmd_t zc = { 0 };
41872082Seschrock 	di_devlink_handle_t dhdl;
41884543Smarks 	priv_set_t *priv_effective;
41894543Smarks 	int privileged;
4190789Sahrens 
4191789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4192789Sahrens 
4193789Sahrens 	/*
4194789Sahrens 	 * Issue the appropriate ioctl.
4195789Sahrens 	 */
41962082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4197789Sahrens 		switch (errno) {
4198789Sahrens 		case EEXIST:
4199789Sahrens 			/*
4200789Sahrens 			 * Silently ignore the case where the link already
4201789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
4202789Sahrens 			 * times without errors.
4203789Sahrens 			 */
4204789Sahrens 			return (0);
4205789Sahrens 
42064007Smmusante 		case ENOENT:
42074007Smmusante 			/*
42084007Smmusante 			 * Dataset does not exist in the kernel.  If we
42094007Smmusante 			 * don't care (see zfs_rename), then ignore the
42104007Smmusante 			 * error quietly.
42114007Smmusante 			 */
42124007Smmusante 			if (ifexists) {
42134007Smmusante 				return (0);
42144007Smmusante 			}
42154007Smmusante 
42164007Smmusante 			/* FALLTHROUGH */
42174007Smmusante 
4218789Sahrens 		default:
42193237Slling 			return (zfs_standard_error_fmt(hdl, errno,
42202082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
42212082Seschrock 			    "for '%s'"), dataset));
4222789Sahrens 		}
4223789Sahrens 	}
4224789Sahrens 
4225789Sahrens 	/*
42264543Smarks 	 * If privileged call devfsadm and wait for the links to
42274543Smarks 	 * magically appear.
42284543Smarks 	 * Otherwise, print out an informational message.
4229789Sahrens 	 */
42304543Smarks 
42314543Smarks 	priv_effective = priv_allocset();
42324543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
42334543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
42344543Smarks 	priv_freeset(priv_effective);
42354543Smarks 
42364543Smarks 	if (privileged) {
42374543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
42384543Smarks 		    DI_MAKE_LINK)) == NULL) {
42394543Smarks 			zfs_error_aux(hdl, strerror(errno));
42407301SEric.Taylor@Sun.COM 			(void) zfs_error_fmt(hdl, errno,
42414543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
42424543Smarks 			    "for '%s'"), dataset);
42434543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
42444543Smarks 			return (-1);
42454543Smarks 		} else {
42464543Smarks 			(void) di_devlink_fini(&dhdl);
42474543Smarks 		}
4248789Sahrens 	} else {
42494543Smarks 		char pathname[MAXPATHLEN];
42504543Smarks 		struct stat64 statbuf;
42514543Smarks 		int i;
42524543Smarks 
42534543Smarks #define	MAX_WAIT	10
42544543Smarks 
42554543Smarks 		/*
42564543Smarks 		 * This is the poor mans way of waiting for the link
42574543Smarks 		 * to show up.  If after 10 seconds we still don't
42584543Smarks 		 * have it, then print out a message.
42594543Smarks 		 */
42604543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
42614543Smarks 		    dataset);
42624543Smarks 
42634543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
42644543Smarks 			if (stat64(pathname, &statbuf) == 0)
42654543Smarks 				break;
42664543Smarks 			(void) sleep(1);
42674543Smarks 		}
42684543Smarks 		if (i == MAX_WAIT)
42694543Smarks 			(void) printf(gettext("%s may not be immediately "
42704543Smarks 			    "available\n"), pathname);
4271789Sahrens 	}
4272789Sahrens 
4273789Sahrens 	return (0);
4274789Sahrens }
4275789Sahrens 
4276789Sahrens /*
4277789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4278789Sahrens  */
4279789Sahrens int
42802082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4281789Sahrens {
4282789Sahrens 	zfs_cmd_t zc = { 0 };
4283789Sahrens 
4284789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4285789Sahrens 
42862082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4287789Sahrens 		switch (errno) {
4288789Sahrens 		case ENXIO:
4289789Sahrens 			/*
4290789Sahrens 			 * Silently ignore the case where the link no longer
4291789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4292789Sahrens 			 * times without errors.
4293789Sahrens 			 */
4294789Sahrens 			return (0);
4295789Sahrens 
4296789Sahrens 		default:
42973237Slling 			return (zfs_standard_error_fmt(hdl, errno,
42982082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
42992082Seschrock 			    "links for '%s'"), dataset));
4300789Sahrens 		}
4301789Sahrens 	}
4302789Sahrens 
4303789Sahrens 	return (0);
4304789Sahrens }
43052676Seschrock 
43062676Seschrock nvlist_t *
43072676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
43082676Seschrock {
43092676Seschrock 	return (zhp->zfs_user_props);
43102676Seschrock }
43112676Seschrock 
43122676Seschrock /*
43133912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
43143912Slling  * display, and their maximum widths.  This does two main things:
43153912Slling  *
43163912Slling  *      - If this is a list of all properties, then expand the list to include
43173912Slling  *        all native properties, and set a flag so that for each dataset we look
43183912Slling  *        for new unique user properties and add them to the list.
43193912Slling  *
43203912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
43213912Slling  *        so that we can size the column appropriately.
43223912Slling  */
43233912Slling int
43245094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
43253912Slling {
43263912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
43275094Slling 	zprop_list_t *entry;
43285094Slling 	zprop_list_t **last, **start;
43293912Slling 	nvlist_t *userprops, *propval;
43303912Slling 	nvpair_t *elem;
43313912Slling 	char *strval;
43323912Slling 	char buf[ZFS_MAXPROPLEN];
43333912Slling 
43345094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
43353912Slling 		return (-1);
43362676Seschrock 
43372676Seschrock 	userprops = zfs_get_user_props(zhp);
43382676Seschrock 
43392676Seschrock 	entry = *plp;
43402676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
43412676Seschrock 		/*
43422676Seschrock 		 * Go through and add any user properties as necessary.  We
43432676Seschrock 		 * start by incrementing our list pointer to the first
43442676Seschrock 		 * non-native property.
43452676Seschrock 		 */
43462676Seschrock 		start = plp;
43472676Seschrock 		while (*start != NULL) {
43485094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
43492676Seschrock 				break;
43502676Seschrock 			start = &(*start)->pl_next;
43512676Seschrock 		}
43522676Seschrock 
43532676Seschrock 		elem = NULL;
43542676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
43552676Seschrock 			/*
43562676Seschrock 			 * See if we've already found this property in our list.
43572676Seschrock 			 */
43582676Seschrock 			for (last = start; *last != NULL;
43592676Seschrock 			    last = &(*last)->pl_next) {
43602676Seschrock 				if (strcmp((*last)->pl_user_prop,
43612676Seschrock 				    nvpair_name(elem)) == 0)
43622676Seschrock 					break;
43632676Seschrock 			}
43642676Seschrock 
43652676Seschrock 			if (*last == NULL) {
43662676Seschrock 				if ((entry = zfs_alloc(hdl,
43675094Slling 				    sizeof (zprop_list_t))) == NULL ||
43682676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
43692676Seschrock 				    nvpair_name(elem)))) == NULL) {
43702676Seschrock 					free(entry);
43712676Seschrock 					return (-1);
43722676Seschrock 				}
43732676Seschrock 
43745094Slling 				entry->pl_prop = ZPROP_INVAL;
43752676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
43762676Seschrock 				entry->pl_all = B_TRUE;
43772676Seschrock 				*last = entry;
43782676Seschrock 			}
43792676Seschrock 		}
43802676Seschrock 	}
43812676Seschrock 
43822676Seschrock 	/*
43832676Seschrock 	 * Now go through and check the width of any non-fixed columns
43842676Seschrock 	 */
43852676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
43862676Seschrock 		if (entry->pl_fixed)
43872676Seschrock 			continue;
43882676Seschrock 
43895094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
43902676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
43912676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
43922676Seschrock 				if (strlen(buf) > entry->pl_width)
43932676Seschrock 					entry->pl_width = strlen(buf);
43942676Seschrock 			}
43952676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
43962676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
43972676Seschrock 			verify(nvlist_lookup_string(propval,
43985094Slling 			    ZPROP_VALUE, &strval) == 0);
43992676Seschrock 			if (strlen(strval) > entry->pl_width)
44002676Seschrock 				entry->pl_width = strlen(strval);
44012676Seschrock 		}
44022676Seschrock 	}
44032676Seschrock 
44042676Seschrock 	return (0);
44052676Seschrock }
44064543Smarks 
44074543Smarks int
44084543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
44094543Smarks {
44104543Smarks 	zfs_cmd_t zc = { 0 };
44114543Smarks 	nvlist_t *nvp;
44124543Smarks 	gid_t gid;
44134543Smarks 	uid_t uid;
44144543Smarks 	const gid_t *groups;
44154543Smarks 	int group_cnt;
44164543Smarks 	int error;
44174543Smarks 
44184543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
44194543Smarks 		return (no_memory(hdl));
44204543Smarks 
44214543Smarks 	uid = ucred_geteuid(cred);
44224543Smarks 	gid = ucred_getegid(cred);
44234543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
44244543Smarks 
44254543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
44264543Smarks 		return (1);
44274543Smarks 
44284543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
44294543Smarks 		nvlist_free(nvp);
44304543Smarks 		return (1);
44314543Smarks 	}
44324543Smarks 
44334543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
44344543Smarks 		nvlist_free(nvp);
44354543Smarks 		return (1);
44364543Smarks 	}
44374543Smarks 
44384543Smarks 	if (nvlist_add_uint32_array(nvp,
44394543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
44404543Smarks 		nvlist_free(nvp);
44414543Smarks 		return (1);
44424543Smarks 	}
44434543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
44444543Smarks 
44455094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
44464543Smarks 		return (-1);
44474543Smarks 
44484543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
44494543Smarks 	nvlist_free(nvp);
44504543Smarks 	return (error);
44514543Smarks }
44524543Smarks 
44534543Smarks int
44544543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
44555331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
44564543Smarks {
44574543Smarks 	zfs_cmd_t zc = { 0 };
44584543Smarks 	int error;
44594543Smarks 
44604543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
44614543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
44624543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
44634543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
44645331Samw 	zc.zc_share.z_sharetype = operation;
44654543Smarks 	zc.zc_share.z_sharemax = sharemax;
44664543Smarks 
44674543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
44684543Smarks 	return (error);
44694543Smarks }
44708802SSanjeev.Bagewadi@Sun.COM 
44718802SSanjeev.Bagewadi@Sun.COM void
44728802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
44738802SSanjeev.Bagewadi@Sun.COM {
44748802SSanjeev.Bagewadi@Sun.COM 	nvpair_t *curr;
44758802SSanjeev.Bagewadi@Sun.COM 
44768802SSanjeev.Bagewadi@Sun.COM 	/*
44778802SSanjeev.Bagewadi@Sun.COM 	 * Keep a reference to the props-table against which we prune the
44788802SSanjeev.Bagewadi@Sun.COM 	 * properties.
44798802SSanjeev.Bagewadi@Sun.COM 	 */
44808802SSanjeev.Bagewadi@Sun.COM 	zhp->zfs_props_table = props;
44818802SSanjeev.Bagewadi@Sun.COM 
44828802SSanjeev.Bagewadi@Sun.COM 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
44838802SSanjeev.Bagewadi@Sun.COM 
44848802SSanjeev.Bagewadi@Sun.COM 	while (curr) {
44858802SSanjeev.Bagewadi@Sun.COM 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
44868802SSanjeev.Bagewadi@Sun.COM 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
44878802SSanjeev.Bagewadi@Sun.COM 
44888802SSanjeev.Bagewadi@Sun.COM 		if (props[zfs_prop] == B_FALSE)
44898802SSanjeev.Bagewadi@Sun.COM 			(void) nvlist_remove(zhp->zfs_props,
44908802SSanjeev.Bagewadi@Sun.COM 			    nvpair_name(curr), nvpair_type(curr));
44918802SSanjeev.Bagewadi@Sun.COM 		curr = next;
44928802SSanjeev.Bagewadi@Sun.COM 	}
44938802SSanjeev.Bagewadi@Sun.COM }
4494