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 /*
235977Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <assert.h>
28789Sahrens #include <ctype.h>
29789Sahrens #include <errno.h>
30789Sahrens #include <libdevinfo.h>
31789Sahrens #include <libintl.h>
32789Sahrens #include <math.h>
33789Sahrens #include <stdio.h>
34789Sahrens #include <stdlib.h>
35789Sahrens #include <strings.h>
36789Sahrens #include <unistd.h>
375367Sahrens #include <stddef.h>
38789Sahrens #include <zone.h>
392082Seschrock #include <fcntl.h>
40789Sahrens #include <sys/mntent.h>
41789Sahrens #include <sys/mnttab.h>
421294Slling #include <sys/mount.h>
434543Smarks #include <sys/avl.h>
444543Smarks #include <priv.h>
454543Smarks #include <pwd.h>
464543Smarks #include <grp.h>
474543Smarks #include <stddef.h>
484543Smarks #include <ucred.h>
49789Sahrens 
50789Sahrens #include <sys/spa.h>
512676Seschrock #include <sys/zap.h>
52789Sahrens #include <libzfs.h>
53789Sahrens 
54789Sahrens #include "zfs_namecheck.h"
55789Sahrens #include "zfs_prop.h"
56789Sahrens #include "libzfs_impl.h"
574543Smarks #include "zfs_deleg.h"
58789Sahrens 
594007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
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 	/*
113789Sahrens 	 * The user has requested either filesystems or volumes.
114789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
115789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
116789Sahrens 	 */
117789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
118789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
119789Sahrens 
120789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
121789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
122789Sahrens }
123789Sahrens 
124789Sahrens /*
125789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
126789Sahrens  * provide a more meaningful error message.  We place a more useful message in
127789Sahrens  * 'buf' detailing exactly why the name was not valid.
128789Sahrens  */
129789Sahrens static int
1305326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1315326Sek110237     boolean_t modifying)
132789Sahrens {
133789Sahrens 	namecheck_err_t why;
134789Sahrens 	char what;
135789Sahrens 
136789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1372082Seschrock 		if (hdl != NULL) {
138789Sahrens 			switch (why) {
1391003Slling 			case NAME_ERR_TOOLONG:
1402082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1412082Seschrock 				    "name is too long"));
1421003Slling 				break;
1431003Slling 
144789Sahrens 			case NAME_ERR_LEADING_SLASH:
1452082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1462082Seschrock 				    "leading slash in name"));
147789Sahrens 				break;
148789Sahrens 
149789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1502082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512082Seschrock 				    "empty component in name"));
152789Sahrens 				break;
153789Sahrens 
154789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1552082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562082Seschrock 				    "trailing slash in name"));
157789Sahrens 				break;
158789Sahrens 
159789Sahrens 			case NAME_ERR_INVALCHAR:
1602082Seschrock 				zfs_error_aux(hdl,
161789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1622082Seschrock 				    "'%c' in name"), what);
163789Sahrens 				break;
164789Sahrens 
165789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1662082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1672082Seschrock 				    "multiple '@' delimiters in name"));
168789Sahrens 				break;
1692856Snd150628 
1702856Snd150628 			case NAME_ERR_NOLETTER:
1712856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1722856Snd150628 				    "pool doesn't begin with a letter"));
1732856Snd150628 				break;
1742856Snd150628 
1752856Snd150628 			case NAME_ERR_RESERVED:
1762856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1772856Snd150628 				    "name is reserved"));
1782856Snd150628 				break;
1792856Snd150628 
1802856Snd150628 			case NAME_ERR_DISKLIKE:
1812856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1822856Snd150628 				    "reserved disk name"));
1832856Snd150628 				break;
184789Sahrens 			}
185789Sahrens 		}
186789Sahrens 
187789Sahrens 		return (0);
188789Sahrens 	}
189789Sahrens 
190789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1912082Seschrock 		if (hdl != NULL)
1922082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1932082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
194789Sahrens 		return (0);
195789Sahrens 	}
196789Sahrens 
1972199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1982199Sahrens 		if (hdl != NULL)
1992199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2003413Smmusante 			    "missing '@' delimiter in snapshot name"));
2012199Sahrens 		return (0);
2022199Sahrens 	}
2032199Sahrens 
2045326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2055326Sek110237 		if (hdl != NULL)
2065326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2075326Sek110237 			    "invalid character %c in name"), '%');
2085326Sek110237 		return (0);
2095326Sek110237 	}
2105326Sek110237 
2112082Seschrock 	return (-1);
212789Sahrens }
213789Sahrens 
214789Sahrens int
215789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
216789Sahrens {
2176423Sgw25295 	if (type == ZFS_TYPE_POOL)
2186423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2195326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
220789Sahrens }
221789Sahrens 
222789Sahrens /*
2232676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2242676Seschrock  * properties into a separate nvlist.
2252676Seschrock  */
2264217Seschrock static nvlist_t *
2274217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2282676Seschrock {
2292676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2302676Seschrock 	nvpair_t *elem;
2312676Seschrock 	nvlist_t *propval;
2324217Seschrock 	nvlist_t *nvl;
2334217Seschrock 
2344217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2354217Seschrock 		(void) no_memory(hdl);
2364217Seschrock 		return (NULL);
2374217Seschrock 	}
2382676Seschrock 
2392676Seschrock 	elem = NULL;
2404217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2412676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2422676Seschrock 			continue;
2432676Seschrock 
2442676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2454217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2464217Seschrock 			nvlist_free(nvl);
2474217Seschrock 			(void) no_memory(hdl);
2484217Seschrock 			return (NULL);
2494217Seschrock 		}
2502676Seschrock 	}
2512676Seschrock 
2524217Seschrock 	return (nvl);
2532676Seschrock }
2542676Seschrock 
2556865Srm160521 static zpool_handle_t *
2566865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2576865Srm160521 {
2586865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2596865Srm160521 	zpool_handle_t *zph;
2606865Srm160521 
2616865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2626865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2636865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2646865Srm160521 		hdl->libzfs_pool_handles = zph;
2656865Srm160521 	}
2666865Srm160521 	return (zph);
2676865Srm160521 }
2686865Srm160521 
2696865Srm160521 static zpool_handle_t *
2706865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2716865Srm160521 {
2726865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2736865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2746865Srm160521 
2756865Srm160521 	while ((zph != NULL) &&
2766865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2776865Srm160521 		zph = zph->zpool_next;
2786865Srm160521 	return (zph);
2796865Srm160521 }
2806865Srm160521 
2816865Srm160521 /*
2826865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2836865Srm160521  * If a handle to that pool already exists then that handle is returned.
2846865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2856865Srm160521  */
2866865Srm160521 static zpool_handle_t *
2876865Srm160521 zpool_handle(zfs_handle_t *zhp)
2886865Srm160521 {
2896865Srm160521 	char *pool_name;
2906865Srm160521 	int len;
2916865Srm160521 	zpool_handle_t *zph;
2926865Srm160521 
2936865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2946865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2956865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2966865Srm160521 
2976865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
2986865Srm160521 	if (zph == NULL)
2996865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
3006865Srm160521 
3016865Srm160521 	free(pool_name);
3026865Srm160521 	return (zph);
3036865Srm160521 }
3046865Srm160521 
3056865Srm160521 void
3066865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3076865Srm160521 {
3086865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3096865Srm160521 
3106865Srm160521 	while (zph != NULL) {
3116865Srm160521 		next = zph->zpool_next;
3126865Srm160521 		zpool_close(zph);
3136865Srm160521 		zph = next;
3146865Srm160521 	}
3156865Srm160521 	hdl->libzfs_pool_handles = NULL;
3166865Srm160521 }
3176865Srm160521 
3182676Seschrock /*
319789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
320789Sahrens  */
321789Sahrens static int
322789Sahrens get_stats(zfs_handle_t *zhp)
323789Sahrens {
324789Sahrens 	zfs_cmd_t zc = { 0 };
3252676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3264217Seschrock 	nvlist_t *allprops, *userprops;
327789Sahrens 
328789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
329789Sahrens 
3302676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
3312082Seschrock 		return (-1);
3321356Seschrock 
3332082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
3341356Seschrock 		if (errno == ENOMEM) {
3352676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3362676Seschrock 				zcmd_free_nvlists(&zc);
3372082Seschrock 				return (-1);
3382676Seschrock 			}
3391356Seschrock 		} else {
3402676Seschrock 			zcmd_free_nvlists(&zc);
3411356Seschrock 			return (-1);
3421356Seschrock 		}
3431356Seschrock 	}
344789Sahrens 
3452885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
346789Sahrens 
3474217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
3482676Seschrock 		zcmd_free_nvlists(&zc);
3492082Seschrock 		return (-1);
3502082Seschrock 	}
351789Sahrens 
3522676Seschrock 	zcmd_free_nvlists(&zc);
3532676Seschrock 
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 
368789Sahrens /*
369789Sahrens  * Refresh the properties currently stored in the handle.
370789Sahrens  */
371789Sahrens void
372789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
373789Sahrens {
374789Sahrens 	(void) get_stats(zhp);
375789Sahrens }
376789Sahrens 
377789Sahrens /*
378789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
379789Sahrens  * zfs_iter_* to create child handles on the fly.
380789Sahrens  */
381789Sahrens zfs_handle_t *
3822082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
383789Sahrens {
3842082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3854543Smarks 	char *logstr;
3862082Seschrock 
3872082Seschrock 	if (zhp == NULL)
3882082Seschrock 		return (NULL);
3892082Seschrock 
3902082Seschrock 	zhp->zfs_hdl = hdl;
391789Sahrens 
3924543Smarks 	/*
3934543Smarks 	 * Preserve history log string.
3944543Smarks 	 * any changes performed here will be
3954543Smarks 	 * logged as an internal event.
3964543Smarks 	 */
3974543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
3984543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
3991758Sahrens top:
400789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
401789Sahrens 
402789Sahrens 	if (get_stats(zhp) != 0) {
4034543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
404789Sahrens 		free(zhp);
405789Sahrens 		return (NULL);
406789Sahrens 	}
407789Sahrens 
4081758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4091758Sahrens 		zfs_cmd_t zc = { 0 };
4101758Sahrens 
4111758Sahrens 		/*
4121758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4131758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4141758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4151758Sahrens 		 * can't be mounted.  However, it could also be that we
4161758Sahrens 		 * have crashed in the middle of one of those
4171758Sahrens 		 * operations, in which case we need to get rid of the
4181758Sahrens 		 * inconsistent state.  We do that by either rolling
4191758Sahrens 		 * back to the previous snapshot (which will fail if
4201758Sahrens 		 * there is none), or destroying the filesystem.  Note
4211758Sahrens 		 * that if we are still in the middle of an active
4221758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4231758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4241758Sahrens 		 */
4251758Sahrens 
4261758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4271758Sahrens 
4282885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4292082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4301758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
4311758Sahrens 		} else {
4321758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
4331758Sahrens 		}
4341758Sahrens 
4351758Sahrens 		/*
4365331Samw 		 * If we can successfully destroy it, pretend that it
4371758Sahrens 		 * never existed.
4381758Sahrens 		 */
4392082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
4404543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4411758Sahrens 			free(zhp);
4421758Sahrens 			errno = ENOENT;
4431758Sahrens 			return (NULL);
4441758Sahrens 		}
4455367Sahrens 		/* If we can successfully roll it back, reget the stats */
4465367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
4475367Sahrens 			goto top;
4481758Sahrens 	}
4491758Sahrens 
450789Sahrens 	/*
451789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
452789Sahrens 	 * the high-level type.
453789Sahrens 	 */
4542885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4552885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4562885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4572885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4582885Sahrens 	else
4592885Sahrens 		abort();
4602885Sahrens 
461789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
462789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
463789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
464789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
465789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
466789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
467789Sahrens 	else
4682082Seschrock 		abort();	/* we should never see any other types */
469789Sahrens 
4704543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
4716865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
472789Sahrens 	return (zhp);
473789Sahrens }
474789Sahrens 
475789Sahrens /*
476789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
477789Sahrens  * argument is a mask of acceptable types.  The function will print an
478789Sahrens  * appropriate error message and return NULL if it can't be opened.
479789Sahrens  */
480789Sahrens zfs_handle_t *
4812082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
482789Sahrens {
483789Sahrens 	zfs_handle_t *zhp;
4842082Seschrock 	char errbuf[1024];
4852082Seschrock 
4862082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4872082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
488789Sahrens 
489789Sahrens 	/*
4902082Seschrock 	 * Validate the name before we even try to open it.
491789Sahrens 	 */
4925326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4932082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4942082Seschrock 		    "invalid dataset name"));
4952082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
496789Sahrens 		return (NULL);
497789Sahrens 	}
498789Sahrens 
499789Sahrens 	/*
500789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
501789Sahrens 	 */
502789Sahrens 	errno = 0;
5032082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5043237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
505789Sahrens 		return (NULL);
506789Sahrens 	}
507789Sahrens 
508789Sahrens 	if (!(types & zhp->zfs_type)) {
5092082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5102142Seschrock 		zfs_close(zhp);
511789Sahrens 		return (NULL);
512789Sahrens 	}
513789Sahrens 
514789Sahrens 	return (zhp);
515789Sahrens }
516789Sahrens 
517789Sahrens /*
518789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
519789Sahrens  */
520789Sahrens void
521789Sahrens zfs_close(zfs_handle_t *zhp)
522789Sahrens {
523789Sahrens 	if (zhp->zfs_mntopts)
524789Sahrens 		free(zhp->zfs_mntopts);
5252676Seschrock 	nvlist_free(zhp->zfs_props);
5262676Seschrock 	nvlist_free(zhp->zfs_user_props);
527789Sahrens 	free(zhp);
528789Sahrens }
529789Sahrens 
5305713Srm160521 int
5315713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
5325713Srm160521 {
5336865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
5346865Srm160521 
5355713Srm160521 	if (zpool_handle == NULL)
5365713Srm160521 		return (-1);
5375713Srm160521 
5385713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
5395713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
5405713Srm160521 	return (0);
5415713Srm160521 }
5425713Srm160521 
5435713Srm160521 /*
5445713Srm160521  * The choice of reservation property depends on the SPA version.
5455713Srm160521  */
5465713Srm160521 static int
5475713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5485713Srm160521 {
5495713Srm160521 	int spa_version;
5505713Srm160521 
5515713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5525713Srm160521 		return (-1);
5535713Srm160521 
5545713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5555713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5565713Srm160521 	else
5575713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5585713Srm160521 
5595713Srm160521 	return (0);
5605713Srm160521 }
5615713Srm160521 
5623912Slling /*
5632676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
5642676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
5652676Seschrock  * strings.
566789Sahrens  */
5677184Stimh nvlist_t *
5687184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
5695094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
570789Sahrens {
5712676Seschrock 	nvpair_t *elem;
5722676Seschrock 	uint64_t intval;
5732676Seschrock 	char *strval;
5745094Slling 	zfs_prop_t prop;
5752676Seschrock 	nvlist_t *ret;
5765331Samw 	int chosen_normal = -1;
5775331Samw 	int chosen_utf = -1;
5782676Seschrock 
5795094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5805094Slling 		(void) no_memory(hdl);
5815094Slling 		return (NULL);
582789Sahrens 	}
583789Sahrens 
5842676Seschrock 	elem = NULL;
5852676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5865094Slling 		const char *propname = nvpair_name(elem);
5872676Seschrock 
5882676Seschrock 		/*
5892676Seschrock 		 * Make sure this property is valid and applies to this type.
5902676Seschrock 		 */
5915094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
5925094Slling 			if (!zfs_prop_user(propname)) {
5932676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5945094Slling 				    "invalid property '%s'"), propname);
5955094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5965094Slling 				goto error;
5975094Slling 			}
5985094Slling 
5995094Slling 			/*
6005094Slling 			 * If this is a user property, make sure it's a
6015094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
6025094Slling 			 */
6035094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
6045094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6055094Slling 				    "'%s' must be a string"), propname);
6065094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6075094Slling 				goto error;
6085094Slling 			}
6095094Slling 
6105094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
6115094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6125094Slling 				    "property name '%s' is too long"),
6132676Seschrock 				    propname);
6142676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6152676Seschrock 				goto error;
6162676Seschrock 			}
6172676Seschrock 
6182676Seschrock 			(void) nvpair_value_string(elem, &strval);
6192676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
6202676Seschrock 				(void) no_memory(hdl);
6212676Seschrock 				goto error;
6222676Seschrock 			}
6232676Seschrock 			continue;
624789Sahrens 		}
6252676Seschrock 
6267265Sahrens 		if (type == ZFS_TYPE_SNAPSHOT) {
6277265Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6287265Sahrens 			    "this property can not be modified for snapshots"));
6297265Sahrens 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
6307265Sahrens 			goto error;
6317265Sahrens 		}
6327265Sahrens 
6332676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
6342676Seschrock 			zfs_error_aux(hdl,
6352676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
6362676Seschrock 			    "apply to datasets of this type"), propname);
6372676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
6382676Seschrock 			goto error;
6392676Seschrock 		}
6402676Seschrock 
6412676Seschrock 		if (zfs_prop_readonly(prop) &&
6425331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
6432676Seschrock 			zfs_error_aux(hdl,
6442676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
6452676Seschrock 			    propname);
6462676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
6472676Seschrock 			goto error;
6482676Seschrock 		}
6492676Seschrock 
6505094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
6515094Slling 		    &strval, &intval, errbuf) != 0)
6522676Seschrock 			goto error;
6532676Seschrock 
6542676Seschrock 		/*
6552676Seschrock 		 * Perform some additional checks for specific properties.
6562676Seschrock 		 */
6572676Seschrock 		switch (prop) {
6584577Sahrens 		case ZFS_PROP_VERSION:
6594577Sahrens 		{
6604577Sahrens 			int version;
6614577Sahrens 
6624577Sahrens 			if (zhp == NULL)
6634577Sahrens 				break;
6644577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6654577Sahrens 			if (intval < version) {
6664577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6674577Sahrens 				    "Can not downgrade; already at version %u"),
6684577Sahrens 				    version);
6694577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6704577Sahrens 				goto error;
6714577Sahrens 			}
6724577Sahrens 			break;
6734577Sahrens 		}
6744577Sahrens 
6752676Seschrock 		case ZFS_PROP_RECORDSIZE:
6762676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
6772676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
6782676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
6792676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
6802082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6812676Seschrock 				    "'%s' must be power of 2 from %u "
6822676Seschrock 				    "to %uk"), propname,
6832676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
6842676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
6852676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6862676Seschrock 				goto error;
687789Sahrens 			}
688789Sahrens 			break;
689789Sahrens 
6903126Sahl 		case ZFS_PROP_SHAREISCSI:
6913126Sahl 			if (strcmp(strval, "off") != 0 &&
6923126Sahl 			    strcmp(strval, "on") != 0 &&
6933126Sahl 			    strcmp(strval, "type=disk") != 0) {
6943126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6953126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6963126Sahl 				    propname);
6973126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6983126Sahl 				goto error;
6993126Sahl 			}
7003126Sahl 
7013126Sahl 			break;
7023126Sahl 
7032676Seschrock 		case ZFS_PROP_MOUNTPOINT:
7044778Srm160521 		{
7054778Srm160521 			namecheck_err_t why;
7064778Srm160521 
7072676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
7082676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
7092676Seschrock 				break;
7102676Seschrock 
7114778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
7124778Srm160521 				switch (why) {
7134778Srm160521 				case NAME_ERR_LEADING_SLASH:
7144778Srm160521 					zfs_error_aux(hdl,
7154778Srm160521 					    dgettext(TEXT_DOMAIN,
7164778Srm160521 					    "'%s' must be an absolute path, "
7174778Srm160521 					    "'none', or 'legacy'"), propname);
7184778Srm160521 					break;
7194778Srm160521 				case NAME_ERR_TOOLONG:
7204778Srm160521 					zfs_error_aux(hdl,
7214778Srm160521 					    dgettext(TEXT_DOMAIN,
7224778Srm160521 					    "component of '%s' is too long"),
7234778Srm160521 					    propname);
7244778Srm160521 					break;
7254778Srm160521 				}
7262676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7272676Seschrock 				goto error;
728789Sahrens 			}
7294778Srm160521 		}
7304778Srm160521 
7313126Sahl 			/*FALLTHRU*/
7323126Sahl 
7335331Samw 		case ZFS_PROP_SHARESMB:
7343126Sahl 		case ZFS_PROP_SHARENFS:
7353126Sahl 			/*
7365331Samw 			 * For the mountpoint and sharenfs or sharesmb
7375331Samw 			 * properties, check if it can be set in a
7385331Samw 			 * global/non-global zone based on
7393126Sahl 			 * the zoned property value:
7403126Sahl 			 *
7413126Sahl 			 *		global zone	    non-global zone
7423126Sahl 			 * --------------------------------------------------
7433126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
7443126Sahl 			 *		sharenfs (no)	    sharenfs (no)
7455331Samw 			 *		sharesmb (no)	    sharesmb (no)
7463126Sahl 			 *
7473126Sahl 			 * zoned=off	mountpoint (yes)	N/A
7483126Sahl 			 *		sharenfs (yes)
7495331Samw 			 *		sharesmb (yes)
7503126Sahl 			 */
7512676Seschrock 			if (zoned) {
7522676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
7532676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7542676Seschrock 					    "'%s' cannot be set on "
7552676Seschrock 					    "dataset in a non-global zone"),
7562676Seschrock 					    propname);
7572676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7582676Seschrock 					    errbuf);
7592676Seschrock 					goto error;
7605331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
7615331Samw 				    prop == ZFS_PROP_SHARESMB) {
7622676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7632676Seschrock 					    "'%s' cannot be set in "
7642676Seschrock 					    "a non-global zone"), propname);
7652676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7662676Seschrock 					    errbuf);
7672676Seschrock 					goto error;
7682676Seschrock 				}
7692676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
7702676Seschrock 				/*
7712676Seschrock 				 * If zoned property is 'off', this must be in
7722676Seschrock 				 * a globle zone. If not, something is wrong.
7732676Seschrock 				 */
7742676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7752676Seschrock 				    "'%s' cannot be set while dataset "
7762676Seschrock 				    "'zoned' property is set"), propname);
7772676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
7782676Seschrock 				goto error;
7792676Seschrock 			}
7803126Sahl 
7814180Sdougm 			/*
7824180Sdougm 			 * At this point, it is legitimate to set the
7834180Sdougm 			 * property. Now we want to make sure that the
7844180Sdougm 			 * property value is valid if it is sharenfs.
7854180Sdougm 			 */
7865331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
7875331Samw 			    prop == ZFS_PROP_SHARESMB) &&
7884217Seschrock 			    strcmp(strval, "on") != 0 &&
7894217Seschrock 			    strcmp(strval, "off") != 0) {
7905331Samw 				zfs_share_proto_t proto;
7915331Samw 
7925331Samw 				if (prop == ZFS_PROP_SHARESMB)
7935331Samw 					proto = PROTO_SMB;
7945331Samw 				else
7955331Samw 					proto = PROTO_NFS;
7964180Sdougm 
7974180Sdougm 				/*
7985331Samw 				 * Must be an valid sharing protocol
7995331Samw 				 * option string so init the libshare
8005331Samw 				 * in order to enable the parser and
8015331Samw 				 * then parse the options. We use the
8025331Samw 				 * control API since we don't care about
8035331Samw 				 * the current configuration and don't
8044180Sdougm 				 * want the overhead of loading it
8054180Sdougm 				 * until we actually do something.
8064180Sdougm 				 */
8074180Sdougm 
8084217Seschrock 				if (zfs_init_libshare(hdl,
8094217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
8104217Seschrock 					/*
8114217Seschrock 					 * An error occurred so we can't do
8124217Seschrock 					 * anything
8134217Seschrock 					 */
8144217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8154217Seschrock 					    "'%s' cannot be set: problem "
8164217Seschrock 					    "in share initialization"),
8174217Seschrock 					    propname);
8184217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8194217Seschrock 					    errbuf);
8204217Seschrock 					goto error;
8214217Seschrock 				}
8224217Seschrock 
8235331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
8244217Seschrock 					/*
8254217Seschrock 					 * There was an error in parsing so
8264217Seschrock 					 * deal with it by issuing an error
8274217Seschrock 					 * message and leaving after
8284217Seschrock 					 * uninitializing the the libshare
8294217Seschrock 					 * interface.
8304217Seschrock 					 */
8314217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8324217Seschrock 					    "'%s' cannot be set to invalid "
8334217Seschrock 					    "options"), propname);
8344217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8354217Seschrock 					    errbuf);
8364217Seschrock 					zfs_uninit_libshare(hdl);
8374217Seschrock 					goto error;
8384217Seschrock 				}
8394180Sdougm 				zfs_uninit_libshare(hdl);
8404180Sdougm 			}
8414180Sdougm 
8423126Sahl 			break;
8435331Samw 		case ZFS_PROP_UTF8ONLY:
8445331Samw 			chosen_utf = (int)intval;
8455331Samw 			break;
8465331Samw 		case ZFS_PROP_NORMALIZE:
8475331Samw 			chosen_normal = (int)intval;
8485331Samw 			break;
8492676Seschrock 		}
8502676Seschrock 
8512676Seschrock 		/*
8522676Seschrock 		 * For changes to existing volumes, we have some additional
8532676Seschrock 		 * checks to enforce.
8542676Seschrock 		 */
8552676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
8562676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
8572676Seschrock 			    ZFS_PROP_VOLSIZE);
8582676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
8592676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
8602676Seschrock 			char buf[64];
8612676Seschrock 
8622676Seschrock 			switch (prop) {
8632676Seschrock 			case ZFS_PROP_RESERVATION:
8645378Sck153898 			case ZFS_PROP_REFRESERVATION:
8652676Seschrock 				if (intval > volsize) {
8662676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8672676Seschrock 					    "'%s' is greater than current "
8682676Seschrock 					    "volume size"), propname);
8692676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8702676Seschrock 					    errbuf);
8712676Seschrock 					goto error;
8722676Seschrock 				}
8732676Seschrock 				break;
8742676Seschrock 
8752676Seschrock 			case ZFS_PROP_VOLSIZE:
8762676Seschrock 				if (intval % blocksize != 0) {
8772676Seschrock 					zfs_nicenum(blocksize, buf,
8782676Seschrock 					    sizeof (buf));
8792676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8802676Seschrock 					    "'%s' must be a multiple of "
8812676Seschrock 					    "volume block size (%s)"),
8822676Seschrock 					    propname, buf);
8832676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8842676Seschrock 					    errbuf);
8852676Seschrock 					goto error;
8862676Seschrock 				}
8872676Seschrock 
8882676Seschrock 				if (intval == 0) {
8892676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8902676Seschrock 					    "'%s' cannot be zero"),
8912676Seschrock 					    propname);
8922676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8932676Seschrock 					    errbuf);
8942676Seschrock 					goto error;
895789Sahrens 				}
8963126Sahl 				break;
897789Sahrens 			}
898789Sahrens 		}
899789Sahrens 	}
900789Sahrens 
9012676Seschrock 	/*
9025331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
9035331Samw 	 * enforce rejection of non-UTF8 names.
9045331Samw 	 *
9055331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
9065331Samw 	 * was explicitly not chosen, it is an error.
9075331Samw 	 */
9085498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
9095331Samw 		if (nvlist_add_uint64(ret,
9105331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
9115331Samw 			(void) no_memory(hdl);
9125331Samw 			goto error;
9135331Samw 		}
9145498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
9155331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9165331Samw 		    "'%s' must be set 'on' if normalization chosen"),
9175331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
9185331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9195331Samw 		goto error;
9205331Samw 	}
9215331Samw 
9225331Samw 	/*
9232676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
9242676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
9252676Seschrock 	 */
9262676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
9272676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
9282676Seschrock 	    &intval) == 0) {
9292676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
9302676Seschrock 		    ZFS_PROP_VOLSIZE);
9315481Sck153898 		uint64_t old_reservation;
9322676Seschrock 		uint64_t new_reservation;
9335481Sck153898 		zfs_prop_t resv_prop;
9345713Srm160521 
9355713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
9365481Sck153898 			goto error;
9375481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
9382676Seschrock 
9392676Seschrock 		if (old_volsize == old_reservation &&
9405481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
9412676Seschrock 		    &new_reservation) != 0) {
9422676Seschrock 			if (nvlist_add_uint64(ret,
9435481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
9442676Seschrock 				(void) no_memory(hdl);
9452676Seschrock 				goto error;
9462676Seschrock 			}
9472676Seschrock 		}
9482676Seschrock 	}
9492676Seschrock 	return (ret);
9502676Seschrock 
9512676Seschrock error:
9522676Seschrock 	nvlist_free(ret);
9532676Seschrock 	return (NULL);
954789Sahrens }
955789Sahrens 
9564543Smarks static int
9574543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9584543Smarks     uint64_t *ret_who)
9594543Smarks {
9604543Smarks 	struct passwd *pwd;
9614543Smarks 	struct group *grp;
9624543Smarks 	uid_t id;
9634543Smarks 
9644543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9654543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9664543Smarks 		*ret_who = -1;
9674543Smarks 		return (0);
9684543Smarks 	}
9694543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9704543Smarks 		return (EZFS_BADWHO);
9714543Smarks 
9724543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9734543Smarks 	    strcmp(who, "everyone") == 0) {
9744543Smarks 		*ret_who = -1;
9754543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9764543Smarks 		return (0);
9774543Smarks 	}
9784543Smarks 
9794543Smarks 	pwd = getpwnam(who);
9804543Smarks 	grp = getgrnam(who);
9814543Smarks 
9824543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9834543Smarks 		*ret_who = pwd->pw_uid;
9844543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9854543Smarks 		*ret_who = grp->gr_gid;
9864543Smarks 	} else if (pwd) {
9874543Smarks 		*ret_who = pwd->pw_uid;
9884543Smarks 		*who_type = ZFS_DELEG_USER;
9894543Smarks 	} else if (grp) {
9904543Smarks 		*ret_who = grp->gr_gid;
9914543Smarks 		*who_type = ZFS_DELEG_GROUP;
9924543Smarks 	} else {
9934543Smarks 		char *end;
9944543Smarks 
9954543Smarks 		id = strtol(who, &end, 10);
9964543Smarks 		if (errno != 0 || *end != '\0') {
9974543Smarks 			return (EZFS_BADWHO);
9984543Smarks 		} else {
9994543Smarks 			*ret_who = id;
10004543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
10014543Smarks 				*who_type = ZFS_DELEG_USER;
10024543Smarks 		}
10034543Smarks 	}
10044543Smarks 
10054543Smarks 	return (0);
10064543Smarks }
10074543Smarks 
10084543Smarks static void
10094543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
10104543Smarks {
10114543Smarks 	if (perms_nvp != NULL) {
10124543Smarks 		verify(nvlist_add_nvlist(who_nvp,
10134543Smarks 		    name, perms_nvp) == 0);
10144543Smarks 	} else {
10154543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
10164543Smarks 	}
10174543Smarks }
10184543Smarks 
10194543Smarks static void
10204543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
10214543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
10224543Smarks     nvlist_t *sets_nvp)
10234543Smarks {
10244543Smarks 	boolean_t do_perms, do_sets;
10254543Smarks 	char name[ZFS_MAX_DELEG_NAME];
10264543Smarks 
10274543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
10284543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
10294543Smarks 
10304543Smarks 	if (!do_perms && !do_sets)
10314543Smarks 		do_perms = do_sets = B_TRUE;
10324543Smarks 
10334543Smarks 	if (do_perms) {
10344543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
10354543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10364543Smarks 		    whostr : (void *)&whoid);
10374543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
10384543Smarks 	}
10394543Smarks 	if (do_sets) {
10404543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
10414543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10424543Smarks 		    whostr : (void *)&whoid);
10434543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
10444543Smarks 	}
10454543Smarks }
10464543Smarks 
10474543Smarks static void
10484543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
10494543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
10504543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10514543Smarks {
10524543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10534543Smarks 		helper(who_type, whoid, whostr, 0,
10544543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10554543Smarks 	} else {
10564543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10574543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10584543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10594543Smarks 		}
10604543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10614543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10624543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10634543Smarks 		}
10644543Smarks 	}
10654543Smarks }
10664543Smarks 
10674543Smarks /*
10684543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10694543Smarks  *
10704543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10714543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10724543Smarks  * base attribute named stored in the dsl.
10734543Smarks  * Arguments:
10744543Smarks  *
10754543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10764543Smarks  *           whostr may be null for everyone or create perms.
10774543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10784543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10795331Samw  * perms:    common separated list of permissions.  May be null if user
10804543Smarks  *           is requested to remove permissions by who.
10814543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10824543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10834543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10844543Smarks  *           The output nvp will look something like this.
10854543Smarks  *              ul$1234 -> {create ; destroy }
10864543Smarks  *              Ul$1234 -> { @myset }
10874543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10884543Smarks  */
10894543Smarks int
10904543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10914543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10924543Smarks {
10934543Smarks 	nvlist_t *who_nvp;
10944543Smarks 	nvlist_t *perms_nvp = NULL;
10954543Smarks 	nvlist_t *sets_nvp = NULL;
10964543Smarks 	char errbuf[1024];
10974787Sahrens 	char *who_tok, *perm;
10984543Smarks 	int error;
10994543Smarks 
11004543Smarks 	*nvp = NULL;
11014543Smarks 
11024543Smarks 	if (perms) {
11034543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
11044543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11054543Smarks 			return (1);
11064543Smarks 		}
11074543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
11084543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11094543Smarks 			nvlist_free(perms_nvp);
11104543Smarks 			return (1);
11114543Smarks 		}
11124543Smarks 	}
11134543Smarks 
11144543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
11154543Smarks 		if (perms_nvp)
11164543Smarks 			nvlist_free(perms_nvp);
11174543Smarks 		if (sets_nvp)
11184543Smarks 			nvlist_free(sets_nvp);
11194543Smarks 		return (1);
11204543Smarks 	}
11214543Smarks 
11224543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
11234543Smarks 		namecheck_err_t why;
11244543Smarks 		char what;
11254543Smarks 
11264543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
11274787Sahrens 			nvlist_free(who_nvp);
11284787Sahrens 			if (perms_nvp)
11294787Sahrens 				nvlist_free(perms_nvp);
11304787Sahrens 			if (sets_nvp)
11314787Sahrens 				nvlist_free(sets_nvp);
11324787Sahrens 
11334543Smarks 			switch (why) {
11344543Smarks 			case NAME_ERR_NO_AT:
11354543Smarks 				zfs_error_aux(zhp->zfs_hdl,
11364543Smarks 				    dgettext(TEXT_DOMAIN,
11374543Smarks 				    "set definition must begin with an '@' "
11384543Smarks 				    "character"));
11394543Smarks 			}
11404543Smarks 			return (zfs_error(zhp->zfs_hdl,
11414543Smarks 			    EZFS_BADPERMSET, whostr));
11424543Smarks 		}
11434543Smarks 	}
11444543Smarks 
11454543Smarks 	/*
11464543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
11474543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
11484543Smarks 	 * other sets_nvp will have only permssion set names in it.
11494543Smarks 	 */
11504787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11514787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11524787Sahrens 
11534787Sahrens 		if (perm_canonical) {
11544787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11554787Sahrens 			    perm_canonical) == 0);
11564787Sahrens 		} else if (perm[0] == '@') {
11574787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11584787Sahrens 		} else {
11594787Sahrens 			nvlist_free(who_nvp);
11604787Sahrens 			nvlist_free(perms_nvp);
11614787Sahrens 			nvlist_free(sets_nvp);
11624787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11634543Smarks 		}
11644543Smarks 	}
11654543Smarks 
11664543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11674543Smarks 		who_tok = strtok(whostr, ",");
11684543Smarks 		if (who_tok == NULL) {
11694543Smarks 			nvlist_free(who_nvp);
11704787Sahrens 			if (perms_nvp)
11714787Sahrens 				nvlist_free(perms_nvp);
11724543Smarks 			if (sets_nvp)
11734543Smarks 				nvlist_free(sets_nvp);
11744543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11754543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11764543Smarks 			    whostr);
11774543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11784543Smarks 		}
11794543Smarks 	}
11804543Smarks 
11814543Smarks 	/*
11824543Smarks 	 * Now create the nvlist(s)
11834543Smarks 	 */
11844543Smarks 	do {
11854543Smarks 		uint64_t who_id;
11864543Smarks 
11874543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11884543Smarks 		    &who_id);
11894543Smarks 		if (error) {
11904543Smarks 			nvlist_free(who_nvp);
11914787Sahrens 			if (perms_nvp)
11924787Sahrens 				nvlist_free(perms_nvp);
11934543Smarks 			if (sets_nvp)
11944543Smarks 				nvlist_free(sets_nvp);
11954543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11964543Smarks 			    dgettext(TEXT_DOMAIN,
11974543Smarks 			    "Unable to determine uid/gid for "
11984543Smarks 			    "%s "), who_tok);
11994543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
12004543Smarks 		}
12014543Smarks 
12024543Smarks 		/*
12034543Smarks 		 * add entries for both local and descendent when required
12044543Smarks 		 */
12054543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
12064543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
12074543Smarks 
12084543Smarks 	} while (who_tok = strtok(NULL, ","));
12094543Smarks 	*nvp = who_nvp;
12104543Smarks 	return (0);
12114543Smarks }
12124543Smarks 
12134543Smarks static int
12144543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
12154543Smarks {
12164543Smarks 	zfs_cmd_t zc = { 0 };
12174543Smarks 	int error;
12184543Smarks 	char errbuf[1024];
12194543Smarks 
12204543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
12214543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
12224543Smarks 	    zhp->zfs_name);
12234543Smarks 
12245094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
12254543Smarks 		return (-1);
12264543Smarks 
12274543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12284543Smarks 	zc.zc_perm_action = unset;
12294543Smarks 
12304543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
12314543Smarks 	if (error && errno == ENOTSUP) {
12324543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
12334543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
12344543Smarks 		zcmd_free_nvlists(&zc);
12354543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
12364543Smarks 	} else if (error) {
12374543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
12384543Smarks 	}
12394543Smarks 	zcmd_free_nvlists(&zc);
12404543Smarks 
12414543Smarks 	return (error);
12424543Smarks }
12434543Smarks 
12444543Smarks int
12454543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
12464543Smarks {
12474543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12484543Smarks }
12494543Smarks 
12504543Smarks int
12514543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12524543Smarks {
12534543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12544543Smarks }
12554543Smarks 
12564543Smarks static int
12574543Smarks perm_compare(const void *arg1, const void *arg2)
12584543Smarks {
12594543Smarks 	const zfs_perm_node_t *node1 = arg1;
12604543Smarks 	const zfs_perm_node_t *node2 = arg2;
12614543Smarks 	int ret;
12624543Smarks 
12634543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12644543Smarks 
12654543Smarks 	if (ret > 0)
12664543Smarks 		return (1);
12674543Smarks 	if (ret < 0)
12684543Smarks 		return (-1);
12694543Smarks 	else
12704543Smarks 		return (0);
12714543Smarks }
12724543Smarks 
12734543Smarks static void
12744543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12754543Smarks {
12764543Smarks 	zfs_perm_node_t *permnode;
12775367Sahrens 	void *cookie = NULL;
12785367Sahrens 
12795367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12804543Smarks 		free(permnode);
12815367Sahrens 	avl_destroy(tree);
12824543Smarks }
12834543Smarks 
12844543Smarks static void
12854543Smarks zfs_destroy_tree(avl_tree_t *tree)
12864543Smarks {
12874543Smarks 	zfs_allow_node_t *allownode;
12885367Sahrens 	void *cookie = NULL;
12895367Sahrens 
12904543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12914543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12924543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12934543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12944543Smarks 		free(allownode);
12954543Smarks 	}
12965367Sahrens 	avl_destroy(tree);
12974543Smarks }
12984543Smarks 
12994543Smarks void
13004543Smarks zfs_free_allows(zfs_allow_t *allow)
13014543Smarks {
13024543Smarks 	zfs_allow_t *allownext;
13034543Smarks 	zfs_allow_t *freeallow;
13044543Smarks 
13054543Smarks 	allownext = allow;
13064543Smarks 	while (allownext) {
13074543Smarks 		zfs_destroy_tree(&allownext->z_sets);
13084543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
13094543Smarks 		zfs_destroy_tree(&allownext->z_user);
13104543Smarks 		zfs_destroy_tree(&allownext->z_group);
13114543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
13124543Smarks 		freeallow = allownext;
13134543Smarks 		allownext = allownext->z_next;
13144543Smarks 		free(freeallow);
13154543Smarks 	}
13164543Smarks }
13174543Smarks 
13184543Smarks static zfs_allow_t *
13194543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
13204543Smarks {
13214543Smarks 	zfs_allow_t *ptree;
13224543Smarks 
13234543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
13244543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
13254543Smarks 		return (NULL);
13264543Smarks 	}
13274543Smarks 
13284543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
13294543Smarks 	avl_create(&ptree->z_sets,
13304543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13314543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13324543Smarks 	avl_create(&ptree->z_crperms,
13334543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13344543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13354543Smarks 	avl_create(&ptree->z_user,
13364543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13374543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13384543Smarks 	avl_create(&ptree->z_group,
13394543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13404543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13414543Smarks 	avl_create(&ptree->z_everyone,
13424543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13434543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13444543Smarks 
13454543Smarks 	if (prev)
13464543Smarks 		prev->z_next = ptree;
13474543Smarks 	ptree->z_next = NULL;
13484543Smarks 	return (ptree);
13494543Smarks }
13504543Smarks 
13514543Smarks /*
13524543Smarks  * Add permissions to the appropriate AVL permission tree.
13534543Smarks  * The appropriate tree may not be the requested tree.
13544543Smarks  * For example if ld indicates a local permission, but
13554543Smarks  * same permission also exists as a descendent permission
13564543Smarks  * then the permission will be removed from the descendent
13574543Smarks  * tree and add the the local+descendent tree.
13584543Smarks  */
13594543Smarks static int
13604543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13614543Smarks     char *perm, char ld)
13624543Smarks {
13634543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13644543Smarks 	zfs_perm_node_t *newnode;
13654543Smarks 	avl_index_t where, where2;
13664543Smarks 	avl_tree_t *tree, *altree;
13674543Smarks 
13684543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13694543Smarks 
13704543Smarks 	if (ld == ZFS_DELEG_NA) {
13714543Smarks 		tree =  &allownode->z_localdescend;
13724543Smarks 		altree = &allownode->z_descend;
13734543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13744543Smarks 		tree = &allownode->z_local;
13754543Smarks 		altree = &allownode->z_descend;
13764543Smarks 	} else {
13774543Smarks 		tree = &allownode->z_descend;
13784543Smarks 		altree = &allownode->z_local;
13794543Smarks 	}
13804543Smarks 	permnode = avl_find(tree, &pnode, &where);
13814543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13824543Smarks 
13834543Smarks 	if (permnode2) {
13844543Smarks 		avl_remove(altree, permnode2);
13854543Smarks 		free(permnode2);
13864543Smarks 		if (permnode == NULL) {
13874543Smarks 			tree =  &allownode->z_localdescend;
13884543Smarks 		}
13894543Smarks 	}
13904543Smarks 
13914543Smarks 	/*
13924543Smarks 	 * Now insert new permission in either requested location
13934543Smarks 	 * local/descendent or into ld when perm will exist in both.
13944543Smarks 	 */
13954543Smarks 	if (permnode == NULL) {
13964543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13974543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
13984543Smarks 			return (-1);
13994543Smarks 		}
14004543Smarks 		*newnode = pnode;
14014543Smarks 		avl_add(tree, newnode);
14024543Smarks 	}
14034543Smarks 	return (0);
14044543Smarks }
14054577Sahrens 
14064543Smarks /*
14074543Smarks  * Uggh, this is going to be a bit complicated.
14084543Smarks  * we have an nvlist coming out of the kernel that
14094543Smarks  * will indicate where the permission is set and then
14104543Smarks  * it will contain allow of the various "who's", and what
14114543Smarks  * their permissions are.  To further complicate this
14124543Smarks  * we will then have to coalesce the local,descendent
14134543Smarks  * and local+descendent permissions where appropriate.
14144543Smarks  * The kernel only knows about a permission as being local
14154543Smarks  * or descendent, but not both.
14164543Smarks  *
14174543Smarks  * In order to make this easier for zfs_main to deal with
14184543Smarks  * a series of AVL trees will be used to maintain
14194543Smarks  * all of this, primarily for sorting purposes as well
14204543Smarks  * as the ability to quickly locate a specific entry.
14214543Smarks  *
14224543Smarks  * What we end up with are tree's for sets, create perms,
14234543Smarks  * user, groups and everyone.  With each of those trees
14244543Smarks  * we have subtrees for local, descendent and local+descendent
14254543Smarks  * permissions.
14264543Smarks  */
14274543Smarks int
14284543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
14294543Smarks {
14304543Smarks 	zfs_cmd_t zc = { 0 };
14314543Smarks 	int error;
14324543Smarks 	nvlist_t *nvlist;
14334543Smarks 	nvlist_t *permnv, *sourcenv;
14344543Smarks 	nvpair_t *who_pair, *source_pair;
14354543Smarks 	nvpair_t *perm_pair;
14364543Smarks 	char errbuf[1024];
14374543Smarks 	zfs_allow_t *zallowp, *newallowp;
14384543Smarks 	char  ld;
14394543Smarks 	char *nvpname;
14404543Smarks 	uid_t	uid;
14414543Smarks 	gid_t	gid;
14424543Smarks 	avl_tree_t *tree;
14434543Smarks 	avl_index_t where;
14444543Smarks 
14454543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14464543Smarks 
14474543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
14484543Smarks 		return (-1);
14494543Smarks 
14504543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14514543Smarks 		if (errno == ENOMEM) {
14524543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14534543Smarks 				zcmd_free_nvlists(&zc);
14544543Smarks 				return (-1);
14554543Smarks 			}
14564543Smarks 		} else if (errno == ENOTSUP) {
14574543Smarks 			zcmd_free_nvlists(&zc);
14584543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14594543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14604543Smarks 			return (zfs_error(zhp->zfs_hdl,
14614543Smarks 			    EZFS_BADVERSION, errbuf));
14624543Smarks 		} else {
14634543Smarks 			zcmd_free_nvlists(&zc);
14644543Smarks 			return (-1);
14654543Smarks 		}
14664543Smarks 	}
14674543Smarks 
14684543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14694543Smarks 		zcmd_free_nvlists(&zc);
14704543Smarks 		return (-1);
14714543Smarks 	}
14724543Smarks 
14734543Smarks 	zcmd_free_nvlists(&zc);
14744543Smarks 
14754543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14764543Smarks 
14774543Smarks 	if (source_pair == NULL) {
14784543Smarks 		*zfs_perms = NULL;
14794543Smarks 		return (0);
14804543Smarks 	}
14814543Smarks 
14824543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14834543Smarks 	if (*zfs_perms == NULL) {
14844543Smarks 		return (0);
14854543Smarks 	}
14864543Smarks 
14874543Smarks 	zallowp = *zfs_perms;
14884543Smarks 
14894543Smarks 	for (;;) {
14904543Smarks 		struct passwd *pwd;
14914543Smarks 		struct group *grp;
14924543Smarks 		zfs_allow_node_t *allownode;
14934543Smarks 		zfs_allow_node_t  findallownode;
14944543Smarks 		zfs_allow_node_t *newallownode;
14954543Smarks 
14964543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14974543Smarks 		    nvpair_name(source_pair),
14984543Smarks 		    sizeof (zallowp->z_setpoint));
14994543Smarks 
15004543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
15014543Smarks 			goto abort;
15024543Smarks 
15034543Smarks 		/*
15044543Smarks 		 * Make sure nvlist is composed correctly
15054543Smarks 		 */
15064543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
15074543Smarks 			goto abort;
15084543Smarks 		}
15094543Smarks 
15104543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
15114543Smarks 		if (who_pair == NULL) {
15124543Smarks 			goto abort;
15134543Smarks 		}
15144543Smarks 
15154543Smarks 		do {
15164543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
15174543Smarks 			if (error) {
15184543Smarks 				goto abort;
15194543Smarks 			}
15204543Smarks 
15214543Smarks 			/*
15224543Smarks 			 * First build up the key to use
15234543Smarks 			 * for looking up in the various
15244543Smarks 			 * who trees.
15254543Smarks 			 */
15264543Smarks 			ld = nvpair_name(who_pair)[1];
15274543Smarks 			nvpname = nvpair_name(who_pair);
15284543Smarks 			switch (nvpair_name(who_pair)[0]) {
15294543Smarks 			case ZFS_DELEG_USER:
15304543Smarks 			case ZFS_DELEG_USER_SETS:
15314543Smarks 				tree = &zallowp->z_user;
15324543Smarks 				uid = atol(&nvpname[3]);
15334543Smarks 				pwd = getpwuid(uid);
15344543Smarks 				(void) snprintf(findallownode.z_key,
15354543Smarks 				    sizeof (findallownode.z_key), "user %s",
15364543Smarks 				    (pwd) ? pwd->pw_name :
15374543Smarks 				    &nvpair_name(who_pair)[3]);
15384543Smarks 				break;
15394543Smarks 			case ZFS_DELEG_GROUP:
15404543Smarks 			case ZFS_DELEG_GROUP_SETS:
15414543Smarks 				tree = &zallowp->z_group;
15424543Smarks 				gid = atol(&nvpname[3]);
15434543Smarks 				grp = getgrgid(gid);
15444543Smarks 				(void) snprintf(findallownode.z_key,
15454543Smarks 				    sizeof (findallownode.z_key), "group %s",
15464543Smarks 				    (grp) ? grp->gr_name :
15474543Smarks 				    &nvpair_name(who_pair)[3]);
15484543Smarks 				break;
15494543Smarks 			case ZFS_DELEG_CREATE:
15504543Smarks 			case ZFS_DELEG_CREATE_SETS:
15514543Smarks 				tree = &zallowp->z_crperms;
15524543Smarks 				(void) strlcpy(findallownode.z_key, "",
15534543Smarks 				    sizeof (findallownode.z_key));
15544543Smarks 				break;
15554543Smarks 			case ZFS_DELEG_EVERYONE:
15564543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15574543Smarks 				(void) snprintf(findallownode.z_key,
15584543Smarks 				    sizeof (findallownode.z_key), "everyone");
15594543Smarks 				tree = &zallowp->z_everyone;
15604543Smarks 				break;
15614543Smarks 			case ZFS_DELEG_NAMED_SET:
15624543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15634543Smarks 				(void) snprintf(findallownode.z_key,
15644543Smarks 				    sizeof (findallownode.z_key), "%s",
15654543Smarks 				    &nvpair_name(who_pair)[3]);
15664543Smarks 				tree = &zallowp->z_sets;
15674543Smarks 				break;
15684543Smarks 			}
15694543Smarks 
15704543Smarks 			/*
15714543Smarks 			 * Place who in tree
15724543Smarks 			 */
15734543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15744543Smarks 			if (allownode == NULL) {
15754543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15764543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15774543Smarks 					goto abort;
15784543Smarks 				}
15794543Smarks 				avl_create(&newallownode->z_localdescend,
15804543Smarks 				    perm_compare,
15814543Smarks 				    sizeof (zfs_perm_node_t),
15824543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15834543Smarks 				avl_create(&newallownode->z_local,
15844543Smarks 				    perm_compare,
15854543Smarks 				    sizeof (zfs_perm_node_t),
15864543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15874543Smarks 				avl_create(&newallownode->z_descend,
15884543Smarks 				    perm_compare,
15894543Smarks 				    sizeof (zfs_perm_node_t),
15904543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15914543Smarks 				(void) strlcpy(newallownode->z_key,
15924543Smarks 				    findallownode.z_key,
15934543Smarks 				    sizeof (findallownode.z_key));
15944543Smarks 				avl_insert(tree, newallownode, where);
15954543Smarks 				allownode = newallownode;
15964543Smarks 			}
15974543Smarks 
15984543Smarks 			/*
15994543Smarks 			 * Now iterate over the permissions and
16004543Smarks 			 * place them in the appropriate local,
16014543Smarks 			 * descendent or local+descendent tree.
16024543Smarks 			 *
16034543Smarks 			 * The permissions are added to the tree
16044543Smarks 			 * via zfs_coalesce_perm().
16054543Smarks 			 */
16064543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
16074543Smarks 			if (perm_pair == NULL)
16084543Smarks 				goto abort;
16094543Smarks 			do {
16104543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
16114543Smarks 				    nvpair_name(perm_pair), ld) != 0)
16124543Smarks 					goto abort;
16134543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
16144543Smarks 			    perm_pair));
16154543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
16164543Smarks 
16174543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
16184543Smarks 		if (source_pair == NULL)
16194543Smarks 			break;
16204543Smarks 
16214543Smarks 		/*
16224543Smarks 		 * allocate another node from the link list of
16234543Smarks 		 * zfs_allow_t structures
16244543Smarks 		 */
16254543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
16264543Smarks 		    nvpair_name(source_pair));
16274543Smarks 		if (newallowp == NULL) {
16284543Smarks 			goto abort;
16294543Smarks 		}
16304543Smarks 		zallowp = newallowp;
16314543Smarks 	}
16324543Smarks 	nvlist_free(nvlist);
16334543Smarks 	return (0);
16344543Smarks abort:
16354543Smarks 	zfs_free_allows(*zfs_perms);
16364543Smarks 	nvlist_free(nvlist);
16374543Smarks 	return (-1);
16384543Smarks }
16394543Smarks 
16405993Smarks static char *
16415993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
16425993Smarks {
16435993Smarks 	/*
16445993Smarks 	 * Don't put newlines on end of lines
16455993Smarks 	 */
16465993Smarks 	switch (note) {
16475993Smarks 	case ZFS_DELEG_NOTE_CREATE:
16485993Smarks 		return (dgettext(TEXT_DOMAIN,
16495993Smarks 		    "Must also have the 'mount' ability"));
16505993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16515993Smarks 		return (dgettext(TEXT_DOMAIN,
16525993Smarks 		    "Must also have the 'mount' ability"));
16535993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16545993Smarks 		return (dgettext(TEXT_DOMAIN,
16555993Smarks 		    "Must also have the 'mount' ability"));
16565993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16575993Smarks 		return (dgettext(TEXT_DOMAIN,
16585993Smarks 		    "Must also have the 'mount' ability"));
16595993Smarks 	case ZFS_DELEG_NOTE_CLONE:
16605993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16615993Smarks 		    "ability and 'mount'\n"
16625993Smarks 		    "\t\t\t\tability in the origin file system"));
16635993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16645993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16655993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16665993Smarks 	case ZFS_DELEG_NOTE_RENAME:
16675993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16685993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16695993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16705993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16715993Smarks 		    " and 'create' ability"));
16725993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16735993Smarks 		return (dgettext(TEXT_DOMAIN,
16745993Smarks 		    "Allows changing any user property"));
16755993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16765993Smarks 		return (dgettext(TEXT_DOMAIN,
16775993Smarks 		    "Must also have the permission that is being\n"
16785993Smarks 		    "\t\t\t\tallowed"));
16795993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16805993Smarks 		return (dgettext(TEXT_DOMAIN,
16815993Smarks 		    "Allows mount/umount of ZFS datasets"));
16825993Smarks 	case ZFS_DELEG_NOTE_SHARE:
16835993Smarks 		return (dgettext(TEXT_DOMAIN,
16845993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16855993Smarks 		    "\t\t\t\tprotocols"));
16865993Smarks 	case ZFS_DELEG_NOTE_NONE:
16875993Smarks 	default:
16885993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16895993Smarks 	}
16905993Smarks }
16915993Smarks 
16925993Smarks typedef enum {
16935993Smarks 	ZFS_DELEG_SUBCOMMAND,
16945993Smarks 	ZFS_DELEG_PROP,
16955993Smarks 	ZFS_DELEG_OTHER
16965993Smarks } zfs_deleg_perm_type_t;
16975993Smarks 
16985993Smarks /*
16995993Smarks  * is the permission a subcommand or other?
17005993Smarks  */
17015993Smarks zfs_deleg_perm_type_t
17025993Smarks zfs_deleg_perm_type(const char *perm)
17035993Smarks {
17045993Smarks 	if (strcmp(perm, "userprop") == 0)
17055993Smarks 		return (ZFS_DELEG_OTHER);
17065993Smarks 	else
17075993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
17085993Smarks }
17095993Smarks 
17105993Smarks static char *
17115993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
17125993Smarks {
17135993Smarks 	switch (type) {
17145993Smarks 	case ZFS_DELEG_SUBCOMMAND:
17155993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
17165993Smarks 	case ZFS_DELEG_PROP:
17175993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
17185993Smarks 	case ZFS_DELEG_OTHER:
17195993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
17205993Smarks 	}
17215993Smarks 	return ("");
17225993Smarks }
17235993Smarks 
17245993Smarks /*ARGSUSED*/
17255993Smarks static int
17265993Smarks zfs_deleg_prop_cb(int prop, void *cb)
17275993Smarks {
17285993Smarks 	if (zfs_prop_delegatable(prop))
17295993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
17305993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
17315993Smarks 
17325993Smarks 	return (ZPROP_CONT);
17335993Smarks }
17345993Smarks 
17355993Smarks void
17365993Smarks zfs_deleg_permissions(void)
17375993Smarks {
17385993Smarks 	int i;
17395993Smarks 
17405993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
17415993Smarks 	    "TYPE", "NOTES");
17425993Smarks 
17435993Smarks 	/*
17445993Smarks 	 * First print out the subcommands
17455993Smarks 	 */
17465993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
17475993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17485993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17495993Smarks 		    zfs_deleg_perm_type_str(
17505993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17515993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17525993Smarks 	}
17535993Smarks 
17545993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17555993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17565993Smarks }
17575993Smarks 
1758789Sahrens /*
1759789Sahrens  * Given a property name and value, set the property for the given dataset.
1760789Sahrens  */
1761789Sahrens int
17622676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1763789Sahrens {
1764789Sahrens 	zfs_cmd_t zc = { 0 };
17652676Seschrock 	int ret = -1;
17662676Seschrock 	prop_changelist_t *cl = NULL;
17672082Seschrock 	char errbuf[1024];
17682082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17692676Seschrock 	nvlist_t *nvl = NULL, *realprops;
17702676Seschrock 	zfs_prop_t prop;
1771*7509SMark.Musante@Sun.COM 	boolean_t do_prefix;
1772*7509SMark.Musante@Sun.COM 	uint64_t idx;
17732082Seschrock 
17742082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
17752676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
17762082Seschrock 	    zhp->zfs_name);
17772082Seschrock 
17782676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
17792676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
17802676Seschrock 		(void) no_memory(hdl);
17812676Seschrock 		goto error;
1782789Sahrens 	}
1783789Sahrens 
17847184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
17852676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
17862676Seschrock 		goto error;
17875094Slling 
17882676Seschrock 	nvlist_free(nvl);
17892676Seschrock 	nvl = realprops;
17902676Seschrock 
17912676Seschrock 	prop = zfs_name_to_prop(propname);
17922676Seschrock 
17937366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
17942676Seschrock 		goto error;
1795789Sahrens 
1796789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17972082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17982082Seschrock 		    "child dataset with inherited mountpoint is used "
17992082Seschrock 		    "in a non-global zone"));
18002082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1801789Sahrens 		goto error;
1802789Sahrens 	}
1803789Sahrens 
1804*7509SMark.Musante@Sun.COM 	/*
1805*7509SMark.Musante@Sun.COM 	 * If the dataset's canmount property is being set to noauto,
1806*7509SMark.Musante@Sun.COM 	 * then we want to prevent unmounting & remounting it.
1807*7509SMark.Musante@Sun.COM 	 */
1808*7509SMark.Musante@Sun.COM 	do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1809*7509SMark.Musante@Sun.COM 	    (zprop_string_to_index(prop, propval, &idx,
1810*7509SMark.Musante@Sun.COM 	    ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
18116168Shs24103 
18126168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1813*7509SMark.Musante@Sun.COM 		goto error;
1814789Sahrens 
1815789Sahrens 	/*
1816789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1817789Sahrens 	 */
1818789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1819789Sahrens 
18205094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
18212676Seschrock 		goto error;
18222676Seschrock 
18234543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1824789Sahrens 	if (ret != 0) {
1825789Sahrens 		switch (errno) {
1826789Sahrens 
1827789Sahrens 		case ENOSPC:
1828789Sahrens 			/*
1829789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1830789Sahrens 			 * something different; setting a quota or reservation
1831789Sahrens 			 * doesn't use any disk space.
1832789Sahrens 			 */
1833789Sahrens 			switch (prop) {
1834789Sahrens 			case ZFS_PROP_QUOTA:
18355378Sck153898 			case ZFS_PROP_REFQUOTA:
18362082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18372082Seschrock 				    "size is less than current used or "
18382082Seschrock 				    "reserved space"));
18392082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1840789Sahrens 				break;
1841789Sahrens 
1842789Sahrens 			case ZFS_PROP_RESERVATION:
18435378Sck153898 			case ZFS_PROP_REFRESERVATION:
18442082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18452082Seschrock 				    "size is greater than available space"));
18462082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1847789Sahrens 				break;
1848789Sahrens 
1849789Sahrens 			default:
18502082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1851789Sahrens 				break;
1852789Sahrens 			}
1853789Sahrens 			break;
1854789Sahrens 
1855789Sahrens 		case EBUSY:
18562082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
18572082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
18582082Seschrock 			else
18592676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1860789Sahrens 			break;
1861789Sahrens 
18621175Slling 		case EROFS:
18632082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18641175Slling 			break;
18651175Slling 
18663886Sahl 		case ENOTSUP:
18673886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18685977Smarks 			    "pool and or dataset must be upgraded to set this "
18694603Sahrens 			    "property or value"));
18703886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
18713886Sahl 			break;
18723886Sahl 
18737042Sgw25295 		case ERANGE:
18747042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
18757042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18767042Sgw25295 				    "property setting is not allowed on "
18777042Sgw25295 				    "bootable datasets"));
18787042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
18797042Sgw25295 			} else {
18807042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
18817042Sgw25295 			}
18827042Sgw25295 			break;
18837042Sgw25295 
1884789Sahrens 		case EOVERFLOW:
1885789Sahrens 			/*
1886789Sahrens 			 * This platform can't address a volume this big.
1887789Sahrens 			 */
1888789Sahrens #ifdef _ILP32
1889789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
18902082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1891789Sahrens 				break;
1892789Sahrens 			}
1893789Sahrens #endif
18942082Seschrock 			/* FALLTHROUGH */
1895789Sahrens 		default:
18962082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1897789Sahrens 		}
1898789Sahrens 	} else {
18996168Shs24103 		if (do_prefix)
19006168Shs24103 			ret = changelist_postfix(cl);
19016168Shs24103 
1902789Sahrens 		/*
1903789Sahrens 		 * Refresh the statistics so the new property value
1904789Sahrens 		 * is reflected.
1905789Sahrens 		 */
19066168Shs24103 		if (ret == 0)
19072676Seschrock 			(void) get_stats(zhp);
1908789Sahrens 	}
1909789Sahrens 
1910789Sahrens error:
19112676Seschrock 	nvlist_free(nvl);
19122676Seschrock 	zcmd_free_nvlists(&zc);
19132676Seschrock 	if (cl)
19142676Seschrock 		changelist_free(cl);
1915789Sahrens 	return (ret);
1916789Sahrens }
1917789Sahrens 
1918789Sahrens /*
1919789Sahrens  * Given a property, inherit the value from the parent dataset.
1920789Sahrens  */
1921789Sahrens int
19222676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1923789Sahrens {
1924789Sahrens 	zfs_cmd_t zc = { 0 };
1925789Sahrens 	int ret;
1926789Sahrens 	prop_changelist_t *cl;
19272082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
19282082Seschrock 	char errbuf[1024];
19292676Seschrock 	zfs_prop_t prop;
19302082Seschrock 
19312082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
19322082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1933789Sahrens 
19345094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
19352676Seschrock 		/*
19362676Seschrock 		 * For user properties, the amount of work we have to do is very
19372676Seschrock 		 * small, so just do it here.
19382676Seschrock 		 */
19392676Seschrock 		if (!zfs_prop_user(propname)) {
19402676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19412676Seschrock 			    "invalid property"));
19422676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
19432676Seschrock 		}
19442676Seschrock 
19452676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19462676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
19472676Seschrock 
19484849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
19492676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
19502676Seschrock 
19512676Seschrock 		return (0);
19522676Seschrock 	}
19532676Seschrock 
1954789Sahrens 	/*
1955789Sahrens 	 * Verify that this property is inheritable.
1956789Sahrens 	 */
19572082Seschrock 	if (zfs_prop_readonly(prop))
19582082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
19592082Seschrock 
19602082Seschrock 	if (!zfs_prop_inheritable(prop))
19612082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1962789Sahrens 
1963789Sahrens 	/*
1964789Sahrens 	 * Check to see if the value applies to this type
1965789Sahrens 	 */
19662082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
19672082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1968789Sahrens 
19693443Srm160521 	/*
19703443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
19713443Srm160521 	 */
19723443Srm160521 	propname = zfs_prop_to_name(prop);
1973789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19742676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1975789Sahrens 
1976789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1977789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
19782082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19792082Seschrock 		    "dataset is used in a non-global zone"));
19802082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1981789Sahrens 	}
1982789Sahrens 
1983789Sahrens 	/*
1984789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1985789Sahrens 	 */
19867366STim.Haley@Sun.COM 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1987789Sahrens 		return (-1);
1988789Sahrens 
1989789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19902082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19912082Seschrock 		    "child dataset with inherited mountpoint is used "
19922082Seschrock 		    "in a non-global zone"));
19932082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1994789Sahrens 		goto error;
1995789Sahrens 	}
1996789Sahrens 
1997789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1998789Sahrens 		goto error;
1999789Sahrens 
20004849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
20012082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2002789Sahrens 	} else {
2003789Sahrens 
20042169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
2005789Sahrens 			goto error;
2006789Sahrens 
2007789Sahrens 		/*
2008789Sahrens 		 * Refresh the statistics so the new property is reflected.
2009789Sahrens 		 */
2010789Sahrens 		(void) get_stats(zhp);
2011789Sahrens 	}
2012789Sahrens 
2013789Sahrens error:
2014789Sahrens 	changelist_free(cl);
2015789Sahrens 	return (ret);
2016789Sahrens }
2017789Sahrens 
2018789Sahrens /*
20191356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
20201356Seschrock  * extract them appropriately.
20211356Seschrock  */
20221356Seschrock static uint64_t
20231356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20241356Seschrock {
20251356Seschrock 	nvlist_t *nv;
20261356Seschrock 	uint64_t value;
20271356Seschrock 
20282885Sahrens 	*source = NULL;
20291356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20301356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20315094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
20325094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20331356Seschrock 	} else {
20341356Seschrock 		value = zfs_prop_default_numeric(prop);
20351356Seschrock 		*source = "";
20361356Seschrock 	}
20371356Seschrock 
20381356Seschrock 	return (value);
20391356Seschrock }
20401356Seschrock 
20411356Seschrock static char *
20421356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20431356Seschrock {
20441356Seschrock 	nvlist_t *nv;
20451356Seschrock 	char *value;
20461356Seschrock 
20472885Sahrens 	*source = NULL;
20481356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20491356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20505094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
20515094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20521356Seschrock 	} else {
20531356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
20541356Seschrock 			value = "";
20551356Seschrock 		*source = "";
20561356Seschrock 	}
20571356Seschrock 
20581356Seschrock 	return (value);
20591356Seschrock }
20601356Seschrock 
20611356Seschrock /*
2062789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2063789Sahrens  * zfs_prop_get_int() are built using this interface.
2064789Sahrens  *
2065789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2066789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2067789Sahrens  * If they differ from the on-disk values, report the current values and mark
2068789Sahrens  * the source "temporary".
2069789Sahrens  */
20702082Seschrock static int
20715094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
20722082Seschrock     char **source, uint64_t *val)
2073789Sahrens {
20745147Srm160521 	zfs_cmd_t zc = { 0 };
20755592Stimh 	nvlist_t *zplprops = NULL;
2076789Sahrens 	struct mnttab mnt;
20773265Sahrens 	char *mntopt_on = NULL;
20783265Sahrens 	char *mntopt_off = NULL;
2079789Sahrens 
2080789Sahrens 	*source = NULL;
2081789Sahrens 
20823265Sahrens 	switch (prop) {
20833265Sahrens 	case ZFS_PROP_ATIME:
20843265Sahrens 		mntopt_on = MNTOPT_ATIME;
20853265Sahrens 		mntopt_off = MNTOPT_NOATIME;
20863265Sahrens 		break;
20873265Sahrens 
20883265Sahrens 	case ZFS_PROP_DEVICES:
20893265Sahrens 		mntopt_on = MNTOPT_DEVICES;
20903265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
20913265Sahrens 		break;
20923265Sahrens 
20933265Sahrens 	case ZFS_PROP_EXEC:
20943265Sahrens 		mntopt_on = MNTOPT_EXEC;
20953265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
20963265Sahrens 		break;
20973265Sahrens 
20983265Sahrens 	case ZFS_PROP_READONLY:
20993265Sahrens 		mntopt_on = MNTOPT_RO;
21003265Sahrens 		mntopt_off = MNTOPT_RW;
21013265Sahrens 		break;
21023265Sahrens 
21033265Sahrens 	case ZFS_PROP_SETUID:
21043265Sahrens 		mntopt_on = MNTOPT_SETUID;
21053265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
21063265Sahrens 		break;
21073265Sahrens 
21083265Sahrens 	case ZFS_PROP_XATTR:
21093265Sahrens 		mntopt_on = MNTOPT_XATTR;
21103265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
21113265Sahrens 		break;
21125331Samw 
21135331Samw 	case ZFS_PROP_NBMAND:
21145331Samw 		mntopt_on = MNTOPT_NBMAND;
21155331Samw 		mntopt_off = MNTOPT_NONBMAND;
21165331Samw 		break;
21173265Sahrens 	}
21183265Sahrens 
21192474Seschrock 	/*
21202474Seschrock 	 * Because looking up the mount options is potentially expensive
21212474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
21222474Seschrock 	 * we're looking up a property which requires its presence.
21232474Seschrock 	 */
21242474Seschrock 	if (!zhp->zfs_mntcheck &&
21253265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
21263265Sahrens 		struct mnttab entry, search = { 0 };
21273265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
21282474Seschrock 
21292474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
21302474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
21313265Sahrens 		rewind(mnttab);
21323265Sahrens 
21333265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
21343265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
21353265Sahrens 			    entry.mnt_mntopts);
21363265Sahrens 			if (zhp->zfs_mntopts == NULL)
21373265Sahrens 				return (-1);
21383265Sahrens 		}
21392474Seschrock 
21402474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
21412474Seschrock 	}
21422474Seschrock 
2143789Sahrens 	if (zhp->zfs_mntopts == NULL)
2144789Sahrens 		mnt.mnt_mntopts = "";
2145789Sahrens 	else
2146789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2147789Sahrens 
2148789Sahrens 	switch (prop) {
2149789Sahrens 	case ZFS_PROP_ATIME:
21503265Sahrens 	case ZFS_PROP_DEVICES:
21513265Sahrens 	case ZFS_PROP_EXEC:
21523265Sahrens 	case ZFS_PROP_READONLY:
21533265Sahrens 	case ZFS_PROP_SETUID:
21543265Sahrens 	case ZFS_PROP_XATTR:
21555331Samw 	case ZFS_PROP_NBMAND:
21562082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21572082Seschrock 
21583265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
21592082Seschrock 			*val = B_TRUE;
2160789Sahrens 			if (src)
21615094Slling 				*src = ZPROP_SRC_TEMPORARY;
21623265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
21632082Seschrock 			*val = B_FALSE;
2164789Sahrens 			if (src)
21655094Slling 				*src = ZPROP_SRC_TEMPORARY;
2166789Sahrens 		}
21672082Seschrock 		break;
2168789Sahrens 
21693265Sahrens 	case ZFS_PROP_CANMOUNT:
21702082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21716168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
21723417Srm160521 			*source = zhp->zfs_name;
21733417Srm160521 		else
21743417Srm160521 			*source = "";	/* default */
21752082Seschrock 		break;
2176789Sahrens 
2177789Sahrens 	case ZFS_PROP_QUOTA:
21785378Sck153898 	case ZFS_PROP_REFQUOTA:
2179789Sahrens 	case ZFS_PROP_RESERVATION:
21805378Sck153898 	case ZFS_PROP_REFRESERVATION:
21812885Sahrens 		*val = getprop_uint64(zhp, prop, source);
21822885Sahrens 		if (*val == 0)
2183789Sahrens 			*source = "";	/* default */
2184789Sahrens 		else
2185789Sahrens 			*source = zhp->zfs_name;
21862082Seschrock 		break;
2187789Sahrens 
2188789Sahrens 	case ZFS_PROP_MOUNTED:
21892082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
21902082Seschrock 		break;
2191789Sahrens 
21923377Seschrock 	case ZFS_PROP_NUMCLONES:
21933377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
21943377Seschrock 		break;
21953377Seschrock 
21965147Srm160521 	case ZFS_PROP_VERSION:
21975498Stimh 	case ZFS_PROP_NORMALIZE:
21985498Stimh 	case ZFS_PROP_UTF8ONLY:
21995498Stimh 	case ZFS_PROP_CASE:
22005498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
22015498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22025473Srm160521 			return (-1);
22035147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
22045498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
22055498Stimh 			zcmd_free_nvlists(&zc);
22065147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22075498Stimh 			    "unable to get %s property"),
22085498Stimh 			    zfs_prop_to_name(prop));
22095147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
22105147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
22115147Srm160521 		}
22125498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
22135498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
22145498Stimh 		    val) != 0) {
22155498Stimh 			zcmd_free_nvlists(&zc);
22165498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22175498Stimh 			    "unable to get %s property"),
22185498Stimh 			    zfs_prop_to_name(prop));
22195498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
22205498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
22215498Stimh 		}
22225592Stimh 		if (zplprops)
22235592Stimh 			nvlist_free(zplprops);
22245498Stimh 		zcmd_free_nvlists(&zc);
22255147Srm160521 		break;
22265147Srm160521 
2227789Sahrens 	default:
22284577Sahrens 		switch (zfs_prop_get_type(prop)) {
22294787Sahrens 		case PROP_TYPE_NUMBER:
22304787Sahrens 		case PROP_TYPE_INDEX:
22314577Sahrens 			*val = getprop_uint64(zhp, prop, source);
22327390SMatthew.Ahrens@Sun.COM 			/*
22337390SMatthew.Ahrens@Sun.COM 			 * If we tried to use a defalut value for a
22347390SMatthew.Ahrens@Sun.COM 			 * readonly property, it means that it was not
22357390SMatthew.Ahrens@Sun.COM 			 * present; return an error.
22367390SMatthew.Ahrens@Sun.COM 			 */
22377390SMatthew.Ahrens@Sun.COM 			if (zfs_prop_readonly(prop) &&
22387390SMatthew.Ahrens@Sun.COM 			    *source && (*source)[0] == '\0') {
22397390SMatthew.Ahrens@Sun.COM 				return (-1);
22407390SMatthew.Ahrens@Sun.COM 			}
22414577Sahrens 			break;
22424577Sahrens 
22434787Sahrens 		case PROP_TYPE_STRING:
22444577Sahrens 		default:
22454577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22464577Sahrens 			    "cannot get non-numeric property"));
22474577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
22484577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
22494577Sahrens 		}
2250789Sahrens 	}
2251789Sahrens 
2252789Sahrens 	return (0);
2253789Sahrens }
2254789Sahrens 
2255789Sahrens /*
2256789Sahrens  * Calculate the source type, given the raw source string.
2257789Sahrens  */
2258789Sahrens static void
22595094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2260789Sahrens     char *statbuf, size_t statlen)
2261789Sahrens {
22625094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2263789Sahrens 		return;
2264789Sahrens 
2265789Sahrens 	if (source == NULL) {
22665094Slling 		*srctype = ZPROP_SRC_NONE;
2267789Sahrens 	} else if (source[0] == '\0') {
22685094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2269789Sahrens 	} else {
2270789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
22715094Slling 			*srctype = ZPROP_SRC_LOCAL;
2272789Sahrens 		} else {
2273789Sahrens 			(void) strlcpy(statbuf, source, statlen);
22745094Slling 			*srctype = ZPROP_SRC_INHERITED;
2275789Sahrens 		}
2276789Sahrens 	}
2277789Sahrens 
2278789Sahrens }
2279789Sahrens 
2280789Sahrens /*
2281789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2282789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2283789Sahrens  * human-readable form.
2284789Sahrens  *
2285789Sahrens  * Returns 0 on success, or -1 on error.
2286789Sahrens  */
2287789Sahrens int
2288789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
22895094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2290789Sahrens {
2291789Sahrens 	char *source = NULL;
2292789Sahrens 	uint64_t val;
2293789Sahrens 	char *str;
22942676Seschrock 	const char *strval;
2295789Sahrens 
2296789Sahrens 	/*
2297789Sahrens 	 * Check to see if this property applies to our object
2298789Sahrens 	 */
2299789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2300789Sahrens 		return (-1);
2301789Sahrens 
2302789Sahrens 	if (src)
23035094Slling 		*src = ZPROP_SRC_NONE;
2304789Sahrens 
2305789Sahrens 	switch (prop) {
2306789Sahrens 	case ZFS_PROP_CREATION:
2307789Sahrens 		/*
2308789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2309789Sahrens 		 * this into a string unless 'literal' is specified.
2310789Sahrens 		 */
2311789Sahrens 		{
23122885Sahrens 			val = getprop_uint64(zhp, prop, &source);
23132885Sahrens 			time_t time = (time_t)val;
2314789Sahrens 			struct tm t;
2315789Sahrens 
2316789Sahrens 			if (literal ||
2317789Sahrens 			    localtime_r(&time, &t) == NULL ||
2318789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2319789Sahrens 			    &t) == 0)
23202885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2321789Sahrens 		}
2322789Sahrens 		break;
2323789Sahrens 
2324789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2325789Sahrens 		/*
2326789Sahrens 		 * Getting the precise mountpoint can be tricky.
2327789Sahrens 		 *
2328789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2329789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2330789Sahrens 		 *    after our ancestor and append it to the inherited value.
2331789Sahrens 		 *
2332789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2333789Sahrens 		 * root to any values we return.
2334789Sahrens 		 */
23356865Srm160521 
23361356Seschrock 		str = getprop_string(zhp, prop, &source);
23371356Seschrock 
23386612Sgw25295 		if (str[0] == '/') {
23396865Srm160521 			char buf[MAXPATHLEN];
23406865Srm160521 			char *root = buf;
23411356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2342789Sahrens 
2343789Sahrens 			if (relpath[0] == '/')
2344789Sahrens 				relpath++;
23456612Sgw25295 
23466865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
23476865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
23486865Srm160521 			    (strcmp(root, "-") == 0))
23496865Srm160521 				root[0] = '\0';
23506612Sgw25295 			/*
23516612Sgw25295 			 * Special case an alternate root of '/'. This will
23526612Sgw25295 			 * avoid having multiple leading slashes in the
23536612Sgw25295 			 * mountpoint path.
23546612Sgw25295 			 */
23556612Sgw25295 			if (strcmp(root, "/") == 0)
23566612Sgw25295 				root++;
23576612Sgw25295 
23586612Sgw25295 			/*
23596612Sgw25295 			 * If the mountpoint is '/' then skip over this
23606612Sgw25295 			 * if we are obtaining either an alternate root or
23616612Sgw25295 			 * an inherited mountpoint.
23626612Sgw25295 			 */
23636612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
23646612Sgw25295 			    relpath[0] != '\0'))
23651356Seschrock 				str++;
2366789Sahrens 
2367789Sahrens 			if (relpath[0] == '\0')
2368789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
23691356Seschrock 				    root, str);
2370789Sahrens 			else
2371789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
23721356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2373789Sahrens 				    relpath);
2374789Sahrens 		} else {
2375789Sahrens 			/* 'legacy' or 'none' */
23761356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2377789Sahrens 		}
2378789Sahrens 
2379789Sahrens 		break;
2380789Sahrens 
2381789Sahrens 	case ZFS_PROP_ORIGIN:
23822885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2383789Sahrens 		    proplen);
2384789Sahrens 		/*
2385789Sahrens 		 * If there is no parent at all, return failure to indicate that
2386789Sahrens 		 * it doesn't apply to this dataset.
2387789Sahrens 		 */
2388789Sahrens 		if (propbuf[0] == '\0')
2389789Sahrens 			return (-1);
2390789Sahrens 		break;
2391789Sahrens 
2392789Sahrens 	case ZFS_PROP_QUOTA:
23935378Sck153898 	case ZFS_PROP_REFQUOTA:
2394789Sahrens 	case ZFS_PROP_RESERVATION:
23955378Sck153898 	case ZFS_PROP_REFRESERVATION:
23965378Sck153898 
23972082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23982082Seschrock 			return (-1);
2399789Sahrens 
2400789Sahrens 		/*
2401789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2402789Sahrens 		 * (unless literal is set), and indicate that it's the default
2403789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2404789Sahrens 		 * that its set locally.
2405789Sahrens 		 */
2406789Sahrens 		if (val == 0) {
2407789Sahrens 			if (literal)
2408789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2409789Sahrens 			else
2410789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2411789Sahrens 		} else {
2412789Sahrens 			if (literal)
24132856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24143912Slling 				    (u_longlong_t)val);
2415789Sahrens 			else
2416789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2417789Sahrens 		}
2418789Sahrens 		break;
2419789Sahrens 
2420789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
24212082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24222082Seschrock 			return (-1);
24232856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
24242856Snd150628 		    val / 100, (longlong_t)val % 100);
2425789Sahrens 		break;
2426789Sahrens 
2427789Sahrens 	case ZFS_PROP_TYPE:
2428789Sahrens 		switch (zhp->zfs_type) {
2429789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2430789Sahrens 			str = "filesystem";
2431789Sahrens 			break;
2432789Sahrens 		case ZFS_TYPE_VOLUME:
2433789Sahrens 			str = "volume";
2434789Sahrens 			break;
2435789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2436789Sahrens 			str = "snapshot";
2437789Sahrens 			break;
2438789Sahrens 		default:
24392082Seschrock 			abort();
2440789Sahrens 		}
2441789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2442789Sahrens 		break;
2443789Sahrens 
2444789Sahrens 	case ZFS_PROP_MOUNTED:
2445789Sahrens 		/*
2446789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2447789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2448789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2449789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2450789Sahrens 		 */
24512082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
24522082Seschrock 		    src, &source, &val) != 0)
24532082Seschrock 			return (-1);
24542082Seschrock 		if (val)
2455789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2456789Sahrens 		else
2457789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2458789Sahrens 		break;
2459789Sahrens 
2460789Sahrens 	case ZFS_PROP_NAME:
2461789Sahrens 		/*
2462789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2463789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2464789Sahrens 		 * consumers.
2465789Sahrens 		 */
2466789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2467789Sahrens 		break;
2468789Sahrens 
2469789Sahrens 	default:
24704577Sahrens 		switch (zfs_prop_get_type(prop)) {
24714787Sahrens 		case PROP_TYPE_NUMBER:
24724577Sahrens 			if (get_numeric_property(zhp, prop, src,
24734577Sahrens 			    &source, &val) != 0)
24744577Sahrens 				return (-1);
24754577Sahrens 			if (literal)
24764577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
24774577Sahrens 				    (u_longlong_t)val);
24784577Sahrens 			else
24794577Sahrens 				zfs_nicenum(val, propbuf, proplen);
24804577Sahrens 			break;
24814577Sahrens 
24824787Sahrens 		case PROP_TYPE_STRING:
24834577Sahrens 			(void) strlcpy(propbuf,
24844577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
24854577Sahrens 			break;
24864577Sahrens 
24874787Sahrens 		case PROP_TYPE_INDEX:
24884861Sahrens 			if (get_numeric_property(zhp, prop, src,
24894861Sahrens 			    &source, &val) != 0)
24904861Sahrens 				return (-1);
24914861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
24924577Sahrens 				return (-1);
24934577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
24944577Sahrens 			break;
24954577Sahrens 
24964577Sahrens 		default:
24974577Sahrens 			abort();
24984577Sahrens 		}
2499789Sahrens 	}
2500789Sahrens 
2501789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2502789Sahrens 
2503789Sahrens 	return (0);
2504789Sahrens }
2505789Sahrens 
2506789Sahrens /*
2507789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2508789Sahrens  * the given property is the appropriate type; should only be used with
2509789Sahrens  * hard-coded property types.
2510789Sahrens  */
2511789Sahrens uint64_t
2512789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2513789Sahrens {
2514789Sahrens 	char *source;
25152082Seschrock 	uint64_t val;
25162082Seschrock 
25175367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
25182082Seschrock 
25192082Seschrock 	return (val);
2520789Sahrens }
2521789Sahrens 
25225713Srm160521 int
25235713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
25245713Srm160521 {
25255713Srm160521 	char buf[64];
25265713Srm160521 
25275713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
25285713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
25295713Srm160521 }
25305713Srm160521 
2531789Sahrens /*
2532789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2533789Sahrens  */
2534789Sahrens int
2535789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
25365094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2537789Sahrens {
2538789Sahrens 	char *source;
2539789Sahrens 
2540789Sahrens 	/*
2541789Sahrens 	 * Check to see if this property applies to our object
2542789Sahrens 	 */
25434849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
25443237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
25452082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
25462082Seschrock 		    zfs_prop_to_name(prop)));
25474849Sahrens 	}
2548789Sahrens 
2549789Sahrens 	if (src)
25505094Slling 		*src = ZPROP_SRC_NONE;
2551789Sahrens 
25522082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
25532082Seschrock 		return (-1);
2554789Sahrens 
2555789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2556789Sahrens 
2557789Sahrens 	return (0);
2558789Sahrens }
2559789Sahrens 
2560789Sahrens /*
2561789Sahrens  * Returns the name of the given zfs handle.
2562789Sahrens  */
2563789Sahrens const char *
2564789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2565789Sahrens {
2566789Sahrens 	return (zhp->zfs_name);
2567789Sahrens }
2568789Sahrens 
2569789Sahrens /*
2570789Sahrens  * Returns the type of the given zfs handle.
2571789Sahrens  */
2572789Sahrens zfs_type_t
2573789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2574789Sahrens {
2575789Sahrens 	return (zhp->zfs_type);
2576789Sahrens }
2577789Sahrens 
2578789Sahrens /*
25791356Seschrock  * Iterate over all child filesystems
2580789Sahrens  */
2581789Sahrens int
25821356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2583789Sahrens {
2584789Sahrens 	zfs_cmd_t zc = { 0 };
2585789Sahrens 	zfs_handle_t *nzhp;
2586789Sahrens 	int ret;
2587789Sahrens 
25885367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25895367Sahrens 		return (0);
25905367Sahrens 
2591789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25922082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2593789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2594789Sahrens 		/*
2595789Sahrens 		 * Ignore private dataset names.
2596789Sahrens 		 */
2597789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2598789Sahrens 			continue;
2599789Sahrens 
2600789Sahrens 		/*
2601789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2602789Sahrens 		 * that the pool has since been removed.
2603789Sahrens 		 */
26042082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26052082Seschrock 		    zc.zc_name)) == NULL)
2606789Sahrens 			continue;
2607789Sahrens 
2608789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2609789Sahrens 			return (ret);
2610789Sahrens 	}
2611789Sahrens 
2612789Sahrens 	/*
2613789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2614789Sahrens 	 * returned, then the underlying dataset has been removed since we
2615789Sahrens 	 * obtained the handle.
2616789Sahrens 	 */
2617789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26182082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26192082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2620789Sahrens 
26211356Seschrock 	return (0);
26221356Seschrock }
26231356Seschrock 
26241356Seschrock /*
26251356Seschrock  * Iterate over all snapshots
26261356Seschrock  */
26271356Seschrock int
26281356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26291356Seschrock {
26301356Seschrock 	zfs_cmd_t zc = { 0 };
26311356Seschrock 	zfs_handle_t *nzhp;
26321356Seschrock 	int ret;
2633789Sahrens 
26345367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
26355367Sahrens 		return (0);
26365367Sahrens 
2637789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26382082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
26392082Seschrock 	    &zc) == 0;
2640789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2641789Sahrens 
26422082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26432082Seschrock 		    zc.zc_name)) == NULL)
2644789Sahrens 			continue;
2645789Sahrens 
2646789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2647789Sahrens 			return (ret);
2648789Sahrens 	}
2649789Sahrens 
2650789Sahrens 	/*
2651789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2652789Sahrens 	 * returned, then the underlying dataset has been removed since we
2653789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2654789Sahrens 	 */
2655789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26562082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26572082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2658789Sahrens 
2659789Sahrens 	return (0);
2660789Sahrens }
2661789Sahrens 
2662789Sahrens /*
26631356Seschrock  * Iterate over all children, snapshots and filesystems
26641356Seschrock  */
26651356Seschrock int
26661356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26671356Seschrock {
26681356Seschrock 	int ret;
26691356Seschrock 
26701356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
26711356Seschrock 		return (ret);
26721356Seschrock 
26731356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
26741356Seschrock }
26751356Seschrock 
26761356Seschrock /*
2677789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2678789Sahrens  * Can return NULL if this is a pool.
2679789Sahrens  */
2680789Sahrens static int
2681789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2682789Sahrens {
2683789Sahrens 	char *loc;
2684789Sahrens 
2685789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2686789Sahrens 		return (-1);
2687789Sahrens 
2688789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2689789Sahrens 	buf[loc - path] = '\0';
2690789Sahrens 
2691789Sahrens 	return (0);
2692789Sahrens }
2693789Sahrens 
2694789Sahrens /*
26954490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
26964490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26974490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
26984490Svb160487  * length of already existing prefix of the given path.  We also fetch the
26994490Svb160487  * 'zoned' property, which is used to validate property settings when creating
27004490Svb160487  * new datasets.
2701789Sahrens  */
2702789Sahrens static int
27034490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
27044490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2705789Sahrens {
2706789Sahrens 	zfs_cmd_t zc = { 0 };
2707789Sahrens 	char parent[ZFS_MAXNAMELEN];
2708789Sahrens 	char *slash;
27091356Seschrock 	zfs_handle_t *zhp;
27102082Seschrock 	char errbuf[1024];
27112082Seschrock 
27122082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
27132082Seschrock 	    path);
2714789Sahrens 
2715789Sahrens 	/* get parent, and check to see if this is just a pool */
2716789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
27172082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27182082Seschrock 		    "missing dataset name"));
27192082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2720789Sahrens 	}
2721789Sahrens 
2722789Sahrens 	/* check to see if the pool exists */
2723789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2724789Sahrens 		slash = parent + strlen(parent);
2725789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2726789Sahrens 	zc.zc_name[slash - parent] = '\0';
27272082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2728789Sahrens 	    errno == ENOENT) {
27292082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27302082Seschrock 		    "no such pool '%s'"), zc.zc_name);
27312082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2732789Sahrens 	}
2733789Sahrens 
2734789Sahrens 	/* check to see if the parent dataset exists */
27354490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27364490Svb160487 		if (errno == ENOENT && accept_ancestor) {
27374490Svb160487 			/*
27384490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
27394490Svb160487 			 */
27404490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27414490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27424490Svb160487 				    "no such pool '%s'"), zc.zc_name);
27434490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27444490Svb160487 			}
27454490Svb160487 		} else if (errno == ENOENT) {
27462082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27472082Seschrock 			    "parent does not exist"));
27482082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27494490Svb160487 		} else
27502082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2751789Sahrens 	}
2752789Sahrens 
27532676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2754789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
27552676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
27562082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27571356Seschrock 		zfs_close(zhp);
2758789Sahrens 		return (-1);
2759789Sahrens 	}
2760789Sahrens 
2761789Sahrens 	/* make sure parent is a filesystem */
27621356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
27632082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27642082Seschrock 		    "parent is not a filesystem"));
27652082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27661356Seschrock 		zfs_close(zhp);
2767789Sahrens 		return (-1);
2768789Sahrens 	}
2769789Sahrens 
27701356Seschrock 	zfs_close(zhp);
27714490Svb160487 	if (prefixlen != NULL)
27724490Svb160487 		*prefixlen = strlen(parent);
27734490Svb160487 	return (0);
27744490Svb160487 }
27754490Svb160487 
27764490Svb160487 /*
27774490Svb160487  * Finds whether the dataset of the given type(s) exists.
27784490Svb160487  */
27794490Svb160487 boolean_t
27804490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
27814490Svb160487 {
27824490Svb160487 	zfs_handle_t *zhp;
27834490Svb160487 
27845326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
27854490Svb160487 		return (B_FALSE);
27864490Svb160487 
27874490Svb160487 	/*
27884490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27894490Svb160487 	 */
27904490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27914490Svb160487 		int ds_type = zhp->zfs_type;
27924490Svb160487 
27934490Svb160487 		zfs_close(zhp);
27944490Svb160487 		if (types & ds_type)
27954490Svb160487 			return (B_TRUE);
27964490Svb160487 	}
27974490Svb160487 	return (B_FALSE);
27984490Svb160487 }
27994490Svb160487 
28004490Svb160487 /*
28015367Sahrens  * Given a path to 'target', create all the ancestors between
28025367Sahrens  * the prefixlen portion of the path, and the target itself.
28035367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
28045367Sahrens  */
28055367Sahrens int
28065367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
28075367Sahrens {
28085367Sahrens 	zfs_handle_t *h;
28095367Sahrens 	char *cp;
28105367Sahrens 	const char *opname;
28115367Sahrens 
28125367Sahrens 	/* make sure prefix exists */
28135367Sahrens 	cp = target + prefixlen;
28145367Sahrens 	if (*cp != '/') {
28155367Sahrens 		assert(strchr(cp, '/') == NULL);
28165367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28175367Sahrens 	} else {
28185367Sahrens 		*cp = '\0';
28195367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28205367Sahrens 		*cp = '/';
28215367Sahrens 	}
28225367Sahrens 	if (h == NULL)
28235367Sahrens 		return (-1);
28245367Sahrens 	zfs_close(h);
28255367Sahrens 
28265367Sahrens 	/*
28275367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28285367Sahrens 	 * up to the prefixlen-long one.
28295367Sahrens 	 */
28305367Sahrens 	for (cp = target + prefixlen + 1;
28315367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28325367Sahrens 		char *logstr;
28335367Sahrens 
28345367Sahrens 		*cp = '\0';
28355367Sahrens 
28365367Sahrens 		h = make_dataset_handle(hdl, target);
28375367Sahrens 		if (h) {
28385367Sahrens 			/* it already exists, nothing to do here */
28395367Sahrens 			zfs_close(h);
28405367Sahrens 			continue;
28415367Sahrens 		}
28425367Sahrens 
28435367Sahrens 		logstr = hdl->libzfs_log_str;
28445367Sahrens 		hdl->libzfs_log_str = NULL;
28455367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28465367Sahrens 		    NULL) != 0) {
28475367Sahrens 			hdl->libzfs_log_str = logstr;
28485367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28495367Sahrens 			goto ancestorerr;
28505367Sahrens 		}
28515367Sahrens 
28525367Sahrens 		hdl->libzfs_log_str = logstr;
28535367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28545367Sahrens 		if (h == NULL) {
28555367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28565367Sahrens 			goto ancestorerr;
28575367Sahrens 		}
28585367Sahrens 
28595367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28605367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28615367Sahrens 			goto ancestorerr;
28625367Sahrens 		}
28635367Sahrens 
28645367Sahrens 		if (zfs_share(h) != 0) {
28655367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28665367Sahrens 			goto ancestorerr;
28675367Sahrens 		}
28685367Sahrens 
28695367Sahrens 		zfs_close(h);
28705367Sahrens 	}
28715367Sahrens 
28725367Sahrens 	return (0);
28735367Sahrens 
28745367Sahrens ancestorerr:
28755367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28765367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28775367Sahrens 	return (-1);
28785367Sahrens }
28795367Sahrens 
28805367Sahrens /*
28814490Svb160487  * Creates non-existing ancestors of the given path.
28824490Svb160487  */
28834490Svb160487 int
28844490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28854490Svb160487 {
28864490Svb160487 	int prefix;
28874490Svb160487 	uint64_t zoned;
28884490Svb160487 	char *path_copy;
28894490Svb160487 	int rc;
28904490Svb160487 
28914490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28924490Svb160487 		return (-1);
28934490Svb160487 
28944490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28954490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28964490Svb160487 		free(path_copy);
28974490Svb160487 	}
28984490Svb160487 	if (path_copy == NULL || rc != 0)
28994490Svb160487 		return (-1);
29004490Svb160487 
2901789Sahrens 	return (0);
2902789Sahrens }
2903789Sahrens 
2904789Sahrens /*
29052676Seschrock  * Create a new filesystem or volume.
2906789Sahrens  */
2907789Sahrens int
29082082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
29092676Seschrock     nvlist_t *props)
2910789Sahrens {
2911789Sahrens 	zfs_cmd_t zc = { 0 };
2912789Sahrens 	int ret;
2913789Sahrens 	uint64_t size = 0;
2914789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
29152082Seschrock 	char errbuf[1024];
29162676Seschrock 	uint64_t zoned;
29172082Seschrock 
29182082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29192082Seschrock 	    "cannot create '%s'"), path);
2920789Sahrens 
2921789Sahrens 	/* validate the path, taking care to note the extended error message */
29225326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
29232082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2924789Sahrens 
2925789Sahrens 	/* validate parents exist */
29264490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2927789Sahrens 		return (-1);
2928789Sahrens 
2929789Sahrens 	/*
2930789Sahrens 	 * The failure modes when creating a dataset of a different type over
2931789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2932789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2933789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2934789Sahrens 	 * first try to see if the dataset exists.
2935789Sahrens 	 */
2936789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
29375094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
29382082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29392082Seschrock 		    "dataset already exists"));
29402082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2941789Sahrens 	}
2942789Sahrens 
2943789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2944789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2945789Sahrens 	else
2946789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2947789Sahrens 
29487184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
29493912Slling 	    zoned, NULL, errbuf)) == 0)
29502676Seschrock 		return (-1);
29512676Seschrock 
2952789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29531133Seschrock 		/*
29541133Seschrock 		 * If we are creating a volume, the size and block size must
29551133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29561133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29571133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29581133Seschrock 		 * zero.
29591133Seschrock 		 */
29602676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
29612676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
29622676Seschrock 			nvlist_free(props);
29632082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29642676Seschrock 			    "missing volume size"));
29652676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2966789Sahrens 		}
2967789Sahrens 
29682676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
29692676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29702676Seschrock 		    &blocksize)) != 0) {
29712676Seschrock 			if (ret == ENOENT) {
29722676Seschrock 				blocksize = zfs_prop_default_numeric(
29732676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29742676Seschrock 			} else {
29752676Seschrock 				nvlist_free(props);
29762676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29772676Seschrock 				    "missing volume block size"));
29782676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29792676Seschrock 			}
29802676Seschrock 		}
29812676Seschrock 
29822676Seschrock 		if (size == 0) {
29832676Seschrock 			nvlist_free(props);
29842082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29852676Seschrock 			    "volume size cannot be zero"));
29862676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29871133Seschrock 		}
29881133Seschrock 
29891133Seschrock 		if (size % blocksize != 0) {
29902676Seschrock 			nvlist_free(props);
29912082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29922676Seschrock 			    "volume size must be a multiple of volume block "
29932676Seschrock 			    "size"));
29942676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29951133Seschrock 		}
2996789Sahrens 	}
2997789Sahrens 
29985094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
29992676Seschrock 		return (-1);
30002676Seschrock 	nvlist_free(props);
30012676Seschrock 
3002789Sahrens 	/* create the dataset */
30034543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3004789Sahrens 
30053912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
30062082Seschrock 		ret = zvol_create_link(hdl, path);
30073912Slling 		if (ret) {
30083912Slling 			(void) zfs_standard_error(hdl, errno,
30093912Slling 			    dgettext(TEXT_DOMAIN,
30103912Slling 			    "Volume successfully created, but device links "
30113912Slling 			    "were not created"));
30123912Slling 			zcmd_free_nvlists(&zc);
30133912Slling 			return (-1);
30143912Slling 		}
30153912Slling 	}
3016789Sahrens 
30172676Seschrock 	zcmd_free_nvlists(&zc);
30182676Seschrock 
3019789Sahrens 	/* check for failure */
3020789Sahrens 	if (ret != 0) {
3021789Sahrens 		char parent[ZFS_MAXNAMELEN];
3022789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3023789Sahrens 
3024789Sahrens 		switch (errno) {
3025789Sahrens 		case ENOENT:
30262082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30272082Seschrock 			    "no such parent '%s'"), parent);
30282082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3029789Sahrens 
3030789Sahrens 		case EINVAL:
30312082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30323413Smmusante 			    "parent '%s' is not a filesystem"), parent);
30332082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3034789Sahrens 
3035789Sahrens 		case EDOM:
30362082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30372676Seschrock 			    "volume block size must be power of 2 from "
30382676Seschrock 			    "%u to %uk"),
3039789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3040789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
30412082Seschrock 
30422676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30432082Seschrock 
30444603Sahrens 		case ENOTSUP:
30454603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30464603Sahrens 			    "pool must be upgraded to set this "
30474603Sahrens 			    "property or value"));
30484603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3049789Sahrens #ifdef _ILP32
3050789Sahrens 		case EOVERFLOW:
3051789Sahrens 			/*
3052789Sahrens 			 * This platform can't address a volume this big.
3053789Sahrens 			 */
30542082Seschrock 			if (type == ZFS_TYPE_VOLUME)
30552082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
30562082Seschrock 				    errbuf));
3057789Sahrens #endif
30582082Seschrock 			/* FALLTHROUGH */
3059789Sahrens 		default:
30602082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3061789Sahrens 		}
3062789Sahrens 	}
3063789Sahrens 
3064789Sahrens 	return (0);
3065789Sahrens }
3066789Sahrens 
3067789Sahrens /*
3068789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3069789Sahrens  * isn't mounted, and that there are no active dependents.
3070789Sahrens  */
3071789Sahrens int
3072789Sahrens zfs_destroy(zfs_handle_t *zhp)
3073789Sahrens {
3074789Sahrens 	zfs_cmd_t zc = { 0 };
3075789Sahrens 
3076789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3077789Sahrens 
30782676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30793126Sahl 		/*
30804543Smarks 		 * If user doesn't have permissions to unshare volume, then
30814543Smarks 		 * abort the request.  This would only happen for a
30824543Smarks 		 * non-privileged user.
30833126Sahl 		 */
30844543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30854543Smarks 			return (-1);
30864543Smarks 		}
30873126Sahl 
30882082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3089789Sahrens 			return (-1);
3090789Sahrens 
3091789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3092789Sahrens 	} else {
3093789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3094789Sahrens 	}
3095789Sahrens 
30964543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30973237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30982082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30992082Seschrock 		    zhp->zfs_name));
31002199Sahrens 	}
3101789Sahrens 
3102789Sahrens 	remove_mountpoint(zhp);
3103789Sahrens 
3104789Sahrens 	return (0);
3105789Sahrens }
3106789Sahrens 
31072199Sahrens struct destroydata {
31082199Sahrens 	char *snapname;
31092199Sahrens 	boolean_t gotone;
31103265Sahrens 	boolean_t closezhp;
31112199Sahrens };
31122199Sahrens 
31132199Sahrens static int
31142199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
31152199Sahrens {
31162199Sahrens 	struct destroydata *dd = arg;
31172199Sahrens 	zfs_handle_t *szhp;
31182199Sahrens 	char name[ZFS_MAXNAMELEN];
31193265Sahrens 	boolean_t closezhp = dd->closezhp;
31203265Sahrens 	int rv;
31212199Sahrens 
31222676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31232676Seschrock 	(void) strlcat(name, "@", sizeof (name));
31242676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
31252199Sahrens 
31262199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31272199Sahrens 	if (szhp) {
31282199Sahrens 		dd->gotone = B_TRUE;
31292199Sahrens 		zfs_close(szhp);
31302199Sahrens 	}
31312199Sahrens 
31322199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31332199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
31342199Sahrens 		/*
31352199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31362199Sahrens 		 * return an error, because then we wouldn't visit all
31372199Sahrens 		 * the volumes.
31382199Sahrens 		 */
31392199Sahrens 	}
31402199Sahrens 
31413265Sahrens 	dd->closezhp = B_TRUE;
31423265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
31433265Sahrens 	if (closezhp)
31443265Sahrens 		zfs_close(zhp);
31453265Sahrens 	return (rv);
31462199Sahrens }
31472199Sahrens 
31482199Sahrens /*
31492199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31502199Sahrens  */
31512199Sahrens int
31522199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
31532199Sahrens {
31542199Sahrens 	zfs_cmd_t zc = { 0 };
31552199Sahrens 	int ret;
31562199Sahrens 	struct destroydata dd = { 0 };
31572199Sahrens 
31582199Sahrens 	dd.snapname = snapname;
31592199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
31602199Sahrens 
31612199Sahrens 	if (!dd.gotone) {
31623237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31632199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
31642199Sahrens 		    zhp->zfs_name, snapname));
31652199Sahrens 	}
31662199Sahrens 
31672199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31682676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
31692199Sahrens 
31704543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
31712199Sahrens 	if (ret != 0) {
31722199Sahrens 		char errbuf[1024];
31732199Sahrens 
31742199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31752199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31762199Sahrens 
31772199Sahrens 		switch (errno) {
31782199Sahrens 		case EEXIST:
31792199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31802199Sahrens 			    "snapshot is cloned"));
31812199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31822199Sahrens 
31832199Sahrens 		default:
31842199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31852199Sahrens 			    errbuf));
31862199Sahrens 		}
31872199Sahrens 	}
31882199Sahrens 
31892199Sahrens 	return (0);
31902199Sahrens }
31912199Sahrens 
3192789Sahrens /*
3193789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3194789Sahrens  */
3195789Sahrens int
31962676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3197789Sahrens {
3198789Sahrens 	zfs_cmd_t zc = { 0 };
3199789Sahrens 	char parent[ZFS_MAXNAMELEN];
3200789Sahrens 	int ret;
32012082Seschrock 	char errbuf[1024];
32022082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
32032676Seschrock 	zfs_type_t type;
32042676Seschrock 	uint64_t zoned;
3205789Sahrens 
3206789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3207789Sahrens 
32082082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
32092082Seschrock 	    "cannot create '%s'"), target);
32102082Seschrock 
3211789Sahrens 	/* validate the target name */
32125326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
32132082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3214789Sahrens 
3215789Sahrens 	/* validate parents exist */
32164490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3217789Sahrens 		return (-1);
3218789Sahrens 
3219789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3220789Sahrens 
3221789Sahrens 	/* do the clone */
32222676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3223789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32242744Snn35248 		type = ZFS_TYPE_VOLUME;
32252676Seschrock 	} else {
3226789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32272744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
32282676Seschrock 	}
32292676Seschrock 
32302676Seschrock 	if (props) {
32317184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
32327184Stimh 		    zhp, errbuf)) == NULL)
32332676Seschrock 			return (-1);
32342676Seschrock 
32355094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
32362676Seschrock 			nvlist_free(props);
32372676Seschrock 			return (-1);
32382676Seschrock 		}
32392676Seschrock 
32402676Seschrock 		nvlist_free(props);
32412676Seschrock 	}
3242789Sahrens 
3243789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
32442676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
32454543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3246789Sahrens 
32472676Seschrock 	zcmd_free_nvlists(&zc);
32482676Seschrock 
3249789Sahrens 	if (ret != 0) {
3250789Sahrens 		switch (errno) {
3251789Sahrens 
3252789Sahrens 		case ENOENT:
3253789Sahrens 			/*
3254789Sahrens 			 * The parent doesn't exist.  We should have caught this
3255789Sahrens 			 * above, but there may a race condition that has since
3256789Sahrens 			 * destroyed the parent.
3257789Sahrens 			 *
3258789Sahrens 			 * At this point, we don't know whether it's the source
3259789Sahrens 			 * that doesn't exist anymore, or whether the target
3260789Sahrens 			 * dataset doesn't exist.
3261789Sahrens 			 */
32622082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32632082Seschrock 			    "no such parent '%s'"), parent);
32642082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
32652082Seschrock 
32662082Seschrock 		case EXDEV:
32672082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32682082Seschrock 			    "source and target pools differ"));
32692082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
32702082Seschrock 			    errbuf));
32712082Seschrock 
32722082Seschrock 		default:
32732082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32742082Seschrock 			    errbuf));
32752082Seschrock 		}
32762676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32772082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32782082Seschrock 	}
32792082Seschrock 
32802082Seschrock 	return (ret);
32812082Seschrock }
32822082Seschrock 
32832082Seschrock typedef struct promote_data {
32842082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32852082Seschrock 	const char *cb_target;
32862082Seschrock 	const char *cb_errbuf;
32872082Seschrock 	uint64_t cb_pivot_txg;
32882082Seschrock } promote_data_t;
32892082Seschrock 
32902082Seschrock static int
32912082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32922082Seschrock {
32932082Seschrock 	promote_data_t *pd = data;
32942082Seschrock 	zfs_handle_t *szhp;
32952082Seschrock 	char snapname[MAXPATHLEN];
32963265Sahrens 	int rv = 0;
32972082Seschrock 
32982082Seschrock 	/* We don't care about snapshots after the pivot point */
32993265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
33003265Sahrens 		zfs_close(zhp);
33012082Seschrock 		return (0);
33023265Sahrens 	}
33032082Seschrock 
33042417Sahrens 	/* Remove the device link if it's a zvol. */
33052676Seschrock 	if (ZFS_IS_VOLUME(zhp))
33062417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
33072082Seschrock 
33082082Seschrock 	/* Check for conflicting names */
33092676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
33102676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
33112082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
33122082Seschrock 	if (szhp != NULL) {
33132082Seschrock 		zfs_close(szhp);
33142082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
33152082Seschrock 		    "snapshot name '%s' from origin \n"
33162082Seschrock 		    "conflicts with '%s' from target"),
33172082Seschrock 		    zhp->zfs_name, snapname);
33183265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
33192082Seschrock 	}
33203265Sahrens 	zfs_close(zhp);
33213265Sahrens 	return (rv);
33222082Seschrock }
33232082Seschrock 
33242417Sahrens static int
33252417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
33262417Sahrens {
33272417Sahrens 	promote_data_t *pd = data;
33282417Sahrens 
33292417Sahrens 	/* We don't care about snapshots after the pivot point */
33303265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
33313265Sahrens 		/* Create the device link if it's a zvol. */
33323265Sahrens 		if (ZFS_IS_VOLUME(zhp))
33333265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
33343265Sahrens 	}
33353265Sahrens 
33363265Sahrens 	zfs_close(zhp);
33372417Sahrens 	return (0);
33382417Sahrens }
33392417Sahrens 
33402082Seschrock /*
33412082Seschrock  * Promotes the given clone fs to be the clone parent.
33422082Seschrock  */
33432082Seschrock int
33442082Seschrock zfs_promote(zfs_handle_t *zhp)
33452082Seschrock {
33462082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33472082Seschrock 	zfs_cmd_t zc = { 0 };
33482082Seschrock 	char parent[MAXPATHLEN];
33492082Seschrock 	char *cp;
33502082Seschrock 	int ret;
33512082Seschrock 	zfs_handle_t *pzhp;
33522082Seschrock 	promote_data_t pd;
33532082Seschrock 	char errbuf[1024];
33542082Seschrock 
33552082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33562082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
33572082Seschrock 
33582082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
33592082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33602082Seschrock 		    "snapshots can not be promoted"));
33612082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33622082Seschrock 	}
33632082Seschrock 
33645367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
33652082Seschrock 	if (parent[0] == '\0') {
33662082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33672082Seschrock 		    "not a cloned filesystem"));
33682082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33692082Seschrock 	}
33702082Seschrock 	cp = strchr(parent, '@');
33712082Seschrock 	*cp = '\0';
33722082Seschrock 
33732082Seschrock 	/* Walk the snapshots we will be moving */
33745367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
33752082Seschrock 	if (pzhp == NULL)
33762082Seschrock 		return (-1);
33772082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33782082Seschrock 	zfs_close(pzhp);
33792082Seschrock 	pd.cb_target = zhp->zfs_name;
33802082Seschrock 	pd.cb_errbuf = errbuf;
33815094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
33822082Seschrock 	if (pzhp == NULL)
33832082Seschrock 		return (-1);
33842082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33852082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33862082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33872417Sahrens 	if (ret != 0) {
33882417Sahrens 		zfs_close(pzhp);
33892082Seschrock 		return (-1);
33902417Sahrens 	}
33912082Seschrock 
33922082Seschrock 	/* issue the ioctl */
33935367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
33942676Seschrock 	    sizeof (zc.zc_value));
33952082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33964543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33972082Seschrock 
33982082Seschrock 	if (ret != 0) {
33992417Sahrens 		int save_errno = errno;
34002417Sahrens 
34012417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
34022417Sahrens 		zfs_close(pzhp);
34032417Sahrens 
34042417Sahrens 		switch (save_errno) {
3405789Sahrens 		case EEXIST:
3406789Sahrens 			/*
34072082Seschrock 			 * There is a conflicting snapshot name.  We
34082082Seschrock 			 * should have caught this above, but they could
34092082Seschrock 			 * have renamed something in the mean time.
3410789Sahrens 			 */
34112082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34122082Seschrock 			    "conflicting snapshot name from parent '%s'"),
34132082Seschrock 			    parent);
34142082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3415789Sahrens 
3416789Sahrens 		default:
34172417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3418789Sahrens 		}
34192417Sahrens 	} else {
34202417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3421789Sahrens 	}
3422789Sahrens 
34232417Sahrens 	zfs_close(pzhp);
3424789Sahrens 	return (ret);
3425789Sahrens }
3426789Sahrens 
34274007Smmusante struct createdata {
34284007Smmusante 	const char *cd_snapname;
34294007Smmusante 	int cd_ifexists;
34304007Smmusante };
34314007Smmusante 
34322199Sahrens static int
34332199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
34342199Sahrens {
34354007Smmusante 	struct createdata *cd = arg;
34362676Seschrock 	int ret;
34372199Sahrens 
34382199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
34392199Sahrens 		char name[MAXPATHLEN];
34402199Sahrens 
34412676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
34422676Seschrock 		(void) strlcat(name, "@", sizeof (name));
34434007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
34444007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
34454007Smmusante 		    cd->cd_ifexists);
34462199Sahrens 		/*
34472199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
34482199Sahrens 		 * return an error, because then we wouldn't visit all
34492199Sahrens 		 * the volumes.
34502199Sahrens 		 */
34512199Sahrens 	}
34522676Seschrock 
34534007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
34542676Seschrock 
34552676Seschrock 	zfs_close(zhp);
34562676Seschrock 
34572676Seschrock 	return (ret);
34582199Sahrens }
34592199Sahrens 
3460789Sahrens /*
34613504Sahl  * Takes a snapshot of the given dataset.
3462789Sahrens  */
3463789Sahrens int
34647265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
34657265Sahrens     nvlist_t *props)
3466789Sahrens {
3467789Sahrens 	const char *delim;
34687265Sahrens 	char parent[ZFS_MAXNAMELEN];
3469789Sahrens 	zfs_handle_t *zhp;
3470789Sahrens 	zfs_cmd_t zc = { 0 };
3471789Sahrens 	int ret;
34722082Seschrock 	char errbuf[1024];
34732082Seschrock 
34742082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34752082Seschrock 	    "cannot snapshot '%s'"), path);
34762082Seschrock 
34772082Seschrock 	/* validate the target name */
34785326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
34792082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3480789Sahrens 
34817265Sahrens 	if (props) {
34827265Sahrens 		if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
34837265Sahrens 		    props, B_FALSE, NULL, errbuf)) == NULL)
34847265Sahrens 			return (-1);
34857265Sahrens 
34867265Sahrens 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
34877265Sahrens 			nvlist_free(props);
34887265Sahrens 			return (-1);
34897265Sahrens 		}
34907265Sahrens 
34917265Sahrens 		nvlist_free(props);
34927265Sahrens 	}
34937265Sahrens 
3494789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34952199Sahrens 	delim = strchr(path, '@');
3496789Sahrens 	(void) strncpy(parent, path, delim - path);
3497789Sahrens 	parent[delim - path] = '\0';
3498789Sahrens 
34992082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3500789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
35017265Sahrens 		zcmd_free_nvlists(&zc);
3502789Sahrens 		return (-1);
3503789Sahrens 	}
3504789Sahrens 
35052199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35062676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
35074543Smarks 	if (ZFS_IS_VOLUME(zhp))
35084543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
35094543Smarks 	else
35104543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
35112199Sahrens 	zc.zc_cookie = recursive;
35124543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
35132199Sahrens 
35147265Sahrens 	zcmd_free_nvlists(&zc);
35157265Sahrens 
35162199Sahrens 	/*
35172199Sahrens 	 * if it was recursive, the one that actually failed will be in
35182199Sahrens 	 * zc.zc_name.
35192199Sahrens 	 */
35204543Smarks 	if (ret != 0)
35214543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35224543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
35234543Smarks 
35242199Sahrens 	if (ret == 0 && recursive) {
35254007Smmusante 		struct createdata cd;
35264007Smmusante 
35274007Smmusante 		cd.cd_snapname = delim + 1;
35284007Smmusante 		cd.cd_ifexists = B_FALSE;
35294007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
35302199Sahrens 	}
3531789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
35322082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
35332199Sahrens 		if (ret != 0) {
35344543Smarks 			(void) zfs_standard_error(hdl, errno,
35354543Smarks 			    dgettext(TEXT_DOMAIN,
35364543Smarks 			    "Volume successfully snapshotted, but device links "
35374543Smarks 			    "were not created"));
35384543Smarks 			zfs_close(zhp);
35394543Smarks 			return (-1);
35402199Sahrens 		}
3541789Sahrens 	}
3542789Sahrens 
35432082Seschrock 	if (ret != 0)
35442082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3545789Sahrens 
3546789Sahrens 	zfs_close(zhp);
3547789Sahrens 
3548789Sahrens 	return (ret);
3549789Sahrens }
3550789Sahrens 
3551789Sahrens /*
35521294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
35531294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
35541294Slling  * is a dependent and we should just destroy it without checking the transaction
35551294Slling  * group.
3556789Sahrens  */
35571294Slling typedef struct rollback_data {
35581294Slling 	const char	*cb_target;		/* the snapshot */
35591294Slling 	uint64_t	cb_create;		/* creation time reference */
35605749Sahrens 	boolean_t	cb_error;
35612082Seschrock 	boolean_t	cb_dependent;
35625749Sahrens 	boolean_t	cb_force;
35631294Slling } rollback_data_t;
35641294Slling 
35651294Slling static int
35661294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
35671294Slling {
35681294Slling 	rollback_data_t *cbp = data;
35691294Slling 
35701294Slling 	if (!cbp->cb_dependent) {
35711294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
35721294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
35731294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
35741294Slling 		    cbp->cb_create) {
35754543Smarks 			char *logstr;
35761294Slling 
35772082Seschrock 			cbp->cb_dependent = B_TRUE;
35785446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
35795446Sahrens 			    rollback_destroy, cbp);
35802082Seschrock 			cbp->cb_dependent = B_FALSE;
35811294Slling 
35824543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
35834543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
35845446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
35854543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
35861294Slling 		}
35871294Slling 	} else {
35885749Sahrens 		/* We must destroy this clone; first unmount it */
35895749Sahrens 		prop_changelist_t *clp;
35905749Sahrens 
35917366STim.Haley@Sun.COM 		clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
35925749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
35935749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
35945749Sahrens 			cbp->cb_error = B_TRUE;
35955749Sahrens 			zfs_close(zhp);
35965749Sahrens 			return (0);
35975749Sahrens 		}
35985749Sahrens 		if (zfs_destroy(zhp) != 0)
35995749Sahrens 			cbp->cb_error = B_TRUE;
36005749Sahrens 		else
36015749Sahrens 			changelist_remove(clp, zhp->zfs_name);
36025751Sahrens 		(void) changelist_postfix(clp);
36035749Sahrens 		changelist_free(clp);
36041294Slling 	}
36051294Slling 
36061294Slling 	zfs_close(zhp);
36071294Slling 	return (0);
36081294Slling }
36091294Slling 
36101294Slling /*
36115446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
36125446Sahrens  * data changes since then and making it the active dataset.
36135446Sahrens  *
36145446Sahrens  * Any snapshots more recent than the target are destroyed, along with
36155446Sahrens  * their dependents.
36161294Slling  */
36175446Sahrens int
36185749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3619789Sahrens {
36205446Sahrens 	rollback_data_t cb = { 0 };
36215446Sahrens 	int err;
3622789Sahrens 	zfs_cmd_t zc = { 0 };
36235713Srm160521 	boolean_t restore_resv = 0;
36245713Srm160521 	uint64_t old_volsize, new_volsize;
36255713Srm160521 	zfs_prop_t resv_prop;
3626789Sahrens 
3627789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3628789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3629789Sahrens 
36305446Sahrens 	/*
36315446Sahrens 	 * Destroy all recent snapshots and its dependends.
36325446Sahrens 	 */
36335749Sahrens 	cb.cb_force = force;
36345446Sahrens 	cb.cb_target = snap->zfs_name;
36355446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
36365446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
36375446Sahrens 
36385749Sahrens 	if (cb.cb_error)
36395749Sahrens 		return (-1);
36405446Sahrens 
36415446Sahrens 	/*
36425446Sahrens 	 * Now that we have verified that the snapshot is the latest,
36435446Sahrens 	 * rollback to the given snapshot.
36445446Sahrens 	 */
36455446Sahrens 
36465713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
36475713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
36485713Srm160521 			return (-1);
36495713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
36505713Srm160521 			return (-1);
36515713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36525713Srm160521 		restore_resv =
36535713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
36545713Srm160521 	}
3655789Sahrens 
3656789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3657789Sahrens 
36582676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3659789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3660789Sahrens 	else
3661789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3662789Sahrens 
3663789Sahrens 	/*
36645446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
36655446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
36665446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
36675446Sahrens 	 * an unlikely race condition where the user has taken a
36685446Sahrens 	 * snapshot since we verified that this was the most recent.
36695713Srm160521 	 *
3670789Sahrens 	 */
36715446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
36723237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
36732082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
36742082Seschrock 		    zhp->zfs_name);
36755717Srm160521 		return (err);
36765717Srm160521 	}
36775713Srm160521 
36785713Srm160521 	/*
36795713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
36805713Srm160521 	 * rollback reservation and the volsize has changed then set
36815713Srm160521 	 * the reservation property to the post-rollback volsize.
36825713Srm160521 	 * Make a new handle since the rollback closed the dataset.
36835713Srm160521 	 */
36845717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
36855717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
36865717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
36875717Srm160521 			zfs_close(zhp);
36885713Srm160521 			return (err);
36895717Srm160521 		}
36905713Srm160521 		if (restore_resv) {
36915713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36925713Srm160521 			if (old_volsize != new_volsize)
36935717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
36945717Srm160521 				    new_volsize);
36955713Srm160521 		}
36965713Srm160521 		zfs_close(zhp);
3697789Sahrens 	}
36985446Sahrens 	return (err);
36991294Slling }
37001294Slling 
37011294Slling /*
3702789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3703789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3704789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3705789Sahrens  * libzfs_graph.c.
3706789Sahrens  */
3707789Sahrens int
37082474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
37092474Seschrock     zfs_iter_f func, void *data)
3710789Sahrens {
3711789Sahrens 	char **dependents;
3712789Sahrens 	size_t count;
3713789Sahrens 	int i;
3714789Sahrens 	zfs_handle_t *child;
3715789Sahrens 	int ret = 0;
3716789Sahrens 
37172474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
37182474Seschrock 	    &dependents, &count) != 0)
37192474Seschrock 		return (-1);
37202474Seschrock 
3721789Sahrens 	for (i = 0; i < count; i++) {
37222082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
37232082Seschrock 		    dependents[i])) == NULL)
3724789Sahrens 			continue;
3725789Sahrens 
3726789Sahrens 		if ((ret = func(child, data)) != 0)
3727789Sahrens 			break;
3728789Sahrens 	}
3729789Sahrens 
3730789Sahrens 	for (i = 0; i < count; i++)
3731789Sahrens 		free(dependents[i]);
3732789Sahrens 	free(dependents);
3733789Sahrens 
3734789Sahrens 	return (ret);
3735789Sahrens }
3736789Sahrens 
3737789Sahrens /*
3738789Sahrens  * Renames the given dataset.
3739789Sahrens  */
3740789Sahrens int
37414490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3742789Sahrens {
3743789Sahrens 	int ret;
3744789Sahrens 	zfs_cmd_t zc = { 0 };
3745789Sahrens 	char *delim;
37464007Smmusante 	prop_changelist_t *cl = NULL;
37474007Smmusante 	zfs_handle_t *zhrp = NULL;
37484007Smmusante 	char *parentname = NULL;
3749789Sahrens 	char parent[ZFS_MAXNAMELEN];
37502082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37512082Seschrock 	char errbuf[1024];
3752789Sahrens 
3753789Sahrens 	/* if we have the same exact name, just return success */
3754789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3755789Sahrens 		return (0);
3756789Sahrens 
37572082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37582082Seschrock 	    "cannot rename to '%s'"), target);
37592082Seschrock 
3760789Sahrens 	/*
3761789Sahrens 	 * Make sure the target name is valid
3762789Sahrens 	 */
3763789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
37642665Snd150628 		if ((strchr(target, '@') == NULL) ||
37652665Snd150628 		    *target == '@') {
37662665Snd150628 			/*
37672665Snd150628 			 * Snapshot target name is abbreviated,
37682665Snd150628 			 * reconstruct full dataset name
37692665Snd150628 			 */
37702665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
37712665Snd150628 			    sizeof (parent));
37722665Snd150628 			delim = strchr(parent, '@');
37732665Snd150628 			if (strchr(target, '@') == NULL)
37742665Snd150628 				*(++delim) = '\0';
37752665Snd150628 			else
37762665Snd150628 				*delim = '\0';
37772665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
37782665Snd150628 			target = parent;
37792665Snd150628 		} else {
37802665Snd150628 			/*
37812665Snd150628 			 * Make sure we're renaming within the same dataset.
37822665Snd150628 			 */
37832665Snd150628 			delim = strchr(target, '@');
37842665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
37852665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
37862665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37872665Snd150628 				    "snapshots must be part of same "
37882665Snd150628 				    "dataset"));
37892665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
37903912Slling 				    errbuf));
37912665Snd150628 			}
3792789Sahrens 		}
37935326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37942665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3795789Sahrens 	} else {
37964007Smmusante 		if (recursive) {
37974007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37984007Smmusante 			    "recursive rename must be a snapshot"));
37994007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
38004007Smmusante 		}
38014007Smmusante 
38025326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
38032665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
38042676Seschrock 		uint64_t unused;
38052676Seschrock 
3806789Sahrens 		/* validate parents */
38074490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3808789Sahrens 			return (-1);
3809789Sahrens 
3810789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3811789Sahrens 
3812789Sahrens 		/* make sure we're in the same pool */
3813789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3814789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3815789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
38162082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38172082Seschrock 			    "datasets must be within same pool"));
38182082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3819789Sahrens 		}
38202440Snd150628 
38212440Snd150628 		/* new name cannot be a child of the current dataset name */
38222440Snd150628 		if (strncmp(parent, zhp->zfs_name,
38233912Slling 		    strlen(zhp->zfs_name)) == 0) {
38242440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38252440Snd150628 			    "New dataset name cannot be a descendent of "
38262440Snd150628 			    "current dataset name"));
38272440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
38282440Snd150628 		}
3829789Sahrens 	}
3830789Sahrens 
38312082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
38322082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
38332082Seschrock 
3834789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3835789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
38362082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38372082Seschrock 		    "dataset is used in a non-global zone"));
38382082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3839789Sahrens 	}
3840789Sahrens 
38414007Smmusante 	if (recursive) {
38424007Smmusante 		struct destroydata dd;
38434007Smmusante 
38444183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
38454183Smmusante 		if (parentname == NULL) {
38464183Smmusante 			ret = -1;
38474183Smmusante 			goto error;
38484183Smmusante 		}
38494007Smmusante 		delim = strchr(parentname, '@');
38504007Smmusante 		*delim = '\0';
38515094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
38524007Smmusante 		if (zhrp == NULL) {
38534183Smmusante 			ret = -1;
38544183Smmusante 			goto error;
38554007Smmusante 		}
38564007Smmusante 
38574007Smmusante 		dd.snapname = delim + 1;
38584007Smmusante 		dd.gotone = B_FALSE;
38594183Smmusante 		dd.closezhp = B_TRUE;
38604007Smmusante 
38614007Smmusante 		/* We remove any zvol links prior to renaming them */
38624007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
38634007Smmusante 		if (ret) {
38644007Smmusante 			goto error;
38654007Smmusante 		}
38664007Smmusante 	} else {
38677366STim.Haley@Sun.COM 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
38684007Smmusante 			return (-1);
38694007Smmusante 
38704007Smmusante 		if (changelist_haszonedchild(cl)) {
38714007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38724007Smmusante 			    "child dataset with inherited mountpoint is used "
38734007Smmusante 			    "in a non-global zone"));
38744007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
38754007Smmusante 			goto error;
38764007Smmusante 		}
38774007Smmusante 
38784007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
38794007Smmusante 			goto error;
3880789Sahrens 	}
3881789Sahrens 
38822676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3883789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3884789Sahrens 	else
3885789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3886789Sahrens 
38872665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
38882676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
38892665Snd150628 
38904007Smmusante 	zc.zc_cookie = recursive;
38914007Smmusante 
38924543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
38934007Smmusante 		/*
38944007Smmusante 		 * if it was recursive, the one that actually failed will
38954007Smmusante 		 * be in zc.zc_name
38964007Smmusante 		 */
38974007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38985367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
38994007Smmusante 
39004007Smmusante 		if (recursive && errno == EEXIST) {
39014007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39024007Smmusante 			    "a child dataset already has a snapshot "
39034007Smmusante 			    "with the new name"));
39044801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
39054007Smmusante 		} else {
39064007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
39074007Smmusante 		}
3908789Sahrens 
3909789Sahrens 		/*
3910789Sahrens 		 * On failure, we still want to remount any filesystems that
3911789Sahrens 		 * were previously mounted, so we don't alter the system state.
3912789Sahrens 		 */
39134007Smmusante 		if (recursive) {
39144007Smmusante 			struct createdata cd;
39154007Smmusante 
39164007Smmusante 			/* only create links for datasets that had existed */
39174007Smmusante 			cd.cd_snapname = delim + 1;
39184007Smmusante 			cd.cd_ifexists = B_TRUE;
39194007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
39204007Smmusante 			    &cd);
39214007Smmusante 		} else {
39224007Smmusante 			(void) changelist_postfix(cl);
39234007Smmusante 		}
3924789Sahrens 	} else {
39254007Smmusante 		if (recursive) {
39264007Smmusante 			struct createdata cd;
39274007Smmusante 
39284007Smmusante 			/* only create links for datasets that had existed */
39294007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
39304007Smmusante 			cd.cd_ifexists = B_TRUE;
39314007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
39324007Smmusante 			    &cd);
39334007Smmusante 		} else {
39344007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
39354007Smmusante 			ret = changelist_postfix(cl);
39364007Smmusante 		}
3937789Sahrens 	}
3938789Sahrens 
3939789Sahrens error:
39404007Smmusante 	if (parentname) {
39414007Smmusante 		free(parentname);
39424007Smmusante 	}
39434007Smmusante 	if (zhrp) {
39444007Smmusante 		zfs_close(zhrp);
39454007Smmusante 	}
39464007Smmusante 	if (cl) {
39474007Smmusante 		changelist_free(cl);
39484007Smmusante 	}
3949789Sahrens 	return (ret);
3950789Sahrens }
3951789Sahrens 
3952789Sahrens /*
3953789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3954789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3955789Sahrens  */
3956789Sahrens int
39572082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3958789Sahrens {
39594007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
39604007Smmusante }
39614007Smmusante 
39624007Smmusante static int
39634007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
39644007Smmusante {
3965789Sahrens 	zfs_cmd_t zc = { 0 };
39662082Seschrock 	di_devlink_handle_t dhdl;
39674543Smarks 	priv_set_t *priv_effective;
39684543Smarks 	int privileged;
3969789Sahrens 
3970789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3971789Sahrens 
3972789Sahrens 	/*
3973789Sahrens 	 * Issue the appropriate ioctl.
3974789Sahrens 	 */
39752082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3976789Sahrens 		switch (errno) {
3977789Sahrens 		case EEXIST:
3978789Sahrens 			/*
3979789Sahrens 			 * Silently ignore the case where the link already
3980789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3981789Sahrens 			 * times without errors.
3982789Sahrens 			 */
3983789Sahrens 			return (0);
3984789Sahrens 
39854007Smmusante 		case ENOENT:
39864007Smmusante 			/*
39874007Smmusante 			 * Dataset does not exist in the kernel.  If we
39884007Smmusante 			 * don't care (see zfs_rename), then ignore the
39894007Smmusante 			 * error quietly.
39904007Smmusante 			 */
39914007Smmusante 			if (ifexists) {
39924007Smmusante 				return (0);
39934007Smmusante 			}
39944007Smmusante 
39954007Smmusante 			/* FALLTHROUGH */
39964007Smmusante 
3997789Sahrens 		default:
39983237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39992082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
40002082Seschrock 			    "for '%s'"), dataset));
4001789Sahrens 		}
4002789Sahrens 	}
4003789Sahrens 
4004789Sahrens 	/*
40054543Smarks 	 * If privileged call devfsadm and wait for the links to
40064543Smarks 	 * magically appear.
40074543Smarks 	 * Otherwise, print out an informational message.
4008789Sahrens 	 */
40094543Smarks 
40104543Smarks 	priv_effective = priv_allocset();
40114543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
40124543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
40134543Smarks 	priv_freeset(priv_effective);
40144543Smarks 
40154543Smarks 	if (privileged) {
40164543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
40174543Smarks 		    DI_MAKE_LINK)) == NULL) {
40184543Smarks 			zfs_error_aux(hdl, strerror(errno));
40197301SEric.Taylor@Sun.COM 			(void) zfs_error_fmt(hdl, errno,
40204543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
40214543Smarks 			    "for '%s'"), dataset);
40224543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
40234543Smarks 			return (-1);
40244543Smarks 		} else {
40254543Smarks 			(void) di_devlink_fini(&dhdl);
40264543Smarks 		}
4027789Sahrens 	} else {
40284543Smarks 		char pathname[MAXPATHLEN];
40294543Smarks 		struct stat64 statbuf;
40304543Smarks 		int i;
40314543Smarks 
40324543Smarks #define	MAX_WAIT	10
40334543Smarks 
40344543Smarks 		/*
40354543Smarks 		 * This is the poor mans way of waiting for the link
40364543Smarks 		 * to show up.  If after 10 seconds we still don't
40374543Smarks 		 * have it, then print out a message.
40384543Smarks 		 */
40394543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
40404543Smarks 		    dataset);
40414543Smarks 
40424543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
40434543Smarks 			if (stat64(pathname, &statbuf) == 0)
40444543Smarks 				break;
40454543Smarks 			(void) sleep(1);
40464543Smarks 		}
40474543Smarks 		if (i == MAX_WAIT)
40484543Smarks 			(void) printf(gettext("%s may not be immediately "
40494543Smarks 			    "available\n"), pathname);
4050789Sahrens 	}
4051789Sahrens 
4052789Sahrens 	return (0);
4053789Sahrens }
4054789Sahrens 
4055789Sahrens /*
4056789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4057789Sahrens  */
4058789Sahrens int
40592082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4060789Sahrens {
4061789Sahrens 	zfs_cmd_t zc = { 0 };
4062789Sahrens 
4063789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4064789Sahrens 
40652082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4066789Sahrens 		switch (errno) {
4067789Sahrens 		case ENXIO:
4068789Sahrens 			/*
4069789Sahrens 			 * Silently ignore the case where the link no longer
4070789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4071789Sahrens 			 * times without errors.
4072789Sahrens 			 */
4073789Sahrens 			return (0);
4074789Sahrens 
4075789Sahrens 		default:
40763237Slling 			return (zfs_standard_error_fmt(hdl, errno,
40772082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
40782082Seschrock 			    "links for '%s'"), dataset));
4079789Sahrens 		}
4080789Sahrens 	}
4081789Sahrens 
4082789Sahrens 	return (0);
4083789Sahrens }
40842676Seschrock 
40852676Seschrock nvlist_t *
40862676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
40872676Seschrock {
40882676Seschrock 	return (zhp->zfs_user_props);
40892676Seschrock }
40902676Seschrock 
40912676Seschrock /*
40923912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
40933912Slling  * display, and their maximum widths.  This does two main things:
40943912Slling  *
40953912Slling  *      - If this is a list of all properties, then expand the list to include
40963912Slling  *        all native properties, and set a flag so that for each dataset we look
40973912Slling  *        for new unique user properties and add them to the list.
40983912Slling  *
40993912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
41003912Slling  *        so that we can size the column appropriately.
41013912Slling  */
41023912Slling int
41035094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
41043912Slling {
41053912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
41065094Slling 	zprop_list_t *entry;
41075094Slling 	zprop_list_t **last, **start;
41083912Slling 	nvlist_t *userprops, *propval;
41093912Slling 	nvpair_t *elem;
41103912Slling 	char *strval;
41113912Slling 	char buf[ZFS_MAXPROPLEN];
41123912Slling 
41135094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
41143912Slling 		return (-1);
41152676Seschrock 
41162676Seschrock 	userprops = zfs_get_user_props(zhp);
41172676Seschrock 
41182676Seschrock 	entry = *plp;
41192676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
41202676Seschrock 		/*
41212676Seschrock 		 * Go through and add any user properties as necessary.  We
41222676Seschrock 		 * start by incrementing our list pointer to the first
41232676Seschrock 		 * non-native property.
41242676Seschrock 		 */
41252676Seschrock 		start = plp;
41262676Seschrock 		while (*start != NULL) {
41275094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
41282676Seschrock 				break;
41292676Seschrock 			start = &(*start)->pl_next;
41302676Seschrock 		}
41312676Seschrock 
41322676Seschrock 		elem = NULL;
41332676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
41342676Seschrock 			/*
41352676Seschrock 			 * See if we've already found this property in our list.
41362676Seschrock 			 */
41372676Seschrock 			for (last = start; *last != NULL;
41382676Seschrock 			    last = &(*last)->pl_next) {
41392676Seschrock 				if (strcmp((*last)->pl_user_prop,
41402676Seschrock 				    nvpair_name(elem)) == 0)
41412676Seschrock 					break;
41422676Seschrock 			}
41432676Seschrock 
41442676Seschrock 			if (*last == NULL) {
41452676Seschrock 				if ((entry = zfs_alloc(hdl,
41465094Slling 				    sizeof (zprop_list_t))) == NULL ||
41472676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
41482676Seschrock 				    nvpair_name(elem)))) == NULL) {
41492676Seschrock 					free(entry);
41502676Seschrock 					return (-1);
41512676Seschrock 				}
41522676Seschrock 
41535094Slling 				entry->pl_prop = ZPROP_INVAL;
41542676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
41552676Seschrock 				entry->pl_all = B_TRUE;
41562676Seschrock 				*last = entry;
41572676Seschrock 			}
41582676Seschrock 		}
41592676Seschrock 	}
41602676Seschrock 
41612676Seschrock 	/*
41622676Seschrock 	 * Now go through and check the width of any non-fixed columns
41632676Seschrock 	 */
41642676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
41652676Seschrock 		if (entry->pl_fixed)
41662676Seschrock 			continue;
41672676Seschrock 
41685094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
41692676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
41702676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
41712676Seschrock 				if (strlen(buf) > entry->pl_width)
41722676Seschrock 					entry->pl_width = strlen(buf);
41732676Seschrock 			}
41742676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
41752676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
41762676Seschrock 			verify(nvlist_lookup_string(propval,
41775094Slling 			    ZPROP_VALUE, &strval) == 0);
41782676Seschrock 			if (strlen(strval) > entry->pl_width)
41792676Seschrock 				entry->pl_width = strlen(strval);
41802676Seschrock 		}
41812676Seschrock 	}
41822676Seschrock 
41832676Seschrock 	return (0);
41842676Seschrock }
41854543Smarks 
41864543Smarks int
41874543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
41884543Smarks {
41894543Smarks 	zfs_cmd_t zc = { 0 };
41904543Smarks 	nvlist_t *nvp;
41914543Smarks 	gid_t gid;
41924543Smarks 	uid_t uid;
41934543Smarks 	const gid_t *groups;
41944543Smarks 	int group_cnt;
41954543Smarks 	int error;
41964543Smarks 
41974543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
41984543Smarks 		return (no_memory(hdl));
41994543Smarks 
42004543Smarks 	uid = ucred_geteuid(cred);
42014543Smarks 	gid = ucred_getegid(cred);
42024543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
42034543Smarks 
42044543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
42054543Smarks 		return (1);
42064543Smarks 
42074543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
42084543Smarks 		nvlist_free(nvp);
42094543Smarks 		return (1);
42104543Smarks 	}
42114543Smarks 
42124543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
42134543Smarks 		nvlist_free(nvp);
42144543Smarks 		return (1);
42154543Smarks 	}
42164543Smarks 
42174543Smarks 	if (nvlist_add_uint32_array(nvp,
42184543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
42194543Smarks 		nvlist_free(nvp);
42204543Smarks 		return (1);
42214543Smarks 	}
42224543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42234543Smarks 
42245094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
42254543Smarks 		return (-1);
42264543Smarks 
42274543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
42284543Smarks 	nvlist_free(nvp);
42294543Smarks 	return (error);
42304543Smarks }
42314543Smarks 
42324543Smarks int
42334543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
42345331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
42354543Smarks {
42364543Smarks 	zfs_cmd_t zc = { 0 };
42374543Smarks 	int error;
42384543Smarks 
42394543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42404543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
42414543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
42424543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
42435331Samw 	zc.zc_share.z_sharetype = operation;
42444543Smarks 	zc.zc_share.z_sharemax = sharemax;
42454543Smarks 
42464543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
42474543Smarks 	return (error);
42484543Smarks }
4249