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 {
404*10298SMatthew.Ahrens@Sun.COM 	if (put_stats_zhdl(zhp, zc) != 0)
4058228SEric.Taylor@Sun.COM 		return (-1);
4061758Sahrens 
407789Sahrens 	/*
408789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
409789Sahrens 	 * the high-level type.
410789Sahrens 	 */
4112885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4122885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4132885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4142885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4152885Sahrens 	else
4162885Sahrens 		abort();
4172885Sahrens 
418789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
419789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
420789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
421789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
422789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
423789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
424789Sahrens 	else
4252082Seschrock 		abort();	/* we should never see any other types */
426789Sahrens 
4276865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
4288228SEric.Taylor@Sun.COM 	return (0);
4298228SEric.Taylor@Sun.COM }
4308228SEric.Taylor@Sun.COM 
4318228SEric.Taylor@Sun.COM zfs_handle_t *
4328228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path)
4338228SEric.Taylor@Sun.COM {
4348228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
4358228SEric.Taylor@Sun.COM 
4368228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4378228SEric.Taylor@Sun.COM 
4388228SEric.Taylor@Sun.COM 	if (zhp == NULL)
4398228SEric.Taylor@Sun.COM 		return (NULL);
4408228SEric.Taylor@Sun.COM 
4418228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
4428228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
4438228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
4448228SEric.Taylor@Sun.COM 		free(zhp);
4458228SEric.Taylor@Sun.COM 		return (NULL);
4468228SEric.Taylor@Sun.COM 	}
4478228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) == -1) {
4488228SEric.Taylor@Sun.COM 		zcmd_free_nvlists(&zc);
4498228SEric.Taylor@Sun.COM 		free(zhp);
4508228SEric.Taylor@Sun.COM 		return (NULL);
4518228SEric.Taylor@Sun.COM 	}
4528228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, &zc) == -1) {
4538228SEric.Taylor@Sun.COM 		free(zhp);
4548228SEric.Taylor@Sun.COM 		zhp = NULL;
4558228SEric.Taylor@Sun.COM 	}
4568228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
4578228SEric.Taylor@Sun.COM 	return (zhp);
4588228SEric.Taylor@Sun.COM }
4598228SEric.Taylor@Sun.COM 
4608228SEric.Taylor@Sun.COM static zfs_handle_t *
4618228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
4628228SEric.Taylor@Sun.COM {
4638228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4648228SEric.Taylor@Sun.COM 
4658228SEric.Taylor@Sun.COM 	if (zhp == NULL)
4668228SEric.Taylor@Sun.COM 		return (NULL);
4678228SEric.Taylor@Sun.COM 
4688228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
4698228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
4708228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, zc) == -1) {
4718228SEric.Taylor@Sun.COM 		free(zhp);
4728228SEric.Taylor@Sun.COM 		return (NULL);
4738228SEric.Taylor@Sun.COM 	}
474789Sahrens 	return (zhp);
475789Sahrens }
476789Sahrens 
477789Sahrens /*
478789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
479789Sahrens  * argument is a mask of acceptable types.  The function will print an
480789Sahrens  * appropriate error message and return NULL if it can't be opened.
481789Sahrens  */
482789Sahrens zfs_handle_t *
4832082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
484789Sahrens {
485789Sahrens 	zfs_handle_t *zhp;
4862082Seschrock 	char errbuf[1024];
4872082Seschrock 
4882082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4892082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
490789Sahrens 
491789Sahrens 	/*
4922082Seschrock 	 * Validate the name before we even try to open it.
493789Sahrens 	 */
4945326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4962082Seschrock 		    "invalid dataset name"));
4972082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
498789Sahrens 		return (NULL);
499789Sahrens 	}
500789Sahrens 
501789Sahrens 	/*
502789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
503789Sahrens 	 */
504789Sahrens 	errno = 0;
5052082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5063237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
507789Sahrens 		return (NULL);
508789Sahrens 	}
509789Sahrens 
510789Sahrens 	if (!(types & zhp->zfs_type)) {
5112082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5122142Seschrock 		zfs_close(zhp);
513789Sahrens 		return (NULL);
514789Sahrens 	}
515789Sahrens 
516789Sahrens 	return (zhp);
517789Sahrens }
518789Sahrens 
519789Sahrens /*
520789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
521789Sahrens  */
522789Sahrens void
523789Sahrens zfs_close(zfs_handle_t *zhp)
524789Sahrens {
525789Sahrens 	if (zhp->zfs_mntopts)
526789Sahrens 		free(zhp->zfs_mntopts);
5272676Seschrock 	nvlist_free(zhp->zfs_props);
5282676Seschrock 	nvlist_free(zhp->zfs_user_props);
529789Sahrens 	free(zhp);
530789Sahrens }
531789Sahrens 
5328228SEric.Taylor@Sun.COM typedef struct mnttab_node {
5338228SEric.Taylor@Sun.COM 	struct mnttab mtn_mt;
5348228SEric.Taylor@Sun.COM 	avl_node_t mtn_node;
5358228SEric.Taylor@Sun.COM } mnttab_node_t;
5368228SEric.Taylor@Sun.COM 
5378228SEric.Taylor@Sun.COM static int
5388228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
5398228SEric.Taylor@Sun.COM {
5408228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn1 = arg1;
5418228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn2 = arg2;
5428228SEric.Taylor@Sun.COM 	int rv;
5438228SEric.Taylor@Sun.COM 
5448228SEric.Taylor@Sun.COM 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
5458228SEric.Taylor@Sun.COM 
5468228SEric.Taylor@Sun.COM 	if (rv == 0)
5478228SEric.Taylor@Sun.COM 		return (0);
5488228SEric.Taylor@Sun.COM 	return (rv > 0 ? 1 : -1);
5498228SEric.Taylor@Sun.COM }
5508228SEric.Taylor@Sun.COM 
5518228SEric.Taylor@Sun.COM void
5528228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl)
5538228SEric.Taylor@Sun.COM {
5548228SEric.Taylor@Sun.COM 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
5558228SEric.Taylor@Sun.COM 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
5568228SEric.Taylor@Sun.COM 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
5578811SEric.Taylor@Sun.COM }
5588811SEric.Taylor@Sun.COM 
5598811SEric.Taylor@Sun.COM void
5608811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl)
5618811SEric.Taylor@Sun.COM {
5628811SEric.Taylor@Sun.COM 	struct mnttab entry;
5638228SEric.Taylor@Sun.COM 
5648228SEric.Taylor@Sun.COM 	rewind(hdl->libzfs_mnttab);
5658228SEric.Taylor@Sun.COM 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
5668228SEric.Taylor@Sun.COM 		mnttab_node_t *mtn;
5678228SEric.Taylor@Sun.COM 
5688228SEric.Taylor@Sun.COM 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
5698228SEric.Taylor@Sun.COM 			continue;
5708228SEric.Taylor@Sun.COM 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
5718228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
5728228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
5738228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
5748228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
5758228SEric.Taylor@Sun.COM 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
5768228SEric.Taylor@Sun.COM 	}
5778228SEric.Taylor@Sun.COM }
5788228SEric.Taylor@Sun.COM 
5798228SEric.Taylor@Sun.COM void
5808228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl)
5818228SEric.Taylor@Sun.COM {
5828228SEric.Taylor@Sun.COM 	void *cookie = NULL;
5838228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
5848228SEric.Taylor@Sun.COM 
5858228SEric.Taylor@Sun.COM 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
5868228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_special);
5878228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mountp);
5888228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_fstype);
5898228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mntopts);
5908228SEric.Taylor@Sun.COM 		free(mtn);
5918228SEric.Taylor@Sun.COM 	}
5928228SEric.Taylor@Sun.COM 	avl_destroy(&hdl->libzfs_mnttab_cache);
5938228SEric.Taylor@Sun.COM }
5948228SEric.Taylor@Sun.COM 
5958811SEric.Taylor@Sun.COM void
5968811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
5978811SEric.Taylor@Sun.COM {
5988811SEric.Taylor@Sun.COM 	hdl->libzfs_mnttab_enable = enable;
5998811SEric.Taylor@Sun.COM }
6008811SEric.Taylor@Sun.COM 
6018228SEric.Taylor@Sun.COM int
6028228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
6038228SEric.Taylor@Sun.COM     struct mnttab *entry)
6048228SEric.Taylor@Sun.COM {
6058228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6068228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6078228SEric.Taylor@Sun.COM 
6088811SEric.Taylor@Sun.COM 	if (!hdl->libzfs_mnttab_enable) {
6098811SEric.Taylor@Sun.COM 		struct mnttab srch = { 0 };
6108811SEric.Taylor@Sun.COM 
6118811SEric.Taylor@Sun.COM 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
6128811SEric.Taylor@Sun.COM 			libzfs_mnttab_fini(hdl);
6138811SEric.Taylor@Sun.COM 		rewind(hdl->libzfs_mnttab);
6148811SEric.Taylor@Sun.COM 		srch.mnt_special = (char *)fsname;
6158811SEric.Taylor@Sun.COM 		srch.mnt_fstype = MNTTYPE_ZFS;
6168811SEric.Taylor@Sun.COM 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
6178811SEric.Taylor@Sun.COM 			return (0);
6188811SEric.Taylor@Sun.COM 		else
6198811SEric.Taylor@Sun.COM 			return (ENOENT);
6208811SEric.Taylor@Sun.COM 	}
6218811SEric.Taylor@Sun.COM 
6228228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6238811SEric.Taylor@Sun.COM 		libzfs_mnttab_update(hdl);
6248228SEric.Taylor@Sun.COM 
6258228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6268228SEric.Taylor@Sun.COM 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
6278228SEric.Taylor@Sun.COM 	if (mtn) {
6288228SEric.Taylor@Sun.COM 		*entry = mtn->mtn_mt;
6298228SEric.Taylor@Sun.COM 		return (0);
6308228SEric.Taylor@Sun.COM 	}
6318228SEric.Taylor@Sun.COM 	return (ENOENT);
6328228SEric.Taylor@Sun.COM }
6338228SEric.Taylor@Sun.COM 
6348228SEric.Taylor@Sun.COM void
6358228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
6368228SEric.Taylor@Sun.COM     const char *mountp, const char *mntopts)
6378228SEric.Taylor@Sun.COM {
6388228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6398228SEric.Taylor@Sun.COM 
6408228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6418228SEric.Taylor@Sun.COM 		return;
6428228SEric.Taylor@Sun.COM 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
6438228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
6448228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
6458228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
6468228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
6478228SEric.Taylor@Sun.COM 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
6488228SEric.Taylor@Sun.COM }
6498228SEric.Taylor@Sun.COM 
6508228SEric.Taylor@Sun.COM void
6518228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
6528228SEric.Taylor@Sun.COM {
6538228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6548228SEric.Taylor@Sun.COM 	mnttab_node_t *ret;
6558228SEric.Taylor@Sun.COM 
6568228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6578228SEric.Taylor@Sun.COM 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
6588228SEric.Taylor@Sun.COM 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
6598228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_special);
6608228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mountp);
6618228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_fstype);
6628228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mntopts);
6638228SEric.Taylor@Sun.COM 		free(ret);
6648228SEric.Taylor@Sun.COM 	}
6658228SEric.Taylor@Sun.COM }
6668228SEric.Taylor@Sun.COM 
6675713Srm160521 int
6685713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
6695713Srm160521 {
6706865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
6716865Srm160521 
6725713Srm160521 	if (zpool_handle == NULL)
6735713Srm160521 		return (-1);
6745713Srm160521 
6755713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
6765713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
6775713Srm160521 	return (0);
6785713Srm160521 }
6795713Srm160521 
6805713Srm160521 /*
6815713Srm160521  * The choice of reservation property depends on the SPA version.
6825713Srm160521  */
6835713Srm160521 static int
6845713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
6855713Srm160521 {
6865713Srm160521 	int spa_version;
6875713Srm160521 
6885713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
6895713Srm160521 		return (-1);
6905713Srm160521 
6915713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
6925713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
6935713Srm160521 	else
6945713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
6955713Srm160521 
6965713Srm160521 	return (0);
6975713Srm160521 }
6985713Srm160521 
6993912Slling /*
7002676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
7012676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
7022676Seschrock  * strings.
703789Sahrens  */
7047184Stimh nvlist_t *
7057184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
7065094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
707789Sahrens {
7082676Seschrock 	nvpair_t *elem;
7092676Seschrock 	uint64_t intval;
7102676Seschrock 	char *strval;
7115094Slling 	zfs_prop_t prop;
7122676Seschrock 	nvlist_t *ret;
7135331Samw 	int chosen_normal = -1;
7145331Samw 	int chosen_utf = -1;
7152676Seschrock 
7165094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
7175094Slling 		(void) no_memory(hdl);
7185094Slling 		return (NULL);
719789Sahrens 	}
720789Sahrens 
7219396SMatthew.Ahrens@Sun.COM 	/*
7229396SMatthew.Ahrens@Sun.COM 	 * Make sure this property is valid and applies to this type.
7239396SMatthew.Ahrens@Sun.COM 	 */
7249396SMatthew.Ahrens@Sun.COM 
7252676Seschrock 	elem = NULL;
7262676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
7275094Slling 		const char *propname = nvpair_name(elem);
7282676Seschrock 
7299396SMatthew.Ahrens@Sun.COM 		prop = zfs_name_to_prop(propname);
7309396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
7315094Slling 			/*
7329396SMatthew.Ahrens@Sun.COM 			 * This is a user property: make sure it's a
7335094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
7345094Slling 			 */
7355094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
7365094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7375094Slling 				    "'%s' must be a string"), propname);
7385094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7395094Slling 				goto error;
7405094Slling 			}
7415094Slling 
7425094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
7435094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7445094Slling 				    "property name '%s' is too long"),
7452676Seschrock 				    propname);
7462676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7472676Seschrock 				goto error;
7482676Seschrock 			}
7492676Seschrock 
7502676Seschrock 			(void) nvpair_value_string(elem, &strval);
7512676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
7522676Seschrock 				(void) no_memory(hdl);
7532676Seschrock 				goto error;
7542676Seschrock 			}
7552676Seschrock 			continue;
756789Sahrens 		}
7572676Seschrock 
7589396SMatthew.Ahrens@Sun.COM 		/*
7599396SMatthew.Ahrens@Sun.COM 		 * Currently, only user properties can be modified on
7609396SMatthew.Ahrens@Sun.COM 		 * snapshots.
7619396SMatthew.Ahrens@Sun.COM 		 */
7627265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
7637265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7647265Sahrens 			    "this property can not be modified for snapshots"));
7657265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
7667265Sahrens 			goto error;
7677265Sahrens 		}
7687265Sahrens 
7699396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
7709396SMatthew.Ahrens@Sun.COM 			zfs_userquota_prop_t uqtype;
7719396SMatthew.Ahrens@Sun.COM 			char newpropname[128];
7729396SMatthew.Ahrens@Sun.COM 			char domain[128];
7739396SMatthew.Ahrens@Sun.COM 			uint64_t rid;
7749396SMatthew.Ahrens@Sun.COM 			uint64_t valary[3];
7759396SMatthew.Ahrens@Sun.COM 
7769396SMatthew.Ahrens@Sun.COM 			if (userquota_propname_decode(propname, zoned,
7779396SMatthew.Ahrens@Sun.COM 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
7789396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
7799396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN,
7809396SMatthew.Ahrens@Sun.COM 				    "'%s' has an invalid user/group name"),
7819396SMatthew.Ahrens@Sun.COM 				    propname);
7829396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7839396SMatthew.Ahrens@Sun.COM 				goto error;
7849396SMatthew.Ahrens@Sun.COM 			}
7859396SMatthew.Ahrens@Sun.COM 
7869396SMatthew.Ahrens@Sun.COM 			if (uqtype != ZFS_PROP_USERQUOTA &&
7879396SMatthew.Ahrens@Sun.COM 			    uqtype != ZFS_PROP_GROUPQUOTA) {
7889396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
7899396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
7909396SMatthew.Ahrens@Sun.COM 				    propname);
7919396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
7929396SMatthew.Ahrens@Sun.COM 				    errbuf);
7939396SMatthew.Ahrens@Sun.COM 				goto error;
7949396SMatthew.Ahrens@Sun.COM 			}
7959396SMatthew.Ahrens@Sun.COM 
7969396SMatthew.Ahrens@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
7979396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_string(elem, &strval);
7989396SMatthew.Ahrens@Sun.COM 				if (strcmp(strval, "none") == 0) {
7999396SMatthew.Ahrens@Sun.COM 					intval = 0;
8009396SMatthew.Ahrens@Sun.COM 				} else if (zfs_nicestrtonum(hdl,
8019396SMatthew.Ahrens@Sun.COM 				    strval, &intval) != 0) {
8029396SMatthew.Ahrens@Sun.COM 					(void) zfs_error(hdl,
8039396SMatthew.Ahrens@Sun.COM 					    EZFS_BADPROP, errbuf);
8049396SMatthew.Ahrens@Sun.COM 					goto error;
8059396SMatthew.Ahrens@Sun.COM 				}
8069396SMatthew.Ahrens@Sun.COM 			} else if (nvpair_type(elem) ==
8079396SMatthew.Ahrens@Sun.COM 			    DATA_TYPE_UINT64) {
8089396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_uint64(elem, &intval);
8099396SMatthew.Ahrens@Sun.COM 				if (intval == 0) {
8109396SMatthew.Ahrens@Sun.COM 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8119396SMatthew.Ahrens@Sun.COM 					    "use 'none' to disable "
8129396SMatthew.Ahrens@Sun.COM 					    "userquota/groupquota"));
8139396SMatthew.Ahrens@Sun.COM 					goto error;
8149396SMatthew.Ahrens@Sun.COM 				}
8159396SMatthew.Ahrens@Sun.COM 			} else {
8169396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8179396SMatthew.Ahrens@Sun.COM 				    "'%s' must be a number"), propname);
8189396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8199396SMatthew.Ahrens@Sun.COM 				goto error;
8209396SMatthew.Ahrens@Sun.COM 			}
8219396SMatthew.Ahrens@Sun.COM 
8229396SMatthew.Ahrens@Sun.COM 			(void) snprintf(newpropname, sizeof (newpropname),
8239396SMatthew.Ahrens@Sun.COM 			    "%s%s", zfs_userquota_prop_prefixes[uqtype],
8249396SMatthew.Ahrens@Sun.COM 			    domain);
8259396SMatthew.Ahrens@Sun.COM 			valary[0] = uqtype;
8269396SMatthew.Ahrens@Sun.COM 			valary[1] = rid;
8279396SMatthew.Ahrens@Sun.COM 			valary[2] = intval;
8289396SMatthew.Ahrens@Sun.COM 			if (nvlist_add_uint64_array(ret, newpropname,
8299396SMatthew.Ahrens@Sun.COM 			    valary, 3) != 0) {
8309396SMatthew.Ahrens@Sun.COM 				(void) no_memory(hdl);
8319396SMatthew.Ahrens@Sun.COM 				goto error;
8329396SMatthew.Ahrens@Sun.COM 			}
8339396SMatthew.Ahrens@Sun.COM 			continue;
8349396SMatthew.Ahrens@Sun.COM 		}
8359396SMatthew.Ahrens@Sun.COM 
8369396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL) {
8379396SMatthew.Ahrens@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8389396SMatthew.Ahrens@Sun.COM 			    "invalid property '%s'"), propname);
8399396SMatthew.Ahrens@Sun.COM 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8409396SMatthew.Ahrens@Sun.COM 			goto error;
8419396SMatthew.Ahrens@Sun.COM 		}
8429396SMatthew.Ahrens@Sun.COM 
8432676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
8442676Seschrock 			zfs_error_aux(hdl,
8452676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
8462676Seschrock 			    "apply to datasets of this type"), propname);
8472676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8482676Seschrock 			goto error;
8492676Seschrock 		}
8502676Seschrock 
8512676Seschrock 		if (zfs_prop_readonly(prop) &&
8525331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
8532676Seschrock 			zfs_error_aux(hdl,
8542676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
8552676Seschrock 			    propname);
8562676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
8572676Seschrock 			goto error;
8582676Seschrock 		}
8592676Seschrock 
8605094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
8615094Slling 		    &strval, &intval, errbuf) != 0)
8622676Seschrock 			goto error;
8632676Seschrock 
8642676Seschrock 		/*
8652676Seschrock 		 * Perform some additional checks for specific properties.
8662676Seschrock 		 */
8672676Seschrock 		switch (prop) {
8684577Sahrens 		case ZFS_PROP_VERSION:
8694577Sahrens 		{
8704577Sahrens 			int version;
8714577Sahrens 
8724577Sahrens 			if (zhp == NULL)
8734577Sahrens 				break;
8744577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
8754577Sahrens 			if (intval < version) {
8764577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8774577Sahrens 				    "Can not downgrade; already at version %u"),
8784577Sahrens 				    version);
8794577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8804577Sahrens 				goto error;
8814577Sahrens 			}
8824577Sahrens 			break;
8834577Sahrens 		}
8844577Sahrens 
8852676Seschrock 		case ZFS_PROP_RECORDSIZE:
8862676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
8872676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
8882676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
8892676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
8902082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8912676Seschrock 				    "'%s' must be power of 2 from %u "
8922676Seschrock 				    "to %uk"), propname,
8932676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
8942676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
8952676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8962676Seschrock 				goto error;
897789Sahrens 			}
898789Sahrens 			break;
899789Sahrens 
9003126Sahl 		case ZFS_PROP_SHAREISCSI:
9013126Sahl 			if (strcmp(strval, "off") != 0 &&
9023126Sahl 			    strcmp(strval, "on") != 0 &&
9033126Sahl 			    strcmp(strval, "type=disk") != 0) {
9043126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9053126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
9063126Sahl 				    propname);
9073126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9083126Sahl 				goto error;
9093126Sahl 			}
9103126Sahl 
9113126Sahl 			break;
9123126Sahl 
9132676Seschrock 		case ZFS_PROP_MOUNTPOINT:
9144778Srm160521 		{
9154778Srm160521 			namecheck_err_t why;
9164778Srm160521 
9172676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
9182676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9192676Seschrock 				break;
9202676Seschrock 
9214778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
9224778Srm160521 				switch (why) {
9234778Srm160521 				case NAME_ERR_LEADING_SLASH:
9244778Srm160521 					zfs_error_aux(hdl,
9254778Srm160521 					    dgettext(TEXT_DOMAIN,
9264778Srm160521 					    "'%s' must be an absolute path, "
9274778Srm160521 					    "'none', or 'legacy'"), propname);
9284778Srm160521 					break;
9294778Srm160521 				case NAME_ERR_TOOLONG:
9304778Srm160521 					zfs_error_aux(hdl,
9314778Srm160521 					    dgettext(TEXT_DOMAIN,
9324778Srm160521 					    "component of '%s' is too long"),
9334778Srm160521 					    propname);
9344778Srm160521 					break;
9354778Srm160521 				}
9362676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9372676Seschrock 				goto error;
938789Sahrens 			}
9394778Srm160521 		}
9404778Srm160521 
9413126Sahl 			/*FALLTHRU*/
9423126Sahl 
9435331Samw 		case ZFS_PROP_SHARESMB:
9443126Sahl 		case ZFS_PROP_SHARENFS:
9453126Sahl 			/*
9465331Samw 			 * For the mountpoint and sharenfs or sharesmb
9475331Samw 			 * properties, check if it can be set in a
9485331Samw 			 * global/non-global zone based on
9493126Sahl 			 * the zoned property value:
9503126Sahl 			 *
9513126Sahl 			 *		global zone	    non-global zone
9523126Sahl 			 * --------------------------------------------------
9533126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
9543126Sahl 			 *		sharenfs (no)	    sharenfs (no)
9555331Samw 			 *		sharesmb (no)	    sharesmb (no)
9563126Sahl 			 *
9573126Sahl 			 * zoned=off	mountpoint (yes)	N/A
9583126Sahl 			 *		sharenfs (yes)
9595331Samw 			 *		sharesmb (yes)
9603126Sahl 			 */
9612676Seschrock 			if (zoned) {
9622676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
9632676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9642676Seschrock 					    "'%s' cannot be set on "
9652676Seschrock 					    "dataset in a non-global zone"),
9662676Seschrock 					    propname);
9672676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9682676Seschrock 					    errbuf);
9692676Seschrock 					goto error;
9705331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
9715331Samw 				    prop == ZFS_PROP_SHARESMB) {
9722676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9732676Seschrock 					    "'%s' cannot be set in "
9742676Seschrock 					    "a non-global zone"), propname);
9752676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9762676Seschrock 					    errbuf);
9772676Seschrock 					goto error;
9782676Seschrock 				}
9792676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
9802676Seschrock 				/*
9812676Seschrock 				 * If zoned property is 'off', this must be in
9829396SMatthew.Ahrens@Sun.COM 				 * a global zone. If not, something is wrong.
9832676Seschrock 				 */
9842676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9852676Seschrock 				    "'%s' cannot be set while dataset "
9862676Seschrock 				    "'zoned' property is set"), propname);
9872676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
9882676Seschrock 				goto error;
9892676Seschrock 			}
9903126Sahl 
9914180Sdougm 			/*
9924180Sdougm 			 * At this point, it is legitimate to set the
9934180Sdougm 			 * property. Now we want to make sure that the
9944180Sdougm 			 * property value is valid if it is sharenfs.
9954180Sdougm 			 */
9965331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
9975331Samw 			    prop == ZFS_PROP_SHARESMB) &&
9984217Seschrock 			    strcmp(strval, "on") != 0 &&
9994217Seschrock 			    strcmp(strval, "off") != 0) {
10005331Samw 				zfs_share_proto_t proto;
10015331Samw 
10025331Samw 				if (prop == ZFS_PROP_SHARESMB)
10035331Samw 					proto = PROTO_SMB;
10045331Samw 				else
10055331Samw 					proto = PROTO_NFS;
10064180Sdougm 
10074180Sdougm 				/*
10085331Samw 				 * Must be an valid sharing protocol
10095331Samw 				 * option string so init the libshare
10105331Samw 				 * in order to enable the parser and
10115331Samw 				 * then parse the options. We use the
10125331Samw 				 * control API since we don't care about
10135331Samw 				 * the current configuration and don't
10144180Sdougm 				 * want the overhead of loading it
10154180Sdougm 				 * until we actually do something.
10164180Sdougm 				 */
10174180Sdougm 
10184217Seschrock 				if (zfs_init_libshare(hdl,
10194217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
10204217Seschrock 					/*
10214217Seschrock 					 * An error occurred so we can't do
10224217Seschrock 					 * anything
10234217Seschrock 					 */
10244217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10254217Seschrock 					    "'%s' cannot be set: problem "
10264217Seschrock 					    "in share initialization"),
10274217Seschrock 					    propname);
10284217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10294217Seschrock 					    errbuf);
10304217Seschrock 					goto error;
10314217Seschrock 				}
10324217Seschrock 
10335331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
10344217Seschrock 					/*
10354217Seschrock 					 * There was an error in parsing so
10364217Seschrock 					 * deal with it by issuing an error
10374217Seschrock 					 * message and leaving after
10384217Seschrock 					 * uninitializing the the libshare
10394217Seschrock 					 * interface.
10404217Seschrock 					 */
10414217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10424217Seschrock 					    "'%s' cannot be set to invalid "
10434217Seschrock 					    "options"), propname);
10444217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10454217Seschrock 					    errbuf);
10464217Seschrock 					zfs_uninit_libshare(hdl);
10474217Seschrock 					goto error;
10484217Seschrock 				}
10494180Sdougm 				zfs_uninit_libshare(hdl);
10504180Sdougm 			}
10514180Sdougm 
10523126Sahl 			break;
10535331Samw 		case ZFS_PROP_UTF8ONLY:
10545331Samw 			chosen_utf = (int)intval;
10555331Samw 			break;
10565331Samw 		case ZFS_PROP_NORMALIZE:
10575331Samw 			chosen_normal = (int)intval;
10585331Samw 			break;
10592676Seschrock 		}
10602676Seschrock 
10612676Seschrock 		/*
10622676Seschrock 		 * For changes to existing volumes, we have some additional
10632676Seschrock 		 * checks to enforce.
10642676Seschrock 		 */
10652676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
10662676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
10672676Seschrock 			    ZFS_PROP_VOLSIZE);
10682676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
10692676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
10702676Seschrock 			char buf[64];
10712676Seschrock 
10722676Seschrock 			switch (prop) {
10732676Seschrock 			case ZFS_PROP_RESERVATION:
10745378Sck153898 			case ZFS_PROP_REFRESERVATION:
10752676Seschrock 				if (intval > volsize) {
10762676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10772676Seschrock 					    "'%s' is greater than current "
10782676Seschrock 					    "volume size"), propname);
10792676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10802676Seschrock 					    errbuf);
10812676Seschrock 					goto error;
10822676Seschrock 				}
10832676Seschrock 				break;
10842676Seschrock 
10852676Seschrock 			case ZFS_PROP_VOLSIZE:
10862676Seschrock 				if (intval % blocksize != 0) {
10872676Seschrock 					zfs_nicenum(blocksize, buf,
10882676Seschrock 					    sizeof (buf));
10892676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10902676Seschrock 					    "'%s' must be a multiple of "
10912676Seschrock 					    "volume block size (%s)"),
10922676Seschrock 					    propname, buf);
10932676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10942676Seschrock 					    errbuf);
10952676Seschrock 					goto error;
10962676Seschrock 				}
10972676Seschrock 
10982676Seschrock 				if (intval == 0) {
10992676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11002676Seschrock 					    "'%s' cannot be zero"),
11012676Seschrock 					    propname);
11022676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
11032676Seschrock 					    errbuf);
11042676Seschrock 					goto error;
1105789Sahrens 				}
11063126Sahl 				break;
1107789Sahrens 			}
1108789Sahrens 		}
1109789Sahrens 	}
1110789Sahrens 
11112676Seschrock 	/*
11125331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
11135331Samw 	 * enforce rejection of non-UTF8 names.
11145331Samw 	 *
11155331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
11165331Samw 	 * was explicitly not chosen, it is an error.
11175331Samw 	 */
11185498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
11195331Samw 		if (nvlist_add_uint64(ret,
11205331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
11215331Samw 			(void) no_memory(hdl);
11225331Samw 			goto error;
11235331Samw 		}
11245498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
11255331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11265331Samw 		    "'%s' must be set 'on' if normalization chosen"),
11275331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
11285331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11295331Samw 		goto error;
11305331Samw 	}
11315331Samw 
11325331Samw 	/*
11332676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
11342676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
11352676Seschrock 	 */
11362676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
11372676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
11382676Seschrock 	    &intval) == 0) {
11392676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
11402676Seschrock 		    ZFS_PROP_VOLSIZE);
11415481Sck153898 		uint64_t old_reservation;
11422676Seschrock 		uint64_t new_reservation;
11435481Sck153898 		zfs_prop_t resv_prop;
11445713Srm160521 
11455713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
11465481Sck153898 			goto error;
11475481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
11482676Seschrock 
11492676Seschrock 		if (old_volsize == old_reservation &&
11505481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
11512676Seschrock 		    &new_reservation) != 0) {
11522676Seschrock 			if (nvlist_add_uint64(ret,
11535481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
11542676Seschrock 				(void) no_memory(hdl);
11552676Seschrock 				goto error;
11562676Seschrock 			}
11572676Seschrock 		}
11582676Seschrock 	}
11592676Seschrock 	return (ret);
11602676Seschrock 
11612676Seschrock error:
11622676Seschrock 	nvlist_free(ret);
11632676Seschrock 	return (NULL);
1164789Sahrens }
1165789Sahrens 
1166789Sahrens /*
1167789Sahrens  * Given a property name and value, set the property for the given dataset.
1168789Sahrens  */
1169789Sahrens int
11702676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1171789Sahrens {
1172789Sahrens 	zfs_cmd_t zc = { 0 };
11732676Seschrock 	int ret = -1;
11742676Seschrock 	prop_changelist_t *cl = NULL;
11752082Seschrock 	char errbuf[1024];
11762082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
11772676Seschrock 	nvlist_t *nvl = NULL, *realprops;
11782676Seschrock 	zfs_prop_t prop;
11797509SMark.Musante@Sun.COM 	boolean_t do_prefix;
11807509SMark.Musante@Sun.COM 	uint64_t idx;
11812082Seschrock 
11822082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
11832676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
11842082Seschrock 	    zhp->zfs_name);
11852082Seschrock 
11862676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
11872676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
11882676Seschrock 		(void) no_memory(hdl);
11892676Seschrock 		goto error;
1190789Sahrens 	}
1191789Sahrens 
11927184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
11932676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
11942676Seschrock 		goto error;
11955094Slling 
11962676Seschrock 	nvlist_free(nvl);
11972676Seschrock 	nvl = realprops;
11982676Seschrock 
11992676Seschrock 	prop = zfs_name_to_prop(propname);
12002676Seschrock 
12017366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
12022676Seschrock 		goto error;
1203789Sahrens 
1204789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
12052082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12062082Seschrock 		    "child dataset with inherited mountpoint is used "
12072082Seschrock 		    "in a non-global zone"));
12082082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1209789Sahrens 		goto error;
1210789Sahrens 	}
1211789Sahrens 
12127509SMark.Musante@Sun.COM 	/*
12137509SMark.Musante@Sun.COM 	 * If the dataset's canmount property is being set to noauto,
12147509SMark.Musante@Sun.COM 	 * then we want to prevent unmounting & remounting it.
12157509SMark.Musante@Sun.COM 	 */
12167509SMark.Musante@Sun.COM 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
12177509SMark.Musante@Sun.COM 	    (zprop_string_to_index(prop, propval, &idx,
12187509SMark.Musante@Sun.COM 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
12196168Shs24103 
12206168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
12217509SMark.Musante@Sun.COM 		goto error;
1222789Sahrens 
1223789Sahrens 	/*
1224789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1225789Sahrens 	 */
1226789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1227789Sahrens 
12285094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
12292676Seschrock 		goto error;
12302676Seschrock 
12314543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
12328845Samw@Sun.COM 
1233789Sahrens 	if (ret != 0) {
1234789Sahrens 		switch (errno) {
1235789Sahrens 
1236789Sahrens 		case ENOSPC:
1237789Sahrens 			/*
1238789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1239789Sahrens 			 * something different; setting a quota or reservation
1240789Sahrens 			 * doesn't use any disk space.
1241789Sahrens 			 */
1242789Sahrens 			switch (prop) {
1243789Sahrens 			case ZFS_PROP_QUOTA:
12445378Sck153898 			case ZFS_PROP_REFQUOTA:
12452082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12462082Seschrock 				    "size is less than current used or "
12472082Seschrock 				    "reserved space"));
12482082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1249789Sahrens 				break;
1250789Sahrens 
1251789Sahrens 			case ZFS_PROP_RESERVATION:
12525378Sck153898 			case ZFS_PROP_REFRESERVATION:
12532082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12542082Seschrock 				    "size is greater than available space"));
12552082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1256789Sahrens 				break;
1257789Sahrens 
1258789Sahrens 			default:
12592082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1260789Sahrens 				break;
1261789Sahrens 			}
1262789Sahrens 			break;
1263789Sahrens 
1264789Sahrens 		case EBUSY:
12652082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
12662082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
12672082Seschrock 			else
12682676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1269789Sahrens 			break;
1270789Sahrens 
12711175Slling 		case EROFS:
12722082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
12731175Slling 			break;
12741175Slling 
12753886Sahl 		case ENOTSUP:
12763886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12775977Smarks 			    "pool and or dataset must be upgraded to set this "
12784603Sahrens 			    "property or value"));
12793886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
12803886Sahl 			break;
12813886Sahl 
12827042Sgw25295 		case ERANGE:
12837042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
12847042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12857042Sgw25295 				    "property setting is not allowed on "
12867042Sgw25295 				    "bootable datasets"));
12877042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
12887042Sgw25295 			} else {
12897042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
12907042Sgw25295 			}
12917042Sgw25295 			break;
12927042Sgw25295 
1293789Sahrens 		case EOVERFLOW:
1294789Sahrens 			/*
1295789Sahrens 			 * This platform can't address a volume this big.
1296789Sahrens 			 */
1297789Sahrens #ifdef _ILP32
1298789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
12992082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1300789Sahrens 				break;
1301789Sahrens 			}
1302789Sahrens #endif
13032082Seschrock 			/* FALLTHROUGH */
1304789Sahrens 		default:
13052082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1306789Sahrens 		}
1307789Sahrens 	} else {
13086168Shs24103 		if (do_prefix)
13096168Shs24103 			ret = changelist_postfix(cl);
13106168Shs24103 
1311789Sahrens 		/*
1312789Sahrens 		 * Refresh the statistics so the new property value
1313789Sahrens 		 * is reflected.
1314789Sahrens 		 */
13156168Shs24103 		if (ret == 0)
13162676Seschrock 			(void) get_stats(zhp);
1317789Sahrens 	}
1318789Sahrens 
1319789Sahrens error:
13202676Seschrock 	nvlist_free(nvl);
13212676Seschrock 	zcmd_free_nvlists(&zc);
13222676Seschrock 	if (cl)
13232676Seschrock 		changelist_free(cl);
1324789Sahrens 	return (ret);
1325789Sahrens }
1326789Sahrens 
1327789Sahrens /*
1328789Sahrens  * Given a property, inherit the value from the parent dataset.
1329789Sahrens  */
1330789Sahrens int
13312676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1332789Sahrens {
1333789Sahrens 	zfs_cmd_t zc = { 0 };
1334789Sahrens 	int ret;
1335789Sahrens 	prop_changelist_t *cl;
13362082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
13372082Seschrock 	char errbuf[1024];
13382676Seschrock 	zfs_prop_t prop;
13392082Seschrock 
13402082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
13412082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1342789Sahrens 
13435094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
13442676Seschrock 		/*
13452676Seschrock 		 * For user properties, the amount of work we have to do is very
13462676Seschrock 		 * small, so just do it here.
13472676Seschrock 		 */
13482676Seschrock 		if (!zfs_prop_user(propname)) {
13492676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13502676Seschrock 			    "invalid property"));
13512676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
13522676Seschrock 		}
13532676Seschrock 
13542676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13552676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
13562676Seschrock 
13574849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
13582676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
13592676Seschrock 
13602676Seschrock 		return (0);
13612676Seschrock 	}
13622676Seschrock 
1363789Sahrens 	/*
1364789Sahrens 	 * Verify that this property is inheritable.
1365789Sahrens 	 */
13662082Seschrock 	if (zfs_prop_readonly(prop))
13672082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
13682082Seschrock 
13692082Seschrock 	if (!zfs_prop_inheritable(prop))
13702082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1371789Sahrens 
1372789Sahrens 	/*
1373789Sahrens 	 * Check to see if the value applies to this type
1374789Sahrens 	 */
13752082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
13762082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1377789Sahrens 
13783443Srm160521 	/*
13793443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
13803443Srm160521 	 */
13813443Srm160521 	propname = zfs_prop_to_name(prop);
1382789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13832676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1384789Sahrens 
1385789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1386789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
13872082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13882082Seschrock 		    "dataset is used in a non-global zone"));
13892082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1390789Sahrens 	}
1391789Sahrens 
1392789Sahrens 	/*
1393789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1394789Sahrens 	 */
13957366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1396789Sahrens 		return (-1);
1397789Sahrens 
1398789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
13992082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14002082Seschrock 		    "child dataset with inherited mountpoint is used "
14012082Seschrock 		    "in a non-global zone"));
14022082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1403789Sahrens 		goto error;
1404789Sahrens 	}
1405789Sahrens 
1406789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1407789Sahrens 		goto error;
1408789Sahrens 
14094849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
14102082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1411789Sahrens 	} else {
1412789Sahrens 
14132169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1414789Sahrens 			goto error;
1415789Sahrens 
1416789Sahrens 		/*
1417789Sahrens 		 * Refresh the statistics so the new property is reflected.
1418789Sahrens 		 */
1419789Sahrens 		(void) get_stats(zhp);
1420789Sahrens 	}
1421789Sahrens 
1422789Sahrens error:
1423789Sahrens 	changelist_free(cl);
1424789Sahrens 	return (ret);
1425789Sahrens }
1426789Sahrens 
1427789Sahrens /*
14281356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
14291356Seschrock  * extract them appropriately.
14301356Seschrock  */
14311356Seschrock static uint64_t
14321356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
14331356Seschrock {
14341356Seschrock 	nvlist_t *nv;
14351356Seschrock 	uint64_t value;
14361356Seschrock 
14372885Sahrens 	*source = NULL;
14381356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
14391356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
14405094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
14415094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
14421356Seschrock 	} else {
14438802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
14448802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
14451356Seschrock 		value = zfs_prop_default_numeric(prop);
14461356Seschrock 		*source = "";
14471356Seschrock 	}
14481356Seschrock 
14491356Seschrock 	return (value);
14501356Seschrock }
14511356Seschrock 
14521356Seschrock static char *
14531356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
14541356Seschrock {
14551356Seschrock 	nvlist_t *nv;
14561356Seschrock 	char *value;
14571356Seschrock 
14582885Sahrens 	*source = NULL;
14591356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
14601356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
14615094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
14625094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
14631356Seschrock 	} else {
14648802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
14658802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
14661356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
14671356Seschrock 			value = "";
14681356Seschrock 		*source = "";
14691356Seschrock 	}
14701356Seschrock 
14711356Seschrock 	return (value);
14721356Seschrock }
14731356Seschrock 
14741356Seschrock /*
1475789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1476789Sahrens  * zfs_prop_get_int() are built using this interface.
1477789Sahrens  *
1478789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1479789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1480789Sahrens  * If they differ from the on-disk values, report the current values and mark
1481789Sahrens  * the source "temporary".
1482789Sahrens  */
14832082Seschrock static int
14845094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
14852082Seschrock     char **source, uint64_t *val)
1486789Sahrens {
14875147Srm160521 	zfs_cmd_t zc = { 0 };
14885592Stimh 	nvlist_t *zplprops = NULL;
1489789Sahrens 	struct mnttab mnt;
14903265Sahrens 	char *mntopt_on = NULL;
14913265Sahrens 	char *mntopt_off = NULL;
1492789Sahrens 
1493789Sahrens 	*source = NULL;
1494789Sahrens 
14953265Sahrens 	switch (prop) {
14963265Sahrens 	case ZFS_PROP_ATIME:
14973265Sahrens 		mntopt_on = MNTOPT_ATIME;
14983265Sahrens 		mntopt_off = MNTOPT_NOATIME;
14993265Sahrens 		break;
15003265Sahrens 
15013265Sahrens 	case ZFS_PROP_DEVICES:
15023265Sahrens 		mntopt_on = MNTOPT_DEVICES;
15033265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
15043265Sahrens 		break;
15053265Sahrens 
15063265Sahrens 	case ZFS_PROP_EXEC:
15073265Sahrens 		mntopt_on = MNTOPT_EXEC;
15083265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
15093265Sahrens 		break;
15103265Sahrens 
15113265Sahrens 	case ZFS_PROP_READONLY:
15123265Sahrens 		mntopt_on = MNTOPT_RO;
15133265Sahrens 		mntopt_off = MNTOPT_RW;
15143265Sahrens 		break;
15153265Sahrens 
15163265Sahrens 	case ZFS_PROP_SETUID:
15173265Sahrens 		mntopt_on = MNTOPT_SETUID;
15183265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
15193265Sahrens 		break;
15203265Sahrens 
15213265Sahrens 	case ZFS_PROP_XATTR:
15223265Sahrens 		mntopt_on = MNTOPT_XATTR;
15233265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
15243265Sahrens 		break;
15255331Samw 
15265331Samw 	case ZFS_PROP_NBMAND:
15275331Samw 		mntopt_on = MNTOPT_NBMAND;
15285331Samw 		mntopt_off = MNTOPT_NONBMAND;
15295331Samw 		break;
15303265Sahrens 	}
15313265Sahrens 
15322474Seschrock 	/*
15332474Seschrock 	 * Because looking up the mount options is potentially expensive
15342474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
15352474Seschrock 	 * we're looking up a property which requires its presence.
15362474Seschrock 	 */
15372474Seschrock 	if (!zhp->zfs_mntcheck &&
15383265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
15398228SEric.Taylor@Sun.COM 		libzfs_handle_t *hdl = zhp->zfs_hdl;
15408228SEric.Taylor@Sun.COM 		struct mnttab entry;
15418228SEric.Taylor@Sun.COM 
15428228SEric.Taylor@Sun.COM 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
15438228SEric.Taylor@Sun.COM 			zhp->zfs_mntopts = zfs_strdup(hdl,
15443265Sahrens 			    entry.mnt_mntopts);
15453265Sahrens 			if (zhp->zfs_mntopts == NULL)
15463265Sahrens 				return (-1);
15473265Sahrens 		}
15482474Seschrock 
15492474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
15502474Seschrock 	}
15512474Seschrock 
1552789Sahrens 	if (zhp->zfs_mntopts == NULL)
1553789Sahrens 		mnt.mnt_mntopts = "";
1554789Sahrens 	else
1555789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1556789Sahrens 
1557789Sahrens 	switch (prop) {
1558789Sahrens 	case ZFS_PROP_ATIME:
15593265Sahrens 	case ZFS_PROP_DEVICES:
15603265Sahrens 	case ZFS_PROP_EXEC:
15613265Sahrens 	case ZFS_PROP_READONLY:
15623265Sahrens 	case ZFS_PROP_SETUID:
15633265Sahrens 	case ZFS_PROP_XATTR:
15645331Samw 	case ZFS_PROP_NBMAND:
15652082Seschrock 		*val = getprop_uint64(zhp, prop, source);
15662082Seschrock 
15673265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
15682082Seschrock 			*val = B_TRUE;
1569789Sahrens 			if (src)
15705094Slling 				*src = ZPROP_SRC_TEMPORARY;
15713265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
15722082Seschrock 			*val = B_FALSE;
1573789Sahrens 			if (src)
15745094Slling 				*src = ZPROP_SRC_TEMPORARY;
1575789Sahrens 		}
15762082Seschrock 		break;
1577789Sahrens 
15783265Sahrens 	case ZFS_PROP_CANMOUNT:
15792082Seschrock 		*val = getprop_uint64(zhp, prop, source);
15806168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
15813417Srm160521 			*source = zhp->zfs_name;
15823417Srm160521 		else
15833417Srm160521 			*source = "";	/* default */
15842082Seschrock 		break;
1585789Sahrens 
1586789Sahrens 	case ZFS_PROP_QUOTA:
15875378Sck153898 	case ZFS_PROP_REFQUOTA:
1588789Sahrens 	case ZFS_PROP_RESERVATION:
15895378Sck153898 	case ZFS_PROP_REFRESERVATION:
15902885Sahrens 		*val = getprop_uint64(zhp, prop, source);
15912885Sahrens 		if (*val == 0)
1592789Sahrens 			*source = "";	/* default */
1593789Sahrens 		else
1594789Sahrens 			*source = zhp->zfs_name;
15952082Seschrock 		break;
1596789Sahrens 
1597789Sahrens 	case ZFS_PROP_MOUNTED:
15982082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
15992082Seschrock 		break;
1600789Sahrens 
16013377Seschrock 	case ZFS_PROP_NUMCLONES:
16023377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
16033377Seschrock 		break;
16043377Seschrock 
16055147Srm160521 	case ZFS_PROP_VERSION:
16065498Stimh 	case ZFS_PROP_NORMALIZE:
16075498Stimh 	case ZFS_PROP_UTF8ONLY:
16085498Stimh 	case ZFS_PROP_CASE:
16095498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
16105498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
16115473Srm160521 			return (-1);
16125147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16135498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
16145498Stimh 			zcmd_free_nvlists(&zc);
161510204SMatthew.Ahrens@Sun.COM 			return (-1);
16165147Srm160521 		}
16175498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
16185498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
16195498Stimh 		    val) != 0) {
16205498Stimh 			zcmd_free_nvlists(&zc);
162110204SMatthew.Ahrens@Sun.COM 			return (-1);
16225498Stimh 		}
16235592Stimh 		if (zplprops)
16245592Stimh 			nvlist_free(zplprops);
16255498Stimh 		zcmd_free_nvlists(&zc);
16265147Srm160521 		break;
16275147Srm160521 
1628789Sahrens 	default:
16294577Sahrens 		switch (zfs_prop_get_type(prop)) {
16304787Sahrens 		case PROP_TYPE_NUMBER:
16314787Sahrens 		case PROP_TYPE_INDEX:
16324577Sahrens 			*val = getprop_uint64(zhp, prop, source);
16337390SMatthew.Ahrens@Sun.COM 			/*
16349396SMatthew.Ahrens@Sun.COM 			 * If we tried to use a default value for a
16357390SMatthew.Ahrens@Sun.COM 			 * readonly property, it means that it was not
16367390SMatthew.Ahrens@Sun.COM 			 * present; return an error.
16377390SMatthew.Ahrens@Sun.COM 			 */
16387390SMatthew.Ahrens@Sun.COM 			if (zfs_prop_readonly(prop) &&
16397390SMatthew.Ahrens@Sun.COM 			    *source && (*source)[0] == '\0') {
16407390SMatthew.Ahrens@Sun.COM 				return (-1);
16417390SMatthew.Ahrens@Sun.COM 			}
16424577Sahrens 			break;
16434577Sahrens 
16444787Sahrens 		case PROP_TYPE_STRING:
16454577Sahrens 		default:
16464577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
16474577Sahrens 			    "cannot get non-numeric property"));
16484577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
16494577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
16504577Sahrens 		}
1651789Sahrens 	}
1652789Sahrens 
1653789Sahrens 	return (0);
1654789Sahrens }
1655789Sahrens 
1656789Sahrens /*
1657789Sahrens  * Calculate the source type, given the raw source string.
1658789Sahrens  */
1659789Sahrens static void
16605094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1661789Sahrens     char *statbuf, size_t statlen)
1662789Sahrens {
16635094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1664789Sahrens 		return;
1665789Sahrens 
1666789Sahrens 	if (source == NULL) {
16675094Slling 		*srctype = ZPROP_SRC_NONE;
1668789Sahrens 	} else if (source[0] == '\0') {
16695094Slling 		*srctype = ZPROP_SRC_DEFAULT;
1670789Sahrens 	} else {
1671789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
16725094Slling 			*srctype = ZPROP_SRC_LOCAL;
1673789Sahrens 		} else {
1674789Sahrens 			(void) strlcpy(statbuf, source, statlen);
16755094Slling 			*srctype = ZPROP_SRC_INHERITED;
1676789Sahrens 		}
1677789Sahrens 	}
1678789Sahrens 
1679789Sahrens }
1680789Sahrens 
1681789Sahrens /*
1682789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
1683789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
1684789Sahrens  * human-readable form.
1685789Sahrens  *
1686789Sahrens  * Returns 0 on success, or -1 on error.
1687789Sahrens  */
1688789Sahrens int
1689789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
16905094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1691789Sahrens {
1692789Sahrens 	char *source = NULL;
1693789Sahrens 	uint64_t val;
1694789Sahrens 	char *str;
16952676Seschrock 	const char *strval;
1696789Sahrens 
1697789Sahrens 	/*
1698789Sahrens 	 * Check to see if this property applies to our object
1699789Sahrens 	 */
1700789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1701789Sahrens 		return (-1);
1702789Sahrens 
1703789Sahrens 	if (src)
17045094Slling 		*src = ZPROP_SRC_NONE;
1705789Sahrens 
1706789Sahrens 	switch (prop) {
1707789Sahrens 	case ZFS_PROP_CREATION:
1708789Sahrens 		/*
1709789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
1710789Sahrens 		 * this into a string unless 'literal' is specified.
1711789Sahrens 		 */
1712789Sahrens 		{
17132885Sahrens 			val = getprop_uint64(zhp, prop, &source);
17142885Sahrens 			time_t time = (time_t)val;
1715789Sahrens 			struct tm t;
1716789Sahrens 
1717789Sahrens 			if (literal ||
1718789Sahrens 			    localtime_r(&time, &t) == NULL ||
1719789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1720789Sahrens 			    &t) == 0)
17212885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
1722789Sahrens 		}
1723789Sahrens 		break;
1724789Sahrens 
1725789Sahrens 	case ZFS_PROP_MOUNTPOINT:
1726789Sahrens 		/*
1727789Sahrens 		 * Getting the precise mountpoint can be tricky.
1728789Sahrens 		 *
1729789Sahrens 		 *  - for 'none' or 'legacy', return those values.
1730789Sahrens 		 *  - for inherited mountpoints, we want to take everything
1731789Sahrens 		 *    after our ancestor and append it to the inherited value.
1732789Sahrens 		 *
1733789Sahrens 		 * If the pool has an alternate root, we want to prepend that
1734789Sahrens 		 * root to any values we return.
1735789Sahrens 		 */
17366865Srm160521 
17371356Seschrock 		str = getprop_string(zhp, prop, &source);
17381356Seschrock 
17396612Sgw25295 		if (str[0] == '/') {
17406865Srm160521 			char buf[MAXPATHLEN];
17416865Srm160521 			char *root = buf;
17421356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
1743789Sahrens 
1744789Sahrens 			if (relpath[0] == '/')
1745789Sahrens 				relpath++;
17466612Sgw25295 
17476865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
17486865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
17496865Srm160521 			    (strcmp(root, "-") == 0))
17506865Srm160521 				root[0] = '\0';
17516612Sgw25295 			/*
17526612Sgw25295 			 * Special case an alternate root of '/'. This will
17536612Sgw25295 			 * avoid having multiple leading slashes in the
17546612Sgw25295 			 * mountpoint path.
17556612Sgw25295 			 */
17566612Sgw25295 			if (strcmp(root, "/") == 0)
17576612Sgw25295 				root++;
17586612Sgw25295 
17596612Sgw25295 			/*
17606612Sgw25295 			 * If the mountpoint is '/' then skip over this
17616612Sgw25295 			 * if we are obtaining either an alternate root or
17626612Sgw25295 			 * an inherited mountpoint.
17636612Sgw25295 			 */
17646612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
17656612Sgw25295 			    relpath[0] != '\0'))
17661356Seschrock 				str++;
1767789Sahrens 
1768789Sahrens 			if (relpath[0] == '\0')
1769789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
17701356Seschrock 				    root, str);
1771789Sahrens 			else
1772789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
17731356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
1774789Sahrens 				    relpath);
1775789Sahrens 		} else {
1776789Sahrens 			/* 'legacy' or 'none' */
17771356Seschrock 			(void) strlcpy(propbuf, str, proplen);
1778789Sahrens 		}
1779789Sahrens 
1780789Sahrens 		break;
1781789Sahrens 
1782789Sahrens 	case ZFS_PROP_ORIGIN:
17832885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1784789Sahrens 		    proplen);
1785789Sahrens 		/*
1786789Sahrens 		 * If there is no parent at all, return failure to indicate that
1787789Sahrens 		 * it doesn't apply to this dataset.
1788789Sahrens 		 */
1789789Sahrens 		if (propbuf[0] == '\0')
1790789Sahrens 			return (-1);
1791789Sahrens 		break;
1792789Sahrens 
1793789Sahrens 	case ZFS_PROP_QUOTA:
17945378Sck153898 	case ZFS_PROP_REFQUOTA:
1795789Sahrens 	case ZFS_PROP_RESERVATION:
17965378Sck153898 	case ZFS_PROP_REFRESERVATION:
17975378Sck153898 
17982082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
17992082Seschrock 			return (-1);
1800789Sahrens 
1801789Sahrens 		/*
1802789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
1803789Sahrens 		 * (unless literal is set), and indicate that it's the default
1804789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
1805789Sahrens 		 * that its set locally.
1806789Sahrens 		 */
1807789Sahrens 		if (val == 0) {
1808789Sahrens 			if (literal)
1809789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
1810789Sahrens 			else
1811789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
1812789Sahrens 		} else {
1813789Sahrens 			if (literal)
18142856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
18153912Slling 				    (u_longlong_t)val);
1816789Sahrens 			else
1817789Sahrens 				zfs_nicenum(val, propbuf, proplen);
1818789Sahrens 		}
1819789Sahrens 		break;
1820789Sahrens 
1821789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
18222082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
18232082Seschrock 			return (-1);
18242856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
18252856Snd150628 		    val / 100, (longlong_t)val % 100);
1826789Sahrens 		break;
1827789Sahrens 
1828789Sahrens 	case ZFS_PROP_TYPE:
1829789Sahrens 		switch (zhp->zfs_type) {
1830789Sahrens 		case ZFS_TYPE_FILESYSTEM:
1831789Sahrens 			str = "filesystem";
1832789Sahrens 			break;
1833789Sahrens 		case ZFS_TYPE_VOLUME:
1834789Sahrens 			str = "volume";
1835789Sahrens 			break;
1836789Sahrens 		case ZFS_TYPE_SNAPSHOT:
1837789Sahrens 			str = "snapshot";
1838789Sahrens 			break;
1839789Sahrens 		default:
18402082Seschrock 			abort();
1841789Sahrens 		}
1842789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
1843789Sahrens 		break;
1844789Sahrens 
1845789Sahrens 	case ZFS_PROP_MOUNTED:
1846789Sahrens 		/*
1847789Sahrens 		 * The 'mounted' property is a pseudo-property that described
1848789Sahrens 		 * whether the filesystem is currently mounted.  Even though
1849789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
1850789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
1851789Sahrens 		 */
18522082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
18532082Seschrock 		    src, &source, &val) != 0)
18542082Seschrock 			return (-1);
18552082Seschrock 		if (val)
1856789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
1857789Sahrens 		else
1858789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
1859789Sahrens 		break;
1860789Sahrens 
1861789Sahrens 	case ZFS_PROP_NAME:
1862789Sahrens 		/*
1863789Sahrens 		 * The 'name' property is a pseudo-property derived from the
1864789Sahrens 		 * dataset name.  It is presented as a real property to simplify
1865789Sahrens 		 * consumers.
1866789Sahrens 		 */
1867789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
1868789Sahrens 		break;
1869789Sahrens 
1870789Sahrens 	default:
18714577Sahrens 		switch (zfs_prop_get_type(prop)) {
18724787Sahrens 		case PROP_TYPE_NUMBER:
18734577Sahrens 			if (get_numeric_property(zhp, prop, src,
18744577Sahrens 			    &source, &val) != 0)
18754577Sahrens 				return (-1);
18764577Sahrens 			if (literal)
18774577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
18784577Sahrens 				    (u_longlong_t)val);
18794577Sahrens 			else
18804577Sahrens 				zfs_nicenum(val, propbuf, proplen);
18814577Sahrens 			break;
18824577Sahrens 
18834787Sahrens 		case PROP_TYPE_STRING:
18844577Sahrens 			(void) strlcpy(propbuf,
18854577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
18864577Sahrens 			break;
18874577Sahrens 
18884787Sahrens 		case PROP_TYPE_INDEX:
18894861Sahrens 			if (get_numeric_property(zhp, prop, src,
18904861Sahrens 			    &source, &val) != 0)
18914861Sahrens 				return (-1);
18924861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
18934577Sahrens 				return (-1);
18944577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
18954577Sahrens 			break;
18964577Sahrens 
18974577Sahrens 		default:
18984577Sahrens 			abort();
18994577Sahrens 		}
1900789Sahrens 	}
1901789Sahrens 
1902789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
1903789Sahrens 
1904789Sahrens 	return (0);
1905789Sahrens }
1906789Sahrens 
1907789Sahrens /*
1908789Sahrens  * Utility function to get the given numeric property.  Does no validation that
1909789Sahrens  * the given property is the appropriate type; should only be used with
1910789Sahrens  * hard-coded property types.
1911789Sahrens  */
1912789Sahrens uint64_t
1913789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1914789Sahrens {
1915789Sahrens 	char *source;
19162082Seschrock 	uint64_t val;
19172082Seschrock 
19185367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
19192082Seschrock 
19202082Seschrock 	return (val);
1921789Sahrens }
1922789Sahrens 
19235713Srm160521 int
19245713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
19255713Srm160521 {
19265713Srm160521 	char buf[64];
19275713Srm160521 
19289396SMatthew.Ahrens@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
19295713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
19305713Srm160521 }
19315713Srm160521 
1932789Sahrens /*
1933789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
1934789Sahrens  */
1935789Sahrens int
1936789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
19375094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
1938789Sahrens {
1939789Sahrens 	char *source;
1940789Sahrens 
1941789Sahrens 	/*
1942789Sahrens 	 * Check to see if this property applies to our object
1943789Sahrens 	 */
19444849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
19453237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
19462082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
19472082Seschrock 		    zfs_prop_to_name(prop)));
19484849Sahrens 	}
1949789Sahrens 
1950789Sahrens 	if (src)
19515094Slling 		*src = ZPROP_SRC_NONE;
1952789Sahrens 
19532082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
19542082Seschrock 		return (-1);
1955789Sahrens 
1956789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
1957789Sahrens 
1958789Sahrens 	return (0);
1959789Sahrens }
1960789Sahrens 
19619396SMatthew.Ahrens@Sun.COM static int
19629396SMatthew.Ahrens@Sun.COM idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
19639396SMatthew.Ahrens@Sun.COM     char **domainp, idmap_rid_t *ridp)
19649396SMatthew.Ahrens@Sun.COM {
19659396SMatthew.Ahrens@Sun.COM 	idmap_handle_t *idmap_hdl = NULL;
19669396SMatthew.Ahrens@Sun.COM 	idmap_get_handle_t *get_hdl = NULL;
19679396SMatthew.Ahrens@Sun.COM 	idmap_stat status;
19689396SMatthew.Ahrens@Sun.COM 	int err = EINVAL;
19699396SMatthew.Ahrens@Sun.COM 
19709396SMatthew.Ahrens@Sun.COM 	if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
19719396SMatthew.Ahrens@Sun.COM 		goto out;
19729396SMatthew.Ahrens@Sun.COM 	if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
19739396SMatthew.Ahrens@Sun.COM 		goto out;
19749396SMatthew.Ahrens@Sun.COM 
19759396SMatthew.Ahrens@Sun.COM 	if (isuser) {
19769396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbyuid(get_hdl, id,
19779396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
19789396SMatthew.Ahrens@Sun.COM 	} else {
19799396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbygid(get_hdl, id,
19809396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
19819396SMatthew.Ahrens@Sun.COM 	}
19829396SMatthew.Ahrens@Sun.COM 	if (err == IDMAP_SUCCESS &&
19839396SMatthew.Ahrens@Sun.COM 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
19849396SMatthew.Ahrens@Sun.COM 	    status == IDMAP_SUCCESS)
19859396SMatthew.Ahrens@Sun.COM 		err = 0;
19869396SMatthew.Ahrens@Sun.COM 	else
19879396SMatthew.Ahrens@Sun.COM 		err = EINVAL;
19889396SMatthew.Ahrens@Sun.COM out:
19899396SMatthew.Ahrens@Sun.COM 	if (get_hdl)
19909396SMatthew.Ahrens@Sun.COM 		idmap_get_destroy(get_hdl);
19919396SMatthew.Ahrens@Sun.COM 	if (idmap_hdl)
19929396SMatthew.Ahrens@Sun.COM 		(void) idmap_fini(idmap_hdl);
19939396SMatthew.Ahrens@Sun.COM 	return (err);
19949396SMatthew.Ahrens@Sun.COM }
19959396SMatthew.Ahrens@Sun.COM 
19969396SMatthew.Ahrens@Sun.COM /*
19979396SMatthew.Ahrens@Sun.COM  * convert the propname into parameters needed by kernel
19989396SMatthew.Ahrens@Sun.COM  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
19999396SMatthew.Ahrens@Sun.COM  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
20009396SMatthew.Ahrens@Sun.COM  */
20019396SMatthew.Ahrens@Sun.COM static int
20029396SMatthew.Ahrens@Sun.COM userquota_propname_decode(const char *propname, boolean_t zoned,
20039396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
20049396SMatthew.Ahrens@Sun.COM {
20059396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t type;
20069396SMatthew.Ahrens@Sun.COM 	char *cp, *end;
200710160SMatthew.Ahrens@Sun.COM 	char *numericsid = NULL;
20089396SMatthew.Ahrens@Sun.COM 	boolean_t isuser;
20099396SMatthew.Ahrens@Sun.COM 
20109396SMatthew.Ahrens@Sun.COM 	domain[0] = '\0';
20119396SMatthew.Ahrens@Sun.COM 
20129396SMatthew.Ahrens@Sun.COM 	/* Figure out the property type ({user|group}{quota|space}) */
20139396SMatthew.Ahrens@Sun.COM 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
20149396SMatthew.Ahrens@Sun.COM 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
20159396SMatthew.Ahrens@Sun.COM 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
20169396SMatthew.Ahrens@Sun.COM 			break;
20179396SMatthew.Ahrens@Sun.COM 	}
20189396SMatthew.Ahrens@Sun.COM 	if (type == ZFS_NUM_USERQUOTA_PROPS)
20199396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
20209396SMatthew.Ahrens@Sun.COM 	*typep = type;
20219396SMatthew.Ahrens@Sun.COM 
20229396SMatthew.Ahrens@Sun.COM 	isuser = (type == ZFS_PROP_USERQUOTA ||
20239396SMatthew.Ahrens@Sun.COM 	    type == ZFS_PROP_USERUSED);
20249396SMatthew.Ahrens@Sun.COM 
20259396SMatthew.Ahrens@Sun.COM 	cp = strchr(propname, '@') + 1;
20269396SMatthew.Ahrens@Sun.COM 
20279396SMatthew.Ahrens@Sun.COM 	if (strchr(cp, '@')) {
20289396SMatthew.Ahrens@Sun.COM 		/*
20299396SMatthew.Ahrens@Sun.COM 		 * It's a SID name (eg "user@domain") that needs to be
203010160SMatthew.Ahrens@Sun.COM 		 * turned into S-1-domainID-RID.
20319396SMatthew.Ahrens@Sun.COM 		 */
203210160SMatthew.Ahrens@Sun.COM 		directory_error_t e;
20339396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
20349396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
203510160SMatthew.Ahrens@Sun.COM 		if (isuser) {
203610160SMatthew.Ahrens@Sun.COM 			e = directory_sid_from_user_name(NULL,
203710160SMatthew.Ahrens@Sun.COM 			    cp, &numericsid);
203810160SMatthew.Ahrens@Sun.COM 		} else {
203910160SMatthew.Ahrens@Sun.COM 			e = directory_sid_from_group_name(NULL,
204010160SMatthew.Ahrens@Sun.COM 			    cp, &numericsid);
204110160SMatthew.Ahrens@Sun.COM 		}
204210160SMatthew.Ahrens@Sun.COM 		if (e != NULL) {
204310160SMatthew.Ahrens@Sun.COM 			directory_error_free(e);
20449396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
204510160SMatthew.Ahrens@Sun.COM 		}
204610160SMatthew.Ahrens@Sun.COM 		if (numericsid == NULL)
204710160SMatthew.Ahrens@Sun.COM 			return (ENOENT);
204810160SMatthew.Ahrens@Sun.COM 		cp = numericsid;
204910160SMatthew.Ahrens@Sun.COM 		/* will be further decoded below */
205010160SMatthew.Ahrens@Sun.COM 	}
205110160SMatthew.Ahrens@Sun.COM 
205210160SMatthew.Ahrens@Sun.COM 	if (strncmp(cp, "S-1-", 4) == 0) {
20539396SMatthew.Ahrens@Sun.COM 		/* It's a numeric SID (eg "S-1-234-567-89") */
205410160SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domain, cp, domainlen);
20559396SMatthew.Ahrens@Sun.COM 		cp = strrchr(domain, '-');
20569396SMatthew.Ahrens@Sun.COM 		*cp = '\0';
20579396SMatthew.Ahrens@Sun.COM 		cp++;
20589396SMatthew.Ahrens@Sun.COM 
20599396SMatthew.Ahrens@Sun.COM 		errno = 0;
20609396SMatthew.Ahrens@Sun.COM 		*ridp = strtoull(cp, &end, 10);
206110160SMatthew.Ahrens@Sun.COM 		if (numericsid) {
206210160SMatthew.Ahrens@Sun.COM 			free(numericsid);
206310160SMatthew.Ahrens@Sun.COM 			numericsid = NULL;
206410160SMatthew.Ahrens@Sun.COM 		}
20659688SMatthew.Ahrens@Sun.COM 		if (errno != 0 || *end != '\0')
20669396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
20679396SMatthew.Ahrens@Sun.COM 	} else if (!isdigit(*cp)) {
20689396SMatthew.Ahrens@Sun.COM 		/*
20699396SMatthew.Ahrens@Sun.COM 		 * It's a user/group name (eg "user") that needs to be
20709396SMatthew.Ahrens@Sun.COM 		 * turned into a uid/gid
20719396SMatthew.Ahrens@Sun.COM 		 */
20729396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
20739396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
20749396SMatthew.Ahrens@Sun.COM 		if (isuser) {
20759396SMatthew.Ahrens@Sun.COM 			struct passwd *pw;
20769396SMatthew.Ahrens@Sun.COM 			pw = getpwnam(cp);
20779396SMatthew.Ahrens@Sun.COM 			if (pw == NULL)
20789396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
20799396SMatthew.Ahrens@Sun.COM 			*ridp = pw->pw_uid;
20809396SMatthew.Ahrens@Sun.COM 		} else {
20819396SMatthew.Ahrens@Sun.COM 			struct group *gr;
20829396SMatthew.Ahrens@Sun.COM 			gr = getgrnam(cp);
20839396SMatthew.Ahrens@Sun.COM 			if (gr == NULL)
20849396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
20859396SMatthew.Ahrens@Sun.COM 			*ridp = gr->gr_gid;
20869396SMatthew.Ahrens@Sun.COM 		}
20879396SMatthew.Ahrens@Sun.COM 	} else {
20889396SMatthew.Ahrens@Sun.COM 		/* It's a user/group ID (eg "12345"). */
20899396SMatthew.Ahrens@Sun.COM 		uid_t id = strtoul(cp, &end, 10);
20909396SMatthew.Ahrens@Sun.COM 		idmap_rid_t rid;
20919396SMatthew.Ahrens@Sun.COM 		char *mapdomain;
20929396SMatthew.Ahrens@Sun.COM 
20939396SMatthew.Ahrens@Sun.COM 		if (*end != '\0')
20949396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
20959396SMatthew.Ahrens@Sun.COM 		if (id > MAXUID) {
20969396SMatthew.Ahrens@Sun.COM 			/* It's an ephemeral ID. */
20979396SMatthew.Ahrens@Sun.COM 			if (idmap_id_to_numeric_domain_rid(id, isuser,
20989396SMatthew.Ahrens@Sun.COM 			    &mapdomain, &rid) != 0)
20999396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
210010160SMatthew.Ahrens@Sun.COM 			(void) strlcpy(domain, mapdomain, domainlen);
21019396SMatthew.Ahrens@Sun.COM 			*ridp = rid;
21029396SMatthew.Ahrens@Sun.COM 		} else {
21039396SMatthew.Ahrens@Sun.COM 			*ridp = id;
21049396SMatthew.Ahrens@Sun.COM 		}
21059396SMatthew.Ahrens@Sun.COM 	}
21069396SMatthew.Ahrens@Sun.COM 
210710160SMatthew.Ahrens@Sun.COM 	ASSERT3P(numericsid, ==, NULL);
21089396SMatthew.Ahrens@Sun.COM 	return (0);
21099396SMatthew.Ahrens@Sun.COM }
21109396SMatthew.Ahrens@Sun.COM 
21119469SLin.Ling@Sun.COM static int
21129469SLin.Ling@Sun.COM zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
21139469SLin.Ling@Sun.COM     uint64_t *propvalue, zfs_userquota_prop_t *typep)
21149396SMatthew.Ahrens@Sun.COM {
21159396SMatthew.Ahrens@Sun.COM 	int err;
21169396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
21179396SMatthew.Ahrens@Sun.COM 
21189396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21199396SMatthew.Ahrens@Sun.COM 
21209396SMatthew.Ahrens@Sun.COM 	err = userquota_propname_decode(propname,
21219396SMatthew.Ahrens@Sun.COM 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
21229469SLin.Ling@Sun.COM 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
21239469SLin.Ling@Sun.COM 	zc.zc_objset_type = *typep;
21249396SMatthew.Ahrens@Sun.COM 	if (err)
21259396SMatthew.Ahrens@Sun.COM 		return (err);
21269396SMatthew.Ahrens@Sun.COM 
21279396SMatthew.Ahrens@Sun.COM 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
21289396SMatthew.Ahrens@Sun.COM 	if (err)
21299396SMatthew.Ahrens@Sun.COM 		return (err);
21309396SMatthew.Ahrens@Sun.COM 
21319469SLin.Ling@Sun.COM 	*propvalue = zc.zc_cookie;
21329469SLin.Ling@Sun.COM 	return (0);
21339469SLin.Ling@Sun.COM }
21349469SLin.Ling@Sun.COM 
21359469SLin.Ling@Sun.COM int
21369469SLin.Ling@Sun.COM zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
21379469SLin.Ling@Sun.COM     uint64_t *propvalue)
21389469SLin.Ling@Sun.COM {
21399469SLin.Ling@Sun.COM 	zfs_userquota_prop_t type;
21409469SLin.Ling@Sun.COM 
21419469SLin.Ling@Sun.COM 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
21429469SLin.Ling@Sun.COM 	    &type));
21439469SLin.Ling@Sun.COM }
21449469SLin.Ling@Sun.COM 
21459469SLin.Ling@Sun.COM int
21469469SLin.Ling@Sun.COM zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
21479469SLin.Ling@Sun.COM     char *propbuf, int proplen, boolean_t literal)
21489469SLin.Ling@Sun.COM {
21499469SLin.Ling@Sun.COM 	int err;
21509469SLin.Ling@Sun.COM 	uint64_t propvalue;
21519469SLin.Ling@Sun.COM 	zfs_userquota_prop_t type;
21529469SLin.Ling@Sun.COM 
21539469SLin.Ling@Sun.COM 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
21549469SLin.Ling@Sun.COM 	    &type);
21559469SLin.Ling@Sun.COM 
21569469SLin.Ling@Sun.COM 	if (err)
21579469SLin.Ling@Sun.COM 		return (err);
21589469SLin.Ling@Sun.COM 
21599396SMatthew.Ahrens@Sun.COM 	if (literal) {
21609469SLin.Ling@Sun.COM 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
21619469SLin.Ling@Sun.COM 	} else if (propvalue == 0 &&
21629396SMatthew.Ahrens@Sun.COM 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
21639396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(propbuf, "none", proplen);
21649396SMatthew.Ahrens@Sun.COM 	} else {
21659469SLin.Ling@Sun.COM 		zfs_nicenum(propvalue, propbuf, proplen);
21669396SMatthew.Ahrens@Sun.COM 	}
21679396SMatthew.Ahrens@Sun.COM 	return (0);
21689396SMatthew.Ahrens@Sun.COM }
21699396SMatthew.Ahrens@Sun.COM 
2170789Sahrens /*
2171789Sahrens  * Returns the name of the given zfs handle.
2172789Sahrens  */
2173789Sahrens const char *
2174789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2175789Sahrens {
2176789Sahrens 	return (zhp->zfs_name);
2177789Sahrens }
2178789Sahrens 
2179789Sahrens /*
2180789Sahrens  * Returns the type of the given zfs handle.
2181789Sahrens  */
2182789Sahrens zfs_type_t
2183789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2184789Sahrens {
2185789Sahrens 	return (zhp->zfs_type);
2186789Sahrens }
2187789Sahrens 
21888228SEric.Taylor@Sun.COM static int
21898228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
21908228SEric.Taylor@Sun.COM {
21918228SEric.Taylor@Sun.COM 	int rc;
21928228SEric.Taylor@Sun.COM 	uint64_t	orig_cookie;
21938228SEric.Taylor@Sun.COM 
21948228SEric.Taylor@Sun.COM 	orig_cookie = zc->zc_cookie;
21958228SEric.Taylor@Sun.COM top:
21968228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
21978228SEric.Taylor@Sun.COM 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
21988228SEric.Taylor@Sun.COM 
21998228SEric.Taylor@Sun.COM 	if (rc == -1) {
22008228SEric.Taylor@Sun.COM 		switch (errno) {
22018228SEric.Taylor@Sun.COM 		case ENOMEM:
22028228SEric.Taylor@Sun.COM 			/* expand nvlist memory and try again */
22038228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
22048228SEric.Taylor@Sun.COM 				zcmd_free_nvlists(zc);
22058228SEric.Taylor@Sun.COM 				return (-1);
22068228SEric.Taylor@Sun.COM 			}
22078228SEric.Taylor@Sun.COM 			zc->zc_cookie = orig_cookie;
22088228SEric.Taylor@Sun.COM 			goto top;
22098228SEric.Taylor@Sun.COM 		/*
22108228SEric.Taylor@Sun.COM 		 * An errno value of ESRCH indicates normal completion.
22118228SEric.Taylor@Sun.COM 		 * If ENOENT is returned, then the underlying dataset
22128228SEric.Taylor@Sun.COM 		 * has been removed since we obtained the handle.
22138228SEric.Taylor@Sun.COM 		 */
22148228SEric.Taylor@Sun.COM 		case ESRCH:
22158228SEric.Taylor@Sun.COM 		case ENOENT:
22168228SEric.Taylor@Sun.COM 			rc = 1;
22178228SEric.Taylor@Sun.COM 			break;
22188228SEric.Taylor@Sun.COM 		default:
22198228SEric.Taylor@Sun.COM 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
22208228SEric.Taylor@Sun.COM 			    dgettext(TEXT_DOMAIN,
22218228SEric.Taylor@Sun.COM 			    "cannot iterate filesystems"));
22228228SEric.Taylor@Sun.COM 			break;
22238228SEric.Taylor@Sun.COM 		}
22248228SEric.Taylor@Sun.COM 	}
22258228SEric.Taylor@Sun.COM 	return (rc);
22268228SEric.Taylor@Sun.COM }
22278228SEric.Taylor@Sun.COM 
2228789Sahrens /*
22291356Seschrock  * Iterate over all child filesystems
2230789Sahrens  */
2231789Sahrens int
22321356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2233789Sahrens {
2234789Sahrens 	zfs_cmd_t zc = { 0 };
2235789Sahrens 	zfs_handle_t *nzhp;
2236789Sahrens 	int ret;
2237789Sahrens 
22385367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
22395367Sahrens 		return (0);
22405367Sahrens 
22418228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22428228SEric.Taylor@Sun.COM 		return (-1);
22438228SEric.Taylor@Sun.COM 
22448228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
22458228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
2246789Sahrens 		/*
2247789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2248789Sahrens 		 * that the pool has since been removed.
2249789Sahrens 		 */
22508228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
22518228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2252789Sahrens 			continue;
22538228SEric.Taylor@Sun.COM 		}
22548228SEric.Taylor@Sun.COM 
22558228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
22568228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2257789Sahrens 			return (ret);
22588228SEric.Taylor@Sun.COM 		}
2259789Sahrens 	}
22608228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
22618228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
22621356Seschrock }
22631356Seschrock 
22641356Seschrock /*
22651356Seschrock  * Iterate over all snapshots
22661356Seschrock  */
22671356Seschrock int
22681356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
22691356Seschrock {
22701356Seschrock 	zfs_cmd_t zc = { 0 };
22711356Seschrock 	zfs_handle_t *nzhp;
22721356Seschrock 	int ret;
2273789Sahrens 
22745367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
22755367Sahrens 		return (0);
22765367Sahrens 
22778228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22788228SEric.Taylor@Sun.COM 		return (-1);
22798228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
22808228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
22818228SEric.Taylor@Sun.COM 
22828228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
22838228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2284789Sahrens 			continue;
22858228SEric.Taylor@Sun.COM 		}
22868228SEric.Taylor@Sun.COM 
22878228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
22888228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2289789Sahrens 			return (ret);
22908228SEric.Taylor@Sun.COM 		}
2291789Sahrens 	}
22928228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
22938228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
2294789Sahrens }
2295789Sahrens 
2296789Sahrens /*
22971356Seschrock  * Iterate over all children, snapshots and filesystems
22981356Seschrock  */
22991356Seschrock int
23001356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
23011356Seschrock {
23021356Seschrock 	int ret;
23031356Seschrock 
23041356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
23051356Seschrock 		return (ret);
23061356Seschrock 
23071356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
23081356Seschrock }
23091356Seschrock 
23101356Seschrock /*
2311789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2312789Sahrens  * Can return NULL if this is a pool.
2313789Sahrens  */
2314789Sahrens static int
2315789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2316789Sahrens {
2317789Sahrens 	char *loc;
2318789Sahrens 
2319789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2320789Sahrens 		return (-1);
2321789Sahrens 
2322789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2323789Sahrens 	buf[loc - path] = '\0';
2324789Sahrens 
2325789Sahrens 	return (0);
2326789Sahrens }
2327789Sahrens 
2328789Sahrens /*
23294490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
23304490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
23314490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
23324490Svb160487  * length of already existing prefix of the given path.  We also fetch the
23334490Svb160487  * 'zoned' property, which is used to validate property settings when creating
23344490Svb160487  * new datasets.
2335789Sahrens  */
2336789Sahrens static int
23374490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
23384490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2339789Sahrens {
2340789Sahrens 	zfs_cmd_t zc = { 0 };
2341789Sahrens 	char parent[ZFS_MAXNAMELEN];
2342789Sahrens 	char *slash;
23431356Seschrock 	zfs_handle_t *zhp;
23442082Seschrock 	char errbuf[1024];
23452082Seschrock 
23468269SMark.Musante@Sun.COM 	(void) snprintf(errbuf, sizeof (errbuf),
23478269SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2348789Sahrens 
2349789Sahrens 	/* get parent, and check to see if this is just a pool */
2350789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
23512082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23522082Seschrock 		    "missing dataset name"));
23532082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2354789Sahrens 	}
2355789Sahrens 
2356789Sahrens 	/* check to see if the pool exists */
2357789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2358789Sahrens 		slash = parent + strlen(parent);
2359789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2360789Sahrens 	zc.zc_name[slash - parent] = '\0';
23612082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2362789Sahrens 	    errno == ENOENT) {
23632082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23642082Seschrock 		    "no such pool '%s'"), zc.zc_name);
23652082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2366789Sahrens 	}
2367789Sahrens 
2368789Sahrens 	/* check to see if the parent dataset exists */
23694490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
23704490Svb160487 		if (errno == ENOENT && accept_ancestor) {
23714490Svb160487 			/*
23724490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
23734490Svb160487 			 */
23744490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
23754490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23764490Svb160487 				    "no such pool '%s'"), zc.zc_name);
23774490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
23784490Svb160487 			}
23794490Svb160487 		} else if (errno == ENOENT) {
23802082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23812082Seschrock 			    "parent does not exist"));
23822082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
23834490Svb160487 		} else
23842082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2385789Sahrens 	}
2386789Sahrens 
23872676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2388789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
23892676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
23902082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
23911356Seschrock 		zfs_close(zhp);
2392789Sahrens 		return (-1);
2393789Sahrens 	}
2394789Sahrens 
2395789Sahrens 	/* make sure parent is a filesystem */
23961356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
23972082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23982082Seschrock 		    "parent is not a filesystem"));
23992082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
24001356Seschrock 		zfs_close(zhp);
2401789Sahrens 		return (-1);
2402789Sahrens 	}
2403789Sahrens 
24041356Seschrock 	zfs_close(zhp);
24054490Svb160487 	if (prefixlen != NULL)
24064490Svb160487 		*prefixlen = strlen(parent);
24074490Svb160487 	return (0);
24084490Svb160487 }
24094490Svb160487 
24104490Svb160487 /*
24114490Svb160487  * Finds whether the dataset of the given type(s) exists.
24124490Svb160487  */
24134490Svb160487 boolean_t
24144490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
24154490Svb160487 {
24164490Svb160487 	zfs_handle_t *zhp;
24174490Svb160487 
24185326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
24194490Svb160487 		return (B_FALSE);
24204490Svb160487 
24214490Svb160487 	/*
24224490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
24234490Svb160487 	 */
24244490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
24254490Svb160487 		int ds_type = zhp->zfs_type;
24264490Svb160487 
24274490Svb160487 		zfs_close(zhp);
24284490Svb160487 		if (types & ds_type)
24294490Svb160487 			return (B_TRUE);
24304490Svb160487 	}
24314490Svb160487 	return (B_FALSE);
24324490Svb160487 }
24334490Svb160487 
24344490Svb160487 /*
24355367Sahrens  * Given a path to 'target', create all the ancestors between
24365367Sahrens  * the prefixlen portion of the path, and the target itself.
24375367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
24385367Sahrens  */
24395367Sahrens int
24405367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
24415367Sahrens {
24425367Sahrens 	zfs_handle_t *h;
24435367Sahrens 	char *cp;
24445367Sahrens 	const char *opname;
24455367Sahrens 
24465367Sahrens 	/* make sure prefix exists */
24475367Sahrens 	cp = target + prefixlen;
24485367Sahrens 	if (*cp != '/') {
24495367Sahrens 		assert(strchr(cp, '/') == NULL);
24505367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24515367Sahrens 	} else {
24525367Sahrens 		*cp = '\0';
24535367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24545367Sahrens 		*cp = '/';
24555367Sahrens 	}
24565367Sahrens 	if (h == NULL)
24575367Sahrens 		return (-1);
24585367Sahrens 	zfs_close(h);
24595367Sahrens 
24605367Sahrens 	/*
24615367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
24625367Sahrens 	 * up to the prefixlen-long one.
24635367Sahrens 	 */
24645367Sahrens 	for (cp = target + prefixlen + 1;
24655367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
24665367Sahrens 		char *logstr;
24675367Sahrens 
24685367Sahrens 		*cp = '\0';
24695367Sahrens 
24705367Sahrens 		h = make_dataset_handle(hdl, target);
24715367Sahrens 		if (h) {
24725367Sahrens 			/* it already exists, nothing to do here */
24735367Sahrens 			zfs_close(h);
24745367Sahrens 			continue;
24755367Sahrens 		}
24765367Sahrens 
24775367Sahrens 		logstr = hdl->libzfs_log_str;
24785367Sahrens 		hdl->libzfs_log_str = NULL;
24795367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
24805367Sahrens 		    NULL) != 0) {
24815367Sahrens 			hdl->libzfs_log_str = logstr;
24825367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
24835367Sahrens 			goto ancestorerr;
24845367Sahrens 		}
24855367Sahrens 
24865367Sahrens 		hdl->libzfs_log_str = logstr;
24875367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24885367Sahrens 		if (h == NULL) {
24895367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
24905367Sahrens 			goto ancestorerr;
24915367Sahrens 		}
24925367Sahrens 
24935367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
24945367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
24955367Sahrens 			goto ancestorerr;
24965367Sahrens 		}
24975367Sahrens 
24985367Sahrens 		if (zfs_share(h) != 0) {
24995367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
25005367Sahrens 			goto ancestorerr;
25015367Sahrens 		}
25025367Sahrens 
25035367Sahrens 		zfs_close(h);
25045367Sahrens 	}
25055367Sahrens 
25065367Sahrens 	return (0);
25075367Sahrens 
25085367Sahrens ancestorerr:
25095367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25105367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
25115367Sahrens 	return (-1);
25125367Sahrens }
25135367Sahrens 
25145367Sahrens /*
25154490Svb160487  * Creates non-existing ancestors of the given path.
25164490Svb160487  */
25174490Svb160487 int
25184490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
25194490Svb160487 {
25204490Svb160487 	int prefix;
25214490Svb160487 	uint64_t zoned;
25224490Svb160487 	char *path_copy;
25234490Svb160487 	int rc;
25244490Svb160487 
25254490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
25264490Svb160487 		return (-1);
25274490Svb160487 
25284490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
25294490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
25304490Svb160487 		free(path_copy);
25314490Svb160487 	}
25324490Svb160487 	if (path_copy == NULL || rc != 0)
25334490Svb160487 		return (-1);
25344490Svb160487 
2535789Sahrens 	return (0);
2536789Sahrens }
2537789Sahrens 
2538789Sahrens /*
25392676Seschrock  * Create a new filesystem or volume.
2540789Sahrens  */
2541789Sahrens int
25422082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
25432676Seschrock     nvlist_t *props)
2544789Sahrens {
2545789Sahrens 	zfs_cmd_t zc = { 0 };
2546789Sahrens 	int ret;
2547789Sahrens 	uint64_t size = 0;
2548789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
25492082Seschrock 	char errbuf[1024];
25502676Seschrock 	uint64_t zoned;
25512082Seschrock 
25522082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
25532082Seschrock 	    "cannot create '%s'"), path);
2554789Sahrens 
2555789Sahrens 	/* validate the path, taking care to note the extended error message */
25565326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
25572082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2558789Sahrens 
2559789Sahrens 	/* validate parents exist */
25604490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2561789Sahrens 		return (-1);
2562789Sahrens 
2563789Sahrens 	/*
2564789Sahrens 	 * The failure modes when creating a dataset of a different type over
2565789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2566789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2567789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2568789Sahrens 	 * first try to see if the dataset exists.
2569789Sahrens 	 */
2570789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
25715094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
25722082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25732082Seschrock 		    "dataset already exists"));
25742082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2575789Sahrens 	}
2576789Sahrens 
2577789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2578789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2579789Sahrens 	else
2580789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2581789Sahrens 
25827184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
25833912Slling 	    zoned, NULL, errbuf)) == 0)
25842676Seschrock 		return (-1);
25852676Seschrock 
2586789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
25871133Seschrock 		/*
25881133Seschrock 		 * If we are creating a volume, the size and block size must
25891133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
25901133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
25911133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
25921133Seschrock 		 * zero.
25931133Seschrock 		 */
25942676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
25952676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
25962676Seschrock 			nvlist_free(props);
25972082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25982676Seschrock 			    "missing volume size"));
25992676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2600789Sahrens 		}
2601789Sahrens 
26022676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
26032676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
26042676Seschrock 		    &blocksize)) != 0) {
26052676Seschrock 			if (ret == ENOENT) {
26062676Seschrock 				blocksize = zfs_prop_default_numeric(
26072676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
26082676Seschrock 			} else {
26092676Seschrock 				nvlist_free(props);
26102676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26112676Seschrock 				    "missing volume block size"));
26122676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26132676Seschrock 			}
26142676Seschrock 		}
26152676Seschrock 
26162676Seschrock 		if (size == 0) {
26172676Seschrock 			nvlist_free(props);
26182082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26192676Seschrock 			    "volume size cannot be zero"));
26202676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26211133Seschrock 		}
26221133Seschrock 
26231133Seschrock 		if (size % blocksize != 0) {
26242676Seschrock 			nvlist_free(props);
26252082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26262676Seschrock 			    "volume size must be a multiple of volume block "
26272676Seschrock 			    "size"));
26282676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26291133Seschrock 		}
2630789Sahrens 	}
2631789Sahrens 
26325094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
26332676Seschrock 		return (-1);
26342676Seschrock 	nvlist_free(props);
26352676Seschrock 
2636789Sahrens 	/* create the dataset */
26374543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2638789Sahrens 
26393912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
26402082Seschrock 		ret = zvol_create_link(hdl, path);
26413912Slling 		if (ret) {
26423912Slling 			(void) zfs_standard_error(hdl, errno,
26433912Slling 			    dgettext(TEXT_DOMAIN,
26443912Slling 			    "Volume successfully created, but device links "
26453912Slling 			    "were not created"));
26463912Slling 			zcmd_free_nvlists(&zc);
26473912Slling 			return (-1);
26483912Slling 		}
26493912Slling 	}
2650789Sahrens 
26512676Seschrock 	zcmd_free_nvlists(&zc);
26522676Seschrock 
2653789Sahrens 	/* check for failure */
2654789Sahrens 	if (ret != 0) {
2655789Sahrens 		char parent[ZFS_MAXNAMELEN];
2656789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2657789Sahrens 
2658789Sahrens 		switch (errno) {
2659789Sahrens 		case ENOENT:
26602082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26612082Seschrock 			    "no such parent '%s'"), parent);
26622082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2663789Sahrens 
2664789Sahrens 		case EINVAL:
26652082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26663413Smmusante 			    "parent '%s' is not a filesystem"), parent);
26672082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2668789Sahrens 
2669789Sahrens 		case EDOM:
26702082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26712676Seschrock 			    "volume block size must be power of 2 from "
26722676Seschrock 			    "%u to %uk"),
2673789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2674789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
26752082Seschrock 
26762676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26772082Seschrock 
26784603Sahrens 		case ENOTSUP:
26794603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26804603Sahrens 			    "pool must be upgraded to set this "
26814603Sahrens 			    "property or value"));
26824603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2683789Sahrens #ifdef _ILP32
2684789Sahrens 		case EOVERFLOW:
2685789Sahrens 			/*
2686789Sahrens 			 * This platform can't address a volume this big.
2687789Sahrens 			 */
26882082Seschrock 			if (type == ZFS_TYPE_VOLUME)
26892082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
26902082Seschrock 				    errbuf));
2691789Sahrens #endif
26922082Seschrock 			/* FALLTHROUGH */
2693789Sahrens 		default:
26942082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2695789Sahrens 		}
2696789Sahrens 	}
2697789Sahrens 
2698789Sahrens 	return (0);
2699789Sahrens }
2700789Sahrens 
2701789Sahrens /*
2702789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2703789Sahrens  * isn't mounted, and that there are no active dependents.
2704789Sahrens  */
2705789Sahrens int
270610242Schris.kirby@sun.com zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2707789Sahrens {
2708789Sahrens 	zfs_cmd_t zc = { 0 };
2709789Sahrens 
2710789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2711789Sahrens 
27122676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
27133126Sahl 		/*
27144543Smarks 		 * If user doesn't have permissions to unshare volume, then
27154543Smarks 		 * abort the request.  This would only happen for a
27164543Smarks 		 * non-privileged user.
27173126Sahl 		 */
27184543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
27194543Smarks 			return (-1);
27204543Smarks 		}
27213126Sahl 
27222082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2723789Sahrens 			return (-1);
2724789Sahrens 
2725789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2726789Sahrens 	} else {
2727789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2728789Sahrens 	}
2729789Sahrens 
273010242Schris.kirby@sun.com 	zc.zc_defer_destroy = defer;
27314543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
27323237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
27332082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
27342082Seschrock 		    zhp->zfs_name));
27352199Sahrens 	}
2736789Sahrens 
2737789Sahrens 	remove_mountpoint(zhp);
2738789Sahrens 
2739789Sahrens 	return (0);
2740789Sahrens }
2741789Sahrens 
27422199Sahrens struct destroydata {
27432199Sahrens 	char *snapname;
27442199Sahrens 	boolean_t gotone;
27453265Sahrens 	boolean_t closezhp;
27462199Sahrens };
27472199Sahrens 
27482199Sahrens static int
27492199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
27502199Sahrens {
27512199Sahrens 	struct destroydata *dd = arg;
27522199Sahrens 	zfs_handle_t *szhp;
27532199Sahrens 	char name[ZFS_MAXNAMELEN];
27543265Sahrens 	boolean_t closezhp = dd->closezhp;
27553265Sahrens 	int rv;
27562199Sahrens 
27572676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
27582676Seschrock 	(void) strlcat(name, "@", sizeof (name));
27592676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
27602199Sahrens 
27612199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
27622199Sahrens 	if (szhp) {
27632199Sahrens 		dd->gotone = B_TRUE;
27642199Sahrens 		zfs_close(szhp);
27652199Sahrens 	}
27662199Sahrens 
27672199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
27682199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
27692199Sahrens 		/*
27702199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
27712199Sahrens 		 * return an error, because then we wouldn't visit all
27722199Sahrens 		 * the volumes.
27732199Sahrens 		 */
27742199Sahrens 	}
27752199Sahrens 
27763265Sahrens 	dd->closezhp = B_TRUE;
27773265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
27783265Sahrens 	if (closezhp)
27793265Sahrens 		zfs_close(zhp);
27803265Sahrens 	return (rv);
27812199Sahrens }
27822199Sahrens 
27832199Sahrens /*
27842199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
27852199Sahrens  */
27862199Sahrens int
278710242Schris.kirby@sun.com zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
27882199Sahrens {
27892199Sahrens 	zfs_cmd_t zc = { 0 };
27902199Sahrens 	int ret;
27912199Sahrens 	struct destroydata dd = { 0 };
27922199Sahrens 
27932199Sahrens 	dd.snapname = snapname;
27942199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
27952199Sahrens 
27962199Sahrens 	if (!dd.gotone) {
27973237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
27982199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
27992199Sahrens 		    zhp->zfs_name, snapname));
28002199Sahrens 	}
28012199Sahrens 
28022199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
28032676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
280410242Schris.kirby@sun.com 	zc.zc_defer_destroy = defer;
28052199Sahrens 
28064543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
28072199Sahrens 	if (ret != 0) {
28082199Sahrens 		char errbuf[1024];
28092199Sahrens 
28102199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28112199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
28122199Sahrens 
28132199Sahrens 		switch (errno) {
28142199Sahrens 		case EEXIST:
28152199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28162199Sahrens 			    "snapshot is cloned"));
28172199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
28182199Sahrens 
28192199Sahrens 		default:
28202199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
28212199Sahrens 			    errbuf));
28222199Sahrens 		}
28232199Sahrens 	}
28242199Sahrens 
28252199Sahrens 	return (0);
28262199Sahrens }
28272199Sahrens 
2828789Sahrens /*
2829789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
2830789Sahrens  */
2831789Sahrens int
28322676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2833789Sahrens {
2834789Sahrens 	zfs_cmd_t zc = { 0 };
2835789Sahrens 	char parent[ZFS_MAXNAMELEN];
2836789Sahrens 	int ret;
28372082Seschrock 	char errbuf[1024];
28382082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
28392676Seschrock 	zfs_type_t type;
28402676Seschrock 	uint64_t zoned;
2841789Sahrens 
2842789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2843789Sahrens 
28442082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28452082Seschrock 	    "cannot create '%s'"), target);
28462082Seschrock 
2847789Sahrens 	/* validate the target name */
28485326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
28492082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2850789Sahrens 
2851789Sahrens 	/* validate parents exist */
28524490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2853789Sahrens 		return (-1);
2854789Sahrens 
2855789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
2856789Sahrens 
2857789Sahrens 	/* do the clone */
28582676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
2859789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
28602744Snn35248 		type = ZFS_TYPE_VOLUME;
28612676Seschrock 	} else {
2862789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
28632744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
28642676Seschrock 	}
28652676Seschrock 
28662676Seschrock 	if (props) {
28677184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
28687184Stimh 		    zhp, errbuf)) == NULL)
28692676Seschrock 			return (-1);
28702676Seschrock 
28715094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
28722676Seschrock 			nvlist_free(props);
28732676Seschrock 			return (-1);
28742676Seschrock 		}
28752676Seschrock 
28762676Seschrock 		nvlist_free(props);
28772676Seschrock 	}
2878789Sahrens 
2879789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
28802676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
28814543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2882789Sahrens 
28832676Seschrock 	zcmd_free_nvlists(&zc);
28842676Seschrock 
2885789Sahrens 	if (ret != 0) {
2886789Sahrens 		switch (errno) {
2887789Sahrens 
2888789Sahrens 		case ENOENT:
2889789Sahrens 			/*
2890789Sahrens 			 * The parent doesn't exist.  We should have caught this
2891789Sahrens 			 * above, but there may a race condition that has since
2892789Sahrens 			 * destroyed the parent.
2893789Sahrens 			 *
2894789Sahrens 			 * At this point, we don't know whether it's the source
2895789Sahrens 			 * that doesn't exist anymore, or whether the target
2896789Sahrens 			 * dataset doesn't exist.
2897789Sahrens 			 */
28982082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28992082Seschrock 			    "no such parent '%s'"), parent);
29002082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
29012082Seschrock 
29022082Seschrock 		case EXDEV:
29032082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29042082Seschrock 			    "source and target pools differ"));
29052082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
29062082Seschrock 			    errbuf));
29072082Seschrock 
29082082Seschrock 		default:
29092082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
29102082Seschrock 			    errbuf));
29112082Seschrock 		}
29122676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
29132082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
29142082Seschrock 	}
29152082Seschrock 
29162082Seschrock 	return (ret);
29172082Seschrock }
29182082Seschrock 
29192082Seschrock typedef struct promote_data {
29202082Seschrock 	char cb_mountpoint[MAXPATHLEN];
29212082Seschrock 	const char *cb_target;
29222082Seschrock 	const char *cb_errbuf;
29232082Seschrock 	uint64_t cb_pivot_txg;
29242082Seschrock } promote_data_t;
29252082Seschrock 
29262082Seschrock static int
29272082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
29282082Seschrock {
29292082Seschrock 	promote_data_t *pd = data;
29302082Seschrock 	zfs_handle_t *szhp;
29312082Seschrock 	char snapname[MAXPATHLEN];
29323265Sahrens 	int rv = 0;
29332082Seschrock 
29342082Seschrock 	/* We don't care about snapshots after the pivot point */
29353265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
29363265Sahrens 		zfs_close(zhp);
29372082Seschrock 		return (0);
29383265Sahrens 	}
29392082Seschrock 
29402417Sahrens 	/* Remove the device link if it's a zvol. */
29412676Seschrock 	if (ZFS_IS_VOLUME(zhp))
29422417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
29432082Seschrock 
29442082Seschrock 	/* Check for conflicting names */
29452676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
29462676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
29472082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
29482082Seschrock 	if (szhp != NULL) {
29492082Seschrock 		zfs_close(szhp);
29502082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
29512082Seschrock 		    "snapshot name '%s' from origin \n"
29522082Seschrock 		    "conflicts with '%s' from target"),
29532082Seschrock 		    zhp->zfs_name, snapname);
29543265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
29552082Seschrock 	}
29563265Sahrens 	zfs_close(zhp);
29573265Sahrens 	return (rv);
29582082Seschrock }
29592082Seschrock 
29602417Sahrens static int
29612417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
29622417Sahrens {
29632417Sahrens 	promote_data_t *pd = data;
29642417Sahrens 
29652417Sahrens 	/* We don't care about snapshots after the pivot point */
29663265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
29673265Sahrens 		/* Create the device link if it's a zvol. */
29683265Sahrens 		if (ZFS_IS_VOLUME(zhp))
29693265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
29703265Sahrens 	}
29713265Sahrens 
29723265Sahrens 	zfs_close(zhp);
29732417Sahrens 	return (0);
29742417Sahrens }
29752417Sahrens 
29762082Seschrock /*
29772082Seschrock  * Promotes the given clone fs to be the clone parent.
29782082Seschrock  */
29792082Seschrock int
29802082Seschrock zfs_promote(zfs_handle_t *zhp)
29812082Seschrock {
29822082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
29832082Seschrock 	zfs_cmd_t zc = { 0 };
29842082Seschrock 	char parent[MAXPATHLEN];
29852082Seschrock 	char *cp;
29862082Seschrock 	int ret;
29872082Seschrock 	zfs_handle_t *pzhp;
29882082Seschrock 	promote_data_t pd;
29892082Seschrock 	char errbuf[1024];
29902082Seschrock 
29912082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29922082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
29932082Seschrock 
29942082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
29952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29962082Seschrock 		    "snapshots can not be promoted"));
29972082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
29982082Seschrock 	}
29992082Seschrock 
30005367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
30012082Seschrock 	if (parent[0] == '\0') {
30022082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30032082Seschrock 		    "not a cloned filesystem"));
30042082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
30052082Seschrock 	}
30062082Seschrock 	cp = strchr(parent, '@');
30072082Seschrock 	*cp = '\0';
30082082Seschrock 
30092082Seschrock 	/* Walk the snapshots we will be moving */
30105367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
30112082Seschrock 	if (pzhp == NULL)
30122082Seschrock 		return (-1);
30132082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
30142082Seschrock 	zfs_close(pzhp);
30152082Seschrock 	pd.cb_target = zhp->zfs_name;
30162082Seschrock 	pd.cb_errbuf = errbuf;
30175094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
30182082Seschrock 	if (pzhp == NULL)
30192082Seschrock 		return (-1);
30202082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
30212082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
30222082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
30232417Sahrens 	if (ret != 0) {
30242417Sahrens 		zfs_close(pzhp);
30252082Seschrock 		return (-1);
30262417Sahrens 	}
30272082Seschrock 
30282082Seschrock 	/* issue the ioctl */
30295367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
30302676Seschrock 	    sizeof (zc.zc_value));
30312082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
30324543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
30332082Seschrock 
30342082Seschrock 	if (ret != 0) {
30352417Sahrens 		int save_errno = errno;
30362417Sahrens 
30372417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
30382417Sahrens 		zfs_close(pzhp);
30392417Sahrens 
30402417Sahrens 		switch (save_errno) {
3041789Sahrens 		case EEXIST:
3042789Sahrens 			/*
30432082Seschrock 			 * There is a conflicting snapshot name.  We
30442082Seschrock 			 * should have caught this above, but they could
30452082Seschrock 			 * have renamed something in the mean time.
3046789Sahrens 			 */
30472082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30482082Seschrock 			    "conflicting snapshot name from parent '%s'"),
30492082Seschrock 			    parent);
30502082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3051789Sahrens 
3052789Sahrens 		default:
30532417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3054789Sahrens 		}
30552417Sahrens 	} else {
30562417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3057789Sahrens 	}
3058789Sahrens 
30592417Sahrens 	zfs_close(pzhp);
3060789Sahrens 	return (ret);
3061789Sahrens }
3062789Sahrens 
30634007Smmusante struct createdata {
30644007Smmusante 	const char *cd_snapname;
30654007Smmusante 	int cd_ifexists;
30664007Smmusante };
30674007Smmusante 
30682199Sahrens static int
30692199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
30702199Sahrens {
30714007Smmusante 	struct createdata *cd = arg;
30722676Seschrock 	int ret;
30732199Sahrens 
30742199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
30752199Sahrens 		char name[MAXPATHLEN];
30762199Sahrens 
30772676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
30782676Seschrock 		(void) strlcat(name, "@", sizeof (name));
30794007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
30804007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
30814007Smmusante 		    cd->cd_ifexists);
30822199Sahrens 		/*
30832199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
30842199Sahrens 		 * return an error, because then we wouldn't visit all
30852199Sahrens 		 * the volumes.
30862199Sahrens 		 */
30872199Sahrens 	}
30882676Seschrock 
30894007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
30902676Seschrock 
30912676Seschrock 	zfs_close(zhp);
30922676Seschrock 
30932676Seschrock 	return (ret);
30942199Sahrens }
30952199Sahrens 
3096789Sahrens /*
30973504Sahl  * Takes a snapshot of the given dataset.
3098789Sahrens  */
3099789Sahrens int
31007265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
31017265Sahrens     nvlist_t *props)
3102789Sahrens {
3103789Sahrens 	const char *delim;
31047265Sahrens 	char parent[ZFS_MAXNAMELEN];
3105789Sahrens 	zfs_handle_t *zhp;
3106789Sahrens 	zfs_cmd_t zc = { 0 };
3107789Sahrens 	int ret;
31082082Seschrock 	char errbuf[1024];
31092082Seschrock 
31102082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31112082Seschrock 	    "cannot snapshot '%s'"), path);
31122082Seschrock 
31132082Seschrock 	/* validate the target name */
31145326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
31152082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3116789Sahrens 
31177265Sahrens 	if (props) {
31187265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
31197265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
31207265Sahrens 			return (-1);
31217265Sahrens 
31227265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
31237265Sahrens 			nvlist_free(props);
31247265Sahrens 			return (-1);
31257265Sahrens 		}
31267265Sahrens 
31277265Sahrens 		nvlist_free(props);
31287265Sahrens 	}
31297265Sahrens 
3130789Sahrens 	/* make sure the parent exists and is of the appropriate type */
31312199Sahrens 	delim = strchr(path, '@');
3132789Sahrens 	(void) strncpy(parent, path, delim - path);
3133789Sahrens 	parent[delim - path] = '\0';
3134789Sahrens 
31352082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3136789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
31377265Sahrens 		zcmd_free_nvlists(&zc);
3138789Sahrens 		return (-1);
3139789Sahrens 	}
3140789Sahrens 
31412199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31422676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
31434543Smarks 	if (ZFS_IS_VOLUME(zhp))
31444543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
31454543Smarks 	else
31464543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
31472199Sahrens 	zc.zc_cookie = recursive;
31484543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
31492199Sahrens 
31507265Sahrens 	zcmd_free_nvlists(&zc);
31517265Sahrens 
31522199Sahrens 	/*
31532199Sahrens 	 * if it was recursive, the one that actually failed will be in
31542199Sahrens 	 * zc.zc_name.
31552199Sahrens 	 */
31564543Smarks 	if (ret != 0)
31574543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31584543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
31594543Smarks 
31602199Sahrens 	if (ret == 0 && recursive) {
31614007Smmusante 		struct createdata cd;
31624007Smmusante 
31634007Smmusante 		cd.cd_snapname = delim + 1;
31644007Smmusante 		cd.cd_ifexists = B_FALSE;
31654007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
31662199Sahrens 	}
3167789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
31682082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
31692199Sahrens 		if (ret != 0) {
31704543Smarks 			(void) zfs_standard_error(hdl, errno,
31714543Smarks 			    dgettext(TEXT_DOMAIN,
31724543Smarks 			    "Volume successfully snapshotted, but device links "
31734543Smarks 			    "were not created"));
31744543Smarks 			zfs_close(zhp);
31754543Smarks 			return (-1);
31762199Sahrens 		}
3177789Sahrens 	}
3178789Sahrens 
31792082Seschrock 	if (ret != 0)
31802082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3181789Sahrens 
3182789Sahrens 	zfs_close(zhp);
3183789Sahrens 
3184789Sahrens 	return (ret);
3185789Sahrens }
3186789Sahrens 
3187789Sahrens /*
31881294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
31891294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
31901294Slling  * is a dependent and we should just destroy it without checking the transaction
31911294Slling  * group.
3192789Sahrens  */
31931294Slling typedef struct rollback_data {
31941294Slling 	const char	*cb_target;		/* the snapshot */
31951294Slling 	uint64_t	cb_create;		/* creation time reference */
31965749Sahrens 	boolean_t	cb_error;
31972082Seschrock 	boolean_t	cb_dependent;
31985749Sahrens 	boolean_t	cb_force;
31991294Slling } rollback_data_t;
32001294Slling 
32011294Slling static int
32021294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
32031294Slling {
32041294Slling 	rollback_data_t *cbp = data;
32051294Slling 
32061294Slling 	if (!cbp->cb_dependent) {
32071294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
32081294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
32091294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
32101294Slling 		    cbp->cb_create) {
32114543Smarks 			char *logstr;
32121294Slling 
32132082Seschrock 			cbp->cb_dependent = B_TRUE;
32145446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
32155446Sahrens 			    rollback_destroy, cbp);
32162082Seschrock 			cbp->cb_dependent = B_FALSE;
32171294Slling 
32184543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
32194543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
322010242Schris.kirby@sun.com 			cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
32214543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
32221294Slling 		}
32231294Slling 	} else {
32245749Sahrens 		/* We must destroy this clone; first unmount it */
32255749Sahrens 		prop_changelist_t *clp;
32265749Sahrens 
32277366STim.Haley@Sun.COM 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
32285749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
32295749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
32305749Sahrens 			cbp->cb_error = B_TRUE;
32315749Sahrens 			zfs_close(zhp);
32325749Sahrens 			return (0);
32335749Sahrens 		}
323410242Schris.kirby@sun.com 		if (zfs_destroy(zhp, B_FALSE) != 0)
32355749Sahrens 			cbp->cb_error = B_TRUE;
32365749Sahrens 		else
32375749Sahrens 			changelist_remove(clp, zhp->zfs_name);
32385751Sahrens 		(void) changelist_postfix(clp);
32395749Sahrens 		changelist_free(clp);
32401294Slling 	}
32411294Slling 
32421294Slling 	zfs_close(zhp);
32431294Slling 	return (0);
32441294Slling }
32451294Slling 
32461294Slling /*
32475446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
32485446Sahrens  * data changes since then and making it the active dataset.
32495446Sahrens  *
32505446Sahrens  * Any snapshots more recent than the target are destroyed, along with
32515446Sahrens  * their dependents.
32521294Slling  */
32535446Sahrens int
32545749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3255789Sahrens {
32565446Sahrens 	rollback_data_t cb = { 0 };
32575446Sahrens 	int err;
3258789Sahrens 	zfs_cmd_t zc = { 0 };
32595713Srm160521 	boolean_t restore_resv = 0;
32605713Srm160521 	uint64_t old_volsize, new_volsize;
32615713Srm160521 	zfs_prop_t resv_prop;
3262789Sahrens 
3263789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3264789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3265789Sahrens 
32665446Sahrens 	/*
32675446Sahrens 	 * Destroy all recent snapshots and its dependends.
32685446Sahrens 	 */
32695749Sahrens 	cb.cb_force = force;
32705446Sahrens 	cb.cb_target = snap->zfs_name;
32715446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
32725446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
32735446Sahrens 
32745749Sahrens 	if (cb.cb_error)
32755749Sahrens 		return (-1);
32765446Sahrens 
32775446Sahrens 	/*
32785446Sahrens 	 * Now that we have verified that the snapshot is the latest,
32795446Sahrens 	 * rollback to the given snapshot.
32805446Sahrens 	 */
32815446Sahrens 
32825713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
32835713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
32845713Srm160521 			return (-1);
32855713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
32865713Srm160521 			return (-1);
32875713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
32885713Srm160521 		restore_resv =
32895713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
32905713Srm160521 	}
3291789Sahrens 
3292789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3293789Sahrens 
32942676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3295789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3296789Sahrens 	else
3297789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3298789Sahrens 
3299789Sahrens 	/*
33005446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
33015446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
33025446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
33035446Sahrens 	 * an unlikely race condition where the user has taken a
33045446Sahrens 	 * snapshot since we verified that this was the most recent.
33055713Srm160521 	 *
3306789Sahrens 	 */
33075446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
33083237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
33092082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
33102082Seschrock 		    zhp->zfs_name);
33115717Srm160521 		return (err);
33125717Srm160521 	}
33135713Srm160521 
33145713Srm160521 	/*
33155713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
33165713Srm160521 	 * rollback reservation and the volsize has changed then set
33175713Srm160521 	 * the reservation property to the post-rollback volsize.
33185713Srm160521 	 * Make a new handle since the rollback closed the dataset.
33195713Srm160521 	 */
33205717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
33215717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
33225717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
33235717Srm160521 			zfs_close(zhp);
33245713Srm160521 			return (err);
33255717Srm160521 		}
33265713Srm160521 		if (restore_resv) {
33275713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
33285713Srm160521 			if (old_volsize != new_volsize)
33295717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
33305717Srm160521 				    new_volsize);
33315713Srm160521 		}
33325713Srm160521 		zfs_close(zhp);
3333789Sahrens 	}
33345446Sahrens 	return (err);
33351294Slling }
33361294Slling 
33371294Slling /*
3338789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3339789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3340789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3341789Sahrens  * libzfs_graph.c.
3342789Sahrens  */
3343789Sahrens int
33442474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
33452474Seschrock     zfs_iter_f func, void *data)
3346789Sahrens {
3347789Sahrens 	char **dependents;
3348789Sahrens 	size_t count;
3349789Sahrens 	int i;
3350789Sahrens 	zfs_handle_t *child;
3351789Sahrens 	int ret = 0;
3352789Sahrens 
33532474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
33542474Seschrock 	    &dependents, &count) != 0)
33552474Seschrock 		return (-1);
33562474Seschrock 
3357789Sahrens 	for (i = 0; i < count; i++) {
33582082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
33592082Seschrock 		    dependents[i])) == NULL)
3360789Sahrens 			continue;
3361789Sahrens 
3362789Sahrens 		if ((ret = func(child, data)) != 0)
3363789Sahrens 			break;
3364789Sahrens 	}
3365789Sahrens 
3366789Sahrens 	for (i = 0; i < count; i++)
3367789Sahrens 		free(dependents[i]);
3368789Sahrens 	free(dependents);
3369789Sahrens 
3370789Sahrens 	return (ret);
3371789Sahrens }
3372789Sahrens 
3373789Sahrens /*
3374789Sahrens  * Renames the given dataset.
3375789Sahrens  */
3376789Sahrens int
33774490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3378789Sahrens {
3379789Sahrens 	int ret;
3380789Sahrens 	zfs_cmd_t zc = { 0 };
3381789Sahrens 	char *delim;
33824007Smmusante 	prop_changelist_t *cl = NULL;
33834007Smmusante 	zfs_handle_t *zhrp = NULL;
33844007Smmusante 	char *parentname = NULL;
3385789Sahrens 	char parent[ZFS_MAXNAMELEN];
33862082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33872082Seschrock 	char errbuf[1024];
3388789Sahrens 
3389789Sahrens 	/* if we have the same exact name, just return success */
3390789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3391789Sahrens 		return (0);
3392789Sahrens 
33932082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33942082Seschrock 	    "cannot rename to '%s'"), target);
33952082Seschrock 
3396789Sahrens 	/*
3397789Sahrens 	 * Make sure the target name is valid
3398789Sahrens 	 */
3399789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
34002665Snd150628 		if ((strchr(target, '@') == NULL) ||
34012665Snd150628 		    *target == '@') {
34022665Snd150628 			/*
34032665Snd150628 			 * Snapshot target name is abbreviated,
34042665Snd150628 			 * reconstruct full dataset name
34052665Snd150628 			 */
34062665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
34072665Snd150628 			    sizeof (parent));
34082665Snd150628 			delim = strchr(parent, '@');
34092665Snd150628 			if (strchr(target, '@') == NULL)
34102665Snd150628 				*(++delim) = '\0';
34112665Snd150628 			else
34122665Snd150628 				*delim = '\0';
34132665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
34142665Snd150628 			target = parent;
34152665Snd150628 		} else {
34162665Snd150628 			/*
34172665Snd150628 			 * Make sure we're renaming within the same dataset.
34182665Snd150628 			 */
34192665Snd150628 			delim = strchr(target, '@');
34202665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
34212665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
34222665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34232665Snd150628 				    "snapshots must be part of same "
34242665Snd150628 				    "dataset"));
34252665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
34263912Slling 				    errbuf));
34272665Snd150628 			}
3428789Sahrens 		}
34295326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
34302665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3431789Sahrens 	} else {
34324007Smmusante 		if (recursive) {
34334007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34344007Smmusante 			    "recursive rename must be a snapshot"));
34354007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
34364007Smmusante 		}
34374007Smmusante 
34385326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
34392665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
34402676Seschrock 		uint64_t unused;
34412676Seschrock 
3442789Sahrens 		/* validate parents */
34434490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3444789Sahrens 			return (-1);
3445789Sahrens 
3446789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3447789Sahrens 
3448789Sahrens 		/* make sure we're in the same pool */
3449789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3450789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3451789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
34522082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34532082Seschrock 			    "datasets must be within same pool"));
34542082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3455789Sahrens 		}
34562440Snd150628 
34572440Snd150628 		/* new name cannot be a child of the current dataset name */
34582440Snd150628 		if (strncmp(parent, zhp->zfs_name,
34593912Slling 		    strlen(zhp->zfs_name)) == 0) {
34602440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34612440Snd150628 			    "New dataset name cannot be a descendent of "
34622440Snd150628 			    "current dataset name"));
34632440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
34642440Snd150628 		}
3465789Sahrens 	}
3466789Sahrens 
34672082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
34682082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
34692082Seschrock 
3470789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3471789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
34722082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34732082Seschrock 		    "dataset is used in a non-global zone"));
34742082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3475789Sahrens 	}
3476789Sahrens 
34774007Smmusante 	if (recursive) {
34784007Smmusante 		struct destroydata dd;
34794007Smmusante 
34804183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
34814183Smmusante 		if (parentname == NULL) {
34824183Smmusante 			ret = -1;
34834183Smmusante 			goto error;
34844183Smmusante 		}
34854007Smmusante 		delim = strchr(parentname, '@');
34864007Smmusante 		*delim = '\0';
34875094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
34884007Smmusante 		if (zhrp == NULL) {
34894183Smmusante 			ret = -1;
34904183Smmusante 			goto error;
34914007Smmusante 		}
34924007Smmusante 
34934007Smmusante 		dd.snapname = delim + 1;
34944007Smmusante 		dd.gotone = B_FALSE;
34954183Smmusante 		dd.closezhp = B_TRUE;
34964007Smmusante 
34974007Smmusante 		/* We remove any zvol links prior to renaming them */
34984007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
34994007Smmusante 		if (ret) {
35004007Smmusante 			goto error;
35014007Smmusante 		}
35024007Smmusante 	} else {
35037366STim.Haley@Sun.COM 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
35044007Smmusante 			return (-1);
35054007Smmusante 
35064007Smmusante 		if (changelist_haszonedchild(cl)) {
35074007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35084007Smmusante 			    "child dataset with inherited mountpoint is used "
35094007Smmusante 			    "in a non-global zone"));
35104007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
35114007Smmusante 			goto error;
35124007Smmusante 		}
35134007Smmusante 
35144007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
35154007Smmusante 			goto error;
3516789Sahrens 	}
3517789Sahrens 
35182676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3519789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3520789Sahrens 	else
3521789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3522789Sahrens 
35232665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35242676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
35252665Snd150628 
35264007Smmusante 	zc.zc_cookie = recursive;
35274007Smmusante 
35284543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
35294007Smmusante 		/*
35304007Smmusante 		 * if it was recursive, the one that actually failed will
35314007Smmusante 		 * be in zc.zc_name
35324007Smmusante 		 */
35334007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35345367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
35354007Smmusante 
35364007Smmusante 		if (recursive && errno == EEXIST) {
35374007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35384007Smmusante 			    "a child dataset already has a snapshot "
35394007Smmusante 			    "with the new name"));
35404801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
35414007Smmusante 		} else {
35424007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
35434007Smmusante 		}
3544789Sahrens 
3545789Sahrens 		/*
3546789Sahrens 		 * On failure, we still want to remount any filesystems that
3547789Sahrens 		 * were previously mounted, so we don't alter the system state.
3548789Sahrens 		 */
35494007Smmusante 		if (recursive) {
35504007Smmusante 			struct createdata cd;
35514007Smmusante 
35524007Smmusante 			/* only create links for datasets that had existed */
35534007Smmusante 			cd.cd_snapname = delim + 1;
35544007Smmusante 			cd.cd_ifexists = B_TRUE;
35554007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
35564007Smmusante 			    &cd);
35574007Smmusante 		} else {
35584007Smmusante 			(void) changelist_postfix(cl);
35594007Smmusante 		}
3560789Sahrens 	} else {
35614007Smmusante 		if (recursive) {
35624007Smmusante 			struct createdata cd;
35634007Smmusante 
35644007Smmusante 			/* only create links for datasets that had existed */
35654007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
35664007Smmusante 			cd.cd_ifexists = B_TRUE;
35674007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
35684007Smmusante 			    &cd);
35694007Smmusante 		} else {
35704007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
35714007Smmusante 			ret = changelist_postfix(cl);
35724007Smmusante 		}
3573789Sahrens 	}
3574789Sahrens 
3575789Sahrens error:
35764007Smmusante 	if (parentname) {
35774007Smmusante 		free(parentname);
35784007Smmusante 	}
35794007Smmusante 	if (zhrp) {
35804007Smmusante 		zfs_close(zhrp);
35814007Smmusante 	}
35824007Smmusante 	if (cl) {
35834007Smmusante 		changelist_free(cl);
35844007Smmusante 	}
3585789Sahrens 	return (ret);
3586789Sahrens }
3587789Sahrens 
3588789Sahrens /*
3589789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3590789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3591789Sahrens  */
3592789Sahrens int
35932082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3594789Sahrens {
35954007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
35964007Smmusante }
35974007Smmusante 
35984007Smmusante static int
35994007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
36004007Smmusante {
3601789Sahrens 	zfs_cmd_t zc = { 0 };
36022082Seschrock 	di_devlink_handle_t dhdl;
36034543Smarks 	priv_set_t *priv_effective;
36044543Smarks 	int privileged;
3605789Sahrens 
3606789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3607789Sahrens 
3608789Sahrens 	/*
3609789Sahrens 	 * Issue the appropriate ioctl.
3610789Sahrens 	 */
36112082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3612789Sahrens 		switch (errno) {
3613789Sahrens 		case EEXIST:
3614789Sahrens 			/*
3615789Sahrens 			 * Silently ignore the case where the link already
3616789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3617789Sahrens 			 * times without errors.
3618789Sahrens 			 */
3619789Sahrens 			return (0);
3620789Sahrens 
36214007Smmusante 		case ENOENT:
36224007Smmusante 			/*
36234007Smmusante 			 * Dataset does not exist in the kernel.  If we
36244007Smmusante 			 * don't care (see zfs_rename), then ignore the
36254007Smmusante 			 * error quietly.
36264007Smmusante 			 */
36274007Smmusante 			if (ifexists) {
36284007Smmusante 				return (0);
36294007Smmusante 			}
36304007Smmusante 
36314007Smmusante 			/* FALLTHROUGH */
36324007Smmusante 
3633789Sahrens 		default:
36343237Slling 			return (zfs_standard_error_fmt(hdl, errno,
36352082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
36362082Seschrock 			    "for '%s'"), dataset));
3637789Sahrens 		}
3638789Sahrens 	}
3639789Sahrens 
3640789Sahrens 	/*
36414543Smarks 	 * If privileged call devfsadm and wait for the links to
36424543Smarks 	 * magically appear.
36434543Smarks 	 * Otherwise, print out an informational message.
3644789Sahrens 	 */
36454543Smarks 
36464543Smarks 	priv_effective = priv_allocset();
36474543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
36484543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
36494543Smarks 	priv_freeset(priv_effective);
36504543Smarks 
36514543Smarks 	if (privileged) {
36524543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
36534543Smarks 		    DI_MAKE_LINK)) == NULL) {
36544543Smarks 			zfs_error_aux(hdl, strerror(errno));
36557301SEric.Taylor@Sun.COM 			(void) zfs_error_fmt(hdl, errno,
36564543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
36574543Smarks 			    "for '%s'"), dataset);
36584543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
36594543Smarks 			return (-1);
36604543Smarks 		} else {
36614543Smarks 			(void) di_devlink_fini(&dhdl);
36624543Smarks 		}
3663789Sahrens 	} else {
36644543Smarks 		char pathname[MAXPATHLEN];
36654543Smarks 		struct stat64 statbuf;
36664543Smarks 		int i;
36674543Smarks 
36684543Smarks #define	MAX_WAIT	10
36694543Smarks 
36704543Smarks 		/*
36714543Smarks 		 * This is the poor mans way of waiting for the link
36724543Smarks 		 * to show up.  If after 10 seconds we still don't
36734543Smarks 		 * have it, then print out a message.
36744543Smarks 		 */
36754543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
36764543Smarks 		    dataset);
36774543Smarks 
36784543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
36794543Smarks 			if (stat64(pathname, &statbuf) == 0)
36804543Smarks 				break;
36814543Smarks 			(void) sleep(1);
36824543Smarks 		}
36834543Smarks 		if (i == MAX_WAIT)
36844543Smarks 			(void) printf(gettext("%s may not be immediately "
36854543Smarks 			    "available\n"), pathname);
3686789Sahrens 	}
3687789Sahrens 
3688789Sahrens 	return (0);
3689789Sahrens }
3690789Sahrens 
3691789Sahrens /*
3692789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
3693789Sahrens  */
3694789Sahrens int
36952082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3696789Sahrens {
3697789Sahrens 	zfs_cmd_t zc = { 0 };
3698789Sahrens 
3699789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3700789Sahrens 
37012082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3702789Sahrens 		switch (errno) {
3703789Sahrens 		case ENXIO:
3704789Sahrens 			/*
3705789Sahrens 			 * Silently ignore the case where the link no longer
3706789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
3707789Sahrens 			 * times without errors.
3708789Sahrens 			 */
3709789Sahrens 			return (0);
3710789Sahrens 
3711789Sahrens 		default:
37123237Slling 			return (zfs_standard_error_fmt(hdl, errno,
37132082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
37142082Seschrock 			    "links for '%s'"), dataset));
3715789Sahrens 		}
3716789Sahrens 	}
3717789Sahrens 
3718789Sahrens 	return (0);
3719789Sahrens }
37202676Seschrock 
37212676Seschrock nvlist_t *
37222676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
37232676Seschrock {
37242676Seschrock 	return (zhp->zfs_user_props);
37252676Seschrock }
37262676Seschrock 
37272676Seschrock /*
37283912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
37293912Slling  * display, and their maximum widths.  This does two main things:
37303912Slling  *
37313912Slling  *      - If this is a list of all properties, then expand the list to include
37323912Slling  *        all native properties, and set a flag so that for each dataset we look
37333912Slling  *        for new unique user properties and add them to the list.
37343912Slling  *
37353912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
37363912Slling  *        so that we can size the column appropriately.
37373912Slling  */
37383912Slling int
37395094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
37403912Slling {
37413912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37425094Slling 	zprop_list_t *entry;
37435094Slling 	zprop_list_t **last, **start;
37443912Slling 	nvlist_t *userprops, *propval;
37453912Slling 	nvpair_t *elem;
37463912Slling 	char *strval;
37473912Slling 	char buf[ZFS_MAXPROPLEN];
37483912Slling 
37495094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
37503912Slling 		return (-1);
37512676Seschrock 
37522676Seschrock 	userprops = zfs_get_user_props(zhp);
37532676Seschrock 
37542676Seschrock 	entry = *plp;
37552676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
37562676Seschrock 		/*
37572676Seschrock 		 * Go through and add any user properties as necessary.  We
37582676Seschrock 		 * start by incrementing our list pointer to the first
37592676Seschrock 		 * non-native property.
37602676Seschrock 		 */
37612676Seschrock 		start = plp;
37622676Seschrock 		while (*start != NULL) {
37635094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
37642676Seschrock 				break;
37652676Seschrock 			start = &(*start)->pl_next;
37662676Seschrock 		}
37672676Seschrock 
37682676Seschrock 		elem = NULL;
37692676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
37702676Seschrock 			/*
37712676Seschrock 			 * See if we've already found this property in our list.
37722676Seschrock 			 */
37732676Seschrock 			for (last = start; *last != NULL;
37742676Seschrock 			    last = &(*last)->pl_next) {
37752676Seschrock 				if (strcmp((*last)->pl_user_prop,
37762676Seschrock 				    nvpair_name(elem)) == 0)
37772676Seschrock 					break;
37782676Seschrock 			}
37792676Seschrock 
37802676Seschrock 			if (*last == NULL) {
37812676Seschrock 				if ((entry = zfs_alloc(hdl,
37825094Slling 				    sizeof (zprop_list_t))) == NULL ||
37832676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
37842676Seschrock 				    nvpair_name(elem)))) == NULL) {
37852676Seschrock 					free(entry);
37862676Seschrock 					return (-1);
37872676Seschrock 				}
37882676Seschrock 
37895094Slling 				entry->pl_prop = ZPROP_INVAL;
37902676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
37912676Seschrock 				entry->pl_all = B_TRUE;
37922676Seschrock 				*last = entry;
37932676Seschrock 			}
37942676Seschrock 		}
37952676Seschrock 	}
37962676Seschrock 
37972676Seschrock 	/*
37982676Seschrock 	 * Now go through and check the width of any non-fixed columns
37992676Seschrock 	 */
38002676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
38012676Seschrock 		if (entry->pl_fixed)
38022676Seschrock 			continue;
38032676Seschrock 
38045094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
38052676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
38062676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
38072676Seschrock 				if (strlen(buf) > entry->pl_width)
38082676Seschrock 					entry->pl_width = strlen(buf);
38092676Seschrock 			}
38102676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
38112676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
38122676Seschrock 			verify(nvlist_lookup_string(propval,
38135094Slling 			    ZPROP_VALUE, &strval) == 0);
38142676Seschrock 			if (strlen(strval) > entry->pl_width)
38152676Seschrock 				entry->pl_width = strlen(strval);
38162676Seschrock 		}
38172676Seschrock 	}
38182676Seschrock 
38192676Seschrock 	return (0);
38202676Seschrock }
38214543Smarks 
38224543Smarks int
38234543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
38244543Smarks {
38254543Smarks 	zfs_cmd_t zc = { 0 };
38264543Smarks 	nvlist_t *nvp;
38274543Smarks 	gid_t gid;
38284543Smarks 	uid_t uid;
38294543Smarks 	const gid_t *groups;
38304543Smarks 	int group_cnt;
38314543Smarks 	int error;
38324543Smarks 
38334543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
38344543Smarks 		return (no_memory(hdl));
38354543Smarks 
38364543Smarks 	uid = ucred_geteuid(cred);
38374543Smarks 	gid = ucred_getegid(cred);
38384543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
38394543Smarks 
38404543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
38414543Smarks 		return (1);
38424543Smarks 
38434543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
38444543Smarks 		nvlist_free(nvp);
38454543Smarks 		return (1);
38464543Smarks 	}
38474543Smarks 
38484543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
38494543Smarks 		nvlist_free(nvp);
38504543Smarks 		return (1);
38514543Smarks 	}
38524543Smarks 
38534543Smarks 	if (nvlist_add_uint32_array(nvp,
38544543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
38554543Smarks 		nvlist_free(nvp);
38564543Smarks 		return (1);
38574543Smarks 	}
38584543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
38594543Smarks 
38605094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
38614543Smarks 		return (-1);
38624543Smarks 
38634543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
38644543Smarks 	nvlist_free(nvp);
38654543Smarks 	return (error);
38664543Smarks }
38674543Smarks 
38684543Smarks int
38694543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
38708845Samw@Sun.COM     char *resource, void *export, void *sharetab,
38718845Samw@Sun.COM     int sharemax, zfs_share_op_t operation)
38724543Smarks {
38734543Smarks 	zfs_cmd_t zc = { 0 };
38744543Smarks 	int error;
38754543Smarks 
38764543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
38774543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
38788845Samw@Sun.COM 	if (resource)
38798845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
38804543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
38814543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
38825331Samw 	zc.zc_share.z_sharetype = operation;
38834543Smarks 	zc.zc_share.z_sharemax = sharemax;
38844543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
38854543Smarks 	return (error);
38864543Smarks }
38878802SSanjeev.Bagewadi@Sun.COM 
38888802SSanjeev.Bagewadi@Sun.COM void
38898802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
38908802SSanjeev.Bagewadi@Sun.COM {
38918802SSanjeev.Bagewadi@Sun.COM 	nvpair_t *curr;
38928802SSanjeev.Bagewadi@Sun.COM 
38938802SSanjeev.Bagewadi@Sun.COM 	/*
38948802SSanjeev.Bagewadi@Sun.COM 	 * Keep a reference to the props-table against which we prune the
38958802SSanjeev.Bagewadi@Sun.COM 	 * properties.
38968802SSanjeev.Bagewadi@Sun.COM 	 */
38978802SSanjeev.Bagewadi@Sun.COM 	zhp->zfs_props_table = props;
38988802SSanjeev.Bagewadi@Sun.COM 
38998802SSanjeev.Bagewadi@Sun.COM 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
39008802SSanjeev.Bagewadi@Sun.COM 
39018802SSanjeev.Bagewadi@Sun.COM 	while (curr) {
39028802SSanjeev.Bagewadi@Sun.COM 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
39038802SSanjeev.Bagewadi@Sun.COM 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
39048802SSanjeev.Bagewadi@Sun.COM 
39059396SMatthew.Ahrens@Sun.COM 		/*
39069396SMatthew.Ahrens@Sun.COM 		 * We leave user:props in the nvlist, so there will be
39079396SMatthew.Ahrens@Sun.COM 		 * some ZPROP_INVAL.  To be extra safe, don't prune
39089396SMatthew.Ahrens@Sun.COM 		 * those.
39099396SMatthew.Ahrens@Sun.COM 		 */
39109396SMatthew.Ahrens@Sun.COM 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
39118802SSanjeev.Bagewadi@Sun.COM 			(void) nvlist_remove(zhp->zfs_props,
39128802SSanjeev.Bagewadi@Sun.COM 			    nvpair_name(curr), nvpair_type(curr));
39138802SSanjeev.Bagewadi@Sun.COM 		curr = next;
39148802SSanjeev.Bagewadi@Sun.COM 	}
39158802SSanjeev.Bagewadi@Sun.COM }
39168845Samw@Sun.COM 
39178845Samw@Sun.COM static int
39188845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
39198845Samw@Sun.COM     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
39208845Samw@Sun.COM {
39218845Samw@Sun.COM 	zfs_cmd_t zc = { 0 };
39228845Samw@Sun.COM 	nvlist_t *nvlist = NULL;
39238845Samw@Sun.COM 	int error;
39248845Samw@Sun.COM 
39258845Samw@Sun.COM 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
39268845Samw@Sun.COM 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
39278845Samw@Sun.COM 	zc.zc_cookie = (uint64_t)cmd;
39288845Samw@Sun.COM 
39298845Samw@Sun.COM 	if (cmd == ZFS_SMB_ACL_RENAME) {
39308845Samw@Sun.COM 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
39318845Samw@Sun.COM 			(void) no_memory(hdl);
39328845Samw@Sun.COM 			return (NULL);
39338845Samw@Sun.COM 		}
39348845Samw@Sun.COM 	}
39358845Samw@Sun.COM 
39368845Samw@Sun.COM 	switch (cmd) {
39378845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
39388845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
39398845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
39408845Samw@Sun.COM 		break;
39418845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
39428845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
39438845Samw@Sun.COM 		    resource1) != 0) {
39448845Samw@Sun.COM 				(void) no_memory(hdl);
39458845Samw@Sun.COM 				return (-1);
39468845Samw@Sun.COM 		}
39478845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
39488845Samw@Sun.COM 		    resource2) != 0) {
39498845Samw@Sun.COM 				(void) no_memory(hdl);
39508845Samw@Sun.COM 				return (-1);
39518845Samw@Sun.COM 		}
39528845Samw@Sun.COM 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
39538845Samw@Sun.COM 			nvlist_free(nvlist);
39548845Samw@Sun.COM 			return (-1);
39558845Samw@Sun.COM 		}
39568845Samw@Sun.COM 		break;
39578845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
39588845Samw@Sun.COM 		break;
39598845Samw@Sun.COM 	default:
39608845Samw@Sun.COM 		return (-1);
39618845Samw@Sun.COM 	}
39628845Samw@Sun.COM 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
39638845Samw@Sun.COM 	if (nvlist)
39648845Samw@Sun.COM 		nvlist_free(nvlist);
39658845Samw@Sun.COM 	return (error);
39668845Samw@Sun.COM }
39678845Samw@Sun.COM 
39688845Samw@Sun.COM int
39698845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
39708845Samw@Sun.COM     char *path, char *resource)
39718845Samw@Sun.COM {
39728845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
39738845Samw@Sun.COM 	    resource, NULL));
39748845Samw@Sun.COM }
39758845Samw@Sun.COM 
39768845Samw@Sun.COM int
39778845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
39788845Samw@Sun.COM     char *path, char *resource)
39798845Samw@Sun.COM {
39808845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
39818845Samw@Sun.COM 	    resource, NULL));
39828845Samw@Sun.COM }
39838845Samw@Sun.COM 
39848845Samw@Sun.COM int
39858845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
39868845Samw@Sun.COM {
39878845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
39888845Samw@Sun.COM 	    NULL, NULL));
39898845Samw@Sun.COM }
39908845Samw@Sun.COM 
39918845Samw@Sun.COM int
39928845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
39938845Samw@Sun.COM     char *oldname, char *newname)
39948845Samw@Sun.COM {
39958845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
39968845Samw@Sun.COM 	    oldname, newname));
39978845Samw@Sun.COM }
39989396SMatthew.Ahrens@Sun.COM 
39999396SMatthew.Ahrens@Sun.COM int
40009396SMatthew.Ahrens@Sun.COM zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
40019396SMatthew.Ahrens@Sun.COM     zfs_userspace_cb_t func, void *arg)
40029396SMatthew.Ahrens@Sun.COM {
40039396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
40049396SMatthew.Ahrens@Sun.COM 	int error;
40059396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t buf[100];
40069396SMatthew.Ahrens@Sun.COM 
40079396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
40089396SMatthew.Ahrens@Sun.COM 
40099396SMatthew.Ahrens@Sun.COM 	zc.zc_objset_type = type;
40109396SMatthew.Ahrens@Sun.COM 	zc.zc_nvlist_dst = (uintptr_t)buf;
40119396SMatthew.Ahrens@Sun.COM 
40129396SMatthew.Ahrens@Sun.COM 	/* CONSTCOND */
40139396SMatthew.Ahrens@Sun.COM 	while (1) {
40149396SMatthew.Ahrens@Sun.COM 		zfs_useracct_t *zua = buf;
40159396SMatthew.Ahrens@Sun.COM 
40169396SMatthew.Ahrens@Sun.COM 		zc.zc_nvlist_dst_size = sizeof (buf);
40179396SMatthew.Ahrens@Sun.COM 		error = ioctl(zhp->zfs_hdl->libzfs_fd,
40189396SMatthew.Ahrens@Sun.COM 		    ZFS_IOC_USERSPACE_MANY, &zc);
40199396SMatthew.Ahrens@Sun.COM 		if (error || zc.zc_nvlist_dst_size == 0)
40209396SMatthew.Ahrens@Sun.COM 			break;
40219396SMatthew.Ahrens@Sun.COM 
40229396SMatthew.Ahrens@Sun.COM 		while (zc.zc_nvlist_dst_size > 0) {
40239554SMatthew.Ahrens@Sun.COM 			error = func(arg, zua->zu_domain, zua->zu_rid,
40249554SMatthew.Ahrens@Sun.COM 			    zua->zu_space);
40259554SMatthew.Ahrens@Sun.COM 			if (error != 0)
40269554SMatthew.Ahrens@Sun.COM 				return (error);
40279396SMatthew.Ahrens@Sun.COM 			zua++;
40289396SMatthew.Ahrens@Sun.COM 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
40299396SMatthew.Ahrens@Sun.COM 		}
40309396SMatthew.Ahrens@Sun.COM 	}
40319396SMatthew.Ahrens@Sun.COM 
40329396SMatthew.Ahrens@Sun.COM 	return (error);
40339396SMatthew.Ahrens@Sun.COM }
403410242Schris.kirby@sun.com 
403510242Schris.kirby@sun.com int
403610242Schris.kirby@sun.com zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
403710242Schris.kirby@sun.com     boolean_t recursive)
403810242Schris.kirby@sun.com {
403910242Schris.kirby@sun.com 	zfs_cmd_t zc = { 0 };
404010242Schris.kirby@sun.com 	libzfs_handle_t *hdl = zhp->zfs_hdl;
404110242Schris.kirby@sun.com 
404210242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
404310242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
404410242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_string, tag, sizeof (zc.zc_string));
404510242Schris.kirby@sun.com 	zc.zc_cookie = recursive;
404610242Schris.kirby@sun.com 
404710242Schris.kirby@sun.com 	if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
404810242Schris.kirby@sun.com 		char errbuf[ZFS_MAXNAMELEN+32];
404910242Schris.kirby@sun.com 
405010242Schris.kirby@sun.com 		/*
405110242Schris.kirby@sun.com 		 * if it was recursive, the one that actually failed will be in
405210242Schris.kirby@sun.com 		 * zc.zc_name.
405310242Schris.kirby@sun.com 		 */
405410242Schris.kirby@sun.com 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
405510242Schris.kirby@sun.com 		    "cannot hold '%s@%s'"), zc.zc_name, snapname);
405610242Schris.kirby@sun.com 		switch (errno) {
405710242Schris.kirby@sun.com 		case ENOTSUP:
405810242Schris.kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
405910242Schris.kirby@sun.com 			    "pool must be upgraded"));
406010242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
406110242Schris.kirby@sun.com 		case EINVAL:
406210242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
406310242Schris.kirby@sun.com 		case EEXIST:
406410242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
406510242Schris.kirby@sun.com 		default:
406610242Schris.kirby@sun.com 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
406710242Schris.kirby@sun.com 		}
406810242Schris.kirby@sun.com 	}
406910242Schris.kirby@sun.com 
407010242Schris.kirby@sun.com 	return (0);
407110242Schris.kirby@sun.com }
407210242Schris.kirby@sun.com 
407310242Schris.kirby@sun.com int
407410242Schris.kirby@sun.com zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
407510242Schris.kirby@sun.com     boolean_t recursive)
407610242Schris.kirby@sun.com {
407710242Schris.kirby@sun.com 	zfs_cmd_t zc = { 0 };
407810242Schris.kirby@sun.com 	libzfs_handle_t *hdl = zhp->zfs_hdl;
407910242Schris.kirby@sun.com 
408010242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
408110242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
408210242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_string, tag, sizeof (zc.zc_string));
408310242Schris.kirby@sun.com 	zc.zc_cookie = recursive;
408410242Schris.kirby@sun.com 
408510242Schris.kirby@sun.com 	if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
408610242Schris.kirby@sun.com 		char errbuf[ZFS_MAXNAMELEN+32];
408710242Schris.kirby@sun.com 
408810242Schris.kirby@sun.com 		/*
408910242Schris.kirby@sun.com 		 * if it was recursive, the one that actually failed will be in
409010242Schris.kirby@sun.com 		 * zc.zc_name.
409110242Schris.kirby@sun.com 		 */
409210242Schris.kirby@sun.com 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
409310242Schris.kirby@sun.com 		    "cannot release '%s@%s'"), zc.zc_name, snapname);
409410242Schris.kirby@sun.com 		switch (errno) {
409510242Schris.kirby@sun.com 		case ESRCH:
409610242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
409710242Schris.kirby@sun.com 		case ENOTSUP:
409810242Schris.kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
409910242Schris.kirby@sun.com 			    "pool must be upgraded"));
410010242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
410110242Schris.kirby@sun.com 		case EINVAL:
410210242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
410310242Schris.kirby@sun.com 		default:
410410242Schris.kirby@sun.com 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
410510242Schris.kirby@sun.com 		}
410610242Schris.kirby@sun.com 	}
410710242Schris.kirby@sun.com 
410810242Schris.kirby@sun.com 	return (0);
410910242Schris.kirby@sun.com }
4110