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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens #include <assert.h>
30789Sahrens #include <ctype.h>
31789Sahrens #include <errno.h>
32789Sahrens #include <libdevinfo.h>
33789Sahrens #include <libintl.h>
34789Sahrens #include <math.h>
35789Sahrens #include <stdio.h>
36789Sahrens #include <stdlib.h>
37789Sahrens #include <strings.h>
38789Sahrens #include <unistd.h>
395367Sahrens #include <stddef.h>
40789Sahrens #include <zone.h>
412082Seschrock #include <fcntl.h>
42789Sahrens #include <sys/mntent.h>
43789Sahrens #include <sys/mnttab.h>
441294Slling #include <sys/mount.h>
454543Smarks #include <sys/avl.h>
464543Smarks #include <priv.h>
474543Smarks #include <pwd.h>
484543Smarks #include <grp.h>
494543Smarks #include <stddef.h>
504543Smarks #include <ucred.h>
51789Sahrens 
52789Sahrens #include <sys/spa.h>
532676Seschrock #include <sys/zap.h>
54789Sahrens #include <libzfs.h>
55789Sahrens 
56789Sahrens #include "zfs_namecheck.h"
57789Sahrens #include "zfs_prop.h"
58789Sahrens #include "libzfs_impl.h"
594543Smarks #include "zfs_deleg.h"
60789Sahrens 
614007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
624007Smmusante 
63789Sahrens /*
64789Sahrens  * Given a single type (not a mask of types), return the type in a human
65789Sahrens  * readable form.
66789Sahrens  */
67789Sahrens const char *
68789Sahrens zfs_type_to_name(zfs_type_t type)
69789Sahrens {
70789Sahrens 	switch (type) {
71789Sahrens 	case ZFS_TYPE_FILESYSTEM:
72789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
73789Sahrens 	case ZFS_TYPE_SNAPSHOT:
74789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
75789Sahrens 	case ZFS_TYPE_VOLUME:
76789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
77789Sahrens 	}
78789Sahrens 
79789Sahrens 	return (NULL);
80789Sahrens }
81789Sahrens 
82789Sahrens /*
83789Sahrens  * Given a path and mask of ZFS types, return a string describing this dataset.
84789Sahrens  * This is used when we fail to open a dataset and we cannot get an exact type.
85789Sahrens  * We guess what the type would have been based on the path and the mask of
86789Sahrens  * acceptable types.
87789Sahrens  */
88789Sahrens static const char *
89789Sahrens path_to_str(const char *path, int types)
90789Sahrens {
91789Sahrens 	/*
92789Sahrens 	 * When given a single type, always report the exact type.
93789Sahrens 	 */
94789Sahrens 	if (types == ZFS_TYPE_SNAPSHOT)
95789Sahrens 		return (dgettext(TEXT_DOMAIN, "snapshot"));
96789Sahrens 	if (types == ZFS_TYPE_FILESYSTEM)
97789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
98789Sahrens 	if (types == ZFS_TYPE_VOLUME)
99789Sahrens 		return (dgettext(TEXT_DOMAIN, "volume"));
100789Sahrens 
101789Sahrens 	/*
102789Sahrens 	 * The user is requesting more than one type of dataset.  If this is the
103789Sahrens 	 * case, consult the path itself.  If we're looking for a snapshot, and
104789Sahrens 	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
105789Sahrens 	 * snapshot attribute and try again.
106789Sahrens 	 */
107789Sahrens 	if (types & ZFS_TYPE_SNAPSHOT) {
108789Sahrens 		if (strchr(path, '@') != NULL)
109789Sahrens 			return (dgettext(TEXT_DOMAIN, "snapshot"));
110789Sahrens 		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111789Sahrens 	}
112789Sahrens 
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * The user has requested either filesystems or volumes.
116789Sahrens 	 * We have no way of knowing a priori what type this would be, so always
117789Sahrens 	 * report it as "filesystem" or "volume", our two primitive types.
118789Sahrens 	 */
119789Sahrens 	if (types & ZFS_TYPE_FILESYSTEM)
120789Sahrens 		return (dgettext(TEXT_DOMAIN, "filesystem"));
121789Sahrens 
122789Sahrens 	assert(types & ZFS_TYPE_VOLUME);
123789Sahrens 	return (dgettext(TEXT_DOMAIN, "volume"));
124789Sahrens }
125789Sahrens 
126789Sahrens /*
127789Sahrens  * Validate a ZFS path.  This is used even before trying to open the dataset, to
128789Sahrens  * provide a more meaningful error message.  We place a more useful message in
129789Sahrens  * 'buf' detailing exactly why the name was not valid.
130789Sahrens  */
131789Sahrens static int
1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
1335326Sek110237     boolean_t modifying)
134789Sahrens {
135789Sahrens 	namecheck_err_t why;
136789Sahrens 	char what;
137789Sahrens 
138789Sahrens 	if (dataset_namecheck(path, &why, &what) != 0) {
1392082Seschrock 		if (hdl != NULL) {
140789Sahrens 			switch (why) {
1411003Slling 			case NAME_ERR_TOOLONG:
1422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432082Seschrock 				    "name is too long"));
1441003Slling 				break;
1451003Slling 
146789Sahrens 			case NAME_ERR_LEADING_SLASH:
1472082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482082Seschrock 				    "leading slash in name"));
149789Sahrens 				break;
150789Sahrens 
151789Sahrens 			case NAME_ERR_EMPTY_COMPONENT:
1522082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1532082Seschrock 				    "empty component in name"));
154789Sahrens 				break;
155789Sahrens 
156789Sahrens 			case NAME_ERR_TRAILING_SLASH:
1572082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582082Seschrock 				    "trailing slash in name"));
159789Sahrens 				break;
160789Sahrens 
161789Sahrens 			case NAME_ERR_INVALCHAR:
1622082Seschrock 				zfs_error_aux(hdl,
163789Sahrens 				    dgettext(TEXT_DOMAIN, "invalid character "
1642082Seschrock 				    "'%c' in name"), what);
165789Sahrens 				break;
166789Sahrens 
167789Sahrens 			case NAME_ERR_MULTIPLE_AT:
1682082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692082Seschrock 				    "multiple '@' delimiters in name"));
170789Sahrens 				break;
1712856Snd150628 
1722856Snd150628 			case NAME_ERR_NOLETTER:
1732856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1742856Snd150628 				    "pool doesn't begin with a letter"));
1752856Snd150628 				break;
1762856Snd150628 
1772856Snd150628 			case NAME_ERR_RESERVED:
1782856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1792856Snd150628 				    "name is reserved"));
1802856Snd150628 				break;
1812856Snd150628 
1822856Snd150628 			case NAME_ERR_DISKLIKE:
1832856Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1842856Snd150628 				    "reserved disk name"));
1852856Snd150628 				break;
186789Sahrens 			}
187789Sahrens 		}
188789Sahrens 
189789Sahrens 		return (0);
190789Sahrens 	}
191789Sahrens 
192789Sahrens 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
1932082Seschrock 		if (hdl != NULL)
1942082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1952082Seschrock 			    "snapshot delimiter '@' in filesystem name"));
196789Sahrens 		return (0);
197789Sahrens 	}
198789Sahrens 
1992199Sahrens 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
2002199Sahrens 		if (hdl != NULL)
2012199Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2023413Smmusante 			    "missing '@' delimiter in snapshot name"));
2032199Sahrens 		return (0);
2042199Sahrens 	}
2052199Sahrens 
2065326Sek110237 	if (modifying && strchr(path, '%') != NULL) {
2075326Sek110237 		if (hdl != NULL)
2085326Sek110237 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2095326Sek110237 			    "invalid character %c in name"), '%');
2105326Sek110237 		return (0);
2115326Sek110237 	}
2125326Sek110237 
2132082Seschrock 	return (-1);
214789Sahrens }
215789Sahrens 
216789Sahrens int
217789Sahrens zfs_name_valid(const char *name, zfs_type_t type)
218789Sahrens {
2196423Sgw25295 	if (type == ZFS_TYPE_POOL)
2206423Sgw25295 		return (zpool_name_valid(NULL, B_FALSE, name));
2215326Sek110237 	return (zfs_validate_name(NULL, name, type, B_FALSE));
222789Sahrens }
223789Sahrens 
224789Sahrens /*
2252676Seschrock  * This function takes the raw DSL properties, and filters out the user-defined
2262676Seschrock  * properties into a separate nvlist.
2272676Seschrock  */
2284217Seschrock static nvlist_t *
2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2302676Seschrock {
2312676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2322676Seschrock 	nvpair_t *elem;
2332676Seschrock 	nvlist_t *propval;
2344217Seschrock 	nvlist_t *nvl;
2354217Seschrock 
2364217Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2374217Seschrock 		(void) no_memory(hdl);
2384217Seschrock 		return (NULL);
2394217Seschrock 	}
2402676Seschrock 
2412676Seschrock 	elem = NULL;
2424217Seschrock 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2432676Seschrock 		if (!zfs_prop_user(nvpair_name(elem)))
2442676Seschrock 			continue;
2452676Seschrock 
2462676Seschrock 		verify(nvpair_value_nvlist(elem, &propval) == 0);
2474217Seschrock 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
2484217Seschrock 			nvlist_free(nvl);
2494217Seschrock 			(void) no_memory(hdl);
2504217Seschrock 			return (NULL);
2514217Seschrock 		}
2522676Seschrock 	}
2532676Seschrock 
2544217Seschrock 	return (nvl);
2552676Seschrock }
2562676Seschrock 
2576865Srm160521 static zpool_handle_t *
2586865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
2596865Srm160521 {
2606865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2616865Srm160521 	zpool_handle_t *zph;
2626865Srm160521 
2636865Srm160521 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
2646865Srm160521 		if (hdl->libzfs_pool_handles != NULL)
2656865Srm160521 			zph->zpool_next = hdl->libzfs_pool_handles;
2666865Srm160521 		hdl->libzfs_pool_handles = zph;
2676865Srm160521 	}
2686865Srm160521 	return (zph);
2696865Srm160521 }
2706865Srm160521 
2716865Srm160521 static zpool_handle_t *
2726865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
2736865Srm160521 {
2746865Srm160521 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2756865Srm160521 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
2766865Srm160521 
2776865Srm160521 	while ((zph != NULL) &&
2786865Srm160521 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
2796865Srm160521 		zph = zph->zpool_next;
2806865Srm160521 	return (zph);
2816865Srm160521 }
2826865Srm160521 
2836865Srm160521 /*
2846865Srm160521  * Returns a handle to the pool that contains the provided dataset.
2856865Srm160521  * If a handle to that pool already exists then that handle is returned.
2866865Srm160521  * Otherwise, a new handle is created and added to the list of handles.
2876865Srm160521  */
2886865Srm160521 static zpool_handle_t *
2896865Srm160521 zpool_handle(zfs_handle_t *zhp)
2906865Srm160521 {
2916865Srm160521 	char *pool_name;
2926865Srm160521 	int len;
2936865Srm160521 	zpool_handle_t *zph;
2946865Srm160521 
2956865Srm160521 	len = strcspn(zhp->zfs_name, "/@") + 1;
2966865Srm160521 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
2976865Srm160521 	(void) strlcpy(pool_name, zhp->zfs_name, len);
2986865Srm160521 
2996865Srm160521 	zph = zpool_find_handle(zhp, pool_name, len);
3006865Srm160521 	if (zph == NULL)
3016865Srm160521 		zph = zpool_add_handle(zhp, pool_name);
3026865Srm160521 
3036865Srm160521 	free(pool_name);
3046865Srm160521 	return (zph);
3056865Srm160521 }
3066865Srm160521 
3076865Srm160521 void
3086865Srm160521 zpool_free_handles(libzfs_handle_t *hdl)
3096865Srm160521 {
3106865Srm160521 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
3116865Srm160521 
3126865Srm160521 	while (zph != NULL) {
3136865Srm160521 		next = zph->zpool_next;
3146865Srm160521 		zpool_close(zph);
3156865Srm160521 		zph = next;
3166865Srm160521 	}
3176865Srm160521 	hdl->libzfs_pool_handles = NULL;
3186865Srm160521 }
3196865Srm160521 
3202676Seschrock /*
321789Sahrens  * Utility function to gather stats (objset and zpl) for the given object.
322789Sahrens  */
323789Sahrens static int
324789Sahrens get_stats(zfs_handle_t *zhp)
325789Sahrens {
326789Sahrens 	zfs_cmd_t zc = { 0 };
3272676Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3284217Seschrock 	nvlist_t *allprops, *userprops;
329789Sahrens 
330789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
331789Sahrens 
3322676Seschrock 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
3332082Seschrock 		return (-1);
3341356Seschrock 
3352082Seschrock 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
3361356Seschrock 		if (errno == ENOMEM) {
3372676Seschrock 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3382676Seschrock 				zcmd_free_nvlists(&zc);
3392082Seschrock 				return (-1);
3402676Seschrock 			}
3411356Seschrock 		} else {
3422676Seschrock 			zcmd_free_nvlists(&zc);
3431356Seschrock 			return (-1);
3441356Seschrock 		}
3451356Seschrock 	}
346789Sahrens 
3472885Sahrens 	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
348789Sahrens 
3494217Seschrock 	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
3502676Seschrock 		zcmd_free_nvlists(&zc);
3512082Seschrock 		return (-1);
3522082Seschrock 	}
353789Sahrens 
3542676Seschrock 	zcmd_free_nvlists(&zc);
3552676Seschrock 
3564217Seschrock 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3574217Seschrock 		nvlist_free(allprops);
3582676Seschrock 		return (-1);
3594217Seschrock 	}
3604217Seschrock 
3614217Seschrock 	nvlist_free(zhp->zfs_props);
3624217Seschrock 	nvlist_free(zhp->zfs_user_props);
3634217Seschrock 
3644217Seschrock 	zhp->zfs_props = allprops;
3654217Seschrock 	zhp->zfs_user_props = userprops;
3662082Seschrock 
367789Sahrens 	return (0);
368789Sahrens }
369789Sahrens 
370789Sahrens /*
371789Sahrens  * Refresh the properties currently stored in the handle.
372789Sahrens  */
373789Sahrens void
374789Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
375789Sahrens {
376789Sahrens 	(void) get_stats(zhp);
377789Sahrens }
378789Sahrens 
379789Sahrens /*
380789Sahrens  * Makes a handle from the given dataset name.  Used by zfs_open() and
381789Sahrens  * zfs_iter_* to create child handles on the fly.
382789Sahrens  */
383789Sahrens zfs_handle_t *
3842082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path)
385789Sahrens {
3862082Seschrock 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
3874543Smarks 	char *logstr;
3882082Seschrock 
3892082Seschrock 	if (zhp == NULL)
3902082Seschrock 		return (NULL);
3912082Seschrock 
3922082Seschrock 	zhp->zfs_hdl = hdl;
393789Sahrens 
3944543Smarks 	/*
3954543Smarks 	 * Preserve history log string.
3964543Smarks 	 * any changes performed here will be
3974543Smarks 	 * logged as an internal event.
3984543Smarks 	 */
3994543Smarks 	logstr = zhp->zfs_hdl->libzfs_log_str;
4004543Smarks 	zhp->zfs_hdl->libzfs_log_str = NULL;
4011758Sahrens top:
402789Sahrens 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
403789Sahrens 
404789Sahrens 	if (get_stats(zhp) != 0) {
4054543Smarks 		zhp->zfs_hdl->libzfs_log_str = logstr;
406789Sahrens 		free(zhp);
407789Sahrens 		return (NULL);
408789Sahrens 	}
409789Sahrens 
4101758Sahrens 	if (zhp->zfs_dmustats.dds_inconsistent) {
4111758Sahrens 		zfs_cmd_t zc = { 0 };
4121758Sahrens 
4131758Sahrens 		/*
4141758Sahrens 		 * If it is dds_inconsistent, then we've caught it in
4151758Sahrens 		 * the middle of a 'zfs receive' or 'zfs destroy', and
4161758Sahrens 		 * it is inconsistent from the ZPL's point of view, so
4171758Sahrens 		 * can't be mounted.  However, it could also be that we
4181758Sahrens 		 * have crashed in the middle of one of those
4191758Sahrens 		 * operations, in which case we need to get rid of the
4201758Sahrens 		 * inconsistent state.  We do that by either rolling
4211758Sahrens 		 * back to the previous snapshot (which will fail if
4221758Sahrens 		 * there is none), or destroying the filesystem.  Note
4231758Sahrens 		 * that if we are still in the middle of an active
4241758Sahrens 		 * 'receive' or 'destroy', then the rollback and destroy
4251758Sahrens 		 * will fail with EBUSY and we will drive on as usual.
4261758Sahrens 		 */
4271758Sahrens 
4281758Sahrens 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4291758Sahrens 
4302885Sahrens 		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
4312082Seschrock 			(void) zvol_remove_link(hdl, zhp->zfs_name);
4321758Sahrens 			zc.zc_objset_type = DMU_OST_ZVOL;
4331758Sahrens 		} else {
4341758Sahrens 			zc.zc_objset_type = DMU_OST_ZFS;
4351758Sahrens 		}
4361758Sahrens 
4371758Sahrens 		/*
4385331Samw 		 * If we can successfully destroy it, pretend that it
4391758Sahrens 		 * never existed.
4401758Sahrens 		 */
4412082Seschrock 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
4424543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
4431758Sahrens 			free(zhp);
4441758Sahrens 			errno = ENOENT;
4451758Sahrens 			return (NULL);
4461758Sahrens 		}
4475367Sahrens 		/* If we can successfully roll it back, reget the stats */
4485367Sahrens 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
4495367Sahrens 			goto top;
4501758Sahrens 	}
4511758Sahrens 
452789Sahrens 	/*
453789Sahrens 	 * We've managed to open the dataset and gather statistics.  Determine
454789Sahrens 	 * the high-level type.
455789Sahrens 	 */
4562885Sahrens 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
4572885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
4582885Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
4592885Sahrens 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
4602885Sahrens 	else
4612885Sahrens 		abort();
4622885Sahrens 
463789Sahrens 	if (zhp->zfs_dmustats.dds_is_snapshot)
464789Sahrens 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
465789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
466789Sahrens 		zhp->zfs_type = ZFS_TYPE_VOLUME;
467789Sahrens 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
468789Sahrens 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
469789Sahrens 	else
4702082Seschrock 		abort();	/* we should never see any other types */
471789Sahrens 
4724543Smarks 	zhp->zfs_hdl->libzfs_log_str = logstr;
4736865Srm160521 	zhp->zpool_hdl = zpool_handle(zhp);
474789Sahrens 	return (zhp);
475789Sahrens }
476789Sahrens 
477789Sahrens /*
478789Sahrens  * Opens the given snapshot, filesystem, or volume.   The 'types'
479789Sahrens  * argument is a mask of acceptable types.  The function will print an
480789Sahrens  * appropriate error message and return NULL if it can't be opened.
481789Sahrens  */
482789Sahrens zfs_handle_t *
4832082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
484789Sahrens {
485789Sahrens 	zfs_handle_t *zhp;
4862082Seschrock 	char errbuf[1024];
4872082Seschrock 
4882082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
4892082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
490789Sahrens 
491789Sahrens 	/*
4922082Seschrock 	 * Validate the name before we even try to open it.
493789Sahrens 	 */
4945326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
4952082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4962082Seschrock 		    "invalid dataset name"));
4972082Seschrock 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
498789Sahrens 		return (NULL);
499789Sahrens 	}
500789Sahrens 
501789Sahrens 	/*
502789Sahrens 	 * Try to get stats for the dataset, which will tell us if it exists.
503789Sahrens 	 */
504789Sahrens 	errno = 0;
5052082Seschrock 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5063237Slling 		(void) zfs_standard_error(hdl, errno, errbuf);
507789Sahrens 		return (NULL);
508789Sahrens 	}
509789Sahrens 
510789Sahrens 	if (!(types & zhp->zfs_type)) {
5112082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5122142Seschrock 		zfs_close(zhp);
513789Sahrens 		return (NULL);
514789Sahrens 	}
515789Sahrens 
516789Sahrens 	return (zhp);
517789Sahrens }
518789Sahrens 
519789Sahrens /*
520789Sahrens  * Release a ZFS handle.  Nothing to do but free the associated memory.
521789Sahrens  */
522789Sahrens void
523789Sahrens zfs_close(zfs_handle_t *zhp)
524789Sahrens {
525789Sahrens 	if (zhp->zfs_mntopts)
526789Sahrens 		free(zhp->zfs_mntopts);
5272676Seschrock 	nvlist_free(zhp->zfs_props);
5282676Seschrock 	nvlist_free(zhp->zfs_user_props);
529789Sahrens 	free(zhp);
530789Sahrens }
531789Sahrens 
5325713Srm160521 int
5335713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
5345713Srm160521 {
5356865Srm160521 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
5366865Srm160521 
5375713Srm160521 	if (zpool_handle == NULL)
5385713Srm160521 		return (-1);
5395713Srm160521 
5405713Srm160521 	*spa_version = zpool_get_prop_int(zpool_handle,
5415713Srm160521 	    ZPOOL_PROP_VERSION, NULL);
5425713Srm160521 	return (0);
5435713Srm160521 }
5445713Srm160521 
5455713Srm160521 /*
5465713Srm160521  * The choice of reservation property depends on the SPA version.
5475713Srm160521  */
5485713Srm160521 static int
5495713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
5505713Srm160521 {
5515713Srm160521 	int spa_version;
5525713Srm160521 
5535713Srm160521 	if (zfs_spa_version(zhp, &spa_version) < 0)
5545713Srm160521 		return (-1);
5555713Srm160521 
5565713Srm160521 	if (spa_version >= SPA_VERSION_REFRESERVATION)
5575713Srm160521 		*resv_prop = ZFS_PROP_REFRESERVATION;
5585713Srm160521 	else
5595713Srm160521 		*resv_prop = ZFS_PROP_RESERVATION;
5605713Srm160521 
5615713Srm160521 	return (0);
5625713Srm160521 }
5635713Srm160521 
5643912Slling /*
5652676Seschrock  * Given an nvlist of properties to set, validates that they are correct, and
5662676Seschrock  * parses any numeric properties (index, boolean, etc) if they are specified as
5672676Seschrock  * strings.
568789Sahrens  */
569*7184Stimh nvlist_t *
570*7184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
5715094Slling     uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
572789Sahrens {
5732676Seschrock 	nvpair_t *elem;
5742676Seschrock 	uint64_t intval;
5752676Seschrock 	char *strval;
5765094Slling 	zfs_prop_t prop;
5772676Seschrock 	nvlist_t *ret;
5785331Samw 	int chosen_normal = -1;
5795331Samw 	int chosen_utf = -1;
5802676Seschrock 
5812676Seschrock 	if (type == ZFS_TYPE_SNAPSHOT) {
5822676Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5833413Smmusante 		    "snapshot properties cannot be modified"));
5842676Seschrock 		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5855094Slling 		return (NULL);
5865094Slling 	}
5875094Slling 
5885094Slling 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5895094Slling 		(void) no_memory(hdl);
5905094Slling 		return (NULL);
591789Sahrens 	}
592789Sahrens 
5932676Seschrock 	elem = NULL;
5942676Seschrock 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5955094Slling 		const char *propname = nvpair_name(elem);
5962676Seschrock 
5972676Seschrock 		/*
5982676Seschrock 		 * Make sure this property is valid and applies to this type.
5992676Seschrock 		 */
6005094Slling 		if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
6015094Slling 			if (!zfs_prop_user(propname)) {
6022676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6035094Slling 				    "invalid property '%s'"), propname);
6045094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6055094Slling 				goto error;
6065094Slling 			}
6075094Slling 
6085094Slling 			/*
6095094Slling 			 * If this is a user property, make sure it's a
6105094Slling 			 * string, and that it's less than ZAP_MAXNAMELEN.
6115094Slling 			 */
6125094Slling 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
6135094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6145094Slling 				    "'%s' must be a string"), propname);
6155094Slling 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6165094Slling 				goto error;
6175094Slling 			}
6185094Slling 
6195094Slling 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
6205094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6215094Slling 				    "property name '%s' is too long"),
6222676Seschrock 				    propname);
6232676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6242676Seschrock 				goto error;
6252676Seschrock 			}
6262676Seschrock 
6272676Seschrock 			(void) nvpair_value_string(elem, &strval);
6282676Seschrock 			if (nvlist_add_string(ret, propname, strval) != 0) {
6292676Seschrock 				(void) no_memory(hdl);
6302676Seschrock 				goto error;
6312676Seschrock 			}
6322676Seschrock 			continue;
633789Sahrens 		}
6342676Seschrock 
6352676Seschrock 		if (!zfs_prop_valid_for_type(prop, type)) {
6362676Seschrock 			zfs_error_aux(hdl,
6372676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' does not "
6382676Seschrock 			    "apply to datasets of this type"), propname);
6392676Seschrock 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
6402676Seschrock 			goto error;
6412676Seschrock 		}
6422676Seschrock 
6432676Seschrock 		if (zfs_prop_readonly(prop) &&
6445331Samw 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
6452676Seschrock 			zfs_error_aux(hdl,
6462676Seschrock 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
6472676Seschrock 			    propname);
6482676Seschrock 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
6492676Seschrock 			goto error;
6502676Seschrock 		}
6512676Seschrock 
6525094Slling 		if (zprop_parse_value(hdl, elem, prop, type, ret,
6535094Slling 		    &strval, &intval, errbuf) != 0)
6542676Seschrock 			goto error;
6552676Seschrock 
6562676Seschrock 		/*
6572676Seschrock 		 * Perform some additional checks for specific properties.
6582676Seschrock 		 */
6592676Seschrock 		switch (prop) {
6604577Sahrens 		case ZFS_PROP_VERSION:
6614577Sahrens 		{
6624577Sahrens 			int version;
6634577Sahrens 
6644577Sahrens 			if (zhp == NULL)
6654577Sahrens 				break;
6664577Sahrens 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
6674577Sahrens 			if (intval < version) {
6684577Sahrens 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6694577Sahrens 				    "Can not downgrade; already at version %u"),
6704577Sahrens 				    version);
6714577Sahrens 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6724577Sahrens 				goto error;
6734577Sahrens 			}
6744577Sahrens 			break;
6754577Sahrens 		}
6764577Sahrens 
6772676Seschrock 		case ZFS_PROP_RECORDSIZE:
6782676Seschrock 		case ZFS_PROP_VOLBLOCKSIZE:
6792676Seschrock 			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
6802676Seschrock 			if (intval < SPA_MINBLOCKSIZE ||
6812676Seschrock 			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
6822082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6832676Seschrock 				    "'%s' must be power of 2 from %u "
6842676Seschrock 				    "to %uk"), propname,
6852676Seschrock 				    (uint_t)SPA_MINBLOCKSIZE,
6862676Seschrock 				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
6872676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6882676Seschrock 				goto error;
689789Sahrens 			}
690789Sahrens 			break;
691789Sahrens 
6923126Sahl 		case ZFS_PROP_SHAREISCSI:
6933126Sahl 			if (strcmp(strval, "off") != 0 &&
6943126Sahl 			    strcmp(strval, "on") != 0 &&
6953126Sahl 			    strcmp(strval, "type=disk") != 0) {
6963126Sahl 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6973126Sahl 				    "'%s' must be 'on', 'off', or 'type=disk'"),
6983126Sahl 				    propname);
6993126Sahl 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7003126Sahl 				goto error;
7013126Sahl 			}
7023126Sahl 
7033126Sahl 			break;
7043126Sahl 
7052676Seschrock 		case ZFS_PROP_MOUNTPOINT:
7064778Srm160521 		{
7074778Srm160521 			namecheck_err_t why;
7084778Srm160521 
7092676Seschrock 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
7102676Seschrock 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
7112676Seschrock 				break;
7122676Seschrock 
7134778Srm160521 			if (mountpoint_namecheck(strval, &why)) {
7144778Srm160521 				switch (why) {
7154778Srm160521 				case NAME_ERR_LEADING_SLASH:
7164778Srm160521 					zfs_error_aux(hdl,
7174778Srm160521 					    dgettext(TEXT_DOMAIN,
7184778Srm160521 					    "'%s' must be an absolute path, "
7194778Srm160521 					    "'none', or 'legacy'"), propname);
7204778Srm160521 					break;
7214778Srm160521 				case NAME_ERR_TOOLONG:
7224778Srm160521 					zfs_error_aux(hdl,
7234778Srm160521 					    dgettext(TEXT_DOMAIN,
7244778Srm160521 					    "component of '%s' is too long"),
7254778Srm160521 					    propname);
7264778Srm160521 					break;
7274778Srm160521 				}
7282676Seschrock 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
7292676Seschrock 				goto error;
730789Sahrens 			}
7314778Srm160521 		}
7324778Srm160521 
7333126Sahl 			/*FALLTHRU*/
7343126Sahl 
7355331Samw 		case ZFS_PROP_SHARESMB:
7363126Sahl 		case ZFS_PROP_SHARENFS:
7373126Sahl 			/*
7385331Samw 			 * For the mountpoint and sharenfs or sharesmb
7395331Samw 			 * properties, check if it can be set in a
7405331Samw 			 * global/non-global zone based on
7413126Sahl 			 * the zoned property value:
7423126Sahl 			 *
7433126Sahl 			 *		global zone	    non-global zone
7443126Sahl 			 * --------------------------------------------------
7453126Sahl 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
7463126Sahl 			 *		sharenfs (no)	    sharenfs (no)
7475331Samw 			 *		sharesmb (no)	    sharesmb (no)
7483126Sahl 			 *
7493126Sahl 			 * zoned=off	mountpoint (yes)	N/A
7503126Sahl 			 *		sharenfs (yes)
7515331Samw 			 *		sharesmb (yes)
7523126Sahl 			 */
7532676Seschrock 			if (zoned) {
7542676Seschrock 				if (getzoneid() == GLOBAL_ZONEID) {
7552676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7562676Seschrock 					    "'%s' cannot be set on "
7572676Seschrock 					    "dataset in a non-global zone"),
7582676Seschrock 					    propname);
7592676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7602676Seschrock 					    errbuf);
7612676Seschrock 					goto error;
7625331Samw 				} else if (prop == ZFS_PROP_SHARENFS ||
7635331Samw 				    prop == ZFS_PROP_SHARESMB) {
7642676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7652676Seschrock 					    "'%s' cannot be set in "
7662676Seschrock 					    "a non-global zone"), propname);
7672676Seschrock 					(void) zfs_error(hdl, EZFS_ZONED,
7682676Seschrock 					    errbuf);
7692676Seschrock 					goto error;
7702676Seschrock 				}
7712676Seschrock 			} else if (getzoneid() != GLOBAL_ZONEID) {
7722676Seschrock 				/*
7732676Seschrock 				 * If zoned property is 'off', this must be in
7742676Seschrock 				 * a globle zone. If not, something is wrong.
7752676Seschrock 				 */
7762676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7772676Seschrock 				    "'%s' cannot be set while dataset "
7782676Seschrock 				    "'zoned' property is set"), propname);
7792676Seschrock 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
7802676Seschrock 				goto error;
7812676Seschrock 			}
7823126Sahl 
7834180Sdougm 			/*
7844180Sdougm 			 * At this point, it is legitimate to set the
7854180Sdougm 			 * property. Now we want to make sure that the
7864180Sdougm 			 * property value is valid if it is sharenfs.
7874180Sdougm 			 */
7885331Samw 			if ((prop == ZFS_PROP_SHARENFS ||
7895331Samw 			    prop == ZFS_PROP_SHARESMB) &&
7904217Seschrock 			    strcmp(strval, "on") != 0 &&
7914217Seschrock 			    strcmp(strval, "off") != 0) {
7925331Samw 				zfs_share_proto_t proto;
7935331Samw 
7945331Samw 				if (prop == ZFS_PROP_SHARESMB)
7955331Samw 					proto = PROTO_SMB;
7965331Samw 				else
7975331Samw 					proto = PROTO_NFS;
7984180Sdougm 
7994180Sdougm 				/*
8005331Samw 				 * Must be an valid sharing protocol
8015331Samw 				 * option string so init the libshare
8025331Samw 				 * in order to enable the parser and
8035331Samw 				 * then parse the options. We use the
8045331Samw 				 * control API since we don't care about
8055331Samw 				 * the current configuration and don't
8064180Sdougm 				 * want the overhead of loading it
8074180Sdougm 				 * until we actually do something.
8084180Sdougm 				 */
8094180Sdougm 
8104217Seschrock 				if (zfs_init_libshare(hdl,
8114217Seschrock 				    SA_INIT_CONTROL_API) != SA_OK) {
8124217Seschrock 					/*
8134217Seschrock 					 * An error occurred so we can't do
8144217Seschrock 					 * anything
8154217Seschrock 					 */
8164217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8174217Seschrock 					    "'%s' cannot be set: problem "
8184217Seschrock 					    "in share initialization"),
8194217Seschrock 					    propname);
8204217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8214217Seschrock 					    errbuf);
8224217Seschrock 					goto error;
8234217Seschrock 				}
8244217Seschrock 
8255331Samw 				if (zfs_parse_options(strval, proto) != SA_OK) {
8264217Seschrock 					/*
8274217Seschrock 					 * There was an error in parsing so
8284217Seschrock 					 * deal with it by issuing an error
8294217Seschrock 					 * message and leaving after
8304217Seschrock 					 * uninitializing the the libshare
8314217Seschrock 					 * interface.
8324217Seschrock 					 */
8334217Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8344217Seschrock 					    "'%s' cannot be set to invalid "
8354217Seschrock 					    "options"), propname);
8364217Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8374217Seschrock 					    errbuf);
8384217Seschrock 					zfs_uninit_libshare(hdl);
8394217Seschrock 					goto error;
8404217Seschrock 				}
8414180Sdougm 				zfs_uninit_libshare(hdl);
8424180Sdougm 			}
8434180Sdougm 
8443126Sahl 			break;
8455331Samw 		case ZFS_PROP_UTF8ONLY:
8465331Samw 			chosen_utf = (int)intval;
8475331Samw 			break;
8485331Samw 		case ZFS_PROP_NORMALIZE:
8495331Samw 			chosen_normal = (int)intval;
8505331Samw 			break;
8512676Seschrock 		}
8522676Seschrock 
8532676Seschrock 		/*
8542676Seschrock 		 * For changes to existing volumes, we have some additional
8552676Seschrock 		 * checks to enforce.
8562676Seschrock 		 */
8572676Seschrock 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
8582676Seschrock 			uint64_t volsize = zfs_prop_get_int(zhp,
8592676Seschrock 			    ZFS_PROP_VOLSIZE);
8602676Seschrock 			uint64_t blocksize = zfs_prop_get_int(zhp,
8612676Seschrock 			    ZFS_PROP_VOLBLOCKSIZE);
8622676Seschrock 			char buf[64];
8632676Seschrock 
8642676Seschrock 			switch (prop) {
8652676Seschrock 			case ZFS_PROP_RESERVATION:
8665378Sck153898 			case ZFS_PROP_REFRESERVATION:
8672676Seschrock 				if (intval > volsize) {
8682676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8692676Seschrock 					    "'%s' is greater than current "
8702676Seschrock 					    "volume size"), propname);
8712676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8722676Seschrock 					    errbuf);
8732676Seschrock 					goto error;
8742676Seschrock 				}
8752676Seschrock 				break;
8762676Seschrock 
8772676Seschrock 			case ZFS_PROP_VOLSIZE:
8782676Seschrock 				if (intval % blocksize != 0) {
8792676Seschrock 					zfs_nicenum(blocksize, buf,
8802676Seschrock 					    sizeof (buf));
8812676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8822676Seschrock 					    "'%s' must be a multiple of "
8832676Seschrock 					    "volume block size (%s)"),
8842676Seschrock 					    propname, buf);
8852676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8862676Seschrock 					    errbuf);
8872676Seschrock 					goto error;
8882676Seschrock 				}
8892676Seschrock 
8902676Seschrock 				if (intval == 0) {
8912676Seschrock 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8922676Seschrock 					    "'%s' cannot be zero"),
8932676Seschrock 					    propname);
8942676Seschrock 					(void) zfs_error(hdl, EZFS_BADPROP,
8952676Seschrock 					    errbuf);
8962676Seschrock 					goto error;
897789Sahrens 				}
8983126Sahl 				break;
899789Sahrens 			}
900789Sahrens 		}
901789Sahrens 	}
902789Sahrens 
9032676Seschrock 	/*
9045331Samw 	 * If normalization was chosen, but no UTF8 choice was made,
9055331Samw 	 * enforce rejection of non-UTF8 names.
9065331Samw 	 *
9075331Samw 	 * If normalization was chosen, but rejecting non-UTF8 names
9085331Samw 	 * was explicitly not chosen, it is an error.
9095331Samw 	 */
9105498Stimh 	if (chosen_normal > 0 && chosen_utf < 0) {
9115331Samw 		if (nvlist_add_uint64(ret,
9125331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
9135331Samw 			(void) no_memory(hdl);
9145331Samw 			goto error;
9155331Samw 		}
9165498Stimh 	} else if (chosen_normal > 0 && chosen_utf == 0) {
9175331Samw 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9185331Samw 		    "'%s' must be set 'on' if normalization chosen"),
9195331Samw 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
9205331Samw 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
9215331Samw 		goto error;
9225331Samw 	}
9235331Samw 
9245331Samw 	/*
9252676Seschrock 	 * If this is an existing volume, and someone is setting the volsize,
9262676Seschrock 	 * make sure that it matches the reservation, or add it if necessary.
9272676Seschrock 	 */
9282676Seschrock 	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
9292676Seschrock 	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
9302676Seschrock 	    &intval) == 0) {
9312676Seschrock 		uint64_t old_volsize = zfs_prop_get_int(zhp,
9322676Seschrock 		    ZFS_PROP_VOLSIZE);
9335481Sck153898 		uint64_t old_reservation;
9342676Seschrock 		uint64_t new_reservation;
9355481Sck153898 		zfs_prop_t resv_prop;
9365713Srm160521 
9375713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
9385481Sck153898 			goto error;
9395481Sck153898 		old_reservation = zfs_prop_get_int(zhp, resv_prop);
9402676Seschrock 
9412676Seschrock 		if (old_volsize == old_reservation &&
9425481Sck153898 		    nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
9432676Seschrock 		    &new_reservation) != 0) {
9442676Seschrock 			if (nvlist_add_uint64(ret,
9455481Sck153898 			    zfs_prop_to_name(resv_prop), intval) != 0) {
9462676Seschrock 				(void) no_memory(hdl);
9472676Seschrock 				goto error;
9482676Seschrock 			}
9492676Seschrock 		}
9502676Seschrock 	}
9512676Seschrock 	return (ret);
9522676Seschrock 
9532676Seschrock error:
9542676Seschrock 	nvlist_free(ret);
9552676Seschrock 	return (NULL);
956789Sahrens }
957789Sahrens 
9584543Smarks static int
9594543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
9604543Smarks     uint64_t *ret_who)
9614543Smarks {
9624543Smarks 	struct passwd *pwd;
9634543Smarks 	struct group *grp;
9644543Smarks 	uid_t id;
9654543Smarks 
9664543Smarks 	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
9674543Smarks 	    *who_type == ZFS_DELEG_NAMED_SET) {
9684543Smarks 		*ret_who = -1;
9694543Smarks 		return (0);
9704543Smarks 	}
9714543Smarks 	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
9724543Smarks 		return (EZFS_BADWHO);
9734543Smarks 
9744543Smarks 	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
9754543Smarks 	    strcmp(who, "everyone") == 0) {
9764543Smarks 		*ret_who = -1;
9774543Smarks 		*who_type = ZFS_DELEG_EVERYONE;
9784543Smarks 		return (0);
9794543Smarks 	}
9804543Smarks 
9814543Smarks 	pwd = getpwnam(who);
9824543Smarks 	grp = getgrnam(who);
9834543Smarks 
9844543Smarks 	if ((*who_type == ZFS_DELEG_USER) && pwd) {
9854543Smarks 		*ret_who = pwd->pw_uid;
9864543Smarks 	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
9874543Smarks 		*ret_who = grp->gr_gid;
9884543Smarks 	} else if (pwd) {
9894543Smarks 		*ret_who = pwd->pw_uid;
9904543Smarks 		*who_type = ZFS_DELEG_USER;
9914543Smarks 	} else if (grp) {
9924543Smarks 		*ret_who = grp->gr_gid;
9934543Smarks 		*who_type = ZFS_DELEG_GROUP;
9944543Smarks 	} else {
9954543Smarks 		char *end;
9964543Smarks 
9974543Smarks 		id = strtol(who, &end, 10);
9984543Smarks 		if (errno != 0 || *end != '\0') {
9994543Smarks 			return (EZFS_BADWHO);
10004543Smarks 		} else {
10014543Smarks 			*ret_who = id;
10024543Smarks 			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
10034543Smarks 				*who_type = ZFS_DELEG_USER;
10044543Smarks 		}
10054543Smarks 	}
10064543Smarks 
10074543Smarks 	return (0);
10084543Smarks }
10094543Smarks 
10104543Smarks static void
10114543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
10124543Smarks {
10134543Smarks 	if (perms_nvp != NULL) {
10144543Smarks 		verify(nvlist_add_nvlist(who_nvp,
10154543Smarks 		    name, perms_nvp) == 0);
10164543Smarks 	} else {
10174543Smarks 		verify(nvlist_add_boolean(who_nvp, name) == 0);
10184543Smarks 	}
10194543Smarks }
10204543Smarks 
10214543Smarks static void
10224543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
10234543Smarks     zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
10244543Smarks     nvlist_t *sets_nvp)
10254543Smarks {
10264543Smarks 	boolean_t do_perms, do_sets;
10274543Smarks 	char name[ZFS_MAX_DELEG_NAME];
10284543Smarks 
10294543Smarks 	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
10304543Smarks 	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
10314543Smarks 
10324543Smarks 	if (!do_perms && !do_sets)
10334543Smarks 		do_perms = do_sets = B_TRUE;
10344543Smarks 
10354543Smarks 	if (do_perms) {
10364543Smarks 		zfs_deleg_whokey(name, who_type, inherit,
10374543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10384543Smarks 		    whostr : (void *)&whoid);
10394543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
10404543Smarks 	}
10414543Smarks 	if (do_sets) {
10424543Smarks 		zfs_deleg_whokey(name, toupper(who_type), inherit,
10434543Smarks 		    (who_type == ZFS_DELEG_NAMED_SET) ?
10444543Smarks 		    whostr : (void *)&whoid);
10454543Smarks 		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
10464543Smarks 	}
10474543Smarks }
10484543Smarks 
10494543Smarks static void
10504543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
10514543Smarks     nvlist_t *perms_nvp, nvlist_t *sets_nvp,
10524543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
10534543Smarks {
10544543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
10554543Smarks 		helper(who_type, whoid, whostr, 0,
10564543Smarks 		    who_nvp, perms_nvp, sets_nvp);
10574543Smarks 	} else {
10584543Smarks 		if (inherit & ZFS_DELEG_PERM_LOCAL) {
10594543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
10604543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10614543Smarks 		}
10624543Smarks 		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
10634543Smarks 			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
10644543Smarks 			    who_nvp, perms_nvp, sets_nvp);
10654543Smarks 		}
10664543Smarks 	}
10674543Smarks }
10684543Smarks 
10694543Smarks /*
10704543Smarks  * Construct nvlist to pass down to kernel for setting/removing permissions.
10714543Smarks  *
10724543Smarks  * The nvlist is constructed as a series of nvpairs with an optional embedded
10734543Smarks  * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
10744543Smarks  * base attribute named stored in the dsl.
10754543Smarks  * Arguments:
10764543Smarks  *
10774543Smarks  * whostr:   is a comma separated list of users, groups, or a single set name.
10784543Smarks  *           whostr may be null for everyone or create perms.
10794543Smarks  * who_type: is the type of entry in whostr.  Typically this will be
10804543Smarks  *           ZFS_DELEG_WHO_UNKNOWN.
10815331Samw  * perms:    common separated list of permissions.  May be null if user
10824543Smarks  *           is requested to remove permissions by who.
10834543Smarks  * inherit:  Specifies the inheritance of the permissions.  Will be either
10844543Smarks  *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
10854543Smarks  * nvp       The constructed nvlist to pass to zfs_perm_set().
10864543Smarks  *           The output nvp will look something like this.
10874543Smarks  *              ul$1234 -> {create ; destroy }
10884543Smarks  *              Ul$1234 -> { @myset }
10894543Smarks  *              s-$@myset - { snapshot; checksum; compression }
10904543Smarks  */
10914543Smarks int
10924543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
10934543Smarks     zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
10944543Smarks {
10954543Smarks 	nvlist_t *who_nvp;
10964543Smarks 	nvlist_t *perms_nvp = NULL;
10974543Smarks 	nvlist_t *sets_nvp = NULL;
10984543Smarks 	char errbuf[1024];
10994787Sahrens 	char *who_tok, *perm;
11004543Smarks 	int error;
11014543Smarks 
11024543Smarks 	*nvp = NULL;
11034543Smarks 
11044543Smarks 	if (perms) {
11054543Smarks 		if ((error = nvlist_alloc(&perms_nvp,
11064543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11074543Smarks 			return (1);
11084543Smarks 		}
11094543Smarks 		if ((error = nvlist_alloc(&sets_nvp,
11104543Smarks 		    NV_UNIQUE_NAME, 0)) != 0) {
11114543Smarks 			nvlist_free(perms_nvp);
11124543Smarks 			return (1);
11134543Smarks 		}
11144543Smarks 	}
11154543Smarks 
11164543Smarks 	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
11174543Smarks 		if (perms_nvp)
11184543Smarks 			nvlist_free(perms_nvp);
11194543Smarks 		if (sets_nvp)
11204543Smarks 			nvlist_free(sets_nvp);
11214543Smarks 		return (1);
11224543Smarks 	}
11234543Smarks 
11244543Smarks 	if (who_type == ZFS_DELEG_NAMED_SET) {
11254543Smarks 		namecheck_err_t why;
11264543Smarks 		char what;
11274543Smarks 
11284543Smarks 		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
11294787Sahrens 			nvlist_free(who_nvp);
11304787Sahrens 			if (perms_nvp)
11314787Sahrens 				nvlist_free(perms_nvp);
11324787Sahrens 			if (sets_nvp)
11334787Sahrens 				nvlist_free(sets_nvp);
11344787Sahrens 
11354543Smarks 			switch (why) {
11364543Smarks 			case NAME_ERR_NO_AT:
11374543Smarks 				zfs_error_aux(zhp->zfs_hdl,
11384543Smarks 				    dgettext(TEXT_DOMAIN,
11394543Smarks 				    "set definition must begin with an '@' "
11404543Smarks 				    "character"));
11414543Smarks 			}
11424543Smarks 			return (zfs_error(zhp->zfs_hdl,
11434543Smarks 			    EZFS_BADPERMSET, whostr));
11444543Smarks 		}
11454543Smarks 	}
11464543Smarks 
11474543Smarks 	/*
11484543Smarks 	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
11494543Smarks 	 * The first nvlist perms_nvp will have normal permissions and the
11504543Smarks 	 * other sets_nvp will have only permssion set names in it.
11514543Smarks 	 */
11524787Sahrens 	for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) {
11534787Sahrens 		const char *perm_canonical = zfs_deleg_canonicalize_perm(perm);
11544787Sahrens 
11554787Sahrens 		if (perm_canonical) {
11564787Sahrens 			verify(nvlist_add_boolean(perms_nvp,
11574787Sahrens 			    perm_canonical) == 0);
11584787Sahrens 		} else if (perm[0] == '@') {
11594787Sahrens 			verify(nvlist_add_boolean(sets_nvp, perm) == 0);
11604787Sahrens 		} else {
11614787Sahrens 			nvlist_free(who_nvp);
11624787Sahrens 			nvlist_free(perms_nvp);
11634787Sahrens 			nvlist_free(sets_nvp);
11644787Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm));
11654543Smarks 		}
11664543Smarks 	}
11674543Smarks 
11684543Smarks 	if (whostr && who_type != ZFS_DELEG_CREATE) {
11694543Smarks 		who_tok = strtok(whostr, ",");
11704543Smarks 		if (who_tok == NULL) {
11714543Smarks 			nvlist_free(who_nvp);
11724787Sahrens 			if (perms_nvp)
11734787Sahrens 				nvlist_free(perms_nvp);
11744543Smarks 			if (sets_nvp)
11754543Smarks 				nvlist_free(sets_nvp);
11764543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11774543Smarks 			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
11784543Smarks 			    whostr);
11794543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
11804543Smarks 		}
11814543Smarks 	}
11824543Smarks 
11834543Smarks 	/*
11844543Smarks 	 * Now create the nvlist(s)
11854543Smarks 	 */
11864543Smarks 	do {
11874543Smarks 		uint64_t who_id;
11884543Smarks 
11894543Smarks 		error = zfs_get_perm_who(who_tok, &who_type,
11904543Smarks 		    &who_id);
11914543Smarks 		if (error) {
11924543Smarks 			nvlist_free(who_nvp);
11934787Sahrens 			if (perms_nvp)
11944787Sahrens 				nvlist_free(perms_nvp);
11954543Smarks 			if (sets_nvp)
11964543Smarks 				nvlist_free(sets_nvp);
11974543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
11984543Smarks 			    dgettext(TEXT_DOMAIN,
11994543Smarks 			    "Unable to determine uid/gid for "
12004543Smarks 			    "%s "), who_tok);
12014543Smarks 			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
12024543Smarks 		}
12034543Smarks 
12044543Smarks 		/*
12054543Smarks 		 * add entries for both local and descendent when required
12064543Smarks 		 */
12074543Smarks 		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
12084543Smarks 		    perms_nvp, sets_nvp, who_type, inherit);
12094543Smarks 
12104543Smarks 	} while (who_tok = strtok(NULL, ","));
12114543Smarks 	*nvp = who_nvp;
12124543Smarks 	return (0);
12134543Smarks }
12144543Smarks 
12154543Smarks static int
12164543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
12174543Smarks {
12184543Smarks 	zfs_cmd_t zc = { 0 };
12194543Smarks 	int error;
12204543Smarks 	char errbuf[1024];
12214543Smarks 
12224543Smarks 	(void) snprintf(errbuf, sizeof (errbuf),
12234543Smarks 	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
12244543Smarks 	    zhp->zfs_name);
12254543Smarks 
12265094Slling 	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp))
12274543Smarks 		return (-1);
12284543Smarks 
12294543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12304543Smarks 	zc.zc_perm_action = unset;
12314543Smarks 
12324543Smarks 	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
12334543Smarks 	if (error && errno == ENOTSUP) {
12344543Smarks 		(void) snprintf(errbuf, sizeof (errbuf),
12354543Smarks 		    gettext("Pool must be upgraded to use 'allow/unallow'"));
12364543Smarks 		zcmd_free_nvlists(&zc);
12374543Smarks 		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
12384543Smarks 	} else if (error) {
12394543Smarks 		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
12404543Smarks 	}
12414543Smarks 	zcmd_free_nvlists(&zc);
12424543Smarks 
12434543Smarks 	return (error);
12444543Smarks }
12454543Smarks 
12464543Smarks int
12474543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
12484543Smarks {
12494543Smarks 	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12504543Smarks }
12514543Smarks 
12524543Smarks int
12534543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12544543Smarks {
12554543Smarks 	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12564543Smarks }
12574543Smarks 
12584543Smarks static int
12594543Smarks perm_compare(const void *arg1, const void *arg2)
12604543Smarks {
12614543Smarks 	const zfs_perm_node_t *node1 = arg1;
12624543Smarks 	const zfs_perm_node_t *node2 = arg2;
12634543Smarks 	int ret;
12644543Smarks 
12654543Smarks 	ret = strcmp(node1->z_pname, node2->z_pname);
12664543Smarks 
12674543Smarks 	if (ret > 0)
12684543Smarks 		return (1);
12694543Smarks 	if (ret < 0)
12704543Smarks 		return (-1);
12714543Smarks 	else
12724543Smarks 		return (0);
12734543Smarks }
12744543Smarks 
12754543Smarks static void
12764543Smarks zfs_destroy_perm_tree(avl_tree_t *tree)
12774543Smarks {
12784543Smarks 	zfs_perm_node_t *permnode;
12795367Sahrens 	void *cookie = NULL;
12805367Sahrens 
12815367Sahrens 	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL)
12824543Smarks 		free(permnode);
12835367Sahrens 	avl_destroy(tree);
12844543Smarks }
12854543Smarks 
12864543Smarks static void
12874543Smarks zfs_destroy_tree(avl_tree_t *tree)
12884543Smarks {
12894543Smarks 	zfs_allow_node_t *allownode;
12905367Sahrens 	void *cookie = NULL;
12915367Sahrens 
12924543Smarks 	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12934543Smarks 		zfs_destroy_perm_tree(&allownode->z_localdescend);
12944543Smarks 		zfs_destroy_perm_tree(&allownode->z_local);
12954543Smarks 		zfs_destroy_perm_tree(&allownode->z_descend);
12964543Smarks 		free(allownode);
12974543Smarks 	}
12985367Sahrens 	avl_destroy(tree);
12994543Smarks }
13004543Smarks 
13014543Smarks void
13024543Smarks zfs_free_allows(zfs_allow_t *allow)
13034543Smarks {
13044543Smarks 	zfs_allow_t *allownext;
13054543Smarks 	zfs_allow_t *freeallow;
13064543Smarks 
13074543Smarks 	allownext = allow;
13084543Smarks 	while (allownext) {
13094543Smarks 		zfs_destroy_tree(&allownext->z_sets);
13104543Smarks 		zfs_destroy_tree(&allownext->z_crperms);
13114543Smarks 		zfs_destroy_tree(&allownext->z_user);
13124543Smarks 		zfs_destroy_tree(&allownext->z_group);
13134543Smarks 		zfs_destroy_tree(&allownext->z_everyone);
13144543Smarks 		freeallow = allownext;
13154543Smarks 		allownext = allownext->z_next;
13164543Smarks 		free(freeallow);
13174543Smarks 	}
13184543Smarks }
13194543Smarks 
13204543Smarks static zfs_allow_t *
13214543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
13224543Smarks {
13234543Smarks 	zfs_allow_t *ptree;
13244543Smarks 
13254543Smarks 	if ((ptree = zfs_alloc(zhp->zfs_hdl,
13264543Smarks 	    sizeof (zfs_allow_t))) == NULL) {
13274543Smarks 		return (NULL);
13284543Smarks 	}
13294543Smarks 
13304543Smarks 	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
13314543Smarks 	avl_create(&ptree->z_sets,
13324543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13334543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13344543Smarks 	avl_create(&ptree->z_crperms,
13354543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13364543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13374543Smarks 	avl_create(&ptree->z_user,
13384543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13394543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13404543Smarks 	avl_create(&ptree->z_group,
13414543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13424543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13434543Smarks 	avl_create(&ptree->z_everyone,
13444543Smarks 	    perm_compare, sizeof (zfs_allow_node_t),
13454543Smarks 	    offsetof(zfs_allow_node_t, z_node));
13464543Smarks 
13474543Smarks 	if (prev)
13484543Smarks 		prev->z_next = ptree;
13494543Smarks 	ptree->z_next = NULL;
13504543Smarks 	return (ptree);
13514543Smarks }
13524543Smarks 
13534543Smarks /*
13544543Smarks  * Add permissions to the appropriate AVL permission tree.
13554543Smarks  * The appropriate tree may not be the requested tree.
13564543Smarks  * For example if ld indicates a local permission, but
13574543Smarks  * same permission also exists as a descendent permission
13584543Smarks  * then the permission will be removed from the descendent
13594543Smarks  * tree and add the the local+descendent tree.
13604543Smarks  */
13614543Smarks static int
13624543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
13634543Smarks     char *perm, char ld)
13644543Smarks {
13654543Smarks 	zfs_perm_node_t pnode, *permnode, *permnode2;
13664543Smarks 	zfs_perm_node_t *newnode;
13674543Smarks 	avl_index_t where, where2;
13684543Smarks 	avl_tree_t *tree, *altree;
13694543Smarks 
13704543Smarks 	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
13714543Smarks 
13724543Smarks 	if (ld == ZFS_DELEG_NA) {
13734543Smarks 		tree =  &allownode->z_localdescend;
13744543Smarks 		altree = &allownode->z_descend;
13754543Smarks 	} else if (ld == ZFS_DELEG_LOCAL) {
13764543Smarks 		tree = &allownode->z_local;
13774543Smarks 		altree = &allownode->z_descend;
13784543Smarks 	} else {
13794543Smarks 		tree = &allownode->z_descend;
13804543Smarks 		altree = &allownode->z_local;
13814543Smarks 	}
13824543Smarks 	permnode = avl_find(tree, &pnode, &where);
13834543Smarks 	permnode2 = avl_find(altree, &pnode, &where2);
13844543Smarks 
13854543Smarks 	if (permnode2) {
13864543Smarks 		avl_remove(altree, permnode2);
13874543Smarks 		free(permnode2);
13884543Smarks 		if (permnode == NULL) {
13894543Smarks 			tree =  &allownode->z_localdescend;
13904543Smarks 		}
13914543Smarks 	}
13924543Smarks 
13934543Smarks 	/*
13944543Smarks 	 * Now insert new permission in either requested location
13954543Smarks 	 * local/descendent or into ld when perm will exist in both.
13964543Smarks 	 */
13974543Smarks 	if (permnode == NULL) {
13984543Smarks 		if ((newnode = zfs_alloc(zhp->zfs_hdl,
13994543Smarks 		    sizeof (zfs_perm_node_t))) == NULL) {
14004543Smarks 			return (-1);
14014543Smarks 		}
14024543Smarks 		*newnode = pnode;
14034543Smarks 		avl_add(tree, newnode);
14044543Smarks 	}
14054543Smarks 	return (0);
14064543Smarks }
14074577Sahrens 
14084543Smarks /*
14094543Smarks  * Uggh, this is going to be a bit complicated.
14104543Smarks  * we have an nvlist coming out of the kernel that
14114543Smarks  * will indicate where the permission is set and then
14124543Smarks  * it will contain allow of the various "who's", and what
14134543Smarks  * their permissions are.  To further complicate this
14144543Smarks  * we will then have to coalesce the local,descendent
14154543Smarks  * and local+descendent permissions where appropriate.
14164543Smarks  * The kernel only knows about a permission as being local
14174543Smarks  * or descendent, but not both.
14184543Smarks  *
14194543Smarks  * In order to make this easier for zfs_main to deal with
14204543Smarks  * a series of AVL trees will be used to maintain
14214543Smarks  * all of this, primarily for sorting purposes as well
14224543Smarks  * as the ability to quickly locate a specific entry.
14234543Smarks  *
14244543Smarks  * What we end up with are tree's for sets, create perms,
14254543Smarks  * user, groups and everyone.  With each of those trees
14264543Smarks  * we have subtrees for local, descendent and local+descendent
14274543Smarks  * permissions.
14284543Smarks  */
14294543Smarks int
14304543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
14314543Smarks {
14324543Smarks 	zfs_cmd_t zc = { 0 };
14334543Smarks 	int error;
14344543Smarks 	nvlist_t *nvlist;
14354543Smarks 	nvlist_t *permnv, *sourcenv;
14364543Smarks 	nvpair_t *who_pair, *source_pair;
14374543Smarks 	nvpair_t *perm_pair;
14384543Smarks 	char errbuf[1024];
14394543Smarks 	zfs_allow_t *zallowp, *newallowp;
14404543Smarks 	char  ld;
14414543Smarks 	char *nvpname;
14424543Smarks 	uid_t	uid;
14434543Smarks 	gid_t	gid;
14444543Smarks 	avl_tree_t *tree;
14454543Smarks 	avl_index_t where;
14464543Smarks 
14474543Smarks 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
14484543Smarks 
14494543Smarks 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
14504543Smarks 		return (-1);
14514543Smarks 
14524543Smarks 	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
14534543Smarks 		if (errno == ENOMEM) {
14544543Smarks 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
14554543Smarks 				zcmd_free_nvlists(&zc);
14564543Smarks 				return (-1);
14574543Smarks 			}
14584543Smarks 		} else if (errno == ENOTSUP) {
14594543Smarks 			zcmd_free_nvlists(&zc);
14604543Smarks 			(void) snprintf(errbuf, sizeof (errbuf),
14614543Smarks 			    gettext("Pool must be upgraded to use 'allow'"));
14624543Smarks 			return (zfs_error(zhp->zfs_hdl,
14634543Smarks 			    EZFS_BADVERSION, errbuf));
14644543Smarks 		} else {
14654543Smarks 			zcmd_free_nvlists(&zc);
14664543Smarks 			return (-1);
14674543Smarks 		}
14684543Smarks 	}
14694543Smarks 
14704543Smarks 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
14714543Smarks 		zcmd_free_nvlists(&zc);
14724543Smarks 		return (-1);
14734543Smarks 	}
14744543Smarks 
14754543Smarks 	zcmd_free_nvlists(&zc);
14764543Smarks 
14774543Smarks 	source_pair = nvlist_next_nvpair(nvlist, NULL);
14784543Smarks 
14794543Smarks 	if (source_pair == NULL) {
14804543Smarks 		*zfs_perms = NULL;
14814543Smarks 		return (0);
14824543Smarks 	}
14834543Smarks 
14844543Smarks 	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
14854543Smarks 	if (*zfs_perms == NULL) {
14864543Smarks 		return (0);
14874543Smarks 	}
14884543Smarks 
14894543Smarks 	zallowp = *zfs_perms;
14904543Smarks 
14914543Smarks 	for (;;) {
14924543Smarks 		struct passwd *pwd;
14934543Smarks 		struct group *grp;
14944543Smarks 		zfs_allow_node_t *allownode;
14954543Smarks 		zfs_allow_node_t  findallownode;
14964543Smarks 		zfs_allow_node_t *newallownode;
14974543Smarks 
14984543Smarks 		(void) strlcpy(zallowp->z_setpoint,
14994543Smarks 		    nvpair_name(source_pair),
15004543Smarks 		    sizeof (zallowp->z_setpoint));
15014543Smarks 
15024543Smarks 		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
15034543Smarks 			goto abort;
15044543Smarks 
15054543Smarks 		/*
15064543Smarks 		 * Make sure nvlist is composed correctly
15074543Smarks 		 */
15084543Smarks 		if (zfs_deleg_verify_nvlist(sourcenv)) {
15094543Smarks 			goto abort;
15104543Smarks 		}
15114543Smarks 
15124543Smarks 		who_pair = nvlist_next_nvpair(sourcenv, NULL);
15134543Smarks 		if (who_pair == NULL) {
15144543Smarks 			goto abort;
15154543Smarks 		}
15164543Smarks 
15174543Smarks 		do {
15184543Smarks 			error = nvpair_value_nvlist(who_pair, &permnv);
15194543Smarks 			if (error) {
15204543Smarks 				goto abort;
15214543Smarks 			}
15224543Smarks 
15234543Smarks 			/*
15244543Smarks 			 * First build up the key to use
15254543Smarks 			 * for looking up in the various
15264543Smarks 			 * who trees.
15274543Smarks 			 */
15284543Smarks 			ld = nvpair_name(who_pair)[1];
15294543Smarks 			nvpname = nvpair_name(who_pair);
15304543Smarks 			switch (nvpair_name(who_pair)[0]) {
15314543Smarks 			case ZFS_DELEG_USER:
15324543Smarks 			case ZFS_DELEG_USER_SETS:
15334543Smarks 				tree = &zallowp->z_user;
15344543Smarks 				uid = atol(&nvpname[3]);
15354543Smarks 				pwd = getpwuid(uid);
15364543Smarks 				(void) snprintf(findallownode.z_key,
15374543Smarks 				    sizeof (findallownode.z_key), "user %s",
15384543Smarks 				    (pwd) ? pwd->pw_name :
15394543Smarks 				    &nvpair_name(who_pair)[3]);
15404543Smarks 				break;
15414543Smarks 			case ZFS_DELEG_GROUP:
15424543Smarks 			case ZFS_DELEG_GROUP_SETS:
15434543Smarks 				tree = &zallowp->z_group;
15444543Smarks 				gid = atol(&nvpname[3]);
15454543Smarks 				grp = getgrgid(gid);
15464543Smarks 				(void) snprintf(findallownode.z_key,
15474543Smarks 				    sizeof (findallownode.z_key), "group %s",
15484543Smarks 				    (grp) ? grp->gr_name :
15494543Smarks 				    &nvpair_name(who_pair)[3]);
15504543Smarks 				break;
15514543Smarks 			case ZFS_DELEG_CREATE:
15524543Smarks 			case ZFS_DELEG_CREATE_SETS:
15534543Smarks 				tree = &zallowp->z_crperms;
15544543Smarks 				(void) strlcpy(findallownode.z_key, "",
15554543Smarks 				    sizeof (findallownode.z_key));
15564543Smarks 				break;
15574543Smarks 			case ZFS_DELEG_EVERYONE:
15584543Smarks 			case ZFS_DELEG_EVERYONE_SETS:
15594543Smarks 				(void) snprintf(findallownode.z_key,
15604543Smarks 				    sizeof (findallownode.z_key), "everyone");
15614543Smarks 				tree = &zallowp->z_everyone;
15624543Smarks 				break;
15634543Smarks 			case ZFS_DELEG_NAMED_SET:
15644543Smarks 			case ZFS_DELEG_NAMED_SET_SETS:
15654543Smarks 				(void) snprintf(findallownode.z_key,
15664543Smarks 				    sizeof (findallownode.z_key), "%s",
15674543Smarks 				    &nvpair_name(who_pair)[3]);
15684543Smarks 				tree = &zallowp->z_sets;
15694543Smarks 				break;
15704543Smarks 			}
15714543Smarks 
15724543Smarks 			/*
15734543Smarks 			 * Place who in tree
15744543Smarks 			 */
15754543Smarks 			allownode = avl_find(tree, &findallownode, &where);
15764543Smarks 			if (allownode == NULL) {
15774543Smarks 				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
15784543Smarks 				    sizeof (zfs_allow_node_t))) == NULL) {
15794543Smarks 					goto abort;
15804543Smarks 				}
15814543Smarks 				avl_create(&newallownode->z_localdescend,
15824543Smarks 				    perm_compare,
15834543Smarks 				    sizeof (zfs_perm_node_t),
15844543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15854543Smarks 				avl_create(&newallownode->z_local,
15864543Smarks 				    perm_compare,
15874543Smarks 				    sizeof (zfs_perm_node_t),
15884543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15894543Smarks 				avl_create(&newallownode->z_descend,
15904543Smarks 				    perm_compare,
15914543Smarks 				    sizeof (zfs_perm_node_t),
15924543Smarks 				    offsetof(zfs_perm_node_t, z_node));
15934543Smarks 				(void) strlcpy(newallownode->z_key,
15944543Smarks 				    findallownode.z_key,
15954543Smarks 				    sizeof (findallownode.z_key));
15964543Smarks 				avl_insert(tree, newallownode, where);
15974543Smarks 				allownode = newallownode;
15984543Smarks 			}
15994543Smarks 
16004543Smarks 			/*
16014543Smarks 			 * Now iterate over the permissions and
16024543Smarks 			 * place them in the appropriate local,
16034543Smarks 			 * descendent or local+descendent tree.
16044543Smarks 			 *
16054543Smarks 			 * The permissions are added to the tree
16064543Smarks 			 * via zfs_coalesce_perm().
16074543Smarks 			 */
16084543Smarks 			perm_pair = nvlist_next_nvpair(permnv, NULL);
16094543Smarks 			if (perm_pair == NULL)
16104543Smarks 				goto abort;
16114543Smarks 			do {
16124543Smarks 				if (zfs_coalesce_perm(zhp, allownode,
16134543Smarks 				    nvpair_name(perm_pair), ld) != 0)
16144543Smarks 					goto abort;
16154543Smarks 			} while (perm_pair = nvlist_next_nvpair(permnv,
16164543Smarks 			    perm_pair));
16174543Smarks 		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
16184543Smarks 
16194543Smarks 		source_pair = nvlist_next_nvpair(nvlist, source_pair);
16204543Smarks 		if (source_pair == NULL)
16214543Smarks 			break;
16224543Smarks 
16234543Smarks 		/*
16244543Smarks 		 * allocate another node from the link list of
16254543Smarks 		 * zfs_allow_t structures
16264543Smarks 		 */
16274543Smarks 		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
16284543Smarks 		    nvpair_name(source_pair));
16294543Smarks 		if (newallowp == NULL) {
16304543Smarks 			goto abort;
16314543Smarks 		}
16324543Smarks 		zallowp = newallowp;
16334543Smarks 	}
16344543Smarks 	nvlist_free(nvlist);
16354543Smarks 	return (0);
16364543Smarks abort:
16374543Smarks 	zfs_free_allows(*zfs_perms);
16384543Smarks 	nvlist_free(nvlist);
16394543Smarks 	return (-1);
16404543Smarks }
16414543Smarks 
16425993Smarks static char *
16435993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note)
16445993Smarks {
16455993Smarks 	/*
16465993Smarks 	 * Don't put newlines on end of lines
16475993Smarks 	 */
16485993Smarks 	switch (note) {
16495993Smarks 	case ZFS_DELEG_NOTE_CREATE:
16505993Smarks 		return (dgettext(TEXT_DOMAIN,
16515993Smarks 		    "Must also have the 'mount' ability"));
16525993Smarks 	case ZFS_DELEG_NOTE_DESTROY:
16535993Smarks 		return (dgettext(TEXT_DOMAIN,
16545993Smarks 		    "Must also have the 'mount' ability"));
16555993Smarks 	case ZFS_DELEG_NOTE_SNAPSHOT:
16565993Smarks 		return (dgettext(TEXT_DOMAIN,
16575993Smarks 		    "Must also have the 'mount' ability"));
16585993Smarks 	case ZFS_DELEG_NOTE_ROLLBACK:
16595993Smarks 		return (dgettext(TEXT_DOMAIN,
16605993Smarks 		    "Must also have the 'mount' ability"));
16615993Smarks 	case ZFS_DELEG_NOTE_CLONE:
16625993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'create' "
16635993Smarks 		    "ability and 'mount'\n"
16645993Smarks 		    "\t\t\t\tability in the origin file system"));
16655993Smarks 	case ZFS_DELEG_NOTE_PROMOTE:
16665993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n"
16675993Smarks 		    "\t\t\t\tand 'promote' ability in the origin file system"));
16685993Smarks 	case ZFS_DELEG_NOTE_RENAME:
16695993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' "
16705993Smarks 		    "and 'create' \n\t\t\t\tability in the new parent"));
16715993Smarks 	case ZFS_DELEG_NOTE_RECEIVE:
16725993Smarks 		return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'"
16735993Smarks 		    " and 'create' ability"));
16745993Smarks 	case ZFS_DELEG_NOTE_USERPROP:
16755993Smarks 		return (dgettext(TEXT_DOMAIN,
16765993Smarks 		    "Allows changing any user property"));
16775993Smarks 	case ZFS_DELEG_NOTE_ALLOW:
16785993Smarks 		return (dgettext(TEXT_DOMAIN,
16795993Smarks 		    "Must also have the permission that is being\n"
16805993Smarks 		    "\t\t\t\tallowed"));
16815993Smarks 	case ZFS_DELEG_NOTE_MOUNT:
16825993Smarks 		return (dgettext(TEXT_DOMAIN,
16835993Smarks 		    "Allows mount/umount of ZFS datasets"));
16845993Smarks 	case ZFS_DELEG_NOTE_SHARE:
16855993Smarks 		return (dgettext(TEXT_DOMAIN,
16865993Smarks 		    "Allows sharing file systems over NFS or SMB\n"
16875993Smarks 		    "\t\t\t\tprotocols"));
16885993Smarks 	case ZFS_DELEG_NOTE_NONE:
16895993Smarks 	default:
16905993Smarks 		return (dgettext(TEXT_DOMAIN, ""));
16915993Smarks 	}
16925993Smarks }
16935993Smarks 
16945993Smarks typedef enum {
16955993Smarks 	ZFS_DELEG_SUBCOMMAND,
16965993Smarks 	ZFS_DELEG_PROP,
16975993Smarks 	ZFS_DELEG_OTHER
16985993Smarks } zfs_deleg_perm_type_t;
16995993Smarks 
17005993Smarks /*
17015993Smarks  * is the permission a subcommand or other?
17025993Smarks  */
17035993Smarks zfs_deleg_perm_type_t
17045993Smarks zfs_deleg_perm_type(const char *perm)
17055993Smarks {
17065993Smarks 	if (strcmp(perm, "userprop") == 0)
17075993Smarks 		return (ZFS_DELEG_OTHER);
17085993Smarks 	else
17095993Smarks 		return (ZFS_DELEG_SUBCOMMAND);
17105993Smarks }
17115993Smarks 
17125993Smarks static char *
17135993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type)
17145993Smarks {
17155993Smarks 	switch (type) {
17165993Smarks 	case ZFS_DELEG_SUBCOMMAND:
17175993Smarks 		return (dgettext(TEXT_DOMAIN, "subcommand"));
17185993Smarks 	case ZFS_DELEG_PROP:
17195993Smarks 		return (dgettext(TEXT_DOMAIN, "property"));
17205993Smarks 	case ZFS_DELEG_OTHER:
17215993Smarks 		return (dgettext(TEXT_DOMAIN, "other"));
17225993Smarks 	}
17235993Smarks 	return ("");
17245993Smarks }
17255993Smarks 
17265993Smarks /*ARGSUSED*/
17275993Smarks static int
17285993Smarks zfs_deleg_prop_cb(int prop, void *cb)
17295993Smarks {
17305993Smarks 	if (zfs_prop_delegatable(prop))
17315993Smarks 		(void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop),
17325993Smarks 		    zfs_deleg_perm_type_str(ZFS_DELEG_PROP));
17335993Smarks 
17345993Smarks 	return (ZPROP_CONT);
17355993Smarks }
17365993Smarks 
17375993Smarks void
17385993Smarks zfs_deleg_permissions(void)
17395993Smarks {
17405993Smarks 	int i;
17415993Smarks 
17425993Smarks 	(void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME",
17435993Smarks 	    "TYPE", "NOTES");
17445993Smarks 
17455993Smarks 	/*
17465993Smarks 	 * First print out the subcommands
17475993Smarks 	 */
17485993Smarks 	for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) {
17495993Smarks 		(void) fprintf(stderr, "%-15s %-15s\t%s\n",
17505993Smarks 		    zfs_deleg_perm_tab[i].z_perm,
17515993Smarks 		    zfs_deleg_perm_type_str(
17525993Smarks 		    zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)),
17535993Smarks 		    zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note));
17545993Smarks 	}
17555993Smarks 
17565993Smarks 	(void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE,
17575993Smarks 	    ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME);
17585993Smarks }
17595993Smarks 
1760789Sahrens /*
1761789Sahrens  * Given a property name and value, set the property for the given dataset.
1762789Sahrens  */
1763789Sahrens int
17642676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1765789Sahrens {
1766789Sahrens 	zfs_cmd_t zc = { 0 };
17672676Seschrock 	int ret = -1;
17682676Seschrock 	prop_changelist_t *cl = NULL;
17692082Seschrock 	char errbuf[1024];
17702082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
17712676Seschrock 	nvlist_t *nvl = NULL, *realprops;
17722676Seschrock 	zfs_prop_t prop;
17736168Shs24103 	int do_prefix = 1;
17742082Seschrock 
17752082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
17762676Seschrock 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
17772082Seschrock 	    zhp->zfs_name);
17782082Seschrock 
17792676Seschrock 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
17802676Seschrock 	    nvlist_add_string(nvl, propname, propval) != 0) {
17812676Seschrock 		(void) no_memory(hdl);
17822676Seschrock 		goto error;
1783789Sahrens 	}
1784789Sahrens 
1785*7184Stimh 	if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
17862676Seschrock 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
17872676Seschrock 		goto error;
17885094Slling 
17892676Seschrock 	nvlist_free(nvl);
17902676Seschrock 	nvl = realprops;
17912676Seschrock 
17922676Seschrock 	prop = zfs_name_to_prop(propname);
17932676Seschrock 
1794789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
17952676Seschrock 		goto error;
1796789Sahrens 
1797789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
17982082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17992082Seschrock 		    "child dataset with inherited mountpoint is used "
18002082Seschrock 		    "in a non-global zone"));
18012082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1802789Sahrens 		goto error;
1803789Sahrens 	}
1804789Sahrens 
18056168Shs24103 
18066168Shs24103 	/* do not unmount dataset if canmount is being set to noauto */
18076168Shs24103 	if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO)
18086168Shs24103 		do_prefix = 0;
18096168Shs24103 
18106168Shs24103 	if (do_prefix && (ret = changelist_prefix(cl)) != 0)
18116168Shs24103 			goto error;
1812789Sahrens 
1813789Sahrens 	/*
1814789Sahrens 	 * Execute the corresponding ioctl() to set this property.
1815789Sahrens 	 */
1816789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1817789Sahrens 
18185094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
18192676Seschrock 		goto error;
18202676Seschrock 
18214543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1822789Sahrens 	if (ret != 0) {
1823789Sahrens 		switch (errno) {
1824789Sahrens 
1825789Sahrens 		case ENOSPC:
1826789Sahrens 			/*
1827789Sahrens 			 * For quotas and reservations, ENOSPC indicates
1828789Sahrens 			 * something different; setting a quota or reservation
1829789Sahrens 			 * doesn't use any disk space.
1830789Sahrens 			 */
1831789Sahrens 			switch (prop) {
1832789Sahrens 			case ZFS_PROP_QUOTA:
18335378Sck153898 			case ZFS_PROP_REFQUOTA:
18342082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18352082Seschrock 				    "size is less than current used or "
18362082Seschrock 				    "reserved space"));
18372082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1838789Sahrens 				break;
1839789Sahrens 
1840789Sahrens 			case ZFS_PROP_RESERVATION:
18415378Sck153898 			case ZFS_PROP_REFRESERVATION:
18422082Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18432082Seschrock 				    "size is greater than available space"));
18442082Seschrock 				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1845789Sahrens 				break;
1846789Sahrens 
1847789Sahrens 			default:
18482082Seschrock 				(void) zfs_standard_error(hdl, errno, errbuf);
1849789Sahrens 				break;
1850789Sahrens 			}
1851789Sahrens 			break;
1852789Sahrens 
1853789Sahrens 		case EBUSY:
18542082Seschrock 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
18552082Seschrock 				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
18562082Seschrock 			else
18572676Seschrock 				(void) zfs_standard_error(hdl, EBUSY, errbuf);
1858789Sahrens 			break;
1859789Sahrens 
18601175Slling 		case EROFS:
18612082Seschrock 			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
18621175Slling 			break;
18631175Slling 
18643886Sahl 		case ENOTSUP:
18653886Sahl 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18665977Smarks 			    "pool and or dataset must be upgraded to set this "
18674603Sahrens 			    "property or value"));
18683886Sahl 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
18693886Sahl 			break;
18703886Sahl 
18717042Sgw25295 		case ERANGE:
18727042Sgw25295 			if (prop == ZFS_PROP_COMPRESSION) {
18737042Sgw25295 				(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
18747042Sgw25295 				    "property setting is not allowed on "
18757042Sgw25295 				    "bootable datasets"));
18767042Sgw25295 				(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
18777042Sgw25295 			} else {
18787042Sgw25295 				(void) zfs_standard_error(hdl, errno, errbuf);
18797042Sgw25295 			}
18807042Sgw25295 			break;
18817042Sgw25295 
1882789Sahrens 		case EOVERFLOW:
1883789Sahrens 			/*
1884789Sahrens 			 * This platform can't address a volume this big.
1885789Sahrens 			 */
1886789Sahrens #ifdef _ILP32
1887789Sahrens 			if (prop == ZFS_PROP_VOLSIZE) {
18882082Seschrock 				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1889789Sahrens 				break;
1890789Sahrens 			}
1891789Sahrens #endif
18922082Seschrock 			/* FALLTHROUGH */
1893789Sahrens 		default:
18942082Seschrock 			(void) zfs_standard_error(hdl, errno, errbuf);
1895789Sahrens 		}
1896789Sahrens 	} else {
18976168Shs24103 		if (do_prefix)
18986168Shs24103 			ret = changelist_postfix(cl);
18996168Shs24103 
1900789Sahrens 		/*
1901789Sahrens 		 * Refresh the statistics so the new property value
1902789Sahrens 		 * is reflected.
1903789Sahrens 		 */
19046168Shs24103 		if (ret == 0)
19052676Seschrock 			(void) get_stats(zhp);
1906789Sahrens 	}
1907789Sahrens 
1908789Sahrens error:
19092676Seschrock 	nvlist_free(nvl);
19102676Seschrock 	zcmd_free_nvlists(&zc);
19112676Seschrock 	if (cl)
19122676Seschrock 		changelist_free(cl);
1913789Sahrens 	return (ret);
1914789Sahrens }
1915789Sahrens 
1916789Sahrens /*
1917789Sahrens  * Given a property, inherit the value from the parent dataset.
1918789Sahrens  */
1919789Sahrens int
19202676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1921789Sahrens {
1922789Sahrens 	zfs_cmd_t zc = { 0 };
1923789Sahrens 	int ret;
1924789Sahrens 	prop_changelist_t *cl;
19252082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
19262082Seschrock 	char errbuf[1024];
19272676Seschrock 	zfs_prop_t prop;
19282082Seschrock 
19292082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
19302082Seschrock 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1931789Sahrens 
19325094Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
19332676Seschrock 		/*
19342676Seschrock 		 * For user properties, the amount of work we have to do is very
19352676Seschrock 		 * small, so just do it here.
19362676Seschrock 		 */
19372676Seschrock 		if (!zfs_prop_user(propname)) {
19382676Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19392676Seschrock 			    "invalid property"));
19402676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
19412676Seschrock 		}
19422676Seschrock 
19432676Seschrock 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19442676Seschrock 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
19452676Seschrock 
19464849Sahrens 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
19472676Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
19482676Seschrock 
19492676Seschrock 		return (0);
19502676Seschrock 	}
19512676Seschrock 
1952789Sahrens 	/*
1953789Sahrens 	 * Verify that this property is inheritable.
1954789Sahrens 	 */
19552082Seschrock 	if (zfs_prop_readonly(prop))
19562082Seschrock 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
19572082Seschrock 
19582082Seschrock 	if (!zfs_prop_inheritable(prop))
19592082Seschrock 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1960789Sahrens 
1961789Sahrens 	/*
1962789Sahrens 	 * Check to see if the value applies to this type
1963789Sahrens 	 */
19642082Seschrock 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
19652082Seschrock 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1966789Sahrens 
19673443Srm160521 	/*
19683443Srm160521 	 * Normalize the name, to get rid of shorthand abbrevations.
19693443Srm160521 	 */
19703443Srm160521 	propname = zfs_prop_to_name(prop);
1971789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19722676Seschrock 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1973789Sahrens 
1974789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1975789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
19762082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19772082Seschrock 		    "dataset is used in a non-global zone"));
19782082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1979789Sahrens 	}
1980789Sahrens 
1981789Sahrens 	/*
1982789Sahrens 	 * Determine datasets which will be affected by this change, if any.
1983789Sahrens 	 */
1984789Sahrens 	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
1985789Sahrens 		return (-1);
1986789Sahrens 
1987789Sahrens 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
19882082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
19892082Seschrock 		    "child dataset with inherited mountpoint is used "
19902082Seschrock 		    "in a non-global zone"));
19912082Seschrock 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1992789Sahrens 		goto error;
1993789Sahrens 	}
1994789Sahrens 
1995789Sahrens 	if ((ret = changelist_prefix(cl)) != 0)
1996789Sahrens 		goto error;
1997789Sahrens 
19984849Sahrens 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
19992082Seschrock 		return (zfs_standard_error(hdl, errno, errbuf));
2000789Sahrens 	} else {
2001789Sahrens 
20022169Snd150628 		if ((ret = changelist_postfix(cl)) != 0)
2003789Sahrens 			goto error;
2004789Sahrens 
2005789Sahrens 		/*
2006789Sahrens 		 * Refresh the statistics so the new property is reflected.
2007789Sahrens 		 */
2008789Sahrens 		(void) get_stats(zhp);
2009789Sahrens 	}
2010789Sahrens 
2011789Sahrens error:
2012789Sahrens 	changelist_free(cl);
2013789Sahrens 	return (ret);
2014789Sahrens }
2015789Sahrens 
2016789Sahrens /*
20171356Seschrock  * True DSL properties are stored in an nvlist.  The following two functions
20181356Seschrock  * extract them appropriately.
20191356Seschrock  */
20201356Seschrock static uint64_t
20211356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20221356Seschrock {
20231356Seschrock 	nvlist_t *nv;
20241356Seschrock 	uint64_t value;
20251356Seschrock 
20262885Sahrens 	*source = NULL;
20271356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20281356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20295094Slling 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
20305094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20311356Seschrock 	} else {
20321356Seschrock 		value = zfs_prop_default_numeric(prop);
20331356Seschrock 		*source = "";
20341356Seschrock 	}
20351356Seschrock 
20361356Seschrock 	return (value);
20371356Seschrock }
20381356Seschrock 
20391356Seschrock static char *
20401356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20411356Seschrock {
20421356Seschrock 	nvlist_t *nv;
20431356Seschrock 	char *value;
20441356Seschrock 
20452885Sahrens 	*source = NULL;
20461356Seschrock 	if (nvlist_lookup_nvlist(zhp->zfs_props,
20471356Seschrock 	    zfs_prop_to_name(prop), &nv) == 0) {
20485094Slling 		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
20495094Slling 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
20501356Seschrock 	} else {
20511356Seschrock 		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
20521356Seschrock 			value = "";
20531356Seschrock 		*source = "";
20541356Seschrock 	}
20551356Seschrock 
20561356Seschrock 	return (value);
20571356Seschrock }
20581356Seschrock 
20591356Seschrock /*
2060789Sahrens  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2061789Sahrens  * zfs_prop_get_int() are built using this interface.
2062789Sahrens  *
2063789Sahrens  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2064789Sahrens  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2065789Sahrens  * If they differ from the on-disk values, report the current values and mark
2066789Sahrens  * the source "temporary".
2067789Sahrens  */
20682082Seschrock static int
20695094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
20702082Seschrock     char **source, uint64_t *val)
2071789Sahrens {
20725147Srm160521 	zfs_cmd_t zc = { 0 };
20735592Stimh 	nvlist_t *zplprops = NULL;
2074789Sahrens 	struct mnttab mnt;
20753265Sahrens 	char *mntopt_on = NULL;
20763265Sahrens 	char *mntopt_off = NULL;
2077789Sahrens 
2078789Sahrens 	*source = NULL;
2079789Sahrens 
20803265Sahrens 	switch (prop) {
20813265Sahrens 	case ZFS_PROP_ATIME:
20823265Sahrens 		mntopt_on = MNTOPT_ATIME;
20833265Sahrens 		mntopt_off = MNTOPT_NOATIME;
20843265Sahrens 		break;
20853265Sahrens 
20863265Sahrens 	case ZFS_PROP_DEVICES:
20873265Sahrens 		mntopt_on = MNTOPT_DEVICES;
20883265Sahrens 		mntopt_off = MNTOPT_NODEVICES;
20893265Sahrens 		break;
20903265Sahrens 
20913265Sahrens 	case ZFS_PROP_EXEC:
20923265Sahrens 		mntopt_on = MNTOPT_EXEC;
20933265Sahrens 		mntopt_off = MNTOPT_NOEXEC;
20943265Sahrens 		break;
20953265Sahrens 
20963265Sahrens 	case ZFS_PROP_READONLY:
20973265Sahrens 		mntopt_on = MNTOPT_RO;
20983265Sahrens 		mntopt_off = MNTOPT_RW;
20993265Sahrens 		break;
21003265Sahrens 
21013265Sahrens 	case ZFS_PROP_SETUID:
21023265Sahrens 		mntopt_on = MNTOPT_SETUID;
21033265Sahrens 		mntopt_off = MNTOPT_NOSETUID;
21043265Sahrens 		break;
21053265Sahrens 
21063265Sahrens 	case ZFS_PROP_XATTR:
21073265Sahrens 		mntopt_on = MNTOPT_XATTR;
21083265Sahrens 		mntopt_off = MNTOPT_NOXATTR;
21093265Sahrens 		break;
21105331Samw 
21115331Samw 	case ZFS_PROP_NBMAND:
21125331Samw 		mntopt_on = MNTOPT_NBMAND;
21135331Samw 		mntopt_off = MNTOPT_NONBMAND;
21145331Samw 		break;
21153265Sahrens 	}
21163265Sahrens 
21172474Seschrock 	/*
21182474Seschrock 	 * Because looking up the mount options is potentially expensive
21192474Seschrock 	 * (iterating over all of /etc/mnttab), we defer its calculation until
21202474Seschrock 	 * we're looking up a property which requires its presence.
21212474Seschrock 	 */
21222474Seschrock 	if (!zhp->zfs_mntcheck &&
21233265Sahrens 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
21243265Sahrens 		struct mnttab entry, search = { 0 };
21253265Sahrens 		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
21262474Seschrock 
21272474Seschrock 		search.mnt_special = (char *)zhp->zfs_name;
21282474Seschrock 		search.mnt_fstype = MNTTYPE_ZFS;
21293265Sahrens 		rewind(mnttab);
21303265Sahrens 
21313265Sahrens 		if (getmntany(mnttab, &entry, &search) == 0) {
21323265Sahrens 			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
21333265Sahrens 			    entry.mnt_mntopts);
21343265Sahrens 			if (zhp->zfs_mntopts == NULL)
21353265Sahrens 				return (-1);
21363265Sahrens 		}
21372474Seschrock 
21382474Seschrock 		zhp->zfs_mntcheck = B_TRUE;
21392474Seschrock 	}
21402474Seschrock 
2141789Sahrens 	if (zhp->zfs_mntopts == NULL)
2142789Sahrens 		mnt.mnt_mntopts = "";
2143789Sahrens 	else
2144789Sahrens 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2145789Sahrens 
2146789Sahrens 	switch (prop) {
2147789Sahrens 	case ZFS_PROP_ATIME:
21483265Sahrens 	case ZFS_PROP_DEVICES:
21493265Sahrens 	case ZFS_PROP_EXEC:
21503265Sahrens 	case ZFS_PROP_READONLY:
21513265Sahrens 	case ZFS_PROP_SETUID:
21523265Sahrens 	case ZFS_PROP_XATTR:
21535331Samw 	case ZFS_PROP_NBMAND:
21542082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21552082Seschrock 
21563265Sahrens 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
21572082Seschrock 			*val = B_TRUE;
2158789Sahrens 			if (src)
21595094Slling 				*src = ZPROP_SRC_TEMPORARY;
21603265Sahrens 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
21612082Seschrock 			*val = B_FALSE;
2162789Sahrens 			if (src)
21635094Slling 				*src = ZPROP_SRC_TEMPORARY;
2164789Sahrens 		}
21652082Seschrock 		break;
2166789Sahrens 
21673265Sahrens 	case ZFS_PROP_CANMOUNT:
21682082Seschrock 		*val = getprop_uint64(zhp, prop, source);
21696168Shs24103 		if (*val != ZFS_CANMOUNT_ON)
21703417Srm160521 			*source = zhp->zfs_name;
21713417Srm160521 		else
21723417Srm160521 			*source = "";	/* default */
21732082Seschrock 		break;
2174789Sahrens 
2175789Sahrens 	case ZFS_PROP_QUOTA:
21765378Sck153898 	case ZFS_PROP_REFQUOTA:
2177789Sahrens 	case ZFS_PROP_RESERVATION:
21785378Sck153898 	case ZFS_PROP_REFRESERVATION:
21792885Sahrens 		*val = getprop_uint64(zhp, prop, source);
21802885Sahrens 		if (*val == 0)
2181789Sahrens 			*source = "";	/* default */
2182789Sahrens 		else
2183789Sahrens 			*source = zhp->zfs_name;
21842082Seschrock 		break;
2185789Sahrens 
2186789Sahrens 	case ZFS_PROP_MOUNTED:
21872082Seschrock 		*val = (zhp->zfs_mntopts != NULL);
21882082Seschrock 		break;
2189789Sahrens 
21903377Seschrock 	case ZFS_PROP_NUMCLONES:
21913377Seschrock 		*val = zhp->zfs_dmustats.dds_num_clones;
21923377Seschrock 		break;
21933377Seschrock 
21945147Srm160521 	case ZFS_PROP_VERSION:
21955498Stimh 	case ZFS_PROP_NORMALIZE:
21965498Stimh 	case ZFS_PROP_UTF8ONLY:
21975498Stimh 	case ZFS_PROP_CASE:
21985498Stimh 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
21995498Stimh 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
22005473Srm160521 			return (-1);
22015147Srm160521 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
22025498Stimh 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
22035498Stimh 			zcmd_free_nvlists(&zc);
22045147Srm160521 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22055498Stimh 			    "unable to get %s property"),
22065498Stimh 			    zfs_prop_to_name(prop));
22075147Srm160521 			return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
22085147Srm160521 			    dgettext(TEXT_DOMAIN, "internal error")));
22095147Srm160521 		}
22105498Stimh 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
22115498Stimh 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
22125498Stimh 		    val) != 0) {
22135498Stimh 			zcmd_free_nvlists(&zc);
22145498Stimh 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22155498Stimh 			    "unable to get %s property"),
22165498Stimh 			    zfs_prop_to_name(prop));
22175498Stimh 			return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
22185498Stimh 			    dgettext(TEXT_DOMAIN, "internal error")));
22195498Stimh 		}
22205592Stimh 		if (zplprops)
22215592Stimh 			nvlist_free(zplprops);
22225498Stimh 		zcmd_free_nvlists(&zc);
22235147Srm160521 		break;
22245147Srm160521 
2225789Sahrens 	default:
22264577Sahrens 		switch (zfs_prop_get_type(prop)) {
22274787Sahrens 		case PROP_TYPE_NUMBER:
22284787Sahrens 		case PROP_TYPE_INDEX:
22294577Sahrens 			*val = getprop_uint64(zhp, prop, source);
22304577Sahrens 			break;
22314577Sahrens 
22324787Sahrens 		case PROP_TYPE_STRING:
22334577Sahrens 		default:
22344577Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
22354577Sahrens 			    "cannot get non-numeric property"));
22364577Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
22374577Sahrens 			    dgettext(TEXT_DOMAIN, "internal error")));
22384577Sahrens 		}
2239789Sahrens 	}
2240789Sahrens 
2241789Sahrens 	return (0);
2242789Sahrens }
2243789Sahrens 
2244789Sahrens /*
2245789Sahrens  * Calculate the source type, given the raw source string.
2246789Sahrens  */
2247789Sahrens static void
22485094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2249789Sahrens     char *statbuf, size_t statlen)
2250789Sahrens {
22515094Slling 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2252789Sahrens 		return;
2253789Sahrens 
2254789Sahrens 	if (source == NULL) {
22555094Slling 		*srctype = ZPROP_SRC_NONE;
2256789Sahrens 	} else if (source[0] == '\0') {
22575094Slling 		*srctype = ZPROP_SRC_DEFAULT;
2258789Sahrens 	} else {
2259789Sahrens 		if (strcmp(source, zhp->zfs_name) == 0) {
22605094Slling 			*srctype = ZPROP_SRC_LOCAL;
2261789Sahrens 		} else {
2262789Sahrens 			(void) strlcpy(statbuf, source, statlen);
22635094Slling 			*srctype = ZPROP_SRC_INHERITED;
2264789Sahrens 		}
2265789Sahrens 	}
2266789Sahrens 
2267789Sahrens }
2268789Sahrens 
2269789Sahrens /*
2270789Sahrens  * Retrieve a property from the given object.  If 'literal' is specified, then
2271789Sahrens  * numbers are left as exact values.  Otherwise, numbers are converted to a
2272789Sahrens  * human-readable form.
2273789Sahrens  *
2274789Sahrens  * Returns 0 on success, or -1 on error.
2275789Sahrens  */
2276789Sahrens int
2277789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
22785094Slling     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2279789Sahrens {
2280789Sahrens 	char *source = NULL;
2281789Sahrens 	uint64_t val;
2282789Sahrens 	char *str;
22832676Seschrock 	const char *strval;
2284789Sahrens 
2285789Sahrens 	/*
2286789Sahrens 	 * Check to see if this property applies to our object
2287789Sahrens 	 */
2288789Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2289789Sahrens 		return (-1);
2290789Sahrens 
2291789Sahrens 	if (src)
22925094Slling 		*src = ZPROP_SRC_NONE;
2293789Sahrens 
2294789Sahrens 	switch (prop) {
2295789Sahrens 	case ZFS_PROP_CREATION:
2296789Sahrens 		/*
2297789Sahrens 		 * 'creation' is a time_t stored in the statistics.  We convert
2298789Sahrens 		 * this into a string unless 'literal' is specified.
2299789Sahrens 		 */
2300789Sahrens 		{
23012885Sahrens 			val = getprop_uint64(zhp, prop, &source);
23022885Sahrens 			time_t time = (time_t)val;
2303789Sahrens 			struct tm t;
2304789Sahrens 
2305789Sahrens 			if (literal ||
2306789Sahrens 			    localtime_r(&time, &t) == NULL ||
2307789Sahrens 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2308789Sahrens 			    &t) == 0)
23092885Sahrens 				(void) snprintf(propbuf, proplen, "%llu", val);
2310789Sahrens 		}
2311789Sahrens 		break;
2312789Sahrens 
2313789Sahrens 	case ZFS_PROP_MOUNTPOINT:
2314789Sahrens 		/*
2315789Sahrens 		 * Getting the precise mountpoint can be tricky.
2316789Sahrens 		 *
2317789Sahrens 		 *  - for 'none' or 'legacy', return those values.
2318789Sahrens 		 *  - for inherited mountpoints, we want to take everything
2319789Sahrens 		 *    after our ancestor and append it to the inherited value.
2320789Sahrens 		 *
2321789Sahrens 		 * If the pool has an alternate root, we want to prepend that
2322789Sahrens 		 * root to any values we return.
2323789Sahrens 		 */
23246865Srm160521 
23251356Seschrock 		str = getprop_string(zhp, prop, &source);
23261356Seschrock 
23276612Sgw25295 		if (str[0] == '/') {
23286865Srm160521 			char buf[MAXPATHLEN];
23296865Srm160521 			char *root = buf;
23301356Seschrock 			const char *relpath = zhp->zfs_name + strlen(source);
2331789Sahrens 
2332789Sahrens 			if (relpath[0] == '/')
2333789Sahrens 				relpath++;
23346612Sgw25295 
23356865Srm160521 			if ((zpool_get_prop(zhp->zpool_hdl,
23366865Srm160521 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
23376865Srm160521 			    (strcmp(root, "-") == 0))
23386865Srm160521 				root[0] = '\0';
23396612Sgw25295 			/*
23406612Sgw25295 			 * Special case an alternate root of '/'. This will
23416612Sgw25295 			 * avoid having multiple leading slashes in the
23426612Sgw25295 			 * mountpoint path.
23436612Sgw25295 			 */
23446612Sgw25295 			if (strcmp(root, "/") == 0)
23456612Sgw25295 				root++;
23466612Sgw25295 
23476612Sgw25295 			/*
23486612Sgw25295 			 * If the mountpoint is '/' then skip over this
23496612Sgw25295 			 * if we are obtaining either an alternate root or
23506612Sgw25295 			 * an inherited mountpoint.
23516612Sgw25295 			 */
23526612Sgw25295 			if (str[1] == '\0' && (root[0] != '\0' ||
23536612Sgw25295 			    relpath[0] != '\0'))
23541356Seschrock 				str++;
2355789Sahrens 
2356789Sahrens 			if (relpath[0] == '\0')
2357789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s",
23581356Seschrock 				    root, str);
2359789Sahrens 			else
2360789Sahrens 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
23611356Seschrock 				    root, str, relpath[0] == '@' ? "" : "/",
2362789Sahrens 				    relpath);
2363789Sahrens 		} else {
2364789Sahrens 			/* 'legacy' or 'none' */
23651356Seschrock 			(void) strlcpy(propbuf, str, proplen);
2366789Sahrens 		}
2367789Sahrens 
2368789Sahrens 		break;
2369789Sahrens 
2370789Sahrens 	case ZFS_PROP_ORIGIN:
23712885Sahrens 		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2372789Sahrens 		    proplen);
2373789Sahrens 		/*
2374789Sahrens 		 * If there is no parent at all, return failure to indicate that
2375789Sahrens 		 * it doesn't apply to this dataset.
2376789Sahrens 		 */
2377789Sahrens 		if (propbuf[0] == '\0')
2378789Sahrens 			return (-1);
2379789Sahrens 		break;
2380789Sahrens 
2381789Sahrens 	case ZFS_PROP_QUOTA:
23825378Sck153898 	case ZFS_PROP_REFQUOTA:
2383789Sahrens 	case ZFS_PROP_RESERVATION:
23845378Sck153898 	case ZFS_PROP_REFRESERVATION:
23855378Sck153898 
23862082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
23872082Seschrock 			return (-1);
2388789Sahrens 
2389789Sahrens 		/*
2390789Sahrens 		 * If quota or reservation is 0, we translate this into 'none'
2391789Sahrens 		 * (unless literal is set), and indicate that it's the default
2392789Sahrens 		 * value.  Otherwise, we print the number nicely and indicate
2393789Sahrens 		 * that its set locally.
2394789Sahrens 		 */
2395789Sahrens 		if (val == 0) {
2396789Sahrens 			if (literal)
2397789Sahrens 				(void) strlcpy(propbuf, "0", proplen);
2398789Sahrens 			else
2399789Sahrens 				(void) strlcpy(propbuf, "none", proplen);
2400789Sahrens 		} else {
2401789Sahrens 			if (literal)
24022856Snd150628 				(void) snprintf(propbuf, proplen, "%llu",
24033912Slling 				    (u_longlong_t)val);
2404789Sahrens 			else
2405789Sahrens 				zfs_nicenum(val, propbuf, proplen);
2406789Sahrens 		}
2407789Sahrens 		break;
2408789Sahrens 
2409789Sahrens 	case ZFS_PROP_COMPRESSRATIO:
24102082Seschrock 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
24112082Seschrock 			return (-1);
24122856Snd150628 		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
24132856Snd150628 		    val / 100, (longlong_t)val % 100);
2414789Sahrens 		break;
2415789Sahrens 
2416789Sahrens 	case ZFS_PROP_TYPE:
2417789Sahrens 		switch (zhp->zfs_type) {
2418789Sahrens 		case ZFS_TYPE_FILESYSTEM:
2419789Sahrens 			str = "filesystem";
2420789Sahrens 			break;
2421789Sahrens 		case ZFS_TYPE_VOLUME:
2422789Sahrens 			str = "volume";
2423789Sahrens 			break;
2424789Sahrens 		case ZFS_TYPE_SNAPSHOT:
2425789Sahrens 			str = "snapshot";
2426789Sahrens 			break;
2427789Sahrens 		default:
24282082Seschrock 			abort();
2429789Sahrens 		}
2430789Sahrens 		(void) snprintf(propbuf, proplen, "%s", str);
2431789Sahrens 		break;
2432789Sahrens 
2433789Sahrens 	case ZFS_PROP_MOUNTED:
2434789Sahrens 		/*
2435789Sahrens 		 * The 'mounted' property is a pseudo-property that described
2436789Sahrens 		 * whether the filesystem is currently mounted.  Even though
2437789Sahrens 		 * it's a boolean value, the typical values of "on" and "off"
2438789Sahrens 		 * don't make sense, so we translate to "yes" and "no".
2439789Sahrens 		 */
24402082Seschrock 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
24412082Seschrock 		    src, &source, &val) != 0)
24422082Seschrock 			return (-1);
24432082Seschrock 		if (val)
2444789Sahrens 			(void) strlcpy(propbuf, "yes", proplen);
2445789Sahrens 		else
2446789Sahrens 			(void) strlcpy(propbuf, "no", proplen);
2447789Sahrens 		break;
2448789Sahrens 
2449789Sahrens 	case ZFS_PROP_NAME:
2450789Sahrens 		/*
2451789Sahrens 		 * The 'name' property is a pseudo-property derived from the
2452789Sahrens 		 * dataset name.  It is presented as a real property to simplify
2453789Sahrens 		 * consumers.
2454789Sahrens 		 */
2455789Sahrens 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2456789Sahrens 		break;
2457789Sahrens 
2458789Sahrens 	default:
24594577Sahrens 		switch (zfs_prop_get_type(prop)) {
24604787Sahrens 		case PROP_TYPE_NUMBER:
24614577Sahrens 			if (get_numeric_property(zhp, prop, src,
24624577Sahrens 			    &source, &val) != 0)
24634577Sahrens 				return (-1);
24644577Sahrens 			if (literal)
24654577Sahrens 				(void) snprintf(propbuf, proplen, "%llu",
24664577Sahrens 				    (u_longlong_t)val);
24674577Sahrens 			else
24684577Sahrens 				zfs_nicenum(val, propbuf, proplen);
24694577Sahrens 			break;
24704577Sahrens 
24714787Sahrens 		case PROP_TYPE_STRING:
24724577Sahrens 			(void) strlcpy(propbuf,
24734577Sahrens 			    getprop_string(zhp, prop, &source), proplen);
24744577Sahrens 			break;
24754577Sahrens 
24764787Sahrens 		case PROP_TYPE_INDEX:
24774861Sahrens 			if (get_numeric_property(zhp, prop, src,
24784861Sahrens 			    &source, &val) != 0)
24794861Sahrens 				return (-1);
24804861Sahrens 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
24814577Sahrens 				return (-1);
24824577Sahrens 			(void) strlcpy(propbuf, strval, proplen);
24834577Sahrens 			break;
24844577Sahrens 
24854577Sahrens 		default:
24864577Sahrens 			abort();
24874577Sahrens 		}
2488789Sahrens 	}
2489789Sahrens 
2490789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2491789Sahrens 
2492789Sahrens 	return (0);
2493789Sahrens }
2494789Sahrens 
2495789Sahrens /*
2496789Sahrens  * Utility function to get the given numeric property.  Does no validation that
2497789Sahrens  * the given property is the appropriate type; should only be used with
2498789Sahrens  * hard-coded property types.
2499789Sahrens  */
2500789Sahrens uint64_t
2501789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2502789Sahrens {
2503789Sahrens 	char *source;
25042082Seschrock 	uint64_t val;
25052082Seschrock 
25065367Sahrens 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
25072082Seschrock 
25082082Seschrock 	return (val);
2509789Sahrens }
2510789Sahrens 
25115713Srm160521 int
25125713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
25135713Srm160521 {
25145713Srm160521 	char buf[64];
25155713Srm160521 
25165713Srm160521 	zfs_nicenum(val, buf, sizeof (buf));
25175713Srm160521 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
25185713Srm160521 }
25195713Srm160521 
2520789Sahrens /*
2521789Sahrens  * Similar to zfs_prop_get(), but returns the value as an integer.
2522789Sahrens  */
2523789Sahrens int
2524789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
25255094Slling     zprop_source_t *src, char *statbuf, size_t statlen)
2526789Sahrens {
2527789Sahrens 	char *source;
2528789Sahrens 
2529789Sahrens 	/*
2530789Sahrens 	 * Check to see if this property applies to our object
2531789Sahrens 	 */
25324849Sahrens 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
25333237Slling 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
25342082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
25352082Seschrock 		    zfs_prop_to_name(prop)));
25364849Sahrens 	}
2537789Sahrens 
2538789Sahrens 	if (src)
25395094Slling 		*src = ZPROP_SRC_NONE;
2540789Sahrens 
25412082Seschrock 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
25422082Seschrock 		return (-1);
2543789Sahrens 
2544789Sahrens 	get_source(zhp, src, source, statbuf, statlen);
2545789Sahrens 
2546789Sahrens 	return (0);
2547789Sahrens }
2548789Sahrens 
2549789Sahrens /*
2550789Sahrens  * Returns the name of the given zfs handle.
2551789Sahrens  */
2552789Sahrens const char *
2553789Sahrens zfs_get_name(const zfs_handle_t *zhp)
2554789Sahrens {
2555789Sahrens 	return (zhp->zfs_name);
2556789Sahrens }
2557789Sahrens 
2558789Sahrens /*
2559789Sahrens  * Returns the type of the given zfs handle.
2560789Sahrens  */
2561789Sahrens zfs_type_t
2562789Sahrens zfs_get_type(const zfs_handle_t *zhp)
2563789Sahrens {
2564789Sahrens 	return (zhp->zfs_type);
2565789Sahrens }
2566789Sahrens 
2567789Sahrens /*
25681356Seschrock  * Iterate over all child filesystems
2569789Sahrens  */
2570789Sahrens int
25711356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2572789Sahrens {
2573789Sahrens 	zfs_cmd_t zc = { 0 };
2574789Sahrens 	zfs_handle_t *nzhp;
2575789Sahrens 	int ret;
2576789Sahrens 
25775367Sahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
25785367Sahrens 		return (0);
25795367Sahrens 
2580789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
25812082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
2582789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2583789Sahrens 		/*
2584789Sahrens 		 * Ignore private dataset names.
2585789Sahrens 		 */
2586789Sahrens 		if (dataset_name_hidden(zc.zc_name))
2587789Sahrens 			continue;
2588789Sahrens 
2589789Sahrens 		/*
2590789Sahrens 		 * Silently ignore errors, as the only plausible explanation is
2591789Sahrens 		 * that the pool has since been removed.
2592789Sahrens 		 */
25932082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
25942082Seschrock 		    zc.zc_name)) == NULL)
2595789Sahrens 			continue;
2596789Sahrens 
2597789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2598789Sahrens 			return (ret);
2599789Sahrens 	}
2600789Sahrens 
2601789Sahrens 	/*
2602789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2603789Sahrens 	 * returned, then the underlying dataset has been removed since we
2604789Sahrens 	 * obtained the handle.
2605789Sahrens 	 */
2606789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26072082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26082082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2609789Sahrens 
26101356Seschrock 	return (0);
26111356Seschrock }
26121356Seschrock 
26131356Seschrock /*
26141356Seschrock  * Iterate over all snapshots
26151356Seschrock  */
26161356Seschrock int
26171356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26181356Seschrock {
26191356Seschrock 	zfs_cmd_t zc = { 0 };
26201356Seschrock 	zfs_handle_t *nzhp;
26211356Seschrock 	int ret;
2622789Sahrens 
26235367Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
26245367Sahrens 		return (0);
26255367Sahrens 
2626789Sahrens 	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
26272082Seschrock 	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
26282082Seschrock 	    &zc) == 0;
2629789Sahrens 	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
2630789Sahrens 
26312082Seschrock 		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
26322082Seschrock 		    zc.zc_name)) == NULL)
2633789Sahrens 			continue;
2634789Sahrens 
2635789Sahrens 		if ((ret = func(nzhp, data)) != 0)
2636789Sahrens 			return (ret);
2637789Sahrens 	}
2638789Sahrens 
2639789Sahrens 	/*
2640789Sahrens 	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
2641789Sahrens 	 * returned, then the underlying dataset has been removed since we
2642789Sahrens 	 * obtained the handle.  Silently ignore this case, and return success.
2643789Sahrens 	 */
2644789Sahrens 	if (errno != ESRCH && errno != ENOENT)
26452082Seschrock 		return (zfs_standard_error(zhp->zfs_hdl, errno,
26462082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
2647789Sahrens 
2648789Sahrens 	return (0);
2649789Sahrens }
2650789Sahrens 
2651789Sahrens /*
26521356Seschrock  * Iterate over all children, snapshots and filesystems
26531356Seschrock  */
26541356Seschrock int
26551356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
26561356Seschrock {
26571356Seschrock 	int ret;
26581356Seschrock 
26591356Seschrock 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
26601356Seschrock 		return (ret);
26611356Seschrock 
26621356Seschrock 	return (zfs_iter_snapshots(zhp, func, data));
26631356Seschrock }
26641356Seschrock 
26651356Seschrock /*
2666789Sahrens  * Given a complete name, return just the portion that refers to the parent.
2667789Sahrens  * Can return NULL if this is a pool.
2668789Sahrens  */
2669789Sahrens static int
2670789Sahrens parent_name(const char *path, char *buf, size_t buflen)
2671789Sahrens {
2672789Sahrens 	char *loc;
2673789Sahrens 
2674789Sahrens 	if ((loc = strrchr(path, '/')) == NULL)
2675789Sahrens 		return (-1);
2676789Sahrens 
2677789Sahrens 	(void) strncpy(buf, path, MIN(buflen, loc - path));
2678789Sahrens 	buf[loc - path] = '\0';
2679789Sahrens 
2680789Sahrens 	return (0);
2681789Sahrens }
2682789Sahrens 
2683789Sahrens /*
26844490Svb160487  * If accept_ancestor is false, then check to make sure that the given path has
26854490Svb160487  * a parent, and that it exists.  If accept_ancestor is true, then find the
26864490Svb160487  * closest existing ancestor for the given path.  In prefixlen return the
26874490Svb160487  * length of already existing prefix of the given path.  We also fetch the
26884490Svb160487  * 'zoned' property, which is used to validate property settings when creating
26894490Svb160487  * new datasets.
2690789Sahrens  */
2691789Sahrens static int
26924490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
26934490Svb160487     boolean_t accept_ancestor, int *prefixlen)
2694789Sahrens {
2695789Sahrens 	zfs_cmd_t zc = { 0 };
2696789Sahrens 	char parent[ZFS_MAXNAMELEN];
2697789Sahrens 	char *slash;
26981356Seschrock 	zfs_handle_t *zhp;
26992082Seschrock 	char errbuf[1024];
27002082Seschrock 
27012082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
27022082Seschrock 	    path);
2703789Sahrens 
2704789Sahrens 	/* get parent, and check to see if this is just a pool */
2705789Sahrens 	if (parent_name(path, parent, sizeof (parent)) != 0) {
27062082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27072082Seschrock 		    "missing dataset name"));
27082082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2709789Sahrens 	}
2710789Sahrens 
2711789Sahrens 	/* check to see if the pool exists */
2712789Sahrens 	if ((slash = strchr(parent, '/')) == NULL)
2713789Sahrens 		slash = parent + strlen(parent);
2714789Sahrens 	(void) strncpy(zc.zc_name, parent, slash - parent);
2715789Sahrens 	zc.zc_name[slash - parent] = '\0';
27162082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2717789Sahrens 	    errno == ENOENT) {
27182082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27192082Seschrock 		    "no such pool '%s'"), zc.zc_name);
27202082Seschrock 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2721789Sahrens 	}
2722789Sahrens 
2723789Sahrens 	/* check to see if the parent dataset exists */
27244490Svb160487 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
27254490Svb160487 		if (errno == ENOENT && accept_ancestor) {
27264490Svb160487 			/*
27274490Svb160487 			 * Go deeper to find an ancestor, give up on top level.
27284490Svb160487 			 */
27294490Svb160487 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
27304490Svb160487 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27314490Svb160487 				    "no such pool '%s'"), zc.zc_name);
27324490Svb160487 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
27334490Svb160487 			}
27344490Svb160487 		} else if (errno == ENOENT) {
27352082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27362082Seschrock 			    "parent does not exist"));
27372082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
27384490Svb160487 		} else
27392082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
2740789Sahrens 	}
2741789Sahrens 
27422676Seschrock 	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2743789Sahrens 	/* we are in a non-global zone, but parent is in the global zone */
27442676Seschrock 	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
27452082Seschrock 		(void) zfs_standard_error(hdl, EPERM, errbuf);
27461356Seschrock 		zfs_close(zhp);
2747789Sahrens 		return (-1);
2748789Sahrens 	}
2749789Sahrens 
2750789Sahrens 	/* make sure parent is a filesystem */
27511356Seschrock 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
27522082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27532082Seschrock 		    "parent is not a filesystem"));
27542082Seschrock 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
27551356Seschrock 		zfs_close(zhp);
2756789Sahrens 		return (-1);
2757789Sahrens 	}
2758789Sahrens 
27591356Seschrock 	zfs_close(zhp);
27604490Svb160487 	if (prefixlen != NULL)
27614490Svb160487 		*prefixlen = strlen(parent);
27624490Svb160487 	return (0);
27634490Svb160487 }
27644490Svb160487 
27654490Svb160487 /*
27664490Svb160487  * Finds whether the dataset of the given type(s) exists.
27674490Svb160487  */
27684490Svb160487 boolean_t
27694490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
27704490Svb160487 {
27714490Svb160487 	zfs_handle_t *zhp;
27724490Svb160487 
27735326Sek110237 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
27744490Svb160487 		return (B_FALSE);
27754490Svb160487 
27764490Svb160487 	/*
27774490Svb160487 	 * Try to get stats for the dataset, which will tell us if it exists.
27784490Svb160487 	 */
27794490Svb160487 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
27804490Svb160487 		int ds_type = zhp->zfs_type;
27814490Svb160487 
27824490Svb160487 		zfs_close(zhp);
27834490Svb160487 		if (types & ds_type)
27844490Svb160487 			return (B_TRUE);
27854490Svb160487 	}
27864490Svb160487 	return (B_FALSE);
27874490Svb160487 }
27884490Svb160487 
27894490Svb160487 /*
27905367Sahrens  * Given a path to 'target', create all the ancestors between
27915367Sahrens  * the prefixlen portion of the path, and the target itself.
27925367Sahrens  * Fail if the initial prefixlen-ancestor does not already exist.
27935367Sahrens  */
27945367Sahrens int
27955367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
27965367Sahrens {
27975367Sahrens 	zfs_handle_t *h;
27985367Sahrens 	char *cp;
27995367Sahrens 	const char *opname;
28005367Sahrens 
28015367Sahrens 	/* make sure prefix exists */
28025367Sahrens 	cp = target + prefixlen;
28035367Sahrens 	if (*cp != '/') {
28045367Sahrens 		assert(strchr(cp, '/') == NULL);
28055367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28065367Sahrens 	} else {
28075367Sahrens 		*cp = '\0';
28085367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28095367Sahrens 		*cp = '/';
28105367Sahrens 	}
28115367Sahrens 	if (h == NULL)
28125367Sahrens 		return (-1);
28135367Sahrens 	zfs_close(h);
28145367Sahrens 
28155367Sahrens 	/*
28165367Sahrens 	 * Attempt to create, mount, and share any ancestor filesystems,
28175367Sahrens 	 * up to the prefixlen-long one.
28185367Sahrens 	 */
28195367Sahrens 	for (cp = target + prefixlen + 1;
28205367Sahrens 	    cp = strchr(cp, '/'); *cp = '/', cp++) {
28215367Sahrens 		char *logstr;
28225367Sahrens 
28235367Sahrens 		*cp = '\0';
28245367Sahrens 
28255367Sahrens 		h = make_dataset_handle(hdl, target);
28265367Sahrens 		if (h) {
28275367Sahrens 			/* it already exists, nothing to do here */
28285367Sahrens 			zfs_close(h);
28295367Sahrens 			continue;
28305367Sahrens 		}
28315367Sahrens 
28325367Sahrens 		logstr = hdl->libzfs_log_str;
28335367Sahrens 		hdl->libzfs_log_str = NULL;
28345367Sahrens 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
28355367Sahrens 		    NULL) != 0) {
28365367Sahrens 			hdl->libzfs_log_str = logstr;
28375367Sahrens 			opname = dgettext(TEXT_DOMAIN, "create");
28385367Sahrens 			goto ancestorerr;
28395367Sahrens 		}
28405367Sahrens 
28415367Sahrens 		hdl->libzfs_log_str = logstr;
28425367Sahrens 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
28435367Sahrens 		if (h == NULL) {
28445367Sahrens 			opname = dgettext(TEXT_DOMAIN, "open");
28455367Sahrens 			goto ancestorerr;
28465367Sahrens 		}
28475367Sahrens 
28485367Sahrens 		if (zfs_mount(h, NULL, 0) != 0) {
28495367Sahrens 			opname = dgettext(TEXT_DOMAIN, "mount");
28505367Sahrens 			goto ancestorerr;
28515367Sahrens 		}
28525367Sahrens 
28535367Sahrens 		if (zfs_share(h) != 0) {
28545367Sahrens 			opname = dgettext(TEXT_DOMAIN, "share");
28555367Sahrens 			goto ancestorerr;
28565367Sahrens 		}
28575367Sahrens 
28585367Sahrens 		zfs_close(h);
28595367Sahrens 	}
28605367Sahrens 
28615367Sahrens 	return (0);
28625367Sahrens 
28635367Sahrens ancestorerr:
28645367Sahrens 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
28655367Sahrens 	    "failed to %s ancestor '%s'"), opname, target);
28665367Sahrens 	return (-1);
28675367Sahrens }
28685367Sahrens 
28695367Sahrens /*
28704490Svb160487  * Creates non-existing ancestors of the given path.
28714490Svb160487  */
28724490Svb160487 int
28734490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
28744490Svb160487 {
28754490Svb160487 	int prefix;
28764490Svb160487 	uint64_t zoned;
28774490Svb160487 	char *path_copy;
28784490Svb160487 	int rc;
28794490Svb160487 
28804490Svb160487 	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
28814490Svb160487 		return (-1);
28824490Svb160487 
28834490Svb160487 	if ((path_copy = strdup(path)) != NULL) {
28844490Svb160487 		rc = create_parents(hdl, path_copy, prefix);
28854490Svb160487 		free(path_copy);
28864490Svb160487 	}
28874490Svb160487 	if (path_copy == NULL || rc != 0)
28884490Svb160487 		return (-1);
28894490Svb160487 
2890789Sahrens 	return (0);
2891789Sahrens }
2892789Sahrens 
2893789Sahrens /*
28942676Seschrock  * Create a new filesystem or volume.
2895789Sahrens  */
2896789Sahrens int
28972082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
28982676Seschrock     nvlist_t *props)
2899789Sahrens {
2900789Sahrens 	zfs_cmd_t zc = { 0 };
2901789Sahrens 	int ret;
2902789Sahrens 	uint64_t size = 0;
2903789Sahrens 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
29042082Seschrock 	char errbuf[1024];
29052676Seschrock 	uint64_t zoned;
29062082Seschrock 
29072082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
29082082Seschrock 	    "cannot create '%s'"), path);
2909789Sahrens 
2910789Sahrens 	/* validate the path, taking care to note the extended error message */
29115326Sek110237 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
29122082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2913789Sahrens 
2914789Sahrens 	/* validate parents exist */
29154490Svb160487 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2916789Sahrens 		return (-1);
2917789Sahrens 
2918789Sahrens 	/*
2919789Sahrens 	 * The failure modes when creating a dataset of a different type over
2920789Sahrens 	 * one that already exists is a little strange.  In particular, if you
2921789Sahrens 	 * try to create a dataset on top of an existing dataset, the ioctl()
2922789Sahrens 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
2923789Sahrens 	 * first try to see if the dataset exists.
2924789Sahrens 	 */
2925789Sahrens 	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
29265094Slling 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
29272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29282082Seschrock 		    "dataset already exists"));
29292082Seschrock 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2930789Sahrens 	}
2931789Sahrens 
2932789Sahrens 	if (type == ZFS_TYPE_VOLUME)
2933789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
2934789Sahrens 	else
2935789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
2936789Sahrens 
2937*7184Stimh 	if (props && (props = zfs_valid_proplist(hdl, type, props,
29383912Slling 	    zoned, NULL, errbuf)) == 0)
29392676Seschrock 		return (-1);
29402676Seschrock 
2941789Sahrens 	if (type == ZFS_TYPE_VOLUME) {
29421133Seschrock 		/*
29431133Seschrock 		 * If we are creating a volume, the size and block size must
29441133Seschrock 		 * satisfy a few restraints.  First, the blocksize must be a
29451133Seschrock 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
29461133Seschrock 		 * volsize must be a multiple of the block size, and cannot be
29471133Seschrock 		 * zero.
29481133Seschrock 		 */
29492676Seschrock 		if (props == NULL || nvlist_lookup_uint64(props,
29502676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
29512676Seschrock 			nvlist_free(props);
29522082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29532676Seschrock 			    "missing volume size"));
29542676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2955789Sahrens 		}
2956789Sahrens 
29572676Seschrock 		if ((ret = nvlist_lookup_uint64(props,
29582676Seschrock 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
29592676Seschrock 		    &blocksize)) != 0) {
29602676Seschrock 			if (ret == ENOENT) {
29612676Seschrock 				blocksize = zfs_prop_default_numeric(
29622676Seschrock 				    ZFS_PROP_VOLBLOCKSIZE);
29632676Seschrock 			} else {
29642676Seschrock 				nvlist_free(props);
29652676Seschrock 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29662676Seschrock 				    "missing volume block size"));
29672676Seschrock 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29682676Seschrock 			}
29692676Seschrock 		}
29702676Seschrock 
29712676Seschrock 		if (size == 0) {
29722676Seschrock 			nvlist_free(props);
29732082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29742676Seschrock 			    "volume size cannot be zero"));
29752676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29761133Seschrock 		}
29771133Seschrock 
29781133Seschrock 		if (size % blocksize != 0) {
29792676Seschrock 			nvlist_free(props);
29802082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29812676Seschrock 			    "volume size must be a multiple of volume block "
29822676Seschrock 			    "size"));
29832676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
29841133Seschrock 		}
2985789Sahrens 	}
2986789Sahrens 
29875094Slling 	if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
29882676Seschrock 		return (-1);
29892676Seschrock 	nvlist_free(props);
29902676Seschrock 
2991789Sahrens 	/* create the dataset */
29924543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2993789Sahrens 
29943912Slling 	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
29952082Seschrock 		ret = zvol_create_link(hdl, path);
29963912Slling 		if (ret) {
29973912Slling 			(void) zfs_standard_error(hdl, errno,
29983912Slling 			    dgettext(TEXT_DOMAIN,
29993912Slling 			    "Volume successfully created, but device links "
30003912Slling 			    "were not created"));
30013912Slling 			zcmd_free_nvlists(&zc);
30023912Slling 			return (-1);
30033912Slling 		}
30043912Slling 	}
3005789Sahrens 
30062676Seschrock 	zcmd_free_nvlists(&zc);
30072676Seschrock 
3008789Sahrens 	/* check for failure */
3009789Sahrens 	if (ret != 0) {
3010789Sahrens 		char parent[ZFS_MAXNAMELEN];
3011789Sahrens 		(void) parent_name(path, parent, sizeof (parent));
3012789Sahrens 
3013789Sahrens 		switch (errno) {
3014789Sahrens 		case ENOENT:
30152082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30162082Seschrock 			    "no such parent '%s'"), parent);
30172082Seschrock 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3018789Sahrens 
3019789Sahrens 		case EINVAL:
30202082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30213413Smmusante 			    "parent '%s' is not a filesystem"), parent);
30222082Seschrock 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3023789Sahrens 
3024789Sahrens 		case EDOM:
30252082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30262676Seschrock 			    "volume block size must be power of 2 from "
30272676Seschrock 			    "%u to %uk"),
3028789Sahrens 			    (uint_t)SPA_MINBLOCKSIZE,
3029789Sahrens 			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
30302082Seschrock 
30312676Seschrock 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
30322082Seschrock 
30334603Sahrens 		case ENOTSUP:
30344603Sahrens 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
30354603Sahrens 			    "pool must be upgraded to set this "
30364603Sahrens 			    "property or value"));
30374603Sahrens 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3038789Sahrens #ifdef _ILP32
3039789Sahrens 		case EOVERFLOW:
3040789Sahrens 			/*
3041789Sahrens 			 * This platform can't address a volume this big.
3042789Sahrens 			 */
30432082Seschrock 			if (type == ZFS_TYPE_VOLUME)
30442082Seschrock 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
30452082Seschrock 				    errbuf));
3046789Sahrens #endif
30472082Seschrock 			/* FALLTHROUGH */
3048789Sahrens 		default:
30492082Seschrock 			return (zfs_standard_error(hdl, errno, errbuf));
3050789Sahrens 		}
3051789Sahrens 	}
3052789Sahrens 
3053789Sahrens 	return (0);
3054789Sahrens }
3055789Sahrens 
3056789Sahrens /*
3057789Sahrens  * Destroys the given dataset.  The caller must make sure that the filesystem
3058789Sahrens  * isn't mounted, and that there are no active dependents.
3059789Sahrens  */
3060789Sahrens int
3061789Sahrens zfs_destroy(zfs_handle_t *zhp)
3062789Sahrens {
3063789Sahrens 	zfs_cmd_t zc = { 0 };
3064789Sahrens 
3065789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3066789Sahrens 
30672676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
30683126Sahl 		/*
30694543Smarks 		 * If user doesn't have permissions to unshare volume, then
30704543Smarks 		 * abort the request.  This would only happen for a
30714543Smarks 		 * non-privileged user.
30723126Sahl 		 */
30734543Smarks 		if (zfs_unshare_iscsi(zhp) != 0) {
30744543Smarks 			return (-1);
30754543Smarks 		}
30763126Sahl 
30772082Seschrock 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3078789Sahrens 			return (-1);
3079789Sahrens 
3080789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3081789Sahrens 	} else {
3082789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3083789Sahrens 	}
3084789Sahrens 
30854543Smarks 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
30863237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
30872082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
30882082Seschrock 		    zhp->zfs_name));
30892199Sahrens 	}
3090789Sahrens 
3091789Sahrens 	remove_mountpoint(zhp);
3092789Sahrens 
3093789Sahrens 	return (0);
3094789Sahrens }
3095789Sahrens 
30962199Sahrens struct destroydata {
30972199Sahrens 	char *snapname;
30982199Sahrens 	boolean_t gotone;
30993265Sahrens 	boolean_t closezhp;
31002199Sahrens };
31012199Sahrens 
31022199Sahrens static int
31032199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
31042199Sahrens {
31052199Sahrens 	struct destroydata *dd = arg;
31062199Sahrens 	zfs_handle_t *szhp;
31072199Sahrens 	char name[ZFS_MAXNAMELEN];
31083265Sahrens 	boolean_t closezhp = dd->closezhp;
31093265Sahrens 	int rv;
31102199Sahrens 
31112676Seschrock 	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
31122676Seschrock 	(void) strlcat(name, "@", sizeof (name));
31132676Seschrock 	(void) strlcat(name, dd->snapname, sizeof (name));
31142199Sahrens 
31152199Sahrens 	szhp = make_dataset_handle(zhp->zfs_hdl, name);
31162199Sahrens 	if (szhp) {
31172199Sahrens 		dd->gotone = B_TRUE;
31182199Sahrens 		zfs_close(szhp);
31192199Sahrens 	}
31202199Sahrens 
31212199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
31222199Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, name);
31232199Sahrens 		/*
31242199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
31252199Sahrens 		 * return an error, because then we wouldn't visit all
31262199Sahrens 		 * the volumes.
31272199Sahrens 		 */
31282199Sahrens 	}
31292199Sahrens 
31303265Sahrens 	dd->closezhp = B_TRUE;
31313265Sahrens 	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
31323265Sahrens 	if (closezhp)
31333265Sahrens 		zfs_close(zhp);
31343265Sahrens 	return (rv);
31352199Sahrens }
31362199Sahrens 
31372199Sahrens /*
31382199Sahrens  * Destroys all snapshots with the given name in zhp & descendants.
31392199Sahrens  */
31402199Sahrens int
31412199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
31422199Sahrens {
31432199Sahrens 	zfs_cmd_t zc = { 0 };
31442199Sahrens 	int ret;
31452199Sahrens 	struct destroydata dd = { 0 };
31462199Sahrens 
31472199Sahrens 	dd.snapname = snapname;
31482199Sahrens 	(void) zfs_remove_link_cb(zhp, &dd);
31492199Sahrens 
31502199Sahrens 	if (!dd.gotone) {
31513237Slling 		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
31522199Sahrens 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
31532199Sahrens 		    zhp->zfs_name, snapname));
31542199Sahrens 	}
31552199Sahrens 
31562199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
31572676Seschrock 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
31582199Sahrens 
31594543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
31602199Sahrens 	if (ret != 0) {
31612199Sahrens 		char errbuf[1024];
31622199Sahrens 
31632199Sahrens 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31642199Sahrens 		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
31652199Sahrens 
31662199Sahrens 		switch (errno) {
31672199Sahrens 		case EEXIST:
31682199Sahrens 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
31692199Sahrens 			    "snapshot is cloned"));
31702199Sahrens 			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
31712199Sahrens 
31722199Sahrens 		default:
31732199Sahrens 			return (zfs_standard_error(zhp->zfs_hdl, errno,
31742199Sahrens 			    errbuf));
31752199Sahrens 		}
31762199Sahrens 	}
31772199Sahrens 
31782199Sahrens 	return (0);
31792199Sahrens }
31802199Sahrens 
3181789Sahrens /*
3182789Sahrens  * Clones the given dataset.  The target must be of the same type as the source.
3183789Sahrens  */
3184789Sahrens int
31852676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3186789Sahrens {
3187789Sahrens 	zfs_cmd_t zc = { 0 };
3188789Sahrens 	char parent[ZFS_MAXNAMELEN];
3189789Sahrens 	int ret;
31902082Seschrock 	char errbuf[1024];
31912082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
31922676Seschrock 	zfs_type_t type;
31932676Seschrock 	uint64_t zoned;
3194789Sahrens 
3195789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3196789Sahrens 
31972082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31982082Seschrock 	    "cannot create '%s'"), target);
31992082Seschrock 
3200789Sahrens 	/* validate the target name */
32015326Sek110237 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
32022082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3203789Sahrens 
3204789Sahrens 	/* validate parents exist */
32054490Svb160487 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3206789Sahrens 		return (-1);
3207789Sahrens 
3208789Sahrens 	(void) parent_name(target, parent, sizeof (parent));
3209789Sahrens 
3210789Sahrens 	/* do the clone */
32112676Seschrock 	if (ZFS_IS_VOLUME(zhp)) {
3212789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
32132744Snn35248 		type = ZFS_TYPE_VOLUME;
32142676Seschrock 	} else {
3215789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
32162744Snn35248 		type = ZFS_TYPE_FILESYSTEM;
32172676Seschrock 	}
32182676Seschrock 
32192676Seschrock 	if (props) {
3220*7184Stimh 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3221*7184Stimh 		    zhp, errbuf)) == NULL)
32222676Seschrock 			return (-1);
32232676Seschrock 
32245094Slling 		if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
32252676Seschrock 			nvlist_free(props);
32262676Seschrock 			return (-1);
32272676Seschrock 		}
32282676Seschrock 
32292676Seschrock 		nvlist_free(props);
32302676Seschrock 	}
3231789Sahrens 
3232789Sahrens 	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
32332676Seschrock 	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
32344543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3235789Sahrens 
32362676Seschrock 	zcmd_free_nvlists(&zc);
32372676Seschrock 
3238789Sahrens 	if (ret != 0) {
3239789Sahrens 		switch (errno) {
3240789Sahrens 
3241789Sahrens 		case ENOENT:
3242789Sahrens 			/*
3243789Sahrens 			 * The parent doesn't exist.  We should have caught this
3244789Sahrens 			 * above, but there may a race condition that has since
3245789Sahrens 			 * destroyed the parent.
3246789Sahrens 			 *
3247789Sahrens 			 * At this point, we don't know whether it's the source
3248789Sahrens 			 * that doesn't exist anymore, or whether the target
3249789Sahrens 			 * dataset doesn't exist.
3250789Sahrens 			 */
32512082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32522082Seschrock 			    "no such parent '%s'"), parent);
32532082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
32542082Seschrock 
32552082Seschrock 		case EXDEV:
32562082Seschrock 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
32572082Seschrock 			    "source and target pools differ"));
32582082Seschrock 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
32592082Seschrock 			    errbuf));
32602082Seschrock 
32612082Seschrock 		default:
32622082Seschrock 			return (zfs_standard_error(zhp->zfs_hdl, errno,
32632082Seschrock 			    errbuf));
32642082Seschrock 		}
32652676Seschrock 	} else if (ZFS_IS_VOLUME(zhp)) {
32662082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, target);
32672082Seschrock 	}
32682082Seschrock 
32692082Seschrock 	return (ret);
32702082Seschrock }
32712082Seschrock 
32722082Seschrock typedef struct promote_data {
32732082Seschrock 	char cb_mountpoint[MAXPATHLEN];
32742082Seschrock 	const char *cb_target;
32752082Seschrock 	const char *cb_errbuf;
32762082Seschrock 	uint64_t cb_pivot_txg;
32772082Seschrock } promote_data_t;
32782082Seschrock 
32792082Seschrock static int
32802082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data)
32812082Seschrock {
32822082Seschrock 	promote_data_t *pd = data;
32832082Seschrock 	zfs_handle_t *szhp;
32842082Seschrock 	char snapname[MAXPATHLEN];
32853265Sahrens 	int rv = 0;
32862082Seschrock 
32872082Seschrock 	/* We don't care about snapshots after the pivot point */
32883265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
32893265Sahrens 		zfs_close(zhp);
32902082Seschrock 		return (0);
32913265Sahrens 	}
32922082Seschrock 
32932417Sahrens 	/* Remove the device link if it's a zvol. */
32942676Seschrock 	if (ZFS_IS_VOLUME(zhp))
32952417Sahrens 		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
32962082Seschrock 
32972082Seschrock 	/* Check for conflicting names */
32982676Seschrock 	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
32992676Seschrock 	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
33002082Seschrock 	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
33012082Seschrock 	if (szhp != NULL) {
33022082Seschrock 		zfs_close(szhp);
33032082Seschrock 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
33042082Seschrock 		    "snapshot name '%s' from origin \n"
33052082Seschrock 		    "conflicts with '%s' from target"),
33062082Seschrock 		    zhp->zfs_name, snapname);
33073265Sahrens 		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
33082082Seschrock 	}
33093265Sahrens 	zfs_close(zhp);
33103265Sahrens 	return (rv);
33112082Seschrock }
33122082Seschrock 
33132417Sahrens static int
33142417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data)
33152417Sahrens {
33162417Sahrens 	promote_data_t *pd = data;
33172417Sahrens 
33182417Sahrens 	/* We don't care about snapshots after the pivot point */
33193265Sahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
33203265Sahrens 		/* Create the device link if it's a zvol. */
33213265Sahrens 		if (ZFS_IS_VOLUME(zhp))
33223265Sahrens 			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
33233265Sahrens 	}
33243265Sahrens 
33253265Sahrens 	zfs_close(zhp);
33262417Sahrens 	return (0);
33272417Sahrens }
33282417Sahrens 
33292082Seschrock /*
33302082Seschrock  * Promotes the given clone fs to be the clone parent.
33312082Seschrock  */
33322082Seschrock int
33332082Seschrock zfs_promote(zfs_handle_t *zhp)
33342082Seschrock {
33352082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
33362082Seschrock 	zfs_cmd_t zc = { 0 };
33372082Seschrock 	char parent[MAXPATHLEN];
33382082Seschrock 	char *cp;
33392082Seschrock 	int ret;
33402082Seschrock 	zfs_handle_t *pzhp;
33412082Seschrock 	promote_data_t pd;
33422082Seschrock 	char errbuf[1024];
33432082Seschrock 
33442082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33452082Seschrock 	    "cannot promote '%s'"), zhp->zfs_name);
33462082Seschrock 
33472082Seschrock 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
33482082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33492082Seschrock 		    "snapshots can not be promoted"));
33502082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33512082Seschrock 	}
33522082Seschrock 
33535367Sahrens 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
33542082Seschrock 	if (parent[0] == '\0') {
33552082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33562082Seschrock 		    "not a cloned filesystem"));
33572082Seschrock 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
33582082Seschrock 	}
33592082Seschrock 	cp = strchr(parent, '@');
33602082Seschrock 	*cp = '\0';
33612082Seschrock 
33622082Seschrock 	/* Walk the snapshots we will be moving */
33635367Sahrens 	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
33642082Seschrock 	if (pzhp == NULL)
33652082Seschrock 		return (-1);
33662082Seschrock 	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
33672082Seschrock 	zfs_close(pzhp);
33682082Seschrock 	pd.cb_target = zhp->zfs_name;
33692082Seschrock 	pd.cb_errbuf = errbuf;
33705094Slling 	pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
33712082Seschrock 	if (pzhp == NULL)
33722082Seschrock 		return (-1);
33732082Seschrock 	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
33742082Seschrock 	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
33752082Seschrock 	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
33762417Sahrens 	if (ret != 0) {
33772417Sahrens 		zfs_close(pzhp);
33782082Seschrock 		return (-1);
33792417Sahrens 	}
33802082Seschrock 
33812082Seschrock 	/* issue the ioctl */
33825367Sahrens 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
33832676Seschrock 	    sizeof (zc.zc_value));
33842082Seschrock 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
33854543Smarks 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
33862082Seschrock 
33872082Seschrock 	if (ret != 0) {
33882417Sahrens 		int save_errno = errno;
33892417Sahrens 
33902417Sahrens 		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
33912417Sahrens 		zfs_close(pzhp);
33922417Sahrens 
33932417Sahrens 		switch (save_errno) {
3394789Sahrens 		case EEXIST:
3395789Sahrens 			/*
33962082Seschrock 			 * There is a conflicting snapshot name.  We
33972082Seschrock 			 * should have caught this above, but they could
33982082Seschrock 			 * have renamed something in the mean time.
3399789Sahrens 			 */
34002082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34012082Seschrock 			    "conflicting snapshot name from parent '%s'"),
34022082Seschrock 			    parent);
34032082Seschrock 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3404789Sahrens 
3405789Sahrens 		default:
34062417Sahrens 			return (zfs_standard_error(hdl, save_errno, errbuf));
3407789Sahrens 		}
34082417Sahrens 	} else {
34092417Sahrens 		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3410789Sahrens 	}
3411789Sahrens 
34122417Sahrens 	zfs_close(pzhp);
3413789Sahrens 	return (ret);
3414789Sahrens }
3415789Sahrens 
34164007Smmusante struct createdata {
34174007Smmusante 	const char *cd_snapname;
34184007Smmusante 	int cd_ifexists;
34194007Smmusante };
34204007Smmusante 
34212199Sahrens static int
34222199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
34232199Sahrens {
34244007Smmusante 	struct createdata *cd = arg;
34252676Seschrock 	int ret;
34262199Sahrens 
34272199Sahrens 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
34282199Sahrens 		char name[MAXPATHLEN];
34292199Sahrens 
34302676Seschrock 		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
34312676Seschrock 		(void) strlcat(name, "@", sizeof (name));
34324007Smmusante 		(void) strlcat(name, cd->cd_snapname, sizeof (name));
34334007Smmusante 		(void) zvol_create_link_common(zhp->zfs_hdl, name,
34344007Smmusante 		    cd->cd_ifexists);
34352199Sahrens 		/*
34362199Sahrens 		 * NB: this is simply a best-effort.  We don't want to
34372199Sahrens 		 * return an error, because then we wouldn't visit all
34382199Sahrens 		 * the volumes.
34392199Sahrens 		 */
34402199Sahrens 	}
34412676Seschrock 
34424007Smmusante 	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
34432676Seschrock 
34442676Seschrock 	zfs_close(zhp);
34452676Seschrock 
34462676Seschrock 	return (ret);
34472199Sahrens }
34482199Sahrens 
3449789Sahrens /*
34503504Sahl  * Takes a snapshot of the given dataset.
3451789Sahrens  */
3452789Sahrens int
34532199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
3454789Sahrens {
3455789Sahrens 	const char *delim;
3456789Sahrens 	char *parent;
3457789Sahrens 	zfs_handle_t *zhp;
3458789Sahrens 	zfs_cmd_t zc = { 0 };
3459789Sahrens 	int ret;
34602082Seschrock 	char errbuf[1024];
34612082Seschrock 
34622082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34632082Seschrock 	    "cannot snapshot '%s'"), path);
34642082Seschrock 
34652082Seschrock 	/* validate the target name */
34665326Sek110237 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
34672082Seschrock 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3468789Sahrens 
3469789Sahrens 	/* make sure the parent exists and is of the appropriate type */
34702199Sahrens 	delim = strchr(path, '@');
34712082Seschrock 	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
34722082Seschrock 		return (-1);
3473789Sahrens 	(void) strncpy(parent, path, delim - path);
3474789Sahrens 	parent[delim - path] = '\0';
3475789Sahrens 
34762082Seschrock 	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3477789Sahrens 	    ZFS_TYPE_VOLUME)) == NULL) {
3478789Sahrens 		free(parent);
3479789Sahrens 		return (-1);
3480789Sahrens 	}
3481789Sahrens 
34822199Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34832676Seschrock 	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
34844543Smarks 	if (ZFS_IS_VOLUME(zhp))
34854543Smarks 		zc.zc_objset_type = DMU_OST_ZVOL;
34864543Smarks 	else
34874543Smarks 		zc.zc_objset_type = DMU_OST_ZFS;
34882199Sahrens 	zc.zc_cookie = recursive;
34894543Smarks 	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
34902199Sahrens 
34912199Sahrens 	/*
34922199Sahrens 	 * if it was recursive, the one that actually failed will be in
34932199Sahrens 	 * zc.zc_name.
34942199Sahrens 	 */
34954543Smarks 	if (ret != 0)
34964543Smarks 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
34974543Smarks 		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
34984543Smarks 
34992199Sahrens 	if (ret == 0 && recursive) {
35004007Smmusante 		struct createdata cd;
35014007Smmusante 
35024007Smmusante 		cd.cd_snapname = delim + 1;
35034007Smmusante 		cd.cd_ifexists = B_FALSE;
35044007Smmusante 		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
35052199Sahrens 	}
3506789Sahrens 	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
35072082Seschrock 		ret = zvol_create_link(zhp->zfs_hdl, path);
35082199Sahrens 		if (ret != 0) {
35094543Smarks 			(void) zfs_standard_error(hdl, errno,
35104543Smarks 			    dgettext(TEXT_DOMAIN,
35114543Smarks 			    "Volume successfully snapshotted, but device links "
35124543Smarks 			    "were not created"));
35134543Smarks 			free(parent);
35144543Smarks 			zfs_close(zhp);
35154543Smarks 			return (-1);
35162199Sahrens 		}
3517789Sahrens 	}
3518789Sahrens 
35192082Seschrock 	if (ret != 0)
35202082Seschrock 		(void) zfs_standard_error(hdl, errno, errbuf);
3521789Sahrens 
3522789Sahrens 	free(parent);
3523789Sahrens 	zfs_close(zhp);
3524789Sahrens 
3525789Sahrens 	return (ret);
3526789Sahrens }
3527789Sahrens 
3528789Sahrens /*
35291294Slling  * Destroy any more recent snapshots.  We invoke this callback on any dependents
35301294Slling  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
35311294Slling  * is a dependent and we should just destroy it without checking the transaction
35321294Slling  * group.
3533789Sahrens  */
35341294Slling typedef struct rollback_data {
35351294Slling 	const char	*cb_target;		/* the snapshot */
35361294Slling 	uint64_t	cb_create;		/* creation time reference */
35375749Sahrens 	boolean_t	cb_error;
35382082Seschrock 	boolean_t	cb_dependent;
35395749Sahrens 	boolean_t	cb_force;
35401294Slling } rollback_data_t;
35411294Slling 
35421294Slling static int
35431294Slling rollback_destroy(zfs_handle_t *zhp, void *data)
35441294Slling {
35451294Slling 	rollback_data_t *cbp = data;
35461294Slling 
35471294Slling 	if (!cbp->cb_dependent) {
35481294Slling 		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
35491294Slling 		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
35501294Slling 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
35511294Slling 		    cbp->cb_create) {
35524543Smarks 			char *logstr;
35531294Slling 
35542082Seschrock 			cbp->cb_dependent = B_TRUE;
35555446Sahrens 			cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
35565446Sahrens 			    rollback_destroy, cbp);
35572082Seschrock 			cbp->cb_dependent = B_FALSE;
35581294Slling 
35594543Smarks 			logstr = zhp->zfs_hdl->libzfs_log_str;
35604543Smarks 			zhp->zfs_hdl->libzfs_log_str = NULL;
35615446Sahrens 			cbp->cb_error |= zfs_destroy(zhp);
35624543Smarks 			zhp->zfs_hdl->libzfs_log_str = logstr;
35631294Slling 		}
35641294Slling 	} else {
35655749Sahrens 		/* We must destroy this clone; first unmount it */
35665749Sahrens 		prop_changelist_t *clp;
35675749Sahrens 
35685749Sahrens 		clp = changelist_gather(zhp, ZFS_PROP_NAME,
35695749Sahrens 		    cbp->cb_force ? MS_FORCE: 0);
35705749Sahrens 		if (clp == NULL || changelist_prefix(clp) != 0) {
35715749Sahrens 			cbp->cb_error = B_TRUE;
35725749Sahrens 			zfs_close(zhp);
35735749Sahrens 			return (0);
35745749Sahrens 		}
35755749Sahrens 		if (zfs_destroy(zhp) != 0)
35765749Sahrens 			cbp->cb_error = B_TRUE;
35775749Sahrens 		else
35785749Sahrens 			changelist_remove(clp, zhp->zfs_name);
35795751Sahrens 		(void) changelist_postfix(clp);
35805749Sahrens 		changelist_free(clp);
35811294Slling 	}
35821294Slling 
35831294Slling 	zfs_close(zhp);
35841294Slling 	return (0);
35851294Slling }
35861294Slling 
35871294Slling /*
35885446Sahrens  * Given a dataset, rollback to a specific snapshot, discarding any
35895446Sahrens  * data changes since then and making it the active dataset.
35905446Sahrens  *
35915446Sahrens  * Any snapshots more recent than the target are destroyed, along with
35925446Sahrens  * their dependents.
35931294Slling  */
35945446Sahrens int
35955749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3596789Sahrens {
35975446Sahrens 	rollback_data_t cb = { 0 };
35985446Sahrens 	int err;
3599789Sahrens 	zfs_cmd_t zc = { 0 };
36005713Srm160521 	boolean_t restore_resv = 0;
36015713Srm160521 	uint64_t old_volsize, new_volsize;
36025713Srm160521 	zfs_prop_t resv_prop;
3603789Sahrens 
3604789Sahrens 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3605789Sahrens 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
3606789Sahrens 
36075446Sahrens 	/*
36085446Sahrens 	 * Destroy all recent snapshots and its dependends.
36095446Sahrens 	 */
36105749Sahrens 	cb.cb_force = force;
36115446Sahrens 	cb.cb_target = snap->zfs_name;
36125446Sahrens 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
36135446Sahrens 	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
36145446Sahrens 
36155749Sahrens 	if (cb.cb_error)
36165749Sahrens 		return (-1);
36175446Sahrens 
36185446Sahrens 	/*
36195446Sahrens 	 * Now that we have verified that the snapshot is the latest,
36205446Sahrens 	 * rollback to the given snapshot.
36215446Sahrens 	 */
36225446Sahrens 
36235713Srm160521 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
36245713Srm160521 		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
36255713Srm160521 			return (-1);
36265713Srm160521 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
36275713Srm160521 			return (-1);
36285713Srm160521 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36295713Srm160521 		restore_resv =
36305713Srm160521 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
36315713Srm160521 	}
3632789Sahrens 
3633789Sahrens 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3634789Sahrens 
36352676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3636789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3637789Sahrens 	else
3638789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3639789Sahrens 
3640789Sahrens 	/*
36415446Sahrens 	 * We rely on zfs_iter_children() to verify that there are no
36425446Sahrens 	 * newer snapshots for the given dataset.  Therefore, we can
36435446Sahrens 	 * simply pass the name on to the ioctl() call.  There is still
36445446Sahrens 	 * an unlikely race condition where the user has taken a
36455446Sahrens 	 * snapshot since we verified that this was the most recent.
36465713Srm160521 	 *
3647789Sahrens 	 */
36485446Sahrens 	if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
36493237Slling 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
36502082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
36512082Seschrock 		    zhp->zfs_name);
36525717Srm160521 		return (err);
36535717Srm160521 	}
36545713Srm160521 
36555713Srm160521 	/*
36565713Srm160521 	 * For volumes, if the pre-rollback volsize matched the pre-
36575713Srm160521 	 * rollback reservation and the volsize has changed then set
36585713Srm160521 	 * the reservation property to the post-rollback volsize.
36595713Srm160521 	 * Make a new handle since the rollback closed the dataset.
36605713Srm160521 	 */
36615717Srm160521 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
36625717Srm160521 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
36635717Srm160521 		if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
36645717Srm160521 			zfs_close(zhp);
36655713Srm160521 			return (err);
36665717Srm160521 		}
36675713Srm160521 		if (restore_resv) {
36685713Srm160521 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
36695713Srm160521 			if (old_volsize != new_volsize)
36705717Srm160521 				err = zfs_prop_set_int(zhp, resv_prop,
36715717Srm160521 				    new_volsize);
36725713Srm160521 		}
36735713Srm160521 		zfs_close(zhp);
3674789Sahrens 	}
36755446Sahrens 	return (err);
36761294Slling }
36771294Slling 
36781294Slling /*
3679789Sahrens  * Iterate over all dependents for a given dataset.  This includes both
3680789Sahrens  * hierarchical dependents (children) and data dependents (snapshots and
3681789Sahrens  * clones).  The bulk of the processing occurs in get_dependents() in
3682789Sahrens  * libzfs_graph.c.
3683789Sahrens  */
3684789Sahrens int
36852474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
36862474Seschrock     zfs_iter_f func, void *data)
3687789Sahrens {
3688789Sahrens 	char **dependents;
3689789Sahrens 	size_t count;
3690789Sahrens 	int i;
3691789Sahrens 	zfs_handle_t *child;
3692789Sahrens 	int ret = 0;
3693789Sahrens 
36942474Seschrock 	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
36952474Seschrock 	    &dependents, &count) != 0)
36962474Seschrock 		return (-1);
36972474Seschrock 
3698789Sahrens 	for (i = 0; i < count; i++) {
36992082Seschrock 		if ((child = make_dataset_handle(zhp->zfs_hdl,
37002082Seschrock 		    dependents[i])) == NULL)
3701789Sahrens 			continue;
3702789Sahrens 
3703789Sahrens 		if ((ret = func(child, data)) != 0)
3704789Sahrens 			break;
3705789Sahrens 	}
3706789Sahrens 
3707789Sahrens 	for (i = 0; i < count; i++)
3708789Sahrens 		free(dependents[i]);
3709789Sahrens 	free(dependents);
3710789Sahrens 
3711789Sahrens 	return (ret);
3712789Sahrens }
3713789Sahrens 
3714789Sahrens /*
3715789Sahrens  * Renames the given dataset.
3716789Sahrens  */
3717789Sahrens int
37184490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3719789Sahrens {
3720789Sahrens 	int ret;
3721789Sahrens 	zfs_cmd_t zc = { 0 };
3722789Sahrens 	char *delim;
37234007Smmusante 	prop_changelist_t *cl = NULL;
37244007Smmusante 	zfs_handle_t *zhrp = NULL;
37254007Smmusante 	char *parentname = NULL;
3726789Sahrens 	char parent[ZFS_MAXNAMELEN];
37272082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
37282082Seschrock 	char errbuf[1024];
3729789Sahrens 
3730789Sahrens 	/* if we have the same exact name, just return success */
3731789Sahrens 	if (strcmp(zhp->zfs_name, target) == 0)
3732789Sahrens 		return (0);
3733789Sahrens 
37342082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
37352082Seschrock 	    "cannot rename to '%s'"), target);
37362082Seschrock 
3737789Sahrens 	/*
3738789Sahrens 	 * Make sure the target name is valid
3739789Sahrens 	 */
3740789Sahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
37412665Snd150628 		if ((strchr(target, '@') == NULL) ||
37422665Snd150628 		    *target == '@') {
37432665Snd150628 			/*
37442665Snd150628 			 * Snapshot target name is abbreviated,
37452665Snd150628 			 * reconstruct full dataset name
37462665Snd150628 			 */
37472665Snd150628 			(void) strlcpy(parent, zhp->zfs_name,
37482665Snd150628 			    sizeof (parent));
37492665Snd150628 			delim = strchr(parent, '@');
37502665Snd150628 			if (strchr(target, '@') == NULL)
37512665Snd150628 				*(++delim) = '\0';
37522665Snd150628 			else
37532665Snd150628 				*delim = '\0';
37542665Snd150628 			(void) strlcat(parent, target, sizeof (parent));
37552665Snd150628 			target = parent;
37562665Snd150628 		} else {
37572665Snd150628 			/*
37582665Snd150628 			 * Make sure we're renaming within the same dataset.
37592665Snd150628 			 */
37602665Snd150628 			delim = strchr(target, '@');
37612665Snd150628 			if (strncmp(zhp->zfs_name, target, delim - target)
37622665Snd150628 			    != 0 || zhp->zfs_name[delim - target] != '@') {
37632665Snd150628 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37642665Snd150628 				    "snapshots must be part of same "
37652665Snd150628 				    "dataset"));
37662665Snd150628 				return (zfs_error(hdl, EZFS_CROSSTARGET,
37673912Slling 				    errbuf));
37682665Snd150628 			}
3769789Sahrens 		}
37705326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37712665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3772789Sahrens 	} else {
37734007Smmusante 		if (recursive) {
37744007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37754007Smmusante 			    "recursive rename must be a snapshot"));
37764007Smmusante 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
37774007Smmusante 		}
37784007Smmusante 
37795326Sek110237 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
37802665Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
37812676Seschrock 		uint64_t unused;
37822676Seschrock 
3783789Sahrens 		/* validate parents */
37844490Svb160487 		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3785789Sahrens 			return (-1);
3786789Sahrens 
3787789Sahrens 		(void) parent_name(target, parent, sizeof (parent));
3788789Sahrens 
3789789Sahrens 		/* make sure we're in the same pool */
3790789Sahrens 		verify((delim = strchr(target, '/')) != NULL);
3791789Sahrens 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3792789Sahrens 		    zhp->zfs_name[delim - target] != '/') {
37932082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37942082Seschrock 			    "datasets must be within same pool"));
37952082Seschrock 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3796789Sahrens 		}
37972440Snd150628 
37982440Snd150628 		/* new name cannot be a child of the current dataset name */
37992440Snd150628 		if (strncmp(parent, zhp->zfs_name,
38003912Slling 		    strlen(zhp->zfs_name)) == 0) {
38012440Snd150628 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38022440Snd150628 			    "New dataset name cannot be a descendent of "
38032440Snd150628 			    "current dataset name"));
38042440Snd150628 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
38052440Snd150628 		}
3806789Sahrens 	}
3807789Sahrens 
38082082Seschrock 	(void) snprintf(errbuf, sizeof (errbuf),
38092082Seschrock 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
38102082Seschrock 
3811789Sahrens 	if (getzoneid() == GLOBAL_ZONEID &&
3812789Sahrens 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
38132082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38142082Seschrock 		    "dataset is used in a non-global zone"));
38152082Seschrock 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
3816789Sahrens 	}
3817789Sahrens 
38184007Smmusante 	if (recursive) {
38194007Smmusante 		struct destroydata dd;
38204007Smmusante 
38214183Smmusante 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
38224183Smmusante 		if (parentname == NULL) {
38234183Smmusante 			ret = -1;
38244183Smmusante 			goto error;
38254183Smmusante 		}
38264007Smmusante 		delim = strchr(parentname, '@');
38274007Smmusante 		*delim = '\0';
38285094Slling 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
38294007Smmusante 		if (zhrp == NULL) {
38304183Smmusante 			ret = -1;
38314183Smmusante 			goto error;
38324007Smmusante 		}
38334007Smmusante 
38344007Smmusante 		dd.snapname = delim + 1;
38354007Smmusante 		dd.gotone = B_FALSE;
38364183Smmusante 		dd.closezhp = B_TRUE;
38374007Smmusante 
38384007Smmusante 		/* We remove any zvol links prior to renaming them */
38394007Smmusante 		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
38404007Smmusante 		if (ret) {
38414007Smmusante 			goto error;
38424007Smmusante 		}
38434007Smmusante 	} else {
38444007Smmusante 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
38454007Smmusante 			return (-1);
38464007Smmusante 
38474007Smmusante 		if (changelist_haszonedchild(cl)) {
38484007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38494007Smmusante 			    "child dataset with inherited mountpoint is used "
38504007Smmusante 			    "in a non-global zone"));
38514007Smmusante 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
38524007Smmusante 			goto error;
38534007Smmusante 		}
38544007Smmusante 
38554007Smmusante 		if ((ret = changelist_prefix(cl)) != 0)
38564007Smmusante 			goto error;
3857789Sahrens 	}
3858789Sahrens 
38592676Seschrock 	if (ZFS_IS_VOLUME(zhp))
3860789Sahrens 		zc.zc_objset_type = DMU_OST_ZVOL;
3861789Sahrens 	else
3862789Sahrens 		zc.zc_objset_type = DMU_OST_ZFS;
3863789Sahrens 
38642665Snd150628 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
38652676Seschrock 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
38662665Snd150628 
38674007Smmusante 	zc.zc_cookie = recursive;
38684007Smmusante 
38694543Smarks 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
38704007Smmusante 		/*
38714007Smmusante 		 * if it was recursive, the one that actually failed will
38724007Smmusante 		 * be in zc.zc_name
38734007Smmusante 		 */
38744007Smmusante 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38755367Sahrens 		    "cannot rename '%s'"), zc.zc_name);
38764007Smmusante 
38774007Smmusante 		if (recursive && errno == EEXIST) {
38784007Smmusante 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38794007Smmusante 			    "a child dataset already has a snapshot "
38804007Smmusante 			    "with the new name"));
38814801Seschrock 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
38824007Smmusante 		} else {
38834007Smmusante 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
38844007Smmusante 		}
3885789Sahrens 
3886789Sahrens 		/*
3887789Sahrens 		 * On failure, we still want to remount any filesystems that
3888789Sahrens 		 * were previously mounted, so we don't alter the system state.
3889789Sahrens 		 */
38904007Smmusante 		if (recursive) {
38914007Smmusante 			struct createdata cd;
38924007Smmusante 
38934007Smmusante 			/* only create links for datasets that had existed */
38944007Smmusante 			cd.cd_snapname = delim + 1;
38954007Smmusante 			cd.cd_ifexists = B_TRUE;
38964007Smmusante 			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
38974007Smmusante 			    &cd);
38984007Smmusante 		} else {
38994007Smmusante 			(void) changelist_postfix(cl);
39004007Smmusante 		}
3901789Sahrens 	} else {
39024007Smmusante 		if (recursive) {
39034007Smmusante 			struct createdata cd;
39044007Smmusante 
39054007Smmusante 			/* only create links for datasets that had existed */
39064007Smmusante 			cd.cd_snapname = strchr(target, '@') + 1;
39074007Smmusante 			cd.cd_ifexists = B_TRUE;
39084007Smmusante 			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
39094007Smmusante 			    &cd);
39104007Smmusante 		} else {
39114007Smmusante 			changelist_rename(cl, zfs_get_name(zhp), target);
39124007Smmusante 			ret = changelist_postfix(cl);
39134007Smmusante 		}
3914789Sahrens 	}
3915789Sahrens 
3916789Sahrens error:
39174007Smmusante 	if (parentname) {
39184007Smmusante 		free(parentname);
39194007Smmusante 	}
39204007Smmusante 	if (zhrp) {
39214007Smmusante 		zfs_close(zhrp);
39224007Smmusante 	}
39234007Smmusante 	if (cl) {
39244007Smmusante 		changelist_free(cl);
39254007Smmusante 	}
3926789Sahrens 	return (ret);
3927789Sahrens }
3928789Sahrens 
3929789Sahrens /*
3930789Sahrens  * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3931789Sahrens  * poke devfsadm to create the /dev link, and then wait for the link to appear.
3932789Sahrens  */
3933789Sahrens int
39342082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3935789Sahrens {
39364007Smmusante 	return (zvol_create_link_common(hdl, dataset, B_FALSE));
39374007Smmusante }
39384007Smmusante 
39394007Smmusante static int
39404007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
39414007Smmusante {
3942789Sahrens 	zfs_cmd_t zc = { 0 };
39432082Seschrock 	di_devlink_handle_t dhdl;
39444543Smarks 	priv_set_t *priv_effective;
39454543Smarks 	int privileged;
3946789Sahrens 
3947789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3948789Sahrens 
3949789Sahrens 	/*
3950789Sahrens 	 * Issue the appropriate ioctl.
3951789Sahrens 	 */
39522082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3953789Sahrens 		switch (errno) {
3954789Sahrens 		case EEXIST:
3955789Sahrens 			/*
3956789Sahrens 			 * Silently ignore the case where the link already
3957789Sahrens 			 * exists.  This allows 'zfs volinit' to be run multiple
3958789Sahrens 			 * times without errors.
3959789Sahrens 			 */
3960789Sahrens 			return (0);
3961789Sahrens 
39624007Smmusante 		case ENOENT:
39634007Smmusante 			/*
39644007Smmusante 			 * Dataset does not exist in the kernel.  If we
39654007Smmusante 			 * don't care (see zfs_rename), then ignore the
39664007Smmusante 			 * error quietly.
39674007Smmusante 			 */
39684007Smmusante 			if (ifexists) {
39694007Smmusante 				return (0);
39704007Smmusante 			}
39714007Smmusante 
39724007Smmusante 			/* FALLTHROUGH */
39734007Smmusante 
3974789Sahrens 		default:
39753237Slling 			return (zfs_standard_error_fmt(hdl, errno,
39762082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39772082Seschrock 			    "for '%s'"), dataset));
3978789Sahrens 		}
3979789Sahrens 	}
3980789Sahrens 
3981789Sahrens 	/*
39824543Smarks 	 * If privileged call devfsadm and wait for the links to
39834543Smarks 	 * magically appear.
39844543Smarks 	 * Otherwise, print out an informational message.
3985789Sahrens 	 */
39864543Smarks 
39874543Smarks 	priv_effective = priv_allocset();
39884543Smarks 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
39894543Smarks 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
39904543Smarks 	priv_freeset(priv_effective);
39914543Smarks 
39924543Smarks 	if (privileged) {
39934543Smarks 		if ((dhdl = di_devlink_init(ZFS_DRIVER,
39944543Smarks 		    DI_MAKE_LINK)) == NULL) {
39954543Smarks 			zfs_error_aux(hdl, strerror(errno));
39964543Smarks 			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
39974543Smarks 			    dgettext(TEXT_DOMAIN, "cannot create device links "
39984543Smarks 			    "for '%s'"), dataset);
39994543Smarks 			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
40004543Smarks 			return (-1);
40014543Smarks 		} else {
40024543Smarks 			(void) di_devlink_fini(&dhdl);
40034543Smarks 		}
4004789Sahrens 	} else {
40054543Smarks 		char pathname[MAXPATHLEN];
40064543Smarks 		struct stat64 statbuf;
40074543Smarks 		int i;
40084543Smarks 
40094543Smarks #define	MAX_WAIT	10
40104543Smarks 
40114543Smarks 		/*
40124543Smarks 		 * This is the poor mans way of waiting for the link
40134543Smarks 		 * to show up.  If after 10 seconds we still don't
40144543Smarks 		 * have it, then print out a message.
40154543Smarks 		 */
40164543Smarks 		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
40174543Smarks 		    dataset);
40184543Smarks 
40194543Smarks 		for (i = 0; i != MAX_WAIT; i++) {
40204543Smarks 			if (stat64(pathname, &statbuf) == 0)
40214543Smarks 				break;
40224543Smarks 			(void) sleep(1);
40234543Smarks 		}
40244543Smarks 		if (i == MAX_WAIT)
40254543Smarks 			(void) printf(gettext("%s may not be immediately "
40264543Smarks 			    "available\n"), pathname);
4027789Sahrens 	}
4028789Sahrens 
4029789Sahrens 	return (0);
4030789Sahrens }
4031789Sahrens 
4032789Sahrens /*
4033789Sahrens  * Remove a minor node for the given zvol and the associated /dev links.
4034789Sahrens  */
4035789Sahrens int
40362082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4037789Sahrens {
4038789Sahrens 	zfs_cmd_t zc = { 0 };
4039789Sahrens 
4040789Sahrens 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4041789Sahrens 
40422082Seschrock 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
4043789Sahrens 		switch (errno) {
4044789Sahrens 		case ENXIO:
4045789Sahrens 			/*
4046789Sahrens 			 * Silently ignore the case where the link no longer
4047789Sahrens 			 * exists, so that 'zfs volfini' can be run multiple
4048789Sahrens 			 * times without errors.
4049789Sahrens 			 */
4050789Sahrens 			return (0);
4051789Sahrens 
4052789Sahrens 		default:
40533237Slling 			return (zfs_standard_error_fmt(hdl, errno,
40542082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot remove device "
40552082Seschrock 			    "links for '%s'"), dataset));
4056789Sahrens 		}
4057789Sahrens 	}
4058789Sahrens 
4059789Sahrens 	return (0);
4060789Sahrens }
40612676Seschrock 
40622676Seschrock nvlist_t *
40632676Seschrock zfs_get_user_props(zfs_handle_t *zhp)
40642676Seschrock {
40652676Seschrock 	return (zhp->zfs_user_props);
40662676Seschrock }
40672676Seschrock 
40682676Seschrock /*
40693912Slling  * This function is used by 'zfs list' to determine the exact set of columns to
40703912Slling  * display, and their maximum widths.  This does two main things:
40713912Slling  *
40723912Slling  *      - If this is a list of all properties, then expand the list to include
40733912Slling  *        all native properties, and set a flag so that for each dataset we look
40743912Slling  *        for new unique user properties and add them to the list.
40753912Slling  *
40763912Slling  *      - For non fixed-width properties, keep track of the maximum width seen
40773912Slling  *        so that we can size the column appropriately.
40783912Slling  */
40793912Slling int
40805094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
40813912Slling {
40823912Slling 	libzfs_handle_t *hdl = zhp->zfs_hdl;
40835094Slling 	zprop_list_t *entry;
40845094Slling 	zprop_list_t **last, **start;
40853912Slling 	nvlist_t *userprops, *propval;
40863912Slling 	nvpair_t *elem;
40873912Slling 	char *strval;
40883912Slling 	char buf[ZFS_MAXPROPLEN];
40893912Slling 
40905094Slling 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
40913912Slling 		return (-1);
40922676Seschrock 
40932676Seschrock 	userprops = zfs_get_user_props(zhp);
40942676Seschrock 
40952676Seschrock 	entry = *plp;
40962676Seschrock 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
40972676Seschrock 		/*
40982676Seschrock 		 * Go through and add any user properties as necessary.  We
40992676Seschrock 		 * start by incrementing our list pointer to the first
41002676Seschrock 		 * non-native property.
41012676Seschrock 		 */
41022676Seschrock 		start = plp;
41032676Seschrock 		while (*start != NULL) {
41045094Slling 			if ((*start)->pl_prop == ZPROP_INVAL)
41052676Seschrock 				break;
41062676Seschrock 			start = &(*start)->pl_next;
41072676Seschrock 		}
41082676Seschrock 
41092676Seschrock 		elem = NULL;
41102676Seschrock 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
41112676Seschrock 			/*
41122676Seschrock 			 * See if we've already found this property in our list.
41132676Seschrock 			 */
41142676Seschrock 			for (last = start; *last != NULL;
41152676Seschrock 			    last = &(*last)->pl_next) {
41162676Seschrock 				if (strcmp((*last)->pl_user_prop,
41172676Seschrock 				    nvpair_name(elem)) == 0)
41182676Seschrock 					break;
41192676Seschrock 			}
41202676Seschrock 
41212676Seschrock 			if (*last == NULL) {
41222676Seschrock 				if ((entry = zfs_alloc(hdl,
41235094Slling 				    sizeof (zprop_list_t))) == NULL ||
41242676Seschrock 				    ((entry->pl_user_prop = zfs_strdup(hdl,
41252676Seschrock 				    nvpair_name(elem)))) == NULL) {
41262676Seschrock 					free(entry);
41272676Seschrock 					return (-1);
41282676Seschrock 				}
41292676Seschrock 
41305094Slling 				entry->pl_prop = ZPROP_INVAL;
41312676Seschrock 				entry->pl_width = strlen(nvpair_name(elem));
41322676Seschrock 				entry->pl_all = B_TRUE;
41332676Seschrock 				*last = entry;
41342676Seschrock 			}
41352676Seschrock 		}
41362676Seschrock 	}
41372676Seschrock 
41382676Seschrock 	/*
41392676Seschrock 	 * Now go through and check the width of any non-fixed columns
41402676Seschrock 	 */
41412676Seschrock 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
41422676Seschrock 		if (entry->pl_fixed)
41432676Seschrock 			continue;
41442676Seschrock 
41455094Slling 		if (entry->pl_prop != ZPROP_INVAL) {
41462676Seschrock 			if (zfs_prop_get(zhp, entry->pl_prop,
41472676Seschrock 			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
41482676Seschrock 				if (strlen(buf) > entry->pl_width)
41492676Seschrock 					entry->pl_width = strlen(buf);
41502676Seschrock 			}
41512676Seschrock 		} else if (nvlist_lookup_nvlist(userprops,
41522676Seschrock 		    entry->pl_user_prop, &propval)  == 0) {
41532676Seschrock 			verify(nvlist_lookup_string(propval,
41545094Slling 			    ZPROP_VALUE, &strval) == 0);
41552676Seschrock 			if (strlen(strval) > entry->pl_width)
41562676Seschrock 				entry->pl_width = strlen(strval);
41572676Seschrock 		}
41582676Seschrock 	}
41592676Seschrock 
41602676Seschrock 	return (0);
41612676Seschrock }
41624543Smarks 
41634543Smarks int
41644543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
41654543Smarks {
41664543Smarks 	zfs_cmd_t zc = { 0 };
41674543Smarks 	nvlist_t *nvp;
41684543Smarks 	gid_t gid;
41694543Smarks 	uid_t uid;
41704543Smarks 	const gid_t *groups;
41714543Smarks 	int group_cnt;
41724543Smarks 	int error;
41734543Smarks 
41744543Smarks 	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
41754543Smarks 		return (no_memory(hdl));
41764543Smarks 
41774543Smarks 	uid = ucred_geteuid(cred);
41784543Smarks 	gid = ucred_getegid(cred);
41794543Smarks 	group_cnt = ucred_getgroups(cred, &groups);
41804543Smarks 
41814543Smarks 	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
41824543Smarks 		return (1);
41834543Smarks 
41844543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
41854543Smarks 		nvlist_free(nvp);
41864543Smarks 		return (1);
41874543Smarks 	}
41884543Smarks 
41894543Smarks 	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
41904543Smarks 		nvlist_free(nvp);
41914543Smarks 		return (1);
41924543Smarks 	}
41934543Smarks 
41944543Smarks 	if (nvlist_add_uint32_array(nvp,
41954543Smarks 	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
41964543Smarks 		nvlist_free(nvp);
41974543Smarks 		return (1);
41984543Smarks 	}
41994543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42004543Smarks 
42015094Slling 	if (zcmd_write_src_nvlist(hdl, &zc, nvp))
42024543Smarks 		return (-1);
42034543Smarks 
42044543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
42054543Smarks 	nvlist_free(nvp);
42064543Smarks 	return (error);
42074543Smarks }
42084543Smarks 
42094543Smarks int
42104543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
42115331Samw     void *export, void *sharetab, int sharemax, zfs_share_op_t operation)
42124543Smarks {
42134543Smarks 	zfs_cmd_t zc = { 0 };
42144543Smarks 	int error;
42154543Smarks 
42164543Smarks 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
42174543Smarks 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
42184543Smarks 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
42194543Smarks 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
42205331Samw 	zc.zc_share.z_sharetype = operation;
42214543Smarks 	zc.zc_share.z_sharemax = sharemax;
42224543Smarks 
42234543Smarks 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
42244543Smarks 	return (error);
42254543Smarks }
4226