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>
48*9396SMatthew.Ahrens@Sun.COM #include <idmap.h>
49*9396SMatthew.Ahrens@Sun.COM #include <aclutils.h>
50789Sahrens 
51789Sahrens #include <sys/spa.h>
522676Seschrock #include <sys/zap.h>
53789Sahrens #include <libzfs.h>
54789Sahrens 
55789Sahrens #include "zfs_namecheck.h"
56789Sahrens #include "zfs_prop.h"
57789Sahrens #include "libzfs_impl.h"
584543Smarks #include "zfs_deleg.h"
59789Sahrens 
604007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
61*9396SMatthew.Ahrens@Sun.COM static int userquota_propname_decode(const char *propname, boolean_t zoned,
62*9396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
634007Smmusante 
64789Sahrens /*
65789Sahrens  * Given a single type (not a mask of types), return the type in a human
66789Sahrens  * readable form.
67789Sahrens  */
68789Sahrens const char *
69789Sahrens zfs_type_to_name(zfs_type_t type)
70789Sahrens {
71789Sahrens 	switch (type) {
72789Sahrens 	case ZFS_TYPE_FILESYSTEM:
73789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
74789Sahrens 	case ZFS_TYPE_SNAPSHOT:
75789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
76789Sahrens 	case ZFS_TYPE_VOLUME:
77789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
78789Sahrens 	}
79789Sahrens 
80789Sahrens 	return (NULL);
81789Sahrens }
82789Sahrens 
83789Sahrens /*
84789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
85789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
86789Sahrens  * We guess what the type would have been based on the path and the mask of
87789Sahrens  * acceptable types.
88789Sahrens  */
89789Sahrens static const char *
90789Sahrens path_to_str(const char *path, int types)
91789Sahrens {
92789Sahrens 	/*
93789Sahrens 	 * When given a single type, always report the exact type.
94789Sahrens 	 */
95789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
96789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
97789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
98789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
99789Sahrens 	if (types == ZFS_TYPE_VOLUME)
100789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
101789Sahrens 
102789Sahrens 	/*
103789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
104789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
105789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
106789Sahrens 	 * snapshot attribute and try again.
107789Sahrens 	 */
108789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
109789Sahrens 		if (strchr(path, '@') != NULL)
110789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
111789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
112789Sahrens 	}
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * The user has requested either filesystems or volumes.
116789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118789Sahrens 	 */
119789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121789Sahrens 
122789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124789Sahrens }
125789Sahrens 
126789Sahrens /*
127789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128*9396SMatthew.Ahrens@Sun.COM  * provide a more meaningful error message.  We call zfs_error_aux() to
129*9396SMatthew.Ahrens@Sun.COM  * explain exactly why the name was not valid.
130789Sahrens  */
131789Sahrens static int
1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1335326Sek110237     boolean_t modifying)
134789Sahrens {
135789Sahrens 	namecheck_err_t why;
136789Sahrens 	char what;
137789Sahrens 
138789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1392082Seschrock 		if (hdl != NULL) {
140789Sahrens 			switch (why) {
1411003Slling 			case NAME_ERR_TOOLONG:
1422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432082Seschrock 				    "name is too long"));
1441003Slling 				break;
1451003Slling 
146789Sahrens 			case NAME_ERR_LEADING_SLASH:
1472082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482082Seschrock 				    "leading slash in name"));
149789Sahrens 				break;
150789Sahrens 
151789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532082Seschrock 				    "empty component in name"));
154789Sahrens 				break;
155789Sahrens 
156789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1572082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582082Seschrock 				    "trailing slash in name"));
159789Sahrens 				break;
160789Sahrens 
161789Sahrens 			case NAME_ERR_INVALCHAR:
1622082Seschrock 				zfs_error_aux(hdl,
163789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1642082Seschrock 				    "'%c' in name"), what);
165789Sahrens 				break;
166789Sahrens 
167789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1682082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692082Seschrock 				    "multiple '@' delimiters in name"));
170789Sahrens 				break;
1712856Snd150628 
1722856Snd150628 			case NAME_ERR_NOLETTER:
1732856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1742856Snd150628 				    "pool doesn't begin with a letter"));
1752856Snd150628 				break;
1762856Snd150628 
1772856Snd150628 			case NAME_ERR_RESERVED:
1782856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1792856Snd150628 				    "name is reserved"));
1802856Snd150628 				break;
1812856Snd150628 
1822856Snd150628 			case NAME_ERR_DISKLIKE:
1832856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1842856Snd150628 				    "reserved disk name"));
1852856Snd150628 				break;
186789Sahrens 			}
187789Sahrens 		}
188789Sahrens 
189789Sahrens 		return (0);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1932082Seschrock 		if (hdl != NULL)
1942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1952082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
196789Sahrens 		return (0);
197789Sahrens 	}
198789Sahrens 
1992199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2002199Sahrens 		if (hdl != NULL)
2012199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2023413Smmusante 			    "missing '@' delimiter in snapshot name"));
2032199Sahrens 		return (0);
2042199Sahrens 	}
2052199Sahrens 
2065326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2075326Sek110237 		if (hdl != NULL)
2085326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2095326Sek110237 			    "invalid character %c in name"), '%');
2105326Sek110237 		return (0);
2115326Sek110237 	}
2125326Sek110237 
2132082Seschrock 	return (-1);
214789Sahrens }
215789Sahrens 
216789Sahrens int
217789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218789Sahrens {
2196423Sgw25295 	if (type == ZFS_TYPE_POOL)
2206423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2215326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222789Sahrens }
223789Sahrens 
224789Sahrens /*
2252676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2262676Seschrock  * properties into a separate nvlist.
2272676Seschrock  */
2284217Seschrock static nvlist_t *
2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2302676Seschrock {
2312676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2322676Seschrock 	nvpair_t *elem;
2332676Seschrock 	nvlist_t *propval;
2344217Seschrock 	nvlist_t *nvl;
2354217Seschrock 
2364217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2374217Seschrock 		(void) no_memory(hdl);
2384217Seschrock 		return (NULL);
2394217Seschrock 	}
2402676Seschrock 
2412676Seschrock 	elem = NULL;
2424217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2432676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2442676Seschrock 			continue;
2452676Seschrock 
2462676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2474217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2484217Seschrock 			nvlist_free(nvl);
2494217Seschrock 			(void) no_memory(hdl);
2504217Seschrock 			return (NULL);
2514217Seschrock 		}
2522676Seschrock 	}
2532676Seschrock 
2544217Seschrock 	return (nvl);
2552676Seschrock }
2562676Seschrock 
2576865Srm160521 static zpool_handle_t *
2586865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2596865Srm160521 {
2606865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2616865Srm160521 	zpool_handle_t *zph;
2626865Srm160521 
2636865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2646865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2656865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2666865Srm160521 		hdl->libzfs_pool_handles = zph;
2676865Srm160521 	}
2686865Srm160521 	return (zph);
2696865Srm160521 }
2706865Srm160521 
2716865Srm160521 static zpool_handle_t *
2726865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2736865Srm160521 {
2746865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2756865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2766865Srm160521 
2776865Srm160521 	while ((zph != NULL) &&
2786865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2796865Srm160521 		zph = zph->zpool_next;
2806865Srm160521 	return (zph);
2816865Srm160521 }
2826865Srm160521 
2836865Srm160521 /*
2846865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2856865Srm160521  * If a handle to that pool already exists then that handle is returned.
2866865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2876865Srm160521  */
2886865Srm160521 static zpool_handle_t *
2896865Srm160521 zpool_handle(zfs_handle_t *zhp)
2906865Srm160521 {
2916865Srm160521 	char *pool_name;
2926865Srm160521 	int len;
2936865Srm160521 	zpool_handle_t *zph;
2946865Srm160521 
2956865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2966865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2976865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2986865Srm160521 
2996865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
3006865Srm160521 	if (zph == NULL)
3016865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
3026865Srm160521 
3036865Srm160521 	free(pool_name);
3046865Srm160521 	return (zph);
3056865Srm160521 }
3066865Srm160521 
3076865Srm160521 void
3086865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3096865Srm160521 {
3106865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3116865Srm160521 
3126865Srm160521 	while (zph != NULL) {
3136865Srm160521 		next = zph->zpool_next;
3146865Srm160521 		zpool_close(zph);
3156865Srm160521 		zph = next;
3166865Srm160521 	}
3176865Srm160521 	hdl->libzfs_pool_handles = NULL;
3186865Srm160521 }
3196865Srm160521 
3202676Seschrock /*
321789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
322789Sahrens  */
323789Sahrens static int
3248228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
325789Sahrens {
3262676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3278228SEric.Taylor@Sun.COM 
3288228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
3298228SEric.Taylor@Sun.COM 
3308228SEric.Taylor@Sun.COM 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3311356Seschrock 		if (errno == ENOMEM) {
3328228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
3332082Seschrock 				return (-1);
3342676Seschrock 			}
3351356Seschrock 		} else {
3361356Seschrock 			return (-1);
3371356Seschrock 		}
3381356Seschrock 	}
3398228SEric.Taylor@Sun.COM 	return (0);
3408228SEric.Taylor@Sun.COM }
3418228SEric.Taylor@Sun.COM 
3428228SEric.Taylor@Sun.COM static int
3438228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
3448228SEric.Taylor@Sun.COM {
3458228SEric.Taylor@Sun.COM 	nvlist_t *allprops, *userprops;
3468228SEric.Taylor@Sun.COM 
3478228SEric.Taylor@Sun.COM 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
3488228SEric.Taylor@Sun.COM 
3498228SEric.Taylor@Sun.COM 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
3502082Seschrock 		return (-1);
3512082Seschrock 	}
352789Sahrens 
353*9396SMatthew.Ahrens@Sun.COM 	/*
354*9396SMatthew.Ahrens@Sun.COM 	 * XXX Why do we store the user props separately, in addition to
355*9396SMatthew.Ahrens@Sun.COM 	 * storing them in zfs_props?
356*9396SMatthew.Ahrens@Sun.COM 	 */
3574217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3584217Seschrock 		nvlist_free(allprops);
3592676Seschrock 		return (-1);
3604217Seschrock 	}
3614217Seschrock 
3624217Seschrock 	nvlist_free(zhp->zfs_props);
3634217Seschrock 	nvlist_free(zhp->zfs_user_props);
3644217Seschrock 
3654217Seschrock 	zhp->zfs_props = allprops;
3664217Seschrock 	zhp->zfs_user_props = userprops;
3672082Seschrock 
368789Sahrens 	return (0);
369789Sahrens }
370789Sahrens 
3718228SEric.Taylor@Sun.COM static int
3728228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp)
3738228SEric.Taylor@Sun.COM {
3748228SEric.Taylor@Sun.COM 	int rc = 0;
3758228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
3768228SEric.Taylor@Sun.COM 
3778228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
3788228SEric.Taylor@Sun.COM 		return (-1);
3798228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) != 0)
3808228SEric.Taylor@Sun.COM 		rc = -1;
3818228SEric.Taylor@Sun.COM 	else if (put_stats_zhdl(zhp, &zc) != 0)
3828228SEric.Taylor@Sun.COM 		rc = -1;
3838228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
3848228SEric.Taylor@Sun.COM 	return (rc);
3858228SEric.Taylor@Sun.COM }
3868228SEric.Taylor@Sun.COM 
387789Sahrens /*
388789Sahrens  * Refresh the properties currently stored in the handle.
389789Sahrens  */
390789Sahrens void
391789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
392789Sahrens {
393789Sahrens 	(void) get_stats(zhp);
394789Sahrens }
395789Sahrens 
396789Sahrens /*
397789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
398789Sahrens  * zfs_iter_* to create child handles on the fly.
399789Sahrens  */
4008228SEric.Taylor@Sun.COM static int
4018228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
402789Sahrens {
4034543Smarks 	char *logstr;
4048228SEric.Taylor@Sun.COM 	libzfs_handle_t *hdl = zhp->zfs_hdl;
405789Sahrens 
4064543Smarks 	/*
4074543Smarks 	 * Preserve history log string.
4084543Smarks 	 * any changes performed here will be
4094543Smarks 	 * logged as an internal event.
4104543Smarks 	 */
4114543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
4124543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
4138228SEric.Taylor@Sun.COM 
4141758Sahrens top:
4158228SEric.Taylor@Sun.COM 	if (put_stats_zhdl(zhp, zc) != 0) {
4164543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
4178228SEric.Taylor@Sun.COM 		return (-1);
418789Sahrens 	}
419789Sahrens 
4208228SEric.Taylor@Sun.COM 
4211758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4228228SEric.Taylor@Sun.COM 		zfs_cmd_t zc2 = { 0 };
4231758Sahrens 
4241758Sahrens 		/*
4251758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4261758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4271758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4281758Sahrens 		 * can't be mounted.  However, it could also be that we
4291758Sahrens 		 * have crashed in the middle of one of those
4301758Sahrens 		 * operations, in which case we need to get rid of the
4311758Sahrens 		 * inconsistent state.  We do that by either rolling
4321758Sahrens 		 * back to the previous snapshot (which will fail if
4331758Sahrens 		 * there is none), or destroying the filesystem.  Note
4341758Sahrens 		 * that if we are still in the middle of an active
4351758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4361758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4371758Sahrens 		 */
4381758Sahrens 
4398228SEric.Taylor@Sun.COM 		(void) strlcpy(zc2.zc_name, zhp->zfs_name,
4408228SEric.Taylor@Sun.COM 		    sizeof (zc2.zc_name));
4411758Sahrens 
4422885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4432082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4448228SEric.Taylor@Sun.COM 			zc2.zc_objset_type = DMU_OST_ZVOL;
4451758Sahrens 		} else {
4468228SEric.Taylor@Sun.COM 			zc2.zc_objset_type = DMU_OST_ZFS;
4471758Sahrens 		}
4481758Sahrens 
4491758Sahrens 		/*
4505331Samw 		 * If we can successfully destroy it, pretend that it
4511758Sahrens 		 * never existed.
4521758Sahrens 		 */
4538228SEric.Taylor@Sun.COM 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
4544543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4551758Sahrens 			errno = ENOENT;
4568228SEric.Taylor@Sun.COM 			return (-1);
4571758Sahrens 		}
4588228SEric.Taylor@Sun.COM 		/* If we can successfully roll it back, reset the stats */
4598228SEric.Taylor@Sun.COM 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
4608228SEric.Taylor@Sun.COM 			if (get_stats_ioctl(zhp, zc) != 0) {
4618228SEric.Taylor@Sun.COM 				zhp->zfs_hdl->libzfs_log_str = logstr;
4628228SEric.Taylor@Sun.COM 				return (-1);
4638228SEric.Taylor@Sun.COM 			}
4645367Sahrens 			goto top;
4658228SEric.Taylor@Sun.COM 		}
4661758Sahrens 	}
4671758Sahrens 
468789Sahrens 	/*
469789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
470789Sahrens 	 * the high-level type.
471789Sahrens 	 */
4722885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4732885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4742885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4752885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4762885Sahrens 	else
4772885Sahrens 		abort();
4782885Sahrens 
479789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
480789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
481789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
482789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
483789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
484789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
485789Sahrens 	else
4862082Seschrock 		abort();	/* we should never see any other types */
487789Sahrens 
4884543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
4896865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
4908228SEric.Taylor@Sun.COM 	return (0);
4918228SEric.Taylor@Sun.COM }
4928228SEric.Taylor@Sun.COM 
4938228SEric.Taylor@Sun.COM zfs_handle_t *
4948228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path)
4958228SEric.Taylor@Sun.COM {
4968228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
4978228SEric.Taylor@Sun.COM 
4988228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4998228SEric.Taylor@Sun.COM 
5008228SEric.Taylor@Sun.COM 	if (zhp == NULL)
5018228SEric.Taylor@Sun.COM 		return (NULL);
5028228SEric.Taylor@Sun.COM 
5038228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
5048228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
5058228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
5068228SEric.Taylor@Sun.COM 		free(zhp);
5078228SEric.Taylor@Sun.COM 		return (NULL);
5088228SEric.Taylor@Sun.COM 	}
5098228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) == -1) {
5108228SEric.Taylor@Sun.COM 		zcmd_free_nvlists(&zc);
5118228SEric.Taylor@Sun.COM 		free(zhp);
5128228SEric.Taylor@Sun.COM 		return (NULL);
5138228SEric.Taylor@Sun.COM 	}
5148228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, &zc) == -1) {
5158228SEric.Taylor@Sun.COM 		free(zhp);
5168228SEric.Taylor@Sun.COM 		zhp = NULL;
5178228SEric.Taylor@Sun.COM 	}
5188228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
5198228SEric.Taylor@Sun.COM 	return (zhp);
5208228SEric.Taylor@Sun.COM }
5218228SEric.Taylor@Sun.COM 
5228228SEric.Taylor@Sun.COM static zfs_handle_t *
5238228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
5248228SEric.Taylor@Sun.COM {
5258228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5268228SEric.Taylor@Sun.COM 
5278228SEric.Taylor@Sun.COM 	if (zhp == NULL)
5288228SEric.Taylor@Sun.COM 		return (NULL);
5298228SEric.Taylor@Sun.COM 
5308228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
5318228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5328228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, zc) == -1) {
5338228SEric.Taylor@Sun.COM 		free(zhp);
5348228SEric.Taylor@Sun.COM 		return (NULL);
5358228SEric.Taylor@Sun.COM 	}
536789Sahrens 	return (zhp);
537789Sahrens }
538789Sahrens 
539789Sahrens /*
540789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
541789Sahrens  * argument is a mask of acceptable types.  The function will print an
542789Sahrens  * appropriate error message and return NULL if it can't be opened.
543789Sahrens  */
544789Sahrens zfs_handle_t *
5452082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
546789Sahrens {
547789Sahrens 	zfs_handle_t *zhp;
5482082Seschrock 	char errbuf[1024];
5492082Seschrock 
5502082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
5512082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
552789Sahrens 
553789Sahrens 	/*
5542082Seschrock 	 * Validate the name before we even try to open it.
555789Sahrens 	 */
5565326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
5572082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5582082Seschrock 		    "invalid dataset name"));
5592082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
560789Sahrens 		return (NULL);
561789Sahrens 	}
562789Sahrens 
563789Sahrens 	/*
564789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
565789Sahrens 	 */
566789Sahrens 	errno = 0;
5672082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5683237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
569789Sahrens 		return (NULL);
570789Sahrens 	}
571789Sahrens 
572789Sahrens 	if (!(types & zhp->zfs_type)) {
5732082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5742142Seschrock 		zfs_close(zhp);
575789Sahrens 		return (NULL);
576789Sahrens 	}
577789Sahrens 
578789Sahrens 	return (zhp);
579789Sahrens }
580789Sahrens 
581789Sahrens /*
582789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
583789Sahrens  */
584789Sahrens void
585789Sahrens zfs_close(zfs_handle_t *zhp)
586789Sahrens {
587789Sahrens 	if (zhp->zfs_mntopts)
588789Sahrens 		free(zhp->zfs_mntopts);
5892676Seschrock 	nvlist_free(zhp->zfs_props);
5902676Seschrock 	nvlist_free(zhp->zfs_user_props);
591789Sahrens 	free(zhp);
592789Sahrens }
593789Sahrens 
5948228SEric.Taylor@Sun.COM typedef struct mnttab_node {
5958228SEric.Taylor@Sun.COM 	struct mnttab mtn_mt;
5968228SEric.Taylor@Sun.COM 	avl_node_t mtn_node;
5978228SEric.Taylor@Sun.COM } mnttab_node_t;
5988228SEric.Taylor@Sun.COM 
5998228SEric.Taylor@Sun.COM static int
6008228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
6018228SEric.Taylor@Sun.COM {
6028228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn1 = arg1;
6038228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn2 = arg2;
6048228SEric.Taylor@Sun.COM 	int rv;
6058228SEric.Taylor@Sun.COM 
6068228SEric.Taylor@Sun.COM 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
6078228SEric.Taylor@Sun.COM 
6088228SEric.Taylor@Sun.COM 	if (rv == 0)
6098228SEric.Taylor@Sun.COM 		return (0);
6108228SEric.Taylor@Sun.COM 	return (rv > 0 ? 1 : -1);
6118228SEric.Taylor@Sun.COM }
6128228SEric.Taylor@Sun.COM 
6138228SEric.Taylor@Sun.COM void
6148228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl)
6158228SEric.Taylor@Sun.COM {
6168228SEric.Taylor@Sun.COM 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
6178228SEric.Taylor@Sun.COM 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
6188228SEric.Taylor@Sun.COM 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
6198811SEric.Taylor@Sun.COM }
6208811SEric.Taylor@Sun.COM 
6218811SEric.Taylor@Sun.COM void
6228811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl)
6238811SEric.Taylor@Sun.COM {
6248811SEric.Taylor@Sun.COM 	struct mnttab entry;
6258228SEric.Taylor@Sun.COM 
6268228SEric.Taylor@Sun.COM 	rewind(hdl->libzfs_mnttab);
6278228SEric.Taylor@Sun.COM 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
6288228SEric.Taylor@Sun.COM 		mnttab_node_t *mtn;
6298228SEric.Taylor@Sun.COM 
6308228SEric.Taylor@Sun.COM 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6318228SEric.Taylor@Sun.COM 			continue;
6328228SEric.Taylor@Sun.COM 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
6338228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
6348228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
6358228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
6368228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
6378228SEric.Taylor@Sun.COM 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
6388228SEric.Taylor@Sun.COM 	}
6398228SEric.Taylor@Sun.COM }
6408228SEric.Taylor@Sun.COM 
6418228SEric.Taylor@Sun.COM void
6428228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl)
6438228SEric.Taylor@Sun.COM {
6448228SEric.Taylor@Sun.COM 	void *cookie = NULL;
6458228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6468228SEric.Taylor@Sun.COM 
6478228SEric.Taylor@Sun.COM 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
6488228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_special);
6498228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mountp);
6508228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_fstype);
6518228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mntopts);
6528228SEric.Taylor@Sun.COM 		free(mtn);
6538228SEric.Taylor@Sun.COM 	}
6548228SEric.Taylor@Sun.COM 	avl_destroy(&hdl->libzfs_mnttab_cache);
6558228SEric.Taylor@Sun.COM }
6568228SEric.Taylor@Sun.COM 
6578811SEric.Taylor@Sun.COM void
6588811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
6598811SEric.Taylor@Sun.COM {
6608811SEric.Taylor@Sun.COM 	hdl->libzfs_mnttab_enable = enable;
6618811SEric.Taylor@Sun.COM }
6628811SEric.Taylor@Sun.COM 
6638228SEric.Taylor@Sun.COM int
6648228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
6658228SEric.Taylor@Sun.COM     struct mnttab *entry)
6668228SEric.Taylor@Sun.COM {
6678228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6688228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6698228SEric.Taylor@Sun.COM 
6708811SEric.Taylor@Sun.COM 	if (!hdl->libzfs_mnttab_enable) {
6718811SEric.Taylor@Sun.COM 		struct mnttab srch = { 0 };
6728811SEric.Taylor@Sun.COM 
6738811SEric.Taylor@Sun.COM 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
6748811SEric.Taylor@Sun.COM 			libzfs_mnttab_fini(hdl);
6758811SEric.Taylor@Sun.COM 		rewind(hdl->libzfs_mnttab);
6768811SEric.Taylor@Sun.COM 		srch.mnt_special = (char *)fsname;
6778811SEric.Taylor@Sun.COM 		srch.mnt_fstype = MNTTYPE_ZFS;
6788811SEric.Taylor@Sun.COM 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
6798811SEric.Taylor@Sun.COM 			return (0);
6808811SEric.Taylor@Sun.COM 		else
6818811SEric.Taylor@Sun.COM 			return (ENOENT);
6828811SEric.Taylor@Sun.COM 	}
6838811SEric.Taylor@Sun.COM 
6848228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6858811SEric.Taylor@Sun.COM 		libzfs_mnttab_update(hdl);
6868228SEric.Taylor@Sun.COM 
6878228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6888228SEric.Taylor@Sun.COM 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
6898228SEric.Taylor@Sun.COM 	if (mtn) {
6908228SEric.Taylor@Sun.COM 		*entry = mtn->mtn_mt;
6918228SEric.Taylor@Sun.COM 		return (0);
6928228SEric.Taylor@Sun.COM 	}
6938228SEric.Taylor@Sun.COM 	return (ENOENT);
6948228SEric.Taylor@Sun.COM }
6958228SEric.Taylor@Sun.COM 
6968228SEric.Taylor@Sun.COM void
6978228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
6988228SEric.Taylor@Sun.COM     const char *mountp, const char *mntopts)
6998228SEric.Taylor@Sun.COM {
7008228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
7018228SEric.Taylor@Sun.COM 
7028228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
7038228SEric.Taylor@Sun.COM 		return;
7048228SEric.Taylor@Sun.COM 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
7058228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
7068228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
7078228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
7088228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
7098228SEric.Taylor@Sun.COM 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
7108228SEric.Taylor@Sun.COM }
7118228SEric.Taylor@Sun.COM 
7128228SEric.Taylor@Sun.COM void
7138228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
7148228SEric.Taylor@Sun.COM {
7158228SEric.Taylor@Sun.COM 	mnttab_node_t find;
7168228SEric.Taylor@Sun.COM 	mnttab_node_t *ret;
7178228SEric.Taylor@Sun.COM 
7188228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
7198228SEric.Taylor@Sun.COM 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
7208228SEric.Taylor@Sun.COM 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
7218228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_special);
7228228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mountp);
7238228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_fstype);
7248228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mntopts);
7258228SEric.Taylor@Sun.COM 		free(ret);
7268228SEric.Taylor@Sun.COM 	}
7278228SEric.Taylor@Sun.COM }
7288228SEric.Taylor@Sun.COM 
7295713Srm160521 int
7305713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
7315713Srm160521 {
7326865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
7336865Srm160521 
7345713Srm160521 	if (zpool_handle == NULL)
7355713Srm160521 		return (-1);
7365713Srm160521 
7375713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
7385713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
7395713Srm160521 	return (0);
7405713Srm160521 }
7415713Srm160521 
7425713Srm160521 /*
7435713Srm160521  * The choice of reservation property depends on the SPA version.
7445713Srm160521  */
7455713Srm160521 static int
7465713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
7475713Srm160521 {
7485713Srm160521 	int spa_version;
7495713Srm160521 
7505713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
7515713Srm160521 		return (-1);
7525713Srm160521 
7535713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
7545713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
7555713Srm160521 	else
7565713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
7575713Srm160521 
7585713Srm160521 	return (0);
7595713Srm160521 }
7605713Srm160521 
7613912Slling /*
7622676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
7632676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
7642676Seschrock  * strings.
765789Sahrens  */
7667184Stimh nvlist_t *
7677184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
7685094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
769789Sahrens {
7702676Seschrock 	nvpair_t *elem;
7712676Seschrock 	uint64_t intval;
7722676Seschrock 	char *strval;
7735094Slling 	zfs_prop_t prop;
7742676Seschrock 	nvlist_t *ret;
7755331Samw 	int chosen_normal = -1;
7765331Samw 	int chosen_utf = -1;
7772676Seschrock 
7785094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
7795094Slling 		(void) no_memory(hdl);
7805094Slling 		return (NULL);
781789Sahrens 	}
782789Sahrens 
783*9396SMatthew.Ahrens@Sun.COM 	/*
784*9396SMatthew.Ahrens@Sun.COM 	 * Make sure this property is valid and applies to this type.
785*9396SMatthew.Ahrens@Sun.COM 	 */
786*9396SMatthew.Ahrens@Sun.COM 
7872676Seschrock 	elem = NULL;
7882676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
7895094Slling 		const char *propname = nvpair_name(elem);
7902676Seschrock 
791*9396SMatthew.Ahrens@Sun.COM 		prop = zfs_name_to_prop(propname);
792*9396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
7935094Slling 			/*
794*9396SMatthew.Ahrens@Sun.COM 			 * This is a user property: make sure it's a
7955094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
7965094Slling 			 */
7975094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
7985094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7995094Slling 				    "'%s' must be a string"), propname);
8005094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8015094Slling 				goto error;
8025094Slling 			}
8035094Slling 
8045094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
8055094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8065094Slling 				    "property name '%s' is too long"),
8072676Seschrock 				    propname);
8082676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8092676Seschrock 				goto error;
8102676Seschrock 			}
8112676Seschrock 
8122676Seschrock 			(void) nvpair_value_string(elem, &strval);
8132676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
8142676Seschrock 				(void) no_memory(hdl);
8152676Seschrock 				goto error;
8162676Seschrock 			}
8172676Seschrock 			continue;
818789Sahrens 		}
8192676Seschrock 
820*9396SMatthew.Ahrens@Sun.COM 		/*
821*9396SMatthew.Ahrens@Sun.COM 		 * Currently, only user properties can be modified on
822*9396SMatthew.Ahrens@Sun.COM 		 * snapshots.
823*9396SMatthew.Ahrens@Sun.COM 		 */
8247265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
8257265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8267265Sahrens 			    "this property can not be modified for snapshots"));
8277265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8287265Sahrens 			goto error;
8297265Sahrens 		}
8307265Sahrens 
831*9396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
832*9396SMatthew.Ahrens@Sun.COM 			zfs_userquota_prop_t uqtype;
833*9396SMatthew.Ahrens@Sun.COM 			char newpropname[128];
834*9396SMatthew.Ahrens@Sun.COM 			char domain[128];
835*9396SMatthew.Ahrens@Sun.COM 			uint64_t rid;
836*9396SMatthew.Ahrens@Sun.COM 			uint64_t valary[3];
837*9396SMatthew.Ahrens@Sun.COM 
838*9396SMatthew.Ahrens@Sun.COM 			if (userquota_propname_decode(propname, zoned,
839*9396SMatthew.Ahrens@Sun.COM 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
840*9396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
841*9396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN,
842*9396SMatthew.Ahrens@Sun.COM 				    "'%s' has an invalid user/group name"),
843*9396SMatthew.Ahrens@Sun.COM 				    propname);
844*9396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
845*9396SMatthew.Ahrens@Sun.COM 				goto error;
846*9396SMatthew.Ahrens@Sun.COM 			}
847*9396SMatthew.Ahrens@Sun.COM 
848*9396SMatthew.Ahrens@Sun.COM 			if (uqtype != ZFS_PROP_USERQUOTA &&
849*9396SMatthew.Ahrens@Sun.COM 			    uqtype != ZFS_PROP_GROUPQUOTA) {
850*9396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
851*9396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
852*9396SMatthew.Ahrens@Sun.COM 				    propname);
853*9396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
854*9396SMatthew.Ahrens@Sun.COM 				    errbuf);
855*9396SMatthew.Ahrens@Sun.COM 				goto error;
856*9396SMatthew.Ahrens@Sun.COM 			}
857*9396SMatthew.Ahrens@Sun.COM 
858*9396SMatthew.Ahrens@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
859*9396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_string(elem, &strval);
860*9396SMatthew.Ahrens@Sun.COM 				if (strcmp(strval, "none") == 0) {
861*9396SMatthew.Ahrens@Sun.COM 					intval = 0;
862*9396SMatthew.Ahrens@Sun.COM 				} else if (zfs_nicestrtonum(hdl,
863*9396SMatthew.Ahrens@Sun.COM 				    strval, &intval) != 0) {
864*9396SMatthew.Ahrens@Sun.COM 					(void) zfs_error(hdl,
865*9396SMatthew.Ahrens@Sun.COM 					    EZFS_BADPROP, errbuf);
866*9396SMatthew.Ahrens@Sun.COM 					goto error;
867*9396SMatthew.Ahrens@Sun.COM 				}
868*9396SMatthew.Ahrens@Sun.COM 			} else if (nvpair_type(elem) ==
869*9396SMatthew.Ahrens@Sun.COM 			    DATA_TYPE_UINT64) {
870*9396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_uint64(elem, &intval);
871*9396SMatthew.Ahrens@Sun.COM 				if (intval == 0) {
872*9396SMatthew.Ahrens@Sun.COM 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
873*9396SMatthew.Ahrens@Sun.COM 					    "use 'none' to disable "
874*9396SMatthew.Ahrens@Sun.COM 					    "userquota/groupquota"));
875*9396SMatthew.Ahrens@Sun.COM 					goto error;
876*9396SMatthew.Ahrens@Sun.COM 				}
877*9396SMatthew.Ahrens@Sun.COM 			} else {
878*9396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
879*9396SMatthew.Ahrens@Sun.COM 				    "'%s' must be a number"), propname);
880*9396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
881*9396SMatthew.Ahrens@Sun.COM 				goto error;
882*9396SMatthew.Ahrens@Sun.COM 			}
883*9396SMatthew.Ahrens@Sun.COM 
884*9396SMatthew.Ahrens@Sun.COM 			(void) snprintf(newpropname, sizeof (newpropname),
885*9396SMatthew.Ahrens@Sun.COM 			    "%s%s", zfs_userquota_prop_prefixes[uqtype],
886*9396SMatthew.Ahrens@Sun.COM 			    domain);
887*9396SMatthew.Ahrens@Sun.COM 			valary[0] = uqtype;
888*9396SMatthew.Ahrens@Sun.COM 			valary[1] = rid;
889*9396SMatthew.Ahrens@Sun.COM 			valary[2] = intval;
890*9396SMatthew.Ahrens@Sun.COM 			if (nvlist_add_uint64_array(ret, newpropname,
891*9396SMatthew.Ahrens@Sun.COM 			    valary, 3) != 0) {
892*9396SMatthew.Ahrens@Sun.COM 				(void) no_memory(hdl);
893*9396SMatthew.Ahrens@Sun.COM 				goto error;
894*9396SMatthew.Ahrens@Sun.COM 			}
895*9396SMatthew.Ahrens@Sun.COM 			continue;
896*9396SMatthew.Ahrens@Sun.COM 		}
897*9396SMatthew.Ahrens@Sun.COM 
898*9396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL) {
899*9396SMatthew.Ahrens@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
900*9396SMatthew.Ahrens@Sun.COM 			    "invalid property '%s'"), propname);
901*9396SMatthew.Ahrens@Sun.COM 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
902*9396SMatthew.Ahrens@Sun.COM 			goto error;
903*9396SMatthew.Ahrens@Sun.COM 		}
904*9396SMatthew.Ahrens@Sun.COM 
9052676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
9062676Seschrock 			zfs_error_aux(hdl,
9072676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
9082676Seschrock 			    "apply to datasets of this type"), propname);
9092676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
9102676Seschrock 			goto error;
9112676Seschrock 		}
9122676Seschrock 
9132676Seschrock 		if (zfs_prop_readonly(prop) &&
9145331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
9152676Seschrock 			zfs_error_aux(hdl,
9162676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
9172676Seschrock 			    propname);
9182676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
9192676Seschrock 			goto error;
9202676Seschrock 		}
9212676Seschrock 
9225094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
9235094Slling 		    &strval, &intval, errbuf) != 0)
9242676Seschrock 			goto error;
9252676Seschrock 
9262676Seschrock 		/*
9272676Seschrock 		 * Perform some additional checks for specific properties.
9282676Seschrock 		 */
9292676Seschrock 		switch (prop) {
9304577Sahrens 		case ZFS_PROP_VERSION:
9314577Sahrens 		{
9324577Sahrens 			int version;
9334577Sahrens 
9344577Sahrens 			if (zhp == NULL)
9354577Sahrens 				break;
9364577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
9374577Sahrens 			if (intval < version) {
9384577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9394577Sahrens 				    "Can not downgrade; already at version %u"),
9404577Sahrens 				    version);
9414577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9424577Sahrens 				goto error;
9434577Sahrens 			}
9444577Sahrens 			break;
9454577Sahrens 		}
9464577Sahrens 
9472676Seschrock 		case ZFS_PROP_RECORDSIZE:
9482676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
9492676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
9502676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
9512676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
9522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9532676Seschrock 				    "'%s' must be power of 2 from %u "
9542676Seschrock 				    "to %uk"), propname,
9552676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
9562676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
9572676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9582676Seschrock 				goto error;
959789Sahrens 			}
960789Sahrens 			break;
961789Sahrens 
9623126Sahl 		case ZFS_PROP_SHAREISCSI:
9633126Sahl 			if (strcmp(strval, "off") != 0 &&
9643126Sahl 			    strcmp(strval, "on") != 0 &&
9653126Sahl 			    strcmp(strval, "type=disk") != 0) {
9663126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9673126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
9683126Sahl 				    propname);
9693126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9703126Sahl 				goto error;
9713126Sahl 			}
9723126Sahl 
9733126Sahl 			break;
9743126Sahl 
9752676Seschrock 		case ZFS_PROP_MOUNTPOINT:
9764778Srm160521 		{
9774778Srm160521 			namecheck_err_t why;
9784778Srm160521 
9792676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
9802676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9812676Seschrock 				break;
9822676Seschrock 
9834778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
9844778Srm160521 				switch (why) {
9854778Srm160521 				case NAME_ERR_LEADING_SLASH:
9864778Srm160521 					zfs_error_aux(hdl,
9874778Srm160521 					    dgettext(TEXT_DOMAIN,
9884778Srm160521 					    "'%s' must be an absolute path, "
9894778Srm160521 					    "'none', or 'legacy'"), propname);
9904778Srm160521 					break;
9914778Srm160521 				case NAME_ERR_TOOLONG:
9924778Srm160521 					zfs_error_aux(hdl,
9934778Srm160521 					    dgettext(TEXT_DOMAIN,
9944778Srm160521 					    "component of '%s' is too long"),
9954778Srm160521 					    propname);
9964778Srm160521 					break;
9974778Srm160521 				}
9982676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9992676Seschrock 				goto error;
1000789Sahrens 			}
10014778Srm160521 		}
10024778Srm160521 
10033126Sahl 			/*FALLTHRU*/
10043126Sahl 
10055331Samw 		case ZFS_PROP_SHARESMB:
10063126Sahl 		case ZFS_PROP_SHARENFS:
10073126Sahl 			/*
10085331Samw 			 * For the mountpoint and sharenfs or sharesmb
10095331Samw 			 * properties, check if it can be set in a
10105331Samw 			 * global/non-global zone based on
10113126Sahl 			 * the zoned property value:
10123126Sahl 			 *
10133126Sahl 			 *		global zone	    non-global zone
10143126Sahl 			 * --------------------------------------------------
10153126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
10163126Sahl 			 *		sharenfs (no)	    sharenfs (no)
10175331Samw 			 *		sharesmb (no)	    sharesmb (no)
10183126Sahl 			 *
10193126Sahl 			 * zoned=off	mountpoint (yes)	N/A
10203126Sahl 			 *		sharenfs (yes)
10215331Samw 			 *		sharesmb (yes)
10223126Sahl 			 */
10232676Seschrock 			if (zoned) {
10242676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
10252676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10262676Seschrock 					    "'%s' cannot be set on "
10272676Seschrock 					    "dataset in a non-global zone"),
10282676Seschrock 					    propname);
10292676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
10302676Seschrock 					    errbuf);
10312676Seschrock 					goto error;
10325331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
10335331Samw 				    prop == ZFS_PROP_SHARESMB) {
10342676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10352676Seschrock 					    "'%s' cannot be set in "
10362676Seschrock 					    "a non-global zone"), propname);
10372676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
10382676Seschrock 					    errbuf);
10392676Seschrock 					goto error;
10402676Seschrock 				}
10412676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
10422676Seschrock 				/*
10432676Seschrock 				 * If zoned property is 'off', this must be in
1044*9396SMatthew.Ahrens@Sun.COM 				 * a global zone. If not, something is wrong.
10452676Seschrock 				 */
10462676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10472676Seschrock 				    "'%s' cannot be set while dataset "
10482676Seschrock 				    "'zoned' property is set"), propname);
10492676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
10502676Seschrock 				goto error;
10512676Seschrock 			}
10523126Sahl 
10534180Sdougm 			/*
10544180Sdougm 			 * At this point, it is legitimate to set the
10554180Sdougm 			 * property. Now we want to make sure that the
10564180Sdougm 			 * property value is valid if it is sharenfs.
10574180Sdougm 			 */
10585331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
10595331Samw 			    prop == ZFS_PROP_SHARESMB) &&
10604217Seschrock 			    strcmp(strval, "on") != 0 &&
10614217Seschrock 			    strcmp(strval, "off") != 0) {
10625331Samw 				zfs_share_proto_t proto;
10635331Samw 
10645331Samw 				if (prop == ZFS_PROP_SHARESMB)
10655331Samw 					proto = PROTO_SMB;
10665331Samw 				else
10675331Samw 					proto = PROTO_NFS;
10684180Sdougm 
10694180Sdougm 				/*
10705331Samw 				 * Must be an valid sharing protocol
10715331Samw 				 * option string so init the libshare
10725331Samw 				 * in order to enable the parser and
10735331Samw 				 * then parse the options. We use the
10745331Samw 				 * control API since we don't care about
10755331Samw 				 * the current configuration and don't
10764180Sdougm 				 * want the overhead of loading it
10774180Sdougm 				 * until we actually do something.
10784180Sdougm 				 */
10794180Sdougm 
10804217Seschrock 				if (zfs_init_libshare(hdl,
10814217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
10824217Seschrock 					/*
10834217Seschrock 					 * An error occurred so we can't do
10844217Seschrock 					 * anything
10854217Seschrock 					 */
10864217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10874217Seschrock 					    "'%s' cannot be set: problem "
10884217Seschrock 					    "in share initialization"),
10894217Seschrock 					    propname);
10904217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10914217Seschrock 					    errbuf);
10924217Seschrock 					goto error;
10934217Seschrock 				}
10944217Seschrock 
10955331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
10964217Seschrock 					/*
10974217Seschrock 					 * There was an error in parsing so
10984217Seschrock 					 * deal with it by issuing an error
10994217Seschrock 					 * message and leaving after
11004217Seschrock 					 * uninitializing the the libshare
11014217Seschrock 					 * interface.
11024217Seschrock 					 */
11034217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11044217Seschrock 					    "'%s' cannot be set to invalid "
11054217Seschrock 					    "options"), propname);
11064217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11074217Seschrock 					    errbuf);
11084217Seschrock 					zfs_uninit_libshare(hdl);
11094217Seschrock 					goto error;
11104217Seschrock 				}
11114180Sdougm 				zfs_uninit_libshare(hdl);
11124180Sdougm 			}
11134180Sdougm 
11143126Sahl 			break;
11155331Samw 		case ZFS_PROP_UTF8ONLY:
11165331Samw 			chosen_utf = (int)intval;
11175331Samw 			break;
11185331Samw 		case ZFS_PROP_NORMALIZE:
11195331Samw 			chosen_normal = (int)intval;
11205331Samw 			break;
11212676Seschrock 		}
11222676Seschrock 
11232676Seschrock 		/*
11242676Seschrock 		 * For changes to existing volumes, we have some additional
11252676Seschrock 		 * checks to enforce.
11262676Seschrock 		 */
11272676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
11282676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
11292676Seschrock 			    ZFS_PROP_VOLSIZE);
11302676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
11312676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
11322676Seschrock 			char buf[64];
11332676Seschrock 
11342676Seschrock 			switch (prop) {
11352676Seschrock 			case ZFS_PROP_RESERVATION:
11365378Sck153898 			case ZFS_PROP_REFRESERVATION:
11372676Seschrock 				if (intval > volsize) {
11382676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11392676Seschrock 					    "'%s' is greater than current "
11402676Seschrock 					    "volume size"), propname);
11412676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11422676Seschrock 					    errbuf);
11432676Seschrock 					goto error;
11442676Seschrock 				}
11452676Seschrock 				break;
11462676Seschrock 
11472676Seschrock 			case ZFS_PROP_VOLSIZE:
11482676Seschrock 				if (intval % blocksize != 0) {
11492676Seschrock 					zfs_nicenum(blocksize, buf,
11502676Seschrock 					    sizeof (buf));
11512676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11522676Seschrock 					    "'%s' must be a multiple of "
11532676Seschrock 					    "volume block size (%s)"),
11542676Seschrock 					    propname, buf);
11552676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11562676Seschrock 					    errbuf);
11572676Seschrock 					goto error;
11582676Seschrock 				}
11592676Seschrock 
11602676Seschrock 				if (intval == 0) {
11612676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11622676Seschrock 					    "'%s' cannot be zero"),
11632676Seschrock 					    propname);
11642676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11652676Seschrock 					    errbuf);
11662676Seschrock 					goto error;
1167789Sahrens 				}
11683126Sahl 				break;
1169789Sahrens 			}
1170789Sahrens 		}
1171789Sahrens 	}
1172789Sahrens 
11732676Seschrock 	/*
11745331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
11755331Samw 	 * enforce rejection of non-UTF8 names.
11765331Samw 	 *
11775331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
11785331Samw 	 * was explicitly not chosen, it is an error.
11795331Samw 	 */
11805498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
11815331Samw 		if (nvlist_add_uint64(ret,
11825331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
11835331Samw 			(void) no_memory(hdl);
11845331Samw 			goto error;
11855331Samw 		}
11865498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
11875331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11885331Samw 		    "'%s' must be set 'on' if normalization chosen"),
11895331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
11905331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11915331Samw 		goto error;
11925331Samw 	}
11935331Samw 
11945331Samw 	/*
11952676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
11962676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
11972676Seschrock 	 */
11982676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
11992676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
12002676Seschrock 	    &intval) == 0) {
12012676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
12022676Seschrock 		    ZFS_PROP_VOLSIZE);
12035481Sck153898 		uint64_t old_reservation;
12042676Seschrock 		uint64_t new_reservation;
12055481Sck153898 		zfs_prop_t resv_prop;
12065713Srm160521 
12075713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
12085481Sck153898 			goto error;
12095481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
12102676Seschrock 
12112676Seschrock 		if (old_volsize == old_reservation &&
12125481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
12132676Seschrock 		    &new_reservation) != 0) {
12142676Seschrock 			if (nvlist_add_uint64(ret,
12155481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
12162676Seschrock 				(void) no_memory(hdl);
12172676Seschrock 				goto error;
12182676Seschrock 			}
12192676Seschrock 		}
12202676Seschrock 	}
12212676Seschrock 	return (ret);
12222676Seschrock 
12232676Seschrock error:
12242676Seschrock 	nvlist_free(ret);
12252676Seschrock 	return (NULL);
1226789Sahrens }
1227789Sahrens 
1228789Sahrens /*
1229789Sahrens  * Given a property name and value, set the property for the given dataset.
1230789Sahrens  */
1231789Sahrens int
12322676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1233789Sahrens {
1234789Sahrens 	zfs_cmd_t zc = { 0 };
12352676Seschrock 	int ret = -1;
12362676Seschrock 	prop_changelist_t *cl = NULL;
12372082Seschrock 	char errbuf[1024];
12382082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
12392676Seschrock 	nvlist_t *nvl = NULL, *realprops;
12402676Seschrock 	zfs_prop_t prop;
12417509SMark.Musante@Sun.COM 	boolean_t do_prefix;
12427509SMark.Musante@Sun.COM 	uint64_t idx;
12432082Seschrock 
12442082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
12452676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
12462082Seschrock 	    zhp->zfs_name);
12472082Seschrock 
12482676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
12492676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
12502676Seschrock 		(void) no_memory(hdl);
12512676Seschrock 		goto error;
1252789Sahrens 	}
1253789Sahrens 
12547184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
12552676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
12562676Seschrock 		goto error;
12575094Slling 
12582676Seschrock 	nvlist_free(nvl);
12592676Seschrock 	nvl = realprops;
12602676Seschrock 
12612676Seschrock 	prop = zfs_name_to_prop(propname);
12622676Seschrock 
12637366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
12642676Seschrock 		goto error;
1265789Sahrens 
1266789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
12672082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12682082Seschrock 		    "child dataset with inherited mountpoint is used "
12692082Seschrock 		    "in a non-global zone"));
12702082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1271789Sahrens 		goto error;
1272789Sahrens 	}
1273789Sahrens 
12747509SMark.Musante@Sun.COM 	/*
12757509SMark.Musante@Sun.COM 	 * If the dataset's canmount property is being set to noauto,
12767509SMark.Musante@Sun.COM 	 * then we want to prevent unmounting & remounting it.
12777509SMark.Musante@Sun.COM 	 */
12787509SMark.Musante@Sun.COM 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
12797509SMark.Musante@Sun.COM 	    (zprop_string_to_index(prop, propval, &idx,
12807509SMark.Musante@Sun.COM 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
12816168Shs24103 
12826168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
12837509SMark.Musante@Sun.COM 		goto error;
1284789Sahrens 
1285789Sahrens 	/*
1286789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1287789Sahrens 	 */
1288789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1289789Sahrens 
12905094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
12912676Seschrock 		goto error;
12922676Seschrock 
12934543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
12948845Samw@Sun.COM 
1295789Sahrens 	if (ret != 0) {
1296789Sahrens 		switch (errno) {
1297789Sahrens 
1298789Sahrens 		case ENOSPC:
1299789Sahrens 			/*
1300789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1301789Sahrens 			 * something different; setting a quota or reservation
1302789Sahrens 			 * doesn't use any disk space.
1303789Sahrens 			 */
1304789Sahrens 			switch (prop) {
1305789Sahrens 			case ZFS_PROP_QUOTA:
13065378Sck153898 			case ZFS_PROP_REFQUOTA:
13072082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13082082Seschrock 				    "size is less than current used or "
13092082Seschrock 				    "reserved space"));
13102082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1311789Sahrens 				break;
1312789Sahrens 
1313789Sahrens 			case ZFS_PROP_RESERVATION:
13145378Sck153898 			case ZFS_PROP_REFRESERVATION:
13152082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13162082Seschrock 				    "size is greater than available space"));
13172082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1318789Sahrens 				break;
1319789Sahrens 
1320789Sahrens 			default:
13212082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1322789Sahrens 				break;
1323789Sahrens 			}
1324789Sahrens 			break;
1325789Sahrens 
1326789Sahrens 		case EBUSY:
13272082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
13282082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
13292082Seschrock 			else
13302676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1331789Sahrens 			break;
1332789Sahrens 
13331175Slling 		case EROFS:
13342082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
13351175Slling 			break;
13361175Slling 
13373886Sahl 		case ENOTSUP:
13383886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13395977Smarks 			    "pool and or dataset must be upgraded to set this "
13404603Sahrens 			    "property or value"));
13413886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
13423886Sahl 			break;
13433886Sahl 
13447042Sgw25295 		case ERANGE:
13457042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
13467042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13477042Sgw25295 				    "property setting is not allowed on "
13487042Sgw25295 				    "bootable datasets"));
13497042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
13507042Sgw25295 			} else {
13517042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
13527042Sgw25295 			}
13537042Sgw25295 			break;
13547042Sgw25295 
1355789Sahrens 		case EOVERFLOW:
1356789Sahrens 			/*
1357789Sahrens 			 * This platform can't address a volume this big.
1358789Sahrens 			 */
1359789Sahrens #ifdef _ILP32
1360789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
13612082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1362789Sahrens 				break;
1363789Sahrens 			}
1364789Sahrens #endif
13652082Seschrock 			/* FALLTHROUGH */
1366789Sahrens 		default:
13672082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1368789Sahrens 		}
1369789Sahrens 	} else {
13706168Shs24103 		if (do_prefix)
13716168Shs24103 			ret = changelist_postfix(cl);
13726168Shs24103 
1373789Sahrens 		/*
1374789Sahrens 		 * Refresh the statistics so the new property value
1375789Sahrens 		 * is reflected.
1376789Sahrens 		 */
13776168Shs24103 		if (ret == 0)
13782676Seschrock 			(void) get_stats(zhp);
1379789Sahrens 	}
1380789Sahrens 
1381789Sahrens error:
13822676Seschrock 	nvlist_free(nvl);
13832676Seschrock 	zcmd_free_nvlists(&zc);
13842676Seschrock 	if (cl)
13852676Seschrock 		changelist_free(cl);
1386789Sahrens 	return (ret);
1387789Sahrens }
1388789Sahrens 
1389789Sahrens /*
1390789Sahrens  * Given a property, inherit the value from the parent dataset.
1391789Sahrens  */
1392789Sahrens int
13932676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1394789Sahrens {
1395789Sahrens 	zfs_cmd_t zc = { 0 };
1396789Sahrens 	int ret;
1397789Sahrens 	prop_changelist_t *cl;
13982082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
13992082Seschrock 	char errbuf[1024];
14002676Seschrock 	zfs_prop_t prop;
14012082Seschrock 
14022082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
14032082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1404789Sahrens 
14055094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
14062676Seschrock 		/*
14072676Seschrock 		 * For user properties, the amount of work we have to do is very
14082676Seschrock 		 * small, so just do it here.
14092676Seschrock 		 */
14102676Seschrock 		if (!zfs_prop_user(propname)) {
14112676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14122676Seschrock 			    "invalid property"));
14132676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
14142676Seschrock 		}
14152676Seschrock 
14162676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14172676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
14182676Seschrock 
14194849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
14202676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
14212676Seschrock 
14222676Seschrock 		return (0);
14232676Seschrock 	}
14242676Seschrock 
1425789Sahrens 	/*
1426789Sahrens 	 * Verify that this property is inheritable.
1427789Sahrens 	 */
14282082Seschrock 	if (zfs_prop_readonly(prop))
14292082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
14302082Seschrock 
14312082Seschrock 	if (!zfs_prop_inheritable(prop))
14322082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1433789Sahrens 
1434789Sahrens 	/*
1435789Sahrens 	 * Check to see if the value applies to this type
1436789Sahrens 	 */
14372082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
14382082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1439789Sahrens 
14403443Srm160521 	/*
14413443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
14423443Srm160521 	 */
14433443Srm160521 	propname = zfs_prop_to_name(prop);
1444789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14452676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1446789Sahrens 
1447789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1448789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
14492082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14502082Seschrock 		    "dataset is used in a non-global zone"));
14512082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1452789Sahrens 	}
1453789Sahrens 
1454789Sahrens 	/*
1455789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1456789Sahrens 	 */
14577366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1458789Sahrens 		return (-1);
1459789Sahrens 
1460789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
14612082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14622082Seschrock 		    "child dataset with inherited mountpoint is used "
14632082Seschrock 		    "in a non-global zone"));
14642082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1465789Sahrens 		goto error;
1466789Sahrens 	}
1467789Sahrens 
1468789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1469789Sahrens 		goto error;
1470789Sahrens 
14714849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
14722082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1473789Sahrens 	} else {
1474789Sahrens 
14752169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1476789Sahrens 			goto error;
1477789Sahrens 
1478789Sahrens 		/*
1479789Sahrens 		 * Refresh the statistics so the new property is reflected.
1480789Sahrens 		 */
1481789Sahrens 		(void) get_stats(zhp);
1482789Sahrens 	}
1483789Sahrens 
1484789Sahrens error:
1485789Sahrens 	changelist_free(cl);
1486789Sahrens 	return (ret);
1487789Sahrens }
1488789Sahrens 
1489789Sahrens /*
14901356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
14911356Seschrock  * extract them appropriately.
14921356Seschrock  */
14931356Seschrock static uint64_t
14941356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
14951356Seschrock {
14961356Seschrock 	nvlist_t *nv;
14971356Seschrock 	uint64_t value;
14981356Seschrock 
14992885Sahrens 	*source = NULL;
15001356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
15011356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
15025094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
15035094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
15041356Seschrock 	} else {
15058802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
15068802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
15071356Seschrock 		value = zfs_prop_default_numeric(prop);
15081356Seschrock 		*source = "";
15091356Seschrock 	}
15101356Seschrock 
15111356Seschrock 	return (value);
15121356Seschrock }
15131356Seschrock 
15141356Seschrock static char *
15151356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
15161356Seschrock {
15171356Seschrock 	nvlist_t *nv;
15181356Seschrock 	char *value;
15191356Seschrock 
15202885Sahrens 	*source = NULL;
15211356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
15221356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
15235094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
15245094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
15251356Seschrock 	} else {
15268802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
15278802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
15281356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
15291356Seschrock 			value = "";
15301356Seschrock 		*source = "";
15311356Seschrock 	}
15321356Seschrock 
15331356Seschrock 	return (value);
15341356Seschrock }
15351356Seschrock 
15361356Seschrock /*
1537789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1538789Sahrens  * zfs_prop_get_int() are built using this interface.
1539789Sahrens  *
1540789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1541789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1542789Sahrens  * If they differ from the on-disk values, report the current values and mark
1543789Sahrens  * the source "temporary".
1544789Sahrens  */
15452082Seschrock static int
15465094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
15472082Seschrock     char **source, uint64_t *val)
1548789Sahrens {
15495147Srm160521 	zfs_cmd_t zc = { 0 };
15505592Stimh 	nvlist_t *zplprops = NULL;
1551789Sahrens 	struct mnttab mnt;
15523265Sahrens 	char *mntopt_on = NULL;
15533265Sahrens 	char *mntopt_off = NULL;
1554789Sahrens 
1555789Sahrens 	*source = NULL;
1556789Sahrens 
15573265Sahrens 	switch (prop) {
15583265Sahrens 	case ZFS_PROP_ATIME:
15593265Sahrens 		mntopt_on = MNTOPT_ATIME;
15603265Sahrens 		mntopt_off = MNTOPT_NOATIME;
15613265Sahrens 		break;
15623265Sahrens 
15633265Sahrens 	case ZFS_PROP_DEVICES:
15643265Sahrens 		mntopt_on = MNTOPT_DEVICES;
15653265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
15663265Sahrens 		break;
15673265Sahrens 
15683265Sahrens 	case ZFS_PROP_EXEC:
15693265Sahrens 		mntopt_on = MNTOPT_EXEC;
15703265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
15713265Sahrens 		break;
15723265Sahrens 
15733265Sahrens 	case ZFS_PROP_READONLY:
15743265Sahrens 		mntopt_on = MNTOPT_RO;
15753265Sahrens 		mntopt_off = MNTOPT_RW;
15763265Sahrens 		break;
15773265Sahrens 
15783265Sahrens 	case ZFS_PROP_SETUID:
15793265Sahrens 		mntopt_on = MNTOPT_SETUID;
15803265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
15813265Sahrens 		break;
15823265Sahrens 
15833265Sahrens 	case ZFS_PROP_XATTR:
15843265Sahrens 		mntopt_on = MNTOPT_XATTR;
15853265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
15863265Sahrens 		break;
15875331Samw 
15885331Samw 	case ZFS_PROP_NBMAND:
15895331Samw 		mntopt_on = MNTOPT_NBMAND;
15905331Samw 		mntopt_off = MNTOPT_NONBMAND;
15915331Samw 		break;
15923265Sahrens 	}
15933265Sahrens 
15942474Seschrock 	/*
15952474Seschrock 	 * Because looking up the mount options is potentially expensive
15962474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
15972474Seschrock 	 * we're looking up a property which requires its presence.
15982474Seschrock 	 */
15992474Seschrock 	if (!zhp->zfs_mntcheck &&
16003265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
16018228SEric.Taylor@Sun.COM 		libzfs_handle_t *hdl = zhp->zfs_hdl;
16028228SEric.Taylor@Sun.COM 		struct mnttab entry;
16038228SEric.Taylor@Sun.COM 
16048228SEric.Taylor@Sun.COM 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
16058228SEric.Taylor@Sun.COM 			zhp->zfs_mntopts = zfs_strdup(hdl,
16063265Sahrens 			    entry.mnt_mntopts);
16073265Sahrens 			if (zhp->zfs_mntopts == NULL)
16083265Sahrens 				return (-1);
16093265Sahrens 		}
16102474Seschrock 
16112474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
16122474Seschrock 	}
16132474Seschrock 
1614789Sahrens 	if (zhp->zfs_mntopts == NULL)
1615789Sahrens 		mnt.mnt_mntopts = "";
1616789Sahrens 	else
1617789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1618789Sahrens 
1619789Sahrens 	switch (prop) {
1620789Sahrens 	case ZFS_PROP_ATIME:
16213265Sahrens 	case ZFS_PROP_DEVICES:
16223265Sahrens 	case ZFS_PROP_EXEC:
16233265Sahrens 	case ZFS_PROP_READONLY:
16243265Sahrens 	case ZFS_PROP_SETUID:
16253265Sahrens 	case ZFS_PROP_XATTR:
16265331Samw 	case ZFS_PROP_NBMAND:
16272082Seschrock 		*val = getprop_uint64(zhp, prop, source);
16282082Seschrock 
16293265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
16302082Seschrock 			*val = B_TRUE;
1631789Sahrens 			if (src)
16325094Slling 				*src = ZPROP_SRC_TEMPORARY;
16333265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
16342082Seschrock 			*val = B_FALSE;
1635789Sahrens 			if (src)
16365094Slling 				*src = ZPROP_SRC_TEMPORARY;
1637789Sahrens 		}
16382082Seschrock 		break;
1639789Sahrens 
16403265Sahrens 	case ZFS_PROP_CANMOUNT:
16412082Seschrock 		*val = getprop_uint64(zhp, prop, source);
16426168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
16433417Srm160521 			*source = zhp->zfs_name;
16443417Srm160521 		else
16453417Srm160521 			*source = "";	/* default */
16462082Seschrock 		break;
1647789Sahrens 
1648789Sahrens 	case ZFS_PROP_QUOTA:
16495378Sck153898 	case ZFS_PROP_REFQUOTA:
1650789Sahrens 	case ZFS_PROP_RESERVATION:
16515378Sck153898 	case ZFS_PROP_REFRESERVATION:
16522885Sahrens 		*val = getprop_uint64(zhp, prop, source);
16532885Sahrens 		if (*val == 0)
1654789Sahrens 			*source = "";	/* default */
1655789Sahrens 		else
1656789Sahrens 			*source = zhp->zfs_name;
16572082Seschrock 		break;
1658789Sahrens 
1659789Sahrens 	case ZFS_PROP_MOUNTED:
16602082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
16612082Seschrock 		break;
1662789Sahrens 
16633377Seschrock 	case ZFS_PROP_NUMCLONES:
16643377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
16653377Seschrock 		break;
16663377Seschrock 
16675147Srm160521 	case ZFS_PROP_VERSION:
16685498Stimh 	case ZFS_PROP_NORMALIZE:
16695498Stimh 	case ZFS_PROP_UTF8ONLY:
16705498Stimh 	case ZFS_PROP_CASE:
16715498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
16725498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
16735473Srm160521 			return (-1);
16745147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16755498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
16765498Stimh 			zcmd_free_nvlists(&zc);
16775147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
16785498Stimh 			    "unable to get %s property"),
16795498Stimh 			    zfs_prop_to_name(prop));
16805147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
16815147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
16825147Srm160521 		}
16835498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
16845498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
16855498Stimh 		    val) != 0) {
16865498Stimh 			zcmd_free_nvlists(&zc);
16875498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
16885498Stimh 			    "unable to get %s property"),
16895498Stimh 			    zfs_prop_to_name(prop));
16905498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
16915498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
16925498Stimh 		}
16935592Stimh 		if (zplprops)
16945592Stimh 			nvlist_free(zplprops);
16955498Stimh 		zcmd_free_nvlists(&zc);
16965147Srm160521 		break;
16975147Srm160521 
1698789Sahrens 	default:
16994577Sahrens 		switch (zfs_prop_get_type(prop)) {
17004787Sahrens 		case PROP_TYPE_NUMBER:
17014787Sahrens 		case PROP_TYPE_INDEX:
17024577Sahrens 			*val = getprop_uint64(zhp, prop, source);
17037390SMatthew.Ahrens@Sun.COM 			/*
1704*9396SMatthew.Ahrens@Sun.COM 			 * If we tried to use a default value for a
17057390SMatthew.Ahrens@Sun.COM 			 * readonly property, it means that it was not
17067390SMatthew.Ahrens@Sun.COM 			 * present; return an error.
17077390SMatthew.Ahrens@Sun.COM 			 */
17087390SMatthew.Ahrens@Sun.COM 			if (zfs_prop_readonly(prop) &&
17097390SMatthew.Ahrens@Sun.COM 			    *source && (*source)[0] == '\0') {
17107390SMatthew.Ahrens@Sun.COM 				return (-1);
17117390SMatthew.Ahrens@Sun.COM 			}
17124577Sahrens 			break;
17134577Sahrens 
17144787Sahrens 		case PROP_TYPE_STRING:
17154577Sahrens 		default:
17164577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
17174577Sahrens 			    "cannot get non-numeric property"));
17184577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
17194577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
17204577Sahrens 		}
1721789Sahrens 	}
1722789Sahrens 
1723789Sahrens 	return (0);
1724789Sahrens }
1725789Sahrens 
1726789Sahrens /*
1727789Sahrens  * Calculate the source type, given the raw source string.
1728789Sahrens  */
1729789Sahrens static void
17305094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1731789Sahrens     char *statbuf, size_t statlen)
1732789Sahrens {
17335094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1734789Sahrens 		return;
1735789Sahrens 
1736789Sahrens 	if (source == NULL) {
17375094Slling 		*srctype = ZPROP_SRC_NONE;
1738789Sahrens 	} else if (source[0] == '\0') {
17395094Slling 		*srctype = ZPROP_SRC_DEFAULT;
1740789Sahrens 	} else {
1741789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
17425094Slling 			*srctype = ZPROP_SRC_LOCAL;
1743789Sahrens 		} else {
1744789Sahrens 			(void) strlcpy(statbuf, source, statlen);
17455094Slling 			*srctype = ZPROP_SRC_INHERITED;
1746789Sahrens 		}
1747789Sahrens 	}
1748789Sahrens 
1749789Sahrens }
1750789Sahrens 
1751789Sahrens /*
1752789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
1753789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
1754789Sahrens  * human-readable form.
1755789Sahrens  *
1756789Sahrens  * Returns 0 on success, or -1 on error.
1757789Sahrens  */
1758789Sahrens int
1759789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
17605094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1761789Sahrens {
1762789Sahrens 	char *source = NULL;
1763789Sahrens 	uint64_t val;
1764789Sahrens 	char *str;
17652676Seschrock 	const char *strval;
1766789Sahrens 
1767789Sahrens 	/*
1768789Sahrens 	 * Check to see if this property applies to our object
1769789Sahrens 	 */
1770789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1771789Sahrens 		return (-1);
1772789Sahrens 
1773789Sahrens 	if (src)
17745094Slling 		*src = ZPROP_SRC_NONE;
1775789Sahrens 
1776789Sahrens 	switch (prop) {
1777789Sahrens 	case ZFS_PROP_CREATION:
1778789Sahrens 		/*
1779789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
1780789Sahrens 		 * this into a string unless 'literal' is specified.
1781789Sahrens 		 */
1782789Sahrens 		{
17832885Sahrens 			val = getprop_uint64(zhp, prop, &source);
17842885Sahrens 			time_t time = (time_t)val;
1785789Sahrens 			struct tm t;
1786789Sahrens 
1787789Sahrens 			if (literal ||
1788789Sahrens 			    localtime_r(&time, &t) == NULL ||
1789789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1790789Sahrens 			    &t) == 0)
17912885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
1792789Sahrens 		}
1793789Sahrens 		break;
1794789Sahrens 
1795789Sahrens 	case ZFS_PROP_MOUNTPOINT:
1796789Sahrens 		/*
1797789Sahrens 		 * Getting the precise mountpoint can be tricky.
1798789Sahrens 		 *
1799789Sahrens 		 *  - for 'none' or 'legacy', return those values.
1800789Sahrens 		 *  - for inherited mountpoints, we want to take everything
1801789Sahrens 		 *    after our ancestor and append it to the inherited value.
1802789Sahrens 		 *
1803789Sahrens 		 * If the pool has an alternate root, we want to prepend that
1804789Sahrens 		 * root to any values we return.
1805789Sahrens 		 */
18066865Srm160521 
18071356Seschrock 		str = getprop_string(zhp, prop, &source);
18081356Seschrock 
18096612Sgw25295 		if (str[0] == '/') {
18106865Srm160521 			char buf[MAXPATHLEN];
18116865Srm160521 			char *root = buf;
18121356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
1813789Sahrens 
1814789Sahrens 			if (relpath[0] == '/')
1815789Sahrens 				relpath++;
18166612Sgw25295 
18176865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
18186865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
18196865Srm160521 			    (strcmp(root, "-") == 0))
18206865Srm160521 				root[0] = '\0';
18216612Sgw25295 			/*
18226612Sgw25295 			 * Special case an alternate root of '/'. This will
18236612Sgw25295 			 * avoid having multiple leading slashes in the
18246612Sgw25295 			 * mountpoint path.
18256612Sgw25295 			 */
18266612Sgw25295 			if (strcmp(root, "/") == 0)
18276612Sgw25295 				root++;
18286612Sgw25295 
18296612Sgw25295 			/*
18306612Sgw25295 			 * If the mountpoint is '/' then skip over this
18316612Sgw25295 			 * if we are obtaining either an alternate root or
18326612Sgw25295 			 * an inherited mountpoint.
18336612Sgw25295 			 */
18346612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
18356612Sgw25295 			    relpath[0] != '\0'))
18361356Seschrock 				str++;
1837789Sahrens 
1838789Sahrens 			if (relpath[0] == '\0')
1839789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
18401356Seschrock 				    root, str);
1841789Sahrens 			else
1842789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
18431356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
1844789Sahrens 				    relpath);
1845789Sahrens 		} else {
1846789Sahrens 			/* 'legacy' or 'none' */
18471356Seschrock 			(void) strlcpy(propbuf, str, proplen);
1848789Sahrens 		}
1849789Sahrens 
1850789Sahrens 		break;
1851789Sahrens 
1852789Sahrens 	case ZFS_PROP_ORIGIN:
18532885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1854789Sahrens 		    proplen);
1855789Sahrens 		/*
1856789Sahrens 		 * If there is no parent at all, return failure to indicate that
1857789Sahrens 		 * it doesn't apply to this dataset.
1858789Sahrens 		 */
1859789Sahrens 		if (propbuf[0] == '\0')
1860789Sahrens 			return (-1);
1861789Sahrens 		break;
1862789Sahrens 
1863789Sahrens 	case ZFS_PROP_QUOTA:
18645378Sck153898 	case ZFS_PROP_REFQUOTA:
1865789Sahrens 	case ZFS_PROP_RESERVATION:
18665378Sck153898 	case ZFS_PROP_REFRESERVATION:
18675378Sck153898 
18682082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
18692082Seschrock 			return (-1);
1870789Sahrens 
1871789Sahrens 		/*
1872789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
1873789Sahrens 		 * (unless literal is set), and indicate that it's the default
1874789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
1875789Sahrens 		 * that its set locally.
1876789Sahrens 		 */
1877789Sahrens 		if (val == 0) {
1878789Sahrens 			if (literal)
1879789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
1880789Sahrens 			else
1881789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
1882789Sahrens 		} else {
1883789Sahrens 			if (literal)
18842856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
18853912Slling 				    (u_longlong_t)val);
1886789Sahrens 			else
1887789Sahrens 				zfs_nicenum(val, propbuf, proplen);
1888789Sahrens 		}
1889789Sahrens 		break;
1890789Sahrens 
1891789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
18922082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
18932082Seschrock 			return (-1);
18942856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
18952856Snd150628 		    val / 100, (longlong_t)val % 100);
1896789Sahrens 		break;
1897789Sahrens 
1898789Sahrens 	case ZFS_PROP_TYPE:
1899789Sahrens 		switch (zhp->zfs_type) {
1900789Sahrens 		case ZFS_TYPE_FILESYSTEM:
1901789Sahrens 			str = "filesystem";
1902789Sahrens 			break;
1903789Sahrens 		case ZFS_TYPE_VOLUME:
1904789Sahrens 			str = "volume";
1905789Sahrens 			break;
1906789Sahrens 		case ZFS_TYPE_SNAPSHOT:
1907789Sahrens 			str = "snapshot";
1908789Sahrens 			break;
1909789Sahrens 		default:
19102082Seschrock 			abort();
1911789Sahrens 		}
1912789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
1913789Sahrens 		break;
1914789Sahrens 
1915789Sahrens 	case ZFS_PROP_MOUNTED:
1916789Sahrens 		/*
1917789Sahrens 		 * The 'mounted' property is a pseudo-property that described
1918789Sahrens 		 * whether the filesystem is currently mounted.  Even though
1919789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
1920789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
1921789Sahrens 		 */
19222082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
19232082Seschrock 		    src, &source, &val) != 0)
19242082Seschrock 			return (-1);
19252082Seschrock 		if (val)
1926789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
1927789Sahrens 		else
1928789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
1929789Sahrens 		break;
1930789Sahrens 
1931789Sahrens 	case ZFS_PROP_NAME:
1932789Sahrens 		/*
1933789Sahrens 		 * The 'name' property is a pseudo-property derived from the
1934789Sahrens 		 * dataset name.  It is presented as a real property to simplify
1935789Sahrens 		 * consumers.
1936789Sahrens 		 */
1937789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
1938789Sahrens 		break;
1939789Sahrens 
1940789Sahrens 	default:
19414577Sahrens 		switch (zfs_prop_get_type(prop)) {
19424787Sahrens 		case PROP_TYPE_NUMBER:
19434577Sahrens 			if (get_numeric_property(zhp, prop, src,
19444577Sahrens 			    &source, &val) != 0)
19454577Sahrens 				return (-1);
19464577Sahrens 			if (literal)
19474577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
19484577Sahrens 				    (u_longlong_t)val);
19494577Sahrens 			else
19504577Sahrens 				zfs_nicenum(val, propbuf, proplen);
19514577Sahrens 			break;
19524577Sahrens 
19534787Sahrens 		case PROP_TYPE_STRING:
19544577Sahrens 			(void) strlcpy(propbuf,
19554577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
19564577Sahrens 			break;
19574577Sahrens 
19584787Sahrens 		case PROP_TYPE_INDEX:
19594861Sahrens 			if (get_numeric_property(zhp, prop, src,
19604861Sahrens 			    &source, &val) != 0)
19614861Sahrens 				return (-1);
19624861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
19634577Sahrens 				return (-1);
19644577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
19654577Sahrens 			break;
19664577Sahrens 
19674577Sahrens 		default:
19684577Sahrens 			abort();
19694577Sahrens 		}
1970789Sahrens 	}
1971789Sahrens 
1972789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
1973789Sahrens 
1974789Sahrens 	return (0);
1975789Sahrens }
1976789Sahrens 
1977789Sahrens /*
1978789Sahrens  * Utility function to get the given numeric property.  Does no validation that
1979789Sahrens  * the given property is the appropriate type; should only be used with
1980789Sahrens  * hard-coded property types.
1981789Sahrens  */
1982789Sahrens uint64_t
1983789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1984789Sahrens {
1985789Sahrens 	char *source;
19862082Seschrock 	uint64_t val;
19872082Seschrock 
19885367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
19892082Seschrock 
19902082Seschrock 	return (val);
1991789Sahrens }
1992789Sahrens 
19935713Srm160521 int
19945713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
19955713Srm160521 {
19965713Srm160521 	char buf[64];
19975713Srm160521 
1998*9396SMatthew.Ahrens@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
19995713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
20005713Srm160521 }
20015713Srm160521 
2002789Sahrens /*
2003789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2004789Sahrens  */
2005789Sahrens int
2006789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
20075094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2008789Sahrens {
2009789Sahrens 	char *source;
2010789Sahrens 
2011789Sahrens 	/*
2012789Sahrens 	 * Check to see if this property applies to our object
2013789Sahrens 	 */
20144849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
20153237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
20162082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
20172082Seschrock 		    zfs_prop_to_name(prop)));
20184849Sahrens 	}
2019789Sahrens 
2020789Sahrens 	if (src)
20215094Slling 		*src = ZPROP_SRC_NONE;
2022789Sahrens 
20232082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
20242082Seschrock 		return (-1);
2025789Sahrens 
2026789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2027789Sahrens 
2028789Sahrens 	return (0);
2029789Sahrens }
2030789Sahrens 
2031*9396SMatthew.Ahrens@Sun.COM static int
2032*9396SMatthew.Ahrens@Sun.COM idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2033*9396SMatthew.Ahrens@Sun.COM     char **domainp, idmap_rid_t *ridp)
2034*9396SMatthew.Ahrens@Sun.COM {
2035*9396SMatthew.Ahrens@Sun.COM 	idmap_handle_t *idmap_hdl = NULL;
2036*9396SMatthew.Ahrens@Sun.COM 	idmap_get_handle_t *get_hdl = NULL;
2037*9396SMatthew.Ahrens@Sun.COM 	idmap_stat status;
2038*9396SMatthew.Ahrens@Sun.COM 	int err = EINVAL;
2039*9396SMatthew.Ahrens@Sun.COM 
2040*9396SMatthew.Ahrens@Sun.COM 	if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
2041*9396SMatthew.Ahrens@Sun.COM 		goto out;
2042*9396SMatthew.Ahrens@Sun.COM 	if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
2043*9396SMatthew.Ahrens@Sun.COM 		goto out;
2044*9396SMatthew.Ahrens@Sun.COM 
2045*9396SMatthew.Ahrens@Sun.COM 	if (isuser) {
2046*9396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbyuid(get_hdl, id,
2047*9396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2048*9396SMatthew.Ahrens@Sun.COM 	} else {
2049*9396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbygid(get_hdl, id,
2050*9396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2051*9396SMatthew.Ahrens@Sun.COM 	}
2052*9396SMatthew.Ahrens@Sun.COM 	if (err == IDMAP_SUCCESS &&
2053*9396SMatthew.Ahrens@Sun.COM 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2054*9396SMatthew.Ahrens@Sun.COM 	    status == IDMAP_SUCCESS)
2055*9396SMatthew.Ahrens@Sun.COM 		err = 0;
2056*9396SMatthew.Ahrens@Sun.COM 	else
2057*9396SMatthew.Ahrens@Sun.COM 		err = EINVAL;
2058*9396SMatthew.Ahrens@Sun.COM out:
2059*9396SMatthew.Ahrens@Sun.COM 	if (get_hdl)
2060*9396SMatthew.Ahrens@Sun.COM 		idmap_get_destroy(get_hdl);
2061*9396SMatthew.Ahrens@Sun.COM 	if (idmap_hdl)
2062*9396SMatthew.Ahrens@Sun.COM 		(void) idmap_fini(idmap_hdl);
2063*9396SMatthew.Ahrens@Sun.COM 	return (err);
2064*9396SMatthew.Ahrens@Sun.COM }
2065*9396SMatthew.Ahrens@Sun.COM 
2066*9396SMatthew.Ahrens@Sun.COM /*
2067*9396SMatthew.Ahrens@Sun.COM  * convert the propname into parameters needed by kernel
2068*9396SMatthew.Ahrens@Sun.COM  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2069*9396SMatthew.Ahrens@Sun.COM  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2070*9396SMatthew.Ahrens@Sun.COM  */
2071*9396SMatthew.Ahrens@Sun.COM static int
2072*9396SMatthew.Ahrens@Sun.COM userquota_propname_decode(const char *propname, boolean_t zoned,
2073*9396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2074*9396SMatthew.Ahrens@Sun.COM {
2075*9396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t type;
2076*9396SMatthew.Ahrens@Sun.COM 	char *cp, *end;
2077*9396SMatthew.Ahrens@Sun.COM 	boolean_t isuser;
2078*9396SMatthew.Ahrens@Sun.COM 
2079*9396SMatthew.Ahrens@Sun.COM 	domain[0] = '\0';
2080*9396SMatthew.Ahrens@Sun.COM 
2081*9396SMatthew.Ahrens@Sun.COM 	/* Figure out the property type ({user|group}{quota|space}) */
2082*9396SMatthew.Ahrens@Sun.COM 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2083*9396SMatthew.Ahrens@Sun.COM 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2084*9396SMatthew.Ahrens@Sun.COM 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
2085*9396SMatthew.Ahrens@Sun.COM 			break;
2086*9396SMatthew.Ahrens@Sun.COM 	}
2087*9396SMatthew.Ahrens@Sun.COM 	if (type == ZFS_NUM_USERQUOTA_PROPS)
2088*9396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
2089*9396SMatthew.Ahrens@Sun.COM 	*typep = type;
2090*9396SMatthew.Ahrens@Sun.COM 
2091*9396SMatthew.Ahrens@Sun.COM 	isuser = (type == ZFS_PROP_USERQUOTA ||
2092*9396SMatthew.Ahrens@Sun.COM 	    type == ZFS_PROP_USERUSED);
2093*9396SMatthew.Ahrens@Sun.COM 
2094*9396SMatthew.Ahrens@Sun.COM 	cp = strchr(propname, '@') + 1;
2095*9396SMatthew.Ahrens@Sun.COM 
2096*9396SMatthew.Ahrens@Sun.COM 	if (strchr(cp, '@')) {
2097*9396SMatthew.Ahrens@Sun.COM 		/*
2098*9396SMatthew.Ahrens@Sun.COM 		 * It's a SID name (eg "user@domain") that needs to be
2099*9396SMatthew.Ahrens@Sun.COM 		 * turned into S-1-domainID-RID.  There should be a
2100*9396SMatthew.Ahrens@Sun.COM 		 * better way to do this, but for now just translate it
2101*9396SMatthew.Ahrens@Sun.COM 		 * to the (possibly ephemeral) uid and then back to the
2102*9396SMatthew.Ahrens@Sun.COM 		 * SID.  This is like getsidname(noresolve=TRUE).
2103*9396SMatthew.Ahrens@Sun.COM 		 */
2104*9396SMatthew.Ahrens@Sun.COM 		uid_t id;
2105*9396SMatthew.Ahrens@Sun.COM 		idmap_rid_t rid;
2106*9396SMatthew.Ahrens@Sun.COM 		char *mapdomain;
2107*9396SMatthew.Ahrens@Sun.COM 
2108*9396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2109*9396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
2110*9396SMatthew.Ahrens@Sun.COM 		if (sid_to_id(cp, isuser, &id) != 0)
2111*9396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
2112*9396SMatthew.Ahrens@Sun.COM 		if (idmap_id_to_numeric_domain_rid(id, isuser,
2113*9396SMatthew.Ahrens@Sun.COM 		    &mapdomain, &rid) != 0)
2114*9396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
2115*9396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domain, mapdomain, domainlen);
2116*9396SMatthew.Ahrens@Sun.COM 		*ridp = rid;
2117*9396SMatthew.Ahrens@Sun.COM 	} else if (strncmp(cp, "S-1-", 4) == 0) {
2118*9396SMatthew.Ahrens@Sun.COM 		/* It's a numeric SID (eg "S-1-234-567-89") */
2119*9396SMatthew.Ahrens@Sun.COM 		(void) strcpy(domain, cp);
2120*9396SMatthew.Ahrens@Sun.COM 		cp = strrchr(domain, '-');
2121*9396SMatthew.Ahrens@Sun.COM 		*cp = '\0';
2122*9396SMatthew.Ahrens@Sun.COM 		cp++;
2123*9396SMatthew.Ahrens@Sun.COM 
2124*9396SMatthew.Ahrens@Sun.COM 		errno = 0;
2125*9396SMatthew.Ahrens@Sun.COM 		*ridp = strtoull(cp, &end, 10);
2126*9396SMatthew.Ahrens@Sun.COM 		if (errno == 0 || *end != '\0')
2127*9396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
2128*9396SMatthew.Ahrens@Sun.COM 	} else if (!isdigit(*cp)) {
2129*9396SMatthew.Ahrens@Sun.COM 		/*
2130*9396SMatthew.Ahrens@Sun.COM 		 * It's a user/group name (eg "user") that needs to be
2131*9396SMatthew.Ahrens@Sun.COM 		 * turned into a uid/gid
2132*9396SMatthew.Ahrens@Sun.COM 		 */
2133*9396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2134*9396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
2135*9396SMatthew.Ahrens@Sun.COM 		if (isuser) {
2136*9396SMatthew.Ahrens@Sun.COM 			struct passwd *pw;
2137*9396SMatthew.Ahrens@Sun.COM 			pw = getpwnam(cp);
2138*9396SMatthew.Ahrens@Sun.COM 			if (pw == NULL)
2139*9396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
2140*9396SMatthew.Ahrens@Sun.COM 			*ridp = pw->pw_uid;
2141*9396SMatthew.Ahrens@Sun.COM 		} else {
2142*9396SMatthew.Ahrens@Sun.COM 			struct group *gr;
2143*9396SMatthew.Ahrens@Sun.COM 			gr = getgrnam(cp);
2144*9396SMatthew.Ahrens@Sun.COM 			if (gr == NULL)
2145*9396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
2146*9396SMatthew.Ahrens@Sun.COM 			*ridp = gr->gr_gid;
2147*9396SMatthew.Ahrens@Sun.COM 		}
2148*9396SMatthew.Ahrens@Sun.COM 	} else {
2149*9396SMatthew.Ahrens@Sun.COM 		/* It's a user/group ID (eg "12345"). */
2150*9396SMatthew.Ahrens@Sun.COM 		uid_t id = strtoul(cp, &end, 10);
2151*9396SMatthew.Ahrens@Sun.COM 		idmap_rid_t rid;
2152*9396SMatthew.Ahrens@Sun.COM 		char *mapdomain;
2153*9396SMatthew.Ahrens@Sun.COM 
2154*9396SMatthew.Ahrens@Sun.COM 		if (*end != '\0')
2155*9396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
2156*9396SMatthew.Ahrens@Sun.COM 		if (id > MAXUID) {
2157*9396SMatthew.Ahrens@Sun.COM 			/* It's an ephemeral ID. */
2158*9396SMatthew.Ahrens@Sun.COM 			if (idmap_id_to_numeric_domain_rid(id, isuser,
2159*9396SMatthew.Ahrens@Sun.COM 			    &mapdomain, &rid) != 0)
2160*9396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
2161*9396SMatthew.Ahrens@Sun.COM 			(void) strcpy(domain, mapdomain);
2162*9396SMatthew.Ahrens@Sun.COM 			*ridp = rid;
2163*9396SMatthew.Ahrens@Sun.COM 		} else {
2164*9396SMatthew.Ahrens@Sun.COM 			*ridp = id;
2165*9396SMatthew.Ahrens@Sun.COM 		}
2166*9396SMatthew.Ahrens@Sun.COM 	}
2167*9396SMatthew.Ahrens@Sun.COM 
2168*9396SMatthew.Ahrens@Sun.COM 	return (0);
2169*9396SMatthew.Ahrens@Sun.COM }
2170*9396SMatthew.Ahrens@Sun.COM 
2171*9396SMatthew.Ahrens@Sun.COM int
2172*9396SMatthew.Ahrens@Sun.COM zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2173*9396SMatthew.Ahrens@Sun.COM     char *propbuf, int proplen, boolean_t literal)
2174*9396SMatthew.Ahrens@Sun.COM {
2175*9396SMatthew.Ahrens@Sun.COM 	int err;
2176*9396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
2177*9396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t type;
2178*9396SMatthew.Ahrens@Sun.COM 
2179*9396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2180*9396SMatthew.Ahrens@Sun.COM 
2181*9396SMatthew.Ahrens@Sun.COM 	err = userquota_propname_decode(propname,
2182*9396SMatthew.Ahrens@Sun.COM 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2183*9396SMatthew.Ahrens@Sun.COM 	    &type, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2184*9396SMatthew.Ahrens@Sun.COM 	zc.zc_objset_type = type;
2185*9396SMatthew.Ahrens@Sun.COM 	if (err)
2186*9396SMatthew.Ahrens@Sun.COM 		return (err);
2187*9396SMatthew.Ahrens@Sun.COM 
2188*9396SMatthew.Ahrens@Sun.COM 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2189*9396SMatthew.Ahrens@Sun.COM 	if (err)
2190*9396SMatthew.Ahrens@Sun.COM 		return (err);
2191*9396SMatthew.Ahrens@Sun.COM 
2192*9396SMatthew.Ahrens@Sun.COM 	if (literal) {
2193*9396SMatthew.Ahrens@Sun.COM 		(void) snprintf(propbuf, proplen, "%llu",
2194*9396SMatthew.Ahrens@Sun.COM 		    (u_longlong_t)zc.zc_cookie);
2195*9396SMatthew.Ahrens@Sun.COM 	} else if (zc.zc_cookie == 0 &&
2196*9396SMatthew.Ahrens@Sun.COM 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2197*9396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(propbuf, "none", proplen);
2198*9396SMatthew.Ahrens@Sun.COM 	} else {
2199*9396SMatthew.Ahrens@Sun.COM 		zfs_nicenum(zc.zc_cookie, propbuf, proplen);
2200*9396SMatthew.Ahrens@Sun.COM 	}
2201*9396SMatthew.Ahrens@Sun.COM 	return (0);
2202*9396SMatthew.Ahrens@Sun.COM }
2203*9396SMatthew.Ahrens@Sun.COM 
2204789Sahrens /*
2205789Sahrens  * Returns the name of the given zfs handle.
2206789Sahrens  */
2207789Sahrens const char *
2208789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2209789Sahrens {
2210789Sahrens 	return (zhp->zfs_name);
2211789Sahrens }
2212789Sahrens 
2213789Sahrens /*
2214789Sahrens  * Returns the type of the given zfs handle.
2215789Sahrens  */
2216789Sahrens zfs_type_t
2217789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2218789Sahrens {
2219789Sahrens 	return (zhp->zfs_type);
2220789Sahrens }
2221789Sahrens 
22228228SEric.Taylor@Sun.COM static int
22238228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
22248228SEric.Taylor@Sun.COM {
22258228SEric.Taylor@Sun.COM 	int rc;
22268228SEric.Taylor@Sun.COM 	uint64_t	orig_cookie;
22278228SEric.Taylor@Sun.COM 
22288228SEric.Taylor@Sun.COM 	orig_cookie = zc->zc_cookie;
22298228SEric.Taylor@Sun.COM top:
22308228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
22318228SEric.Taylor@Sun.COM 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
22328228SEric.Taylor@Sun.COM 
22338228SEric.Taylor@Sun.COM 	if (rc == -1) {
22348228SEric.Taylor@Sun.COM 		switch (errno) {
22358228SEric.Taylor@Sun.COM 		case ENOMEM:
22368228SEric.Taylor@Sun.COM 			/* expand nvlist memory and try again */
22378228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
22388228SEric.Taylor@Sun.COM 				zcmd_free_nvlists(zc);
22398228SEric.Taylor@Sun.COM 				return (-1);
22408228SEric.Taylor@Sun.COM 			}
22418228SEric.Taylor@Sun.COM 			zc->zc_cookie = orig_cookie;
22428228SEric.Taylor@Sun.COM 			goto top;
22438228SEric.Taylor@Sun.COM 		/*
22448228SEric.Taylor@Sun.COM 		 * An errno value of ESRCH indicates normal completion.
22458228SEric.Taylor@Sun.COM 		 * If ENOENT is returned, then the underlying dataset
22468228SEric.Taylor@Sun.COM 		 * has been removed since we obtained the handle.
22478228SEric.Taylor@Sun.COM 		 */
22488228SEric.Taylor@Sun.COM 		case ESRCH:
22498228SEric.Taylor@Sun.COM 		case ENOENT:
22508228SEric.Taylor@Sun.COM 			rc = 1;
22518228SEric.Taylor@Sun.COM 			break;
22528228SEric.Taylor@Sun.COM 		default:
22538228SEric.Taylor@Sun.COM 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
22548228SEric.Taylor@Sun.COM 			    dgettext(TEXT_DOMAIN,
22558228SEric.Taylor@Sun.COM 			    "cannot iterate filesystems"));
22568228SEric.Taylor@Sun.COM 			break;
22578228SEric.Taylor@Sun.COM 		}
22588228SEric.Taylor@Sun.COM 	}
22598228SEric.Taylor@Sun.COM 	return (rc);
22608228SEric.Taylor@Sun.COM }
22618228SEric.Taylor@Sun.COM 
2262789Sahrens /*
22631356Seschrock  * Iterate over all child filesystems
2264789Sahrens  */
2265789Sahrens int
22661356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2267789Sahrens {
2268789Sahrens 	zfs_cmd_t zc = { 0 };
2269789Sahrens 	zfs_handle_t *nzhp;
2270789Sahrens 	int ret;
2271789Sahrens 
22725367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
22735367Sahrens 		return (0);
22745367Sahrens 
22758228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22768228SEric.Taylor@Sun.COM 		return (-1);
22778228SEric.Taylor@Sun.COM 
22788228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
22798228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
2280789Sahrens 		/*
2281789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2282789Sahrens 		 * that the pool has since been removed.
2283789Sahrens 		 */
22848228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
22858228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2286789Sahrens 			continue;
22878228SEric.Taylor@Sun.COM 		}
22888228SEric.Taylor@Sun.COM 
22898228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
22908228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2291789Sahrens 			return (ret);
22928228SEric.Taylor@Sun.COM 		}
2293789Sahrens 	}
22948228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
22958228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
22961356Seschrock }
22971356Seschrock 
22981356Seschrock /*
22991356Seschrock  * Iterate over all snapshots
23001356Seschrock  */
23011356Seschrock int
23021356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23031356Seschrock {
23041356Seschrock 	zfs_cmd_t zc = { 0 };
23051356Seschrock 	zfs_handle_t *nzhp;
23061356Seschrock 	int ret;
2307789Sahrens 
23085367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
23095367Sahrens 		return (0);
23105367Sahrens 
23118228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
23128228SEric.Taylor@Sun.COM 		return (-1);
23138228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
23148228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
23158228SEric.Taylor@Sun.COM 
23168228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
23178228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2318789Sahrens 			continue;
23198228SEric.Taylor@Sun.COM 		}
23208228SEric.Taylor@Sun.COM 
23218228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
23228228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2323789Sahrens 			return (ret);
23248228SEric.Taylor@Sun.COM 		}
2325789Sahrens 	}
23268228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
23278228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
2328789Sahrens }
2329789Sahrens 
2330789Sahrens /*
23311356Seschrock  * Iterate over all children, snapshots and filesystems
23321356Seschrock  */
23331356Seschrock int
23341356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23351356Seschrock {
23361356Seschrock 	int ret;
23371356Seschrock 
23381356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
23391356Seschrock 		return (ret);
23401356Seschrock 
23411356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
23421356Seschrock }
23431356Seschrock 
23441356Seschrock /*
2345789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2346789Sahrens  * Can return NULL if this is a pool.
2347789Sahrens  */
2348789Sahrens static int
2349789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2350789Sahrens {
2351789Sahrens 	char *loc;
2352789Sahrens 
2353789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2354789Sahrens 		return (-1);
2355789Sahrens 
2356789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2357789Sahrens 	buf[loc - path] = '\0';
2358789Sahrens 
2359789Sahrens 	return (0);
2360789Sahrens }
2361789Sahrens 
2362789Sahrens /*
23634490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
23644490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
23654490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
23664490Svb160487  * length of already existing prefix of the given path.  We also fetch the
23674490Svb160487  * 'zoned' property, which is used to validate property settings when creating
23684490Svb160487  * new datasets.
2369789Sahrens  */
2370789Sahrens static int
23714490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
23724490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2373789Sahrens {
2374789Sahrens 	zfs_cmd_t zc = { 0 };
2375789Sahrens 	char parent[ZFS_MAXNAMELEN];
2376789Sahrens 	char *slash;
23771356Seschrock 	zfs_handle_t *zhp;
23782082Seschrock 	char errbuf[1024];
23792082Seschrock 
23808269SMark.Musante@Sun.COM 	(void) snprintf(errbuf, sizeof (errbuf),
23818269SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2382789Sahrens 
2383789Sahrens 	/* get parent, and check to see if this is just a pool */
2384789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
23852082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23862082Seschrock 		    "missing dataset name"));
23872082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2388789Sahrens 	}
2389789Sahrens 
2390789Sahrens 	/* check to see if the pool exists */
2391789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2392789Sahrens 		slash = parent + strlen(parent);
2393789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2394789Sahrens 	zc.zc_name[slash - parent] = '\0';
23952082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2396789Sahrens 	    errno == ENOENT) {
23972082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23982082Seschrock 		    "no such pool '%s'"), zc.zc_name);
23992082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2400789Sahrens 	}
2401789Sahrens 
2402789Sahrens 	/* check to see if the parent dataset exists */
24034490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
24044490Svb160487 		if (errno == ENOENT && accept_ancestor) {
24054490Svb160487 			/*
24064490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
24074490Svb160487 			 */
24084490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
24094490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24104490Svb160487 				    "no such pool '%s'"), zc.zc_name);
24114490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
24124490Svb160487 			}
24134490Svb160487 		} else if (errno == ENOENT) {
24142082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24152082Seschrock 			    "parent does not exist"));
24162082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
24174490Svb160487 		} else
24182082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2419789Sahrens 	}
2420789Sahrens 
24212676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2422789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
24232676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
24242082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
24251356Seschrock 		zfs_close(zhp);
2426789Sahrens 		return (-1);
2427789Sahrens 	}
2428789Sahrens 
2429789Sahrens 	/* make sure parent is a filesystem */
24301356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
24312082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24322082Seschrock 		    "parent is not a filesystem"));
24332082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
24341356Seschrock 		zfs_close(zhp);
2435789Sahrens 		return (-1);
2436789Sahrens 	}
2437789Sahrens 
24381356Seschrock 	zfs_close(zhp);
24394490Svb160487 	if (prefixlen != NULL)
24404490Svb160487 		*prefixlen = strlen(parent);
24414490Svb160487 	return (0);
24424490Svb160487 }
24434490Svb160487 
24444490Svb160487 /*
24454490Svb160487  * Finds whether the dataset of the given type(s) exists.
24464490Svb160487  */
24474490Svb160487 boolean_t
24484490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
24494490Svb160487 {
24504490Svb160487 	zfs_handle_t *zhp;
24514490Svb160487 
24525326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
24534490Svb160487 		return (B_FALSE);
24544490Svb160487 
24554490Svb160487 	/*
24564490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
24574490Svb160487 	 */
24584490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
24594490Svb160487 		int ds_type = zhp->zfs_type;
24604490Svb160487 
24614490Svb160487 		zfs_close(zhp);
24624490Svb160487 		if (types & ds_type)
24634490Svb160487 			return (B_TRUE);
24644490Svb160487 	}
24654490Svb160487 	return (B_FALSE);
24664490Svb160487 }
24674490Svb160487 
24684490Svb160487 /*
24695367Sahrens  * Given a path to 'target', create all the ancestors between
24705367Sahrens  * the prefixlen portion of the path, and the target itself.
24715367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
24725367Sahrens  */
24735367Sahrens int
24745367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
24755367Sahrens {
24765367Sahrens 	zfs_handle_t *h;
24775367Sahrens 	char *cp;
24785367Sahrens 	const char *opname;
24795367Sahrens 
24805367Sahrens 	/* make sure prefix exists */
24815367Sahrens 	cp = target + prefixlen;
24825367Sahrens 	if (*cp != '/') {
24835367Sahrens 		assert(strchr(cp, '/') == NULL);
24845367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24855367Sahrens 	} else {
24865367Sahrens 		*cp = '\0';
24875367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24885367Sahrens 		*cp = '/';
24895367Sahrens 	}
24905367Sahrens 	if (h == NULL)
24915367Sahrens 		return (-1);
24925367Sahrens 	zfs_close(h);
24935367Sahrens 
24945367Sahrens 	/*
24955367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
24965367Sahrens 	 * up to the prefixlen-long one.
24975367Sahrens 	 */
24985367Sahrens 	for (cp = target + prefixlen + 1;
24995367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
25005367Sahrens 		char *logstr;
25015367Sahrens 
25025367Sahrens 		*cp = '\0';
25035367Sahrens 
25045367Sahrens 		h = make_dataset_handle(hdl, target);
25055367Sahrens 		if (h) {
25065367Sahrens 			/* it already exists, nothing to do here */
25075367Sahrens 			zfs_close(h);
25085367Sahrens 			continue;
25095367Sahrens 		}
25105367Sahrens 
25115367Sahrens 		logstr = hdl->libzfs_log_str;
25125367Sahrens 		hdl->libzfs_log_str = NULL;
25135367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
25145367Sahrens 		    NULL) != 0) {
25155367Sahrens 			hdl->libzfs_log_str = logstr;
25165367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
25175367Sahrens 			goto ancestorerr;
25185367Sahrens 		}
25195367Sahrens 
25205367Sahrens 		hdl->libzfs_log_str = logstr;
25215367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
25225367Sahrens 		if (h == NULL) {
25235367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
25245367Sahrens 			goto ancestorerr;
25255367Sahrens 		}
25265367Sahrens 
25275367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
25285367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
25295367Sahrens 			goto ancestorerr;
25305367Sahrens 		}
25315367Sahrens 
25325367Sahrens 		if (zfs_share(h) != 0) {
25335367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
25345367Sahrens 			goto ancestorerr;
25355367Sahrens 		}
25365367Sahrens 
25375367Sahrens 		zfs_close(h);
25385367Sahrens 	}
25395367Sahrens 
25405367Sahrens 	return (0);
25415367Sahrens 
25425367Sahrens ancestorerr:
25435367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25445367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
25455367Sahrens 	return (-1);
25465367Sahrens }
25475367Sahrens 
25485367Sahrens /*
25494490Svb160487  * Creates non-existing ancestors of the given path.
25504490Svb160487  */
25514490Svb160487 int
25524490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
25534490Svb160487 {
25544490Svb160487 	int prefix;
25554490Svb160487 	uint64_t zoned;
25564490Svb160487 	char *path_copy;
25574490Svb160487 	int rc;
25584490Svb160487 
25594490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
25604490Svb160487 		return (-1);
25614490Svb160487 
25624490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
25634490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
25644490Svb160487 		free(path_copy);
25654490Svb160487 	}
25664490Svb160487 	if (path_copy == NULL || rc != 0)
25674490Svb160487 		return (-1);
25684490Svb160487 
2569789Sahrens 	return (0);
2570789Sahrens }
2571789Sahrens 
2572789Sahrens /*
25732676Seschrock  * Create a new filesystem or volume.
2574789Sahrens  */
2575789Sahrens int
25762082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
25772676Seschrock     nvlist_t *props)
2578789Sahrens {
2579789Sahrens 	zfs_cmd_t zc = { 0 };
2580789Sahrens 	int ret;
2581789Sahrens 	uint64_t size = 0;
2582789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
25832082Seschrock 	char errbuf[1024];
25842676Seschrock 	uint64_t zoned;
25852082Seschrock 
25862082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
25872082Seschrock 	    "cannot create '%s'"), path);
2588789Sahrens 
2589789Sahrens 	/* validate the path, taking care to note the extended error message */
25905326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
25912082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2592789Sahrens 
2593789Sahrens 	/* validate parents exist */
25944490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2595789Sahrens 		return (-1);
2596789Sahrens 
2597789Sahrens 	/*
2598789Sahrens 	 * The failure modes when creating a dataset of a different type over
2599789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2600789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2601789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2602789Sahrens 	 * first try to see if the dataset exists.
2603789Sahrens 	 */
2604789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
26055094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
26062082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26072082Seschrock 		    "dataset already exists"));
26082082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2609789Sahrens 	}
2610789Sahrens 
2611789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2612789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2613789Sahrens 	else
2614789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2615789Sahrens 
26167184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
26173912Slling 	    zoned, NULL, errbuf)) == 0)
26182676Seschrock 		return (-1);
26192676Seschrock 
2620789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
26211133Seschrock 		/*
26221133Seschrock 		 * If we are creating a volume, the size and block size must
26231133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
26241133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
26251133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
26261133Seschrock 		 * zero.
26271133Seschrock 		 */
26282676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
26292676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
26302676Seschrock 			nvlist_free(props);
26312082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26322676Seschrock 			    "missing volume size"));
26332676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2634789Sahrens 		}
2635789Sahrens 
26362676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
26372676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
26382676Seschrock 		    &blocksize)) != 0) {
26392676Seschrock 			if (ret == ENOENT) {
26402676Seschrock 				blocksize = zfs_prop_default_numeric(
26412676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
26422676Seschrock 			} else {
26432676Seschrock 				nvlist_free(props);
26442676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26452676Seschrock 				    "missing volume block size"));
26462676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26472676Seschrock 			}
26482676Seschrock 		}
26492676Seschrock 
26502676Seschrock 		if (size == 0) {
26512676Seschrock 			nvlist_free(props);
26522082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26532676Seschrock 			    "volume size cannot be zero"));
26542676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26551133Seschrock 		}
26561133Seschrock 
26571133Seschrock 		if (size % blocksize != 0) {
26582676Seschrock 			nvlist_free(props);
26592082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26602676Seschrock 			    "volume size must be a multiple of volume block "
26612676Seschrock 			    "size"));
26622676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26631133Seschrock 		}
2664789Sahrens 	}
2665789Sahrens 
26665094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
26672676Seschrock 		return (-1);
26682676Seschrock 	nvlist_free(props);
26692676Seschrock 
2670789Sahrens 	/* create the dataset */
26714543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2672789Sahrens 
26733912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
26742082Seschrock 		ret = zvol_create_link(hdl, path);
26753912Slling 		if (ret) {
26763912Slling 			(void) zfs_standard_error(hdl, errno,
26773912Slling 			    dgettext(TEXT_DOMAIN,
26783912Slling 			    "Volume successfully created, but device links "
26793912Slling 			    "were not created"));
26803912Slling 			zcmd_free_nvlists(&zc);
26813912Slling 			return (-1);
26823912Slling 		}
26833912Slling 	}
2684789Sahrens 
26852676Seschrock 	zcmd_free_nvlists(&zc);
26862676Seschrock 
2687789Sahrens 	/* check for failure */
2688789Sahrens 	if (ret != 0) {
2689789Sahrens 		char parent[ZFS_MAXNAMELEN];
2690789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2691789Sahrens 
2692789Sahrens 		switch (errno) {
2693789Sahrens 		case ENOENT:
26942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26952082Seschrock 			    "no such parent '%s'"), parent);
26962082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2697789Sahrens 
2698789Sahrens 		case EINVAL:
26992082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27003413Smmusante 			    "parent '%s' is not a filesystem"), parent);
27012082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2702789Sahrens 
2703789Sahrens 		case EDOM:
27042082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27052676Seschrock 			    "volume block size must be power of 2 from "
27062676Seschrock 			    "%u to %uk"),
2707789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2708789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
27092082Seschrock 
27102676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
27112082Seschrock 
27124603Sahrens 		case ENOTSUP:
27134603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27144603Sahrens 			    "pool must be upgraded to set this "
27154603Sahrens 			    "property or value"));
27164603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2717789Sahrens #ifdef _ILP32
2718789Sahrens 		case EOVERFLOW:
2719789Sahrens 			/*
2720789Sahrens 			 * This platform can't address a volume this big.
2721789Sahrens 			 */
27222082Seschrock 			if (type == ZFS_TYPE_VOLUME)
27232082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
27242082Seschrock 				    errbuf));
2725789Sahrens #endif
27262082Seschrock 			/* FALLTHROUGH */
2727789Sahrens 		default:
27282082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2729789Sahrens 		}
2730789Sahrens 	}
2731789Sahrens 
2732789Sahrens 	return (0);
2733789Sahrens }
2734789Sahrens 
2735789Sahrens /*
2736789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2737789Sahrens  * isn't mounted, and that there are no active dependents.
2738789Sahrens  */
2739789Sahrens int
2740789Sahrens zfs_destroy(zfs_handle_t *zhp)
2741789Sahrens {
2742789Sahrens 	zfs_cmd_t zc = { 0 };
2743789Sahrens 
2744789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2745789Sahrens 
27462676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
27473126Sahl 		/*
27484543Smarks 		 * If user doesn't have permissions to unshare volume, then
27494543Smarks 		 * abort the request.  This would only happen for a
27504543Smarks 		 * non-privileged user.
27513126Sahl 		 */
27524543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
27534543Smarks 			return (-1);
27544543Smarks 		}
27553126Sahl 
27562082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2757789Sahrens 			return (-1);
2758789Sahrens 
2759789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2760789Sahrens 	} else {
2761789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2762789Sahrens 	}
2763789Sahrens 
27644543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
27653237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
27662082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
27672082Seschrock 		    zhp->zfs_name));
27682199Sahrens 	}
2769789Sahrens 
2770789Sahrens 	remove_mountpoint(zhp);
2771789Sahrens 
2772789Sahrens 	return (0);
2773789Sahrens }
2774789Sahrens 
27752199Sahrens struct destroydata {
27762199Sahrens 	char *snapname;
27772199Sahrens 	boolean_t gotone;
27783265Sahrens 	boolean_t closezhp;
27792199Sahrens };
27802199Sahrens 
27812199Sahrens static int
27822199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
27832199Sahrens {
27842199Sahrens 	struct destroydata *dd = arg;
27852199Sahrens 	zfs_handle_t *szhp;
27862199Sahrens 	char name[ZFS_MAXNAMELEN];
27873265Sahrens 	boolean_t closezhp = dd->closezhp;
27883265Sahrens 	int rv;
27892199Sahrens 
27902676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
27912676Seschrock 	(void) strlcat(name, "@", sizeof (name));
27922676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
27932199Sahrens 
27942199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
27952199Sahrens 	if (szhp) {
27962199Sahrens 		dd->gotone = B_TRUE;
27972199Sahrens 		zfs_close(szhp);
27982199Sahrens 	}
27992199Sahrens 
28002199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
28012199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
28022199Sahrens 		/*
28032199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
28042199Sahrens 		 * return an error, because then we wouldn't visit all
28052199Sahrens 		 * the volumes.
28062199Sahrens 		 */
28072199Sahrens 	}
28082199Sahrens 
28093265Sahrens 	dd->closezhp = B_TRUE;
28103265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
28113265Sahrens 	if (closezhp)
28123265Sahrens 		zfs_close(zhp);
28133265Sahrens 	return (rv);
28142199Sahrens }
28152199Sahrens 
28162199Sahrens /*
28172199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
28182199Sahrens  */
28192199Sahrens int
28202199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
28212199Sahrens {
28222199Sahrens 	zfs_cmd_t zc = { 0 };
28232199Sahrens 	int ret;
28242199Sahrens 	struct destroydata dd = { 0 };
28252199Sahrens 
28262199Sahrens 	dd.snapname = snapname;
28272199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
28282199Sahrens 
28292199Sahrens 	if (!dd.gotone) {
28303237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
28312199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
28322199Sahrens 		    zhp->zfs_name, snapname));
28332199Sahrens 	}
28342199Sahrens 
28352199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
28362676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
28372199Sahrens 
28384543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
28392199Sahrens 	if (ret != 0) {
28402199Sahrens 		char errbuf[1024];
28412199Sahrens 
28422199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28432199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
28442199Sahrens 
28452199Sahrens 		switch (errno) {
28462199Sahrens 		case EEXIST:
28472199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28482199Sahrens 			    "snapshot is cloned"));
28492199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
28502199Sahrens 
28512199Sahrens 		default:
28522199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
28532199Sahrens 			    errbuf));
28542199Sahrens 		}
28552199Sahrens 	}
28562199Sahrens 
28572199Sahrens 	return (0);
28582199Sahrens }
28592199Sahrens 
2860789Sahrens /*
2861789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
2862789Sahrens  */
2863789Sahrens int
28642676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2865789Sahrens {
2866789Sahrens 	zfs_cmd_t zc = { 0 };
2867789Sahrens 	char parent[ZFS_MAXNAMELEN];
2868789Sahrens 	int ret;
28692082Seschrock 	char errbuf[1024];
28702082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
28712676Seschrock 	zfs_type_t type;
28722676Seschrock 	uint64_t zoned;
2873789Sahrens 
2874789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2875789Sahrens 
28762082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28772082Seschrock 	    "cannot create '%s'"), target);
28782082Seschrock 
2879789Sahrens 	/* validate the target name */
28805326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
28812082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2882789Sahrens 
2883789Sahrens 	/* validate parents exist */
28844490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2885789Sahrens 		return (-1);
2886789Sahrens 
2887789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
2888789Sahrens 
2889789Sahrens 	/* do the clone */
28902676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
2891789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
28922744Snn35248 		type = ZFS_TYPE_VOLUME;
28932676Seschrock 	} else {
2894789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
28952744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
28962676Seschrock 	}
28972676Seschrock 
28982676Seschrock 	if (props) {
28997184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
29007184Stimh 		    zhp, errbuf)) == NULL)
29012676Seschrock 			return (-1);
29022676Seschrock 
29035094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
29042676Seschrock 			nvlist_free(props);
29052676Seschrock 			return (-1);
29062676Seschrock 		}
29072676Seschrock 
29082676Seschrock 		nvlist_free(props);
29092676Seschrock 	}
2910789Sahrens 
2911789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
29122676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
29134543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2914789Sahrens 
29152676Seschrock 	zcmd_free_nvlists(&zc);
29162676Seschrock 
2917789Sahrens 	if (ret != 0) {
2918789Sahrens 		switch (errno) {
2919789Sahrens 
2920789Sahrens 		case ENOENT:
2921789Sahrens 			/*
2922789Sahrens 			 * The parent doesn't exist.  We should have caught this
2923789Sahrens 			 * above, but there may a race condition that has since
2924789Sahrens 			 * destroyed the parent.
2925789Sahrens 			 *
2926789Sahrens 			 * At this point, we don't know whether it's the source
2927789Sahrens 			 * that doesn't exist anymore, or whether the target
2928789Sahrens 			 * dataset doesn't exist.
2929789Sahrens 			 */
29302082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29312082Seschrock 			    "no such parent '%s'"), parent);
29322082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
29332082Seschrock 
29342082Seschrock 		case EXDEV:
29352082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29362082Seschrock 			    "source and target pools differ"));
29372082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
29382082Seschrock 			    errbuf));
29392082Seschrock 
29402082Seschrock 		default:
29412082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
29422082Seschrock 			    errbuf));
29432082Seschrock 		}
29442676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
29452082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
29462082Seschrock 	}
29472082Seschrock 
29482082Seschrock 	return (ret);
29492082Seschrock }
29502082Seschrock 
29512082Seschrock typedef struct promote_data {
29522082Seschrock 	char cb_mountpoint[MAXPATHLEN];
29532082Seschrock 	const char *cb_target;
29542082Seschrock 	const char *cb_errbuf;
29552082Seschrock 	uint64_t cb_pivot_txg;
29562082Seschrock } promote_data_t;
29572082Seschrock 
29582082Seschrock static int
29592082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
29602082Seschrock {
29612082Seschrock 	promote_data_t *pd = data;
29622082Seschrock 	zfs_handle_t *szhp;
29632082Seschrock 	char snapname[MAXPATHLEN];
29643265Sahrens 	int rv = 0;
29652082Seschrock 
29662082Seschrock 	/* We don't care about snapshots after the pivot point */
29673265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
29683265Sahrens 		zfs_close(zhp);
29692082Seschrock 		return (0);
29703265Sahrens 	}
29712082Seschrock 
29722417Sahrens 	/* Remove the device link if it's a zvol. */
29732676Seschrock 	if (ZFS_IS_VOLUME(zhp))
29742417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
29752082Seschrock 
29762082Seschrock 	/* Check for conflicting names */
29772676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
29782676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
29792082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
29802082Seschrock 	if (szhp != NULL) {
29812082Seschrock 		zfs_close(szhp);
29822082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29832082Seschrock 		    "snapshot name '%s' from origin \n"
29842082Seschrock 		    "conflicts with '%s' from target"),
29852082Seschrock 		    zhp->zfs_name, snapname);
29863265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
29872082Seschrock 	}
29883265Sahrens 	zfs_close(zhp);
29893265Sahrens 	return (rv);
29902082Seschrock }
29912082Seschrock 
29922417Sahrens static int
29932417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
29942417Sahrens {
29952417Sahrens 	promote_data_t *pd = data;
29962417Sahrens 
29972417Sahrens 	/* We don't care about snapshots after the pivot point */
29983265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
29993265Sahrens 		/* Create the device link if it's a zvol. */
30003265Sahrens 		if (ZFS_IS_VOLUME(zhp))
30013265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
30023265Sahrens 	}
30033265Sahrens 
30043265Sahrens 	zfs_close(zhp);
30052417Sahrens 	return (0);
30062417Sahrens }
30072417Sahrens 
30082082Seschrock /*
30092082Seschrock  * Promotes the given clone fs to be the clone parent.
30102082Seschrock  */
30112082Seschrock int
30122082Seschrock zfs_promote(zfs_handle_t *zhp)
30132082Seschrock {
30142082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
30152082Seschrock 	zfs_cmd_t zc = { 0 };
30162082Seschrock 	char parent[MAXPATHLEN];
30172082Seschrock 	char *cp;
30182082Seschrock 	int ret;
30192082Seschrock 	zfs_handle_t *pzhp;
30202082Seschrock 	promote_data_t pd;
30212082Seschrock 	char errbuf[1024];
30222082Seschrock 
30232082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30242082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
30252082Seschrock 
30262082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
30272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30282082Seschrock 		    "snapshots can not be promoted"));
30292082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
30302082Seschrock 	}
30312082Seschrock 
30325367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
30332082Seschrock 	if (parent[0] == '\0') {
30342082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30352082Seschrock 		    "not a cloned filesystem"));
30362082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
30372082Seschrock 	}
30382082Seschrock 	cp = strchr(parent, '@');
30392082Seschrock 	*cp = '\0';
30402082Seschrock 
30412082Seschrock 	/* Walk the snapshots we will be moving */
30425367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
30432082Seschrock 	if (pzhp == NULL)
30442082Seschrock 		return (-1);
30452082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
30462082Seschrock 	zfs_close(pzhp);
30472082Seschrock 	pd.cb_target = zhp->zfs_name;
30482082Seschrock 	pd.cb_errbuf = errbuf;
30495094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
30502082Seschrock 	if (pzhp == NULL)
30512082Seschrock 		return (-1);
30522082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
30532082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
30542082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
30552417Sahrens 	if (ret != 0) {
30562417Sahrens 		zfs_close(pzhp);
30572082Seschrock 		return (-1);
30582417Sahrens 	}
30592082Seschrock 
30602082Seschrock 	/* issue the ioctl */
30615367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
30622676Seschrock 	    sizeof (zc.zc_value));
30632082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
30644543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
30652082Seschrock 
30662082Seschrock 	if (ret != 0) {
30672417Sahrens 		int save_errno = errno;
30682417Sahrens 
30692417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
30702417Sahrens 		zfs_close(pzhp);
30712417Sahrens 
30722417Sahrens 		switch (save_errno) {
3073789Sahrens 		case EEXIST:
3074789Sahrens 			/*
30752082Seschrock 			 * There is a conflicting snapshot name.  We
30762082Seschrock 			 * should have caught this above, but they could
30772082Seschrock 			 * have renamed something in the mean time.
3078789Sahrens 			 */
30792082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30802082Seschrock 			    "conflicting snapshot name from parent '%s'"),
30812082Seschrock 			    parent);
30822082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3083789Sahrens 
3084789Sahrens 		default:
30852417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3086789Sahrens 		}
30872417Sahrens 	} else {
30882417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3089789Sahrens 	}
3090789Sahrens 
30912417Sahrens 	zfs_close(pzhp);
3092789Sahrens 	return (ret);
3093789Sahrens }
3094789Sahrens 
30954007Smmusante struct createdata {
30964007Smmusante 	const char *cd_snapname;
30974007Smmusante 	int cd_ifexists;
30984007Smmusante };
30994007Smmusante 
31002199Sahrens static int
31012199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
31022199Sahrens {
31034007Smmusante 	struct createdata *cd = arg;
31042676Seschrock 	int ret;
31052199Sahrens 
31062199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31072199Sahrens 		char name[MAXPATHLEN];
31082199Sahrens 
31092676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31102676Seschrock 		(void) strlcat(name, "@", sizeof (name));
31114007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
31124007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
31134007Smmusante 		    cd->cd_ifexists);
31142199Sahrens 		/*
31152199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31162199Sahrens 		 * return an error, because then we wouldn't visit all
31172199Sahrens 		 * the volumes.
31182199Sahrens 		 */
31192199Sahrens 	}
31202676Seschrock 
31214007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
31222676Seschrock 
31232676Seschrock 	zfs_close(zhp);
31242676Seschrock 
31252676Seschrock 	return (ret);
31262199Sahrens }
31272199Sahrens 
3128789Sahrens /*
31293504Sahl  * Takes a snapshot of the given dataset.
3130789Sahrens  */
3131789Sahrens int
31327265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
31337265Sahrens     nvlist_t *props)
3134789Sahrens {
3135789Sahrens 	const char *delim;
31367265Sahrens 	char parent[ZFS_MAXNAMELEN];
3137789Sahrens 	zfs_handle_t *zhp;
3138789Sahrens 	zfs_cmd_t zc = { 0 };
3139789Sahrens 	int ret;
31402082Seschrock 	char errbuf[1024];
31412082Seschrock 
31422082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31432082Seschrock 	    "cannot snapshot '%s'"), path);
31442082Seschrock 
31452082Seschrock 	/* validate the target name */
31465326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
31472082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3148789Sahrens 
31497265Sahrens 	if (props) {
31507265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
31517265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
31527265Sahrens 			return (-1);
31537265Sahrens 
31547265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
31557265Sahrens 			nvlist_free(props);
31567265Sahrens 			return (-1);
31577265Sahrens 		}
31587265Sahrens 
31597265Sahrens 		nvlist_free(props);
31607265Sahrens 	}
31617265Sahrens 
3162789Sahrens 	/* make sure the parent exists and is of the appropriate type */
31632199Sahrens 	delim = strchr(path, '@');
3164789Sahrens 	(void) strncpy(parent, path, delim - path);
3165789Sahrens 	parent[delim - path] = '\0';
3166789Sahrens 
31672082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3168789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
31697265Sahrens 		zcmd_free_nvlists(&zc);
3170789Sahrens 		return (-1);
3171789Sahrens 	}
3172789Sahrens 
31732199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31742676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
31754543Smarks 	if (ZFS_IS_VOLUME(zhp))
31764543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
31774543Smarks 	else
31784543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
31792199Sahrens 	zc.zc_cookie = recursive;
31804543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
31812199Sahrens 
31827265Sahrens 	zcmd_free_nvlists(&zc);
31837265Sahrens 
31842199Sahrens 	/*
31852199Sahrens 	 * if it was recursive, the one that actually failed will be in
31862199Sahrens 	 * zc.zc_name.
31872199Sahrens 	 */
31884543Smarks 	if (ret != 0)
31894543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31904543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
31914543Smarks 
31922199Sahrens 	if (ret == 0 && recursive) {
31934007Smmusante 		struct createdata cd;
31944007Smmusante 
31954007Smmusante 		cd.cd_snapname = delim + 1;
31964007Smmusante 		cd.cd_ifexists = B_FALSE;
31974007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
31982199Sahrens 	}
3199789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
32002082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
32012199Sahrens 		if (ret != 0) {
32024543Smarks 			(void) zfs_standard_error(hdl, errno,
32034543Smarks 			    dgettext(TEXT_DOMAIN,
32044543Smarks 			    "Volume successfully snapshotted, but device links "
32054543Smarks 			    "were not created"));
32064543Smarks 			zfs_close(zhp);
32074543Smarks 			return (-1);
32082199Sahrens 		}
3209789Sahrens 	}
3210789Sahrens 
32112082Seschrock 	if (ret != 0)
32122082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3213789Sahrens 
3214789Sahrens 	zfs_close(zhp);
3215789Sahrens 
3216789Sahrens 	return (ret);
3217789Sahrens }
3218789Sahrens 
3219789Sahrens /*
32201294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
32211294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
32221294Slling  * is a dependent and we should just destroy it without checking the transaction
32231294Slling  * group.
3224789Sahrens  */
32251294Slling typedef struct rollback_data {
32261294Slling 	const char	*cb_target;		/* the snapshot */
32271294Slling 	uint64_t	cb_create;		/* creation time reference */
32285749Sahrens 	boolean_t	cb_error;
32292082Seschrock 	boolean_t	cb_dependent;
32305749Sahrens 	boolean_t	cb_force;
32311294Slling } rollback_data_t;
32321294Slling 
32331294Slling static int
32341294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
32351294Slling {
32361294Slling 	rollback_data_t *cbp = data;
32371294Slling 
32381294Slling 	if (!cbp->cb_dependent) {
32391294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
32401294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
32411294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
32421294Slling 		    cbp->cb_create) {
32434543Smarks 			char *logstr;
32441294Slling 
32452082Seschrock 			cbp->cb_dependent = B_TRUE;
32465446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
32475446Sahrens 			    rollback_destroy, cbp);
32482082Seschrock 			cbp->cb_dependent = B_FALSE;
32491294Slling 
32504543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
32514543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
32525446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
32534543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
32541294Slling 		}
32551294Slling 	} else {
32565749Sahrens 		/* We must destroy this clone; first unmount it */
32575749Sahrens 		prop_changelist_t *clp;
32585749Sahrens 
32597366STim.Haley@Sun.COM 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
32605749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
32615749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
32625749Sahrens 			cbp->cb_error = B_TRUE;
32635749Sahrens 			zfs_close(zhp);
32645749Sahrens 			return (0);
32655749Sahrens 		}
32665749Sahrens 		if (zfs_destroy(zhp) != 0)
32675749Sahrens 			cbp->cb_error = B_TRUE;
32685749Sahrens 		else
32695749Sahrens 			changelist_remove(clp, zhp->zfs_name);
32705751Sahrens 		(void) changelist_postfix(clp);
32715749Sahrens 		changelist_free(clp);
32721294Slling 	}
32731294Slling 
32741294Slling 	zfs_close(zhp);
32751294Slling 	return (0);
32761294Slling }
32771294Slling 
32781294Slling /*
32795446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
32805446Sahrens  * data changes since then and making it the active dataset.
32815446Sahrens  *
32825446Sahrens  * Any snapshots more recent than the target are destroyed, along with
32835446Sahrens  * their dependents.
32841294Slling  */
32855446Sahrens int
32865749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3287789Sahrens {
32885446Sahrens 	rollback_data_t cb = { 0 };
32895446Sahrens 	int err;
3290789Sahrens 	zfs_cmd_t zc = { 0 };
32915713Srm160521 	boolean_t restore_resv = 0;
32925713Srm160521 	uint64_t old_volsize, new_volsize;
32935713Srm160521 	zfs_prop_t resv_prop;
3294789Sahrens 
3295789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3296789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3297789Sahrens 
32985446Sahrens 	/*
32995446Sahrens 	 * Destroy all recent snapshots and its dependends.
33005446Sahrens 	 */
33015749Sahrens 	cb.cb_force = force;
33025446Sahrens 	cb.cb_target = snap->zfs_name;
33035446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
33045446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
33055446Sahrens 
33065749Sahrens 	if (cb.cb_error)
33075749Sahrens 		return (-1);
33085446Sahrens 
33095446Sahrens 	/*
33105446Sahrens 	 * Now that we have verified that the snapshot is the latest,
33115446Sahrens 	 * rollback to the given snapshot.
33125446Sahrens 	 */
33135446Sahrens 
33145713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33155713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
33165713Srm160521 			return (-1);
33175713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
33185713Srm160521 			return (-1);
33195713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
33205713Srm160521 		restore_resv =
33215713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
33225713Srm160521 	}
3323789Sahrens 
3324789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3325789Sahrens 
33262676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3327789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3328789Sahrens 	else
3329789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3330789Sahrens 
3331789Sahrens 	/*
33325446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
33335446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
33345446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
33355446Sahrens 	 * an unlikely race condition where the user has taken a
33365446Sahrens 	 * snapshot since we verified that this was the most recent.
33375713Srm160521 	 *
3338789Sahrens 	 */
33395446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
33403237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
33412082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
33422082Seschrock 		    zhp->zfs_name);
33435717Srm160521 		return (err);
33445717Srm160521 	}
33455713Srm160521 
33465713Srm160521 	/*
33475713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
33485713Srm160521 	 * rollback reservation and the volsize has changed then set
33495713Srm160521 	 * the reservation property to the post-rollback volsize.
33505713Srm160521 	 * Make a new handle since the rollback closed the dataset.
33515713Srm160521 	 */
33525717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
33535717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
33545717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
33555717Srm160521 			zfs_close(zhp);
33565713Srm160521 			return (err);
33575717Srm160521 		}
33585713Srm160521 		if (restore_resv) {
33595713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
33605713Srm160521 			if (old_volsize != new_volsize)
33615717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
33625717Srm160521 				    new_volsize);
33635713Srm160521 		}
33645713Srm160521 		zfs_close(zhp);
3365789Sahrens 	}
33665446Sahrens 	return (err);
33671294Slling }
33681294Slling 
33691294Slling /*
3370789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3371789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3372789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3373789Sahrens  * libzfs_graph.c.
3374789Sahrens  */
3375789Sahrens int
33762474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
33772474Seschrock     zfs_iter_f func, void *data)
3378789Sahrens {
3379789Sahrens 	char **dependents;
3380789Sahrens 	size_t count;
3381789Sahrens 	int i;
3382789Sahrens 	zfs_handle_t *child;
3383789Sahrens 	int ret = 0;
3384789Sahrens 
33852474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
33862474Seschrock 	    &dependents, &count) != 0)
33872474Seschrock 		return (-1);
33882474Seschrock 
3389789Sahrens 	for (i = 0; i < count; i++) {
33902082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
33912082Seschrock 		    dependents[i])) == NULL)
3392789Sahrens 			continue;
3393789Sahrens 
3394789Sahrens 		if ((ret = func(child, data)) != 0)
3395789Sahrens 			break;
3396789Sahrens 	}
3397789Sahrens 
3398789Sahrens 	for (i = 0; i < count; i++)
3399789Sahrens 		free(dependents[i]);
3400789Sahrens 	free(dependents);
3401789Sahrens 
3402789Sahrens 	return (ret);
3403789Sahrens }
3404789Sahrens 
3405789Sahrens /*
3406789Sahrens  * Renames the given dataset.
3407789Sahrens  */
3408789Sahrens int
34094490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3410789Sahrens {
3411789Sahrens 	int ret;
3412789Sahrens 	zfs_cmd_t zc = { 0 };
3413789Sahrens 	char *delim;
34144007Smmusante 	prop_changelist_t *cl = NULL;
34154007Smmusante 	zfs_handle_t *zhrp = NULL;
34164007Smmusante 	char *parentname = NULL;
3417789Sahrens 	char parent[ZFS_MAXNAMELEN];
34182082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34192082Seschrock 	char errbuf[1024];
3420789Sahrens 
3421789Sahrens 	/* if we have the same exact name, just return success */
3422789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3423789Sahrens 		return (0);
3424789Sahrens 
34252082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34262082Seschrock 	    "cannot rename to '%s'"), target);
34272082Seschrock 
3428789Sahrens 	/*
3429789Sahrens 	 * Make sure the target name is valid
3430789Sahrens 	 */
3431789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
34322665Snd150628 		if ((strchr(target, '@') == NULL) ||
34332665Snd150628 		    *target == '@') {
34342665Snd150628 			/*
34352665Snd150628 			 * Snapshot target name is abbreviated,
34362665Snd150628 			 * reconstruct full dataset name
34372665Snd150628 			 */
34382665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
34392665Snd150628 			    sizeof (parent));
34402665Snd150628 			delim = strchr(parent, '@');
34412665Snd150628 			if (strchr(target, '@') == NULL)
34422665Snd150628 				*(++delim) = '\0';
34432665Snd150628 			else
34442665Snd150628 				*delim = '\0';
34452665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
34462665Snd150628 			target = parent;
34472665Snd150628 		} else {
34482665Snd150628 			/*
34492665Snd150628 			 * Make sure we're renaming within the same dataset.
34502665Snd150628 			 */
34512665Snd150628 			delim = strchr(target, '@');
34522665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
34532665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
34542665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34552665Snd150628 				    "snapshots must be part of same "
34562665Snd150628 				    "dataset"));
34572665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
34583912Slling 				    errbuf));
34592665Snd150628 			}
3460789Sahrens 		}
34615326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
34622665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3463789Sahrens 	} else {
34644007Smmusante 		if (recursive) {
34654007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34664007Smmusante 			    "recursive rename must be a snapshot"));
34674007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
34684007Smmusante 		}
34694007Smmusante 
34705326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
34712665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
34722676Seschrock 		uint64_t unused;
34732676Seschrock 
3474789Sahrens 		/* validate parents */
34754490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3476789Sahrens 			return (-1);
3477789Sahrens 
3478789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3479789Sahrens 
3480789Sahrens 		/* make sure we're in the same pool */
3481789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3482789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3483789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
34842082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34852082Seschrock 			    "datasets must be within same pool"));
34862082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3487789Sahrens 		}
34882440Snd150628 
34892440Snd150628 		/* new name cannot be a child of the current dataset name */
34902440Snd150628 		if (strncmp(parent, zhp->zfs_name,
34913912Slling 		    strlen(zhp->zfs_name)) == 0) {
34922440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34932440Snd150628 			    "New dataset name cannot be a descendent of "
34942440Snd150628 			    "current dataset name"));
34952440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
34962440Snd150628 		}
3497789Sahrens 	}
3498789Sahrens 
34992082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
35002082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
35012082Seschrock 
3502789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3503789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
35042082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35052082Seschrock 		    "dataset is used in a non-global zone"));
35062082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3507789Sahrens 	}
3508789Sahrens 
35094007Smmusante 	if (recursive) {
35104007Smmusante 		struct destroydata dd;
35114007Smmusante 
35124183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
35134183Smmusante 		if (parentname == NULL) {
35144183Smmusante 			ret = -1;
35154183Smmusante 			goto error;
35164183Smmusante 		}
35174007Smmusante 		delim = strchr(parentname, '@');
35184007Smmusante 		*delim = '\0';
35195094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
35204007Smmusante 		if (zhrp == NULL) {
35214183Smmusante 			ret = -1;
35224183Smmusante 			goto error;
35234007Smmusante 		}
35244007Smmusante 
35254007Smmusante 		dd.snapname = delim + 1;
35264007Smmusante 		dd.gotone = B_FALSE;
35274183Smmusante 		dd.closezhp = B_TRUE;
35284007Smmusante 
35294007Smmusante 		/* We remove any zvol links prior to renaming them */
35304007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
35314007Smmusante 		if (ret) {
35324007Smmusante 			goto error;
35334007Smmusante 		}
35344007Smmusante 	} else {
35357366STim.Haley@Sun.COM 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
35364007Smmusante 			return (-1);
35374007Smmusante 
35384007Smmusante 		if (changelist_haszonedchild(cl)) {
35394007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35404007Smmusante 			    "child dataset with inherited mountpoint is used "
35414007Smmusante 			    "in a non-global zone"));
35424007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
35434007Smmusante 			goto error;
35444007Smmusante 		}
35454007Smmusante 
35464007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
35474007Smmusante 			goto error;
3548789Sahrens 	}
3549789Sahrens 
35502676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3551789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3552789Sahrens 	else
3553789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3554789Sahrens 
35552665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35562676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
35572665Snd150628 
35584007Smmusante 	zc.zc_cookie = recursive;
35594007Smmusante 
35604543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
35614007Smmusante 		/*
35624007Smmusante 		 * if it was recursive, the one that actually failed will
35634007Smmusante 		 * be in zc.zc_name
35644007Smmusante 		 */
35654007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35665367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
35674007Smmusante 
35684007Smmusante 		if (recursive && errno == EEXIST) {
35694007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35704007Smmusante 			    "a child dataset already has a snapshot "
35714007Smmusante 			    "with the new name"));
35724801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
35734007Smmusante 		} else {
35744007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
35754007Smmusante 		}
3576789Sahrens 
3577789Sahrens 		/*
3578789Sahrens 		 * On failure, we still want to remount any filesystems that
3579789Sahrens 		 * were previously mounted, so we don't alter the system state.
3580789Sahrens 		 */
35814007Smmusante 		if (recursive) {
35824007Smmusante 			struct createdata cd;
35834007Smmusante 
35844007Smmusante 			/* only create links for datasets that had existed */
35854007Smmusante 			cd.cd_snapname = delim + 1;
35864007Smmusante 			cd.cd_ifexists = B_TRUE;
35874007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
35884007Smmusante 			    &cd);
35894007Smmusante 		} else {
35904007Smmusante 			(void) changelist_postfix(cl);
35914007Smmusante 		}
3592789Sahrens 	} else {
35934007Smmusante 		if (recursive) {
35944007Smmusante 			struct createdata cd;
35954007Smmusante 
35964007Smmusante 			/* only create links for datasets that had existed */
35974007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
35984007Smmusante 			cd.cd_ifexists = B_TRUE;
35994007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
36004007Smmusante 			    &cd);
36014007Smmusante 		} else {
36024007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
36034007Smmusante 			ret = changelist_postfix(cl);
36044007Smmusante 		}
3605789Sahrens 	}
3606789Sahrens 
3607789Sahrens error:
36084007Smmusante 	if (parentname) {
36094007Smmusante 		free(parentname);
36104007Smmusante 	}
36114007Smmusante 	if (zhrp) {
36124007Smmusante 		zfs_close(zhrp);
36134007Smmusante 	}
36144007Smmusante 	if (cl) {
36154007Smmusante 		changelist_free(cl);
36164007Smmusante 	}
3617789Sahrens 	return (ret);
3618789Sahrens }
3619789Sahrens 
3620789Sahrens /*
3621789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3622789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3623789Sahrens  */
3624789Sahrens int
36252082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3626789Sahrens {
36274007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
36284007Smmusante }
36294007Smmusante 
36304007Smmusante static int
36314007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
36324007Smmusante {
3633789Sahrens 	zfs_cmd_t zc = { 0 };
36342082Seschrock 	di_devlink_handle_t dhdl;
36354543Smarks 	priv_set_t *priv_effective;
36364543Smarks 	int privileged;
3637789Sahrens 
3638789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3639789Sahrens 
3640789Sahrens 	/*
3641789Sahrens 	 * Issue the appropriate ioctl.
3642789Sahrens 	 */
36432082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3644789Sahrens 		switch (errno) {
3645789Sahrens 		case EEXIST:
3646789Sahrens 			/*
3647789Sahrens 			 * Silently ignore the case where the link already
3648789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3649789Sahrens 			 * times without errors.
3650789Sahrens 			 */
3651789Sahrens 			return (0);
3652789Sahrens 
36534007Smmusante 		case ENOENT:
36544007Smmusante 			/*
36554007Smmusante 			 * Dataset does not exist in the kernel.  If we
36564007Smmusante 			 * don't care (see zfs_rename), then ignore the
36574007Smmusante 			 * error quietly.
36584007Smmusante 			 */
36594007Smmusante 			if (ifexists) {
36604007Smmusante 				return (0);
36614007Smmusante 			}
36624007Smmusante 
36634007Smmusante 			/* FALLTHROUGH */
36644007Smmusante 
3665789Sahrens 		default:
36663237Slling 			return (zfs_standard_error_fmt(hdl, errno,
36672082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
36682082Seschrock 			    "for '%s'"), dataset));
3669789Sahrens 		}
3670789Sahrens 	}
3671789Sahrens 
3672789Sahrens 	/*
36734543Smarks 	 * If privileged call devfsadm and wait for the links to
36744543Smarks 	 * magically appear.
36754543Smarks 	 * Otherwise, print out an informational message.
3676789Sahrens 	 */
36774543Smarks 
36784543Smarks 	priv_effective = priv_allocset();
36794543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
36804543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
36814543Smarks 	priv_freeset(priv_effective);
36824543Smarks 
36834543Smarks 	if (privileged) {
36844543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
36854543Smarks 		    DI_MAKE_LINK)) == NULL) {
36864543Smarks 			zfs_error_aux(hdl, strerror(errno));
36877301SEric.Taylor@Sun.COM 			(void) zfs_error_fmt(hdl, errno,
36884543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
36894543Smarks 			    "for '%s'"), dataset);
36904543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
36914543Smarks 			return (-1);
36924543Smarks 		} else {
36934543Smarks 			(void) di_devlink_fini(&dhdl);
36944543Smarks 		}
3695789Sahrens 	} else {
36964543Smarks 		char pathname[MAXPATHLEN];
36974543Smarks 		struct stat64 statbuf;
36984543Smarks 		int i;
36994543Smarks 
37004543Smarks #define	MAX_WAIT	10
37014543Smarks 
37024543Smarks 		/*
37034543Smarks 		 * This is the poor mans way of waiting for the link
37044543Smarks 		 * to show up.  If after 10 seconds we still don't
37054543Smarks 		 * have it, then print out a message.
37064543Smarks 		 */
37074543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
37084543Smarks 		    dataset);
37094543Smarks 
37104543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
37114543Smarks 			if (stat64(pathname, &statbuf) == 0)
37124543Smarks 				break;
37134543Smarks 			(void) sleep(1);
37144543Smarks 		}
37154543Smarks 		if (i == MAX_WAIT)
37164543Smarks 			(void) printf(gettext("%s may not be immediately "
37174543Smarks 			    "available\n"), pathname);
3718789Sahrens 	}
3719789Sahrens 
3720789Sahrens 	return (0);
3721789Sahrens }
3722789Sahrens 
3723789Sahrens /*
3724789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3725789Sahrens  */
3726789Sahrens int
37272082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3728789Sahrens {
3729789Sahrens 	zfs_cmd_t zc = { 0 };
3730789Sahrens 
3731789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3732789Sahrens 
37332082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3734789Sahrens 		switch (errno) {
3735789Sahrens 		case ENXIO:
3736789Sahrens 			/*
3737789Sahrens 			 * Silently ignore the case where the link no longer
3738789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3739789Sahrens 			 * times without errors.
3740789Sahrens 			 */
3741789Sahrens 			return (0);
3742789Sahrens 
3743789Sahrens 		default:
37443237Slling 			return (zfs_standard_error_fmt(hdl, errno,
37452082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
37462082Seschrock 			    "links for '%s'"), dataset));
3747789Sahrens 		}
3748789Sahrens 	}
3749789Sahrens 
3750789Sahrens 	return (0);
3751789Sahrens }
37522676Seschrock 
37532676Seschrock nvlist_t *
37542676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
37552676Seschrock {
37562676Seschrock 	return (zhp->zfs_user_props);
37572676Seschrock }
37582676Seschrock 
37592676Seschrock /*
37603912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
37613912Slling  * display, and their maximum widths.  This does two main things:
37623912Slling  *
37633912Slling  *      - If this is a list of all properties, then expand the list to include
37643912Slling  *        all native properties, and set a flag so that for each dataset we look
37653912Slling  *        for new unique user properties and add them to the list.
37663912Slling  *
37673912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
37683912Slling  *        so that we can size the column appropriately.
37693912Slling  */
37703912Slling int
37715094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
37723912Slling {
37733912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37745094Slling 	zprop_list_t *entry;
37755094Slling 	zprop_list_t **last, **start;
37763912Slling 	nvlist_t *userprops, *propval;
37773912Slling 	nvpair_t *elem;
37783912Slling 	char *strval;
37793912Slling 	char buf[ZFS_MAXPROPLEN];
37803912Slling 
37815094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
37823912Slling 		return (-1);
37832676Seschrock 
37842676Seschrock 	userprops = zfs_get_user_props(zhp);
37852676Seschrock 
37862676Seschrock 	entry = *plp;
37872676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
37882676Seschrock 		/*
37892676Seschrock 		 * Go through and add any user properties as necessary.  We
37902676Seschrock 		 * start by incrementing our list pointer to the first
37912676Seschrock 		 * non-native property.
37922676Seschrock 		 */
37932676Seschrock 		start = plp;
37942676Seschrock 		while (*start != NULL) {
37955094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
37962676Seschrock 				break;
37972676Seschrock 			start = &(*start)->pl_next;
37982676Seschrock 		}
37992676Seschrock 
38002676Seschrock 		elem = NULL;
38012676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
38022676Seschrock 			/*
38032676Seschrock 			 * See if we've already found this property in our list.
38042676Seschrock 			 */
38052676Seschrock 			for (last = start; *last != NULL;
38062676Seschrock 			    last = &(*last)->pl_next) {
38072676Seschrock 				if (strcmp((*last)->pl_user_prop,
38082676Seschrock 				    nvpair_name(elem)) == 0)
38092676Seschrock 					break;
38102676Seschrock 			}
38112676Seschrock 
38122676Seschrock 			if (*last == NULL) {
38132676Seschrock 				if ((entry = zfs_alloc(hdl,
38145094Slling 				    sizeof (zprop_list_t))) == NULL ||
38152676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
38162676Seschrock 				    nvpair_name(elem)))) == NULL) {
38172676Seschrock 					free(entry);
38182676Seschrock 					return (-1);
38192676Seschrock 				}
38202676Seschrock 
38215094Slling 				entry->pl_prop = ZPROP_INVAL;
38222676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
38232676Seschrock 				entry->pl_all = B_TRUE;
38242676Seschrock 				*last = entry;
38252676Seschrock 			}
38262676Seschrock 		}
38272676Seschrock 	}
38282676Seschrock 
38292676Seschrock 	/*
38302676Seschrock 	 * Now go through and check the width of any non-fixed columns
38312676Seschrock 	 */
38322676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
38332676Seschrock 		if (entry->pl_fixed)
38342676Seschrock 			continue;
38352676Seschrock 
38365094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
38372676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
38382676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
38392676Seschrock 				if (strlen(buf) > entry->pl_width)
38402676Seschrock 					entry->pl_width = strlen(buf);
38412676Seschrock 			}
38422676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
38432676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
38442676Seschrock 			verify(nvlist_lookup_string(propval,
38455094Slling 			    ZPROP_VALUE, &strval) == 0);
38462676Seschrock 			if (strlen(strval) > entry->pl_width)
38472676Seschrock 				entry->pl_width = strlen(strval);
38482676Seschrock 		}
38492676Seschrock 	}
38502676Seschrock 
38512676Seschrock 	return (0);
38522676Seschrock }
38534543Smarks 
38544543Smarks int
38554543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
38564543Smarks {
38574543Smarks 	zfs_cmd_t zc = { 0 };
38584543Smarks 	nvlist_t *nvp;
38594543Smarks 	gid_t gid;
38604543Smarks 	uid_t uid;
38614543Smarks 	const gid_t *groups;
38624543Smarks 	int group_cnt;
38634543Smarks 	int error;
38644543Smarks 
38654543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
38664543Smarks 		return (no_memory(hdl));
38674543Smarks 
38684543Smarks 	uid = ucred_geteuid(cred);
38694543Smarks 	gid = ucred_getegid(cred);
38704543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
38714543Smarks 
38724543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
38734543Smarks 		return (1);
38744543Smarks 
38754543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
38764543Smarks 		nvlist_free(nvp);
38774543Smarks 		return (1);
38784543Smarks 	}
38794543Smarks 
38804543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
38814543Smarks 		nvlist_free(nvp);
38824543Smarks 		return (1);
38834543Smarks 	}
38844543Smarks 
38854543Smarks 	if (nvlist_add_uint32_array(nvp,
38864543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
38874543Smarks 		nvlist_free(nvp);
38884543Smarks 		return (1);
38894543Smarks 	}
38904543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
38914543Smarks 
38925094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
38934543Smarks 		return (-1);
38944543Smarks 
38954543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
38964543Smarks 	nvlist_free(nvp);
38974543Smarks 	return (error);
38984543Smarks }
38994543Smarks 
39004543Smarks int
39014543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
39028845Samw@Sun.COM     char *resource, void *export, void *sharetab,
39038845Samw@Sun.COM     int sharemax, zfs_share_op_t operation)
39044543Smarks {
39054543Smarks 	zfs_cmd_t zc = { 0 };
39064543Smarks 	int error;
39074543Smarks 
39084543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39094543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
39108845Samw@Sun.COM 	if (resource)
39118845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
39124543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
39134543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
39145331Samw 	zc.zc_share.z_sharetype = operation;
39154543Smarks 	zc.zc_share.z_sharemax = sharemax;
39164543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
39174543Smarks 	return (error);
39184543Smarks }
39198802SSanjeev.Bagewadi@Sun.COM 
39208802SSanjeev.Bagewadi@Sun.COM void
39218802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
39228802SSanjeev.Bagewadi@Sun.COM {
39238802SSanjeev.Bagewadi@Sun.COM 	nvpair_t *curr;
39248802SSanjeev.Bagewadi@Sun.COM 
39258802SSanjeev.Bagewadi@Sun.COM 	/*
39268802SSanjeev.Bagewadi@Sun.COM 	 * Keep a reference to the props-table against which we prune the
39278802SSanjeev.Bagewadi@Sun.COM 	 * properties.
39288802SSanjeev.Bagewadi@Sun.COM 	 */
39298802SSanjeev.Bagewadi@Sun.COM 	zhp->zfs_props_table = props;
39308802SSanjeev.Bagewadi@Sun.COM 
39318802SSanjeev.Bagewadi@Sun.COM 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
39328802SSanjeev.Bagewadi@Sun.COM 
39338802SSanjeev.Bagewadi@Sun.COM 	while (curr) {
39348802SSanjeev.Bagewadi@Sun.COM 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
39358802SSanjeev.Bagewadi@Sun.COM 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
39368802SSanjeev.Bagewadi@Sun.COM 
3937*9396SMatthew.Ahrens@Sun.COM 		/*
3938*9396SMatthew.Ahrens@Sun.COM 		 * We leave user:props in the nvlist, so there will be
3939*9396SMatthew.Ahrens@Sun.COM 		 * some ZPROP_INVAL.  To be extra safe, don't prune
3940*9396SMatthew.Ahrens@Sun.COM 		 * those.
3941*9396SMatthew.Ahrens@Sun.COM 		 */
3942*9396SMatthew.Ahrens@Sun.COM 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
39438802SSanjeev.Bagewadi@Sun.COM 			(void) nvlist_remove(zhp->zfs_props,
39448802SSanjeev.Bagewadi@Sun.COM 			    nvpair_name(curr), nvpair_type(curr));
39458802SSanjeev.Bagewadi@Sun.COM 		curr = next;
39468802SSanjeev.Bagewadi@Sun.COM 	}
39478802SSanjeev.Bagewadi@Sun.COM }
39488845Samw@Sun.COM 
39498845Samw@Sun.COM static int
39508845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
39518845Samw@Sun.COM     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
39528845Samw@Sun.COM {
39538845Samw@Sun.COM 	zfs_cmd_t zc = { 0 };
39548845Samw@Sun.COM 	nvlist_t *nvlist = NULL;
39558845Samw@Sun.COM 	int error;
39568845Samw@Sun.COM 
39578845Samw@Sun.COM 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39588845Samw@Sun.COM 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
39598845Samw@Sun.COM 	zc.zc_cookie = (uint64_t)cmd;
39608845Samw@Sun.COM 
39618845Samw@Sun.COM 	if (cmd == ZFS_SMB_ACL_RENAME) {
39628845Samw@Sun.COM 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
39638845Samw@Sun.COM 			(void) no_memory(hdl);
39648845Samw@Sun.COM 			return (NULL);
39658845Samw@Sun.COM 		}
39668845Samw@Sun.COM 	}
39678845Samw@Sun.COM 
39688845Samw@Sun.COM 	switch (cmd) {
39698845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
39708845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
39718845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
39728845Samw@Sun.COM 		break;
39738845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
39748845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
39758845Samw@Sun.COM 		    resource1) != 0) {
39768845Samw@Sun.COM 				(void) no_memory(hdl);
39778845Samw@Sun.COM 				return (-1);
39788845Samw@Sun.COM 		}
39798845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
39808845Samw@Sun.COM 		    resource2) != 0) {
39818845Samw@Sun.COM 				(void) no_memory(hdl);
39828845Samw@Sun.COM 				return (-1);
39838845Samw@Sun.COM 		}
39848845Samw@Sun.COM 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
39858845Samw@Sun.COM 			nvlist_free(nvlist);
39868845Samw@Sun.COM 			return (-1);
39878845Samw@Sun.COM 		}
39888845Samw@Sun.COM 		break;
39898845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
39908845Samw@Sun.COM 		break;
39918845Samw@Sun.COM 	default:
39928845Samw@Sun.COM 		return (-1);
39938845Samw@Sun.COM 	}
39948845Samw@Sun.COM 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
39958845Samw@Sun.COM 	if (nvlist)
39968845Samw@Sun.COM 		nvlist_free(nvlist);
39978845Samw@Sun.COM 	return (error);
39988845Samw@Sun.COM }
39998845Samw@Sun.COM 
40008845Samw@Sun.COM int
40018845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
40028845Samw@Sun.COM     char *path, char *resource)
40038845Samw@Sun.COM {
40048845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
40058845Samw@Sun.COM 	    resource, NULL));
40068845Samw@Sun.COM }
40078845Samw@Sun.COM 
40088845Samw@Sun.COM int
40098845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
40108845Samw@Sun.COM     char *path, char *resource)
40118845Samw@Sun.COM {
40128845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
40138845Samw@Sun.COM 	    resource, NULL));
40148845Samw@Sun.COM }
40158845Samw@Sun.COM 
40168845Samw@Sun.COM int
40178845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
40188845Samw@Sun.COM {
40198845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
40208845Samw@Sun.COM 	    NULL, NULL));
40218845Samw@Sun.COM }
40228845Samw@Sun.COM 
40238845Samw@Sun.COM int
40248845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
40258845Samw@Sun.COM     char *oldname, char *newname)
40268845Samw@Sun.COM {
40278845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
40288845Samw@Sun.COM 	    oldname, newname));
40298845Samw@Sun.COM }
4030*9396SMatthew.Ahrens@Sun.COM 
4031*9396SMatthew.Ahrens@Sun.COM int
4032*9396SMatthew.Ahrens@Sun.COM zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4033*9396SMatthew.Ahrens@Sun.COM     zfs_userspace_cb_t func, void *arg)
4034*9396SMatthew.Ahrens@Sun.COM {
4035*9396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
4036*9396SMatthew.Ahrens@Sun.COM 	int error;
4037*9396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t buf[100];
4038*9396SMatthew.Ahrens@Sun.COM 
4039*9396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4040*9396SMatthew.Ahrens@Sun.COM 
4041*9396SMatthew.Ahrens@Sun.COM 	zc.zc_objset_type = type;
4042*9396SMatthew.Ahrens@Sun.COM 	zc.zc_nvlist_dst = (uintptr_t)buf;
4043*9396SMatthew.Ahrens@Sun.COM 
4044*9396SMatthew.Ahrens@Sun.COM 	/* CONSTCOND */
4045*9396SMatthew.Ahrens@Sun.COM 	while (1) {
4046*9396SMatthew.Ahrens@Sun.COM 		zfs_useracct_t *zua = buf;
4047*9396SMatthew.Ahrens@Sun.COM 
4048*9396SMatthew.Ahrens@Sun.COM 		zc.zc_nvlist_dst_size = sizeof (buf);
4049*9396SMatthew.Ahrens@Sun.COM 		error = ioctl(zhp->zfs_hdl->libzfs_fd,
4050*9396SMatthew.Ahrens@Sun.COM 		    ZFS_IOC_USERSPACE_MANY, &zc);
4051*9396SMatthew.Ahrens@Sun.COM 		if (error || zc.zc_nvlist_dst_size == 0)
4052*9396SMatthew.Ahrens@Sun.COM 			break;
4053*9396SMatthew.Ahrens@Sun.COM 
4054*9396SMatthew.Ahrens@Sun.COM 		while (zc.zc_nvlist_dst_size > 0) {
4055*9396SMatthew.Ahrens@Sun.COM 			func(arg, zua->zu_domain, zua->zu_rid, zua->zu_space);
4056*9396SMatthew.Ahrens@Sun.COM 			zua++;
4057*9396SMatthew.Ahrens@Sun.COM 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4058*9396SMatthew.Ahrens@Sun.COM 		}
4059*9396SMatthew.Ahrens@Sun.COM 	}
4060*9396SMatthew.Ahrens@Sun.COM 
4061*9396SMatthew.Ahrens@Sun.COM 	return (error);
4062*9396SMatthew.Ahrens@Sun.COM }
4063