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>
489396SMatthew.Ahrens@Sun.COM #include <idmap.h>
499396SMatthew.Ahrens@Sun.COM #include <aclutils.h>
5010160SMatthew.Ahrens@Sun.COM #include <directory.h>
51789Sahrens 
52789Sahrens #include <sys/spa.h>
532676Seschrock #include <sys/zap.h>
54789Sahrens #include <libzfs.h>
55789Sahrens 
56789Sahrens #include "zfs_namecheck.h"
57789Sahrens #include "zfs_prop.h"
58789Sahrens #include "libzfs_impl.h"
594543Smarks #include "zfs_deleg.h"
60789Sahrens 
614007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
629396SMatthew.Ahrens@Sun.COM static int userquota_propname_decode(const char *propname, boolean_t zoned,
639396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
644007Smmusante 
65789Sahrens /*
66789Sahrens  * Given a single type (not a mask of types), return the type in a human
67789Sahrens  * readable form.
68789Sahrens  */
69789Sahrens const char *
70789Sahrens zfs_type_to_name(zfs_type_t type)
71789Sahrens {
72789Sahrens 	switch (type) {
73789Sahrens 	case ZFS_TYPE_FILESYSTEM:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
75789Sahrens 	case ZFS_TYPE_SNAPSHOT:
76789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
77789Sahrens 	case ZFS_TYPE_VOLUME:
78789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
79789Sahrens 	}
80789Sahrens 
81789Sahrens 	return (NULL);
82789Sahrens }
83789Sahrens 
84789Sahrens /*
85789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
86789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
87789Sahrens  * We guess what the type would have been based on the path and the mask of
88789Sahrens  * acceptable types.
89789Sahrens  */
90789Sahrens static const char *
91789Sahrens path_to_str(const char *path, int types)
92789Sahrens {
93789Sahrens 	/*
94789Sahrens 	 * When given a single type, always report the exact type.
95789Sahrens 	 */
96789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
98789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
99789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
100789Sahrens 	if (types == ZFS_TYPE_VOLUME)
101789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
102789Sahrens 
103789Sahrens 	/*
104789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
105789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
106789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
107789Sahrens 	 * snapshot attribute and try again.
108789Sahrens 	 */
109789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
110789Sahrens 		if (strchr(path, '@') != NULL)
111789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
112789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
113789Sahrens 	}
114789Sahrens 
115789Sahrens 	/*
116789Sahrens 	 * The user has requested either filesystems or volumes.
117789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
118789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
119789Sahrens 	 */
120789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
121789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
122789Sahrens 
123789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
124789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
125789Sahrens }
126789Sahrens 
127789Sahrens /*
128789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
1299396SMatthew.Ahrens@Sun.COM  * provide a more meaningful error message.  We call zfs_error_aux() to
1309396SMatthew.Ahrens@Sun.COM  * explain exactly why the name was not valid.
131789Sahrens  */
132789Sahrens static int
1335326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1345326Sek110237     boolean_t modifying)
135789Sahrens {
136789Sahrens 	namecheck_err_t why;
137789Sahrens 	char what;
138789Sahrens 
139789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1402082Seschrock 		if (hdl != NULL) {
141789Sahrens 			switch (why) {
1421003Slling 			case NAME_ERR_TOOLONG:
1432082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442082Seschrock 				    "name is too long"));
1451003Slling 				break;
1461003Slling 
147789Sahrens 			case NAME_ERR_LEADING_SLASH:
1482082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492082Seschrock 				    "leading slash in name"));
150789Sahrens 				break;
151789Sahrens 
152789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1532082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542082Seschrock 				    "empty component in name"));
155789Sahrens 				break;
156789Sahrens 
157789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1582082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1592082Seschrock 				    "trailing slash in name"));
160789Sahrens 				break;
161789Sahrens 
162789Sahrens 			case NAME_ERR_INVALCHAR:
1632082Seschrock 				zfs_error_aux(hdl,
164789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1652082Seschrock 				    "'%c' in name"), what);
166789Sahrens 				break;
167789Sahrens 
168789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1692082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1702082Seschrock 				    "multiple '@' delimiters in name"));
171789Sahrens 				break;
1722856Snd150628 
1732856Snd150628 			case NAME_ERR_NOLETTER:
1742856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1752856Snd150628 				    "pool doesn't begin with a letter"));
1762856Snd150628 				break;
1772856Snd150628 
1782856Snd150628 			case NAME_ERR_RESERVED:
1792856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1802856Snd150628 				    "name is reserved"));
1812856Snd150628 				break;
1822856Snd150628 
1832856Snd150628 			case NAME_ERR_DISKLIKE:
1842856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1852856Snd150628 				    "reserved disk name"));
1862856Snd150628 				break;
187789Sahrens 			}
188789Sahrens 		}
189789Sahrens 
190789Sahrens 		return (0);
191789Sahrens 	}
192789Sahrens 
193789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1942082Seschrock 		if (hdl != NULL)
1952082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1962082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
197789Sahrens 		return (0);
198789Sahrens 	}
199789Sahrens 
2002199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2012199Sahrens 		if (hdl != NULL)
2022199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2033413Smmusante 			    "missing '@' delimiter in snapshot name"));
2042199Sahrens 		return (0);
2052199Sahrens 	}
2062199Sahrens 
2075326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2085326Sek110237 		if (hdl != NULL)
2095326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2105326Sek110237 			    "invalid character %c in name"), '%');
2115326Sek110237 		return (0);
2125326Sek110237 	}
2135326Sek110237 
2142082Seschrock 	return (-1);
215789Sahrens }
216789Sahrens 
217789Sahrens int
218789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
219789Sahrens {
2206423Sgw25295 	if (type == ZFS_TYPE_POOL)
2216423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2225326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
223789Sahrens }
224789Sahrens 
225789Sahrens /*
2262676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2272676Seschrock  * properties into a separate nvlist.
2282676Seschrock  */
2294217Seschrock static nvlist_t *
2304217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2312676Seschrock {
2322676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2332676Seschrock 	nvpair_t *elem;
2342676Seschrock 	nvlist_t *propval;
2354217Seschrock 	nvlist_t *nvl;
2364217Seschrock 
2374217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2384217Seschrock 		(void) no_memory(hdl);
2394217Seschrock 		return (NULL);
2404217Seschrock 	}
2412676Seschrock 
2422676Seschrock 	elem = NULL;
2434217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2442676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2452676Seschrock 			continue;
2462676Seschrock 
2472676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2484217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2494217Seschrock 			nvlist_free(nvl);
2504217Seschrock 			(void) no_memory(hdl);
2514217Seschrock 			return (NULL);
2524217Seschrock 		}
2532676Seschrock 	}
2542676Seschrock 
2554217Seschrock 	return (nvl);
2562676Seschrock }
2572676Seschrock 
2586865Srm160521 static zpool_handle_t *
2596865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2606865Srm160521 {
2616865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2626865Srm160521 	zpool_handle_t *zph;
2636865Srm160521 
2646865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2656865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2666865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2676865Srm160521 		hdl->libzfs_pool_handles = zph;
2686865Srm160521 	}
2696865Srm160521 	return (zph);
2706865Srm160521 }
2716865Srm160521 
2726865Srm160521 static zpool_handle_t *
2736865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2746865Srm160521 {
2756865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2766865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2776865Srm160521 
2786865Srm160521 	while ((zph != NULL) &&
2796865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2806865Srm160521 		zph = zph->zpool_next;
2816865Srm160521 	return (zph);
2826865Srm160521 }
2836865Srm160521 
2846865Srm160521 /*
2856865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2866865Srm160521  * If a handle to that pool already exists then that handle is returned.
2876865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2886865Srm160521  */
2896865Srm160521 static zpool_handle_t *
2906865Srm160521 zpool_handle(zfs_handle_t *zhp)
2916865Srm160521 {
2926865Srm160521 	char *pool_name;
2936865Srm160521 	int len;
2946865Srm160521 	zpool_handle_t *zph;
2956865Srm160521 
2966865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2976865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2986865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2996865Srm160521 
3006865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
3016865Srm160521 	if (zph == NULL)
3026865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
3036865Srm160521 
3046865Srm160521 	free(pool_name);
3056865Srm160521 	return (zph);
3066865Srm160521 }
3076865Srm160521 
3086865Srm160521 void
3096865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3106865Srm160521 {
3116865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3126865Srm160521 
3136865Srm160521 	while (zph != NULL) {
3146865Srm160521 		next = zph->zpool_next;
3156865Srm160521 		zpool_close(zph);
3166865Srm160521 		zph = next;
3176865Srm160521 	}
3186865Srm160521 	hdl->libzfs_pool_handles = NULL;
3196865Srm160521 }
3206865Srm160521 
3212676Seschrock /*
322789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
323789Sahrens  */
324789Sahrens static int
3258228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
326789Sahrens {
3272676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3288228SEric.Taylor@Sun.COM 
3298228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
3308228SEric.Taylor@Sun.COM 
3318228SEric.Taylor@Sun.COM 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3321356Seschrock 		if (errno == ENOMEM) {
3338228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
3342082Seschrock 				return (-1);
3352676Seschrock 			}
3361356Seschrock 		} else {
3371356Seschrock 			return (-1);
3381356Seschrock 		}
3391356Seschrock 	}
3408228SEric.Taylor@Sun.COM 	return (0);
3418228SEric.Taylor@Sun.COM }
3428228SEric.Taylor@Sun.COM 
3438228SEric.Taylor@Sun.COM static int
3448228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
3458228SEric.Taylor@Sun.COM {
3468228SEric.Taylor@Sun.COM 	nvlist_t *allprops, *userprops;
3478228SEric.Taylor@Sun.COM 
3488228SEric.Taylor@Sun.COM 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
3498228SEric.Taylor@Sun.COM 
3508228SEric.Taylor@Sun.COM 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
3512082Seschrock 		return (-1);
3522082Seschrock 	}
353789Sahrens 
3549396SMatthew.Ahrens@Sun.COM 	/*
3559396SMatthew.Ahrens@Sun.COM 	 * XXX Why do we store the user props separately, in addition to
3569396SMatthew.Ahrens@Sun.COM 	 * storing them in zfs_props?
3579396SMatthew.Ahrens@Sun.COM 	 */
3584217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3594217Seschrock 		nvlist_free(allprops);
3602676Seschrock 		return (-1);
3614217Seschrock 	}
3624217Seschrock 
3634217Seschrock 	nvlist_free(zhp->zfs_props);
3644217Seschrock 	nvlist_free(zhp->zfs_user_props);
3654217Seschrock 
3664217Seschrock 	zhp->zfs_props = allprops;
3674217Seschrock 	zhp->zfs_user_props = userprops;
3682082Seschrock 
369789Sahrens 	return (0);
370789Sahrens }
371789Sahrens 
3728228SEric.Taylor@Sun.COM static int
3738228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp)
3748228SEric.Taylor@Sun.COM {
3758228SEric.Taylor@Sun.COM 	int rc = 0;
3768228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
3778228SEric.Taylor@Sun.COM 
3788228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
3798228SEric.Taylor@Sun.COM 		return (-1);
3808228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) != 0)
3818228SEric.Taylor@Sun.COM 		rc = -1;
3828228SEric.Taylor@Sun.COM 	else if (put_stats_zhdl(zhp, &zc) != 0)
3838228SEric.Taylor@Sun.COM 		rc = -1;
3848228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
3858228SEric.Taylor@Sun.COM 	return (rc);
3868228SEric.Taylor@Sun.COM }
3878228SEric.Taylor@Sun.COM 
388789Sahrens /*
389789Sahrens  * Refresh the properties currently stored in the handle.
390789Sahrens  */
391789Sahrens void
392789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
393789Sahrens {
394789Sahrens 	(void) get_stats(zhp);
395789Sahrens }
396789Sahrens 
397789Sahrens /*
398789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
399789Sahrens  * zfs_iter_* to create child handles on the fly.
400789Sahrens  */
4018228SEric.Taylor@Sun.COM static int
4028228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
403789Sahrens {
4044543Smarks 	char *logstr;
4058228SEric.Taylor@Sun.COM 	libzfs_handle_t *hdl = zhp->zfs_hdl;
406789Sahrens 
4074543Smarks 	/*
4084543Smarks 	 * Preserve history log string.
4094543Smarks 	 * any changes performed here will be
4104543Smarks 	 * logged as an internal event.
4114543Smarks 	 */
4124543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
4134543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
4148228SEric.Taylor@Sun.COM 
4151758Sahrens top:
4168228SEric.Taylor@Sun.COM 	if (put_stats_zhdl(zhp, zc) != 0) {
4174543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
4188228SEric.Taylor@Sun.COM 		return (-1);
419789Sahrens 	}
420789Sahrens 
4218228SEric.Taylor@Sun.COM 
4221758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4238228SEric.Taylor@Sun.COM 		zfs_cmd_t zc2 = { 0 };
4241758Sahrens 
4251758Sahrens 		/*
4261758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4271758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4281758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4291758Sahrens 		 * can't be mounted.  However, it could also be that we
4301758Sahrens 		 * have crashed in the middle of one of those
4311758Sahrens 		 * operations, in which case we need to get rid of the
4321758Sahrens 		 * inconsistent state.  We do that by either rolling
4331758Sahrens 		 * back to the previous snapshot (which will fail if
4341758Sahrens 		 * there is none), or destroying the filesystem.  Note
4351758Sahrens 		 * that if we are still in the middle of an active
4361758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4371758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4381758Sahrens 		 */
4391758Sahrens 
4408228SEric.Taylor@Sun.COM 		(void) strlcpy(zc2.zc_name, zhp->zfs_name,
4418228SEric.Taylor@Sun.COM 		    sizeof (zc2.zc_name));
4421758Sahrens 
4432885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4442082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4458228SEric.Taylor@Sun.COM 			zc2.zc_objset_type = DMU_OST_ZVOL;
4461758Sahrens 		} else {
4478228SEric.Taylor@Sun.COM 			zc2.zc_objset_type = DMU_OST_ZFS;
4481758Sahrens 		}
4491758Sahrens 
4501758Sahrens 		/*
4515331Samw 		 * If we can successfully destroy it, pretend that it
4521758Sahrens 		 * never existed.
4531758Sahrens 		 */
4548228SEric.Taylor@Sun.COM 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
4554543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4561758Sahrens 			errno = ENOENT;
4578228SEric.Taylor@Sun.COM 			return (-1);
4581758Sahrens 		}
4598228SEric.Taylor@Sun.COM 		/* If we can successfully roll it back, reset the stats */
4608228SEric.Taylor@Sun.COM 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
4618228SEric.Taylor@Sun.COM 			if (get_stats_ioctl(zhp, zc) != 0) {
4628228SEric.Taylor@Sun.COM 				zhp->zfs_hdl->libzfs_log_str = logstr;
4638228SEric.Taylor@Sun.COM 				return (-1);
4648228SEric.Taylor@Sun.COM 			}
4655367Sahrens 			goto top;
4668228SEric.Taylor@Sun.COM 		}
4671758Sahrens 	}
4681758Sahrens 
469789Sahrens 	/*
470789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
471789Sahrens 	 * the high-level type.
472789Sahrens 	 */
4732885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4742885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4752885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4762885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4772885Sahrens 	else
4782885Sahrens 		abort();
4792885Sahrens 
480789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
481789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
482789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
483789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
484789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
485789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
486789Sahrens 	else
4872082Seschrock 		abort();	/* we should never see any other types */
488789Sahrens 
4894543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
4906865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
4918228SEric.Taylor@Sun.COM 	return (0);
4928228SEric.Taylor@Sun.COM }
4938228SEric.Taylor@Sun.COM 
4948228SEric.Taylor@Sun.COM zfs_handle_t *
4958228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path)
4968228SEric.Taylor@Sun.COM {
4978228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
4988228SEric.Taylor@Sun.COM 
4998228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5008228SEric.Taylor@Sun.COM 
5018228SEric.Taylor@Sun.COM 	if (zhp == NULL)
5028228SEric.Taylor@Sun.COM 		return (NULL);
5038228SEric.Taylor@Sun.COM 
5048228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
5058228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
5068228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
5078228SEric.Taylor@Sun.COM 		free(zhp);
5088228SEric.Taylor@Sun.COM 		return (NULL);
5098228SEric.Taylor@Sun.COM 	}
5108228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) == -1) {
5118228SEric.Taylor@Sun.COM 		zcmd_free_nvlists(&zc);
5128228SEric.Taylor@Sun.COM 		free(zhp);
5138228SEric.Taylor@Sun.COM 		return (NULL);
5148228SEric.Taylor@Sun.COM 	}
5158228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, &zc) == -1) {
5168228SEric.Taylor@Sun.COM 		free(zhp);
5178228SEric.Taylor@Sun.COM 		zhp = NULL;
5188228SEric.Taylor@Sun.COM 	}
5198228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
5208228SEric.Taylor@Sun.COM 	return (zhp);
5218228SEric.Taylor@Sun.COM }
5228228SEric.Taylor@Sun.COM 
5238228SEric.Taylor@Sun.COM static zfs_handle_t *
5248228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
5258228SEric.Taylor@Sun.COM {
5268228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5278228SEric.Taylor@Sun.COM 
5288228SEric.Taylor@Sun.COM 	if (zhp == NULL)
5298228SEric.Taylor@Sun.COM 		return (NULL);
5308228SEric.Taylor@Sun.COM 
5318228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
5328228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5338228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, zc) == -1) {
5348228SEric.Taylor@Sun.COM 		free(zhp);
5358228SEric.Taylor@Sun.COM 		return (NULL);
5368228SEric.Taylor@Sun.COM 	}
537789Sahrens 	return (zhp);
538789Sahrens }
539789Sahrens 
540789Sahrens /*
541789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
542789Sahrens  * argument is a mask of acceptable types.  The function will print an
543789Sahrens  * appropriate error message and return NULL if it can't be opened.
544789Sahrens  */
545789Sahrens zfs_handle_t *
5462082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
547789Sahrens {
548789Sahrens 	zfs_handle_t *zhp;
5492082Seschrock 	char errbuf[1024];
5502082Seschrock 
5512082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
5522082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
553789Sahrens 
554789Sahrens 	/*
5552082Seschrock 	 * Validate the name before we even try to open it.
556789Sahrens 	 */
5575326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
5582082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5592082Seschrock 		    "invalid dataset name"));
5602082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
561789Sahrens 		return (NULL);
562789Sahrens 	}
563789Sahrens 
564789Sahrens 	/*
565789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
566789Sahrens 	 */
567789Sahrens 	errno = 0;
5682082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5693237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
570789Sahrens 		return (NULL);
571789Sahrens 	}
572789Sahrens 
573789Sahrens 	if (!(types & zhp->zfs_type)) {
5742082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5752142Seschrock 		zfs_close(zhp);
576789Sahrens 		return (NULL);
577789Sahrens 	}
578789Sahrens 
579789Sahrens 	return (zhp);
580789Sahrens }
581789Sahrens 
582789Sahrens /*
583789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
584789Sahrens  */
585789Sahrens void
586789Sahrens zfs_close(zfs_handle_t *zhp)
587789Sahrens {
588789Sahrens 	if (zhp->zfs_mntopts)
589789Sahrens 		free(zhp->zfs_mntopts);
5902676Seschrock 	nvlist_free(zhp->zfs_props);
5912676Seschrock 	nvlist_free(zhp->zfs_user_props);
592789Sahrens 	free(zhp);
593789Sahrens }
594789Sahrens 
5958228SEric.Taylor@Sun.COM typedef struct mnttab_node {
5968228SEric.Taylor@Sun.COM 	struct mnttab mtn_mt;
5978228SEric.Taylor@Sun.COM 	avl_node_t mtn_node;
5988228SEric.Taylor@Sun.COM } mnttab_node_t;
5998228SEric.Taylor@Sun.COM 
6008228SEric.Taylor@Sun.COM static int
6018228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
6028228SEric.Taylor@Sun.COM {
6038228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn1 = arg1;
6048228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn2 = arg2;
6058228SEric.Taylor@Sun.COM 	int rv;
6068228SEric.Taylor@Sun.COM 
6078228SEric.Taylor@Sun.COM 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
6088228SEric.Taylor@Sun.COM 
6098228SEric.Taylor@Sun.COM 	if (rv == 0)
6108228SEric.Taylor@Sun.COM 		return (0);
6118228SEric.Taylor@Sun.COM 	return (rv > 0 ? 1 : -1);
6128228SEric.Taylor@Sun.COM }
6138228SEric.Taylor@Sun.COM 
6148228SEric.Taylor@Sun.COM void
6158228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl)
6168228SEric.Taylor@Sun.COM {
6178228SEric.Taylor@Sun.COM 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
6188228SEric.Taylor@Sun.COM 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
6198228SEric.Taylor@Sun.COM 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
6208811SEric.Taylor@Sun.COM }
6218811SEric.Taylor@Sun.COM 
6228811SEric.Taylor@Sun.COM void
6238811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl)
6248811SEric.Taylor@Sun.COM {
6258811SEric.Taylor@Sun.COM 	struct mnttab entry;
6268228SEric.Taylor@Sun.COM 
6278228SEric.Taylor@Sun.COM 	rewind(hdl->libzfs_mnttab);
6288228SEric.Taylor@Sun.COM 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
6298228SEric.Taylor@Sun.COM 		mnttab_node_t *mtn;
6308228SEric.Taylor@Sun.COM 
6318228SEric.Taylor@Sun.COM 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6328228SEric.Taylor@Sun.COM 			continue;
6338228SEric.Taylor@Sun.COM 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
6348228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
6358228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
6368228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
6378228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
6388228SEric.Taylor@Sun.COM 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
6398228SEric.Taylor@Sun.COM 	}
6408228SEric.Taylor@Sun.COM }
6418228SEric.Taylor@Sun.COM 
6428228SEric.Taylor@Sun.COM void
6438228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl)
6448228SEric.Taylor@Sun.COM {
6458228SEric.Taylor@Sun.COM 	void *cookie = NULL;
6468228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6478228SEric.Taylor@Sun.COM 
6488228SEric.Taylor@Sun.COM 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
6498228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_special);
6508228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mountp);
6518228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_fstype);
6528228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mntopts);
6538228SEric.Taylor@Sun.COM 		free(mtn);
6548228SEric.Taylor@Sun.COM 	}
6558228SEric.Taylor@Sun.COM 	avl_destroy(&hdl->libzfs_mnttab_cache);
6568228SEric.Taylor@Sun.COM }
6578228SEric.Taylor@Sun.COM 
6588811SEric.Taylor@Sun.COM void
6598811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
6608811SEric.Taylor@Sun.COM {
6618811SEric.Taylor@Sun.COM 	hdl->libzfs_mnttab_enable = enable;
6628811SEric.Taylor@Sun.COM }
6638811SEric.Taylor@Sun.COM 
6648228SEric.Taylor@Sun.COM int
6658228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
6668228SEric.Taylor@Sun.COM     struct mnttab *entry)
6678228SEric.Taylor@Sun.COM {
6688228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6698228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6708228SEric.Taylor@Sun.COM 
6718811SEric.Taylor@Sun.COM 	if (!hdl->libzfs_mnttab_enable) {
6728811SEric.Taylor@Sun.COM 		struct mnttab srch = { 0 };
6738811SEric.Taylor@Sun.COM 
6748811SEric.Taylor@Sun.COM 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
6758811SEric.Taylor@Sun.COM 			libzfs_mnttab_fini(hdl);
6768811SEric.Taylor@Sun.COM 		rewind(hdl->libzfs_mnttab);
6778811SEric.Taylor@Sun.COM 		srch.mnt_special = (char *)fsname;
6788811SEric.Taylor@Sun.COM 		srch.mnt_fstype = MNTTYPE_ZFS;
6798811SEric.Taylor@Sun.COM 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
6808811SEric.Taylor@Sun.COM 			return (0);
6818811SEric.Taylor@Sun.COM 		else
6828811SEric.Taylor@Sun.COM 			return (ENOENT);
6838811SEric.Taylor@Sun.COM 	}
6848811SEric.Taylor@Sun.COM 
6858228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6868811SEric.Taylor@Sun.COM 		libzfs_mnttab_update(hdl);
6878228SEric.Taylor@Sun.COM 
6888228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6898228SEric.Taylor@Sun.COM 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
6908228SEric.Taylor@Sun.COM 	if (mtn) {
6918228SEric.Taylor@Sun.COM 		*entry = mtn->mtn_mt;
6928228SEric.Taylor@Sun.COM 		return (0);
6938228SEric.Taylor@Sun.COM 	}
6948228SEric.Taylor@Sun.COM 	return (ENOENT);
6958228SEric.Taylor@Sun.COM }
6968228SEric.Taylor@Sun.COM 
6978228SEric.Taylor@Sun.COM void
6988228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
6998228SEric.Taylor@Sun.COM     const char *mountp, const char *mntopts)
7008228SEric.Taylor@Sun.COM {
7018228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
7028228SEric.Taylor@Sun.COM 
7038228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
7048228SEric.Taylor@Sun.COM 		return;
7058228SEric.Taylor@Sun.COM 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
7068228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
7078228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
7088228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
7098228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
7108228SEric.Taylor@Sun.COM 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
7118228SEric.Taylor@Sun.COM }
7128228SEric.Taylor@Sun.COM 
7138228SEric.Taylor@Sun.COM void
7148228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
7158228SEric.Taylor@Sun.COM {
7168228SEric.Taylor@Sun.COM 	mnttab_node_t find;
7178228SEric.Taylor@Sun.COM 	mnttab_node_t *ret;
7188228SEric.Taylor@Sun.COM 
7198228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
7208228SEric.Taylor@Sun.COM 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
7218228SEric.Taylor@Sun.COM 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
7228228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_special);
7238228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mountp);
7248228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_fstype);
7258228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mntopts);
7268228SEric.Taylor@Sun.COM 		free(ret);
7278228SEric.Taylor@Sun.COM 	}
7288228SEric.Taylor@Sun.COM }
7298228SEric.Taylor@Sun.COM 
7305713Srm160521 int
7315713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
7325713Srm160521 {
7336865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
7346865Srm160521 
7355713Srm160521 	if (zpool_handle == NULL)
7365713Srm160521 		return (-1);
7375713Srm160521 
7385713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
7395713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
7405713Srm160521 	return (0);
7415713Srm160521 }
7425713Srm160521 
7435713Srm160521 /*
7445713Srm160521  * The choice of reservation property depends on the SPA version.
7455713Srm160521  */
7465713Srm160521 static int
7475713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
7485713Srm160521 {
7495713Srm160521 	int spa_version;
7505713Srm160521 
7515713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
7525713Srm160521 		return (-1);
7535713Srm160521 
7545713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
7555713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
7565713Srm160521 	else
7575713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
7585713Srm160521 
7595713Srm160521 	return (0);
7605713Srm160521 }
7615713Srm160521 
7623912Slling /*
7632676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
7642676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
7652676Seschrock  * strings.
766789Sahrens  */
7677184Stimh nvlist_t *
7687184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
7695094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
770789Sahrens {
7712676Seschrock 	nvpair_t *elem;
7722676Seschrock 	uint64_t intval;
7732676Seschrock 	char *strval;
7745094Slling 	zfs_prop_t prop;
7752676Seschrock 	nvlist_t *ret;
7765331Samw 	int chosen_normal = -1;
7775331Samw 	int chosen_utf = -1;
7782676Seschrock 
7795094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
7805094Slling 		(void) no_memory(hdl);
7815094Slling 		return (NULL);
782789Sahrens 	}
783789Sahrens 
7849396SMatthew.Ahrens@Sun.COM 	/*
7859396SMatthew.Ahrens@Sun.COM 	 * Make sure this property is valid and applies to this type.
7869396SMatthew.Ahrens@Sun.COM 	 */
7879396SMatthew.Ahrens@Sun.COM 
7882676Seschrock 	elem = NULL;
7892676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
7905094Slling 		const char *propname = nvpair_name(elem);
7912676Seschrock 
7929396SMatthew.Ahrens@Sun.COM 		prop = zfs_name_to_prop(propname);
7939396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
7945094Slling 			/*
7959396SMatthew.Ahrens@Sun.COM 			 * This is a user property: make sure it's a
7965094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
7975094Slling 			 */
7985094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
7995094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8005094Slling 				    "'%s' must be a string"), propname);
8015094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8025094Slling 				goto error;
8035094Slling 			}
8045094Slling 
8055094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
8065094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8075094Slling 				    "property name '%s' is too long"),
8082676Seschrock 				    propname);
8092676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8102676Seschrock 				goto error;
8112676Seschrock 			}
8122676Seschrock 
8132676Seschrock 			(void) nvpair_value_string(elem, &strval);
8142676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
8152676Seschrock 				(void) no_memory(hdl);
8162676Seschrock 				goto error;
8172676Seschrock 			}
8182676Seschrock 			continue;
819789Sahrens 		}
8202676Seschrock 
8219396SMatthew.Ahrens@Sun.COM 		/*
8229396SMatthew.Ahrens@Sun.COM 		 * Currently, only user properties can be modified on
8239396SMatthew.Ahrens@Sun.COM 		 * snapshots.
8249396SMatthew.Ahrens@Sun.COM 		 */
8257265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
8267265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8277265Sahrens 			    "this property can not be modified for snapshots"));
8287265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8297265Sahrens 			goto error;
8307265Sahrens 		}
8317265Sahrens 
8329396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
8339396SMatthew.Ahrens@Sun.COM 			zfs_userquota_prop_t uqtype;
8349396SMatthew.Ahrens@Sun.COM 			char newpropname[128];
8359396SMatthew.Ahrens@Sun.COM 			char domain[128];
8369396SMatthew.Ahrens@Sun.COM 			uint64_t rid;
8379396SMatthew.Ahrens@Sun.COM 			uint64_t valary[3];
8389396SMatthew.Ahrens@Sun.COM 
8399396SMatthew.Ahrens@Sun.COM 			if (userquota_propname_decode(propname, zoned,
8409396SMatthew.Ahrens@Sun.COM 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
8419396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
8429396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN,
8439396SMatthew.Ahrens@Sun.COM 				    "'%s' has an invalid user/group name"),
8449396SMatthew.Ahrens@Sun.COM 				    propname);
8459396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8469396SMatthew.Ahrens@Sun.COM 				goto error;
8479396SMatthew.Ahrens@Sun.COM 			}
8489396SMatthew.Ahrens@Sun.COM 
8499396SMatthew.Ahrens@Sun.COM 			if (uqtype != ZFS_PROP_USERQUOTA &&
8509396SMatthew.Ahrens@Sun.COM 			    uqtype != ZFS_PROP_GROUPQUOTA) {
8519396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
8529396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
8539396SMatthew.Ahrens@Sun.COM 				    propname);
8549396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
8559396SMatthew.Ahrens@Sun.COM 				    errbuf);
8569396SMatthew.Ahrens@Sun.COM 				goto error;
8579396SMatthew.Ahrens@Sun.COM 			}
8589396SMatthew.Ahrens@Sun.COM 
8599396SMatthew.Ahrens@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
8609396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_string(elem, &strval);
8619396SMatthew.Ahrens@Sun.COM 				if (strcmp(strval, "none") == 0) {
8629396SMatthew.Ahrens@Sun.COM 					intval = 0;
8639396SMatthew.Ahrens@Sun.COM 				} else if (zfs_nicestrtonum(hdl,
8649396SMatthew.Ahrens@Sun.COM 				    strval, &intval) != 0) {
8659396SMatthew.Ahrens@Sun.COM 					(void) zfs_error(hdl,
8669396SMatthew.Ahrens@Sun.COM 					    EZFS_BADPROP, errbuf);
8679396SMatthew.Ahrens@Sun.COM 					goto error;
8689396SMatthew.Ahrens@Sun.COM 				}
8699396SMatthew.Ahrens@Sun.COM 			} else if (nvpair_type(elem) ==
8709396SMatthew.Ahrens@Sun.COM 			    DATA_TYPE_UINT64) {
8719396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_uint64(elem, &intval);
8729396SMatthew.Ahrens@Sun.COM 				if (intval == 0) {
8739396SMatthew.Ahrens@Sun.COM 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8749396SMatthew.Ahrens@Sun.COM 					    "use 'none' to disable "
8759396SMatthew.Ahrens@Sun.COM 					    "userquota/groupquota"));
8769396SMatthew.Ahrens@Sun.COM 					goto error;
8779396SMatthew.Ahrens@Sun.COM 				}
8789396SMatthew.Ahrens@Sun.COM 			} else {
8799396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8809396SMatthew.Ahrens@Sun.COM 				    "'%s' must be a number"), propname);
8819396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8829396SMatthew.Ahrens@Sun.COM 				goto error;
8839396SMatthew.Ahrens@Sun.COM 			}
8849396SMatthew.Ahrens@Sun.COM 
8859396SMatthew.Ahrens@Sun.COM 			(void) snprintf(newpropname, sizeof (newpropname),
8869396SMatthew.Ahrens@Sun.COM 			    "%s%s", zfs_userquota_prop_prefixes[uqtype],
8879396SMatthew.Ahrens@Sun.COM 			    domain);
8889396SMatthew.Ahrens@Sun.COM 			valary[0] = uqtype;
8899396SMatthew.Ahrens@Sun.COM 			valary[1] = rid;
8909396SMatthew.Ahrens@Sun.COM 			valary[2] = intval;
8919396SMatthew.Ahrens@Sun.COM 			if (nvlist_add_uint64_array(ret, newpropname,
8929396SMatthew.Ahrens@Sun.COM 			    valary, 3) != 0) {
8939396SMatthew.Ahrens@Sun.COM 				(void) no_memory(hdl);
8949396SMatthew.Ahrens@Sun.COM 				goto error;
8959396SMatthew.Ahrens@Sun.COM 			}
8969396SMatthew.Ahrens@Sun.COM 			continue;
8979396SMatthew.Ahrens@Sun.COM 		}
8989396SMatthew.Ahrens@Sun.COM 
8999396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL) {
9009396SMatthew.Ahrens@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9019396SMatthew.Ahrens@Sun.COM 			    "invalid property '%s'"), propname);
9029396SMatthew.Ahrens@Sun.COM 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9039396SMatthew.Ahrens@Sun.COM 			goto error;
9049396SMatthew.Ahrens@Sun.COM 		}
9059396SMatthew.Ahrens@Sun.COM 
9062676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
9072676Seschrock 			zfs_error_aux(hdl,
9082676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
9092676Seschrock 			    "apply to datasets of this type"), propname);
9102676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
9112676Seschrock 			goto error;
9122676Seschrock 		}
9132676Seschrock 
9142676Seschrock 		if (zfs_prop_readonly(prop) &&
9155331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
9162676Seschrock 			zfs_error_aux(hdl,
9172676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
9182676Seschrock 			    propname);
9192676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
9202676Seschrock 			goto error;
9212676Seschrock 		}
9222676Seschrock 
9235094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
9245094Slling 		    &strval, &intval, errbuf) != 0)
9252676Seschrock 			goto error;
9262676Seschrock 
9272676Seschrock 		/*
9282676Seschrock 		 * Perform some additional checks for specific properties.
9292676Seschrock 		 */
9302676Seschrock 		switch (prop) {
9314577Sahrens 		case ZFS_PROP_VERSION:
9324577Sahrens 		{
9334577Sahrens 			int version;
9344577Sahrens 
9354577Sahrens 			if (zhp == NULL)
9364577Sahrens 				break;
9374577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
9384577Sahrens 			if (intval < version) {
9394577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9404577Sahrens 				    "Can not downgrade; already at version %u"),
9414577Sahrens 				    version);
9424577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9434577Sahrens 				goto error;
9444577Sahrens 			}
9454577Sahrens 			break;
9464577Sahrens 		}
9474577Sahrens 
9482676Seschrock 		case ZFS_PROP_RECORDSIZE:
9492676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
9502676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
9512676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
9522676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
9532082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9542676Seschrock 				    "'%s' must be power of 2 from %u "
9552676Seschrock 				    "to %uk"), propname,
9562676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
9572676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
9582676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9592676Seschrock 				goto error;
960789Sahrens 			}
961789Sahrens 			break;
962789Sahrens 
9633126Sahl 		case ZFS_PROP_SHAREISCSI:
9643126Sahl 			if (strcmp(strval, "off") != 0 &&
9653126Sahl 			    strcmp(strval, "on") != 0 &&
9663126Sahl 			    strcmp(strval, "type=disk") != 0) {
9673126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9683126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
9693126Sahl 				    propname);
9703126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9713126Sahl 				goto error;
9723126Sahl 			}
9733126Sahl 
9743126Sahl 			break;
9753126Sahl 
9762676Seschrock 		case ZFS_PROP_MOUNTPOINT:
9774778Srm160521 		{
9784778Srm160521 			namecheck_err_t why;
9794778Srm160521 
9802676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
9812676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9822676Seschrock 				break;
9832676Seschrock 
9844778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
9854778Srm160521 				switch (why) {
9864778Srm160521 				case NAME_ERR_LEADING_SLASH:
9874778Srm160521 					zfs_error_aux(hdl,
9884778Srm160521 					    dgettext(TEXT_DOMAIN,
9894778Srm160521 					    "'%s' must be an absolute path, "
9904778Srm160521 					    "'none', or 'legacy'"), propname);
9914778Srm160521 					break;
9924778Srm160521 				case NAME_ERR_TOOLONG:
9934778Srm160521 					zfs_error_aux(hdl,
9944778Srm160521 					    dgettext(TEXT_DOMAIN,
9954778Srm160521 					    "component of '%s' is too long"),
9964778Srm160521 					    propname);
9974778Srm160521 					break;
9984778Srm160521 				}
9992676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
10002676Seschrock 				goto error;
1001789Sahrens 			}
10024778Srm160521 		}
10034778Srm160521 
10043126Sahl 			/*FALLTHRU*/
10053126Sahl 
10065331Samw 		case ZFS_PROP_SHARESMB:
10073126Sahl 		case ZFS_PROP_SHARENFS:
10083126Sahl 			/*
10095331Samw 			 * For the mountpoint and sharenfs or sharesmb
10105331Samw 			 * properties, check if it can be set in a
10115331Samw 			 * global/non-global zone based on
10123126Sahl 			 * the zoned property value:
10133126Sahl 			 *
10143126Sahl 			 *		global zone	    non-global zone
10153126Sahl 			 * --------------------------------------------------
10163126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
10173126Sahl 			 *		sharenfs (no)	    sharenfs (no)
10185331Samw 			 *		sharesmb (no)	    sharesmb (no)
10193126Sahl 			 *
10203126Sahl 			 * zoned=off	mountpoint (yes)	N/A
10213126Sahl 			 *		sharenfs (yes)
10225331Samw 			 *		sharesmb (yes)
10233126Sahl 			 */
10242676Seschrock 			if (zoned) {
10252676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
10262676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10272676Seschrock 					    "'%s' cannot be set on "
10282676Seschrock 					    "dataset in a non-global zone"),
10292676Seschrock 					    propname);
10302676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
10312676Seschrock 					    errbuf);
10322676Seschrock 					goto error;
10335331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
10345331Samw 				    prop == ZFS_PROP_SHARESMB) {
10352676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10362676Seschrock 					    "'%s' cannot be set in "
10372676Seschrock 					    "a non-global zone"), propname);
10382676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
10392676Seschrock 					    errbuf);
10402676Seschrock 					goto error;
10412676Seschrock 				}
10422676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
10432676Seschrock 				/*
10442676Seschrock 				 * If zoned property is 'off', this must be in
10459396SMatthew.Ahrens@Sun.COM 				 * a global zone. If not, something is wrong.
10462676Seschrock 				 */
10472676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10482676Seschrock 				    "'%s' cannot be set while dataset "
10492676Seschrock 				    "'zoned' property is set"), propname);
10502676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
10512676Seschrock 				goto error;
10522676Seschrock 			}
10533126Sahl 
10544180Sdougm 			/*
10554180Sdougm 			 * At this point, it is legitimate to set the
10564180Sdougm 			 * property. Now we want to make sure that the
10574180Sdougm 			 * property value is valid if it is sharenfs.
10584180Sdougm 			 */
10595331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
10605331Samw 			    prop == ZFS_PROP_SHARESMB) &&
10614217Seschrock 			    strcmp(strval, "on") != 0 &&
10624217Seschrock 			    strcmp(strval, "off") != 0) {
10635331Samw 				zfs_share_proto_t proto;
10645331Samw 
10655331Samw 				if (prop == ZFS_PROP_SHARESMB)
10665331Samw 					proto = PROTO_SMB;
10675331Samw 				else
10685331Samw 					proto = PROTO_NFS;
10694180Sdougm 
10704180Sdougm 				/*
10715331Samw 				 * Must be an valid sharing protocol
10725331Samw 				 * option string so init the libshare
10735331Samw 				 * in order to enable the parser and
10745331Samw 				 * then parse the options. We use the
10755331Samw 				 * control API since we don't care about
10765331Samw 				 * the current configuration and don't
10774180Sdougm 				 * want the overhead of loading it
10784180Sdougm 				 * until we actually do something.
10794180Sdougm 				 */
10804180Sdougm 
10814217Seschrock 				if (zfs_init_libshare(hdl,
10824217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
10834217Seschrock 					/*
10844217Seschrock 					 * An error occurred so we can't do
10854217Seschrock 					 * anything
10864217Seschrock 					 */
10874217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10884217Seschrock 					    "'%s' cannot be set: problem "
10894217Seschrock 					    "in share initialization"),
10904217Seschrock 					    propname);
10914217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10924217Seschrock 					    errbuf);
10934217Seschrock 					goto error;
10944217Seschrock 				}
10954217Seschrock 
10965331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
10974217Seschrock 					/*
10984217Seschrock 					 * There was an error in parsing so
10994217Seschrock 					 * deal with it by issuing an error
11004217Seschrock 					 * message and leaving after
11014217Seschrock 					 * uninitializing the the libshare
11024217Seschrock 					 * interface.
11034217Seschrock 					 */
11044217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11054217Seschrock 					    "'%s' cannot be set to invalid "
11064217Seschrock 					    "options"), propname);
11074217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11084217Seschrock 					    errbuf);
11094217Seschrock 					zfs_uninit_libshare(hdl);
11104217Seschrock 					goto error;
11114217Seschrock 				}
11124180Sdougm 				zfs_uninit_libshare(hdl);
11134180Sdougm 			}
11144180Sdougm 
11153126Sahl 			break;
11165331Samw 		case ZFS_PROP_UTF8ONLY:
11175331Samw 			chosen_utf = (int)intval;
11185331Samw 			break;
11195331Samw 		case ZFS_PROP_NORMALIZE:
11205331Samw 			chosen_normal = (int)intval;
11215331Samw 			break;
11222676Seschrock 		}
11232676Seschrock 
11242676Seschrock 		/*
11252676Seschrock 		 * For changes to existing volumes, we have some additional
11262676Seschrock 		 * checks to enforce.
11272676Seschrock 		 */
11282676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
11292676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
11302676Seschrock 			    ZFS_PROP_VOLSIZE);
11312676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
11322676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
11332676Seschrock 			char buf[64];
11342676Seschrock 
11352676Seschrock 			switch (prop) {
11362676Seschrock 			case ZFS_PROP_RESERVATION:
11375378Sck153898 			case ZFS_PROP_REFRESERVATION:
11382676Seschrock 				if (intval > volsize) {
11392676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11402676Seschrock 					    "'%s' is greater than current "
11412676Seschrock 					    "volume size"), propname);
11422676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11432676Seschrock 					    errbuf);
11442676Seschrock 					goto error;
11452676Seschrock 				}
11462676Seschrock 				break;
11472676Seschrock 
11482676Seschrock 			case ZFS_PROP_VOLSIZE:
11492676Seschrock 				if (intval % blocksize != 0) {
11502676Seschrock 					zfs_nicenum(blocksize, buf,
11512676Seschrock 					    sizeof (buf));
11522676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11532676Seschrock 					    "'%s' must be a multiple of "
11542676Seschrock 					    "volume block size (%s)"),
11552676Seschrock 					    propname, buf);
11562676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11572676Seschrock 					    errbuf);
11582676Seschrock 					goto error;
11592676Seschrock 				}
11602676Seschrock 
11612676Seschrock 				if (intval == 0) {
11622676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11632676Seschrock 					    "'%s' cannot be zero"),
11642676Seschrock 					    propname);
11652676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11662676Seschrock 					    errbuf);
11672676Seschrock 					goto error;
1168789Sahrens 				}
11693126Sahl 				break;
1170789Sahrens 			}
1171789Sahrens 		}
1172789Sahrens 	}
1173789Sahrens 
11742676Seschrock 	/*
11755331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
11765331Samw 	 * enforce rejection of non-UTF8 names.
11775331Samw 	 *
11785331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
11795331Samw 	 * was explicitly not chosen, it is an error.
11805331Samw 	 */
11815498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
11825331Samw 		if (nvlist_add_uint64(ret,
11835331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
11845331Samw 			(void) no_memory(hdl);
11855331Samw 			goto error;
11865331Samw 		}
11875498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
11885331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11895331Samw 		    "'%s' must be set 'on' if normalization chosen"),
11905331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
11915331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11925331Samw 		goto error;
11935331Samw 	}
11945331Samw 
11955331Samw 	/*
11962676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
11972676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
11982676Seschrock 	 */
11992676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
12002676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
12012676Seschrock 	    &intval) == 0) {
12022676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
12032676Seschrock 		    ZFS_PROP_VOLSIZE);
12045481Sck153898 		uint64_t old_reservation;
12052676Seschrock 		uint64_t new_reservation;
12065481Sck153898 		zfs_prop_t resv_prop;
12075713Srm160521 
12085713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
12095481Sck153898 			goto error;
12105481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
12112676Seschrock 
12122676Seschrock 		if (old_volsize == old_reservation &&
12135481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
12142676Seschrock 		    &new_reservation) != 0) {
12152676Seschrock 			if (nvlist_add_uint64(ret,
12165481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
12172676Seschrock 				(void) no_memory(hdl);
12182676Seschrock 				goto error;
12192676Seschrock 			}
12202676Seschrock 		}
12212676Seschrock 	}
12222676Seschrock 	return (ret);
12232676Seschrock 
12242676Seschrock error:
12252676Seschrock 	nvlist_free(ret);
12262676Seschrock 	return (NULL);
1227789Sahrens }
1228789Sahrens 
1229789Sahrens /*
1230789Sahrens  * Given a property name and value, set the property for the given dataset.
1231789Sahrens  */
1232789Sahrens int
12332676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1234789Sahrens {
1235789Sahrens 	zfs_cmd_t zc = { 0 };
12362676Seschrock 	int ret = -1;
12372676Seschrock 	prop_changelist_t *cl = NULL;
12382082Seschrock 	char errbuf[1024];
12392082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
12402676Seschrock 	nvlist_t *nvl = NULL, *realprops;
12412676Seschrock 	zfs_prop_t prop;
12427509SMark.Musante@Sun.COM 	boolean_t do_prefix;
12437509SMark.Musante@Sun.COM 	uint64_t idx;
12442082Seschrock 
12452082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
12462676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
12472082Seschrock 	    zhp->zfs_name);
12482082Seschrock 
12492676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
12502676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
12512676Seschrock 		(void) no_memory(hdl);
12522676Seschrock 		goto error;
1253789Sahrens 	}
1254789Sahrens 
12557184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
12562676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
12572676Seschrock 		goto error;
12585094Slling 
12592676Seschrock 	nvlist_free(nvl);
12602676Seschrock 	nvl = realprops;
12612676Seschrock 
12622676Seschrock 	prop = zfs_name_to_prop(propname);
12632676Seschrock 
12647366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
12652676Seschrock 		goto error;
1266789Sahrens 
1267789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
12682082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12692082Seschrock 		    "child dataset with inherited mountpoint is used "
12702082Seschrock 		    "in a non-global zone"));
12712082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1272789Sahrens 		goto error;
1273789Sahrens 	}
1274789Sahrens 
12757509SMark.Musante@Sun.COM 	/*
12767509SMark.Musante@Sun.COM 	 * If the dataset's canmount property is being set to noauto,
12777509SMark.Musante@Sun.COM 	 * then we want to prevent unmounting & remounting it.
12787509SMark.Musante@Sun.COM 	 */
12797509SMark.Musante@Sun.COM 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
12807509SMark.Musante@Sun.COM 	    (zprop_string_to_index(prop, propval, &idx,
12817509SMark.Musante@Sun.COM 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
12826168Shs24103 
12836168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
12847509SMark.Musante@Sun.COM 		goto error;
1285789Sahrens 
1286789Sahrens 	/*
1287789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1288789Sahrens 	 */
1289789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1290789Sahrens 
12915094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
12922676Seschrock 		goto error;
12932676Seschrock 
12944543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
12958845Samw@Sun.COM 
1296789Sahrens 	if (ret != 0) {
1297789Sahrens 		switch (errno) {
1298789Sahrens 
1299789Sahrens 		case ENOSPC:
1300789Sahrens 			/*
1301789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1302789Sahrens 			 * something different; setting a quota or reservation
1303789Sahrens 			 * doesn't use any disk space.
1304789Sahrens 			 */
1305789Sahrens 			switch (prop) {
1306789Sahrens 			case ZFS_PROP_QUOTA:
13075378Sck153898 			case ZFS_PROP_REFQUOTA:
13082082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13092082Seschrock 				    "size is less than current used or "
13102082Seschrock 				    "reserved space"));
13112082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1312789Sahrens 				break;
1313789Sahrens 
1314789Sahrens 			case ZFS_PROP_RESERVATION:
13155378Sck153898 			case ZFS_PROP_REFRESERVATION:
13162082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13172082Seschrock 				    "size is greater than available space"));
13182082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1319789Sahrens 				break;
1320789Sahrens 
1321789Sahrens 			default:
13222082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1323789Sahrens 				break;
1324789Sahrens 			}
1325789Sahrens 			break;
1326789Sahrens 
1327789Sahrens 		case EBUSY:
13282082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
13292082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
13302082Seschrock 			else
13312676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1332789Sahrens 			break;
1333789Sahrens 
13341175Slling 		case EROFS:
13352082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
13361175Slling 			break;
13371175Slling 
13383886Sahl 		case ENOTSUP:
13393886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13405977Smarks 			    "pool and or dataset must be upgraded to set this "
13414603Sahrens 			    "property or value"));
13423886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
13433886Sahl 			break;
13443886Sahl 
13457042Sgw25295 		case ERANGE:
13467042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
13477042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13487042Sgw25295 				    "property setting is not allowed on "
13497042Sgw25295 				    "bootable datasets"));
13507042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
13517042Sgw25295 			} else {
13527042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
13537042Sgw25295 			}
13547042Sgw25295 			break;
13557042Sgw25295 
1356789Sahrens 		case EOVERFLOW:
1357789Sahrens 			/*
1358789Sahrens 			 * This platform can't address a volume this big.
1359789Sahrens 			 */
1360789Sahrens #ifdef _ILP32
1361789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
13622082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1363789Sahrens 				break;
1364789Sahrens 			}
1365789Sahrens #endif
13662082Seschrock 			/* FALLTHROUGH */
1367789Sahrens 		default:
13682082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1369789Sahrens 		}
1370789Sahrens 	} else {
13716168Shs24103 		if (do_prefix)
13726168Shs24103 			ret = changelist_postfix(cl);
13736168Shs24103 
1374789Sahrens 		/*
1375789Sahrens 		 * Refresh the statistics so the new property value
1376789Sahrens 		 * is reflected.
1377789Sahrens 		 */
13786168Shs24103 		if (ret == 0)
13792676Seschrock 			(void) get_stats(zhp);
1380789Sahrens 	}
1381789Sahrens 
1382789Sahrens error:
13832676Seschrock 	nvlist_free(nvl);
13842676Seschrock 	zcmd_free_nvlists(&zc);
13852676Seschrock 	if (cl)
13862676Seschrock 		changelist_free(cl);
1387789Sahrens 	return (ret);
1388789Sahrens }
1389789Sahrens 
1390789Sahrens /*
1391789Sahrens  * Given a property, inherit the value from the parent dataset.
1392789Sahrens  */
1393789Sahrens int
13942676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1395789Sahrens {
1396789Sahrens 	zfs_cmd_t zc = { 0 };
1397789Sahrens 	int ret;
1398789Sahrens 	prop_changelist_t *cl;
13992082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
14002082Seschrock 	char errbuf[1024];
14012676Seschrock 	zfs_prop_t prop;
14022082Seschrock 
14032082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
14042082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1405789Sahrens 
14065094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
14072676Seschrock 		/*
14082676Seschrock 		 * For user properties, the amount of work we have to do is very
14092676Seschrock 		 * small, so just do it here.
14102676Seschrock 		 */
14112676Seschrock 		if (!zfs_prop_user(propname)) {
14122676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14132676Seschrock 			    "invalid property"));
14142676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
14152676Seschrock 		}
14162676Seschrock 
14172676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14182676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
14192676Seschrock 
14204849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
14212676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
14222676Seschrock 
14232676Seschrock 		return (0);
14242676Seschrock 	}
14252676Seschrock 
1426789Sahrens 	/*
1427789Sahrens 	 * Verify that this property is inheritable.
1428789Sahrens 	 */
14292082Seschrock 	if (zfs_prop_readonly(prop))
14302082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
14312082Seschrock 
14322082Seschrock 	if (!zfs_prop_inheritable(prop))
14332082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1434789Sahrens 
1435789Sahrens 	/*
1436789Sahrens 	 * Check to see if the value applies to this type
1437789Sahrens 	 */
14382082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
14392082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1440789Sahrens 
14413443Srm160521 	/*
14423443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
14433443Srm160521 	 */
14443443Srm160521 	propname = zfs_prop_to_name(prop);
1445789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14462676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1447789Sahrens 
1448789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1449789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
14502082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14512082Seschrock 		    "dataset is used in a non-global zone"));
14522082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1453789Sahrens 	}
1454789Sahrens 
1455789Sahrens 	/*
1456789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1457789Sahrens 	 */
14587366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1459789Sahrens 		return (-1);
1460789Sahrens 
1461789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
14622082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14632082Seschrock 		    "child dataset with inherited mountpoint is used "
14642082Seschrock 		    "in a non-global zone"));
14652082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1466789Sahrens 		goto error;
1467789Sahrens 	}
1468789Sahrens 
1469789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1470789Sahrens 		goto error;
1471789Sahrens 
14724849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
14732082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1474789Sahrens 	} else {
1475789Sahrens 
14762169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1477789Sahrens 			goto error;
1478789Sahrens 
1479789Sahrens 		/*
1480789Sahrens 		 * Refresh the statistics so the new property is reflected.
1481789Sahrens 		 */
1482789Sahrens 		(void) get_stats(zhp);
1483789Sahrens 	}
1484789Sahrens 
1485789Sahrens error:
1486789Sahrens 	changelist_free(cl);
1487789Sahrens 	return (ret);
1488789Sahrens }
1489789Sahrens 
1490789Sahrens /*
14911356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
14921356Seschrock  * extract them appropriately.
14931356Seschrock  */
14941356Seschrock static uint64_t
14951356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
14961356Seschrock {
14971356Seschrock 	nvlist_t *nv;
14981356Seschrock 	uint64_t value;
14991356Seschrock 
15002885Sahrens 	*source = NULL;
15011356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
15021356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
15035094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
15045094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
15051356Seschrock 	} else {
15068802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
15078802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
15081356Seschrock 		value = zfs_prop_default_numeric(prop);
15091356Seschrock 		*source = "";
15101356Seschrock 	}
15111356Seschrock 
15121356Seschrock 	return (value);
15131356Seschrock }
15141356Seschrock 
15151356Seschrock static char *
15161356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
15171356Seschrock {
15181356Seschrock 	nvlist_t *nv;
15191356Seschrock 	char *value;
15201356Seschrock 
15212885Sahrens 	*source = NULL;
15221356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
15231356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
15245094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
15255094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
15261356Seschrock 	} else {
15278802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
15288802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
15291356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
15301356Seschrock 			value = "";
15311356Seschrock 		*source = "";
15321356Seschrock 	}
15331356Seschrock 
15341356Seschrock 	return (value);
15351356Seschrock }
15361356Seschrock 
15371356Seschrock /*
1538789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1539789Sahrens  * zfs_prop_get_int() are built using this interface.
1540789Sahrens  *
1541789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1542789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1543789Sahrens  * If they differ from the on-disk values, report the current values and mark
1544789Sahrens  * the source "temporary".
1545789Sahrens  */
15462082Seschrock static int
15475094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
15482082Seschrock     char **source, uint64_t *val)
1549789Sahrens {
15505147Srm160521 	zfs_cmd_t zc = { 0 };
15515592Stimh 	nvlist_t *zplprops = NULL;
1552789Sahrens 	struct mnttab mnt;
15533265Sahrens 	char *mntopt_on = NULL;
15543265Sahrens 	char *mntopt_off = NULL;
1555789Sahrens 
1556789Sahrens 	*source = NULL;
1557789Sahrens 
15583265Sahrens 	switch (prop) {
15593265Sahrens 	case ZFS_PROP_ATIME:
15603265Sahrens 		mntopt_on = MNTOPT_ATIME;
15613265Sahrens 		mntopt_off = MNTOPT_NOATIME;
15623265Sahrens 		break;
15633265Sahrens 
15643265Sahrens 	case ZFS_PROP_DEVICES:
15653265Sahrens 		mntopt_on = MNTOPT_DEVICES;
15663265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
15673265Sahrens 		break;
15683265Sahrens 
15693265Sahrens 	case ZFS_PROP_EXEC:
15703265Sahrens 		mntopt_on = MNTOPT_EXEC;
15713265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
15723265Sahrens 		break;
15733265Sahrens 
15743265Sahrens 	case ZFS_PROP_READONLY:
15753265Sahrens 		mntopt_on = MNTOPT_RO;
15763265Sahrens 		mntopt_off = MNTOPT_RW;
15773265Sahrens 		break;
15783265Sahrens 
15793265Sahrens 	case ZFS_PROP_SETUID:
15803265Sahrens 		mntopt_on = MNTOPT_SETUID;
15813265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
15823265Sahrens 		break;
15833265Sahrens 
15843265Sahrens 	case ZFS_PROP_XATTR:
15853265Sahrens 		mntopt_on = MNTOPT_XATTR;
15863265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
15873265Sahrens 		break;
15885331Samw 
15895331Samw 	case ZFS_PROP_NBMAND:
15905331Samw 		mntopt_on = MNTOPT_NBMAND;
15915331Samw 		mntopt_off = MNTOPT_NONBMAND;
15925331Samw 		break;
15933265Sahrens 	}
15943265Sahrens 
15952474Seschrock 	/*
15962474Seschrock 	 * Because looking up the mount options is potentially expensive
15972474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
15982474Seschrock 	 * we're looking up a property which requires its presence.
15992474Seschrock 	 */
16002474Seschrock 	if (!zhp->zfs_mntcheck &&
16013265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
16028228SEric.Taylor@Sun.COM 		libzfs_handle_t *hdl = zhp->zfs_hdl;
16038228SEric.Taylor@Sun.COM 		struct mnttab entry;
16048228SEric.Taylor@Sun.COM 
16058228SEric.Taylor@Sun.COM 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
16068228SEric.Taylor@Sun.COM 			zhp->zfs_mntopts = zfs_strdup(hdl,
16073265Sahrens 			    entry.mnt_mntopts);
16083265Sahrens 			if (zhp->zfs_mntopts == NULL)
16093265Sahrens 				return (-1);
16103265Sahrens 		}
16112474Seschrock 
16122474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
16132474Seschrock 	}
16142474Seschrock 
1615789Sahrens 	if (zhp->zfs_mntopts == NULL)
1616789Sahrens 		mnt.mnt_mntopts = "";
1617789Sahrens 	else
1618789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1619789Sahrens 
1620789Sahrens 	switch (prop) {
1621789Sahrens 	case ZFS_PROP_ATIME:
16223265Sahrens 	case ZFS_PROP_DEVICES:
16233265Sahrens 	case ZFS_PROP_EXEC:
16243265Sahrens 	case ZFS_PROP_READONLY:
16253265Sahrens 	case ZFS_PROP_SETUID:
16263265Sahrens 	case ZFS_PROP_XATTR:
16275331Samw 	case ZFS_PROP_NBMAND:
16282082Seschrock 		*val = getprop_uint64(zhp, prop, source);
16292082Seschrock 
16303265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
16312082Seschrock 			*val = B_TRUE;
1632789Sahrens 			if (src)
16335094Slling 				*src = ZPROP_SRC_TEMPORARY;
16343265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
16352082Seschrock 			*val = B_FALSE;
1636789Sahrens 			if (src)
16375094Slling 				*src = ZPROP_SRC_TEMPORARY;
1638789Sahrens 		}
16392082Seschrock 		break;
1640789Sahrens 
16413265Sahrens 	case ZFS_PROP_CANMOUNT:
16422082Seschrock 		*val = getprop_uint64(zhp, prop, source);
16436168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
16443417Srm160521 			*source = zhp->zfs_name;
16453417Srm160521 		else
16463417Srm160521 			*source = "";	/* default */
16472082Seschrock 		break;
1648789Sahrens 
1649789Sahrens 	case ZFS_PROP_QUOTA:
16505378Sck153898 	case ZFS_PROP_REFQUOTA:
1651789Sahrens 	case ZFS_PROP_RESERVATION:
16525378Sck153898 	case ZFS_PROP_REFRESERVATION:
16532885Sahrens 		*val = getprop_uint64(zhp, prop, source);
16542885Sahrens 		if (*val == 0)
1655789Sahrens 			*source = "";	/* default */
1656789Sahrens 		else
1657789Sahrens 			*source = zhp->zfs_name;
16582082Seschrock 		break;
1659789Sahrens 
1660789Sahrens 	case ZFS_PROP_MOUNTED:
16612082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
16622082Seschrock 		break;
1663789Sahrens 
16643377Seschrock 	case ZFS_PROP_NUMCLONES:
16653377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
16663377Seschrock 		break;
16673377Seschrock 
16685147Srm160521 	case ZFS_PROP_VERSION:
16695498Stimh 	case ZFS_PROP_NORMALIZE:
16705498Stimh 	case ZFS_PROP_UTF8ONLY:
16715498Stimh 	case ZFS_PROP_CASE:
16725498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
16735498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
16745473Srm160521 			return (-1);
16755147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16765498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
16775498Stimh 			zcmd_free_nvlists(&zc);
167810204SMatthew.Ahrens@Sun.COM 			return (-1);
16795147Srm160521 		}
16805498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
16815498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
16825498Stimh 		    val) != 0) {
16835498Stimh 			zcmd_free_nvlists(&zc);
168410204SMatthew.Ahrens@Sun.COM 			return (-1);
16855498Stimh 		}
16865592Stimh 		if (zplprops)
16875592Stimh 			nvlist_free(zplprops);
16885498Stimh 		zcmd_free_nvlists(&zc);
16895147Srm160521 		break;
16905147Srm160521 
1691789Sahrens 	default:
16924577Sahrens 		switch (zfs_prop_get_type(prop)) {
16934787Sahrens 		case PROP_TYPE_NUMBER:
16944787Sahrens 		case PROP_TYPE_INDEX:
16954577Sahrens 			*val = getprop_uint64(zhp, prop, source);
16967390SMatthew.Ahrens@Sun.COM 			/*
16979396SMatthew.Ahrens@Sun.COM 			 * If we tried to use a default value for a
16987390SMatthew.Ahrens@Sun.COM 			 * readonly property, it means that it was not
16997390SMatthew.Ahrens@Sun.COM 			 * present; return an error.
17007390SMatthew.Ahrens@Sun.COM 			 */
17017390SMatthew.Ahrens@Sun.COM 			if (zfs_prop_readonly(prop) &&
17027390SMatthew.Ahrens@Sun.COM 			    *source && (*source)[0] == '\0') {
17037390SMatthew.Ahrens@Sun.COM 				return (-1);
17047390SMatthew.Ahrens@Sun.COM 			}
17054577Sahrens 			break;
17064577Sahrens 
17074787Sahrens 		case PROP_TYPE_STRING:
17084577Sahrens 		default:
17094577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
17104577Sahrens 			    "cannot get non-numeric property"));
17114577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
17124577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
17134577Sahrens 		}
1714789Sahrens 	}
1715789Sahrens 
1716789Sahrens 	return (0);
1717789Sahrens }
1718789Sahrens 
1719789Sahrens /*
1720789Sahrens  * Calculate the source type, given the raw source string.
1721789Sahrens  */
1722789Sahrens static void
17235094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1724789Sahrens     char *statbuf, size_t statlen)
1725789Sahrens {
17265094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1727789Sahrens 		return;
1728789Sahrens 
1729789Sahrens 	if (source == NULL) {
17305094Slling 		*srctype = ZPROP_SRC_NONE;
1731789Sahrens 	} else if (source[0] == '\0') {
17325094Slling 		*srctype = ZPROP_SRC_DEFAULT;
1733789Sahrens 	} else {
1734789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
17355094Slling 			*srctype = ZPROP_SRC_LOCAL;
1736789Sahrens 		} else {
1737789Sahrens 			(void) strlcpy(statbuf, source, statlen);
17385094Slling 			*srctype = ZPROP_SRC_INHERITED;
1739789Sahrens 		}
1740789Sahrens 	}
1741789Sahrens 
1742789Sahrens }
1743789Sahrens 
1744789Sahrens /*
1745789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
1746789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
1747789Sahrens  * human-readable form.
1748789Sahrens  *
1749789Sahrens  * Returns 0 on success, or -1 on error.
1750789Sahrens  */
1751789Sahrens int
1752789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
17535094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1754789Sahrens {
1755789Sahrens 	char *source = NULL;
1756789Sahrens 	uint64_t val;
1757789Sahrens 	char *str;
17582676Seschrock 	const char *strval;
1759789Sahrens 
1760789Sahrens 	/*
1761789Sahrens 	 * Check to see if this property applies to our object
1762789Sahrens 	 */
1763789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1764789Sahrens 		return (-1);
1765789Sahrens 
1766789Sahrens 	if (src)
17675094Slling 		*src = ZPROP_SRC_NONE;
1768789Sahrens 
1769789Sahrens 	switch (prop) {
1770789Sahrens 	case ZFS_PROP_CREATION:
1771789Sahrens 		/*
1772789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
1773789Sahrens 		 * this into a string unless 'literal' is specified.
1774789Sahrens 		 */
1775789Sahrens 		{
17762885Sahrens 			val = getprop_uint64(zhp, prop, &source);
17772885Sahrens 			time_t time = (time_t)val;
1778789Sahrens 			struct tm t;
1779789Sahrens 
1780789Sahrens 			if (literal ||
1781789Sahrens 			    localtime_r(&time, &t) == NULL ||
1782789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1783789Sahrens 			    &t) == 0)
17842885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
1785789Sahrens 		}
1786789Sahrens 		break;
1787789Sahrens 
1788789Sahrens 	case ZFS_PROP_MOUNTPOINT:
1789789Sahrens 		/*
1790789Sahrens 		 * Getting the precise mountpoint can be tricky.
1791789Sahrens 		 *
1792789Sahrens 		 *  - for 'none' or 'legacy', return those values.
1793789Sahrens 		 *  - for inherited mountpoints, we want to take everything
1794789Sahrens 		 *    after our ancestor and append it to the inherited value.
1795789Sahrens 		 *
1796789Sahrens 		 * If the pool has an alternate root, we want to prepend that
1797789Sahrens 		 * root to any values we return.
1798789Sahrens 		 */
17996865Srm160521 
18001356Seschrock 		str = getprop_string(zhp, prop, &source);
18011356Seschrock 
18026612Sgw25295 		if (str[0] == '/') {
18036865Srm160521 			char buf[MAXPATHLEN];
18046865Srm160521 			char *root = buf;
18051356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
1806789Sahrens 
1807789Sahrens 			if (relpath[0] == '/')
1808789Sahrens 				relpath++;
18096612Sgw25295 
18106865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
18116865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
18126865Srm160521 			    (strcmp(root, "-") == 0))
18136865Srm160521 				root[0] = '\0';
18146612Sgw25295 			/*
18156612Sgw25295 			 * Special case an alternate root of '/'. This will
18166612Sgw25295 			 * avoid having multiple leading slashes in the
18176612Sgw25295 			 * mountpoint path.
18186612Sgw25295 			 */
18196612Sgw25295 			if (strcmp(root, "/") == 0)
18206612Sgw25295 				root++;
18216612Sgw25295 
18226612Sgw25295 			/*
18236612Sgw25295 			 * If the mountpoint is '/' then skip over this
18246612Sgw25295 			 * if we are obtaining either an alternate root or
18256612Sgw25295 			 * an inherited mountpoint.
18266612Sgw25295 			 */
18276612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
18286612Sgw25295 			    relpath[0] != '\0'))
18291356Seschrock 				str++;
1830789Sahrens 
1831789Sahrens 			if (relpath[0] == '\0')
1832789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
18331356Seschrock 				    root, str);
1834789Sahrens 			else
1835789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
18361356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
1837789Sahrens 				    relpath);
1838789Sahrens 		} else {
1839789Sahrens 			/* 'legacy' or 'none' */
18401356Seschrock 			(void) strlcpy(propbuf, str, proplen);
1841789Sahrens 		}
1842789Sahrens 
1843789Sahrens 		break;
1844789Sahrens 
1845789Sahrens 	case ZFS_PROP_ORIGIN:
18462885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1847789Sahrens 		    proplen);
1848789Sahrens 		/*
1849789Sahrens 		 * If there is no parent at all, return failure to indicate that
1850789Sahrens 		 * it doesn't apply to this dataset.
1851789Sahrens 		 */
1852789Sahrens 		if (propbuf[0] == '\0')
1853789Sahrens 			return (-1);
1854789Sahrens 		break;
1855789Sahrens 
1856789Sahrens 	case ZFS_PROP_QUOTA:
18575378Sck153898 	case ZFS_PROP_REFQUOTA:
1858789Sahrens 	case ZFS_PROP_RESERVATION:
18595378Sck153898 	case ZFS_PROP_REFRESERVATION:
18605378Sck153898 
18612082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
18622082Seschrock 			return (-1);
1863789Sahrens 
1864789Sahrens 		/*
1865789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
1866789Sahrens 		 * (unless literal is set), and indicate that it's the default
1867789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
1868789Sahrens 		 * that its set locally.
1869789Sahrens 		 */
1870789Sahrens 		if (val == 0) {
1871789Sahrens 			if (literal)
1872789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
1873789Sahrens 			else
1874789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
1875789Sahrens 		} else {
1876789Sahrens 			if (literal)
18772856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
18783912Slling 				    (u_longlong_t)val);
1879789Sahrens 			else
1880789Sahrens 				zfs_nicenum(val, propbuf, proplen);
1881789Sahrens 		}
1882789Sahrens 		break;
1883789Sahrens 
1884789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
18852082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
18862082Seschrock 			return (-1);
18872856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
18882856Snd150628 		    val / 100, (longlong_t)val % 100);
1889789Sahrens 		break;
1890789Sahrens 
1891789Sahrens 	case ZFS_PROP_TYPE:
1892789Sahrens 		switch (zhp->zfs_type) {
1893789Sahrens 		case ZFS_TYPE_FILESYSTEM:
1894789Sahrens 			str = "filesystem";
1895789Sahrens 			break;
1896789Sahrens 		case ZFS_TYPE_VOLUME:
1897789Sahrens 			str = "volume";
1898789Sahrens 			break;
1899789Sahrens 		case ZFS_TYPE_SNAPSHOT:
1900789Sahrens 			str = "snapshot";
1901789Sahrens 			break;
1902789Sahrens 		default:
19032082Seschrock 			abort();
1904789Sahrens 		}
1905789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
1906789Sahrens 		break;
1907789Sahrens 
1908789Sahrens 	case ZFS_PROP_MOUNTED:
1909789Sahrens 		/*
1910789Sahrens 		 * The 'mounted' property is a pseudo-property that described
1911789Sahrens 		 * whether the filesystem is currently mounted.  Even though
1912789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
1913789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
1914789Sahrens 		 */
19152082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
19162082Seschrock 		    src, &source, &val) != 0)
19172082Seschrock 			return (-1);
19182082Seschrock 		if (val)
1919789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
1920789Sahrens 		else
1921789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
1922789Sahrens 		break;
1923789Sahrens 
1924789Sahrens 	case ZFS_PROP_NAME:
1925789Sahrens 		/*
1926789Sahrens 		 * The 'name' property is a pseudo-property derived from the
1927789Sahrens 		 * dataset name.  It is presented as a real property to simplify
1928789Sahrens 		 * consumers.
1929789Sahrens 		 */
1930789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
1931789Sahrens 		break;
1932789Sahrens 
1933789Sahrens 	default:
19344577Sahrens 		switch (zfs_prop_get_type(prop)) {
19354787Sahrens 		case PROP_TYPE_NUMBER:
19364577Sahrens 			if (get_numeric_property(zhp, prop, src,
19374577Sahrens 			    &source, &val) != 0)
19384577Sahrens 				return (-1);
19394577Sahrens 			if (literal)
19404577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
19414577Sahrens 				    (u_longlong_t)val);
19424577Sahrens 			else
19434577Sahrens 				zfs_nicenum(val, propbuf, proplen);
19444577Sahrens 			break;
19454577Sahrens 
19464787Sahrens 		case PROP_TYPE_STRING:
19474577Sahrens 			(void) strlcpy(propbuf,
19484577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
19494577Sahrens 			break;
19504577Sahrens 
19514787Sahrens 		case PROP_TYPE_INDEX:
19524861Sahrens 			if (get_numeric_property(zhp, prop, src,
19534861Sahrens 			    &source, &val) != 0)
19544861Sahrens 				return (-1);
19554861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
19564577Sahrens 				return (-1);
19574577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
19584577Sahrens 			break;
19594577Sahrens 
19604577Sahrens 		default:
19614577Sahrens 			abort();
19624577Sahrens 		}
1963789Sahrens 	}
1964789Sahrens 
1965789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
1966789Sahrens 
1967789Sahrens 	return (0);
1968789Sahrens }
1969789Sahrens 
1970789Sahrens /*
1971789Sahrens  * Utility function to get the given numeric property.  Does no validation that
1972789Sahrens  * the given property is the appropriate type; should only be used with
1973789Sahrens  * hard-coded property types.
1974789Sahrens  */
1975789Sahrens uint64_t
1976789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1977789Sahrens {
1978789Sahrens 	char *source;
19792082Seschrock 	uint64_t val;
19802082Seschrock 
19815367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
19822082Seschrock 
19832082Seschrock 	return (val);
1984789Sahrens }
1985789Sahrens 
19865713Srm160521 int
19875713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
19885713Srm160521 {
19895713Srm160521 	char buf[64];
19905713Srm160521 
19919396SMatthew.Ahrens@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
19925713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
19935713Srm160521 }
19945713Srm160521 
1995789Sahrens /*
1996789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
1997789Sahrens  */
1998789Sahrens int
1999789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
20005094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2001789Sahrens {
2002789Sahrens 	char *source;
2003789Sahrens 
2004789Sahrens 	/*
2005789Sahrens 	 * Check to see if this property applies to our object
2006789Sahrens 	 */
20074849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
20083237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
20092082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
20102082Seschrock 		    zfs_prop_to_name(prop)));
20114849Sahrens 	}
2012789Sahrens 
2013789Sahrens 	if (src)
20145094Slling 		*src = ZPROP_SRC_NONE;
2015789Sahrens 
20162082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
20172082Seschrock 		return (-1);
2018789Sahrens 
2019789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2020789Sahrens 
2021789Sahrens 	return (0);
2022789Sahrens }
2023789Sahrens 
20249396SMatthew.Ahrens@Sun.COM static int
20259396SMatthew.Ahrens@Sun.COM idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
20269396SMatthew.Ahrens@Sun.COM     char **domainp, idmap_rid_t *ridp)
20279396SMatthew.Ahrens@Sun.COM {
20289396SMatthew.Ahrens@Sun.COM 	idmap_handle_t *idmap_hdl = NULL;
20299396SMatthew.Ahrens@Sun.COM 	idmap_get_handle_t *get_hdl = NULL;
20309396SMatthew.Ahrens@Sun.COM 	idmap_stat status;
20319396SMatthew.Ahrens@Sun.COM 	int err = EINVAL;
20329396SMatthew.Ahrens@Sun.COM 
20339396SMatthew.Ahrens@Sun.COM 	if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
20349396SMatthew.Ahrens@Sun.COM 		goto out;
20359396SMatthew.Ahrens@Sun.COM 	if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
20369396SMatthew.Ahrens@Sun.COM 		goto out;
20379396SMatthew.Ahrens@Sun.COM 
20389396SMatthew.Ahrens@Sun.COM 	if (isuser) {
20399396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbyuid(get_hdl, id,
20409396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
20419396SMatthew.Ahrens@Sun.COM 	} else {
20429396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbygid(get_hdl, id,
20439396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
20449396SMatthew.Ahrens@Sun.COM 	}
20459396SMatthew.Ahrens@Sun.COM 	if (err == IDMAP_SUCCESS &&
20469396SMatthew.Ahrens@Sun.COM 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
20479396SMatthew.Ahrens@Sun.COM 	    status == IDMAP_SUCCESS)
20489396SMatthew.Ahrens@Sun.COM 		err = 0;
20499396SMatthew.Ahrens@Sun.COM 	else
20509396SMatthew.Ahrens@Sun.COM 		err = EINVAL;
20519396SMatthew.Ahrens@Sun.COM out:
20529396SMatthew.Ahrens@Sun.COM 	if (get_hdl)
20539396SMatthew.Ahrens@Sun.COM 		idmap_get_destroy(get_hdl);
20549396SMatthew.Ahrens@Sun.COM 	if (idmap_hdl)
20559396SMatthew.Ahrens@Sun.COM 		(void) idmap_fini(idmap_hdl);
20569396SMatthew.Ahrens@Sun.COM 	return (err);
20579396SMatthew.Ahrens@Sun.COM }
20589396SMatthew.Ahrens@Sun.COM 
20599396SMatthew.Ahrens@Sun.COM /*
20609396SMatthew.Ahrens@Sun.COM  * convert the propname into parameters needed by kernel
20619396SMatthew.Ahrens@Sun.COM  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
20629396SMatthew.Ahrens@Sun.COM  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
20639396SMatthew.Ahrens@Sun.COM  */
20649396SMatthew.Ahrens@Sun.COM static int
20659396SMatthew.Ahrens@Sun.COM userquota_propname_decode(const char *propname, boolean_t zoned,
20669396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
20679396SMatthew.Ahrens@Sun.COM {
20689396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t type;
20699396SMatthew.Ahrens@Sun.COM 	char *cp, *end;
207010160SMatthew.Ahrens@Sun.COM 	char *numericsid = NULL;
20719396SMatthew.Ahrens@Sun.COM 	boolean_t isuser;
20729396SMatthew.Ahrens@Sun.COM 
20739396SMatthew.Ahrens@Sun.COM 	domain[0] = '\0';
20749396SMatthew.Ahrens@Sun.COM 
20759396SMatthew.Ahrens@Sun.COM 	/* Figure out the property type ({user|group}{quota|space}) */
20769396SMatthew.Ahrens@Sun.COM 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
20779396SMatthew.Ahrens@Sun.COM 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
20789396SMatthew.Ahrens@Sun.COM 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
20799396SMatthew.Ahrens@Sun.COM 			break;
20809396SMatthew.Ahrens@Sun.COM 	}
20819396SMatthew.Ahrens@Sun.COM 	if (type == ZFS_NUM_USERQUOTA_PROPS)
20829396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
20839396SMatthew.Ahrens@Sun.COM 	*typep = type;
20849396SMatthew.Ahrens@Sun.COM 
20859396SMatthew.Ahrens@Sun.COM 	isuser = (type == ZFS_PROP_USERQUOTA ||
20869396SMatthew.Ahrens@Sun.COM 	    type == ZFS_PROP_USERUSED);
20879396SMatthew.Ahrens@Sun.COM 
20889396SMatthew.Ahrens@Sun.COM 	cp = strchr(propname, '@') + 1;
20899396SMatthew.Ahrens@Sun.COM 
20909396SMatthew.Ahrens@Sun.COM 	if (strchr(cp, '@')) {
20919396SMatthew.Ahrens@Sun.COM 		/*
20929396SMatthew.Ahrens@Sun.COM 		 * It's a SID name (eg "user@domain") that needs to be
209310160SMatthew.Ahrens@Sun.COM 		 * turned into S-1-domainID-RID.
20949396SMatthew.Ahrens@Sun.COM 		 */
209510160SMatthew.Ahrens@Sun.COM 		directory_error_t e;
20969396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
20979396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
209810160SMatthew.Ahrens@Sun.COM 		if (isuser) {
209910160SMatthew.Ahrens@Sun.COM 			e = directory_sid_from_user_name(NULL,
210010160SMatthew.Ahrens@Sun.COM 			    cp, &numericsid);
210110160SMatthew.Ahrens@Sun.COM 		} else {
210210160SMatthew.Ahrens@Sun.COM 			e = directory_sid_from_group_name(NULL,
210310160SMatthew.Ahrens@Sun.COM 			    cp, &numericsid);
210410160SMatthew.Ahrens@Sun.COM 		}
210510160SMatthew.Ahrens@Sun.COM 		if (e != NULL) {
210610160SMatthew.Ahrens@Sun.COM 			directory_error_free(e);
21079396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
210810160SMatthew.Ahrens@Sun.COM 		}
210910160SMatthew.Ahrens@Sun.COM 		if (numericsid == NULL)
211010160SMatthew.Ahrens@Sun.COM 			return (ENOENT);
211110160SMatthew.Ahrens@Sun.COM 		cp = numericsid;
211210160SMatthew.Ahrens@Sun.COM 		/* will be further decoded below */
211310160SMatthew.Ahrens@Sun.COM 	}
211410160SMatthew.Ahrens@Sun.COM 
211510160SMatthew.Ahrens@Sun.COM 	if (strncmp(cp, "S-1-", 4) == 0) {
21169396SMatthew.Ahrens@Sun.COM 		/* It's a numeric SID (eg "S-1-234-567-89") */
211710160SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domain, cp, domainlen);
21189396SMatthew.Ahrens@Sun.COM 		cp = strrchr(domain, '-');
21199396SMatthew.Ahrens@Sun.COM 		*cp = '\0';
21209396SMatthew.Ahrens@Sun.COM 		cp++;
21219396SMatthew.Ahrens@Sun.COM 
21229396SMatthew.Ahrens@Sun.COM 		errno = 0;
21239396SMatthew.Ahrens@Sun.COM 		*ridp = strtoull(cp, &end, 10);
212410160SMatthew.Ahrens@Sun.COM 		if (numericsid) {
212510160SMatthew.Ahrens@Sun.COM 			free(numericsid);
212610160SMatthew.Ahrens@Sun.COM 			numericsid = NULL;
212710160SMatthew.Ahrens@Sun.COM 		}
21289688SMatthew.Ahrens@Sun.COM 		if (errno != 0 || *end != '\0')
21299396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
21309396SMatthew.Ahrens@Sun.COM 	} else if (!isdigit(*cp)) {
21319396SMatthew.Ahrens@Sun.COM 		/*
21329396SMatthew.Ahrens@Sun.COM 		 * It's a user/group name (eg "user") that needs to be
21339396SMatthew.Ahrens@Sun.COM 		 * turned into a uid/gid
21349396SMatthew.Ahrens@Sun.COM 		 */
21359396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
21369396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
21379396SMatthew.Ahrens@Sun.COM 		if (isuser) {
21389396SMatthew.Ahrens@Sun.COM 			struct passwd *pw;
21399396SMatthew.Ahrens@Sun.COM 			pw = getpwnam(cp);
21409396SMatthew.Ahrens@Sun.COM 			if (pw == NULL)
21419396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
21429396SMatthew.Ahrens@Sun.COM 			*ridp = pw->pw_uid;
21439396SMatthew.Ahrens@Sun.COM 		} else {
21449396SMatthew.Ahrens@Sun.COM 			struct group *gr;
21459396SMatthew.Ahrens@Sun.COM 			gr = getgrnam(cp);
21469396SMatthew.Ahrens@Sun.COM 			if (gr == NULL)
21479396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
21489396SMatthew.Ahrens@Sun.COM 			*ridp = gr->gr_gid;
21499396SMatthew.Ahrens@Sun.COM 		}
21509396SMatthew.Ahrens@Sun.COM 	} else {
21519396SMatthew.Ahrens@Sun.COM 		/* It's a user/group ID (eg "12345"). */
21529396SMatthew.Ahrens@Sun.COM 		uid_t id = strtoul(cp, &end, 10);
21539396SMatthew.Ahrens@Sun.COM 		idmap_rid_t rid;
21549396SMatthew.Ahrens@Sun.COM 		char *mapdomain;
21559396SMatthew.Ahrens@Sun.COM 
21569396SMatthew.Ahrens@Sun.COM 		if (*end != '\0')
21579396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
21589396SMatthew.Ahrens@Sun.COM 		if (id > MAXUID) {
21599396SMatthew.Ahrens@Sun.COM 			/* It's an ephemeral ID. */
21609396SMatthew.Ahrens@Sun.COM 			if (idmap_id_to_numeric_domain_rid(id, isuser,
21619396SMatthew.Ahrens@Sun.COM 			    &mapdomain, &rid) != 0)
21629396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
216310160SMatthew.Ahrens@Sun.COM 			(void) strlcpy(domain, mapdomain, domainlen);
21649396SMatthew.Ahrens@Sun.COM 			*ridp = rid;
21659396SMatthew.Ahrens@Sun.COM 		} else {
21669396SMatthew.Ahrens@Sun.COM 			*ridp = id;
21679396SMatthew.Ahrens@Sun.COM 		}
21689396SMatthew.Ahrens@Sun.COM 	}
21699396SMatthew.Ahrens@Sun.COM 
217010160SMatthew.Ahrens@Sun.COM 	ASSERT3P(numericsid, ==, NULL);
21719396SMatthew.Ahrens@Sun.COM 	return (0);
21729396SMatthew.Ahrens@Sun.COM }
21739396SMatthew.Ahrens@Sun.COM 
21749469SLin.Ling@Sun.COM static int
21759469SLin.Ling@Sun.COM zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
21769469SLin.Ling@Sun.COM     uint64_t *propvalue, zfs_userquota_prop_t *typep)
21779396SMatthew.Ahrens@Sun.COM {
21789396SMatthew.Ahrens@Sun.COM 	int err;
21799396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
21809396SMatthew.Ahrens@Sun.COM 
21819396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21829396SMatthew.Ahrens@Sun.COM 
21839396SMatthew.Ahrens@Sun.COM 	err = userquota_propname_decode(propname,
21849396SMatthew.Ahrens@Sun.COM 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
21859469SLin.Ling@Sun.COM 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
21869469SLin.Ling@Sun.COM 	zc.zc_objset_type = *typep;
21879396SMatthew.Ahrens@Sun.COM 	if (err)
21889396SMatthew.Ahrens@Sun.COM 		return (err);
21899396SMatthew.Ahrens@Sun.COM 
21909396SMatthew.Ahrens@Sun.COM 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
21919396SMatthew.Ahrens@Sun.COM 	if (err)
21929396SMatthew.Ahrens@Sun.COM 		return (err);
21939396SMatthew.Ahrens@Sun.COM 
21949469SLin.Ling@Sun.COM 	*propvalue = zc.zc_cookie;
21959469SLin.Ling@Sun.COM 	return (0);
21969469SLin.Ling@Sun.COM }
21979469SLin.Ling@Sun.COM 
21989469SLin.Ling@Sun.COM int
21999469SLin.Ling@Sun.COM zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
22009469SLin.Ling@Sun.COM     uint64_t *propvalue)
22019469SLin.Ling@Sun.COM {
22029469SLin.Ling@Sun.COM 	zfs_userquota_prop_t type;
22039469SLin.Ling@Sun.COM 
22049469SLin.Ling@Sun.COM 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
22059469SLin.Ling@Sun.COM 	    &type));
22069469SLin.Ling@Sun.COM }
22079469SLin.Ling@Sun.COM 
22089469SLin.Ling@Sun.COM int
22099469SLin.Ling@Sun.COM zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
22109469SLin.Ling@Sun.COM     char *propbuf, int proplen, boolean_t literal)
22119469SLin.Ling@Sun.COM {
22129469SLin.Ling@Sun.COM 	int err;
22139469SLin.Ling@Sun.COM 	uint64_t propvalue;
22149469SLin.Ling@Sun.COM 	zfs_userquota_prop_t type;
22159469SLin.Ling@Sun.COM 
22169469SLin.Ling@Sun.COM 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
22179469SLin.Ling@Sun.COM 	    &type);
22189469SLin.Ling@Sun.COM 
22199469SLin.Ling@Sun.COM 	if (err)
22209469SLin.Ling@Sun.COM 		return (err);
22219469SLin.Ling@Sun.COM 
22229396SMatthew.Ahrens@Sun.COM 	if (literal) {
22239469SLin.Ling@Sun.COM 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
22249469SLin.Ling@Sun.COM 	} else if (propvalue == 0 &&
22259396SMatthew.Ahrens@Sun.COM 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
22269396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(propbuf, "none", proplen);
22279396SMatthew.Ahrens@Sun.COM 	} else {
22289469SLin.Ling@Sun.COM 		zfs_nicenum(propvalue, propbuf, proplen);
22299396SMatthew.Ahrens@Sun.COM 	}
22309396SMatthew.Ahrens@Sun.COM 	return (0);
22319396SMatthew.Ahrens@Sun.COM }
22329396SMatthew.Ahrens@Sun.COM 
2233789Sahrens /*
2234789Sahrens  * Returns the name of the given zfs handle.
2235789Sahrens  */
2236789Sahrens const char *
2237789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2238789Sahrens {
2239789Sahrens 	return (zhp->zfs_name);
2240789Sahrens }
2241789Sahrens 
2242789Sahrens /*
2243789Sahrens  * Returns the type of the given zfs handle.
2244789Sahrens  */
2245789Sahrens zfs_type_t
2246789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2247789Sahrens {
2248789Sahrens 	return (zhp->zfs_type);
2249789Sahrens }
2250789Sahrens 
22518228SEric.Taylor@Sun.COM static int
22528228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
22538228SEric.Taylor@Sun.COM {
22548228SEric.Taylor@Sun.COM 	int rc;
22558228SEric.Taylor@Sun.COM 	uint64_t	orig_cookie;
22568228SEric.Taylor@Sun.COM 
22578228SEric.Taylor@Sun.COM 	orig_cookie = zc->zc_cookie;
22588228SEric.Taylor@Sun.COM top:
22598228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
22608228SEric.Taylor@Sun.COM 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
22618228SEric.Taylor@Sun.COM 
22628228SEric.Taylor@Sun.COM 	if (rc == -1) {
22638228SEric.Taylor@Sun.COM 		switch (errno) {
22648228SEric.Taylor@Sun.COM 		case ENOMEM:
22658228SEric.Taylor@Sun.COM 			/* expand nvlist memory and try again */
22668228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
22678228SEric.Taylor@Sun.COM 				zcmd_free_nvlists(zc);
22688228SEric.Taylor@Sun.COM 				return (-1);
22698228SEric.Taylor@Sun.COM 			}
22708228SEric.Taylor@Sun.COM 			zc->zc_cookie = orig_cookie;
22718228SEric.Taylor@Sun.COM 			goto top;
22728228SEric.Taylor@Sun.COM 		/*
22738228SEric.Taylor@Sun.COM 		 * An errno value of ESRCH indicates normal completion.
22748228SEric.Taylor@Sun.COM 		 * If ENOENT is returned, then the underlying dataset
22758228SEric.Taylor@Sun.COM 		 * has been removed since we obtained the handle.
22768228SEric.Taylor@Sun.COM 		 */
22778228SEric.Taylor@Sun.COM 		case ESRCH:
22788228SEric.Taylor@Sun.COM 		case ENOENT:
22798228SEric.Taylor@Sun.COM 			rc = 1;
22808228SEric.Taylor@Sun.COM 			break;
22818228SEric.Taylor@Sun.COM 		default:
22828228SEric.Taylor@Sun.COM 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
22838228SEric.Taylor@Sun.COM 			    dgettext(TEXT_DOMAIN,
22848228SEric.Taylor@Sun.COM 			    "cannot iterate filesystems"));
22858228SEric.Taylor@Sun.COM 			break;
22868228SEric.Taylor@Sun.COM 		}
22878228SEric.Taylor@Sun.COM 	}
22888228SEric.Taylor@Sun.COM 	return (rc);
22898228SEric.Taylor@Sun.COM }
22908228SEric.Taylor@Sun.COM 
2291789Sahrens /*
22921356Seschrock  * Iterate over all child filesystems
2293789Sahrens  */
2294789Sahrens int
22951356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2296789Sahrens {
2297789Sahrens 	zfs_cmd_t zc = { 0 };
2298789Sahrens 	zfs_handle_t *nzhp;
2299789Sahrens 	int ret;
2300789Sahrens 
23015367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
23025367Sahrens 		return (0);
23035367Sahrens 
23048228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
23058228SEric.Taylor@Sun.COM 		return (-1);
23068228SEric.Taylor@Sun.COM 
23078228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
23088228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
2309789Sahrens 		/*
2310789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2311789Sahrens 		 * that the pool has since been removed.
2312789Sahrens 		 */
23138228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
23148228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2315789Sahrens 			continue;
23168228SEric.Taylor@Sun.COM 		}
23178228SEric.Taylor@Sun.COM 
23188228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
23198228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2320789Sahrens 			return (ret);
23218228SEric.Taylor@Sun.COM 		}
2322789Sahrens 	}
23238228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
23248228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
23251356Seschrock }
23261356Seschrock 
23271356Seschrock /*
23281356Seschrock  * Iterate over all snapshots
23291356Seschrock  */
23301356Seschrock int
23311356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23321356Seschrock {
23331356Seschrock 	zfs_cmd_t zc = { 0 };
23341356Seschrock 	zfs_handle_t *nzhp;
23351356Seschrock 	int ret;
2336789Sahrens 
23375367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
23385367Sahrens 		return (0);
23395367Sahrens 
23408228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
23418228SEric.Taylor@Sun.COM 		return (-1);
23428228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
23438228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
23448228SEric.Taylor@Sun.COM 
23458228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
23468228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2347789Sahrens 			continue;
23488228SEric.Taylor@Sun.COM 		}
23498228SEric.Taylor@Sun.COM 
23508228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
23518228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2352789Sahrens 			return (ret);
23538228SEric.Taylor@Sun.COM 		}
2354789Sahrens 	}
23558228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
23568228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
2357789Sahrens }
2358789Sahrens 
2359789Sahrens /*
23601356Seschrock  * Iterate over all children, snapshots and filesystems
23611356Seschrock  */
23621356Seschrock int
23631356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23641356Seschrock {
23651356Seschrock 	int ret;
23661356Seschrock 
23671356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
23681356Seschrock 		return (ret);
23691356Seschrock 
23701356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
23711356Seschrock }
23721356Seschrock 
23731356Seschrock /*
2374789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2375789Sahrens  * Can return NULL if this is a pool.
2376789Sahrens  */
2377789Sahrens static int
2378789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2379789Sahrens {
2380789Sahrens 	char *loc;
2381789Sahrens 
2382789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2383789Sahrens 		return (-1);
2384789Sahrens 
2385789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2386789Sahrens 	buf[loc - path] = '\0';
2387789Sahrens 
2388789Sahrens 	return (0);
2389789Sahrens }
2390789Sahrens 
2391789Sahrens /*
23924490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
23934490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
23944490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
23954490Svb160487  * length of already existing prefix of the given path.  We also fetch the
23964490Svb160487  * 'zoned' property, which is used to validate property settings when creating
23974490Svb160487  * new datasets.
2398789Sahrens  */
2399789Sahrens static int
24004490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
24014490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2402789Sahrens {
2403789Sahrens 	zfs_cmd_t zc = { 0 };
2404789Sahrens 	char parent[ZFS_MAXNAMELEN];
2405789Sahrens 	char *slash;
24061356Seschrock 	zfs_handle_t *zhp;
24072082Seschrock 	char errbuf[1024];
24082082Seschrock 
24098269SMark.Musante@Sun.COM 	(void) snprintf(errbuf, sizeof (errbuf),
24108269SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2411789Sahrens 
2412789Sahrens 	/* get parent, and check to see if this is just a pool */
2413789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
24142082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24152082Seschrock 		    "missing dataset name"));
24162082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2417789Sahrens 	}
2418789Sahrens 
2419789Sahrens 	/* check to see if the pool exists */
2420789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2421789Sahrens 		slash = parent + strlen(parent);
2422789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2423789Sahrens 	zc.zc_name[slash - parent] = '\0';
24242082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2425789Sahrens 	    errno == ENOENT) {
24262082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24272082Seschrock 		    "no such pool '%s'"), zc.zc_name);
24282082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2429789Sahrens 	}
2430789Sahrens 
2431789Sahrens 	/* check to see if the parent dataset exists */
24324490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
24334490Svb160487 		if (errno == ENOENT && accept_ancestor) {
24344490Svb160487 			/*
24354490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
24364490Svb160487 			 */
24374490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
24384490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24394490Svb160487 				    "no such pool '%s'"), zc.zc_name);
24404490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
24414490Svb160487 			}
24424490Svb160487 		} else if (errno == ENOENT) {
24432082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24442082Seschrock 			    "parent does not exist"));
24452082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
24464490Svb160487 		} else
24472082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2448789Sahrens 	}
2449789Sahrens 
24502676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2451789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
24522676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
24532082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
24541356Seschrock 		zfs_close(zhp);
2455789Sahrens 		return (-1);
2456789Sahrens 	}
2457789Sahrens 
2458789Sahrens 	/* make sure parent is a filesystem */
24591356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
24602082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
24612082Seschrock 		    "parent is not a filesystem"));
24622082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
24631356Seschrock 		zfs_close(zhp);
2464789Sahrens 		return (-1);
2465789Sahrens 	}
2466789Sahrens 
24671356Seschrock 	zfs_close(zhp);
24684490Svb160487 	if (prefixlen != NULL)
24694490Svb160487 		*prefixlen = strlen(parent);
24704490Svb160487 	return (0);
24714490Svb160487 }
24724490Svb160487 
24734490Svb160487 /*
24744490Svb160487  * Finds whether the dataset of the given type(s) exists.
24754490Svb160487  */
24764490Svb160487 boolean_t
24774490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
24784490Svb160487 {
24794490Svb160487 	zfs_handle_t *zhp;
24804490Svb160487 
24815326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
24824490Svb160487 		return (B_FALSE);
24834490Svb160487 
24844490Svb160487 	/*
24854490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
24864490Svb160487 	 */
24874490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
24884490Svb160487 		int ds_type = zhp->zfs_type;
24894490Svb160487 
24904490Svb160487 		zfs_close(zhp);
24914490Svb160487 		if (types & ds_type)
24924490Svb160487 			return (B_TRUE);
24934490Svb160487 	}
24944490Svb160487 	return (B_FALSE);
24954490Svb160487 }
24964490Svb160487 
24974490Svb160487 /*
24985367Sahrens  * Given a path to 'target', create all the ancestors between
24995367Sahrens  * the prefixlen portion of the path, and the target itself.
25005367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
25015367Sahrens  */
25025367Sahrens int
25035367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
25045367Sahrens {
25055367Sahrens 	zfs_handle_t *h;
25065367Sahrens 	char *cp;
25075367Sahrens 	const char *opname;
25085367Sahrens 
25095367Sahrens 	/* make sure prefix exists */
25105367Sahrens 	cp = target + prefixlen;
25115367Sahrens 	if (*cp != '/') {
25125367Sahrens 		assert(strchr(cp, '/') == NULL);
25135367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
25145367Sahrens 	} else {
25155367Sahrens 		*cp = '\0';
25165367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
25175367Sahrens 		*cp = '/';
25185367Sahrens 	}
25195367Sahrens 	if (h == NULL)
25205367Sahrens 		return (-1);
25215367Sahrens 	zfs_close(h);
25225367Sahrens 
25235367Sahrens 	/*
25245367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
25255367Sahrens 	 * up to the prefixlen-long one.
25265367Sahrens 	 */
25275367Sahrens 	for (cp = target + prefixlen + 1;
25285367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
25295367Sahrens 		char *logstr;
25305367Sahrens 
25315367Sahrens 		*cp = '\0';
25325367Sahrens 
25335367Sahrens 		h = make_dataset_handle(hdl, target);
25345367Sahrens 		if (h) {
25355367Sahrens 			/* it already exists, nothing to do here */
25365367Sahrens 			zfs_close(h);
25375367Sahrens 			continue;
25385367Sahrens 		}
25395367Sahrens 
25405367Sahrens 		logstr = hdl->libzfs_log_str;
25415367Sahrens 		hdl->libzfs_log_str = NULL;
25425367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
25435367Sahrens 		    NULL) != 0) {
25445367Sahrens 			hdl->libzfs_log_str = logstr;
25455367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
25465367Sahrens 			goto ancestorerr;
25475367Sahrens 		}
25485367Sahrens 
25495367Sahrens 		hdl->libzfs_log_str = logstr;
25505367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
25515367Sahrens 		if (h == NULL) {
25525367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
25535367Sahrens 			goto ancestorerr;
25545367Sahrens 		}
25555367Sahrens 
25565367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
25575367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
25585367Sahrens 			goto ancestorerr;
25595367Sahrens 		}
25605367Sahrens 
25615367Sahrens 		if (zfs_share(h) != 0) {
25625367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
25635367Sahrens 			goto ancestorerr;
25645367Sahrens 		}
25655367Sahrens 
25665367Sahrens 		zfs_close(h);
25675367Sahrens 	}
25685367Sahrens 
25695367Sahrens 	return (0);
25705367Sahrens 
25715367Sahrens ancestorerr:
25725367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25735367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
25745367Sahrens 	return (-1);
25755367Sahrens }
25765367Sahrens 
25775367Sahrens /*
25784490Svb160487  * Creates non-existing ancestors of the given path.
25794490Svb160487  */
25804490Svb160487 int
25814490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
25824490Svb160487 {
25834490Svb160487 	int prefix;
25844490Svb160487 	uint64_t zoned;
25854490Svb160487 	char *path_copy;
25864490Svb160487 	int rc;
25874490Svb160487 
25884490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
25894490Svb160487 		return (-1);
25904490Svb160487 
25914490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
25924490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
25934490Svb160487 		free(path_copy);
25944490Svb160487 	}
25954490Svb160487 	if (path_copy == NULL || rc != 0)
25964490Svb160487 		return (-1);
25974490Svb160487 
2598789Sahrens 	return (0);
2599789Sahrens }
2600789Sahrens 
2601789Sahrens /*
26022676Seschrock  * Create a new filesystem or volume.
2603789Sahrens  */
2604789Sahrens int
26052082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
26062676Seschrock     nvlist_t *props)
2607789Sahrens {
2608789Sahrens 	zfs_cmd_t zc = { 0 };
2609789Sahrens 	int ret;
2610789Sahrens 	uint64_t size = 0;
2611789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
26122082Seschrock 	char errbuf[1024];
26132676Seschrock 	uint64_t zoned;
26142082Seschrock 
26152082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
26162082Seschrock 	    "cannot create '%s'"), path);
2617789Sahrens 
2618789Sahrens 	/* validate the path, taking care to note the extended error message */
26195326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
26202082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2621789Sahrens 
2622789Sahrens 	/* validate parents exist */
26234490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2624789Sahrens 		return (-1);
2625789Sahrens 
2626789Sahrens 	/*
2627789Sahrens 	 * The failure modes when creating a dataset of a different type over
2628789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2629789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2630789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2631789Sahrens 	 * first try to see if the dataset exists.
2632789Sahrens 	 */
2633789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
26345094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
26352082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26362082Seschrock 		    "dataset already exists"));
26372082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2638789Sahrens 	}
2639789Sahrens 
2640789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2641789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2642789Sahrens 	else
2643789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2644789Sahrens 
26457184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
26463912Slling 	    zoned, NULL, errbuf)) == 0)
26472676Seschrock 		return (-1);
26482676Seschrock 
2649789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
26501133Seschrock 		/*
26511133Seschrock 		 * If we are creating a volume, the size and block size must
26521133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
26531133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
26541133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
26551133Seschrock 		 * zero.
26561133Seschrock 		 */
26572676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
26582676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
26592676Seschrock 			nvlist_free(props);
26602082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26612676Seschrock 			    "missing volume size"));
26622676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2663789Sahrens 		}
2664789Sahrens 
26652676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
26662676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
26672676Seschrock 		    &blocksize)) != 0) {
26682676Seschrock 			if (ret == ENOENT) {
26692676Seschrock 				blocksize = zfs_prop_default_numeric(
26702676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
26712676Seschrock 			} else {
26722676Seschrock 				nvlist_free(props);
26732676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26742676Seschrock 				    "missing volume block size"));
26752676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26762676Seschrock 			}
26772676Seschrock 		}
26782676Seschrock 
26792676Seschrock 		if (size == 0) {
26802676Seschrock 			nvlist_free(props);
26812082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26822676Seschrock 			    "volume size cannot be zero"));
26832676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26841133Seschrock 		}
26851133Seschrock 
26861133Seschrock 		if (size % blocksize != 0) {
26872676Seschrock 			nvlist_free(props);
26882082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26892676Seschrock 			    "volume size must be a multiple of volume block "
26902676Seschrock 			    "size"));
26912676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26921133Seschrock 		}
2693789Sahrens 	}
2694789Sahrens 
26955094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
26962676Seschrock 		return (-1);
26972676Seschrock 	nvlist_free(props);
26982676Seschrock 
2699789Sahrens 	/* create the dataset */
27004543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2701789Sahrens 
27023912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
27032082Seschrock 		ret = zvol_create_link(hdl, path);
27043912Slling 		if (ret) {
27053912Slling 			(void) zfs_standard_error(hdl, errno,
27063912Slling 			    dgettext(TEXT_DOMAIN,
27073912Slling 			    "Volume successfully created, but device links "
27083912Slling 			    "were not created"));
27093912Slling 			zcmd_free_nvlists(&zc);
27103912Slling 			return (-1);
27113912Slling 		}
27123912Slling 	}
2713789Sahrens 
27142676Seschrock 	zcmd_free_nvlists(&zc);
27152676Seschrock 
2716789Sahrens 	/* check for failure */
2717789Sahrens 	if (ret != 0) {
2718789Sahrens 		char parent[ZFS_MAXNAMELEN];
2719789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2720789Sahrens 
2721789Sahrens 		switch (errno) {
2722789Sahrens 		case ENOENT:
27232082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27242082Seschrock 			    "no such parent '%s'"), parent);
27252082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2726789Sahrens 
2727789Sahrens 		case EINVAL:
27282082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27293413Smmusante 			    "parent '%s' is not a filesystem"), parent);
27302082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2731789Sahrens 
2732789Sahrens 		case EDOM:
27332082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27342676Seschrock 			    "volume block size must be power of 2 from "
27352676Seschrock 			    "%u to %uk"),
2736789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2737789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
27382082Seschrock 
27392676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
27402082Seschrock 
27414603Sahrens 		case ENOTSUP:
27424603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27434603Sahrens 			    "pool must be upgraded to set this "
27444603Sahrens 			    "property or value"));
27454603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2746789Sahrens #ifdef _ILP32
2747789Sahrens 		case EOVERFLOW:
2748789Sahrens 			/*
2749789Sahrens 			 * This platform can't address a volume this big.
2750789Sahrens 			 */
27512082Seschrock 			if (type == ZFS_TYPE_VOLUME)
27522082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
27532082Seschrock 				    errbuf));
2754789Sahrens #endif
27552082Seschrock 			/* FALLTHROUGH */
2756789Sahrens 		default:
27572082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2758789Sahrens 		}
2759789Sahrens 	}
2760789Sahrens 
2761789Sahrens 	return (0);
2762789Sahrens }
2763789Sahrens 
2764789Sahrens /*
2765789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2766789Sahrens  * isn't mounted, and that there are no active dependents.
2767789Sahrens  */
2768789Sahrens int
2769*10242Schris.kirby@sun.com zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2770789Sahrens {
2771789Sahrens 	zfs_cmd_t zc = { 0 };
2772789Sahrens 
2773789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2774789Sahrens 
27752676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
27763126Sahl 		/*
27774543Smarks 		 * If user doesn't have permissions to unshare volume, then
27784543Smarks 		 * abort the request.  This would only happen for a
27794543Smarks 		 * non-privileged user.
27803126Sahl 		 */
27814543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
27824543Smarks 			return (-1);
27834543Smarks 		}
27843126Sahl 
27852082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2786789Sahrens 			return (-1);
2787789Sahrens 
2788789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2789789Sahrens 	} else {
2790789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2791789Sahrens 	}
2792789Sahrens 
2793*10242Schris.kirby@sun.com 	zc.zc_defer_destroy = defer;
27944543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
27953237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
27962082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
27972082Seschrock 		    zhp->zfs_name));
27982199Sahrens 	}
2799789Sahrens 
2800789Sahrens 	remove_mountpoint(zhp);
2801789Sahrens 
2802789Sahrens 	return (0);
2803789Sahrens }
2804789Sahrens 
28052199Sahrens struct destroydata {
28062199Sahrens 	char *snapname;
28072199Sahrens 	boolean_t gotone;
28083265Sahrens 	boolean_t closezhp;
28092199Sahrens };
28102199Sahrens 
28112199Sahrens static int
28122199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
28132199Sahrens {
28142199Sahrens 	struct destroydata *dd = arg;
28152199Sahrens 	zfs_handle_t *szhp;
28162199Sahrens 	char name[ZFS_MAXNAMELEN];
28173265Sahrens 	boolean_t closezhp = dd->closezhp;
28183265Sahrens 	int rv;
28192199Sahrens 
28202676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
28212676Seschrock 	(void) strlcat(name, "@", sizeof (name));
28222676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
28232199Sahrens 
28242199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
28252199Sahrens 	if (szhp) {
28262199Sahrens 		dd->gotone = B_TRUE;
28272199Sahrens 		zfs_close(szhp);
28282199Sahrens 	}
28292199Sahrens 
28302199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
28312199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
28322199Sahrens 		/*
28332199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
28342199Sahrens 		 * return an error, because then we wouldn't visit all
28352199Sahrens 		 * the volumes.
28362199Sahrens 		 */
28372199Sahrens 	}
28382199Sahrens 
28393265Sahrens 	dd->closezhp = B_TRUE;
28403265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
28413265Sahrens 	if (closezhp)
28423265Sahrens 		zfs_close(zhp);
28433265Sahrens 	return (rv);
28442199Sahrens }
28452199Sahrens 
28462199Sahrens /*
28472199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
28482199Sahrens  */
28492199Sahrens int
2850*10242Schris.kirby@sun.com zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
28512199Sahrens {
28522199Sahrens 	zfs_cmd_t zc = { 0 };
28532199Sahrens 	int ret;
28542199Sahrens 	struct destroydata dd = { 0 };
28552199Sahrens 
28562199Sahrens 	dd.snapname = snapname;
28572199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
28582199Sahrens 
28592199Sahrens 	if (!dd.gotone) {
28603237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
28612199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
28622199Sahrens 		    zhp->zfs_name, snapname));
28632199Sahrens 	}
28642199Sahrens 
28652199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
28662676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2867*10242Schris.kirby@sun.com 	zc.zc_defer_destroy = defer;
28682199Sahrens 
28694543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
28702199Sahrens 	if (ret != 0) {
28712199Sahrens 		char errbuf[1024];
28722199Sahrens 
28732199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28742199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
28752199Sahrens 
28762199Sahrens 		switch (errno) {
28772199Sahrens 		case EEXIST:
28782199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28792199Sahrens 			    "snapshot is cloned"));
28802199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
28812199Sahrens 
28822199Sahrens 		default:
28832199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
28842199Sahrens 			    errbuf));
28852199Sahrens 		}
28862199Sahrens 	}
28872199Sahrens 
28882199Sahrens 	return (0);
28892199Sahrens }
28902199Sahrens 
2891789Sahrens /*
2892789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
2893789Sahrens  */
2894789Sahrens int
28952676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2896789Sahrens {
2897789Sahrens 	zfs_cmd_t zc = { 0 };
2898789Sahrens 	char parent[ZFS_MAXNAMELEN];
2899789Sahrens 	int ret;
29002082Seschrock 	char errbuf[1024];
29012082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
29022676Seschrock 	zfs_type_t type;
29032676Seschrock 	uint64_t zoned;
2904789Sahrens 
2905789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2906789Sahrens 
29072082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29082082Seschrock 	    "cannot create '%s'"), target);
29092082Seschrock 
2910789Sahrens 	/* validate the target name */
29115326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
29122082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2913789Sahrens 
2914789Sahrens 	/* validate parents exist */
29154490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2916789Sahrens 		return (-1);
2917789Sahrens 
2918789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
2919789Sahrens 
2920789Sahrens 	/* do the clone */
29212676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
2922789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
29232744Snn35248 		type = ZFS_TYPE_VOLUME;
29242676Seschrock 	} else {
2925789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
29262744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
29272676Seschrock 	}
29282676Seschrock 
29292676Seschrock 	if (props) {
29307184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
29317184Stimh 		    zhp, errbuf)) == NULL)
29322676Seschrock 			return (-1);
29332676Seschrock 
29345094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
29352676Seschrock 			nvlist_free(props);
29362676Seschrock 			return (-1);
29372676Seschrock 		}
29382676Seschrock 
29392676Seschrock 		nvlist_free(props);
29402676Seschrock 	}
2941789Sahrens 
2942789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
29432676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
29444543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2945789Sahrens 
29462676Seschrock 	zcmd_free_nvlists(&zc);
29472676Seschrock 
2948789Sahrens 	if (ret != 0) {
2949789Sahrens 		switch (errno) {
2950789Sahrens 
2951789Sahrens 		case ENOENT:
2952789Sahrens 			/*
2953789Sahrens 			 * The parent doesn't exist.  We should have caught this
2954789Sahrens 			 * above, but there may a race condition that has since
2955789Sahrens 			 * destroyed the parent.
2956789Sahrens 			 *
2957789Sahrens 			 * At this point, we don't know whether it's the source
2958789Sahrens 			 * that doesn't exist anymore, or whether the target
2959789Sahrens 			 * dataset doesn't exist.
2960789Sahrens 			 */
29612082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29622082Seschrock 			    "no such parent '%s'"), parent);
29632082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
29642082Seschrock 
29652082Seschrock 		case EXDEV:
29662082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29672082Seschrock 			    "source and target pools differ"));
29682082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
29692082Seschrock 			    errbuf));
29702082Seschrock 
29712082Seschrock 		default:
29722082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
29732082Seschrock 			    errbuf));
29742082Seschrock 		}
29752676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
29762082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
29772082Seschrock 	}
29782082Seschrock 
29792082Seschrock 	return (ret);
29802082Seschrock }
29812082Seschrock 
29822082Seschrock typedef struct promote_data {
29832082Seschrock 	char cb_mountpoint[MAXPATHLEN];
29842082Seschrock 	const char *cb_target;
29852082Seschrock 	const char *cb_errbuf;
29862082Seschrock 	uint64_t cb_pivot_txg;
29872082Seschrock } promote_data_t;
29882082Seschrock 
29892082Seschrock static int
29902082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
29912082Seschrock {
29922082Seschrock 	promote_data_t *pd = data;
29932082Seschrock 	zfs_handle_t *szhp;
29942082Seschrock 	char snapname[MAXPATHLEN];
29953265Sahrens 	int rv = 0;
29962082Seschrock 
29972082Seschrock 	/* 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 		zfs_close(zhp);
30002082Seschrock 		return (0);
30013265Sahrens 	}
30022082Seschrock 
30032417Sahrens 	/* Remove the device link if it's a zvol. */
30042676Seschrock 	if (ZFS_IS_VOLUME(zhp))
30052417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
30062082Seschrock 
30072082Seschrock 	/* Check for conflicting names */
30082676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
30092676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
30102082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
30112082Seschrock 	if (szhp != NULL) {
30122082Seschrock 		zfs_close(szhp);
30132082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
30142082Seschrock 		    "snapshot name '%s' from origin \n"
30152082Seschrock 		    "conflicts with '%s' from target"),
30162082Seschrock 		    zhp->zfs_name, snapname);
30173265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
30182082Seschrock 	}
30193265Sahrens 	zfs_close(zhp);
30203265Sahrens 	return (rv);
30212082Seschrock }
30222082Seschrock 
30232417Sahrens static int
30242417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
30252417Sahrens {
30262417Sahrens 	promote_data_t *pd = data;
30272417Sahrens 
30282417Sahrens 	/* We don't care about snapshots after the pivot point */
30293265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
30303265Sahrens 		/* Create the device link if it's a zvol. */
30313265Sahrens 		if (ZFS_IS_VOLUME(zhp))
30323265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
30333265Sahrens 	}
30343265Sahrens 
30353265Sahrens 	zfs_close(zhp);
30362417Sahrens 	return (0);
30372417Sahrens }
30382417Sahrens 
30392082Seschrock /*
30402082Seschrock  * Promotes the given clone fs to be the clone parent.
30412082Seschrock  */
30422082Seschrock int
30432082Seschrock zfs_promote(zfs_handle_t *zhp)
30442082Seschrock {
30452082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
30462082Seschrock 	zfs_cmd_t zc = { 0 };
30472082Seschrock 	char parent[MAXPATHLEN];
30482082Seschrock 	char *cp;
30492082Seschrock 	int ret;
30502082Seschrock 	zfs_handle_t *pzhp;
30512082Seschrock 	promote_data_t pd;
30522082Seschrock 	char errbuf[1024];
30532082Seschrock 
30542082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30552082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
30562082Seschrock 
30572082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
30582082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30592082Seschrock 		    "snapshots can not be promoted"));
30602082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
30612082Seschrock 	}
30622082Seschrock 
30635367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
30642082Seschrock 	if (parent[0] == '\0') {
30652082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30662082Seschrock 		    "not a cloned filesystem"));
30672082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
30682082Seschrock 	}
30692082Seschrock 	cp = strchr(parent, '@');
30702082Seschrock 	*cp = '\0';
30712082Seschrock 
30722082Seschrock 	/* Walk the snapshots we will be moving */
30735367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
30742082Seschrock 	if (pzhp == NULL)
30752082Seschrock 		return (-1);
30762082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
30772082Seschrock 	zfs_close(pzhp);
30782082Seschrock 	pd.cb_target = zhp->zfs_name;
30792082Seschrock 	pd.cb_errbuf = errbuf;
30805094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
30812082Seschrock 	if (pzhp == NULL)
30822082Seschrock 		return (-1);
30832082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
30842082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
30852082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
30862417Sahrens 	if (ret != 0) {
30872417Sahrens 		zfs_close(pzhp);
30882082Seschrock 		return (-1);
30892417Sahrens 	}
30902082Seschrock 
30912082Seschrock 	/* issue the ioctl */
30925367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
30932676Seschrock 	    sizeof (zc.zc_value));
30942082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
30954543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
30962082Seschrock 
30972082Seschrock 	if (ret != 0) {
30982417Sahrens 		int save_errno = errno;
30992417Sahrens 
31002417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
31012417Sahrens 		zfs_close(pzhp);
31022417Sahrens 
31032417Sahrens 		switch (save_errno) {
3104789Sahrens 		case EEXIST:
3105789Sahrens 			/*
31062082Seschrock 			 * There is a conflicting snapshot name.  We
31072082Seschrock 			 * should have caught this above, but they could
31082082Seschrock 			 * have renamed something in the mean time.
3109789Sahrens 			 */
31102082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31112082Seschrock 			    "conflicting snapshot name from parent '%s'"),
31122082Seschrock 			    parent);
31132082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3114789Sahrens 
3115789Sahrens 		default:
31162417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3117789Sahrens 		}
31182417Sahrens 	} else {
31192417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3120789Sahrens 	}
3121789Sahrens 
31222417Sahrens 	zfs_close(pzhp);
3123789Sahrens 	return (ret);
3124789Sahrens }
3125789Sahrens 
31264007Smmusante struct createdata {
31274007Smmusante 	const char *cd_snapname;
31284007Smmusante 	int cd_ifexists;
31294007Smmusante };
31304007Smmusante 
31312199Sahrens static int
31322199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
31332199Sahrens {
31344007Smmusante 	struct createdata *cd = arg;
31352676Seschrock 	int ret;
31362199Sahrens 
31372199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31382199Sahrens 		char name[MAXPATHLEN];
31392199Sahrens 
31402676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31412676Seschrock 		(void) strlcat(name, "@", sizeof (name));
31424007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
31434007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
31444007Smmusante 		    cd->cd_ifexists);
31452199Sahrens 		/*
31462199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31472199Sahrens 		 * return an error, because then we wouldn't visit all
31482199Sahrens 		 * the volumes.
31492199Sahrens 		 */
31502199Sahrens 	}
31512676Seschrock 
31524007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
31532676Seschrock 
31542676Seschrock 	zfs_close(zhp);
31552676Seschrock 
31562676Seschrock 	return (ret);
31572199Sahrens }
31582199Sahrens 
3159789Sahrens /*
31603504Sahl  * Takes a snapshot of the given dataset.
3161789Sahrens  */
3162789Sahrens int
31637265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
31647265Sahrens     nvlist_t *props)
3165789Sahrens {
3166789Sahrens 	const char *delim;
31677265Sahrens 	char parent[ZFS_MAXNAMELEN];
3168789Sahrens 	zfs_handle_t *zhp;
3169789Sahrens 	zfs_cmd_t zc = { 0 };
3170789Sahrens 	int ret;
31712082Seschrock 	char errbuf[1024];
31722082Seschrock 
31732082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31742082Seschrock 	    "cannot snapshot '%s'"), path);
31752082Seschrock 
31762082Seschrock 	/* validate the target name */
31775326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
31782082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3179789Sahrens 
31807265Sahrens 	if (props) {
31817265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
31827265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
31837265Sahrens 			return (-1);
31847265Sahrens 
31857265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
31867265Sahrens 			nvlist_free(props);
31877265Sahrens 			return (-1);
31887265Sahrens 		}
31897265Sahrens 
31907265Sahrens 		nvlist_free(props);
31917265Sahrens 	}
31927265Sahrens 
3193789Sahrens 	/* make sure the parent exists and is of the appropriate type */
31942199Sahrens 	delim = strchr(path, '@');
3195789Sahrens 	(void) strncpy(parent, path, delim - path);
3196789Sahrens 	parent[delim - path] = '\0';
3197789Sahrens 
31982082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3199789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
32007265Sahrens 		zcmd_free_nvlists(&zc);
3201789Sahrens 		return (-1);
3202789Sahrens 	}
3203789Sahrens 
32042199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
32052676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
32064543Smarks 	if (ZFS_IS_VOLUME(zhp))
32074543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
32084543Smarks 	else
32094543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
32102199Sahrens 	zc.zc_cookie = recursive;
32114543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
32122199Sahrens 
32137265Sahrens 	zcmd_free_nvlists(&zc);
32147265Sahrens 
32152199Sahrens 	/*
32162199Sahrens 	 * if it was recursive, the one that actually failed will be in
32172199Sahrens 	 * zc.zc_name.
32182199Sahrens 	 */
32194543Smarks 	if (ret != 0)
32204543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32214543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
32224543Smarks 
32232199Sahrens 	if (ret == 0 && recursive) {
32244007Smmusante 		struct createdata cd;
32254007Smmusante 
32264007Smmusante 		cd.cd_snapname = delim + 1;
32274007Smmusante 		cd.cd_ifexists = B_FALSE;
32284007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
32292199Sahrens 	}
3230789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
32312082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
32322199Sahrens 		if (ret != 0) {
32334543Smarks 			(void) zfs_standard_error(hdl, errno,
32344543Smarks 			    dgettext(TEXT_DOMAIN,
32354543Smarks 			    "Volume successfully snapshotted, but device links "
32364543Smarks 			    "were not created"));
32374543Smarks 			zfs_close(zhp);
32384543Smarks 			return (-1);
32392199Sahrens 		}
3240789Sahrens 	}
3241789Sahrens 
32422082Seschrock 	if (ret != 0)
32432082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3244789Sahrens 
3245789Sahrens 	zfs_close(zhp);
3246789Sahrens 
3247789Sahrens 	return (ret);
3248789Sahrens }
3249789Sahrens 
3250789Sahrens /*
32511294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
32521294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
32531294Slling  * is a dependent and we should just destroy it without checking the transaction
32541294Slling  * group.
3255789Sahrens  */
32561294Slling typedef struct rollback_data {
32571294Slling 	const char	*cb_target;		/* the snapshot */
32581294Slling 	uint64_t	cb_create;		/* creation time reference */
32595749Sahrens 	boolean_t	cb_error;
32602082Seschrock 	boolean_t	cb_dependent;
32615749Sahrens 	boolean_t	cb_force;
32621294Slling } rollback_data_t;
32631294Slling 
32641294Slling static int
32651294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
32661294Slling {
32671294Slling 	rollback_data_t *cbp = data;
32681294Slling 
32691294Slling 	if (!cbp->cb_dependent) {
32701294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
32711294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
32721294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
32731294Slling 		    cbp->cb_create) {
32744543Smarks 			char *logstr;
32751294Slling 
32762082Seschrock 			cbp->cb_dependent = B_TRUE;
32775446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
32785446Sahrens 			    rollback_destroy, cbp);
32792082Seschrock 			cbp->cb_dependent = B_FALSE;
32801294Slling 
32814543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
32824543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
3283*10242Schris.kirby@sun.com 			cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
32844543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
32851294Slling 		}
32861294Slling 	} else {
32875749Sahrens 		/* We must destroy this clone; first unmount it */
32885749Sahrens 		prop_changelist_t *clp;
32895749Sahrens 
32907366STim.Haley@Sun.COM 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
32915749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
32925749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
32935749Sahrens 			cbp->cb_error = B_TRUE;
32945749Sahrens 			zfs_close(zhp);
32955749Sahrens 			return (0);
32965749Sahrens 		}
3297*10242Schris.kirby@sun.com 		if (zfs_destroy(zhp, B_FALSE) != 0)
32985749Sahrens 			cbp->cb_error = B_TRUE;
32995749Sahrens 		else
33005749Sahrens 			changelist_remove(clp, zhp->zfs_name);
33015751Sahrens 		(void) changelist_postfix(clp);
33025749Sahrens 		changelist_free(clp);
33031294Slling 	}
33041294Slling 
33051294Slling 	zfs_close(zhp);
33061294Slling 	return (0);
33071294Slling }
33081294Slling 
33091294Slling /*
33105446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
33115446Sahrens  * data changes since then and making it the active dataset.
33125446Sahrens  *
33135446Sahrens  * Any snapshots more recent than the target are destroyed, along with
33145446Sahrens  * their dependents.
33151294Slling  */
33165446Sahrens int
33175749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3318789Sahrens {
33195446Sahrens 	rollback_data_t cb = { 0 };
33205446Sahrens 	int err;
3321789Sahrens 	zfs_cmd_t zc = { 0 };
33225713Srm160521 	boolean_t restore_resv = 0;
33235713Srm160521 	uint64_t old_volsize, new_volsize;
33245713Srm160521 	zfs_prop_t resv_prop;
3325789Sahrens 
3326789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3327789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3328789Sahrens 
33295446Sahrens 	/*
33305446Sahrens 	 * Destroy all recent snapshots and its dependends.
33315446Sahrens 	 */
33325749Sahrens 	cb.cb_force = force;
33335446Sahrens 	cb.cb_target = snap->zfs_name;
33345446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
33355446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
33365446Sahrens 
33375749Sahrens 	if (cb.cb_error)
33385749Sahrens 		return (-1);
33395446Sahrens 
33405446Sahrens 	/*
33415446Sahrens 	 * Now that we have verified that the snapshot is the latest,
33425446Sahrens 	 * rollback to the given snapshot.
33435446Sahrens 	 */
33445446Sahrens 
33455713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
33465713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
33475713Srm160521 			return (-1);
33485713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
33495713Srm160521 			return (-1);
33505713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
33515713Srm160521 		restore_resv =
33525713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
33535713Srm160521 	}
3354789Sahrens 
3355789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3356789Sahrens 
33572676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3358789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3359789Sahrens 	else
3360789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3361789Sahrens 
3362789Sahrens 	/*
33635446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
33645446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
33655446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
33665446Sahrens 	 * an unlikely race condition where the user has taken a
33675446Sahrens 	 * snapshot since we verified that this was the most recent.
33685713Srm160521 	 *
3369789Sahrens 	 */
33705446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
33713237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
33722082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
33732082Seschrock 		    zhp->zfs_name);
33745717Srm160521 		return (err);
33755717Srm160521 	}
33765713Srm160521 
33775713Srm160521 	/*
33785713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
33795713Srm160521 	 * rollback reservation and the volsize has changed then set
33805713Srm160521 	 * the reservation property to the post-rollback volsize.
33815713Srm160521 	 * Make a new handle since the rollback closed the dataset.
33825713Srm160521 	 */
33835717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
33845717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
33855717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
33865717Srm160521 			zfs_close(zhp);
33875713Srm160521 			return (err);
33885717Srm160521 		}
33895713Srm160521 		if (restore_resv) {
33905713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
33915713Srm160521 			if (old_volsize != new_volsize)
33925717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
33935717Srm160521 				    new_volsize);
33945713Srm160521 		}
33955713Srm160521 		zfs_close(zhp);
3396789Sahrens 	}
33975446Sahrens 	return (err);
33981294Slling }
33991294Slling 
34001294Slling /*
3401789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3402789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3403789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3404789Sahrens  * libzfs_graph.c.
3405789Sahrens  */
3406789Sahrens int
34072474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
34082474Seschrock     zfs_iter_f func, void *data)
3409789Sahrens {
3410789Sahrens 	char **dependents;
3411789Sahrens 	size_t count;
3412789Sahrens 	int i;
3413789Sahrens 	zfs_handle_t *child;
3414789Sahrens 	int ret = 0;
3415789Sahrens 
34162474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
34172474Seschrock 	    &dependents, &count) != 0)
34182474Seschrock 		return (-1);
34192474Seschrock 
3420789Sahrens 	for (i = 0; i < count; i++) {
34212082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
34222082Seschrock 		    dependents[i])) == NULL)
3423789Sahrens 			continue;
3424789Sahrens 
3425789Sahrens 		if ((ret = func(child, data)) != 0)
3426789Sahrens 			break;
3427789Sahrens 	}
3428789Sahrens 
3429789Sahrens 	for (i = 0; i < count; i++)
3430789Sahrens 		free(dependents[i]);
3431789Sahrens 	free(dependents);
3432789Sahrens 
3433789Sahrens 	return (ret);
3434789Sahrens }
3435789Sahrens 
3436789Sahrens /*
3437789Sahrens  * Renames the given dataset.
3438789Sahrens  */
3439789Sahrens int
34404490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3441789Sahrens {
3442789Sahrens 	int ret;
3443789Sahrens 	zfs_cmd_t zc = { 0 };
3444789Sahrens 	char *delim;
34454007Smmusante 	prop_changelist_t *cl = NULL;
34464007Smmusante 	zfs_handle_t *zhrp = NULL;
34474007Smmusante 	char *parentname = NULL;
3448789Sahrens 	char parent[ZFS_MAXNAMELEN];
34492082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
34502082Seschrock 	char errbuf[1024];
3451789Sahrens 
3452789Sahrens 	/* if we have the same exact name, just return success */
3453789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3454789Sahrens 		return (0);
3455789Sahrens 
34562082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34572082Seschrock 	    "cannot rename to '%s'"), target);
34582082Seschrock 
3459789Sahrens 	/*
3460789Sahrens 	 * Make sure the target name is valid
3461789Sahrens 	 */
3462789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
34632665Snd150628 		if ((strchr(target, '@') == NULL) ||
34642665Snd150628 		    *target == '@') {
34652665Snd150628 			/*
34662665Snd150628 			 * Snapshot target name is abbreviated,
34672665Snd150628 			 * reconstruct full dataset name
34682665Snd150628 			 */
34692665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
34702665Snd150628 			    sizeof (parent));
34712665Snd150628 			delim = strchr(parent, '@');
34722665Snd150628 			if (strchr(target, '@') == NULL)
34732665Snd150628 				*(++delim) = '\0';
34742665Snd150628 			else
34752665Snd150628 				*delim = '\0';
34762665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
34772665Snd150628 			target = parent;
34782665Snd150628 		} else {
34792665Snd150628 			/*
34802665Snd150628 			 * Make sure we're renaming within the same dataset.
34812665Snd150628 			 */
34822665Snd150628 			delim = strchr(target, '@');
34832665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
34842665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
34852665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34862665Snd150628 				    "snapshots must be part of same "
34872665Snd150628 				    "dataset"));
34882665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
34893912Slling 				    errbuf));
34902665Snd150628 			}
3491789Sahrens 		}
34925326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
34932665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3494789Sahrens 	} else {
34954007Smmusante 		if (recursive) {
34964007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34974007Smmusante 			    "recursive rename must be a snapshot"));
34984007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
34994007Smmusante 		}
35004007Smmusante 
35015326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
35022665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35032676Seschrock 		uint64_t unused;
35042676Seschrock 
3505789Sahrens 		/* validate parents */
35064490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3507789Sahrens 			return (-1);
3508789Sahrens 
3509789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3510789Sahrens 
3511789Sahrens 		/* make sure we're in the same pool */
3512789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3513789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3514789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
35152082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35162082Seschrock 			    "datasets must be within same pool"));
35172082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3518789Sahrens 		}
35192440Snd150628 
35202440Snd150628 		/* new name cannot be a child of the current dataset name */
35212440Snd150628 		if (strncmp(parent, zhp->zfs_name,
35223912Slling 		    strlen(zhp->zfs_name)) == 0) {
35232440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35242440Snd150628 			    "New dataset name cannot be a descendent of "
35252440Snd150628 			    "current dataset name"));
35262440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35272440Snd150628 		}
3528789Sahrens 	}
3529789Sahrens 
35302082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
35312082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
35322082Seschrock 
3533789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3534789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
35352082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35362082Seschrock 		    "dataset is used in a non-global zone"));
35372082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3538789Sahrens 	}
3539789Sahrens 
35404007Smmusante 	if (recursive) {
35414007Smmusante 		struct destroydata dd;
35424007Smmusante 
35434183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
35444183Smmusante 		if (parentname == NULL) {
35454183Smmusante 			ret = -1;
35464183Smmusante 			goto error;
35474183Smmusante 		}
35484007Smmusante 		delim = strchr(parentname, '@');
35494007Smmusante 		*delim = '\0';
35505094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
35514007Smmusante 		if (zhrp == NULL) {
35524183Smmusante 			ret = -1;
35534183Smmusante 			goto error;
35544007Smmusante 		}
35554007Smmusante 
35564007Smmusante 		dd.snapname = delim + 1;
35574007Smmusante 		dd.gotone = B_FALSE;
35584183Smmusante 		dd.closezhp = B_TRUE;
35594007Smmusante 
35604007Smmusante 		/* We remove any zvol links prior to renaming them */
35614007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
35624007Smmusante 		if (ret) {
35634007Smmusante 			goto error;
35644007Smmusante 		}
35654007Smmusante 	} else {
35667366STim.Haley@Sun.COM 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
35674007Smmusante 			return (-1);
35684007Smmusante 
35694007Smmusante 		if (changelist_haszonedchild(cl)) {
35704007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35714007Smmusante 			    "child dataset with inherited mountpoint is used "
35724007Smmusante 			    "in a non-global zone"));
35734007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
35744007Smmusante 			goto error;
35754007Smmusante 		}
35764007Smmusante 
35774007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
35784007Smmusante 			goto error;
3579789Sahrens 	}
3580789Sahrens 
35812676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3582789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3583789Sahrens 	else
3584789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3585789Sahrens 
35862665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35872676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
35882665Snd150628 
35894007Smmusante 	zc.zc_cookie = recursive;
35904007Smmusante 
35914543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
35924007Smmusante 		/*
35934007Smmusante 		 * if it was recursive, the one that actually failed will
35944007Smmusante 		 * be in zc.zc_name
35954007Smmusante 		 */
35964007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35975367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
35984007Smmusante 
35994007Smmusante 		if (recursive && errno == EEXIST) {
36004007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36014007Smmusante 			    "a child dataset already has a snapshot "
36024007Smmusante 			    "with the new name"));
36034801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
36044007Smmusante 		} else {
36054007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
36064007Smmusante 		}
3607789Sahrens 
3608789Sahrens 		/*
3609789Sahrens 		 * On failure, we still want to remount any filesystems that
3610789Sahrens 		 * were previously mounted, so we don't alter the system state.
3611789Sahrens 		 */
36124007Smmusante 		if (recursive) {
36134007Smmusante 			struct createdata cd;
36144007Smmusante 
36154007Smmusante 			/* only create links for datasets that had existed */
36164007Smmusante 			cd.cd_snapname = delim + 1;
36174007Smmusante 			cd.cd_ifexists = B_TRUE;
36184007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
36194007Smmusante 			    &cd);
36204007Smmusante 		} else {
36214007Smmusante 			(void) changelist_postfix(cl);
36224007Smmusante 		}
3623789Sahrens 	} else {
36244007Smmusante 		if (recursive) {
36254007Smmusante 			struct createdata cd;
36264007Smmusante 
36274007Smmusante 			/* only create links for datasets that had existed */
36284007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
36294007Smmusante 			cd.cd_ifexists = B_TRUE;
36304007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
36314007Smmusante 			    &cd);
36324007Smmusante 		} else {
36334007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
36344007Smmusante 			ret = changelist_postfix(cl);
36354007Smmusante 		}
3636789Sahrens 	}
3637789Sahrens 
3638789Sahrens error:
36394007Smmusante 	if (parentname) {
36404007Smmusante 		free(parentname);
36414007Smmusante 	}
36424007Smmusante 	if (zhrp) {
36434007Smmusante 		zfs_close(zhrp);
36444007Smmusante 	}
36454007Smmusante 	if (cl) {
36464007Smmusante 		changelist_free(cl);
36474007Smmusante 	}
3648789Sahrens 	return (ret);
3649789Sahrens }
3650789Sahrens 
3651789Sahrens /*
3652789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3653789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3654789Sahrens  */
3655789Sahrens int
36562082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3657789Sahrens {
36584007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
36594007Smmusante }
36604007Smmusante 
36614007Smmusante static int
36624007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
36634007Smmusante {
3664789Sahrens 	zfs_cmd_t zc = { 0 };
36652082Seschrock 	di_devlink_handle_t dhdl;
36664543Smarks 	priv_set_t *priv_effective;
36674543Smarks 	int privileged;
3668789Sahrens 
3669789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3670789Sahrens 
3671789Sahrens 	/*
3672789Sahrens 	 * Issue the appropriate ioctl.
3673789Sahrens 	 */
36742082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3675789Sahrens 		switch (errno) {
3676789Sahrens 		case EEXIST:
3677789Sahrens 			/*
3678789Sahrens 			 * Silently ignore the case where the link already
3679789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3680789Sahrens 			 * times without errors.
3681789Sahrens 			 */
3682789Sahrens 			return (0);
3683789Sahrens 
36844007Smmusante 		case ENOENT:
36854007Smmusante 			/*
36864007Smmusante 			 * Dataset does not exist in the kernel.  If we
36874007Smmusante 			 * don't care (see zfs_rename), then ignore the
36884007Smmusante 			 * error quietly.
36894007Smmusante 			 */
36904007Smmusante 			if (ifexists) {
36914007Smmusante 				return (0);
36924007Smmusante 			}
36934007Smmusante 
36944007Smmusante 			/* FALLTHROUGH */
36954007Smmusante 
3696789Sahrens 		default:
36973237Slling 			return (zfs_standard_error_fmt(hdl, errno,
36982082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
36992082Seschrock 			    "for '%s'"), dataset));
3700789Sahrens 		}
3701789Sahrens 	}
3702789Sahrens 
3703789Sahrens 	/*
37044543Smarks 	 * If privileged call devfsadm and wait for the links to
37054543Smarks 	 * magically appear.
37064543Smarks 	 * Otherwise, print out an informational message.
3707789Sahrens 	 */
37084543Smarks 
37094543Smarks 	priv_effective = priv_allocset();
37104543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
37114543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
37124543Smarks 	priv_freeset(priv_effective);
37134543Smarks 
37144543Smarks 	if (privileged) {
37154543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
37164543Smarks 		    DI_MAKE_LINK)) == NULL) {
37174543Smarks 			zfs_error_aux(hdl, strerror(errno));
37187301SEric.Taylor@Sun.COM 			(void) zfs_error_fmt(hdl, errno,
37194543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
37204543Smarks 			    "for '%s'"), dataset);
37214543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
37224543Smarks 			return (-1);
37234543Smarks 		} else {
37244543Smarks 			(void) di_devlink_fini(&dhdl);
37254543Smarks 		}
3726789Sahrens 	} else {
37274543Smarks 		char pathname[MAXPATHLEN];
37284543Smarks 		struct stat64 statbuf;
37294543Smarks 		int i;
37304543Smarks 
37314543Smarks #define	MAX_WAIT	10
37324543Smarks 
37334543Smarks 		/*
37344543Smarks 		 * This is the poor mans way of waiting for the link
37354543Smarks 		 * to show up.  If after 10 seconds we still don't
37364543Smarks 		 * have it, then print out a message.
37374543Smarks 		 */
37384543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
37394543Smarks 		    dataset);
37404543Smarks 
37414543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
37424543Smarks 			if (stat64(pathname, &statbuf) == 0)
37434543Smarks 				break;
37444543Smarks 			(void) sleep(1);
37454543Smarks 		}
37464543Smarks 		if (i == MAX_WAIT)
37474543Smarks 			(void) printf(gettext("%s may not be immediately "
37484543Smarks 			    "available\n"), pathname);
3749789Sahrens 	}
3750789Sahrens 
3751789Sahrens 	return (0);
3752789Sahrens }
3753789Sahrens 
3754789Sahrens /*
3755789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3756789Sahrens  */
3757789Sahrens int
37582082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3759789Sahrens {
3760789Sahrens 	zfs_cmd_t zc = { 0 };
3761789Sahrens 
3762789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3763789Sahrens 
37642082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3765789Sahrens 		switch (errno) {
3766789Sahrens 		case ENXIO:
3767789Sahrens 			/*
3768789Sahrens 			 * Silently ignore the case where the link no longer
3769789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3770789Sahrens 			 * times without errors.
3771789Sahrens 			 */
3772789Sahrens 			return (0);
3773789Sahrens 
3774789Sahrens 		default:
37753237Slling 			return (zfs_standard_error_fmt(hdl, errno,
37762082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
37772082Seschrock 			    "links for '%s'"), dataset));
3778789Sahrens 		}
3779789Sahrens 	}
3780789Sahrens 
3781789Sahrens 	return (0);
3782789Sahrens }
37832676Seschrock 
37842676Seschrock nvlist_t *
37852676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
37862676Seschrock {
37872676Seschrock 	return (zhp->zfs_user_props);
37882676Seschrock }
37892676Seschrock 
37902676Seschrock /*
37913912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
37923912Slling  * display, and their maximum widths.  This does two main things:
37933912Slling  *
37943912Slling  *      - If this is a list of all properties, then expand the list to include
37953912Slling  *        all native properties, and set a flag so that for each dataset we look
37963912Slling  *        for new unique user properties and add them to the list.
37973912Slling  *
37983912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
37993912Slling  *        so that we can size the column appropriately.
38003912Slling  */
38013912Slling int
38025094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
38033912Slling {
38043912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
38055094Slling 	zprop_list_t *entry;
38065094Slling 	zprop_list_t **last, **start;
38073912Slling 	nvlist_t *userprops, *propval;
38083912Slling 	nvpair_t *elem;
38093912Slling 	char *strval;
38103912Slling 	char buf[ZFS_MAXPROPLEN];
38113912Slling 
38125094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
38133912Slling 		return (-1);
38142676Seschrock 
38152676Seschrock 	userprops = zfs_get_user_props(zhp);
38162676Seschrock 
38172676Seschrock 	entry = *plp;
38182676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
38192676Seschrock 		/*
38202676Seschrock 		 * Go through and add any user properties as necessary.  We
38212676Seschrock 		 * start by incrementing our list pointer to the first
38222676Seschrock 		 * non-native property.
38232676Seschrock 		 */
38242676Seschrock 		start = plp;
38252676Seschrock 		while (*start != NULL) {
38265094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
38272676Seschrock 				break;
38282676Seschrock 			start = &(*start)->pl_next;
38292676Seschrock 		}
38302676Seschrock 
38312676Seschrock 		elem = NULL;
38322676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
38332676Seschrock 			/*
38342676Seschrock 			 * See if we've already found this property in our list.
38352676Seschrock 			 */
38362676Seschrock 			for (last = start; *last != NULL;
38372676Seschrock 			    last = &(*last)->pl_next) {
38382676Seschrock 				if (strcmp((*last)->pl_user_prop,
38392676Seschrock 				    nvpair_name(elem)) == 0)
38402676Seschrock 					break;
38412676Seschrock 			}
38422676Seschrock 
38432676Seschrock 			if (*last == NULL) {
38442676Seschrock 				if ((entry = zfs_alloc(hdl,
38455094Slling 				    sizeof (zprop_list_t))) == NULL ||
38462676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
38472676Seschrock 				    nvpair_name(elem)))) == NULL) {
38482676Seschrock 					free(entry);
38492676Seschrock 					return (-1);
38502676Seschrock 				}
38512676Seschrock 
38525094Slling 				entry->pl_prop = ZPROP_INVAL;
38532676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
38542676Seschrock 				entry->pl_all = B_TRUE;
38552676Seschrock 				*last = entry;
38562676Seschrock 			}
38572676Seschrock 		}
38582676Seschrock 	}
38592676Seschrock 
38602676Seschrock 	/*
38612676Seschrock 	 * Now go through and check the width of any non-fixed columns
38622676Seschrock 	 */
38632676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
38642676Seschrock 		if (entry->pl_fixed)
38652676Seschrock 			continue;
38662676Seschrock 
38675094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
38682676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
38692676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
38702676Seschrock 				if (strlen(buf) > entry->pl_width)
38712676Seschrock 					entry->pl_width = strlen(buf);
38722676Seschrock 			}
38732676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
38742676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
38752676Seschrock 			verify(nvlist_lookup_string(propval,
38765094Slling 			    ZPROP_VALUE, &strval) == 0);
38772676Seschrock 			if (strlen(strval) > entry->pl_width)
38782676Seschrock 				entry->pl_width = strlen(strval);
38792676Seschrock 		}
38802676Seschrock 	}
38812676Seschrock 
38822676Seschrock 	return (0);
38832676Seschrock }
38844543Smarks 
38854543Smarks int
38864543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
38874543Smarks {
38884543Smarks 	zfs_cmd_t zc = { 0 };
38894543Smarks 	nvlist_t *nvp;
38904543Smarks 	gid_t gid;
38914543Smarks 	uid_t uid;
38924543Smarks 	const gid_t *groups;
38934543Smarks 	int group_cnt;
38944543Smarks 	int error;
38954543Smarks 
38964543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
38974543Smarks 		return (no_memory(hdl));
38984543Smarks 
38994543Smarks 	uid = ucred_geteuid(cred);
39004543Smarks 	gid = ucred_getegid(cred);
39014543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
39024543Smarks 
39034543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
39044543Smarks 		return (1);
39054543Smarks 
39064543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
39074543Smarks 		nvlist_free(nvp);
39084543Smarks 		return (1);
39094543Smarks 	}
39104543Smarks 
39114543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
39124543Smarks 		nvlist_free(nvp);
39134543Smarks 		return (1);
39144543Smarks 	}
39154543Smarks 
39164543Smarks 	if (nvlist_add_uint32_array(nvp,
39174543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
39184543Smarks 		nvlist_free(nvp);
39194543Smarks 		return (1);
39204543Smarks 	}
39214543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39224543Smarks 
39235094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
39244543Smarks 		return (-1);
39254543Smarks 
39264543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
39274543Smarks 	nvlist_free(nvp);
39284543Smarks 	return (error);
39294543Smarks }
39304543Smarks 
39314543Smarks int
39324543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
39338845Samw@Sun.COM     char *resource, void *export, void *sharetab,
39348845Samw@Sun.COM     int sharemax, zfs_share_op_t operation)
39354543Smarks {
39364543Smarks 	zfs_cmd_t zc = { 0 };
39374543Smarks 	int error;
39384543Smarks 
39394543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39404543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
39418845Samw@Sun.COM 	if (resource)
39428845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
39434543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
39444543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
39455331Samw 	zc.zc_share.z_sharetype = operation;
39464543Smarks 	zc.zc_share.z_sharemax = sharemax;
39474543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
39484543Smarks 	return (error);
39494543Smarks }
39508802SSanjeev.Bagewadi@Sun.COM 
39518802SSanjeev.Bagewadi@Sun.COM void
39528802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
39538802SSanjeev.Bagewadi@Sun.COM {
39548802SSanjeev.Bagewadi@Sun.COM 	nvpair_t *curr;
39558802SSanjeev.Bagewadi@Sun.COM 
39568802SSanjeev.Bagewadi@Sun.COM 	/*
39578802SSanjeev.Bagewadi@Sun.COM 	 * Keep a reference to the props-table against which we prune the
39588802SSanjeev.Bagewadi@Sun.COM 	 * properties.
39598802SSanjeev.Bagewadi@Sun.COM 	 */
39608802SSanjeev.Bagewadi@Sun.COM 	zhp->zfs_props_table = props;
39618802SSanjeev.Bagewadi@Sun.COM 
39628802SSanjeev.Bagewadi@Sun.COM 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
39638802SSanjeev.Bagewadi@Sun.COM 
39648802SSanjeev.Bagewadi@Sun.COM 	while (curr) {
39658802SSanjeev.Bagewadi@Sun.COM 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
39668802SSanjeev.Bagewadi@Sun.COM 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
39678802SSanjeev.Bagewadi@Sun.COM 
39689396SMatthew.Ahrens@Sun.COM 		/*
39699396SMatthew.Ahrens@Sun.COM 		 * We leave user:props in the nvlist, so there will be
39709396SMatthew.Ahrens@Sun.COM 		 * some ZPROP_INVAL.  To be extra safe, don't prune
39719396SMatthew.Ahrens@Sun.COM 		 * those.
39729396SMatthew.Ahrens@Sun.COM 		 */
39739396SMatthew.Ahrens@Sun.COM 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
39748802SSanjeev.Bagewadi@Sun.COM 			(void) nvlist_remove(zhp->zfs_props,
39758802SSanjeev.Bagewadi@Sun.COM 			    nvpair_name(curr), nvpair_type(curr));
39768802SSanjeev.Bagewadi@Sun.COM 		curr = next;
39778802SSanjeev.Bagewadi@Sun.COM 	}
39788802SSanjeev.Bagewadi@Sun.COM }
39798845Samw@Sun.COM 
39808845Samw@Sun.COM static int
39818845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
39828845Samw@Sun.COM     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
39838845Samw@Sun.COM {
39848845Samw@Sun.COM 	zfs_cmd_t zc = { 0 };
39858845Samw@Sun.COM 	nvlist_t *nvlist = NULL;
39868845Samw@Sun.COM 	int error;
39878845Samw@Sun.COM 
39888845Samw@Sun.COM 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39898845Samw@Sun.COM 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
39908845Samw@Sun.COM 	zc.zc_cookie = (uint64_t)cmd;
39918845Samw@Sun.COM 
39928845Samw@Sun.COM 	if (cmd == ZFS_SMB_ACL_RENAME) {
39938845Samw@Sun.COM 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
39948845Samw@Sun.COM 			(void) no_memory(hdl);
39958845Samw@Sun.COM 			return (NULL);
39968845Samw@Sun.COM 		}
39978845Samw@Sun.COM 	}
39988845Samw@Sun.COM 
39998845Samw@Sun.COM 	switch (cmd) {
40008845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
40018845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
40028845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
40038845Samw@Sun.COM 		break;
40048845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
40058845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
40068845Samw@Sun.COM 		    resource1) != 0) {
40078845Samw@Sun.COM 				(void) no_memory(hdl);
40088845Samw@Sun.COM 				return (-1);
40098845Samw@Sun.COM 		}
40108845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
40118845Samw@Sun.COM 		    resource2) != 0) {
40128845Samw@Sun.COM 				(void) no_memory(hdl);
40138845Samw@Sun.COM 				return (-1);
40148845Samw@Sun.COM 		}
40158845Samw@Sun.COM 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
40168845Samw@Sun.COM 			nvlist_free(nvlist);
40178845Samw@Sun.COM 			return (-1);
40188845Samw@Sun.COM 		}
40198845Samw@Sun.COM 		break;
40208845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
40218845Samw@Sun.COM 		break;
40228845Samw@Sun.COM 	default:
40238845Samw@Sun.COM 		return (-1);
40248845Samw@Sun.COM 	}
40258845Samw@Sun.COM 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
40268845Samw@Sun.COM 	if (nvlist)
40278845Samw@Sun.COM 		nvlist_free(nvlist);
40288845Samw@Sun.COM 	return (error);
40298845Samw@Sun.COM }
40308845Samw@Sun.COM 
40318845Samw@Sun.COM int
40328845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
40338845Samw@Sun.COM     char *path, char *resource)
40348845Samw@Sun.COM {
40358845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
40368845Samw@Sun.COM 	    resource, NULL));
40378845Samw@Sun.COM }
40388845Samw@Sun.COM 
40398845Samw@Sun.COM int
40408845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
40418845Samw@Sun.COM     char *path, char *resource)
40428845Samw@Sun.COM {
40438845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
40448845Samw@Sun.COM 	    resource, NULL));
40458845Samw@Sun.COM }
40468845Samw@Sun.COM 
40478845Samw@Sun.COM int
40488845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
40498845Samw@Sun.COM {
40508845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
40518845Samw@Sun.COM 	    NULL, NULL));
40528845Samw@Sun.COM }
40538845Samw@Sun.COM 
40548845Samw@Sun.COM int
40558845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
40568845Samw@Sun.COM     char *oldname, char *newname)
40578845Samw@Sun.COM {
40588845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
40598845Samw@Sun.COM 	    oldname, newname));
40608845Samw@Sun.COM }
40619396SMatthew.Ahrens@Sun.COM 
40629396SMatthew.Ahrens@Sun.COM int
40639396SMatthew.Ahrens@Sun.COM zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
40649396SMatthew.Ahrens@Sun.COM     zfs_userspace_cb_t func, void *arg)
40659396SMatthew.Ahrens@Sun.COM {
40669396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
40679396SMatthew.Ahrens@Sun.COM 	int error;
40689396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t buf[100];
40699396SMatthew.Ahrens@Sun.COM 
40709396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
40719396SMatthew.Ahrens@Sun.COM 
40729396SMatthew.Ahrens@Sun.COM 	zc.zc_objset_type = type;
40739396SMatthew.Ahrens@Sun.COM 	zc.zc_nvlist_dst = (uintptr_t)buf;
40749396SMatthew.Ahrens@Sun.COM 
40759396SMatthew.Ahrens@Sun.COM 	/* CONSTCOND */
40769396SMatthew.Ahrens@Sun.COM 	while (1) {
40779396SMatthew.Ahrens@Sun.COM 		zfs_useracct_t *zua = buf;
40789396SMatthew.Ahrens@Sun.COM 
40799396SMatthew.Ahrens@Sun.COM 		zc.zc_nvlist_dst_size = sizeof (buf);
40809396SMatthew.Ahrens@Sun.COM 		error = ioctl(zhp->zfs_hdl->libzfs_fd,
40819396SMatthew.Ahrens@Sun.COM 		    ZFS_IOC_USERSPACE_MANY, &zc);
40829396SMatthew.Ahrens@Sun.COM 		if (error || zc.zc_nvlist_dst_size == 0)
40839396SMatthew.Ahrens@Sun.COM 			break;
40849396SMatthew.Ahrens@Sun.COM 
40859396SMatthew.Ahrens@Sun.COM 		while (zc.zc_nvlist_dst_size > 0) {
40869554SMatthew.Ahrens@Sun.COM 			error = func(arg, zua->zu_domain, zua->zu_rid,
40879554SMatthew.Ahrens@Sun.COM 			    zua->zu_space);
40889554SMatthew.Ahrens@Sun.COM 			if (error != 0)
40899554SMatthew.Ahrens@Sun.COM 				return (error);
40909396SMatthew.Ahrens@Sun.COM 			zua++;
40919396SMatthew.Ahrens@Sun.COM 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
40929396SMatthew.Ahrens@Sun.COM 		}
40939396SMatthew.Ahrens@Sun.COM 	}
40949396SMatthew.Ahrens@Sun.COM 
40959396SMatthew.Ahrens@Sun.COM 	return (error);
40969396SMatthew.Ahrens@Sun.COM }
4097*10242Schris.kirby@sun.com 
4098*10242Schris.kirby@sun.com int
4099*10242Schris.kirby@sun.com zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4100*10242Schris.kirby@sun.com     boolean_t recursive)
4101*10242Schris.kirby@sun.com {
4102*10242Schris.kirby@sun.com 	zfs_cmd_t zc = { 0 };
4103*10242Schris.kirby@sun.com 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4104*10242Schris.kirby@sun.com 
4105*10242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4106*10242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4107*10242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_string, tag, sizeof (zc.zc_string));
4108*10242Schris.kirby@sun.com 	zc.zc_cookie = recursive;
4109*10242Schris.kirby@sun.com 
4110*10242Schris.kirby@sun.com 	if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4111*10242Schris.kirby@sun.com 		char errbuf[ZFS_MAXNAMELEN+32];
4112*10242Schris.kirby@sun.com 
4113*10242Schris.kirby@sun.com 		/*
4114*10242Schris.kirby@sun.com 		 * if it was recursive, the one that actually failed will be in
4115*10242Schris.kirby@sun.com 		 * zc.zc_name.
4116*10242Schris.kirby@sun.com 		 */
4117*10242Schris.kirby@sun.com 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4118*10242Schris.kirby@sun.com 		    "cannot hold '%s@%s'"), zc.zc_name, snapname);
4119*10242Schris.kirby@sun.com 		switch (errno) {
4120*10242Schris.kirby@sun.com 		case ENOTSUP:
4121*10242Schris.kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4122*10242Schris.kirby@sun.com 			    "pool must be upgraded"));
4123*10242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4124*10242Schris.kirby@sun.com 		case EINVAL:
4125*10242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4126*10242Schris.kirby@sun.com 		case EEXIST:
4127*10242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4128*10242Schris.kirby@sun.com 		default:
4129*10242Schris.kirby@sun.com 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4130*10242Schris.kirby@sun.com 		}
4131*10242Schris.kirby@sun.com 	}
4132*10242Schris.kirby@sun.com 
4133*10242Schris.kirby@sun.com 	return (0);
4134*10242Schris.kirby@sun.com }
4135*10242Schris.kirby@sun.com 
4136*10242Schris.kirby@sun.com int
4137*10242Schris.kirby@sun.com zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4138*10242Schris.kirby@sun.com     boolean_t recursive)
4139*10242Schris.kirby@sun.com {
4140*10242Schris.kirby@sun.com 	zfs_cmd_t zc = { 0 };
4141*10242Schris.kirby@sun.com 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4142*10242Schris.kirby@sun.com 
4143*10242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4144*10242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4145*10242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_string, tag, sizeof (zc.zc_string));
4146*10242Schris.kirby@sun.com 	zc.zc_cookie = recursive;
4147*10242Schris.kirby@sun.com 
4148*10242Schris.kirby@sun.com 	if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4149*10242Schris.kirby@sun.com 		char errbuf[ZFS_MAXNAMELEN+32];
4150*10242Schris.kirby@sun.com 
4151*10242Schris.kirby@sun.com 		/*
4152*10242Schris.kirby@sun.com 		 * if it was recursive, the one that actually failed will be in
4153*10242Schris.kirby@sun.com 		 * zc.zc_name.
4154*10242Schris.kirby@sun.com 		 */
4155*10242Schris.kirby@sun.com 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4156*10242Schris.kirby@sun.com 		    "cannot release '%s@%s'"), zc.zc_name, snapname);
4157*10242Schris.kirby@sun.com 		switch (errno) {
4158*10242Schris.kirby@sun.com 		case ESRCH:
4159*10242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4160*10242Schris.kirby@sun.com 		case ENOTSUP:
4161*10242Schris.kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4162*10242Schris.kirby@sun.com 			    "pool must be upgraded"));
4163*10242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4164*10242Schris.kirby@sun.com 		case EINVAL:
4165*10242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4166*10242Schris.kirby@sun.com 		default:
4167*10242Schris.kirby@sun.com 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
4168*10242Schris.kirby@sun.com 		}
4169*10242Schris.kirby@sun.com 	}
4170*10242Schris.kirby@sun.com 
4171*10242Schris.kirby@sun.com 	return (0);
4172*10242Schris.kirby@sun.com }
4173