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 <ctype.h>
28789Sahrens #include <errno.h>
29789Sahrens #include <libintl.h>
30789Sahrens #include <math.h>
31789Sahrens #include <stdio.h>
32789Sahrens #include <stdlib.h>
33789Sahrens #include <strings.h>
34789Sahrens #include <unistd.h>
355367Sahrens #include <stddef.h>
36789Sahrens #include <zone.h>
372082Seschrock #include <fcntl.h>
38789Sahrens #include <sys/mntent.h>
391294Slling #include <sys/mount.h>
404543Smarks #include <priv.h>
414543Smarks #include <pwd.h>
424543Smarks #include <grp.h>
434543Smarks #include <stddef.h>
444543Smarks #include <ucred.h>
459396SMatthew.Ahrens@Sun.COM #include <idmap.h>
469396SMatthew.Ahrens@Sun.COM #include <aclutils.h>
4710160SMatthew.Ahrens@Sun.COM #include <directory.h>
48789Sahrens 
49789Sahrens #include <sys/spa.h>
502676Seschrock #include <sys/zap.h>
51789Sahrens #include <libzfs.h>
52789Sahrens 
53789Sahrens #include "zfs_namecheck.h"
54789Sahrens #include "zfs_prop.h"
55789Sahrens #include "libzfs_impl.h"
564543Smarks #include "zfs_deleg.h"
57789Sahrens 
589396SMatthew.Ahrens@Sun.COM static int userquota_propname_decode(const char *propname, boolean_t zoned,
599396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
604007Smmusante 
61789Sahrens /*
62789Sahrens  * Given a single type (not a mask of types), return the type in a human
63789Sahrens  * readable form.
64789Sahrens  */
65789Sahrens const char *
66789Sahrens zfs_type_to_name(zfs_type_t type)
67789Sahrens {
68789Sahrens 	switch (type) {
69789Sahrens 	case ZFS_TYPE_FILESYSTEM:
70789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
71789Sahrens 	case ZFS_TYPE_SNAPSHOT:
72789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
73789Sahrens 	case ZFS_TYPE_VOLUME:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
75789Sahrens 	}
76789Sahrens 
77789Sahrens 	return (NULL);
78789Sahrens }
79789Sahrens 
80789Sahrens /*
81789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
82789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
83789Sahrens  * We guess what the type would have been based on the path and the mask of
84789Sahrens  * acceptable types.
85789Sahrens  */
86789Sahrens static const char *
87789Sahrens path_to_str(const char *path, int types)
88789Sahrens {
89789Sahrens 	/*
90789Sahrens 	 * When given a single type, always report the exact type.
91789Sahrens 	 */
92789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
93789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
94789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
95789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
96789Sahrens 	if (types == ZFS_TYPE_VOLUME)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
98789Sahrens 
99789Sahrens 	/*
100789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
101789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
102789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
103789Sahrens 	 * snapshot attribute and try again.
104789Sahrens 	 */
105789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
106789Sahrens 		if (strchr(path, '@') != NULL)
107789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
108789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
109789Sahrens 	}
110789Sahrens 
111789Sahrens 	/*
112789Sahrens 	 * The user has requested either filesystems or volumes.
113789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
114789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
115789Sahrens 	 */
116789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
117789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
118789Sahrens 
119789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
120789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
121789Sahrens }
122789Sahrens 
123789Sahrens /*
124789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
1259396SMatthew.Ahrens@Sun.COM  * provide a more meaningful error message.  We call zfs_error_aux() to
1269396SMatthew.Ahrens@Sun.COM  * explain exactly why the name was not valid.
127789Sahrens  */
128789Sahrens static int
1295326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1305326Sek110237     boolean_t modifying)
131789Sahrens {
132789Sahrens 	namecheck_err_t why;
133789Sahrens 	char what;
134789Sahrens 
135789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1362082Seschrock 		if (hdl != NULL) {
137789Sahrens 			switch (why) {
1381003Slling 			case NAME_ERR_TOOLONG:
1392082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1402082Seschrock 				    "name is too long"));
1411003Slling 				break;
1421003Slling 
143789Sahrens 			case NAME_ERR_LEADING_SLASH:
1442082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1452082Seschrock 				    "leading slash in name"));
146789Sahrens 				break;
147789Sahrens 
148789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1492082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1502082Seschrock 				    "empty component in name"));
151789Sahrens 				break;
152789Sahrens 
153789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1542082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1552082Seschrock 				    "trailing slash in name"));
156789Sahrens 				break;
157789Sahrens 
158789Sahrens 			case NAME_ERR_INVALCHAR:
1592082Seschrock 				zfs_error_aux(hdl,
160789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1612082Seschrock 				    "'%c' in name"), what);
162789Sahrens 				break;
163789Sahrens 
164789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1652082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1662082Seschrock 				    "multiple '@' delimiters in name"));
167789Sahrens 				break;
1682856Snd150628 
1692856Snd150628 			case NAME_ERR_NOLETTER:
1702856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1712856Snd150628 				    "pool doesn't begin with a letter"));
1722856Snd150628 				break;
1732856Snd150628 
1742856Snd150628 			case NAME_ERR_RESERVED:
1752856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1762856Snd150628 				    "name is reserved"));
1772856Snd150628 				break;
1782856Snd150628 
1792856Snd150628 			case NAME_ERR_DISKLIKE:
1802856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1812856Snd150628 				    "reserved disk name"));
1822856Snd150628 				break;
183789Sahrens 			}
184789Sahrens 		}
185789Sahrens 
186789Sahrens 		return (0);
187789Sahrens 	}
188789Sahrens 
189789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1902082Seschrock 		if (hdl != NULL)
1912082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1922082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
193789Sahrens 		return (0);
194789Sahrens 	}
195789Sahrens 
1962199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1972199Sahrens 		if (hdl != NULL)
1982199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1993413Smmusante 			    "missing '@' delimiter in snapshot name"));
2002199Sahrens 		return (0);
2012199Sahrens 	}
2022199Sahrens 
2035326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2045326Sek110237 		if (hdl != NULL)
2055326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2065326Sek110237 			    "invalid character %c in name"), '%');
2075326Sek110237 		return (0);
2085326Sek110237 	}
2095326Sek110237 
2102082Seschrock 	return (-1);
211789Sahrens }
212789Sahrens 
213789Sahrens int
214789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
215789Sahrens {
2166423Sgw25295 	if (type == ZFS_TYPE_POOL)
2176423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2185326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
219789Sahrens }
220789Sahrens 
221789Sahrens /*
2222676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2232676Seschrock  * properties into a separate nvlist.
2242676Seschrock  */
2254217Seschrock static nvlist_t *
2264217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2272676Seschrock {
2282676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2292676Seschrock 	nvpair_t *elem;
2302676Seschrock 	nvlist_t *propval;
2314217Seschrock 	nvlist_t *nvl;
2324217Seschrock 
2334217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2344217Seschrock 		(void) no_memory(hdl);
2354217Seschrock 		return (NULL);
2364217Seschrock 	}
2372676Seschrock 
2382676Seschrock 	elem = NULL;
2394217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2402676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2412676Seschrock 			continue;
2422676Seschrock 
2432676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2444217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2454217Seschrock 			nvlist_free(nvl);
2464217Seschrock 			(void) no_memory(hdl);
2474217Seschrock 			return (NULL);
2484217Seschrock 		}
2492676Seschrock 	}
2502676Seschrock 
2514217Seschrock 	return (nvl);
2522676Seschrock }
2532676Seschrock 
2546865Srm160521 static zpool_handle_t *
2556865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2566865Srm160521 {
2576865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2586865Srm160521 	zpool_handle_t *zph;
2596865Srm160521 
2606865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2616865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2626865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2636865Srm160521 		hdl->libzfs_pool_handles = zph;
2646865Srm160521 	}
2656865Srm160521 	return (zph);
2666865Srm160521 }
2676865Srm160521 
2686865Srm160521 static zpool_handle_t *
2696865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2706865Srm160521 {
2716865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2726865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2736865Srm160521 
2746865Srm160521 	while ((zph != NULL) &&
2756865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2766865Srm160521 		zph = zph->zpool_next;
2776865Srm160521 	return (zph);
2786865Srm160521 }
2796865Srm160521 
2806865Srm160521 /*
2816865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2826865Srm160521  * If a handle to that pool already exists then that handle is returned.
2836865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2846865Srm160521  */
2856865Srm160521 static zpool_handle_t *
2866865Srm160521 zpool_handle(zfs_handle_t *zhp)
2876865Srm160521 {
2886865Srm160521 	char *pool_name;
2896865Srm160521 	int len;
2906865Srm160521 	zpool_handle_t *zph;
2916865Srm160521 
2926865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2936865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2946865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2956865Srm160521 
2966865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
2976865Srm160521 	if (zph == NULL)
2986865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
2996865Srm160521 
3006865Srm160521 	free(pool_name);
3016865Srm160521 	return (zph);
3026865Srm160521 }
3036865Srm160521 
3046865Srm160521 void
3056865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3066865Srm160521 {
3076865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3086865Srm160521 
3096865Srm160521 	while (zph != NULL) {
3106865Srm160521 		next = zph->zpool_next;
3116865Srm160521 		zpool_close(zph);
3126865Srm160521 		zph = next;
3136865Srm160521 	}
3146865Srm160521 	hdl->libzfs_pool_handles = NULL;
3156865Srm160521 }
3166865Srm160521 
3172676Seschrock /*
318789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
319789Sahrens  */
320789Sahrens static int
3218228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
322789Sahrens {
3232676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3248228SEric.Taylor@Sun.COM 
3258228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
3268228SEric.Taylor@Sun.COM 
3278228SEric.Taylor@Sun.COM 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3281356Seschrock 		if (errno == ENOMEM) {
3298228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
3302082Seschrock 				return (-1);
3312676Seschrock 			}
3321356Seschrock 		} else {
3331356Seschrock 			return (-1);
3341356Seschrock 		}
3351356Seschrock 	}
3368228SEric.Taylor@Sun.COM 	return (0);
3378228SEric.Taylor@Sun.COM }
3388228SEric.Taylor@Sun.COM 
3398228SEric.Taylor@Sun.COM static int
3408228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
3418228SEric.Taylor@Sun.COM {
3428228SEric.Taylor@Sun.COM 	nvlist_t *allprops, *userprops;
3438228SEric.Taylor@Sun.COM 
3448228SEric.Taylor@Sun.COM 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
3458228SEric.Taylor@Sun.COM 
3468228SEric.Taylor@Sun.COM 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
3472082Seschrock 		return (-1);
3482082Seschrock 	}
349789Sahrens 
3509396SMatthew.Ahrens@Sun.COM 	/*
3519396SMatthew.Ahrens@Sun.COM 	 * XXX Why do we store the user props separately, in addition to
3529396SMatthew.Ahrens@Sun.COM 	 * storing them in zfs_props?
3539396SMatthew.Ahrens@Sun.COM 	 */
3544217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3554217Seschrock 		nvlist_free(allprops);
3562676Seschrock 		return (-1);
3574217Seschrock 	}
3584217Seschrock 
3594217Seschrock 	nvlist_free(zhp->zfs_props);
3604217Seschrock 	nvlist_free(zhp->zfs_user_props);
3614217Seschrock 
3624217Seschrock 	zhp->zfs_props = allprops;
3634217Seschrock 	zhp->zfs_user_props = userprops;
3642082Seschrock 
365789Sahrens 	return (0);
366789Sahrens }
367789Sahrens 
3688228SEric.Taylor@Sun.COM static int
3698228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp)
3708228SEric.Taylor@Sun.COM {
3718228SEric.Taylor@Sun.COM 	int rc = 0;
3728228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
3738228SEric.Taylor@Sun.COM 
3748228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
3758228SEric.Taylor@Sun.COM 		return (-1);
3768228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) != 0)
3778228SEric.Taylor@Sun.COM 		rc = -1;
3788228SEric.Taylor@Sun.COM 	else if (put_stats_zhdl(zhp, &zc) != 0)
3798228SEric.Taylor@Sun.COM 		rc = -1;
3808228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
3818228SEric.Taylor@Sun.COM 	return (rc);
3828228SEric.Taylor@Sun.COM }
3838228SEric.Taylor@Sun.COM 
384789Sahrens /*
385789Sahrens  * Refresh the properties currently stored in the handle.
386789Sahrens  */
387789Sahrens void
388789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
389789Sahrens {
390789Sahrens 	(void) get_stats(zhp);
391789Sahrens }
392789Sahrens 
393789Sahrens /*
394789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
395789Sahrens  * zfs_iter_* to create child handles on the fly.
396789Sahrens  */
3978228SEric.Taylor@Sun.COM static int
3988228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
399789Sahrens {
40010298SMatthew.Ahrens@Sun.COM 	if (put_stats_zhdl(zhp, zc) != 0)
4018228SEric.Taylor@Sun.COM 		return (-1);
4021758Sahrens 
403789Sahrens 	/*
404789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
405789Sahrens 	 * the high-level type.
406789Sahrens 	 */
4072885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4082885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4092885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4102885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4112885Sahrens 	else
4122885Sahrens 		abort();
4132885Sahrens 
414789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
415789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
416789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
417789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
418789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
419789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
420789Sahrens 	else
4212082Seschrock 		abort();	/* we should never see any other types */
422789Sahrens 
4236865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
4248228SEric.Taylor@Sun.COM 	return (0);
4258228SEric.Taylor@Sun.COM }
4268228SEric.Taylor@Sun.COM 
4278228SEric.Taylor@Sun.COM zfs_handle_t *
4288228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path)
4298228SEric.Taylor@Sun.COM {
4308228SEric.Taylor@Sun.COM 	zfs_cmd_t zc = { 0 };
4318228SEric.Taylor@Sun.COM 
4328228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4338228SEric.Taylor@Sun.COM 
4348228SEric.Taylor@Sun.COM 	if (zhp == NULL)
4358228SEric.Taylor@Sun.COM 		return (NULL);
4368228SEric.Taylor@Sun.COM 
4378228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
4388228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
4398228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
4408228SEric.Taylor@Sun.COM 		free(zhp);
4418228SEric.Taylor@Sun.COM 		return (NULL);
4428228SEric.Taylor@Sun.COM 	}
4438228SEric.Taylor@Sun.COM 	if (get_stats_ioctl(zhp, &zc) == -1) {
4448228SEric.Taylor@Sun.COM 		zcmd_free_nvlists(&zc);
4458228SEric.Taylor@Sun.COM 		free(zhp);
4468228SEric.Taylor@Sun.COM 		return (NULL);
4478228SEric.Taylor@Sun.COM 	}
4488228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, &zc) == -1) {
4498228SEric.Taylor@Sun.COM 		free(zhp);
4508228SEric.Taylor@Sun.COM 		zhp = NULL;
4518228SEric.Taylor@Sun.COM 	}
4528228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
4538228SEric.Taylor@Sun.COM 	return (zhp);
4548228SEric.Taylor@Sun.COM }
4558228SEric.Taylor@Sun.COM 
4568228SEric.Taylor@Sun.COM static zfs_handle_t *
4578228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
4588228SEric.Taylor@Sun.COM {
4598228SEric.Taylor@Sun.COM 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4608228SEric.Taylor@Sun.COM 
4618228SEric.Taylor@Sun.COM 	if (zhp == NULL)
4628228SEric.Taylor@Sun.COM 		return (NULL);
4638228SEric.Taylor@Sun.COM 
4648228SEric.Taylor@Sun.COM 	zhp->zfs_hdl = hdl;
4658228SEric.Taylor@Sun.COM 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
4668228SEric.Taylor@Sun.COM 	if (make_dataset_handle_common(zhp, zc) == -1) {
4678228SEric.Taylor@Sun.COM 		free(zhp);
4688228SEric.Taylor@Sun.COM 		return (NULL);
4698228SEric.Taylor@Sun.COM 	}
470789Sahrens 	return (zhp);
471789Sahrens }
472789Sahrens 
473789Sahrens /*
474789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
475789Sahrens  * argument is a mask of acceptable types.  The function will print an
476789Sahrens  * appropriate error message and return NULL if it can't be opened.
477789Sahrens  */
478789Sahrens zfs_handle_t *
4792082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
480789Sahrens {
481789Sahrens 	zfs_handle_t *zhp;
4822082Seschrock 	char errbuf[1024];
4832082Seschrock 
4842082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4852082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
486789Sahrens 
487789Sahrens 	/*
4882082Seschrock 	 * Validate the name before we even try to open it.
489789Sahrens 	 */
4905326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4912082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4922082Seschrock 		    "invalid dataset name"));
4932082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
494789Sahrens 		return (NULL);
495789Sahrens 	}
496789Sahrens 
497789Sahrens 	/*
498789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
499789Sahrens 	 */
500789Sahrens 	errno = 0;
5012082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5023237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
503789Sahrens 		return (NULL);
504789Sahrens 	}
505789Sahrens 
506789Sahrens 	if (!(types & zhp->zfs_type)) {
5072082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5082142Seschrock 		zfs_close(zhp);
509789Sahrens 		return (NULL);
510789Sahrens 	}
511789Sahrens 
512789Sahrens 	return (zhp);
513789Sahrens }
514789Sahrens 
515789Sahrens /*
516789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
517789Sahrens  */
518789Sahrens void
519789Sahrens zfs_close(zfs_handle_t *zhp)
520789Sahrens {
521789Sahrens 	if (zhp->zfs_mntopts)
522789Sahrens 		free(zhp->zfs_mntopts);
5232676Seschrock 	nvlist_free(zhp->zfs_props);
5242676Seschrock 	nvlist_free(zhp->zfs_user_props);
525789Sahrens 	free(zhp);
526789Sahrens }
527789Sahrens 
5288228SEric.Taylor@Sun.COM typedef struct mnttab_node {
5298228SEric.Taylor@Sun.COM 	struct mnttab mtn_mt;
5308228SEric.Taylor@Sun.COM 	avl_node_t mtn_node;
5318228SEric.Taylor@Sun.COM } mnttab_node_t;
5328228SEric.Taylor@Sun.COM 
5338228SEric.Taylor@Sun.COM static int
5348228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
5358228SEric.Taylor@Sun.COM {
5368228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn1 = arg1;
5378228SEric.Taylor@Sun.COM 	const mnttab_node_t *mtn2 = arg2;
5388228SEric.Taylor@Sun.COM 	int rv;
5398228SEric.Taylor@Sun.COM 
5408228SEric.Taylor@Sun.COM 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
5418228SEric.Taylor@Sun.COM 
5428228SEric.Taylor@Sun.COM 	if (rv == 0)
5438228SEric.Taylor@Sun.COM 		return (0);
5448228SEric.Taylor@Sun.COM 	return (rv > 0 ? 1 : -1);
5458228SEric.Taylor@Sun.COM }
5468228SEric.Taylor@Sun.COM 
5478228SEric.Taylor@Sun.COM void
5488228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl)
5498228SEric.Taylor@Sun.COM {
5508228SEric.Taylor@Sun.COM 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
5518228SEric.Taylor@Sun.COM 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
5528228SEric.Taylor@Sun.COM 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
5538811SEric.Taylor@Sun.COM }
5548811SEric.Taylor@Sun.COM 
5558811SEric.Taylor@Sun.COM void
5568811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl)
5578811SEric.Taylor@Sun.COM {
5588811SEric.Taylor@Sun.COM 	struct mnttab entry;
5598228SEric.Taylor@Sun.COM 
5608228SEric.Taylor@Sun.COM 	rewind(hdl->libzfs_mnttab);
5618228SEric.Taylor@Sun.COM 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
5628228SEric.Taylor@Sun.COM 		mnttab_node_t *mtn;
5638228SEric.Taylor@Sun.COM 
5648228SEric.Taylor@Sun.COM 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
5658228SEric.Taylor@Sun.COM 			continue;
5668228SEric.Taylor@Sun.COM 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
5678228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
5688228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
5698228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
5708228SEric.Taylor@Sun.COM 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
5718228SEric.Taylor@Sun.COM 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
5728228SEric.Taylor@Sun.COM 	}
5738228SEric.Taylor@Sun.COM }
5748228SEric.Taylor@Sun.COM 
5758228SEric.Taylor@Sun.COM void
5768228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl)
5778228SEric.Taylor@Sun.COM {
5788228SEric.Taylor@Sun.COM 	void *cookie = NULL;
5798228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
5808228SEric.Taylor@Sun.COM 
5818228SEric.Taylor@Sun.COM 	while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
5828228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_special);
5838228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mountp);
5848228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_fstype);
5858228SEric.Taylor@Sun.COM 		free(mtn->mtn_mt.mnt_mntopts);
5868228SEric.Taylor@Sun.COM 		free(mtn);
5878228SEric.Taylor@Sun.COM 	}
5888228SEric.Taylor@Sun.COM 	avl_destroy(&hdl->libzfs_mnttab_cache);
5898228SEric.Taylor@Sun.COM }
5908228SEric.Taylor@Sun.COM 
5918811SEric.Taylor@Sun.COM void
5928811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
5938811SEric.Taylor@Sun.COM {
5948811SEric.Taylor@Sun.COM 	hdl->libzfs_mnttab_enable = enable;
5958811SEric.Taylor@Sun.COM }
5968811SEric.Taylor@Sun.COM 
5978228SEric.Taylor@Sun.COM int
5988228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
5998228SEric.Taylor@Sun.COM     struct mnttab *entry)
6008228SEric.Taylor@Sun.COM {
6018228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6028228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6038228SEric.Taylor@Sun.COM 
6048811SEric.Taylor@Sun.COM 	if (!hdl->libzfs_mnttab_enable) {
6058811SEric.Taylor@Sun.COM 		struct mnttab srch = { 0 };
6068811SEric.Taylor@Sun.COM 
6078811SEric.Taylor@Sun.COM 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
6088811SEric.Taylor@Sun.COM 			libzfs_mnttab_fini(hdl);
6098811SEric.Taylor@Sun.COM 		rewind(hdl->libzfs_mnttab);
6108811SEric.Taylor@Sun.COM 		srch.mnt_special = (char *)fsname;
6118811SEric.Taylor@Sun.COM 		srch.mnt_fstype = MNTTYPE_ZFS;
6128811SEric.Taylor@Sun.COM 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
6138811SEric.Taylor@Sun.COM 			return (0);
6148811SEric.Taylor@Sun.COM 		else
6158811SEric.Taylor@Sun.COM 			return (ENOENT);
6168811SEric.Taylor@Sun.COM 	}
6178811SEric.Taylor@Sun.COM 
6188228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6198811SEric.Taylor@Sun.COM 		libzfs_mnttab_update(hdl);
6208228SEric.Taylor@Sun.COM 
6218228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6228228SEric.Taylor@Sun.COM 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
6238228SEric.Taylor@Sun.COM 	if (mtn) {
6248228SEric.Taylor@Sun.COM 		*entry = mtn->mtn_mt;
6258228SEric.Taylor@Sun.COM 		return (0);
6268228SEric.Taylor@Sun.COM 	}
6278228SEric.Taylor@Sun.COM 	return (ENOENT);
6288228SEric.Taylor@Sun.COM }
6298228SEric.Taylor@Sun.COM 
6308228SEric.Taylor@Sun.COM void
6318228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
6328228SEric.Taylor@Sun.COM     const char *mountp, const char *mntopts)
6338228SEric.Taylor@Sun.COM {
6348228SEric.Taylor@Sun.COM 	mnttab_node_t *mtn;
6358228SEric.Taylor@Sun.COM 
6368228SEric.Taylor@Sun.COM 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
6378228SEric.Taylor@Sun.COM 		return;
6388228SEric.Taylor@Sun.COM 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
6398228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
6408228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
6418228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
6428228SEric.Taylor@Sun.COM 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
6438228SEric.Taylor@Sun.COM 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
6448228SEric.Taylor@Sun.COM }
6458228SEric.Taylor@Sun.COM 
6468228SEric.Taylor@Sun.COM void
6478228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
6488228SEric.Taylor@Sun.COM {
6498228SEric.Taylor@Sun.COM 	mnttab_node_t find;
6508228SEric.Taylor@Sun.COM 	mnttab_node_t *ret;
6518228SEric.Taylor@Sun.COM 
6528228SEric.Taylor@Sun.COM 	find.mtn_mt.mnt_special = (char *)fsname;
6538228SEric.Taylor@Sun.COM 	if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
6548228SEric.Taylor@Sun.COM 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
6558228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_special);
6568228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mountp);
6578228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_fstype);
6588228SEric.Taylor@Sun.COM 		free(ret->mtn_mt.mnt_mntopts);
6598228SEric.Taylor@Sun.COM 		free(ret);
6608228SEric.Taylor@Sun.COM 	}
6618228SEric.Taylor@Sun.COM }
6628228SEric.Taylor@Sun.COM 
6635713Srm160521 int
6645713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
6655713Srm160521 {
6666865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
6676865Srm160521 
6685713Srm160521 	if (zpool_handle == NULL)
6695713Srm160521 		return (-1);
6705713Srm160521 
6715713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
6725713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
6735713Srm160521 	return (0);
6745713Srm160521 }
6755713Srm160521 
6765713Srm160521 /*
6775713Srm160521  * The choice of reservation property depends on the SPA version.
6785713Srm160521  */
6795713Srm160521 static int
6805713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
6815713Srm160521 {
6825713Srm160521 	int spa_version;
6835713Srm160521 
6845713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
6855713Srm160521 		return (-1);
6865713Srm160521 
6875713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
6885713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
6895713Srm160521 	else
6905713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
6915713Srm160521 
6925713Srm160521 	return (0);
6935713Srm160521 }
6945713Srm160521 
6953912Slling /*
6962676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
6972676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
6982676Seschrock  * strings.
699789Sahrens  */
7007184Stimh nvlist_t *
7017184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
7025094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
703789Sahrens {
7042676Seschrock 	nvpair_t *elem;
7052676Seschrock 	uint64_t intval;
7062676Seschrock 	char *strval;
7075094Slling 	zfs_prop_t prop;
7082676Seschrock 	nvlist_t *ret;
7095331Samw 	int chosen_normal = -1;
7105331Samw 	int chosen_utf = -1;
7112676Seschrock 
7125094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
7135094Slling 		(void) no_memory(hdl);
7145094Slling 		return (NULL);
715789Sahrens 	}
716789Sahrens 
7179396SMatthew.Ahrens@Sun.COM 	/*
7189396SMatthew.Ahrens@Sun.COM 	 * Make sure this property is valid and applies to this type.
7199396SMatthew.Ahrens@Sun.COM 	 */
7209396SMatthew.Ahrens@Sun.COM 
7212676Seschrock 	elem = NULL;
7222676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
7235094Slling 		const char *propname = nvpair_name(elem);
7242676Seschrock 
7259396SMatthew.Ahrens@Sun.COM 		prop = zfs_name_to_prop(propname);
7269396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
7275094Slling 			/*
7289396SMatthew.Ahrens@Sun.COM 			 * This is a user property: make sure it's a
7295094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
7305094Slling 			 */
7315094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
7325094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7335094Slling 				    "'%s' must be a string"), propname);
7345094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7355094Slling 				goto error;
7365094Slling 			}
7375094Slling 
7385094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
7395094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7405094Slling 				    "property name '%s' is too long"),
7412676Seschrock 				    propname);
7422676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7432676Seschrock 				goto error;
7442676Seschrock 			}
7452676Seschrock 
7462676Seschrock 			(void) nvpair_value_string(elem, &strval);
7472676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
7482676Seschrock 				(void) no_memory(hdl);
7492676Seschrock 				goto error;
7502676Seschrock 			}
7512676Seschrock 			continue;
752789Sahrens 		}
7532676Seschrock 
7549396SMatthew.Ahrens@Sun.COM 		/*
7559396SMatthew.Ahrens@Sun.COM 		 * Currently, only user properties can be modified on
7569396SMatthew.Ahrens@Sun.COM 		 * snapshots.
7579396SMatthew.Ahrens@Sun.COM 		 */
7587265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
7597265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7607265Sahrens 			    "this property can not be modified for snapshots"));
7617265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
7627265Sahrens 			goto error;
7637265Sahrens 		}
7647265Sahrens 
7659396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
7669396SMatthew.Ahrens@Sun.COM 			zfs_userquota_prop_t uqtype;
7679396SMatthew.Ahrens@Sun.COM 			char newpropname[128];
7689396SMatthew.Ahrens@Sun.COM 			char domain[128];
7699396SMatthew.Ahrens@Sun.COM 			uint64_t rid;
7709396SMatthew.Ahrens@Sun.COM 			uint64_t valary[3];
7719396SMatthew.Ahrens@Sun.COM 
7729396SMatthew.Ahrens@Sun.COM 			if (userquota_propname_decode(propname, zoned,
7739396SMatthew.Ahrens@Sun.COM 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
7749396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
7759396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN,
7769396SMatthew.Ahrens@Sun.COM 				    "'%s' has an invalid user/group name"),
7779396SMatthew.Ahrens@Sun.COM 				    propname);
7789396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7799396SMatthew.Ahrens@Sun.COM 				goto error;
7809396SMatthew.Ahrens@Sun.COM 			}
7819396SMatthew.Ahrens@Sun.COM 
7829396SMatthew.Ahrens@Sun.COM 			if (uqtype != ZFS_PROP_USERQUOTA &&
7839396SMatthew.Ahrens@Sun.COM 			    uqtype != ZFS_PROP_GROUPQUOTA) {
7849396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl,
7859396SMatthew.Ahrens@Sun.COM 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
7869396SMatthew.Ahrens@Sun.COM 				    propname);
7879396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
7889396SMatthew.Ahrens@Sun.COM 				    errbuf);
7899396SMatthew.Ahrens@Sun.COM 				goto error;
7909396SMatthew.Ahrens@Sun.COM 			}
7919396SMatthew.Ahrens@Sun.COM 
7929396SMatthew.Ahrens@Sun.COM 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
7939396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_string(elem, &strval);
7949396SMatthew.Ahrens@Sun.COM 				if (strcmp(strval, "none") == 0) {
7959396SMatthew.Ahrens@Sun.COM 					intval = 0;
7969396SMatthew.Ahrens@Sun.COM 				} else if (zfs_nicestrtonum(hdl,
7979396SMatthew.Ahrens@Sun.COM 				    strval, &intval) != 0) {
7989396SMatthew.Ahrens@Sun.COM 					(void) zfs_error(hdl,
7999396SMatthew.Ahrens@Sun.COM 					    EZFS_BADPROP, errbuf);
8009396SMatthew.Ahrens@Sun.COM 					goto error;
8019396SMatthew.Ahrens@Sun.COM 				}
8029396SMatthew.Ahrens@Sun.COM 			} else if (nvpair_type(elem) ==
8039396SMatthew.Ahrens@Sun.COM 			    DATA_TYPE_UINT64) {
8049396SMatthew.Ahrens@Sun.COM 				(void) nvpair_value_uint64(elem, &intval);
8059396SMatthew.Ahrens@Sun.COM 				if (intval == 0) {
8069396SMatthew.Ahrens@Sun.COM 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8079396SMatthew.Ahrens@Sun.COM 					    "use 'none' to disable "
8089396SMatthew.Ahrens@Sun.COM 					    "userquota/groupquota"));
8099396SMatthew.Ahrens@Sun.COM 					goto error;
8109396SMatthew.Ahrens@Sun.COM 				}
8119396SMatthew.Ahrens@Sun.COM 			} else {
8129396SMatthew.Ahrens@Sun.COM 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8139396SMatthew.Ahrens@Sun.COM 				    "'%s' must be a number"), propname);
8149396SMatthew.Ahrens@Sun.COM 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8159396SMatthew.Ahrens@Sun.COM 				goto error;
8169396SMatthew.Ahrens@Sun.COM 			}
8179396SMatthew.Ahrens@Sun.COM 
8189396SMatthew.Ahrens@Sun.COM 			(void) snprintf(newpropname, sizeof (newpropname),
8199396SMatthew.Ahrens@Sun.COM 			    "%s%s", zfs_userquota_prop_prefixes[uqtype],
8209396SMatthew.Ahrens@Sun.COM 			    domain);
8219396SMatthew.Ahrens@Sun.COM 			valary[0] = uqtype;
8229396SMatthew.Ahrens@Sun.COM 			valary[1] = rid;
8239396SMatthew.Ahrens@Sun.COM 			valary[2] = intval;
8249396SMatthew.Ahrens@Sun.COM 			if (nvlist_add_uint64_array(ret, newpropname,
8259396SMatthew.Ahrens@Sun.COM 			    valary, 3) != 0) {
8269396SMatthew.Ahrens@Sun.COM 				(void) no_memory(hdl);
8279396SMatthew.Ahrens@Sun.COM 				goto error;
8289396SMatthew.Ahrens@Sun.COM 			}
8299396SMatthew.Ahrens@Sun.COM 			continue;
8309396SMatthew.Ahrens@Sun.COM 		}
8319396SMatthew.Ahrens@Sun.COM 
8329396SMatthew.Ahrens@Sun.COM 		if (prop == ZPROP_INVAL) {
8339396SMatthew.Ahrens@Sun.COM 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8349396SMatthew.Ahrens@Sun.COM 			    "invalid property '%s'"), propname);
8359396SMatthew.Ahrens@Sun.COM 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8369396SMatthew.Ahrens@Sun.COM 			goto error;
8379396SMatthew.Ahrens@Sun.COM 		}
8389396SMatthew.Ahrens@Sun.COM 
8392676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
8402676Seschrock 			zfs_error_aux(hdl,
8412676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
8422676Seschrock 			    "apply to datasets of this type"), propname);
8432676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
8442676Seschrock 			goto error;
8452676Seschrock 		}
8462676Seschrock 
8472676Seschrock 		if (zfs_prop_readonly(prop) &&
8485331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
8492676Seschrock 			zfs_error_aux(hdl,
8502676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
8512676Seschrock 			    propname);
8522676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
8532676Seschrock 			goto error;
8542676Seschrock 		}
8552676Seschrock 
8565094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
8575094Slling 		    &strval, &intval, errbuf) != 0)
8582676Seschrock 			goto error;
8592676Seschrock 
8602676Seschrock 		/*
8612676Seschrock 		 * Perform some additional checks for specific properties.
8622676Seschrock 		 */
8632676Seschrock 		switch (prop) {
8644577Sahrens 		case ZFS_PROP_VERSION:
8654577Sahrens 		{
8664577Sahrens 			int version;
8674577Sahrens 
8684577Sahrens 			if (zhp == NULL)
8694577Sahrens 				break;
8704577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
8714577Sahrens 			if (intval < version) {
8724577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8734577Sahrens 				    "Can not downgrade; already at version %u"),
8744577Sahrens 				    version);
8754577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8764577Sahrens 				goto error;
8774577Sahrens 			}
8784577Sahrens 			break;
8794577Sahrens 		}
8804577Sahrens 
8812676Seschrock 		case ZFS_PROP_RECORDSIZE:
8822676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
8832676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
8842676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
8852676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
8862082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8872676Seschrock 				    "'%s' must be power of 2 from %u "
8882676Seschrock 				    "to %uk"), propname,
8892676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
8902676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
8912676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
8922676Seschrock 				goto error;
893789Sahrens 			}
894789Sahrens 			break;
895789Sahrens 
8963126Sahl 		case ZFS_PROP_SHAREISCSI:
8973126Sahl 			if (strcmp(strval, "off") != 0 &&
8983126Sahl 			    strcmp(strval, "on") != 0 &&
8993126Sahl 			    strcmp(strval, "type=disk") != 0) {
9003126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9013126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
9023126Sahl 				    propname);
9033126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9043126Sahl 				goto error;
9053126Sahl 			}
9063126Sahl 
9073126Sahl 			break;
9083126Sahl 
9092676Seschrock 		case ZFS_PROP_MOUNTPOINT:
9104778Srm160521 		{
9114778Srm160521 			namecheck_err_t why;
9124778Srm160521 
9132676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
9142676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
9152676Seschrock 				break;
9162676Seschrock 
9174778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
9184778Srm160521 				switch (why) {
9194778Srm160521 				case NAME_ERR_LEADING_SLASH:
9204778Srm160521 					zfs_error_aux(hdl,
9214778Srm160521 					    dgettext(TEXT_DOMAIN,
9224778Srm160521 					    "'%s' must be an absolute path, "
9234778Srm160521 					    "'none', or 'legacy'"), propname);
9244778Srm160521 					break;
9254778Srm160521 				case NAME_ERR_TOOLONG:
9264778Srm160521 					zfs_error_aux(hdl,
9274778Srm160521 					    dgettext(TEXT_DOMAIN,
9284778Srm160521 					    "component of '%s' is too long"),
9294778Srm160521 					    propname);
9304778Srm160521 					break;
9314778Srm160521 				}
9322676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9332676Seschrock 				goto error;
934789Sahrens 			}
9354778Srm160521 		}
9364778Srm160521 
9373126Sahl 			/*FALLTHRU*/
9383126Sahl 
9395331Samw 		case ZFS_PROP_SHARESMB:
9403126Sahl 		case ZFS_PROP_SHARENFS:
9413126Sahl 			/*
9425331Samw 			 * For the mountpoint and sharenfs or sharesmb
9435331Samw 			 * properties, check if it can be set in a
9445331Samw 			 * global/non-global zone based on
9453126Sahl 			 * the zoned property value:
9463126Sahl 			 *
9473126Sahl 			 *		global zone	    non-global zone
9483126Sahl 			 * --------------------------------------------------
9493126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
9503126Sahl 			 *		sharenfs (no)	    sharenfs (no)
9515331Samw 			 *		sharesmb (no)	    sharesmb (no)
9523126Sahl 			 *
9533126Sahl 			 * zoned=off	mountpoint (yes)	N/A
9543126Sahl 			 *		sharenfs (yes)
9555331Samw 			 *		sharesmb (yes)
9563126Sahl 			 */
9572676Seschrock 			if (zoned) {
9582676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
9592676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9602676Seschrock 					    "'%s' cannot be set on "
9612676Seschrock 					    "dataset in a non-global zone"),
9622676Seschrock 					    propname);
9632676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9642676Seschrock 					    errbuf);
9652676Seschrock 					goto error;
9665331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
9675331Samw 				    prop == ZFS_PROP_SHARESMB) {
9682676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9692676Seschrock 					    "'%s' cannot be set in "
9702676Seschrock 					    "a non-global zone"), propname);
9712676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
9722676Seschrock 					    errbuf);
9732676Seschrock 					goto error;
9742676Seschrock 				}
9752676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
9762676Seschrock 				/*
9772676Seschrock 				 * If zoned property is 'off', this must be in
9789396SMatthew.Ahrens@Sun.COM 				 * a global zone. If not, something is wrong.
9792676Seschrock 				 */
9802676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9812676Seschrock 				    "'%s' cannot be set while dataset "
9822676Seschrock 				    "'zoned' property is set"), propname);
9832676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
9842676Seschrock 				goto error;
9852676Seschrock 			}
9863126Sahl 
9874180Sdougm 			/*
9884180Sdougm 			 * At this point, it is legitimate to set the
9894180Sdougm 			 * property. Now we want to make sure that the
9904180Sdougm 			 * property value is valid if it is sharenfs.
9914180Sdougm 			 */
9925331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
9935331Samw 			    prop == ZFS_PROP_SHARESMB) &&
9944217Seschrock 			    strcmp(strval, "on") != 0 &&
9954217Seschrock 			    strcmp(strval, "off") != 0) {
9965331Samw 				zfs_share_proto_t proto;
9975331Samw 
9985331Samw 				if (prop == ZFS_PROP_SHARESMB)
9995331Samw 					proto = PROTO_SMB;
10005331Samw 				else
10015331Samw 					proto = PROTO_NFS;
10024180Sdougm 
10034180Sdougm 				/*
10045331Samw 				 * Must be an valid sharing protocol
10055331Samw 				 * option string so init the libshare
10065331Samw 				 * in order to enable the parser and
10075331Samw 				 * then parse the options. We use the
10085331Samw 				 * control API since we don't care about
10095331Samw 				 * the current configuration and don't
10104180Sdougm 				 * want the overhead of loading it
10114180Sdougm 				 * until we actually do something.
10124180Sdougm 				 */
10134180Sdougm 
10144217Seschrock 				if (zfs_init_libshare(hdl,
10154217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
10164217Seschrock 					/*
10174217Seschrock 					 * An error occurred so we can't do
10184217Seschrock 					 * anything
10194217Seschrock 					 */
10204217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10214217Seschrock 					    "'%s' cannot be set: problem "
10224217Seschrock 					    "in share initialization"),
10234217Seschrock 					    propname);
10244217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10254217Seschrock 					    errbuf);
10264217Seschrock 					goto error;
10274217Seschrock 				}
10284217Seschrock 
10295331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
10304217Seschrock 					/*
10314217Seschrock 					 * There was an error in parsing so
10324217Seschrock 					 * deal with it by issuing an error
10334217Seschrock 					 * message and leaving after
10344217Seschrock 					 * uninitializing the the libshare
10354217Seschrock 					 * interface.
10364217Seschrock 					 */
10374217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10384217Seschrock 					    "'%s' cannot be set to invalid "
10394217Seschrock 					    "options"), propname);
10404217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10414217Seschrock 					    errbuf);
10424217Seschrock 					zfs_uninit_libshare(hdl);
10434217Seschrock 					goto error;
10444217Seschrock 				}
10454180Sdougm 				zfs_uninit_libshare(hdl);
10464180Sdougm 			}
10474180Sdougm 
10483126Sahl 			break;
10495331Samw 		case ZFS_PROP_UTF8ONLY:
10505331Samw 			chosen_utf = (int)intval;
10515331Samw 			break;
10525331Samw 		case ZFS_PROP_NORMALIZE:
10535331Samw 			chosen_normal = (int)intval;
10545331Samw 			break;
10552676Seschrock 		}
10562676Seschrock 
10572676Seschrock 		/*
10582676Seschrock 		 * For changes to existing volumes, we have some additional
10592676Seschrock 		 * checks to enforce.
10602676Seschrock 		 */
10612676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
10622676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
10632676Seschrock 			    ZFS_PROP_VOLSIZE);
10642676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
10652676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
10662676Seschrock 			char buf[64];
10672676Seschrock 
10682676Seschrock 			switch (prop) {
10692676Seschrock 			case ZFS_PROP_RESERVATION:
10705378Sck153898 			case ZFS_PROP_REFRESERVATION:
10712676Seschrock 				if (intval > volsize) {
10722676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10732676Seschrock 					    "'%s' is greater than current "
10742676Seschrock 					    "volume size"), propname);
10752676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10762676Seschrock 					    errbuf);
10772676Seschrock 					goto error;
10782676Seschrock 				}
10792676Seschrock 				break;
10802676Seschrock 
10812676Seschrock 			case ZFS_PROP_VOLSIZE:
10822676Seschrock 				if (intval % blocksize != 0) {
10832676Seschrock 					zfs_nicenum(blocksize, buf,
10842676Seschrock 					    sizeof (buf));
10852676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10862676Seschrock 					    "'%s' must be a multiple of "
10872676Seschrock 					    "volume block size (%s)"),
10882676Seschrock 					    propname, buf);
10892676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10902676Seschrock 					    errbuf);
10912676Seschrock 					goto error;
10922676Seschrock 				}
10932676Seschrock 
10942676Seschrock 				if (intval == 0) {
10952676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10962676Seschrock 					    "'%s' cannot be zero"),
10972676Seschrock 					    propname);
10982676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
10992676Seschrock 					    errbuf);
11002676Seschrock 					goto error;
1101789Sahrens 				}
11023126Sahl 				break;
1103789Sahrens 			}
1104789Sahrens 		}
1105789Sahrens 	}
1106789Sahrens 
11072676Seschrock 	/*
11085331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
11095331Samw 	 * enforce rejection of non-UTF8 names.
11105331Samw 	 *
11115331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
11125331Samw 	 * was explicitly not chosen, it is an error.
11135331Samw 	 */
11145498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
11155331Samw 		if (nvlist_add_uint64(ret,
11165331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
11175331Samw 			(void) no_memory(hdl);
11185331Samw 			goto error;
11195331Samw 		}
11205498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
11215331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11225331Samw 		    "'%s' must be set 'on' if normalization chosen"),
11235331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
11245331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11255331Samw 		goto error;
11265331Samw 	}
11275331Samw 
11285331Samw 	/*
11292676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
11302676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
11312676Seschrock 	 */
11322676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
11332676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
11342676Seschrock 	    &intval) == 0) {
11352676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
11362676Seschrock 		    ZFS_PROP_VOLSIZE);
11375481Sck153898 		uint64_t old_reservation;
11382676Seschrock 		uint64_t new_reservation;
11395481Sck153898 		zfs_prop_t resv_prop;
11405713Srm160521 
11415713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
11425481Sck153898 			goto error;
11435481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
11442676Seschrock 
11452676Seschrock 		if (old_volsize == old_reservation &&
11465481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
11472676Seschrock 		    &new_reservation) != 0) {
11482676Seschrock 			if (nvlist_add_uint64(ret,
11495481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
11502676Seschrock 				(void) no_memory(hdl);
11512676Seschrock 				goto error;
11522676Seschrock 			}
11532676Seschrock 		}
11542676Seschrock 	}
11552676Seschrock 	return (ret);
11562676Seschrock 
11572676Seschrock error:
11582676Seschrock 	nvlist_free(ret);
11592676Seschrock 	return (NULL);
1160789Sahrens }
1161789Sahrens 
1162789Sahrens /*
1163789Sahrens  * Given a property name and value, set the property for the given dataset.
1164789Sahrens  */
1165789Sahrens int
11662676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1167789Sahrens {
1168789Sahrens 	zfs_cmd_t zc = { 0 };
11692676Seschrock 	int ret = -1;
11702676Seschrock 	prop_changelist_t *cl = NULL;
11712082Seschrock 	char errbuf[1024];
11722082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
11732676Seschrock 	nvlist_t *nvl = NULL, *realprops;
11742676Seschrock 	zfs_prop_t prop;
11757509SMark.Musante@Sun.COM 	boolean_t do_prefix;
11767509SMark.Musante@Sun.COM 	uint64_t idx;
11772082Seschrock 
11782082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
11792676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
11802082Seschrock 	    zhp->zfs_name);
11812082Seschrock 
11822676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
11832676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
11842676Seschrock 		(void) no_memory(hdl);
11852676Seschrock 		goto error;
1186789Sahrens 	}
1187789Sahrens 
11887184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
11892676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
11902676Seschrock 		goto error;
11915094Slling 
11922676Seschrock 	nvlist_free(nvl);
11932676Seschrock 	nvl = realprops;
11942676Seschrock 
11952676Seschrock 	prop = zfs_name_to_prop(propname);
11962676Seschrock 
11977366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
11982676Seschrock 		goto error;
1199789Sahrens 
1200789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
12012082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12022082Seschrock 		    "child dataset with inherited mountpoint is used "
12032082Seschrock 		    "in a non-global zone"));
12042082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1205789Sahrens 		goto error;
1206789Sahrens 	}
1207789Sahrens 
12087509SMark.Musante@Sun.COM 	/*
12097509SMark.Musante@Sun.COM 	 * If the dataset's canmount property is being set to noauto,
12107509SMark.Musante@Sun.COM 	 * then we want to prevent unmounting & remounting it.
12117509SMark.Musante@Sun.COM 	 */
12127509SMark.Musante@Sun.COM 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
12137509SMark.Musante@Sun.COM 	    (zprop_string_to_index(prop, propval, &idx,
12147509SMark.Musante@Sun.COM 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
12156168Shs24103 
12166168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
12177509SMark.Musante@Sun.COM 		goto error;
1218789Sahrens 
1219789Sahrens 	/*
1220789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1221789Sahrens 	 */
1222789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1223789Sahrens 
12245094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
12252676Seschrock 		goto error;
12262676Seschrock 
12274543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
12288845Samw@Sun.COM 
1229789Sahrens 	if (ret != 0) {
1230789Sahrens 		switch (errno) {
1231789Sahrens 
1232789Sahrens 		case ENOSPC:
1233789Sahrens 			/*
1234789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1235789Sahrens 			 * something different; setting a quota or reservation
1236789Sahrens 			 * doesn't use any disk space.
1237789Sahrens 			 */
1238789Sahrens 			switch (prop) {
1239789Sahrens 			case ZFS_PROP_QUOTA:
12405378Sck153898 			case ZFS_PROP_REFQUOTA:
12412082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12422082Seschrock 				    "size is less than current used or "
12432082Seschrock 				    "reserved space"));
12442082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1245789Sahrens 				break;
1246789Sahrens 
1247789Sahrens 			case ZFS_PROP_RESERVATION:
12485378Sck153898 			case ZFS_PROP_REFRESERVATION:
12492082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12502082Seschrock 				    "size is greater than available space"));
12512082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1252789Sahrens 				break;
1253789Sahrens 
1254789Sahrens 			default:
12552082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1256789Sahrens 				break;
1257789Sahrens 			}
1258789Sahrens 			break;
1259789Sahrens 
1260789Sahrens 		case EBUSY:
1261*10588SEric.Taylor@Sun.COM 			(void) zfs_standard_error(hdl, EBUSY, errbuf);
1262789Sahrens 			break;
1263789Sahrens 
12641175Slling 		case EROFS:
12652082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
12661175Slling 			break;
12671175Slling 
12683886Sahl 		case ENOTSUP:
12693886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12705977Smarks 			    "pool and or dataset must be upgraded to set this "
12714603Sahrens 			    "property or value"));
12723886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
12733886Sahl 			break;
12743886Sahl 
12757042Sgw25295 		case ERANGE:
12767042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
12777042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12787042Sgw25295 				    "property setting is not allowed on "
12797042Sgw25295 				    "bootable datasets"));
12807042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
12817042Sgw25295 			} else {
12827042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
12837042Sgw25295 			}
12847042Sgw25295 			break;
12857042Sgw25295 
1286789Sahrens 		case EOVERFLOW:
1287789Sahrens 			/*
1288789Sahrens 			 * This platform can't address a volume this big.
1289789Sahrens 			 */
1290789Sahrens #ifdef _ILP32
1291789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
12922082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1293789Sahrens 				break;
1294789Sahrens 			}
1295789Sahrens #endif
12962082Seschrock 			/* FALLTHROUGH */
1297789Sahrens 		default:
12982082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1299789Sahrens 		}
1300789Sahrens 	} else {
13016168Shs24103 		if (do_prefix)
13026168Shs24103 			ret = changelist_postfix(cl);
13036168Shs24103 
1304789Sahrens 		/*
1305789Sahrens 		 * Refresh the statistics so the new property value
1306789Sahrens 		 * is reflected.
1307789Sahrens 		 */
13086168Shs24103 		if (ret == 0)
13092676Seschrock 			(void) get_stats(zhp);
1310789Sahrens 	}
1311789Sahrens 
1312789Sahrens error:
13132676Seschrock 	nvlist_free(nvl);
13142676Seschrock 	zcmd_free_nvlists(&zc);
13152676Seschrock 	if (cl)
13162676Seschrock 		changelist_free(cl);
1317789Sahrens 	return (ret);
1318789Sahrens }
1319789Sahrens 
1320789Sahrens /*
1321789Sahrens  * Given a property, inherit the value from the parent dataset.
1322789Sahrens  */
1323789Sahrens int
13242676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1325789Sahrens {
1326789Sahrens 	zfs_cmd_t zc = { 0 };
1327789Sahrens 	int ret;
1328789Sahrens 	prop_changelist_t *cl;
13292082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
13302082Seschrock 	char errbuf[1024];
13312676Seschrock 	zfs_prop_t prop;
13322082Seschrock 
13332082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
13342082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1335789Sahrens 
13365094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
13372676Seschrock 		/*
13382676Seschrock 		 * For user properties, the amount of work we have to do is very
13392676Seschrock 		 * small, so just do it here.
13402676Seschrock 		 */
13412676Seschrock 		if (!zfs_prop_user(propname)) {
13422676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13432676Seschrock 			    "invalid property"));
13442676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
13452676Seschrock 		}
13462676Seschrock 
13472676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13482676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
13492676Seschrock 
13504849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
13512676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
13522676Seschrock 
13532676Seschrock 		return (0);
13542676Seschrock 	}
13552676Seschrock 
1356789Sahrens 	/*
1357789Sahrens 	 * Verify that this property is inheritable.
1358789Sahrens 	 */
13592082Seschrock 	if (zfs_prop_readonly(prop))
13602082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
13612082Seschrock 
13622082Seschrock 	if (!zfs_prop_inheritable(prop))
13632082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1364789Sahrens 
1365789Sahrens 	/*
1366789Sahrens 	 * Check to see if the value applies to this type
1367789Sahrens 	 */
13682082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
13692082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1370789Sahrens 
13713443Srm160521 	/*
13723443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
13733443Srm160521 	 */
13743443Srm160521 	propname = zfs_prop_to_name(prop);
1375789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
13762676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1377789Sahrens 
1378789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1379789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
13802082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13812082Seschrock 		    "dataset is used in a non-global zone"));
13822082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1383789Sahrens 	}
1384789Sahrens 
1385789Sahrens 	/*
1386789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1387789Sahrens 	 */
13887366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1389789Sahrens 		return (-1);
1390789Sahrens 
1391789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
13922082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13932082Seschrock 		    "child dataset with inherited mountpoint is used "
13942082Seschrock 		    "in a non-global zone"));
13952082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1396789Sahrens 		goto error;
1397789Sahrens 	}
1398789Sahrens 
1399789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1400789Sahrens 		goto error;
1401789Sahrens 
14024849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
14032082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
1404789Sahrens 	} else {
1405789Sahrens 
14062169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
1407789Sahrens 			goto error;
1408789Sahrens 
1409789Sahrens 		/*
1410789Sahrens 		 * Refresh the statistics so the new property is reflected.
1411789Sahrens 		 */
1412789Sahrens 		(void) get_stats(zhp);
1413789Sahrens 	}
1414789Sahrens 
1415789Sahrens error:
1416789Sahrens 	changelist_free(cl);
1417789Sahrens 	return (ret);
1418789Sahrens }
1419789Sahrens 
1420789Sahrens /*
14211356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
14221356Seschrock  * extract them appropriately.
14231356Seschrock  */
14241356Seschrock static uint64_t
14251356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
14261356Seschrock {
14271356Seschrock 	nvlist_t *nv;
14281356Seschrock 	uint64_t value;
14291356Seschrock 
14302885Sahrens 	*source = NULL;
14311356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
14321356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
14335094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
14345094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
14351356Seschrock 	} else {
14368802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
14378802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
14381356Seschrock 		value = zfs_prop_default_numeric(prop);
14391356Seschrock 		*source = "";
14401356Seschrock 	}
14411356Seschrock 
14421356Seschrock 	return (value);
14431356Seschrock }
14441356Seschrock 
14451356Seschrock static char *
14461356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
14471356Seschrock {
14481356Seschrock 	nvlist_t *nv;
14491356Seschrock 	char *value;
14501356Seschrock 
14512885Sahrens 	*source = NULL;
14521356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
14531356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
14545094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
14555094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
14561356Seschrock 	} else {
14578802SSanjeev.Bagewadi@Sun.COM 		verify(!zhp->zfs_props_table ||
14588802SSanjeev.Bagewadi@Sun.COM 		    zhp->zfs_props_table[prop] == B_TRUE);
14591356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
14601356Seschrock 			value = "";
14611356Seschrock 		*source = "";
14621356Seschrock 	}
14631356Seschrock 
14641356Seschrock 	return (value);
14651356Seschrock }
14661356Seschrock 
14671356Seschrock /*
1468789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1469789Sahrens  * zfs_prop_get_int() are built using this interface.
1470789Sahrens  *
1471789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1472789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1473789Sahrens  * If they differ from the on-disk values, report the current values and mark
1474789Sahrens  * the source "temporary".
1475789Sahrens  */
14762082Seschrock static int
14775094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
14782082Seschrock     char **source, uint64_t *val)
1479789Sahrens {
14805147Srm160521 	zfs_cmd_t zc = { 0 };
14815592Stimh 	nvlist_t *zplprops = NULL;
1482789Sahrens 	struct mnttab mnt;
14833265Sahrens 	char *mntopt_on = NULL;
14843265Sahrens 	char *mntopt_off = NULL;
1485789Sahrens 
1486789Sahrens 	*source = NULL;
1487789Sahrens 
14883265Sahrens 	switch (prop) {
14893265Sahrens 	case ZFS_PROP_ATIME:
14903265Sahrens 		mntopt_on = MNTOPT_ATIME;
14913265Sahrens 		mntopt_off = MNTOPT_NOATIME;
14923265Sahrens 		break;
14933265Sahrens 
14943265Sahrens 	case ZFS_PROP_DEVICES:
14953265Sahrens 		mntopt_on = MNTOPT_DEVICES;
14963265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
14973265Sahrens 		break;
14983265Sahrens 
14993265Sahrens 	case ZFS_PROP_EXEC:
15003265Sahrens 		mntopt_on = MNTOPT_EXEC;
15013265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
15023265Sahrens 		break;
15033265Sahrens 
15043265Sahrens 	case ZFS_PROP_READONLY:
15053265Sahrens 		mntopt_on = MNTOPT_RO;
15063265Sahrens 		mntopt_off = MNTOPT_RW;
15073265Sahrens 		break;
15083265Sahrens 
15093265Sahrens 	case ZFS_PROP_SETUID:
15103265Sahrens 		mntopt_on = MNTOPT_SETUID;
15113265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
15123265Sahrens 		break;
15133265Sahrens 
15143265Sahrens 	case ZFS_PROP_XATTR:
15153265Sahrens 		mntopt_on = MNTOPT_XATTR;
15163265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
15173265Sahrens 		break;
15185331Samw 
15195331Samw 	case ZFS_PROP_NBMAND:
15205331Samw 		mntopt_on = MNTOPT_NBMAND;
15215331Samw 		mntopt_off = MNTOPT_NONBMAND;
15225331Samw 		break;
15233265Sahrens 	}
15243265Sahrens 
15252474Seschrock 	/*
15262474Seschrock 	 * Because looking up the mount options is potentially expensive
15272474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
15282474Seschrock 	 * we're looking up a property which requires its presence.
15292474Seschrock 	 */
15302474Seschrock 	if (!zhp->zfs_mntcheck &&
15313265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
15328228SEric.Taylor@Sun.COM 		libzfs_handle_t *hdl = zhp->zfs_hdl;
15338228SEric.Taylor@Sun.COM 		struct mnttab entry;
15348228SEric.Taylor@Sun.COM 
15358228SEric.Taylor@Sun.COM 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
15368228SEric.Taylor@Sun.COM 			zhp->zfs_mntopts = zfs_strdup(hdl,
15373265Sahrens 			    entry.mnt_mntopts);
15383265Sahrens 			if (zhp->zfs_mntopts == NULL)
15393265Sahrens 				return (-1);
15403265Sahrens 		}
15412474Seschrock 
15422474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
15432474Seschrock 	}
15442474Seschrock 
1545789Sahrens 	if (zhp->zfs_mntopts == NULL)
1546789Sahrens 		mnt.mnt_mntopts = "";
1547789Sahrens 	else
1548789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
1549789Sahrens 
1550789Sahrens 	switch (prop) {
1551789Sahrens 	case ZFS_PROP_ATIME:
15523265Sahrens 	case ZFS_PROP_DEVICES:
15533265Sahrens 	case ZFS_PROP_EXEC:
15543265Sahrens 	case ZFS_PROP_READONLY:
15553265Sahrens 	case ZFS_PROP_SETUID:
15563265Sahrens 	case ZFS_PROP_XATTR:
15575331Samw 	case ZFS_PROP_NBMAND:
15582082Seschrock 		*val = getprop_uint64(zhp, prop, source);
15592082Seschrock 
15603265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
15612082Seschrock 			*val = B_TRUE;
1562789Sahrens 			if (src)
15635094Slling 				*src = ZPROP_SRC_TEMPORARY;
15643265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
15652082Seschrock 			*val = B_FALSE;
1566789Sahrens 			if (src)
15675094Slling 				*src = ZPROP_SRC_TEMPORARY;
1568789Sahrens 		}
15692082Seschrock 		break;
1570789Sahrens 
15713265Sahrens 	case ZFS_PROP_CANMOUNT:
15722082Seschrock 		*val = getprop_uint64(zhp, prop, source);
15736168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
15743417Srm160521 			*source = zhp->zfs_name;
15753417Srm160521 		else
15763417Srm160521 			*source = "";	/* default */
15772082Seschrock 		break;
1578789Sahrens 
1579789Sahrens 	case ZFS_PROP_QUOTA:
15805378Sck153898 	case ZFS_PROP_REFQUOTA:
1581789Sahrens 	case ZFS_PROP_RESERVATION:
15825378Sck153898 	case ZFS_PROP_REFRESERVATION:
15832885Sahrens 		*val = getprop_uint64(zhp, prop, source);
15842885Sahrens 		if (*val == 0)
1585789Sahrens 			*source = "";	/* default */
1586789Sahrens 		else
1587789Sahrens 			*source = zhp->zfs_name;
15882082Seschrock 		break;
1589789Sahrens 
1590789Sahrens 	case ZFS_PROP_MOUNTED:
15912082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
15922082Seschrock 		break;
1593789Sahrens 
15943377Seschrock 	case ZFS_PROP_NUMCLONES:
15953377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
15963377Seschrock 		break;
15973377Seschrock 
15985147Srm160521 	case ZFS_PROP_VERSION:
15995498Stimh 	case ZFS_PROP_NORMALIZE:
16005498Stimh 	case ZFS_PROP_UTF8ONLY:
16015498Stimh 	case ZFS_PROP_CASE:
16025498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
16035498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
16045473Srm160521 			return (-1);
16055147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
16065498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
16075498Stimh 			zcmd_free_nvlists(&zc);
160810204SMatthew.Ahrens@Sun.COM 			return (-1);
16095147Srm160521 		}
16105498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
16115498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
16125498Stimh 		    val) != 0) {
16135498Stimh 			zcmd_free_nvlists(&zc);
161410204SMatthew.Ahrens@Sun.COM 			return (-1);
16155498Stimh 		}
16165592Stimh 		if (zplprops)
16175592Stimh 			nvlist_free(zplprops);
16185498Stimh 		zcmd_free_nvlists(&zc);
16195147Srm160521 		break;
16205147Srm160521 
1621789Sahrens 	default:
16224577Sahrens 		switch (zfs_prop_get_type(prop)) {
16234787Sahrens 		case PROP_TYPE_NUMBER:
16244787Sahrens 		case PROP_TYPE_INDEX:
16254577Sahrens 			*val = getprop_uint64(zhp, prop, source);
16267390SMatthew.Ahrens@Sun.COM 			/*
16279396SMatthew.Ahrens@Sun.COM 			 * If we tried to use a default value for a
16287390SMatthew.Ahrens@Sun.COM 			 * readonly property, it means that it was not
16297390SMatthew.Ahrens@Sun.COM 			 * present; return an error.
16307390SMatthew.Ahrens@Sun.COM 			 */
16317390SMatthew.Ahrens@Sun.COM 			if (zfs_prop_readonly(prop) &&
16327390SMatthew.Ahrens@Sun.COM 			    *source && (*source)[0] == '\0') {
16337390SMatthew.Ahrens@Sun.COM 				return (-1);
16347390SMatthew.Ahrens@Sun.COM 			}
16354577Sahrens 			break;
16364577Sahrens 
16374787Sahrens 		case PROP_TYPE_STRING:
16384577Sahrens 		default:
16394577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
16404577Sahrens 			    "cannot get non-numeric property"));
16414577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
16424577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
16434577Sahrens 		}
1644789Sahrens 	}
1645789Sahrens 
1646789Sahrens 	return (0);
1647789Sahrens }
1648789Sahrens 
1649789Sahrens /*
1650789Sahrens  * Calculate the source type, given the raw source string.
1651789Sahrens  */
1652789Sahrens static void
16535094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1654789Sahrens     char *statbuf, size_t statlen)
1655789Sahrens {
16565094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1657789Sahrens 		return;
1658789Sahrens 
1659789Sahrens 	if (source == NULL) {
16605094Slling 		*srctype = ZPROP_SRC_NONE;
1661789Sahrens 	} else if (source[0] == '\0') {
16625094Slling 		*srctype = ZPROP_SRC_DEFAULT;
1663789Sahrens 	} else {
1664789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
16655094Slling 			*srctype = ZPROP_SRC_LOCAL;
1666789Sahrens 		} else {
1667789Sahrens 			(void) strlcpy(statbuf, source, statlen);
16685094Slling 			*srctype = ZPROP_SRC_INHERITED;
1669789Sahrens 		}
1670789Sahrens 	}
1671789Sahrens 
1672789Sahrens }
1673789Sahrens 
1674789Sahrens /*
1675789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
1676789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
1677789Sahrens  * human-readable form.
1678789Sahrens  *
1679789Sahrens  * Returns 0 on success, or -1 on error.
1680789Sahrens  */
1681789Sahrens int
1682789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
16835094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1684789Sahrens {
1685789Sahrens 	char *source = NULL;
1686789Sahrens 	uint64_t val;
1687789Sahrens 	char *str;
16882676Seschrock 	const char *strval;
1689789Sahrens 
1690789Sahrens 	/*
1691789Sahrens 	 * Check to see if this property applies to our object
1692789Sahrens 	 */
1693789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1694789Sahrens 		return (-1);
1695789Sahrens 
1696789Sahrens 	if (src)
16975094Slling 		*src = ZPROP_SRC_NONE;
1698789Sahrens 
1699789Sahrens 	switch (prop) {
1700789Sahrens 	case ZFS_PROP_CREATION:
1701789Sahrens 		/*
1702789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
1703789Sahrens 		 * this into a string unless 'literal' is specified.
1704789Sahrens 		 */
1705789Sahrens 		{
17062885Sahrens 			val = getprop_uint64(zhp, prop, &source);
17072885Sahrens 			time_t time = (time_t)val;
1708789Sahrens 			struct tm t;
1709789Sahrens 
1710789Sahrens 			if (literal ||
1711789Sahrens 			    localtime_r(&time, &t) == NULL ||
1712789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1713789Sahrens 			    &t) == 0)
17142885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
1715789Sahrens 		}
1716789Sahrens 		break;
1717789Sahrens 
1718789Sahrens 	case ZFS_PROP_MOUNTPOINT:
1719789Sahrens 		/*
1720789Sahrens 		 * Getting the precise mountpoint can be tricky.
1721789Sahrens 		 *
1722789Sahrens 		 *  - for 'none' or 'legacy', return those values.
1723789Sahrens 		 *  - for inherited mountpoints, we want to take everything
1724789Sahrens 		 *    after our ancestor and append it to the inherited value.
1725789Sahrens 		 *
1726789Sahrens 		 * If the pool has an alternate root, we want to prepend that
1727789Sahrens 		 * root to any values we return.
1728789Sahrens 		 */
17296865Srm160521 
17301356Seschrock 		str = getprop_string(zhp, prop, &source);
17311356Seschrock 
17326612Sgw25295 		if (str[0] == '/') {
17336865Srm160521 			char buf[MAXPATHLEN];
17346865Srm160521 			char *root = buf;
17351356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
1736789Sahrens 
1737789Sahrens 			if (relpath[0] == '/')
1738789Sahrens 				relpath++;
17396612Sgw25295 
17406865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
17416865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
17426865Srm160521 			    (strcmp(root, "-") == 0))
17436865Srm160521 				root[0] = '\0';
17446612Sgw25295 			/*
17456612Sgw25295 			 * Special case an alternate root of '/'. This will
17466612Sgw25295 			 * avoid having multiple leading slashes in the
17476612Sgw25295 			 * mountpoint path.
17486612Sgw25295 			 */
17496612Sgw25295 			if (strcmp(root, "/") == 0)
17506612Sgw25295 				root++;
17516612Sgw25295 
17526612Sgw25295 			/*
17536612Sgw25295 			 * If the mountpoint is '/' then skip over this
17546612Sgw25295 			 * if we are obtaining either an alternate root or
17556612Sgw25295 			 * an inherited mountpoint.
17566612Sgw25295 			 */
17576612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
17586612Sgw25295 			    relpath[0] != '\0'))
17591356Seschrock 				str++;
1760789Sahrens 
1761789Sahrens 			if (relpath[0] == '\0')
1762789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
17631356Seschrock 				    root, str);
1764789Sahrens 			else
1765789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
17661356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
1767789Sahrens 				    relpath);
1768789Sahrens 		} else {
1769789Sahrens 			/* 'legacy' or 'none' */
17701356Seschrock 			(void) strlcpy(propbuf, str, proplen);
1771789Sahrens 		}
1772789Sahrens 
1773789Sahrens 		break;
1774789Sahrens 
1775789Sahrens 	case ZFS_PROP_ORIGIN:
17762885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1777789Sahrens 		    proplen);
1778789Sahrens 		/*
1779789Sahrens 		 * If there is no parent at all, return failure to indicate that
1780789Sahrens 		 * it doesn't apply to this dataset.
1781789Sahrens 		 */
1782789Sahrens 		if (propbuf[0] == '\0')
1783789Sahrens 			return (-1);
1784789Sahrens 		break;
1785789Sahrens 
1786789Sahrens 	case ZFS_PROP_QUOTA:
17875378Sck153898 	case ZFS_PROP_REFQUOTA:
1788789Sahrens 	case ZFS_PROP_RESERVATION:
17895378Sck153898 	case ZFS_PROP_REFRESERVATION:
17905378Sck153898 
17912082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
17922082Seschrock 			return (-1);
1793789Sahrens 
1794789Sahrens 		/*
1795789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
1796789Sahrens 		 * (unless literal is set), and indicate that it's the default
1797789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
1798789Sahrens 		 * that its set locally.
1799789Sahrens 		 */
1800789Sahrens 		if (val == 0) {
1801789Sahrens 			if (literal)
1802789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
1803789Sahrens 			else
1804789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
1805789Sahrens 		} else {
1806789Sahrens 			if (literal)
18072856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
18083912Slling 				    (u_longlong_t)val);
1809789Sahrens 			else
1810789Sahrens 				zfs_nicenum(val, propbuf, proplen);
1811789Sahrens 		}
1812789Sahrens 		break;
1813789Sahrens 
1814789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
18152082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
18162082Seschrock 			return (-1);
18172856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
18182856Snd150628 		    val / 100, (longlong_t)val % 100);
1819789Sahrens 		break;
1820789Sahrens 
1821789Sahrens 	case ZFS_PROP_TYPE:
1822789Sahrens 		switch (zhp->zfs_type) {
1823789Sahrens 		case ZFS_TYPE_FILESYSTEM:
1824789Sahrens 			str = "filesystem";
1825789Sahrens 			break;
1826789Sahrens 		case ZFS_TYPE_VOLUME:
1827789Sahrens 			str = "volume";
1828789Sahrens 			break;
1829789Sahrens 		case ZFS_TYPE_SNAPSHOT:
1830789Sahrens 			str = "snapshot";
1831789Sahrens 			break;
1832789Sahrens 		default:
18332082Seschrock 			abort();
1834789Sahrens 		}
1835789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
1836789Sahrens 		break;
1837789Sahrens 
1838789Sahrens 	case ZFS_PROP_MOUNTED:
1839789Sahrens 		/*
1840789Sahrens 		 * The 'mounted' property is a pseudo-property that described
1841789Sahrens 		 * whether the filesystem is currently mounted.  Even though
1842789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
1843789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
1844789Sahrens 		 */
18452082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
18462082Seschrock 		    src, &source, &val) != 0)
18472082Seschrock 			return (-1);
18482082Seschrock 		if (val)
1849789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
1850789Sahrens 		else
1851789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
1852789Sahrens 		break;
1853789Sahrens 
1854789Sahrens 	case ZFS_PROP_NAME:
1855789Sahrens 		/*
1856789Sahrens 		 * The 'name' property is a pseudo-property derived from the
1857789Sahrens 		 * dataset name.  It is presented as a real property to simplify
1858789Sahrens 		 * consumers.
1859789Sahrens 		 */
1860789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
1861789Sahrens 		break;
1862789Sahrens 
1863789Sahrens 	default:
18644577Sahrens 		switch (zfs_prop_get_type(prop)) {
18654787Sahrens 		case PROP_TYPE_NUMBER:
18664577Sahrens 			if (get_numeric_property(zhp, prop, src,
18674577Sahrens 			    &source, &val) != 0)
18684577Sahrens 				return (-1);
18694577Sahrens 			if (literal)
18704577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
18714577Sahrens 				    (u_longlong_t)val);
18724577Sahrens 			else
18734577Sahrens 				zfs_nicenum(val, propbuf, proplen);
18744577Sahrens 			break;
18754577Sahrens 
18764787Sahrens 		case PROP_TYPE_STRING:
18774577Sahrens 			(void) strlcpy(propbuf,
18784577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
18794577Sahrens 			break;
18804577Sahrens 
18814787Sahrens 		case PROP_TYPE_INDEX:
18824861Sahrens 			if (get_numeric_property(zhp, prop, src,
18834861Sahrens 			    &source, &val) != 0)
18844861Sahrens 				return (-1);
18854861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
18864577Sahrens 				return (-1);
18874577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
18884577Sahrens 			break;
18894577Sahrens 
18904577Sahrens 		default:
18914577Sahrens 			abort();
18924577Sahrens 		}
1893789Sahrens 	}
1894789Sahrens 
1895789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
1896789Sahrens 
1897789Sahrens 	return (0);
1898789Sahrens }
1899789Sahrens 
1900789Sahrens /*
1901789Sahrens  * Utility function to get the given numeric property.  Does no validation that
1902789Sahrens  * the given property is the appropriate type; should only be used with
1903789Sahrens  * hard-coded property types.
1904789Sahrens  */
1905789Sahrens uint64_t
1906789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1907789Sahrens {
1908789Sahrens 	char *source;
19092082Seschrock 	uint64_t val;
19102082Seschrock 
19115367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
19122082Seschrock 
19132082Seschrock 	return (val);
1914789Sahrens }
1915789Sahrens 
19165713Srm160521 int
19175713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
19185713Srm160521 {
19195713Srm160521 	char buf[64];
19205713Srm160521 
19219396SMatthew.Ahrens@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
19225713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
19235713Srm160521 }
19245713Srm160521 
1925789Sahrens /*
1926789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
1927789Sahrens  */
1928789Sahrens int
1929789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
19305094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
1931789Sahrens {
1932789Sahrens 	char *source;
1933789Sahrens 
1934789Sahrens 	/*
1935789Sahrens 	 * Check to see if this property applies to our object
1936789Sahrens 	 */
19374849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
19383237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
19392082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
19402082Seschrock 		    zfs_prop_to_name(prop)));
19414849Sahrens 	}
1942789Sahrens 
1943789Sahrens 	if (src)
19445094Slling 		*src = ZPROP_SRC_NONE;
1945789Sahrens 
19462082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
19472082Seschrock 		return (-1);
1948789Sahrens 
1949789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
1950789Sahrens 
1951789Sahrens 	return (0);
1952789Sahrens }
1953789Sahrens 
19549396SMatthew.Ahrens@Sun.COM static int
19559396SMatthew.Ahrens@Sun.COM idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
19569396SMatthew.Ahrens@Sun.COM     char **domainp, idmap_rid_t *ridp)
19579396SMatthew.Ahrens@Sun.COM {
19589396SMatthew.Ahrens@Sun.COM 	idmap_handle_t *idmap_hdl = NULL;
19599396SMatthew.Ahrens@Sun.COM 	idmap_get_handle_t *get_hdl = NULL;
19609396SMatthew.Ahrens@Sun.COM 	idmap_stat status;
19619396SMatthew.Ahrens@Sun.COM 	int err = EINVAL;
19629396SMatthew.Ahrens@Sun.COM 
19639396SMatthew.Ahrens@Sun.COM 	if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
19649396SMatthew.Ahrens@Sun.COM 		goto out;
19659396SMatthew.Ahrens@Sun.COM 	if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
19669396SMatthew.Ahrens@Sun.COM 		goto out;
19679396SMatthew.Ahrens@Sun.COM 
19689396SMatthew.Ahrens@Sun.COM 	if (isuser) {
19699396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbyuid(get_hdl, id,
19709396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
19719396SMatthew.Ahrens@Sun.COM 	} else {
19729396SMatthew.Ahrens@Sun.COM 		err = idmap_get_sidbygid(get_hdl, id,
19739396SMatthew.Ahrens@Sun.COM 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
19749396SMatthew.Ahrens@Sun.COM 	}
19759396SMatthew.Ahrens@Sun.COM 	if (err == IDMAP_SUCCESS &&
19769396SMatthew.Ahrens@Sun.COM 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
19779396SMatthew.Ahrens@Sun.COM 	    status == IDMAP_SUCCESS)
19789396SMatthew.Ahrens@Sun.COM 		err = 0;
19799396SMatthew.Ahrens@Sun.COM 	else
19809396SMatthew.Ahrens@Sun.COM 		err = EINVAL;
19819396SMatthew.Ahrens@Sun.COM out:
19829396SMatthew.Ahrens@Sun.COM 	if (get_hdl)
19839396SMatthew.Ahrens@Sun.COM 		idmap_get_destroy(get_hdl);
19849396SMatthew.Ahrens@Sun.COM 	if (idmap_hdl)
19859396SMatthew.Ahrens@Sun.COM 		(void) idmap_fini(idmap_hdl);
19869396SMatthew.Ahrens@Sun.COM 	return (err);
19879396SMatthew.Ahrens@Sun.COM }
19889396SMatthew.Ahrens@Sun.COM 
19899396SMatthew.Ahrens@Sun.COM /*
19909396SMatthew.Ahrens@Sun.COM  * convert the propname into parameters needed by kernel
19919396SMatthew.Ahrens@Sun.COM  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
19929396SMatthew.Ahrens@Sun.COM  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
19939396SMatthew.Ahrens@Sun.COM  */
19949396SMatthew.Ahrens@Sun.COM static int
19959396SMatthew.Ahrens@Sun.COM userquota_propname_decode(const char *propname, boolean_t zoned,
19969396SMatthew.Ahrens@Sun.COM     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
19979396SMatthew.Ahrens@Sun.COM {
19989396SMatthew.Ahrens@Sun.COM 	zfs_userquota_prop_t type;
19999396SMatthew.Ahrens@Sun.COM 	char *cp, *end;
200010160SMatthew.Ahrens@Sun.COM 	char *numericsid = NULL;
20019396SMatthew.Ahrens@Sun.COM 	boolean_t isuser;
20029396SMatthew.Ahrens@Sun.COM 
20039396SMatthew.Ahrens@Sun.COM 	domain[0] = '\0';
20049396SMatthew.Ahrens@Sun.COM 
20059396SMatthew.Ahrens@Sun.COM 	/* Figure out the property type ({user|group}{quota|space}) */
20069396SMatthew.Ahrens@Sun.COM 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
20079396SMatthew.Ahrens@Sun.COM 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
20089396SMatthew.Ahrens@Sun.COM 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
20099396SMatthew.Ahrens@Sun.COM 			break;
20109396SMatthew.Ahrens@Sun.COM 	}
20119396SMatthew.Ahrens@Sun.COM 	if (type == ZFS_NUM_USERQUOTA_PROPS)
20129396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
20139396SMatthew.Ahrens@Sun.COM 	*typep = type;
20149396SMatthew.Ahrens@Sun.COM 
20159396SMatthew.Ahrens@Sun.COM 	isuser = (type == ZFS_PROP_USERQUOTA ||
20169396SMatthew.Ahrens@Sun.COM 	    type == ZFS_PROP_USERUSED);
20179396SMatthew.Ahrens@Sun.COM 
20189396SMatthew.Ahrens@Sun.COM 	cp = strchr(propname, '@') + 1;
20199396SMatthew.Ahrens@Sun.COM 
20209396SMatthew.Ahrens@Sun.COM 	if (strchr(cp, '@')) {
20219396SMatthew.Ahrens@Sun.COM 		/*
20229396SMatthew.Ahrens@Sun.COM 		 * It's a SID name (eg "user@domain") that needs to be
202310160SMatthew.Ahrens@Sun.COM 		 * turned into S-1-domainID-RID.
20249396SMatthew.Ahrens@Sun.COM 		 */
202510160SMatthew.Ahrens@Sun.COM 		directory_error_t e;
20269396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
20279396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
202810160SMatthew.Ahrens@Sun.COM 		if (isuser) {
202910160SMatthew.Ahrens@Sun.COM 			e = directory_sid_from_user_name(NULL,
203010160SMatthew.Ahrens@Sun.COM 			    cp, &numericsid);
203110160SMatthew.Ahrens@Sun.COM 		} else {
203210160SMatthew.Ahrens@Sun.COM 			e = directory_sid_from_group_name(NULL,
203310160SMatthew.Ahrens@Sun.COM 			    cp, &numericsid);
203410160SMatthew.Ahrens@Sun.COM 		}
203510160SMatthew.Ahrens@Sun.COM 		if (e != NULL) {
203610160SMatthew.Ahrens@Sun.COM 			directory_error_free(e);
20379396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
203810160SMatthew.Ahrens@Sun.COM 		}
203910160SMatthew.Ahrens@Sun.COM 		if (numericsid == NULL)
204010160SMatthew.Ahrens@Sun.COM 			return (ENOENT);
204110160SMatthew.Ahrens@Sun.COM 		cp = numericsid;
204210160SMatthew.Ahrens@Sun.COM 		/* will be further decoded below */
204310160SMatthew.Ahrens@Sun.COM 	}
204410160SMatthew.Ahrens@Sun.COM 
204510160SMatthew.Ahrens@Sun.COM 	if (strncmp(cp, "S-1-", 4) == 0) {
20469396SMatthew.Ahrens@Sun.COM 		/* It's a numeric SID (eg "S-1-234-567-89") */
204710160SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domain, cp, domainlen);
20489396SMatthew.Ahrens@Sun.COM 		cp = strrchr(domain, '-');
20499396SMatthew.Ahrens@Sun.COM 		*cp = '\0';
20509396SMatthew.Ahrens@Sun.COM 		cp++;
20519396SMatthew.Ahrens@Sun.COM 
20529396SMatthew.Ahrens@Sun.COM 		errno = 0;
20539396SMatthew.Ahrens@Sun.COM 		*ridp = strtoull(cp, &end, 10);
205410160SMatthew.Ahrens@Sun.COM 		if (numericsid) {
205510160SMatthew.Ahrens@Sun.COM 			free(numericsid);
205610160SMatthew.Ahrens@Sun.COM 			numericsid = NULL;
205710160SMatthew.Ahrens@Sun.COM 		}
20589688SMatthew.Ahrens@Sun.COM 		if (errno != 0 || *end != '\0')
20599396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
20609396SMatthew.Ahrens@Sun.COM 	} else if (!isdigit(*cp)) {
20619396SMatthew.Ahrens@Sun.COM 		/*
20629396SMatthew.Ahrens@Sun.COM 		 * It's a user/group name (eg "user") that needs to be
20639396SMatthew.Ahrens@Sun.COM 		 * turned into a uid/gid
20649396SMatthew.Ahrens@Sun.COM 		 */
20659396SMatthew.Ahrens@Sun.COM 		if (zoned && getzoneid() == GLOBAL_ZONEID)
20669396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
20679396SMatthew.Ahrens@Sun.COM 		if (isuser) {
20689396SMatthew.Ahrens@Sun.COM 			struct passwd *pw;
20699396SMatthew.Ahrens@Sun.COM 			pw = getpwnam(cp);
20709396SMatthew.Ahrens@Sun.COM 			if (pw == NULL)
20719396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
20729396SMatthew.Ahrens@Sun.COM 			*ridp = pw->pw_uid;
20739396SMatthew.Ahrens@Sun.COM 		} else {
20749396SMatthew.Ahrens@Sun.COM 			struct group *gr;
20759396SMatthew.Ahrens@Sun.COM 			gr = getgrnam(cp);
20769396SMatthew.Ahrens@Sun.COM 			if (gr == NULL)
20779396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
20789396SMatthew.Ahrens@Sun.COM 			*ridp = gr->gr_gid;
20799396SMatthew.Ahrens@Sun.COM 		}
20809396SMatthew.Ahrens@Sun.COM 	} else {
20819396SMatthew.Ahrens@Sun.COM 		/* It's a user/group ID (eg "12345"). */
20829396SMatthew.Ahrens@Sun.COM 		uid_t id = strtoul(cp, &end, 10);
20839396SMatthew.Ahrens@Sun.COM 		idmap_rid_t rid;
20849396SMatthew.Ahrens@Sun.COM 		char *mapdomain;
20859396SMatthew.Ahrens@Sun.COM 
20869396SMatthew.Ahrens@Sun.COM 		if (*end != '\0')
20879396SMatthew.Ahrens@Sun.COM 			return (EINVAL);
20889396SMatthew.Ahrens@Sun.COM 		if (id > MAXUID) {
20899396SMatthew.Ahrens@Sun.COM 			/* It's an ephemeral ID. */
20909396SMatthew.Ahrens@Sun.COM 			if (idmap_id_to_numeric_domain_rid(id, isuser,
20919396SMatthew.Ahrens@Sun.COM 			    &mapdomain, &rid) != 0)
20929396SMatthew.Ahrens@Sun.COM 				return (ENOENT);
209310160SMatthew.Ahrens@Sun.COM 			(void) strlcpy(domain, mapdomain, domainlen);
20949396SMatthew.Ahrens@Sun.COM 			*ridp = rid;
20959396SMatthew.Ahrens@Sun.COM 		} else {
20969396SMatthew.Ahrens@Sun.COM 			*ridp = id;
20979396SMatthew.Ahrens@Sun.COM 		}
20989396SMatthew.Ahrens@Sun.COM 	}
20999396SMatthew.Ahrens@Sun.COM 
210010160SMatthew.Ahrens@Sun.COM 	ASSERT3P(numericsid, ==, NULL);
21019396SMatthew.Ahrens@Sun.COM 	return (0);
21029396SMatthew.Ahrens@Sun.COM }
21039396SMatthew.Ahrens@Sun.COM 
21049469SLin.Ling@Sun.COM static int
21059469SLin.Ling@Sun.COM zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
21069469SLin.Ling@Sun.COM     uint64_t *propvalue, zfs_userquota_prop_t *typep)
21079396SMatthew.Ahrens@Sun.COM {
21089396SMatthew.Ahrens@Sun.COM 	int err;
21099396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
21109396SMatthew.Ahrens@Sun.COM 
21119396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
21129396SMatthew.Ahrens@Sun.COM 
21139396SMatthew.Ahrens@Sun.COM 	err = userquota_propname_decode(propname,
21149396SMatthew.Ahrens@Sun.COM 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
21159469SLin.Ling@Sun.COM 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
21169469SLin.Ling@Sun.COM 	zc.zc_objset_type = *typep;
21179396SMatthew.Ahrens@Sun.COM 	if (err)
21189396SMatthew.Ahrens@Sun.COM 		return (err);
21199396SMatthew.Ahrens@Sun.COM 
21209396SMatthew.Ahrens@Sun.COM 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
21219396SMatthew.Ahrens@Sun.COM 	if (err)
21229396SMatthew.Ahrens@Sun.COM 		return (err);
21239396SMatthew.Ahrens@Sun.COM 
21249469SLin.Ling@Sun.COM 	*propvalue = zc.zc_cookie;
21259469SLin.Ling@Sun.COM 	return (0);
21269469SLin.Ling@Sun.COM }
21279469SLin.Ling@Sun.COM 
21289469SLin.Ling@Sun.COM int
21299469SLin.Ling@Sun.COM zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
21309469SLin.Ling@Sun.COM     uint64_t *propvalue)
21319469SLin.Ling@Sun.COM {
21329469SLin.Ling@Sun.COM 	zfs_userquota_prop_t type;
21339469SLin.Ling@Sun.COM 
21349469SLin.Ling@Sun.COM 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
21359469SLin.Ling@Sun.COM 	    &type));
21369469SLin.Ling@Sun.COM }
21379469SLin.Ling@Sun.COM 
21389469SLin.Ling@Sun.COM int
21399469SLin.Ling@Sun.COM zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
21409469SLin.Ling@Sun.COM     char *propbuf, int proplen, boolean_t literal)
21419469SLin.Ling@Sun.COM {
21429469SLin.Ling@Sun.COM 	int err;
21439469SLin.Ling@Sun.COM 	uint64_t propvalue;
21449469SLin.Ling@Sun.COM 	zfs_userquota_prop_t type;
21459469SLin.Ling@Sun.COM 
21469469SLin.Ling@Sun.COM 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
21479469SLin.Ling@Sun.COM 	    &type);
21489469SLin.Ling@Sun.COM 
21499469SLin.Ling@Sun.COM 	if (err)
21509469SLin.Ling@Sun.COM 		return (err);
21519469SLin.Ling@Sun.COM 
21529396SMatthew.Ahrens@Sun.COM 	if (literal) {
21539469SLin.Ling@Sun.COM 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
21549469SLin.Ling@Sun.COM 	} else if (propvalue == 0 &&
21559396SMatthew.Ahrens@Sun.COM 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
21569396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(propbuf, "none", proplen);
21579396SMatthew.Ahrens@Sun.COM 	} else {
21589469SLin.Ling@Sun.COM 		zfs_nicenum(propvalue, propbuf, proplen);
21599396SMatthew.Ahrens@Sun.COM 	}
21609396SMatthew.Ahrens@Sun.COM 	return (0);
21619396SMatthew.Ahrens@Sun.COM }
21629396SMatthew.Ahrens@Sun.COM 
2163789Sahrens /*
2164789Sahrens  * Returns the name of the given zfs handle.
2165789Sahrens  */
2166789Sahrens const char *
2167789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2168789Sahrens {
2169789Sahrens 	return (zhp->zfs_name);
2170789Sahrens }
2171789Sahrens 
2172789Sahrens /*
2173789Sahrens  * Returns the type of the given zfs handle.
2174789Sahrens  */
2175789Sahrens zfs_type_t
2176789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2177789Sahrens {
2178789Sahrens 	return (zhp->zfs_type);
2179789Sahrens }
2180789Sahrens 
21818228SEric.Taylor@Sun.COM static int
21828228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
21838228SEric.Taylor@Sun.COM {
21848228SEric.Taylor@Sun.COM 	int rc;
21858228SEric.Taylor@Sun.COM 	uint64_t	orig_cookie;
21868228SEric.Taylor@Sun.COM 
21878228SEric.Taylor@Sun.COM 	orig_cookie = zc->zc_cookie;
21888228SEric.Taylor@Sun.COM top:
21898228SEric.Taylor@Sun.COM 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
21908228SEric.Taylor@Sun.COM 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
21918228SEric.Taylor@Sun.COM 
21928228SEric.Taylor@Sun.COM 	if (rc == -1) {
21938228SEric.Taylor@Sun.COM 		switch (errno) {
21948228SEric.Taylor@Sun.COM 		case ENOMEM:
21958228SEric.Taylor@Sun.COM 			/* expand nvlist memory and try again */
21968228SEric.Taylor@Sun.COM 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
21978228SEric.Taylor@Sun.COM 				zcmd_free_nvlists(zc);
21988228SEric.Taylor@Sun.COM 				return (-1);
21998228SEric.Taylor@Sun.COM 			}
22008228SEric.Taylor@Sun.COM 			zc->zc_cookie = orig_cookie;
22018228SEric.Taylor@Sun.COM 			goto top;
22028228SEric.Taylor@Sun.COM 		/*
22038228SEric.Taylor@Sun.COM 		 * An errno value of ESRCH indicates normal completion.
22048228SEric.Taylor@Sun.COM 		 * If ENOENT is returned, then the underlying dataset
22058228SEric.Taylor@Sun.COM 		 * has been removed since we obtained the handle.
22068228SEric.Taylor@Sun.COM 		 */
22078228SEric.Taylor@Sun.COM 		case ESRCH:
22088228SEric.Taylor@Sun.COM 		case ENOENT:
22098228SEric.Taylor@Sun.COM 			rc = 1;
22108228SEric.Taylor@Sun.COM 			break;
22118228SEric.Taylor@Sun.COM 		default:
22128228SEric.Taylor@Sun.COM 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
22138228SEric.Taylor@Sun.COM 			    dgettext(TEXT_DOMAIN,
22148228SEric.Taylor@Sun.COM 			    "cannot iterate filesystems"));
22158228SEric.Taylor@Sun.COM 			break;
22168228SEric.Taylor@Sun.COM 		}
22178228SEric.Taylor@Sun.COM 	}
22188228SEric.Taylor@Sun.COM 	return (rc);
22198228SEric.Taylor@Sun.COM }
22208228SEric.Taylor@Sun.COM 
2221789Sahrens /*
22221356Seschrock  * Iterate over all child filesystems
2223789Sahrens  */
2224789Sahrens int
22251356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2226789Sahrens {
2227789Sahrens 	zfs_cmd_t zc = { 0 };
2228789Sahrens 	zfs_handle_t *nzhp;
2229789Sahrens 	int ret;
2230789Sahrens 
22315367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
22325367Sahrens 		return (0);
22335367Sahrens 
22348228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22358228SEric.Taylor@Sun.COM 		return (-1);
22368228SEric.Taylor@Sun.COM 
22378228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
22388228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
2239789Sahrens 		/*
2240789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2241789Sahrens 		 * that the pool has since been removed.
2242789Sahrens 		 */
22438228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
22448228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2245789Sahrens 			continue;
22468228SEric.Taylor@Sun.COM 		}
22478228SEric.Taylor@Sun.COM 
22488228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
22498228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2250789Sahrens 			return (ret);
22518228SEric.Taylor@Sun.COM 		}
2252789Sahrens 	}
22538228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
22548228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
22551356Seschrock }
22561356Seschrock 
22571356Seschrock /*
22581356Seschrock  * Iterate over all snapshots
22591356Seschrock  */
22601356Seschrock int
22611356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
22621356Seschrock {
22631356Seschrock 	zfs_cmd_t zc = { 0 };
22641356Seschrock 	zfs_handle_t *nzhp;
22651356Seschrock 	int ret;
2266789Sahrens 
22675367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
22685367Sahrens 		return (0);
22695367Sahrens 
22708228SEric.Taylor@Sun.COM 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22718228SEric.Taylor@Sun.COM 		return (-1);
22728228SEric.Taylor@Sun.COM 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
22738228SEric.Taylor@Sun.COM 	    &zc)) == 0) {
22748228SEric.Taylor@Sun.COM 
22758228SEric.Taylor@Sun.COM 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
22768228SEric.Taylor@Sun.COM 		    &zc)) == NULL) {
2277789Sahrens 			continue;
22788228SEric.Taylor@Sun.COM 		}
22798228SEric.Taylor@Sun.COM 
22808228SEric.Taylor@Sun.COM 		if ((ret = func(nzhp, data)) != 0) {
22818228SEric.Taylor@Sun.COM 			zcmd_free_nvlists(&zc);
2282789Sahrens 			return (ret);
22838228SEric.Taylor@Sun.COM 		}
2284789Sahrens 	}
22858228SEric.Taylor@Sun.COM 	zcmd_free_nvlists(&zc);
22868228SEric.Taylor@Sun.COM 	return ((ret < 0) ? ret : 0);
2287789Sahrens }
2288789Sahrens 
2289789Sahrens /*
22901356Seschrock  * Iterate over all children, snapshots and filesystems
22911356Seschrock  */
22921356Seschrock int
22931356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
22941356Seschrock {
22951356Seschrock 	int ret;
22961356Seschrock 
22971356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
22981356Seschrock 		return (ret);
22991356Seschrock 
23001356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
23011356Seschrock }
23021356Seschrock 
23031356Seschrock /*
2304789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2305789Sahrens  * Can return NULL if this is a pool.
2306789Sahrens  */
2307789Sahrens static int
2308789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2309789Sahrens {
2310789Sahrens 	char *loc;
2311789Sahrens 
2312789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2313789Sahrens 		return (-1);
2314789Sahrens 
2315789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2316789Sahrens 	buf[loc - path] = '\0';
2317789Sahrens 
2318789Sahrens 	return (0);
2319789Sahrens }
2320789Sahrens 
2321789Sahrens /*
23224490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
23234490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
23244490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
23254490Svb160487  * length of already existing prefix of the given path.  We also fetch the
23264490Svb160487  * 'zoned' property, which is used to validate property settings when creating
23274490Svb160487  * new datasets.
2328789Sahrens  */
2329789Sahrens static int
23304490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
23314490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2332789Sahrens {
2333789Sahrens 	zfs_cmd_t zc = { 0 };
2334789Sahrens 	char parent[ZFS_MAXNAMELEN];
2335789Sahrens 	char *slash;
23361356Seschrock 	zfs_handle_t *zhp;
23372082Seschrock 	char errbuf[1024];
23382082Seschrock 
23398269SMark.Musante@Sun.COM 	(void) snprintf(errbuf, sizeof (errbuf),
23408269SMark.Musante@Sun.COM 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2341789Sahrens 
2342789Sahrens 	/* get parent, and check to see if this is just a pool */
2343789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
23442082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23452082Seschrock 		    "missing dataset name"));
23462082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2347789Sahrens 	}
2348789Sahrens 
2349789Sahrens 	/* check to see if the pool exists */
2350789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2351789Sahrens 		slash = parent + strlen(parent);
2352789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2353789Sahrens 	zc.zc_name[slash - parent] = '\0';
23542082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2355789Sahrens 	    errno == ENOENT) {
23562082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23572082Seschrock 		    "no such pool '%s'"), zc.zc_name);
23582082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2359789Sahrens 	}
2360789Sahrens 
2361789Sahrens 	/* check to see if the parent dataset exists */
23624490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
23634490Svb160487 		if (errno == ENOENT && accept_ancestor) {
23644490Svb160487 			/*
23654490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
23664490Svb160487 			 */
23674490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
23684490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23694490Svb160487 				    "no such pool '%s'"), zc.zc_name);
23704490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
23714490Svb160487 			}
23724490Svb160487 		} else if (errno == ENOENT) {
23732082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23742082Seschrock 			    "parent does not exist"));
23752082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
23764490Svb160487 		} else
23772082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2378789Sahrens 	}
2379789Sahrens 
23802676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2381789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
23822676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
23832082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
23841356Seschrock 		zfs_close(zhp);
2385789Sahrens 		return (-1);
2386789Sahrens 	}
2387789Sahrens 
2388789Sahrens 	/* make sure parent is a filesystem */
23891356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
23902082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
23912082Seschrock 		    "parent is not a filesystem"));
23922082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
23931356Seschrock 		zfs_close(zhp);
2394789Sahrens 		return (-1);
2395789Sahrens 	}
2396789Sahrens 
23971356Seschrock 	zfs_close(zhp);
23984490Svb160487 	if (prefixlen != NULL)
23994490Svb160487 		*prefixlen = strlen(parent);
24004490Svb160487 	return (0);
24014490Svb160487 }
24024490Svb160487 
24034490Svb160487 /*
24044490Svb160487  * Finds whether the dataset of the given type(s) exists.
24054490Svb160487  */
24064490Svb160487 boolean_t
24074490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
24084490Svb160487 {
24094490Svb160487 	zfs_handle_t *zhp;
24104490Svb160487 
24115326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
24124490Svb160487 		return (B_FALSE);
24134490Svb160487 
24144490Svb160487 	/*
24154490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
24164490Svb160487 	 */
24174490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
24184490Svb160487 		int ds_type = zhp->zfs_type;
24194490Svb160487 
24204490Svb160487 		zfs_close(zhp);
24214490Svb160487 		if (types & ds_type)
24224490Svb160487 			return (B_TRUE);
24234490Svb160487 	}
24244490Svb160487 	return (B_FALSE);
24254490Svb160487 }
24264490Svb160487 
24274490Svb160487 /*
24285367Sahrens  * Given a path to 'target', create all the ancestors between
24295367Sahrens  * the prefixlen portion of the path, and the target itself.
24305367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
24315367Sahrens  */
24325367Sahrens int
24335367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
24345367Sahrens {
24355367Sahrens 	zfs_handle_t *h;
24365367Sahrens 	char *cp;
24375367Sahrens 	const char *opname;
24385367Sahrens 
24395367Sahrens 	/* make sure prefix exists */
24405367Sahrens 	cp = target + prefixlen;
24415367Sahrens 	if (*cp != '/') {
24425367Sahrens 		assert(strchr(cp, '/') == NULL);
24435367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24445367Sahrens 	} else {
24455367Sahrens 		*cp = '\0';
24465367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24475367Sahrens 		*cp = '/';
24485367Sahrens 	}
24495367Sahrens 	if (h == NULL)
24505367Sahrens 		return (-1);
24515367Sahrens 	zfs_close(h);
24525367Sahrens 
24535367Sahrens 	/*
24545367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
24555367Sahrens 	 * up to the prefixlen-long one.
24565367Sahrens 	 */
24575367Sahrens 	for (cp = target + prefixlen + 1;
24585367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
24595367Sahrens 		char *logstr;
24605367Sahrens 
24615367Sahrens 		*cp = '\0';
24625367Sahrens 
24635367Sahrens 		h = make_dataset_handle(hdl, target);
24645367Sahrens 		if (h) {
24655367Sahrens 			/* it already exists, nothing to do here */
24665367Sahrens 			zfs_close(h);
24675367Sahrens 			continue;
24685367Sahrens 		}
24695367Sahrens 
24705367Sahrens 		logstr = hdl->libzfs_log_str;
24715367Sahrens 		hdl->libzfs_log_str = NULL;
24725367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
24735367Sahrens 		    NULL) != 0) {
24745367Sahrens 			hdl->libzfs_log_str = logstr;
24755367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
24765367Sahrens 			goto ancestorerr;
24775367Sahrens 		}
24785367Sahrens 
24795367Sahrens 		hdl->libzfs_log_str = logstr;
24805367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
24815367Sahrens 		if (h == NULL) {
24825367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
24835367Sahrens 			goto ancestorerr;
24845367Sahrens 		}
24855367Sahrens 
24865367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
24875367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
24885367Sahrens 			goto ancestorerr;
24895367Sahrens 		}
24905367Sahrens 
24915367Sahrens 		if (zfs_share(h) != 0) {
24925367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
24935367Sahrens 			goto ancestorerr;
24945367Sahrens 		}
24955367Sahrens 
24965367Sahrens 		zfs_close(h);
24975367Sahrens 	}
24985367Sahrens 
24995367Sahrens 	return (0);
25005367Sahrens 
25015367Sahrens ancestorerr:
25025367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25035367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
25045367Sahrens 	return (-1);
25055367Sahrens }
25065367Sahrens 
25075367Sahrens /*
25084490Svb160487  * Creates non-existing ancestors of the given path.
25094490Svb160487  */
25104490Svb160487 int
25114490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
25124490Svb160487 {
25134490Svb160487 	int prefix;
25144490Svb160487 	uint64_t zoned;
25154490Svb160487 	char *path_copy;
25164490Svb160487 	int rc;
25174490Svb160487 
25184490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
25194490Svb160487 		return (-1);
25204490Svb160487 
25214490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
25224490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
25234490Svb160487 		free(path_copy);
25244490Svb160487 	}
25254490Svb160487 	if (path_copy == NULL || rc != 0)
25264490Svb160487 		return (-1);
25274490Svb160487 
2528789Sahrens 	return (0);
2529789Sahrens }
2530789Sahrens 
2531789Sahrens /*
25322676Seschrock  * Create a new filesystem or volume.
2533789Sahrens  */
2534789Sahrens int
25352082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
25362676Seschrock     nvlist_t *props)
2537789Sahrens {
2538789Sahrens 	zfs_cmd_t zc = { 0 };
2539789Sahrens 	int ret;
2540789Sahrens 	uint64_t size = 0;
2541789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
25422082Seschrock 	char errbuf[1024];
25432676Seschrock 	uint64_t zoned;
25442082Seschrock 
25452082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
25462082Seschrock 	    "cannot create '%s'"), path);
2547789Sahrens 
2548789Sahrens 	/* validate the path, taking care to note the extended error message */
25495326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
25502082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2551789Sahrens 
2552789Sahrens 	/* validate parents exist */
25534490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2554789Sahrens 		return (-1);
2555789Sahrens 
2556789Sahrens 	/*
2557789Sahrens 	 * The failure modes when creating a dataset of a different type over
2558789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2559789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2560789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2561789Sahrens 	 * first try to see if the dataset exists.
2562789Sahrens 	 */
2563789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
25645094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
25652082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25662082Seschrock 		    "dataset already exists"));
25672082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2568789Sahrens 	}
2569789Sahrens 
2570789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2571789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2572789Sahrens 	else
2573789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2574789Sahrens 
25757184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
25763912Slling 	    zoned, NULL, errbuf)) == 0)
25772676Seschrock 		return (-1);
25782676Seschrock 
2579789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
25801133Seschrock 		/*
25811133Seschrock 		 * If we are creating a volume, the size and block size must
25821133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
25831133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
25841133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
25851133Seschrock 		 * zero.
25861133Seschrock 		 */
25872676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
25882676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
25892676Seschrock 			nvlist_free(props);
25902082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25912676Seschrock 			    "missing volume size"));
25922676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2593789Sahrens 		}
2594789Sahrens 
25952676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
25962676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
25972676Seschrock 		    &blocksize)) != 0) {
25982676Seschrock 			if (ret == ENOENT) {
25992676Seschrock 				blocksize = zfs_prop_default_numeric(
26002676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
26012676Seschrock 			} else {
26022676Seschrock 				nvlist_free(props);
26032676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26042676Seschrock 				    "missing volume block size"));
26052676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26062676Seschrock 			}
26072676Seschrock 		}
26082676Seschrock 
26092676Seschrock 		if (size == 0) {
26102676Seschrock 			nvlist_free(props);
26112082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26122676Seschrock 			    "volume size cannot be zero"));
26132676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26141133Seschrock 		}
26151133Seschrock 
26161133Seschrock 		if (size % blocksize != 0) {
26172676Seschrock 			nvlist_free(props);
26182082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26192676Seschrock 			    "volume size must be a multiple of volume block "
26202676Seschrock 			    "size"));
26212676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26221133Seschrock 		}
2623789Sahrens 	}
2624789Sahrens 
26255094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
26262676Seschrock 		return (-1);
26272676Seschrock 	nvlist_free(props);
26282676Seschrock 
2629789Sahrens 	/* create the dataset */
26304543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2631789Sahrens 
26322676Seschrock 	zcmd_free_nvlists(&zc);
26332676Seschrock 
2634789Sahrens 	/* check for failure */
2635789Sahrens 	if (ret != 0) {
2636789Sahrens 		char parent[ZFS_MAXNAMELEN];
2637789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
2638789Sahrens 
2639789Sahrens 		switch (errno) {
2640789Sahrens 		case ENOENT:
26412082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26422082Seschrock 			    "no such parent '%s'"), parent);
26432082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2644789Sahrens 
2645789Sahrens 		case EINVAL:
26462082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26473413Smmusante 			    "parent '%s' is not a filesystem"), parent);
26482082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2649789Sahrens 
2650789Sahrens 		case EDOM:
26512082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26522676Seschrock 			    "volume block size must be power of 2 from "
26532676Seschrock 			    "%u to %uk"),
2654789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
2655789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
26562082Seschrock 
26572676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
26582082Seschrock 
26594603Sahrens 		case ENOTSUP:
26604603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
26614603Sahrens 			    "pool must be upgraded to set this "
26624603Sahrens 			    "property or value"));
26634603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2664789Sahrens #ifdef _ILP32
2665789Sahrens 		case EOVERFLOW:
2666789Sahrens 			/*
2667789Sahrens 			 * This platform can't address a volume this big.
2668789Sahrens 			 */
26692082Seschrock 			if (type == ZFS_TYPE_VOLUME)
26702082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
26712082Seschrock 				    errbuf));
2672789Sahrens #endif
26732082Seschrock 			/* FALLTHROUGH */
2674789Sahrens 		default:
26752082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2676789Sahrens 		}
2677789Sahrens 	}
2678789Sahrens 
2679789Sahrens 	return (0);
2680789Sahrens }
2681789Sahrens 
2682789Sahrens /*
2683789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
2684789Sahrens  * isn't mounted, and that there are no active dependents.
2685789Sahrens  */
2686789Sahrens int
268710242Schris.kirby@sun.com zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2688789Sahrens {
2689789Sahrens 	zfs_cmd_t zc = { 0 };
2690789Sahrens 
2691789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2692789Sahrens 
26932676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
26943126Sahl 		/*
26954543Smarks 		 * If user doesn't have permissions to unshare volume, then
26964543Smarks 		 * abort the request.  This would only happen for a
26974543Smarks 		 * non-privileged user.
26983126Sahl 		 */
26994543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
27004543Smarks 			return (-1);
27014543Smarks 		}
27023126Sahl 
2703789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2704789Sahrens 	} else {
2705789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2706789Sahrens 	}
2707789Sahrens 
270810242Schris.kirby@sun.com 	zc.zc_defer_destroy = defer;
27094543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
27103237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
27112082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
27122082Seschrock 		    zhp->zfs_name));
27132199Sahrens 	}
2714789Sahrens 
2715789Sahrens 	remove_mountpoint(zhp);
2716789Sahrens 
2717789Sahrens 	return (0);
2718789Sahrens }
2719789Sahrens 
27202199Sahrens struct destroydata {
27212199Sahrens 	char *snapname;
27222199Sahrens 	boolean_t gotone;
27233265Sahrens 	boolean_t closezhp;
27242199Sahrens };
27252199Sahrens 
27262199Sahrens static int
2727*10588SEric.Taylor@Sun.COM zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
27282199Sahrens {
27292199Sahrens 	struct destroydata *dd = arg;
27302199Sahrens 	zfs_handle_t *szhp;
27312199Sahrens 	char name[ZFS_MAXNAMELEN];
27323265Sahrens 	boolean_t closezhp = dd->closezhp;
2733*10588SEric.Taylor@Sun.COM 	int rv = 0;
27342199Sahrens 
27352676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
27362676Seschrock 	(void) strlcat(name, "@", sizeof (name));
27372676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
27382199Sahrens 
27392199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
27402199Sahrens 	if (szhp) {
27412199Sahrens 		dd->gotone = B_TRUE;
27422199Sahrens 		zfs_close(szhp);
27432199Sahrens 	}
27442199Sahrens 
27453265Sahrens 	dd->closezhp = B_TRUE;
2746*10588SEric.Taylor@Sun.COM 	if (!dd->gotone)
2747*10588SEric.Taylor@Sun.COM 		rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
27483265Sahrens 	if (closezhp)
27493265Sahrens 		zfs_close(zhp);
27503265Sahrens 	return (rv);
27512199Sahrens }
27522199Sahrens 
27532199Sahrens /*
27542199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
27552199Sahrens  */
27562199Sahrens int
275710242Schris.kirby@sun.com zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
27582199Sahrens {
27592199Sahrens 	zfs_cmd_t zc = { 0 };
27602199Sahrens 	int ret;
27612199Sahrens 	struct destroydata dd = { 0 };
27622199Sahrens 
27632199Sahrens 	dd.snapname = snapname;
2764*10588SEric.Taylor@Sun.COM 	(void) zfs_check_snap_cb(zhp, &dd);
27652199Sahrens 
27662199Sahrens 	if (!dd.gotone) {
27673237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
27682199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
27692199Sahrens 		    zhp->zfs_name, snapname));
27702199Sahrens 	}
27712199Sahrens 
27722199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
27732676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
277410242Schris.kirby@sun.com 	zc.zc_defer_destroy = defer;
27752199Sahrens 
27764543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
27772199Sahrens 	if (ret != 0) {
27782199Sahrens 		char errbuf[1024];
27792199Sahrens 
27802199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
27812199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
27822199Sahrens 
27832199Sahrens 		switch (errno) {
27842199Sahrens 		case EEXIST:
27852199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
27862199Sahrens 			    "snapshot is cloned"));
27872199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
27882199Sahrens 
27892199Sahrens 		default:
27902199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
27912199Sahrens 			    errbuf));
27922199Sahrens 		}
27932199Sahrens 	}
27942199Sahrens 
27952199Sahrens 	return (0);
27962199Sahrens }
27972199Sahrens 
2798789Sahrens /*
2799789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
2800789Sahrens  */
2801789Sahrens int
28022676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2803789Sahrens {
2804789Sahrens 	zfs_cmd_t zc = { 0 };
2805789Sahrens 	char parent[ZFS_MAXNAMELEN];
2806789Sahrens 	int ret;
28072082Seschrock 	char errbuf[1024];
28082082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
28092676Seschrock 	zfs_type_t type;
28102676Seschrock 	uint64_t zoned;
2811789Sahrens 
2812789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2813789Sahrens 
28142082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
28152082Seschrock 	    "cannot create '%s'"), target);
28162082Seschrock 
2817789Sahrens 	/* validate the target name */
28185326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
28192082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2820789Sahrens 
2821789Sahrens 	/* validate parents exist */
28224490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2823789Sahrens 		return (-1);
2824789Sahrens 
2825789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
2826789Sahrens 
2827789Sahrens 	/* do the clone */
28282676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
2829789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
28302744Snn35248 		type = ZFS_TYPE_VOLUME;
28312676Seschrock 	} else {
2832789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
28332744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
28342676Seschrock 	}
28352676Seschrock 
28362676Seschrock 	if (props) {
28377184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
28387184Stimh 		    zhp, errbuf)) == NULL)
28392676Seschrock 			return (-1);
28402676Seschrock 
28415094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
28422676Seschrock 			nvlist_free(props);
28432676Seschrock 			return (-1);
28442676Seschrock 		}
28452676Seschrock 
28462676Seschrock 		nvlist_free(props);
28472676Seschrock 	}
2848789Sahrens 
2849789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
28502676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
28514543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2852789Sahrens 
28532676Seschrock 	zcmd_free_nvlists(&zc);
28542676Seschrock 
2855789Sahrens 	if (ret != 0) {
2856789Sahrens 		switch (errno) {
2857789Sahrens 
2858789Sahrens 		case ENOENT:
2859789Sahrens 			/*
2860789Sahrens 			 * The parent doesn't exist.  We should have caught this
2861789Sahrens 			 * above, but there may a race condition that has since
2862789Sahrens 			 * destroyed the parent.
2863789Sahrens 			 *
2864789Sahrens 			 * At this point, we don't know whether it's the source
2865789Sahrens 			 * that doesn't exist anymore, or whether the target
2866789Sahrens 			 * dataset doesn't exist.
2867789Sahrens 			 */
28682082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28692082Seschrock 			    "no such parent '%s'"), parent);
28702082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
28712082Seschrock 
28722082Seschrock 		case EXDEV:
28732082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
28742082Seschrock 			    "source and target pools differ"));
28752082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
28762082Seschrock 			    errbuf));
28772082Seschrock 
28782082Seschrock 		default:
28792082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
28802082Seschrock 			    errbuf));
28812082Seschrock 		}
28822082Seschrock 	}
28832082Seschrock 
28842082Seschrock 	return (ret);
28852082Seschrock }
28862082Seschrock 
28872082Seschrock /*
28882082Seschrock  * Promotes the given clone fs to be the clone parent.
28892082Seschrock  */
28902082Seschrock int
28912082Seschrock zfs_promote(zfs_handle_t *zhp)
28922082Seschrock {
28932082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
28942082Seschrock 	zfs_cmd_t zc = { 0 };
28952082Seschrock 	char parent[MAXPATHLEN];
28962082Seschrock 	int ret;
28972082Seschrock 	char errbuf[1024];
28982082Seschrock 
28992082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29002082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
29012082Seschrock 
29022082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
29032082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29042082Seschrock 		    "snapshots can not be promoted"));
29052082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
29062082Seschrock 	}
29072082Seschrock 
29085367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
29092082Seschrock 	if (parent[0] == '\0') {
29102082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29112082Seschrock 		    "not a cloned filesystem"));
29122082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
29132082Seschrock 	}
2914*10588SEric.Taylor@Sun.COM 
29155367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
29162676Seschrock 	    sizeof (zc.zc_value));
29172082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
29184543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
29192082Seschrock 
29202082Seschrock 	if (ret != 0) {
29212417Sahrens 		int save_errno = errno;
29222417Sahrens 
29232417Sahrens 		switch (save_errno) {
2924789Sahrens 		case EEXIST:
2925*10588SEric.Taylor@Sun.COM 			/* There is a conflicting snapshot name. */
29262082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2927*10588SEric.Taylor@Sun.COM 			    "conflicting snapshot '%s' from parent '%s'"),
2928*10588SEric.Taylor@Sun.COM 			    zc.zc_string, parent);
29292082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2930789Sahrens 
2931789Sahrens 		default:
29322417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
2933789Sahrens 		}
2934789Sahrens 	}
29352676Seschrock 	return (ret);
29362199Sahrens }
29372199Sahrens 
2938789Sahrens /*
29393504Sahl  * Takes a snapshot of the given dataset.
2940789Sahrens  */
2941789Sahrens int
29427265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
29437265Sahrens     nvlist_t *props)
2944789Sahrens {
2945789Sahrens 	const char *delim;
29467265Sahrens 	char parent[ZFS_MAXNAMELEN];
2947789Sahrens 	zfs_handle_t *zhp;
2948789Sahrens 	zfs_cmd_t zc = { 0 };
2949789Sahrens 	int ret;
29502082Seschrock 	char errbuf[1024];
29512082Seschrock 
29522082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29532082Seschrock 	    "cannot snapshot '%s'"), path);
29542082Seschrock 
29552082Seschrock 	/* validate the target name */
29565326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
29572082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2958789Sahrens 
29597265Sahrens 	if (props) {
29607265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
29617265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
29627265Sahrens 			return (-1);
29637265Sahrens 
29647265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
29657265Sahrens 			nvlist_free(props);
29667265Sahrens 			return (-1);
29677265Sahrens 		}
29687265Sahrens 
29697265Sahrens 		nvlist_free(props);
29707265Sahrens 	}
29717265Sahrens 
2972789Sahrens 	/* make sure the parent exists and is of the appropriate type */
29732199Sahrens 	delim = strchr(path, '@');
2974789Sahrens 	(void) strncpy(parent, path, delim - path);
2975789Sahrens 	parent[delim - path] = '\0';
2976789Sahrens 
29772082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
2978789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
29797265Sahrens 		zcmd_free_nvlists(&zc);
2980789Sahrens 		return (-1);
2981789Sahrens 	}
2982789Sahrens 
29832199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
29842676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
29854543Smarks 	if (ZFS_IS_VOLUME(zhp))
29864543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
29874543Smarks 	else
29884543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
29892199Sahrens 	zc.zc_cookie = recursive;
29904543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
29912199Sahrens 
29927265Sahrens 	zcmd_free_nvlists(&zc);
29937265Sahrens 
29942199Sahrens 	/*
29952199Sahrens 	 * if it was recursive, the one that actually failed will be in
29962199Sahrens 	 * zc.zc_name.
29972199Sahrens 	 */
2998*10588SEric.Taylor@Sun.COM 	if (ret != 0) {
29994543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
30004543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3001*10588SEric.Taylor@Sun.COM 		(void) zfs_standard_error(hdl, errno, errbuf);
30022199Sahrens 	}
3003789Sahrens 
3004789Sahrens 	zfs_close(zhp);
3005789Sahrens 
3006789Sahrens 	return (ret);
3007789Sahrens }
3008789Sahrens 
3009789Sahrens /*
30101294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
30111294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
30121294Slling  * is a dependent and we should just destroy it without checking the transaction
30131294Slling  * group.
3014789Sahrens  */
30151294Slling typedef struct rollback_data {
30161294Slling 	const char	*cb_target;		/* the snapshot */
30171294Slling 	uint64_t	cb_create;		/* creation time reference */
30185749Sahrens 	boolean_t	cb_error;
30192082Seschrock 	boolean_t	cb_dependent;
30205749Sahrens 	boolean_t	cb_force;
30211294Slling } rollback_data_t;
30221294Slling 
30231294Slling static int
30241294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
30251294Slling {
30261294Slling 	rollback_data_t *cbp = data;
30271294Slling 
30281294Slling 	if (!cbp->cb_dependent) {
30291294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
30301294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
30311294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
30321294Slling 		    cbp->cb_create) {
30334543Smarks 			char *logstr;
30341294Slling 
30352082Seschrock 			cbp->cb_dependent = B_TRUE;
30365446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
30375446Sahrens 			    rollback_destroy, cbp);
30382082Seschrock 			cbp->cb_dependent = B_FALSE;
30391294Slling 
30404543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
30414543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
304210242Schris.kirby@sun.com 			cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
30434543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
30441294Slling 		}
30451294Slling 	} else {
30465749Sahrens 		/* We must destroy this clone; first unmount it */
30475749Sahrens 		prop_changelist_t *clp;
30485749Sahrens 
30497366STim.Haley@Sun.COM 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
30505749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
30515749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
30525749Sahrens 			cbp->cb_error = B_TRUE;
30535749Sahrens 			zfs_close(zhp);
30545749Sahrens 			return (0);
30555749Sahrens 		}
305610242Schris.kirby@sun.com 		if (zfs_destroy(zhp, B_FALSE) != 0)
30575749Sahrens 			cbp->cb_error = B_TRUE;
30585749Sahrens 		else
30595749Sahrens 			changelist_remove(clp, zhp->zfs_name);
30605751Sahrens 		(void) changelist_postfix(clp);
30615749Sahrens 		changelist_free(clp);
30621294Slling 	}
30631294Slling 
30641294Slling 	zfs_close(zhp);
30651294Slling 	return (0);
30661294Slling }
30671294Slling 
30681294Slling /*
30695446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
30705446Sahrens  * data changes since then and making it the active dataset.
30715446Sahrens  *
30725446Sahrens  * Any snapshots more recent than the target are destroyed, along with
30735446Sahrens  * their dependents.
30741294Slling  */
30755446Sahrens int
30765749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3077789Sahrens {
30785446Sahrens 	rollback_data_t cb = { 0 };
30795446Sahrens 	int err;
3080789Sahrens 	zfs_cmd_t zc = { 0 };
30815713Srm160521 	boolean_t restore_resv = 0;
30825713Srm160521 	uint64_t old_volsize, new_volsize;
30835713Srm160521 	zfs_prop_t resv_prop;
3084789Sahrens 
3085789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3086789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3087789Sahrens 
30885446Sahrens 	/*
30895446Sahrens 	 * Destroy all recent snapshots and its dependends.
30905446Sahrens 	 */
30915749Sahrens 	cb.cb_force = force;
30925446Sahrens 	cb.cb_target = snap->zfs_name;
30935446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
30945446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
30955446Sahrens 
30965749Sahrens 	if (cb.cb_error)
30975749Sahrens 		return (-1);
30985446Sahrens 
30995446Sahrens 	/*
31005446Sahrens 	 * Now that we have verified that the snapshot is the latest,
31015446Sahrens 	 * rollback to the given snapshot.
31025446Sahrens 	 */
31035446Sahrens 
31045713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31055713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
31065713Srm160521 			return (-1);
31075713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
31085713Srm160521 		restore_resv =
31095713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
31105713Srm160521 	}
3111789Sahrens 
3112789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3113789Sahrens 
31142676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3115789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3116789Sahrens 	else
3117789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3118789Sahrens 
3119789Sahrens 	/*
31205446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
31215446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
31225446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
31235446Sahrens 	 * an unlikely race condition where the user has taken a
31245446Sahrens 	 * snapshot since we verified that this was the most recent.
31255713Srm160521 	 *
3126789Sahrens 	 */
31275446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
31283237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
31292082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
31302082Seschrock 		    zhp->zfs_name);
31315717Srm160521 		return (err);
31325717Srm160521 	}
31335713Srm160521 
31345713Srm160521 	/*
31355713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
31365713Srm160521 	 * rollback reservation and the volsize has changed then set
31375713Srm160521 	 * the reservation property to the post-rollback volsize.
31385713Srm160521 	 * Make a new handle since the rollback closed the dataset.
31395713Srm160521 	 */
31405717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
31415717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
31425713Srm160521 		if (restore_resv) {
31435713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
31445713Srm160521 			if (old_volsize != new_volsize)
31455717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
31465717Srm160521 				    new_volsize);
31475713Srm160521 		}
31485713Srm160521 		zfs_close(zhp);
3149789Sahrens 	}
31505446Sahrens 	return (err);
31511294Slling }
31521294Slling 
31531294Slling /*
3154789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3155789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3156789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3157789Sahrens  * libzfs_graph.c.
3158789Sahrens  */
3159789Sahrens int
31602474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
31612474Seschrock     zfs_iter_f func, void *data)
3162789Sahrens {
3163789Sahrens 	char **dependents;
3164789Sahrens 	size_t count;
3165789Sahrens 	int i;
3166789Sahrens 	zfs_handle_t *child;
3167789Sahrens 	int ret = 0;
3168789Sahrens 
31692474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
31702474Seschrock 	    &dependents, &count) != 0)
31712474Seschrock 		return (-1);
31722474Seschrock 
3173789Sahrens 	for (i = 0; i < count; i++) {
31742082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
31752082Seschrock 		    dependents[i])) == NULL)
3176789Sahrens 			continue;
3177789Sahrens 
3178789Sahrens 		if ((ret = func(child, data)) != 0)
3179789Sahrens 			break;
3180789Sahrens 	}
3181789Sahrens 
3182789Sahrens 	for (i = 0; i < count; i++)
3183789Sahrens 		free(dependents[i]);
3184789Sahrens 	free(dependents);
3185789Sahrens 
3186789Sahrens 	return (ret);
3187789Sahrens }
3188789Sahrens 
3189789Sahrens /*
3190789Sahrens  * Renames the given dataset.
3191789Sahrens  */
3192789Sahrens int
31934490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3194789Sahrens {
3195789Sahrens 	int ret;
3196789Sahrens 	zfs_cmd_t zc = { 0 };
3197789Sahrens 	char *delim;
31984007Smmusante 	prop_changelist_t *cl = NULL;
31994007Smmusante 	zfs_handle_t *zhrp = NULL;
32004007Smmusante 	char *parentname = NULL;
3201789Sahrens 	char parent[ZFS_MAXNAMELEN];
32022082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
32032082Seschrock 	char errbuf[1024];
3204789Sahrens 
3205789Sahrens 	/* if we have the same exact name, just return success */
3206789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3207789Sahrens 		return (0);
3208789Sahrens 
32092082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32102082Seschrock 	    "cannot rename to '%s'"), target);
32112082Seschrock 
3212789Sahrens 	/*
3213789Sahrens 	 * Make sure the target name is valid
3214789Sahrens 	 */
3215789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
32162665Snd150628 		if ((strchr(target, '@') == NULL) ||
32172665Snd150628 		    *target == '@') {
32182665Snd150628 			/*
32192665Snd150628 			 * Snapshot target name is abbreviated,
32202665Snd150628 			 * reconstruct full dataset name
32212665Snd150628 			 */
32222665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
32232665Snd150628 			    sizeof (parent));
32242665Snd150628 			delim = strchr(parent, '@');
32252665Snd150628 			if (strchr(target, '@') == NULL)
32262665Snd150628 				*(++delim) = '\0';
32272665Snd150628 			else
32282665Snd150628 				*delim = '\0';
32292665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
32302665Snd150628 			target = parent;
32312665Snd150628 		} else {
32322665Snd150628 			/*
32332665Snd150628 			 * Make sure we're renaming within the same dataset.
32342665Snd150628 			 */
32352665Snd150628 			delim = strchr(target, '@');
32362665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
32372665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
32382665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32392665Snd150628 				    "snapshots must be part of same "
32402665Snd150628 				    "dataset"));
32412665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
32423912Slling 				    errbuf));
32432665Snd150628 			}
3244789Sahrens 		}
32455326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
32462665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3247789Sahrens 	} else {
32484007Smmusante 		if (recursive) {
32494007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32504007Smmusante 			    "recursive rename must be a snapshot"));
32514007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
32524007Smmusante 		}
32534007Smmusante 
32545326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
32552665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
32562676Seschrock 		uint64_t unused;
32572676Seschrock 
3258789Sahrens 		/* validate parents */
32594490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3260789Sahrens 			return (-1);
3261789Sahrens 
3262789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3263789Sahrens 
3264789Sahrens 		/* make sure we're in the same pool */
3265789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3266789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3267789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
32682082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32692082Seschrock 			    "datasets must be within same pool"));
32702082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3271789Sahrens 		}
32722440Snd150628 
32732440Snd150628 		/* new name cannot be a child of the current dataset name */
32742440Snd150628 		if (strncmp(parent, zhp->zfs_name,
32753912Slling 		    strlen(zhp->zfs_name)) == 0) {
32762440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32772440Snd150628 			    "New dataset name cannot be a descendent of "
32782440Snd150628 			    "current dataset name"));
32792440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
32802440Snd150628 		}
3281789Sahrens 	}
3282789Sahrens 
32832082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
32842082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
32852082Seschrock 
3286789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3287789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
32882082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32892082Seschrock 		    "dataset is used in a non-global zone"));
32902082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3291789Sahrens 	}
3292789Sahrens 
32934007Smmusante 	if (recursive) {
32944007Smmusante 
32954183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
32964183Smmusante 		if (parentname == NULL) {
32974183Smmusante 			ret = -1;
32984183Smmusante 			goto error;
32994183Smmusante 		}
33004007Smmusante 		delim = strchr(parentname, '@');
33014007Smmusante 		*delim = '\0';
33025094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
33034007Smmusante 		if (zhrp == NULL) {
33044183Smmusante 			ret = -1;
33054183Smmusante 			goto error;
33064007Smmusante 		}
33074007Smmusante 
33084007Smmusante 	} else {
33097366STim.Haley@Sun.COM 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
33104007Smmusante 			return (-1);
33114007Smmusante 
33124007Smmusante 		if (changelist_haszonedchild(cl)) {
33134007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33144007Smmusante 			    "child dataset with inherited mountpoint is used "
33154007Smmusante 			    "in a non-global zone"));
33164007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
33174007Smmusante 			goto error;
33184007Smmusante 		}
33194007Smmusante 
33204007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
33214007Smmusante 			goto error;
3322789Sahrens 	}
3323789Sahrens 
33242676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3325789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3326789Sahrens 	else
3327789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3328789Sahrens 
33292665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33302676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
33312665Snd150628 
33324007Smmusante 	zc.zc_cookie = recursive;
33334007Smmusante 
33344543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
33354007Smmusante 		/*
33364007Smmusante 		 * if it was recursive, the one that actually failed will
33374007Smmusante 		 * be in zc.zc_name
33384007Smmusante 		 */
33394007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33405367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
33414007Smmusante 
33424007Smmusante 		if (recursive && errno == EEXIST) {
33434007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33444007Smmusante 			    "a child dataset already has a snapshot "
33454007Smmusante 			    "with the new name"));
33464801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
33474007Smmusante 		} else {
33484007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
33494007Smmusante 		}
3350789Sahrens 
3351789Sahrens 		/*
3352789Sahrens 		 * On failure, we still want to remount any filesystems that
3353789Sahrens 		 * were previously mounted, so we don't alter the system state.
3354789Sahrens 		 */
3355*10588SEric.Taylor@Sun.COM 		if (!recursive)
33564007Smmusante 			(void) changelist_postfix(cl);
3357789Sahrens 	} else {
3358*10588SEric.Taylor@Sun.COM 		if (!recursive) {
33594007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
33604007Smmusante 			ret = changelist_postfix(cl);
33614007Smmusante 		}
3362789Sahrens 	}
3363789Sahrens 
3364789Sahrens error:
33654007Smmusante 	if (parentname) {
33664007Smmusante 		free(parentname);
33674007Smmusante 	}
33684007Smmusante 	if (zhrp) {
33694007Smmusante 		zfs_close(zhrp);
33704007Smmusante 	}
33714007Smmusante 	if (cl) {
33724007Smmusante 		changelist_free(cl);
33734007Smmusante 	}
3374789Sahrens 	return (ret);
3375789Sahrens }
3376789Sahrens 
33772676Seschrock nvlist_t *
33782676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
33792676Seschrock {
33802676Seschrock 	return (zhp->zfs_user_props);
33812676Seschrock }
33822676Seschrock 
33832676Seschrock /*
33843912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
33853912Slling  * display, and their maximum widths.  This does two main things:
33863912Slling  *
33873912Slling  *      - If this is a list of all properties, then expand the list to include
33883912Slling  *        all native properties, and set a flag so that for each dataset we look
33893912Slling  *        for new unique user properties and add them to the list.
33903912Slling  *
33913912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
33923912Slling  *        so that we can size the column appropriately.
33933912Slling  */
33943912Slling int
33955094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
33963912Slling {
33973912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33985094Slling 	zprop_list_t *entry;
33995094Slling 	zprop_list_t **last, **start;
34003912Slling 	nvlist_t *userprops, *propval;
34013912Slling 	nvpair_t *elem;
34023912Slling 	char *strval;
34033912Slling 	char buf[ZFS_MAXPROPLEN];
34043912Slling 
34055094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
34063912Slling 		return (-1);
34072676Seschrock 
34082676Seschrock 	userprops = zfs_get_user_props(zhp);
34092676Seschrock 
34102676Seschrock 	entry = *plp;
34112676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
34122676Seschrock 		/*
34132676Seschrock 		 * Go through and add any user properties as necessary.  We
34142676Seschrock 		 * start by incrementing our list pointer to the first
34152676Seschrock 		 * non-native property.
34162676Seschrock 		 */
34172676Seschrock 		start = plp;
34182676Seschrock 		while (*start != NULL) {
34195094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
34202676Seschrock 				break;
34212676Seschrock 			start = &(*start)->pl_next;
34222676Seschrock 		}
34232676Seschrock 
34242676Seschrock 		elem = NULL;
34252676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
34262676Seschrock 			/*
34272676Seschrock 			 * See if we've already found this property in our list.
34282676Seschrock 			 */
34292676Seschrock 			for (last = start; *last != NULL;
34302676Seschrock 			    last = &(*last)->pl_next) {
34312676Seschrock 				if (strcmp((*last)->pl_user_prop,
34322676Seschrock 				    nvpair_name(elem)) == 0)
34332676Seschrock 					break;
34342676Seschrock 			}
34352676Seschrock 
34362676Seschrock 			if (*last == NULL) {
34372676Seschrock 				if ((entry = zfs_alloc(hdl,
34385094Slling 				    sizeof (zprop_list_t))) == NULL ||
34392676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
34402676Seschrock 				    nvpair_name(elem)))) == NULL) {
34412676Seschrock 					free(entry);
34422676Seschrock 					return (-1);
34432676Seschrock 				}
34442676Seschrock 
34455094Slling 				entry->pl_prop = ZPROP_INVAL;
34462676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
34472676Seschrock 				entry->pl_all = B_TRUE;
34482676Seschrock 				*last = entry;
34492676Seschrock 			}
34502676Seschrock 		}
34512676Seschrock 	}
34522676Seschrock 
34532676Seschrock 	/*
34542676Seschrock 	 * Now go through and check the width of any non-fixed columns
34552676Seschrock 	 */
34562676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
34572676Seschrock 		if (entry->pl_fixed)
34582676Seschrock 			continue;
34592676Seschrock 
34605094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
34612676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
34622676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
34632676Seschrock 				if (strlen(buf) > entry->pl_width)
34642676Seschrock 					entry->pl_width = strlen(buf);
34652676Seschrock 			}
34662676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
34672676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
34682676Seschrock 			verify(nvlist_lookup_string(propval,
34695094Slling 			    ZPROP_VALUE, &strval) == 0);
34702676Seschrock 			if (strlen(strval) > entry->pl_width)
34712676Seschrock 				entry->pl_width = strlen(strval);
34722676Seschrock 		}
34732676Seschrock 	}
34742676Seschrock 
34752676Seschrock 	return (0);
34762676Seschrock }
34774543Smarks 
34784543Smarks int
34794543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
34804543Smarks {
34814543Smarks 	zfs_cmd_t zc = { 0 };
34824543Smarks 	nvlist_t *nvp;
34834543Smarks 	gid_t gid;
34844543Smarks 	uid_t uid;
34854543Smarks 	const gid_t *groups;
34864543Smarks 	int group_cnt;
34874543Smarks 	int error;
34884543Smarks 
34894543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
34904543Smarks 		return (no_memory(hdl));
34914543Smarks 
34924543Smarks 	uid = ucred_geteuid(cred);
34934543Smarks 	gid = ucred_getegid(cred);
34944543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
34954543Smarks 
34964543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
34974543Smarks 		return (1);
34984543Smarks 
34994543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
35004543Smarks 		nvlist_free(nvp);
35014543Smarks 		return (1);
35024543Smarks 	}
35034543Smarks 
35044543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
35054543Smarks 		nvlist_free(nvp);
35064543Smarks 		return (1);
35074543Smarks 	}
35084543Smarks 
35094543Smarks 	if (nvlist_add_uint32_array(nvp,
35104543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
35114543Smarks 		nvlist_free(nvp);
35124543Smarks 		return (1);
35134543Smarks 	}
35144543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
35154543Smarks 
35165094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
35174543Smarks 		return (-1);
35184543Smarks 
35194543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
35204543Smarks 	nvlist_free(nvp);
35214543Smarks 	return (error);
35224543Smarks }
35234543Smarks 
35244543Smarks int
35254543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
35268845Samw@Sun.COM     char *resource, void *export, void *sharetab,
35278845Samw@Sun.COM     int sharemax, zfs_share_op_t operation)
35284543Smarks {
35294543Smarks 	zfs_cmd_t zc = { 0 };
35304543Smarks 	int error;
35314543Smarks 
35324543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
35334543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
35348845Samw@Sun.COM 	if (resource)
35358845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
35364543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
35374543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
35385331Samw 	zc.zc_share.z_sharetype = operation;
35394543Smarks 	zc.zc_share.z_sharemax = sharemax;
35404543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
35414543Smarks 	return (error);
35424543Smarks }
35438802SSanjeev.Bagewadi@Sun.COM 
35448802SSanjeev.Bagewadi@Sun.COM void
35458802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
35468802SSanjeev.Bagewadi@Sun.COM {
35478802SSanjeev.Bagewadi@Sun.COM 	nvpair_t *curr;
35488802SSanjeev.Bagewadi@Sun.COM 
35498802SSanjeev.Bagewadi@Sun.COM 	/*
35508802SSanjeev.Bagewadi@Sun.COM 	 * Keep a reference to the props-table against which we prune the
35518802SSanjeev.Bagewadi@Sun.COM 	 * properties.
35528802SSanjeev.Bagewadi@Sun.COM 	 */
35538802SSanjeev.Bagewadi@Sun.COM 	zhp->zfs_props_table = props;
35548802SSanjeev.Bagewadi@Sun.COM 
35558802SSanjeev.Bagewadi@Sun.COM 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
35568802SSanjeev.Bagewadi@Sun.COM 
35578802SSanjeev.Bagewadi@Sun.COM 	while (curr) {
35588802SSanjeev.Bagewadi@Sun.COM 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
35598802SSanjeev.Bagewadi@Sun.COM 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
35608802SSanjeev.Bagewadi@Sun.COM 
35619396SMatthew.Ahrens@Sun.COM 		/*
35629396SMatthew.Ahrens@Sun.COM 		 * We leave user:props in the nvlist, so there will be
35639396SMatthew.Ahrens@Sun.COM 		 * some ZPROP_INVAL.  To be extra safe, don't prune
35649396SMatthew.Ahrens@Sun.COM 		 * those.
35659396SMatthew.Ahrens@Sun.COM 		 */
35669396SMatthew.Ahrens@Sun.COM 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
35678802SSanjeev.Bagewadi@Sun.COM 			(void) nvlist_remove(zhp->zfs_props,
35688802SSanjeev.Bagewadi@Sun.COM 			    nvpair_name(curr), nvpair_type(curr));
35698802SSanjeev.Bagewadi@Sun.COM 		curr = next;
35708802SSanjeev.Bagewadi@Sun.COM 	}
35718802SSanjeev.Bagewadi@Sun.COM }
35728845Samw@Sun.COM 
35738845Samw@Sun.COM static int
35748845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
35758845Samw@Sun.COM     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
35768845Samw@Sun.COM {
35778845Samw@Sun.COM 	zfs_cmd_t zc = { 0 };
35788845Samw@Sun.COM 	nvlist_t *nvlist = NULL;
35798845Samw@Sun.COM 	int error;
35808845Samw@Sun.COM 
35818845Samw@Sun.COM 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
35828845Samw@Sun.COM 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
35838845Samw@Sun.COM 	zc.zc_cookie = (uint64_t)cmd;
35848845Samw@Sun.COM 
35858845Samw@Sun.COM 	if (cmd == ZFS_SMB_ACL_RENAME) {
35868845Samw@Sun.COM 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
35878845Samw@Sun.COM 			(void) no_memory(hdl);
35888845Samw@Sun.COM 			return (NULL);
35898845Samw@Sun.COM 		}
35908845Samw@Sun.COM 	}
35918845Samw@Sun.COM 
35928845Samw@Sun.COM 	switch (cmd) {
35938845Samw@Sun.COM 	case ZFS_SMB_ACL_ADD:
35948845Samw@Sun.COM 	case ZFS_SMB_ACL_REMOVE:
35958845Samw@Sun.COM 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
35968845Samw@Sun.COM 		break;
35978845Samw@Sun.COM 	case ZFS_SMB_ACL_RENAME:
35988845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
35998845Samw@Sun.COM 		    resource1) != 0) {
36008845Samw@Sun.COM 				(void) no_memory(hdl);
36018845Samw@Sun.COM 				return (-1);
36028845Samw@Sun.COM 		}
36038845Samw@Sun.COM 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
36048845Samw@Sun.COM 		    resource2) != 0) {
36058845Samw@Sun.COM 				(void) no_memory(hdl);
36068845Samw@Sun.COM 				return (-1);
36078845Samw@Sun.COM 		}
36088845Samw@Sun.COM 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
36098845Samw@Sun.COM 			nvlist_free(nvlist);
36108845Samw@Sun.COM 			return (-1);
36118845Samw@Sun.COM 		}
36128845Samw@Sun.COM 		break;
36138845Samw@Sun.COM 	case ZFS_SMB_ACL_PURGE:
36148845Samw@Sun.COM 		break;
36158845Samw@Sun.COM 	default:
36168845Samw@Sun.COM 		return (-1);
36178845Samw@Sun.COM 	}
36188845Samw@Sun.COM 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
36198845Samw@Sun.COM 	if (nvlist)
36208845Samw@Sun.COM 		nvlist_free(nvlist);
36218845Samw@Sun.COM 	return (error);
36228845Samw@Sun.COM }
36238845Samw@Sun.COM 
36248845Samw@Sun.COM int
36258845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
36268845Samw@Sun.COM     char *path, char *resource)
36278845Samw@Sun.COM {
36288845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
36298845Samw@Sun.COM 	    resource, NULL));
36308845Samw@Sun.COM }
36318845Samw@Sun.COM 
36328845Samw@Sun.COM int
36338845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
36348845Samw@Sun.COM     char *path, char *resource)
36358845Samw@Sun.COM {
36368845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
36378845Samw@Sun.COM 	    resource, NULL));
36388845Samw@Sun.COM }
36398845Samw@Sun.COM 
36408845Samw@Sun.COM int
36418845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
36428845Samw@Sun.COM {
36438845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
36448845Samw@Sun.COM 	    NULL, NULL));
36458845Samw@Sun.COM }
36468845Samw@Sun.COM 
36478845Samw@Sun.COM int
36488845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
36498845Samw@Sun.COM     char *oldname, char *newname)
36508845Samw@Sun.COM {
36518845Samw@Sun.COM 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
36528845Samw@Sun.COM 	    oldname, newname));
36538845Samw@Sun.COM }
36549396SMatthew.Ahrens@Sun.COM 
36559396SMatthew.Ahrens@Sun.COM int
36569396SMatthew.Ahrens@Sun.COM zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
36579396SMatthew.Ahrens@Sun.COM     zfs_userspace_cb_t func, void *arg)
36589396SMatthew.Ahrens@Sun.COM {
36599396SMatthew.Ahrens@Sun.COM 	zfs_cmd_t zc = { 0 };
36609396SMatthew.Ahrens@Sun.COM 	int error;
36619396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t buf[100];
36629396SMatthew.Ahrens@Sun.COM 
36639396SMatthew.Ahrens@Sun.COM 	(void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
36649396SMatthew.Ahrens@Sun.COM 
36659396SMatthew.Ahrens@Sun.COM 	zc.zc_objset_type = type;
36669396SMatthew.Ahrens@Sun.COM 	zc.zc_nvlist_dst = (uintptr_t)buf;
36679396SMatthew.Ahrens@Sun.COM 
36689396SMatthew.Ahrens@Sun.COM 	/* CONSTCOND */
36699396SMatthew.Ahrens@Sun.COM 	while (1) {
36709396SMatthew.Ahrens@Sun.COM 		zfs_useracct_t *zua = buf;
36719396SMatthew.Ahrens@Sun.COM 
36729396SMatthew.Ahrens@Sun.COM 		zc.zc_nvlist_dst_size = sizeof (buf);
36739396SMatthew.Ahrens@Sun.COM 		error = ioctl(zhp->zfs_hdl->libzfs_fd,
36749396SMatthew.Ahrens@Sun.COM 		    ZFS_IOC_USERSPACE_MANY, &zc);
36759396SMatthew.Ahrens@Sun.COM 		if (error || zc.zc_nvlist_dst_size == 0)
36769396SMatthew.Ahrens@Sun.COM 			break;
36779396SMatthew.Ahrens@Sun.COM 
36789396SMatthew.Ahrens@Sun.COM 		while (zc.zc_nvlist_dst_size > 0) {
36799554SMatthew.Ahrens@Sun.COM 			error = func(arg, zua->zu_domain, zua->zu_rid,
36809554SMatthew.Ahrens@Sun.COM 			    zua->zu_space);
36819554SMatthew.Ahrens@Sun.COM 			if (error != 0)
36829554SMatthew.Ahrens@Sun.COM 				return (error);
36839396SMatthew.Ahrens@Sun.COM 			zua++;
36849396SMatthew.Ahrens@Sun.COM 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
36859396SMatthew.Ahrens@Sun.COM 		}
36869396SMatthew.Ahrens@Sun.COM 	}
36879396SMatthew.Ahrens@Sun.COM 
36889396SMatthew.Ahrens@Sun.COM 	return (error);
36899396SMatthew.Ahrens@Sun.COM }
369010242Schris.kirby@sun.com 
369110242Schris.kirby@sun.com int
369210242Schris.kirby@sun.com zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
369310342Schris.kirby@sun.com     boolean_t recursive, boolean_t temphold)
369410242Schris.kirby@sun.com {
369510242Schris.kirby@sun.com 	zfs_cmd_t zc = { 0 };
369610242Schris.kirby@sun.com 	libzfs_handle_t *hdl = zhp->zfs_hdl;
369710242Schris.kirby@sun.com 
369810242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
369910242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
370010342Schris.kirby@sun.com 	if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
370110342Schris.kirby@sun.com 	    >= sizeof (zc.zc_string))
370210342Schris.kirby@sun.com 		return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
370310242Schris.kirby@sun.com 	zc.zc_cookie = recursive;
370410342Schris.kirby@sun.com 	zc.zc_temphold = temphold;
370510242Schris.kirby@sun.com 
370610242Schris.kirby@sun.com 	if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
370710242Schris.kirby@sun.com 		char errbuf[ZFS_MAXNAMELEN+32];
370810242Schris.kirby@sun.com 
370910242Schris.kirby@sun.com 		/*
371010242Schris.kirby@sun.com 		 * if it was recursive, the one that actually failed will be in
371110242Schris.kirby@sun.com 		 * zc.zc_name.
371210242Schris.kirby@sun.com 		 */
371310242Schris.kirby@sun.com 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
371410242Schris.kirby@sun.com 		    "cannot hold '%s@%s'"), zc.zc_name, snapname);
371510242Schris.kirby@sun.com 		switch (errno) {
371610242Schris.kirby@sun.com 		case ENOTSUP:
371710242Schris.kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
371810242Schris.kirby@sun.com 			    "pool must be upgraded"));
371910242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
372010242Schris.kirby@sun.com 		case EINVAL:
372110242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
372210242Schris.kirby@sun.com 		case EEXIST:
372310242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
372410242Schris.kirby@sun.com 		default:
372510242Schris.kirby@sun.com 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
372610242Schris.kirby@sun.com 		}
372710242Schris.kirby@sun.com 	}
372810242Schris.kirby@sun.com 
372910242Schris.kirby@sun.com 	return (0);
373010242Schris.kirby@sun.com }
373110242Schris.kirby@sun.com 
373210342Schris.kirby@sun.com struct hold_range_arg {
373310342Schris.kirby@sun.com 	zfs_handle_t	*origin;
373410342Schris.kirby@sun.com 	const char	*fromsnap;
373510342Schris.kirby@sun.com 	const char	*tosnap;
373610342Schris.kirby@sun.com 	char		lastsnapheld[ZFS_MAXNAMELEN];
373710342Schris.kirby@sun.com 	const char	*tag;
373810342Schris.kirby@sun.com 	boolean_t	temphold;
373910342Schris.kirby@sun.com 	boolean_t	seento;
374010342Schris.kirby@sun.com 	boolean_t	seenfrom;
374110342Schris.kirby@sun.com 	boolean_t	holding;
374210342Schris.kirby@sun.com };
374310342Schris.kirby@sun.com 
374410342Schris.kirby@sun.com static int
374510342Schris.kirby@sun.com zfs_hold_range_one(zfs_handle_t *zhp, void *arg)
374610342Schris.kirby@sun.com {
374710342Schris.kirby@sun.com 	struct hold_range_arg *hra = arg;
374810342Schris.kirby@sun.com 	const char *thissnap;
374910342Schris.kirby@sun.com 	int error;
375010342Schris.kirby@sun.com 
375110342Schris.kirby@sun.com 	thissnap = strchr(zfs_get_name(zhp), '@') + 1;
375210342Schris.kirby@sun.com 
375310342Schris.kirby@sun.com 	if (hra->fromsnap && !hra->seenfrom &&
375410342Schris.kirby@sun.com 	    strcmp(hra->fromsnap, thissnap) == 0)
375510342Schris.kirby@sun.com 		hra->seenfrom = B_TRUE;
375610342Schris.kirby@sun.com 
375710342Schris.kirby@sun.com 	/* snap is older or newer than the desired range, ignore it */
375810342Schris.kirby@sun.com 	if (hra->seento || !hra->seenfrom) {
375910342Schris.kirby@sun.com 		zfs_close(zhp);
376010342Schris.kirby@sun.com 		return (0);
376110342Schris.kirby@sun.com 	}
376210342Schris.kirby@sun.com 
376310342Schris.kirby@sun.com 	if (hra->holding) {
376410342Schris.kirby@sun.com 		error = zfs_hold(hra->origin, thissnap, hra->tag, B_FALSE,
376510342Schris.kirby@sun.com 		    hra->temphold);
376610342Schris.kirby@sun.com 		if (error == 0) {
376710342Schris.kirby@sun.com 			(void) strlcpy(hra->lastsnapheld, zfs_get_name(zhp),
376810342Schris.kirby@sun.com 			    sizeof (hra->lastsnapheld));
376910342Schris.kirby@sun.com 		}
377010342Schris.kirby@sun.com 	} else {
377110342Schris.kirby@sun.com 		error = zfs_release(hra->origin, thissnap, hra->tag, B_FALSE);
377210342Schris.kirby@sun.com 	}
377310342Schris.kirby@sun.com 
377410342Schris.kirby@sun.com 	if (!hra->seento && strcmp(hra->tosnap, thissnap) == 0)
377510342Schris.kirby@sun.com 		hra->seento = B_TRUE;
377610342Schris.kirby@sun.com 
377710342Schris.kirby@sun.com 	zfs_close(zhp);
377810342Schris.kirby@sun.com 	return (error);
377910342Schris.kirby@sun.com }
378010342Schris.kirby@sun.com 
378110342Schris.kirby@sun.com /*
378210342Schris.kirby@sun.com  * Add a user hold on the set of snapshots starting with fromsnap up to
378310342Schris.kirby@sun.com  * and including tosnap. If we're unable to to acquire a particular hold,
378410342Schris.kirby@sun.com  * undo any holds up to that point.
378510342Schris.kirby@sun.com  */
378610342Schris.kirby@sun.com int
378710342Schris.kirby@sun.com zfs_hold_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
378810342Schris.kirby@sun.com     const char *tag, boolean_t temphold)
378910342Schris.kirby@sun.com {
379010342Schris.kirby@sun.com 	struct hold_range_arg arg = { 0 };
379110342Schris.kirby@sun.com 	int error;
379210342Schris.kirby@sun.com 
379310342Schris.kirby@sun.com 	arg.origin = zhp;
379410342Schris.kirby@sun.com 	arg.fromsnap = fromsnap;
379510342Schris.kirby@sun.com 	arg.tosnap = tosnap;
379610342Schris.kirby@sun.com 	arg.tag = tag;
379710342Schris.kirby@sun.com 	arg.temphold = temphold;
379810342Schris.kirby@sun.com 	arg.holding = B_TRUE;
379910342Schris.kirby@sun.com 
380010342Schris.kirby@sun.com 	error = zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg);
380110342Schris.kirby@sun.com 
380210342Schris.kirby@sun.com 	/*
380310342Schris.kirby@sun.com 	 * Make sure we either hold the entire range or none.
380410342Schris.kirby@sun.com 	 */
380510342Schris.kirby@sun.com 	if (error && arg.lastsnapheld[0] != '\0') {
380610342Schris.kirby@sun.com 		(void) zfs_release_range(zhp, fromsnap,
380710342Schris.kirby@sun.com 		    (const char *)arg.lastsnapheld, tag);
380810342Schris.kirby@sun.com 	}
380910342Schris.kirby@sun.com 	return (error);
381010342Schris.kirby@sun.com }
381110342Schris.kirby@sun.com 
381210242Schris.kirby@sun.com int
381310242Schris.kirby@sun.com zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
381410242Schris.kirby@sun.com     boolean_t recursive)
381510242Schris.kirby@sun.com {
381610242Schris.kirby@sun.com 	zfs_cmd_t zc = { 0 };
381710242Schris.kirby@sun.com 	libzfs_handle_t *hdl = zhp->zfs_hdl;
381810242Schris.kirby@sun.com 
381910242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
382010242Schris.kirby@sun.com 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
382110342Schris.kirby@sun.com 	if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
382210342Schris.kirby@sun.com 	    >= sizeof (zc.zc_string))
382310342Schris.kirby@sun.com 		return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
382410242Schris.kirby@sun.com 	zc.zc_cookie = recursive;
382510242Schris.kirby@sun.com 
382610242Schris.kirby@sun.com 	if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
382710242Schris.kirby@sun.com 		char errbuf[ZFS_MAXNAMELEN+32];
382810242Schris.kirby@sun.com 
382910242Schris.kirby@sun.com 		/*
383010242Schris.kirby@sun.com 		 * if it was recursive, the one that actually failed will be in
383110242Schris.kirby@sun.com 		 * zc.zc_name.
383210242Schris.kirby@sun.com 		 */
383310242Schris.kirby@sun.com 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
383410242Schris.kirby@sun.com 		    "cannot release '%s@%s'"), zc.zc_name, snapname);
383510242Schris.kirby@sun.com 		switch (errno) {
383610242Schris.kirby@sun.com 		case ESRCH:
383710242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
383810242Schris.kirby@sun.com 		case ENOTSUP:
383910242Schris.kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
384010242Schris.kirby@sun.com 			    "pool must be upgraded"));
384110242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
384210242Schris.kirby@sun.com 		case EINVAL:
384310242Schris.kirby@sun.com 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
384410242Schris.kirby@sun.com 		default:
384510242Schris.kirby@sun.com 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
384610242Schris.kirby@sun.com 		}
384710242Schris.kirby@sun.com 	}
384810242Schris.kirby@sun.com 
384910242Schris.kirby@sun.com 	return (0);
385010242Schris.kirby@sun.com }
385110342Schris.kirby@sun.com 
385210342Schris.kirby@sun.com /*
385310342Schris.kirby@sun.com  * Release a user hold from the set of snapshots starting with fromsnap
385410342Schris.kirby@sun.com  * up to and including tosnap.
385510342Schris.kirby@sun.com  */
385610342Schris.kirby@sun.com int
385710342Schris.kirby@sun.com zfs_release_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
385810342Schris.kirby@sun.com     const char *tag)
385910342Schris.kirby@sun.com {
386010342Schris.kirby@sun.com 	struct hold_range_arg arg = { 0 };
386110342Schris.kirby@sun.com 
386210342Schris.kirby@sun.com 	arg.origin = zhp;
386310342Schris.kirby@sun.com 	arg.fromsnap = fromsnap;
386410342Schris.kirby@sun.com 	arg.tosnap = tosnap;
386510342Schris.kirby@sun.com 	arg.tag = tag;
386610342Schris.kirby@sun.com 
386710342Schris.kirby@sun.com 	return (zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg));
386810342Schris.kirby@sun.com }
3869